Anon port selection

Janne Huttunen jahuttun at gmail.com
Tue Jan 12 16:24:48 UTC 2010


> > Then when the second process in in_pcbbind_setup() tries
> > to check if the port is already in use, it won't match
> > the INADDR_ANY and assigns the same port again.
>
> Well it has been almost 20 years since I first ran across
> this issue and was told back then that it was "as designed".

May be, but it sure feels unintuitive to me. Besides, as far
as I can tell, NetBSD (5.0.1) doesn't do it, OpenBSD (4.6)
doesn't do it, Linux (2.6.31) doesn't do it and FreeBSD with
the attached patch doesn't do it. Whether or not this change
breaks something else is another story.


--- in_pcb.c.orig	2010-01-12 16:47:57.000000000 +0200
+++ in_pcb.c	2010-01-12 16:50:18.000000000 +0200
@@ -800,6 +800,12 @@ in_pcbconnect_setup(struct inpcb *inp, s
 	faddr = sin->sin_addr;
 	fport = sin->sin_port;

+	if (lport == 0) {
+		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
+		    cred);
+		if (error)
+			return (error);
+	}
 	if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
 		/*
 		 * If the destination address is INADDR_ANY,
@@ -864,12 +870,6 @@ in_pcbconnect_setup(struct inpcb *inp, s
 			*oinpp = oinp;
 		return (EADDRINUSE);
 	}
-	if (lport == 0) {
-		error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
-		    cred);
-		if (error)
-			return (error);
-	}
 	*laddrp = laddr.s_addr;
 	*lportp = lport;
 	*faddrp = faddr.s_addr;


More information about the freebsd-net mailing list