1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
4 */
5
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/netlink.h>
10 #include <linux/netfilter.h>
11 #include <linux/netfilter/nf_tables.h>
12 #include <net/netfilter/nf_tables_core.h>
13 #include <net/netfilter/nf_tables.h>
14
15 struct nft_range_expr {
16 struct nft_data data_from;
17 struct nft_data data_to;
18 u8 sreg;
19 u8 len;
20 enum nft_range_ops op:8;
21 };
22
nft_range_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)23 void nft_range_eval(const struct nft_expr *expr,
24 struct nft_regs *regs, const struct nft_pktinfo *pkt)
25 {
26 const struct nft_range_expr *priv = nft_expr_priv(expr);
27 int d1, d2;
28
29 d1 = memcmp(®s->data[priv->sreg], &priv->data_from, priv->len);
30 d2 = memcmp(®s->data[priv->sreg], &priv->data_to, priv->len);
31 switch (priv->op) {
32 case NFT_RANGE_EQ:
33 if (d1 < 0 || d2 > 0)
34 regs->verdict.code = NFT_BREAK;
35 break;
36 case NFT_RANGE_NEQ:
37 if (d1 >= 0 && d2 <= 0)
38 regs->verdict.code = NFT_BREAK;
39 break;
40 }
41 }
42
43 static const struct nla_policy nft_range_policy[NFTA_RANGE_MAX + 1] = {
44 [NFTA_RANGE_SREG] = { .type = NLA_U32 },
45 [NFTA_RANGE_OP] = { .type = NLA_U32 },
46 [NFTA_RANGE_FROM_DATA] = { .type = NLA_NESTED },
47 [NFTA_RANGE_TO_DATA] = { .type = NLA_NESTED },
48 };
49
nft_range_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])50 static int nft_range_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
51 const struct nlattr * const tb[])
52 {
53 struct nft_range_expr *priv = nft_expr_priv(expr);
54 struct nft_data_desc desc_from = {
55 .type = NFT_DATA_VALUE,
56 .size = sizeof(priv->data_from),
57 };
58 struct nft_data_desc desc_to = {
59 .type = NFT_DATA_VALUE,
60 .size = sizeof(priv->data_to),
61 };
62 int err;
63 u32 op;
64
65 if (!tb[NFTA_RANGE_SREG] ||
66 !tb[NFTA_RANGE_OP] ||
67 !tb[NFTA_RANGE_FROM_DATA] ||
68 !tb[NFTA_RANGE_TO_DATA])
69 return -EINVAL;
70
71 err = nft_data_init(NULL, &priv->data_from, &desc_from,
72 tb[NFTA_RANGE_FROM_DATA]);
73 if (err < 0)
74 return err;
75
76 err = nft_data_init(NULL, &priv->data_to, &desc_to,
77 tb[NFTA_RANGE_TO_DATA]);
78 if (err < 0)
79 goto err1;
80
81 if (desc_from.len != desc_to.len) {
82 err = -EINVAL;
83 goto err2;
84 }
85
86 err = nft_parse_register_load(tb[NFTA_RANGE_SREG], &priv->sreg,
87 desc_from.len);
88 if (err < 0)
89 goto err2;
90
91 err = nft_parse_u32_check(tb[NFTA_RANGE_OP], U8_MAX, &op);
92 if (err < 0)
93 goto err2;
94
95 switch (op) {
96 case NFT_RANGE_EQ:
97 case NFT_RANGE_NEQ:
98 break;
99 default:
100 err = -EINVAL;
101 goto err2;
102 }
103
104 priv->op = op;
105 priv->len = desc_from.len;
106 return 0;
107 err2:
108 nft_data_release(&priv->data_to, desc_to.type);
109 err1:
110 nft_data_release(&priv->data_from, desc_from.type);
111 return err;
112 }
113
nft_range_dump(struct sk_buff * skb,const struct nft_expr * expr)114 static int nft_range_dump(struct sk_buff *skb, const struct nft_expr *expr)
115 {
116 const struct nft_range_expr *priv = nft_expr_priv(expr);
117
118 if (nft_dump_register(skb, NFTA_RANGE_SREG, priv->sreg))
119 goto nla_put_failure;
120 if (nla_put_be32(skb, NFTA_RANGE_OP, htonl(priv->op)))
121 goto nla_put_failure;
122
123 if (nft_data_dump(skb, NFTA_RANGE_FROM_DATA, &priv->data_from,
124 NFT_DATA_VALUE, priv->len) < 0 ||
125 nft_data_dump(skb, NFTA_RANGE_TO_DATA, &priv->data_to,
126 NFT_DATA_VALUE, priv->len) < 0)
127 goto nla_put_failure;
128 return 0;
129
130 nla_put_failure:
131 return -1;
132 }
133
134 static const struct nft_expr_ops nft_range_ops = {
135 .type = &nft_range_type,
136 .size = NFT_EXPR_SIZE(sizeof(struct nft_range_expr)),
137 .eval = nft_range_eval,
138 .init = nft_range_init,
139 .dump = nft_range_dump,
140 };
141
142 struct nft_expr_type nft_range_type __read_mostly = {
143 .name = "range",
144 .ops = &nft_range_ops,
145 .policy = nft_range_policy,
146 .maxattr = NFTA_RANGE_MAX,
147 .owner = THIS_MODULE,
148 };
149