bind error when using SO_REUSEPORT(implies SO_REUSEADDR)

Mikolaj Golub trociny at
Wed Jul 17 21:23:43 UTC 2013

On Tue, Jul 16, 2013 at 11:12:46AM -0400, John Baldwin wrote:
> On Thursday, March 15, 2012 8:07:46 pm Sean Bruno wrote:
> > On Thu, 2012-03-15 at 16:59 -0700, Sean Bruno wrote:
> > > Hey, I just found a bind bug ticket in my queue about bind.  I noted
> > > that on stable/6 stable/7 stable/9 & head the referenced code "fails".
> > > 
> > > It seems that this is a problem, but I have no idea if its a real
> > > problem or not.  Our devs think it is.  Anyway, here is a code snippet
> > > to show the failure in bind.  On linux/solaris this does not fail.
> > > 
> > >
> > > 
> > > simple compile with gcc -o test test.c and run as normal user.
> > > 
> > > Sean
> > > 
> > 
> > this is bind() not bind ... :-)
> Did the recent commit to HEAD fix this btw?

As for me, bind_test.c does not expose any bug in freebsd, it only
shows different behavior for freebsd and linux.

On freebsd the test output is:

serversock addr is
dup bind: Address already in use
This error was expected, tried to bind to used addr/port
BUG: binding duplicate socket to server port succeeded
dup2sock addr is
overlapping explicit bind to same port number succeeded without SO_REUSEPORT
listen succeeded after explicitly overlapping port bind
autosock addr is
bug triggered, port number conflict on sockets without SO_REUSEPORT
listen succeded after implicitly overlapping port bind

So, the first socket (serversock) is bound to the loopback address,
then it tries some combinations of binding the second socket to the
same port but to the wildcard address. When SO_REUSEADDR socket option
is set, binding to the wildcard address succeeds for freebsd (and
fails for linux).

They call this a bug in freebsd, but this is well known and expected
behavior (see e.g. Stevens' TCP/IP Illustrated Vol1). 

Or I missed the test's point?

Mikolaj Golub

More information about the freebsd-net mailing list