1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * lib/netfilter/queue_msg_obj.c Netfilter Queue Message 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) 2007, 2008 Patrick McHardy <kaber@trash.net>
11 */
12
13 #include <netlink-private/netlink.h>
14 #include <netlink/netfilter/nfnl.h>
15 #include <netlink/netfilter/netfilter.h>
16 #include <netlink/netfilter/queue_msg.h>
17 #include <linux/netfilter.h>
18
19 /** @cond SKIP */
20 #define QUEUE_MSG_ATTR_GROUP (1UL << 0)
21 #define QUEUE_MSG_ATTR_FAMILY (1UL << 1)
22 #define QUEUE_MSG_ATTR_PACKETID (1UL << 2)
23 #define QUEUE_MSG_ATTR_HWPROTO (1UL << 3)
24 #define QUEUE_MSG_ATTR_HOOK (1UL << 4)
25 #define QUEUE_MSG_ATTR_MARK (1UL << 5)
26 #define QUEUE_MSG_ATTR_TIMESTAMP (1UL << 6)
27 #define QUEUE_MSG_ATTR_INDEV (1UL << 7)
28 #define QUEUE_MSG_ATTR_OUTDEV (1UL << 8)
29 #define QUEUE_MSG_ATTR_PHYSINDEV (1UL << 9)
30 #define QUEUE_MSG_ATTR_PHYSOUTDEV (1UL << 10)
31 #define QUEUE_MSG_ATTR_HWADDR (1UL << 11)
32 #define QUEUE_MSG_ATTR_PAYLOAD (1UL << 12)
33 #define QUEUE_MSG_ATTR_VERDICT (1UL << 13)
34 /** @endcond */
35
nfnl_queue_msg_free_data(struct nl_object * c)36 static void nfnl_queue_msg_free_data(struct nl_object *c)
37 {
38 struct nfnl_queue_msg *msg = (struct nfnl_queue_msg *) c;
39
40 if (msg == NULL)
41 return;
42
43 free(msg->queue_msg_payload);
44 }
45
nfnl_queue_msg_clone(struct nl_object * _dst,struct nl_object * _src)46 static int nfnl_queue_msg_clone(struct nl_object *_dst, struct nl_object *_src)
47 {
48 struct nfnl_queue_msg *dst = (struct nfnl_queue_msg *) _dst;
49 struct nfnl_queue_msg *src = (struct nfnl_queue_msg *) _src;
50 int err;
51
52 if (src->queue_msg_payload) {
53 err = nfnl_queue_msg_set_payload(dst, src->queue_msg_payload,
54 src->queue_msg_payload_len);
55 if (err < 0)
56 goto errout;
57 }
58
59 return 0;
60 errout:
61 return err;
62 }
63
nfnl_queue_msg_dump(struct nl_object * a,struct nl_dump_params * p)64 static void nfnl_queue_msg_dump(struct nl_object *a, struct nl_dump_params *p)
65 {
66 struct nfnl_queue_msg *msg = (struct nfnl_queue_msg *) a;
67 struct nl_cache *link_cache;
68 char buf[64];
69
70 link_cache = nl_cache_mngt_require_safe("route/link");
71
72 nl_new_line(p);
73
74 if (msg->ce_mask & QUEUE_MSG_ATTR_GROUP)
75 nl_dump(p, "GROUP=%u ", msg->queue_msg_group);
76
77 if (msg->ce_mask & QUEUE_MSG_ATTR_INDEV) {
78 if (link_cache)
79 nl_dump(p, "IN=%s ",
80 rtnl_link_i2name(link_cache,
81 msg->queue_msg_indev,
82 buf, sizeof(buf)));
83 else
84 nl_dump(p, "IN=%d ", msg->queue_msg_indev);
85 }
86
87 if (msg->ce_mask & QUEUE_MSG_ATTR_PHYSINDEV) {
88 if (link_cache)
89 nl_dump(p, "PHYSIN=%s ",
90 rtnl_link_i2name(link_cache,
91 msg->queue_msg_physindev,
92 buf, sizeof(buf)));
93 else
94 nl_dump(p, "IN=%d ", msg->queue_msg_physindev);
95 }
96
97 if (msg->ce_mask & QUEUE_MSG_ATTR_OUTDEV) {
98 if (link_cache)
99 nl_dump(p, "OUT=%s ",
100 rtnl_link_i2name(link_cache,
101 msg->queue_msg_outdev,
102 buf, sizeof(buf)));
103 else
104 nl_dump(p, "OUT=%d ", msg->queue_msg_outdev);
105 }
106
107 if (msg->ce_mask & QUEUE_MSG_ATTR_PHYSOUTDEV) {
108 if (link_cache)
109 nl_dump(p, "PHYSOUT=%s ",
110 rtnl_link_i2name(link_cache,
111 msg->queue_msg_physoutdev,
112 buf, sizeof(buf)));
113 else
114 nl_dump(p, "PHYSOUT=%d ", msg->queue_msg_physoutdev);
115 }
116
117 if (msg->ce_mask & QUEUE_MSG_ATTR_HWADDR) {
118 int i;
119
120 nl_dump(p, "MAC");
121 for (i = 0; i < msg->queue_msg_hwaddr_len; i++)
122 nl_dump(p, "%c%02x", i?':':'=',
123 msg->queue_msg_hwaddr[i]);
124 nl_dump(p, " ");
125 }
126
127 if (msg->ce_mask & QUEUE_MSG_ATTR_FAMILY)
128 nl_dump(p, "FAMILY=%s ",
129 nl_af2str(msg->queue_msg_family, buf, sizeof(buf)));
130
131 if (msg->ce_mask & QUEUE_MSG_ATTR_HWPROTO)
132 nl_dump(p, "HWPROTO=%s ",
133 nl_ether_proto2str(ntohs(msg->queue_msg_hwproto),
134 buf, sizeof(buf)));
135
136 if (msg->ce_mask & QUEUE_MSG_ATTR_HOOK)
137 nl_dump(p, "HOOK=%s ",
138 nfnl_inet_hook2str(msg->queue_msg_hook,
139 buf, sizeof(buf)));
140
141 if (msg->ce_mask & QUEUE_MSG_ATTR_MARK)
142 nl_dump(p, "MARK=%d ", msg->queue_msg_mark);
143
144 if (msg->ce_mask & QUEUE_MSG_ATTR_PAYLOAD)
145 nl_dump(p, "PAYLOADLEN=%d ", msg->queue_msg_payload_len);
146
147 if (msg->ce_mask & QUEUE_MSG_ATTR_PACKETID)
148 nl_dump(p, "PACKETID=%u ", msg->queue_msg_packetid);
149
150 if (msg->ce_mask & QUEUE_MSG_ATTR_VERDICT)
151 nl_dump(p, "VERDICT=%s ",
152 nfnl_verdict2str(msg->queue_msg_verdict,
153 buf, sizeof(buf)));
154
155 nl_dump(p, "\n");
156
157 if (link_cache)
158 nl_cache_put(link_cache);
159 }
160
161 /**
162 * @name Allocation/Freeing
163 * @{
164 */
165
nfnl_queue_msg_alloc(void)166 struct nfnl_queue_msg *nfnl_queue_msg_alloc(void)
167 {
168 return (struct nfnl_queue_msg *) nl_object_alloc(&queue_msg_obj_ops);
169 }
170
nfnl_queue_msg_get(struct nfnl_queue_msg * msg)171 void nfnl_queue_msg_get(struct nfnl_queue_msg *msg)
172 {
173 nl_object_get((struct nl_object *) msg);
174 }
175
nfnl_queue_msg_put(struct nfnl_queue_msg * msg)176 void nfnl_queue_msg_put(struct nfnl_queue_msg *msg)
177 {
178 nl_object_put((struct nl_object *) msg);
179 }
180
181 /** @} */
182
183 /**
184 * @name Attributes
185 * @{
186 */
187
nfnl_queue_msg_set_group(struct nfnl_queue_msg * msg,uint16_t group)188 void nfnl_queue_msg_set_group(struct nfnl_queue_msg *msg, uint16_t group)
189 {
190 msg->queue_msg_group = group;
191 msg->ce_mask |= QUEUE_MSG_ATTR_GROUP;
192 }
193
nfnl_queue_msg_test_group(const struct nfnl_queue_msg * msg)194 int nfnl_queue_msg_test_group(const struct nfnl_queue_msg *msg)
195 {
196 return !!(msg->ce_mask & QUEUE_MSG_ATTR_GROUP);
197 }
198
nfnl_queue_msg_get_group(const struct nfnl_queue_msg * msg)199 uint16_t nfnl_queue_msg_get_group(const struct nfnl_queue_msg *msg)
200 {
201 return msg->queue_msg_group;
202 }
203
204 /**
205 * Set the protocol family
206 * @arg msg NF queue message
207 * @arg family AF_XXX address family example: AF_INET, AF_UNIX, etc
208 */
nfnl_queue_msg_set_family(struct nfnl_queue_msg * msg,uint8_t family)209 void nfnl_queue_msg_set_family(struct nfnl_queue_msg *msg, uint8_t family)
210 {
211 msg->queue_msg_family = family;
212 msg->ce_mask |= QUEUE_MSG_ATTR_FAMILY;
213 }
214
nfnl_queue_msg_test_family(const struct nfnl_queue_msg * msg)215 int nfnl_queue_msg_test_family(const struct nfnl_queue_msg *msg)
216 {
217 return !!(msg->ce_mask & QUEUE_MSG_ATTR_FAMILY);
218 }
219
nfnl_queue_msg_get_family(const struct nfnl_queue_msg * msg)220 uint8_t nfnl_queue_msg_get_family(const struct nfnl_queue_msg *msg)
221 {
222 if (msg->ce_mask & QUEUE_MSG_ATTR_FAMILY)
223 return msg->queue_msg_family;
224 else
225 return AF_UNSPEC;
226 }
227
nfnl_queue_msg_set_packetid(struct nfnl_queue_msg * msg,uint32_t packetid)228 void nfnl_queue_msg_set_packetid(struct nfnl_queue_msg *msg, uint32_t packetid)
229 {
230 msg->queue_msg_packetid = packetid;
231 msg->ce_mask |= QUEUE_MSG_ATTR_PACKETID;
232 }
233
nfnl_queue_msg_test_packetid(const struct nfnl_queue_msg * msg)234 int nfnl_queue_msg_test_packetid(const struct nfnl_queue_msg *msg)
235 {
236 return !!(msg->ce_mask & QUEUE_MSG_ATTR_PACKETID);
237 }
238
nfnl_queue_msg_get_packetid(const struct nfnl_queue_msg * msg)239 uint32_t nfnl_queue_msg_get_packetid(const struct nfnl_queue_msg *msg)
240 {
241 return msg->queue_msg_packetid;
242 }
243
nfnl_queue_msg_set_hwproto(struct nfnl_queue_msg * msg,uint16_t hwproto)244 void nfnl_queue_msg_set_hwproto(struct nfnl_queue_msg *msg, uint16_t hwproto)
245 {
246 msg->queue_msg_hwproto = hwproto;
247 msg->ce_mask |= QUEUE_MSG_ATTR_HWPROTO;
248 }
249
nfnl_queue_msg_test_hwproto(const struct nfnl_queue_msg * msg)250 int nfnl_queue_msg_test_hwproto(const struct nfnl_queue_msg *msg)
251 {
252 return !!(msg->ce_mask & QUEUE_MSG_ATTR_HWPROTO);
253 }
254
nfnl_queue_msg_get_hwproto(const struct nfnl_queue_msg * msg)255 uint16_t nfnl_queue_msg_get_hwproto(const struct nfnl_queue_msg *msg)
256 {
257 return msg->queue_msg_hwproto;
258 }
259
nfnl_queue_msg_set_hook(struct nfnl_queue_msg * msg,uint8_t hook)260 void nfnl_queue_msg_set_hook(struct nfnl_queue_msg *msg, uint8_t hook)
261 {
262 msg->queue_msg_hook = hook;
263 msg->ce_mask |= QUEUE_MSG_ATTR_HOOK;
264 }
265
nfnl_queue_msg_test_hook(const struct nfnl_queue_msg * msg)266 int nfnl_queue_msg_test_hook(const struct nfnl_queue_msg *msg)
267 {
268 return !!(msg->ce_mask & QUEUE_MSG_ATTR_HOOK);
269 }
270
nfnl_queue_msg_get_hook(const struct nfnl_queue_msg * msg)271 uint8_t nfnl_queue_msg_get_hook(const struct nfnl_queue_msg *msg)
272 {
273 return msg->queue_msg_hook;
274 }
275
nfnl_queue_msg_set_mark(struct nfnl_queue_msg * msg,uint32_t mark)276 void nfnl_queue_msg_set_mark(struct nfnl_queue_msg *msg, uint32_t mark)
277 {
278 msg->queue_msg_mark = mark;
279 msg->ce_mask |= QUEUE_MSG_ATTR_MARK;
280 }
281
nfnl_queue_msg_test_mark(const struct nfnl_queue_msg * msg)282 int nfnl_queue_msg_test_mark(const struct nfnl_queue_msg *msg)
283 {
284 return !!(msg->ce_mask & QUEUE_MSG_ATTR_MARK);
285 }
286
nfnl_queue_msg_get_mark(const struct nfnl_queue_msg * msg)287 uint32_t nfnl_queue_msg_get_mark(const struct nfnl_queue_msg *msg)
288 {
289 return msg->queue_msg_mark;
290 }
291
nfnl_queue_msg_set_timestamp(struct nfnl_queue_msg * msg,struct timeval * tv)292 void nfnl_queue_msg_set_timestamp(struct nfnl_queue_msg *msg,
293 struct timeval *tv)
294 {
295 msg->queue_msg_timestamp.tv_sec = tv->tv_sec;
296 msg->queue_msg_timestamp.tv_usec = tv->tv_usec;
297 msg->ce_mask |= QUEUE_MSG_ATTR_TIMESTAMP;
298 }
299
nfnl_queue_msg_test_timestamp(const struct nfnl_queue_msg * msg)300 int nfnl_queue_msg_test_timestamp(const struct nfnl_queue_msg *msg)
301 {
302 return !!(msg->ce_mask & QUEUE_MSG_ATTR_TIMESTAMP);
303 }
304
nfnl_queue_msg_get_timestamp(const struct nfnl_queue_msg * msg)305 const struct timeval *nfnl_queue_msg_get_timestamp(const struct nfnl_queue_msg *msg)
306 {
307 if (!(msg->ce_mask & QUEUE_MSG_ATTR_TIMESTAMP))
308 return NULL;
309 return &msg->queue_msg_timestamp;
310 }
311
nfnl_queue_msg_set_indev(struct nfnl_queue_msg * msg,uint32_t indev)312 void nfnl_queue_msg_set_indev(struct nfnl_queue_msg *msg, uint32_t indev)
313 {
314 msg->queue_msg_indev = indev;
315 msg->ce_mask |= QUEUE_MSG_ATTR_INDEV;
316 }
317
nfnl_queue_msg_test_indev(const struct nfnl_queue_msg * msg)318 int nfnl_queue_msg_test_indev(const struct nfnl_queue_msg *msg)
319 {
320 return !!(msg->ce_mask & QUEUE_MSG_ATTR_INDEV);
321 }
322
nfnl_queue_msg_get_indev(const struct nfnl_queue_msg * msg)323 uint32_t nfnl_queue_msg_get_indev(const struct nfnl_queue_msg *msg)
324 {
325 return msg->queue_msg_indev;
326 }
327
nfnl_queue_msg_set_outdev(struct nfnl_queue_msg * msg,uint32_t outdev)328 void nfnl_queue_msg_set_outdev(struct nfnl_queue_msg *msg, uint32_t outdev)
329 {
330 msg->queue_msg_outdev = outdev;
331 msg->ce_mask |= QUEUE_MSG_ATTR_OUTDEV;
332 }
333
nfnl_queue_msg_test_outdev(const struct nfnl_queue_msg * msg)334 int nfnl_queue_msg_test_outdev(const struct nfnl_queue_msg *msg)
335 {
336 return !!(msg->ce_mask & QUEUE_MSG_ATTR_OUTDEV);
337 }
338
nfnl_queue_msg_get_outdev(const struct nfnl_queue_msg * msg)339 uint32_t nfnl_queue_msg_get_outdev(const struct nfnl_queue_msg *msg)
340 {
341 return msg->queue_msg_outdev;
342 }
343
nfnl_queue_msg_set_physindev(struct nfnl_queue_msg * msg,uint32_t physindev)344 void nfnl_queue_msg_set_physindev(struct nfnl_queue_msg *msg,
345 uint32_t physindev)
346 {
347 msg->queue_msg_physindev = physindev;
348 msg->ce_mask |= QUEUE_MSG_ATTR_PHYSINDEV;
349 }
350
nfnl_queue_msg_test_physindev(const struct nfnl_queue_msg * msg)351 int nfnl_queue_msg_test_physindev(const struct nfnl_queue_msg *msg)
352 {
353 return !!(msg->ce_mask & QUEUE_MSG_ATTR_PHYSINDEV);
354 }
355
nfnl_queue_msg_get_physindev(const struct nfnl_queue_msg * msg)356 uint32_t nfnl_queue_msg_get_physindev(const struct nfnl_queue_msg *msg)
357 {
358 return msg->queue_msg_physindev;
359 }
360
nfnl_queue_msg_set_physoutdev(struct nfnl_queue_msg * msg,uint32_t physoutdev)361 void nfnl_queue_msg_set_physoutdev(struct nfnl_queue_msg *msg,
362 uint32_t physoutdev)
363 {
364 msg->queue_msg_physoutdev = physoutdev;
365 msg->ce_mask |= QUEUE_MSG_ATTR_PHYSOUTDEV;
366 }
367
nfnl_queue_msg_test_physoutdev(const struct nfnl_queue_msg * msg)368 int nfnl_queue_msg_test_physoutdev(const struct nfnl_queue_msg *msg)
369 {
370 return !!(msg->ce_mask & QUEUE_MSG_ATTR_PHYSOUTDEV);
371 }
372
nfnl_queue_msg_get_physoutdev(const struct nfnl_queue_msg * msg)373 uint32_t nfnl_queue_msg_get_physoutdev(const struct nfnl_queue_msg *msg)
374 {
375 return msg->queue_msg_physoutdev;
376 }
377
nfnl_queue_msg_set_hwaddr(struct nfnl_queue_msg * msg,uint8_t * hwaddr,int len)378 void nfnl_queue_msg_set_hwaddr(struct nfnl_queue_msg *msg, uint8_t *hwaddr,
379 int len)
380 {
381 if (len > sizeof(msg->queue_msg_hwaddr))
382 len = sizeof(msg->queue_msg_hwaddr);
383
384 msg->queue_msg_hwaddr_len = len;
385 memcpy(msg->queue_msg_hwaddr, hwaddr, len);
386 msg->ce_mask |= QUEUE_MSG_ATTR_HWADDR;
387 }
388
nfnl_queue_msg_test_hwaddr(const struct nfnl_queue_msg * msg)389 int nfnl_queue_msg_test_hwaddr(const struct nfnl_queue_msg *msg)
390 {
391 return !!(msg->ce_mask & QUEUE_MSG_ATTR_HWADDR);
392 }
393
nfnl_queue_msg_get_hwaddr(const struct nfnl_queue_msg * msg,int * len)394 const uint8_t *nfnl_queue_msg_get_hwaddr(const struct nfnl_queue_msg *msg,
395 int *len)
396 {
397 if (!(msg->ce_mask & QUEUE_MSG_ATTR_HWADDR)) {
398 *len = 0;
399 return NULL;
400 }
401
402 *len = msg->queue_msg_hwaddr_len;
403 return msg->queue_msg_hwaddr;
404 }
405
nfnl_queue_msg_set_payload(struct nfnl_queue_msg * msg,uint8_t * payload,int len)406 int nfnl_queue_msg_set_payload(struct nfnl_queue_msg *msg, uint8_t *payload,
407 int len)
408 {
409 void *new_payload = malloc(len);
410
411 if (new_payload == NULL)
412 return -NLE_NOMEM;
413 memcpy(new_payload, payload, len);
414
415 free(msg->queue_msg_payload);
416
417 msg->queue_msg_payload = new_payload;
418 msg->queue_msg_payload_len = len;
419 msg->ce_mask |= QUEUE_MSG_ATTR_PAYLOAD;
420 return 0;
421 }
422
nfnl_queue_msg_test_payload(const struct nfnl_queue_msg * msg)423 int nfnl_queue_msg_test_payload(const struct nfnl_queue_msg *msg)
424 {
425 return !!(msg->ce_mask & QUEUE_MSG_ATTR_PAYLOAD);
426 }
427
nfnl_queue_msg_get_payload(const struct nfnl_queue_msg * msg,int * len)428 const void *nfnl_queue_msg_get_payload(const struct nfnl_queue_msg *msg, int *len)
429 {
430 if (!(msg->ce_mask & QUEUE_MSG_ATTR_PAYLOAD)) {
431 *len = 0;
432 return NULL;
433 }
434
435 *len = msg->queue_msg_payload_len;
436 return msg->queue_msg_payload;
437 }
438
439 /**
440 * Return the number of items matching a filter in the cache
441 * @arg msg queue msg
442 * @arg verdict NF_DROP, NF_ACCEPT, NF_REPEAT, etc
443 */
nfnl_queue_msg_set_verdict(struct nfnl_queue_msg * msg,unsigned int verdict)444 void nfnl_queue_msg_set_verdict(struct nfnl_queue_msg *msg,
445 unsigned int verdict)
446 {
447 msg->queue_msg_verdict = verdict;
448 msg->ce_mask |= QUEUE_MSG_ATTR_VERDICT;
449 }
450
nfnl_queue_msg_test_verdict(const struct nfnl_queue_msg * msg)451 int nfnl_queue_msg_test_verdict(const struct nfnl_queue_msg *msg)
452 {
453 return !!(msg->ce_mask & QUEUE_MSG_ATTR_VERDICT);
454 }
455
nfnl_queue_msg_get_verdict(const struct nfnl_queue_msg * msg)456 unsigned int nfnl_queue_msg_get_verdict(const struct nfnl_queue_msg *msg)
457 {
458 return msg->queue_msg_verdict;
459 }
460
461 static const struct trans_tbl nfnl_queue_msg_attrs[] = {
462 __ADD(QUEUE_MSG_ATTR_GROUP, group),
463 __ADD(QUEUE_MSG_ATTR_FAMILY, family),
464 __ADD(QUEUE_MSG_ATTR_PACKETID, packetid),
465 __ADD(QUEUE_MSG_ATTR_HWPROTO, hwproto),
466 __ADD(QUEUE_MSG_ATTR_HOOK, hook),
467 __ADD(QUEUE_MSG_ATTR_MARK, mark),
468 __ADD(QUEUE_MSG_ATTR_TIMESTAMP, timestamp),
469 __ADD(QUEUE_MSG_ATTR_INDEV, indev),
470 __ADD(QUEUE_MSG_ATTR_OUTDEV, outdev),
471 __ADD(QUEUE_MSG_ATTR_PHYSINDEV, physindev),
472 __ADD(QUEUE_MSG_ATTR_PHYSOUTDEV, physoutdev),
473 __ADD(QUEUE_MSG_ATTR_HWADDR, hwaddr),
474 __ADD(QUEUE_MSG_ATTR_PAYLOAD, payload),
475 __ADD(QUEUE_MSG_ATTR_VERDICT, verdict),
476 };
477
nfnl_queue_msg_attrs2str(int attrs,char * buf,size_t len)478 static char *nfnl_queue_msg_attrs2str(int attrs, char *buf, size_t len)
479 {
480 return __flags2str(attrs, buf, len, nfnl_queue_msg_attrs,
481 ARRAY_SIZE(nfnl_queue_msg_attrs));
482 }
483
484 /** @} */
485
486 struct nl_object_ops queue_msg_obj_ops = {
487 .oo_name = "netfilter/queuemsg",
488 .oo_size = sizeof(struct nfnl_queue_msg),
489 .oo_free_data = nfnl_queue_msg_free_data,
490 .oo_clone = nfnl_queue_msg_clone,
491 .oo_dump = {
492 [NL_DUMP_LINE] = nfnl_queue_msg_dump,
493 [NL_DUMP_DETAILS] = nfnl_queue_msg_dump,
494 [NL_DUMP_STATS] = nfnl_queue_msg_dump,
495 },
496 .oo_attrs2str = nfnl_queue_msg_attrs2str,
497 };
498
499 /** @} */
500