xref: /aosp_15_r20/external/libnl/lib/netfilter/queue_obj.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2007, 2008 Patrick McHardy <[email protected]>
4  */
5 
6 /**
7  * @ingroup nfnl
8  * @defgroup queue Queue
9  * @brief
10  * @{
11  */
12 
13 #include "nl-default.h"
14 
15 #include <netlink/netfilter/nfnl.h>
16 #include <netlink/netfilter/queue.h>
17 
18 #include "nl-priv-dynamic-core/object-api.h"
19 #include "nl-priv-dynamic-core/nl-core.h"
20 
21 /** @cond SKIP */
22 struct nfnl_queue {
23 	NLHDR_COMMON
24 
25 	uint16_t		queue_group;
26 	uint32_t		queue_maxlen;
27 	uint32_t		queue_copy_range;
28 	uint8_t			queue_copy_mode;
29 };
30 
31 #define QUEUE_ATTR_GROUP		(1UL << 0)
32 #define QUEUE_ATTR_MAXLEN		(1UL << 1)
33 #define QUEUE_ATTR_COPY_MODE		(1UL << 2)
34 #define QUEUE_ATTR_COPY_RANGE		(1UL << 3)
35 /** @endcond */
36 
37 
nfnl_queue_dump(struct nl_object * a,struct nl_dump_params * p)38 static void nfnl_queue_dump(struct nl_object *a, struct nl_dump_params *p)
39 {
40 	struct nfnl_queue *queue = (struct nfnl_queue *) a;
41 	char buf[64];
42 
43 	nl_new_line(p);
44 
45 	if (queue->ce_mask & QUEUE_ATTR_GROUP)
46 		nl_dump(p, "group=%u ", queue->queue_group);
47 
48 	if (queue->ce_mask & QUEUE_ATTR_MAXLEN)
49 		nl_dump(p, "maxlen=%u ", queue->queue_maxlen);
50 
51 	if (queue->ce_mask & QUEUE_ATTR_COPY_MODE)
52 		nl_dump(p, "copy_mode=%s ",
53 			nfnl_queue_copy_mode2str(queue->queue_copy_mode,
54 						 buf, sizeof(buf)));
55 
56 	if (queue->ce_mask & QUEUE_ATTR_COPY_RANGE)
57 		nl_dump(p, "copy_range=%u ", queue->queue_copy_range);
58 
59 	nl_dump(p, "\n");
60 }
61 
62 static const struct trans_tbl copy_modes[] = {
63 	__ADD(NFNL_QUEUE_COPY_NONE,	none),
64 	__ADD(NFNL_QUEUE_COPY_META,	meta),
65 	__ADD(NFNL_QUEUE_COPY_PACKET,	packet),
66 };
67 
nfnl_queue_copy_mode2str(enum nfnl_queue_copy_mode copy_mode,char * buf,size_t len)68 char *nfnl_queue_copy_mode2str(enum nfnl_queue_copy_mode copy_mode, char *buf,
69 			       size_t len)
70 {
71 	return __type2str(copy_mode, buf, len, copy_modes,
72 			   ARRAY_SIZE(copy_modes));
73 }
74 
nfnl_queue_str2copy_mode(const char * name)75 int nfnl_queue_str2copy_mode(const char *name)
76 {
77 	return __str2type(name, copy_modes, ARRAY_SIZE(copy_modes));
78 }
79 
80 /**
81  * @name Allocation/Freeing
82  * @{
83  */
84 
nfnl_queue_alloc(void)85 struct nfnl_queue *nfnl_queue_alloc(void)
86 {
87 	return (struct nfnl_queue *) nl_object_alloc(&queue_obj_ops);
88 }
89 
nfnl_queue_get(struct nfnl_queue * queue)90 void nfnl_queue_get(struct nfnl_queue *queue)
91 {
92 	nl_object_get((struct nl_object *) queue);
93 }
94 
nfnl_queue_put(struct nfnl_queue * queue)95 void nfnl_queue_put(struct nfnl_queue *queue)
96 {
97 	nl_object_put((struct nl_object *) queue);
98 }
99 
100 /** @} */
101 
102 /**
103  * @name Attributes
104  * @{
105  */
106 
nfnl_queue_set_group(struct nfnl_queue * queue,uint16_t group)107 void nfnl_queue_set_group(struct nfnl_queue *queue, uint16_t group)
108 {
109 	queue->queue_group = group;
110 	queue->ce_mask |= QUEUE_ATTR_GROUP;
111 }
112 
nfnl_queue_test_group(const struct nfnl_queue * queue)113 int nfnl_queue_test_group(const struct nfnl_queue *queue)
114 {
115 	return !!(queue->ce_mask & QUEUE_ATTR_GROUP);
116 }
117 
nfnl_queue_get_group(const struct nfnl_queue * queue)118 uint16_t nfnl_queue_get_group(const struct nfnl_queue *queue)
119 {
120 	return queue->queue_group;
121 }
122 
nfnl_queue_set_maxlen(struct nfnl_queue * queue,uint32_t maxlen)123 void nfnl_queue_set_maxlen(struct nfnl_queue *queue, uint32_t maxlen)
124 {
125 	queue->queue_maxlen = maxlen;
126 	queue->ce_mask |= QUEUE_ATTR_MAXLEN;
127 }
128 
nfnl_queue_test_maxlen(const struct nfnl_queue * queue)129 int nfnl_queue_test_maxlen(const struct nfnl_queue *queue)
130 {
131 	return !!(queue->ce_mask & QUEUE_ATTR_MAXLEN);
132 }
133 
nfnl_queue_get_maxlen(const struct nfnl_queue * queue)134 uint32_t nfnl_queue_get_maxlen(const struct nfnl_queue *queue)
135 {
136 	return queue->queue_maxlen;
137 }
138 
nfnl_queue_set_copy_mode(struct nfnl_queue * queue,enum nfnl_queue_copy_mode mode)139 void nfnl_queue_set_copy_mode(struct nfnl_queue *queue, enum nfnl_queue_copy_mode mode)
140 {
141 	queue->queue_copy_mode = mode;
142 	queue->ce_mask |= QUEUE_ATTR_COPY_MODE;
143 }
144 
nfnl_queue_test_copy_mode(const struct nfnl_queue * queue)145 int nfnl_queue_test_copy_mode(const struct nfnl_queue *queue)
146 {
147 	return !!(queue->ce_mask & QUEUE_ATTR_COPY_MODE);
148 }
149 
nfnl_queue_get_copy_mode(const struct nfnl_queue * queue)150 enum nfnl_queue_copy_mode nfnl_queue_get_copy_mode(const struct nfnl_queue *queue)
151 {
152 	return queue->queue_copy_mode;
153 }
154 
nfnl_queue_set_copy_range(struct nfnl_queue * queue,uint32_t copy_range)155 void nfnl_queue_set_copy_range(struct nfnl_queue *queue, uint32_t copy_range)
156 {
157 	queue->queue_copy_range = copy_range;
158 	queue->ce_mask |= QUEUE_ATTR_COPY_RANGE;
159 }
160 
nfnl_queue_test_copy_range(const struct nfnl_queue * queue)161 int nfnl_queue_test_copy_range(const struct nfnl_queue *queue)
162 {
163 	return !!(queue->ce_mask & QUEUE_ATTR_COPY_RANGE);
164 }
165 
nfnl_queue_get_copy_range(const struct nfnl_queue * queue)166 uint32_t nfnl_queue_get_copy_range(const struct nfnl_queue *queue)
167 {
168 	return queue->queue_copy_range;
169 }
170 
nfnl_queue_compare(struct nl_object * _a,struct nl_object * _b,uint64_t attrs,int flags)171 static uint64_t nfnl_queue_compare(struct nl_object *_a, struct nl_object *_b,
172 				   uint64_t attrs, int flags)
173 {
174 	struct nfnl_queue *a = (struct nfnl_queue *) _a;
175 	struct nfnl_queue *b = (struct nfnl_queue *) _b;
176 	uint64_t diff = 0;
177 
178 #define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR)
179 #define _DIFF_VAL(ATTR, FIELD) _DIFF(ATTR, a->FIELD != b->FIELD)
180 	diff |= _DIFF_VAL(QUEUE_ATTR_GROUP, queue_group);
181 	diff |= _DIFF_VAL(QUEUE_ATTR_MAXLEN, queue_maxlen);
182 	diff |= _DIFF_VAL(QUEUE_ATTR_COPY_MODE, queue_copy_mode);
183 	diff |= _DIFF_VAL(QUEUE_ATTR_COPY_RANGE, queue_copy_range);
184 #undef _DIFF
185 #undef _DIFF_VAL
186 
187 	return diff;
188 }
189 
190 static const struct trans_tbl nfnl_queue_attrs[] = {
191 	__ADD(QUEUE_ATTR_GROUP,		group),
192 	__ADD(QUEUE_ATTR_MAXLEN,	maxlen),
193 	__ADD(QUEUE_ATTR_COPY_MODE,	copy_mode),
194 	__ADD(QUEUE_ATTR_COPY_RANGE,	copy_range),
195 };
196 
nfnl_queue_attrs2str(int attrs,char * buf,size_t len)197 static char *nfnl_queue_attrs2str(int attrs, char *buf, size_t len)
198 {
199 	return __flags2str(attrs, buf, len, nfnl_queue_attrs,
200 			   ARRAY_SIZE(nfnl_queue_attrs));
201 }
202 
203 /** @} */
204 
205 struct nl_object_ops queue_obj_ops = {
206 	.oo_name		= "netfilter/queue",
207 	.oo_size		= sizeof(struct nfnl_queue),
208 	.oo_dump = {
209 	    [NL_DUMP_LINE]	= nfnl_queue_dump,
210 	    [NL_DUMP_DETAILS]	= nfnl_queue_dump,
211 	    [NL_DUMP_STATS]	= nfnl_queue_dump,
212 	},
213 	.oo_compare		= nfnl_queue_compare,
214 	.oo_attrs2str		= nfnl_queue_attrs2str,
215 	.oo_id_attrs		= QUEUE_ATTR_GROUP,
216 };
217 
218 /** @} */
219