]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
96518518 | 2 | /* |
20a69341 | 3 | * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net> |
96518518 | 4 | * |
96518518 PM |
5 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
6 | */ | |
7 | ||
8 | #include <linux/module.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/list.h> | |
11 | #include <linux/skbuff.h> | |
12 | #include <linux/netlink.h> | |
1ff75a3e | 13 | #include <linux/vmalloc.h> |
0eb71a9d | 14 | #include <linux/rhashtable.h> |
96518518 PM |
15 | #include <linux/netfilter.h> |
16 | #include <linux/netfilter/nfnetlink.h> | |
17 | #include <linux/netfilter/nf_tables.h> | |
3b49e2e9 | 18 | #include <net/netfilter/nf_flow_table.h> |
96518518 PM |
19 | #include <net/netfilter/nf_tables_core.h> |
20 | #include <net/netfilter/nf_tables.h> | |
c9626a2c | 21 | #include <net/netfilter/nf_tables_offload.h> |
99633ab2 | 22 | #include <net/net_namespace.h> |
96518518 PM |
23 | #include <net/sock.h> |
24 | ||
9332d27d FW |
25 | #define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-")) |
26 | ||
96518518 | 27 | static LIST_HEAD(nf_tables_expressions); |
e5009240 | 28 | static LIST_HEAD(nf_tables_objects); |
3b49e2e9 | 29 | static LIST_HEAD(nf_tables_flowtables); |
0935d558 FW |
30 | static LIST_HEAD(nf_tables_destroy_list); |
31 | static DEFINE_SPINLOCK(nf_tables_destroy_list_lock); | |
3ecbfd65 | 32 | static u64 table_handle; |
96518518 | 33 | |
a654de8f PNA |
34 | enum { |
35 | NFT_VALIDATE_SKIP = 0, | |
36 | NFT_VALIDATE_NEED, | |
37 | NFT_VALIDATE_DO, | |
38 | }; | |
39 | ||
4d44175a FW |
40 | static struct rhltable nft_objname_ht; |
41 | ||
1b2470e5 FW |
42 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed); |
43 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed); | |
44 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *); | |
45 | ||
4d44175a FW |
46 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed); |
47 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed); | |
48 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *); | |
49 | ||
1b2470e5 FW |
50 | static const struct rhashtable_params nft_chain_ht_params = { |
51 | .head_offset = offsetof(struct nft_chain, rhlhead), | |
52 | .key_offset = offsetof(struct nft_chain, name), | |
53 | .hashfn = nft_chain_hash, | |
54 | .obj_hashfn = nft_chain_hash_obj, | |
55 | .obj_cmpfn = nft_chain_hash_cmp, | |
1b2470e5 FW |
56 | .automatic_shrinking = true, |
57 | }; | |
58 | ||
4d44175a FW |
59 | static const struct rhashtable_params nft_objname_ht_params = { |
60 | .head_offset = offsetof(struct nft_object, rhlhead), | |
61 | .key_offset = offsetof(struct nft_object, key), | |
62 | .hashfn = nft_objname_hash, | |
63 | .obj_hashfn = nft_objname_hash_obj, | |
64 | .obj_cmpfn = nft_objname_hash_cmp, | |
65 | .automatic_shrinking = true, | |
66 | }; | |
67 | ||
a654de8f PNA |
68 | static void nft_validate_state_update(struct net *net, u8 new_validate_state) |
69 | { | |
70 | switch (net->nft.validate_state) { | |
71 | case NFT_VALIDATE_SKIP: | |
72 | WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO); | |
73 | break; | |
74 | case NFT_VALIDATE_NEED: | |
75 | break; | |
76 | case NFT_VALIDATE_DO: | |
77 | if (new_validate_state == NFT_VALIDATE_NEED) | |
78 | return; | |
79 | } | |
80 | ||
81 | net->nft.validate_state = new_validate_state; | |
82 | } | |
0935d558 FW |
83 | static void nf_tables_trans_destroy_work(struct work_struct *w); |
84 | static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work); | |
a654de8f | 85 | |
7c95f6d8 | 86 | static void nft_ctx_init(struct nft_ctx *ctx, |
633c9a84 | 87 | struct net *net, |
7c95f6d8 PNA |
88 | const struct sk_buff *skb, |
89 | const struct nlmsghdr *nlh, | |
36596dad | 90 | u8 family, |
7c95f6d8 PNA |
91 | struct nft_table *table, |
92 | struct nft_chain *chain, | |
93 | const struct nlattr * const *nla) | |
94 | { | |
633c9a84 | 95 | ctx->net = net; |
36596dad | 96 | ctx->family = family; |
26b2f552 | 97 | ctx->level = 0; |
128ad332 PNA |
98 | ctx->table = table; |
99 | ctx->chain = chain; | |
100 | ctx->nla = nla; | |
101 | ctx->portid = NETLINK_CB(skb).portid; | |
102 | ctx->report = nlmsg_report(nlh); | |
c9626a2c | 103 | ctx->flags = nlh->nlmsg_flags; |
128ad332 | 104 | ctx->seq = nlh->nlmsg_seq; |
7c95f6d8 PNA |
105 | } |
106 | ||
8411b644 PNA |
107 | static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx, |
108 | int msg_type, u32 size, gfp_t gfp) | |
1081d11b PNA |
109 | { |
110 | struct nft_trans *trans; | |
111 | ||
8411b644 | 112 | trans = kzalloc(sizeof(struct nft_trans) + size, gfp); |
1081d11b PNA |
113 | if (trans == NULL) |
114 | return NULL; | |
115 | ||
b380e5c7 | 116 | trans->msg_type = msg_type; |
1081d11b PNA |
117 | trans->ctx = *ctx; |
118 | ||
119 | return trans; | |
120 | } | |
121 | ||
8411b644 PNA |
122 | static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx, |
123 | int msg_type, u32 size) | |
124 | { | |
125 | return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL); | |
126 | } | |
127 | ||
1081d11b PNA |
128 | static void nft_trans_destroy(struct nft_trans *trans) |
129 | { | |
130 | list_del(&trans->list); | |
131 | kfree(trans); | |
132 | } | |
133 | ||
f6ac8585 PNA |
134 | static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set) |
135 | { | |
136 | struct net *net = ctx->net; | |
137 | struct nft_trans *trans; | |
138 | ||
139 | if (!nft_set_is_anonymous(set)) | |
140 | return; | |
141 | ||
142 | list_for_each_entry_reverse(trans, &net->nft.commit_list, list) { | |
6a0a8d10 PNA |
143 | switch (trans->msg_type) { |
144 | case NFT_MSG_NEWSET: | |
145 | if (nft_trans_set(trans) == set) | |
146 | nft_trans_set_bound(trans) = true; | |
147 | break; | |
148 | case NFT_MSG_NEWSETELEM: | |
149 | if (nft_trans_elem_set(trans) == set) | |
150 | nft_trans_elem_set_bound(trans) = true; | |
f6ac8585 PNA |
151 | break; |
152 | } | |
153 | } | |
154 | } | |
155 | ||
d54725cd PNA |
156 | static int nft_netdev_register_hooks(struct net *net, |
157 | struct list_head *hook_list) | |
158 | { | |
159 | struct nft_hook *hook; | |
160 | int err, j; | |
161 | ||
162 | j = 0; | |
163 | list_for_each_entry(hook, hook_list, list) { | |
164 | err = nf_register_net_hook(net, &hook->ops); | |
165 | if (err < 0) | |
166 | goto err_register; | |
167 | ||
168 | j++; | |
169 | } | |
170 | return 0; | |
171 | ||
172 | err_register: | |
173 | list_for_each_entry(hook, hook_list, list) { | |
174 | if (j-- <= 0) | |
175 | break; | |
176 | ||
177 | nf_unregister_net_hook(net, &hook->ops); | |
178 | } | |
179 | return err; | |
180 | } | |
181 | ||
182 | static void nft_netdev_unregister_hooks(struct net *net, | |
183 | struct list_head *hook_list) | |
184 | { | |
185 | struct nft_hook *hook; | |
186 | ||
187 | list_for_each_entry(hook, hook_list, list) | |
188 | nf_unregister_net_hook(net, &hook->ops); | |
189 | } | |
190 | ||
191 | static int nft_register_basechain_hooks(struct net *net, int family, | |
192 | struct nft_base_chain *basechain) | |
193 | { | |
194 | if (family == NFPROTO_NETDEV) | |
195 | return nft_netdev_register_hooks(net, &basechain->hook_list); | |
196 | ||
197 | return nf_register_net_hook(net, &basechain->ops); | |
198 | } | |
199 | ||
200 | static void nft_unregister_basechain_hooks(struct net *net, int family, | |
201 | struct nft_base_chain *basechain) | |
202 | { | |
203 | if (family == NFPROTO_NETDEV) | |
204 | nft_netdev_unregister_hooks(net, &basechain->hook_list); | |
205 | else | |
206 | nf_unregister_net_hook(net, &basechain->ops); | |
207 | } | |
208 | ||
c974a3a3 PNA |
209 | static int nf_tables_register_hook(struct net *net, |
210 | const struct nft_table *table, | |
211 | struct nft_chain *chain) | |
d8ee8f7c | 212 | { |
d54725cd | 213 | struct nft_base_chain *basechain; |
a37061a6 | 214 | const struct nf_hook_ops *ops; |
ae6153b5 | 215 | |
d8ee8f7c | 216 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 217 | !nft_is_base_chain(chain)) |
d8ee8f7c PNA |
218 | return 0; |
219 | ||
4e25ceb8 FW |
220 | basechain = nft_base_chain(chain); |
221 | ops = &basechain->ops; | |
ae6153b5 | 222 | |
4e25ceb8 FW |
223 | if (basechain->type->ops_register) |
224 | return basechain->type->ops_register(net, ops); | |
225 | ||
d54725cd | 226 | return nft_register_basechain_hooks(net, table->family, basechain); |
d8ee8f7c PNA |
227 | } |
228 | ||
c974a3a3 PNA |
229 | static void nf_tables_unregister_hook(struct net *net, |
230 | const struct nft_table *table, | |
231 | struct nft_chain *chain) | |
c5598794 | 232 | { |
d54725cd | 233 | struct nft_base_chain *basechain; |
4e25ceb8 FW |
234 | const struct nf_hook_ops *ops; |
235 | ||
d8ee8f7c | 236 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 237 | !nft_is_base_chain(chain)) |
d8ee8f7c | 238 | return; |
4e25ceb8 FW |
239 | basechain = nft_base_chain(chain); |
240 | ops = &basechain->ops; | |
d8ee8f7c | 241 | |
4e25ceb8 FW |
242 | if (basechain->type->ops_unregister) |
243 | return basechain->type->ops_unregister(net, ops); | |
244 | ||
d54725cd | 245 | nft_unregister_basechain_hooks(net, table->family, basechain); |
c5598794 AB |
246 | } |
247 | ||
ee01d542 AB |
248 | static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type) |
249 | { | |
250 | struct nft_trans *trans; | |
251 | ||
252 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table)); | |
253 | if (trans == NULL) | |
254 | return -ENOMEM; | |
255 | ||
256 | if (msg_type == NFT_MSG_NEWTABLE) | |
f2a6d766 | 257 | nft_activate_next(ctx->net, ctx->table); |
ee01d542 AB |
258 | |
259 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
260 | return 0; | |
261 | } | |
262 | ||
263 | static int nft_deltable(struct nft_ctx *ctx) | |
264 | { | |
265 | int err; | |
266 | ||
267 | err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE); | |
268 | if (err < 0) | |
269 | return err; | |
270 | ||
f2a6d766 | 271 | nft_deactivate_next(ctx->net, ctx->table); |
ee01d542 AB |
272 | return err; |
273 | } | |
274 | ||
66293c46 | 275 | static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type) |
ee01d542 AB |
276 | { |
277 | struct nft_trans *trans; | |
278 | ||
279 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain)); | |
280 | if (trans == NULL) | |
66293c46 | 281 | return ERR_PTR(-ENOMEM); |
ee01d542 AB |
282 | |
283 | if (msg_type == NFT_MSG_NEWCHAIN) | |
664b0f8c | 284 | nft_activate_next(ctx->net, ctx->chain); |
ee01d542 AB |
285 | |
286 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
66293c46 | 287 | return trans; |
ee01d542 AB |
288 | } |
289 | ||
290 | static int nft_delchain(struct nft_ctx *ctx) | |
291 | { | |
66293c46 | 292 | struct nft_trans *trans; |
ee01d542 | 293 | |
66293c46 FW |
294 | trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN); |
295 | if (IS_ERR(trans)) | |
296 | return PTR_ERR(trans); | |
ee01d542 AB |
297 | |
298 | ctx->table->use--; | |
664b0f8c | 299 | nft_deactivate_next(ctx->net, ctx->chain); |
ee01d542 | 300 | |
66293c46 | 301 | return 0; |
ee01d542 AB |
302 | } |
303 | ||
bb7b40ae PNA |
304 | static void nft_rule_expr_activate(const struct nft_ctx *ctx, |
305 | struct nft_rule *rule) | |
306 | { | |
307 | struct nft_expr *expr; | |
308 | ||
309 | expr = nft_expr_first(rule); | |
310 | while (expr != nft_expr_last(rule) && expr->ops) { | |
311 | if (expr->ops->activate) | |
312 | expr->ops->activate(ctx, expr); | |
313 | ||
314 | expr = nft_expr_next(expr); | |
315 | } | |
316 | } | |
317 | ||
318 | static void nft_rule_expr_deactivate(const struct nft_ctx *ctx, | |
f6ac8585 PNA |
319 | struct nft_rule *rule, |
320 | enum nft_trans_phase phase) | |
bb7b40ae PNA |
321 | { |
322 | struct nft_expr *expr; | |
323 | ||
324 | expr = nft_expr_first(rule); | |
325 | while (expr != nft_expr_last(rule) && expr->ops) { | |
326 | if (expr->ops->deactivate) | |
f6ac8585 | 327 | expr->ops->deactivate(ctx, expr, phase); |
bb7b40ae PNA |
328 | |
329 | expr = nft_expr_next(expr); | |
330 | } | |
331 | } | |
332 | ||
ee01d542 AB |
333 | static int |
334 | nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule) | |
335 | { | |
336 | /* You cannot delete the same rule twice */ | |
889f7ee7 PNA |
337 | if (nft_is_active_next(ctx->net, rule)) { |
338 | nft_deactivate_next(ctx->net, rule); | |
ee01d542 AB |
339 | ctx->chain->use--; |
340 | return 0; | |
341 | } | |
342 | return -ENOENT; | |
343 | } | |
344 | ||
345 | static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type, | |
346 | struct nft_rule *rule) | |
347 | { | |
348 | struct nft_trans *trans; | |
349 | ||
350 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule)); | |
351 | if (trans == NULL) | |
352 | return NULL; | |
353 | ||
1a94e38d PNA |
354 | if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) { |
355 | nft_trans_rule_id(trans) = | |
356 | ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID])); | |
357 | } | |
ee01d542 AB |
358 | nft_trans_rule(trans) = rule; |
359 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
360 | ||
361 | return trans; | |
362 | } | |
363 | ||
364 | static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule) | |
365 | { | |
63b48c73 | 366 | struct nft_flow_rule *flow; |
ee01d542 AB |
367 | struct nft_trans *trans; |
368 | int err; | |
369 | ||
370 | trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule); | |
371 | if (trans == NULL) | |
372 | return -ENOMEM; | |
373 | ||
63b48c73 PNA |
374 | if (ctx->chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
375 | flow = nft_flow_rule_create(ctx->net, rule); | |
376 | if (IS_ERR(flow)) { | |
377 | nft_trans_destroy(trans); | |
378 | return PTR_ERR(flow); | |
379 | } | |
380 | ||
381 | nft_trans_flow_rule(trans) = flow; | |
382 | } | |
383 | ||
ee01d542 AB |
384 | err = nf_tables_delrule_deactivate(ctx, rule); |
385 | if (err < 0) { | |
386 | nft_trans_destroy(trans); | |
387 | return err; | |
388 | } | |
f6ac8585 | 389 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE); |
ee01d542 AB |
390 | |
391 | return 0; | |
392 | } | |
393 | ||
394 | static int nft_delrule_by_chain(struct nft_ctx *ctx) | |
395 | { | |
396 | struct nft_rule *rule; | |
397 | int err; | |
398 | ||
399 | list_for_each_entry(rule, &ctx->chain->rules, list) { | |
23b7ca4f PNA |
400 | if (!nft_is_active_next(ctx->net, rule)) |
401 | continue; | |
402 | ||
ee01d542 AB |
403 | err = nft_delrule(ctx, rule); |
404 | if (err < 0) | |
405 | return err; | |
406 | } | |
407 | return 0; | |
408 | } | |
409 | ||
cd5125d8 | 410 | static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type, |
ee01d542 AB |
411 | struct nft_set *set) |
412 | { | |
413 | struct nft_trans *trans; | |
414 | ||
415 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set)); | |
416 | if (trans == NULL) | |
417 | return -ENOMEM; | |
418 | ||
419 | if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) { | |
420 | nft_trans_set_id(trans) = | |
421 | ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID])); | |
37a9cc52 | 422 | nft_activate_next(ctx->net, set); |
ee01d542 AB |
423 | } |
424 | nft_trans_set(trans) = set; | |
425 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
426 | ||
427 | return 0; | |
428 | } | |
429 | ||
cd5125d8 | 430 | static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set) |
ee01d542 AB |
431 | { |
432 | int err; | |
433 | ||
434 | err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set); | |
435 | if (err < 0) | |
436 | return err; | |
437 | ||
37a9cc52 | 438 | nft_deactivate_next(ctx->net, set); |
ee01d542 AB |
439 | ctx->table->use--; |
440 | ||
441 | return err; | |
442 | } | |
443 | ||
e5009240 PNA |
444 | static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type, |
445 | struct nft_object *obj) | |
446 | { | |
447 | struct nft_trans *trans; | |
448 | ||
449 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj)); | |
450 | if (trans == NULL) | |
451 | return -ENOMEM; | |
452 | ||
453 | if (msg_type == NFT_MSG_NEWOBJ) | |
454 | nft_activate_next(ctx->net, obj); | |
455 | ||
456 | nft_trans_obj(trans) = obj; | |
457 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
458 | ||
459 | return 0; | |
460 | } | |
461 | ||
462 | static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj) | |
463 | { | |
464 | int err; | |
465 | ||
466 | err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj); | |
467 | if (err < 0) | |
468 | return err; | |
469 | ||
470 | nft_deactivate_next(ctx->net, obj); | |
471 | ctx->table->use--; | |
472 | ||
473 | return err; | |
474 | } | |
475 | ||
3b49e2e9 PNA |
476 | static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type, |
477 | struct nft_flowtable *flowtable) | |
478 | { | |
479 | struct nft_trans *trans; | |
480 | ||
481 | trans = nft_trans_alloc(ctx, msg_type, | |
482 | sizeof(struct nft_trans_flowtable)); | |
483 | if (trans == NULL) | |
484 | return -ENOMEM; | |
485 | ||
486 | if (msg_type == NFT_MSG_NEWFLOWTABLE) | |
487 | nft_activate_next(ctx->net, flowtable); | |
488 | ||
489 | nft_trans_flowtable(trans) = flowtable; | |
490 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
491 | ||
492 | return 0; | |
493 | } | |
494 | ||
495 | static int nft_delflowtable(struct nft_ctx *ctx, | |
496 | struct nft_flowtable *flowtable) | |
497 | { | |
498 | int err; | |
499 | ||
500 | err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable); | |
501 | if (err < 0) | |
502 | return err; | |
503 | ||
504 | nft_deactivate_next(ctx->net, flowtable); | |
505 | ctx->table->use--; | |
506 | ||
507 | return err; | |
508 | } | |
509 | ||
96518518 PM |
510 | /* |
511 | * Tables | |
512 | */ | |
513 | ||
36596dad | 514 | static struct nft_table *nft_table_lookup(const struct net *net, |
f2a6d766 | 515 | const struct nlattr *nla, |
36596dad | 516 | u8 family, u8 genmask) |
96518518 PM |
517 | { |
518 | struct nft_table *table; | |
519 | ||
cac20fcd PNA |
520 | if (nla == NULL) |
521 | return ERR_PTR(-EINVAL); | |
522 | ||
d9adf22a | 523 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
f2a6d766 | 524 | if (!nla_strcmp(nla, table->name) && |
98319cb9 | 525 | table->family == family && |
f2a6d766 | 526 | nft_active_genmask(table, genmask)) |
96518518 PM |
527 | return table; |
528 | } | |
cac20fcd PNA |
529 | |
530 | return ERR_PTR(-ENOENT); | |
96518518 PM |
531 | } |
532 | ||
3ecbfd65 HS |
533 | static struct nft_table *nft_table_lookup_byhandle(const struct net *net, |
534 | const struct nlattr *nla, | |
535 | u8 genmask) | |
536 | { | |
537 | struct nft_table *table; | |
538 | ||
539 | list_for_each_entry(table, &net->nft.tables, list) { | |
540 | if (be64_to_cpu(nla_get_be64(nla)) == table->handle && | |
541 | nft_active_genmask(table, genmask)) | |
542 | return table; | |
543 | } | |
3ecbfd65 HS |
544 | |
545 | return ERR_PTR(-ENOENT); | |
546 | } | |
547 | ||
96518518 PM |
548 | static inline u64 nf_tables_alloc_handle(struct nft_table *table) |
549 | { | |
550 | return ++table->hgenerator; | |
551 | } | |
552 | ||
32537e91 | 553 | static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX]; |
9370761c | 554 | |
82603549 PNA |
555 | static const struct nft_chain_type * |
556 | __nft_chain_type_get(u8 family, enum nft_chain_types type) | |
557 | { | |
558 | if (family >= NFPROTO_NUMPROTO || | |
559 | type >= NFT_CHAIN_T_MAX) | |
560 | return NULL; | |
561 | ||
562 | return chain_type[family][type]; | |
563 | } | |
564 | ||
32537e91 | 565 | static const struct nft_chain_type * |
1ea26cca | 566 | __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) |
9370761c | 567 | { |
82603549 | 568 | const struct nft_chain_type *type; |
9370761c PNA |
569 | int i; |
570 | ||
baae3e62 | 571 | for (i = 0; i < NFT_CHAIN_T_MAX; i++) { |
82603549 PNA |
572 | type = __nft_chain_type_get(family, i); |
573 | if (!type) | |
574 | continue; | |
575 | if (!nla_strcmp(nla, type->name)) | |
576 | return type; | |
9370761c | 577 | } |
baae3e62 | 578 | return NULL; |
9370761c PNA |
579 | } |
580 | ||
eb014de4 PNA |
581 | struct nft_module_request { |
582 | struct list_head list; | |
583 | char module[MODULE_NAME_LEN]; | |
584 | bool done; | |
585 | }; | |
586 | ||
452238e8 | 587 | #ifdef CONFIG_MODULES |
eb014de4 | 588 | static int nft_request_module(struct net *net, const char *fmt, ...) |
452238e8 FW |
589 | { |
590 | char module_name[MODULE_NAME_LEN]; | |
eb014de4 | 591 | struct nft_module_request *req; |
452238e8 FW |
592 | va_list args; |
593 | int ret; | |
594 | ||
452238e8 FW |
595 | va_start(args, fmt); |
596 | ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args); | |
597 | va_end(args); | |
9332d27d | 598 | if (ret >= MODULE_NAME_LEN) |
eb014de4 | 599 | return 0; |
452238e8 | 600 | |
eb014de4 PNA |
601 | list_for_each_entry(req, &net->nft.module_list, list) { |
602 | if (!strcmp(req->module, module_name)) { | |
603 | if (req->done) | |
604 | return 0; | |
ec7470b8 | 605 | |
eb014de4 PNA |
606 | /* A request to load this module already exists. */ |
607 | return -EAGAIN; | |
608 | } | |
609 | } | |
610 | ||
611 | req = kmalloc(sizeof(*req), GFP_KERNEL); | |
612 | if (!req) | |
613 | return -ENOMEM; | |
614 | ||
615 | req->done = false; | |
616 | strlcpy(req->module, module_name, MODULE_NAME_LEN); | |
617 | list_add_tail(&req->list, &net->nft.module_list); | |
618 | ||
619 | return -EAGAIN; | |
452238e8 FW |
620 | } |
621 | #endif | |
622 | ||
f102d66b FW |
623 | static void lockdep_nfnl_nft_mutex_not_held(void) |
624 | { | |
625 | #ifdef CONFIG_PROVE_LOCKING | |
626 | WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES)); | |
627 | #endif | |
628 | } | |
629 | ||
32537e91 | 630 | static const struct nft_chain_type * |
452238e8 FW |
631 | nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla, |
632 | u8 family, bool autoload) | |
9370761c | 633 | { |
32537e91 | 634 | const struct nft_chain_type *type; |
9370761c | 635 | |
1ea26cca | 636 | type = __nf_tables_chain_type_lookup(nla, family); |
93b0806f PM |
637 | if (type != NULL) |
638 | return type; | |
f102d66b FW |
639 | |
640 | lockdep_nfnl_nft_mutex_not_held(); | |
9370761c | 641 | #ifdef CONFIG_MODULES |
93b0806f | 642 | if (autoload) { |
eb014de4 PNA |
643 | if (nft_request_module(net, "nft-chain-%u-%.*s", family, |
644 | nla_len(nla), | |
645 | (const char *)nla_data(nla)) == -EAGAIN) | |
93b0806f | 646 | return ERR_PTR(-EAGAIN); |
9370761c PNA |
647 | } |
648 | #endif | |
93b0806f | 649 | return ERR_PTR(-ENOENT); |
9370761c PNA |
650 | } |
651 | ||
96518518 | 652 | static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { |
1cae565e PNA |
653 | [NFTA_TABLE_NAME] = { .type = NLA_STRING, |
654 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
9ddf6323 | 655 | [NFTA_TABLE_FLAGS] = { .type = NLA_U32 }, |
3ecbfd65 | 656 | [NFTA_TABLE_HANDLE] = { .type = NLA_U64 }, |
96518518 PM |
657 | }; |
658 | ||
84d7fce6 PNA |
659 | static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, |
660 | u32 portid, u32 seq, int event, u32 flags, | |
661 | int family, const struct nft_table *table) | |
96518518 PM |
662 | { |
663 | struct nlmsghdr *nlh; | |
664 | struct nfgenmsg *nfmsg; | |
665 | ||
dedb67c4 | 666 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 PM |
667 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); |
668 | if (nlh == NULL) | |
669 | goto nla_put_failure; | |
670 | ||
671 | nfmsg = nlmsg_data(nlh); | |
672 | nfmsg->nfgen_family = family; | |
673 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 674 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
96518518 | 675 | |
9ddf6323 | 676 | if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) || |
d8bcc768 | 677 | nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) || |
3ecbfd65 HS |
678 | nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) || |
679 | nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle), | |
680 | NFTA_TABLE_PAD)) | |
96518518 PM |
681 | goto nla_put_failure; |
682 | ||
053c095a JB |
683 | nlmsg_end(skb, nlh); |
684 | return 0; | |
96518518 PM |
685 | |
686 | nla_put_failure: | |
687 | nlmsg_trim(skb, nlh); | |
688 | return -1; | |
689 | } | |
690 | ||
25e94a99 | 691 | static void nf_tables_table_notify(const struct nft_ctx *ctx, int event) |
96518518 PM |
692 | { |
693 | struct sk_buff *skb; | |
96518518 PM |
694 | int err; |
695 | ||
128ad332 PNA |
696 | if (!ctx->report && |
697 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 698 | return; |
96518518 | 699 | |
96518518 PM |
700 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
701 | if (skb == NULL) | |
702 | goto err; | |
703 | ||
84d7fce6 | 704 | err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq, |
36596dad | 705 | event, 0, ctx->family, ctx->table); |
96518518 PM |
706 | if (err < 0) { |
707 | kfree_skb(skb); | |
708 | goto err; | |
709 | } | |
710 | ||
25e94a99 PNA |
711 | nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
712 | ctx->report, GFP_KERNEL); | |
713 | return; | |
96518518 | 714 | err: |
25e94a99 | 715 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
716 | } |
717 | ||
718 | static int nf_tables_dump_tables(struct sk_buff *skb, | |
719 | struct netlink_callback *cb) | |
720 | { | |
721 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
96518518 PM |
722 | const struct nft_table *table; |
723 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 724 | struct net *net = sock_net(skb->sk); |
96518518 PM |
725 | int family = nfmsg->nfgen_family; |
726 | ||
e688a7f8 | 727 | rcu_read_lock(); |
38e029f1 PNA |
728 | cb->seq = net->nft.base_seq; |
729 | ||
36596dad | 730 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 731 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
732 | continue; |
733 | ||
36596dad PNA |
734 | if (idx < s_idx) |
735 | goto cont; | |
736 | if (idx > s_idx) | |
737 | memset(&cb->args[1], 0, | |
738 | sizeof(cb->args) - sizeof(cb->args[0])); | |
739 | if (!nft_is_active(net, table)) | |
740 | continue; | |
741 | if (nf_tables_fill_table_info(skb, net, | |
742 | NETLINK_CB(cb->skb).portid, | |
743 | cb->nlh->nlmsg_seq, | |
744 | NFT_MSG_NEWTABLE, NLM_F_MULTI, | |
98319cb9 | 745 | table->family, table) < 0) |
36596dad PNA |
746 | goto done; |
747 | ||
748 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
96518518 | 749 | cont: |
36596dad | 750 | idx++; |
96518518 PM |
751 | } |
752 | done: | |
e688a7f8 | 753 | rcu_read_unlock(); |
96518518 PM |
754 | cb->args[0] = idx; |
755 | return skb->len; | |
756 | } | |
757 | ||
d9adf22a FW |
758 | static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb, |
759 | const struct nlmsghdr *nlh, | |
760 | struct netlink_dump_control *c) | |
761 | { | |
762 | int err; | |
763 | ||
764 | if (!try_module_get(THIS_MODULE)) | |
765 | return -EINVAL; | |
766 | ||
767 | rcu_read_unlock(); | |
768 | err = netlink_dump_start(nlsk, skb, nlh, c); | |
769 | rcu_read_lock(); | |
770 | module_put(THIS_MODULE); | |
771 | ||
772 | return err; | |
773 | } | |
774 | ||
775 | /* called with rcu_read_lock held */ | |
7b8002a1 PNA |
776 | static int nf_tables_gettable(struct net *net, struct sock *nlsk, |
777 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
778 | const struct nlattr * const nla[], |
779 | struct netlink_ext_ack *extack) | |
96518518 PM |
780 | { |
781 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 782 | u8 genmask = nft_genmask_cur(net); |
96518518 PM |
783 | const struct nft_table *table; |
784 | struct sk_buff *skb2; | |
785 | int family = nfmsg->nfgen_family; | |
786 | int err; | |
787 | ||
788 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
789 | struct netlink_dump_control c = { | |
790 | .dump = nf_tables_dump_tables, | |
d9adf22a | 791 | .module = THIS_MODULE, |
96518518 | 792 | }; |
d9adf22a FW |
793 | |
794 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); | |
96518518 PM |
795 | } |
796 | ||
cac20fcd | 797 | table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask); |
36dd1bcc PNA |
798 | if (IS_ERR(table)) { |
799 | NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]); | |
96518518 | 800 | return PTR_ERR(table); |
36dd1bcc | 801 | } |
96518518 | 802 | |
d9adf22a | 803 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
804 | if (!skb2) |
805 | return -ENOMEM; | |
806 | ||
84d7fce6 | 807 | err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid, |
96518518 PM |
808 | nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0, |
809 | family, table); | |
810 | if (err < 0) | |
811 | goto err; | |
812 | ||
813 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
814 | ||
815 | err: | |
816 | kfree_skb(skb2); | |
817 | return err; | |
818 | } | |
819 | ||
c9c17211 | 820 | static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt) |
10435c11 F |
821 | { |
822 | struct nft_chain *chain; | |
823 | u32 i = 0; | |
824 | ||
825 | list_for_each_entry(chain, &table->chains, list) { | |
826 | if (!nft_is_active_next(net, chain)) | |
827 | continue; | |
f323d954 | 828 | if (!nft_is_base_chain(chain)) |
10435c11 F |
829 | continue; |
830 | ||
831 | if (cnt && i++ == cnt) | |
832 | break; | |
833 | ||
d54725cd PNA |
834 | nft_unregister_basechain_hooks(net, table->family, |
835 | nft_base_chain(chain)); | |
10435c11 F |
836 | } |
837 | } | |
838 | ||
c9c17211 | 839 | static int nf_tables_table_enable(struct net *net, struct nft_table *table) |
9ddf6323 PNA |
840 | { |
841 | struct nft_chain *chain; | |
842 | int err, i = 0; | |
843 | ||
844 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
845 | if (!nft_is_active_next(net, chain)) |
846 | continue; | |
f323d954 | 847 | if (!nft_is_base_chain(chain)) |
d2012975 PNA |
848 | continue; |
849 | ||
d54725cd PNA |
850 | err = nft_register_basechain_hooks(net, table->family, |
851 | nft_base_chain(chain)); | |
9ddf6323 | 852 | if (err < 0) |
d54725cd | 853 | goto err_register_hooks; |
9ddf6323 PNA |
854 | |
855 | i++; | |
856 | } | |
857 | return 0; | |
d54725cd PNA |
858 | |
859 | err_register_hooks: | |
10435c11 | 860 | if (i) |
c9c17211 | 861 | nft_table_disable(net, table, i); |
9ddf6323 PNA |
862 | return err; |
863 | } | |
864 | ||
c9c17211 | 865 | static void nf_tables_table_disable(struct net *net, struct nft_table *table) |
9ddf6323 | 866 | { |
c9c17211 | 867 | nft_table_disable(net, table, 0); |
9ddf6323 PNA |
868 | } |
869 | ||
e1aaca93 | 870 | static int nf_tables_updtable(struct nft_ctx *ctx) |
9ddf6323 | 871 | { |
55dd6f93 | 872 | struct nft_trans *trans; |
e1aaca93 | 873 | u32 flags; |
55dd6f93 | 874 | int ret = 0; |
9ddf6323 | 875 | |
e1aaca93 PNA |
876 | if (!ctx->nla[NFTA_TABLE_FLAGS]) |
877 | return 0; | |
9ddf6323 | 878 | |
e1aaca93 PNA |
879 | flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS])); |
880 | if (flags & ~NFT_TABLE_F_DORMANT) | |
881 | return -EINVAL; | |
882 | ||
63283dd2 PNA |
883 | if (flags == ctx->table->flags) |
884 | return 0; | |
885 | ||
55dd6f93 PNA |
886 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE, |
887 | sizeof(struct nft_trans_table)); | |
888 | if (trans == NULL) | |
889 | return -ENOMEM; | |
9ddf6323 | 890 | |
e1aaca93 PNA |
891 | if ((flags & NFT_TABLE_F_DORMANT) && |
892 | !(ctx->table->flags & NFT_TABLE_F_DORMANT)) { | |
55dd6f93 | 893 | nft_trans_table_enable(trans) = false; |
e1aaca93 PNA |
894 | } else if (!(flags & NFT_TABLE_F_DORMANT) && |
895 | ctx->table->flags & NFT_TABLE_F_DORMANT) { | |
c9c17211 | 896 | ret = nf_tables_table_enable(ctx->net, ctx->table); |
55dd6f93 | 897 | if (ret >= 0) { |
e1aaca93 | 898 | ctx->table->flags &= ~NFT_TABLE_F_DORMANT; |
55dd6f93 | 899 | nft_trans_table_enable(trans) = true; |
9ddf6323 | 900 | } |
9ddf6323 | 901 | } |
e1aaca93 PNA |
902 | if (ret < 0) |
903 | goto err; | |
9ddf6323 | 904 | |
55dd6f93 PNA |
905 | nft_trans_table_update(trans) = true; |
906 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
907 | return 0; | |
9ddf6323 | 908 | err: |
55dd6f93 | 909 | nft_trans_destroy(trans); |
9ddf6323 PNA |
910 | return ret; |
911 | } | |
912 | ||
1b2470e5 FW |
913 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed) |
914 | { | |
915 | const char *name = data; | |
916 | ||
917 | return jhash(name, strlen(name), seed); | |
918 | } | |
919 | ||
920 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed) | |
921 | { | |
922 | const struct nft_chain *chain = data; | |
923 | ||
924 | return nft_chain_hash(chain->name, 0, seed); | |
925 | } | |
926 | ||
927 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg, | |
928 | const void *ptr) | |
929 | { | |
930 | const struct nft_chain *chain = ptr; | |
931 | const char *name = arg->key; | |
932 | ||
933 | return strcmp(chain->name, name); | |
934 | } | |
935 | ||
4d44175a FW |
936 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed) |
937 | { | |
938 | const struct nft_object_hash_key *k = data; | |
939 | ||
940 | seed ^= hash_ptr(k->table, 32); | |
941 | ||
942 | return jhash(k->name, strlen(k->name), seed); | |
943 | } | |
944 | ||
945 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed) | |
946 | { | |
947 | const struct nft_object *obj = data; | |
948 | ||
949 | return nft_objname_hash(&obj->key, 0, seed); | |
950 | } | |
951 | ||
952 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg, | |
953 | const void *ptr) | |
954 | { | |
955 | const struct nft_object_hash_key *k = arg->key; | |
956 | const struct nft_object *obj = ptr; | |
957 | ||
958 | if (obj->key.table != k->table) | |
959 | return -1; | |
960 | ||
961 | return strcmp(obj->key.name, k->name); | |
962 | } | |
963 | ||
633c9a84 PNA |
964 | static int nf_tables_newtable(struct net *net, struct sock *nlsk, |
965 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
966 | const struct nlattr * const nla[], |
967 | struct netlink_ext_ack *extack) | |
96518518 PM |
968 | { |
969 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 970 | u8 genmask = nft_genmask_next(net); |
96518518 | 971 | int family = nfmsg->nfgen_family; |
36dd1bcc PNA |
972 | const struct nlattr *attr; |
973 | struct nft_table *table; | |
c5c1f975 | 974 | u32 flags = 0; |
e1aaca93 | 975 | struct nft_ctx ctx; |
55dd6f93 | 976 | int err; |
96518518 | 977 | |
f102d66b | 978 | lockdep_assert_held(&net->nft.commit_mutex); |
36dd1bcc PNA |
979 | attr = nla[NFTA_TABLE_NAME]; |
980 | table = nft_table_lookup(net, attr, family, genmask); | |
96518518 PM |
981 | if (IS_ERR(table)) { |
982 | if (PTR_ERR(table) != -ENOENT) | |
983 | return PTR_ERR(table); | |
1a28ad74 | 984 | } else { |
36dd1bcc PNA |
985 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
986 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 987 | return -EEXIST; |
36dd1bcc | 988 | } |
96518518 PM |
989 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
990 | return -EOPNOTSUPP; | |
e1aaca93 | 991 | |
98319cb9 | 992 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
e1aaca93 | 993 | return nf_tables_updtable(&ctx); |
96518518 PM |
994 | } |
995 | ||
c5c1f975 PM |
996 | if (nla[NFTA_TABLE_FLAGS]) { |
997 | flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS])); | |
998 | if (flags & ~NFT_TABLE_F_DORMANT) | |
999 | return -EINVAL; | |
1000 | } | |
1001 | ||
ffdb210e | 1002 | err = -ENOMEM; |
1cae565e | 1003 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
ffdb210e | 1004 | if (table == NULL) |
98319cb9 | 1005 | goto err_kzalloc; |
96518518 | 1006 | |
36dd1bcc | 1007 | table->name = nla_strdup(attr, GFP_KERNEL); |
e46abbcc | 1008 | if (table->name == NULL) |
98319cb9 | 1009 | goto err_strdup; |
e46abbcc | 1010 | |
1b2470e5 FW |
1011 | err = rhltable_init(&table->chains_ht, &nft_chain_ht_params); |
1012 | if (err) | |
1013 | goto err_chain_ht; | |
1014 | ||
96518518 | 1015 | INIT_LIST_HEAD(&table->chains); |
20a69341 | 1016 | INIT_LIST_HEAD(&table->sets); |
e5009240 | 1017 | INIT_LIST_HEAD(&table->objects); |
3b49e2e9 | 1018 | INIT_LIST_HEAD(&table->flowtables); |
98319cb9 | 1019 | table->family = family; |
c5c1f975 | 1020 | table->flags = flags; |
3ecbfd65 | 1021 | table->handle = ++table_handle; |
9ddf6323 | 1022 | |
98319cb9 | 1023 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
55dd6f93 | 1024 | err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE); |
ffdb210e | 1025 | if (err < 0) |
98319cb9 | 1026 | goto err_trans; |
ffdb210e | 1027 | |
36596dad | 1028 | list_add_tail_rcu(&table->list, &net->nft.tables); |
96518518 | 1029 | return 0; |
98319cb9 | 1030 | err_trans: |
1b2470e5 FW |
1031 | rhltable_destroy(&table->chains_ht); |
1032 | err_chain_ht: | |
e46abbcc | 1033 | kfree(table->name); |
98319cb9 | 1034 | err_strdup: |
ffdb210e | 1035 | kfree(table); |
98319cb9 | 1036 | err_kzalloc: |
ffdb210e | 1037 | return err; |
96518518 PM |
1038 | } |
1039 | ||
b9ac12ef AB |
1040 | static int nft_flush_table(struct nft_ctx *ctx) |
1041 | { | |
3b49e2e9 | 1042 | struct nft_flowtable *flowtable, *nft; |
b9ac12ef | 1043 | struct nft_chain *chain, *nc; |
e5009240 | 1044 | struct nft_object *obj, *ne; |
b9ac12ef | 1045 | struct nft_set *set, *ns; |
3b49e2e9 | 1046 | int err; |
b9ac12ef | 1047 | |
a2f18db0 | 1048 | list_for_each_entry(chain, &ctx->table->chains, list) { |
664b0f8c PNA |
1049 | if (!nft_is_active_next(ctx->net, chain)) |
1050 | continue; | |
1051 | ||
b9ac12ef AB |
1052 | ctx->chain = chain; |
1053 | ||
1054 | err = nft_delrule_by_chain(ctx); | |
1055 | if (err < 0) | |
1056 | goto out; | |
b9ac12ef AB |
1057 | } |
1058 | ||
1059 | list_for_each_entry_safe(set, ns, &ctx->table->sets, list) { | |
37a9cc52 PNA |
1060 | if (!nft_is_active_next(ctx->net, set)) |
1061 | continue; | |
1062 | ||
408070d6 | 1063 | if (nft_set_is_anonymous(set) && |
b9ac12ef AB |
1064 | !list_empty(&set->bindings)) |
1065 | continue; | |
1066 | ||
1067 | err = nft_delset(ctx, set); | |
1068 | if (err < 0) | |
1069 | goto out; | |
1070 | } | |
1071 | ||
3b49e2e9 | 1072 | list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) { |
335178d5 FW |
1073 | if (!nft_is_active_next(ctx->net, flowtable)) |
1074 | continue; | |
1075 | ||
3b49e2e9 PNA |
1076 | err = nft_delflowtable(ctx, flowtable); |
1077 | if (err < 0) | |
1078 | goto out; | |
1079 | } | |
1080 | ||
e5009240 | 1081 | list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) { |
335178d5 FW |
1082 | if (!nft_is_active_next(ctx->net, obj)) |
1083 | continue; | |
1084 | ||
e5009240 PNA |
1085 | err = nft_delobj(ctx, obj); |
1086 | if (err < 0) | |
1087 | goto out; | |
1088 | } | |
1089 | ||
a2f18db0 | 1090 | list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) { |
664b0f8c PNA |
1091 | if (!nft_is_active_next(ctx->net, chain)) |
1092 | continue; | |
1093 | ||
a2f18db0 PNA |
1094 | ctx->chain = chain; |
1095 | ||
1096 | err = nft_delchain(ctx); | |
1097 | if (err < 0) | |
1098 | goto out; | |
1099 | } | |
1100 | ||
b9ac12ef AB |
1101 | err = nft_deltable(ctx); |
1102 | out: | |
1103 | return err; | |
1104 | } | |
1105 | ||
1106 | static int nft_flush(struct nft_ctx *ctx, int family) | |
1107 | { | |
b9ac12ef AB |
1108 | struct nft_table *table, *nt; |
1109 | const struct nlattr * const *nla = ctx->nla; | |
1110 | int err = 0; | |
1111 | ||
36596dad | 1112 | list_for_each_entry_safe(table, nt, &ctx->net->nft.tables, list) { |
98319cb9 | 1113 | if (family != AF_UNSPEC && table->family != family) |
b9ac12ef AB |
1114 | continue; |
1115 | ||
98319cb9 | 1116 | ctx->family = table->family; |
f2a6d766 | 1117 | |
36596dad PNA |
1118 | if (!nft_is_active_next(ctx->net, table)) |
1119 | continue; | |
b9ac12ef | 1120 | |
36596dad PNA |
1121 | if (nla[NFTA_TABLE_NAME] && |
1122 | nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0) | |
1123 | continue; | |
b9ac12ef | 1124 | |
36596dad PNA |
1125 | ctx->table = table; |
1126 | ||
1127 | err = nft_flush_table(ctx); | |
1128 | if (err < 0) | |
1129 | goto out; | |
b9ac12ef AB |
1130 | } |
1131 | out: | |
1132 | return err; | |
1133 | } | |
1134 | ||
633c9a84 PNA |
1135 | static int nf_tables_deltable(struct net *net, struct sock *nlsk, |
1136 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
1137 | const struct nlattr * const nla[], |
1138 | struct netlink_ext_ack *extack) | |
96518518 PM |
1139 | { |
1140 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 1141 | u8 genmask = nft_genmask_next(net); |
ee01d542 | 1142 | int family = nfmsg->nfgen_family; |
36dd1bcc PNA |
1143 | const struct nlattr *attr; |
1144 | struct nft_table *table; | |
55dd6f93 | 1145 | struct nft_ctx ctx; |
96518518 | 1146 | |
36596dad | 1147 | nft_ctx_init(&ctx, net, skb, nlh, 0, NULL, NULL, nla); |
3ecbfd65 HS |
1148 | if (family == AF_UNSPEC || |
1149 | (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE])) | |
b9ac12ef AB |
1150 | return nft_flush(&ctx, family); |
1151 | ||
36dd1bcc PNA |
1152 | if (nla[NFTA_TABLE_HANDLE]) { |
1153 | attr = nla[NFTA_TABLE_HANDLE]; | |
1154 | table = nft_table_lookup_byhandle(net, attr, genmask); | |
1155 | } else { | |
1156 | attr = nla[NFTA_TABLE_NAME]; | |
1157 | table = nft_table_lookup(net, attr, family, genmask); | |
1158 | } | |
3ecbfd65 | 1159 | |
36dd1bcc PNA |
1160 | if (IS_ERR(table)) { |
1161 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 1162 | return PTR_ERR(table); |
36dd1bcc | 1163 | } |
96518518 | 1164 | |
a8278400 PNA |
1165 | if (nlh->nlmsg_flags & NLM_F_NONREC && |
1166 | table->use > 0) | |
1167 | return -EBUSY; | |
1168 | ||
98319cb9 | 1169 | ctx.family = family; |
b9ac12ef | 1170 | ctx.table = table; |
55dd6f93 | 1171 | |
b9ac12ef | 1172 | return nft_flush_table(&ctx); |
96518518 PM |
1173 | } |
1174 | ||
55dd6f93 PNA |
1175 | static void nf_tables_table_destroy(struct nft_ctx *ctx) |
1176 | { | |
fa5950e4 FW |
1177 | if (WARN_ON(ctx->table->use > 0)) |
1178 | return; | |
4fefee57 | 1179 | |
1b2470e5 | 1180 | rhltable_destroy(&ctx->table->chains_ht); |
e46abbcc | 1181 | kfree(ctx->table->name); |
55dd6f93 | 1182 | kfree(ctx->table); |
55dd6f93 PNA |
1183 | } |
1184 | ||
cc07eeb0 | 1185 | void nft_register_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1186 | { |
96518518 | 1187 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
82603549 | 1188 | if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) { |
cc07eeb0 PNA |
1189 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1190 | return; | |
96518518 | 1191 | } |
9370761c | 1192 | chain_type[ctype->family][ctype->type] = ctype; |
96518518 | 1193 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
96518518 | 1194 | } |
9370761c | 1195 | EXPORT_SYMBOL_GPL(nft_register_chain_type); |
96518518 | 1196 | |
32537e91 | 1197 | void nft_unregister_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1198 | { |
96518518 | 1199 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
9370761c | 1200 | chain_type[ctype->family][ctype->type] = NULL; |
96518518 PM |
1201 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1202 | } | |
9370761c | 1203 | EXPORT_SYMBOL_GPL(nft_unregister_chain_type); |
96518518 PM |
1204 | |
1205 | /* | |
1206 | * Chains | |
1207 | */ | |
1208 | ||
1209 | static struct nft_chain * | |
cac20fcd | 1210 | nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask) |
96518518 PM |
1211 | { |
1212 | struct nft_chain *chain; | |
1213 | ||
1214 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1215 | if (chain->handle == handle && |
1216 | nft_active_genmask(chain, genmask)) | |
96518518 PM |
1217 | return chain; |
1218 | } | |
1219 | ||
1220 | return ERR_PTR(-ENOENT); | |
1221 | } | |
1222 | ||
4d44175a | 1223 | static bool lockdep_commit_lock_is_held(const struct net *net) |
f102d66b FW |
1224 | { |
1225 | #ifdef CONFIG_PROVE_LOCKING | |
1226 | return lockdep_is_held(&net->nft.commit_mutex); | |
1227 | #else | |
1228 | return true; | |
1229 | #endif | |
1230 | } | |
1231 | ||
1232 | static struct nft_chain *nft_chain_lookup(struct net *net, | |
1233 | struct nft_table *table, | |
cac20fcd | 1234 | const struct nlattr *nla, u8 genmask) |
96518518 | 1235 | { |
1b2470e5 FW |
1236 | char search[NFT_CHAIN_MAXNAMELEN + 1]; |
1237 | struct rhlist_head *tmp, *list; | |
96518518 PM |
1238 | struct nft_chain *chain; |
1239 | ||
1240 | if (nla == NULL) | |
1241 | return ERR_PTR(-EINVAL); | |
1242 | ||
1b2470e5 | 1243 | nla_strlcpy(search, nla, sizeof(search)); |
96518518 | 1244 | |
1b2470e5 | 1245 | WARN_ON(!rcu_read_lock_held() && |
f102d66b | 1246 | !lockdep_commit_lock_is_held(net)); |
1b2470e5 FW |
1247 | |
1248 | chain = ERR_PTR(-ENOENT); | |
1249 | rcu_read_lock(); | |
1250 | list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params); | |
1251 | if (!list) | |
1252 | goto out_unlock; | |
1253 | ||
1254 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
1255 | if (nft_active_genmask(chain, genmask)) | |
1256 | goto out_unlock; | |
1257 | } | |
1258 | chain = ERR_PTR(-ENOENT); | |
1259 | out_unlock: | |
1260 | rcu_read_unlock(); | |
1261 | return chain; | |
96518518 PM |
1262 | } |
1263 | ||
1264 | static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { | |
b2fbd044 LZ |
1265 | [NFTA_CHAIN_TABLE] = { .type = NLA_STRING, |
1266 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
1267 | [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 }, |
1268 | [NFTA_CHAIN_NAME] = { .type = NLA_STRING, | |
1269 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
1270 | [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED }, | |
0ca743a5 | 1271 | [NFTA_CHAIN_POLICY] = { .type = NLA_U32 }, |
9332d27d FW |
1272 | [NFTA_CHAIN_TYPE] = { .type = NLA_STRING, |
1273 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
0ca743a5 | 1274 | [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, |
c9626a2c | 1275 | [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 }, |
96518518 PM |
1276 | }; |
1277 | ||
1278 | static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = { | |
1279 | [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 }, | |
1280 | [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
2cbce139 PNA |
1281 | [NFTA_HOOK_DEV] = { .type = NLA_STRING, |
1282 | .len = IFNAMSIZ - 1 }, | |
96518518 PM |
1283 | }; |
1284 | ||
0ca743a5 PNA |
1285 | static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats) |
1286 | { | |
1287 | struct nft_stats *cpu_stats, total; | |
1288 | struct nlattr *nest; | |
ce355e20 ED |
1289 | unsigned int seq; |
1290 | u64 pkts, bytes; | |
0ca743a5 PNA |
1291 | int cpu; |
1292 | ||
edbd82c5 FW |
1293 | if (!stats) |
1294 | return 0; | |
1295 | ||
0ca743a5 PNA |
1296 | memset(&total, 0, sizeof(total)); |
1297 | for_each_possible_cpu(cpu) { | |
1298 | cpu_stats = per_cpu_ptr(stats, cpu); | |
ce355e20 ED |
1299 | do { |
1300 | seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp); | |
1301 | pkts = cpu_stats->pkts; | |
1302 | bytes = cpu_stats->bytes; | |
1303 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq)); | |
1304 | total.pkts += pkts; | |
1305 | total.bytes += bytes; | |
0ca743a5 | 1306 | } |
ae0be8de | 1307 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS); |
0ca743a5 PNA |
1308 | if (nest == NULL) |
1309 | goto nla_put_failure; | |
1310 | ||
b46f6ded ND |
1311 | if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts), |
1312 | NFTA_COUNTER_PAD) || | |
1313 | nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes), | |
1314 | NFTA_COUNTER_PAD)) | |
0ca743a5 PNA |
1315 | goto nla_put_failure; |
1316 | ||
1317 | nla_nest_end(skb, nest); | |
1318 | return 0; | |
1319 | ||
1320 | nla_put_failure: | |
1321 | return -ENOSPC; | |
1322 | } | |
1323 | ||
d54725cd PNA |
1324 | static int nft_dump_basechain_hook(struct sk_buff *skb, int family, |
1325 | const struct nft_base_chain *basechain) | |
1326 | { | |
1327 | const struct nf_hook_ops *ops = &basechain->ops; | |
1328 | struct nft_hook *hook, *first = NULL; | |
1329 | struct nlattr *nest, *nest_devs; | |
1330 | int n = 0; | |
1331 | ||
1332 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK); | |
1333 | if (nest == NULL) | |
1334 | goto nla_put_failure; | |
1335 | if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum))) | |
1336 | goto nla_put_failure; | |
1337 | if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority))) | |
1338 | goto nla_put_failure; | |
1339 | ||
1340 | if (family == NFPROTO_NETDEV) { | |
1341 | nest_devs = nla_nest_start_noflag(skb, NFTA_HOOK_DEVS); | |
1342 | list_for_each_entry(hook, &basechain->hook_list, list) { | |
1343 | if (!first) | |
1344 | first = hook; | |
1345 | ||
1346 | if (nla_put_string(skb, NFTA_DEVICE_NAME, | |
1347 | hook->ops.dev->name)) | |
1348 | goto nla_put_failure; | |
1349 | n++; | |
1350 | } | |
1351 | nla_nest_end(skb, nest_devs); | |
1352 | ||
1353 | if (n == 1 && | |
1354 | nla_put_string(skb, NFTA_HOOK_DEV, first->ops.dev->name)) | |
1355 | goto nla_put_failure; | |
1356 | } | |
1357 | nla_nest_end(skb, nest); | |
1358 | ||
1359 | return 0; | |
1360 | nla_put_failure: | |
1361 | return -1; | |
1362 | } | |
1363 | ||
84d7fce6 PNA |
1364 | static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net, |
1365 | u32 portid, u32 seq, int event, u32 flags, | |
1366 | int family, const struct nft_table *table, | |
96518518 PM |
1367 | const struct nft_chain *chain) |
1368 | { | |
1369 | struct nlmsghdr *nlh; | |
1370 | struct nfgenmsg *nfmsg; | |
1371 | ||
dedb67c4 | 1372 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 PM |
1373 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); |
1374 | if (nlh == NULL) | |
1375 | goto nla_put_failure; | |
1376 | ||
1377 | nfmsg = nlmsg_data(nlh); | |
1378 | nfmsg->nfgen_family = family; | |
1379 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 1380 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
96518518 PM |
1381 | |
1382 | if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name)) | |
1383 | goto nla_put_failure; | |
b46f6ded ND |
1384 | if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle), |
1385 | NFTA_CHAIN_PAD)) | |
96518518 PM |
1386 | goto nla_put_failure; |
1387 | if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name)) | |
1388 | goto nla_put_failure; | |
1389 | ||
f323d954 | 1390 | if (nft_is_base_chain(chain)) { |
0ca743a5 | 1391 | const struct nft_base_chain *basechain = nft_base_chain(chain); |
edbd82c5 | 1392 | struct nft_stats __percpu *stats; |
0ca743a5 | 1393 | |
d54725cd | 1394 | if (nft_dump_basechain_hook(skb, family, basechain)) |
2cbce139 | 1395 | goto nla_put_failure; |
9370761c | 1396 | |
0ca743a5 PNA |
1397 | if (nla_put_be32(skb, NFTA_CHAIN_POLICY, |
1398 | htonl(basechain->policy))) | |
1399 | goto nla_put_failure; | |
1400 | ||
baae3e62 PM |
1401 | if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name)) |
1402 | goto nla_put_failure; | |
0ca743a5 | 1403 | |
edbd82c5 FW |
1404 | stats = rcu_dereference_check(basechain->stats, |
1405 | lockdep_commit_lock_is_held(net)); | |
1406 | if (nft_dump_stats(skb, stats)) | |
0ca743a5 | 1407 | goto nla_put_failure; |
d78008de PNA |
1408 | |
1409 | if ((chain->flags & NFT_CHAIN_HW_OFFLOAD) && | |
1410 | nla_put_be32(skb, NFTA_CHAIN_FLAGS, | |
1411 | htonl(NFT_CHAIN_HW_OFFLOAD))) | |
1412 | goto nla_put_failure; | |
96518518 PM |
1413 | } |
1414 | ||
0ca743a5 PNA |
1415 | if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use))) |
1416 | goto nla_put_failure; | |
1417 | ||
053c095a JB |
1418 | nlmsg_end(skb, nlh); |
1419 | return 0; | |
96518518 PM |
1420 | |
1421 | nla_put_failure: | |
1422 | nlmsg_trim(skb, nlh); | |
1423 | return -1; | |
1424 | } | |
1425 | ||
25e94a99 | 1426 | static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event) |
96518518 PM |
1427 | { |
1428 | struct sk_buff *skb; | |
96518518 PM |
1429 | int err; |
1430 | ||
128ad332 PNA |
1431 | if (!ctx->report && |
1432 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 1433 | return; |
96518518 | 1434 | |
96518518 PM |
1435 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
1436 | if (skb == NULL) | |
1437 | goto err; | |
1438 | ||
84d7fce6 | 1439 | err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq, |
36596dad | 1440 | event, 0, ctx->family, ctx->table, |
35151d84 | 1441 | ctx->chain); |
96518518 PM |
1442 | if (err < 0) { |
1443 | kfree_skb(skb); | |
1444 | goto err; | |
1445 | } | |
1446 | ||
25e94a99 PNA |
1447 | nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
1448 | ctx->report, GFP_KERNEL); | |
1449 | return; | |
96518518 | 1450 | err: |
25e94a99 | 1451 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
1452 | } |
1453 | ||
1454 | static int nf_tables_dump_chains(struct sk_buff *skb, | |
1455 | struct netlink_callback *cb) | |
1456 | { | |
1457 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
96518518 PM |
1458 | const struct nft_table *table; |
1459 | const struct nft_chain *chain; | |
1460 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 1461 | struct net *net = sock_net(skb->sk); |
96518518 PM |
1462 | int family = nfmsg->nfgen_family; |
1463 | ||
e688a7f8 | 1464 | rcu_read_lock(); |
38e029f1 PNA |
1465 | cb->seq = net->nft.base_seq; |
1466 | ||
36596dad | 1467 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 1468 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
1469 | continue; |
1470 | ||
36596dad PNA |
1471 | list_for_each_entry_rcu(chain, &table->chains, list) { |
1472 | if (idx < s_idx) | |
1473 | goto cont; | |
1474 | if (idx > s_idx) | |
1475 | memset(&cb->args[1], 0, | |
1476 | sizeof(cb->args) - sizeof(cb->args[0])); | |
1477 | if (!nft_is_active(net, chain)) | |
1478 | continue; | |
1479 | if (nf_tables_fill_chain_info(skb, net, | |
1480 | NETLINK_CB(cb->skb).portid, | |
1481 | cb->nlh->nlmsg_seq, | |
1482 | NFT_MSG_NEWCHAIN, | |
1483 | NLM_F_MULTI, | |
98319cb9 | 1484 | table->family, table, |
36596dad PNA |
1485 | chain) < 0) |
1486 | goto done; | |
38e029f1 | 1487 | |
36596dad | 1488 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
96518518 | 1489 | cont: |
36596dad | 1490 | idx++; |
96518518 PM |
1491 | } |
1492 | } | |
1493 | done: | |
e688a7f8 | 1494 | rcu_read_unlock(); |
96518518 PM |
1495 | cb->args[0] = idx; |
1496 | return skb->len; | |
1497 | } | |
1498 | ||
d9adf22a | 1499 | /* called with rcu_read_lock held */ |
7b8002a1 PNA |
1500 | static int nf_tables_getchain(struct net *net, struct sock *nlsk, |
1501 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
1502 | const struct nlattr * const nla[], |
1503 | struct netlink_ext_ack *extack) | |
96518518 PM |
1504 | { |
1505 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 1506 | u8 genmask = nft_genmask_cur(net); |
96518518 | 1507 | const struct nft_chain *chain; |
1b2470e5 | 1508 | struct nft_table *table; |
96518518 PM |
1509 | struct sk_buff *skb2; |
1510 | int family = nfmsg->nfgen_family; | |
1511 | int err; | |
1512 | ||
1513 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1514 | struct netlink_dump_control c = { | |
1515 | .dump = nf_tables_dump_chains, | |
d9adf22a | 1516 | .module = THIS_MODULE, |
96518518 | 1517 | }; |
d9adf22a FW |
1518 | |
1519 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); | |
96518518 PM |
1520 | } |
1521 | ||
cac20fcd | 1522 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask); |
36dd1bcc PNA |
1523 | if (IS_ERR(table)) { |
1524 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 1525 | return PTR_ERR(table); |
36dd1bcc | 1526 | } |
96518518 | 1527 | |
f102d66b | 1528 | chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask); |
36dd1bcc PNA |
1529 | if (IS_ERR(chain)) { |
1530 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
96518518 | 1531 | return PTR_ERR(chain); |
36dd1bcc | 1532 | } |
96518518 | 1533 | |
d9adf22a | 1534 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
1535 | if (!skb2) |
1536 | return -ENOMEM; | |
1537 | ||
84d7fce6 | 1538 | err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid, |
96518518 PM |
1539 | nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0, |
1540 | family, table, chain); | |
1541 | if (err < 0) | |
1542 | goto err; | |
1543 | ||
1544 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
1545 | ||
1546 | err: | |
1547 | kfree_skb(skb2); | |
1548 | return err; | |
1549 | } | |
1550 | ||
0ca743a5 PNA |
1551 | static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { |
1552 | [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, | |
1553 | [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, | |
1554 | }; | |
1555 | ||
ff3cd7b3 | 1556 | static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr) |
0ca743a5 PNA |
1557 | { |
1558 | struct nlattr *tb[NFTA_COUNTER_MAX+1]; | |
1559 | struct nft_stats __percpu *newstats; | |
1560 | struct nft_stats *stats; | |
1561 | int err; | |
1562 | ||
8cb08174 JB |
1563 | err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr, |
1564 | nft_counter_policy, NULL); | |
0ca743a5 | 1565 | if (err < 0) |
ff3cd7b3 | 1566 | return ERR_PTR(err); |
0ca743a5 PNA |
1567 | |
1568 | if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS]) | |
ff3cd7b3 | 1569 | return ERR_PTR(-EINVAL); |
0ca743a5 | 1570 | |
ce355e20 | 1571 | newstats = netdev_alloc_pcpu_stats(struct nft_stats); |
0ca743a5 | 1572 | if (newstats == NULL) |
ff3cd7b3 | 1573 | return ERR_PTR(-ENOMEM); |
0ca743a5 PNA |
1574 | |
1575 | /* Restore old counters on this cpu, no problem. Per-cpu statistics | |
1576 | * are not exposed to userspace. | |
1577 | */ | |
e8781f70 | 1578 | preempt_disable(); |
0ca743a5 PNA |
1579 | stats = this_cpu_ptr(newstats); |
1580 | stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); | |
1581 | stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); | |
e8781f70 | 1582 | preempt_enable(); |
0ca743a5 | 1583 | |
ff3cd7b3 PNA |
1584 | return newstats; |
1585 | } | |
1586 | ||
53315ac6 | 1587 | static void nft_chain_stats_replace(struct nft_trans *trans) |
ff3cd7b3 | 1588 | { |
53315ac6 | 1589 | struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain); |
0befd061 | 1590 | |
53315ac6 | 1591 | if (!nft_trans_chain_stats(trans)) |
b88825de PNA |
1592 | return; |
1593 | ||
b685b534 PM |
1594 | nft_trans_chain_stats(trans) = |
1595 | rcu_replace_pointer(chain->stats, nft_trans_chain_stats(trans), | |
1596 | lockdep_commit_lock_is_held(trans->ctx.net)); | |
53315ac6 FW |
1597 | |
1598 | if (!nft_trans_chain_stats(trans)) | |
bbb8c61f | 1599 | static_branch_inc(&nft_counters_enabled); |
0ca743a5 PNA |
1600 | } |
1601 | ||
0cbc06b3 FW |
1602 | static void nf_tables_chain_free_chain_rules(struct nft_chain *chain) |
1603 | { | |
1604 | struct nft_rule **g0 = rcu_dereference_raw(chain->rules_gen_0); | |
1605 | struct nft_rule **g1 = rcu_dereference_raw(chain->rules_gen_1); | |
1606 | ||
1607 | if (g0 != g1) | |
1608 | kvfree(g1); | |
1609 | kvfree(g0); | |
1610 | ||
1611 | /* should be NULL either via abort or via successful commit */ | |
1612 | WARN_ON_ONCE(chain->rules_next); | |
1613 | kvfree(chain->rules_next); | |
1614 | } | |
1615 | ||
43a605f2 | 1616 | static void nf_tables_chain_destroy(struct nft_ctx *ctx) |
91c7b38d | 1617 | { |
43a605f2 | 1618 | struct nft_chain *chain = ctx->chain; |
d54725cd | 1619 | struct nft_hook *hook, *next; |
43a605f2 | 1620 | |
fa5950e4 FW |
1621 | if (WARN_ON(chain->use > 0)) |
1622 | return; | |
91c7b38d | 1623 | |
0cbc06b3 FW |
1624 | /* no concurrent access possible anymore */ |
1625 | nf_tables_chain_free_chain_rules(chain); | |
1626 | ||
f323d954 | 1627 | if (nft_is_base_chain(chain)) { |
2cbce139 PNA |
1628 | struct nft_base_chain *basechain = nft_base_chain(chain); |
1629 | ||
d54725cd PNA |
1630 | if (ctx->family == NFPROTO_NETDEV) { |
1631 | list_for_each_entry_safe(hook, next, | |
1632 | &basechain->hook_list, list) { | |
1633 | list_del_rcu(&hook->list); | |
1634 | kfree_rcu(hook, rcu); | |
1635 | } | |
1636 | } | |
2cbce139 | 1637 | module_put(basechain->type->owner); |
4c05ec47 | 1638 | if (rcu_access_pointer(basechain->stats)) { |
9f08ea84 | 1639 | static_branch_dec(&nft_counters_enabled); |
4c05ec47 TY |
1640 | free_percpu(rcu_dereference_raw(basechain->stats)); |
1641 | } | |
b7263e07 | 1642 | kfree(chain->name); |
2cbce139 | 1643 | kfree(basechain); |
91c7b38d | 1644 | } else { |
b7263e07 | 1645 | kfree(chain->name); |
91c7b38d PNA |
1646 | kfree(chain); |
1647 | } | |
1648 | } | |
1649 | ||
3f0465a9 PNA |
1650 | static struct nft_hook *nft_netdev_hook_alloc(struct net *net, |
1651 | const struct nlattr *attr) | |
1652 | { | |
1653 | struct net_device *dev; | |
1654 | char ifname[IFNAMSIZ]; | |
1655 | struct nft_hook *hook; | |
1656 | int err; | |
1657 | ||
1658 | hook = kmalloc(sizeof(struct nft_hook), GFP_KERNEL); | |
1659 | if (!hook) { | |
1660 | err = -ENOMEM; | |
1661 | goto err_hook_alloc; | |
1662 | } | |
1663 | ||
1664 | nla_strlcpy(ifname, attr, IFNAMSIZ); | |
1665 | dev = __dev_get_by_name(net, ifname); | |
1666 | if (!dev) { | |
1667 | err = -ENOENT; | |
1668 | goto err_hook_dev; | |
1669 | } | |
1670 | hook->ops.dev = dev; | |
1671 | ||
1672 | return hook; | |
1673 | ||
1674 | err_hook_dev: | |
1675 | kfree(hook); | |
1676 | err_hook_alloc: | |
1677 | return ERR_PTR(err); | |
1678 | } | |
1679 | ||
b75a3e83 PNA |
1680 | static bool nft_hook_list_find(struct list_head *hook_list, |
1681 | const struct nft_hook *this) | |
1682 | { | |
1683 | struct nft_hook *hook; | |
1684 | ||
1685 | list_for_each_entry(hook, hook_list, list) { | |
1686 | if (this->ops.dev == hook->ops.dev) | |
1687 | return true; | |
1688 | } | |
1689 | ||
1690 | return false; | |
1691 | } | |
1692 | ||
3f0465a9 PNA |
1693 | static int nf_tables_parse_netdev_hooks(struct net *net, |
1694 | const struct nlattr *attr, | |
1695 | struct list_head *hook_list) | |
1696 | { | |
1697 | struct nft_hook *hook, *next; | |
1698 | const struct nlattr *tmp; | |
1699 | int rem, n = 0, err; | |
1700 | ||
1701 | nla_for_each_nested(tmp, attr, rem) { | |
1702 | if (nla_type(tmp) != NFTA_DEVICE_NAME) { | |
1703 | err = -EINVAL; | |
1704 | goto err_hook; | |
1705 | } | |
1706 | ||
1707 | hook = nft_netdev_hook_alloc(net, tmp); | |
1708 | if (IS_ERR(hook)) { | |
1709 | err = PTR_ERR(hook); | |
1710 | goto err_hook; | |
1711 | } | |
b75a3e83 | 1712 | if (nft_hook_list_find(hook_list, hook)) { |
cd77e75b | 1713 | kfree(hook); |
b75a3e83 PNA |
1714 | err = -EEXIST; |
1715 | goto err_hook; | |
1716 | } | |
3f0465a9 PNA |
1717 | list_add_tail(&hook->list, hook_list); |
1718 | n++; | |
1719 | ||
cb662ac6 | 1720 | if (n == NFT_NETDEVICE_MAX) { |
3f0465a9 PNA |
1721 | err = -EFBIG; |
1722 | goto err_hook; | |
1723 | } | |
1724 | } | |
1725 | if (!n) | |
1726 | return -EINVAL; | |
1727 | ||
1728 | return 0; | |
1729 | ||
1730 | err_hook: | |
1731 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
1732 | list_del(&hook->list); | |
1733 | kfree(hook); | |
1734 | } | |
1735 | return err; | |
1736 | } | |
1737 | ||
508f8ccd PNA |
1738 | struct nft_chain_hook { |
1739 | u32 num; | |
84ba7dd7 | 1740 | s32 priority; |
32537e91 | 1741 | const struct nft_chain_type *type; |
d54725cd | 1742 | struct list_head list; |
508f8ccd PNA |
1743 | }; |
1744 | ||
d54725cd PNA |
1745 | static int nft_chain_parse_netdev(struct net *net, |
1746 | struct nlattr *tb[], | |
1747 | struct list_head *hook_list) | |
1748 | { | |
1749 | struct nft_hook *hook; | |
1750 | int err; | |
1751 | ||
1752 | if (tb[NFTA_HOOK_DEV]) { | |
1753 | hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]); | |
1754 | if (IS_ERR(hook)) | |
1755 | return PTR_ERR(hook); | |
1756 | ||
1757 | list_add_tail(&hook->list, hook_list); | |
1758 | } else if (tb[NFTA_HOOK_DEVS]) { | |
1759 | err = nf_tables_parse_netdev_hooks(net, tb[NFTA_HOOK_DEVS], | |
1760 | hook_list); | |
1761 | if (err < 0) | |
1762 | return err; | |
1763 | } else { | |
1764 | return -EINVAL; | |
1765 | } | |
1766 | ||
1767 | return 0; | |
1768 | } | |
1769 | ||
508f8ccd PNA |
1770 | static int nft_chain_parse_hook(struct net *net, |
1771 | const struct nlattr * const nla[], | |
36596dad | 1772 | struct nft_chain_hook *hook, u8 family, |
445509eb | 1773 | bool autoload) |
508f8ccd PNA |
1774 | { |
1775 | struct nlattr *ha[NFTA_HOOK_MAX + 1]; | |
32537e91 | 1776 | const struct nft_chain_type *type; |
508f8ccd PNA |
1777 | int err; |
1778 | ||
f102d66b FW |
1779 | lockdep_assert_held(&net->nft.commit_mutex); |
1780 | lockdep_nfnl_nft_mutex_not_held(); | |
1781 | ||
8cb08174 JB |
1782 | err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX, |
1783 | nla[NFTA_CHAIN_HOOK], | |
1784 | nft_hook_policy, NULL); | |
508f8ccd PNA |
1785 | if (err < 0) |
1786 | return err; | |
1787 | ||
1788 | if (ha[NFTA_HOOK_HOOKNUM] == NULL || | |
1789 | ha[NFTA_HOOK_PRIORITY] == NULL) | |
1790 | return -EINVAL; | |
1791 | ||
1792 | hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])); | |
508f8ccd PNA |
1793 | hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY])); |
1794 | ||
82603549 PNA |
1795 | type = __nft_chain_type_get(family, NFT_CHAIN_T_DEFAULT); |
1796 | if (!type) | |
1797 | return -EOPNOTSUPP; | |
1798 | ||
508f8ccd | 1799 | if (nla[NFTA_CHAIN_TYPE]) { |
452238e8 | 1800 | type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE], |
445509eb | 1801 | family, autoload); |
508f8ccd PNA |
1802 | if (IS_ERR(type)) |
1803 | return PTR_ERR(type); | |
1804 | } | |
33d1c018 | 1805 | if (hook->num > NF_MAX_HOOKS || !(type->hook_mask & (1 << hook->num))) |
508f8ccd | 1806 | return -EOPNOTSUPP; |
84ba7dd7 FW |
1807 | |
1808 | if (type->type == NFT_CHAIN_T_NAT && | |
1809 | hook->priority <= NF_IP_PRI_CONNTRACK) | |
1810 | return -EOPNOTSUPP; | |
1811 | ||
508f8ccd PNA |
1812 | if (!try_module_get(type->owner)) |
1813 | return -ENOENT; | |
1814 | ||
1815 | hook->type = type; | |
1816 | ||
d54725cd | 1817 | INIT_LIST_HEAD(&hook->list); |
36596dad | 1818 | if (family == NFPROTO_NETDEV) { |
d54725cd PNA |
1819 | err = nft_chain_parse_netdev(net, ha, &hook->list); |
1820 | if (err < 0) { | |
508f8ccd | 1821 | module_put(type->owner); |
d54725cd | 1822 | return err; |
508f8ccd | 1823 | } |
d54725cd | 1824 | } else if (ha[NFTA_HOOK_DEV] || ha[NFTA_HOOK_DEVS]) { |
508f8ccd PNA |
1825 | module_put(type->owner); |
1826 | return -EOPNOTSUPP; | |
1827 | } | |
1828 | ||
1829 | return 0; | |
1830 | } | |
1831 | ||
1832 | static void nft_chain_release_hook(struct nft_chain_hook *hook) | |
1833 | { | |
d54725cd PNA |
1834 | struct nft_hook *h, *next; |
1835 | ||
1836 | list_for_each_entry_safe(h, next, &hook->list, list) { | |
1837 | list_del(&h->list); | |
1838 | kfree(h); | |
1839 | } | |
508f8ccd | 1840 | module_put(hook->type->owner); |
508f8ccd PNA |
1841 | } |
1842 | ||
0cbc06b3 FW |
1843 | struct nft_rules_old { |
1844 | struct rcu_head h; | |
1845 | struct nft_rule **start; | |
1846 | }; | |
1847 | ||
1848 | static struct nft_rule **nf_tables_chain_alloc_rules(const struct nft_chain *chain, | |
1849 | unsigned int alloc) | |
1850 | { | |
1851 | if (alloc > INT_MAX) | |
1852 | return NULL; | |
1853 | ||
1854 | alloc += 1; /* NULL, ends rules */ | |
1855 | if (sizeof(struct nft_rule *) > INT_MAX / alloc) | |
1856 | return NULL; | |
1857 | ||
1858 | alloc *= sizeof(struct nft_rule *); | |
1859 | alloc += sizeof(struct nft_rules_old); | |
1860 | ||
1861 | return kvmalloc(alloc, GFP_KERNEL); | |
1862 | } | |
1863 | ||
d54725cd PNA |
1864 | static void nft_basechain_hook_init(struct nf_hook_ops *ops, u8 family, |
1865 | const struct nft_chain_hook *hook, | |
1866 | struct nft_chain *chain) | |
1867 | { | |
1868 | ops->pf = family; | |
1869 | ops->hooknum = hook->num; | |
1870 | ops->priority = hook->priority; | |
1871 | ops->priv = chain; | |
1872 | ops->hook = hook->type->hooks[ops->hooknum]; | |
1873 | } | |
1874 | ||
1875 | static int nft_basechain_init(struct nft_base_chain *basechain, u8 family, | |
1876 | struct nft_chain_hook *hook, u32 flags) | |
1877 | { | |
1878 | struct nft_chain *chain; | |
1879 | struct nft_hook *h; | |
1880 | ||
1881 | basechain->type = hook->type; | |
1882 | INIT_LIST_HEAD(&basechain->hook_list); | |
1883 | chain = &basechain->chain; | |
1884 | ||
1885 | if (family == NFPROTO_NETDEV) { | |
1886 | list_splice_init(&hook->list, &basechain->hook_list); | |
1887 | list_for_each_entry(h, &basechain->hook_list, list) | |
1888 | nft_basechain_hook_init(&h->ops, family, hook, chain); | |
1889 | ||
1890 | basechain->ops.hooknum = hook->num; | |
1891 | basechain->ops.priority = hook->priority; | |
1892 | } else { | |
1893 | nft_basechain_hook_init(&basechain->ops, family, hook, chain); | |
1894 | } | |
1895 | ||
1896 | chain->flags |= NFT_BASE_CHAIN | flags; | |
1897 | basechain->policy = NF_ACCEPT; | |
1898 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD && | |
1899 | nft_chain_offload_priority(basechain) < 0) | |
1900 | return -EOPNOTSUPP; | |
1901 | ||
1902 | flow_block_init(&basechain->flow_block); | |
1903 | ||
1904 | return 0; | |
1905 | } | |
1906 | ||
4035285f | 1907 | static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, |
c9626a2c | 1908 | u8 policy, u32 flags) |
4035285f PNA |
1909 | { |
1910 | const struct nlattr * const *nla = ctx->nla; | |
1911 | struct nft_table *table = ctx->table; | |
4035285f PNA |
1912 | struct nft_base_chain *basechain; |
1913 | struct nft_stats __percpu *stats; | |
1914 | struct net *net = ctx->net; | |
66293c46 | 1915 | struct nft_trans *trans; |
4035285f | 1916 | struct nft_chain *chain; |
0cbc06b3 | 1917 | struct nft_rule **rules; |
4035285f PNA |
1918 | int err; |
1919 | ||
1920 | if (table->use == UINT_MAX) | |
1921 | return -EOVERFLOW; | |
1922 | ||
1923 | if (nla[NFTA_CHAIN_HOOK]) { | |
1924 | struct nft_chain_hook hook; | |
4035285f | 1925 | |
445509eb | 1926 | err = nft_chain_parse_hook(net, nla, &hook, family, true); |
4035285f PNA |
1927 | if (err < 0) |
1928 | return err; | |
1929 | ||
1930 | basechain = kzalloc(sizeof(*basechain), GFP_KERNEL); | |
1931 | if (basechain == NULL) { | |
1932 | nft_chain_release_hook(&hook); | |
1933 | return -ENOMEM; | |
1934 | } | |
d54725cd | 1935 | chain = &basechain->chain; |
4035285f PNA |
1936 | |
1937 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
1938 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
1939 | if (IS_ERR(stats)) { | |
1940 | nft_chain_release_hook(&hook); | |
1941 | kfree(basechain); | |
1942 | return PTR_ERR(stats); | |
1943 | } | |
4c05ec47 | 1944 | rcu_assign_pointer(basechain->stats, stats); |
4035285f PNA |
1945 | static_branch_inc(&nft_counters_enabled); |
1946 | } | |
1947 | ||
d54725cd PNA |
1948 | err = nft_basechain_init(basechain, family, &hook, flags); |
1949 | if (err < 0) { | |
1950 | nft_chain_release_hook(&hook); | |
1951 | kfree(basechain); | |
1952 | return err; | |
1953 | } | |
4035285f PNA |
1954 | } else { |
1955 | chain = kzalloc(sizeof(*chain), GFP_KERNEL); | |
1956 | if (chain == NULL) | |
1957 | return -ENOMEM; | |
1958 | } | |
43a605f2 PNA |
1959 | ctx->chain = chain; |
1960 | ||
4035285f PNA |
1961 | INIT_LIST_HEAD(&chain->rules); |
1962 | chain->handle = nf_tables_alloc_handle(table); | |
1963 | chain->table = table; | |
1964 | chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL); | |
1965 | if (!chain->name) { | |
1966 | err = -ENOMEM; | |
1967 | goto err1; | |
1968 | } | |
1969 | ||
0cbc06b3 FW |
1970 | rules = nf_tables_chain_alloc_rules(chain, 0); |
1971 | if (!rules) { | |
1972 | err = -ENOMEM; | |
1973 | goto err1; | |
1974 | } | |
1975 | ||
1976 | *rules = NULL; | |
1977 | rcu_assign_pointer(chain->rules_gen_0, rules); | |
1978 | rcu_assign_pointer(chain->rules_gen_1, rules); | |
1979 | ||
c974a3a3 | 1980 | err = nf_tables_register_hook(net, table, chain); |
4035285f PNA |
1981 | if (err < 0) |
1982 | goto err1; | |
1983 | ||
1b2470e5 FW |
1984 | err = rhltable_insert_key(&table->chains_ht, chain->name, |
1985 | &chain->rhlhead, nft_chain_ht_params); | |
1986 | if (err) | |
1987 | goto err2; | |
1988 | ||
66293c46 FW |
1989 | trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN); |
1990 | if (IS_ERR(trans)) { | |
1991 | err = PTR_ERR(trans); | |
1b2470e5 FW |
1992 | rhltable_remove(&table->chains_ht, &chain->rhlhead, |
1993 | nft_chain_ht_params); | |
4035285f | 1994 | goto err2; |
1b2470e5 | 1995 | } |
4035285f | 1996 | |
ad652f38 | 1997 | nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET; |
66293c46 FW |
1998 | if (nft_is_base_chain(chain)) |
1999 | nft_trans_chain_policy(trans) = policy; | |
2000 | ||
4035285f PNA |
2001 | table->use++; |
2002 | list_add_tail_rcu(&chain->list, &table->chains); | |
2003 | ||
2004 | return 0; | |
2005 | err2: | |
c974a3a3 | 2006 | nf_tables_unregister_hook(net, table, chain); |
4035285f | 2007 | err1: |
43a605f2 | 2008 | nf_tables_chain_destroy(ctx); |
4035285f PNA |
2009 | |
2010 | return err; | |
2011 | } | |
2012 | ||
d54725cd PNA |
2013 | static bool nft_hook_list_equal(struct list_head *hook_list1, |
2014 | struct list_head *hook_list2) | |
2015 | { | |
2016 | struct nft_hook *hook; | |
2017 | int n = 0, m = 0; | |
2018 | ||
2019 | n = 0; | |
2020 | list_for_each_entry(hook, hook_list2, list) { | |
2021 | if (!nft_hook_list_find(hook_list1, hook)) | |
2022 | return false; | |
2023 | ||
2024 | n++; | |
2025 | } | |
2026 | list_for_each_entry(hook, hook_list1, list) | |
2027 | m++; | |
2028 | ||
2029 | return n == m; | |
2030 | } | |
2031 | ||
c9626a2c PNA |
2032 | static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy, |
2033 | u32 flags) | |
2c4a488a PNA |
2034 | { |
2035 | const struct nlattr * const *nla = ctx->nla; | |
2036 | struct nft_table *table = ctx->table; | |
2037 | struct nft_chain *chain = ctx->chain; | |
2c4a488a PNA |
2038 | struct nft_base_chain *basechain; |
2039 | struct nft_stats *stats = NULL; | |
2040 | struct nft_chain_hook hook; | |
2c4a488a PNA |
2041 | struct nf_hook_ops *ops; |
2042 | struct nft_trans *trans; | |
c974a3a3 | 2043 | int err; |
2c4a488a | 2044 | |
c9626a2c PNA |
2045 | if (chain->flags ^ flags) |
2046 | return -EOPNOTSUPP; | |
2047 | ||
2c4a488a PNA |
2048 | if (nla[NFTA_CHAIN_HOOK]) { |
2049 | if (!nft_is_base_chain(chain)) | |
2050 | return -EBUSY; | |
2051 | ||
36596dad | 2052 | err = nft_chain_parse_hook(ctx->net, nla, &hook, ctx->family, |
445509eb | 2053 | false); |
2c4a488a PNA |
2054 | if (err < 0) |
2055 | return err; | |
2056 | ||
2057 | basechain = nft_base_chain(chain); | |
2058 | if (basechain->type != hook.type) { | |
2059 | nft_chain_release_hook(&hook); | |
2060 | return -EBUSY; | |
2061 | } | |
2062 | ||
d54725cd PNA |
2063 | if (ctx->family == NFPROTO_NETDEV) { |
2064 | if (!nft_hook_list_equal(&basechain->hook_list, | |
2065 | &hook.list)) { | |
2066 | nft_chain_release_hook(&hook); | |
2067 | return -EBUSY; | |
2068 | } | |
2069 | } else { | |
2070 | ops = &basechain->ops; | |
2071 | if (ops->hooknum != hook.num || | |
2072 | ops->priority != hook.priority) { | |
2073 | nft_chain_release_hook(&hook); | |
2074 | return -EBUSY; | |
2075 | } | |
2c4a488a PNA |
2076 | } |
2077 | nft_chain_release_hook(&hook); | |
2078 | } | |
2079 | ||
2080 | if (nla[NFTA_CHAIN_HANDLE] && | |
2081 | nla[NFTA_CHAIN_NAME]) { | |
2082 | struct nft_chain *chain2; | |
2083 | ||
f102d66b FW |
2084 | chain2 = nft_chain_lookup(ctx->net, table, |
2085 | nla[NFTA_CHAIN_NAME], genmask); | |
0d18779b JC |
2086 | if (!IS_ERR(chain2)) |
2087 | return -EEXIST; | |
2c4a488a PNA |
2088 | } |
2089 | ||
2090 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2091 | if (!nft_is_base_chain(chain)) | |
2092 | return -EOPNOTSUPP; | |
2093 | ||
2094 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2095 | if (IS_ERR(stats)) | |
2096 | return PTR_ERR(stats); | |
2097 | } | |
2098 | ||
c6cc94df | 2099 | err = -ENOMEM; |
2c4a488a PNA |
2100 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN, |
2101 | sizeof(struct nft_trans_chain)); | |
c6cc94df FW |
2102 | if (trans == NULL) |
2103 | goto err; | |
2c4a488a PNA |
2104 | |
2105 | nft_trans_chain_stats(trans) = stats; | |
2106 | nft_trans_chain_update(trans) = true; | |
2107 | ||
2108 | if (nla[NFTA_CHAIN_POLICY]) | |
2109 | nft_trans_chain_policy(trans) = policy; | |
2110 | else | |
2111 | nft_trans_chain_policy(trans) = -1; | |
2112 | ||
c6cc94df FW |
2113 | if (nla[NFTA_CHAIN_HANDLE] && |
2114 | nla[NFTA_CHAIN_NAME]) { | |
2115 | struct nft_trans *tmp; | |
2116 | char *name; | |
2117 | ||
2118 | err = -ENOMEM; | |
2119 | name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL); | |
2120 | if (!name) | |
2121 | goto err; | |
2122 | ||
2123 | err = -EEXIST; | |
2124 | list_for_each_entry(tmp, &ctx->net->nft.commit_list, list) { | |
2125 | if (tmp->msg_type == NFT_MSG_NEWCHAIN && | |
2126 | tmp->ctx.table == table && | |
2127 | nft_trans_chain_update(tmp) && | |
2128 | nft_trans_chain_name(tmp) && | |
2129 | strcmp(name, nft_trans_chain_name(tmp)) == 0) { | |
2130 | kfree(name); | |
2131 | goto err; | |
2132 | } | |
2c4a488a | 2133 | } |
c6cc94df FW |
2134 | |
2135 | nft_trans_chain_name(trans) = name; | |
2c4a488a PNA |
2136 | } |
2137 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
2138 | ||
2139 | return 0; | |
c6cc94df FW |
2140 | err: |
2141 | free_percpu(stats); | |
2142 | kfree(trans); | |
2143 | return err; | |
2c4a488a PNA |
2144 | } |
2145 | ||
633c9a84 PNA |
2146 | static int nf_tables_newchain(struct net *net, struct sock *nlsk, |
2147 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
2148 | const struct nlattr * const nla[], |
2149 | struct netlink_ext_ack *extack) | |
96518518 PM |
2150 | { |
2151 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
4035285f PNA |
2152 | u8 genmask = nft_genmask_next(net); |
2153 | int family = nfmsg->nfgen_family; | |
36dd1bcc | 2154 | const struct nlattr *attr; |
96518518 PM |
2155 | struct nft_table *table; |
2156 | struct nft_chain *chain; | |
57de2a0c | 2157 | u8 policy = NF_ACCEPT; |
4035285f | 2158 | struct nft_ctx ctx; |
96518518 | 2159 | u64 handle = 0; |
c9626a2c | 2160 | u32 flags = 0; |
96518518 | 2161 | |
f102d66b FW |
2162 | lockdep_assert_held(&net->nft.commit_mutex); |
2163 | ||
cac20fcd | 2164 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask); |
36dd1bcc PNA |
2165 | if (IS_ERR(table)) { |
2166 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2167 | return PTR_ERR(table); |
36dd1bcc | 2168 | } |
96518518 | 2169 | |
96518518 | 2170 | chain = NULL; |
36dd1bcc | 2171 | attr = nla[NFTA_CHAIN_NAME]; |
96518518 PM |
2172 | |
2173 | if (nla[NFTA_CHAIN_HANDLE]) { | |
2174 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE])); | |
cac20fcd | 2175 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
36dd1bcc PNA |
2176 | if (IS_ERR(chain)) { |
2177 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]); | |
96518518 | 2178 | return PTR_ERR(chain); |
36dd1bcc PNA |
2179 | } |
2180 | attr = nla[NFTA_CHAIN_HANDLE]; | |
96518518 | 2181 | } else { |
f102d66b | 2182 | chain = nft_chain_lookup(net, table, attr, genmask); |
96518518 | 2183 | if (IS_ERR(chain)) { |
36dd1bcc PNA |
2184 | if (PTR_ERR(chain) != -ENOENT) { |
2185 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2186 | return PTR_ERR(chain); |
36dd1bcc | 2187 | } |
96518518 PM |
2188 | chain = NULL; |
2189 | } | |
2190 | } | |
2191 | ||
57de2a0c | 2192 | if (nla[NFTA_CHAIN_POLICY]) { |
f323d954 | 2193 | if (chain != NULL && |
36dd1bcc PNA |
2194 | !nft_is_base_chain(chain)) { |
2195 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
d6b6cb1d | 2196 | return -EOPNOTSUPP; |
36dd1bcc | 2197 | } |
d6b6cb1d PNA |
2198 | |
2199 | if (chain == NULL && | |
36dd1bcc PNA |
2200 | nla[NFTA_CHAIN_HOOK] == NULL) { |
2201 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
57de2a0c | 2202 | return -EOPNOTSUPP; |
36dd1bcc | 2203 | } |
57de2a0c | 2204 | |
8f46df18 | 2205 | policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY])); |
57de2a0c PM |
2206 | switch (policy) { |
2207 | case NF_DROP: | |
2208 | case NF_ACCEPT: | |
2209 | break; | |
2210 | default: | |
2211 | return -EINVAL; | |
2212 | } | |
2213 | } | |
2214 | ||
c9626a2c PNA |
2215 | if (nla[NFTA_CHAIN_FLAGS]) |
2216 | flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS])); | |
b717273d FW |
2217 | else if (chain) |
2218 | flags = chain->flags; | |
c9626a2c | 2219 | |
98319cb9 | 2220 | nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla); |
4035285f | 2221 | |
96518518 | 2222 | if (chain != NULL) { |
36dd1bcc PNA |
2223 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
2224 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2225 | return -EEXIST; |
36dd1bcc | 2226 | } |
96518518 PM |
2227 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
2228 | return -EOPNOTSUPP; | |
2229 | ||
1ed012f6 | 2230 | flags |= chain->flags & NFT_BASE_CHAIN; |
c9626a2c | 2231 | return nf_tables_updchain(&ctx, genmask, policy, flags); |
96518518 PM |
2232 | } |
2233 | ||
c9626a2c | 2234 | return nf_tables_addchain(&ctx, family, genmask, policy, flags); |
96518518 PM |
2235 | } |
2236 | ||
633c9a84 PNA |
2237 | static int nf_tables_delchain(struct net *net, struct sock *nlsk, |
2238 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
2239 | const struct nlattr * const nla[], |
2240 | struct netlink_ext_ack *extack) | |
96518518 PM |
2241 | { |
2242 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 2243 | u8 genmask = nft_genmask_next(net); |
36dd1bcc PNA |
2244 | int family = nfmsg->nfgen_family; |
2245 | const struct nlattr *attr; | |
96518518 PM |
2246 | struct nft_table *table; |
2247 | struct nft_chain *chain; | |
9dee1474 | 2248 | struct nft_rule *rule; |
91c7b38d | 2249 | struct nft_ctx ctx; |
3ecbfd65 | 2250 | u64 handle; |
9dee1474 PNA |
2251 | u32 use; |
2252 | int err; | |
96518518 | 2253 | |
cac20fcd | 2254 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask); |
36dd1bcc PNA |
2255 | if (IS_ERR(table)) { |
2256 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2257 | return PTR_ERR(table); |
36dd1bcc | 2258 | } |
96518518 | 2259 | |
3ecbfd65 | 2260 | if (nla[NFTA_CHAIN_HANDLE]) { |
36dd1bcc PNA |
2261 | attr = nla[NFTA_CHAIN_HANDLE]; |
2262 | handle = be64_to_cpu(nla_get_be64(attr)); | |
cac20fcd | 2263 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
3ecbfd65 | 2264 | } else { |
36dd1bcc | 2265 | attr = nla[NFTA_CHAIN_NAME]; |
f102d66b | 2266 | chain = nft_chain_lookup(net, table, attr, genmask); |
3ecbfd65 | 2267 | } |
36dd1bcc PNA |
2268 | if (IS_ERR(chain)) { |
2269 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2270 | return PTR_ERR(chain); |
36dd1bcc | 2271 | } |
9dee1474 PNA |
2272 | |
2273 | if (nlh->nlmsg_flags & NLM_F_NONREC && | |
2274 | chain->use > 0) | |
96518518 PM |
2275 | return -EBUSY; |
2276 | ||
98319cb9 | 2277 | nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla); |
0165d932 | 2278 | |
9dee1474 PNA |
2279 | use = chain->use; |
2280 | list_for_each_entry(rule, &chain->rules, list) { | |
2281 | if (!nft_is_active_next(net, rule)) | |
2282 | continue; | |
2283 | use--; | |
2284 | ||
2285 | err = nft_delrule(&ctx, rule); | |
2286 | if (err < 0) | |
2287 | return err; | |
2288 | } | |
2289 | ||
2290 | /* There are rules and elements that are still holding references to us, | |
2291 | * we cannot do a recursive removal in this case. | |
2292 | */ | |
36dd1bcc PNA |
2293 | if (use > 0) { |
2294 | NL_SET_BAD_ATTR(extack, attr); | |
9dee1474 | 2295 | return -EBUSY; |
36dd1bcc | 2296 | } |
9dee1474 | 2297 | |
ee01d542 | 2298 | return nft_delchain(&ctx); |
96518518 PM |
2299 | } |
2300 | ||
96518518 PM |
2301 | /* |
2302 | * Expressions | |
2303 | */ | |
2304 | ||
2305 | /** | |
ef1f7df9 PM |
2306 | * nft_register_expr - register nf_tables expr type |
2307 | * @ops: expr type | |
96518518 | 2308 | * |
ef1f7df9 | 2309 | * Registers the expr type for use with nf_tables. Returns zero on |
96518518 PM |
2310 | * success or a negative errno code otherwise. |
2311 | */ | |
ef1f7df9 | 2312 | int nft_register_expr(struct nft_expr_type *type) |
96518518 PM |
2313 | { |
2314 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
758dbcec | 2315 | if (type->family == NFPROTO_UNSPEC) |
e688a7f8 | 2316 | list_add_tail_rcu(&type->list, &nf_tables_expressions); |
758dbcec | 2317 | else |
e688a7f8 | 2318 | list_add_rcu(&type->list, &nf_tables_expressions); |
96518518 PM |
2319 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2320 | return 0; | |
2321 | } | |
2322 | EXPORT_SYMBOL_GPL(nft_register_expr); | |
2323 | ||
2324 | /** | |
ef1f7df9 PM |
2325 | * nft_unregister_expr - unregister nf_tables expr type |
2326 | * @ops: expr type | |
96518518 | 2327 | * |
ef1f7df9 | 2328 | * Unregisters the expr typefor use with nf_tables. |
96518518 | 2329 | */ |
ef1f7df9 | 2330 | void nft_unregister_expr(struct nft_expr_type *type) |
96518518 PM |
2331 | { |
2332 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 2333 | list_del_rcu(&type->list); |
96518518 PM |
2334 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2335 | } | |
2336 | EXPORT_SYMBOL_GPL(nft_unregister_expr); | |
2337 | ||
64d46806 PM |
2338 | static const struct nft_expr_type *__nft_expr_type_get(u8 family, |
2339 | struct nlattr *nla) | |
96518518 | 2340 | { |
9cff126f | 2341 | const struct nft_expr_type *type, *candidate = NULL; |
96518518 | 2342 | |
ef1f7df9 | 2343 | list_for_each_entry(type, &nf_tables_expressions, list) { |
9cff126f PNA |
2344 | if (!nla_strcmp(nla, type->name)) { |
2345 | if (!type->family && !candidate) | |
2346 | candidate = type; | |
2347 | else if (type->family == family) | |
2348 | candidate = type; | |
2349 | } | |
96518518 | 2350 | } |
9cff126f | 2351 | return candidate; |
96518518 PM |
2352 | } |
2353 | ||
b9c04ae7 PNA |
2354 | #ifdef CONFIG_MODULES |
2355 | static int nft_expr_type_request_module(struct net *net, u8 family, | |
2356 | struct nlattr *nla) | |
2357 | { | |
eb014de4 PNA |
2358 | if (nft_request_module(net, "nft-expr-%u-%.*s", family, |
2359 | nla_len(nla), (char *)nla_data(nla)) == -EAGAIN) | |
b9c04ae7 PNA |
2360 | return -EAGAIN; |
2361 | ||
2362 | return 0; | |
2363 | } | |
2364 | #endif | |
2365 | ||
452238e8 FW |
2366 | static const struct nft_expr_type *nft_expr_type_get(struct net *net, |
2367 | u8 family, | |
64d46806 | 2368 | struct nlattr *nla) |
96518518 | 2369 | { |
ef1f7df9 | 2370 | const struct nft_expr_type *type; |
96518518 PM |
2371 | |
2372 | if (nla == NULL) | |
2373 | return ERR_PTR(-EINVAL); | |
2374 | ||
64d46806 | 2375 | type = __nft_expr_type_get(family, nla); |
ef1f7df9 PM |
2376 | if (type != NULL && try_module_get(type->owner)) |
2377 | return type; | |
96518518 | 2378 | |
f102d66b | 2379 | lockdep_nfnl_nft_mutex_not_held(); |
96518518 | 2380 | #ifdef CONFIG_MODULES |
ef1f7df9 | 2381 | if (type == NULL) { |
b9c04ae7 | 2382 | if (nft_expr_type_request_module(net, family, nla) == -EAGAIN) |
64d46806 PM |
2383 | return ERR_PTR(-EAGAIN); |
2384 | ||
eb014de4 PNA |
2385 | if (nft_request_module(net, "nft-expr-%.*s", |
2386 | nla_len(nla), | |
2387 | (char *)nla_data(nla)) == -EAGAIN) | |
96518518 PM |
2388 | return ERR_PTR(-EAGAIN); |
2389 | } | |
2390 | #endif | |
2391 | return ERR_PTR(-ENOENT); | |
2392 | } | |
2393 | ||
2394 | static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = { | |
9332d27d FW |
2395 | [NFTA_EXPR_NAME] = { .type = NLA_STRING, |
2396 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
96518518 PM |
2397 | [NFTA_EXPR_DATA] = { .type = NLA_NESTED }, |
2398 | }; | |
2399 | ||
2400 | static int nf_tables_fill_expr_info(struct sk_buff *skb, | |
2401 | const struct nft_expr *expr) | |
2402 | { | |
ef1f7df9 | 2403 | if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name)) |
96518518 PM |
2404 | goto nla_put_failure; |
2405 | ||
2406 | if (expr->ops->dump) { | |
ae0be8de MK |
2407 | struct nlattr *data = nla_nest_start_noflag(skb, |
2408 | NFTA_EXPR_DATA); | |
96518518 PM |
2409 | if (data == NULL) |
2410 | goto nla_put_failure; | |
2411 | if (expr->ops->dump(skb, expr) < 0) | |
2412 | goto nla_put_failure; | |
2413 | nla_nest_end(skb, data); | |
2414 | } | |
2415 | ||
2416 | return skb->len; | |
2417 | ||
2418 | nla_put_failure: | |
2419 | return -1; | |
2420 | }; | |
2421 | ||
0b2d8a7b PM |
2422 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
2423 | const struct nft_expr *expr) | |
2424 | { | |
2425 | struct nlattr *nest; | |
2426 | ||
ae0be8de | 2427 | nest = nla_nest_start_noflag(skb, attr); |
0b2d8a7b PM |
2428 | if (!nest) |
2429 | goto nla_put_failure; | |
2430 | if (nf_tables_fill_expr_info(skb, expr) < 0) | |
2431 | goto nla_put_failure; | |
2432 | nla_nest_end(skb, nest); | |
2433 | return 0; | |
2434 | ||
2435 | nla_put_failure: | |
2436 | return -1; | |
2437 | } | |
2438 | ||
96518518 PM |
2439 | struct nft_expr_info { |
2440 | const struct nft_expr_ops *ops; | |
ef1f7df9 | 2441 | struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; |
96518518 PM |
2442 | }; |
2443 | ||
0ca743a5 PNA |
2444 | static int nf_tables_expr_parse(const struct nft_ctx *ctx, |
2445 | const struct nlattr *nla, | |
96518518 PM |
2446 | struct nft_expr_info *info) |
2447 | { | |
ef1f7df9 | 2448 | const struct nft_expr_type *type; |
96518518 | 2449 | const struct nft_expr_ops *ops; |
ef1f7df9 | 2450 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; |
96518518 PM |
2451 | int err; |
2452 | ||
8cb08174 JB |
2453 | err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla, |
2454 | nft_expr_policy, NULL); | |
96518518 PM |
2455 | if (err < 0) |
2456 | return err; | |
2457 | ||
452238e8 | 2458 | type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]); |
ef1f7df9 PM |
2459 | if (IS_ERR(type)) |
2460 | return PTR_ERR(type); | |
2461 | ||
2462 | if (tb[NFTA_EXPR_DATA]) { | |
8cb08174 JB |
2463 | err = nla_parse_nested_deprecated(info->tb, type->maxattr, |
2464 | tb[NFTA_EXPR_DATA], | |
2465 | type->policy, NULL); | |
ef1f7df9 PM |
2466 | if (err < 0) |
2467 | goto err1; | |
2468 | } else | |
2469 | memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1)); | |
2470 | ||
2471 | if (type->select_ops != NULL) { | |
0ca743a5 PNA |
2472 | ops = type->select_ops(ctx, |
2473 | (const struct nlattr * const *)info->tb); | |
ef1f7df9 PM |
2474 | if (IS_ERR(ops)) { |
2475 | err = PTR_ERR(ops); | |
0ef1efd1 PNA |
2476 | #ifdef CONFIG_MODULES |
2477 | if (err == -EAGAIN) | |
eb014de4 PNA |
2478 | if (nft_expr_type_request_module(ctx->net, |
2479 | ctx->family, | |
2480 | tb[NFTA_EXPR_NAME]) != -EAGAIN) | |
2481 | err = -ENOENT; | |
0ef1efd1 | 2482 | #endif |
ef1f7df9 PM |
2483 | goto err1; |
2484 | } | |
2485 | } else | |
2486 | ops = type->ops; | |
2487 | ||
96518518 PM |
2488 | info->ops = ops; |
2489 | return 0; | |
ef1f7df9 PM |
2490 | |
2491 | err1: | |
2492 | module_put(type->owner); | |
2493 | return err; | |
96518518 PM |
2494 | } |
2495 | ||
2496 | static int nf_tables_newexpr(const struct nft_ctx *ctx, | |
ef1f7df9 | 2497 | const struct nft_expr_info *info, |
96518518 PM |
2498 | struct nft_expr *expr) |
2499 | { | |
2500 | const struct nft_expr_ops *ops = info->ops; | |
2501 | int err; | |
2502 | ||
2503 | expr->ops = ops; | |
2504 | if (ops->init) { | |
ef1f7df9 | 2505 | err = ops->init(ctx, expr, (const struct nlattr **)info->tb); |
96518518 PM |
2506 | if (err < 0) |
2507 | goto err1; | |
2508 | } | |
2509 | ||
96518518 | 2510 | return 0; |
96518518 PM |
2511 | err1: |
2512 | expr->ops = NULL; | |
2513 | return err; | |
2514 | } | |
2515 | ||
62472bce PM |
2516 | static void nf_tables_expr_destroy(const struct nft_ctx *ctx, |
2517 | struct nft_expr *expr) | |
96518518 | 2518 | { |
3f3a390d PNA |
2519 | const struct nft_expr_type *type = expr->ops->type; |
2520 | ||
96518518 | 2521 | if (expr->ops->destroy) |
62472bce | 2522 | expr->ops->destroy(ctx, expr); |
3f3a390d | 2523 | module_put(type->owner); |
96518518 PM |
2524 | } |
2525 | ||
0b2d8a7b PM |
2526 | struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
2527 | const struct nlattr *nla) | |
2528 | { | |
2529 | struct nft_expr_info info; | |
2530 | struct nft_expr *expr; | |
b8e20400 | 2531 | struct module *owner; |
0b2d8a7b PM |
2532 | int err; |
2533 | ||
2534 | err = nf_tables_expr_parse(ctx, nla, &info); | |
2535 | if (err < 0) | |
2536 | goto err1; | |
2537 | ||
2538 | err = -ENOMEM; | |
2539 | expr = kzalloc(info.ops->size, GFP_KERNEL); | |
2540 | if (expr == NULL) | |
2541 | goto err2; | |
2542 | ||
2543 | err = nf_tables_newexpr(ctx, &info, expr); | |
2544 | if (err < 0) | |
6cafaf47 | 2545 | goto err3; |
0b2d8a7b PM |
2546 | |
2547 | return expr; | |
6cafaf47 LZ |
2548 | err3: |
2549 | kfree(expr); | |
0b2d8a7b | 2550 | err2: |
b8e20400 PNA |
2551 | owner = info.ops->type->owner; |
2552 | if (info.ops->type->release_ops) | |
2553 | info.ops->type->release_ops(info.ops); | |
2554 | ||
2555 | module_put(owner); | |
0b2d8a7b PM |
2556 | err1: |
2557 | return ERR_PTR(err); | |
2558 | } | |
2559 | ||
2560 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr) | |
2561 | { | |
2562 | nf_tables_expr_destroy(ctx, expr); | |
2563 | kfree(expr); | |
2564 | } | |
2565 | ||
96518518 PM |
2566 | /* |
2567 | * Rules | |
2568 | */ | |
2569 | ||
cac20fcd PNA |
2570 | static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain, |
2571 | u64 handle) | |
96518518 PM |
2572 | { |
2573 | struct nft_rule *rule; | |
2574 | ||
2575 | // FIXME: this sucks | |
d9adf22a | 2576 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
96518518 PM |
2577 | if (handle == rule->handle) |
2578 | return rule; | |
2579 | } | |
2580 | ||
2581 | return ERR_PTR(-ENOENT); | |
2582 | } | |
2583 | ||
cac20fcd PNA |
2584 | static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain, |
2585 | const struct nlattr *nla) | |
96518518 PM |
2586 | { |
2587 | if (nla == NULL) | |
2588 | return ERR_PTR(-EINVAL); | |
2589 | ||
cac20fcd | 2590 | return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla))); |
96518518 PM |
2591 | } |
2592 | ||
2593 | static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { | |
b2fbd044 LZ |
2594 | [NFTA_RULE_TABLE] = { .type = NLA_STRING, |
2595 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
2596 | [NFTA_RULE_CHAIN] = { .type = NLA_STRING, |
2597 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
2598 | [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, | |
2599 | [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, | |
0ca743a5 | 2600 | [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, |
5e948466 | 2601 | [NFTA_RULE_POSITION] = { .type = NLA_U64 }, |
0768b3b3 PNA |
2602 | [NFTA_RULE_USERDATA] = { .type = NLA_BINARY, |
2603 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 2604 | [NFTA_RULE_ID] = { .type = NLA_U32 }, |
0604628b | 2605 | [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 }, |
96518518 PM |
2606 | }; |
2607 | ||
84d7fce6 PNA |
2608 | static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, |
2609 | u32 portid, u32 seq, int event, | |
2610 | u32 flags, int family, | |
96518518 PM |
2611 | const struct nft_table *table, |
2612 | const struct nft_chain *chain, | |
2c82c7e7 FW |
2613 | const struct nft_rule *rule, |
2614 | const struct nft_rule *prule) | |
96518518 PM |
2615 | { |
2616 | struct nlmsghdr *nlh; | |
2617 | struct nfgenmsg *nfmsg; | |
2618 | const struct nft_expr *expr, *next; | |
2619 | struct nlattr *list; | |
dedb67c4 | 2620 | u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 | 2621 | |
dedb67c4 | 2622 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), flags); |
96518518 PM |
2623 | if (nlh == NULL) |
2624 | goto nla_put_failure; | |
2625 | ||
2626 | nfmsg = nlmsg_data(nlh); | |
2627 | nfmsg->nfgen_family = family; | |
2628 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 2629 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
96518518 PM |
2630 | |
2631 | if (nla_put_string(skb, NFTA_RULE_TABLE, table->name)) | |
2632 | goto nla_put_failure; | |
2633 | if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name)) | |
2634 | goto nla_put_failure; | |
b46f6ded ND |
2635 | if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle), |
2636 | NFTA_RULE_PAD)) | |
96518518 PM |
2637 | goto nla_put_failure; |
2638 | ||
2c82c7e7 | 2639 | if (event != NFT_MSG_DELRULE && prule) { |
5e948466 | 2640 | if (nla_put_be64(skb, NFTA_RULE_POSITION, |
b46f6ded ND |
2641 | cpu_to_be64(prule->handle), |
2642 | NFTA_RULE_PAD)) | |
5e948466 EL |
2643 | goto nla_put_failure; |
2644 | } | |
2645 | ||
ae0be8de | 2646 | list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS); |
96518518 PM |
2647 | if (list == NULL) |
2648 | goto nla_put_failure; | |
2649 | nft_rule_for_each_expr(expr, next, rule) { | |
0b2d8a7b | 2650 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0) |
96518518 | 2651 | goto nla_put_failure; |
96518518 PM |
2652 | } |
2653 | nla_nest_end(skb, list); | |
2654 | ||
86f1ec32 PM |
2655 | if (rule->udata) { |
2656 | struct nft_userdata *udata = nft_userdata(rule); | |
2657 | if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1, | |
2658 | udata->data) < 0) | |
2659 | goto nla_put_failure; | |
2660 | } | |
0768b3b3 | 2661 | |
053c095a JB |
2662 | nlmsg_end(skb, nlh); |
2663 | return 0; | |
96518518 PM |
2664 | |
2665 | nla_put_failure: | |
2666 | nlmsg_trim(skb, nlh); | |
2667 | return -1; | |
2668 | } | |
2669 | ||
25e94a99 PNA |
2670 | static void nf_tables_rule_notify(const struct nft_ctx *ctx, |
2671 | const struct nft_rule *rule, int event) | |
96518518 PM |
2672 | { |
2673 | struct sk_buff *skb; | |
96518518 PM |
2674 | int err; |
2675 | ||
128ad332 PNA |
2676 | if (!ctx->report && |
2677 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 2678 | return; |
96518518 | 2679 | |
96518518 PM |
2680 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
2681 | if (skb == NULL) | |
2682 | goto err; | |
2683 | ||
84d7fce6 | 2684 | err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, |
36596dad | 2685 | event, 0, ctx->family, ctx->table, |
2c82c7e7 | 2686 | ctx->chain, rule, NULL); |
96518518 PM |
2687 | if (err < 0) { |
2688 | kfree_skb(skb); | |
2689 | goto err; | |
2690 | } | |
2691 | ||
25e94a99 PNA |
2692 | nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
2693 | ctx->report, GFP_KERNEL); | |
2694 | return; | |
96518518 | 2695 | err: |
25e94a99 | 2696 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
2697 | } |
2698 | ||
6e1f760e | 2699 | struct nft_rule_dump_ctx { |
e46abbcc | 2700 | char *table; |
b7263e07 | 2701 | char *chain; |
6e1f760e PNA |
2702 | }; |
2703 | ||
241faece PS |
2704 | static int __nf_tables_dump_rules(struct sk_buff *skb, |
2705 | unsigned int *idx, | |
2706 | struct netlink_callback *cb, | |
2707 | const struct nft_table *table, | |
2708 | const struct nft_chain *chain) | |
2709 | { | |
2710 | struct net *net = sock_net(skb->sk); | |
2c82c7e7 | 2711 | const struct nft_rule *rule, *prule; |
241faece | 2712 | unsigned int s_idx = cb->args[0]; |
241faece | 2713 | |
2c82c7e7 | 2714 | prule = NULL; |
241faece PS |
2715 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
2716 | if (!nft_is_active(net, rule)) | |
2c82c7e7 | 2717 | goto cont_skip; |
241faece PS |
2718 | if (*idx < s_idx) |
2719 | goto cont; | |
2720 | if (*idx > s_idx) { | |
2721 | memset(&cb->args[1], 0, | |
2722 | sizeof(cb->args) - sizeof(cb->args[0])); | |
2723 | } | |
2724 | if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid, | |
2725 | cb->nlh->nlmsg_seq, | |
2726 | NFT_MSG_NEWRULE, | |
2727 | NLM_F_MULTI | NLM_F_APPEND, | |
2728 | table->family, | |
2c82c7e7 | 2729 | table, chain, rule, prule) < 0) |
310529e6 | 2730 | return 1; |
241faece PS |
2731 | |
2732 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
2733 | cont: | |
2c82c7e7 FW |
2734 | prule = rule; |
2735 | cont_skip: | |
241faece PS |
2736 | (*idx)++; |
2737 | } | |
310529e6 | 2738 | return 0; |
241faece PS |
2739 | } |
2740 | ||
96518518 PM |
2741 | static int nf_tables_dump_rules(struct sk_buff *skb, |
2742 | struct netlink_callback *cb) | |
2743 | { | |
2744 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
6e1f760e | 2745 | const struct nft_rule_dump_ctx *ctx = cb->data; |
241faece | 2746 | struct nft_table *table; |
96518518 | 2747 | const struct nft_chain *chain; |
241faece | 2748 | unsigned int idx = 0; |
99633ab2 | 2749 | struct net *net = sock_net(skb->sk); |
96518518 PM |
2750 | int family = nfmsg->nfgen_family; |
2751 | ||
e688a7f8 | 2752 | rcu_read_lock(); |
38e029f1 PNA |
2753 | cb->seq = net->nft.base_seq; |
2754 | ||
36596dad | 2755 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 2756 | if (family != NFPROTO_UNSPEC && family != table->family) |
36596dad PNA |
2757 | continue; |
2758 | ||
2759 | if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0) | |
96518518 PM |
2760 | continue; |
2761 | ||
715849ab | 2762 | if (ctx && ctx->table && ctx->chain) { |
241faece | 2763 | struct rhlist_head *list, *tmp; |
6e1f760e | 2764 | |
241faece PS |
2765 | list = rhltable_lookup(&table->chains_ht, ctx->chain, |
2766 | nft_chain_ht_params); | |
2767 | if (!list) | |
2768 | goto done; | |
2769 | ||
2770 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
2771 | if (!nft_is_active(net, chain)) | |
2772 | continue; | |
2773 | __nf_tables_dump_rules(skb, &idx, | |
2774 | cb, table, chain); | |
2775 | break; | |
96518518 | 2776 | } |
241faece | 2777 | goto done; |
96518518 | 2778 | } |
241faece PS |
2779 | |
2780 | list_for_each_entry_rcu(chain, &table->chains, list) { | |
2781 | if (__nf_tables_dump_rules(skb, &idx, cb, table, chain)) | |
2782 | goto done; | |
2783 | } | |
2784 | ||
2785 | if (ctx && ctx->table) | |
2786 | break; | |
96518518 PM |
2787 | } |
2788 | done: | |
e688a7f8 | 2789 | rcu_read_unlock(); |
310529e6 PS |
2790 | |
2791 | cb->args[0] = idx; | |
96518518 PM |
2792 | return skb->len; |
2793 | } | |
2794 | ||
90fd131a FW |
2795 | static int nf_tables_dump_rules_start(struct netlink_callback *cb) |
2796 | { | |
2797 | const struct nlattr * const *nla = cb->data; | |
2798 | struct nft_rule_dump_ctx *ctx = NULL; | |
2799 | ||
2800 | if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) { | |
2801 | ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC); | |
2802 | if (!ctx) | |
2803 | return -ENOMEM; | |
2804 | ||
2805 | if (nla[NFTA_RULE_TABLE]) { | |
2806 | ctx->table = nla_strdup(nla[NFTA_RULE_TABLE], | |
2807 | GFP_ATOMIC); | |
2808 | if (!ctx->table) { | |
2809 | kfree(ctx); | |
2810 | return -ENOMEM; | |
2811 | } | |
2812 | } | |
2813 | if (nla[NFTA_RULE_CHAIN]) { | |
2814 | ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN], | |
2815 | GFP_ATOMIC); | |
2816 | if (!ctx->chain) { | |
2817 | kfree(ctx->table); | |
2818 | kfree(ctx); | |
2819 | return -ENOMEM; | |
2820 | } | |
2821 | } | |
2822 | } | |
2823 | ||
2824 | cb->data = ctx; | |
2825 | return 0; | |
2826 | } | |
2827 | ||
6e1f760e PNA |
2828 | static int nf_tables_dump_rules_done(struct netlink_callback *cb) |
2829 | { | |
e46abbcc PS |
2830 | struct nft_rule_dump_ctx *ctx = cb->data; |
2831 | ||
2832 | if (ctx) { | |
2833 | kfree(ctx->table); | |
b7263e07 | 2834 | kfree(ctx->chain); |
e46abbcc PS |
2835 | kfree(ctx); |
2836 | } | |
6e1f760e PNA |
2837 | return 0; |
2838 | } | |
2839 | ||
d9adf22a | 2840 | /* called with rcu_read_lock held */ |
7b8002a1 PNA |
2841 | static int nf_tables_getrule(struct net *net, struct sock *nlsk, |
2842 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
2843 | const struct nlattr * const nla[], |
2844 | struct netlink_ext_ack *extack) | |
96518518 PM |
2845 | { |
2846 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 2847 | u8 genmask = nft_genmask_cur(net); |
96518518 PM |
2848 | const struct nft_chain *chain; |
2849 | const struct nft_rule *rule; | |
1b2470e5 | 2850 | struct nft_table *table; |
96518518 PM |
2851 | struct sk_buff *skb2; |
2852 | int family = nfmsg->nfgen_family; | |
2853 | int err; | |
2854 | ||
2855 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
2856 | struct netlink_dump_control c = { | |
90fd131a | 2857 | .start= nf_tables_dump_rules_start, |
96518518 | 2858 | .dump = nf_tables_dump_rules, |
6e1f760e | 2859 | .done = nf_tables_dump_rules_done, |
d9adf22a | 2860 | .module = THIS_MODULE, |
90fd131a | 2861 | .data = (void *)nla, |
96518518 | 2862 | }; |
6e1f760e | 2863 | |
d9adf22a | 2864 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
96518518 PM |
2865 | } |
2866 | ||
cac20fcd | 2867 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask); |
36dd1bcc PNA |
2868 | if (IS_ERR(table)) { |
2869 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 2870 | return PTR_ERR(table); |
36dd1bcc | 2871 | } |
96518518 | 2872 | |
f102d66b | 2873 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
2874 | if (IS_ERR(chain)) { |
2875 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
96518518 | 2876 | return PTR_ERR(chain); |
36dd1bcc | 2877 | } |
96518518 | 2878 | |
cac20fcd | 2879 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
2880 | if (IS_ERR(rule)) { |
2881 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 2882 | return PTR_ERR(rule); |
36dd1bcc | 2883 | } |
96518518 | 2884 | |
d9adf22a | 2885 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
2886 | if (!skb2) |
2887 | return -ENOMEM; | |
2888 | ||
84d7fce6 | 2889 | err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, |
96518518 | 2890 | nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, |
2c82c7e7 | 2891 | family, table, chain, rule, NULL); |
96518518 PM |
2892 | if (err < 0) |
2893 | goto err; | |
2894 | ||
2895 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
2896 | ||
2897 | err: | |
2898 | kfree_skb(skb2); | |
2899 | return err; | |
2900 | } | |
2901 | ||
62472bce PM |
2902 | static void nf_tables_rule_destroy(const struct nft_ctx *ctx, |
2903 | struct nft_rule *rule) | |
96518518 | 2904 | { |
29e38801 | 2905 | struct nft_expr *expr, *next; |
96518518 PM |
2906 | |
2907 | /* | |
2908 | * Careful: some expressions might not be initialized in case this | |
2909 | * is called on error from nf_tables_newrule(). | |
2910 | */ | |
2911 | expr = nft_expr_first(rule); | |
3e38df13 | 2912 | while (expr != nft_expr_last(rule) && expr->ops) { |
29e38801 | 2913 | next = nft_expr_next(expr); |
62472bce | 2914 | nf_tables_expr_destroy(ctx, expr); |
29e38801 | 2915 | expr = next; |
96518518 PM |
2916 | } |
2917 | kfree(rule); | |
2918 | } | |
2919 | ||
bb7b40ae PNA |
2920 | static void nf_tables_rule_release(const struct nft_ctx *ctx, |
2921 | struct nft_rule *rule) | |
2922 | { | |
f6ac8585 | 2923 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE); |
bb7b40ae PNA |
2924 | nf_tables_rule_destroy(ctx, rule); |
2925 | } | |
2926 | ||
a654de8f PNA |
2927 | int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain) |
2928 | { | |
2929 | struct nft_expr *expr, *last; | |
2930 | const struct nft_data *data; | |
2931 | struct nft_rule *rule; | |
2932 | int err; | |
2933 | ||
26b2f552 TY |
2934 | if (ctx->level == NFT_JUMP_STACK_SIZE) |
2935 | return -EMLINK; | |
2936 | ||
a654de8f PNA |
2937 | list_for_each_entry(rule, &chain->rules, list) { |
2938 | if (!nft_is_active_next(ctx->net, rule)) | |
2939 | continue; | |
2940 | ||
2941 | nft_rule_for_each_expr(expr, last, rule) { | |
2942 | if (!expr->ops->validate) | |
2943 | continue; | |
2944 | ||
2945 | err = expr->ops->validate(ctx, expr, &data); | |
2946 | if (err < 0) | |
2947 | return err; | |
2948 | } | |
2949 | } | |
2950 | ||
2951 | return 0; | |
2952 | } | |
2953 | EXPORT_SYMBOL_GPL(nft_chain_validate); | |
2954 | ||
2955 | static int nft_table_validate(struct net *net, const struct nft_table *table) | |
2956 | { | |
2957 | struct nft_chain *chain; | |
2958 | struct nft_ctx ctx = { | |
2959 | .net = net, | |
2960 | .family = table->family, | |
2961 | }; | |
2962 | int err; | |
2963 | ||
2964 | list_for_each_entry(chain, &table->chains, list) { | |
2965 | if (!nft_is_base_chain(chain)) | |
2966 | continue; | |
2967 | ||
2968 | ctx.chain = chain; | |
2969 | err = nft_chain_validate(&ctx, chain); | |
2970 | if (err < 0) | |
2971 | return err; | |
2972 | } | |
2973 | ||
2974 | return 0; | |
2975 | } | |
2976 | ||
75dd48e2 PS |
2977 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
2978 | const struct nlattr *nla); | |
2979 | ||
1081d11b PNA |
2980 | #define NFT_RULE_MAXEXPRS 128 |
2981 | ||
633c9a84 PNA |
2982 | static int nf_tables_newrule(struct net *net, struct sock *nlsk, |
2983 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
2984 | const struct nlattr * const nla[], |
2985 | struct netlink_ext_ack *extack) | |
96518518 PM |
2986 | { |
2987 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 2988 | u8 genmask = nft_genmask_next(net); |
2a43ecf9 | 2989 | struct nft_expr_info *info = NULL; |
98319cb9 | 2990 | int family = nfmsg->nfgen_family; |
c9626a2c | 2991 | struct nft_flow_rule *flow; |
96518518 PM |
2992 | struct nft_table *table; |
2993 | struct nft_chain *chain; | |
2994 | struct nft_rule *rule, *old_rule = NULL; | |
86f1ec32 | 2995 | struct nft_userdata *udata; |
1081d11b | 2996 | struct nft_trans *trans = NULL; |
96518518 PM |
2997 | struct nft_expr *expr; |
2998 | struct nft_ctx ctx; | |
2999 | struct nlattr *tmp; | |
86f1ec32 | 3000 | unsigned int size, i, n, ulen = 0, usize = 0; |
96518518 | 3001 | int err, rem; |
5e948466 | 3002 | u64 handle, pos_handle; |
96518518 | 3003 | |
f102d66b FW |
3004 | lockdep_assert_held(&net->nft.commit_mutex); |
3005 | ||
cac20fcd | 3006 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask); |
36dd1bcc PNA |
3007 | if (IS_ERR(table)) { |
3008 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3009 | return PTR_ERR(table); |
36dd1bcc | 3010 | } |
96518518 | 3011 | |
f102d66b | 3012 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
3013 | if (IS_ERR(chain)) { |
3014 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
96518518 | 3015 | return PTR_ERR(chain); |
36dd1bcc | 3016 | } |
96518518 PM |
3017 | |
3018 | if (nla[NFTA_RULE_HANDLE]) { | |
3019 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE])); | |
cac20fcd | 3020 | rule = __nft_rule_lookup(chain, handle); |
36dd1bcc PNA |
3021 | if (IS_ERR(rule)) { |
3022 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3023 | return PTR_ERR(rule); |
36dd1bcc | 3024 | } |
96518518 | 3025 | |
36dd1bcc PNA |
3026 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
3027 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3028 | return -EEXIST; |
36dd1bcc | 3029 | } |
96518518 PM |
3030 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
3031 | old_rule = rule; | |
3032 | else | |
3033 | return -EOPNOTSUPP; | |
3034 | } else { | |
445509eb PNA |
3035 | if (!(nlh->nlmsg_flags & NLM_F_CREATE) || |
3036 | nlh->nlmsg_flags & NLM_F_REPLACE) | |
96518518 PM |
3037 | return -EINVAL; |
3038 | handle = nf_tables_alloc_handle(table); | |
a0a7379e PNA |
3039 | |
3040 | if (chain->use == UINT_MAX) | |
3041 | return -EOVERFLOW; | |
5e948466 | 3042 | |
447750f2 FW |
3043 | if (nla[NFTA_RULE_POSITION]) { |
3044 | pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION])); | |
3045 | old_rule = __nft_rule_lookup(chain, pos_handle); | |
3046 | if (IS_ERR(old_rule)) { | |
3047 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]); | |
3048 | return PTR_ERR(old_rule); | |
3049 | } | |
75dd48e2 PS |
3050 | } else if (nla[NFTA_RULE_POSITION_ID]) { |
3051 | old_rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_POSITION_ID]); | |
3052 | if (IS_ERR(old_rule)) { | |
3053 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]); | |
3054 | return PTR_ERR(old_rule); | |
3055 | } | |
36dd1bcc | 3056 | } |
5e948466 EL |
3057 | } |
3058 | ||
98319cb9 | 3059 | nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla); |
0ca743a5 | 3060 | |
96518518 PM |
3061 | n = 0; |
3062 | size = 0; | |
3063 | if (nla[NFTA_RULE_EXPRESSIONS]) { | |
2a43ecf9 FW |
3064 | info = kvmalloc_array(NFT_RULE_MAXEXPRS, |
3065 | sizeof(struct nft_expr_info), | |
3066 | GFP_KERNEL); | |
3067 | if (!info) | |
3068 | return -ENOMEM; | |
3069 | ||
96518518 PM |
3070 | nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) { |
3071 | err = -EINVAL; | |
3072 | if (nla_type(tmp) != NFTA_LIST_ELEM) | |
3073 | goto err1; | |
3074 | if (n == NFT_RULE_MAXEXPRS) | |
3075 | goto err1; | |
0ca743a5 | 3076 | err = nf_tables_expr_parse(&ctx, tmp, &info[n]); |
96518518 PM |
3077 | if (err < 0) |
3078 | goto err1; | |
3079 | size += info[n].ops->size; | |
3080 | n++; | |
3081 | } | |
3082 | } | |
9889840f PM |
3083 | /* Check for overflow of dlen field */ |
3084 | err = -EFBIG; | |
3085 | if (size >= 1 << 12) | |
3086 | goto err1; | |
96518518 | 3087 | |
86f1ec32 | 3088 | if (nla[NFTA_RULE_USERDATA]) { |
0768b3b3 | 3089 | ulen = nla_len(nla[NFTA_RULE_USERDATA]); |
86f1ec32 PM |
3090 | if (ulen > 0) |
3091 | usize = sizeof(struct nft_userdata) + ulen; | |
3092 | } | |
0768b3b3 | 3093 | |
96518518 | 3094 | err = -ENOMEM; |
86f1ec32 | 3095 | rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL); |
96518518 PM |
3096 | if (rule == NULL) |
3097 | goto err1; | |
3098 | ||
889f7ee7 | 3099 | nft_activate_next(net, rule); |
0628b123 | 3100 | |
96518518 PM |
3101 | rule->handle = handle; |
3102 | rule->dlen = size; | |
86f1ec32 | 3103 | rule->udata = ulen ? 1 : 0; |
0768b3b3 | 3104 | |
86f1ec32 PM |
3105 | if (ulen) { |
3106 | udata = nft_userdata(rule); | |
3107 | udata->len = ulen - 1; | |
3108 | nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen); | |
3109 | } | |
96518518 | 3110 | |
96518518 PM |
3111 | expr = nft_expr_first(rule); |
3112 | for (i = 0; i < n; i++) { | |
3113 | err = nf_tables_newexpr(&ctx, &info[i], expr); | |
3114 | if (err < 0) | |
3115 | goto err2; | |
a654de8f PNA |
3116 | |
3117 | if (info[i].ops->validate) | |
3118 | nft_validate_state_update(net, NFT_VALIDATE_NEED); | |
3119 | ||
ef1f7df9 | 3120 | info[i].ops = NULL; |
96518518 PM |
3121 | expr = nft_expr_next(expr); |
3122 | } | |
3123 | ||
96518518 | 3124 | if (nlh->nlmsg_flags & NLM_F_REPLACE) { |
ca089878 | 3125 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
569ccae6 FW |
3126 | if (trans == NULL) { |
3127 | err = -ENOMEM; | |
3128 | goto err2; | |
3129 | } | |
ca089878 TY |
3130 | err = nft_delrule(&ctx, old_rule); |
3131 | if (err < 0) { | |
3132 | nft_trans_destroy(trans); | |
569ccae6 FW |
3133 | goto err2; |
3134 | } | |
3135 | ||
3136 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
3137 | } else { | |
c9626a2c PNA |
3138 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
3139 | if (!trans) { | |
569ccae6 FW |
3140 | err = -ENOMEM; |
3141 | goto err2; | |
3142 | } | |
3143 | ||
3144 | if (nlh->nlmsg_flags & NLM_F_APPEND) { | |
3145 | if (old_rule) | |
3146 | list_add_rcu(&rule->list, &old_rule->list); | |
3147 | else | |
3148 | list_add_tail_rcu(&rule->list, &chain->rules); | |
3149 | } else { | |
3150 | if (old_rule) | |
3151 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
3152 | else | |
3153 | list_add_rcu(&rule->list, &chain->rules); | |
3154 | } | |
0628b123 | 3155 | } |
2a43ecf9 | 3156 | kvfree(info); |
4fefee57 | 3157 | chain->use++; |
96518518 | 3158 | |
a654de8f PNA |
3159 | if (net->nft.validate_state == NFT_VALIDATE_DO) |
3160 | return nft_table_validate(net, table); | |
3161 | ||
c9626a2c | 3162 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
be2861dc | 3163 | flow = nft_flow_rule_create(net, rule); |
c9626a2c PNA |
3164 | if (IS_ERR(flow)) |
3165 | return PTR_ERR(flow); | |
3166 | ||
3167 | nft_trans_flow_rule(trans) = flow; | |
3168 | } | |
3169 | ||
a654de8f | 3170 | return 0; |
96518518 | 3171 | err2: |
bb7b40ae | 3172 | nf_tables_rule_release(&ctx, rule); |
96518518 PM |
3173 | err1: |
3174 | for (i = 0; i < n; i++) { | |
b25a31bf | 3175 | if (info[i].ops) { |
ef1f7df9 | 3176 | module_put(info[i].ops->type->owner); |
b25a31bf TY |
3177 | if (info[i].ops->type->release_ops) |
3178 | info[i].ops->type->release_ops(info[i].ops); | |
3179 | } | |
96518518 | 3180 | } |
2a43ecf9 | 3181 | kvfree(info); |
96518518 PM |
3182 | return err; |
3183 | } | |
3184 | ||
1a94e38d PNA |
3185 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
3186 | const struct nlattr *nla) | |
3187 | { | |
3188 | u32 id = ntohl(nla_get_be32(nla)); | |
3189 | struct nft_trans *trans; | |
3190 | ||
3191 | list_for_each_entry(trans, &net->nft.commit_list, list) { | |
3192 | struct nft_rule *rule = nft_trans_rule(trans); | |
3193 | ||
3194 | if (trans->msg_type == NFT_MSG_NEWRULE && | |
3195 | id == nft_trans_rule_id(trans)) | |
3196 | return rule; | |
3197 | } | |
3198 | return ERR_PTR(-ENOENT); | |
3199 | } | |
3200 | ||
633c9a84 PNA |
3201 | static int nf_tables_delrule(struct net *net, struct sock *nlsk, |
3202 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
3203 | const struct nlattr * const nla[], |
3204 | struct netlink_ext_ack *extack) | |
96518518 PM |
3205 | { |
3206 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 3207 | u8 genmask = nft_genmask_next(net); |
7c95f6d8 | 3208 | struct nft_table *table; |
cf9dc09d PNA |
3209 | struct nft_chain *chain = NULL; |
3210 | struct nft_rule *rule; | |
0628b123 PNA |
3211 | int family = nfmsg->nfgen_family, err = 0; |
3212 | struct nft_ctx ctx; | |
96518518 | 3213 | |
cac20fcd | 3214 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask); |
36dd1bcc PNA |
3215 | if (IS_ERR(table)) { |
3216 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3217 | return PTR_ERR(table); |
36dd1bcc | 3218 | } |
96518518 | 3219 | |
cf9dc09d | 3220 | if (nla[NFTA_RULE_CHAIN]) { |
f102d66b FW |
3221 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], |
3222 | genmask); | |
36dd1bcc PNA |
3223 | if (IS_ERR(chain)) { |
3224 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
cf9dc09d | 3225 | return PTR_ERR(chain); |
36dd1bcc | 3226 | } |
cf9dc09d | 3227 | } |
96518518 | 3228 | |
98319cb9 | 3229 | nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla); |
0628b123 | 3230 | |
cf9dc09d PNA |
3231 | if (chain) { |
3232 | if (nla[NFTA_RULE_HANDLE]) { | |
cac20fcd | 3233 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
3234 | if (IS_ERR(rule)) { |
3235 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
cf9dc09d | 3236 | return PTR_ERR(rule); |
36dd1bcc | 3237 | } |
96518518 | 3238 | |
1a94e38d PNA |
3239 | err = nft_delrule(&ctx, rule); |
3240 | } else if (nla[NFTA_RULE_ID]) { | |
3241 | rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_ID]); | |
36dd1bcc PNA |
3242 | if (IS_ERR(rule)) { |
3243 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]); | |
1a94e38d | 3244 | return PTR_ERR(rule); |
36dd1bcc | 3245 | } |
1a94e38d | 3246 | |
5e266fe7 | 3247 | err = nft_delrule(&ctx, rule); |
cf9dc09d | 3248 | } else { |
ce24b721 | 3249 | err = nft_delrule_by_chain(&ctx); |
cf9dc09d PNA |
3250 | } |
3251 | } else { | |
3252 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
3253 | if (!nft_is_active_next(net, chain)) |
3254 | continue; | |
3255 | ||
cf9dc09d | 3256 | ctx.chain = chain; |
ce24b721 | 3257 | err = nft_delrule_by_chain(&ctx); |
0628b123 PNA |
3258 | if (err < 0) |
3259 | break; | |
3260 | } | |
3261 | } | |
3262 | ||
3263 | return err; | |
3264 | } | |
3265 | ||
20a69341 PM |
3266 | /* |
3267 | * Sets | |
3268 | */ | |
3269 | ||
2b664957 | 3270 | static LIST_HEAD(nf_tables_set_types); |
20a69341 | 3271 | |
2b664957 | 3272 | int nft_register_set(struct nft_set_type *type) |
20a69341 PM |
3273 | { |
3274 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
2b664957 | 3275 | list_add_tail_rcu(&type->list, &nf_tables_set_types); |
20a69341 PM |
3276 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
3277 | return 0; | |
3278 | } | |
3279 | EXPORT_SYMBOL_GPL(nft_register_set); | |
3280 | ||
2b664957 | 3281 | void nft_unregister_set(struct nft_set_type *type) |
20a69341 PM |
3282 | { |
3283 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
2b664957 | 3284 | list_del_rcu(&type->list); |
20a69341 PM |
3285 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
3286 | } | |
3287 | EXPORT_SYMBOL_GPL(nft_unregister_set); | |
3288 | ||
2b664957 | 3289 | #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \ |
71cc0873 PS |
3290 | NFT_SET_TIMEOUT | NFT_SET_OBJECT | \ |
3291 | NFT_SET_EVAL) | |
2b664957 | 3292 | |
71cc0873 | 3293 | static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags) |
2b664957 | 3294 | { |
71cc0873 | 3295 | return (flags & type->features) == (flags & NFT_SET_FEATURES); |
2b664957 PNA |
3296 | } |
3297 | ||
c50b960c PM |
3298 | /* |
3299 | * Select a set implementation based on the data characteristics and the | |
3300 | * given policy. The total memory use might not be known if no size is | |
3301 | * given, in that case the amount of memory per element is used. | |
3302 | */ | |
3303 | static const struct nft_set_ops * | |
2b664957 PNA |
3304 | nft_select_set_ops(const struct nft_ctx *ctx, |
3305 | const struct nlattr * const nla[], | |
c50b960c PM |
3306 | const struct nft_set_desc *desc, |
3307 | enum nft_set_policies policy) | |
20a69341 | 3308 | { |
c50b960c PM |
3309 | const struct nft_set_ops *ops, *bops; |
3310 | struct nft_set_estimate est, best; | |
2b664957 PNA |
3311 | const struct nft_set_type *type; |
3312 | u32 flags = 0; | |
20a69341 | 3313 | |
f102d66b FW |
3314 | lockdep_assert_held(&ctx->net->nft.commit_mutex); |
3315 | lockdep_nfnl_nft_mutex_not_held(); | |
20a69341 | 3316 | #ifdef CONFIG_MODULES |
2b664957 | 3317 | if (list_empty(&nf_tables_set_types)) { |
eb014de4 | 3318 | if (nft_request_module(ctx->net, "nft-set") == -EAGAIN) |
20a69341 PM |
3319 | return ERR_PTR(-EAGAIN); |
3320 | } | |
3321 | #endif | |
2b664957 PNA |
3322 | if (nla[NFTA_SET_FLAGS] != NULL) |
3323 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
20a69341 | 3324 | |
55af753c PNA |
3325 | bops = NULL; |
3326 | best.size = ~0; | |
3327 | best.lookup = ~0; | |
0b5a7874 | 3328 | best.space = ~0; |
c50b960c | 3329 | |
2b664957 | 3330 | list_for_each_entry(type, &nf_tables_set_types, list) { |
71cc0873 | 3331 | ops = &type->ops; |
2b664957 | 3332 | |
71cc0873 | 3333 | if (!nft_set_ops_candidate(type, flags)) |
20a69341 | 3334 | continue; |
2b664957 | 3335 | if (!ops->estimate(desc, flags, &est)) |
c50b960c PM |
3336 | continue; |
3337 | ||
3338 | switch (policy) { | |
3339 | case NFT_SET_POL_PERFORMANCE: | |
55af753c | 3340 | if (est.lookup < best.lookup) |
c50b960c | 3341 | break; |
644e334e PNA |
3342 | if (est.lookup == best.lookup && |
3343 | est.space < best.space) | |
3344 | break; | |
c50b960c PM |
3345 | continue; |
3346 | case NFT_SET_POL_MEMORY: | |
0b5a7874 PNA |
3347 | if (!desc->size) { |
3348 | if (est.space < best.space) | |
3349 | break; | |
3350 | if (est.space == best.space && | |
3351 | est.lookup < best.lookup) | |
3352 | break; | |
4f2921ca | 3353 | } else if (est.size < best.size || !bops) { |
c50b960c | 3354 | break; |
0b5a7874 | 3355 | } |
c50b960c PM |
3356 | continue; |
3357 | default: | |
3358 | break; | |
3359 | } | |
3360 | ||
2b664957 | 3361 | if (!try_module_get(type->owner)) |
20a69341 | 3362 | continue; |
c50b960c | 3363 | if (bops != NULL) |
71cc0873 | 3364 | module_put(to_set_type(bops)->owner); |
c50b960c PM |
3365 | |
3366 | bops = ops; | |
3367 | best = est; | |
20a69341 PM |
3368 | } |
3369 | ||
c50b960c PM |
3370 | if (bops != NULL) |
3371 | return bops; | |
3372 | ||
20a69341 PM |
3373 | return ERR_PTR(-EOPNOTSUPP); |
3374 | } | |
3375 | ||
3376 | static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = { | |
b2fbd044 LZ |
3377 | [NFTA_SET_TABLE] = { .type = NLA_STRING, |
3378 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
a9bdd836 | 3379 | [NFTA_SET_NAME] = { .type = NLA_STRING, |
cb39ad8b | 3380 | .len = NFT_SET_MAXNAMELEN - 1 }, |
20a69341 PM |
3381 | [NFTA_SET_FLAGS] = { .type = NLA_U32 }, |
3382 | [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 }, | |
3383 | [NFTA_SET_KEY_LEN] = { .type = NLA_U32 }, | |
3384 | [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 }, | |
3385 | [NFTA_SET_DATA_LEN] = { .type = NLA_U32 }, | |
c50b960c PM |
3386 | [NFTA_SET_POLICY] = { .type = NLA_U32 }, |
3387 | [NFTA_SET_DESC] = { .type = NLA_NESTED }, | |
958bee14 | 3388 | [NFTA_SET_ID] = { .type = NLA_U32 }, |
761da293 PM |
3389 | [NFTA_SET_TIMEOUT] = { .type = NLA_U64 }, |
3390 | [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 }, | |
e6d8ecac CFG |
3391 | [NFTA_SET_USERDATA] = { .type = NLA_BINARY, |
3392 | .len = NFT_USERDATA_MAXLEN }, | |
8aeff920 | 3393 | [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 }, |
3ecbfd65 | 3394 | [NFTA_SET_HANDLE] = { .type = NLA_U64 }, |
c50b960c PM |
3395 | }; |
3396 | ||
3397 | static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { | |
3398 | [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 }, | |
f3a2181e | 3399 | [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED }, |
20a69341 PM |
3400 | }; |
3401 | ||
633c9a84 | 3402 | static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net, |
20a69341 PM |
3403 | const struct sk_buff *skb, |
3404 | const struct nlmsghdr *nlh, | |
f2a6d766 | 3405 | const struct nlattr * const nla[], |
36dd1bcc | 3406 | struct netlink_ext_ack *extack, |
f2a6d766 | 3407 | u8 genmask) |
20a69341 PM |
3408 | { |
3409 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
98319cb9 | 3410 | int family = nfmsg->nfgen_family; |
7c95f6d8 | 3411 | struct nft_table *table = NULL; |
20a69341 | 3412 | |
20a69341 | 3413 | if (nla[NFTA_SET_TABLE] != NULL) { |
cac20fcd PNA |
3414 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, |
3415 | genmask); | |
36dd1bcc PNA |
3416 | if (IS_ERR(table)) { |
3417 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
20a69341 | 3418 | return PTR_ERR(table); |
36dd1bcc | 3419 | } |
20a69341 PM |
3420 | } |
3421 | ||
98319cb9 | 3422 | nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla); |
20a69341 PM |
3423 | return 0; |
3424 | } | |
3425 | ||
cac20fcd PNA |
3426 | static struct nft_set *nft_set_lookup(const struct nft_table *table, |
3427 | const struct nlattr *nla, u8 genmask) | |
20a69341 PM |
3428 | { |
3429 | struct nft_set *set; | |
3430 | ||
3431 | if (nla == NULL) | |
3432 | return ERR_PTR(-EINVAL); | |
3433 | ||
d9adf22a | 3434 | list_for_each_entry_rcu(set, &table->sets, list) { |
37a9cc52 PNA |
3435 | if (!nla_strcmp(nla, set->name) && |
3436 | nft_active_genmask(set, genmask)) | |
20a69341 PM |
3437 | return set; |
3438 | } | |
3439 | return ERR_PTR(-ENOENT); | |
3440 | } | |
3441 | ||
cac20fcd PNA |
3442 | static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table, |
3443 | const struct nlattr *nla, | |
3444 | u8 genmask) | |
3ecbfd65 HS |
3445 | { |
3446 | struct nft_set *set; | |
3447 | ||
3ecbfd65 HS |
3448 | list_for_each_entry(set, &table->sets, list) { |
3449 | if (be64_to_cpu(nla_get_be64(nla)) == set->handle && | |
3450 | nft_active_genmask(set, genmask)) | |
3451 | return set; | |
3452 | } | |
3453 | return ERR_PTR(-ENOENT); | |
3454 | } | |
3455 | ||
cac20fcd PNA |
3456 | static struct nft_set *nft_set_lookup_byid(const struct net *net, |
3457 | const struct nlattr *nla, u8 genmask) | |
958bee14 PNA |
3458 | { |
3459 | struct nft_trans *trans; | |
3460 | u32 id = ntohl(nla_get_be32(nla)); | |
3461 | ||
3462 | list_for_each_entry(trans, &net->nft.commit_list, list) { | |
9c7f96fd AK |
3463 | if (trans->msg_type == NFT_MSG_NEWSET) { |
3464 | struct nft_set *set = nft_trans_set(trans); | |
37a9cc52 | 3465 | |
9c7f96fd AK |
3466 | if (id == nft_trans_set_id(trans) && |
3467 | nft_active_genmask(set, genmask)) | |
3468 | return set; | |
3469 | } | |
958bee14 PNA |
3470 | } |
3471 | return ERR_PTR(-ENOENT); | |
3472 | } | |
c7a72e3f | 3473 | |
10659cba PNA |
3474 | struct nft_set *nft_set_lookup_global(const struct net *net, |
3475 | const struct nft_table *table, | |
3476 | const struct nlattr *nla_set_name, | |
3477 | const struct nlattr *nla_set_id, | |
3478 | u8 genmask) | |
c7a72e3f PNA |
3479 | { |
3480 | struct nft_set *set; | |
3481 | ||
cac20fcd | 3482 | set = nft_set_lookup(table, nla_set_name, genmask); |
c7a72e3f PNA |
3483 | if (IS_ERR(set)) { |
3484 | if (!nla_set_id) | |
3485 | return set; | |
3486 | ||
cac20fcd | 3487 | set = nft_set_lookup_byid(net, nla_set_id, genmask); |
c7a72e3f PNA |
3488 | } |
3489 | return set; | |
3490 | } | |
10659cba | 3491 | EXPORT_SYMBOL_GPL(nft_set_lookup_global); |
958bee14 | 3492 | |
20a69341 PM |
3493 | static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, |
3494 | const char *name) | |
3495 | { | |
3496 | const struct nft_set *i; | |
3497 | const char *p; | |
3498 | unsigned long *inuse; | |
60eb1894 | 3499 | unsigned int n = 0, min = 0; |
20a69341 | 3500 | |
38745490 | 3501 | p = strchr(name, '%'); |
20a69341 PM |
3502 | if (p != NULL) { |
3503 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
3504 | return -EINVAL; | |
3505 | ||
3506 | inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL); | |
3507 | if (inuse == NULL) | |
3508 | return -ENOMEM; | |
60eb1894 | 3509 | cont: |
20a69341 | 3510 | list_for_each_entry(i, &ctx->table->sets, list) { |
14662917 DB |
3511 | int tmp; |
3512 | ||
37a9cc52 PNA |
3513 | if (!nft_is_active_next(ctx->net, set)) |
3514 | continue; | |
14662917 | 3515 | if (!sscanf(i->name, name, &tmp)) |
20a69341 | 3516 | continue; |
60eb1894 | 3517 | if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE) |
20a69341 | 3518 | continue; |
14662917 | 3519 | |
60eb1894 | 3520 | set_bit(tmp - min, inuse); |
20a69341 PM |
3521 | } |
3522 | ||
53b70287 | 3523 | n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE); |
60eb1894 PM |
3524 | if (n >= BITS_PER_BYTE * PAGE_SIZE) { |
3525 | min += BITS_PER_BYTE * PAGE_SIZE; | |
3526 | memset(inuse, 0, PAGE_SIZE); | |
3527 | goto cont; | |
3528 | } | |
20a69341 PM |
3529 | free_page((unsigned long)inuse); |
3530 | } | |
3531 | ||
38745490 PS |
3532 | set->name = kasprintf(GFP_KERNEL, name, min + n); |
3533 | if (!set->name) | |
3534 | return -ENOMEM; | |
3535 | ||
20a69341 | 3536 | list_for_each_entry(i, &ctx->table->sets, list) { |
37a9cc52 PNA |
3537 | if (!nft_is_active_next(ctx->net, i)) |
3538 | continue; | |
e63aaaa6 AY |
3539 | if (!strcmp(set->name, i->name)) { |
3540 | kfree(set->name); | |
20a69341 | 3541 | return -ENFILE; |
e63aaaa6 | 3542 | } |
20a69341 PM |
3543 | } |
3544 | return 0; | |
3545 | } | |
3546 | ||
8e1102d5 FW |
3547 | static int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result) |
3548 | { | |
3549 | u64 ms = be64_to_cpu(nla_get_be64(nla)); | |
3550 | u64 max = (u64)(~((u64)0)); | |
3551 | ||
3552 | max = div_u64(max, NSEC_PER_MSEC); | |
3553 | if (ms >= max) | |
3554 | return -ERANGE; | |
3555 | ||
3556 | ms *= NSEC_PER_MSEC; | |
3557 | *result = nsecs_to_jiffies64(ms); | |
3558 | return 0; | |
3559 | } | |
3560 | ||
d6501de8 | 3561 | static __be64 nf_jiffies64_to_msecs(u64 input) |
8e1102d5 | 3562 | { |
3b15d09f | 3563 | return cpu_to_be64(jiffies64_to_msecs(input)); |
8e1102d5 FW |
3564 | } |
3565 | ||
f3a2181e SB |
3566 | static int nf_tables_fill_set_concat(struct sk_buff *skb, |
3567 | const struct nft_set *set) | |
3568 | { | |
3569 | struct nlattr *concat, *field; | |
3570 | int i; | |
3571 | ||
3572 | concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT); | |
3573 | if (!concat) | |
3574 | return -ENOMEM; | |
3575 | ||
3576 | for (i = 0; i < set->field_count; i++) { | |
3577 | field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); | |
3578 | if (!field) | |
3579 | return -ENOMEM; | |
3580 | ||
3581 | if (nla_put_be32(skb, NFTA_SET_FIELD_LEN, | |
3582 | htonl(set->field_len[i]))) | |
3583 | return -ENOMEM; | |
3584 | ||
3585 | nla_nest_end(skb, field); | |
3586 | } | |
3587 | ||
3588 | nla_nest_end(skb, concat); | |
3589 | ||
3590 | return 0; | |
3591 | } | |
3592 | ||
20a69341 PM |
3593 | static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, |
3594 | const struct nft_set *set, u16 event, u16 flags) | |
3595 | { | |
3596 | struct nfgenmsg *nfmsg; | |
3597 | struct nlmsghdr *nlh; | |
c50b960c | 3598 | struct nlattr *desc; |
128ad332 PNA |
3599 | u32 portid = ctx->portid; |
3600 | u32 seq = ctx->seq; | |
20a69341 | 3601 | |
dedb67c4 | 3602 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
20a69341 PM |
3603 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), |
3604 | flags); | |
3605 | if (nlh == NULL) | |
3606 | goto nla_put_failure; | |
3607 | ||
3608 | nfmsg = nlmsg_data(nlh); | |
36596dad | 3609 | nfmsg->nfgen_family = ctx->family; |
20a69341 | 3610 | nfmsg->version = NFNETLINK_V0; |
84d7fce6 | 3611 | nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff); |
20a69341 PM |
3612 | |
3613 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) | |
3614 | goto nla_put_failure; | |
3615 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
3616 | goto nla_put_failure; | |
3ecbfd65 HS |
3617 | if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle), |
3618 | NFTA_SET_PAD)) | |
3619 | goto nla_put_failure; | |
20a69341 PM |
3620 | if (set->flags != 0) |
3621 | if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags))) | |
3622 | goto nla_put_failure; | |
3623 | ||
3624 | if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype))) | |
3625 | goto nla_put_failure; | |
3626 | if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen))) | |
3627 | goto nla_put_failure; | |
3628 | if (set->flags & NFT_SET_MAP) { | |
3629 | if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype))) | |
3630 | goto nla_put_failure; | |
3631 | if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen))) | |
3632 | goto nla_put_failure; | |
3633 | } | |
8aeff920 PNA |
3634 | if (set->flags & NFT_SET_OBJECT && |
3635 | nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype))) | |
3636 | goto nla_put_failure; | |
20a69341 | 3637 | |
761da293 | 3638 | if (set->timeout && |
d3e2a111 | 3639 | nla_put_be64(skb, NFTA_SET_TIMEOUT, |
8e1102d5 | 3640 | nf_jiffies64_to_msecs(set->timeout), |
b46f6ded | 3641 | NFTA_SET_PAD)) |
761da293 PM |
3642 | goto nla_put_failure; |
3643 | if (set->gc_int && | |
3644 | nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int))) | |
3645 | goto nla_put_failure; | |
3646 | ||
9363dc4b AB |
3647 | if (set->policy != NFT_SET_POL_PERFORMANCE) { |
3648 | if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) | |
3649 | goto nla_put_failure; | |
3650 | } | |
3651 | ||
e6d8ecac CFG |
3652 | if (nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata)) |
3653 | goto nla_put_failure; | |
3654 | ||
ae0be8de | 3655 | desc = nla_nest_start_noflag(skb, NFTA_SET_DESC); |
f3a2181e | 3656 | |
c50b960c PM |
3657 | if (desc == NULL) |
3658 | goto nla_put_failure; | |
3659 | if (set->size && | |
3660 | nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size))) | |
3661 | goto nla_put_failure; | |
f3a2181e SB |
3662 | |
3663 | if (set->field_count > 1 && | |
3664 | nf_tables_fill_set_concat(skb, set)) | |
3665 | goto nla_put_failure; | |
3666 | ||
c50b960c PM |
3667 | nla_nest_end(skb, desc); |
3668 | ||
053c095a JB |
3669 | nlmsg_end(skb, nlh); |
3670 | return 0; | |
20a69341 PM |
3671 | |
3672 | nla_put_failure: | |
3673 | nlmsg_trim(skb, nlh); | |
3674 | return -1; | |
3675 | } | |
3676 | ||
25e94a99 PNA |
3677 | static void nf_tables_set_notify(const struct nft_ctx *ctx, |
3678 | const struct nft_set *set, int event, | |
3679 | gfp_t gfp_flags) | |
20a69341 PM |
3680 | { |
3681 | struct sk_buff *skb; | |
128ad332 | 3682 | u32 portid = ctx->portid; |
20a69341 PM |
3683 | int err; |
3684 | ||
128ad332 PNA |
3685 | if (!ctx->report && |
3686 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 3687 | return; |
20a69341 | 3688 | |
31f8441c | 3689 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags); |
20a69341 PM |
3690 | if (skb == NULL) |
3691 | goto err; | |
3692 | ||
3693 | err = nf_tables_fill_set(skb, ctx, set, event, 0); | |
3694 | if (err < 0) { | |
3695 | kfree_skb(skb); | |
3696 | goto err; | |
3697 | } | |
3698 | ||
25e94a99 PNA |
3699 | nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, ctx->report, |
3700 | gfp_flags); | |
3701 | return; | |
20a69341 | 3702 | err: |
25e94a99 | 3703 | nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
20a69341 PM |
3704 | } |
3705 | ||
5b96af77 | 3706 | static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb) |
c9c8e485 PNA |
3707 | { |
3708 | const struct nft_set *set; | |
3709 | unsigned int idx, s_idx = cb->args[0]; | |
c9c8e485 PNA |
3710 | struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2]; |
3711 | struct net *net = sock_net(skb->sk); | |
5b96af77 | 3712 | struct nft_ctx *ctx = cb->data, ctx_set; |
c9c8e485 PNA |
3713 | |
3714 | if (cb->args[1]) | |
3715 | return skb->len; | |
3716 | ||
e688a7f8 | 3717 | rcu_read_lock(); |
38e029f1 PNA |
3718 | cb->seq = net->nft.base_seq; |
3719 | ||
36596dad PNA |
3720 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
3721 | if (ctx->family != NFPROTO_UNSPEC && | |
98319cb9 | 3722 | ctx->family != table->family) |
36596dad PNA |
3723 | continue; |
3724 | ||
3725 | if (ctx->table && ctx->table != table) | |
5b96af77 PNA |
3726 | continue; |
3727 | ||
36596dad PNA |
3728 | if (cur_table) { |
3729 | if (cur_table != table) | |
c9c8e485 PNA |
3730 | continue; |
3731 | ||
36596dad | 3732 | cur_table = NULL; |
c9c8e485 | 3733 | } |
36596dad PNA |
3734 | idx = 0; |
3735 | list_for_each_entry_rcu(set, &table->sets, list) { | |
3736 | if (idx < s_idx) | |
3737 | goto cont; | |
3738 | if (!nft_is_active(net, set)) | |
3739 | goto cont; | |
5b96af77 | 3740 | |
36596dad PNA |
3741 | ctx_set = *ctx; |
3742 | ctx_set.table = table; | |
98319cb9 | 3743 | ctx_set.family = table->family; |
c9c8e485 | 3744 | |
36596dad PNA |
3745 | if (nf_tables_fill_set(skb, &ctx_set, set, |
3746 | NFT_MSG_NEWSET, | |
3747 | NLM_F_MULTI) < 0) { | |
3748 | cb->args[0] = idx; | |
3749 | cb->args[2] = (unsigned long) table; | |
3750 | goto done; | |
c9c8e485 | 3751 | } |
36596dad | 3752 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
c9c8e485 | 3753 | cont: |
36596dad | 3754 | idx++; |
c9c8e485 | 3755 | } |
36596dad PNA |
3756 | if (s_idx) |
3757 | s_idx = 0; | |
c9c8e485 PNA |
3758 | } |
3759 | cb->args[1] = 1; | |
3760 | done: | |
e688a7f8 | 3761 | rcu_read_unlock(); |
c9c8e485 PNA |
3762 | return skb->len; |
3763 | } | |
3764 | ||
90fd131a FW |
3765 | static int nf_tables_dump_sets_start(struct netlink_callback *cb) |
3766 | { | |
3767 | struct nft_ctx *ctx_dump = NULL; | |
3768 | ||
3769 | ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC); | |
3770 | if (ctx_dump == NULL) | |
3771 | return -ENOMEM; | |
3772 | ||
3773 | cb->data = ctx_dump; | |
3774 | return 0; | |
3775 | } | |
3776 | ||
5b96af77 | 3777 | static int nf_tables_dump_sets_done(struct netlink_callback *cb) |
20a69341 | 3778 | { |
5b96af77 PNA |
3779 | kfree(cb->data); |
3780 | return 0; | |
20a69341 PM |
3781 | } |
3782 | ||
d9adf22a | 3783 | /* called with rcu_read_lock held */ |
7b8002a1 PNA |
3784 | static int nf_tables_getset(struct net *net, struct sock *nlsk, |
3785 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
3786 | const struct nlattr * const nla[], |
3787 | struct netlink_ext_ack *extack) | |
20a69341 | 3788 | { |
f2a6d766 | 3789 | u8 genmask = nft_genmask_cur(net); |
20a69341 PM |
3790 | const struct nft_set *set; |
3791 | struct nft_ctx ctx; | |
3792 | struct sk_buff *skb2; | |
c9c8e485 | 3793 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
20a69341 PM |
3794 | int err; |
3795 | ||
01cfa0a4 | 3796 | /* Verify existence before starting dump */ |
36dd1bcc PNA |
3797 | err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack, |
3798 | genmask); | |
20a69341 PM |
3799 | if (err < 0) |
3800 | return err; | |
3801 | ||
3802 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
3803 | struct netlink_dump_control c = { | |
90fd131a | 3804 | .start = nf_tables_dump_sets_start, |
20a69341 | 3805 | .dump = nf_tables_dump_sets, |
5b96af77 | 3806 | .done = nf_tables_dump_sets_done, |
90fd131a | 3807 | .data = &ctx, |
d9adf22a | 3808 | .module = THIS_MODULE, |
20a69341 | 3809 | }; |
5b96af77 | 3810 | |
d9adf22a | 3811 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
20a69341 PM |
3812 | } |
3813 | ||
c9c8e485 PNA |
3814 | /* Only accept unspec with dump */ |
3815 | if (nfmsg->nfgen_family == NFPROTO_UNSPEC) | |
3816 | return -EAFNOSUPPORT; | |
eaa2bcd6 PT |
3817 | if (!nla[NFTA_SET_TABLE]) |
3818 | return -EINVAL; | |
c9c8e485 | 3819 | |
cac20fcd | 3820 | set = nft_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask); |
20a69341 PM |
3821 | if (IS_ERR(set)) |
3822 | return PTR_ERR(set); | |
3823 | ||
d9adf22a | 3824 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
20a69341 PM |
3825 | if (skb2 == NULL) |
3826 | return -ENOMEM; | |
3827 | ||
3828 | err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0); | |
3829 | if (err < 0) | |
3830 | goto err; | |
3831 | ||
3832 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
3833 | ||
3834 | err: | |
3835 | kfree_skb(skb2); | |
3836 | return err; | |
3837 | } | |
3838 | ||
f3a2181e SB |
3839 | static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = { |
3840 | [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 }, | |
3841 | }; | |
3842 | ||
3843 | static int nft_set_desc_concat_parse(const struct nlattr *attr, | |
3844 | struct nft_set_desc *desc) | |
3845 | { | |
3846 | struct nlattr *tb[NFTA_SET_FIELD_MAX + 1]; | |
3847 | u32 len; | |
3848 | int err; | |
3849 | ||
3850 | err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr, | |
3851 | nft_concat_policy, NULL); | |
3852 | if (err < 0) | |
3853 | return err; | |
3854 | ||
3855 | if (!tb[NFTA_SET_FIELD_LEN]) | |
3856 | return -EINVAL; | |
3857 | ||
3858 | len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN])); | |
3859 | ||
3860 | if (len * BITS_PER_BYTE / 32 > NFT_REG32_COUNT) | |
3861 | return -E2BIG; | |
3862 | ||
3863 | desc->field_len[desc->field_count++] = len; | |
3864 | ||
3865 | return 0; | |
3866 | } | |
3867 | ||
3868 | static int nft_set_desc_concat(struct nft_set_desc *desc, | |
3869 | const struct nlattr *nla) | |
3870 | { | |
3871 | struct nlattr *attr; | |
3872 | int rem, err; | |
3873 | ||
3874 | nla_for_each_nested(attr, nla, rem) { | |
3875 | if (nla_type(attr) != NFTA_LIST_ELEM) | |
3876 | return -EINVAL; | |
3877 | ||
3878 | err = nft_set_desc_concat_parse(attr, desc); | |
3879 | if (err < 0) | |
3880 | return err; | |
3881 | } | |
3882 | ||
3883 | return 0; | |
3884 | } | |
3885 | ||
f7e840ee | 3886 | static int nf_tables_set_desc_parse(struct nft_set_desc *desc, |
c50b960c PM |
3887 | const struct nlattr *nla) |
3888 | { | |
3889 | struct nlattr *da[NFTA_SET_DESC_MAX + 1]; | |
3890 | int err; | |
3891 | ||
8cb08174 JB |
3892 | err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla, |
3893 | nft_set_desc_policy, NULL); | |
c50b960c PM |
3894 | if (err < 0) |
3895 | return err; | |
3896 | ||
3897 | if (da[NFTA_SET_DESC_SIZE] != NULL) | |
3898 | desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE])); | |
f3a2181e SB |
3899 | if (da[NFTA_SET_DESC_CONCAT]) |
3900 | err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]); | |
c50b960c | 3901 | |
f3a2181e | 3902 | return err; |
c50b960c PM |
3903 | } |
3904 | ||
633c9a84 PNA |
3905 | static int nf_tables_newset(struct net *net, struct sock *nlsk, |
3906 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
3907 | const struct nlattr * const nla[], |
3908 | struct netlink_ext_ack *extack) | |
20a69341 PM |
3909 | { |
3910 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 3911 | u8 genmask = nft_genmask_next(net); |
98319cb9 | 3912 | int family = nfmsg->nfgen_family; |
20a69341 | 3913 | const struct nft_set_ops *ops; |
20a69341 PM |
3914 | struct nft_table *table; |
3915 | struct nft_set *set; | |
3916 | struct nft_ctx ctx; | |
38745490 | 3917 | char *name; |
4ef360dd | 3918 | u64 size; |
761da293 | 3919 | u64 timeout; |
8aeff920 | 3920 | u32 ktype, dtype, flags, policy, gc_int, objtype; |
c50b960c | 3921 | struct nft_set_desc desc; |
e6d8ecac CFG |
3922 | unsigned char *udata; |
3923 | u16 udlen; | |
20a69341 | 3924 | int err; |
f3a2181e | 3925 | int i; |
20a69341 PM |
3926 | |
3927 | if (nla[NFTA_SET_TABLE] == NULL || | |
3928 | nla[NFTA_SET_NAME] == NULL || | |
958bee14 PNA |
3929 | nla[NFTA_SET_KEY_LEN] == NULL || |
3930 | nla[NFTA_SET_ID] == NULL) | |
20a69341 PM |
3931 | return -EINVAL; |
3932 | ||
c50b960c PM |
3933 | memset(&desc, 0, sizeof(desc)); |
3934 | ||
20a69341 PM |
3935 | ktype = NFT_DATA_VALUE; |
3936 | if (nla[NFTA_SET_KEY_TYPE] != NULL) { | |
3937 | ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE])); | |
3938 | if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK) | |
3939 | return -EINVAL; | |
3940 | } | |
3941 | ||
c50b960c | 3942 | desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); |
7d740264 | 3943 | if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
3944 | return -EINVAL; |
3945 | ||
3946 | flags = 0; | |
3947 | if (nla[NFTA_SET_FLAGS] != NULL) { | |
3948 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
3949 | if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT | | |
7c6c6e95 | 3950 | NFT_SET_INTERVAL | NFT_SET_TIMEOUT | |
8aeff920 PNA |
3951 | NFT_SET_MAP | NFT_SET_EVAL | |
3952 | NFT_SET_OBJECT)) | |
20a69341 | 3953 | return -EINVAL; |
8aeff920 | 3954 | /* Only one of these operations is supported */ |
acab7131 FW |
3955 | if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) == |
3956 | (NFT_SET_MAP | NFT_SET_OBJECT)) | |
3957 | return -EOPNOTSUPP; | |
3958 | if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) == | |
3959 | (NFT_SET_EVAL | NFT_SET_OBJECT)) | |
7c6c6e95 | 3960 | return -EOPNOTSUPP; |
20a69341 PM |
3961 | } |
3962 | ||
3963 | dtype = 0; | |
20a69341 PM |
3964 | if (nla[NFTA_SET_DATA_TYPE] != NULL) { |
3965 | if (!(flags & NFT_SET_MAP)) | |
3966 | return -EINVAL; | |
3967 | ||
3968 | dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE])); | |
3969 | if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK && | |
3970 | dtype != NFT_DATA_VERDICT) | |
3971 | return -EINVAL; | |
3972 | ||
3973 | if (dtype != NFT_DATA_VERDICT) { | |
3974 | if (nla[NFTA_SET_DATA_LEN] == NULL) | |
3975 | return -EINVAL; | |
c50b960c | 3976 | desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN])); |
7d740264 | 3977 | if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
3978 | return -EINVAL; |
3979 | } else | |
7d740264 | 3980 | desc.dlen = sizeof(struct nft_verdict); |
20a69341 PM |
3981 | } else if (flags & NFT_SET_MAP) |
3982 | return -EINVAL; | |
3983 | ||
8aeff920 PNA |
3984 | if (nla[NFTA_SET_OBJ_TYPE] != NULL) { |
3985 | if (!(flags & NFT_SET_OBJECT)) | |
3986 | return -EINVAL; | |
3987 | ||
3988 | objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE])); | |
3989 | if (objtype == NFT_OBJECT_UNSPEC || | |
3990 | objtype > NFT_OBJECT_MAX) | |
3991 | return -EINVAL; | |
3992 | } else if (flags & NFT_SET_OBJECT) | |
3993 | return -EINVAL; | |
3994 | else | |
3995 | objtype = NFT_OBJECT_UNSPEC; | |
3996 | ||
761da293 PM |
3997 | timeout = 0; |
3998 | if (nla[NFTA_SET_TIMEOUT] != NULL) { | |
3999 | if (!(flags & NFT_SET_TIMEOUT)) | |
4000 | return -EINVAL; | |
8e1102d5 FW |
4001 | |
4002 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &timeout); | |
4003 | if (err) | |
4004 | return err; | |
761da293 PM |
4005 | } |
4006 | gc_int = 0; | |
4007 | if (nla[NFTA_SET_GC_INTERVAL] != NULL) { | |
4008 | if (!(flags & NFT_SET_TIMEOUT)) | |
4009 | return -EINVAL; | |
4010 | gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL])); | |
4011 | } | |
4012 | ||
c50b960c PM |
4013 | policy = NFT_SET_POL_PERFORMANCE; |
4014 | if (nla[NFTA_SET_POLICY] != NULL) | |
4015 | policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY])); | |
4016 | ||
4017 | if (nla[NFTA_SET_DESC] != NULL) { | |
f7e840ee | 4018 | err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]); |
c50b960c PM |
4019 | if (err < 0) |
4020 | return err; | |
4021 | } | |
4022 | ||
cac20fcd | 4023 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask); |
36dd1bcc PNA |
4024 | if (IS_ERR(table)) { |
4025 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
20a69341 | 4026 | return PTR_ERR(table); |
36dd1bcc | 4027 | } |
20a69341 | 4028 | |
98319cb9 | 4029 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
20a69341 | 4030 | |
cac20fcd | 4031 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 | 4032 | if (IS_ERR(set)) { |
36dd1bcc PNA |
4033 | if (PTR_ERR(set) != -ENOENT) { |
4034 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
20a69341 | 4035 | return PTR_ERR(set); |
36dd1bcc | 4036 | } |
1a28ad74 | 4037 | } else { |
36dd1bcc PNA |
4038 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
4039 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
20a69341 | 4040 | return -EEXIST; |
36dd1bcc | 4041 | } |
20a69341 PM |
4042 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
4043 | return -EOPNOTSUPP; | |
36dd1bcc | 4044 | |
20a69341 PM |
4045 | return 0; |
4046 | } | |
4047 | ||
4048 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) | |
4049 | return -ENOENT; | |
4050 | ||
2b664957 | 4051 | ops = nft_select_set_ops(&ctx, nla, &desc, policy); |
20a69341 PM |
4052 | if (IS_ERR(ops)) |
4053 | return PTR_ERR(ops); | |
4054 | ||
e6d8ecac CFG |
4055 | udlen = 0; |
4056 | if (nla[NFTA_SET_USERDATA]) | |
4057 | udlen = nla_len(nla[NFTA_SET_USERDATA]); | |
4058 | ||
20a69341 PM |
4059 | size = 0; |
4060 | if (ops->privsize != NULL) | |
347b408d | 4061 | size = ops->privsize(nla, &desc); |
20a69341 | 4062 | |
1ff75a3e PNA |
4063 | set = kvzalloc(sizeof(*set) + size + udlen, GFP_KERNEL); |
4064 | if (!set) { | |
4065 | err = -ENOMEM; | |
20a69341 | 4066 | goto err1; |
1ff75a3e | 4067 | } |
20a69341 | 4068 | |
38745490 PS |
4069 | name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL); |
4070 | if (!name) { | |
4071 | err = -ENOMEM; | |
4072 | goto err2; | |
4073 | } | |
4074 | ||
20a69341 | 4075 | err = nf_tables_set_alloc_name(&ctx, set, name); |
38745490 | 4076 | kfree(name); |
20a69341 PM |
4077 | if (err < 0) |
4078 | goto err2; | |
4079 | ||
e6d8ecac CFG |
4080 | udata = NULL; |
4081 | if (udlen) { | |
4082 | udata = set->data + size; | |
4083 | nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen); | |
4084 | } | |
4085 | ||
20a69341 | 4086 | INIT_LIST_HEAD(&set->bindings); |
3453c927 PNA |
4087 | set->table = table; |
4088 | write_pnet(&set->net, net); | |
20a69341 PM |
4089 | set->ops = ops; |
4090 | set->ktype = ktype; | |
c50b960c | 4091 | set->klen = desc.klen; |
20a69341 | 4092 | set->dtype = dtype; |
8aeff920 | 4093 | set->objtype = objtype; |
c50b960c | 4094 | set->dlen = desc.dlen; |
20a69341 | 4095 | set->flags = flags; |
c50b960c | 4096 | set->size = desc.size; |
9363dc4b | 4097 | set->policy = policy; |
e6d8ecac CFG |
4098 | set->udlen = udlen; |
4099 | set->udata = udata; | |
761da293 PM |
4100 | set->timeout = timeout; |
4101 | set->gc_int = gc_int; | |
3ecbfd65 | 4102 | set->handle = nf_tables_alloc_handle(table); |
20a69341 | 4103 | |
f3a2181e SB |
4104 | set->field_count = desc.field_count; |
4105 | for (i = 0; i < desc.field_count; i++) | |
4106 | set->field_len[i] = desc.field_len[i]; | |
4107 | ||
c50b960c | 4108 | err = ops->init(set, &desc, nla); |
20a69341 | 4109 | if (err < 0) |
2f6adf48 | 4110 | goto err3; |
20a69341 | 4111 | |
958bee14 | 4112 | err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set); |
20a69341 | 4113 | if (err < 0) |
2f6adf48 | 4114 | goto err4; |
20a69341 | 4115 | |
e688a7f8 | 4116 | list_add_tail_rcu(&set->list, &table->sets); |
4fefee57 | 4117 | table->use++; |
20a69341 PM |
4118 | return 0; |
4119 | ||
2f6adf48 | 4120 | err4: |
c17c3cdf | 4121 | ops->destroy(set); |
2f6adf48 FW |
4122 | err3: |
4123 | kfree(set->name); | |
20a69341 | 4124 | err2: |
1ff75a3e | 4125 | kvfree(set); |
20a69341 | 4126 | err1: |
71cc0873 | 4127 | module_put(to_set_type(ops)->owner); |
20a69341 PM |
4128 | return err; |
4129 | } | |
4130 | ||
958bee14 | 4131 | static void nft_set_destroy(struct nft_set *set) |
20a69341 | 4132 | { |
273fe3f1 PNA |
4133 | if (WARN_ON(set->use > 0)) |
4134 | return; | |
4135 | ||
20a69341 | 4136 | set->ops->destroy(set); |
71cc0873 | 4137 | module_put(to_set_type(set->ops)->owner); |
38745490 | 4138 | kfree(set->name); |
1ff75a3e | 4139 | kvfree(set); |
20a69341 PM |
4140 | } |
4141 | ||
633c9a84 PNA |
4142 | static int nf_tables_delset(struct net *net, struct sock *nlsk, |
4143 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
4144 | const struct nlattr * const nla[], |
4145 | struct netlink_ext_ack *extack) | |
20a69341 | 4146 | { |
c9c8e485 | 4147 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
f2a6d766 | 4148 | u8 genmask = nft_genmask_next(net); |
36dd1bcc | 4149 | const struct nlattr *attr; |
20a69341 PM |
4150 | struct nft_set *set; |
4151 | struct nft_ctx ctx; | |
4152 | int err; | |
4153 | ||
ec2c9935 PM |
4154 | if (nfmsg->nfgen_family == NFPROTO_UNSPEC) |
4155 | return -EAFNOSUPPORT; | |
20a69341 PM |
4156 | if (nla[NFTA_SET_TABLE] == NULL) |
4157 | return -EINVAL; | |
4158 | ||
36dd1bcc PNA |
4159 | err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack, |
4160 | genmask); | |
20a69341 PM |
4161 | if (err < 0) |
4162 | return err; | |
4163 | ||
36dd1bcc PNA |
4164 | if (nla[NFTA_SET_HANDLE]) { |
4165 | attr = nla[NFTA_SET_HANDLE]; | |
4166 | set = nft_set_lookup_byhandle(ctx.table, attr, genmask); | |
4167 | } else { | |
4168 | attr = nla[NFTA_SET_NAME]; | |
4169 | set = nft_set_lookup(ctx.table, attr, genmask); | |
4170 | } | |
a8278400 | 4171 | |
36dd1bcc PNA |
4172 | if (IS_ERR(set)) { |
4173 | NL_SET_BAD_ATTR(extack, attr); | |
4174 | return PTR_ERR(set); | |
4175 | } | |
273fe3f1 | 4176 | if (set->use || |
36dd1bcc PNA |
4177 | (nlh->nlmsg_flags & NLM_F_NONREC && atomic_read(&set->nelems) > 0)) { |
4178 | NL_SET_BAD_ATTR(extack, attr); | |
20a69341 | 4179 | return -EBUSY; |
36dd1bcc | 4180 | } |
20a69341 | 4181 | |
ee01d542 | 4182 | return nft_delset(&ctx, set); |
20a69341 PM |
4183 | } |
4184 | ||
4185 | static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, | |
de70185d | 4186 | struct nft_set *set, |
20a69341 | 4187 | const struct nft_set_iter *iter, |
de70185d | 4188 | struct nft_set_elem *elem) |
20a69341 | 4189 | { |
fe2811eb | 4190 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
4191 | enum nft_registers dreg; |
4192 | ||
4193 | dreg = nft_type_to_reg(set->dtype); | |
1ec10212 PM |
4194 | return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext), |
4195 | set->dtype == NFT_DATA_VERDICT ? | |
4196 | NFT_DATA_VERDICT : NFT_DATA_VALUE, | |
4197 | set->dlen); | |
20a69341 PM |
4198 | } |
4199 | ||
4200 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, | |
4201 | struct nft_set_binding *binding) | |
4202 | { | |
4203 | struct nft_set_binding *i; | |
4204 | struct nft_set_iter iter; | |
4205 | ||
273fe3f1 PNA |
4206 | if (set->use == UINT_MAX) |
4207 | return -EOVERFLOW; | |
4208 | ||
408070d6 | 4209 | if (!list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
20a69341 PM |
4210 | return -EBUSY; |
4211 | ||
11113e19 | 4212 | if (binding->flags & NFT_SET_MAP) { |
20a69341 PM |
4213 | /* If the set is already bound to the same chain all |
4214 | * jumps are already validated for that chain. | |
4215 | */ | |
4216 | list_for_each_entry(i, &set->bindings, list) { | |
a4684402 | 4217 | if (i->flags & NFT_SET_MAP && |
11113e19 | 4218 | i->chain == binding->chain) |
20a69341 PM |
4219 | goto bind; |
4220 | } | |
4221 | ||
8588ac09 | 4222 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
4223 | iter.skip = 0; |
4224 | iter.count = 0; | |
4225 | iter.err = 0; | |
4226 | iter.fn = nf_tables_bind_check_setelem; | |
4227 | ||
4228 | set->ops->walk(ctx, set, &iter); | |
a02f4248 | 4229 | if (iter.err < 0) |
20a69341 | 4230 | return iter.err; |
20a69341 PM |
4231 | } |
4232 | bind: | |
4233 | binding->chain = ctx->chain; | |
e688a7f8 | 4234 | list_add_tail_rcu(&binding->list, &set->bindings); |
f6ac8585 | 4235 | nft_set_trans_bind(ctx, set); |
273fe3f1 | 4236 | set->use++; |
f6ac8585 | 4237 | |
20a69341 PM |
4238 | return 0; |
4239 | } | |
63aea290 | 4240 | EXPORT_SYMBOL_GPL(nf_tables_bind_set); |
20a69341 | 4241 | |
3b0a081d FW |
4242 | static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, |
4243 | struct nft_set_binding *binding, bool event) | |
20a69341 | 4244 | { |
e688a7f8 | 4245 | list_del_rcu(&binding->list); |
20a69341 | 4246 | |
f6ac8585 | 4247 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) { |
cd5125d8 | 4248 | list_del_rcu(&set->list); |
f6ac8585 PNA |
4249 | if (event) |
4250 | nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, | |
4251 | GFP_KERNEL); | |
4252 | } | |
20a69341 PM |
4253 | } |
4254 | ||
273fe3f1 PNA |
4255 | void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, |
4256 | struct nft_set_binding *binding, | |
4257 | enum nft_trans_phase phase) | |
4258 | { | |
4259 | switch (phase) { | |
4260 | case NFT_TRANS_PREPARE: | |
4261 | set->use--; | |
4262 | return; | |
4263 | case NFT_TRANS_ABORT: | |
4264 | case NFT_TRANS_RELEASE: | |
4265 | set->use--; | |
4266 | /* fall through */ | |
4267 | default: | |
4268 | nf_tables_unbind_set(ctx, set, binding, | |
4269 | phase == NFT_TRANS_COMMIT); | |
4270 | } | |
4271 | } | |
4272 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_set); | |
4273 | ||
cd5125d8 FW |
4274 | void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set) |
4275 | { | |
f6ac8585 | 4276 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
cd5125d8 | 4277 | nft_set_destroy(set); |
cd5125d8 FW |
4278 | } |
4279 | EXPORT_SYMBOL_GPL(nf_tables_destroy_set); | |
4280 | ||
3ac4c07a PM |
4281 | const struct nft_set_ext_type nft_set_ext_types[] = { |
4282 | [NFT_SET_EXT_KEY] = { | |
7d740264 | 4283 | .align = __alignof__(u32), |
3ac4c07a PM |
4284 | }, |
4285 | [NFT_SET_EXT_DATA] = { | |
7d740264 | 4286 | .align = __alignof__(u32), |
3ac4c07a | 4287 | }, |
f25ad2e9 PM |
4288 | [NFT_SET_EXT_EXPR] = { |
4289 | .align = __alignof__(struct nft_expr), | |
4290 | }, | |
8aeff920 PNA |
4291 | [NFT_SET_EXT_OBJREF] = { |
4292 | .len = sizeof(struct nft_object *), | |
4293 | .align = __alignof__(struct nft_object *), | |
4294 | }, | |
3ac4c07a PM |
4295 | [NFT_SET_EXT_FLAGS] = { |
4296 | .len = sizeof(u8), | |
4297 | .align = __alignof__(u8), | |
4298 | }, | |
c3e1b005 PM |
4299 | [NFT_SET_EXT_TIMEOUT] = { |
4300 | .len = sizeof(u64), | |
4301 | .align = __alignof__(u64), | |
4302 | }, | |
4303 | [NFT_SET_EXT_EXPIRATION] = { | |
8e1102d5 FW |
4304 | .len = sizeof(u64), |
4305 | .align = __alignof__(u64), | |
c3e1b005 | 4306 | }, |
68e942e8 PM |
4307 | [NFT_SET_EXT_USERDATA] = { |
4308 | .len = sizeof(struct nft_userdata), | |
4309 | .align = __alignof__(struct nft_userdata), | |
4310 | }, | |
7b225d0b PNA |
4311 | [NFT_SET_EXT_KEY_END] = { |
4312 | .align = __alignof__(u32), | |
4313 | }, | |
3ac4c07a PM |
4314 | }; |
4315 | EXPORT_SYMBOL_GPL(nft_set_ext_types); | |
4316 | ||
20a69341 PM |
4317 | /* |
4318 | * Set elements | |
4319 | */ | |
4320 | ||
4321 | static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = { | |
4322 | [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED }, | |
4323 | [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED }, | |
4324 | [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 }, | |
c3e1b005 | 4325 | [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 }, |
79ebb5bb | 4326 | [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 }, |
68e942e8 PM |
4327 | [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY, |
4328 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 4329 | [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED }, |
9332d27d FW |
4330 | [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING, |
4331 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
7b225d0b | 4332 | [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED }, |
20a69341 PM |
4333 | }; |
4334 | ||
4335 | static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = { | |
b2fbd044 LZ |
4336 | [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING, |
4337 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
4338 | [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING, | |
4339 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
20a69341 | 4340 | [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED }, |
958bee14 | 4341 | [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 }, |
20a69341 PM |
4342 | }; |
4343 | ||
633c9a84 | 4344 | static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net, |
20a69341 PM |
4345 | const struct sk_buff *skb, |
4346 | const struct nlmsghdr *nlh, | |
f2a6d766 | 4347 | const struct nlattr * const nla[], |
36dd1bcc | 4348 | struct netlink_ext_ack *extack, |
f2a6d766 | 4349 | u8 genmask) |
20a69341 PM |
4350 | { |
4351 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
98319cb9 | 4352 | int family = nfmsg->nfgen_family; |
7c95f6d8 | 4353 | struct nft_table *table; |
20a69341 | 4354 | |
cac20fcd PNA |
4355 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
4356 | genmask); | |
36dd1bcc PNA |
4357 | if (IS_ERR(table)) { |
4358 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
20a69341 | 4359 | return PTR_ERR(table); |
36dd1bcc | 4360 | } |
20a69341 | 4361 | |
98319cb9 | 4362 | nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla); |
20a69341 PM |
4363 | return 0; |
4364 | } | |
4365 | ||
4366 | static int nf_tables_fill_setelem(struct sk_buff *skb, | |
4367 | const struct nft_set *set, | |
4368 | const struct nft_set_elem *elem) | |
4369 | { | |
fe2811eb | 4370 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
4371 | unsigned char *b = skb_tail_pointer(skb); |
4372 | struct nlattr *nest; | |
4373 | ||
ae0be8de | 4374 | nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); |
20a69341 PM |
4375 | if (nest == NULL) |
4376 | goto nla_put_failure; | |
4377 | ||
fe2811eb PM |
4378 | if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), |
4379 | NFT_DATA_VALUE, set->klen) < 0) | |
20a69341 PM |
4380 | goto nla_put_failure; |
4381 | ||
7b225d0b PNA |
4382 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && |
4383 | nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext), | |
4384 | NFT_DATA_VALUE, set->klen) < 0) | |
4385 | goto nla_put_failure; | |
4386 | ||
fe2811eb PM |
4387 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
4388 | nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext), | |
20a69341 PM |
4389 | set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE, |
4390 | set->dlen) < 0) | |
4391 | goto nla_put_failure; | |
4392 | ||
f25ad2e9 PM |
4393 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR) && |
4394 | nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, nft_set_ext_expr(ext)) < 0) | |
4395 | goto nla_put_failure; | |
4396 | ||
8aeff920 PNA |
4397 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && |
4398 | nla_put_string(skb, NFTA_SET_ELEM_OBJREF, | |
d152159b | 4399 | (*nft_set_ext_obj(ext))->key.name) < 0) |
8aeff920 PNA |
4400 | goto nla_put_failure; |
4401 | ||
fe2811eb PM |
4402 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
4403 | nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, | |
4404 | htonl(*nft_set_ext_flags(ext)))) | |
4405 | goto nla_put_failure; | |
20a69341 | 4406 | |
c3e1b005 PM |
4407 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) && |
4408 | nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT, | |
8e1102d5 | 4409 | nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)), |
b46f6ded | 4410 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
4411 | goto nla_put_failure; |
4412 | ||
4413 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { | |
8e1102d5 | 4414 | u64 expires, now = get_jiffies_64(); |
c3e1b005 PM |
4415 | |
4416 | expires = *nft_set_ext_expiration(ext); | |
8e1102d5 | 4417 | if (time_before64(now, expires)) |
c3e1b005 PM |
4418 | expires -= now; |
4419 | else | |
4420 | expires = 0; | |
4421 | ||
4422 | if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION, | |
8e1102d5 | 4423 | nf_jiffies64_to_msecs(expires), |
b46f6ded | 4424 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
4425 | goto nla_put_failure; |
4426 | } | |
4427 | ||
68e942e8 PM |
4428 | if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) { |
4429 | struct nft_userdata *udata; | |
4430 | ||
4431 | udata = nft_set_ext_userdata(ext); | |
4432 | if (nla_put(skb, NFTA_SET_ELEM_USERDATA, | |
4433 | udata->len + 1, udata->data)) | |
4434 | goto nla_put_failure; | |
4435 | } | |
4436 | ||
20a69341 PM |
4437 | nla_nest_end(skb, nest); |
4438 | return 0; | |
4439 | ||
4440 | nla_put_failure: | |
4441 | nlmsg_trim(skb, b); | |
4442 | return -EMSGSIZE; | |
4443 | } | |
4444 | ||
4445 | struct nft_set_dump_args { | |
4446 | const struct netlink_callback *cb; | |
4447 | struct nft_set_iter iter; | |
4448 | struct sk_buff *skb; | |
4449 | }; | |
4450 | ||
4451 | static int nf_tables_dump_setelem(const struct nft_ctx *ctx, | |
de70185d | 4452 | struct nft_set *set, |
20a69341 | 4453 | const struct nft_set_iter *iter, |
de70185d | 4454 | struct nft_set_elem *elem) |
20a69341 PM |
4455 | { |
4456 | struct nft_set_dump_args *args; | |
4457 | ||
4458 | args = container_of(iter, struct nft_set_dump_args, iter); | |
4459 | return nf_tables_fill_setelem(args->skb, set, elem); | |
4460 | } | |
4461 | ||
fa803605 LZ |
4462 | struct nft_set_dump_ctx { |
4463 | const struct nft_set *set; | |
4464 | struct nft_ctx ctx; | |
4465 | }; | |
4466 | ||
20a69341 PM |
4467 | static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) |
4468 | { | |
fa803605 | 4469 | struct nft_set_dump_ctx *dump_ctx = cb->data; |
633c9a84 | 4470 | struct net *net = sock_net(skb->sk); |
fa803605 | 4471 | struct nft_table *table; |
de70185d | 4472 | struct nft_set *set; |
20a69341 | 4473 | struct nft_set_dump_args args; |
fa803605 | 4474 | bool set_found = false; |
20a69341 PM |
4475 | struct nfgenmsg *nfmsg; |
4476 | struct nlmsghdr *nlh; | |
4477 | struct nlattr *nest; | |
4478 | u32 portid, seq; | |
fa803605 | 4479 | int event; |
20a69341 | 4480 | |
fa803605 | 4481 | rcu_read_lock(); |
36596dad PNA |
4482 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
4483 | if (dump_ctx->ctx.family != NFPROTO_UNSPEC && | |
98319cb9 | 4484 | dump_ctx->ctx.family != table->family) |
fa803605 | 4485 | continue; |
20a69341 | 4486 | |
36596dad PNA |
4487 | if (table != dump_ctx->ctx.table) |
4488 | continue; | |
20a69341 | 4489 | |
36596dad PNA |
4490 | list_for_each_entry_rcu(set, &table->sets, list) { |
4491 | if (set == dump_ctx->set) { | |
4492 | set_found = true; | |
4493 | break; | |
fa803605 | 4494 | } |
fa803605 LZ |
4495 | } |
4496 | break; | |
4497 | } | |
4498 | ||
4499 | if (!set_found) { | |
4500 | rcu_read_unlock(); | |
4501 | return -ENOENT; | |
4502 | } | |
20a69341 | 4503 | |
dedb67c4 | 4504 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM); |
20a69341 PM |
4505 | portid = NETLINK_CB(cb->skb).portid; |
4506 | seq = cb->nlh->nlmsg_seq; | |
4507 | ||
4508 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), | |
4509 | NLM_F_MULTI); | |
4510 | if (nlh == NULL) | |
4511 | goto nla_put_failure; | |
4512 | ||
4513 | nfmsg = nlmsg_data(nlh); | |
98319cb9 | 4514 | nfmsg->nfgen_family = table->family; |
20a69341 | 4515 | nfmsg->version = NFNETLINK_V0; |
fa803605 | 4516 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
20a69341 | 4517 | |
fa803605 | 4518 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name)) |
20a69341 PM |
4519 | goto nla_put_failure; |
4520 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name)) | |
4521 | goto nla_put_failure; | |
4522 | ||
ae0be8de | 4523 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
20a69341 PM |
4524 | if (nest == NULL) |
4525 | goto nla_put_failure; | |
4526 | ||
8588ac09 PNA |
4527 | args.cb = cb; |
4528 | args.skb = skb; | |
fa803605 | 4529 | args.iter.genmask = nft_genmask_cur(net); |
8588ac09 PNA |
4530 | args.iter.skip = cb->args[0]; |
4531 | args.iter.count = 0; | |
4532 | args.iter.err = 0; | |
4533 | args.iter.fn = nf_tables_dump_setelem; | |
fa803605 LZ |
4534 | set->ops->walk(&dump_ctx->ctx, set, &args.iter); |
4535 | rcu_read_unlock(); | |
20a69341 PM |
4536 | |
4537 | nla_nest_end(skb, nest); | |
4538 | nlmsg_end(skb, nlh); | |
4539 | ||
4540 | if (args.iter.err && args.iter.err != -EMSGSIZE) | |
4541 | return args.iter.err; | |
4542 | if (args.iter.count == cb->args[0]) | |
4543 | return 0; | |
4544 | ||
4545 | cb->args[0] = args.iter.count; | |
4546 | return skb->len; | |
4547 | ||
4548 | nla_put_failure: | |
fa803605 | 4549 | rcu_read_unlock(); |
20a69341 PM |
4550 | return -ENOSPC; |
4551 | } | |
4552 | ||
90fd131a FW |
4553 | static int nf_tables_dump_set_start(struct netlink_callback *cb) |
4554 | { | |
4555 | struct nft_set_dump_ctx *dump_ctx = cb->data; | |
4556 | ||
4557 | cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC); | |
4558 | ||
4559 | return cb->data ? 0 : -ENOMEM; | |
4560 | } | |
4561 | ||
fa803605 LZ |
4562 | static int nf_tables_dump_set_done(struct netlink_callback *cb) |
4563 | { | |
4564 | kfree(cb->data); | |
4565 | return 0; | |
4566 | } | |
4567 | ||
d60ce62f AB |
4568 | static int nf_tables_fill_setelem_info(struct sk_buff *skb, |
4569 | const struct nft_ctx *ctx, u32 seq, | |
4570 | u32 portid, int event, u16 flags, | |
4571 | const struct nft_set *set, | |
4572 | const struct nft_set_elem *elem) | |
4573 | { | |
4574 | struct nfgenmsg *nfmsg; | |
4575 | struct nlmsghdr *nlh; | |
4576 | struct nlattr *nest; | |
4577 | int err; | |
4578 | ||
dedb67c4 | 4579 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
d60ce62f AB |
4580 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), |
4581 | flags); | |
4582 | if (nlh == NULL) | |
4583 | goto nla_put_failure; | |
4584 | ||
4585 | nfmsg = nlmsg_data(nlh); | |
36596dad | 4586 | nfmsg->nfgen_family = ctx->family; |
d60ce62f | 4587 | nfmsg->version = NFNETLINK_V0; |
84d7fce6 | 4588 | nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff); |
d60ce62f AB |
4589 | |
4590 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) | |
4591 | goto nla_put_failure; | |
4592 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
4593 | goto nla_put_failure; | |
4594 | ||
ae0be8de | 4595 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
d60ce62f AB |
4596 | if (nest == NULL) |
4597 | goto nla_put_failure; | |
4598 | ||
4599 | err = nf_tables_fill_setelem(skb, set, elem); | |
4600 | if (err < 0) | |
4601 | goto nla_put_failure; | |
4602 | ||
4603 | nla_nest_end(skb, nest); | |
4604 | ||
053c095a JB |
4605 | nlmsg_end(skb, nlh); |
4606 | return 0; | |
d60ce62f AB |
4607 | |
4608 | nla_put_failure: | |
4609 | nlmsg_trim(skb, nlh); | |
4610 | return -1; | |
4611 | } | |
4612 | ||
ba0e4d99 PNA |
4613 | static int nft_setelem_parse_flags(const struct nft_set *set, |
4614 | const struct nlattr *attr, u32 *flags) | |
4615 | { | |
4616 | if (attr == NULL) | |
4617 | return 0; | |
4618 | ||
4619 | *flags = ntohl(nla_get_be32(attr)); | |
4620 | if (*flags & ~NFT_SET_ELEM_INTERVAL_END) | |
4621 | return -EINVAL; | |
4622 | if (!(set->flags & NFT_SET_INTERVAL) && | |
4623 | *flags & NFT_SET_ELEM_INTERVAL_END) | |
4624 | return -EINVAL; | |
4625 | ||
4626 | return 0; | |
4627 | } | |
4628 | ||
20a1452c PNA |
4629 | static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set, |
4630 | struct nft_data *key, struct nlattr *attr) | |
4631 | { | |
4632 | struct nft_data_desc desc; | |
4633 | int err; | |
4634 | ||
4635 | err = nft_data_init(ctx, key, NFT_DATA_VALUE_MAXLEN, &desc, attr); | |
4636 | if (err < 0) | |
4637 | return err; | |
4638 | ||
4639 | if (desc.type != NFT_DATA_VALUE || desc.len != set->klen) { | |
4640 | nft_data_release(key, desc.type); | |
4641 | return -EINVAL; | |
4642 | } | |
4643 | ||
4644 | return 0; | |
4645 | } | |
4646 | ||
ba0e4d99 PNA |
4647 | static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
4648 | const struct nlattr *attr) | |
4649 | { | |
4650 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
ba0e4d99 PNA |
4651 | struct nft_set_elem elem; |
4652 | struct sk_buff *skb; | |
4653 | uint32_t flags = 0; | |
4654 | void *priv; | |
4655 | int err; | |
4656 | ||
8cb08174 JB |
4657 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
4658 | nft_set_elem_policy, NULL); | |
ba0e4d99 PNA |
4659 | if (err < 0) |
4660 | return err; | |
4661 | ||
4662 | if (!nla[NFTA_SET_ELEM_KEY]) | |
4663 | return -EINVAL; | |
4664 | ||
4665 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); | |
4666 | if (err < 0) | |
4667 | return err; | |
4668 | ||
20a1452c PNA |
4669 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, |
4670 | nla[NFTA_SET_ELEM_KEY]); | |
ba0e4d99 PNA |
4671 | if (err < 0) |
4672 | return err; | |
4673 | ||
7b225d0b PNA |
4674 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
4675 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
4676 | nla[NFTA_SET_ELEM_KEY_END]); | |
4677 | if (err < 0) | |
4678 | return err; | |
4679 | } | |
4680 | ||
ba0e4d99 PNA |
4681 | priv = set->ops->get(ctx->net, set, &elem, flags); |
4682 | if (IS_ERR(priv)) | |
4683 | return PTR_ERR(priv); | |
4684 | ||
4685 | elem.priv = priv; | |
ba0e4d99 PNA |
4686 | |
4687 | err = -ENOMEM; | |
d9adf22a | 4688 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
ba0e4d99 PNA |
4689 | if (skb == NULL) |
4690 | goto err1; | |
4691 | ||
4692 | err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid, | |
4693 | NFT_MSG_NEWSETELEM, 0, set, &elem); | |
4694 | if (err < 0) | |
4695 | goto err2; | |
4696 | ||
4697 | err = nfnetlink_unicast(skb, ctx->net, ctx->portid, MSG_DONTWAIT); | |
4698 | /* This avoids a loop in nfnetlink. */ | |
4699 | if (err < 0) | |
4700 | goto err1; | |
4701 | ||
4702 | return 0; | |
4703 | err2: | |
4704 | kfree_skb(skb); | |
4705 | err1: | |
4706 | /* this avoids a loop in nfnetlink. */ | |
4707 | return err == -EAGAIN ? -ENOBUFS : err; | |
4708 | } | |
4709 | ||
d9adf22a | 4710 | /* called with rcu_read_lock held */ |
ba0e4d99 PNA |
4711 | static int nf_tables_getsetelem(struct net *net, struct sock *nlsk, |
4712 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
4713 | const struct nlattr * const nla[], | |
4714 | struct netlink_ext_ack *extack) | |
4715 | { | |
4716 | u8 genmask = nft_genmask_cur(net); | |
4717 | struct nft_set *set; | |
4718 | struct nlattr *attr; | |
4719 | struct nft_ctx ctx; | |
4720 | int rem, err = 0; | |
4721 | ||
36dd1bcc PNA |
4722 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack, |
4723 | genmask); | |
ba0e4d99 PNA |
4724 | if (err < 0) |
4725 | return err; | |
4726 | ||
cac20fcd | 4727 | set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
ba0e4d99 PNA |
4728 | if (IS_ERR(set)) |
4729 | return PTR_ERR(set); | |
4730 | ||
4731 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
4732 | struct netlink_dump_control c = { | |
90fd131a | 4733 | .start = nf_tables_dump_set_start, |
ba0e4d99 PNA |
4734 | .dump = nf_tables_dump_set, |
4735 | .done = nf_tables_dump_set_done, | |
d9adf22a | 4736 | .module = THIS_MODULE, |
ba0e4d99 | 4737 | }; |
90fd131a FW |
4738 | struct nft_set_dump_ctx dump_ctx = { |
4739 | .set = set, | |
4740 | .ctx = ctx, | |
4741 | }; | |
ba0e4d99 | 4742 | |
90fd131a | 4743 | c.data = &dump_ctx; |
d9adf22a | 4744 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
ba0e4d99 PNA |
4745 | } |
4746 | ||
4747 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) | |
4748 | return -EINVAL; | |
4749 | ||
4750 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { | |
4751 | err = nft_get_set_elem(&ctx, set, attr); | |
4752 | if (err < 0) | |
4753 | break; | |
4754 | } | |
4755 | ||
4756 | return err; | |
4757 | } | |
4758 | ||
25e94a99 PNA |
4759 | static void nf_tables_setelem_notify(const struct nft_ctx *ctx, |
4760 | const struct nft_set *set, | |
4761 | const struct nft_set_elem *elem, | |
4762 | int event, u16 flags) | |
d60ce62f | 4763 | { |
128ad332 PNA |
4764 | struct net *net = ctx->net; |
4765 | u32 portid = ctx->portid; | |
d60ce62f AB |
4766 | struct sk_buff *skb; |
4767 | int err; | |
4768 | ||
128ad332 | 4769 | if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 4770 | return; |
d60ce62f | 4771 | |
d60ce62f AB |
4772 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
4773 | if (skb == NULL) | |
4774 | goto err; | |
4775 | ||
4776 | err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags, | |
4777 | set, elem); | |
4778 | if (err < 0) { | |
4779 | kfree_skb(skb); | |
4780 | goto err; | |
4781 | } | |
4782 | ||
25e94a99 PNA |
4783 | nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report, |
4784 | GFP_KERNEL); | |
4785 | return; | |
d60ce62f | 4786 | err: |
25e94a99 | 4787 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
d60ce62f AB |
4788 | } |
4789 | ||
60319eb1 PNA |
4790 | static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx, |
4791 | int msg_type, | |
4792 | struct nft_set *set) | |
4793 | { | |
4794 | struct nft_trans *trans; | |
4795 | ||
4796 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem)); | |
4797 | if (trans == NULL) | |
4798 | return NULL; | |
4799 | ||
4800 | nft_trans_elem_set(trans) = set; | |
4801 | return trans; | |
4802 | } | |
4803 | ||
22fe54d5 PM |
4804 | void *nft_set_elem_init(const struct nft_set *set, |
4805 | const struct nft_set_ext_tmpl *tmpl, | |
7b225d0b PNA |
4806 | const u32 *key, const u32 *key_end, |
4807 | const u32 *data, u64 timeout, u64 expiration, gfp_t gfp) | |
fe2811eb PM |
4808 | { |
4809 | struct nft_set_ext *ext; | |
4810 | void *elem; | |
4811 | ||
4812 | elem = kzalloc(set->ops->elemsize + tmpl->len, gfp); | |
4813 | if (elem == NULL) | |
4814 | return NULL; | |
4815 | ||
4816 | ext = nft_set_elem_ext(set, elem); | |
4817 | nft_set_ext_init(ext, tmpl); | |
4818 | ||
4819 | memcpy(nft_set_ext_key(ext), key, set->klen); | |
7b225d0b PNA |
4820 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END)) |
4821 | memcpy(nft_set_ext_key_end(ext), key_end, set->klen); | |
fe2811eb PM |
4822 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
4823 | memcpy(nft_set_ext_data(ext), data, set->dlen); | |
79ebb5bb LGL |
4824 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { |
4825 | *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration; | |
4826 | if (expiration == 0) | |
4827 | *nft_set_ext_expiration(ext) += timeout; | |
4828 | } | |
c3e1b005 PM |
4829 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT)) |
4830 | *nft_set_ext_timeout(ext) = timeout; | |
fe2811eb PM |
4831 | |
4832 | return elem; | |
4833 | } | |
4834 | ||
61f9e292 LZ |
4835 | void nft_set_elem_destroy(const struct nft_set *set, void *elem, |
4836 | bool destroy_expr) | |
61edafbb PM |
4837 | { |
4838 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
3453c927 PNA |
4839 | struct nft_ctx ctx = { |
4840 | .net = read_pnet(&set->net), | |
4841 | .family = set->table->family, | |
4842 | }; | |
61edafbb | 4843 | |
59105446 | 4844 | nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE); |
61edafbb | 4845 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
59105446 | 4846 | nft_data_release(nft_set_ext_data(ext), set->dtype); |
371ebcbb PNA |
4847 | if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) { |
4848 | struct nft_expr *expr = nft_set_ext_expr(ext); | |
4849 | ||
4850 | if (expr->ops->destroy_clone) { | |
4851 | expr->ops->destroy_clone(&ctx, expr); | |
4852 | module_put(expr->ops->type->owner); | |
4853 | } else { | |
4854 | nf_tables_expr_destroy(&ctx, expr); | |
4855 | } | |
4856 | } | |
8aeff920 PNA |
4857 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) |
4858 | (*nft_set_ext_obj(ext))->use--; | |
61edafbb PM |
4859 | kfree(elem); |
4860 | } | |
4861 | EXPORT_SYMBOL_GPL(nft_set_elem_destroy); | |
4862 | ||
59105446 PNA |
4863 | /* Only called from commit path, nft_set_elem_deactivate() already deals with |
4864 | * the refcounting from the preparation phase. | |
4865 | */ | |
3453c927 PNA |
4866 | static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx, |
4867 | const struct nft_set *set, void *elem) | |
59105446 PNA |
4868 | { |
4869 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
4870 | ||
4871 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) | |
3453c927 | 4872 | nf_tables_expr_destroy(ctx, nft_set_ext_expr(ext)); |
59105446 PNA |
4873 | kfree(elem); |
4874 | } | |
4875 | ||
60319eb1 | 4876 | static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
c016c7e4 | 4877 | const struct nlattr *attr, u32 nlmsg_flags) |
20a69341 PM |
4878 | { |
4879 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
8aeff920 | 4880 | u8 genmask = nft_genmask_next(ctx->net); |
fe2811eb | 4881 | struct nft_set_ext_tmpl tmpl; |
c016c7e4 | 4882 | struct nft_set_ext *ext, *ext2; |
20a69341 PM |
4883 | struct nft_set_elem elem; |
4884 | struct nft_set_binding *binding; | |
8aeff920 | 4885 | struct nft_object *obj = NULL; |
68e942e8 | 4886 | struct nft_userdata *udata; |
20a1452c | 4887 | struct nft_data_desc desc; |
fe2811eb | 4888 | struct nft_data data; |
20a69341 | 4889 | enum nft_registers dreg; |
60319eb1 | 4890 | struct nft_trans *trans; |
0e9091d6 | 4891 | u32 flags = 0; |
c3e1b005 | 4892 | u64 timeout; |
79ebb5bb | 4893 | u64 expiration; |
68e942e8 | 4894 | u8 ulen; |
20a69341 PM |
4895 | int err; |
4896 | ||
8cb08174 JB |
4897 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
4898 | nft_set_elem_policy, NULL); | |
20a69341 PM |
4899 | if (err < 0) |
4900 | return err; | |
4901 | ||
4902 | if (nla[NFTA_SET_ELEM_KEY] == NULL) | |
4903 | return -EINVAL; | |
4904 | ||
fe2811eb PM |
4905 | nft_set_ext_prepare(&tmpl); |
4906 | ||
0e9091d6 PNA |
4907 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
4908 | if (err < 0) | |
4909 | return err; | |
4910 | if (flags != 0) | |
4911 | nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
20a69341 PM |
4912 | |
4913 | if (set->flags & NFT_SET_MAP) { | |
4914 | if (nla[NFTA_SET_ELEM_DATA] == NULL && | |
fe2811eb | 4915 | !(flags & NFT_SET_ELEM_INTERVAL_END)) |
20a69341 PM |
4916 | return -EINVAL; |
4917 | } else { | |
4918 | if (nla[NFTA_SET_ELEM_DATA] != NULL) | |
4919 | return -EINVAL; | |
4920 | } | |
4921 | ||
bffc124b PNA |
4922 | if ((flags & NFT_SET_ELEM_INTERVAL_END) && |
4923 | (nla[NFTA_SET_ELEM_DATA] || | |
4924 | nla[NFTA_SET_ELEM_OBJREF] || | |
4925 | nla[NFTA_SET_ELEM_TIMEOUT] || | |
4926 | nla[NFTA_SET_ELEM_EXPIRATION] || | |
4927 | nla[NFTA_SET_ELEM_USERDATA] || | |
4928 | nla[NFTA_SET_ELEM_EXPR])) | |
4929 | return -EINVAL; | |
4930 | ||
c3e1b005 PM |
4931 | timeout = 0; |
4932 | if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) { | |
4933 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
4934 | return -EINVAL; | |
8e1102d5 FW |
4935 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT], |
4936 | &timeout); | |
4937 | if (err) | |
4938 | return err; | |
c3e1b005 PM |
4939 | } else if (set->flags & NFT_SET_TIMEOUT) { |
4940 | timeout = set->timeout; | |
4941 | } | |
4942 | ||
79ebb5bb LGL |
4943 | expiration = 0; |
4944 | if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) { | |
4945 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
4946 | return -EINVAL; | |
4947 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION], | |
4948 | &expiration); | |
4949 | if (err) | |
4950 | return err; | |
4951 | } | |
4952 | ||
20a1452c PNA |
4953 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, |
4954 | nla[NFTA_SET_ELEM_KEY]); | |
20a69341 | 4955 | if (err < 0) |
7b225d0b | 4956 | return err; |
20a69341 | 4957 | |
20a1452c | 4958 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
7b225d0b PNA |
4959 | |
4960 | if (nla[NFTA_SET_ELEM_KEY_END]) { | |
4961 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
4962 | nla[NFTA_SET_ELEM_KEY_END]); | |
4963 | if (err < 0) | |
4964 | goto err_parse_key; | |
4965 | ||
4966 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); | |
4967 | } | |
4968 | ||
c3e1b005 PM |
4969 | if (timeout > 0) { |
4970 | nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION); | |
4971 | if (timeout != set->timeout) | |
4972 | nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT); | |
4973 | } | |
fe2811eb | 4974 | |
8aeff920 PNA |
4975 | if (nla[NFTA_SET_ELEM_OBJREF] != NULL) { |
4976 | if (!(set->flags & NFT_SET_OBJECT)) { | |
4977 | err = -EINVAL; | |
7b225d0b | 4978 | goto err_parse_key_end; |
8aeff920 | 4979 | } |
4d44175a FW |
4980 | obj = nft_obj_lookup(ctx->net, ctx->table, |
4981 | nla[NFTA_SET_ELEM_OBJREF], | |
cac20fcd | 4982 | set->objtype, genmask); |
8aeff920 PNA |
4983 | if (IS_ERR(obj)) { |
4984 | err = PTR_ERR(obj); | |
7b225d0b | 4985 | goto err_parse_key_end; |
8aeff920 PNA |
4986 | } |
4987 | nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF); | |
4988 | } | |
4989 | ||
20a69341 | 4990 | if (nla[NFTA_SET_ELEM_DATA] != NULL) { |
20a1452c | 4991 | err = nft_data_init(ctx, &data, sizeof(data), &desc, |
d0a11fc3 | 4992 | nla[NFTA_SET_ELEM_DATA]); |
20a69341 | 4993 | if (err < 0) |
7b225d0b | 4994 | goto err_parse_key_end; |
20a69341 PM |
4995 | |
4996 | err = -EINVAL; | |
20a1452c | 4997 | if (set->dtype != NFT_DATA_VERDICT && desc.len != set->dlen) |
7b225d0b | 4998 | goto err_parse_data; |
20a69341 PM |
4999 | |
5000 | dreg = nft_type_to_reg(set->dtype); | |
5001 | list_for_each_entry(binding, &set->bindings, list) { | |
5002 | struct nft_ctx bind_ctx = { | |
58c78e10 | 5003 | .net = ctx->net, |
36596dad | 5004 | .family = ctx->family, |
20a69341 | 5005 | .table = ctx->table, |
7c95f6d8 | 5006 | .chain = (struct nft_chain *)binding->chain, |
20a69341 PM |
5007 | }; |
5008 | ||
11113e19 PM |
5009 | if (!(binding->flags & NFT_SET_MAP)) |
5010 | continue; | |
5011 | ||
1ec10212 PM |
5012 | err = nft_validate_register_store(&bind_ctx, dreg, |
5013 | &data, | |
20a1452c | 5014 | desc.type, desc.len); |
20a69341 | 5015 | if (err < 0) |
7b225d0b | 5016 | goto err_parse_data; |
a654de8f | 5017 | |
20a1452c | 5018 | if (desc.type == NFT_DATA_VERDICT && |
a654de8f PNA |
5019 | (data.verdict.code == NFT_GOTO || |
5020 | data.verdict.code == NFT_JUMP)) | |
5021 | nft_validate_state_update(ctx->net, | |
5022 | NFT_VALIDATE_NEED); | |
20a69341 | 5023 | } |
fe2811eb | 5024 | |
20a1452c | 5025 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len); |
20a69341 PM |
5026 | } |
5027 | ||
68e942e8 PM |
5028 | /* The full maximum length of userdata can exceed the maximum |
5029 | * offset value (U8_MAX) for following extensions, therefor it | |
5030 | * must be the last extension added. | |
5031 | */ | |
5032 | ulen = 0; | |
5033 | if (nla[NFTA_SET_ELEM_USERDATA] != NULL) { | |
5034 | ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]); | |
5035 | if (ulen > 0) | |
5036 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA, | |
5037 | ulen); | |
5038 | } | |
5039 | ||
fe2811eb | 5040 | err = -ENOMEM; |
7b225d0b PNA |
5041 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
5042 | elem.key_end.val.data, data.data, | |
79ebb5bb | 5043 | timeout, expiration, GFP_KERNEL); |
fe2811eb | 5044 | if (elem.priv == NULL) |
7b225d0b | 5045 | goto err_parse_data; |
fe2811eb PM |
5046 | |
5047 | ext = nft_set_elem_ext(set, elem.priv); | |
5048 | if (flags) | |
5049 | *nft_set_ext_flags(ext) = flags; | |
68e942e8 PM |
5050 | if (ulen > 0) { |
5051 | udata = nft_set_ext_userdata(ext); | |
5052 | udata->len = ulen - 1; | |
5053 | nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen); | |
5054 | } | |
8aeff920 PNA |
5055 | if (obj) { |
5056 | *nft_set_ext_obj(ext) = obj; | |
5057 | obj->use++; | |
5058 | } | |
fe2811eb | 5059 | |
60319eb1 PNA |
5060 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set); |
5061 | if (trans == NULL) | |
7b225d0b | 5062 | goto err_trans; |
60319eb1 | 5063 | |
69086658 | 5064 | ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK; |
c016c7e4 PNA |
5065 | err = set->ops->insert(ctx->net, set, &elem, &ext2); |
5066 | if (err) { | |
5067 | if (err == -EEXIST) { | |
9744a6fc PNA |
5068 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^ |
5069 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) || | |
5070 | nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^ | |
f0dfd7a2 CIK |
5071 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF)) { |
5072 | err = -EBUSY; | |
7b225d0b | 5073 | goto err_element_clash; |
f0dfd7a2 | 5074 | } |
8aeff920 PNA |
5075 | if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
5076 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) && | |
5077 | memcmp(nft_set_ext_data(ext), | |
5078 | nft_set_ext_data(ext2), set->dlen) != 0) || | |
5079 | (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && | |
5080 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) && | |
5081 | *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2))) | |
c016c7e4 PNA |
5082 | err = -EBUSY; |
5083 | else if (!(nlmsg_flags & NLM_F_EXCL)) | |
5084 | err = 0; | |
5085 | } | |
7b225d0b | 5086 | goto err_element_clash; |
c016c7e4 | 5087 | } |
20a69341 | 5088 | |
35d0ac90 PNA |
5089 | if (set->size && |
5090 | !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) { | |
5091 | err = -ENFILE; | |
7b225d0b | 5092 | goto err_set_full; |
35d0ac90 PNA |
5093 | } |
5094 | ||
60319eb1 | 5095 | nft_trans_elem(trans) = elem; |
46bbafce | 5096 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
20a69341 PM |
5097 | return 0; |
5098 | ||
7b225d0b | 5099 | err_set_full: |
5cb82a38 | 5100 | set->ops->remove(ctx->net, set, &elem); |
7b225d0b | 5101 | err_element_clash: |
60319eb1 | 5102 | kfree(trans); |
7b225d0b | 5103 | err_trans: |
b91d9036 TY |
5104 | if (obj) |
5105 | obj->use--; | |
fe2811eb | 5106 | kfree(elem.priv); |
7b225d0b | 5107 | err_parse_data: |
20a69341 | 5108 | if (nla[NFTA_SET_ELEM_DATA] != NULL) |
20a1452c | 5109 | nft_data_release(&data, desc.type); |
7b225d0b PNA |
5110 | err_parse_key_end: |
5111 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
5112 | err_parse_key: | |
20a1452c | 5113 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); |
7b225d0b | 5114 | |
20a69341 PM |
5115 | return err; |
5116 | } | |
5117 | ||
633c9a84 PNA |
5118 | static int nf_tables_newsetelem(struct net *net, struct sock *nlsk, |
5119 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
5120 | const struct nlattr * const nla[], |
5121 | struct netlink_ext_ack *extack) | |
20a69341 | 5122 | { |
f2a6d766 | 5123 | u8 genmask = nft_genmask_next(net); |
20a69341 PM |
5124 | const struct nlattr *attr; |
5125 | struct nft_set *set; | |
5126 | struct nft_ctx ctx; | |
a654de8f | 5127 | int rem, err; |
20a69341 | 5128 | |
7d5570ca PNA |
5129 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) |
5130 | return -EINVAL; | |
5131 | ||
36dd1bcc PNA |
5132 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack, |
5133 | genmask); | |
20a69341 PM |
5134 | if (err < 0) |
5135 | return err; | |
5136 | ||
a3073c17 PNA |
5137 | set = nft_set_lookup_global(net, ctx.table, nla[NFTA_SET_ELEM_LIST_SET], |
5138 | nla[NFTA_SET_ELEM_LIST_SET_ID], genmask); | |
5139 | if (IS_ERR(set)) | |
5140 | return PTR_ERR(set); | |
958bee14 | 5141 | |
20a69341 PM |
5142 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) |
5143 | return -EBUSY; | |
5144 | ||
5145 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { | |
c016c7e4 | 5146 | err = nft_add_set_elem(&ctx, set, attr, nlh->nlmsg_flags); |
35d0ac90 | 5147 | if (err < 0) |
a654de8f | 5148 | return err; |
20a69341 | 5149 | } |
a654de8f PNA |
5150 | |
5151 | if (net->nft.validate_state == NFT_VALIDATE_DO) | |
5152 | return nft_table_validate(net, ctx.table); | |
5153 | ||
5154 | return 0; | |
20a69341 PM |
5155 | } |
5156 | ||
59105446 PNA |
5157 | /** |
5158 | * nft_data_hold - hold a nft_data item | |
5159 | * | |
5160 | * @data: struct nft_data to release | |
5161 | * @type: type of data | |
5162 | * | |
5163 | * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
5164 | * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and | |
5165 | * NFT_GOTO verdicts. This function must be called on active data objects | |
5166 | * from the second phase of the commit protocol. | |
5167 | */ | |
bb7b40ae | 5168 | void nft_data_hold(const struct nft_data *data, enum nft_data_types type) |
59105446 PNA |
5169 | { |
5170 | if (type == NFT_DATA_VERDICT) { | |
5171 | switch (data->verdict.code) { | |
5172 | case NFT_JUMP: | |
5173 | case NFT_GOTO: | |
5174 | data->verdict.chain->use++; | |
5175 | break; | |
5176 | } | |
5177 | } | |
5178 | } | |
5179 | ||
5180 | static void nft_set_elem_activate(const struct net *net, | |
5181 | const struct nft_set *set, | |
5182 | struct nft_set_elem *elem) | |
5183 | { | |
5184 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5185 | ||
5186 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
5187 | nft_data_hold(nft_set_ext_data(ext), set->dtype); | |
5188 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
5189 | (*nft_set_ext_obj(ext))->use++; | |
5190 | } | |
5191 | ||
5192 | static void nft_set_elem_deactivate(const struct net *net, | |
5193 | const struct nft_set *set, | |
5194 | struct nft_set_elem *elem) | |
5195 | { | |
5196 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5197 | ||
5198 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
5199 | nft_data_release(nft_set_ext_data(ext), set->dtype); | |
5200 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
5201 | (*nft_set_ext_obj(ext))->use--; | |
5202 | } | |
5203 | ||
60319eb1 | 5204 | static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, |
20a69341 PM |
5205 | const struct nlattr *attr) |
5206 | { | |
5207 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
3971ca14 | 5208 | struct nft_set_ext_tmpl tmpl; |
20a69341 | 5209 | struct nft_set_elem elem; |
3971ca14 | 5210 | struct nft_set_ext *ext; |
60319eb1 | 5211 | struct nft_trans *trans; |
3971ca14 PNA |
5212 | u32 flags = 0; |
5213 | void *priv; | |
20a69341 PM |
5214 | int err; |
5215 | ||
8cb08174 JB |
5216 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
5217 | nft_set_elem_policy, NULL); | |
20a69341 | 5218 | if (err < 0) |
20a1452c | 5219 | return err; |
20a69341 | 5220 | |
20a69341 | 5221 | if (nla[NFTA_SET_ELEM_KEY] == NULL) |
20a1452c | 5222 | return -EINVAL; |
20a69341 | 5223 | |
3971ca14 PNA |
5224 | nft_set_ext_prepare(&tmpl); |
5225 | ||
5226 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); | |
5227 | if (err < 0) | |
5228 | return err; | |
5229 | if (flags != 0) | |
5230 | nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
5231 | ||
20a1452c PNA |
5232 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, |
5233 | nla[NFTA_SET_ELEM_KEY]); | |
20a69341 | 5234 | if (err < 0) |
20a1452c | 5235 | return err; |
20a69341 | 5236 | |
20a1452c | 5237 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
3971ca14 | 5238 | |
7b225d0b PNA |
5239 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
5240 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
5241 | nla[NFTA_SET_ELEM_KEY_END]); | |
5242 | if (err < 0) | |
5243 | return err; | |
5244 | ||
5245 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); | |
5246 | } | |
5247 | ||
3971ca14 | 5248 | err = -ENOMEM; |
7b225d0b PNA |
5249 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
5250 | elem.key_end.val.data, NULL, 0, 0, | |
5251 | GFP_KERNEL); | |
3971ca14 | 5252 | if (elem.priv == NULL) |
20a1452c | 5253 | goto fail_elem; |
3971ca14 PNA |
5254 | |
5255 | ext = nft_set_elem_ext(set, elem.priv); | |
5256 | if (flags) | |
5257 | *nft_set_ext_flags(ext) = flags; | |
5258 | ||
60319eb1 | 5259 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); |
20a1452c PNA |
5260 | if (trans == NULL) |
5261 | goto fail_trans; | |
20a69341 | 5262 | |
42a55769 | 5263 | priv = set->ops->deactivate(ctx->net, set, &elem); |
3971ca14 | 5264 | if (priv == NULL) { |
cc02e457 | 5265 | err = -ENOENT; |
20a1452c | 5266 | goto fail_ops; |
cc02e457 | 5267 | } |
3971ca14 PNA |
5268 | kfree(elem.priv); |
5269 | elem.priv = priv; | |
cc02e457 | 5270 | |
59105446 PNA |
5271 | nft_set_elem_deactivate(ctx->net, set, &elem); |
5272 | ||
60319eb1 | 5273 | nft_trans_elem(trans) = elem; |
46bbafce | 5274 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
0dc13625 | 5275 | return 0; |
cc02e457 | 5276 | |
20a1452c | 5277 | fail_ops: |
cc02e457 | 5278 | kfree(trans); |
20a1452c | 5279 | fail_trans: |
3971ca14 | 5280 | kfree(elem.priv); |
20a1452c PNA |
5281 | fail_elem: |
5282 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); | |
20a69341 PM |
5283 | return err; |
5284 | } | |
5285 | ||
8411b644 | 5286 | static int nft_flush_set(const struct nft_ctx *ctx, |
de70185d | 5287 | struct nft_set *set, |
8411b644 | 5288 | const struct nft_set_iter *iter, |
de70185d | 5289 | struct nft_set_elem *elem) |
8411b644 PNA |
5290 | { |
5291 | struct nft_trans *trans; | |
5292 | int err; | |
5293 | ||
5294 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
5295 | sizeof(struct nft_trans_elem), GFP_ATOMIC); | |
5296 | if (!trans) | |
5297 | return -ENOMEM; | |
5298 | ||
1ba1c414 | 5299 | if (!set->ops->flush(ctx->net, set, elem->priv)) { |
8411b644 PNA |
5300 | err = -ENOENT; |
5301 | goto err1; | |
5302 | } | |
b2c11e4b | 5303 | set->ndeact++; |
8411b644 | 5304 | |
7acfda53 | 5305 | nft_set_elem_deactivate(ctx->net, set, elem); |
de70185d PNA |
5306 | nft_trans_elem_set(trans) = set; |
5307 | nft_trans_elem(trans) = *elem; | |
8411b644 PNA |
5308 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
5309 | ||
5310 | return 0; | |
5311 | err1: | |
5312 | kfree(trans); | |
5313 | return err; | |
5314 | } | |
5315 | ||
633c9a84 PNA |
5316 | static int nf_tables_delsetelem(struct net *net, struct sock *nlsk, |
5317 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
5318 | const struct nlattr * const nla[], |
5319 | struct netlink_ext_ack *extack) | |
20a69341 | 5320 | { |
f2a6d766 | 5321 | u8 genmask = nft_genmask_next(net); |
20a69341 PM |
5322 | const struct nlattr *attr; |
5323 | struct nft_set *set; | |
5324 | struct nft_ctx ctx; | |
60319eb1 | 5325 | int rem, err = 0; |
20a69341 | 5326 | |
36dd1bcc PNA |
5327 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack, |
5328 | genmask); | |
20a69341 PM |
5329 | if (err < 0) |
5330 | return err; | |
5331 | ||
cac20fcd | 5332 | set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
20a69341 PM |
5333 | if (IS_ERR(set)) |
5334 | return PTR_ERR(set); | |
5335 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) | |
5336 | return -EBUSY; | |
5337 | ||
8411b644 | 5338 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) { |
baa2d42c PNA |
5339 | struct nft_set_iter iter = { |
5340 | .genmask = genmask, | |
5341 | .fn = nft_flush_set, | |
8411b644 | 5342 | }; |
baa2d42c | 5343 | set->ops->walk(&ctx, set, &iter); |
8411b644 | 5344 | |
baa2d42c | 5345 | return iter.err; |
8411b644 PNA |
5346 | } |
5347 | ||
20a69341 PM |
5348 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
5349 | err = nft_del_setelem(&ctx, set, attr); | |
5350 | if (err < 0) | |
60319eb1 | 5351 | break; |
4fefee57 | 5352 | |
3dd0673a | 5353 | set->ndeact++; |
20a69341 | 5354 | } |
60319eb1 | 5355 | return err; |
20a69341 PM |
5356 | } |
5357 | ||
cfed7e1b PM |
5358 | void nft_set_gc_batch_release(struct rcu_head *rcu) |
5359 | { | |
5360 | struct nft_set_gc_batch *gcb; | |
5361 | unsigned int i; | |
5362 | ||
5363 | gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu); | |
5364 | for (i = 0; i < gcb->head.cnt; i++) | |
61f9e292 | 5365 | nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true); |
cfed7e1b PM |
5366 | kfree(gcb); |
5367 | } | |
5368 | EXPORT_SYMBOL_GPL(nft_set_gc_batch_release); | |
5369 | ||
5370 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, | |
5371 | gfp_t gfp) | |
5372 | { | |
5373 | struct nft_set_gc_batch *gcb; | |
5374 | ||
5375 | gcb = kzalloc(sizeof(*gcb), gfp); | |
5376 | if (gcb == NULL) | |
5377 | return gcb; | |
5378 | gcb->head.set = set; | |
5379 | return gcb; | |
5380 | } | |
5381 | EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc); | |
5382 | ||
e5009240 PNA |
5383 | /* |
5384 | * Stateful objects | |
5385 | */ | |
5386 | ||
5387 | /** | |
5388 | * nft_register_obj- register nf_tables stateful object type | |
5389 | * @obj: object type | |
5390 | * | |
5391 | * Registers the object type for use with nf_tables. Returns zero on | |
5392 | * success or a negative errno code otherwise. | |
5393 | */ | |
5394 | int nft_register_obj(struct nft_object_type *obj_type) | |
5395 | { | |
5396 | if (obj_type->type == NFT_OBJECT_UNSPEC) | |
5397 | return -EINVAL; | |
5398 | ||
5399 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
5400 | list_add_rcu(&obj_type->list, &nf_tables_objects); | |
5401 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
5402 | return 0; | |
5403 | } | |
5404 | EXPORT_SYMBOL_GPL(nft_register_obj); | |
5405 | ||
5406 | /** | |
5407 | * nft_unregister_obj - unregister nf_tables object type | |
5408 | * @obj: object type | |
5409 | * | |
5410 | * Unregisters the object type for use with nf_tables. | |
5411 | */ | |
5412 | void nft_unregister_obj(struct nft_object_type *obj_type) | |
5413 | { | |
5414 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
5415 | list_del_rcu(&obj_type->list); | |
5416 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
5417 | } | |
5418 | EXPORT_SYMBOL_GPL(nft_unregister_obj); | |
5419 | ||
4d44175a FW |
5420 | struct nft_object *nft_obj_lookup(const struct net *net, |
5421 | const struct nft_table *table, | |
cac20fcd PNA |
5422 | const struct nlattr *nla, u32 objtype, |
5423 | u8 genmask) | |
e5009240 | 5424 | { |
4d44175a FW |
5425 | struct nft_object_hash_key k = { .table = table }; |
5426 | char search[NFT_OBJ_MAXNAMELEN]; | |
5427 | struct rhlist_head *tmp, *list; | |
e5009240 PNA |
5428 | struct nft_object *obj; |
5429 | ||
4d44175a FW |
5430 | nla_strlcpy(search, nla, sizeof(search)); |
5431 | k.name = search; | |
5432 | ||
5433 | WARN_ON_ONCE(!rcu_read_lock_held() && | |
5434 | !lockdep_commit_lock_is_held(net)); | |
5435 | ||
5436 | rcu_read_lock(); | |
5437 | list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params); | |
5438 | if (!list) | |
5439 | goto out; | |
5440 | ||
5441 | rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) { | |
5442 | if (objtype == obj->ops->type->type && | |
5443 | nft_active_genmask(obj, genmask)) { | |
5444 | rcu_read_unlock(); | |
e5009240 | 5445 | return obj; |
4d44175a | 5446 | } |
e5009240 | 5447 | } |
4d44175a FW |
5448 | out: |
5449 | rcu_read_unlock(); | |
e5009240 PNA |
5450 | return ERR_PTR(-ENOENT); |
5451 | } | |
cac20fcd | 5452 | EXPORT_SYMBOL_GPL(nft_obj_lookup); |
e5009240 | 5453 | |
cac20fcd PNA |
5454 | static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table, |
5455 | const struct nlattr *nla, | |
5456 | u32 objtype, u8 genmask) | |
3ecbfd65 HS |
5457 | { |
5458 | struct nft_object *obj; | |
5459 | ||
5460 | list_for_each_entry(obj, &table->objects, list) { | |
5461 | if (be64_to_cpu(nla_get_be64(nla)) == obj->handle && | |
5462 | objtype == obj->ops->type->type && | |
5463 | nft_active_genmask(obj, genmask)) | |
5464 | return obj; | |
5465 | } | |
5466 | return ERR_PTR(-ENOENT); | |
5467 | } | |
5468 | ||
e5009240 | 5469 | static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = { |
b2fbd044 LZ |
5470 | [NFTA_OBJ_TABLE] = { .type = NLA_STRING, |
5471 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
5472 | [NFTA_OBJ_NAME] = { .type = NLA_STRING, | |
5473 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
e5009240 PNA |
5474 | [NFTA_OBJ_TYPE] = { .type = NLA_U32 }, |
5475 | [NFTA_OBJ_DATA] = { .type = NLA_NESTED }, | |
3ecbfd65 | 5476 | [NFTA_OBJ_HANDLE] = { .type = NLA_U64}, |
e5009240 PNA |
5477 | }; |
5478 | ||
84fba055 FW |
5479 | static struct nft_object *nft_obj_init(const struct nft_ctx *ctx, |
5480 | const struct nft_object_type *type, | |
e5009240 PNA |
5481 | const struct nlattr *attr) |
5482 | { | |
5b4c6e38 | 5483 | struct nlattr **tb; |
dfc46034 | 5484 | const struct nft_object_ops *ops; |
e5009240 | 5485 | struct nft_object *obj; |
5b4c6e38 GS |
5486 | int err = -ENOMEM; |
5487 | ||
5488 | tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL); | |
5489 | if (!tb) | |
5490 | goto err1; | |
e5009240 PNA |
5491 | |
5492 | if (attr) { | |
8cb08174 JB |
5493 | err = nla_parse_nested_deprecated(tb, type->maxattr, attr, |
5494 | type->policy, NULL); | |
e5009240 | 5495 | if (err < 0) |
5b4c6e38 | 5496 | goto err2; |
e5009240 PNA |
5497 | } else { |
5498 | memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1)); | |
5499 | } | |
5500 | ||
dfc46034 PBG |
5501 | if (type->select_ops) { |
5502 | ops = type->select_ops(ctx, (const struct nlattr * const *)tb); | |
5503 | if (IS_ERR(ops)) { | |
5504 | err = PTR_ERR(ops); | |
5b4c6e38 | 5505 | goto err2; |
dfc46034 PBG |
5506 | } |
5507 | } else { | |
5508 | ops = type->ops; | |
5509 | } | |
5510 | ||
e5009240 | 5511 | err = -ENOMEM; |
dfc46034 | 5512 | obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL); |
5b4c6e38 GS |
5513 | if (!obj) |
5514 | goto err2; | |
e5009240 | 5515 | |
dfc46034 | 5516 | err = ops->init(ctx, (const struct nlattr * const *)tb, obj); |
e5009240 | 5517 | if (err < 0) |
5b4c6e38 | 5518 | goto err3; |
e5009240 | 5519 | |
dfc46034 PBG |
5520 | obj->ops = ops; |
5521 | ||
5b4c6e38 | 5522 | kfree(tb); |
e5009240 | 5523 | return obj; |
5b4c6e38 | 5524 | err3: |
e5009240 | 5525 | kfree(obj); |
5b4c6e38 GS |
5526 | err2: |
5527 | kfree(tb); | |
e5009240 PNA |
5528 | err1: |
5529 | return ERR_PTR(err); | |
5530 | } | |
5531 | ||
5532 | static int nft_object_dump(struct sk_buff *skb, unsigned int attr, | |
43da04a5 | 5533 | struct nft_object *obj, bool reset) |
e5009240 PNA |
5534 | { |
5535 | struct nlattr *nest; | |
5536 | ||
ae0be8de | 5537 | nest = nla_nest_start_noflag(skb, attr); |
e5009240 PNA |
5538 | if (!nest) |
5539 | goto nla_put_failure; | |
dfc46034 | 5540 | if (obj->ops->dump(skb, obj, reset) < 0) |
e5009240 PNA |
5541 | goto nla_put_failure; |
5542 | nla_nest_end(skb, nest); | |
5543 | return 0; | |
5544 | ||
5545 | nla_put_failure: | |
5546 | return -1; | |
5547 | } | |
5548 | ||
5549 | static const struct nft_object_type *__nft_obj_type_get(u32 objtype) | |
5550 | { | |
5551 | const struct nft_object_type *type; | |
5552 | ||
5553 | list_for_each_entry(type, &nf_tables_objects, list) { | |
5554 | if (objtype == type->type) | |
5555 | return type; | |
5556 | } | |
5557 | return NULL; | |
5558 | } | |
5559 | ||
452238e8 FW |
5560 | static const struct nft_object_type * |
5561 | nft_obj_type_get(struct net *net, u32 objtype) | |
e5009240 PNA |
5562 | { |
5563 | const struct nft_object_type *type; | |
5564 | ||
5565 | type = __nft_obj_type_get(objtype); | |
5566 | if (type != NULL && try_module_get(type->owner)) | |
5567 | return type; | |
5568 | ||
f102d66b | 5569 | lockdep_nfnl_nft_mutex_not_held(); |
e5009240 PNA |
5570 | #ifdef CONFIG_MODULES |
5571 | if (type == NULL) { | |
eb014de4 | 5572 | if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN) |
e5009240 PNA |
5573 | return ERR_PTR(-EAGAIN); |
5574 | } | |
5575 | #endif | |
5576 | return ERR_PTR(-ENOENT); | |
5577 | } | |
5578 | ||
d62d0ba9 FFM |
5579 | static int nf_tables_updobj(const struct nft_ctx *ctx, |
5580 | const struct nft_object_type *type, | |
5581 | const struct nlattr *attr, | |
5582 | struct nft_object *obj) | |
5583 | { | |
5584 | struct nft_object *newobj; | |
5585 | struct nft_trans *trans; | |
5586 | int err; | |
5587 | ||
5588 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ, | |
5589 | sizeof(struct nft_trans_obj)); | |
5590 | if (!trans) | |
5591 | return -ENOMEM; | |
5592 | ||
5593 | newobj = nft_obj_init(ctx, type, attr); | |
5594 | if (IS_ERR(newobj)) { | |
5595 | err = PTR_ERR(newobj); | |
b74ae961 | 5596 | goto err_free_trans; |
d62d0ba9 FFM |
5597 | } |
5598 | ||
5599 | nft_trans_obj(trans) = obj; | |
5600 | nft_trans_obj_update(trans) = true; | |
5601 | nft_trans_obj_newobj(trans) = newobj; | |
5602 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
5603 | ||
5604 | return 0; | |
b74ae961 DC |
5605 | |
5606 | err_free_trans: | |
d62d0ba9 | 5607 | kfree(trans); |
d62d0ba9 FFM |
5608 | return err; |
5609 | } | |
5610 | ||
e5009240 PNA |
5611 | static int nf_tables_newobj(struct net *net, struct sock *nlsk, |
5612 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
5613 | const struct nlattr * const nla[], |
5614 | struct netlink_ext_ack *extack) | |
e5009240 PNA |
5615 | { |
5616 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
5617 | const struct nft_object_type *type; | |
5618 | u8 genmask = nft_genmask_next(net); | |
5619 | int family = nfmsg->nfgen_family; | |
e5009240 PNA |
5620 | struct nft_table *table; |
5621 | struct nft_object *obj; | |
5622 | struct nft_ctx ctx; | |
5623 | u32 objtype; | |
5624 | int err; | |
5625 | ||
5626 | if (!nla[NFTA_OBJ_TYPE] || | |
5627 | !nla[NFTA_OBJ_NAME] || | |
5628 | !nla[NFTA_OBJ_DATA]) | |
5629 | return -EINVAL; | |
5630 | ||
cac20fcd | 5631 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask); |
36dd1bcc PNA |
5632 | if (IS_ERR(table)) { |
5633 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 5634 | return PTR_ERR(table); |
36dd1bcc | 5635 | } |
e5009240 PNA |
5636 | |
5637 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 5638 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
e5009240 PNA |
5639 | if (IS_ERR(obj)) { |
5640 | err = PTR_ERR(obj); | |
36dd1bcc PNA |
5641 | if (err != -ENOENT) { |
5642 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 5643 | return err; |
36dd1bcc | 5644 | } |
1a28ad74 | 5645 | } else { |
36dd1bcc PNA |
5646 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
5647 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 5648 | return -EEXIST; |
36dd1bcc | 5649 | } |
d62d0ba9 FFM |
5650 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
5651 | return -EOPNOTSUPP; | |
5652 | ||
fd57d0cb | 5653 | type = __nft_obj_type_get(objtype); |
d62d0ba9 FFM |
5654 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
5655 | ||
5656 | return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj); | |
e5009240 PNA |
5657 | } |
5658 | ||
98319cb9 | 5659 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
e5009240 | 5660 | |
452238e8 | 5661 | type = nft_obj_type_get(net, objtype); |
e5009240 PNA |
5662 | if (IS_ERR(type)) |
5663 | return PTR_ERR(type); | |
5664 | ||
84fba055 | 5665 | obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]); |
e5009240 PNA |
5666 | if (IS_ERR(obj)) { |
5667 | err = PTR_ERR(obj); | |
5668 | goto err1; | |
5669 | } | |
d152159b | 5670 | obj->key.table = table; |
3ecbfd65 HS |
5671 | obj->handle = nf_tables_alloc_handle(table); |
5672 | ||
d152159b FW |
5673 | obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL); |
5674 | if (!obj->key.name) { | |
61509575 PS |
5675 | err = -ENOMEM; |
5676 | goto err2; | |
5677 | } | |
e5009240 PNA |
5678 | |
5679 | err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj); | |
5680 | if (err < 0) | |
61509575 | 5681 | goto err3; |
e5009240 | 5682 | |
4d44175a FW |
5683 | err = rhltable_insert(&nft_objname_ht, &obj->rhlhead, |
5684 | nft_objname_ht_params); | |
5685 | if (err < 0) | |
5686 | goto err4; | |
5687 | ||
e5009240 PNA |
5688 | list_add_tail_rcu(&obj->list, &table->objects); |
5689 | table->use++; | |
5690 | return 0; | |
4d44175a FW |
5691 | err4: |
5692 | /* queued in transaction log */ | |
5693 | INIT_LIST_HEAD(&obj->list); | |
5694 | return err; | |
61509575 | 5695 | err3: |
d152159b | 5696 | kfree(obj->key.name); |
e5009240 | 5697 | err2: |
dfc46034 | 5698 | if (obj->ops->destroy) |
00bfb320 | 5699 | obj->ops->destroy(&ctx, obj); |
e5009240 PNA |
5700 | kfree(obj); |
5701 | err1: | |
5702 | module_put(type->owner); | |
5703 | return err; | |
5704 | } | |
5705 | ||
5706 | static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net, | |
5707 | u32 portid, u32 seq, int event, u32 flags, | |
5708 | int family, const struct nft_table *table, | |
43da04a5 | 5709 | struct nft_object *obj, bool reset) |
e5009240 PNA |
5710 | { |
5711 | struct nfgenmsg *nfmsg; | |
5712 | struct nlmsghdr *nlh; | |
5713 | ||
dedb67c4 | 5714 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
e5009240 PNA |
5715 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); |
5716 | if (nlh == NULL) | |
5717 | goto nla_put_failure; | |
5718 | ||
5719 | nfmsg = nlmsg_data(nlh); | |
5720 | nfmsg->nfgen_family = family; | |
5721 | nfmsg->version = NFNETLINK_V0; | |
5722 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); | |
5723 | ||
5724 | if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) || | |
d152159b | 5725 | nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) || |
dfc46034 | 5726 | nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) || |
e5009240 | 5727 | nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) || |
3ecbfd65 HS |
5728 | nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) || |
5729 | nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle), | |
5730 | NFTA_OBJ_PAD)) | |
e5009240 PNA |
5731 | goto nla_put_failure; |
5732 | ||
5733 | nlmsg_end(skb, nlh); | |
5734 | return 0; | |
5735 | ||
5736 | nla_put_failure: | |
5737 | nlmsg_trim(skb, nlh); | |
5738 | return -1; | |
5739 | } | |
5740 | ||
a9fea2a3 | 5741 | struct nft_obj_filter { |
e46abbcc | 5742 | char *table; |
a9fea2a3 PNA |
5743 | u32 type; |
5744 | }; | |
5745 | ||
e5009240 PNA |
5746 | static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) |
5747 | { | |
5748 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
e5009240 | 5749 | const struct nft_table *table; |
e5009240 | 5750 | unsigned int idx = 0, s_idx = cb->args[0]; |
a9fea2a3 | 5751 | struct nft_obj_filter *filter = cb->data; |
e5009240 PNA |
5752 | struct net *net = sock_net(skb->sk); |
5753 | int family = nfmsg->nfgen_family; | |
43da04a5 PNA |
5754 | struct nft_object *obj; |
5755 | bool reset = false; | |
5756 | ||
5757 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) | |
5758 | reset = true; | |
e5009240 PNA |
5759 | |
5760 | rcu_read_lock(); | |
5761 | cb->seq = net->nft.base_seq; | |
5762 | ||
36596dad | 5763 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 5764 | if (family != NFPROTO_UNSPEC && family != table->family) |
e5009240 PNA |
5765 | continue; |
5766 | ||
36596dad PNA |
5767 | list_for_each_entry_rcu(obj, &table->objects, list) { |
5768 | if (!nft_is_active(net, obj)) | |
5769 | goto cont; | |
5770 | if (idx < s_idx) | |
5771 | goto cont; | |
5772 | if (idx > s_idx) | |
5773 | memset(&cb->args[1], 0, | |
5774 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 5775 | if (filter && filter->table && |
36596dad PNA |
5776 | strcmp(filter->table, table->name)) |
5777 | goto cont; | |
5778 | if (filter && | |
5779 | filter->type != NFT_OBJECT_UNSPEC && | |
5780 | obj->ops->type->type != filter->type) | |
5781 | goto cont; | |
a9fea2a3 | 5782 | |
36596dad PNA |
5783 | if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid, |
5784 | cb->nlh->nlmsg_seq, | |
5785 | NFT_MSG_NEWOBJ, | |
5786 | NLM_F_MULTI | NLM_F_APPEND, | |
98319cb9 | 5787 | table->family, table, |
36596dad PNA |
5788 | obj, reset) < 0) |
5789 | goto done; | |
e5009240 | 5790 | |
36596dad | 5791 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
e5009240 | 5792 | cont: |
36596dad | 5793 | idx++; |
e5009240 PNA |
5794 | } |
5795 | } | |
5796 | done: | |
5797 | rcu_read_unlock(); | |
5798 | ||
5799 | cb->args[0] = idx; | |
5800 | return skb->len; | |
5801 | } | |
5802 | ||
90fd131a | 5803 | static int nf_tables_dump_obj_start(struct netlink_callback *cb) |
a9fea2a3 | 5804 | { |
90fd131a FW |
5805 | const struct nlattr * const *nla = cb->data; |
5806 | struct nft_obj_filter *filter = NULL; | |
e46abbcc | 5807 | |
90fd131a FW |
5808 | if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) { |
5809 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
5810 | if (!filter) | |
5811 | return -ENOMEM; | |
5812 | ||
5813 | if (nla[NFTA_OBJ_TABLE]) { | |
5814 | filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC); | |
5815 | if (!filter->table) { | |
5816 | kfree(filter); | |
5817 | return -ENOMEM; | |
5818 | } | |
5819 | } | |
5820 | ||
5821 | if (nla[NFTA_OBJ_TYPE]) | |
5822 | filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
8bea728d | 5823 | } |
a9fea2a3 | 5824 | |
90fd131a | 5825 | cb->data = filter; |
a9fea2a3 PNA |
5826 | return 0; |
5827 | } | |
5828 | ||
90fd131a | 5829 | static int nf_tables_dump_obj_done(struct netlink_callback *cb) |
a9fea2a3 | 5830 | { |
90fd131a | 5831 | struct nft_obj_filter *filter = cb->data; |
a9fea2a3 | 5832 | |
90fd131a FW |
5833 | if (filter) { |
5834 | kfree(filter->table); | |
5835 | kfree(filter); | |
e46abbcc | 5836 | } |
a9fea2a3 | 5837 | |
90fd131a | 5838 | return 0; |
a9fea2a3 PNA |
5839 | } |
5840 | ||
d9adf22a | 5841 | /* called with rcu_read_lock held */ |
e5009240 PNA |
5842 | static int nf_tables_getobj(struct net *net, struct sock *nlsk, |
5843 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
5844 | const struct nlattr * const nla[], |
5845 | struct netlink_ext_ack *extack) | |
e5009240 PNA |
5846 | { |
5847 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
5848 | u8 genmask = nft_genmask_cur(net); | |
5849 | int family = nfmsg->nfgen_family; | |
e5009240 PNA |
5850 | const struct nft_table *table; |
5851 | struct nft_object *obj; | |
5852 | struct sk_buff *skb2; | |
43da04a5 | 5853 | bool reset = false; |
e5009240 PNA |
5854 | u32 objtype; |
5855 | int err; | |
5856 | ||
5857 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
5858 | struct netlink_dump_control c = { | |
90fd131a | 5859 | .start = nf_tables_dump_obj_start, |
e5009240 | 5860 | .dump = nf_tables_dump_obj, |
a9fea2a3 | 5861 | .done = nf_tables_dump_obj_done, |
d9adf22a | 5862 | .module = THIS_MODULE, |
90fd131a | 5863 | .data = (void *)nla, |
e5009240 | 5864 | }; |
a9fea2a3 | 5865 | |
d9adf22a | 5866 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
e5009240 PNA |
5867 | } |
5868 | ||
5869 | if (!nla[NFTA_OBJ_NAME] || | |
5870 | !nla[NFTA_OBJ_TYPE]) | |
5871 | return -EINVAL; | |
5872 | ||
cac20fcd | 5873 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask); |
36dd1bcc PNA |
5874 | if (IS_ERR(table)) { |
5875 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 5876 | return PTR_ERR(table); |
36dd1bcc | 5877 | } |
e5009240 PNA |
5878 | |
5879 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 5880 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
36dd1bcc PNA |
5881 | if (IS_ERR(obj)) { |
5882 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 5883 | return PTR_ERR(obj); |
36dd1bcc | 5884 | } |
e5009240 | 5885 | |
d9adf22a | 5886 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
e5009240 PNA |
5887 | if (!skb2) |
5888 | return -ENOMEM; | |
5889 | ||
43da04a5 PNA |
5890 | if (NFNL_MSG_TYPE(nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) |
5891 | reset = true; | |
5892 | ||
e5009240 PNA |
5893 | err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid, |
5894 | nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0, | |
43da04a5 | 5895 | family, table, obj, reset); |
e5009240 PNA |
5896 | if (err < 0) |
5897 | goto err; | |
5898 | ||
5899 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
5900 | err: | |
5901 | kfree_skb(skb2); | |
5902 | return err; | |
e5009240 PNA |
5903 | } |
5904 | ||
00bfb320 | 5905 | static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj) |
e5009240 | 5906 | { |
dfc46034 | 5907 | if (obj->ops->destroy) |
00bfb320 | 5908 | obj->ops->destroy(ctx, obj); |
e5009240 | 5909 | |
dfc46034 | 5910 | module_put(obj->ops->type->owner); |
d152159b | 5911 | kfree(obj->key.name); |
e5009240 PNA |
5912 | kfree(obj); |
5913 | } | |
5914 | ||
5915 | static int nf_tables_delobj(struct net *net, struct sock *nlsk, | |
04ba724b PNA |
5916 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
5917 | const struct nlattr * const nla[], | |
5918 | struct netlink_ext_ack *extack) | |
e5009240 PNA |
5919 | { |
5920 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
5921 | u8 genmask = nft_genmask_next(net); | |
5922 | int family = nfmsg->nfgen_family; | |
36dd1bcc | 5923 | const struct nlattr *attr; |
e5009240 PNA |
5924 | struct nft_table *table; |
5925 | struct nft_object *obj; | |
5926 | struct nft_ctx ctx; | |
5927 | u32 objtype; | |
5928 | ||
5929 | if (!nla[NFTA_OBJ_TYPE] || | |
3ecbfd65 | 5930 | (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE])) |
e5009240 PNA |
5931 | return -EINVAL; |
5932 | ||
cac20fcd | 5933 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask); |
36dd1bcc PNA |
5934 | if (IS_ERR(table)) { |
5935 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 5936 | return PTR_ERR(table); |
36dd1bcc | 5937 | } |
e5009240 PNA |
5938 | |
5939 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
36dd1bcc PNA |
5940 | if (nla[NFTA_OBJ_HANDLE]) { |
5941 | attr = nla[NFTA_OBJ_HANDLE]; | |
5942 | obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask); | |
5943 | } else { | |
5944 | attr = nla[NFTA_OBJ_NAME]; | |
4d44175a | 5945 | obj = nft_obj_lookup(net, table, attr, objtype, genmask); |
36dd1bcc PNA |
5946 | } |
5947 | ||
5948 | if (IS_ERR(obj)) { | |
5949 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 5950 | return PTR_ERR(obj); |
36dd1bcc PNA |
5951 | } |
5952 | if (obj->use > 0) { | |
5953 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 5954 | return -EBUSY; |
36dd1bcc | 5955 | } |
e5009240 | 5956 | |
98319cb9 | 5957 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
e5009240 PNA |
5958 | |
5959 | return nft_delobj(&ctx, obj); | |
5960 | } | |
5961 | ||
d152159b | 5962 | void nft_obj_notify(struct net *net, const struct nft_table *table, |
25e94a99 PNA |
5963 | struct nft_object *obj, u32 portid, u32 seq, int event, |
5964 | int family, int report, gfp_t gfp) | |
e5009240 PNA |
5965 | { |
5966 | struct sk_buff *skb; | |
5967 | int err; | |
5968 | ||
2599e989 PNA |
5969 | if (!report && |
5970 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
25e94a99 | 5971 | return; |
e5009240 | 5972 | |
2599e989 | 5973 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp); |
e5009240 PNA |
5974 | if (skb == NULL) |
5975 | goto err; | |
5976 | ||
2599e989 PNA |
5977 | err = nf_tables_fill_obj_info(skb, net, portid, seq, event, 0, family, |
5978 | table, obj, false); | |
e5009240 PNA |
5979 | if (err < 0) { |
5980 | kfree_skb(skb); | |
5981 | goto err; | |
5982 | } | |
5983 | ||
25e94a99 PNA |
5984 | nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report, gfp); |
5985 | return; | |
e5009240 | 5986 | err: |
25e94a99 | 5987 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
e5009240 | 5988 | } |
2599e989 PNA |
5989 | EXPORT_SYMBOL_GPL(nft_obj_notify); |
5990 | ||
25e94a99 PNA |
5991 | static void nf_tables_obj_notify(const struct nft_ctx *ctx, |
5992 | struct nft_object *obj, int event) | |
2599e989 | 5993 | { |
25e94a99 | 5994 | nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event, |
36596dad | 5995 | ctx->family, ctx->report, GFP_KERNEL); |
2599e989 | 5996 | } |
e5009240 | 5997 | |
3b49e2e9 PNA |
5998 | /* |
5999 | * Flow tables | |
6000 | */ | |
6001 | void nft_register_flowtable_type(struct nf_flowtable_type *type) | |
6002 | { | |
6003 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6004 | list_add_tail_rcu(&type->list, &nf_tables_flowtables); | |
6005 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6006 | } | |
6007 | EXPORT_SYMBOL_GPL(nft_register_flowtable_type); | |
6008 | ||
6009 | void nft_unregister_flowtable_type(struct nf_flowtable_type *type) | |
6010 | { | |
6011 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6012 | list_del_rcu(&type->list); | |
6013 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6014 | } | |
6015 | EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type); | |
6016 | ||
6017 | static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = { | |
6018 | [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING, | |
6019 | .len = NFT_NAME_MAXLEN - 1 }, | |
6020 | [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING, | |
6021 | .len = NFT_NAME_MAXLEN - 1 }, | |
6022 | [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED }, | |
3ecbfd65 | 6023 | [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 }, |
8bb69f3b | 6024 | [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 }, |
3b49e2e9 PNA |
6025 | }; |
6026 | ||
cac20fcd PNA |
6027 | struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table, |
6028 | const struct nlattr *nla, u8 genmask) | |
3b49e2e9 PNA |
6029 | { |
6030 | struct nft_flowtable *flowtable; | |
6031 | ||
d9adf22a | 6032 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
3b49e2e9 PNA |
6033 | if (!nla_strcmp(nla, flowtable->name) && |
6034 | nft_active_genmask(flowtable, genmask)) | |
6035 | return flowtable; | |
6036 | } | |
6037 | return ERR_PTR(-ENOENT); | |
6038 | } | |
cac20fcd | 6039 | EXPORT_SYMBOL_GPL(nft_flowtable_lookup); |
3b49e2e9 | 6040 | |
9b05b6e1 LGL |
6041 | void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx, |
6042 | struct nft_flowtable *flowtable, | |
6043 | enum nft_trans_phase phase) | |
6044 | { | |
6045 | switch (phase) { | |
6046 | case NFT_TRANS_PREPARE: | |
6047 | case NFT_TRANS_ABORT: | |
6048 | case NFT_TRANS_RELEASE: | |
6049 | flowtable->use--; | |
6050 | /* fall through */ | |
6051 | default: | |
6052 | return; | |
6053 | } | |
6054 | } | |
6055 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable); | |
6056 | ||
ae0662f8 | 6057 | static struct nft_flowtable * |
cac20fcd PNA |
6058 | nft_flowtable_lookup_byhandle(const struct nft_table *table, |
6059 | const struct nlattr *nla, u8 genmask) | |
3ecbfd65 HS |
6060 | { |
6061 | struct nft_flowtable *flowtable; | |
6062 | ||
6063 | list_for_each_entry(flowtable, &table->flowtables, list) { | |
6064 | if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle && | |
6065 | nft_active_genmask(flowtable, genmask)) | |
6066 | return flowtable; | |
6067 | } | |
6068 | return ERR_PTR(-ENOENT); | |
6069 | } | |
6070 | ||
3b49e2e9 PNA |
6071 | static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = { |
6072 | [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 }, | |
6073 | [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
6074 | [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED }, | |
6075 | }; | |
6076 | ||
6077 | static int nf_tables_flowtable_parse_hook(const struct nft_ctx *ctx, | |
6078 | const struct nlattr *attr, | |
6079 | struct nft_flowtable *flowtable) | |
6080 | { | |
3b49e2e9 | 6081 | struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1]; |
3f0465a9 | 6082 | struct nft_hook *hook; |
3b49e2e9 | 6083 | int hooknum, priority; |
3f0465a9 | 6084 | int err; |
3b49e2e9 | 6085 | |
8cb08174 JB |
6086 | err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, attr, |
6087 | nft_flowtable_hook_policy, NULL); | |
3b49e2e9 PNA |
6088 | if (err < 0) |
6089 | return err; | |
6090 | ||
6091 | if (!tb[NFTA_FLOWTABLE_HOOK_NUM] || | |
6092 | !tb[NFTA_FLOWTABLE_HOOK_PRIORITY] || | |
6093 | !tb[NFTA_FLOWTABLE_HOOK_DEVS]) | |
6094 | return -EINVAL; | |
6095 | ||
6096 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); | |
fe19c04c | 6097 | if (hooknum != NF_NETDEV_INGRESS) |
3b49e2e9 PNA |
6098 | return -EINVAL; |
6099 | ||
6100 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
6101 | ||
3f0465a9 PNA |
6102 | err = nf_tables_parse_netdev_hooks(ctx->net, |
6103 | tb[NFTA_FLOWTABLE_HOOK_DEVS], | |
6104 | &flowtable->hook_list); | |
3b49e2e9 | 6105 | if (err < 0) |
d92191aa | 6106 | return err; |
3b49e2e9 | 6107 | |
71a8a63b PNA |
6108 | flowtable->hooknum = hooknum; |
6109 | flowtable->data.priority = priority; | |
3b49e2e9 | 6110 | |
3f0465a9 PNA |
6111 | list_for_each_entry(hook, &flowtable->hook_list, list) { |
6112 | hook->ops.pf = NFPROTO_NETDEV; | |
6113 | hook->ops.hooknum = hooknum; | |
6114 | hook->ops.priority = priority; | |
6115 | hook->ops.priv = &flowtable->data; | |
6116 | hook->ops.hook = flowtable->data.type->hook; | |
3b49e2e9 PNA |
6117 | } |
6118 | ||
3b49e2e9 PNA |
6119 | return err; |
6120 | } | |
6121 | ||
98319cb9 | 6122 | static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family) |
3b49e2e9 PNA |
6123 | { |
6124 | const struct nf_flowtable_type *type; | |
6125 | ||
6126 | list_for_each_entry(type, &nf_tables_flowtables, list) { | |
98319cb9 | 6127 | if (family == type->family) |
3b49e2e9 PNA |
6128 | return type; |
6129 | } | |
6130 | return NULL; | |
6131 | } | |
6132 | ||
452238e8 FW |
6133 | static const struct nf_flowtable_type * |
6134 | nft_flowtable_type_get(struct net *net, u8 family) | |
3b49e2e9 PNA |
6135 | { |
6136 | const struct nf_flowtable_type *type; | |
6137 | ||
98319cb9 | 6138 | type = __nft_flowtable_type_get(family); |
3b49e2e9 PNA |
6139 | if (type != NULL && try_module_get(type->owner)) |
6140 | return type; | |
6141 | ||
f102d66b | 6142 | lockdep_nfnl_nft_mutex_not_held(); |
3b49e2e9 PNA |
6143 | #ifdef CONFIG_MODULES |
6144 | if (type == NULL) { | |
eb014de4 | 6145 | if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN) |
3b49e2e9 PNA |
6146 | return ERR_PTR(-EAGAIN); |
6147 | } | |
6148 | #endif | |
6149 | return ERR_PTR(-ENOENT); | |
6150 | } | |
6151 | ||
5acab914 | 6152 | /* Only called from error and netdev event paths. */ |
ff4bf2f4 PNA |
6153 | static void nft_unregister_flowtable_hook(struct net *net, |
6154 | struct nft_flowtable *flowtable, | |
6155 | struct nft_hook *hook) | |
6156 | { | |
6157 | nf_unregister_net_hook(net, &hook->ops); | |
6158 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, | |
6159 | FLOW_BLOCK_UNBIND); | |
6160 | } | |
6161 | ||
3b49e2e9 PNA |
6162 | static void nft_unregister_flowtable_net_hooks(struct net *net, |
6163 | struct nft_flowtable *flowtable) | |
6164 | { | |
3f0465a9 | 6165 | struct nft_hook *hook; |
3b49e2e9 | 6166 | |
ff4bf2f4 | 6167 | list_for_each_entry(hook, &flowtable->hook_list, list) |
5acab914 | 6168 | nf_unregister_net_hook(net, &hook->ops); |
3f0465a9 PNA |
6169 | } |
6170 | ||
6171 | static int nft_register_flowtable_net_hooks(struct net *net, | |
6172 | struct nft_table *table, | |
6173 | struct nft_flowtable *flowtable) | |
6174 | { | |
6175 | struct nft_hook *hook, *hook2, *next; | |
6176 | struct nft_flowtable *ft; | |
6177 | int err, i = 0; | |
6178 | ||
6179 | list_for_each_entry(hook, &flowtable->hook_list, list) { | |
6180 | list_for_each_entry(ft, &table->flowtables, list) { | |
6181 | list_for_each_entry(hook2, &ft->hook_list, list) { | |
6182 | if (hook->ops.dev == hook2->ops.dev && | |
6183 | hook->ops.pf == hook2->ops.pf) { | |
6184 | err = -EBUSY; | |
6185 | goto err_unregister_net_hooks; | |
6186 | } | |
6187 | } | |
6188 | } | |
3b49e2e9 | 6189 | |
d7c03a9f | 6190 | err = flowtable->data.type->setup(&flowtable->data, |
6191 | hook->ops.dev, | |
6192 | FLOW_BLOCK_BIND); | |
3f0465a9 PNA |
6193 | if (err < 0) |
6194 | goto err_unregister_net_hooks; | |
6195 | ||
d7c03a9f | 6196 | err = nf_register_net_hook(net, &hook->ops); |
6197 | if (err < 0) { | |
6198 | flowtable->data.type->setup(&flowtable->data, | |
6199 | hook->ops.dev, | |
6200 | FLOW_BLOCK_UNBIND); | |
6201 | goto err_unregister_net_hooks; | |
6202 | } | |
6203 | ||
3f0465a9 | 6204 | i++; |
3b49e2e9 | 6205 | } |
3f0465a9 PNA |
6206 | |
6207 | return 0; | |
6208 | ||
6209 | err_unregister_net_hooks: | |
6210 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { | |
6211 | if (i-- <= 0) | |
6212 | break; | |
6213 | ||
ff4bf2f4 | 6214 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
6215 | list_del_rcu(&hook->list); |
6216 | kfree_rcu(hook, rcu); | |
6217 | } | |
6218 | ||
6219 | return err; | |
3b49e2e9 PNA |
6220 | } |
6221 | ||
6222 | static int nf_tables_newflowtable(struct net *net, struct sock *nlsk, | |
6223 | struct sk_buff *skb, | |
6224 | const struct nlmsghdr *nlh, | |
6225 | const struct nlattr * const nla[], | |
6226 | struct netlink_ext_ack *extack) | |
6227 | { | |
6228 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
6229 | const struct nf_flowtable_type *type; | |
6230 | u8 genmask = nft_genmask_next(net); | |
6231 | int family = nfmsg->nfgen_family; | |
3f0465a9 PNA |
6232 | struct nft_flowtable *flowtable; |
6233 | struct nft_hook *hook, *next; | |
3b49e2e9 PNA |
6234 | struct nft_table *table; |
6235 | struct nft_ctx ctx; | |
3f0465a9 | 6236 | int err; |
3b49e2e9 PNA |
6237 | |
6238 | if (!nla[NFTA_FLOWTABLE_TABLE] || | |
6239 | !nla[NFTA_FLOWTABLE_NAME] || | |
6240 | !nla[NFTA_FLOWTABLE_HOOK]) | |
6241 | return -EINVAL; | |
6242 | ||
cac20fcd PNA |
6243 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6244 | genmask); | |
36dd1bcc PNA |
6245 | if (IS_ERR(table)) { |
6246 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 6247 | return PTR_ERR(table); |
36dd1bcc | 6248 | } |
3b49e2e9 | 6249 | |
cac20fcd PNA |
6250 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
6251 | genmask); | |
3b49e2e9 PNA |
6252 | if (IS_ERR(flowtable)) { |
6253 | err = PTR_ERR(flowtable); | |
36dd1bcc PNA |
6254 | if (err != -ENOENT) { |
6255 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
3b49e2e9 | 6256 | return err; |
36dd1bcc | 6257 | } |
3b49e2e9 | 6258 | } else { |
36dd1bcc PNA |
6259 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
6260 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
3b49e2e9 | 6261 | return -EEXIST; |
36dd1bcc | 6262 | } |
3b49e2e9 PNA |
6263 | |
6264 | return 0; | |
6265 | } | |
6266 | ||
98319cb9 | 6267 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
3b49e2e9 PNA |
6268 | |
6269 | flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL); | |
6270 | if (!flowtable) | |
6271 | return -ENOMEM; | |
6272 | ||
6273 | flowtable->table = table; | |
3ecbfd65 | 6274 | flowtable->handle = nf_tables_alloc_handle(table); |
3f0465a9 | 6275 | INIT_LIST_HEAD(&flowtable->hook_list); |
3ecbfd65 | 6276 | |
3b49e2e9 PNA |
6277 | flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL); |
6278 | if (!flowtable->name) { | |
6279 | err = -ENOMEM; | |
6280 | goto err1; | |
6281 | } | |
6282 | ||
452238e8 | 6283 | type = nft_flowtable_type_get(net, family); |
3b49e2e9 PNA |
6284 | if (IS_ERR(type)) { |
6285 | err = PTR_ERR(type); | |
6286 | goto err2; | |
6287 | } | |
6288 | ||
8bb69f3b PNA |
6289 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
6290 | flowtable->data.flags = | |
6291 | ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
6292 | if (flowtable->data.flags & ~NF_FLOWTABLE_HW_OFFLOAD) | |
6293 | goto err3; | |
6294 | } | |
6295 | ||
6296 | write_pnet(&flowtable->data.net, net); | |
3b49e2e9 | 6297 | flowtable->data.type = type; |
a268de77 | 6298 | err = type->init(&flowtable->data); |
3b49e2e9 PNA |
6299 | if (err < 0) |
6300 | goto err3; | |
6301 | ||
6302 | err = nf_tables_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK], | |
6303 | flowtable); | |
6304 | if (err < 0) | |
a268de77 | 6305 | goto err4; |
3b49e2e9 | 6306 | |
3f0465a9 | 6307 | err = nft_register_flowtable_net_hooks(ctx.net, table, flowtable); |
2d285f26 FW |
6308 | if (err < 0) { |
6309 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { | |
6310 | list_del_rcu(&hook->list); | |
6311 | kfree_rcu(hook, rcu); | |
6312 | } | |
3f0465a9 | 6313 | goto err4; |
2d285f26 | 6314 | } |
3b49e2e9 PNA |
6315 | |
6316 | err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable); | |
6317 | if (err < 0) | |
3f0465a9 | 6318 | goto err5; |
3b49e2e9 PNA |
6319 | |
6320 | list_add_tail_rcu(&flowtable->list, &table->flowtables); | |
6321 | table->use++; | |
6322 | ||
6323 | return 0; | |
a268de77 | 6324 | err5: |
3f0465a9 | 6325 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
ff4bf2f4 | 6326 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
6327 | list_del_rcu(&hook->list); |
6328 | kfree_rcu(hook, rcu); | |
6329 | } | |
a268de77 FF |
6330 | err4: |
6331 | flowtable->data.type->free(&flowtable->data); | |
3b49e2e9 PNA |
6332 | err3: |
6333 | module_put(type->owner); | |
6334 | err2: | |
6335 | kfree(flowtable->name); | |
6336 | err1: | |
6337 | kfree(flowtable); | |
6338 | return err; | |
6339 | } | |
6340 | ||
6341 | static int nf_tables_delflowtable(struct net *net, struct sock *nlsk, | |
6342 | struct sk_buff *skb, | |
6343 | const struct nlmsghdr *nlh, | |
6344 | const struct nlattr * const nla[], | |
6345 | struct netlink_ext_ack *extack) | |
6346 | { | |
6347 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
6348 | u8 genmask = nft_genmask_next(net); | |
6349 | int family = nfmsg->nfgen_family; | |
6350 | struct nft_flowtable *flowtable; | |
36dd1bcc | 6351 | const struct nlattr *attr; |
3b49e2e9 PNA |
6352 | struct nft_table *table; |
6353 | struct nft_ctx ctx; | |
6354 | ||
e603ea4b PNA |
6355 | if (!nla[NFTA_FLOWTABLE_TABLE] || |
6356 | (!nla[NFTA_FLOWTABLE_NAME] && | |
6357 | !nla[NFTA_FLOWTABLE_HANDLE])) | |
6358 | return -EINVAL; | |
6359 | ||
cac20fcd PNA |
6360 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6361 | genmask); | |
36dd1bcc PNA |
6362 | if (IS_ERR(table)) { |
6363 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 6364 | return PTR_ERR(table); |
36dd1bcc | 6365 | } |
3b49e2e9 | 6366 | |
36dd1bcc PNA |
6367 | if (nla[NFTA_FLOWTABLE_HANDLE]) { |
6368 | attr = nla[NFTA_FLOWTABLE_HANDLE]; | |
6369 | flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask); | |
6370 | } else { | |
6371 | attr = nla[NFTA_FLOWTABLE_NAME]; | |
6372 | flowtable = nft_flowtable_lookup(table, attr, genmask); | |
6373 | } | |
6374 | ||
6375 | if (IS_ERR(flowtable)) { | |
6376 | NL_SET_BAD_ATTR(extack, attr); | |
6377 | return PTR_ERR(flowtable); | |
6378 | } | |
6379 | if (flowtable->use > 0) { | |
6380 | NL_SET_BAD_ATTR(extack, attr); | |
3b49e2e9 | 6381 | return -EBUSY; |
36dd1bcc | 6382 | } |
3b49e2e9 | 6383 | |
98319cb9 | 6384 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
3b49e2e9 PNA |
6385 | |
6386 | return nft_delflowtable(&ctx, flowtable); | |
6387 | } | |
6388 | ||
6389 | static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net, | |
6390 | u32 portid, u32 seq, int event, | |
6391 | u32 flags, int family, | |
6392 | struct nft_flowtable *flowtable) | |
6393 | { | |
6394 | struct nlattr *nest, *nest_devs; | |
6395 | struct nfgenmsg *nfmsg; | |
3f0465a9 | 6396 | struct nft_hook *hook; |
3b49e2e9 | 6397 | struct nlmsghdr *nlh; |
3b49e2e9 PNA |
6398 | |
6399 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); | |
6400 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); | |
6401 | if (nlh == NULL) | |
6402 | goto nla_put_failure; | |
6403 | ||
6404 | nfmsg = nlmsg_data(nlh); | |
6405 | nfmsg->nfgen_family = family; | |
6406 | nfmsg->version = NFNETLINK_V0; | |
6407 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); | |
6408 | ||
6409 | if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) || | |
6410 | nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) || | |
3ecbfd65 HS |
6411 | nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) || |
6412 | nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle), | |
8bb69f3b PNA |
6413 | NFTA_FLOWTABLE_PAD) || |
6414 | nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags))) | |
3b49e2e9 PNA |
6415 | goto nla_put_failure; |
6416 | ||
ae0be8de | 6417 | nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK); |
eb895086 KL |
6418 | if (!nest) |
6419 | goto nla_put_failure; | |
3b49e2e9 | 6420 | if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) || |
71a8a63b | 6421 | nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority))) |
3b49e2e9 PNA |
6422 | goto nla_put_failure; |
6423 | ||
ae0be8de | 6424 | nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS); |
3b49e2e9 PNA |
6425 | if (!nest_devs) |
6426 | goto nla_put_failure; | |
6427 | ||
3f0465a9 PNA |
6428 | list_for_each_entry_rcu(hook, &flowtable->hook_list, list) { |
6429 | if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name)) | |
3b49e2e9 PNA |
6430 | goto nla_put_failure; |
6431 | } | |
6432 | nla_nest_end(skb, nest_devs); | |
6433 | nla_nest_end(skb, nest); | |
6434 | ||
6435 | nlmsg_end(skb, nlh); | |
6436 | return 0; | |
6437 | ||
6438 | nla_put_failure: | |
6439 | nlmsg_trim(skb, nlh); | |
6440 | return -1; | |
6441 | } | |
6442 | ||
6443 | struct nft_flowtable_filter { | |
6444 | char *table; | |
6445 | }; | |
6446 | ||
6447 | static int nf_tables_dump_flowtable(struct sk_buff *skb, | |
6448 | struct netlink_callback *cb) | |
6449 | { | |
6450 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
6451 | struct nft_flowtable_filter *filter = cb->data; | |
6452 | unsigned int idx = 0, s_idx = cb->args[0]; | |
6453 | struct net *net = sock_net(skb->sk); | |
6454 | int family = nfmsg->nfgen_family; | |
6455 | struct nft_flowtable *flowtable; | |
3b49e2e9 PNA |
6456 | const struct nft_table *table; |
6457 | ||
6458 | rcu_read_lock(); | |
6459 | cb->seq = net->nft.base_seq; | |
6460 | ||
36596dad | 6461 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 6462 | if (family != NFPROTO_UNSPEC && family != table->family) |
3b49e2e9 PNA |
6463 | continue; |
6464 | ||
36596dad PNA |
6465 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
6466 | if (!nft_is_active(net, flowtable)) | |
6467 | goto cont; | |
6468 | if (idx < s_idx) | |
6469 | goto cont; | |
6470 | if (idx > s_idx) | |
6471 | memset(&cb->args[1], 0, | |
6472 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 6473 | if (filter && filter->table && |
36596dad PNA |
6474 | strcmp(filter->table, table->name)) |
6475 | goto cont; | |
3b49e2e9 | 6476 | |
36596dad PNA |
6477 | if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid, |
6478 | cb->nlh->nlmsg_seq, | |
6479 | NFT_MSG_NEWFLOWTABLE, | |
6480 | NLM_F_MULTI | NLM_F_APPEND, | |
98319cb9 | 6481 | table->family, flowtable) < 0) |
36596dad | 6482 | goto done; |
3b49e2e9 | 6483 | |
36596dad | 6484 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
3b49e2e9 | 6485 | cont: |
36596dad | 6486 | idx++; |
3b49e2e9 PNA |
6487 | } |
6488 | } | |
6489 | done: | |
6490 | rcu_read_unlock(); | |
6491 | ||
6492 | cb->args[0] = idx; | |
6493 | return skb->len; | |
6494 | } | |
6495 | ||
90fd131a | 6496 | static int nf_tables_dump_flowtable_start(struct netlink_callback *cb) |
3b49e2e9 | 6497 | { |
90fd131a FW |
6498 | const struct nlattr * const *nla = cb->data; |
6499 | struct nft_flowtable_filter *filter = NULL; | |
3b49e2e9 | 6500 | |
90fd131a FW |
6501 | if (nla[NFTA_FLOWTABLE_TABLE]) { |
6502 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
6503 | if (!filter) | |
6504 | return -ENOMEM; | |
3b49e2e9 | 6505 | |
90fd131a FW |
6506 | filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE], |
6507 | GFP_ATOMIC); | |
6508 | if (!filter->table) { | |
6509 | kfree(filter); | |
6510 | return -ENOMEM; | |
6511 | } | |
6512 | } | |
3b49e2e9 | 6513 | |
90fd131a | 6514 | cb->data = filter; |
3b49e2e9 PNA |
6515 | return 0; |
6516 | } | |
6517 | ||
90fd131a | 6518 | static int nf_tables_dump_flowtable_done(struct netlink_callback *cb) |
3b49e2e9 | 6519 | { |
90fd131a | 6520 | struct nft_flowtable_filter *filter = cb->data; |
3b49e2e9 | 6521 | |
3b49e2e9 | 6522 | if (!filter) |
90fd131a | 6523 | return 0; |
3b49e2e9 | 6524 | |
90fd131a FW |
6525 | kfree(filter->table); |
6526 | kfree(filter); | |
6527 | ||
6528 | return 0; | |
3b49e2e9 PNA |
6529 | } |
6530 | ||
d9adf22a | 6531 | /* called with rcu_read_lock held */ |
3b49e2e9 PNA |
6532 | static int nf_tables_getflowtable(struct net *net, struct sock *nlsk, |
6533 | struct sk_buff *skb, | |
6534 | const struct nlmsghdr *nlh, | |
6535 | const struct nlattr * const nla[], | |
6536 | struct netlink_ext_ack *extack) | |
6537 | { | |
6538 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
6539 | u8 genmask = nft_genmask_cur(net); | |
6540 | int family = nfmsg->nfgen_family; | |
6541 | struct nft_flowtable *flowtable; | |
3b49e2e9 PNA |
6542 | const struct nft_table *table; |
6543 | struct sk_buff *skb2; | |
6544 | int err; | |
6545 | ||
6546 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
6547 | struct netlink_dump_control c = { | |
90fd131a | 6548 | .start = nf_tables_dump_flowtable_start, |
3b49e2e9 PNA |
6549 | .dump = nf_tables_dump_flowtable, |
6550 | .done = nf_tables_dump_flowtable_done, | |
d9adf22a | 6551 | .module = THIS_MODULE, |
90fd131a | 6552 | .data = (void *)nla, |
3b49e2e9 PNA |
6553 | }; |
6554 | ||
d9adf22a | 6555 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
3b49e2e9 PNA |
6556 | } |
6557 | ||
6558 | if (!nla[NFTA_FLOWTABLE_NAME]) | |
6559 | return -EINVAL; | |
6560 | ||
cac20fcd PNA |
6561 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6562 | genmask); | |
3b49e2e9 PNA |
6563 | if (IS_ERR(table)) |
6564 | return PTR_ERR(table); | |
6565 | ||
cac20fcd PNA |
6566 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
6567 | genmask); | |
03a0120f | 6568 | if (IS_ERR(flowtable)) |
3b49e2e9 PNA |
6569 | return PTR_ERR(flowtable); |
6570 | ||
d9adf22a | 6571 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
3b49e2e9 PNA |
6572 | if (!skb2) |
6573 | return -ENOMEM; | |
6574 | ||
6575 | err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid, | |
6576 | nlh->nlmsg_seq, | |
6577 | NFT_MSG_NEWFLOWTABLE, 0, family, | |
6578 | flowtable); | |
6579 | if (err < 0) | |
6580 | goto err; | |
6581 | ||
6582 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
6583 | err: | |
6584 | kfree_skb(skb2); | |
6585 | return err; | |
6586 | } | |
6587 | ||
6588 | static void nf_tables_flowtable_notify(struct nft_ctx *ctx, | |
6589 | struct nft_flowtable *flowtable, | |
6590 | int event) | |
6591 | { | |
6592 | struct sk_buff *skb; | |
6593 | int err; | |
6594 | ||
6595 | if (ctx->report && | |
6596 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
6597 | return; | |
6598 | ||
6599 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
6600 | if (skb == NULL) | |
6601 | goto err; | |
6602 | ||
6603 | err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid, | |
6604 | ctx->seq, event, 0, | |
36596dad | 6605 | ctx->family, flowtable); |
3b49e2e9 PNA |
6606 | if (err < 0) { |
6607 | kfree_skb(skb); | |
6608 | goto err; | |
6609 | } | |
6610 | ||
6611 | nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, | |
6612 | ctx->report, GFP_KERNEL); | |
6613 | return; | |
6614 | err: | |
6615 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); | |
6616 | } | |
6617 | ||
3b49e2e9 PNA |
6618 | static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) |
6619 | { | |
3f0465a9 PNA |
6620 | struct nft_hook *hook, *next; |
6621 | ||
5acab914 | 6622 | flowtable->data.type->free(&flowtable->data); |
3f0465a9 | 6623 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
5acab914 PNA |
6624 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, |
6625 | FLOW_BLOCK_UNBIND); | |
3f0465a9 PNA |
6626 | list_del_rcu(&hook->list); |
6627 | kfree(hook); | |
6628 | } | |
3b49e2e9 | 6629 | kfree(flowtable->name); |
3b49e2e9 | 6630 | module_put(flowtable->data.type->owner); |
a12486eb | 6631 | kfree(flowtable); |
3b49e2e9 PNA |
6632 | } |
6633 | ||
84d7fce6 PNA |
6634 | static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, |
6635 | u32 portid, u32 seq) | |
6636 | { | |
6637 | struct nlmsghdr *nlh; | |
6638 | struct nfgenmsg *nfmsg; | |
784b4e61 | 6639 | char buf[TASK_COMM_LEN]; |
dedb67c4 | 6640 | int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN); |
84d7fce6 PNA |
6641 | |
6642 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0); | |
6643 | if (nlh == NULL) | |
6644 | goto nla_put_failure; | |
6645 | ||
6646 | nfmsg = nlmsg_data(nlh); | |
6647 | nfmsg->nfgen_family = AF_UNSPEC; | |
6648 | nfmsg->version = NFNETLINK_V0; | |
6649 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); | |
6650 | ||
784b4e61 PS |
6651 | if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)) || |
6652 | nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) || | |
6653 | nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current))) | |
84d7fce6 PNA |
6654 | goto nla_put_failure; |
6655 | ||
053c095a JB |
6656 | nlmsg_end(skb, nlh); |
6657 | return 0; | |
84d7fce6 PNA |
6658 | |
6659 | nla_put_failure: | |
6660 | nlmsg_trim(skb, nlh); | |
6661 | return -EMSGSIZE; | |
6662 | } | |
6663 | ||
3b49e2e9 PNA |
6664 | static void nft_flowtable_event(unsigned long event, struct net_device *dev, |
6665 | struct nft_flowtable *flowtable) | |
6666 | { | |
3f0465a9 | 6667 | struct nft_hook *hook; |
3b49e2e9 | 6668 | |
3f0465a9 PNA |
6669 | list_for_each_entry(hook, &flowtable->hook_list, list) { |
6670 | if (hook->ops.dev != dev) | |
3b49e2e9 PNA |
6671 | continue; |
6672 | ||
5acab914 | 6673 | /* flow_offload_netdev_event() cleans up entries for us. */ |
ff4bf2f4 | 6674 | nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook); |
3f0465a9 PNA |
6675 | list_del_rcu(&hook->list); |
6676 | kfree_rcu(hook, rcu); | |
3b49e2e9 PNA |
6677 | break; |
6678 | } | |
6679 | } | |
6680 | ||
6681 | static int nf_tables_flowtable_event(struct notifier_block *this, | |
6682 | unsigned long event, void *ptr) | |
6683 | { | |
6684 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
6685 | struct nft_flowtable *flowtable; | |
6686 | struct nft_table *table; | |
0a2cf5ee | 6687 | struct net *net; |
3b49e2e9 PNA |
6688 | |
6689 | if (event != NETDEV_UNREGISTER) | |
6690 | return 0; | |
6691 | ||
6a48de01 | 6692 | net = dev_net(dev); |
9e619d87 | 6693 | mutex_lock(&net->nft.commit_mutex); |
0a2cf5ee | 6694 | list_for_each_entry(table, &net->nft.tables, list) { |
36596dad PNA |
6695 | list_for_each_entry(flowtable, &table->flowtables, list) { |
6696 | nft_flowtable_event(event, dev, flowtable); | |
3b49e2e9 PNA |
6697 | } |
6698 | } | |
9e619d87 | 6699 | mutex_unlock(&net->nft.commit_mutex); |
6a48de01 | 6700 | |
3b49e2e9 PNA |
6701 | return NOTIFY_DONE; |
6702 | } | |
6703 | ||
6704 | static struct notifier_block nf_tables_flowtable_notifier = { | |
6705 | .notifier_call = nf_tables_flowtable_event, | |
6706 | }; | |
6707 | ||
25e94a99 PNA |
6708 | static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb, |
6709 | int event) | |
84d7fce6 PNA |
6710 | { |
6711 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
6712 | struct sk_buff *skb2; | |
6713 | int err; | |
6714 | ||
6715 | if (nlmsg_report(nlh) && | |
6716 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
25e94a99 | 6717 | return; |
84d7fce6 | 6718 | |
84d7fce6 PNA |
6719 | skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
6720 | if (skb2 == NULL) | |
6721 | goto err; | |
6722 | ||
6723 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
6724 | nlh->nlmsg_seq); | |
6725 | if (err < 0) { | |
6726 | kfree_skb(skb2); | |
6727 | goto err; | |
6728 | } | |
6729 | ||
25e94a99 PNA |
6730 | nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
6731 | nlmsg_report(nlh), GFP_KERNEL); | |
6732 | return; | |
84d7fce6 | 6733 | err: |
25e94a99 PNA |
6734 | nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
6735 | -ENOBUFS); | |
84d7fce6 PNA |
6736 | } |
6737 | ||
7b8002a1 PNA |
6738 | static int nf_tables_getgen(struct net *net, struct sock *nlsk, |
6739 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
6740 | const struct nlattr * const nla[], |
6741 | struct netlink_ext_ack *extack) | |
84d7fce6 | 6742 | { |
84d7fce6 PNA |
6743 | struct sk_buff *skb2; |
6744 | int err; | |
6745 | ||
d9adf22a | 6746 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
84d7fce6 PNA |
6747 | if (skb2 == NULL) |
6748 | return -ENOMEM; | |
6749 | ||
6750 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
6751 | nlh->nlmsg_seq); | |
6752 | if (err < 0) | |
6753 | goto err; | |
6754 | ||
6755 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
6756 | err: | |
6757 | kfree_skb(skb2); | |
6758 | return err; | |
6759 | } | |
6760 | ||
96518518 PM |
6761 | static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { |
6762 | [NFT_MSG_NEWTABLE] = { | |
55dd6f93 | 6763 | .call_batch = nf_tables_newtable, |
96518518 PM |
6764 | .attr_count = NFTA_TABLE_MAX, |
6765 | .policy = nft_table_policy, | |
6766 | }, | |
6767 | [NFT_MSG_GETTABLE] = { | |
d9adf22a | 6768 | .call_rcu = nf_tables_gettable, |
96518518 PM |
6769 | .attr_count = NFTA_TABLE_MAX, |
6770 | .policy = nft_table_policy, | |
6771 | }, | |
6772 | [NFT_MSG_DELTABLE] = { | |
55dd6f93 | 6773 | .call_batch = nf_tables_deltable, |
96518518 PM |
6774 | .attr_count = NFTA_TABLE_MAX, |
6775 | .policy = nft_table_policy, | |
6776 | }, | |
6777 | [NFT_MSG_NEWCHAIN] = { | |
91c7b38d | 6778 | .call_batch = nf_tables_newchain, |
96518518 PM |
6779 | .attr_count = NFTA_CHAIN_MAX, |
6780 | .policy = nft_chain_policy, | |
6781 | }, | |
6782 | [NFT_MSG_GETCHAIN] = { | |
d9adf22a | 6783 | .call_rcu = nf_tables_getchain, |
96518518 PM |
6784 | .attr_count = NFTA_CHAIN_MAX, |
6785 | .policy = nft_chain_policy, | |
6786 | }, | |
6787 | [NFT_MSG_DELCHAIN] = { | |
91c7b38d | 6788 | .call_batch = nf_tables_delchain, |
96518518 PM |
6789 | .attr_count = NFTA_CHAIN_MAX, |
6790 | .policy = nft_chain_policy, | |
6791 | }, | |
6792 | [NFT_MSG_NEWRULE] = { | |
0628b123 | 6793 | .call_batch = nf_tables_newrule, |
96518518 PM |
6794 | .attr_count = NFTA_RULE_MAX, |
6795 | .policy = nft_rule_policy, | |
6796 | }, | |
6797 | [NFT_MSG_GETRULE] = { | |
d9adf22a | 6798 | .call_rcu = nf_tables_getrule, |
96518518 PM |
6799 | .attr_count = NFTA_RULE_MAX, |
6800 | .policy = nft_rule_policy, | |
6801 | }, | |
6802 | [NFT_MSG_DELRULE] = { | |
0628b123 | 6803 | .call_batch = nf_tables_delrule, |
96518518 PM |
6804 | .attr_count = NFTA_RULE_MAX, |
6805 | .policy = nft_rule_policy, | |
6806 | }, | |
20a69341 | 6807 | [NFT_MSG_NEWSET] = { |
958bee14 | 6808 | .call_batch = nf_tables_newset, |
20a69341 PM |
6809 | .attr_count = NFTA_SET_MAX, |
6810 | .policy = nft_set_policy, | |
6811 | }, | |
6812 | [NFT_MSG_GETSET] = { | |
d9adf22a | 6813 | .call_rcu = nf_tables_getset, |
20a69341 PM |
6814 | .attr_count = NFTA_SET_MAX, |
6815 | .policy = nft_set_policy, | |
6816 | }, | |
6817 | [NFT_MSG_DELSET] = { | |
958bee14 | 6818 | .call_batch = nf_tables_delset, |
20a69341 PM |
6819 | .attr_count = NFTA_SET_MAX, |
6820 | .policy = nft_set_policy, | |
6821 | }, | |
6822 | [NFT_MSG_NEWSETELEM] = { | |
958bee14 | 6823 | .call_batch = nf_tables_newsetelem, |
20a69341 PM |
6824 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
6825 | .policy = nft_set_elem_list_policy, | |
6826 | }, | |
6827 | [NFT_MSG_GETSETELEM] = { | |
d9adf22a | 6828 | .call_rcu = nf_tables_getsetelem, |
20a69341 PM |
6829 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
6830 | .policy = nft_set_elem_list_policy, | |
6831 | }, | |
6832 | [NFT_MSG_DELSETELEM] = { | |
958bee14 | 6833 | .call_batch = nf_tables_delsetelem, |
20a69341 PM |
6834 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
6835 | .policy = nft_set_elem_list_policy, | |
6836 | }, | |
84d7fce6 | 6837 | [NFT_MSG_GETGEN] = { |
d9adf22a | 6838 | .call_rcu = nf_tables_getgen, |
84d7fce6 | 6839 | }, |
e5009240 PNA |
6840 | [NFT_MSG_NEWOBJ] = { |
6841 | .call_batch = nf_tables_newobj, | |
6842 | .attr_count = NFTA_OBJ_MAX, | |
6843 | .policy = nft_obj_policy, | |
6844 | }, | |
6845 | [NFT_MSG_GETOBJ] = { | |
d9adf22a | 6846 | .call_rcu = nf_tables_getobj, |
e5009240 PNA |
6847 | .attr_count = NFTA_OBJ_MAX, |
6848 | .policy = nft_obj_policy, | |
6849 | }, | |
6850 | [NFT_MSG_DELOBJ] = { | |
6851 | .call_batch = nf_tables_delobj, | |
6852 | .attr_count = NFTA_OBJ_MAX, | |
6853 | .policy = nft_obj_policy, | |
6854 | }, | |
43da04a5 | 6855 | [NFT_MSG_GETOBJ_RESET] = { |
d9adf22a | 6856 | .call_rcu = nf_tables_getobj, |
43da04a5 PNA |
6857 | .attr_count = NFTA_OBJ_MAX, |
6858 | .policy = nft_obj_policy, | |
6859 | }, | |
3b49e2e9 PNA |
6860 | [NFT_MSG_NEWFLOWTABLE] = { |
6861 | .call_batch = nf_tables_newflowtable, | |
6862 | .attr_count = NFTA_FLOWTABLE_MAX, | |
6863 | .policy = nft_flowtable_policy, | |
6864 | }, | |
6865 | [NFT_MSG_GETFLOWTABLE] = { | |
d9adf22a | 6866 | .call_rcu = nf_tables_getflowtable, |
3b49e2e9 PNA |
6867 | .attr_count = NFTA_FLOWTABLE_MAX, |
6868 | .policy = nft_flowtable_policy, | |
6869 | }, | |
6870 | [NFT_MSG_DELFLOWTABLE] = { | |
6871 | .call_batch = nf_tables_delflowtable, | |
6872 | .attr_count = NFTA_FLOWTABLE_MAX, | |
6873 | .policy = nft_flowtable_policy, | |
6874 | }, | |
96518518 PM |
6875 | }; |
6876 | ||
a654de8f PNA |
6877 | static int nf_tables_validate(struct net *net) |
6878 | { | |
6879 | struct nft_table *table; | |
6880 | ||
6881 | switch (net->nft.validate_state) { | |
6882 | case NFT_VALIDATE_SKIP: | |
6883 | break; | |
6884 | case NFT_VALIDATE_NEED: | |
6885 | nft_validate_state_update(net, NFT_VALIDATE_DO); | |
6886 | /* fall through */ | |
6887 | case NFT_VALIDATE_DO: | |
6888 | list_for_each_entry(table, &net->nft.tables, list) { | |
6889 | if (nft_table_validate(net, table) < 0) | |
6890 | return -EAGAIN; | |
6891 | } | |
6892 | break; | |
6893 | } | |
6894 | ||
6895 | return 0; | |
6896 | } | |
6897 | ||
66293c46 FW |
6898 | /* a drop policy has to be deferred until all rules have been activated, |
6899 | * otherwise a large ruleset that contains a drop-policy base chain will | |
6900 | * cause all packets to get dropped until the full transaction has been | |
6901 | * processed. | |
6902 | * | |
6903 | * We defer the drop policy until the transaction has been finalized. | |
6904 | */ | |
6905 | static void nft_chain_commit_drop_policy(struct nft_trans *trans) | |
6906 | { | |
6907 | struct nft_base_chain *basechain; | |
6908 | ||
6909 | if (nft_trans_chain_policy(trans) != NF_DROP) | |
6910 | return; | |
6911 | ||
6912 | if (!nft_is_base_chain(trans->ctx.chain)) | |
6913 | return; | |
6914 | ||
6915 | basechain = nft_base_chain(trans->ctx.chain); | |
6916 | basechain->policy = NF_DROP; | |
6917 | } | |
6918 | ||
91c7b38d PNA |
6919 | static void nft_chain_commit_update(struct nft_trans *trans) |
6920 | { | |
6921 | struct nft_base_chain *basechain; | |
6922 | ||
1b2470e5 FW |
6923 | if (nft_trans_chain_name(trans)) { |
6924 | rhltable_remove(&trans->ctx.table->chains_ht, | |
6925 | &trans->ctx.chain->rhlhead, | |
6926 | nft_chain_ht_params); | |
d71efb59 | 6927 | swap(trans->ctx.chain->name, nft_trans_chain_name(trans)); |
1b2470e5 FW |
6928 | rhltable_insert_key(&trans->ctx.table->chains_ht, |
6929 | trans->ctx.chain->name, | |
6930 | &trans->ctx.chain->rhlhead, | |
6931 | nft_chain_ht_params); | |
6932 | } | |
91c7b38d | 6933 | |
f323d954 | 6934 | if (!nft_is_base_chain(trans->ctx.chain)) |
91c7b38d PNA |
6935 | return; |
6936 | ||
53315ac6 FW |
6937 | nft_chain_stats_replace(trans); |
6938 | ||
91c7b38d | 6939 | basechain = nft_base_chain(trans->ctx.chain); |
91c7b38d PNA |
6940 | |
6941 | switch (nft_trans_chain_policy(trans)) { | |
6942 | case NF_DROP: | |
6943 | case NF_ACCEPT: | |
6944 | basechain->policy = nft_trans_chain_policy(trans); | |
6945 | break; | |
6946 | } | |
6947 | } | |
6948 | ||
d62d0ba9 FFM |
6949 | static void nft_obj_commit_update(struct nft_trans *trans) |
6950 | { | |
6951 | struct nft_object *newobj; | |
6952 | struct nft_object *obj; | |
6953 | ||
6954 | obj = nft_trans_obj(trans); | |
6955 | newobj = nft_trans_obj_newobj(trans); | |
6956 | ||
9fedd894 FFM |
6957 | if (obj->ops->update) |
6958 | obj->ops->update(obj, newobj); | |
d62d0ba9 FFM |
6959 | |
6960 | kfree(newobj); | |
6961 | } | |
6962 | ||
2f99aa31 | 6963 | static void nft_commit_release(struct nft_trans *trans) |
c7c32e72 | 6964 | { |
c7c32e72 PNA |
6965 | switch (trans->msg_type) { |
6966 | case NFT_MSG_DELTABLE: | |
6967 | nf_tables_table_destroy(&trans->ctx); | |
6968 | break; | |
9f8aac0b | 6969 | case NFT_MSG_NEWCHAIN: |
53315ac6 | 6970 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b FW |
6971 | kfree(nft_trans_chain_name(trans)); |
6972 | break; | |
c7c32e72 | 6973 | case NFT_MSG_DELCHAIN: |
43a605f2 | 6974 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
6975 | break; |
6976 | case NFT_MSG_DELRULE: | |
6977 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
6978 | break; | |
6979 | case NFT_MSG_DELSET: | |
6980 | nft_set_destroy(nft_trans_set(trans)); | |
6981 | break; | |
61edafbb | 6982 | case NFT_MSG_DELSETELEM: |
3453c927 PNA |
6983 | nf_tables_set_elem_destroy(&trans->ctx, |
6984 | nft_trans_elem_set(trans), | |
59105446 | 6985 | nft_trans_elem(trans).priv); |
61edafbb | 6986 | break; |
e5009240 | 6987 | case NFT_MSG_DELOBJ: |
00bfb320 | 6988 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 6989 | break; |
3b49e2e9 PNA |
6990 | case NFT_MSG_DELFLOWTABLE: |
6991 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
6992 | break; | |
c7c32e72 | 6993 | } |
0935d558 FW |
6994 | |
6995 | if (trans->put_net) | |
6996 | put_net(trans->ctx.net); | |
6997 | ||
c7c32e72 PNA |
6998 | kfree(trans); |
6999 | } | |
7000 | ||
0935d558 | 7001 | static void nf_tables_trans_destroy_work(struct work_struct *w) |
2f99aa31 FW |
7002 | { |
7003 | struct nft_trans *trans, *next; | |
0935d558 FW |
7004 | LIST_HEAD(head); |
7005 | ||
7006 | spin_lock(&nf_tables_destroy_list_lock); | |
7007 | list_splice_init(&nf_tables_destroy_list, &head); | |
7008 | spin_unlock(&nf_tables_destroy_list_lock); | |
2f99aa31 | 7009 | |
0935d558 | 7010 | if (list_empty(&head)) |
2f99aa31 FW |
7011 | return; |
7012 | ||
7013 | synchronize_rcu(); | |
7014 | ||
0935d558 | 7015 | list_for_each_entry_safe(trans, next, &head, list) { |
2f99aa31 FW |
7016 | list_del(&trans->list); |
7017 | nft_commit_release(trans); | |
7018 | } | |
7019 | } | |
7020 | ||
0cbc06b3 FW |
7021 | static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain) |
7022 | { | |
7023 | struct nft_rule *rule; | |
7024 | unsigned int alloc = 0; | |
7025 | int i; | |
7026 | ||
7027 | /* already handled or inactive chain? */ | |
7028 | if (chain->rules_next || !nft_is_active_next(net, chain)) | |
7029 | return 0; | |
7030 | ||
7031 | rule = list_entry(&chain->rules, struct nft_rule, list); | |
7032 | i = 0; | |
7033 | ||
7034 | list_for_each_entry_continue(rule, &chain->rules, list) { | |
7035 | if (nft_is_active_next(net, rule)) | |
7036 | alloc++; | |
7037 | } | |
7038 | ||
7039 | chain->rules_next = nf_tables_chain_alloc_rules(chain, alloc); | |
7040 | if (!chain->rules_next) | |
7041 | return -ENOMEM; | |
7042 | ||
7043 | list_for_each_entry_continue(rule, &chain->rules, list) { | |
7044 | if (nft_is_active_next(net, rule)) | |
7045 | chain->rules_next[i++] = rule; | |
7046 | } | |
7047 | ||
7048 | chain->rules_next[i] = NULL; | |
7049 | return 0; | |
7050 | } | |
7051 | ||
7052 | static void nf_tables_commit_chain_prepare_cancel(struct net *net) | |
7053 | { | |
7054 | struct nft_trans *trans, *next; | |
7055 | ||
7056 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { | |
7057 | struct nft_chain *chain = trans->ctx.chain; | |
7058 | ||
7059 | if (trans->msg_type == NFT_MSG_NEWRULE || | |
7060 | trans->msg_type == NFT_MSG_DELRULE) { | |
7061 | kvfree(chain->rules_next); | |
7062 | chain->rules_next = NULL; | |
7063 | } | |
7064 | } | |
7065 | } | |
7066 | ||
7067 | static void __nf_tables_commit_chain_free_rules_old(struct rcu_head *h) | |
7068 | { | |
7069 | struct nft_rules_old *o = container_of(h, struct nft_rules_old, h); | |
7070 | ||
7071 | kvfree(o->start); | |
7072 | } | |
7073 | ||
7074 | static void nf_tables_commit_chain_free_rules_old(struct nft_rule **rules) | |
7075 | { | |
7076 | struct nft_rule **r = rules; | |
7077 | struct nft_rules_old *old; | |
7078 | ||
7079 | while (*r) | |
7080 | r++; | |
7081 | ||
7082 | r++; /* rcu_head is after end marker */ | |
7083 | old = (void *) r; | |
7084 | old->start = rules; | |
7085 | ||
7086 | call_rcu(&old->h, __nf_tables_commit_chain_free_rules_old); | |
7087 | } | |
7088 | ||
0fb39bbe | 7089 | static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain) |
0cbc06b3 FW |
7090 | { |
7091 | struct nft_rule **g0, **g1; | |
7092 | bool next_genbit; | |
7093 | ||
7094 | next_genbit = nft_gencursor_next(net); | |
7095 | ||
7096 | g0 = rcu_dereference_protected(chain->rules_gen_0, | |
f102d66b | 7097 | lockdep_commit_lock_is_held(net)); |
0cbc06b3 | 7098 | g1 = rcu_dereference_protected(chain->rules_gen_1, |
f102d66b | 7099 | lockdep_commit_lock_is_held(net)); |
0cbc06b3 FW |
7100 | |
7101 | /* No changes to this chain? */ | |
7102 | if (chain->rules_next == NULL) { | |
7103 | /* chain had no change in last or next generation */ | |
7104 | if (g0 == g1) | |
7105 | return; | |
7106 | /* | |
7107 | * chain had no change in this generation; make sure next | |
7108 | * one uses same rules as current generation. | |
7109 | */ | |
7110 | if (next_genbit) { | |
7111 | rcu_assign_pointer(chain->rules_gen_1, g0); | |
7112 | nf_tables_commit_chain_free_rules_old(g1); | |
7113 | } else { | |
7114 | rcu_assign_pointer(chain->rules_gen_0, g1); | |
7115 | nf_tables_commit_chain_free_rules_old(g0); | |
7116 | } | |
7117 | ||
7118 | return; | |
7119 | } | |
7120 | ||
7121 | if (next_genbit) | |
7122 | rcu_assign_pointer(chain->rules_gen_1, chain->rules_next); | |
7123 | else | |
7124 | rcu_assign_pointer(chain->rules_gen_0, chain->rules_next); | |
7125 | ||
7126 | chain->rules_next = NULL; | |
7127 | ||
7128 | if (g0 == g1) | |
7129 | return; | |
7130 | ||
7131 | if (next_genbit) | |
7132 | nf_tables_commit_chain_free_rules_old(g1); | |
7133 | else | |
7134 | nf_tables_commit_chain_free_rules_old(g0); | |
7135 | } | |
7136 | ||
d152159b FW |
7137 | static void nft_obj_del(struct nft_object *obj) |
7138 | { | |
4d44175a | 7139 | rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params); |
d152159b FW |
7140 | list_del_rcu(&obj->list); |
7141 | } | |
7142 | ||
1b2470e5 FW |
7143 | static void nft_chain_del(struct nft_chain *chain) |
7144 | { | |
7145 | struct nft_table *table = chain->table; | |
7146 | ||
7147 | WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead, | |
7148 | nft_chain_ht_params)); | |
7149 | list_del_rcu(&chain->list); | |
7150 | } | |
7151 | ||
eb014de4 PNA |
7152 | static void nf_tables_module_autoload_cleanup(struct net *net) |
7153 | { | |
7154 | struct nft_module_request *req, *next; | |
7155 | ||
7156 | WARN_ON_ONCE(!list_empty(&net->nft.commit_list)); | |
7157 | list_for_each_entry_safe(req, next, &net->nft.module_list, list) { | |
7158 | WARN_ON_ONCE(!req->done); | |
7159 | list_del(&req->list); | |
7160 | kfree(req); | |
7161 | } | |
7162 | } | |
7163 | ||
0935d558 FW |
7164 | static void nf_tables_commit_release(struct net *net) |
7165 | { | |
7166 | struct nft_trans *trans; | |
7167 | ||
7168 | /* all side effects have to be made visible. | |
7169 | * For example, if a chain named 'foo' has been deleted, a | |
7170 | * new transaction must not find it anymore. | |
7171 | * | |
7172 | * Memory reclaim happens asynchronously from work queue | |
7173 | * to prevent expensive synchronize_rcu() in commit phase. | |
7174 | */ | |
7175 | if (list_empty(&net->nft.commit_list)) { | |
eb014de4 | 7176 | nf_tables_module_autoload_cleanup(net); |
0935d558 FW |
7177 | mutex_unlock(&net->nft.commit_mutex); |
7178 | return; | |
7179 | } | |
7180 | ||
7181 | trans = list_last_entry(&net->nft.commit_list, | |
7182 | struct nft_trans, list); | |
7183 | get_net(trans->ctx.net); | |
7184 | WARN_ON_ONCE(trans->put_net); | |
7185 | ||
7186 | trans->put_net = true; | |
7187 | spin_lock(&nf_tables_destroy_list_lock); | |
7188 | list_splice_tail_init(&net->nft.commit_list, &nf_tables_destroy_list); | |
7189 | spin_unlock(&nf_tables_destroy_list_lock); | |
7190 | ||
eb014de4 | 7191 | nf_tables_module_autoload_cleanup(net); |
0935d558 FW |
7192 | mutex_unlock(&net->nft.commit_mutex); |
7193 | ||
7194 | schedule_work(&trans_destroy_work); | |
7195 | } | |
7196 | ||
5913beaf | 7197 | static int nf_tables_commit(struct net *net, struct sk_buff *skb) |
37082f93 | 7198 | { |
37082f93 | 7199 | struct nft_trans *trans, *next; |
a3716e70 | 7200 | struct nft_trans_elem *te; |
0cbc06b3 FW |
7201 | struct nft_chain *chain; |
7202 | struct nft_table *table; | |
c9626a2c | 7203 | int err; |
37082f93 | 7204 | |
b8b27498 FW |
7205 | if (list_empty(&net->nft.commit_list)) { |
7206 | mutex_unlock(&net->nft.commit_mutex); | |
7207 | return 0; | |
7208 | } | |
7209 | ||
a654de8f PNA |
7210 | /* 0. Validate ruleset, otherwise roll back for error reporting. */ |
7211 | if (nf_tables_validate(net) < 0) | |
7212 | return -EAGAIN; | |
7213 | ||
c9626a2c PNA |
7214 | err = nft_flow_rule_offload_commit(net); |
7215 | if (err < 0) | |
7216 | return err; | |
7217 | ||
0cbc06b3 FW |
7218 | /* 1. Allocate space for next generation rules_gen_X[] */ |
7219 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { | |
7220 | int ret; | |
37082f93 | 7221 | |
0cbc06b3 FW |
7222 | if (trans->msg_type == NFT_MSG_NEWRULE || |
7223 | trans->msg_type == NFT_MSG_DELRULE) { | |
7224 | chain = trans->ctx.chain; | |
7225 | ||
7226 | ret = nf_tables_commit_chain_prepare(net, chain); | |
7227 | if (ret < 0) { | |
7228 | nf_tables_commit_chain_prepare_cancel(net); | |
7229 | return ret; | |
7230 | } | |
7231 | } | |
7232 | } | |
37082f93 | 7233 | |
0cbc06b3 FW |
7234 | /* step 2. Make rules_gen_X visible to packet path */ |
7235 | list_for_each_entry(table, &net->nft.tables, list) { | |
0fb39bbe FW |
7236 | list_for_each_entry(chain, &table->chains, list) |
7237 | nf_tables_commit_chain(net, chain); | |
0cbc06b3 FW |
7238 | } |
7239 | ||
7240 | /* | |
7241 | * Bump generation counter, invalidate any dump in progress. | |
7242 | * Cannot fail after this point. | |
37082f93 | 7243 | */ |
0cbc06b3 FW |
7244 | while (++net->nft.base_seq == 0); |
7245 | ||
7246 | /* step 3. Start new generation, rules_gen_X now in use. */ | |
7247 | net->nft.gencursor = nft_gencursor_next(net); | |
37082f93 PNA |
7248 | |
7249 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { | |
b380e5c7 | 7250 | switch (trans->msg_type) { |
55dd6f93 PNA |
7251 | case NFT_MSG_NEWTABLE: |
7252 | if (nft_trans_table_update(trans)) { | |
7253 | if (!nft_trans_table_enable(trans)) { | |
664b0f8c | 7254 | nf_tables_table_disable(net, |
55dd6f93 PNA |
7255 | trans->ctx.table); |
7256 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; | |
7257 | } | |
7258 | } else { | |
f2a6d766 | 7259 | nft_clear(net, trans->ctx.table); |
55dd6f93 | 7260 | } |
35151d84 | 7261 | nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE); |
55dd6f93 PNA |
7262 | nft_trans_destroy(trans); |
7263 | break; | |
7264 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 7265 | list_del_rcu(&trans->ctx.table->list); |
35151d84 | 7266 | nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE); |
55dd6f93 | 7267 | break; |
91c7b38d | 7268 | case NFT_MSG_NEWCHAIN: |
9f8aac0b | 7269 | if (nft_trans_chain_update(trans)) { |
91c7b38d | 7270 | nft_chain_commit_update(trans); |
9f8aac0b FW |
7271 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
7272 | /* trans destroyed after rcu grace period */ | |
7273 | } else { | |
66293c46 | 7274 | nft_chain_commit_drop_policy(trans); |
664b0f8c | 7275 | nft_clear(net, trans->ctx.chain); |
9f8aac0b FW |
7276 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
7277 | nft_trans_destroy(trans); | |
7278 | } | |
91c7b38d PNA |
7279 | break; |
7280 | case NFT_MSG_DELCHAIN: | |
1b2470e5 | 7281 | nft_chain_del(trans->ctx.chain); |
35151d84 | 7282 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN); |
c974a3a3 PNA |
7283 | nf_tables_unregister_hook(trans->ctx.net, |
7284 | trans->ctx.table, | |
7285 | trans->ctx.chain); | |
91c7b38d | 7286 | break; |
b380e5c7 | 7287 | case NFT_MSG_NEWRULE: |
889f7ee7 | 7288 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
35151d84 | 7289 | nf_tables_rule_notify(&trans->ctx, |
37082f93 | 7290 | nft_trans_rule(trans), |
35151d84 | 7291 | NFT_MSG_NEWRULE); |
37082f93 | 7292 | nft_trans_destroy(trans); |
b380e5c7 PNA |
7293 | break; |
7294 | case NFT_MSG_DELRULE: | |
7295 | list_del_rcu(&nft_trans_rule(trans)->list); | |
35151d84 PNA |
7296 | nf_tables_rule_notify(&trans->ctx, |
7297 | nft_trans_rule(trans), | |
7298 | NFT_MSG_DELRULE); | |
f6ac8585 PNA |
7299 | nft_rule_expr_deactivate(&trans->ctx, |
7300 | nft_trans_rule(trans), | |
7301 | NFT_TRANS_COMMIT); | |
b380e5c7 | 7302 | break; |
958bee14 | 7303 | case NFT_MSG_NEWSET: |
37a9cc52 | 7304 | nft_clear(net, nft_trans_set(trans)); |
4fefee57 PNA |
7305 | /* This avoids hitting -EBUSY when deleting the table |
7306 | * from the transaction. | |
7307 | */ | |
408070d6 | 7308 | if (nft_set_is_anonymous(nft_trans_set(trans)) && |
4fefee57 PNA |
7309 | !list_empty(&nft_trans_set(trans)->bindings)) |
7310 | trans->ctx.table->use--; | |
7311 | ||
958bee14 | 7312 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 7313 | NFT_MSG_NEWSET, GFP_KERNEL); |
958bee14 PNA |
7314 | nft_trans_destroy(trans); |
7315 | break; | |
7316 | case NFT_MSG_DELSET: | |
37a9cc52 | 7317 | list_del_rcu(&nft_trans_set(trans)->list); |
958bee14 | 7318 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 7319 | NFT_MSG_DELSET, GFP_KERNEL); |
958bee14 | 7320 | break; |
60319eb1 | 7321 | case NFT_MSG_NEWSETELEM: |
cc02e457 PM |
7322 | te = (struct nft_trans_elem *)trans->data; |
7323 | ||
42a55769 | 7324 | te->set->ops->activate(net, te->set, &te->elem); |
cc02e457 PM |
7325 | nf_tables_setelem_notify(&trans->ctx, te->set, |
7326 | &te->elem, | |
60319eb1 PNA |
7327 | NFT_MSG_NEWSETELEM, 0); |
7328 | nft_trans_destroy(trans); | |
7329 | break; | |
7330 | case NFT_MSG_DELSETELEM: | |
a3716e70 | 7331 | te = (struct nft_trans_elem *)trans->data; |
fe2811eb | 7332 | |
a3716e70 PNA |
7333 | nf_tables_setelem_notify(&trans->ctx, te->set, |
7334 | &te->elem, | |
60319eb1 | 7335 | NFT_MSG_DELSETELEM, 0); |
5cb82a38 | 7336 | te->set->ops->remove(net, te->set, &te->elem); |
3dd0673a PM |
7337 | atomic_dec(&te->set->nelems); |
7338 | te->set->ndeact--; | |
60319eb1 | 7339 | break; |
e5009240 | 7340 | case NFT_MSG_NEWOBJ: |
d62d0ba9 FFM |
7341 | if (nft_trans_obj_update(trans)) { |
7342 | nft_obj_commit_update(trans); | |
7343 | nf_tables_obj_notify(&trans->ctx, | |
7344 | nft_trans_obj(trans), | |
7345 | NFT_MSG_NEWOBJ); | |
7346 | } else { | |
7347 | nft_clear(net, nft_trans_obj(trans)); | |
7348 | nf_tables_obj_notify(&trans->ctx, | |
7349 | nft_trans_obj(trans), | |
7350 | NFT_MSG_NEWOBJ); | |
7351 | nft_trans_destroy(trans); | |
7352 | } | |
e5009240 PNA |
7353 | break; |
7354 | case NFT_MSG_DELOBJ: | |
d152159b | 7355 | nft_obj_del(nft_trans_obj(trans)); |
e5009240 PNA |
7356 | nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans), |
7357 | NFT_MSG_DELOBJ); | |
7358 | break; | |
3b49e2e9 PNA |
7359 | case NFT_MSG_NEWFLOWTABLE: |
7360 | nft_clear(net, nft_trans_flowtable(trans)); | |
7361 | nf_tables_flowtable_notify(&trans->ctx, | |
7362 | nft_trans_flowtable(trans), | |
7363 | NFT_MSG_NEWFLOWTABLE); | |
7364 | nft_trans_destroy(trans); | |
7365 | break; | |
7366 | case NFT_MSG_DELFLOWTABLE: | |
7367 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
7368 | nf_tables_flowtable_notify(&trans->ctx, | |
7369 | nft_trans_flowtable(trans), | |
7370 | NFT_MSG_DELFLOWTABLE); | |
7371 | nft_unregister_flowtable_net_hooks(net, | |
7372 | nft_trans_flowtable(trans)); | |
7373 | break; | |
37082f93 | 7374 | } |
37082f93 PNA |
7375 | } |
7376 | ||
84d7fce6 | 7377 | nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN); |
0935d558 | 7378 | nf_tables_commit_release(net); |
37082f93 PNA |
7379 | |
7380 | return 0; | |
7381 | } | |
7382 | ||
eb014de4 PNA |
7383 | static void nf_tables_module_autoload(struct net *net) |
7384 | { | |
7385 | struct nft_module_request *req, *next; | |
7386 | LIST_HEAD(module_list); | |
7387 | ||
7388 | list_splice_init(&net->nft.module_list, &module_list); | |
7389 | mutex_unlock(&net->nft.commit_mutex); | |
7390 | list_for_each_entry_safe(req, next, &module_list, list) { | |
1d305ba4 FW |
7391 | request_module("%s", req->module); |
7392 | req->done = true; | |
eb014de4 PNA |
7393 | } |
7394 | mutex_lock(&net->nft.commit_mutex); | |
7395 | list_splice(&module_list, &net->nft.module_list); | |
7396 | } | |
7397 | ||
b326dd37 | 7398 | static void nf_tables_abort_release(struct nft_trans *trans) |
c7c32e72 | 7399 | { |
c7c32e72 PNA |
7400 | switch (trans->msg_type) { |
7401 | case NFT_MSG_NEWTABLE: | |
7402 | nf_tables_table_destroy(&trans->ctx); | |
7403 | break; | |
7404 | case NFT_MSG_NEWCHAIN: | |
43a605f2 | 7405 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
7406 | break; |
7407 | case NFT_MSG_NEWRULE: | |
7408 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
7409 | break; | |
7410 | case NFT_MSG_NEWSET: | |
40ba1d9b | 7411 | nft_set_destroy(nft_trans_set(trans)); |
c7c32e72 | 7412 | break; |
61edafbb PM |
7413 | case NFT_MSG_NEWSETELEM: |
7414 | nft_set_elem_destroy(nft_trans_elem_set(trans), | |
61f9e292 | 7415 | nft_trans_elem(trans).priv, true); |
61edafbb | 7416 | break; |
e5009240 | 7417 | case NFT_MSG_NEWOBJ: |
00bfb320 | 7418 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 7419 | break; |
3b49e2e9 PNA |
7420 | case NFT_MSG_NEWFLOWTABLE: |
7421 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
7422 | break; | |
c7c32e72 PNA |
7423 | } |
7424 | kfree(trans); | |
7425 | } | |
7426 | ||
eb014de4 | 7427 | static int __nf_tables_abort(struct net *net, bool autoload) |
37082f93 | 7428 | { |
37082f93 | 7429 | struct nft_trans *trans, *next; |
02263db0 | 7430 | struct nft_trans_elem *te; |
37082f93 | 7431 | |
a907e36d XL |
7432 | list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list, |
7433 | list) { | |
b380e5c7 | 7434 | switch (trans->msg_type) { |
55dd6f93 PNA |
7435 | case NFT_MSG_NEWTABLE: |
7436 | if (nft_trans_table_update(trans)) { | |
7437 | if (nft_trans_table_enable(trans)) { | |
664b0f8c | 7438 | nf_tables_table_disable(net, |
55dd6f93 PNA |
7439 | trans->ctx.table); |
7440 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; | |
7441 | } | |
7442 | nft_trans_destroy(trans); | |
7443 | } else { | |
e688a7f8 | 7444 | list_del_rcu(&trans->ctx.table->list); |
55dd6f93 PNA |
7445 | } |
7446 | break; | |
7447 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 7448 | nft_clear(trans->ctx.net, trans->ctx.table); |
55dd6f93 PNA |
7449 | nft_trans_destroy(trans); |
7450 | break; | |
91c7b38d PNA |
7451 | case NFT_MSG_NEWCHAIN: |
7452 | if (nft_trans_chain_update(trans)) { | |
982f4051 | 7453 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b | 7454 | kfree(nft_trans_chain_name(trans)); |
91c7b38d PNA |
7455 | nft_trans_destroy(trans); |
7456 | } else { | |
4fefee57 | 7457 | trans->ctx.table->use--; |
1b2470e5 | 7458 | nft_chain_del(trans->ctx.chain); |
c974a3a3 PNA |
7459 | nf_tables_unregister_hook(trans->ctx.net, |
7460 | trans->ctx.table, | |
7461 | trans->ctx.chain); | |
91c7b38d PNA |
7462 | } |
7463 | break; | |
7464 | case NFT_MSG_DELCHAIN: | |
4fefee57 | 7465 | trans->ctx.table->use++; |
664b0f8c | 7466 | nft_clear(trans->ctx.net, trans->ctx.chain); |
91c7b38d PNA |
7467 | nft_trans_destroy(trans); |
7468 | break; | |
b380e5c7 | 7469 | case NFT_MSG_NEWRULE: |
4fefee57 | 7470 | trans->ctx.chain->use--; |
b380e5c7 | 7471 | list_del_rcu(&nft_trans_rule(trans)->list); |
f6ac8585 PNA |
7472 | nft_rule_expr_deactivate(&trans->ctx, |
7473 | nft_trans_rule(trans), | |
7474 | NFT_TRANS_ABORT); | |
b380e5c7 PNA |
7475 | break; |
7476 | case NFT_MSG_DELRULE: | |
4fefee57 | 7477 | trans->ctx.chain->use++; |
889f7ee7 | 7478 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
bb7b40ae | 7479 | nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans)); |
37082f93 | 7480 | nft_trans_destroy(trans); |
b380e5c7 | 7481 | break; |
958bee14 | 7482 | case NFT_MSG_NEWSET: |
4fefee57 | 7483 | trans->ctx.table->use--; |
6a0a8d10 | 7484 | if (nft_trans_set_bound(trans)) { |
40ba1d9b PNA |
7485 | nft_trans_destroy(trans); |
7486 | break; | |
7487 | } | |
7488 | list_del_rcu(&nft_trans_set(trans)->list); | |
958bee14 PNA |
7489 | break; |
7490 | case NFT_MSG_DELSET: | |
4fefee57 | 7491 | trans->ctx.table->use++; |
37a9cc52 | 7492 | nft_clear(trans->ctx.net, nft_trans_set(trans)); |
958bee14 PNA |
7493 | nft_trans_destroy(trans); |
7494 | break; | |
60319eb1 | 7495 | case NFT_MSG_NEWSETELEM: |
6a0a8d10 | 7496 | if (nft_trans_elem_set_bound(trans)) { |
40ba1d9b PNA |
7497 | nft_trans_destroy(trans); |
7498 | break; | |
7499 | } | |
02263db0 | 7500 | te = (struct nft_trans_elem *)trans->data; |
5cb82a38 | 7501 | te->set->ops->remove(net, te->set, &te->elem); |
3dd0673a | 7502 | atomic_dec(&te->set->nelems); |
60319eb1 PNA |
7503 | break; |
7504 | case NFT_MSG_DELSETELEM: | |
cc02e457 PM |
7505 | te = (struct nft_trans_elem *)trans->data; |
7506 | ||
59105446 | 7507 | nft_set_elem_activate(net, te->set, &te->elem); |
42a55769 | 7508 | te->set->ops->activate(net, te->set, &te->elem); |
3dd0673a | 7509 | te->set->ndeact--; |
cc02e457 | 7510 | |
e5009240 PNA |
7511 | nft_trans_destroy(trans); |
7512 | break; | |
7513 | case NFT_MSG_NEWOBJ: | |
d62d0ba9 FFM |
7514 | if (nft_trans_obj_update(trans)) { |
7515 | kfree(nft_trans_obj_newobj(trans)); | |
7516 | nft_trans_destroy(trans); | |
7517 | } else { | |
7518 | trans->ctx.table->use--; | |
7519 | nft_obj_del(nft_trans_obj(trans)); | |
7520 | } | |
e5009240 PNA |
7521 | break; |
7522 | case NFT_MSG_DELOBJ: | |
7523 | trans->ctx.table->use++; | |
7524 | nft_clear(trans->ctx.net, nft_trans_obj(trans)); | |
60319eb1 PNA |
7525 | nft_trans_destroy(trans); |
7526 | break; | |
3b49e2e9 PNA |
7527 | case NFT_MSG_NEWFLOWTABLE: |
7528 | trans->ctx.table->use--; | |
7529 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
7530 | nft_unregister_flowtable_net_hooks(net, | |
7531 | nft_trans_flowtable(trans)); | |
7532 | break; | |
7533 | case NFT_MSG_DELFLOWTABLE: | |
7534 | trans->ctx.table->use++; | |
7535 | nft_clear(trans->ctx.net, nft_trans_flowtable(trans)); | |
7536 | nft_trans_destroy(trans); | |
7537 | break; | |
37082f93 | 7538 | } |
37082f93 PNA |
7539 | } |
7540 | ||
b326dd37 PNA |
7541 | synchronize_rcu(); |
7542 | ||
a1cee076 PNA |
7543 | list_for_each_entry_safe_reverse(trans, next, |
7544 | &net->nft.commit_list, list) { | |
c7c32e72 | 7545 | list_del(&trans->list); |
b326dd37 | 7546 | nf_tables_abort_release(trans); |
37082f93 PNA |
7547 | } |
7548 | ||
eb014de4 PNA |
7549 | if (autoload) |
7550 | nf_tables_module_autoload(net); | |
7551 | else | |
7552 | nf_tables_module_autoload_cleanup(net); | |
7553 | ||
37082f93 PNA |
7554 | return 0; |
7555 | } | |
7556 | ||
a654de8f PNA |
7557 | static void nf_tables_cleanup(struct net *net) |
7558 | { | |
7559 | nft_validate_state_update(net, NFT_VALIDATE_SKIP); | |
7560 | } | |
7561 | ||
eb014de4 | 7562 | static int nf_tables_abort(struct net *net, struct sk_buff *skb, bool autoload) |
71ad00c5 | 7563 | { |
eb014de4 | 7564 | int ret = __nf_tables_abort(net, autoload); |
f102d66b FW |
7565 | |
7566 | mutex_unlock(&net->nft.commit_mutex); | |
7567 | ||
7568 | return ret; | |
71ad00c5 FW |
7569 | } |
7570 | ||
74e8bcd2 PNA |
7571 | static bool nf_tables_valid_genid(struct net *net, u32 genid) |
7572 | { | |
f102d66b FW |
7573 | bool genid_ok; |
7574 | ||
7575 | mutex_lock(&net->nft.commit_mutex); | |
7576 | ||
7577 | genid_ok = genid == 0 || net->nft.base_seq == genid; | |
7578 | if (!genid_ok) | |
7579 | mutex_unlock(&net->nft.commit_mutex); | |
7580 | ||
7581 | /* else, commit mutex has to be released by commit or abort function */ | |
7582 | return genid_ok; | |
74e8bcd2 PNA |
7583 | } |
7584 | ||
96518518 PM |
7585 | static const struct nfnetlink_subsystem nf_tables_subsys = { |
7586 | .name = "nf_tables", | |
7587 | .subsys_id = NFNL_SUBSYS_NFTABLES, | |
7588 | .cb_count = NFT_MSG_MAX, | |
7589 | .cb = nf_tables_cb, | |
0628b123 PNA |
7590 | .commit = nf_tables_commit, |
7591 | .abort = nf_tables_abort, | |
a654de8f | 7592 | .cleanup = nf_tables_cleanup, |
74e8bcd2 | 7593 | .valid_genid = nf_tables_valid_genid, |
be2ab5b4 | 7594 | .owner = THIS_MODULE, |
96518518 PM |
7595 | }; |
7596 | ||
7210e4e3 | 7597 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
32537e91 | 7598 | enum nft_chain_types type) |
7210e4e3 PNA |
7599 | { |
7600 | const struct nft_base_chain *basechain; | |
7601 | ||
f323d954 | 7602 | if (nft_is_base_chain(chain)) { |
7210e4e3 PNA |
7603 | basechain = nft_base_chain(chain); |
7604 | if (basechain->type->type != type) | |
7605 | return -EOPNOTSUPP; | |
7606 | } | |
7607 | return 0; | |
7608 | } | |
7609 | EXPORT_SYMBOL_GPL(nft_chain_validate_dependency); | |
7610 | ||
75e8d06d PNA |
7611 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
7612 | unsigned int hook_flags) | |
7613 | { | |
7614 | struct nft_base_chain *basechain; | |
7615 | ||
f323d954 | 7616 | if (nft_is_base_chain(chain)) { |
75e8d06d PNA |
7617 | basechain = nft_base_chain(chain); |
7618 | ||
c974a3a3 | 7619 | if ((1 << basechain->ops.hooknum) & hook_flags) |
75e8d06d PNA |
7620 | return 0; |
7621 | ||
7622 | return -EOPNOTSUPP; | |
7623 | } | |
7624 | ||
7625 | return 0; | |
7626 | } | |
7627 | EXPORT_SYMBOL_GPL(nft_chain_validate_hooks); | |
7628 | ||
20a69341 PM |
7629 | /* |
7630 | * Loop detection - walk through the ruleset beginning at the destination chain | |
7631 | * of a new jump until either the source chain is reached (loop) or all | |
7632 | * reachable chains have been traversed. | |
7633 | * | |
7634 | * The loop check is performed whenever a new jump verdict is added to an | |
7635 | * expression or verdict map or a verdict map is bound to a new chain. | |
7636 | */ | |
7637 | ||
7638 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
7639 | const struct nft_chain *chain); | |
7640 | ||
7641 | static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx, | |
de70185d | 7642 | struct nft_set *set, |
20a69341 | 7643 | const struct nft_set_iter *iter, |
de70185d | 7644 | struct nft_set_elem *elem) |
20a69341 | 7645 | { |
fe2811eb PM |
7646 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
7647 | const struct nft_data *data; | |
7648 | ||
7649 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
7650 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
62f9c8b4 PNA |
7651 | return 0; |
7652 | ||
fe2811eb | 7653 | data = nft_set_ext_data(ext); |
1ca2e170 | 7654 | switch (data->verdict.code) { |
20a69341 PM |
7655 | case NFT_JUMP: |
7656 | case NFT_GOTO: | |
1ca2e170 | 7657 | return nf_tables_check_loops(ctx, data->verdict.chain); |
20a69341 PM |
7658 | default: |
7659 | return 0; | |
7660 | } | |
7661 | } | |
7662 | ||
7663 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
7664 | const struct nft_chain *chain) | |
7665 | { | |
7666 | const struct nft_rule *rule; | |
7667 | const struct nft_expr *expr, *last; | |
de70185d | 7668 | struct nft_set *set; |
20a69341 PM |
7669 | struct nft_set_binding *binding; |
7670 | struct nft_set_iter iter; | |
20a69341 PM |
7671 | |
7672 | if (ctx->chain == chain) | |
7673 | return -ELOOP; | |
7674 | ||
7675 | list_for_each_entry(rule, &chain->rules, list) { | |
7676 | nft_rule_for_each_expr(expr, last, rule) { | |
a654de8f PNA |
7677 | struct nft_immediate_expr *priv; |
7678 | const struct nft_data *data; | |
0ca743a5 PNA |
7679 | int err; |
7680 | ||
a654de8f | 7681 | if (strcmp(expr->ops->type->name, "immediate")) |
20a69341 PM |
7682 | continue; |
7683 | ||
a654de8f PNA |
7684 | priv = nft_expr_priv(expr); |
7685 | if (priv->dreg != NFT_REG_VERDICT) | |
0ca743a5 | 7686 | continue; |
20a69341 | 7687 | |
a654de8f | 7688 | data = &priv->data; |
1ca2e170 | 7689 | switch (data->verdict.code) { |
20a69341 PM |
7690 | case NFT_JUMP: |
7691 | case NFT_GOTO: | |
1ca2e170 PM |
7692 | err = nf_tables_check_loops(ctx, |
7693 | data->verdict.chain); | |
20a69341 PM |
7694 | if (err < 0) |
7695 | return err; | |
7696 | default: | |
7697 | break; | |
7698 | } | |
7699 | } | |
7700 | } | |
7701 | ||
7702 | list_for_each_entry(set, &ctx->table->sets, list) { | |
37a9cc52 PNA |
7703 | if (!nft_is_active_next(ctx->net, set)) |
7704 | continue; | |
20a69341 PM |
7705 | if (!(set->flags & NFT_SET_MAP) || |
7706 | set->dtype != NFT_DATA_VERDICT) | |
7707 | continue; | |
7708 | ||
7709 | list_for_each_entry(binding, &set->bindings, list) { | |
11113e19 PM |
7710 | if (!(binding->flags & NFT_SET_MAP) || |
7711 | binding->chain != chain) | |
20a69341 PM |
7712 | continue; |
7713 | ||
8588ac09 | 7714 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
7715 | iter.skip = 0; |
7716 | iter.count = 0; | |
7717 | iter.err = 0; | |
7718 | iter.fn = nf_tables_loop_check_setelem; | |
7719 | ||
7720 | set->ops->walk(ctx, set, &iter); | |
7721 | if (iter.err < 0) | |
7722 | return iter.err; | |
7723 | } | |
7724 | } | |
7725 | ||
7726 | return 0; | |
7727 | } | |
7728 | ||
36b701fa LGL |
7729 | /** |
7730 | * nft_parse_u32_check - fetch u32 attribute and check for maximum value | |
7731 | * | |
7732 | * @attr: netlink attribute to fetch value from | |
7733 | * @max: maximum value to be stored in dest | |
7734 | * @dest: pointer to the variable | |
7735 | * | |
7736 | * Parse, check and store a given u32 netlink attribute into variable. | |
7737 | * This function returns -ERANGE if the value goes over maximum value. | |
7738 | * Otherwise a 0 is returned and the attribute value is stored in the | |
7739 | * destination variable. | |
7740 | */ | |
f1d505bb | 7741 | int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest) |
36b701fa | 7742 | { |
09525a09 | 7743 | u32 val; |
36b701fa LGL |
7744 | |
7745 | val = ntohl(nla_get_be32(attr)); | |
7746 | if (val > max) | |
7747 | return -ERANGE; | |
7748 | ||
7749 | *dest = val; | |
7750 | return 0; | |
7751 | } | |
7752 | EXPORT_SYMBOL_GPL(nft_parse_u32_check); | |
7753 | ||
49499c3e PM |
7754 | /** |
7755 | * nft_parse_register - parse a register value from a netlink attribute | |
7756 | * | |
7757 | * @attr: netlink attribute | |
7758 | * | |
7759 | * Parse and translate a register value from a netlink attribute. | |
7760 | * Registers used to be 128 bit wide, these register numbers will be | |
7761 | * mapped to the corresponding 32 bit register numbers. | |
7762 | */ | |
b1c96ed3 PM |
7763 | unsigned int nft_parse_register(const struct nlattr *attr) |
7764 | { | |
49499c3e PM |
7765 | unsigned int reg; |
7766 | ||
7767 | reg = ntohl(nla_get_be32(attr)); | |
7768 | switch (reg) { | |
7769 | case NFT_REG_VERDICT...NFT_REG_4: | |
7770 | return reg * NFT_REG_SIZE / NFT_REG32_SIZE; | |
7771 | default: | |
7772 | return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00; | |
7773 | } | |
b1c96ed3 PM |
7774 | } |
7775 | EXPORT_SYMBOL_GPL(nft_parse_register); | |
7776 | ||
49499c3e PM |
7777 | /** |
7778 | * nft_dump_register - dump a register value to a netlink attribute | |
7779 | * | |
7780 | * @skb: socket buffer | |
7781 | * @attr: attribute number | |
7782 | * @reg: register number | |
7783 | * | |
7784 | * Construct a netlink attribute containing the register number. For | |
7785 | * compatibility reasons, register numbers being a multiple of 4 are | |
7786 | * translated to the corresponding 128 bit register numbers. | |
7787 | */ | |
b1c96ed3 PM |
7788 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg) |
7789 | { | |
49499c3e PM |
7790 | if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0) |
7791 | reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE); | |
7792 | else | |
7793 | reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00; | |
7794 | ||
b1c96ed3 PM |
7795 | return nla_put_be32(skb, attr, htonl(reg)); |
7796 | } | |
7797 | EXPORT_SYMBOL_GPL(nft_dump_register); | |
7798 | ||
96518518 | 7799 | /** |
d07db988 | 7800 | * nft_validate_register_load - validate a load from a register |
96518518 PM |
7801 | * |
7802 | * @reg: the register number | |
d07db988 | 7803 | * @len: the length of the data |
96518518 PM |
7804 | * |
7805 | * Validate that the input register is one of the general purpose | |
d07db988 | 7806 | * registers and that the length of the load is within the bounds. |
96518518 | 7807 | */ |
d07db988 | 7808 | int nft_validate_register_load(enum nft_registers reg, unsigned int len) |
96518518 | 7809 | { |
49499c3e | 7810 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
96518518 | 7811 | return -EINVAL; |
d07db988 PM |
7812 | if (len == 0) |
7813 | return -EINVAL; | |
c593642c | 7814 | if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data)) |
d07db988 | 7815 | return -ERANGE; |
49499c3e | 7816 | |
96518518 PM |
7817 | return 0; |
7818 | } | |
d07db988 | 7819 | EXPORT_SYMBOL_GPL(nft_validate_register_load); |
96518518 | 7820 | |
96518518 | 7821 | /** |
1ec10212 | 7822 | * nft_validate_register_store - validate an expressions' register store |
96518518 PM |
7823 | * |
7824 | * @ctx: context of the expression performing the load | |
7825 | * @reg: the destination register number | |
7826 | * @data: the data to load | |
7827 | * @type: the data type | |
45d9bcda | 7828 | * @len: the length of the data |
96518518 PM |
7829 | * |
7830 | * Validate that a data load uses the appropriate data type for | |
45d9bcda PM |
7831 | * the destination register and the length is within the bounds. |
7832 | * A value of NULL for the data means that its runtime gathered | |
58f40ab6 | 7833 | * data. |
96518518 | 7834 | */ |
1ec10212 PM |
7835 | int nft_validate_register_store(const struct nft_ctx *ctx, |
7836 | enum nft_registers reg, | |
7837 | const struct nft_data *data, | |
7838 | enum nft_data_types type, unsigned int len) | |
96518518 | 7839 | { |
20a69341 PM |
7840 | int err; |
7841 | ||
96518518 PM |
7842 | switch (reg) { |
7843 | case NFT_REG_VERDICT: | |
58f40ab6 | 7844 | if (type != NFT_DATA_VERDICT) |
96518518 | 7845 | return -EINVAL; |
20a69341 | 7846 | |
58f40ab6 | 7847 | if (data != NULL && |
1ca2e170 PM |
7848 | (data->verdict.code == NFT_GOTO || |
7849 | data->verdict.code == NFT_JUMP)) { | |
7850 | err = nf_tables_check_loops(ctx, data->verdict.chain); | |
20a69341 PM |
7851 | if (err < 0) |
7852 | return err; | |
20a69341 PM |
7853 | } |
7854 | ||
96518518 PM |
7855 | return 0; |
7856 | default: | |
49499c3e | 7857 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
27e6d201 | 7858 | return -EINVAL; |
45d9bcda PM |
7859 | if (len == 0) |
7860 | return -EINVAL; | |
49499c3e | 7861 | if (reg * NFT_REG32_SIZE + len > |
c593642c | 7862 | sizeof_field(struct nft_regs, data)) |
45d9bcda | 7863 | return -ERANGE; |
27e6d201 | 7864 | |
96518518 PM |
7865 | if (data != NULL && type != NFT_DATA_VALUE) |
7866 | return -EINVAL; | |
7867 | return 0; | |
7868 | } | |
7869 | } | |
1ec10212 | 7870 | EXPORT_SYMBOL_GPL(nft_validate_register_store); |
96518518 PM |
7871 | |
7872 | static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { | |
7873 | [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, | |
7874 | [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING, | |
7875 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
7876 | }; | |
7877 | ||
7878 | static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data, | |
7879 | struct nft_data_desc *desc, const struct nlattr *nla) | |
7880 | { | |
664b0f8c | 7881 | u8 genmask = nft_genmask_next(ctx->net); |
96518518 PM |
7882 | struct nlattr *tb[NFTA_VERDICT_MAX + 1]; |
7883 | struct nft_chain *chain; | |
7884 | int err; | |
7885 | ||
8cb08174 JB |
7886 | err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla, |
7887 | nft_verdict_policy, NULL); | |
96518518 PM |
7888 | if (err < 0) |
7889 | return err; | |
7890 | ||
7891 | if (!tb[NFTA_VERDICT_CODE]) | |
7892 | return -EINVAL; | |
1ca2e170 | 7893 | data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE])); |
96518518 | 7894 | |
1ca2e170 | 7895 | switch (data->verdict.code) { |
e0abdadc | 7896 | default: |
1ca2e170 | 7897 | switch (data->verdict.code & NF_VERDICT_MASK) { |
e0abdadc PM |
7898 | case NF_ACCEPT: |
7899 | case NF_DROP: | |
7900 | case NF_QUEUE: | |
7901 | break; | |
7902 | default: | |
7903 | return -EINVAL; | |
7904 | } | |
7905 | /* fall through */ | |
96518518 PM |
7906 | case NFT_CONTINUE: |
7907 | case NFT_BREAK: | |
7908 | case NFT_RETURN: | |
96518518 PM |
7909 | break; |
7910 | case NFT_JUMP: | |
7911 | case NFT_GOTO: | |
7912 | if (!tb[NFTA_VERDICT_CHAIN]) | |
7913 | return -EINVAL; | |
f102d66b FW |
7914 | chain = nft_chain_lookup(ctx->net, ctx->table, |
7915 | tb[NFTA_VERDICT_CHAIN], genmask); | |
96518518 PM |
7916 | if (IS_ERR(chain)) |
7917 | return PTR_ERR(chain); | |
f323d954 | 7918 | if (nft_is_base_chain(chain)) |
96518518 PM |
7919 | return -EOPNOTSUPP; |
7920 | ||
96518518 | 7921 | chain->use++; |
1ca2e170 | 7922 | data->verdict.chain = chain; |
96518518 | 7923 | break; |
96518518 PM |
7924 | } |
7925 | ||
4c4ed074 | 7926 | desc->len = sizeof(data->verdict); |
96518518 PM |
7927 | desc->type = NFT_DATA_VERDICT; |
7928 | return 0; | |
7929 | } | |
7930 | ||
7931 | static void nft_verdict_uninit(const struct nft_data *data) | |
7932 | { | |
1ca2e170 | 7933 | switch (data->verdict.code) { |
96518518 PM |
7934 | case NFT_JUMP: |
7935 | case NFT_GOTO: | |
1ca2e170 | 7936 | data->verdict.chain->use--; |
96518518 PM |
7937 | break; |
7938 | } | |
7939 | } | |
7940 | ||
33d5a7b1 | 7941 | int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v) |
96518518 PM |
7942 | { |
7943 | struct nlattr *nest; | |
7944 | ||
ae0be8de | 7945 | nest = nla_nest_start_noflag(skb, type); |
96518518 PM |
7946 | if (!nest) |
7947 | goto nla_put_failure; | |
7948 | ||
33d5a7b1 | 7949 | if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code))) |
96518518 PM |
7950 | goto nla_put_failure; |
7951 | ||
33d5a7b1 | 7952 | switch (v->code) { |
96518518 PM |
7953 | case NFT_JUMP: |
7954 | case NFT_GOTO: | |
1ca2e170 | 7955 | if (nla_put_string(skb, NFTA_VERDICT_CHAIN, |
33d5a7b1 | 7956 | v->chain->name)) |
96518518 PM |
7957 | goto nla_put_failure; |
7958 | } | |
7959 | nla_nest_end(skb, nest); | |
7960 | return 0; | |
7961 | ||
7962 | nla_put_failure: | |
7963 | return -1; | |
7964 | } | |
7965 | ||
d0a11fc3 PM |
7966 | static int nft_value_init(const struct nft_ctx *ctx, |
7967 | struct nft_data *data, unsigned int size, | |
96518518 PM |
7968 | struct nft_data_desc *desc, const struct nlattr *nla) |
7969 | { | |
7970 | unsigned int len; | |
7971 | ||
7972 | len = nla_len(nla); | |
7973 | if (len == 0) | |
7974 | return -EINVAL; | |
d0a11fc3 | 7975 | if (len > size) |
96518518 PM |
7976 | return -EOVERFLOW; |
7977 | ||
d0a11fc3 | 7978 | nla_memcpy(data->data, nla, len); |
96518518 PM |
7979 | desc->type = NFT_DATA_VALUE; |
7980 | desc->len = len; | |
7981 | return 0; | |
7982 | } | |
7983 | ||
7984 | static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data, | |
7985 | unsigned int len) | |
7986 | { | |
7987 | return nla_put(skb, NFTA_DATA_VALUE, len, data->data); | |
7988 | } | |
7989 | ||
7990 | static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = { | |
d0a11fc3 | 7991 | [NFTA_DATA_VALUE] = { .type = NLA_BINARY }, |
96518518 PM |
7992 | [NFTA_DATA_VERDICT] = { .type = NLA_NESTED }, |
7993 | }; | |
7994 | ||
7995 | /** | |
7996 | * nft_data_init - parse nf_tables data netlink attributes | |
7997 | * | |
7998 | * @ctx: context of the expression using the data | |
7999 | * @data: destination struct nft_data | |
d0a11fc3 | 8000 | * @size: maximum data length |
96518518 PM |
8001 | * @desc: data description |
8002 | * @nla: netlink attribute containing data | |
8003 | * | |
8004 | * Parse the netlink data attributes and initialize a struct nft_data. | |
8005 | * The type and length of data are returned in the data description. | |
8006 | * | |
8007 | * The caller can indicate that it only wants to accept data of type | |
8008 | * NFT_DATA_VALUE by passing NULL for the ctx argument. | |
8009 | */ | |
d0a11fc3 PM |
8010 | int nft_data_init(const struct nft_ctx *ctx, |
8011 | struct nft_data *data, unsigned int size, | |
96518518 PM |
8012 | struct nft_data_desc *desc, const struct nlattr *nla) |
8013 | { | |
8014 | struct nlattr *tb[NFTA_DATA_MAX + 1]; | |
8015 | int err; | |
8016 | ||
8cb08174 JB |
8017 | err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla, |
8018 | nft_data_policy, NULL); | |
96518518 PM |
8019 | if (err < 0) |
8020 | return err; | |
8021 | ||
8022 | if (tb[NFTA_DATA_VALUE]) | |
d0a11fc3 PM |
8023 | return nft_value_init(ctx, data, size, desc, |
8024 | tb[NFTA_DATA_VALUE]); | |
96518518 PM |
8025 | if (tb[NFTA_DATA_VERDICT] && ctx != NULL) |
8026 | return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]); | |
8027 | return -EINVAL; | |
8028 | } | |
8029 | EXPORT_SYMBOL_GPL(nft_data_init); | |
8030 | ||
8031 | /** | |
59105446 | 8032 | * nft_data_release - release a nft_data item |
96518518 PM |
8033 | * |
8034 | * @data: struct nft_data to release | |
8035 | * @type: type of data | |
8036 | * | |
8037 | * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
8038 | * all others need to be released by calling this function. | |
8039 | */ | |
59105446 | 8040 | void nft_data_release(const struct nft_data *data, enum nft_data_types type) |
96518518 | 8041 | { |
960bd2c2 | 8042 | if (type < NFT_DATA_VERDICT) |
96518518 | 8043 | return; |
960bd2c2 | 8044 | switch (type) { |
96518518 PM |
8045 | case NFT_DATA_VERDICT: |
8046 | return nft_verdict_uninit(data); | |
8047 | default: | |
8048 | WARN_ON(1); | |
8049 | } | |
8050 | } | |
59105446 | 8051 | EXPORT_SYMBOL_GPL(nft_data_release); |
96518518 PM |
8052 | |
8053 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, | |
8054 | enum nft_data_types type, unsigned int len) | |
8055 | { | |
8056 | struct nlattr *nest; | |
8057 | int err; | |
8058 | ||
ae0be8de | 8059 | nest = nla_nest_start_noflag(skb, attr); |
96518518 PM |
8060 | if (nest == NULL) |
8061 | return -1; | |
8062 | ||
8063 | switch (type) { | |
8064 | case NFT_DATA_VALUE: | |
8065 | err = nft_value_dump(skb, data, len); | |
8066 | break; | |
8067 | case NFT_DATA_VERDICT: | |
33d5a7b1 | 8068 | err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict); |
96518518 PM |
8069 | break; |
8070 | default: | |
8071 | err = -EINVAL; | |
8072 | WARN_ON(1); | |
8073 | } | |
8074 | ||
8075 | nla_nest_end(skb, nest); | |
8076 | return err; | |
8077 | } | |
8078 | EXPORT_SYMBOL_GPL(nft_data_dump); | |
8079 | ||
5ebe0b0e PNA |
8080 | int __nft_release_basechain(struct nft_ctx *ctx) |
8081 | { | |
8082 | struct nft_rule *rule, *nr; | |
8083 | ||
fa5950e4 FW |
8084 | if (WARN_ON(!nft_is_base_chain(ctx->chain))) |
8085 | return 0; | |
5ebe0b0e | 8086 | |
c974a3a3 | 8087 | nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain); |
5ebe0b0e PNA |
8088 | list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) { |
8089 | list_del(&rule->list); | |
8090 | ctx->chain->use--; | |
bb7b40ae | 8091 | nf_tables_rule_release(ctx, rule); |
5ebe0b0e | 8092 | } |
1b2470e5 | 8093 | nft_chain_del(ctx->chain); |
5ebe0b0e | 8094 | ctx->table->use--; |
43a605f2 | 8095 | nf_tables_chain_destroy(ctx); |
5ebe0b0e PNA |
8096 | |
8097 | return 0; | |
8098 | } | |
8099 | EXPORT_SYMBOL_GPL(__nft_release_basechain); | |
8100 | ||
98319cb9 | 8101 | static void __nft_release_tables(struct net *net) |
df05ef87 | 8102 | { |
3b49e2e9 | 8103 | struct nft_flowtable *flowtable, *nf; |
df05ef87 PNA |
8104 | struct nft_table *table, *nt; |
8105 | struct nft_chain *chain, *nc; | |
e5009240 | 8106 | struct nft_object *obj, *ne; |
df05ef87 PNA |
8107 | struct nft_rule *rule, *nr; |
8108 | struct nft_set *set, *ns; | |
8109 | struct nft_ctx ctx = { | |
8110 | .net = net, | |
43a605f2 | 8111 | .family = NFPROTO_NETDEV, |
df05ef87 PNA |
8112 | }; |
8113 | ||
36596dad | 8114 | list_for_each_entry_safe(table, nt, &net->nft.tables, list) { |
98319cb9 | 8115 | ctx.family = table->family; |
dd4cbef7 | 8116 | |
df05ef87 | 8117 | list_for_each_entry(chain, &table->chains, list) |
c974a3a3 | 8118 | nf_tables_unregister_hook(net, table, chain); |
df05ef87 PNA |
8119 | /* No packets are walking on these chains anymore. */ |
8120 | ctx.table = table; | |
8121 | list_for_each_entry(chain, &table->chains, list) { | |
8122 | ctx.chain = chain; | |
8123 | list_for_each_entry_safe(rule, nr, &chain->rules, list) { | |
8124 | list_del(&rule->list); | |
8125 | chain->use--; | |
bb7b40ae | 8126 | nf_tables_rule_release(&ctx, rule); |
df05ef87 PNA |
8127 | } |
8128 | } | |
3b49e2e9 PNA |
8129 | list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) { |
8130 | list_del(&flowtable->list); | |
8131 | table->use--; | |
8132 | nf_tables_flowtable_destroy(flowtable); | |
8133 | } | |
df05ef87 PNA |
8134 | list_for_each_entry_safe(set, ns, &table->sets, list) { |
8135 | list_del(&set->list); | |
8136 | table->use--; | |
8137 | nft_set_destroy(set); | |
8138 | } | |
e5009240 | 8139 | list_for_each_entry_safe(obj, ne, &table->objects, list) { |
d152159b | 8140 | nft_obj_del(obj); |
e5009240 | 8141 | table->use--; |
00bfb320 | 8142 | nft_obj_destroy(&ctx, obj); |
e5009240 | 8143 | } |
df05ef87 | 8144 | list_for_each_entry_safe(chain, nc, &table->chains, list) { |
43a605f2 | 8145 | ctx.chain = chain; |
1b2470e5 | 8146 | nft_chain_del(chain); |
df05ef87 | 8147 | table->use--; |
43a605f2 | 8148 | nf_tables_chain_destroy(&ctx); |
df05ef87 PNA |
8149 | } |
8150 | list_del(&table->list); | |
8151 | nf_tables_table_destroy(&ctx); | |
8152 | } | |
8153 | } | |
8154 | ||
dd4cbef7 PNA |
8155 | static int __net_init nf_tables_init_net(struct net *net) |
8156 | { | |
8157 | INIT_LIST_HEAD(&net->nft.tables); | |
8158 | INIT_LIST_HEAD(&net->nft.commit_list); | |
eb014de4 | 8159 | INIT_LIST_HEAD(&net->nft.module_list); |
f102d66b | 8160 | mutex_init(&net->nft.commit_mutex); |
dd4cbef7 | 8161 | net->nft.base_seq = 1; |
a654de8f PNA |
8162 | net->nft.validate_state = NFT_VALIDATE_SKIP; |
8163 | ||
dd4cbef7 PNA |
8164 | return 0; |
8165 | } | |
8166 | ||
8167 | static void __net_exit nf_tables_exit_net(struct net *net) | |
8168 | { | |
f102d66b | 8169 | mutex_lock(&net->nft.commit_mutex); |
71ad00c5 | 8170 | if (!list_empty(&net->nft.commit_list)) |
eb014de4 | 8171 | __nf_tables_abort(net, false); |
98319cb9 | 8172 | __nft_release_tables(net); |
f102d66b | 8173 | mutex_unlock(&net->nft.commit_mutex); |
dd4cbef7 | 8174 | WARN_ON_ONCE(!list_empty(&net->nft.tables)); |
1d305ba4 | 8175 | WARN_ON_ONCE(!list_empty(&net->nft.module_list)); |
dd4cbef7 PNA |
8176 | } |
8177 | ||
99633ab2 PNA |
8178 | static struct pernet_operations nf_tables_net_ops = { |
8179 | .init = nf_tables_init_net, | |
613d0776 | 8180 | .exit = nf_tables_exit_net, |
99633ab2 PNA |
8181 | }; |
8182 | ||
96518518 PM |
8183 | static int __init nf_tables_module_init(void) |
8184 | { | |
8185 | int err; | |
8186 | ||
0935d558 | 8187 | spin_lock_init(&nf_tables_destroy_list_lock); |
d209df3e FW |
8188 | err = register_pernet_subsys(&nf_tables_net_ops); |
8189 | if (err < 0) | |
8190 | return err; | |
8191 | ||
8192 | err = nft_chain_filter_init(); | |
8193 | if (err < 0) | |
8194 | goto err1; | |
02c7b25e | 8195 | |
96518518 PM |
8196 | err = nf_tables_core_module_init(); |
8197 | if (err < 0) | |
d209df3e | 8198 | goto err2; |
96518518 | 8199 | |
d209df3e | 8200 | err = register_netdevice_notifier(&nf_tables_flowtable_notifier); |
96518518 | 8201 | if (err < 0) |
d209df3e | 8202 | goto err3; |
96518518 | 8203 | |
4d44175a FW |
8204 | err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params); |
8205 | if (err < 0) | |
8206 | goto err4; | |
8207 | ||
06d392cb | 8208 | err = nft_offload_init(); |
8209 | if (err < 0) | |
8210 | goto err5; | |
8211 | ||
d209df3e FW |
8212 | /* must be last */ |
8213 | err = nfnetlink_subsys_register(&nf_tables_subsys); | |
8214 | if (err < 0) | |
06d392cb | 8215 | goto err6; |
3b49e2e9 | 8216 | |
c1deb065 | 8217 | nft_chain_route_init(); |
3474a2c6 | 8218 | |
d209df3e | 8219 | return err; |
06d392cb | 8220 | err6: |
8221 | nft_offload_exit(); | |
4d44175a FW |
8222 | err5: |
8223 | rhltable_destroy(&nft_objname_ht); | |
d209df3e FW |
8224 | err4: |
8225 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); | |
8226 | err3: | |
96518518 | 8227 | nf_tables_core_module_exit(); |
d209df3e FW |
8228 | err2: |
8229 | nft_chain_filter_fini(); | |
8230 | err1: | |
8231 | unregister_pernet_subsys(&nf_tables_net_ops); | |
96518518 PM |
8232 | return err; |
8233 | } | |
8234 | ||
8235 | static void __exit nf_tables_module_exit(void) | |
8236 | { | |
8237 | nfnetlink_subsys_unregister(&nf_tables_subsys); | |
06d392cb | 8238 | nft_offload_exit(); |
3b49e2e9 | 8239 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
0a2cf5ee | 8240 | nft_chain_filter_fini(); |
c1deb065 | 8241 | nft_chain_route_fini(); |
71ad00c5 | 8242 | unregister_pernet_subsys(&nf_tables_net_ops); |
0935d558 | 8243 | cancel_work_sync(&trans_destroy_work); |
1b1bc49c | 8244 | rcu_barrier(); |
4d44175a | 8245 | rhltable_destroy(&nft_objname_ht); |
96518518 | 8246 | nf_tables_core_module_exit(); |
96518518 PM |
8247 | } |
8248 | ||
8249 | module_init(nf_tables_module_init); | |
8250 | module_exit(nf_tables_module_exit); | |
8251 | ||
8252 | MODULE_LICENSE("GPL"); | |
8253 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
8254 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES); |