xref: /linux-6.15/net/netfilter/nft_cmp.c (revision 6b772053)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2008-2009 Patrick McHardy <[email protected]>
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 
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(&regs->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 
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 	int err;
78 
79 	err = nft_data_init(NULL, &priv->data, sizeof(priv->data), &desc,
80 			    tb[NFTA_CMP_DATA]);
81 	if (err < 0)
82 		return err;
83 
84 	if (desc.type != NFT_DATA_VALUE) {
85 		err = -EINVAL;
86 		nft_data_release(&priv->data, desc.type);
87 		return err;
88 	}
89 
90 	err = nft_parse_register_load(tb[NFTA_CMP_SREG], &priv->sreg, desc.len);
91 	if (err < 0)
92 		return err;
93 
94 	priv->op  = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
95 	priv->len = desc.len;
96 	return 0;
97 }
98 
99 static int nft_cmp_dump(struct sk_buff *skb, const struct nft_expr *expr)
100 {
101 	const struct nft_cmp_expr *priv = nft_expr_priv(expr);
102 
103 	if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
104 		goto nla_put_failure;
105 	if (nla_put_be32(skb, NFTA_CMP_OP, htonl(priv->op)))
106 		goto nla_put_failure;
107 
108 	if (nft_data_dump(skb, NFTA_CMP_DATA, &priv->data,
109 			  NFT_DATA_VALUE, priv->len) < 0)
110 		goto nla_put_failure;
111 	return 0;
112 
113 nla_put_failure:
114 	return -1;
115 }
116 
117 union nft_cmp_offload_data {
118 	u16	val16;
119 	u32	val32;
120 	u64	val64;
121 };
122 
123 static void nft_payload_n2h(union nft_cmp_offload_data *data,
124 			    const u8 *val, u32 len)
125 {
126 	switch (len) {
127 	case 2:
128 		data->val16 = ntohs(*((__be16 *)val));
129 		break;
130 	case 4:
131 		data->val32 = ntohl(*((__be32 *)val));
132 		break;
133 	case 8:
134 		data->val64 = be64_to_cpu(*((__be64 *)val));
135 		break;
136 	default:
137 		WARN_ON_ONCE(1);
138 		break;
139 	}
140 }
141 
142 static int __nft_cmp_offload(struct nft_offload_ctx *ctx,
143 			     struct nft_flow_rule *flow,
144 			     const struct nft_cmp_expr *priv)
145 {
146 	struct nft_offload_reg *reg = &ctx->regs[priv->sreg];
147 	union nft_cmp_offload_data _data, _datamask;
148 	u8 *mask = (u8 *)&flow->match.mask;
149 	u8 *key = (u8 *)&flow->match.key;
150 	u8 *data, *datamask;
151 
152 	if (priv->op != NFT_CMP_EQ || priv->len > reg->len)
153 		return -EOPNOTSUPP;
154 
155 	if (reg->flags & NFT_OFFLOAD_F_NETWORK2HOST) {
156 		nft_payload_n2h(&_data, (u8 *)&priv->data, reg->len);
157 		nft_payload_n2h(&_datamask, (u8 *)&reg->mask, reg->len);
158 		data = (u8 *)&_data;
159 		datamask = (u8 *)&_datamask;
160 	} else {
161 		data = (u8 *)&priv->data;
162 		datamask = (u8 *)&reg->mask;
163 	}
164 
165 	memcpy(key + reg->offset, data, reg->len);
166 	memcpy(mask + reg->offset, datamask, reg->len);
167 
168 	flow->match.dissector.used_keys |= BIT(reg->key);
169 	flow->match.dissector.offset[reg->key] = reg->base_offset;
170 
171 	if (reg->key == FLOW_DISSECTOR_KEY_META &&
172 	    reg->offset == offsetof(struct nft_flow_key, meta.ingress_iftype) &&
173 	    nft_reg_load16(priv->data.data) != ARPHRD_ETHER)
174 		return -EOPNOTSUPP;
175 
176 	nft_offload_update_dependency(ctx, &priv->data, reg->len);
177 
178 	return 0;
179 }
180 
181 static int nft_cmp_offload(struct nft_offload_ctx *ctx,
182 			   struct nft_flow_rule *flow,
183 			   const struct nft_expr *expr)
184 {
185 	const struct nft_cmp_expr *priv = nft_expr_priv(expr);
186 
187 	return __nft_cmp_offload(ctx, flow, priv);
188 }
189 
190 static const struct nft_expr_ops nft_cmp_ops = {
191 	.type		= &nft_cmp_type,
192 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_cmp_expr)),
193 	.eval		= nft_cmp_eval,
194 	.init		= nft_cmp_init,
195 	.dump		= nft_cmp_dump,
196 	.reduce		= NFT_REDUCE_READONLY,
197 	.offload	= nft_cmp_offload,
198 };
199 
200 /* Calculate the mask for the nft_cmp_fast expression. On big endian the
201  * mask needs to include the *upper* bytes when interpreting that data as
202  * something smaller than the full u32, therefore a cpu_to_le32 is done.
203  */
204 static u32 nft_cmp_fast_mask(unsigned int len)
205 {
206 	__le32 mask = cpu_to_le32(~0U >> (sizeof_field(struct nft_cmp_fast_expr,
207 					  data) * BITS_PER_BYTE - len));
208 
209 	return (__force u32)mask;
210 }
211 
212 static int nft_cmp_fast_init(const struct nft_ctx *ctx,
213 			     const struct nft_expr *expr,
214 			     const struct nlattr * const tb[])
215 {
216 	struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
217 	struct nft_data_desc desc;
218 	struct nft_data data;
219 	int err;
220 
221 	err = nft_data_init(NULL, &data, sizeof(data), &desc,
222 			    tb[NFTA_CMP_DATA]);
223 	if (err < 0)
224 		return err;
225 
226 	err = nft_parse_register_load(tb[NFTA_CMP_SREG], &priv->sreg, desc.len);
227 	if (err < 0)
228 		return err;
229 
230 	desc.len *= BITS_PER_BYTE;
231 
232 	priv->mask = nft_cmp_fast_mask(desc.len);
233 	priv->data = data.data[0] & priv->mask;
234 	priv->len  = desc.len;
235 	priv->inv  = ntohl(nla_get_be32(tb[NFTA_CMP_OP])) != NFT_CMP_EQ;
236 	return 0;
237 }
238 
239 static int nft_cmp_fast_offload(struct nft_offload_ctx *ctx,
240 				struct nft_flow_rule *flow,
241 				const struct nft_expr *expr)
242 {
243 	const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
244 	struct nft_cmp_expr cmp = {
245 		.data	= {
246 			.data	= {
247 				[0] = priv->data,
248 			},
249 		},
250 		.sreg	= priv->sreg,
251 		.len	= priv->len / BITS_PER_BYTE,
252 		.op	= priv->inv ? NFT_CMP_NEQ : NFT_CMP_EQ,
253 	};
254 
255 	return __nft_cmp_offload(ctx, flow, &cmp);
256 }
257 
258 static int nft_cmp_fast_dump(struct sk_buff *skb, const struct nft_expr *expr)
259 {
260 	const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
261 	enum nft_cmp_ops op = priv->inv ? NFT_CMP_NEQ : NFT_CMP_EQ;
262 	struct nft_data data;
263 
264 	if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
265 		goto nla_put_failure;
266 	if (nla_put_be32(skb, NFTA_CMP_OP, htonl(op)))
267 		goto nla_put_failure;
268 
269 	data.data[0] = priv->data;
270 	if (nft_data_dump(skb, NFTA_CMP_DATA, &data,
271 			  NFT_DATA_VALUE, priv->len / BITS_PER_BYTE) < 0)
272 		goto nla_put_failure;
273 	return 0;
274 
275 nla_put_failure:
276 	return -1;
277 }
278 
279 const struct nft_expr_ops nft_cmp_fast_ops = {
280 	.type		= &nft_cmp_type,
281 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_cmp_fast_expr)),
282 	.eval		= NULL,	/* inlined */
283 	.init		= nft_cmp_fast_init,
284 	.dump		= nft_cmp_fast_dump,
285 	.reduce		= NFT_REDUCE_READONLY,
286 	.offload	= nft_cmp_fast_offload,
287 };
288 
289 static u32 nft_cmp_mask(u32 bitlen)
290 {
291 	return (__force u32)cpu_to_le32(~0U >> (sizeof(u32) * BITS_PER_BYTE - bitlen));
292 }
293 
294 static void nft_cmp16_fast_mask(struct nft_data *data, unsigned int bitlen)
295 {
296 	int len = bitlen / BITS_PER_BYTE;
297 	int i, words = len / sizeof(u32);
298 
299 	for (i = 0; i < words; i++) {
300 		data->data[i] = 0xffffffff;
301 		bitlen -= sizeof(u32) * BITS_PER_BYTE;
302 	}
303 
304 	if (len % sizeof(u32))
305 		data->data[i++] = nft_cmp_mask(bitlen);
306 
307 	for (; i < 4; i++)
308 		data->data[i] = 0;
309 }
310 
311 static int nft_cmp16_fast_init(const struct nft_ctx *ctx,
312 			       const struct nft_expr *expr,
313 			       const struct nlattr * const tb[])
314 {
315 	struct nft_cmp16_fast_expr *priv = nft_expr_priv(expr);
316 	struct nft_data_desc desc;
317 	int err;
318 
319 	err = nft_data_init(NULL, &priv->data, sizeof(priv->data), &desc,
320 			    tb[NFTA_CMP_DATA]);
321 	if (err < 0)
322 		return err;
323 
324 	err = nft_parse_register_load(tb[NFTA_CMP_SREG], &priv->sreg, desc.len);
325 	if (err < 0)
326 		return err;
327 
328 	nft_cmp16_fast_mask(&priv->mask, desc.len * BITS_PER_BYTE);
329 	priv->inv = ntohl(nla_get_be32(tb[NFTA_CMP_OP])) != NFT_CMP_EQ;
330 	priv->len = desc.len;
331 
332 	return 0;
333 }
334 
335 static int nft_cmp16_fast_offload(struct nft_offload_ctx *ctx,
336 				  struct nft_flow_rule *flow,
337 				  const struct nft_expr *expr)
338 {
339 	const struct nft_cmp16_fast_expr *priv = nft_expr_priv(expr);
340 	struct nft_cmp_expr cmp = {
341 		.data	= priv->data,
342 		.sreg	= priv->sreg,
343 		.len	= priv->len,
344 		.op	= priv->inv ? NFT_CMP_NEQ : NFT_CMP_EQ,
345 	};
346 
347 	return __nft_cmp_offload(ctx, flow, &cmp);
348 }
349 
350 static int nft_cmp16_fast_dump(struct sk_buff *skb, const struct nft_expr *expr)
351 {
352 	const struct nft_cmp16_fast_expr *priv = nft_expr_priv(expr);
353 	enum nft_cmp_ops op = priv->inv ? NFT_CMP_NEQ : NFT_CMP_EQ;
354 
355 	if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
356 		goto nla_put_failure;
357 	if (nla_put_be32(skb, NFTA_CMP_OP, htonl(op)))
358 		goto nla_put_failure;
359 
360 	if (nft_data_dump(skb, NFTA_CMP_DATA, &priv->data,
361 			  NFT_DATA_VALUE, priv->len) < 0)
362 		goto nla_put_failure;
363 	return 0;
364 
365 nla_put_failure:
366 	return -1;
367 }
368 
369 
370 const struct nft_expr_ops nft_cmp16_fast_ops = {
371 	.type		= &nft_cmp_type,
372 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_cmp16_fast_expr)),
373 	.eval		= NULL,	/* inlined */
374 	.init		= nft_cmp16_fast_init,
375 	.dump		= nft_cmp16_fast_dump,
376 	.reduce		= NFT_REDUCE_READONLY,
377 	.offload	= nft_cmp16_fast_offload,
378 };
379 
380 static const struct nft_expr_ops *
381 nft_cmp_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
382 {
383 	struct nft_data_desc desc;
384 	struct nft_data data;
385 	enum nft_cmp_ops op;
386 	u8 sreg;
387 	int err;
388 
389 	if (tb[NFTA_CMP_SREG] == NULL ||
390 	    tb[NFTA_CMP_OP] == NULL ||
391 	    tb[NFTA_CMP_DATA] == NULL)
392 		return ERR_PTR(-EINVAL);
393 
394 	op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
395 	switch (op) {
396 	case NFT_CMP_EQ:
397 	case NFT_CMP_NEQ:
398 	case NFT_CMP_LT:
399 	case NFT_CMP_LTE:
400 	case NFT_CMP_GT:
401 	case NFT_CMP_GTE:
402 		break;
403 	default:
404 		return ERR_PTR(-EINVAL);
405 	}
406 
407 	err = nft_data_init(NULL, &data, sizeof(data), &desc,
408 			    tb[NFTA_CMP_DATA]);
409 	if (err < 0)
410 		return ERR_PTR(err);
411 
412 	if (desc.type != NFT_DATA_VALUE)
413 		goto err1;
414 
415 	sreg = ntohl(nla_get_be32(tb[NFTA_CMP_SREG]));
416 
417 	if (op == NFT_CMP_EQ || op == NFT_CMP_NEQ) {
418 		if (desc.len <= sizeof(u32))
419 			return &nft_cmp_fast_ops;
420 		else if (desc.len <= sizeof(data) &&
421 			 ((sreg >= NFT_REG_1 && sreg <= NFT_REG_4) ||
422 			  (sreg >= NFT_REG32_00 && sreg <= NFT_REG32_12 && sreg % 2 == 0)))
423 			return &nft_cmp16_fast_ops;
424 	}
425 	return &nft_cmp_ops;
426 err1:
427 	nft_data_release(&data, desc.type);
428 	return ERR_PTR(-EINVAL);
429 }
430 
431 struct nft_expr_type nft_cmp_type __read_mostly = {
432 	.name		= "cmp",
433 	.select_ops	= nft_cmp_select_ops,
434 	.policy		= nft_cmp_policy,
435 	.maxattr	= NFTA_CMP_MAX,
436 	.owner		= THIS_MODULE,
437 };
438