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