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