1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
4 *
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/if_arp.h>
14 #include <linux/netfilter/nf_tables.h>
15 #include <net/netfilter/nf_tables_core.h>
16 #include <net/netfilter/nf_tables_offload.h>
17 #include <net/netfilter/nf_tables.h>
18
19 struct nft_cmp_expr {
20 struct nft_data data;
21 u8 sreg;
22 u8 len;
23 enum nft_cmp_ops op:8;
24 };
25
nft_cmp_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)26 void nft_cmp_eval(const struct nft_expr *expr,
27 struct nft_regs *regs,
28 const struct nft_pktinfo *pkt)
29 {
30 const struct nft_cmp_expr *priv = nft_expr_priv(expr);
31 int d;
32
33 d = memcmp(®s->data[priv->sreg], &priv->data, priv->len);
34 switch (priv->op) {
35 case NFT_CMP_EQ:
36 if (d != 0)
37 goto mismatch;
38 break;
39 case NFT_CMP_NEQ:
40 if (d == 0)
41 goto mismatch;
42 break;
43 case NFT_CMP_LT:
44 if (d == 0)
45 goto mismatch;
46 fallthrough;
47 case NFT_CMP_LTE:
48 if (d > 0)
49 goto mismatch;
50 break;
51 case NFT_CMP_GT:
52 if (d == 0)
53 goto mismatch;
54 fallthrough;
55 case NFT_CMP_GTE:
56 if (d < 0)
57 goto mismatch;
58 break;
59 }
60 return;
61
62 mismatch:
63 regs->verdict.code = NFT_BREAK;
64 }
65
66 static const struct nla_policy nft_cmp_policy[NFTA_CMP_MAX + 1] = {
67 [NFTA_CMP_SREG] = { .type = NLA_U32 },
68 [NFTA_CMP_OP] = { .type = NLA_U32 },
69 [NFTA_CMP_DATA] = { .type = NLA_NESTED },
70 };
71
nft_cmp_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])72 static int nft_cmp_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
73 const struct nlattr * const tb[])
74 {
75 struct nft_cmp_expr *priv = nft_expr_priv(expr);
76 struct nft_data_desc desc = {
77 .type = NFT_DATA_VALUE,
78 .size = sizeof(priv->data),
79 };
80 int err;
81
82 err = nft_data_init(NULL, &priv->data, &desc, tb[NFTA_CMP_DATA]);
83 if (err < 0)
84 return err;
85
86 err = nft_parse_register_load(tb[NFTA_CMP_SREG], &priv->sreg, desc.len);
87 if (err < 0)
88 return err;
89
90 priv->op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
91 priv->len = desc.len;
92 return 0;
93 }
94
nft_cmp_dump(struct sk_buff * skb,const struct nft_expr * expr)95 static int nft_cmp_dump(struct sk_buff *skb, const struct nft_expr *expr)
96 {
97 const struct nft_cmp_expr *priv = nft_expr_priv(expr);
98
99 if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
100 goto nla_put_failure;
101 if (nla_put_be32(skb, NFTA_CMP_OP, htonl(priv->op)))
102 goto nla_put_failure;
103
104 if (nft_data_dump(skb, NFTA_CMP_DATA, &priv->data,
105 NFT_DATA_VALUE, priv->len) < 0)
106 goto nla_put_failure;
107 return 0;
108
109 nla_put_failure:
110 return -1;
111 }
112
113 union nft_cmp_offload_data {
114 u16 val16;
115 u32 val32;
116 u64 val64;
117 };
118
nft_payload_n2h(union nft_cmp_offload_data * data,const u8 * val,u32 len)119 static void nft_payload_n2h(union nft_cmp_offload_data *data,
120 const u8 *val, u32 len)
121 {
122 switch (len) {
123 case 2:
124 data->val16 = ntohs(*((u16 *)val));
125 break;
126 case 4:
127 data->val32 = ntohl(*((u32 *)val));
128 break;
129 case 8:
130 data->val64 = be64_to_cpu(*((u64 *)val));
131 break;
132 default:
133 WARN_ON_ONCE(1);
134 break;
135 }
136 }
137
__nft_cmp_offload(struct nft_offload_ctx * ctx,struct nft_flow_rule * flow,const struct nft_cmp_expr * priv)138 static int __nft_cmp_offload(struct nft_offload_ctx *ctx,
139 struct nft_flow_rule *flow,
140 const struct nft_cmp_expr *priv)
141 {
142 struct nft_offload_reg *reg = &ctx->regs[priv->sreg];
143 union nft_cmp_offload_data _data, _datamask;
144 u8 *mask = (u8 *)&flow->match.mask;
145 u8 *key = (u8 *)&flow->match.key;
146 u8 *data, *datamask;
147
148 if (priv->op != NFT_CMP_EQ || priv->len > reg->len)
149 return -EOPNOTSUPP;
150
151 if (reg->flags & NFT_OFFLOAD_F_NETWORK2HOST) {
152 nft_payload_n2h(&_data, (u8 *)&priv->data, reg->len);
153 nft_payload_n2h(&_datamask, (u8 *)®->mask, reg->len);
154 data = (u8 *)&_data;
155 datamask = (u8 *)&_datamask;
156 } else {
157 data = (u8 *)&priv->data;
158 datamask = (u8 *)®->mask;
159 }
160
161 memcpy(key + reg->offset, data, reg->len);
162 memcpy(mask + reg->offset, datamask, reg->len);
163
164 flow->match.dissector.used_keys |= BIT(reg->key);
165 flow->match.dissector.offset[reg->key] = reg->base_offset;
166
167 if (reg->key == FLOW_DISSECTOR_KEY_META &&
168 reg->offset == offsetof(struct nft_flow_key, meta.ingress_iftype) &&
169 nft_reg_load16(priv->data.data) != ARPHRD_ETHER)
170 return -EOPNOTSUPP;
171
172 nft_offload_update_dependency(ctx, &priv->data, reg->len);
173
174 return 0;
175 }
176
nft_cmp_offload(struct nft_offload_ctx * ctx,struct nft_flow_rule * flow,const struct nft_expr * expr)177 static int nft_cmp_offload(struct nft_offload_ctx *ctx,
178 struct nft_flow_rule *flow,
179 const struct nft_expr *expr)
180 {
181 const struct nft_cmp_expr *priv = nft_expr_priv(expr);
182
183 return __nft_cmp_offload(ctx, flow, priv);
184 }
185
186 static const struct nft_expr_ops nft_cmp_ops = {
187 .type = &nft_cmp_type,
188 .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp_expr)),
189 .eval = nft_cmp_eval,
190 .init = nft_cmp_init,
191 .dump = nft_cmp_dump,
192 .offload = nft_cmp_offload,
193 };
194
nft_cmp_fast_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])195 static int nft_cmp_fast_init(const struct nft_ctx *ctx,
196 const struct nft_expr *expr,
197 const struct nlattr * const tb[])
198 {
199 struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
200 struct nft_data data;
201 struct nft_data_desc desc = {
202 .type = NFT_DATA_VALUE,
203 .size = sizeof(data),
204 };
205 int err;
206
207 err = nft_data_init(NULL, &data, &desc, tb[NFTA_CMP_DATA]);
208 if (err < 0)
209 return err;
210
211 err = nft_parse_register_load(tb[NFTA_CMP_SREG], &priv->sreg, desc.len);
212 if (err < 0)
213 return err;
214
215 desc.len *= BITS_PER_BYTE;
216
217 priv->mask = nft_cmp_fast_mask(desc.len);
218 priv->data = data.data[0] & priv->mask;
219 priv->len = desc.len;
220 priv->inv = ntohl(nla_get_be32(tb[NFTA_CMP_OP])) != NFT_CMP_EQ;
221 return 0;
222 }
223
nft_cmp_fast_offload(struct nft_offload_ctx * ctx,struct nft_flow_rule * flow,const struct nft_expr * expr)224 static int nft_cmp_fast_offload(struct nft_offload_ctx *ctx,
225 struct nft_flow_rule *flow,
226 const struct nft_expr *expr)
227 {
228 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
229 struct nft_cmp_expr cmp = {
230 .data = {
231 .data = {
232 [0] = priv->data,
233 },
234 },
235 .sreg = priv->sreg,
236 .len = priv->len / BITS_PER_BYTE,
237 .op = priv->inv ? NFT_CMP_NEQ : NFT_CMP_EQ,
238 };
239
240 return __nft_cmp_offload(ctx, flow, &cmp);
241 }
242
nft_cmp_fast_dump(struct sk_buff * skb,const struct nft_expr * expr)243 static int nft_cmp_fast_dump(struct sk_buff *skb, const struct nft_expr *expr)
244 {
245 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
246 enum nft_cmp_ops op = priv->inv ? NFT_CMP_NEQ : NFT_CMP_EQ;
247 struct nft_data data;
248
249 if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
250 goto nla_put_failure;
251 if (nla_put_be32(skb, NFTA_CMP_OP, htonl(op)))
252 goto nla_put_failure;
253
254 data.data[0] = priv->data;
255 if (nft_data_dump(skb, NFTA_CMP_DATA, &data,
256 NFT_DATA_VALUE, priv->len / BITS_PER_BYTE) < 0)
257 goto nla_put_failure;
258 return 0;
259
260 nla_put_failure:
261 return -1;
262 }
263
264 const struct nft_expr_ops nft_cmp_fast_ops = {
265 .type = &nft_cmp_type,
266 .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp_fast_expr)),
267 .eval = NULL, /* inlined */
268 .init = nft_cmp_fast_init,
269 .dump = nft_cmp_fast_dump,
270 .offload = nft_cmp_fast_offload,
271 };
272
nft_cmp_mask(u32 bitlen)273 static u32 nft_cmp_mask(u32 bitlen)
274 {
275 return (__force u32)cpu_to_le32(~0U >> (sizeof(u32) * BITS_PER_BYTE - bitlen));
276 }
277
nft_cmp16_fast_mask(struct nft_data * data,unsigned int bitlen)278 static void nft_cmp16_fast_mask(struct nft_data *data, unsigned int bitlen)
279 {
280 int len = bitlen / BITS_PER_BYTE;
281 int i, words = len / sizeof(u32);
282
283 for (i = 0; i < words; i++) {
284 data->data[i] = 0xffffffff;
285 bitlen -= sizeof(u32) * BITS_PER_BYTE;
286 }
287
288 if (len % sizeof(u32))
289 data->data[i++] = nft_cmp_mask(bitlen);
290
291 for (; i < 4; i++)
292 data->data[i] = 0;
293 }
294
nft_cmp16_fast_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])295 static int nft_cmp16_fast_init(const struct nft_ctx *ctx,
296 const struct nft_expr *expr,
297 const struct nlattr * const tb[])
298 {
299 struct nft_cmp16_fast_expr *priv = nft_expr_priv(expr);
300 struct nft_data_desc desc = {
301 .type = NFT_DATA_VALUE,
302 .size = sizeof(priv->data),
303 };
304 int err;
305
306 err = nft_data_init(NULL, &priv->data, &desc, tb[NFTA_CMP_DATA]);
307 if (err < 0)
308 return err;
309
310 err = nft_parse_register_load(tb[NFTA_CMP_SREG], &priv->sreg, desc.len);
311 if (err < 0)
312 return err;
313
314 nft_cmp16_fast_mask(&priv->mask, desc.len * BITS_PER_BYTE);
315 priv->inv = ntohl(nla_get_be32(tb[NFTA_CMP_OP])) != NFT_CMP_EQ;
316 priv->len = desc.len;
317
318 return 0;
319 }
320
nft_cmp16_fast_offload(struct nft_offload_ctx * ctx,struct nft_flow_rule * flow,const struct nft_expr * expr)321 static int nft_cmp16_fast_offload(struct nft_offload_ctx *ctx,
322 struct nft_flow_rule *flow,
323 const struct nft_expr *expr)
324 {
325 const struct nft_cmp16_fast_expr *priv = nft_expr_priv(expr);
326 struct nft_cmp_expr cmp = {
327 .data = priv->data,
328 .sreg = priv->sreg,
329 .len = priv->len,
330 .op = priv->inv ? NFT_CMP_NEQ : NFT_CMP_EQ,
331 };
332
333 return __nft_cmp_offload(ctx, flow, &cmp);
334 }
335
nft_cmp16_fast_dump(struct sk_buff * skb,const struct nft_expr * expr)336 static int nft_cmp16_fast_dump(struct sk_buff *skb, const struct nft_expr *expr)
337 {
338 const struct nft_cmp16_fast_expr *priv = nft_expr_priv(expr);
339 enum nft_cmp_ops op = priv->inv ? NFT_CMP_NEQ : NFT_CMP_EQ;
340
341 if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
342 goto nla_put_failure;
343 if (nla_put_be32(skb, NFTA_CMP_OP, htonl(op)))
344 goto nla_put_failure;
345
346 if (nft_data_dump(skb, NFTA_CMP_DATA, &priv->data,
347 NFT_DATA_VALUE, priv->len) < 0)
348 goto nla_put_failure;
349 return 0;
350
351 nla_put_failure:
352 return -1;
353 }
354
355
356 const struct nft_expr_ops nft_cmp16_fast_ops = {
357 .type = &nft_cmp_type,
358 .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp16_fast_expr)),
359 .eval = NULL, /* inlined */
360 .init = nft_cmp16_fast_init,
361 .dump = nft_cmp16_fast_dump,
362 .offload = nft_cmp16_fast_offload,
363 };
364
365 static const struct nft_expr_ops *
nft_cmp_select_ops(const struct nft_ctx * ctx,const struct nlattr * const tb[])366 nft_cmp_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
367 {
368 struct nft_data data;
369 struct nft_data_desc desc = {
370 .type = NFT_DATA_VALUE,
371 .size = sizeof(data),
372 };
373 enum nft_cmp_ops op;
374 u8 sreg;
375 int err;
376
377 if (tb[NFTA_CMP_SREG] == NULL ||
378 tb[NFTA_CMP_OP] == NULL ||
379 tb[NFTA_CMP_DATA] == NULL)
380 return ERR_PTR(-EINVAL);
381
382 op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
383 switch (op) {
384 case NFT_CMP_EQ:
385 case NFT_CMP_NEQ:
386 case NFT_CMP_LT:
387 case NFT_CMP_LTE:
388 case NFT_CMP_GT:
389 case NFT_CMP_GTE:
390 break;
391 default:
392 return ERR_PTR(-EINVAL);
393 }
394
395 err = nft_data_init(NULL, &data, &desc, tb[NFTA_CMP_DATA]);
396 if (err < 0)
397 return ERR_PTR(err);
398
399 sreg = ntohl(nla_get_be32(tb[NFTA_CMP_SREG]));
400
401 if (op == NFT_CMP_EQ || op == NFT_CMP_NEQ) {
402 if (desc.len <= sizeof(u32))
403 return &nft_cmp_fast_ops;
404 else if (desc.len <= sizeof(data) &&
405 ((sreg >= NFT_REG_1 && sreg <= NFT_REG_4) ||
406 (sreg >= NFT_REG32_00 && sreg <= NFT_REG32_12 && sreg % 2 == 0)))
407 return &nft_cmp16_fast_ops;
408 }
409 return &nft_cmp_ops;
410 }
411
412 struct nft_expr_type nft_cmp_type __read_mostly = {
413 .name = "cmp",
414 .select_ops = nft_cmp_select_ops,
415 .policy = nft_cmp_policy,
416 .maxattr = NFTA_CMP_MAX,
417 .owner = THIS_MODULE,
418 };
419