Skip to contentSkip to conversation

new sysctl net/ipv4/ip_default_bind

RFC PATCH In review Mark Frazer 3 participants 4 messages Jan 17, 2002

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 new sysctl net/ipv4/ip_default_bind

The following patch applies against 2.4.17 and creates a new sysctl node /proc/sys/net/ipv4/ip_default_bind. The purpose of the control is to allow a default IP address to be selected by the sysadmin for outgoing connections. That is, for sockets which do not bind(2) the local end of the socket before calling connect(2).

For high-availability, we have several numbered interfaces on the same subnet. There is a virtual interface which is expected to be highly available. In order for connections to survive the disconnection of one or the physical interfaces, all connections should use the IP address of the virtual interface. We cannot use bonding as we have some cheezy tulip chip without an input for the link state signal provided by the PHY.

This patch causes legacy applications such as telnet to behave the way we like them to with no apparent adverse affects.

Does anyone find anything particularly offensive about this?

cheers
-mark

include/linux/sysctl.h +4 -1
diff -Nur linux/include/linux/sysctl.h linux.mjf/include/linux/sysctl.h
@@ -289,7 +289,8 @@
 	NET_TCP_ADV_WIN_SCALE=87,
 	NET_IPV4_NONLOCAL_BIND=88,
 	NET_IPV4_ICMP_RATELIMIT=89,
-	NET_IPV4_ICMP_RATEMASK=90
+	NET_IPV4_ICMP_RATEMASK=90,
+        NET_IPV4_DEFAULT_BIND=91
 };
 
 enum {
@@ -641,6 +642,8 @@
 			  void *buffer, size_t *lenp);
 
 extern int proc_dostring(ctl_table *, int, struct file *,
+			 void *, size_t *);
+extern int proc_doinaddr(ctl_table *, int, struct file *,
 			 void *, size_t *);
 extern int proc_dointvec(ctl_table *, int, struct file *,
 			 void *, size_t *);
kernel/sysctl.c +98 -0

104 lines not on this page · raw message

net/ipv4/af_inet.c +7 -0
diff -Nur linux/net/ipv4/af_inet.c linux.mjf/net/ipv4/af_inet.c
@@ -469,6 +469,8 @@
 
 /* It is off by default, see below. */
 int sysctl_ip_nonlocal_bind;
+/* Default local address to use. */
+struct in_addr sysctl_ip_default_bind;
 
 static int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 {
@@ -484,6 +486,11 @@
 
 	if (addr_len < sizeof(struct sockaddr_in))
 		return -EINVAL;
+
+        /* this will catch UDP sockets not bound before connect() */
+	if (addr->sin_addr.s_addr == INADDR_ANY) {
+		addr->sin_addr.s_addr = sysctl_ip_default_bind.s_addr;
+        }
 
 	chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
 
net/ipv4/sysctl_net_ipv4.c +4 -0
diff -Nur linux/net/ipv4/sysctl_net_ipv4.c linux.mjf/net/ipv4/sysctl_net_ipv4.c
@@ -17,6 +17,7 @@
 
 /* From af_inet.c */
 extern int sysctl_ip_nonlocal_bind;
+extern struct in_addr sysctl_ip_default_bind;
 
 /* From icmp.c */
 extern int sysctl_icmp_echo_ignore_all;
@@ -115,6 +116,9 @@
 	{NET_IPV4_NONLOCAL_BIND, "ip_nonlocal_bind",
 	 &sysctl_ip_nonlocal_bind, sizeof(int), 0644, NULL,
 	 &proc_dointvec},
+	{NET_IPV4_DEFAULT_BIND, "ip_default_bind",
+	 &sysctl_ip_default_bind, sizeof(struct in_addr), 0644, NULL,
+	 &proc_doinaddr},
 	{NET_IPV4_TCP_SYN_RETRIES, "tcp_syn_retries",
 	 &sysctl_tcp_syn_retries, sizeof(int), 0644, NULL, &proc_dointvec},
 	{NET_TCP_SYNACK_RETRIES, "tcp_synack_retries",
net/ipv4/tcp_ipv4.c +6 -0
diff -Nur linux/net/ipv4/tcp_ipv4.c linux.mjf/net/ipv4/tcp_ipv4.c
@@ -643,6 +643,7 @@
 }
 
 /* This will initiate an outgoing connection. */
+extern struct in_addr sysctl_ip_default_bind;
 int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
 {
 	struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
@@ -665,6 +666,11 @@
 			return -EINVAL;
 		nexthop = sk->protinfo.af_inet.opt->faddr;
 	}
+
+        /* This will catch TCP sockets not bound before connect */
+        if (sk->saddr == INADDR_ANY) {
+                sk->saddr = sysctl_ip_default_bind.s_addr;
+        }
 
 	tmp = ip_route_connect(&rt, nexthop, sk->saddr,
 			       RT_CONN_FLAGS(sk), sk->bound_dev_if);
#1 lore raw

If you setup your routes properly, one will be marked "primary" and will be used for outgoing address selection.

#2 lore raw
In reply to Mark Frazer
Quoted 5 lines · Mark Frazer

The following patch applies against 2.4.17 and creates a new sysctl node /proc/sys/net/ipv4/ip_default_bind. The purpose of the control is to allow a default IP address to be selected by the sysadmin for outgoing connections. That is, for sockets which do not bind(2) the local end of the socket before calling connect(2).

You can already do that using the 'from' attribute in iproute2 aka prefered source address per route. Just set it for your default route.

-Andi

#3 lore raw
In reply to Mark Frazer

Doh. I was using the old SIOCADDRT to add routes and such. Off to learn rtnetlink...

thanks
-mark

Andi Kleen <[email protected]> [02/01/17 17:42]:

Quoted 5 lines

You can already do that using the 'from' attribute in iproute2 aka prefered source address per route. Just set it for your default route.

-Andi

Signature · 1 line
"we are like unbaked soma vessels"