netfilter: nf_conncount: rework conncount API to
Subsystems (3)
cover netfilter: nf_conncount: rework conncount API to
This series is fixing two different problems. The first issue is related to duplicated entries when used for non-confirmed connections in nft_connlimit and xt_connlimit. Now, nf_conncount_add() checks whether the connection is confirmed or not. If the connection is confirmed, skip the add.
In order to do that, the nf_conncount API is now receiving struct nf_conn as argument instead of tuple and zone. In addition, nf_conncount_count() also needs to receive the net because it calls nf_conncount_gc_list() inside it if ct is NULL.
The second issue this series is fixing is related to nft_connlimit/xt_connlimit not updating the list of connection for confirmed connections breaking softlimiting use-cases like limiting the bandwidth when too many connections are open.
This has been tested on datapath using connlimit in nftables and iptables. I have stressed the system up to 2000 connections.
CC'ing openvswitch maintainers as this change on the API required me to touch their code. I am not very familiar with the internals of openvswitch but I believe this should be fine for them. If you could provide some testing from openvswitch side it would be really helpful.
Fernando Fernandez Mancera (3):
netfilter: nf_conncount: only track connection if it is not confirmed
netfilter: nf_conncount: make nf_conncount_gc_list() to disable BH
netfilter: nft_connlimit: update connection list if add was skipped
include/net/netfilter/nf_conntrack_count.h | 10 +-- net/netfilter/nf_conncount.c | 94 +++++++++++++--------- net/netfilter/nft_connlimit.c | 49 ++++++----- net/netfilter/xt_connlimit.c | 28 ++++--- net/openvswitch/conntrack.c | 14 ++-- 5 files changed, 106 insertions(+), 89 deletions(-)
Signature · 1 line
2.51.0
PATCH 1/3 netfilter: nf_conncount: only track connection if it is not confirmed
Since commit d265929930e2 ("netfilter: nf_conncount: reduce unnecessary GC") if nftables/iptables connlimit is used without a check for new connections there can be duplicated entries tracked.
Pass the nf_conn struct directly to the nf_conncount API and check whether the connection is confirmed or not inside nf_conncount_add(). If the connection is confirmed, skip it.
--- include/net/netfilter/nf_conntrack_count.h | 10 +--- net/netfilter/nf_conncount.c | 70 ++++++++++++---------- net/netfilter/nft_connlimit.c | 35 ++++++----- net/netfilter/xt_connlimit.c | 28 +++++---- net/openvswitch/conntrack.c | 14 ++--- 5 files changed, 81 insertions(+), 76 deletions(-)
include/net/netfilter/nf_conntrack_count.h +3 -7
@@ -18,15 +18,11 @@ struct nf_conncount_list {struct nf_conncount_data *nf_conncount_init (struct net *net,unsigned int keylen);void nf_conncount_destroy (struct net *net,struct nf_conncount_data *data); -unsigned int nf_conncount_count (struct net *net, +unsigned int nf_conncount_count (struct net *net,const struct nf_conn *ct,struct nf_conncount_data *data, -const u32 *key, -const struct nf_conntrack_tuple *tuple, -const struct nf_conntrack_zone *zone); +const u32 *key); -int nf_conncount_add (struct net *net,struct nf_conncount_list *list, -const struct nf_conntrack_tuple *tuple, -const struct nf_conntrack_zone *zone); +int nf_conncount_add (const struct nf_conn *ct,struct nf_conncount_list *list);void nf_conncount_list_init (struct nf_conncount_list *list);
net/netfilter/nf_conncount.c +37 -33
165 lines not on this page · raw message
net/netfilter/nft_connlimit.c +21 -14
@@ -24,28 +24,35 @@ static inline void nft_connlimit_do_eval(struct nft_connlimit *priv,const struct nft_pktinfo *pkt, const struct nft_set_ext *ext) { - const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt; -const struct nf_conntrack_tuple *tuple_ptr; + struct nf_conntrack_tuple_hash *h;struct nf_conntrack_tuple tuple;enum ip_conntrack_info ctinfo;const struct nf_conn *ct;unsigned int count; - - tuple_ptr = &tuple; +int err; ct =nf_ct_get (pkt->skb, &ctinfo); -if (ct !=NULL ) { - tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple; - zone =nf_ct_zone (ct); - }else if (!nf_ct_get_tuplepr (pkt->skb,skb_network_offset (pkt->skb), -nft_pf (pkt),nft_net (pkt), &tuple)) { - regs->verdict.code =NF_DROP ; -return ; +if (!ct) { +if (!nf_ct_get_tuplepr (pkt->skb,skb_network_offset (pkt->skb), +nft_pf (pkt),nft_net (pkt), &tuple)) { + regs->verdict.code =NF_DROP ; +return ; + } + + h =nf_conntrack_find_get (nft_net (pkt), &nf_ct_zone_dflt, &tuple); +if (!h) { + regs->verdict.code =NF_DROP ; +return ; + } + ct =nf_ct_tuplehash_to_ctrack (h); } -if (nf_conncount_add (nft_net (pkt), priv->list, tuple_ptr, zone)) { - regs->verdict.code =NF_DROP ; -return ; + err =nf_conncount_add (ct, priv->list); +if (err) { +if (err != -EINVAL ) { + regs->verdict.code =NF_DROP ; +return ; + } } count =READ_ONCE (priv->list->count);
net/netfilter/xt_connlimit.c +15 -13
@@ -29,23 +29,26 @@static bool connlimit_mt (const struct sk_buff *skb,struct xt_action_param *par) { -struct net *net =xt_net (par);const struct xt_connlimit_info *info = par->matchinfo; +struct nf_conntrack_tuple_hash *h;struct nf_conntrack_tuple tuple; -const struct nf_conntrack_tuple *tuple_ptr = &tuple; -const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;enum ip_conntrack_info ctinfo; +struct net *net =xt_net (par);const struct nf_conn *ct;unsigned int connections;u32 key[5 ]; ct =nf_ct_get (skb, &ctinfo); -if (ct !=NULL ) { - tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple; - zone =nf_ct_zone (ct); - }else if (!nf_ct_get_tuplepr (skb,skb_network_offset (skb), -xt_family (par), net, &tuple)) { - goto hotdrop; +if (!ct) { +if (!nf_ct_get_tuplepr (skb,skb_network_offset (skb), +xt_family (par), net, &tuple)) + goto hotdrop; + + h =nf_conntrack_find_get (net, &nf_ct_zone_dflt, &tuple); +if (!h) + goto hotdrop; + + ct =nf_ct_tuplehash_to_ctrack (h); }if (xt_family (par) ==NFPROTO_IPV6 ) {
@@ -59,18 +62,17 @@ connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)for (i =0 ; i <ARRAY_SIZE (addr.ip6); ++i) addr.ip6[i] &= info->mask.ip6[i];memcpy (key, &addr,sizeof (addr.ip6)); - key[4 ] = zone->id; + key[4 ] =nf_ct_zone (ct)->id; }else {const struct iphdr *iph =ip_hdr (skb); key[0 ] = (info->flags &XT_CONNLIMIT_DADDR ) ? (__force __u32 )iph->daddr : (__force __u32 )iph->saddr; key[0 ] &= (__force __u32 )info->mask.ip; - key[1 ] = zone->id; + key[1 ] =nf_ct_zone (ct)->id; } - connections =nf_conncount_count (net, info->data, key, tuple_ptr, - zone); + connections =nf_conncount_count (net, ct, info->data, key);if (connections ==0 )/* kmalloc failed, drop it entirely */ goto hotdrop;
net/openvswitch/conntrack.c +5 -9
@@ -929,7 +929,7 @@ static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone)static int ovs_ct_check_limit (struct net *net,const struct ovs_conntrack_info *info, -const struct nf_conntrack_tuple *tuple) +const struct nf_conn *ct) {struct ovs_net *ovs_net =net_generic (net, ovs_net_id);const struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
@@ -942,8 +942,8 @@ static int ovs_ct_check_limit(struct net *net,if (per_zone_limit ==OVS_CT_LIMIT_UNLIMITED )return 0 ; - connections =nf_conncount_count (net, ct_limit_info->data, - &conncount_key, tuple, &info->zone); + connections =nf_conncount_count (net, ct, ct_limit_info->data, + &conncount_key);if (connections > per_zone_limit)return -ENOMEM ;
@@ -972,8 +972,7 @@ static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,#if IS_ENABLED (CONFIG_NETFILTER_CONNCOUNT )if (static_branch_unlikely (&ovs_ct_limit_enabled)) {if (!nf_ct_is_confirmed (ct)) { - err =ovs_ct_check_limit (net, info, - &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple); + err =ovs_ct_check_limit (net, info, ct);if (err) { net_warn_ratelimited("openvswitch: zone: %u " "exceeds conntrack limit\n",
@@ -1762,16 +1761,13 @@ static int __ovs_ct_limit_get_zone_limit(struct net *net, u16 zone_id, u32 limit, struct sk_buff *reply) { - struct nf_conntrack_zone ct_zone; struct ovs_zone_limit zone_limit;u32 conncount_key = zone_id; zone_limit.zone_id = zone_id; zone_limit.limit = limit; -nf_ct_zone_init (&ct_zone, zone_id,NF_CT_DEFAULT_ZONE_DIR ,0 ); - zone_limit.count =nf_conncount_count (net, data, &conncount_key,NULL , - &ct_zone); + zone_limit.count =nf_conncount_count (net,NULL , data, &conncount_key);return nla_put_nohdr (reply,sizeof (zone_limit), &zone_limit); }
Signature · 1 line
2.51.0
Florian Westphal
maintainerRe: [PATCH 1/3 nf-next] netfilter: nf_conncount: only track connection if it is not confirmed
Quoted 7 lines · Fernando Fernandez Mancera
Since commit
d265929930e2("netfilter:nf_conncount: reduce unnecessary GC") if nftables/iptables connlimit is used without a check for new connections there can be duplicated entries tracked.Pass the
nf_connstruct directly to thenf_conncountAPI and check whether the connection is confirmed or not insidenf_conncount_add(). If the connection is confirmed, skip it.
I think there is a bit too much noise here, can this be split in several chunks?
include/net/netfilter/nf_conntrack_count.h struct nf_conncount_list { · line 23
Quoted 7 lines
include/net/netfilter/nf_conntrack_count.h +2
-4-unsigned int nf_conncount_count (struct net *net, +unsigned int nf_conncount_count (struct net *net,const struct nf_conn *ct,struct nf_conncount_data *data, -const u32 *key, -const struct nf_conntrack_tuple *tuple, -const struct nf_conntrack_zone *zone); +const u32 *key);
nf_conn *ct has pitfalls that I did not realize earlier. Current code is also buggy in that regard.
Maybe we need additional function to ease this for callers.
See below.
net/netfilter/nf_conncount.c find_or_evict(struct net *net, struct nf_conncount_list *list, · line 137
Quoted 13 lines
net/netfilter/nf_conncount.c +7
-0+static int __nf_conncount_add (const struct nf_conn *ct, +struct nf_conncount_list *list) {const struct nf_conntrack_tuple_hash *found;struct nf_conncount_tuple *conn, *conn_n; +const struct nf_conntrack_tuple *tuple; +const struct nf_conntrack_zone *zone;struct nf_conn *found_ct;unsigned int collect =0 ; +if (!ct ||nf_ct_is_confirmed (ct)) +return -EINVAL ; +
When is the caller expected to pass a NULL ct?
Maybe this just misses a comment.
Quoted 29 lines
index
fc35a11cdca2..e815c0235b62100644net/netfilter/nft_connlimit.c +5
-11@@ -24,28 +24,35 @@ static inline void nft_connlimit_do_eval(struct nft_connlimit *priv,const struct nft_pktinfo *pkt, const struct nft_set_ext *ext) { - const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt; -const struct nf_conntrack_tuple *tuple_ptr; + struct nf_conntrack_tuple_hash *h;struct nf_conntrack_tuple tuple;enum ip_conntrack_info ctinfo;const struct nf_conn *ct;unsigned int count; - - tuple_ptr = &tuple; +int err; ct =nf_ct_get (pkt->skb, &ctinfo); -if (ct !=NULL ) { - tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple; - zone =nf_ct_zone (ct); - }else if (!nf_ct_get_tuplepr (pkt->skb,skb_network_offset (pkt->skb), -nft_pf (pkt),nft_net (pkt), &tuple)) { - regs->verdict.code =NF_DROP ; -return ; +if (!ct) { +if (!nf_ct_get_tuplepr (pkt->skb,skb_network_offset (pkt->skb), +nft_pf (pkt),nft_net (pkt), &tuple)) {
net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 47
Quoted 10 lines
net/netfilter/nft_connlimit.c +10
-0+ regs->verdict.code =NF_DROP ; +return ; + } + + h =nf_conntrack_find_get (nft_net (pkt), &nf_ct_zone_dflt, &tuple); +if (!h) { + regs->verdict.code =NF_DROP ; +return ; + } + ct =nf_ct_tuplehash_to_ctrack (h);
This ct had its refcount incremented.
I also see that this shared copypaste with xtables. Would it be possible to pass 'const struct sk_buff *' and let the nf_conncount core handle this internally?
nf_conncount_add(net, pf, skb, priv->list);
which does:
ct = nf_ct_get(skb, &ctinfo); if (ct && !nf_ct_is_template(ct)) return __nf_conncount_add(ct, list); if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), pf, net, &tuple)) return -ERR; if (ct) /* its a template, so do lookup in right zone */ zone = nf_ct_zone(ct); else zone = &nf_ct_zone_dflt; h = nf_conntrack_find_get(nft_net(pkt), zone, &tuple); if (!h) return -ERR; ct = nf_ct_tuplehash_to_ctrack(h); err = __nf_conncount_add(ct, list); nf_ct_put(ct): return err;
I.e., the existing nf_conncount_add() becomes a helper that takes a ct, as you have already proposed, but its renamed and turned into an internal helper so frontends don't need to duplicate tuple lookup.
Alternatively, no need to rename it and instead add a new API call that takes the ct argument, e.g. 'nf_conncount_add_ct' or whatever, and then make nf_conncount_add() internal in a followup patch.
net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 48
Quoted 1 line
net/netfilter/nft_connlimit.c +1
-0+ h =nf_conntrack_find_get (net, &nf_ct_zone_dflt, &tuple);
We should not restrict the lookup to the default zone, but we should follow what the template (if any) says.
I under-estimated the work to get this right, I think this is too much code to copypaste between nft+xt frontends. Sorry for making that suggestion originally.
Re: [PATCH 1/3 nf-next] netfilter: nf_conncount: only track connection if it is not confirmed
Quoted 5 lines · Florian Westphal
Quoted 4 lines · Fernando Fernandez Mancera
Since commit
d265929930e2("netfilter:nf_conncount: reduce unnecessary GC") if nftables/iptables connlimit is used without a check for new connections there can be duplicated entries tracked.
Quoted 7 lines
Quoted 3 lines
Pass the
nf_connstruct directly to thenf_conncountAPI and check whether the connection is confirmed or not insidenf_conncount_add(). If the connection is confirmed, skip it.I think there is a bit too much noise here, can this be split in several chunks?
Not sure, I could try but the noise comes from removing zone and tuple which requires many changes around. Otherwise this would compile with warnings/errors. I am not sure I can split this more.
Quoted 23 lines
include/net/netfilter/nf_conntrack_count.h struct nf_conncount_list { · line 23
Quoted 7 lines
include/net/netfilter/nf_conntrack_count.h +2
-4-unsigned int nf_conncount_count (struct net *net, +unsigned int nf_conncount_count (struct net *net,const struct nf_conn *ct,struct nf_conncount_data *data, -const u32 *key, -const struct nf_conntrack_tuple *tuple, -const struct nf_conntrack_zone *zone); +const u32 *key);
nf_conn*ct has pitfalls that I did not realize earlier. Current code is also buggy in that regard.
Maybe we need additional function to ease this for callers.See below.
Quoted 10 lines
net/netfilter/nf_conncount.c +4
-0+static int __nf_conncount_add (const struct nf_conn *ct, +struct nf_conncount_list *list) {const struct nf_conntrack_tuple_hash *found;struct nf_conncount_tuple *conn, *conn_n; +const struct nf_conntrack_tuple *tuple; +const struct nf_conntrack_zone *zone;struct nf_conn *found_ct;unsigned int collect =0 ;
Quoted 7 lines
net/netfilter/nf_conncount.c find_or_evict(struct net *net, struct nf_conncount_list *list, · line 137
Quoted 3 lines
net/netfilter/nf_conncount.c +3
-0+if (!ct ||nf_ct_is_confirmed (ct)) +return -EINVAL ; +When is the caller expected to pass a NULL ct?
Maybe this just misses a comment.
No, I don't think the caller is expected to pass a NULL ct. That should be removed.
Quoted 33 lines
Quoted 33 lines
index
fc35a11cdca2..e815c0235b62100644net/netfilter/nft_connlimit.c +9
-11@@ -24,28 +24,35 @@ static inline void nft_connlimit_do_eval(struct nft_connlimit *priv,const struct nft_pktinfo *pkt, const struct nft_set_ext *ext) { - const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt; -const struct nf_conntrack_tuple *tuple_ptr; + struct nf_conntrack_tuple_hash *h;struct nf_conntrack_tuple tuple;enum ip_conntrack_info ctinfo;const struct nf_conn *ct;unsigned int count; - - tuple_ptr = &tuple; +int err; ct =nf_ct_get (pkt->skb, &ctinfo); -if (ct !=NULL ) { - tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple; - zone =nf_ct_zone (ct); - }else if (!nf_ct_get_tuplepr (pkt->skb,skb_network_offset (pkt->skb), -nft_pf (pkt),nft_net (pkt), &tuple)) { - regs->verdict.code =NF_DROP ; -return ; +if (!ct) { +if (!nf_ct_get_tuplepr (pkt->skb,skb_network_offset (pkt->skb), +nft_pf (pkt),nft_net (pkt), &tuple)) { + regs->verdict.code =NF_DROP ; +return ; + } +
Quoted 9 lines
net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 47
Quoted 6 lines
net/netfilter/nft_connlimit.c +6
-0+ h =nf_conntrack_find_get (nft_net (pkt), &nf_ct_zone_dflt, &tuple); +if (!h) { + regs->verdict.code =NF_DROP ; +return ; + } + ct =nf_ct_tuplehash_to_ctrack (h);This ct had its refcount incremented.
Thank you! Will fix it.
Quoted 32 lines
I also see that this shared copypaste with xtables. Would it be possible to pass 'const struct
sk_buff*' and let thenf_conncountcore handle this internally?
nf_conncount_add(net, pf, skb, priv->list);which does:
ct = nf_ct_get(skb, &ctinfo); if (ct && !nf_ct_is_template(ct)) return __nf_conncount_add(ct, list); if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), pf, net, &tuple)) return -ERR; if (ct) /* its a template, so do lookup in right zone */ zone = nf_ct_zone(ct); else zone = &nf_ct_zone_dflt; h = nf_conntrack_find_get(nft_net(pkt), zone, &tuple); if (!h) return -ERR; ct = nf_ct_tuplehash_to_ctrack(h); err = __nf_conncount_add(ct, list); nf_ct_put(ct): return err;
Quoted 8 lines
I.e., the existing
nf_conncount_add()becomes a helper that takes a ct, as you have already proposed, but its renamed and turned into an internal helper so frontends don't need to duplicate tuple lookup.Alternatively, no need to rename it and instead add a new API call that takes the ct argument, e.g. '
nf_conncount_add_ct' or whatever, and then makenf_conncount_add()internal in a followup patch.
Unfortunately, I do not think this is possible. xt_connlimit is using the rbtree with nf_conncount_count() while nft_connlimit isn't. I believe we do not want to change that. In addition, for the rbtree we need to calculate the key.. I would leave this code as duplicated given that is shared only between xt_connlimit and nft_connlimit. Openvswitch doesn't care about this as they always call nf_conncount_count() while holding a reference to a ct..
Quoted 8 lines
net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 48
Quoted 1 line
net/netfilter/nft_connlimit.c +1
-0+ h =nf_conntrack_find_get (net, &nf_ct_zone_dflt, &tuple);We should not restrict the lookup to the default zone, but we should follow what the template (if any) says.
I under-estimated the work to get this right, I think this is too much code to copypaste between nft+xt frontends. Sorry for making that suggestion originally.
No worries at all, I think there is some benefit from this change even with the copy-paste. Maybe we can create a helper function to just get the ct from the sk_buff.. what about "nf_conntrack_get_or_find()"? I accept suggestions for a better name :)
Florian Westphal
maintainerRe: [PATCH 1/3 nf-next] netfilter: nf_conncount: only track connection if it is not confirmed
Quoted 12 lines · Fernando Fernandez Mancera
Quoted 9 lines · Florian Westphal
Quoted 7 lines · Fernando Fernandez Mancera
Since commit
d265929930e2("netfilter:nf_conncount: reduce unnecessary GC") if nftables/iptables connlimit is used without a check for new connections there can be duplicated entries tracked.Pass the
nf_connstruct directly to thenf_conncountAPI and check whether the connection is confirmed or not insidenf_conncount_add(). If the connection is confirmed, skip it.
Quoted 7 lines
Quoted 3 lines
I think there is a bit too much noise here, can this be split in several chunks?
Not sure, I could try but the noise comes from removing zone and tuple which requires many changes around. Otherwise this would compile with warnings/errors. I am not sure I can split this more.
Ok.
Quoted 36 lines
Quoted 36 lines
I also see that this shared copypaste with xtables. Would it be possible to pass 'const struct
sk_buff*' and let thenf_conncountcore handle this internally?
nf_conncount_add(net, pf, skb, priv->list);which does:
ct = nf_ct_get(skb, &ctinfo); if (ct && !nf_ct_is_template(ct)) return __nf_conncount_add(ct, list); if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), pf, net, &tuple)) return -ERR; if (ct) /* its a template, so do lookup in right zone */ zone = nf_ct_zone(ct); else zone = &nf_ct_zone_dflt; h = nf_conntrack_find_get(nft_net(pkt), zone, &tuple); if (!h) return -ERR; ct = nf_ct_tuplehash_to_ctrack(h); err = __nf_conncount_add(ct, list); nf_ct_put(ct): return err;I.e., the existing
nf_conncount_add()becomes a helper that takes a ct, as you have already proposed, but its renamed and turned into an internal helper so frontends don't need to duplicate tuple lookup.
Quoted 8 lines
Quoted 4 lines
Alternatively, no need to rename it and instead add a new API call that takes the ct argument, e.g. '
nf_conncount_add_ct' or whatever, and then makenf_conncount_add()internal in a followup patch.Unfortunately, I do not think this is possible.
xt_connlimitis using the rbtree withnf_conncount_count()whilenft_connlimitisn't. I believe we do not want to change that.
OK. I had hoped one could start with refactoring nf_conncount_count() and then after that nf_conncount_count().
Quoted 2 lines
In addition, for the rbtree we
need to calculate the key..
Right but AFAICS due to the missing 'template check' we pass all-zero tuple_ptr if there is a template attached to the skb.
Not catastrophic but its not correct either.
I had hoped it was possible so s/tuple, zone/sk_buff/ in the arguments and then handle that internally (first in count_tree and then later in a converted nf_conncount_count(), i.e. push the sk_buff -> ct handling down in followup patches.
Quoted 4 lines
I would leave this code as duplicated given
that is shared only betweenxt_connlimitandnft_connlimit. Openvswitch doesn't care about this as they always callnf_conncount_count()while holding a reference to a ct..
[..]
Quoted 4 lines
No worries at all, I think there is some benefit from this change even with the copy-paste. Maybe we can create a helper function to just get the ct from the
sk_buff.. what about "nf_conntrack_get_or_find()"? I accept suggestions for a better name :)
OK, but the problem is that you need to know when you need to put the reference and when you don't have to.
Task is:
given skb, return a
'ct' that is not a template
... but if its a template we need the zone to make a lookup ourselves ... and we have to bump (and put) refcount when we do that lookup ourselves.
Maybe you could try adding a new api, e.g. call it 'nf_conncount_count_skb()' that then calls nf_conncount_count() internally just to see how bad it looks?
If its not too bad, then all callers could be converted one after another.
And then same with nf_conncount_add().
Just a suggestion, alternatively give your nf_conntrack_get_or_find() a try, it would need a 'bool *refcounted' or similar arg and a conditional 'if (refcounted) nf_ct_put(ct)' to be done by the callers.
PATCH 2/3 netfilter: nf_conncount: make nf_conncount_gc_list() to disable BH
In reply to Fernando Fernandez ManceraFor convenience when performing GC over the connection list, make nf_conncount_gc_list() to disable BH. This unifies the behavior with nf_conncount_add() and nf_conncount_count().
--- net/netfilter/nf_conncount.c | 24 +++++++++++++++++------- net/netfilter/nft_connlimit.c | 7 +------ 2 files changed, 18 insertions(+), 13 deletions(-)
net/netfilter/nf_conncount.c +17 -7
@@ -227,8 +227,8 @@ void nf_conncount_list_init(struct nf_conncount_list *list)EXPORT_SYMBOL_GPL (nf_conncount_list_init);/* Return true if the list is empty. Must be called with BH disabled. */ -bool nf_conncount_gc_list (struct net *net, -struct nf_conncount_list *list) +static bool __nf_conncount_gc_list (struct net *net, +struct nf_conncount_list *list) {const struct nf_conntrack_tuple_hash *found;struct nf_conncount_tuple *conn, *conn_n;
@@ -240,10 +240,6 @@ bool nf_conncount_gc_list(struct net *net,if ((u32 )jiffies ==READ_ONCE (list->last_gc))return false; -/* don't bother if other cpu is already doing GC */ -if (!spin_trylock (&list->list_lock)) -return false; -list_for_each_entry_safe (conn, conn_n, &list->head, node) { found =find_or_evict (net, list, conn);if (IS_ERR (found)) {
@@ -272,7 +268,21 @@ bool nf_conncount_gc_list(struct net *net,if (!list->count) ret = true; list->last_gc = (u32 )jiffies; -spin_unlock (&list->list_lock); + +return ret; +}; + +bool nf_conncount_gc_list (struct net *net, +struct nf_conncount_list *list) +{ +bool ret; + +/* don't bother if other cpu is already doing GC */ +if (!spin_trylock_bh (&list->list_lock)) +return false; + + ret =__nf_conncount_gc_list (net, list); +spin_unlock_bh (&list->list_lock);return ret; }
net/netfilter/nft_connlimit.c +1 -6
@@ -245,13 +245,8 @@ static void nft_connlimit_destroy_clone(const struct nft_ctx *ctx,static bool nft_connlimit_gc (struct net *net,const struct nft_expr *expr) {struct nft_connlimit *priv =nft_expr_priv (expr); -bool ret; -local_bh_disable (); - ret =nf_conncount_gc_list (net, priv->list); -local_bh_enable (); - -return ret; +return nf_conncount_gc_list (net, priv->list); }static struct nft_expr_type nft_connlimit_type;
Signature · 1 line
2.51.0
PATCH 3/3 netfilter: nft_connlimit: update connection list if add was skipped
In reply to Fernando Fernandez ManceraConnlimit expression can be used for all kind of packets and not only for packets with connection state new. See this ruleset as example:
table ip filter {
chain input {
type filter hook input priority filter; policy accept;
tcp dport 22 ct count over 4 counter
}
}Currently, if the connection count goes over the limit the counter will count the packets. When a connection is closed, the connection count won't decrement as it should because it is only updated for new connections due to an optimization on __nf_conncount_add() that prevents updating the list if the connection is duplicated.
To solve this problem, check whether the connection was skipped and if so, update the list. This fix isn't necessary on xt_connlimit as the new nf_conncount API updates the list when the add is skipped inside nf_conncount_count().
--- net/netfilter/nft_connlimit.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
net/netfilter/nft_connlimit.c +8 -1
@@ -49,7 +49,14 @@ static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, err =nf_conncount_add (ct, priv->list);if (err) { -if (err != -EINVAL ) { +if (err == -EINVAL ) { +/* Call gc to update the list count if any connection has +* been closed already. This is useful to softlimit +* connections like limiting bandwidth based on a number +* of open connections. +*/ +nf_conncount_gc_list (nf_ct_net (ct), priv->list); + }else { regs->verdict.code =NF_DROP ;return ; }
Signature · 1 line
2.51.0