netfilter: nf_tables: fix transaction race condition
[deliverable/linux.git] / net / netfilter / nf_tables_api.c
1 /*
2 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/list.h>
14 #include <linux/skbuff.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nfnetlink.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_tables_core.h>
20 #include <net/netfilter/nf_tables.h>
21 #include <net/net_namespace.h>
22 #include <net/sock.h>
23
24 static LIST_HEAD(nf_tables_expressions);
25
26 /**
27 * nft_register_afinfo - register nf_tables address family info
28 *
29 * @afi: address family info to register
30 *
31 * Register the address family for use with nf_tables. Returns zero on
32 * success or a negative errno code otherwise.
33 */
34 int nft_register_afinfo(struct net *net, struct nft_af_info *afi)
35 {
36 INIT_LIST_HEAD(&afi->tables);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES);
38 list_add_tail_rcu(&afi->list, &net->nft.af_info);
39 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
40 return 0;
41 }
42 EXPORT_SYMBOL_GPL(nft_register_afinfo);
43
44 /**
45 * nft_unregister_afinfo - unregister nf_tables address family info
46 *
47 * @afi: address family info to unregister
48 *
49 * Unregister the address family for use with nf_tables.
50 */
51 void nft_unregister_afinfo(struct nft_af_info *afi)
52 {
53 nfnl_lock(NFNL_SUBSYS_NFTABLES);
54 list_del_rcu(&afi->list);
55 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
56 }
57 EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
58
59 static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
60 {
61 struct nft_af_info *afi;
62
63 list_for_each_entry(afi, &net->nft.af_info, list) {
64 if (afi->family == family)
65 return afi;
66 }
67 return NULL;
68 }
69
70 static struct nft_af_info *
71 nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
72 {
73 struct nft_af_info *afi;
74
75 afi = nft_afinfo_lookup(net, family);
76 if (afi != NULL)
77 return afi;
78 #ifdef CONFIG_MODULES
79 if (autoload) {
80 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
81 request_module("nft-afinfo-%u", family);
82 nfnl_lock(NFNL_SUBSYS_NFTABLES);
83 afi = nft_afinfo_lookup(net, family);
84 if (afi != NULL)
85 return ERR_PTR(-EAGAIN);
86 }
87 #endif
88 return ERR_PTR(-EAFNOSUPPORT);
89 }
90
91 static void nft_ctx_init(struct nft_ctx *ctx,
92 const struct sk_buff *skb,
93 const struct nlmsghdr *nlh,
94 struct nft_af_info *afi,
95 struct nft_table *table,
96 struct nft_chain *chain,
97 const struct nlattr * const *nla)
98 {
99 ctx->net = sock_net(skb->sk);
100 ctx->afi = afi;
101 ctx->table = table;
102 ctx->chain = chain;
103 ctx->nla = nla;
104 ctx->portid = NETLINK_CB(skb).portid;
105 ctx->report = nlmsg_report(nlh);
106 ctx->seq = nlh->nlmsg_seq;
107 }
108
109 static struct nft_trans *nft_trans_alloc(struct nft_ctx *ctx, int msg_type,
110 u32 size)
111 {
112 struct nft_trans *trans;
113
114 trans = kzalloc(sizeof(struct nft_trans) + size, GFP_KERNEL);
115 if (trans == NULL)
116 return NULL;
117
118 trans->msg_type = msg_type;
119 trans->ctx = *ctx;
120
121 return trans;
122 }
123
124 static void nft_trans_destroy(struct nft_trans *trans)
125 {
126 list_del(&trans->list);
127 kfree(trans);
128 }
129
130 static void nf_tables_unregister_hooks(const struct nft_table *table,
131 const struct nft_chain *chain,
132 unsigned int hook_nops)
133 {
134 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
135 chain->flags & NFT_BASE_CHAIN)
136 nf_unregister_hooks(nft_base_chain(chain)->ops, hook_nops);
137 }
138
139 /* Internal table flags */
140 #define NFT_TABLE_INACTIVE (1 << 15)
141
142 static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
143 {
144 struct nft_trans *trans;
145
146 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
147 if (trans == NULL)
148 return -ENOMEM;
149
150 if (msg_type == NFT_MSG_NEWTABLE)
151 ctx->table->flags |= NFT_TABLE_INACTIVE;
152
153 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
154 return 0;
155 }
156
157 static int nft_deltable(struct nft_ctx *ctx)
158 {
159 int err;
160
161 err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
162 if (err < 0)
163 return err;
164
165 list_del_rcu(&ctx->table->list);
166 return err;
167 }
168
169 static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
170 {
171 struct nft_trans *trans;
172
173 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
174 if (trans == NULL)
175 return -ENOMEM;
176
177 if (msg_type == NFT_MSG_NEWCHAIN)
178 ctx->chain->flags |= NFT_CHAIN_INACTIVE;
179
180 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
181 return 0;
182 }
183
184 static int nft_delchain(struct nft_ctx *ctx)
185 {
186 int err;
187
188 err = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
189 if (err < 0)
190 return err;
191
192 ctx->table->use--;
193 list_del_rcu(&ctx->chain->list);
194
195 return err;
196 }
197
198 static inline bool
199 nft_rule_is_active(struct net *net, const struct nft_rule *rule)
200 {
201 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
202 }
203
204 static inline int gencursor_next(struct net *net)
205 {
206 return net->nft.gencursor+1 == 1 ? 1 : 0;
207 }
208
209 static inline int
210 nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
211 {
212 return (rule->genmask & (1 << gencursor_next(net))) == 0;
213 }
214
215 static inline void
216 nft_rule_activate_next(struct net *net, struct nft_rule *rule)
217 {
218 /* Now inactive, will be active in the future */
219 rule->genmask = (1 << net->nft.gencursor);
220 }
221
222 static inline void
223 nft_rule_deactivate_next(struct net *net, struct nft_rule *rule)
224 {
225 rule->genmask = (1 << gencursor_next(net));
226 }
227
228 static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
229 {
230 rule->genmask &= ~(1 << gencursor_next(net));
231 }
232
233 static int
234 nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
235 {
236 /* You cannot delete the same rule twice */
237 if (nft_rule_is_active_next(ctx->net, rule)) {
238 nft_rule_deactivate_next(ctx->net, rule);
239 ctx->chain->use--;
240 return 0;
241 }
242 return -ENOENT;
243 }
244
245 static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
246 struct nft_rule *rule)
247 {
248 struct nft_trans *trans;
249
250 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
251 if (trans == NULL)
252 return NULL;
253
254 nft_trans_rule(trans) = rule;
255 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
256
257 return trans;
258 }
259
260 static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
261 {
262 struct nft_trans *trans;
263 int err;
264
265 trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
266 if (trans == NULL)
267 return -ENOMEM;
268
269 err = nf_tables_delrule_deactivate(ctx, rule);
270 if (err < 0) {
271 nft_trans_destroy(trans);
272 return err;
273 }
274
275 return 0;
276 }
277
278 static int nft_delrule_by_chain(struct nft_ctx *ctx)
279 {
280 struct nft_rule *rule;
281 int err;
282
283 list_for_each_entry(rule, &ctx->chain->rules, list) {
284 err = nft_delrule(ctx, rule);
285 if (err < 0)
286 return err;
287 }
288 return 0;
289 }
290
291 /* Internal set flag */
292 #define NFT_SET_INACTIVE (1 << 15)
293
294 static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
295 struct nft_set *set)
296 {
297 struct nft_trans *trans;
298
299 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
300 if (trans == NULL)
301 return -ENOMEM;
302
303 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
304 nft_trans_set_id(trans) =
305 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
306 set->flags |= NFT_SET_INACTIVE;
307 }
308 nft_trans_set(trans) = set;
309 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
310
311 return 0;
312 }
313
314 static int nft_delset(struct nft_ctx *ctx, struct nft_set *set)
315 {
316 int err;
317
318 err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
319 if (err < 0)
320 return err;
321
322 list_del_rcu(&set->list);
323 ctx->table->use--;
324
325 return err;
326 }
327
328 /*
329 * Tables
330 */
331
332 static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
333 const struct nlattr *nla)
334 {
335 struct nft_table *table;
336
337 list_for_each_entry(table, &afi->tables, list) {
338 if (!nla_strcmp(nla, table->name))
339 return table;
340 }
341 return NULL;
342 }
343
344 static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
345 const struct nlattr *nla)
346 {
347 struct nft_table *table;
348
349 if (nla == NULL)
350 return ERR_PTR(-EINVAL);
351
352 table = nft_table_lookup(afi, nla);
353 if (table != NULL)
354 return table;
355
356 return ERR_PTR(-ENOENT);
357 }
358
359 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
360 {
361 return ++table->hgenerator;
362 }
363
364 static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
365
366 static const struct nf_chain_type *
367 __nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
368 {
369 int i;
370
371 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
372 if (chain_type[family][i] != NULL &&
373 !nla_strcmp(nla, chain_type[family][i]->name))
374 return chain_type[family][i];
375 }
376 return NULL;
377 }
378
379 static const struct nf_chain_type *
380 nf_tables_chain_type_lookup(const struct nft_af_info *afi,
381 const struct nlattr *nla,
382 bool autoload)
383 {
384 const struct nf_chain_type *type;
385
386 type = __nf_tables_chain_type_lookup(afi->family, nla);
387 if (type != NULL)
388 return type;
389 #ifdef CONFIG_MODULES
390 if (autoload) {
391 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
392 request_module("nft-chain-%u-%.*s", afi->family,
393 nla_len(nla), (const char *)nla_data(nla));
394 nfnl_lock(NFNL_SUBSYS_NFTABLES);
395 type = __nf_tables_chain_type_lookup(afi->family, nla);
396 if (type != NULL)
397 return ERR_PTR(-EAGAIN);
398 }
399 #endif
400 return ERR_PTR(-ENOENT);
401 }
402
403 static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
404 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
405 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
406 };
407
408 static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
409 u32 portid, u32 seq, int event, u32 flags,
410 int family, const struct nft_table *table)
411 {
412 struct nlmsghdr *nlh;
413 struct nfgenmsg *nfmsg;
414
415 event |= NFNL_SUBSYS_NFTABLES << 8;
416 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
417 if (nlh == NULL)
418 goto nla_put_failure;
419
420 nfmsg = nlmsg_data(nlh);
421 nfmsg->nfgen_family = family;
422 nfmsg->version = NFNETLINK_V0;
423 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
424
425 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
426 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
427 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
428 goto nla_put_failure;
429
430 nlmsg_end(skb, nlh);
431 return 0;
432
433 nla_put_failure:
434 nlmsg_trim(skb, nlh);
435 return -1;
436 }
437
438 static int nf_tables_table_notify(const struct nft_ctx *ctx, int event)
439 {
440 struct sk_buff *skb;
441 int err;
442
443 if (!ctx->report &&
444 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
445 return 0;
446
447 err = -ENOBUFS;
448 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
449 if (skb == NULL)
450 goto err;
451
452 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
453 event, 0, ctx->afi->family, ctx->table);
454 if (err < 0) {
455 kfree_skb(skb);
456 goto err;
457 }
458
459 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
460 ctx->report, GFP_KERNEL);
461 err:
462 if (err < 0) {
463 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
464 err);
465 }
466 return err;
467 }
468
469 static int nf_tables_dump_tables(struct sk_buff *skb,
470 struct netlink_callback *cb)
471 {
472 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
473 const struct nft_af_info *afi;
474 const struct nft_table *table;
475 unsigned int idx = 0, s_idx = cb->args[0];
476 struct net *net = sock_net(skb->sk);
477 int family = nfmsg->nfgen_family;
478
479 rcu_read_lock();
480 cb->seq = net->nft.base_seq;
481
482 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
483 if (family != NFPROTO_UNSPEC && family != afi->family)
484 continue;
485
486 list_for_each_entry_rcu(table, &afi->tables, list) {
487 if (idx < s_idx)
488 goto cont;
489 if (idx > s_idx)
490 memset(&cb->args[1], 0,
491 sizeof(cb->args) - sizeof(cb->args[0]));
492 if (nf_tables_fill_table_info(skb, net,
493 NETLINK_CB(cb->skb).portid,
494 cb->nlh->nlmsg_seq,
495 NFT_MSG_NEWTABLE,
496 NLM_F_MULTI,
497 afi->family, table) < 0)
498 goto done;
499
500 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
501 cont:
502 idx++;
503 }
504 }
505 done:
506 rcu_read_unlock();
507 cb->args[0] = idx;
508 return skb->len;
509 }
510
511 static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
512 const struct nlmsghdr *nlh,
513 const struct nlattr * const nla[])
514 {
515 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
516 const struct nft_af_info *afi;
517 const struct nft_table *table;
518 struct sk_buff *skb2;
519 struct net *net = sock_net(skb->sk);
520 int family = nfmsg->nfgen_family;
521 int err;
522
523 if (nlh->nlmsg_flags & NLM_F_DUMP) {
524 struct netlink_dump_control c = {
525 .dump = nf_tables_dump_tables,
526 };
527 return netlink_dump_start(nlsk, skb, nlh, &c);
528 }
529
530 afi = nf_tables_afinfo_lookup(net, family, false);
531 if (IS_ERR(afi))
532 return PTR_ERR(afi);
533
534 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
535 if (IS_ERR(table))
536 return PTR_ERR(table);
537 if (table->flags & NFT_TABLE_INACTIVE)
538 return -ENOENT;
539
540 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
541 if (!skb2)
542 return -ENOMEM;
543
544 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
545 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
546 family, table);
547 if (err < 0)
548 goto err;
549
550 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
551
552 err:
553 kfree_skb(skb2);
554 return err;
555 }
556
557 static int nf_tables_table_enable(const struct nft_af_info *afi,
558 struct nft_table *table)
559 {
560 struct nft_chain *chain;
561 int err, i = 0;
562
563 list_for_each_entry(chain, &table->chains, list) {
564 if (!(chain->flags & NFT_BASE_CHAIN))
565 continue;
566
567 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
568 if (err < 0)
569 goto err;
570
571 i++;
572 }
573 return 0;
574 err:
575 list_for_each_entry(chain, &table->chains, list) {
576 if (!(chain->flags & NFT_BASE_CHAIN))
577 continue;
578
579 if (i-- <= 0)
580 break;
581
582 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
583 }
584 return err;
585 }
586
587 static void nf_tables_table_disable(const struct nft_af_info *afi,
588 struct nft_table *table)
589 {
590 struct nft_chain *chain;
591
592 list_for_each_entry(chain, &table->chains, list) {
593 if (chain->flags & NFT_BASE_CHAIN)
594 nf_unregister_hooks(nft_base_chain(chain)->ops,
595 afi->nops);
596 }
597 }
598
599 static int nf_tables_updtable(struct nft_ctx *ctx)
600 {
601 struct nft_trans *trans;
602 u32 flags;
603 int ret = 0;
604
605 if (!ctx->nla[NFTA_TABLE_FLAGS])
606 return 0;
607
608 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
609 if (flags & ~NFT_TABLE_F_DORMANT)
610 return -EINVAL;
611
612 if (flags == ctx->table->flags)
613 return 0;
614
615 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
616 sizeof(struct nft_trans_table));
617 if (trans == NULL)
618 return -ENOMEM;
619
620 if ((flags & NFT_TABLE_F_DORMANT) &&
621 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
622 nft_trans_table_enable(trans) = false;
623 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
624 ctx->table->flags & NFT_TABLE_F_DORMANT) {
625 ret = nf_tables_table_enable(ctx->afi, ctx->table);
626 if (ret >= 0) {
627 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
628 nft_trans_table_enable(trans) = true;
629 }
630 }
631 if (ret < 0)
632 goto err;
633
634 nft_trans_table_update(trans) = true;
635 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
636 return 0;
637 err:
638 nft_trans_destroy(trans);
639 return ret;
640 }
641
642 static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
643 const struct nlmsghdr *nlh,
644 const struct nlattr * const nla[])
645 {
646 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
647 const struct nlattr *name;
648 struct nft_af_info *afi;
649 struct nft_table *table;
650 struct net *net = sock_net(skb->sk);
651 int family = nfmsg->nfgen_family;
652 u32 flags = 0;
653 struct nft_ctx ctx;
654 int err;
655
656 afi = nf_tables_afinfo_lookup(net, family, true);
657 if (IS_ERR(afi))
658 return PTR_ERR(afi);
659
660 name = nla[NFTA_TABLE_NAME];
661 table = nf_tables_table_lookup(afi, name);
662 if (IS_ERR(table)) {
663 if (PTR_ERR(table) != -ENOENT)
664 return PTR_ERR(table);
665 table = NULL;
666 }
667
668 if (table != NULL) {
669 if (table->flags & NFT_TABLE_INACTIVE)
670 return -ENOENT;
671 if (nlh->nlmsg_flags & NLM_F_EXCL)
672 return -EEXIST;
673 if (nlh->nlmsg_flags & NLM_F_REPLACE)
674 return -EOPNOTSUPP;
675
676 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
677 return nf_tables_updtable(&ctx);
678 }
679
680 if (nla[NFTA_TABLE_FLAGS]) {
681 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
682 if (flags & ~NFT_TABLE_F_DORMANT)
683 return -EINVAL;
684 }
685
686 if (!try_module_get(afi->owner))
687 return -EAFNOSUPPORT;
688
689 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
690 if (table == NULL) {
691 module_put(afi->owner);
692 return -ENOMEM;
693 }
694
695 nla_strlcpy(table->name, name, nla_len(name));
696 INIT_LIST_HEAD(&table->chains);
697 INIT_LIST_HEAD(&table->sets);
698 table->flags = flags;
699
700 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
701 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
702 if (err < 0) {
703 kfree(table);
704 module_put(afi->owner);
705 return err;
706 }
707 list_add_tail_rcu(&table->list, &afi->tables);
708 return 0;
709 }
710
711 static int nft_flush_table(struct nft_ctx *ctx)
712 {
713 int err;
714 struct nft_chain *chain, *nc;
715 struct nft_set *set, *ns;
716
717 list_for_each_entry(chain, &ctx->table->chains, list) {
718 ctx->chain = chain;
719
720 err = nft_delrule_by_chain(ctx);
721 if (err < 0)
722 goto out;
723 }
724
725 list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
726 if (set->flags & NFT_SET_ANONYMOUS &&
727 !list_empty(&set->bindings))
728 continue;
729
730 err = nft_delset(ctx, set);
731 if (err < 0)
732 goto out;
733 }
734
735 list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
736 ctx->chain = chain;
737
738 err = nft_delchain(ctx);
739 if (err < 0)
740 goto out;
741 }
742
743 err = nft_deltable(ctx);
744 out:
745 return err;
746 }
747
748 static int nft_flush(struct nft_ctx *ctx, int family)
749 {
750 struct nft_af_info *afi;
751 struct nft_table *table, *nt;
752 const struct nlattr * const *nla = ctx->nla;
753 int err = 0;
754
755 list_for_each_entry(afi, &ctx->net->nft.af_info, list) {
756 if (family != AF_UNSPEC && afi->family != family)
757 continue;
758
759 ctx->afi = afi;
760 list_for_each_entry_safe(table, nt, &afi->tables, list) {
761 if (nla[NFTA_TABLE_NAME] &&
762 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
763 continue;
764
765 ctx->table = table;
766
767 err = nft_flush_table(ctx);
768 if (err < 0)
769 goto out;
770 }
771 }
772 out:
773 return err;
774 }
775
776 static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
777 const struct nlmsghdr *nlh,
778 const struct nlattr * const nla[])
779 {
780 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
781 struct nft_af_info *afi;
782 struct nft_table *table;
783 struct net *net = sock_net(skb->sk);
784 int family = nfmsg->nfgen_family;
785 struct nft_ctx ctx;
786
787 nft_ctx_init(&ctx, skb, nlh, NULL, NULL, NULL, nla);
788 if (family == AF_UNSPEC || nla[NFTA_TABLE_NAME] == NULL)
789 return nft_flush(&ctx, family);
790
791 afi = nf_tables_afinfo_lookup(net, family, false);
792 if (IS_ERR(afi))
793 return PTR_ERR(afi);
794
795 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
796 if (IS_ERR(table))
797 return PTR_ERR(table);
798 if (table->flags & NFT_TABLE_INACTIVE)
799 return -ENOENT;
800
801 ctx.afi = afi;
802 ctx.table = table;
803
804 return nft_flush_table(&ctx);
805 }
806
807 static void nf_tables_table_destroy(struct nft_ctx *ctx)
808 {
809 BUG_ON(ctx->table->use > 0);
810
811 kfree(ctx->table);
812 module_put(ctx->afi->owner);
813 }
814
815 int nft_register_chain_type(const struct nf_chain_type *ctype)
816 {
817 int err = 0;
818
819 nfnl_lock(NFNL_SUBSYS_NFTABLES);
820 if (chain_type[ctype->family][ctype->type] != NULL) {
821 err = -EBUSY;
822 goto out;
823 }
824 chain_type[ctype->family][ctype->type] = ctype;
825 out:
826 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
827 return err;
828 }
829 EXPORT_SYMBOL_GPL(nft_register_chain_type);
830
831 void nft_unregister_chain_type(const struct nf_chain_type *ctype)
832 {
833 nfnl_lock(NFNL_SUBSYS_NFTABLES);
834 chain_type[ctype->family][ctype->type] = NULL;
835 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
836 }
837 EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
838
839 /*
840 * Chains
841 */
842
843 static struct nft_chain *
844 nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
845 {
846 struct nft_chain *chain;
847
848 list_for_each_entry(chain, &table->chains, list) {
849 if (chain->handle == handle)
850 return chain;
851 }
852
853 return ERR_PTR(-ENOENT);
854 }
855
856 static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
857 const struct nlattr *nla)
858 {
859 struct nft_chain *chain;
860
861 if (nla == NULL)
862 return ERR_PTR(-EINVAL);
863
864 list_for_each_entry(chain, &table->chains, list) {
865 if (!nla_strcmp(nla, chain->name))
866 return chain;
867 }
868
869 return ERR_PTR(-ENOENT);
870 }
871
872 static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
873 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
874 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
875 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
876 .len = NFT_CHAIN_MAXNAMELEN - 1 },
877 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
878 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
879 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
880 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
881 };
882
883 static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
884 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
885 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
886 };
887
888 static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
889 {
890 struct nft_stats *cpu_stats, total;
891 struct nlattr *nest;
892 unsigned int seq;
893 u64 pkts, bytes;
894 int cpu;
895
896 memset(&total, 0, sizeof(total));
897 for_each_possible_cpu(cpu) {
898 cpu_stats = per_cpu_ptr(stats, cpu);
899 do {
900 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
901 pkts = cpu_stats->pkts;
902 bytes = cpu_stats->bytes;
903 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
904 total.pkts += pkts;
905 total.bytes += bytes;
906 }
907 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
908 if (nest == NULL)
909 goto nla_put_failure;
910
911 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
912 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
913 goto nla_put_failure;
914
915 nla_nest_end(skb, nest);
916 return 0;
917
918 nla_put_failure:
919 return -ENOSPC;
920 }
921
922 static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
923 u32 portid, u32 seq, int event, u32 flags,
924 int family, const struct nft_table *table,
925 const struct nft_chain *chain)
926 {
927 struct nlmsghdr *nlh;
928 struct nfgenmsg *nfmsg;
929
930 event |= NFNL_SUBSYS_NFTABLES << 8;
931 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
932 if (nlh == NULL)
933 goto nla_put_failure;
934
935 nfmsg = nlmsg_data(nlh);
936 nfmsg->nfgen_family = family;
937 nfmsg->version = NFNETLINK_V0;
938 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
939
940 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
941 goto nla_put_failure;
942 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
943 goto nla_put_failure;
944 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
945 goto nla_put_failure;
946
947 if (chain->flags & NFT_BASE_CHAIN) {
948 const struct nft_base_chain *basechain = nft_base_chain(chain);
949 const struct nf_hook_ops *ops = &basechain->ops[0];
950 struct nlattr *nest;
951
952 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
953 if (nest == NULL)
954 goto nla_put_failure;
955 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
956 goto nla_put_failure;
957 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
958 goto nla_put_failure;
959 nla_nest_end(skb, nest);
960
961 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
962 htonl(basechain->policy)))
963 goto nla_put_failure;
964
965 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
966 goto nla_put_failure;
967
968 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
969 goto nla_put_failure;
970 }
971
972 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
973 goto nla_put_failure;
974
975 nlmsg_end(skb, nlh);
976 return 0;
977
978 nla_put_failure:
979 nlmsg_trim(skb, nlh);
980 return -1;
981 }
982
983 static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
984 {
985 struct sk_buff *skb;
986 int err;
987
988 if (!ctx->report &&
989 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
990 return 0;
991
992 err = -ENOBUFS;
993 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
994 if (skb == NULL)
995 goto err;
996
997 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
998 event, 0, ctx->afi->family, ctx->table,
999 ctx->chain);
1000 if (err < 0) {
1001 kfree_skb(skb);
1002 goto err;
1003 }
1004
1005 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1006 ctx->report, GFP_KERNEL);
1007 err:
1008 if (err < 0) {
1009 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1010 err);
1011 }
1012 return err;
1013 }
1014
1015 static int nf_tables_dump_chains(struct sk_buff *skb,
1016 struct netlink_callback *cb)
1017 {
1018 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1019 const struct nft_af_info *afi;
1020 const struct nft_table *table;
1021 const struct nft_chain *chain;
1022 unsigned int idx = 0, s_idx = cb->args[0];
1023 struct net *net = sock_net(skb->sk);
1024 int family = nfmsg->nfgen_family;
1025
1026 rcu_read_lock();
1027 cb->seq = net->nft.base_seq;
1028
1029 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
1030 if (family != NFPROTO_UNSPEC && family != afi->family)
1031 continue;
1032
1033 list_for_each_entry_rcu(table, &afi->tables, list) {
1034 list_for_each_entry_rcu(chain, &table->chains, list) {
1035 if (idx < s_idx)
1036 goto cont;
1037 if (idx > s_idx)
1038 memset(&cb->args[1], 0,
1039 sizeof(cb->args) - sizeof(cb->args[0]));
1040 if (nf_tables_fill_chain_info(skb, net,
1041 NETLINK_CB(cb->skb).portid,
1042 cb->nlh->nlmsg_seq,
1043 NFT_MSG_NEWCHAIN,
1044 NLM_F_MULTI,
1045 afi->family, table, chain) < 0)
1046 goto done;
1047
1048 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1049 cont:
1050 idx++;
1051 }
1052 }
1053 }
1054 done:
1055 rcu_read_unlock();
1056 cb->args[0] = idx;
1057 return skb->len;
1058 }
1059
1060 static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
1061 const struct nlmsghdr *nlh,
1062 const struct nlattr * const nla[])
1063 {
1064 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1065 const struct nft_af_info *afi;
1066 const struct nft_table *table;
1067 const struct nft_chain *chain;
1068 struct sk_buff *skb2;
1069 struct net *net = sock_net(skb->sk);
1070 int family = nfmsg->nfgen_family;
1071 int err;
1072
1073 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1074 struct netlink_dump_control c = {
1075 .dump = nf_tables_dump_chains,
1076 };
1077 return netlink_dump_start(nlsk, skb, nlh, &c);
1078 }
1079
1080 afi = nf_tables_afinfo_lookup(net, family, false);
1081 if (IS_ERR(afi))
1082 return PTR_ERR(afi);
1083
1084 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1085 if (IS_ERR(table))
1086 return PTR_ERR(table);
1087 if (table->flags & NFT_TABLE_INACTIVE)
1088 return -ENOENT;
1089
1090 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1091 if (IS_ERR(chain))
1092 return PTR_ERR(chain);
1093 if (chain->flags & NFT_CHAIN_INACTIVE)
1094 return -ENOENT;
1095
1096 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1097 if (!skb2)
1098 return -ENOMEM;
1099
1100 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
1101 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
1102 family, table, chain);
1103 if (err < 0)
1104 goto err;
1105
1106 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1107
1108 err:
1109 kfree_skb(skb2);
1110 return err;
1111 }
1112
1113 static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1114 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
1115 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
1116 };
1117
1118 static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
1119 {
1120 struct nlattr *tb[NFTA_COUNTER_MAX+1];
1121 struct nft_stats __percpu *newstats;
1122 struct nft_stats *stats;
1123 int err;
1124
1125 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
1126 if (err < 0)
1127 return ERR_PTR(err);
1128
1129 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
1130 return ERR_PTR(-EINVAL);
1131
1132 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
1133 if (newstats == NULL)
1134 return ERR_PTR(-ENOMEM);
1135
1136 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1137 * are not exposed to userspace.
1138 */
1139 preempt_disable();
1140 stats = this_cpu_ptr(newstats);
1141 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1142 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
1143 preempt_enable();
1144
1145 return newstats;
1146 }
1147
1148 static void nft_chain_stats_replace(struct nft_base_chain *chain,
1149 struct nft_stats __percpu *newstats)
1150 {
1151 if (newstats == NULL)
1152 return;
1153
1154 if (chain->stats) {
1155 struct nft_stats __percpu *oldstats =
1156 nft_dereference(chain->stats);
1157
1158 rcu_assign_pointer(chain->stats, newstats);
1159 synchronize_rcu();
1160 free_percpu(oldstats);
1161 } else
1162 rcu_assign_pointer(chain->stats, newstats);
1163 }
1164
1165 static void nf_tables_chain_destroy(struct nft_chain *chain)
1166 {
1167 BUG_ON(chain->use > 0);
1168
1169 if (chain->flags & NFT_BASE_CHAIN) {
1170 module_put(nft_base_chain(chain)->type->owner);
1171 free_percpu(nft_base_chain(chain)->stats);
1172 kfree(nft_base_chain(chain));
1173 } else {
1174 kfree(chain);
1175 }
1176 }
1177
1178 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
1179 const struct nlmsghdr *nlh,
1180 const struct nlattr * const nla[])
1181 {
1182 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1183 const struct nlattr * uninitialized_var(name);
1184 struct nft_af_info *afi;
1185 struct nft_table *table;
1186 struct nft_chain *chain;
1187 struct nft_base_chain *basechain = NULL;
1188 struct nlattr *ha[NFTA_HOOK_MAX + 1];
1189 struct net *net = sock_net(skb->sk);
1190 int family = nfmsg->nfgen_family;
1191 u8 policy = NF_ACCEPT;
1192 u64 handle = 0;
1193 unsigned int i;
1194 struct nft_stats __percpu *stats;
1195 int err;
1196 bool create;
1197 struct nft_ctx ctx;
1198
1199 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1200
1201 afi = nf_tables_afinfo_lookup(net, family, true);
1202 if (IS_ERR(afi))
1203 return PTR_ERR(afi);
1204
1205 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1206 if (IS_ERR(table))
1207 return PTR_ERR(table);
1208
1209 chain = NULL;
1210 name = nla[NFTA_CHAIN_NAME];
1211
1212 if (nla[NFTA_CHAIN_HANDLE]) {
1213 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
1214 chain = nf_tables_chain_lookup_byhandle(table, handle);
1215 if (IS_ERR(chain))
1216 return PTR_ERR(chain);
1217 } else {
1218 chain = nf_tables_chain_lookup(table, name);
1219 if (IS_ERR(chain)) {
1220 if (PTR_ERR(chain) != -ENOENT)
1221 return PTR_ERR(chain);
1222 chain = NULL;
1223 }
1224 }
1225
1226 if (nla[NFTA_CHAIN_POLICY]) {
1227 if ((chain != NULL &&
1228 !(chain->flags & NFT_BASE_CHAIN)) ||
1229 nla[NFTA_CHAIN_HOOK] == NULL)
1230 return -EOPNOTSUPP;
1231
1232 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
1233 switch (policy) {
1234 case NF_DROP:
1235 case NF_ACCEPT:
1236 break;
1237 default:
1238 return -EINVAL;
1239 }
1240 }
1241
1242 if (chain != NULL) {
1243 struct nft_stats *stats = NULL;
1244 struct nft_trans *trans;
1245
1246 if (chain->flags & NFT_CHAIN_INACTIVE)
1247 return -ENOENT;
1248 if (nlh->nlmsg_flags & NLM_F_EXCL)
1249 return -EEXIST;
1250 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1251 return -EOPNOTSUPP;
1252
1253 if (nla[NFTA_CHAIN_HANDLE] && name &&
1254 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1255 return -EEXIST;
1256
1257 if (nla[NFTA_CHAIN_COUNTERS]) {
1258 if (!(chain->flags & NFT_BASE_CHAIN))
1259 return -EOPNOTSUPP;
1260
1261 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1262 if (IS_ERR(stats))
1263 return PTR_ERR(stats);
1264 }
1265
1266 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1267 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1268 sizeof(struct nft_trans_chain));
1269 if (trans == NULL) {
1270 free_percpu(stats);
1271 return -ENOMEM;
1272 }
1273
1274 nft_trans_chain_stats(trans) = stats;
1275 nft_trans_chain_update(trans) = true;
1276
1277 if (nla[NFTA_CHAIN_POLICY])
1278 nft_trans_chain_policy(trans) = policy;
1279 else
1280 nft_trans_chain_policy(trans) = -1;
1281
1282 if (nla[NFTA_CHAIN_HANDLE] && name) {
1283 nla_strlcpy(nft_trans_chain_name(trans), name,
1284 NFT_CHAIN_MAXNAMELEN);
1285 }
1286 list_add_tail(&trans->list, &net->nft.commit_list);
1287 return 0;
1288 }
1289
1290 if (table->use == UINT_MAX)
1291 return -EOVERFLOW;
1292
1293 if (nla[NFTA_CHAIN_HOOK]) {
1294 const struct nf_chain_type *type;
1295 struct nf_hook_ops *ops;
1296 nf_hookfn *hookfn;
1297 u32 hooknum, priority;
1298
1299 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
1300 if (nla[NFTA_CHAIN_TYPE]) {
1301 type = nf_tables_chain_type_lookup(afi,
1302 nla[NFTA_CHAIN_TYPE],
1303 create);
1304 if (IS_ERR(type))
1305 return PTR_ERR(type);
1306 }
1307
1308 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1309 nft_hook_policy);
1310 if (err < 0)
1311 return err;
1312 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1313 ha[NFTA_HOOK_PRIORITY] == NULL)
1314 return -EINVAL;
1315
1316 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1317 if (hooknum >= afi->nhooks)
1318 return -EINVAL;
1319 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
1320
1321 if (!(type->hook_mask & (1 << hooknum)))
1322 return -EOPNOTSUPP;
1323 if (!try_module_get(type->owner))
1324 return -ENOENT;
1325 hookfn = type->hooks[hooknum];
1326
1327 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
1328 if (basechain == NULL) {
1329 module_put(type->owner);
1330 return -ENOMEM;
1331 }
1332
1333 if (nla[NFTA_CHAIN_COUNTERS]) {
1334 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1335 if (IS_ERR(stats)) {
1336 module_put(type->owner);
1337 kfree(basechain);
1338 return PTR_ERR(stats);
1339 }
1340 basechain->stats = stats;
1341 } else {
1342 stats = netdev_alloc_pcpu_stats(struct nft_stats);
1343 if (stats == NULL) {
1344 module_put(type->owner);
1345 kfree(basechain);
1346 return -ENOMEM;
1347 }
1348 rcu_assign_pointer(basechain->stats, stats);
1349 }
1350
1351 basechain->type = type;
1352 chain = &basechain->chain;
1353
1354 for (i = 0; i < afi->nops; i++) {
1355 ops = &basechain->ops[i];
1356 ops->pf = family;
1357 ops->owner = afi->owner;
1358 ops->hooknum = hooknum;
1359 ops->priority = priority;
1360 ops->priv = chain;
1361 ops->hook = afi->hooks[ops->hooknum];
1362 if (hookfn)
1363 ops->hook = hookfn;
1364 if (afi->hook_ops_init)
1365 afi->hook_ops_init(ops, i);
1366 }
1367
1368 chain->flags |= NFT_BASE_CHAIN;
1369 basechain->policy = policy;
1370 } else {
1371 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1372 if (chain == NULL)
1373 return -ENOMEM;
1374 }
1375
1376 INIT_LIST_HEAD(&chain->rules);
1377 chain->handle = nf_tables_alloc_handle(table);
1378 chain->net = net;
1379 chain->table = table;
1380 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1381
1382 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1383 chain->flags & NFT_BASE_CHAIN) {
1384 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
1385 if (err < 0)
1386 goto err1;
1387 }
1388
1389 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1390 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1391 if (err < 0)
1392 goto err2;
1393
1394 table->use++;
1395 list_add_tail_rcu(&chain->list, &table->chains);
1396 return 0;
1397 err2:
1398 nf_tables_unregister_hooks(table, chain, afi->nops);
1399 err1:
1400 nf_tables_chain_destroy(chain);
1401 return err;
1402 }
1403
1404 static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1405 const struct nlmsghdr *nlh,
1406 const struct nlattr * const nla[])
1407 {
1408 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1409 struct nft_af_info *afi;
1410 struct nft_table *table;
1411 struct nft_chain *chain;
1412 struct net *net = sock_net(skb->sk);
1413 int family = nfmsg->nfgen_family;
1414 struct nft_ctx ctx;
1415
1416 afi = nf_tables_afinfo_lookup(net, family, false);
1417 if (IS_ERR(afi))
1418 return PTR_ERR(afi);
1419
1420 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1421 if (IS_ERR(table))
1422 return PTR_ERR(table);
1423 if (table->flags & NFT_TABLE_INACTIVE)
1424 return -ENOENT;
1425
1426 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1427 if (IS_ERR(chain))
1428 return PTR_ERR(chain);
1429 if (chain->flags & NFT_CHAIN_INACTIVE)
1430 return -ENOENT;
1431 if (chain->use > 0)
1432 return -EBUSY;
1433
1434 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1435
1436 return nft_delchain(&ctx);
1437 }
1438
1439 /*
1440 * Expressions
1441 */
1442
1443 /**
1444 * nft_register_expr - register nf_tables expr type
1445 * @ops: expr type
1446 *
1447 * Registers the expr type for use with nf_tables. Returns zero on
1448 * success or a negative errno code otherwise.
1449 */
1450 int nft_register_expr(struct nft_expr_type *type)
1451 {
1452 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1453 if (type->family == NFPROTO_UNSPEC)
1454 list_add_tail_rcu(&type->list, &nf_tables_expressions);
1455 else
1456 list_add_rcu(&type->list, &nf_tables_expressions);
1457 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1458 return 0;
1459 }
1460 EXPORT_SYMBOL_GPL(nft_register_expr);
1461
1462 /**
1463 * nft_unregister_expr - unregister nf_tables expr type
1464 * @ops: expr type
1465 *
1466 * Unregisters the expr typefor use with nf_tables.
1467 */
1468 void nft_unregister_expr(struct nft_expr_type *type)
1469 {
1470 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1471 list_del_rcu(&type->list);
1472 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1473 }
1474 EXPORT_SYMBOL_GPL(nft_unregister_expr);
1475
1476 static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1477 struct nlattr *nla)
1478 {
1479 const struct nft_expr_type *type;
1480
1481 list_for_each_entry(type, &nf_tables_expressions, list) {
1482 if (!nla_strcmp(nla, type->name) &&
1483 (!type->family || type->family == family))
1484 return type;
1485 }
1486 return NULL;
1487 }
1488
1489 static const struct nft_expr_type *nft_expr_type_get(u8 family,
1490 struct nlattr *nla)
1491 {
1492 const struct nft_expr_type *type;
1493
1494 if (nla == NULL)
1495 return ERR_PTR(-EINVAL);
1496
1497 type = __nft_expr_type_get(family, nla);
1498 if (type != NULL && try_module_get(type->owner))
1499 return type;
1500
1501 #ifdef CONFIG_MODULES
1502 if (type == NULL) {
1503 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1504 request_module("nft-expr-%u-%.*s", family,
1505 nla_len(nla), (char *)nla_data(nla));
1506 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1507 if (__nft_expr_type_get(family, nla))
1508 return ERR_PTR(-EAGAIN);
1509
1510 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1511 request_module("nft-expr-%.*s",
1512 nla_len(nla), (char *)nla_data(nla));
1513 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1514 if (__nft_expr_type_get(family, nla))
1515 return ERR_PTR(-EAGAIN);
1516 }
1517 #endif
1518 return ERR_PTR(-ENOENT);
1519 }
1520
1521 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1522 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1523 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1524 };
1525
1526 static int nf_tables_fill_expr_info(struct sk_buff *skb,
1527 const struct nft_expr *expr)
1528 {
1529 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1530 goto nla_put_failure;
1531
1532 if (expr->ops->dump) {
1533 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1534 if (data == NULL)
1535 goto nla_put_failure;
1536 if (expr->ops->dump(skb, expr) < 0)
1537 goto nla_put_failure;
1538 nla_nest_end(skb, data);
1539 }
1540
1541 return skb->len;
1542
1543 nla_put_failure:
1544 return -1;
1545 };
1546
1547 struct nft_expr_info {
1548 const struct nft_expr_ops *ops;
1549 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
1550 };
1551
1552 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1553 const struct nlattr *nla,
1554 struct nft_expr_info *info)
1555 {
1556 const struct nft_expr_type *type;
1557 const struct nft_expr_ops *ops;
1558 struct nlattr *tb[NFTA_EXPR_MAX + 1];
1559 int err;
1560
1561 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1562 if (err < 0)
1563 return err;
1564
1565 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
1566 if (IS_ERR(type))
1567 return PTR_ERR(type);
1568
1569 if (tb[NFTA_EXPR_DATA]) {
1570 err = nla_parse_nested(info->tb, type->maxattr,
1571 tb[NFTA_EXPR_DATA], type->policy);
1572 if (err < 0)
1573 goto err1;
1574 } else
1575 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1576
1577 if (type->select_ops != NULL) {
1578 ops = type->select_ops(ctx,
1579 (const struct nlattr * const *)info->tb);
1580 if (IS_ERR(ops)) {
1581 err = PTR_ERR(ops);
1582 goto err1;
1583 }
1584 } else
1585 ops = type->ops;
1586
1587 info->ops = ops;
1588 return 0;
1589
1590 err1:
1591 module_put(type->owner);
1592 return err;
1593 }
1594
1595 static int nf_tables_newexpr(const struct nft_ctx *ctx,
1596 const struct nft_expr_info *info,
1597 struct nft_expr *expr)
1598 {
1599 const struct nft_expr_ops *ops = info->ops;
1600 int err;
1601
1602 expr->ops = ops;
1603 if (ops->init) {
1604 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1605 if (err < 0)
1606 goto err1;
1607 }
1608
1609 return 0;
1610
1611 err1:
1612 expr->ops = NULL;
1613 return err;
1614 }
1615
1616 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1617 struct nft_expr *expr)
1618 {
1619 if (expr->ops->destroy)
1620 expr->ops->destroy(ctx, expr);
1621 module_put(expr->ops->type->owner);
1622 }
1623
1624 /*
1625 * Rules
1626 */
1627
1628 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1629 u64 handle)
1630 {
1631 struct nft_rule *rule;
1632
1633 // FIXME: this sucks
1634 list_for_each_entry(rule, &chain->rules, list) {
1635 if (handle == rule->handle)
1636 return rule;
1637 }
1638
1639 return ERR_PTR(-ENOENT);
1640 }
1641
1642 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1643 const struct nlattr *nla)
1644 {
1645 if (nla == NULL)
1646 return ERR_PTR(-EINVAL);
1647
1648 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1649 }
1650
1651 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1652 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1653 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1654 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1655 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1656 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1657 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
1658 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
1659 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1660 .len = NFT_USERDATA_MAXLEN },
1661 };
1662
1663 static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
1664 u32 portid, u32 seq, int event,
1665 u32 flags, int family,
1666 const struct nft_table *table,
1667 const struct nft_chain *chain,
1668 const struct nft_rule *rule)
1669 {
1670 struct nlmsghdr *nlh;
1671 struct nfgenmsg *nfmsg;
1672 const struct nft_expr *expr, *next;
1673 struct nlattr *list;
1674 const struct nft_rule *prule;
1675 int type = event | NFNL_SUBSYS_NFTABLES << 8;
1676
1677 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1678 flags);
1679 if (nlh == NULL)
1680 goto nla_put_failure;
1681
1682 nfmsg = nlmsg_data(nlh);
1683 nfmsg->nfgen_family = family;
1684 nfmsg->version = NFNETLINK_V0;
1685 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
1686
1687 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1688 goto nla_put_failure;
1689 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1690 goto nla_put_failure;
1691 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1692 goto nla_put_failure;
1693
1694 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1695 prule = list_entry(rule->list.prev, struct nft_rule, list);
1696 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1697 cpu_to_be64(prule->handle)))
1698 goto nla_put_failure;
1699 }
1700
1701 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1702 if (list == NULL)
1703 goto nla_put_failure;
1704 nft_rule_for_each_expr(expr, next, rule) {
1705 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1706 if (elem == NULL)
1707 goto nla_put_failure;
1708 if (nf_tables_fill_expr_info(skb, expr) < 0)
1709 goto nla_put_failure;
1710 nla_nest_end(skb, elem);
1711 }
1712 nla_nest_end(skb, list);
1713
1714 if (rule->ulen &&
1715 nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1716 goto nla_put_failure;
1717
1718 nlmsg_end(skb, nlh);
1719 return 0;
1720
1721 nla_put_failure:
1722 nlmsg_trim(skb, nlh);
1723 return -1;
1724 }
1725
1726 static int nf_tables_rule_notify(const struct nft_ctx *ctx,
1727 const struct nft_rule *rule,
1728 int event)
1729 {
1730 struct sk_buff *skb;
1731 int err;
1732
1733 if (!ctx->report &&
1734 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1735 return 0;
1736
1737 err = -ENOBUFS;
1738 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1739 if (skb == NULL)
1740 goto err;
1741
1742 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
1743 event, 0, ctx->afi->family, ctx->table,
1744 ctx->chain, rule);
1745 if (err < 0) {
1746 kfree_skb(skb);
1747 goto err;
1748 }
1749
1750 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1751 ctx->report, GFP_KERNEL);
1752 err:
1753 if (err < 0) {
1754 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1755 err);
1756 }
1757 return err;
1758 }
1759
1760 static int nf_tables_dump_rules(struct sk_buff *skb,
1761 struct netlink_callback *cb)
1762 {
1763 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1764 const struct nft_af_info *afi;
1765 const struct nft_table *table;
1766 const struct nft_chain *chain;
1767 const struct nft_rule *rule;
1768 unsigned int idx = 0, s_idx = cb->args[0];
1769 struct net *net = sock_net(skb->sk);
1770 int family = nfmsg->nfgen_family;
1771
1772 rcu_read_lock();
1773 cb->seq = net->nft.base_seq;
1774
1775 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
1776 if (family != NFPROTO_UNSPEC && family != afi->family)
1777 continue;
1778
1779 list_for_each_entry_rcu(table, &afi->tables, list) {
1780 list_for_each_entry_rcu(chain, &table->chains, list) {
1781 list_for_each_entry_rcu(rule, &chain->rules, list) {
1782 if (!nft_rule_is_active(net, rule))
1783 goto cont;
1784 if (idx < s_idx)
1785 goto cont;
1786 if (idx > s_idx)
1787 memset(&cb->args[1], 0,
1788 sizeof(cb->args) - sizeof(cb->args[0]));
1789 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
1790 cb->nlh->nlmsg_seq,
1791 NFT_MSG_NEWRULE,
1792 NLM_F_MULTI | NLM_F_APPEND,
1793 afi->family, table, chain, rule) < 0)
1794 goto done;
1795
1796 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1797 cont:
1798 idx++;
1799 }
1800 }
1801 }
1802 }
1803 done:
1804 rcu_read_unlock();
1805
1806 cb->args[0] = idx;
1807 return skb->len;
1808 }
1809
1810 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1811 const struct nlmsghdr *nlh,
1812 const struct nlattr * const nla[])
1813 {
1814 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1815 const struct nft_af_info *afi;
1816 const struct nft_table *table;
1817 const struct nft_chain *chain;
1818 const struct nft_rule *rule;
1819 struct sk_buff *skb2;
1820 struct net *net = sock_net(skb->sk);
1821 int family = nfmsg->nfgen_family;
1822 int err;
1823
1824 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1825 struct netlink_dump_control c = {
1826 .dump = nf_tables_dump_rules,
1827 };
1828 return netlink_dump_start(nlsk, skb, nlh, &c);
1829 }
1830
1831 afi = nf_tables_afinfo_lookup(net, family, false);
1832 if (IS_ERR(afi))
1833 return PTR_ERR(afi);
1834
1835 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1836 if (IS_ERR(table))
1837 return PTR_ERR(table);
1838 if (table->flags & NFT_TABLE_INACTIVE)
1839 return -ENOENT;
1840
1841 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1842 if (IS_ERR(chain))
1843 return PTR_ERR(chain);
1844 if (chain->flags & NFT_CHAIN_INACTIVE)
1845 return -ENOENT;
1846
1847 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1848 if (IS_ERR(rule))
1849 return PTR_ERR(rule);
1850
1851 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1852 if (!skb2)
1853 return -ENOMEM;
1854
1855 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
1856 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1857 family, table, chain, rule);
1858 if (err < 0)
1859 goto err;
1860
1861 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1862
1863 err:
1864 kfree_skb(skb2);
1865 return err;
1866 }
1867
1868 static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1869 struct nft_rule *rule)
1870 {
1871 struct nft_expr *expr;
1872
1873 /*
1874 * Careful: some expressions might not be initialized in case this
1875 * is called on error from nf_tables_newrule().
1876 */
1877 expr = nft_expr_first(rule);
1878 while (expr->ops && expr != nft_expr_last(rule)) {
1879 nf_tables_expr_destroy(ctx, expr);
1880 expr = nft_expr_next(expr);
1881 }
1882 kfree(rule);
1883 }
1884
1885 #define NFT_RULE_MAXEXPRS 128
1886
1887 static struct nft_expr_info *info;
1888
1889 static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1890 const struct nlmsghdr *nlh,
1891 const struct nlattr * const nla[])
1892 {
1893 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1894 struct nft_af_info *afi;
1895 struct net *net = sock_net(skb->sk);
1896 struct nft_table *table;
1897 struct nft_chain *chain;
1898 struct nft_rule *rule, *old_rule = NULL;
1899 struct nft_trans *trans = NULL;
1900 struct nft_expr *expr;
1901 struct nft_ctx ctx;
1902 struct nlattr *tmp;
1903 unsigned int size, i, n, ulen = 0;
1904 int err, rem;
1905 bool create;
1906 u64 handle, pos_handle;
1907
1908 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1909
1910 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1911 if (IS_ERR(afi))
1912 return PTR_ERR(afi);
1913
1914 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1915 if (IS_ERR(table))
1916 return PTR_ERR(table);
1917
1918 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1919 if (IS_ERR(chain))
1920 return PTR_ERR(chain);
1921
1922 if (nla[NFTA_RULE_HANDLE]) {
1923 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1924 rule = __nf_tables_rule_lookup(chain, handle);
1925 if (IS_ERR(rule))
1926 return PTR_ERR(rule);
1927
1928 if (nlh->nlmsg_flags & NLM_F_EXCL)
1929 return -EEXIST;
1930 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1931 old_rule = rule;
1932 else
1933 return -EOPNOTSUPP;
1934 } else {
1935 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1936 return -EINVAL;
1937 handle = nf_tables_alloc_handle(table);
1938
1939 if (chain->use == UINT_MAX)
1940 return -EOVERFLOW;
1941 }
1942
1943 if (nla[NFTA_RULE_POSITION]) {
1944 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1945 return -EOPNOTSUPP;
1946
1947 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1948 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1949 if (IS_ERR(old_rule))
1950 return PTR_ERR(old_rule);
1951 }
1952
1953 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1954
1955 n = 0;
1956 size = 0;
1957 if (nla[NFTA_RULE_EXPRESSIONS]) {
1958 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1959 err = -EINVAL;
1960 if (nla_type(tmp) != NFTA_LIST_ELEM)
1961 goto err1;
1962 if (n == NFT_RULE_MAXEXPRS)
1963 goto err1;
1964 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1965 if (err < 0)
1966 goto err1;
1967 size += info[n].ops->size;
1968 n++;
1969 }
1970 }
1971
1972 if (nla[NFTA_RULE_USERDATA])
1973 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1974
1975 err = -ENOMEM;
1976 rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
1977 if (rule == NULL)
1978 goto err1;
1979
1980 nft_rule_activate_next(net, rule);
1981
1982 rule->handle = handle;
1983 rule->dlen = size;
1984 rule->ulen = ulen;
1985
1986 if (ulen)
1987 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
1988
1989 expr = nft_expr_first(rule);
1990 for (i = 0; i < n; i++) {
1991 err = nf_tables_newexpr(&ctx, &info[i], expr);
1992 if (err < 0)
1993 goto err2;
1994 info[i].ops = NULL;
1995 expr = nft_expr_next(expr);
1996 }
1997
1998 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1999 if (nft_rule_is_active_next(net, old_rule)) {
2000 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
2001 old_rule);
2002 if (trans == NULL) {
2003 err = -ENOMEM;
2004 goto err2;
2005 }
2006 nft_rule_deactivate_next(net, old_rule);
2007 chain->use--;
2008 list_add_tail_rcu(&rule->list, &old_rule->list);
2009 } else {
2010 err = -ENOENT;
2011 goto err2;
2012 }
2013 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
2014 if (old_rule)
2015 list_add_rcu(&rule->list, &old_rule->list);
2016 else
2017 list_add_tail_rcu(&rule->list, &chain->rules);
2018 else {
2019 if (old_rule)
2020 list_add_tail_rcu(&rule->list, &old_rule->list);
2021 else
2022 list_add_rcu(&rule->list, &chain->rules);
2023 }
2024
2025 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
2026 err = -ENOMEM;
2027 goto err3;
2028 }
2029 chain->use++;
2030 return 0;
2031
2032 err3:
2033 list_del_rcu(&rule->list);
2034 if (trans) {
2035 list_del_rcu(&nft_trans_rule(trans)->list);
2036 nft_rule_clear(net, nft_trans_rule(trans));
2037 nft_trans_destroy(trans);
2038 chain->use++;
2039 }
2040 err2:
2041 nf_tables_rule_destroy(&ctx, rule);
2042 err1:
2043 for (i = 0; i < n; i++) {
2044 if (info[i].ops != NULL)
2045 module_put(info[i].ops->type->owner);
2046 }
2047 return err;
2048 }
2049
2050 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
2051 const struct nlmsghdr *nlh,
2052 const struct nlattr * const nla[])
2053 {
2054 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2055 struct nft_af_info *afi;
2056 struct net *net = sock_net(skb->sk);
2057 struct nft_table *table;
2058 struct nft_chain *chain = NULL;
2059 struct nft_rule *rule;
2060 int family = nfmsg->nfgen_family, err = 0;
2061 struct nft_ctx ctx;
2062
2063 afi = nf_tables_afinfo_lookup(net, family, false);
2064 if (IS_ERR(afi))
2065 return PTR_ERR(afi);
2066
2067 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
2068 if (IS_ERR(table))
2069 return PTR_ERR(table);
2070 if (table->flags & NFT_TABLE_INACTIVE)
2071 return -ENOENT;
2072
2073 if (nla[NFTA_RULE_CHAIN]) {
2074 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
2075 if (IS_ERR(chain))
2076 return PTR_ERR(chain);
2077 }
2078
2079 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
2080
2081 if (chain) {
2082 if (nla[NFTA_RULE_HANDLE]) {
2083 rule = nf_tables_rule_lookup(chain,
2084 nla[NFTA_RULE_HANDLE]);
2085 if (IS_ERR(rule))
2086 return PTR_ERR(rule);
2087
2088 err = nft_delrule(&ctx, rule);
2089 } else {
2090 err = nft_delrule_by_chain(&ctx);
2091 }
2092 } else {
2093 list_for_each_entry(chain, &table->chains, list) {
2094 ctx.chain = chain;
2095 err = nft_delrule_by_chain(&ctx);
2096 if (err < 0)
2097 break;
2098 }
2099 }
2100
2101 return err;
2102 }
2103
2104 /*
2105 * Sets
2106 */
2107
2108 static LIST_HEAD(nf_tables_set_ops);
2109
2110 int nft_register_set(struct nft_set_ops *ops)
2111 {
2112 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2113 list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
2114 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2115 return 0;
2116 }
2117 EXPORT_SYMBOL_GPL(nft_register_set);
2118
2119 void nft_unregister_set(struct nft_set_ops *ops)
2120 {
2121 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2122 list_del_rcu(&ops->list);
2123 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2124 }
2125 EXPORT_SYMBOL_GPL(nft_unregister_set);
2126
2127 /*
2128 * Select a set implementation based on the data characteristics and the
2129 * given policy. The total memory use might not be known if no size is
2130 * given, in that case the amount of memory per element is used.
2131 */
2132 static const struct nft_set_ops *
2133 nft_select_set_ops(const struct nlattr * const nla[],
2134 const struct nft_set_desc *desc,
2135 enum nft_set_policies policy)
2136 {
2137 const struct nft_set_ops *ops, *bops;
2138 struct nft_set_estimate est, best;
2139 u32 features;
2140
2141 #ifdef CONFIG_MODULES
2142 if (list_empty(&nf_tables_set_ops)) {
2143 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2144 request_module("nft-set");
2145 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2146 if (!list_empty(&nf_tables_set_ops))
2147 return ERR_PTR(-EAGAIN);
2148 }
2149 #endif
2150 features = 0;
2151 if (nla[NFTA_SET_FLAGS] != NULL) {
2152 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2153 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
2154 }
2155
2156 bops = NULL;
2157 best.size = ~0;
2158 best.class = ~0;
2159
2160 list_for_each_entry(ops, &nf_tables_set_ops, list) {
2161 if ((ops->features & features) != features)
2162 continue;
2163 if (!ops->estimate(desc, features, &est))
2164 continue;
2165
2166 switch (policy) {
2167 case NFT_SET_POL_PERFORMANCE:
2168 if (est.class < best.class)
2169 break;
2170 if (est.class == best.class && est.size < best.size)
2171 break;
2172 continue;
2173 case NFT_SET_POL_MEMORY:
2174 if (est.size < best.size)
2175 break;
2176 if (est.size == best.size && est.class < best.class)
2177 break;
2178 continue;
2179 default:
2180 break;
2181 }
2182
2183 if (!try_module_get(ops->owner))
2184 continue;
2185 if (bops != NULL)
2186 module_put(bops->owner);
2187
2188 bops = ops;
2189 best = est;
2190 }
2191
2192 if (bops != NULL)
2193 return bops;
2194
2195 return ERR_PTR(-EOPNOTSUPP);
2196 }
2197
2198 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2199 [NFTA_SET_TABLE] = { .type = NLA_STRING },
2200 [NFTA_SET_NAME] = { .type = NLA_STRING,
2201 .len = IFNAMSIZ - 1 },
2202 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2203 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2204 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2205 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2206 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
2207 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2208 [NFTA_SET_DESC] = { .type = NLA_NESTED },
2209 [NFTA_SET_ID] = { .type = NLA_U32 },
2210 };
2211
2212 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2213 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
2214 };
2215
2216 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2217 const struct sk_buff *skb,
2218 const struct nlmsghdr *nlh,
2219 const struct nlattr * const nla[])
2220 {
2221 struct net *net = sock_net(skb->sk);
2222 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2223 struct nft_af_info *afi = NULL;
2224 struct nft_table *table = NULL;
2225
2226 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2227 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2228 if (IS_ERR(afi))
2229 return PTR_ERR(afi);
2230 }
2231
2232 if (nla[NFTA_SET_TABLE] != NULL) {
2233 if (afi == NULL)
2234 return -EAFNOSUPPORT;
2235
2236 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2237 if (IS_ERR(table))
2238 return PTR_ERR(table);
2239 if (table->flags & NFT_TABLE_INACTIVE)
2240 return -ENOENT;
2241 }
2242
2243 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2244 return 0;
2245 }
2246
2247 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2248 const struct nlattr *nla)
2249 {
2250 struct nft_set *set;
2251
2252 if (nla == NULL)
2253 return ERR_PTR(-EINVAL);
2254
2255 list_for_each_entry(set, &table->sets, list) {
2256 if (!nla_strcmp(nla, set->name))
2257 return set;
2258 }
2259 return ERR_PTR(-ENOENT);
2260 }
2261
2262 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2263 const struct nlattr *nla)
2264 {
2265 struct nft_trans *trans;
2266 u32 id = ntohl(nla_get_be32(nla));
2267
2268 list_for_each_entry(trans, &net->nft.commit_list, list) {
2269 if (trans->msg_type == NFT_MSG_NEWSET &&
2270 id == nft_trans_set_id(trans))
2271 return nft_trans_set(trans);
2272 }
2273 return ERR_PTR(-ENOENT);
2274 }
2275
2276 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2277 const char *name)
2278 {
2279 const struct nft_set *i;
2280 const char *p;
2281 unsigned long *inuse;
2282 unsigned int n = 0, min = 0;
2283
2284 p = strnchr(name, IFNAMSIZ, '%');
2285 if (p != NULL) {
2286 if (p[1] != 'd' || strchr(p + 2, '%'))
2287 return -EINVAL;
2288
2289 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2290 if (inuse == NULL)
2291 return -ENOMEM;
2292 cont:
2293 list_for_each_entry(i, &ctx->table->sets, list) {
2294 int tmp;
2295
2296 if (!sscanf(i->name, name, &tmp))
2297 continue;
2298 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
2299 continue;
2300
2301 set_bit(tmp - min, inuse);
2302 }
2303
2304 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
2305 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2306 min += BITS_PER_BYTE * PAGE_SIZE;
2307 memset(inuse, 0, PAGE_SIZE);
2308 goto cont;
2309 }
2310 free_page((unsigned long)inuse);
2311 }
2312
2313 snprintf(set->name, sizeof(set->name), name, min + n);
2314 list_for_each_entry(i, &ctx->table->sets, list) {
2315 if (!strcmp(set->name, i->name))
2316 return -ENFILE;
2317 }
2318 return 0;
2319 }
2320
2321 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2322 const struct nft_set *set, u16 event, u16 flags)
2323 {
2324 struct nfgenmsg *nfmsg;
2325 struct nlmsghdr *nlh;
2326 struct nlattr *desc;
2327 u32 portid = ctx->portid;
2328 u32 seq = ctx->seq;
2329
2330 event |= NFNL_SUBSYS_NFTABLES << 8;
2331 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2332 flags);
2333 if (nlh == NULL)
2334 goto nla_put_failure;
2335
2336 nfmsg = nlmsg_data(nlh);
2337 nfmsg->nfgen_family = ctx->afi->family;
2338 nfmsg->version = NFNETLINK_V0;
2339 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
2340
2341 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2342 goto nla_put_failure;
2343 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2344 goto nla_put_failure;
2345 if (set->flags != 0)
2346 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2347 goto nla_put_failure;
2348
2349 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2350 goto nla_put_failure;
2351 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2352 goto nla_put_failure;
2353 if (set->flags & NFT_SET_MAP) {
2354 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2355 goto nla_put_failure;
2356 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2357 goto nla_put_failure;
2358 }
2359
2360 if (set->policy != NFT_SET_POL_PERFORMANCE) {
2361 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
2362 goto nla_put_failure;
2363 }
2364
2365 desc = nla_nest_start(skb, NFTA_SET_DESC);
2366 if (desc == NULL)
2367 goto nla_put_failure;
2368 if (set->size &&
2369 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2370 goto nla_put_failure;
2371 nla_nest_end(skb, desc);
2372
2373 nlmsg_end(skb, nlh);
2374 return 0;
2375
2376 nla_put_failure:
2377 nlmsg_trim(skb, nlh);
2378 return -1;
2379 }
2380
2381 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2382 const struct nft_set *set,
2383 int event, gfp_t gfp_flags)
2384 {
2385 struct sk_buff *skb;
2386 u32 portid = ctx->portid;
2387 int err;
2388
2389 if (!ctx->report &&
2390 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2391 return 0;
2392
2393 err = -ENOBUFS;
2394 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
2395 if (skb == NULL)
2396 goto err;
2397
2398 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2399 if (err < 0) {
2400 kfree_skb(skb);
2401 goto err;
2402 }
2403
2404 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
2405 ctx->report, gfp_flags);
2406 err:
2407 if (err < 0)
2408 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2409 return err;
2410 }
2411
2412 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2413 {
2414 const struct nft_set *set;
2415 unsigned int idx, s_idx = cb->args[0];
2416 struct nft_af_info *afi;
2417 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2418 struct net *net = sock_net(skb->sk);
2419 int cur_family = cb->args[3];
2420 struct nft_ctx *ctx = cb->data, ctx_set;
2421
2422 if (cb->args[1])
2423 return skb->len;
2424
2425 rcu_read_lock();
2426 cb->seq = net->nft.base_seq;
2427
2428 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
2429 if (ctx->afi && ctx->afi != afi)
2430 continue;
2431
2432 if (cur_family) {
2433 if (afi->family != cur_family)
2434 continue;
2435
2436 cur_family = 0;
2437 }
2438 list_for_each_entry_rcu(table, &afi->tables, list) {
2439 if (ctx->table && ctx->table != table)
2440 continue;
2441
2442 if (cur_table) {
2443 if (cur_table != table)
2444 continue;
2445
2446 cur_table = NULL;
2447 }
2448 idx = 0;
2449 list_for_each_entry_rcu(set, &table->sets, list) {
2450 if (idx < s_idx)
2451 goto cont;
2452
2453 ctx_set = *ctx;
2454 ctx_set.table = table;
2455 ctx_set.afi = afi;
2456 if (nf_tables_fill_set(skb, &ctx_set, set,
2457 NFT_MSG_NEWSET,
2458 NLM_F_MULTI) < 0) {
2459 cb->args[0] = idx;
2460 cb->args[2] = (unsigned long) table;
2461 cb->args[3] = afi->family;
2462 goto done;
2463 }
2464 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2465 cont:
2466 idx++;
2467 }
2468 if (s_idx)
2469 s_idx = 0;
2470 }
2471 }
2472 cb->args[1] = 1;
2473 done:
2474 rcu_read_unlock();
2475 return skb->len;
2476 }
2477
2478 static int nf_tables_dump_sets_done(struct netlink_callback *cb)
2479 {
2480 kfree(cb->data);
2481 return 0;
2482 }
2483
2484 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2485 const struct nlmsghdr *nlh,
2486 const struct nlattr * const nla[])
2487 {
2488 const struct nft_set *set;
2489 struct nft_ctx ctx;
2490 struct sk_buff *skb2;
2491 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2492 int err;
2493
2494 /* Verify existence before starting dump */
2495 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2496 if (err < 0)
2497 return err;
2498
2499 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2500 struct netlink_dump_control c = {
2501 .dump = nf_tables_dump_sets,
2502 .done = nf_tables_dump_sets_done,
2503 };
2504 struct nft_ctx *ctx_dump;
2505
2506 ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL);
2507 if (ctx_dump == NULL)
2508 return -ENOMEM;
2509
2510 *ctx_dump = ctx;
2511 c.data = ctx_dump;
2512
2513 return netlink_dump_start(nlsk, skb, nlh, &c);
2514 }
2515
2516 /* Only accept unspec with dump */
2517 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2518 return -EAFNOSUPPORT;
2519
2520 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2521 if (IS_ERR(set))
2522 return PTR_ERR(set);
2523 if (set->flags & NFT_SET_INACTIVE)
2524 return -ENOENT;
2525
2526 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2527 if (skb2 == NULL)
2528 return -ENOMEM;
2529
2530 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2531 if (err < 0)
2532 goto err;
2533
2534 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2535
2536 err:
2537 kfree_skb(skb2);
2538 return err;
2539 }
2540
2541 static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2542 struct nft_set_desc *desc,
2543 const struct nlattr *nla)
2544 {
2545 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2546 int err;
2547
2548 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2549 if (err < 0)
2550 return err;
2551
2552 if (da[NFTA_SET_DESC_SIZE] != NULL)
2553 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2554
2555 return 0;
2556 }
2557
2558 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2559 const struct nlmsghdr *nlh,
2560 const struct nlattr * const nla[])
2561 {
2562 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2563 const struct nft_set_ops *ops;
2564 struct nft_af_info *afi;
2565 struct net *net = sock_net(skb->sk);
2566 struct nft_table *table;
2567 struct nft_set *set;
2568 struct nft_ctx ctx;
2569 char name[IFNAMSIZ];
2570 unsigned int size;
2571 bool create;
2572 u32 ktype, dtype, flags, policy;
2573 struct nft_set_desc desc;
2574 int err;
2575
2576 if (nla[NFTA_SET_TABLE] == NULL ||
2577 nla[NFTA_SET_NAME] == NULL ||
2578 nla[NFTA_SET_KEY_LEN] == NULL ||
2579 nla[NFTA_SET_ID] == NULL)
2580 return -EINVAL;
2581
2582 memset(&desc, 0, sizeof(desc));
2583
2584 ktype = NFT_DATA_VALUE;
2585 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2586 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2587 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2588 return -EINVAL;
2589 }
2590
2591 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2592 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
2593 return -EINVAL;
2594
2595 flags = 0;
2596 if (nla[NFTA_SET_FLAGS] != NULL) {
2597 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2598 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2599 NFT_SET_INTERVAL | NFT_SET_MAP))
2600 return -EINVAL;
2601 }
2602
2603 dtype = 0;
2604 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2605 if (!(flags & NFT_SET_MAP))
2606 return -EINVAL;
2607
2608 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2609 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2610 dtype != NFT_DATA_VERDICT)
2611 return -EINVAL;
2612
2613 if (dtype != NFT_DATA_VERDICT) {
2614 if (nla[NFTA_SET_DATA_LEN] == NULL)
2615 return -EINVAL;
2616 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2617 if (desc.dlen == 0 ||
2618 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
2619 return -EINVAL;
2620 } else
2621 desc.dlen = sizeof(struct nft_data);
2622 } else if (flags & NFT_SET_MAP)
2623 return -EINVAL;
2624
2625 policy = NFT_SET_POL_PERFORMANCE;
2626 if (nla[NFTA_SET_POLICY] != NULL)
2627 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2628
2629 if (nla[NFTA_SET_DESC] != NULL) {
2630 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2631 if (err < 0)
2632 return err;
2633 }
2634
2635 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2636
2637 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2638 if (IS_ERR(afi))
2639 return PTR_ERR(afi);
2640
2641 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2642 if (IS_ERR(table))
2643 return PTR_ERR(table);
2644
2645 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2646
2647 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2648 if (IS_ERR(set)) {
2649 if (PTR_ERR(set) != -ENOENT)
2650 return PTR_ERR(set);
2651 set = NULL;
2652 }
2653
2654 if (set != NULL) {
2655 if (nlh->nlmsg_flags & NLM_F_EXCL)
2656 return -EEXIST;
2657 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2658 return -EOPNOTSUPP;
2659 return 0;
2660 }
2661
2662 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2663 return -ENOENT;
2664
2665 ops = nft_select_set_ops(nla, &desc, policy);
2666 if (IS_ERR(ops))
2667 return PTR_ERR(ops);
2668
2669 size = 0;
2670 if (ops->privsize != NULL)
2671 size = ops->privsize(nla);
2672
2673 err = -ENOMEM;
2674 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2675 if (set == NULL)
2676 goto err1;
2677
2678 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2679 err = nf_tables_set_alloc_name(&ctx, set, name);
2680 if (err < 0)
2681 goto err2;
2682
2683 INIT_LIST_HEAD(&set->bindings);
2684 set->ops = ops;
2685 set->ktype = ktype;
2686 set->klen = desc.klen;
2687 set->dtype = dtype;
2688 set->dlen = desc.dlen;
2689 set->flags = flags;
2690 set->size = desc.size;
2691 set->policy = policy;
2692
2693 err = ops->init(set, &desc, nla);
2694 if (err < 0)
2695 goto err2;
2696
2697 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
2698 if (err < 0)
2699 goto err2;
2700
2701 list_add_tail_rcu(&set->list, &table->sets);
2702 table->use++;
2703 return 0;
2704
2705 err2:
2706 kfree(set);
2707 err1:
2708 module_put(ops->owner);
2709 return err;
2710 }
2711
2712 static void nft_set_destroy(struct nft_set *set)
2713 {
2714 set->ops->destroy(set);
2715 module_put(set->ops->owner);
2716 kfree(set);
2717 }
2718
2719 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2720 {
2721 list_del_rcu(&set->list);
2722 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
2723 nft_set_destroy(set);
2724 }
2725
2726 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2727 const struct nlmsghdr *nlh,
2728 const struct nlattr * const nla[])
2729 {
2730 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2731 struct nft_set *set;
2732 struct nft_ctx ctx;
2733 int err;
2734
2735 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2736 return -EAFNOSUPPORT;
2737 if (nla[NFTA_SET_TABLE] == NULL)
2738 return -EINVAL;
2739
2740 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2741 if (err < 0)
2742 return err;
2743
2744 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2745 if (IS_ERR(set))
2746 return PTR_ERR(set);
2747 if (set->flags & NFT_SET_INACTIVE)
2748 return -ENOENT;
2749 if (!list_empty(&set->bindings))
2750 return -EBUSY;
2751
2752 return nft_delset(&ctx, set);
2753 }
2754
2755 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2756 const struct nft_set *set,
2757 const struct nft_set_iter *iter,
2758 const struct nft_set_elem *elem)
2759 {
2760 enum nft_registers dreg;
2761
2762 dreg = nft_type_to_reg(set->dtype);
2763 return nft_validate_data_load(ctx, dreg, &elem->data,
2764 set->dtype == NFT_DATA_VERDICT ?
2765 NFT_DATA_VERDICT : NFT_DATA_VALUE);
2766 }
2767
2768 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2769 struct nft_set_binding *binding)
2770 {
2771 struct nft_set_binding *i;
2772 struct nft_set_iter iter;
2773
2774 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2775 return -EBUSY;
2776
2777 if (set->flags & NFT_SET_MAP) {
2778 /* If the set is already bound to the same chain all
2779 * jumps are already validated for that chain.
2780 */
2781 list_for_each_entry(i, &set->bindings, list) {
2782 if (i->chain == binding->chain)
2783 goto bind;
2784 }
2785
2786 iter.skip = 0;
2787 iter.count = 0;
2788 iter.err = 0;
2789 iter.fn = nf_tables_bind_check_setelem;
2790
2791 set->ops->walk(ctx, set, &iter);
2792 if (iter.err < 0) {
2793 /* Destroy anonymous sets if binding fails */
2794 if (set->flags & NFT_SET_ANONYMOUS)
2795 nf_tables_set_destroy(ctx, set);
2796
2797 return iter.err;
2798 }
2799 }
2800 bind:
2801 binding->chain = ctx->chain;
2802 list_add_tail_rcu(&binding->list, &set->bindings);
2803 return 0;
2804 }
2805
2806 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2807 struct nft_set_binding *binding)
2808 {
2809 list_del_rcu(&binding->list);
2810
2811 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2812 !(set->flags & NFT_SET_INACTIVE))
2813 nf_tables_set_destroy(ctx, set);
2814 }
2815
2816 /*
2817 * Set elements
2818 */
2819
2820 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2821 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2822 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2823 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2824 };
2825
2826 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2827 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2828 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2829 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2830 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
2831 };
2832
2833 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2834 const struct sk_buff *skb,
2835 const struct nlmsghdr *nlh,
2836 const struct nlattr * const nla[],
2837 bool trans)
2838 {
2839 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2840 struct nft_af_info *afi;
2841 struct nft_table *table;
2842 struct net *net = sock_net(skb->sk);
2843
2844 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2845 if (IS_ERR(afi))
2846 return PTR_ERR(afi);
2847
2848 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2849 if (IS_ERR(table))
2850 return PTR_ERR(table);
2851 if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2852 return -ENOENT;
2853
2854 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2855 return 0;
2856 }
2857
2858 static int nf_tables_fill_setelem(struct sk_buff *skb,
2859 const struct nft_set *set,
2860 const struct nft_set_elem *elem)
2861 {
2862 unsigned char *b = skb_tail_pointer(skb);
2863 struct nlattr *nest;
2864
2865 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2866 if (nest == NULL)
2867 goto nla_put_failure;
2868
2869 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2870 set->klen) < 0)
2871 goto nla_put_failure;
2872
2873 if (set->flags & NFT_SET_MAP &&
2874 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2875 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2876 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2877 set->dlen) < 0)
2878 goto nla_put_failure;
2879
2880 if (elem->flags != 0)
2881 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2882 goto nla_put_failure;
2883
2884 nla_nest_end(skb, nest);
2885 return 0;
2886
2887 nla_put_failure:
2888 nlmsg_trim(skb, b);
2889 return -EMSGSIZE;
2890 }
2891
2892 struct nft_set_dump_args {
2893 const struct netlink_callback *cb;
2894 struct nft_set_iter iter;
2895 struct sk_buff *skb;
2896 };
2897
2898 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2899 const struct nft_set *set,
2900 const struct nft_set_iter *iter,
2901 const struct nft_set_elem *elem)
2902 {
2903 struct nft_set_dump_args *args;
2904
2905 args = container_of(iter, struct nft_set_dump_args, iter);
2906 return nf_tables_fill_setelem(args->skb, set, elem);
2907 }
2908
2909 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2910 {
2911 const struct nft_set *set;
2912 struct nft_set_dump_args args;
2913 struct nft_ctx ctx;
2914 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2915 struct nfgenmsg *nfmsg;
2916 struct nlmsghdr *nlh;
2917 struct nlattr *nest;
2918 u32 portid, seq;
2919 int event, err;
2920
2921 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2922 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
2923 if (err < 0)
2924 return err;
2925
2926 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
2927 false);
2928 if (err < 0)
2929 return err;
2930
2931 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2932 if (IS_ERR(set))
2933 return PTR_ERR(set);
2934 if (set->flags & NFT_SET_INACTIVE)
2935 return -ENOENT;
2936
2937 event = NFT_MSG_NEWSETELEM;
2938 event |= NFNL_SUBSYS_NFTABLES << 8;
2939 portid = NETLINK_CB(cb->skb).portid;
2940 seq = cb->nlh->nlmsg_seq;
2941
2942 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2943 NLM_F_MULTI);
2944 if (nlh == NULL)
2945 goto nla_put_failure;
2946
2947 nfmsg = nlmsg_data(nlh);
2948 nfmsg->nfgen_family = ctx.afi->family;
2949 nfmsg->version = NFNETLINK_V0;
2950 nfmsg->res_id = htons(ctx.net->nft.base_seq & 0xffff);
2951
2952 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2953 goto nla_put_failure;
2954 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2955 goto nla_put_failure;
2956
2957 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2958 if (nest == NULL)
2959 goto nla_put_failure;
2960
2961 args.cb = cb;
2962 args.skb = skb;
2963 args.iter.skip = cb->args[0];
2964 args.iter.count = 0;
2965 args.iter.err = 0;
2966 args.iter.fn = nf_tables_dump_setelem;
2967 set->ops->walk(&ctx, set, &args.iter);
2968
2969 nla_nest_end(skb, nest);
2970 nlmsg_end(skb, nlh);
2971
2972 if (args.iter.err && args.iter.err != -EMSGSIZE)
2973 return args.iter.err;
2974 if (args.iter.count == cb->args[0])
2975 return 0;
2976
2977 cb->args[0] = args.iter.count;
2978 return skb->len;
2979
2980 nla_put_failure:
2981 return -ENOSPC;
2982 }
2983
2984 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2985 const struct nlmsghdr *nlh,
2986 const struct nlattr * const nla[])
2987 {
2988 const struct nft_set *set;
2989 struct nft_ctx ctx;
2990 int err;
2991
2992 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
2993 if (err < 0)
2994 return err;
2995
2996 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2997 if (IS_ERR(set))
2998 return PTR_ERR(set);
2999 if (set->flags & NFT_SET_INACTIVE)
3000 return -ENOENT;
3001
3002 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3003 struct netlink_dump_control c = {
3004 .dump = nf_tables_dump_set,
3005 };
3006 return netlink_dump_start(nlsk, skb, nlh, &c);
3007 }
3008 return -EOPNOTSUPP;
3009 }
3010
3011 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
3012 const struct nft_ctx *ctx, u32 seq,
3013 u32 portid, int event, u16 flags,
3014 const struct nft_set *set,
3015 const struct nft_set_elem *elem)
3016 {
3017 struct nfgenmsg *nfmsg;
3018 struct nlmsghdr *nlh;
3019 struct nlattr *nest;
3020 int err;
3021
3022 event |= NFNL_SUBSYS_NFTABLES << 8;
3023 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3024 flags);
3025 if (nlh == NULL)
3026 goto nla_put_failure;
3027
3028 nfmsg = nlmsg_data(nlh);
3029 nfmsg->nfgen_family = ctx->afi->family;
3030 nfmsg->version = NFNETLINK_V0;
3031 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
3032
3033 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3034 goto nla_put_failure;
3035 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3036 goto nla_put_failure;
3037
3038 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3039 if (nest == NULL)
3040 goto nla_put_failure;
3041
3042 err = nf_tables_fill_setelem(skb, set, elem);
3043 if (err < 0)
3044 goto nla_put_failure;
3045
3046 nla_nest_end(skb, nest);
3047
3048 nlmsg_end(skb, nlh);
3049 return 0;
3050
3051 nla_put_failure:
3052 nlmsg_trim(skb, nlh);
3053 return -1;
3054 }
3055
3056 static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
3057 const struct nft_set *set,
3058 const struct nft_set_elem *elem,
3059 int event, u16 flags)
3060 {
3061 struct net *net = ctx->net;
3062 u32 portid = ctx->portid;
3063 struct sk_buff *skb;
3064 int err;
3065
3066 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3067 return 0;
3068
3069 err = -ENOBUFS;
3070 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3071 if (skb == NULL)
3072 goto err;
3073
3074 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
3075 set, elem);
3076 if (err < 0) {
3077 kfree_skb(skb);
3078 goto err;
3079 }
3080
3081 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
3082 GFP_KERNEL);
3083 err:
3084 if (err < 0)
3085 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3086 return err;
3087 }
3088
3089 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3090 int msg_type,
3091 struct nft_set *set)
3092 {
3093 struct nft_trans *trans;
3094
3095 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3096 if (trans == NULL)
3097 return NULL;
3098
3099 nft_trans_elem_set(trans) = set;
3100 return trans;
3101 }
3102
3103 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
3104 const struct nlattr *attr)
3105 {
3106 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3107 struct nft_data_desc d1, d2;
3108 struct nft_set_elem elem;
3109 struct nft_set_binding *binding;
3110 enum nft_registers dreg;
3111 struct nft_trans *trans;
3112 int err;
3113
3114 if (set->size && set->nelems == set->size)
3115 return -ENFILE;
3116
3117 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3118 nft_set_elem_policy);
3119 if (err < 0)
3120 return err;
3121
3122 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3123 return -EINVAL;
3124
3125 elem.flags = 0;
3126 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
3127 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3128 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
3129 return -EINVAL;
3130 }
3131
3132 if (set->flags & NFT_SET_MAP) {
3133 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3134 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
3135 return -EINVAL;
3136 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3137 elem.flags & NFT_SET_ELEM_INTERVAL_END)
3138 return -EINVAL;
3139 } else {
3140 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3141 return -EINVAL;
3142 }
3143
3144 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3145 if (err < 0)
3146 goto err1;
3147 err = -EINVAL;
3148 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3149 goto err2;
3150
3151 err = -EEXIST;
3152 if (set->ops->get(set, &elem) == 0)
3153 goto err2;
3154
3155 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3156 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
3157 if (err < 0)
3158 goto err2;
3159
3160 err = -EINVAL;
3161 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3162 goto err3;
3163
3164 dreg = nft_type_to_reg(set->dtype);
3165 list_for_each_entry(binding, &set->bindings, list) {
3166 struct nft_ctx bind_ctx = {
3167 .afi = ctx->afi,
3168 .table = ctx->table,
3169 .chain = (struct nft_chain *)binding->chain,
3170 };
3171
3172 err = nft_validate_data_load(&bind_ctx, dreg,
3173 &elem.data, d2.type);
3174 if (err < 0)
3175 goto err3;
3176 }
3177 }
3178
3179 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3180 if (trans == NULL)
3181 goto err3;
3182
3183 err = set->ops->insert(set, &elem);
3184 if (err < 0)
3185 goto err4;
3186
3187 nft_trans_elem(trans) = elem;
3188 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3189 return 0;
3190
3191 err4:
3192 kfree(trans);
3193 err3:
3194 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3195 nft_data_uninit(&elem.data, d2.type);
3196 err2:
3197 nft_data_uninit(&elem.key, d1.type);
3198 err1:
3199 return err;
3200 }
3201
3202 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3203 const struct nlmsghdr *nlh,
3204 const struct nlattr * const nla[])
3205 {
3206 struct net *net = sock_net(skb->sk);
3207 const struct nlattr *attr;
3208 struct nft_set *set;
3209 struct nft_ctx ctx;
3210 int rem, err = 0;
3211
3212 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3213 return -EINVAL;
3214
3215 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
3216 if (err < 0)
3217 return err;
3218
3219 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3220 if (IS_ERR(set)) {
3221 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3222 set = nf_tables_set_lookup_byid(net,
3223 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3224 }
3225 if (IS_ERR(set))
3226 return PTR_ERR(set);
3227 }
3228
3229 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3230 return -EBUSY;
3231
3232 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3233 err = nft_add_set_elem(&ctx, set, attr);
3234 if (err < 0)
3235 break;
3236
3237 set->nelems++;
3238 }
3239 return err;
3240 }
3241
3242 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
3243 const struct nlattr *attr)
3244 {
3245 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3246 struct nft_data_desc desc;
3247 struct nft_set_elem elem;
3248 struct nft_trans *trans;
3249 int err;
3250
3251 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3252 nft_set_elem_policy);
3253 if (err < 0)
3254 goto err1;
3255
3256 err = -EINVAL;
3257 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3258 goto err1;
3259
3260 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3261 if (err < 0)
3262 goto err1;
3263
3264 err = -EINVAL;
3265 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3266 goto err2;
3267
3268 err = set->ops->get(set, &elem);
3269 if (err < 0)
3270 goto err2;
3271
3272 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
3273 if (trans == NULL) {
3274 err = -ENOMEM;
3275 goto err2;
3276 }
3277
3278 nft_trans_elem(trans) = elem;
3279 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3280 return 0;
3281 err2:
3282 nft_data_uninit(&elem.key, desc.type);
3283 err1:
3284 return err;
3285 }
3286
3287 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3288 const struct nlmsghdr *nlh,
3289 const struct nlattr * const nla[])
3290 {
3291 const struct nlattr *attr;
3292 struct nft_set *set;
3293 struct nft_ctx ctx;
3294 int rem, err = 0;
3295
3296 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3297 return -EINVAL;
3298
3299 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
3300 if (err < 0)
3301 return err;
3302
3303 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3304 if (IS_ERR(set))
3305 return PTR_ERR(set);
3306 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3307 return -EBUSY;
3308
3309 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3310 err = nft_del_setelem(&ctx, set, attr);
3311 if (err < 0)
3312 break;
3313
3314 set->nelems--;
3315 }
3316 return err;
3317 }
3318
3319 static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
3320 u32 portid, u32 seq)
3321 {
3322 struct nlmsghdr *nlh;
3323 struct nfgenmsg *nfmsg;
3324 int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWGEN;
3325
3326 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
3327 if (nlh == NULL)
3328 goto nla_put_failure;
3329
3330 nfmsg = nlmsg_data(nlh);
3331 nfmsg->nfgen_family = AF_UNSPEC;
3332 nfmsg->version = NFNETLINK_V0;
3333 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
3334
3335 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)))
3336 goto nla_put_failure;
3337
3338 nlmsg_end(skb, nlh);
3339 return 0;
3340
3341 nla_put_failure:
3342 nlmsg_trim(skb, nlh);
3343 return -EMSGSIZE;
3344 }
3345
3346 static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event)
3347 {
3348 struct nlmsghdr *nlh = nlmsg_hdr(skb);
3349 struct sk_buff *skb2;
3350 int err;
3351
3352 if (nlmsg_report(nlh) &&
3353 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3354 return 0;
3355
3356 err = -ENOBUFS;
3357 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3358 if (skb2 == NULL)
3359 goto err;
3360
3361 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3362 nlh->nlmsg_seq);
3363 if (err < 0) {
3364 kfree_skb(skb2);
3365 goto err;
3366 }
3367
3368 err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid,
3369 NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL);
3370 err:
3371 if (err < 0) {
3372 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
3373 err);
3374 }
3375 return err;
3376 }
3377
3378 static int nf_tables_getgen(struct sock *nlsk, struct sk_buff *skb,
3379 const struct nlmsghdr *nlh,
3380 const struct nlattr * const nla[])
3381 {
3382 struct net *net = sock_net(skb->sk);
3383 struct sk_buff *skb2;
3384 int err;
3385
3386 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3387 if (skb2 == NULL)
3388 return -ENOMEM;
3389
3390 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3391 nlh->nlmsg_seq);
3392 if (err < 0)
3393 goto err;
3394
3395 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
3396 err:
3397 kfree_skb(skb2);
3398 return err;
3399 }
3400
3401 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3402 [NFT_MSG_NEWTABLE] = {
3403 .call_batch = nf_tables_newtable,
3404 .attr_count = NFTA_TABLE_MAX,
3405 .policy = nft_table_policy,
3406 },
3407 [NFT_MSG_GETTABLE] = {
3408 .call = nf_tables_gettable,
3409 .attr_count = NFTA_TABLE_MAX,
3410 .policy = nft_table_policy,
3411 },
3412 [NFT_MSG_DELTABLE] = {
3413 .call_batch = nf_tables_deltable,
3414 .attr_count = NFTA_TABLE_MAX,
3415 .policy = nft_table_policy,
3416 },
3417 [NFT_MSG_NEWCHAIN] = {
3418 .call_batch = nf_tables_newchain,
3419 .attr_count = NFTA_CHAIN_MAX,
3420 .policy = nft_chain_policy,
3421 },
3422 [NFT_MSG_GETCHAIN] = {
3423 .call = nf_tables_getchain,
3424 .attr_count = NFTA_CHAIN_MAX,
3425 .policy = nft_chain_policy,
3426 },
3427 [NFT_MSG_DELCHAIN] = {
3428 .call_batch = nf_tables_delchain,
3429 .attr_count = NFTA_CHAIN_MAX,
3430 .policy = nft_chain_policy,
3431 },
3432 [NFT_MSG_NEWRULE] = {
3433 .call_batch = nf_tables_newrule,
3434 .attr_count = NFTA_RULE_MAX,
3435 .policy = nft_rule_policy,
3436 },
3437 [NFT_MSG_GETRULE] = {
3438 .call = nf_tables_getrule,
3439 .attr_count = NFTA_RULE_MAX,
3440 .policy = nft_rule_policy,
3441 },
3442 [NFT_MSG_DELRULE] = {
3443 .call_batch = nf_tables_delrule,
3444 .attr_count = NFTA_RULE_MAX,
3445 .policy = nft_rule_policy,
3446 },
3447 [NFT_MSG_NEWSET] = {
3448 .call_batch = nf_tables_newset,
3449 .attr_count = NFTA_SET_MAX,
3450 .policy = nft_set_policy,
3451 },
3452 [NFT_MSG_GETSET] = {
3453 .call = nf_tables_getset,
3454 .attr_count = NFTA_SET_MAX,
3455 .policy = nft_set_policy,
3456 },
3457 [NFT_MSG_DELSET] = {
3458 .call_batch = nf_tables_delset,
3459 .attr_count = NFTA_SET_MAX,
3460 .policy = nft_set_policy,
3461 },
3462 [NFT_MSG_NEWSETELEM] = {
3463 .call_batch = nf_tables_newsetelem,
3464 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3465 .policy = nft_set_elem_list_policy,
3466 },
3467 [NFT_MSG_GETSETELEM] = {
3468 .call = nf_tables_getsetelem,
3469 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3470 .policy = nft_set_elem_list_policy,
3471 },
3472 [NFT_MSG_DELSETELEM] = {
3473 .call_batch = nf_tables_delsetelem,
3474 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3475 .policy = nft_set_elem_list_policy,
3476 },
3477 [NFT_MSG_GETGEN] = {
3478 .call = nf_tables_getgen,
3479 },
3480 };
3481
3482 static void nft_chain_commit_update(struct nft_trans *trans)
3483 {
3484 struct nft_base_chain *basechain;
3485
3486 if (nft_trans_chain_name(trans)[0])
3487 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3488
3489 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3490 return;
3491
3492 basechain = nft_base_chain(trans->ctx.chain);
3493 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3494
3495 switch (nft_trans_chain_policy(trans)) {
3496 case NF_DROP:
3497 case NF_ACCEPT:
3498 basechain->policy = nft_trans_chain_policy(trans);
3499 break;
3500 }
3501 }
3502
3503 static void nf_tables_commit_release(struct nft_trans *trans)
3504 {
3505 switch (trans->msg_type) {
3506 case NFT_MSG_DELTABLE:
3507 nf_tables_table_destroy(&trans->ctx);
3508 break;
3509 case NFT_MSG_DELCHAIN:
3510 nf_tables_chain_destroy(trans->ctx.chain);
3511 break;
3512 case NFT_MSG_DELRULE:
3513 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3514 break;
3515 case NFT_MSG_DELSET:
3516 nft_set_destroy(nft_trans_set(trans));
3517 break;
3518 }
3519 kfree(trans);
3520 }
3521
3522 static int nf_tables_commit(struct sk_buff *skb)
3523 {
3524 struct net *net = sock_net(skb->sk);
3525 struct nft_trans *trans, *next;
3526 struct nft_trans_elem *te;
3527
3528 /* Bump generation counter, invalidate any dump in progress */
3529 while (++net->nft.base_seq == 0);
3530
3531 /* A new generation has just started */
3532 net->nft.gencursor = gencursor_next(net);
3533
3534 /* Make sure all packets have left the previous generation before
3535 * purging old rules.
3536 */
3537 synchronize_rcu();
3538
3539 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3540 switch (trans->msg_type) {
3541 case NFT_MSG_NEWTABLE:
3542 if (nft_trans_table_update(trans)) {
3543 if (!nft_trans_table_enable(trans)) {
3544 nf_tables_table_disable(trans->ctx.afi,
3545 trans->ctx.table);
3546 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3547 }
3548 } else {
3549 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3550 }
3551 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
3552 nft_trans_destroy(trans);
3553 break;
3554 case NFT_MSG_DELTABLE:
3555 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
3556 break;
3557 case NFT_MSG_NEWCHAIN:
3558 if (nft_trans_chain_update(trans))
3559 nft_chain_commit_update(trans);
3560 else
3561 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
3562
3563 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
3564 nft_trans_destroy(trans);
3565 break;
3566 case NFT_MSG_DELCHAIN:
3567 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
3568 nf_tables_unregister_hooks(trans->ctx.table,
3569 trans->ctx.chain,
3570 trans->ctx.afi->nops);
3571 break;
3572 case NFT_MSG_NEWRULE:
3573 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3574 nf_tables_rule_notify(&trans->ctx,
3575 nft_trans_rule(trans),
3576 NFT_MSG_NEWRULE);
3577 nft_trans_destroy(trans);
3578 break;
3579 case NFT_MSG_DELRULE:
3580 list_del_rcu(&nft_trans_rule(trans)->list);
3581 nf_tables_rule_notify(&trans->ctx,
3582 nft_trans_rule(trans),
3583 NFT_MSG_DELRULE);
3584 break;
3585 case NFT_MSG_NEWSET:
3586 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
3587 /* This avoids hitting -EBUSY when deleting the table
3588 * from the transaction.
3589 */
3590 if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
3591 !list_empty(&nft_trans_set(trans)->bindings))
3592 trans->ctx.table->use--;
3593
3594 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3595 NFT_MSG_NEWSET, GFP_KERNEL);
3596 nft_trans_destroy(trans);
3597 break;
3598 case NFT_MSG_DELSET:
3599 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3600 NFT_MSG_DELSET, GFP_KERNEL);
3601 break;
3602 case NFT_MSG_NEWSETELEM:
3603 nf_tables_setelem_notify(&trans->ctx,
3604 nft_trans_elem_set(trans),
3605 &nft_trans_elem(trans),
3606 NFT_MSG_NEWSETELEM, 0);
3607 nft_trans_destroy(trans);
3608 break;
3609 case NFT_MSG_DELSETELEM:
3610 te = (struct nft_trans_elem *)trans->data;
3611 nf_tables_setelem_notify(&trans->ctx, te->set,
3612 &te->elem,
3613 NFT_MSG_DELSETELEM, 0);
3614 te->set->ops->get(te->set, &te->elem);
3615 nft_data_uninit(&te->elem.key, NFT_DATA_VALUE);
3616 if (te->set->flags & NFT_SET_MAP &&
3617 !(te->elem.flags & NFT_SET_ELEM_INTERVAL_END))
3618 nft_data_uninit(&te->elem.data, te->set->dtype);
3619 te->set->ops->remove(te->set, &te->elem);
3620 nft_trans_destroy(trans);
3621 break;
3622 }
3623 }
3624
3625 synchronize_rcu();
3626
3627 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3628 list_del(&trans->list);
3629 nf_tables_commit_release(trans);
3630 }
3631
3632 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
3633
3634 return 0;
3635 }
3636
3637 static void nf_tables_abort_release(struct nft_trans *trans)
3638 {
3639 switch (trans->msg_type) {
3640 case NFT_MSG_NEWTABLE:
3641 nf_tables_table_destroy(&trans->ctx);
3642 break;
3643 case NFT_MSG_NEWCHAIN:
3644 nf_tables_chain_destroy(trans->ctx.chain);
3645 break;
3646 case NFT_MSG_NEWRULE:
3647 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3648 break;
3649 case NFT_MSG_NEWSET:
3650 nft_set_destroy(nft_trans_set(trans));
3651 break;
3652 }
3653 kfree(trans);
3654 }
3655
3656 static int nf_tables_abort(struct sk_buff *skb)
3657 {
3658 struct net *net = sock_net(skb->sk);
3659 struct nft_trans *trans, *next;
3660 struct nft_trans_elem *te;
3661
3662 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3663 switch (trans->msg_type) {
3664 case NFT_MSG_NEWTABLE:
3665 if (nft_trans_table_update(trans)) {
3666 if (nft_trans_table_enable(trans)) {
3667 nf_tables_table_disable(trans->ctx.afi,
3668 trans->ctx.table);
3669 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3670 }
3671 nft_trans_destroy(trans);
3672 } else {
3673 list_del_rcu(&trans->ctx.table->list);
3674 }
3675 break;
3676 case NFT_MSG_DELTABLE:
3677 list_add_tail_rcu(&trans->ctx.table->list,
3678 &trans->ctx.afi->tables);
3679 nft_trans_destroy(trans);
3680 break;
3681 case NFT_MSG_NEWCHAIN:
3682 if (nft_trans_chain_update(trans)) {
3683 free_percpu(nft_trans_chain_stats(trans));
3684
3685 nft_trans_destroy(trans);
3686 } else {
3687 trans->ctx.table->use--;
3688 list_del_rcu(&trans->ctx.chain->list);
3689 nf_tables_unregister_hooks(trans->ctx.table,
3690 trans->ctx.chain,
3691 trans->ctx.afi->nops);
3692 }
3693 break;
3694 case NFT_MSG_DELCHAIN:
3695 trans->ctx.table->use++;
3696 list_add_tail_rcu(&trans->ctx.chain->list,
3697 &trans->ctx.table->chains);
3698 nft_trans_destroy(trans);
3699 break;
3700 case NFT_MSG_NEWRULE:
3701 trans->ctx.chain->use--;
3702 list_del_rcu(&nft_trans_rule(trans)->list);
3703 break;
3704 case NFT_MSG_DELRULE:
3705 trans->ctx.chain->use++;
3706 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3707 nft_trans_destroy(trans);
3708 break;
3709 case NFT_MSG_NEWSET:
3710 trans->ctx.table->use--;
3711 list_del_rcu(&nft_trans_set(trans)->list);
3712 break;
3713 case NFT_MSG_DELSET:
3714 trans->ctx.table->use++;
3715 list_add_tail_rcu(&nft_trans_set(trans)->list,
3716 &trans->ctx.table->sets);
3717 nft_trans_destroy(trans);
3718 break;
3719 case NFT_MSG_NEWSETELEM:
3720 nft_trans_elem_set(trans)->nelems--;
3721 te = (struct nft_trans_elem *)trans->data;
3722 te->set->ops->get(te->set, &te->elem);
3723 nft_data_uninit(&te->elem.key, NFT_DATA_VALUE);
3724 if (te->set->flags & NFT_SET_MAP &&
3725 !(te->elem.flags & NFT_SET_ELEM_INTERVAL_END))
3726 nft_data_uninit(&te->elem.data, te->set->dtype);
3727 te->set->ops->remove(te->set, &te->elem);
3728 nft_trans_destroy(trans);
3729 break;
3730 case NFT_MSG_DELSETELEM:
3731 nft_trans_elem_set(trans)->nelems++;
3732 nft_trans_destroy(trans);
3733 break;
3734 }
3735 }
3736
3737 synchronize_rcu();
3738
3739 list_for_each_entry_safe_reverse(trans, next,
3740 &net->nft.commit_list, list) {
3741 list_del(&trans->list);
3742 nf_tables_abort_release(trans);
3743 }
3744
3745 return 0;
3746 }
3747
3748 static const struct nfnetlink_subsystem nf_tables_subsys = {
3749 .name = "nf_tables",
3750 .subsys_id = NFNL_SUBSYS_NFTABLES,
3751 .cb_count = NFT_MSG_MAX,
3752 .cb = nf_tables_cb,
3753 .commit = nf_tables_commit,
3754 .abort = nf_tables_abort,
3755 };
3756
3757 int nft_chain_validate_dependency(const struct nft_chain *chain,
3758 enum nft_chain_type type)
3759 {
3760 const struct nft_base_chain *basechain;
3761
3762 if (chain->flags & NFT_BASE_CHAIN) {
3763 basechain = nft_base_chain(chain);
3764 if (basechain->type->type != type)
3765 return -EOPNOTSUPP;
3766 }
3767 return 0;
3768 }
3769 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
3770
3771 int nft_chain_validate_hooks(const struct nft_chain *chain,
3772 unsigned int hook_flags)
3773 {
3774 struct nft_base_chain *basechain;
3775
3776 if (chain->flags & NFT_BASE_CHAIN) {
3777 basechain = nft_base_chain(chain);
3778
3779 if ((1 << basechain->ops[0].hooknum) & hook_flags)
3780 return 0;
3781
3782 return -EOPNOTSUPP;
3783 }
3784
3785 return 0;
3786 }
3787 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
3788
3789 /*
3790 * Loop detection - walk through the ruleset beginning at the destination chain
3791 * of a new jump until either the source chain is reached (loop) or all
3792 * reachable chains have been traversed.
3793 *
3794 * The loop check is performed whenever a new jump verdict is added to an
3795 * expression or verdict map or a verdict map is bound to a new chain.
3796 */
3797
3798 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3799 const struct nft_chain *chain);
3800
3801 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3802 const struct nft_set *set,
3803 const struct nft_set_iter *iter,
3804 const struct nft_set_elem *elem)
3805 {
3806 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3807 return 0;
3808
3809 switch (elem->data.verdict) {
3810 case NFT_JUMP:
3811 case NFT_GOTO:
3812 return nf_tables_check_loops(ctx, elem->data.chain);
3813 default:
3814 return 0;
3815 }
3816 }
3817
3818 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3819 const struct nft_chain *chain)
3820 {
3821 const struct nft_rule *rule;
3822 const struct nft_expr *expr, *last;
3823 const struct nft_set *set;
3824 struct nft_set_binding *binding;
3825 struct nft_set_iter iter;
3826
3827 if (ctx->chain == chain)
3828 return -ELOOP;
3829
3830 list_for_each_entry(rule, &chain->rules, list) {
3831 nft_rule_for_each_expr(expr, last, rule) {
3832 const struct nft_data *data = NULL;
3833 int err;
3834
3835 if (!expr->ops->validate)
3836 continue;
3837
3838 err = expr->ops->validate(ctx, expr, &data);
3839 if (err < 0)
3840 return err;
3841
3842 if (data == NULL)
3843 continue;
3844
3845 switch (data->verdict) {
3846 case NFT_JUMP:
3847 case NFT_GOTO:
3848 err = nf_tables_check_loops(ctx, data->chain);
3849 if (err < 0)
3850 return err;
3851 default:
3852 break;
3853 }
3854 }
3855 }
3856
3857 list_for_each_entry(set, &ctx->table->sets, list) {
3858 if (!(set->flags & NFT_SET_MAP) ||
3859 set->dtype != NFT_DATA_VERDICT)
3860 continue;
3861
3862 list_for_each_entry(binding, &set->bindings, list) {
3863 if (binding->chain != chain)
3864 continue;
3865
3866 iter.skip = 0;
3867 iter.count = 0;
3868 iter.err = 0;
3869 iter.fn = nf_tables_loop_check_setelem;
3870
3871 set->ops->walk(ctx, set, &iter);
3872 if (iter.err < 0)
3873 return iter.err;
3874 }
3875 }
3876
3877 return 0;
3878 }
3879
3880 /**
3881 * nft_validate_input_register - validate an expressions' input register
3882 *
3883 * @reg: the register number
3884 *
3885 * Validate that the input register is one of the general purpose
3886 * registers.
3887 */
3888 int nft_validate_input_register(enum nft_registers reg)
3889 {
3890 if (reg <= NFT_REG_VERDICT)
3891 return -EINVAL;
3892 if (reg > NFT_REG_MAX)
3893 return -ERANGE;
3894 return 0;
3895 }
3896 EXPORT_SYMBOL_GPL(nft_validate_input_register);
3897
3898 /**
3899 * nft_validate_output_register - validate an expressions' output register
3900 *
3901 * @reg: the register number
3902 *
3903 * Validate that the output register is one of the general purpose
3904 * registers or the verdict register.
3905 */
3906 int nft_validate_output_register(enum nft_registers reg)
3907 {
3908 if (reg < NFT_REG_VERDICT)
3909 return -EINVAL;
3910 if (reg > NFT_REG_MAX)
3911 return -ERANGE;
3912 return 0;
3913 }
3914 EXPORT_SYMBOL_GPL(nft_validate_output_register);
3915
3916 /**
3917 * nft_validate_data_load - validate an expressions' data load
3918 *
3919 * @ctx: context of the expression performing the load
3920 * @reg: the destination register number
3921 * @data: the data to load
3922 * @type: the data type
3923 *
3924 * Validate that a data load uses the appropriate data type for
3925 * the destination register. A value of NULL for the data means
3926 * that its runtime gathered data, which is always of type
3927 * NFT_DATA_VALUE.
3928 */
3929 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3930 const struct nft_data *data,
3931 enum nft_data_types type)
3932 {
3933 int err;
3934
3935 switch (reg) {
3936 case NFT_REG_VERDICT:
3937 if (data == NULL || type != NFT_DATA_VERDICT)
3938 return -EINVAL;
3939
3940 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3941 err = nf_tables_check_loops(ctx, data->chain);
3942 if (err < 0)
3943 return err;
3944
3945 if (ctx->chain->level + 1 > data->chain->level) {
3946 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3947 return -EMLINK;
3948 data->chain->level = ctx->chain->level + 1;
3949 }
3950 }
3951
3952 return 0;
3953 default:
3954 if (data != NULL && type != NFT_DATA_VALUE)
3955 return -EINVAL;
3956 return 0;
3957 }
3958 }
3959 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3960
3961 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3962 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3963 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3964 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3965 };
3966
3967 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3968 struct nft_data_desc *desc, const struct nlattr *nla)
3969 {
3970 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3971 struct nft_chain *chain;
3972 int err;
3973
3974 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3975 if (err < 0)
3976 return err;
3977
3978 if (!tb[NFTA_VERDICT_CODE])
3979 return -EINVAL;
3980 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3981
3982 switch (data->verdict) {
3983 default:
3984 switch (data->verdict & NF_VERDICT_MASK) {
3985 case NF_ACCEPT:
3986 case NF_DROP:
3987 case NF_QUEUE:
3988 break;
3989 default:
3990 return -EINVAL;
3991 }
3992 /* fall through */
3993 case NFT_CONTINUE:
3994 case NFT_BREAK:
3995 case NFT_RETURN:
3996 desc->len = sizeof(data->verdict);
3997 break;
3998 case NFT_JUMP:
3999 case NFT_GOTO:
4000 if (!tb[NFTA_VERDICT_CHAIN])
4001 return -EINVAL;
4002 chain = nf_tables_chain_lookup(ctx->table,
4003 tb[NFTA_VERDICT_CHAIN]);
4004 if (IS_ERR(chain))
4005 return PTR_ERR(chain);
4006 if (chain->flags & NFT_BASE_CHAIN)
4007 return -EOPNOTSUPP;
4008
4009 chain->use++;
4010 data->chain = chain;
4011 desc->len = sizeof(data);
4012 break;
4013 }
4014
4015 desc->type = NFT_DATA_VERDICT;
4016 return 0;
4017 }
4018
4019 static void nft_verdict_uninit(const struct nft_data *data)
4020 {
4021 switch (data->verdict) {
4022 case NFT_JUMP:
4023 case NFT_GOTO:
4024 data->chain->use--;
4025 break;
4026 }
4027 }
4028
4029 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
4030 {
4031 struct nlattr *nest;
4032
4033 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
4034 if (!nest)
4035 goto nla_put_failure;
4036
4037 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
4038 goto nla_put_failure;
4039
4040 switch (data->verdict) {
4041 case NFT_JUMP:
4042 case NFT_GOTO:
4043 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
4044 goto nla_put_failure;
4045 }
4046 nla_nest_end(skb, nest);
4047 return 0;
4048
4049 nla_put_failure:
4050 return -1;
4051 }
4052
4053 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
4054 struct nft_data_desc *desc, const struct nlattr *nla)
4055 {
4056 unsigned int len;
4057
4058 len = nla_len(nla);
4059 if (len == 0)
4060 return -EINVAL;
4061 if (len > sizeof(data->data))
4062 return -EOVERFLOW;
4063
4064 nla_memcpy(data->data, nla, sizeof(data->data));
4065 desc->type = NFT_DATA_VALUE;
4066 desc->len = len;
4067 return 0;
4068 }
4069
4070 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
4071 unsigned int len)
4072 {
4073 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
4074 }
4075
4076 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
4077 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
4078 .len = FIELD_SIZEOF(struct nft_data, data) },
4079 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
4080 };
4081
4082 /**
4083 * nft_data_init - parse nf_tables data netlink attributes
4084 *
4085 * @ctx: context of the expression using the data
4086 * @data: destination struct nft_data
4087 * @desc: data description
4088 * @nla: netlink attribute containing data
4089 *
4090 * Parse the netlink data attributes and initialize a struct nft_data.
4091 * The type and length of data are returned in the data description.
4092 *
4093 * The caller can indicate that it only wants to accept data of type
4094 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4095 */
4096 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
4097 struct nft_data_desc *desc, const struct nlattr *nla)
4098 {
4099 struct nlattr *tb[NFTA_DATA_MAX + 1];
4100 int err;
4101
4102 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
4103 if (err < 0)
4104 return err;
4105
4106 if (tb[NFTA_DATA_VALUE])
4107 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
4108 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
4109 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
4110 return -EINVAL;
4111 }
4112 EXPORT_SYMBOL_GPL(nft_data_init);
4113
4114 /**
4115 * nft_data_uninit - release a nft_data item
4116 *
4117 * @data: struct nft_data to release
4118 * @type: type of data
4119 *
4120 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4121 * all others need to be released by calling this function.
4122 */
4123 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
4124 {
4125 switch (type) {
4126 case NFT_DATA_VALUE:
4127 return;
4128 case NFT_DATA_VERDICT:
4129 return nft_verdict_uninit(data);
4130 default:
4131 WARN_ON(1);
4132 }
4133 }
4134 EXPORT_SYMBOL_GPL(nft_data_uninit);
4135
4136 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
4137 enum nft_data_types type, unsigned int len)
4138 {
4139 struct nlattr *nest;
4140 int err;
4141
4142 nest = nla_nest_start(skb, attr);
4143 if (nest == NULL)
4144 return -1;
4145
4146 switch (type) {
4147 case NFT_DATA_VALUE:
4148 err = nft_value_dump(skb, data, len);
4149 break;
4150 case NFT_DATA_VERDICT:
4151 err = nft_verdict_dump(skb, data);
4152 break;
4153 default:
4154 err = -EINVAL;
4155 WARN_ON(1);
4156 }
4157
4158 nla_nest_end(skb, nest);
4159 return err;
4160 }
4161 EXPORT_SYMBOL_GPL(nft_data_dump);
4162
4163 static int nf_tables_init_net(struct net *net)
4164 {
4165 INIT_LIST_HEAD(&net->nft.af_info);
4166 INIT_LIST_HEAD(&net->nft.commit_list);
4167 net->nft.base_seq = 1;
4168 return 0;
4169 }
4170
4171 static struct pernet_operations nf_tables_net_ops = {
4172 .init = nf_tables_init_net,
4173 };
4174
4175 static int __init nf_tables_module_init(void)
4176 {
4177 int err;
4178
4179 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
4180 GFP_KERNEL);
4181 if (info == NULL) {
4182 err = -ENOMEM;
4183 goto err1;
4184 }
4185
4186 err = nf_tables_core_module_init();
4187 if (err < 0)
4188 goto err2;
4189
4190 err = nfnetlink_subsys_register(&nf_tables_subsys);
4191 if (err < 0)
4192 goto err3;
4193
4194 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
4195 return register_pernet_subsys(&nf_tables_net_ops);
4196 err3:
4197 nf_tables_core_module_exit();
4198 err2:
4199 kfree(info);
4200 err1:
4201 return err;
4202 }
4203
4204 static void __exit nf_tables_module_exit(void)
4205 {
4206 unregister_pernet_subsys(&nf_tables_net_ops);
4207 nfnetlink_subsys_unregister(&nf_tables_subsys);
4208 rcu_barrier();
4209 nf_tables_core_module_exit();
4210 kfree(info);
4211 }
4212
4213 module_init(nf_tables_module_init);
4214 module_exit(nf_tables_module_exit);
4215
4216 MODULE_LICENSE("GPL");
4217 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4218 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);
This page took 0.177453 seconds and 5 git commands to generate.