Debian Codegraph @

src/expr.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/expr.c`/
2 /*
3 * (C) 2012 by Pablo Neira Ayuso <[email protected]>
4 *
5 * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
6 */
7 #include "internal.h"cxx . . $ `<file>/include/internal.h`/
8
9 #include <time.h>
10 #include <endian.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <netinet/in.h>
16
17 #include <libmnl/libmnl.h>
18 #include <linux/netfilter/nfnetlink.h>cxx . . $ `<file>/include/linux/netfilter/nfnetlink.h`/
19 #include <linux/netfilter/nf_tables.h>cxx . . $ `<file>/include/linux/netfilter/nf_tables.h`/
20
21 #include <libnftnl/expr.h>cxx . . $ `<file>/include/libnftnl/expr.h`/
22
23 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_alloc);
24 struct nftnl_exprcxx . . $ nftnl_expr# *nftnl_expr_alloccxx . . $ nftnl_expr_alloc(acfdfe04e44185c1).(const char *name)
25 {
26 struct nftnl_exprcxx . . $ nftnl_expr# *expr;
27 struct expr_opscxx . . $ expr_ops# *ops;
28
29 ops = nftnl_expr_ops_lookupcxx . . $ nftnl_expr_ops_lookup(7edc9fba769e98c).(name);
30 if (ops == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
31 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
32
33 expr = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(struct nftnl_exprcxx . . $ nftnl_expr#) + ops->alloc_lencxx . . $ expr_ops#alloc_len.);
34 if (expr == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
35 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
36
37 /* Manually set expression name attribute */
38 expr->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_NAMEcxx . . $ NFTNL_EXPR_NAME.);
39 expr->opscxx . . $ nftnl_expr#ops. = ops;
40
41 if (ops->initcxx . . $ expr_ops#init.)
42 ops->initcxx . . $ expr_ops#init.(expr);
43
44 return expr;
45 }
46
47 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_free);
48 void nftnl_expr_freecxx . . $ nftnl_expr_free(cbe1a28527736226).(const struct nftnl_exprcxx . . $ nftnl_expr# *expr)
49 {
50 if (expr->opscxx . . $ nftnl_expr#ops.->freecxx . . $ expr_ops#free.)
51 expr->opscxx . . $ nftnl_expr#ops.->freecxx . . $ expr_ops#free.(expr);
52
53 xfreecxx . . $ `include/utils.h:22:9`!(expr);
54 }
55
56 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_is_set);
57 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! nftnl_expr_is_setcxx . . $ nftnl_expr_is_set(3ec844c1c49fbbfd).(const struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type)
58 {
59 return expr->flagscxx . . $ nftnl_expr#flags. & (1 << type);
60 }
61
62 static int __nftnl_expr_setcxx . . $ __nftnl_expr_set(211371a3e3842127).(struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type,
63 const void *data, uint32_tcxx . . $ uint32_t# data_len,
64 uint32_tcxx . . $ uint32_t# byteorder)
65 {
66 switch(type) {
67 case NFTNL_EXPR_NAMEcxx . . $ NFTNL_EXPR_NAME.: /* cannot be modified */
68 return 0;
69 default:
70 if (type < NFTNL_EXPR_BASEcxx . . $ NFTNL_EXPR_BASE. || type > expr->opscxx . . $ nftnl_expr#ops.->nftnl_max_attrcxx . . $ expr_ops#nftnl_max_attr.)
71 return -1;
72
73 if (!expr->opscxx . . $ nftnl_expr#ops.->attr_policycxx . . $ expr_ops#attr_policy.)
74 return -1;
75
76 if (expr->opscxx . . $ nftnl_expr#ops.->attr_policycxx . . $ expr_ops#attr_policy.[type].maxlencxx . . $ attr_policy#maxlen. &&
77 expr->opscxx . . $ nftnl_expr#ops.->attr_policycxx . . $ expr_ops#attr_policy.[type].maxlencxx . . $ attr_policy#maxlen. < data_len)
78 return -1;
79
80 if (expr->opscxx . . $ nftnl_expr#ops.->setcxx . . $ expr_ops#set.(expr, type, data, data_len, byteorder) < 0)
81 return -1;
82 }
83 expr->flagscxx . . $ nftnl_expr#flags. |= (1 << type);
84 return 0;
85 }
86
87 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_set);
88 int nftnl_expr_setcxx . . $ nftnl_expr_set(ffab15e3b525d41a).(struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type,
89 const void *data, uint32_tcxx . . $ uint32_t# data_len)
90 {
91 return __nftnl_expr_setcxx . . $ __nftnl_expr_set(211371a3e3842127).(expr, type, data, data_len, 0);
92 }
93
94 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_set_imm);
95 int nftnl_expr_set_immcxx . . $ nftnl_expr_set_imm(211371a3e3842127).(struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type,
96 const void *data, uint32_tcxx . . $ uint32_t# data_len, uint32_tcxx . . $ uint32_t# byteorder)
97 {
98 return __nftnl_expr_setcxx . . $ __nftnl_expr_set(211371a3e3842127).(expr, type, data, data_len, byteorder);
99 }
100
101 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_set_u8);
102 void
103 nftnl_expr_set_u8cxx . . $ nftnl_expr_set_u8(e511f11aa8ed2bda).(struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type, uint8_tcxx . . $ uint8_t# data)
104 {
105 nftnl_expr_setcxx . . $ nftnl_expr_set(ffab15e3b525d41a).(expr, type, &data, sizeof(uint8_tcxx . . $ uint8_t#));
106 }
107
108 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_set_u16);
109 void
110 nftnl_expr_set_u16cxx . . $ nftnl_expr_set_u16(aede9ff46ced84d7).(struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type, uint16_tcxx . . $ uint16_t# data)
111 {
112 nftnl_expr_setcxx . . $ nftnl_expr_set(ffab15e3b525d41a).(expr, type, &data, sizeof(uint16_tcxx . . $ uint16_t#));
113 }
114
115 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_set_u32);
116 void
117 nftnl_expr_set_u32cxx . . $ nftnl_expr_set_u32(c89149b3df086d40).(struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type, uint32_tcxx . . $ uint32_t# data)
118 {
119 nftnl_expr_setcxx . . $ nftnl_expr_set(ffab15e3b525d41a).(expr, type, &data, sizeof(uint32_tcxx . . $ uint32_t#));
120 }
121
122 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_set_u64);
123 void
124 nftnl_expr_set_u64cxx . . $ nftnl_expr_set_u64(9d868f73bb27032a).(struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type, uint64_tcxx . . $ uint64_t# data)
125 {
126 nftnl_expr_setcxx . . $ nftnl_expr_set(ffab15e3b525d41a).(expr, type, &data, sizeof(uint64_tcxx . . $ uint64_t#));
127 }
128
129 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_set_str);
130 int nftnl_expr_set_strcxx . . $ nftnl_expr_set_str(95a27dbde9dcfc3e).(struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type, const char *str)
131 {
132 return nftnl_expr_setcxx . . $ nftnl_expr_set(ffab15e3b525d41a).(expr, type, str, strlencxx . . $ strlen(751346f8406fd082).(str) + 1);
133 }
134
135 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_get);
136 const void *nftnl_expr_getcxx . . $ nftnl_expr_get(9de263c4f9e71c4b).(const struct nftnl_exprcxx . . $ nftnl_expr# *expr,
137 uint16_tcxx . . $ uint16_t# type, uint32_tcxx . . $ uint32_t# *data_len)
138 {
139 const void *ret;
140
141 if (!(expr->flagscxx . . $ nftnl_expr#flags. & (1 << type)))
142 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
143
144 switch(type) {
145 case NFTNL_EXPR_NAMEcxx . . $ NFTNL_EXPR_NAME.:
146 *data_len = strlencxx . . $ strlen(751346f8406fd082).(expr->opscxx . . $ nftnl_expr#ops.->namecxx . . $ expr_ops#name.) + 1;
147 ret = expr->opscxx . . $ nftnl_expr#ops.->namecxx . . $ expr_ops#name.;
148 break;
149 default:
150 ret = expr->opscxx . . $ nftnl_expr#ops.->getcxx . . $ expr_ops#get.(expr, type, data_len);
151 break;
152 }
153
154 return ret;
155 }
156
157 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_get_u8);
158 uint8_tcxx . . $ uint8_t# nftnl_expr_get_u8cxx . . $ nftnl_expr_get_u8(7d8d22900a452285).(const struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type)
159 {
160 const void *data;
161 uint32_tcxx . . $ uint32_t# data_len;
162
163 data = nftnl_expr_getcxx . . $ nftnl_expr_get(9de263c4f9e71c4b).(expr, type, &data_len);
164 if (data == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
165 return 0;
166
167 if (data_len != sizeof(uint8_tcxx . . $ uint8_t#))
168 return 0;
169
170 return *((uint8_tcxx . . $ uint8_t# *)data);
171 }
172
173 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_get_u16);
174 uint16_tcxx . . $ uint16_t# nftnl_expr_get_u16cxx . . $ nftnl_expr_get_u16(f4223197fafdacaf).(const struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type)
175 {
176 const void *data;
177 uint32_tcxx . . $ uint32_t# data_len;
178
179 data = nftnl_expr_getcxx . . $ nftnl_expr_get(9de263c4f9e71c4b).(expr, type, &data_len);
180 if (data == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
181 return 0;
182
183 if (data_len != sizeof(uint16_tcxx . . $ uint16_t#))
184 return 0;
185
186 return *((uint16_tcxx . . $ uint16_t# *)data);
187 }
188
189 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_get_u32);
190 uint32_tcxx . . $ uint32_t# nftnl_expr_get_u32cxx . . $ nftnl_expr_get_u32(3d3d725b9eb2d6a2).(const struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type)
191 {
192 const void *data;
193 uint32_tcxx . . $ uint32_t# data_len;
194
195 data = nftnl_expr_getcxx . . $ nftnl_expr_get(9de263c4f9e71c4b).(expr, type, &data_len);
196 if (data == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
197 return 0;
198
199 if (data_len != sizeof(uint32_tcxx . . $ uint32_t#))
200 return 0;
201
202 return *((uint32_tcxx . . $ uint32_t# *)data);
203 }
204
205 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_get_u64);
206 uint64_tcxx . . $ uint64_t# nftnl_expr_get_u64cxx . . $ nftnl_expr_get_u64(263c9df45e45c54b).(const struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type)
207 {
208 const void *data;
209 uint32_tcxx . . $ uint32_t# data_len;
210
211 data = nftnl_expr_getcxx . . $ nftnl_expr_get(9de263c4f9e71c4b).(expr, type, &data_len);
212 if (data == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
213 return 0;
214
215 if (data_len != sizeof(uint64_tcxx . . $ uint64_t#))
216 return 0;
217
218 return *((uint64_tcxx . . $ uint64_t# *)data);
219 }
220
221 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_get_str);
222 const char *nftnl_expr_get_strcxx . . $ nftnl_expr_get_str(89517962841a533e).(const struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint16_tcxx . . $ uint16_t# type)
223 {
224 uint32_tcxx . . $ uint32_t# data_len;
225
226 return (const char *)nftnl_expr_getcxx . . $ nftnl_expr_get(9de263c4f9e71c4b).(expr, type, &data_len);
227 }
228
229 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_build_payload);
230 void nftnl_expr_build_payloadcxx . . $ nftnl_expr_build_payload(efa2a5c645f672a6).(struct nlmsghdrcxx . . $ nlmsghdr# *nlh, struct nftnl_exprcxx . . $ nftnl_expr# *expr)
231 {
232 struct nlattrcxx . . $ nlattr# *nest;
233
234 mnl_attr_put_strzcxx . . $ mnl_attr_put_strz(eb76286d4bfc1a8d).(nlh, NFTA_EXPR_NAMEcxx . . $ NFTA_EXPR_NAME., expr->opscxx . . $ nftnl_expr#ops.->namecxx . . $ expr_ops#name.);
235
236 if (!expr->opscxx . . $ nftnl_expr#ops.->buildcxx . . $ expr_ops#build.)
237 return;
238
239 nest = mnl_attr_nest_startcxx . . $ mnl_attr_nest_start(c8ed348a075426e7).(nlh, NFTA_EXPR_DATAcxx . . $ NFTA_EXPR_DATA.);
240 expr->opscxx . . $ nftnl_expr#ops.->buildcxx . . $ expr_ops#build.(nlh, expr);
241 mnl_attr_nest_endcxx . . $ mnl_attr_nest_end(564f73625c62ba58).(nlh, nest);
242 }
243
244 static int nftnl_rule_parse_expr_cbcxx . . $ nftnl_rule_parse_expr_cb(de2ef18c48fd675c).(const struct nlattrcxx . . $ nlattr# *attr, void *data)
245 {
246 const struct nlattrcxx . . $ nlattr# **tb = data;
247 int type = mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr);
248
249 if (mnl_attr_type_validcxx . . $ mnl_attr_type_valid(47df1cce1c7be855).(attr, NFTA_EXPR_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:552:9`!) < 0)
250 return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!;
251
252 switch (type) {
253 case NFTA_EXPR_NAMEcxx . . $ NFTA_EXPR_NAME.:
254 if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_STRINGcxx . . $ MNL_TYPE_STRING.) < 0)
255 abi_breakagecxx . . $ `include/utils.h:28:9`!();
256 break;
257 case NFTA_EXPR_DATAcxx . . $ NFTA_EXPR_DATA.:
258 if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_NESTEDcxx . . $ MNL_TYPE_NESTED.) < 0)
259 abi_breakagecxx . . $ `include/utils.h:28:9`!();
260 break;
261 }
262
263 tb[type] = attr;
264 return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!;
265 }
266
267 struct nftnl_exprcxx . . $ nftnl_expr# *nftnl_expr_parsecxx . . $ nftnl_expr_parse(ea32a576744d0134).(struct nlattrcxx . . $ nlattr# *attr)
268 {
269 struct nlattrcxx . . $ nlattr# *tb[NFTA_EXPR_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:552:9`!+1] = {};
270 struct nftnl_exprcxx . . $ nftnl_expr# *expr;
271
272 if (mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_rule_parse_expr_cbcxx . . $ nftnl_rule_parse_expr_cb(de2ef18c48fd675c)., tb) < 0)
273 goto err1;
274
275 expr = nftnl_expr_alloccxx . . $ nftnl_expr_alloc(acfdfe04e44185c1).(mnl_attr_get_strcxx . . $ mnl_attr_get_str(c24cebeef6d84d59).(tb[NFTA_EXPR_NAMEcxx . . $ NFTA_EXPR_NAME.]));
276 if (expr == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
277 goto err1;
278
279 if (tb[NFTA_EXPR_DATAcxx . . $ NFTA_EXPR_DATA.] &&
280 expr->opscxx . . $ nftnl_expr#ops.->parsecxx . . $ expr_ops#parse. &&
281 expr->opscxx . . $ nftnl_expr#ops.->parsecxx . . $ expr_ops#parse.(expr, tb[NFTA_EXPR_DATAcxx . . $ NFTA_EXPR_DATA.]) < 0)
282 goto err2;
283
284 return expr;
285
286 err2:
287 xfreecxx . . $ `include/utils.h:22:9`!(expr);
288 err1:
289 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
290 }
291
292 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_snprintf);
293 int nftnl_expr_snprintfcxx . . $ nftnl_expr_snprintf(7e053fe52193f124).(char *buf, size_t remain, const struct nftnl_exprcxx . . $ nftnl_expr# *expr,
294 uint32_tcxx . . $ uint32_t# type, uint32_tcxx . . $ uint32_t# flags)
295 {
296 int ret;
297 unsigned int offset = 0;
298
299 if (remain)
300 buf[0] = '\0';
301
302 if (!expr->opscxx . . $ nftnl_expr#ops.->outputcxx . . $ expr_ops#output. || type != NFTNL_OUTPUT_DEFAULTcxx . . $ NFTNL_OUTPUT_DEFAULT.)
303 return 0;
304
305 ret = expr->opscxx . . $ nftnl_expr#ops.->outputcxx . . $ expr_ops#output.(buf + offset, remain, flags, expr);
306 SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset);
307
308 return offset;
309 }
310
311 static int nftnl_expr_do_snprintfcxx . . $ nftnl_expr_do_snprintf(809f16203406eb30).(char *buf, size_t size, const void *e,
312 uint32_tcxx . . $ uint32_t# cmd, uint32_tcxx . . $ uint32_t# type, uint32_tcxx . . $ uint32_t# flags)
313 {
314 return nftnl_expr_snprintfcxx . . $ nftnl_expr_snprintf(7e053fe52193f124).(buf, size, e, type, flags);
315 }
316
317 EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_expr_fprintf);
318 int nftnl_expr_fprintfcxx . . $ nftnl_expr_fprintf(22a9a098cebfc95c).(FILEcxx . . $ FILE# *fp, const struct nftnl_exprcxx . . $ nftnl_expr# *expr, uint32_tcxx . . $ uint32_t# type,
319 uint32_tcxx . . $ uint32_t# flags)
320 {
321 return nftnl_fprintfcxx . . $ nftnl_fprintf(959aea809fde9589).(fp, expr, NFTNL_CMD_UNSPECcxx . . $ NFTNL_CMD_UNSPEC., type, flags,
322 nftnl_expr_do_snprintfcxx . . $ nftnl_expr_do_snprintf(809f16203406eb30).);
323 }