Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
96518518 | 2 | /* |
ef1f7df9 | 3 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> |
96518518 | 4 | * |
96518518 PM |
5 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
6 | */ | |
7 | ||
8 | #include <linux/kernel.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/netlink.h> | |
12 | #include <linux/netfilter.h> | |
13 | #include <linux/netfilter/nf_tables.h> | |
14 | #include <net/netfilter/nf_tables_core.h> | |
15 | #include <net/netfilter/nf_tables.h> | |
bd8699e9 | 16 | #include <net/netfilter/nf_tables_offload.h> |
96518518 PM |
17 | |
18 | struct nft_bitwise { | |
4f16d25c | 19 | u8 sreg; |
345023b0 | 20 | u8 dreg; |
9d1f9799 | 21 | enum nft_bitwise_ops op:8; |
96518518 PM |
22 | u8 len; |
23 | struct nft_data mask; | |
24 | struct nft_data xor; | |
779f725e | 25 | struct nft_data data; |
96518518 PM |
26 | }; |
27 | ||
71d6ded3 JS |
28 | static void nft_bitwise_eval_bool(u32 *dst, const u32 *src, |
29 | const struct nft_bitwise *priv) | |
30 | { | |
31 | unsigned int i; | |
32 | ||
c70b921f | 33 | for (i = 0; i < DIV_ROUND_UP(priv->len, sizeof(u32)); i++) |
71d6ded3 JS |
34 | dst[i] = (src[i] & priv->mask.data[i]) ^ priv->xor.data[i]; |
35 | } | |
36 | ||
567d746b JS |
37 | static void nft_bitwise_eval_lshift(u32 *dst, const u32 *src, |
38 | const struct nft_bitwise *priv) | |
39 | { | |
40 | u32 shift = priv->data.data[0]; | |
41 | unsigned int i; | |
42 | u32 carry = 0; | |
43 | ||
44 | for (i = DIV_ROUND_UP(priv->len, sizeof(u32)); i > 0; i--) { | |
45 | dst[i - 1] = (src[i - 1] << shift) | carry; | |
46 | carry = src[i - 1] >> (BITS_PER_TYPE(u32) - shift); | |
47 | } | |
48 | } | |
49 | ||
50 | static void nft_bitwise_eval_rshift(u32 *dst, const u32 *src, | |
51 | const struct nft_bitwise *priv) | |
52 | { | |
53 | u32 shift = priv->data.data[0]; | |
54 | unsigned int i; | |
55 | u32 carry = 0; | |
56 | ||
57 | for (i = 0; i < DIV_ROUND_UP(priv->len, sizeof(u32)); i++) { | |
58 | dst[i] = carry | (src[i] >> shift); | |
59 | carry = src[i] << (BITS_PER_TYPE(u32) - shift); | |
60 | } | |
61 | } | |
62 | ||
10870dd8 FW |
63 | void nft_bitwise_eval(const struct nft_expr *expr, |
64 | struct nft_regs *regs, const struct nft_pktinfo *pkt) | |
96518518 PM |
65 | { |
66 | const struct nft_bitwise *priv = nft_expr_priv(expr); | |
49499c3e PM |
67 | const u32 *src = ®s->data[priv->sreg]; |
68 | u32 *dst = ®s->data[priv->dreg]; | |
96518518 | 69 | |
71d6ded3 JS |
70 | switch (priv->op) { |
71 | case NFT_BITWISE_BOOL: | |
72 | nft_bitwise_eval_bool(dst, src, priv); | |
73 | break; | |
567d746b JS |
74 | case NFT_BITWISE_LSHIFT: |
75 | nft_bitwise_eval_lshift(dst, src, priv); | |
76 | break; | |
77 | case NFT_BITWISE_RSHIFT: | |
78 | nft_bitwise_eval_rshift(dst, src, priv); | |
79 | break; | |
71d6ded3 | 80 | } |
96518518 PM |
81 | } |
82 | ||
83 | static const struct nla_policy nft_bitwise_policy[NFTA_BITWISE_MAX + 1] = { | |
84 | [NFTA_BITWISE_SREG] = { .type = NLA_U32 }, | |
85 | [NFTA_BITWISE_DREG] = { .type = NLA_U32 }, | |
86 | [NFTA_BITWISE_LEN] = { .type = NLA_U32 }, | |
87 | [NFTA_BITWISE_MASK] = { .type = NLA_NESTED }, | |
88 | [NFTA_BITWISE_XOR] = { .type = NLA_NESTED }, | |
9d1f9799 | 89 | [NFTA_BITWISE_OP] = { .type = NLA_U32 }, |
779f725e | 90 | [NFTA_BITWISE_DATA] = { .type = NLA_NESTED }, |
96518518 PM |
91 | }; |
92 | ||
3f8d9eb0 JS |
93 | static int nft_bitwise_init_bool(struct nft_bitwise *priv, |
94 | const struct nlattr *const tb[]) | |
95 | { | |
341b6941 PNA |
96 | struct nft_data_desc mask = { |
97 | .type = NFT_DATA_VALUE, | |
98 | .size = sizeof(priv->mask), | |
99 | .len = priv->len, | |
100 | }; | |
101 | struct nft_data_desc xor = { | |
102 | .type = NFT_DATA_VALUE, | |
103 | .size = sizeof(priv->xor), | |
104 | .len = priv->len, | |
105 | }; | |
3f8d9eb0 JS |
106 | int err; |
107 | ||
779f725e JS |
108 | if (tb[NFTA_BITWISE_DATA]) |
109 | return -EINVAL; | |
110 | ||
3f8d9eb0 JS |
111 | if (!tb[NFTA_BITWISE_MASK] || |
112 | !tb[NFTA_BITWISE_XOR]) | |
113 | return -EINVAL; | |
114 | ||
341b6941 | 115 | err = nft_data_init(NULL, &priv->mask, &mask, tb[NFTA_BITWISE_MASK]); |
3f8d9eb0 JS |
116 | if (err < 0) |
117 | return err; | |
3f8d9eb0 | 118 | |
341b6941 | 119 | err = nft_data_init(NULL, &priv->xor, &xor, tb[NFTA_BITWISE_XOR]); |
3f8d9eb0 | 120 | if (err < 0) |
341b6941 | 121 | goto err_xor_err; |
3f8d9eb0 JS |
122 | |
123 | return 0; | |
00bd4352 | 124 | |
341b6941 | 125 | err_xor_err: |
049dee95 | 126 | nft_data_release(&priv->mask, mask.type); |
341b6941 | 127 | |
3f8d9eb0 JS |
128 | return err; |
129 | } | |
130 | ||
567d746b JS |
131 | static int nft_bitwise_init_shift(struct nft_bitwise *priv, |
132 | const struct nlattr *const tb[]) | |
133 | { | |
341b6941 PNA |
134 | struct nft_data_desc desc = { |
135 | .type = NFT_DATA_VALUE, | |
136 | .size = sizeof(priv->data), | |
137 | .len = sizeof(u32), | |
138 | }; | |
567d746b JS |
139 | int err; |
140 | ||
141 | if (tb[NFTA_BITWISE_MASK] || | |
142 | tb[NFTA_BITWISE_XOR]) | |
143 | return -EINVAL; | |
144 | ||
145 | if (!tb[NFTA_BITWISE_DATA]) | |
146 | return -EINVAL; | |
147 | ||
341b6941 | 148 | err = nft_data_init(NULL, &priv->data, &desc, tb[NFTA_BITWISE_DATA]); |
567d746b JS |
149 | if (err < 0) |
150 | return err; | |
341b6941 PNA |
151 | |
152 | if (priv->data.data[0] >= BITS_PER_TYPE(u32)) { | |
153 | nft_data_release(&priv->data, desc.type); | |
567d746b JS |
154 | return -EINVAL; |
155 | } | |
156 | ||
157 | return 0; | |
158 | } | |
159 | ||
96518518 PM |
160 | static int nft_bitwise_init(const struct nft_ctx *ctx, |
161 | const struct nft_expr *expr, | |
162 | const struct nlattr * const tb[]) | |
163 | { | |
164 | struct nft_bitwise *priv = nft_expr_priv(expr); | |
36b701fa | 165 | u32 len; |
96518518 PM |
166 | int err; |
167 | ||
36b701fa LGL |
168 | err = nft_parse_u32_check(tb[NFTA_BITWISE_LEN], U8_MAX, &len); |
169 | if (err < 0) | |
170 | return err; | |
171 | ||
172 | priv->len = len; | |
173 | ||
4f16d25c PNA |
174 | err = nft_parse_register_load(tb[NFTA_BITWISE_SREG], &priv->sreg, |
175 | priv->len); | |
96518518 PM |
176 | if (err < 0) |
177 | return err; | |
178 | ||
345023b0 PNA |
179 | err = nft_parse_register_store(ctx, tb[NFTA_BITWISE_DREG], |
180 | &priv->dreg, NULL, NFT_DATA_VALUE, | |
181 | priv->len); | |
96518518 PM |
182 | if (err < 0) |
183 | return err; | |
184 | ||
9d1f9799 JS |
185 | if (tb[NFTA_BITWISE_OP]) { |
186 | priv->op = ntohl(nla_get_be32(tb[NFTA_BITWISE_OP])); | |
187 | switch (priv->op) { | |
188 | case NFT_BITWISE_BOOL: | |
567d746b JS |
189 | case NFT_BITWISE_LSHIFT: |
190 | case NFT_BITWISE_RSHIFT: | |
9d1f9799 JS |
191 | break; |
192 | default: | |
193 | return -EOPNOTSUPP; | |
194 | } | |
195 | } else { | |
196 | priv->op = NFT_BITWISE_BOOL; | |
197 | } | |
198 | ||
3f8d9eb0 JS |
199 | switch(priv->op) { |
200 | case NFT_BITWISE_BOOL: | |
201 | err = nft_bitwise_init_bool(priv, tb); | |
202 | break; | |
567d746b JS |
203 | case NFT_BITWISE_LSHIFT: |
204 | case NFT_BITWISE_RSHIFT: | |
205 | err = nft_bitwise_init_shift(priv, tb); | |
206 | break; | |
71df14b0 | 207 | } |
96518518 | 208 | |
71df14b0 | 209 | return err; |
96518518 PM |
210 | } |
211 | ||
4d57ca2b JS |
212 | static int nft_bitwise_dump_bool(struct sk_buff *skb, |
213 | const struct nft_bitwise *priv) | |
214 | { | |
215 | if (nft_data_dump(skb, NFTA_BITWISE_MASK, &priv->mask, | |
216 | NFT_DATA_VALUE, priv->len) < 0) | |
217 | return -1; | |
218 | ||
219 | if (nft_data_dump(skb, NFTA_BITWISE_XOR, &priv->xor, | |
220 | NFT_DATA_VALUE, priv->len) < 0) | |
221 | return -1; | |
222 | ||
223 | return 0; | |
224 | } | |
225 | ||
567d746b JS |
226 | static int nft_bitwise_dump_shift(struct sk_buff *skb, |
227 | const struct nft_bitwise *priv) | |
228 | { | |
229 | if (nft_data_dump(skb, NFTA_BITWISE_DATA, &priv->data, | |
230 | NFT_DATA_VALUE, sizeof(u32)) < 0) | |
231 | return -1; | |
232 | return 0; | |
233 | } | |
234 | ||
96518518 PM |
235 | static int nft_bitwise_dump(struct sk_buff *skb, const struct nft_expr *expr) |
236 | { | |
237 | const struct nft_bitwise *priv = nft_expr_priv(expr); | |
4d57ca2b | 238 | int err = 0; |
96518518 | 239 | |
b1c96ed3 | 240 | if (nft_dump_register(skb, NFTA_BITWISE_SREG, priv->sreg)) |
577c734a | 241 | return -1; |
b1c96ed3 | 242 | if (nft_dump_register(skb, NFTA_BITWISE_DREG, priv->dreg)) |
577c734a | 243 | return -1; |
96518518 | 244 | if (nla_put_be32(skb, NFTA_BITWISE_LEN, htonl(priv->len))) |
577c734a | 245 | return -1; |
9d1f9799 JS |
246 | if (nla_put_be32(skb, NFTA_BITWISE_OP, htonl(priv->op))) |
247 | return -1; | |
96518518 | 248 | |
4d57ca2b JS |
249 | switch (priv->op) { |
250 | case NFT_BITWISE_BOOL: | |
251 | err = nft_bitwise_dump_bool(skb, priv); | |
567d746b JS |
252 | break; |
253 | case NFT_BITWISE_LSHIFT: | |
254 | case NFT_BITWISE_RSHIFT: | |
255 | err = nft_bitwise_dump_shift(skb, priv); | |
4d57ca2b JS |
256 | break; |
257 | } | |
96518518 | 258 | |
4d57ca2b | 259 | return err; |
96518518 PM |
260 | } |
261 | ||
bd8699e9 PNA |
262 | static struct nft_data zero; |
263 | ||
264 | static int nft_bitwise_offload(struct nft_offload_ctx *ctx, | |
fbf19ddf JS |
265 | struct nft_flow_rule *flow, |
266 | const struct nft_expr *expr) | |
bd8699e9 PNA |
267 | { |
268 | const struct nft_bitwise *priv = nft_expr_priv(expr); | |
de2a6052 | 269 | struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; |
bd8699e9 | 270 | |
ed991d43 JS |
271 | if (priv->op != NFT_BITWISE_BOOL) |
272 | return -EOPNOTSUPP; | |
273 | ||
83c156d3 | 274 | if (memcmp(&priv->xor, &zero, sizeof(priv->xor)) || |
de2a6052 | 275 | priv->sreg != priv->dreg || priv->len != reg->len) |
bd8699e9 PNA |
276 | return -EOPNOTSUPP; |
277 | ||
de2a6052 | 278 | memcpy(®->mask, &priv->mask, sizeof(priv->mask)); |
bd8699e9 PNA |
279 | |
280 | return 0; | |
281 | } | |
282 | ||
be5650f8 PNA |
283 | static bool nft_bitwise_reduce(struct nft_regs_track *track, |
284 | const struct nft_expr *expr) | |
285 | { | |
286 | const struct nft_bitwise *priv = nft_expr_priv(expr); | |
287 | const struct nft_bitwise *bitwise; | |
34cc9e52 PNA |
288 | unsigned int regcount; |
289 | u8 dreg; | |
290 | int i; | |
be5650f8 PNA |
291 | |
292 | if (!track->regs[priv->sreg].selector) | |
293 | return false; | |
294 | ||
31818213 | 295 | bitwise = nft_expr_priv(track->regs[priv->dreg].selector); |
be5650f8 | 296 | if (track->regs[priv->sreg].selector == track->regs[priv->dreg].selector && |
34cc9e52 | 297 | track->regs[priv->sreg].num_reg == 0 && |
be5650f8 PNA |
298 | track->regs[priv->dreg].bitwise && |
299 | track->regs[priv->dreg].bitwise->ops == expr->ops && | |
300 | priv->sreg == bitwise->sreg && | |
301 | priv->dreg == bitwise->dreg && | |
302 | priv->op == bitwise->op && | |
303 | priv->len == bitwise->len && | |
304 | !memcmp(&priv->mask, &bitwise->mask, sizeof(priv->mask)) && | |
305 | !memcmp(&priv->xor, &bitwise->xor, sizeof(priv->xor)) && | |
306 | !memcmp(&priv->data, &bitwise->data, sizeof(priv->data))) { | |
307 | track->cur = expr; | |
308 | return true; | |
309 | } | |
310 | ||
34cc9e52 PNA |
311 | if (track->regs[priv->sreg].bitwise || |
312 | track->regs[priv->sreg].num_reg != 0) { | |
313 | nft_reg_track_cancel(track, priv->dreg, priv->len); | |
be5650f8 PNA |
314 | return false; |
315 | } | |
316 | ||
317 | if (priv->sreg != priv->dreg) { | |
34cc9e52 PNA |
318 | nft_reg_track_update(track, track->regs[priv->sreg].selector, |
319 | priv->dreg, priv->len); | |
be5650f8 | 320 | } |
34cc9e52 PNA |
321 | |
322 | dreg = priv->dreg; | |
323 | regcount = DIV_ROUND_UP(priv->len, NFT_REG32_SIZE); | |
324 | for (i = 0; i < regcount; i++, dreg++) | |
325 | track->regs[priv->dreg].bitwise = expr; | |
be5650f8 PNA |
326 | |
327 | return false; | |
328 | } | |
329 | ||
ef1f7df9 PM |
330 | static const struct nft_expr_ops nft_bitwise_ops = { |
331 | .type = &nft_bitwise_type, | |
96518518 | 332 | .size = NFT_EXPR_SIZE(sizeof(struct nft_bitwise)), |
96518518 PM |
333 | .eval = nft_bitwise_eval, |
334 | .init = nft_bitwise_init, | |
335 | .dump = nft_bitwise_dump, | |
be5650f8 | 336 | .reduce = nft_bitwise_reduce, |
bd8699e9 | 337 | .offload = nft_bitwise_offload, |
ef1f7df9 PM |
338 | }; |
339 | ||
10fdd6d8 PS |
340 | static int |
341 | nft_bitwise_extract_u32_data(const struct nlattr * const tb, u32 *out) | |
342 | { | |
10fdd6d8 | 343 | struct nft_data data; |
341b6941 PNA |
344 | struct nft_data_desc desc = { |
345 | .type = NFT_DATA_VALUE, | |
346 | .size = sizeof(data), | |
347 | .len = sizeof(u32), | |
348 | }; | |
349 | int err; | |
10fdd6d8 | 350 | |
341b6941 | 351 | err = nft_data_init(NULL, &data, &desc, tb); |
10fdd6d8 PS |
352 | if (err < 0) |
353 | return err; | |
354 | ||
10fdd6d8 | 355 | *out = data.data[0]; |
341b6941 PNA |
356 | |
357 | return 0; | |
10fdd6d8 PS |
358 | } |
359 | ||
360 | static int nft_bitwise_fast_init(const struct nft_ctx *ctx, | |
361 | const struct nft_expr *expr, | |
362 | const struct nlattr * const tb[]) | |
363 | { | |
364 | struct nft_bitwise_fast_expr *priv = nft_expr_priv(expr); | |
365 | int err; | |
366 | ||
4f16d25c PNA |
367 | err = nft_parse_register_load(tb[NFTA_BITWISE_SREG], &priv->sreg, |
368 | sizeof(u32)); | |
10fdd6d8 PS |
369 | if (err < 0) |
370 | return err; | |
371 | ||
345023b0 PNA |
372 | err = nft_parse_register_store(ctx, tb[NFTA_BITWISE_DREG], &priv->dreg, |
373 | NULL, NFT_DATA_VALUE, sizeof(u32)); | |
10fdd6d8 PS |
374 | if (err < 0) |
375 | return err; | |
376 | ||
377 | if (tb[NFTA_BITWISE_DATA]) | |
378 | return -EINVAL; | |
379 | ||
380 | if (!tb[NFTA_BITWISE_MASK] || | |
381 | !tb[NFTA_BITWISE_XOR]) | |
382 | return -EINVAL; | |
383 | ||
384 | err = nft_bitwise_extract_u32_data(tb[NFTA_BITWISE_MASK], &priv->mask); | |
385 | if (err < 0) | |
386 | return err; | |
387 | ||
388 | err = nft_bitwise_extract_u32_data(tb[NFTA_BITWISE_XOR], &priv->xor); | |
389 | if (err < 0) | |
390 | return err; | |
391 | ||
392 | return 0; | |
393 | } | |
394 | ||
395 | static int | |
396 | nft_bitwise_fast_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
397 | { | |
398 | const struct nft_bitwise_fast_expr *priv = nft_expr_priv(expr); | |
399 | struct nft_data data; | |
400 | ||
401 | if (nft_dump_register(skb, NFTA_BITWISE_SREG, priv->sreg)) | |
402 | return -1; | |
403 | if (nft_dump_register(skb, NFTA_BITWISE_DREG, priv->dreg)) | |
404 | return -1; | |
405 | if (nla_put_be32(skb, NFTA_BITWISE_LEN, htonl(sizeof(u32)))) | |
406 | return -1; | |
407 | if (nla_put_be32(skb, NFTA_BITWISE_OP, htonl(NFT_BITWISE_BOOL))) | |
408 | return -1; | |
409 | ||
410 | data.data[0] = priv->mask; | |
411 | if (nft_data_dump(skb, NFTA_BITWISE_MASK, &data, | |
412 | NFT_DATA_VALUE, sizeof(u32)) < 0) | |
413 | return -1; | |
414 | ||
415 | data.data[0] = priv->xor; | |
416 | if (nft_data_dump(skb, NFTA_BITWISE_XOR, &data, | |
417 | NFT_DATA_VALUE, sizeof(u32)) < 0) | |
418 | return -1; | |
419 | ||
420 | return 0; | |
421 | } | |
422 | ||
423 | static int nft_bitwise_fast_offload(struct nft_offload_ctx *ctx, | |
424 | struct nft_flow_rule *flow, | |
425 | const struct nft_expr *expr) | |
426 | { | |
427 | const struct nft_bitwise_fast_expr *priv = nft_expr_priv(expr); | |
428 | struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; | |
429 | ||
430 | if (priv->xor || priv->sreg != priv->dreg || reg->len != sizeof(u32)) | |
431 | return -EOPNOTSUPP; | |
432 | ||
433 | reg->mask.data[0] = priv->mask; | |
434 | return 0; | |
435 | } | |
436 | ||
be5650f8 PNA |
437 | static bool nft_bitwise_fast_reduce(struct nft_regs_track *track, |
438 | const struct nft_expr *expr) | |
439 | { | |
440 | const struct nft_bitwise_fast_expr *priv = nft_expr_priv(expr); | |
441 | const struct nft_bitwise_fast_expr *bitwise; | |
442 | ||
443 | if (!track->regs[priv->sreg].selector) | |
444 | return false; | |
445 | ||
31818213 | 446 | bitwise = nft_expr_priv(track->regs[priv->dreg].selector); |
be5650f8 PNA |
447 | if (track->regs[priv->sreg].selector == track->regs[priv->dreg].selector && |
448 | track->regs[priv->dreg].bitwise && | |
449 | track->regs[priv->dreg].bitwise->ops == expr->ops && | |
450 | priv->sreg == bitwise->sreg && | |
451 | priv->dreg == bitwise->dreg && | |
452 | priv->mask == bitwise->mask && | |
453 | priv->xor == bitwise->xor) { | |
454 | track->cur = expr; | |
455 | return true; | |
456 | } | |
457 | ||
458 | if (track->regs[priv->sreg].bitwise) { | |
34cc9e52 | 459 | nft_reg_track_cancel(track, priv->dreg, NFT_REG32_SIZE); |
be5650f8 PNA |
460 | return false; |
461 | } | |
462 | ||
463 | if (priv->sreg != priv->dreg) { | |
464 | track->regs[priv->dreg].selector = | |
465 | track->regs[priv->sreg].selector; | |
466 | } | |
467 | track->regs[priv->dreg].bitwise = expr; | |
468 | ||
469 | return false; | |
470 | } | |
471 | ||
10fdd6d8 PS |
472 | const struct nft_expr_ops nft_bitwise_fast_ops = { |
473 | .type = &nft_bitwise_type, | |
474 | .size = NFT_EXPR_SIZE(sizeof(struct nft_bitwise_fast_expr)), | |
475 | .eval = NULL, /* inlined */ | |
476 | .init = nft_bitwise_fast_init, | |
477 | .dump = nft_bitwise_fast_dump, | |
be5650f8 | 478 | .reduce = nft_bitwise_fast_reduce, |
10fdd6d8 PS |
479 | .offload = nft_bitwise_fast_offload, |
480 | }; | |
481 | ||
482 | static const struct nft_expr_ops * | |
483 | nft_bitwise_select_ops(const struct nft_ctx *ctx, | |
484 | const struct nlattr * const tb[]) | |
485 | { | |
486 | int err; | |
487 | u32 len; | |
488 | ||
489 | if (!tb[NFTA_BITWISE_LEN] || | |
490 | !tb[NFTA_BITWISE_SREG] || | |
491 | !tb[NFTA_BITWISE_DREG]) | |
492 | return ERR_PTR(-EINVAL); | |
493 | ||
494 | err = nft_parse_u32_check(tb[NFTA_BITWISE_LEN], U8_MAX, &len); | |
495 | if (err < 0) | |
496 | return ERR_PTR(err); | |
497 | ||
498 | if (len != sizeof(u32)) | |
499 | return &nft_bitwise_ops; | |
500 | ||
501 | if (tb[NFTA_BITWISE_OP] && | |
502 | ntohl(nla_get_be32(tb[NFTA_BITWISE_OP])) != NFT_BITWISE_BOOL) | |
503 | return &nft_bitwise_ops; | |
504 | ||
505 | return &nft_bitwise_fast_ops; | |
506 | } | |
507 | ||
4e24877e | 508 | struct nft_expr_type nft_bitwise_type __read_mostly = { |
ef1f7df9 | 509 | .name = "bitwise", |
10fdd6d8 | 510 | .select_ops = nft_bitwise_select_ops, |
96518518 PM |
511 | .policy = nft_bitwise_policy, |
512 | .maxattr = NFTA_BITWISE_MAX, | |
ef1f7df9 | 513 | .owner = THIS_MODULE, |
96518518 | 514 | }; |
be5650f8 PNA |
515 | |
516 | bool nft_expr_reduce_bitwise(struct nft_regs_track *track, | |
517 | const struct nft_expr *expr) | |
518 | { | |
519 | const struct nft_expr *last = track->last; | |
520 | const struct nft_expr *next; | |
521 | ||
522 | if (expr == last) | |
523 | return false; | |
524 | ||
525 | next = nft_expr_next(expr); | |
526 | if (next->ops == &nft_bitwise_ops) | |
527 | return nft_bitwise_reduce(track, next); | |
528 | else if (next->ops == &nft_bitwise_fast_ops) | |
529 | return nft_bitwise_fast_reduce(track, next); | |
530 | ||
531 | return false; | |
532 | } | |
03858af0 | 533 | EXPORT_SYMBOL_GPL(nft_expr_reduce_bitwise); |