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