Skip to content

Commit f1f4376

Browse files
ebiedermdavem330
authored andcommitted
sctp: Make the port hash table use struct net in it's key.
- Add struct net into the port hash table hash calculation - Add struct net inot the struct sctp_bind_bucket so there is a memory of which network namespace a port is allocated in. No need for a ref count because sctp_bind_bucket only exists when there are sockets in the hash table and sockets can not change their network namspace, and sockets already ref count their network namespace. - Add struct net into the key comparison when we are testing to see if we have found the port hash table entry we are looking for. With these changes lookups in the port hash table becomes safe to use in multiple network namespaces. Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> Acked-by: Vlad Yasevich <vyasevich@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent eea68e2 commit f1f4376

3 files changed

Lines changed: 16 additions & 11 deletions

File tree

‎include/net/sctp/sctp.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,9 @@ static inline int sctp_sanity_check(void)
632632

633633
/* Warning: The following hash functions assume a power of two 'size'. */
634634
/* This is the hash function for the SCTP port hash table. */
635-
static inline int sctp_phashfn(__u16 lport)
635+
static inline int sctp_phashfn(struct net *net, __u16 lport)
636636
{
637-
return lport & (sctp_port_hashsize - 1);
637+
return (net_hash_mix(net) + lport) & (sctp_port_hashsize - 1);
638638
}
639639

640640
/* This is the hash function for the endpoint hash table. */

‎include/net/sctp/structs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ struct sctp_bind_bucket {
102102
unsigned short fastreuse;
103103
struct hlist_node node;
104104
struct hlist_head owner;
105+
struct net *net;
105106
};
106107

107108
struct sctp_bind_hashbucket {

‎net/sctp/socket.c‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5769,7 +5769,7 @@ static void sctp_unhash(struct sock *sk)
57695769
* a fastreuse flag (FIXME: NPI ipg).
57705770
*/
57715771
static struct sctp_bind_bucket *sctp_bucket_create(
5772-
struct sctp_bind_hashbucket *head, unsigned short snum);
5772+
struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
57735773

57745774
static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
57755775
{
@@ -5799,11 +5799,12 @@ static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
57995799
rover = low;
58005800
if (inet_is_reserved_local_port(rover))
58015801
continue;
5802-
index = sctp_phashfn(rover);
5802+
index = sctp_phashfn(sock_net(sk), rover);
58035803
head = &sctp_port_hashtable[index];
58045804
sctp_spin_lock(&head->lock);
58055805
sctp_for_each_hentry(pp, node, &head->chain)
5806-
if (pp->port == rover)
5806+
if ((pp->port == rover) &&
5807+
net_eq(sock_net(sk), pp->net))
58075808
goto next;
58085809
break;
58095810
next:
@@ -5827,10 +5828,10 @@ static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
58275828
* to the port number (snum) - we detect that with the
58285829
* port iterator, pp being NULL.
58295830
*/
5830-
head = &sctp_port_hashtable[sctp_phashfn(snum)];
5831+
head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
58315832
sctp_spin_lock(&head->lock);
58325833
sctp_for_each_hentry(pp, node, &head->chain) {
5833-
if (pp->port == snum)
5834+
if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
58345835
goto pp_found;
58355836
}
58365837
}
@@ -5881,7 +5882,7 @@ static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
58815882
pp_not_found:
58825883
/* If there was a hash table miss, create a new port. */
58835884
ret = 1;
5884-
if (!pp && !(pp = sctp_bucket_create(head, snum)))
5885+
if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
58855886
goto fail_unlock;
58865887

58875888
/* In either case (hit or miss), make sure fastreuse is 1 only
@@ -6113,7 +6114,7 @@ unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
61136114
********************************************************************/
61146115

61156116
static struct sctp_bind_bucket *sctp_bucket_create(
6116-
struct sctp_bind_hashbucket *head, unsigned short snum)
6117+
struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
61176118
{
61186119
struct sctp_bind_bucket *pp;
61196120

@@ -6123,6 +6124,7 @@ static struct sctp_bind_bucket *sctp_bucket_create(
61236124
pp->port = snum;
61246125
pp->fastreuse = 0;
61256126
INIT_HLIST_HEAD(&pp->owner);
6127+
pp->net = net;
61266128
hlist_add_head(&pp->node, &head->chain);
61276129
}
61286130
return pp;
@@ -6142,7 +6144,8 @@ static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
61426144
static inline void __sctp_put_port(struct sock *sk)
61436145
{
61446146
struct sctp_bind_hashbucket *head =
6145-
&sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->inet_num)];
6147+
&sctp_port_hashtable[sctp_phashfn(sock_net(sk),
6148+
inet_sk(sk)->inet_num)];
61466149
struct sctp_bind_bucket *pp;
61476150

61486151
sctp_spin_lock(&head->lock);
@@ -6809,7 +6812,8 @@ static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
68096812
newsp->hmac = NULL;
68106813

68116814
/* Hook this new socket in to the bind_hash list. */
6812-
head = &sctp_port_hashtable[sctp_phashfn(inet_sk(oldsk)->inet_num)];
6815+
head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
6816+
inet_sk(oldsk)->inet_num)];
68136817
sctp_local_bh_disable();
68146818
sctp_spin_lock(&head->lock);
68156819
pp = sctp_sk(oldsk)->bind_hash;

0 commit comments

Comments
 (0)