libnl: /data/src/libnl/lib/netfilter/log_obj.c Source File

libnl 3.12.0

log_obj.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
4 * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
5 * Copyright (c) 2007 Secure Computing Corporation
6 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
7 */
8
9#include "nl-default.h"
10
11#include <netlink/netfilter/nfnl.h>
12#include <netlink/netfilter/log.h>
13
14#include "nl-priv-dynamic-core/object-api.h"
15#include "nl-priv-dynamic-core/cache-api.h"
16#include "nl-priv-dynamic-core/nl-core.h"
17
18/** @cond SKIP */
19struct nfnl_log {
20 NLHDR_COMMON
21
22 uint16_t log_group;
23 uint8_t log_copy_mode;
24 uint32_t log_copy_range;
25 uint32_t log_flush_timeout;
26 uint32_t log_alloc_size;
27 uint32_t log_queue_threshold;
28 uint32_t log_flags;
29 uint32_t log_flag_mask;
30};
31
32#define LOG_ATTR_GROUP (1UL << 0)
33#define LOG_ATTR_COPY_MODE (1UL << 1)
34#define LOG_ATTR_COPY_RANGE (1UL << 3)
35#define LOG_ATTR_FLUSH_TIMEOUT (1UL << 4)
36#define LOG_ATTR_ALLOC_SIZE (1UL << 5)
37#define LOG_ATTR_QUEUE_THRESHOLD (1UL << 6)
38
39/** @endcond */
40
41static void nfnl_log_dump(struct nl_object *a, struct nl_dump_params *p)
42{
43 struct nfnl_log *log = (struct nfnl_log *) a;
44 char buf[64];
45
46 nl_new_line(p);
47
48 if (log->ce_mask & LOG_ATTR_GROUP)
49 nl_dump(p, "group=%u ", log->log_group);
50
51 if (log->ce_mask & LOG_ATTR_COPY_MODE)
52 nl_dump(p, "copy_mode=%s ",
53 nfnl_log_copy_mode2str(log->log_copy_mode,
54 buf, sizeof(buf)));
55
56 if (log->ce_mask & LOG_ATTR_COPY_RANGE)
57 nl_dump(p, "copy_range=%u ", log->log_copy_range);
58
59 if (log->ce_mask & LOG_ATTR_FLUSH_TIMEOUT)
60 nl_dump(p, "flush_timeout=%u ", log->log_flush_timeout);
61
62 if (log->ce_mask & LOG_ATTR_ALLOC_SIZE)
63 nl_dump(p, "alloc_size=%u ", log->log_alloc_size);
64
65 if (log->ce_mask & LOG_ATTR_QUEUE_THRESHOLD)
66 nl_dump(p, "queue_threshold=%u ", log->log_queue_threshold);
67
68 nl_dump(p, "\n");
69}
70
71static const struct trans_tbl copy_modes[] = {
72 __ADD(NFNL_LOG_COPY_NONE, none),
73 __ADD(NFNL_LOG_COPY_META, meta),
74 __ADD(NFNL_LOG_COPY_PACKET, packet),
75};
76
77char *nfnl_log_copy_mode2str(enum nfnl_log_copy_mode copy_mode, char *buf,
78 size_t len)
79{
80 return __type2str(copy_mode, buf, len, copy_modes,
81 ARRAY_SIZE(copy_modes));
82}
83
84int nfnl_log_str2copy_mode(const char *name)
85{
86 return __str2type(name, copy_modes, ARRAY_SIZE(copy_modes));
87}
88
89/**
90 * @name Allocation/Freeing
91 * @{
92 */
93
94struct nfnl_log *nfnl_log_alloc(void)
95{
96 return (struct nfnl_log *) nl_object_alloc(&log_obj_ops);
97}
98
99void nfnl_log_get(struct nfnl_log *log)
100{
101 nl_object_get((struct nl_object *) log);
102}
103
104void nfnl_log_put(struct nfnl_log *log)
105{
106 nl_object_put((struct nl_object *) log);
107}
108
109/** @} */
110
111/**
112 * @name Attributes
113 * @{
114 */
115
116void nfnl_log_set_group(struct nfnl_log *log, uint16_t group)
117{
118 log->log_group = group;
119 log->ce_mask |= LOG_ATTR_GROUP;
120}
121
122int nfnl_log_test_group(const struct nfnl_log *log)
123{
124 return !!(log->ce_mask & LOG_ATTR_GROUP);
125}
126
127uint16_t nfnl_log_get_group(const struct nfnl_log *log)
128{
129 return log->log_group;
130}
131
132void nfnl_log_set_copy_mode(struct nfnl_log *log, enum nfnl_log_copy_mode mode)
133{
134 log->log_copy_mode = mode;
135 log->ce_mask |= LOG_ATTR_COPY_MODE;
136}
137
138int nfnl_log_test_copy_mode(const struct nfnl_log *log)
139{
140 return !!(log->ce_mask & LOG_ATTR_COPY_MODE);
141}
142
143enum nfnl_log_copy_mode nfnl_log_get_copy_mode(const struct nfnl_log *log)
144{
145 return log->log_copy_mode;
146}
147
148void nfnl_log_set_copy_range(struct nfnl_log *log, uint32_t copy_range)
149{
150 log->log_copy_range = copy_range;
151 log->ce_mask |= LOG_ATTR_COPY_RANGE;
152}
153
154int nfnl_log_test_copy_range(const struct nfnl_log *log)
155{
156 return !!(log->ce_mask & LOG_ATTR_COPY_RANGE);
157}
158
159uint32_t nfnl_log_get_copy_range(const struct nfnl_log *log)
160{
161 return log->log_copy_range;
162}
163
164void nfnl_log_set_flush_timeout(struct nfnl_log *log, uint32_t timeout)
165{
166 log->log_flush_timeout = timeout;
167 log->ce_mask |= LOG_ATTR_FLUSH_TIMEOUT;
168}
169
170int nfnl_log_test_flush_timeout(const struct nfnl_log *log)
171{
172 return !!(log->ce_mask & LOG_ATTR_FLUSH_TIMEOUT);
173}
174
175uint32_t nfnl_log_get_flush_timeout(const struct nfnl_log *log)
176{
177 return log->log_flush_timeout;
178}
179
180void nfnl_log_set_alloc_size(struct nfnl_log *log, uint32_t alloc_size)
181{
182 log->log_alloc_size = alloc_size;
183 log->ce_mask |= LOG_ATTR_ALLOC_SIZE;
184}
185
186int nfnl_log_test_alloc_size(const struct nfnl_log *log)
187{
188 return !!(log->ce_mask & LOG_ATTR_ALLOC_SIZE);
189}
190
191uint32_t nfnl_log_get_alloc_size(const struct nfnl_log *log)
192{
193 return log->log_alloc_size;
194}
195
196void nfnl_log_set_queue_threshold(struct nfnl_log *log, uint32_t threshold)
197{
198 log->log_queue_threshold = threshold;
199 log->ce_mask |= LOG_ATTR_QUEUE_THRESHOLD;
200}
201
202int nfnl_log_test_queue_threshold(const struct nfnl_log *log)
203{
204 return !!(log->ce_mask & LOG_ATTR_QUEUE_THRESHOLD);
205}
206
207uint32_t nfnl_log_get_queue_threshold(const struct nfnl_log *log)
208{
209 return log->log_queue_threshold;
210}
211
212/* We don't actually use the flags for anything yet since the
213 * nfnetlog_log interface truly sucks - it only contains the
214 * flag value, but not mask, so we would have to make assumptions
215 * about the supported flags.
216 */
217void nfnl_log_set_flags(struct nfnl_log *log, unsigned int flags)
218{
219 log->log_flags |= flags;
220 log->log_flag_mask |= flags;
221}
222
223void nfnl_log_unset_flags(struct nfnl_log *log, unsigned int flags)
224{
225 log->log_flags &= ~flags;
226 log->log_flag_mask |= flags;
227}
228
229unsigned int nfnl_log_get_flags(const struct nfnl_log *log)
230{
231 return log->log_flags;
232}
233
234static const struct trans_tbl log_flags[] = {
235 __ADD(NFNL_LOG_FLAG_SEQ, seq),
236 __ADD(NFNL_LOG_FLAG_SEQ_GLOBAL, seq_global),
237 __ADD(NFNL_LOG_FLAG_CONNTRACK, conntrack),
238};
239
240char *nfnl_log_flags2str(unsigned int flags, char *buf, size_t len)
241{
242 return __flags2str(flags, buf, len, log_flags, ARRAY_SIZE(log_flags));
243}
244
245unsigned int nfnl_log_str2flags(const char *name)
246{
247 return __str2flags(name, log_flags, ARRAY_SIZE(log_flags));
248}
249
250static uint64_t nfnl_log_compare(struct nl_object *_a, struct nl_object *_b,
251 uint64_t attrs, int flags)
252{
253 struct nfnl_log *a = (struct nfnl_log *) _a;
254 struct nfnl_log *b = (struct nfnl_log *) _b;
255 uint64_t diff = 0;
256
257#define NFNL_LOG_DIFF(ATTR, EXPR) \
258 ATTR_DIFF(attrs, ATTR, a, b, EXPR)
259#define NFNL_LOG_DIFF_VAL(ATTR, FIELD) \
260 NFNL_LOG_DIFF(ATTR, a->FIELD != b->FIELD)
261 diff |= NFNL_LOG_DIFF_VAL(LOG_ATTR_GROUP, log_group);
262 diff |= NFNL_LOG_DIFF_VAL(LOG_ATTR_COPY_MODE, log_copy_mode);
263 diff |= NFNL_LOG_DIFF_VAL(LOG_ATTR_COPY_RANGE, log_copy_range);
264 diff |= NFNL_LOG_DIFF_VAL(LOG_ATTR_FLUSH_TIMEOUT, log_flush_timeout);
265 diff |= NFNL_LOG_DIFF_VAL(LOG_ATTR_ALLOC_SIZE, log_alloc_size);
266 diff |= NFNL_LOG_DIFF_VAL(LOG_ATTR_QUEUE_THRESHOLD, log_queue_threshold);
267#undef NFNL_LOG_DIFF
268#undef NFNL_LOG_DIFF_VAL
269
270 return diff;
271}
272
273static const struct trans_tbl nfnl_log_attrs[] = {
274 __ADD(LOG_ATTR_GROUP, group),
275 __ADD(LOG_ATTR_COPY_MODE, copy_mode),
276 __ADD(LOG_ATTR_COPY_RANGE, copy_range),
277 __ADD(LOG_ATTR_FLUSH_TIMEOUT, flush_timeout),
278 __ADD(LOG_ATTR_ALLOC_SIZE, alloc_size),
279 __ADD(LOG_ATTR_QUEUE_THRESHOLD, queue_threshold),
280};
281
282static char *nfnl_log_attrs2str(int attrs, char *buf, size_t len)
283{
284 return __flags2str(attrs, buf, len, nfnl_log_attrs,
285 ARRAY_SIZE(nfnl_log_attrs));
286}
287
288/** @} */
289
290struct nl_object_ops log_obj_ops = {
291 .oo_name = "netfilter/log",
292 .oo_size = sizeof(struct nfnl_log),
293 .oo_dump = {
294 [NL_DUMP_LINE] = nfnl_log_dump,
295 [NL_DUMP_DETAILS] = nfnl_log_dump,
296 [NL_DUMP_STATS] = nfnl_log_dump,
297 },
298 .oo_compare = nfnl_log_compare,
299 .oo_attrs2str = nfnl_log_attrs2str,
300 .oo_id_attrs = LOG_ATTR_GROUP,
301};
302
303/** @} */
nl_object_put
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition object.c:221
nl_object_get
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition object.c:210
nl_object_alloc
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
Definition object.c:55
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition utils.c:1015
nl_new_line
void nl_new_line(struct nl_dump_params *params)
Handle a new line while dumping.
Definition utils.c:966
NL_DUMP_STATS
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition types.h:22
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
NL_DUMP_DETAILS
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
nl_dump_params
Dumping parameters.
Definition types.h:32