Commit | Line | Data |
---|---|---|
22fe54d5 PM |
1 | /* |
2 | * Copyright (c) 2015 Patrick McHardy <kaber@trash.net> | |
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 | ||
10 | #include <linux/kernel.h> | |
11 | #include <linux/module.h> | |
12 | #include <linux/init.h> | |
13 | #include <linux/netlink.h> | |
14 | #include <linux/netfilter.h> | |
15 | #include <linux/netfilter/nf_tables.h> | |
16 | #include <net/netfilter/nf_tables.h> | |
17 | #include <net/netfilter/nf_tables_core.h> | |
18 | ||
19 | struct nft_dynset { | |
20 | struct nft_set *set; | |
21 | struct nft_set_ext_tmpl tmpl; | |
22 | enum nft_dynset_ops op:8; | |
23 | enum nft_registers sreg_key:8; | |
24 | enum nft_registers sreg_data:8; | |
dbd2be06 | 25 | bool invert; |
22fe54d5 | 26 | u64 timeout; |
3e135cd4 | 27 | struct nft_expr *expr; |
22fe54d5 PM |
28 | struct nft_set_binding binding; |
29 | }; | |
30 | ||
31 | static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr, | |
a55e22e9 | 32 | struct nft_regs *regs) |
22fe54d5 PM |
33 | { |
34 | const struct nft_dynset *priv = nft_expr_priv(expr); | |
3e135cd4 | 35 | struct nft_set_ext *ext; |
22fe54d5 PM |
36 | u64 timeout; |
37 | void *elem; | |
38 | ||
99a0efbe | 39 | if (!atomic_add_unless(&set->nelems, 1, set->size)) |
22fe54d5 PM |
40 | return NULL; |
41 | ||
42 | timeout = priv->timeout ? : set->timeout; | |
43 | elem = nft_set_elem_init(set, &priv->tmpl, | |
a55e22e9 PM |
44 | ®s->data[priv->sreg_key], |
45 | ®s->data[priv->sreg_data], | |
22fe54d5 | 46 | timeout, GFP_ATOMIC); |
61f9e292 LZ |
47 | if (elem == NULL) |
48 | goto err1; | |
3e135cd4 PM |
49 | |
50 | ext = nft_set_elem_ext(set, elem); | |
086f3321 PNA |
51 | if (priv->expr != NULL && |
52 | nft_expr_clone(nft_set_ext_expr(ext), priv->expr) < 0) | |
61f9e292 | 53 | goto err2; |
3e135cd4 | 54 | |
22fe54d5 | 55 | return elem; |
61f9e292 LZ |
56 | |
57 | err2: | |
58 | nft_set_elem_destroy(set, elem, false); | |
59 | err1: | |
60 | if (set->size) | |
61 | atomic_dec(&set->nelems); | |
62 | return NULL; | |
22fe54d5 PM |
63 | } |
64 | ||
10870dd8 FW |
65 | void nft_dynset_eval(const struct nft_expr *expr, |
66 | struct nft_regs *regs, const struct nft_pktinfo *pkt) | |
22fe54d5 PM |
67 | { |
68 | const struct nft_dynset *priv = nft_expr_priv(expr); | |
69 | struct nft_set *set = priv->set; | |
70 | const struct nft_set_ext *ext; | |
3e135cd4 | 71 | const struct nft_expr *sexpr; |
22fe54d5 PM |
72 | u64 timeout; |
73 | ||
a55e22e9 PM |
74 | if (set->ops->update(set, ®s->data[priv->sreg_key], nft_dynset_new, |
75 | expr, regs, &ext)) { | |
3e135cd4 PM |
76 | sexpr = NULL; |
77 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) | |
78 | sexpr = nft_set_ext_expr(ext); | |
79 | ||
22fe54d5 PM |
80 | if (priv->op == NFT_DYNSET_OP_UPDATE && |
81 | nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { | |
82 | timeout = priv->timeout ? : set->timeout; | |
b13468dc | 83 | *nft_set_ext_expiration(ext) = get_jiffies_64() + timeout; |
277a2928 | 84 | } |
22fe54d5 | 85 | |
3e135cd4 PM |
86 | if (sexpr != NULL) |
87 | sexpr->ops->eval(sexpr, regs, pkt); | |
dbd2be06 PNA |
88 | |
89 | if (priv->invert) | |
90 | regs->verdict.code = NFT_BREAK; | |
3e135cd4 PM |
91 | return; |
92 | } | |
277a2928 | 93 | |
dbd2be06 PNA |
94 | if (!priv->invert) |
95 | regs->verdict.code = NFT_BREAK; | |
22fe54d5 PM |
96 | } |
97 | ||
98 | static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = { | |
b2fbd044 LZ |
99 | [NFTA_DYNSET_SET_NAME] = { .type = NLA_STRING, |
100 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
22fe54d5 PM |
101 | [NFTA_DYNSET_SET_ID] = { .type = NLA_U32 }, |
102 | [NFTA_DYNSET_OP] = { .type = NLA_U32 }, | |
103 | [NFTA_DYNSET_SREG_KEY] = { .type = NLA_U32 }, | |
104 | [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 }, | |
105 | [NFTA_DYNSET_TIMEOUT] = { .type = NLA_U64 }, | |
3e135cd4 | 106 | [NFTA_DYNSET_EXPR] = { .type = NLA_NESTED }, |
dbd2be06 | 107 | [NFTA_DYNSET_FLAGS] = { .type = NLA_U32 }, |
22fe54d5 PM |
108 | }; |
109 | ||
110 | static int nft_dynset_init(const struct nft_ctx *ctx, | |
111 | const struct nft_expr *expr, | |
112 | const struct nlattr * const tb[]) | |
113 | { | |
114 | struct nft_dynset *priv = nft_expr_priv(expr); | |
37a9cc52 | 115 | u8 genmask = nft_genmask_next(ctx->net); |
22fe54d5 PM |
116 | struct nft_set *set; |
117 | u64 timeout; | |
118 | int err; | |
119 | ||
f102d66b FW |
120 | lockdep_assert_held(&ctx->net->nft.commit_mutex); |
121 | ||
22fe54d5 PM |
122 | if (tb[NFTA_DYNSET_SET_NAME] == NULL || |
123 | tb[NFTA_DYNSET_OP] == NULL || | |
124 | tb[NFTA_DYNSET_SREG_KEY] == NULL) | |
125 | return -EINVAL; | |
126 | ||
dbd2be06 PNA |
127 | if (tb[NFTA_DYNSET_FLAGS]) { |
128 | u32 flags = ntohl(nla_get_be32(tb[NFTA_DYNSET_FLAGS])); | |
129 | ||
130 | if (flags & ~NFT_DYNSET_F_INV) | |
131 | return -EINVAL; | |
132 | if (flags & NFT_DYNSET_F_INV) | |
133 | priv->invert = true; | |
134 | } | |
135 | ||
10659cba PNA |
136 | set = nft_set_lookup_global(ctx->net, ctx->table, |
137 | tb[NFTA_DYNSET_SET_NAME], | |
138 | tb[NFTA_DYNSET_SET_ID], genmask); | |
c7a72e3f PNA |
139 | if (IS_ERR(set)) |
140 | return PTR_ERR(set); | |
22fe54d5 | 141 | |
bb6a6e8e LZ |
142 | if (set->ops->update == NULL) |
143 | return -EOPNOTSUPP; | |
144 | ||
22fe54d5 PM |
145 | if (set->flags & NFT_SET_CONSTANT) |
146 | return -EBUSY; | |
147 | ||
148 | priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP])); | |
149 | switch (priv->op) { | |
150 | case NFT_DYNSET_OP_ADD: | |
151 | break; | |
152 | case NFT_DYNSET_OP_UPDATE: | |
153 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
154 | return -EOPNOTSUPP; | |
155 | break; | |
156 | default: | |
157 | return -EOPNOTSUPP; | |
158 | } | |
159 | ||
160 | timeout = 0; | |
161 | if (tb[NFTA_DYNSET_TIMEOUT] != NULL) { | |
162 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
163 | return -EINVAL; | |
a8b1e36d AP |
164 | timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64( |
165 | tb[NFTA_DYNSET_TIMEOUT]))); | |
22fe54d5 PM |
166 | } |
167 | ||
b1c96ed3 | 168 | priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]); |
b0c3dc65 | 169 | err = nft_validate_register_load(priv->sreg_key, set->klen); |
22fe54d5 PM |
170 | if (err < 0) |
171 | return err; | |
172 | ||
173 | if (tb[NFTA_DYNSET_SREG_DATA] != NULL) { | |
174 | if (!(set->flags & NFT_SET_MAP)) | |
175 | return -EINVAL; | |
176 | if (set->dtype == NFT_DATA_VERDICT) | |
177 | return -EOPNOTSUPP; | |
178 | ||
b1c96ed3 | 179 | priv->sreg_data = nft_parse_register(tb[NFTA_DYNSET_SREG_DATA]); |
d07db988 | 180 | err = nft_validate_register_load(priv->sreg_data, set->dlen); |
22fe54d5 PM |
181 | if (err < 0) |
182 | return err; | |
183 | } else if (set->flags & NFT_SET_MAP) | |
184 | return -EINVAL; | |
185 | ||
3e135cd4 PM |
186 | if (tb[NFTA_DYNSET_EXPR] != NULL) { |
187 | if (!(set->flags & NFT_SET_EVAL)) | |
188 | return -EINVAL; | |
3e135cd4 PM |
189 | |
190 | priv->expr = nft_expr_init(ctx, tb[NFTA_DYNSET_EXPR]); | |
191 | if (IS_ERR(priv->expr)) | |
192 | return PTR_ERR(priv->expr); | |
193 | ||
194 | err = -EOPNOTSUPP; | |
195 | if (!(priv->expr->ops->type->flags & NFT_EXPR_STATEFUL)) | |
196 | goto err1; | |
79b174ad PNA |
197 | |
198 | if (priv->expr->ops->type->flags & NFT_EXPR_GC) { | |
199 | if (set->flags & NFT_SET_TIMEOUT) | |
200 | goto err1; | |
201 | if (!set->ops->gc_init) | |
202 | goto err1; | |
203 | set->ops->gc_init(set); | |
204 | } | |
215a31f1 | 205 | } |
3e135cd4 | 206 | |
22fe54d5 PM |
207 | nft_set_ext_prepare(&priv->tmpl); |
208 | nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_KEY, set->klen); | |
209 | if (set->flags & NFT_SET_MAP) | |
210 | nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_DATA, set->dlen); | |
3e135cd4 PM |
211 | if (priv->expr != NULL) |
212 | nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_EXPR, | |
213 | priv->expr->ops->size); | |
22fe54d5 PM |
214 | if (set->flags & NFT_SET_TIMEOUT) { |
215 | if (timeout || set->timeout) | |
216 | nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION); | |
217 | } | |
218 | ||
219 | priv->timeout = timeout; | |
220 | ||
221 | err = nf_tables_bind_set(ctx, set, &priv->binding); | |
222 | if (err < 0) | |
3e135cd4 | 223 | goto err1; |
22fe54d5 | 224 | |
99a0efbe FW |
225 | if (set->size == 0) |
226 | set->size = 0xffff; | |
227 | ||
22fe54d5 PM |
228 | priv->set = set; |
229 | return 0; | |
3e135cd4 PM |
230 | |
231 | err1: | |
232 | if (priv->expr != NULL) | |
233 | nft_expr_destroy(ctx, priv->expr); | |
234 | return err; | |
22fe54d5 PM |
235 | } |
236 | ||
cd5125d8 | 237 | static void nft_dynset_deactivate(const struct nft_ctx *ctx, |
f6ac8585 PNA |
238 | const struct nft_expr *expr, |
239 | enum nft_trans_phase phase) | |
cd5125d8 FW |
240 | { |
241 | struct nft_dynset *priv = nft_expr_priv(expr); | |
242 | ||
273fe3f1 PNA |
243 | nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase); |
244 | } | |
245 | ||
246 | static void nft_dynset_activate(const struct nft_ctx *ctx, | |
247 | const struct nft_expr *expr) | |
248 | { | |
249 | struct nft_dynset *priv = nft_expr_priv(expr); | |
f6ac8585 | 250 | |
273fe3f1 | 251 | priv->set->use++; |
cd5125d8 FW |
252 | } |
253 | ||
22fe54d5 PM |
254 | static void nft_dynset_destroy(const struct nft_ctx *ctx, |
255 | const struct nft_expr *expr) | |
256 | { | |
257 | struct nft_dynset *priv = nft_expr_priv(expr); | |
258 | ||
3e135cd4 PM |
259 | if (priv->expr != NULL) |
260 | nft_expr_destroy(ctx, priv->expr); | |
cd5125d8 FW |
261 | |
262 | nf_tables_destroy_set(ctx, priv->set); | |
22fe54d5 PM |
263 | } |
264 | ||
265 | static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
266 | { | |
267 | const struct nft_dynset *priv = nft_expr_priv(expr); | |
dbd2be06 | 268 | u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0; |
22fe54d5 | 269 | |
b1c96ed3 | 270 | if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key)) |
22fe54d5 PM |
271 | goto nla_put_failure; |
272 | if (priv->set->flags & NFT_SET_MAP && | |
b1c96ed3 | 273 | nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data)) |
22fe54d5 PM |
274 | goto nla_put_failure; |
275 | if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op))) | |
276 | goto nla_put_failure; | |
277 | if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name)) | |
278 | goto nla_put_failure; | |
a8b1e36d AP |
279 | if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT, |
280 | cpu_to_be64(jiffies_to_msecs(priv->timeout)), | |
b46f6ded | 281 | NFTA_DYNSET_PAD)) |
22fe54d5 | 282 | goto nla_put_failure; |
3e135cd4 PM |
283 | if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr)) |
284 | goto nla_put_failure; | |
dbd2be06 PNA |
285 | if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags))) |
286 | goto nla_put_failure; | |
22fe54d5 PM |
287 | return 0; |
288 | ||
289 | nla_put_failure: | |
290 | return -1; | |
291 | } | |
292 | ||
22fe54d5 PM |
293 | static const struct nft_expr_ops nft_dynset_ops = { |
294 | .type = &nft_dynset_type, | |
295 | .size = NFT_EXPR_SIZE(sizeof(struct nft_dynset)), | |
296 | .eval = nft_dynset_eval, | |
297 | .init = nft_dynset_init, | |
298 | .destroy = nft_dynset_destroy, | |
273fe3f1 | 299 | .activate = nft_dynset_activate, |
cd5125d8 | 300 | .deactivate = nft_dynset_deactivate, |
22fe54d5 PM |
301 | .dump = nft_dynset_dump, |
302 | }; | |
303 | ||
4e24877e | 304 | struct nft_expr_type nft_dynset_type __read_mostly = { |
22fe54d5 PM |
305 | .name = "dynset", |
306 | .ops = &nft_dynset_ops, | |
307 | .policy = nft_dynset_policy, | |
308 | .maxattr = NFTA_DYNSET_MAX, | |
309 | .owner = THIS_MODULE, | |
310 | }; |