2 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
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.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
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>
24 static LIST_HEAD(nf_tables_expressions
);
25 static LIST_HEAD(nf_tables_objects
);
28 * nft_register_afinfo - register nf_tables address family info
30 * @afi: address family info to register
32 * Register the address family for use with nf_tables. Returns zero on
33 * success or a negative errno code otherwise.
35 int nft_register_afinfo(struct net
*net
, struct nft_af_info
*afi
)
37 INIT_LIST_HEAD(&afi
->tables
);
38 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
39 list_add_tail_rcu(&afi
->list
, &net
->nft
.af_info
);
40 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
43 EXPORT_SYMBOL_GPL(nft_register_afinfo
);
45 static void __nft_release_afinfo(struct net
*net
, struct nft_af_info
*afi
);
48 * nft_unregister_afinfo - unregister nf_tables address family info
50 * @afi: address family info to unregister
52 * Unregister the address family for use with nf_tables.
54 void nft_unregister_afinfo(struct net
*net
, struct nft_af_info
*afi
)
56 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
57 __nft_release_afinfo(net
, afi
);
58 list_del_rcu(&afi
->list
);
59 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
61 EXPORT_SYMBOL_GPL(nft_unregister_afinfo
);
63 static struct nft_af_info
*nft_afinfo_lookup(struct net
*net
, int family
)
65 struct nft_af_info
*afi
;
67 list_for_each_entry(afi
, &net
->nft
.af_info
, list
) {
68 if (afi
->family
== family
)
74 static struct nft_af_info
*
75 nf_tables_afinfo_lookup(struct net
*net
, int family
, bool autoload
)
77 struct nft_af_info
*afi
;
79 afi
= nft_afinfo_lookup(net
, family
);
84 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
85 request_module("nft-afinfo-%u", family
);
86 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
87 afi
= nft_afinfo_lookup(net
, family
);
89 return ERR_PTR(-EAGAIN
);
92 return ERR_PTR(-EAFNOSUPPORT
);
95 static void nft_ctx_init(struct nft_ctx
*ctx
,
97 const struct sk_buff
*skb
,
98 const struct nlmsghdr
*nlh
,
99 struct nft_af_info
*afi
,
100 struct nft_table
*table
,
101 struct nft_chain
*chain
,
102 const struct nlattr
* const *nla
)
109 ctx
->portid
= NETLINK_CB(skb
).portid
;
110 ctx
->report
= nlmsg_report(nlh
);
111 ctx
->seq
= nlh
->nlmsg_seq
;
114 static struct nft_trans
*nft_trans_alloc_gfp(const struct nft_ctx
*ctx
,
115 int msg_type
, u32 size
, gfp_t gfp
)
117 struct nft_trans
*trans
;
119 trans
= kzalloc(sizeof(struct nft_trans
) + size
, gfp
);
123 trans
->msg_type
= msg_type
;
129 static struct nft_trans
*nft_trans_alloc(const struct nft_ctx
*ctx
,
130 int msg_type
, u32 size
)
132 return nft_trans_alloc_gfp(ctx
, msg_type
, size
, GFP_KERNEL
);
135 static void nft_trans_destroy(struct nft_trans
*trans
)
137 list_del(&trans
->list
);
141 static int nf_tables_register_hooks(struct net
*net
,
142 const struct nft_table
*table
,
143 struct nft_chain
*chain
,
144 unsigned int hook_nops
)
146 if (table
->flags
& NFT_TABLE_F_DORMANT
||
147 !nft_is_base_chain(chain
))
150 return nf_register_net_hooks(net
, nft_base_chain(chain
)->ops
,
154 static void nf_tables_unregister_hooks(struct net
*net
,
155 const struct nft_table
*table
,
156 struct nft_chain
*chain
,
157 unsigned int hook_nops
)
159 if (table
->flags
& NFT_TABLE_F_DORMANT
||
160 !nft_is_base_chain(chain
))
163 nf_unregister_net_hooks(net
, nft_base_chain(chain
)->ops
, hook_nops
);
166 static int nft_trans_table_add(struct nft_ctx
*ctx
, int msg_type
)
168 struct nft_trans
*trans
;
170 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_table
));
174 if (msg_type
== NFT_MSG_NEWTABLE
)
175 nft_activate_next(ctx
->net
, ctx
->table
);
177 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
181 static int nft_deltable(struct nft_ctx
*ctx
)
185 err
= nft_trans_table_add(ctx
, NFT_MSG_DELTABLE
);
189 nft_deactivate_next(ctx
->net
, ctx
->table
);
193 static int nft_trans_chain_add(struct nft_ctx
*ctx
, int msg_type
)
195 struct nft_trans
*trans
;
197 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_chain
));
201 if (msg_type
== NFT_MSG_NEWCHAIN
)
202 nft_activate_next(ctx
->net
, ctx
->chain
);
204 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
208 static int nft_delchain(struct nft_ctx
*ctx
)
212 err
= nft_trans_chain_add(ctx
, NFT_MSG_DELCHAIN
);
217 nft_deactivate_next(ctx
->net
, ctx
->chain
);
223 nf_tables_delrule_deactivate(struct nft_ctx
*ctx
, struct nft_rule
*rule
)
225 /* You cannot delete the same rule twice */
226 if (nft_is_active_next(ctx
->net
, rule
)) {
227 nft_deactivate_next(ctx
->net
, rule
);
234 static struct nft_trans
*nft_trans_rule_add(struct nft_ctx
*ctx
, int msg_type
,
235 struct nft_rule
*rule
)
237 struct nft_trans
*trans
;
239 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_rule
));
243 if (msg_type
== NFT_MSG_NEWRULE
&& ctx
->nla
[NFTA_RULE_ID
] != NULL
) {
244 nft_trans_rule_id(trans
) =
245 ntohl(nla_get_be32(ctx
->nla
[NFTA_RULE_ID
]));
247 nft_trans_rule(trans
) = rule
;
248 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
253 static int nft_delrule(struct nft_ctx
*ctx
, struct nft_rule
*rule
)
255 struct nft_trans
*trans
;
258 trans
= nft_trans_rule_add(ctx
, NFT_MSG_DELRULE
, rule
);
262 err
= nf_tables_delrule_deactivate(ctx
, rule
);
264 nft_trans_destroy(trans
);
271 static int nft_delrule_by_chain(struct nft_ctx
*ctx
)
273 struct nft_rule
*rule
;
276 list_for_each_entry(rule
, &ctx
->chain
->rules
, list
) {
277 err
= nft_delrule(ctx
, rule
);
284 static int nft_trans_set_add(struct nft_ctx
*ctx
, int msg_type
,
287 struct nft_trans
*trans
;
289 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_set
));
293 if (msg_type
== NFT_MSG_NEWSET
&& ctx
->nla
[NFTA_SET_ID
] != NULL
) {
294 nft_trans_set_id(trans
) =
295 ntohl(nla_get_be32(ctx
->nla
[NFTA_SET_ID
]));
296 nft_activate_next(ctx
->net
, set
);
298 nft_trans_set(trans
) = set
;
299 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
304 static int nft_delset(struct nft_ctx
*ctx
, struct nft_set
*set
)
308 err
= nft_trans_set_add(ctx
, NFT_MSG_DELSET
, set
);
312 nft_deactivate_next(ctx
->net
, set
);
318 static int nft_trans_obj_add(struct nft_ctx
*ctx
, int msg_type
,
319 struct nft_object
*obj
)
321 struct nft_trans
*trans
;
323 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_obj
));
327 if (msg_type
== NFT_MSG_NEWOBJ
)
328 nft_activate_next(ctx
->net
, obj
);
330 nft_trans_obj(trans
) = obj
;
331 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
336 static int nft_delobj(struct nft_ctx
*ctx
, struct nft_object
*obj
)
340 err
= nft_trans_obj_add(ctx
, NFT_MSG_DELOBJ
, obj
);
344 nft_deactivate_next(ctx
->net
, obj
);
354 static struct nft_table
*nft_table_lookup(const struct nft_af_info
*afi
,
355 const struct nlattr
*nla
,
358 struct nft_table
*table
;
360 list_for_each_entry(table
, &afi
->tables
, list
) {
361 if (!nla_strcmp(nla
, table
->name
) &&
362 nft_active_genmask(table
, genmask
))
368 static struct nft_table
*nf_tables_table_lookup(const struct nft_af_info
*afi
,
369 const struct nlattr
*nla
,
372 struct nft_table
*table
;
375 return ERR_PTR(-EINVAL
);
377 table
= nft_table_lookup(afi
, nla
, genmask
);
381 return ERR_PTR(-ENOENT
);
384 static inline u64
nf_tables_alloc_handle(struct nft_table
*table
)
386 return ++table
->hgenerator
;
389 static const struct nf_chain_type
*chain_type
[AF_MAX
][NFT_CHAIN_T_MAX
];
391 static const struct nf_chain_type
*
392 __nf_tables_chain_type_lookup(int family
, const struct nlattr
*nla
)
396 for (i
= 0; i
< NFT_CHAIN_T_MAX
; i
++) {
397 if (chain_type
[family
][i
] != NULL
&&
398 !nla_strcmp(nla
, chain_type
[family
][i
]->name
))
399 return chain_type
[family
][i
];
404 static const struct nf_chain_type
*
405 nf_tables_chain_type_lookup(const struct nft_af_info
*afi
,
406 const struct nlattr
*nla
,
409 const struct nf_chain_type
*type
;
411 type
= __nf_tables_chain_type_lookup(afi
->family
, nla
);
414 #ifdef CONFIG_MODULES
416 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
417 request_module("nft-chain-%u-%.*s", afi
->family
,
418 nla_len(nla
), (const char *)nla_data(nla
));
419 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
420 type
= __nf_tables_chain_type_lookup(afi
->family
, nla
);
422 return ERR_PTR(-EAGAIN
);
425 return ERR_PTR(-ENOENT
);
428 static const struct nla_policy nft_table_policy
[NFTA_TABLE_MAX
+ 1] = {
429 [NFTA_TABLE_NAME
] = { .type
= NLA_STRING
,
430 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
431 [NFTA_TABLE_FLAGS
] = { .type
= NLA_U32
},
434 static int nf_tables_fill_table_info(struct sk_buff
*skb
, struct net
*net
,
435 u32 portid
, u32 seq
, int event
, u32 flags
,
436 int family
, const struct nft_table
*table
)
438 struct nlmsghdr
*nlh
;
439 struct nfgenmsg
*nfmsg
;
441 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
442 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
444 goto nla_put_failure
;
446 nfmsg
= nlmsg_data(nlh
);
447 nfmsg
->nfgen_family
= family
;
448 nfmsg
->version
= NFNETLINK_V0
;
449 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
451 if (nla_put_string(skb
, NFTA_TABLE_NAME
, table
->name
) ||
452 nla_put_be32(skb
, NFTA_TABLE_FLAGS
, htonl(table
->flags
)) ||
453 nla_put_be32(skb
, NFTA_TABLE_USE
, htonl(table
->use
)))
454 goto nla_put_failure
;
460 nlmsg_trim(skb
, nlh
);
464 static void nf_tables_table_notify(const struct nft_ctx
*ctx
, int event
)
470 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
473 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
477 err
= nf_tables_fill_table_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
478 event
, 0, ctx
->afi
->family
, ctx
->table
);
484 nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
485 ctx
->report
, GFP_KERNEL
);
488 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
491 static int nf_tables_dump_tables(struct sk_buff
*skb
,
492 struct netlink_callback
*cb
)
494 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
495 const struct nft_af_info
*afi
;
496 const struct nft_table
*table
;
497 unsigned int idx
= 0, s_idx
= cb
->args
[0];
498 struct net
*net
= sock_net(skb
->sk
);
499 int family
= nfmsg
->nfgen_family
;
502 cb
->seq
= net
->nft
.base_seq
;
504 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
505 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
508 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
512 memset(&cb
->args
[1], 0,
513 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
514 if (!nft_is_active(net
, table
))
516 if (nf_tables_fill_table_info(skb
, net
,
517 NETLINK_CB(cb
->skb
).portid
,
521 afi
->family
, table
) < 0)
524 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
535 static int nf_tables_gettable(struct net
*net
, struct sock
*nlsk
,
536 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
537 const struct nlattr
* const nla
[])
539 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
540 u8 genmask
= nft_genmask_cur(net
);
541 const struct nft_af_info
*afi
;
542 const struct nft_table
*table
;
543 struct sk_buff
*skb2
;
544 int family
= nfmsg
->nfgen_family
;
547 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
548 struct netlink_dump_control c
= {
549 .dump
= nf_tables_dump_tables
,
551 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
554 afi
= nf_tables_afinfo_lookup(net
, family
, false);
558 table
= nf_tables_table_lookup(afi
, nla
[NFTA_TABLE_NAME
], genmask
);
560 return PTR_ERR(table
);
562 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
566 err
= nf_tables_fill_table_info(skb2
, net
, NETLINK_CB(skb
).portid
,
567 nlh
->nlmsg_seq
, NFT_MSG_NEWTABLE
, 0,
572 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
579 static void _nf_tables_table_disable(struct net
*net
,
580 const struct nft_af_info
*afi
,
581 struct nft_table
*table
,
584 struct nft_chain
*chain
;
587 list_for_each_entry(chain
, &table
->chains
, list
) {
588 if (!nft_is_active_next(net
, chain
))
590 if (!nft_is_base_chain(chain
))
593 if (cnt
&& i
++ == cnt
)
596 nf_unregister_net_hooks(net
, nft_base_chain(chain
)->ops
,
601 static int nf_tables_table_enable(struct net
*net
,
602 const struct nft_af_info
*afi
,
603 struct nft_table
*table
)
605 struct nft_chain
*chain
;
608 list_for_each_entry(chain
, &table
->chains
, list
) {
609 if (!nft_is_active_next(net
, chain
))
611 if (!nft_is_base_chain(chain
))
614 err
= nf_register_net_hooks(net
, nft_base_chain(chain
)->ops
,
624 _nf_tables_table_disable(net
, afi
, table
, i
);
628 static void nf_tables_table_disable(struct net
*net
,
629 const struct nft_af_info
*afi
,
630 struct nft_table
*table
)
632 _nf_tables_table_disable(net
, afi
, table
, 0);
635 static int nf_tables_updtable(struct nft_ctx
*ctx
)
637 struct nft_trans
*trans
;
641 if (!ctx
->nla
[NFTA_TABLE_FLAGS
])
644 flags
= ntohl(nla_get_be32(ctx
->nla
[NFTA_TABLE_FLAGS
]));
645 if (flags
& ~NFT_TABLE_F_DORMANT
)
648 if (flags
== ctx
->table
->flags
)
651 trans
= nft_trans_alloc(ctx
, NFT_MSG_NEWTABLE
,
652 sizeof(struct nft_trans_table
));
656 if ((flags
& NFT_TABLE_F_DORMANT
) &&
657 !(ctx
->table
->flags
& NFT_TABLE_F_DORMANT
)) {
658 nft_trans_table_enable(trans
) = false;
659 } else if (!(flags
& NFT_TABLE_F_DORMANT
) &&
660 ctx
->table
->flags
& NFT_TABLE_F_DORMANT
) {
661 ret
= nf_tables_table_enable(ctx
->net
, ctx
->afi
, ctx
->table
);
663 ctx
->table
->flags
&= ~NFT_TABLE_F_DORMANT
;
664 nft_trans_table_enable(trans
) = true;
670 nft_trans_table_update(trans
) = true;
671 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
674 nft_trans_destroy(trans
);
678 static int nf_tables_newtable(struct net
*net
, struct sock
*nlsk
,
679 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
680 const struct nlattr
* const nla
[])
682 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
683 u8 genmask
= nft_genmask_next(net
);
684 const struct nlattr
*name
;
685 struct nft_af_info
*afi
;
686 struct nft_table
*table
;
687 int family
= nfmsg
->nfgen_family
;
692 afi
= nf_tables_afinfo_lookup(net
, family
, true);
696 name
= nla
[NFTA_TABLE_NAME
];
697 table
= nf_tables_table_lookup(afi
, name
, genmask
);
699 if (PTR_ERR(table
) != -ENOENT
)
700 return PTR_ERR(table
);
702 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
704 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
707 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
708 return nf_tables_updtable(&ctx
);
711 if (nla
[NFTA_TABLE_FLAGS
]) {
712 flags
= ntohl(nla_get_be32(nla
[NFTA_TABLE_FLAGS
]));
713 if (flags
& ~NFT_TABLE_F_DORMANT
)
718 if (!try_module_get(afi
->owner
))
722 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
726 nla_strlcpy(table
->name
, name
, NFT_TABLE_MAXNAMELEN
);
727 INIT_LIST_HEAD(&table
->chains
);
728 INIT_LIST_HEAD(&table
->sets
);
729 INIT_LIST_HEAD(&table
->objects
);
730 table
->flags
= flags
;
732 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
733 err
= nft_trans_table_add(&ctx
, NFT_MSG_NEWTABLE
);
737 list_add_tail_rcu(&table
->list
, &afi
->tables
);
742 module_put(afi
->owner
);
747 static int nft_flush_table(struct nft_ctx
*ctx
)
750 struct nft_chain
*chain
, *nc
;
751 struct nft_object
*obj
, *ne
;
752 struct nft_set
*set
, *ns
;
754 list_for_each_entry(chain
, &ctx
->table
->chains
, list
) {
755 if (!nft_is_active_next(ctx
->net
, chain
))
760 err
= nft_delrule_by_chain(ctx
);
765 list_for_each_entry_safe(set
, ns
, &ctx
->table
->sets
, list
) {
766 if (!nft_is_active_next(ctx
->net
, set
))
769 if (set
->flags
& NFT_SET_ANONYMOUS
&&
770 !list_empty(&set
->bindings
))
773 err
= nft_delset(ctx
, set
);
778 list_for_each_entry_safe(obj
, ne
, &ctx
->table
->objects
, list
) {
779 err
= nft_delobj(ctx
, obj
);
784 list_for_each_entry_safe(chain
, nc
, &ctx
->table
->chains
, list
) {
785 if (!nft_is_active_next(ctx
->net
, chain
))
790 err
= nft_delchain(ctx
);
795 err
= nft_deltable(ctx
);
800 static int nft_flush(struct nft_ctx
*ctx
, int family
)
802 struct nft_af_info
*afi
;
803 struct nft_table
*table
, *nt
;
804 const struct nlattr
* const *nla
= ctx
->nla
;
807 list_for_each_entry(afi
, &ctx
->net
->nft
.af_info
, list
) {
808 if (family
!= AF_UNSPEC
&& afi
->family
!= family
)
812 list_for_each_entry_safe(table
, nt
, &afi
->tables
, list
) {
813 if (!nft_is_active_next(ctx
->net
, table
))
816 if (nla
[NFTA_TABLE_NAME
] &&
817 nla_strcmp(nla
[NFTA_TABLE_NAME
], table
->name
) != 0)
822 err
= nft_flush_table(ctx
);
831 static int nf_tables_deltable(struct net
*net
, struct sock
*nlsk
,
832 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
833 const struct nlattr
* const nla
[])
835 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
836 u8 genmask
= nft_genmask_next(net
);
837 struct nft_af_info
*afi
;
838 struct nft_table
*table
;
839 int family
= nfmsg
->nfgen_family
;
842 nft_ctx_init(&ctx
, net
, skb
, nlh
, NULL
, NULL
, NULL
, nla
);
843 if (family
== AF_UNSPEC
|| nla
[NFTA_TABLE_NAME
] == NULL
)
844 return nft_flush(&ctx
, family
);
846 afi
= nf_tables_afinfo_lookup(net
, family
, false);
850 table
= nf_tables_table_lookup(afi
, nla
[NFTA_TABLE_NAME
], genmask
);
852 return PTR_ERR(table
);
857 return nft_flush_table(&ctx
);
860 static void nf_tables_table_destroy(struct nft_ctx
*ctx
)
862 BUG_ON(ctx
->table
->use
> 0);
865 module_put(ctx
->afi
->owner
);
868 int nft_register_chain_type(const struct nf_chain_type
*ctype
)
872 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
873 if (chain_type
[ctype
->family
][ctype
->type
] != NULL
) {
877 chain_type
[ctype
->family
][ctype
->type
] = ctype
;
879 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
882 EXPORT_SYMBOL_GPL(nft_register_chain_type
);
884 void nft_unregister_chain_type(const struct nf_chain_type
*ctype
)
886 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
887 chain_type
[ctype
->family
][ctype
->type
] = NULL
;
888 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
890 EXPORT_SYMBOL_GPL(nft_unregister_chain_type
);
896 static struct nft_chain
*
897 nf_tables_chain_lookup_byhandle(const struct nft_table
*table
, u64 handle
,
900 struct nft_chain
*chain
;
902 list_for_each_entry(chain
, &table
->chains
, list
) {
903 if (chain
->handle
== handle
&&
904 nft_active_genmask(chain
, genmask
))
908 return ERR_PTR(-ENOENT
);
911 static struct nft_chain
*nf_tables_chain_lookup(const struct nft_table
*table
,
912 const struct nlattr
*nla
,
915 struct nft_chain
*chain
;
918 return ERR_PTR(-EINVAL
);
920 list_for_each_entry(chain
, &table
->chains
, list
) {
921 if (!nla_strcmp(nla
, chain
->name
) &&
922 nft_active_genmask(chain
, genmask
))
926 return ERR_PTR(-ENOENT
);
929 static const struct nla_policy nft_chain_policy
[NFTA_CHAIN_MAX
+ 1] = {
930 [NFTA_CHAIN_TABLE
] = { .type
= NLA_STRING
,
931 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
932 [NFTA_CHAIN_HANDLE
] = { .type
= NLA_U64
},
933 [NFTA_CHAIN_NAME
] = { .type
= NLA_STRING
,
934 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
935 [NFTA_CHAIN_HOOK
] = { .type
= NLA_NESTED
},
936 [NFTA_CHAIN_POLICY
] = { .type
= NLA_U32
},
937 [NFTA_CHAIN_TYPE
] = { .type
= NLA_STRING
},
938 [NFTA_CHAIN_COUNTERS
] = { .type
= NLA_NESTED
},
941 static const struct nla_policy nft_hook_policy
[NFTA_HOOK_MAX
+ 1] = {
942 [NFTA_HOOK_HOOKNUM
] = { .type
= NLA_U32
},
943 [NFTA_HOOK_PRIORITY
] = { .type
= NLA_U32
},
944 [NFTA_HOOK_DEV
] = { .type
= NLA_STRING
,
945 .len
= IFNAMSIZ
- 1 },
948 static int nft_dump_stats(struct sk_buff
*skb
, struct nft_stats __percpu
*stats
)
950 struct nft_stats
*cpu_stats
, total
;
956 memset(&total
, 0, sizeof(total
));
957 for_each_possible_cpu(cpu
) {
958 cpu_stats
= per_cpu_ptr(stats
, cpu
);
960 seq
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
961 pkts
= cpu_stats
->pkts
;
962 bytes
= cpu_stats
->bytes
;
963 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, seq
));
965 total
.bytes
+= bytes
;
967 nest
= nla_nest_start(skb
, NFTA_CHAIN_COUNTERS
);
969 goto nla_put_failure
;
971 if (nla_put_be64(skb
, NFTA_COUNTER_PACKETS
, cpu_to_be64(total
.pkts
),
973 nla_put_be64(skb
, NFTA_COUNTER_BYTES
, cpu_to_be64(total
.bytes
),
975 goto nla_put_failure
;
977 nla_nest_end(skb
, nest
);
984 static int nf_tables_fill_chain_info(struct sk_buff
*skb
, struct net
*net
,
985 u32 portid
, u32 seq
, int event
, u32 flags
,
986 int family
, const struct nft_table
*table
,
987 const struct nft_chain
*chain
)
989 struct nlmsghdr
*nlh
;
990 struct nfgenmsg
*nfmsg
;
992 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
993 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
995 goto nla_put_failure
;
997 nfmsg
= nlmsg_data(nlh
);
998 nfmsg
->nfgen_family
= family
;
999 nfmsg
->version
= NFNETLINK_V0
;
1000 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
1002 if (nla_put_string(skb
, NFTA_CHAIN_TABLE
, table
->name
))
1003 goto nla_put_failure
;
1004 if (nla_put_be64(skb
, NFTA_CHAIN_HANDLE
, cpu_to_be64(chain
->handle
),
1006 goto nla_put_failure
;
1007 if (nla_put_string(skb
, NFTA_CHAIN_NAME
, chain
->name
))
1008 goto nla_put_failure
;
1010 if (nft_is_base_chain(chain
)) {
1011 const struct nft_base_chain
*basechain
= nft_base_chain(chain
);
1012 const struct nf_hook_ops
*ops
= &basechain
->ops
[0];
1013 struct nlattr
*nest
;
1015 nest
= nla_nest_start(skb
, NFTA_CHAIN_HOOK
);
1017 goto nla_put_failure
;
1018 if (nla_put_be32(skb
, NFTA_HOOK_HOOKNUM
, htonl(ops
->hooknum
)))
1019 goto nla_put_failure
;
1020 if (nla_put_be32(skb
, NFTA_HOOK_PRIORITY
, htonl(ops
->priority
)))
1021 goto nla_put_failure
;
1022 if (basechain
->dev_name
[0] &&
1023 nla_put_string(skb
, NFTA_HOOK_DEV
, basechain
->dev_name
))
1024 goto nla_put_failure
;
1025 nla_nest_end(skb
, nest
);
1027 if (nla_put_be32(skb
, NFTA_CHAIN_POLICY
,
1028 htonl(basechain
->policy
)))
1029 goto nla_put_failure
;
1031 if (nla_put_string(skb
, NFTA_CHAIN_TYPE
, basechain
->type
->name
))
1032 goto nla_put_failure
;
1034 if (nft_dump_stats(skb
, nft_base_chain(chain
)->stats
))
1035 goto nla_put_failure
;
1038 if (nla_put_be32(skb
, NFTA_CHAIN_USE
, htonl(chain
->use
)))
1039 goto nla_put_failure
;
1041 nlmsg_end(skb
, nlh
);
1045 nlmsg_trim(skb
, nlh
);
1049 static void nf_tables_chain_notify(const struct nft_ctx
*ctx
, int event
)
1051 struct sk_buff
*skb
;
1055 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
1058 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1062 err
= nf_tables_fill_chain_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
1063 event
, 0, ctx
->afi
->family
, ctx
->table
,
1070 nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1071 ctx
->report
, GFP_KERNEL
);
1074 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
1077 static int nf_tables_dump_chains(struct sk_buff
*skb
,
1078 struct netlink_callback
*cb
)
1080 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1081 const struct nft_af_info
*afi
;
1082 const struct nft_table
*table
;
1083 const struct nft_chain
*chain
;
1084 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1085 struct net
*net
= sock_net(skb
->sk
);
1086 int family
= nfmsg
->nfgen_family
;
1089 cb
->seq
= net
->nft
.base_seq
;
1091 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
1092 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
1095 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
1096 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
1100 memset(&cb
->args
[1], 0,
1101 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1102 if (!nft_is_active(net
, chain
))
1104 if (nf_tables_fill_chain_info(skb
, net
,
1105 NETLINK_CB(cb
->skb
).portid
,
1109 afi
->family
, table
, chain
) < 0)
1112 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
1124 static int nf_tables_getchain(struct net
*net
, struct sock
*nlsk
,
1125 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1126 const struct nlattr
* const nla
[])
1128 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1129 u8 genmask
= nft_genmask_cur(net
);
1130 const struct nft_af_info
*afi
;
1131 const struct nft_table
*table
;
1132 const struct nft_chain
*chain
;
1133 struct sk_buff
*skb2
;
1134 int family
= nfmsg
->nfgen_family
;
1137 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1138 struct netlink_dump_control c
= {
1139 .dump
= nf_tables_dump_chains
,
1141 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
1144 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1146 return PTR_ERR(afi
);
1148 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
], genmask
);
1150 return PTR_ERR(table
);
1152 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
], genmask
);
1154 return PTR_ERR(chain
);
1156 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1160 err
= nf_tables_fill_chain_info(skb2
, net
, NETLINK_CB(skb
).portid
,
1161 nlh
->nlmsg_seq
, NFT_MSG_NEWCHAIN
, 0,
1162 family
, table
, chain
);
1166 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
1173 static const struct nla_policy nft_counter_policy
[NFTA_COUNTER_MAX
+ 1] = {
1174 [NFTA_COUNTER_PACKETS
] = { .type
= NLA_U64
},
1175 [NFTA_COUNTER_BYTES
] = { .type
= NLA_U64
},
1178 static struct nft_stats __percpu
*nft_stats_alloc(const struct nlattr
*attr
)
1180 struct nlattr
*tb
[NFTA_COUNTER_MAX
+1];
1181 struct nft_stats __percpu
*newstats
;
1182 struct nft_stats
*stats
;
1185 err
= nla_parse_nested(tb
, NFTA_COUNTER_MAX
, attr
, nft_counter_policy
,
1188 return ERR_PTR(err
);
1190 if (!tb
[NFTA_COUNTER_BYTES
] || !tb
[NFTA_COUNTER_PACKETS
])
1191 return ERR_PTR(-EINVAL
);
1193 newstats
= netdev_alloc_pcpu_stats(struct nft_stats
);
1194 if (newstats
== NULL
)
1195 return ERR_PTR(-ENOMEM
);
1197 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1198 * are not exposed to userspace.
1201 stats
= this_cpu_ptr(newstats
);
1202 stats
->bytes
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_BYTES
]));
1203 stats
->pkts
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_PACKETS
]));
1209 static void nft_chain_stats_replace(struct nft_base_chain
*chain
,
1210 struct nft_stats __percpu
*newstats
)
1212 if (newstats
== NULL
)
1216 struct nft_stats __percpu
*oldstats
=
1217 nft_dereference(chain
->stats
);
1219 rcu_assign_pointer(chain
->stats
, newstats
);
1221 free_percpu(oldstats
);
1223 rcu_assign_pointer(chain
->stats
, newstats
);
1226 static void nf_tables_chain_destroy(struct nft_chain
*chain
)
1228 BUG_ON(chain
->use
> 0);
1230 if (nft_is_base_chain(chain
)) {
1231 struct nft_base_chain
*basechain
= nft_base_chain(chain
);
1233 module_put(basechain
->type
->owner
);
1234 free_percpu(basechain
->stats
);
1235 if (basechain
->ops
[0].dev
!= NULL
)
1236 dev_put(basechain
->ops
[0].dev
);
1243 struct nft_chain_hook
{
1246 const struct nf_chain_type
*type
;
1247 struct net_device
*dev
;
1250 static int nft_chain_parse_hook(struct net
*net
,
1251 const struct nlattr
* const nla
[],
1252 struct nft_af_info
*afi
,
1253 struct nft_chain_hook
*hook
, bool create
)
1255 struct nlattr
*ha
[NFTA_HOOK_MAX
+ 1];
1256 const struct nf_chain_type
*type
;
1257 struct net_device
*dev
;
1260 err
= nla_parse_nested(ha
, NFTA_HOOK_MAX
, nla
[NFTA_CHAIN_HOOK
],
1261 nft_hook_policy
, NULL
);
1265 if (ha
[NFTA_HOOK_HOOKNUM
] == NULL
||
1266 ha
[NFTA_HOOK_PRIORITY
] == NULL
)
1269 hook
->num
= ntohl(nla_get_be32(ha
[NFTA_HOOK_HOOKNUM
]));
1270 if (hook
->num
>= afi
->nhooks
)
1273 hook
->priority
= ntohl(nla_get_be32(ha
[NFTA_HOOK_PRIORITY
]));
1275 type
= chain_type
[afi
->family
][NFT_CHAIN_T_DEFAULT
];
1276 if (nla
[NFTA_CHAIN_TYPE
]) {
1277 type
= nf_tables_chain_type_lookup(afi
, nla
[NFTA_CHAIN_TYPE
],
1280 return PTR_ERR(type
);
1282 if (!(type
->hook_mask
& (1 << hook
->num
)))
1284 if (!try_module_get(type
->owner
))
1290 if (afi
->flags
& NFT_AF_NEEDS_DEV
) {
1291 char ifname
[IFNAMSIZ
];
1293 if (!ha
[NFTA_HOOK_DEV
]) {
1294 module_put(type
->owner
);
1298 nla_strlcpy(ifname
, ha
[NFTA_HOOK_DEV
], IFNAMSIZ
);
1299 dev
= dev_get_by_name(net
, ifname
);
1301 module_put(type
->owner
);
1305 } else if (ha
[NFTA_HOOK_DEV
]) {
1306 module_put(type
->owner
);
1313 static void nft_chain_release_hook(struct nft_chain_hook
*hook
)
1315 module_put(hook
->type
->owner
);
1316 if (hook
->dev
!= NULL
)
1320 static int nf_tables_newchain(struct net
*net
, struct sock
*nlsk
,
1321 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1322 const struct nlattr
* const nla
[])
1324 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1325 const struct nlattr
* uninitialized_var(name
);
1326 struct nft_af_info
*afi
;
1327 struct nft_table
*table
;
1328 struct nft_chain
*chain
;
1329 struct nft_base_chain
*basechain
= NULL
;
1330 u8 genmask
= nft_genmask_next(net
);
1331 int family
= nfmsg
->nfgen_family
;
1332 u8 policy
= NF_ACCEPT
;
1335 struct nft_stats __percpu
*stats
;
1340 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
1342 afi
= nf_tables_afinfo_lookup(net
, family
, true);
1344 return PTR_ERR(afi
);
1346 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
], genmask
);
1348 return PTR_ERR(table
);
1351 name
= nla
[NFTA_CHAIN_NAME
];
1353 if (nla
[NFTA_CHAIN_HANDLE
]) {
1354 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_CHAIN_HANDLE
]));
1355 chain
= nf_tables_chain_lookup_byhandle(table
, handle
, genmask
);
1357 return PTR_ERR(chain
);
1359 chain
= nf_tables_chain_lookup(table
, name
, genmask
);
1360 if (IS_ERR(chain
)) {
1361 if (PTR_ERR(chain
) != -ENOENT
)
1362 return PTR_ERR(chain
);
1367 if (nla
[NFTA_CHAIN_POLICY
]) {
1368 if (chain
!= NULL
&&
1369 !nft_is_base_chain(chain
))
1372 if (chain
== NULL
&&
1373 nla
[NFTA_CHAIN_HOOK
] == NULL
)
1376 policy
= ntohl(nla_get_be32(nla
[NFTA_CHAIN_POLICY
]));
1386 if (chain
!= NULL
) {
1387 struct nft_stats
*stats
= NULL
;
1388 struct nft_trans
*trans
;
1390 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
1392 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1395 if (nla
[NFTA_CHAIN_HOOK
]) {
1396 struct nft_base_chain
*basechain
;
1397 struct nft_chain_hook hook
;
1398 struct nf_hook_ops
*ops
;
1400 if (!nft_is_base_chain(chain
))
1403 err
= nft_chain_parse_hook(net
, nla
, afi
, &hook
,
1408 basechain
= nft_base_chain(chain
);
1409 if (basechain
->type
!= hook
.type
) {
1410 nft_chain_release_hook(&hook
);
1414 for (i
= 0; i
< afi
->nops
; i
++) {
1415 ops
= &basechain
->ops
[i
];
1416 if (ops
->hooknum
!= hook
.num
||
1417 ops
->priority
!= hook
.priority
||
1418 ops
->dev
!= hook
.dev
) {
1419 nft_chain_release_hook(&hook
);
1423 nft_chain_release_hook(&hook
);
1426 if (nla
[NFTA_CHAIN_HANDLE
] && name
) {
1427 struct nft_chain
*chain2
;
1429 chain2
= nf_tables_chain_lookup(table
,
1430 nla
[NFTA_CHAIN_NAME
],
1433 return PTR_ERR(chain2
);
1436 if (nla
[NFTA_CHAIN_COUNTERS
]) {
1437 if (!nft_is_base_chain(chain
))
1440 stats
= nft_stats_alloc(nla
[NFTA_CHAIN_COUNTERS
]);
1442 return PTR_ERR(stats
);
1445 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
1446 trans
= nft_trans_alloc(&ctx
, NFT_MSG_NEWCHAIN
,
1447 sizeof(struct nft_trans_chain
));
1448 if (trans
== NULL
) {
1453 nft_trans_chain_stats(trans
) = stats
;
1454 nft_trans_chain_update(trans
) = true;
1456 if (nla
[NFTA_CHAIN_POLICY
])
1457 nft_trans_chain_policy(trans
) = policy
;
1459 nft_trans_chain_policy(trans
) = -1;
1461 if (nla
[NFTA_CHAIN_HANDLE
] && name
) {
1462 nla_strlcpy(nft_trans_chain_name(trans
), name
,
1463 NFT_CHAIN_MAXNAMELEN
);
1465 list_add_tail(&trans
->list
, &net
->nft
.commit_list
);
1469 if (table
->use
== UINT_MAX
)
1472 if (nla
[NFTA_CHAIN_HOOK
]) {
1473 struct nft_chain_hook hook
;
1474 struct nf_hook_ops
*ops
;
1477 err
= nft_chain_parse_hook(net
, nla
, afi
, &hook
, create
);
1481 basechain
= kzalloc(sizeof(*basechain
), GFP_KERNEL
);
1482 if (basechain
== NULL
) {
1483 nft_chain_release_hook(&hook
);
1487 if (hook
.dev
!= NULL
)
1488 strncpy(basechain
->dev_name
, hook
.dev
->name
, IFNAMSIZ
);
1490 if (nla
[NFTA_CHAIN_COUNTERS
]) {
1491 stats
= nft_stats_alloc(nla
[NFTA_CHAIN_COUNTERS
]);
1492 if (IS_ERR(stats
)) {
1493 nft_chain_release_hook(&hook
);
1495 return PTR_ERR(stats
);
1497 basechain
->stats
= stats
;
1499 stats
= netdev_alloc_pcpu_stats(struct nft_stats
);
1500 if (stats
== NULL
) {
1501 nft_chain_release_hook(&hook
);
1505 rcu_assign_pointer(basechain
->stats
, stats
);
1508 hookfn
= hook
.type
->hooks
[hook
.num
];
1509 basechain
->type
= hook
.type
;
1510 chain
= &basechain
->chain
;
1512 for (i
= 0; i
< afi
->nops
; i
++) {
1513 ops
= &basechain
->ops
[i
];
1515 ops
->hooknum
= hook
.num
;
1516 ops
->priority
= hook
.priority
;
1518 ops
->hook
= afi
->hooks
[ops
->hooknum
];
1519 ops
->dev
= hook
.dev
;
1522 if (afi
->hook_ops_init
)
1523 afi
->hook_ops_init(ops
, i
);
1526 chain
->flags
|= NFT_BASE_CHAIN
;
1527 basechain
->policy
= policy
;
1529 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
1534 INIT_LIST_HEAD(&chain
->rules
);
1535 chain
->handle
= nf_tables_alloc_handle(table
);
1536 chain
->table
= table
;
1537 nla_strlcpy(chain
->name
, name
, NFT_CHAIN_MAXNAMELEN
);
1539 err
= nf_tables_register_hooks(net
, table
, chain
, afi
->nops
);
1543 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
1544 err
= nft_trans_chain_add(&ctx
, NFT_MSG_NEWCHAIN
);
1549 list_add_tail_rcu(&chain
->list
, &table
->chains
);
1552 nf_tables_unregister_hooks(net
, table
, chain
, afi
->nops
);
1554 nf_tables_chain_destroy(chain
);
1558 static int nf_tables_delchain(struct net
*net
, struct sock
*nlsk
,
1559 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1560 const struct nlattr
* const nla
[])
1562 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1563 u8 genmask
= nft_genmask_next(net
);
1564 struct nft_af_info
*afi
;
1565 struct nft_table
*table
;
1566 struct nft_chain
*chain
;
1567 int family
= nfmsg
->nfgen_family
;
1570 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1572 return PTR_ERR(afi
);
1574 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
], genmask
);
1576 return PTR_ERR(table
);
1578 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
], genmask
);
1580 return PTR_ERR(chain
);
1584 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
1586 return nft_delchain(&ctx
);
1594 * nft_register_expr - register nf_tables expr type
1597 * Registers the expr type for use with nf_tables. Returns zero on
1598 * success or a negative errno code otherwise.
1600 int nft_register_expr(struct nft_expr_type
*type
)
1602 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1603 if (type
->family
== NFPROTO_UNSPEC
)
1604 list_add_tail_rcu(&type
->list
, &nf_tables_expressions
);
1606 list_add_rcu(&type
->list
, &nf_tables_expressions
);
1607 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1610 EXPORT_SYMBOL_GPL(nft_register_expr
);
1613 * nft_unregister_expr - unregister nf_tables expr type
1616 * Unregisters the expr typefor use with nf_tables.
1618 void nft_unregister_expr(struct nft_expr_type
*type
)
1620 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1621 list_del_rcu(&type
->list
);
1622 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1624 EXPORT_SYMBOL_GPL(nft_unregister_expr
);
1626 static const struct nft_expr_type
*__nft_expr_type_get(u8 family
,
1629 const struct nft_expr_type
*type
;
1631 list_for_each_entry(type
, &nf_tables_expressions
, list
) {
1632 if (!nla_strcmp(nla
, type
->name
) &&
1633 (!type
->family
|| type
->family
== family
))
1639 static const struct nft_expr_type
*nft_expr_type_get(u8 family
,
1642 const struct nft_expr_type
*type
;
1645 return ERR_PTR(-EINVAL
);
1647 type
= __nft_expr_type_get(family
, nla
);
1648 if (type
!= NULL
&& try_module_get(type
->owner
))
1651 #ifdef CONFIG_MODULES
1653 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1654 request_module("nft-expr-%u-%.*s", family
,
1655 nla_len(nla
), (char *)nla_data(nla
));
1656 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1657 if (__nft_expr_type_get(family
, nla
))
1658 return ERR_PTR(-EAGAIN
);
1660 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1661 request_module("nft-expr-%.*s",
1662 nla_len(nla
), (char *)nla_data(nla
));
1663 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1664 if (__nft_expr_type_get(family
, nla
))
1665 return ERR_PTR(-EAGAIN
);
1668 return ERR_PTR(-ENOENT
);
1671 static const struct nla_policy nft_expr_policy
[NFTA_EXPR_MAX
+ 1] = {
1672 [NFTA_EXPR_NAME
] = { .type
= NLA_STRING
},
1673 [NFTA_EXPR_DATA
] = { .type
= NLA_NESTED
},
1676 static int nf_tables_fill_expr_info(struct sk_buff
*skb
,
1677 const struct nft_expr
*expr
)
1679 if (nla_put_string(skb
, NFTA_EXPR_NAME
, expr
->ops
->type
->name
))
1680 goto nla_put_failure
;
1682 if (expr
->ops
->dump
) {
1683 struct nlattr
*data
= nla_nest_start(skb
, NFTA_EXPR_DATA
);
1685 goto nla_put_failure
;
1686 if (expr
->ops
->dump(skb
, expr
) < 0)
1687 goto nla_put_failure
;
1688 nla_nest_end(skb
, data
);
1697 int nft_expr_dump(struct sk_buff
*skb
, unsigned int attr
,
1698 const struct nft_expr
*expr
)
1700 struct nlattr
*nest
;
1702 nest
= nla_nest_start(skb
, attr
);
1704 goto nla_put_failure
;
1705 if (nf_tables_fill_expr_info(skb
, expr
) < 0)
1706 goto nla_put_failure
;
1707 nla_nest_end(skb
, nest
);
1714 struct nft_expr_info
{
1715 const struct nft_expr_ops
*ops
;
1716 struct nlattr
*tb
[NFT_EXPR_MAXATTR
+ 1];
1719 static int nf_tables_expr_parse(const struct nft_ctx
*ctx
,
1720 const struct nlattr
*nla
,
1721 struct nft_expr_info
*info
)
1723 const struct nft_expr_type
*type
;
1724 const struct nft_expr_ops
*ops
;
1725 struct nlattr
*tb
[NFTA_EXPR_MAX
+ 1];
1728 err
= nla_parse_nested(tb
, NFTA_EXPR_MAX
, nla
, nft_expr_policy
, NULL
);
1732 type
= nft_expr_type_get(ctx
->afi
->family
, tb
[NFTA_EXPR_NAME
]);
1734 return PTR_ERR(type
);
1736 if (tb
[NFTA_EXPR_DATA
]) {
1737 err
= nla_parse_nested(info
->tb
, type
->maxattr
,
1738 tb
[NFTA_EXPR_DATA
], type
->policy
, NULL
);
1742 memset(info
->tb
, 0, sizeof(info
->tb
[0]) * (type
->maxattr
+ 1));
1744 if (type
->select_ops
!= NULL
) {
1745 ops
= type
->select_ops(ctx
,
1746 (const struct nlattr
* const *)info
->tb
);
1758 module_put(type
->owner
);
1762 static int nf_tables_newexpr(const struct nft_ctx
*ctx
,
1763 const struct nft_expr_info
*info
,
1764 struct nft_expr
*expr
)
1766 const struct nft_expr_ops
*ops
= info
->ops
;
1771 err
= ops
->init(ctx
, expr
, (const struct nlattr
**)info
->tb
);
1776 if (ops
->validate
) {
1777 const struct nft_data
*data
= NULL
;
1779 err
= ops
->validate(ctx
, expr
, &data
);
1788 ops
->destroy(ctx
, expr
);
1794 static void nf_tables_expr_destroy(const struct nft_ctx
*ctx
,
1795 struct nft_expr
*expr
)
1797 if (expr
->ops
->destroy
)
1798 expr
->ops
->destroy(ctx
, expr
);
1799 module_put(expr
->ops
->type
->owner
);
1802 struct nft_expr
*nft_expr_init(const struct nft_ctx
*ctx
,
1803 const struct nlattr
*nla
)
1805 struct nft_expr_info info
;
1806 struct nft_expr
*expr
;
1809 err
= nf_tables_expr_parse(ctx
, nla
, &info
);
1814 expr
= kzalloc(info
.ops
->size
, GFP_KERNEL
);
1818 err
= nf_tables_newexpr(ctx
, &info
, expr
);
1826 module_put(info
.ops
->type
->owner
);
1828 return ERR_PTR(err
);
1831 void nft_expr_destroy(const struct nft_ctx
*ctx
, struct nft_expr
*expr
)
1833 nf_tables_expr_destroy(ctx
, expr
);
1841 static struct nft_rule
*__nf_tables_rule_lookup(const struct nft_chain
*chain
,
1844 struct nft_rule
*rule
;
1846 // FIXME: this sucks
1847 list_for_each_entry(rule
, &chain
->rules
, list
) {
1848 if (handle
== rule
->handle
)
1852 return ERR_PTR(-ENOENT
);
1855 static struct nft_rule
*nf_tables_rule_lookup(const struct nft_chain
*chain
,
1856 const struct nlattr
*nla
)
1859 return ERR_PTR(-EINVAL
);
1861 return __nf_tables_rule_lookup(chain
, be64_to_cpu(nla_get_be64(nla
)));
1864 static const struct nla_policy nft_rule_policy
[NFTA_RULE_MAX
+ 1] = {
1865 [NFTA_RULE_TABLE
] = { .type
= NLA_STRING
,
1866 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
1867 [NFTA_RULE_CHAIN
] = { .type
= NLA_STRING
,
1868 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
1869 [NFTA_RULE_HANDLE
] = { .type
= NLA_U64
},
1870 [NFTA_RULE_EXPRESSIONS
] = { .type
= NLA_NESTED
},
1871 [NFTA_RULE_COMPAT
] = { .type
= NLA_NESTED
},
1872 [NFTA_RULE_POSITION
] = { .type
= NLA_U64
},
1873 [NFTA_RULE_USERDATA
] = { .type
= NLA_BINARY
,
1874 .len
= NFT_USERDATA_MAXLEN
},
1877 static int nf_tables_fill_rule_info(struct sk_buff
*skb
, struct net
*net
,
1878 u32 portid
, u32 seq
, int event
,
1879 u32 flags
, int family
,
1880 const struct nft_table
*table
,
1881 const struct nft_chain
*chain
,
1882 const struct nft_rule
*rule
)
1884 struct nlmsghdr
*nlh
;
1885 struct nfgenmsg
*nfmsg
;
1886 const struct nft_expr
*expr
, *next
;
1887 struct nlattr
*list
;
1888 const struct nft_rule
*prule
;
1889 u16 type
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
1891 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(struct nfgenmsg
), flags
);
1893 goto nla_put_failure
;
1895 nfmsg
= nlmsg_data(nlh
);
1896 nfmsg
->nfgen_family
= family
;
1897 nfmsg
->version
= NFNETLINK_V0
;
1898 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
1900 if (nla_put_string(skb
, NFTA_RULE_TABLE
, table
->name
))
1901 goto nla_put_failure
;
1902 if (nla_put_string(skb
, NFTA_RULE_CHAIN
, chain
->name
))
1903 goto nla_put_failure
;
1904 if (nla_put_be64(skb
, NFTA_RULE_HANDLE
, cpu_to_be64(rule
->handle
),
1906 goto nla_put_failure
;
1908 if ((event
!= NFT_MSG_DELRULE
) && (rule
->list
.prev
!= &chain
->rules
)) {
1909 prule
= list_prev_entry(rule
, list
);
1910 if (nla_put_be64(skb
, NFTA_RULE_POSITION
,
1911 cpu_to_be64(prule
->handle
),
1913 goto nla_put_failure
;
1916 list
= nla_nest_start(skb
, NFTA_RULE_EXPRESSIONS
);
1918 goto nla_put_failure
;
1919 nft_rule_for_each_expr(expr
, next
, rule
) {
1920 if (nft_expr_dump(skb
, NFTA_LIST_ELEM
, expr
) < 0)
1921 goto nla_put_failure
;
1923 nla_nest_end(skb
, list
);
1926 struct nft_userdata
*udata
= nft_userdata(rule
);
1927 if (nla_put(skb
, NFTA_RULE_USERDATA
, udata
->len
+ 1,
1929 goto nla_put_failure
;
1932 nlmsg_end(skb
, nlh
);
1936 nlmsg_trim(skb
, nlh
);
1940 static void nf_tables_rule_notify(const struct nft_ctx
*ctx
,
1941 const struct nft_rule
*rule
, int event
)
1943 struct sk_buff
*skb
;
1947 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
1950 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1954 err
= nf_tables_fill_rule_info(skb
, ctx
->net
, ctx
->portid
, ctx
->seq
,
1955 event
, 0, ctx
->afi
->family
, ctx
->table
,
1962 nfnetlink_send(skb
, ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
,
1963 ctx
->report
, GFP_KERNEL
);
1966 nfnetlink_set_err(ctx
->net
, ctx
->portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
1969 struct nft_rule_dump_ctx
{
1970 char table
[NFT_TABLE_MAXNAMELEN
];
1971 char chain
[NFT_CHAIN_MAXNAMELEN
];
1974 static int nf_tables_dump_rules(struct sk_buff
*skb
,
1975 struct netlink_callback
*cb
)
1977 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1978 const struct nft_rule_dump_ctx
*ctx
= cb
->data
;
1979 const struct nft_af_info
*afi
;
1980 const struct nft_table
*table
;
1981 const struct nft_chain
*chain
;
1982 const struct nft_rule
*rule
;
1983 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1984 struct net
*net
= sock_net(skb
->sk
);
1985 int family
= nfmsg
->nfgen_family
;
1988 cb
->seq
= net
->nft
.base_seq
;
1990 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
1991 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
1994 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
1995 if (ctx
&& ctx
->table
[0] &&
1996 strcmp(ctx
->table
, table
->name
) != 0)
1999 list_for_each_entry_rcu(chain
, &table
->chains
, list
) {
2000 if (ctx
&& ctx
->chain
[0] &&
2001 strcmp(ctx
->chain
, chain
->name
) != 0)
2004 list_for_each_entry_rcu(rule
, &chain
->rules
, list
) {
2005 if (!nft_is_active(net
, rule
))
2010 memset(&cb
->args
[1], 0,
2011 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
2012 if (nf_tables_fill_rule_info(skb
, net
, NETLINK_CB(cb
->skb
).portid
,
2015 NLM_F_MULTI
| NLM_F_APPEND
,
2016 afi
->family
, table
, chain
, rule
) < 0)
2019 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2033 static int nf_tables_dump_rules_done(struct netlink_callback
*cb
)
2039 static int nf_tables_getrule(struct net
*net
, struct sock
*nlsk
,
2040 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2041 const struct nlattr
* const nla
[])
2043 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2044 u8 genmask
= nft_genmask_cur(net
);
2045 const struct nft_af_info
*afi
;
2046 const struct nft_table
*table
;
2047 const struct nft_chain
*chain
;
2048 const struct nft_rule
*rule
;
2049 struct sk_buff
*skb2
;
2050 int family
= nfmsg
->nfgen_family
;
2053 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2054 struct netlink_dump_control c
= {
2055 .dump
= nf_tables_dump_rules
,
2056 .done
= nf_tables_dump_rules_done
,
2059 if (nla
[NFTA_RULE_TABLE
] || nla
[NFTA_RULE_CHAIN
]) {
2060 struct nft_rule_dump_ctx
*ctx
;
2062 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
2066 if (nla
[NFTA_RULE_TABLE
])
2067 nla_strlcpy(ctx
->table
, nla
[NFTA_RULE_TABLE
],
2068 sizeof(ctx
->table
));
2069 if (nla
[NFTA_RULE_CHAIN
])
2070 nla_strlcpy(ctx
->chain
, nla
[NFTA_RULE_CHAIN
],
2071 sizeof(ctx
->chain
));
2075 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
2078 afi
= nf_tables_afinfo_lookup(net
, family
, false);
2080 return PTR_ERR(afi
);
2082 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
], genmask
);
2084 return PTR_ERR(table
);
2086 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
], genmask
);
2088 return PTR_ERR(chain
);
2090 rule
= nf_tables_rule_lookup(chain
, nla
[NFTA_RULE_HANDLE
]);
2092 return PTR_ERR(rule
);
2094 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2098 err
= nf_tables_fill_rule_info(skb2
, net
, NETLINK_CB(skb
).portid
,
2099 nlh
->nlmsg_seq
, NFT_MSG_NEWRULE
, 0,
2100 family
, table
, chain
, rule
);
2104 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
2111 static void nf_tables_rule_destroy(const struct nft_ctx
*ctx
,
2112 struct nft_rule
*rule
)
2114 struct nft_expr
*expr
;
2117 * Careful: some expressions might not be initialized in case this
2118 * is called on error from nf_tables_newrule().
2120 expr
= nft_expr_first(rule
);
2121 while (expr
!= nft_expr_last(rule
) && expr
->ops
) {
2122 nf_tables_expr_destroy(ctx
, expr
);
2123 expr
= nft_expr_next(expr
);
2128 #define NFT_RULE_MAXEXPRS 128
2130 static struct nft_expr_info
*info
;
2132 static int nf_tables_newrule(struct net
*net
, struct sock
*nlsk
,
2133 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2134 const struct nlattr
* const nla
[])
2136 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2137 u8 genmask
= nft_genmask_next(net
);
2138 struct nft_af_info
*afi
;
2139 struct nft_table
*table
;
2140 struct nft_chain
*chain
;
2141 struct nft_rule
*rule
, *old_rule
= NULL
;
2142 struct nft_userdata
*udata
;
2143 struct nft_trans
*trans
= NULL
;
2144 struct nft_expr
*expr
;
2147 unsigned int size
, i
, n
, ulen
= 0, usize
= 0;
2150 u64 handle
, pos_handle
;
2152 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
2154 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, create
);
2156 return PTR_ERR(afi
);
2158 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
], genmask
);
2160 return PTR_ERR(table
);
2162 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
], genmask
);
2164 return PTR_ERR(chain
);
2166 if (nla
[NFTA_RULE_HANDLE
]) {
2167 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_HANDLE
]));
2168 rule
= __nf_tables_rule_lookup(chain
, handle
);
2170 return PTR_ERR(rule
);
2172 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2174 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2179 if (!create
|| nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2181 handle
= nf_tables_alloc_handle(table
);
2183 if (chain
->use
== UINT_MAX
)
2187 if (nla
[NFTA_RULE_POSITION
]) {
2188 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
2191 pos_handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_POSITION
]));
2192 old_rule
= __nf_tables_rule_lookup(chain
, pos_handle
);
2193 if (IS_ERR(old_rule
))
2194 return PTR_ERR(old_rule
);
2197 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
2201 if (nla
[NFTA_RULE_EXPRESSIONS
]) {
2202 nla_for_each_nested(tmp
, nla
[NFTA_RULE_EXPRESSIONS
], rem
) {
2204 if (nla_type(tmp
) != NFTA_LIST_ELEM
)
2206 if (n
== NFT_RULE_MAXEXPRS
)
2208 err
= nf_tables_expr_parse(&ctx
, tmp
, &info
[n
]);
2211 size
+= info
[n
].ops
->size
;
2215 /* Check for overflow of dlen field */
2217 if (size
>= 1 << 12)
2220 if (nla
[NFTA_RULE_USERDATA
]) {
2221 ulen
= nla_len(nla
[NFTA_RULE_USERDATA
]);
2223 usize
= sizeof(struct nft_userdata
) + ulen
;
2227 rule
= kzalloc(sizeof(*rule
) + size
+ usize
, GFP_KERNEL
);
2231 nft_activate_next(net
, rule
);
2233 rule
->handle
= handle
;
2235 rule
->udata
= ulen
? 1 : 0;
2238 udata
= nft_userdata(rule
);
2239 udata
->len
= ulen
- 1;
2240 nla_memcpy(udata
->data
, nla
[NFTA_RULE_USERDATA
], ulen
);
2243 expr
= nft_expr_first(rule
);
2244 for (i
= 0; i
< n
; i
++) {
2245 err
= nf_tables_newexpr(&ctx
, &info
[i
], expr
);
2249 expr
= nft_expr_next(expr
);
2252 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
2253 if (nft_is_active_next(net
, old_rule
)) {
2254 trans
= nft_trans_rule_add(&ctx
, NFT_MSG_DELRULE
,
2256 if (trans
== NULL
) {
2260 nft_deactivate_next(net
, old_rule
);
2262 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
2267 } else if (nlh
->nlmsg_flags
& NLM_F_APPEND
)
2269 list_add_rcu(&rule
->list
, &old_rule
->list
);
2271 list_add_tail_rcu(&rule
->list
, &chain
->rules
);
2274 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
2276 list_add_rcu(&rule
->list
, &chain
->rules
);
2279 if (nft_trans_rule_add(&ctx
, NFT_MSG_NEWRULE
, rule
) == NULL
) {
2287 list_del_rcu(&rule
->list
);
2289 nf_tables_rule_destroy(&ctx
, rule
);
2291 for (i
= 0; i
< n
; i
++) {
2292 if (info
[i
].ops
!= NULL
)
2293 module_put(info
[i
].ops
->type
->owner
);
2298 static struct nft_rule
*nft_rule_lookup_byid(const struct net
*net
,
2299 const struct nlattr
*nla
)
2301 u32 id
= ntohl(nla_get_be32(nla
));
2302 struct nft_trans
*trans
;
2304 list_for_each_entry(trans
, &net
->nft
.commit_list
, list
) {
2305 struct nft_rule
*rule
= nft_trans_rule(trans
);
2307 if (trans
->msg_type
== NFT_MSG_NEWRULE
&&
2308 id
== nft_trans_rule_id(trans
))
2311 return ERR_PTR(-ENOENT
);
2314 static int nf_tables_delrule(struct net
*net
, struct sock
*nlsk
,
2315 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2316 const struct nlattr
* const nla
[])
2318 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2319 u8 genmask
= nft_genmask_next(net
);
2320 struct nft_af_info
*afi
;
2321 struct nft_table
*table
;
2322 struct nft_chain
*chain
= NULL
;
2323 struct nft_rule
*rule
;
2324 int family
= nfmsg
->nfgen_family
, err
= 0;
2327 afi
= nf_tables_afinfo_lookup(net
, family
, false);
2329 return PTR_ERR(afi
);
2331 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
], genmask
);
2333 return PTR_ERR(table
);
2335 if (nla
[NFTA_RULE_CHAIN
]) {
2336 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
],
2339 return PTR_ERR(chain
);
2342 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, chain
, nla
);
2345 if (nla
[NFTA_RULE_HANDLE
]) {
2346 rule
= nf_tables_rule_lookup(chain
,
2347 nla
[NFTA_RULE_HANDLE
]);
2349 return PTR_ERR(rule
);
2351 err
= nft_delrule(&ctx
, rule
);
2352 } else if (nla
[NFTA_RULE_ID
]) {
2353 rule
= nft_rule_lookup_byid(net
, nla
[NFTA_RULE_ID
]);
2355 return PTR_ERR(rule
);
2357 err
= nft_delrule(&ctx
, rule
);
2359 err
= nft_delrule_by_chain(&ctx
);
2362 list_for_each_entry(chain
, &table
->chains
, list
) {
2363 if (!nft_is_active_next(net
, chain
))
2367 err
= nft_delrule_by_chain(&ctx
);
2380 static LIST_HEAD(nf_tables_set_ops
);
2382 int nft_register_set(struct nft_set_ops
*ops
)
2384 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2385 list_add_tail_rcu(&ops
->list
, &nf_tables_set_ops
);
2386 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2389 EXPORT_SYMBOL_GPL(nft_register_set
);
2391 void nft_unregister_set(struct nft_set_ops
*ops
)
2393 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2394 list_del_rcu(&ops
->list
);
2395 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2397 EXPORT_SYMBOL_GPL(nft_unregister_set
);
2400 * Select a set implementation based on the data characteristics and the
2401 * given policy. The total memory use might not be known if no size is
2402 * given, in that case the amount of memory per element is used.
2404 static const struct nft_set_ops
*
2405 nft_select_set_ops(const struct nlattr
* const nla
[],
2406 const struct nft_set_desc
*desc
,
2407 enum nft_set_policies policy
)
2409 const struct nft_set_ops
*ops
, *bops
;
2410 struct nft_set_estimate est
, best
;
2413 #ifdef CONFIG_MODULES
2414 if (list_empty(&nf_tables_set_ops
)) {
2415 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
2416 request_module("nft-set");
2417 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
2418 if (!list_empty(&nf_tables_set_ops
))
2419 return ERR_PTR(-EAGAIN
);
2423 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
2424 features
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
2425 features
&= NFT_SET_INTERVAL
| NFT_SET_MAP
| NFT_SET_TIMEOUT
|
2434 list_for_each_entry(ops
, &nf_tables_set_ops
, list
) {
2435 if ((ops
->features
& features
) != features
)
2437 if (!ops
->estimate(desc
, features
, &est
))
2441 case NFT_SET_POL_PERFORMANCE
:
2442 if (est
.lookup
< best
.lookup
)
2444 if (est
.lookup
== best
.lookup
) {
2446 if (est
.space
< best
.space
)
2448 } else if (est
.size
< best
.size
) {
2453 case NFT_SET_POL_MEMORY
:
2455 if (est
.space
< best
.space
)
2457 if (est
.space
== best
.space
&&
2458 est
.lookup
< best
.lookup
)
2460 } else if (est
.size
< best
.size
) {
2468 if (!try_module_get(ops
->owner
))
2471 module_put(bops
->owner
);
2480 return ERR_PTR(-EOPNOTSUPP
);
2483 static const struct nla_policy nft_set_policy
[NFTA_SET_MAX
+ 1] = {
2484 [NFTA_SET_TABLE
] = { .type
= NLA_STRING
,
2485 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
2486 [NFTA_SET_NAME
] = { .type
= NLA_STRING
,
2487 .len
= NFT_SET_MAXNAMELEN
- 1 },
2488 [NFTA_SET_FLAGS
] = { .type
= NLA_U32
},
2489 [NFTA_SET_KEY_TYPE
] = { .type
= NLA_U32
},
2490 [NFTA_SET_KEY_LEN
] = { .type
= NLA_U32
},
2491 [NFTA_SET_DATA_TYPE
] = { .type
= NLA_U32
},
2492 [NFTA_SET_DATA_LEN
] = { .type
= NLA_U32
},
2493 [NFTA_SET_POLICY
] = { .type
= NLA_U32
},
2494 [NFTA_SET_DESC
] = { .type
= NLA_NESTED
},
2495 [NFTA_SET_ID
] = { .type
= NLA_U32
},
2496 [NFTA_SET_TIMEOUT
] = { .type
= NLA_U64
},
2497 [NFTA_SET_GC_INTERVAL
] = { .type
= NLA_U32
},
2498 [NFTA_SET_USERDATA
] = { .type
= NLA_BINARY
,
2499 .len
= NFT_USERDATA_MAXLEN
},
2500 [NFTA_SET_OBJ_TYPE
] = { .type
= NLA_U32
},
2503 static const struct nla_policy nft_set_desc_policy
[NFTA_SET_DESC_MAX
+ 1] = {
2504 [NFTA_SET_DESC_SIZE
] = { .type
= NLA_U32
},
2507 static int nft_ctx_init_from_setattr(struct nft_ctx
*ctx
, struct net
*net
,
2508 const struct sk_buff
*skb
,
2509 const struct nlmsghdr
*nlh
,
2510 const struct nlattr
* const nla
[],
2513 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2514 struct nft_af_info
*afi
= NULL
;
2515 struct nft_table
*table
= NULL
;
2517 if (nfmsg
->nfgen_family
!= NFPROTO_UNSPEC
) {
2518 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, false);
2520 return PTR_ERR(afi
);
2523 if (nla
[NFTA_SET_TABLE
] != NULL
) {
2525 return -EAFNOSUPPORT
;
2527 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
],
2530 return PTR_ERR(table
);
2533 nft_ctx_init(ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
2537 static struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
2538 const struct nlattr
*nla
, u8 genmask
)
2540 struct nft_set
*set
;
2543 return ERR_PTR(-EINVAL
);
2545 list_for_each_entry(set
, &table
->sets
, list
) {
2546 if (!nla_strcmp(nla
, set
->name
) &&
2547 nft_active_genmask(set
, genmask
))
2550 return ERR_PTR(-ENOENT
);
2553 static struct nft_set
*nf_tables_set_lookup_byid(const struct net
*net
,
2554 const struct nlattr
*nla
,
2557 struct nft_trans
*trans
;
2558 u32 id
= ntohl(nla_get_be32(nla
));
2560 list_for_each_entry(trans
, &net
->nft
.commit_list
, list
) {
2561 struct nft_set
*set
= nft_trans_set(trans
);
2563 if (trans
->msg_type
== NFT_MSG_NEWSET
&&
2564 id
== nft_trans_set_id(trans
) &&
2565 nft_active_genmask(set
, genmask
))
2568 return ERR_PTR(-ENOENT
);
2571 struct nft_set
*nft_set_lookup(const struct net
*net
,
2572 const struct nft_table
*table
,
2573 const struct nlattr
*nla_set_name
,
2574 const struct nlattr
*nla_set_id
,
2577 struct nft_set
*set
;
2579 set
= nf_tables_set_lookup(table
, nla_set_name
, genmask
);
2584 set
= nf_tables_set_lookup_byid(net
, nla_set_id
, genmask
);
2588 EXPORT_SYMBOL_GPL(nft_set_lookup
);
2590 static int nf_tables_set_alloc_name(struct nft_ctx
*ctx
, struct nft_set
*set
,
2593 const struct nft_set
*i
;
2595 unsigned long *inuse
;
2596 unsigned int n
= 0, min
= 0;
2598 p
= strnchr(name
, NFT_SET_MAXNAMELEN
, '%');
2600 if (p
[1] != 'd' || strchr(p
+ 2, '%'))
2603 inuse
= (unsigned long *)get_zeroed_page(GFP_KERNEL
);
2607 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
2610 if (!nft_is_active_next(ctx
->net
, set
))
2612 if (!sscanf(i
->name
, name
, &tmp
))
2614 if (tmp
< min
|| tmp
>= min
+ BITS_PER_BYTE
* PAGE_SIZE
)
2617 set_bit(tmp
- min
, inuse
);
2620 n
= find_first_zero_bit(inuse
, BITS_PER_BYTE
* PAGE_SIZE
);
2621 if (n
>= BITS_PER_BYTE
* PAGE_SIZE
) {
2622 min
+= BITS_PER_BYTE
* PAGE_SIZE
;
2623 memset(inuse
, 0, PAGE_SIZE
);
2626 free_page((unsigned long)inuse
);
2629 snprintf(set
->name
, sizeof(set
->name
), name
, min
+ n
);
2630 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
2631 if (!nft_is_active_next(ctx
->net
, i
))
2633 if (!strcmp(set
->name
, i
->name
))
2639 static int nf_tables_fill_set(struct sk_buff
*skb
, const struct nft_ctx
*ctx
,
2640 const struct nft_set
*set
, u16 event
, u16 flags
)
2642 struct nfgenmsg
*nfmsg
;
2643 struct nlmsghdr
*nlh
;
2644 struct nlattr
*desc
;
2645 u32 portid
= ctx
->portid
;
2648 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
2649 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
2652 goto nla_put_failure
;
2654 nfmsg
= nlmsg_data(nlh
);
2655 nfmsg
->nfgen_family
= ctx
->afi
->family
;
2656 nfmsg
->version
= NFNETLINK_V0
;
2657 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
2659 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
2660 goto nla_put_failure
;
2661 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
2662 goto nla_put_failure
;
2663 if (set
->flags
!= 0)
2664 if (nla_put_be32(skb
, NFTA_SET_FLAGS
, htonl(set
->flags
)))
2665 goto nla_put_failure
;
2667 if (nla_put_be32(skb
, NFTA_SET_KEY_TYPE
, htonl(set
->ktype
)))
2668 goto nla_put_failure
;
2669 if (nla_put_be32(skb
, NFTA_SET_KEY_LEN
, htonl(set
->klen
)))
2670 goto nla_put_failure
;
2671 if (set
->flags
& NFT_SET_MAP
) {
2672 if (nla_put_be32(skb
, NFTA_SET_DATA_TYPE
, htonl(set
->dtype
)))
2673 goto nla_put_failure
;
2674 if (nla_put_be32(skb
, NFTA_SET_DATA_LEN
, htonl(set
->dlen
)))
2675 goto nla_put_failure
;
2677 if (set
->flags
& NFT_SET_OBJECT
&&
2678 nla_put_be32(skb
, NFTA_SET_OBJ_TYPE
, htonl(set
->objtype
)))
2679 goto nla_put_failure
;
2682 nla_put_be64(skb
, NFTA_SET_TIMEOUT
,
2683 cpu_to_be64(jiffies_to_msecs(set
->timeout
)),
2685 goto nla_put_failure
;
2687 nla_put_be32(skb
, NFTA_SET_GC_INTERVAL
, htonl(set
->gc_int
)))
2688 goto nla_put_failure
;
2690 if (set
->policy
!= NFT_SET_POL_PERFORMANCE
) {
2691 if (nla_put_be32(skb
, NFTA_SET_POLICY
, htonl(set
->policy
)))
2692 goto nla_put_failure
;
2695 if (nla_put(skb
, NFTA_SET_USERDATA
, set
->udlen
, set
->udata
))
2696 goto nla_put_failure
;
2698 desc
= nla_nest_start(skb
, NFTA_SET_DESC
);
2700 goto nla_put_failure
;
2702 nla_put_be32(skb
, NFTA_SET_DESC_SIZE
, htonl(set
->size
)))
2703 goto nla_put_failure
;
2704 nla_nest_end(skb
, desc
);
2706 nlmsg_end(skb
, nlh
);
2710 nlmsg_trim(skb
, nlh
);
2714 static void nf_tables_set_notify(const struct nft_ctx
*ctx
,
2715 const struct nft_set
*set
, int event
,
2718 struct sk_buff
*skb
;
2719 u32 portid
= ctx
->portid
;
2723 !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
2726 skb
= nlmsg_new(NLMSG_GOODSIZE
, gfp_flags
);
2730 err
= nf_tables_fill_set(skb
, ctx
, set
, event
, 0);
2736 nfnetlink_send(skb
, ctx
->net
, portid
, NFNLGRP_NFTABLES
, ctx
->report
,
2740 nfnetlink_set_err(ctx
->net
, portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
2743 static int nf_tables_dump_sets(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2745 const struct nft_set
*set
;
2746 unsigned int idx
, s_idx
= cb
->args
[0];
2747 struct nft_af_info
*afi
;
2748 struct nft_table
*table
, *cur_table
= (struct nft_table
*)cb
->args
[2];
2749 struct net
*net
= sock_net(skb
->sk
);
2750 int cur_family
= cb
->args
[3];
2751 struct nft_ctx
*ctx
= cb
->data
, ctx_set
;
2757 cb
->seq
= net
->nft
.base_seq
;
2759 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
2760 if (ctx
->afi
&& ctx
->afi
!= afi
)
2764 if (afi
->family
!= cur_family
)
2769 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
2770 if (ctx
->table
&& ctx
->table
!= table
)
2774 if (cur_table
!= table
)
2780 list_for_each_entry_rcu(set
, &table
->sets
, list
) {
2783 if (!nft_is_active(net
, set
))
2787 ctx_set
.table
= table
;
2789 if (nf_tables_fill_set(skb
, &ctx_set
, set
,
2793 cb
->args
[2] = (unsigned long) table
;
2794 cb
->args
[3] = afi
->family
;
2797 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
2811 static int nf_tables_dump_sets_done(struct netlink_callback
*cb
)
2817 static int nf_tables_getset(struct net
*net
, struct sock
*nlsk
,
2818 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2819 const struct nlattr
* const nla
[])
2821 u8 genmask
= nft_genmask_cur(net
);
2822 const struct nft_set
*set
;
2824 struct sk_buff
*skb2
;
2825 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2828 /* Verify existence before starting dump */
2829 err
= nft_ctx_init_from_setattr(&ctx
, net
, skb
, nlh
, nla
, genmask
);
2833 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2834 struct netlink_dump_control c
= {
2835 .dump
= nf_tables_dump_sets
,
2836 .done
= nf_tables_dump_sets_done
,
2838 struct nft_ctx
*ctx_dump
;
2840 ctx_dump
= kmalloc(sizeof(*ctx_dump
), GFP_KERNEL
);
2841 if (ctx_dump
== NULL
)
2847 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
2850 /* Only accept unspec with dump */
2851 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
2852 return -EAFNOSUPPORT
;
2853 if (!nla
[NFTA_SET_TABLE
])
2856 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
], genmask
);
2858 return PTR_ERR(set
);
2860 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2864 err
= nf_tables_fill_set(skb2
, &ctx
, set
, NFT_MSG_NEWSET
, 0);
2868 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
2875 static int nf_tables_set_desc_parse(const struct nft_ctx
*ctx
,
2876 struct nft_set_desc
*desc
,
2877 const struct nlattr
*nla
)
2879 struct nlattr
*da
[NFTA_SET_DESC_MAX
+ 1];
2882 err
= nla_parse_nested(da
, NFTA_SET_DESC_MAX
, nla
,
2883 nft_set_desc_policy
, NULL
);
2887 if (da
[NFTA_SET_DESC_SIZE
] != NULL
)
2888 desc
->size
= ntohl(nla_get_be32(da
[NFTA_SET_DESC_SIZE
]));
2893 static int nf_tables_newset(struct net
*net
, struct sock
*nlsk
,
2894 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
2895 const struct nlattr
* const nla
[])
2897 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2898 u8 genmask
= nft_genmask_next(net
);
2899 const struct nft_set_ops
*ops
;
2900 struct nft_af_info
*afi
;
2901 struct nft_table
*table
;
2902 struct nft_set
*set
;
2904 char name
[NFT_SET_MAXNAMELEN
];
2908 u32 ktype
, dtype
, flags
, policy
, gc_int
, objtype
;
2909 struct nft_set_desc desc
;
2910 unsigned char *udata
;
2914 if (nla
[NFTA_SET_TABLE
] == NULL
||
2915 nla
[NFTA_SET_NAME
] == NULL
||
2916 nla
[NFTA_SET_KEY_LEN
] == NULL
||
2917 nla
[NFTA_SET_ID
] == NULL
)
2920 memset(&desc
, 0, sizeof(desc
));
2922 ktype
= NFT_DATA_VALUE
;
2923 if (nla
[NFTA_SET_KEY_TYPE
] != NULL
) {
2924 ktype
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_TYPE
]));
2925 if ((ktype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
)
2929 desc
.klen
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_LEN
]));
2930 if (desc
.klen
== 0 || desc
.klen
> NFT_DATA_VALUE_MAXLEN
)
2934 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
2935 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
2936 if (flags
& ~(NFT_SET_ANONYMOUS
| NFT_SET_CONSTANT
|
2937 NFT_SET_INTERVAL
| NFT_SET_TIMEOUT
|
2938 NFT_SET_MAP
| NFT_SET_EVAL
|
2941 /* Only one of these operations is supported */
2942 if ((flags
& (NFT_SET_MAP
| NFT_SET_EVAL
| NFT_SET_OBJECT
)) ==
2943 (NFT_SET_MAP
| NFT_SET_EVAL
| NFT_SET_OBJECT
))
2948 if (nla
[NFTA_SET_DATA_TYPE
] != NULL
) {
2949 if (!(flags
& NFT_SET_MAP
))
2952 dtype
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_TYPE
]));
2953 if ((dtype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
&&
2954 dtype
!= NFT_DATA_VERDICT
)
2957 if (dtype
!= NFT_DATA_VERDICT
) {
2958 if (nla
[NFTA_SET_DATA_LEN
] == NULL
)
2960 desc
.dlen
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_LEN
]));
2961 if (desc
.dlen
== 0 || desc
.dlen
> NFT_DATA_VALUE_MAXLEN
)
2964 desc
.dlen
= sizeof(struct nft_verdict
);
2965 } else if (flags
& NFT_SET_MAP
)
2968 if (nla
[NFTA_SET_OBJ_TYPE
] != NULL
) {
2969 if (!(flags
& NFT_SET_OBJECT
))
2972 objtype
= ntohl(nla_get_be32(nla
[NFTA_SET_OBJ_TYPE
]));
2973 if (objtype
== NFT_OBJECT_UNSPEC
||
2974 objtype
> NFT_OBJECT_MAX
)
2976 } else if (flags
& NFT_SET_OBJECT
)
2979 objtype
= NFT_OBJECT_UNSPEC
;
2982 if (nla
[NFTA_SET_TIMEOUT
] != NULL
) {
2983 if (!(flags
& NFT_SET_TIMEOUT
))
2985 timeout
= msecs_to_jiffies(be64_to_cpu(nla_get_be64(
2986 nla
[NFTA_SET_TIMEOUT
])));
2989 if (nla
[NFTA_SET_GC_INTERVAL
] != NULL
) {
2990 if (!(flags
& NFT_SET_TIMEOUT
))
2992 gc_int
= ntohl(nla_get_be32(nla
[NFTA_SET_GC_INTERVAL
]));
2995 policy
= NFT_SET_POL_PERFORMANCE
;
2996 if (nla
[NFTA_SET_POLICY
] != NULL
)
2997 policy
= ntohl(nla_get_be32(nla
[NFTA_SET_POLICY
]));
2999 if (nla
[NFTA_SET_DESC
] != NULL
) {
3000 err
= nf_tables_set_desc_parse(&ctx
, &desc
, nla
[NFTA_SET_DESC
]);
3005 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
3007 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, create
);
3009 return PTR_ERR(afi
);
3011 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
], genmask
);
3013 return PTR_ERR(table
);
3015 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
3017 set
= nf_tables_set_lookup(table
, nla
[NFTA_SET_NAME
], genmask
);
3019 if (PTR_ERR(set
) != -ENOENT
)
3020 return PTR_ERR(set
);
3022 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
3024 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
3029 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
3032 ops
= nft_select_set_ops(nla
, &desc
, policy
);
3034 return PTR_ERR(ops
);
3037 if (nla
[NFTA_SET_USERDATA
])
3038 udlen
= nla_len(nla
[NFTA_SET_USERDATA
]);
3041 if (ops
->privsize
!= NULL
)
3042 size
= ops
->privsize(nla
);
3045 set
= kzalloc(sizeof(*set
) + size
+ udlen
, GFP_KERNEL
);
3049 nla_strlcpy(name
, nla
[NFTA_SET_NAME
], sizeof(set
->name
));
3050 err
= nf_tables_set_alloc_name(&ctx
, set
, name
);
3056 udata
= set
->data
+ size
;
3057 nla_memcpy(udata
, nla
[NFTA_SET_USERDATA
], udlen
);
3060 INIT_LIST_HEAD(&set
->bindings
);
3063 set
->klen
= desc
.klen
;
3065 set
->objtype
= objtype
;
3066 set
->dlen
= desc
.dlen
;
3068 set
->size
= desc
.size
;
3069 set
->policy
= policy
;
3072 set
->timeout
= timeout
;
3073 set
->gc_int
= gc_int
;
3075 err
= ops
->init(set
, &desc
, nla
);
3079 err
= nft_trans_set_add(&ctx
, NFT_MSG_NEWSET
, set
);
3083 list_add_tail_rcu(&set
->list
, &table
->sets
);
3092 module_put(ops
->owner
);
3096 static void nft_set_destroy(struct nft_set
*set
)
3098 set
->ops
->destroy(set
);
3099 module_put(set
->ops
->owner
);
3103 static void nf_tables_set_destroy(const struct nft_ctx
*ctx
, struct nft_set
*set
)
3105 list_del_rcu(&set
->list
);
3106 nf_tables_set_notify(ctx
, set
, NFT_MSG_DELSET
, GFP_ATOMIC
);
3107 nft_set_destroy(set
);
3110 static int nf_tables_delset(struct net
*net
, struct sock
*nlsk
,
3111 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3112 const struct nlattr
* const nla
[])
3114 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3115 u8 genmask
= nft_genmask_next(net
);
3116 struct nft_set
*set
;
3120 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
3121 return -EAFNOSUPPORT
;
3122 if (nla
[NFTA_SET_TABLE
] == NULL
)
3125 err
= nft_ctx_init_from_setattr(&ctx
, net
, skb
, nlh
, nla
, genmask
);
3129 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
], genmask
);
3131 return PTR_ERR(set
);
3132 if (!list_empty(&set
->bindings
))
3135 return nft_delset(&ctx
, set
);
3138 static int nf_tables_bind_check_setelem(const struct nft_ctx
*ctx
,
3139 struct nft_set
*set
,
3140 const struct nft_set_iter
*iter
,
3141 struct nft_set_elem
*elem
)
3143 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
3144 enum nft_registers dreg
;
3146 dreg
= nft_type_to_reg(set
->dtype
);
3147 return nft_validate_register_store(ctx
, dreg
, nft_set_ext_data(ext
),
3148 set
->dtype
== NFT_DATA_VERDICT
?
3149 NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
3153 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
3154 struct nft_set_binding
*binding
)
3156 struct nft_set_binding
*i
;
3157 struct nft_set_iter iter
;
3159 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
)
3162 if (binding
->flags
& NFT_SET_MAP
) {
3163 /* If the set is already bound to the same chain all
3164 * jumps are already validated for that chain.
3166 list_for_each_entry(i
, &set
->bindings
, list
) {
3167 if (i
->flags
& NFT_SET_MAP
&&
3168 i
->chain
== binding
->chain
)
3172 iter
.genmask
= nft_genmask_next(ctx
->net
);
3176 iter
.fn
= nf_tables_bind_check_setelem
;
3178 set
->ops
->walk(ctx
, set
, &iter
);
3183 binding
->chain
= ctx
->chain
;
3184 list_add_tail_rcu(&binding
->list
, &set
->bindings
);
3187 EXPORT_SYMBOL_GPL(nf_tables_bind_set
);
3189 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
3190 struct nft_set_binding
*binding
)
3192 list_del_rcu(&binding
->list
);
3194 if (list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
&&
3195 nft_is_active(ctx
->net
, set
))
3196 nf_tables_set_destroy(ctx
, set
);
3198 EXPORT_SYMBOL_GPL(nf_tables_unbind_set
);
3200 const struct nft_set_ext_type nft_set_ext_types
[] = {
3201 [NFT_SET_EXT_KEY
] = {
3202 .align
= __alignof__(u32
),
3204 [NFT_SET_EXT_DATA
] = {
3205 .align
= __alignof__(u32
),
3207 [NFT_SET_EXT_EXPR
] = {
3208 .align
= __alignof__(struct nft_expr
),
3210 [NFT_SET_EXT_OBJREF
] = {
3211 .len
= sizeof(struct nft_object
*),
3212 .align
= __alignof__(struct nft_object
*),
3214 [NFT_SET_EXT_FLAGS
] = {
3216 .align
= __alignof__(u8
),
3218 [NFT_SET_EXT_TIMEOUT
] = {
3220 .align
= __alignof__(u64
),
3222 [NFT_SET_EXT_EXPIRATION
] = {
3223 .len
= sizeof(unsigned long),
3224 .align
= __alignof__(unsigned long),
3226 [NFT_SET_EXT_USERDATA
] = {
3227 .len
= sizeof(struct nft_userdata
),
3228 .align
= __alignof__(struct nft_userdata
),
3231 EXPORT_SYMBOL_GPL(nft_set_ext_types
);
3237 static const struct nla_policy nft_set_elem_policy
[NFTA_SET_ELEM_MAX
+ 1] = {
3238 [NFTA_SET_ELEM_KEY
] = { .type
= NLA_NESTED
},
3239 [NFTA_SET_ELEM_DATA
] = { .type
= NLA_NESTED
},
3240 [NFTA_SET_ELEM_FLAGS
] = { .type
= NLA_U32
},
3241 [NFTA_SET_ELEM_TIMEOUT
] = { .type
= NLA_U64
},
3242 [NFTA_SET_ELEM_USERDATA
] = { .type
= NLA_BINARY
,
3243 .len
= NFT_USERDATA_MAXLEN
},
3246 static const struct nla_policy nft_set_elem_list_policy
[NFTA_SET_ELEM_LIST_MAX
+ 1] = {
3247 [NFTA_SET_ELEM_LIST_TABLE
] = { .type
= NLA_STRING
,
3248 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
3249 [NFTA_SET_ELEM_LIST_SET
] = { .type
= NLA_STRING
,
3250 .len
= NFT_SET_MAXNAMELEN
- 1 },
3251 [NFTA_SET_ELEM_LIST_ELEMENTS
] = { .type
= NLA_NESTED
},
3252 [NFTA_SET_ELEM_LIST_SET_ID
] = { .type
= NLA_U32
},
3255 static int nft_ctx_init_from_elemattr(struct nft_ctx
*ctx
, struct net
*net
,
3256 const struct sk_buff
*skb
,
3257 const struct nlmsghdr
*nlh
,
3258 const struct nlattr
* const nla
[],
3261 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3262 struct nft_af_info
*afi
;
3263 struct nft_table
*table
;
3265 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, false);
3267 return PTR_ERR(afi
);
3269 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_ELEM_LIST_TABLE
],
3272 return PTR_ERR(table
);
3274 nft_ctx_init(ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
3278 static int nf_tables_fill_setelem(struct sk_buff
*skb
,
3279 const struct nft_set
*set
,
3280 const struct nft_set_elem
*elem
)
3282 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
3283 unsigned char *b
= skb_tail_pointer(skb
);
3284 struct nlattr
*nest
;
3286 nest
= nla_nest_start(skb
, NFTA_LIST_ELEM
);
3288 goto nla_put_failure
;
3290 if (nft_data_dump(skb
, NFTA_SET_ELEM_KEY
, nft_set_ext_key(ext
),
3291 NFT_DATA_VALUE
, set
->klen
) < 0)
3292 goto nla_put_failure
;
3294 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
) &&
3295 nft_data_dump(skb
, NFTA_SET_ELEM_DATA
, nft_set_ext_data(ext
),
3296 set
->dtype
== NFT_DATA_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
3298 goto nla_put_failure
;
3300 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
) &&
3301 nft_expr_dump(skb
, NFTA_SET_ELEM_EXPR
, nft_set_ext_expr(ext
)) < 0)
3302 goto nla_put_failure
;
3304 if (nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
) &&
3305 nla_put_string(skb
, NFTA_SET_ELEM_OBJREF
,
3306 (*nft_set_ext_obj(ext
))->name
) < 0)
3307 goto nla_put_failure
;
3309 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
3310 nla_put_be32(skb
, NFTA_SET_ELEM_FLAGS
,
3311 htonl(*nft_set_ext_flags(ext
))))
3312 goto nla_put_failure
;
3314 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
) &&
3315 nla_put_be64(skb
, NFTA_SET_ELEM_TIMEOUT
,
3316 cpu_to_be64(jiffies_to_msecs(
3317 *nft_set_ext_timeout(ext
))),
3319 goto nla_put_failure
;
3321 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
)) {
3322 unsigned long expires
, now
= jiffies
;
3324 expires
= *nft_set_ext_expiration(ext
);
3325 if (time_before(now
, expires
))
3330 if (nla_put_be64(skb
, NFTA_SET_ELEM_EXPIRATION
,
3331 cpu_to_be64(jiffies_to_msecs(expires
)),
3333 goto nla_put_failure
;
3336 if (nft_set_ext_exists(ext
, NFT_SET_EXT_USERDATA
)) {
3337 struct nft_userdata
*udata
;
3339 udata
= nft_set_ext_userdata(ext
);
3340 if (nla_put(skb
, NFTA_SET_ELEM_USERDATA
,
3341 udata
->len
+ 1, udata
->data
))
3342 goto nla_put_failure
;
3345 nla_nest_end(skb
, nest
);
3353 struct nft_set_dump_args
{
3354 const struct netlink_callback
*cb
;
3355 struct nft_set_iter iter
;
3356 struct sk_buff
*skb
;
3359 static int nf_tables_dump_setelem(const struct nft_ctx
*ctx
,
3360 struct nft_set
*set
,
3361 const struct nft_set_iter
*iter
,
3362 struct nft_set_elem
*elem
)
3364 struct nft_set_dump_args
*args
;
3366 args
= container_of(iter
, struct nft_set_dump_args
, iter
);
3367 return nf_tables_fill_setelem(args
->skb
, set
, elem
);
3370 static int nf_tables_dump_set(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3372 struct net
*net
= sock_net(skb
->sk
);
3373 u8 genmask
= nft_genmask_cur(net
);
3374 struct nft_set
*set
;
3375 struct nft_set_dump_args args
;
3377 struct nlattr
*nla
[NFTA_SET_ELEM_LIST_MAX
+ 1];
3378 struct nfgenmsg
*nfmsg
;
3379 struct nlmsghdr
*nlh
;
3380 struct nlattr
*nest
;
3384 err
= nlmsg_parse(cb
->nlh
, sizeof(struct nfgenmsg
), nla
,
3385 NFTA_SET_ELEM_LIST_MAX
, nft_set_elem_list_policy
,
3390 err
= nft_ctx_init_from_elemattr(&ctx
, net
, cb
->skb
, cb
->nlh
,
3391 (void *)nla
, genmask
);
3395 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
],
3398 return PTR_ERR(set
);
3400 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, NFT_MSG_NEWSETELEM
);
3401 portid
= NETLINK_CB(cb
->skb
).portid
;
3402 seq
= cb
->nlh
->nlmsg_seq
;
3404 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
3407 goto nla_put_failure
;
3409 nfmsg
= nlmsg_data(nlh
);
3410 nfmsg
->nfgen_family
= ctx
.afi
->family
;
3411 nfmsg
->version
= NFNETLINK_V0
;
3412 nfmsg
->res_id
= htons(ctx
.net
->nft
.base_seq
& 0xffff);
3414 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_TABLE
, ctx
.table
->name
))
3415 goto nla_put_failure
;
3416 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_SET
, set
->name
))
3417 goto nla_put_failure
;
3419 nest
= nla_nest_start(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
3421 goto nla_put_failure
;
3425 args
.iter
.genmask
= nft_genmask_cur(ctx
.net
);
3426 args
.iter
.skip
= cb
->args
[0];
3427 args
.iter
.count
= 0;
3429 args
.iter
.fn
= nf_tables_dump_setelem
;
3430 set
->ops
->walk(&ctx
, set
, &args
.iter
);
3432 nla_nest_end(skb
, nest
);
3433 nlmsg_end(skb
, nlh
);
3435 if (args
.iter
.err
&& args
.iter
.err
!= -EMSGSIZE
)
3436 return args
.iter
.err
;
3437 if (args
.iter
.count
== cb
->args
[0])
3440 cb
->args
[0] = args
.iter
.count
;
3447 static int nf_tables_getsetelem(struct net
*net
, struct sock
*nlsk
,
3448 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3449 const struct nlattr
* const nla
[])
3451 u8 genmask
= nft_genmask_cur(net
);
3452 const struct nft_set
*set
;
3456 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
, genmask
);
3460 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
],
3463 return PTR_ERR(set
);
3465 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3466 struct netlink_dump_control c
= {
3467 .dump
= nf_tables_dump_set
,
3469 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
3474 static int nf_tables_fill_setelem_info(struct sk_buff
*skb
,
3475 const struct nft_ctx
*ctx
, u32 seq
,
3476 u32 portid
, int event
, u16 flags
,
3477 const struct nft_set
*set
,
3478 const struct nft_set_elem
*elem
)
3480 struct nfgenmsg
*nfmsg
;
3481 struct nlmsghdr
*nlh
;
3482 struct nlattr
*nest
;
3485 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
3486 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
3489 goto nla_put_failure
;
3491 nfmsg
= nlmsg_data(nlh
);
3492 nfmsg
->nfgen_family
= ctx
->afi
->family
;
3493 nfmsg
->version
= NFNETLINK_V0
;
3494 nfmsg
->res_id
= htons(ctx
->net
->nft
.base_seq
& 0xffff);
3496 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
3497 goto nla_put_failure
;
3498 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
3499 goto nla_put_failure
;
3501 nest
= nla_nest_start(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
3503 goto nla_put_failure
;
3505 err
= nf_tables_fill_setelem(skb
, set
, elem
);
3507 goto nla_put_failure
;
3509 nla_nest_end(skb
, nest
);
3511 nlmsg_end(skb
, nlh
);
3515 nlmsg_trim(skb
, nlh
);
3519 static void nf_tables_setelem_notify(const struct nft_ctx
*ctx
,
3520 const struct nft_set
*set
,
3521 const struct nft_set_elem
*elem
,
3522 int event
, u16 flags
)
3524 struct net
*net
= ctx
->net
;
3525 u32 portid
= ctx
->portid
;
3526 struct sk_buff
*skb
;
3529 if (!ctx
->report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
3532 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
3536 err
= nf_tables_fill_setelem_info(skb
, ctx
, 0, portid
, event
, flags
,
3543 nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, ctx
->report
,
3547 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
3550 static struct nft_trans
*nft_trans_elem_alloc(struct nft_ctx
*ctx
,
3552 struct nft_set
*set
)
3554 struct nft_trans
*trans
;
3556 trans
= nft_trans_alloc(ctx
, msg_type
, sizeof(struct nft_trans_elem
));
3560 nft_trans_elem_set(trans
) = set
;
3564 void *nft_set_elem_init(const struct nft_set
*set
,
3565 const struct nft_set_ext_tmpl
*tmpl
,
3566 const u32
*key
, const u32
*data
,
3567 u64 timeout
, gfp_t gfp
)
3569 struct nft_set_ext
*ext
;
3572 elem
= kzalloc(set
->ops
->elemsize
+ tmpl
->len
, gfp
);
3576 ext
= nft_set_elem_ext(set
, elem
);
3577 nft_set_ext_init(ext
, tmpl
);
3579 memcpy(nft_set_ext_key(ext
), key
, set
->klen
);
3580 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
3581 memcpy(nft_set_ext_data(ext
), data
, set
->dlen
);
3582 if (nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
))
3583 *nft_set_ext_expiration(ext
) =
3585 if (nft_set_ext_exists(ext
, NFT_SET_EXT_TIMEOUT
))
3586 *nft_set_ext_timeout(ext
) = timeout
;
3591 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
,
3594 struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
);
3596 nft_data_uninit(nft_set_ext_key(ext
), NFT_DATA_VALUE
);
3597 if (nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
))
3598 nft_data_uninit(nft_set_ext_data(ext
), set
->dtype
);
3599 if (destroy_expr
&& nft_set_ext_exists(ext
, NFT_SET_EXT_EXPR
))
3600 nf_tables_expr_destroy(NULL
, nft_set_ext_expr(ext
));
3601 if (nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
))
3602 (*nft_set_ext_obj(ext
))->use
--;
3605 EXPORT_SYMBOL_GPL(nft_set_elem_destroy
);
3607 static int nft_setelem_parse_flags(const struct nft_set
*set
,
3608 const struct nlattr
*attr
, u32
*flags
)
3613 *flags
= ntohl(nla_get_be32(attr
));
3614 if (*flags
& ~NFT_SET_ELEM_INTERVAL_END
)
3616 if (!(set
->flags
& NFT_SET_INTERVAL
) &&
3617 *flags
& NFT_SET_ELEM_INTERVAL_END
)
3623 static int nft_add_set_elem(struct nft_ctx
*ctx
, struct nft_set
*set
,
3624 const struct nlattr
*attr
, u32 nlmsg_flags
)
3626 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
3627 u8 genmask
= nft_genmask_next(ctx
->net
);
3628 struct nft_data_desc d1
, d2
;
3629 struct nft_set_ext_tmpl tmpl
;
3630 struct nft_set_ext
*ext
, *ext2
;
3631 struct nft_set_elem elem
;
3632 struct nft_set_binding
*binding
;
3633 struct nft_object
*obj
= NULL
;
3634 struct nft_userdata
*udata
;
3635 struct nft_data data
;
3636 enum nft_registers dreg
;
3637 struct nft_trans
*trans
;
3643 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
3644 nft_set_elem_policy
, NULL
);
3648 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
3651 nft_set_ext_prepare(&tmpl
);
3653 err
= nft_setelem_parse_flags(set
, nla
[NFTA_SET_ELEM_FLAGS
], &flags
);
3657 nft_set_ext_add(&tmpl
, NFT_SET_EXT_FLAGS
);
3659 if (set
->flags
& NFT_SET_MAP
) {
3660 if (nla
[NFTA_SET_ELEM_DATA
] == NULL
&&
3661 !(flags
& NFT_SET_ELEM_INTERVAL_END
))
3663 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
&&
3664 flags
& NFT_SET_ELEM_INTERVAL_END
)
3667 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
3672 if (nla
[NFTA_SET_ELEM_TIMEOUT
] != NULL
) {
3673 if (!(set
->flags
& NFT_SET_TIMEOUT
))
3675 timeout
= msecs_to_jiffies(be64_to_cpu(nla_get_be64(
3676 nla
[NFTA_SET_ELEM_TIMEOUT
])));
3677 } else if (set
->flags
& NFT_SET_TIMEOUT
) {
3678 timeout
= set
->timeout
;
3681 err
= nft_data_init(ctx
, &elem
.key
.val
, sizeof(elem
.key
), &d1
,
3682 nla
[NFTA_SET_ELEM_KEY
]);
3686 if (d1
.type
!= NFT_DATA_VALUE
|| d1
.len
!= set
->klen
)
3689 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_KEY
, d1
.len
);
3691 nft_set_ext_add(&tmpl
, NFT_SET_EXT_EXPIRATION
);
3692 if (timeout
!= set
->timeout
)
3693 nft_set_ext_add(&tmpl
, NFT_SET_EXT_TIMEOUT
);
3696 if (nla
[NFTA_SET_ELEM_OBJREF
] != NULL
) {
3697 if (!(set
->flags
& NFT_SET_OBJECT
)) {
3701 obj
= nf_tables_obj_lookup(ctx
->table
, nla
[NFTA_SET_ELEM_OBJREF
],
3702 set
->objtype
, genmask
);
3707 nft_set_ext_add(&tmpl
, NFT_SET_EXT_OBJREF
);
3710 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
) {
3711 err
= nft_data_init(ctx
, &data
, sizeof(data
), &d2
,
3712 nla
[NFTA_SET_ELEM_DATA
]);
3717 if (set
->dtype
!= NFT_DATA_VERDICT
&& d2
.len
!= set
->dlen
)
3720 dreg
= nft_type_to_reg(set
->dtype
);
3721 list_for_each_entry(binding
, &set
->bindings
, list
) {
3722 struct nft_ctx bind_ctx
= {
3725 .table
= ctx
->table
,
3726 .chain
= (struct nft_chain
*)binding
->chain
,
3729 if (!(binding
->flags
& NFT_SET_MAP
))
3732 err
= nft_validate_register_store(&bind_ctx
, dreg
,
3739 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_DATA
, d2
.len
);
3742 /* The full maximum length of userdata can exceed the maximum
3743 * offset value (U8_MAX) for following extensions, therefor it
3744 * must be the last extension added.
3747 if (nla
[NFTA_SET_ELEM_USERDATA
] != NULL
) {
3748 ulen
= nla_len(nla
[NFTA_SET_ELEM_USERDATA
]);
3750 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_USERDATA
,
3755 elem
.priv
= nft_set_elem_init(set
, &tmpl
, elem
.key
.val
.data
, data
.data
,
3756 timeout
, GFP_KERNEL
);
3757 if (elem
.priv
== NULL
)
3760 ext
= nft_set_elem_ext(set
, elem
.priv
);
3762 *nft_set_ext_flags(ext
) = flags
;
3764 udata
= nft_set_ext_userdata(ext
);
3765 udata
->len
= ulen
- 1;
3766 nla_memcpy(&udata
->data
, nla
[NFTA_SET_ELEM_USERDATA
], ulen
);
3769 *nft_set_ext_obj(ext
) = obj
;
3773 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_NEWSETELEM
, set
);
3777 ext
->genmask
= nft_genmask_cur(ctx
->net
) | NFT_SET_ELEM_BUSY_MASK
;
3778 err
= set
->ops
->insert(ctx
->net
, set
, &elem
, &ext2
);
3780 if (err
== -EEXIST
) {
3781 if ((nft_set_ext_exists(ext
, NFT_SET_EXT_DATA
) &&
3782 nft_set_ext_exists(ext2
, NFT_SET_EXT_DATA
) &&
3783 memcmp(nft_set_ext_data(ext
),
3784 nft_set_ext_data(ext2
), set
->dlen
) != 0) ||
3785 (nft_set_ext_exists(ext
, NFT_SET_EXT_OBJREF
) &&
3786 nft_set_ext_exists(ext2
, NFT_SET_EXT_OBJREF
) &&
3787 *nft_set_ext_obj(ext
) != *nft_set_ext_obj(ext2
)))
3789 else if (!(nlmsg_flags
& NLM_F_EXCL
))
3796 !atomic_add_unless(&set
->nelems
, 1, set
->size
+ set
->ndeact
)) {
3801 nft_trans_elem(trans
) = elem
;
3802 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
3806 set
->ops
->remove(ctx
->net
, set
, &elem
);
3812 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
3813 nft_data_uninit(&data
, d2
.type
);
3815 nft_data_uninit(&elem
.key
.val
, d1
.type
);
3820 static int nf_tables_newsetelem(struct net
*net
, struct sock
*nlsk
,
3821 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3822 const struct nlattr
* const nla
[])
3824 u8 genmask
= nft_genmask_next(net
);
3825 const struct nlattr
*attr
;
3826 struct nft_set
*set
;
3830 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
)
3833 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
, genmask
);
3837 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
],
3840 if (nla
[NFTA_SET_ELEM_LIST_SET_ID
]) {
3841 set
= nf_tables_set_lookup_byid(net
,
3842 nla
[NFTA_SET_ELEM_LIST_SET_ID
],
3846 return PTR_ERR(set
);
3849 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
3852 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
3853 err
= nft_add_set_elem(&ctx
, set
, attr
, nlh
->nlmsg_flags
);
3860 static int nft_del_setelem(struct nft_ctx
*ctx
, struct nft_set
*set
,
3861 const struct nlattr
*attr
)
3863 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
3864 struct nft_set_ext_tmpl tmpl
;
3865 struct nft_data_desc desc
;
3866 struct nft_set_elem elem
;
3867 struct nft_set_ext
*ext
;
3868 struct nft_trans
*trans
;
3873 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
3874 nft_set_elem_policy
, NULL
);
3879 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
3882 nft_set_ext_prepare(&tmpl
);
3884 err
= nft_setelem_parse_flags(set
, nla
[NFTA_SET_ELEM_FLAGS
], &flags
);
3888 nft_set_ext_add(&tmpl
, NFT_SET_EXT_FLAGS
);
3890 err
= nft_data_init(ctx
, &elem
.key
.val
, sizeof(elem
.key
), &desc
,
3891 nla
[NFTA_SET_ELEM_KEY
]);
3896 if (desc
.type
!= NFT_DATA_VALUE
|| desc
.len
!= set
->klen
)
3899 nft_set_ext_add_length(&tmpl
, NFT_SET_EXT_KEY
, desc
.len
);
3902 elem
.priv
= nft_set_elem_init(set
, &tmpl
, elem
.key
.val
.data
, NULL
, 0,
3904 if (elem
.priv
== NULL
)
3907 ext
= nft_set_elem_ext(set
, elem
.priv
);
3909 *nft_set_ext_flags(ext
) = flags
;
3911 trans
= nft_trans_elem_alloc(ctx
, NFT_MSG_DELSETELEM
, set
);
3912 if (trans
== NULL
) {
3917 priv
= set
->ops
->deactivate(ctx
->net
, set
, &elem
);
3925 nft_trans_elem(trans
) = elem
;
3926 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
3934 nft_data_uninit(&elem
.key
.val
, desc
.type
);
3939 static int nft_flush_set(const struct nft_ctx
*ctx
,
3940 struct nft_set
*set
,
3941 const struct nft_set_iter
*iter
,
3942 struct nft_set_elem
*elem
)
3944 struct nft_trans
*trans
;
3947 trans
= nft_trans_alloc_gfp(ctx
, NFT_MSG_DELSETELEM
,
3948 sizeof(struct nft_trans_elem
), GFP_ATOMIC
);
3952 if (!set
->ops
->flush(ctx
->net
, set
, elem
->priv
)) {
3958 nft_trans_elem_set(trans
) = set
;
3959 nft_trans_elem(trans
) = *elem
;
3960 list_add_tail(&trans
->list
, &ctx
->net
->nft
.commit_list
);
3968 static int nf_tables_delsetelem(struct net
*net
, struct sock
*nlsk
,
3969 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
3970 const struct nlattr
* const nla
[])
3972 u8 genmask
= nft_genmask_next(net
);
3973 const struct nlattr
*attr
;
3974 struct nft_set
*set
;
3978 err
= nft_ctx_init_from_elemattr(&ctx
, net
, skb
, nlh
, nla
, genmask
);
3982 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
],
3985 return PTR_ERR(set
);
3986 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
3989 if (nla
[NFTA_SET_ELEM_LIST_ELEMENTS
] == NULL
) {
3990 struct nft_set_iter iter
= {
3992 .fn
= nft_flush_set
,
3994 set
->ops
->walk(&ctx
, set
, &iter
);
3999 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
4000 err
= nft_del_setelem(&ctx
, set
, attr
);
4009 void nft_set_gc_batch_release(struct rcu_head
*rcu
)
4011 struct nft_set_gc_batch
*gcb
;
4014 gcb
= container_of(rcu
, struct nft_set_gc_batch
, head
.rcu
);
4015 for (i
= 0; i
< gcb
->head
.cnt
; i
++)
4016 nft_set_elem_destroy(gcb
->head
.set
, gcb
->elems
[i
], true);
4019 EXPORT_SYMBOL_GPL(nft_set_gc_batch_release
);
4021 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
4024 struct nft_set_gc_batch
*gcb
;
4026 gcb
= kzalloc(sizeof(*gcb
), gfp
);
4029 gcb
->head
.set
= set
;
4032 EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc
);
4039 * nft_register_obj- register nf_tables stateful object type
4042 * Registers the object type for use with nf_tables. Returns zero on
4043 * success or a negative errno code otherwise.
4045 int nft_register_obj(struct nft_object_type
*obj_type
)
4047 if (obj_type
->type
== NFT_OBJECT_UNSPEC
)
4050 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
4051 list_add_rcu(&obj_type
->list
, &nf_tables_objects
);
4052 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
4055 EXPORT_SYMBOL_GPL(nft_register_obj
);
4058 * nft_unregister_obj - unregister nf_tables object type
4061 * Unregisters the object type for use with nf_tables.
4063 void nft_unregister_obj(struct nft_object_type
*obj_type
)
4065 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
4066 list_del_rcu(&obj_type
->list
);
4067 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
4069 EXPORT_SYMBOL_GPL(nft_unregister_obj
);
4071 struct nft_object
*nf_tables_obj_lookup(const struct nft_table
*table
,
4072 const struct nlattr
*nla
,
4073 u32 objtype
, u8 genmask
)
4075 struct nft_object
*obj
;
4077 list_for_each_entry(obj
, &table
->objects
, list
) {
4078 if (!nla_strcmp(nla
, obj
->name
) &&
4079 objtype
== obj
->type
->type
&&
4080 nft_active_genmask(obj
, genmask
))
4083 return ERR_PTR(-ENOENT
);
4085 EXPORT_SYMBOL_GPL(nf_tables_obj_lookup
);
4087 static const struct nla_policy nft_obj_policy
[NFTA_OBJ_MAX
+ 1] = {
4088 [NFTA_OBJ_TABLE
] = { .type
= NLA_STRING
,
4089 .len
= NFT_TABLE_MAXNAMELEN
- 1 },
4090 [NFTA_OBJ_NAME
] = { .type
= NLA_STRING
,
4091 .len
= NFT_OBJ_MAXNAMELEN
- 1 },
4092 [NFTA_OBJ_TYPE
] = { .type
= NLA_U32
},
4093 [NFTA_OBJ_DATA
] = { .type
= NLA_NESTED
},
4096 static struct nft_object
*nft_obj_init(const struct nft_ctx
*ctx
,
4097 const struct nft_object_type
*type
,
4098 const struct nlattr
*attr
)
4100 struct nlattr
*tb
[type
->maxattr
+ 1];
4101 struct nft_object
*obj
;
4105 err
= nla_parse_nested(tb
, type
->maxattr
, attr
, type
->policy
,
4110 memset(tb
, 0, sizeof(tb
[0]) * (type
->maxattr
+ 1));
4114 obj
= kzalloc(sizeof(struct nft_object
) + type
->size
, GFP_KERNEL
);
4118 err
= type
->init(ctx
, (const struct nlattr
* const *)tb
, obj
);
4127 return ERR_PTR(err
);
4130 static int nft_object_dump(struct sk_buff
*skb
, unsigned int attr
,
4131 struct nft_object
*obj
, bool reset
)
4133 struct nlattr
*nest
;
4135 nest
= nla_nest_start(skb
, attr
);
4137 goto nla_put_failure
;
4138 if (obj
->type
->dump(skb
, obj
, reset
) < 0)
4139 goto nla_put_failure
;
4140 nla_nest_end(skb
, nest
);
4147 static const struct nft_object_type
*__nft_obj_type_get(u32 objtype
)
4149 const struct nft_object_type
*type
;
4151 list_for_each_entry(type
, &nf_tables_objects
, list
) {
4152 if (objtype
== type
->type
)
4158 static const struct nft_object_type
*nft_obj_type_get(u32 objtype
)
4160 const struct nft_object_type
*type
;
4162 type
= __nft_obj_type_get(objtype
);
4163 if (type
!= NULL
&& try_module_get(type
->owner
))
4166 #ifdef CONFIG_MODULES
4168 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
4169 request_module("nft-obj-%u", objtype
);
4170 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
4171 if (__nft_obj_type_get(objtype
))
4172 return ERR_PTR(-EAGAIN
);
4175 return ERR_PTR(-ENOENT
);
4178 static int nf_tables_newobj(struct net
*net
, struct sock
*nlsk
,
4179 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
4180 const struct nlattr
* const nla
[])
4182 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
4183 const struct nft_object_type
*type
;
4184 u8 genmask
= nft_genmask_next(net
);
4185 int family
= nfmsg
->nfgen_family
;
4186 struct nft_af_info
*afi
;
4187 struct nft_table
*table
;
4188 struct nft_object
*obj
;
4193 if (!nla
[NFTA_OBJ_TYPE
] ||
4194 !nla
[NFTA_OBJ_NAME
] ||
4195 !nla
[NFTA_OBJ_DATA
])
4198 afi
= nf_tables_afinfo_lookup(net
, family
, true);
4200 return PTR_ERR(afi
);
4202 table
= nf_tables_table_lookup(afi
, nla
[NFTA_OBJ_TABLE
], genmask
);
4204 return PTR_ERR(table
);
4206 objtype
= ntohl(nla_get_be32(nla
[NFTA_OBJ_TYPE
]));
4207 obj
= nf_tables_obj_lookup(table
, nla
[NFTA_OBJ_NAME
], objtype
, genmask
);
4214 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
4220 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
4222 type
= nft_obj_type_get(objtype
);
4224 return PTR_ERR(type
);
4226 obj
= nft_obj_init(&ctx
, type
, nla
[NFTA_OBJ_DATA
]);
4232 nla_strlcpy(obj
->name
, nla
[NFTA_OBJ_NAME
], NFT_OBJ_MAXNAMELEN
);
4234 err
= nft_trans_obj_add(&ctx
, NFT_MSG_NEWOBJ
, obj
);
4238 list_add_tail_rcu(&obj
->list
, &table
->objects
);
4242 if (obj
->type
->destroy
)
4243 obj
->type
->destroy(obj
);
4246 module_put(type
->owner
);
4250 static int nf_tables_fill_obj_info(struct sk_buff
*skb
, struct net
*net
,
4251 u32 portid
, u32 seq
, int event
, u32 flags
,
4252 int family
, const struct nft_table
*table
,
4253 struct nft_object
*obj
, bool reset
)
4255 struct nfgenmsg
*nfmsg
;
4256 struct nlmsghdr
*nlh
;
4258 event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, event
);
4259 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
4261 goto nla_put_failure
;
4263 nfmsg
= nlmsg_data(nlh
);
4264 nfmsg
->nfgen_family
= family
;
4265 nfmsg
->version
= NFNETLINK_V0
;
4266 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
4268 if (nla_put_string(skb
, NFTA_OBJ_TABLE
, table
->name
) ||
4269 nla_put_string(skb
, NFTA_OBJ_NAME
, obj
->name
) ||
4270 nla_put_be32(skb
, NFTA_OBJ_TYPE
, htonl(obj
->type
->type
)) ||
4271 nla_put_be32(skb
, NFTA_OBJ_USE
, htonl(obj
->use
)) ||
4272 nft_object_dump(skb
, NFTA_OBJ_DATA
, obj
, reset
))
4273 goto nla_put_failure
;
4275 nlmsg_end(skb
, nlh
);
4279 nlmsg_trim(skb
, nlh
);
4283 struct nft_obj_filter
{
4284 char table
[NFT_OBJ_MAXNAMELEN
];
4288 static int nf_tables_dump_obj(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4290 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
4291 const struct nft_af_info
*afi
;
4292 const struct nft_table
*table
;
4293 unsigned int idx
= 0, s_idx
= cb
->args
[0];
4294 struct nft_obj_filter
*filter
= cb
->data
;
4295 struct net
*net
= sock_net(skb
->sk
);
4296 int family
= nfmsg
->nfgen_family
;
4297 struct nft_object
*obj
;
4300 if (NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
) == NFT_MSG_GETOBJ_RESET
)
4304 cb
->seq
= net
->nft
.base_seq
;
4306 list_for_each_entry_rcu(afi
, &net
->nft
.af_info
, list
) {
4307 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
4310 list_for_each_entry_rcu(table
, &afi
->tables
, list
) {
4311 list_for_each_entry_rcu(obj
, &table
->objects
, list
) {
4312 if (!nft_is_active(net
, obj
))
4317 memset(&cb
->args
[1], 0,
4318 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
4319 if (filter
&& filter
->table
[0] &&
4320 strcmp(filter
->table
, table
->name
))
4323 filter
->type
!= NFT_OBJECT_UNSPEC
&&
4324 obj
->type
->type
!= filter
->type
)
4327 if (nf_tables_fill_obj_info(skb
, net
, NETLINK_CB(cb
->skb
).portid
,
4330 NLM_F_MULTI
| NLM_F_APPEND
,
4331 afi
->family
, table
, obj
, reset
) < 0)
4334 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
4347 static int nf_tables_dump_obj_done(struct netlink_callback
*cb
)
4354 static struct nft_obj_filter
*
4355 nft_obj_filter_alloc(const struct nlattr
* const nla
[])
4357 struct nft_obj_filter
*filter
;
4359 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
4361 return ERR_PTR(-ENOMEM
);
4363 if (nla
[NFTA_OBJ_TABLE
])
4364 nla_strlcpy(filter
->table
, nla
[NFTA_OBJ_TABLE
],
4365 NFT_TABLE_MAXNAMELEN
);
4366 if (nla
[NFTA_OBJ_TYPE
])
4367 filter
->type
= ntohl(nla_get_be32(nla
[NFTA_OBJ_TYPE
]));
4372 static int nf_tables_getobj(struct net
*net
, struct sock
*nlsk
,
4373 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
4374 const struct nlattr
* const nla
[])
4376 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
4377 u8 genmask
= nft_genmask_cur(net
);
4378 int family
= nfmsg
->nfgen_family
;
4379 const struct nft_af_info
*afi
;
4380 const struct nft_table
*table
;
4381 struct nft_object
*obj
;
4382 struct sk_buff
*skb2
;
4387 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
4388 struct netlink_dump_control c
= {
4389 .dump
= nf_tables_dump_obj
,
4390 .done
= nf_tables_dump_obj_done
,
4393 if (nla
[NFTA_OBJ_TABLE
] ||
4394 nla
[NFTA_OBJ_TYPE
]) {
4395 struct nft_obj_filter
*filter
;
4397 filter
= nft_obj_filter_alloc(nla
);
4403 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
4406 if (!nla
[NFTA_OBJ_NAME
] ||
4407 !nla
[NFTA_OBJ_TYPE
])
4410 afi
= nf_tables_afinfo_lookup(net
, family
, false);
4412 return PTR_ERR(afi
);
4414 table
= nf_tables_table_lookup(afi
, nla
[NFTA_OBJ_TABLE
], genmask
);
4416 return PTR_ERR(table
);
4418 objtype
= ntohl(nla_get_be32(nla
[NFTA_OBJ_TYPE
]));
4419 obj
= nf_tables_obj_lookup(table
, nla
[NFTA_OBJ_NAME
], objtype
, genmask
);
4421 return PTR_ERR(obj
);
4423 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
4427 if (NFNL_MSG_TYPE(nlh
->nlmsg_type
) == NFT_MSG_GETOBJ_RESET
)
4430 err
= nf_tables_fill_obj_info(skb2
, net
, NETLINK_CB(skb
).portid
,
4431 nlh
->nlmsg_seq
, NFT_MSG_NEWOBJ
, 0,
4432 family
, table
, obj
, reset
);
4436 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
4442 static void nft_obj_destroy(struct nft_object
*obj
)
4444 if (obj
->type
->destroy
)
4445 obj
->type
->destroy(obj
);
4447 module_put(obj
->type
->owner
);
4451 static int nf_tables_delobj(struct net
*net
, struct sock
*nlsk
,
4452 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
4453 const struct nlattr
* const nla
[])
4455 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
4456 u8 genmask
= nft_genmask_next(net
);
4457 int family
= nfmsg
->nfgen_family
;
4458 struct nft_af_info
*afi
;
4459 struct nft_table
*table
;
4460 struct nft_object
*obj
;
4464 if (!nla
[NFTA_OBJ_TYPE
] ||
4465 !nla
[NFTA_OBJ_NAME
])
4468 afi
= nf_tables_afinfo_lookup(net
, family
, true);
4470 return PTR_ERR(afi
);
4472 table
= nf_tables_table_lookup(afi
, nla
[NFTA_OBJ_TABLE
], genmask
);
4474 return PTR_ERR(table
);
4476 objtype
= ntohl(nla_get_be32(nla
[NFTA_OBJ_TYPE
]));
4477 obj
= nf_tables_obj_lookup(table
, nla
[NFTA_OBJ_NAME
], objtype
, genmask
);
4479 return PTR_ERR(obj
);
4483 nft_ctx_init(&ctx
, net
, skb
, nlh
, afi
, table
, NULL
, nla
);
4485 return nft_delobj(&ctx
, obj
);
4488 void nft_obj_notify(struct net
*net
, struct nft_table
*table
,
4489 struct nft_object
*obj
, u32 portid
, u32 seq
, int event
,
4490 int family
, int report
, gfp_t gfp
)
4492 struct sk_buff
*skb
;
4496 !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
4499 skb
= nlmsg_new(NLMSG_GOODSIZE
, gfp
);
4503 err
= nf_tables_fill_obj_info(skb
, net
, portid
, seq
, event
, 0, family
,
4510 nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, report
, gfp
);
4513 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, -ENOBUFS
);
4515 EXPORT_SYMBOL_GPL(nft_obj_notify
);
4517 static void nf_tables_obj_notify(const struct nft_ctx
*ctx
,
4518 struct nft_object
*obj
, int event
)
4520 nft_obj_notify(ctx
->net
, ctx
->table
, obj
, ctx
->portid
, ctx
->seq
, event
,
4521 ctx
->afi
->family
, ctx
->report
, GFP_KERNEL
);
4524 static int nf_tables_fill_gen_info(struct sk_buff
*skb
, struct net
*net
,
4525 u32 portid
, u32 seq
)
4527 struct nlmsghdr
*nlh
;
4528 struct nfgenmsg
*nfmsg
;
4529 int event
= nfnl_msg_type(NFNL_SUBSYS_NFTABLES
, NFT_MSG_NEWGEN
);
4531 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), 0);
4533 goto nla_put_failure
;
4535 nfmsg
= nlmsg_data(nlh
);
4536 nfmsg
->nfgen_family
= AF_UNSPEC
;
4537 nfmsg
->version
= NFNETLINK_V0
;
4538 nfmsg
->res_id
= htons(net
->nft
.base_seq
& 0xffff);
4540 if (nla_put_be32(skb
, NFTA_GEN_ID
, htonl(net
->nft
.base_seq
)))
4541 goto nla_put_failure
;
4543 nlmsg_end(skb
, nlh
);
4547 nlmsg_trim(skb
, nlh
);
4551 static void nf_tables_gen_notify(struct net
*net
, struct sk_buff
*skb
,
4554 struct nlmsghdr
*nlh
= nlmsg_hdr(skb
);
4555 struct sk_buff
*skb2
;
4558 if (nlmsg_report(nlh
) &&
4559 !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
4562 skb2
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
4566 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
4573 nfnetlink_send(skb2
, net
, NETLINK_CB(skb
).portid
, NFNLGRP_NFTABLES
,
4574 nlmsg_report(nlh
), GFP_KERNEL
);
4577 nfnetlink_set_err(net
, NETLINK_CB(skb
).portid
, NFNLGRP_NFTABLES
,
4581 static int nf_tables_getgen(struct net
*net
, struct sock
*nlsk
,
4582 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
4583 const struct nlattr
* const nla
[])
4585 struct sk_buff
*skb2
;
4588 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
4592 err
= nf_tables_fill_gen_info(skb2
, net
, NETLINK_CB(skb
).portid
,
4597 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
4603 static const struct nfnl_callback nf_tables_cb
[NFT_MSG_MAX
] = {
4604 [NFT_MSG_NEWTABLE
] = {
4605 .call_batch
= nf_tables_newtable
,
4606 .attr_count
= NFTA_TABLE_MAX
,
4607 .policy
= nft_table_policy
,
4609 [NFT_MSG_GETTABLE
] = {
4610 .call
= nf_tables_gettable
,
4611 .attr_count
= NFTA_TABLE_MAX
,
4612 .policy
= nft_table_policy
,
4614 [NFT_MSG_DELTABLE
] = {
4615 .call_batch
= nf_tables_deltable
,
4616 .attr_count
= NFTA_TABLE_MAX
,
4617 .policy
= nft_table_policy
,
4619 [NFT_MSG_NEWCHAIN
] = {
4620 .call_batch
= nf_tables_newchain
,
4621 .attr_count
= NFTA_CHAIN_MAX
,
4622 .policy
= nft_chain_policy
,
4624 [NFT_MSG_GETCHAIN
] = {
4625 .call
= nf_tables_getchain
,
4626 .attr_count
= NFTA_CHAIN_MAX
,
4627 .policy
= nft_chain_policy
,
4629 [NFT_MSG_DELCHAIN
] = {
4630 .call_batch
= nf_tables_delchain
,
4631 .attr_count
= NFTA_CHAIN_MAX
,
4632 .policy
= nft_chain_policy
,
4634 [NFT_MSG_NEWRULE
] = {
4635 .call_batch
= nf_tables_newrule
,
4636 .attr_count
= NFTA_RULE_MAX
,
4637 .policy
= nft_rule_policy
,
4639 [NFT_MSG_GETRULE
] = {
4640 .call
= nf_tables_getrule
,
4641 .attr_count
= NFTA_RULE_MAX
,
4642 .policy
= nft_rule_policy
,
4644 [NFT_MSG_DELRULE
] = {
4645 .call_batch
= nf_tables_delrule
,
4646 .attr_count
= NFTA_RULE_MAX
,
4647 .policy
= nft_rule_policy
,
4649 [NFT_MSG_NEWSET
] = {
4650 .call_batch
= nf_tables_newset
,
4651 .attr_count
= NFTA_SET_MAX
,
4652 .policy
= nft_set_policy
,
4654 [NFT_MSG_GETSET
] = {
4655 .call
= nf_tables_getset
,
4656 .attr_count
= NFTA_SET_MAX
,
4657 .policy
= nft_set_policy
,
4659 [NFT_MSG_DELSET
] = {
4660 .call_batch
= nf_tables_delset
,
4661 .attr_count
= NFTA_SET_MAX
,
4662 .policy
= nft_set_policy
,
4664 [NFT_MSG_NEWSETELEM
] = {
4665 .call_batch
= nf_tables_newsetelem
,
4666 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
4667 .policy
= nft_set_elem_list_policy
,
4669 [NFT_MSG_GETSETELEM
] = {
4670 .call
= nf_tables_getsetelem
,
4671 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
4672 .policy
= nft_set_elem_list_policy
,
4674 [NFT_MSG_DELSETELEM
] = {
4675 .call_batch
= nf_tables_delsetelem
,
4676 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
4677 .policy
= nft_set_elem_list_policy
,
4679 [NFT_MSG_GETGEN
] = {
4680 .call
= nf_tables_getgen
,
4682 [NFT_MSG_NEWOBJ
] = {
4683 .call_batch
= nf_tables_newobj
,
4684 .attr_count
= NFTA_OBJ_MAX
,
4685 .policy
= nft_obj_policy
,
4687 [NFT_MSG_GETOBJ
] = {
4688 .call
= nf_tables_getobj
,
4689 .attr_count
= NFTA_OBJ_MAX
,
4690 .policy
= nft_obj_policy
,
4692 [NFT_MSG_DELOBJ
] = {
4693 .call_batch
= nf_tables_delobj
,
4694 .attr_count
= NFTA_OBJ_MAX
,
4695 .policy
= nft_obj_policy
,
4697 [NFT_MSG_GETOBJ_RESET
] = {
4698 .call
= nf_tables_getobj
,
4699 .attr_count
= NFTA_OBJ_MAX
,
4700 .policy
= nft_obj_policy
,
4704 static void nft_chain_commit_update(struct nft_trans
*trans
)
4706 struct nft_base_chain
*basechain
;
4708 if (nft_trans_chain_name(trans
)[0])
4709 strcpy(trans
->ctx
.chain
->name
, nft_trans_chain_name(trans
));
4711 if (!nft_is_base_chain(trans
->ctx
.chain
))
4714 basechain
= nft_base_chain(trans
->ctx
.chain
);
4715 nft_chain_stats_replace(basechain
, nft_trans_chain_stats(trans
));
4717 switch (nft_trans_chain_policy(trans
)) {
4720 basechain
->policy
= nft_trans_chain_policy(trans
);
4725 static void nf_tables_commit_release(struct nft_trans
*trans
)
4727 switch (trans
->msg_type
) {
4728 case NFT_MSG_DELTABLE
:
4729 nf_tables_table_destroy(&trans
->ctx
);
4731 case NFT_MSG_DELCHAIN
:
4732 nf_tables_chain_destroy(trans
->ctx
.chain
);
4734 case NFT_MSG_DELRULE
:
4735 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
4737 case NFT_MSG_DELSET
:
4738 nft_set_destroy(nft_trans_set(trans
));
4740 case NFT_MSG_DELSETELEM
:
4741 nft_set_elem_destroy(nft_trans_elem_set(trans
),
4742 nft_trans_elem(trans
).priv
, true);
4744 case NFT_MSG_DELOBJ
:
4745 nft_obj_destroy(nft_trans_obj(trans
));
4751 static int nf_tables_commit(struct net
*net
, struct sk_buff
*skb
)
4753 struct nft_trans
*trans
, *next
;
4754 struct nft_trans_elem
*te
;
4756 /* Bump generation counter, invalidate any dump in progress */
4757 while (++net
->nft
.base_seq
== 0);
4759 /* A new generation has just started */
4760 net
->nft
.gencursor
= nft_gencursor_next(net
);
4762 /* Make sure all packets have left the previous generation before
4763 * purging old rules.
4767 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
4768 switch (trans
->msg_type
) {
4769 case NFT_MSG_NEWTABLE
:
4770 if (nft_trans_table_update(trans
)) {
4771 if (!nft_trans_table_enable(trans
)) {
4772 nf_tables_table_disable(net
,
4775 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
4778 nft_clear(net
, trans
->ctx
.table
);
4780 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_NEWTABLE
);
4781 nft_trans_destroy(trans
);
4783 case NFT_MSG_DELTABLE
:
4784 list_del_rcu(&trans
->ctx
.table
->list
);
4785 nf_tables_table_notify(&trans
->ctx
, NFT_MSG_DELTABLE
);
4787 case NFT_MSG_NEWCHAIN
:
4788 if (nft_trans_chain_update(trans
))
4789 nft_chain_commit_update(trans
);
4791 nft_clear(net
, trans
->ctx
.chain
);
4793 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_NEWCHAIN
);
4794 nft_trans_destroy(trans
);
4796 case NFT_MSG_DELCHAIN
:
4797 list_del_rcu(&trans
->ctx
.chain
->list
);
4798 nf_tables_chain_notify(&trans
->ctx
, NFT_MSG_DELCHAIN
);
4799 nf_tables_unregister_hooks(trans
->ctx
.net
,
4802 trans
->ctx
.afi
->nops
);
4804 case NFT_MSG_NEWRULE
:
4805 nft_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
4806 nf_tables_rule_notify(&trans
->ctx
,
4807 nft_trans_rule(trans
),
4809 nft_trans_destroy(trans
);
4811 case NFT_MSG_DELRULE
:
4812 list_del_rcu(&nft_trans_rule(trans
)->list
);
4813 nf_tables_rule_notify(&trans
->ctx
,
4814 nft_trans_rule(trans
),
4817 case NFT_MSG_NEWSET
:
4818 nft_clear(net
, nft_trans_set(trans
));
4819 /* This avoids hitting -EBUSY when deleting the table
4820 * from the transaction.
4822 if (nft_trans_set(trans
)->flags
& NFT_SET_ANONYMOUS
&&
4823 !list_empty(&nft_trans_set(trans
)->bindings
))
4824 trans
->ctx
.table
->use
--;
4826 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
4827 NFT_MSG_NEWSET
, GFP_KERNEL
);
4828 nft_trans_destroy(trans
);
4830 case NFT_MSG_DELSET
:
4831 list_del_rcu(&nft_trans_set(trans
)->list
);
4832 nf_tables_set_notify(&trans
->ctx
, nft_trans_set(trans
),
4833 NFT_MSG_DELSET
, GFP_KERNEL
);
4835 case NFT_MSG_NEWSETELEM
:
4836 te
= (struct nft_trans_elem
*)trans
->data
;
4838 te
->set
->ops
->activate(net
, te
->set
, &te
->elem
);
4839 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
4841 NFT_MSG_NEWSETELEM
, 0);
4842 nft_trans_destroy(trans
);
4844 case NFT_MSG_DELSETELEM
:
4845 te
= (struct nft_trans_elem
*)trans
->data
;
4847 nf_tables_setelem_notify(&trans
->ctx
, te
->set
,
4849 NFT_MSG_DELSETELEM
, 0);
4850 te
->set
->ops
->remove(net
, te
->set
, &te
->elem
);
4851 atomic_dec(&te
->set
->nelems
);
4854 case NFT_MSG_NEWOBJ
:
4855 nft_clear(net
, nft_trans_obj(trans
));
4856 nf_tables_obj_notify(&trans
->ctx
, nft_trans_obj(trans
),
4858 nft_trans_destroy(trans
);
4860 case NFT_MSG_DELOBJ
:
4861 list_del_rcu(&nft_trans_obj(trans
)->list
);
4862 nf_tables_obj_notify(&trans
->ctx
, nft_trans_obj(trans
),
4870 list_for_each_entry_safe(trans
, next
, &net
->nft
.commit_list
, list
) {
4871 list_del(&trans
->list
);
4872 nf_tables_commit_release(trans
);
4875 nf_tables_gen_notify(net
, skb
, NFT_MSG_NEWGEN
);
4880 static void nf_tables_abort_release(struct nft_trans
*trans
)
4882 switch (trans
->msg_type
) {
4883 case NFT_MSG_NEWTABLE
:
4884 nf_tables_table_destroy(&trans
->ctx
);
4886 case NFT_MSG_NEWCHAIN
:
4887 nf_tables_chain_destroy(trans
->ctx
.chain
);
4889 case NFT_MSG_NEWRULE
:
4890 nf_tables_rule_destroy(&trans
->ctx
, nft_trans_rule(trans
));
4892 case NFT_MSG_NEWSET
:
4893 nft_set_destroy(nft_trans_set(trans
));
4895 case NFT_MSG_NEWSETELEM
:
4896 nft_set_elem_destroy(nft_trans_elem_set(trans
),
4897 nft_trans_elem(trans
).priv
, true);
4899 case NFT_MSG_NEWOBJ
:
4900 nft_obj_destroy(nft_trans_obj(trans
));
4906 static int nf_tables_abort(struct net
*net
, struct sk_buff
*skb
)
4908 struct nft_trans
*trans
, *next
;
4909 struct nft_trans_elem
*te
;
4911 list_for_each_entry_safe_reverse(trans
, next
, &net
->nft
.commit_list
,
4913 switch (trans
->msg_type
) {
4914 case NFT_MSG_NEWTABLE
:
4915 if (nft_trans_table_update(trans
)) {
4916 if (nft_trans_table_enable(trans
)) {
4917 nf_tables_table_disable(net
,
4920 trans
->ctx
.table
->flags
|= NFT_TABLE_F_DORMANT
;
4922 nft_trans_destroy(trans
);
4924 list_del_rcu(&trans
->ctx
.table
->list
);
4927 case NFT_MSG_DELTABLE
:
4928 nft_clear(trans
->ctx
.net
, trans
->ctx
.table
);
4929 nft_trans_destroy(trans
);
4931 case NFT_MSG_NEWCHAIN
:
4932 if (nft_trans_chain_update(trans
)) {
4933 free_percpu(nft_trans_chain_stats(trans
));
4935 nft_trans_destroy(trans
);
4937 trans
->ctx
.table
->use
--;
4938 list_del_rcu(&trans
->ctx
.chain
->list
);
4939 nf_tables_unregister_hooks(trans
->ctx
.net
,
4942 trans
->ctx
.afi
->nops
);
4945 case NFT_MSG_DELCHAIN
:
4946 trans
->ctx
.table
->use
++;
4947 nft_clear(trans
->ctx
.net
, trans
->ctx
.chain
);
4948 nft_trans_destroy(trans
);
4950 case NFT_MSG_NEWRULE
:
4951 trans
->ctx
.chain
->use
--;
4952 list_del_rcu(&nft_trans_rule(trans
)->list
);
4954 case NFT_MSG_DELRULE
:
4955 trans
->ctx
.chain
->use
++;
4956 nft_clear(trans
->ctx
.net
, nft_trans_rule(trans
));
4957 nft_trans_destroy(trans
);
4959 case NFT_MSG_NEWSET
:
4960 trans
->ctx
.table
->use
--;
4961 list_del_rcu(&nft_trans_set(trans
)->list
);
4963 case NFT_MSG_DELSET
:
4964 trans
->ctx
.table
->use
++;
4965 nft_clear(trans
->ctx
.net
, nft_trans_set(trans
));
4966 nft_trans_destroy(trans
);
4968 case NFT_MSG_NEWSETELEM
:
4969 te
= (struct nft_trans_elem
*)trans
->data
;
4971 te
->set
->ops
->remove(net
, te
->set
, &te
->elem
);
4972 atomic_dec(&te
->set
->nelems
);
4974 case NFT_MSG_DELSETELEM
:
4975 te
= (struct nft_trans_elem
*)trans
->data
;
4977 te
->set
->ops
->activate(net
, te
->set
, &te
->elem
);
4980 nft_trans_destroy(trans
);
4982 case NFT_MSG_NEWOBJ
:
4983 trans
->ctx
.table
->use
--;
4984 list_del_rcu(&nft_trans_obj(trans
)->list
);
4986 case NFT_MSG_DELOBJ
:
4987 trans
->ctx
.table
->use
++;
4988 nft_clear(trans
->ctx
.net
, nft_trans_obj(trans
));
4989 nft_trans_destroy(trans
);
4996 list_for_each_entry_safe_reverse(trans
, next
,
4997 &net
->nft
.commit_list
, list
) {
4998 list_del(&trans
->list
);
4999 nf_tables_abort_release(trans
);
5005 static bool nf_tables_valid_genid(struct net
*net
, u32 genid
)
5007 return net
->nft
.base_seq
== genid
;
5010 static const struct nfnetlink_subsystem nf_tables_subsys
= {
5011 .name
= "nf_tables",
5012 .subsys_id
= NFNL_SUBSYS_NFTABLES
,
5013 .cb_count
= NFT_MSG_MAX
,
5015 .commit
= nf_tables_commit
,
5016 .abort
= nf_tables_abort
,
5017 .valid_genid
= nf_tables_valid_genid
,
5020 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
5021 enum nft_chain_type type
)
5023 const struct nft_base_chain
*basechain
;
5025 if (nft_is_base_chain(chain
)) {
5026 basechain
= nft_base_chain(chain
);
5027 if (basechain
->type
->type
!= type
)
5032 EXPORT_SYMBOL_GPL(nft_chain_validate_dependency
);
5034 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
5035 unsigned int hook_flags
)
5037 struct nft_base_chain
*basechain
;
5039 if (nft_is_base_chain(chain
)) {
5040 basechain
= nft_base_chain(chain
);
5042 if ((1 << basechain
->ops
[0].hooknum
) & hook_flags
)
5050 EXPORT_SYMBOL_GPL(nft_chain_validate_hooks
);
5053 * Loop detection - walk through the ruleset beginning at the destination chain
5054 * of a new jump until either the source chain is reached (loop) or all
5055 * reachable chains have been traversed.
5057 * The loop check is performed whenever a new jump verdict is added to an
5058 * expression or verdict map or a verdict map is bound to a new chain.
5061 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
5062 const struct nft_chain
*chain
);
5064 static int nf_tables_loop_check_setelem(const struct nft_ctx
*ctx
,
5065 struct nft_set
*set
,
5066 const struct nft_set_iter
*iter
,
5067 struct nft_set_elem
*elem
)
5069 const struct nft_set_ext
*ext
= nft_set_elem_ext(set
, elem
->priv
);
5070 const struct nft_data
*data
;
5072 if (nft_set_ext_exists(ext
, NFT_SET_EXT_FLAGS
) &&
5073 *nft_set_ext_flags(ext
) & NFT_SET_ELEM_INTERVAL_END
)
5076 data
= nft_set_ext_data(ext
);
5077 switch (data
->verdict
.code
) {
5080 return nf_tables_check_loops(ctx
, data
->verdict
.chain
);
5086 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
5087 const struct nft_chain
*chain
)
5089 const struct nft_rule
*rule
;
5090 const struct nft_expr
*expr
, *last
;
5091 struct nft_set
*set
;
5092 struct nft_set_binding
*binding
;
5093 struct nft_set_iter iter
;
5095 if (ctx
->chain
== chain
)
5098 list_for_each_entry(rule
, &chain
->rules
, list
) {
5099 nft_rule_for_each_expr(expr
, last
, rule
) {
5100 const struct nft_data
*data
= NULL
;
5103 if (!expr
->ops
->validate
)
5106 err
= expr
->ops
->validate(ctx
, expr
, &data
);
5113 switch (data
->verdict
.code
) {
5116 err
= nf_tables_check_loops(ctx
,
5117 data
->verdict
.chain
);
5126 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
5127 if (!nft_is_active_next(ctx
->net
, set
))
5129 if (!(set
->flags
& NFT_SET_MAP
) ||
5130 set
->dtype
!= NFT_DATA_VERDICT
)
5133 list_for_each_entry(binding
, &set
->bindings
, list
) {
5134 if (!(binding
->flags
& NFT_SET_MAP
) ||
5135 binding
->chain
!= chain
)
5138 iter
.genmask
= nft_genmask_next(ctx
->net
);
5142 iter
.fn
= nf_tables_loop_check_setelem
;
5144 set
->ops
->walk(ctx
, set
, &iter
);
5154 * nft_parse_u32_check - fetch u32 attribute and check for maximum value
5156 * @attr: netlink attribute to fetch value from
5157 * @max: maximum value to be stored in dest
5158 * @dest: pointer to the variable
5160 * Parse, check and store a given u32 netlink attribute into variable.
5161 * This function returns -ERANGE if the value goes over maximum value.
5162 * Otherwise a 0 is returned and the attribute value is stored in the
5163 * destination variable.
5165 int nft_parse_u32_check(const struct nlattr
*attr
, int max
, u32
*dest
)
5169 val
= ntohl(nla_get_be32(attr
));
5176 EXPORT_SYMBOL_GPL(nft_parse_u32_check
);
5179 * nft_parse_register - parse a register value from a netlink attribute
5181 * @attr: netlink attribute
5183 * Parse and translate a register value from a netlink attribute.
5184 * Registers used to be 128 bit wide, these register numbers will be
5185 * mapped to the corresponding 32 bit register numbers.
5187 unsigned int nft_parse_register(const struct nlattr
*attr
)
5191 reg
= ntohl(nla_get_be32(attr
));
5193 case NFT_REG_VERDICT
...NFT_REG_4
:
5194 return reg
* NFT_REG_SIZE
/ NFT_REG32_SIZE
;
5196 return reg
+ NFT_REG_SIZE
/ NFT_REG32_SIZE
- NFT_REG32_00
;
5199 EXPORT_SYMBOL_GPL(nft_parse_register
);
5202 * nft_dump_register - dump a register value to a netlink attribute
5204 * @skb: socket buffer
5205 * @attr: attribute number
5206 * @reg: register number
5208 * Construct a netlink attribute containing the register number. For
5209 * compatibility reasons, register numbers being a multiple of 4 are
5210 * translated to the corresponding 128 bit register numbers.
5212 int nft_dump_register(struct sk_buff
*skb
, unsigned int attr
, unsigned int reg
)
5214 if (reg
% (NFT_REG_SIZE
/ NFT_REG32_SIZE
) == 0)
5215 reg
= reg
/ (NFT_REG_SIZE
/ NFT_REG32_SIZE
);
5217 reg
= reg
- NFT_REG_SIZE
/ NFT_REG32_SIZE
+ NFT_REG32_00
;
5219 return nla_put_be32(skb
, attr
, htonl(reg
));
5221 EXPORT_SYMBOL_GPL(nft_dump_register
);
5224 * nft_validate_register_load - validate a load from a register
5226 * @reg: the register number
5227 * @len: the length of the data
5229 * Validate that the input register is one of the general purpose
5230 * registers and that the length of the load is within the bounds.
5232 int nft_validate_register_load(enum nft_registers reg
, unsigned int len
)
5234 if (reg
< NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
)
5238 if (reg
* NFT_REG32_SIZE
+ len
> FIELD_SIZEOF(struct nft_regs
, data
))
5243 EXPORT_SYMBOL_GPL(nft_validate_register_load
);
5246 * nft_validate_register_store - validate an expressions' register store
5248 * @ctx: context of the expression performing the load
5249 * @reg: the destination register number
5250 * @data: the data to load
5251 * @type: the data type
5252 * @len: the length of the data
5254 * Validate that a data load uses the appropriate data type for
5255 * the destination register and the length is within the bounds.
5256 * A value of NULL for the data means that its runtime gathered
5259 int nft_validate_register_store(const struct nft_ctx
*ctx
,
5260 enum nft_registers reg
,
5261 const struct nft_data
*data
,
5262 enum nft_data_types type
, unsigned int len
)
5267 case NFT_REG_VERDICT
:
5268 if (type
!= NFT_DATA_VERDICT
)
5272 (data
->verdict
.code
== NFT_GOTO
||
5273 data
->verdict
.code
== NFT_JUMP
)) {
5274 err
= nf_tables_check_loops(ctx
, data
->verdict
.chain
);
5278 if (ctx
->chain
->level
+ 1 >
5279 data
->verdict
.chain
->level
) {
5280 if (ctx
->chain
->level
+ 1 == NFT_JUMP_STACK_SIZE
)
5282 data
->verdict
.chain
->level
= ctx
->chain
->level
+ 1;
5288 if (reg
< NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
)
5292 if (reg
* NFT_REG32_SIZE
+ len
>
5293 FIELD_SIZEOF(struct nft_regs
, data
))
5296 if (data
!= NULL
&& type
!= NFT_DATA_VALUE
)
5301 EXPORT_SYMBOL_GPL(nft_validate_register_store
);
5303 static const struct nla_policy nft_verdict_policy
[NFTA_VERDICT_MAX
+ 1] = {
5304 [NFTA_VERDICT_CODE
] = { .type
= NLA_U32
},
5305 [NFTA_VERDICT_CHAIN
] = { .type
= NLA_STRING
,
5306 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
5309 static int nft_verdict_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
5310 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
5312 u8 genmask
= nft_genmask_next(ctx
->net
);
5313 struct nlattr
*tb
[NFTA_VERDICT_MAX
+ 1];
5314 struct nft_chain
*chain
;
5317 err
= nla_parse_nested(tb
, NFTA_VERDICT_MAX
, nla
, nft_verdict_policy
,
5322 if (!tb
[NFTA_VERDICT_CODE
])
5324 data
->verdict
.code
= ntohl(nla_get_be32(tb
[NFTA_VERDICT_CODE
]));
5326 switch (data
->verdict
.code
) {
5328 switch (data
->verdict
.code
& NF_VERDICT_MASK
) {
5343 if (!tb
[NFTA_VERDICT_CHAIN
])
5345 chain
= nf_tables_chain_lookup(ctx
->table
,
5346 tb
[NFTA_VERDICT_CHAIN
], genmask
);
5348 return PTR_ERR(chain
);
5349 if (nft_is_base_chain(chain
))
5353 data
->verdict
.chain
= chain
;
5357 desc
->len
= sizeof(data
->verdict
);
5358 desc
->type
= NFT_DATA_VERDICT
;
5362 static void nft_verdict_uninit(const struct nft_data
*data
)
5364 switch (data
->verdict
.code
) {
5367 data
->verdict
.chain
->use
--;
5372 int nft_verdict_dump(struct sk_buff
*skb
, int type
, const struct nft_verdict
*v
)
5374 struct nlattr
*nest
;
5376 nest
= nla_nest_start(skb
, type
);
5378 goto nla_put_failure
;
5380 if (nla_put_be32(skb
, NFTA_VERDICT_CODE
, htonl(v
->code
)))
5381 goto nla_put_failure
;
5386 if (nla_put_string(skb
, NFTA_VERDICT_CHAIN
,
5388 goto nla_put_failure
;
5390 nla_nest_end(skb
, nest
);
5397 static int nft_value_init(const struct nft_ctx
*ctx
,
5398 struct nft_data
*data
, unsigned int size
,
5399 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
5409 nla_memcpy(data
->data
, nla
, len
);
5410 desc
->type
= NFT_DATA_VALUE
;
5415 static int nft_value_dump(struct sk_buff
*skb
, const struct nft_data
*data
,
5418 return nla_put(skb
, NFTA_DATA_VALUE
, len
, data
->data
);
5421 static const struct nla_policy nft_data_policy
[NFTA_DATA_MAX
+ 1] = {
5422 [NFTA_DATA_VALUE
] = { .type
= NLA_BINARY
},
5423 [NFTA_DATA_VERDICT
] = { .type
= NLA_NESTED
},
5427 * nft_data_init - parse nf_tables data netlink attributes
5429 * @ctx: context of the expression using the data
5430 * @data: destination struct nft_data
5431 * @size: maximum data length
5432 * @desc: data description
5433 * @nla: netlink attribute containing data
5435 * Parse the netlink data attributes and initialize a struct nft_data.
5436 * The type and length of data are returned in the data description.
5438 * The caller can indicate that it only wants to accept data of type
5439 * NFT_DATA_VALUE by passing NULL for the ctx argument.
5441 int nft_data_init(const struct nft_ctx
*ctx
,
5442 struct nft_data
*data
, unsigned int size
,
5443 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
5445 struct nlattr
*tb
[NFTA_DATA_MAX
+ 1];
5448 err
= nla_parse_nested(tb
, NFTA_DATA_MAX
, nla
, nft_data_policy
, NULL
);
5452 if (tb
[NFTA_DATA_VALUE
])
5453 return nft_value_init(ctx
, data
, size
, desc
,
5454 tb
[NFTA_DATA_VALUE
]);
5455 if (tb
[NFTA_DATA_VERDICT
] && ctx
!= NULL
)
5456 return nft_verdict_init(ctx
, data
, desc
, tb
[NFTA_DATA_VERDICT
]);
5459 EXPORT_SYMBOL_GPL(nft_data_init
);
5462 * nft_data_uninit - release a nft_data item
5464 * @data: struct nft_data to release
5465 * @type: type of data
5467 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
5468 * all others need to be released by calling this function.
5470 void nft_data_uninit(const struct nft_data
*data
, enum nft_data_types type
)
5472 if (type
< NFT_DATA_VERDICT
)
5475 case NFT_DATA_VERDICT
:
5476 return nft_verdict_uninit(data
);
5481 EXPORT_SYMBOL_GPL(nft_data_uninit
);
5483 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
5484 enum nft_data_types type
, unsigned int len
)
5486 struct nlattr
*nest
;
5489 nest
= nla_nest_start(skb
, attr
);
5494 case NFT_DATA_VALUE
:
5495 err
= nft_value_dump(skb
, data
, len
);
5497 case NFT_DATA_VERDICT
:
5498 err
= nft_verdict_dump(skb
, NFTA_DATA_VERDICT
, &data
->verdict
);
5505 nla_nest_end(skb
, nest
);
5508 EXPORT_SYMBOL_GPL(nft_data_dump
);
5510 static int __net_init
nf_tables_init_net(struct net
*net
)
5512 INIT_LIST_HEAD(&net
->nft
.af_info
);
5513 INIT_LIST_HEAD(&net
->nft
.commit_list
);
5514 net
->nft
.base_seq
= 1;
5518 int __nft_release_basechain(struct nft_ctx
*ctx
)
5520 struct nft_rule
*rule
, *nr
;
5522 BUG_ON(!nft_is_base_chain(ctx
->chain
));
5524 nf_tables_unregister_hooks(ctx
->net
, ctx
->chain
->table
, ctx
->chain
,
5526 list_for_each_entry_safe(rule
, nr
, &ctx
->chain
->rules
, list
) {
5527 list_del(&rule
->list
);
5529 nf_tables_rule_destroy(ctx
, rule
);
5531 list_del(&ctx
->chain
->list
);
5533 nf_tables_chain_destroy(ctx
->chain
);
5537 EXPORT_SYMBOL_GPL(__nft_release_basechain
);
5539 /* Called by nft_unregister_afinfo() from __net_exit path, nfnl_lock is held. */
5540 static void __nft_release_afinfo(struct net
*net
, struct nft_af_info
*afi
)
5542 struct nft_table
*table
, *nt
;
5543 struct nft_chain
*chain
, *nc
;
5544 struct nft_object
*obj
, *ne
;
5545 struct nft_rule
*rule
, *nr
;
5546 struct nft_set
*set
, *ns
;
5547 struct nft_ctx ctx
= {
5552 list_for_each_entry_safe(table
, nt
, &afi
->tables
, list
) {
5553 list_for_each_entry(chain
, &table
->chains
, list
)
5554 nf_tables_unregister_hooks(net
, table
, chain
,
5556 /* No packets are walking on these chains anymore. */
5558 list_for_each_entry(chain
, &table
->chains
, list
) {
5560 list_for_each_entry_safe(rule
, nr
, &chain
->rules
, list
) {
5561 list_del(&rule
->list
);
5563 nf_tables_rule_destroy(&ctx
, rule
);
5566 list_for_each_entry_safe(set
, ns
, &table
->sets
, list
) {
5567 list_del(&set
->list
);
5569 nft_set_destroy(set
);
5571 list_for_each_entry_safe(obj
, ne
, &table
->objects
, list
) {
5572 list_del(&obj
->list
);
5574 nft_obj_destroy(obj
);
5576 list_for_each_entry_safe(chain
, nc
, &table
->chains
, list
) {
5577 list_del(&chain
->list
);
5579 nf_tables_chain_destroy(chain
);
5581 list_del(&table
->list
);
5582 nf_tables_table_destroy(&ctx
);
5586 static struct pernet_operations nf_tables_net_ops
= {
5587 .init
= nf_tables_init_net
,
5590 static int __init
nf_tables_module_init(void)
5594 info
= kmalloc(sizeof(struct nft_expr_info
) * NFT_RULE_MAXEXPRS
,
5601 err
= nf_tables_core_module_init();
5605 err
= nfnetlink_subsys_register(&nf_tables_subsys
);
5609 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
5610 return register_pernet_subsys(&nf_tables_net_ops
);
5612 nf_tables_core_module_exit();
5619 static void __exit
nf_tables_module_exit(void)
5621 unregister_pernet_subsys(&nf_tables_net_ops
);
5622 nfnetlink_subsys_unregister(&nf_tables_subsys
);
5624 nf_tables_core_module_exit();
5628 module_init(nf_tables_module_init
);
5629 module_exit(nf_tables_module_exit
);
5631 MODULE_LICENSE("GPL");
5632 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
5633 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES
);