From: Stephen Hemminger <hidden> Date: 2004-10-29 17:28:28
Provide port randomization for incoming connections using variation of
existing sequence number hash. Replace tcp_portalloc_lock and tcp_port_rover
with atomic operation to allow better parallelism.
This is based on
http://www.ietf.org/internet-drafts/draft-larsen-tsvwg-port-randomisation-00.txt
(with confirmation of of no IPR issues).
Signed-off-by: Stephen Hemminger <redacted>
diff -urNp -X dontdiff linux-2.6/drivers/char/random.c port-2.6/drivers/char/random.c
@@ -151,14 +150,19 @@ extern struct tcp_hashinfo {#define tcp_lhash_lock (tcp_hashinfo.__tcp_lhash_lock)#define tcp_lhash_users (tcp_hashinfo.__tcp_lhash_users)#define tcp_lhash_wait (tcp_hashinfo.__tcp_lhash_wait)-#define tcp_portalloc_lock (tcp_hashinfo.__tcp_portalloc_lock)externkmem_cache_t*tcp_bucket_cachep;externstructtcp_bind_bucket*tcp_bucket_create(structtcp_bind_hashbucket*head,unsignedshortsnum);externvoidtcp_bucket_destroy(structtcp_bind_bucket*tb);externvoidtcp_bucket_unlock(structsock*sk);-externinttcp_port_rover;+externatomic_ttcp_rover_next;++/* offset in ephemeral port space to start next scan */+staticinlineu32tcp_port_rover(void)+{+return(u32)atomic_inc_return(&tcp_rover_next);+}/* These are AF independent. */static__inline__inttcp_bhashfn(__u16lport)
@@ -219,14 +219,10 @@ static int tcp_v4_get_port(struct sock *intlow=sysctl_local_port_range[0];inthigh=sysctl_local_port_range[1];intremaining=(high-low)+1;-introver;+__u16rover;-spin_lock(&tcp_portalloc_lock);-rover=tcp_port_rover;+rover=low+tcp_port_rover()%(high-low);do{-rover++;-if(rover<low||rover>high)-rover=low;head=&tcp_bhash[tcp_bhashfn(rover)];spin_lock(&head->lock);tb_for_each(tb,node,&head->chain)
@@ -235,9 +231,9 @@ static int tcp_v4_get_port(struct sock *break;next:spin_unlock(&head->lock);+if(++rover>=high)+rover=low;}while(--remaining>0);-tcp_port_rover=rover;-spin_unlock(&tcp_portalloc_lock);/* Exhausted local port range during search? */ret=1;
@@ -645,35 +648,17 @@ static int tcp_v4_hash_connect(struct sointret;if(!snum){-introver;intlow=sysctl_local_port_range[0];inthigh=sysctl_local_port_range[1];intremaining=(high-low)+1;structhlist_node*node;structtcp_tw_bucket*tw=NULL;+__u16rover;+rover=low+(tcp_port_rover()+connect_port_offset(sk))+%(high-low);local_bh_disable();--/* TODO. Actually it is not so bad idea to remove-*tcp_portalloc_lockbeforenextsubmissiontoLinus.-*Assoonaswetouchthisplaceatallitistimetothink.-*-*Nowitprotectssingle_advisory_variabletcp_port_rover,-*henceitismostlyuseless.-*Codewillworknicelyifwejustdeleteit,but-*Iamafraidincontentedcaseitwillworknotbetteror-*evenworse:anothercpujustwillhitthesamebucket-*andspinthere.-*Sosomecpusaltcouldremovebothcontentionand-*memorypingpong.Anyideashowtodothisinaniceway?-*/-spin_lock(&tcp_portalloc_lock);-rover=tcp_port_rover;-do{-rover++;-if((rover<low)||(rover>high))-rover=low;head=&tcp_bhash[tcp_bhashfn(rover)];spin_lock(&head->lock);
@@ -704,9 +689,10 @@ static int tcp_v4_hash_connect(struct sonext_port:spin_unlock(&head->lock);++if(++rover>=high)+rover=low;}while(--remaining>0);-tcp_port_rover=rover;-spin_unlock(&tcp_portalloc_lock);local_bh_enable();
@@ -714,9 +700,6 @@ static int tcp_v4_hash_connect(struct sook:/* All locks still held and bhs disabled */-tcp_port_rover=rover;-spin_unlock(&tcp_portalloc_lock);-tcp_bind_hash(sk,tb,rover);if(sk_unhashed(sk)){inet_sk(sk)->sport=htons(rover);
@@ -136,13 +136,10 @@ static int tcp_v6_get_port(struct sock *intlow=sysctl_local_port_range[0];inthigh=sysctl_local_port_range[1];intremaining=(high-low)+1;-introver;+u16rover;-spin_lock(&tcp_portalloc_lock);-rover=tcp_port_rover;-do{rover++;-if((rover<low)||(rover>high))-rover=low;+rover=low+tcp_port_rover()%(high-low);+do{head=&tcp_bhash[tcp_bhashfn(rover)];spin_lock(&head->lock);tb_for_each(tb,node,&head->chain)
@@ -151,9 +148,9 @@ static int tcp_v6_get_port(struct sock *break;next:spin_unlock(&head->lock);+if(++rover>=high)+rover=low;}while(--remaining>0);-tcp_port_rover=rover;-spin_unlock(&tcp_portalloc_lock);/* Exhausted local port range during search? */ret=1;
From: Michael Vittrup Larsen <hidden> Date: 2004-11-01 09:58:23
On Friday 29 October 2004 19:28, Stephen Hemminger wrote:
Provide port randomization for incoming connections using variation of
existing sequence number hash. Replace tcp_portalloc_lock and
tcp_port_rover with atomic operation to allow better parallelism.
This is based on
http://www.ietf.org/internet-drafts/draft-larsen-tsvwg-port-randomisation-0
0.txt (with confirmation of of no IPR issues).
I have looked through this, and have a few comments:
* It is probably a good strategy to set 'tcp_rover_next' such that
the next search is resumed from the previous port found to be free.
(similar to the old algorithm). I don't see this in your patch,
but of course I could have missed it.
* connect_port_offset() does not (at least from an algorithm point
of view) need to return an u32, an u16 is sufficient.
Michael Larsen
From: Stephen Hemminger <hidden> Date: 2004-11-01 17:20:27
On Mon, 1 Nov 2004 11:58:23 +0200
Michael Vittrup Larsen [off-list ref] wrote:
On Friday 29 October 2004 19:28, Stephen Hemminger wrote:
quoted
Provide port randomization for incoming connections using variation of
existing sequence number hash. Replace tcp_portalloc_lock and
tcp_port_rover with atomic operation to allow better parallelism.
This is based on
http://www.ietf.org/internet-drafts/draft-larsen-tsvwg-port-randomisation-0
0.txt (with confirmation of of no IPR issues).
I have looked through this, and have a few comments:
* It is probably a good strategy to set 'tcp_rover_next' such that
the next search is resumed from the previous port found to be free.
(similar to the old algorithm). I don't see this in your patch,
but of course I could have missed it.
It was intentional since it would require holding a lock around the search. The tradeoff
is better SMP performance in the sparsely filled port space (more typical) vs.
better UP performance in the case of a mostly full port space.
* connect_port_offset() does not (at least from an algorithm point
of view) need to return an u32, an u16 is sufficient.
If it is truncated to u16, then compiler has to take extra effort to truncate
is unnecessary given later modulo operation.
From: Michael Vittrup Larsen <hidden> Date: 2004-11-02 07:54:44
On Monday 01 November 2004 18:20, Stephen Hemminger wrote:
quoted
* It is probably a good strategy to set 'tcp_rover_next' such that
the next search is resumed from the previous port found to be free.
(similar to the old algorithm). I don't see this in your patch,
but of course I could have missed it.
It was intentional since it would require holding a lock around the search.
The tradeoff is better SMP performance in the sparsely filled port space
(more typical) vs. better UP performance in the case of a mostly full port
space.
I think a typical scenario is many short-lived (e.g. minutes) TCP connections,
few long-lived (e.g. hours) connections and an ephemeral port wrap-around
probably also in hours - at least a long time compared to the life-time of
the short-lived connections.
This would result in a closely spaced 'group' of ports being occupied
somewhere in the ephemeral port range, and 'tcp_rover_next' would point at
the uppermost extreme of this group and thus always guarantee a free port on
first try (collisions will only happen with long-lived connections). If you
don't update 'tcp_rover_next', and this somehow gets to lag behind this
'group' of ports (say point at the lower extreme) you will need to search
through this group first before you enter the unoccupied port space.
Your scheme works initially because you do not lag behind the free port space,
but eventually you will, and I think this will result in less optimal
performance compared to the old behaviour.
Since updating the 'tcp_rover_next' practically always result in a free port
on first try, I think SMP performance will not suffer even though the lock
was held all through the port search (except when the port space is very
crowded).
And yes, I do use Linux exclusively, so I do care :-))
From a statistically point of view, if the connection life-times are uniformly
distributed from zero to infinite (theoretical scenario), it does not matter
what starting point you use. However, soon as life-times are not uniformly
distributed, this kind of search algorithm will benefit from good starting
point defining where the probability of used vs. unused port drop from high
to low.
The BSD solution with a pure random rover suffers similarly, especially when
the port space becomes crowded.
quoted
* connect_port_offset() does not (at least from an algorithm point
of view) need to return an u32, an u16 is sufficient.
If it is truncated to u16, then compiler has to take extra effort to
truncate is unnecessary given later modulo operation.
I agree (in fact thats what I argued in the draft) - it probably depends on
your platform - you are assuming a 32-bit platform I guess.
From: Stephen Hemminger <hidden> Date: 2004-11-04 18:01:04
On Tue, 2 Nov 2004 09:54:44 +0200
Michael Vittrup Larsen [off-list ref] wrote:
On Monday 01 November 2004 18:20, Stephen Hemminger wrote:
quoted
quoted
* It is probably a good strategy to set 'tcp_rover_next' such that
the next search is resumed from the previous port found to be free.
(similar to the old algorithm). I don't see this in your patch,
but of course I could have missed it.
It was intentional since it would require holding a lock around the search.
The tradeoff is better SMP performance in the sparsely filled port space
(more typical) vs. better UP performance in the case of a mostly full port
space.
I think a typical scenario is many short-lived (e.g. minutes) TCP connections,
few long-lived (e.g. hours) connections and an ephemeral port wrap-around
probably also in hours - at least a long time compared to the life-time of
the short-lived connections.
But because of the hashing most ports will be scattered all over the port space,
because they come from different hosts.
This would result in a closely spaced 'group' of ports being occupied
somewhere in the ephemeral port range, and 'tcp_rover_next' would point at
the uppermost extreme of this group and thus always guarantee a free port on
first try (collisions will only happen with long-lived connections). If you
don't update 'tcp_rover_next', and this somehow gets to lag behind this
'group' of ports (say point at the lower extreme) you will need to search
through this group first before you enter the unoccupied port space.
Also, Linux TCP will reuse ports if (saddr, daddr, sport) are different.
Look at __tcp_v4_check_established. This means that the ports actually have
to be in use with real connections to the same host.
Your scheme works initially because you do not lag behind the free port space,
but eventually you will, and I think this will result in less optimal
performance compared to the old behaviour.
Free port space should be evenly distributed because of the hash function.
Since updating the 'tcp_rover_next' practically always result in a free port
on first try, I think SMP performance will not suffer even though the lock
was held all through the port search (except when the port space is very
crowded).
But by not having a global lock on port allocation, different cpu's can be
searching different hash trees. This would matter under Dos attack with
multiple interfaces.
And yes, I do use Linux exclusively, so I do care :-))
quoted
From a statistically point of view, if the connection life-times are uniformly
distributed from zero to infinite (theoretical scenario), it does not matter
what starting point you use. However, soon as life-times are not uniformly
distributed, this kind of search algorithm will benefit from good starting
point defining where the probability of used vs. unused port drop from high
to low.
The BSD solution with a pure random rover suffers similarly, especially when
the port space becomes crowded.
quoted
quoted
* connect_port_offset() does not (at least from an algorithm point
of view) need to return an u32, an u16 is sufficient.
If it is truncated to u16, then compiler has to take extra effort to
truncate is unnecessary given later modulo operation.
I agree (in fact thats what I argued in the draft) - it probably depends on
your platform - you are assuming a 32-bit platform I guess.
From: Michael Vittrup Larsen <hidden> Date: 2004-11-05 10:03:58
On Thursday 04 November 2004 19:01, Stephen Hemminger wrote:
But because of the hashing most ports will be scattered all over the port
space, because they come from different hosts.
Also, Linux TCP will reuse ports if (saddr, daddr, sport) are different.
Look at __tcp_v4_check_established. This means that the ports actually
have to be in use with real connections to the same host.
__tcp_v4_check_established is the linux version of the uniqueness test from
the draft, and of course ports ports can be reused when at least one of the
other parameters (saddr, daddr, sport) are different.
I focus on the situation where (saddr, daddr, sport) is constant since this is
where we get collisions and need to try another port. Not storing the port
found to be unused will result in situations like:
tcp_rover_next is say 2000
Ports 2000-2010 are already used because you are browsing www.osdl.org
Your search will find 2011 to be unused after 10 retries and tcp_rover_next
will be 2001.
Your next search (you continue to browse www.osdl.org) will result in 2012 -
again after 10 retries.
In a simple browsing scenario like this, you will usually not have holes
because of TCP TIME-WAIT and your rover will continue to lag behind and you
will continue to make 10 retries on ports.
The question is then, when do the rover begin to lack behind?
Everytime you meet a long-lived connection in the port space your rover will
lag one port behind the real 'unused' rover. Using wget and browsing
www.osdl.org may easily produce this problem.
I understand your argument for not holding the lock, and maybe the following
algorithm is a compromise:
1. Use the current algorithm that does not hold the lock
2. If a port was found in first try, then exit (you have already
incremented tcp_rover_next by 1 so this is up to date as per
the old algorithm).
3. If more than one port try was necessary, compute the difference between
the initial rover to the current unused port, and atomic_add() this to
tcp_rover_next.
The only drawback of this is, that tcp_rover_next may 'run' a little too fast
in contention cases, which only has theoretical impact on performance. Also,
this only happens when we meet a long-lived connection, which we usually do
not have many of.
From: Stephen Hemminger <hidden> Date: 2004-11-17 23:30:25
Here is a more conservative version of earlier patch vthat keeps the same port rover locking and global port rover. This randomizes TCP ephemeral ports
of incoming connections using variation of existing sequence number hash.
Thanks to original author Michael Larsen.
http://www.ietf.org/internet-drafts/draft-larsen-tsvwg-port-randomisation-00.txt
It behaves correctly if someone is perverse and sets low > high
and it separates the outgoing port rover (tcp_port_rover) from the incoming port rover (start_rover).
Signed-off-by: Stephen Hemminger <redacted>
diff -Nru a/drivers/char/random.c b/drivers/char/random.c
@@ -2347,6 +2347,24 @@returnhalfMD4Transform(hash,keyptr->secret);}+/* Generate secure starting point for ephemeral TCP port search */+u32secure_tcp_port_ephemeral(__u32saddr,__u32daddr,__u16dport)+{+structkeydata*keyptr=get_keyptr();+u32hash[4];++/*+*Pickauniquestartingoffsetforeachephemeralportsearch+*(saddr,daddr,dport)and48bitsofrandomdata.+*/+hash[0]=saddr;+hash[1]=daddr;+hash[2]=dport^keyptr->secret[10];+hash[3]=keyptr->secret[11];++returnhalfMD4Transform(hash,keyptr->secret);+}+#ifdef CONFIG_SYN_COOKIES/**SecureSYNcookiecomputation.Thisisthealgorithmworkedoutby
From: Michael Vittrup Larsen <hidden> Date: 2004-11-19 07:38:37
I have looked through this patch and found no problems - thank you for
implementing the draft.
/Michael
On Thursday 18 November 2004 00:30, Stephen Hemminger wrote:
quoted hunk
Here is a more conservative version of earlier patch vthat keeps the same
port rover locking and global port rover. This randomizes TCP ephemeral
ports of incoming connections using variation of existing sequence number
hash.
Thanks to original author Michael Larsen.
http://www.ietf.org/internet-drafts/draft-larsen-tsvwg-port-randomisation-0
0.txt
It behaves correctly if someone is perverse and sets low > high
and it separates the outgoing port rover (tcp_port_rover) from the incoming
port rover (start_rover).
Signed-off-by: Stephen Hemminger <redacted>
diff -Nru a/drivers/char/random.c b/drivers/char/random.c
@@ -2347,6 +2347,24 @@returnhalfMD4Transform(hash,keyptr->secret);}+/* Generate secure starting point for ephemeral TCP port search */+u32secure_tcp_port_ephemeral(__u32saddr,__u32daddr,__u16dport)+{+structkeydata*keyptr=get_keyptr();+u32hash[4];++/*+*Pickauniquestartingoffsetforeachephemeralportsearch+*(saddr,daddr,dport)and48bitsofrandomdata.+*/+hash[0]=saddr;+hash[1]=daddr;+hash[2]=dport^keyptr->secret[10];+hash[3]=keyptr->secret[11];++returnhalfMD4Transform(hash,keyptr->secret);+}+#ifdef CONFIG_SYN_COOKIES/**SecureSYNcookiecomputation.Thisisthealgorithmworkedoutby
From: "David S. Miller" <davem@davemloft.net> Date: 2004-12-01 05:46:43
On Wed, 17 Nov 2004 15:30:25 -0800
Stephen Hemminger [off-list ref] wrote:
Here is a more conservative version of earlier patch vthat keeps the
same port rover locking and global port rover. This randomizes TCP
ephemeral ports of incoming connections using variation of existing
sequence number hash.
Thanks to original author Michael Larsen.
http://www.ietf.org/internet-drafts/draft-larsen-tsvwg-port-randomisation-00.txt
It behaves correctly if someone is perverse and sets low > high
and it separates the outgoing port rover (tcp_port_rover) from the
incoming port rover (start_rover).
I'm fine with this patch semantically. What do the
before/after microbenchmarks look like? We're adding
a MD4 transform plus a modulus for every local port
select operation.