]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
20a69341 PM |
2 | /* |
3 | * Copyright (c) 2009 Patrick McHardy <kaber@trash.net> | |
4 | * | |
20a69341 PM |
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/list.h> | |
11 | #include <linux/rbtree.h> | |
12 | #include <linux/netlink.h> | |
13 | #include <linux/netfilter.h> | |
14 | #include <linux/netfilter/nf_tables.h> | |
15 | #include <net/netfilter/nf_tables.h> | |
bd76ed36 | 16 | #include <net/netfilter/nf_tables_core.h> |
20a69341 PM |
17 | |
18 | struct nft_lookup { | |
19 | struct nft_set *set; | |
4f16d25c | 20 | u8 sreg; |
345023b0 | 21 | u8 dreg; |
0071e184 | 22 | bool invert; |
20a69341 PM |
23 | struct nft_set_binding binding; |
24 | }; | |
25 | ||
222440b4 FW |
26 | void nft_lookup_eval(const struct nft_expr *expr, |
27 | struct nft_regs *regs, | |
28 | const struct nft_pktinfo *pkt) | |
20a69341 PM |
29 | { |
30 | const struct nft_lookup *priv = nft_expr_priv(expr); | |
31 | const struct nft_set *set = priv->set; | |
a26c1e49 | 32 | const struct nft_set_ext *ext = NULL; |
aaa31047 | 33 | const struct net *net = nft_net(pkt); |
0071e184 | 34 | bool found; |
20a69341 | 35 | |
aaa31047 PNA |
36 | found = set->ops->lookup(net, set, ®s->data[priv->sreg], &ext) ^ |
37 | priv->invert; | |
0071e184 | 38 | if (!found) { |
aaa31047 PNA |
39 | ext = nft_set_catchall_lookup(net, set); |
40 | if (!ext) { | |
41 | regs->verdict.code = NFT_BREAK; | |
42 | return; | |
43 | } | |
b2832dd6 | 44 | } |
0071e184 | 45 | |
a26c1e49 PNA |
46 | if (ext) { |
47 | if (set->flags & NFT_SET_MAP) | |
48 | nft_data_copy(®s->data[priv->dreg], | |
49 | nft_set_ext_data(ext), set->dlen); | |
0071e184 | 50 | |
a26c1e49 PNA |
51 | nft_set_elem_update_expr(ext, regs, pkt); |
52 | } | |
20a69341 PM |
53 | } |
54 | ||
55 | static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = { | |
b2fbd044 LZ |
56 | [NFTA_LOOKUP_SET] = { .type = NLA_STRING, |
57 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
4c1017aa | 58 | [NFTA_LOOKUP_SET_ID] = { .type = NLA_U32 }, |
20a69341 PM |
59 | [NFTA_LOOKUP_SREG] = { .type = NLA_U32 }, |
60 | [NFTA_LOOKUP_DREG] = { .type = NLA_U32 }, | |
0071e184 | 61 | [NFTA_LOOKUP_FLAGS] = { .type = NLA_U32 }, |
20a69341 PM |
62 | }; |
63 | ||
64 | static int nft_lookup_init(const struct nft_ctx *ctx, | |
65 | const struct nft_expr *expr, | |
66 | const struct nlattr * const tb[]) | |
67 | { | |
68 | struct nft_lookup *priv = nft_expr_priv(expr); | |
37a9cc52 | 69 | u8 genmask = nft_genmask_next(ctx->net); |
20a69341 | 70 | struct nft_set *set; |
0071e184 | 71 | u32 flags; |
20a69341 PM |
72 | int err; |
73 | ||
74 | if (tb[NFTA_LOOKUP_SET] == NULL || | |
75 | tb[NFTA_LOOKUP_SREG] == NULL) | |
76 | return -EINVAL; | |
77 | ||
10659cba PNA |
78 | set = nft_set_lookup_global(ctx->net, ctx->table, tb[NFTA_LOOKUP_SET], |
79 | tb[NFTA_LOOKUP_SET_ID], genmask); | |
c7a72e3f PNA |
80 | if (IS_ERR(set)) |
81 | return PTR_ERR(set); | |
20a69341 | 82 | |
4f16d25c PNA |
83 | err = nft_parse_register_load(tb[NFTA_LOOKUP_SREG], &priv->sreg, |
84 | set->klen); | |
20a69341 PM |
85 | if (err < 0) |
86 | return err; | |
87 | ||
0071e184 AB |
88 | if (tb[NFTA_LOOKUP_FLAGS]) { |
89 | flags = ntohl(nla_get_be32(tb[NFTA_LOOKUP_FLAGS])); | |
90 | ||
91 | if (flags & ~NFT_LOOKUP_F_INV) | |
92 | return -EINVAL; | |
93 | ||
94 | if (flags & NFT_LOOKUP_F_INV) { | |
95 | if (set->flags & NFT_SET_MAP) | |
96 | return -EINVAL; | |
97 | priv->invert = true; | |
98 | } | |
99 | } | |
100 | ||
20a69341 | 101 | if (tb[NFTA_LOOKUP_DREG] != NULL) { |
0071e184 AB |
102 | if (priv->invert) |
103 | return -EINVAL; | |
20a69341 PM |
104 | if (!(set->flags & NFT_SET_MAP)) |
105 | return -EINVAL; | |
106 | ||
345023b0 PNA |
107 | err = nft_parse_register_store(ctx, tb[NFTA_LOOKUP_DREG], |
108 | &priv->dreg, NULL, set->dtype, | |
109 | set->dlen); | |
58f40ab6 PM |
110 | if (err < 0) |
111 | return err; | |
20a69341 PM |
112 | } else if (set->flags & NFT_SET_MAP) |
113 | return -EINVAL; | |
114 | ||
11113e19 PM |
115 | priv->binding.flags = set->flags & NFT_SET_MAP; |
116 | ||
20a69341 PM |
117 | err = nf_tables_bind_set(ctx, set, &priv->binding); |
118 | if (err < 0) | |
119 | return err; | |
120 | ||
121 | priv->set = set; | |
122 | return 0; | |
123 | } | |
124 | ||
cd5125d8 | 125 | static void nft_lookup_deactivate(const struct nft_ctx *ctx, |
f6ac8585 PNA |
126 | const struct nft_expr *expr, |
127 | enum nft_trans_phase phase) | |
cd5125d8 FW |
128 | { |
129 | struct nft_lookup *priv = nft_expr_priv(expr); | |
130 | ||
273fe3f1 PNA |
131 | nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase); |
132 | } | |
133 | ||
134 | static void nft_lookup_activate(const struct nft_ctx *ctx, | |
135 | const struct nft_expr *expr) | |
136 | { | |
137 | struct nft_lookup *priv = nft_expr_priv(expr); | |
f6ac8585 | 138 | |
273fe3f1 | 139 | priv->set->use++; |
cd5125d8 FW |
140 | } |
141 | ||
62472bce PM |
142 | static void nft_lookup_destroy(const struct nft_ctx *ctx, |
143 | const struct nft_expr *expr) | |
20a69341 PM |
144 | { |
145 | struct nft_lookup *priv = nft_expr_priv(expr); | |
146 | ||
cd5125d8 | 147 | nf_tables_destroy_set(ctx, priv->set); |
20a69341 PM |
148 | } |
149 | ||
150 | static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
151 | { | |
152 | const struct nft_lookup *priv = nft_expr_priv(expr); | |
0071e184 | 153 | u32 flags = priv->invert ? NFT_LOOKUP_F_INV : 0; |
20a69341 PM |
154 | |
155 | if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name)) | |
156 | goto nla_put_failure; | |
b1c96ed3 | 157 | if (nft_dump_register(skb, NFTA_LOOKUP_SREG, priv->sreg)) |
20a69341 PM |
158 | goto nla_put_failure; |
159 | if (priv->set->flags & NFT_SET_MAP) | |
b1c96ed3 | 160 | if (nft_dump_register(skb, NFTA_LOOKUP_DREG, priv->dreg)) |
20a69341 | 161 | goto nla_put_failure; |
0071e184 AB |
162 | if (nla_put_be32(skb, NFTA_LOOKUP_FLAGS, htonl(flags))) |
163 | goto nla_put_failure; | |
20a69341 PM |
164 | return 0; |
165 | ||
166 | nla_put_failure: | |
167 | return -1; | |
168 | } | |
169 | ||
a654de8f PNA |
170 | static int nft_lookup_validate_setelem(const struct nft_ctx *ctx, |
171 | struct nft_set *set, | |
172 | const struct nft_set_iter *iter, | |
173 | struct nft_set_elem *elem) | |
174 | { | |
175 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
26b2f552 | 176 | struct nft_ctx *pctx = (struct nft_ctx *)ctx; |
a654de8f | 177 | const struct nft_data *data; |
26b2f552 | 178 | int err; |
a654de8f PNA |
179 | |
180 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
181 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
182 | return 0; | |
183 | ||
184 | data = nft_set_ext_data(ext); | |
185 | switch (data->verdict.code) { | |
186 | case NFT_JUMP: | |
187 | case NFT_GOTO: | |
26b2f552 TY |
188 | pctx->level++; |
189 | err = nft_chain_validate(ctx, data->verdict.chain); | |
190 | if (err < 0) | |
191 | return err; | |
192 | pctx->level--; | |
193 | break; | |
a654de8f | 194 | default: |
26b2f552 | 195 | break; |
a654de8f | 196 | } |
26b2f552 TY |
197 | |
198 | return 0; | |
a654de8f PNA |
199 | } |
200 | ||
201 | static int nft_lookup_validate(const struct nft_ctx *ctx, | |
202 | const struct nft_expr *expr, | |
203 | const struct nft_data **d) | |
204 | { | |
205 | const struct nft_lookup *priv = nft_expr_priv(expr); | |
206 | struct nft_set_iter iter; | |
207 | ||
208 | if (!(priv->set->flags & NFT_SET_MAP) || | |
209 | priv->set->dtype != NFT_DATA_VERDICT) | |
210 | return 0; | |
211 | ||
212 | iter.genmask = nft_genmask_next(ctx->net); | |
213 | iter.skip = 0; | |
214 | iter.count = 0; | |
215 | iter.err = 0; | |
216 | iter.fn = nft_lookup_validate_setelem; | |
217 | ||
218 | priv->set->ops->walk(ctx, priv->set, &iter); | |
219 | if (iter.err < 0) | |
220 | return iter.err; | |
221 | ||
222 | return 0; | |
223 | } | |
224 | ||
ef1f7df9 PM |
225 | static const struct nft_expr_ops nft_lookup_ops = { |
226 | .type = &nft_lookup_type, | |
20a69341 | 227 | .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)), |
20a69341 PM |
228 | .eval = nft_lookup_eval, |
229 | .init = nft_lookup_init, | |
273fe3f1 | 230 | .activate = nft_lookup_activate, |
cd5125d8 | 231 | .deactivate = nft_lookup_deactivate, |
20a69341 PM |
232 | .destroy = nft_lookup_destroy, |
233 | .dump = nft_lookup_dump, | |
a654de8f | 234 | .validate = nft_lookup_validate, |
ef1f7df9 PM |
235 | }; |
236 | ||
4e24877e | 237 | struct nft_expr_type nft_lookup_type __read_mostly = { |
ef1f7df9 PM |
238 | .name = "lookup", |
239 | .ops = &nft_lookup_ops, | |
20a69341 PM |
240 | .policy = nft_lookup_policy, |
241 | .maxattr = NFTA_LOOKUP_MAX, | |
ef1f7df9 | 242 | .owner = THIS_MODULE, |
20a69341 | 243 | }; |