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; |
a4878eea | 22 | bool dreg_set; |
0071e184 | 23 | bool invert; |
20a69341 PM |
24 | struct nft_set_binding binding; |
25 | }; | |
26 | ||
f227925e FW |
27 | #ifdef CONFIG_RETPOLINE |
28 | bool nft_set_do_lookup(const struct net *net, const struct nft_set *set, | |
29 | const u32 *key, const struct nft_set_ext **ext) | |
30 | { | |
31 | if (set->ops == &nft_set_hash_fast_type.ops) | |
32 | return nft_hash_lookup_fast(net, set, key, ext); | |
33 | if (set->ops == &nft_set_hash_type.ops) | |
34 | return nft_hash_lookup(net, set, key, ext); | |
35 | ||
36 | if (set->ops == &nft_set_rhash_type.ops) | |
37 | return nft_rhash_lookup(net, set, key, ext); | |
38 | ||
39 | if (set->ops == &nft_set_bitmap_type.ops) | |
40 | return nft_bitmap_lookup(net, set, key, ext); | |
41 | ||
42 | if (set->ops == &nft_set_pipapo_type.ops) | |
43 | return nft_pipapo_lookup(net, set, key, ext); | |
44 | #if defined(CONFIG_X86_64) && !defined(CONFIG_UML) | |
45 | if (set->ops == &nft_set_pipapo_avx2_type.ops) | |
46 | return nft_pipapo_avx2_lookup(net, set, key, ext); | |
47 | #endif | |
48 | ||
49 | if (set->ops == &nft_set_rbtree_type.ops) | |
50 | return nft_rbtree_lookup(net, set, key, ext); | |
51 | ||
52 | WARN_ON_ONCE(1); | |
53 | return set->ops->lookup(net, set, key, ext); | |
54 | } | |
55 | EXPORT_SYMBOL_GPL(nft_set_do_lookup); | |
56 | #endif | |
57 | ||
222440b4 FW |
58 | void nft_lookup_eval(const struct nft_expr *expr, |
59 | struct nft_regs *regs, | |
60 | const struct nft_pktinfo *pkt) | |
20a69341 PM |
61 | { |
62 | const struct nft_lookup *priv = nft_expr_priv(expr); | |
63 | const struct nft_set *set = priv->set; | |
a26c1e49 | 64 | const struct nft_set_ext *ext = NULL; |
aaa31047 | 65 | const struct net *net = nft_net(pkt); |
0071e184 | 66 | bool found; |
20a69341 | 67 | |
0974cff3 FW |
68 | found = nft_set_do_lookup(net, set, ®s->data[priv->sreg], &ext) ^ |
69 | priv->invert; | |
0071e184 | 70 | if (!found) { |
aaa31047 PNA |
71 | ext = nft_set_catchall_lookup(net, set); |
72 | if (!ext) { | |
73 | regs->verdict.code = NFT_BREAK; | |
74 | return; | |
75 | } | |
b2832dd6 | 76 | } |
0071e184 | 77 | |
a26c1e49 | 78 | if (ext) { |
a4878eea | 79 | if (priv->dreg_set) |
a26c1e49 PNA |
80 | nft_data_copy(®s->data[priv->dreg], |
81 | nft_set_ext_data(ext), set->dlen); | |
0071e184 | 82 | |
a26c1e49 PNA |
83 | nft_set_elem_update_expr(ext, regs, pkt); |
84 | } | |
20a69341 PM |
85 | } |
86 | ||
87 | static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = { | |
b2fbd044 LZ |
88 | [NFTA_LOOKUP_SET] = { .type = NLA_STRING, |
89 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
4c1017aa | 90 | [NFTA_LOOKUP_SET_ID] = { .type = NLA_U32 }, |
20a69341 PM |
91 | [NFTA_LOOKUP_SREG] = { .type = NLA_U32 }, |
92 | [NFTA_LOOKUP_DREG] = { .type = NLA_U32 }, | |
100a11b6 FW |
93 | [NFTA_LOOKUP_FLAGS] = |
94 | NLA_POLICY_MASK(NLA_BE32, NFT_LOOKUP_F_INV), | |
20a69341 PM |
95 | }; |
96 | ||
97 | static int nft_lookup_init(const struct nft_ctx *ctx, | |
98 | const struct nft_expr *expr, | |
99 | const struct nlattr * const tb[]) | |
100 | { | |
101 | struct nft_lookup *priv = nft_expr_priv(expr); | |
37a9cc52 | 102 | u8 genmask = nft_genmask_next(ctx->net); |
20a69341 | 103 | struct nft_set *set; |
0071e184 | 104 | u32 flags; |
20a69341 PM |
105 | int err; |
106 | ||
107 | if (tb[NFTA_LOOKUP_SET] == NULL || | |
108 | tb[NFTA_LOOKUP_SREG] == NULL) | |
109 | return -EINVAL; | |
110 | ||
10659cba PNA |
111 | set = nft_set_lookup_global(ctx->net, ctx->table, tb[NFTA_LOOKUP_SET], |
112 | tb[NFTA_LOOKUP_SET_ID], genmask); | |
c7a72e3f PNA |
113 | if (IS_ERR(set)) |
114 | return PTR_ERR(set); | |
20a69341 | 115 | |
4f16d25c PNA |
116 | err = nft_parse_register_load(tb[NFTA_LOOKUP_SREG], &priv->sreg, |
117 | set->klen); | |
20a69341 PM |
118 | if (err < 0) |
119 | return err; | |
120 | ||
0071e184 AB |
121 | if (tb[NFTA_LOOKUP_FLAGS]) { |
122 | flags = ntohl(nla_get_be32(tb[NFTA_LOOKUP_FLAGS])); | |
123 | ||
a4878eea | 124 | if (flags & NFT_LOOKUP_F_INV) |
0071e184 | 125 | priv->invert = true; |
0071e184 AB |
126 | } |
127 | ||
20a69341 | 128 | if (tb[NFTA_LOOKUP_DREG] != NULL) { |
0071e184 AB |
129 | if (priv->invert) |
130 | return -EINVAL; | |
20a69341 PM |
131 | if (!(set->flags & NFT_SET_MAP)) |
132 | return -EINVAL; | |
133 | ||
345023b0 PNA |
134 | err = nft_parse_register_store(ctx, tb[NFTA_LOOKUP_DREG], |
135 | &priv->dreg, NULL, set->dtype, | |
136 | set->dlen); | |
58f40ab6 PM |
137 | if (err < 0) |
138 | return err; | |
a4878eea FW |
139 | priv->dreg_set = true; |
140 | } else if (set->flags & NFT_SET_MAP) { | |
141 | /* Map given, but user asks for lookup only (i.e. to | |
142 | * ignore value assoicated with key). | |
143 | * | |
144 | * This makes no sense for anonymous maps since they are | |
145 | * scoped to the rule, but for named sets this can be useful. | |
146 | */ | |
147 | if (set->flags & NFT_SET_ANONYMOUS) | |
148 | return -EINVAL; | |
149 | } | |
20a69341 | 150 | |
11113e19 PM |
151 | priv->binding.flags = set->flags & NFT_SET_MAP; |
152 | ||
20a69341 PM |
153 | err = nf_tables_bind_set(ctx, set, &priv->binding); |
154 | if (err < 0) | |
155 | return err; | |
156 | ||
157 | priv->set = set; | |
158 | return 0; | |
159 | } | |
160 | ||
cd5125d8 | 161 | static void nft_lookup_deactivate(const struct nft_ctx *ctx, |
f6ac8585 PNA |
162 | const struct nft_expr *expr, |
163 | enum nft_trans_phase phase) | |
cd5125d8 FW |
164 | { |
165 | struct nft_lookup *priv = nft_expr_priv(expr); | |
166 | ||
273fe3f1 PNA |
167 | nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase); |
168 | } | |
169 | ||
170 | static void nft_lookup_activate(const struct nft_ctx *ctx, | |
171 | const struct nft_expr *expr) | |
172 | { | |
173 | struct nft_lookup *priv = nft_expr_priv(expr); | |
f6ac8585 | 174 | |
c1592a89 | 175 | nf_tables_activate_set(ctx, priv->set); |
cd5125d8 FW |
176 | } |
177 | ||
62472bce PM |
178 | static void nft_lookup_destroy(const struct nft_ctx *ctx, |
179 | const struct nft_expr *expr) | |
20a69341 PM |
180 | { |
181 | struct nft_lookup *priv = nft_expr_priv(expr); | |
182 | ||
cd5125d8 | 183 | nf_tables_destroy_set(ctx, priv->set); |
20a69341 PM |
184 | } |
185 | ||
7d34aa3e PS |
186 | static int nft_lookup_dump(struct sk_buff *skb, |
187 | const struct nft_expr *expr, bool reset) | |
20a69341 PM |
188 | { |
189 | const struct nft_lookup *priv = nft_expr_priv(expr); | |
0071e184 | 190 | u32 flags = priv->invert ? NFT_LOOKUP_F_INV : 0; |
20a69341 PM |
191 | |
192 | if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name)) | |
193 | goto nla_put_failure; | |
b1c96ed3 | 194 | if (nft_dump_register(skb, NFTA_LOOKUP_SREG, priv->sreg)) |
20a69341 | 195 | goto nla_put_failure; |
a4878eea | 196 | if (priv->dreg_set) |
b1c96ed3 | 197 | if (nft_dump_register(skb, NFTA_LOOKUP_DREG, priv->dreg)) |
20a69341 | 198 | goto nla_put_failure; |
0071e184 AB |
199 | if (nla_put_be32(skb, NFTA_LOOKUP_FLAGS, htonl(flags))) |
200 | goto nla_put_failure; | |
20a69341 PM |
201 | return 0; |
202 | ||
203 | nla_put_failure: | |
204 | return -1; | |
205 | } | |
206 | ||
a654de8f PNA |
207 | static int nft_lookup_validate(const struct nft_ctx *ctx, |
208 | const struct nft_expr *expr, | |
209 | const struct nft_data **d) | |
210 | { | |
211 | const struct nft_lookup *priv = nft_expr_priv(expr); | |
212 | struct nft_set_iter iter; | |
213 | ||
214 | if (!(priv->set->flags & NFT_SET_MAP) || | |
215 | priv->set->dtype != NFT_DATA_VERDICT) | |
216 | return 0; | |
217 | ||
218 | iter.genmask = nft_genmask_next(ctx->net); | |
219 | iter.skip = 0; | |
220 | iter.count = 0; | |
221 | iter.err = 0; | |
d46fc894 | 222 | iter.fn = nft_setelem_validate; |
a654de8f PNA |
223 | |
224 | priv->set->ops->walk(ctx, priv->set, &iter); | |
d46fc894 PNA |
225 | if (!iter.err) |
226 | iter.err = nft_set_catchall_validate(ctx, priv->set); | |
227 | ||
a654de8f PNA |
228 | if (iter.err < 0) |
229 | return iter.err; | |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
e50ae445 FW |
234 | static bool nft_lookup_reduce(struct nft_regs_track *track, |
235 | const struct nft_expr *expr) | |
236 | { | |
237 | const struct nft_lookup *priv = nft_expr_priv(expr); | |
238 | ||
239 | if (priv->set->flags & NFT_SET_MAP) | |
240 | nft_reg_track_cancel(track, priv->dreg, priv->set->dlen); | |
241 | ||
242 | return false; | |
243 | } | |
244 | ||
ef1f7df9 PM |
245 | static const struct nft_expr_ops nft_lookup_ops = { |
246 | .type = &nft_lookup_type, | |
20a69341 | 247 | .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)), |
20a69341 PM |
248 | .eval = nft_lookup_eval, |
249 | .init = nft_lookup_init, | |
273fe3f1 | 250 | .activate = nft_lookup_activate, |
cd5125d8 | 251 | .deactivate = nft_lookup_deactivate, |
20a69341 PM |
252 | .destroy = nft_lookup_destroy, |
253 | .dump = nft_lookup_dump, | |
a654de8f | 254 | .validate = nft_lookup_validate, |
e50ae445 | 255 | .reduce = nft_lookup_reduce, |
ef1f7df9 PM |
256 | }; |
257 | ||
4e24877e | 258 | struct nft_expr_type nft_lookup_type __read_mostly = { |
ef1f7df9 PM |
259 | .name = "lookup", |
260 | .ops = &nft_lookup_ops, | |
20a69341 PM |
261 | .policy = nft_lookup_policy, |
262 | .maxattr = NFTA_LOOKUP_MAX, | |
ef1f7df9 | 263 | .owner = THIS_MODULE, |
20a69341 | 264 | }; |