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>
23 static LIST_HEAD(nf_tables_afinfo
);
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 nft_af_info
*afi
)
36 INIT_LIST_HEAD(&afi
->tables
);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
38 list_add_tail(&afi
->list
, &nf_tables_afinfo
);
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(int family
)
61 struct nft_af_info
*afi
;
63 list_for_each_entry(afi
, &nf_tables_afinfo
, list
) {
64 if (afi
->family
== family
)
70 static struct nft_af_info
*nf_tables_afinfo_lookup(int family
, bool autoload
)
72 struct nft_af_info
*afi
;
74 afi
= nft_afinfo_lookup(family
);
79 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
80 request_module("nft-afinfo-%u", family
);
81 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
82 afi
= nft_afinfo_lookup(family
);
84 return ERR_PTR(-EAGAIN
);
87 return ERR_PTR(-EAFNOSUPPORT
);
94 static struct nft_table
*nft_table_lookup(const struct nft_af_info
*afi
,
95 const struct nlattr
*nla
)
97 struct nft_table
*table
;
99 list_for_each_entry(table
, &afi
->tables
, list
) {
100 if (!nla_strcmp(nla
, table
->name
))
106 static struct nft_table
*nf_tables_table_lookup(const struct nft_af_info
*afi
,
107 const struct nlattr
*nla
)
109 struct nft_table
*table
;
112 return ERR_PTR(-EINVAL
);
114 table
= nft_table_lookup(afi
, nla
);
118 return ERR_PTR(-ENOENT
);
121 static inline u64
nf_tables_alloc_handle(struct nft_table
*table
)
123 return ++table
->hgenerator
;
126 static struct nf_chain_type
*chain_type
[AF_MAX
][NFT_CHAIN_T_MAX
];
128 static int __nf_tables_chain_type_lookup(int family
, const struct nlattr
*nla
)
132 for (i
=0; i
<NFT_CHAIN_T_MAX
; i
++) {
133 if (chain_type
[family
][i
] != NULL
&&
134 !nla_strcmp(nla
, chain_type
[family
][i
]->name
))
140 static int nf_tables_chain_type_lookup(const struct nft_af_info
*afi
,
141 const struct nlattr
*nla
,
146 type
= __nf_tables_chain_type_lookup(afi
->family
, nla
);
147 #ifdef CONFIG_MODULES
148 if (type
< 0 && autoload
) {
149 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
150 request_module("nft-chain-%u-%*.s", afi
->family
,
151 nla_len(nla
)-1, (const char *)nla_data(nla
));
152 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
153 type
= __nf_tables_chain_type_lookup(afi
->family
, nla
);
159 static const struct nla_policy nft_table_policy
[NFTA_TABLE_MAX
+ 1] = {
160 [NFTA_TABLE_NAME
] = { .type
= NLA_STRING
},
163 static int nf_tables_fill_table_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
164 int event
, u32 flags
, int family
,
165 const struct nft_table
*table
)
167 struct nlmsghdr
*nlh
;
168 struct nfgenmsg
*nfmsg
;
170 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
171 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
173 goto nla_put_failure
;
175 nfmsg
= nlmsg_data(nlh
);
176 nfmsg
->nfgen_family
= family
;
177 nfmsg
->version
= NFNETLINK_V0
;
180 if (nla_put_string(skb
, NFTA_TABLE_NAME
, table
->name
))
181 goto nla_put_failure
;
183 return nlmsg_end(skb
, nlh
);
186 nlmsg_trim(skb
, nlh
);
190 static int nf_tables_table_notify(const struct sk_buff
*oskb
,
191 const struct nlmsghdr
*nlh
,
192 const struct nft_table
*table
,
193 int event
, int family
)
196 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
197 u32 seq
= nlh
? nlh
->nlmsg_seq
: 0;
198 struct net
*net
= oskb
? sock_net(oskb
->sk
) : &init_net
;
202 report
= nlh
? nlmsg_report(nlh
) : false;
203 if (!report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
207 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
211 err
= nf_tables_fill_table_info(skb
, portid
, seq
, event
, 0,
218 err
= nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, report
,
222 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, err
);
226 static int nf_tables_dump_tables(struct sk_buff
*skb
,
227 struct netlink_callback
*cb
)
229 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
230 const struct nft_af_info
*afi
;
231 const struct nft_table
*table
;
232 unsigned int idx
= 0, s_idx
= cb
->args
[0];
233 int family
= nfmsg
->nfgen_family
;
235 list_for_each_entry(afi
, &nf_tables_afinfo
, list
) {
236 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
239 list_for_each_entry(table
, &afi
->tables
, list
) {
243 memset(&cb
->args
[1], 0,
244 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
245 if (nf_tables_fill_table_info(skb
,
246 NETLINK_CB(cb
->skb
).portid
,
250 afi
->family
, table
) < 0)
261 static int nf_tables_gettable(struct sock
*nlsk
, struct sk_buff
*skb
,
262 const struct nlmsghdr
*nlh
,
263 const struct nlattr
* const nla
[])
265 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
266 const struct nft_af_info
*afi
;
267 const struct nft_table
*table
;
268 struct sk_buff
*skb2
;
269 int family
= nfmsg
->nfgen_family
;
272 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
273 struct netlink_dump_control c
= {
274 .dump
= nf_tables_dump_tables
,
276 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
279 afi
= nf_tables_afinfo_lookup(family
, false);
283 table
= nf_tables_table_lookup(afi
, nla
[NFTA_TABLE_NAME
]);
285 return PTR_ERR(table
);
287 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
291 err
= nf_tables_fill_table_info(skb2
, NETLINK_CB(skb
).portid
,
292 nlh
->nlmsg_seq
, NFT_MSG_NEWTABLE
, 0,
297 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
304 static int nf_tables_newtable(struct sock
*nlsk
, struct sk_buff
*skb
,
305 const struct nlmsghdr
*nlh
,
306 const struct nlattr
* const nla
[])
308 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
309 const struct nlattr
*name
;
310 struct nft_af_info
*afi
;
311 struct nft_table
*table
;
312 int family
= nfmsg
->nfgen_family
;
314 afi
= nf_tables_afinfo_lookup(family
, true);
318 name
= nla
[NFTA_TABLE_NAME
];
319 table
= nf_tables_table_lookup(afi
, name
);
321 if (PTR_ERR(table
) != -ENOENT
)
322 return PTR_ERR(table
);
327 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
329 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
334 table
= kzalloc(sizeof(*table
) + nla_len(name
), GFP_KERNEL
);
338 nla_strlcpy(table
->name
, name
, nla_len(name
));
339 INIT_LIST_HEAD(&table
->chains
);
340 INIT_LIST_HEAD(&table
->sets
);
342 list_add_tail(&table
->list
, &afi
->tables
);
343 nf_tables_table_notify(skb
, nlh
, table
, NFT_MSG_NEWTABLE
, family
);
347 static int nf_tables_deltable(struct sock
*nlsk
, struct sk_buff
*skb
,
348 const struct nlmsghdr
*nlh
,
349 const struct nlattr
* const nla
[])
351 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
352 struct nft_af_info
*afi
;
353 struct nft_table
*table
;
354 int family
= nfmsg
->nfgen_family
;
356 afi
= nf_tables_afinfo_lookup(family
, false);
360 table
= nf_tables_table_lookup(afi
, nla
[NFTA_TABLE_NAME
]);
362 return PTR_ERR(table
);
367 list_del(&table
->list
);
368 nf_tables_table_notify(skb
, nlh
, table
, NFT_MSG_DELTABLE
, family
);
373 int nft_register_chain_type(struct nf_chain_type
*ctype
)
377 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
378 if (chain_type
[ctype
->family
][ctype
->type
] != NULL
) {
383 if (!try_module_get(ctype
->me
))
386 chain_type
[ctype
->family
][ctype
->type
] = ctype
;
388 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
391 EXPORT_SYMBOL_GPL(nft_register_chain_type
);
393 void nft_unregister_chain_type(struct nf_chain_type
*ctype
)
395 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
396 chain_type
[ctype
->family
][ctype
->type
] = NULL
;
397 module_put(ctype
->me
);
398 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
400 EXPORT_SYMBOL_GPL(nft_unregister_chain_type
);
406 static struct nft_chain
*
407 nf_tables_chain_lookup_byhandle(const struct nft_table
*table
, u64 handle
)
409 struct nft_chain
*chain
;
411 list_for_each_entry(chain
, &table
->chains
, list
) {
412 if (chain
->handle
== handle
)
416 return ERR_PTR(-ENOENT
);
419 static struct nft_chain
*nf_tables_chain_lookup(const struct nft_table
*table
,
420 const struct nlattr
*nla
)
422 struct nft_chain
*chain
;
425 return ERR_PTR(-EINVAL
);
427 list_for_each_entry(chain
, &table
->chains
, list
) {
428 if (!nla_strcmp(nla
, chain
->name
))
432 return ERR_PTR(-ENOENT
);
435 static const struct nla_policy nft_chain_policy
[NFTA_CHAIN_MAX
+ 1] = {
436 [NFTA_CHAIN_TABLE
] = { .type
= NLA_STRING
},
437 [NFTA_CHAIN_HANDLE
] = { .type
= NLA_U64
},
438 [NFTA_CHAIN_NAME
] = { .type
= NLA_STRING
,
439 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
440 [NFTA_CHAIN_HOOK
] = { .type
= NLA_NESTED
},
441 [NFTA_CHAIN_POLICY
] = { .type
= NLA_U32
},
442 [NFTA_CHAIN_TYPE
] = { .type
= NLA_NUL_STRING
},
443 [NFTA_CHAIN_COUNTERS
] = { .type
= NLA_NESTED
},
446 static const struct nla_policy nft_hook_policy
[NFTA_HOOK_MAX
+ 1] = {
447 [NFTA_HOOK_HOOKNUM
] = { .type
= NLA_U32
},
448 [NFTA_HOOK_PRIORITY
] = { .type
= NLA_U32
},
451 static int nft_dump_stats(struct sk_buff
*skb
, struct nft_stats __percpu
*stats
)
453 struct nft_stats
*cpu_stats
, total
;
457 memset(&total
, 0, sizeof(total
));
458 for_each_possible_cpu(cpu
) {
459 cpu_stats
= per_cpu_ptr(stats
, cpu
);
460 total
.pkts
+= cpu_stats
->pkts
;
461 total
.bytes
+= cpu_stats
->bytes
;
463 nest
= nla_nest_start(skb
, NFTA_CHAIN_COUNTERS
);
465 goto nla_put_failure
;
467 if (nla_put_be64(skb
, NFTA_COUNTER_PACKETS
, cpu_to_be64(total
.pkts
)) ||
468 nla_put_be64(skb
, NFTA_COUNTER_BYTES
, cpu_to_be64(total
.bytes
)))
469 goto nla_put_failure
;
471 nla_nest_end(skb
, nest
);
478 static int nf_tables_fill_chain_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
479 int event
, u32 flags
, int family
,
480 const struct nft_table
*table
,
481 const struct nft_chain
*chain
)
483 struct nlmsghdr
*nlh
;
484 struct nfgenmsg
*nfmsg
;
486 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
487 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
), flags
);
489 goto nla_put_failure
;
491 nfmsg
= nlmsg_data(nlh
);
492 nfmsg
->nfgen_family
= family
;
493 nfmsg
->version
= NFNETLINK_V0
;
496 if (nla_put_string(skb
, NFTA_CHAIN_TABLE
, table
->name
))
497 goto nla_put_failure
;
498 if (nla_put_be64(skb
, NFTA_CHAIN_HANDLE
, cpu_to_be64(chain
->handle
)))
499 goto nla_put_failure
;
500 if (nla_put_string(skb
, NFTA_CHAIN_NAME
, chain
->name
))
501 goto nla_put_failure
;
503 if (chain
->flags
& NFT_BASE_CHAIN
) {
504 const struct nft_base_chain
*basechain
= nft_base_chain(chain
);
505 const struct nf_hook_ops
*ops
= &basechain
->ops
;
508 nest
= nla_nest_start(skb
, NFTA_CHAIN_HOOK
);
510 goto nla_put_failure
;
511 if (nla_put_be32(skb
, NFTA_HOOK_HOOKNUM
, htonl(ops
->hooknum
)))
512 goto nla_put_failure
;
513 if (nla_put_be32(skb
, NFTA_HOOK_PRIORITY
, htonl(ops
->priority
)))
514 goto nla_put_failure
;
515 nla_nest_end(skb
, nest
);
517 if (nla_put_be32(skb
, NFTA_CHAIN_POLICY
,
518 htonl(basechain
->policy
)))
519 goto nla_put_failure
;
521 if (nla_put_string(skb
, NFTA_CHAIN_TYPE
,
522 chain_type
[ops
->pf
][nft_base_chain(chain
)->type
]->name
))
523 goto nla_put_failure
;
525 if (nft_dump_stats(skb
, nft_base_chain(chain
)->stats
))
526 goto nla_put_failure
;
529 if (nla_put_be32(skb
, NFTA_CHAIN_USE
, htonl(chain
->use
)))
530 goto nla_put_failure
;
532 return nlmsg_end(skb
, nlh
);
535 nlmsg_trim(skb
, nlh
);
539 static int nf_tables_chain_notify(const struct sk_buff
*oskb
,
540 const struct nlmsghdr
*nlh
,
541 const struct nft_table
*table
,
542 const struct nft_chain
*chain
,
543 int event
, int family
)
546 u32 portid
= oskb
? NETLINK_CB(oskb
).portid
: 0;
547 struct net
*net
= oskb
? sock_net(oskb
->sk
) : &init_net
;
548 u32 seq
= nlh
? nlh
->nlmsg_seq
: 0;
552 report
= nlh
? nlmsg_report(nlh
) : false;
553 if (!report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
557 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
561 err
= nf_tables_fill_chain_info(skb
, portid
, seq
, event
, 0, family
,
568 err
= nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, report
,
572 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, err
);
576 static int nf_tables_dump_chains(struct sk_buff
*skb
,
577 struct netlink_callback
*cb
)
579 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
580 const struct nft_af_info
*afi
;
581 const struct nft_table
*table
;
582 const struct nft_chain
*chain
;
583 unsigned int idx
= 0, s_idx
= cb
->args
[0];
584 int family
= nfmsg
->nfgen_family
;
586 list_for_each_entry(afi
, &nf_tables_afinfo
, list
) {
587 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
590 list_for_each_entry(table
, &afi
->tables
, list
) {
591 list_for_each_entry(chain
, &table
->chains
, list
) {
595 memset(&cb
->args
[1], 0,
596 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
597 if (nf_tables_fill_chain_info(skb
, NETLINK_CB(cb
->skb
).portid
,
601 afi
->family
, table
, chain
) < 0)
614 static int nf_tables_getchain(struct sock
*nlsk
, struct sk_buff
*skb
,
615 const struct nlmsghdr
*nlh
,
616 const struct nlattr
* const nla
[])
618 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
619 const struct nft_af_info
*afi
;
620 const struct nft_table
*table
;
621 const struct nft_chain
*chain
;
622 struct sk_buff
*skb2
;
623 int family
= nfmsg
->nfgen_family
;
626 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
627 struct netlink_dump_control c
= {
628 .dump
= nf_tables_dump_chains
,
630 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
633 afi
= nf_tables_afinfo_lookup(family
, false);
637 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
639 return PTR_ERR(table
);
641 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
]);
643 return PTR_ERR(chain
);
645 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
649 err
= nf_tables_fill_chain_info(skb2
, NETLINK_CB(skb
).portid
,
650 nlh
->nlmsg_seq
, NFT_MSG_NEWCHAIN
, 0,
651 family
, table
, chain
);
655 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
663 nf_tables_chain_policy(struct nft_base_chain
*chain
, const struct nlattr
*attr
)
665 switch (ntohl(nla_get_be32(attr
))) {
667 chain
->policy
= NF_DROP
;
670 chain
->policy
= NF_ACCEPT
;
678 static const struct nla_policy nft_counter_policy
[NFTA_COUNTER_MAX
+ 1] = {
679 [NFTA_COUNTER_PACKETS
] = { .type
= NLA_U64
},
680 [NFTA_COUNTER_BYTES
] = { .type
= NLA_U64
},
684 nf_tables_counters(struct nft_base_chain
*chain
, const struct nlattr
*attr
)
686 struct nlattr
*tb
[NFTA_COUNTER_MAX
+1];
687 struct nft_stats __percpu
*newstats
;
688 struct nft_stats
*stats
;
691 err
= nla_parse_nested(tb
, NFTA_COUNTER_MAX
, attr
, nft_counter_policy
);
695 if (!tb
[NFTA_COUNTER_BYTES
] || !tb
[NFTA_COUNTER_PACKETS
])
698 newstats
= alloc_percpu(struct nft_stats
);
699 if (newstats
== NULL
)
702 /* Restore old counters on this cpu, no problem. Per-cpu statistics
703 * are not exposed to userspace.
705 stats
= this_cpu_ptr(newstats
);
706 stats
->bytes
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_BYTES
]));
707 stats
->pkts
= be64_to_cpu(nla_get_be64(tb
[NFTA_COUNTER_PACKETS
]));
710 /* nfnl_lock is held, add some nfnl function for this, later */
711 struct nft_stats __percpu
*oldstats
=
712 rcu_dereference_protected(chain
->stats
, 1);
714 rcu_assign_pointer(chain
->stats
, newstats
);
716 free_percpu(oldstats
);
718 rcu_assign_pointer(chain
->stats
, newstats
);
723 static int nf_tables_newchain(struct sock
*nlsk
, struct sk_buff
*skb
,
724 const struct nlmsghdr
*nlh
,
725 const struct nlattr
* const nla
[])
727 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
728 const struct nlattr
* uninitialized_var(name
);
729 const struct nft_af_info
*afi
;
730 struct nft_table
*table
;
731 struct nft_chain
*chain
;
732 struct nft_base_chain
*basechain
= NULL
;
733 struct nlattr
*ha
[NFTA_HOOK_MAX
+ 1];
734 int family
= nfmsg
->nfgen_family
;
739 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
741 afi
= nf_tables_afinfo_lookup(family
, true);
745 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
747 return PTR_ERR(table
);
749 if (table
->use
== UINT_MAX
)
753 name
= nla
[NFTA_CHAIN_NAME
];
755 if (nla
[NFTA_CHAIN_HANDLE
]) {
756 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_CHAIN_HANDLE
]));
757 chain
= nf_tables_chain_lookup_byhandle(table
, handle
);
759 return PTR_ERR(chain
);
761 chain
= nf_tables_chain_lookup(table
, name
);
763 if (PTR_ERR(chain
) != -ENOENT
)
764 return PTR_ERR(chain
);
770 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
772 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
775 if (nla
[NFTA_CHAIN_HANDLE
] && name
&&
776 !IS_ERR(nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
])))
779 if (nla
[NFTA_CHAIN_POLICY
]) {
780 if (!(chain
->flags
& NFT_BASE_CHAIN
))
783 err
= nf_tables_chain_policy(nft_base_chain(chain
),
784 nla
[NFTA_CHAIN_POLICY
]);
789 if (nla
[NFTA_CHAIN_COUNTERS
]) {
790 if (!(chain
->flags
& NFT_BASE_CHAIN
))
793 err
= nf_tables_counters(nft_base_chain(chain
),
794 nla
[NFTA_CHAIN_COUNTERS
]);
799 if (nla
[NFTA_CHAIN_HANDLE
] && name
)
800 nla_strlcpy(chain
->name
, name
, NFT_CHAIN_MAXNAMELEN
);
805 if (nla
[NFTA_CHAIN_HOOK
]) {
806 struct nf_hook_ops
*ops
;
809 int type
= NFT_CHAIN_T_DEFAULT
;
811 if (nla
[NFTA_CHAIN_TYPE
]) {
812 type
= nf_tables_chain_type_lookup(afi
,
813 nla
[NFTA_CHAIN_TYPE
],
819 err
= nla_parse_nested(ha
, NFTA_HOOK_MAX
, nla
[NFTA_CHAIN_HOOK
],
823 if (ha
[NFTA_HOOK_HOOKNUM
] == NULL
||
824 ha
[NFTA_HOOK_PRIORITY
] == NULL
)
827 hooknum
= ntohl(nla_get_be32(ha
[NFTA_HOOK_HOOKNUM
]));
828 if (hooknum
>= afi
->nhooks
)
831 hookfn
= chain_type
[family
][type
]->fn
[hooknum
];
835 basechain
= kzalloc(sizeof(*basechain
), GFP_KERNEL
);
836 if (basechain
== NULL
)
839 basechain
->type
= type
;
840 chain
= &basechain
->chain
;
842 ops
= &basechain
->ops
;
844 ops
->owner
= afi
->owner
;
845 ops
->hooknum
= ntohl(nla_get_be32(ha
[NFTA_HOOK_HOOKNUM
]));
846 ops
->priority
= ntohl(nla_get_be32(ha
[NFTA_HOOK_PRIORITY
]));
849 if (afi
->hooks
[ops
->hooknum
])
850 ops
->hook
= afi
->hooks
[ops
->hooknum
];
852 chain
->flags
|= NFT_BASE_CHAIN
;
854 if (nla
[NFTA_CHAIN_POLICY
]) {
855 err
= nf_tables_chain_policy(basechain
,
856 nla
[NFTA_CHAIN_POLICY
]);
858 free_percpu(basechain
->stats
);
863 basechain
->policy
= NF_ACCEPT
;
865 if (nla
[NFTA_CHAIN_COUNTERS
]) {
866 err
= nf_tables_counters(basechain
,
867 nla
[NFTA_CHAIN_COUNTERS
]);
869 free_percpu(basechain
->stats
);
874 struct nft_stats __percpu
*newstats
;
876 newstats
= alloc_percpu(struct nft_stats
);
877 if (newstats
== NULL
)
880 rcu_assign_pointer(nft_base_chain(chain
)->stats
,
884 chain
= kzalloc(sizeof(*chain
), GFP_KERNEL
);
889 INIT_LIST_HEAD(&chain
->rules
);
890 chain
->handle
= nf_tables_alloc_handle(table
);
891 nla_strlcpy(chain
->name
, name
, NFT_CHAIN_MAXNAMELEN
);
893 list_add_tail(&chain
->list
, &table
->chains
);
896 if (chain
->flags
& NFT_BASE_CHAIN
) {
897 err
= nf_register_hook(&nft_base_chain(chain
)->ops
);
899 free_percpu(basechain
->stats
);
905 nf_tables_chain_notify(skb
, nlh
, table
, chain
, NFT_MSG_NEWCHAIN
,
910 static void nf_tables_rcu_chain_destroy(struct rcu_head
*head
)
912 struct nft_chain
*chain
= container_of(head
, struct nft_chain
, rcu_head
);
914 BUG_ON(chain
->use
> 0);
916 if (chain
->flags
& NFT_BASE_CHAIN
) {
917 free_percpu(nft_base_chain(chain
)->stats
);
918 kfree(nft_base_chain(chain
));
923 static int nf_tables_delchain(struct sock
*nlsk
, struct sk_buff
*skb
,
924 const struct nlmsghdr
*nlh
,
925 const struct nlattr
* const nla
[])
927 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
928 const struct nft_af_info
*afi
;
929 struct nft_table
*table
;
930 struct nft_chain
*chain
;
931 int family
= nfmsg
->nfgen_family
;
933 afi
= nf_tables_afinfo_lookup(family
, false);
937 table
= nf_tables_table_lookup(afi
, nla
[NFTA_CHAIN_TABLE
]);
939 return PTR_ERR(table
);
941 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_CHAIN_NAME
]);
943 return PTR_ERR(chain
);
945 if (!list_empty(&chain
->rules
))
948 list_del(&chain
->list
);
951 if (chain
->flags
& NFT_BASE_CHAIN
)
952 nf_unregister_hook(&nft_base_chain(chain
)->ops
);
954 nf_tables_chain_notify(skb
, nlh
, table
, chain
, NFT_MSG_DELCHAIN
,
957 /* Make sure all rule references are gone before this is released */
958 call_rcu(&chain
->rcu_head
, nf_tables_rcu_chain_destroy
);
962 static void nft_ctx_init(struct nft_ctx
*ctx
,
963 const struct sk_buff
*skb
,
964 const struct nlmsghdr
*nlh
,
965 const struct nft_af_info
*afi
,
966 const struct nft_table
*table
,
967 const struct nft_chain
*chain
,
968 const struct nlattr
* const *nla
)
983 * nft_register_expr - register nf_tables expr type
986 * Registers the expr type for use with nf_tables. Returns zero on
987 * success or a negative errno code otherwise.
989 int nft_register_expr(struct nft_expr_type
*type
)
991 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
992 list_add_tail(&type
->list
, &nf_tables_expressions
);
993 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
996 EXPORT_SYMBOL_GPL(nft_register_expr
);
999 * nft_unregister_expr - unregister nf_tables expr type
1002 * Unregisters the expr typefor use with nf_tables.
1004 void nft_unregister_expr(struct nft_expr_type
*type
)
1006 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1007 list_del(&type
->list
);
1008 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1010 EXPORT_SYMBOL_GPL(nft_unregister_expr
);
1012 static const struct nft_expr_type
*__nft_expr_type_get(struct nlattr
*nla
)
1014 const struct nft_expr_type
*type
;
1016 list_for_each_entry(type
, &nf_tables_expressions
, list
) {
1017 if (!nla_strcmp(nla
, type
->name
))
1023 static const struct nft_expr_type
*nft_expr_type_get(struct nlattr
*nla
)
1025 const struct nft_expr_type
*type
;
1028 return ERR_PTR(-EINVAL
);
1030 type
= __nft_expr_type_get(nla
);
1031 if (type
!= NULL
&& try_module_get(type
->owner
))
1034 #ifdef CONFIG_MODULES
1036 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1037 request_module("nft-expr-%.*s",
1038 nla_len(nla
), (char *)nla_data(nla
));
1039 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1040 if (__nft_expr_type_get(nla
))
1041 return ERR_PTR(-EAGAIN
);
1044 return ERR_PTR(-ENOENT
);
1047 static const struct nla_policy nft_expr_policy
[NFTA_EXPR_MAX
+ 1] = {
1048 [NFTA_EXPR_NAME
] = { .type
= NLA_STRING
},
1049 [NFTA_EXPR_DATA
] = { .type
= NLA_NESTED
},
1052 static int nf_tables_fill_expr_info(struct sk_buff
*skb
,
1053 const struct nft_expr
*expr
)
1055 if (nla_put_string(skb
, NFTA_EXPR_NAME
, expr
->ops
->type
->name
))
1056 goto nla_put_failure
;
1058 if (expr
->ops
->dump
) {
1059 struct nlattr
*data
= nla_nest_start(skb
, NFTA_EXPR_DATA
);
1061 goto nla_put_failure
;
1062 if (expr
->ops
->dump(skb
, expr
) < 0)
1063 goto nla_put_failure
;
1064 nla_nest_end(skb
, data
);
1073 struct nft_expr_info
{
1074 const struct nft_expr_ops
*ops
;
1075 struct nlattr
*tb
[NFT_EXPR_MAXATTR
+ 1];
1078 static int nf_tables_expr_parse(const struct nft_ctx
*ctx
,
1079 const struct nlattr
*nla
,
1080 struct nft_expr_info
*info
)
1082 const struct nft_expr_type
*type
;
1083 const struct nft_expr_ops
*ops
;
1084 struct nlattr
*tb
[NFTA_EXPR_MAX
+ 1];
1087 err
= nla_parse_nested(tb
, NFTA_EXPR_MAX
, nla
, nft_expr_policy
);
1091 type
= nft_expr_type_get(tb
[NFTA_EXPR_NAME
]);
1093 return PTR_ERR(type
);
1095 if (tb
[NFTA_EXPR_DATA
]) {
1096 err
= nla_parse_nested(info
->tb
, type
->maxattr
,
1097 tb
[NFTA_EXPR_DATA
], type
->policy
);
1101 memset(info
->tb
, 0, sizeof(info
->tb
[0]) * (type
->maxattr
+ 1));
1103 if (type
->select_ops
!= NULL
) {
1104 ops
= type
->select_ops(ctx
,
1105 (const struct nlattr
* const *)info
->tb
);
1117 module_put(type
->owner
);
1121 static int nf_tables_newexpr(const struct nft_ctx
*ctx
,
1122 const struct nft_expr_info
*info
,
1123 struct nft_expr
*expr
)
1125 const struct nft_expr_ops
*ops
= info
->ops
;
1130 err
= ops
->init(ctx
, expr
, (const struct nlattr
**)info
->tb
);
1142 static void nf_tables_expr_destroy(struct nft_expr
*expr
)
1144 if (expr
->ops
->destroy
)
1145 expr
->ops
->destroy(expr
);
1146 module_put(expr
->ops
->type
->owner
);
1153 static struct nft_rule
*__nf_tables_rule_lookup(const struct nft_chain
*chain
,
1156 struct nft_rule
*rule
;
1158 // FIXME: this sucks
1159 list_for_each_entry(rule
, &chain
->rules
, list
) {
1160 if (handle
== rule
->handle
)
1164 return ERR_PTR(-ENOENT
);
1167 static struct nft_rule
*nf_tables_rule_lookup(const struct nft_chain
*chain
,
1168 const struct nlattr
*nla
)
1171 return ERR_PTR(-EINVAL
);
1173 return __nf_tables_rule_lookup(chain
, be64_to_cpu(nla_get_be64(nla
)));
1176 static const struct nla_policy nft_rule_policy
[NFTA_RULE_MAX
+ 1] = {
1177 [NFTA_RULE_TABLE
] = { .type
= NLA_STRING
},
1178 [NFTA_RULE_CHAIN
] = { .type
= NLA_STRING
,
1179 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
1180 [NFTA_RULE_HANDLE
] = { .type
= NLA_U64
},
1181 [NFTA_RULE_EXPRESSIONS
] = { .type
= NLA_NESTED
},
1182 [NFTA_RULE_COMPAT
] = { .type
= NLA_NESTED
},
1185 static int nf_tables_fill_rule_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
1186 int event
, u32 flags
, int family
,
1187 const struct nft_table
*table
,
1188 const struct nft_chain
*chain
,
1189 const struct nft_rule
*rule
)
1191 struct nlmsghdr
*nlh
;
1192 struct nfgenmsg
*nfmsg
;
1193 const struct nft_expr
*expr
, *next
;
1194 struct nlattr
*list
;
1196 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
1197 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
1200 goto nla_put_failure
;
1202 nfmsg
= nlmsg_data(nlh
);
1203 nfmsg
->nfgen_family
= family
;
1204 nfmsg
->version
= NFNETLINK_V0
;
1207 if (nla_put_string(skb
, NFTA_RULE_TABLE
, table
->name
))
1208 goto nla_put_failure
;
1209 if (nla_put_string(skb
, NFTA_RULE_CHAIN
, chain
->name
))
1210 goto nla_put_failure
;
1211 if (nla_put_be64(skb
, NFTA_RULE_HANDLE
, cpu_to_be64(rule
->handle
)))
1212 goto nla_put_failure
;
1214 list
= nla_nest_start(skb
, NFTA_RULE_EXPRESSIONS
);
1216 goto nla_put_failure
;
1217 nft_rule_for_each_expr(expr
, next
, rule
) {
1218 struct nlattr
*elem
= nla_nest_start(skb
, NFTA_LIST_ELEM
);
1220 goto nla_put_failure
;
1221 if (nf_tables_fill_expr_info(skb
, expr
) < 0)
1222 goto nla_put_failure
;
1223 nla_nest_end(skb
, elem
);
1225 nla_nest_end(skb
, list
);
1227 return nlmsg_end(skb
, nlh
);
1230 nlmsg_trim(skb
, nlh
);
1234 static int nf_tables_rule_notify(const struct sk_buff
*oskb
,
1235 const struct nlmsghdr
*nlh
,
1236 const struct nft_table
*table
,
1237 const struct nft_chain
*chain
,
1238 const struct nft_rule
*rule
,
1239 int event
, u32 flags
, int family
)
1241 struct sk_buff
*skb
;
1242 u32 portid
= NETLINK_CB(oskb
).portid
;
1243 struct net
*net
= oskb
? sock_net(oskb
->sk
) : &init_net
;
1244 u32 seq
= nlh
->nlmsg_seq
;
1248 report
= nlmsg_report(nlh
);
1249 if (!report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
1253 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1257 err
= nf_tables_fill_rule_info(skb
, portid
, seq
, event
, flags
,
1258 family
, table
, chain
, rule
);
1264 err
= nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, report
,
1268 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, err
);
1272 static int nf_tables_dump_rules(struct sk_buff
*skb
,
1273 struct netlink_callback
*cb
)
1275 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1276 const struct nft_af_info
*afi
;
1277 const struct nft_table
*table
;
1278 const struct nft_chain
*chain
;
1279 const struct nft_rule
*rule
;
1280 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1281 int family
= nfmsg
->nfgen_family
;
1283 list_for_each_entry(afi
, &nf_tables_afinfo
, list
) {
1284 if (family
!= NFPROTO_UNSPEC
&& family
!= afi
->family
)
1287 list_for_each_entry(table
, &afi
->tables
, list
) {
1288 list_for_each_entry(chain
, &table
->chains
, list
) {
1289 list_for_each_entry(rule
, &chain
->rules
, list
) {
1293 memset(&cb
->args
[1], 0,
1294 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1295 if (nf_tables_fill_rule_info(skb
, NETLINK_CB(cb
->skb
).portid
,
1298 NLM_F_MULTI
| NLM_F_APPEND
,
1299 afi
->family
, table
, chain
, rule
) < 0)
1312 static int nf_tables_getrule(struct sock
*nlsk
, struct sk_buff
*skb
,
1313 const struct nlmsghdr
*nlh
,
1314 const struct nlattr
* const nla
[])
1316 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1317 const struct nft_af_info
*afi
;
1318 const struct nft_table
*table
;
1319 const struct nft_chain
*chain
;
1320 const struct nft_rule
*rule
;
1321 struct sk_buff
*skb2
;
1322 int family
= nfmsg
->nfgen_family
;
1325 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1326 struct netlink_dump_control c
= {
1327 .dump
= nf_tables_dump_rules
,
1329 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
1332 afi
= nf_tables_afinfo_lookup(family
, false);
1334 return PTR_ERR(afi
);
1336 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
1338 return PTR_ERR(table
);
1340 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
1342 return PTR_ERR(chain
);
1344 rule
= nf_tables_rule_lookup(chain
, nla
[NFTA_RULE_HANDLE
]);
1346 return PTR_ERR(rule
);
1348 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1352 err
= nf_tables_fill_rule_info(skb2
, NETLINK_CB(skb
).portid
,
1353 nlh
->nlmsg_seq
, NFT_MSG_NEWRULE
, 0,
1354 family
, table
, chain
, rule
);
1358 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
1365 static void nf_tables_rcu_rule_destroy(struct rcu_head
*head
)
1367 struct nft_rule
*rule
= container_of(head
, struct nft_rule
, rcu_head
);
1368 struct nft_expr
*expr
;
1371 * Careful: some expressions might not be initialized in case this
1372 * is called on error from nf_tables_newrule().
1374 expr
= nft_expr_first(rule
);
1375 while (expr
->ops
&& expr
!= nft_expr_last(rule
)) {
1376 nf_tables_expr_destroy(expr
);
1377 expr
= nft_expr_next(expr
);
1382 static void nf_tables_rule_destroy(struct nft_rule
*rule
)
1384 call_rcu(&rule
->rcu_head
, nf_tables_rcu_rule_destroy
);
1387 #define NFT_RULE_MAXEXPRS 128
1389 static struct nft_expr_info
*info
;
1391 static int nf_tables_newrule(struct sock
*nlsk
, struct sk_buff
*skb
,
1392 const struct nlmsghdr
*nlh
,
1393 const struct nlattr
* const nla
[])
1395 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1396 const struct nft_af_info
*afi
;
1397 struct nft_table
*table
;
1398 struct nft_chain
*chain
;
1399 struct nft_rule
*rule
, *old_rule
= NULL
;
1400 struct nft_expr
*expr
;
1403 unsigned int size
, i
, n
;
1408 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
1410 afi
= nf_tables_afinfo_lookup(nfmsg
->nfgen_family
, create
);
1412 return PTR_ERR(afi
);
1414 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
1416 return PTR_ERR(table
);
1418 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
1420 return PTR_ERR(chain
);
1422 if (nla
[NFTA_RULE_HANDLE
]) {
1423 handle
= be64_to_cpu(nla_get_be64(nla
[NFTA_RULE_HANDLE
]));
1424 rule
= __nf_tables_rule_lookup(chain
, handle
);
1426 return PTR_ERR(rule
);
1428 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
1430 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1435 if (!create
|| nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1437 handle
= nf_tables_alloc_handle(table
);
1440 nft_ctx_init(&ctx
, skb
, nlh
, afi
, table
, chain
, nla
);
1444 if (nla
[NFTA_RULE_EXPRESSIONS
]) {
1445 nla_for_each_nested(tmp
, nla
[NFTA_RULE_EXPRESSIONS
], rem
) {
1447 if (nla_type(tmp
) != NFTA_LIST_ELEM
)
1449 if (n
== NFT_RULE_MAXEXPRS
)
1451 err
= nf_tables_expr_parse(&ctx
, tmp
, &info
[n
]);
1454 size
+= info
[n
].ops
->size
;
1460 rule
= kzalloc(sizeof(*rule
) + size
, GFP_KERNEL
);
1464 rule
->handle
= handle
;
1467 expr
= nft_expr_first(rule
);
1468 for (i
= 0; i
< n
; i
++) {
1469 err
= nf_tables_newexpr(&ctx
, &info
[i
], expr
);
1473 expr
= nft_expr_next(expr
);
1476 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
1477 list_replace_rcu(&old_rule
->list
, &rule
->list
);
1478 nf_tables_rule_destroy(old_rule
);
1479 } else if (nlh
->nlmsg_flags
& NLM_F_APPEND
)
1480 list_add_tail_rcu(&rule
->list
, &chain
->rules
);
1482 list_add_rcu(&rule
->list
, &chain
->rules
);
1484 nf_tables_rule_notify(skb
, nlh
, table
, chain
, rule
, NFT_MSG_NEWRULE
,
1485 nlh
->nlmsg_flags
& (NLM_F_APPEND
| NLM_F_REPLACE
),
1486 nfmsg
->nfgen_family
);
1490 nf_tables_rule_destroy(rule
);
1492 for (i
= 0; i
< n
; i
++) {
1493 if (info
[i
].ops
!= NULL
)
1494 module_put(info
[i
].ops
->type
->owner
);
1499 static int nf_tables_delrule(struct sock
*nlsk
, struct sk_buff
*skb
,
1500 const struct nlmsghdr
*nlh
,
1501 const struct nlattr
* const nla
[])
1503 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1504 const struct nft_af_info
*afi
;
1505 const struct nft_table
*table
;
1506 struct nft_chain
*chain
;
1507 struct nft_rule
*rule
, *tmp
;
1508 int family
= nfmsg
->nfgen_family
;
1510 afi
= nf_tables_afinfo_lookup(family
, false);
1512 return PTR_ERR(afi
);
1514 table
= nf_tables_table_lookup(afi
, nla
[NFTA_RULE_TABLE
]);
1516 return PTR_ERR(table
);
1518 chain
= nf_tables_chain_lookup(table
, nla
[NFTA_RULE_CHAIN
]);
1520 return PTR_ERR(chain
);
1522 if (nla
[NFTA_RULE_HANDLE
]) {
1523 rule
= nf_tables_rule_lookup(chain
, nla
[NFTA_RULE_HANDLE
]);
1525 return PTR_ERR(rule
);
1527 /* List removal must be visible before destroying expressions */
1528 list_del_rcu(&rule
->list
);
1530 nf_tables_rule_notify(skb
, nlh
, table
, chain
, rule
,
1531 NFT_MSG_DELRULE
, 0, family
);
1532 nf_tables_rule_destroy(rule
);
1534 /* Remove all rules in this chain */
1535 list_for_each_entry_safe(rule
, tmp
, &chain
->rules
, list
) {
1536 list_del_rcu(&rule
->list
);
1538 nf_tables_rule_notify(skb
, nlh
, table
, chain
, rule
,
1539 NFT_MSG_DELRULE
, 0, family
);
1540 nf_tables_rule_destroy(rule
);
1551 static LIST_HEAD(nf_tables_set_ops
);
1553 int nft_register_set(struct nft_set_ops
*ops
)
1555 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1556 list_add_tail(&ops
->list
, &nf_tables_set_ops
);
1557 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1560 EXPORT_SYMBOL_GPL(nft_register_set
);
1562 void nft_unregister_set(struct nft_set_ops
*ops
)
1564 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1565 list_del(&ops
->list
);
1566 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1568 EXPORT_SYMBOL_GPL(nft_unregister_set
);
1570 static const struct nft_set_ops
*nft_select_set_ops(const struct nlattr
* const nla
[])
1572 const struct nft_set_ops
*ops
;
1575 #ifdef CONFIG_MODULES
1576 if (list_empty(&nf_tables_set_ops
)) {
1577 nfnl_unlock(NFNL_SUBSYS_NFTABLES
);
1578 request_module("nft-set");
1579 nfnl_lock(NFNL_SUBSYS_NFTABLES
);
1580 if (!list_empty(&nf_tables_set_ops
))
1581 return ERR_PTR(-EAGAIN
);
1585 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
1586 features
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
1587 features
&= NFT_SET_INTERVAL
| NFT_SET_MAP
;
1590 // FIXME: implement selection properly
1591 list_for_each_entry(ops
, &nf_tables_set_ops
, list
) {
1592 if ((ops
->features
& features
) != features
)
1594 if (!try_module_get(ops
->owner
))
1599 return ERR_PTR(-EOPNOTSUPP
);
1602 static const struct nla_policy nft_set_policy
[NFTA_SET_MAX
+ 1] = {
1603 [NFTA_SET_TABLE
] = { .type
= NLA_STRING
},
1604 [NFTA_SET_NAME
] = { .type
= NLA_STRING
},
1605 [NFTA_SET_FLAGS
] = { .type
= NLA_U32
},
1606 [NFTA_SET_KEY_TYPE
] = { .type
= NLA_U32
},
1607 [NFTA_SET_KEY_LEN
] = { .type
= NLA_U32
},
1608 [NFTA_SET_DATA_TYPE
] = { .type
= NLA_U32
},
1609 [NFTA_SET_DATA_LEN
] = { .type
= NLA_U32
},
1612 static int nft_ctx_init_from_setattr(struct nft_ctx
*ctx
,
1613 const struct sk_buff
*skb
,
1614 const struct nlmsghdr
*nlh
,
1615 const struct nlattr
* const nla
[])
1617 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1618 const struct nft_af_info
*afi
;
1619 const struct nft_table
*table
= NULL
;
1621 afi
= nf_tables_afinfo_lookup(nfmsg
->nfgen_family
, false);
1623 return PTR_ERR(afi
);
1625 if (nla
[NFTA_SET_TABLE
] != NULL
) {
1626 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
]);
1628 return PTR_ERR(table
);
1631 nft_ctx_init(ctx
, skb
, nlh
, afi
, table
, NULL
, nla
);
1635 struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
1636 const struct nlattr
*nla
)
1638 struct nft_set
*set
;
1641 return ERR_PTR(-EINVAL
);
1643 list_for_each_entry(set
, &table
->sets
, list
) {
1644 if (!nla_strcmp(nla
, set
->name
))
1647 return ERR_PTR(-ENOENT
);
1650 static int nf_tables_set_alloc_name(struct nft_ctx
*ctx
, struct nft_set
*set
,
1653 const struct nft_set
*i
;
1655 unsigned long *inuse
;
1658 p
= strnchr(name
, IFNAMSIZ
, '%');
1660 if (p
[1] != 'd' || strchr(p
+ 2, '%'))
1663 inuse
= (unsigned long *)get_zeroed_page(GFP_KERNEL
);
1667 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
1668 if (!sscanf(i
->name
, name
, &n
))
1670 if (n
< 0 || n
> BITS_PER_LONG
* PAGE_SIZE
)
1675 n
= find_first_zero_bit(inuse
, BITS_PER_LONG
* PAGE_SIZE
);
1676 free_page((unsigned long)inuse
);
1679 snprintf(set
->name
, sizeof(set
->name
), name
, n
);
1680 list_for_each_entry(i
, &ctx
->table
->sets
, list
) {
1681 if (!strcmp(set
->name
, i
->name
))
1687 static int nf_tables_fill_set(struct sk_buff
*skb
, const struct nft_ctx
*ctx
,
1688 const struct nft_set
*set
, u16 event
, u16 flags
)
1690 struct nfgenmsg
*nfmsg
;
1691 struct nlmsghdr
*nlh
;
1692 u32 portid
= NETLINK_CB(ctx
->skb
).portid
;
1693 u32 seq
= ctx
->nlh
->nlmsg_seq
;
1695 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
1696 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
1699 goto nla_put_failure
;
1701 nfmsg
= nlmsg_data(nlh
);
1702 nfmsg
->nfgen_family
= ctx
->afi
->family
;
1703 nfmsg
->version
= NFNETLINK_V0
;
1706 if (nla_put_string(skb
, NFTA_SET_TABLE
, ctx
->table
->name
))
1707 goto nla_put_failure
;
1708 if (nla_put_string(skb
, NFTA_SET_NAME
, set
->name
))
1709 goto nla_put_failure
;
1710 if (set
->flags
!= 0)
1711 if (nla_put_be32(skb
, NFTA_SET_FLAGS
, htonl(set
->flags
)))
1712 goto nla_put_failure
;
1714 if (nla_put_be32(skb
, NFTA_SET_KEY_TYPE
, htonl(set
->ktype
)))
1715 goto nla_put_failure
;
1716 if (nla_put_be32(skb
, NFTA_SET_KEY_LEN
, htonl(set
->klen
)))
1717 goto nla_put_failure
;
1718 if (set
->flags
& NFT_SET_MAP
) {
1719 if (nla_put_be32(skb
, NFTA_SET_DATA_TYPE
, htonl(set
->dtype
)))
1720 goto nla_put_failure
;
1721 if (nla_put_be32(skb
, NFTA_SET_DATA_LEN
, htonl(set
->dlen
)))
1722 goto nla_put_failure
;
1725 return nlmsg_end(skb
, nlh
);
1728 nlmsg_trim(skb
, nlh
);
1732 static int nf_tables_set_notify(const struct nft_ctx
*ctx
,
1733 const struct nft_set
*set
,
1736 struct sk_buff
*skb
;
1737 u32 portid
= NETLINK_CB(ctx
->skb
).portid
;
1738 struct net
*net
= sock_net(ctx
->skb
->sk
);
1742 report
= nlmsg_report(ctx
->nlh
);
1743 if (!report
&& !nfnetlink_has_listeners(net
, NFNLGRP_NFTABLES
))
1747 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1751 err
= nf_tables_fill_set(skb
, ctx
, set
, event
, 0);
1757 err
= nfnetlink_send(skb
, net
, portid
, NFNLGRP_NFTABLES
, report
,
1761 nfnetlink_set_err(net
, portid
, NFNLGRP_NFTABLES
, err
);
1765 static int nf_tables_dump_sets_table(struct nft_ctx
*ctx
, struct sk_buff
*skb
,
1766 struct netlink_callback
*cb
)
1768 const struct nft_set
*set
;
1769 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1774 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
1777 if (nf_tables_fill_set(skb
, ctx
, set
, NFT_MSG_NEWSET
,
1790 static int nf_tables_dump_sets_all(struct nft_ctx
*ctx
, struct sk_buff
*skb
,
1791 struct netlink_callback
*cb
)
1793 const struct nft_set
*set
;
1794 unsigned int idx
= 0, s_idx
= cb
->args
[0];
1795 struct nft_table
*table
, *cur_table
= (struct nft_table
*)cb
->args
[2];
1800 list_for_each_entry(table
, &ctx
->afi
->tables
, list
) {
1801 if (cur_table
&& cur_table
!= table
)
1805 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
1808 if (nf_tables_fill_set(skb
, ctx
, set
, NFT_MSG_NEWSET
,
1811 cb
->args
[2] = (unsigned long) table
;
1823 static int nf_tables_dump_sets(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1825 const struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1826 struct nlattr
*nla
[NFTA_SET_MAX
+ 1];
1830 err
= nlmsg_parse(cb
->nlh
, sizeof(*nfmsg
), nla
, NFTA_SET_MAX
,
1835 err
= nft_ctx_init_from_setattr(&ctx
, cb
->skb
, cb
->nlh
, (void *)nla
);
1839 if (ctx
.table
== NULL
)
1840 ret
= nf_tables_dump_sets_all(&ctx
, skb
, cb
);
1842 ret
= nf_tables_dump_sets_table(&ctx
, skb
, cb
);
1847 static int nf_tables_getset(struct sock
*nlsk
, struct sk_buff
*skb
,
1848 const struct nlmsghdr
*nlh
,
1849 const struct nlattr
* const nla
[])
1851 const struct nft_set
*set
;
1853 struct sk_buff
*skb2
;
1856 /* Verify existance before starting dump */
1857 err
= nft_ctx_init_from_setattr(&ctx
, skb
, nlh
, nla
);
1861 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1862 struct netlink_dump_control c
= {
1863 .dump
= nf_tables_dump_sets
,
1865 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
1868 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
]);
1870 return PTR_ERR(set
);
1872 skb2
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1876 err
= nf_tables_fill_set(skb2
, &ctx
, set
, NFT_MSG_NEWSET
, 0);
1880 return nlmsg_unicast(nlsk
, skb2
, NETLINK_CB(skb
).portid
);
1887 static int nf_tables_newset(struct sock
*nlsk
, struct sk_buff
*skb
,
1888 const struct nlmsghdr
*nlh
,
1889 const struct nlattr
* const nla
[])
1891 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1892 const struct nft_set_ops
*ops
;
1893 const struct nft_af_info
*afi
;
1894 struct nft_table
*table
;
1895 struct nft_set
*set
;
1897 char name
[IFNAMSIZ
];
1900 u32 ktype
, klen
, dlen
, dtype
, flags
;
1903 if (nla
[NFTA_SET_TABLE
] == NULL
||
1904 nla
[NFTA_SET_NAME
] == NULL
||
1905 nla
[NFTA_SET_KEY_LEN
] == NULL
)
1908 ktype
= NFT_DATA_VALUE
;
1909 if (nla
[NFTA_SET_KEY_TYPE
] != NULL
) {
1910 ktype
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_TYPE
]));
1911 if ((ktype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
)
1915 klen
= ntohl(nla_get_be32(nla
[NFTA_SET_KEY_LEN
]));
1916 if (klen
== 0 || klen
> FIELD_SIZEOF(struct nft_data
, data
))
1920 if (nla
[NFTA_SET_FLAGS
] != NULL
) {
1921 flags
= ntohl(nla_get_be32(nla
[NFTA_SET_FLAGS
]));
1922 if (flags
& ~(NFT_SET_ANONYMOUS
| NFT_SET_CONSTANT
|
1923 NFT_SET_INTERVAL
| NFT_SET_MAP
))
1929 if (nla
[NFTA_SET_DATA_TYPE
] != NULL
) {
1930 if (!(flags
& NFT_SET_MAP
))
1933 dtype
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_TYPE
]));
1934 if ((dtype
& NFT_DATA_RESERVED_MASK
) == NFT_DATA_RESERVED_MASK
&&
1935 dtype
!= NFT_DATA_VERDICT
)
1938 if (dtype
!= NFT_DATA_VERDICT
) {
1939 if (nla
[NFTA_SET_DATA_LEN
] == NULL
)
1941 dlen
= ntohl(nla_get_be32(nla
[NFTA_SET_DATA_LEN
]));
1943 dlen
> FIELD_SIZEOF(struct nft_data
, data
))
1946 dlen
= sizeof(struct nft_data
);
1947 } else if (flags
& NFT_SET_MAP
)
1950 create
= nlh
->nlmsg_flags
& NLM_F_CREATE
? true : false;
1952 afi
= nf_tables_afinfo_lookup(nfmsg
->nfgen_family
, create
);
1954 return PTR_ERR(afi
);
1956 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_TABLE
]);
1958 return PTR_ERR(table
);
1960 nft_ctx_init(&ctx
, skb
, nlh
, afi
, table
, NULL
, nla
);
1962 set
= nf_tables_set_lookup(table
, nla
[NFTA_SET_NAME
]);
1964 if (PTR_ERR(set
) != -ENOENT
)
1965 return PTR_ERR(set
);
1970 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
1972 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1977 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
1980 ops
= nft_select_set_ops(nla
);
1982 return PTR_ERR(ops
);
1985 if (ops
->privsize
!= NULL
)
1986 size
= ops
->privsize(nla
);
1989 set
= kzalloc(sizeof(*set
) + size
, GFP_KERNEL
);
1993 nla_strlcpy(name
, nla
[NFTA_SET_NAME
], sizeof(set
->name
));
1994 err
= nf_tables_set_alloc_name(&ctx
, set
, name
);
1998 INIT_LIST_HEAD(&set
->bindings
);
2006 err
= ops
->init(set
, nla
);
2010 list_add_tail(&set
->list
, &table
->sets
);
2011 nf_tables_set_notify(&ctx
, set
, NFT_MSG_NEWSET
);
2017 module_put(ops
->owner
);
2021 static void nf_tables_set_destroy(const struct nft_ctx
*ctx
, struct nft_set
*set
)
2023 list_del(&set
->list
);
2024 if (!(set
->flags
& NFT_SET_ANONYMOUS
))
2025 nf_tables_set_notify(ctx
, set
, NFT_MSG_DELSET
);
2027 set
->ops
->destroy(set
);
2028 module_put(set
->ops
->owner
);
2032 static int nf_tables_delset(struct sock
*nlsk
, struct sk_buff
*skb
,
2033 const struct nlmsghdr
*nlh
,
2034 const struct nlattr
* const nla
[])
2036 struct nft_set
*set
;
2040 if (nla
[NFTA_SET_TABLE
] == NULL
)
2043 err
= nft_ctx_init_from_setattr(&ctx
, skb
, nlh
, nla
);
2047 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_NAME
]);
2049 return PTR_ERR(set
);
2050 if (!list_empty(&set
->bindings
))
2053 nf_tables_set_destroy(&ctx
, set
);
2057 static int nf_tables_bind_check_setelem(const struct nft_ctx
*ctx
,
2058 const struct nft_set
*set
,
2059 const struct nft_set_iter
*iter
,
2060 const struct nft_set_elem
*elem
)
2062 enum nft_registers dreg
;
2064 dreg
= nft_type_to_reg(set
->dtype
);
2065 return nft_validate_data_load(ctx
, dreg
, &elem
->data
, set
->dtype
);
2068 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2069 struct nft_set_binding
*binding
)
2071 struct nft_set_binding
*i
;
2072 struct nft_set_iter iter
;
2074 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
)
2077 if (set
->flags
& NFT_SET_MAP
) {
2078 /* If the set is already bound to the same chain all
2079 * jumps are already validated for that chain.
2081 list_for_each_entry(i
, &set
->bindings
, list
) {
2082 if (i
->chain
== binding
->chain
)
2089 iter
.fn
= nf_tables_bind_check_setelem
;
2091 set
->ops
->walk(ctx
, set
, &iter
);
2093 /* Destroy anonymous sets if binding fails */
2094 if (set
->flags
& NFT_SET_ANONYMOUS
)
2095 nf_tables_set_destroy(ctx
, set
);
2101 binding
->chain
= ctx
->chain
;
2102 list_add_tail(&binding
->list
, &set
->bindings
);
2106 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2107 struct nft_set_binding
*binding
)
2109 list_del(&binding
->list
);
2111 if (list_empty(&set
->bindings
) && set
->flags
& NFT_SET_ANONYMOUS
)
2112 nf_tables_set_destroy(ctx
, set
);
2119 static const struct nla_policy nft_set_elem_policy
[NFTA_SET_ELEM_MAX
+ 1] = {
2120 [NFTA_SET_ELEM_KEY
] = { .type
= NLA_NESTED
},
2121 [NFTA_SET_ELEM_DATA
] = { .type
= NLA_NESTED
},
2122 [NFTA_SET_ELEM_FLAGS
] = { .type
= NLA_U32
},
2125 static const struct nla_policy nft_set_elem_list_policy
[NFTA_SET_ELEM_LIST_MAX
+ 1] = {
2126 [NFTA_SET_ELEM_LIST_TABLE
] = { .type
= NLA_STRING
},
2127 [NFTA_SET_ELEM_LIST_SET
] = { .type
= NLA_STRING
},
2128 [NFTA_SET_ELEM_LIST_ELEMENTS
] = { .type
= NLA_NESTED
},
2131 static int nft_ctx_init_from_elemattr(struct nft_ctx
*ctx
,
2132 const struct sk_buff
*skb
,
2133 const struct nlmsghdr
*nlh
,
2134 const struct nlattr
* const nla
[])
2136 const struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
2137 const struct nft_af_info
*afi
;
2138 const struct nft_table
*table
;
2140 afi
= nf_tables_afinfo_lookup(nfmsg
->nfgen_family
, false);
2142 return PTR_ERR(afi
);
2144 table
= nf_tables_table_lookup(afi
, nla
[NFTA_SET_ELEM_LIST_TABLE
]);
2146 return PTR_ERR(table
);
2148 nft_ctx_init(ctx
, skb
, nlh
, afi
, table
, NULL
, nla
);
2152 static int nf_tables_fill_setelem(struct sk_buff
*skb
,
2153 const struct nft_set
*set
,
2154 const struct nft_set_elem
*elem
)
2156 unsigned char *b
= skb_tail_pointer(skb
);
2157 struct nlattr
*nest
;
2159 nest
= nla_nest_start(skb
, NFTA_LIST_ELEM
);
2161 goto nla_put_failure
;
2163 if (nft_data_dump(skb
, NFTA_SET_ELEM_KEY
, &elem
->key
, NFT_DATA_VALUE
,
2165 goto nla_put_failure
;
2167 if (set
->flags
& NFT_SET_MAP
&&
2168 !(elem
->flags
& NFT_SET_ELEM_INTERVAL_END
) &&
2169 nft_data_dump(skb
, NFTA_SET_ELEM_DATA
, &elem
->data
,
2170 set
->dtype
== NFT_DATA_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
,
2172 goto nla_put_failure
;
2174 if (elem
->flags
!= 0)
2175 if (nla_put_be32(skb
, NFTA_SET_ELEM_FLAGS
, htonl(elem
->flags
)))
2176 goto nla_put_failure
;
2178 nla_nest_end(skb
, nest
);
2186 struct nft_set_dump_args
{
2187 const struct netlink_callback
*cb
;
2188 struct nft_set_iter iter
;
2189 struct sk_buff
*skb
;
2192 static int nf_tables_dump_setelem(const struct nft_ctx
*ctx
,
2193 const struct nft_set
*set
,
2194 const struct nft_set_iter
*iter
,
2195 const struct nft_set_elem
*elem
)
2197 struct nft_set_dump_args
*args
;
2199 args
= container_of(iter
, struct nft_set_dump_args
, iter
);
2200 return nf_tables_fill_setelem(args
->skb
, set
, elem
);
2203 static int nf_tables_dump_set(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2205 const struct nft_set
*set
;
2206 struct nft_set_dump_args args
;
2208 struct nlattr
*nla
[NFTA_SET_ELEM_LIST_MAX
+ 1];
2209 struct nfgenmsg
*nfmsg
;
2210 struct nlmsghdr
*nlh
;
2211 struct nlattr
*nest
;
2215 nfmsg
= nlmsg_data(cb
->nlh
);
2216 err
= nlmsg_parse(cb
->nlh
, sizeof(*nfmsg
), nla
, NFTA_SET_ELEM_LIST_MAX
,
2217 nft_set_elem_list_policy
);
2221 err
= nft_ctx_init_from_elemattr(&ctx
, cb
->skb
, cb
->nlh
, (void *)nla
);
2225 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
2227 return PTR_ERR(set
);
2229 event
= NFT_MSG_NEWSETELEM
;
2230 event
|= NFNL_SUBSYS_NFTABLES
<< 8;
2231 portid
= NETLINK_CB(cb
->skb
).portid
;
2232 seq
= cb
->nlh
->nlmsg_seq
;
2234 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct nfgenmsg
),
2237 goto nla_put_failure
;
2239 nfmsg
= nlmsg_data(nlh
);
2240 nfmsg
->nfgen_family
= NFPROTO_UNSPEC
;
2241 nfmsg
->version
= NFNETLINK_V0
;
2244 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_TABLE
, ctx
.table
->name
))
2245 goto nla_put_failure
;
2246 if (nla_put_string(skb
, NFTA_SET_ELEM_LIST_SET
, set
->name
))
2247 goto nla_put_failure
;
2249 nest
= nla_nest_start(skb
, NFTA_SET_ELEM_LIST_ELEMENTS
);
2251 goto nla_put_failure
;
2255 args
.iter
.skip
= cb
->args
[0];
2256 args
.iter
.count
= 0;
2258 args
.iter
.fn
= nf_tables_dump_setelem
;
2259 set
->ops
->walk(&ctx
, set
, &args
.iter
);
2261 nla_nest_end(skb
, nest
);
2262 nlmsg_end(skb
, nlh
);
2264 if (args
.iter
.err
&& args
.iter
.err
!= -EMSGSIZE
)
2265 return args
.iter
.err
;
2266 if (args
.iter
.count
== cb
->args
[0])
2269 cb
->args
[0] = args
.iter
.count
;
2276 static int nf_tables_getsetelem(struct sock
*nlsk
, struct sk_buff
*skb
,
2277 const struct nlmsghdr
*nlh
,
2278 const struct nlattr
* const nla
[])
2280 const struct nft_set
*set
;
2284 err
= nft_ctx_init_from_elemattr(&ctx
, skb
, nlh
, nla
);
2288 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
2290 return PTR_ERR(set
);
2292 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2293 struct netlink_dump_control c
= {
2294 .dump
= nf_tables_dump_set
,
2296 return netlink_dump_start(nlsk
, skb
, nlh
, &c
);
2301 static int nft_add_set_elem(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2302 const struct nlattr
*attr
)
2304 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
2305 struct nft_data_desc d1
, d2
;
2306 struct nft_set_elem elem
;
2307 struct nft_set_binding
*binding
;
2308 enum nft_registers dreg
;
2311 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
2312 nft_set_elem_policy
);
2316 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
2320 if (nla
[NFTA_SET_ELEM_FLAGS
] != NULL
) {
2321 elem
.flags
= ntohl(nla_get_be32(nla
[NFTA_SET_ELEM_FLAGS
]));
2322 if (elem
.flags
& ~NFT_SET_ELEM_INTERVAL_END
)
2326 if (set
->flags
& NFT_SET_MAP
) {
2327 if (nla
[NFTA_SET_ELEM_DATA
] == NULL
&&
2328 !(elem
.flags
& NFT_SET_ELEM_INTERVAL_END
))
2331 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
2335 err
= nft_data_init(ctx
, &elem
.key
, &d1
, nla
[NFTA_SET_ELEM_KEY
]);
2339 if (d1
.type
!= NFT_DATA_VALUE
|| d1
.len
!= set
->klen
)
2343 if (set
->ops
->get(set
, &elem
) == 0)
2346 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
) {
2347 err
= nft_data_init(ctx
, &elem
.data
, &d2
, nla
[NFTA_SET_ELEM_DATA
]);
2352 if (set
->dtype
!= NFT_DATA_VERDICT
&& d2
.len
!= set
->dlen
)
2355 dreg
= nft_type_to_reg(set
->dtype
);
2356 list_for_each_entry(binding
, &set
->bindings
, list
) {
2357 struct nft_ctx bind_ctx
= {
2359 .table
= ctx
->table
,
2360 .chain
= binding
->chain
,
2363 err
= nft_validate_data_load(&bind_ctx
, dreg
,
2364 &elem
.data
, d2
.type
);
2370 err
= set
->ops
->insert(set
, &elem
);
2377 if (nla
[NFTA_SET_ELEM_DATA
] != NULL
)
2378 nft_data_uninit(&elem
.data
, d2
.type
);
2380 nft_data_uninit(&elem
.key
, d1
.type
);
2385 static int nf_tables_newsetelem(struct sock
*nlsk
, struct sk_buff
*skb
,
2386 const struct nlmsghdr
*nlh
,
2387 const struct nlattr
* const nla
[])
2389 const struct nlattr
*attr
;
2390 struct nft_set
*set
;
2394 err
= nft_ctx_init_from_elemattr(&ctx
, skb
, nlh
, nla
);
2398 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
2400 return PTR_ERR(set
);
2401 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
2404 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
2405 err
= nft_add_set_elem(&ctx
, set
, attr
);
2412 static int nft_del_setelem(const struct nft_ctx
*ctx
, struct nft_set
*set
,
2413 const struct nlattr
*attr
)
2415 struct nlattr
*nla
[NFTA_SET_ELEM_MAX
+ 1];
2416 struct nft_data_desc desc
;
2417 struct nft_set_elem elem
;
2420 err
= nla_parse_nested(nla
, NFTA_SET_ELEM_MAX
, attr
,
2421 nft_set_elem_policy
);
2426 if (nla
[NFTA_SET_ELEM_KEY
] == NULL
)
2429 err
= nft_data_init(ctx
, &elem
.key
, &desc
, nla
[NFTA_SET_ELEM_KEY
]);
2434 if (desc
.type
!= NFT_DATA_VALUE
|| desc
.len
!= set
->klen
)
2437 err
= set
->ops
->get(set
, &elem
);
2441 set
->ops
->remove(set
, &elem
);
2443 nft_data_uninit(&elem
.key
, NFT_DATA_VALUE
);
2444 if (set
->flags
& NFT_SET_MAP
)
2445 nft_data_uninit(&elem
.data
, set
->dtype
);
2448 nft_data_uninit(&elem
.key
, desc
.type
);
2453 static int nf_tables_delsetelem(struct sock
*nlsk
, struct sk_buff
*skb
,
2454 const struct nlmsghdr
*nlh
,
2455 const struct nlattr
* const nla
[])
2457 const struct nlattr
*attr
;
2458 struct nft_set
*set
;
2462 err
= nft_ctx_init_from_elemattr(&ctx
, skb
, nlh
, nla
);
2466 set
= nf_tables_set_lookup(ctx
.table
, nla
[NFTA_SET_ELEM_LIST_SET
]);
2468 return PTR_ERR(set
);
2469 if (!list_empty(&set
->bindings
) && set
->flags
& NFT_SET_CONSTANT
)
2472 nla_for_each_nested(attr
, nla
[NFTA_SET_ELEM_LIST_ELEMENTS
], rem
) {
2473 err
= nft_del_setelem(&ctx
, set
, attr
);
2480 static const struct nfnl_callback nf_tables_cb
[NFT_MSG_MAX
] = {
2481 [NFT_MSG_NEWTABLE
] = {
2482 .call
= nf_tables_newtable
,
2483 .attr_count
= NFTA_TABLE_MAX
,
2484 .policy
= nft_table_policy
,
2486 [NFT_MSG_GETTABLE
] = {
2487 .call
= nf_tables_gettable
,
2488 .attr_count
= NFTA_TABLE_MAX
,
2489 .policy
= nft_table_policy
,
2491 [NFT_MSG_DELTABLE
] = {
2492 .call
= nf_tables_deltable
,
2493 .attr_count
= NFTA_TABLE_MAX
,
2494 .policy
= nft_table_policy
,
2496 [NFT_MSG_NEWCHAIN
] = {
2497 .call
= nf_tables_newchain
,
2498 .attr_count
= NFTA_CHAIN_MAX
,
2499 .policy
= nft_chain_policy
,
2501 [NFT_MSG_GETCHAIN
] = {
2502 .call
= nf_tables_getchain
,
2503 .attr_count
= NFTA_CHAIN_MAX
,
2504 .policy
= nft_chain_policy
,
2506 [NFT_MSG_DELCHAIN
] = {
2507 .call
= nf_tables_delchain
,
2508 .attr_count
= NFTA_CHAIN_MAX
,
2509 .policy
= nft_chain_policy
,
2511 [NFT_MSG_NEWRULE
] = {
2512 .call
= nf_tables_newrule
,
2513 .attr_count
= NFTA_RULE_MAX
,
2514 .policy
= nft_rule_policy
,
2516 [NFT_MSG_GETRULE
] = {
2517 .call
= nf_tables_getrule
,
2518 .attr_count
= NFTA_RULE_MAX
,
2519 .policy
= nft_rule_policy
,
2521 [NFT_MSG_DELRULE
] = {
2522 .call
= nf_tables_delrule
,
2523 .attr_count
= NFTA_RULE_MAX
,
2524 .policy
= nft_rule_policy
,
2526 [NFT_MSG_NEWSET
] = {
2527 .call
= nf_tables_newset
,
2528 .attr_count
= NFTA_SET_MAX
,
2529 .policy
= nft_set_policy
,
2531 [NFT_MSG_GETSET
] = {
2532 .call
= nf_tables_getset
,
2533 .attr_count
= NFTA_SET_MAX
,
2534 .policy
= nft_set_policy
,
2536 [NFT_MSG_DELSET
] = {
2537 .call
= nf_tables_delset
,
2538 .attr_count
= NFTA_SET_MAX
,
2539 .policy
= nft_set_policy
,
2541 [NFT_MSG_NEWSETELEM
] = {
2542 .call
= nf_tables_newsetelem
,
2543 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
2544 .policy
= nft_set_elem_list_policy
,
2546 [NFT_MSG_GETSETELEM
] = {
2547 .call
= nf_tables_getsetelem
,
2548 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
2549 .policy
= nft_set_elem_list_policy
,
2551 [NFT_MSG_DELSETELEM
] = {
2552 .call
= nf_tables_delsetelem
,
2553 .attr_count
= NFTA_SET_ELEM_LIST_MAX
,
2554 .policy
= nft_set_elem_list_policy
,
2558 static const struct nfnetlink_subsystem nf_tables_subsys
= {
2559 .name
= "nf_tables",
2560 .subsys_id
= NFNL_SUBSYS_NFTABLES
,
2561 .cb_count
= NFT_MSG_MAX
,
2566 * Loop detection - walk through the ruleset beginning at the destination chain
2567 * of a new jump until either the source chain is reached (loop) or all
2568 * reachable chains have been traversed.
2570 * The loop check is performed whenever a new jump verdict is added to an
2571 * expression or verdict map or a verdict map is bound to a new chain.
2574 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
2575 const struct nft_chain
*chain
);
2577 static int nf_tables_loop_check_setelem(const struct nft_ctx
*ctx
,
2578 const struct nft_set
*set
,
2579 const struct nft_set_iter
*iter
,
2580 const struct nft_set_elem
*elem
)
2582 switch (elem
->data
.verdict
) {
2585 return nf_tables_check_loops(ctx
, elem
->data
.chain
);
2591 static int nf_tables_check_loops(const struct nft_ctx
*ctx
,
2592 const struct nft_chain
*chain
)
2594 const struct nft_rule
*rule
;
2595 const struct nft_expr
*expr
, *last
;
2596 const struct nft_set
*set
;
2597 struct nft_set_binding
*binding
;
2598 struct nft_set_iter iter
;
2600 if (ctx
->chain
== chain
)
2603 list_for_each_entry(rule
, &chain
->rules
, list
) {
2604 nft_rule_for_each_expr(expr
, last
, rule
) {
2605 const struct nft_data
*data
= NULL
;
2608 if (!expr
->ops
->validate
)
2611 err
= expr
->ops
->validate(ctx
, expr
, &data
);
2618 switch (data
->verdict
) {
2621 err
= nf_tables_check_loops(ctx
, data
->chain
);
2630 list_for_each_entry(set
, &ctx
->table
->sets
, list
) {
2631 if (!(set
->flags
& NFT_SET_MAP
) ||
2632 set
->dtype
!= NFT_DATA_VERDICT
)
2635 list_for_each_entry(binding
, &set
->bindings
, list
) {
2636 if (binding
->chain
!= chain
)
2642 iter
.fn
= nf_tables_loop_check_setelem
;
2644 set
->ops
->walk(ctx
, set
, &iter
);
2654 * nft_validate_input_register - validate an expressions' input register
2656 * @reg: the register number
2658 * Validate that the input register is one of the general purpose
2661 int nft_validate_input_register(enum nft_registers reg
)
2663 if (reg
<= NFT_REG_VERDICT
)
2665 if (reg
> NFT_REG_MAX
)
2669 EXPORT_SYMBOL_GPL(nft_validate_input_register
);
2672 * nft_validate_output_register - validate an expressions' output register
2674 * @reg: the register number
2676 * Validate that the output register is one of the general purpose
2677 * registers or the verdict register.
2679 int nft_validate_output_register(enum nft_registers reg
)
2681 if (reg
< NFT_REG_VERDICT
)
2683 if (reg
> NFT_REG_MAX
)
2687 EXPORT_SYMBOL_GPL(nft_validate_output_register
);
2690 * nft_validate_data_load - validate an expressions' data load
2692 * @ctx: context of the expression performing the load
2693 * @reg: the destination register number
2694 * @data: the data to load
2695 * @type: the data type
2697 * Validate that a data load uses the appropriate data type for
2698 * the destination register. A value of NULL for the data means
2699 * that its runtime gathered data, which is always of type
2702 int nft_validate_data_load(const struct nft_ctx
*ctx
, enum nft_registers reg
,
2703 const struct nft_data
*data
,
2704 enum nft_data_types type
)
2709 case NFT_REG_VERDICT
:
2710 if (data
== NULL
|| type
!= NFT_DATA_VERDICT
)
2713 if (data
->verdict
== NFT_GOTO
|| data
->verdict
== NFT_JUMP
) {
2714 err
= nf_tables_check_loops(ctx
, data
->chain
);
2718 if (ctx
->chain
->level
+ 1 > data
->chain
->level
) {
2719 if (ctx
->chain
->level
+ 1 == NFT_JUMP_STACK_SIZE
)
2721 data
->chain
->level
= ctx
->chain
->level
+ 1;
2727 if (data
!= NULL
&& type
!= NFT_DATA_VALUE
)
2732 EXPORT_SYMBOL_GPL(nft_validate_data_load
);
2734 static const struct nla_policy nft_verdict_policy
[NFTA_VERDICT_MAX
+ 1] = {
2735 [NFTA_VERDICT_CODE
] = { .type
= NLA_U32
},
2736 [NFTA_VERDICT_CHAIN
] = { .type
= NLA_STRING
,
2737 .len
= NFT_CHAIN_MAXNAMELEN
- 1 },
2740 static int nft_verdict_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
2741 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
2743 struct nlattr
*tb
[NFTA_VERDICT_MAX
+ 1];
2744 struct nft_chain
*chain
;
2747 err
= nla_parse_nested(tb
, NFTA_VERDICT_MAX
, nla
, nft_verdict_policy
);
2751 if (!tb
[NFTA_VERDICT_CODE
])
2753 data
->verdict
= ntohl(nla_get_be32(tb
[NFTA_VERDICT_CODE
]));
2755 switch (data
->verdict
) {
2762 desc
->len
= sizeof(data
->verdict
);
2766 if (!tb
[NFTA_VERDICT_CHAIN
])
2768 chain
= nf_tables_chain_lookup(ctx
->table
,
2769 tb
[NFTA_VERDICT_CHAIN
]);
2771 return PTR_ERR(chain
);
2772 if (chain
->flags
& NFT_BASE_CHAIN
)
2776 data
->chain
= chain
;
2777 desc
->len
= sizeof(data
);
2783 desc
->type
= NFT_DATA_VERDICT
;
2787 static void nft_verdict_uninit(const struct nft_data
*data
)
2789 switch (data
->verdict
) {
2797 static int nft_verdict_dump(struct sk_buff
*skb
, const struct nft_data
*data
)
2799 struct nlattr
*nest
;
2801 nest
= nla_nest_start(skb
, NFTA_DATA_VERDICT
);
2803 goto nla_put_failure
;
2805 if (nla_put_be32(skb
, NFTA_VERDICT_CODE
, htonl(data
->verdict
)))
2806 goto nla_put_failure
;
2808 switch (data
->verdict
) {
2811 if (nla_put_string(skb
, NFTA_VERDICT_CHAIN
, data
->chain
->name
))
2812 goto nla_put_failure
;
2814 nla_nest_end(skb
, nest
);
2821 static int nft_value_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
2822 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
2829 if (len
> sizeof(data
->data
))
2832 nla_memcpy(data
->data
, nla
, sizeof(data
->data
));
2833 desc
->type
= NFT_DATA_VALUE
;
2838 static int nft_value_dump(struct sk_buff
*skb
, const struct nft_data
*data
,
2841 return nla_put(skb
, NFTA_DATA_VALUE
, len
, data
->data
);
2844 static const struct nla_policy nft_data_policy
[NFTA_DATA_MAX
+ 1] = {
2845 [NFTA_DATA_VALUE
] = { .type
= NLA_BINARY
,
2846 .len
= FIELD_SIZEOF(struct nft_data
, data
) },
2847 [NFTA_DATA_VERDICT
] = { .type
= NLA_NESTED
},
2851 * nft_data_init - parse nf_tables data netlink attributes
2853 * @ctx: context of the expression using the data
2854 * @data: destination struct nft_data
2855 * @desc: data description
2856 * @nla: netlink attribute containing data
2858 * Parse the netlink data attributes and initialize a struct nft_data.
2859 * The type and length of data are returned in the data description.
2861 * The caller can indicate that it only wants to accept data of type
2862 * NFT_DATA_VALUE by passing NULL for the ctx argument.
2864 int nft_data_init(const struct nft_ctx
*ctx
, struct nft_data
*data
,
2865 struct nft_data_desc
*desc
, const struct nlattr
*nla
)
2867 struct nlattr
*tb
[NFTA_DATA_MAX
+ 1];
2870 err
= nla_parse_nested(tb
, NFTA_DATA_MAX
, nla
, nft_data_policy
);
2874 if (tb
[NFTA_DATA_VALUE
])
2875 return nft_value_init(ctx
, data
, desc
, tb
[NFTA_DATA_VALUE
]);
2876 if (tb
[NFTA_DATA_VERDICT
] && ctx
!= NULL
)
2877 return nft_verdict_init(ctx
, data
, desc
, tb
[NFTA_DATA_VERDICT
]);
2880 EXPORT_SYMBOL_GPL(nft_data_init
);
2883 * nft_data_uninit - release a nft_data item
2885 * @data: struct nft_data to release
2886 * @type: type of data
2888 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
2889 * all others need to be released by calling this function.
2891 void nft_data_uninit(const struct nft_data
*data
, enum nft_data_types type
)
2894 case NFT_DATA_VALUE
:
2896 case NFT_DATA_VERDICT
:
2897 return nft_verdict_uninit(data
);
2902 EXPORT_SYMBOL_GPL(nft_data_uninit
);
2904 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
2905 enum nft_data_types type
, unsigned int len
)
2907 struct nlattr
*nest
;
2910 nest
= nla_nest_start(skb
, attr
);
2915 case NFT_DATA_VALUE
:
2916 err
= nft_value_dump(skb
, data
, len
);
2918 case NFT_DATA_VERDICT
:
2919 err
= nft_verdict_dump(skb
, data
);
2926 nla_nest_end(skb
, nest
);
2929 EXPORT_SYMBOL_GPL(nft_data_dump
);
2931 static int __init
nf_tables_module_init(void)
2935 info
= kmalloc(sizeof(struct nft_expr_info
) * NFT_RULE_MAXEXPRS
,
2942 err
= nf_tables_core_module_init();
2946 err
= nfnetlink_subsys_register(&nf_tables_subsys
);
2950 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
2953 nf_tables_core_module_exit();
2960 static void __exit
nf_tables_module_exit(void)
2962 nfnetlink_subsys_unregister(&nf_tables_subsys
);
2963 nf_tables_core_module_exit();
2967 module_init(nf_tables_module_init
);
2968 module_exit(nf_tables_module_exit
);
2970 MODULE_LICENSE("GPL");
2971 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
2972 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES
);