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