libnl 3.5.0

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