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
);
27 * nft_register_afinfo - register nf_tables address family info
29 * @afi: address family info to register
31 * Register the address family for use with nf_tables. Returns zero on
32 * success or a negative errno code otherwise.
34 int nft_register_afinfo(struct net
*net
, struct nft_af_info
*afi
)
36 INIT_LIST_HEAD(&afi
->tables
);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
38 list_add_tail(&afi
->list
, &net
->nft
.af_info
);
39 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
42 EXPORT_SYMBOL_GPL(nft_register_afinfo
);
45 * nft_unregister_afinfo - unregister nf_tables address family info
47 * @afi: address family info to unregister
49 * Unregister the address family for use with nf_tables.
51 void nft_unregister_afinfo(struct nft_af_info
*afi
)
53 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
55 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
57 EXPORT_SYMBOL_GPL(nft_unregister_afinfo
);
59 static struct nft_af_info
*nft_afinfo_lookup(struct net
*net
, int family
)
61 struct nft_af_info
*afi
;
63 list_for_each_entry(afi
, &net
->nft
.af_info
, list
) {
64 if (afi
->family
== family
)
70 static struct nft_af_info
*
71 nf_tables_afinfo_lookup(struct net
*net
, int family
, bool autoload
)
73 struct nft_af_info
*afi
;
75 afi
= nft_afinfo_lookup(net
, family
);
80 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
81 request_module("nft-afinfo-%u", family
);
82 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
83 afi
= nft_afinfo_lookup(net
, family
);
85 return ERR_PTR(-EAGAIN
);
88 return ERR_PTR(-EAFNOSUPPORT
);
95 static struct nft_table
*nft_table_lookup(const struct nft_af_info
*afi
,
96 const struct nlattr
*nla
)
98 struct nft_table
*table
;
100 list_for_each_entry(table
, &afi
->tables
, list
) {
101 if (!nla_strcmp(nla
, table
->name
))
107 static struct nft_table
*nf_tables_table_lookup(const struct nft_af_info
*afi
,
108 const struct nlattr
*nla
)
110 struct nft_table
*table
;
113 return ERR_PTR(-EINVAL
);
115 table
= nft_table_lookup(afi
, nla
);
119 return ERR_PTR(-ENOENT
);
122 static inline u64
nf_tables_alloc_handle(struct nft_table
*table
)
124 return ++table
->hgenerator
;
127 static const struct nf_chain_type
*chain_type
[AF_MAX
][NFT_CHAIN_T_MAX
];
129 static const struct nf_chain_type
*
130 __nf_tables_chain_type_lookup(int family
, const struct nlattr
*nla
)
134 for (i
= 0; i
< NFT_CHAIN_T_MAX
; i
++) {
135 if (chain_type
[family
][i
] != NULL
&&
136 !nla_strcmp(nla
, chain_type
[family
][i
]->name
))
137 return chain_type
[family
][i
];
142 static const struct nf_chain_type
*
143 nf_tables_chain_type_lookup(const struct nft_af_info
*afi
,
144 const struct nlattr
*nla
,
147 const struct nf_chain_type
*type
;
149 type
= __nf_tables_chain_type_lookup(afi
->family
, nla
);
152 #ifdef CONFIG_MODULES
154 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
155 request_module("nft-chain-%u-%*.s", afi
->family
,
156 nla_len(nla
)-1, (const char *)nla_data(nla
));
157 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
158 type
= __nf_tables_chain_type_lookup(afi
->family
, nla
);
160 return ERR_PTR(-EAGAIN
);
163 return ERR_PTR(-ENOENT
);
166 static const struct nla_policy nft_table_policy
[NFTA_TABLE_MAX
+ 1] = {
167 [NFTA_TABLE_NAME
] = { .type
= NLA_STRING
},
168 [NFTA_TABLE_FLAGS
] = { .type
= NLA_U32
},
171 static int nf_tables_fill_table_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
172 int event
, u32 flags
, int family
,
173 const struct nft_table
*table
)
175 struct nlmsghdr
*nlh
;
176 struct nfgenmsg
*nfmsg
;
178 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
179 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
181 goto nla_put_failure
;
183 nfmsg
= nlmsg_data(nlh
);
184 nfmsg
->nfgen_family
= family
;
185 nfmsg
->version
= NFNETLINK_V0
;
188 if (nla_put_string(skb
, NFTA_TABLE_NAME
, table
->name
) ||
189 nla_put_be32(skb
, NFTA_TABLE_FLAGS
, htonl(table
->flags
)) ||
190 nla_put_be32(skb
, NFTA_TABLE_USE
, htonl(table
->use
)))
191 goto nla_put_failure
;
193 return nlmsg_end(skb
, nlh
);
196 nlmsg_trim(skb
, nlh
);
200 static int nf_tables_table_notify(const struct sk_buff
*oskb
,
201 const struct nlmsghdr
*nlh
,
202 const struct nft_table
*table
,
203 int event
, int family
)
206 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
207 u32 seq
= nlh
? nlh
->nlmsg_seq
: 0;
208 struct net
*net
= oskb
? sock_net(oskb
->sk
) : &init_net
;
212 report
= nlh
? nlmsg_report(nlh
) : false;
213 if (!report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
217 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
221 err
= nf_tables_fill_table_info(skb
, portid
, seq
, event
, 0,
228 err
= nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, report
,
232 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, err
);
236 static int nf_tables_dump_tables(struct sk_buff
*skb
,
237 struct netlink_callback
*cb
)
239 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
240 const struct nft_af_info
*afi
;
241 const struct nft_table
*table
;
242 unsigned int idx
= 0, s_idx
= cb
->args
[0];
243 struct net
*net
= sock_net(skb
->sk
);
244 int family
= nfmsg
->nfgen_family
;
246 list_for_each_entry(afi
, &net
->nft
.af_info
, list
) {
247 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
250 list_for_each_entry(table
, &afi
->tables
, list
) {
254 memset(&cb
->args
[1], 0,
255 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
256 if (nf_tables_fill_table_info(skb
,
257 NETLINK_CB(cb
->skb
).portid
,
261 afi
->family
, table
) < 0)
272 static int nf_tables_gettable(struct sock
*nlsk
, struct sk_buff
*skb
,
273 const struct nlmsghdr
*nlh
,
274 const struct nlattr
* const nla
[])
276 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
277 const struct nft_af_info
*afi
;
278 const struct nft_table
*table
;
279 struct sk_buff
*skb2
;
280 struct net
*net
= sock_net(skb
->sk
);
281 int family
= nfmsg
->nfgen_family
;
284 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
285 struct netlink_dump_control c
= {
286 .dump
= nf_tables_dump_tables
,
288 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
291 afi
= nf_tables_afinfo_lookup(net
, family
, false);
295 table
= nf_tables_table_lookup(afi
, nla
[NFTA_TABLE_NAME
]);
297 return PTR_ERR(table
);
299 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
303 err
= nf_tables_fill_table_info(skb2
, NETLINK_CB(skb
).portid
,
304 nlh
->nlmsg_seq
, NFT_MSG_NEWTABLE
, 0,
309 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
316 static int nf_tables_table_enable(const struct nft_af_info
*afi
,
317 struct nft_table
*table
)
319 struct nft_chain
*chain
;
322 list_for_each_entry(chain
, &table
->chains
, list
) {
323 if (!(chain
->flags
& NFT_BASE_CHAIN
))
326 err
= nf_register_hooks(nft_base_chain(chain
)->ops
, afi
->nops
);
334 list_for_each_entry(chain
, &table
->chains
, list
) {
335 if (!(chain
->flags
& NFT_BASE_CHAIN
))
341 nf_unregister_hooks(nft_base_chain(chain
)->ops
, afi
->nops
);
346 static int nf_tables_table_disable(const struct nft_af_info
*afi
,
347 struct nft_table
*table
)
349 struct nft_chain
*chain
;
351 list_for_each_entry(chain
, &table
->chains
, list
) {
352 if (chain
->flags
& NFT_BASE_CHAIN
)
353 nf_unregister_hooks(nft_base_chain(chain
)->ops
,
360 static int nf_tables_updtable(struct sock
*nlsk
, struct sk_buff
*skb
,
361 const struct nlmsghdr
*nlh
,
362 const struct nlattr
* const nla
[],
363 struct nft_af_info
*afi
, struct nft_table
*table
)
365 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
366 int family
= nfmsg
->nfgen_family
, ret
= 0;
368 if (nla
[NFTA_TABLE_FLAGS
]) {
371 flags
= ntohl(nla_get_be32(nla
[NFTA_TABLE_FLAGS
]));
372 if (flags
& ~NFT_TABLE_F_DORMANT
)
375 if ((flags
& NFT_TABLE_F_DORMANT
) &&
376 !(table
->flags
& NFT_TABLE_F_DORMANT
)) {
377 ret
= nf_tables_table_disable(afi
, table
);
379 table
->flags
|= NFT_TABLE_F_DORMANT
;
380 } else if (!(flags
& NFT_TABLE_F_DORMANT
) &&
381 table
->flags
& NFT_TABLE_F_DORMANT
) {
382 ret
= nf_tables_table_enable(afi
, table
);
384 table
->flags
&= ~NFT_TABLE_F_DORMANT
;
390 nf_tables_table_notify(skb
, nlh
, table
, NFT_MSG_NEWTABLE
, family
);
395 static int nf_tables_newtable(struct sock
*nlsk
, struct sk_buff
*skb
,
396 const struct nlmsghdr
*nlh
,
397 const struct nlattr
* const nla
[])
399 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
400 const struct nlattr
*name
;
401 struct nft_af_info
*afi
;
402 struct nft_table
*table
;
403 struct net
*net
= sock_net(skb
->sk
);
404 int family
= nfmsg
->nfgen_family
;
407 afi
= nf_tables_afinfo_lookup(net
, family
, true);
411 name
= nla
[NFTA_TABLE_NAME
];
412 table
= nf_tables_table_lookup(afi
, name
);
414 if (PTR_ERR(table
) != -ENOENT
)
415 return PTR_ERR(table
);
420 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
422 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
424 return nf_tables_updtable(nlsk
, skb
, nlh
, nla
, afi
, table
);
427 if (nla
[NFTA_TABLE_FLAGS
]) {
428 flags
= ntohl(nla_get_be32(nla
[NFTA_TABLE_FLAGS
]));
429 if (flags
& ~NFT_TABLE_F_DORMANT
)
433 if (!try_module_get(afi
->owner
))
434 return -EAFNOSUPPORT
;
436 table
= kzalloc(sizeof(*table
) + nla_len(name
), GFP_KERNEL
);
438 module_put(afi
->owner
);
442 nla_strlcpy(table
->name
, name
, nla_len(name
));
443 INIT_LIST_HEAD(&table
->chains
);
444 INIT_LIST_HEAD(&table
->sets
);
445 table
->flags
= flags
;
447 list_add_tail(&table
->list
, &afi
->tables
);
448 nf_tables_table_notify(skb
, nlh
, table
, NFT_MSG_NEWTABLE
, family
);
452 static int nf_tables_deltable(struct sock
*nlsk
, struct sk_buff
*skb
,
453 const struct nlmsghdr
*nlh
,
454 const struct nlattr
* const nla
[])
456 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
457 struct nft_af_info
*afi
;
458 struct nft_table
*table
;
459 struct net
*net
= sock_net(skb
->sk
);
460 int family
= nfmsg
->nfgen_family
;
462 afi
= nf_tables_afinfo_lookup(net
, family
, false);
466 table
= nf_tables_table_lookup(afi
, nla
[NFTA_TABLE_NAME
]);
468 return PTR_ERR(table
);
470 if (!list_empty(&table
->chains
) || !list_empty(&table
->sets
))
473 list_del(&table
->list
);
474 nf_tables_table_notify(skb
, nlh
, table
, NFT_MSG_DELTABLE
, family
);
476 module_put(afi
->owner
);
480 int nft_register_chain_type(const struct nf_chain_type
*ctype
)
484 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
485 if (chain_type
[ctype
->family
][ctype
->type
] != NULL
) {
489 chain_type
[ctype
->family
][ctype
->type
] = ctype
;
491 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
494 EXPORT_SYMBOL_GPL(nft_register_chain_type
);
496 void nft_unregister_chain_type(const struct nf_chain_type
*ctype
)
498 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
499 chain_type
[ctype
->family
][ctype
->type
] = NULL
;
500 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
502 EXPORT_SYMBOL_GPL(nft_unregister_chain_type
);
508 static struct nft_chain
*
509 nf_tables_chain_lookup_byhandle(const struct nft_table
*table
, u64 handle
)
511 struct nft_chain
*chain
;
513 list_for_each_entry(chain
, &table
->chains
, list
) {
514 if (chain
->handle
== handle
)
518 return ERR_PTR(-ENOENT
);
521 static struct nft_chain
*nf_tables_chain_lookup(const struct nft_table
*table
,
522 const struct nlattr
*nla
)
524 struct nft_chain
*chain
;
527 return ERR_PTR(-EINVAL
);
529 list_for_each_entry(chain
, &table
->chains
, list
) {
530 if (!nla_strcmp(nla
, chain
->name
))
534 return ERR_PTR(-ENOENT
);
537 static const struct nla_policy nft_chain_policy
[NFTA_CHAIN_MAX
+ 1] = {
538 [NFTA_CHAIN_TABLE
] = { .type
= NLA_STRING
},
539 [NFTA_CHAIN_HANDLE
] = { .type
= NLA_U64
},
540 [NFTA_CHAIN_NAME
] = { .type
= NLA_STRING
,
541 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
542 [NFTA_CHAIN_HOOK
] = { .type
= NLA_NESTED
},
543 [NFTA_CHAIN_POLICY
] = { .type
= NLA_U32
},
544 [NFTA_CHAIN_TYPE
] = { .type
= NLA_NUL_STRING
},
545 [NFTA_CHAIN_COUNTERS
] = { .type
= NLA_NESTED
},
548 static const struct nla_policy nft_hook_policy
[NFTA_HOOK_MAX
+ 1] = {
549 [NFTA_HOOK_HOOKNUM
] = { .type
= NLA_U32
},
550 [NFTA_HOOK_PRIORITY
] = { .type
= NLA_U32
},
553 static int nft_dump_stats(struct sk_buff
*skb
, struct nft_stats __percpu
*stats
)
555 struct nft_stats
*cpu_stats
, total
;
559 memset(&total
, 0, sizeof(total
));
560 for_each_possible_cpu(cpu
) {
561 cpu_stats
= per_cpu_ptr(stats
, cpu
);
562 total
.pkts
+= cpu_stats
->pkts
;
563 total
.bytes
+= cpu_stats
->bytes
;
565 nest
= nla_nest_start(skb
, NFTA_CHAIN_COUNTERS
);
567 goto nla_put_failure
;
569 if (nla_put_be64(skb
, NFTA_COUNTER_PACKETS
, cpu_to_be64(total
.pkts
)) ||
570 nla_put_be64(skb
, NFTA_COUNTER_BYTES
, cpu_to_be64(total
.bytes
)))
571 goto nla_put_failure
;
573 nla_nest_end(skb
, nest
);
580 static int nf_tables_fill_chain_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
581 int event
, u32 flags
, int family
,
582 const struct nft_table
*table
,
583 const struct nft_chain
*chain
)
585 struct nlmsghdr
*nlh
;
586 struct nfgenmsg
*nfmsg
;
588 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
589 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
591 goto nla_put_failure
;
593 nfmsg
= nlmsg_data(nlh
);
594 nfmsg
->nfgen_family
= family
;
595 nfmsg
->version
= NFNETLINK_V0
;
598 if (nla_put_string(skb
, NFTA_CHAIN_TABLE
, table
->name
))
599 goto nla_put_failure
;
600 if (nla_put_be64(skb
, NFTA_CHAIN_HANDLE
, cpu_to_be64(chain
->handle
)))
601 goto nla_put_failure
;
602 if (nla_put_string(skb
, NFTA_CHAIN_NAME
, chain
->name
))
603 goto nla_put_failure
;
605 if (chain
->flags
& NFT_BASE_CHAIN
) {
606 const struct nft_base_chain
*basechain
= nft_base_chain(chain
);
607 const struct nf_hook_ops
*ops
= &basechain
->ops
[0];
610 nest
= nla_nest_start(skb
, NFTA_CHAIN_HOOK
);
612 goto nla_put_failure
;
613 if (nla_put_be32(skb
, NFTA_HOOK_HOOKNUM
, htonl(ops
->hooknum
)))
614 goto nla_put_failure
;
615 if (nla_put_be32(skb
, NFTA_HOOK_PRIORITY
, htonl(ops
->priority
)))
616 goto nla_put_failure
;
617 nla_nest_end(skb
, nest
);
619 if (nla_put_be32(skb
, NFTA_CHAIN_POLICY
,
620 htonl(basechain
->policy
)))
621 goto nla_put_failure
;
623 if (nla_put_string(skb
, NFTA_CHAIN_TYPE
, basechain
->type
->name
))
624 goto nla_put_failure
;
626 if (nft_dump_stats(skb
, nft_base_chain(chain
)->stats
))
627 goto nla_put_failure
;
630 if (nla_put_be32(skb
, NFTA_CHAIN_USE
, htonl(chain
->use
)))
631 goto nla_put_failure
;
633 return nlmsg_end(skb
, nlh
);
636 nlmsg_trim(skb
, nlh
);
640 static int nf_tables_chain_notify(const struct sk_buff
*oskb
,
641 const struct nlmsghdr
*nlh
,
642 const struct nft_table
*table
,
643 const struct nft_chain
*chain
,
644 int event
, int family
)
647 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
648 struct net
*net
= oskb
? sock_net(oskb
->sk
) : &init_net
;
649 u32 seq
= nlh
? nlh
->nlmsg_seq
: 0;
653 report
= nlh
? nlmsg_report(nlh
) : false;
654 if (!report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
658 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
662 err
= nf_tables_fill_chain_info(skb
, portid
, seq
, event
, 0, family
,
669 err
= nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, report
,
673 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, err
);
677 static int nf_tables_dump_chains(struct sk_buff
*skb
,
678 struct netlink_callback
*cb
)
680 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
681 const struct nft_af_info
*afi
;
682 const struct nft_table
*table
;
683 const struct nft_chain
*chain
;
684 unsigned int idx
= 0, s_idx
= cb
->args
[0];
685 struct net
*net
= sock_net(skb
->sk
);
686 int family
= nfmsg
->nfgen_family
;
688 list_for_each_entry(afi
, &net
->nft
.af_info
, list
) {
689 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
692 list_for_each_entry(table
, &afi
->tables
, list
) {
693 list_for_each_entry(chain
, &table
->chains
, list
) {
697 memset(&cb
->args
[1], 0,
698 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
699 if (nf_tables_fill_chain_info(skb
, NETLINK_CB(cb
->skb
).portid
,
703 afi
->family
, table
, chain
) < 0)
716 static int nf_tables_getchain(struct sock
*nlsk
, struct sk_buff
*skb
,
717 const struct nlmsghdr
*nlh
,
718 const struct nlattr
* const nla
[])
720 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
721 const struct nft_af_info
*afi
;
722 const struct nft_table
*table
;
723 const struct nft_chain
*chain
;
724 struct sk_buff
*skb2
;
725 struct net
*net
= sock_net(skb
->sk
);
726 int family
= nfmsg
->nfgen_family
;
729 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
730 struct netlink_dump_control c
= {
731 .dump
= nf_tables_dump_chains
,
733 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
736 afi
= nf_tables_afinfo_lookup(net
, family
, false);
740 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
742 return PTR_ERR(table
);
744 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
]);
746 return PTR_ERR(chain
);
748 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
752 err
= nf_tables_fill_chain_info(skb2
, NETLINK_CB(skb
).portid
,
753 nlh
->nlmsg_seq
, NFT_MSG_NEWCHAIN
, 0,
754 family
, table
, chain
);
758 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
765 static const struct nla_policy nft_counter_policy
[NFTA_COUNTER_MAX
+ 1] = {
766 [NFTA_COUNTER_PACKETS
] = { .type
= NLA_U64
},
767 [NFTA_COUNTER_BYTES
] = { .type
= NLA_U64
},
771 nf_tables_counters(struct nft_base_chain
*chain
, const struct nlattr
*attr
)
773 struct nlattr
*tb
[NFTA_COUNTER_MAX
+1];
774 struct nft_stats __percpu
*newstats
;
775 struct nft_stats
*stats
;
778 err
= nla_parse_nested(tb
, NFTA_COUNTER_MAX
, attr
, nft_counter_policy
);
782 if (!tb
[NFTA_COUNTER_BYTES
] || !tb
[NFTA_COUNTER_PACKETS
])
785 newstats
= alloc_percpu(struct nft_stats
);
786 if (newstats
== NULL
)
789 /* Restore old counters on this cpu, no problem. Per-cpu statistics
790 * are not exposed to userspace.
792 stats
= this_cpu_ptr(newstats
);
793 stats
->bytes
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_BYTES
]));
794 stats
->pkts
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_PACKETS
]));
797 /* nfnl_lock is held, add some nfnl function for this, later */
798 struct nft_stats __percpu
*oldstats
=
799 rcu_dereference_protected(chain
->stats
, 1);
801 rcu_assign_pointer(chain
->stats
, newstats
);
803 free_percpu(oldstats
);
805 rcu_assign_pointer(chain
->stats
, newstats
);
810 static int nf_tables_newchain(struct sock
*nlsk
, struct sk_buff
*skb
,
811 const struct nlmsghdr
*nlh
,
812 const struct nlattr
* const nla
[])
814 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
815 const struct nlattr
* uninitialized_var(name
);
816 const struct nft_af_info
*afi
;
817 struct nft_table
*table
;
818 struct nft_chain
*chain
;
819 struct nft_base_chain
*basechain
= NULL
;
820 struct nlattr
*ha
[NFTA_HOOK_MAX
+ 1];
821 struct net
*net
= sock_net(skb
->sk
);
822 int family
= nfmsg
->nfgen_family
;
823 u8 policy
= NF_ACCEPT
;
829 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
831 afi
= nf_tables_afinfo_lookup(net
, family
, true);
835 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
837 return PTR_ERR(table
);
840 name
= nla
[NFTA_CHAIN_NAME
];
842 if (nla
[NFTA_CHAIN_HANDLE
]) {
843 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_CHAIN_HANDLE
]));
844 chain
= nf_tables_chain_lookup_byhandle(table
, handle
);
846 return PTR_ERR(chain
);
848 chain
= nf_tables_chain_lookup(table
, name
);
850 if (PTR_ERR(chain
) != -ENOENT
)
851 return PTR_ERR(chain
);
856 if (nla
[NFTA_CHAIN_POLICY
]) {
857 if ((chain
!= NULL
&&
858 !(chain
->flags
& NFT_BASE_CHAIN
)) ||
859 nla
[NFTA_CHAIN_HOOK
] == NULL
)
862 policy
= ntohl(nla_get_be32(nla
[NFTA_CHAIN_POLICY
]));
873 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
875 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
878 if (nla
[NFTA_CHAIN_HANDLE
] && name
&&
879 !IS_ERR(nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
])))
882 if (nla
[NFTA_CHAIN_COUNTERS
]) {
883 if (!(chain
->flags
& NFT_BASE_CHAIN
))
886 err
= nf_tables_counters(nft_base_chain(chain
),
887 nla
[NFTA_CHAIN_COUNTERS
]);
892 if (nla
[NFTA_CHAIN_POLICY
])
893 nft_base_chain(chain
)->policy
= policy
;
895 if (nla
[NFTA_CHAIN_HANDLE
] && name
)
896 nla_strlcpy(chain
->name
, name
, NFT_CHAIN_MAXNAMELEN
);
901 if (table
->use
== UINT_MAX
)
904 if (nla
[NFTA_CHAIN_HOOK
]) {
905 const struct nf_chain_type
*type
;
906 struct nf_hook_ops
*ops
;
908 u32 hooknum
, priority
;
910 type
= chain_type
[family
][NFT_CHAIN_T_DEFAULT
];
911 if (nla
[NFTA_CHAIN_TYPE
]) {
912 type
= nf_tables_chain_type_lookup(afi
,
913 nla
[NFTA_CHAIN_TYPE
],
916 return PTR_ERR(type
);
919 err
= nla_parse_nested(ha
, NFTA_HOOK_MAX
, nla
[NFTA_CHAIN_HOOK
],
923 if (ha
[NFTA_HOOK_HOOKNUM
] == NULL
||
924 ha
[NFTA_HOOK_PRIORITY
] == NULL
)
927 hooknum
= ntohl(nla_get_be32(ha
[NFTA_HOOK_HOOKNUM
]));
928 if (hooknum
>= afi
->nhooks
)
930 priority
= ntohl(nla_get_be32(ha
[NFTA_HOOK_PRIORITY
]));
932 if (!(type
->hook_mask
& (1 << hooknum
)))
934 if (!try_module_get(type
->owner
))
936 hookfn
= type
->hooks
[hooknum
];
938 basechain
= kzalloc(sizeof(*basechain
), GFP_KERNEL
);
939 if (basechain
== NULL
)
942 if (nla
[NFTA_CHAIN_COUNTERS
]) {
943 err
= nf_tables_counters(basechain
,
944 nla
[NFTA_CHAIN_COUNTERS
]);
946 module_put(type
->owner
);
951 struct nft_stats __percpu
*newstats
;
953 newstats
= alloc_percpu(struct nft_stats
);
954 if (newstats
== NULL
) {
955 module_put(type
->owner
);
959 rcu_assign_pointer(basechain
->stats
, newstats
);
962 basechain
->type
= type
;
963 chain
= &basechain
->chain
;
965 for (i
= 0; i
< afi
->nops
; i
++) {
966 ops
= &basechain
->ops
[i
];
968 ops
->owner
= afi
->owner
;
969 ops
->hooknum
= hooknum
;
970 ops
->priority
= priority
;
972 ops
->hook
= afi
->hooks
[ops
->hooknum
];
975 if (afi
->hook_ops_init
)
976 afi
->hook_ops_init(ops
, i
);
979 chain
->flags
|= NFT_BASE_CHAIN
;
980 basechain
->policy
= policy
;
982 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
987 INIT_LIST_HEAD(&chain
->rules
);
988 chain
->handle
= nf_tables_alloc_handle(table
);
990 chain
->table
= table
;
991 nla_strlcpy(chain
->name
, name
, NFT_CHAIN_MAXNAMELEN
);
993 if (!(table
->flags
& NFT_TABLE_F_DORMANT
) &&
994 chain
->flags
& NFT_BASE_CHAIN
) {
995 err
= nf_register_hooks(nft_base_chain(chain
)->ops
, afi
->nops
);
997 module_put(basechain
->type
->owner
);
998 free_percpu(basechain
->stats
);
1003 list_add_tail(&chain
->list
, &table
->chains
);
1006 nf_tables_chain_notify(skb
, nlh
, table
, chain
, NFT_MSG_NEWCHAIN
,
1011 static void nf_tables_chain_destroy(struct nft_chain
*chain
)
1013 BUG_ON(chain
->use
> 0);
1015 if (chain
->flags
& NFT_BASE_CHAIN
) {
1016 module_put(nft_base_chain(chain
)->type
->owner
);
1017 free_percpu(nft_base_chain(chain
)->stats
);
1018 kfree(nft_base_chain(chain
));
1023 static int nf_tables_delchain(struct sock
*nlsk
, struct sk_buff
*skb
,
1024 const struct nlmsghdr
*nlh
,
1025 const struct nlattr
* const nla
[])
1027 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1028 const struct nft_af_info
*afi
;
1029 struct nft_table
*table
;
1030 struct nft_chain
*chain
;
1031 struct net
*net
= sock_net(skb
->sk
);
1032 int family
= nfmsg
->nfgen_family
;
1034 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1036 return PTR_ERR(afi
);
1038 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
1040 return PTR_ERR(table
);
1042 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
]);
1044 return PTR_ERR(chain
);
1046 if (!list_empty(&chain
->rules
) || chain
->use
> 0)
1049 list_del(&chain
->list
);
1052 if (!(table
->flags
& NFT_TABLE_F_DORMANT
) &&
1053 chain
->flags
& NFT_BASE_CHAIN
)
1054 nf_unregister_hooks(nft_base_chain(chain
)->ops
, afi
->nops
);
1056 nf_tables_chain_notify(skb
, nlh
, table
, chain
, NFT_MSG_DELCHAIN
,
1059 /* Make sure all rule references are gone before this is released */
1062 nf_tables_chain_destroy(chain
);
1066 static void nft_ctx_init(struct nft_ctx
*ctx
,
1067 const struct sk_buff
*skb
,
1068 const struct nlmsghdr
*nlh
,
1069 const struct nft_af_info
*afi
,
1070 const struct nft_table
*table
,
1071 const struct nft_chain
*chain
,
1072 const struct nlattr
* const *nla
)
1074 ctx
->net
= sock_net(skb
->sk
);
1088 * nft_register_expr - register nf_tables expr type
1091 * Registers the expr type for use with nf_tables. Returns zero on
1092 * success or a negative errno code otherwise.
1094 int nft_register_expr(struct nft_expr_type
*type
)
1096 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1097 list_add_tail(&type
->list
, &nf_tables_expressions
);
1098 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1101 EXPORT_SYMBOL_GPL(nft_register_expr
);
1104 * nft_unregister_expr - unregister nf_tables expr type
1107 * Unregisters the expr typefor use with nf_tables.
1109 void nft_unregister_expr(struct nft_expr_type
*type
)
1111 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1112 list_del(&type
->list
);
1113 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1115 EXPORT_SYMBOL_GPL(nft_unregister_expr
);
1117 static const struct nft_expr_type
*__nft_expr_type_get(u8 family
,
1120 const struct nft_expr_type
*type
;
1122 list_for_each_entry(type
, &nf_tables_expressions
, list
) {
1123 if (!nla_strcmp(nla
, type
->name
) &&
1124 (!type
->family
|| type
->family
== family
))
1130 static const struct nft_expr_type
*nft_expr_type_get(u8 family
,
1133 const struct nft_expr_type
*type
;
1136 return ERR_PTR(-EINVAL
);
1138 type
= __nft_expr_type_get(family
, nla
);
1139 if (type
!= NULL
&& try_module_get(type
->owner
))
1142 #ifdef CONFIG_MODULES
1144 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1145 request_module("nft-expr-%u-%.*s", family
,
1146 nla_len(nla
), (char *)nla_data(nla
));
1147 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1148 if (__nft_expr_type_get(family
, nla
))
1149 return ERR_PTR(-EAGAIN
);
1151 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1152 request_module("nft-expr-%.*s",
1153 nla_len(nla
), (char *)nla_data(nla
));
1154 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1155 if (__nft_expr_type_get(family
, nla
))
1156 return ERR_PTR(-EAGAIN
);
1159 return ERR_PTR(-ENOENT
);
1162 static const struct nla_policy nft_expr_policy
[NFTA_EXPR_MAX
+ 1] = {
1163 [NFTA_EXPR_NAME
] = { .type
= NLA_STRING
},
1164 [NFTA_EXPR_DATA
] = { .type
= NLA_NESTED
},
1167 static int nf_tables_fill_expr_info(struct sk_buff
*skb
,
1168 const struct nft_expr
*expr
)
1170 if (nla_put_string(skb
, NFTA_EXPR_NAME
, expr
->ops
->type
->name
))
1171 goto nla_put_failure
;
1173 if (expr
->ops
->dump
) {
1174 struct nlattr
*data
= nla_nest_start(skb
, NFTA_EXPR_DATA
);
1176 goto nla_put_failure
;
1177 if (expr
->ops
->dump(skb
, expr
) < 0)
1178 goto nla_put_failure
;
1179 nla_nest_end(skb
, data
);
1188 struct nft_expr_info
{
1189 const struct nft_expr_ops
*ops
;
1190 struct nlattr
*tb
[NFT_EXPR_MAXATTR
+ 1];
1193 static int nf_tables_expr_parse(const struct nft_ctx
*ctx
,
1194 const struct nlattr
*nla
,
1195 struct nft_expr_info
*info
)
1197 const struct nft_expr_type
*type
;
1198 const struct nft_expr_ops
*ops
;
1199 struct nlattr
*tb
[NFTA_EXPR_MAX
+ 1];
1202 err
= nla_parse_nested(tb
, NFTA_EXPR_MAX
, nla
, nft_expr_policy
);
1206 type
= nft_expr_type_get(ctx
->afi
->family
, tb
[NFTA_EXPR_NAME
]);
1208 return PTR_ERR(type
);
1210 if (tb
[NFTA_EXPR_DATA
]) {
1211 err
= nla_parse_nested(info
->tb
, type
->maxattr
,
1212 tb
[NFTA_EXPR_DATA
], type
->policy
);
1216 memset(info
->tb
, 0, sizeof(info
->tb
[0]) * (type
->maxattr
+ 1));
1218 if (type
->select_ops
!= NULL
) {
1219 ops
= type
->select_ops(ctx
,
1220 (const struct nlattr
* const *)info
->tb
);
1232 module_put(type
->owner
);
1236 static int nf_tables_newexpr(const struct nft_ctx
*ctx
,
1237 const struct nft_expr_info
*info
,
1238 struct nft_expr
*expr
)
1240 const struct nft_expr_ops
*ops
= info
->ops
;
1245 err
= ops
->init(ctx
, expr
, (const struct nlattr
**)info
->tb
);
1257 static void nf_tables_expr_destroy(struct nft_expr
*expr
)
1259 if (expr
->ops
->destroy
)
1260 expr
->ops
->destroy(expr
);
1261 module_put(expr
->ops
->type
->owner
);
1268 static struct nft_rule
*__nf_tables_rule_lookup(const struct nft_chain
*chain
,
1271 struct nft_rule
*rule
;
1273 // FIXME: this sucks
1274 list_for_each_entry(rule
, &chain
->rules
, list
) {
1275 if (handle
== rule
->handle
)
1279 return ERR_PTR(-ENOENT
);
1282 static struct nft_rule
*nf_tables_rule_lookup(const struct nft_chain
*chain
,
1283 const struct nlattr
*nla
)
1286 return ERR_PTR(-EINVAL
);
1288 return __nf_tables_rule_lookup(chain
, be64_to_cpu(nla_get_be64(nla
)));
1291 static const struct nla_policy nft_rule_policy
[NFTA_RULE_MAX
+ 1] = {
1292 [NFTA_RULE_TABLE
] = { .type
= NLA_STRING
},
1293 [NFTA_RULE_CHAIN
] = { .type
= NLA_STRING
,
1294 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
1295 [NFTA_RULE_HANDLE
] = { .type
= NLA_U64
},
1296 [NFTA_RULE_EXPRESSIONS
] = { .type
= NLA_NESTED
},
1297 [NFTA_RULE_COMPAT
] = { .type
= NLA_NESTED
},
1298 [NFTA_RULE_POSITION
] = { .type
= NLA_U64
},
1301 static int nf_tables_fill_rule_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
1302 int event
, u32 flags
, int family
,
1303 const struct nft_table
*table
,
1304 const struct nft_chain
*chain
,
1305 const struct nft_rule
*rule
)
1307 struct nlmsghdr
*nlh
;
1308 struct nfgenmsg
*nfmsg
;
1309 const struct nft_expr
*expr
, *next
;
1310 struct nlattr
*list
;
1311 const struct nft_rule
*prule
;
1312 int type
= event
| NFNL_SUBSYS_NFTABLES
<< 8;
1314 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(struct nfgenmsg
),
1317 goto nla_put_failure
;
1319 nfmsg
= nlmsg_data(nlh
);
1320 nfmsg
->nfgen_family
= family
;
1321 nfmsg
->version
= NFNETLINK_V0
;
1324 if (nla_put_string(skb
, NFTA_RULE_TABLE
, table
->name
))
1325 goto nla_put_failure
;
1326 if (nla_put_string(skb
, NFTA_RULE_CHAIN
, chain
->name
))
1327 goto nla_put_failure
;
1328 if (nla_put_be64(skb
, NFTA_RULE_HANDLE
, cpu_to_be64(rule
->handle
)))
1329 goto nla_put_failure
;
1331 if ((event
!= NFT_MSG_DELRULE
) && (rule
->list
.prev
!= &chain
->rules
)) {
1332 prule
= list_entry(rule
->list
.prev
, struct nft_rule
, list
);
1333 if (nla_put_be64(skb
, NFTA_RULE_POSITION
,
1334 cpu_to_be64(prule
->handle
)))
1335 goto nla_put_failure
;
1338 list
= nla_nest_start(skb
, NFTA_RULE_EXPRESSIONS
);
1340 goto nla_put_failure
;
1341 nft_rule_for_each_expr(expr
, next
, rule
) {
1342 struct nlattr
*elem
= nla_nest_start(skb
, NFTA_LIST_ELEM
);
1344 goto nla_put_failure
;
1345 if (nf_tables_fill_expr_info(skb
, expr
) < 0)
1346 goto nla_put_failure
;
1347 nla_nest_end(skb
, elem
);
1349 nla_nest_end(skb
, list
);
1351 return nlmsg_end(skb
, nlh
);
1354 nlmsg_trim(skb
, nlh
);
1358 static int nf_tables_rule_notify(const struct sk_buff
*oskb
,
1359 const struct nlmsghdr
*nlh
,
1360 const struct nft_table
*table
,
1361 const struct nft_chain
*chain
,
1362 const struct nft_rule
*rule
,
1363 int event
, u32 flags
, int family
)
1365 struct sk_buff
*skb
;
1366 u32 portid
= NETLINK_CB(oskb
).portid
;
1367 struct net
*net
= oskb
? sock_net(oskb
->sk
) : &init_net
;
1368 u32 seq
= nlh
->nlmsg_seq
;
1372 report
= nlmsg_report(nlh
);
1373 if (!report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
1377 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1381 err
= nf_tables_fill_rule_info(skb
, portid
, seq
, event
, flags
,
1382 family
, table
, chain
, rule
);
1388 err
= nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, report
,
1392 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, err
);
1397 nft_rule_is_active(struct net
*net
, const struct nft_rule
*rule
)
1399 return (rule
->genmask
& (1 << net
->nft
.gencursor
)) == 0;
1402 static inline int gencursor_next(struct net
*net
)
1404 return net
->nft
.gencursor
+1 == 1 ? 1 : 0;
1408 nft_rule_is_active_next(struct net
*net
, const struct nft_rule
*rule
)
1410 return (rule
->genmask
& (1 << gencursor_next(net
))) == 0;
1414 nft_rule_activate_next(struct net
*net
, struct nft_rule
*rule
)
1416 /* Now inactive, will be active in the future */
1417 rule
->genmask
= (1 << net
->nft
.gencursor
);
1421 nft_rule_disactivate_next(struct net
*net
, struct nft_rule
*rule
)
1423 rule
->genmask
= (1 << gencursor_next(net
));
1426 static inline void nft_rule_clear(struct net
*net
, struct nft_rule
*rule
)
1431 static int nf_tables_dump_rules(struct sk_buff
*skb
,
1432 struct netlink_callback
*cb
)
1434 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1435 const struct nft_af_info
*afi
;
1436 const struct nft_table
*table
;
1437 const struct nft_chain
*chain
;
1438 const struct nft_rule
*rule
;
1439 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1440 struct net
*net
= sock_net(skb
->sk
);
1441 int family
= nfmsg
->nfgen_family
;
1442 u8 genctr
= ACCESS_ONCE(net
->nft
.genctr
);
1443 u8 gencursor
= ACCESS_ONCE(net
->nft
.gencursor
);
1445 list_for_each_entry(afi
, &net
->nft
.af_info
, list
) {
1446 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
1449 list_for_each_entry(table
, &afi
->tables
, list
) {
1450 list_for_each_entry(chain
, &table
->chains
, list
) {
1451 list_for_each_entry(rule
, &chain
->rules
, list
) {
1452 if (!nft_rule_is_active(net
, rule
))
1457 memset(&cb
->args
[1], 0,
1458 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1459 if (nf_tables_fill_rule_info(skb
, NETLINK_CB(cb
->skb
).portid
,
1462 NLM_F_MULTI
| NLM_F_APPEND
,
1463 afi
->family
, table
, chain
, rule
) < 0)
1472 /* Invalidate this dump, a transition to the new generation happened */
1473 if (gencursor
!= net
->nft
.gencursor
|| genctr
!= net
->nft
.genctr
)
1480 static int nf_tables_getrule(struct sock
*nlsk
, struct sk_buff
*skb
,
1481 const struct nlmsghdr
*nlh
,
1482 const struct nlattr
* const nla
[])
1484 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1485 const struct nft_af_info
*afi
;
1486 const struct nft_table
*table
;
1487 const struct nft_chain
*chain
;
1488 const struct nft_rule
*rule
;
1489 struct sk_buff
*skb2
;
1490 struct net
*net
= sock_net(skb
->sk
);
1491 int family
= nfmsg
->nfgen_family
;
1494 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1495 struct netlink_dump_control c
= {
1496 .dump
= nf_tables_dump_rules
,
1498 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
1501 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1503 return PTR_ERR(afi
);
1505 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
1507 return PTR_ERR(table
);
1509 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
1511 return PTR_ERR(chain
);
1513 rule
= nf_tables_rule_lookup(chain
, nla
[NFTA_RULE_HANDLE
]);
1515 return PTR_ERR(rule
);
1517 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1521 err
= nf_tables_fill_rule_info(skb2
, NETLINK_CB(skb
).portid
,
1522 nlh
->nlmsg_seq
, NFT_MSG_NEWRULE
, 0,
1523 family
, table
, chain
, rule
);
1527 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
1534 static void nf_tables_rule_destroy(struct nft_rule
*rule
)
1536 struct nft_expr
*expr
;
1539 * Careful: some expressions might not be initialized in case this
1540 * is called on error from nf_tables_newrule().
1542 expr
= nft_expr_first(rule
);
1543 while (expr
->ops
&& expr
!= nft_expr_last(rule
)) {
1544 nf_tables_expr_destroy(expr
);
1545 expr
= nft_expr_next(expr
);
1550 #define NFT_RULE_MAXEXPRS 128
1552 static struct nft_expr_info
*info
;
1554 static struct nft_rule_trans
*
1555 nf_tables_trans_add(struct nft_rule
*rule
, const struct nft_ctx
*ctx
)
1557 struct nft_rule_trans
*rupd
;
1559 rupd
= kmalloc(sizeof(struct nft_rule_trans
), GFP_KERNEL
);
1563 rupd
->chain
= ctx
->chain
;
1564 rupd
->table
= ctx
->table
;
1566 rupd
->family
= ctx
->afi
->family
;
1567 rupd
->nlh
= ctx
->nlh
;
1568 list_add_tail(&rupd
->list
, &ctx
->net
->nft
.commit_list
);
1573 static int nf_tables_newrule(struct sock
*nlsk
, struct sk_buff
*skb
,
1574 const struct nlmsghdr
*nlh
,
1575 const struct nlattr
* const nla
[])
1577 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1578 const struct nft_af_info
*afi
;
1579 struct net
*net
= sock_net(skb
->sk
);
1580 struct nft_table
*table
;
1581 struct nft_chain
*chain
;
1582 struct nft_rule
*rule
, *old_rule
= NULL
;
1583 struct nft_rule_trans
*repl
= NULL
;
1584 struct nft_expr
*expr
;
1587 unsigned int size
, i
, n
;
1590 u64 handle
, pos_handle
;
1592 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
1594 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, create
);
1596 return PTR_ERR(afi
);
1598 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
1600 return PTR_ERR(table
);
1602 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
1604 return PTR_ERR(chain
);
1606 if (nla
[NFTA_RULE_HANDLE
]) {
1607 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_HANDLE
]));
1608 rule
= __nf_tables_rule_lookup(chain
, handle
);
1610 return PTR_ERR(rule
);
1612 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
1614 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1619 if (!create
|| nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1621 handle
= nf_tables_alloc_handle(table
);
1624 if (nla
[NFTA_RULE_POSITION
]) {
1625 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
1628 pos_handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_POSITION
]));
1629 old_rule
= __nf_tables_rule_lookup(chain
, pos_handle
);
1630 if (IS_ERR(old_rule
))
1631 return PTR_ERR(old_rule
);
1634 nft_ctx_init(&ctx
, skb
, nlh
, afi
, table
, chain
, nla
);
1638 if (nla
[NFTA_RULE_EXPRESSIONS
]) {
1639 nla_for_each_nested(tmp
, nla
[NFTA_RULE_EXPRESSIONS
], rem
) {
1641 if (nla_type(tmp
) != NFTA_LIST_ELEM
)
1643 if (n
== NFT_RULE_MAXEXPRS
)
1645 err
= nf_tables_expr_parse(&ctx
, tmp
, &info
[n
]);
1648 size
+= info
[n
].ops
->size
;
1654 rule
= kzalloc(sizeof(*rule
) + size
, GFP_KERNEL
);
1658 nft_rule_activate_next(net
, rule
);
1660 rule
->handle
= handle
;
1663 expr
= nft_expr_first(rule
);
1664 for (i
= 0; i
< n
; i
++) {
1665 err
= nf_tables_newexpr(&ctx
, &info
[i
], expr
);
1669 expr
= nft_expr_next(expr
);
1672 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
1673 if (nft_rule_is_active_next(net
, old_rule
)) {
1674 repl
= nf_tables_trans_add(old_rule
, &ctx
);
1679 nft_rule_disactivate_next(net
, old_rule
);
1680 list_add_tail(&rule
->list
, &old_rule
->list
);
1685 } else if (nlh
->nlmsg_flags
& NLM_F_APPEND
)
1687 list_add_rcu(&rule
->list
, &old_rule
->list
);
1689 list_add_tail_rcu(&rule
->list
, &chain
->rules
);
1692 list_add_tail_rcu(&rule
->list
, &old_rule
->list
);
1694 list_add_rcu(&rule
->list
, &chain
->rules
);
1697 if (nf_tables_trans_add(rule
, &ctx
) == NULL
) {
1704 list_del_rcu(&rule
->list
);
1706 list_del_rcu(&repl
->rule
->list
);
1707 list_del(&repl
->list
);
1708 nft_rule_clear(net
, repl
->rule
);
1712 nf_tables_rule_destroy(rule
);
1714 for (i
= 0; i
< n
; i
++) {
1715 if (info
[i
].ops
!= NULL
)
1716 module_put(info
[i
].ops
->type
->owner
);
1722 nf_tables_delrule_one(struct nft_ctx
*ctx
, struct nft_rule
*rule
)
1724 /* You cannot delete the same rule twice */
1725 if (nft_rule_is_active_next(ctx
->net
, rule
)) {
1726 if (nf_tables_trans_add(rule
, ctx
) == NULL
)
1728 nft_rule_disactivate_next(ctx
->net
, rule
);
1734 static int nf_table_delrule_by_chain(struct nft_ctx
*ctx
)
1736 struct nft_rule
*rule
;
1739 list_for_each_entry(rule
, &ctx
->chain
->rules
, list
) {
1740 err
= nf_tables_delrule_one(ctx
, rule
);
1747 static int nf_tables_delrule(struct sock
*nlsk
, struct sk_buff
*skb
,
1748 const struct nlmsghdr
*nlh
,
1749 const struct nlattr
* const nla
[])
1751 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1752 const struct nft_af_info
*afi
;
1753 struct net
*net
= sock_net(skb
->sk
);
1754 const struct nft_table
*table
;
1755 struct nft_chain
*chain
= NULL
;
1756 struct nft_rule
*rule
;
1757 int family
= nfmsg
->nfgen_family
, err
= 0;
1760 afi
= nf_tables_afinfo_lookup(net
, family
, false);
1762 return PTR_ERR(afi
);
1764 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
1766 return PTR_ERR(table
);
1768 if (nla
[NFTA_RULE_CHAIN
]) {
1769 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
1771 return PTR_ERR(chain
);
1774 nft_ctx_init(&ctx
, skb
, nlh
, afi
, table
, chain
, nla
);
1777 if (nla
[NFTA_RULE_HANDLE
]) {
1778 rule
= nf_tables_rule_lookup(chain
,
1779 nla
[NFTA_RULE_HANDLE
]);
1781 return PTR_ERR(rule
);
1783 err
= nf_tables_delrule_one(&ctx
, rule
);
1785 err
= nf_table_delrule_by_chain(&ctx
);
1788 list_for_each_entry(chain
, &table
->chains
, list
) {
1790 err
= nf_table_delrule_by_chain(&ctx
);
1799 static int nf_tables_commit(struct sk_buff
*skb
)
1801 struct net
*net
= sock_net(skb
->sk
);
1802 struct nft_rule_trans
*rupd
, *tmp
;
1804 /* Bump generation counter, invalidate any dump in progress */
1807 /* A new generation has just started */
1808 net
->nft
.gencursor
= gencursor_next(net
);
1810 /* Make sure all packets have left the previous generation before
1811 * purging old rules.
1815 list_for_each_entry_safe(rupd
, tmp
, &net
->nft
.commit_list
, list
) {
1816 /* This rule was inactive in the past and just became active.
1817 * Clear the next bit of the genmask since its meaning has
1818 * changed, now it is the future.
1820 if (nft_rule_is_active(net
, rupd
->rule
)) {
1821 nft_rule_clear(net
, rupd
->rule
);
1822 nf_tables_rule_notify(skb
, rupd
->nlh
, rupd
->table
,
1823 rupd
->chain
, rupd
->rule
,
1826 list_del(&rupd
->list
);
1831 /* This rule is in the past, get rid of it */
1832 list_del_rcu(&rupd
->rule
->list
);
1833 nf_tables_rule_notify(skb
, rupd
->nlh
, rupd
->table
, rupd
->chain
,
1834 rupd
->rule
, NFT_MSG_DELRULE
, 0,
1838 /* Make sure we don't see any packet traversing old rules */
1841 /* Now we can safely release unused old rules */
1842 list_for_each_entry_safe(rupd
, tmp
, &net
->nft
.commit_list
, list
) {
1843 nf_tables_rule_destroy(rupd
->rule
);
1844 list_del(&rupd
->list
);
1851 static int nf_tables_abort(struct sk_buff
*skb
)
1853 struct net
*net
= sock_net(skb
->sk
);
1854 struct nft_rule_trans
*rupd
, *tmp
;
1856 list_for_each_entry_safe(rupd
, tmp
, &net
->nft
.commit_list
, list
) {
1857 if (!nft_rule_is_active_next(net
, rupd
->rule
)) {
1858 nft_rule_clear(net
, rupd
->rule
);
1859 list_del(&rupd
->list
);
1864 /* This rule is inactive, get rid of it */
1865 list_del_rcu(&rupd
->rule
->list
);
1868 /* Make sure we don't see any packet accessing aborted rules */
1871 list_for_each_entry_safe(rupd
, tmp
, &net
->nft
.commit_list
, list
) {
1872 nf_tables_rule_destroy(rupd
->rule
);
1873 list_del(&rupd
->list
);
1884 static LIST_HEAD(nf_tables_set_ops
);
1886 int nft_register_set(struct nft_set_ops
*ops
)
1888 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1889 list_add_tail(&ops
->list
, &nf_tables_set_ops
);
1890 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1893 EXPORT_SYMBOL_GPL(nft_register_set
);
1895 void nft_unregister_set(struct nft_set_ops
*ops
)
1897 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1898 list_del(&ops
->list
);
1899 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1901 EXPORT_SYMBOL_GPL(nft_unregister_set
);
1903 static const struct nft_set_ops
*nft_select_set_ops(const struct nlattr
* const nla
[])
1905 const struct nft_set_ops
*ops
;
1908 #ifdef CONFIG_MODULES
1909 if (list_empty(&nf_tables_set_ops
)) {
1910 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1911 request_module("nft-set");
1912 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1913 if (!list_empty(&nf_tables_set_ops
))
1914 return ERR_PTR(-EAGAIN
);
1918 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
1919 features
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
1920 features
&= NFT_SET_INTERVAL
| NFT_SET_MAP
;
1923 // FIXME: implement selection properly
1924 list_for_each_entry(ops
, &nf_tables_set_ops
, list
) {
1925 if ((ops
->features
& features
) != features
)
1927 if (!try_module_get(ops
->owner
))
1932 return ERR_PTR(-EOPNOTSUPP
);
1935 static const struct nla_policy nft_set_policy
[NFTA_SET_MAX
+ 1] = {
1936 [NFTA_SET_TABLE
] = { .type
= NLA_STRING
},
1937 [NFTA_SET_NAME
] = { .type
= NLA_STRING
},
1938 [NFTA_SET_FLAGS
] = { .type
= NLA_U32
},
1939 [NFTA_SET_KEY_TYPE
] = { .type
= NLA_U32
},
1940 [NFTA_SET_KEY_LEN
] = { .type
= NLA_U32
},
1941 [NFTA_SET_DATA_TYPE
] = { .type
= NLA_U32
},
1942 [NFTA_SET_DATA_LEN
] = { .type
= NLA_U32
},
1945 static int nft_ctx_init_from_setattr(struct nft_ctx
*ctx
,
1946 const struct sk_buff
*skb
,
1947 const struct nlmsghdr
*nlh
,
1948 const struct nlattr
* const nla
[])
1950 struct net
*net
= sock_net(skb
->sk
);
1951 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1952 const struct nft_af_info
*afi
= NULL
;
1953 const struct nft_table
*table
= NULL
;
1955 if (nfmsg
->nfgen_family
!= NFPROTO_UNSPEC
) {
1956 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, false);
1958 return PTR_ERR(afi
);
1961 if (nla
[NFTA_SET_TABLE
] != NULL
) {
1963 return -EAFNOSUPPORT
;
1965 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
]);
1967 return PTR_ERR(table
);
1970 nft_ctx_init(ctx
, skb
, nlh
, afi
, table
, NULL
, nla
);
1974 struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
1975 const struct nlattr
*nla
)
1977 struct nft_set
*set
;
1980 return ERR_PTR(-EINVAL
);
1982 list_for_each_entry(set
, &table
->sets
, list
) {
1983 if (!nla_strcmp(nla
, set
->name
))
1986 return ERR_PTR(-ENOENT
);
1989 static int nf_tables_set_alloc_name(struct nft_ctx
*ctx
, struct nft_set
*set
,
1992 const struct nft_set
*i
;
1994 unsigned long *inuse
;
1997 p
= strnchr(name
, IFNAMSIZ
, '%');
1999 if (p
[1] != 'd' || strchr(p
+ 2, '%'))
2002 inuse
= (unsigned long *)get_zeroed_page(GFP_KERNEL
);
2006 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
2009 if (!sscanf(i
->name
, name
, &tmp
))
2011 if (tmp
< 0 || tmp
>= BITS_PER_BYTE
* PAGE_SIZE
)
2014 set_bit(tmp
, inuse
);
2017 n
= find_first_zero_bit(inuse
, BITS_PER_BYTE
* PAGE_SIZE
);
2018 free_page((unsigned long)inuse
);
2021 snprintf(set
->name
, sizeof(set
->name
), name
, n
);
2022 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
2023 if (!strcmp(set
->name
, i
->name
))
2029 static int nf_tables_fill_set(struct sk_buff
*skb
, const struct nft_ctx
*ctx
,
2030 const struct nft_set
*set
, u16 event
, u16 flags
)
2032 struct nfgenmsg
*nfmsg
;
2033 struct nlmsghdr
*nlh
;
2034 u32 portid
= NETLINK_CB(ctx
->skb
).portid
;
2035 u32 seq
= ctx
->nlh
->nlmsg_seq
;
2037 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
2038 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
2041 goto nla_put_failure
;
2043 nfmsg
= nlmsg_data(nlh
);
2044 nfmsg
->nfgen_family
= ctx
->afi
->family
;
2045 nfmsg
->version
= NFNETLINK_V0
;
2048 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
2049 goto nla_put_failure
;
2050 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
2051 goto nla_put_failure
;
2052 if (set
->flags
!= 0)
2053 if (nla_put_be32(skb
, NFTA_SET_FLAGS
, htonl(set
->flags
)))
2054 goto nla_put_failure
;
2056 if (nla_put_be32(skb
, NFTA_SET_KEY_TYPE
, htonl(set
->ktype
)))
2057 goto nla_put_failure
;
2058 if (nla_put_be32(skb
, NFTA_SET_KEY_LEN
, htonl(set
->klen
)))
2059 goto nla_put_failure
;
2060 if (set
->flags
& NFT_SET_MAP
) {
2061 if (nla_put_be32(skb
, NFTA_SET_DATA_TYPE
, htonl(set
->dtype
)))
2062 goto nla_put_failure
;
2063 if (nla_put_be32(skb
, NFTA_SET_DATA_LEN
, htonl(set
->dlen
)))
2064 goto nla_put_failure
;
2067 return nlmsg_end(skb
, nlh
);
2070 nlmsg_trim(skb
, nlh
);
2074 static int nf_tables_set_notify(const struct nft_ctx
*ctx
,
2075 const struct nft_set
*set
,
2078 struct sk_buff
*skb
;
2079 u32 portid
= NETLINK_CB(ctx
->skb
).portid
;
2083 report
= nlmsg_report(ctx
->nlh
);
2084 if (!report
&& !nfnetlink_has_listeners(ctx
->net
, NFNLGRP_NFTABLES
))
2088 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
2092 err
= nf_tables_fill_set(skb
, ctx
, set
, event
, 0);
2098 err
= nfnetlink_send(skb
, ctx
->net
, portid
, NFNLGRP_NFTABLES
, report
,
2102 nfnetlink_set_err(ctx
->net
, portid
, NFNLGRP_NFTABLES
, err
);
2106 static int nf_tables_dump_sets_table(struct nft_ctx
*ctx
, struct sk_buff
*skb
,
2107 struct netlink_callback
*cb
)
2109 const struct nft_set
*set
;
2110 unsigned int idx
= 0, s_idx
= cb
->args
[0];
2115 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
2118 if (nf_tables_fill_set(skb
, ctx
, set
, NFT_MSG_NEWSET
,
2131 static int nf_tables_dump_sets_family(struct nft_ctx
*ctx
, struct sk_buff
*skb
,
2132 struct netlink_callback
*cb
)
2134 const struct nft_set
*set
;
2135 unsigned int idx
, s_idx
= cb
->args
[0];
2136 struct nft_table
*table
, *cur_table
= (struct nft_table
*)cb
->args
[2];
2141 list_for_each_entry(table
, &ctx
->afi
->tables
, list
) {
2143 if (cur_table
!= table
)
2150 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
2153 if (nf_tables_fill_set(skb
, ctx
, set
, NFT_MSG_NEWSET
,
2156 cb
->args
[2] = (unsigned long) table
;
2168 static int nf_tables_dump_sets_all(struct nft_ctx
*ctx
, struct sk_buff
*skb
,
2169 struct netlink_callback
*cb
)
2171 const struct nft_set
*set
;
2172 unsigned int idx
, s_idx
= cb
->args
[0];
2173 const struct nft_af_info
*afi
;
2174 struct nft_table
*table
, *cur_table
= (struct nft_table
*)cb
->args
[2];
2175 struct net
*net
= sock_net(skb
->sk
);
2176 int cur_family
= cb
->args
[3];
2181 list_for_each_entry(afi
, &net
->nft
.af_info
, list
) {
2183 if (afi
->family
!= cur_family
)
2189 list_for_each_entry(table
, &afi
->tables
, list
) {
2191 if (cur_table
!= table
)
2200 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
2203 if (nf_tables_fill_set(skb
, ctx
, set
,
2207 cb
->args
[2] = (unsigned long) table
;
2208 cb
->args
[3] = afi
->family
;
2223 static int nf_tables_dump_sets(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2225 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
2226 struct nlattr
*nla
[NFTA_SET_MAX
+ 1];
2230 err
= nlmsg_parse(cb
->nlh
, sizeof(*nfmsg
), nla
, NFTA_SET_MAX
,
2235 err
= nft_ctx_init_from_setattr(&ctx
, cb
->skb
, cb
->nlh
, (void *)nla
);
2239 if (ctx
.table
== NULL
) {
2240 if (ctx
.afi
== NULL
)
2241 ret
= nf_tables_dump_sets_all(&ctx
, skb
, cb
);
2243 ret
= nf_tables_dump_sets_family(&ctx
, skb
, cb
);
2245 ret
= nf_tables_dump_sets_table(&ctx
, skb
, cb
);
2250 static int nf_tables_getset(struct sock
*nlsk
, struct sk_buff
*skb
,
2251 const struct nlmsghdr
*nlh
,
2252 const struct nlattr
* const nla
[])
2254 const struct nft_set
*set
;
2256 struct sk_buff
*skb2
;
2257 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2260 /* Verify existance before starting dump */
2261 err
= nft_ctx_init_from_setattr(&ctx
, skb
, nlh
, nla
);
2265 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2266 struct netlink_dump_control c
= {
2267 .dump
= nf_tables_dump_sets
,
2269 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
2272 /* Only accept unspec with dump */
2273 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
2274 return -EAFNOSUPPORT
;
2276 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
]);
2278 return PTR_ERR(set
);
2280 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2284 err
= nf_tables_fill_set(skb2
, &ctx
, set
, NFT_MSG_NEWSET
, 0);
2288 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
2295 static int nf_tables_newset(struct sock
*nlsk
, struct sk_buff
*skb
,
2296 const struct nlmsghdr
*nlh
,
2297 const struct nlattr
* const nla
[])
2299 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2300 const struct nft_set_ops
*ops
;
2301 const struct nft_af_info
*afi
;
2302 struct net
*net
= sock_net(skb
->sk
);
2303 struct nft_table
*table
;
2304 struct nft_set
*set
;
2306 char name
[IFNAMSIZ
];
2309 u32 ktype
, klen
, dlen
, dtype
, flags
;
2312 if (nla
[NFTA_SET_TABLE
] == NULL
||
2313 nla
[NFTA_SET_NAME
] == NULL
||
2314 nla
[NFTA_SET_KEY_LEN
] == NULL
)
2317 ktype
= NFT_DATA_VALUE
;
2318 if (nla
[NFTA_SET_KEY_TYPE
] != NULL
) {
2319 ktype
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_TYPE
]));
2320 if ((ktype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
)
2324 klen
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_LEN
]));
2325 if (klen
== 0 || klen
> FIELD_SIZEOF(struct nft_data
, data
))
2329 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
2330 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
2331 if (flags
& ~(NFT_SET_ANONYMOUS
| NFT_SET_CONSTANT
|
2332 NFT_SET_INTERVAL
| NFT_SET_MAP
))
2338 if (nla
[NFTA_SET_DATA_TYPE
] != NULL
) {
2339 if (!(flags
& NFT_SET_MAP
))
2342 dtype
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_TYPE
]));
2343 if ((dtype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
&&
2344 dtype
!= NFT_DATA_VERDICT
)
2347 if (dtype
!= NFT_DATA_VERDICT
) {
2348 if (nla
[NFTA_SET_DATA_LEN
] == NULL
)
2350 dlen
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_LEN
]));
2352 dlen
> FIELD_SIZEOF(struct nft_data
, data
))
2355 dlen
= sizeof(struct nft_data
);
2356 } else if (flags
& NFT_SET_MAP
)
2359 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
2361 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, create
);
2363 return PTR_ERR(afi
);
2365 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
]);
2367 return PTR_ERR(table
);
2369 nft_ctx_init(&ctx
, skb
, nlh
, afi
, table
, NULL
, nla
);
2371 set
= nf_tables_set_lookup(table
, nla
[NFTA_SET_NAME
]);
2373 if (PTR_ERR(set
) != -ENOENT
)
2374 return PTR_ERR(set
);
2379 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
2381 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
2386 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
2389 ops
= nft_select_set_ops(nla
);
2391 return PTR_ERR(ops
);
2394 if (ops
->privsize
!= NULL
)
2395 size
= ops
->privsize(nla
);
2398 set
= kzalloc(sizeof(*set
) + size
, GFP_KERNEL
);
2402 nla_strlcpy(name
, nla
[NFTA_SET_NAME
], sizeof(set
->name
));
2403 err
= nf_tables_set_alloc_name(&ctx
, set
, name
);
2407 INIT_LIST_HEAD(&set
->bindings
);
2415 err
= ops
->init(set
, nla
);
2419 list_add_tail(&set
->list
, &table
->sets
);
2420 nf_tables_set_notify(&ctx
, set
, NFT_MSG_NEWSET
);
2426 module_put(ops
->owner
);
2430 static void nf_tables_set_destroy(const struct nft_ctx
*ctx
, struct nft_set
*set
)
2432 list_del(&set
->list
);
2433 if (!(set
->flags
& NFT_SET_ANONYMOUS
))
2434 nf_tables_set_notify(ctx
, set
, NFT_MSG_DELSET
);
2436 set
->ops
->destroy(set
);
2437 module_put(set
->ops
->owner
);
2441 static int nf_tables_delset(struct sock
*nlsk
, struct sk_buff
*skb
,
2442 const struct nlmsghdr
*nlh
,
2443 const struct nlattr
* const nla
[])
2445 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2446 struct nft_set
*set
;
2450 if (nfmsg
->nfgen_family
== NFPROTO_UNSPEC
)
2451 return -EAFNOSUPPORT
;
2452 if (nla
[NFTA_SET_TABLE
] == NULL
)
2455 err
= nft_ctx_init_from_setattr(&ctx
, skb
, nlh
, nla
);
2459 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
]);
2461 return PTR_ERR(set
);
2462 if (!list_empty(&set
->bindings
))
2465 nf_tables_set_destroy(&ctx
, set
);
2469 static int nf_tables_bind_check_setelem(const struct nft_ctx
*ctx
,
2470 const struct nft_set
*set
,
2471 const struct nft_set_iter
*iter
,
2472 const struct nft_set_elem
*elem
)
2474 enum nft_registers dreg
;
2476 dreg
= nft_type_to_reg(set
->dtype
);
2477 return nft_validate_data_load(ctx
, dreg
, &elem
->data
,
2478 set
->dtype
== NFT_DATA_VERDICT
?
2479 NFT_DATA_VERDICT
: NFT_DATA_VALUE
);
2482 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2483 struct nft_set_binding
*binding
)
2485 struct nft_set_binding
*i
;
2486 struct nft_set_iter iter
;
2488 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
)
2491 if (set
->flags
& NFT_SET_MAP
) {
2492 /* If the set is already bound to the same chain all
2493 * jumps are already validated for that chain.
2495 list_for_each_entry(i
, &set
->bindings
, list
) {
2496 if (i
->chain
== binding
->chain
)
2503 iter
.fn
= nf_tables_bind_check_setelem
;
2505 set
->ops
->walk(ctx
, set
, &iter
);
2507 /* Destroy anonymous sets if binding fails */
2508 if (set
->flags
& NFT_SET_ANONYMOUS
)
2509 nf_tables_set_destroy(ctx
, set
);
2515 binding
->chain
= ctx
->chain
;
2516 list_add_tail(&binding
->list
, &set
->bindings
);
2520 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2521 struct nft_set_binding
*binding
)
2523 list_del(&binding
->list
);
2525 if (list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
)
2526 nf_tables_set_destroy(ctx
, set
);
2533 static const struct nla_policy nft_set_elem_policy
[NFTA_SET_ELEM_MAX
+ 1] = {
2534 [NFTA_SET_ELEM_KEY
] = { .type
= NLA_NESTED
},
2535 [NFTA_SET_ELEM_DATA
] = { .type
= NLA_NESTED
},
2536 [NFTA_SET_ELEM_FLAGS
] = { .type
= NLA_U32
},
2539 static const struct nla_policy nft_set_elem_list_policy
[NFTA_SET_ELEM_LIST_MAX
+ 1] = {
2540 [NFTA_SET_ELEM_LIST_TABLE
] = { .type
= NLA_STRING
},
2541 [NFTA_SET_ELEM_LIST_SET
] = { .type
= NLA_STRING
},
2542 [NFTA_SET_ELEM_LIST_ELEMENTS
] = { .type
= NLA_NESTED
},
2545 static int nft_ctx_init_from_elemattr(struct nft_ctx
*ctx
,
2546 const struct sk_buff
*skb
,
2547 const struct nlmsghdr
*nlh
,
2548 const struct nlattr
* const nla
[])
2550 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2551 const struct nft_af_info
*afi
;
2552 const struct nft_table
*table
;
2553 struct net
*net
= sock_net(skb
->sk
);
2555 afi
= nf_tables_afinfo_lookup(net
, nfmsg
->nfgen_family
, false);
2557 return PTR_ERR(afi
);
2559 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_ELEM_LIST_TABLE
]);
2561 return PTR_ERR(table
);
2563 nft_ctx_init(ctx
, skb
, nlh
, afi
, table
, NULL
, nla
);
2567 static int nf_tables_fill_setelem(struct sk_buff
*skb
,
2568 const struct nft_set
*set
,
2569 const struct nft_set_elem
*elem
)
2571 unsigned char *b
= skb_tail_pointer(skb
);
2572 struct nlattr
*nest
;
2574 nest
= nla_nest_start(skb
, NFTA_LIST_ELEM
);
2576 goto nla_put_failure
;
2578 if (nft_data_dump(skb
, NFTA_SET_ELEM_KEY
, &elem
->key
, NFT_DATA_VALUE
,
2580 goto nla_put_failure
;
2582 if (set
->flags
& NFT_SET_MAP
&&
2583 !(elem
->flags
& NFT_SET_ELEM_INTERVAL_END
) &&
2584 nft_data_dump(skb
, NFTA_SET_ELEM_DATA
, &elem
->data
,
2585 set
->dtype
== NFT_DATA_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
2587 goto nla_put_failure
;
2589 if (elem
->flags
!= 0)
2590 if (nla_put_be32(skb
, NFTA_SET_ELEM_FLAGS
, htonl(elem
->flags
)))
2591 goto nla_put_failure
;
2593 nla_nest_end(skb
, nest
);
2601 struct nft_set_dump_args
{
2602 const struct netlink_callback
*cb
;
2603 struct nft_set_iter iter
;
2604 struct sk_buff
*skb
;
2607 static int nf_tables_dump_setelem(const struct nft_ctx
*ctx
,
2608 const struct nft_set
*set
,
2609 const struct nft_set_iter
*iter
,
2610 const struct nft_set_elem
*elem
)
2612 struct nft_set_dump_args
*args
;
2614 args
= container_of(iter
, struct nft_set_dump_args
, iter
);
2615 return nf_tables_fill_setelem(args
->skb
, set
, elem
);
2618 static int nf_tables_dump_set(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2620 const struct nft_set
*set
;
2621 struct nft_set_dump_args args
;
2623 struct nlattr
*nla
[NFTA_SET_ELEM_LIST_MAX
+ 1];
2624 struct nfgenmsg
*nfmsg
;
2625 struct nlmsghdr
*nlh
;
2626 struct nlattr
*nest
;
2630 err
= nlmsg_parse(cb
->nlh
, sizeof(struct nfgenmsg
), nla
,
2631 NFTA_SET_ELEM_LIST_MAX
, nft_set_elem_list_policy
);
2635 err
= nft_ctx_init_from_elemattr(&ctx
, cb
->skb
, cb
->nlh
, (void *)nla
);
2639 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
2641 return PTR_ERR(set
);
2643 event
= NFT_MSG_NEWSETELEM
;
2644 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
2645 portid
= NETLINK_CB(cb
->skb
).portid
;
2646 seq
= cb
->nlh
->nlmsg_seq
;
2648 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
2651 goto nla_put_failure
;
2653 nfmsg
= nlmsg_data(nlh
);
2654 nfmsg
->nfgen_family
= NFPROTO_UNSPEC
;
2655 nfmsg
->version
= NFNETLINK_V0
;
2658 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_TABLE
, ctx
.table
->name
))
2659 goto nla_put_failure
;
2660 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_SET
, set
->name
))
2661 goto nla_put_failure
;
2663 nest
= nla_nest_start(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
2665 goto nla_put_failure
;
2669 args
.iter
.skip
= cb
->args
[0];
2670 args
.iter
.count
= 0;
2672 args
.iter
.fn
= nf_tables_dump_setelem
;
2673 set
->ops
->walk(&ctx
, set
, &args
.iter
);
2675 nla_nest_end(skb
, nest
);
2676 nlmsg_end(skb
, nlh
);
2678 if (args
.iter
.err
&& args
.iter
.err
!= -EMSGSIZE
)
2679 return args
.iter
.err
;
2680 if (args
.iter
.count
== cb
->args
[0])
2683 cb
->args
[0] = args
.iter
.count
;
2690 static int nf_tables_getsetelem(struct sock
*nlsk
, struct sk_buff
*skb
,
2691 const struct nlmsghdr
*nlh
,
2692 const struct nlattr
* const nla
[])
2694 const struct nft_set
*set
;
2698 err
= nft_ctx_init_from_elemattr(&ctx
, skb
, nlh
, nla
);
2702 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
2704 return PTR_ERR(set
);
2706 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2707 struct netlink_dump_control c
= {
2708 .dump
= nf_tables_dump_set
,
2710 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
2715 static int nft_add_set_elem(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2716 const struct nlattr
*attr
)
2718 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
2719 struct nft_data_desc d1
, d2
;
2720 struct nft_set_elem elem
;
2721 struct nft_set_binding
*binding
;
2722 enum nft_registers dreg
;
2725 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
2726 nft_set_elem_policy
);
2730 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
2734 if (nla
[NFTA_SET_ELEM_FLAGS
] != NULL
) {
2735 elem
.flags
= ntohl(nla_get_be32(nla
[NFTA_SET_ELEM_FLAGS
]));
2736 if (elem
.flags
& ~NFT_SET_ELEM_INTERVAL_END
)
2740 if (set
->flags
& NFT_SET_MAP
) {
2741 if (nla
[NFTA_SET_ELEM_DATA
] == NULL
&&
2742 !(elem
.flags
& NFT_SET_ELEM_INTERVAL_END
))
2744 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
&&
2745 elem
.flags
& NFT_SET_ELEM_INTERVAL_END
)
2748 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
2752 err
= nft_data_init(ctx
, &elem
.key
, &d1
, nla
[NFTA_SET_ELEM_KEY
]);
2756 if (d1
.type
!= NFT_DATA_VALUE
|| d1
.len
!= set
->klen
)
2760 if (set
->ops
->get(set
, &elem
) == 0)
2763 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
) {
2764 err
= nft_data_init(ctx
, &elem
.data
, &d2
, nla
[NFTA_SET_ELEM_DATA
]);
2769 if (set
->dtype
!= NFT_DATA_VERDICT
&& d2
.len
!= set
->dlen
)
2772 dreg
= nft_type_to_reg(set
->dtype
);
2773 list_for_each_entry(binding
, &set
->bindings
, list
) {
2774 struct nft_ctx bind_ctx
= {
2776 .table
= ctx
->table
,
2777 .chain
= binding
->chain
,
2780 err
= nft_validate_data_load(&bind_ctx
, dreg
,
2781 &elem
.data
, d2
.type
);
2787 err
= set
->ops
->insert(set
, &elem
);
2794 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
2795 nft_data_uninit(&elem
.data
, d2
.type
);
2797 nft_data_uninit(&elem
.key
, d1
.type
);
2802 static int nf_tables_newsetelem(struct sock
*nlsk
, struct sk_buff
*skb
,
2803 const struct nlmsghdr
*nlh
,
2804 const struct nlattr
* const nla
[])
2806 const struct nlattr
*attr
;
2807 struct nft_set
*set
;
2811 err
= nft_ctx_init_from_elemattr(&ctx
, skb
, nlh
, nla
);
2815 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
2817 return PTR_ERR(set
);
2818 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
2821 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
2822 err
= nft_add_set_elem(&ctx
, set
, attr
);
2829 static int nft_del_setelem(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2830 const struct nlattr
*attr
)
2832 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
2833 struct nft_data_desc desc
;
2834 struct nft_set_elem elem
;
2837 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
2838 nft_set_elem_policy
);
2843 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
2846 err
= nft_data_init(ctx
, &elem
.key
, &desc
, nla
[NFTA_SET_ELEM_KEY
]);
2851 if (desc
.type
!= NFT_DATA_VALUE
|| desc
.len
!= set
->klen
)
2854 err
= set
->ops
->get(set
, &elem
);
2858 set
->ops
->remove(set
, &elem
);
2860 nft_data_uninit(&elem
.key
, NFT_DATA_VALUE
);
2861 if (set
->flags
& NFT_SET_MAP
)
2862 nft_data_uninit(&elem
.data
, set
->dtype
);
2865 nft_data_uninit(&elem
.key
, desc
.type
);
2870 static int nf_tables_delsetelem(struct sock
*nlsk
, struct sk_buff
*skb
,
2871 const struct nlmsghdr
*nlh
,
2872 const struct nlattr
* const nla
[])
2874 const struct nlattr
*attr
;
2875 struct nft_set
*set
;
2879 err
= nft_ctx_init_from_elemattr(&ctx
, skb
, nlh
, nla
);
2883 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
2885 return PTR_ERR(set
);
2886 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
2889 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
2890 err
= nft_del_setelem(&ctx
, set
, attr
);
2897 static const struct nfnl_callback nf_tables_cb
[NFT_MSG_MAX
] = {
2898 [NFT_MSG_NEWTABLE
] = {
2899 .call
= nf_tables_newtable
,
2900 .attr_count
= NFTA_TABLE_MAX
,
2901 .policy
= nft_table_policy
,
2903 [NFT_MSG_GETTABLE
] = {
2904 .call
= nf_tables_gettable
,
2905 .attr_count
= NFTA_TABLE_MAX
,
2906 .policy
= nft_table_policy
,
2908 [NFT_MSG_DELTABLE
] = {
2909 .call
= nf_tables_deltable
,
2910 .attr_count
= NFTA_TABLE_MAX
,
2911 .policy
= nft_table_policy
,
2913 [NFT_MSG_NEWCHAIN
] = {
2914 .call
= nf_tables_newchain
,
2915 .attr_count
= NFTA_CHAIN_MAX
,
2916 .policy
= nft_chain_policy
,
2918 [NFT_MSG_GETCHAIN
] = {
2919 .call
= nf_tables_getchain
,
2920 .attr_count
= NFTA_CHAIN_MAX
,
2921 .policy
= nft_chain_policy
,
2923 [NFT_MSG_DELCHAIN
] = {
2924 .call
= nf_tables_delchain
,
2925 .attr_count
= NFTA_CHAIN_MAX
,
2926 .policy
= nft_chain_policy
,
2928 [NFT_MSG_NEWRULE
] = {
2929 .call_batch
= nf_tables_newrule
,
2930 .attr_count
= NFTA_RULE_MAX
,
2931 .policy
= nft_rule_policy
,
2933 [NFT_MSG_GETRULE
] = {
2934 .call
= nf_tables_getrule
,
2935 .attr_count
= NFTA_RULE_MAX
,
2936 .policy
= nft_rule_policy
,
2938 [NFT_MSG_DELRULE
] = {
2939 .call_batch
= nf_tables_delrule
,
2940 .attr_count
= NFTA_RULE_MAX
,
2941 .policy
= nft_rule_policy
,
2943 [NFT_MSG_NEWSET
] = {
2944 .call
= nf_tables_newset
,
2945 .attr_count
= NFTA_SET_MAX
,
2946 .policy
= nft_set_policy
,
2948 [NFT_MSG_GETSET
] = {
2949 .call
= nf_tables_getset
,
2950 .attr_count
= NFTA_SET_MAX
,
2951 .policy
= nft_set_policy
,
2953 [NFT_MSG_DELSET
] = {
2954 .call
= nf_tables_delset
,
2955 .attr_count
= NFTA_SET_MAX
,
2956 .policy
= nft_set_policy
,
2958 [NFT_MSG_NEWSETELEM
] = {
2959 .call
= nf_tables_newsetelem
,
2960 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
2961 .policy
= nft_set_elem_list_policy
,
2963 [NFT_MSG_GETSETELEM
] = {
2964 .call
= nf_tables_getsetelem
,
2965 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
2966 .policy
= nft_set_elem_list_policy
,
2968 [NFT_MSG_DELSETELEM
] = {
2969 .call
= nf_tables_delsetelem
,
2970 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
2971 .policy
= nft_set_elem_list_policy
,
2975 static const struct nfnetlink_subsystem nf_tables_subsys
= {
2976 .name
= "nf_tables",
2977 .subsys_id
= NFNL_SUBSYS_NFTABLES
,
2978 .cb_count
= NFT_MSG_MAX
,
2980 .commit
= nf_tables_commit
,
2981 .abort
= nf_tables_abort
,
2985 * Loop detection - walk through the ruleset beginning at the destination chain
2986 * of a new jump until either the source chain is reached (loop) or all
2987 * reachable chains have been traversed.
2989 * The loop check is performed whenever a new jump verdict is added to an
2990 * expression or verdict map or a verdict map is bound to a new chain.
2993 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
2994 const struct nft_chain
*chain
);
2996 static int nf_tables_loop_check_setelem(const struct nft_ctx
*ctx
,
2997 const struct nft_set
*set
,
2998 const struct nft_set_iter
*iter
,
2999 const struct nft_set_elem
*elem
)
3001 if (elem
->flags
& NFT_SET_ELEM_INTERVAL_END
)
3004 switch (elem
->data
.verdict
) {
3007 return nf_tables_check_loops(ctx
, elem
->data
.chain
);
3013 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
3014 const struct nft_chain
*chain
)
3016 const struct nft_rule
*rule
;
3017 const struct nft_expr
*expr
, *last
;
3018 const struct nft_set
*set
;
3019 struct nft_set_binding
*binding
;
3020 struct nft_set_iter iter
;
3022 if (ctx
->chain
== chain
)
3025 list_for_each_entry(rule
, &chain
->rules
, list
) {
3026 nft_rule_for_each_expr(expr
, last
, rule
) {
3027 const struct nft_data
*data
= NULL
;
3030 if (!expr
->ops
->validate
)
3033 err
= expr
->ops
->validate(ctx
, expr
, &data
);
3040 switch (data
->verdict
) {
3043 err
= nf_tables_check_loops(ctx
, data
->chain
);
3052 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
3053 if (!(set
->flags
& NFT_SET_MAP
) ||
3054 set
->dtype
!= NFT_DATA_VERDICT
)
3057 list_for_each_entry(binding
, &set
->bindings
, list
) {
3058 if (binding
->chain
!= chain
)
3064 iter
.fn
= nf_tables_loop_check_setelem
;
3066 set
->ops
->walk(ctx
, set
, &iter
);
3076 * nft_validate_input_register - validate an expressions' input register
3078 * @reg: the register number
3080 * Validate that the input register is one of the general purpose
3083 int nft_validate_input_register(enum nft_registers reg
)
3085 if (reg
<= NFT_REG_VERDICT
)
3087 if (reg
> NFT_REG_MAX
)
3091 EXPORT_SYMBOL_GPL(nft_validate_input_register
);
3094 * nft_validate_output_register - validate an expressions' output register
3096 * @reg: the register number
3098 * Validate that the output register is one of the general purpose
3099 * registers or the verdict register.
3101 int nft_validate_output_register(enum nft_registers reg
)
3103 if (reg
< NFT_REG_VERDICT
)
3105 if (reg
> NFT_REG_MAX
)
3109 EXPORT_SYMBOL_GPL(nft_validate_output_register
);
3112 * nft_validate_data_load - validate an expressions' data load
3114 * @ctx: context of the expression performing the load
3115 * @reg: the destination register number
3116 * @data: the data to load
3117 * @type: the data type
3119 * Validate that a data load uses the appropriate data type for
3120 * the destination register. A value of NULL for the data means
3121 * that its runtime gathered data, which is always of type
3124 int nft_validate_data_load(const struct nft_ctx
*ctx
, enum nft_registers reg
,
3125 const struct nft_data
*data
,
3126 enum nft_data_types type
)
3131 case NFT_REG_VERDICT
:
3132 if (data
== NULL
|| type
!= NFT_DATA_VERDICT
)
3135 if (data
->verdict
== NFT_GOTO
|| data
->verdict
== NFT_JUMP
) {
3136 err
= nf_tables_check_loops(ctx
, data
->chain
);
3140 if (ctx
->chain
->level
+ 1 > data
->chain
->level
) {
3141 if (ctx
->chain
->level
+ 1 == NFT_JUMP_STACK_SIZE
)
3143 data
->chain
->level
= ctx
->chain
->level
+ 1;
3149 if (data
!= NULL
&& type
!= NFT_DATA_VALUE
)
3154 EXPORT_SYMBOL_GPL(nft_validate_data_load
);
3156 static const struct nla_policy nft_verdict_policy
[NFTA_VERDICT_MAX
+ 1] = {
3157 [NFTA_VERDICT_CODE
] = { .type
= NLA_U32
},
3158 [NFTA_VERDICT_CHAIN
] = { .type
= NLA_STRING
,
3159 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
3162 static int nft_verdict_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
3163 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
3165 struct nlattr
*tb
[NFTA_VERDICT_MAX
+ 1];
3166 struct nft_chain
*chain
;
3169 err
= nla_parse_nested(tb
, NFTA_VERDICT_MAX
, nla
, nft_verdict_policy
);
3173 if (!tb
[NFTA_VERDICT_CODE
])
3175 data
->verdict
= ntohl(nla_get_be32(tb
[NFTA_VERDICT_CODE
]));
3177 switch (data
->verdict
) {
3184 desc
->len
= sizeof(data
->verdict
);
3188 if (!tb
[NFTA_VERDICT_CHAIN
])
3190 chain
= nf_tables_chain_lookup(ctx
->table
,
3191 tb
[NFTA_VERDICT_CHAIN
]);
3193 return PTR_ERR(chain
);
3194 if (chain
->flags
& NFT_BASE_CHAIN
)
3198 data
->chain
= chain
;
3199 desc
->len
= sizeof(data
);
3205 desc
->type
= NFT_DATA_VERDICT
;
3209 static void nft_verdict_uninit(const struct nft_data
*data
)
3211 switch (data
->verdict
) {
3219 static int nft_verdict_dump(struct sk_buff
*skb
, const struct nft_data
*data
)
3221 struct nlattr
*nest
;
3223 nest
= nla_nest_start(skb
, NFTA_DATA_VERDICT
);
3225 goto nla_put_failure
;
3227 if (nla_put_be32(skb
, NFTA_VERDICT_CODE
, htonl(data
->verdict
)))
3228 goto nla_put_failure
;
3230 switch (data
->verdict
) {
3233 if (nla_put_string(skb
, NFTA_VERDICT_CHAIN
, data
->chain
->name
))
3234 goto nla_put_failure
;
3236 nla_nest_end(skb
, nest
);
3243 static int nft_value_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
3244 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
3251 if (len
> sizeof(data
->data
))
3254 nla_memcpy(data
->data
, nla
, sizeof(data
->data
));
3255 desc
->type
= NFT_DATA_VALUE
;
3260 static int nft_value_dump(struct sk_buff
*skb
, const struct nft_data
*data
,
3263 return nla_put(skb
, NFTA_DATA_VALUE
, len
, data
->data
);
3266 static const struct nla_policy nft_data_policy
[NFTA_DATA_MAX
+ 1] = {
3267 [NFTA_DATA_VALUE
] = { .type
= NLA_BINARY
,
3268 .len
= FIELD_SIZEOF(struct nft_data
, data
) },
3269 [NFTA_DATA_VERDICT
] = { .type
= NLA_NESTED
},
3273 * nft_data_init - parse nf_tables data netlink attributes
3275 * @ctx: context of the expression using the data
3276 * @data: destination struct nft_data
3277 * @desc: data description
3278 * @nla: netlink attribute containing data
3280 * Parse the netlink data attributes and initialize a struct nft_data.
3281 * The type and length of data are returned in the data description.
3283 * The caller can indicate that it only wants to accept data of type
3284 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3286 int nft_data_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
3287 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
3289 struct nlattr
*tb
[NFTA_DATA_MAX
+ 1];
3292 err
= nla_parse_nested(tb
, NFTA_DATA_MAX
, nla
, nft_data_policy
);
3296 if (tb
[NFTA_DATA_VALUE
])
3297 return nft_value_init(ctx
, data
, desc
, tb
[NFTA_DATA_VALUE
]);
3298 if (tb
[NFTA_DATA_VERDICT
] && ctx
!= NULL
)
3299 return nft_verdict_init(ctx
, data
, desc
, tb
[NFTA_DATA_VERDICT
]);
3302 EXPORT_SYMBOL_GPL(nft_data_init
);
3305 * nft_data_uninit - release a nft_data item
3307 * @data: struct nft_data to release
3308 * @type: type of data
3310 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3311 * all others need to be released by calling this function.
3313 void nft_data_uninit(const struct nft_data
*data
, enum nft_data_types type
)
3316 case NFT_DATA_VALUE
:
3318 case NFT_DATA_VERDICT
:
3319 return nft_verdict_uninit(data
);
3324 EXPORT_SYMBOL_GPL(nft_data_uninit
);
3326 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
3327 enum nft_data_types type
, unsigned int len
)
3329 struct nlattr
*nest
;
3332 nest
= nla_nest_start(skb
, attr
);
3337 case NFT_DATA_VALUE
:
3338 err
= nft_value_dump(skb
, data
, len
);
3340 case NFT_DATA_VERDICT
:
3341 err
= nft_verdict_dump(skb
, data
);
3348 nla_nest_end(skb
, nest
);
3351 EXPORT_SYMBOL_GPL(nft_data_dump
);
3353 static int nf_tables_init_net(struct net
*net
)
3355 INIT_LIST_HEAD(&net
->nft
.af_info
);
3356 INIT_LIST_HEAD(&net
->nft
.commit_list
);
3360 static struct pernet_operations nf_tables_net_ops
= {
3361 .init
= nf_tables_init_net
,
3364 static int __init
nf_tables_module_init(void)
3368 info
= kmalloc(sizeof(struct nft_expr_info
) * NFT_RULE_MAXEXPRS
,
3375 err
= nf_tables_core_module_init();
3379 err
= nfnetlink_subsys_register(&nf_tables_subsys
);
3383 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3384 return register_pernet_subsys(&nf_tables_net_ops
);
3386 nf_tables_core_module_exit();
3393 static void __exit
nf_tables_module_exit(void)
3395 unregister_pernet_subsys(&nf_tables_net_ops
);
3396 nfnetlink_subsys_unregister(&nf_tables_subsys
);
3397 nf_tables_core_module_exit();
3401 module_init(nf_tables_module_init
);
3402 module_exit(nf_tables_module_exit
);
3404 MODULE_LICENSE("GPL");
3405 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3406 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES
);