Netfilter updates for net-next
Subsystems (3)
Pablo Neira Ayuso
maintainercover Netfilter updates for net-next
Hi David,
The following patchset contains Netfilter updates for your net-next tree:
1) Get rid of nf_sk_is_transparent(), use inet_sk_transparent() instead. From Máté Eckl.
2) Move shared tproxy infrastructure to nf_tproxy_ipv4 and nf_tproxy_ipv6. Also from Máté.
3) Add hashtable to speed up chain lookups by name, from Florian Westphal.
4) Patch series to add connlimit support reusing part of the nf_conncount infrastructure. This includes preparation changes such passing context to the object and expression destroy interface; garbage collection for expressions embedded into set elements, and the introduction of the clone_destroy interface for expressions.
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git
Thanks.
----------------------------------------------------------------
The following changes since commit 1ffdd8e1643f6ce28792edd3314be84167faabf1:
Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next (2018-06-02 09:04:21 -0400)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git HEAD
for you to fetch changes up to 9b73c579df3d3d6359a010beda9b03be5a4c3ed5:
netfilter: nf_tables: handle chain name lookups via rhltable (2018-06-03 00:02:13 +0200)
----------------------------------------------------------------
Florian Westphal (1):
netfilter: nf_tables: handle chain name lookups via rhltable
Máté Eckl (2):
netfilter: Decrease code duplication regarding transparent socket option
netfilter: Libify xt_TPROXY
Pablo Neira Ayuso (6):
netfilter: nf_tables: pass context to object destroy indirection
netfilter: nf_conncount: expose connection list interface
netfilter: nf_tables: pass ctx to nf_tables_expr_destroy()
netfilter: nf_tables: garbage collection for stateful expressions
netfilter: nf_tables: add destroy_clone expression
netfilter: nf_tables: add connlimit support
include/net/netfilter/nf_conntrack_count.h | 11 + include/net/netfilter/nf_socket.h | 13 - include/net/netfilter/nf_tables.h | 20 +- include/net/netfilter/nf_tproxy.h | 113 +++++++++ include/uapi/linux/netfilter/nf_tables.h | 21 +- net/ipv4/netfilter/Kconfig | 5 +- net/ipv4/netfilter/Makefile | 1 + net/ipv4/netfilter/nf_tproxy_ipv4.c | 147 ++++++++++++ net/ipv6/netfilter/Kconfig | 5 +- net/ipv6/netfilter/Makefile | 1 + net/ipv6/netfilter/nf_tproxy_ipv6.c | 146 ++++++++++++ net/netfilter/Kconfig | 11 + net/netfilter/Makefile | 1 + net/netfilter/nf_conncount.c | 36 ++- net/netfilter/nf_tables_api.c | 151 ++++++++++-- net/netfilter/nft_counter.c | 4 +- net/netfilter/nft_ct.c | 3 +- net/netfilter/nft_dynset.c | 9 + net/netfilter/nft_set_hash.c | 21 +- net/netfilter/nft_socket.c | 3 +- net/netfilter/xt_TPROXY.c | 366 ++--------------------------- net/netfilter/xt_socket.c | 4 +- 22 files changed, 680 insertions(+), 412 deletions(-) create mode 100644 include/net/netfilter/nf_tproxy.h create mode 100644 net/ipv4/netfilter/nf_tproxy_ipv4.c create mode 100644 net/ipv6/netfilter/nf_tproxy_ipv6.c
Pablo Neira Ayuso
maintainerPATCH 1/9 netfilter: Decrease code duplication regarding transparent socket option
From: Máté Eckl <[email protected]>
There is a function in include/net/netfilter/nf_socket.h to decide if a socket has IP(V6)_TRANSPARENT socket option set or not. However this does the same as inet_sk_transparent() in include/net/tcp.h
include/net/tcp.h:1733
/* This helper checks if socket has IP_TRANSPARENT set */ static inline bool inet_sk_transparent(const struct sock *sk)
{
switch (sk->sk_state) {
case TCP_TIME_WAIT:
return inet_twsk(sk)->tw_transparent;
case TCP_NEW_SYN_RECV:
return inet_rsk(inet_reqsk(sk))->no_srccheck;
}
return inet_sk(sk)->transparent;
}tproxy_sk_is_transparent has also been refactored to use this function instead of reimplementing it.
--- include/net/netfilter/nf_socket.h | 13 ------------- net/netfilter/nft_socket.c | 3 ++- net/netfilter/xt_TPROXY.c | 15 ++------------- net/netfilter/xt_socket.c | 4 ++-- 4 files changed, 6 insertions(+), 29 deletions(-)
include/net/netfilter/nf_socket.h +0 -13
@@ -3,19 +3,6 @@#define _NF_SOCK_H_#include <net/sock.h> -#include <net/inet_timewait_sock.h> - -static inline bool nf_sk_is_transparent (struct sock *sk) -{ -switch (sk->sk_state) { -case TCP_TIME_WAIT : -return inet_twsk (sk)->tw_transparent; -case TCP_NEW_SYN_RECV : -return inet_rsk (inet_reqsk (sk))->no_srccheck; -default : -return inet_sk (sk)->transparent; - } -}struct sock *nf_sk_lookup_slow_v4 (struct net *net,const struct sk_buff *skb,const struct net_device *indev);
net/netfilter/nft_socket.c +2 -1
@@ -5,6 +5,7 @@#include <net/netfilter/nf_tables_core.h> #include <net/netfilter/nf_socket.h> #include <net/inet_sock.h> +#include <net/tcp.h> struct nft_socket {enum nft_socket_keys key:8 ;
@@ -48,7 +49,7 @@ static void nft_socket_eval(const struct nft_expr *expr,switch (priv->key) {case NFT_SOCKET_TRANSPARENT : -nft_reg_store8 (dest,nf_sk_is_transparent (sk)); +nft_reg_store8 (dest,inet_sk_transparent (sk));break ;default :WARN_ON (1 );
net/netfilter/xt_TPROXY.c +2 -13
@@ -42,19 +42,8 @@ enum nf_tproxy_lookup_t {static bool tproxy_sk_is_transparent (struct sock *sk) { -switch (sk->sk_state) { -case TCP_TIME_WAIT : -if (inet_twsk (sk)->tw_transparent) -return true; -break ; -case TCP_NEW_SYN_RECV : -if (inet_rsk (inet_reqsk (sk))->no_srccheck) -return true; -break ; -default : -if (inet_sk (sk)->transparent) -return true; - } +if (inet_sk_transparent (sk)) +return true;sock_gen_put (sk);return false;
net/netfilter/xt_socket.c +2 -2
@@ -73,7 +73,7 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,* if XT_SOCKET_TRANSPARENT is used */ if (info->flags &XT_SOCKET_TRANSPARENT ) - transparent =nf_sk_is_transparent (sk); + transparent =inet_sk_transparent (sk);if (info->flags &XT_SOCKET_RESTORESKMARK && !wildcard && transparent &&sk_fullsock (sk))
@@ -130,7 +130,7 @@ socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)* if XT_SOCKET_TRANSPARENT is used */ if (info->flags &XT_SOCKET_TRANSPARENT ) - transparent =nf_sk_is_transparent (sk); + transparent =inet_sk_transparent (sk);if (info->flags &XT_SOCKET_RESTORESKMARK && !wildcard && transparent &&sk_fullsock (sk))
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 2/9 netfilter: Libify xt_TPROXY
In reply to Pablo Neira AyusoFrom: Máté Eckl <[email protected]>
The extracted functions will likely be usefull to implement tproxy support in nf_tables.
Extrancted functions:
- nf_tproxy_sk_is_transparent
- nf_tproxy_laddr4
- nf_tproxy_handle_time_wait4
- nf_tproxy_get_sock_v4
- nf_tproxy_laddr6
- nf_tproxy_handle_time_wait6
- nf_tproxy_get_sock_v6
(nf_)tproxy_handle_time_wait6 also needed some refactor as its current implementation was xtables-specific.
--- include/net/netfilter/nf_tproxy.h | 113 ++++++++++++ net/ipv4/netfilter/Kconfig | 5 +- net/ipv4/netfilter/Makefile | 1 + net/ipv4/netfilter/nf_tproxy_ipv4.c | 147 +++++++++++++++ net/ipv6/netfilter/Kconfig | 5 +- net/ipv6/netfilter/Makefile | 1 + net/ipv6/netfilter/nf_tproxy_ipv6.c | 146 +++++++++++++++ net/netfilter/Kconfig | 2 + net/netfilter/xt_TPROXY.c | 355 ++---------------------------------- 9 files changed, 436 insertions(+), 339 deletions(-) create mode 100644 include/net/netfilter/nf_tproxy.h create mode 100644 net/ipv4/netfilter/nf_tproxy_ipv4.c create mode 100644 net/ipv6/netfilter/nf_tproxy_ipv6.c
include/net/netfilter/nf_tproxy.h new file +113 -0
@@ -0,0 +1,113 @@ +#ifndef _NF_TPROXY_H_ +#define _NF_TPROXY_H_ + +#include <net/tcp.h> + +enum nf_tproxy_lookup_t { +NF_TPROXY_LOOKUP_LISTENER , +NF_TPROXY_LOOKUP_ESTABLISHED , +}; + +static inline bool nf_tproxy_sk_is_transparent (struct sock *sk) +{ +if (inet_sk_transparent (sk)) +return true; + +sock_gen_put (sk); +return false; +} + +__be32 nf_tproxy_laddr4 (struct sk_buff *skb,__be32 user_laddr,__be32 daddr); + +/** +* nf_tproxy_handle_time_wait4 - handle IPv4 TCP TIME_WAIT reopen redirections +* @skb: The skb being processed. +* @laddr: IPv4 address to redirect to or zero. +* @lport: TCP port to redirect to or zero. +* @sk: The TIME_WAIT TCP socket found by the lookup. +* +* We have to handle SYN packets arriving to TIME_WAIT sockets +* differently: instead of reopening the connection we should rather +* redirect the new connection to the proxy if there's a listener +* socket present. +* +* nf_tproxy_handle_time_wait4() consumes the socket reference passed in. +* +* Returns the listener socket if there's one, the TIME_WAIT socket if +* no such listener is found, or NULL if the TCP header is incomplete. +*/ +struct sock * +nf_tproxy_handle_time_wait4 (struct net *net,struct sk_buff *skb, +__be32 laddr,__be16 lport,struct sock *sk); + +/* +* This is used when the user wants to intercept a connection matching +* an explicit iptables rule. In this case the sockets are assumed +* matching in preference order: +* +* - match: if there's a fully established connection matching the +* _packet_ tuple, it is returned, assuming the redirection +* already took place and we process a packet belonging to an +* established connection +* +* - match: if there's a listening socket matching the redirection +* (e.g. on-port & on-ip of the connection), it is returned, +* regardless if it was bound to 0.0.0.0 or an explicit +* address. The reasoning is that if there's an explicit rule, it +* does not really matter if the listener is bound to an interface +* or to 0. The user already stated that he wants redirection +* (since he added the rule). +* +* Please note that there's an overlap between what a TPROXY target +* and a socket match will match. Normally if you have both rules the +* "socket" match will be the first one, effectively all packets +* belonging to established connections going through that one. +*/ +struct sock * +nf_tproxy_get_sock_v4 (struct net *net,struct sk_buff *skb,void *hp, +const u8 protocol, +const __be32 saddr,const __be32 daddr, +const __be16 sport,const __be16 dport, +const struct net_device *in, +const enum nf_tproxy_lookup_t lookup_type); + +const struct in6_addr * +nf_tproxy_laddr6 (struct sk_buff *skb,const struct in6_addr *user_laddr, +const struct in6_addr *daddr); + +/** +* nf_tproxy_handle_time_wait6 - handle IPv6 TCP TIME_WAIT reopen redirections +* @skb: The skb being processed. +* @tproto: Transport protocol. +* @thoff: Transport protocol header offset. +* @net: Network namespace. +* @laddr: IPv6 address to redirect to. +* @lport: TCP port to redirect to or zero. +* @sk: The TIME_WAIT TCP socket found by the lookup. +* +* We have to handle SYN packets arriving to TIME_WAIT sockets +* differently: instead of reopening the connection we should rather +* redirect the new connection to the proxy if there's a listener +* socket present. +* +* nf_tproxy_handle_time_wait6() consumes the socket reference passed in. +* +* Returns the listener socket if there's one, the TIME_WAIT socket if +* no such listener is found, or NULL if the TCP header is incomplete. +*/ +struct sock * +nf_tproxy_handle_time_wait6 (struct sk_buff *skb,int tproto,int thoff, +struct net *net, +const struct in6_addr *laddr, +const __be16 lport, +struct sock *sk); + +struct sock * +nf_tproxy_get_sock_v6 (struct net *net,struct sk_buff *skb,int thoff,void *hp, +const u8 protocol, +const struct in6_addr *saddr,const struct in6_addr *daddr, +const __be16 sport,const __be16 dport, +const struct net_device *in, +const enum nf_tproxy_lookup_t lookup_type); + +#endif /* _NF_TPROXY_H_ */
net/ipv4/netfilter/Kconfig +4 -1
@@ -29,7 +29,10 @@ config NF_SOCKET_IPV4 tristate "IPv4 socket lookup support" help This option enables the IPv4 socket lookup infrastructure. This is - is required by the iptables socket match. + is required by the {ip,nf}tables socket match. + +config NF_TPROXY_IPV4 + tristate "IPv4 tproxy support" if NF_TABLES
net/ipv4/netfilter/Makefile +1 -0
@@ -17,6 +17,7 @@ obj-$(CONFIG_NF_NAT_IPV4) += nf_nat_ipv4.o obj-$(CONFIG_NF_DEFRAG_IPV4) += nf_defrag_ipv4.o obj-$(CONFIG_NF_SOCKET_IPV4) += nf_socket_ipv4.o +obj-$(CONFIG_NF_TPROXY_IPV4) += nf_tproxy_ipv4.o # logging obj-$(CONFIG_NF_LOG_ARP) += nf_log_arp.o
net/ipv4/netfilter/nf_tproxy_ipv4.c new file +147 -0
147 lines not on this page · raw message
net/ipv6/netfilter/Kconfig +4 -1
@@ -29,7 +29,10 @@ config NF_SOCKET_IPV6 tristate "IPv6 socket lookup support" help This option enables the IPv6 socket lookup infrastructure. This - is used by the ip6tables socket match. + is used by the {ip6,nf}tables socket match. + +config NF_TPROXY_IPV6 + tristate "IPv6 tproxy support" if NF_TABLES
net/ipv6/netfilter/Makefile +1 -0
@@ -26,6 +26,7 @@ nf_defrag_ipv6-y := nf_defrag_ipv6_hooks.o nf_conntrack_reasm.o obj-$(CONFIG_NF_DEFRAG_IPV6) += nf_defrag_ipv6.o obj-$(CONFIG_NF_SOCKET_IPV6) += nf_socket_ipv6.o +obj-$(CONFIG_NF_TPROXY_IPV6) += nf_tproxy_ipv6.o # logging obj-$(CONFIG_NF_LOG_IPV6) += nf_log_ipv6.o
net/ipv6/netfilter/nf_tproxy_ipv6.c new file +146 -0
146 lines not on this page · raw message
net/netfilter/Kconfig +2 -0
@@ -989,6 +989,8 @@ config NETFILTER_XT_TARGET_TPROXY depends on IP_NF_MANGLE select NF_DEFRAG_IPV4 select NF_DEFRAG_IPV6 if IP6_NF_IPTABLES != n + select NF_TPROXY_IPV4 + select NF_TPROXY_IPV6 if IP6_NF_IPTABLES help This option adds a `TPROXY' target, which is somewhat similar to REDIRECT. It can only be used in the mangle table and is useful
net/netfilter/xt_TPROXY.c +18 -337
409 lines not on this page · raw message
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 3/9 netfilter: nf_tables: pass context to object destroy indirection
In reply to Pablo Neira AyusoThe new connlimit object needs this to properly deal with conntrack dependencies.
--- include/net/netfilter/nf_tables.h | 3 ++- net/netfilter/nf_tables_api.c | 12 ++++++------ net/netfilter/nft_counter.c | 3 ++- net/netfilter/nft_ct.c | 3 ++- 4 files changed, 12 insertions(+), 9 deletions(-)
include/net/netfilter/nf_tables.h +2 -1
@@ -1070,7 +1070,8 @@ struct nft_object_ops {int (*init)(const struct nft_ctx *ctx,const struct nlattr *const tb[],struct nft_object *obj); -void (*destroy)(struct nft_object *obj); +void (*destroy)(const struct nft_ctx *ctx, +struct nft_object *obj);int (*dump)(struct sk_buff *skb,struct nft_object *obj,bool reset);
net/netfilter/nf_tables_api.c +6 -6
@@ -4787,7 +4787,7 @@ static int nf_tables_newobj(struct net *net, struct sock *nlsk,kfree (obj->name); err2:if (obj->ops->destroy) - obj->ops->destroy (obj); + obj->ops->destroy (&ctx, obj);kfree (obj); err1:module_put (type->owner);
@@ -4997,10 +4997,10 @@ static int nf_tables_getobj(struct net *net, struct sock *nlsk,return err; } -static void nft_obj_destroy (struct nft_object *obj) +static void nft_obj_destroy (const struct nft_ctx *ctx,struct nft_object *obj) {if (obj->ops->destroy) - obj->ops->destroy (obj); + obj->ops->destroy (ctx, obj);module_put (obj->ops->type->owner);kfree (obj->name);
@@ -6003,7 +6003,7 @@ static void nft_commit_release(struct nft_trans *trans) nft_trans_elem(trans).priv);break ;case NFT_MSG_DELOBJ : -nft_obj_destroy (nft_trans_obj (trans)); +nft_obj_destroy (&trans->ctx,nft_trans_obj (trans));break ;case NFT_MSG_DELFLOWTABLE :nf_tables_flowtable_destroy (nft_trans_flowtable (trans));
@@ -6328,7 +6328,7 @@ static void nf_tables_abort_release(struct nft_trans *trans) nft_trans_elem(trans).priv, true);break ;case NFT_MSG_NEWOBJ : -nft_obj_destroy (nft_trans_obj (trans)); +nft_obj_destroy (&trans->ctx,nft_trans_obj (trans));break ;case NFT_MSG_NEWFLOWTABLE :nf_tables_flowtable_destroy (nft_trans_flowtable (trans));
@@ -7022,7 +7022,7 @@ static void __nft_release_tables(struct net *net)list_for_each_entry_safe (obj, ne, &table->objects, list) {list_del (&obj->list); table->use--; -nft_obj_destroy (obj); +nft_obj_destroy (&ctx, obj); }list_for_each_entry_safe (chain, nc, &table->chains, list) { ctx.chain = chain;
net/netfilter/nft_counter.c +2 -1
@@ -96,7 +96,8 @@ static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)free_percpu (priv->counter); } -static void nft_counter_obj_destroy (struct nft_object *obj) +static void nft_counter_obj_destroy (const struct nft_ctx *ctx, +struct nft_object *obj) {struct nft_counter_percpu_priv *priv =nft_obj_data (obj);
net/netfilter/nft_ct.c +2 -1
@@ -826,7 +826,8 @@ static int nft_ct_helper_obj_init(const struct nft_ctx *ctx,return 0 ; } -static void nft_ct_helper_obj_destroy (struct nft_object *obj) +static void nft_ct_helper_obj_destroy (const struct nft_ctx *ctx, +struct nft_object *obj) {struct nft_ct_helper_obj *priv =nft_obj_data (obj);
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 4/9 netfilter: nf_conncount: expose connection list interface
In reply to Pablo Neira AyusoThis patch provides an interface to maintain the list of connections and the lookup function to obtain the number of connections in the list.
--- include/net/netfilter/nf_conntrack_count.h | 11 +++++++++ net/netfilter/nf_conncount.c | 36 +++++++++++++++++++----------- 2 files changed, 34 insertions(+), 13 deletions(-)
include/net/netfilter/nf_conntrack_count.h +11 -0
@@ -13,4 +13,15 @@ unsigned int nf_conncount_count(struct net *net,const u32 *key, const struct nf_conntrack_tuple *tuple, const struct nf_conntrack_zone *zone); + +unsigned int nf_conncount_lookup (struct net *net,struct hlist_head *head, +const struct nf_conntrack_tuple *tuple, +const struct nf_conntrack_zone *zone, +bool *addit); + +bool nf_conncount_add (struct hlist_head *head, +const struct nf_conntrack_tuple *tuple); + +void nf_conncount_cache_free (struct hlist_head *hhead); +#endif
net/netfilter/nf_conncount.c +23 -13
@@ -79,7 +79,7 @@ static int key_diff(const u32 *a, const u32 *b, unsigned int klen)return memcmp (a, b, klen *sizeof (u32)); } -static bool add_hlist (struct hlist_head *head, +bool nf_conncount_add (struct hlist_head *head,const struct nf_conntrack_tuple *tuple) {struct nf_conncount_tuple *conn;
@@ -91,12 +91,12 @@ static bool add_hlist(struct hlist_head *head,hlist_add_head (&conn->node, head);return true; } +EXPORT_SYMBOL_GPL (nf_conncount_add); -static unsigned int check_hlist (struct net *net, -struct hlist_head *head, -const struct nf_conntrack_tuple *tuple, -const struct nf_conntrack_zone *zone, -bool *addit) +unsigned int nf_conncount_lookup (struct net *net,struct hlist_head *head, +const struct nf_conntrack_tuple *tuple, +const struct nf_conntrack_zone *zone, +bool *addit) {const struct nf_conntrack_tuple_hash *found;struct nf_conncount_tuple *conn;
@@ -141,6 +141,7 @@ static unsigned int check_hlist(struct net *net,return length; } +EXPORT_SYMBOL_GPL (nf_conncount_lookup); static void tree_nodes_free(struct rb_root *root, struct nf_conncount_rb *gc_nodes[],
@@ -187,13 +188,15 @@ count_tree(struct net *net, struct rb_root *root, }else {/* same source network -> be counted! */ unsigned int count; - count =check_hlist (net, &rbconn->hhead, tuple, zone, &addit); + + count =nf_conncount_lookup (net, &rbconn->hhead, tuple, + zone, &addit);tree_nodes_free (root, gc_nodes, gc_count);if (!addit)return count; -if (!add_hlist (&rbconn->hhead, tuple)) +if (!nf_conncount_add (&rbconn->hhead, tuple))return 0 ;/* hotdrop */ return count +1 ;
@@ -203,7 +206,7 @@ count_tree(struct net *net, struct rb_root *root,continue ;/* only used for GC on hhead, retval and 'addit' ignored */ -check_hlist (net, &rbconn->hhead, tuple, zone, &addit); +nf_conncount_lookup (net, &rbconn->hhead, tuple, zone, &addit);if (hlist_empty (&rbconn->hhead)) gc_nodes[gc_count++] = rbconn; }
@@ -303,11 +306,19 @@ struct nf_conncount_data *nf_conncount_init(struct net *net, unsigned int family }EXPORT_SYMBOL_GPL (nf_conncount_init); -static void destroy_tree (struct rb_root *r) +void nf_conncount_cache_free (struct hlist_head *hhead) {struct nf_conncount_tuple *conn; -struct nf_conncount_rb *rbconn;struct hlist_node *n; + +hlist_for_each_entry_safe (conn, n, hhead, node) +kmem_cache_free (conncount_conn_cachep, conn); +} +EXPORT_SYMBOL_GPL (nf_conncount_cache_free); + +static void destroy_tree (struct rb_root *r) +{ +struct nf_conncount_rb *rbconn;struct rb_node *node;while ((node =rb_first (r)) !=NULL ) {
@@ -315,8 +326,7 @@ static void destroy_tree(struct rb_root *r)rb_erase (node, r); -hlist_for_each_entry_safe (conn, n, &rbconn->hhead, node) -kmem_cache_free (conncount_conn_cachep, conn); +nf_conncount_cache_free (&rbconn->hhead);kmem_cache_free (conncount_rb_cachep, rbconn); }
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 5/9 netfilter: nf_tables: pass ctx to nf_tables_expr_destroy()
In reply to Pablo Neira Ayusonft_set_elem_destroy() can be called from call_rcu context. Annotate netns and table in set object so we can populate the context object. Moreover, pass context object to nf_tables_set_elem_destroy() from the commit phase, since it is already available from there.
--- include/net/netfilter/nf_tables.h | 4 ++++ net/netfilter/nf_tables_api.c | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-)
include/net/netfilter/nf_tables.h +4 -0
@@ -370,6 +370,8 @@ void nft_unregister_set(struct nft_set_type *type);* * @list: table set list node * @bindings: list of set bindings +* @table: table this set belongs to +* @net: netnamespace this set belongs to * @name: name of the set * @handle: unique handle of the set * @ktype: key type (numeric type defined by userspace, not used in the kernel)
@@ -393,6 +395,8 @@ void nft_unregister_set(struct nft_set_type *type);struct nft_set {struct list_head list;struct list_head bindings; +struct nft_table *table; +possible_net_t net;char *name;u64 handle;u32 ktype;
net/netfilter/nf_tables_api.c +12 -4
@@ -3359,6 +3359,8 @@ static int nf_tables_newset(struct net *net, struct sock *nlsk, }INIT_LIST_HEAD (&set->bindings); + set->table = table; +write_pnet (&set->net, net); set->ops = ops; set->ktype = ktype; set->klen = desc.klen;
@@ -4036,12 +4038,16 @@ void nft_set_elem_destroy(const struct nft_set *set, void *elem, bool destroy_expr) {struct nft_set_ext *ext =nft_set_elem_ext (set, elem); +struct nft_ctx ctx = { + .net =read_pnet (&set->net), + .family = set->table->family, + };nft_data_release (nft_set_ext_key (ext),NFT_DATA_VALUE );if (nft_set_ext_exists (ext,NFT_SET_EXT_DATA ))nft_data_release (nft_set_ext_data (ext), set->dtype);if (destroy_expr &&nft_set_ext_exists (ext,NFT_SET_EXT_EXPR )) -nf_tables_expr_destroy (NULL ,nft_set_ext_expr (ext)); +nf_tables_expr_destroy (&ctx,nft_set_ext_expr (ext));if (nft_set_ext_exists (ext,NFT_SET_EXT_OBJREF )) (*nft_set_ext_obj (ext))->use--;kfree (elem);
@@ -4051,12 +4057,13 @@ EXPORT_SYMBOL_GPL(nft_set_elem_destroy);/* Only called from commit path, nft_set_elem_deactivate() already deals with * the refcounting from the preparation phase. */ -static void nf_tables_set_elem_destroy (const struct nft_set *set,void *elem) +static void nf_tables_set_elem_destroy (const struct nft_ctx *ctx, +const struct nft_set *set,void *elem) {struct nft_set_ext *ext =nft_set_elem_ext (set, elem);if (nft_set_ext_exists (ext,NFT_SET_EXT_EXPR )) -nf_tables_expr_destroy (NULL ,nft_set_ext_expr (ext)); +nf_tables_expr_destroy (ctx,nft_set_ext_expr (ext));kfree (elem); }
@@ -5999,7 +6006,8 @@ static void nft_commit_release(struct nft_trans *trans)nft_set_destroy (nft_trans_set (trans));break ;case NFT_MSG_DELSETELEM : -nf_tables_set_elem_destroy (nft_trans_elem_set (trans), +nf_tables_set_elem_destroy (&trans->ctx, +nft_trans_elem_set (trans),nft_trans_elem (trans).priv);break ;case NFT_MSG_DELOBJ :
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 6/9 netfilter: nf_tables: garbage collection for stateful expressions
In reply to Pablo Neira AyusoUse garbage collector to schedule removal of elements based of feedback from expression that this element comes with. Therefore, the garbage collector is not guided by timeout expirations in this new mode.
The new connlimit expression sets on the NFT_EXPR_GC flag to enable this behaviour, the dynset expression needs to explicitly enable the garbage collector via set->ops->gc_init call.
--- include/net/netfilter/nf_tables.h | 4 ++++ net/netfilter/nft_dynset.c | 9 +++++++++ net/netfilter/nft_set_hash.c | 21 +++++++++++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-)
include/net/netfilter/nf_tables.h +4 -0
@@ -342,6 +342,7 @@ struct nft_set_ops {const struct nft_set_desc *desc, const struct nlattr * const nla[]);void (*destroy)(const struct nft_set *set); +void (*gc_init)(const struct nft_set *set);unsigned int elemsize; };
@@ -712,6 +713,7 @@ struct nft_expr_type { };#define NFT_EXPR_STATEFUL 0x1 +#define NFT_EXPR_GC 0x2/** * struct nft_expr_ops - nf_tables expression operations
@@ -748,6 +750,8 @@ struct nft_expr_ops {int (*validate)(const struct nft_ctx *ctx,const struct nft_expr *expr,const struct nft_data **data); +bool (*gc)(struct net *net, +const struct nft_expr *expr);const struct nft_expr_type *type;void *data; };
net/netfilter/nft_dynset.c +9 -0
@@ -195,6 +195,15 @@ static int nft_dynset_init(const struct nft_ctx *ctx, err = -EOPNOTSUPP ;if (!(priv->expr->ops->type->flags &NFT_EXPR_STATEFUL )) goto err1; + +if (priv->expr->ops->type->flags &NFT_EXPR_GC ) { +if (set->flags &NFT_SET_TIMEOUT ) + goto err1; +if (!set->ops->gc_init) + goto err1; + set->ops->gc_init (set); + } + }else if (set->flags &NFT_SET_EVAL )return -EINVAL ;
net/netfilter/nft_set_hash.c +19 -2
@@ -311,8 +311,16 @@ static void nft_rhash_gc(struct work_struct *work)continue ; } +if (nft_set_ext_exists (&he->ext,NFT_SET_EXT_EXPR )) { +struct nft_expr *expr =nft_set_ext_expr (&he->ext); + +if (expr->ops->gc && + expr->ops->gc (read_pnet (&set->net), expr)) + goto gc; + }if (!nft_set_elem_expired (&he->ext))continue ; +gc:if (nft_set_elem_mark_busy (&he->ext))continue ;
@@ -339,6 +347,14 @@ static unsigned int nft_rhash_privsize(const struct nlattr * const nla[],return sizeof (struct nft_rhash ); } +static void nft_rhash_gc_init (const struct nft_set *set) +{ +struct nft_rhash *priv =nft_set_priv (set); + +queue_delayed_work (system_power_efficient_wq, &priv->gc_work, +nft_set_gc_interval (set)); +} +static int nft_rhash_init (const struct nft_set *set,const struct nft_set_desc *desc,const struct nlattr *const tb[])
@@ -356,8 +372,8 @@ static int nft_rhash_init(const struct nft_set *set,INIT_DEFERRABLE_WORK (&priv->gc_work, nft_rhash_gc);if (set->flags &NFT_SET_TIMEOUT ) -queue_delayed_work (system_power_efficient_wq, &priv->gc_work, -nft_set_gc_interval (set)); +nft_rhash_gc_init (set); +return 0 ; }
@@ -647,6 +663,7 @@ static struct nft_set_type nft_rhash_type __read_mostly = { .elemsize = offsetof(struct nft_rhash_elem, ext), .estimate = nft_rhash_estimate, .init = nft_rhash_init, + .gc_init = nft_rhash_gc_init, .destroy = nft_rhash_destroy, .insert = nft_rhash_insert, .activate = nft_rhash_activate,
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 7/9 netfilter: nf_tables: add destroy_clone expression
In reply to Pablo Neira AyusoBefore this patch, cloned expressions are released via ->destroy. This is a problem for the new connlimit expression since the ->destroy path drop a reference on the conntrack modules and it unregisters hooks. The new ->destroy_clone provides context that this expression is being released from the packet path, so it is mirroring ->clone(), where neither module reference is dropped nor hooks need to be unregistered - because this done from the control plane path from the ->init() path.
--- include/net/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 12 ++++++++++-- net/netfilter/nft_counter.c | 1 + 3 files changed, 13 insertions(+), 2 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
@@ -745,6 +745,8 @@ struct nft_expr_ops { const struct nft_expr *expr);void (*destroy)(const struct nft_ctx *ctx,const struct nft_expr *expr); +void (*destroy_clone)(const struct nft_ctx *ctx, +const struct nft_expr *expr);int (*dump)(struct sk_buff *skb,const struct nft_expr *expr); int (*validate)(const struct nft_ctx *ctx,
net/netfilter/nf_tables_api.c +10 -2
@@ -4046,8 +4046,16 @@ void nft_set_elem_destroy(const struct nft_set *set, void *elem,nft_data_release (nft_set_ext_key (ext),NFT_DATA_VALUE );if (nft_set_ext_exists (ext,NFT_SET_EXT_DATA ))nft_data_release (nft_set_ext_data (ext), set->dtype); -if (destroy_expr &&nft_set_ext_exists (ext,NFT_SET_EXT_EXPR )) -nf_tables_expr_destroy (&ctx,nft_set_ext_expr (ext)); +if (destroy_expr &&nft_set_ext_exists (ext,NFT_SET_EXT_EXPR )) { +struct nft_expr *expr =nft_set_ext_expr (ext); + +if (expr->ops->destroy_clone) { + expr->ops->destroy_clone (&ctx, expr); +module_put (expr->ops->type->owner); + }else { +nf_tables_expr_destroy (&ctx, expr); + } + }if (nft_set_ext_exists (ext,NFT_SET_EXT_OBJREF )) (*nft_set_ext_obj (ext))->use--;kfree (elem);
net/netfilter/nft_counter.c +1 -0
@@ -258,6 +258,7 @@ static const struct nft_expr_ops nft_counter_ops = { .eval = nft_counter_eval, .init = nft_counter_init, .destroy = nft_counter_destroy, + .destroy_clone = nft_counter_destroy, .dump = nft_counter_dump, .clone = nft_counter_clone, };
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 8/9 netfilter: nf_tables: add connlimit support
In reply to Pablo Neira AyusoThis features which allows you to limit the maximum number of connections per arbitrary key. The connlimit expression is stateful, therefore it can be used from meters to dynamically populate a set, this provides a mapping to the iptables' connlimit match. This patch also comes that allows you define static connlimit policies.
This extension depends on the nf_conncount infrastructure.
--- include/uapi/linux/netfilter/nf_tables.h | 21 ++++++++++++++++++++- net/netfilter/Kconfig | 9 +++++++++ net/netfilter/Makefile | 1 + 3 files changed, 30 insertions(+), 1 deletion(-)
include/uapi/linux/netfilter/nf_tables.h +20 -1
@@ -1043,6 +1043,24 @@ enum nft_limit_attributes { };#define NFTA_LIMIT_MAX (__NFTA_LIMIT_MAX - 1) +enum nft_connlimit_flags { +NFT_CONNLIMIT_F_INV = (1 <<0 ), +}; + +/** +* enum nft_connlimit_attributes - nf_tables connlimit expression netlink attributes +* +* @NFTA_CONNLIMIT_COUNT: number of connections (NLA_U32) +* @NFTA_CONNLIMIT_FLAGS: flags (NLA_U32: enum nft_connlimit_flags) +*/ +enum nft_connlimit_attributes { +NFTA_CONNLIMIT_UNSPEC , +NFTA_CONNLIMIT_COUNT , +NFTA_CONNLIMIT_FLAGS , + __NFTA_CONNLIMIT_MAX +}; +#define NFTA_CONNLIMIT_MAX (__NFTA_CONNLIMIT_MAX - 1) +/** * enum nft_counter_attributes - nf_tables counter expression netlink attributes *
@@ -1357,7 +1375,8 @@ enum nft_ct_helper_attributes {#define NFT_OBJECT_QUOTA 2#define NFT_OBJECT_CT_HELPER 3#define NFT_OBJECT_LIMIT 4 -#define __NFT_OBJECT_MAX 5 +#define NFT_OBJECT_CONNLIMIT 5 +#define __NFT_OBJECT_MAX 6#define NFT_OBJECT_MAX (__NFT_OBJECT_MAX - 1)/**
net/netfilter/Kconfig +9 -0
@@ -517,6 +517,15 @@ config NFT_COUNTER This option adds the "counter" expression that you can use to include packet and byte counters in a rule. +config NFT_CONNLIMIT + tristate "Netfilter nf_tables connlimit module" + depends on NF_CONNTRACK + depends on NETFILTER_ADVANCED + select NETFILTER_CONNCOUNT + help + This option adds the "connlimit" expression that you can use to + ratelimit rule matchings per connections. + config NFT_LOG tristate "Netfilter nf_tables log module" help
net/netfilter/Makefile +1 -0
@@ -80,6 +80,7 @@ nf_tables-objs := nf_tables_core.o nf_tables_api.o nft_chain_filter.o \ obj-$(CONFIG_NF_TABLES) += nf_tables.o obj-$(CONFIG_NFT_COMPAT) += nft_compat.o +obj-$(CONFIG_NFT_CONNLIMIT) += nft_connlimit.o obj-$(CONFIG_NFT_NUMGEN) += nft_numgen.o obj-$(CONFIG_NFT_CT) += nft_ct.o obj-$(CONFIG_NFT_FLOW_OFFLOAD) += nft_flow_offload.o
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 9/9 netfilter: nf_tables: handle chain name lookups via rhltable
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
If there is a significant amount of chains list search is too slow, so add an rhlist table for this.
This speeds up ruleset loading: for every new rule we have to check if the name already exists in current generation.
We need to be able to cope with duplicate chain names in case a transaction drops the nfnl mutex (for request_module) and the abort of this old transaction is still pending.
The list is kept -- we need a way to iterate chains even if hash resize is in progress without missing an entry.
--- include/net/netfilter/nf_tables.h | 7 ++- net/netfilter/nf_tables_api.c | 113 +++++++++++++++++++++++++++++++++----- 2 files changed, 104 insertions(+), 16 deletions(-)
include/net/netfilter/nf_tables.h +6 -1
@@ -9,6 +9,7 @@#include <linux/netfilter/x_tables.h> #include <linux/netfilter/nf_tables.h> #include <linux/u64_stats_sync.h> +#include <linux/rhashtable.h> #include <net/netfilter/nf_flow_table.h> #include <net/netlink.h>
@@ -860,6 +861,7 @@ enum nft_chain_flags {* * @rules: list of rules in the chain * @list: used internally +* @rhlhead: used internally * @table: table that this chain belongs to * @handle: chain handle * @use: number of jump references to this chain
@@ -872,6 +874,7 @@ struct nft_chain {struct nft_rule *__rcu *rules_gen_1;struct list_head rules;struct list_head list; +struct rhlist_head rhlhead;struct nft_table *table;u64 handle;u32 use;
@@ -965,7 +968,8 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);* struct nft_table - nf_tables table * * @list: used internally -* @chains: chains in the table +* @chains_ht: chains in the table +* @chains: same, for stable walks * @sets: sets in the table * @objects: stateful objects in the table * @flowtables: flow tables in the table
@@ -979,6 +983,7 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);*/ struct nft_table {struct list_head list; +struct rhltable chains_ht;struct list_head chains;struct list_head sets;struct list_head objects;
net/netfilter/nf_tables_api.c +98 -15
217 lines not on this page · raw message
Signature · 1 line
2.11.0