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