Skip to content

Commit

Permalink
[SCTP]: port randomization
Browse files Browse the repository at this point in the history
Add port randomization rather than a simple fixed rover
for use with SCTP.  This makes it act similar to TCP, UDP, DCCP
when allocating ports.

No longer need port_alloc_lock as well (suggestion by Brian Haley).

Signed-off-by: Stephen Hemminger <shemminger@linux-foundation.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Stephen Hemminger authored and David S. Miller committed Oct 11, 2007
1 parent 3c0cfc1 commit 0639300
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 21 deletions.
2 changes: 0 additions & 2 deletions include/net/sctp/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@ extern struct sctp_globals {

/* This is the sctp port control hash. */
int port_hashsize;
int port_rover;
spinlock_t port_alloc_lock; /* Protects port_rover. */
struct sctp_bind_hashbucket *port_hashtable;

/* This is the global local address list.
Expand Down
3 changes: 0 additions & 3 deletions net/sctp/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -1172,9 +1172,6 @@ SCTP_STATIC __init int sctp_init(void)
sctp_port_hashtable[i].chain = NULL;
}

spin_lock_init(&sctp_port_alloc_lock);
sctp_port_rover = sysctl_local_port_range[0] - 1;

printk(KERN_INFO "SCTP: Hash tables configured "
"(established %d bind %d)\n",
sctp_assoc_hashsize, sctp_port_hashsize);
Expand Down
21 changes: 5 additions & 16 deletions net/sctp/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -5314,22 +5314,13 @@ static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
sctp_local_bh_disable();

if (snum == 0) {
/* Search for an available port.
*
* 'sctp_port_rover' was the last port assigned, so
* we start to search from 'sctp_port_rover +
* 1'. What we do is first check if port 'rover' is
* already in the hash table; if not, we use that; if
* it is, we try next.
*/
int low = sysctl_local_port_range[0];
int high = sysctl_local_port_range[1];
int remaining = (high - low) + 1;
int rover;
/* Search for an available port. */
unsigned int low = sysctl_local_port_range[0];
unsigned int high = sysctl_local_port_range[1];
unsigned int remaining = (high - low) + 1;
unsigned int rover = net_random() % remaining + low;
int index;

sctp_spin_lock(&sctp_port_alloc_lock);
rover = sctp_port_rover;
do {
rover++;
if ((rover < low) || (rover > high))
Expand All @@ -5344,8 +5335,6 @@ static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
next:
sctp_spin_unlock(&head->lock);
} while (--remaining > 0);
sctp_port_rover = rover;
sctp_spin_unlock(&sctp_port_alloc_lock);

/* Exhausted local port range during search? */
ret = 1;
Expand Down

0 comments on commit 0639300

Please sign in to comment.