]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - net/netfilter/nf_tables_api.c
netlink: make nlmsg_end() and genlmsg_end() void
[mirror_ubuntu-jammy-kernel.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 = 0;
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 stats = this_cpu_ptr(newstats);
1140 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1141 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
1142
1143 return newstats;
1144 }
1145
1146 static void nft_chain_stats_replace(struct nft_base_chain *chain,
1147 struct nft_stats __percpu *newstats)
1148 {
1149 if (newstats == NULL)
1150 return;
1151
1152 if (chain->stats) {
1153 struct nft_stats __percpu *oldstats =
1154 nft_dereference(chain->stats);
1155
1156 rcu_assign_pointer(chain->stats, newstats);
1157 synchronize_rcu();
1158 free_percpu(oldstats);
1159 } else
1160 rcu_assign_pointer(chain->stats, newstats);
1161 }
1162
1163 static void nf_tables_chain_destroy(struct nft_chain *chain)
1164 {
1165 BUG_ON(chain->use > 0);
1166
1167 if (chain->flags & NFT_BASE_CHAIN) {
1168 module_put(nft_base_chain(chain)->type->owner);
1169 free_percpu(nft_base_chain(chain)->stats);
1170 kfree(nft_base_chain(chain));
1171 } else {
1172 kfree(chain);
1173 }
1174 }
1175
1176 static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
1177 const struct nlmsghdr *nlh,
1178 const struct nlattr * const nla[])
1179 {
1180 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1181 const struct nlattr * uninitialized_var(name);
1182 struct nft_af_info *afi;
1183 struct nft_table *table;
1184 struct nft_chain *chain;
1185 struct nft_base_chain *basechain = NULL;
1186 struct nlattr *ha[NFTA_HOOK_MAX + 1];
1187 struct net *net = sock_net(skb->sk);
1188 int family = nfmsg->nfgen_family;
1189 u8 policy = NF_ACCEPT;
1190 u64 handle = 0;
1191 unsigned int i;
1192 struct nft_stats __percpu *stats;
1193 int err;
1194 bool create;
1195 struct nft_ctx ctx;
1196
1197 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1198
1199 afi = nf_tables_afinfo_lookup(net, family, true);
1200 if (IS_ERR(afi))
1201 return PTR_ERR(afi);
1202
1203 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1204 if (IS_ERR(table))
1205 return PTR_ERR(table);
1206
1207 chain = NULL;
1208 name = nla[NFTA_CHAIN_NAME];
1209
1210 if (nla[NFTA_CHAIN_HANDLE]) {
1211 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
1212 chain = nf_tables_chain_lookup_byhandle(table, handle);
1213 if (IS_ERR(chain))
1214 return PTR_ERR(chain);
1215 } else {
1216 chain = nf_tables_chain_lookup(table, name);
1217 if (IS_ERR(chain)) {
1218 if (PTR_ERR(chain) != -ENOENT)
1219 return PTR_ERR(chain);
1220 chain = NULL;
1221 }
1222 }
1223
1224 if (nla[NFTA_CHAIN_POLICY]) {
1225 if ((chain != NULL &&
1226 !(chain->flags & NFT_BASE_CHAIN)) ||
1227 nla[NFTA_CHAIN_HOOK] == NULL)
1228 return -EOPNOTSUPP;
1229
1230 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
1231 switch (policy) {
1232 case NF_DROP:
1233 case NF_ACCEPT:
1234 break;
1235 default:
1236 return -EINVAL;
1237 }
1238 }
1239
1240 if (chain != NULL) {
1241 struct nft_stats *stats = NULL;
1242 struct nft_trans *trans;
1243
1244 if (chain->flags & NFT_CHAIN_INACTIVE)
1245 return -ENOENT;
1246 if (nlh->nlmsg_flags & NLM_F_EXCL)
1247 return -EEXIST;
1248 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1249 return -EOPNOTSUPP;
1250
1251 if (nla[NFTA_CHAIN_HANDLE] && name &&
1252 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1253 return -EEXIST;
1254
1255 if (nla[NFTA_CHAIN_COUNTERS]) {
1256 if (!(chain->flags & NFT_BASE_CHAIN))
1257 return -EOPNOTSUPP;
1258
1259 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1260 if (IS_ERR(stats))
1261 return PTR_ERR(stats);
1262 }
1263
1264 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1265 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1266 sizeof(struct nft_trans_chain));
1267 if (trans == NULL)
1268 return -ENOMEM;
1269
1270 nft_trans_chain_stats(trans) = stats;
1271 nft_trans_chain_update(trans) = true;
1272
1273 if (nla[NFTA_CHAIN_POLICY])
1274 nft_trans_chain_policy(trans) = policy;
1275 else
1276 nft_trans_chain_policy(trans) = -1;
1277
1278 if (nla[NFTA_CHAIN_HANDLE] && name) {
1279 nla_strlcpy(nft_trans_chain_name(trans), name,
1280 NFT_CHAIN_MAXNAMELEN);
1281 }
1282 list_add_tail(&trans->list, &net->nft.commit_list);
1283 return 0;
1284 }
1285
1286 if (table->use == UINT_MAX)
1287 return -EOVERFLOW;
1288
1289 if (nla[NFTA_CHAIN_HOOK]) {
1290 const struct nf_chain_type *type;
1291 struct nf_hook_ops *ops;
1292 nf_hookfn *hookfn;
1293 u32 hooknum, priority;
1294
1295 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
1296 if (nla[NFTA_CHAIN_TYPE]) {
1297 type = nf_tables_chain_type_lookup(afi,
1298 nla[NFTA_CHAIN_TYPE],
1299 create);
1300 if (IS_ERR(type))
1301 return PTR_ERR(type);
1302 }
1303
1304 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1305 nft_hook_policy);
1306 if (err < 0)
1307 return err;
1308 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1309 ha[NFTA_HOOK_PRIORITY] == NULL)
1310 return -EINVAL;
1311
1312 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1313 if (hooknum >= afi->nhooks)
1314 return -EINVAL;
1315 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
1316
1317 if (!(type->hook_mask & (1 << hooknum)))
1318 return -EOPNOTSUPP;
1319 if (!try_module_get(type->owner))
1320 return -ENOENT;
1321 hookfn = type->hooks[hooknum];
1322
1323 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
1324 if (basechain == NULL)
1325 return -ENOMEM;
1326
1327 if (nla[NFTA_CHAIN_COUNTERS]) {
1328 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1329 if (IS_ERR(stats)) {
1330 module_put(type->owner);
1331 kfree(basechain);
1332 return PTR_ERR(stats);
1333 }
1334 basechain->stats = stats;
1335 } else {
1336 stats = netdev_alloc_pcpu_stats(struct nft_stats);
1337 if (stats == NULL) {
1338 module_put(type->owner);
1339 kfree(basechain);
1340 return -ENOMEM;
1341 }
1342 rcu_assign_pointer(basechain->stats, stats);
1343 }
1344
1345 basechain->type = type;
1346 chain = &basechain->chain;
1347
1348 for (i = 0; i < afi->nops; i++) {
1349 ops = &basechain->ops[i];
1350 ops->pf = family;
1351 ops->owner = afi->owner;
1352 ops->hooknum = hooknum;
1353 ops->priority = priority;
1354 ops->priv = chain;
1355 ops->hook = afi->hooks[ops->hooknum];
1356 if (hookfn)
1357 ops->hook = hookfn;
1358 if (afi->hook_ops_init)
1359 afi->hook_ops_init(ops, i);
1360 }
1361
1362 chain->flags |= NFT_BASE_CHAIN;
1363 basechain->policy = policy;
1364 } else {
1365 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1366 if (chain == NULL)
1367 return -ENOMEM;
1368 }
1369
1370 INIT_LIST_HEAD(&chain->rules);
1371 chain->handle = nf_tables_alloc_handle(table);
1372 chain->net = net;
1373 chain->table = table;
1374 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1375
1376 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1377 chain->flags & NFT_BASE_CHAIN) {
1378 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
1379 if (err < 0)
1380 goto err1;
1381 }
1382
1383 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1384 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1385 if (err < 0)
1386 goto err2;
1387
1388 table->use++;
1389 list_add_tail_rcu(&chain->list, &table->chains);
1390 return 0;
1391 err2:
1392 nf_tables_unregister_hooks(table, chain, afi->nops);
1393 err1:
1394 nf_tables_chain_destroy(chain);
1395 return err;
1396 }
1397
1398 static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1399 const struct nlmsghdr *nlh,
1400 const struct nlattr * const nla[])
1401 {
1402 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1403 struct nft_af_info *afi;
1404 struct nft_table *table;
1405 struct nft_chain *chain;
1406 struct net *net = sock_net(skb->sk);
1407 int family = nfmsg->nfgen_family;
1408 struct nft_ctx ctx;
1409
1410 afi = nf_tables_afinfo_lookup(net, family, false);
1411 if (IS_ERR(afi))
1412 return PTR_ERR(afi);
1413
1414 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1415 if (IS_ERR(table))
1416 return PTR_ERR(table);
1417 if (table->flags & NFT_TABLE_INACTIVE)
1418 return -ENOENT;
1419
1420 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1421 if (IS_ERR(chain))
1422 return PTR_ERR(chain);
1423 if (chain->flags & NFT_CHAIN_INACTIVE)
1424 return -ENOENT;
1425 if (chain->use > 0)
1426 return -EBUSY;
1427
1428 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1429
1430 return nft_delchain(&ctx);
1431 }
1432
1433 /*
1434 * Expressions
1435 */
1436
1437 /**
1438 * nft_register_expr - register nf_tables expr type
1439 * @ops: expr type
1440 *
1441 * Registers the expr type for use with nf_tables. Returns zero on
1442 * success or a negative errno code otherwise.
1443 */
1444 int nft_register_expr(struct nft_expr_type *type)
1445 {
1446 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1447 if (type->family == NFPROTO_UNSPEC)
1448 list_add_tail_rcu(&type->list, &nf_tables_expressions);
1449 else
1450 list_add_rcu(&type->list, &nf_tables_expressions);
1451 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1452 return 0;
1453 }
1454 EXPORT_SYMBOL_GPL(nft_register_expr);
1455
1456 /**
1457 * nft_unregister_expr - unregister nf_tables expr type
1458 * @ops: expr type
1459 *
1460 * Unregisters the expr typefor use with nf_tables.
1461 */
1462 void nft_unregister_expr(struct nft_expr_type *type)
1463 {
1464 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1465 list_del_rcu(&type->list);
1466 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1467 }
1468 EXPORT_SYMBOL_GPL(nft_unregister_expr);
1469
1470 static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1471 struct nlattr *nla)
1472 {
1473 const struct nft_expr_type *type;
1474
1475 list_for_each_entry(type, &nf_tables_expressions, list) {
1476 if (!nla_strcmp(nla, type->name) &&
1477 (!type->family || type->family == family))
1478 return type;
1479 }
1480 return NULL;
1481 }
1482
1483 static const struct nft_expr_type *nft_expr_type_get(u8 family,
1484 struct nlattr *nla)
1485 {
1486 const struct nft_expr_type *type;
1487
1488 if (nla == NULL)
1489 return ERR_PTR(-EINVAL);
1490
1491 type = __nft_expr_type_get(family, nla);
1492 if (type != NULL && try_module_get(type->owner))
1493 return type;
1494
1495 #ifdef CONFIG_MODULES
1496 if (type == NULL) {
1497 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1498 request_module("nft-expr-%u-%.*s", family,
1499 nla_len(nla), (char *)nla_data(nla));
1500 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1501 if (__nft_expr_type_get(family, nla))
1502 return ERR_PTR(-EAGAIN);
1503
1504 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1505 request_module("nft-expr-%.*s",
1506 nla_len(nla), (char *)nla_data(nla));
1507 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1508 if (__nft_expr_type_get(family, nla))
1509 return ERR_PTR(-EAGAIN);
1510 }
1511 #endif
1512 return ERR_PTR(-ENOENT);
1513 }
1514
1515 static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1516 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1517 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1518 };
1519
1520 static int nf_tables_fill_expr_info(struct sk_buff *skb,
1521 const struct nft_expr *expr)
1522 {
1523 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1524 goto nla_put_failure;
1525
1526 if (expr->ops->dump) {
1527 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1528 if (data == NULL)
1529 goto nla_put_failure;
1530 if (expr->ops->dump(skb, expr) < 0)
1531 goto nla_put_failure;
1532 nla_nest_end(skb, data);
1533 }
1534
1535 return skb->len;
1536
1537 nla_put_failure:
1538 return -1;
1539 };
1540
1541 struct nft_expr_info {
1542 const struct nft_expr_ops *ops;
1543 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
1544 };
1545
1546 static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1547 const struct nlattr *nla,
1548 struct nft_expr_info *info)
1549 {
1550 const struct nft_expr_type *type;
1551 const struct nft_expr_ops *ops;
1552 struct nlattr *tb[NFTA_EXPR_MAX + 1];
1553 int err;
1554
1555 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1556 if (err < 0)
1557 return err;
1558
1559 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
1560 if (IS_ERR(type))
1561 return PTR_ERR(type);
1562
1563 if (tb[NFTA_EXPR_DATA]) {
1564 err = nla_parse_nested(info->tb, type->maxattr,
1565 tb[NFTA_EXPR_DATA], type->policy);
1566 if (err < 0)
1567 goto err1;
1568 } else
1569 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1570
1571 if (type->select_ops != NULL) {
1572 ops = type->select_ops(ctx,
1573 (const struct nlattr * const *)info->tb);
1574 if (IS_ERR(ops)) {
1575 err = PTR_ERR(ops);
1576 goto err1;
1577 }
1578 } else
1579 ops = type->ops;
1580
1581 info->ops = ops;
1582 return 0;
1583
1584 err1:
1585 module_put(type->owner);
1586 return err;
1587 }
1588
1589 static int nf_tables_newexpr(const struct nft_ctx *ctx,
1590 const struct nft_expr_info *info,
1591 struct nft_expr *expr)
1592 {
1593 const struct nft_expr_ops *ops = info->ops;
1594 int err;
1595
1596 expr->ops = ops;
1597 if (ops->init) {
1598 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1599 if (err < 0)
1600 goto err1;
1601 }
1602
1603 return 0;
1604
1605 err1:
1606 expr->ops = NULL;
1607 return err;
1608 }
1609
1610 static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1611 struct nft_expr *expr)
1612 {
1613 if (expr->ops->destroy)
1614 expr->ops->destroy(ctx, expr);
1615 module_put(expr->ops->type->owner);
1616 }
1617
1618 /*
1619 * Rules
1620 */
1621
1622 static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1623 u64 handle)
1624 {
1625 struct nft_rule *rule;
1626
1627 // FIXME: this sucks
1628 list_for_each_entry(rule, &chain->rules, list) {
1629 if (handle == rule->handle)
1630 return rule;
1631 }
1632
1633 return ERR_PTR(-ENOENT);
1634 }
1635
1636 static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1637 const struct nlattr *nla)
1638 {
1639 if (nla == NULL)
1640 return ERR_PTR(-EINVAL);
1641
1642 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1643 }
1644
1645 static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1646 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1647 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1648 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1649 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1650 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1651 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
1652 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
1653 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1654 .len = NFT_USERDATA_MAXLEN },
1655 };
1656
1657 static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
1658 u32 portid, u32 seq, int event,
1659 u32 flags, int family,
1660 const struct nft_table *table,
1661 const struct nft_chain *chain,
1662 const struct nft_rule *rule)
1663 {
1664 struct nlmsghdr *nlh;
1665 struct nfgenmsg *nfmsg;
1666 const struct nft_expr *expr, *next;
1667 struct nlattr *list;
1668 const struct nft_rule *prule;
1669 int type = event | NFNL_SUBSYS_NFTABLES << 8;
1670
1671 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1672 flags);
1673 if (nlh == NULL)
1674 goto nla_put_failure;
1675
1676 nfmsg = nlmsg_data(nlh);
1677 nfmsg->nfgen_family = family;
1678 nfmsg->version = NFNETLINK_V0;
1679 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
1680
1681 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1682 goto nla_put_failure;
1683 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1684 goto nla_put_failure;
1685 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1686 goto nla_put_failure;
1687
1688 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1689 prule = list_entry(rule->list.prev, struct nft_rule, list);
1690 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1691 cpu_to_be64(prule->handle)))
1692 goto nla_put_failure;
1693 }
1694
1695 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1696 if (list == NULL)
1697 goto nla_put_failure;
1698 nft_rule_for_each_expr(expr, next, rule) {
1699 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1700 if (elem == NULL)
1701 goto nla_put_failure;
1702 if (nf_tables_fill_expr_info(skb, expr) < 0)
1703 goto nla_put_failure;
1704 nla_nest_end(skb, elem);
1705 }
1706 nla_nest_end(skb, list);
1707
1708 if (rule->ulen &&
1709 nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1710 goto nla_put_failure;
1711
1712 nlmsg_end(skb, nlh);
1713 return 0;
1714
1715 nla_put_failure:
1716 nlmsg_trim(skb, nlh);
1717 return -1;
1718 }
1719
1720 static int nf_tables_rule_notify(const struct nft_ctx *ctx,
1721 const struct nft_rule *rule,
1722 int event)
1723 {
1724 struct sk_buff *skb;
1725 int err;
1726
1727 if (!ctx->report &&
1728 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1729 return 0;
1730
1731 err = -ENOBUFS;
1732 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1733 if (skb == NULL)
1734 goto err;
1735
1736 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
1737 event, 0, ctx->afi->family, ctx->table,
1738 ctx->chain, rule);
1739 if (err < 0) {
1740 kfree_skb(skb);
1741 goto err;
1742 }
1743
1744 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1745 ctx->report, GFP_KERNEL);
1746 err:
1747 if (err < 0) {
1748 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1749 err);
1750 }
1751 return err;
1752 }
1753
1754 static int nf_tables_dump_rules(struct sk_buff *skb,
1755 struct netlink_callback *cb)
1756 {
1757 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1758 const struct nft_af_info *afi;
1759 const struct nft_table *table;
1760 const struct nft_chain *chain;
1761 const struct nft_rule *rule;
1762 unsigned int idx = 0, s_idx = cb->args[0];
1763 struct net *net = sock_net(skb->sk);
1764 int family = nfmsg->nfgen_family;
1765
1766 rcu_read_lock();
1767 cb->seq = net->nft.base_seq;
1768
1769 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
1770 if (family != NFPROTO_UNSPEC && family != afi->family)
1771 continue;
1772
1773 list_for_each_entry_rcu(table, &afi->tables, list) {
1774 list_for_each_entry_rcu(chain, &table->chains, list) {
1775 list_for_each_entry_rcu(rule, &chain->rules, list) {
1776 if (!nft_rule_is_active(net, rule))
1777 goto cont;
1778 if (idx < s_idx)
1779 goto cont;
1780 if (idx > s_idx)
1781 memset(&cb->args[1], 0,
1782 sizeof(cb->args) - sizeof(cb->args[0]));
1783 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
1784 cb->nlh->nlmsg_seq,
1785 NFT_MSG_NEWRULE,
1786 NLM_F_MULTI | NLM_F_APPEND,
1787 afi->family, table, chain, rule) < 0)
1788 goto done;
1789
1790 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1791 cont:
1792 idx++;
1793 }
1794 }
1795 }
1796 }
1797 done:
1798 rcu_read_unlock();
1799
1800 cb->args[0] = idx;
1801 return skb->len;
1802 }
1803
1804 static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1805 const struct nlmsghdr *nlh,
1806 const struct nlattr * const nla[])
1807 {
1808 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1809 const struct nft_af_info *afi;
1810 const struct nft_table *table;
1811 const struct nft_chain *chain;
1812 const struct nft_rule *rule;
1813 struct sk_buff *skb2;
1814 struct net *net = sock_net(skb->sk);
1815 int family = nfmsg->nfgen_family;
1816 int err;
1817
1818 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1819 struct netlink_dump_control c = {
1820 .dump = nf_tables_dump_rules,
1821 };
1822 return netlink_dump_start(nlsk, skb, nlh, &c);
1823 }
1824
1825 afi = nf_tables_afinfo_lookup(net, family, false);
1826 if (IS_ERR(afi))
1827 return PTR_ERR(afi);
1828
1829 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1830 if (IS_ERR(table))
1831 return PTR_ERR(table);
1832 if (table->flags & NFT_TABLE_INACTIVE)
1833 return -ENOENT;
1834
1835 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1836 if (IS_ERR(chain))
1837 return PTR_ERR(chain);
1838 if (chain->flags & NFT_CHAIN_INACTIVE)
1839 return -ENOENT;
1840
1841 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1842 if (IS_ERR(rule))
1843 return PTR_ERR(rule);
1844
1845 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1846 if (!skb2)
1847 return -ENOMEM;
1848
1849 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
1850 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1851 family, table, chain, rule);
1852 if (err < 0)
1853 goto err;
1854
1855 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1856
1857 err:
1858 kfree_skb(skb2);
1859 return err;
1860 }
1861
1862 static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1863 struct nft_rule *rule)
1864 {
1865 struct nft_expr *expr;
1866
1867 /*
1868 * Careful: some expressions might not be initialized in case this
1869 * is called on error from nf_tables_newrule().
1870 */
1871 expr = nft_expr_first(rule);
1872 while (expr->ops && expr != nft_expr_last(rule)) {
1873 nf_tables_expr_destroy(ctx, expr);
1874 expr = nft_expr_next(expr);
1875 }
1876 kfree(rule);
1877 }
1878
1879 #define NFT_RULE_MAXEXPRS 128
1880
1881 static struct nft_expr_info *info;
1882
1883 static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1884 const struct nlmsghdr *nlh,
1885 const struct nlattr * const nla[])
1886 {
1887 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1888 struct nft_af_info *afi;
1889 struct net *net = sock_net(skb->sk);
1890 struct nft_table *table;
1891 struct nft_chain *chain;
1892 struct nft_rule *rule, *old_rule = NULL;
1893 struct nft_trans *trans = NULL;
1894 struct nft_expr *expr;
1895 struct nft_ctx ctx;
1896 struct nlattr *tmp;
1897 unsigned int size, i, n, ulen = 0;
1898 int err, rem;
1899 bool create;
1900 u64 handle, pos_handle;
1901
1902 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1903
1904 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1905 if (IS_ERR(afi))
1906 return PTR_ERR(afi);
1907
1908 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1909 if (IS_ERR(table))
1910 return PTR_ERR(table);
1911
1912 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1913 if (IS_ERR(chain))
1914 return PTR_ERR(chain);
1915
1916 if (nla[NFTA_RULE_HANDLE]) {
1917 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1918 rule = __nf_tables_rule_lookup(chain, handle);
1919 if (IS_ERR(rule))
1920 return PTR_ERR(rule);
1921
1922 if (nlh->nlmsg_flags & NLM_F_EXCL)
1923 return -EEXIST;
1924 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1925 old_rule = rule;
1926 else
1927 return -EOPNOTSUPP;
1928 } else {
1929 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1930 return -EINVAL;
1931 handle = nf_tables_alloc_handle(table);
1932
1933 if (chain->use == UINT_MAX)
1934 return -EOVERFLOW;
1935 }
1936
1937 if (nla[NFTA_RULE_POSITION]) {
1938 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1939 return -EOPNOTSUPP;
1940
1941 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1942 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1943 if (IS_ERR(old_rule))
1944 return PTR_ERR(old_rule);
1945 }
1946
1947 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1948
1949 n = 0;
1950 size = 0;
1951 if (nla[NFTA_RULE_EXPRESSIONS]) {
1952 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1953 err = -EINVAL;
1954 if (nla_type(tmp) != NFTA_LIST_ELEM)
1955 goto err1;
1956 if (n == NFT_RULE_MAXEXPRS)
1957 goto err1;
1958 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1959 if (err < 0)
1960 goto err1;
1961 size += info[n].ops->size;
1962 n++;
1963 }
1964 }
1965
1966 if (nla[NFTA_RULE_USERDATA])
1967 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1968
1969 err = -ENOMEM;
1970 rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
1971 if (rule == NULL)
1972 goto err1;
1973
1974 nft_rule_activate_next(net, rule);
1975
1976 rule->handle = handle;
1977 rule->dlen = size;
1978 rule->ulen = ulen;
1979
1980 if (ulen)
1981 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
1982
1983 expr = nft_expr_first(rule);
1984 for (i = 0; i < n; i++) {
1985 err = nf_tables_newexpr(&ctx, &info[i], expr);
1986 if (err < 0)
1987 goto err2;
1988 info[i].ops = NULL;
1989 expr = nft_expr_next(expr);
1990 }
1991
1992 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1993 if (nft_rule_is_active_next(net, old_rule)) {
1994 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
1995 old_rule);
1996 if (trans == NULL) {
1997 err = -ENOMEM;
1998 goto err2;
1999 }
2000 nft_rule_deactivate_next(net, old_rule);
2001 chain->use--;
2002 list_add_tail_rcu(&rule->list, &old_rule->list);
2003 } else {
2004 err = -ENOENT;
2005 goto err2;
2006 }
2007 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
2008 if (old_rule)
2009 list_add_rcu(&rule->list, &old_rule->list);
2010 else
2011 list_add_tail_rcu(&rule->list, &chain->rules);
2012 else {
2013 if (old_rule)
2014 list_add_tail_rcu(&rule->list, &old_rule->list);
2015 else
2016 list_add_rcu(&rule->list, &chain->rules);
2017 }
2018
2019 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
2020 err = -ENOMEM;
2021 goto err3;
2022 }
2023 chain->use++;
2024 return 0;
2025
2026 err3:
2027 list_del_rcu(&rule->list);
2028 if (trans) {
2029 list_del_rcu(&nft_trans_rule(trans)->list);
2030 nft_rule_clear(net, nft_trans_rule(trans));
2031 nft_trans_destroy(trans);
2032 chain->use++;
2033 }
2034 err2:
2035 nf_tables_rule_destroy(&ctx, rule);
2036 err1:
2037 for (i = 0; i < n; i++) {
2038 if (info[i].ops != NULL)
2039 module_put(info[i].ops->type->owner);
2040 }
2041 return err;
2042 }
2043
2044 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
2045 const struct nlmsghdr *nlh,
2046 const struct nlattr * const nla[])
2047 {
2048 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2049 struct nft_af_info *afi;
2050 struct net *net = sock_net(skb->sk);
2051 struct nft_table *table;
2052 struct nft_chain *chain = NULL;
2053 struct nft_rule *rule;
2054 int family = nfmsg->nfgen_family, err = 0;
2055 struct nft_ctx ctx;
2056
2057 afi = nf_tables_afinfo_lookup(net, family, false);
2058 if (IS_ERR(afi))
2059 return PTR_ERR(afi);
2060
2061 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
2062 if (IS_ERR(table))
2063 return PTR_ERR(table);
2064 if (table->flags & NFT_TABLE_INACTIVE)
2065 return -ENOENT;
2066
2067 if (nla[NFTA_RULE_CHAIN]) {
2068 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
2069 if (IS_ERR(chain))
2070 return PTR_ERR(chain);
2071 }
2072
2073 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
2074
2075 if (chain) {
2076 if (nla[NFTA_RULE_HANDLE]) {
2077 rule = nf_tables_rule_lookup(chain,
2078 nla[NFTA_RULE_HANDLE]);
2079 if (IS_ERR(rule))
2080 return PTR_ERR(rule);
2081
2082 err = nft_delrule(&ctx, rule);
2083 } else {
2084 err = nft_delrule_by_chain(&ctx);
2085 }
2086 } else {
2087 list_for_each_entry(chain, &table->chains, list) {
2088 ctx.chain = chain;
2089 err = nft_delrule_by_chain(&ctx);
2090 if (err < 0)
2091 break;
2092 }
2093 }
2094
2095 return err;
2096 }
2097
2098 /*
2099 * Sets
2100 */
2101
2102 static LIST_HEAD(nf_tables_set_ops);
2103
2104 int nft_register_set(struct nft_set_ops *ops)
2105 {
2106 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2107 list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
2108 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2109 return 0;
2110 }
2111 EXPORT_SYMBOL_GPL(nft_register_set);
2112
2113 void nft_unregister_set(struct nft_set_ops *ops)
2114 {
2115 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2116 list_del_rcu(&ops->list);
2117 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2118 }
2119 EXPORT_SYMBOL_GPL(nft_unregister_set);
2120
2121 /*
2122 * Select a set implementation based on the data characteristics and the
2123 * given policy. The total memory use might not be known if no size is
2124 * given, in that case the amount of memory per element is used.
2125 */
2126 static const struct nft_set_ops *
2127 nft_select_set_ops(const struct nlattr * const nla[],
2128 const struct nft_set_desc *desc,
2129 enum nft_set_policies policy)
2130 {
2131 const struct nft_set_ops *ops, *bops;
2132 struct nft_set_estimate est, best;
2133 u32 features;
2134
2135 #ifdef CONFIG_MODULES
2136 if (list_empty(&nf_tables_set_ops)) {
2137 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2138 request_module("nft-set");
2139 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2140 if (!list_empty(&nf_tables_set_ops))
2141 return ERR_PTR(-EAGAIN);
2142 }
2143 #endif
2144 features = 0;
2145 if (nla[NFTA_SET_FLAGS] != NULL) {
2146 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2147 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
2148 }
2149
2150 bops = NULL;
2151 best.size = ~0;
2152 best.class = ~0;
2153
2154 list_for_each_entry(ops, &nf_tables_set_ops, list) {
2155 if ((ops->features & features) != features)
2156 continue;
2157 if (!ops->estimate(desc, features, &est))
2158 continue;
2159
2160 switch (policy) {
2161 case NFT_SET_POL_PERFORMANCE:
2162 if (est.class < best.class)
2163 break;
2164 if (est.class == best.class && est.size < best.size)
2165 break;
2166 continue;
2167 case NFT_SET_POL_MEMORY:
2168 if (est.size < best.size)
2169 break;
2170 if (est.size == best.size && est.class < best.class)
2171 break;
2172 continue;
2173 default:
2174 break;
2175 }
2176
2177 if (!try_module_get(ops->owner))
2178 continue;
2179 if (bops != NULL)
2180 module_put(bops->owner);
2181
2182 bops = ops;
2183 best = est;
2184 }
2185
2186 if (bops != NULL)
2187 return bops;
2188
2189 return ERR_PTR(-EOPNOTSUPP);
2190 }
2191
2192 static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2193 [NFTA_SET_TABLE] = { .type = NLA_STRING },
2194 [NFTA_SET_NAME] = { .type = NLA_STRING,
2195 .len = IFNAMSIZ - 1 },
2196 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2197 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2198 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2199 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2200 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
2201 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2202 [NFTA_SET_DESC] = { .type = NLA_NESTED },
2203 [NFTA_SET_ID] = { .type = NLA_U32 },
2204 };
2205
2206 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2207 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
2208 };
2209
2210 static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2211 const struct sk_buff *skb,
2212 const struct nlmsghdr *nlh,
2213 const struct nlattr * const nla[])
2214 {
2215 struct net *net = sock_net(skb->sk);
2216 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2217 struct nft_af_info *afi = NULL;
2218 struct nft_table *table = NULL;
2219
2220 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2221 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2222 if (IS_ERR(afi))
2223 return PTR_ERR(afi);
2224 }
2225
2226 if (nla[NFTA_SET_TABLE] != NULL) {
2227 if (afi == NULL)
2228 return -EAFNOSUPPORT;
2229
2230 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2231 if (IS_ERR(table))
2232 return PTR_ERR(table);
2233 if (table->flags & NFT_TABLE_INACTIVE)
2234 return -ENOENT;
2235 }
2236
2237 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2238 return 0;
2239 }
2240
2241 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2242 const struct nlattr *nla)
2243 {
2244 struct nft_set *set;
2245
2246 if (nla == NULL)
2247 return ERR_PTR(-EINVAL);
2248
2249 list_for_each_entry(set, &table->sets, list) {
2250 if (!nla_strcmp(nla, set->name))
2251 return set;
2252 }
2253 return ERR_PTR(-ENOENT);
2254 }
2255
2256 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2257 const struct nlattr *nla)
2258 {
2259 struct nft_trans *trans;
2260 u32 id = ntohl(nla_get_be32(nla));
2261
2262 list_for_each_entry(trans, &net->nft.commit_list, list) {
2263 if (trans->msg_type == NFT_MSG_NEWSET &&
2264 id == nft_trans_set_id(trans))
2265 return nft_trans_set(trans);
2266 }
2267 return ERR_PTR(-ENOENT);
2268 }
2269
2270 static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2271 const char *name)
2272 {
2273 const struct nft_set *i;
2274 const char *p;
2275 unsigned long *inuse;
2276 unsigned int n = 0, min = 0;
2277
2278 p = strnchr(name, IFNAMSIZ, '%');
2279 if (p != NULL) {
2280 if (p[1] != 'd' || strchr(p + 2, '%'))
2281 return -EINVAL;
2282
2283 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2284 if (inuse == NULL)
2285 return -ENOMEM;
2286 cont:
2287 list_for_each_entry(i, &ctx->table->sets, list) {
2288 int tmp;
2289
2290 if (!sscanf(i->name, name, &tmp))
2291 continue;
2292 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
2293 continue;
2294
2295 set_bit(tmp - min, inuse);
2296 }
2297
2298 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
2299 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2300 min += BITS_PER_BYTE * PAGE_SIZE;
2301 memset(inuse, 0, PAGE_SIZE);
2302 goto cont;
2303 }
2304 free_page((unsigned long)inuse);
2305 }
2306
2307 snprintf(set->name, sizeof(set->name), name, min + n);
2308 list_for_each_entry(i, &ctx->table->sets, list) {
2309 if (!strcmp(set->name, i->name))
2310 return -ENFILE;
2311 }
2312 return 0;
2313 }
2314
2315 static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2316 const struct nft_set *set, u16 event, u16 flags)
2317 {
2318 struct nfgenmsg *nfmsg;
2319 struct nlmsghdr *nlh;
2320 struct nlattr *desc;
2321 u32 portid = ctx->portid;
2322 u32 seq = ctx->seq;
2323
2324 event |= NFNL_SUBSYS_NFTABLES << 8;
2325 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2326 flags);
2327 if (nlh == NULL)
2328 goto nla_put_failure;
2329
2330 nfmsg = nlmsg_data(nlh);
2331 nfmsg->nfgen_family = ctx->afi->family;
2332 nfmsg->version = NFNETLINK_V0;
2333 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
2334
2335 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2336 goto nla_put_failure;
2337 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2338 goto nla_put_failure;
2339 if (set->flags != 0)
2340 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2341 goto nla_put_failure;
2342
2343 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2344 goto nla_put_failure;
2345 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2346 goto nla_put_failure;
2347 if (set->flags & NFT_SET_MAP) {
2348 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2349 goto nla_put_failure;
2350 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2351 goto nla_put_failure;
2352 }
2353
2354 if (set->policy != NFT_SET_POL_PERFORMANCE) {
2355 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
2356 goto nla_put_failure;
2357 }
2358
2359 desc = nla_nest_start(skb, NFTA_SET_DESC);
2360 if (desc == NULL)
2361 goto nla_put_failure;
2362 if (set->size &&
2363 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2364 goto nla_put_failure;
2365 nla_nest_end(skb, desc);
2366
2367 nlmsg_end(skb, nlh);
2368 return 0;
2369
2370 nla_put_failure:
2371 nlmsg_trim(skb, nlh);
2372 return -1;
2373 }
2374
2375 static int nf_tables_set_notify(const struct nft_ctx *ctx,
2376 const struct nft_set *set,
2377 int event, gfp_t gfp_flags)
2378 {
2379 struct sk_buff *skb;
2380 u32 portid = ctx->portid;
2381 int err;
2382
2383 if (!ctx->report &&
2384 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2385 return 0;
2386
2387 err = -ENOBUFS;
2388 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
2389 if (skb == NULL)
2390 goto err;
2391
2392 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2393 if (err < 0) {
2394 kfree_skb(skb);
2395 goto err;
2396 }
2397
2398 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
2399 ctx->report, gfp_flags);
2400 err:
2401 if (err < 0)
2402 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2403 return err;
2404 }
2405
2406 static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2407 {
2408 const struct nft_set *set;
2409 unsigned int idx, s_idx = cb->args[0];
2410 struct nft_af_info *afi;
2411 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2412 struct net *net = sock_net(skb->sk);
2413 int cur_family = cb->args[3];
2414 struct nft_ctx *ctx = cb->data, ctx_set;
2415
2416 if (cb->args[1])
2417 return skb->len;
2418
2419 rcu_read_lock();
2420 cb->seq = net->nft.base_seq;
2421
2422 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
2423 if (ctx->afi && ctx->afi != afi)
2424 continue;
2425
2426 if (cur_family) {
2427 if (afi->family != cur_family)
2428 continue;
2429
2430 cur_family = 0;
2431 }
2432 list_for_each_entry_rcu(table, &afi->tables, list) {
2433 if (ctx->table && ctx->table != table)
2434 continue;
2435
2436 if (cur_table) {
2437 if (cur_table != table)
2438 continue;
2439
2440 cur_table = NULL;
2441 }
2442 idx = 0;
2443 list_for_each_entry_rcu(set, &table->sets, list) {
2444 if (idx < s_idx)
2445 goto cont;
2446
2447 ctx_set = *ctx;
2448 ctx_set.table = table;
2449 ctx_set.afi = afi;
2450 if (nf_tables_fill_set(skb, &ctx_set, set,
2451 NFT_MSG_NEWSET,
2452 NLM_F_MULTI) < 0) {
2453 cb->args[0] = idx;
2454 cb->args[2] = (unsigned long) table;
2455 cb->args[3] = afi->family;
2456 goto done;
2457 }
2458 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2459 cont:
2460 idx++;
2461 }
2462 if (s_idx)
2463 s_idx = 0;
2464 }
2465 }
2466 cb->args[1] = 1;
2467 done:
2468 rcu_read_unlock();
2469 return skb->len;
2470 }
2471
2472 static int nf_tables_dump_sets_done(struct netlink_callback *cb)
2473 {
2474 kfree(cb->data);
2475 return 0;
2476 }
2477
2478 static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2479 const struct nlmsghdr *nlh,
2480 const struct nlattr * const nla[])
2481 {
2482 const struct nft_set *set;
2483 struct nft_ctx ctx;
2484 struct sk_buff *skb2;
2485 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2486 int err;
2487
2488 /* Verify existence before starting dump */
2489 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2490 if (err < 0)
2491 return err;
2492
2493 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2494 struct netlink_dump_control c = {
2495 .dump = nf_tables_dump_sets,
2496 .done = nf_tables_dump_sets_done,
2497 };
2498 struct nft_ctx *ctx_dump;
2499
2500 ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL);
2501 if (ctx_dump == NULL)
2502 return -ENOMEM;
2503
2504 *ctx_dump = ctx;
2505 c.data = ctx_dump;
2506
2507 return netlink_dump_start(nlsk, skb, nlh, &c);
2508 }
2509
2510 /* Only accept unspec with dump */
2511 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2512 return -EAFNOSUPPORT;
2513
2514 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2515 if (IS_ERR(set))
2516 return PTR_ERR(set);
2517 if (set->flags & NFT_SET_INACTIVE)
2518 return -ENOENT;
2519
2520 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2521 if (skb2 == NULL)
2522 return -ENOMEM;
2523
2524 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2525 if (err < 0)
2526 goto err;
2527
2528 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2529
2530 err:
2531 kfree_skb(skb2);
2532 return err;
2533 }
2534
2535 static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2536 struct nft_set_desc *desc,
2537 const struct nlattr *nla)
2538 {
2539 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2540 int err;
2541
2542 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2543 if (err < 0)
2544 return err;
2545
2546 if (da[NFTA_SET_DESC_SIZE] != NULL)
2547 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2548
2549 return 0;
2550 }
2551
2552 static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2553 const struct nlmsghdr *nlh,
2554 const struct nlattr * const nla[])
2555 {
2556 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2557 const struct nft_set_ops *ops;
2558 struct nft_af_info *afi;
2559 struct net *net = sock_net(skb->sk);
2560 struct nft_table *table;
2561 struct nft_set *set;
2562 struct nft_ctx ctx;
2563 char name[IFNAMSIZ];
2564 unsigned int size;
2565 bool create;
2566 u32 ktype, dtype, flags, policy;
2567 struct nft_set_desc desc;
2568 int err;
2569
2570 if (nla[NFTA_SET_TABLE] == NULL ||
2571 nla[NFTA_SET_NAME] == NULL ||
2572 nla[NFTA_SET_KEY_LEN] == NULL ||
2573 nla[NFTA_SET_ID] == NULL)
2574 return -EINVAL;
2575
2576 memset(&desc, 0, sizeof(desc));
2577
2578 ktype = NFT_DATA_VALUE;
2579 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2580 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2581 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2582 return -EINVAL;
2583 }
2584
2585 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2586 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
2587 return -EINVAL;
2588
2589 flags = 0;
2590 if (nla[NFTA_SET_FLAGS] != NULL) {
2591 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2592 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2593 NFT_SET_INTERVAL | NFT_SET_MAP))
2594 return -EINVAL;
2595 }
2596
2597 dtype = 0;
2598 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2599 if (!(flags & NFT_SET_MAP))
2600 return -EINVAL;
2601
2602 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2603 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2604 dtype != NFT_DATA_VERDICT)
2605 return -EINVAL;
2606
2607 if (dtype != NFT_DATA_VERDICT) {
2608 if (nla[NFTA_SET_DATA_LEN] == NULL)
2609 return -EINVAL;
2610 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2611 if (desc.dlen == 0 ||
2612 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
2613 return -EINVAL;
2614 } else
2615 desc.dlen = sizeof(struct nft_data);
2616 } else if (flags & NFT_SET_MAP)
2617 return -EINVAL;
2618
2619 policy = NFT_SET_POL_PERFORMANCE;
2620 if (nla[NFTA_SET_POLICY] != NULL)
2621 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2622
2623 if (nla[NFTA_SET_DESC] != NULL) {
2624 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2625 if (err < 0)
2626 return err;
2627 }
2628
2629 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2630
2631 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2632 if (IS_ERR(afi))
2633 return PTR_ERR(afi);
2634
2635 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2636 if (IS_ERR(table))
2637 return PTR_ERR(table);
2638
2639 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2640
2641 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2642 if (IS_ERR(set)) {
2643 if (PTR_ERR(set) != -ENOENT)
2644 return PTR_ERR(set);
2645 set = NULL;
2646 }
2647
2648 if (set != NULL) {
2649 if (nlh->nlmsg_flags & NLM_F_EXCL)
2650 return -EEXIST;
2651 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2652 return -EOPNOTSUPP;
2653 return 0;
2654 }
2655
2656 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2657 return -ENOENT;
2658
2659 ops = nft_select_set_ops(nla, &desc, policy);
2660 if (IS_ERR(ops))
2661 return PTR_ERR(ops);
2662
2663 size = 0;
2664 if (ops->privsize != NULL)
2665 size = ops->privsize(nla);
2666
2667 err = -ENOMEM;
2668 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2669 if (set == NULL)
2670 goto err1;
2671
2672 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2673 err = nf_tables_set_alloc_name(&ctx, set, name);
2674 if (err < 0)
2675 goto err2;
2676
2677 INIT_LIST_HEAD(&set->bindings);
2678 set->ops = ops;
2679 set->ktype = ktype;
2680 set->klen = desc.klen;
2681 set->dtype = dtype;
2682 set->dlen = desc.dlen;
2683 set->flags = flags;
2684 set->size = desc.size;
2685 set->policy = policy;
2686
2687 err = ops->init(set, &desc, nla);
2688 if (err < 0)
2689 goto err2;
2690
2691 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
2692 if (err < 0)
2693 goto err2;
2694
2695 list_add_tail_rcu(&set->list, &table->sets);
2696 table->use++;
2697 return 0;
2698
2699 err2:
2700 kfree(set);
2701 err1:
2702 module_put(ops->owner);
2703 return err;
2704 }
2705
2706 static void nft_set_destroy(struct nft_set *set)
2707 {
2708 set->ops->destroy(set);
2709 module_put(set->ops->owner);
2710 kfree(set);
2711 }
2712
2713 static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2714 {
2715 list_del_rcu(&set->list);
2716 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
2717 nft_set_destroy(set);
2718 }
2719
2720 static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2721 const struct nlmsghdr *nlh,
2722 const struct nlattr * const nla[])
2723 {
2724 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2725 struct nft_set *set;
2726 struct nft_ctx ctx;
2727 int err;
2728
2729 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2730 return -EAFNOSUPPORT;
2731 if (nla[NFTA_SET_TABLE] == NULL)
2732 return -EINVAL;
2733
2734 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2735 if (err < 0)
2736 return err;
2737
2738 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2739 if (IS_ERR(set))
2740 return PTR_ERR(set);
2741 if (set->flags & NFT_SET_INACTIVE)
2742 return -ENOENT;
2743 if (!list_empty(&set->bindings))
2744 return -EBUSY;
2745
2746 return nft_delset(&ctx, set);
2747 }
2748
2749 static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2750 const struct nft_set *set,
2751 const struct nft_set_iter *iter,
2752 const struct nft_set_elem *elem)
2753 {
2754 enum nft_registers dreg;
2755
2756 dreg = nft_type_to_reg(set->dtype);
2757 return nft_validate_data_load(ctx, dreg, &elem->data,
2758 set->dtype == NFT_DATA_VERDICT ?
2759 NFT_DATA_VERDICT : NFT_DATA_VALUE);
2760 }
2761
2762 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2763 struct nft_set_binding *binding)
2764 {
2765 struct nft_set_binding *i;
2766 struct nft_set_iter iter;
2767
2768 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2769 return -EBUSY;
2770
2771 if (set->flags & NFT_SET_MAP) {
2772 /* If the set is already bound to the same chain all
2773 * jumps are already validated for that chain.
2774 */
2775 list_for_each_entry(i, &set->bindings, list) {
2776 if (i->chain == binding->chain)
2777 goto bind;
2778 }
2779
2780 iter.skip = 0;
2781 iter.count = 0;
2782 iter.err = 0;
2783 iter.fn = nf_tables_bind_check_setelem;
2784
2785 set->ops->walk(ctx, set, &iter);
2786 if (iter.err < 0) {
2787 /* Destroy anonymous sets if binding fails */
2788 if (set->flags & NFT_SET_ANONYMOUS)
2789 nf_tables_set_destroy(ctx, set);
2790
2791 return iter.err;
2792 }
2793 }
2794 bind:
2795 binding->chain = ctx->chain;
2796 list_add_tail_rcu(&binding->list, &set->bindings);
2797 return 0;
2798 }
2799
2800 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2801 struct nft_set_binding *binding)
2802 {
2803 list_del_rcu(&binding->list);
2804
2805 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2806 !(set->flags & NFT_SET_INACTIVE))
2807 nf_tables_set_destroy(ctx, set);
2808 }
2809
2810 /*
2811 * Set elements
2812 */
2813
2814 static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2815 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2816 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2817 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2818 };
2819
2820 static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2821 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2822 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2823 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2824 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
2825 };
2826
2827 static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2828 const struct sk_buff *skb,
2829 const struct nlmsghdr *nlh,
2830 const struct nlattr * const nla[],
2831 bool trans)
2832 {
2833 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2834 struct nft_af_info *afi;
2835 struct nft_table *table;
2836 struct net *net = sock_net(skb->sk);
2837
2838 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2839 if (IS_ERR(afi))
2840 return PTR_ERR(afi);
2841
2842 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2843 if (IS_ERR(table))
2844 return PTR_ERR(table);
2845 if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2846 return -ENOENT;
2847
2848 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2849 return 0;
2850 }
2851
2852 static int nf_tables_fill_setelem(struct sk_buff *skb,
2853 const struct nft_set *set,
2854 const struct nft_set_elem *elem)
2855 {
2856 unsigned char *b = skb_tail_pointer(skb);
2857 struct nlattr *nest;
2858
2859 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2860 if (nest == NULL)
2861 goto nla_put_failure;
2862
2863 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2864 set->klen) < 0)
2865 goto nla_put_failure;
2866
2867 if (set->flags & NFT_SET_MAP &&
2868 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2869 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2870 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2871 set->dlen) < 0)
2872 goto nla_put_failure;
2873
2874 if (elem->flags != 0)
2875 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2876 goto nla_put_failure;
2877
2878 nla_nest_end(skb, nest);
2879 return 0;
2880
2881 nla_put_failure:
2882 nlmsg_trim(skb, b);
2883 return -EMSGSIZE;
2884 }
2885
2886 struct nft_set_dump_args {
2887 const struct netlink_callback *cb;
2888 struct nft_set_iter iter;
2889 struct sk_buff *skb;
2890 };
2891
2892 static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2893 const struct nft_set *set,
2894 const struct nft_set_iter *iter,
2895 const struct nft_set_elem *elem)
2896 {
2897 struct nft_set_dump_args *args;
2898
2899 args = container_of(iter, struct nft_set_dump_args, iter);
2900 return nf_tables_fill_setelem(args->skb, set, elem);
2901 }
2902
2903 static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2904 {
2905 const struct nft_set *set;
2906 struct nft_set_dump_args args;
2907 struct nft_ctx ctx;
2908 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2909 struct nfgenmsg *nfmsg;
2910 struct nlmsghdr *nlh;
2911 struct nlattr *nest;
2912 u32 portid, seq;
2913 int event, err;
2914
2915 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2916 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
2917 if (err < 0)
2918 return err;
2919
2920 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
2921 false);
2922 if (err < 0)
2923 return err;
2924
2925 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2926 if (IS_ERR(set))
2927 return PTR_ERR(set);
2928 if (set->flags & NFT_SET_INACTIVE)
2929 return -ENOENT;
2930
2931 event = NFT_MSG_NEWSETELEM;
2932 event |= NFNL_SUBSYS_NFTABLES << 8;
2933 portid = NETLINK_CB(cb->skb).portid;
2934 seq = cb->nlh->nlmsg_seq;
2935
2936 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2937 NLM_F_MULTI);
2938 if (nlh == NULL)
2939 goto nla_put_failure;
2940
2941 nfmsg = nlmsg_data(nlh);
2942 nfmsg->nfgen_family = ctx.afi->family;
2943 nfmsg->version = NFNETLINK_V0;
2944 nfmsg->res_id = htons(ctx.net->nft.base_seq & 0xffff);
2945
2946 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2947 goto nla_put_failure;
2948 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2949 goto nla_put_failure;
2950
2951 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2952 if (nest == NULL)
2953 goto nla_put_failure;
2954
2955 args.cb = cb;
2956 args.skb = skb;
2957 args.iter.skip = cb->args[0];
2958 args.iter.count = 0;
2959 args.iter.err = 0;
2960 args.iter.fn = nf_tables_dump_setelem;
2961 set->ops->walk(&ctx, set, &args.iter);
2962
2963 nla_nest_end(skb, nest);
2964 nlmsg_end(skb, nlh);
2965
2966 if (args.iter.err && args.iter.err != -EMSGSIZE)
2967 return args.iter.err;
2968 if (args.iter.count == cb->args[0])
2969 return 0;
2970
2971 cb->args[0] = args.iter.count;
2972 return skb->len;
2973
2974 nla_put_failure:
2975 return -ENOSPC;
2976 }
2977
2978 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2979 const struct nlmsghdr *nlh,
2980 const struct nlattr * const nla[])
2981 {
2982 const struct nft_set *set;
2983 struct nft_ctx ctx;
2984 int err;
2985
2986 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
2987 if (err < 0)
2988 return err;
2989
2990 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2991 if (IS_ERR(set))
2992 return PTR_ERR(set);
2993 if (set->flags & NFT_SET_INACTIVE)
2994 return -ENOENT;
2995
2996 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2997 struct netlink_dump_control c = {
2998 .dump = nf_tables_dump_set,
2999 };
3000 return netlink_dump_start(nlsk, skb, nlh, &c);
3001 }
3002 return -EOPNOTSUPP;
3003 }
3004
3005 static int nf_tables_fill_setelem_info(struct sk_buff *skb,
3006 const struct nft_ctx *ctx, u32 seq,
3007 u32 portid, int event, u16 flags,
3008 const struct nft_set *set,
3009 const struct nft_set_elem *elem)
3010 {
3011 struct nfgenmsg *nfmsg;
3012 struct nlmsghdr *nlh;
3013 struct nlattr *nest;
3014 int err;
3015
3016 event |= NFNL_SUBSYS_NFTABLES << 8;
3017 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3018 flags);
3019 if (nlh == NULL)
3020 goto nla_put_failure;
3021
3022 nfmsg = nlmsg_data(nlh);
3023 nfmsg->nfgen_family = ctx->afi->family;
3024 nfmsg->version = NFNETLINK_V0;
3025 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
3026
3027 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3028 goto nla_put_failure;
3029 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3030 goto nla_put_failure;
3031
3032 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3033 if (nest == NULL)
3034 goto nla_put_failure;
3035
3036 err = nf_tables_fill_setelem(skb, set, elem);
3037 if (err < 0)
3038 goto nla_put_failure;
3039
3040 nla_nest_end(skb, nest);
3041
3042 nlmsg_end(skb, nlh);
3043 return 0;
3044
3045 nla_put_failure:
3046 nlmsg_trim(skb, nlh);
3047 return -1;
3048 }
3049
3050 static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
3051 const struct nft_set *set,
3052 const struct nft_set_elem *elem,
3053 int event, u16 flags)
3054 {
3055 struct net *net = ctx->net;
3056 u32 portid = ctx->portid;
3057 struct sk_buff *skb;
3058 int err;
3059
3060 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3061 return 0;
3062
3063 err = -ENOBUFS;
3064 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3065 if (skb == NULL)
3066 goto err;
3067
3068 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
3069 set, elem);
3070 if (err < 0) {
3071 kfree_skb(skb);
3072 goto err;
3073 }
3074
3075 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
3076 GFP_KERNEL);
3077 err:
3078 if (err < 0)
3079 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3080 return err;
3081 }
3082
3083 static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3084 int msg_type,
3085 struct nft_set *set)
3086 {
3087 struct nft_trans *trans;
3088
3089 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3090 if (trans == NULL)
3091 return NULL;
3092
3093 nft_trans_elem_set(trans) = set;
3094 return trans;
3095 }
3096
3097 static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
3098 const struct nlattr *attr)
3099 {
3100 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3101 struct nft_data_desc d1, d2;
3102 struct nft_set_elem elem;
3103 struct nft_set_binding *binding;
3104 enum nft_registers dreg;
3105 struct nft_trans *trans;
3106 int err;
3107
3108 if (set->size && set->nelems == set->size)
3109 return -ENFILE;
3110
3111 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3112 nft_set_elem_policy);
3113 if (err < 0)
3114 return err;
3115
3116 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3117 return -EINVAL;
3118
3119 elem.flags = 0;
3120 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
3121 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3122 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
3123 return -EINVAL;
3124 }
3125
3126 if (set->flags & NFT_SET_MAP) {
3127 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3128 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
3129 return -EINVAL;
3130 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3131 elem.flags & NFT_SET_ELEM_INTERVAL_END)
3132 return -EINVAL;
3133 } else {
3134 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3135 return -EINVAL;
3136 }
3137
3138 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3139 if (err < 0)
3140 goto err1;
3141 err = -EINVAL;
3142 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3143 goto err2;
3144
3145 err = -EEXIST;
3146 if (set->ops->get(set, &elem) == 0)
3147 goto err2;
3148
3149 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3150 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
3151 if (err < 0)
3152 goto err2;
3153
3154 err = -EINVAL;
3155 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3156 goto err3;
3157
3158 dreg = nft_type_to_reg(set->dtype);
3159 list_for_each_entry(binding, &set->bindings, list) {
3160 struct nft_ctx bind_ctx = {
3161 .afi = ctx->afi,
3162 .table = ctx->table,
3163 .chain = (struct nft_chain *)binding->chain,
3164 };
3165
3166 err = nft_validate_data_load(&bind_ctx, dreg,
3167 &elem.data, d2.type);
3168 if (err < 0)
3169 goto err3;
3170 }
3171 }
3172
3173 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3174 if (trans == NULL)
3175 goto err3;
3176
3177 err = set->ops->insert(set, &elem);
3178 if (err < 0)
3179 goto err4;
3180
3181 nft_trans_elem(trans) = elem;
3182 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3183 return 0;
3184
3185 err4:
3186 kfree(trans);
3187 err3:
3188 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3189 nft_data_uninit(&elem.data, d2.type);
3190 err2:
3191 nft_data_uninit(&elem.key, d1.type);
3192 err1:
3193 return err;
3194 }
3195
3196 static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3197 const struct nlmsghdr *nlh,
3198 const struct nlattr * const nla[])
3199 {
3200 struct net *net = sock_net(skb->sk);
3201 const struct nlattr *attr;
3202 struct nft_set *set;
3203 struct nft_ctx ctx;
3204 int rem, err = 0;
3205
3206 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3207 return -EINVAL;
3208
3209 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
3210 if (err < 0)
3211 return err;
3212
3213 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3214 if (IS_ERR(set)) {
3215 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3216 set = nf_tables_set_lookup_byid(net,
3217 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3218 }
3219 if (IS_ERR(set))
3220 return PTR_ERR(set);
3221 }
3222
3223 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3224 return -EBUSY;
3225
3226 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3227 err = nft_add_set_elem(&ctx, set, attr);
3228 if (err < 0)
3229 break;
3230
3231 set->nelems++;
3232 }
3233 return err;
3234 }
3235
3236 static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
3237 const struct nlattr *attr)
3238 {
3239 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3240 struct nft_data_desc desc;
3241 struct nft_set_elem elem;
3242 struct nft_trans *trans;
3243 int err;
3244
3245 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3246 nft_set_elem_policy);
3247 if (err < 0)
3248 goto err1;
3249
3250 err = -EINVAL;
3251 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3252 goto err1;
3253
3254 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3255 if (err < 0)
3256 goto err1;
3257
3258 err = -EINVAL;
3259 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3260 goto err2;
3261
3262 err = set->ops->get(set, &elem);
3263 if (err < 0)
3264 goto err2;
3265
3266 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
3267 if (trans == NULL) {
3268 err = -ENOMEM;
3269 goto err2;
3270 }
3271
3272 nft_trans_elem(trans) = elem;
3273 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3274 return 0;
3275 err2:
3276 nft_data_uninit(&elem.key, desc.type);
3277 err1:
3278 return err;
3279 }
3280
3281 static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3282 const struct nlmsghdr *nlh,
3283 const struct nlattr * const nla[])
3284 {
3285 const struct nlattr *attr;
3286 struct nft_set *set;
3287 struct nft_ctx ctx;
3288 int rem, err = 0;
3289
3290 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3291 return -EINVAL;
3292
3293 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
3294 if (err < 0)
3295 return err;
3296
3297 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3298 if (IS_ERR(set))
3299 return PTR_ERR(set);
3300 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3301 return -EBUSY;
3302
3303 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3304 err = nft_del_setelem(&ctx, set, attr);
3305 if (err < 0)
3306 break;
3307
3308 set->nelems--;
3309 }
3310 return err;
3311 }
3312
3313 static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
3314 u32 portid, u32 seq)
3315 {
3316 struct nlmsghdr *nlh;
3317 struct nfgenmsg *nfmsg;
3318 int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWGEN;
3319
3320 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
3321 if (nlh == NULL)
3322 goto nla_put_failure;
3323
3324 nfmsg = nlmsg_data(nlh);
3325 nfmsg->nfgen_family = AF_UNSPEC;
3326 nfmsg->version = NFNETLINK_V0;
3327 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
3328
3329 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)))
3330 goto nla_put_failure;
3331
3332 nlmsg_end(skb, nlh);
3333 return 0;
3334
3335 nla_put_failure:
3336 nlmsg_trim(skb, nlh);
3337 return -EMSGSIZE;
3338 }
3339
3340 static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event)
3341 {
3342 struct nlmsghdr *nlh = nlmsg_hdr(skb);
3343 struct sk_buff *skb2;
3344 int err;
3345
3346 if (nlmsg_report(nlh) &&
3347 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3348 return 0;
3349
3350 err = -ENOBUFS;
3351 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3352 if (skb2 == NULL)
3353 goto err;
3354
3355 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3356 nlh->nlmsg_seq);
3357 if (err < 0) {
3358 kfree_skb(skb2);
3359 goto err;
3360 }
3361
3362 err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid,
3363 NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL);
3364 err:
3365 if (err < 0) {
3366 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
3367 err);
3368 }
3369 return err;
3370 }
3371
3372 static int nf_tables_getgen(struct sock *nlsk, struct sk_buff *skb,
3373 const struct nlmsghdr *nlh,
3374 const struct nlattr * const nla[])
3375 {
3376 struct net *net = sock_net(skb->sk);
3377 struct sk_buff *skb2;
3378 int err;
3379
3380 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3381 if (skb2 == NULL)
3382 return -ENOMEM;
3383
3384 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3385 nlh->nlmsg_seq);
3386 if (err < 0)
3387 goto err;
3388
3389 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
3390 err:
3391 kfree_skb(skb2);
3392 return err;
3393 }
3394
3395 static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3396 [NFT_MSG_NEWTABLE] = {
3397 .call_batch = nf_tables_newtable,
3398 .attr_count = NFTA_TABLE_MAX,
3399 .policy = nft_table_policy,
3400 },
3401 [NFT_MSG_GETTABLE] = {
3402 .call = nf_tables_gettable,
3403 .attr_count = NFTA_TABLE_MAX,
3404 .policy = nft_table_policy,
3405 },
3406 [NFT_MSG_DELTABLE] = {
3407 .call_batch = nf_tables_deltable,
3408 .attr_count = NFTA_TABLE_MAX,
3409 .policy = nft_table_policy,
3410 },
3411 [NFT_MSG_NEWCHAIN] = {
3412 .call_batch = nf_tables_newchain,
3413 .attr_count = NFTA_CHAIN_MAX,
3414 .policy = nft_chain_policy,
3415 },
3416 [NFT_MSG_GETCHAIN] = {
3417 .call = nf_tables_getchain,
3418 .attr_count = NFTA_CHAIN_MAX,
3419 .policy = nft_chain_policy,
3420 },
3421 [NFT_MSG_DELCHAIN] = {
3422 .call_batch = nf_tables_delchain,
3423 .attr_count = NFTA_CHAIN_MAX,
3424 .policy = nft_chain_policy,
3425 },
3426 [NFT_MSG_NEWRULE] = {
3427 .call_batch = nf_tables_newrule,
3428 .attr_count = NFTA_RULE_MAX,
3429 .policy = nft_rule_policy,
3430 },
3431 [NFT_MSG_GETRULE] = {
3432 .call = nf_tables_getrule,
3433 .attr_count = NFTA_RULE_MAX,
3434 .policy = nft_rule_policy,
3435 },
3436 [NFT_MSG_DELRULE] = {
3437 .call_batch = nf_tables_delrule,
3438 .attr_count = NFTA_RULE_MAX,
3439 .policy = nft_rule_policy,
3440 },
3441 [NFT_MSG_NEWSET] = {
3442 .call_batch = nf_tables_newset,
3443 .attr_count = NFTA_SET_MAX,
3444 .policy = nft_set_policy,
3445 },
3446 [NFT_MSG_GETSET] = {
3447 .call = nf_tables_getset,
3448 .attr_count = NFTA_SET_MAX,
3449 .policy = nft_set_policy,
3450 },
3451 [NFT_MSG_DELSET] = {
3452 .call_batch = nf_tables_delset,
3453 .attr_count = NFTA_SET_MAX,
3454 .policy = nft_set_policy,
3455 },
3456 [NFT_MSG_NEWSETELEM] = {
3457 .call_batch = nf_tables_newsetelem,
3458 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3459 .policy = nft_set_elem_list_policy,
3460 },
3461 [NFT_MSG_GETSETELEM] = {
3462 .call = nf_tables_getsetelem,
3463 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3464 .policy = nft_set_elem_list_policy,
3465 },
3466 [NFT_MSG_DELSETELEM] = {
3467 .call_batch = nf_tables_delsetelem,
3468 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3469 .policy = nft_set_elem_list_policy,
3470 },
3471 [NFT_MSG_GETGEN] = {
3472 .call = nf_tables_getgen,
3473 },
3474 };
3475
3476 static void nft_chain_commit_update(struct nft_trans *trans)
3477 {
3478 struct nft_base_chain *basechain;
3479
3480 if (nft_trans_chain_name(trans)[0])
3481 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3482
3483 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3484 return;
3485
3486 basechain = nft_base_chain(trans->ctx.chain);
3487 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3488
3489 switch (nft_trans_chain_policy(trans)) {
3490 case NF_DROP:
3491 case NF_ACCEPT:
3492 basechain->policy = nft_trans_chain_policy(trans);
3493 break;
3494 }
3495 }
3496
3497 static void nf_tables_commit_release(struct nft_trans *trans)
3498 {
3499 switch (trans->msg_type) {
3500 case NFT_MSG_DELTABLE:
3501 nf_tables_table_destroy(&trans->ctx);
3502 break;
3503 case NFT_MSG_DELCHAIN:
3504 nf_tables_chain_destroy(trans->ctx.chain);
3505 break;
3506 case NFT_MSG_DELRULE:
3507 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3508 break;
3509 case NFT_MSG_DELSET:
3510 nft_set_destroy(nft_trans_set(trans));
3511 break;
3512 }
3513 kfree(trans);
3514 }
3515
3516 static int nf_tables_commit(struct sk_buff *skb)
3517 {
3518 struct net *net = sock_net(skb->sk);
3519 struct nft_trans *trans, *next;
3520 struct nft_trans_elem *te;
3521
3522 /* Bump generation counter, invalidate any dump in progress */
3523 while (++net->nft.base_seq == 0);
3524
3525 /* A new generation has just started */
3526 net->nft.gencursor = gencursor_next(net);
3527
3528 /* Make sure all packets have left the previous generation before
3529 * purging old rules.
3530 */
3531 synchronize_rcu();
3532
3533 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3534 switch (trans->msg_type) {
3535 case NFT_MSG_NEWTABLE:
3536 if (nft_trans_table_update(trans)) {
3537 if (!nft_trans_table_enable(trans)) {
3538 nf_tables_table_disable(trans->ctx.afi,
3539 trans->ctx.table);
3540 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3541 }
3542 } else {
3543 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3544 }
3545 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
3546 nft_trans_destroy(trans);
3547 break;
3548 case NFT_MSG_DELTABLE:
3549 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
3550 break;
3551 case NFT_MSG_NEWCHAIN:
3552 if (nft_trans_chain_update(trans))
3553 nft_chain_commit_update(trans);
3554 else
3555 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
3556
3557 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
3558 nft_trans_destroy(trans);
3559 break;
3560 case NFT_MSG_DELCHAIN:
3561 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
3562 nf_tables_unregister_hooks(trans->ctx.table,
3563 trans->ctx.chain,
3564 trans->ctx.afi->nops);
3565 break;
3566 case NFT_MSG_NEWRULE:
3567 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3568 nf_tables_rule_notify(&trans->ctx,
3569 nft_trans_rule(trans),
3570 NFT_MSG_NEWRULE);
3571 nft_trans_destroy(trans);
3572 break;
3573 case NFT_MSG_DELRULE:
3574 list_del_rcu(&nft_trans_rule(trans)->list);
3575 nf_tables_rule_notify(&trans->ctx,
3576 nft_trans_rule(trans),
3577 NFT_MSG_DELRULE);
3578 break;
3579 case NFT_MSG_NEWSET:
3580 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
3581 /* This avoids hitting -EBUSY when deleting the table
3582 * from the transaction.
3583 */
3584 if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
3585 !list_empty(&nft_trans_set(trans)->bindings))
3586 trans->ctx.table->use--;
3587
3588 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3589 NFT_MSG_NEWSET, GFP_KERNEL);
3590 nft_trans_destroy(trans);
3591 break;
3592 case NFT_MSG_DELSET:
3593 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3594 NFT_MSG_DELSET, GFP_KERNEL);
3595 break;
3596 case NFT_MSG_NEWSETELEM:
3597 nf_tables_setelem_notify(&trans->ctx,
3598 nft_trans_elem_set(trans),
3599 &nft_trans_elem(trans),
3600 NFT_MSG_NEWSETELEM, 0);
3601 nft_trans_destroy(trans);
3602 break;
3603 case NFT_MSG_DELSETELEM:
3604 te = (struct nft_trans_elem *)trans->data;
3605 nf_tables_setelem_notify(&trans->ctx, te->set,
3606 &te->elem,
3607 NFT_MSG_DELSETELEM, 0);
3608 te->set->ops->get(te->set, &te->elem);
3609 te->set->ops->remove(te->set, &te->elem);
3610 nft_data_uninit(&te->elem.key, NFT_DATA_VALUE);
3611 if (te->elem.flags & NFT_SET_MAP) {
3612 nft_data_uninit(&te->elem.data,
3613 te->set->dtype);
3614 }
3615 nft_trans_destroy(trans);
3616 break;
3617 }
3618 }
3619
3620 synchronize_rcu();
3621
3622 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3623 list_del(&trans->list);
3624 nf_tables_commit_release(trans);
3625 }
3626
3627 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
3628
3629 return 0;
3630 }
3631
3632 static void nf_tables_abort_release(struct nft_trans *trans)
3633 {
3634 switch (trans->msg_type) {
3635 case NFT_MSG_NEWTABLE:
3636 nf_tables_table_destroy(&trans->ctx);
3637 break;
3638 case NFT_MSG_NEWCHAIN:
3639 nf_tables_chain_destroy(trans->ctx.chain);
3640 break;
3641 case NFT_MSG_NEWRULE:
3642 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3643 break;
3644 case NFT_MSG_NEWSET:
3645 nft_set_destroy(nft_trans_set(trans));
3646 break;
3647 }
3648 kfree(trans);
3649 }
3650
3651 static int nf_tables_abort(struct sk_buff *skb)
3652 {
3653 struct net *net = sock_net(skb->sk);
3654 struct nft_trans *trans, *next;
3655 struct nft_set *set;
3656
3657 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
3658 switch (trans->msg_type) {
3659 case NFT_MSG_NEWTABLE:
3660 if (nft_trans_table_update(trans)) {
3661 if (nft_trans_table_enable(trans)) {
3662 nf_tables_table_disable(trans->ctx.afi,
3663 trans->ctx.table);
3664 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3665 }
3666 nft_trans_destroy(trans);
3667 } else {
3668 list_del_rcu(&trans->ctx.table->list);
3669 }
3670 break;
3671 case NFT_MSG_DELTABLE:
3672 list_add_tail_rcu(&trans->ctx.table->list,
3673 &trans->ctx.afi->tables);
3674 nft_trans_destroy(trans);
3675 break;
3676 case NFT_MSG_NEWCHAIN:
3677 if (nft_trans_chain_update(trans)) {
3678 free_percpu(nft_trans_chain_stats(trans));
3679
3680 nft_trans_destroy(trans);
3681 } else {
3682 trans->ctx.table->use--;
3683 list_del_rcu(&trans->ctx.chain->list);
3684 nf_tables_unregister_hooks(trans->ctx.table,
3685 trans->ctx.chain,
3686 trans->ctx.afi->nops);
3687 }
3688 break;
3689 case NFT_MSG_DELCHAIN:
3690 trans->ctx.table->use++;
3691 list_add_tail_rcu(&trans->ctx.chain->list,
3692 &trans->ctx.table->chains);
3693 nft_trans_destroy(trans);
3694 break;
3695 case NFT_MSG_NEWRULE:
3696 trans->ctx.chain->use--;
3697 list_del_rcu(&nft_trans_rule(trans)->list);
3698 break;
3699 case NFT_MSG_DELRULE:
3700 trans->ctx.chain->use++;
3701 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3702 nft_trans_destroy(trans);
3703 break;
3704 case NFT_MSG_NEWSET:
3705 trans->ctx.table->use--;
3706 list_del_rcu(&nft_trans_set(trans)->list);
3707 break;
3708 case NFT_MSG_DELSET:
3709 trans->ctx.table->use++;
3710 list_add_tail_rcu(&nft_trans_set(trans)->list,
3711 &trans->ctx.table->sets);
3712 nft_trans_destroy(trans);
3713 break;
3714 case NFT_MSG_NEWSETELEM:
3715 nft_trans_elem_set(trans)->nelems--;
3716 set = nft_trans_elem_set(trans);
3717 set->ops->get(set, &nft_trans_elem(trans));
3718 set->ops->remove(set, &nft_trans_elem(trans));
3719 nft_trans_destroy(trans);
3720 break;
3721 case NFT_MSG_DELSETELEM:
3722 nft_trans_elem_set(trans)->nelems++;
3723 nft_trans_destroy(trans);
3724 break;
3725 }
3726 }
3727
3728 synchronize_rcu();
3729
3730 list_for_each_entry_safe_reverse(trans, next,
3731 &net->nft.commit_list, list) {
3732 list_del(&trans->list);
3733 nf_tables_abort_release(trans);
3734 }
3735
3736 return 0;
3737 }
3738
3739 static const struct nfnetlink_subsystem nf_tables_subsys = {
3740 .name = "nf_tables",
3741 .subsys_id = NFNL_SUBSYS_NFTABLES,
3742 .cb_count = NFT_MSG_MAX,
3743 .cb = nf_tables_cb,
3744 .commit = nf_tables_commit,
3745 .abort = nf_tables_abort,
3746 };
3747
3748 int nft_chain_validate_dependency(const struct nft_chain *chain,
3749 enum nft_chain_type type)
3750 {
3751 const struct nft_base_chain *basechain;
3752
3753 if (chain->flags & NFT_BASE_CHAIN) {
3754 basechain = nft_base_chain(chain);
3755 if (basechain->type->type != type)
3756 return -EOPNOTSUPP;
3757 }
3758 return 0;
3759 }
3760 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
3761
3762 /*
3763 * Loop detection - walk through the ruleset beginning at the destination chain
3764 * of a new jump until either the source chain is reached (loop) or all
3765 * reachable chains have been traversed.
3766 *
3767 * The loop check is performed whenever a new jump verdict is added to an
3768 * expression or verdict map or a verdict map is bound to a new chain.
3769 */
3770
3771 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3772 const struct nft_chain *chain);
3773
3774 static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3775 const struct nft_set *set,
3776 const struct nft_set_iter *iter,
3777 const struct nft_set_elem *elem)
3778 {
3779 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3780 return 0;
3781
3782 switch (elem->data.verdict) {
3783 case NFT_JUMP:
3784 case NFT_GOTO:
3785 return nf_tables_check_loops(ctx, elem->data.chain);
3786 default:
3787 return 0;
3788 }
3789 }
3790
3791 static int nf_tables_check_loops(const struct nft_ctx *ctx,
3792 const struct nft_chain *chain)
3793 {
3794 const struct nft_rule *rule;
3795 const struct nft_expr *expr, *last;
3796 const struct nft_set *set;
3797 struct nft_set_binding *binding;
3798 struct nft_set_iter iter;
3799
3800 if (ctx->chain == chain)
3801 return -ELOOP;
3802
3803 list_for_each_entry(rule, &chain->rules, list) {
3804 nft_rule_for_each_expr(expr, last, rule) {
3805 const struct nft_data *data = NULL;
3806 int err;
3807
3808 if (!expr->ops->validate)
3809 continue;
3810
3811 err = expr->ops->validate(ctx, expr, &data);
3812 if (err < 0)
3813 return err;
3814
3815 if (data == NULL)
3816 continue;
3817
3818 switch (data->verdict) {
3819 case NFT_JUMP:
3820 case NFT_GOTO:
3821 err = nf_tables_check_loops(ctx, data->chain);
3822 if (err < 0)
3823 return err;
3824 default:
3825 break;
3826 }
3827 }
3828 }
3829
3830 list_for_each_entry(set, &ctx->table->sets, list) {
3831 if (!(set->flags & NFT_SET_MAP) ||
3832 set->dtype != NFT_DATA_VERDICT)
3833 continue;
3834
3835 list_for_each_entry(binding, &set->bindings, list) {
3836 if (binding->chain != chain)
3837 continue;
3838
3839 iter.skip = 0;
3840 iter.count = 0;
3841 iter.err = 0;
3842 iter.fn = nf_tables_loop_check_setelem;
3843
3844 set->ops->walk(ctx, set, &iter);
3845 if (iter.err < 0)
3846 return iter.err;
3847 }
3848 }
3849
3850 return 0;
3851 }
3852
3853 /**
3854 * nft_validate_input_register - validate an expressions' input register
3855 *
3856 * @reg: the register number
3857 *
3858 * Validate that the input register is one of the general purpose
3859 * registers.
3860 */
3861 int nft_validate_input_register(enum nft_registers reg)
3862 {
3863 if (reg <= NFT_REG_VERDICT)
3864 return -EINVAL;
3865 if (reg > NFT_REG_MAX)
3866 return -ERANGE;
3867 return 0;
3868 }
3869 EXPORT_SYMBOL_GPL(nft_validate_input_register);
3870
3871 /**
3872 * nft_validate_output_register - validate an expressions' output register
3873 *
3874 * @reg: the register number
3875 *
3876 * Validate that the output register is one of the general purpose
3877 * registers or the verdict register.
3878 */
3879 int nft_validate_output_register(enum nft_registers reg)
3880 {
3881 if (reg < NFT_REG_VERDICT)
3882 return -EINVAL;
3883 if (reg > NFT_REG_MAX)
3884 return -ERANGE;
3885 return 0;
3886 }
3887 EXPORT_SYMBOL_GPL(nft_validate_output_register);
3888
3889 /**
3890 * nft_validate_data_load - validate an expressions' data load
3891 *
3892 * @ctx: context of the expression performing the load
3893 * @reg: the destination register number
3894 * @data: the data to load
3895 * @type: the data type
3896 *
3897 * Validate that a data load uses the appropriate data type for
3898 * the destination register. A value of NULL for the data means
3899 * that its runtime gathered data, which is always of type
3900 * NFT_DATA_VALUE.
3901 */
3902 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3903 const struct nft_data *data,
3904 enum nft_data_types type)
3905 {
3906 int err;
3907
3908 switch (reg) {
3909 case NFT_REG_VERDICT:
3910 if (data == NULL || type != NFT_DATA_VERDICT)
3911 return -EINVAL;
3912
3913 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3914 err = nf_tables_check_loops(ctx, data->chain);
3915 if (err < 0)
3916 return err;
3917
3918 if (ctx->chain->level + 1 > data->chain->level) {
3919 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3920 return -EMLINK;
3921 data->chain->level = ctx->chain->level + 1;
3922 }
3923 }
3924
3925 return 0;
3926 default:
3927 if (data != NULL && type != NFT_DATA_VALUE)
3928 return -EINVAL;
3929 return 0;
3930 }
3931 }
3932 EXPORT_SYMBOL_GPL(nft_validate_data_load);
3933
3934 static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3935 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3936 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3937 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3938 };
3939
3940 static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3941 struct nft_data_desc *desc, const struct nlattr *nla)
3942 {
3943 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3944 struct nft_chain *chain;
3945 int err;
3946
3947 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3948 if (err < 0)
3949 return err;
3950
3951 if (!tb[NFTA_VERDICT_CODE])
3952 return -EINVAL;
3953 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3954
3955 switch (data->verdict) {
3956 default:
3957 switch (data->verdict & NF_VERDICT_MASK) {
3958 case NF_ACCEPT:
3959 case NF_DROP:
3960 case NF_QUEUE:
3961 break;
3962 default:
3963 return -EINVAL;
3964 }
3965 /* fall through */
3966 case NFT_CONTINUE:
3967 case NFT_BREAK:
3968 case NFT_RETURN:
3969 desc->len = sizeof(data->verdict);
3970 break;
3971 case NFT_JUMP:
3972 case NFT_GOTO:
3973 if (!tb[NFTA_VERDICT_CHAIN])
3974 return -EINVAL;
3975 chain = nf_tables_chain_lookup(ctx->table,
3976 tb[NFTA_VERDICT_CHAIN]);
3977 if (IS_ERR(chain))
3978 return PTR_ERR(chain);
3979 if (chain->flags & NFT_BASE_CHAIN)
3980 return -EOPNOTSUPP;
3981
3982 chain->use++;
3983 data->chain = chain;
3984 desc->len = sizeof(data);
3985 break;
3986 }
3987
3988 desc->type = NFT_DATA_VERDICT;
3989 return 0;
3990 }
3991
3992 static void nft_verdict_uninit(const struct nft_data *data)
3993 {
3994 switch (data->verdict) {
3995 case NFT_JUMP:
3996 case NFT_GOTO:
3997 data->chain->use--;
3998 break;
3999 }
4000 }
4001
4002 static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
4003 {
4004 struct nlattr *nest;
4005
4006 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
4007 if (!nest)
4008 goto nla_put_failure;
4009
4010 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
4011 goto nla_put_failure;
4012
4013 switch (data->verdict) {
4014 case NFT_JUMP:
4015 case NFT_GOTO:
4016 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
4017 goto nla_put_failure;
4018 }
4019 nla_nest_end(skb, nest);
4020 return 0;
4021
4022 nla_put_failure:
4023 return -1;
4024 }
4025
4026 static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
4027 struct nft_data_desc *desc, const struct nlattr *nla)
4028 {
4029 unsigned int len;
4030
4031 len = nla_len(nla);
4032 if (len == 0)
4033 return -EINVAL;
4034 if (len > sizeof(data->data))
4035 return -EOVERFLOW;
4036
4037 nla_memcpy(data->data, nla, sizeof(data->data));
4038 desc->type = NFT_DATA_VALUE;
4039 desc->len = len;
4040 return 0;
4041 }
4042
4043 static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
4044 unsigned int len)
4045 {
4046 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
4047 }
4048
4049 static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
4050 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
4051 .len = FIELD_SIZEOF(struct nft_data, data) },
4052 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
4053 };
4054
4055 /**
4056 * nft_data_init - parse nf_tables data netlink attributes
4057 *
4058 * @ctx: context of the expression using the data
4059 * @data: destination struct nft_data
4060 * @desc: data description
4061 * @nla: netlink attribute containing data
4062 *
4063 * Parse the netlink data attributes and initialize a struct nft_data.
4064 * The type and length of data are returned in the data description.
4065 *
4066 * The caller can indicate that it only wants to accept data of type
4067 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4068 */
4069 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
4070 struct nft_data_desc *desc, const struct nlattr *nla)
4071 {
4072 struct nlattr *tb[NFTA_DATA_MAX + 1];
4073 int err;
4074
4075 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
4076 if (err < 0)
4077 return err;
4078
4079 if (tb[NFTA_DATA_VALUE])
4080 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
4081 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
4082 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
4083 return -EINVAL;
4084 }
4085 EXPORT_SYMBOL_GPL(nft_data_init);
4086
4087 /**
4088 * nft_data_uninit - release a nft_data item
4089 *
4090 * @data: struct nft_data to release
4091 * @type: type of data
4092 *
4093 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4094 * all others need to be released by calling this function.
4095 */
4096 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
4097 {
4098 switch (type) {
4099 case NFT_DATA_VALUE:
4100 return;
4101 case NFT_DATA_VERDICT:
4102 return nft_verdict_uninit(data);
4103 default:
4104 WARN_ON(1);
4105 }
4106 }
4107 EXPORT_SYMBOL_GPL(nft_data_uninit);
4108
4109 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
4110 enum nft_data_types type, unsigned int len)
4111 {
4112 struct nlattr *nest;
4113 int err;
4114
4115 nest = nla_nest_start(skb, attr);
4116 if (nest == NULL)
4117 return -1;
4118
4119 switch (type) {
4120 case NFT_DATA_VALUE:
4121 err = nft_value_dump(skb, data, len);
4122 break;
4123 case NFT_DATA_VERDICT:
4124 err = nft_verdict_dump(skb, data);
4125 break;
4126 default:
4127 err = -EINVAL;
4128 WARN_ON(1);
4129 }
4130
4131 nla_nest_end(skb, nest);
4132 return err;
4133 }
4134 EXPORT_SYMBOL_GPL(nft_data_dump);
4135
4136 static int nf_tables_init_net(struct net *net)
4137 {
4138 INIT_LIST_HEAD(&net->nft.af_info);
4139 INIT_LIST_HEAD(&net->nft.commit_list);
4140 net->nft.base_seq = 1;
4141 return 0;
4142 }
4143
4144 static struct pernet_operations nf_tables_net_ops = {
4145 .init = nf_tables_init_net,
4146 };
4147
4148 static int __init nf_tables_module_init(void)
4149 {
4150 int err;
4151
4152 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
4153 GFP_KERNEL);
4154 if (info == NULL) {
4155 err = -ENOMEM;
4156 goto err1;
4157 }
4158
4159 err = nf_tables_core_module_init();
4160 if (err < 0)
4161 goto err2;
4162
4163 err = nfnetlink_subsys_register(&nf_tables_subsys);
4164 if (err < 0)
4165 goto err3;
4166
4167 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
4168 return register_pernet_subsys(&nf_tables_net_ops);
4169 err3:
4170 nf_tables_core_module_exit();
4171 err2:
4172 kfree(info);
4173 err1:
4174 return err;
4175 }
4176
4177 static void __exit nf_tables_module_exit(void)
4178 {
4179 unregister_pernet_subsys(&nf_tables_net_ops);
4180 nfnetlink_subsys_unregister(&nf_tables_subsys);
4181 rcu_barrier();
4182 nf_tables_core_module_exit();
4183 kfree(info);
4184 }
4185
4186 module_init(nf_tables_module_init);
4187 module_exit(nf_tables_module_exit);
4188
4189 MODULE_LICENSE("GPL");
4190 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4191 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);