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