Skip to contentSkip to conversation

Allow dynamic choice of source port

PATCH In review Nielsen 3 participants 3 messages Nov 24, 2005 – Nov 28, 2005

lore
Share & tools
lore

Nielsen

author
#0 lore raw

PATCH 1/1 Allow dynamic choice of source port

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

As it is openvpn has no way to allow the kernel to dynamically choose a source port for it's connections. If you have two openvpn client processes on the same machine both you have to configure lport to different ports manually. This patch allows you to specify '--lport 0' and get the behaviour of normal programs, where the source port is dynamically chosen.

Not sure if this is an issue for others, but here's the patch in case anyone has run into the same issue.

Cheers,
Nate Nielsen

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDhqR2e/sRCNknZa8RAkxcAKCLOrVEiVp0NZ3cBr0XQuemmGMkZQCdFbWl hgFOINIEwoZnYcs2noCM8ik=
=2H3u
-----END PGP SIGNATURE-----

options.c renamed from openvpn-2.0/options.c +1 -1
diff -rU 3 ../openvpn-2.0/options.c ./options.c
@@ -3189,7 +3189,7 @@
       ++i;
       VERIFY_PERMISSION (OPT_P_GENERAL);
       port = atoi (p[1]);
-      if (!legal_ipv4_port (port))
+      if (port != 0 && !legal_ipv4_port (port))
 	{
 	  msg (msglevel, "Bad local port number: %s", p[1]);
 	  goto err;
socket.c renamed from openvpn-2.0/socket.c +13 -0
diff -rU 3 ../openvpn-2.0/socket.c ./socket.c
@@ -737,7 +737,20 @@
 	       print_sockaddr (&sock->info.lsa->local, &gc),
 	       strerror_ts (errnum, &gc));
 	}
+
+      /* If the local port was '0' look it up again */
+      if (sock->local_port == 0)
+	{
+	  socklen_t l = sizeof(sock->info.lsa->local);
+	  if (getsockname (sock->sd, (struct sockaddr *)&sock->info.lsa->local, &l) < 0)
+	    {
+	      const int errnum = openvpn_errno_socket ();
+	      msg (M_FATAL, "TCP/UDP: Socket lookup failed: %s",
+		   strerror_ts (errnum, &gc));
+	    }
+	}
     }
+
   gc_free (&gc);
 }
 
#1 lore raw
Quoted 10 lines · Nielsen

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

As it is openvpn has no way to allow the kernel to dynamically choose a source port for it's connections. If you have two openvpn client processes on the same machine both you have to configure lport to different ports manually. This patch allows you to specify '--lport 0' and get the behaviour of normal programs, where the source port is dynamically chosen.

Quoted 2 lines

Not sure if this is an issue for others, but here's the patch in case anyone has run into the same issue.

How is this different from simply using --nobind to allocate a dynamic port #?

James

#2 lore raw
Quoted 10 lines · James Yonan
Quoted 6 lines · Nielsen

As it is openvpn has no way to allow the kernel to dynamically choose a source port for it's connections. If you have two openvpn client processes on the same machine both you have to configure lport to different ports manually. This patch allows you to specify '--lport 0' and get the behaviour of normal programs, where the source port is dynamically chosen.

How is this different from simply using --nobind to allocate a dynamic port #?

The issue I ran into was that I did want to bind to an address (in order to make the packets come from a specific interface) but not to a specific port (as in some cases I needed to run multiple openvpn processes on a single interface).

Cheers,
Nate