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