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