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