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