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 u64 tstamp = nft_net_tstamp(net);
6999 struct nft_set_ext *ext;
7000
7001 list_for_each_entry(catchall, &set->catchall_list, list) {
7002 ext = nft_set_elem_ext(set, catchall->elem);
7003 if (nft_set_elem_active(ext, genmask) &&
7004 !__nft_set_elem_expired(ext, tstamp) &&
7005 !nft_set_elem_is_dead(ext)) {
7006 *priv = catchall->elem;
7007 return -EEXIST;
7008 }
7009 }
7010
7011 catchall = kmalloc_obj(*catchall, GFP_KERNEL_ACCOUNT);
7012 if (!catchall)
7013 return -ENOMEM;
7014
7015 catchall->elem = elem->priv;
7016 list_add_tail_rcu(&catchall->list, &set->catchall_list);
7017
7018 return 0;
7019 }
7020
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)7021 static int nft_setelem_insert(const struct net *net,
7022 struct nft_set *set,
7023 const struct nft_set_elem *elem,
7024 struct nft_elem_priv **elem_priv,
7025 unsigned int flags)
7026 {
7027 int ret;
7028
7029 if (flags & NFT_SET_ELEM_CATCHALL)
7030 ret = nft_setelem_catchall_insert(net, set, elem, elem_priv);
7031 else
7032 ret = set->ops->insert(net, set, elem, elem_priv);
7033
7034 return ret;
7035 }
7036
nft_setelem_is_catchall(const struct nft_set * set,const struct nft_elem_priv * elem_priv)7037 static bool nft_setelem_is_catchall(const struct nft_set *set,
7038 const struct nft_elem_priv *elem_priv)
7039 {
7040 struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
7041
7042 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
7043 *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL)
7044 return true;
7045
7046 return false;
7047 }
7048
nft_setelem_activate(struct net * net,struct nft_set * set,struct nft_elem_priv * elem_priv)7049 static void nft_setelem_activate(struct net *net, struct nft_set *set,
7050 struct nft_elem_priv *elem_priv)
7051 {
7052 struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
7053
7054 if (nft_setelem_is_catchall(set, elem_priv)) {
7055 nft_clear(net, ext);
7056 } else {
7057 set->ops->activate(net, set, elem_priv);
7058 }
7059 }
7060
nft_trans_elem_update(const struct nft_set * set,const struct nft_trans_one_elem * elem)7061 static void nft_trans_elem_update(const struct nft_set *set,
7062 const struct nft_trans_one_elem *elem)
7063 {
7064 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
7065 const struct nft_elem_update *update = elem->update;
7066
7067 if (update->flags & NFT_TRANS_UPD_TIMEOUT)
7068 WRITE_ONCE(nft_set_ext_timeout(ext)->timeout, update->timeout);
7069
7070 if (update->flags & NFT_TRANS_UPD_EXPIRATION)
7071 WRITE_ONCE(nft_set_ext_timeout(ext)->expiration, get_jiffies_64() + update->expiration);
7072 }
7073
nft_trans_elems_add(const struct nft_ctx * ctx,struct nft_trans_elem * te)7074 static void nft_trans_elems_add(const struct nft_ctx *ctx,
7075 struct nft_trans_elem *te)
7076 {
7077 int i;
7078
7079 for (i = 0; i < te->nelems; i++) {
7080 struct nft_trans_one_elem *elem = &te->elems[i];
7081
7082 if (elem->update)
7083 nft_trans_elem_update(te->set, elem);
7084 else
7085 nft_setelem_activate(ctx->net, te->set, elem->priv);
7086
7087 nf_tables_setelem_notify(ctx, te->set, elem->priv,
7088 NFT_MSG_NEWSETELEM);
7089 kfree(elem->update);
7090 }
7091 }
7092
nft_setelem_catchall_deactivate(const struct net * net,struct nft_set * set,struct nft_set_elem * elem)7093 static int nft_setelem_catchall_deactivate(const struct net *net,
7094 struct nft_set *set,
7095 struct nft_set_elem *elem)
7096 {
7097 struct nft_set_elem_catchall *catchall;
7098 u64 tstamp = nft_net_tstamp(net);
7099 struct nft_set_ext *ext;
7100
7101 list_for_each_entry(catchall, &set->catchall_list, list) {
7102 ext = nft_set_elem_ext(set, catchall->elem);
7103 if (!nft_is_active_next(net, ext) ||
7104 __nft_set_elem_expired(ext, tstamp) ||
7105 nft_set_elem_is_dead(ext))
7106 continue;
7107
7108 kfree(elem->priv);
7109 elem->priv = catchall->elem;
7110 nft_set_elem_change_active(net, set, ext);
7111 return 0;
7112 }
7113
7114 return -ENOENT;
7115 }
7116
__nft_setelem_deactivate(const struct net * net,struct nft_set * set,struct nft_set_elem * elem)7117 static int __nft_setelem_deactivate(const struct net *net,
7118 struct nft_set *set,
7119 struct nft_set_elem *elem)
7120 {
7121 void *priv;
7122
7123 priv = set->ops->deactivate(net, set, elem);
7124 if (!priv)
7125 return -ENOENT;
7126
7127 kfree(elem->priv);
7128 elem->priv = priv;
7129 set->ndeact++;
7130
7131 return 0;
7132 }
7133
nft_setelem_deactivate(const struct net * net,struct nft_set * set,struct nft_set_elem * elem,u32 flags)7134 static int nft_setelem_deactivate(const struct net *net,
7135 struct nft_set *set,
7136 struct nft_set_elem *elem, u32 flags)
7137 {
7138 int ret;
7139
7140 if (flags & NFT_SET_ELEM_CATCHALL)
7141 ret = nft_setelem_catchall_deactivate(net, set, elem);
7142 else
7143 ret = __nft_setelem_deactivate(net, set, elem);
7144
7145 return ret;
7146 }
7147
nft_setelem_catchall_destroy(struct nft_set_elem_catchall * catchall)7148 static void nft_setelem_catchall_destroy(struct nft_set_elem_catchall *catchall)
7149 {
7150 list_del_rcu(&catchall->list);
7151 kfree_rcu(catchall, rcu);
7152 }
7153
nft_setelem_catchall_remove(const struct net * net,const struct nft_set * set,struct nft_elem_priv * elem_priv)7154 static void nft_setelem_catchall_remove(const struct net *net,
7155 const struct nft_set *set,
7156 struct nft_elem_priv *elem_priv)
7157 {
7158 struct nft_set_elem_catchall *catchall, *next;
7159
7160 list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
7161 if (catchall->elem == elem_priv) {
7162 nft_setelem_catchall_destroy(catchall);
7163 break;
7164 }
7165 }
7166 }
7167
nft_setelem_remove(const struct net * net,const struct nft_set * set,struct nft_elem_priv * elem_priv)7168 static void nft_setelem_remove(const struct net *net,
7169 const struct nft_set *set,
7170 struct nft_elem_priv *elem_priv)
7171 {
7172 if (nft_setelem_is_catchall(set, elem_priv))
7173 nft_setelem_catchall_remove(net, set, elem_priv);
7174 else
7175 set->ops->remove(net, set, elem_priv);
7176 }
7177
nft_trans_elems_remove(const struct nft_ctx * ctx,const struct nft_trans_elem * te,bool notify)7178 static void nft_trans_elems_remove(const struct nft_ctx *ctx,
7179 const struct nft_trans_elem *te,
7180 bool notify)
7181 {
7182 int i;
7183
7184 for (i = 0; i < te->nelems; i++) {
7185 WARN_ON_ONCE(te->elems[i].update);
7186
7187 if (notify) {
7188 nf_tables_setelem_notify(ctx, te->set,
7189 te->elems[i].priv,
7190 te->nft_trans.msg_type);
7191 }
7192
7193 nft_setelem_remove(ctx->net, te->set, te->elems[i].priv);
7194 if (!nft_setelem_is_catchall(te->set, te->elems[i].priv)) {
7195 atomic_dec(&te->set->nelems);
7196 te->set->ndeact--;
7197 }
7198 }
7199 }
7200
nft_trans_elems_remove_notify(const struct nft_ctx * ctx,const struct nft_trans_elem * te)7201 static void nft_trans_elems_remove_notify(const struct nft_ctx *ctx,
7202 const struct nft_trans_elem *te)
7203 {
7204 int i;
7205
7206 for (i = 0; i < te->nelems; i++) {
7207 WARN_ON_ONCE(te->elems[i].update);
7208
7209 nf_tables_setelem_notify(ctx, te->set,
7210 te->elems[i].priv,
7211 te->nft_trans.msg_type);
7212 }
7213 }
7214
nft_setelem_valid_key_end(const struct nft_set * set,struct nlattr ** nla,u32 flags)7215 static bool nft_setelem_valid_key_end(const struct nft_set *set,
7216 struct nlattr **nla, u32 flags)
7217 {
7218 if ((set->flags & (NFT_SET_CONCAT | NFT_SET_INTERVAL)) ==
7219 (NFT_SET_CONCAT | NFT_SET_INTERVAL)) {
7220 if (flags & NFT_SET_ELEM_INTERVAL_END)
7221 return false;
7222
7223 if (nla[NFTA_SET_ELEM_KEY_END] &&
7224 flags & NFT_SET_ELEM_CATCHALL)
7225 return false;
7226 } else {
7227 if (nla[NFTA_SET_ELEM_KEY_END])
7228 return false;
7229 }
7230
7231 return true;
7232 }
7233
nft_set_maxsize(const struct nft_set * set)7234 static u32 nft_set_maxsize(const struct nft_set *set)
7235 {
7236 u32 maxsize, delta;
7237
7238 if (!set->size)
7239 return UINT_MAX;
7240
7241 if (set->ops->adjust_maxsize)
7242 delta = set->ops->adjust_maxsize(set);
7243 else
7244 delta = 0;
7245
7246 if (check_add_overflow(set->size, set->ndeact, &maxsize))
7247 return UINT_MAX;
7248
7249 if (check_add_overflow(maxsize, delta, &maxsize))
7250 return UINT_MAX;
7251
7252 return maxsize;
7253 }
7254
nft_add_set_elem(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * attr,u32 nlmsg_flags)7255 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
7256 const struct nlattr *attr, u32 nlmsg_flags)
7257 {
7258 struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {};
7259 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
7260 u8 genmask = nft_genmask_next(ctx->net);
7261 u32 flags = 0, size = 0, num_exprs = 0;
7262 struct nft_set_ext_tmpl tmpl;
7263 struct nft_set_ext *ext, *ext2;
7264 struct nft_set_elem elem;
7265 struct nft_set_binding *binding;
7266 struct nft_elem_priv *elem_priv;
7267 struct nft_object *obj = NULL;
7268 bool override_exprs = false;
7269 struct nft_userdata *udata;
7270 struct nft_data_desc desc;
7271 enum nft_registers dreg;
7272 struct nft_trans *trans;
7273 bool set_full = false;
7274 u64 expiration;
7275 u64 timeout;
7276 int err, i;
7277 u8 ulen;
7278
7279 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
7280 nft_set_elem_policy, NULL);
7281 if (err < 0)
7282 return err;
7283
7284 nft_set_ext_prepare(&tmpl);
7285
7286 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
7287 if (err < 0)
7288 return err;
7289
7290 if (((flags & NFT_SET_ELEM_CATCHALL) && nla[NFTA_SET_ELEM_KEY]) ||
7291 (!(flags & NFT_SET_ELEM_CATCHALL) && !nla[NFTA_SET_ELEM_KEY]))
7292 return -EINVAL;
7293
7294 if (flags != 0) {
7295 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
7296 if (err < 0)
7297 return err;
7298 }
7299
7300 if (set->flags & NFT_SET_MAP) {
7301 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
7302 !(flags & NFT_SET_ELEM_INTERVAL_END))
7303 return -EINVAL;
7304 } else {
7305 if (nla[NFTA_SET_ELEM_DATA] != NULL)
7306 return -EINVAL;
7307 }
7308
7309 if (set->flags & NFT_SET_OBJECT) {
7310 if (!nla[NFTA_SET_ELEM_OBJREF] &&
7311 !(flags & NFT_SET_ELEM_INTERVAL_END))
7312 return -EINVAL;
7313 } else {
7314 if (nla[NFTA_SET_ELEM_OBJREF])
7315 return -EINVAL;
7316 }
7317
7318 if (!nft_setelem_valid_key_end(set, nla, flags))
7319 return -EINVAL;
7320
7321 if ((flags & NFT_SET_ELEM_INTERVAL_END) &&
7322 (nla[NFTA_SET_ELEM_DATA] ||
7323 nla[NFTA_SET_ELEM_OBJREF] ||
7324 nla[NFTA_SET_ELEM_TIMEOUT] ||
7325 nla[NFTA_SET_ELEM_EXPIRATION] ||
7326 nla[NFTA_SET_ELEM_USERDATA] ||
7327 nla[NFTA_SET_ELEM_EXPR] ||
7328 nla[NFTA_SET_ELEM_KEY_END] ||
7329 nla[NFTA_SET_ELEM_EXPRESSIONS]))
7330 return -EINVAL;
7331
7332 timeout = 0;
7333 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
7334 if (!(set->flags & NFT_SET_TIMEOUT))
7335 return -EINVAL;
7336 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT],
7337 &timeout);
7338 if (err)
7339 return err;
7340 } else if (set->flags & NFT_SET_TIMEOUT &&
7341 !(flags & NFT_SET_ELEM_INTERVAL_END)) {
7342 timeout = set->timeout;
7343 }
7344
7345 expiration = 0;
7346 if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) {
7347 if (!(set->flags & NFT_SET_TIMEOUT))
7348 return -EINVAL;
7349 if (timeout == 0)
7350 return -EOPNOTSUPP;
7351
7352 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION],
7353 &expiration);
7354 if (err)
7355 return err;
7356
7357 if (expiration > timeout)
7358 return -ERANGE;
7359 }
7360
7361 if (nla[NFTA_SET_ELEM_EXPR]) {
7362 struct nft_expr *expr;
7363
7364 if (set->num_exprs && set->num_exprs != 1)
7365 return -EOPNOTSUPP;
7366
7367 expr = nft_set_elem_expr_alloc(ctx, set,
7368 nla[NFTA_SET_ELEM_EXPR]);
7369 if (IS_ERR(expr))
7370 return PTR_ERR(expr);
7371
7372 expr_array[0] = expr;
7373 num_exprs = 1;
7374 override_exprs = true;
7375
7376 if (set->num_exprs && set->exprs[0]->ops != expr->ops) {
7377 err = -EOPNOTSUPP;
7378 goto err_set_elem_expr;
7379 }
7380 } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) {
7381 struct nft_expr *expr;
7382 struct nlattr *tmp;
7383 int left;
7384
7385 i = 0;
7386 nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) {
7387 if (i == NFT_SET_EXPR_MAX ||
7388 (set->num_exprs && set->num_exprs == i)) {
7389 err = -E2BIG;
7390 goto err_set_elem_expr;
7391 }
7392 if (nla_type(tmp) != NFTA_LIST_ELEM) {
7393 err = -EINVAL;
7394 goto err_set_elem_expr;
7395 }
7396 expr = nft_set_elem_expr_alloc(ctx, set, tmp);
7397 if (IS_ERR(expr)) {
7398 err = PTR_ERR(expr);
7399 goto err_set_elem_expr;
7400 }
7401 expr_array[i] = expr;
7402 num_exprs++;
7403 override_exprs = true;
7404
7405 if (set->num_exprs && expr->ops != set->exprs[i]->ops) {
7406 err = -EOPNOTSUPP;
7407 goto err_set_elem_expr;
7408 }
7409 i++;
7410 }
7411 if (set->num_exprs && set->num_exprs != i) {
7412 err = -EOPNOTSUPP;
7413 goto err_set_elem_expr;
7414 }
7415 } else if (set->num_exprs > 0 &&
7416 !(flags & NFT_SET_ELEM_INTERVAL_END)) {
7417 for (i = 0; i < set->num_exprs; i++)
7418 expr_array[i] = set->exprs[i];
7419
7420 num_exprs = set->num_exprs;
7421 }
7422
7423 if (nla[NFTA_SET_ELEM_KEY]) {
7424 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
7425 nla[NFTA_SET_ELEM_KEY]);
7426 if (err < 0)
7427 goto err_set_elem_expr;
7428
7429 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
7430 if (err < 0)
7431 goto err_parse_key;
7432 }
7433
7434 if (nla[NFTA_SET_ELEM_KEY_END]) {
7435 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
7436 nla[NFTA_SET_ELEM_KEY_END]);
7437 if (err < 0)
7438 goto err_parse_key;
7439
7440 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
7441 if (err < 0)
7442 goto err_parse_key_end;
7443 }
7444
7445 if (set->flags & NFT_SET_TIMEOUT) {
7446 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
7447 if (err < 0)
7448 goto err_parse_key_end;
7449 }
7450
7451 if (num_exprs) {
7452 for (i = 0; i < num_exprs; i++)
7453 size += expr_array[i]->ops->size;
7454
7455 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS,
7456 sizeof(struct nft_set_elem_expr) + size);
7457 if (err < 0)
7458 goto err_parse_key_end;
7459 }
7460
7461 if (nla[NFTA_SET_ELEM_OBJREF] != NULL) {
7462 obj = nft_obj_lookup(ctx->net, ctx->table,
7463 nla[NFTA_SET_ELEM_OBJREF],
7464 set->objtype, genmask);
7465 if (IS_ERR(obj)) {
7466 err = PTR_ERR(obj);
7467 obj = NULL;
7468 goto err_parse_key_end;
7469 }
7470
7471 if (!nft_use_inc(&obj->use)) {
7472 err = -EMFILE;
7473 obj = NULL;
7474 goto err_parse_key_end;
7475 }
7476
7477 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF);
7478 if (err < 0)
7479 goto err_parse_key_end;
7480 }
7481
7482 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
7483 err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val,
7484 nla[NFTA_SET_ELEM_DATA]);
7485 if (err < 0)
7486 goto err_parse_key_end;
7487
7488 dreg = nft_type_to_reg(set->dtype);
7489 list_for_each_entry(binding, &set->bindings, list) {
7490 struct nft_ctx bind_ctx = {
7491 .net = ctx->net,
7492 .family = ctx->family,
7493 .table = ctx->table,
7494 .chain = (struct nft_chain *)binding->chain,
7495 };
7496
7497 if (!(binding->flags & NFT_SET_MAP))
7498 continue;
7499
7500 err = nft_validate_register_store(&bind_ctx, dreg,
7501 &elem.data.val,
7502 desc.type, desc.len);
7503 if (err < 0)
7504 goto err_parse_data;
7505
7506 if (desc.type == NFT_DATA_VERDICT &&
7507 (elem.data.val.verdict.code == NFT_GOTO ||
7508 elem.data.val.verdict.code == NFT_JUMP))
7509 nft_validate_state_update(ctx->table,
7510 NFT_VALIDATE_NEED);
7511 }
7512
7513 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len);
7514 if (err < 0)
7515 goto err_parse_data;
7516 }
7517
7518 /* The full maximum length of userdata can exceed the maximum
7519 * offset value (U8_MAX) for following extensions, therefor it
7520 * must be the last extension added.
7521 */
7522 ulen = 0;
7523 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
7524 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
7525 if (ulen > 0) {
7526 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
7527 ulen);
7528 if (err < 0)
7529 goto err_parse_data;
7530 }
7531 }
7532
7533 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
7534 elem.key_end.val.data, elem.data.val.data,
7535 timeout, expiration, GFP_KERNEL_ACCOUNT);
7536 if (IS_ERR(elem.priv)) {
7537 err = PTR_ERR(elem.priv);
7538 goto err_parse_data;
7539 }
7540
7541 ext = nft_set_elem_ext(set, elem.priv);
7542 if (flags)
7543 *nft_set_ext_flags(ext) = flags;
7544
7545 if (obj)
7546 *nft_set_ext_obj(ext) = obj;
7547
7548 if (ulen > 0) {
7549 if (nft_set_ext_check(&tmpl, NFT_SET_EXT_USERDATA, ulen) < 0) {
7550 err = -EINVAL;
7551 goto err_elem_free;
7552 }
7553 udata = nft_set_ext_userdata(ext);
7554 udata->len = ulen - 1;
7555 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
7556 }
7557 err = nft_set_elem_expr_setup(ctx, &tmpl, ext, expr_array, num_exprs,
7558 override_exprs);
7559 if (err < 0)
7560 goto err_elem_free;
7561
7562 if (!(flags & NFT_SET_ELEM_CATCHALL)) {
7563 unsigned int max = nft_set_maxsize(set), nelems;
7564
7565 nelems = atomic_inc_return(&set->nelems);
7566 if (nelems > max)
7567 set_full = true;
7568 }
7569
7570 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
7571 if (trans == NULL) {
7572 err = -ENOMEM;
7573 goto err_set_size;
7574 }
7575
7576 ext->genmask = nft_genmask_cur(ctx->net);
7577
7578 err = nft_setelem_insert(ctx->net, set, &elem, &elem_priv, flags);
7579 if (err) {
7580 if (err == -EEXIST) {
7581 ext2 = nft_set_elem_ext(set, elem_priv);
7582 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^
7583 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) ||
7584 nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^
7585 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF))
7586 goto err_element_clash;
7587 if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
7588 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) &&
7589 memcmp(nft_set_ext_data(ext),
7590 nft_set_ext_data(ext2), set->dlen) != 0) ||
7591 (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
7592 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) &&
7593 *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2)))
7594 goto err_element_clash;
7595 else if (!(nlmsg_flags & NLM_F_EXCL)) {
7596 err = 0;
7597 if (nft_set_ext_exists(ext2, NFT_SET_EXT_TIMEOUT)) {
7598 struct nft_elem_update update = { };
7599
7600 if (timeout != nft_set_ext_timeout(ext2)->timeout) {
7601 update.timeout = timeout;
7602 if (expiration == 0)
7603 expiration = timeout;
7604
7605 update.flags |= NFT_TRANS_UPD_TIMEOUT;
7606 }
7607 if (expiration) {
7608 update.expiration = expiration;
7609 update.flags |= NFT_TRANS_UPD_EXPIRATION;
7610 }
7611
7612 if (update.flags) {
7613 struct nft_trans_one_elem *ue;
7614
7615 ue = &nft_trans_container_elem(trans)->elems[0];
7616
7617 ue->update = kmemdup(&update, sizeof(update), GFP_KERNEL);
7618 if (!ue->update) {
7619 err = -ENOMEM;
7620 goto err_element_clash;
7621 }
7622
7623 ue->priv = elem_priv;
7624 nft_trans_commit_list_add_elem(ctx->net, trans);
7625 goto err_set_size;
7626 }
7627 }
7628 }
7629 } else if (err == -ENOTEMPTY) {
7630 /* ENOTEMPTY reports overlapping between this element
7631 * and an existing one.
7632 */
7633 err = -EEXIST;
7634 } else if (err == -ECANCELED) {
7635 /* ECANCELED reports an existing nul-element in
7636 * interval sets.
7637 */
7638 err = 0;
7639 }
7640 goto err_element_clash;
7641 }
7642
7643 nft_trans_container_elem(trans)->elems[0].priv = elem.priv;
7644 nft_trans_commit_list_add_elem(ctx->net, trans);
7645
7646 return set_full ? -ENFILE : 0;
7647
7648 err_element_clash:
7649 kfree(trans);
7650 err_set_size:
7651 if (!(flags & NFT_SET_ELEM_CATCHALL))
7652 atomic_dec(&set->nelems);
7653 err_elem_free:
7654 nf_tables_set_elem_destroy(ctx, set, elem.priv);
7655 err_parse_data:
7656 if (nla[NFTA_SET_ELEM_DATA] != NULL)
7657 nft_data_release(&elem.data.val, desc.type);
7658 err_parse_key_end:
7659 if (obj)
7660 nft_use_dec_restore(&obj->use);
7661
7662 nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
7663 err_parse_key:
7664 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
7665 err_set_elem_expr:
7666 if (override_exprs) {
7667 for (i = 0; i < num_exprs && expr_array[i]; i++)
7668 nft_expr_destroy(ctx, expr_array[i]);
7669 }
7670
7671 return err;
7672 }
7673
nf_tables_newsetelem(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])7674 static int nf_tables_newsetelem(struct sk_buff *skb,
7675 const struct nfnl_info *info,
7676 const struct nlattr * const nla[])
7677 {
7678 struct netlink_ext_ack *extack = info->extack;
7679 u8 genmask = nft_genmask_next(info->net);
7680 u8 family = info->nfmsg->nfgen_family;
7681 struct net *net = info->net;
7682 const struct nlattr *attr;
7683 struct nft_table *table;
7684 struct nft_set *set;
7685 struct nft_ctx ctx;
7686 int rem, err;
7687
7688 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
7689 return -EINVAL;
7690
7691 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
7692 genmask, NETLINK_CB(skb).portid);
7693 if (IS_ERR(table)) {
7694 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
7695 return PTR_ERR(table);
7696 }
7697
7698 set = nft_set_lookup_global(net, table, nla[NFTA_SET_ELEM_LIST_SET],
7699 nla[NFTA_SET_ELEM_LIST_SET_ID], genmask);
7700 if (IS_ERR(set)) {
7701 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_SET]);
7702 return PTR_ERR(set);
7703 }
7704
7705 if (!list_empty(&set->bindings) &&
7706 (set->flags & (NFT_SET_CONSTANT | NFT_SET_ANONYMOUS)))
7707 return -EBUSY;
7708
7709 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7710
7711 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
7712 err = nft_add_set_elem(&ctx, set, attr, info->nlh->nlmsg_flags);
7713 if (err < 0) {
7714 NL_SET_BAD_ATTR(extack, attr);
7715 return err;
7716 }
7717 }
7718
7719 if (table->validate_state == NFT_VALIDATE_DO)
7720 return nft_table_validate(net, table);
7721
7722 return 0;
7723 }
7724
7725 /**
7726 * nft_data_hold - hold a nft_data item
7727 *
7728 * @data: struct nft_data to release
7729 * @type: type of data
7730 *
7731 * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded,
7732 * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and
7733 * NFT_GOTO verdicts. This function must be called on active data objects
7734 * from the second phase of the commit protocol.
7735 */
nft_data_hold(const struct nft_data * data,enum nft_data_types type)7736 void nft_data_hold(const struct nft_data *data, enum nft_data_types type)
7737 {
7738 struct nft_chain *chain;
7739
7740 if (type == NFT_DATA_VERDICT) {
7741 switch (data->verdict.code) {
7742 case NFT_JUMP:
7743 case NFT_GOTO:
7744 chain = data->verdict.chain;
7745 nft_use_inc_restore(&chain->use);
7746 break;
7747 }
7748 }
7749 }
7750
nft_setelem_active_next(const struct net * net,const struct nft_set * set,struct nft_elem_priv * elem_priv)7751 static int nft_setelem_active_next(const struct net *net,
7752 const struct nft_set *set,
7753 struct nft_elem_priv *elem_priv)
7754 {
7755 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
7756 u8 genmask = nft_genmask_next(net);
7757
7758 return nft_set_elem_active(ext, genmask);
7759 }
7760
nft_setelem_data_activate(const struct net * net,const struct nft_set * set,struct nft_elem_priv * elem_priv)7761 static void nft_setelem_data_activate(const struct net *net,
7762 const struct nft_set *set,
7763 struct nft_elem_priv *elem_priv)
7764 {
7765 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
7766
7767 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
7768 nft_data_hold(nft_set_ext_data(ext), set->dtype);
7769 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
7770 nft_use_inc_restore(&(*nft_set_ext_obj(ext))->use);
7771 }
7772
nft_setelem_data_deactivate(const struct net * net,const struct nft_set * set,struct nft_elem_priv * elem_priv)7773 void nft_setelem_data_deactivate(const struct net *net,
7774 const struct nft_set *set,
7775 struct nft_elem_priv *elem_priv)
7776 {
7777 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
7778
7779 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
7780 nft_data_release(nft_set_ext_data(ext), set->dtype);
7781 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
7782 nft_use_dec(&(*nft_set_ext_obj(ext))->use);
7783 }
7784
7785 /* similar to nft_trans_elems_remove, but called from abort path to undo newsetelem.
7786 * No notifications and no ndeact changes.
7787 *
7788 * Returns true if set had been added to (i.e., elements need to be removed again).
7789 */
nft_trans_elems_new_abort(const struct nft_ctx * ctx,struct nft_trans_elem * te)7790 static bool nft_trans_elems_new_abort(const struct nft_ctx *ctx,
7791 struct nft_trans_elem *te)
7792 {
7793 bool removed = false;
7794 int i;
7795
7796 for (i = 0; i < te->nelems; i++) {
7797 if (te->elems[i].update) {
7798 kfree(te->elems[i].update);
7799 te->elems[i].update = NULL;
7800 /* Update request, so do not release this element */
7801 te->elems[i].priv = NULL;
7802 continue;
7803 }
7804
7805 if (!te->set->ops->abort_skip_removal ||
7806 nft_setelem_is_catchall(te->set, te->elems[i].priv))
7807 nft_setelem_remove(ctx->net, te->set, te->elems[i].priv);
7808
7809 if (!nft_setelem_is_catchall(te->set, te->elems[i].priv))
7810 atomic_dec(&te->set->nelems);
7811
7812 removed = true;
7813 }
7814
7815 return removed;
7816 }
7817
7818 /* Called from abort path to undo DELSETELEM/DESTROYSETELEM. */
nft_trans_elems_destroy_abort(const struct nft_ctx * ctx,const struct nft_trans_elem * te)7819 static void nft_trans_elems_destroy_abort(const struct nft_ctx *ctx,
7820 const struct nft_trans_elem *te)
7821 {
7822 int i;
7823
7824 for (i = 0; i < te->nelems; i++) {
7825 if (!nft_setelem_active_next(ctx->net, te->set, te->elems[i].priv)) {
7826 nft_setelem_data_activate(ctx->net, te->set, te->elems[i].priv);
7827 nft_setelem_activate(ctx->net, te->set, te->elems[i].priv);
7828 }
7829
7830 if (!nft_setelem_is_catchall(te->set, te->elems[i].priv))
7831 te->set->ndeact--;
7832 }
7833 }
7834
nft_del_setelem(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * attr)7835 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
7836 const struct nlattr *attr)
7837 {
7838 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
7839 struct nft_set_ext_tmpl tmpl;
7840 struct nft_set_elem elem;
7841 struct nft_set_ext *ext;
7842 struct nft_trans *trans;
7843 u32 flags = 0;
7844 int err;
7845
7846 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
7847 nft_set_elem_policy, NULL);
7848 if (err < 0)
7849 return err;
7850
7851 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
7852 if (err < 0)
7853 return err;
7854
7855 if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL))
7856 return -EINVAL;
7857
7858 if (!nft_setelem_valid_key_end(set, nla, flags))
7859 return -EINVAL;
7860
7861 nft_set_ext_prepare(&tmpl);
7862
7863 if (flags != 0) {
7864 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
7865 if (err < 0)
7866 return err;
7867 }
7868
7869 if (nla[NFTA_SET_ELEM_KEY]) {
7870 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
7871 nla[NFTA_SET_ELEM_KEY]);
7872 if (err < 0)
7873 return err;
7874
7875 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
7876 if (err < 0)
7877 goto fail_elem;
7878 }
7879
7880 if (nla[NFTA_SET_ELEM_KEY_END]) {
7881 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
7882 nla[NFTA_SET_ELEM_KEY_END]);
7883 if (err < 0)
7884 goto fail_elem;
7885
7886 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
7887 if (err < 0)
7888 goto fail_elem_key_end;
7889 }
7890
7891 err = -ENOMEM;
7892 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
7893 elem.key_end.val.data, NULL, 0, 0,
7894 GFP_KERNEL_ACCOUNT);
7895 if (IS_ERR(elem.priv)) {
7896 err = PTR_ERR(elem.priv);
7897 goto fail_elem_key_end;
7898 }
7899
7900 ext = nft_set_elem_ext(set, elem.priv);
7901 if (flags)
7902 *nft_set_ext_flags(ext) = flags;
7903
7904 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
7905 if (trans == NULL)
7906 goto fail_trans;
7907
7908 err = nft_setelem_deactivate(ctx->net, set, &elem, flags);
7909 if (err < 0)
7910 goto fail_ops;
7911
7912 nft_setelem_data_deactivate(ctx->net, set, elem.priv);
7913
7914 nft_trans_container_elem(trans)->elems[0].priv = elem.priv;
7915 nft_trans_commit_list_add_elem(ctx->net, trans);
7916 return 0;
7917
7918 fail_ops:
7919 kfree(trans);
7920 fail_trans:
7921 kfree(elem.priv);
7922 fail_elem_key_end:
7923 nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
7924 fail_elem:
7925 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
7926 return err;
7927 }
7928
nft_setelem_flush(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_elem_priv * elem_priv)7929 static int nft_setelem_flush(const struct nft_ctx *ctx,
7930 struct nft_set *set,
7931 const struct nft_set_iter *iter,
7932 struct nft_elem_priv *elem_priv)
7933 {
7934 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem_priv);
7935 struct nft_trans *trans;
7936
7937 if (!nft_set_elem_active(ext, iter->genmask))
7938 return 0;
7939
7940 trans = nft_trans_alloc(ctx, NFT_MSG_DELSETELEM,
7941 struct_size_t(struct nft_trans_elem, elems, 1));
7942 if (!trans)
7943 return -ENOMEM;
7944
7945 set->ops->flush(ctx->net, set, elem_priv);
7946 set->ndeact++;
7947
7948 nft_setelem_data_deactivate(ctx->net, set, elem_priv);
7949 nft_trans_elem_set(trans) = set;
7950 nft_trans_container_elem(trans)->nelems = 1;
7951 nft_trans_container_elem(trans)->elems[0].priv = elem_priv;
7952 nft_trans_commit_list_add_elem(ctx->net, trans);
7953
7954 return 0;
7955 }
7956
__nft_set_catchall_flush(const struct nft_ctx * ctx,struct nft_set * set,struct nft_elem_priv * elem_priv)7957 static int __nft_set_catchall_flush(const struct nft_ctx *ctx,
7958 struct nft_set *set,
7959 struct nft_elem_priv *elem_priv)
7960 {
7961 struct nft_trans *trans;
7962
7963 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
7964 if (!trans)
7965 return -ENOMEM;
7966
7967 nft_setelem_data_deactivate(ctx->net, set, elem_priv);
7968 nft_trans_container_elem(trans)->elems[0].priv = elem_priv;
7969 nft_trans_commit_list_add_elem(ctx->net, trans);
7970
7971 return 0;
7972 }
7973
nft_set_catchall_flush(const struct nft_ctx * ctx,struct nft_set * set)7974 static int nft_set_catchall_flush(const struct nft_ctx *ctx,
7975 struct nft_set *set)
7976 {
7977 u8 genmask = nft_genmask_next(ctx->net);
7978 struct nft_set_elem_catchall *catchall;
7979 struct nft_set_ext *ext;
7980 int ret = 0;
7981
7982 list_for_each_entry_rcu(catchall, &set->catchall_list, list,
7983 lockdep_commit_lock_is_held(ctx->net)) {
7984 ext = nft_set_elem_ext(set, catchall->elem);
7985 if (!nft_set_elem_active(ext, genmask))
7986 continue;
7987
7988 ret = __nft_set_catchall_flush(ctx, set, catchall->elem);
7989 if (ret < 0)
7990 break;
7991 nft_set_elem_change_active(ctx->net, set, ext);
7992 }
7993
7994 return ret;
7995 }
7996
nft_set_flush(struct nft_ctx * ctx,struct nft_set * set,u8 genmask)7997 static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask)
7998 {
7999 /* The set backend might need to clone the set, do it now from the
8000 * preparation phase, use NFT_ITER_UPDATE_CLONE iterator type.
8001 */
8002 struct nft_set_iter iter = {
8003 .genmask = genmask,
8004 .type = NFT_ITER_UPDATE_CLONE,
8005 .fn = nft_setelem_flush,
8006 };
8007
8008 set->ops->walk(ctx, set, &iter);
8009 if (!iter.err)
8010 iter.err = nft_set_catchall_flush(ctx, set);
8011
8012 return iter.err;
8013 }
8014
nf_tables_delsetelem(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8015 static int nf_tables_delsetelem(struct sk_buff *skb,
8016 const struct nfnl_info *info,
8017 const struct nlattr * const nla[])
8018 {
8019 struct netlink_ext_ack *extack = info->extack;
8020 u8 genmask = nft_genmask_next(info->net);
8021 u8 family = info->nfmsg->nfgen_family;
8022 struct net *net = info->net;
8023 const struct nlattr *attr;
8024 struct nft_table *table;
8025 struct nft_set *set;
8026 struct nft_ctx ctx;
8027 int rem, err = 0;
8028
8029 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
8030 genmask, NETLINK_CB(skb).portid);
8031 if (IS_ERR(table)) {
8032 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
8033 return PTR_ERR(table);
8034 }
8035
8036 set = nft_set_lookup(net, table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
8037 if (IS_ERR(set)) {
8038 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_SET]);
8039 return PTR_ERR(set);
8040 }
8041
8042 if (nft_set_is_anonymous(set))
8043 return -EOPNOTSUPP;
8044
8045 if (!list_empty(&set->bindings) && (set->flags & NFT_SET_CONSTANT))
8046 return -EBUSY;
8047
8048 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
8049
8050 if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
8051 return nft_set_flush(&ctx, set, genmask);
8052
8053 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
8054 err = nft_del_setelem(&ctx, set, attr);
8055 if (err == -ENOENT &&
8056 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSETELEM)
8057 continue;
8058
8059 if (err < 0) {
8060 NL_SET_BAD_ATTR(extack, attr);
8061 return err;
8062 }
8063 }
8064
8065 return 0;
8066 }
8067
8068 /*
8069 * Stateful objects
8070 */
8071
8072 /**
8073 * nft_register_obj- register nf_tables stateful object type
8074 * @obj_type: object type
8075 *
8076 * Registers the object type for use with nf_tables. Returns zero on
8077 * success or a negative errno code otherwise.
8078 */
nft_register_obj(struct nft_object_type * obj_type)8079 int nft_register_obj(struct nft_object_type *obj_type)
8080 {
8081 if (obj_type->type == NFT_OBJECT_UNSPEC)
8082 return -EINVAL;
8083
8084 nfnl_lock(NFNL_SUBSYS_NFTABLES);
8085 list_add_rcu(&obj_type->list, &nf_tables_objects);
8086 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
8087 return 0;
8088 }
8089 EXPORT_SYMBOL_GPL(nft_register_obj);
8090
8091 /**
8092 * nft_unregister_obj - unregister nf_tables object type
8093 * @obj_type: object type
8094 *
8095 * Unregisters the object type for use with nf_tables.
8096 */
nft_unregister_obj(struct nft_object_type * obj_type)8097 void nft_unregister_obj(struct nft_object_type *obj_type)
8098 {
8099 nfnl_lock(NFNL_SUBSYS_NFTABLES);
8100 list_del_rcu(&obj_type->list);
8101 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
8102 }
8103 EXPORT_SYMBOL_GPL(nft_unregister_obj);
8104
nft_obj_lookup(const struct net * net,struct nft_table * table,const struct nlattr * nla,u32 objtype,u8 genmask)8105 struct nft_object *nft_obj_lookup(const struct net *net,
8106 struct nft_table *table,
8107 const struct nlattr *nla, u32 objtype,
8108 u8 genmask)
8109 {
8110 struct nft_object_hash_key k = { .table = table };
8111 char search[NFT_OBJ_MAXNAMELEN];
8112 struct rhlist_head *tmp, *list;
8113 struct nft_object *obj;
8114
8115 nla_strscpy(search, nla, sizeof(search));
8116 k.name = search;
8117
8118 WARN_ON_ONCE(!rcu_read_lock_held() &&
8119 !lockdep_commit_lock_is_held(net));
8120
8121 rcu_read_lock();
8122 list = rhltable_lookup(&table->objname_ht, &k, nft_objname_ht_params);
8123 if (!list)
8124 goto out;
8125
8126 rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) {
8127 if (objtype == obj->ops->type->type &&
8128 nft_active_genmask(obj, genmask)) {
8129 rcu_read_unlock();
8130 return obj;
8131 }
8132 }
8133 out:
8134 rcu_read_unlock();
8135 return ERR_PTR(-ENOENT);
8136 }
8137
nft_obj_lookup_byhandle(const struct nft_table * table,const struct nlattr * nla,u32 objtype,u8 genmask)8138 static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table,
8139 const struct nlattr *nla,
8140 u32 objtype, u8 genmask)
8141 {
8142 struct nft_object *obj;
8143
8144 list_for_each_entry(obj, &table->objects, list) {
8145 if (be64_to_cpu(nla_get_be64(nla)) == obj->handle &&
8146 objtype == obj->ops->type->type &&
8147 nft_active_genmask(obj, genmask))
8148 return obj;
8149 }
8150 return ERR_PTR(-ENOENT);
8151 }
8152
8153 static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = {
8154 [NFTA_OBJ_TABLE] = { .type = NLA_STRING,
8155 .len = NFT_TABLE_MAXNAMELEN - 1 },
8156 [NFTA_OBJ_NAME] = { .type = NLA_STRING,
8157 .len = NFT_OBJ_MAXNAMELEN - 1 },
8158 [NFTA_OBJ_TYPE] = { .type = NLA_U32 },
8159 [NFTA_OBJ_DATA] = { .type = NLA_NESTED },
8160 [NFTA_OBJ_HANDLE] = { .type = NLA_U64},
8161 [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY,
8162 .len = NFT_USERDATA_MAXLEN },
8163 };
8164
nft_obj_init(const struct nft_ctx * ctx,const struct nft_object_type * type,const struct nlattr * attr)8165 static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
8166 const struct nft_object_type *type,
8167 const struct nlattr *attr)
8168 {
8169 struct nlattr **tb;
8170 const struct nft_object_ops *ops;
8171 struct nft_object *obj;
8172 int err = -ENOMEM;
8173
8174 tb = kmalloc_objs(*tb, type->maxattr + 1);
8175 if (!tb)
8176 goto err1;
8177
8178 if (attr) {
8179 err = nla_parse_nested_deprecated(tb, type->maxattr, attr,
8180 type->policy, NULL);
8181 if (err < 0)
8182 goto err2;
8183 } else {
8184 memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1));
8185 }
8186
8187 if (type->select_ops) {
8188 ops = type->select_ops(ctx, (const struct nlattr * const *)tb);
8189 if (IS_ERR(ops)) {
8190 err = PTR_ERR(ops);
8191 goto err2;
8192 }
8193 } else {
8194 ops = type->ops;
8195 }
8196
8197 err = -ENOMEM;
8198 obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL_ACCOUNT);
8199 if (!obj)
8200 goto err2;
8201
8202 err = ops->init(ctx, (const struct nlattr * const *)tb, obj);
8203 if (err < 0)
8204 goto err3;
8205
8206 obj->ops = ops;
8207
8208 kfree(tb);
8209 return obj;
8210 err3:
8211 kfree(obj);
8212 err2:
8213 kfree(tb);
8214 err1:
8215 return ERR_PTR(err);
8216 }
8217
nft_object_dump(struct sk_buff * skb,unsigned int attr,struct nft_object * obj,bool reset)8218 static int nft_object_dump(struct sk_buff *skb, unsigned int attr,
8219 struct nft_object *obj, bool reset)
8220 {
8221 struct nlattr *nest;
8222
8223 nest = nla_nest_start_noflag(skb, attr);
8224 if (!nest)
8225 goto nla_put_failure;
8226 if (obj->ops->dump(skb, obj, reset) < 0)
8227 goto nla_put_failure;
8228 nla_nest_end(skb, nest);
8229 return 0;
8230
8231 nla_put_failure:
8232 return -1;
8233 }
8234
__nft_obj_type_get(u32 objtype,u8 family)8235 static const struct nft_object_type *__nft_obj_type_get(u32 objtype, u8 family)
8236 {
8237 const struct nft_object_type *type;
8238
8239 list_for_each_entry_rcu(type, &nf_tables_objects, list) {
8240 if (type->family != NFPROTO_UNSPEC &&
8241 type->family != family)
8242 continue;
8243
8244 if (objtype == type->type)
8245 return type;
8246 }
8247 return NULL;
8248 }
8249
8250 static const struct nft_object_type *
nft_obj_type_get(struct net * net,u32 objtype,u8 family)8251 nft_obj_type_get(struct net *net, u32 objtype, u8 family)
8252 {
8253 const struct nft_object_type *type;
8254
8255 rcu_read_lock();
8256 type = __nft_obj_type_get(objtype, family);
8257 if (type != NULL && try_module_get(type->owner)) {
8258 rcu_read_unlock();
8259 return type;
8260 }
8261 rcu_read_unlock();
8262
8263 lockdep_nfnl_nft_mutex_not_held();
8264 #ifdef CONFIG_MODULES
8265 if (type == NULL) {
8266 if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN)
8267 return ERR_PTR(-EAGAIN);
8268 }
8269 #endif
8270 return ERR_PTR(-ENOENT);
8271 }
8272
nf_tables_updobj(const struct nft_ctx * ctx,const struct nft_object_type * type,const struct nlattr * attr,struct nft_object * obj)8273 static int nf_tables_updobj(const struct nft_ctx *ctx,
8274 const struct nft_object_type *type,
8275 const struct nlattr *attr,
8276 struct nft_object *obj)
8277 {
8278 struct nft_object *newobj;
8279 struct nft_trans *trans;
8280 int err = -ENOMEM;
8281
8282 /* caller must have obtained type->owner reference. */
8283 trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ,
8284 sizeof(struct nft_trans_obj));
8285 if (!trans)
8286 goto err_trans;
8287
8288 newobj = nft_obj_init(ctx, type, attr);
8289 if (IS_ERR(newobj)) {
8290 err = PTR_ERR(newobj);
8291 goto err_free_trans;
8292 }
8293
8294 nft_trans_obj(trans) = obj;
8295 nft_trans_obj_update(trans) = true;
8296 nft_trans_obj_newobj(trans) = newobj;
8297 nft_trans_commit_list_add_tail(ctx->net, trans);
8298
8299 return 0;
8300
8301 err_free_trans:
8302 kfree(trans);
8303 err_trans:
8304 module_put(type->owner);
8305 return err;
8306 }
8307
nf_tables_newobj(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8308 static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info,
8309 const struct nlattr * const nla[])
8310 {
8311 struct netlink_ext_ack *extack = info->extack;
8312 u8 genmask = nft_genmask_next(info->net);
8313 u8 family = info->nfmsg->nfgen_family;
8314 const struct nft_object_type *type;
8315 struct net *net = info->net;
8316 struct nft_table *table;
8317 struct nft_object *obj;
8318 struct nft_ctx ctx;
8319 u32 objtype;
8320 int err;
8321
8322 if (!nla[NFTA_OBJ_TYPE] ||
8323 !nla[NFTA_OBJ_NAME] ||
8324 !nla[NFTA_OBJ_DATA])
8325 return -EINVAL;
8326
8327 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask,
8328 NETLINK_CB(skb).portid);
8329 if (IS_ERR(table)) {
8330 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
8331 return PTR_ERR(table);
8332 }
8333
8334 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
8335 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
8336 if (IS_ERR(obj)) {
8337 err = PTR_ERR(obj);
8338 if (err != -ENOENT) {
8339 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
8340 return err;
8341 }
8342 } else {
8343 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
8344 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
8345 return -EEXIST;
8346 }
8347 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
8348 return -EOPNOTSUPP;
8349
8350 if (!obj->ops->update)
8351 return 0;
8352
8353 type = nft_obj_type_get(net, objtype, family);
8354 if (IS_ERR(type)) {
8355 DEBUG_NET_WARN_ON_ONCE(1);
8356 return PTR_ERR(type);
8357 }
8358
8359 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
8360
8361 /* type->owner reference is put when transaction object is released. */
8362 return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj);
8363 }
8364
8365 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
8366
8367 if (!nft_use_inc(&table->use))
8368 return -EMFILE;
8369
8370 type = nft_obj_type_get(net, objtype, family);
8371 if (IS_ERR(type)) {
8372 err = PTR_ERR(type);
8373 goto err_type;
8374 }
8375
8376 obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]);
8377 if (IS_ERR(obj)) {
8378 err = PTR_ERR(obj);
8379 goto err_init;
8380 }
8381 obj->key.table = table;
8382 obj->handle = nf_tables_alloc_handle(table);
8383
8384 obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL_ACCOUNT);
8385 if (!obj->key.name) {
8386 err = -ENOMEM;
8387 goto err_strdup;
8388 }
8389
8390 if (nla[NFTA_OBJ_USERDATA]) {
8391 obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL_ACCOUNT);
8392 if (obj->udata == NULL)
8393 goto err_userdata;
8394
8395 obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]);
8396 }
8397
8398 err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj);
8399 if (err < 0)
8400 goto err_trans;
8401
8402 err = rhltable_insert(&table->objname_ht, &obj->rhlhead,
8403 nft_objname_ht_params);
8404 if (err < 0)
8405 goto err_obj_ht;
8406
8407 list_add_tail_rcu(&obj->list, &table->objects);
8408
8409 return 0;
8410 err_obj_ht:
8411 /* queued in transaction log */
8412 INIT_LIST_HEAD(&obj->list);
8413 return err;
8414 err_trans:
8415 kfree(obj->udata);
8416 err_userdata:
8417 kfree(obj->key.name);
8418 err_strdup:
8419 if (obj->ops->destroy)
8420 obj->ops->destroy(&ctx, obj);
8421 kfree(obj);
8422 err_init:
8423 module_put(type->owner);
8424 err_type:
8425 nft_use_dec_restore(&table->use);
8426
8427 return err;
8428 }
8429
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)8430 static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net,
8431 u32 portid, u32 seq, int event, u32 flags,
8432 int family, const struct nft_table *table,
8433 struct nft_object *obj, bool reset)
8434 {
8435 struct nlmsghdr *nlh;
8436
8437 nlh = nfnl_msg_put(skb, portid, seq,
8438 nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event),
8439 flags, family, NFNETLINK_V0, nft_base_seq_be16(net));
8440 if (!nlh)
8441 goto nla_put_failure;
8442
8443 if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) ||
8444 nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) ||
8445 nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) ||
8446 nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle),
8447 NFTA_OBJ_PAD))
8448 goto nla_put_failure;
8449
8450 if (event == NFT_MSG_DELOBJ ||
8451 event == NFT_MSG_DESTROYOBJ) {
8452 nlmsg_end(skb, nlh);
8453 return 0;
8454 }
8455
8456 if (nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) ||
8457 nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset))
8458 goto nla_put_failure;
8459
8460 if (obj->udata &&
8461 nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata))
8462 goto nla_put_failure;
8463
8464 nlmsg_end(skb, nlh);
8465 return 0;
8466
8467 nla_put_failure:
8468 nlmsg_trim(skb, nlh);
8469 return -1;
8470 }
8471
audit_log_obj_reset(const struct nft_table * table,unsigned int base_seq,unsigned int nentries)8472 static void audit_log_obj_reset(const struct nft_table *table,
8473 unsigned int base_seq, unsigned int nentries)
8474 {
8475 char *buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, base_seq);
8476
8477 audit_log_nfcfg(buf, table->family, nentries,
8478 AUDIT_NFT_OP_OBJ_RESET, GFP_ATOMIC);
8479 kfree(buf);
8480 }
8481
8482 struct nft_obj_dump_ctx {
8483 unsigned int s_idx;
8484 char *table;
8485 u32 type;
8486 bool reset;
8487 };
8488
nf_tables_dump_obj(struct sk_buff * skb,struct netlink_callback * cb)8489 static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)
8490 {
8491 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
8492 struct nft_obj_dump_ctx *ctx = (void *)cb->ctx;
8493 struct net *net = sock_net(skb->sk);
8494 int family = nfmsg->nfgen_family;
8495 struct nftables_pernet *nft_net;
8496 const struct nft_table *table;
8497 unsigned int entries = 0;
8498 struct nft_object *obj;
8499 unsigned int idx = 0;
8500 int rc = 0;
8501
8502 rcu_read_lock();
8503 nft_net = nft_pernet(net);
8504 cb->seq = nft_base_seq(net);
8505
8506 list_for_each_entry_rcu(table, &nft_net->tables, list) {
8507 if (family != NFPROTO_UNSPEC && family != table->family)
8508 continue;
8509
8510 entries = 0;
8511 list_for_each_entry_rcu(obj, &table->objects, list) {
8512 if (!nft_is_active(net, obj))
8513 goto cont;
8514 if (idx < ctx->s_idx)
8515 goto cont;
8516 if (ctx->table && strcmp(ctx->table, table->name))
8517 goto cont;
8518 if (ctx->type != NFT_OBJECT_UNSPEC &&
8519 obj->ops->type->type != ctx->type)
8520 goto cont;
8521
8522 rc = nf_tables_fill_obj_info(skb, net,
8523 NETLINK_CB(cb->skb).portid,
8524 cb->nlh->nlmsg_seq,
8525 NFT_MSG_NEWOBJ,
8526 NLM_F_MULTI | NLM_F_APPEND,
8527 table->family, table,
8528 obj, ctx->reset);
8529 if (rc < 0)
8530 break;
8531
8532 entries++;
8533 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
8534 cont:
8535 idx++;
8536 }
8537 if (ctx->reset && entries)
8538 audit_log_obj_reset(table, nft_base_seq(net), entries);
8539 if (rc < 0)
8540 break;
8541 }
8542 rcu_read_unlock();
8543
8544 ctx->s_idx = idx;
8545 return skb->len;
8546 }
8547
nf_tables_dump_obj_start(struct netlink_callback * cb)8548 static int nf_tables_dump_obj_start(struct netlink_callback *cb)
8549 {
8550 struct nft_obj_dump_ctx *ctx = (void *)cb->ctx;
8551 const struct nlattr * const *nla = cb->data;
8552
8553 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
8554
8555 if (nla[NFTA_OBJ_TABLE]) {
8556 ctx->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC);
8557 if (!ctx->table)
8558 return -ENOMEM;
8559 }
8560
8561 if (nla[NFTA_OBJ_TYPE])
8562 ctx->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
8563
8564 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
8565 ctx->reset = true;
8566
8567 return 0;
8568 }
8569
nf_tables_dump_obj_done(struct netlink_callback * cb)8570 static int nf_tables_dump_obj_done(struct netlink_callback *cb)
8571 {
8572 struct nft_obj_dump_ctx *ctx = (void *)cb->ctx;
8573
8574 kfree(ctx->table);
8575
8576 return 0;
8577 }
8578
8579 /* Caller must hold rcu read lock or transaction mutex */
8580 static struct sk_buff *
nf_tables_getobj_single(u32 portid,const struct nfnl_info * info,const struct nlattr * const nla[],bool reset)8581 nf_tables_getobj_single(u32 portid, const struct nfnl_info *info,
8582 const struct nlattr * const nla[], bool reset)
8583 {
8584 struct netlink_ext_ack *extack = info->extack;
8585 u8 genmask = nft_genmask_cur(info->net);
8586 u8 family = info->nfmsg->nfgen_family;
8587 struct net *net = info->net;
8588 struct nft_table *table;
8589 struct nft_object *obj;
8590 struct sk_buff *skb2;
8591 u32 objtype;
8592 int err;
8593
8594 if (!nla[NFTA_OBJ_NAME] ||
8595 !nla[NFTA_OBJ_TYPE])
8596 return ERR_PTR(-EINVAL);
8597
8598 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, 0);
8599 if (IS_ERR(table)) {
8600 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
8601 return ERR_CAST(table);
8602 }
8603
8604 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
8605 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
8606 if (IS_ERR(obj)) {
8607 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
8608 return ERR_CAST(obj);
8609 }
8610
8611 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
8612 if (!skb2)
8613 return ERR_PTR(-ENOMEM);
8614
8615 err = nf_tables_fill_obj_info(skb2, net, portid,
8616 info->nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0,
8617 family, table, obj, reset);
8618 if (err < 0) {
8619 kfree_skb(skb2);
8620 return ERR_PTR(err);
8621 }
8622
8623 return skb2;
8624 }
8625
nf_tables_getobj(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8626 static int nf_tables_getobj(struct sk_buff *skb, const struct nfnl_info *info,
8627 const struct nlattr * const nla[])
8628 {
8629 u32 portid = NETLINK_CB(skb).portid;
8630 struct net *net = info->net;
8631 struct sk_buff *skb2;
8632 bool reset = false;
8633 char *buf;
8634
8635 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
8636 struct netlink_dump_control c = {
8637 .start = nf_tables_dump_obj_start,
8638 .dump = nf_tables_dump_obj,
8639 .done = nf_tables_dump_obj_done,
8640 .module = THIS_MODULE,
8641 .data = (void *)nla,
8642 };
8643
8644 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
8645 }
8646
8647 if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
8648 reset = true;
8649
8650 skb2 = nf_tables_getobj_single(portid, info, nla, reset);
8651 if (IS_ERR(skb2))
8652 return PTR_ERR(skb2);
8653
8654 if (!reset)
8655 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
8656
8657 buf = kasprintf(GFP_ATOMIC, "%.*s:%u",
8658 nla_len(nla[NFTA_OBJ_TABLE]),
8659 (char *)nla_data(nla[NFTA_OBJ_TABLE]),
8660 nft_base_seq(net));
8661 audit_log_nfcfg(buf, info->nfmsg->nfgen_family, 1,
8662 AUDIT_NFT_OP_OBJ_RESET, GFP_ATOMIC);
8663 kfree(buf);
8664
8665 return nfnetlink_unicast(skb2, net, portid);
8666 }
8667
nft_obj_destroy(const struct nft_ctx * ctx,struct nft_object * obj)8668 static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj)
8669 {
8670 if (obj->ops->destroy)
8671 obj->ops->destroy(ctx, obj);
8672
8673 module_put(obj->ops->type->owner);
8674 kfree(obj->key.name);
8675 kfree(obj->udata);
8676 kfree(obj);
8677 }
8678
nf_tables_delobj(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8679 static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info,
8680 const struct nlattr * const nla[])
8681 {
8682 struct netlink_ext_ack *extack = info->extack;
8683 u8 genmask = nft_genmask_next(info->net);
8684 u8 family = info->nfmsg->nfgen_family;
8685 struct net *net = info->net;
8686 const struct nlattr *attr;
8687 struct nft_table *table;
8688 struct nft_object *obj;
8689 struct nft_ctx ctx;
8690 u32 objtype;
8691
8692 if (!nla[NFTA_OBJ_TYPE] ||
8693 (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE]))
8694 return -EINVAL;
8695
8696 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask,
8697 NETLINK_CB(skb).portid);
8698 if (IS_ERR(table)) {
8699 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
8700 return PTR_ERR(table);
8701 }
8702
8703 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
8704 if (nla[NFTA_OBJ_HANDLE]) {
8705 attr = nla[NFTA_OBJ_HANDLE];
8706 obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask);
8707 } else {
8708 attr = nla[NFTA_OBJ_NAME];
8709 obj = nft_obj_lookup(net, table, attr, objtype, genmask);
8710 }
8711
8712 if (IS_ERR(obj)) {
8713 if (PTR_ERR(obj) == -ENOENT &&
8714 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYOBJ)
8715 return 0;
8716
8717 NL_SET_BAD_ATTR(extack, attr);
8718 return PTR_ERR(obj);
8719 }
8720 if (obj->use > 0) {
8721 NL_SET_BAD_ATTR(extack, attr);
8722 return -EBUSY;
8723 }
8724
8725 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
8726
8727 return nft_delobj(&ctx, obj);
8728 }
8729
8730 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)8731 nft_obj_notify_alloc(struct net *net, const struct nft_table *table,
8732 struct nft_object *obj, u32 portid, u32 seq, int event,
8733 u16 flags, int family, int report, gfp_t gfp)
8734 {
8735 struct sk_buff *skb;
8736 int err;
8737
8738 if (!report &&
8739 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
8740 return NULL;
8741
8742 skb = nlmsg_new(NLMSG_GOODSIZE, gfp);
8743 if (skb == NULL)
8744 goto err;
8745
8746 err = nf_tables_fill_obj_info(skb, net, portid, seq, event,
8747 flags & (NLM_F_CREATE | NLM_F_EXCL),
8748 family, table, obj, false);
8749 if (err < 0) {
8750 kfree_skb(skb);
8751 goto err;
8752 }
8753
8754 return skb;
8755 err:
8756 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
8757 return NULL;
8758 }
8759
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)8760 void nft_obj_notify(struct net *net, const struct nft_table *table,
8761 struct nft_object *obj, u32 portid, u32 seq, int event,
8762 u16 flags, int family, int report, gfp_t gfp)
8763 {
8764 char *buf = kasprintf(gfp, "%s:%u",
8765 table->name, nft_base_seq(net));
8766 struct sk_buff *skb;
8767
8768 audit_log_nfcfg(buf,
8769 family,
8770 obj->handle,
8771 event == NFT_MSG_NEWOBJ ?
8772 AUDIT_NFT_OP_OBJ_REGISTER :
8773 AUDIT_NFT_OP_OBJ_UNREGISTER,
8774 gfp);
8775 kfree(buf);
8776
8777 /* Called from the packet path, holding no mutex: notify_list is
8778 * serialised by commit_mutex, so send this notification directly.
8779 */
8780 skb = nft_obj_notify_alloc(net, table, obj, portid, seq, event,
8781 flags, family, report, gfp);
8782 if (skb)
8783 nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report, gfp);
8784 }
8785 EXPORT_SYMBOL_GPL(nft_obj_notify);
8786
nf_tables_obj_notify(const struct nft_ctx * ctx,struct nft_object * obj,int event)8787 static void nf_tables_obj_notify(const struct nft_ctx *ctx,
8788 struct nft_object *obj, int event)
8789 {
8790 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
8791 struct sk_buff *skb;
8792
8793 skb = nft_obj_notify_alloc(ctx->net, ctx->table, obj, ctx->portid,
8794 ctx->seq, event, ctx->flags, ctx->family,
8795 ctx->report, GFP_KERNEL);
8796 if (skb)
8797 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
8798 }
8799
8800 /*
8801 * Flow tables
8802 */
nft_register_flowtable_type(struct nf_flowtable_type * type)8803 void nft_register_flowtable_type(struct nf_flowtable_type *type)
8804 {
8805 nfnl_lock(NFNL_SUBSYS_NFTABLES);
8806 list_add_tail_rcu(&type->list, &nf_tables_flowtables);
8807 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
8808 }
8809 EXPORT_SYMBOL_GPL(nft_register_flowtable_type);
8810
nft_unregister_flowtable_type(struct nf_flowtable_type * type)8811 void nft_unregister_flowtable_type(struct nf_flowtable_type *type)
8812 {
8813 nfnl_lock(NFNL_SUBSYS_NFTABLES);
8814 list_del_rcu(&type->list);
8815 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
8816 }
8817 EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type);
8818
8819 static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = {
8820 [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING,
8821 .len = NFT_NAME_MAXLEN - 1 },
8822 [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING,
8823 .len = NFT_NAME_MAXLEN - 1 },
8824 [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED },
8825 [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 },
8826 [NFTA_FLOWTABLE_FLAGS] = NLA_POLICY_MASK(NLA_BE32, NFT_FLOWTABLE_MASK),
8827 };
8828
nft_flowtable_lookup(const struct net * net,const struct nft_table * table,const struct nlattr * nla,u8 genmask)8829 struct nft_flowtable *nft_flowtable_lookup(const struct net *net,
8830 const struct nft_table *table,
8831 const struct nlattr *nla, u8 genmask)
8832 {
8833 struct nft_flowtable *flowtable;
8834
8835 list_for_each_entry_rcu(flowtable, &table->flowtables, list,
8836 lockdep_commit_lock_is_held(net)) {
8837 if (!nla_strcmp(nla, flowtable->name) &&
8838 nft_active_genmask(flowtable, genmask))
8839 return flowtable;
8840 }
8841 return ERR_PTR(-ENOENT);
8842 }
8843 EXPORT_SYMBOL_GPL(nft_flowtable_lookup);
8844
nf_tables_deactivate_flowtable(const struct nft_ctx * ctx,struct nft_flowtable * flowtable,enum nft_trans_phase phase)8845 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
8846 struct nft_flowtable *flowtable,
8847 enum nft_trans_phase phase)
8848 {
8849 switch (phase) {
8850 case NFT_TRANS_PREPARE_ERROR:
8851 case NFT_TRANS_PREPARE:
8852 case NFT_TRANS_ABORT:
8853 case NFT_TRANS_RELEASE:
8854 nft_use_dec(&flowtable->use);
8855 fallthrough;
8856 default:
8857 return;
8858 }
8859 }
8860 EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable);
8861
8862 static struct nft_flowtable *
nft_flowtable_lookup_byhandle(const struct nft_table * table,const struct nlattr * nla,u8 genmask)8863 nft_flowtable_lookup_byhandle(const struct nft_table *table,
8864 const struct nlattr *nla, u8 genmask)
8865 {
8866 struct nft_flowtable *flowtable;
8867
8868 list_for_each_entry(flowtable, &table->flowtables, list) {
8869 if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle &&
8870 nft_active_genmask(flowtable, genmask))
8871 return flowtable;
8872 }
8873 return ERR_PTR(-ENOENT);
8874 }
8875
8876 struct nft_flowtable_hook {
8877 u32 num;
8878 int priority;
8879 struct list_head list;
8880 };
8881
8882 static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = {
8883 [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 },
8884 [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 },
8885 [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED },
8886 };
8887
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)8888 static int nft_flowtable_parse_hook(const struct nft_ctx *ctx,
8889 const struct nlattr * const nla[],
8890 struct nft_flowtable_hook *flowtable_hook,
8891 struct nft_flowtable *flowtable,
8892 struct netlink_ext_ack *extack, bool add)
8893 {
8894 struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1];
8895 struct nf_hook_ops *ops;
8896 struct nft_hook *hook;
8897 int hooknum, priority;
8898 int err;
8899
8900 INIT_LIST_HEAD(&flowtable_hook->list);
8901
8902 err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX,
8903 nla[NFTA_FLOWTABLE_HOOK],
8904 nft_flowtable_hook_policy, NULL);
8905 if (err < 0)
8906 return err;
8907
8908 if (add) {
8909 if (!tb[NFTA_FLOWTABLE_HOOK_NUM] ||
8910 !tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) {
8911 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
8912 return -ENOENT;
8913 }
8914
8915 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
8916 if (hooknum != NF_NETDEV_INGRESS)
8917 return -EOPNOTSUPP;
8918
8919 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
8920
8921 flowtable_hook->priority = priority;
8922 flowtable_hook->num = hooknum;
8923 } else {
8924 if (tb[NFTA_FLOWTABLE_HOOK_NUM]) {
8925 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
8926 if (hooknum != flowtable->hooknum)
8927 return -EOPNOTSUPP;
8928 }
8929
8930 if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) {
8931 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
8932 if (priority != flowtable->data.priority)
8933 return -EOPNOTSUPP;
8934 }
8935
8936 flowtable_hook->priority = flowtable->data.priority;
8937 flowtable_hook->num = flowtable->hooknum;
8938 }
8939
8940 if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) {
8941 err = nf_tables_parse_netdev_hooks(ctx->net,
8942 tb[NFTA_FLOWTABLE_HOOK_DEVS],
8943 &flowtable_hook->list,
8944 extack);
8945 if (err < 0)
8946 return err;
8947 }
8948
8949 list_for_each_entry(hook, &flowtable_hook->list, list) {
8950 list_for_each_entry(ops, &hook->ops_list, list) {
8951 ops->pf = NFPROTO_NETDEV;
8952 ops->hooknum = flowtable_hook->num;
8953 ops->priority = flowtable_hook->priority;
8954 ops->priv = &flowtable->data;
8955 ops->hook = flowtable->data.type->hook;
8956 ops->hook_ops_type = NF_HOOK_OP_NFT_FT;
8957 }
8958 }
8959
8960 return err;
8961 }
8962
8963 /* call under rcu_read_lock */
__nft_flowtable_type_get(u8 family)8964 static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family)
8965 {
8966 const struct nf_flowtable_type *type;
8967
8968 list_for_each_entry_rcu(type, &nf_tables_flowtables, list) {
8969 if (family == type->family)
8970 return type;
8971 }
8972 return NULL;
8973 }
8974
8975 static const struct nf_flowtable_type *
nft_flowtable_type_get(struct net * net,u8 family)8976 nft_flowtable_type_get(struct net *net, u8 family)
8977 {
8978 const struct nf_flowtable_type *type;
8979
8980 rcu_read_lock();
8981 type = __nft_flowtable_type_get(family);
8982 if (type != NULL && try_module_get(type->owner)) {
8983 rcu_read_unlock();
8984 return type;
8985 }
8986 rcu_read_unlock();
8987
8988 lockdep_nfnl_nft_mutex_not_held();
8989 #ifdef CONFIG_MODULES
8990 if (type == NULL) {
8991 if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN)
8992 return ERR_PTR(-EAGAIN);
8993 }
8994 #endif
8995 return ERR_PTR(-ENOENT);
8996 }
8997
8998 /* Only called from error and netdev event paths. */
nft_unregister_flowtable_ops(struct net * net,struct nft_flowtable * flowtable,struct nf_hook_ops * ops)8999 static void nft_unregister_flowtable_ops(struct net *net,
9000 struct nft_flowtable *flowtable,
9001 struct nf_hook_ops *ops)
9002 {
9003 nf_unregister_net_hook(net, ops);
9004 flowtable->data.type->setup(&flowtable->data, ops->dev,
9005 FLOW_BLOCK_UNBIND);
9006 }
9007
__nft_unregister_flowtable_net_hooks(struct net * net,struct nft_flowtable * flowtable,struct list_head * hook_list,bool release_netdev)9008 static void __nft_unregister_flowtable_net_hooks(struct net *net,
9009 struct nft_flowtable *flowtable,
9010 struct list_head *hook_list,
9011 bool release_netdev)
9012 {
9013 struct nft_hook *hook, *next;
9014 struct nf_hook_ops *ops;
9015
9016 list_for_each_entry_safe(hook, next, hook_list, list) {
9017 list_for_each_entry(ops, &hook->ops_list, list)
9018 nft_unregister_flowtable_ops(net, flowtable, ops);
9019 if (release_netdev)
9020 nft_netdev_hook_unlink_free_rcu(hook);
9021 }
9022 }
9023
nft_unregister_flowtable_net_hooks(struct net * net,struct nft_flowtable * flowtable,struct list_head * hook_list)9024 static void nft_unregister_flowtable_net_hooks(struct net *net,
9025 struct nft_flowtable *flowtable,
9026 struct list_head *hook_list)
9027 {
9028 __nft_unregister_flowtable_net_hooks(net, flowtable, hook_list, false);
9029 }
9030
nft_register_flowtable_ops(struct net * net,struct nft_flowtable * flowtable,struct nf_hook_ops * ops)9031 static int nft_register_flowtable_ops(struct net *net,
9032 struct nft_flowtable *flowtable,
9033 struct nf_hook_ops *ops)
9034 {
9035 int err;
9036
9037 err = flowtable->data.type->setup(&flowtable->data,
9038 ops->dev, FLOW_BLOCK_BIND);
9039 if (err < 0)
9040 return err;
9041
9042 err = nf_register_net_hook(net, ops);
9043 if (!err)
9044 return 0;
9045
9046 flowtable->data.type->setup(&flowtable->data,
9047 ops->dev, FLOW_BLOCK_UNBIND);
9048 return err;
9049 }
9050
nft_register_flowtable_net_hooks(struct net * net,struct nft_table * table,struct list_head * hook_list,struct nft_flowtable * flowtable)9051 static int nft_register_flowtable_net_hooks(struct net *net,
9052 struct nft_table *table,
9053 struct list_head *hook_list,
9054 struct nft_flowtable *flowtable)
9055 {
9056 struct nft_hook *hook, *next;
9057 struct nft_flowtable *ft;
9058 struct nf_hook_ops *ops;
9059 int err, i = 0;
9060
9061 list_for_each_entry(hook, hook_list, list) {
9062 list_for_each_entry(ft, &table->flowtables, list) {
9063 if (!nft_is_active_next(net, ft))
9064 continue;
9065
9066 if (nft_hook_list_find(&ft->hook_list, hook, false)) {
9067 err = -EEXIST;
9068 goto err_unregister_net_hooks;
9069 }
9070 }
9071
9072 list_for_each_entry(ops, &hook->ops_list, list) {
9073 err = nft_register_flowtable_ops(net, flowtable, ops);
9074 if (err < 0)
9075 goto err_unregister_net_hooks;
9076
9077 i++;
9078 }
9079 }
9080
9081 return 0;
9082
9083 err_unregister_net_hooks:
9084 list_for_each_entry_safe(hook, next, hook_list, list) {
9085 list_for_each_entry(ops, &hook->ops_list, list) {
9086 if (i-- <= 0)
9087 break;
9088
9089 nft_unregister_flowtable_ops(net, flowtable, ops);
9090 }
9091 nft_netdev_hook_unlink_free_rcu(hook);
9092 }
9093
9094 return err;
9095 }
9096
nft_hooks_destroy(struct list_head * hook_list)9097 static void nft_hooks_destroy(struct list_head *hook_list)
9098 {
9099 struct nft_hook *hook, *next;
9100
9101 list_for_each_entry_safe(hook, next, hook_list, list)
9102 nft_netdev_hook_unlink_free_rcu(hook);
9103 }
9104
nft_flowtable_unregister_trans_hook(struct net * net,struct nft_flowtable * flowtable,struct list_head * hook_list)9105 static void nft_flowtable_unregister_trans_hook(struct net *net,
9106 struct nft_flowtable *flowtable,
9107 struct list_head *hook_list)
9108 {
9109 struct nft_trans_hook *trans_hook, *next;
9110 struct nf_hook_ops *ops;
9111 struct nft_hook *hook;
9112
9113 list_for_each_entry_safe(trans_hook, next, hook_list, list) {
9114 hook = trans_hook->hook;
9115 list_for_each_entry(ops, &hook->ops_list, list)
9116 nft_unregister_flowtable_ops(net, flowtable, ops);
9117
9118 nft_netdev_hook_unlink_free_rcu(hook);
9119 nft_trans_hook_destroy(trans_hook);
9120 }
9121 }
9122
nft_flowtable_update(struct nft_ctx * ctx,const struct nlmsghdr * nlh,struct nft_flowtable * flowtable,struct netlink_ext_ack * extack)9123 static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh,
9124 struct nft_flowtable *flowtable,
9125 struct netlink_ext_ack *extack)
9126 {
9127 const struct nlattr * const *nla = ctx->nla;
9128 struct nft_flowtable_hook flowtable_hook;
9129 struct nftables_pernet *nft_net;
9130 struct nft_hook *hook, *next;
9131 struct nf_hook_ops *ops;
9132 struct nft_trans *trans;
9133 bool unregister = false;
9134 u32 flags;
9135 int err;
9136
9137 err = nft_flowtable_parse_hook(ctx, nla, &flowtable_hook, flowtable,
9138 extack, false);
9139 if (err < 0)
9140 return err;
9141
9142 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
9143 if (nft_hook_list_find(&flowtable->hook_list, hook, false)) {
9144 list_del(&hook->list);
9145 nft_netdev_hook_free(hook);
9146 continue;
9147 }
9148
9149 nft_net = nft_pernet(ctx->net);
9150 list_for_each_entry(trans, &nft_net->commit_list, list) {
9151 if (trans->msg_type != NFT_MSG_NEWFLOWTABLE ||
9152 trans->table != ctx->table ||
9153 !nft_trans_flowtable_update(trans))
9154 continue;
9155
9156 if (nft_hook_list_find(&nft_trans_flowtable_hooks(trans), hook, false)) {
9157 err = -EEXIST;
9158 goto err_flowtable_update_hook;
9159 }
9160 }
9161 }
9162
9163 if (nla[NFTA_FLOWTABLE_FLAGS]) {
9164 flags = ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
9165 if (flags & ~NFT_FLOWTABLE_MASK) {
9166 err = -EOPNOTSUPP;
9167 goto err_flowtable_update_hook;
9168 }
9169 if ((flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD) ^
9170 (flags & NFT_FLOWTABLE_HW_OFFLOAD)) {
9171 err = -EOPNOTSUPP;
9172 goto err_flowtable_update_hook;
9173 }
9174 } else {
9175 flags = flowtable->data.flags;
9176 }
9177
9178 err = nft_register_flowtable_net_hooks(ctx->net, ctx->table,
9179 &flowtable_hook.list, flowtable);
9180 if (err < 0)
9181 goto err_flowtable_update_hook;
9182
9183 trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE,
9184 sizeof(struct nft_trans_flowtable));
9185 if (!trans) {
9186 unregister = true;
9187 err = -ENOMEM;
9188 goto err_flowtable_update_hook;
9189 }
9190
9191 nft_trans_flowtable_flags(trans) = flags;
9192 nft_trans_flowtable(trans) = flowtable;
9193 nft_trans_flowtable_update(trans) = true;
9194 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
9195 list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans));
9196
9197 nft_trans_commit_list_add_tail(ctx->net, trans);
9198
9199 return 0;
9200
9201 err_flowtable_update_hook:
9202 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
9203 if (unregister) {
9204 list_for_each_entry(ops, &hook->ops_list, list)
9205 nft_unregister_flowtable_ops(ctx->net,
9206 flowtable, ops);
9207 }
9208 nft_netdev_hook_unlink_free_rcu(hook);
9209 }
9210
9211 return err;
9212
9213 }
9214
nf_tables_newflowtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])9215 static int nf_tables_newflowtable(struct sk_buff *skb,
9216 const struct nfnl_info *info,
9217 const struct nlattr * const nla[])
9218 {
9219 struct netlink_ext_ack *extack = info->extack;
9220 struct nft_flowtable_hook flowtable_hook;
9221 u8 genmask = nft_genmask_next(info->net);
9222 u8 family = info->nfmsg->nfgen_family;
9223 const struct nf_flowtable_type *type;
9224 struct nft_flowtable *flowtable;
9225 struct net *net = info->net;
9226 struct nft_table *table;
9227 struct nft_trans *trans;
9228 struct nft_ctx ctx;
9229 int err;
9230
9231 if (!nla[NFTA_FLOWTABLE_TABLE] ||
9232 !nla[NFTA_FLOWTABLE_NAME] ||
9233 !nla[NFTA_FLOWTABLE_HOOK])
9234 return -EINVAL;
9235
9236 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
9237 genmask, NETLINK_CB(skb).portid);
9238 if (IS_ERR(table)) {
9239 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
9240 return PTR_ERR(table);
9241 }
9242
9243 flowtable = nft_flowtable_lookup(net, table, nla[NFTA_FLOWTABLE_NAME],
9244 genmask);
9245 if (IS_ERR(flowtable)) {
9246 err = PTR_ERR(flowtable);
9247 if (err != -ENOENT) {
9248 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
9249 return err;
9250 }
9251 } else {
9252 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
9253 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
9254 return -EEXIST;
9255 }
9256
9257 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
9258
9259 return nft_flowtable_update(&ctx, info->nlh, flowtable, extack);
9260 }
9261
9262 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
9263
9264 if (!nft_use_inc(&table->use))
9265 return -EMFILE;
9266
9267 flowtable = kzalloc_obj(*flowtable, GFP_KERNEL_ACCOUNT);
9268 if (!flowtable) {
9269 err = -ENOMEM;
9270 goto flowtable_alloc;
9271 }
9272
9273 flowtable->table = table;
9274 flowtable->handle = nf_tables_alloc_handle(table);
9275 INIT_LIST_HEAD(&flowtable->hook_list);
9276
9277 flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL_ACCOUNT);
9278 if (!flowtable->name) {
9279 err = -ENOMEM;
9280 goto err1;
9281 }
9282
9283 type = nft_flowtable_type_get(net, family);
9284 if (IS_ERR(type)) {
9285 err = PTR_ERR(type);
9286 goto err2;
9287 }
9288
9289 if (nla[NFTA_FLOWTABLE_FLAGS]) {
9290 flowtable->data.flags =
9291 ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
9292 if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) {
9293 err = -EOPNOTSUPP;
9294 goto err3;
9295 }
9296 }
9297
9298 write_pnet(&flowtable->data.net, net);
9299 flowtable->data.type = type;
9300 err = type->init(&flowtable->data);
9301 if (err < 0)
9302 goto err3;
9303
9304 err = nft_flowtable_parse_hook(&ctx, nla, &flowtable_hook, flowtable,
9305 extack, true);
9306 if (err < 0)
9307 goto err_flowtable_parse_hooks;
9308
9309 list_splice(&flowtable_hook.list, &flowtable->hook_list);
9310 flowtable->data.priority = flowtable_hook.priority;
9311 flowtable->hooknum = flowtable_hook.num;
9312
9313 trans = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable);
9314 if (IS_ERR(trans)) {
9315 err = PTR_ERR(trans);
9316 goto err_flowtable_trans;
9317 }
9318
9319 /* This must be LAST to ensure no packets are walking over this flowtable. */
9320 err = nft_register_flowtable_net_hooks(ctx.net, table,
9321 &flowtable->hook_list,
9322 flowtable);
9323 if (err < 0)
9324 goto err_flowtable_hooks;
9325
9326 list_add_tail_rcu(&flowtable->list, &table->flowtables);
9327
9328 return 0;
9329
9330 err_flowtable_hooks:
9331 synchronize_rcu();
9332 nft_trans_destroy(trans);
9333 err_flowtable_trans:
9334 nft_hooks_destroy(&flowtable->hook_list);
9335 err_flowtable_parse_hooks:
9336 flowtable->data.type->free(&flowtable->data);
9337 err3:
9338 module_put(type->owner);
9339 err2:
9340 kfree(flowtable->name);
9341 err1:
9342 kfree(flowtable);
9343 flowtable_alloc:
9344 nft_use_dec_restore(&table->use);
9345
9346 return err;
9347 }
9348
nft_flowtable_hook_release(struct nft_flowtable_hook * flowtable_hook)9349 static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook)
9350 {
9351 struct nft_hook *this, *next;
9352
9353 list_for_each_entry_safe(this, next, &flowtable_hook->list, list) {
9354 list_del(&this->list);
9355 nft_netdev_hook_free(this);
9356 }
9357 }
9358
nft_delflowtable_hook(struct nft_ctx * ctx,struct nft_flowtable * flowtable,struct netlink_ext_ack * extack)9359 static int nft_delflowtable_hook(struct nft_ctx *ctx,
9360 struct nft_flowtable *flowtable,
9361 struct netlink_ext_ack *extack)
9362 {
9363 const struct nlattr * const *nla = ctx->nla;
9364 struct nft_flowtable_hook flowtable_hook;
9365 LIST_HEAD(flowtable_del_list);
9366 struct nft_hook *this, *hook;
9367 struct nft_trans *trans;
9368 int err;
9369
9370 err = nft_flowtable_parse_hook(ctx, nla, &flowtable_hook, flowtable,
9371 extack, false);
9372 if (err < 0)
9373 return err;
9374
9375 list_for_each_entry(this, &flowtable_hook.list, list) {
9376 hook = nft_hook_list_find(&flowtable->hook_list, this, true);
9377 if (!hook) {
9378 err = -ENOENT;
9379 goto err_flowtable_del_hook;
9380 }
9381 if (nft_trans_delhook(hook, &flowtable_del_list) < 0) {
9382 err = -ENOMEM;
9383 goto err_flowtable_del_hook;
9384 }
9385 }
9386
9387 trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE,
9388 sizeof(struct nft_trans_flowtable));
9389 if (!trans) {
9390 err = -ENOMEM;
9391 goto err_flowtable_del_hook;
9392 }
9393
9394 nft_trans_flowtable(trans) = flowtable;
9395 nft_trans_flowtable_update(trans) = true;
9396 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
9397 list_splice(&flowtable_del_list, &nft_trans_flowtable_hooks(trans));
9398 nft_flowtable_hook_release(&flowtable_hook);
9399
9400 nft_trans_commit_list_add_tail(ctx->net, trans);
9401
9402 return 0;
9403
9404 err_flowtable_del_hook:
9405 nft_trans_delhook_abort(&flowtable_del_list);
9406 nft_flowtable_hook_release(&flowtable_hook);
9407
9408 return err;
9409 }
9410
nf_tables_delflowtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])9411 static int nf_tables_delflowtable(struct sk_buff *skb,
9412 const struct nfnl_info *info,
9413 const struct nlattr * const nla[])
9414 {
9415 struct netlink_ext_ack *extack = info->extack;
9416 u8 genmask = nft_genmask_next(info->net);
9417 u8 family = info->nfmsg->nfgen_family;
9418 struct nft_flowtable *flowtable;
9419 struct net *net = info->net;
9420 const struct nlattr *attr;
9421 struct nft_table *table;
9422 struct nft_ctx ctx;
9423
9424 if (!nla[NFTA_FLOWTABLE_TABLE] ||
9425 (!nla[NFTA_FLOWTABLE_NAME] &&
9426 !nla[NFTA_FLOWTABLE_HANDLE]))
9427 return -EINVAL;
9428
9429 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
9430 genmask, NETLINK_CB(skb).portid);
9431 if (IS_ERR(table)) {
9432 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
9433 return PTR_ERR(table);
9434 }
9435
9436 if (nla[NFTA_FLOWTABLE_HANDLE]) {
9437 attr = nla[NFTA_FLOWTABLE_HANDLE];
9438 flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask);
9439 } else {
9440 attr = nla[NFTA_FLOWTABLE_NAME];
9441 flowtable = nft_flowtable_lookup(net, table, attr, genmask);
9442 }
9443
9444 if (IS_ERR(flowtable)) {
9445 if (PTR_ERR(flowtable) == -ENOENT &&
9446 NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYFLOWTABLE)
9447 return 0;
9448
9449 NL_SET_BAD_ATTR(extack, attr);
9450 return PTR_ERR(flowtable);
9451 }
9452
9453 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
9454
9455 if (nla[NFTA_FLOWTABLE_HOOK])
9456 return nft_delflowtable_hook(&ctx, flowtable, extack);
9457
9458 if (flowtable->use > 0) {
9459 NL_SET_BAD_ATTR(extack, attr);
9460 return -EBUSY;
9461 }
9462
9463 return nft_delflowtable(&ctx, flowtable);
9464 }
9465
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)9466 static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,
9467 u32 portid, u32 seq, int event,
9468 u32 flags, int family,
9469 struct nft_flowtable *flowtable,
9470 struct list_head *hook_list,
9471 struct list_head *trans_hook_list)
9472 {
9473 struct nft_trans_hook *trans_hook;
9474 struct nlattr *nest, *nest_devs;
9475 struct nft_hook *hook;
9476 struct nlmsghdr *nlh;
9477
9478 nlh = nfnl_msg_put(skb, portid, seq,
9479 nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event),
9480 flags, family, NFNETLINK_V0, nft_base_seq_be16(net));
9481 if (!nlh)
9482 goto nla_put_failure;
9483
9484 if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) ||
9485 nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) ||
9486 nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle),
9487 NFTA_FLOWTABLE_PAD))
9488 goto nla_put_failure;
9489
9490 if (!hook_list && !trans_hook_list &&
9491 (event == NFT_MSG_DELFLOWTABLE ||
9492 event == NFT_MSG_DESTROYFLOWTABLE)) {
9493 nlmsg_end(skb, nlh);
9494 return 0;
9495 }
9496
9497 if (nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) ||
9498 nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags)))
9499 goto nla_put_failure;
9500
9501 nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK);
9502 if (!nest)
9503 goto nla_put_failure;
9504 if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) ||
9505 nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority)))
9506 goto nla_put_failure;
9507
9508 nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS);
9509 if (!nest_devs)
9510 goto nla_put_failure;
9511
9512 if (!hook_list && !trans_hook_list)
9513 hook_list = &flowtable->hook_list;
9514
9515 if (hook_list) {
9516 list_for_each_entry_rcu(hook, hook_list, list,
9517 lockdep_commit_lock_is_held(net)) {
9518 if (nft_nla_put_hook_dev(skb, hook))
9519 goto nla_put_failure;
9520 }
9521 } else if (trans_hook_list) {
9522 list_for_each_entry(trans_hook, trans_hook_list, list) {
9523 if (nft_nla_put_hook_dev(skb, trans_hook->hook))
9524 goto nla_put_failure;
9525 }
9526 }
9527 nla_nest_end(skb, nest_devs);
9528 nla_nest_end(skb, nest);
9529
9530 nlmsg_end(skb, nlh);
9531 return 0;
9532
9533 nla_put_failure:
9534 nlmsg_trim(skb, nlh);
9535 return -1;
9536 }
9537
9538 struct nft_flowtable_filter {
9539 char *table;
9540 };
9541
nf_tables_dump_flowtable(struct sk_buff * skb,struct netlink_callback * cb)9542 static int nf_tables_dump_flowtable(struct sk_buff *skb,
9543 struct netlink_callback *cb)
9544 {
9545 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
9546 struct nft_flowtable_filter *filter = cb->data;
9547 unsigned int idx = 0, s_idx = cb->args[0];
9548 struct net *net = sock_net(skb->sk);
9549 int family = nfmsg->nfgen_family;
9550 struct nft_flowtable *flowtable;
9551 struct nftables_pernet *nft_net;
9552 const struct nft_table *table;
9553
9554 rcu_read_lock();
9555 nft_net = nft_pernet(net);
9556 cb->seq = nft_base_seq(net);
9557
9558 list_for_each_entry_rcu(table, &nft_net->tables, list) {
9559 if (family != NFPROTO_UNSPEC && family != table->family)
9560 continue;
9561
9562 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
9563 if (!nft_is_active(net, flowtable))
9564 goto cont;
9565 if (idx < s_idx)
9566 goto cont;
9567 if (idx > s_idx)
9568 memset(&cb->args[1], 0,
9569 sizeof(cb->args) - sizeof(cb->args[0]));
9570 if (filter && filter->table &&
9571 strcmp(filter->table, table->name))
9572 goto cont;
9573
9574 if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid,
9575 cb->nlh->nlmsg_seq,
9576 NFT_MSG_NEWFLOWTABLE,
9577 NLM_F_MULTI | NLM_F_APPEND,
9578 table->family,
9579 flowtable, NULL, NULL) < 0)
9580 goto done;
9581
9582 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
9583 cont:
9584 idx++;
9585 }
9586 }
9587 done:
9588 rcu_read_unlock();
9589
9590 cb->args[0] = idx;
9591 return skb->len;
9592 }
9593
nf_tables_dump_flowtable_start(struct netlink_callback * cb)9594 static int nf_tables_dump_flowtable_start(struct netlink_callback *cb)
9595 {
9596 const struct nlattr * const *nla = cb->data;
9597 struct nft_flowtable_filter *filter = NULL;
9598
9599 if (nla[NFTA_FLOWTABLE_TABLE]) {
9600 filter = kzalloc_obj(*filter, GFP_ATOMIC);
9601 if (!filter)
9602 return -ENOMEM;
9603
9604 filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE],
9605 GFP_ATOMIC);
9606 if (!filter->table) {
9607 kfree(filter);
9608 return -ENOMEM;
9609 }
9610 }
9611
9612 cb->data = filter;
9613 return 0;
9614 }
9615
nf_tables_dump_flowtable_done(struct netlink_callback * cb)9616 static int nf_tables_dump_flowtable_done(struct netlink_callback *cb)
9617 {
9618 struct nft_flowtable_filter *filter = cb->data;
9619
9620 if (!filter)
9621 return 0;
9622
9623 kfree(filter->table);
9624 kfree(filter);
9625
9626 return 0;
9627 }
9628
9629 /* called with rcu_read_lock held */
nf_tables_getflowtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])9630 static int nf_tables_getflowtable(struct sk_buff *skb,
9631 const struct nfnl_info *info,
9632 const struct nlattr * const nla[])
9633 {
9634 struct netlink_ext_ack *extack = info->extack;
9635 u8 genmask = nft_genmask_cur(info->net);
9636 u8 family = info->nfmsg->nfgen_family;
9637 struct nft_flowtable *flowtable;
9638 const struct nft_table *table;
9639 struct net *net = info->net;
9640 struct sk_buff *skb2;
9641 int err;
9642
9643 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
9644 struct netlink_dump_control c = {
9645 .start = nf_tables_dump_flowtable_start,
9646 .dump = nf_tables_dump_flowtable,
9647 .done = nf_tables_dump_flowtable_done,
9648 .module = THIS_MODULE,
9649 .data = (void *)nla,
9650 };
9651
9652 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
9653 }
9654
9655 if (!nla[NFTA_FLOWTABLE_NAME])
9656 return -EINVAL;
9657
9658 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
9659 genmask, 0);
9660 if (IS_ERR(table)) {
9661 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
9662 return PTR_ERR(table);
9663 }
9664
9665 flowtable = nft_flowtable_lookup(net, table, nla[NFTA_FLOWTABLE_NAME],
9666 genmask);
9667 if (IS_ERR(flowtable)) {
9668 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
9669 return PTR_ERR(flowtable);
9670 }
9671
9672 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
9673 if (!skb2)
9674 return -ENOMEM;
9675
9676 err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid,
9677 info->nlh->nlmsg_seq,
9678 NFT_MSG_NEWFLOWTABLE, 0, family,
9679 flowtable, NULL, NULL);
9680 if (err < 0)
9681 goto err_fill_flowtable_info;
9682
9683 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
9684
9685 err_fill_flowtable_info:
9686 kfree_skb(skb2);
9687 return err;
9688 }
9689
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)9690 static void nf_tables_flowtable_notify(struct nft_ctx *ctx,
9691 struct nft_flowtable *flowtable,
9692 struct list_head *hook_list,
9693 struct list_head *trans_hook_list,
9694 int event)
9695 {
9696 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
9697 struct sk_buff *skb;
9698 u16 flags = 0;
9699 int err;
9700
9701 if (!ctx->report &&
9702 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
9703 return;
9704
9705 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
9706 if (skb == NULL)
9707 goto err;
9708
9709 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
9710 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
9711
9712 err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid,
9713 ctx->seq, event, flags,
9714 ctx->family, flowtable,
9715 hook_list, trans_hook_list);
9716 if (err < 0) {
9717 kfree_skb(skb);
9718 goto err;
9719 }
9720
9721 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
9722 return;
9723 err:
9724 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
9725 }
9726
nf_tables_flowtable_destroy(struct nft_flowtable * flowtable)9727 static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
9728 {
9729 flowtable->data.type->free(&flowtable->data);
9730 nft_hooks_destroy(&flowtable->hook_list);
9731 kfree(flowtable->name);
9732 module_put(flowtable->data.type->owner);
9733 kfree(flowtable);
9734 }
9735
nf_tables_fill_gen_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq)9736 static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
9737 u32 portid, u32 seq)
9738 {
9739 struct nlmsghdr *nlh;
9740 char buf[TASK_COMM_LEN];
9741 int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN);
9742
9743 nlh = nfnl_msg_put(skb, portid, seq, event, 0, AF_UNSPEC,
9744 NFNETLINK_V0, nft_base_seq_be16(net));
9745 if (!nlh)
9746 goto nla_put_failure;
9747
9748 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(nft_base_seq(net))) ||
9749 nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) ||
9750 nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current)))
9751 goto nla_put_failure;
9752
9753 nlmsg_end(skb, nlh);
9754 return 0;
9755
9756 nla_put_failure:
9757 nlmsg_trim(skb, nlh);
9758 return -EMSGSIZE;
9759 }
9760
nft_hook_find_ops(const struct nft_hook * hook,const struct net_device * dev)9761 struct nf_hook_ops *nft_hook_find_ops(const struct nft_hook *hook,
9762 const struct net_device *dev)
9763 {
9764 struct nf_hook_ops *ops;
9765
9766 list_for_each_entry(ops, &hook->ops_list, list) {
9767 if (ops->dev == dev)
9768 return ops;
9769 }
9770 return NULL;
9771 }
9772 EXPORT_SYMBOL_GPL(nft_hook_find_ops);
9773
nft_hook_find_ops_rcu(const struct nft_hook * hook,const struct net_device * dev)9774 struct nf_hook_ops *nft_hook_find_ops_rcu(const struct nft_hook *hook,
9775 const struct net_device *dev)
9776 {
9777 struct nf_hook_ops *ops;
9778
9779 list_for_each_entry_rcu(ops, &hook->ops_list, list) {
9780 if (ops->dev == dev)
9781 return ops;
9782 }
9783 return NULL;
9784 }
9785 EXPORT_SYMBOL_GPL(nft_hook_find_ops_rcu);
9786
nft_flowtable_event(unsigned long event,struct net_device * dev,struct nft_flowtable * flowtable,bool changename)9787 static int nft_flowtable_event(unsigned long event, struct net_device *dev,
9788 struct nft_flowtable *flowtable, bool changename)
9789 {
9790 struct nf_hook_ops *ops;
9791 struct nft_hook *hook;
9792 bool match;
9793
9794 list_for_each_entry(hook, &flowtable->hook_list, list) {
9795 ops = nft_hook_find_ops(hook, dev);
9796 match = !strncmp(hook->ifname, dev->name, hook->ifnamelen);
9797
9798 switch (event) {
9799 case NETDEV_UNREGISTER:
9800 /* NOP if not found or new name still matching */
9801 if (!ops || (changename && match))
9802 continue;
9803
9804 /* flow_offload_netdev_event() cleans up entries for us. */
9805 nft_unregister_flowtable_ops(dev_net(dev),
9806 flowtable, ops);
9807 list_del_rcu(&ops->list);
9808 kfree_rcu(ops, rcu);
9809 break;
9810 case NETDEV_REGISTER:
9811 /* NOP if not matching or already registered */
9812 if (!match || ops)
9813 continue;
9814
9815 ops = kzalloc_obj(struct nf_hook_ops,
9816 GFP_KERNEL_ACCOUNT);
9817 if (!ops)
9818 return 1;
9819
9820 ops->pf = NFPROTO_NETDEV;
9821 ops->hooknum = flowtable->hooknum;
9822 ops->priority = flowtable->data.priority;
9823 ops->priv = &flowtable->data;
9824 ops->hook = flowtable->data.type->hook;
9825 ops->hook_ops_type = NF_HOOK_OP_NFT_FT;
9826 ops->dev = dev;
9827 if (nft_register_flowtable_ops(dev_net(dev),
9828 flowtable, ops)) {
9829 kfree(ops);
9830 return 1;
9831 }
9832 list_add_tail_rcu(&ops->list, &hook->ops_list);
9833 break;
9834 }
9835 break;
9836 }
9837 return 0;
9838 }
9839
__nf_tables_flowtable_event(unsigned long event,struct net_device * dev,bool changename)9840 static int __nf_tables_flowtable_event(unsigned long event,
9841 struct net_device *dev,
9842 bool changename)
9843 {
9844 struct nftables_pernet *nft_net = nft_pernet(dev_net(dev));
9845 struct nft_flowtable *flowtable;
9846 struct nft_table *table;
9847
9848 list_for_each_entry(table, &nft_net->tables, list) {
9849 list_for_each_entry(flowtable, &table->flowtables, list) {
9850 if (nft_flowtable_event(event, dev,
9851 flowtable, changename))
9852 return 1;
9853 }
9854 }
9855 return 0;
9856 }
9857
nf_tables_flowtable_event(struct notifier_block * this,unsigned long event,void * ptr)9858 static int nf_tables_flowtable_event(struct notifier_block *this,
9859 unsigned long event, void *ptr)
9860 {
9861 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
9862 struct nftables_pernet *nft_net;
9863 int ret = NOTIFY_DONE;
9864 struct net *net;
9865
9866 if (event != NETDEV_REGISTER &&
9867 event != NETDEV_UNREGISTER &&
9868 event != NETDEV_CHANGENAME)
9869 return NOTIFY_DONE;
9870
9871 net = dev_net(dev);
9872 nft_net = nft_pernet(net);
9873 mutex_lock(&nft_net->commit_mutex);
9874
9875 if (event == NETDEV_CHANGENAME) {
9876 if (__nf_tables_flowtable_event(NETDEV_REGISTER, dev, true)) {
9877 ret = NOTIFY_BAD;
9878 goto out_unlock;
9879 }
9880 __nf_tables_flowtable_event(NETDEV_UNREGISTER, dev, true);
9881 } else if (__nf_tables_flowtable_event(event, dev, false)) {
9882 ret = NOTIFY_BAD;
9883 }
9884 out_unlock:
9885 mutex_unlock(&nft_net->commit_mutex);
9886 return ret;
9887 }
9888
9889 static struct notifier_block nf_tables_flowtable_notifier = {
9890 .notifier_call = nf_tables_flowtable_event,
9891 };
9892
nf_tables_gen_notify(struct net * net,struct sk_buff * skb,int event)9893 static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb,
9894 int event)
9895 {
9896 struct nlmsghdr *nlh = nlmsg_hdr(skb);
9897 struct sk_buff *skb2;
9898 int err;
9899
9900 if (!nlmsg_report(nlh) &&
9901 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
9902 return;
9903
9904 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
9905 if (skb2 == NULL)
9906 goto err;
9907
9908 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
9909 nlh->nlmsg_seq);
9910 if (err < 0) {
9911 kfree_skb(skb2);
9912 goto err;
9913 }
9914
9915 nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
9916 nlmsg_report(nlh), GFP_KERNEL);
9917 return;
9918 err:
9919 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
9920 -ENOBUFS);
9921 }
9922
nf_tables_getgen(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])9923 static int nf_tables_getgen(struct sk_buff *skb, const struct nfnl_info *info,
9924 const struct nlattr * const nla[])
9925 {
9926 struct sk_buff *skb2;
9927 int err;
9928
9929 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
9930 if (skb2 == NULL)
9931 return -ENOMEM;
9932
9933 err = nf_tables_fill_gen_info(skb2, info->net, NETLINK_CB(skb).portid,
9934 info->nlh->nlmsg_seq);
9935 if (err < 0)
9936 goto err_fill_gen_info;
9937
9938 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
9939
9940 err_fill_gen_info:
9941 kfree_skb(skb2);
9942 return err;
9943 }
9944
9945 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
9946 [NFT_MSG_NEWTABLE] = {
9947 .call = nf_tables_newtable,
9948 .type = NFNL_CB_BATCH,
9949 .attr_count = NFTA_TABLE_MAX,
9950 .policy = nft_table_policy,
9951 },
9952 [NFT_MSG_GETTABLE] = {
9953 .call = nf_tables_gettable,
9954 .type = NFNL_CB_RCU,
9955 .attr_count = NFTA_TABLE_MAX,
9956 .policy = nft_table_policy,
9957 },
9958 [NFT_MSG_DELTABLE] = {
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_DESTROYTABLE] = {
9965 .call = nf_tables_deltable,
9966 .type = NFNL_CB_BATCH,
9967 .attr_count = NFTA_TABLE_MAX,
9968 .policy = nft_table_policy,
9969 },
9970 [NFT_MSG_NEWCHAIN] = {
9971 .call = nf_tables_newchain,
9972 .type = NFNL_CB_BATCH,
9973 .attr_count = NFTA_CHAIN_MAX,
9974 .policy = nft_chain_policy,
9975 },
9976 [NFT_MSG_GETCHAIN] = {
9977 .call = nf_tables_getchain,
9978 .type = NFNL_CB_RCU,
9979 .attr_count = NFTA_CHAIN_MAX,
9980 .policy = nft_chain_policy,
9981 },
9982 [NFT_MSG_DELCHAIN] = {
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_DESTROYCHAIN] = {
9989 .call = nf_tables_delchain,
9990 .type = NFNL_CB_BATCH,
9991 .attr_count = NFTA_CHAIN_MAX,
9992 .policy = nft_chain_policy,
9993 },
9994 [NFT_MSG_NEWRULE] = {
9995 .call = nf_tables_newrule,
9996 .type = NFNL_CB_BATCH,
9997 .attr_count = NFTA_RULE_MAX,
9998 .policy = nft_rule_policy,
9999 },
10000 [NFT_MSG_GETRULE] = {
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_GETRULE_RESET] = {
10007 .call = nf_tables_getrule,
10008 .type = NFNL_CB_RCU,
10009 .attr_count = NFTA_RULE_MAX,
10010 .policy = nft_rule_policy,
10011 },
10012 [NFT_MSG_DELRULE] = {
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_DESTROYRULE] = {
10019 .call = nf_tables_delrule,
10020 .type = NFNL_CB_BATCH,
10021 .attr_count = NFTA_RULE_MAX,
10022 .policy = nft_rule_policy,
10023 },
10024 [NFT_MSG_NEWSET] = {
10025 .call = nf_tables_newset,
10026 .type = NFNL_CB_BATCH,
10027 .attr_count = NFTA_SET_MAX,
10028 .policy = nft_set_policy,
10029 },
10030 [NFT_MSG_GETSET] = {
10031 .call = nf_tables_getset,
10032 .type = NFNL_CB_RCU,
10033 .attr_count = NFTA_SET_MAX,
10034 .policy = nft_set_policy,
10035 },
10036 [NFT_MSG_DELSET] = {
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_DESTROYSET] = {
10043 .call = nf_tables_delset,
10044 .type = NFNL_CB_BATCH,
10045 .attr_count = NFTA_SET_MAX,
10046 .policy = nft_set_policy,
10047 },
10048 [NFT_MSG_NEWSETELEM] = {
10049 .call = nf_tables_newsetelem,
10050 .type = NFNL_CB_BATCH,
10051 .attr_count = NFTA_SET_ELEM_LIST_MAX,
10052 .policy = nft_set_elem_list_policy,
10053 },
10054 [NFT_MSG_GETSETELEM] = {
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_GETSETELEM_RESET] = {
10061 .call = nf_tables_getsetelem,
10062 .type = NFNL_CB_RCU,
10063 .attr_count = NFTA_SET_ELEM_LIST_MAX,
10064 .policy = nft_set_elem_list_policy,
10065 },
10066 [NFT_MSG_DELSETELEM] = {
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_DESTROYSETELEM] = {
10073 .call = nf_tables_delsetelem,
10074 .type = NFNL_CB_BATCH,
10075 .attr_count = NFTA_SET_ELEM_LIST_MAX,
10076 .policy = nft_set_elem_list_policy,
10077 },
10078 [NFT_MSG_GETGEN] = {
10079 .call = nf_tables_getgen,
10080 .type = NFNL_CB_RCU,
10081 },
10082 [NFT_MSG_NEWOBJ] = {
10083 .call = nf_tables_newobj,
10084 .type = NFNL_CB_BATCH,
10085 .attr_count = NFTA_OBJ_MAX,
10086 .policy = nft_obj_policy,
10087 },
10088 [NFT_MSG_GETOBJ] = {
10089 .call = nf_tables_getobj,
10090 .type = NFNL_CB_RCU,
10091 .attr_count = NFTA_OBJ_MAX,
10092 .policy = nft_obj_policy,
10093 },
10094 [NFT_MSG_DELOBJ] = {
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_DESTROYOBJ] = {
10101 .call = nf_tables_delobj,
10102 .type = NFNL_CB_BATCH,
10103 .attr_count = NFTA_OBJ_MAX,
10104 .policy = nft_obj_policy,
10105 },
10106 [NFT_MSG_GETOBJ_RESET] = {
10107 .call = nf_tables_getobj,
10108 .type = NFNL_CB_RCU,
10109 .attr_count = NFTA_OBJ_MAX,
10110 .policy = nft_obj_policy,
10111 },
10112 [NFT_MSG_NEWFLOWTABLE] = {
10113 .call = nf_tables_newflowtable,
10114 .type = NFNL_CB_BATCH,
10115 .attr_count = NFTA_FLOWTABLE_MAX,
10116 .policy = nft_flowtable_policy,
10117 },
10118 [NFT_MSG_GETFLOWTABLE] = {
10119 .call = nf_tables_getflowtable,
10120 .type = NFNL_CB_RCU,
10121 .attr_count = NFTA_FLOWTABLE_MAX,
10122 .policy = nft_flowtable_policy,
10123 },
10124 [NFT_MSG_DELFLOWTABLE] = {
10125 .call = nf_tables_delflowtable,
10126 .type = NFNL_CB_BATCH,
10127 .attr_count = NFTA_FLOWTABLE_MAX,
10128 .policy = nft_flowtable_policy,
10129 },
10130 [NFT_MSG_DESTROYFLOWTABLE] = {
10131 .call = nf_tables_delflowtable,
10132 .type = NFNL_CB_BATCH,
10133 .attr_count = NFTA_FLOWTABLE_MAX,
10134 .policy = nft_flowtable_policy,
10135 },
10136 };
10137
nf_tables_validate(struct net * net)10138 static int nf_tables_validate(struct net *net)
10139 {
10140 struct nftables_pernet *nft_net = nft_pernet(net);
10141 struct nft_table *table;
10142
10143 list_for_each_entry(table, &nft_net->tables, list) {
10144 switch (table->validate_state) {
10145 case NFT_VALIDATE_SKIP:
10146 continue;
10147 case NFT_VALIDATE_NEED:
10148 nft_validate_state_update(table, NFT_VALIDATE_DO);
10149 fallthrough;
10150 case NFT_VALIDATE_DO:
10151 if (nft_table_validate(net, table) < 0)
10152 return -EAGAIN;
10153
10154 nft_validate_state_update(table, NFT_VALIDATE_SKIP);
10155 break;
10156 }
10157 }
10158
10159 return 0;
10160 }
10161
10162 /* a drop policy has to be deferred until all rules have been activated,
10163 * otherwise a large ruleset that contains a drop-policy base chain will
10164 * cause all packets to get dropped until the full transaction has been
10165 * processed.
10166 *
10167 * We defer the drop policy until the transaction has been finalized.
10168 */
nft_chain_commit_drop_policy(struct nft_trans_chain * trans)10169 static void nft_chain_commit_drop_policy(struct nft_trans_chain *trans)
10170 {
10171 struct nft_base_chain *basechain;
10172
10173 if (trans->policy != NF_DROP)
10174 return;
10175
10176 if (!nft_is_base_chain(trans->chain))
10177 return;
10178
10179 basechain = nft_base_chain(trans->chain);
10180 basechain->policy = NF_DROP;
10181 }
10182
nft_chain_commit_update(struct nft_trans_chain * trans)10183 static void nft_chain_commit_update(struct nft_trans_chain *trans)
10184 {
10185 struct nft_table *table = trans->nft_trans_binding.nft_trans.table;
10186 struct nft_base_chain *basechain;
10187
10188 if (trans->name) {
10189 rhltable_remove(&table->chains_ht,
10190 &trans->chain->rhlhead,
10191 nft_chain_ht_params);
10192 swap(trans->chain->name, trans->name);
10193 rhltable_insert_key(&table->chains_ht,
10194 trans->chain->name,
10195 &trans->chain->rhlhead,
10196 nft_chain_ht_params);
10197 }
10198
10199 if (!nft_is_base_chain(trans->chain))
10200 return;
10201
10202 nft_chain_stats_replace(trans);
10203
10204 basechain = nft_base_chain(trans->chain);
10205
10206 switch (trans->policy) {
10207 case NF_DROP:
10208 case NF_ACCEPT:
10209 basechain->policy = trans->policy;
10210 break;
10211 }
10212 }
10213
nft_obj_commit_update(const struct nft_ctx * ctx,struct nft_trans * trans)10214 static void nft_obj_commit_update(const struct nft_ctx *ctx,
10215 struct nft_trans *trans)
10216 {
10217 struct nft_object *newobj;
10218 struct nft_object *obj;
10219
10220 obj = nft_trans_obj(trans);
10221 newobj = nft_trans_obj_newobj(trans);
10222
10223 if (WARN_ON_ONCE(!obj->ops->update))
10224 return;
10225
10226 obj->ops->update(obj, newobj);
10227 nft_obj_destroy(ctx, newobj);
10228 }
10229
nft_commit_release(struct nft_trans * trans)10230 static void nft_commit_release(struct nft_trans *trans)
10231 {
10232 struct nft_ctx ctx = {
10233 .net = trans->net,
10234 };
10235
10236 nft_ctx_update(&ctx, trans);
10237
10238 switch (trans->msg_type) {
10239 case NFT_MSG_DELTABLE:
10240 case NFT_MSG_DESTROYTABLE:
10241 nf_tables_table_destroy(trans->table);
10242 break;
10243 case NFT_MSG_NEWCHAIN:
10244 free_percpu(nft_trans_chain_stats(trans));
10245 kfree(nft_trans_chain_name(trans));
10246 break;
10247 case NFT_MSG_DELCHAIN:
10248 case NFT_MSG_DESTROYCHAIN:
10249 if (!nft_trans_chain_update(trans))
10250 nf_tables_chain_destroy(nft_trans_chain(trans));
10251 break;
10252 case NFT_MSG_DELRULE:
10253 case NFT_MSG_DESTROYRULE:
10254 nf_tables_rule_destroy(&ctx, nft_trans_rule(trans));
10255 break;
10256 case NFT_MSG_DELSET:
10257 case NFT_MSG_DESTROYSET:
10258 nft_set_destroy(&ctx, nft_trans_set(trans));
10259 break;
10260 case NFT_MSG_DELSETELEM:
10261 case NFT_MSG_DESTROYSETELEM:
10262 nft_trans_elems_destroy(&ctx, nft_trans_container_elem(trans));
10263 break;
10264 case NFT_MSG_DELOBJ:
10265 case NFT_MSG_DESTROYOBJ:
10266 nft_obj_destroy(&ctx, nft_trans_obj(trans));
10267 break;
10268 case NFT_MSG_DELFLOWTABLE:
10269 case NFT_MSG_DESTROYFLOWTABLE:
10270 if (!nft_trans_flowtable_update(trans))
10271 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
10272 break;
10273 }
10274
10275 if (trans->put_net)
10276 put_net(trans->net);
10277
10278 kfree(trans);
10279 }
10280
nf_tables_trans_destroy_work(struct work_struct * w)10281 static void nf_tables_trans_destroy_work(struct work_struct *w)
10282 {
10283 struct nftables_pernet *nft_net = container_of(w, struct nftables_pernet, destroy_work);
10284 struct nft_trans *trans, *next;
10285 LIST_HEAD(head);
10286
10287 spin_lock(&nf_tables_destroy_list_lock);
10288 list_splice_init(&nft_net->destroy_list, &head);
10289 spin_unlock(&nf_tables_destroy_list_lock);
10290
10291 if (list_empty(&head))
10292 return;
10293
10294 synchronize_rcu();
10295
10296 list_for_each_entry_safe(trans, next, &head, list) {
10297 nft_trans_list_del(trans);
10298 nft_commit_release(trans);
10299 }
10300 }
10301
nf_tables_trans_destroy_flush_work(struct net * net)10302 void nf_tables_trans_destroy_flush_work(struct net *net)
10303 {
10304 struct nftables_pernet *nft_net = nft_pernet(net);
10305
10306 flush_work(&nft_net->destroy_work);
10307 }
10308 EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work);
10309
nf_tables_commit_chain_prepare(struct net * net,struct nft_chain * chain)10310 static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain)
10311 {
10312 const struct nft_expr *expr, *last;
10313 unsigned int size, data_size;
10314 void *data, *data_boundary;
10315 struct nft_rule_dp *prule;
10316 struct nft_rule *rule;
10317
10318 /* already handled or inactive chain? */
10319 if (chain->blob_next || !nft_is_active_next(net, chain))
10320 return 0;
10321
10322 data_size = 0;
10323 list_for_each_entry(rule, &chain->rules, list) {
10324 if (nft_is_active_next(net, rule)) {
10325 data_size += sizeof(*prule) + rule->dlen;
10326 if (data_size > INT_MAX)
10327 return -ENOMEM;
10328 }
10329 }
10330
10331 chain->blob_next = nf_tables_chain_alloc_rules(chain, data_size);
10332 if (!chain->blob_next)
10333 return -ENOMEM;
10334
10335 data = (void *)chain->blob_next->data;
10336 data_boundary = data + data_size;
10337 size = 0;
10338
10339 list_for_each_entry(rule, &chain->rules, list) {
10340 if (!nft_is_active_next(net, rule))
10341 continue;
10342
10343 prule = (struct nft_rule_dp *)data;
10344 data += offsetof(struct nft_rule_dp, data);
10345 if (unlikely(data > data_boundary)) {
10346 DEBUG_NET_WARN_ON_ONCE(1);
10347 return -ENOMEM;
10348 }
10349
10350 size = 0;
10351 nft_rule_for_each_expr(expr, last, rule) {
10352 if (unlikely(data + size + expr->ops->size > data_boundary)) {
10353 DEBUG_NET_WARN_ON_ONCE(1);
10354 return -ENOMEM;
10355 }
10356
10357 memcpy(data + size, expr, expr->ops->size);
10358 size += expr->ops->size;
10359 }
10360 if (unlikely(size >= 1 << 12)) {
10361 DEBUG_NET_WARN_ON_ONCE(1);
10362 return -ENOMEM;
10363 }
10364
10365 prule->handle = rule->handle;
10366 prule->dlen = size;
10367 prule->is_last = 0;
10368
10369 data += size;
10370 size = 0;
10371 chain->blob_next->size += (unsigned long)(data - (void *)prule);
10372 }
10373
10374 if (unlikely(data > data_boundary)) {
10375 DEBUG_NET_WARN_ON_ONCE(1);
10376 return -ENOMEM;
10377 }
10378
10379 prule = (struct nft_rule_dp *)data;
10380 nft_last_rule(chain, prule);
10381
10382 return 0;
10383 }
10384
nf_tables_commit_chain_prepare_cancel(struct net * net)10385 static void nf_tables_commit_chain_prepare_cancel(struct net *net)
10386 {
10387 struct nftables_pernet *nft_net = nft_pernet(net);
10388 struct nft_trans *trans, *next;
10389
10390 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
10391 if (trans->msg_type == NFT_MSG_NEWRULE ||
10392 trans->msg_type == NFT_MSG_DELRULE) {
10393 struct nft_chain *chain = nft_trans_rule_chain(trans);
10394
10395 kvfree(chain->blob_next);
10396 chain->blob_next = NULL;
10397 }
10398 }
10399 }
10400
__nf_tables_commit_chain_free_rules(struct rcu_head * h)10401 static void __nf_tables_commit_chain_free_rules(struct rcu_head *h)
10402 {
10403 struct nft_rule_dp_last *l = container_of(h, struct nft_rule_dp_last, h);
10404
10405 kvfree(l->blob);
10406 }
10407
nf_tables_commit_chain_free_rules_old(struct nft_rule_blob * blob)10408 static void nf_tables_commit_chain_free_rules_old(struct nft_rule_blob *blob)
10409 {
10410 struct nft_rule_dp_last *last;
10411
10412 /* last rule trailer is after end marker */
10413 last = (void *)blob + sizeof(*blob) + blob->size;
10414 last->blob = blob;
10415
10416 call_rcu(&last->h, __nf_tables_commit_chain_free_rules);
10417 }
10418
nf_tables_commit_chain(struct net * net,struct nft_chain * chain)10419 static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain)
10420 {
10421 struct nft_rule_blob *g0, *g1;
10422 bool next_genbit;
10423
10424 next_genbit = nft_gencursor_next(net);
10425
10426 g0 = rcu_dereference_protected(chain->blob_gen_0,
10427 lockdep_commit_lock_is_held(net));
10428 g1 = rcu_dereference_protected(chain->blob_gen_1,
10429 lockdep_commit_lock_is_held(net));
10430
10431 /* No changes to this chain? */
10432 if (chain->blob_next == NULL) {
10433 /* chain had no change in last or next generation */
10434 if (g0 == g1)
10435 return;
10436 /*
10437 * chain had no change in this generation; make sure next
10438 * one uses same rules as current generation.
10439 */
10440 if (next_genbit) {
10441 rcu_assign_pointer(chain->blob_gen_1, g0);
10442 nf_tables_commit_chain_free_rules_old(g1);
10443 } else {
10444 rcu_assign_pointer(chain->blob_gen_0, g1);
10445 nf_tables_commit_chain_free_rules_old(g0);
10446 }
10447
10448 return;
10449 }
10450
10451 if (next_genbit)
10452 rcu_assign_pointer(chain->blob_gen_1, chain->blob_next);
10453 else
10454 rcu_assign_pointer(chain->blob_gen_0, chain->blob_next);
10455
10456 chain->blob_next = NULL;
10457
10458 if (g0 == g1)
10459 return;
10460
10461 if (next_genbit)
10462 nf_tables_commit_chain_free_rules_old(g1);
10463 else
10464 nf_tables_commit_chain_free_rules_old(g0);
10465 }
10466
nft_obj_del(struct nft_table * table,struct nft_object * obj)10467 static void nft_obj_del(struct nft_table *table, struct nft_object *obj)
10468 {
10469 rhltable_remove(&table->objname_ht, &obj->rhlhead, nft_objname_ht_params);
10470 list_del_rcu(&obj->list);
10471 }
10472
nft_chain_del(struct nft_chain * chain)10473 void nft_chain_del(struct nft_chain *chain)
10474 {
10475 struct nft_table *table = chain->table;
10476
10477 WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead,
10478 nft_chain_ht_params));
10479 list_del_rcu(&chain->list);
10480 }
10481
nft_trans_gc_setelem_remove(struct nft_ctx * ctx,struct nft_trans_gc * trans)10482 static void nft_trans_gc_setelem_remove(struct nft_ctx *ctx,
10483 struct nft_trans_gc *trans)
10484 {
10485 struct nft_elem_priv **priv = trans->priv;
10486 unsigned int i;
10487
10488 for (i = 0; i < trans->count; i++) {
10489 nft_setelem_data_deactivate(ctx->net, trans->set, priv[i]);
10490 nft_setelem_remove(ctx->net, trans->set, priv[i]);
10491 }
10492 }
10493
nft_trans_gc_destroy(struct nft_trans_gc * trans)10494 void nft_trans_gc_destroy(struct nft_trans_gc *trans)
10495 {
10496 nft_set_put(trans->set);
10497 put_net(trans->net);
10498 kfree(trans);
10499 }
10500
nft_trans_gc_trans_free(struct rcu_head * rcu)10501 static void nft_trans_gc_trans_free(struct rcu_head *rcu)
10502 {
10503 struct nft_elem_priv *elem_priv;
10504 struct nft_trans_gc *trans;
10505 struct nft_ctx ctx = {};
10506 unsigned int i;
10507
10508 trans = container_of(rcu, struct nft_trans_gc, rcu);
10509 ctx.net = read_pnet(&trans->set->net);
10510
10511 for (i = 0; i < trans->count; i++) {
10512 elem_priv = trans->priv[i];
10513 if (!nft_setelem_is_catchall(trans->set, elem_priv))
10514 atomic_dec(&trans->set->nelems);
10515
10516 nf_tables_set_elem_destroy(&ctx, trans->set, elem_priv);
10517 }
10518
10519 nft_trans_gc_destroy(trans);
10520 }
10521
nft_trans_gc_work_done(struct nft_trans_gc * trans)10522 static bool nft_trans_gc_work_done(struct nft_trans_gc *trans)
10523 {
10524 struct nftables_pernet *nft_net;
10525 struct nft_ctx ctx = {};
10526
10527 nft_net = nft_pernet(trans->net);
10528
10529 mutex_lock(&nft_net->commit_mutex);
10530
10531 /* Check for race with transaction, otherwise this batch refers to
10532 * stale objects that might not be there anymore. Skip transaction if
10533 * set has been destroyed from control plane transaction in case gc
10534 * worker loses race.
10535 */
10536 if (READ_ONCE(nft_net->gc_seq) != trans->seq || trans->set->dead) {
10537 mutex_unlock(&nft_net->commit_mutex);
10538 return false;
10539 }
10540
10541 ctx.net = trans->net;
10542 ctx.table = trans->set->table;
10543
10544 nft_trans_gc_setelem_remove(&ctx, trans);
10545 mutex_unlock(&nft_net->commit_mutex);
10546
10547 return true;
10548 }
10549
nft_trans_gc_work(struct work_struct * work)10550 static void nft_trans_gc_work(struct work_struct *work)
10551 {
10552 struct nft_trans_gc *trans, *next;
10553 LIST_HEAD(trans_gc_list);
10554
10555 spin_lock(&nf_tables_gc_list_lock);
10556 list_splice_init(&nf_tables_gc_list, &trans_gc_list);
10557 spin_unlock(&nf_tables_gc_list_lock);
10558
10559 list_for_each_entry_safe(trans, next, &trans_gc_list, list) {
10560 list_del(&trans->list);
10561 if (!nft_trans_gc_work_done(trans)) {
10562 nft_trans_gc_destroy(trans);
10563 continue;
10564 }
10565 call_rcu(&trans->rcu, nft_trans_gc_trans_free);
10566 }
10567 }
10568
nft_trans_gc_alloc(struct nft_set * set,unsigned int gc_seq,gfp_t gfp)10569 struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set,
10570 unsigned int gc_seq, gfp_t gfp)
10571 {
10572 struct net *net = read_pnet(&set->net);
10573 struct nft_trans_gc *trans;
10574
10575 trans = kzalloc_obj(*trans, gfp);
10576 if (!trans)
10577 return NULL;
10578
10579 trans->net = maybe_get_net(net);
10580 if (!trans->net) {
10581 kfree(trans);
10582 return NULL;
10583 }
10584
10585 refcount_inc(&set->refs);
10586 trans->set = set;
10587 trans->seq = gc_seq;
10588
10589 return trans;
10590 }
10591
nft_trans_gc_elem_add(struct nft_trans_gc * trans,void * priv)10592 void nft_trans_gc_elem_add(struct nft_trans_gc *trans, void *priv)
10593 {
10594 trans->priv[trans->count++] = priv;
10595 }
10596
nft_trans_gc_queue_work(struct nft_trans_gc * trans)10597 static void nft_trans_gc_queue_work(struct nft_trans_gc *trans)
10598 {
10599 spin_lock(&nf_tables_gc_list_lock);
10600 list_add_tail(&trans->list, &nf_tables_gc_list);
10601 spin_unlock(&nf_tables_gc_list_lock);
10602
10603 schedule_work(&trans_gc_work);
10604 }
10605
nft_trans_gc_queue_async(struct nft_trans_gc * gc,unsigned int gc_seq,gfp_t gfp)10606 struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc,
10607 unsigned int gc_seq, gfp_t gfp)
10608 {
10609 struct nft_set *set;
10610
10611 if (nft_trans_gc_space(gc))
10612 return gc;
10613
10614 set = gc->set;
10615 nft_trans_gc_queue_work(gc);
10616
10617 return nft_trans_gc_alloc(set, gc_seq, gfp);
10618 }
10619
nft_trans_gc_queue_async_done(struct nft_trans_gc * trans)10620 void nft_trans_gc_queue_async_done(struct nft_trans_gc *trans)
10621 {
10622 if (trans->count == 0) {
10623 nft_trans_gc_destroy(trans);
10624 return;
10625 }
10626
10627 nft_trans_gc_queue_work(trans);
10628 }
10629
nft_trans_gc_queue_sync(struct nft_trans_gc * gc,gfp_t gfp)10630 struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp)
10631 {
10632 struct nft_set *set;
10633
10634 if (WARN_ON_ONCE(!lockdep_commit_lock_is_held(gc->net)))
10635 return NULL;
10636
10637 if (nft_trans_gc_space(gc))
10638 return gc;
10639
10640 set = gc->set;
10641 call_rcu(&gc->rcu, nft_trans_gc_trans_free);
10642
10643 return nft_trans_gc_alloc(set, 0, gfp);
10644 }
10645
nft_trans_gc_queue_sync_done(struct nft_trans_gc * trans)10646 void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans)
10647 {
10648 WARN_ON_ONCE(!lockdep_commit_lock_is_held(trans->net));
10649
10650 if (trans->count == 0) {
10651 nft_trans_gc_destroy(trans);
10652 return;
10653 }
10654
10655 call_rcu(&trans->rcu, nft_trans_gc_trans_free);
10656 }
10657
nft_trans_gc_catchall_async(struct nft_trans_gc * gc,unsigned int gc_seq)10658 struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,
10659 unsigned int gc_seq)
10660 {
10661 struct nft_set_elem_catchall *catchall;
10662 const struct nft_set *set = gc->set;
10663 struct nft_set_ext *ext;
10664
10665 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
10666 ext = nft_set_elem_ext(set, catchall->elem);
10667
10668 if (!nft_set_elem_expired(ext))
10669 continue;
10670 if (nft_set_elem_is_dead(ext))
10671 goto dead_elem;
10672
10673 nft_set_elem_dead(ext);
10674 dead_elem:
10675 gc = nft_trans_gc_queue_async(gc, gc_seq, GFP_ATOMIC);
10676 if (!gc)
10677 return NULL;
10678
10679 nft_trans_gc_elem_add(gc, catchall->elem);
10680 }
10681
10682 return gc;
10683 }
10684
nft_trans_gc_catchall_sync(struct nft_trans_gc * gc)10685 struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc)
10686 {
10687 struct nft_set_elem_catchall *catchall, *next;
10688 u64 tstamp = nft_net_tstamp(gc->net);
10689 const struct nft_set *set = gc->set;
10690 struct nft_elem_priv *elem_priv;
10691 struct nft_set_ext *ext;
10692
10693 WARN_ON_ONCE(!lockdep_commit_lock_is_held(gc->net));
10694
10695 list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
10696 ext = nft_set_elem_ext(set, catchall->elem);
10697
10698 if (!__nft_set_elem_expired(ext, tstamp))
10699 continue;
10700
10701 gc = nft_trans_gc_queue_sync(gc, GFP_KERNEL);
10702 if (!gc)
10703 return NULL;
10704
10705 elem_priv = catchall->elem;
10706 nft_setelem_data_deactivate(gc->net, gc->set, elem_priv);
10707 nft_setelem_catchall_destroy(catchall);
10708 nft_trans_gc_elem_add(gc, elem_priv);
10709 }
10710
10711 return gc;
10712 }
10713
nf_tables_module_autoload_cleanup(struct net * net)10714 static void nf_tables_module_autoload_cleanup(struct net *net)
10715 {
10716 struct nftables_pernet *nft_net = nft_pernet(net);
10717 struct nft_module_request *req, *next;
10718
10719 WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
10720 list_for_each_entry_safe(req, next, &nft_net->module_list, list) {
10721 WARN_ON_ONCE(!req->done);
10722 list_del(&req->list);
10723 kfree(req);
10724 }
10725 }
10726
nf_tables_commit_release(struct net * net)10727 static void nf_tables_commit_release(struct net *net)
10728 {
10729 struct nftables_pernet *nft_net = nft_pernet(net);
10730 struct nft_trans *trans;
10731
10732 /* all side effects have to be made visible.
10733 * For example, if a chain named 'foo' has been deleted, a
10734 * new transaction must not find it anymore.
10735 *
10736 * Memory reclaim happens asynchronously from work queue
10737 * to prevent expensive synchronize_rcu() in commit phase.
10738 */
10739 if (list_empty(&nft_net->commit_list)) {
10740 nf_tables_module_autoload_cleanup(net);
10741 mutex_unlock(&nft_net->commit_mutex);
10742 return;
10743 }
10744
10745 trans = list_last_entry(&nft_net->commit_list,
10746 struct nft_trans, list);
10747 get_net(trans->net);
10748 WARN_ON_ONCE(trans->put_net);
10749
10750 trans->put_net = true;
10751 spin_lock(&nf_tables_destroy_list_lock);
10752 list_splice_tail_init(&nft_net->commit_list, &nft_net->destroy_list);
10753 spin_unlock(&nf_tables_destroy_list_lock);
10754
10755 nf_tables_module_autoload_cleanup(net);
10756 schedule_work(&nft_net->destroy_work);
10757
10758 mutex_unlock(&nft_net->commit_mutex);
10759 }
10760
nft_commit_notify(struct net * net,u32 portid)10761 static void nft_commit_notify(struct net *net, u32 portid)
10762 {
10763 struct nftables_pernet *nft_net = nft_pernet(net);
10764 struct sk_buff *batch_skb = NULL, *nskb, *skb;
10765 unsigned char *data;
10766 int len;
10767
10768 list_for_each_entry_safe(skb, nskb, &nft_net->notify_list, list) {
10769 if (!batch_skb) {
10770 new_batch:
10771 batch_skb = skb;
10772 len = NLMSG_GOODSIZE - skb->len;
10773 list_del(&skb->list);
10774 continue;
10775 }
10776 len -= skb->len;
10777 if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) {
10778 data = skb_put(batch_skb, skb->len);
10779 memcpy(data, skb->data, skb->len);
10780 list_del(&skb->list);
10781 kfree_skb(skb);
10782 continue;
10783 }
10784 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
10785 NFT_CB(batch_skb).report, GFP_KERNEL);
10786 goto new_batch;
10787 }
10788
10789 if (batch_skb) {
10790 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
10791 NFT_CB(batch_skb).report, GFP_KERNEL);
10792 }
10793
10794 WARN_ON_ONCE(!list_empty(&nft_net->notify_list));
10795 }
10796
nf_tables_commit_audit_alloc(struct list_head * adl,struct nft_table * table)10797 static int nf_tables_commit_audit_alloc(struct list_head *adl,
10798 struct nft_table *table)
10799 {
10800 struct nft_audit_data *adp;
10801
10802 list_for_each_entry(adp, adl, list) {
10803 if (adp->table == table)
10804 return 0;
10805 }
10806 adp = kzalloc_obj(*adp);
10807 if (!adp)
10808 return -ENOMEM;
10809 adp->table = table;
10810 list_add(&adp->list, adl);
10811 return 0;
10812 }
10813
nf_tables_commit_audit_free(struct list_head * adl)10814 static void nf_tables_commit_audit_free(struct list_head *adl)
10815 {
10816 struct nft_audit_data *adp, *adn;
10817
10818 list_for_each_entry_safe(adp, adn, adl, list) {
10819 list_del(&adp->list);
10820 kfree(adp);
10821 }
10822 }
10823
10824 /* nft audit emits the number of elements that get added/removed/updated,
10825 * so NEW/DELSETELEM needs to increment based on the total elem count.
10826 */
nf_tables_commit_audit_entrycount(const struct nft_trans * trans)10827 static unsigned int nf_tables_commit_audit_entrycount(const struct nft_trans *trans)
10828 {
10829 switch (trans->msg_type) {
10830 case NFT_MSG_NEWSETELEM:
10831 case NFT_MSG_DELSETELEM:
10832 return nft_trans_container_elem(trans)->nelems;
10833 }
10834
10835 return 1;
10836 }
10837
nf_tables_commit_audit_collect(struct list_head * adl,const struct nft_trans * trans,u32 op)10838 static void nf_tables_commit_audit_collect(struct list_head *adl,
10839 const struct nft_trans *trans, u32 op)
10840 {
10841 const struct nft_table *table = trans->table;
10842 struct nft_audit_data *adp;
10843
10844 list_for_each_entry(adp, adl, list) {
10845 if (adp->table == table)
10846 goto found;
10847 }
10848 WARN_ONCE(1, "table=%s not expected in commit list", table->name);
10849 return;
10850 found:
10851 adp->entries += nf_tables_commit_audit_entrycount(trans);
10852 if (!adp->op || adp->op > op)
10853 adp->op = op;
10854 }
10855
10856 #define AUNFTABLENAMELEN (NFT_TABLE_MAXNAMELEN + 22)
10857
nf_tables_commit_audit_log(struct list_head * adl,u32 generation)10858 static void nf_tables_commit_audit_log(struct list_head *adl, u32 generation)
10859 {
10860 struct nft_audit_data *adp, *adn;
10861 char aubuf[AUNFTABLENAMELEN];
10862
10863 list_for_each_entry_safe(adp, adn, adl, list) {
10864 snprintf(aubuf, AUNFTABLENAMELEN, "%s:%u", adp->table->name,
10865 generation);
10866 audit_log_nfcfg(aubuf, adp->table->family, adp->entries,
10867 nft2audit_op[adp->op], GFP_KERNEL);
10868 list_del(&adp->list);
10869 kfree(adp);
10870 }
10871 }
10872
nft_set_commit_update(struct nft_ctx * ctx,struct nftables_pernet * nft_net)10873 static void nft_set_commit_update(struct nft_ctx *ctx,
10874 struct nftables_pernet *nft_net)
10875 {
10876 struct nft_set *set, *next;
10877 struct nft_trans_elem *te;
10878 struct nft_trans *trans;
10879
10880 if (list_empty(&nft_net->set_update_list))
10881 return;
10882
10883 list_for_each_entry(trans, &nft_net->commit_list, list) {
10884 nft_ctx_update(ctx, trans);
10885
10886 switch (trans->msg_type) {
10887 case NFT_MSG_DELSET:
10888 case NFT_MSG_DESTROYSET:
10889 nft_trans_set(trans)->dead = 1;
10890 break;
10891 case NFT_MSG_DELSETELEM:
10892 te = nft_trans_container_elem(trans);
10893 if (!te->set->ops->commit)
10894 break;
10895
10896 nft_trans_elems_remove(ctx, te, false);
10897 break;
10898 }
10899 }
10900
10901 list_for_each_entry_safe(set, next, &nft_net->set_update_list, pending_update) {
10902 list_del_init(&set->pending_update);
10903
10904 if (!set->ops->commit || set->dead)
10905 continue;
10906
10907 set->ops->commit(set);
10908 }
10909 }
10910
nft_gc_seq_begin(struct nftables_pernet * nft_net)10911 static unsigned int nft_gc_seq_begin(struct nftables_pernet *nft_net)
10912 {
10913 unsigned int gc_seq;
10914
10915 /* Bump gc counter, it becomes odd, this is the busy mark. */
10916 gc_seq = READ_ONCE(nft_net->gc_seq);
10917 WRITE_ONCE(nft_net->gc_seq, ++gc_seq);
10918
10919 return gc_seq;
10920 }
10921
nft_gc_seq_end(struct nftables_pernet * nft_net,unsigned int gc_seq)10922 static void nft_gc_seq_end(struct nftables_pernet *nft_net, unsigned int gc_seq)
10923 {
10924 WRITE_ONCE(nft_net->gc_seq, ++gc_seq);
10925 }
10926
nf_tables_commit(struct net * net,struct sk_buff * skb)10927 static int nf_tables_commit(struct net *net, struct sk_buff *skb)
10928 {
10929 struct nftables_pernet *nft_net = nft_pernet(net);
10930 const struct nlmsghdr *nlh = nlmsg_hdr(skb);
10931 struct nft_trans_binding *trans_binding;
10932 struct nft_trans *trans, *next;
10933 unsigned int base_seq, gc_seq;
10934 struct nft_trans_elem *te;
10935 struct nft_chain *chain;
10936 struct nft_table *table;
10937 struct nft_ctx ctx;
10938 LIST_HEAD(adl);
10939 int err;
10940
10941 if (list_empty(&nft_net->commit_list)) {
10942 mutex_unlock(&nft_net->commit_mutex);
10943 return 0;
10944 }
10945
10946 nft_ctx_init(&ctx, net, skb, nlh, NFPROTO_UNSPEC, NULL, NULL, NULL);
10947
10948 list_for_each_entry(trans_binding, &nft_net->binding_list, binding_list) {
10949 trans = &trans_binding->nft_trans;
10950 switch (trans->msg_type) {
10951 case NFT_MSG_NEWSET:
10952 if (!nft_trans_set_update(trans) &&
10953 nft_set_is_anonymous(nft_trans_set(trans)) &&
10954 !nft_trans_set_bound(trans)) {
10955 pr_warn_once("nftables ruleset with unbound set\n");
10956 return -EINVAL;
10957 }
10958 break;
10959 case NFT_MSG_NEWCHAIN:
10960 if (!nft_trans_chain_update(trans) &&
10961 nft_chain_binding(nft_trans_chain(trans)) &&
10962 !nft_trans_chain_bound(trans)) {
10963 pr_warn_once("nftables ruleset with unbound chain\n");
10964 return -EINVAL;
10965 }
10966 break;
10967 default:
10968 WARN_ONCE(1, "Unhandled bind type %d", trans->msg_type);
10969 break;
10970 }
10971 }
10972
10973 /* 0. Validate ruleset, otherwise roll back for error reporting. */
10974 if (nf_tables_validate(net) < 0) {
10975 nft_net->validate_state = NFT_VALIDATE_DO;
10976 return -EAGAIN;
10977 }
10978
10979 /* 1. Allocate space for next generation rules_gen_X[] */
10980 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
10981 struct nft_table *table = trans->table;
10982 int ret;
10983
10984 ret = nf_tables_commit_audit_alloc(&adl, table);
10985 if (ret) {
10986 nf_tables_commit_chain_prepare_cancel(net);
10987 nf_tables_commit_audit_free(&adl);
10988 return ret;
10989 }
10990 if (trans->msg_type == NFT_MSG_NEWRULE ||
10991 trans->msg_type == NFT_MSG_DELRULE) {
10992 chain = nft_trans_rule_chain(trans);
10993
10994 ret = nf_tables_commit_chain_prepare(net, chain);
10995 if (ret < 0) {
10996 nf_tables_commit_chain_prepare_cancel(net);
10997 nf_tables_commit_audit_free(&adl);
10998 return ret;
10999 }
11000 }
11001 }
11002
11003 /* must be last, so audit and chain blob set up does not leave hardware
11004 * in consistent state.
11005 */
11006 err = nft_flow_rule_offload_commit(net);
11007 if (err < 0) {
11008 nf_tables_commit_chain_prepare_cancel(net);
11009 nf_tables_commit_audit_free(&adl);
11010 return err;
11011 }
11012
11013 /* step 2. Make rules_gen_X visible to packet path */
11014 nft_set_commit_update(&ctx, nft_net);
11015
11016 list_for_each_entry(table, &nft_net->tables, list) {
11017 list_for_each_entry(chain, &table->chains, list)
11018 nf_tables_commit_chain(net, chain);
11019 }
11020
11021 /*
11022 * Bump generation counter, invalidate any dump in progress.
11023 * Cannot fail after this point.
11024 */
11025 base_seq = nft_base_seq(net);
11026 while (++base_seq == 0)
11027 ;
11028
11029 /* pairs with smp_load_acquire in nft_lookup_eval */
11030 smp_store_release(&net->nft.base_seq, base_seq);
11031
11032 gc_seq = nft_gc_seq_begin(nft_net);
11033
11034 /* step 3. Start new generation, rules_gen_X now in use. */
11035 net->nft.gencursor = nft_gencursor_next(net);
11036
11037 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
11038 struct nft_table *table = trans->table;
11039
11040 nft_ctx_update(&ctx, trans);
11041
11042 nf_tables_commit_audit_collect(&adl, trans, trans->msg_type);
11043 switch (trans->msg_type) {
11044 case NFT_MSG_NEWTABLE:
11045 if (nft_trans_table_update(trans)) {
11046 if (!(table->flags & __NFT_TABLE_F_UPDATE)) {
11047 nft_trans_destroy(trans);
11048 break;
11049 }
11050 if (table->flags & NFT_TABLE_F_DORMANT)
11051 nf_tables_table_disable(net, table);
11052
11053 table->flags &= ~__NFT_TABLE_F_UPDATE;
11054 } else {
11055 nft_clear(net, table);
11056 }
11057 nf_tables_table_notify(&ctx, NFT_MSG_NEWTABLE);
11058 nft_trans_destroy(trans);
11059 break;
11060 case NFT_MSG_DELTABLE:
11061 case NFT_MSG_DESTROYTABLE:
11062 list_del_rcu(&table->list);
11063 nf_tables_table_notify(&ctx, trans->msg_type);
11064 break;
11065 case NFT_MSG_NEWCHAIN:
11066 if (nft_trans_chain_update(trans)) {
11067 nft_chain_commit_update(nft_trans_container_chain(trans));
11068 nf_tables_chain_notify(&ctx, NFT_MSG_NEWCHAIN,
11069 &nft_trans_chain_hooks(trans), NULL);
11070 list_splice_rcu(&nft_trans_chain_hooks(trans),
11071 &nft_trans_basechain(trans)->hook_list);
11072 /* trans destroyed after rcu grace period */
11073 } else {
11074 nft_chain_commit_drop_policy(nft_trans_container_chain(trans));
11075 nft_clear(net, nft_trans_chain(trans));
11076 nf_tables_chain_notify(&ctx, NFT_MSG_NEWCHAIN, NULL, NULL);
11077 nft_trans_destroy(trans);
11078 }
11079 break;
11080 case NFT_MSG_DELCHAIN:
11081 case NFT_MSG_DESTROYCHAIN:
11082 if (nft_trans_chain_update(trans)) {
11083 nf_tables_chain_notify(&ctx, NFT_MSG_DELCHAIN, NULL,
11084 &nft_trans_chain_hooks(trans));
11085 nft_netdev_unregister_trans_hook(net, table,
11086 &nft_trans_chain_hooks(trans));
11087 } else {
11088 nft_chain_del(nft_trans_chain(trans));
11089 nf_tables_chain_notify(&ctx, NFT_MSG_DELCHAIN,
11090 NULL, NULL);
11091 nf_tables_unregister_hook(ctx.net, ctx.table,
11092 nft_trans_chain(trans));
11093 }
11094 break;
11095 case NFT_MSG_NEWRULE:
11096 nft_clear(net, nft_trans_rule(trans));
11097 nf_tables_rule_notify(&ctx, nft_trans_rule(trans),
11098 NFT_MSG_NEWRULE);
11099 if (nft_trans_rule_chain(trans)->flags & NFT_CHAIN_HW_OFFLOAD)
11100 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
11101
11102 nft_trans_destroy(trans);
11103 break;
11104 case NFT_MSG_DELRULE:
11105 case NFT_MSG_DESTROYRULE:
11106 list_del_rcu(&nft_trans_rule(trans)->list);
11107 nf_tables_rule_notify(&ctx, nft_trans_rule(trans),
11108 trans->msg_type);
11109 nft_rule_expr_deactivate(&ctx, nft_trans_rule(trans),
11110 NFT_TRANS_COMMIT);
11111
11112 if (nft_trans_rule_chain(trans)->flags & NFT_CHAIN_HW_OFFLOAD)
11113 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
11114 break;
11115 case NFT_MSG_NEWSET:
11116 list_del(&nft_trans_container_set(trans)->list_trans_newset);
11117 if (nft_trans_set_update(trans)) {
11118 struct nft_set *set = nft_trans_set(trans);
11119
11120 WRITE_ONCE(set->timeout, nft_trans_set_timeout(trans));
11121 WRITE_ONCE(set->gc_int, nft_trans_set_gc_int(trans));
11122
11123 if (nft_trans_set_size(trans))
11124 WRITE_ONCE(set->size, nft_trans_set_size(trans));
11125 } else {
11126 nft_clear(net, nft_trans_set(trans));
11127 /* This avoids hitting -EBUSY when deleting the table
11128 * from the transaction.
11129 */
11130 if (nft_set_is_anonymous(nft_trans_set(trans)) &&
11131 !list_empty(&nft_trans_set(trans)->bindings))
11132 nft_use_dec(&table->use);
11133 }
11134 nf_tables_set_notify(&ctx, nft_trans_set(trans),
11135 NFT_MSG_NEWSET, GFP_KERNEL);
11136 nft_trans_destroy(trans);
11137 break;
11138 case NFT_MSG_DELSET:
11139 case NFT_MSG_DESTROYSET:
11140 nft_trans_set(trans)->dead = 1;
11141 list_del_rcu(&nft_trans_set(trans)->list);
11142 nf_tables_set_notify(&ctx, nft_trans_set(trans),
11143 trans->msg_type, GFP_KERNEL);
11144 break;
11145 case NFT_MSG_NEWSETELEM:
11146 te = nft_trans_container_elem(trans);
11147 nft_trans_elems_add(&ctx, te);
11148 nft_trans_destroy(trans);
11149 break;
11150 case NFT_MSG_DELSETELEM:
11151 case NFT_MSG_DESTROYSETELEM:
11152 te = nft_trans_container_elem(trans);
11153 if (te->set->ops->commit)
11154 nft_trans_elems_remove_notify(&ctx, te);
11155 else
11156 nft_trans_elems_remove(&ctx, te, true);
11157 break;
11158 case NFT_MSG_NEWOBJ:
11159 if (nft_trans_obj_update(trans)) {
11160 nft_obj_commit_update(&ctx, trans);
11161 nf_tables_obj_notify(&ctx,
11162 nft_trans_obj(trans),
11163 NFT_MSG_NEWOBJ);
11164 } else {
11165 nft_clear(net, nft_trans_obj(trans));
11166 nf_tables_obj_notify(&ctx,
11167 nft_trans_obj(trans),
11168 NFT_MSG_NEWOBJ);
11169 nft_trans_destroy(trans);
11170 }
11171 break;
11172 case NFT_MSG_DELOBJ:
11173 case NFT_MSG_DESTROYOBJ:
11174 nft_obj_del(table, nft_trans_obj(trans));
11175 nf_tables_obj_notify(&ctx, nft_trans_obj(trans),
11176 trans->msg_type);
11177 break;
11178 case NFT_MSG_NEWFLOWTABLE:
11179 if (nft_trans_flowtable_update(trans)) {
11180 nft_trans_flowtable(trans)->data.flags =
11181 nft_trans_flowtable_flags(trans);
11182 nf_tables_flowtable_notify(&ctx,
11183 nft_trans_flowtable(trans),
11184 &nft_trans_flowtable_hooks(trans),
11185 NULL,
11186 NFT_MSG_NEWFLOWTABLE);
11187 list_splice_rcu(&nft_trans_flowtable_hooks(trans),
11188 &nft_trans_flowtable(trans)->hook_list);
11189 } else {
11190 nft_clear(net, nft_trans_flowtable(trans));
11191 nf_tables_flowtable_notify(&ctx,
11192 nft_trans_flowtable(trans),
11193 NULL,
11194 NULL,
11195 NFT_MSG_NEWFLOWTABLE);
11196 }
11197 nft_trans_destroy(trans);
11198 break;
11199 case NFT_MSG_DELFLOWTABLE:
11200 case NFT_MSG_DESTROYFLOWTABLE:
11201 if (nft_trans_flowtable_update(trans)) {
11202 nf_tables_flowtable_notify(&ctx,
11203 nft_trans_flowtable(trans),
11204 NULL,
11205 &nft_trans_flowtable_hooks(trans),
11206 trans->msg_type);
11207 nft_flowtable_unregister_trans_hook(net,
11208 nft_trans_flowtable(trans),
11209 &nft_trans_flowtable_hooks(trans));
11210 } else {
11211 list_del_rcu(&nft_trans_flowtable(trans)->list);
11212 nf_tables_flowtable_notify(&ctx,
11213 nft_trans_flowtable(trans),
11214 NULL,
11215 NULL,
11216 trans->msg_type);
11217 nft_unregister_flowtable_net_hooks(net,
11218 nft_trans_flowtable(trans),
11219 &nft_trans_flowtable(trans)->hook_list);
11220 }
11221 break;
11222 }
11223 }
11224
11225 nft_commit_notify(net, NETLINK_CB(skb).portid);
11226 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
11227 nf_tables_commit_audit_log(&adl, nft_base_seq(net));
11228
11229 nft_gc_seq_end(nft_net, gc_seq);
11230 nft_net->validate_state = NFT_VALIDATE_SKIP;
11231 nf_tables_commit_release(net);
11232
11233 return 0;
11234 }
11235
nf_tables_module_autoload(struct net * net)11236 static void nf_tables_module_autoload(struct net *net)
11237 {
11238 struct nftables_pernet *nft_net = nft_pernet(net);
11239 struct nft_module_request *req, *next;
11240 LIST_HEAD(module_list);
11241
11242 list_splice_init(&nft_net->module_list, &module_list);
11243 mutex_unlock(&nft_net->commit_mutex);
11244 list_for_each_entry_safe(req, next, &module_list, list) {
11245 request_module("%s", req->module);
11246 req->done = true;
11247 }
11248 mutex_lock(&nft_net->commit_mutex);
11249 list_splice(&module_list, &nft_net->module_list);
11250 }
11251
nf_tables_abort_release(struct nft_trans * trans)11252 static void nf_tables_abort_release(struct nft_trans *trans)
11253 {
11254 struct nft_ctx ctx = { };
11255
11256 nft_ctx_update(&ctx, trans);
11257
11258 switch (trans->msg_type) {
11259 case NFT_MSG_NEWTABLE:
11260 nf_tables_table_destroy(trans->table);
11261 break;
11262 case NFT_MSG_NEWCHAIN:
11263 if (nft_trans_chain_update(trans))
11264 nft_hooks_destroy(&nft_trans_chain_hooks(trans));
11265 else
11266 nf_tables_chain_destroy(nft_trans_chain(trans));
11267 break;
11268 case NFT_MSG_NEWRULE:
11269 nf_tables_rule_destroy(&ctx, nft_trans_rule(trans));
11270 break;
11271 case NFT_MSG_NEWSET:
11272 nft_set_destroy(&ctx, nft_trans_set(trans));
11273 break;
11274 case NFT_MSG_NEWSETELEM:
11275 nft_trans_set_elem_destroy(&ctx, nft_trans_container_elem(trans));
11276 break;
11277 case NFT_MSG_NEWOBJ:
11278 nft_obj_destroy(&ctx, nft_trans_obj(trans));
11279 break;
11280 case NFT_MSG_NEWFLOWTABLE:
11281 if (nft_trans_flowtable_update(trans))
11282 nft_hooks_destroy(&nft_trans_flowtable_hooks(trans));
11283 else
11284 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
11285 break;
11286 }
11287 kfree(trans);
11288 }
11289
nft_set_abort_update(struct nftables_pernet * nft_net)11290 static void nft_set_abort_update(struct nftables_pernet *nft_net)
11291 {
11292 struct nft_set *set, *next;
11293
11294 list_for_each_entry_safe(set, next, &nft_net->set_update_list, pending_update) {
11295 list_del_init(&set->pending_update);
11296
11297 if (!set->ops->abort)
11298 continue;
11299
11300 set->ops->abort(set);
11301 }
11302 }
11303
__nf_tables_abort(struct net * net,enum nfnl_abort_action action)11304 static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
11305 {
11306 struct nftables_pernet *nft_net = nft_pernet(net);
11307 struct nft_trans *trans, *next;
11308 struct nft_trans_elem *te;
11309 struct nft_ctx ctx = {
11310 .net = net,
11311 };
11312 int err = 0;
11313
11314 if (action == NFNL_ABORT_VALIDATE &&
11315 nf_tables_validate(net) < 0)
11316 err = -EAGAIN;
11317
11318 list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list,
11319 list) {
11320 struct nft_table *table = trans->table;
11321
11322 nft_ctx_update(&ctx, trans);
11323
11324 switch (trans->msg_type) {
11325 case NFT_MSG_NEWTABLE:
11326 if (nft_trans_table_update(trans)) {
11327 if (!(table->flags & __NFT_TABLE_F_UPDATE)) {
11328 nft_trans_destroy(trans);
11329 break;
11330 }
11331 if (table->flags & __NFT_TABLE_F_WAS_DORMANT) {
11332 nf_tables_table_disable(net, table);
11333 table->flags |= NFT_TABLE_F_DORMANT;
11334 } else if (table->flags & __NFT_TABLE_F_WAS_AWAKEN) {
11335 table->flags &= ~NFT_TABLE_F_DORMANT;
11336 }
11337 if (table->flags & __NFT_TABLE_F_WAS_ORPHAN) {
11338 table->flags &= ~NFT_TABLE_F_OWNER;
11339 table->nlpid = 0;
11340 }
11341 table->flags &= ~__NFT_TABLE_F_UPDATE;
11342 nft_trans_destroy(trans);
11343 } else {
11344 list_del_rcu(&table->list);
11345 }
11346 break;
11347 case NFT_MSG_DELTABLE:
11348 case NFT_MSG_DESTROYTABLE:
11349 nft_clear(trans->net, table);
11350 nft_trans_destroy(trans);
11351 break;
11352 case NFT_MSG_NEWCHAIN:
11353 if (nft_trans_chain_update(trans)) {
11354 nft_netdev_unregister_hooks(net, table,
11355 &nft_trans_chain_hooks(trans),
11356 true);
11357 free_percpu(nft_trans_chain_stats(trans));
11358 kfree(nft_trans_chain_name(trans));
11359 nft_trans_destroy(trans);
11360 } else {
11361 if (nft_trans_chain_bound(trans)) {
11362 nft_trans_destroy(trans);
11363 break;
11364 }
11365 nft_use_dec_restore(&table->use);
11366 nft_chain_del(nft_trans_chain(trans));
11367 nf_tables_unregister_hook(trans->net, table,
11368 nft_trans_chain(trans));
11369 }
11370 break;
11371 case NFT_MSG_DELCHAIN:
11372 case NFT_MSG_DESTROYCHAIN:
11373 if (nft_trans_chain_update(trans)) {
11374 nft_trans_delhook_abort(&nft_trans_chain_hooks(trans));
11375 } else {
11376 nft_use_inc_restore(&table->use);
11377 nft_clear(trans->net, nft_trans_chain(trans));
11378 }
11379 nft_trans_destroy(trans);
11380 break;
11381 case NFT_MSG_NEWRULE:
11382 if (nft_trans_rule_bound(trans)) {
11383 nft_trans_destroy(trans);
11384 break;
11385 }
11386 nft_use_dec_restore(&nft_trans_rule_chain(trans)->use);
11387 list_del_rcu(&nft_trans_rule(trans)->list);
11388 nft_rule_expr_deactivate(&ctx,
11389 nft_trans_rule(trans),
11390 NFT_TRANS_ABORT);
11391 if (nft_trans_rule_chain(trans)->flags & NFT_CHAIN_HW_OFFLOAD)
11392 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
11393 break;
11394 case NFT_MSG_DELRULE:
11395 case NFT_MSG_DESTROYRULE:
11396 nft_use_inc_restore(&nft_trans_rule_chain(trans)->use);
11397 nft_clear(trans->net, nft_trans_rule(trans));
11398 nft_rule_expr_activate(&ctx, nft_trans_rule(trans));
11399 if (nft_trans_rule_chain(trans)->flags & NFT_CHAIN_HW_OFFLOAD)
11400 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
11401
11402 nft_trans_destroy(trans);
11403 break;
11404 case NFT_MSG_NEWSET:
11405 list_del(&nft_trans_container_set(trans)->list_trans_newset);
11406 if (nft_trans_set_update(trans)) {
11407 nft_trans_destroy(trans);
11408 break;
11409 }
11410 nft_use_dec_restore(&table->use);
11411 if (nft_trans_set_bound(trans)) {
11412 nft_trans_destroy(trans);
11413 break;
11414 }
11415 nft_trans_set(trans)->dead = 1;
11416 list_del_rcu(&nft_trans_set(trans)->list);
11417 break;
11418 case NFT_MSG_DELSET:
11419 case NFT_MSG_DESTROYSET:
11420 nft_use_inc_restore(&table->use);
11421 nft_clear(trans->net, nft_trans_set(trans));
11422 if (nft_trans_set(trans)->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
11423 nft_map_activate(&ctx, nft_trans_set(trans));
11424
11425 nft_trans_destroy(trans);
11426 break;
11427 case NFT_MSG_NEWSETELEM:
11428 te = nft_trans_container_elem(trans);
11429 if (nft_trans_elem_set_bound(trans)) {
11430 list_del_init(&te->set->pending_update);
11431 nft_trans_destroy(trans);
11432 break;
11433 }
11434 if (!nft_trans_elems_new_abort(&ctx, te)) {
11435 nft_trans_destroy(trans);
11436 break;
11437 }
11438 break;
11439 case NFT_MSG_DELSETELEM:
11440 case NFT_MSG_DESTROYSETELEM:
11441 te = nft_trans_container_elem(trans);
11442
11443 nft_trans_elems_destroy_abort(&ctx, te);
11444 nft_trans_destroy(trans);
11445 break;
11446 case NFT_MSG_NEWOBJ:
11447 if (nft_trans_obj_update(trans)) {
11448 nft_obj_destroy(&ctx, nft_trans_obj_newobj(trans));
11449 nft_trans_destroy(trans);
11450 } else {
11451 nft_use_dec_restore(&table->use);
11452 nft_obj_del(table, nft_trans_obj(trans));
11453 }
11454 break;
11455 case NFT_MSG_DELOBJ:
11456 case NFT_MSG_DESTROYOBJ:
11457 nft_use_inc_restore(&table->use);
11458 nft_clear(trans->net, nft_trans_obj(trans));
11459 nft_trans_destroy(trans);
11460 break;
11461 case NFT_MSG_NEWFLOWTABLE:
11462 if (nft_trans_flowtable_update(trans)) {
11463 nft_unregister_flowtable_net_hooks(net,
11464 nft_trans_flowtable(trans),
11465 &nft_trans_flowtable_hooks(trans));
11466 } else {
11467 nft_use_dec_restore(&table->use);
11468 list_del_rcu(&nft_trans_flowtable(trans)->list);
11469 nft_unregister_flowtable_net_hooks(net,
11470 nft_trans_flowtable(trans),
11471 &nft_trans_flowtable(trans)->hook_list);
11472 }
11473 break;
11474 case NFT_MSG_DELFLOWTABLE:
11475 case NFT_MSG_DESTROYFLOWTABLE:
11476 if (nft_trans_flowtable_update(trans)) {
11477 nft_trans_delhook_abort(&nft_trans_flowtable_hooks(trans));
11478 } else {
11479 nft_use_inc_restore(&table->use);
11480 nft_clear(trans->net, nft_trans_flowtable(trans));
11481 }
11482 nft_trans_destroy(trans);
11483 break;
11484 }
11485 }
11486
11487 WARN_ON_ONCE(!list_empty(&nft_net->commit_set_list));
11488
11489 nft_set_abort_update(nft_net);
11490
11491 synchronize_rcu();
11492
11493 list_for_each_entry_safe_reverse(trans, next,
11494 &nft_net->commit_list, list) {
11495 nft_trans_list_del(trans);
11496 nf_tables_abort_release(trans);
11497 }
11498
11499 return err;
11500 }
11501
nf_tables_abort(struct net * net,struct sk_buff * skb,enum nfnl_abort_action action)11502 static int nf_tables_abort(struct net *net, struct sk_buff *skb,
11503 enum nfnl_abort_action action)
11504 {
11505 struct nftables_pernet *nft_net = nft_pernet(net);
11506 unsigned int gc_seq;
11507 int ret;
11508
11509 gc_seq = nft_gc_seq_begin(nft_net);
11510 ret = __nf_tables_abort(net, action);
11511 nft_gc_seq_end(nft_net, gc_seq);
11512
11513 if (action == NFNL_ABORT_NONE) {
11514 struct nft_table *table;
11515
11516 list_for_each_entry(table, &nft_net->tables, list)
11517 table->validate_state = NFT_VALIDATE_SKIP;
11518 }
11519
11520 WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
11521
11522 /* module autoload needs to happen after GC sequence update because it
11523 * temporarily releases and grabs mutex again.
11524 */
11525 if (action == NFNL_ABORT_AUTOLOAD)
11526 nf_tables_module_autoload(net);
11527 else
11528 nf_tables_module_autoload_cleanup(net);
11529
11530 mutex_unlock(&nft_net->commit_mutex);
11531
11532 return ret;
11533 }
11534
nf_tables_valid_genid(struct net * net,u32 genid)11535 static bool nf_tables_valid_genid(struct net *net, u32 genid)
11536 {
11537 struct nftables_pernet *nft_net = nft_pernet(net);
11538 bool genid_ok;
11539
11540 mutex_lock(&nft_net->commit_mutex);
11541 nft_net->tstamp = get_jiffies_64();
11542
11543 genid_ok = genid == 0 || nft_base_seq(net) == genid;
11544 if (!genid_ok)
11545 mutex_unlock(&nft_net->commit_mutex);
11546
11547 /* else, commit mutex has to be released by commit or abort function */
11548 return genid_ok;
11549 }
11550
11551 static const struct nfnetlink_subsystem nf_tables_subsys = {
11552 .name = "nf_tables",
11553 .subsys_id = NFNL_SUBSYS_NFTABLES,
11554 .cb_count = NFT_MSG_MAX,
11555 .cb = nf_tables_cb,
11556 .commit = nf_tables_commit,
11557 .abort = nf_tables_abort,
11558 .valid_genid = nf_tables_valid_genid,
11559 .owner = THIS_MODULE,
11560 };
11561
nft_chain_validate_dependency(const struct nft_chain * chain,enum nft_chain_types type)11562 int nft_chain_validate_dependency(const struct nft_chain *chain,
11563 enum nft_chain_types type)
11564 {
11565 const struct nft_base_chain *basechain;
11566
11567 if (nft_is_base_chain(chain)) {
11568 basechain = nft_base_chain(chain);
11569 if (basechain->type->type != type)
11570 return -EOPNOTSUPP;
11571 }
11572 return 0;
11573 }
11574 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
11575
nft_chain_validate_hooks(const struct nft_chain * chain,unsigned int hook_flags)11576 int nft_chain_validate_hooks(const struct nft_chain *chain,
11577 unsigned int hook_flags)
11578 {
11579 struct nft_base_chain *basechain;
11580
11581 if (nft_is_base_chain(chain)) {
11582 basechain = nft_base_chain(chain);
11583
11584 if ((1 << basechain->ops.hooknum) & hook_flags)
11585 return 0;
11586
11587 return -EOPNOTSUPP;
11588 }
11589
11590 return 0;
11591 }
11592 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
11593
11594 /**
11595 * nft_parse_u32_check - fetch u32 attribute and check for maximum value
11596 *
11597 * @attr: netlink attribute to fetch value from
11598 * @max: maximum value to be stored in dest
11599 * @dest: pointer to the variable
11600 *
11601 * Parse, check and store a given u32 netlink attribute into variable.
11602 * This function returns -ERANGE if the value goes over maximum value.
11603 * Otherwise a 0 is returned and the attribute value is stored in the
11604 * destination variable.
11605 */
nft_parse_u32_check(const struct nlattr * attr,int max,u32 * dest)11606 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest)
11607 {
11608 u32 val;
11609
11610 val = ntohl(nla_get_be32(attr));
11611 if (val > max)
11612 return -ERANGE;
11613
11614 *dest = val;
11615 return 0;
11616 }
11617 EXPORT_SYMBOL_GPL(nft_parse_u32_check);
11618
nft_parse_register(const struct nlattr * attr,u32 * preg)11619 static int nft_parse_register(const struct nlattr *attr, u32 *preg)
11620 {
11621 unsigned int reg;
11622
11623 reg = ntohl(nla_get_be32(attr));
11624 switch (reg) {
11625 case NFT_REG_VERDICT...NFT_REG_4:
11626 *preg = reg * NFT_REG_SIZE / NFT_REG32_SIZE;
11627 break;
11628 case NFT_REG32_00...NFT_REG32_15:
11629 *preg = reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
11630 break;
11631 default:
11632 return -ERANGE;
11633 }
11634
11635 return 0;
11636 }
11637
11638 /**
11639 * nft_dump_register - dump a register value to a netlink attribute
11640 *
11641 * @skb: socket buffer
11642 * @attr: attribute number
11643 * @reg: register number
11644 *
11645 * Construct a netlink attribute containing the register number. For
11646 * compatibility reasons, register numbers being a multiple of 4 are
11647 * translated to the corresponding 128 bit register numbers.
11648 */
nft_dump_register(struct sk_buff * skb,unsigned int attr,unsigned int reg)11649 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
11650 {
11651 if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
11652 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
11653 else
11654 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
11655
11656 return nla_put_be32(skb, attr, htonl(reg));
11657 }
11658 EXPORT_SYMBOL_GPL(nft_dump_register);
11659
nft_validate_register_load(enum nft_registers reg,unsigned int len)11660 static int nft_validate_register_load(enum nft_registers reg, unsigned int len)
11661 {
11662 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
11663 return -EINVAL;
11664 if (len == 0)
11665 return -EINVAL;
11666 if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data))
11667 return -ERANGE;
11668
11669 return 0;
11670 }
11671
nft_parse_register_load(const struct nft_ctx * ctx,const struct nlattr * attr,u8 * sreg,u32 len)11672 int nft_parse_register_load(const struct nft_ctx *ctx,
11673 const struct nlattr *attr, u8 *sreg, u32 len)
11674 {
11675 int err, invalid_reg;
11676 u32 reg, next_register;
11677
11678 err = nft_parse_register(attr, ®);
11679 if (err < 0)
11680 return err;
11681
11682 err = nft_validate_register_load(reg, len);
11683 if (err < 0)
11684 return err;
11685
11686 next_register = DIV_ROUND_UP(len, NFT_REG32_SIZE) + reg;
11687
11688 /* Can't happen: nft_validate_register_load() should have failed */
11689 if (unlikely(next_register > NFT_REG32_NUM)) {
11690 DEBUG_NET_WARN_ON_ONCE(1);
11691 return -EINVAL;
11692 }
11693
11694 /* find first register that did not see an earlier store. */
11695 invalid_reg = find_next_zero_bit(ctx->reg_inited, NFT_REG32_NUM, reg);
11696
11697 /* invalid register within the range that we're loading from? */
11698 if (invalid_reg < next_register)
11699 return -ENODATA;
11700
11701 *sreg = reg;
11702 return 0;
11703 }
11704 EXPORT_SYMBOL_GPL(nft_parse_register_load);
11705
nft_saw_register_store(const struct nft_ctx * __ctx,int reg,unsigned int len)11706 static void nft_saw_register_store(const struct nft_ctx *__ctx,
11707 int reg, unsigned int len)
11708 {
11709 unsigned int registers = DIV_ROUND_UP(len, NFT_REG32_SIZE);
11710 struct nft_ctx *ctx = (struct nft_ctx *)__ctx;
11711
11712 if (WARN_ON_ONCE(len == 0 || reg < 0))
11713 return;
11714
11715 bitmap_set(ctx->reg_inited, reg, registers);
11716 }
11717
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)11718 static int nft_validate_register_store(const struct nft_ctx *ctx,
11719 enum nft_registers reg,
11720 const struct nft_data *data,
11721 enum nft_data_types type,
11722 unsigned int len)
11723 {
11724 switch (reg) {
11725 case NFT_REG_VERDICT:
11726 if (type != NFT_DATA_VERDICT)
11727 return -EINVAL;
11728 break;
11729 default:
11730 if (type != NFT_DATA_VALUE)
11731 return -EINVAL;
11732
11733 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
11734 return -EINVAL;
11735 if (len == 0)
11736 return -EINVAL;
11737 if (reg * NFT_REG32_SIZE + len >
11738 sizeof_field(struct nft_regs, data))
11739 return -ERANGE;
11740
11741 break;
11742 }
11743
11744 nft_saw_register_store(ctx, reg, len);
11745 return 0;
11746 }
11747
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)11748 int nft_parse_register_store(const struct nft_ctx *ctx,
11749 const struct nlattr *attr, u8 *dreg,
11750 const struct nft_data *data,
11751 enum nft_data_types type, unsigned int len)
11752 {
11753 int err;
11754 u32 reg;
11755
11756 err = nft_parse_register(attr, ®);
11757 if (err < 0)
11758 return err;
11759
11760 err = nft_validate_register_store(ctx, reg, data, type, len);
11761 if (err < 0)
11762 return err;
11763
11764 *dreg = reg;
11765 return 0;
11766 }
11767 EXPORT_SYMBOL_GPL(nft_parse_register_store);
11768
11769 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
11770 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
11771 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
11772 .len = NFT_CHAIN_MAXNAMELEN - 1 },
11773 [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 },
11774 };
11775
nft_verdict_init(const struct nft_ctx * ctx,struct nft_data * data,struct nft_data_desc * desc,const struct nlattr * nla)11776 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
11777 struct nft_data_desc *desc, const struct nlattr *nla)
11778 {
11779 u8 genmask = nft_genmask_next(ctx->net);
11780 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
11781 struct nft_chain *chain;
11782 int err;
11783
11784 err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla,
11785 nft_verdict_policy, NULL);
11786 if (err < 0)
11787 return err;
11788
11789 if (!tb[NFTA_VERDICT_CODE])
11790 return -EINVAL;
11791
11792 /* zero padding hole for memcmp */
11793 memset(data, 0, sizeof(*data));
11794 data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
11795
11796 switch (data->verdict.code) {
11797 case NF_ACCEPT:
11798 case NF_DROP:
11799 case NFT_CONTINUE:
11800 case NFT_BREAK:
11801 case NFT_RETURN:
11802 break;
11803 case NFT_JUMP:
11804 case NFT_GOTO:
11805 if (tb[NFTA_VERDICT_CHAIN]) {
11806 chain = nft_chain_lookup(ctx->net, ctx->table,
11807 tb[NFTA_VERDICT_CHAIN],
11808 genmask);
11809 } else if (tb[NFTA_VERDICT_CHAIN_ID]) {
11810 chain = nft_chain_lookup_byid(ctx->net, ctx->table,
11811 tb[NFTA_VERDICT_CHAIN_ID],
11812 genmask);
11813 if (IS_ERR(chain))
11814 return PTR_ERR(chain);
11815 } else {
11816 return -EINVAL;
11817 }
11818
11819 if (IS_ERR(chain))
11820 return PTR_ERR(chain);
11821 if (nft_is_base_chain(chain))
11822 return -EOPNOTSUPP;
11823 if (nft_chain_is_bound(chain))
11824 return -EINVAL;
11825 if (desc->flags & NFT_DATA_DESC_SETELEM &&
11826 chain->flags & NFT_CHAIN_BINDING)
11827 return -EINVAL;
11828 if (!nft_use_inc(&chain->use))
11829 return -EMFILE;
11830
11831 data->verdict.chain = chain;
11832 break;
11833 case NF_QUEUE:
11834 /* The nft_queue expression is used for this purpose, an
11835 * immediate NF_QUEUE verdict should not ever be seen here.
11836 */
11837 fallthrough;
11838 default:
11839 return -EINVAL;
11840 }
11841
11842 desc->len = sizeof(data->verdict);
11843
11844 return 0;
11845 }
11846
nft_verdict_uninit(const struct nft_data * data)11847 static void nft_verdict_uninit(const struct nft_data *data)
11848 {
11849 struct nft_chain *chain;
11850
11851 switch (data->verdict.code) {
11852 case NFT_JUMP:
11853 case NFT_GOTO:
11854 chain = data->verdict.chain;
11855 nft_use_dec(&chain->use);
11856 break;
11857 }
11858 }
11859
nft_verdict_dump(struct sk_buff * skb,int type,const struct nft_verdict * v)11860 int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
11861 {
11862 struct nlattr *nest;
11863
11864 nest = nla_nest_start_noflag(skb, type);
11865 if (!nest)
11866 goto nla_put_failure;
11867
11868 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
11869 goto nla_put_failure;
11870
11871 switch (v->code) {
11872 case NFT_JUMP:
11873 case NFT_GOTO:
11874 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
11875 v->chain->name))
11876 goto nla_put_failure;
11877 }
11878 nla_nest_end(skb, nest);
11879 return 0;
11880
11881 nla_put_failure:
11882 return -1;
11883 }
11884
nft_value_init(const struct nft_ctx * ctx,struct nft_data * data,struct nft_data_desc * desc,const struct nlattr * nla)11885 static int nft_value_init(const struct nft_ctx *ctx,
11886 struct nft_data *data, struct nft_data_desc *desc,
11887 const struct nlattr *nla)
11888 {
11889 unsigned int len;
11890
11891 len = nla_len(nla);
11892 if (len == 0)
11893 return -EINVAL;
11894 if (len > desc->size)
11895 return -EOVERFLOW;
11896 if (desc->len) {
11897 if (len != desc->len)
11898 return -EINVAL;
11899 } else {
11900 desc->len = len;
11901 }
11902
11903 nla_memcpy(data->data, nla, len);
11904
11905 return 0;
11906 }
11907
nft_value_dump(struct sk_buff * skb,const struct nft_data * data,unsigned int len)11908 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
11909 unsigned int len)
11910 {
11911 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
11912 }
11913
11914 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
11915 [NFTA_DATA_VALUE] = { .type = NLA_BINARY },
11916 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
11917 };
11918
11919 /**
11920 * nft_data_init - parse nf_tables data netlink attributes
11921 *
11922 * @ctx: context of the expression using the data
11923 * @data: destination struct nft_data
11924 * @desc: data description
11925 * @nla: netlink attribute containing data
11926 *
11927 * Parse the netlink data attributes and initialize a struct nft_data.
11928 * The type and length of data are returned in the data description.
11929 *
11930 * The caller can indicate that it only wants to accept data of type
11931 * NFT_DATA_VALUE by passing NULL for the ctx argument.
11932 */
nft_data_init(const struct nft_ctx * ctx,struct nft_data * data,struct nft_data_desc * desc,const struct nlattr * nla)11933 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
11934 struct nft_data_desc *desc, const struct nlattr *nla)
11935 {
11936 struct nlattr *tb[NFTA_DATA_MAX + 1];
11937 int err;
11938
11939 if (unlikely(!desc->size)) {
11940 DEBUG_NET_WARN_ON_ONCE(1);
11941 return -EINVAL;
11942 }
11943
11944 err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla,
11945 nft_data_policy, NULL);
11946 if (err < 0)
11947 return err;
11948
11949 if (tb[NFTA_DATA_VALUE]) {
11950 if (desc->type != NFT_DATA_VALUE)
11951 return -EINVAL;
11952
11953 err = nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
11954 } else if (tb[NFTA_DATA_VERDICT] && ctx != NULL) {
11955 if (desc->type != NFT_DATA_VERDICT)
11956 return -EINVAL;
11957
11958 err = nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
11959 } else {
11960 err = -EINVAL;
11961 }
11962
11963 return err;
11964 }
11965 EXPORT_SYMBOL_GPL(nft_data_init);
11966
11967 /**
11968 * nft_data_release - release a nft_data item
11969 *
11970 * @data: struct nft_data to release
11971 * @type: type of data
11972 *
11973 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
11974 * all others need to be released by calling this function.
11975 */
nft_data_release(const struct nft_data * data,enum nft_data_types type)11976 void nft_data_release(const struct nft_data *data, enum nft_data_types type)
11977 {
11978 if (type < NFT_DATA_VERDICT)
11979 return;
11980 switch (type) {
11981 case NFT_DATA_VERDICT:
11982 return nft_verdict_uninit(data);
11983 default:
11984 WARN_ON(1);
11985 }
11986 }
11987
nft_data_dump(struct sk_buff * skb,int attr,const struct nft_data * data,enum nft_data_types type,unsigned int len)11988 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
11989 enum nft_data_types type, unsigned int len)
11990 {
11991 struct nlattr *nest;
11992 int err;
11993
11994 nest = nla_nest_start_noflag(skb, attr);
11995 if (nest == NULL)
11996 return -1;
11997
11998 switch (type) {
11999 case NFT_DATA_VALUE:
12000 err = nft_value_dump(skb, data, len);
12001 break;
12002 case NFT_DATA_VERDICT:
12003 err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
12004 break;
12005 default:
12006 err = -EINVAL;
12007 DEBUG_NET_WARN_ON_ONCE(1);
12008 }
12009
12010 nla_nest_end(skb, nest);
12011 return err;
12012 }
12013
__nft_release_hook(struct net * net,struct nft_table * table)12014 static void __nft_release_hook(struct net *net, struct nft_table *table)
12015 {
12016 struct nft_flowtable *flowtable;
12017 struct nft_chain *chain;
12018
12019 list_for_each_entry(chain, &table->chains, list)
12020 __nf_tables_unregister_hook(net, table, chain, true);
12021 list_for_each_entry(flowtable, &table->flowtables, list)
12022 __nft_unregister_flowtable_net_hooks(net, flowtable,
12023 &flowtable->hook_list,
12024 true);
12025 }
12026
__nft_release_hooks(struct net * net)12027 static void __nft_release_hooks(struct net *net)
12028 {
12029 struct nftables_pernet *nft_net = nft_pernet(net);
12030 struct nft_table *table;
12031
12032 list_for_each_entry(table, &nft_net->tables, list) {
12033 if (nft_table_has_owner(table))
12034 continue;
12035
12036 __nft_release_hook(net, table);
12037 }
12038 }
12039
__nft_release_table(struct net * net,struct nft_table * table)12040 static void __nft_release_table(struct net *net, struct nft_table *table)
12041 {
12042 struct nft_flowtable *flowtable, *nf;
12043 struct nft_chain *chain, *nc;
12044 struct nft_object *obj, *ne;
12045 struct nft_rule *rule, *nr;
12046 struct nft_set *set, *ns;
12047 struct nft_ctx ctx = {
12048 .net = net,
12049 .family = NFPROTO_NETDEV,
12050 };
12051
12052 ctx.family = table->family;
12053 ctx.table = table;
12054 list_for_each_entry(chain, &table->chains, list) {
12055 if (nft_chain_binding(chain))
12056 continue;
12057
12058 ctx.chain = chain;
12059 list_for_each_entry_safe(rule, nr, &chain->rules, list) {
12060 list_del(&rule->list);
12061 nft_use_dec(&chain->use);
12062 nf_tables_rule_release(&ctx, rule);
12063 }
12064 }
12065 list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) {
12066 list_del(&flowtable->list);
12067 nft_use_dec(&table->use);
12068 nf_tables_flowtable_destroy(flowtable);
12069 }
12070 list_for_each_entry_safe(set, ns, &table->sets, list) {
12071 list_del(&set->list);
12072 nft_use_dec(&table->use);
12073 if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT))
12074 nft_map_deactivate(&ctx, set);
12075
12076 nft_set_destroy(&ctx, set);
12077 }
12078 list_for_each_entry_safe(obj, ne, &table->objects, list) {
12079 nft_obj_del(table, obj);
12080 nft_use_dec(&table->use);
12081 nft_obj_destroy(&ctx, obj);
12082 }
12083 list_for_each_entry_safe(chain, nc, &table->chains, list) {
12084 nft_chain_del(chain);
12085 nft_use_dec(&table->use);
12086 nf_tables_chain_destroy(chain);
12087 }
12088 nf_tables_table_destroy(table);
12089 }
12090
__nft_release_tables(struct net * net)12091 static void __nft_release_tables(struct net *net)
12092 {
12093 struct nftables_pernet *nft_net = nft_pernet(net);
12094 struct nft_table *table, *nt;
12095
12096 list_for_each_entry_safe(table, nt, &nft_net->tables, list) {
12097 if (nft_table_has_owner(table))
12098 continue;
12099
12100 list_del(&table->list);
12101
12102 __nft_release_table(net, table);
12103 }
12104 }
12105
nft_rcv_nl_event(struct notifier_block * this,unsigned long event,void * ptr)12106 static int nft_rcv_nl_event(struct notifier_block *this, unsigned long event,
12107 void *ptr)
12108 {
12109 struct nft_table *table, *to_delete[8];
12110 struct nftables_pernet *nft_net;
12111 struct netlink_notify *n = ptr;
12112 struct net *net = n->net;
12113 unsigned int deleted;
12114 bool restart = false;
12115 unsigned int gc_seq;
12116
12117 if (event != NETLINK_URELEASE || n->protocol != NETLINK_NETFILTER)
12118 return NOTIFY_DONE;
12119
12120 nft_net = nft_pernet(net);
12121 deleted = 0;
12122 mutex_lock(&nft_net->commit_mutex);
12123
12124 gc_seq = nft_gc_seq_begin(nft_net);
12125
12126 nf_tables_trans_destroy_flush_work(net);
12127 again:
12128 list_for_each_entry(table, &nft_net->tables, list) {
12129 if (nft_table_has_owner(table) &&
12130 n->portid == table->nlpid) {
12131 if (table->flags & NFT_TABLE_F_PERSIST) {
12132 table->flags &= ~NFT_TABLE_F_OWNER;
12133 continue;
12134 }
12135 __nft_release_hook(net, table);
12136 list_del_rcu(&table->list);
12137 to_delete[deleted++] = table;
12138 if (deleted >= ARRAY_SIZE(to_delete))
12139 break;
12140 }
12141 }
12142 if (deleted) {
12143 restart = deleted >= ARRAY_SIZE(to_delete);
12144 synchronize_rcu();
12145 while (deleted)
12146 __nft_release_table(net, to_delete[--deleted]);
12147
12148 if (restart)
12149 goto again;
12150 }
12151 nft_gc_seq_end(nft_net, gc_seq);
12152
12153 mutex_unlock(&nft_net->commit_mutex);
12154
12155 return NOTIFY_DONE;
12156 }
12157
12158 static struct notifier_block nft_nl_notifier = {
12159 .notifier_call = nft_rcv_nl_event,
12160 };
12161
nf_tables_init_net(struct net * net)12162 static int __net_init nf_tables_init_net(struct net *net)
12163 {
12164 struct nftables_pernet *nft_net = nft_pernet(net);
12165
12166 INIT_LIST_HEAD(&nft_net->tables);
12167 INIT_LIST_HEAD(&nft_net->commit_list);
12168 INIT_LIST_HEAD(&nft_net->destroy_list);
12169 INIT_LIST_HEAD(&nft_net->commit_set_list);
12170 INIT_LIST_HEAD(&nft_net->binding_list);
12171 INIT_LIST_HEAD(&nft_net->module_list);
12172 INIT_LIST_HEAD(&nft_net->notify_list);
12173 INIT_LIST_HEAD(&nft_net->set_update_list);
12174 mutex_init(&nft_net->commit_mutex);
12175 net->nft.base_seq = 1;
12176 nft_net->gc_seq = 0;
12177 nft_net->validate_state = NFT_VALIDATE_SKIP;
12178 INIT_WORK(&nft_net->destroy_work, nf_tables_trans_destroy_work);
12179
12180 return 0;
12181 }
12182
nf_tables_pre_exit_net(struct net * net)12183 static void __net_exit nf_tables_pre_exit_net(struct net *net)
12184 {
12185 struct nftables_pernet *nft_net = nft_pernet(net);
12186
12187 mutex_lock(&nft_net->commit_mutex);
12188 __nft_release_hooks(net);
12189 mutex_unlock(&nft_net->commit_mutex);
12190 }
12191
nf_tables_exit_net(struct net * net)12192 static void __net_exit nf_tables_exit_net(struct net *net)
12193 {
12194 struct nftables_pernet *nft_net = nft_pernet(net);
12195 unsigned int gc_seq;
12196
12197 mutex_lock(&nft_net->commit_mutex);
12198
12199 gc_seq = nft_gc_seq_begin(nft_net);
12200
12201 WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
12202 WARN_ON_ONCE(!list_empty(&nft_net->commit_set_list));
12203
12204 if (!list_empty(&nft_net->module_list))
12205 nf_tables_module_autoload_cleanup(net);
12206
12207 cancel_work_sync(&nft_net->destroy_work);
12208 __nft_release_tables(net);
12209
12210 nft_gc_seq_end(nft_net, gc_seq);
12211
12212 mutex_unlock(&nft_net->commit_mutex);
12213
12214 WARN_ON_ONCE(!list_empty(&nft_net->tables));
12215 WARN_ON_ONCE(!list_empty(&nft_net->module_list));
12216 WARN_ON_ONCE(!list_empty(&nft_net->notify_list));
12217 WARN_ON_ONCE(!list_empty(&nft_net->destroy_list));
12218 WARN_ON_ONCE(!list_empty(&nft_net->set_update_list));
12219 }
12220
nf_tables_exit_batch(struct list_head * net_exit_list)12221 static void nf_tables_exit_batch(struct list_head *net_exit_list)
12222 {
12223 flush_work(&trans_gc_work);
12224 }
12225
12226 static struct pernet_operations nf_tables_net_ops = {
12227 .init = nf_tables_init_net,
12228 .pre_exit = nf_tables_pre_exit_net,
12229 .exit = nf_tables_exit_net,
12230 .exit_batch = nf_tables_exit_batch,
12231 .id = &nf_tables_net_id,
12232 .size = sizeof(struct nftables_pernet),
12233 };
12234
nf_tables_module_init(void)12235 static int __init nf_tables_module_init(void)
12236 {
12237 int err;
12238
12239 BUILD_BUG_ON(offsetof(struct nft_trans_table, nft_trans) != 0);
12240 BUILD_BUG_ON(offsetof(struct nft_trans_chain, nft_trans_binding.nft_trans) != 0);
12241 BUILD_BUG_ON(offsetof(struct nft_trans_rule, nft_trans) != 0);
12242 BUILD_BUG_ON(offsetof(struct nft_trans_set, nft_trans_binding.nft_trans) != 0);
12243 BUILD_BUG_ON(offsetof(struct nft_trans_elem, nft_trans) != 0);
12244 BUILD_BUG_ON(offsetof(struct nft_trans_obj, nft_trans) != 0);
12245 BUILD_BUG_ON(offsetof(struct nft_trans_flowtable, nft_trans) != 0);
12246
12247 err = register_pernet_subsys(&nf_tables_net_ops);
12248 if (err < 0)
12249 return err;
12250
12251 err = nft_chain_filter_init();
12252 if (err < 0)
12253 goto err_chain_filter;
12254
12255 err = nf_tables_core_module_init();
12256 if (err < 0)
12257 goto err_core_module;
12258
12259 err = register_netdevice_notifier(&nf_tables_flowtable_notifier);
12260 if (err < 0)
12261 goto err_netdev_notifier;
12262
12263 err = nft_offload_init();
12264 if (err < 0)
12265 goto err_offload;
12266
12267 err = netlink_register_notifier(&nft_nl_notifier);
12268 if (err < 0)
12269 goto err_netlink_notifier;
12270
12271 /* must be last */
12272 err = nfnetlink_subsys_register(&nf_tables_subsys);
12273 if (err < 0)
12274 goto err_nfnl_subsys;
12275
12276 nft_chain_route_init();
12277
12278 return err;
12279
12280 err_nfnl_subsys:
12281 netlink_unregister_notifier(&nft_nl_notifier);
12282 err_netlink_notifier:
12283 nft_offload_exit();
12284 err_offload:
12285 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
12286 err_netdev_notifier:
12287 nf_tables_core_module_exit();
12288 err_core_module:
12289 nft_chain_filter_fini();
12290 err_chain_filter:
12291 unregister_pernet_subsys(&nf_tables_net_ops);
12292 return err;
12293 }
12294
nf_tables_module_exit(void)12295 static void __exit nf_tables_module_exit(void)
12296 {
12297 nfnetlink_subsys_unregister(&nf_tables_subsys);
12298 netlink_unregister_notifier(&nft_nl_notifier);
12299 nft_offload_exit();
12300 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
12301 nft_chain_filter_fini();
12302 nft_chain_route_fini();
12303 unregister_pernet_subsys(&nf_tables_net_ops);
12304 cancel_work_sync(&trans_gc_work);
12305 rcu_barrier();
12306 nf_tables_core_module_exit();
12307 }
12308
12309 module_init(nf_tables_module_init);
12310 module_exit(nf_tables_module_exit);
12311
12312 MODULE_LICENSE("GPL");
12313 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
12314 MODULE_DESCRIPTION("Framework for packet filtering and classification");
12315 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);
12316