Debian Codegraph @

src/expr/hash.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/expr/hash.c`/
2 /*
3 * (C) 2016 by Laura Garcia <[email protected]>
4 */
5
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <arpa/inet.h>
10 #include <errno.h>
11 #include <linux/netfilter/nf_tables.h>cxx . . $ `<file>/include/linux/netfilter/nf_tables.h`/
12
13 #include "internal.h"cxx . . $ `<file>/include/internal.h`/
14 #include <libmnl/libmnl.h>
15 #include <libnftnl/expr.h>cxx . . $ `<file>/include/libnftnl/expr.h`/
16 #include <libnftnl/rule.h>cxx . . $ `<file>/include/libnftnl/rule.h`/
17
18 struct nftnl_expr_hashcxx . . $ nftnl_expr_hash# {
19 enum nft_hash_typescxx . . $ nft_hash_types# typecxx . . $ nftnl_expr_hash#type.;
20 enum nft_registerscxx . . $ nft_registers# sregcxx . . $ nftnl_expr_hash#sreg.;
21 enum nft_registerscxx . . $ nft_registers# dregcxx . . $ nftnl_expr_hash#dreg.;
22 unsigned int lencxx . . $ nftnl_expr_hash#len.;
23 unsigned int moduluscxx . . $ nftnl_expr_hash#modulus.;
24 unsigned int seedcxx . . $ nftnl_expr_hash#seed.;
25 unsigned int offsetcxx . . $ nftnl_expr_hash#offset.;
26 };
27
28 static int
29 nftnl_expr_hash_setcxx . . $ nftnl_expr_hash_set(211371a3e3842127).(struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type,
30 const void *data, uint32_tcxx . . $ uint32_t# data_len, uint32_tcxx . . $ uint32_t# byteorder)
31 {
32 struct nftnl_expr_hashcxx . . $ nftnl_expr_hash# *hash = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
33 switch (type) {
34 case NFTNL_EXPR_HASH_SREGcxx . . $ NFTNL_EXPR_HASH_SREG.:
35 memcpycxx . . $ memcpy(cc817cb175f282d).(&hash->sregcxx . . $ nftnl_expr_hash#sreg., data, data_len);
36 break;
37 case NFTNL_EXPR_HASH_DREGcxx . . $ NFTNL_EXPR_HASH_DREG.:
38 memcpycxx . . $ memcpy(cc817cb175f282d).(&hash->dregcxx . . $ nftnl_expr_hash#dreg., data, data_len);
39 break;
40 case NFTNL_EXPR_HASH_LENcxx . . $ NFTNL_EXPR_HASH_LEN.:
41 memcpycxx . . $ memcpy(cc817cb175f282d).(&hash->lencxx . . $ nftnl_expr_hash#len., data, data_len);
42 break;
43 case NFTNL_EXPR_HASH_MODULUScxx . . $ NFTNL_EXPR_HASH_MODULUS.:
44 memcpycxx . . $ memcpy(cc817cb175f282d).(&hash->moduluscxx . . $ nftnl_expr_hash#modulus., data, data_len);
45 break;
46 case NFTNL_EXPR_HASH_SEEDcxx . . $ NFTNL_EXPR_HASH_SEED.:
47 memcpycxx . . $ memcpy(cc817cb175f282d).(&hash->seedcxx . . $ nftnl_expr_hash#seed., data, data_len);
48 break;
49 case NFTNL_EXPR_HASH_OFFSETcxx . . $ NFTNL_EXPR_HASH_OFFSET.:
50 memcpycxx . . $ memcpy(cc817cb175f282d).(&hash->offsetcxx . . $ nftnl_expr_hash#offset., data, data_len);
51 break;
52 case NFTNL_EXPR_HASH_TYPEcxx . . $ NFTNL_EXPR_HASH_TYPE.:
53 memcpycxx . . $ memcpy(cc817cb175f282d).(&hash->typecxx . . $ nftnl_expr_hash#type., data, data_len);
54 break;
55 default:
56 return -1;
57 }
58 return 0;
59 }
60
61 static const void *
62 nftnl_expr_hash_getcxx . . $ nftnl_expr_hash_get(9de263c4f9e71c4b).(const struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type,
63 uint32_tcxx . . $ uint32_t# *data_len)
64 {
65 struct nftnl_expr_hashcxx . . $ nftnl_expr_hash# *hash = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
66
67 switch (type) {
68 case NFTNL_EXPR_HASH_SREGcxx . . $ NFTNL_EXPR_HASH_SREG.:
69 *data_len = sizeof(hash->sregcxx . . $ nftnl_expr_hash#sreg.);
70 return &hash->sregcxx . . $ nftnl_expr_hash#sreg.;
71 case NFTNL_EXPR_HASH_DREGcxx . . $ NFTNL_EXPR_HASH_DREG.:
72 *data_len = sizeof(hash->dregcxx . . $ nftnl_expr_hash#dreg.);
73 return &hash->dregcxx . . $ nftnl_expr_hash#dreg.;
74 case NFTNL_EXPR_HASH_LENcxx . . $ NFTNL_EXPR_HASH_LEN.:
75 *data_len = sizeof(hash->lencxx . . $ nftnl_expr_hash#len.);
76 return &hash->lencxx . . $ nftnl_expr_hash#len.;
77 case NFTNL_EXPR_HASH_MODULUScxx . . $ NFTNL_EXPR_HASH_MODULUS.:
78 *data_len = sizeof(hash->moduluscxx . . $ nftnl_expr_hash#modulus.);
79 return &hash->moduluscxx . . $ nftnl_expr_hash#modulus.;
80 case NFTNL_EXPR_HASH_SEEDcxx . . $ NFTNL_EXPR_HASH_SEED.:
81 *data_len = sizeof(hash->seedcxx . . $ nftnl_expr_hash#seed.);
82 return &hash->seedcxx . . $ nftnl_expr_hash#seed.;
83 case NFTNL_EXPR_HASH_OFFSETcxx . . $ NFTNL_EXPR_HASH_OFFSET.:
84 *data_len = sizeof(hash->offsetcxx . . $ nftnl_expr_hash#offset.);
85 return &hash->offsetcxx . . $ nftnl_expr_hash#offset.;
86 case NFTNL_EXPR_HASH_TYPEcxx . . $ NFTNL_EXPR_HASH_TYPE.:
87 *data_len = sizeof(hash->typecxx . . $ nftnl_expr_hash#type.);
88 return &hash->typecxx . . $ nftnl_expr_hash#type.;
89 }
90 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
91 }
92
93 static int nftnl_expr_hash_cbcxx . . $ nftnl_expr_hash_cb(de2ef18c48fd675c).(const struct nlattrcxx . . $ nlattr# *attr, void *data)
94 {
95 const struct nlattrcxx . . $ nlattr# **tb = data;
96 int type = mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr);
97
98 if (mnl_attr_type_validcxx . . $ mnl_attr_type_valid(47df1cce1c7be855).(attr, NFTA_HASH_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1062:9`!) < 0)
99 return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!;
100
101 switch (type) {
102 case NFTA_HASH_SREGcxx . . $ NFTA_HASH_SREG.:
103 case NFTA_HASH_DREGcxx . . $ NFTA_HASH_DREG.:
104 case NFTA_HASH_LENcxx . . $ NFTA_HASH_LEN.:
105 case NFTA_HASH_MODULUScxx . . $ NFTA_HASH_MODULUS.:
106 case NFTA_HASH_SEEDcxx . . $ NFTA_HASH_SEED.:
107 case NFTA_HASH_OFFSETcxx . . $ NFTA_HASH_OFFSET.:
108 case NFTA_HASH_TYPEcxx . . $ NFTA_HASH_TYPE.:
109 if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_U32cxx . . $ MNL_TYPE_U32.) < 0)
110 abi_breakagecxx . . $ `include/utils.h:28:9`!();
111 break;
112 }
113
114 tb[type] = attr;
115 return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!;
116 }
117
118 static void
119 nftnl_expr_hash_buildcxx . . $ nftnl_expr_hash_build(93c7528a9cf90b41).(struct nlmsghdrcxx . . $ nlmsghdr# *nlh, const struct nftnl_exprcxx . . $ nftnl_expr# *e)
120 {
121 struct nftnl_expr_hashcxx . . $ nftnl_expr_hash# *hash = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
122
123 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_HASH_SREGcxx . . $ NFTNL_EXPR_HASH_SREG.))
124 mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_HASH_SREGcxx . . $ NFTA_HASH_SREG., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(hash->sreg));
125 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_HASH_DREGcxx . . $ NFTNL_EXPR_HASH_DREG.))
126 mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_HASH_DREGcxx . . $ NFTA_HASH_DREG., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(hash->dreg));
127 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_HASH_LENcxx . . $ NFTNL_EXPR_HASH_LEN.))
128 mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_HASH_LENcxx . . $ NFTA_HASH_LEN., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(hash->len));
129 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_HASH_MODULUScxx . . $ NFTNL_EXPR_HASH_MODULUS.))
130 mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_HASH_MODULUScxx . . $ NFTA_HASH_MODULUS., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(hash->modulus));
131 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_HASH_SEEDcxx . . $ NFTNL_EXPR_HASH_SEED.))
132 mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_HASH_SEEDcxx . . $ NFTA_HASH_SEED., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(hash->seed));
133 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_HASH_OFFSETcxx . . $ NFTNL_EXPR_HASH_OFFSET.))
134 mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_HASH_OFFSETcxx . . $ NFTA_HASH_OFFSET., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(hash->offset));
135 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_HASH_TYPEcxx . . $ NFTNL_EXPR_HASH_TYPE.))
136 mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_HASH_TYPEcxx . . $ NFTA_HASH_TYPE., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(hash->type));
137 }
138
139 static int
140 nftnl_expr_hash_parsecxx . . $ nftnl_expr_hash_parse(ed61207049d8af9a).(struct nftnl_exprcxx . . $ nftnl_expr# *e, struct nlattrcxx . . $ nlattr# *attr)
141 {
142 struct nftnl_expr_hashcxx . . $ nftnl_expr_hash# *hash = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
143 struct nlattrcxx . . $ nlattr# *tb[NFTA_HASH_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1062:9`!+1] = {};
144 int ret = 0;
145
146 if (mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_expr_hash_cbcxx . . $ nftnl_expr_hash_cb(de2ef18c48fd675c)., tb) < 0)
147 return -1;
148
149 if (tb[NFTA_HASH_SREGcxx . . $ NFTA_HASH_SREG.]) {
150 hash->sregcxx . . $ nftnl_expr_hash#sreg. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_HASH_SREG]));
151 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_HASH_SREGcxx . . $ NFTNL_EXPR_HASH_SREG.);
152 }
153 if (tb[NFTA_HASH_DREGcxx . . $ NFTA_HASH_DREG.]) {
154 hash->dregcxx . . $ nftnl_expr_hash#dreg. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_HASH_DREG]));
155 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_HASH_DREGcxx . . $ NFTNL_EXPR_HASH_DREG.);
156 }
157 if (tb[NFTA_HASH_LENcxx . . $ NFTA_HASH_LEN.]) {
158 hash->lencxx . . $ nftnl_expr_hash#len. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_HASH_LEN]));
159 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_HASH_LENcxx . . $ NFTNL_EXPR_HASH_LEN.);
160 }
161 if (tb[NFTA_HASH_MODULUScxx . . $ NFTA_HASH_MODULUS.]) {
162 hash->moduluscxx . . $ nftnl_expr_hash#modulus. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_HASH_MODULUS]));
163 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_HASH_MODULUScxx . . $ NFTNL_EXPR_HASH_MODULUS.);
164 }
165 if (tb[NFTA_HASH_SEEDcxx . . $ NFTA_HASH_SEED.]) {
166 hash->seedcxx . . $ nftnl_expr_hash#seed. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_HASH_SEED]));
167 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_HASH_SEEDcxx . . $ NFTNL_EXPR_HASH_SEED.);
168 }
169 if (tb[NFTA_HASH_OFFSETcxx . . $ NFTA_HASH_OFFSET.]) {
170 hash->offsetcxx . . $ nftnl_expr_hash#offset. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_HASH_OFFSET]));
171 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_HASH_OFFSETcxx . . $ NFTNL_EXPR_HASH_OFFSET.);
172 }
173 if (tb[NFTA_HASH_TYPEcxx . . $ NFTA_HASH_TYPE.]) {
174 hash->typecxx . . $ nftnl_expr_hash#type. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_HASH_TYPE]));
175 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_HASH_TYPEcxx . . $ NFTNL_EXPR_HASH_TYPE.);
176 }
177
178 return ret;
179 }
180
181 static int
182 nftnl_expr_hash_snprintfcxx . . $ nftnl_expr_hash_snprintf(2b4af69501aef780).(char *buf, size_t remain,
183 uint32_tcxx . . $ uint32_t# flags, const struct nftnl_exprcxx . . $ nftnl_expr# *e)
184 {
185 struct nftnl_expr_hashcxx . . $ nftnl_expr_hash# *hash = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
186 int offset = 0, ret;
187
188 switch (hash->typecxx . . $ nftnl_expr_hash#type.) {
189 case NFT_HASH_SYMcxx . . $ NFT_HASH_SYM.:
190 ret =
191 snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, remain, "reg %u = symhash() %% mod %u ",
192 hash->dregcxx . . $ nftnl_expr_hash#dreg.,
193 hash->moduluscxx . . $ nftnl_expr_hash#modulus.);
194 SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset);
195 break;
196 case NFT_HASH_JENKINScxx . . $ NFT_HASH_JENKINS.:
197 default:
198 ret =
199 snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, remain,
200 "reg %u = jhash(reg %u, %u, 0x%x) %% mod %u ",
201 hash->dregcxx . . $ nftnl_expr_hash#dreg., hash->sregcxx . . $ nftnl_expr_hash#sreg., hash->lencxx . . $ nftnl_expr_hash#len., hash->seedcxx . . $ nftnl_expr_hash#seed.,
202 hash->moduluscxx . . $ nftnl_expr_hash#modulus.);
203 SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset);
204 break;
205 }
206
207 if (hash->offsetcxx . . $ nftnl_expr_hash#offset.) {
208 ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, remain, "offset %u ",
209 hash->offsetcxx . . $ nftnl_expr_hash#offset.);
210 SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset);
211 }
212
213 return offset;
214 }
215
216 static struct attr_policycxx . . $ attr_policy# hash_attr_policycxx . . $ hash_attr_policy.[__NFTNL_EXPR_HASH_MAXcxx . . $ __NFTNL_EXPR_HASH_MAX.] = {
217 [NFTNL_EXPR_HASH_SREGcxx . . $ NFTNL_EXPR_HASH_SREG.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) },
218 [NFTNL_EXPR_HASH_DREGcxx . . $ NFTNL_EXPR_HASH_DREG.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) },
219 [NFTNL_EXPR_HASH_LENcxx . . $ NFTNL_EXPR_HASH_LEN.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) },
220 [NFTNL_EXPR_HASH_MODULUScxx . . $ NFTNL_EXPR_HASH_MODULUS.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) },
221 [NFTNL_EXPR_HASH_SEEDcxx . . $ NFTNL_EXPR_HASH_SEED.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) },
222 [NFTNL_EXPR_HASH_OFFSETcxx . . $ NFTNL_EXPR_HASH_OFFSET.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) },
223 [NFTNL_EXPR_HASH_TYPEcxx . . $ NFTNL_EXPR_HASH_TYPE.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) },
224 };
225
226 struct expr_opscxx . . $ expr_ops# expr_ops_hashcxx . . $ expr_ops_hash. = {
227 .name = "hash",
228 .alloc_len = sizeof(struct nftnl_expr_hashcxx . . $ nftnl_expr_hash#),
229 .nftnl_max_attr = __NFTNL_EXPR_HASH_MAXcxx . . $ __NFTNL_EXPR_HASH_MAX. - 1,
230 .attr_policy = hash_attr_policycxx . . $ hash_attr_policy.,
231 .set = nftnl_expr_hash_setcxx . . $ nftnl_expr_hash_set(211371a3e3842127).,
232 .get = nftnl_expr_hash_getcxx . . $ nftnl_expr_hash_get(9de263c4f9e71c4b).,
233 .parse = nftnl_expr_hash_parsecxx . . $ nftnl_expr_hash_parse(ed61207049d8af9a).,
234 .build = nftnl_expr_hash_buildcxx . . $ nftnl_expr_hash_build(93c7528a9cf90b41).,
235 .output = nftnl_expr_hash_snprintfcxx . . $ nftnl_expr_hash_snprintf(2b4af69501aef780).,
236 };