Debian Codegraph @

src/expr/range.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/expr/range.c`/
2 /*
3 * (C) 2016 by Pablo Neira Ayuso <[email protected]>
4 */
5
6 #include "internal.h"cxx . . $ `<file>/include/internal.h`/
7
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <string.h>
11 #include <arpa/inet.h>
12 #include <errno.h>
13
14 #include <libmnl/libmnl.h>
15 #include <linux/netfilter/nf_tables.h>cxx . . $ `<file>/include/linux/netfilter/nf_tables.h`/
16 #include <libnftnl/expr.h>cxx . . $ `<file>/include/libnftnl/expr.h`/
17 #include <libnftnl/rule.h>cxx . . $ `<file>/include/libnftnl/rule.h`/
18
19 struct nftnl_expr_rangecxx . . $ nftnl_expr_range# {
20 union nftnl_data_regcxx . . $ nftnl_data_reg# data_fromcxx . . $ nftnl_expr_range#data_from.;
21 union nftnl_data_regcxx . . $ nftnl_data_reg# data_tocxx . . $ nftnl_expr_range#data_to.;
22 enum nft_registerscxx . . $ nft_registers# sregcxx . . $ nftnl_expr_range#sreg.;
23 enum nft_range_opscxx . . $ nft_range_ops# opcxx . . $ nftnl_expr_range#op.;
24 };
25
26 static int
27 nftnl_expr_range_setcxx . . $ nftnl_expr_range_set(211371a3e3842127).(struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type,
28 const void *data, uint32_tcxx . . $ uint32_t# data_len, uint32_tcxx . . $ uint32_t# byteorder)
29 {
30 struct nftnl_expr_rangecxx . . $ nftnl_expr_range# *range = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
31
32 switch(type) {
33 case NFTNL_EXPR_RANGE_SREGcxx . . $ NFTNL_EXPR_RANGE_SREG.:
34 memcpycxx . . $ memcpy(cc817cb175f282d).(&range->sregcxx . . $ nftnl_expr_range#sreg., data, data_len);
35 break;
36 case NFTNL_EXPR_RANGE_OPcxx . . $ NFTNL_EXPR_RANGE_OP.:
37 memcpycxx . . $ memcpy(cc817cb175f282d).(&range->opcxx . . $ nftnl_expr_range#op., data, data_len);
38 break;
39 case NFTNL_EXPR_RANGE_FROM_DATAcxx . . $ NFTNL_EXPR_RANGE_FROM_DATA.:
40 return nftnl_data_cpycxx . . $ nftnl_data_cpy(fe1838cfef5a2dc9).(&range->data_fromcxx . . $ nftnl_expr_range#data_from., data,
41 data_len, byteorder, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
42 case NFTNL_EXPR_RANGE_TO_DATAcxx . . $ NFTNL_EXPR_RANGE_TO_DATA.:
43 return nftnl_data_cpycxx . . $ nftnl_data_cpy(fe1838cfef5a2dc9).(&range->data_tocxx . . $ nftnl_expr_range#data_to., data,
44 data_len, byteorder, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
45 }
46 return 0;
47 }
48
49 static const void *nftnl_expr_range_getcxx . . $ nftnl_expr_range_get(9de263c4f9e71c4b).(const struct nftnl_exprcxx . . $ nftnl_expr# *e,
50 uint16_tcxx . . $ uint16_t# type, uint32_tcxx . . $ uint32_t# *data_len)
51 {
52 struct nftnl_expr_rangecxx . . $ nftnl_expr_range# *range = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
53
54 switch(type) {
55 case NFTNL_EXPR_RANGE_SREGcxx . . $ NFTNL_EXPR_RANGE_SREG.:
56 *data_len = sizeof(range->sregcxx . . $ nftnl_expr_range#sreg.);
57 return &range->sregcxx . . $ nftnl_expr_range#sreg.;
58 case NFTNL_EXPR_RANGE_OPcxx . . $ NFTNL_EXPR_RANGE_OP.:
59 *data_len = sizeof(range->opcxx . . $ nftnl_expr_range#op.);
60 return &range->opcxx . . $ nftnl_expr_range#op.;
61 case NFTNL_EXPR_RANGE_FROM_DATAcxx . . $ NFTNL_EXPR_RANGE_FROM_DATA.:
62 *data_len = range->data_fromcxx . . $ nftnl_expr_range#data_from..lencxx . . $ nftnl_data_reg#$anonymous_type_2#len.;
63 return &range->data_fromcxx . . $ nftnl_expr_range#data_from..valcxx . . $ nftnl_data_reg#$anonymous_type_2#val.;
64 case NFTNL_EXPR_RANGE_TO_DATAcxx . . $ NFTNL_EXPR_RANGE_TO_DATA.:
65 *data_len = range->data_tocxx . . $ nftnl_expr_range#data_to..lencxx . . $ nftnl_data_reg#$anonymous_type_2#len.;
66 return &range->data_tocxx . . $ nftnl_expr_range#data_to..valcxx . . $ nftnl_data_reg#$anonymous_type_2#val.;
67 }
68 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
69 }
70
71 static int nftnl_expr_range_cbcxx . . $ nftnl_expr_range_cb(de2ef18c48fd675c).(const struct nlattrcxx . . $ nlattr# *attr, void *data)
72 {
73 const struct nlattrcxx . . $ nlattr# **tb = data;
74 int type = mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr);
75
76 if (mnl_attr_type_validcxx . . $ mnl_attr_type_valid(47df1cce1c7be855).(attr, NFTA_RANGE_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:725:9`!) < 0)
77 return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!;
78
79 switch(type) {
80 case NFTA_RANGE_SREGcxx . . $ NFTA_RANGE_SREG.:
81 case NFTA_RANGE_OPcxx . . $ NFTA_RANGE_OP.:
82 if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_U32cxx . . $ MNL_TYPE_U32.) < 0)
83 abi_breakagecxx . . $ `include/utils.h:28:9`!();
84 break;
85 case NFTA_RANGE_FROM_DATAcxx . . $ NFTA_RANGE_FROM_DATA.:
86 case NFTA_RANGE_TO_DATAcxx . . $ NFTA_RANGE_TO_DATA.:
87 if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_BINARYcxx . . $ MNL_TYPE_BINARY.) < 0)
88 abi_breakagecxx . . $ `include/utils.h:28:9`!();
89 break;
90 }
91
92 tb[type] = attr;
93 return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!;
94 }
95
96 static void
97 nftnl_expr_range_buildcxx . . $ nftnl_expr_range_build(93c7528a9cf90b41).(struct nlmsghdrcxx . . $ nlmsghdr# *nlh, const struct nftnl_exprcxx . . $ nftnl_expr# *e)
98 {
99 struct nftnl_expr_rangecxx . . $ nftnl_expr_range# *range = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
100
101 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_RANGE_SREGcxx . . $ NFTNL_EXPR_RANGE_SREG.))
102 mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_RANGE_SREGcxx . . $ NFTA_RANGE_SREG., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(range->sreg));
103 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_RANGE_OPcxx . . $ NFTNL_EXPR_RANGE_OP.))
104 mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_RANGE_OPcxx . . $ NFTA_RANGE_OP., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(range->op));
105 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_RANGE_FROM_DATAcxx . . $ NFTNL_EXPR_RANGE_FROM_DATA.)) {
106 struct nlattrcxx . . $ nlattr# *nest;
107
108 nest = mnl_attr_nest_startcxx . . $ mnl_attr_nest_start(c8ed348a075426e7).(nlh, NFTA_RANGE_FROM_DATAcxx . . $ NFTA_RANGE_FROM_DATA.);
109 mnl_attr_putcxx . . $ mnl_attr_put(612d908c39ff741d).(nlh, NFTA_DATA_VALUEcxx . . $ NFTA_DATA_VALUE., range->data_fromcxx . . $ nftnl_expr_range#data_from..lencxx . . $ nftnl_data_reg#$anonymous_type_2#len.,
110 range->data_fromcxx . . $ nftnl_expr_range#data_from..valcxx . . $ nftnl_data_reg#$anonymous_type_2#val.);
111 mnl_attr_nest_endcxx . . $ mnl_attr_nest_end(564f73625c62ba58).(nlh, nest);
112 }
113 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_RANGE_TO_DATAcxx . . $ NFTNL_EXPR_RANGE_TO_DATA.)) {
114 struct nlattrcxx . . $ nlattr# *nest;
115
116 nest = mnl_attr_nest_startcxx . . $ mnl_attr_nest_start(c8ed348a075426e7).(nlh, NFTA_RANGE_TO_DATAcxx . . $ NFTA_RANGE_TO_DATA.);
117 mnl_attr_putcxx . . $ mnl_attr_put(612d908c39ff741d).(nlh, NFTA_DATA_VALUEcxx . . $ NFTA_DATA_VALUE., range->data_tocxx . . $ nftnl_expr_range#data_to..lencxx . . $ nftnl_data_reg#$anonymous_type_2#len.,
118 range->data_tocxx . . $ nftnl_expr_range#data_to..valcxx . . $ nftnl_data_reg#$anonymous_type_2#val.);
119 mnl_attr_nest_endcxx . . $ mnl_attr_nest_end(564f73625c62ba58).(nlh, nest);
120 }
121 }
122
123 static int
124 nftnl_expr_range_parsecxx . . $ nftnl_expr_range_parse(ed61207049d8af9a).(struct nftnl_exprcxx . . $ nftnl_expr# *e, struct nlattrcxx . . $ nlattr# *attr)
125 {
126 struct nftnl_expr_rangecxx . . $ nftnl_expr_range# *range = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
127 struct nlattrcxx . . $ nlattr# *tb[NFTA_RANGE_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:725:9`!+1] = {};
128 int ret = 0;
129
130 if (mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_expr_range_cbcxx . . $ nftnl_expr_range_cb(de2ef18c48fd675c)., tb) < 0)
131 return -1;
132
133 if (tb[NFTA_RANGE_SREGcxx . . $ NFTA_RANGE_SREG.]) {
134 range->sregcxx . . $ nftnl_expr_range#sreg. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_RANGE_SREG]));
135 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTA_RANGE_SREGcxx . . $ NFTA_RANGE_SREG.);
136 }
137 if (tb[NFTA_RANGE_OPcxx . . $ NFTA_RANGE_OP.]) {
138 range->opcxx . . $ nftnl_expr_range#op. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_RANGE_OP]));
139 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTA_RANGE_OPcxx . . $ NFTA_RANGE_OP.);
140 }
141 if (tb[NFTA_RANGE_FROM_DATAcxx . . $ NFTA_RANGE_FROM_DATA.]) {
142 ret = nftnl_parse_datacxx . . $ nftnl_parse_data(1f43ec2decb5ffec).(&range->data_fromcxx . . $ nftnl_expr_range#data_from.,
143 tb[NFTA_RANGE_FROM_DATAcxx . . $ NFTA_RANGE_FROM_DATA.], NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
144 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTA_RANGE_FROM_DATAcxx . . $ NFTA_RANGE_FROM_DATA.);
145 }
146 if (tb[NFTA_RANGE_TO_DATAcxx . . $ NFTA_RANGE_TO_DATA.]) {
147 ret = nftnl_parse_datacxx . . $ nftnl_parse_data(1f43ec2decb5ffec).(&range->data_tocxx . . $ nftnl_expr_range#data_to.,
148 tb[NFTA_RANGE_TO_DATAcxx . . $ NFTA_RANGE_TO_DATA.], NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
149 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTA_RANGE_TO_DATAcxx . . $ NFTA_RANGE_TO_DATA.);
150 }
151
152 return ret;
153 }
154
155 static const char *expr_range_strcxx . . $ expr_range_str.[] = {
156 [NFT_RANGE_EQcxx . . $ NFT_RANGE_EQ.] = "eq",
157 [NFT_RANGE_NEQcxx . . $ NFT_RANGE_NEQ.] = "neq",
158 };
159
160 static const char *range2strcxx . . $ range2str(81a3ae90da26a711).(uint32_tcxx . . $ uint32_t# op)
161 {
162 if (op > NFT_RANGE_NEQcxx . . $ NFT_RANGE_NEQ.)
163 return "unknown";
164
165 return expr_range_strcxx . . $ expr_range_str.[op];
166 }
167
168 static int nftnl_expr_range_snprintfcxx . . $ nftnl_expr_range_snprintf(2b4af69501aef780).(char *buf, size_t remain,
169 uint32_tcxx . . $ uint32_t# flags, const struct nftnl_exprcxx . . $ nftnl_expr# *e)
170 {
171 struct nftnl_expr_rangecxx . . $ nftnl_expr_range# *range = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
172 int offset = 0, ret;
173
174 ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, remain, "%s reg %u ",
175 range2strcxx . . $ range2str(81a3ae90da26a711).(range->opcxx . . $ nftnl_expr_range#op.), range->sregcxx . . $ nftnl_expr_range#sreg.);
176 SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset);
177
178 ret = nftnl_data_reg_snprintfcxx . . $ nftnl_data_reg_snprintf(92088fbd4b6db60b).(buf + offset, remain, &range->data_fromcxx . . $ nftnl_expr_range#data_from.,
179 0, DATA_VALUEcxx . . $ DATA_VALUE.);
180 SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset);
181
182 ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, remain, " ");
183 SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset);
184
185 ret = nftnl_data_reg_snprintfcxx . . $ nftnl_data_reg_snprintf(92088fbd4b6db60b).(buf + offset, remain, &range->data_tocxx . . $ nftnl_expr_range#data_to.,
186 0, DATA_VALUEcxx . . $ DATA_VALUE.);
187 SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset);
188
189 ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, remain, " ");
190 SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset);
191
192 return offset;
193 }
194
195 static struct attr_policycxx . . $ attr_policy# range_attr_policycxx . . $ range_attr_policy.[__NFTNL_EXPR_RANGE_MAXcxx . . $ __NFTNL_EXPR_RANGE_MAX.] = {
196 [NFTNL_EXPR_RANGE_SREGcxx . . $ NFTNL_EXPR_RANGE_SREG.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) },
197 [NFTNL_EXPR_RANGE_OPcxx . . $ NFTNL_EXPR_RANGE_OP.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) },
198 [NFTNL_EXPR_RANGE_FROM_DATAcxx . . $ NFTNL_EXPR_RANGE_FROM_DATA.] = { .maxlen = NFT_DATA_VALUE_MAXLENcxx . . $ `include/linux/netfilter/nf_tables.h:522:9`! },
199 [NFTNL_EXPR_RANGE_TO_DATAcxx . . $ NFTNL_EXPR_RANGE_TO_DATA.] = { .maxlen = NFT_DATA_VALUE_MAXLENcxx . . $ `include/linux/netfilter/nf_tables.h:522:9`! },
200 };
201
202 struct expr_opscxx . . $ expr_ops# expr_ops_rangecxx . . $ expr_ops_range. = {
203 .name = "range",
204 .alloc_len = sizeof(struct nftnl_expr_rangecxx . . $ nftnl_expr_range#),
205 .nftnl_max_attr = __NFTNL_EXPR_RANGE_MAXcxx . . $ __NFTNL_EXPR_RANGE_MAX. - 1,
206 .attr_policy = range_attr_policycxx . . $ range_attr_policy.,
207 .set = nftnl_expr_range_setcxx . . $ nftnl_expr_range_set(211371a3e3842127).,
208 .get = nftnl_expr_range_getcxx . . $ nftnl_expr_range_get(9de263c4f9e71c4b).,
209 .parse = nftnl_expr_range_parsecxx . . $ nftnl_expr_range_parse(ed61207049d8af9a).,
210 .build = nftnl_expr_range_buildcxx . . $ nftnl_expr_range_build(93c7528a9cf90b41).,
211 .output = nftnl_expr_range_snprintfcxx . . $ nftnl_expr_range_snprintf(2b4af69501aef780).,
212 };