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