1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
4 *
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>
13 #include <linux/vmalloc.h>
14 #include <linux/rhashtable.h>
15 #include <linux/audit.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nfnetlink.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_flow_table.h>
20 #include <net/netfilter/nf_tables_core.h>
21 #include <net/netfilter/nf_tables.h>
22 #include <net/netfilter/nf_tables_offload.h>
23 #include <net/net_namespace.h>
24 #include <net/sock.h>
25
26 #define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-"))
27 #define NFT_SET_MAX_ANONLEN 16
28
29 unsigned int nf_tables_net_id __read_mostly;
30
31 static LIST_HEAD(nf_tables_expressions);
32 static LIST_HEAD(nf_tables_objects);
33 static LIST_HEAD(nf_tables_flowtables);
34 static LIST_HEAD(nf_tables_destroy_list);
35 static LIST_HEAD(nf_tables_gc_list);
36 static DEFINE_SPINLOCK(nf_tables_destroy_list_lock);
37 static DEFINE_SPINLOCK(nf_tables_gc_list_lock);
38
39 enum {
40 NFT_VALIDATE_SKIP = 0,
41 NFT_VALIDATE_NEED,
42 NFT_VALIDATE_DO,
43 };
44
45 static struct rhltable nft_objname_ht;
46
47 static u32 nft_chain_hash(const void *data, u32 len, u32 seed);
48 static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed);
49 static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *);
50
51 static u32 nft_objname_hash(const void *data, u32 len, u32 seed);
52 static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed);
53 static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *);
54
55 static const struct rhashtable_params nft_chain_ht_params = {
56 .head_offset = offsetof(struct nft_chain, rhlhead),
57 .key_offset = offsetof(struct nft_chain, name),
58 .hashfn = nft_chain_hash,
59 .obj_hashfn = nft_chain_hash_obj,
60 .obj_cmpfn = nft_chain_hash_cmp,
61 .automatic_shrinking = true,
62 };
63
64 static const struct rhashtable_params nft_objname_ht_params = {
65 .head_offset = offsetof(struct nft_object, rhlhead),
66 .key_offset = offsetof(struct nft_object, key),
67 .hashfn = nft_objname_hash,
68 .obj_hashfn = nft_objname_hash_obj,
69 .obj_cmpfn = nft_objname_hash_cmp,
70 .automatic_shrinking = true,
71 };
72
73 struct nft_audit_data {
74 struct nft_table *table;
75 int entries;
76 int op;
77 struct list_head list;
78 };
79
80 static const u8 nft2audit_op[NFT_MSG_MAX] = { // enum nf_tables_msg_types
81 [NFT_MSG_NEWTABLE] = AUDIT_NFT_OP_TABLE_REGISTER,
82 [NFT_MSG_GETTABLE] = AUDIT_NFT_OP_INVALID,
83 [NFT_MSG_DELTABLE] = AUDIT_NFT_OP_TABLE_UNREGISTER,
84 [NFT_MSG_NEWCHAIN] = AUDIT_NFT_OP_CHAIN_REGISTER,
85 [NFT_MSG_GETCHAIN] = AUDIT_NFT_OP_INVALID,
86 [NFT_MSG_DELCHAIN] = AUDIT_NFT_OP_CHAIN_UNREGISTER,
87 [NFT_MSG_NEWRULE] = AUDIT_NFT_OP_RULE_REGISTER,
88 [NFT_MSG_GETRULE] = AUDIT_NFT_OP_INVALID,
89 [NFT_MSG_DELRULE] = AUDIT_NFT_OP_RULE_UNREGISTER,
90 [NFT_MSG_NEWSET] = AUDIT_NFT_OP_SET_REGISTER,
91 [NFT_MSG_GETSET] = AUDIT_NFT_OP_INVALID,
92 [NFT_MSG_DELSET] = AUDIT_NFT_OP_SET_UNREGISTER,
93 [NFT_MSG_NEWSETELEM] = AUDIT_NFT_OP_SETELEM_REGISTER,
94 [NFT_MSG_GETSETELEM] = AUDIT_NFT_OP_INVALID,
95 [NFT_MSG_DELSETELEM] = AUDIT_NFT_OP_SETELEM_UNREGISTER,
96 [NFT_MSG_NEWGEN] = AUDIT_NFT_OP_GEN_REGISTER,
97 [NFT_MSG_GETGEN] = AUDIT_NFT_OP_INVALID,
98 [NFT_MSG_TRACE] = AUDIT_NFT_OP_INVALID,
99 [NFT_MSG_NEWOBJ] = AUDIT_NFT_OP_OBJ_REGISTER,
100 [NFT_MSG_GETOBJ] = AUDIT_NFT_OP_INVALID,
101 [NFT_MSG_DELOBJ] = AUDIT_NFT_OP_OBJ_UNREGISTER,
102 [NFT_MSG_GETOBJ_RESET] = AUDIT_NFT_OP_OBJ_RESET,
103 [NFT_MSG_NEWFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_REGISTER,
104 [NFT_MSG_GETFLOWTABLE] = AUDIT_NFT_OP_INVALID,
105 [NFT_MSG_DELFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_UNREGISTER,
106 [NFT_MSG_GETSETELEM_RESET] = AUDIT_NFT_OP_SETELEM_RESET,
107 };
108
nft_validate_state_update(struct nft_table * table,u8 new_validate_state)109 static void nft_validate_state_update(struct nft_table *table, u8 new_validate_state)
110 {
111 switch (table->validate_state) {
112 case NFT_VALIDATE_SKIP:
113 WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO);
114 break;
115 case NFT_VALIDATE_NEED:
116 break;
117 case NFT_VALIDATE_DO:
118 if (new_validate_state == NFT_VALIDATE_NEED)
119 return;
120 }
121
122 table->validate_state = new_validate_state;
123 }
124 static void nf_tables_trans_destroy_work(struct work_struct *w);
125 static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work);
126
127 static void nft_trans_gc_work(struct work_struct *work);
128 static DECLARE_WORK(trans_gc_work, nft_trans_gc_work);
129
nft_ctx_init(struct nft_ctx * ctx,struct net * net,const struct sk_buff * skb,const struct nlmsghdr * nlh,u8 family,struct nft_table * table,struct nft_chain * chain,const struct nlattr * const * nla)130 static void nft_ctx_init(struct nft_ctx *ctx,
131 struct net *net,
132 const struct sk_buff *skb,
133 const struct nlmsghdr *nlh,
134 u8 family,
135 struct nft_table *table,
136 struct nft_chain *chain,
137 const struct nlattr * const *nla)
138 {
139 ctx->net = net;
140 ctx->family = family;
141 ctx->level = 0;
142 ctx->table = table;
143 ctx->chain = chain;
144 ctx->nla = nla;
145 ctx->portid = NETLINK_CB(skb).portid;
146 ctx->report = nlmsg_report(nlh);
147 ctx->flags = nlh->nlmsg_flags;
148 ctx->seq = nlh->nlmsg_seq;
149 }
150
nft_trans_alloc_gfp(const struct nft_ctx * ctx,int msg_type,u32 size,gfp_t gfp)151 static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx,
152 int msg_type, u32 size, gfp_t gfp)
153 {
154 struct nft_trans *trans;
155
156 trans = kzalloc(sizeof(struct nft_trans) + size, gfp);
157 if (trans == NULL)
158 return NULL;
159
160 INIT_LIST_HEAD(&trans->list);
161 INIT_LIST_HEAD(&trans->binding_list);
162 trans->msg_type = msg_type;
163 trans->ctx = *ctx;
164
165 return trans;
166 }
167
nft_trans_alloc(const struct nft_ctx * ctx,int msg_type,u32 size)168 static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx,
169 int msg_type, u32 size)
170 {
171 return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL);
172 }
173
nft_trans_list_del(struct nft_trans * trans)174 static void nft_trans_list_del(struct nft_trans *trans)
175 {
176 list_del(&trans->list);
177 list_del(&trans->binding_list);
178 }
179
nft_trans_destroy(struct nft_trans * trans)180 static void nft_trans_destroy(struct nft_trans *trans)
181 {
182 nft_trans_list_del(trans);
183 kfree(trans);
184 }
185
__nft_set_trans_bind(const struct nft_ctx * ctx,struct nft_set * set,bool bind)186 static void __nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set,
187 bool bind)
188 {
189 struct nftables_pernet *nft_net;
190 struct net *net = ctx->net;
191 struct nft_trans *trans;
192
193 if (!nft_set_is_anonymous(set))
194 return;
195
196 nft_net = nft_pernet(net);
197 list_for_each_entry_reverse(trans, &nft_net->commit_list, list) {
198 switch (trans->msg_type) {
199 case NFT_MSG_NEWSET:
200 if (nft_trans_set(trans) == set)
201 nft_trans_set_bound(trans) = bind;
202 break;
203 case NFT_MSG_NEWSETELEM:
204 if (nft_trans_elem_set(trans) == set)
205 nft_trans_elem_set_bound(trans) = bind;
206 break;
207 }
208 }
209 }
210
nft_set_trans_bind(const struct nft_ctx * ctx,struct nft_set * set)211 static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set)
212 {
213 return __nft_set_trans_bind(ctx, set, true);
214 }
215
nft_set_trans_unbind(const struct nft_ctx * ctx,struct nft_set * set)216 static void nft_set_trans_unbind(const struct nft_ctx *ctx, struct nft_set *set)
217 {
218 return __nft_set_trans_bind(ctx, set, false);
219 }
220
__nft_chain_trans_bind(const struct nft_ctx * ctx,struct nft_chain * chain,bool bind)221 static void __nft_chain_trans_bind(const struct nft_ctx *ctx,
222 struct nft_chain *chain, bool bind)
223 {
224 struct nftables_pernet *nft_net;
225 struct net *net = ctx->net;
226 struct nft_trans *trans;
227
228 if (!nft_chain_binding(chain))
229 return;
230
231 nft_net = nft_pernet(net);
232 list_for_each_entry_reverse(trans, &nft_net->commit_list, list) {
233 switch (trans->msg_type) {
234 case NFT_MSG_NEWCHAIN:
235 if (nft_trans_chain(trans) == chain)
236 nft_trans_chain_bound(trans) = bind;
237 break;
238 case NFT_MSG_NEWRULE:
239 if (trans->ctx.chain == chain)
240 nft_trans_rule_bound(trans) = bind;
241 break;
242 }
243 }
244 }
245
nft_chain_trans_bind(const struct nft_ctx * ctx,struct nft_chain * chain)246 static void nft_chain_trans_bind(const struct nft_ctx *ctx,
247 struct nft_chain *chain)
248 {
249 __nft_chain_trans_bind(ctx, chain, true);
250 }
251
nf_tables_bind_chain(const struct nft_ctx * ctx,struct nft_chain * chain)252 int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain)
253 {
254 if (!nft_chain_binding(chain))
255 return 0;
256
257 if (nft_chain_binding(ctx->chain))
258 return -EOPNOTSUPP;
259
260 if (chain->bound)
261 return -EBUSY;
262
263 if (!nft_use_inc(&chain->use))
264 return -EMFILE;
265
266 chain->bound = true;
267 nft_chain_trans_bind(ctx, chain);
268
269 return 0;
270 }
271
nf_tables_unbind_chain(const struct nft_ctx * ctx,struct nft_chain * chain)272 void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain)
273 {
274 __nft_chain_trans_bind(ctx, chain, false);
275 }
276
nft_netdev_register_hooks(struct net * net,struct list_head * hook_list)277 static int nft_netdev_register_hooks(struct net *net,
278 struct list_head *hook_list)
279 {
280 struct nft_hook *hook;
281 int err, j;
282
283 j = 0;
284 list_for_each_entry(hook, hook_list, list) {
285 err = nf_register_net_hook(net, &hook->ops);
286 if (err < 0)
287 goto err_register;
288
289 j++;
290 }
291 return 0;
292
293 err_register:
294 list_for_each_entry(hook, hook_list, list) {
295 if (j-- <= 0)
296 break;
297
298 nf_unregister_net_hook(net, &hook->ops);
299 }
300 return err;
301 }
302
nft_netdev_unregister_hooks(struct net * net,struct list_head * hook_list,bool release_netdev)303 static void nft_netdev_unregister_hooks(struct net *net,
304 struct list_head *hook_list,
305 bool release_netdev)
306 {
307 struct nft_hook *hook, *next;
308
309 list_for_each_entry_safe(hook, next, hook_list, list) {
310 nf_unregister_net_hook(net, &hook->ops);
311 if (release_netdev) {
312 list_del(&hook->list);
313 kfree_rcu(hook, rcu);
314 }
315 }
316 }
317
nf_tables_register_hook(struct net * net,const struct nft_table * table,struct nft_chain * chain)318 static int nf_tables_register_hook(struct net *net,
319 const struct nft_table *table,
320 struct nft_chain *chain)
321 {
322 struct nft_base_chain *basechain;
323 const struct nf_hook_ops *ops;
324
325 if (table->flags & NFT_TABLE_F_DORMANT ||
326 !nft_is_base_chain(chain))
327 return 0;
328
329 basechain = nft_base_chain(chain);
330 ops = &basechain->ops;
331
332 if (basechain->type->ops_register)
333 return basechain->type->ops_register(net, ops);
334
335 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
336 return nft_netdev_register_hooks(net, &basechain->hook_list);
337
338 return nf_register_net_hook(net, &basechain->ops);
339 }
340
__nf_tables_unregister_hook(struct net * net,const struct nft_table * table,struct nft_chain * chain,bool release_netdev)341 static void __nf_tables_unregister_hook(struct net *net,
342 const struct nft_table *table,
343 struct nft_chain *chain,
344 bool release_netdev)
345 {
346 struct nft_base_chain *basechain;
347 const struct nf_hook_ops *ops;
348
349 if (table->flags & NFT_TABLE_F_DORMANT ||
350 !nft_is_base_chain(chain))
351 return;
352 basechain = nft_base_chain(chain);
353 ops = &basechain->ops;
354
355 if (basechain->type->ops_unregister)
356 return basechain->type->ops_unregister(net, ops);
357
358 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
359 nft_netdev_unregister_hooks(net, &basechain->hook_list,
360 release_netdev);
361 else
362 nf_unregister_net_hook(net, &basechain->ops);
363 }
364
nf_tables_unregister_hook(struct net * net,const struct nft_table * table,struct nft_chain * chain)365 static void nf_tables_unregister_hook(struct net *net,
366 const struct nft_table *table,
367 struct nft_chain *chain)
368 {
369 return __nf_tables_unregister_hook(net, table, chain, false);
370 }
371
nft_trans_commit_list_add_tail(struct net * net,struct nft_trans * trans)372 static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *trans)
373 {
374 struct nftables_pernet *nft_net = nft_pernet(net);
375
376 switch (trans->msg_type) {
377 case NFT_MSG_NEWSET:
378 if (!nft_trans_set_update(trans) &&
379 nft_set_is_anonymous(nft_trans_set(trans)))
380 list_add_tail(&trans->binding_list, &nft_net->binding_list);
381 break;
382 case NFT_MSG_NEWCHAIN:
383 if (!nft_trans_chain_update(trans) &&
384 nft_chain_binding(nft_trans_chain(trans)))
385 list_add_tail(&trans->binding_list, &nft_net->binding_list);
386 break;
387 }
388
389 list_add_tail(&trans->list, &nft_net->commit_list);
390 }
391
nft_trans_table_add(struct nft_ctx * ctx,int msg_type)392 static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
393 {
394 struct nft_trans *trans;
395
396 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
397 if (trans == NULL)
398 return -ENOMEM;
399
400 if (msg_type == NFT_MSG_NEWTABLE)
401 nft_activate_next(ctx->net, ctx->table);
402
403 nft_trans_commit_list_add_tail(ctx->net, trans);
404 return 0;
405 }
406
nft_deltable(struct nft_ctx * ctx)407 static int nft_deltable(struct nft_ctx *ctx)
408 {
409 int err;
410
411 err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
412 if (err < 0)
413 return err;
414
415 nft_deactivate_next(ctx->net, ctx->table);
416 return err;
417 }
418
nft_trans_chain_add(struct nft_ctx * ctx,int msg_type)419 static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
420 {
421 struct nft_trans *trans;
422
423 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
424 if (trans == NULL)
425 return ERR_PTR(-ENOMEM);
426
427 if (msg_type == NFT_MSG_NEWCHAIN) {
428 nft_activate_next(ctx->net, ctx->chain);
429
430 if (ctx->nla[NFTA_CHAIN_ID]) {
431 nft_trans_chain_id(trans) =
432 ntohl(nla_get_be32(ctx->nla[NFTA_CHAIN_ID]));
433 }
434 }
435 nft_trans_chain(trans) = ctx->chain;
436 nft_trans_commit_list_add_tail(ctx->net, trans);
437
438 return trans;
439 }
440
nft_delchain(struct nft_ctx * ctx)441 static int nft_delchain(struct nft_ctx *ctx)
442 {
443 struct nft_trans *trans;
444
445 trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
446 if (IS_ERR(trans))
447 return PTR_ERR(trans);
448
449 nft_use_dec(&ctx->table->use);
450 nft_deactivate_next(ctx->net, ctx->chain);
451
452 return 0;
453 }
454
nft_rule_expr_activate(const struct nft_ctx * ctx,struct nft_rule * rule)455 void nft_rule_expr_activate(const struct nft_ctx *ctx, struct nft_rule *rule)
456 {
457 struct nft_expr *expr;
458
459 expr = nft_expr_first(rule);
460 while (nft_expr_more(rule, expr)) {
461 if (expr->ops->activate)
462 expr->ops->activate(ctx, expr);
463
464 expr = nft_expr_next(expr);
465 }
466 }
467
nft_rule_expr_deactivate(const struct nft_ctx * ctx,struct nft_rule * rule,enum nft_trans_phase phase)468 void nft_rule_expr_deactivate(const struct nft_ctx *ctx, struct nft_rule *rule,
469 enum nft_trans_phase phase)
470 {
471 struct nft_expr *expr;
472
473 expr = nft_expr_first(rule);
474 while (nft_expr_more(rule, expr)) {
475 if (expr->ops->deactivate)
476 expr->ops->deactivate(ctx, expr, phase);
477
478 expr = nft_expr_next(expr);
479 }
480 }
481
482 static int
nf_tables_delrule_deactivate(struct nft_ctx * ctx,struct nft_rule * rule)483 nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
484 {
485 /* You cannot delete the same rule twice */
486 if (nft_is_active_next(ctx->net, rule)) {
487 nft_deactivate_next(ctx->net, rule);
488 nft_use_dec(&ctx->chain->use);
489 return 0;
490 }
491 return -ENOENT;
492 }
493
nft_trans_rule_add(struct nft_ctx * ctx,int msg_type,struct nft_rule * rule)494 static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
495 struct nft_rule *rule)
496 {
497 struct nft_trans *trans;
498
499 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
500 if (trans == NULL)
501 return NULL;
502
503 if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) {
504 nft_trans_rule_id(trans) =
505 ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID]));
506 }
507 nft_trans_rule(trans) = rule;
508 nft_trans_commit_list_add_tail(ctx->net, trans);
509
510 return trans;
511 }
512
nft_delrule(struct nft_ctx * ctx,struct nft_rule * rule)513 static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
514 {
515 struct nft_flow_rule *flow;
516 struct nft_trans *trans;
517 int err;
518
519 trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
520 if (trans == NULL)
521 return -ENOMEM;
522
523 if (ctx->chain->flags & NFT_CHAIN_HW_OFFLOAD) {
524 flow = nft_flow_rule_create(ctx->net, rule);
525 if (IS_ERR(flow)) {
526 nft_trans_destroy(trans);
527 return PTR_ERR(flow);
528 }
529
530 nft_trans_flow_rule(trans) = flow;
531 }
532
533 err = nf_tables_delrule_deactivate(ctx, rule);
534 if (err < 0) {
535 nft_trans_destroy(trans);
536 return err;
537 }
538 nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE);
539
540 return 0;
541 }
542
nft_delrule_by_chain(struct nft_ctx * ctx)543 static int nft_delrule_by_chain(struct nft_ctx *ctx)
544 {
545 struct nft_rule *rule;
546 int err;
547
548 list_for_each_entry(rule, &ctx->chain->rules, list) {
549 if (!nft_is_active_next(ctx->net, rule))
550 continue;
551
552 err = nft_delrule(ctx, rule);
553 if (err < 0)
554 return err;
555 }
556 return 0;
557 }
558
__nft_trans_set_add(const struct nft_ctx * ctx,int msg_type,struct nft_set * set,const struct nft_set_desc * desc)559 static int __nft_trans_set_add(const struct nft_ctx *ctx, int msg_type,
560 struct nft_set *set,
561 const struct nft_set_desc *desc)
562 {
563 struct nft_trans *trans;
564
565 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
566 if (trans == NULL)
567 return -ENOMEM;
568
569 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] && !desc) {
570 nft_trans_set_id(trans) =
571 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
572 nft_activate_next(ctx->net, set);
573 }
574 nft_trans_set(trans) = set;
575 if (desc) {
576 nft_trans_set_update(trans) = true;
577 nft_trans_set_gc_int(trans) = desc->gc_int;
578 nft_trans_set_timeout(trans) = desc->timeout;
579 nft_trans_set_size(trans) = desc->size;
580 }
581 nft_trans_commit_list_add_tail(ctx->net, trans);
582
583 return 0;
584 }
585
nft_trans_set_add(const struct nft_ctx * ctx,int msg_type,struct nft_set * set)586 static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type,
587 struct nft_set *set)
588 {
589 return __nft_trans_set_add(ctx, msg_type, set, NULL);
590 }
591
nft_mapelem_deactivate(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)592 static int nft_mapelem_deactivate(const struct nft_ctx *ctx,
593 struct nft_set *set,
594 const struct nft_set_iter *iter,
595 struct nft_set_elem *elem)
596 {
597 struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
598
599 if (!nft_set_elem_active(ext, iter->genmask))
600 return 0;
601
602 nft_set_elem_change_active(ctx->net, set, ext);
603 nft_setelem_data_deactivate(ctx->net, set, elem);
604
605 return 0;
606 }
607
608 struct nft_set_elem_catchall {
609 struct list_head list;
610 struct rcu_head rcu;
611 void *elem;
612 };
613
nft_map_catchall_deactivate(const struct nft_ctx * ctx,struct nft_set * set)614 static void nft_map_catchall_deactivate(const struct nft_ctx *ctx,
615 struct nft_set *set)
616 {
617 u8 genmask = nft_genmask_next(ctx->net);
618 struct nft_set_elem_catchall *catchall;
619 struct nft_set_elem elem;
620 struct nft_set_ext *ext;
621
622 list_for_each_entry(catchall, &set->catchall_list, list) {
623 ext = nft_set_elem_ext(set, catchall->elem);
624 if (!nft_set_elem_active(ext, genmask))
625 continue;
626
627 elem.priv = catchall->elem;
628 nft_set_elem_change_active(ctx->net, set, ext);
629 nft_setelem_data_deactivate(ctx->net, set, &elem);
630 break;
631 }
632 }
633
nft_map_deactivate(const struct nft_ctx * ctx,struct nft_set * set)634 static void nft_map_deactivate(const struct nft_ctx *ctx, struct nft_set *set)
635 {
636 struct nft_set_iter iter = {
637 .genmask = nft_genmask_next(ctx->net),
638 .type = NFT_ITER_UPDATE,
639 .fn = nft_mapelem_deactivate,
640 };
641
642 set->ops->walk(ctx, set, &iter);
643 WARN_ON_ONCE(iter.err);
644
645 nft_map_catchall_deactivate(ctx, set);
646 }
647
nft_delset(const struct nft_ctx * ctx,struct nft_set * set)648 static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set)
649 {
650 int err;
651
652 err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
653 if (err < 0)
654 return err;
655
656 if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
657 nft_map_deactivate(ctx, set);
658
659 nft_deactivate_next(ctx->net, set);
660 nft_use_dec(&ctx->table->use);
661
662 return err;
663 }
664
nft_trans_obj_add(struct nft_ctx * ctx,int msg_type,struct nft_object * obj)665 static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type,
666 struct nft_object *obj)
667 {
668 struct nft_trans *trans;
669
670 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj));
671 if (trans == NULL)
672 return -ENOMEM;
673
674 if (msg_type == NFT_MSG_NEWOBJ)
675 nft_activate_next(ctx->net, obj);
676
677 nft_trans_obj(trans) = obj;
678 nft_trans_commit_list_add_tail(ctx->net, trans);
679
680 return 0;
681 }
682
nft_delobj(struct nft_ctx * ctx,struct nft_object * obj)683 static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj)
684 {
685 int err;
686
687 err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj);
688 if (err < 0)
689 return err;
690
691 nft_deactivate_next(ctx->net, obj);
692 nft_use_dec(&ctx->table->use);
693
694 return err;
695 }
696
697 static struct nft_trans *
nft_trans_flowtable_add(struct nft_ctx * ctx,int msg_type,struct nft_flowtable * flowtable)698 nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type,
699 struct nft_flowtable *flowtable)
700 {
701 struct nft_trans *trans;
702
703 trans = nft_trans_alloc(ctx, msg_type,
704 sizeof(struct nft_trans_flowtable));
705 if (trans == NULL)
706 return ERR_PTR(-ENOMEM);
707
708 if (msg_type == NFT_MSG_NEWFLOWTABLE)
709 nft_activate_next(ctx->net, flowtable);
710
711 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
712 nft_trans_flowtable(trans) = flowtable;
713 nft_trans_commit_list_add_tail(ctx->net, trans);
714
715 return trans;
716 }
717
nft_delflowtable(struct nft_ctx * ctx,struct nft_flowtable * flowtable)718 static int nft_delflowtable(struct nft_ctx *ctx,
719 struct nft_flowtable *flowtable)
720 {
721 struct nft_trans *trans;
722
723 trans = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable);
724 if (IS_ERR(trans))
725 return PTR_ERR(trans);
726
727 nft_deactivate_next(ctx->net, flowtable);
728 nft_use_dec(&ctx->table->use);
729
730 return 0;
731 }
732
__nft_reg_track_clobber(struct nft_regs_track * track,u8 dreg)733 static void __nft_reg_track_clobber(struct nft_regs_track *track, u8 dreg)
734 {
735 int i;
736
737 for (i = track->regs[dreg].num_reg; i > 0; i--)
738 __nft_reg_track_cancel(track, dreg - i);
739 }
740
__nft_reg_track_update(struct nft_regs_track * track,const struct nft_expr * expr,u8 dreg,u8 num_reg)741 static void __nft_reg_track_update(struct nft_regs_track *track,
742 const struct nft_expr *expr,
743 u8 dreg, u8 num_reg)
744 {
745 track->regs[dreg].selector = expr;
746 track->regs[dreg].bitwise = NULL;
747 track->regs[dreg].num_reg = num_reg;
748 }
749
nft_reg_track_update(struct nft_regs_track * track,const struct nft_expr * expr,u8 dreg,u8 len)750 void nft_reg_track_update(struct nft_regs_track *track,
751 const struct nft_expr *expr, u8 dreg, u8 len)
752 {
753 unsigned int regcount;
754 int i;
755
756 __nft_reg_track_clobber(track, dreg);
757
758 regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE);
759 for (i = 0; i < regcount; i++, dreg++)
760 __nft_reg_track_update(track, expr, dreg, i);
761 }
762 EXPORT_SYMBOL_GPL(nft_reg_track_update);
763
nft_reg_track_cancel(struct nft_regs_track * track,u8 dreg,u8 len)764 void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len)
765 {
766 unsigned int regcount;
767 int i;
768
769 __nft_reg_track_clobber(track, dreg);
770
771 regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE);
772 for (i = 0; i < regcount; i++, dreg++)
773 __nft_reg_track_cancel(track, dreg);
774 }
775 EXPORT_SYMBOL_GPL(nft_reg_track_cancel);
776
__nft_reg_track_cancel(struct nft_regs_track * track,u8 dreg)777 void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg)
778 {
779 track->regs[dreg].selector = NULL;
780 track->regs[dreg].bitwise = NULL;
781 track->regs[dreg].num_reg = 0;
782 }
783 EXPORT_SYMBOL_GPL(__nft_reg_track_cancel);
784
785 /*
786 * Tables
787 */
788
nft_table_lookup(const struct net * net,const struct nlattr * nla,u8 family,u8 genmask,u32 nlpid)789 static struct nft_table *nft_table_lookup(const struct net *net,
790 const struct nlattr *nla,
791 u8 family, u8 genmask, u32 nlpid)
792 {
793 struct nftables_pernet *nft_net;
794 struct nft_table *table;
795
796 if (nla == NULL)
797 return ERR_PTR(-EINVAL);
798
799 nft_net = nft_pernet(net);
800 list_for_each_entry_rcu(table, &nft_net->tables, list,
801 lockdep_is_held(&nft_net->commit_mutex)) {
802 if (!nla_strcmp(nla, table->name) &&
803 table->family == family &&
804 nft_active_genmask(table, genmask)) {
805 if (nft_table_has_owner(table) &&
806 nlpid && table->nlpid != nlpid)
807 return ERR_PTR(-EPERM);
808
809 return table;
810 }
811 }
812
813 return ERR_PTR(-ENOENT);
814 }
815
nft_table_lookup_byhandle(const struct net * net,const struct nlattr * nla,int family,u8 genmask,u32 nlpid)816 static struct nft_table *nft_table_lookup_byhandle(const struct net *net,
817 const struct nlattr *nla,
818 int family, u8 genmask, u32 nlpid)
819 {
820 struct nftables_pernet *nft_net;
821 struct nft_table *table;
822
823 nft_net = nft_pernet(net);
824 list_for_each_entry(table, &nft_net->tables, list) {
825 if (be64_to_cpu(nla_get_be64(nla)) == table->handle &&
826 table->family == family &&
827 nft_active_genmask(table, genmask)) {
828 if (nft_table_has_owner(table) &&
829 nlpid && table->nlpid != nlpid)
830 return ERR_PTR(-EPERM);
831
832 return table;
833 }
834 }
835
836 return ERR_PTR(-ENOENT);
837 }
838
nf_tables_alloc_handle(struct nft_table * table)839 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
840 {
841 return ++table->hgenerator;
842 }
843
844 static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX];
845
846 static const struct nft_chain_type *
__nft_chain_type_get(u8 family,enum nft_chain_types type)847 __nft_chain_type_get(u8 family, enum nft_chain_types type)
848 {
849 if (family >= NFPROTO_NUMPROTO ||
850 type >= NFT_CHAIN_T_MAX)
851 return NULL;
852
853 return chain_type[family][type];
854 }
855
856 static const struct nft_chain_type *
__nf_tables_chain_type_lookup(const struct nlattr * nla,u8 family)857 __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family)
858 {
859 const struct nft_chain_type *type;
860 int i;
861
862 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
863 type = __nft_chain_type_get(family, i);
864 if (!type)
865 continue;
866 if (!nla_strcmp(nla, type->name))
867 return type;
868 }
869 return NULL;
870 }
871
872 struct nft_module_request {
873 struct list_head list;
874 char module[MODULE_NAME_LEN];
875 bool done;
876 };
877
878 #ifdef CONFIG_MODULES
nft_request_module(struct net * net,const char * fmt,...)879 __printf(2, 3) int nft_request_module(struct net *net, const char *fmt,
880 ...)
881 {
882 char module_name[MODULE_NAME_LEN];
883 struct nftables_pernet *nft_net;
884 struct nft_module_request *req;
885 va_list args;
886 int ret;
887
888 va_start(args, fmt);
889 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
890 va_end(args);
891 if (ret >= MODULE_NAME_LEN)
892 return 0;
893
894 nft_net = nft_pernet(net);
895 list_for_each_entry(req, &nft_net->module_list, list) {
896 if (!strcmp(req->module, module_name)) {
897 if (req->done)
898 return 0;
899
900 /* A request to load this module already exists. */
901 return -EAGAIN;
902 }
903 }
904
905 req = kmalloc(sizeof(*req), GFP_KERNEL);
906 if (!req)
907 return -ENOMEM;
908
909 req->done = false;
910 strscpy(req->module, module_name, MODULE_NAME_LEN);
911 list_add_tail(&req->list, &nft_net->module_list);
912
913 return -EAGAIN;
914 }
915 EXPORT_SYMBOL_GPL(nft_request_module);
916 #endif
917
lockdep_nfnl_nft_mutex_not_held(void)918 static void lockdep_nfnl_nft_mutex_not_held(void)
919 {
920 #ifdef CONFIG_PROVE_LOCKING
921 if (debug_locks)
922 WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES));
923 #endif
924 }
925
926 static const struct nft_chain_type *
nf_tables_chain_type_lookup(struct net * net,const struct nlattr * nla,u8 family,bool autoload)927 nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla,
928 u8 family, bool autoload)
929 {
930 const struct nft_chain_type *type;
931
932 type = __nf_tables_chain_type_lookup(nla, family);
933 if (type != NULL)
934 return type;
935
936 lockdep_nfnl_nft_mutex_not_held();
937 #ifdef CONFIG_MODULES
938 if (autoload) {
939 if (nft_request_module(net, "nft-chain-%u-%.*s", family,
940 nla_len(nla),
941 (const char *)nla_data(nla)) == -EAGAIN)
942 return ERR_PTR(-EAGAIN);
943 }
944 #endif
945 return ERR_PTR(-ENOENT);
946 }
947
nft_base_seq(const struct net * net)948 static __be16 nft_base_seq(const struct net *net)
949 {
950 struct nftables_pernet *nft_net = nft_pernet(net);
951
952 return htons(nft_net->base_seq & 0xffff);
953 }
954
955 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
956 [NFTA_TABLE_NAME] = { .type = NLA_STRING,
957 .len = NFT_TABLE_MAXNAMELEN - 1 },
958 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
959 [NFTA_TABLE_HANDLE] = { .type = NLA_U64 },
960 [NFTA_TABLE_USERDATA] = { .type = NLA_BINARY,
961 .len = NFT_USERDATA_MAXLEN }
962 };
963
nf_tables_fill_table_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq,int event,u32 flags,int family,const struct nft_table * table)964 static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
965 u32 portid, u32 seq, int event, u32 flags,
966 int family, const struct nft_table *table)
967 {
968 struct nlmsghdr *nlh;
969
970 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
971 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
972 NFNETLINK_V0, nft_base_seq(net));
973 if (!nlh)
974 goto nla_put_failure;
975
976 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
977 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) ||
978 nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle),
979 NFTA_TABLE_PAD))
980 goto nla_put_failure;
981
982 if (event == NFT_MSG_DELTABLE) {
983 nlmsg_end(skb, nlh);
984 return 0;
985 }
986
987 if (nla_put_be32(skb, NFTA_TABLE_FLAGS,
988 htonl(table->flags & NFT_TABLE_F_MASK)))
989 goto nla_put_failure;
990
991 if (nft_table_has_owner(table) &&
992 nla_put_be32(skb, NFTA_TABLE_OWNER, htonl(table->nlpid)))
993 goto nla_put_failure;
994
995 if (table->udata) {
996 if (nla_put(skb, NFTA_TABLE_USERDATA, table->udlen, table->udata))
997 goto nla_put_failure;
998 }
999
1000 nlmsg_end(skb, nlh);
1001 return 0;
1002
1003 nla_put_failure:
1004 nlmsg_trim(skb, nlh);
1005 return -1;
1006 }
1007
1008 struct nftnl_skb_parms {
1009 bool report;
1010 };
1011 #define NFT_CB(skb) (*(struct nftnl_skb_parms*)&((skb)->cb))
1012
nft_notify_enqueue(struct sk_buff * skb,bool report,struct list_head * notify_list)1013 static void nft_notify_enqueue(struct sk_buff *skb, bool report,
1014 struct list_head *notify_list)
1015 {
1016 NFT_CB(skb).report = report;
1017 list_add_tail(&skb->list, notify_list);
1018 }
1019
nf_tables_table_notify(const struct nft_ctx * ctx,int event)1020 static void nf_tables_table_notify(const struct nft_ctx *ctx, int event)
1021 {
1022 struct nftables_pernet *nft_net;
1023 struct sk_buff *skb;
1024 u16 flags = 0;
1025 int err;
1026
1027 if (!ctx->report &&
1028 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1029 return;
1030
1031 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1032 if (skb == NULL)
1033 goto err;
1034
1035 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
1036 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
1037
1038 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
1039 event, flags, ctx->family, ctx->table);
1040 if (err < 0) {
1041 kfree_skb(skb);
1042 goto err;
1043 }
1044
1045 nft_net = nft_pernet(ctx->net);
1046 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
1047 return;
1048 err:
1049 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
1050 }
1051
nf_tables_dump_tables(struct sk_buff * skb,struct netlink_callback * cb)1052 static int nf_tables_dump_tables(struct sk_buff *skb,
1053 struct netlink_callback *cb)
1054 {
1055 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1056 struct nftables_pernet *nft_net;
1057 const struct nft_table *table;
1058 unsigned int idx = 0, s_idx = cb->args[0];
1059 struct net *net = sock_net(skb->sk);
1060 int family = nfmsg->nfgen_family;
1061
1062 rcu_read_lock();
1063 nft_net = nft_pernet(net);
1064 cb->seq = READ_ONCE(nft_net->base_seq);
1065
1066 list_for_each_entry_rcu(table, &nft_net->tables, list) {
1067 if (family != NFPROTO_UNSPEC && family != table->family)
1068 continue;
1069
1070 if (idx < s_idx)
1071 goto cont;
1072 if (idx > s_idx)
1073 memset(&cb->args[1], 0,
1074 sizeof(cb->args) - sizeof(cb->args[0]));
1075 if (!nft_is_active(net, table))
1076 continue;
1077 if (nf_tables_fill_table_info(skb, net,
1078 NETLINK_CB(cb->skb).portid,
1079 cb->nlh->nlmsg_seq,
1080 NFT_MSG_NEWTABLE, NLM_F_MULTI,
1081 table->family, table) < 0)
1082 goto done;
1083
1084 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1085 cont:
1086 idx++;
1087 }
1088 done:
1089 rcu_read_unlock();
1090 cb->args[0] = idx;
1091 return skb->len;
1092 }
1093
nft_netlink_dump_start_rcu(struct sock * nlsk,struct sk_buff * skb,const struct nlmsghdr * nlh,struct netlink_dump_control * c)1094 static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb,
1095 const struct nlmsghdr *nlh,
1096 struct netlink_dump_control *c)
1097 {
1098 int err;
1099
1100 if (!try_module_get(THIS_MODULE))
1101 return -EINVAL;
1102
1103 rcu_read_unlock();
1104 err = netlink_dump_start(nlsk, skb, nlh, c);
1105 rcu_read_lock();
1106 module_put(THIS_MODULE);
1107
1108 return err;
1109 }
1110
1111 /* called with rcu_read_lock held */
nf_tables_gettable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])1112 static int nf_tables_gettable(struct sk_buff *skb, const struct nfnl_info *info,
1113 const struct nlattr * const nla[])
1114 {
1115 struct netlink_ext_ack *extack = info->extack;
1116 u8 genmask = nft_genmask_cur(info->net);
1117 u8 family = info->nfmsg->nfgen_family;
1118 const struct nft_table *table;
1119 struct net *net = info->net;
1120 struct sk_buff *skb2;
1121 int err;
1122
1123 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
1124 struct netlink_dump_control c = {
1125 .dump = nf_tables_dump_tables,
1126 .module = THIS_MODULE,
1127 };
1128
1129 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
1130 }
1131
1132 table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask, 0);
1133 if (IS_ERR(table)) {
1134 NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]);
1135 return PTR_ERR(table);
1136 }
1137
1138 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1139 if (!skb2)
1140 return -ENOMEM;
1141
1142 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
1143 info->nlh->nlmsg_seq, NFT_MSG_NEWTABLE,
1144 0, family, table);
1145 if (err < 0)
1146 goto err_fill_table_info;
1147
1148 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
1149
1150 err_fill_table_info:
1151 kfree_skb(skb2);
1152 return err;
1153 }
1154
nft_table_disable(struct net * net,struct nft_table * table,u32 cnt)1155 static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt)
1156 {
1157 struct nft_chain *chain;
1158 u32 i = 0;
1159
1160 list_for_each_entry(chain, &table->chains, list) {
1161 if (!nft_is_active_next(net, chain))
1162 continue;
1163 if (!nft_is_base_chain(chain))
1164 continue;
1165
1166 if (cnt && i++ == cnt)
1167 break;
1168
1169 nf_tables_unregister_hook(net, table, chain);
1170 }
1171 }
1172
nf_tables_table_enable(struct net * net,struct nft_table * table)1173 static int nf_tables_table_enable(struct net *net, struct nft_table *table)
1174 {
1175 struct nft_chain *chain;
1176 int err, i = 0;
1177
1178 list_for_each_entry(chain, &table->chains, list) {
1179 if (!nft_is_active_next(net, chain))
1180 continue;
1181 if (!nft_is_base_chain(chain))
1182 continue;
1183
1184 err = nf_tables_register_hook(net, table, chain);
1185 if (err < 0)
1186 goto err_register_hooks;
1187
1188 i++;
1189 }
1190 return 0;
1191
1192 err_register_hooks:
1193 if (i)
1194 nft_table_disable(net, table, i);
1195 return err;
1196 }
1197
nf_tables_table_disable(struct net * net,struct nft_table * table)1198 static void nf_tables_table_disable(struct net *net, struct nft_table *table)
1199 {
1200 table->flags &= ~NFT_TABLE_F_DORMANT;
1201 nft_table_disable(net, table, 0);
1202 table->flags |= NFT_TABLE_F_DORMANT;
1203 }
1204
1205 #define __NFT_TABLE_F_INTERNAL (NFT_TABLE_F_MASK + 1)
1206 #define __NFT_TABLE_F_WAS_DORMANT (__NFT_TABLE_F_INTERNAL << 0)
1207 #define __NFT_TABLE_F_WAS_AWAKEN (__NFT_TABLE_F_INTERNAL << 1)
1208 #define __NFT_TABLE_F_UPDATE (__NFT_TABLE_F_WAS_DORMANT | \
1209 __NFT_TABLE_F_WAS_AWAKEN)
1210
nft_table_pending_update(const struct nft_ctx * ctx)1211 static bool nft_table_pending_update(const struct nft_ctx *ctx)
1212 {
1213 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
1214 struct nft_trans *trans;
1215
1216 if (ctx->table->flags & __NFT_TABLE_F_UPDATE)
1217 return true;
1218
1219 list_for_each_entry(trans, &nft_net->commit_list, list) {
1220 if (trans->ctx.table == ctx->table &&
1221 ((trans->msg_type == NFT_MSG_NEWCHAIN &&
1222 nft_trans_chain_update(trans)) ||
1223 (trans->msg_type == NFT_MSG_DELCHAIN &&
1224 nft_is_base_chain(trans->ctx.chain))))
1225 return true;
1226 }
1227
1228 return false;
1229 }
1230
nf_tables_updtable(struct nft_ctx * ctx)1231 static int nf_tables_updtable(struct nft_ctx *ctx)
1232 {
1233 struct nft_trans *trans;
1234 u32 flags;
1235 int ret;
1236
1237 if (!ctx->nla[NFTA_TABLE_FLAGS])
1238 return 0;
1239
1240 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
1241 if (flags & ~NFT_TABLE_F_MASK)
1242 return -EOPNOTSUPP;
1243
1244 if (flags == (ctx->table->flags & NFT_TABLE_F_MASK))
1245 return 0;
1246
1247 if ((nft_table_has_owner(ctx->table) &&
1248 !(flags & NFT_TABLE_F_OWNER)) ||
1249 (!nft_table_has_owner(ctx->table) &&
1250 flags & NFT_TABLE_F_OWNER))
1251 return -EOPNOTSUPP;
1252
1253 /* No dormant off/on/off/on games in single transaction */
1254 if (nft_table_pending_update(ctx))
1255 return -EINVAL;
1256
1257 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
1258 sizeof(struct nft_trans_table));
1259 if (trans == NULL)
1260 return -ENOMEM;
1261
1262 if ((flags & NFT_TABLE_F_DORMANT) &&
1263 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
1264 ctx->table->flags |= NFT_TABLE_F_DORMANT;
1265 if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE))
1266 ctx->table->flags |= __NFT_TABLE_F_WAS_AWAKEN;
1267 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
1268 ctx->table->flags & NFT_TABLE_F_DORMANT) {
1269 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
1270 if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) {
1271 ret = nf_tables_table_enable(ctx->net, ctx->table);
1272 if (ret < 0)
1273 goto err_register_hooks;
1274
1275 ctx->table->flags |= __NFT_TABLE_F_WAS_DORMANT;
1276 }
1277 }
1278
1279 nft_trans_table_update(trans) = true;
1280 nft_trans_commit_list_add_tail(ctx->net, trans);
1281
1282 return 0;
1283
1284 err_register_hooks:
1285 ctx->table->flags |= NFT_TABLE_F_DORMANT;
1286 nft_trans_destroy(trans);
1287 return ret;
1288 }
1289
nft_chain_hash(const void * data,u32 len,u32 seed)1290 static u32 nft_chain_hash(const void *data, u32 len, u32 seed)
1291 {
1292 const char *name = data;
1293
1294 return jhash(name, strlen(name), seed);
1295 }
1296
nft_chain_hash_obj(const void * data,u32 len,u32 seed)1297 static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed)
1298 {
1299 const struct nft_chain *chain = data;
1300
1301 return nft_chain_hash(chain->name, 0, seed);
1302 }
1303
nft_chain_hash_cmp(struct rhashtable_compare_arg * arg,const void * ptr)1304 static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg,
1305 const void *ptr)
1306 {
1307 const struct nft_chain *chain = ptr;
1308 const char *name = arg->key;
1309
1310 return strcmp(chain->name, name);
1311 }
1312
nft_objname_hash(const void * data,u32 len,u32 seed)1313 static u32 nft_objname_hash(const void *data, u32 len, u32 seed)
1314 {
1315 const struct nft_object_hash_key *k = data;
1316
1317 seed ^= hash_ptr(k->table, 32);
1318
1319 return jhash(k->name, strlen(k->name), seed);
1320 }
1321
nft_objname_hash_obj(const void * data,u32 len,u32 seed)1322 static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed)
1323 {
1324 const struct nft_object *obj = data;
1325
1326 return nft_objname_hash(&obj->key, 0, seed);
1327 }
1328
nft_objname_hash_cmp(struct rhashtable_compare_arg * arg,const void * ptr)1329 static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg,
1330 const void *ptr)
1331 {
1332 const struct nft_object_hash_key *k = arg->key;
1333 const struct nft_object *obj = ptr;
1334
1335 if (obj->key.table != k->table)
1336 return -1;
1337
1338 return strcmp(obj->key.name, k->name);
1339 }
1340
nft_supported_family(u8 family)1341 static bool nft_supported_family(u8 family)
1342 {
1343 return false
1344 #ifdef CONFIG_NF_TABLES_INET
1345 || family == NFPROTO_INET
1346 #endif
1347 #ifdef CONFIG_NF_TABLES_IPV4
1348 || family == NFPROTO_IPV4
1349 #endif
1350 #ifdef CONFIG_NF_TABLES_ARP
1351 || family == NFPROTO_ARP
1352 #endif
1353 #ifdef CONFIG_NF_TABLES_NETDEV
1354 || family == NFPROTO_NETDEV
1355 #endif
1356 #if IS_ENABLED(CONFIG_NF_TABLES_BRIDGE)
1357 || family == NFPROTO_BRIDGE
1358 #endif
1359 #ifdef CONFIG_NF_TABLES_IPV6
1360 || family == NFPROTO_IPV6
1361 #endif
1362 ;
1363 }
1364
nf_tables_newtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])1365 static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info,
1366 const struct nlattr * const nla[])
1367 {
1368 struct nftables_pernet *nft_net = nft_pernet(info->net);
1369 struct netlink_ext_ack *extack = info->extack;
1370 u8 genmask = nft_genmask_next(info->net);
1371 u8 family = info->nfmsg->nfgen_family;
1372 struct net *net = info->net;
1373 const struct nlattr *attr;
1374 struct nft_table *table;
1375 struct nft_ctx ctx;
1376 u32 flags = 0;
1377 int err;
1378
1379 if (!nft_supported_family(family))
1380 return -EOPNOTSUPP;
1381
1382 lockdep_assert_held(&nft_net->commit_mutex);
1383 attr = nla[NFTA_TABLE_NAME];
1384 table = nft_table_lookup(net, attr, family, genmask,
1385 NETLINK_CB(skb).portid);
1386 if (IS_ERR(table)) {
1387 if (PTR_ERR(table) != -ENOENT)
1388 return PTR_ERR(table);
1389 } else {
1390 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
1391 NL_SET_BAD_ATTR(extack, attr);
1392 return -EEXIST;
1393 }
1394 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1395 return -EOPNOTSUPP;
1396
1397 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
1398
1399 return nf_tables_updtable(&ctx);
1400 }
1401
1402 if (nla[NFTA_TABLE_FLAGS]) {
1403 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
1404 if (flags & ~NFT_TABLE_F_MASK)
1405 return -EOPNOTSUPP;
1406 }
1407
1408 err = -ENOMEM;
1409 table = kzalloc(sizeof(*table), GFP_KERNEL_ACCOUNT);
1410 if (table == NULL)
1411 goto err_kzalloc;
1412
1413 table->validate_state = nft_net->validate_state;
1414 table->name = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
1415 if (table->name == NULL)
1416 goto err_strdup;
1417
1418 if (nla[NFTA_TABLE_USERDATA]) {
1419 table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL_ACCOUNT);
1420 if (table->udata == NULL)
1421 goto err_table_udata;
1422
1423 table->udlen = nla_len(nla[NFTA_TABLE_USERDATA]);
1424 }
1425
1426 err = rhltable_init(&table->chains_ht, &nft_chain_ht_params);
1427 if (err)
1428 goto err_chain_ht;
1429
1430 INIT_LIST_HEAD(&table->chains);
1431 INIT_LIST_HEAD(&table->sets);
1432 INIT_LIST_HEAD(&table->objects);
1433 INIT_LIST_HEAD(&table->flowtables);
1434 table->family = family;
1435 table->flags = flags;
1436 table->handle = ++nft_net->table_handle;
1437 if (table->flags & NFT_TABLE_F_OWNER)
1438 table->nlpid = NETLINK_CB(skb).portid;
1439
1440 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
1441 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
1442 if (err < 0)
1443 goto err_trans;
1444
1445 list_add_tail_rcu(&table->list, &nft_net->tables);
1446 return 0;
1447 err_trans:
1448 rhltable_destroy(&table->chains_ht);
1449 err_chain_ht:
1450 kfree(table->udata);
1451 err_table_udata:
1452 kfree(table->name);
1453 err_strdup:
1454 kfree(table);
1455 err_kzalloc:
1456 return err;
1457 }
1458
nft_flush_table(struct nft_ctx * ctx)1459 static int nft_flush_table(struct nft_ctx *ctx)
1460 {
1461 struct nft_flowtable *flowtable, *nft;
1462 struct nft_chain *chain, *nc;
1463 struct nft_object *obj, *ne;
1464 struct nft_set *set, *ns;
1465 int err;
1466
1467 list_for_each_entry(chain, &ctx->table->chains, list) {
1468 if (!nft_is_active_next(ctx->net, chain))
1469 continue;
1470
1471 if (nft_chain_binding(chain))
1472 continue;
1473
1474 ctx->chain = chain;
1475
1476 err = nft_delrule_by_chain(ctx);
1477 if (err < 0)
1478 goto out;
1479 }
1480
1481 list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
1482 if (!nft_is_active_next(ctx->net, set))
1483 continue;
1484
1485 if (nft_set_is_anonymous(set))
1486 continue;
1487
1488 err = nft_delset(ctx, set);
1489 if (err < 0)
1490 goto out;
1491 }
1492
1493 list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) {
1494 if (!nft_is_active_next(ctx->net, flowtable))
1495 continue;
1496
1497 err = nft_delflowtable(ctx, flowtable);
1498 if (err < 0)
1499 goto out;
1500 }
1501
1502 list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) {
1503 if (!nft_is_active_next(ctx->net, obj))
1504 continue;
1505
1506 err = nft_delobj(ctx, obj);
1507 if (err < 0)
1508 goto out;
1509 }
1510
1511 list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
1512 if (!nft_is_active_next(ctx->net, chain))
1513 continue;
1514
1515 if (nft_chain_binding(chain))
1516 continue;
1517
1518 ctx->chain = chain;
1519
1520 err = nft_delchain(ctx);
1521 if (err < 0)
1522 goto out;
1523 }
1524
1525 err = nft_deltable(ctx);
1526 out:
1527 return err;
1528 }
1529
nft_flush(struct nft_ctx * ctx,int family)1530 static int nft_flush(struct nft_ctx *ctx, int family)
1531 {
1532 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
1533 const struct nlattr * const *nla = ctx->nla;
1534 struct nft_table *table, *nt;
1535 int err = 0;
1536
1537 list_for_each_entry_safe(table, nt, &nft_net->tables, list) {
1538 if (family != AF_UNSPEC && table->family != family)
1539 continue;
1540
1541 ctx->family = table->family;
1542
1543 if (!nft_is_active_next(ctx->net, table))
1544 continue;
1545
1546 if (nft_table_has_owner(table) && table->nlpid != ctx->portid)
1547 continue;
1548
1549 if (nla[NFTA_TABLE_NAME] &&
1550 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
1551 continue;
1552
1553 ctx->table = table;
1554
1555 err = nft_flush_table(ctx);
1556 if (err < 0)
1557 goto out;
1558 }
1559 out:
1560 return err;
1561 }
1562
nf_tables_deltable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])1563 static int nf_tables_deltable(struct sk_buff *skb, const struct nfnl_info *info,
1564 const struct nlattr * const nla[])
1565 {
1566 struct netlink_ext_ack *extack = info->extack;
1567 u8 genmask = nft_genmask_next(info->net);
1568 u8 family = info->nfmsg->nfgen_family;
1569 struct net *net = info->net;
1570 const struct nlattr *attr;
1571 struct nft_table *table;
1572 struct nft_ctx ctx;
1573
1574 nft_ctx_init(&ctx, net, skb, info->nlh, 0, NULL, NULL, nla);
1575 if (family == AF_UNSPEC ||
1576 (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE]))
1577 return nft_flush(&ctx, family);
1578
1579 if (nla[NFTA_TABLE_HANDLE]) {
1580 attr = nla[NFTA_TABLE_HANDLE];
1581 table = nft_table_lookup_byhandle(net, attr, family, genmask,
1582 NETLINK_CB(skb).portid);
1583 } else {
1584 attr = nla[NFTA_TABLE_NAME];
1585 table = nft_table_lookup(net, attr, family, genmask,
1586 NETLINK_CB(skb).portid);
1587 }
1588
1589 if (IS_ERR(table)) {
1590 if (PTR_ERR(table) == -ENOENT &&
1591 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYTABLE)
1592 return 0;
1593
1594 NL_SET_BAD_ATTR(extack, attr);
1595 return PTR_ERR(table);
1596 }
1597
1598 if (info->nlh->nlmsg_flags & NLM_F_NONREC &&
1599 table->use > 0)
1600 return -EBUSY;
1601
1602 ctx.family = family;
1603 ctx.table = table;
1604
1605 return nft_flush_table(&ctx);
1606 }
1607
nf_tables_table_destroy(struct nft_ctx * ctx)1608 static void nf_tables_table_destroy(struct nft_ctx *ctx)
1609 {
1610 if (WARN_ON(ctx->table->use > 0))
1611 return;
1612
1613 rhltable_destroy(&ctx->table->chains_ht);
1614 kfree(ctx->table->name);
1615 kfree(ctx->table->udata);
1616 kfree(ctx->table);
1617 }
1618
nft_register_chain_type(const struct nft_chain_type * ctype)1619 void nft_register_chain_type(const struct nft_chain_type *ctype)
1620 {
1621 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1622 if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) {
1623 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1624 return;
1625 }
1626 chain_type[ctype->family][ctype->type] = ctype;
1627 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1628 }
1629 EXPORT_SYMBOL_GPL(nft_register_chain_type);
1630
nft_unregister_chain_type(const struct nft_chain_type * ctype)1631 void nft_unregister_chain_type(const struct nft_chain_type *ctype)
1632 {
1633 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1634 chain_type[ctype->family][ctype->type] = NULL;
1635 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1636 }
1637 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
1638
1639 /*
1640 * Chains
1641 */
1642
1643 static struct nft_chain *
nft_chain_lookup_byhandle(const struct nft_table * table,u64 handle,u8 genmask)1644 nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask)
1645 {
1646 struct nft_chain *chain;
1647
1648 list_for_each_entry(chain, &table->chains, list) {
1649 if (chain->handle == handle &&
1650 nft_active_genmask(chain, genmask))
1651 return chain;
1652 }
1653
1654 return ERR_PTR(-ENOENT);
1655 }
1656
lockdep_commit_lock_is_held(const struct net * net)1657 static bool lockdep_commit_lock_is_held(const struct net *net)
1658 {
1659 #ifdef CONFIG_PROVE_LOCKING
1660 struct nftables_pernet *nft_net = nft_pernet(net);
1661
1662 return lockdep_is_held(&nft_net->commit_mutex);
1663 #else
1664 return true;
1665 #endif
1666 }
1667
nft_chain_lookup(struct net * net,struct nft_table * table,const struct nlattr * nla,u8 genmask)1668 static struct nft_chain *nft_chain_lookup(struct net *net,
1669 struct nft_table *table,
1670 const struct nlattr *nla, u8 genmask)
1671 {
1672 char search[NFT_CHAIN_MAXNAMELEN + 1];
1673 struct rhlist_head *tmp, *list;
1674 struct nft_chain *chain;
1675
1676 if (nla == NULL)
1677 return ERR_PTR(-EINVAL);
1678
1679 nla_strscpy(search, nla, sizeof(search));
1680
1681 WARN_ON(!rcu_read_lock_held() &&
1682 !lockdep_commit_lock_is_held(net));
1683
1684 chain = ERR_PTR(-ENOENT);
1685 rcu_read_lock();
1686 list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params);
1687 if (!list)
1688 goto out_unlock;
1689
1690 rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
1691 if (nft_active_genmask(chain, genmask))
1692 goto out_unlock;
1693 }
1694 chain = ERR_PTR(-ENOENT);
1695 out_unlock:
1696 rcu_read_unlock();
1697 return chain;
1698 }
1699
1700 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
1701 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING,
1702 .len = NFT_TABLE_MAXNAMELEN - 1 },
1703 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
1704 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
1705 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1706 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
1707 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
1708 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING,
1709 .len = NFT_MODULE_AUTOLOAD_LIMIT },
1710 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
1711 [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 },
1712 [NFTA_CHAIN_ID] = { .type = NLA_U32 },
1713 [NFTA_CHAIN_USERDATA] = { .type = NLA_BINARY,
1714 .len = NFT_USERDATA_MAXLEN },
1715 };
1716
1717 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
1718 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
1719 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
1720 [NFTA_HOOK_DEV] = { .type = NLA_STRING,
1721 .len = IFNAMSIZ - 1 },
1722 };
1723
nft_dump_stats(struct sk_buff * skb,struct nft_stats __percpu * stats)1724 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
1725 {
1726 struct nft_stats *cpu_stats, total;
1727 struct nlattr *nest;
1728 unsigned int seq;
1729 u64 pkts, bytes;
1730 int cpu;
1731
1732 if (!stats)
1733 return 0;
1734
1735 memset(&total, 0, sizeof(total));
1736 for_each_possible_cpu(cpu) {
1737 cpu_stats = per_cpu_ptr(stats, cpu);
1738 do {
1739 seq = u64_stats_fetch_begin(&cpu_stats->syncp);
1740 pkts = cpu_stats->pkts;
1741 bytes = cpu_stats->bytes;
1742 } while (u64_stats_fetch_retry(&cpu_stats->syncp, seq));
1743 total.pkts += pkts;
1744 total.bytes += bytes;
1745 }
1746 nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS);
1747 if (nest == NULL)
1748 goto nla_put_failure;
1749
1750 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts),
1751 NFTA_COUNTER_PAD) ||
1752 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
1753 NFTA_COUNTER_PAD))
1754 goto nla_put_failure;
1755
1756 nla_nest_end(skb, nest);
1757 return 0;
1758
1759 nla_put_failure:
1760 return -ENOSPC;
1761 }
1762
nft_dump_basechain_hook(struct sk_buff * skb,int family,const struct nft_base_chain * basechain,const struct list_head * hook_list)1763 static int nft_dump_basechain_hook(struct sk_buff *skb, int family,
1764 const struct nft_base_chain *basechain,
1765 const struct list_head *hook_list)
1766 {
1767 const struct nf_hook_ops *ops = &basechain->ops;
1768 struct nft_hook *hook, *first = NULL;
1769 struct nlattr *nest, *nest_devs;
1770 int n = 0;
1771
1772 nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK);
1773 if (nest == NULL)
1774 goto nla_put_failure;
1775 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
1776 goto nla_put_failure;
1777 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
1778 goto nla_put_failure;
1779
1780 if (nft_base_chain_netdev(family, ops->hooknum)) {
1781 nest_devs = nla_nest_start_noflag(skb, NFTA_HOOK_DEVS);
1782 if (!nest_devs)
1783 goto nla_put_failure;
1784
1785 if (!hook_list)
1786 hook_list = &basechain->hook_list;
1787
1788 list_for_each_entry_rcu(hook, hook_list, list) {
1789 if (!first)
1790 first = hook;
1791
1792 if (nla_put_string(skb, NFTA_DEVICE_NAME,
1793 hook->ops.dev->name))
1794 goto nla_put_failure;
1795 n++;
1796 }
1797 nla_nest_end(skb, nest_devs);
1798
1799 if (n == 1 &&
1800 nla_put_string(skb, NFTA_HOOK_DEV, first->ops.dev->name))
1801 goto nla_put_failure;
1802 }
1803 nla_nest_end(skb, nest);
1804
1805 return 0;
1806 nla_put_failure:
1807 return -1;
1808 }
1809
nf_tables_fill_chain_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq,int event,u32 flags,int family,const struct nft_table * table,const struct nft_chain * chain,const struct list_head * hook_list)1810 static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
1811 u32 portid, u32 seq, int event, u32 flags,
1812 int family, const struct nft_table *table,
1813 const struct nft_chain *chain,
1814 const struct list_head *hook_list)
1815 {
1816 struct nlmsghdr *nlh;
1817
1818 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
1819 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
1820 NFNETLINK_V0, nft_base_seq(net));
1821 if (!nlh)
1822 goto nla_put_failure;
1823
1824 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name) ||
1825 nla_put_string(skb, NFTA_CHAIN_NAME, chain->name) ||
1826 nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle),
1827 NFTA_CHAIN_PAD))
1828 goto nla_put_failure;
1829
1830 if (event == NFT_MSG_DELCHAIN && !hook_list) {
1831 nlmsg_end(skb, nlh);
1832 return 0;
1833 }
1834
1835 if (nft_is_base_chain(chain)) {
1836 const struct nft_base_chain *basechain = nft_base_chain(chain);
1837 struct nft_stats __percpu *stats;
1838
1839 if (nft_dump_basechain_hook(skb, family, basechain, hook_list))
1840 goto nla_put_failure;
1841
1842 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
1843 htonl(basechain->policy)))
1844 goto nla_put_failure;
1845
1846 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
1847 goto nla_put_failure;
1848
1849 stats = rcu_dereference_check(basechain->stats,
1850 lockdep_commit_lock_is_held(net));
1851 if (nft_dump_stats(skb, stats))
1852 goto nla_put_failure;
1853 }
1854
1855 if (chain->flags &&
1856 nla_put_be32(skb, NFTA_CHAIN_FLAGS, htonl(chain->flags)))
1857 goto nla_put_failure;
1858
1859 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
1860 goto nla_put_failure;
1861
1862 if (chain->udata &&
1863 nla_put(skb, NFTA_CHAIN_USERDATA, chain->udlen, chain->udata))
1864 goto nla_put_failure;
1865
1866 nlmsg_end(skb, nlh);
1867 return 0;
1868
1869 nla_put_failure:
1870 nlmsg_trim(skb, nlh);
1871 return -1;
1872 }
1873
nf_tables_chain_notify(const struct nft_ctx * ctx,int event,const struct list_head * hook_list)1874 static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event,
1875 const struct list_head *hook_list)
1876 {
1877 struct nftables_pernet *nft_net;
1878 struct sk_buff *skb;
1879 u16 flags = 0;
1880 int err;
1881
1882 if (!ctx->report &&
1883 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1884 return;
1885
1886 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1887 if (skb == NULL)
1888 goto err;
1889
1890 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
1891 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
1892
1893 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
1894 event, flags, ctx->family, ctx->table,
1895 ctx->chain, hook_list);
1896 if (err < 0) {
1897 kfree_skb(skb);
1898 goto err;
1899 }
1900
1901 nft_net = nft_pernet(ctx->net);
1902 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
1903 return;
1904 err:
1905 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
1906 }
1907
nf_tables_dump_chains(struct sk_buff * skb,struct netlink_callback * cb)1908 static int nf_tables_dump_chains(struct sk_buff *skb,
1909 struct netlink_callback *cb)
1910 {
1911 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1912 unsigned int idx = 0, s_idx = cb->args[0];
1913 struct net *net = sock_net(skb->sk);
1914 int family = nfmsg->nfgen_family;
1915 struct nftables_pernet *nft_net;
1916 const struct nft_table *table;
1917 const struct nft_chain *chain;
1918
1919 rcu_read_lock();
1920 nft_net = nft_pernet(net);
1921 cb->seq = READ_ONCE(nft_net->base_seq);
1922
1923 list_for_each_entry_rcu(table, &nft_net->tables, list) {
1924 if (family != NFPROTO_UNSPEC && family != table->family)
1925 continue;
1926
1927 list_for_each_entry_rcu(chain, &table->chains, list) {
1928 if (idx < s_idx)
1929 goto cont;
1930 if (idx > s_idx)
1931 memset(&cb->args[1], 0,
1932 sizeof(cb->args) - sizeof(cb->args[0]));
1933 if (!nft_is_active(net, chain))
1934 continue;
1935 if (nf_tables_fill_chain_info(skb, net,
1936 NETLINK_CB(cb->skb).portid,
1937 cb->nlh->nlmsg_seq,
1938 NFT_MSG_NEWCHAIN,
1939 NLM_F_MULTI,
1940 table->family, table,
1941 chain, NULL) < 0)
1942 goto done;
1943
1944 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1945 cont:
1946 idx++;
1947 }
1948 }
1949 done:
1950 rcu_read_unlock();
1951 cb->args[0] = idx;
1952 return skb->len;
1953 }
1954
1955 /* called with rcu_read_lock held */
nf_tables_getchain(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])1956 static int nf_tables_getchain(struct sk_buff *skb, const struct nfnl_info *info,
1957 const struct nlattr * const nla[])
1958 {
1959 struct netlink_ext_ack *extack = info->extack;
1960 u8 genmask = nft_genmask_cur(info->net);
1961 u8 family = info->nfmsg->nfgen_family;
1962 const struct nft_chain *chain;
1963 struct net *net = info->net;
1964 struct nft_table *table;
1965 struct sk_buff *skb2;
1966 int err;
1967
1968 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
1969 struct netlink_dump_control c = {
1970 .dump = nf_tables_dump_chains,
1971 .module = THIS_MODULE,
1972 };
1973
1974 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
1975 }
1976
1977 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, 0);
1978 if (IS_ERR(table)) {
1979 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
1980 return PTR_ERR(table);
1981 }
1982
1983 chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask);
1984 if (IS_ERR(chain)) {
1985 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
1986 return PTR_ERR(chain);
1987 }
1988
1989 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1990 if (!skb2)
1991 return -ENOMEM;
1992
1993 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
1994 info->nlh->nlmsg_seq, NFT_MSG_NEWCHAIN,
1995 0, family, table, chain, NULL);
1996 if (err < 0)
1997 goto err_fill_chain_info;
1998
1999 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
2000
2001 err_fill_chain_info:
2002 kfree_skb(skb2);
2003 return err;
2004 }
2005
2006 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
2007 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
2008 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
2009 };
2010
nft_stats_alloc(const struct nlattr * attr)2011 static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
2012 {
2013 struct nlattr *tb[NFTA_COUNTER_MAX+1];
2014 struct nft_stats __percpu *newstats;
2015 struct nft_stats *stats;
2016 int err;
2017
2018 err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr,
2019 nft_counter_policy, NULL);
2020 if (err < 0)
2021 return ERR_PTR(err);
2022
2023 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
2024 return ERR_PTR(-EINVAL);
2025
2026 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
2027 if (newstats == NULL)
2028 return ERR_PTR(-ENOMEM);
2029
2030 /* Restore old counters on this cpu, no problem. Per-cpu statistics
2031 * are not exposed to userspace.
2032 */
2033 preempt_disable();
2034 stats = this_cpu_ptr(newstats);
2035 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
2036 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
2037 preempt_enable();
2038
2039 return newstats;
2040 }
2041
nft_chain_stats_replace(struct nft_trans * trans)2042 static void nft_chain_stats_replace(struct nft_trans *trans)
2043 {
2044 struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain);
2045
2046 if (!nft_trans_chain_stats(trans))
2047 return;
2048
2049 nft_trans_chain_stats(trans) =
2050 rcu_replace_pointer(chain->stats, nft_trans_chain_stats(trans),
2051 lockdep_commit_lock_is_held(trans->ctx.net));
2052
2053 if (!nft_trans_chain_stats(trans))
2054 static_branch_inc(&nft_counters_enabled);
2055 }
2056
nf_tables_chain_free_chain_rules(struct nft_chain * chain)2057 static void nf_tables_chain_free_chain_rules(struct nft_chain *chain)
2058 {
2059 struct nft_rule_blob *g0 = rcu_dereference_raw(chain->blob_gen_0);
2060 struct nft_rule_blob *g1 = rcu_dereference_raw(chain->blob_gen_1);
2061
2062 if (g0 != g1)
2063 kvfree(g1);
2064 kvfree(g0);
2065
2066 /* should be NULL either via abort or via successful commit */
2067 WARN_ON_ONCE(chain->blob_next);
2068 kvfree(chain->blob_next);
2069 }
2070
nf_tables_chain_destroy(struct nft_chain * chain)2071 void nf_tables_chain_destroy(struct nft_chain *chain)
2072 {
2073 const struct nft_table *table = chain->table;
2074 struct nft_hook *hook, *next;
2075
2076 if (WARN_ON(chain->use > 0))
2077 return;
2078
2079 /* no concurrent access possible anymore */
2080 nf_tables_chain_free_chain_rules(chain);
2081
2082 if (nft_is_base_chain(chain)) {
2083 struct nft_base_chain *basechain = nft_base_chain(chain);
2084
2085 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) {
2086 list_for_each_entry_safe(hook, next,
2087 &basechain->hook_list, list) {
2088 list_del_rcu(&hook->list);
2089 kfree_rcu(hook, rcu);
2090 }
2091 }
2092 module_put(basechain->type->owner);
2093 if (rcu_access_pointer(basechain->stats)) {
2094 static_branch_dec(&nft_counters_enabled);
2095 free_percpu(rcu_dereference_raw(basechain->stats));
2096 }
2097 kfree(chain->name);
2098 kfree(chain->udata);
2099 kfree(basechain);
2100 } else {
2101 kfree(chain->name);
2102 kfree(chain->udata);
2103 kfree(chain);
2104 }
2105 }
2106
nft_netdev_hook_alloc(struct net * net,const struct nlattr * attr)2107 static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
2108 const struct nlattr *attr)
2109 {
2110 struct net_device *dev;
2111 char ifname[IFNAMSIZ];
2112 struct nft_hook *hook;
2113 int err;
2114
2115 hook = kzalloc(sizeof(struct nft_hook), GFP_KERNEL_ACCOUNT);
2116 if (!hook) {
2117 err = -ENOMEM;
2118 goto err_hook_alloc;
2119 }
2120
2121 nla_strscpy(ifname, attr, IFNAMSIZ);
2122 /* nf_tables_netdev_event() is called under rtnl_mutex, this is
2123 * indirectly serializing all the other holders of the commit_mutex with
2124 * the rtnl_mutex.
2125 */
2126 dev = __dev_get_by_name(net, ifname);
2127 if (!dev) {
2128 err = -ENOENT;
2129 goto err_hook_dev;
2130 }
2131 hook->ops.dev = dev;
2132
2133 return hook;
2134
2135 err_hook_dev:
2136 kfree(hook);
2137 err_hook_alloc:
2138 return ERR_PTR(err);
2139 }
2140
nft_hook_list_find(struct list_head * hook_list,const struct nft_hook * this)2141 static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,
2142 const struct nft_hook *this)
2143 {
2144 struct nft_hook *hook;
2145
2146 list_for_each_entry(hook, hook_list, list) {
2147 if (this->ops.dev == hook->ops.dev)
2148 return hook;
2149 }
2150
2151 return NULL;
2152 }
2153
nf_tables_parse_netdev_hooks(struct net * net,const struct nlattr * attr,struct list_head * hook_list,struct netlink_ext_ack * extack)2154 static int nf_tables_parse_netdev_hooks(struct net *net,
2155 const struct nlattr *attr,
2156 struct list_head *hook_list,
2157 struct netlink_ext_ack *extack)
2158 {
2159 struct nft_hook *hook, *next;
2160 const struct nlattr *tmp;
2161 int rem, n = 0, err;
2162
2163 nla_for_each_nested(tmp, attr, rem) {
2164 if (nla_type(tmp) != NFTA_DEVICE_NAME) {
2165 err = -EINVAL;
2166 goto err_hook;
2167 }
2168
2169 hook = nft_netdev_hook_alloc(net, tmp);
2170 if (IS_ERR(hook)) {
2171 NL_SET_BAD_ATTR(extack, tmp);
2172 err = PTR_ERR(hook);
2173 goto err_hook;
2174 }
2175 if (nft_hook_list_find(hook_list, hook)) {
2176 NL_SET_BAD_ATTR(extack, tmp);
2177 kfree(hook);
2178 err = -EEXIST;
2179 goto err_hook;
2180 }
2181 list_add_tail(&hook->list, hook_list);
2182 n++;
2183
2184 if (n == NFT_NETDEVICE_MAX) {
2185 err = -EFBIG;
2186 goto err_hook;
2187 }
2188 }
2189
2190 return 0;
2191
2192 err_hook:
2193 list_for_each_entry_safe(hook, next, hook_list, list) {
2194 list_del(&hook->list);
2195 kfree(hook);
2196 }
2197 return err;
2198 }
2199
2200 struct nft_chain_hook {
2201 u32 num;
2202 s32 priority;
2203 const struct nft_chain_type *type;
2204 struct list_head list;
2205 };
2206
nft_chain_parse_netdev(struct net * net,struct nlattr * tb[],struct list_head * hook_list,struct netlink_ext_ack * extack,u32 flags)2207 static int nft_chain_parse_netdev(struct net *net, struct nlattr *tb[],
2208 struct list_head *hook_list,
2209 struct netlink_ext_ack *extack, u32 flags)
2210 {
2211 struct nft_hook *hook;
2212 int err;
2213
2214 if (tb[NFTA_HOOK_DEV]) {
2215 hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]);
2216 if (IS_ERR(hook)) {
2217 NL_SET_BAD_ATTR(extack, tb[NFTA_HOOK_DEV]);
2218 return PTR_ERR(hook);
2219 }
2220
2221 list_add_tail(&hook->list, hook_list);
2222 } else if (tb[NFTA_HOOK_DEVS]) {
2223 err = nf_tables_parse_netdev_hooks(net, tb[NFTA_HOOK_DEVS],
2224 hook_list, extack);
2225 if (err < 0)
2226 return err;
2227
2228 }
2229
2230 if (flags & NFT_CHAIN_HW_OFFLOAD &&
2231 list_empty(hook_list))
2232 return -EINVAL;
2233
2234 return 0;
2235 }
2236
nft_chain_parse_hook(struct net * net,struct nft_base_chain * basechain,const struct nlattr * const nla[],struct nft_chain_hook * hook,u8 family,u32 flags,struct netlink_ext_ack * extack)2237 static int nft_chain_parse_hook(struct net *net,
2238 struct nft_base_chain *basechain,
2239 const struct nlattr * const nla[],
2240 struct nft_chain_hook *hook, u8 family,
2241 u32 flags, struct netlink_ext_ack *extack)
2242 {
2243 struct nftables_pernet *nft_net = nft_pernet(net);
2244 struct nlattr *ha[NFTA_HOOK_MAX + 1];
2245 const struct nft_chain_type *type;
2246 int err;
2247
2248 lockdep_assert_held(&nft_net->commit_mutex);
2249 lockdep_nfnl_nft_mutex_not_held();
2250
2251 err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX,
2252 nla[NFTA_CHAIN_HOOK],
2253 nft_hook_policy, NULL);
2254 if (err < 0)
2255 return err;
2256
2257 if (!basechain) {
2258 if (!ha[NFTA_HOOK_HOOKNUM] ||
2259 !ha[NFTA_HOOK_PRIORITY]) {
2260 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
2261 return -ENOENT;
2262 }
2263
2264 hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
2265 hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
2266
2267 type = __nft_chain_type_get(family, NFT_CHAIN_T_DEFAULT);
2268 if (!type)
2269 return -EOPNOTSUPP;
2270
2271 if (nla[NFTA_CHAIN_TYPE]) {
2272 type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE],
2273 family, true);
2274 if (IS_ERR(type)) {
2275 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]);
2276 return PTR_ERR(type);
2277 }
2278 }
2279 if (hook->num >= NFT_MAX_HOOKS || !(type->hook_mask & (1 << hook->num)))
2280 return -EOPNOTSUPP;
2281
2282 if (type->type == NFT_CHAIN_T_NAT &&
2283 hook->priority <= NF_IP_PRI_CONNTRACK)
2284 return -EOPNOTSUPP;
2285 } else {
2286 if (ha[NFTA_HOOK_HOOKNUM]) {
2287 hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
2288 if (hook->num != basechain->ops.hooknum)
2289 return -EOPNOTSUPP;
2290 }
2291 if (ha[NFTA_HOOK_PRIORITY]) {
2292 hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
2293 if (hook->priority != basechain->ops.priority)
2294 return -EOPNOTSUPP;
2295 }
2296
2297 if (nla[NFTA_CHAIN_TYPE]) {
2298 type = __nf_tables_chain_type_lookup(nla[NFTA_CHAIN_TYPE],
2299 family);
2300 if (!type) {
2301 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]);
2302 return -ENOENT;
2303 }
2304 } else {
2305 type = basechain->type;
2306 }
2307 }
2308
2309 if (!try_module_get(type->owner)) {
2310 if (nla[NFTA_CHAIN_TYPE])
2311 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]);
2312 return -ENOENT;
2313 }
2314
2315 hook->type = type;
2316
2317 INIT_LIST_HEAD(&hook->list);
2318 if (nft_base_chain_netdev(family, hook->num)) {
2319 err = nft_chain_parse_netdev(net, ha, &hook->list, extack, flags);
2320 if (err < 0) {
2321 module_put(type->owner);
2322 return err;
2323 }
2324 } else if (ha[NFTA_HOOK_DEV] || ha[NFTA_HOOK_DEVS]) {
2325 module_put(type->owner);
2326 return -EOPNOTSUPP;
2327 }
2328
2329 return 0;
2330 }
2331
nft_chain_release_hook(struct nft_chain_hook * hook)2332 static void nft_chain_release_hook(struct nft_chain_hook *hook)
2333 {
2334 struct nft_hook *h, *next;
2335
2336 list_for_each_entry_safe(h, next, &hook->list, list) {
2337 list_del(&h->list);
2338 kfree(h);
2339 }
2340 module_put(hook->type->owner);
2341 }
2342
nft_last_rule(const struct nft_chain * chain,const void * ptr)2343 static void nft_last_rule(const struct nft_chain *chain, const void *ptr)
2344 {
2345 struct nft_rule_dp_last *lrule;
2346
2347 BUILD_BUG_ON(offsetof(struct nft_rule_dp_last, end) != 0);
2348
2349 lrule = (struct nft_rule_dp_last *)ptr;
2350 lrule->end.is_last = 1;
2351 lrule->chain = chain;
2352 /* blob size does not include the trailer rule */
2353 }
2354
nf_tables_chain_alloc_rules(const struct nft_chain * chain,unsigned int size)2355 static struct nft_rule_blob *nf_tables_chain_alloc_rules(const struct nft_chain *chain,
2356 unsigned int size)
2357 {
2358 struct nft_rule_blob *blob;
2359
2360 if (size > INT_MAX)
2361 return NULL;
2362
2363 size += sizeof(struct nft_rule_blob) + sizeof(struct nft_rule_dp_last);
2364
2365 blob = kvmalloc(size, GFP_KERNEL_ACCOUNT);
2366 if (!blob)
2367 return NULL;
2368
2369 blob->size = 0;
2370 nft_last_rule(chain, blob->data);
2371
2372 return blob;
2373 }
2374
nft_basechain_hook_init(struct nf_hook_ops * ops,u8 family,const struct nft_chain_hook * hook,struct nft_chain * chain)2375 static void nft_basechain_hook_init(struct nf_hook_ops *ops, u8 family,
2376 const struct nft_chain_hook *hook,
2377 struct nft_chain *chain)
2378 {
2379 ops->pf = family;
2380 ops->hooknum = hook->num;
2381 ops->priority = hook->priority;
2382 ops->priv = chain;
2383 ops->hook = hook->type->hooks[ops->hooknum];
2384 ops->hook_ops_type = NF_HOOK_OP_NF_TABLES;
2385 }
2386
nft_basechain_init(struct nft_base_chain * basechain,u8 family,struct nft_chain_hook * hook,u32 flags)2387 static int nft_basechain_init(struct nft_base_chain *basechain, u8 family,
2388 struct nft_chain_hook *hook, u32 flags)
2389 {
2390 struct nft_chain *chain;
2391 struct nft_hook *h;
2392
2393 basechain->type = hook->type;
2394 INIT_LIST_HEAD(&basechain->hook_list);
2395 chain = &basechain->chain;
2396
2397 if (nft_base_chain_netdev(family, hook->num)) {
2398 list_splice_init(&hook->list, &basechain->hook_list);
2399 list_for_each_entry(h, &basechain->hook_list, list)
2400 nft_basechain_hook_init(&h->ops, family, hook, chain);
2401 }
2402 nft_basechain_hook_init(&basechain->ops, family, hook, chain);
2403
2404 chain->flags |= NFT_CHAIN_BASE | flags;
2405 basechain->policy = NF_ACCEPT;
2406 if (chain->flags & NFT_CHAIN_HW_OFFLOAD &&
2407 !nft_chain_offload_support(basechain)) {
2408 list_splice_init(&basechain->hook_list, &hook->list);
2409 return -EOPNOTSUPP;
2410 }
2411
2412 flow_block_init(&basechain->flow_block);
2413
2414 return 0;
2415 }
2416
nft_chain_add(struct nft_table * table,struct nft_chain * chain)2417 int nft_chain_add(struct nft_table *table, struct nft_chain *chain)
2418 {
2419 int err;
2420
2421 err = rhltable_insert_key(&table->chains_ht, chain->name,
2422 &chain->rhlhead, nft_chain_ht_params);
2423 if (err)
2424 return err;
2425
2426 list_add_tail_rcu(&chain->list, &table->chains);
2427
2428 return 0;
2429 }
2430
2431 static u64 chain_id;
2432
nf_tables_addchain(struct nft_ctx * ctx,u8 family,u8 genmask,u8 policy,u32 flags,struct netlink_ext_ack * extack)2433 static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
2434 u8 policy, u32 flags,
2435 struct netlink_ext_ack *extack)
2436 {
2437 const struct nlattr * const *nla = ctx->nla;
2438 struct nft_table *table = ctx->table;
2439 struct nft_base_chain *basechain;
2440 struct net *net = ctx->net;
2441 char name[NFT_NAME_MAXLEN];
2442 struct nft_rule_blob *blob;
2443 struct nft_trans *trans;
2444 struct nft_chain *chain;
2445 int err;
2446
2447 if (nla[NFTA_CHAIN_HOOK]) {
2448 struct nft_stats __percpu *stats = NULL;
2449 struct nft_chain_hook hook = {};
2450
2451 if (table->flags & __NFT_TABLE_F_UPDATE)
2452 return -EINVAL;
2453
2454 if (flags & NFT_CHAIN_BINDING)
2455 return -EOPNOTSUPP;
2456
2457 err = nft_chain_parse_hook(net, NULL, nla, &hook, family, flags,
2458 extack);
2459 if (err < 0)
2460 return err;
2461
2462 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL_ACCOUNT);
2463 if (basechain == NULL) {
2464 nft_chain_release_hook(&hook);
2465 return -ENOMEM;
2466 }
2467 chain = &basechain->chain;
2468
2469 if (nla[NFTA_CHAIN_COUNTERS]) {
2470 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
2471 if (IS_ERR(stats)) {
2472 nft_chain_release_hook(&hook);
2473 kfree(basechain);
2474 return PTR_ERR(stats);
2475 }
2476 rcu_assign_pointer(basechain->stats, stats);
2477 }
2478
2479 err = nft_basechain_init(basechain, family, &hook, flags);
2480 if (err < 0) {
2481 nft_chain_release_hook(&hook);
2482 kfree(basechain);
2483 free_percpu(stats);
2484 return err;
2485 }
2486 if (stats)
2487 static_branch_inc(&nft_counters_enabled);
2488 } else {
2489 if (flags & NFT_CHAIN_BASE)
2490 return -EINVAL;
2491 if (flags & NFT_CHAIN_HW_OFFLOAD)
2492 return -EOPNOTSUPP;
2493
2494 chain = kzalloc(sizeof(*chain), GFP_KERNEL_ACCOUNT);
2495 if (chain == NULL)
2496 return -ENOMEM;
2497
2498 chain->flags = flags;
2499 }
2500 ctx->chain = chain;
2501
2502 INIT_LIST_HEAD(&chain->rules);
2503 chain->handle = nf_tables_alloc_handle(table);
2504 chain->table = table;
2505
2506 if (nla[NFTA_CHAIN_NAME]) {
2507 chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT);
2508 } else {
2509 if (!(flags & NFT_CHAIN_BINDING)) {
2510 err = -EINVAL;
2511 goto err_destroy_chain;
2512 }
2513
2514 snprintf(name, sizeof(name), "__chain%llu", ++chain_id);
2515 chain->name = kstrdup(name, GFP_KERNEL_ACCOUNT);
2516 }
2517
2518 if (!chain->name) {
2519 err = -ENOMEM;
2520 goto err_destroy_chain;
2521 }
2522
2523 if (nla[NFTA_CHAIN_USERDATA]) {
2524 chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL_ACCOUNT);
2525 if (chain->udata == NULL) {
2526 err = -ENOMEM;
2527 goto err_destroy_chain;
2528 }
2529 chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]);
2530 }
2531
2532 blob = nf_tables_chain_alloc_rules(chain, 0);
2533 if (!blob) {
2534 err = -ENOMEM;
2535 goto err_destroy_chain;
2536 }
2537
2538 RCU_INIT_POINTER(chain->blob_gen_0, blob);
2539 RCU_INIT_POINTER(chain->blob_gen_1, blob);
2540
2541 if (!nft_use_inc(&table->use)) {
2542 err = -EMFILE;
2543 goto err_destroy_chain;
2544 }
2545
2546 trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN);
2547 if (IS_ERR(trans)) {
2548 err = PTR_ERR(trans);
2549 goto err_trans;
2550 }
2551
2552 nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET;
2553 if (nft_is_base_chain(chain))
2554 nft_trans_chain_policy(trans) = policy;
2555
2556 err = nft_chain_add(table, chain);
2557 if (err < 0)
2558 goto err_chain_add;
2559
2560 /* This must be LAST to ensure no packets are walking over this chain. */
2561 err = nf_tables_register_hook(net, table, chain);
2562 if (err < 0)
2563 goto err_register_hook;
2564
2565 return 0;
2566
2567 err_register_hook:
2568 nft_chain_del(chain);
2569 err_chain_add:
2570 nft_trans_destroy(trans);
2571 err_trans:
2572 nft_use_dec_restore(&table->use);
2573 err_destroy_chain:
2574 nf_tables_chain_destroy(chain);
2575
2576 return err;
2577 }
2578
nf_tables_updchain(struct nft_ctx * ctx,u8 genmask,u8 policy,u32 flags,const struct nlattr * attr,struct netlink_ext_ack * extack)2579 static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy,
2580 u32 flags, const struct nlattr *attr,
2581 struct netlink_ext_ack *extack)
2582 {
2583 const struct nlattr * const *nla = ctx->nla;
2584 struct nft_base_chain *basechain = NULL;
2585 struct nft_table *table = ctx->table;
2586 struct nft_chain *chain = ctx->chain;
2587 struct nft_chain_hook hook = {};
2588 struct nft_stats *stats = NULL;
2589 struct nft_hook *h, *next;
2590 struct nf_hook_ops *ops;
2591 struct nft_trans *trans;
2592 bool unregister = false;
2593 int err;
2594
2595 if (chain->flags ^ flags)
2596 return -EOPNOTSUPP;
2597
2598 INIT_LIST_HEAD(&hook.list);
2599
2600 if (nla[NFTA_CHAIN_HOOK]) {
2601 if (!nft_is_base_chain(chain)) {
2602 NL_SET_BAD_ATTR(extack, attr);
2603 return -EEXIST;
2604 }
2605
2606 basechain = nft_base_chain(chain);
2607 err = nft_chain_parse_hook(ctx->net, basechain, nla, &hook,
2608 ctx->family, flags, extack);
2609 if (err < 0)
2610 return err;
2611
2612 if (basechain->type != hook.type) {
2613 nft_chain_release_hook(&hook);
2614 NL_SET_BAD_ATTR(extack, attr);
2615 return -EEXIST;
2616 }
2617
2618 if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) {
2619 list_for_each_entry_safe(h, next, &hook.list, list) {
2620 h->ops.pf = basechain->ops.pf;
2621 h->ops.hooknum = basechain->ops.hooknum;
2622 h->ops.priority = basechain->ops.priority;
2623 h->ops.priv = basechain->ops.priv;
2624 h->ops.hook = basechain->ops.hook;
2625
2626 if (nft_hook_list_find(&basechain->hook_list, h)) {
2627 list_del(&h->list);
2628 kfree(h);
2629 }
2630 }
2631 } else {
2632 ops = &basechain->ops;
2633 if (ops->hooknum != hook.num ||
2634 ops->priority != hook.priority) {
2635 nft_chain_release_hook(&hook);
2636 NL_SET_BAD_ATTR(extack, attr);
2637 return -EEXIST;
2638 }
2639 }
2640 }
2641
2642 if (nla[NFTA_CHAIN_HANDLE] &&
2643 nla[NFTA_CHAIN_NAME]) {
2644 struct nft_chain *chain2;
2645
2646 chain2 = nft_chain_lookup(ctx->net, table,
2647 nla[NFTA_CHAIN_NAME], genmask);
2648 if (!IS_ERR(chain2)) {
2649 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
2650 err = -EEXIST;
2651 goto err_hooks;
2652 }
2653 }
2654
2655 if (table->flags & __NFT_TABLE_F_UPDATE &&
2656 !list_empty(&hook.list)) {
2657 NL_SET_BAD_ATTR(extack, attr);
2658 err = -EOPNOTSUPP;
2659 goto err_hooks;
2660 }
2661
2662 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
2663 nft_is_base_chain(chain) &&
2664 !list_empty(&hook.list)) {
2665 basechain = nft_base_chain(chain);
2666 ops = &basechain->ops;
2667
2668 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) {
2669 err = nft_netdev_register_hooks(ctx->net, &hook.list);
2670 if (err < 0)
2671 goto err_hooks;
2672 }
2673 }
2674
2675 unregister = true;
2676
2677 if (nla[NFTA_CHAIN_COUNTERS]) {
2678 if (!nft_is_base_chain(chain)) {
2679 err = -EOPNOTSUPP;
2680 goto err_hooks;
2681 }
2682
2683 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
2684 if (IS_ERR(stats)) {
2685 err = PTR_ERR(stats);
2686 goto err_hooks;
2687 }
2688 }
2689
2690 err = -ENOMEM;
2691 trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN,
2692 sizeof(struct nft_trans_chain));
2693 if (trans == NULL)
2694 goto err_trans;
2695
2696 nft_trans_chain_stats(trans) = stats;
2697 nft_trans_chain_update(trans) = true;
2698
2699 if (nla[NFTA_CHAIN_POLICY])
2700 nft_trans_chain_policy(trans) = policy;
2701 else
2702 nft_trans_chain_policy(trans) = -1;
2703
2704 if (nla[NFTA_CHAIN_HANDLE] &&
2705 nla[NFTA_CHAIN_NAME]) {
2706 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
2707 struct nft_trans *tmp;
2708 char *name;
2709
2710 err = -ENOMEM;
2711 name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT);
2712 if (!name)
2713 goto err_trans;
2714
2715 err = -EEXIST;
2716 list_for_each_entry(tmp, &nft_net->commit_list, list) {
2717 if (tmp->msg_type == NFT_MSG_NEWCHAIN &&
2718 tmp->ctx.table == table &&
2719 nft_trans_chain_update(tmp) &&
2720 nft_trans_chain_name(tmp) &&
2721 strcmp(name, nft_trans_chain_name(tmp)) == 0) {
2722 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
2723 kfree(name);
2724 goto err_trans;
2725 }
2726 }
2727
2728 nft_trans_chain_name(trans) = name;
2729 }
2730
2731 nft_trans_basechain(trans) = basechain;
2732 INIT_LIST_HEAD(&nft_trans_chain_hooks(trans));
2733 list_splice(&hook.list, &nft_trans_chain_hooks(trans));
2734 if (nla[NFTA_CHAIN_HOOK])
2735 module_put(hook.type->owner);
2736
2737 nft_trans_commit_list_add_tail(ctx->net, trans);
2738
2739 return 0;
2740
2741 err_trans:
2742 free_percpu(stats);
2743 kfree(trans);
2744 err_hooks:
2745 if (nla[NFTA_CHAIN_HOOK]) {
2746 list_for_each_entry_safe(h, next, &hook.list, list) {
2747 if (unregister)
2748 nf_unregister_net_hook(ctx->net, &h->ops);
2749 list_del(&h->list);
2750 kfree_rcu(h, rcu);
2751 }
2752 module_put(hook.type->owner);
2753 }
2754
2755 return err;
2756 }
2757
nft_chain_lookup_byid(const struct net * net,const struct nft_table * table,const struct nlattr * nla,u8 genmask)2758 static struct nft_chain *nft_chain_lookup_byid(const struct net *net,
2759 const struct nft_table *table,
2760 const struct nlattr *nla, u8 genmask)
2761 {
2762 struct nftables_pernet *nft_net = nft_pernet(net);
2763 u32 id = ntohl(nla_get_be32(nla));
2764 struct nft_trans *trans;
2765
2766 list_for_each_entry(trans, &nft_net->commit_list, list) {
2767 struct nft_chain *chain = trans->ctx.chain;
2768
2769 if (trans->msg_type == NFT_MSG_NEWCHAIN &&
2770 chain->table == table &&
2771 id == nft_trans_chain_id(trans) &&
2772 nft_active_genmask(chain, genmask))
2773 return chain;
2774 }
2775 return ERR_PTR(-ENOENT);
2776 }
2777
nf_tables_newchain(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])2778 static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info,
2779 const struct nlattr * const nla[])
2780 {
2781 struct nftables_pernet *nft_net = nft_pernet(info->net);
2782 struct netlink_ext_ack *extack = info->extack;
2783 u8 genmask = nft_genmask_next(info->net);
2784 u8 family = info->nfmsg->nfgen_family;
2785 struct nft_chain *chain = NULL;
2786 struct net *net = info->net;
2787 const struct nlattr *attr;
2788 struct nft_table *table;
2789 u8 policy = NF_ACCEPT;
2790 struct nft_ctx ctx;
2791 u64 handle = 0;
2792 u32 flags = 0;
2793
2794 lockdep_assert_held(&nft_net->commit_mutex);
2795
2796 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask,
2797 NETLINK_CB(skb).portid);
2798 if (IS_ERR(table)) {
2799 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
2800 return PTR_ERR(table);
2801 }
2802
2803 chain = NULL;
2804 attr = nla[NFTA_CHAIN_NAME];
2805
2806 if (nla[NFTA_CHAIN_HANDLE]) {
2807 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
2808 chain = nft_chain_lookup_byhandle(table, handle, genmask);
2809 if (IS_ERR(chain)) {
2810 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]);
2811 return PTR_ERR(chain);
2812 }
2813 attr = nla[NFTA_CHAIN_HANDLE];
2814 } else if (nla[NFTA_CHAIN_NAME]) {
2815 chain = nft_chain_lookup(net, table, attr, genmask);
2816 if (IS_ERR(chain)) {
2817 if (PTR_ERR(chain) != -ENOENT) {
2818 NL_SET_BAD_ATTR(extack, attr);
2819 return PTR_ERR(chain);
2820 }
2821 chain = NULL;
2822 }
2823 } else if (!nla[NFTA_CHAIN_ID]) {
2824 return -EINVAL;
2825 }
2826
2827 if (nla[NFTA_CHAIN_POLICY]) {
2828 if (chain != NULL &&
2829 !nft_is_base_chain(chain)) {
2830 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
2831 return -EOPNOTSUPP;
2832 }
2833
2834 if (chain == NULL &&
2835 nla[NFTA_CHAIN_HOOK] == NULL) {
2836 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
2837 return -EOPNOTSUPP;
2838 }
2839
2840 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
2841 switch (policy) {
2842 case NF_DROP:
2843 case NF_ACCEPT:
2844 break;
2845 default:
2846 return -EINVAL;
2847 }
2848 }
2849
2850 if (nla[NFTA_CHAIN_FLAGS])
2851 flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS]));
2852 else if (chain)
2853 flags = chain->flags;
2854
2855 if (flags & ~NFT_CHAIN_FLAGS)
2856 return -EOPNOTSUPP;
2857
2858 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
2859
2860 if (chain != NULL) {
2861 if (chain->flags & NFT_CHAIN_BINDING)
2862 return -EINVAL;
2863
2864 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
2865 NL_SET_BAD_ATTR(extack, attr);
2866 return -EEXIST;
2867 }
2868 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
2869 return -EOPNOTSUPP;
2870
2871 flags |= chain->flags & NFT_CHAIN_BASE;
2872 return nf_tables_updchain(&ctx, genmask, policy, flags, attr,
2873 extack);
2874 }
2875
2876 return nf_tables_addchain(&ctx, family, genmask, policy, flags, extack);
2877 }
2878
nft_delchain_hook(struct nft_ctx * ctx,struct nft_base_chain * basechain,struct netlink_ext_ack * extack)2879 static int nft_delchain_hook(struct nft_ctx *ctx,
2880 struct nft_base_chain *basechain,
2881 struct netlink_ext_ack *extack)
2882 {
2883 const struct nft_chain *chain = &basechain->chain;
2884 const struct nlattr * const *nla = ctx->nla;
2885 struct nft_chain_hook chain_hook = {};
2886 struct nft_hook *this, *hook;
2887 LIST_HEAD(chain_del_list);
2888 struct nft_trans *trans;
2889 int err;
2890
2891 if (ctx->table->flags & __NFT_TABLE_F_UPDATE)
2892 return -EOPNOTSUPP;
2893
2894 err = nft_chain_parse_hook(ctx->net, basechain, nla, &chain_hook,
2895 ctx->family, chain->flags, extack);
2896 if (err < 0)
2897 return err;
2898
2899 list_for_each_entry(this, &chain_hook.list, list) {
2900 hook = nft_hook_list_find(&basechain->hook_list, this);
2901 if (!hook) {
2902 err = -ENOENT;
2903 goto err_chain_del_hook;
2904 }
2905 list_move(&hook->list, &chain_del_list);
2906 }
2907
2908 trans = nft_trans_alloc(ctx, NFT_MSG_DELCHAIN,
2909 sizeof(struct nft_trans_chain));
2910 if (!trans) {
2911 err = -ENOMEM;
2912 goto err_chain_del_hook;
2913 }
2914
2915 nft_trans_basechain(trans) = basechain;
2916 nft_trans_chain_update(trans) = true;
2917 INIT_LIST_HEAD(&nft_trans_chain_hooks(trans));
2918 list_splice(&chain_del_list, &nft_trans_chain_hooks(trans));
2919 nft_chain_release_hook(&chain_hook);
2920
2921 nft_trans_commit_list_add_tail(ctx->net, trans);
2922
2923 return 0;
2924
2925 err_chain_del_hook:
2926 list_splice(&chain_del_list, &basechain->hook_list);
2927 nft_chain_release_hook(&chain_hook);
2928
2929 return err;
2930 }
2931
nf_tables_delchain(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])2932 static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info,
2933 const struct nlattr * const nla[])
2934 {
2935 struct netlink_ext_ack *extack = info->extack;
2936 u8 genmask = nft_genmask_next(info->net);
2937 u8 family = info->nfmsg->nfgen_family;
2938 struct net *net = info->net;
2939 const struct nlattr *attr;
2940 struct nft_table *table;
2941 struct nft_chain *chain;
2942 struct nft_rule *rule;
2943 struct nft_ctx ctx;
2944 u64 handle;
2945 u32 use;
2946 int err;
2947
2948 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask,
2949 NETLINK_CB(skb).portid);
2950 if (IS_ERR(table)) {
2951 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
2952 return PTR_ERR(table);
2953 }
2954
2955 if (nla[NFTA_CHAIN_HANDLE]) {
2956 attr = nla[NFTA_CHAIN_HANDLE];
2957 handle = be64_to_cpu(nla_get_be64(attr));
2958 chain = nft_chain_lookup_byhandle(table, handle, genmask);
2959 } else {
2960 attr = nla[NFTA_CHAIN_NAME];
2961 chain = nft_chain_lookup(net, table, attr, genmask);
2962 }
2963 if (IS_ERR(chain)) {
2964 if (PTR_ERR(chain) == -ENOENT &&
2965 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYCHAIN)
2966 return 0;
2967
2968 NL_SET_BAD_ATTR(extack, attr);
2969 return PTR_ERR(chain);
2970 }
2971
2972 if (nft_chain_binding(chain))
2973 return -EOPNOTSUPP;
2974
2975 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
2976
2977 if (nla[NFTA_CHAIN_HOOK]) {
2978 if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYCHAIN ||
2979 chain->flags & NFT_CHAIN_HW_OFFLOAD)
2980 return -EOPNOTSUPP;
2981
2982 if (nft_is_base_chain(chain)) {
2983 struct nft_base_chain *basechain = nft_base_chain(chain);
2984
2985 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
2986 return nft_delchain_hook(&ctx, basechain, extack);
2987 }
2988 }
2989
2990 if (info->nlh->nlmsg_flags & NLM_F_NONREC &&
2991 chain->use > 0)
2992 return -EBUSY;
2993
2994 use = chain->use;
2995 list_for_each_entry(rule, &chain->rules, list) {
2996 if (!nft_is_active_next(net, rule))
2997 continue;
2998 use--;
2999
3000 err = nft_delrule(&ctx, rule);
3001 if (err < 0)
3002 return err;
3003 }
3004
3005 /* There are rules and elements that are still holding references to us,
3006 * we cannot do a recursive removal in this case.
3007 */
3008 if (use > 0) {
3009 NL_SET_BAD_ATTR(extack, attr);
3010 return -EBUSY;
3011 }
3012
3013 return nft_delchain(&ctx);
3014 }
3015
3016 /*
3017 * Expressions
3018 */
3019
3020 /**
3021 * nft_register_expr - register nf_tables expr type
3022 * @type: expr type
3023 *
3024 * Registers the expr type for use with nf_tables. Returns zero on
3025 * success or a negative errno code otherwise.
3026 */
nft_register_expr(struct nft_expr_type * type)3027 int nft_register_expr(struct nft_expr_type *type)
3028 {
3029 nfnl_lock(NFNL_SUBSYS_NFTABLES);
3030 if (type->family == NFPROTO_UNSPEC)
3031 list_add_tail_rcu(&type->list, &nf_tables_expressions);
3032 else
3033 list_add_rcu(&type->list, &nf_tables_expressions);
3034 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
3035 return 0;
3036 }
3037 EXPORT_SYMBOL_GPL(nft_register_expr);
3038
3039 /**
3040 * nft_unregister_expr - unregister nf_tables expr type
3041 * @type: expr type
3042 *
3043 * Unregisters the expr typefor use with nf_tables.
3044 */
nft_unregister_expr(struct nft_expr_type * type)3045 void nft_unregister_expr(struct nft_expr_type *type)
3046 {
3047 nfnl_lock(NFNL_SUBSYS_NFTABLES);
3048 list_del_rcu(&type->list);
3049 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
3050 }
3051 EXPORT_SYMBOL_GPL(nft_unregister_expr);
3052
__nft_expr_type_get(u8 family,struct nlattr * nla)3053 static const struct nft_expr_type *__nft_expr_type_get(u8 family,
3054 struct nlattr *nla)
3055 {
3056 const struct nft_expr_type *type, *candidate = NULL;
3057
3058 list_for_each_entry_rcu(type, &nf_tables_expressions, list) {
3059 if (!nla_strcmp(nla, type->name)) {
3060 if (!type->family && !candidate)
3061 candidate = type;
3062 else if (type->family == family)
3063 candidate = type;
3064 }
3065 }
3066 return candidate;
3067 }
3068
3069 #ifdef CONFIG_MODULES
nft_expr_type_request_module(struct net * net,u8 family,struct nlattr * nla)3070 static int nft_expr_type_request_module(struct net *net, u8 family,
3071 struct nlattr *nla)
3072 {
3073 if (nft_request_module(net, "nft-expr-%u-%.*s", family,
3074 nla_len(nla), (char *)nla_data(nla)) == -EAGAIN)
3075 return -EAGAIN;
3076
3077 return 0;
3078 }
3079 #endif
3080
nft_expr_type_get(struct net * net,u8 family,struct nlattr * nla)3081 static const struct nft_expr_type *nft_expr_type_get(struct net *net,
3082 u8 family,
3083 struct nlattr *nla)
3084 {
3085 const struct nft_expr_type *type;
3086
3087 if (nla == NULL)
3088 return ERR_PTR(-EINVAL);
3089
3090 rcu_read_lock();
3091 type = __nft_expr_type_get(family, nla);
3092 if (type != NULL && try_module_get(type->owner)) {
3093 rcu_read_unlock();
3094 return type;
3095 }
3096 rcu_read_unlock();
3097
3098 lockdep_nfnl_nft_mutex_not_held();
3099 #ifdef CONFIG_MODULES
3100 if (type == NULL) {
3101 if (nft_expr_type_request_module(net, family, nla) == -EAGAIN)
3102 return ERR_PTR(-EAGAIN);
3103
3104 if (nft_request_module(net, "nft-expr-%.*s",
3105 nla_len(nla),
3106 (char *)nla_data(nla)) == -EAGAIN)
3107 return ERR_PTR(-EAGAIN);
3108 }
3109 #endif
3110 return ERR_PTR(-ENOENT);
3111 }
3112
3113 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
3114 [NFTA_EXPR_NAME] = { .type = NLA_STRING,
3115 .len = NFT_MODULE_AUTOLOAD_LIMIT },
3116 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
3117 };
3118
nf_tables_fill_expr_info(struct sk_buff * skb,const struct nft_expr * expr,bool reset)3119 static int nf_tables_fill_expr_info(struct sk_buff *skb,
3120 const struct nft_expr *expr, bool reset)
3121 {
3122 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
3123 goto nla_put_failure;
3124
3125 if (expr->ops->dump) {
3126 struct nlattr *data = nla_nest_start_noflag(skb,
3127 NFTA_EXPR_DATA);
3128 if (data == NULL)
3129 goto nla_put_failure;
3130 if (expr->ops->dump(skb, expr, reset) < 0)
3131 goto nla_put_failure;
3132 nla_nest_end(skb, data);
3133 }
3134
3135 return skb->len;
3136
3137 nla_put_failure:
3138 return -1;
3139 };
3140
nft_expr_dump(struct sk_buff * skb,unsigned int attr,const struct nft_expr * expr,bool reset)3141 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
3142 const struct nft_expr *expr, bool reset)
3143 {
3144 struct nlattr *nest;
3145
3146 nest = nla_nest_start_noflag(skb, attr);
3147 if (!nest)
3148 goto nla_put_failure;
3149 if (nf_tables_fill_expr_info(skb, expr, reset) < 0)
3150 goto nla_put_failure;
3151 nla_nest_end(skb, nest);
3152 return 0;
3153
3154 nla_put_failure:
3155 return -1;
3156 }
3157
3158 struct nft_expr_info {
3159 const struct nft_expr_ops *ops;
3160 const struct nlattr *attr;
3161 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
3162 };
3163
nf_tables_expr_parse(const struct nft_ctx * ctx,const struct nlattr * nla,struct nft_expr_info * info)3164 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
3165 const struct nlattr *nla,
3166 struct nft_expr_info *info)
3167 {
3168 const struct nft_expr_type *type;
3169 const struct nft_expr_ops *ops;
3170 struct nlattr *tb[NFTA_EXPR_MAX + 1];
3171 int err;
3172
3173 err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla,
3174 nft_expr_policy, NULL);
3175 if (err < 0)
3176 return err;
3177
3178 type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]);
3179 if (IS_ERR(type))
3180 return PTR_ERR(type);
3181
3182 if (tb[NFTA_EXPR_DATA]) {
3183 err = nla_parse_nested_deprecated(info->tb, type->maxattr,
3184 tb[NFTA_EXPR_DATA],
3185 type->policy, NULL);
3186 if (err < 0)
3187 goto err1;
3188 } else
3189 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
3190
3191 if (type->select_ops != NULL) {
3192 ops = type->select_ops(ctx,
3193 (const struct nlattr * const *)info->tb);
3194 if (IS_ERR(ops)) {
3195 err = PTR_ERR(ops);
3196 #ifdef CONFIG_MODULES
3197 if (err == -EAGAIN)
3198 if (nft_expr_type_request_module(ctx->net,
3199 ctx->family,
3200 tb[NFTA_EXPR_NAME]) != -EAGAIN)
3201 err = -ENOENT;
3202 #endif
3203 goto err1;
3204 }
3205 } else
3206 ops = type->ops;
3207
3208 info->attr = nla;
3209 info->ops = ops;
3210
3211 return 0;
3212
3213 err1:
3214 module_put(type->owner);
3215 return err;
3216 }
3217
nft_expr_inner_parse(const struct nft_ctx * ctx,const struct nlattr * nla,struct nft_expr_info * info)3218 int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla,
3219 struct nft_expr_info *info)
3220 {
3221 struct nlattr *tb[NFTA_EXPR_MAX + 1];
3222 const struct nft_expr_type *type;
3223 int err;
3224
3225 err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla,
3226 nft_expr_policy, NULL);
3227 if (err < 0)
3228 return err;
3229
3230 if (!tb[NFTA_EXPR_DATA] || !tb[NFTA_EXPR_NAME])
3231 return -EINVAL;
3232
3233 rcu_read_lock();
3234
3235 type = __nft_expr_type_get(ctx->family, tb[NFTA_EXPR_NAME]);
3236 if (!type) {
3237 err = -ENOENT;
3238 goto out_unlock;
3239 }
3240
3241 if (!type->inner_ops) {
3242 err = -EOPNOTSUPP;
3243 goto out_unlock;
3244 }
3245
3246 err = nla_parse_nested_deprecated(info->tb, type->maxattr,
3247 tb[NFTA_EXPR_DATA],
3248 type->policy, NULL);
3249 if (err < 0)
3250 goto out_unlock;
3251
3252 info->attr = nla;
3253 info->ops = type->inner_ops;
3254
3255 /* No module reference will be taken on type->owner.
3256 * Presence of type->inner_ops implies that the expression
3257 * is builtin, so it cannot go away.
3258 */
3259 rcu_read_unlock();
3260 return 0;
3261
3262 out_unlock:
3263 rcu_read_unlock();
3264 return err;
3265 }
3266
nf_tables_newexpr(const struct nft_ctx * ctx,const struct nft_expr_info * expr_info,struct nft_expr * expr)3267 static int nf_tables_newexpr(const struct nft_ctx *ctx,
3268 const struct nft_expr_info *expr_info,
3269 struct nft_expr *expr)
3270 {
3271 const struct nft_expr_ops *ops = expr_info->ops;
3272 int err;
3273
3274 expr->ops = ops;
3275 if (ops->init) {
3276 err = ops->init(ctx, expr, (const struct nlattr **)expr_info->tb);
3277 if (err < 0)
3278 goto err1;
3279 }
3280
3281 return 0;
3282 err1:
3283 expr->ops = NULL;
3284 return err;
3285 }
3286
nf_tables_expr_destroy(const struct nft_ctx * ctx,struct nft_expr * expr)3287 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
3288 struct nft_expr *expr)
3289 {
3290 const struct nft_expr_type *type = expr->ops->type;
3291
3292 if (expr->ops->destroy)
3293 expr->ops->destroy(ctx, expr);
3294 module_put(type->owner);
3295 }
3296
nft_expr_init(const struct nft_ctx * ctx,const struct nlattr * nla)3297 static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
3298 const struct nlattr *nla)
3299 {
3300 struct nft_expr_info expr_info;
3301 struct nft_expr *expr;
3302 struct module *owner;
3303 int err;
3304
3305 err = nf_tables_expr_parse(ctx, nla, &expr_info);
3306 if (err < 0)
3307 goto err_expr_parse;
3308
3309 err = -EOPNOTSUPP;
3310 if (!(expr_info.ops->type->flags & NFT_EXPR_STATEFUL))
3311 goto err_expr_stateful;
3312
3313 err = -ENOMEM;
3314 expr = kzalloc(expr_info.ops->size, GFP_KERNEL_ACCOUNT);
3315 if (expr == NULL)
3316 goto err_expr_stateful;
3317
3318 err = nf_tables_newexpr(ctx, &expr_info, expr);
3319 if (err < 0)
3320 goto err_expr_new;
3321
3322 return expr;
3323 err_expr_new:
3324 kfree(expr);
3325 err_expr_stateful:
3326 owner = expr_info.ops->type->owner;
3327 if (expr_info.ops->type->release_ops)
3328 expr_info.ops->type->release_ops(expr_info.ops);
3329
3330 module_put(owner);
3331 err_expr_parse:
3332 return ERR_PTR(err);
3333 }
3334
nft_expr_clone(struct nft_expr * dst,struct nft_expr * src)3335 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
3336 {
3337 int err;
3338
3339 if (src->ops->clone) {
3340 dst->ops = src->ops;
3341 err = src->ops->clone(dst, src);
3342 if (err < 0)
3343 return err;
3344 } else {
3345 memcpy(dst, src, src->ops->size);
3346 }
3347
3348 __module_get(src->ops->type->owner);
3349
3350 return 0;
3351 }
3352
nft_expr_destroy(const struct nft_ctx * ctx,struct nft_expr * expr)3353 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
3354 {
3355 nf_tables_expr_destroy(ctx, expr);
3356 kfree(expr);
3357 }
3358
3359 /*
3360 * Rules
3361 */
3362
__nft_rule_lookup(const struct net * net,const struct nft_chain * chain,u64 handle)3363 static struct nft_rule *__nft_rule_lookup(const struct net *net,
3364 const struct nft_chain *chain,
3365 u64 handle)
3366 {
3367 struct nft_rule *rule;
3368
3369 // FIXME: this sucks
3370 list_for_each_entry_rcu(rule, &chain->rules, list,
3371 lockdep_commit_lock_is_held(net)) {
3372 if (handle == rule->handle)
3373 return rule;
3374 }
3375
3376 return ERR_PTR(-ENOENT);
3377 }
3378
nft_rule_lookup(const struct net * net,const struct nft_chain * chain,const struct nlattr * nla)3379 static struct nft_rule *nft_rule_lookup(const struct net *net,
3380 const struct nft_chain *chain,
3381 const struct nlattr *nla)
3382 {
3383 if (nla == NULL)
3384 return ERR_PTR(-EINVAL);
3385
3386 return __nft_rule_lookup(net, chain, be64_to_cpu(nla_get_be64(nla)));
3387 }
3388
3389 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
3390 [NFTA_RULE_TABLE] = { .type = NLA_STRING,
3391 .len = NFT_TABLE_MAXNAMELEN - 1 },
3392 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
3393 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3394 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
3395 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
3396 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
3397 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
3398 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
3399 .len = NFT_USERDATA_MAXLEN },
3400 [NFTA_RULE_ID] = { .type = NLA_U32 },
3401 [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 },
3402 [NFTA_RULE_CHAIN_ID] = { .type = NLA_U32 },
3403 };
3404
nf_tables_fill_rule_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq,int event,u32 flags,int family,const struct nft_table * table,const struct nft_chain * chain,const struct nft_rule * rule,u64 handle,bool reset)3405 static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
3406 u32 portid, u32 seq, int event,
3407 u32 flags, int family,
3408 const struct nft_table *table,
3409 const struct nft_chain *chain,
3410 const struct nft_rule *rule, u64 handle,
3411 bool reset)
3412 {
3413 struct nlmsghdr *nlh;
3414 const struct nft_expr *expr, *next;
3415 struct nlattr *list;
3416 u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
3417
3418 nlh = nfnl_msg_put(skb, portid, seq, type, flags, family, NFNETLINK_V0,
3419 nft_base_seq(net));
3420 if (!nlh)
3421 goto nla_put_failure;
3422
3423 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
3424 goto nla_put_failure;
3425 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
3426 goto nla_put_failure;
3427 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle),
3428 NFTA_RULE_PAD))
3429 goto nla_put_failure;
3430
3431 if (event != NFT_MSG_DELRULE && handle) {
3432 if (nla_put_be64(skb, NFTA_RULE_POSITION, cpu_to_be64(handle),
3433 NFTA_RULE_PAD))
3434 goto nla_put_failure;
3435 }
3436
3437 if (chain->flags & NFT_CHAIN_HW_OFFLOAD)
3438 nft_flow_rule_stats(chain, rule);
3439
3440 list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS);
3441 if (list == NULL)
3442 goto nla_put_failure;
3443 nft_rule_for_each_expr(expr, next, rule) {
3444 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, reset) < 0)
3445 goto nla_put_failure;
3446 }
3447 nla_nest_end(skb, list);
3448
3449 if (rule->udata) {
3450 struct nft_userdata *udata = nft_userdata(rule);
3451 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
3452 udata->data) < 0)
3453 goto nla_put_failure;
3454 }
3455
3456 nlmsg_end(skb, nlh);
3457 return 0;
3458
3459 nla_put_failure:
3460 nlmsg_trim(skb, nlh);
3461 return -1;
3462 }
3463
nf_tables_rule_notify(const struct nft_ctx * ctx,const struct nft_rule * rule,int event)3464 static void nf_tables_rule_notify(const struct nft_ctx *ctx,
3465 const struct nft_rule *rule, int event)
3466 {
3467 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
3468 const struct nft_rule *prule;
3469 struct sk_buff *skb;
3470 u64 handle = 0;
3471 u16 flags = 0;
3472 int err;
3473
3474 if (!ctx->report &&
3475 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
3476 return;
3477
3478 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3479 if (skb == NULL)
3480 goto err;
3481
3482 if (event == NFT_MSG_NEWRULE &&
3483 !list_is_first(&rule->list, &ctx->chain->rules) &&
3484 !list_is_last(&rule->list, &ctx->chain->rules)) {
3485 prule = list_prev_entry(rule, list);
3486 handle = prule->handle;
3487 }
3488 if (ctx->flags & (NLM_F_APPEND | NLM_F_REPLACE))
3489 flags |= NLM_F_APPEND;
3490 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
3491 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
3492
3493 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
3494 event, flags, ctx->family, ctx->table,
3495 ctx->chain, rule, handle, false);
3496 if (err < 0) {
3497 kfree_skb(skb);
3498 goto err;
3499 }
3500
3501 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
3502 return;
3503 err:
3504 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
3505 }
3506
audit_log_rule_reset(const struct nft_table * table,unsigned int base_seq,unsigned int nentries)3507 static void audit_log_rule_reset(const struct nft_table *table,
3508 unsigned int base_seq,
3509 unsigned int nentries)
3510 {
3511 char *buf = kasprintf(GFP_ATOMIC, "%s:%u",
3512 table->name, base_seq);
3513
3514 audit_log_nfcfg(buf, table->family, nentries,
3515 AUDIT_NFT_OP_RULE_RESET, GFP_ATOMIC);
3516 kfree(buf);
3517 }
3518
3519 struct nft_rule_dump_ctx {
3520 char *table;
3521 char *chain;
3522 };
3523
__nf_tables_dump_rules(struct sk_buff * skb,unsigned int * idx,struct netlink_callback * cb,const struct nft_table * table,const struct nft_chain * chain,bool reset)3524 static int __nf_tables_dump_rules(struct sk_buff *skb,
3525 unsigned int *idx,
3526 struct netlink_callback *cb,
3527 const struct nft_table *table,
3528 const struct nft_chain *chain,
3529 bool reset)
3530 {
3531 struct net *net = sock_net(skb->sk);
3532 const struct nft_rule *rule, *prule;
3533 unsigned int s_idx = cb->args[0];
3534 unsigned int entries = 0;
3535 int ret = 0;
3536 u64 handle;
3537
3538 prule = NULL;
3539 list_for_each_entry_rcu(rule, &chain->rules, list) {
3540 if (!nft_is_active(net, rule))
3541 goto cont_skip;
3542 if (*idx < s_idx)
3543 goto cont;
3544 if (prule)
3545 handle = prule->handle;
3546 else
3547 handle = 0;
3548
3549 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
3550 cb->nlh->nlmsg_seq,
3551 NFT_MSG_NEWRULE,
3552 NLM_F_MULTI | NLM_F_APPEND,
3553 table->family,
3554 table, chain, rule, handle, reset) < 0) {
3555 ret = 1;
3556 break;
3557 }
3558 entries++;
3559 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3560 cont:
3561 prule = rule;
3562 cont_skip:
3563 (*idx)++;
3564 }
3565
3566 if (reset && entries)
3567 audit_log_rule_reset(table, cb->seq, entries);
3568
3569 return ret;
3570 }
3571
nf_tables_dump_rules(struct sk_buff * skb,struct netlink_callback * cb)3572 static int nf_tables_dump_rules(struct sk_buff *skb,
3573 struct netlink_callback *cb)
3574 {
3575 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
3576 const struct nft_rule_dump_ctx *ctx = cb->data;
3577 struct nft_table *table;
3578 const struct nft_chain *chain;
3579 unsigned int idx = 0;
3580 struct net *net = sock_net(skb->sk);
3581 int family = nfmsg->nfgen_family;
3582 struct nftables_pernet *nft_net;
3583 bool reset = false;
3584
3585 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET)
3586 reset = true;
3587
3588 rcu_read_lock();
3589 nft_net = nft_pernet(net);
3590 cb->seq = READ_ONCE(nft_net->base_seq);
3591
3592 list_for_each_entry_rcu(table, &nft_net->tables, list) {
3593 if (family != NFPROTO_UNSPEC && family != table->family)
3594 continue;
3595
3596 if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0)
3597 continue;
3598
3599 if (ctx && ctx->table && ctx->chain) {
3600 struct rhlist_head *list, *tmp;
3601
3602 list = rhltable_lookup(&table->chains_ht, ctx->chain,
3603 nft_chain_ht_params);
3604 if (!list)
3605 goto done;
3606
3607 rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
3608 if (!nft_is_active(net, chain))
3609 continue;
3610 __nf_tables_dump_rules(skb, &idx,
3611 cb, table, chain, reset);
3612 break;
3613 }
3614 goto done;
3615 }
3616
3617 list_for_each_entry_rcu(chain, &table->chains, list) {
3618 if (__nf_tables_dump_rules(skb, &idx,
3619 cb, table, chain, reset))
3620 goto done;
3621 }
3622
3623 if (ctx && ctx->table)
3624 break;
3625 }
3626 done:
3627 rcu_read_unlock();
3628
3629 cb->args[0] = idx;
3630 return skb->len;
3631 }
3632
nf_tables_dump_rules_start(struct netlink_callback * cb)3633 static int nf_tables_dump_rules_start(struct netlink_callback *cb)
3634 {
3635 const struct nlattr * const *nla = cb->data;
3636 struct nft_rule_dump_ctx *ctx = NULL;
3637
3638 if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) {
3639 ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
3640 if (!ctx)
3641 return -ENOMEM;
3642
3643 if (nla[NFTA_RULE_TABLE]) {
3644 ctx->table = nla_strdup(nla[NFTA_RULE_TABLE],
3645 GFP_ATOMIC);
3646 if (!ctx->table) {
3647 kfree(ctx);
3648 return -ENOMEM;
3649 }
3650 }
3651 if (nla[NFTA_RULE_CHAIN]) {
3652 ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN],
3653 GFP_ATOMIC);
3654 if (!ctx->chain) {
3655 kfree(ctx->table);
3656 kfree(ctx);
3657 return -ENOMEM;
3658 }
3659 }
3660 }
3661
3662 cb->data = ctx;
3663 return 0;
3664 }
3665
nf_tables_dump_rules_done(struct netlink_callback * cb)3666 static int nf_tables_dump_rules_done(struct netlink_callback *cb)
3667 {
3668 struct nft_rule_dump_ctx *ctx = cb->data;
3669
3670 if (ctx) {
3671 kfree(ctx->table);
3672 kfree(ctx->chain);
3673 kfree(ctx);
3674 }
3675 return 0;
3676 }
3677
3678 /* Caller must hold rcu read lock or transaction mutex */
3679 static struct sk_buff *
nf_tables_getrule_single(u32 portid,const struct nfnl_info * info,const struct nlattr * const nla[],bool reset)3680 nf_tables_getrule_single(u32 portid, const struct nfnl_info *info,
3681 const struct nlattr * const nla[], bool reset)
3682 {
3683 struct netlink_ext_ack *extack = info->extack;
3684 u8 genmask = nft_genmask_cur(info->net);
3685 u8 family = info->nfmsg->nfgen_family;
3686 const struct nft_chain *chain;
3687 const struct nft_rule *rule;
3688 struct net *net = info->net;
3689 struct nft_table *table;
3690 struct sk_buff *skb2;
3691 int err;
3692
3693 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, 0);
3694 if (IS_ERR(table)) {
3695 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
3696 return ERR_CAST(table);
3697 }
3698
3699 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask);
3700 if (IS_ERR(chain)) {
3701 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
3702 return ERR_CAST(chain);
3703 }
3704
3705 rule = nft_rule_lookup(net, chain, nla[NFTA_RULE_HANDLE]);
3706 if (IS_ERR(rule)) {
3707 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
3708 return ERR_CAST(rule);
3709 }
3710
3711 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
3712 if (!skb2)
3713 return ERR_PTR(-ENOMEM);
3714
3715 err = nf_tables_fill_rule_info(skb2, net, portid,
3716 info->nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
3717 family, table, chain, rule, 0, reset);
3718 if (err < 0) {
3719 kfree_skb(skb2);
3720 return ERR_PTR(err);
3721 }
3722
3723 return skb2;
3724 }
3725
nf_tables_getrule(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])3726 static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info,
3727 const struct nlattr * const nla[])
3728 {
3729 struct nftables_pernet *nft_net = nft_pernet(info->net);
3730 u32 portid = NETLINK_CB(skb).portid;
3731 struct net *net = info->net;
3732 struct sk_buff *skb2;
3733 bool reset = false;
3734 char *buf;
3735
3736 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
3737 struct netlink_dump_control c = {
3738 .start= nf_tables_dump_rules_start,
3739 .dump = nf_tables_dump_rules,
3740 .done = nf_tables_dump_rules_done,
3741 .module = THIS_MODULE,
3742 .data = (void *)nla,
3743 };
3744
3745 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
3746 }
3747
3748 if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET)
3749 reset = true;
3750
3751 skb2 = nf_tables_getrule_single(portid, info, nla, reset);
3752 if (IS_ERR(skb2))
3753 return PTR_ERR(skb2);
3754
3755 if (!reset)
3756 return nfnetlink_unicast(skb2, net, portid);
3757
3758 buf = kasprintf(GFP_ATOMIC, "%.*s:%u",
3759 nla_len(nla[NFTA_RULE_TABLE]),
3760 (char *)nla_data(nla[NFTA_RULE_TABLE]),
3761 nft_net->base_seq);
3762 audit_log_nfcfg(buf, info->nfmsg->nfgen_family, 1,
3763 AUDIT_NFT_OP_RULE_RESET, GFP_ATOMIC);
3764 kfree(buf);
3765
3766 return nfnetlink_unicast(skb2, net, portid);
3767 }
3768
nf_tables_rule_destroy(const struct nft_ctx * ctx,struct nft_rule * rule)3769 void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule)
3770 {
3771 struct nft_expr *expr, *next;
3772
3773 /*
3774 * Careful: some expressions might not be initialized in case this
3775 * is called on error from nf_tables_newrule().
3776 */
3777 expr = nft_expr_first(rule);
3778 while (nft_expr_more(rule, expr)) {
3779 next = nft_expr_next(expr);
3780 nf_tables_expr_destroy(ctx, expr);
3781 expr = next;
3782 }
3783 kfree(rule);
3784 }
3785
3786 /* can only be used if rule is no longer visible to dumps */
nf_tables_rule_release(const struct nft_ctx * ctx,struct nft_rule * rule)3787 static void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule)
3788 {
3789 lockdep_commit_lock_is_held(ctx->net);
3790
3791 nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE);
3792 nf_tables_rule_destroy(ctx, rule);
3793 }
3794
3795 /** nft_chain_validate - loop detection and hook validation
3796 *
3797 * @ctx: context containing call depth and base chain
3798 * @chain: chain to validate
3799 *
3800 * Walk through the rules of the given chain and chase all jumps/gotos
3801 * and set lookups until either the jump limit is hit or all reachable
3802 * chains have been validated.
3803 */
nft_chain_validate(const struct nft_ctx * ctx,const struct nft_chain * chain)3804 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain)
3805 {
3806 struct nft_expr *expr, *last;
3807 const struct nft_data *data;
3808 struct nft_rule *rule;
3809 int err;
3810
3811 if (ctx->level == NFT_JUMP_STACK_SIZE)
3812 return -EMLINK;
3813
3814 list_for_each_entry(rule, &chain->rules, list) {
3815 if (fatal_signal_pending(current))
3816 return -EINTR;
3817
3818 if (!nft_is_active_next(ctx->net, rule))
3819 continue;
3820
3821 nft_rule_for_each_expr(expr, last, rule) {
3822 if (!expr->ops->validate)
3823 continue;
3824
3825 /* This may call nft_chain_validate() recursively,
3826 * callers that do so must increment ctx->level.
3827 */
3828 err = expr->ops->validate(ctx, expr, &data);
3829 if (err < 0)
3830 return err;
3831 }
3832 }
3833
3834 return 0;
3835 }
3836 EXPORT_SYMBOL_GPL(nft_chain_validate);
3837
nft_table_validate(struct net * net,const struct nft_table * table)3838 static int nft_table_validate(struct net *net, const struct nft_table *table)
3839 {
3840 struct nft_chain *chain;
3841 struct nft_ctx ctx = {
3842 .net = net,
3843 .family = table->family,
3844 };
3845 int err;
3846
3847 list_for_each_entry(chain, &table->chains, list) {
3848 if (!nft_is_base_chain(chain))
3849 continue;
3850
3851 ctx.chain = chain;
3852 err = nft_chain_validate(&ctx, chain);
3853 if (err < 0)
3854 return err;
3855
3856 cond_resched();
3857 }
3858
3859 return 0;
3860 }
3861
nft_setelem_validate(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)3862 int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,
3863 const struct nft_set_iter *iter,
3864 struct nft_set_elem *elem)
3865 {
3866 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
3867 struct nft_ctx *pctx = (struct nft_ctx *)ctx;
3868 const struct nft_data *data;
3869 int err;
3870
3871 if (!nft_set_elem_active(ext, iter->genmask))
3872 return 0;
3873
3874 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
3875 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
3876 return 0;
3877
3878 data = nft_set_ext_data(ext);
3879 switch (data->verdict.code) {
3880 case NFT_JUMP:
3881 case NFT_GOTO:
3882 pctx->level++;
3883 err = nft_chain_validate(ctx, data->verdict.chain);
3884 if (err < 0)
3885 return err;
3886 pctx->level--;
3887 break;
3888 default:
3889 break;
3890 }
3891
3892 return 0;
3893 }
3894
nft_set_catchall_validate(const struct nft_ctx * ctx,struct nft_set * set)3895 int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set)
3896 {
3897 struct nft_set_iter dummy_iter = {
3898 .genmask = nft_genmask_next(ctx->net),
3899 };
3900 struct nft_set_elem_catchall *catchall;
3901 struct nft_set_elem elem;
3902
3903 struct nft_set_ext *ext;
3904 int ret = 0;
3905
3906 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
3907 ext = nft_set_elem_ext(set, catchall->elem);
3908 if (!nft_set_elem_active(ext, dummy_iter.genmask))
3909 continue;
3910
3911 elem.priv = catchall->elem;
3912 ret = nft_setelem_validate(ctx, set, &dummy_iter, &elem);
3913 if (ret < 0)
3914 return ret;
3915 }
3916
3917 return ret;
3918 }
3919
3920 static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
3921 const struct nft_chain *chain,
3922 const struct nlattr *nla);
3923
3924 #define NFT_RULE_MAXEXPRS 128
3925
nf_tables_newrule(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])3926 static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info,
3927 const struct nlattr * const nla[])
3928 {
3929 struct nftables_pernet *nft_net = nft_pernet(info->net);
3930 struct netlink_ext_ack *extack = info->extack;
3931 unsigned int size, i, n, ulen = 0, usize = 0;
3932 u8 genmask = nft_genmask_next(info->net);
3933 struct nft_rule *rule, *old_rule = NULL;
3934 struct nft_expr_info *expr_info = NULL;
3935 u8 family = info->nfmsg->nfgen_family;
3936 struct nft_flow_rule *flow = NULL;
3937 struct net *net = info->net;
3938 struct nft_userdata *udata;
3939 struct nft_table *table;
3940 struct nft_chain *chain;
3941 struct nft_trans *trans;
3942 u64 handle, pos_handle;
3943 struct nft_expr *expr;
3944 struct nft_ctx ctx;
3945 struct nlattr *tmp;
3946 int err, rem;
3947
3948 lockdep_assert_held(&nft_net->commit_mutex);
3949
3950 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask,
3951 NETLINK_CB(skb).portid);
3952 if (IS_ERR(table)) {
3953 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
3954 return PTR_ERR(table);
3955 }
3956
3957 if (nla[NFTA_RULE_CHAIN]) {
3958 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
3959 genmask);
3960 if (IS_ERR(chain)) {
3961 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
3962 return PTR_ERR(chain);
3963 }
3964
3965 } else if (nla[NFTA_RULE_CHAIN_ID]) {
3966 chain = nft_chain_lookup_byid(net, table, nla[NFTA_RULE_CHAIN_ID],
3967 genmask);
3968 if (IS_ERR(chain)) {
3969 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN_ID]);
3970 return PTR_ERR(chain);
3971 }
3972 } else {
3973 return -EINVAL;
3974 }
3975
3976 if (nft_chain_is_bound(chain))
3977 return -EOPNOTSUPP;
3978
3979 if (nla[NFTA_RULE_HANDLE]) {
3980 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
3981 rule = __nft_rule_lookup(net, chain, handle);
3982 if (IS_ERR(rule)) {
3983 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
3984 return PTR_ERR(rule);
3985 }
3986
3987 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
3988 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
3989 return -EEXIST;
3990 }
3991 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
3992 old_rule = rule;
3993 else
3994 return -EOPNOTSUPP;
3995 } else {
3996 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE) ||
3997 info->nlh->nlmsg_flags & NLM_F_REPLACE)
3998 return -EINVAL;
3999 handle = nf_tables_alloc_handle(table);
4000
4001 if (nla[NFTA_RULE_POSITION]) {
4002 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
4003 old_rule = __nft_rule_lookup(net, chain, pos_handle);
4004 if (IS_ERR(old_rule)) {
4005 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]);
4006 return PTR_ERR(old_rule);
4007 }
4008 } else if (nla[NFTA_RULE_POSITION_ID]) {
4009 old_rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_POSITION_ID]);
4010 if (IS_ERR(old_rule)) {
4011 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]);
4012 return PTR_ERR(old_rule);
4013 }
4014 }
4015 }
4016
4017 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
4018
4019 n = 0;
4020 size = 0;
4021 if (nla[NFTA_RULE_EXPRESSIONS]) {
4022 expr_info = kvmalloc_array(NFT_RULE_MAXEXPRS,
4023 sizeof(struct nft_expr_info),
4024 GFP_KERNEL);
4025 if (!expr_info)
4026 return -ENOMEM;
4027
4028 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
4029 err = -EINVAL;
4030 if (nla_type(tmp) != NFTA_LIST_ELEM)
4031 goto err_release_expr;
4032 if (n == NFT_RULE_MAXEXPRS)
4033 goto err_release_expr;
4034 err = nf_tables_expr_parse(&ctx, tmp, &expr_info[n]);
4035 if (err < 0) {
4036 NL_SET_BAD_ATTR(extack, tmp);
4037 goto err_release_expr;
4038 }
4039 size += expr_info[n].ops->size;
4040 n++;
4041 }
4042 }
4043 /* Check for overflow of dlen field */
4044 err = -EFBIG;
4045 if (size >= 1 << 12)
4046 goto err_release_expr;
4047
4048 if (nla[NFTA_RULE_USERDATA]) {
4049 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
4050 if (ulen > 0)
4051 usize = sizeof(struct nft_userdata) + ulen;
4052 }
4053
4054 err = -ENOMEM;
4055 rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL_ACCOUNT);
4056 if (rule == NULL)
4057 goto err_release_expr;
4058
4059 nft_activate_next(net, rule);
4060
4061 rule->handle = handle;
4062 rule->dlen = size;
4063 rule->udata = ulen ? 1 : 0;
4064
4065 if (ulen) {
4066 udata = nft_userdata(rule);
4067 udata->len = ulen - 1;
4068 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
4069 }
4070
4071 expr = nft_expr_first(rule);
4072 for (i = 0; i < n; i++) {
4073 err = nf_tables_newexpr(&ctx, &expr_info[i], expr);
4074 if (err < 0) {
4075 NL_SET_BAD_ATTR(extack, expr_info[i].attr);
4076 goto err_release_rule;
4077 }
4078
4079 if (expr_info[i].ops->validate)
4080 nft_validate_state_update(table, NFT_VALIDATE_NEED);
4081
4082 expr_info[i].ops = NULL;
4083 expr = nft_expr_next(expr);
4084 }
4085
4086 if (chain->flags & NFT_CHAIN_HW_OFFLOAD) {
4087 flow = nft_flow_rule_create(net, rule);
4088 if (IS_ERR(flow)) {
4089 err = PTR_ERR(flow);
4090 goto err_release_rule;
4091 }
4092 }
4093
4094 if (!nft_use_inc(&chain->use)) {
4095 err = -EMFILE;
4096 goto err_release_rule;
4097 }
4098
4099 if (info->nlh->nlmsg_flags & NLM_F_REPLACE) {
4100 if (nft_chain_binding(chain)) {
4101 err = -EOPNOTSUPP;
4102 goto err_destroy_flow_rule;
4103 }
4104
4105 err = nft_delrule(&ctx, old_rule);
4106 if (err < 0)
4107 goto err_destroy_flow_rule;
4108
4109 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
4110 if (trans == NULL) {
4111 err = -ENOMEM;
4112 goto err_destroy_flow_rule;
4113 }
4114 list_add_tail_rcu(&rule->list, &old_rule->list);
4115 } else {
4116 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
4117 if (!trans) {
4118 err = -ENOMEM;
4119 goto err_destroy_flow_rule;
4120 }
4121
4122 if (info->nlh->nlmsg_flags & NLM_F_APPEND) {
4123 if (old_rule)
4124 list_add_rcu(&rule->list, &old_rule->list);
4125 else
4126 list_add_tail_rcu(&rule->list, &chain->rules);
4127 } else {
4128 if (old_rule)
4129 list_add_tail_rcu(&rule->list, &old_rule->list);
4130 else
4131 list_add_rcu(&rule->list, &chain->rules);
4132 }
4133 }
4134 kvfree(expr_info);
4135
4136 if (flow)
4137 nft_trans_flow_rule(trans) = flow;
4138
4139 if (table->validate_state == NFT_VALIDATE_DO)
4140 return nft_table_validate(net, table);
4141
4142 return 0;
4143
4144 err_destroy_flow_rule:
4145 nft_use_dec_restore(&chain->use);
4146 if (flow)
4147 nft_flow_rule_destroy(flow);
4148 err_release_rule:
4149 nft_rule_expr_deactivate(&ctx, rule, NFT_TRANS_PREPARE_ERROR);
4150 nf_tables_rule_destroy(&ctx, rule);
4151 err_release_expr:
4152 for (i = 0; i < n; i++) {
4153 if (expr_info[i].ops) {
4154 module_put(expr_info[i].ops->type->owner);
4155 if (expr_info[i].ops->type->release_ops)
4156 expr_info[i].ops->type->release_ops(expr_info[i].ops);
4157 }
4158 }
4159 kvfree(expr_info);
4160
4161 return err;
4162 }
4163
nft_rule_lookup_byid(const struct net * net,const struct nft_chain * chain,const struct nlattr * nla)4164 static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
4165 const struct nft_chain *chain,
4166 const struct nlattr *nla)
4167 {
4168 struct nftables_pernet *nft_net = nft_pernet(net);
4169 u32 id = ntohl(nla_get_be32(nla));
4170 struct nft_trans *trans;
4171
4172 list_for_each_entry(trans, &nft_net->commit_list, list) {
4173 if (trans->msg_type == NFT_MSG_NEWRULE &&
4174 trans->ctx.chain == chain &&
4175 id == nft_trans_rule_id(trans))
4176 return nft_trans_rule(trans);
4177 }
4178 return ERR_PTR(-ENOENT);
4179 }
4180
nf_tables_delrule(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])4181 static int nf_tables_delrule(struct sk_buff *skb, const struct nfnl_info *info,
4182 const struct nlattr * const nla[])
4183 {
4184 struct netlink_ext_ack *extack = info->extack;
4185 u8 genmask = nft_genmask_next(info->net);
4186 u8 family = info->nfmsg->nfgen_family;
4187 struct nft_chain *chain = NULL;
4188 struct net *net = info->net;
4189 struct nft_table *table;
4190 struct nft_rule *rule;
4191 struct nft_ctx ctx;
4192 int err = 0;
4193
4194 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask,
4195 NETLINK_CB(skb).portid);
4196 if (IS_ERR(table)) {
4197 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
4198 return PTR_ERR(table);
4199 }
4200
4201 if (nla[NFTA_RULE_CHAIN]) {
4202 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
4203 genmask);
4204 if (IS_ERR(chain)) {
4205 if (PTR_ERR(chain) == -ENOENT &&
4206 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE)
4207 return 0;
4208
4209 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
4210 return PTR_ERR(chain);
4211 }
4212 if (nft_chain_binding(chain))
4213 return -EOPNOTSUPP;
4214 }
4215
4216 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla);
4217
4218 if (chain) {
4219 if (nla[NFTA_RULE_HANDLE]) {
4220 rule = nft_rule_lookup(info->net, chain, nla[NFTA_RULE_HANDLE]);
4221 if (IS_ERR(rule)) {
4222 if (PTR_ERR(rule) == -ENOENT &&
4223 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE)
4224 return 0;
4225
4226 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
4227 return PTR_ERR(rule);
4228 }
4229
4230 err = nft_delrule(&ctx, rule);
4231 } else if (nla[NFTA_RULE_ID]) {
4232 rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_ID]);
4233 if (IS_ERR(rule)) {
4234 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]);
4235 return PTR_ERR(rule);
4236 }
4237
4238 err = nft_delrule(&ctx, rule);
4239 } else {
4240 err = nft_delrule_by_chain(&ctx);
4241 }
4242 } else {
4243 list_for_each_entry(chain, &table->chains, list) {
4244 if (!nft_is_active_next(net, chain))
4245 continue;
4246 if (nft_chain_binding(chain))
4247 continue;
4248
4249 ctx.chain = chain;
4250 err = nft_delrule_by_chain(&ctx);
4251 if (err < 0)
4252 break;
4253 }
4254 }
4255
4256 return err;
4257 }
4258
4259 /*
4260 * Sets
4261 */
4262 static const struct nft_set_type *nft_set_types[] = {
4263 &nft_set_hash_fast_type,
4264 &nft_set_hash_type,
4265 &nft_set_rhash_type,
4266 &nft_set_bitmap_type,
4267 &nft_set_rbtree_type,
4268 #if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
4269 &nft_set_pipapo_avx2_type,
4270 #endif
4271 &nft_set_pipapo_type,
4272 };
4273
4274 #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \
4275 NFT_SET_TIMEOUT | NFT_SET_OBJECT | \
4276 NFT_SET_EVAL)
4277
nft_set_ops_candidate(const struct nft_set_type * type,u32 flags)4278 static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags)
4279 {
4280 return (flags & type->features) == (flags & NFT_SET_FEATURES);
4281 }
4282
4283 /*
4284 * Select a set implementation based on the data characteristics and the
4285 * given policy. The total memory use might not be known if no size is
4286 * given, in that case the amount of memory per element is used.
4287 */
4288 static const struct nft_set_ops *
nft_select_set_ops(const struct nft_ctx * ctx,const struct nlattr * const nla[],const struct nft_set_desc * desc)4289 nft_select_set_ops(const struct nft_ctx *ctx,
4290 const struct nlattr * const nla[],
4291 const struct nft_set_desc *desc)
4292 {
4293 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
4294 const struct nft_set_ops *ops, *bops;
4295 struct nft_set_estimate est, best;
4296 const struct nft_set_type *type;
4297 u32 flags = 0;
4298 int i;
4299
4300 lockdep_assert_held(&nft_net->commit_mutex);
4301 lockdep_nfnl_nft_mutex_not_held();
4302
4303 if (nla[NFTA_SET_FLAGS] != NULL)
4304 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
4305
4306 bops = NULL;
4307 best.size = ~0;
4308 best.lookup = ~0;
4309 best.space = ~0;
4310
4311 for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) {
4312 type = nft_set_types[i];
4313 ops = &type->ops;
4314
4315 if (!nft_set_ops_candidate(type, flags))
4316 continue;
4317 if (!ops->estimate(desc, flags, &est))
4318 continue;
4319
4320 switch (desc->policy) {
4321 case NFT_SET_POL_PERFORMANCE:
4322 if (est.lookup < best.lookup)
4323 break;
4324 if (est.lookup == best.lookup &&
4325 est.space < best.space)
4326 break;
4327 continue;
4328 case NFT_SET_POL_MEMORY:
4329 if (!desc->size) {
4330 if (est.space < best.space)
4331 break;
4332 if (est.space == best.space &&
4333 est.lookup < best.lookup)
4334 break;
4335 } else if (est.size < best.size || !bops) {
4336 break;
4337 }
4338 continue;
4339 default:
4340 break;
4341 }
4342
4343 bops = ops;
4344 best = est;
4345 }
4346
4347 if (bops != NULL)
4348 return bops;
4349
4350 return ERR_PTR(-EOPNOTSUPP);
4351 }
4352
4353 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
4354 [NFTA_SET_TABLE] = { .type = NLA_STRING,
4355 .len = NFT_TABLE_MAXNAMELEN - 1 },
4356 [NFTA_SET_NAME] = { .type = NLA_STRING,
4357 .len = NFT_SET_MAXNAMELEN - 1 },
4358 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
4359 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
4360 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
4361 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
4362 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
4363 [NFTA_SET_POLICY] = { .type = NLA_U32 },
4364 [NFTA_SET_DESC] = { .type = NLA_NESTED },
4365 [NFTA_SET_ID] = { .type = NLA_U32 },
4366 [NFTA_SET_TIMEOUT] = { .type = NLA_U64 },
4367 [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 },
4368 [NFTA_SET_USERDATA] = { .type = NLA_BINARY,
4369 .len = NFT_USERDATA_MAXLEN },
4370 [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 },
4371 [NFTA_SET_HANDLE] = { .type = NLA_U64 },
4372 [NFTA_SET_EXPR] = { .type = NLA_NESTED },
4373 [NFTA_SET_EXPRESSIONS] = { .type = NLA_NESTED },
4374 };
4375
4376 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
4377 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
4378 [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED },
4379 };
4380
nft_set_lookup(const struct nft_table * table,const struct nlattr * nla,u8 genmask)4381 static struct nft_set *nft_set_lookup(const struct nft_table *table,
4382 const struct nlattr *nla, u8 genmask)
4383 {
4384 struct nft_set *set;
4385
4386 if (nla == NULL)
4387 return ERR_PTR(-EINVAL);
4388
4389 list_for_each_entry_rcu(set, &table->sets, list) {
4390 if (!nla_strcmp(nla, set->name) &&
4391 nft_active_genmask(set, genmask))
4392 return set;
4393 }
4394 return ERR_PTR(-ENOENT);
4395 }
4396
nft_set_lookup_byhandle(const struct nft_table * table,const struct nlattr * nla,u8 genmask)4397 static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table,
4398 const struct nlattr *nla,
4399 u8 genmask)
4400 {
4401 struct nft_set *set;
4402
4403 list_for_each_entry(set, &table->sets, list) {
4404 if (be64_to_cpu(nla_get_be64(nla)) == set->handle &&
4405 nft_active_genmask(set, genmask))
4406 return set;
4407 }
4408 return ERR_PTR(-ENOENT);
4409 }
4410
nft_set_lookup_byid(const struct net * net,const struct nft_table * table,const struct nlattr * nla,u8 genmask)4411 static struct nft_set *nft_set_lookup_byid(const struct net *net,
4412 const struct nft_table *table,
4413 const struct nlattr *nla, u8 genmask)
4414 {
4415 struct nftables_pernet *nft_net = nft_pernet(net);
4416 u32 id = ntohl(nla_get_be32(nla));
4417 struct nft_trans *trans;
4418
4419 list_for_each_entry(trans, &nft_net->commit_list, list) {
4420 if (trans->msg_type == NFT_MSG_NEWSET) {
4421 struct nft_set *set = nft_trans_set(trans);
4422
4423 if (id == nft_trans_set_id(trans) &&
4424 set->table == table &&
4425 nft_active_genmask(set, genmask))
4426 return set;
4427 }
4428 }
4429 return ERR_PTR(-ENOENT);
4430 }
4431
nft_set_lookup_global(const struct net * net,const struct nft_table * table,const struct nlattr * nla_set_name,const struct nlattr * nla_set_id,u8 genmask)4432 struct nft_set *nft_set_lookup_global(const struct net *net,
4433 const struct nft_table *table,
4434 const struct nlattr *nla_set_name,
4435 const struct nlattr *nla_set_id,
4436 u8 genmask)
4437 {
4438 struct nft_set *set;
4439
4440 set = nft_set_lookup(table, nla_set_name, genmask);
4441 if (IS_ERR(set)) {
4442 if (!nla_set_id)
4443 return set;
4444
4445 set = nft_set_lookup_byid(net, table, nla_set_id, genmask);
4446 }
4447 return set;
4448 }
4449 EXPORT_SYMBOL_GPL(nft_set_lookup_global);
4450
nf_tables_set_alloc_name(struct nft_ctx * ctx,struct nft_set * set,const char * name)4451 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
4452 const char *name)
4453 {
4454 const struct nft_set *i;
4455 const char *p;
4456 unsigned long *inuse;
4457 unsigned int n = 0, min = 0;
4458
4459 p = strchr(name, '%');
4460 if (p != NULL) {
4461 if (p[1] != 'd' || strchr(p + 2, '%'))
4462 return -EINVAL;
4463
4464 if (strnlen(name, NFT_SET_MAX_ANONLEN) >= NFT_SET_MAX_ANONLEN)
4465 return -EINVAL;
4466
4467 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
4468 if (inuse == NULL)
4469 return -ENOMEM;
4470 cont:
4471 list_for_each_entry(i, &ctx->table->sets, list) {
4472 int tmp;
4473
4474 if (!nft_is_active_next(ctx->net, i))
4475 continue;
4476 if (!sscanf(i->name, name, &tmp))
4477 continue;
4478 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
4479 continue;
4480
4481 set_bit(tmp - min, inuse);
4482 }
4483
4484 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
4485 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
4486 min += BITS_PER_BYTE * PAGE_SIZE;
4487 memset(inuse, 0, PAGE_SIZE);
4488 goto cont;
4489 }
4490 free_page((unsigned long)inuse);
4491 }
4492
4493 set->name = kasprintf(GFP_KERNEL_ACCOUNT, name, min + n);
4494 if (!set->name)
4495 return -ENOMEM;
4496
4497 list_for_each_entry(i, &ctx->table->sets, list) {
4498 if (!nft_is_active_next(ctx->net, i))
4499 continue;
4500 if (!strcmp(set->name, i->name)) {
4501 kfree(set->name);
4502 set->name = NULL;
4503 return -ENFILE;
4504 }
4505 }
4506 return 0;
4507 }
4508
nf_msecs_to_jiffies64(const struct nlattr * nla,u64 * result)4509 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result)
4510 {
4511 u64 ms = be64_to_cpu(nla_get_be64(nla));
4512 u64 max = (u64)(~((u64)0));
4513
4514 max = div_u64(max, NSEC_PER_MSEC);
4515 if (ms >= max)
4516 return -ERANGE;
4517
4518 ms *= NSEC_PER_MSEC;
4519 *result = nsecs_to_jiffies64(ms) ? : !!ms;
4520 return 0;
4521 }
4522
nf_jiffies64_to_msecs(u64 input)4523 __be64 nf_jiffies64_to_msecs(u64 input)
4524 {
4525 return cpu_to_be64(jiffies64_to_msecs(input));
4526 }
4527
nf_tables_fill_set_concat(struct sk_buff * skb,const struct nft_set * set)4528 static int nf_tables_fill_set_concat(struct sk_buff *skb,
4529 const struct nft_set *set)
4530 {
4531 struct nlattr *concat, *field;
4532 int i;
4533
4534 concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT);
4535 if (!concat)
4536 return -ENOMEM;
4537
4538 for (i = 0; i < set->field_count; i++) {
4539 field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
4540 if (!field)
4541 return -ENOMEM;
4542
4543 if (nla_put_be32(skb, NFTA_SET_FIELD_LEN,
4544 htonl(set->field_len[i])))
4545 return -ENOMEM;
4546
4547 nla_nest_end(skb, field);
4548 }
4549
4550 nla_nest_end(skb, concat);
4551
4552 return 0;
4553 }
4554
nf_tables_fill_set(struct sk_buff * skb,const struct nft_ctx * ctx,const struct nft_set * set,u16 event,u16 flags)4555 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
4556 const struct nft_set *set, u16 event, u16 flags)
4557 {
4558 u64 timeout = READ_ONCE(set->timeout);
4559 u32 gc_int = READ_ONCE(set->gc_int);
4560 u32 portid = ctx->portid;
4561 struct nlmsghdr *nlh;
4562 struct nlattr *nest;
4563 u32 seq = ctx->seq;
4564 int i;
4565
4566 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
4567 nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family,
4568 NFNETLINK_V0, nft_base_seq(ctx->net));
4569 if (!nlh)
4570 goto nla_put_failure;
4571
4572 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
4573 goto nla_put_failure;
4574 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
4575 goto nla_put_failure;
4576 if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle),
4577 NFTA_SET_PAD))
4578 goto nla_put_failure;
4579
4580 if (event == NFT_MSG_DELSET) {
4581 nlmsg_end(skb, nlh);
4582 return 0;
4583 }
4584
4585 if (set->flags != 0)
4586 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
4587 goto nla_put_failure;
4588
4589 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
4590 goto nla_put_failure;
4591 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
4592 goto nla_put_failure;
4593 if (set->flags & NFT_SET_MAP) {
4594 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
4595 goto nla_put_failure;
4596 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
4597 goto nla_put_failure;
4598 }
4599 if (set->flags & NFT_SET_OBJECT &&
4600 nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype)))
4601 goto nla_put_failure;
4602
4603 if (timeout &&
4604 nla_put_be64(skb, NFTA_SET_TIMEOUT,
4605 nf_jiffies64_to_msecs(timeout),
4606 NFTA_SET_PAD))
4607 goto nla_put_failure;
4608 if (gc_int &&
4609 nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(gc_int)))
4610 goto nla_put_failure;
4611
4612 if (set->policy != NFT_SET_POL_PERFORMANCE) {
4613 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
4614 goto nla_put_failure;
4615 }
4616
4617 if (set->udata &&
4618 nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata))
4619 goto nla_put_failure;
4620
4621 nest = nla_nest_start_noflag(skb, NFTA_SET_DESC);
4622 if (!nest)
4623 goto nla_put_failure;
4624 if (set->size &&
4625 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
4626 goto nla_put_failure;
4627
4628 if (set->field_count > 1 &&
4629 nf_tables_fill_set_concat(skb, set))
4630 goto nla_put_failure;
4631
4632 nla_nest_end(skb, nest);
4633
4634 if (set->num_exprs == 1) {
4635 nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR);
4636 if (nf_tables_fill_expr_info(skb, set->exprs[0], false) < 0)
4637 goto nla_put_failure;
4638
4639 nla_nest_end(skb, nest);
4640 } else if (set->num_exprs > 1) {
4641 nest = nla_nest_start_noflag(skb, NFTA_SET_EXPRESSIONS);
4642 if (nest == NULL)
4643 goto nla_put_failure;
4644
4645 for (i = 0; i < set->num_exprs; i++) {
4646 if (nft_expr_dump(skb, NFTA_LIST_ELEM,
4647 set->exprs[i], false) < 0)
4648 goto nla_put_failure;
4649 }
4650 nla_nest_end(skb, nest);
4651 }
4652
4653 nlmsg_end(skb, nlh);
4654 return 0;
4655
4656 nla_put_failure:
4657 nlmsg_trim(skb, nlh);
4658 return -1;
4659 }
4660
nf_tables_set_notify(const struct nft_ctx * ctx,const struct nft_set * set,int event,gfp_t gfp_flags)4661 static void nf_tables_set_notify(const struct nft_ctx *ctx,
4662 const struct nft_set *set, int event,
4663 gfp_t gfp_flags)
4664 {
4665 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
4666 u32 portid = ctx->portid;
4667 struct sk_buff *skb;
4668 u16 flags = 0;
4669 int err;
4670
4671 if (!ctx->report &&
4672 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
4673 return;
4674
4675 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
4676 if (skb == NULL)
4677 goto err;
4678
4679 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
4680 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
4681
4682 err = nf_tables_fill_set(skb, ctx, set, event, flags);
4683 if (err < 0) {
4684 kfree_skb(skb);
4685 goto err;
4686 }
4687
4688 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
4689 return;
4690 err:
4691 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
4692 }
4693
nf_tables_dump_sets(struct sk_buff * skb,struct netlink_callback * cb)4694 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
4695 {
4696 const struct nft_set *set;
4697 unsigned int idx, s_idx = cb->args[0];
4698 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
4699 struct net *net = sock_net(skb->sk);
4700 struct nft_ctx *ctx = cb->data, ctx_set;
4701 struct nftables_pernet *nft_net;
4702
4703 if (cb->args[1])
4704 return skb->len;
4705
4706 rcu_read_lock();
4707 nft_net = nft_pernet(net);
4708 cb->seq = READ_ONCE(nft_net->base_seq);
4709
4710 list_for_each_entry_rcu(table, &nft_net->tables, list) {
4711 if (ctx->family != NFPROTO_UNSPEC &&
4712 ctx->family != table->family)
4713 continue;
4714
4715 if (ctx->table && ctx->table != table)
4716 continue;
4717
4718 if (cur_table) {
4719 if (cur_table != table)
4720 continue;
4721
4722 cur_table = NULL;
4723 }
4724 idx = 0;
4725 list_for_each_entry_rcu(set, &table->sets, list) {
4726 if (idx < s_idx)
4727 goto cont;
4728 if (!nft_is_active(net, set))
4729 goto cont;
4730
4731 ctx_set = *ctx;
4732 ctx_set.table = table;
4733 ctx_set.family = table->family;
4734
4735 if (nf_tables_fill_set(skb, &ctx_set, set,
4736 NFT_MSG_NEWSET,
4737 NLM_F_MULTI) < 0) {
4738 cb->args[0] = idx;
4739 cb->args[2] = (unsigned long) table;
4740 goto done;
4741 }
4742 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4743 cont:
4744 idx++;
4745 }
4746 if (s_idx)
4747 s_idx = 0;
4748 }
4749 cb->args[1] = 1;
4750 done:
4751 rcu_read_unlock();
4752 return skb->len;
4753 }
4754
nf_tables_dump_sets_start(struct netlink_callback * cb)4755 static int nf_tables_dump_sets_start(struct netlink_callback *cb)
4756 {
4757 struct nft_ctx *ctx_dump = NULL;
4758
4759 ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC);
4760 if (ctx_dump == NULL)
4761 return -ENOMEM;
4762
4763 cb->data = ctx_dump;
4764 return 0;
4765 }
4766
nf_tables_dump_sets_done(struct netlink_callback * cb)4767 static int nf_tables_dump_sets_done(struct netlink_callback *cb)
4768 {
4769 kfree(cb->data);
4770 return 0;
4771 }
4772
4773 /* called with rcu_read_lock held */
nf_tables_getset(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])4774 static int nf_tables_getset(struct sk_buff *skb, const struct nfnl_info *info,
4775 const struct nlattr * const nla[])
4776 {
4777 struct netlink_ext_ack *extack = info->extack;
4778 u8 genmask = nft_genmask_cur(info->net);
4779 u8 family = info->nfmsg->nfgen_family;
4780 struct nft_table *table = NULL;
4781 struct net *net = info->net;
4782 const struct nft_set *set;
4783 struct sk_buff *skb2;
4784 struct nft_ctx ctx;
4785 int err;
4786
4787 if (nla[NFTA_SET_TABLE]) {
4788 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family,
4789 genmask, 0);
4790 if (IS_ERR(table)) {
4791 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
4792 return PTR_ERR(table);
4793 }
4794 }
4795
4796 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
4797
4798 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
4799 struct netlink_dump_control c = {
4800 .start = nf_tables_dump_sets_start,
4801 .dump = nf_tables_dump_sets,
4802 .done = nf_tables_dump_sets_done,
4803 .data = &ctx,
4804 .module = THIS_MODULE,
4805 };
4806
4807 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
4808 }
4809
4810 /* Only accept unspec with dump */
4811 if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC)
4812 return -EAFNOSUPPORT;
4813 if (!nla[NFTA_SET_TABLE])
4814 return -EINVAL;
4815
4816 set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask);
4817 if (IS_ERR(set))
4818 return PTR_ERR(set);
4819
4820 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
4821 if (skb2 == NULL)
4822 return -ENOMEM;
4823
4824 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
4825 if (err < 0)
4826 goto err_fill_set_info;
4827
4828 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
4829
4830 err_fill_set_info:
4831 kfree_skb(skb2);
4832 return err;
4833 }
4834
4835 static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = {
4836 [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 },
4837 };
4838
nft_set_desc_concat_parse(const struct nlattr * attr,struct nft_set_desc * desc)4839 static int nft_set_desc_concat_parse(const struct nlattr *attr,
4840 struct nft_set_desc *desc)
4841 {
4842 struct nlattr *tb[NFTA_SET_FIELD_MAX + 1];
4843 u32 len;
4844 int err;
4845
4846 if (desc->field_count >= ARRAY_SIZE(desc->field_len))
4847 return -E2BIG;
4848
4849 err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr,
4850 nft_concat_policy, NULL);
4851 if (err < 0)
4852 return err;
4853
4854 if (!tb[NFTA_SET_FIELD_LEN])
4855 return -EINVAL;
4856
4857 len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN]));
4858 if (!len || len > U8_MAX)
4859 return -EINVAL;
4860
4861 desc->field_len[desc->field_count++] = len;
4862
4863 return 0;
4864 }
4865
nft_set_desc_concat(struct nft_set_desc * desc,const struct nlattr * nla)4866 static int nft_set_desc_concat(struct nft_set_desc *desc,
4867 const struct nlattr *nla)
4868 {
4869 u32 num_regs = 0, key_num_regs = 0;
4870 struct nlattr *attr;
4871 int rem, err, i;
4872
4873 nla_for_each_nested(attr, nla, rem) {
4874 if (nla_type(attr) != NFTA_LIST_ELEM)
4875 return -EINVAL;
4876
4877 err = nft_set_desc_concat_parse(attr, desc);
4878 if (err < 0)
4879 return err;
4880 }
4881
4882 for (i = 0; i < desc->field_count; i++)
4883 num_regs += DIV_ROUND_UP(desc->field_len[i], sizeof(u32));
4884
4885 key_num_regs = DIV_ROUND_UP(desc->klen, sizeof(u32));
4886 if (key_num_regs != num_regs)
4887 return -EINVAL;
4888
4889 if (num_regs > NFT_REG32_COUNT)
4890 return -E2BIG;
4891
4892 return 0;
4893 }
4894
nf_tables_set_desc_parse(struct nft_set_desc * desc,const struct nlattr * nla)4895 static int nf_tables_set_desc_parse(struct nft_set_desc *desc,
4896 const struct nlattr *nla)
4897 {
4898 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
4899 int err;
4900
4901 err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla,
4902 nft_set_desc_policy, NULL);
4903 if (err < 0)
4904 return err;
4905
4906 if (da[NFTA_SET_DESC_SIZE] != NULL)
4907 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
4908 if (da[NFTA_SET_DESC_CONCAT])
4909 err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]);
4910
4911 return err;
4912 }
4913
nft_set_expr_alloc(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * const * nla,struct nft_expr ** exprs,int * num_exprs,u32 flags)4914 static int nft_set_expr_alloc(struct nft_ctx *ctx, struct nft_set *set,
4915 const struct nlattr * const *nla,
4916 struct nft_expr **exprs, int *num_exprs,
4917 u32 flags)
4918 {
4919 struct nft_expr *expr;
4920 int err, i;
4921
4922 if (nla[NFTA_SET_EXPR]) {
4923 expr = nft_set_elem_expr_alloc(ctx, set, nla[NFTA_SET_EXPR]);
4924 if (IS_ERR(expr)) {
4925 err = PTR_ERR(expr);
4926 goto err_set_expr_alloc;
4927 }
4928 exprs[0] = expr;
4929 (*num_exprs)++;
4930 } else if (nla[NFTA_SET_EXPRESSIONS]) {
4931 struct nlattr *tmp;
4932 int left;
4933
4934 if (!(flags & NFT_SET_EXPR)) {
4935 err = -EINVAL;
4936 goto err_set_expr_alloc;
4937 }
4938 i = 0;
4939 nla_for_each_nested(tmp, nla[NFTA_SET_EXPRESSIONS], left) {
4940 if (i == NFT_SET_EXPR_MAX) {
4941 err = -E2BIG;
4942 goto err_set_expr_alloc;
4943 }
4944 if (nla_type(tmp) != NFTA_LIST_ELEM) {
4945 err = -EINVAL;
4946 goto err_set_expr_alloc;
4947 }
4948 expr = nft_set_elem_expr_alloc(ctx, set, tmp);
4949 if (IS_ERR(expr)) {
4950 err = PTR_ERR(expr);
4951 goto err_set_expr_alloc;
4952 }
4953 exprs[i++] = expr;
4954 (*num_exprs)++;
4955 }
4956 }
4957
4958 return 0;
4959
4960 err_set_expr_alloc:
4961 for (i = 0; i < *num_exprs; i++)
4962 nft_expr_destroy(ctx, exprs[i]);
4963
4964 return err;
4965 }
4966
nft_set_is_same(const struct nft_set * set,const struct nft_set_desc * desc,struct nft_expr * exprs[],u32 num_exprs,u32 flags)4967 static bool nft_set_is_same(const struct nft_set *set,
4968 const struct nft_set_desc *desc,
4969 struct nft_expr *exprs[], u32 num_exprs, u32 flags)
4970 {
4971 int i;
4972
4973 if (set->ktype != desc->ktype ||
4974 set->dtype != desc->dtype ||
4975 set->flags != flags ||
4976 set->klen != desc->klen ||
4977 set->dlen != desc->dlen ||
4978 set->field_count != desc->field_count ||
4979 set->num_exprs != num_exprs)
4980 return false;
4981
4982 for (i = 0; i < desc->field_count; i++) {
4983 if (set->field_len[i] != desc->field_len[i])
4984 return false;
4985 }
4986
4987 for (i = 0; i < num_exprs; i++) {
4988 if (set->exprs[i]->ops != exprs[i]->ops)
4989 return false;
4990 }
4991
4992 return true;
4993 }
4994
nf_tables_newset(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])4995 static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,
4996 const struct nlattr * const nla[])
4997 {
4998 struct netlink_ext_ack *extack = info->extack;
4999 u8 genmask = nft_genmask_next(info->net);
5000 u8 family = info->nfmsg->nfgen_family;
5001 const struct nft_set_ops *ops;
5002 struct net *net = info->net;
5003 struct nft_set_desc desc;
5004 struct nft_table *table;
5005 unsigned char *udata;
5006 struct nft_set *set;
5007 struct nft_ctx ctx;
5008 size_t alloc_size;
5009 int num_exprs = 0;
5010 char *name;
5011 int err, i;
5012 u16 udlen;
5013 u32 flags;
5014 u64 size;
5015
5016 if (nla[NFTA_SET_TABLE] == NULL ||
5017 nla[NFTA_SET_NAME] == NULL ||
5018 nla[NFTA_SET_KEY_LEN] == NULL ||
5019 nla[NFTA_SET_ID] == NULL)
5020 return -EINVAL;
5021
5022 memset(&desc, 0, sizeof(desc));
5023
5024 desc.ktype = NFT_DATA_VALUE;
5025 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
5026 desc.ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
5027 if ((desc.ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
5028 return -EINVAL;
5029 }
5030
5031 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
5032 if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
5033 return -EINVAL;
5034
5035 flags = 0;
5036 if (nla[NFTA_SET_FLAGS] != NULL) {
5037 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
5038 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
5039 NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
5040 NFT_SET_MAP | NFT_SET_EVAL |
5041 NFT_SET_OBJECT | NFT_SET_CONCAT | NFT_SET_EXPR))
5042 return -EOPNOTSUPP;
5043 /* Only one of these operations is supported */
5044 if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) ==
5045 (NFT_SET_MAP | NFT_SET_OBJECT))
5046 return -EOPNOTSUPP;
5047 if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) ==
5048 (NFT_SET_EVAL | NFT_SET_OBJECT))
5049 return -EOPNOTSUPP;
5050 if ((flags & (NFT_SET_ANONYMOUS | NFT_SET_TIMEOUT | NFT_SET_EVAL)) ==
5051 (NFT_SET_ANONYMOUS | NFT_SET_TIMEOUT))
5052 return -EOPNOTSUPP;
5053 if ((flags & (NFT_SET_CONSTANT | NFT_SET_TIMEOUT)) ==
5054 (NFT_SET_CONSTANT | NFT_SET_TIMEOUT))
5055 return -EOPNOTSUPP;
5056 }
5057
5058 desc.dtype = 0;
5059 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
5060 if (!(flags & NFT_SET_MAP))
5061 return -EINVAL;
5062
5063 desc.dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
5064 if ((desc.dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
5065 desc.dtype != NFT_DATA_VERDICT)
5066 return -EINVAL;
5067
5068 if (desc.dtype != NFT_DATA_VERDICT) {
5069 if (nla[NFTA_SET_DATA_LEN] == NULL)
5070 return -EINVAL;
5071 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
5072 if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
5073 return -EINVAL;
5074 } else
5075 desc.dlen = sizeof(struct nft_verdict);
5076 } else if (flags & NFT_SET_MAP)
5077 return -EINVAL;
5078
5079 if (nla[NFTA_SET_OBJ_TYPE] != NULL) {
5080 if (!(flags & NFT_SET_OBJECT))
5081 return -EINVAL;
5082
5083 desc.objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE]));
5084 if (desc.objtype == NFT_OBJECT_UNSPEC ||
5085 desc.objtype > NFT_OBJECT_MAX)
5086 return -EOPNOTSUPP;
5087 } else if (flags & NFT_SET_OBJECT)
5088 return -EINVAL;
5089 else
5090 desc.objtype = NFT_OBJECT_UNSPEC;
5091
5092 desc.timeout = 0;
5093 if (nla[NFTA_SET_TIMEOUT] != NULL) {
5094 if (!(flags & NFT_SET_TIMEOUT))
5095 return -EINVAL;
5096
5097 if (flags & NFT_SET_ANONYMOUS)
5098 return -EOPNOTSUPP;
5099
5100 err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &desc.timeout);
5101 if (err)
5102 return err;
5103 }
5104 desc.gc_int = 0;
5105 if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
5106 if (!(flags & NFT_SET_TIMEOUT))
5107 return -EINVAL;
5108
5109 if (flags & NFT_SET_ANONYMOUS)
5110 return -EOPNOTSUPP;
5111
5112 desc.gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
5113 }
5114
5115 desc.policy = NFT_SET_POL_PERFORMANCE;
5116 if (nla[NFTA_SET_POLICY] != NULL) {
5117 desc.policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
5118 switch (desc.policy) {
5119 case NFT_SET_POL_PERFORMANCE:
5120 case NFT_SET_POL_MEMORY:
5121 break;
5122 default:
5123 return -EOPNOTSUPP;
5124 }
5125 }
5126
5127 if (nla[NFTA_SET_DESC] != NULL) {
5128 err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]);
5129 if (err < 0)
5130 return err;
5131
5132 if (desc.field_count > 1) {
5133 if (!(flags & NFT_SET_CONCAT))
5134 return -EINVAL;
5135 } else if (flags & NFT_SET_CONCAT) {
5136 return -EINVAL;
5137 }
5138 } else if (flags & NFT_SET_CONCAT) {
5139 return -EINVAL;
5140 }
5141
5142 if (nla[NFTA_SET_EXPR] || nla[NFTA_SET_EXPRESSIONS])
5143 desc.expr = true;
5144
5145 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask,
5146 NETLINK_CB(skb).portid);
5147 if (IS_ERR(table)) {
5148 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
5149 return PTR_ERR(table);
5150 }
5151
5152 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
5153
5154 set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask);
5155 if (IS_ERR(set)) {
5156 if (PTR_ERR(set) != -ENOENT) {
5157 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
5158 return PTR_ERR(set);
5159 }
5160 } else {
5161 struct nft_expr *exprs[NFT_SET_EXPR_MAX] = {};
5162
5163 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
5164 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
5165 return -EEXIST;
5166 }
5167 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
5168 return -EOPNOTSUPP;
5169
5170 if (nft_set_is_anonymous(set))
5171 return -EOPNOTSUPP;
5172
5173 err = nft_set_expr_alloc(&ctx, set, nla, exprs, &num_exprs, flags);
5174 if (err < 0)
5175 return err;
5176
5177 err = 0;
5178 if (!nft_set_is_same(set, &desc, exprs, num_exprs, flags)) {
5179 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
5180 err = -EEXIST;
5181 }
5182
5183 for (i = 0; i < num_exprs; i++)
5184 nft_expr_destroy(&ctx, exprs[i]);
5185
5186 if (err < 0)
5187 return err;
5188
5189 return __nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set, &desc);
5190 }
5191
5192 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
5193 return -ENOENT;
5194
5195 ops = nft_select_set_ops(&ctx, nla, &desc);
5196 if (IS_ERR(ops))
5197 return PTR_ERR(ops);
5198
5199 udlen = 0;
5200 if (nla[NFTA_SET_USERDATA])
5201 udlen = nla_len(nla[NFTA_SET_USERDATA]);
5202
5203 size = 0;
5204 if (ops->privsize != NULL)
5205 size = ops->privsize(nla, &desc);
5206 alloc_size = sizeof(*set) + size + udlen;
5207 if (alloc_size < size || alloc_size > INT_MAX)
5208 return -ENOMEM;
5209
5210 if (!nft_use_inc(&table->use))
5211 return -EMFILE;
5212
5213 set = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT);
5214 if (!set) {
5215 err = -ENOMEM;
5216 goto err_alloc;
5217 }
5218
5219 name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL_ACCOUNT);
5220 if (!name) {
5221 err = -ENOMEM;
5222 goto err_set_name;
5223 }
5224
5225 err = nf_tables_set_alloc_name(&ctx, set, name);
5226 kfree(name);
5227 if (err < 0)
5228 goto err_set_name;
5229
5230 udata = NULL;
5231 if (udlen) {
5232 udata = set->data + size;
5233 nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen);
5234 }
5235
5236 INIT_LIST_HEAD(&set->bindings);
5237 INIT_LIST_HEAD(&set->catchall_list);
5238 refcount_set(&set->refs, 1);
5239 set->table = table;
5240 write_pnet(&set->net, net);
5241 set->ops = ops;
5242 set->ktype = desc.ktype;
5243 set->klen = desc.klen;
5244 set->dtype = desc.dtype;
5245 set->objtype = desc.objtype;
5246 set->dlen = desc.dlen;
5247 set->flags = flags;
5248 set->size = desc.size;
5249 set->policy = desc.policy;
5250 set->udlen = udlen;
5251 set->udata = udata;
5252 set->timeout = desc.timeout;
5253 set->gc_int = desc.gc_int;
5254
5255 set->field_count = desc.field_count;
5256 for (i = 0; i < desc.field_count; i++)
5257 set->field_len[i] = desc.field_len[i];
5258
5259 err = ops->init(set, &desc, nla);
5260 if (err < 0)
5261 goto err_set_init;
5262
5263 err = nft_set_expr_alloc(&ctx, set, nla, set->exprs, &num_exprs, flags);
5264 if (err < 0)
5265 goto err_set_destroy;
5266
5267 set->num_exprs = num_exprs;
5268 set->handle = nf_tables_alloc_handle(table);
5269 INIT_LIST_HEAD(&set->pending_update);
5270
5271 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
5272 if (err < 0)
5273 goto err_set_expr_alloc;
5274
5275 list_add_tail_rcu(&set->list, &table->sets);
5276
5277 return 0;
5278
5279 err_set_expr_alloc:
5280 for (i = 0; i < set->num_exprs; i++)
5281 nft_expr_destroy(&ctx, set->exprs[i]);
5282 err_set_destroy:
5283 ops->destroy(&ctx, set);
5284 err_set_init:
5285 kfree(set->name);
5286 err_set_name:
5287 kvfree(set);
5288 err_alloc:
5289 nft_use_dec_restore(&table->use);
5290
5291 return err;
5292 }
5293
nft_set_catchall_destroy(const struct nft_ctx * ctx,struct nft_set * set)5294 static void nft_set_catchall_destroy(const struct nft_ctx *ctx,
5295 struct nft_set *set)
5296 {
5297 struct nft_set_elem_catchall *next, *catchall;
5298
5299 list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
5300 list_del_rcu(&catchall->list);
5301 nf_tables_set_elem_destroy(ctx, set, catchall->elem);
5302 kfree_rcu(catchall, rcu);
5303 }
5304 }
5305
nft_set_put(struct nft_set * set)5306 static void nft_set_put(struct nft_set *set)
5307 {
5308 if (refcount_dec_and_test(&set->refs)) {
5309 kfree(set->name);
5310 kvfree(set);
5311 }
5312 }
5313
nft_set_destroy(const struct nft_ctx * ctx,struct nft_set * set)5314 static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
5315 {
5316 int i;
5317
5318 if (WARN_ON(set->use > 0))
5319 return;
5320
5321 for (i = 0; i < set->num_exprs; i++)
5322 nft_expr_destroy(ctx, set->exprs[i]);
5323
5324 set->ops->destroy(ctx, set);
5325 nft_set_catchall_destroy(ctx, set);
5326 nft_set_put(set);
5327 }
5328
nf_tables_delset(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])5329 static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info,
5330 const struct nlattr * const nla[])
5331 {
5332 struct netlink_ext_ack *extack = info->extack;
5333 u8 genmask = nft_genmask_next(info->net);
5334 u8 family = info->nfmsg->nfgen_family;
5335 struct net *net = info->net;
5336 const struct nlattr *attr;
5337 struct nft_table *table;
5338 struct nft_set *set;
5339 struct nft_ctx ctx;
5340
5341 if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC)
5342 return -EAFNOSUPPORT;
5343
5344 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family,
5345 genmask, NETLINK_CB(skb).portid);
5346 if (IS_ERR(table)) {
5347 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
5348 return PTR_ERR(table);
5349 }
5350
5351 if (nla[NFTA_SET_HANDLE]) {
5352 attr = nla[NFTA_SET_HANDLE];
5353 set = nft_set_lookup_byhandle(table, attr, genmask);
5354 } else {
5355 attr = nla[NFTA_SET_NAME];
5356 set = nft_set_lookup(table, attr, genmask);
5357 }
5358
5359 if (IS_ERR(set)) {
5360 if (PTR_ERR(set) == -ENOENT &&
5361 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSET)
5362 return 0;
5363
5364 NL_SET_BAD_ATTR(extack, attr);
5365 return PTR_ERR(set);
5366 }
5367 if (set->use ||
5368 (info->nlh->nlmsg_flags & NLM_F_NONREC &&
5369 atomic_read(&set->nelems) > 0)) {
5370 NL_SET_BAD_ATTR(extack, attr);
5371 return -EBUSY;
5372 }
5373
5374 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
5375
5376 return nft_delset(&ctx, set);
5377 }
5378
5379 static int nft_validate_register_store(const struct nft_ctx *ctx,
5380 enum nft_registers reg,
5381 const struct nft_data *data,
5382 enum nft_data_types type,
5383 unsigned int len);
5384
nft_setelem_data_validate(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_elem * elem)5385 static int nft_setelem_data_validate(const struct nft_ctx *ctx,
5386 struct nft_set *set,
5387 struct nft_set_elem *elem)
5388 {
5389 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5390 enum nft_registers dreg;
5391
5392 dreg = nft_type_to_reg(set->dtype);
5393 return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
5394 set->dtype == NFT_DATA_VERDICT ?
5395 NFT_DATA_VERDICT : NFT_DATA_VALUE,
5396 set->dlen);
5397 }
5398
nf_tables_bind_check_setelem(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)5399 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
5400 struct nft_set *set,
5401 const struct nft_set_iter *iter,
5402 struct nft_set_elem *elem)
5403 {
5404 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5405
5406 if (!nft_set_elem_active(ext, iter->genmask))
5407 return 0;
5408
5409 return nft_setelem_data_validate(ctx, set, elem);
5410 }
5411
nft_set_catchall_bind_check(const struct nft_ctx * ctx,struct nft_set * set)5412 static int nft_set_catchall_bind_check(const struct nft_ctx *ctx,
5413 struct nft_set *set)
5414 {
5415 u8 genmask = nft_genmask_next(ctx->net);
5416 struct nft_set_elem_catchall *catchall;
5417 struct nft_set_elem elem;
5418 struct nft_set_ext *ext;
5419 int ret = 0;
5420
5421 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
5422 ext = nft_set_elem_ext(set, catchall->elem);
5423 if (!nft_set_elem_active(ext, genmask))
5424 continue;
5425
5426 elem.priv = catchall->elem;
5427 ret = nft_setelem_data_validate(ctx, set, &elem);
5428 if (ret < 0)
5429 break;
5430 }
5431
5432 return ret;
5433 }
5434
nf_tables_bind_set(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_binding * binding)5435 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
5436 struct nft_set_binding *binding)
5437 {
5438 struct nft_set_binding *i;
5439 struct nft_set_iter iter;
5440
5441 if (!list_empty(&set->bindings) && nft_set_is_anonymous(set))
5442 return -EBUSY;
5443
5444 if (binding->flags & NFT_SET_MAP) {
5445 /* If the set is already bound to the same chain all
5446 * jumps are already validated for that chain.
5447 */
5448 list_for_each_entry(i, &set->bindings, list) {
5449 if (i->flags & NFT_SET_MAP &&
5450 i->chain == binding->chain)
5451 goto bind;
5452 }
5453
5454 iter.genmask = nft_genmask_next(ctx->net);
5455 iter.type = NFT_ITER_UPDATE;
5456 iter.skip = 0;
5457 iter.count = 0;
5458 iter.err = 0;
5459 iter.fn = nf_tables_bind_check_setelem;
5460
5461 set->ops->walk(ctx, set, &iter);
5462 if (!iter.err)
5463 iter.err = nft_set_catchall_bind_check(ctx, set);
5464
5465 if (iter.err < 0)
5466 return iter.err;
5467 }
5468 bind:
5469 if (!nft_use_inc(&set->use))
5470 return -EMFILE;
5471
5472 binding->chain = ctx->chain;
5473 list_add_tail_rcu(&binding->list, &set->bindings);
5474 nft_set_trans_bind(ctx, set);
5475
5476 return 0;
5477 }
5478 EXPORT_SYMBOL_GPL(nf_tables_bind_set);
5479
nf_tables_unbind_set(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_binding * binding,bool event)5480 static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
5481 struct nft_set_binding *binding, bool event)
5482 {
5483 list_del_rcu(&binding->list);
5484
5485 if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) {
5486 list_del_rcu(&set->list);
5487 set->dead = 1;
5488 if (event)
5489 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET,
5490 GFP_KERNEL);
5491 }
5492 }
5493
5494 static void nft_setelem_data_activate(const struct net *net,
5495 const struct nft_set *set,
5496 struct nft_set_elem *elem);
5497
nft_mapelem_activate(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)5498 static int nft_mapelem_activate(const struct nft_ctx *ctx,
5499 struct nft_set *set,
5500 const struct nft_set_iter *iter,
5501 struct nft_set_elem *elem)
5502 {
5503 struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5504
5505 /* called from abort path, reverse check to undo changes. */
5506 if (nft_set_elem_active(ext, iter->genmask))
5507 return 0;
5508
5509 nft_clear(ctx->net, ext);
5510 nft_setelem_data_activate(ctx->net, set, elem);
5511
5512 return 0;
5513 }
5514
nft_map_catchall_activate(const struct nft_ctx * ctx,struct nft_set * set)5515 static void nft_map_catchall_activate(const struct nft_ctx *ctx,
5516 struct nft_set *set)
5517 {
5518 u8 genmask = nft_genmask_next(ctx->net);
5519 struct nft_set_elem_catchall *catchall;
5520 struct nft_set_elem elem;
5521 struct nft_set_ext *ext;
5522
5523 list_for_each_entry(catchall, &set->catchall_list, list) {
5524 ext = nft_set_elem_ext(set, catchall->elem);
5525 if (!nft_set_elem_active(ext, genmask))
5526 continue;
5527
5528 nft_clear(ctx->net, ext);
5529 elem.priv = catchall->elem;
5530 nft_setelem_data_activate(ctx->net, set, &elem);
5531 break;
5532 }
5533 }
5534
nft_map_activate(const struct nft_ctx * ctx,struct nft_set * set)5535 static void nft_map_activate(const struct nft_ctx *ctx, struct nft_set *set)
5536 {
5537 struct nft_set_iter iter = {
5538 .genmask = nft_genmask_next(ctx->net),
5539 .type = NFT_ITER_UPDATE,
5540 .fn = nft_mapelem_activate,
5541 };
5542
5543 set->ops->walk(ctx, set, &iter);
5544 WARN_ON_ONCE(iter.err);
5545
5546 nft_map_catchall_activate(ctx, set);
5547 }
5548
nf_tables_activate_set(const struct nft_ctx * ctx,struct nft_set * set)5549 void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set)
5550 {
5551 if (nft_set_is_anonymous(set)) {
5552 if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
5553 nft_map_activate(ctx, set);
5554
5555 nft_clear(ctx->net, set);
5556 }
5557
5558 nft_use_inc_restore(&set->use);
5559 }
5560 EXPORT_SYMBOL_GPL(nf_tables_activate_set);
5561
nf_tables_deactivate_set(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_binding * binding,enum nft_trans_phase phase)5562 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
5563 struct nft_set_binding *binding,
5564 enum nft_trans_phase phase)
5565 {
5566 lockdep_commit_lock_is_held(ctx->net);
5567
5568 switch (phase) {
5569 case NFT_TRANS_PREPARE_ERROR:
5570 nft_set_trans_unbind(ctx, set);
5571 if (nft_set_is_anonymous(set))
5572 nft_deactivate_next(ctx->net, set);
5573 else
5574 list_del_rcu(&binding->list);
5575
5576 nft_use_dec(&set->use);
5577 break;
5578 case NFT_TRANS_PREPARE:
5579 if (nft_set_is_anonymous(set)) {
5580 if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
5581 nft_map_deactivate(ctx, set);
5582
5583 nft_deactivate_next(ctx->net, set);
5584 }
5585 nft_use_dec(&set->use);
5586 return;
5587 case NFT_TRANS_ABORT:
5588 case NFT_TRANS_RELEASE:
5589 if (nft_set_is_anonymous(set) &&
5590 set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
5591 nft_map_deactivate(ctx, set);
5592
5593 nft_use_dec(&set->use);
5594 fallthrough;
5595 default:
5596 nf_tables_unbind_set(ctx, set, binding,
5597 phase == NFT_TRANS_COMMIT);
5598 }
5599 }
5600 EXPORT_SYMBOL_GPL(nf_tables_deactivate_set);
5601
nf_tables_destroy_set(const struct nft_ctx * ctx,struct nft_set * set)5602 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set)
5603 {
5604 if (list_empty(&set->bindings) && nft_set_is_anonymous(set))
5605 nft_set_destroy(ctx, set);
5606 }
5607 EXPORT_SYMBOL_GPL(nf_tables_destroy_set);
5608
5609 const struct nft_set_ext_type nft_set_ext_types[] = {
5610 [NFT_SET_EXT_KEY] = {
5611 .align = __alignof__(u32),
5612 },
5613 [NFT_SET_EXT_DATA] = {
5614 .align = __alignof__(u32),
5615 },
5616 [NFT_SET_EXT_EXPRESSIONS] = {
5617 .align = __alignof__(struct nft_set_elem_expr),
5618 },
5619 [NFT_SET_EXT_OBJREF] = {
5620 .len = sizeof(struct nft_object *),
5621 .align = __alignof__(struct nft_object *),
5622 },
5623 [NFT_SET_EXT_FLAGS] = {
5624 .len = sizeof(u8),
5625 .align = __alignof__(u8),
5626 },
5627 [NFT_SET_EXT_TIMEOUT] = {
5628 .len = sizeof(u64),
5629 .align = __alignof__(u64),
5630 },
5631 [NFT_SET_EXT_EXPIRATION] = {
5632 .len = sizeof(u64),
5633 .align = __alignof__(u64),
5634 },
5635 [NFT_SET_EXT_USERDATA] = {
5636 .len = sizeof(struct nft_userdata),
5637 .align = __alignof__(struct nft_userdata),
5638 },
5639 [NFT_SET_EXT_KEY_END] = {
5640 .align = __alignof__(u32),
5641 },
5642 };
5643
5644 /*
5645 * Set elements
5646 */
5647
5648 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
5649 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
5650 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
5651 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
5652 [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 },
5653 [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 },
5654 [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY,
5655 .len = NFT_USERDATA_MAXLEN },
5656 [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED },
5657 [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING,
5658 .len = NFT_OBJ_MAXNAMELEN - 1 },
5659 [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED },
5660 [NFTA_SET_ELEM_EXPRESSIONS] = { .type = NLA_NESTED },
5661 };
5662
5663 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
5664 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING,
5665 .len = NFT_TABLE_MAXNAMELEN - 1 },
5666 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING,
5667 .len = NFT_SET_MAXNAMELEN - 1 },
5668 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
5669 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
5670 };
5671
nft_set_elem_expr_dump(struct sk_buff * skb,const struct nft_set * set,const struct nft_set_ext * ext,bool reset)5672 static int nft_set_elem_expr_dump(struct sk_buff *skb,
5673 const struct nft_set *set,
5674 const struct nft_set_ext *ext,
5675 bool reset)
5676 {
5677 struct nft_set_elem_expr *elem_expr;
5678 u32 size, num_exprs = 0;
5679 struct nft_expr *expr;
5680 struct nlattr *nest;
5681
5682 elem_expr = nft_set_ext_expr(ext);
5683 nft_setelem_expr_foreach(expr, elem_expr, size)
5684 num_exprs++;
5685
5686 if (num_exprs == 1) {
5687 expr = nft_setelem_expr_at(elem_expr, 0);
5688 if (nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, expr, reset) < 0)
5689 return -1;
5690
5691 return 0;
5692 } else if (num_exprs > 1) {
5693 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_EXPRESSIONS);
5694 if (nest == NULL)
5695 goto nla_put_failure;
5696
5697 nft_setelem_expr_foreach(expr, elem_expr, size) {
5698 expr = nft_setelem_expr_at(elem_expr, size);
5699 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, reset) < 0)
5700 goto nla_put_failure;
5701 }
5702 nla_nest_end(skb, nest);
5703 }
5704 return 0;
5705
5706 nla_put_failure:
5707 return -1;
5708 }
5709
nf_tables_fill_setelem(struct sk_buff * skb,const struct nft_set * set,const struct nft_set_elem * elem,bool reset)5710 static int nf_tables_fill_setelem(struct sk_buff *skb,
5711 const struct nft_set *set,
5712 const struct nft_set_elem *elem,
5713 bool reset)
5714 {
5715 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5716 unsigned char *b = skb_tail_pointer(skb);
5717 struct nlattr *nest;
5718
5719 nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
5720 if (nest == NULL)
5721 goto nla_put_failure;
5722
5723 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) &&
5724 nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
5725 NFT_DATA_VALUE, set->klen) < 0)
5726 goto nla_put_failure;
5727
5728 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) &&
5729 nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext),
5730 NFT_DATA_VALUE, set->klen) < 0)
5731 goto nla_put_failure;
5732
5733 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
5734 nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
5735 nft_set_datatype(set), set->dlen) < 0)
5736 goto nla_put_failure;
5737
5738 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS) &&
5739 nft_set_elem_expr_dump(skb, set, ext, reset))
5740 goto nla_put_failure;
5741
5742 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
5743 nla_put_string(skb, NFTA_SET_ELEM_OBJREF,
5744 (*nft_set_ext_obj(ext))->key.name) < 0)
5745 goto nla_put_failure;
5746
5747 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
5748 nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
5749 htonl(*nft_set_ext_flags(ext))))
5750 goto nla_put_failure;
5751
5752 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
5753 nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
5754 nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)),
5755 NFTA_SET_ELEM_PAD))
5756 goto nla_put_failure;
5757
5758 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
5759 u64 expires, now = get_jiffies_64();
5760
5761 expires = *nft_set_ext_expiration(ext);
5762 if (time_before64(now, expires))
5763 expires -= now;
5764 else
5765 expires = 0;
5766
5767 if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
5768 nf_jiffies64_to_msecs(expires),
5769 NFTA_SET_ELEM_PAD))
5770 goto nla_put_failure;
5771 }
5772
5773 if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
5774 struct nft_userdata *udata;
5775
5776 udata = nft_set_ext_userdata(ext);
5777 if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
5778 udata->len + 1, udata->data))
5779 goto nla_put_failure;
5780 }
5781
5782 nla_nest_end(skb, nest);
5783 return 0;
5784
5785 nla_put_failure:
5786 nlmsg_trim(skb, b);
5787 return -EMSGSIZE;
5788 }
5789
5790 struct nft_set_dump_args {
5791 const struct netlink_callback *cb;
5792 struct nft_set_iter iter;
5793 struct sk_buff *skb;
5794 bool reset;
5795 };
5796
nf_tables_dump_setelem(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)5797 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
5798 struct nft_set *set,
5799 const struct nft_set_iter *iter,
5800 struct nft_set_elem *elem)
5801 {
5802 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5803 struct nft_set_dump_args *args;
5804
5805 if (!nft_set_elem_active(ext, iter->genmask))
5806 return 0;
5807
5808 if (nft_set_elem_expired(ext) || nft_set_elem_is_dead(ext))
5809 return 0;
5810
5811 args = container_of(iter, struct nft_set_dump_args, iter);
5812 return nf_tables_fill_setelem(args->skb, set, elem, args->reset);
5813 }
5814
audit_log_nft_set_reset(const struct nft_table * table,unsigned int base_seq,unsigned int nentries)5815 static void audit_log_nft_set_reset(const struct nft_table *table,
5816 unsigned int base_seq,
5817 unsigned int nentries)
5818 {
5819 char *buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, base_seq);
5820
5821 audit_log_nfcfg(buf, table->family, nentries,
5822 AUDIT_NFT_OP_SETELEM_RESET, GFP_ATOMIC);
5823 kfree(buf);
5824 }
5825
5826 struct nft_set_dump_ctx {
5827 const struct nft_set *set;
5828 struct nft_ctx ctx;
5829 };
5830
nft_set_catchall_dump(struct net * net,struct sk_buff * skb,const struct nft_set * set,bool reset,unsigned int base_seq)5831 static int nft_set_catchall_dump(struct net *net, struct sk_buff *skb,
5832 const struct nft_set *set, bool reset,
5833 unsigned int base_seq)
5834 {
5835 struct nft_set_elem_catchall *catchall;
5836 u8 genmask = nft_genmask_cur(net);
5837 struct nft_set_elem elem;
5838 struct nft_set_ext *ext;
5839 int ret = 0;
5840
5841 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
5842 ext = nft_set_elem_ext(set, catchall->elem);
5843 if (!nft_set_elem_active(ext, genmask) ||
5844 nft_set_elem_expired(ext))
5845 continue;
5846
5847 elem.priv = catchall->elem;
5848 ret = nf_tables_fill_setelem(skb, set, &elem, reset);
5849 if (reset && !ret)
5850 audit_log_nft_set_reset(set->table, base_seq, 1);
5851 break;
5852 }
5853
5854 return ret;
5855 }
5856
nf_tables_dump_set(struct sk_buff * skb,struct netlink_callback * cb)5857 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
5858 {
5859 struct nft_set_dump_ctx *dump_ctx = cb->data;
5860 struct net *net = sock_net(skb->sk);
5861 struct nftables_pernet *nft_net;
5862 struct nft_table *table;
5863 struct nft_set *set;
5864 struct nft_set_dump_args args;
5865 bool set_found = false;
5866 struct nlmsghdr *nlh;
5867 struct nlattr *nest;
5868 bool reset = false;
5869 u32 portid, seq;
5870 int event;
5871
5872 rcu_read_lock();
5873 nft_net = nft_pernet(net);
5874 cb->seq = READ_ONCE(nft_net->base_seq);
5875
5876 list_for_each_entry_rcu(table, &nft_net->tables, list) {
5877 if (dump_ctx->ctx.family != NFPROTO_UNSPEC &&
5878 dump_ctx->ctx.family != table->family)
5879 continue;
5880
5881 if (table != dump_ctx->ctx.table)
5882 continue;
5883
5884 list_for_each_entry_rcu(set, &table->sets, list) {
5885 if (set == dump_ctx->set) {
5886 set_found = true;
5887 break;
5888 }
5889 }
5890 break;
5891 }
5892
5893 if (!set_found) {
5894 rcu_read_unlock();
5895 return -ENOENT;
5896 }
5897
5898 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM);
5899 portid = NETLINK_CB(cb->skb).portid;
5900 seq = cb->nlh->nlmsg_seq;
5901
5902 nlh = nfnl_msg_put(skb, portid, seq, event, NLM_F_MULTI,
5903 table->family, NFNETLINK_V0, nft_base_seq(net));
5904 if (!nlh)
5905 goto nla_put_failure;
5906
5907 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name))
5908 goto nla_put_failure;
5909 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
5910 goto nla_put_failure;
5911
5912 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
5913 if (nest == NULL)
5914 goto nla_put_failure;
5915
5916 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETSETELEM_RESET)
5917 reset = true;
5918
5919 args.cb = cb;
5920 args.skb = skb;
5921 args.reset = reset;
5922 args.iter.genmask = nft_genmask_cur(net);
5923 args.iter.type = NFT_ITER_READ;
5924 args.iter.skip = cb->args[0];
5925 args.iter.count = 0;
5926 args.iter.err = 0;
5927 args.iter.fn = nf_tables_dump_setelem;
5928 set->ops->walk(&dump_ctx->ctx, set, &args.iter);
5929
5930 if (!args.iter.err && args.iter.count == cb->args[0])
5931 args.iter.err = nft_set_catchall_dump(net, skb, set,
5932 reset, cb->seq);
5933 nla_nest_end(skb, nest);
5934 nlmsg_end(skb, nlh);
5935
5936 if (reset && args.iter.count > args.iter.skip)
5937 audit_log_nft_set_reset(table, cb->seq,
5938 args.iter.count - args.iter.skip);
5939
5940 rcu_read_unlock();
5941
5942 if (args.iter.err && args.iter.err != -EMSGSIZE)
5943 return args.iter.err;
5944 if (args.iter.count == cb->args[0])
5945 return 0;
5946
5947 cb->args[0] = args.iter.count;
5948 return skb->len;
5949
5950 nla_put_failure:
5951 rcu_read_unlock();
5952 return -ENOSPC;
5953 }
5954
nf_tables_dump_set_start(struct netlink_callback * cb)5955 static int nf_tables_dump_set_start(struct netlink_callback *cb)
5956 {
5957 struct nft_set_dump_ctx *dump_ctx = cb->data;
5958
5959 cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC);
5960
5961 return cb->data ? 0 : -ENOMEM;
5962 }
5963
nf_tables_dump_set_done(struct netlink_callback * cb)5964 static int nf_tables_dump_set_done(struct netlink_callback *cb)
5965 {
5966 kfree(cb->data);
5967 return 0;
5968 }
5969
nf_tables_fill_setelem_info(struct sk_buff * skb,const struct nft_ctx * ctx,u32 seq,u32 portid,int event,u16 flags,const struct nft_set * set,const struct nft_set_elem * elem,bool reset)5970 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
5971 const struct nft_ctx *ctx, u32 seq,
5972 u32 portid, int event, u16 flags,
5973 const struct nft_set *set,
5974 const struct nft_set_elem *elem,
5975 bool reset)
5976 {
5977 struct nlmsghdr *nlh;
5978 struct nlattr *nest;
5979 int err;
5980
5981 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
5982 nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family,
5983 NFNETLINK_V0, nft_base_seq(ctx->net));
5984 if (!nlh)
5985 goto nla_put_failure;
5986
5987 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
5988 goto nla_put_failure;
5989 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
5990 goto nla_put_failure;
5991
5992 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
5993 if (nest == NULL)
5994 goto nla_put_failure;
5995
5996 err = nf_tables_fill_setelem(skb, set, elem, reset);
5997 if (err < 0)
5998 goto nla_put_failure;
5999
6000 nla_nest_end(skb, nest);
6001
6002 nlmsg_end(skb, nlh);
6003 return 0;
6004
6005 nla_put_failure:
6006 nlmsg_trim(skb, nlh);
6007 return -1;
6008 }
6009
nft_setelem_parse_flags(const struct nft_set * set,const struct nlattr * attr,u32 * flags)6010 static int nft_setelem_parse_flags(const struct nft_set *set,
6011 const struct nlattr *attr, u32 *flags)
6012 {
6013 if (attr == NULL)
6014 return 0;
6015
6016 *flags = ntohl(nla_get_be32(attr));
6017 if (*flags & ~(NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL))
6018 return -EOPNOTSUPP;
6019 if (!(set->flags & NFT_SET_INTERVAL) &&
6020 *flags & NFT_SET_ELEM_INTERVAL_END)
6021 return -EINVAL;
6022 if ((*flags & (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) ==
6023 (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL))
6024 return -EINVAL;
6025
6026 return 0;
6027 }
6028
nft_setelem_parse_key(struct nft_ctx * ctx,struct nft_set * set,struct nft_data * key,struct nlattr * attr)6029 static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set,
6030 struct nft_data *key, struct nlattr *attr)
6031 {
6032 struct nft_data_desc desc = {
6033 .type = NFT_DATA_VALUE,
6034 .size = NFT_DATA_VALUE_MAXLEN,
6035 .len = set->klen,
6036 };
6037
6038 return nft_data_init(ctx, key, &desc, attr);
6039 }
6040
nft_setelem_parse_data(struct nft_ctx * ctx,struct nft_set * set,struct nft_data_desc * desc,struct nft_data * data,struct nlattr * attr)6041 static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set,
6042 struct nft_data_desc *desc,
6043 struct nft_data *data,
6044 struct nlattr *attr)
6045 {
6046 u32 dtype;
6047
6048 if (set->dtype == NFT_DATA_VERDICT)
6049 dtype = NFT_DATA_VERDICT;
6050 else
6051 dtype = NFT_DATA_VALUE;
6052
6053 desc->type = dtype;
6054 desc->size = NFT_DATA_VALUE_MAXLEN;
6055 desc->len = set->dlen;
6056 desc->flags = NFT_DATA_DESC_SETELEM;
6057
6058 return nft_data_init(ctx, data, desc, attr);
6059 }
6060
nft_setelem_catchall_get(const struct net * net,const struct nft_set * set)6061 static void *nft_setelem_catchall_get(const struct net *net,
6062 const struct nft_set *set)
6063 {
6064 struct nft_set_elem_catchall *catchall;
6065 u8 genmask = nft_genmask_cur(net);
6066 struct nft_set_ext *ext;
6067 void *priv = NULL;
6068
6069 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
6070 ext = nft_set_elem_ext(set, catchall->elem);
6071 if (!nft_set_elem_active(ext, genmask) ||
6072 nft_set_elem_expired(ext))
6073 continue;
6074
6075 priv = catchall->elem;
6076 break;
6077 }
6078
6079 return priv;
6080 }
6081
nft_setelem_get(struct nft_ctx * ctx,struct nft_set * set,struct nft_set_elem * elem,u32 flags)6082 static int nft_setelem_get(struct nft_ctx *ctx, struct nft_set *set,
6083 struct nft_set_elem *elem, u32 flags)
6084 {
6085 void *priv;
6086
6087 if (!(flags & NFT_SET_ELEM_CATCHALL)) {
6088 priv = set->ops->get(ctx->net, set, elem, flags);
6089 if (IS_ERR(priv))
6090 return PTR_ERR(priv);
6091 } else {
6092 priv = nft_setelem_catchall_get(ctx->net, set);
6093 if (!priv)
6094 return -ENOENT;
6095 }
6096 elem->priv = priv;
6097
6098 return 0;
6099 }
6100
nft_get_set_elem(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * attr,bool reset)6101 static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set,
6102 const struct nlattr *attr, bool reset)
6103 {
6104 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
6105 struct nft_set_elem elem;
6106 struct sk_buff *skb;
6107 uint32_t flags = 0;
6108 int err;
6109
6110 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
6111 nft_set_elem_policy, NULL);
6112 if (err < 0)
6113 return err;
6114
6115 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
6116 if (err < 0)
6117 return err;
6118
6119 if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL))
6120 return -EINVAL;
6121
6122 if (nla[NFTA_SET_ELEM_KEY]) {
6123 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
6124 nla[NFTA_SET_ELEM_KEY]);
6125 if (err < 0)
6126 return err;
6127 }
6128
6129 if (nla[NFTA_SET_ELEM_KEY_END]) {
6130 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
6131 nla[NFTA_SET_ELEM_KEY_END]);
6132 if (err < 0)
6133 return err;
6134 }
6135
6136 err = nft_setelem_get(ctx, set, &elem, flags);
6137 if (err < 0)
6138 return err;
6139
6140 err = -ENOMEM;
6141 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
6142 if (skb == NULL)
6143 return err;
6144
6145 err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid,
6146 NFT_MSG_NEWSETELEM, 0, set, &elem,
6147 reset);
6148 if (err < 0)
6149 goto err_fill_setelem;
6150
6151 return nfnetlink_unicast(skb, ctx->net, ctx->portid);
6152
6153 err_fill_setelem:
6154 kfree_skb(skb);
6155 return err;
6156 }
6157
6158 /* called with rcu_read_lock held */
nf_tables_getsetelem(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])6159 static int nf_tables_getsetelem(struct sk_buff *skb,
6160 const struct nfnl_info *info,
6161 const struct nlattr * const nla[])
6162 {
6163 struct netlink_ext_ack *extack = info->extack;
6164 u8 genmask = nft_genmask_cur(info->net);
6165 u8 family = info->nfmsg->nfgen_family;
6166 int rem, err = 0, nelems = 0;
6167 struct net *net = info->net;
6168 struct nft_table *table;
6169 struct nft_set *set;
6170 struct nlattr *attr;
6171 struct nft_ctx ctx;
6172 bool reset = false;
6173
6174 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
6175 genmask, 0);
6176 if (IS_ERR(table)) {
6177 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
6178 return PTR_ERR(table);
6179 }
6180
6181 set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
6182 if (IS_ERR(set))
6183 return PTR_ERR(set);
6184
6185 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
6186
6187 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
6188 struct netlink_dump_control c = {
6189 .start = nf_tables_dump_set_start,
6190 .dump = nf_tables_dump_set,
6191 .done = nf_tables_dump_set_done,
6192 .module = THIS_MODULE,
6193 };
6194 struct nft_set_dump_ctx dump_ctx = {
6195 .set = set,
6196 .ctx = ctx,
6197 };
6198
6199 c.data = &dump_ctx;
6200 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
6201 }
6202
6203 if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
6204 return -EINVAL;
6205
6206 if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETSETELEM_RESET)
6207 reset = true;
6208
6209 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
6210 err = nft_get_set_elem(&ctx, set, attr, reset);
6211 if (err < 0) {
6212 NL_SET_BAD_ATTR(extack, attr);
6213 break;
6214 }
6215 nelems++;
6216 }
6217
6218 if (reset)
6219 audit_log_nft_set_reset(table, nft_pernet(net)->base_seq,
6220 nelems);
6221
6222 return err;
6223 }
6224
nf_tables_setelem_notify(const struct nft_ctx * ctx,const struct nft_set * set,const struct nft_set_elem * elem,int event)6225 static void nf_tables_setelem_notify(const struct nft_ctx *ctx,
6226 const struct nft_set *set,
6227 const struct nft_set_elem *elem,
6228 int event)
6229 {
6230 struct nftables_pernet *nft_net;
6231 struct net *net = ctx->net;
6232 u32 portid = ctx->portid;
6233 struct sk_buff *skb;
6234 u16 flags = 0;
6235 int err;
6236
6237 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
6238 return;
6239
6240 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6241 if (skb == NULL)
6242 goto err;
6243
6244 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
6245 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
6246
6247 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
6248 set, elem, false);
6249 if (err < 0) {
6250 kfree_skb(skb);
6251 goto err;
6252 }
6253
6254 nft_net = nft_pernet(net);
6255 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
6256 return;
6257 err:
6258 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
6259 }
6260
nft_trans_elem_alloc(struct nft_ctx * ctx,int msg_type,struct nft_set * set)6261 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
6262 int msg_type,
6263 struct nft_set *set)
6264 {
6265 struct nft_trans *trans;
6266
6267 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
6268 if (trans == NULL)
6269 return NULL;
6270
6271 nft_trans_elem_set(trans) = set;
6272 return trans;
6273 }
6274
nft_set_elem_expr_alloc(const struct nft_ctx * ctx,const struct nft_set * set,const struct nlattr * attr)6275 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
6276 const struct nft_set *set,
6277 const struct nlattr *attr)
6278 {
6279 struct nft_expr *expr;
6280 int err;
6281
6282 expr = nft_expr_init(ctx, attr);
6283 if (IS_ERR(expr))
6284 return expr;
6285
6286 err = -EOPNOTSUPP;
6287 if (expr->ops->type->flags & NFT_EXPR_GC) {
6288 if (set->flags & NFT_SET_TIMEOUT)
6289 goto err_set_elem_expr;
6290 if (!set->ops->gc_init)
6291 goto err_set_elem_expr;
6292 set->ops->gc_init(set);
6293 }
6294
6295 return expr;
6296
6297 err_set_elem_expr:
6298 nft_expr_destroy(ctx, expr);
6299 return ERR_PTR(err);
6300 }
6301
nft_set_ext_check(const struct nft_set_ext_tmpl * tmpl,u8 id,u32 len)6302 static int nft_set_ext_check(const struct nft_set_ext_tmpl *tmpl, u8 id, u32 len)
6303 {
6304 len += nft_set_ext_types[id].len;
6305 if (len > tmpl->ext_len[id] ||
6306 len > U8_MAX)
6307 return -1;
6308
6309 return 0;
6310 }
6311
nft_set_ext_memcpy(const struct nft_set_ext_tmpl * tmpl,u8 id,void * to,const void * from,u32 len)6312 static int nft_set_ext_memcpy(const struct nft_set_ext_tmpl *tmpl, u8 id,
6313 void *to, const void *from, u32 len)
6314 {
6315 if (nft_set_ext_check(tmpl, id, len) < 0)
6316 return -1;
6317
6318 memcpy(to, from, len);
6319
6320 return 0;
6321 }
6322
nft_set_elem_init(const struct nft_set * set,const struct nft_set_ext_tmpl * tmpl,const u32 * key,const u32 * key_end,const u32 * data,u64 timeout,u64 expiration,gfp_t gfp)6323 void *nft_set_elem_init(const struct nft_set *set,
6324 const struct nft_set_ext_tmpl *tmpl,
6325 const u32 *key, const u32 *key_end,
6326 const u32 *data, u64 timeout, u64 expiration, gfp_t gfp)
6327 {
6328 struct nft_set_ext *ext;
6329 void *elem;
6330
6331 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
6332 if (elem == NULL)
6333 return ERR_PTR(-ENOMEM);
6334
6335 ext = nft_set_elem_ext(set, elem);
6336 nft_set_ext_init(ext, tmpl);
6337
6338 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) &&
6339 nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY,
6340 nft_set_ext_key(ext), key, set->klen) < 0)
6341 goto err_ext_check;
6342
6343 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) &&
6344 nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY_END,
6345 nft_set_ext_key_end(ext), key_end, set->klen) < 0)
6346 goto err_ext_check;
6347
6348 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
6349 nft_set_ext_memcpy(tmpl, NFT_SET_EXT_DATA,
6350 nft_set_ext_data(ext), data, set->dlen) < 0)
6351 goto err_ext_check;
6352
6353 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
6354 *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration;
6355 if (expiration == 0)
6356 *nft_set_ext_expiration(ext) += timeout;
6357 }
6358 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
6359 *nft_set_ext_timeout(ext) = timeout;
6360
6361 return elem;
6362
6363 err_ext_check:
6364 kfree(elem);
6365
6366 return ERR_PTR(-EINVAL);
6367 }
6368
__nft_set_elem_expr_destroy(const struct nft_ctx * ctx,struct nft_expr * expr)6369 static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
6370 struct nft_expr *expr)
6371 {
6372 if (expr->ops->destroy_clone) {
6373 expr->ops->destroy_clone(ctx, expr);
6374 module_put(expr->ops->type->owner);
6375 } else {
6376 nf_tables_expr_destroy(ctx, expr);
6377 }
6378 }
6379
nft_set_elem_expr_destroy(const struct nft_ctx * ctx,struct nft_set_elem_expr * elem_expr)6380 static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
6381 struct nft_set_elem_expr *elem_expr)
6382 {
6383 struct nft_expr *expr;
6384 u32 size;
6385
6386 nft_setelem_expr_foreach(expr, elem_expr, size)
6387 __nft_set_elem_expr_destroy(ctx, expr);
6388 }
6389
6390 /* Drop references and destroy. Called from gc, dynset and abort path. */
nft_set_elem_destroy(const struct nft_set * set,void * elem,bool destroy_expr)6391 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
6392 bool destroy_expr)
6393 {
6394 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
6395 struct nft_ctx ctx = {
6396 .net = read_pnet(&set->net),
6397 .family = set->table->family,
6398 };
6399
6400 nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE);
6401 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
6402 nft_data_release(nft_set_ext_data(ext), set->dtype);
6403 if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
6404 nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext));
6405
6406 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
6407 nft_use_dec(&(*nft_set_ext_obj(ext))->use);
6408 kfree(elem);
6409 }
6410 EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
6411
6412 /* Destroy element. References have been already dropped in the preparation
6413 * path via nft_setelem_data_deactivate().
6414 */
nf_tables_set_elem_destroy(const struct nft_ctx * ctx,const struct nft_set * set,void * elem)6415 void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
6416 const struct nft_set *set, void *elem)
6417 {
6418 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
6419
6420 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
6421 nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext));
6422
6423 kfree(elem);
6424 }
6425
nft_set_elem_expr_clone(const struct nft_ctx * ctx,struct nft_set * set,struct nft_expr * expr_array[])6426 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
6427 struct nft_expr *expr_array[])
6428 {
6429 struct nft_expr *expr;
6430 int err, i, k;
6431
6432 for (i = 0; i < set->num_exprs; i++) {
6433 expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL_ACCOUNT);
6434 if (!expr)
6435 goto err_expr;
6436
6437 err = nft_expr_clone(expr, set->exprs[i]);
6438 if (err < 0) {
6439 kfree(expr);
6440 goto err_expr;
6441 }
6442 expr_array[i] = expr;
6443 }
6444
6445 return 0;
6446
6447 err_expr:
6448 for (k = i - 1; k >= 0; k--)
6449 nft_expr_destroy(ctx, expr_array[k]);
6450
6451 return -ENOMEM;
6452 }
6453
nft_set_elem_expr_setup(struct nft_ctx * ctx,const struct nft_set_ext_tmpl * tmpl,const struct nft_set_ext * ext,struct nft_expr * expr_array[],u32 num_exprs)6454 static int nft_set_elem_expr_setup(struct nft_ctx *ctx,
6455 const struct nft_set_ext_tmpl *tmpl,
6456 const struct nft_set_ext *ext,
6457 struct nft_expr *expr_array[],
6458 u32 num_exprs)
6459 {
6460 struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext);
6461 u32 len = sizeof(struct nft_set_elem_expr);
6462 struct nft_expr *expr;
6463 int i, err;
6464
6465 if (num_exprs == 0)
6466 return 0;
6467
6468 for (i = 0; i < num_exprs; i++)
6469 len += expr_array[i]->ops->size;
6470
6471 if (nft_set_ext_check(tmpl, NFT_SET_EXT_EXPRESSIONS, len) < 0)
6472 return -EINVAL;
6473
6474 for (i = 0; i < num_exprs; i++) {
6475 expr = nft_setelem_expr_at(elem_expr, elem_expr->size);
6476 err = nft_expr_clone(expr, expr_array[i]);
6477 if (err < 0)
6478 goto err_elem_expr_setup;
6479
6480 elem_expr->size += expr_array[i]->ops->size;
6481 nft_expr_destroy(ctx, expr_array[i]);
6482 expr_array[i] = NULL;
6483 }
6484
6485 return 0;
6486
6487 err_elem_expr_setup:
6488 for (; i < num_exprs; i++) {
6489 nft_expr_destroy(ctx, expr_array[i]);
6490 expr_array[i] = NULL;
6491 }
6492
6493 return -ENOMEM;
6494 }
6495
nft_set_catchall_lookup(const struct net * net,const struct nft_set * set)6496 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
6497 const struct nft_set *set)
6498 {
6499 struct nft_set_elem_catchall *catchall;
6500 u8 genmask = nft_genmask_cur(net);
6501 struct nft_set_ext *ext;
6502
6503 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
6504 ext = nft_set_elem_ext(set, catchall->elem);
6505 if (nft_set_elem_active(ext, genmask) &&
6506 !nft_set_elem_expired(ext) &&
6507 !nft_set_elem_is_dead(ext))
6508 return ext;
6509 }
6510
6511 return NULL;
6512 }
6513 EXPORT_SYMBOL_GPL(nft_set_catchall_lookup);
6514
nft_setelem_catchall_insert(const struct net * net,struct nft_set * set,const struct nft_set_elem * elem,struct nft_set_ext ** pext)6515 static int nft_setelem_catchall_insert(const struct net *net,
6516 struct nft_set *set,
6517 const struct nft_set_elem *elem,
6518 struct nft_set_ext **pext)
6519 {
6520 struct nft_set_elem_catchall *catchall;
6521 u8 genmask = nft_genmask_next(net);
6522 struct nft_set_ext *ext;
6523
6524 list_for_each_entry(catchall, &set->catchall_list, list) {
6525 ext = nft_set_elem_ext(set, catchall->elem);
6526 if (nft_set_elem_active(ext, genmask)) {
6527 *pext = ext;
6528 return -EEXIST;
6529 }
6530 }
6531
6532 catchall = kmalloc(sizeof(*catchall), GFP_KERNEL);
6533 if (!catchall)
6534 return -ENOMEM;
6535
6536 catchall->elem = elem->priv;
6537 list_add_tail_rcu(&catchall->list, &set->catchall_list);
6538
6539 return 0;
6540 }
6541
nft_setelem_insert(const struct net * net,struct nft_set * set,const struct nft_set_elem * elem,struct nft_set_ext ** ext,unsigned int flags)6542 static int nft_setelem_insert(const struct net *net,
6543 struct nft_set *set,
6544 const struct nft_set_elem *elem,
6545 struct nft_set_ext **ext, unsigned int flags)
6546 {
6547 int ret;
6548
6549 if (flags & NFT_SET_ELEM_CATCHALL)
6550 ret = nft_setelem_catchall_insert(net, set, elem, ext);
6551 else
6552 ret = set->ops->insert(net, set, elem, ext);
6553
6554 return ret;
6555 }
6556
nft_setelem_is_catchall(const struct nft_set * set,const struct nft_set_elem * elem)6557 static bool nft_setelem_is_catchall(const struct nft_set *set,
6558 const struct nft_set_elem *elem)
6559 {
6560 struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
6561
6562 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
6563 *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL)
6564 return true;
6565
6566 return false;
6567 }
6568
nft_setelem_activate(struct net * net,struct nft_set * set,struct nft_set_elem * elem)6569 static void nft_setelem_activate(struct net *net, struct nft_set *set,
6570 struct nft_set_elem *elem)
6571 {
6572 struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
6573
6574 if (nft_setelem_is_catchall(set, elem)) {
6575 nft_clear(net, ext);
6576 } else {
6577 set->ops->activate(net, set, elem);
6578 }
6579 }
6580
nft_setelem_catchall_deactivate(const struct net * net,struct nft_set * set,struct nft_set_elem * elem)6581 static int nft_setelem_catchall_deactivate(const struct net *net,
6582 struct nft_set *set,
6583 struct nft_set_elem *elem)
6584 {
6585 struct nft_set_elem_catchall *catchall;
6586 struct nft_set_ext *ext;
6587
6588 list_for_each_entry(catchall, &set->catchall_list, list) {
6589 ext = nft_set_elem_ext(set, catchall->elem);
6590 if (!nft_is_active_next(net, ext))
6591 continue;
6592
6593 kfree(elem->priv);
6594 elem->priv = catchall->elem;
6595 nft_set_elem_change_active(net, set, ext);
6596 return 0;
6597 }
6598
6599 return -ENOENT;
6600 }
6601
__nft_setelem_deactivate(const struct net * net,struct nft_set * set,struct nft_set_elem * elem)6602 static int __nft_setelem_deactivate(const struct net *net,
6603 struct nft_set *set,
6604 struct nft_set_elem *elem)
6605 {
6606 void *priv;
6607
6608 priv = set->ops->deactivate(net, set, elem);
6609 if (!priv)
6610 return -ENOENT;
6611
6612 kfree(elem->priv);
6613 elem->priv = priv;
6614 set->ndeact++;
6615
6616 return 0;
6617 }
6618
nft_setelem_deactivate(const struct net * net,struct nft_set * set,struct nft_set_elem * elem,u32 flags)6619 static int nft_setelem_deactivate(const struct net *net,
6620 struct nft_set *set,
6621 struct nft_set_elem *elem, u32 flags)
6622 {
6623 int ret;
6624
6625 if (flags & NFT_SET_ELEM_CATCHALL)
6626 ret = nft_setelem_catchall_deactivate(net, set, elem);
6627 else
6628 ret = __nft_setelem_deactivate(net, set, elem);
6629
6630 return ret;
6631 }
6632
nft_setelem_catchall_destroy(struct nft_set_elem_catchall * catchall)6633 static void nft_setelem_catchall_destroy(struct nft_set_elem_catchall *catchall)
6634 {
6635 list_del_rcu(&catchall->list);
6636 kfree_rcu(catchall, rcu);
6637 }
6638
nft_setelem_catchall_remove(const struct net * net,const struct nft_set * set,const struct nft_set_elem * elem)6639 static void nft_setelem_catchall_remove(const struct net *net,
6640 const struct nft_set *set,
6641 const struct nft_set_elem *elem)
6642 {
6643 struct nft_set_elem_catchall *catchall, *next;
6644
6645 list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
6646 if (catchall->elem == elem->priv) {
6647 nft_setelem_catchall_destroy(catchall);
6648 break;
6649 }
6650 }
6651 }
6652
nft_setelem_remove(const struct net * net,const struct nft_set * set,const struct nft_set_elem * elem)6653 static void nft_setelem_remove(const struct net *net,
6654 const struct nft_set *set,
6655 const struct nft_set_elem *elem)
6656 {
6657 if (nft_setelem_is_catchall(set, elem))
6658 nft_setelem_catchall_remove(net, set, elem);
6659 else
6660 set->ops->remove(net, set, elem);
6661 }
6662
nft_setelem_valid_key_end(const struct nft_set * set,struct nlattr ** nla,u32 flags)6663 static bool nft_setelem_valid_key_end(const struct nft_set *set,
6664 struct nlattr **nla, u32 flags)
6665 {
6666 if ((set->flags & (NFT_SET_CONCAT | NFT_SET_INTERVAL)) ==
6667 (NFT_SET_CONCAT | NFT_SET_INTERVAL)) {
6668 if (flags & NFT_SET_ELEM_INTERVAL_END)
6669 return false;
6670
6671 if (nla[NFTA_SET_ELEM_KEY_END] &&
6672 flags & NFT_SET_ELEM_CATCHALL)
6673 return false;
6674 } else {
6675 if (nla[NFTA_SET_ELEM_KEY_END])
6676 return false;
6677 }
6678
6679 return true;
6680 }
6681
nft_add_set_elem(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * attr,u32 nlmsg_flags)6682 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
6683 const struct nlattr *attr, u32 nlmsg_flags)
6684 {
6685 struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {};
6686 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
6687 u8 genmask = nft_genmask_next(ctx->net);
6688 u32 flags = 0, size = 0, num_exprs = 0;
6689 struct nft_set_ext_tmpl tmpl;
6690 struct nft_set_ext *ext, *ext2;
6691 struct nft_set_elem elem;
6692 struct nft_set_binding *binding;
6693 struct nft_object *obj = NULL;
6694 struct nft_userdata *udata;
6695 struct nft_data_desc desc;
6696 enum nft_registers dreg;
6697 struct nft_trans *trans;
6698 u64 timeout;
6699 u64 expiration;
6700 int err, i;
6701 u8 ulen;
6702
6703 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
6704 nft_set_elem_policy, NULL);
6705 if (err < 0)
6706 return err;
6707
6708 nft_set_ext_prepare(&tmpl);
6709
6710 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
6711 if (err < 0)
6712 return err;
6713
6714 if (((flags & NFT_SET_ELEM_CATCHALL) && nla[NFTA_SET_ELEM_KEY]) ||
6715 (!(flags & NFT_SET_ELEM_CATCHALL) && !nla[NFTA_SET_ELEM_KEY]))
6716 return -EINVAL;
6717
6718 if (flags != 0) {
6719 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
6720 if (err < 0)
6721 return err;
6722 }
6723
6724 if (set->flags & NFT_SET_MAP) {
6725 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
6726 !(flags & NFT_SET_ELEM_INTERVAL_END))
6727 return -EINVAL;
6728 } else {
6729 if (nla[NFTA_SET_ELEM_DATA] != NULL)
6730 return -EINVAL;
6731 }
6732
6733 if (set->flags & NFT_SET_OBJECT) {
6734 if (!nla[NFTA_SET_ELEM_OBJREF] &&
6735 !(flags & NFT_SET_ELEM_INTERVAL_END))
6736 return -EINVAL;
6737 } else {
6738 if (nla[NFTA_SET_ELEM_OBJREF])
6739 return -EINVAL;
6740 }
6741
6742 if (!nft_setelem_valid_key_end(set, nla, flags))
6743 return -EINVAL;
6744
6745 if ((flags & NFT_SET_ELEM_INTERVAL_END) &&
6746 (nla[NFTA_SET_ELEM_DATA] ||
6747 nla[NFTA_SET_ELEM_OBJREF] ||
6748 nla[NFTA_SET_ELEM_TIMEOUT] ||
6749 nla[NFTA_SET_ELEM_EXPIRATION] ||
6750 nla[NFTA_SET_ELEM_USERDATA] ||
6751 nla[NFTA_SET_ELEM_EXPR] ||
6752 nla[NFTA_SET_ELEM_KEY_END] ||
6753 nla[NFTA_SET_ELEM_EXPRESSIONS]))
6754 return -EINVAL;
6755
6756 timeout = 0;
6757 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
6758 if (!(set->flags & NFT_SET_TIMEOUT))
6759 return -EINVAL;
6760 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT],
6761 &timeout);
6762 if (err)
6763 return err;
6764 } else if (set->flags & NFT_SET_TIMEOUT &&
6765 !(flags & NFT_SET_ELEM_INTERVAL_END)) {
6766 timeout = set->timeout;
6767 }
6768
6769 expiration = 0;
6770 if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) {
6771 if (!(set->flags & NFT_SET_TIMEOUT))
6772 return -EINVAL;
6773 if (timeout == 0)
6774 return -EOPNOTSUPP;
6775
6776 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION],
6777 &expiration);
6778 if (err)
6779 return err;
6780
6781 if (expiration > timeout)
6782 return -ERANGE;
6783 }
6784
6785 if (nla[NFTA_SET_ELEM_EXPR]) {
6786 struct nft_expr *expr;
6787
6788 if (set->num_exprs && set->num_exprs != 1)
6789 return -EOPNOTSUPP;
6790
6791 expr = nft_set_elem_expr_alloc(ctx, set,
6792 nla[NFTA_SET_ELEM_EXPR]);
6793 if (IS_ERR(expr))
6794 return PTR_ERR(expr);
6795
6796 expr_array[0] = expr;
6797 num_exprs = 1;
6798
6799 if (set->num_exprs && set->exprs[0]->ops != expr->ops) {
6800 err = -EOPNOTSUPP;
6801 goto err_set_elem_expr;
6802 }
6803 } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) {
6804 struct nft_expr *expr;
6805 struct nlattr *tmp;
6806 int left;
6807
6808 i = 0;
6809 nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) {
6810 if (i == NFT_SET_EXPR_MAX ||
6811 (set->num_exprs && set->num_exprs == i)) {
6812 err = -E2BIG;
6813 goto err_set_elem_expr;
6814 }
6815 if (nla_type(tmp) != NFTA_LIST_ELEM) {
6816 err = -EINVAL;
6817 goto err_set_elem_expr;
6818 }
6819 expr = nft_set_elem_expr_alloc(ctx, set, tmp);
6820 if (IS_ERR(expr)) {
6821 err = PTR_ERR(expr);
6822 goto err_set_elem_expr;
6823 }
6824 expr_array[i] = expr;
6825 num_exprs++;
6826
6827 if (set->num_exprs && expr->ops != set->exprs[i]->ops) {
6828 err = -EOPNOTSUPP;
6829 goto err_set_elem_expr;
6830 }
6831 i++;
6832 }
6833 if (set->num_exprs && set->num_exprs != i) {
6834 err = -EOPNOTSUPP;
6835 goto err_set_elem_expr;
6836 }
6837 } else if (set->num_exprs > 0 &&
6838 !(flags & NFT_SET_ELEM_INTERVAL_END)) {
6839 err = nft_set_elem_expr_clone(ctx, set, expr_array);
6840 if (err < 0)
6841 goto err_set_elem_expr_clone;
6842
6843 num_exprs = set->num_exprs;
6844 }
6845
6846 if (nla[NFTA_SET_ELEM_KEY]) {
6847 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
6848 nla[NFTA_SET_ELEM_KEY]);
6849 if (err < 0)
6850 goto err_set_elem_expr;
6851
6852 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
6853 if (err < 0)
6854 goto err_parse_key;
6855 }
6856
6857 if (nla[NFTA_SET_ELEM_KEY_END]) {
6858 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
6859 nla[NFTA_SET_ELEM_KEY_END]);
6860 if (err < 0)
6861 goto err_parse_key;
6862
6863 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
6864 if (err < 0)
6865 goto err_parse_key_end;
6866 }
6867
6868 if (timeout > 0) {
6869 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
6870 if (err < 0)
6871 goto err_parse_key_end;
6872
6873 if (timeout != set->timeout) {
6874 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
6875 if (err < 0)
6876 goto err_parse_key_end;
6877 }
6878 }
6879
6880 if (num_exprs) {
6881 for (i = 0; i < num_exprs; i++)
6882 size += expr_array[i]->ops->size;
6883
6884 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS,
6885 sizeof(struct nft_set_elem_expr) + size);
6886 if (err < 0)
6887 goto err_parse_key_end;
6888 }
6889
6890 if (nla[NFTA_SET_ELEM_OBJREF] != NULL) {
6891 obj = nft_obj_lookup(ctx->net, ctx->table,
6892 nla[NFTA_SET_ELEM_OBJREF],
6893 set->objtype, genmask);
6894 if (IS_ERR(obj)) {
6895 err = PTR_ERR(obj);
6896 obj = NULL;
6897 goto err_parse_key_end;
6898 }
6899
6900 if (!nft_use_inc(&obj->use)) {
6901 err = -EMFILE;
6902 obj = NULL;
6903 goto err_parse_key_end;
6904 }
6905
6906 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF);
6907 if (err < 0)
6908 goto err_parse_key_end;
6909 }
6910
6911 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
6912 err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val,
6913 nla[NFTA_SET_ELEM_DATA]);
6914 if (err < 0)
6915 goto err_parse_key_end;
6916
6917 dreg = nft_type_to_reg(set->dtype);
6918 list_for_each_entry(binding, &set->bindings, list) {
6919 struct nft_ctx bind_ctx = {
6920 .net = ctx->net,
6921 .family = ctx->family,
6922 .table = ctx->table,
6923 .chain = (struct nft_chain *)binding->chain,
6924 };
6925
6926 if (!(binding->flags & NFT_SET_MAP))
6927 continue;
6928
6929 err = nft_validate_register_store(&bind_ctx, dreg,
6930 &elem.data.val,
6931 desc.type, desc.len);
6932 if (err < 0)
6933 goto err_parse_data;
6934
6935 if (desc.type == NFT_DATA_VERDICT &&
6936 (elem.data.val.verdict.code == NFT_GOTO ||
6937 elem.data.val.verdict.code == NFT_JUMP))
6938 nft_validate_state_update(ctx->table,
6939 NFT_VALIDATE_NEED);
6940 }
6941
6942 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len);
6943 if (err < 0)
6944 goto err_parse_data;
6945 }
6946
6947 /* The full maximum length of userdata can exceed the maximum
6948 * offset value (U8_MAX) for following extensions, therefor it
6949 * must be the last extension added.
6950 */
6951 ulen = 0;
6952 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
6953 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
6954 if (ulen > 0) {
6955 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
6956 ulen);
6957 if (err < 0)
6958 goto err_parse_data;
6959 }
6960 }
6961
6962 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
6963 elem.key_end.val.data, elem.data.val.data,
6964 timeout, expiration, GFP_KERNEL_ACCOUNT);
6965 if (IS_ERR(elem.priv)) {
6966 err = PTR_ERR(elem.priv);
6967 goto err_parse_data;
6968 }
6969
6970 ext = nft_set_elem_ext(set, elem.priv);
6971 if (flags)
6972 *nft_set_ext_flags(ext) = flags;
6973
6974 if (obj)
6975 *nft_set_ext_obj(ext) = obj;
6976
6977 if (ulen > 0) {
6978 if (nft_set_ext_check(&tmpl, NFT_SET_EXT_USERDATA, ulen) < 0) {
6979 err = -EINVAL;
6980 goto err_elem_free;
6981 }
6982 udata = nft_set_ext_userdata(ext);
6983 udata->len = ulen - 1;
6984 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
6985 }
6986 err = nft_set_elem_expr_setup(ctx, &tmpl, ext, expr_array, num_exprs);
6987 if (err < 0)
6988 goto err_elem_free;
6989
6990 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
6991 if (trans == NULL) {
6992 err = -ENOMEM;
6993 goto err_elem_free;
6994 }
6995
6996 ext->genmask = nft_genmask_cur(ctx->net);
6997
6998 err = nft_setelem_insert(ctx->net, set, &elem, &ext2, flags);
6999 if (err) {
7000 if (err == -EEXIST) {
7001 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^
7002 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) ||
7003 nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^
7004 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF))
7005 goto err_element_clash;
7006 if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
7007 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) &&
7008 memcmp(nft_set_ext_data(ext),
7009 nft_set_ext_data(ext2), set->dlen) != 0) ||
7010 (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
7011 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) &&
7012 *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2)))
7013 goto err_element_clash;
7014 else if (!(nlmsg_flags & NLM_F_EXCL))
7015 err = 0;
7016 } else if (err == -ENOTEMPTY) {
7017 /* ENOTEMPTY reports overlapping between this element
7018 * and an existing one.
7019 */
7020 err = -EEXIST;
7021 }
7022 goto err_element_clash;
7023 }
7024
7025 if (!(flags & NFT_SET_ELEM_CATCHALL)) {
7026 unsigned int max = set->size ? set->size + set->ndeact : UINT_MAX;
7027
7028 if (!atomic_add_unless(&set->nelems, 1, max)) {
7029 err = -ENFILE;
7030 goto err_set_full;
7031 }
7032 }
7033
7034 nft_trans_elem(trans) = elem;
7035 nft_trans_commit_list_add_tail(ctx->net, trans);
7036 return 0;
7037
7038 err_set_full:
7039 nft_setelem_remove(ctx->net, set, &elem);
7040 err_element_clash:
7041 kfree(trans);
7042 err_elem_free:
7043 nf_tables_set_elem_destroy(ctx, set, elem.priv);
7044 err_parse_data:
7045 if (nla[NFTA_SET_ELEM_DATA] != NULL)
7046 nft_data_release(&elem.data.val, desc.type);
7047 err_parse_key_end:
7048 if (obj)
7049 nft_use_dec_restore(&obj->use);
7050
7051 nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
7052 err_parse_key:
7053 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
7054 err_set_elem_expr:
7055 for (i = 0; i < num_exprs && expr_array[i]; i++)
7056 nft_expr_destroy(ctx, expr_array[i]);
7057 err_set_elem_expr_clone:
7058 return err;
7059 }
7060
nf_tables_newsetelem(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])7061 static int nf_tables_newsetelem(struct sk_buff *skb,
7062 const struct nfnl_info *info,
7063 const struct nlattr * const nla[])
7064 {
7065 struct netlink_ext_ack *extack = info->extack;
7066 u8 genmask = nft_genmask_next(info->net);
7067 u8 family = info->nfmsg->nfgen_family;
7068 struct net *net = info->net;
7069 const struct nlattr *attr;
7070 struct nft_table *table;
7071 struct nft_set *set;
7072 struct nft_ctx ctx;
7073 int rem, err;
7074
7075 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
7076 return -EINVAL;
7077
7078 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
7079 genmask, NETLINK_CB(skb).portid);
7080 if (IS_ERR(table)) {
7081 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
7082 return PTR_ERR(table);
7083 }
7084
7085 set = nft_set_lookup_global(net, table, nla[NFTA_SET_ELEM_LIST_SET],
7086 nla[NFTA_SET_ELEM_LIST_SET_ID], genmask);
7087 if (IS_ERR(set))
7088 return PTR_ERR(set);
7089
7090 if (!list_empty(&set->bindings) &&
7091 (set->flags & (NFT_SET_CONSTANT | NFT_SET_ANONYMOUS)))
7092 return -EBUSY;
7093
7094 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7095
7096 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
7097 err = nft_add_set_elem(&ctx, set, attr, info->nlh->nlmsg_flags);
7098 if (err < 0) {
7099 NL_SET_BAD_ATTR(extack, attr);
7100 return err;
7101 }
7102 }
7103
7104 if (table->validate_state == NFT_VALIDATE_DO)
7105 return nft_table_validate(net, table);
7106
7107 return 0;
7108 }
7109
7110 /**
7111 * nft_data_hold - hold a nft_data item
7112 *
7113 * @data: struct nft_data to release
7114 * @type: type of data
7115 *
7116 * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded,
7117 * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and
7118 * NFT_GOTO verdicts. This function must be called on active data objects
7119 * from the second phase of the commit protocol.
7120 */
nft_data_hold(const struct nft_data * data,enum nft_data_types type)7121 void nft_data_hold(const struct nft_data *data, enum nft_data_types type)
7122 {
7123 struct nft_chain *chain;
7124
7125 if (type == NFT_DATA_VERDICT) {
7126 switch (data->verdict.code) {
7127 case NFT_JUMP:
7128 case NFT_GOTO:
7129 chain = data->verdict.chain;
7130 nft_use_inc_restore(&chain->use);
7131 break;
7132 }
7133 }
7134 }
7135
nft_setelem_active_next(const struct net * net,const struct nft_set * set,struct nft_set_elem * elem)7136 static int nft_setelem_active_next(const struct net *net,
7137 const struct nft_set *set,
7138 struct nft_set_elem *elem)
7139 {
7140 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
7141 u8 genmask = nft_genmask_next(net);
7142
7143 return nft_set_elem_active(ext, genmask);
7144 }
7145
nft_setelem_data_activate(const struct net * net,const struct nft_set * set,struct nft_set_elem * elem)7146 static void nft_setelem_data_activate(const struct net *net,
7147 const struct nft_set *set,
7148 struct nft_set_elem *elem)
7149 {
7150 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
7151
7152 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
7153 nft_data_hold(nft_set_ext_data(ext), set->dtype);
7154 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
7155 nft_use_inc_restore(&(*nft_set_ext_obj(ext))->use);
7156 }
7157
nft_setelem_data_deactivate(const struct net * net,const struct nft_set * set,struct nft_set_elem * elem)7158 void nft_setelem_data_deactivate(const struct net *net,
7159 const struct nft_set *set,
7160 struct nft_set_elem *elem)
7161 {
7162 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
7163
7164 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
7165 nft_data_release(nft_set_ext_data(ext), set->dtype);
7166 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
7167 nft_use_dec(&(*nft_set_ext_obj(ext))->use);
7168 }
7169
nft_del_setelem(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * attr)7170 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
7171 const struct nlattr *attr)
7172 {
7173 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
7174 struct nft_set_ext_tmpl tmpl;
7175 struct nft_set_elem elem;
7176 struct nft_set_ext *ext;
7177 struct nft_trans *trans;
7178 u32 flags = 0;
7179 int err;
7180
7181 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
7182 nft_set_elem_policy, NULL);
7183 if (err < 0)
7184 return err;
7185
7186 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
7187 if (err < 0)
7188 return err;
7189
7190 if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL))
7191 return -EINVAL;
7192
7193 if (!nft_setelem_valid_key_end(set, nla, flags))
7194 return -EINVAL;
7195
7196 nft_set_ext_prepare(&tmpl);
7197
7198 if (flags != 0) {
7199 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
7200 if (err < 0)
7201 return err;
7202 }
7203
7204 if (nla[NFTA_SET_ELEM_KEY]) {
7205 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
7206 nla[NFTA_SET_ELEM_KEY]);
7207 if (err < 0)
7208 return err;
7209
7210 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
7211 if (err < 0)
7212 goto fail_elem;
7213 }
7214
7215 if (nla[NFTA_SET_ELEM_KEY_END]) {
7216 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
7217 nla[NFTA_SET_ELEM_KEY_END]);
7218 if (err < 0)
7219 goto fail_elem;
7220
7221 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
7222 if (err < 0)
7223 goto fail_elem_key_end;
7224 }
7225
7226 err = -ENOMEM;
7227 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
7228 elem.key_end.val.data, NULL, 0, 0,
7229 GFP_KERNEL_ACCOUNT);
7230 if (IS_ERR(elem.priv)) {
7231 err = PTR_ERR(elem.priv);
7232 goto fail_elem_key_end;
7233 }
7234
7235 ext = nft_set_elem_ext(set, elem.priv);
7236 if (flags)
7237 *nft_set_ext_flags(ext) = flags;
7238
7239 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
7240 if (trans == NULL)
7241 goto fail_trans;
7242
7243 err = nft_setelem_deactivate(ctx->net, set, &elem, flags);
7244 if (err < 0)
7245 goto fail_ops;
7246
7247 nft_setelem_data_deactivate(ctx->net, set, &elem);
7248
7249 nft_trans_elem(trans) = elem;
7250 nft_trans_commit_list_add_tail(ctx->net, trans);
7251 return 0;
7252
7253 fail_ops:
7254 kfree(trans);
7255 fail_trans:
7256 kfree(elem.priv);
7257 fail_elem_key_end:
7258 nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
7259 fail_elem:
7260 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
7261 return err;
7262 }
7263
nft_setelem_flush(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)7264 static int nft_setelem_flush(const struct nft_ctx *ctx,
7265 struct nft_set *set,
7266 const struct nft_set_iter *iter,
7267 struct nft_set_elem *elem)
7268 {
7269 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
7270 struct nft_trans *trans;
7271 int err;
7272
7273 if (!nft_set_elem_active(ext, iter->genmask))
7274 return 0;
7275
7276 trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
7277 sizeof(struct nft_trans_elem), GFP_ATOMIC);
7278 if (!trans)
7279 return -ENOMEM;
7280
7281 if (!set->ops->flush(ctx->net, set, elem->priv)) {
7282 err = -ENOENT;
7283 goto err1;
7284 }
7285 set->ndeact++;
7286
7287 nft_setelem_data_deactivate(ctx->net, set, elem);
7288 nft_trans_elem_set(trans) = set;
7289 nft_trans_elem(trans) = *elem;
7290 nft_trans_commit_list_add_tail(ctx->net, trans);
7291
7292 return 0;
7293 err1:
7294 kfree(trans);
7295 return err;
7296 }
7297
__nft_set_catchall_flush(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_elem * elem)7298 static int __nft_set_catchall_flush(const struct nft_ctx *ctx,
7299 struct nft_set *set,
7300 struct nft_set_elem *elem)
7301 {
7302 struct nft_trans *trans;
7303
7304 trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
7305 sizeof(struct nft_trans_elem), GFP_KERNEL);
7306 if (!trans)
7307 return -ENOMEM;
7308
7309 nft_setelem_data_deactivate(ctx->net, set, elem);
7310 nft_trans_elem_set(trans) = set;
7311 nft_trans_elem(trans) = *elem;
7312 nft_trans_commit_list_add_tail(ctx->net, trans);
7313
7314 return 0;
7315 }
7316
nft_set_catchall_flush(const struct nft_ctx * ctx,struct nft_set * set)7317 static int nft_set_catchall_flush(const struct nft_ctx *ctx,
7318 struct nft_set *set)
7319 {
7320 u8 genmask = nft_genmask_next(ctx->net);
7321 struct nft_set_elem_catchall *catchall;
7322 struct nft_set_elem elem;
7323 struct nft_set_ext *ext;
7324 int ret = 0;
7325
7326 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
7327 ext = nft_set_elem_ext(set, catchall->elem);
7328 if (!nft_set_elem_active(ext, genmask))
7329 continue;
7330
7331 elem.priv = catchall->elem;
7332 ret = __nft_set_catchall_flush(ctx, set, &elem);
7333 if (ret < 0)
7334 break;
7335 nft_set_elem_change_active(ctx->net, set, ext);
7336 }
7337
7338 return ret;
7339 }
7340
nft_set_flush(struct nft_ctx * ctx,struct nft_set * set,u8 genmask)7341 static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask)
7342 {
7343 struct nft_set_iter iter = {
7344 .genmask = genmask,
7345 .type = NFT_ITER_UPDATE,
7346 .fn = nft_setelem_flush,
7347 };
7348
7349 set->ops->walk(ctx, set, &iter);
7350 if (!iter.err)
7351 iter.err = nft_set_catchall_flush(ctx, set);
7352
7353 return iter.err;
7354 }
7355
nf_tables_delsetelem(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])7356 static int nf_tables_delsetelem(struct sk_buff *skb,
7357 const struct nfnl_info *info,
7358 const struct nlattr * const nla[])
7359 {
7360 struct netlink_ext_ack *extack = info->extack;
7361 u8 genmask = nft_genmask_next(info->net);
7362 u8 family = info->nfmsg->nfgen_family;
7363 struct net *net = info->net;
7364 const struct nlattr *attr;
7365 struct nft_table *table;
7366 struct nft_set *set;
7367 struct nft_ctx ctx;
7368 int rem, err = 0;
7369
7370 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
7371 genmask, NETLINK_CB(skb).portid);
7372 if (IS_ERR(table)) {
7373 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
7374 return PTR_ERR(table);
7375 }
7376
7377 set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
7378 if (IS_ERR(set))
7379 return PTR_ERR(set);
7380
7381 if (nft_set_is_anonymous(set))
7382 return -EOPNOTSUPP;
7383
7384 if (!list_empty(&set->bindings) && (set->flags & NFT_SET_CONSTANT))
7385 return -EBUSY;
7386
7387 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7388
7389 if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
7390 return nft_set_flush(&ctx, set, genmask);
7391
7392 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
7393 err = nft_del_setelem(&ctx, set, attr);
7394 if (err == -ENOENT &&
7395 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSETELEM)
7396 continue;
7397
7398 if (err < 0) {
7399 NL_SET_BAD_ATTR(extack, attr);
7400 return err;
7401 }
7402 }
7403
7404 return 0;
7405 }
7406
7407 /*
7408 * Stateful objects
7409 */
7410
7411 /**
7412 * nft_register_obj- register nf_tables stateful object type
7413 * @obj_type: object type
7414 *
7415 * Registers the object type for use with nf_tables. Returns zero on
7416 * success or a negative errno code otherwise.
7417 */
nft_register_obj(struct nft_object_type * obj_type)7418 int nft_register_obj(struct nft_object_type *obj_type)
7419 {
7420 if (obj_type->type == NFT_OBJECT_UNSPEC)
7421 return -EINVAL;
7422
7423 nfnl_lock(NFNL_SUBSYS_NFTABLES);
7424 list_add_rcu(&obj_type->list, &nf_tables_objects);
7425 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
7426 return 0;
7427 }
7428 EXPORT_SYMBOL_GPL(nft_register_obj);
7429
7430 /**
7431 * nft_unregister_obj - unregister nf_tables object type
7432 * @obj_type: object type
7433 *
7434 * Unregisters the object type for use with nf_tables.
7435 */
nft_unregister_obj(struct nft_object_type * obj_type)7436 void nft_unregister_obj(struct nft_object_type *obj_type)
7437 {
7438 nfnl_lock(NFNL_SUBSYS_NFTABLES);
7439 list_del_rcu(&obj_type->list);
7440 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
7441 }
7442 EXPORT_SYMBOL_GPL(nft_unregister_obj);
7443
nft_obj_lookup(const struct net * net,const struct nft_table * table,const struct nlattr * nla,u32 objtype,u8 genmask)7444 struct nft_object *nft_obj_lookup(const struct net *net,
7445 const struct nft_table *table,
7446 const struct nlattr *nla, u32 objtype,
7447 u8 genmask)
7448 {
7449 struct nft_object_hash_key k = { .table = table };
7450 char search[NFT_OBJ_MAXNAMELEN];
7451 struct rhlist_head *tmp, *list;
7452 struct nft_object *obj;
7453
7454 nla_strscpy(search, nla, sizeof(search));
7455 k.name = search;
7456
7457 WARN_ON_ONCE(!rcu_read_lock_held() &&
7458 !lockdep_commit_lock_is_held(net));
7459
7460 rcu_read_lock();
7461 list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params);
7462 if (!list)
7463 goto out;
7464
7465 rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) {
7466 if (objtype == obj->ops->type->type &&
7467 nft_active_genmask(obj, genmask)) {
7468 rcu_read_unlock();
7469 return obj;
7470 }
7471 }
7472 out:
7473 rcu_read_unlock();
7474 return ERR_PTR(-ENOENT);
7475 }
7476 EXPORT_SYMBOL_GPL(nft_obj_lookup);
7477
nft_obj_lookup_byhandle(const struct nft_table * table,const struct nlattr * nla,u32 objtype,u8 genmask)7478 static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table,
7479 const struct nlattr *nla,
7480 u32 objtype, u8 genmask)
7481 {
7482 struct nft_object *obj;
7483
7484 list_for_each_entry(obj, &table->objects, list) {
7485 if (be64_to_cpu(nla_get_be64(nla)) == obj->handle &&
7486 objtype == obj->ops->type->type &&
7487 nft_active_genmask(obj, genmask))
7488 return obj;
7489 }
7490 return ERR_PTR(-ENOENT);
7491 }
7492
7493 static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = {
7494 [NFTA_OBJ_TABLE] = { .type = NLA_STRING,
7495 .len = NFT_TABLE_MAXNAMELEN - 1 },
7496 [NFTA_OBJ_NAME] = { .type = NLA_STRING,
7497 .len = NFT_OBJ_MAXNAMELEN - 1 },
7498 [NFTA_OBJ_TYPE] = { .type = NLA_U32 },
7499 [NFTA_OBJ_DATA] = { .type = NLA_NESTED },
7500 [NFTA_OBJ_HANDLE] = { .type = NLA_U64},
7501 [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY,
7502 .len = NFT_USERDATA_MAXLEN },
7503 };
7504
nft_obj_init(const struct nft_ctx * ctx,const struct nft_object_type * type,const struct nlattr * attr)7505 static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
7506 const struct nft_object_type *type,
7507 const struct nlattr *attr)
7508 {
7509 struct nlattr **tb;
7510 const struct nft_object_ops *ops;
7511 struct nft_object *obj;
7512 int err = -ENOMEM;
7513
7514 tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL);
7515 if (!tb)
7516 goto err1;
7517
7518 if (attr) {
7519 err = nla_parse_nested_deprecated(tb, type->maxattr, attr,
7520 type->policy, NULL);
7521 if (err < 0)
7522 goto err2;
7523 } else {
7524 memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1));
7525 }
7526
7527 if (type->select_ops) {
7528 ops = type->select_ops(ctx, (const struct nlattr * const *)tb);
7529 if (IS_ERR(ops)) {
7530 err = PTR_ERR(ops);
7531 goto err2;
7532 }
7533 } else {
7534 ops = type->ops;
7535 }
7536
7537 err = -ENOMEM;
7538 obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL_ACCOUNT);
7539 if (!obj)
7540 goto err2;
7541
7542 err = ops->init(ctx, (const struct nlattr * const *)tb, obj);
7543 if (err < 0)
7544 goto err3;
7545
7546 obj->ops = ops;
7547
7548 kfree(tb);
7549 return obj;
7550 err3:
7551 kfree(obj);
7552 err2:
7553 kfree(tb);
7554 err1:
7555 return ERR_PTR(err);
7556 }
7557
nft_object_dump(struct sk_buff * skb,unsigned int attr,struct nft_object * obj,bool reset)7558 static int nft_object_dump(struct sk_buff *skb, unsigned int attr,
7559 struct nft_object *obj, bool reset)
7560 {
7561 struct nlattr *nest;
7562
7563 nest = nla_nest_start_noflag(skb, attr);
7564 if (!nest)
7565 goto nla_put_failure;
7566 if (obj->ops->dump(skb, obj, reset) < 0)
7567 goto nla_put_failure;
7568 nla_nest_end(skb, nest);
7569 return 0;
7570
7571 nla_put_failure:
7572 return -1;
7573 }
7574
__nft_obj_type_get(u32 objtype,u8 family)7575 static const struct nft_object_type *__nft_obj_type_get(u32 objtype, u8 family)
7576 {
7577 const struct nft_object_type *type;
7578
7579 list_for_each_entry_rcu(type, &nf_tables_objects, list) {
7580 if (type->family != NFPROTO_UNSPEC &&
7581 type->family != family)
7582 continue;
7583
7584 if (objtype == type->type)
7585 return type;
7586 }
7587 return NULL;
7588 }
7589
7590 static const struct nft_object_type *
nft_obj_type_get(struct net * net,u32 objtype,u8 family)7591 nft_obj_type_get(struct net *net, u32 objtype, u8 family)
7592 {
7593 const struct nft_object_type *type;
7594
7595 rcu_read_lock();
7596 type = __nft_obj_type_get(objtype, family);
7597 if (type != NULL && try_module_get(type->owner)) {
7598 rcu_read_unlock();
7599 return type;
7600 }
7601 rcu_read_unlock();
7602
7603 lockdep_nfnl_nft_mutex_not_held();
7604 #ifdef CONFIG_MODULES
7605 if (type == NULL) {
7606 if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN)
7607 return ERR_PTR(-EAGAIN);
7608 }
7609 #endif
7610 return ERR_PTR(-ENOENT);
7611 }
7612
nf_tables_updobj(const struct nft_ctx * ctx,const struct nft_object_type * type,const struct nlattr * attr,struct nft_object * obj)7613 static int nf_tables_updobj(const struct nft_ctx *ctx,
7614 const struct nft_object_type *type,
7615 const struct nlattr *attr,
7616 struct nft_object *obj)
7617 {
7618 struct nft_object *newobj;
7619 struct nft_trans *trans;
7620 int err = -ENOMEM;
7621
7622 /* caller must have obtained type->owner reference. */
7623 trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ,
7624 sizeof(struct nft_trans_obj));
7625 if (!trans)
7626 goto err_trans;
7627
7628 newobj = nft_obj_init(ctx, type, attr);
7629 if (IS_ERR(newobj)) {
7630 err = PTR_ERR(newobj);
7631 goto err_free_trans;
7632 }
7633
7634 nft_trans_obj(trans) = obj;
7635 nft_trans_obj_update(trans) = true;
7636 nft_trans_obj_newobj(trans) = newobj;
7637 nft_trans_commit_list_add_tail(ctx->net, trans);
7638
7639 return 0;
7640
7641 err_free_trans:
7642 kfree(trans);
7643 err_trans:
7644 module_put(type->owner);
7645 return err;
7646 }
7647
nf_tables_newobj(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])7648 static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info,
7649 const struct nlattr * const nla[])
7650 {
7651 struct netlink_ext_ack *extack = info->extack;
7652 u8 genmask = nft_genmask_next(info->net);
7653 u8 family = info->nfmsg->nfgen_family;
7654 const struct nft_object_type *type;
7655 struct net *net = info->net;
7656 struct nft_table *table;
7657 struct nft_object *obj;
7658 struct nft_ctx ctx;
7659 u32 objtype;
7660 int err;
7661
7662 if (!nla[NFTA_OBJ_TYPE] ||
7663 !nla[NFTA_OBJ_NAME] ||
7664 !nla[NFTA_OBJ_DATA])
7665 return -EINVAL;
7666
7667 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask,
7668 NETLINK_CB(skb).portid);
7669 if (IS_ERR(table)) {
7670 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
7671 return PTR_ERR(table);
7672 }
7673
7674 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7675 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
7676 if (IS_ERR(obj)) {
7677 err = PTR_ERR(obj);
7678 if (err != -ENOENT) {
7679 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
7680 return err;
7681 }
7682 } else {
7683 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
7684 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
7685 return -EEXIST;
7686 }
7687 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
7688 return -EOPNOTSUPP;
7689
7690 if (!obj->ops->update)
7691 return 0;
7692
7693 type = nft_obj_type_get(net, objtype, family);
7694 if (WARN_ON_ONCE(IS_ERR(type)))
7695 return PTR_ERR(type);
7696
7697 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7698
7699 /* type->owner reference is put when transaction object is released. */
7700 return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj);
7701 }
7702
7703 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7704
7705 if (!nft_use_inc(&table->use))
7706 return -EMFILE;
7707
7708 type = nft_obj_type_get(net, objtype, family);
7709 if (IS_ERR(type)) {
7710 err = PTR_ERR(type);
7711 goto err_type;
7712 }
7713
7714 obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]);
7715 if (IS_ERR(obj)) {
7716 err = PTR_ERR(obj);
7717 goto err_init;
7718 }
7719 obj->key.table = table;
7720 obj->handle = nf_tables_alloc_handle(table);
7721
7722 obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL_ACCOUNT);
7723 if (!obj->key.name) {
7724 err = -ENOMEM;
7725 goto err_strdup;
7726 }
7727
7728 if (nla[NFTA_OBJ_USERDATA]) {
7729 obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL_ACCOUNT);
7730 if (obj->udata == NULL)
7731 goto err_userdata;
7732
7733 obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]);
7734 }
7735
7736 err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj);
7737 if (err < 0)
7738 goto err_trans;
7739
7740 err = rhltable_insert(&nft_objname_ht, &obj->rhlhead,
7741 nft_objname_ht_params);
7742 if (err < 0)
7743 goto err_obj_ht;
7744
7745 list_add_tail_rcu(&obj->list, &table->objects);
7746
7747 return 0;
7748 err_obj_ht:
7749 /* queued in transaction log */
7750 INIT_LIST_HEAD(&obj->list);
7751 return err;
7752 err_trans:
7753 kfree(obj->udata);
7754 err_userdata:
7755 kfree(obj->key.name);
7756 err_strdup:
7757 if (obj->ops->destroy)
7758 obj->ops->destroy(&ctx, obj);
7759 kfree(obj);
7760 err_init:
7761 module_put(type->owner);
7762 err_type:
7763 nft_use_dec_restore(&table->use);
7764
7765 return err;
7766 }
7767
nf_tables_fill_obj_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq,int event,u32 flags,int family,const struct nft_table * table,struct nft_object * obj,bool reset)7768 static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net,
7769 u32 portid, u32 seq, int event, u32 flags,
7770 int family, const struct nft_table *table,
7771 struct nft_object *obj, bool reset)
7772 {
7773 struct nlmsghdr *nlh;
7774
7775 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
7776 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
7777 NFNETLINK_V0, nft_base_seq(net));
7778 if (!nlh)
7779 goto nla_put_failure;
7780
7781 if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) ||
7782 nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) ||
7783 nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle),
7784 NFTA_OBJ_PAD))
7785 goto nla_put_failure;
7786
7787 if (event == NFT_MSG_DELOBJ) {
7788 nlmsg_end(skb, nlh);
7789 return 0;
7790 }
7791
7792 if (nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) ||
7793 nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) ||
7794 nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset))
7795 goto nla_put_failure;
7796
7797 if (obj->udata &&
7798 nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata))
7799 goto nla_put_failure;
7800
7801 nlmsg_end(skb, nlh);
7802 return 0;
7803
7804 nla_put_failure:
7805 nlmsg_trim(skb, nlh);
7806 return -1;
7807 }
7808
audit_log_obj_reset(const struct nft_table * table,unsigned int base_seq,unsigned int nentries)7809 static void audit_log_obj_reset(const struct nft_table *table,
7810 unsigned int base_seq, unsigned int nentries)
7811 {
7812 char *buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, base_seq);
7813
7814 audit_log_nfcfg(buf, table->family, nentries,
7815 AUDIT_NFT_OP_OBJ_RESET, GFP_ATOMIC);
7816 kfree(buf);
7817 }
7818
7819 struct nft_obj_dump_ctx {
7820 unsigned int s_idx;
7821 char *table;
7822 u32 type;
7823 bool reset;
7824 };
7825
nf_tables_dump_obj(struct sk_buff * skb,struct netlink_callback * cb)7826 static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)
7827 {
7828 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
7829 struct nft_obj_dump_ctx *ctx = (void *)cb->ctx;
7830 struct net *net = sock_net(skb->sk);
7831 int family = nfmsg->nfgen_family;
7832 struct nftables_pernet *nft_net;
7833 const struct nft_table *table;
7834 unsigned int entries = 0;
7835 struct nft_object *obj;
7836 unsigned int idx = 0;
7837 int rc = 0;
7838
7839 rcu_read_lock();
7840 nft_net = nft_pernet(net);
7841 cb->seq = READ_ONCE(nft_net->base_seq);
7842
7843 list_for_each_entry_rcu(table, &nft_net->tables, list) {
7844 if (family != NFPROTO_UNSPEC && family != table->family)
7845 continue;
7846
7847 entries = 0;
7848 list_for_each_entry_rcu(obj, &table->objects, list) {
7849 if (!nft_is_active(net, obj))
7850 goto cont;
7851 if (idx < ctx->s_idx)
7852 goto cont;
7853 if (ctx->table && strcmp(ctx->table, table->name))
7854 goto cont;
7855 if (ctx->type != NFT_OBJECT_UNSPEC &&
7856 obj->ops->type->type != ctx->type)
7857 goto cont;
7858
7859 rc = nf_tables_fill_obj_info(skb, net,
7860 NETLINK_CB(cb->skb).portid,
7861 cb->nlh->nlmsg_seq,
7862 NFT_MSG_NEWOBJ,
7863 NLM_F_MULTI | NLM_F_APPEND,
7864 table->family, table,
7865 obj, ctx->reset);
7866 if (rc < 0)
7867 break;
7868
7869 entries++;
7870 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
7871 cont:
7872 idx++;
7873 }
7874 if (ctx->reset && entries)
7875 audit_log_obj_reset(table, nft_net->base_seq, entries);
7876 if (rc < 0)
7877 break;
7878 }
7879 rcu_read_unlock();
7880
7881 ctx->s_idx = idx;
7882 return skb->len;
7883 }
7884
nf_tables_dumpreset_obj(struct sk_buff * skb,struct netlink_callback * cb)7885 static int nf_tables_dumpreset_obj(struct sk_buff *skb,
7886 struct netlink_callback *cb)
7887 {
7888 struct nftables_pernet *nft_net = nft_pernet(sock_net(skb->sk));
7889 int ret;
7890
7891 mutex_lock(&nft_net->commit_mutex);
7892 ret = nf_tables_dump_obj(skb, cb);
7893 mutex_unlock(&nft_net->commit_mutex);
7894
7895 return ret;
7896 }
7897
nf_tables_dump_obj_start(struct netlink_callback * cb)7898 static int nf_tables_dump_obj_start(struct netlink_callback *cb)
7899 {
7900 struct nft_obj_dump_ctx *ctx = (void *)cb->ctx;
7901 const struct nlattr * const *nla = cb->data;
7902
7903 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
7904
7905 if (nla[NFTA_OBJ_TABLE]) {
7906 ctx->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC);
7907 if (!ctx->table)
7908 return -ENOMEM;
7909 }
7910
7911 if (nla[NFTA_OBJ_TYPE])
7912 ctx->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7913
7914 return 0;
7915 }
7916
nf_tables_dumpreset_obj_start(struct netlink_callback * cb)7917 static int nf_tables_dumpreset_obj_start(struct netlink_callback *cb)
7918 {
7919 struct nft_obj_dump_ctx *ctx = (void *)cb->ctx;
7920
7921 ctx->reset = true;
7922
7923 return nf_tables_dump_obj_start(cb);
7924 }
7925
nf_tables_dump_obj_done(struct netlink_callback * cb)7926 static int nf_tables_dump_obj_done(struct netlink_callback *cb)
7927 {
7928 struct nft_obj_dump_ctx *ctx = (void *)cb->ctx;
7929
7930 kfree(ctx->table);
7931
7932 return 0;
7933 }
7934
7935 /* Caller must hold rcu read lock or transaction mutex */
7936 static struct sk_buff *
nf_tables_getobj_single(u32 portid,const struct nfnl_info * info,const struct nlattr * const nla[],bool reset)7937 nf_tables_getobj_single(u32 portid, const struct nfnl_info *info,
7938 const struct nlattr * const nla[], bool reset)
7939 {
7940 struct netlink_ext_ack *extack = info->extack;
7941 u8 genmask = nft_genmask_cur(info->net);
7942 u8 family = info->nfmsg->nfgen_family;
7943 const struct nft_table *table;
7944 struct net *net = info->net;
7945 struct nft_object *obj;
7946 struct sk_buff *skb2;
7947 u32 objtype;
7948 int err;
7949
7950 if (!nla[NFTA_OBJ_NAME] ||
7951 !nla[NFTA_OBJ_TYPE])
7952 return ERR_PTR(-EINVAL);
7953
7954 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, 0);
7955 if (IS_ERR(table)) {
7956 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
7957 return ERR_CAST(table);
7958 }
7959
7960 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7961 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
7962 if (IS_ERR(obj)) {
7963 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
7964 return ERR_CAST(obj);
7965 }
7966
7967 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
7968 if (!skb2)
7969 return ERR_PTR(-ENOMEM);
7970
7971 err = nf_tables_fill_obj_info(skb2, net, portid,
7972 info->nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0,
7973 family, table, obj, reset);
7974 if (err < 0) {
7975 kfree_skb(skb2);
7976 return ERR_PTR(err);
7977 }
7978
7979 return skb2;
7980 }
7981
nf_tables_getobj(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])7982 static int nf_tables_getobj(struct sk_buff *skb, const struct nfnl_info *info,
7983 const struct nlattr * const nla[])
7984 {
7985 u32 portid = NETLINK_CB(skb).portid;
7986 struct sk_buff *skb2;
7987
7988 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
7989 struct netlink_dump_control c = {
7990 .start = nf_tables_dump_obj_start,
7991 .dump = nf_tables_dump_obj,
7992 .done = nf_tables_dump_obj_done,
7993 .module = THIS_MODULE,
7994 .data = (void *)nla,
7995 };
7996
7997 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
7998 }
7999
8000 skb2 = nf_tables_getobj_single(portid, info, nla, false);
8001 if (IS_ERR(skb2))
8002 return PTR_ERR(skb2);
8003
8004 return nfnetlink_unicast(skb2, info->net, portid);
8005 }
8006
nf_tables_getobj_reset(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8007 static int nf_tables_getobj_reset(struct sk_buff *skb,
8008 const struct nfnl_info *info,
8009 const struct nlattr * const nla[])
8010 {
8011 struct nftables_pernet *nft_net = nft_pernet(info->net);
8012 u32 portid = NETLINK_CB(skb).portid;
8013 struct net *net = info->net;
8014 struct sk_buff *skb2;
8015 char *buf;
8016
8017 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
8018 struct netlink_dump_control c = {
8019 .start = nf_tables_dumpreset_obj_start,
8020 .dump = nf_tables_dumpreset_obj,
8021 .done = nf_tables_dump_obj_done,
8022 .module = THIS_MODULE,
8023 .data = (void *)nla,
8024 };
8025
8026 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
8027 }
8028
8029 if (!try_module_get(THIS_MODULE))
8030 return -EINVAL;
8031 rcu_read_unlock();
8032 mutex_lock(&nft_net->commit_mutex);
8033 skb2 = nf_tables_getobj_single(portid, info, nla, true);
8034 mutex_unlock(&nft_net->commit_mutex);
8035 rcu_read_lock();
8036 module_put(THIS_MODULE);
8037
8038 if (IS_ERR(skb2))
8039 return PTR_ERR(skb2);
8040
8041 buf = kasprintf(GFP_ATOMIC, "%.*s:%u",
8042 nla_len(nla[NFTA_OBJ_TABLE]),
8043 (char *)nla_data(nla[NFTA_OBJ_TABLE]),
8044 nft_net->base_seq);
8045 audit_log_nfcfg(buf, info->nfmsg->nfgen_family, 1,
8046 AUDIT_NFT_OP_OBJ_RESET, GFP_ATOMIC);
8047 kfree(buf);
8048
8049 return nfnetlink_unicast(skb2, net, portid);
8050 }
8051
nft_obj_destroy(const struct nft_ctx * ctx,struct nft_object * obj)8052 static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj)
8053 {
8054 if (obj->ops->destroy)
8055 obj->ops->destroy(ctx, obj);
8056
8057 module_put(obj->ops->type->owner);
8058 kfree(obj->key.name);
8059 kfree(obj->udata);
8060 kfree(obj);
8061 }
8062
nf_tables_delobj(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8063 static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info,
8064 const struct nlattr * const nla[])
8065 {
8066 struct netlink_ext_ack *extack = info->extack;
8067 u8 genmask = nft_genmask_next(info->net);
8068 u8 family = info->nfmsg->nfgen_family;
8069 struct net *net = info->net;
8070 const struct nlattr *attr;
8071 struct nft_table *table;
8072 struct nft_object *obj;
8073 struct nft_ctx ctx;
8074 u32 objtype;
8075
8076 if (!nla[NFTA_OBJ_TYPE] ||
8077 (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE]))
8078 return -EINVAL;
8079
8080 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask,
8081 NETLINK_CB(skb).portid);
8082 if (IS_ERR(table)) {
8083 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
8084 return PTR_ERR(table);
8085 }
8086
8087 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
8088 if (nla[NFTA_OBJ_HANDLE]) {
8089 attr = nla[NFTA_OBJ_HANDLE];
8090 obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask);
8091 } else {
8092 attr = nla[NFTA_OBJ_NAME];
8093 obj = nft_obj_lookup(net, table, attr, objtype, genmask);
8094 }
8095
8096 if (IS_ERR(obj)) {
8097 if (PTR_ERR(obj) == -ENOENT &&
8098 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYOBJ)
8099 return 0;
8100
8101 NL_SET_BAD_ATTR(extack, attr);
8102 return PTR_ERR(obj);
8103 }
8104 if (obj->use > 0) {
8105 NL_SET_BAD_ATTR(extack, attr);
8106 return -EBUSY;
8107 }
8108
8109 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
8110
8111 return nft_delobj(&ctx, obj);
8112 }
8113
8114 static void
__nft_obj_notify(struct net * net,const struct nft_table * table,struct nft_object * obj,u32 portid,u32 seq,int event,u16 flags,int family,int report,gfp_t gfp)8115 __nft_obj_notify(struct net *net, const struct nft_table *table,
8116 struct nft_object *obj, u32 portid, u32 seq, int event,
8117 u16 flags, int family, int report, gfp_t gfp)
8118 {
8119 struct nftables_pernet *nft_net = nft_pernet(net);
8120 struct sk_buff *skb;
8121 int err;
8122
8123 if (!report &&
8124 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
8125 return;
8126
8127 skb = nlmsg_new(NLMSG_GOODSIZE, gfp);
8128 if (skb == NULL)
8129 goto err;
8130
8131 err = nf_tables_fill_obj_info(skb, net, portid, seq, event,
8132 flags & (NLM_F_CREATE | NLM_F_EXCL),
8133 family, table, obj, false);
8134 if (err < 0) {
8135 kfree_skb(skb);
8136 goto err;
8137 }
8138
8139 nft_notify_enqueue(skb, report, &nft_net->notify_list);
8140 return;
8141 err:
8142 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
8143 }
8144
nft_obj_notify(struct net * net,const struct nft_table * table,struct nft_object * obj,u32 portid,u32 seq,int event,u16 flags,int family,int report,gfp_t gfp)8145 void nft_obj_notify(struct net *net, const struct nft_table *table,
8146 struct nft_object *obj, u32 portid, u32 seq, int event,
8147 u16 flags, int family, int report, gfp_t gfp)
8148 {
8149 struct nftables_pernet *nft_net = nft_pernet(net);
8150 char *buf = kasprintf(gfp, "%s:%u",
8151 table->name, nft_net->base_seq);
8152
8153 audit_log_nfcfg(buf,
8154 family,
8155 obj->handle,
8156 event == NFT_MSG_NEWOBJ ?
8157 AUDIT_NFT_OP_OBJ_REGISTER :
8158 AUDIT_NFT_OP_OBJ_UNREGISTER,
8159 gfp);
8160 kfree(buf);
8161
8162 __nft_obj_notify(net, table, obj, portid, seq, event,
8163 flags, family, report, gfp);
8164 }
8165 EXPORT_SYMBOL_GPL(nft_obj_notify);
8166
nf_tables_obj_notify(const struct nft_ctx * ctx,struct nft_object * obj,int event)8167 static void nf_tables_obj_notify(const struct nft_ctx *ctx,
8168 struct nft_object *obj, int event)
8169 {
8170 __nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid,
8171 ctx->seq, event, ctx->flags, ctx->family,
8172 ctx->report, GFP_KERNEL);
8173 }
8174
8175 /*
8176 * Flow tables
8177 */
nft_register_flowtable_type(struct nf_flowtable_type * type)8178 void nft_register_flowtable_type(struct nf_flowtable_type *type)
8179 {
8180 nfnl_lock(NFNL_SUBSYS_NFTABLES);
8181 list_add_tail_rcu(&type->list, &nf_tables_flowtables);
8182 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
8183 }
8184 EXPORT_SYMBOL_GPL(nft_register_flowtable_type);
8185
nft_unregister_flowtable_type(struct nf_flowtable_type * type)8186 void nft_unregister_flowtable_type(struct nf_flowtable_type *type)
8187 {
8188 nfnl_lock(NFNL_SUBSYS_NFTABLES);
8189 list_del_rcu(&type->list);
8190 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
8191 }
8192 EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type);
8193
8194 static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = {
8195 [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING,
8196 .len = NFT_NAME_MAXLEN - 1 },
8197 [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING,
8198 .len = NFT_NAME_MAXLEN - 1 },
8199 [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED },
8200 [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 },
8201 [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 },
8202 };
8203
nft_flowtable_lookup(const struct nft_table * table,const struct nlattr * nla,u8 genmask)8204 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
8205 const struct nlattr *nla, u8 genmask)
8206 {
8207 struct nft_flowtable *flowtable;
8208
8209 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
8210 if (!nla_strcmp(nla, flowtable->name) &&
8211 nft_active_genmask(flowtable, genmask))
8212 return flowtable;
8213 }
8214 return ERR_PTR(-ENOENT);
8215 }
8216 EXPORT_SYMBOL_GPL(nft_flowtable_lookup);
8217
nf_tables_deactivate_flowtable(const struct nft_ctx * ctx,struct nft_flowtable * flowtable,enum nft_trans_phase phase)8218 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
8219 struct nft_flowtable *flowtable,
8220 enum nft_trans_phase phase)
8221 {
8222 switch (phase) {
8223 case NFT_TRANS_PREPARE_ERROR:
8224 case NFT_TRANS_PREPARE:
8225 case NFT_TRANS_ABORT:
8226 case NFT_TRANS_RELEASE:
8227 nft_use_dec(&flowtable->use);
8228 fallthrough;
8229 default:
8230 return;
8231 }
8232 }
8233 EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable);
8234
8235 static struct nft_flowtable *
nft_flowtable_lookup_byhandle(const struct nft_table * table,const struct nlattr * nla,u8 genmask)8236 nft_flowtable_lookup_byhandle(const struct nft_table *table,
8237 const struct nlattr *nla, u8 genmask)
8238 {
8239 struct nft_flowtable *flowtable;
8240
8241 list_for_each_entry(flowtable, &table->flowtables, list) {
8242 if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle &&
8243 nft_active_genmask(flowtable, genmask))
8244 return flowtable;
8245 }
8246 return ERR_PTR(-ENOENT);
8247 }
8248
8249 struct nft_flowtable_hook {
8250 u32 num;
8251 int priority;
8252 struct list_head list;
8253 };
8254
8255 static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = {
8256 [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 },
8257 [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 },
8258 [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED },
8259 };
8260
nft_flowtable_parse_hook(const struct nft_ctx * ctx,const struct nlattr * const nla[],struct nft_flowtable_hook * flowtable_hook,struct nft_flowtable * flowtable,struct netlink_ext_ack * extack,bool add)8261 static int nft_flowtable_parse_hook(const struct nft_ctx *ctx,
8262 const struct nlattr * const nla[],
8263 struct nft_flowtable_hook *flowtable_hook,
8264 struct nft_flowtable *flowtable,
8265 struct netlink_ext_ack *extack, bool add)
8266 {
8267 struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1];
8268 struct nft_hook *hook;
8269 int hooknum, priority;
8270 int err;
8271
8272 INIT_LIST_HEAD(&flowtable_hook->list);
8273
8274 err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX,
8275 nla[NFTA_FLOWTABLE_HOOK],
8276 nft_flowtable_hook_policy, NULL);
8277 if (err < 0)
8278 return err;
8279
8280 if (add) {
8281 if (!tb[NFTA_FLOWTABLE_HOOK_NUM] ||
8282 !tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) {
8283 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
8284 return -ENOENT;
8285 }
8286
8287 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
8288 if (hooknum != NF_NETDEV_INGRESS)
8289 return -EOPNOTSUPP;
8290
8291 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
8292
8293 flowtable_hook->priority = priority;
8294 flowtable_hook->num = hooknum;
8295 } else {
8296 if (tb[NFTA_FLOWTABLE_HOOK_NUM]) {
8297 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
8298 if (hooknum != flowtable->hooknum)
8299 return -EOPNOTSUPP;
8300 }
8301
8302 if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) {
8303 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
8304 if (priority != flowtable->data.priority)
8305 return -EOPNOTSUPP;
8306 }
8307
8308 flowtable_hook->priority = flowtable->data.priority;
8309 flowtable_hook->num = flowtable->hooknum;
8310 }
8311
8312 if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) {
8313 err = nf_tables_parse_netdev_hooks(ctx->net,
8314 tb[NFTA_FLOWTABLE_HOOK_DEVS],
8315 &flowtable_hook->list,
8316 extack);
8317 if (err < 0)
8318 return err;
8319 }
8320
8321 list_for_each_entry(hook, &flowtable_hook->list, list) {
8322 hook->ops.pf = NFPROTO_NETDEV;
8323 hook->ops.hooknum = flowtable_hook->num;
8324 hook->ops.priority = flowtable_hook->priority;
8325 hook->ops.priv = &flowtable->data;
8326 hook->ops.hook = flowtable->data.type->hook;
8327 }
8328
8329 return err;
8330 }
8331
8332 /* call under rcu_read_lock */
__nft_flowtable_type_get(u8 family)8333 static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family)
8334 {
8335 const struct nf_flowtable_type *type;
8336
8337 list_for_each_entry_rcu(type, &nf_tables_flowtables, list) {
8338 if (family == type->family)
8339 return type;
8340 }
8341 return NULL;
8342 }
8343
8344 static const struct nf_flowtable_type *
nft_flowtable_type_get(struct net * net,u8 family)8345 nft_flowtable_type_get(struct net *net, u8 family)
8346 {
8347 const struct nf_flowtable_type *type;
8348
8349 rcu_read_lock();
8350 type = __nft_flowtable_type_get(family);
8351 if (type != NULL && try_module_get(type->owner)) {
8352 rcu_read_unlock();
8353 return type;
8354 }
8355 rcu_read_unlock();
8356
8357 lockdep_nfnl_nft_mutex_not_held();
8358 #ifdef CONFIG_MODULES
8359 if (type == NULL) {
8360 if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN)
8361 return ERR_PTR(-EAGAIN);
8362 }
8363 #endif
8364 return ERR_PTR(-ENOENT);
8365 }
8366
8367 /* Only called from error and netdev event paths. */
nft_unregister_flowtable_hook(struct net * net,struct nft_flowtable * flowtable,struct nft_hook * hook)8368 static void nft_unregister_flowtable_hook(struct net *net,
8369 struct nft_flowtable *flowtable,
8370 struct nft_hook *hook)
8371 {
8372 nf_unregister_net_hook(net, &hook->ops);
8373 flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
8374 FLOW_BLOCK_UNBIND);
8375 }
8376
__nft_unregister_flowtable_net_hooks(struct net * net,struct list_head * hook_list,bool release_netdev)8377 static void __nft_unregister_flowtable_net_hooks(struct net *net,
8378 struct list_head *hook_list,
8379 bool release_netdev)
8380 {
8381 struct nft_hook *hook, *next;
8382
8383 list_for_each_entry_safe(hook, next, hook_list, list) {
8384 nf_unregister_net_hook(net, &hook->ops);
8385 if (release_netdev) {
8386 list_del(&hook->list);
8387 kfree_rcu(hook, rcu);
8388 }
8389 }
8390 }
8391
nft_unregister_flowtable_net_hooks(struct net * net,struct list_head * hook_list)8392 static void nft_unregister_flowtable_net_hooks(struct net *net,
8393 struct list_head *hook_list)
8394 {
8395 __nft_unregister_flowtable_net_hooks(net, hook_list, false);
8396 }
8397
nft_register_flowtable_net_hooks(struct net * net,struct nft_table * table,struct list_head * hook_list,struct nft_flowtable * flowtable)8398 static int nft_register_flowtable_net_hooks(struct net *net,
8399 struct nft_table *table,
8400 struct list_head *hook_list,
8401 struct nft_flowtable *flowtable)
8402 {
8403 struct nft_hook *hook, *hook2, *next;
8404 struct nft_flowtable *ft;
8405 int err, i = 0;
8406
8407 list_for_each_entry(hook, hook_list, list) {
8408 list_for_each_entry(ft, &table->flowtables, list) {
8409 if (!nft_is_active_next(net, ft))
8410 continue;
8411
8412 list_for_each_entry(hook2, &ft->hook_list, list) {
8413 if (hook->ops.dev == hook2->ops.dev &&
8414 hook->ops.pf == hook2->ops.pf) {
8415 err = -EEXIST;
8416 goto err_unregister_net_hooks;
8417 }
8418 }
8419 }
8420
8421 err = flowtable->data.type->setup(&flowtable->data,
8422 hook->ops.dev,
8423 FLOW_BLOCK_BIND);
8424 if (err < 0)
8425 goto err_unregister_net_hooks;
8426
8427 err = nf_register_net_hook(net, &hook->ops);
8428 if (err < 0) {
8429 flowtable->data.type->setup(&flowtable->data,
8430 hook->ops.dev,
8431 FLOW_BLOCK_UNBIND);
8432 goto err_unregister_net_hooks;
8433 }
8434
8435 i++;
8436 }
8437
8438 return 0;
8439
8440 err_unregister_net_hooks:
8441 list_for_each_entry_safe(hook, next, hook_list, list) {
8442 if (i-- <= 0)
8443 break;
8444
8445 nft_unregister_flowtable_hook(net, flowtable, hook);
8446 list_del_rcu(&hook->list);
8447 kfree_rcu(hook, rcu);
8448 }
8449
8450 return err;
8451 }
8452
nft_hooks_destroy(struct list_head * hook_list)8453 static void nft_hooks_destroy(struct list_head *hook_list)
8454 {
8455 struct nft_hook *hook, *next;
8456
8457 list_for_each_entry_safe(hook, next, hook_list, list) {
8458 list_del_rcu(&hook->list);
8459 kfree_rcu(hook, rcu);
8460 }
8461 }
8462
nft_flowtable_update(struct nft_ctx * ctx,const struct nlmsghdr * nlh,struct nft_flowtable * flowtable,struct netlink_ext_ack * extack)8463 static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh,
8464 struct nft_flowtable *flowtable,
8465 struct netlink_ext_ack *extack)
8466 {
8467 const struct nlattr * const *nla = ctx->nla;
8468 struct nft_flowtable_hook flowtable_hook;
8469 struct nft_hook *hook, *next;
8470 struct nft_trans *trans;
8471 bool unregister = false;
8472 u32 flags;
8473 int err;
8474
8475 err = nft_flowtable_parse_hook(ctx, nla, &flowtable_hook, flowtable,
8476 extack, false);
8477 if (err < 0)
8478 return err;
8479
8480 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
8481 if (nft_hook_list_find(&flowtable->hook_list, hook)) {
8482 list_del(&hook->list);
8483 kfree(hook);
8484 }
8485 }
8486
8487 if (nla[NFTA_FLOWTABLE_FLAGS]) {
8488 flags = ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
8489 if (flags & ~NFT_FLOWTABLE_MASK) {
8490 err = -EOPNOTSUPP;
8491 goto err_flowtable_update_hook;
8492 }
8493 if ((flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD) ^
8494 (flags & NFT_FLOWTABLE_HW_OFFLOAD)) {
8495 err = -EOPNOTSUPP;
8496 goto err_flowtable_update_hook;
8497 }
8498 } else {
8499 flags = flowtable->data.flags;
8500 }
8501
8502 err = nft_register_flowtable_net_hooks(ctx->net, ctx->table,
8503 &flowtable_hook.list, flowtable);
8504 if (err < 0)
8505 goto err_flowtable_update_hook;
8506
8507 trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE,
8508 sizeof(struct nft_trans_flowtable));
8509 if (!trans) {
8510 unregister = true;
8511 err = -ENOMEM;
8512 goto err_flowtable_update_hook;
8513 }
8514
8515 nft_trans_flowtable_flags(trans) = flags;
8516 nft_trans_flowtable(trans) = flowtable;
8517 nft_trans_flowtable_update(trans) = true;
8518 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
8519 list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans));
8520
8521 nft_trans_commit_list_add_tail(ctx->net, trans);
8522
8523 return 0;
8524
8525 err_flowtable_update_hook:
8526 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
8527 if (unregister)
8528 nft_unregister_flowtable_hook(ctx->net, flowtable, hook);
8529 list_del_rcu(&hook->list);
8530 kfree_rcu(hook, rcu);
8531 }
8532
8533 return err;
8534
8535 }
8536
nf_tables_newflowtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8537 static int nf_tables_newflowtable(struct sk_buff *skb,
8538 const struct nfnl_info *info,
8539 const struct nlattr * const nla[])
8540 {
8541 struct netlink_ext_ack *extack = info->extack;
8542 struct nft_flowtable_hook flowtable_hook;
8543 u8 genmask = nft_genmask_next(info->net);
8544 u8 family = info->nfmsg->nfgen_family;
8545 const struct nf_flowtable_type *type;
8546 struct nft_flowtable *flowtable;
8547 struct net *net = info->net;
8548 struct nft_table *table;
8549 struct nft_trans *trans;
8550 struct nft_ctx ctx;
8551 int err;
8552
8553 if (!nla[NFTA_FLOWTABLE_TABLE] ||
8554 !nla[NFTA_FLOWTABLE_NAME] ||
8555 !nla[NFTA_FLOWTABLE_HOOK])
8556 return -EINVAL;
8557
8558 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
8559 genmask, NETLINK_CB(skb).portid);
8560 if (IS_ERR(table)) {
8561 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
8562 return PTR_ERR(table);
8563 }
8564
8565 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
8566 genmask);
8567 if (IS_ERR(flowtable)) {
8568 err = PTR_ERR(flowtable);
8569 if (err != -ENOENT) {
8570 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
8571 return err;
8572 }
8573 } else {
8574 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
8575 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
8576 return -EEXIST;
8577 }
8578
8579 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
8580
8581 return nft_flowtable_update(&ctx, info->nlh, flowtable, extack);
8582 }
8583
8584 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
8585
8586 if (!nft_use_inc(&table->use))
8587 return -EMFILE;
8588
8589 flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL_ACCOUNT);
8590 if (!flowtable) {
8591 err = -ENOMEM;
8592 goto flowtable_alloc;
8593 }
8594
8595 flowtable->table = table;
8596 flowtable->handle = nf_tables_alloc_handle(table);
8597 INIT_LIST_HEAD(&flowtable->hook_list);
8598
8599 flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL_ACCOUNT);
8600 if (!flowtable->name) {
8601 err = -ENOMEM;
8602 goto err1;
8603 }
8604
8605 type = nft_flowtable_type_get(net, family);
8606 if (IS_ERR(type)) {
8607 err = PTR_ERR(type);
8608 goto err2;
8609 }
8610
8611 if (nla[NFTA_FLOWTABLE_FLAGS]) {
8612 flowtable->data.flags =
8613 ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
8614 if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) {
8615 err = -EOPNOTSUPP;
8616 goto err3;
8617 }
8618 }
8619
8620 write_pnet(&flowtable->data.net, net);
8621 flowtable->data.type = type;
8622 err = type->init(&flowtable->data);
8623 if (err < 0)
8624 goto err3;
8625
8626 err = nft_flowtable_parse_hook(&ctx, nla, &flowtable_hook, flowtable,
8627 extack, true);
8628 if (err < 0)
8629 goto err_flowtable_parse_hooks;
8630
8631 list_splice(&flowtable_hook.list, &flowtable->hook_list);
8632 flowtable->data.priority = flowtable_hook.priority;
8633 flowtable->hooknum = flowtable_hook.num;
8634
8635 trans = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable);
8636 if (IS_ERR(trans)) {
8637 err = PTR_ERR(trans);
8638 goto err_flowtable_trans;
8639 }
8640
8641 /* This must be LAST to ensure no packets are walking over this flowtable. */
8642 err = nft_register_flowtable_net_hooks(ctx.net, table,
8643 &flowtable->hook_list,
8644 flowtable);
8645 if (err < 0)
8646 goto err_flowtable_hooks;
8647
8648 list_add_tail_rcu(&flowtable->list, &table->flowtables);
8649
8650 return 0;
8651
8652 err_flowtable_hooks:
8653 nft_trans_destroy(trans);
8654 err_flowtable_trans:
8655 nft_hooks_destroy(&flowtable->hook_list);
8656 err_flowtable_parse_hooks:
8657 flowtable->data.type->free(&flowtable->data);
8658 err3:
8659 module_put(type->owner);
8660 err2:
8661 kfree(flowtable->name);
8662 err1:
8663 kfree(flowtable);
8664 flowtable_alloc:
8665 nft_use_dec_restore(&table->use);
8666
8667 return err;
8668 }
8669
nft_flowtable_hook_release(struct nft_flowtable_hook * flowtable_hook)8670 static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook)
8671 {
8672 struct nft_hook *this, *next;
8673
8674 list_for_each_entry_safe(this, next, &flowtable_hook->list, list) {
8675 list_del(&this->list);
8676 kfree(this);
8677 }
8678 }
8679
nft_delflowtable_hook(struct nft_ctx * ctx,struct nft_flowtable * flowtable,struct netlink_ext_ack * extack)8680 static int nft_delflowtable_hook(struct nft_ctx *ctx,
8681 struct nft_flowtable *flowtable,
8682 struct netlink_ext_ack *extack)
8683 {
8684 const struct nlattr * const *nla = ctx->nla;
8685 struct nft_flowtable_hook flowtable_hook;
8686 LIST_HEAD(flowtable_del_list);
8687 struct nft_hook *this, *hook;
8688 struct nft_trans *trans;
8689 int err;
8690
8691 err = nft_flowtable_parse_hook(ctx, nla, &flowtable_hook, flowtable,
8692 extack, false);
8693 if (err < 0)
8694 return err;
8695
8696 list_for_each_entry(this, &flowtable_hook.list, list) {
8697 hook = nft_hook_list_find(&flowtable->hook_list, this);
8698 if (!hook) {
8699 err = -ENOENT;
8700 goto err_flowtable_del_hook;
8701 }
8702 list_move(&hook->list, &flowtable_del_list);
8703 }
8704
8705 trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE,
8706 sizeof(struct nft_trans_flowtable));
8707 if (!trans) {
8708 err = -ENOMEM;
8709 goto err_flowtable_del_hook;
8710 }
8711
8712 nft_trans_flowtable(trans) = flowtable;
8713 nft_trans_flowtable_update(trans) = true;
8714 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
8715 list_splice(&flowtable_del_list, &nft_trans_flowtable_hooks(trans));
8716 nft_flowtable_hook_release(&flowtable_hook);
8717
8718 nft_trans_commit_list_add_tail(ctx->net, trans);
8719
8720 return 0;
8721
8722 err_flowtable_del_hook:
8723 list_splice(&flowtable_del_list, &flowtable->hook_list);
8724 nft_flowtable_hook_release(&flowtable_hook);
8725
8726 return err;
8727 }
8728
nf_tables_delflowtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8729 static int nf_tables_delflowtable(struct sk_buff *skb,
8730 const struct nfnl_info *info,
8731 const struct nlattr * const nla[])
8732 {
8733 struct netlink_ext_ack *extack = info->extack;
8734 u8 genmask = nft_genmask_next(info->net);
8735 u8 family = info->nfmsg->nfgen_family;
8736 struct nft_flowtable *flowtable;
8737 struct net *net = info->net;
8738 const struct nlattr *attr;
8739 struct nft_table *table;
8740 struct nft_ctx ctx;
8741
8742 if (!nla[NFTA_FLOWTABLE_TABLE] ||
8743 (!nla[NFTA_FLOWTABLE_NAME] &&
8744 !nla[NFTA_FLOWTABLE_HANDLE]))
8745 return -EINVAL;
8746
8747 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
8748 genmask, NETLINK_CB(skb).portid);
8749 if (IS_ERR(table)) {
8750 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
8751 return PTR_ERR(table);
8752 }
8753
8754 if (nla[NFTA_FLOWTABLE_HANDLE]) {
8755 attr = nla[NFTA_FLOWTABLE_HANDLE];
8756 flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask);
8757 } else {
8758 attr = nla[NFTA_FLOWTABLE_NAME];
8759 flowtable = nft_flowtable_lookup(table, attr, genmask);
8760 }
8761
8762 if (IS_ERR(flowtable)) {
8763 if (PTR_ERR(flowtable) == -ENOENT &&
8764 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYFLOWTABLE)
8765 return 0;
8766
8767 NL_SET_BAD_ATTR(extack, attr);
8768 return PTR_ERR(flowtable);
8769 }
8770
8771 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
8772
8773 if (nla[NFTA_FLOWTABLE_HOOK])
8774 return nft_delflowtable_hook(&ctx, flowtable, extack);
8775
8776 if (flowtable->use > 0) {
8777 NL_SET_BAD_ATTR(extack, attr);
8778 return -EBUSY;
8779 }
8780
8781 return nft_delflowtable(&ctx, flowtable);
8782 }
8783
nf_tables_fill_flowtable_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq,int event,u32 flags,int family,struct nft_flowtable * flowtable,struct list_head * hook_list)8784 static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,
8785 u32 portid, u32 seq, int event,
8786 u32 flags, int family,
8787 struct nft_flowtable *flowtable,
8788 struct list_head *hook_list)
8789 {
8790 struct nlattr *nest, *nest_devs;
8791 struct nft_hook *hook;
8792 struct nlmsghdr *nlh;
8793
8794 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
8795 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
8796 NFNETLINK_V0, nft_base_seq(net));
8797 if (!nlh)
8798 goto nla_put_failure;
8799
8800 if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) ||
8801 nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) ||
8802 nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle),
8803 NFTA_FLOWTABLE_PAD))
8804 goto nla_put_failure;
8805
8806 if (event == NFT_MSG_DELFLOWTABLE && !hook_list) {
8807 nlmsg_end(skb, nlh);
8808 return 0;
8809 }
8810
8811 if (nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) ||
8812 nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags)))
8813 goto nla_put_failure;
8814
8815 nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK);
8816 if (!nest)
8817 goto nla_put_failure;
8818 if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) ||
8819 nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority)))
8820 goto nla_put_failure;
8821
8822 nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS);
8823 if (!nest_devs)
8824 goto nla_put_failure;
8825
8826 if (!hook_list)
8827 hook_list = &flowtable->hook_list;
8828
8829 list_for_each_entry_rcu(hook, hook_list, list) {
8830 if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name))
8831 goto nla_put_failure;
8832 }
8833 nla_nest_end(skb, nest_devs);
8834 nla_nest_end(skb, nest);
8835
8836 nlmsg_end(skb, nlh);
8837 return 0;
8838
8839 nla_put_failure:
8840 nlmsg_trim(skb, nlh);
8841 return -1;
8842 }
8843
8844 struct nft_flowtable_filter {
8845 char *table;
8846 };
8847
nf_tables_dump_flowtable(struct sk_buff * skb,struct netlink_callback * cb)8848 static int nf_tables_dump_flowtable(struct sk_buff *skb,
8849 struct netlink_callback *cb)
8850 {
8851 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
8852 struct nft_flowtable_filter *filter = cb->data;
8853 unsigned int idx = 0, s_idx = cb->args[0];
8854 struct net *net = sock_net(skb->sk);
8855 int family = nfmsg->nfgen_family;
8856 struct nft_flowtable *flowtable;
8857 struct nftables_pernet *nft_net;
8858 const struct nft_table *table;
8859
8860 rcu_read_lock();
8861 nft_net = nft_pernet(net);
8862 cb->seq = READ_ONCE(nft_net->base_seq);
8863
8864 list_for_each_entry_rcu(table, &nft_net->tables, list) {
8865 if (family != NFPROTO_UNSPEC && family != table->family)
8866 continue;
8867
8868 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
8869 if (!nft_is_active(net, flowtable))
8870 goto cont;
8871 if (idx < s_idx)
8872 goto cont;
8873 if (idx > s_idx)
8874 memset(&cb->args[1], 0,
8875 sizeof(cb->args) - sizeof(cb->args[0]));
8876 if (filter && filter->table &&
8877 strcmp(filter->table, table->name))
8878 goto cont;
8879
8880 if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid,
8881 cb->nlh->nlmsg_seq,
8882 NFT_MSG_NEWFLOWTABLE,
8883 NLM_F_MULTI | NLM_F_APPEND,
8884 table->family,
8885 flowtable, NULL) < 0)
8886 goto done;
8887
8888 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
8889 cont:
8890 idx++;
8891 }
8892 }
8893 done:
8894 rcu_read_unlock();
8895
8896 cb->args[0] = idx;
8897 return skb->len;
8898 }
8899
nf_tables_dump_flowtable_start(struct netlink_callback * cb)8900 static int nf_tables_dump_flowtable_start(struct netlink_callback *cb)
8901 {
8902 const struct nlattr * const *nla = cb->data;
8903 struct nft_flowtable_filter *filter = NULL;
8904
8905 if (nla[NFTA_FLOWTABLE_TABLE]) {
8906 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
8907 if (!filter)
8908 return -ENOMEM;
8909
8910 filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE],
8911 GFP_ATOMIC);
8912 if (!filter->table) {
8913 kfree(filter);
8914 return -ENOMEM;
8915 }
8916 }
8917
8918 cb->data = filter;
8919 return 0;
8920 }
8921
nf_tables_dump_flowtable_done(struct netlink_callback * cb)8922 static int nf_tables_dump_flowtable_done(struct netlink_callback *cb)
8923 {
8924 struct nft_flowtable_filter *filter = cb->data;
8925
8926 if (!filter)
8927 return 0;
8928
8929 kfree(filter->table);
8930 kfree(filter);
8931
8932 return 0;
8933 }
8934
8935 /* called with rcu_read_lock held */
nf_tables_getflowtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8936 static int nf_tables_getflowtable(struct sk_buff *skb,
8937 const struct nfnl_info *info,
8938 const struct nlattr * const nla[])
8939 {
8940 u8 genmask = nft_genmask_cur(info->net);
8941 u8 family = info->nfmsg->nfgen_family;
8942 struct nft_flowtable *flowtable;
8943 const struct nft_table *table;
8944 struct net *net = info->net;
8945 struct sk_buff *skb2;
8946 int err;
8947
8948 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
8949 struct netlink_dump_control c = {
8950 .start = nf_tables_dump_flowtable_start,
8951 .dump = nf_tables_dump_flowtable,
8952 .done = nf_tables_dump_flowtable_done,
8953 .module = THIS_MODULE,
8954 .data = (void *)nla,
8955 };
8956
8957 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
8958 }
8959
8960 if (!nla[NFTA_FLOWTABLE_NAME])
8961 return -EINVAL;
8962
8963 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
8964 genmask, 0);
8965 if (IS_ERR(table))
8966 return PTR_ERR(table);
8967
8968 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
8969 genmask);
8970 if (IS_ERR(flowtable))
8971 return PTR_ERR(flowtable);
8972
8973 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
8974 if (!skb2)
8975 return -ENOMEM;
8976
8977 err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid,
8978 info->nlh->nlmsg_seq,
8979 NFT_MSG_NEWFLOWTABLE, 0, family,
8980 flowtable, NULL);
8981 if (err < 0)
8982 goto err_fill_flowtable_info;
8983
8984 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
8985
8986 err_fill_flowtable_info:
8987 kfree_skb(skb2);
8988 return err;
8989 }
8990
nf_tables_flowtable_notify(struct nft_ctx * ctx,struct nft_flowtable * flowtable,struct list_head * hook_list,int event)8991 static void nf_tables_flowtable_notify(struct nft_ctx *ctx,
8992 struct nft_flowtable *flowtable,
8993 struct list_head *hook_list, int event)
8994 {
8995 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
8996 struct sk_buff *skb;
8997 u16 flags = 0;
8998 int err;
8999
9000 if (!ctx->report &&
9001 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
9002 return;
9003
9004 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
9005 if (skb == NULL)
9006 goto err;
9007
9008 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
9009 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
9010
9011 err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid,
9012 ctx->seq, event, flags,
9013 ctx->family, flowtable, hook_list);
9014 if (err < 0) {
9015 kfree_skb(skb);
9016 goto err;
9017 }
9018
9019 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
9020 return;
9021 err:
9022 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
9023 }
9024
nf_tables_flowtable_destroy(struct nft_flowtable * flowtable)9025 static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
9026 {
9027 struct nft_hook *hook, *next;
9028
9029 flowtable->data.type->free(&flowtable->data);
9030 list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
9031 flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
9032 FLOW_BLOCK_UNBIND);
9033 list_del_rcu(&hook->list);
9034 kfree_rcu(hook, rcu);
9035 }
9036 kfree(flowtable->name);
9037 module_put(flowtable->data.type->owner);
9038 kfree(flowtable);
9039 }
9040
nf_tables_fill_gen_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq)9041 static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
9042 u32 portid, u32 seq)
9043 {
9044 struct nftables_pernet *nft_net = nft_pernet(net);
9045 struct nlmsghdr *nlh;
9046 char buf[TASK_COMM_LEN];
9047 int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN);
9048
9049 nlh = nfnl_msg_put(skb, portid, seq, event, 0, AF_UNSPEC,
9050 NFNETLINK_V0, nft_base_seq(net));
9051 if (!nlh)
9052 goto nla_put_failure;
9053
9054 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(nft_net->base_seq)) ||
9055 nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) ||
9056 nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current)))
9057 goto nla_put_failure;
9058
9059 nlmsg_end(skb, nlh);
9060 return 0;
9061
9062 nla_put_failure:
9063 nlmsg_trim(skb, nlh);
9064 return -EMSGSIZE;
9065 }
9066
nft_flowtable_event(unsigned long event,struct net_device * dev,struct nft_flowtable * flowtable)9067 static void nft_flowtable_event(unsigned long event, struct net_device *dev,
9068 struct nft_flowtable *flowtable)
9069 {
9070 struct nft_hook *hook;
9071
9072 list_for_each_entry(hook, &flowtable->hook_list, list) {
9073 if (hook->ops.dev != dev)
9074 continue;
9075
9076 /* flow_offload_netdev_event() cleans up entries for us. */
9077 nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook);
9078 list_del_rcu(&hook->list);
9079 kfree_rcu(hook, rcu);
9080 break;
9081 }
9082 }
9083
nf_tables_flowtable_event(struct notifier_block * this,unsigned long event,void * ptr)9084 static int nf_tables_flowtable_event(struct notifier_block *this,
9085 unsigned long event, void *ptr)
9086 {
9087 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
9088 struct nft_flowtable *flowtable;
9089 struct nftables_pernet *nft_net;
9090 struct nft_table *table;
9091 struct net *net;
9092
9093 if (event != NETDEV_UNREGISTER)
9094 return 0;
9095
9096 net = dev_net(dev);
9097 nft_net = nft_pernet(net);
9098 mutex_lock(&nft_net->commit_mutex);
9099 list_for_each_entry(table, &nft_net->tables, list) {
9100 list_for_each_entry(flowtable, &table->flowtables, list) {
9101 nft_flowtable_event(event, dev, flowtable);
9102 }
9103 }
9104 mutex_unlock(&nft_net->commit_mutex);
9105
9106 return NOTIFY_DONE;
9107 }
9108
9109 static struct notifier_block nf_tables_flowtable_notifier = {
9110 .notifier_call = nf_tables_flowtable_event,
9111 };
9112
nf_tables_gen_notify(struct net * net,struct sk_buff * skb,int event)9113 static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb,
9114 int event)
9115 {
9116 struct nlmsghdr *nlh = nlmsg_hdr(skb);
9117 struct sk_buff *skb2;
9118 int err;
9119
9120 if (!nlmsg_report(nlh) &&
9121 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
9122 return;
9123
9124 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
9125 if (skb2 == NULL)
9126 goto err;
9127
9128 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
9129 nlh->nlmsg_seq);
9130 if (err < 0) {
9131 kfree_skb(skb2);
9132 goto err;
9133 }
9134
9135 nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
9136 nlmsg_report(nlh), GFP_KERNEL);
9137 return;
9138 err:
9139 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
9140 -ENOBUFS);
9141 }
9142
nf_tables_getgen(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])9143 static int nf_tables_getgen(struct sk_buff *skb, const struct nfnl_info *info,
9144 const struct nlattr * const nla[])
9145 {
9146 struct sk_buff *skb2;
9147 int err;
9148
9149 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
9150 if (skb2 == NULL)
9151 return -ENOMEM;
9152
9153 err = nf_tables_fill_gen_info(skb2, info->net, NETLINK_CB(skb).portid,
9154 info->nlh->nlmsg_seq);
9155 if (err < 0)
9156 goto err_fill_gen_info;
9157
9158 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
9159
9160 err_fill_gen_info:
9161 kfree_skb(skb2);
9162 return err;
9163 }
9164
9165 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
9166 [NFT_MSG_NEWTABLE] = {
9167 .call = nf_tables_newtable,
9168 .type = NFNL_CB_BATCH,
9169 .attr_count = NFTA_TABLE_MAX,
9170 .policy = nft_table_policy,
9171 },
9172 [NFT_MSG_GETTABLE] = {
9173 .call = nf_tables_gettable,
9174 .type = NFNL_CB_RCU,
9175 .attr_count = NFTA_TABLE_MAX,
9176 .policy = nft_table_policy,
9177 },
9178 [NFT_MSG_DELTABLE] = {
9179 .call = nf_tables_deltable,
9180 .type = NFNL_CB_BATCH,
9181 .attr_count = NFTA_TABLE_MAX,
9182 .policy = nft_table_policy,
9183 },
9184 [NFT_MSG_DESTROYTABLE] = {
9185 .call = nf_tables_deltable,
9186 .type = NFNL_CB_BATCH,
9187 .attr_count = NFTA_TABLE_MAX,
9188 .policy = nft_table_policy,
9189 },
9190 [NFT_MSG_NEWCHAIN] = {
9191 .call = nf_tables_newchain,
9192 .type = NFNL_CB_BATCH,
9193 .attr_count = NFTA_CHAIN_MAX,
9194 .policy = nft_chain_policy,
9195 },
9196 [NFT_MSG_GETCHAIN] = {
9197 .call = nf_tables_getchain,
9198 .type = NFNL_CB_RCU,
9199 .attr_count = NFTA_CHAIN_MAX,
9200 .policy = nft_chain_policy,
9201 },
9202 [NFT_MSG_DELCHAIN] = {
9203 .call = nf_tables_delchain,
9204 .type = NFNL_CB_BATCH,
9205 .attr_count = NFTA_CHAIN_MAX,
9206 .policy = nft_chain_policy,
9207 },
9208 [NFT_MSG_DESTROYCHAIN] = {
9209 .call = nf_tables_delchain,
9210 .type = NFNL_CB_BATCH,
9211 .attr_count = NFTA_CHAIN_MAX,
9212 .policy = nft_chain_policy,
9213 },
9214 [NFT_MSG_NEWRULE] = {
9215 .call = nf_tables_newrule,
9216 .type = NFNL_CB_BATCH,
9217 .attr_count = NFTA_RULE_MAX,
9218 .policy = nft_rule_policy,
9219 },
9220 [NFT_MSG_GETRULE] = {
9221 .call = nf_tables_getrule,
9222 .type = NFNL_CB_RCU,
9223 .attr_count = NFTA_RULE_MAX,
9224 .policy = nft_rule_policy,
9225 },
9226 [NFT_MSG_GETRULE_RESET] = {
9227 .call = nf_tables_getrule,
9228 .type = NFNL_CB_RCU,
9229 .attr_count = NFTA_RULE_MAX,
9230 .policy = nft_rule_policy,
9231 },
9232 [NFT_MSG_DELRULE] = {
9233 .call = nf_tables_delrule,
9234 .type = NFNL_CB_BATCH,
9235 .attr_count = NFTA_RULE_MAX,
9236 .policy = nft_rule_policy,
9237 },
9238 [NFT_MSG_DESTROYRULE] = {
9239 .call = nf_tables_delrule,
9240 .type = NFNL_CB_BATCH,
9241 .attr_count = NFTA_RULE_MAX,
9242 .policy = nft_rule_policy,
9243 },
9244 [NFT_MSG_NEWSET] = {
9245 .call = nf_tables_newset,
9246 .type = NFNL_CB_BATCH,
9247 .attr_count = NFTA_SET_MAX,
9248 .policy = nft_set_policy,
9249 },
9250 [NFT_MSG_GETSET] = {
9251 .call = nf_tables_getset,
9252 .type = NFNL_CB_RCU,
9253 .attr_count = NFTA_SET_MAX,
9254 .policy = nft_set_policy,
9255 },
9256 [NFT_MSG_DELSET] = {
9257 .call = nf_tables_delset,
9258 .type = NFNL_CB_BATCH,
9259 .attr_count = NFTA_SET_MAX,
9260 .policy = nft_set_policy,
9261 },
9262 [NFT_MSG_DESTROYSET] = {
9263 .call = nf_tables_delset,
9264 .type = NFNL_CB_BATCH,
9265 .attr_count = NFTA_SET_MAX,
9266 .policy = nft_set_policy,
9267 },
9268 [NFT_MSG_NEWSETELEM] = {
9269 .call = nf_tables_newsetelem,
9270 .type = NFNL_CB_BATCH,
9271 .attr_count = NFTA_SET_ELEM_LIST_MAX,
9272 .policy = nft_set_elem_list_policy,
9273 },
9274 [NFT_MSG_GETSETELEM] = {
9275 .call = nf_tables_getsetelem,
9276 .type = NFNL_CB_RCU,
9277 .attr_count = NFTA_SET_ELEM_LIST_MAX,
9278 .policy = nft_set_elem_list_policy,
9279 },
9280 [NFT_MSG_GETSETELEM_RESET] = {
9281 .call = nf_tables_getsetelem,
9282 .type = NFNL_CB_RCU,
9283 .attr_count = NFTA_SET_ELEM_LIST_MAX,
9284 .policy = nft_set_elem_list_policy,
9285 },
9286 [NFT_MSG_DELSETELEM] = {
9287 .call = nf_tables_delsetelem,
9288 .type = NFNL_CB_BATCH,
9289 .attr_count = NFTA_SET_ELEM_LIST_MAX,
9290 .policy = nft_set_elem_list_policy,
9291 },
9292 [NFT_MSG_DESTROYSETELEM] = {
9293 .call = nf_tables_delsetelem,
9294 .type = NFNL_CB_BATCH,
9295 .attr_count = NFTA_SET_ELEM_LIST_MAX,
9296 .policy = nft_set_elem_list_policy,
9297 },
9298 [NFT_MSG_GETGEN] = {
9299 .call = nf_tables_getgen,
9300 .type = NFNL_CB_RCU,
9301 },
9302 [NFT_MSG_NEWOBJ] = {
9303 .call = nf_tables_newobj,
9304 .type = NFNL_CB_BATCH,
9305 .attr_count = NFTA_OBJ_MAX,
9306 .policy = nft_obj_policy,
9307 },
9308 [NFT_MSG_GETOBJ] = {
9309 .call = nf_tables_getobj,
9310 .type = NFNL_CB_RCU,
9311 .attr_count = NFTA_OBJ_MAX,
9312 .policy = nft_obj_policy,
9313 },
9314 [NFT_MSG_DELOBJ] = {
9315 .call = nf_tables_delobj,
9316 .type = NFNL_CB_BATCH,
9317 .attr_count = NFTA_OBJ_MAX,
9318 .policy = nft_obj_policy,
9319 },
9320 [NFT_MSG_DESTROYOBJ] = {
9321 .call = nf_tables_delobj,
9322 .type = NFNL_CB_BATCH,
9323 .attr_count = NFTA_OBJ_MAX,
9324 .policy = nft_obj_policy,
9325 },
9326 [NFT_MSG_GETOBJ_RESET] = {
9327 .call = nf_tables_getobj_reset,
9328 .type = NFNL_CB_RCU,
9329 .attr_count = NFTA_OBJ_MAX,
9330 .policy = nft_obj_policy,
9331 },
9332 [NFT_MSG_NEWFLOWTABLE] = {
9333 .call = nf_tables_newflowtable,
9334 .type = NFNL_CB_BATCH,
9335 .attr_count = NFTA_FLOWTABLE_MAX,
9336 .policy = nft_flowtable_policy,
9337 },
9338 [NFT_MSG_GETFLOWTABLE] = {
9339 .call = nf_tables_getflowtable,
9340 .type = NFNL_CB_RCU,
9341 .attr_count = NFTA_FLOWTABLE_MAX,
9342 .policy = nft_flowtable_policy,
9343 },
9344 [NFT_MSG_DELFLOWTABLE] = {
9345 .call = nf_tables_delflowtable,
9346 .type = NFNL_CB_BATCH,
9347 .attr_count = NFTA_FLOWTABLE_MAX,
9348 .policy = nft_flowtable_policy,
9349 },
9350 [NFT_MSG_DESTROYFLOWTABLE] = {
9351 .call = nf_tables_delflowtable,
9352 .type = NFNL_CB_BATCH,
9353 .attr_count = NFTA_FLOWTABLE_MAX,
9354 .policy = nft_flowtable_policy,
9355 },
9356 };
9357
nf_tables_validate(struct net * net)9358 static int nf_tables_validate(struct net *net)
9359 {
9360 struct nftables_pernet *nft_net = nft_pernet(net);
9361 struct nft_table *table;
9362
9363 list_for_each_entry(table, &nft_net->tables, list) {
9364 switch (table->validate_state) {
9365 case NFT_VALIDATE_SKIP:
9366 continue;
9367 case NFT_VALIDATE_NEED:
9368 nft_validate_state_update(table, NFT_VALIDATE_DO);
9369 fallthrough;
9370 case NFT_VALIDATE_DO:
9371 if (nft_table_validate(net, table) < 0)
9372 return -EAGAIN;
9373
9374 nft_validate_state_update(table, NFT_VALIDATE_SKIP);
9375 break;
9376 }
9377 }
9378
9379 return 0;
9380 }
9381
9382 /* a drop policy has to be deferred until all rules have been activated,
9383 * otherwise a large ruleset that contains a drop-policy base chain will
9384 * cause all packets to get dropped until the full transaction has been
9385 * processed.
9386 *
9387 * We defer the drop policy until the transaction has been finalized.
9388 */
nft_chain_commit_drop_policy(struct nft_trans * trans)9389 static void nft_chain_commit_drop_policy(struct nft_trans *trans)
9390 {
9391 struct nft_base_chain *basechain;
9392
9393 if (nft_trans_chain_policy(trans) != NF_DROP)
9394 return;
9395
9396 if (!nft_is_base_chain(trans->ctx.chain))
9397 return;
9398
9399 basechain = nft_base_chain(trans->ctx.chain);
9400 basechain->policy = NF_DROP;
9401 }
9402
nft_chain_commit_update(struct nft_trans * trans)9403 static void nft_chain_commit_update(struct nft_trans *trans)
9404 {
9405 struct nft_base_chain *basechain;
9406
9407 if (nft_trans_chain_name(trans)) {
9408 rhltable_remove(&trans->ctx.table->chains_ht,
9409 &trans->ctx.chain->rhlhead,
9410 nft_chain_ht_params);
9411 swap(trans->ctx.chain->name, nft_trans_chain_name(trans));
9412 rhltable_insert_key(&trans->ctx.table->chains_ht,
9413 trans->ctx.chain->name,
9414 &trans->ctx.chain->rhlhead,
9415 nft_chain_ht_params);
9416 }
9417
9418 if (!nft_is_base_chain(trans->ctx.chain))
9419 return;
9420
9421 nft_chain_stats_replace(trans);
9422
9423 basechain = nft_base_chain(trans->ctx.chain);
9424
9425 switch (nft_trans_chain_policy(trans)) {
9426 case NF_DROP:
9427 case NF_ACCEPT:
9428 basechain->policy = nft_trans_chain_policy(trans);
9429 break;
9430 }
9431 }
9432
nft_obj_commit_update(struct nft_trans * trans)9433 static void nft_obj_commit_update(struct nft_trans *trans)
9434 {
9435 struct nft_object *newobj;
9436 struct nft_object *obj;
9437
9438 obj = nft_trans_obj(trans);
9439 newobj = nft_trans_obj_newobj(trans);
9440
9441 if (WARN_ON_ONCE(!obj->ops->update))
9442 return;
9443
9444 obj->ops->update(obj, newobj);
9445 nft_obj_destroy(&trans->ctx, newobj);
9446 }
9447
nft_commit_release(struct nft_trans * trans)9448 static void nft_commit_release(struct nft_trans *trans)
9449 {
9450 switch (trans->msg_type) {
9451 case NFT_MSG_DELTABLE:
9452 case NFT_MSG_DESTROYTABLE:
9453 nf_tables_table_destroy(&trans->ctx);
9454 break;
9455 case NFT_MSG_NEWCHAIN:
9456 free_percpu(nft_trans_chain_stats(trans));
9457 kfree(nft_trans_chain_name(trans));
9458 break;
9459 case NFT_MSG_DELCHAIN:
9460 case NFT_MSG_DESTROYCHAIN:
9461 if (nft_trans_chain_update(trans))
9462 nft_hooks_destroy(&nft_trans_chain_hooks(trans));
9463 else
9464 nf_tables_chain_destroy(nft_trans_chain(trans));
9465 break;
9466 case NFT_MSG_DELRULE:
9467 case NFT_MSG_DESTROYRULE:
9468 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
9469 break;
9470 case NFT_MSG_DELSET:
9471 case NFT_MSG_DESTROYSET:
9472 nft_set_destroy(&trans->ctx, nft_trans_set(trans));
9473 break;
9474 case NFT_MSG_DELSETELEM:
9475 case NFT_MSG_DESTROYSETELEM:
9476 nf_tables_set_elem_destroy(&trans->ctx,
9477 nft_trans_elem_set(trans),
9478 nft_trans_elem(trans).priv);
9479 break;
9480 case NFT_MSG_DELOBJ:
9481 case NFT_MSG_DESTROYOBJ:
9482 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
9483 break;
9484 case NFT_MSG_DELFLOWTABLE:
9485 case NFT_MSG_DESTROYFLOWTABLE:
9486 if (nft_trans_flowtable_update(trans))
9487 nft_hooks_destroy(&nft_trans_flowtable_hooks(trans));
9488 else
9489 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
9490 break;
9491 }
9492
9493 if (trans->put_net)
9494 put_net(trans->ctx.net);
9495
9496 kfree(trans);
9497 }
9498
nf_tables_trans_destroy_work(struct work_struct * w)9499 static void nf_tables_trans_destroy_work(struct work_struct *w)
9500 {
9501 struct nft_trans *trans, *next;
9502 LIST_HEAD(head);
9503
9504 spin_lock(&nf_tables_destroy_list_lock);
9505 list_splice_init(&nf_tables_destroy_list, &head);
9506 spin_unlock(&nf_tables_destroy_list_lock);
9507
9508 if (list_empty(&head))
9509 return;
9510
9511 synchronize_rcu();
9512
9513 list_for_each_entry_safe(trans, next, &head, list) {
9514 nft_trans_list_del(trans);
9515 nft_commit_release(trans);
9516 }
9517 }
9518
nf_tables_trans_destroy_flush_work(void)9519 void nf_tables_trans_destroy_flush_work(void)
9520 {
9521 flush_work(&trans_destroy_work);
9522 }
9523 EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work);
9524
nft_expr_reduce(struct nft_regs_track * track,const struct nft_expr * expr)9525 static bool nft_expr_reduce(struct nft_regs_track *track,
9526 const struct nft_expr *expr)
9527 {
9528 return false;
9529 }
9530
nf_tables_commit_chain_prepare(struct net * net,struct nft_chain * chain)9531 static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain)
9532 {
9533 const struct nft_expr *expr, *last;
9534 struct nft_regs_track track = {};
9535 unsigned int size, data_size;
9536 void *data, *data_boundary;
9537 struct nft_rule_dp *prule;
9538 struct nft_rule *rule;
9539
9540 /* already handled or inactive chain? */
9541 if (chain->blob_next || !nft_is_active_next(net, chain))
9542 return 0;
9543
9544 data_size = 0;
9545 list_for_each_entry(rule, &chain->rules, list) {
9546 if (nft_is_active_next(net, rule)) {
9547 data_size += sizeof(*prule) + rule->dlen;
9548 if (data_size > INT_MAX)
9549 return -ENOMEM;
9550 }
9551 }
9552
9553 chain->blob_next = nf_tables_chain_alloc_rules(chain, data_size);
9554 if (!chain->blob_next)
9555 return -ENOMEM;
9556
9557 data = (void *)chain->blob_next->data;
9558 data_boundary = data + data_size;
9559 size = 0;
9560
9561 list_for_each_entry(rule, &chain->rules, list) {
9562 if (!nft_is_active_next(net, rule))
9563 continue;
9564
9565 prule = (struct nft_rule_dp *)data;
9566 data += offsetof(struct nft_rule_dp, data);
9567 if (WARN_ON_ONCE(data > data_boundary))
9568 return -ENOMEM;
9569
9570 size = 0;
9571 track.last = nft_expr_last(rule);
9572 nft_rule_for_each_expr(expr, last, rule) {
9573 track.cur = expr;
9574
9575 if (nft_expr_reduce(&track, expr)) {
9576 expr = track.cur;
9577 continue;
9578 }
9579
9580 if (WARN_ON_ONCE(data + size + expr->ops->size > data_boundary))
9581 return -ENOMEM;
9582
9583 memcpy(data + size, expr, expr->ops->size);
9584 size += expr->ops->size;
9585 }
9586 if (WARN_ON_ONCE(size >= 1 << 12))
9587 return -ENOMEM;
9588
9589 prule->handle = rule->handle;
9590 prule->dlen = size;
9591 prule->is_last = 0;
9592
9593 data += size;
9594 size = 0;
9595 chain->blob_next->size += (unsigned long)(data - (void *)prule);
9596 }
9597
9598 if (WARN_ON_ONCE(data > data_boundary))
9599 return -ENOMEM;
9600
9601 prule = (struct nft_rule_dp *)data;
9602 nft_last_rule(chain, prule);
9603
9604 return 0;
9605 }
9606
nf_tables_commit_chain_prepare_cancel(struct net * net)9607 static void nf_tables_commit_chain_prepare_cancel(struct net *net)
9608 {
9609 struct nftables_pernet *nft_net = nft_pernet(net);
9610 struct nft_trans *trans, *next;
9611
9612 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
9613 struct nft_chain *chain = trans->ctx.chain;
9614
9615 if (trans->msg_type == NFT_MSG_NEWRULE ||
9616 trans->msg_type == NFT_MSG_DELRULE) {
9617 kvfree(chain->blob_next);
9618 chain->blob_next = NULL;
9619 }
9620 }
9621 }
9622
__nf_tables_commit_chain_free_rules(struct rcu_head * h)9623 static void __nf_tables_commit_chain_free_rules(struct rcu_head *h)
9624 {
9625 struct nft_rule_dp_last *l = container_of(h, struct nft_rule_dp_last, h);
9626
9627 kvfree(l->blob);
9628 }
9629
nf_tables_commit_chain_free_rules_old(struct nft_rule_blob * blob)9630 static void nf_tables_commit_chain_free_rules_old(struct nft_rule_blob *blob)
9631 {
9632 struct nft_rule_dp_last *last;
9633
9634 /* last rule trailer is after end marker */
9635 last = (void *)blob + sizeof(*blob) + blob->size;
9636 last->blob = blob;
9637
9638 call_rcu(&last->h, __nf_tables_commit_chain_free_rules);
9639 }
9640
nf_tables_commit_chain(struct net * net,struct nft_chain * chain)9641 static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain)
9642 {
9643 struct nft_rule_blob *g0, *g1;
9644 bool next_genbit;
9645
9646 next_genbit = nft_gencursor_next(net);
9647
9648 g0 = rcu_dereference_protected(chain->blob_gen_0,
9649 lockdep_commit_lock_is_held(net));
9650 g1 = rcu_dereference_protected(chain->blob_gen_1,
9651 lockdep_commit_lock_is_held(net));
9652
9653 /* No changes to this chain? */
9654 if (chain->blob_next == NULL) {
9655 /* chain had no change in last or next generation */
9656 if (g0 == g1)
9657 return;
9658 /*
9659 * chain had no change in this generation; make sure next
9660 * one uses same rules as current generation.
9661 */
9662 if (next_genbit) {
9663 rcu_assign_pointer(chain->blob_gen_1, g0);
9664 nf_tables_commit_chain_free_rules_old(g1);
9665 } else {
9666 rcu_assign_pointer(chain->blob_gen_0, g1);
9667 nf_tables_commit_chain_free_rules_old(g0);
9668 }
9669
9670 return;
9671 }
9672
9673 if (next_genbit)
9674 rcu_assign_pointer(chain->blob_gen_1, chain->blob_next);
9675 else
9676 rcu_assign_pointer(chain->blob_gen_0, chain->blob_next);
9677
9678 chain->blob_next = NULL;
9679
9680 if (g0 == g1)
9681 return;
9682
9683 if (next_genbit)
9684 nf_tables_commit_chain_free_rules_old(g1);
9685 else
9686 nf_tables_commit_chain_free_rules_old(g0);
9687 }
9688
nft_obj_del(struct nft_object * obj)9689 static void nft_obj_del(struct nft_object *obj)
9690 {
9691 rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params);
9692 list_del_rcu(&obj->list);
9693 }
9694
nft_chain_del(struct nft_chain * chain)9695 void nft_chain_del(struct nft_chain *chain)
9696 {
9697 struct nft_table *table = chain->table;
9698
9699 WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead,
9700 nft_chain_ht_params));
9701 list_del_rcu(&chain->list);
9702 }
9703
nft_trans_gc_setelem_remove(struct nft_ctx * ctx,struct nft_trans_gc * trans)9704 static void nft_trans_gc_setelem_remove(struct nft_ctx *ctx,
9705 struct nft_trans_gc *trans)
9706 {
9707 void **priv = trans->priv;
9708 unsigned int i;
9709
9710 for (i = 0; i < trans->count; i++) {
9711 struct nft_set_elem elem = {
9712 .priv = priv[i],
9713 };
9714
9715 nft_setelem_data_deactivate(ctx->net, trans->set, &elem);
9716 nft_setelem_remove(ctx->net, trans->set, &elem);
9717 }
9718 }
9719
nft_trans_gc_destroy(struct nft_trans_gc * trans)9720 void nft_trans_gc_destroy(struct nft_trans_gc *trans)
9721 {
9722 nft_set_put(trans->set);
9723 put_net(trans->net);
9724 kfree(trans);
9725 }
9726
nft_trans_gc_trans_free(struct rcu_head * rcu)9727 static void nft_trans_gc_trans_free(struct rcu_head *rcu)
9728 {
9729 struct nft_set_elem elem = {};
9730 struct nft_trans_gc *trans;
9731 struct nft_ctx ctx = {};
9732 unsigned int i;
9733
9734 trans = container_of(rcu, struct nft_trans_gc, rcu);
9735 ctx.net = read_pnet(&trans->set->net);
9736
9737 for (i = 0; i < trans->count; i++) {
9738 elem.priv = trans->priv[i];
9739 if (!nft_setelem_is_catchall(trans->set, &elem))
9740 atomic_dec(&trans->set->nelems);
9741
9742 nf_tables_set_elem_destroy(&ctx, trans->set, elem.priv);
9743 }
9744
9745 nft_trans_gc_destroy(trans);
9746 }
9747
nft_trans_gc_work_done(struct nft_trans_gc * trans)9748 static bool nft_trans_gc_work_done(struct nft_trans_gc *trans)
9749 {
9750 struct nftables_pernet *nft_net;
9751 struct nft_ctx ctx = {};
9752
9753 nft_net = nft_pernet(trans->net);
9754
9755 mutex_lock(&nft_net->commit_mutex);
9756
9757 /* Check for race with transaction, otherwise this batch refers to
9758 * stale objects that might not be there anymore. Skip transaction if
9759 * set has been destroyed from control plane transaction in case gc
9760 * worker loses race.
9761 */
9762 if (READ_ONCE(nft_net->gc_seq) != trans->seq || trans->set->dead) {
9763 mutex_unlock(&nft_net->commit_mutex);
9764 return false;
9765 }
9766
9767 ctx.net = trans->net;
9768 ctx.table = trans->set->table;
9769
9770 nft_trans_gc_setelem_remove(&ctx, trans);
9771 mutex_unlock(&nft_net->commit_mutex);
9772
9773 return true;
9774 }
9775
nft_trans_gc_work(struct work_struct * work)9776 static void nft_trans_gc_work(struct work_struct *work)
9777 {
9778 struct nft_trans_gc *trans, *next;
9779 LIST_HEAD(trans_gc_list);
9780
9781 spin_lock(&nf_tables_gc_list_lock);
9782 list_splice_init(&nf_tables_gc_list, &trans_gc_list);
9783 spin_unlock(&nf_tables_gc_list_lock);
9784
9785 list_for_each_entry_safe(trans, next, &trans_gc_list, list) {
9786 list_del(&trans->list);
9787 if (!nft_trans_gc_work_done(trans)) {
9788 nft_trans_gc_destroy(trans);
9789 continue;
9790 }
9791 call_rcu(&trans->rcu, nft_trans_gc_trans_free);
9792 }
9793 }
9794
nft_trans_gc_alloc(struct nft_set * set,unsigned int gc_seq,gfp_t gfp)9795 struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set,
9796 unsigned int gc_seq, gfp_t gfp)
9797 {
9798 struct net *net = read_pnet(&set->net);
9799 struct nft_trans_gc *trans;
9800
9801 trans = kzalloc(sizeof(*trans), gfp);
9802 if (!trans)
9803 return NULL;
9804
9805 trans->net = maybe_get_net(net);
9806 if (!trans->net) {
9807 kfree(trans);
9808 return NULL;
9809 }
9810
9811 refcount_inc(&set->refs);
9812 trans->set = set;
9813 trans->seq = gc_seq;
9814
9815 return trans;
9816 }
9817
nft_trans_gc_elem_add(struct nft_trans_gc * trans,void * priv)9818 void nft_trans_gc_elem_add(struct nft_trans_gc *trans, void *priv)
9819 {
9820 trans->priv[trans->count++] = priv;
9821 }
9822
nft_trans_gc_queue_work(struct nft_trans_gc * trans)9823 static void nft_trans_gc_queue_work(struct nft_trans_gc *trans)
9824 {
9825 spin_lock(&nf_tables_gc_list_lock);
9826 list_add_tail(&trans->list, &nf_tables_gc_list);
9827 spin_unlock(&nf_tables_gc_list_lock);
9828
9829 schedule_work(&trans_gc_work);
9830 }
9831
nft_trans_gc_space(struct nft_trans_gc * trans)9832 static int nft_trans_gc_space(struct nft_trans_gc *trans)
9833 {
9834 return NFT_TRANS_GC_BATCHCOUNT - trans->count;
9835 }
9836
nft_trans_gc_queue_async(struct nft_trans_gc * gc,unsigned int gc_seq,gfp_t gfp)9837 struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc,
9838 unsigned int gc_seq, gfp_t gfp)
9839 {
9840 struct nft_set *set;
9841
9842 if (nft_trans_gc_space(gc))
9843 return gc;
9844
9845 set = gc->set;
9846 nft_trans_gc_queue_work(gc);
9847
9848 return nft_trans_gc_alloc(set, gc_seq, gfp);
9849 }
9850
nft_trans_gc_queue_async_done(struct nft_trans_gc * trans)9851 void nft_trans_gc_queue_async_done(struct nft_trans_gc *trans)
9852 {
9853 if (trans->count == 0) {
9854 nft_trans_gc_destroy(trans);
9855 return;
9856 }
9857
9858 nft_trans_gc_queue_work(trans);
9859 }
9860
nft_trans_gc_queue_sync(struct nft_trans_gc * gc,gfp_t gfp)9861 struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp)
9862 {
9863 struct nft_set *set;
9864
9865 if (WARN_ON_ONCE(!lockdep_commit_lock_is_held(gc->net)))
9866 return NULL;
9867
9868 if (nft_trans_gc_space(gc))
9869 return gc;
9870
9871 set = gc->set;
9872 call_rcu(&gc->rcu, nft_trans_gc_trans_free);
9873
9874 return nft_trans_gc_alloc(set, 0, gfp);
9875 }
9876
nft_trans_gc_queue_sync_done(struct nft_trans_gc * trans)9877 void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans)
9878 {
9879 WARN_ON_ONCE(!lockdep_commit_lock_is_held(trans->net));
9880
9881 if (trans->count == 0) {
9882 nft_trans_gc_destroy(trans);
9883 return;
9884 }
9885
9886 call_rcu(&trans->rcu, nft_trans_gc_trans_free);
9887 }
9888
nft_trans_gc_catchall_async(struct nft_trans_gc * gc,unsigned int gc_seq)9889 struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,
9890 unsigned int gc_seq)
9891 {
9892 struct nft_set_elem_catchall *catchall;
9893 const struct nft_set *set = gc->set;
9894 struct nft_set_ext *ext;
9895
9896 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
9897 ext = nft_set_elem_ext(set, catchall->elem);
9898
9899 if (!nft_set_elem_expired(ext))
9900 continue;
9901 if (nft_set_elem_is_dead(ext))
9902 goto dead_elem;
9903
9904 nft_set_elem_dead(ext);
9905 dead_elem:
9906 gc = nft_trans_gc_queue_async(gc, gc_seq, GFP_ATOMIC);
9907 if (!gc)
9908 return NULL;
9909
9910 nft_trans_gc_elem_add(gc, catchall->elem);
9911 }
9912
9913 return gc;
9914 }
9915
nft_trans_gc_catchall_sync(struct nft_trans_gc * gc)9916 struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc)
9917 {
9918 struct nft_set_elem_catchall *catchall, *next;
9919 const struct nft_set *set = gc->set;
9920 struct nft_set_elem elem;
9921 struct nft_set_ext *ext;
9922
9923 WARN_ON_ONCE(!lockdep_commit_lock_is_held(gc->net));
9924
9925 list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
9926 ext = nft_set_elem_ext(set, catchall->elem);
9927
9928 if (!nft_set_elem_expired(ext))
9929 continue;
9930
9931 gc = nft_trans_gc_queue_sync(gc, GFP_KERNEL);
9932 if (!gc)
9933 return NULL;
9934
9935 memset(&elem, 0, sizeof(elem));
9936 elem.priv = catchall->elem;
9937
9938 nft_setelem_data_deactivate(gc->net, gc->set, &elem);
9939 nft_setelem_catchall_destroy(catchall);
9940 nft_trans_gc_elem_add(gc, elem.priv);
9941 }
9942
9943 return gc;
9944 }
9945
nf_tables_module_autoload_cleanup(struct net * net)9946 static void nf_tables_module_autoload_cleanup(struct net *net)
9947 {
9948 struct nftables_pernet *nft_net = nft_pernet(net);
9949 struct nft_module_request *req, *next;
9950
9951 WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
9952 list_for_each_entry_safe(req, next, &nft_net->module_list, list) {
9953 WARN_ON_ONCE(!req->done);
9954 list_del(&req->list);
9955 kfree(req);
9956 }
9957 }
9958
nf_tables_commit_release(struct net * net)9959 static void nf_tables_commit_release(struct net *net)
9960 {
9961 struct nftables_pernet *nft_net = nft_pernet(net);
9962 struct nft_trans *trans;
9963
9964 /* all side effects have to be made visible.
9965 * For example, if a chain named 'foo' has been deleted, a
9966 * new transaction must not find it anymore.
9967 *
9968 * Memory reclaim happens asynchronously from work queue
9969 * to prevent expensive synchronize_rcu() in commit phase.
9970 */
9971 if (list_empty(&nft_net->commit_list)) {
9972 nf_tables_module_autoload_cleanup(net);
9973 mutex_unlock(&nft_net->commit_mutex);
9974 return;
9975 }
9976
9977 trans = list_last_entry(&nft_net->commit_list,
9978 struct nft_trans, list);
9979 get_net(trans->ctx.net);
9980 WARN_ON_ONCE(trans->put_net);
9981
9982 trans->put_net = true;
9983 spin_lock(&nf_tables_destroy_list_lock);
9984 list_splice_tail_init(&nft_net->commit_list, &nf_tables_destroy_list);
9985 spin_unlock(&nf_tables_destroy_list_lock);
9986
9987 nf_tables_module_autoload_cleanup(net);
9988 schedule_work(&trans_destroy_work);
9989
9990 mutex_unlock(&nft_net->commit_mutex);
9991 }
9992
nft_commit_notify(struct net * net,u32 portid)9993 static void nft_commit_notify(struct net *net, u32 portid)
9994 {
9995 struct nftables_pernet *nft_net = nft_pernet(net);
9996 struct sk_buff *batch_skb = NULL, *nskb, *skb;
9997 unsigned char *data;
9998 int len;
9999
10000 list_for_each_entry_safe(skb, nskb, &nft_net->notify_list, list) {
10001 if (!batch_skb) {
10002 new_batch:
10003 batch_skb = skb;
10004 len = NLMSG_GOODSIZE - skb->len;
10005 list_del(&skb->list);
10006 continue;
10007 }
10008 len -= skb->len;
10009 if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) {
10010 data = skb_put(batch_skb, skb->len);
10011 memcpy(data, skb->data, skb->len);
10012 list_del(&skb->list);
10013 kfree_skb(skb);
10014 continue;
10015 }
10016 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
10017 NFT_CB(batch_skb).report, GFP_KERNEL);
10018 goto new_batch;
10019 }
10020
10021 if (batch_skb) {
10022 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
10023 NFT_CB(batch_skb).report, GFP_KERNEL);
10024 }
10025
10026 WARN_ON_ONCE(!list_empty(&nft_net->notify_list));
10027 }
10028
nf_tables_commit_audit_alloc(struct list_head * adl,struct nft_table * table)10029 static int nf_tables_commit_audit_alloc(struct list_head *adl,
10030 struct nft_table *table)
10031 {
10032 struct nft_audit_data *adp;
10033
10034 list_for_each_entry(adp, adl, list) {
10035 if (adp->table == table)
10036 return 0;
10037 }
10038 adp = kzalloc(sizeof(*adp), GFP_KERNEL);
10039 if (!adp)
10040 return -ENOMEM;
10041 adp->table = table;
10042 list_add(&adp->list, adl);
10043 return 0;
10044 }
10045
nf_tables_commit_audit_free(struct list_head * adl)10046 static void nf_tables_commit_audit_free(struct list_head *adl)
10047 {
10048 struct nft_audit_data *adp, *adn;
10049
10050 list_for_each_entry_safe(adp, adn, adl, list) {
10051 list_del(&adp->list);
10052 kfree(adp);
10053 }
10054 }
10055
nf_tables_commit_audit_collect(struct list_head * adl,struct nft_table * table,u32 op)10056 static void nf_tables_commit_audit_collect(struct list_head *adl,
10057 struct nft_table *table, u32 op)
10058 {
10059 struct nft_audit_data *adp;
10060
10061 list_for_each_entry(adp, adl, list) {
10062 if (adp->table == table)
10063 goto found;
10064 }
10065 WARN_ONCE(1, "table=%s not expected in commit list", table->name);
10066 return;
10067 found:
10068 adp->entries++;
10069 if (!adp->op || adp->op > op)
10070 adp->op = op;
10071 }
10072
10073 #define AUNFTABLENAMELEN (NFT_TABLE_MAXNAMELEN + 22)
10074
nf_tables_commit_audit_log(struct list_head * adl,u32 generation)10075 static void nf_tables_commit_audit_log(struct list_head *adl, u32 generation)
10076 {
10077 struct nft_audit_data *adp, *adn;
10078 char aubuf[AUNFTABLENAMELEN];
10079
10080 list_for_each_entry_safe(adp, adn, adl, list) {
10081 snprintf(aubuf, AUNFTABLENAMELEN, "%s:%u", adp->table->name,
10082 generation);
10083 audit_log_nfcfg(aubuf, adp->table->family, adp->entries,
10084 nft2audit_op[adp->op], GFP_KERNEL);
10085 list_del(&adp->list);
10086 kfree(adp);
10087 }
10088 }
10089
nft_set_commit_update(struct list_head * set_update_list)10090 static void nft_set_commit_update(struct list_head *set_update_list)
10091 {
10092 struct nft_set *set, *next;
10093
10094 list_for_each_entry_safe(set, next, set_update_list, pending_update) {
10095 list_del_init(&set->pending_update);
10096
10097 if (!set->ops->commit || set->dead)
10098 continue;
10099
10100 set->ops->commit(set);
10101 }
10102 }
10103
nft_gc_seq_begin(struct nftables_pernet * nft_net)10104 static unsigned int nft_gc_seq_begin(struct nftables_pernet *nft_net)
10105 {
10106 unsigned int gc_seq;
10107
10108 /* Bump gc counter, it becomes odd, this is the busy mark. */
10109 gc_seq = READ_ONCE(nft_net->gc_seq);
10110 WRITE_ONCE(nft_net->gc_seq, ++gc_seq);
10111
10112 return gc_seq;
10113 }
10114
nft_gc_seq_end(struct nftables_pernet * nft_net,unsigned int gc_seq)10115 static void nft_gc_seq_end(struct nftables_pernet *nft_net, unsigned int gc_seq)
10116 {
10117 WRITE_ONCE(nft_net->gc_seq, ++gc_seq);
10118 }
10119
nf_tables_commit(struct net * net,struct sk_buff * skb)10120 static int nf_tables_commit(struct net *net, struct sk_buff *skb)
10121 {
10122 struct nftables_pernet *nft_net = nft_pernet(net);
10123 struct nft_trans *trans, *next;
10124 unsigned int base_seq, gc_seq;
10125 LIST_HEAD(set_update_list);
10126 struct nft_trans_elem *te;
10127 struct nft_chain *chain;
10128 struct nft_table *table;
10129 LIST_HEAD(adl);
10130 int err;
10131
10132 if (list_empty(&nft_net->commit_list)) {
10133 mutex_unlock(&nft_net->commit_mutex);
10134 return 0;
10135 }
10136
10137 list_for_each_entry(trans, &nft_net->binding_list, binding_list) {
10138 switch (trans->msg_type) {
10139 case NFT_MSG_NEWSET:
10140 if (!nft_trans_set_update(trans) &&
10141 nft_set_is_anonymous(nft_trans_set(trans)) &&
10142 !nft_trans_set_bound(trans)) {
10143 pr_warn_once("nftables ruleset with unbound set\n");
10144 return -EINVAL;
10145 }
10146 break;
10147 case NFT_MSG_NEWCHAIN:
10148 if (!nft_trans_chain_update(trans) &&
10149 nft_chain_binding(nft_trans_chain(trans)) &&
10150 !nft_trans_chain_bound(trans)) {
10151 pr_warn_once("nftables ruleset with unbound chain\n");
10152 return -EINVAL;
10153 }
10154 break;
10155 }
10156 }
10157
10158 /* 0. Validate ruleset, otherwise roll back for error reporting. */
10159 if (nf_tables_validate(net) < 0) {
10160 nft_net->validate_state = NFT_VALIDATE_DO;
10161 return -EAGAIN;
10162 }
10163
10164 err = nft_flow_rule_offload_commit(net);
10165 if (err < 0)
10166 return err;
10167
10168 /* 1. Allocate space for next generation rules_gen_X[] */
10169 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
10170 int ret;
10171
10172 ret = nf_tables_commit_audit_alloc(&adl, trans->ctx.table);
10173 if (ret) {
10174 nf_tables_commit_chain_prepare_cancel(net);
10175 nf_tables_commit_audit_free(&adl);
10176 return ret;
10177 }
10178 if (trans->msg_type == NFT_MSG_NEWRULE ||
10179 trans->msg_type == NFT_MSG_DELRULE) {
10180 chain = trans->ctx.chain;
10181
10182 ret = nf_tables_commit_chain_prepare(net, chain);
10183 if (ret < 0) {
10184 nf_tables_commit_chain_prepare_cancel(net);
10185 nf_tables_commit_audit_free(&adl);
10186 return ret;
10187 }
10188 }
10189 }
10190
10191 /* step 2. Make rules_gen_X visible to packet path */
10192 list_for_each_entry(table, &nft_net->tables, list) {
10193 list_for_each_entry(chain, &table->chains, list)
10194 nf_tables_commit_chain(net, chain);
10195 }
10196
10197 /*
10198 * Bump generation counter, invalidate any dump in progress.
10199 * Cannot fail after this point.
10200 */
10201 base_seq = READ_ONCE(nft_net->base_seq);
10202 while (++base_seq == 0)
10203 ;
10204
10205 WRITE_ONCE(nft_net->base_seq, base_seq);
10206
10207 gc_seq = nft_gc_seq_begin(nft_net);
10208
10209 /* step 3. Start new generation, rules_gen_X now in use. */
10210 net->nft.gencursor = nft_gencursor_next(net);
10211
10212 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
10213 nf_tables_commit_audit_collect(&adl, trans->ctx.table,
10214 trans->msg_type);
10215 switch (trans->msg_type) {
10216 case NFT_MSG_NEWTABLE:
10217 if (nft_trans_table_update(trans)) {
10218 if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) {
10219 nft_trans_destroy(trans);
10220 break;
10221 }
10222 if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT)
10223 nf_tables_table_disable(net, trans->ctx.table);
10224
10225 trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE;
10226 } else {
10227 nft_clear(net, trans->ctx.table);
10228 }
10229 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
10230 nft_trans_destroy(trans);
10231 break;
10232 case NFT_MSG_DELTABLE:
10233 case NFT_MSG_DESTROYTABLE:
10234 list_del_rcu(&trans->ctx.table->list);
10235 nf_tables_table_notify(&trans->ctx, trans->msg_type);
10236 break;
10237 case NFT_MSG_NEWCHAIN:
10238 if (nft_trans_chain_update(trans)) {
10239 nft_chain_commit_update(trans);
10240 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN,
10241 &nft_trans_chain_hooks(trans));
10242 list_splice(&nft_trans_chain_hooks(trans),
10243 &nft_trans_basechain(trans)->hook_list);
10244 /* trans destroyed after rcu grace period */
10245 } else {
10246 nft_chain_commit_drop_policy(trans);
10247 nft_clear(net, trans->ctx.chain);
10248 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN, NULL);
10249 nft_trans_destroy(trans);
10250 }
10251 break;
10252 case NFT_MSG_DELCHAIN:
10253 case NFT_MSG_DESTROYCHAIN:
10254 if (nft_trans_chain_update(trans)) {
10255 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN,
10256 &nft_trans_chain_hooks(trans));
10257 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT)) {
10258 nft_netdev_unregister_hooks(net,
10259 &nft_trans_chain_hooks(trans),
10260 true);
10261 }
10262 } else {
10263 nft_chain_del(trans->ctx.chain);
10264 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN,
10265 NULL);
10266 nf_tables_unregister_hook(trans->ctx.net,
10267 trans->ctx.table,
10268 trans->ctx.chain);
10269 }
10270 break;
10271 case NFT_MSG_NEWRULE:
10272 nft_clear(trans->ctx.net, nft_trans_rule(trans));
10273 nf_tables_rule_notify(&trans->ctx,
10274 nft_trans_rule(trans),
10275 NFT_MSG_NEWRULE);
10276 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
10277 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
10278
10279 nft_trans_destroy(trans);
10280 break;
10281 case NFT_MSG_DELRULE:
10282 case NFT_MSG_DESTROYRULE:
10283 list_del_rcu(&nft_trans_rule(trans)->list);
10284 nf_tables_rule_notify(&trans->ctx,
10285 nft_trans_rule(trans),
10286 trans->msg_type);
10287 nft_rule_expr_deactivate(&trans->ctx,
10288 nft_trans_rule(trans),
10289 NFT_TRANS_COMMIT);
10290
10291 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
10292 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
10293 break;
10294 case NFT_MSG_NEWSET:
10295 if (nft_trans_set_update(trans)) {
10296 struct nft_set *set = nft_trans_set(trans);
10297
10298 WRITE_ONCE(set->timeout, nft_trans_set_timeout(trans));
10299 WRITE_ONCE(set->gc_int, nft_trans_set_gc_int(trans));
10300
10301 if (nft_trans_set_size(trans))
10302 WRITE_ONCE(set->size, nft_trans_set_size(trans));
10303 } else {
10304 nft_clear(net, nft_trans_set(trans));
10305 /* This avoids hitting -EBUSY when deleting the table
10306 * from the transaction.
10307 */
10308 if (nft_set_is_anonymous(nft_trans_set(trans)) &&
10309 !list_empty(&nft_trans_set(trans)->bindings))
10310 nft_use_dec(&trans->ctx.table->use);
10311 }
10312 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
10313 NFT_MSG_NEWSET, GFP_KERNEL);
10314 nft_trans_destroy(trans);
10315 break;
10316 case NFT_MSG_DELSET:
10317 case NFT_MSG_DESTROYSET:
10318 nft_trans_set(trans)->dead = 1;
10319 list_del_rcu(&nft_trans_set(trans)->list);
10320 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
10321 trans->msg_type, GFP_KERNEL);
10322 break;
10323 case NFT_MSG_NEWSETELEM:
10324 te = (struct nft_trans_elem *)trans->data;
10325
10326 nft_setelem_activate(net, te->set, &te->elem);
10327 nf_tables_setelem_notify(&trans->ctx, te->set,
10328 &te->elem,
10329 NFT_MSG_NEWSETELEM);
10330 if (te->set->ops->commit &&
10331 list_empty(&te->set->pending_update)) {
10332 list_add_tail(&te->set->pending_update,
10333 &set_update_list);
10334 }
10335 nft_trans_destroy(trans);
10336 break;
10337 case NFT_MSG_DELSETELEM:
10338 case NFT_MSG_DESTROYSETELEM:
10339 te = (struct nft_trans_elem *)trans->data;
10340
10341 nf_tables_setelem_notify(&trans->ctx, te->set,
10342 &te->elem,
10343 trans->msg_type);
10344 nft_setelem_remove(net, te->set, &te->elem);
10345 if (!nft_setelem_is_catchall(te->set, &te->elem)) {
10346 atomic_dec(&te->set->nelems);
10347 te->set->ndeact--;
10348 }
10349 if (te->set->ops->commit &&
10350 list_empty(&te->set->pending_update)) {
10351 list_add_tail(&te->set->pending_update,
10352 &set_update_list);
10353 }
10354 break;
10355 case NFT_MSG_NEWOBJ:
10356 if (nft_trans_obj_update(trans)) {
10357 nft_obj_commit_update(trans);
10358 nf_tables_obj_notify(&trans->ctx,
10359 nft_trans_obj(trans),
10360 NFT_MSG_NEWOBJ);
10361 } else {
10362 nft_clear(net, nft_trans_obj(trans));
10363 nf_tables_obj_notify(&trans->ctx,
10364 nft_trans_obj(trans),
10365 NFT_MSG_NEWOBJ);
10366 nft_trans_destroy(trans);
10367 }
10368 break;
10369 case NFT_MSG_DELOBJ:
10370 case NFT_MSG_DESTROYOBJ:
10371 nft_obj_del(nft_trans_obj(trans));
10372 nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
10373 trans->msg_type);
10374 break;
10375 case NFT_MSG_NEWFLOWTABLE:
10376 if (nft_trans_flowtable_update(trans)) {
10377 nft_trans_flowtable(trans)->data.flags =
10378 nft_trans_flowtable_flags(trans);
10379 nf_tables_flowtable_notify(&trans->ctx,
10380 nft_trans_flowtable(trans),
10381 &nft_trans_flowtable_hooks(trans),
10382 NFT_MSG_NEWFLOWTABLE);
10383 list_splice(&nft_trans_flowtable_hooks(trans),
10384 &nft_trans_flowtable(trans)->hook_list);
10385 } else {
10386 nft_clear(net, nft_trans_flowtable(trans));
10387 nf_tables_flowtable_notify(&trans->ctx,
10388 nft_trans_flowtable(trans),
10389 NULL,
10390 NFT_MSG_NEWFLOWTABLE);
10391 }
10392 nft_trans_destroy(trans);
10393 break;
10394 case NFT_MSG_DELFLOWTABLE:
10395 case NFT_MSG_DESTROYFLOWTABLE:
10396 if (nft_trans_flowtable_update(trans)) {
10397 nf_tables_flowtable_notify(&trans->ctx,
10398 nft_trans_flowtable(trans),
10399 &nft_trans_flowtable_hooks(trans),
10400 trans->msg_type);
10401 nft_unregister_flowtable_net_hooks(net,
10402 &nft_trans_flowtable_hooks(trans));
10403 } else {
10404 list_del_rcu(&nft_trans_flowtable(trans)->list);
10405 nf_tables_flowtable_notify(&trans->ctx,
10406 nft_trans_flowtable(trans),
10407 NULL,
10408 trans->msg_type);
10409 nft_unregister_flowtable_net_hooks(net,
10410 &nft_trans_flowtable(trans)->hook_list);
10411 }
10412 break;
10413 }
10414 }
10415
10416 nft_set_commit_update(&set_update_list);
10417
10418 nft_commit_notify(net, NETLINK_CB(skb).portid);
10419 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
10420 nf_tables_commit_audit_log(&adl, nft_net->base_seq);
10421
10422 nft_gc_seq_end(nft_net, gc_seq);
10423 nft_net->validate_state = NFT_VALIDATE_SKIP;
10424 nf_tables_commit_release(net);
10425
10426 return 0;
10427 }
10428
nf_tables_module_autoload(struct net * net)10429 static void nf_tables_module_autoload(struct net *net)
10430 {
10431 struct nftables_pernet *nft_net = nft_pernet(net);
10432 struct nft_module_request *req, *next;
10433 LIST_HEAD(module_list);
10434
10435 list_splice_init(&nft_net->module_list, &module_list);
10436 mutex_unlock(&nft_net->commit_mutex);
10437 list_for_each_entry_safe(req, next, &module_list, list) {
10438 request_module("%s", req->module);
10439 req->done = true;
10440 }
10441 mutex_lock(&nft_net->commit_mutex);
10442 list_splice(&module_list, &nft_net->module_list);
10443 }
10444
nf_tables_abort_release(struct nft_trans * trans)10445 static void nf_tables_abort_release(struct nft_trans *trans)
10446 {
10447 switch (trans->msg_type) {
10448 case NFT_MSG_NEWTABLE:
10449 nf_tables_table_destroy(&trans->ctx);
10450 break;
10451 case NFT_MSG_NEWCHAIN:
10452 if (nft_trans_chain_update(trans))
10453 nft_hooks_destroy(&nft_trans_chain_hooks(trans));
10454 else
10455 nf_tables_chain_destroy(nft_trans_chain(trans));
10456 break;
10457 case NFT_MSG_NEWRULE:
10458 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
10459 break;
10460 case NFT_MSG_NEWSET:
10461 nft_set_destroy(&trans->ctx, nft_trans_set(trans));
10462 break;
10463 case NFT_MSG_NEWSETELEM:
10464 nft_set_elem_destroy(nft_trans_elem_set(trans),
10465 nft_trans_elem(trans).priv, true);
10466 break;
10467 case NFT_MSG_NEWOBJ:
10468 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
10469 break;
10470 case NFT_MSG_NEWFLOWTABLE:
10471 if (nft_trans_flowtable_update(trans))
10472 nft_hooks_destroy(&nft_trans_flowtable_hooks(trans));
10473 else
10474 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
10475 break;
10476 }
10477 kfree(trans);
10478 }
10479
nft_set_abort_update(struct list_head * set_update_list)10480 static void nft_set_abort_update(struct list_head *set_update_list)
10481 {
10482 struct nft_set *set, *next;
10483
10484 list_for_each_entry_safe(set, next, set_update_list, pending_update) {
10485 list_del_init(&set->pending_update);
10486
10487 if (!set->ops->abort)
10488 continue;
10489
10490 set->ops->abort(set);
10491 }
10492 }
10493
__nf_tables_abort(struct net * net,enum nfnl_abort_action action)10494 static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
10495 {
10496 struct nftables_pernet *nft_net = nft_pernet(net);
10497 struct nft_trans *trans, *next;
10498 LIST_HEAD(set_update_list);
10499 struct nft_trans_elem *te;
10500 int err = 0;
10501
10502 if (action == NFNL_ABORT_VALIDATE &&
10503 nf_tables_validate(net) < 0)
10504 err = -EAGAIN;
10505
10506 list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list,
10507 list) {
10508 switch (trans->msg_type) {
10509 case NFT_MSG_NEWTABLE:
10510 if (nft_trans_table_update(trans)) {
10511 if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) {
10512 nft_trans_destroy(trans);
10513 break;
10514 }
10515 if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_DORMANT) {
10516 nf_tables_table_disable(net, trans->ctx.table);
10517 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
10518 } else if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_AWAKEN) {
10519 trans->ctx.table->flags &= ~NFT_TABLE_F_DORMANT;
10520 }
10521 trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE;
10522 nft_trans_destroy(trans);
10523 } else {
10524 list_del_rcu(&trans->ctx.table->list);
10525 }
10526 break;
10527 case NFT_MSG_DELTABLE:
10528 case NFT_MSG_DESTROYTABLE:
10529 nft_clear(trans->ctx.net, trans->ctx.table);
10530 nft_trans_destroy(trans);
10531 break;
10532 case NFT_MSG_NEWCHAIN:
10533 if (nft_trans_chain_update(trans)) {
10534 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT)) {
10535 nft_netdev_unregister_hooks(net,
10536 &nft_trans_chain_hooks(trans),
10537 true);
10538 }
10539 free_percpu(nft_trans_chain_stats(trans));
10540 kfree(nft_trans_chain_name(trans));
10541 nft_trans_destroy(trans);
10542 } else {
10543 if (nft_trans_chain_bound(trans)) {
10544 nft_trans_destroy(trans);
10545 break;
10546 }
10547 nft_use_dec_restore(&trans->ctx.table->use);
10548 nft_chain_del(trans->ctx.chain);
10549 nf_tables_unregister_hook(trans->ctx.net,
10550 trans->ctx.table,
10551 trans->ctx.chain);
10552 }
10553 break;
10554 case NFT_MSG_DELCHAIN:
10555 case NFT_MSG_DESTROYCHAIN:
10556 if (nft_trans_chain_update(trans)) {
10557 list_splice(&nft_trans_chain_hooks(trans),
10558 &nft_trans_basechain(trans)->hook_list);
10559 } else {
10560 nft_use_inc_restore(&trans->ctx.table->use);
10561 nft_clear(trans->ctx.net, trans->ctx.chain);
10562 }
10563 nft_trans_destroy(trans);
10564 break;
10565 case NFT_MSG_NEWRULE:
10566 if (nft_trans_rule_bound(trans)) {
10567 nft_trans_destroy(trans);
10568 break;
10569 }
10570 nft_use_dec_restore(&trans->ctx.chain->use);
10571 list_del_rcu(&nft_trans_rule(trans)->list);
10572 nft_rule_expr_deactivate(&trans->ctx,
10573 nft_trans_rule(trans),
10574 NFT_TRANS_ABORT);
10575 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
10576 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
10577 break;
10578 case NFT_MSG_DELRULE:
10579 case NFT_MSG_DESTROYRULE:
10580 nft_use_inc_restore(&trans->ctx.chain->use);
10581 nft_clear(trans->ctx.net, nft_trans_rule(trans));
10582 nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans));
10583 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
10584 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
10585
10586 nft_trans_destroy(trans);
10587 break;
10588 case NFT_MSG_NEWSET:
10589 if (nft_trans_set_update(trans)) {
10590 nft_trans_destroy(trans);
10591 break;
10592 }
10593 nft_use_dec_restore(&trans->ctx.table->use);
10594 if (nft_trans_set_bound(trans)) {
10595 nft_trans_destroy(trans);
10596 break;
10597 }
10598 nft_trans_set(trans)->dead = 1;
10599 list_del_rcu(&nft_trans_set(trans)->list);
10600 break;
10601 case NFT_MSG_DELSET:
10602 case NFT_MSG_DESTROYSET:
10603 nft_use_inc_restore(&trans->ctx.table->use);
10604 nft_clear(trans->ctx.net, nft_trans_set(trans));
10605 if (nft_trans_set(trans)->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
10606 nft_map_activate(&trans->ctx, nft_trans_set(trans));
10607
10608 nft_trans_destroy(trans);
10609 break;
10610 case NFT_MSG_NEWSETELEM:
10611 if (nft_trans_elem_set_bound(trans)) {
10612 nft_trans_destroy(trans);
10613 break;
10614 }
10615 te = (struct nft_trans_elem *)trans->data;
10616 nft_setelem_remove(net, te->set, &te->elem);
10617 if (!nft_setelem_is_catchall(te->set, &te->elem))
10618 atomic_dec(&te->set->nelems);
10619
10620 if (te->set->ops->abort &&
10621 list_empty(&te->set->pending_update)) {
10622 list_add_tail(&te->set->pending_update,
10623 &set_update_list);
10624 }
10625 break;
10626 case NFT_MSG_DELSETELEM:
10627 case NFT_MSG_DESTROYSETELEM:
10628 te = (struct nft_trans_elem *)trans->data;
10629
10630 if (!nft_setelem_active_next(net, te->set, &te->elem)) {
10631 nft_setelem_data_activate(net, te->set, &te->elem);
10632 nft_setelem_activate(net, te->set, &te->elem);
10633 }
10634 if (!nft_setelem_is_catchall(te->set, &te->elem))
10635 te->set->ndeact--;
10636
10637 if (te->set->ops->abort &&
10638 list_empty(&te->set->pending_update)) {
10639 list_add_tail(&te->set->pending_update,
10640 &set_update_list);
10641 }
10642 nft_trans_destroy(trans);
10643 break;
10644 case NFT_MSG_NEWOBJ:
10645 if (nft_trans_obj_update(trans)) {
10646 nft_obj_destroy(&trans->ctx, nft_trans_obj_newobj(trans));
10647 nft_trans_destroy(trans);
10648 } else {
10649 nft_use_dec_restore(&trans->ctx.table->use);
10650 nft_obj_del(nft_trans_obj(trans));
10651 }
10652 break;
10653 case NFT_MSG_DELOBJ:
10654 case NFT_MSG_DESTROYOBJ:
10655 nft_use_inc_restore(&trans->ctx.table->use);
10656 nft_clear(trans->ctx.net, nft_trans_obj(trans));
10657 nft_trans_destroy(trans);
10658 break;
10659 case NFT_MSG_NEWFLOWTABLE:
10660 if (nft_trans_flowtable_update(trans)) {
10661 nft_unregister_flowtable_net_hooks(net,
10662 &nft_trans_flowtable_hooks(trans));
10663 } else {
10664 nft_use_dec_restore(&trans->ctx.table->use);
10665 list_del_rcu(&nft_trans_flowtable(trans)->list);
10666 nft_unregister_flowtable_net_hooks(net,
10667 &nft_trans_flowtable(trans)->hook_list);
10668 }
10669 break;
10670 case NFT_MSG_DELFLOWTABLE:
10671 case NFT_MSG_DESTROYFLOWTABLE:
10672 if (nft_trans_flowtable_update(trans)) {
10673 list_splice(&nft_trans_flowtable_hooks(trans),
10674 &nft_trans_flowtable(trans)->hook_list);
10675 } else {
10676 nft_use_inc_restore(&trans->ctx.table->use);
10677 nft_clear(trans->ctx.net, nft_trans_flowtable(trans));
10678 }
10679 nft_trans_destroy(trans);
10680 break;
10681 }
10682 }
10683
10684 nft_set_abort_update(&set_update_list);
10685
10686 synchronize_rcu();
10687
10688 list_for_each_entry_safe_reverse(trans, next,
10689 &nft_net->commit_list, list) {
10690 nft_trans_list_del(trans);
10691 nf_tables_abort_release(trans);
10692 }
10693
10694 return err;
10695 }
10696
nf_tables_abort(struct net * net,struct sk_buff * skb,enum nfnl_abort_action action)10697 static int nf_tables_abort(struct net *net, struct sk_buff *skb,
10698 enum nfnl_abort_action action)
10699 {
10700 struct nftables_pernet *nft_net = nft_pernet(net);
10701 unsigned int gc_seq;
10702 int ret;
10703
10704 gc_seq = nft_gc_seq_begin(nft_net);
10705 ret = __nf_tables_abort(net, action);
10706 nft_gc_seq_end(nft_net, gc_seq);
10707
10708 WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
10709
10710 /* module autoload needs to happen after GC sequence update because it
10711 * temporarily releases and grabs mutex again.
10712 */
10713 if (action == NFNL_ABORT_AUTOLOAD)
10714 nf_tables_module_autoload(net);
10715 else
10716 nf_tables_module_autoload_cleanup(net);
10717
10718 mutex_unlock(&nft_net->commit_mutex);
10719
10720 return ret;
10721 }
10722
nf_tables_valid_genid(struct net * net,u32 genid)10723 static bool nf_tables_valid_genid(struct net *net, u32 genid)
10724 {
10725 struct nftables_pernet *nft_net = nft_pernet(net);
10726 bool genid_ok;
10727
10728 mutex_lock(&nft_net->commit_mutex);
10729
10730 genid_ok = genid == 0 || nft_net->base_seq == genid;
10731 if (!genid_ok)
10732 mutex_unlock(&nft_net->commit_mutex);
10733
10734 /* else, commit mutex has to be released by commit or abort function */
10735 return genid_ok;
10736 }
10737
10738 static const struct nfnetlink_subsystem nf_tables_subsys = {
10739 .name = "nf_tables",
10740 .subsys_id = NFNL_SUBSYS_NFTABLES,
10741 .cb_count = NFT_MSG_MAX,
10742 .cb = nf_tables_cb,
10743 .commit = nf_tables_commit,
10744 .abort = nf_tables_abort,
10745 .valid_genid = nf_tables_valid_genid,
10746 .owner = THIS_MODULE,
10747 };
10748
nft_chain_validate_dependency(const struct nft_chain * chain,enum nft_chain_types type)10749 int nft_chain_validate_dependency(const struct nft_chain *chain,
10750 enum nft_chain_types type)
10751 {
10752 const struct nft_base_chain *basechain;
10753
10754 if (nft_is_base_chain(chain)) {
10755 basechain = nft_base_chain(chain);
10756 if (basechain->type->type != type)
10757 return -EOPNOTSUPP;
10758 }
10759 return 0;
10760 }
10761 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
10762
nft_chain_validate_hooks(const struct nft_chain * chain,unsigned int hook_flags)10763 int nft_chain_validate_hooks(const struct nft_chain *chain,
10764 unsigned int hook_flags)
10765 {
10766 struct nft_base_chain *basechain;
10767
10768 if (nft_is_base_chain(chain)) {
10769 basechain = nft_base_chain(chain);
10770
10771 if ((1 << basechain->ops.hooknum) & hook_flags)
10772 return 0;
10773
10774 return -EOPNOTSUPP;
10775 }
10776
10777 return 0;
10778 }
10779 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
10780
10781 /**
10782 * nft_parse_u32_check - fetch u32 attribute and check for maximum value
10783 *
10784 * @attr: netlink attribute to fetch value from
10785 * @max: maximum value to be stored in dest
10786 * @dest: pointer to the variable
10787 *
10788 * Parse, check and store a given u32 netlink attribute into variable.
10789 * This function returns -ERANGE if the value goes over maximum value.
10790 * Otherwise a 0 is returned and the attribute value is stored in the
10791 * destination variable.
10792 */
nft_parse_u32_check(const struct nlattr * attr,int max,u32 * dest)10793 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest)
10794 {
10795 u32 val;
10796
10797 val = ntohl(nla_get_be32(attr));
10798 if (val > max)
10799 return -ERANGE;
10800
10801 *dest = val;
10802 return 0;
10803 }
10804 EXPORT_SYMBOL_GPL(nft_parse_u32_check);
10805
nft_parse_register(const struct nlattr * attr,u32 * preg)10806 static int nft_parse_register(const struct nlattr *attr, u32 *preg)
10807 {
10808 unsigned int reg;
10809
10810 reg = ntohl(nla_get_be32(attr));
10811 switch (reg) {
10812 case NFT_REG_VERDICT...NFT_REG_4:
10813 *preg = reg * NFT_REG_SIZE / NFT_REG32_SIZE;
10814 break;
10815 case NFT_REG32_00...NFT_REG32_15:
10816 *preg = reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
10817 break;
10818 default:
10819 return -ERANGE;
10820 }
10821
10822 return 0;
10823 }
10824
10825 /**
10826 * nft_dump_register - dump a register value to a netlink attribute
10827 *
10828 * @skb: socket buffer
10829 * @attr: attribute number
10830 * @reg: register number
10831 *
10832 * Construct a netlink attribute containing the register number. For
10833 * compatibility reasons, register numbers being a multiple of 4 are
10834 * translated to the corresponding 128 bit register numbers.
10835 */
nft_dump_register(struct sk_buff * skb,unsigned int attr,unsigned int reg)10836 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
10837 {
10838 if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
10839 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
10840 else
10841 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
10842
10843 return nla_put_be32(skb, attr, htonl(reg));
10844 }
10845 EXPORT_SYMBOL_GPL(nft_dump_register);
10846
nft_validate_register_load(enum nft_registers reg,unsigned int len)10847 static int nft_validate_register_load(enum nft_registers reg, unsigned int len)
10848 {
10849 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
10850 return -EINVAL;
10851 if (len == 0)
10852 return -EINVAL;
10853 if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data))
10854 return -ERANGE;
10855
10856 return 0;
10857 }
10858
nft_parse_register_load(const struct nlattr * attr,u8 * sreg,u32 len)10859 int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len)
10860 {
10861 u32 reg;
10862 int err;
10863
10864 err = nft_parse_register(attr, ®);
10865 if (err < 0)
10866 return err;
10867
10868 err = nft_validate_register_load(reg, len);
10869 if (err < 0)
10870 return err;
10871
10872 *sreg = reg;
10873 return 0;
10874 }
10875 EXPORT_SYMBOL_GPL(nft_parse_register_load);
10876
nft_validate_register_store(const struct nft_ctx * ctx,enum nft_registers reg,const struct nft_data * data,enum nft_data_types type,unsigned int len)10877 static int nft_validate_register_store(const struct nft_ctx *ctx,
10878 enum nft_registers reg,
10879 const struct nft_data *data,
10880 enum nft_data_types type,
10881 unsigned int len)
10882 {
10883 int err;
10884
10885 switch (reg) {
10886 case NFT_REG_VERDICT:
10887 if (type != NFT_DATA_VERDICT)
10888 return -EINVAL;
10889
10890 if (data != NULL &&
10891 (data->verdict.code == NFT_GOTO ||
10892 data->verdict.code == NFT_JUMP)) {
10893 err = nft_chain_validate(ctx, data->verdict.chain);
10894 if (err < 0)
10895 return err;
10896 }
10897
10898 return 0;
10899 default:
10900 if (type != NFT_DATA_VALUE)
10901 return -EINVAL;
10902
10903 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
10904 return -EINVAL;
10905 if (len == 0)
10906 return -EINVAL;
10907 if (reg * NFT_REG32_SIZE + len >
10908 sizeof_field(struct nft_regs, data))
10909 return -ERANGE;
10910
10911 return 0;
10912 }
10913 }
10914
nft_parse_register_store(const struct nft_ctx * ctx,const struct nlattr * attr,u8 * dreg,const struct nft_data * data,enum nft_data_types type,unsigned int len)10915 int nft_parse_register_store(const struct nft_ctx *ctx,
10916 const struct nlattr *attr, u8 *dreg,
10917 const struct nft_data *data,
10918 enum nft_data_types type, unsigned int len)
10919 {
10920 int err;
10921 u32 reg;
10922
10923 err = nft_parse_register(attr, ®);
10924 if (err < 0)
10925 return err;
10926
10927 err = nft_validate_register_store(ctx, reg, data, type, len);
10928 if (err < 0)
10929 return err;
10930
10931 *dreg = reg;
10932 return 0;
10933 }
10934 EXPORT_SYMBOL_GPL(nft_parse_register_store);
10935
10936 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
10937 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
10938 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
10939 .len = NFT_CHAIN_MAXNAMELEN - 1 },
10940 [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 },
10941 };
10942
nft_verdict_init(const struct nft_ctx * ctx,struct nft_data * data,struct nft_data_desc * desc,const struct nlattr * nla)10943 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
10944 struct nft_data_desc *desc, const struct nlattr *nla)
10945 {
10946 u8 genmask = nft_genmask_next(ctx->net);
10947 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
10948 struct nft_chain *chain;
10949 int err;
10950
10951 err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla,
10952 nft_verdict_policy, NULL);
10953 if (err < 0)
10954 return err;
10955
10956 if (!tb[NFTA_VERDICT_CODE])
10957 return -EINVAL;
10958
10959 /* zero padding hole for memcmp */
10960 memset(data, 0, sizeof(*data));
10961 data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
10962
10963 switch (data->verdict.code) {
10964 case NF_ACCEPT:
10965 case NF_DROP:
10966 case NF_QUEUE:
10967 break;
10968 case NFT_CONTINUE:
10969 case NFT_BREAK:
10970 case NFT_RETURN:
10971 break;
10972 case NFT_JUMP:
10973 case NFT_GOTO:
10974 if (tb[NFTA_VERDICT_CHAIN]) {
10975 chain = nft_chain_lookup(ctx->net, ctx->table,
10976 tb[NFTA_VERDICT_CHAIN],
10977 genmask);
10978 } else if (tb[NFTA_VERDICT_CHAIN_ID]) {
10979 chain = nft_chain_lookup_byid(ctx->net, ctx->table,
10980 tb[NFTA_VERDICT_CHAIN_ID],
10981 genmask);
10982 if (IS_ERR(chain))
10983 return PTR_ERR(chain);
10984 } else {
10985 return -EINVAL;
10986 }
10987
10988 if (IS_ERR(chain))
10989 return PTR_ERR(chain);
10990 if (nft_is_base_chain(chain))
10991 return -EOPNOTSUPP;
10992 if (nft_chain_is_bound(chain))
10993 return -EINVAL;
10994 if (desc->flags & NFT_DATA_DESC_SETELEM &&
10995 chain->flags & NFT_CHAIN_BINDING)
10996 return -EINVAL;
10997 if (!nft_use_inc(&chain->use))
10998 return -EMFILE;
10999
11000 data->verdict.chain = chain;
11001 break;
11002 default:
11003 return -EINVAL;
11004 }
11005
11006 desc->len = sizeof(data->verdict);
11007
11008 return 0;
11009 }
11010
nft_verdict_uninit(const struct nft_data * data)11011 static void nft_verdict_uninit(const struct nft_data *data)
11012 {
11013 struct nft_chain *chain;
11014
11015 switch (data->verdict.code) {
11016 case NFT_JUMP:
11017 case NFT_GOTO:
11018 chain = data->verdict.chain;
11019 nft_use_dec(&chain->use);
11020 break;
11021 }
11022 }
11023
nft_verdict_dump(struct sk_buff * skb,int type,const struct nft_verdict * v)11024 int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
11025 {
11026 struct nlattr *nest;
11027
11028 nest = nla_nest_start_noflag(skb, type);
11029 if (!nest)
11030 goto nla_put_failure;
11031
11032 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
11033 goto nla_put_failure;
11034
11035 switch (v->code) {
11036 case NFT_JUMP:
11037 case NFT_GOTO:
11038 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
11039 v->chain->name))
11040 goto nla_put_failure;
11041 }
11042 nla_nest_end(skb, nest);
11043 return 0;
11044
11045 nla_put_failure:
11046 return -1;
11047 }
11048
nft_value_init(const struct nft_ctx * ctx,struct nft_data * data,struct nft_data_desc * desc,const struct nlattr * nla)11049 static int nft_value_init(const struct nft_ctx *ctx,
11050 struct nft_data *data, struct nft_data_desc *desc,
11051 const struct nlattr *nla)
11052 {
11053 unsigned int len;
11054
11055 len = nla_len(nla);
11056 if (len == 0)
11057 return -EINVAL;
11058 if (len > desc->size)
11059 return -EOVERFLOW;
11060 if (desc->len) {
11061 if (len != desc->len)
11062 return -EINVAL;
11063 } else {
11064 desc->len = len;
11065 }
11066
11067 nla_memcpy(data->data, nla, len);
11068
11069 return 0;
11070 }
11071
nft_value_dump(struct sk_buff * skb,const struct nft_data * data,unsigned int len)11072 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
11073 unsigned int len)
11074 {
11075 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
11076 }
11077
11078 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
11079 [NFTA_DATA_VALUE] = { .type = NLA_BINARY },
11080 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
11081 };
11082
11083 /**
11084 * nft_data_init - parse nf_tables data netlink attributes
11085 *
11086 * @ctx: context of the expression using the data
11087 * @data: destination struct nft_data
11088 * @desc: data description
11089 * @nla: netlink attribute containing data
11090 *
11091 * Parse the netlink data attributes and initialize a struct nft_data.
11092 * The type and length of data are returned in the data description.
11093 *
11094 * The caller can indicate that it only wants to accept data of type
11095 * NFT_DATA_VALUE by passing NULL for the ctx argument.
11096 */
nft_data_init(const struct nft_ctx * ctx,struct nft_data * data,struct nft_data_desc * desc,const struct nlattr * nla)11097 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
11098 struct nft_data_desc *desc, const struct nlattr *nla)
11099 {
11100 struct nlattr *tb[NFTA_DATA_MAX + 1];
11101 int err;
11102
11103 if (WARN_ON_ONCE(!desc->size))
11104 return -EINVAL;
11105
11106 err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla,
11107 nft_data_policy, NULL);
11108 if (err < 0)
11109 return err;
11110
11111 if (tb[NFTA_DATA_VALUE]) {
11112 if (desc->type != NFT_DATA_VALUE)
11113 return -EINVAL;
11114
11115 err = nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
11116 } else if (tb[NFTA_DATA_VERDICT] && ctx != NULL) {
11117 if (desc->type != NFT_DATA_VERDICT)
11118 return -EINVAL;
11119
11120 err = nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
11121 } else {
11122 err = -EINVAL;
11123 }
11124
11125 return err;
11126 }
11127 EXPORT_SYMBOL_GPL(nft_data_init);
11128
11129 /**
11130 * nft_data_release - release a nft_data item
11131 *
11132 * @data: struct nft_data to release
11133 * @type: type of data
11134 *
11135 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
11136 * all others need to be released by calling this function.
11137 */
nft_data_release(const struct nft_data * data,enum nft_data_types type)11138 void nft_data_release(const struct nft_data *data, enum nft_data_types type)
11139 {
11140 if (type < NFT_DATA_VERDICT)
11141 return;
11142 switch (type) {
11143 case NFT_DATA_VERDICT:
11144 return nft_verdict_uninit(data);
11145 default:
11146 WARN_ON(1);
11147 }
11148 }
11149 EXPORT_SYMBOL_GPL(nft_data_release);
11150
nft_data_dump(struct sk_buff * skb,int attr,const struct nft_data * data,enum nft_data_types type,unsigned int len)11151 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
11152 enum nft_data_types type, unsigned int len)
11153 {
11154 struct nlattr *nest;
11155 int err;
11156
11157 nest = nla_nest_start_noflag(skb, attr);
11158 if (nest == NULL)
11159 return -1;
11160
11161 switch (type) {
11162 case NFT_DATA_VALUE:
11163 err = nft_value_dump(skb, data, len);
11164 break;
11165 case NFT_DATA_VERDICT:
11166 err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
11167 break;
11168 default:
11169 err = -EINVAL;
11170 WARN_ON(1);
11171 }
11172
11173 nla_nest_end(skb, nest);
11174 return err;
11175 }
11176 EXPORT_SYMBOL_GPL(nft_data_dump);
11177
__nft_release_basechain_now(struct nft_ctx * ctx)11178 static void __nft_release_basechain_now(struct nft_ctx *ctx)
11179 {
11180 struct nft_rule *rule, *nr;
11181
11182 list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
11183 list_del(&rule->list);
11184 nf_tables_rule_release(ctx, rule);
11185 }
11186 nf_tables_chain_destroy(ctx->chain);
11187 }
11188
__nft_release_basechain(struct nft_ctx * ctx)11189 int __nft_release_basechain(struct nft_ctx *ctx)
11190 {
11191 struct nft_rule *rule;
11192
11193 if (WARN_ON_ONCE(!nft_is_base_chain(ctx->chain)))
11194 return 0;
11195
11196 nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain);
11197 list_for_each_entry(rule, &ctx->chain->rules, list)
11198 nft_use_dec(&ctx->chain->use);
11199
11200 nft_chain_del(ctx->chain);
11201 nft_use_dec(&ctx->table->use);
11202
11203 if (!maybe_get_net(ctx->net)) {
11204 __nft_release_basechain_now(ctx);
11205 return 0;
11206 }
11207
11208 /* wait for ruleset dumps to complete. Owning chain is no longer in
11209 * lists, so new dumps can't find any of these rules anymore.
11210 */
11211 synchronize_rcu();
11212
11213 __nft_release_basechain_now(ctx);
11214 put_net(ctx->net);
11215 return 0;
11216 }
11217 EXPORT_SYMBOL_GPL(__nft_release_basechain);
11218
__nft_release_hook(struct net * net,struct nft_table * table)11219 static void __nft_release_hook(struct net *net, struct nft_table *table)
11220 {
11221 struct nft_flowtable *flowtable;
11222 struct nft_chain *chain;
11223
11224 list_for_each_entry(chain, &table->chains, list)
11225 __nf_tables_unregister_hook(net, table, chain, true);
11226 list_for_each_entry(flowtable, &table->flowtables, list)
11227 __nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list,
11228 true);
11229 }
11230
__nft_release_hooks(struct net * net)11231 static void __nft_release_hooks(struct net *net)
11232 {
11233 struct nftables_pernet *nft_net = nft_pernet(net);
11234 struct nft_table *table;
11235
11236 list_for_each_entry(table, &nft_net->tables, list) {
11237 if (nft_table_has_owner(table))
11238 continue;
11239
11240 __nft_release_hook(net, table);
11241 }
11242 }
11243
__nft_release_table(struct net * net,struct nft_table * table)11244 static void __nft_release_table(struct net *net, struct nft_table *table)
11245 {
11246 struct nft_flowtable *flowtable, *nf;
11247 struct nft_chain *chain, *nc;
11248 struct nft_object *obj, *ne;
11249 struct nft_rule *rule, *nr;
11250 struct nft_set *set, *ns;
11251 struct nft_ctx ctx = {
11252 .net = net,
11253 .family = NFPROTO_NETDEV,
11254 };
11255
11256 ctx.family = table->family;
11257 ctx.table = table;
11258 list_for_each_entry(chain, &table->chains, list) {
11259 if (nft_chain_binding(chain))
11260 continue;
11261
11262 ctx.chain = chain;
11263 list_for_each_entry_safe(rule, nr, &chain->rules, list) {
11264 list_del(&rule->list);
11265 nft_use_dec(&chain->use);
11266 nf_tables_rule_release(&ctx, rule);
11267 }
11268 }
11269 list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) {
11270 list_del(&flowtable->list);
11271 nft_use_dec(&table->use);
11272 nf_tables_flowtable_destroy(flowtable);
11273 }
11274 list_for_each_entry_safe(set, ns, &table->sets, list) {
11275 list_del(&set->list);
11276 nft_use_dec(&table->use);
11277 if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
11278 nft_map_deactivate(&ctx, set);
11279
11280 nft_set_destroy(&ctx, set);
11281 }
11282 list_for_each_entry_safe(obj, ne, &table->objects, list) {
11283 nft_obj_del(obj);
11284 nft_use_dec(&table->use);
11285 nft_obj_destroy(&ctx, obj);
11286 }
11287 list_for_each_entry_safe(chain, nc, &table->chains, list) {
11288 nft_chain_del(chain);
11289 nft_use_dec(&table->use);
11290 nf_tables_chain_destroy(chain);
11291 }
11292 nf_tables_table_destroy(&ctx);
11293 }
11294
__nft_release_tables(struct net * net)11295 static void __nft_release_tables(struct net *net)
11296 {
11297 struct nftables_pernet *nft_net = nft_pernet(net);
11298 struct nft_table *table, *nt;
11299
11300 list_for_each_entry_safe(table, nt, &nft_net->tables, list) {
11301 if (nft_table_has_owner(table))
11302 continue;
11303
11304 list_del(&table->list);
11305
11306 __nft_release_table(net, table);
11307 }
11308 }
11309
nft_rcv_nl_event(struct notifier_block * this,unsigned long event,void * ptr)11310 static int nft_rcv_nl_event(struct notifier_block *this, unsigned long event,
11311 void *ptr)
11312 {
11313 struct nft_table *table, *to_delete[8];
11314 struct nftables_pernet *nft_net;
11315 struct netlink_notify *n = ptr;
11316 struct net *net = n->net;
11317 unsigned int deleted;
11318 bool restart = false;
11319 unsigned int gc_seq;
11320
11321 if (event != NETLINK_URELEASE || n->protocol != NETLINK_NETFILTER)
11322 return NOTIFY_DONE;
11323
11324 nft_net = nft_pernet(net);
11325 deleted = 0;
11326 mutex_lock(&nft_net->commit_mutex);
11327
11328 gc_seq = nft_gc_seq_begin(nft_net);
11329
11330 nf_tables_trans_destroy_flush_work();
11331 again:
11332 list_for_each_entry(table, &nft_net->tables, list) {
11333 if (nft_table_has_owner(table) &&
11334 n->portid == table->nlpid) {
11335 __nft_release_hook(net, table);
11336 list_del_rcu(&table->list);
11337 to_delete[deleted++] = table;
11338 if (deleted >= ARRAY_SIZE(to_delete))
11339 break;
11340 }
11341 }
11342 if (deleted) {
11343 restart = deleted >= ARRAY_SIZE(to_delete);
11344 synchronize_rcu();
11345 while (deleted)
11346 __nft_release_table(net, to_delete[--deleted]);
11347
11348 if (restart)
11349 goto again;
11350 }
11351 nft_gc_seq_end(nft_net, gc_seq);
11352
11353 mutex_unlock(&nft_net->commit_mutex);
11354
11355 return NOTIFY_DONE;
11356 }
11357
11358 static struct notifier_block nft_nl_notifier = {
11359 .notifier_call = nft_rcv_nl_event,
11360 };
11361
nf_tables_init_net(struct net * net)11362 static int __net_init nf_tables_init_net(struct net *net)
11363 {
11364 struct nftables_pernet *nft_net = nft_pernet(net);
11365
11366 INIT_LIST_HEAD(&nft_net->tables);
11367 INIT_LIST_HEAD(&nft_net->commit_list);
11368 INIT_LIST_HEAD(&nft_net->binding_list);
11369 INIT_LIST_HEAD(&nft_net->module_list);
11370 INIT_LIST_HEAD(&nft_net->notify_list);
11371 mutex_init(&nft_net->commit_mutex);
11372 nft_net->base_seq = 1;
11373 nft_net->gc_seq = 0;
11374 nft_net->validate_state = NFT_VALIDATE_SKIP;
11375
11376 return 0;
11377 }
11378
nf_tables_pre_exit_net(struct net * net)11379 static void __net_exit nf_tables_pre_exit_net(struct net *net)
11380 {
11381 struct nftables_pernet *nft_net = nft_pernet(net);
11382
11383 mutex_lock(&nft_net->commit_mutex);
11384 __nft_release_hooks(net);
11385 mutex_unlock(&nft_net->commit_mutex);
11386 }
11387
nf_tables_exit_net(struct net * net)11388 static void __net_exit nf_tables_exit_net(struct net *net)
11389 {
11390 struct nftables_pernet *nft_net = nft_pernet(net);
11391 unsigned int gc_seq;
11392
11393 mutex_lock(&nft_net->commit_mutex);
11394
11395 gc_seq = nft_gc_seq_begin(nft_net);
11396
11397 WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
11398
11399 if (!list_empty(&nft_net->module_list))
11400 nf_tables_module_autoload_cleanup(net);
11401
11402 __nft_release_tables(net);
11403
11404 nft_gc_seq_end(nft_net, gc_seq);
11405
11406 mutex_unlock(&nft_net->commit_mutex);
11407 WARN_ON_ONCE(!list_empty(&nft_net->tables));
11408 WARN_ON_ONCE(!list_empty(&nft_net->module_list));
11409 WARN_ON_ONCE(!list_empty(&nft_net->notify_list));
11410 }
11411
nf_tables_exit_batch(struct list_head * net_exit_list)11412 static void nf_tables_exit_batch(struct list_head *net_exit_list)
11413 {
11414 flush_work(&trans_gc_work);
11415 }
11416
11417 static struct pernet_operations nf_tables_net_ops = {
11418 .init = nf_tables_init_net,
11419 .pre_exit = nf_tables_pre_exit_net,
11420 .exit = nf_tables_exit_net,
11421 .exit_batch = nf_tables_exit_batch,
11422 .id = &nf_tables_net_id,
11423 .size = sizeof(struct nftables_pernet),
11424 };
11425
nf_tables_module_init(void)11426 static int __init nf_tables_module_init(void)
11427 {
11428 int err;
11429
11430 err = register_pernet_subsys(&nf_tables_net_ops);
11431 if (err < 0)
11432 return err;
11433
11434 err = nft_chain_filter_init();
11435 if (err < 0)
11436 goto err_chain_filter;
11437
11438 err = nf_tables_core_module_init();
11439 if (err < 0)
11440 goto err_core_module;
11441
11442 err = register_netdevice_notifier(&nf_tables_flowtable_notifier);
11443 if (err < 0)
11444 goto err_netdev_notifier;
11445
11446 err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params);
11447 if (err < 0)
11448 goto err_rht_objname;
11449
11450 err = nft_offload_init();
11451 if (err < 0)
11452 goto err_offload;
11453
11454 err = netlink_register_notifier(&nft_nl_notifier);
11455 if (err < 0)
11456 goto err_netlink_notifier;
11457
11458 /* must be last */
11459 err = nfnetlink_subsys_register(&nf_tables_subsys);
11460 if (err < 0)
11461 goto err_nfnl_subsys;
11462
11463 nft_chain_route_init();
11464
11465 return err;
11466
11467 err_nfnl_subsys:
11468 netlink_unregister_notifier(&nft_nl_notifier);
11469 err_netlink_notifier:
11470 nft_offload_exit();
11471 err_offload:
11472 rhltable_destroy(&nft_objname_ht);
11473 err_rht_objname:
11474 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
11475 err_netdev_notifier:
11476 nf_tables_core_module_exit();
11477 err_core_module:
11478 nft_chain_filter_fini();
11479 err_chain_filter:
11480 unregister_pernet_subsys(&nf_tables_net_ops);
11481 return err;
11482 }
11483
nf_tables_module_exit(void)11484 static void __exit nf_tables_module_exit(void)
11485 {
11486 nfnetlink_subsys_unregister(&nf_tables_subsys);
11487 netlink_unregister_notifier(&nft_nl_notifier);
11488 nft_offload_exit();
11489 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
11490 nft_chain_filter_fini();
11491 nft_chain_route_fini();
11492 nf_tables_trans_destroy_flush_work();
11493 unregister_pernet_subsys(&nf_tables_net_ops);
11494 cancel_work_sync(&trans_gc_work);
11495 cancel_work_sync(&trans_destroy_work);
11496 rcu_barrier();
11497 rhltable_destroy(&nft_objname_ht);
11498 nf_tables_core_module_exit();
11499 }
11500
11501 module_init(nf_tables_module_init);
11502 module_exit(nf_tables_module_exit);
11503
11504 MODULE_LICENSE("GPL");
11505 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
11506 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);
11507