Skip to content

Commit 1b8c8a9

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: conntrack: make netns address part of hash
Once we place all conntracks into a global hash table we want them to be spread across entire hash table, even if namespaces have overlapping ip addresses. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent e0c7d47 commit 1b8c8a9

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

net/netfilter/nf_conntrack_core.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include <net/netfilter/nf_nat.h>
5555
#include <net/netfilter/nf_nat_core.h>
5656
#include <net/netfilter/nf_nat_helper.h>
57+
#include <net/netns/hash.h>
5758

5859
#define NF_CONNTRACK_VERSION "0.5.0"
5960

@@ -144,42 +145,41 @@ EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);
144145

145146
static unsigned int nf_conntrack_hash_rnd __read_mostly;
146147

147-
static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple)
148+
static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple,
149+
const struct net *net)
148150
{
149151
unsigned int n;
152+
u32 seed;
150153

151154
get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd));
152155

153156
/* The direction must be ignored, so we hash everything up to the
154157
* destination ports (which is a multiple of 4) and treat the last
155158
* three bytes manually.
156159
*/
160+
seed = nf_conntrack_hash_rnd ^ net_hash_mix(net);
157161
n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
158-
return jhash2((u32 *)tuple, n, nf_conntrack_hash_rnd ^
162+
return jhash2((u32 *)tuple, n, seed ^
159163
(((__force __u16)tuple->dst.u.all << 16) |
160164
tuple->dst.protonum));
161165
}
162166

163-
static u32 __hash_bucket(u32 hash, unsigned int size)
164-
{
165-
return reciprocal_scale(hash, size);
166-
}
167-
168167
static u32 hash_bucket(u32 hash, const struct net *net)
169168
{
170-
return __hash_bucket(hash, net->ct.htable_size);
169+
return reciprocal_scale(hash, net->ct.htable_size);
171170
}
172171

173-
static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
174-
unsigned int size)
172+
static u32 __hash_conntrack(const struct net *net,
173+
const struct nf_conntrack_tuple *tuple,
174+
unsigned int size)
175175
{
176-
return __hash_bucket(hash_conntrack_raw(tuple), size);
176+
return reciprocal_scale(hash_conntrack_raw(tuple, net), size);
177177
}
178178

179-
static inline u_int32_t hash_conntrack(const struct net *net,
180-
const struct nf_conntrack_tuple *tuple)
179+
static u32 hash_conntrack(const struct net *net,
180+
const struct nf_conntrack_tuple *tuple)
181181
{
182-
return __hash_conntrack(tuple, net->ct.htable_size);
182+
return __hash_conntrack(net, tuple, net->ct.htable_size);
183183
}
184184

185185
bool
@@ -535,7 +535,7 @@ nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
535535
const struct nf_conntrack_tuple *tuple)
536536
{
537537
return __nf_conntrack_find_get(net, zone, tuple,
538-
hash_conntrack_raw(tuple));
538+
hash_conntrack_raw(tuple, net));
539539
}
540540
EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
541541

@@ -1041,7 +1041,7 @@ resolve_normal_ct(struct net *net, struct nf_conn *tmpl,
10411041

10421042
/* look for tuple match */
10431043
zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
1044-
hash = hash_conntrack_raw(&tuple);
1044+
hash = hash_conntrack_raw(&tuple, net);
10451045
h = __nf_conntrack_find_get(net, zone, &tuple, hash);
10461046
if (!h) {
10471047
h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
@@ -1605,7 +1605,8 @@ int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
16051605
struct nf_conntrack_tuple_hash, hnnode);
16061606
ct = nf_ct_tuplehash_to_ctrack(h);
16071607
hlist_nulls_del_rcu(&h->hnnode);
1608-
bucket = __hash_conntrack(&h->tuple, hashsize);
1608+
bucket = __hash_conntrack(nf_ct_net(ct),
1609+
&h->tuple, hashsize);
16091610
hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
16101611
}
16111612
}

0 commit comments

Comments
 (0)