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] = NLA_POLICY_MAX(NLA_BE32, 255),
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(ctx, 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,bool reset)114 static int nft_range_dump(struct sk_buff *skb,
115 const struct nft_expr *expr, bool reset)
116 {
117 const struct nft_range_expr *priv = nft_expr_priv(expr);
118
119 if (nft_dump_register(skb, NFTA_RANGE_SREG, priv->sreg))
120 goto nla_put_failure;
121 if (nla_put_be32(skb, NFTA_RANGE_OP, htonl(priv->op)))
122 goto nla_put_failure;
123
124 if (nft_data_dump(skb, NFTA_RANGE_FROM_DATA, &priv->data_from,
125 NFT_DATA_VALUE, priv->len) < 0 ||
126 nft_data_dump(skb, NFTA_RANGE_TO_DATA, &priv->data_to,
127 NFT_DATA_VALUE, priv->len) < 0)
128 goto nla_put_failure;
129 return 0;
130
131 nla_put_failure:
132 return -1;
133 }
134
135 static const struct nft_expr_ops nft_range_ops = {
136 .type = &nft_range_type,
137 .size = NFT_EXPR_SIZE(sizeof(struct nft_range_expr)),
138 .eval = nft_range_eval,
139 .init = nft_range_init,
140 .dump = nft_range_dump,
141 .reduce = NFT_REDUCE_READONLY,
142 };
143
144 struct nft_expr_type nft_range_type __read_mostly = {
145 .name = "range",
146 .ops = &nft_range_ops,
147 .policy = nft_range_policy,
148 .maxattr = NFTA_RANGE_MAX,
149 .owner = THIS_MODULE,
150 };
151