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 strscpy(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_table *table = ctx->table;
2201 struct nft_base_chain *basechain;
2202 struct net *net = ctx->net;
2203 char name[NFT_NAME_MAXLEN];
2204 struct nft_rule_blob *blob;
2205 struct nft_trans *trans;
2206 struct nft_chain *chain;
2207 unsigned int data_size;
2208 int err;
2209
2210 if (table->use == UINT_MAX)
2211 return -EOVERFLOW;
2212
2213 if (nla[NFTA_CHAIN_HOOK]) {
2214 struct nft_stats __percpu *stats = NULL;
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 free_percpu(stats);
2247 return err;
2248 }
2249 if (stats)
2250 static_branch_inc(&nft_counters_enabled);
2251 } else {
2252 if (flags & NFT_CHAIN_BASE)
2253 return -EINVAL;
2254 if (flags & NFT_CHAIN_HW_OFFLOAD)
2255 return -EOPNOTSUPP;
2256
2257 chain = kzalloc(sizeof(*chain), GFP_KERNEL_ACCOUNT);
2258 if (chain == NULL)
2259 return -ENOMEM;
2260
2261 chain->flags = flags;
2262 }
2263 ctx->chain = chain;
2264
2265 INIT_LIST_HEAD(&chain->rules);
2266 chain->handle = nf_tables_alloc_handle(table);
2267 chain->table = table;
2268
2269 if (nla[NFTA_CHAIN_NAME]) {
2270 chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT);
2271 } else {
2272 if (!(flags & NFT_CHAIN_BINDING)) {
2273 err = -EINVAL;
2274 goto err_destroy_chain;
2275 }
2276
2277 snprintf(name, sizeof(name), "__chain%llu", ++chain_id);
2278 chain->name = kstrdup(name, GFP_KERNEL_ACCOUNT);
2279 }
2280
2281 if (!chain->name) {
2282 err = -ENOMEM;
2283 goto err_destroy_chain;
2284 }
2285
2286 if (nla[NFTA_CHAIN_USERDATA]) {
2287 chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL_ACCOUNT);
2288 if (chain->udata == NULL) {
2289 err = -ENOMEM;
2290 goto err_destroy_chain;
2291 }
2292 chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]);
2293 }
2294
2295 data_size = offsetof(struct nft_rule_dp, data); /* last rule */
2296 blob = nf_tables_chain_alloc_rules(data_size);
2297 if (!blob) {
2298 err = -ENOMEM;
2299 goto err_destroy_chain;
2300 }
2301
2302 RCU_INIT_POINTER(chain->blob_gen_0, blob);
2303 RCU_INIT_POINTER(chain->blob_gen_1, blob);
2304
2305 err = nf_tables_register_hook(net, table, chain);
2306 if (err < 0)
2307 goto err_destroy_chain;
2308
2309 trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN);
2310 if (IS_ERR(trans)) {
2311 err = PTR_ERR(trans);
2312 goto err_unregister_hook;
2313 }
2314
2315 nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET;
2316 if (nft_is_base_chain(chain))
2317 nft_trans_chain_policy(trans) = policy;
2318
2319 err = nft_chain_add(table, chain);
2320 if (err < 0) {
2321 nft_trans_destroy(trans);
2322 goto err_unregister_hook;
2323 }
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_ext_check(const struct nft_set_ext_tmpl * tmpl,u8 id,u32 len)5482 static int nft_set_ext_check(const struct nft_set_ext_tmpl *tmpl, u8 id, u32 len)
5483 {
5484 len += nft_set_ext_types[id].len;
5485 if (len > tmpl->ext_len[id] ||
5486 len > U8_MAX)
5487 return -1;
5488
5489 return 0;
5490 }
5491
nft_set_ext_memcpy(const struct nft_set_ext_tmpl * tmpl,u8 id,void * to,const void * from,u32 len)5492 static int nft_set_ext_memcpy(const struct nft_set_ext_tmpl *tmpl, u8 id,
5493 void *to, const void *from, u32 len)
5494 {
5495 if (nft_set_ext_check(tmpl, id, len) < 0)
5496 return -1;
5497
5498 memcpy(to, from, len);
5499
5500 return 0;
5501 }
5502
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)5503 void *nft_set_elem_init(const struct nft_set *set,
5504 const struct nft_set_ext_tmpl *tmpl,
5505 const u32 *key, const u32 *key_end,
5506 const u32 *data, u64 timeout, u64 expiration, gfp_t gfp)
5507 {
5508 struct nft_set_ext *ext;
5509 void *elem;
5510
5511 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
5512 if (elem == NULL)
5513 return ERR_PTR(-ENOMEM);
5514
5515 ext = nft_set_elem_ext(set, elem);
5516 nft_set_ext_init(ext, tmpl);
5517
5518 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) &&
5519 nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY,
5520 nft_set_ext_key(ext), key, set->klen) < 0)
5521 goto err_ext_check;
5522
5523 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) &&
5524 nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY_END,
5525 nft_set_ext_key_end(ext), key_end, set->klen) < 0)
5526 goto err_ext_check;
5527
5528 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
5529 nft_set_ext_memcpy(tmpl, NFT_SET_EXT_DATA,
5530 nft_set_ext_data(ext), data, set->dlen) < 0)
5531 goto err_ext_check;
5532
5533 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
5534 *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration;
5535 if (expiration == 0)
5536 *nft_set_ext_expiration(ext) += timeout;
5537 }
5538 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
5539 *nft_set_ext_timeout(ext) = timeout;
5540
5541 return elem;
5542
5543 err_ext_check:
5544 kfree(elem);
5545
5546 return ERR_PTR(-EINVAL);
5547 }
5548
__nft_set_elem_expr_destroy(const struct nft_ctx * ctx,struct nft_expr * expr)5549 static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
5550 struct nft_expr *expr)
5551 {
5552 if (expr->ops->destroy_clone) {
5553 expr->ops->destroy_clone(ctx, expr);
5554 module_put(expr->ops->type->owner);
5555 } else {
5556 nf_tables_expr_destroy(ctx, expr);
5557 }
5558 }
5559
nft_set_elem_expr_destroy(const struct nft_ctx * ctx,struct nft_set_elem_expr * elem_expr)5560 static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
5561 struct nft_set_elem_expr *elem_expr)
5562 {
5563 struct nft_expr *expr;
5564 u32 size;
5565
5566 nft_setelem_expr_foreach(expr, elem_expr, size)
5567 __nft_set_elem_expr_destroy(ctx, expr);
5568 }
5569
nft_set_elem_destroy(const struct nft_set * set,void * elem,bool destroy_expr)5570 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
5571 bool destroy_expr)
5572 {
5573 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
5574 struct nft_ctx ctx = {
5575 .net = read_pnet(&set->net),
5576 .family = set->table->family,
5577 };
5578
5579 nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE);
5580 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
5581 nft_data_release(nft_set_ext_data(ext), set->dtype);
5582 if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
5583 nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext));
5584
5585 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
5586 (*nft_set_ext_obj(ext))->use--;
5587 kfree(elem);
5588 }
5589 EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
5590
5591 /* Only called from commit path, nft_setelem_data_deactivate() already deals
5592 * with the refcounting from the preparation phase.
5593 */
nf_tables_set_elem_destroy(const struct nft_ctx * ctx,const struct nft_set * set,void * elem)5594 static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
5595 const struct nft_set *set, void *elem)
5596 {
5597 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
5598
5599 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
5600 nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext));
5601
5602 kfree(elem);
5603 }
5604
nft_set_elem_expr_clone(const struct nft_ctx * ctx,struct nft_set * set,struct nft_expr * expr_array[])5605 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
5606 struct nft_expr *expr_array[])
5607 {
5608 struct nft_expr *expr;
5609 int err, i, k;
5610
5611 for (i = 0; i < set->num_exprs; i++) {
5612 expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL_ACCOUNT);
5613 if (!expr)
5614 goto err_expr;
5615
5616 err = nft_expr_clone(expr, set->exprs[i]);
5617 if (err < 0) {
5618 kfree(expr);
5619 goto err_expr;
5620 }
5621 expr_array[i] = expr;
5622 }
5623
5624 return 0;
5625
5626 err_expr:
5627 for (k = i - 1; k >= 0; k--)
5628 nft_expr_destroy(ctx, expr_array[k]);
5629
5630 return -ENOMEM;
5631 }
5632
nft_set_elem_expr_setup(struct nft_ctx * ctx,const struct nft_set_ext_tmpl * tmpl,const struct nft_set_ext * ext,struct nft_expr * expr_array[],u32 num_exprs)5633 static int nft_set_elem_expr_setup(struct nft_ctx *ctx,
5634 const struct nft_set_ext_tmpl *tmpl,
5635 const struct nft_set_ext *ext,
5636 struct nft_expr *expr_array[],
5637 u32 num_exprs)
5638 {
5639 struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext);
5640 u32 len = sizeof(struct nft_set_elem_expr);
5641 struct nft_expr *expr;
5642 int i, err;
5643
5644 if (num_exprs == 0)
5645 return 0;
5646
5647 for (i = 0; i < num_exprs; i++)
5648 len += expr_array[i]->ops->size;
5649
5650 if (nft_set_ext_check(tmpl, NFT_SET_EXT_EXPRESSIONS, len) < 0)
5651 return -EINVAL;
5652
5653 for (i = 0; i < num_exprs; i++) {
5654 expr = nft_setelem_expr_at(elem_expr, elem_expr->size);
5655 err = nft_expr_clone(expr, expr_array[i]);
5656 if (err < 0)
5657 goto err_elem_expr_setup;
5658
5659 elem_expr->size += expr_array[i]->ops->size;
5660 nft_expr_destroy(ctx, expr_array[i]);
5661 expr_array[i] = NULL;
5662 }
5663
5664 return 0;
5665
5666 err_elem_expr_setup:
5667 for (; i < num_exprs; i++) {
5668 nft_expr_destroy(ctx, expr_array[i]);
5669 expr_array[i] = NULL;
5670 }
5671
5672 return -ENOMEM;
5673 }
5674
nft_set_catchall_lookup(const struct net * net,const struct nft_set * set)5675 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
5676 const struct nft_set *set)
5677 {
5678 struct nft_set_elem_catchall *catchall;
5679 u8 genmask = nft_genmask_cur(net);
5680 struct nft_set_ext *ext;
5681
5682 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
5683 ext = nft_set_elem_ext(set, catchall->elem);
5684 if (nft_set_elem_active(ext, genmask) &&
5685 !nft_set_elem_expired(ext))
5686 return ext;
5687 }
5688
5689 return NULL;
5690 }
5691 EXPORT_SYMBOL_GPL(nft_set_catchall_lookup);
5692
nft_set_catchall_gc(const struct nft_set * set)5693 void *nft_set_catchall_gc(const struct nft_set *set)
5694 {
5695 struct nft_set_elem_catchall *catchall, *next;
5696 struct nft_set_ext *ext;
5697 void *elem = NULL;
5698
5699 list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
5700 ext = nft_set_elem_ext(set, catchall->elem);
5701
5702 if (!nft_set_elem_expired(ext) ||
5703 nft_set_elem_mark_busy(ext))
5704 continue;
5705
5706 elem = catchall->elem;
5707 list_del_rcu(&catchall->list);
5708 kfree_rcu(catchall, rcu);
5709 break;
5710 }
5711
5712 return elem;
5713 }
5714 EXPORT_SYMBOL_GPL(nft_set_catchall_gc);
5715
nft_setelem_catchall_insert(const struct net * net,struct nft_set * set,const struct nft_set_elem * elem,struct nft_set_ext ** pext)5716 static int nft_setelem_catchall_insert(const struct net *net,
5717 struct nft_set *set,
5718 const struct nft_set_elem *elem,
5719 struct nft_set_ext **pext)
5720 {
5721 struct nft_set_elem_catchall *catchall;
5722 u8 genmask = nft_genmask_next(net);
5723 struct nft_set_ext *ext;
5724
5725 list_for_each_entry(catchall, &set->catchall_list, list) {
5726 ext = nft_set_elem_ext(set, catchall->elem);
5727 if (nft_set_elem_active(ext, genmask)) {
5728 *pext = ext;
5729 return -EEXIST;
5730 }
5731 }
5732
5733 catchall = kmalloc(sizeof(*catchall), GFP_KERNEL);
5734 if (!catchall)
5735 return -ENOMEM;
5736
5737 catchall->elem = elem->priv;
5738 list_add_tail_rcu(&catchall->list, &set->catchall_list);
5739
5740 return 0;
5741 }
5742
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)5743 static int nft_setelem_insert(const struct net *net,
5744 struct nft_set *set,
5745 const struct nft_set_elem *elem,
5746 struct nft_set_ext **ext, unsigned int flags)
5747 {
5748 int ret;
5749
5750 if (flags & NFT_SET_ELEM_CATCHALL)
5751 ret = nft_setelem_catchall_insert(net, set, elem, ext);
5752 else
5753 ret = set->ops->insert(net, set, elem, ext);
5754
5755 return ret;
5756 }
5757
nft_setelem_is_catchall(const struct nft_set * set,const struct nft_set_elem * elem)5758 static bool nft_setelem_is_catchall(const struct nft_set *set,
5759 const struct nft_set_elem *elem)
5760 {
5761 struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5762
5763 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
5764 *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL)
5765 return true;
5766
5767 return false;
5768 }
5769
nft_setelem_activate(struct net * net,struct nft_set * set,struct nft_set_elem * elem)5770 static void nft_setelem_activate(struct net *net, struct nft_set *set,
5771 struct nft_set_elem *elem)
5772 {
5773 struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5774
5775 if (nft_setelem_is_catchall(set, elem)) {
5776 nft_set_elem_change_active(net, set, ext);
5777 nft_set_elem_clear_busy(ext);
5778 } else {
5779 set->ops->activate(net, set, elem);
5780 }
5781 }
5782
nft_setelem_catchall_deactivate(const struct net * net,struct nft_set * set,struct nft_set_elem * elem)5783 static int nft_setelem_catchall_deactivate(const struct net *net,
5784 struct nft_set *set,
5785 struct nft_set_elem *elem)
5786 {
5787 struct nft_set_elem_catchall *catchall;
5788 struct nft_set_ext *ext;
5789
5790 list_for_each_entry(catchall, &set->catchall_list, list) {
5791 ext = nft_set_elem_ext(set, catchall->elem);
5792 if (!nft_is_active(net, ext) ||
5793 nft_set_elem_mark_busy(ext))
5794 continue;
5795
5796 kfree(elem->priv);
5797 elem->priv = catchall->elem;
5798 nft_set_elem_change_active(net, set, ext);
5799 return 0;
5800 }
5801
5802 return -ENOENT;
5803 }
5804
__nft_setelem_deactivate(const struct net * net,struct nft_set * set,struct nft_set_elem * elem)5805 static int __nft_setelem_deactivate(const struct net *net,
5806 struct nft_set *set,
5807 struct nft_set_elem *elem)
5808 {
5809 void *priv;
5810
5811 priv = set->ops->deactivate(net, set, elem);
5812 if (!priv)
5813 return -ENOENT;
5814
5815 kfree(elem->priv);
5816 elem->priv = priv;
5817 set->ndeact++;
5818
5819 return 0;
5820 }
5821
nft_setelem_deactivate(const struct net * net,struct nft_set * set,struct nft_set_elem * elem,u32 flags)5822 static int nft_setelem_deactivate(const struct net *net,
5823 struct nft_set *set,
5824 struct nft_set_elem *elem, u32 flags)
5825 {
5826 int ret;
5827
5828 if (flags & NFT_SET_ELEM_CATCHALL)
5829 ret = nft_setelem_catchall_deactivate(net, set, elem);
5830 else
5831 ret = __nft_setelem_deactivate(net, set, elem);
5832
5833 return ret;
5834 }
5835
nft_setelem_catchall_remove(const struct net * net,const struct nft_set * set,const struct nft_set_elem * elem)5836 static void nft_setelem_catchall_remove(const struct net *net,
5837 const struct nft_set *set,
5838 const struct nft_set_elem *elem)
5839 {
5840 struct nft_set_elem_catchall *catchall, *next;
5841
5842 list_for_each_entry_safe(catchall, next, &set->catchall_list, list) {
5843 if (catchall->elem == elem->priv) {
5844 list_del_rcu(&catchall->list);
5845 kfree_rcu(catchall, rcu);
5846 break;
5847 }
5848 }
5849 }
5850
nft_setelem_remove(const struct net * net,const struct nft_set * set,const struct nft_set_elem * elem)5851 static void nft_setelem_remove(const struct net *net,
5852 const struct nft_set *set,
5853 const struct nft_set_elem *elem)
5854 {
5855 if (nft_setelem_is_catchall(set, elem))
5856 nft_setelem_catchall_remove(net, set, elem);
5857 else
5858 set->ops->remove(net, set, elem);
5859 }
5860
nft_setelem_valid_key_end(const struct nft_set * set,struct nlattr ** nla,u32 flags)5861 static bool nft_setelem_valid_key_end(const struct nft_set *set,
5862 struct nlattr **nla, u32 flags)
5863 {
5864 if ((set->flags & (NFT_SET_CONCAT | NFT_SET_INTERVAL)) ==
5865 (NFT_SET_CONCAT | NFT_SET_INTERVAL)) {
5866 if (flags & NFT_SET_ELEM_INTERVAL_END)
5867 return false;
5868
5869 if (nla[NFTA_SET_ELEM_KEY_END] &&
5870 flags & NFT_SET_ELEM_CATCHALL)
5871 return false;
5872 } else {
5873 if (nla[NFTA_SET_ELEM_KEY_END])
5874 return false;
5875 }
5876
5877 return true;
5878 }
5879
nft_add_set_elem(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * attr,u32 nlmsg_flags)5880 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
5881 const struct nlattr *attr, u32 nlmsg_flags)
5882 {
5883 struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {};
5884 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
5885 u8 genmask = nft_genmask_next(ctx->net);
5886 u32 flags = 0, size = 0, num_exprs = 0;
5887 struct nft_set_ext_tmpl tmpl;
5888 struct nft_set_ext *ext, *ext2;
5889 struct nft_set_elem elem;
5890 struct nft_set_binding *binding;
5891 struct nft_object *obj = NULL;
5892 struct nft_userdata *udata;
5893 struct nft_data_desc desc;
5894 enum nft_registers dreg;
5895 struct nft_trans *trans;
5896 u64 timeout;
5897 u64 expiration;
5898 int err, i;
5899 u8 ulen;
5900
5901 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
5902 nft_set_elem_policy, NULL);
5903 if (err < 0)
5904 return err;
5905
5906 nft_set_ext_prepare(&tmpl);
5907
5908 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
5909 if (err < 0)
5910 return err;
5911
5912 if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL))
5913 return -EINVAL;
5914
5915 if (flags != 0) {
5916 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
5917 if (err < 0)
5918 return err;
5919 }
5920
5921 if (set->flags & NFT_SET_MAP) {
5922 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
5923 !(flags & NFT_SET_ELEM_INTERVAL_END))
5924 return -EINVAL;
5925 } else {
5926 if (nla[NFTA_SET_ELEM_DATA] != NULL)
5927 return -EINVAL;
5928 }
5929
5930 if (set->flags & NFT_SET_OBJECT) {
5931 if (!nla[NFTA_SET_ELEM_OBJREF] &&
5932 !(flags & NFT_SET_ELEM_INTERVAL_END))
5933 return -EINVAL;
5934 } else {
5935 if (nla[NFTA_SET_ELEM_OBJREF])
5936 return -EINVAL;
5937 }
5938
5939 if (!nft_setelem_valid_key_end(set, nla, flags))
5940 return -EINVAL;
5941
5942 if ((flags & NFT_SET_ELEM_INTERVAL_END) &&
5943 (nla[NFTA_SET_ELEM_DATA] ||
5944 nla[NFTA_SET_ELEM_OBJREF] ||
5945 nla[NFTA_SET_ELEM_TIMEOUT] ||
5946 nla[NFTA_SET_ELEM_EXPIRATION] ||
5947 nla[NFTA_SET_ELEM_USERDATA] ||
5948 nla[NFTA_SET_ELEM_EXPR] ||
5949 nla[NFTA_SET_ELEM_KEY_END] ||
5950 nla[NFTA_SET_ELEM_EXPRESSIONS]))
5951 return -EINVAL;
5952
5953 timeout = 0;
5954 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
5955 if (!(set->flags & NFT_SET_TIMEOUT))
5956 return -EINVAL;
5957 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT],
5958 &timeout);
5959 if (err)
5960 return err;
5961 } else if (set->flags & NFT_SET_TIMEOUT &&
5962 !(flags & NFT_SET_ELEM_INTERVAL_END)) {
5963 timeout = set->timeout;
5964 }
5965
5966 expiration = 0;
5967 if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) {
5968 if (!(set->flags & NFT_SET_TIMEOUT))
5969 return -EINVAL;
5970 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION],
5971 &expiration);
5972 if (err)
5973 return err;
5974 }
5975
5976 if (nla[NFTA_SET_ELEM_EXPR]) {
5977 struct nft_expr *expr;
5978
5979 if (set->num_exprs && set->num_exprs != 1)
5980 return -EOPNOTSUPP;
5981
5982 expr = nft_set_elem_expr_alloc(ctx, set,
5983 nla[NFTA_SET_ELEM_EXPR]);
5984 if (IS_ERR(expr))
5985 return PTR_ERR(expr);
5986
5987 expr_array[0] = expr;
5988 num_exprs = 1;
5989
5990 if (set->num_exprs && set->exprs[0]->ops != expr->ops) {
5991 err = -EOPNOTSUPP;
5992 goto err_set_elem_expr;
5993 }
5994 } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) {
5995 struct nft_expr *expr;
5996 struct nlattr *tmp;
5997 int left;
5998
5999 i = 0;
6000 nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) {
6001 if (i == NFT_SET_EXPR_MAX ||
6002 (set->num_exprs && set->num_exprs == i)) {
6003 err = -E2BIG;
6004 goto err_set_elem_expr;
6005 }
6006 if (nla_type(tmp) != NFTA_LIST_ELEM) {
6007 err = -EINVAL;
6008 goto err_set_elem_expr;
6009 }
6010 expr = nft_set_elem_expr_alloc(ctx, set, tmp);
6011 if (IS_ERR(expr)) {
6012 err = PTR_ERR(expr);
6013 goto err_set_elem_expr;
6014 }
6015 expr_array[i] = expr;
6016 num_exprs++;
6017
6018 if (set->num_exprs && expr->ops != set->exprs[i]->ops) {
6019 err = -EOPNOTSUPP;
6020 goto err_set_elem_expr;
6021 }
6022 i++;
6023 }
6024 if (set->num_exprs && set->num_exprs != i) {
6025 err = -EOPNOTSUPP;
6026 goto err_set_elem_expr;
6027 }
6028 } else if (set->num_exprs > 0 &&
6029 !(flags & NFT_SET_ELEM_INTERVAL_END)) {
6030 err = nft_set_elem_expr_clone(ctx, set, expr_array);
6031 if (err < 0)
6032 goto err_set_elem_expr_clone;
6033
6034 num_exprs = set->num_exprs;
6035 }
6036
6037 if (nla[NFTA_SET_ELEM_KEY]) {
6038 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
6039 nla[NFTA_SET_ELEM_KEY]);
6040 if (err < 0)
6041 goto err_set_elem_expr;
6042
6043 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
6044 if (err < 0)
6045 goto err_parse_key;
6046 }
6047
6048 if (nla[NFTA_SET_ELEM_KEY_END]) {
6049 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
6050 nla[NFTA_SET_ELEM_KEY_END]);
6051 if (err < 0)
6052 goto err_parse_key;
6053
6054 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
6055 if (err < 0)
6056 goto err_parse_key_end;
6057 }
6058
6059 if (timeout > 0) {
6060 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
6061 if (err < 0)
6062 goto err_parse_key_end;
6063
6064 if (timeout != set->timeout) {
6065 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
6066 if (err < 0)
6067 goto err_parse_key_end;
6068 }
6069 }
6070
6071 if (num_exprs) {
6072 for (i = 0; i < num_exprs; i++)
6073 size += expr_array[i]->ops->size;
6074
6075 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS,
6076 sizeof(struct nft_set_elem_expr) + size);
6077 if (err < 0)
6078 goto err_parse_key_end;
6079 }
6080
6081 if (nla[NFTA_SET_ELEM_OBJREF] != NULL) {
6082 obj = nft_obj_lookup(ctx->net, ctx->table,
6083 nla[NFTA_SET_ELEM_OBJREF],
6084 set->objtype, genmask);
6085 if (IS_ERR(obj)) {
6086 err = PTR_ERR(obj);
6087 goto err_parse_key_end;
6088 }
6089 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF);
6090 if (err < 0)
6091 goto err_parse_key_end;
6092 }
6093
6094 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
6095 err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val,
6096 nla[NFTA_SET_ELEM_DATA]);
6097 if (err < 0)
6098 goto err_parse_key_end;
6099
6100 dreg = nft_type_to_reg(set->dtype);
6101 list_for_each_entry(binding, &set->bindings, list) {
6102 struct nft_ctx bind_ctx = {
6103 .net = ctx->net,
6104 .family = ctx->family,
6105 .table = ctx->table,
6106 .chain = (struct nft_chain *)binding->chain,
6107 };
6108
6109 if (!(binding->flags & NFT_SET_MAP))
6110 continue;
6111
6112 err = nft_validate_register_store(&bind_ctx, dreg,
6113 &elem.data.val,
6114 desc.type, desc.len);
6115 if (err < 0)
6116 goto err_parse_data;
6117
6118 if (desc.type == NFT_DATA_VERDICT &&
6119 (elem.data.val.verdict.code == NFT_GOTO ||
6120 elem.data.val.verdict.code == NFT_JUMP))
6121 nft_validate_state_update(ctx->net,
6122 NFT_VALIDATE_NEED);
6123 }
6124
6125 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len);
6126 if (err < 0)
6127 goto err_parse_data;
6128 }
6129
6130 /* The full maximum length of userdata can exceed the maximum
6131 * offset value (U8_MAX) for following extensions, therefor it
6132 * must be the last extension added.
6133 */
6134 ulen = 0;
6135 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
6136 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
6137 if (ulen > 0) {
6138 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
6139 ulen);
6140 if (err < 0)
6141 goto err_parse_data;
6142 }
6143 }
6144
6145 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
6146 elem.key_end.val.data, elem.data.val.data,
6147 timeout, expiration, GFP_KERNEL_ACCOUNT);
6148 if (IS_ERR(elem.priv)) {
6149 err = PTR_ERR(elem.priv);
6150 goto err_parse_data;
6151 }
6152
6153 ext = nft_set_elem_ext(set, elem.priv);
6154 if (flags)
6155 *nft_set_ext_flags(ext) = flags;
6156
6157 if (ulen > 0) {
6158 if (nft_set_ext_check(&tmpl, NFT_SET_EXT_USERDATA, ulen) < 0) {
6159 err = -EINVAL;
6160 goto err_elem_userdata;
6161 }
6162 udata = nft_set_ext_userdata(ext);
6163 udata->len = ulen - 1;
6164 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
6165 }
6166 if (obj) {
6167 *nft_set_ext_obj(ext) = obj;
6168 obj->use++;
6169 }
6170 err = nft_set_elem_expr_setup(ctx, &tmpl, ext, expr_array, num_exprs);
6171 if (err < 0)
6172 goto err_elem_free;
6173
6174 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
6175 if (trans == NULL) {
6176 err = -ENOMEM;
6177 goto err_elem_free;
6178 }
6179
6180 ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
6181
6182 err = nft_setelem_insert(ctx->net, set, &elem, &ext2, flags);
6183 if (err) {
6184 if (err == -EEXIST) {
6185 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^
6186 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) ||
6187 nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^
6188 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF))
6189 goto err_element_clash;
6190 if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
6191 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) &&
6192 memcmp(nft_set_ext_data(ext),
6193 nft_set_ext_data(ext2), set->dlen) != 0) ||
6194 (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
6195 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) &&
6196 *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2)))
6197 goto err_element_clash;
6198 else if (!(nlmsg_flags & NLM_F_EXCL))
6199 err = 0;
6200 } else if (err == -ENOTEMPTY) {
6201 /* ENOTEMPTY reports overlapping between this element
6202 * and an existing one.
6203 */
6204 err = -EEXIST;
6205 }
6206 goto err_element_clash;
6207 }
6208
6209 if (!(flags & NFT_SET_ELEM_CATCHALL) && set->size &&
6210 !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) {
6211 err = -ENFILE;
6212 goto err_set_full;
6213 }
6214
6215 nft_trans_elem(trans) = elem;
6216 nft_trans_commit_list_add_tail(ctx->net, trans);
6217 return 0;
6218
6219 err_set_full:
6220 nft_setelem_remove(ctx->net, set, &elem);
6221 err_element_clash:
6222 kfree(trans);
6223 err_elem_free:
6224 if (obj)
6225 obj->use--;
6226 err_elem_userdata:
6227 nf_tables_set_elem_destroy(ctx, set, elem.priv);
6228 err_parse_data:
6229 if (nla[NFTA_SET_ELEM_DATA] != NULL)
6230 nft_data_release(&elem.data.val, desc.type);
6231 err_parse_key_end:
6232 nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
6233 err_parse_key:
6234 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
6235 err_set_elem_expr:
6236 for (i = 0; i < num_exprs && expr_array[i]; i++)
6237 nft_expr_destroy(ctx, expr_array[i]);
6238 err_set_elem_expr_clone:
6239 return err;
6240 }
6241
nf_tables_newsetelem(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])6242 static int nf_tables_newsetelem(struct sk_buff *skb,
6243 const struct nfnl_info *info,
6244 const struct nlattr * const nla[])
6245 {
6246 struct nftables_pernet *nft_net = nft_pernet(info->net);
6247 struct netlink_ext_ack *extack = info->extack;
6248 u8 genmask = nft_genmask_next(info->net);
6249 u8 family = info->nfmsg->nfgen_family;
6250 struct net *net = info->net;
6251 const struct nlattr *attr;
6252 struct nft_table *table;
6253 struct nft_set *set;
6254 struct nft_ctx ctx;
6255 int rem, err;
6256
6257 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
6258 return -EINVAL;
6259
6260 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
6261 genmask, NETLINK_CB(skb).portid);
6262 if (IS_ERR(table)) {
6263 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
6264 return PTR_ERR(table);
6265 }
6266
6267 set = nft_set_lookup_global(net, table, nla[NFTA_SET_ELEM_LIST_SET],
6268 nla[NFTA_SET_ELEM_LIST_SET_ID], genmask);
6269 if (IS_ERR(set))
6270 return PTR_ERR(set);
6271
6272 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
6273 return -EBUSY;
6274
6275 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
6276
6277 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
6278 err = nft_add_set_elem(&ctx, set, attr, info->nlh->nlmsg_flags);
6279 if (err < 0) {
6280 NL_SET_BAD_ATTR(extack, attr);
6281 return err;
6282 }
6283 }
6284
6285 if (nft_net->validate_state == NFT_VALIDATE_DO)
6286 return nft_table_validate(net, table);
6287
6288 return 0;
6289 }
6290
6291 /**
6292 * nft_data_hold - hold a nft_data item
6293 *
6294 * @data: struct nft_data to release
6295 * @type: type of data
6296 *
6297 * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded,
6298 * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and
6299 * NFT_GOTO verdicts. This function must be called on active data objects
6300 * from the second phase of the commit protocol.
6301 */
nft_data_hold(const struct nft_data * data,enum nft_data_types type)6302 void nft_data_hold(const struct nft_data *data, enum nft_data_types type)
6303 {
6304 struct nft_chain *chain;
6305 struct nft_rule *rule;
6306
6307 if (type == NFT_DATA_VERDICT) {
6308 switch (data->verdict.code) {
6309 case NFT_JUMP:
6310 case NFT_GOTO:
6311 chain = data->verdict.chain;
6312 chain->use++;
6313
6314 if (!nft_chain_is_bound(chain))
6315 break;
6316
6317 chain->table->use++;
6318 list_for_each_entry(rule, &chain->rules, list)
6319 chain->use++;
6320
6321 nft_chain_add(chain->table, chain);
6322 break;
6323 }
6324 }
6325 }
6326
nft_setelem_data_activate(const struct net * net,const struct nft_set * set,struct nft_set_elem * elem)6327 static void nft_setelem_data_activate(const struct net *net,
6328 const struct nft_set *set,
6329 struct nft_set_elem *elem)
6330 {
6331 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
6332
6333 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
6334 nft_data_hold(nft_set_ext_data(ext), set->dtype);
6335 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
6336 (*nft_set_ext_obj(ext))->use++;
6337 }
6338
nft_setelem_data_deactivate(const struct net * net,const struct nft_set * set,struct nft_set_elem * elem)6339 static void nft_setelem_data_deactivate(const struct net *net,
6340 const struct nft_set *set,
6341 struct nft_set_elem *elem)
6342 {
6343 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
6344
6345 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
6346 nft_data_release(nft_set_ext_data(ext), set->dtype);
6347 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
6348 (*nft_set_ext_obj(ext))->use--;
6349 }
6350
nft_del_setelem(struct nft_ctx * ctx,struct nft_set * set,const struct nlattr * attr)6351 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
6352 const struct nlattr *attr)
6353 {
6354 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
6355 struct nft_set_ext_tmpl tmpl;
6356 struct nft_set_elem elem;
6357 struct nft_set_ext *ext;
6358 struct nft_trans *trans;
6359 u32 flags = 0;
6360 int err;
6361
6362 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
6363 nft_set_elem_policy, NULL);
6364 if (err < 0)
6365 return err;
6366
6367 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
6368 if (err < 0)
6369 return err;
6370
6371 if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL))
6372 return -EINVAL;
6373
6374 if (!nft_setelem_valid_key_end(set, nla, flags))
6375 return -EINVAL;
6376
6377 nft_set_ext_prepare(&tmpl);
6378
6379 if (flags != 0) {
6380 err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
6381 if (err < 0)
6382 return err;
6383 }
6384
6385 if (nla[NFTA_SET_ELEM_KEY]) {
6386 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
6387 nla[NFTA_SET_ELEM_KEY]);
6388 if (err < 0)
6389 return err;
6390
6391 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
6392 if (err < 0)
6393 goto fail_elem;
6394 }
6395
6396 if (nla[NFTA_SET_ELEM_KEY_END]) {
6397 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
6398 nla[NFTA_SET_ELEM_KEY_END]);
6399 if (err < 0)
6400 goto fail_elem;
6401
6402 err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
6403 if (err < 0)
6404 goto fail_elem_key_end;
6405 }
6406
6407 err = -ENOMEM;
6408 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
6409 elem.key_end.val.data, NULL, 0, 0,
6410 GFP_KERNEL_ACCOUNT);
6411 if (IS_ERR(elem.priv)) {
6412 err = PTR_ERR(elem.priv);
6413 goto fail_elem_key_end;
6414 }
6415
6416 ext = nft_set_elem_ext(set, elem.priv);
6417 if (flags)
6418 *nft_set_ext_flags(ext) = flags;
6419
6420 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
6421 if (trans == NULL)
6422 goto fail_trans;
6423
6424 err = nft_setelem_deactivate(ctx->net, set, &elem, flags);
6425 if (err < 0)
6426 goto fail_ops;
6427
6428 nft_setelem_data_deactivate(ctx->net, set, &elem);
6429
6430 nft_trans_elem(trans) = elem;
6431 nft_trans_commit_list_add_tail(ctx->net, trans);
6432 return 0;
6433
6434 fail_ops:
6435 kfree(trans);
6436 fail_trans:
6437 kfree(elem.priv);
6438 fail_elem_key_end:
6439 nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
6440 fail_elem:
6441 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
6442 return err;
6443 }
6444
nft_setelem_flush(const struct nft_ctx * ctx,struct nft_set * set,const struct nft_set_iter * iter,struct nft_set_elem * elem)6445 static int nft_setelem_flush(const struct nft_ctx *ctx,
6446 struct nft_set *set,
6447 const struct nft_set_iter *iter,
6448 struct nft_set_elem *elem)
6449 {
6450 struct nft_trans *trans;
6451 int err;
6452
6453 trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
6454 sizeof(struct nft_trans_elem), GFP_ATOMIC);
6455 if (!trans)
6456 return -ENOMEM;
6457
6458 if (!set->ops->flush(ctx->net, set, elem->priv)) {
6459 err = -ENOENT;
6460 goto err1;
6461 }
6462 set->ndeact++;
6463
6464 nft_setelem_data_deactivate(ctx->net, set, elem);
6465 nft_trans_elem_set(trans) = set;
6466 nft_trans_elem(trans) = *elem;
6467 nft_trans_commit_list_add_tail(ctx->net, trans);
6468
6469 return 0;
6470 err1:
6471 kfree(trans);
6472 return err;
6473 }
6474
__nft_set_catchall_flush(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_elem * elem)6475 static int __nft_set_catchall_flush(const struct nft_ctx *ctx,
6476 struct nft_set *set,
6477 struct nft_set_elem *elem)
6478 {
6479 struct nft_trans *trans;
6480
6481 trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
6482 sizeof(struct nft_trans_elem), GFP_KERNEL);
6483 if (!trans)
6484 return -ENOMEM;
6485
6486 nft_setelem_data_deactivate(ctx->net, set, elem);
6487 nft_trans_elem_set(trans) = set;
6488 nft_trans_elem(trans) = *elem;
6489 nft_trans_commit_list_add_tail(ctx->net, trans);
6490
6491 return 0;
6492 }
6493
nft_set_catchall_flush(const struct nft_ctx * ctx,struct nft_set * set)6494 static int nft_set_catchall_flush(const struct nft_ctx *ctx,
6495 struct nft_set *set)
6496 {
6497 u8 genmask = nft_genmask_next(ctx->net);
6498 struct nft_set_elem_catchall *catchall;
6499 struct nft_set_elem elem;
6500 struct nft_set_ext *ext;
6501 int ret = 0;
6502
6503 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
6504 ext = nft_set_elem_ext(set, catchall->elem);
6505 if (!nft_set_elem_active(ext, genmask) ||
6506 nft_set_elem_mark_busy(ext))
6507 continue;
6508
6509 elem.priv = catchall->elem;
6510 ret = __nft_set_catchall_flush(ctx, set, &elem);
6511 if (ret < 0)
6512 break;
6513 }
6514
6515 return ret;
6516 }
6517
nft_set_flush(struct nft_ctx * ctx,struct nft_set * set,u8 genmask)6518 static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask)
6519 {
6520 struct nft_set_iter iter = {
6521 .genmask = genmask,
6522 .fn = nft_setelem_flush,
6523 };
6524
6525 set->ops->walk(ctx, set, &iter);
6526 if (!iter.err)
6527 iter.err = nft_set_catchall_flush(ctx, set);
6528
6529 return iter.err;
6530 }
6531
nf_tables_delsetelem(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])6532 static int nf_tables_delsetelem(struct sk_buff *skb,
6533 const struct nfnl_info *info,
6534 const struct nlattr * const nla[])
6535 {
6536 struct netlink_ext_ack *extack = info->extack;
6537 u8 genmask = nft_genmask_next(info->net);
6538 u8 family = info->nfmsg->nfgen_family;
6539 struct net *net = info->net;
6540 const struct nlattr *attr;
6541 struct nft_table *table;
6542 struct nft_set *set;
6543 struct nft_ctx ctx;
6544 int rem, err = 0;
6545
6546 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
6547 genmask, NETLINK_CB(skb).portid);
6548 if (IS_ERR(table)) {
6549 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
6550 return PTR_ERR(table);
6551 }
6552
6553 set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
6554 if (IS_ERR(set))
6555 return PTR_ERR(set);
6556 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
6557 return -EBUSY;
6558
6559 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
6560
6561 if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
6562 return nft_set_flush(&ctx, set, genmask);
6563
6564 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
6565 err = nft_del_setelem(&ctx, set, attr);
6566 if (err < 0) {
6567 NL_SET_BAD_ATTR(extack, attr);
6568 break;
6569 }
6570 }
6571 return err;
6572 }
6573
nft_set_gc_batch_release(struct rcu_head * rcu)6574 void nft_set_gc_batch_release(struct rcu_head *rcu)
6575 {
6576 struct nft_set_gc_batch *gcb;
6577 unsigned int i;
6578
6579 gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu);
6580 for (i = 0; i < gcb->head.cnt; i++)
6581 nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true);
6582 kfree(gcb);
6583 }
6584
nft_set_gc_batch_alloc(const struct nft_set * set,gfp_t gfp)6585 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
6586 gfp_t gfp)
6587 {
6588 struct nft_set_gc_batch *gcb;
6589
6590 gcb = kzalloc(sizeof(*gcb), gfp);
6591 if (gcb == NULL)
6592 return gcb;
6593 gcb->head.set = set;
6594 return gcb;
6595 }
6596
6597 /*
6598 * Stateful objects
6599 */
6600
6601 /**
6602 * nft_register_obj- register nf_tables stateful object type
6603 * @obj_type: object type
6604 *
6605 * Registers the object type for use with nf_tables. Returns zero on
6606 * success or a negative errno code otherwise.
6607 */
nft_register_obj(struct nft_object_type * obj_type)6608 int nft_register_obj(struct nft_object_type *obj_type)
6609 {
6610 if (obj_type->type == NFT_OBJECT_UNSPEC)
6611 return -EINVAL;
6612
6613 nfnl_lock(NFNL_SUBSYS_NFTABLES);
6614 list_add_rcu(&obj_type->list, &nf_tables_objects);
6615 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
6616 return 0;
6617 }
6618 EXPORT_SYMBOL_GPL(nft_register_obj);
6619
6620 /**
6621 * nft_unregister_obj - unregister nf_tables object type
6622 * @obj_type: object type
6623 *
6624 * Unregisters the object type for use with nf_tables.
6625 */
nft_unregister_obj(struct nft_object_type * obj_type)6626 void nft_unregister_obj(struct nft_object_type *obj_type)
6627 {
6628 nfnl_lock(NFNL_SUBSYS_NFTABLES);
6629 list_del_rcu(&obj_type->list);
6630 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
6631 }
6632 EXPORT_SYMBOL_GPL(nft_unregister_obj);
6633
nft_obj_lookup(const struct net * net,const struct nft_table * table,const struct nlattr * nla,u32 objtype,u8 genmask)6634 struct nft_object *nft_obj_lookup(const struct net *net,
6635 const struct nft_table *table,
6636 const struct nlattr *nla, u32 objtype,
6637 u8 genmask)
6638 {
6639 struct nft_object_hash_key k = { .table = table };
6640 char search[NFT_OBJ_MAXNAMELEN];
6641 struct rhlist_head *tmp, *list;
6642 struct nft_object *obj;
6643
6644 nla_strscpy(search, nla, sizeof(search));
6645 k.name = search;
6646
6647 WARN_ON_ONCE(!rcu_read_lock_held() &&
6648 !lockdep_commit_lock_is_held(net));
6649
6650 rcu_read_lock();
6651 list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params);
6652 if (!list)
6653 goto out;
6654
6655 rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) {
6656 if (objtype == obj->ops->type->type &&
6657 nft_active_genmask(obj, genmask)) {
6658 rcu_read_unlock();
6659 return obj;
6660 }
6661 }
6662 out:
6663 rcu_read_unlock();
6664 return ERR_PTR(-ENOENT);
6665 }
6666 EXPORT_SYMBOL_GPL(nft_obj_lookup);
6667
nft_obj_lookup_byhandle(const struct nft_table * table,const struct nlattr * nla,u32 objtype,u8 genmask)6668 static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table,
6669 const struct nlattr *nla,
6670 u32 objtype, u8 genmask)
6671 {
6672 struct nft_object *obj;
6673
6674 list_for_each_entry(obj, &table->objects, list) {
6675 if (be64_to_cpu(nla_get_be64(nla)) == obj->handle &&
6676 objtype == obj->ops->type->type &&
6677 nft_active_genmask(obj, genmask))
6678 return obj;
6679 }
6680 return ERR_PTR(-ENOENT);
6681 }
6682
6683 static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = {
6684 [NFTA_OBJ_TABLE] = { .type = NLA_STRING,
6685 .len = NFT_TABLE_MAXNAMELEN - 1 },
6686 [NFTA_OBJ_NAME] = { .type = NLA_STRING,
6687 .len = NFT_OBJ_MAXNAMELEN - 1 },
6688 [NFTA_OBJ_TYPE] = { .type = NLA_U32 },
6689 [NFTA_OBJ_DATA] = { .type = NLA_NESTED },
6690 [NFTA_OBJ_HANDLE] = { .type = NLA_U64},
6691 [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY,
6692 .len = NFT_USERDATA_MAXLEN },
6693 };
6694
nft_obj_init(const struct nft_ctx * ctx,const struct nft_object_type * type,const struct nlattr * attr)6695 static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
6696 const struct nft_object_type *type,
6697 const struct nlattr *attr)
6698 {
6699 struct nlattr **tb;
6700 const struct nft_object_ops *ops;
6701 struct nft_object *obj;
6702 int err = -ENOMEM;
6703
6704 tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL);
6705 if (!tb)
6706 goto err1;
6707
6708 if (attr) {
6709 err = nla_parse_nested_deprecated(tb, type->maxattr, attr,
6710 type->policy, NULL);
6711 if (err < 0)
6712 goto err2;
6713 } else {
6714 memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1));
6715 }
6716
6717 if (type->select_ops) {
6718 ops = type->select_ops(ctx, (const struct nlattr * const *)tb);
6719 if (IS_ERR(ops)) {
6720 err = PTR_ERR(ops);
6721 goto err2;
6722 }
6723 } else {
6724 ops = type->ops;
6725 }
6726
6727 err = -ENOMEM;
6728 obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL_ACCOUNT);
6729 if (!obj)
6730 goto err2;
6731
6732 err = ops->init(ctx, (const struct nlattr * const *)tb, obj);
6733 if (err < 0)
6734 goto err3;
6735
6736 obj->ops = ops;
6737
6738 kfree(tb);
6739 return obj;
6740 err3:
6741 kfree(obj);
6742 err2:
6743 kfree(tb);
6744 err1:
6745 return ERR_PTR(err);
6746 }
6747
nft_object_dump(struct sk_buff * skb,unsigned int attr,struct nft_object * obj,bool reset)6748 static int nft_object_dump(struct sk_buff *skb, unsigned int attr,
6749 struct nft_object *obj, bool reset)
6750 {
6751 struct nlattr *nest;
6752
6753 nest = nla_nest_start_noflag(skb, attr);
6754 if (!nest)
6755 goto nla_put_failure;
6756 if (obj->ops->dump(skb, obj, reset) < 0)
6757 goto nla_put_failure;
6758 nla_nest_end(skb, nest);
6759 return 0;
6760
6761 nla_put_failure:
6762 return -1;
6763 }
6764
__nft_obj_type_get(u32 objtype)6765 static const struct nft_object_type *__nft_obj_type_get(u32 objtype)
6766 {
6767 const struct nft_object_type *type;
6768
6769 list_for_each_entry(type, &nf_tables_objects, list) {
6770 if (objtype == type->type)
6771 return type;
6772 }
6773 return NULL;
6774 }
6775
6776 static const struct nft_object_type *
nft_obj_type_get(struct net * net,u32 objtype)6777 nft_obj_type_get(struct net *net, u32 objtype)
6778 {
6779 const struct nft_object_type *type;
6780
6781 type = __nft_obj_type_get(objtype);
6782 if (type != NULL && try_module_get(type->owner))
6783 return type;
6784
6785 lockdep_nfnl_nft_mutex_not_held();
6786 #ifdef CONFIG_MODULES
6787 if (type == NULL) {
6788 if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN)
6789 return ERR_PTR(-EAGAIN);
6790 }
6791 #endif
6792 return ERR_PTR(-ENOENT);
6793 }
6794
nf_tables_updobj(const struct nft_ctx * ctx,const struct nft_object_type * type,const struct nlattr * attr,struct nft_object * obj)6795 static int nf_tables_updobj(const struct nft_ctx *ctx,
6796 const struct nft_object_type *type,
6797 const struct nlattr *attr,
6798 struct nft_object *obj)
6799 {
6800 struct nft_object *newobj;
6801 struct nft_trans *trans;
6802 int err = -ENOMEM;
6803
6804 if (!try_module_get(type->owner))
6805 return -ENOENT;
6806
6807 trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ,
6808 sizeof(struct nft_trans_obj));
6809 if (!trans)
6810 goto err_trans;
6811
6812 newobj = nft_obj_init(ctx, type, attr);
6813 if (IS_ERR(newobj)) {
6814 err = PTR_ERR(newobj);
6815 goto err_free_trans;
6816 }
6817
6818 nft_trans_obj(trans) = obj;
6819 nft_trans_obj_update(trans) = true;
6820 nft_trans_obj_newobj(trans) = newobj;
6821 nft_trans_commit_list_add_tail(ctx->net, trans);
6822
6823 return 0;
6824
6825 err_free_trans:
6826 kfree(trans);
6827 err_trans:
6828 module_put(type->owner);
6829 return err;
6830 }
6831
nf_tables_newobj(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])6832 static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info,
6833 const struct nlattr * const nla[])
6834 {
6835 struct netlink_ext_ack *extack = info->extack;
6836 u8 genmask = nft_genmask_next(info->net);
6837 u8 family = info->nfmsg->nfgen_family;
6838 const struct nft_object_type *type;
6839 struct net *net = info->net;
6840 struct nft_table *table;
6841 struct nft_object *obj;
6842 struct nft_ctx ctx;
6843 u32 objtype;
6844 int err;
6845
6846 if (!nla[NFTA_OBJ_TYPE] ||
6847 !nla[NFTA_OBJ_NAME] ||
6848 !nla[NFTA_OBJ_DATA])
6849 return -EINVAL;
6850
6851 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask,
6852 NETLINK_CB(skb).portid);
6853 if (IS_ERR(table)) {
6854 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
6855 return PTR_ERR(table);
6856 }
6857
6858 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
6859 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
6860 if (IS_ERR(obj)) {
6861 err = PTR_ERR(obj);
6862 if (err != -ENOENT) {
6863 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
6864 return err;
6865 }
6866 } else {
6867 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
6868 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
6869 return -EEXIST;
6870 }
6871 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
6872 return -EOPNOTSUPP;
6873
6874 type = __nft_obj_type_get(objtype);
6875 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
6876
6877 return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj);
6878 }
6879
6880 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
6881
6882 type = nft_obj_type_get(net, objtype);
6883 if (IS_ERR(type))
6884 return PTR_ERR(type);
6885
6886 obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]);
6887 if (IS_ERR(obj)) {
6888 err = PTR_ERR(obj);
6889 goto err_init;
6890 }
6891 obj->key.table = table;
6892 obj->handle = nf_tables_alloc_handle(table);
6893
6894 obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL_ACCOUNT);
6895 if (!obj->key.name) {
6896 err = -ENOMEM;
6897 goto err_strdup;
6898 }
6899
6900 if (nla[NFTA_OBJ_USERDATA]) {
6901 obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL);
6902 if (obj->udata == NULL)
6903 goto err_userdata;
6904
6905 obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]);
6906 }
6907
6908 err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj);
6909 if (err < 0)
6910 goto err_trans;
6911
6912 err = rhltable_insert(&nft_objname_ht, &obj->rhlhead,
6913 nft_objname_ht_params);
6914 if (err < 0)
6915 goto err_obj_ht;
6916
6917 list_add_tail_rcu(&obj->list, &table->objects);
6918 table->use++;
6919 return 0;
6920 err_obj_ht:
6921 /* queued in transaction log */
6922 INIT_LIST_HEAD(&obj->list);
6923 return err;
6924 err_trans:
6925 kfree(obj->udata);
6926 err_userdata:
6927 kfree(obj->key.name);
6928 err_strdup:
6929 if (obj->ops->destroy)
6930 obj->ops->destroy(&ctx, obj);
6931 kfree(obj);
6932 err_init:
6933 module_put(type->owner);
6934 return err;
6935 }
6936
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)6937 static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net,
6938 u32 portid, u32 seq, int event, u32 flags,
6939 int family, const struct nft_table *table,
6940 struct nft_object *obj, bool reset)
6941 {
6942 struct nlmsghdr *nlh;
6943
6944 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
6945 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
6946 NFNETLINK_V0, nft_base_seq(net));
6947 if (!nlh)
6948 goto nla_put_failure;
6949
6950 if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) ||
6951 nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) ||
6952 nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) ||
6953 nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) ||
6954 nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) ||
6955 nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle),
6956 NFTA_OBJ_PAD))
6957 goto nla_put_failure;
6958
6959 if (obj->udata &&
6960 nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata))
6961 goto nla_put_failure;
6962
6963 nlmsg_end(skb, nlh);
6964 return 0;
6965
6966 nla_put_failure:
6967 nlmsg_trim(skb, nlh);
6968 return -1;
6969 }
6970
6971 struct nft_obj_filter {
6972 char *table;
6973 u32 type;
6974 };
6975
nf_tables_dump_obj(struct sk_buff * skb,struct netlink_callback * cb)6976 static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)
6977 {
6978 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
6979 const struct nft_table *table;
6980 unsigned int idx = 0, s_idx = cb->args[0];
6981 struct nft_obj_filter *filter = cb->data;
6982 struct net *net = sock_net(skb->sk);
6983 int family = nfmsg->nfgen_family;
6984 struct nftables_pernet *nft_net;
6985 struct nft_object *obj;
6986 bool reset = false;
6987
6988 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
6989 reset = true;
6990
6991 rcu_read_lock();
6992 nft_net = nft_pernet(net);
6993 cb->seq = READ_ONCE(nft_net->base_seq);
6994
6995 list_for_each_entry_rcu(table, &nft_net->tables, list) {
6996 if (family != NFPROTO_UNSPEC && family != table->family)
6997 continue;
6998
6999 list_for_each_entry_rcu(obj, &table->objects, list) {
7000 if (!nft_is_active(net, obj))
7001 goto cont;
7002 if (idx < s_idx)
7003 goto cont;
7004 if (idx > s_idx)
7005 memset(&cb->args[1], 0,
7006 sizeof(cb->args) - sizeof(cb->args[0]));
7007 if (filter && filter->table &&
7008 strcmp(filter->table, table->name))
7009 goto cont;
7010 if (filter &&
7011 filter->type != NFT_OBJECT_UNSPEC &&
7012 obj->ops->type->type != filter->type)
7013 goto cont;
7014 if (reset) {
7015 char *buf = kasprintf(GFP_ATOMIC,
7016 "%s:%u",
7017 table->name,
7018 nft_net->base_seq);
7019
7020 audit_log_nfcfg(buf,
7021 family,
7022 obj->handle,
7023 AUDIT_NFT_OP_OBJ_RESET,
7024 GFP_ATOMIC);
7025 kfree(buf);
7026 }
7027
7028 if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid,
7029 cb->nlh->nlmsg_seq,
7030 NFT_MSG_NEWOBJ,
7031 NLM_F_MULTI | NLM_F_APPEND,
7032 table->family, table,
7033 obj, reset) < 0)
7034 goto done;
7035
7036 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
7037 cont:
7038 idx++;
7039 }
7040 }
7041 done:
7042 rcu_read_unlock();
7043
7044 cb->args[0] = idx;
7045 return skb->len;
7046 }
7047
nf_tables_dump_obj_start(struct netlink_callback * cb)7048 static int nf_tables_dump_obj_start(struct netlink_callback *cb)
7049 {
7050 const struct nlattr * const *nla = cb->data;
7051 struct nft_obj_filter *filter = NULL;
7052
7053 if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) {
7054 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
7055 if (!filter)
7056 return -ENOMEM;
7057
7058 if (nla[NFTA_OBJ_TABLE]) {
7059 filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC);
7060 if (!filter->table) {
7061 kfree(filter);
7062 return -ENOMEM;
7063 }
7064 }
7065
7066 if (nla[NFTA_OBJ_TYPE])
7067 filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7068 }
7069
7070 cb->data = filter;
7071 return 0;
7072 }
7073
nf_tables_dump_obj_done(struct netlink_callback * cb)7074 static int nf_tables_dump_obj_done(struct netlink_callback *cb)
7075 {
7076 struct nft_obj_filter *filter = cb->data;
7077
7078 if (filter) {
7079 kfree(filter->table);
7080 kfree(filter);
7081 }
7082
7083 return 0;
7084 }
7085
7086 /* called with rcu_read_lock held */
nf_tables_getobj(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])7087 static int nf_tables_getobj(struct sk_buff *skb, const struct nfnl_info *info,
7088 const struct nlattr * const nla[])
7089 {
7090 struct netlink_ext_ack *extack = info->extack;
7091 u8 genmask = nft_genmask_cur(info->net);
7092 u8 family = info->nfmsg->nfgen_family;
7093 const struct nft_table *table;
7094 struct net *net = info->net;
7095 struct nft_object *obj;
7096 struct sk_buff *skb2;
7097 bool reset = false;
7098 u32 objtype;
7099 int err;
7100
7101 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
7102 struct netlink_dump_control c = {
7103 .start = nf_tables_dump_obj_start,
7104 .dump = nf_tables_dump_obj,
7105 .done = nf_tables_dump_obj_done,
7106 .module = THIS_MODULE,
7107 .data = (void *)nla,
7108 };
7109
7110 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
7111 }
7112
7113 if (!nla[NFTA_OBJ_NAME] ||
7114 !nla[NFTA_OBJ_TYPE])
7115 return -EINVAL;
7116
7117 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, 0);
7118 if (IS_ERR(table)) {
7119 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
7120 return PTR_ERR(table);
7121 }
7122
7123 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7124 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
7125 if (IS_ERR(obj)) {
7126 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
7127 return PTR_ERR(obj);
7128 }
7129
7130 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
7131 if (!skb2)
7132 return -ENOMEM;
7133
7134 if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
7135 reset = true;
7136
7137 if (reset) {
7138 const struct nftables_pernet *nft_net;
7139 char *buf;
7140
7141 nft_net = nft_pernet(net);
7142 buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, nft_net->base_seq);
7143
7144 audit_log_nfcfg(buf,
7145 family,
7146 obj->handle,
7147 AUDIT_NFT_OP_OBJ_RESET,
7148 GFP_ATOMIC);
7149 kfree(buf);
7150 }
7151
7152 err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid,
7153 info->nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0,
7154 family, table, obj, reset);
7155 if (err < 0)
7156 goto err_fill_obj_info;
7157
7158 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
7159
7160 err_fill_obj_info:
7161 kfree_skb(skb2);
7162 return err;
7163 }
7164
nft_obj_destroy(const struct nft_ctx * ctx,struct nft_object * obj)7165 static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj)
7166 {
7167 if (obj->ops->destroy)
7168 obj->ops->destroy(ctx, obj);
7169
7170 module_put(obj->ops->type->owner);
7171 kfree(obj->key.name);
7172 kfree(obj->udata);
7173 kfree(obj);
7174 }
7175
nf_tables_delobj(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])7176 static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info,
7177 const struct nlattr * const nla[])
7178 {
7179 struct netlink_ext_ack *extack = info->extack;
7180 u8 genmask = nft_genmask_next(info->net);
7181 u8 family = info->nfmsg->nfgen_family;
7182 struct net *net = info->net;
7183 const struct nlattr *attr;
7184 struct nft_table *table;
7185 struct nft_object *obj;
7186 struct nft_ctx ctx;
7187 u32 objtype;
7188
7189 if (!nla[NFTA_OBJ_TYPE] ||
7190 (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE]))
7191 return -EINVAL;
7192
7193 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask,
7194 NETLINK_CB(skb).portid);
7195 if (IS_ERR(table)) {
7196 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
7197 return PTR_ERR(table);
7198 }
7199
7200 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
7201 if (nla[NFTA_OBJ_HANDLE]) {
7202 attr = nla[NFTA_OBJ_HANDLE];
7203 obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask);
7204 } else {
7205 attr = nla[NFTA_OBJ_NAME];
7206 obj = nft_obj_lookup(net, table, attr, objtype, genmask);
7207 }
7208
7209 if (IS_ERR(obj)) {
7210 NL_SET_BAD_ATTR(extack, attr);
7211 return PTR_ERR(obj);
7212 }
7213 if (obj->use > 0) {
7214 NL_SET_BAD_ATTR(extack, attr);
7215 return -EBUSY;
7216 }
7217
7218 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7219
7220 return nft_delobj(&ctx, obj);
7221 }
7222
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)7223 void nft_obj_notify(struct net *net, const struct nft_table *table,
7224 struct nft_object *obj, u32 portid, u32 seq, int event,
7225 u16 flags, int family, int report, gfp_t gfp)
7226 {
7227 struct nftables_pernet *nft_net = nft_pernet(net);
7228 struct sk_buff *skb;
7229 int err;
7230 char *buf = kasprintf(gfp, "%s:%u",
7231 table->name, nft_net->base_seq);
7232
7233 audit_log_nfcfg(buf,
7234 family,
7235 obj->handle,
7236 event == NFT_MSG_NEWOBJ ?
7237 AUDIT_NFT_OP_OBJ_REGISTER :
7238 AUDIT_NFT_OP_OBJ_UNREGISTER,
7239 gfp);
7240 kfree(buf);
7241
7242 if (!report &&
7243 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
7244 return;
7245
7246 skb = nlmsg_new(NLMSG_GOODSIZE, gfp);
7247 if (skb == NULL)
7248 goto err;
7249
7250 err = nf_tables_fill_obj_info(skb, net, portid, seq, event,
7251 flags & (NLM_F_CREATE | NLM_F_EXCL),
7252 family, table, obj, false);
7253 if (err < 0) {
7254 kfree_skb(skb);
7255 goto err;
7256 }
7257
7258 nft_notify_enqueue(skb, report, &nft_net->notify_list);
7259 return;
7260 err:
7261 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
7262 }
7263 EXPORT_SYMBOL_GPL(nft_obj_notify);
7264
nf_tables_obj_notify(const struct nft_ctx * ctx,struct nft_object * obj,int event)7265 static void nf_tables_obj_notify(const struct nft_ctx *ctx,
7266 struct nft_object *obj, int event)
7267 {
7268 nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event,
7269 ctx->flags, ctx->family, ctx->report, GFP_KERNEL);
7270 }
7271
7272 /*
7273 * Flow tables
7274 */
nft_register_flowtable_type(struct nf_flowtable_type * type)7275 void nft_register_flowtable_type(struct nf_flowtable_type *type)
7276 {
7277 nfnl_lock(NFNL_SUBSYS_NFTABLES);
7278 list_add_tail_rcu(&type->list, &nf_tables_flowtables);
7279 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
7280 }
7281 EXPORT_SYMBOL_GPL(nft_register_flowtable_type);
7282
nft_unregister_flowtable_type(struct nf_flowtable_type * type)7283 void nft_unregister_flowtable_type(struct nf_flowtable_type *type)
7284 {
7285 nfnl_lock(NFNL_SUBSYS_NFTABLES);
7286 list_del_rcu(&type->list);
7287 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
7288 }
7289 EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type);
7290
7291 static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = {
7292 [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING,
7293 .len = NFT_NAME_MAXLEN - 1 },
7294 [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING,
7295 .len = NFT_NAME_MAXLEN - 1 },
7296 [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED },
7297 [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 },
7298 [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 },
7299 };
7300
nft_flowtable_lookup(const struct nft_table * table,const struct nlattr * nla,u8 genmask)7301 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
7302 const struct nlattr *nla, u8 genmask)
7303 {
7304 struct nft_flowtable *flowtable;
7305
7306 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
7307 if (!nla_strcmp(nla, flowtable->name) &&
7308 nft_active_genmask(flowtable, genmask))
7309 return flowtable;
7310 }
7311 return ERR_PTR(-ENOENT);
7312 }
7313 EXPORT_SYMBOL_GPL(nft_flowtable_lookup);
7314
nf_tables_deactivate_flowtable(const struct nft_ctx * ctx,struct nft_flowtable * flowtable,enum nft_trans_phase phase)7315 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
7316 struct nft_flowtable *flowtable,
7317 enum nft_trans_phase phase)
7318 {
7319 switch (phase) {
7320 case NFT_TRANS_PREPARE:
7321 case NFT_TRANS_ABORT:
7322 case NFT_TRANS_RELEASE:
7323 flowtable->use--;
7324 fallthrough;
7325 default:
7326 return;
7327 }
7328 }
7329 EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable);
7330
7331 static struct nft_flowtable *
nft_flowtable_lookup_byhandle(const struct nft_table * table,const struct nlattr * nla,u8 genmask)7332 nft_flowtable_lookup_byhandle(const struct nft_table *table,
7333 const struct nlattr *nla, u8 genmask)
7334 {
7335 struct nft_flowtable *flowtable;
7336
7337 list_for_each_entry(flowtable, &table->flowtables, list) {
7338 if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle &&
7339 nft_active_genmask(flowtable, genmask))
7340 return flowtable;
7341 }
7342 return ERR_PTR(-ENOENT);
7343 }
7344
7345 struct nft_flowtable_hook {
7346 u32 num;
7347 int priority;
7348 struct list_head list;
7349 };
7350
7351 static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = {
7352 [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 },
7353 [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 },
7354 [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED },
7355 };
7356
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)7357 static int nft_flowtable_parse_hook(const struct nft_ctx *ctx,
7358 const struct nlattr *attr,
7359 struct nft_flowtable_hook *flowtable_hook,
7360 struct nft_flowtable *flowtable, bool add)
7361 {
7362 struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1];
7363 struct nft_hook *hook;
7364 int hooknum, priority;
7365 int err;
7366
7367 INIT_LIST_HEAD(&flowtable_hook->list);
7368
7369 err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, attr,
7370 nft_flowtable_hook_policy, NULL);
7371 if (err < 0)
7372 return err;
7373
7374 if (add) {
7375 if (!tb[NFTA_FLOWTABLE_HOOK_NUM] ||
7376 !tb[NFTA_FLOWTABLE_HOOK_PRIORITY])
7377 return -EINVAL;
7378
7379 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
7380 if (hooknum != NF_NETDEV_INGRESS)
7381 return -EOPNOTSUPP;
7382
7383 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
7384
7385 flowtable_hook->priority = priority;
7386 flowtable_hook->num = hooknum;
7387 } else {
7388 if (tb[NFTA_FLOWTABLE_HOOK_NUM]) {
7389 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
7390 if (hooknum != flowtable->hooknum)
7391 return -EOPNOTSUPP;
7392 }
7393
7394 if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) {
7395 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
7396 if (priority != flowtable->data.priority)
7397 return -EOPNOTSUPP;
7398 }
7399
7400 flowtable_hook->priority = flowtable->data.priority;
7401 flowtable_hook->num = flowtable->hooknum;
7402 }
7403
7404 if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) {
7405 err = nf_tables_parse_netdev_hooks(ctx->net,
7406 tb[NFTA_FLOWTABLE_HOOK_DEVS],
7407 &flowtable_hook->list);
7408 if (err < 0)
7409 return err;
7410 }
7411
7412 list_for_each_entry(hook, &flowtable_hook->list, list) {
7413 hook->ops.pf = NFPROTO_NETDEV;
7414 hook->ops.hooknum = flowtable_hook->num;
7415 hook->ops.priority = flowtable_hook->priority;
7416 hook->ops.priv = &flowtable->data;
7417 hook->ops.hook = flowtable->data.type->hook;
7418 }
7419
7420 return err;
7421 }
7422
__nft_flowtable_type_get(u8 family)7423 static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family)
7424 {
7425 const struct nf_flowtable_type *type;
7426
7427 list_for_each_entry(type, &nf_tables_flowtables, list) {
7428 if (family == type->family)
7429 return type;
7430 }
7431 return NULL;
7432 }
7433
7434 static const struct nf_flowtable_type *
nft_flowtable_type_get(struct net * net,u8 family)7435 nft_flowtable_type_get(struct net *net, u8 family)
7436 {
7437 const struct nf_flowtable_type *type;
7438
7439 type = __nft_flowtable_type_get(family);
7440 if (type != NULL && try_module_get(type->owner))
7441 return type;
7442
7443 lockdep_nfnl_nft_mutex_not_held();
7444 #ifdef CONFIG_MODULES
7445 if (type == NULL) {
7446 if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN)
7447 return ERR_PTR(-EAGAIN);
7448 }
7449 #endif
7450 return ERR_PTR(-ENOENT);
7451 }
7452
7453 /* Only called from error and netdev event paths. */
nft_unregister_flowtable_hook(struct net * net,struct nft_flowtable * flowtable,struct nft_hook * hook)7454 static void nft_unregister_flowtable_hook(struct net *net,
7455 struct nft_flowtable *flowtable,
7456 struct nft_hook *hook)
7457 {
7458 nf_unregister_net_hook(net, &hook->ops);
7459 flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
7460 FLOW_BLOCK_UNBIND);
7461 }
7462
__nft_unregister_flowtable_net_hooks(struct net * net,struct list_head * hook_list,bool release_netdev)7463 static void __nft_unregister_flowtable_net_hooks(struct net *net,
7464 struct list_head *hook_list,
7465 bool release_netdev)
7466 {
7467 struct nft_hook *hook, *next;
7468
7469 list_for_each_entry_safe(hook, next, hook_list, list) {
7470 nf_unregister_net_hook(net, &hook->ops);
7471 if (release_netdev) {
7472 list_del(&hook->list);
7473 kfree_rcu(hook, rcu);
7474 }
7475 }
7476 }
7477
nft_unregister_flowtable_net_hooks(struct net * net,struct list_head * hook_list)7478 static void nft_unregister_flowtable_net_hooks(struct net *net,
7479 struct list_head *hook_list)
7480 {
7481 __nft_unregister_flowtable_net_hooks(net, hook_list, false);
7482 }
7483
nft_register_flowtable_net_hooks(struct net * net,struct nft_table * table,struct list_head * hook_list,struct nft_flowtable * flowtable)7484 static int nft_register_flowtable_net_hooks(struct net *net,
7485 struct nft_table *table,
7486 struct list_head *hook_list,
7487 struct nft_flowtable *flowtable)
7488 {
7489 struct nft_hook *hook, *hook2, *next;
7490 struct nft_flowtable *ft;
7491 int err, i = 0;
7492
7493 list_for_each_entry(hook, hook_list, list) {
7494 list_for_each_entry(ft, &table->flowtables, list) {
7495 if (!nft_is_active_next(net, ft))
7496 continue;
7497
7498 list_for_each_entry(hook2, &ft->hook_list, list) {
7499 if (hook->ops.dev == hook2->ops.dev &&
7500 hook->ops.pf == hook2->ops.pf) {
7501 err = -EEXIST;
7502 goto err_unregister_net_hooks;
7503 }
7504 }
7505 }
7506
7507 err = flowtable->data.type->setup(&flowtable->data,
7508 hook->ops.dev,
7509 FLOW_BLOCK_BIND);
7510 if (err < 0)
7511 goto err_unregister_net_hooks;
7512
7513 err = nf_register_net_hook(net, &hook->ops);
7514 if (err < 0) {
7515 flowtable->data.type->setup(&flowtable->data,
7516 hook->ops.dev,
7517 FLOW_BLOCK_UNBIND);
7518 goto err_unregister_net_hooks;
7519 }
7520
7521 i++;
7522 }
7523
7524 return 0;
7525
7526 err_unregister_net_hooks:
7527 list_for_each_entry_safe(hook, next, hook_list, list) {
7528 if (i-- <= 0)
7529 break;
7530
7531 nft_unregister_flowtable_hook(net, flowtable, hook);
7532 list_del_rcu(&hook->list);
7533 kfree_rcu(hook, rcu);
7534 }
7535
7536 return err;
7537 }
7538
nft_flowtable_hooks_destroy(struct list_head * hook_list)7539 static void nft_flowtable_hooks_destroy(struct list_head *hook_list)
7540 {
7541 struct nft_hook *hook, *next;
7542
7543 list_for_each_entry_safe(hook, next, hook_list, list) {
7544 list_del_rcu(&hook->list);
7545 kfree_rcu(hook, rcu);
7546 }
7547 }
7548
nft_flowtable_update(struct nft_ctx * ctx,const struct nlmsghdr * nlh,struct nft_flowtable * flowtable)7549 static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh,
7550 struct nft_flowtable *flowtable)
7551 {
7552 const struct nlattr * const *nla = ctx->nla;
7553 struct nft_flowtable_hook flowtable_hook;
7554 struct nft_hook *hook, *next;
7555 struct nft_trans *trans;
7556 bool unregister = false;
7557 u32 flags;
7558 int err;
7559
7560 err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK],
7561 &flowtable_hook, flowtable, false);
7562 if (err < 0)
7563 return err;
7564
7565 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
7566 if (nft_hook_list_find(&flowtable->hook_list, hook)) {
7567 list_del(&hook->list);
7568 kfree(hook);
7569 }
7570 }
7571
7572 if (nla[NFTA_FLOWTABLE_FLAGS]) {
7573 flags = ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
7574 if (flags & ~NFT_FLOWTABLE_MASK) {
7575 err = -EOPNOTSUPP;
7576 goto err_flowtable_update_hook;
7577 }
7578 if ((flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD) ^
7579 (flags & NFT_FLOWTABLE_HW_OFFLOAD)) {
7580 err = -EOPNOTSUPP;
7581 goto err_flowtable_update_hook;
7582 }
7583 } else {
7584 flags = flowtable->data.flags;
7585 }
7586
7587 err = nft_register_flowtable_net_hooks(ctx->net, ctx->table,
7588 &flowtable_hook.list, flowtable);
7589 if (err < 0)
7590 goto err_flowtable_update_hook;
7591
7592 trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE,
7593 sizeof(struct nft_trans_flowtable));
7594 if (!trans) {
7595 unregister = true;
7596 err = -ENOMEM;
7597 goto err_flowtable_update_hook;
7598 }
7599
7600 nft_trans_flowtable_flags(trans) = flags;
7601 nft_trans_flowtable(trans) = flowtable;
7602 nft_trans_flowtable_update(trans) = true;
7603 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
7604 list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans));
7605
7606 nft_trans_commit_list_add_tail(ctx->net, trans);
7607
7608 return 0;
7609
7610 err_flowtable_update_hook:
7611 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
7612 if (unregister)
7613 nft_unregister_flowtable_hook(ctx->net, flowtable, hook);
7614 list_del_rcu(&hook->list);
7615 kfree_rcu(hook, rcu);
7616 }
7617
7618 return err;
7619
7620 }
7621
nf_tables_newflowtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])7622 static int nf_tables_newflowtable(struct sk_buff *skb,
7623 const struct nfnl_info *info,
7624 const struct nlattr * const nla[])
7625 {
7626 struct netlink_ext_ack *extack = info->extack;
7627 struct nft_flowtable_hook flowtable_hook;
7628 u8 genmask = nft_genmask_next(info->net);
7629 u8 family = info->nfmsg->nfgen_family;
7630 const struct nf_flowtable_type *type;
7631 struct nft_flowtable *flowtable;
7632 struct nft_hook *hook, *next;
7633 struct net *net = info->net;
7634 struct nft_table *table;
7635 struct nft_ctx ctx;
7636 int err;
7637
7638 if (!nla[NFTA_FLOWTABLE_TABLE] ||
7639 !nla[NFTA_FLOWTABLE_NAME] ||
7640 !nla[NFTA_FLOWTABLE_HOOK])
7641 return -EINVAL;
7642
7643 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
7644 genmask, NETLINK_CB(skb).portid);
7645 if (IS_ERR(table)) {
7646 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
7647 return PTR_ERR(table);
7648 }
7649
7650 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
7651 genmask);
7652 if (IS_ERR(flowtable)) {
7653 err = PTR_ERR(flowtable);
7654 if (err != -ENOENT) {
7655 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
7656 return err;
7657 }
7658 } else {
7659 if (info->nlh->nlmsg_flags & NLM_F_EXCL) {
7660 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
7661 return -EEXIST;
7662 }
7663
7664 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7665
7666 return nft_flowtable_update(&ctx, info->nlh, flowtable);
7667 }
7668
7669 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7670
7671 flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL_ACCOUNT);
7672 if (!flowtable)
7673 return -ENOMEM;
7674
7675 flowtable->table = table;
7676 flowtable->handle = nf_tables_alloc_handle(table);
7677 INIT_LIST_HEAD(&flowtable->hook_list);
7678
7679 flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL_ACCOUNT);
7680 if (!flowtable->name) {
7681 err = -ENOMEM;
7682 goto err1;
7683 }
7684
7685 type = nft_flowtable_type_get(net, family);
7686 if (IS_ERR(type)) {
7687 err = PTR_ERR(type);
7688 goto err2;
7689 }
7690
7691 if (nla[NFTA_FLOWTABLE_FLAGS]) {
7692 flowtable->data.flags =
7693 ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
7694 if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) {
7695 err = -EOPNOTSUPP;
7696 goto err3;
7697 }
7698 }
7699
7700 write_pnet(&flowtable->data.net, net);
7701 flowtable->data.type = type;
7702 err = type->init(&flowtable->data);
7703 if (err < 0)
7704 goto err3;
7705
7706 err = nft_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK],
7707 &flowtable_hook, flowtable, true);
7708 if (err < 0)
7709 goto err4;
7710
7711 list_splice(&flowtable_hook.list, &flowtable->hook_list);
7712 flowtable->data.priority = flowtable_hook.priority;
7713 flowtable->hooknum = flowtable_hook.num;
7714
7715 err = nft_register_flowtable_net_hooks(ctx.net, table,
7716 &flowtable->hook_list,
7717 flowtable);
7718 if (err < 0) {
7719 nft_flowtable_hooks_destroy(&flowtable->hook_list);
7720 goto err4;
7721 }
7722
7723 err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable);
7724 if (err < 0)
7725 goto err5;
7726
7727 list_add_tail_rcu(&flowtable->list, &table->flowtables);
7728 table->use++;
7729
7730 return 0;
7731 err5:
7732 list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
7733 nft_unregister_flowtable_hook(net, flowtable, hook);
7734 list_del_rcu(&hook->list);
7735 kfree_rcu(hook, rcu);
7736 }
7737 err4:
7738 flowtable->data.type->free(&flowtable->data);
7739 err3:
7740 module_put(type->owner);
7741 err2:
7742 kfree(flowtable->name);
7743 err1:
7744 kfree(flowtable);
7745 return err;
7746 }
7747
nft_flowtable_hook_release(struct nft_flowtable_hook * flowtable_hook)7748 static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook)
7749 {
7750 struct nft_hook *this, *next;
7751
7752 list_for_each_entry_safe(this, next, &flowtable_hook->list, list) {
7753 list_del(&this->list);
7754 kfree(this);
7755 }
7756 }
7757
nft_delflowtable_hook(struct nft_ctx * ctx,struct nft_flowtable * flowtable)7758 static int nft_delflowtable_hook(struct nft_ctx *ctx,
7759 struct nft_flowtable *flowtable)
7760 {
7761 const struct nlattr * const *nla = ctx->nla;
7762 struct nft_flowtable_hook flowtable_hook;
7763 LIST_HEAD(flowtable_del_list);
7764 struct nft_hook *this, *hook;
7765 struct nft_trans *trans;
7766 int err;
7767
7768 err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK],
7769 &flowtable_hook, flowtable, false);
7770 if (err < 0)
7771 return err;
7772
7773 list_for_each_entry(this, &flowtable_hook.list, list) {
7774 hook = nft_hook_list_find(&flowtable->hook_list, this);
7775 if (!hook) {
7776 err = -ENOENT;
7777 goto err_flowtable_del_hook;
7778 }
7779 list_move(&hook->list, &flowtable_del_list);
7780 }
7781
7782 trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE,
7783 sizeof(struct nft_trans_flowtable));
7784 if (!trans) {
7785 err = -ENOMEM;
7786 goto err_flowtable_del_hook;
7787 }
7788
7789 nft_trans_flowtable(trans) = flowtable;
7790 nft_trans_flowtable_update(trans) = true;
7791 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
7792 list_splice(&flowtable_del_list, &nft_trans_flowtable_hooks(trans));
7793 nft_flowtable_hook_release(&flowtable_hook);
7794
7795 nft_trans_commit_list_add_tail(ctx->net, trans);
7796
7797 return 0;
7798
7799 err_flowtable_del_hook:
7800 list_splice(&flowtable_del_list, &flowtable->hook_list);
7801 nft_flowtable_hook_release(&flowtable_hook);
7802
7803 return err;
7804 }
7805
nf_tables_delflowtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])7806 static int nf_tables_delflowtable(struct sk_buff *skb,
7807 const struct nfnl_info *info,
7808 const struct nlattr * const nla[])
7809 {
7810 struct netlink_ext_ack *extack = info->extack;
7811 u8 genmask = nft_genmask_next(info->net);
7812 u8 family = info->nfmsg->nfgen_family;
7813 struct nft_flowtable *flowtable;
7814 struct net *net = info->net;
7815 const struct nlattr *attr;
7816 struct nft_table *table;
7817 struct nft_ctx ctx;
7818
7819 if (!nla[NFTA_FLOWTABLE_TABLE] ||
7820 (!nla[NFTA_FLOWTABLE_NAME] &&
7821 !nla[NFTA_FLOWTABLE_HANDLE]))
7822 return -EINVAL;
7823
7824 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
7825 genmask, NETLINK_CB(skb).portid);
7826 if (IS_ERR(table)) {
7827 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
7828 return PTR_ERR(table);
7829 }
7830
7831 if (nla[NFTA_FLOWTABLE_HANDLE]) {
7832 attr = nla[NFTA_FLOWTABLE_HANDLE];
7833 flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask);
7834 } else {
7835 attr = nla[NFTA_FLOWTABLE_NAME];
7836 flowtable = nft_flowtable_lookup(table, attr, genmask);
7837 }
7838
7839 if (IS_ERR(flowtable)) {
7840 NL_SET_BAD_ATTR(extack, attr);
7841 return PTR_ERR(flowtable);
7842 }
7843
7844 nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla);
7845
7846 if (nla[NFTA_FLOWTABLE_HOOK])
7847 return nft_delflowtable_hook(&ctx, flowtable);
7848
7849 if (flowtable->use > 0) {
7850 NL_SET_BAD_ATTR(extack, attr);
7851 return -EBUSY;
7852 }
7853
7854 return nft_delflowtable(&ctx, flowtable);
7855 }
7856
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)7857 static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,
7858 u32 portid, u32 seq, int event,
7859 u32 flags, int family,
7860 struct nft_flowtable *flowtable,
7861 struct list_head *hook_list)
7862 {
7863 struct nlattr *nest, *nest_devs;
7864 struct nft_hook *hook;
7865 struct nlmsghdr *nlh;
7866
7867 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
7868 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
7869 NFNETLINK_V0, nft_base_seq(net));
7870 if (!nlh)
7871 goto nla_put_failure;
7872
7873 if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) ||
7874 nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) ||
7875 nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) ||
7876 nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle),
7877 NFTA_FLOWTABLE_PAD) ||
7878 nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags)))
7879 goto nla_put_failure;
7880
7881 nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK);
7882 if (!nest)
7883 goto nla_put_failure;
7884 if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) ||
7885 nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority)))
7886 goto nla_put_failure;
7887
7888 nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS);
7889 if (!nest_devs)
7890 goto nla_put_failure;
7891
7892 list_for_each_entry_rcu(hook, hook_list, list) {
7893 if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name))
7894 goto nla_put_failure;
7895 }
7896 nla_nest_end(skb, nest_devs);
7897 nla_nest_end(skb, nest);
7898
7899 nlmsg_end(skb, nlh);
7900 return 0;
7901
7902 nla_put_failure:
7903 nlmsg_trim(skb, nlh);
7904 return -1;
7905 }
7906
7907 struct nft_flowtable_filter {
7908 char *table;
7909 };
7910
nf_tables_dump_flowtable(struct sk_buff * skb,struct netlink_callback * cb)7911 static int nf_tables_dump_flowtable(struct sk_buff *skb,
7912 struct netlink_callback *cb)
7913 {
7914 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
7915 struct nft_flowtable_filter *filter = cb->data;
7916 unsigned int idx = 0, s_idx = cb->args[0];
7917 struct net *net = sock_net(skb->sk);
7918 int family = nfmsg->nfgen_family;
7919 struct nft_flowtable *flowtable;
7920 struct nftables_pernet *nft_net;
7921 const struct nft_table *table;
7922
7923 rcu_read_lock();
7924 nft_net = nft_pernet(net);
7925 cb->seq = READ_ONCE(nft_net->base_seq);
7926
7927 list_for_each_entry_rcu(table, &nft_net->tables, list) {
7928 if (family != NFPROTO_UNSPEC && family != table->family)
7929 continue;
7930
7931 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
7932 if (!nft_is_active(net, flowtable))
7933 goto cont;
7934 if (idx < s_idx)
7935 goto cont;
7936 if (idx > s_idx)
7937 memset(&cb->args[1], 0,
7938 sizeof(cb->args) - sizeof(cb->args[0]));
7939 if (filter && filter->table &&
7940 strcmp(filter->table, table->name))
7941 goto cont;
7942
7943 if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid,
7944 cb->nlh->nlmsg_seq,
7945 NFT_MSG_NEWFLOWTABLE,
7946 NLM_F_MULTI | NLM_F_APPEND,
7947 table->family,
7948 flowtable,
7949 &flowtable->hook_list) < 0)
7950 goto done;
7951
7952 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
7953 cont:
7954 idx++;
7955 }
7956 }
7957 done:
7958 rcu_read_unlock();
7959
7960 cb->args[0] = idx;
7961 return skb->len;
7962 }
7963
nf_tables_dump_flowtable_start(struct netlink_callback * cb)7964 static int nf_tables_dump_flowtable_start(struct netlink_callback *cb)
7965 {
7966 const struct nlattr * const *nla = cb->data;
7967 struct nft_flowtable_filter *filter = NULL;
7968
7969 if (nla[NFTA_FLOWTABLE_TABLE]) {
7970 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
7971 if (!filter)
7972 return -ENOMEM;
7973
7974 filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE],
7975 GFP_ATOMIC);
7976 if (!filter->table) {
7977 kfree(filter);
7978 return -ENOMEM;
7979 }
7980 }
7981
7982 cb->data = filter;
7983 return 0;
7984 }
7985
nf_tables_dump_flowtable_done(struct netlink_callback * cb)7986 static int nf_tables_dump_flowtable_done(struct netlink_callback *cb)
7987 {
7988 struct nft_flowtable_filter *filter = cb->data;
7989
7990 if (!filter)
7991 return 0;
7992
7993 kfree(filter->table);
7994 kfree(filter);
7995
7996 return 0;
7997 }
7998
7999 /* called with rcu_read_lock held */
nf_tables_getflowtable(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8000 static int nf_tables_getflowtable(struct sk_buff *skb,
8001 const struct nfnl_info *info,
8002 const struct nlattr * const nla[])
8003 {
8004 u8 genmask = nft_genmask_cur(info->net);
8005 u8 family = info->nfmsg->nfgen_family;
8006 struct nft_flowtable *flowtable;
8007 const struct nft_table *table;
8008 struct net *net = info->net;
8009 struct sk_buff *skb2;
8010 int err;
8011
8012 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
8013 struct netlink_dump_control c = {
8014 .start = nf_tables_dump_flowtable_start,
8015 .dump = nf_tables_dump_flowtable,
8016 .done = nf_tables_dump_flowtable_done,
8017 .module = THIS_MODULE,
8018 .data = (void *)nla,
8019 };
8020
8021 return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
8022 }
8023
8024 if (!nla[NFTA_FLOWTABLE_NAME])
8025 return -EINVAL;
8026
8027 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
8028 genmask, 0);
8029 if (IS_ERR(table))
8030 return PTR_ERR(table);
8031
8032 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
8033 genmask);
8034 if (IS_ERR(flowtable))
8035 return PTR_ERR(flowtable);
8036
8037 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
8038 if (!skb2)
8039 return -ENOMEM;
8040
8041 err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid,
8042 info->nlh->nlmsg_seq,
8043 NFT_MSG_NEWFLOWTABLE, 0, family,
8044 flowtable, &flowtable->hook_list);
8045 if (err < 0)
8046 goto err_fill_flowtable_info;
8047
8048 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
8049
8050 err_fill_flowtable_info:
8051 kfree_skb(skb2);
8052 return err;
8053 }
8054
nf_tables_flowtable_notify(struct nft_ctx * ctx,struct nft_flowtable * flowtable,struct list_head * hook_list,int event)8055 static void nf_tables_flowtable_notify(struct nft_ctx *ctx,
8056 struct nft_flowtable *flowtable,
8057 struct list_head *hook_list,
8058 int event)
8059 {
8060 struct nftables_pernet *nft_net = nft_pernet(ctx->net);
8061 struct sk_buff *skb;
8062 u16 flags = 0;
8063 int err;
8064
8065 if (!ctx->report &&
8066 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
8067 return;
8068
8069 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
8070 if (skb == NULL)
8071 goto err;
8072
8073 if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL))
8074 flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL);
8075
8076 err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid,
8077 ctx->seq, event, flags,
8078 ctx->family, flowtable, hook_list);
8079 if (err < 0) {
8080 kfree_skb(skb);
8081 goto err;
8082 }
8083
8084 nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
8085 return;
8086 err:
8087 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
8088 }
8089
nf_tables_flowtable_destroy(struct nft_flowtable * flowtable)8090 static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
8091 {
8092 struct nft_hook *hook, *next;
8093
8094 flowtable->data.type->free(&flowtable->data);
8095 list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
8096 flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
8097 FLOW_BLOCK_UNBIND);
8098 list_del_rcu(&hook->list);
8099 kfree(hook);
8100 }
8101 kfree(flowtable->name);
8102 module_put(flowtable->data.type->owner);
8103 kfree(flowtable);
8104 }
8105
nf_tables_fill_gen_info(struct sk_buff * skb,struct net * net,u32 portid,u32 seq)8106 static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
8107 u32 portid, u32 seq)
8108 {
8109 struct nftables_pernet *nft_net = nft_pernet(net);
8110 struct nlmsghdr *nlh;
8111 char buf[TASK_COMM_LEN];
8112 int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN);
8113
8114 nlh = nfnl_msg_put(skb, portid, seq, event, 0, AF_UNSPEC,
8115 NFNETLINK_V0, nft_base_seq(net));
8116 if (!nlh)
8117 goto nla_put_failure;
8118
8119 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(nft_net->base_seq)) ||
8120 nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) ||
8121 nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current)))
8122 goto nla_put_failure;
8123
8124 nlmsg_end(skb, nlh);
8125 return 0;
8126
8127 nla_put_failure:
8128 nlmsg_trim(skb, nlh);
8129 return -EMSGSIZE;
8130 }
8131
nft_flowtable_event(unsigned long event,struct net_device * dev,struct nft_flowtable * flowtable)8132 static void nft_flowtable_event(unsigned long event, struct net_device *dev,
8133 struct nft_flowtable *flowtable)
8134 {
8135 struct nft_hook *hook;
8136
8137 list_for_each_entry(hook, &flowtable->hook_list, list) {
8138 if (hook->ops.dev != dev)
8139 continue;
8140
8141 /* flow_offload_netdev_event() cleans up entries for us. */
8142 nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook);
8143 list_del_rcu(&hook->list);
8144 kfree_rcu(hook, rcu);
8145 break;
8146 }
8147 }
8148
nf_tables_flowtable_event(struct notifier_block * this,unsigned long event,void * ptr)8149 static int nf_tables_flowtable_event(struct notifier_block *this,
8150 unsigned long event, void *ptr)
8151 {
8152 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
8153 struct nft_flowtable *flowtable;
8154 struct nftables_pernet *nft_net;
8155 struct nft_table *table;
8156 struct net *net;
8157
8158 if (event != NETDEV_UNREGISTER)
8159 return 0;
8160
8161 net = dev_net(dev);
8162 nft_net = nft_pernet(net);
8163 mutex_lock(&nft_net->commit_mutex);
8164 list_for_each_entry(table, &nft_net->tables, list) {
8165 list_for_each_entry(flowtable, &table->flowtables, list) {
8166 nft_flowtable_event(event, dev, flowtable);
8167 }
8168 }
8169 mutex_unlock(&nft_net->commit_mutex);
8170
8171 return NOTIFY_DONE;
8172 }
8173
8174 static struct notifier_block nf_tables_flowtable_notifier = {
8175 .notifier_call = nf_tables_flowtable_event,
8176 };
8177
nf_tables_gen_notify(struct net * net,struct sk_buff * skb,int event)8178 static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb,
8179 int event)
8180 {
8181 struct nlmsghdr *nlh = nlmsg_hdr(skb);
8182 struct sk_buff *skb2;
8183 int err;
8184
8185 if (!nlmsg_report(nlh) &&
8186 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
8187 return;
8188
8189 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
8190 if (skb2 == NULL)
8191 goto err;
8192
8193 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
8194 nlh->nlmsg_seq);
8195 if (err < 0) {
8196 kfree_skb(skb2);
8197 goto err;
8198 }
8199
8200 nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
8201 nlmsg_report(nlh), GFP_KERNEL);
8202 return;
8203 err:
8204 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
8205 -ENOBUFS);
8206 }
8207
nf_tables_getgen(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const nla[])8208 static int nf_tables_getgen(struct sk_buff *skb, const struct nfnl_info *info,
8209 const struct nlattr * const nla[])
8210 {
8211 struct sk_buff *skb2;
8212 int err;
8213
8214 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
8215 if (skb2 == NULL)
8216 return -ENOMEM;
8217
8218 err = nf_tables_fill_gen_info(skb2, info->net, NETLINK_CB(skb).portid,
8219 info->nlh->nlmsg_seq);
8220 if (err < 0)
8221 goto err_fill_gen_info;
8222
8223 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
8224
8225 err_fill_gen_info:
8226 kfree_skb(skb2);
8227 return err;
8228 }
8229
8230 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
8231 [NFT_MSG_NEWTABLE] = {
8232 .call = nf_tables_newtable,
8233 .type = NFNL_CB_BATCH,
8234 .attr_count = NFTA_TABLE_MAX,
8235 .policy = nft_table_policy,
8236 },
8237 [NFT_MSG_GETTABLE] = {
8238 .call = nf_tables_gettable,
8239 .type = NFNL_CB_RCU,
8240 .attr_count = NFTA_TABLE_MAX,
8241 .policy = nft_table_policy,
8242 },
8243 [NFT_MSG_DELTABLE] = {
8244 .call = nf_tables_deltable,
8245 .type = NFNL_CB_BATCH,
8246 .attr_count = NFTA_TABLE_MAX,
8247 .policy = nft_table_policy,
8248 },
8249 [NFT_MSG_NEWCHAIN] = {
8250 .call = nf_tables_newchain,
8251 .type = NFNL_CB_BATCH,
8252 .attr_count = NFTA_CHAIN_MAX,
8253 .policy = nft_chain_policy,
8254 },
8255 [NFT_MSG_GETCHAIN] = {
8256 .call = nf_tables_getchain,
8257 .type = NFNL_CB_RCU,
8258 .attr_count = NFTA_CHAIN_MAX,
8259 .policy = nft_chain_policy,
8260 },
8261 [NFT_MSG_DELCHAIN] = {
8262 .call = nf_tables_delchain,
8263 .type = NFNL_CB_BATCH,
8264 .attr_count = NFTA_CHAIN_MAX,
8265 .policy = nft_chain_policy,
8266 },
8267 [NFT_MSG_NEWRULE] = {
8268 .call = nf_tables_newrule,
8269 .type = NFNL_CB_BATCH,
8270 .attr_count = NFTA_RULE_MAX,
8271 .policy = nft_rule_policy,
8272 },
8273 [NFT_MSG_GETRULE] = {
8274 .call = nf_tables_getrule,
8275 .type = NFNL_CB_RCU,
8276 .attr_count = NFTA_RULE_MAX,
8277 .policy = nft_rule_policy,
8278 },
8279 [NFT_MSG_DELRULE] = {
8280 .call = nf_tables_delrule,
8281 .type = NFNL_CB_BATCH,
8282 .attr_count = NFTA_RULE_MAX,
8283 .policy = nft_rule_policy,
8284 },
8285 [NFT_MSG_NEWSET] = {
8286 .call = nf_tables_newset,
8287 .type = NFNL_CB_BATCH,
8288 .attr_count = NFTA_SET_MAX,
8289 .policy = nft_set_policy,
8290 },
8291 [NFT_MSG_GETSET] = {
8292 .call = nf_tables_getset,
8293 .type = NFNL_CB_RCU,
8294 .attr_count = NFTA_SET_MAX,
8295 .policy = nft_set_policy,
8296 },
8297 [NFT_MSG_DELSET] = {
8298 .call = nf_tables_delset,
8299 .type = NFNL_CB_BATCH,
8300 .attr_count = NFTA_SET_MAX,
8301 .policy = nft_set_policy,
8302 },
8303 [NFT_MSG_NEWSETELEM] = {
8304 .call = nf_tables_newsetelem,
8305 .type = NFNL_CB_BATCH,
8306 .attr_count = NFTA_SET_ELEM_LIST_MAX,
8307 .policy = nft_set_elem_list_policy,
8308 },
8309 [NFT_MSG_GETSETELEM] = {
8310 .call = nf_tables_getsetelem,
8311 .type = NFNL_CB_RCU,
8312 .attr_count = NFTA_SET_ELEM_LIST_MAX,
8313 .policy = nft_set_elem_list_policy,
8314 },
8315 [NFT_MSG_DELSETELEM] = {
8316 .call = nf_tables_delsetelem,
8317 .type = NFNL_CB_BATCH,
8318 .attr_count = NFTA_SET_ELEM_LIST_MAX,
8319 .policy = nft_set_elem_list_policy,
8320 },
8321 [NFT_MSG_GETGEN] = {
8322 .call = nf_tables_getgen,
8323 .type = NFNL_CB_RCU,
8324 },
8325 [NFT_MSG_NEWOBJ] = {
8326 .call = nf_tables_newobj,
8327 .type = NFNL_CB_BATCH,
8328 .attr_count = NFTA_OBJ_MAX,
8329 .policy = nft_obj_policy,
8330 },
8331 [NFT_MSG_GETOBJ] = {
8332 .call = nf_tables_getobj,
8333 .type = NFNL_CB_RCU,
8334 .attr_count = NFTA_OBJ_MAX,
8335 .policy = nft_obj_policy,
8336 },
8337 [NFT_MSG_DELOBJ] = {
8338 .call = nf_tables_delobj,
8339 .type = NFNL_CB_BATCH,
8340 .attr_count = NFTA_OBJ_MAX,
8341 .policy = nft_obj_policy,
8342 },
8343 [NFT_MSG_GETOBJ_RESET] = {
8344 .call = nf_tables_getobj,
8345 .type = NFNL_CB_RCU,
8346 .attr_count = NFTA_OBJ_MAX,
8347 .policy = nft_obj_policy,
8348 },
8349 [NFT_MSG_NEWFLOWTABLE] = {
8350 .call = nf_tables_newflowtable,
8351 .type = NFNL_CB_BATCH,
8352 .attr_count = NFTA_FLOWTABLE_MAX,
8353 .policy = nft_flowtable_policy,
8354 },
8355 [NFT_MSG_GETFLOWTABLE] = {
8356 .call = nf_tables_getflowtable,
8357 .type = NFNL_CB_RCU,
8358 .attr_count = NFTA_FLOWTABLE_MAX,
8359 .policy = nft_flowtable_policy,
8360 },
8361 [NFT_MSG_DELFLOWTABLE] = {
8362 .call = nf_tables_delflowtable,
8363 .type = NFNL_CB_BATCH,
8364 .attr_count = NFTA_FLOWTABLE_MAX,
8365 .policy = nft_flowtable_policy,
8366 },
8367 };
8368
nf_tables_validate(struct net * net)8369 static int nf_tables_validate(struct net *net)
8370 {
8371 struct nftables_pernet *nft_net = nft_pernet(net);
8372 struct nft_table *table;
8373
8374 switch (nft_net->validate_state) {
8375 case NFT_VALIDATE_SKIP:
8376 break;
8377 case NFT_VALIDATE_NEED:
8378 nft_validate_state_update(net, NFT_VALIDATE_DO);
8379 fallthrough;
8380 case NFT_VALIDATE_DO:
8381 list_for_each_entry(table, &nft_net->tables, list) {
8382 if (nft_table_validate(net, table) < 0)
8383 return -EAGAIN;
8384 }
8385 break;
8386 }
8387
8388 return 0;
8389 }
8390
8391 /* a drop policy has to be deferred until all rules have been activated,
8392 * otherwise a large ruleset that contains a drop-policy base chain will
8393 * cause all packets to get dropped until the full transaction has been
8394 * processed.
8395 *
8396 * We defer the drop policy until the transaction has been finalized.
8397 */
nft_chain_commit_drop_policy(struct nft_trans * trans)8398 static void nft_chain_commit_drop_policy(struct nft_trans *trans)
8399 {
8400 struct nft_base_chain *basechain;
8401
8402 if (nft_trans_chain_policy(trans) != NF_DROP)
8403 return;
8404
8405 if (!nft_is_base_chain(trans->ctx.chain))
8406 return;
8407
8408 basechain = nft_base_chain(trans->ctx.chain);
8409 basechain->policy = NF_DROP;
8410 }
8411
nft_chain_commit_update(struct nft_trans * trans)8412 static void nft_chain_commit_update(struct nft_trans *trans)
8413 {
8414 struct nft_base_chain *basechain;
8415
8416 if (nft_trans_chain_name(trans)) {
8417 rhltable_remove(&trans->ctx.table->chains_ht,
8418 &trans->ctx.chain->rhlhead,
8419 nft_chain_ht_params);
8420 swap(trans->ctx.chain->name, nft_trans_chain_name(trans));
8421 rhltable_insert_key(&trans->ctx.table->chains_ht,
8422 trans->ctx.chain->name,
8423 &trans->ctx.chain->rhlhead,
8424 nft_chain_ht_params);
8425 }
8426
8427 if (!nft_is_base_chain(trans->ctx.chain))
8428 return;
8429
8430 nft_chain_stats_replace(trans);
8431
8432 basechain = nft_base_chain(trans->ctx.chain);
8433
8434 switch (nft_trans_chain_policy(trans)) {
8435 case NF_DROP:
8436 case NF_ACCEPT:
8437 basechain->policy = nft_trans_chain_policy(trans);
8438 break;
8439 }
8440 }
8441
nft_obj_commit_update(struct nft_trans * trans)8442 static void nft_obj_commit_update(struct nft_trans *trans)
8443 {
8444 struct nft_object *newobj;
8445 struct nft_object *obj;
8446
8447 obj = nft_trans_obj(trans);
8448 newobj = nft_trans_obj_newobj(trans);
8449
8450 if (obj->ops->update)
8451 obj->ops->update(obj, newobj);
8452
8453 nft_obj_destroy(&trans->ctx, newobj);
8454 }
8455
nft_commit_release(struct nft_trans * trans)8456 static void nft_commit_release(struct nft_trans *trans)
8457 {
8458 switch (trans->msg_type) {
8459 case NFT_MSG_DELTABLE:
8460 nf_tables_table_destroy(&trans->ctx);
8461 break;
8462 case NFT_MSG_NEWCHAIN:
8463 free_percpu(nft_trans_chain_stats(trans));
8464 kfree(nft_trans_chain_name(trans));
8465 break;
8466 case NFT_MSG_DELCHAIN:
8467 nf_tables_chain_destroy(&trans->ctx);
8468 break;
8469 case NFT_MSG_DELRULE:
8470 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
8471 break;
8472 case NFT_MSG_DELSET:
8473 nft_set_destroy(&trans->ctx, nft_trans_set(trans));
8474 break;
8475 case NFT_MSG_DELSETELEM:
8476 nf_tables_set_elem_destroy(&trans->ctx,
8477 nft_trans_elem_set(trans),
8478 nft_trans_elem(trans).priv);
8479 break;
8480 case NFT_MSG_DELOBJ:
8481 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
8482 break;
8483 case NFT_MSG_DELFLOWTABLE:
8484 if (nft_trans_flowtable_update(trans))
8485 nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans));
8486 else
8487 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
8488 break;
8489 }
8490
8491 if (trans->put_net)
8492 put_net(trans->ctx.net);
8493
8494 kfree(trans);
8495 }
8496
nf_tables_trans_destroy_work(struct work_struct * w)8497 static void nf_tables_trans_destroy_work(struct work_struct *w)
8498 {
8499 struct nft_trans *trans, *next;
8500 LIST_HEAD(head);
8501
8502 spin_lock(&nf_tables_destroy_list_lock);
8503 list_splice_init(&nf_tables_destroy_list, &head);
8504 spin_unlock(&nf_tables_destroy_list_lock);
8505
8506 if (list_empty(&head))
8507 return;
8508
8509 synchronize_rcu();
8510
8511 list_for_each_entry_safe(trans, next, &head, list) {
8512 list_del(&trans->list);
8513 nft_commit_release(trans);
8514 }
8515 }
8516
nf_tables_trans_destroy_flush_work(void)8517 void nf_tables_trans_destroy_flush_work(void)
8518 {
8519 flush_work(&trans_destroy_work);
8520 }
8521 EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work);
8522
nft_expr_reduce(struct nft_regs_track * track,const struct nft_expr * expr)8523 static bool nft_expr_reduce(struct nft_regs_track *track,
8524 const struct nft_expr *expr)
8525 {
8526 return false;
8527 }
8528
nf_tables_commit_chain_prepare(struct net * net,struct nft_chain * chain)8529 static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain)
8530 {
8531 const struct nft_expr *expr, *last;
8532 struct nft_regs_track track = {};
8533 unsigned int size, data_size;
8534 void *data, *data_boundary;
8535 struct nft_rule_dp *prule;
8536 struct nft_rule *rule;
8537
8538 /* already handled or inactive chain? */
8539 if (chain->blob_next || !nft_is_active_next(net, chain))
8540 return 0;
8541
8542 data_size = 0;
8543 list_for_each_entry(rule, &chain->rules, list) {
8544 if (nft_is_active_next(net, rule)) {
8545 data_size += sizeof(*prule) + rule->dlen;
8546 if (data_size > INT_MAX)
8547 return -ENOMEM;
8548 }
8549 }
8550 data_size += offsetof(struct nft_rule_dp, data); /* last rule */
8551
8552 chain->blob_next = nf_tables_chain_alloc_rules(data_size);
8553 if (!chain->blob_next)
8554 return -ENOMEM;
8555
8556 data = (void *)chain->blob_next->data;
8557 data_boundary = data + data_size;
8558 size = 0;
8559
8560 list_for_each_entry(rule, &chain->rules, list) {
8561 if (!nft_is_active_next(net, rule))
8562 continue;
8563
8564 prule = (struct nft_rule_dp *)data;
8565 data += offsetof(struct nft_rule_dp, data);
8566 if (WARN_ON_ONCE(data > data_boundary))
8567 return -ENOMEM;
8568
8569 size = 0;
8570 track.last = nft_expr_last(rule);
8571 nft_rule_for_each_expr(expr, last, rule) {
8572 track.cur = expr;
8573
8574 if (nft_expr_reduce(&track, expr)) {
8575 expr = track.cur;
8576 continue;
8577 }
8578
8579 if (WARN_ON_ONCE(data + expr->ops->size > data_boundary))
8580 return -ENOMEM;
8581
8582 memcpy(data + size, expr, expr->ops->size);
8583 size += expr->ops->size;
8584 }
8585 if (WARN_ON_ONCE(size >= 1 << 12))
8586 return -ENOMEM;
8587
8588 prule->handle = rule->handle;
8589 prule->dlen = size;
8590 prule->is_last = 0;
8591
8592 data += size;
8593 size = 0;
8594 chain->blob_next->size += (unsigned long)(data - (void *)prule);
8595 }
8596
8597 prule = (struct nft_rule_dp *)data;
8598 data += offsetof(struct nft_rule_dp, data);
8599 if (WARN_ON_ONCE(data > data_boundary))
8600 return -ENOMEM;
8601
8602 nft_last_rule(chain->blob_next, prule);
8603
8604 return 0;
8605 }
8606
nf_tables_commit_chain_prepare_cancel(struct net * net)8607 static void nf_tables_commit_chain_prepare_cancel(struct net *net)
8608 {
8609 struct nftables_pernet *nft_net = nft_pernet(net);
8610 struct nft_trans *trans, *next;
8611
8612 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
8613 struct nft_chain *chain = trans->ctx.chain;
8614
8615 if (trans->msg_type == NFT_MSG_NEWRULE ||
8616 trans->msg_type == NFT_MSG_DELRULE) {
8617 kvfree(chain->blob_next);
8618 chain->blob_next = NULL;
8619 }
8620 }
8621 }
8622
__nf_tables_commit_chain_free_rules_old(struct rcu_head * h)8623 static void __nf_tables_commit_chain_free_rules_old(struct rcu_head *h)
8624 {
8625 struct nft_rules_old *o = container_of(h, struct nft_rules_old, h);
8626
8627 kvfree(o->blob);
8628 }
8629
nf_tables_commit_chain_free_rules_old(struct nft_rule_blob * blob)8630 static void nf_tables_commit_chain_free_rules_old(struct nft_rule_blob *blob)
8631 {
8632 struct nft_rules_old *old;
8633
8634 /* rcu_head is after end marker */
8635 old = (void *)blob + sizeof(*blob) + blob->size;
8636 old->blob = blob;
8637
8638 call_rcu(&old->h, __nf_tables_commit_chain_free_rules_old);
8639 }
8640
nf_tables_commit_chain(struct net * net,struct nft_chain * chain)8641 static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain)
8642 {
8643 struct nft_rule_blob *g0, *g1;
8644 bool next_genbit;
8645
8646 next_genbit = nft_gencursor_next(net);
8647
8648 g0 = rcu_dereference_protected(chain->blob_gen_0,
8649 lockdep_commit_lock_is_held(net));
8650 g1 = rcu_dereference_protected(chain->blob_gen_1,
8651 lockdep_commit_lock_is_held(net));
8652
8653 /* No changes to this chain? */
8654 if (chain->blob_next == NULL) {
8655 /* chain had no change in last or next generation */
8656 if (g0 == g1)
8657 return;
8658 /*
8659 * chain had no change in this generation; make sure next
8660 * one uses same rules as current generation.
8661 */
8662 if (next_genbit) {
8663 rcu_assign_pointer(chain->blob_gen_1, g0);
8664 nf_tables_commit_chain_free_rules_old(g1);
8665 } else {
8666 rcu_assign_pointer(chain->blob_gen_0, g1);
8667 nf_tables_commit_chain_free_rules_old(g0);
8668 }
8669
8670 return;
8671 }
8672
8673 if (next_genbit)
8674 rcu_assign_pointer(chain->blob_gen_1, chain->blob_next);
8675 else
8676 rcu_assign_pointer(chain->blob_gen_0, chain->blob_next);
8677
8678 chain->blob_next = NULL;
8679
8680 if (g0 == g1)
8681 return;
8682
8683 if (next_genbit)
8684 nf_tables_commit_chain_free_rules_old(g1);
8685 else
8686 nf_tables_commit_chain_free_rules_old(g0);
8687 }
8688
nft_obj_del(struct nft_object * obj)8689 static void nft_obj_del(struct nft_object *obj)
8690 {
8691 rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params);
8692 list_del_rcu(&obj->list);
8693 }
8694
nft_chain_del(struct nft_chain * chain)8695 void nft_chain_del(struct nft_chain *chain)
8696 {
8697 struct nft_table *table = chain->table;
8698
8699 WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead,
8700 nft_chain_ht_params));
8701 list_del_rcu(&chain->list);
8702 }
8703
nf_tables_module_autoload_cleanup(struct net * net)8704 static void nf_tables_module_autoload_cleanup(struct net *net)
8705 {
8706 struct nftables_pernet *nft_net = nft_pernet(net);
8707 struct nft_module_request *req, *next;
8708
8709 WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
8710 list_for_each_entry_safe(req, next, &nft_net->module_list, list) {
8711 WARN_ON_ONCE(!req->done);
8712 list_del(&req->list);
8713 kfree(req);
8714 }
8715 }
8716
nf_tables_commit_release(struct net * net)8717 static void nf_tables_commit_release(struct net *net)
8718 {
8719 struct nftables_pernet *nft_net = nft_pernet(net);
8720 struct nft_trans *trans;
8721
8722 /* all side effects have to be made visible.
8723 * For example, if a chain named 'foo' has been deleted, a
8724 * new transaction must not find it anymore.
8725 *
8726 * Memory reclaim happens asynchronously from work queue
8727 * to prevent expensive synchronize_rcu() in commit phase.
8728 */
8729 if (list_empty(&nft_net->commit_list)) {
8730 nf_tables_module_autoload_cleanup(net);
8731 mutex_unlock(&nft_net->commit_mutex);
8732 return;
8733 }
8734
8735 trans = list_last_entry(&nft_net->commit_list,
8736 struct nft_trans, list);
8737 get_net(trans->ctx.net);
8738 WARN_ON_ONCE(trans->put_net);
8739
8740 trans->put_net = true;
8741 spin_lock(&nf_tables_destroy_list_lock);
8742 list_splice_tail_init(&nft_net->commit_list, &nf_tables_destroy_list);
8743 spin_unlock(&nf_tables_destroy_list_lock);
8744
8745 nf_tables_module_autoload_cleanup(net);
8746 schedule_work(&trans_destroy_work);
8747
8748 mutex_unlock(&nft_net->commit_mutex);
8749 }
8750
nft_commit_notify(struct net * net,u32 portid)8751 static void nft_commit_notify(struct net *net, u32 portid)
8752 {
8753 struct nftables_pernet *nft_net = nft_pernet(net);
8754 struct sk_buff *batch_skb = NULL, *nskb, *skb;
8755 unsigned char *data;
8756 int len;
8757
8758 list_for_each_entry_safe(skb, nskb, &nft_net->notify_list, list) {
8759 if (!batch_skb) {
8760 new_batch:
8761 batch_skb = skb;
8762 len = NLMSG_GOODSIZE - skb->len;
8763 list_del(&skb->list);
8764 continue;
8765 }
8766 len -= skb->len;
8767 if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) {
8768 data = skb_put(batch_skb, skb->len);
8769 memcpy(data, skb->data, skb->len);
8770 list_del(&skb->list);
8771 kfree_skb(skb);
8772 continue;
8773 }
8774 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
8775 NFT_CB(batch_skb).report, GFP_KERNEL);
8776 goto new_batch;
8777 }
8778
8779 if (batch_skb) {
8780 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
8781 NFT_CB(batch_skb).report, GFP_KERNEL);
8782 }
8783
8784 WARN_ON_ONCE(!list_empty(&nft_net->notify_list));
8785 }
8786
nf_tables_commit_audit_alloc(struct list_head * adl,struct nft_table * table)8787 static int nf_tables_commit_audit_alloc(struct list_head *adl,
8788 struct nft_table *table)
8789 {
8790 struct nft_audit_data *adp;
8791
8792 list_for_each_entry(adp, adl, list) {
8793 if (adp->table == table)
8794 return 0;
8795 }
8796 adp = kzalloc(sizeof(*adp), GFP_KERNEL);
8797 if (!adp)
8798 return -ENOMEM;
8799 adp->table = table;
8800 list_add(&adp->list, adl);
8801 return 0;
8802 }
8803
nf_tables_commit_audit_free(struct list_head * adl)8804 static void nf_tables_commit_audit_free(struct list_head *adl)
8805 {
8806 struct nft_audit_data *adp, *adn;
8807
8808 list_for_each_entry_safe(adp, adn, adl, list) {
8809 list_del(&adp->list);
8810 kfree(adp);
8811 }
8812 }
8813
nf_tables_commit_audit_collect(struct list_head * adl,struct nft_table * table,u32 op)8814 static void nf_tables_commit_audit_collect(struct list_head *adl,
8815 struct nft_table *table, u32 op)
8816 {
8817 struct nft_audit_data *adp;
8818
8819 list_for_each_entry(adp, adl, list) {
8820 if (adp->table == table)
8821 goto found;
8822 }
8823 WARN_ONCE(1, "table=%s not expected in commit list", table->name);
8824 return;
8825 found:
8826 adp->entries++;
8827 if (!adp->op || adp->op > op)
8828 adp->op = op;
8829 }
8830
8831 #define AUNFTABLENAMELEN (NFT_TABLE_MAXNAMELEN + 22)
8832
nf_tables_commit_audit_log(struct list_head * adl,u32 generation)8833 static void nf_tables_commit_audit_log(struct list_head *adl, u32 generation)
8834 {
8835 struct nft_audit_data *adp, *adn;
8836 char aubuf[AUNFTABLENAMELEN];
8837
8838 list_for_each_entry_safe(adp, adn, adl, list) {
8839 snprintf(aubuf, AUNFTABLENAMELEN, "%s:%u", adp->table->name,
8840 generation);
8841 audit_log_nfcfg(aubuf, adp->table->family, adp->entries,
8842 nft2audit_op[adp->op], GFP_KERNEL);
8843 list_del(&adp->list);
8844 kfree(adp);
8845 }
8846 }
8847
nf_tables_commit(struct net * net,struct sk_buff * skb)8848 static int nf_tables_commit(struct net *net, struct sk_buff *skb)
8849 {
8850 struct nftables_pernet *nft_net = nft_pernet(net);
8851 struct nft_trans *trans, *next;
8852 struct nft_trans_elem *te;
8853 struct nft_chain *chain;
8854 struct nft_table *table;
8855 unsigned int base_seq;
8856 LIST_HEAD(adl);
8857 int err;
8858
8859 if (list_empty(&nft_net->commit_list)) {
8860 mutex_unlock(&nft_net->commit_mutex);
8861 return 0;
8862 }
8863
8864 /* 0. Validate ruleset, otherwise roll back for error reporting. */
8865 if (nf_tables_validate(net) < 0)
8866 return -EAGAIN;
8867
8868 err = nft_flow_rule_offload_commit(net);
8869 if (err < 0)
8870 return err;
8871
8872 /* 1. Allocate space for next generation rules_gen_X[] */
8873 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
8874 int ret;
8875
8876 ret = nf_tables_commit_audit_alloc(&adl, trans->ctx.table);
8877 if (ret) {
8878 nf_tables_commit_chain_prepare_cancel(net);
8879 nf_tables_commit_audit_free(&adl);
8880 return ret;
8881 }
8882 if (trans->msg_type == NFT_MSG_NEWRULE ||
8883 trans->msg_type == NFT_MSG_DELRULE) {
8884 chain = trans->ctx.chain;
8885
8886 ret = nf_tables_commit_chain_prepare(net, chain);
8887 if (ret < 0) {
8888 nf_tables_commit_chain_prepare_cancel(net);
8889 nf_tables_commit_audit_free(&adl);
8890 return ret;
8891 }
8892 }
8893 }
8894
8895 /* step 2. Make rules_gen_X visible to packet path */
8896 list_for_each_entry(table, &nft_net->tables, list) {
8897 list_for_each_entry(chain, &table->chains, list)
8898 nf_tables_commit_chain(net, chain);
8899 }
8900
8901 /*
8902 * Bump generation counter, invalidate any dump in progress.
8903 * Cannot fail after this point.
8904 */
8905 base_seq = READ_ONCE(nft_net->base_seq);
8906 while (++base_seq == 0)
8907 ;
8908
8909 WRITE_ONCE(nft_net->base_seq, base_seq);
8910
8911 /* step 3. Start new generation, rules_gen_X now in use. */
8912 net->nft.gencursor = nft_gencursor_next(net);
8913
8914 list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) {
8915 nf_tables_commit_audit_collect(&adl, trans->ctx.table,
8916 trans->msg_type);
8917 switch (trans->msg_type) {
8918 case NFT_MSG_NEWTABLE:
8919 if (nft_trans_table_update(trans)) {
8920 if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) {
8921 nft_trans_destroy(trans);
8922 break;
8923 }
8924 if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT)
8925 nf_tables_table_disable(net, trans->ctx.table);
8926
8927 trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE;
8928 } else {
8929 nft_clear(net, trans->ctx.table);
8930 }
8931 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
8932 nft_trans_destroy(trans);
8933 break;
8934 case NFT_MSG_DELTABLE:
8935 list_del_rcu(&trans->ctx.table->list);
8936 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
8937 break;
8938 case NFT_MSG_NEWCHAIN:
8939 if (nft_trans_chain_update(trans)) {
8940 nft_chain_commit_update(trans);
8941 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
8942 /* trans destroyed after rcu grace period */
8943 } else {
8944 nft_chain_commit_drop_policy(trans);
8945 nft_clear(net, trans->ctx.chain);
8946 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
8947 nft_trans_destroy(trans);
8948 }
8949 break;
8950 case NFT_MSG_DELCHAIN:
8951 nft_chain_del(trans->ctx.chain);
8952 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
8953 nf_tables_unregister_hook(trans->ctx.net,
8954 trans->ctx.table,
8955 trans->ctx.chain);
8956 break;
8957 case NFT_MSG_NEWRULE:
8958 nft_clear(trans->ctx.net, nft_trans_rule(trans));
8959 nf_tables_rule_notify(&trans->ctx,
8960 nft_trans_rule(trans),
8961 NFT_MSG_NEWRULE);
8962 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
8963 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
8964
8965 nft_trans_destroy(trans);
8966 break;
8967 case NFT_MSG_DELRULE:
8968 list_del_rcu(&nft_trans_rule(trans)->list);
8969 nf_tables_rule_notify(&trans->ctx,
8970 nft_trans_rule(trans),
8971 NFT_MSG_DELRULE);
8972 nft_rule_expr_deactivate(&trans->ctx,
8973 nft_trans_rule(trans),
8974 NFT_TRANS_COMMIT);
8975
8976 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
8977 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
8978 break;
8979 case NFT_MSG_NEWSET:
8980 nft_clear(net, nft_trans_set(trans));
8981 /* This avoids hitting -EBUSY when deleting the table
8982 * from the transaction.
8983 */
8984 if (nft_set_is_anonymous(nft_trans_set(trans)) &&
8985 !list_empty(&nft_trans_set(trans)->bindings))
8986 trans->ctx.table->use--;
8987
8988 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
8989 NFT_MSG_NEWSET, GFP_KERNEL);
8990 nft_trans_destroy(trans);
8991 break;
8992 case NFT_MSG_DELSET:
8993 list_del_rcu(&nft_trans_set(trans)->list);
8994 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
8995 NFT_MSG_DELSET, GFP_KERNEL);
8996 break;
8997 case NFT_MSG_NEWSETELEM:
8998 te = (struct nft_trans_elem *)trans->data;
8999
9000 nft_setelem_activate(net, te->set, &te->elem);
9001 nf_tables_setelem_notify(&trans->ctx, te->set,
9002 &te->elem,
9003 NFT_MSG_NEWSETELEM);
9004 nft_trans_destroy(trans);
9005 break;
9006 case NFT_MSG_DELSETELEM:
9007 te = (struct nft_trans_elem *)trans->data;
9008
9009 nf_tables_setelem_notify(&trans->ctx, te->set,
9010 &te->elem,
9011 NFT_MSG_DELSETELEM);
9012 nft_setelem_remove(net, te->set, &te->elem);
9013 if (!nft_setelem_is_catchall(te->set, &te->elem)) {
9014 atomic_dec(&te->set->nelems);
9015 te->set->ndeact--;
9016 }
9017 break;
9018 case NFT_MSG_NEWOBJ:
9019 if (nft_trans_obj_update(trans)) {
9020 nft_obj_commit_update(trans);
9021 nf_tables_obj_notify(&trans->ctx,
9022 nft_trans_obj(trans),
9023 NFT_MSG_NEWOBJ);
9024 } else {
9025 nft_clear(net, nft_trans_obj(trans));
9026 nf_tables_obj_notify(&trans->ctx,
9027 nft_trans_obj(trans),
9028 NFT_MSG_NEWOBJ);
9029 nft_trans_destroy(trans);
9030 }
9031 break;
9032 case NFT_MSG_DELOBJ:
9033 nft_obj_del(nft_trans_obj(trans));
9034 nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
9035 NFT_MSG_DELOBJ);
9036 break;
9037 case NFT_MSG_NEWFLOWTABLE:
9038 if (nft_trans_flowtable_update(trans)) {
9039 nft_trans_flowtable(trans)->data.flags =
9040 nft_trans_flowtable_flags(trans);
9041 nf_tables_flowtable_notify(&trans->ctx,
9042 nft_trans_flowtable(trans),
9043 &nft_trans_flowtable_hooks(trans),
9044 NFT_MSG_NEWFLOWTABLE);
9045 list_splice(&nft_trans_flowtable_hooks(trans),
9046 &nft_trans_flowtable(trans)->hook_list);
9047 } else {
9048 nft_clear(net, nft_trans_flowtable(trans));
9049 nf_tables_flowtable_notify(&trans->ctx,
9050 nft_trans_flowtable(trans),
9051 &nft_trans_flowtable(trans)->hook_list,
9052 NFT_MSG_NEWFLOWTABLE);
9053 }
9054 nft_trans_destroy(trans);
9055 break;
9056 case NFT_MSG_DELFLOWTABLE:
9057 if (nft_trans_flowtable_update(trans)) {
9058 nf_tables_flowtable_notify(&trans->ctx,
9059 nft_trans_flowtable(trans),
9060 &nft_trans_flowtable_hooks(trans),
9061 NFT_MSG_DELFLOWTABLE);
9062 nft_unregister_flowtable_net_hooks(net,
9063 &nft_trans_flowtable_hooks(trans));
9064 } else {
9065 list_del_rcu(&nft_trans_flowtable(trans)->list);
9066 nf_tables_flowtable_notify(&trans->ctx,
9067 nft_trans_flowtable(trans),
9068 &nft_trans_flowtable(trans)->hook_list,
9069 NFT_MSG_DELFLOWTABLE);
9070 nft_unregister_flowtable_net_hooks(net,
9071 &nft_trans_flowtable(trans)->hook_list);
9072 }
9073 break;
9074 }
9075 }
9076
9077 nft_commit_notify(net, NETLINK_CB(skb).portid);
9078 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
9079 nf_tables_commit_audit_log(&adl, nft_net->base_seq);
9080 nf_tables_commit_release(net);
9081
9082 return 0;
9083 }
9084
nf_tables_module_autoload(struct net * net)9085 static void nf_tables_module_autoload(struct net *net)
9086 {
9087 struct nftables_pernet *nft_net = nft_pernet(net);
9088 struct nft_module_request *req, *next;
9089 LIST_HEAD(module_list);
9090
9091 list_splice_init(&nft_net->module_list, &module_list);
9092 mutex_unlock(&nft_net->commit_mutex);
9093 list_for_each_entry_safe(req, next, &module_list, list) {
9094 request_module("%s", req->module);
9095 req->done = true;
9096 }
9097 mutex_lock(&nft_net->commit_mutex);
9098 list_splice(&module_list, &nft_net->module_list);
9099 }
9100
nf_tables_abort_release(struct nft_trans * trans)9101 static void nf_tables_abort_release(struct nft_trans *trans)
9102 {
9103 switch (trans->msg_type) {
9104 case NFT_MSG_NEWTABLE:
9105 nf_tables_table_destroy(&trans->ctx);
9106 break;
9107 case NFT_MSG_NEWCHAIN:
9108 nf_tables_chain_destroy(&trans->ctx);
9109 break;
9110 case NFT_MSG_NEWRULE:
9111 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
9112 break;
9113 case NFT_MSG_NEWSET:
9114 nft_set_destroy(&trans->ctx, nft_trans_set(trans));
9115 break;
9116 case NFT_MSG_NEWSETELEM:
9117 nft_set_elem_destroy(nft_trans_elem_set(trans),
9118 nft_trans_elem(trans).priv, true);
9119 break;
9120 case NFT_MSG_NEWOBJ:
9121 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
9122 break;
9123 case NFT_MSG_NEWFLOWTABLE:
9124 if (nft_trans_flowtable_update(trans))
9125 nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans));
9126 else
9127 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
9128 break;
9129 }
9130 kfree(trans);
9131 }
9132
__nf_tables_abort(struct net * net,enum nfnl_abort_action action)9133 static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
9134 {
9135 struct nftables_pernet *nft_net = nft_pernet(net);
9136 struct nft_trans *trans, *next;
9137 struct nft_trans_elem *te;
9138
9139 if (action == NFNL_ABORT_VALIDATE &&
9140 nf_tables_validate(net) < 0)
9141 return -EAGAIN;
9142
9143 list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list,
9144 list) {
9145 switch (trans->msg_type) {
9146 case NFT_MSG_NEWTABLE:
9147 if (nft_trans_table_update(trans)) {
9148 if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) {
9149 nft_trans_destroy(trans);
9150 break;
9151 }
9152 if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_DORMANT) {
9153 nf_tables_table_disable(net, trans->ctx.table);
9154 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
9155 } else if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_AWAKEN) {
9156 trans->ctx.table->flags &= ~NFT_TABLE_F_DORMANT;
9157 }
9158 trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE;
9159 nft_trans_destroy(trans);
9160 } else {
9161 list_del_rcu(&trans->ctx.table->list);
9162 }
9163 break;
9164 case NFT_MSG_DELTABLE:
9165 nft_clear(trans->ctx.net, trans->ctx.table);
9166 nft_trans_destroy(trans);
9167 break;
9168 case NFT_MSG_NEWCHAIN:
9169 if (nft_trans_chain_update(trans)) {
9170 free_percpu(nft_trans_chain_stats(trans));
9171 kfree(nft_trans_chain_name(trans));
9172 nft_trans_destroy(trans);
9173 } else {
9174 if (nft_chain_is_bound(trans->ctx.chain)) {
9175 nft_trans_destroy(trans);
9176 break;
9177 }
9178 trans->ctx.table->use--;
9179 nft_chain_del(trans->ctx.chain);
9180 nf_tables_unregister_hook(trans->ctx.net,
9181 trans->ctx.table,
9182 trans->ctx.chain);
9183 }
9184 break;
9185 case NFT_MSG_DELCHAIN:
9186 trans->ctx.table->use++;
9187 nft_clear(trans->ctx.net, trans->ctx.chain);
9188 nft_trans_destroy(trans);
9189 break;
9190 case NFT_MSG_NEWRULE:
9191 trans->ctx.chain->use--;
9192 list_del_rcu(&nft_trans_rule(trans)->list);
9193 nft_rule_expr_deactivate(&trans->ctx,
9194 nft_trans_rule(trans),
9195 NFT_TRANS_ABORT);
9196 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
9197 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
9198 break;
9199 case NFT_MSG_DELRULE:
9200 trans->ctx.chain->use++;
9201 nft_clear(trans->ctx.net, nft_trans_rule(trans));
9202 nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans));
9203 if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD)
9204 nft_flow_rule_destroy(nft_trans_flow_rule(trans));
9205
9206 nft_trans_destroy(trans);
9207 break;
9208 case NFT_MSG_NEWSET:
9209 trans->ctx.table->use--;
9210 if (nft_trans_set_bound(trans)) {
9211 nft_trans_destroy(trans);
9212 break;
9213 }
9214 list_del_rcu(&nft_trans_set(trans)->list);
9215 break;
9216 case NFT_MSG_DELSET:
9217 trans->ctx.table->use++;
9218 nft_clear(trans->ctx.net, nft_trans_set(trans));
9219 nft_trans_destroy(trans);
9220 break;
9221 case NFT_MSG_NEWSETELEM:
9222 if (nft_trans_elem_set_bound(trans)) {
9223 nft_trans_destroy(trans);
9224 break;
9225 }
9226 te = (struct nft_trans_elem *)trans->data;
9227 nft_setelem_remove(net, te->set, &te->elem);
9228 if (!nft_setelem_is_catchall(te->set, &te->elem))
9229 atomic_dec(&te->set->nelems);
9230 break;
9231 case NFT_MSG_DELSETELEM:
9232 te = (struct nft_trans_elem *)trans->data;
9233
9234 nft_setelem_data_activate(net, te->set, &te->elem);
9235 nft_setelem_activate(net, te->set, &te->elem);
9236 if (!nft_setelem_is_catchall(te->set, &te->elem))
9237 te->set->ndeact--;
9238
9239 nft_trans_destroy(trans);
9240 break;
9241 case NFT_MSG_NEWOBJ:
9242 if (nft_trans_obj_update(trans)) {
9243 nft_obj_destroy(&trans->ctx, nft_trans_obj_newobj(trans));
9244 nft_trans_destroy(trans);
9245 } else {
9246 trans->ctx.table->use--;
9247 nft_obj_del(nft_trans_obj(trans));
9248 }
9249 break;
9250 case NFT_MSG_DELOBJ:
9251 trans->ctx.table->use++;
9252 nft_clear(trans->ctx.net, nft_trans_obj(trans));
9253 nft_trans_destroy(trans);
9254 break;
9255 case NFT_MSG_NEWFLOWTABLE:
9256 if (nft_trans_flowtable_update(trans)) {
9257 nft_unregister_flowtable_net_hooks(net,
9258 &nft_trans_flowtable_hooks(trans));
9259 } else {
9260 trans->ctx.table->use--;
9261 list_del_rcu(&nft_trans_flowtable(trans)->list);
9262 nft_unregister_flowtable_net_hooks(net,
9263 &nft_trans_flowtable(trans)->hook_list);
9264 }
9265 break;
9266 case NFT_MSG_DELFLOWTABLE:
9267 if (nft_trans_flowtable_update(trans)) {
9268 list_splice(&nft_trans_flowtable_hooks(trans),
9269 &nft_trans_flowtable(trans)->hook_list);
9270 } else {
9271 trans->ctx.table->use++;
9272 nft_clear(trans->ctx.net, nft_trans_flowtable(trans));
9273 }
9274 nft_trans_destroy(trans);
9275 break;
9276 }
9277 }
9278
9279 synchronize_rcu();
9280
9281 list_for_each_entry_safe_reverse(trans, next,
9282 &nft_net->commit_list, list) {
9283 list_del(&trans->list);
9284 nf_tables_abort_release(trans);
9285 }
9286
9287 if (action == NFNL_ABORT_AUTOLOAD)
9288 nf_tables_module_autoload(net);
9289 else
9290 nf_tables_module_autoload_cleanup(net);
9291
9292 return 0;
9293 }
9294
nf_tables_cleanup(struct net * net)9295 static void nf_tables_cleanup(struct net *net)
9296 {
9297 nft_validate_state_update(net, NFT_VALIDATE_SKIP);
9298 }
9299
nf_tables_abort(struct net * net,struct sk_buff * skb,enum nfnl_abort_action action)9300 static int nf_tables_abort(struct net *net, struct sk_buff *skb,
9301 enum nfnl_abort_action action)
9302 {
9303 struct nftables_pernet *nft_net = nft_pernet(net);
9304 int ret = __nf_tables_abort(net, action);
9305
9306 mutex_unlock(&nft_net->commit_mutex);
9307
9308 return ret;
9309 }
9310
nf_tables_valid_genid(struct net * net,u32 genid)9311 static bool nf_tables_valid_genid(struct net *net, u32 genid)
9312 {
9313 struct nftables_pernet *nft_net = nft_pernet(net);
9314 bool genid_ok;
9315
9316 mutex_lock(&nft_net->commit_mutex);
9317
9318 genid_ok = genid == 0 || nft_net->base_seq == genid;
9319 if (!genid_ok)
9320 mutex_unlock(&nft_net->commit_mutex);
9321
9322 /* else, commit mutex has to be released by commit or abort function */
9323 return genid_ok;
9324 }
9325
9326 static const struct nfnetlink_subsystem nf_tables_subsys = {
9327 .name = "nf_tables",
9328 .subsys_id = NFNL_SUBSYS_NFTABLES,
9329 .cb_count = NFT_MSG_MAX,
9330 .cb = nf_tables_cb,
9331 .commit = nf_tables_commit,
9332 .abort = nf_tables_abort,
9333 .cleanup = nf_tables_cleanup,
9334 .valid_genid = nf_tables_valid_genid,
9335 .owner = THIS_MODULE,
9336 };
9337
nft_chain_validate_dependency(const struct nft_chain * chain,enum nft_chain_types type)9338 int nft_chain_validate_dependency(const struct nft_chain *chain,
9339 enum nft_chain_types type)
9340 {
9341 const struct nft_base_chain *basechain;
9342
9343 if (nft_is_base_chain(chain)) {
9344 basechain = nft_base_chain(chain);
9345 if (basechain->type->type != type)
9346 return -EOPNOTSUPP;
9347 }
9348 return 0;
9349 }
9350 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
9351
nft_chain_validate_hooks(const struct nft_chain * chain,unsigned int hook_flags)9352 int nft_chain_validate_hooks(const struct nft_chain *chain,
9353 unsigned int hook_flags)
9354 {
9355 struct nft_base_chain *basechain;
9356
9357 if (nft_is_base_chain(chain)) {
9358 basechain = nft_base_chain(chain);
9359
9360 if ((1 << basechain->ops.hooknum) & hook_flags)
9361 return 0;
9362
9363 return -EOPNOTSUPP;
9364 }
9365
9366 return 0;
9367 }
9368 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
9369
9370 /*
9371 * Loop detection - walk through the ruleset beginning at the destination chain
9372 * of a new jump until either the source chain is reached (loop) or all
9373 * reachable chains have been traversed.
9374 *
9375 * The loop check is performed whenever a new jump verdict is added to an
9376 * expression or verdict map or a verdict map is bound to a new chain.
9377 */
9378
9379 static int nf_tables_check_loops(const struct nft_ctx *ctx,
9380 const struct nft_chain *chain);
9381
nft_check_loops(const struct nft_ctx * ctx,const struct nft_set_ext * ext)9382 static int nft_check_loops(const struct nft_ctx *ctx,
9383 const struct nft_set_ext *ext)
9384 {
9385 const struct nft_data *data;
9386 int ret;
9387
9388 data = nft_set_ext_data(ext);
9389 switch (data->verdict.code) {
9390 case NFT_JUMP:
9391 case NFT_GOTO:
9392 ret = nf_tables_check_loops(ctx, data->verdict.chain);
9393 break;
9394 default:
9395 ret = 0;
9396 break;
9397 }
9398
9399 return ret;
9400 }
9401
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)9402 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
9403 struct nft_set *set,
9404 const struct nft_set_iter *iter,
9405 struct nft_set_elem *elem)
9406 {
9407 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
9408
9409 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
9410 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
9411 return 0;
9412
9413 return nft_check_loops(ctx, ext);
9414 }
9415
nft_set_catchall_loops(const struct nft_ctx * ctx,struct nft_set * set)9416 static int nft_set_catchall_loops(const struct nft_ctx *ctx,
9417 struct nft_set *set)
9418 {
9419 u8 genmask = nft_genmask_next(ctx->net);
9420 struct nft_set_elem_catchall *catchall;
9421 struct nft_set_ext *ext;
9422 int ret = 0;
9423
9424 list_for_each_entry_rcu(catchall, &set->catchall_list, list) {
9425 ext = nft_set_elem_ext(set, catchall->elem);
9426 if (!nft_set_elem_active(ext, genmask))
9427 continue;
9428
9429 ret = nft_check_loops(ctx, ext);
9430 if (ret < 0)
9431 return ret;
9432 }
9433
9434 return ret;
9435 }
9436
nf_tables_check_loops(const struct nft_ctx * ctx,const struct nft_chain * chain)9437 static int nf_tables_check_loops(const struct nft_ctx *ctx,
9438 const struct nft_chain *chain)
9439 {
9440 const struct nft_rule *rule;
9441 const struct nft_expr *expr, *last;
9442 struct nft_set *set;
9443 struct nft_set_binding *binding;
9444 struct nft_set_iter iter;
9445
9446 if (ctx->chain == chain)
9447 return -ELOOP;
9448
9449 list_for_each_entry(rule, &chain->rules, list) {
9450 nft_rule_for_each_expr(expr, last, rule) {
9451 struct nft_immediate_expr *priv;
9452 const struct nft_data *data;
9453 int err;
9454
9455 if (strcmp(expr->ops->type->name, "immediate"))
9456 continue;
9457
9458 priv = nft_expr_priv(expr);
9459 if (priv->dreg != NFT_REG_VERDICT)
9460 continue;
9461
9462 data = &priv->data;
9463 switch (data->verdict.code) {
9464 case NFT_JUMP:
9465 case NFT_GOTO:
9466 err = nf_tables_check_loops(ctx,
9467 data->verdict.chain);
9468 if (err < 0)
9469 return err;
9470 break;
9471 default:
9472 break;
9473 }
9474 }
9475 }
9476
9477 list_for_each_entry(set, &ctx->table->sets, list) {
9478 if (!nft_is_active_next(ctx->net, set))
9479 continue;
9480 if (!(set->flags & NFT_SET_MAP) ||
9481 set->dtype != NFT_DATA_VERDICT)
9482 continue;
9483
9484 list_for_each_entry(binding, &set->bindings, list) {
9485 if (!(binding->flags & NFT_SET_MAP) ||
9486 binding->chain != chain)
9487 continue;
9488
9489 iter.genmask = nft_genmask_next(ctx->net);
9490 iter.skip = 0;
9491 iter.count = 0;
9492 iter.err = 0;
9493 iter.fn = nf_tables_loop_check_setelem;
9494
9495 set->ops->walk(ctx, set, &iter);
9496 if (!iter.err)
9497 iter.err = nft_set_catchall_loops(ctx, set);
9498
9499 if (iter.err < 0)
9500 return iter.err;
9501 }
9502 }
9503
9504 return 0;
9505 }
9506
9507 /**
9508 * nft_parse_u32_check - fetch u32 attribute and check for maximum value
9509 *
9510 * @attr: netlink attribute to fetch value from
9511 * @max: maximum value to be stored in dest
9512 * @dest: pointer to the variable
9513 *
9514 * Parse, check and store a given u32 netlink attribute into variable.
9515 * This function returns -ERANGE if the value goes over maximum value.
9516 * Otherwise a 0 is returned and the attribute value is stored in the
9517 * destination variable.
9518 */
nft_parse_u32_check(const struct nlattr * attr,int max,u32 * dest)9519 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest)
9520 {
9521 u32 val;
9522
9523 val = ntohl(nla_get_be32(attr));
9524 if (val > max)
9525 return -ERANGE;
9526
9527 *dest = val;
9528 return 0;
9529 }
9530 EXPORT_SYMBOL_GPL(nft_parse_u32_check);
9531
nft_parse_register(const struct nlattr * attr,u32 * preg)9532 static int nft_parse_register(const struct nlattr *attr, u32 *preg)
9533 {
9534 unsigned int reg;
9535
9536 reg = ntohl(nla_get_be32(attr));
9537 switch (reg) {
9538 case NFT_REG_VERDICT...NFT_REG_4:
9539 *preg = reg * NFT_REG_SIZE / NFT_REG32_SIZE;
9540 break;
9541 case NFT_REG32_00...NFT_REG32_15:
9542 *preg = reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
9543 break;
9544 default:
9545 return -ERANGE;
9546 }
9547
9548 return 0;
9549 }
9550
9551 /**
9552 * nft_dump_register - dump a register value to a netlink attribute
9553 *
9554 * @skb: socket buffer
9555 * @attr: attribute number
9556 * @reg: register number
9557 *
9558 * Construct a netlink attribute containing the register number. For
9559 * compatibility reasons, register numbers being a multiple of 4 are
9560 * translated to the corresponding 128 bit register numbers.
9561 */
nft_dump_register(struct sk_buff * skb,unsigned int attr,unsigned int reg)9562 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
9563 {
9564 if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
9565 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
9566 else
9567 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
9568
9569 return nla_put_be32(skb, attr, htonl(reg));
9570 }
9571 EXPORT_SYMBOL_GPL(nft_dump_register);
9572
nft_validate_register_load(enum nft_registers reg,unsigned int len)9573 static int nft_validate_register_load(enum nft_registers reg, unsigned int len)
9574 {
9575 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
9576 return -EINVAL;
9577 if (len == 0)
9578 return -EINVAL;
9579 if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data))
9580 return -ERANGE;
9581
9582 return 0;
9583 }
9584
nft_parse_register_load(const struct nlattr * attr,u8 * sreg,u32 len)9585 int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len)
9586 {
9587 u32 reg;
9588 int err;
9589
9590 err = nft_parse_register(attr, ®);
9591 if (err < 0)
9592 return err;
9593
9594 err = nft_validate_register_load(reg, len);
9595 if (err < 0)
9596 return err;
9597
9598 *sreg = reg;
9599 return 0;
9600 }
9601 EXPORT_SYMBOL_GPL(nft_parse_register_load);
9602
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)9603 static int nft_validate_register_store(const struct nft_ctx *ctx,
9604 enum nft_registers reg,
9605 const struct nft_data *data,
9606 enum nft_data_types type,
9607 unsigned int len)
9608 {
9609 int err;
9610
9611 switch (reg) {
9612 case NFT_REG_VERDICT:
9613 if (type != NFT_DATA_VERDICT)
9614 return -EINVAL;
9615
9616 if (data != NULL &&
9617 (data->verdict.code == NFT_GOTO ||
9618 data->verdict.code == NFT_JUMP)) {
9619 err = nf_tables_check_loops(ctx, data->verdict.chain);
9620 if (err < 0)
9621 return err;
9622 }
9623
9624 return 0;
9625 default:
9626 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
9627 return -EINVAL;
9628 if (len == 0)
9629 return -EINVAL;
9630 if (reg * NFT_REG32_SIZE + len >
9631 sizeof_field(struct nft_regs, data))
9632 return -ERANGE;
9633
9634 if (data != NULL && type != NFT_DATA_VALUE)
9635 return -EINVAL;
9636 return 0;
9637 }
9638 }
9639
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)9640 int nft_parse_register_store(const struct nft_ctx *ctx,
9641 const struct nlattr *attr, u8 *dreg,
9642 const struct nft_data *data,
9643 enum nft_data_types type, unsigned int len)
9644 {
9645 int err;
9646 u32 reg;
9647
9648 err = nft_parse_register(attr, ®);
9649 if (err < 0)
9650 return err;
9651
9652 err = nft_validate_register_store(ctx, reg, data, type, len);
9653 if (err < 0)
9654 return err;
9655
9656 *dreg = reg;
9657 return 0;
9658 }
9659 EXPORT_SYMBOL_GPL(nft_parse_register_store);
9660
9661 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
9662 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
9663 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
9664 .len = NFT_CHAIN_MAXNAMELEN - 1 },
9665 [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 },
9666 };
9667
nft_verdict_init(const struct nft_ctx * ctx,struct nft_data * data,struct nft_data_desc * desc,const struct nlattr * nla)9668 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
9669 struct nft_data_desc *desc, const struct nlattr *nla)
9670 {
9671 u8 genmask = nft_genmask_next(ctx->net);
9672 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
9673 struct nft_chain *chain;
9674 int err;
9675
9676 err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla,
9677 nft_verdict_policy, NULL);
9678 if (err < 0)
9679 return err;
9680
9681 if (!tb[NFTA_VERDICT_CODE])
9682 return -EINVAL;
9683 data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
9684
9685 switch (data->verdict.code) {
9686 default:
9687 switch (data->verdict.code & NF_VERDICT_MASK) {
9688 case NF_ACCEPT:
9689 case NF_DROP:
9690 case NF_QUEUE:
9691 break;
9692 default:
9693 return -EINVAL;
9694 }
9695 fallthrough;
9696 case NFT_CONTINUE:
9697 case NFT_BREAK:
9698 case NFT_RETURN:
9699 break;
9700 case NFT_JUMP:
9701 case NFT_GOTO:
9702 if (tb[NFTA_VERDICT_CHAIN]) {
9703 chain = nft_chain_lookup(ctx->net, ctx->table,
9704 tb[NFTA_VERDICT_CHAIN],
9705 genmask);
9706 } else if (tb[NFTA_VERDICT_CHAIN_ID]) {
9707 chain = nft_chain_lookup_byid(ctx->net, ctx->table,
9708 tb[NFTA_VERDICT_CHAIN_ID]);
9709 if (IS_ERR(chain))
9710 return PTR_ERR(chain);
9711 } else {
9712 return -EINVAL;
9713 }
9714
9715 if (IS_ERR(chain))
9716 return PTR_ERR(chain);
9717 if (nft_is_base_chain(chain))
9718 return -EOPNOTSUPP;
9719 if (nft_chain_is_bound(chain))
9720 return -EINVAL;
9721 if (desc->flags & NFT_DATA_DESC_SETELEM &&
9722 chain->flags & NFT_CHAIN_BINDING)
9723 return -EINVAL;
9724
9725 chain->use++;
9726 data->verdict.chain = chain;
9727 break;
9728 }
9729
9730 desc->len = sizeof(data->verdict);
9731
9732 return 0;
9733 }
9734
nft_verdict_uninit(const struct nft_data * data)9735 static void nft_verdict_uninit(const struct nft_data *data)
9736 {
9737 struct nft_chain *chain;
9738 struct nft_rule *rule;
9739
9740 switch (data->verdict.code) {
9741 case NFT_JUMP:
9742 case NFT_GOTO:
9743 chain = data->verdict.chain;
9744 chain->use--;
9745
9746 if (!nft_chain_is_bound(chain))
9747 break;
9748
9749 chain->table->use--;
9750 list_for_each_entry(rule, &chain->rules, list)
9751 chain->use--;
9752
9753 nft_chain_del(chain);
9754 break;
9755 }
9756 }
9757
nft_verdict_dump(struct sk_buff * skb,int type,const struct nft_verdict * v)9758 int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
9759 {
9760 struct nlattr *nest;
9761
9762 nest = nla_nest_start_noflag(skb, type);
9763 if (!nest)
9764 goto nla_put_failure;
9765
9766 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
9767 goto nla_put_failure;
9768
9769 switch (v->code) {
9770 case NFT_JUMP:
9771 case NFT_GOTO:
9772 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
9773 v->chain->name))
9774 goto nla_put_failure;
9775 }
9776 nla_nest_end(skb, nest);
9777 return 0;
9778
9779 nla_put_failure:
9780 return -1;
9781 }
9782
nft_value_init(const struct nft_ctx * ctx,struct nft_data * data,struct nft_data_desc * desc,const struct nlattr * nla)9783 static int nft_value_init(const struct nft_ctx *ctx,
9784 struct nft_data *data, struct nft_data_desc *desc,
9785 const struct nlattr *nla)
9786 {
9787 unsigned int len;
9788
9789 len = nla_len(nla);
9790 if (len == 0)
9791 return -EINVAL;
9792 if (len > desc->size)
9793 return -EOVERFLOW;
9794 if (desc->len) {
9795 if (len != desc->len)
9796 return -EINVAL;
9797 } else {
9798 desc->len = len;
9799 }
9800
9801 nla_memcpy(data->data, nla, len);
9802
9803 return 0;
9804 }
9805
nft_value_dump(struct sk_buff * skb,const struct nft_data * data,unsigned int len)9806 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
9807 unsigned int len)
9808 {
9809 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
9810 }
9811
9812 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
9813 [NFTA_DATA_VALUE] = { .type = NLA_BINARY },
9814 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
9815 };
9816
9817 /**
9818 * nft_data_init - parse nf_tables data netlink attributes
9819 *
9820 * @ctx: context of the expression using the data
9821 * @data: destination struct nft_data
9822 * @desc: data description
9823 * @nla: netlink attribute containing data
9824 *
9825 * Parse the netlink data attributes and initialize a struct nft_data.
9826 * The type and length of data are returned in the data description.
9827 *
9828 * The caller can indicate that it only wants to accept data of type
9829 * NFT_DATA_VALUE by passing NULL for the ctx argument.
9830 */
nft_data_init(const struct nft_ctx * ctx,struct nft_data * data,struct nft_data_desc * desc,const struct nlattr * nla)9831 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
9832 struct nft_data_desc *desc, const struct nlattr *nla)
9833 {
9834 struct nlattr *tb[NFTA_DATA_MAX + 1];
9835 int err;
9836
9837 if (WARN_ON_ONCE(!desc->size))
9838 return -EINVAL;
9839
9840 err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla,
9841 nft_data_policy, NULL);
9842 if (err < 0)
9843 return err;
9844
9845 if (tb[NFTA_DATA_VALUE]) {
9846 if (desc->type != NFT_DATA_VALUE)
9847 return -EINVAL;
9848
9849 err = nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
9850 } else if (tb[NFTA_DATA_VERDICT] && ctx != NULL) {
9851 if (desc->type != NFT_DATA_VERDICT)
9852 return -EINVAL;
9853
9854 err = nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
9855 } else {
9856 err = -EINVAL;
9857 }
9858
9859 return err;
9860 }
9861 EXPORT_SYMBOL_GPL(nft_data_init);
9862
9863 /**
9864 * nft_data_release - release a nft_data item
9865 *
9866 * @data: struct nft_data to release
9867 * @type: type of data
9868 *
9869 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
9870 * all others need to be released by calling this function.
9871 */
nft_data_release(const struct nft_data * data,enum nft_data_types type)9872 void nft_data_release(const struct nft_data *data, enum nft_data_types type)
9873 {
9874 if (type < NFT_DATA_VERDICT)
9875 return;
9876 switch (type) {
9877 case NFT_DATA_VERDICT:
9878 return nft_verdict_uninit(data);
9879 default:
9880 WARN_ON(1);
9881 }
9882 }
9883 EXPORT_SYMBOL_GPL(nft_data_release);
9884
nft_data_dump(struct sk_buff * skb,int attr,const struct nft_data * data,enum nft_data_types type,unsigned int len)9885 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
9886 enum nft_data_types type, unsigned int len)
9887 {
9888 struct nlattr *nest;
9889 int err;
9890
9891 nest = nla_nest_start_noflag(skb, attr);
9892 if (nest == NULL)
9893 return -1;
9894
9895 switch (type) {
9896 case NFT_DATA_VALUE:
9897 err = nft_value_dump(skb, data, len);
9898 break;
9899 case NFT_DATA_VERDICT:
9900 err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
9901 break;
9902 default:
9903 err = -EINVAL;
9904 WARN_ON(1);
9905 }
9906
9907 nla_nest_end(skb, nest);
9908 return err;
9909 }
9910 EXPORT_SYMBOL_GPL(nft_data_dump);
9911
__nft_release_basechain(struct nft_ctx * ctx)9912 int __nft_release_basechain(struct nft_ctx *ctx)
9913 {
9914 struct nft_rule *rule, *nr;
9915
9916 if (WARN_ON(!nft_is_base_chain(ctx->chain)))
9917 return 0;
9918
9919 nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain);
9920 list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
9921 list_del(&rule->list);
9922 ctx->chain->use--;
9923 nf_tables_rule_release(ctx, rule);
9924 }
9925 nft_chain_del(ctx->chain);
9926 ctx->table->use--;
9927 nf_tables_chain_destroy(ctx);
9928
9929 return 0;
9930 }
9931 EXPORT_SYMBOL_GPL(__nft_release_basechain);
9932
__nft_release_hook(struct net * net,struct nft_table * table)9933 static void __nft_release_hook(struct net *net, struct nft_table *table)
9934 {
9935 struct nft_flowtable *flowtable;
9936 struct nft_chain *chain;
9937
9938 list_for_each_entry(chain, &table->chains, list)
9939 __nf_tables_unregister_hook(net, table, chain, true);
9940 list_for_each_entry(flowtable, &table->flowtables, list)
9941 __nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list,
9942 true);
9943 }
9944
__nft_release_hooks(struct net * net)9945 static void __nft_release_hooks(struct net *net)
9946 {
9947 struct nftables_pernet *nft_net = nft_pernet(net);
9948 struct nft_table *table;
9949
9950 list_for_each_entry(table, &nft_net->tables, list) {
9951 if (nft_table_has_owner(table))
9952 continue;
9953
9954 __nft_release_hook(net, table);
9955 }
9956 }
9957
__nft_release_table(struct net * net,struct nft_table * table)9958 static void __nft_release_table(struct net *net, struct nft_table *table)
9959 {
9960 struct nft_flowtable *flowtable, *nf;
9961 struct nft_chain *chain, *nc;
9962 struct nft_object *obj, *ne;
9963 struct nft_rule *rule, *nr;
9964 struct nft_set *set, *ns;
9965 struct nft_ctx ctx = {
9966 .net = net,
9967 .family = NFPROTO_NETDEV,
9968 };
9969
9970 ctx.family = table->family;
9971 ctx.table = table;
9972 list_for_each_entry(chain, &table->chains, list) {
9973 ctx.chain = chain;
9974 list_for_each_entry_safe(rule, nr, &chain->rules, list) {
9975 list_del(&rule->list);
9976 chain->use--;
9977 nf_tables_rule_release(&ctx, rule);
9978 }
9979 }
9980 list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) {
9981 list_del(&flowtable->list);
9982 table->use--;
9983 nf_tables_flowtable_destroy(flowtable);
9984 }
9985 list_for_each_entry_safe(set, ns, &table->sets, list) {
9986 list_del(&set->list);
9987 table->use--;
9988 nft_set_destroy(&ctx, set);
9989 }
9990 list_for_each_entry_safe(obj, ne, &table->objects, list) {
9991 nft_obj_del(obj);
9992 table->use--;
9993 nft_obj_destroy(&ctx, obj);
9994 }
9995 list_for_each_entry_safe(chain, nc, &table->chains, list) {
9996 ctx.chain = chain;
9997 nft_chain_del(chain);
9998 table->use--;
9999 nf_tables_chain_destroy(&ctx);
10000 }
10001 nf_tables_table_destroy(&ctx);
10002 }
10003
__nft_release_tables(struct net * net)10004 static void __nft_release_tables(struct net *net)
10005 {
10006 struct nftables_pernet *nft_net = nft_pernet(net);
10007 struct nft_table *table, *nt;
10008
10009 list_for_each_entry_safe(table, nt, &nft_net->tables, list) {
10010 if (nft_table_has_owner(table))
10011 continue;
10012
10013 list_del(&table->list);
10014
10015 __nft_release_table(net, table);
10016 }
10017 }
10018
nft_rcv_nl_event(struct notifier_block * this,unsigned long event,void * ptr)10019 static int nft_rcv_nl_event(struct notifier_block *this, unsigned long event,
10020 void *ptr)
10021 {
10022 struct nft_table *table, *to_delete[8];
10023 struct nftables_pernet *nft_net;
10024 struct netlink_notify *n = ptr;
10025 struct net *net = n->net;
10026 unsigned int deleted;
10027 bool restart = false;
10028
10029 if (event != NETLINK_URELEASE || n->protocol != NETLINK_NETFILTER)
10030 return NOTIFY_DONE;
10031
10032 nft_net = nft_pernet(net);
10033 deleted = 0;
10034 mutex_lock(&nft_net->commit_mutex);
10035 if (!list_empty(&nf_tables_destroy_list))
10036 rcu_barrier();
10037 again:
10038 list_for_each_entry(table, &nft_net->tables, list) {
10039 if (nft_table_has_owner(table) &&
10040 n->portid == table->nlpid) {
10041 __nft_release_hook(net, table);
10042 list_del_rcu(&table->list);
10043 to_delete[deleted++] = table;
10044 if (deleted >= ARRAY_SIZE(to_delete))
10045 break;
10046 }
10047 }
10048 if (deleted) {
10049 restart = deleted >= ARRAY_SIZE(to_delete);
10050 synchronize_rcu();
10051 while (deleted)
10052 __nft_release_table(net, to_delete[--deleted]);
10053
10054 if (restart)
10055 goto again;
10056 }
10057 mutex_unlock(&nft_net->commit_mutex);
10058
10059 return NOTIFY_DONE;
10060 }
10061
10062 static struct notifier_block nft_nl_notifier = {
10063 .notifier_call = nft_rcv_nl_event,
10064 };
10065
nf_tables_init_net(struct net * net)10066 static int __net_init nf_tables_init_net(struct net *net)
10067 {
10068 struct nftables_pernet *nft_net = nft_pernet(net);
10069
10070 INIT_LIST_HEAD(&nft_net->tables);
10071 INIT_LIST_HEAD(&nft_net->commit_list);
10072 INIT_LIST_HEAD(&nft_net->module_list);
10073 INIT_LIST_HEAD(&nft_net->notify_list);
10074 mutex_init(&nft_net->commit_mutex);
10075 nft_net->base_seq = 1;
10076 nft_net->validate_state = NFT_VALIDATE_SKIP;
10077
10078 return 0;
10079 }
10080
nf_tables_pre_exit_net(struct net * net)10081 static void __net_exit nf_tables_pre_exit_net(struct net *net)
10082 {
10083 struct nftables_pernet *nft_net = nft_pernet(net);
10084
10085 mutex_lock(&nft_net->commit_mutex);
10086 __nft_release_hooks(net);
10087 mutex_unlock(&nft_net->commit_mutex);
10088 }
10089
nf_tables_exit_net(struct net * net)10090 static void __net_exit nf_tables_exit_net(struct net *net)
10091 {
10092 struct nftables_pernet *nft_net = nft_pernet(net);
10093
10094 mutex_lock(&nft_net->commit_mutex);
10095 if (!list_empty(&nft_net->commit_list) ||
10096 !list_empty(&nft_net->module_list))
10097 __nf_tables_abort(net, NFNL_ABORT_NONE);
10098 __nft_release_tables(net);
10099 mutex_unlock(&nft_net->commit_mutex);
10100 WARN_ON_ONCE(!list_empty(&nft_net->tables));
10101 WARN_ON_ONCE(!list_empty(&nft_net->module_list));
10102 WARN_ON_ONCE(!list_empty(&nft_net->notify_list));
10103 }
10104
10105 static struct pernet_operations nf_tables_net_ops = {
10106 .init = nf_tables_init_net,
10107 .pre_exit = nf_tables_pre_exit_net,
10108 .exit = nf_tables_exit_net,
10109 .id = &nf_tables_net_id,
10110 .size = sizeof(struct nftables_pernet),
10111 };
10112
nf_tables_module_init(void)10113 static int __init nf_tables_module_init(void)
10114 {
10115 int err;
10116
10117 err = register_pernet_subsys(&nf_tables_net_ops);
10118 if (err < 0)
10119 return err;
10120
10121 err = nft_chain_filter_init();
10122 if (err < 0)
10123 goto err_chain_filter;
10124
10125 err = nf_tables_core_module_init();
10126 if (err < 0)
10127 goto err_core_module;
10128
10129 err = register_netdevice_notifier(&nf_tables_flowtable_notifier);
10130 if (err < 0)
10131 goto err_netdev_notifier;
10132
10133 err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params);
10134 if (err < 0)
10135 goto err_rht_objname;
10136
10137 err = nft_offload_init();
10138 if (err < 0)
10139 goto err_offload;
10140
10141 err = netlink_register_notifier(&nft_nl_notifier);
10142 if (err < 0)
10143 goto err_netlink_notifier;
10144
10145 /* must be last */
10146 err = nfnetlink_subsys_register(&nf_tables_subsys);
10147 if (err < 0)
10148 goto err_nfnl_subsys;
10149
10150 nft_chain_route_init();
10151
10152 return err;
10153
10154 err_nfnl_subsys:
10155 netlink_unregister_notifier(&nft_nl_notifier);
10156 err_netlink_notifier:
10157 nft_offload_exit();
10158 err_offload:
10159 rhltable_destroy(&nft_objname_ht);
10160 err_rht_objname:
10161 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
10162 err_netdev_notifier:
10163 nf_tables_core_module_exit();
10164 err_core_module:
10165 nft_chain_filter_fini();
10166 err_chain_filter:
10167 unregister_pernet_subsys(&nf_tables_net_ops);
10168 return err;
10169 }
10170
nf_tables_module_exit(void)10171 static void __exit nf_tables_module_exit(void)
10172 {
10173 nfnetlink_subsys_unregister(&nf_tables_subsys);
10174 netlink_unregister_notifier(&nft_nl_notifier);
10175 nft_offload_exit();
10176 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
10177 nft_chain_filter_fini();
10178 nft_chain_route_fini();
10179 unregister_pernet_subsys(&nf_tables_net_ops);
10180 cancel_work_sync(&trans_destroy_work);
10181 rcu_barrier();
10182 rhltable_destroy(&nft_objname_ht);
10183 nf_tables_core_module_exit();
10184 }
10185
10186 module_init(nf_tables_module_init);
10187 module_exit(nf_tables_module_exit);
10188
10189 MODULE_LICENSE("GPL");
10190 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
10191 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);
10192