Skip to contentSkip to conversation

dynamic IP support for 2.4.0 (SIOCKILLADDR)

PATCH In review John Fremlin 7 participants 9 messages Jan 27, 2001 – Feb 4, 2001

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

PATCH 1/1 dynamic IP support for 2.4.0 (SIOCKILLADDR)

When the IP address of an interface changes, TCP connections with the old source address are useless. Applications are not notified of this and time out ordinarily, just as if nothing had happened. This is behaviour isn't very helpful when you have a dynamic IP and know you're probably not going to get the old one back. In that case, you want processes to get errors when they try to use one of the dead connections, so they can handle the disconnect more cleanly. Otherwise fetchmail, etc. can just hang waiting for ages. Andi Kleen implemented this functionality with a per interface flag in 2.2. See ftp.suse.com:/pub/people/ak/v2.2/iff-dynamic*.

The following patch against 2.4.0 does it a different way. It introduces a new ioctl, called SIOCKILLADDR. When this ioctl is called, it makes all IPv4 sockets with the specified source address return -ENETRESET when they are used.

Is this the right error number? I wasn't quite sure where the ioctl should go to be in keeping with convention - I bunged it in devinet_ioctl.

I patched userspace ppp-2.4.0 to use this functionality. It would be better if SIOCKILLADDR were not used until we are sure that the new IP is in fact different from the old one, but pppd in demand mode would not notice that there were extant connections and so would not bring up the link - so the problem would not be alleviated. Therefore SIOCKILLADDR is used on disconnect. The functionality is activated with the killoldaddr option. I would be happy to document it in the manpage if it were accepted. Further the build process is cleaned up slightly, as in the patch I sent on or around 8 October 2000.

include/linux/sockios.h +1 -0
diff -u --exclude *~ --recursive linux-2.4.0-orig/include/linux/sockios.h linux-hacked-dynip/include/linux/sockios.h
@@ -65,6 +65,7 @@
 #define SIOCDIFADDR	0x8936		/* delete PA address		*/
 #define	SIOCSIFHWBROADCAST	0x8937	/* set hardware broadcast addr	*/
 #define SIOCGIFCOUNT	0x8938		/* get number of devices */
+#define SIOCKILLADDR	0x8939		/* kill all connections with this local address */
 
 #define SIOCGIFBR	0x8940		/* Bridging support		*/
 #define SIOCSIFBR	0x8941		/* Set bridging options 	*/
include/net/tcp.h +1 -2
diff -u --exclude *~ --recursive linux-2.4.0-orig/include/net/tcp.h linux-hacked-dynip/include/net/tcp.h
@@ -787,9 +787,8 @@
 extern int			tcp_disconnect(struct sock *sk, int flags);
 
 extern void			tcp_unhash(struct sock *sk);
-
 extern int			tcp_v4_hash_connecting(struct sock *sk);
-
+extern void		tcp_v4_zap_saddr(u32 saddr);
 
 /* From syncookies.c */
 extern struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb, 
net/ipv4/af_inet.c +1 -0
diff -u --exclude *~ --recursive linux-2.4.0-orig/net/ipv4/af_inet.c linux-hacked-dynip/net/ipv4/af_inet.c
@@ -854,6 +854,7 @@
 		case SIOCSIFPFLAGS:	
 		case SIOCGIFPFLAGS:	
 		case SIOCSIFFLAGS:
+		case SIOCKILLADDR:
 			return(devinet_ioctl(cmd,(void *) arg));
 		case SIOCGIFBR:
 		case SIOCSIFBR:
net/ipv4/devinet.c +8 -1
diff -u --exclude *~ --recursive linux-2.4.0-orig/net/ipv4/devinet.c linux-hacked-dynip/net/ipv4/devinet.c
@@ -510,6 +510,7 @@
 	case SIOCSIFBRDADDR:	/* Set the broadcast address */
 	case SIOCSIFDSTADDR:	/* Set the destination address */
 	case SIOCSIFNETMASK: 	/* Set the netmask for the interface */
+	case SIOCKILLADDR:	/* Kill all connections with this local address */
 		if (!capable(CAP_NET_ADMIN))
 			return -EACCES;
 		if (sin->sin_family != AF_INET)
@@ -536,7 +537,10 @@
 				break;
 	}
 
-	if (ifa == NULL && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS) {
+	if (ifa == NULL
+	    && cmd != SIOCSIFADDR
+	    && cmd != SIOCSIFFLAGS
+	    && cmd != SIOCKILLADDR) {
 		ret = -EADDRNOTAVAIL;
 		goto done;
 	}
@@ -646,6 +650,9 @@
 				ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
 				inet_insert_ifa(ifa);
 			}
+			break;
+		case SIOCKILLADDR:	/* Kill all connections with this local address */
+			tcp_v4_zap_saddr(sin->sin_addr.s_addr);
 			break;
 	}
 done:
net/ipv4/tcp_ipv4.c +32 -0
diff -u --exclude *~ --recursive linux-2.4.0-orig/net/ipv4/tcp_ipv4.c linux-hacked-dynip/net/ipv4/tcp_ipv4.c
@@ -390,6 +390,38 @@
 		wake_up(&tcp_lhash_wait);
 }
 
+/* Terminate all active connections with a local address equal to
+ * SADDR.  If sysctl_ip_dynaddr is set, connections in the SYN_SENT
+ * state are not closed, because their source address will presumably
+ * be rewritten.
+ */
+void tcp_v4_zap_saddr(u32 saddr) 
+{
+	int i;
+	rwlock_t *lock;
+	struct sock *sk;
+	
+	for (i = 0; i < (tcp_ehash_size<<1); i++) {
+		lock = &tcp_ehash[i].lock;
+		
+		read_lock(lock);
+
+		for(sk = tcp_ehash[i].chain; sk; sk = sk->next) 
+			if(sk->rcv_saddr == saddr)
+			{
+				if(sysctl_ip_dynaddr && sk->state == TCP_SYN_SENT)
+					continue;
+				
+				sk->err = ENETRESET;
+				sk->error_report(sk);
+
+				tcp_done(sk);
+			}
+		
+		read_unlock(lock);
+	}
+}
+
 /* Don't inline this cruft.  Here are some nice properties to
  * exploit here.  The BSD API does not allow a listening TCP
  * to specify the remote port nor the remote address for the
chat/Makefile.linux +2 -2
diff -u --recursive ppp-2.4.0-orig/chat/Makefile.linux ppp-2.4.0-hacked/chat/Makefile.linux
@@ -6,14 +6,14 @@
 CDEF4=	-DFNDELAY=O_NDELAY		# Old name value
 CDEFS=	$(CDEF1) $(CDEF2) $(CDEF3) $(CDEF4)
 
-CFLAGS=	-O2 -g -pipe $(CDEFS)
+CFLAGS=	$(COPTS) $(CDEFS)
 
 INSTALL= install
 
 all:	chat
 
 chat:	chat.o
-	$(CC) -o chat chat.o
+	$(CC) $(LDFLAGS) -o chat chat.o
 
 chat.o:	chat.c
 	$(CC) -c $(CFLAGS) -o chat.o chat.c
pppd/options.c +7 -0
diff -u --recursive ppp-2.4.0-orig/pppd/options.c ppp-2.4.0-hacked/pppd/options.c
@@ -77,6 +77,9 @@
 char	user[MAXNAMELEN];	/* Username for PAP */
 char	passwd[MAXSECRETLEN];	/* Password for PAP */
 bool	persist = 0;		/* Reopen link after it goes down */
+bool	killoldaddr = 0;		/* If our IP is reassigned on
+				    reconnect, kill active TCP
+				     connections using the old IP. */
 char	our_name[MAXNAMELEN];	/* Our name for authentication purposes */
 bool	demand = 0;		/* do dial-on-demand */
 char	*ipparam = NULL;	/* Extra parameter for ip up/down scripts */
@@ -194,6 +197,10 @@
       "Turn off persist option" },
     { "demand", o_bool, &demand,
       "Dial on demand", OPT_INITONLY | 1, &persist },
+    { "killoldaddr", o_bool, &killoldaddr,
+      "Kill connections from an old source address", 1},
+    { "nokilloldaddr", o_bool,&killoldaddr,
+      "Don't kill connections from an old source address" },
     { "--version", o_special_noarg, (void *)showversion,
       "Show version number" },
     { "--help", o_special_noarg, (void *)showhelp,
pppd/pppd.h +3 -0
diff -u --recursive ppp-2.4.0-orig/pppd/pppd.h ppp-2.4.0-hacked/pppd/pppd.h
@@ -235,6 +235,9 @@
 extern char	remote_name[MAXNAMELEN]; /* Peer's name for authentication */
 extern bool	explicit_remote;/* remote_name specified with remotename opt */
 extern bool	demand;		/* Do dial-on-demand */
+extern bool	killoldaddr;	/* If our IP is reassigned on
+				    reconnect, kill active TCP
+				     connections using the old IP. */
 extern char	*ipparam;	/* Extra parameter for ip up/down scripts */
 extern bool	cryptpap;	/* Others' PAP passwords are encrypted */
 extern int	idle_time_limit;/* Shut down link if idle for this long */
pppd/sys-linux.c +47 -9

109 lines not on this page · raw message

pppdump/Makefile.linux +2 -2
diff -u --recursive ppp-2.4.0-orig/pppdump/Makefile.linux ppp-2.4.0-hacked/pppdump/Makefile.linux
@@ -1,4 +1,4 @@
-CFLAGS= -O -I../include/net
+CFLAGS= $(COPTS) -I../include/net
 OBJS = pppdump.o bsd-comp.o deflate.o zlib.o
 
 INSTALL= install
@@ -6,7 +6,7 @@
 all:	pppdump
 
 pppdump: $(OBJS)
-	$(CC) -o pppdump $(OBJS)
+	$(CC) $(LDFLAGS) -o pppdump $(OBJS)
 
 clean:
 	rm -f pppdump $(OBJS) *~
pppstats/Makefile.linux +1 -1
diff -u --recursive ppp-2.4.0-orig/pppstats/Makefile.linux ppp-2.4.0-hacked/pppstats/Makefile.linux
@@ -22,7 +22,7 @@
 	$(INSTALL) -c -m 444 pppstats.8 $(MANDIR)/man8/pppstats.8
 
 pppstats: $(PPPSTATSRCS)
-	$(CC) $(CFLAGS) -o pppstats pppstats.c $(LIBS)
+	$(CC) $(CFLAGS) $(LDFLAGS) -o pppstats pppstats.c $(LIBS)
 
 clean:
 	rm -f pppstats *~ #* core
Signature · 3 lines
-- 

	http://www.penguinpowered.com/~vii
#1 lore raw
Quoted 5 lines · John Fremlin

When the IP address of an interface changes, TCP connections with the old source address are useless. Applications are not notified of this and time out ordinarily, just as if nothing had happened. This is behaviour isn't very helpful when you have a dynamic IP and know you're probably not going to get the old one back. In that case, you

...

Quoted 3 lines

I patched userspace ppp-2.4.0 to use this functionality. It would be better if SIOCKILLADDR were not used until we are sure that the new IP is in fact different from the old one, but pppd in demand mode would

I get the same IP about 2/3 of the time, so it is pretty important to avoid killing connections until after the new IP is known. -
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/

#2 lore raw

"Albert D. Cahalan" <[email protected]> writes:
[...]

Quoted 6 lines
Quoted 3 lines

I patched userspace ppp-2.4.0 to use this functionality. It would be better if SIOCKILLADDR were not used until we are sure that the new IP is in fact different from the old one, but pppd in demand mode would

I get the same IP about 2/3 of the time, so it is pretty important to avoid killing connections until after the new IP is known.

I'll try to explain again. If you have an existing (e.g. ssh) connection to a host across the interface, and the interface comes down then pppd _will not bring it up again_ until you try to start a new connection, as far as I have experienced. Therefore you will get the old behaviour and my patch will do nothing. I decided it was better to inform ssh that the link was dead.

Like I said, the solution to this is to make pppd cleverer about bringing the link up when there are existing
connections. Alternatively, you could have some dubious script parsing netstat checking whether there are connections over the interface. and pinging hosts at intervals to bring the link up again ;-)

Here is a patch for pppd-2.4.0 orig that will give you the behaviour you want, provided you can solve the problem in the first paragraph. It almost exactly the same as my last patch. It compiles and everything. Note that there are no changes required to the kernel side patch to enable this functionality.

chat/Makefile.linux +2 -2
diff -u --recursive ppp-2.4.0-orig/chat/Makefile.linux ppp-2.4.0-hacked/chat/Makefile.linux
@@ -6,14 +6,14 @@
 CDEF4=	-DFNDELAY=O_NDELAY		# Old name value
 CDEFS=	$(CDEF1) $(CDEF2) $(CDEF3) $(CDEF4)
 
-CFLAGS=	-O2 -g -pipe $(CDEFS)
+CFLAGS=	$(COPTS) $(CDEFS)
 
 INSTALL= install
 
 all:	chat
 
 chat:	chat.o
-	$(CC) -o chat chat.o
+	$(CC) $(LDFLAGS) -o chat chat.o
 
 chat.o:	chat.c
 	$(CC) -c $(CFLAGS) -o chat.o chat.c
pppd/options.c +7 -0
diff -u --recursive ppp-2.4.0-orig/pppd/options.c ppp-2.4.0-hacked/pppd/options.c
@@ -77,6 +77,9 @@
 char	user[MAXNAMELEN];	/* Username for PAP */
 char	passwd[MAXSECRETLEN];	/* Password for PAP */
 bool	persist = 0;		/* Reopen link after it goes down */
+bool	killoldaddr = 0;		/* If our IP is reassigned on
+				    reconnect, kill active TCP
+				     connections using the old IP. */
 char	our_name[MAXNAMELEN];	/* Our name for authentication purposes */
 bool	demand = 0;		/* do dial-on-demand */
 char	*ipparam = NULL;	/* Extra parameter for ip up/down scripts */
@@ -194,6 +197,10 @@
       "Turn off persist option" },
     { "demand", o_bool, &demand,
       "Dial on demand", OPT_INITONLY | 1, &persist },
+    { "killoldaddr", o_bool, &killoldaddr,
+      "Kill connections from an old source address", 1},
+    { "nokilloldaddr", o_bool,&killoldaddr,
+      "Don't kill connections from an old source address" },
     { "--version", o_special_noarg, (void *)showversion,
       "Show version number" },
     { "--help", o_special_noarg, (void *)showhelp,
pppd/pppd.h +3 -0
diff -u --recursive ppp-2.4.0-orig/pppd/pppd.h ppp-2.4.0-hacked/pppd/pppd.h
@@ -235,6 +235,9 @@
 extern char	remote_name[MAXNAMELEN]; /* Peer's name for authentication */
 extern bool	explicit_remote;/* remote_name specified with remotename opt */
 extern bool	demand;		/* Do dial-on-demand */
+extern bool	killoldaddr;	/* If our IP is reassigned on
+				    reconnect, kill active TCP
+				     connections using the old IP. */
 extern char	*ipparam;	/* Extra parameter for ip up/down scripts */
 extern bool	cryptpap;	/* Others' PAP passwords are encrypted */
 extern int	idle_time_limit;/* Shut down link if idle for this long */
pppd/sys-linux.c +44 -9

100 lines not on this page · raw message

pppdump/Makefile.linux +2 -2
diff -u --recursive ppp-2.4.0-orig/pppdump/Makefile.linux ppp-2.4.0-hacked/pppdump/Makefile.linux
@@ -1,4 +1,4 @@
-CFLAGS= -O -I../include/net
+CFLAGS= $(COPTS) -I../include/net
 OBJS = pppdump.o bsd-comp.o deflate.o zlib.o
 
 INSTALL= install
@@ -6,7 +6,7 @@
 all:	pppdump
 
 pppdump: $(OBJS)
-	$(CC) -o pppdump $(OBJS)
+	$(CC) $(LDFLAGS) -o pppdump $(OBJS)
 
 clean:
 	rm -f pppdump $(OBJS) *~
pppstats/Makefile.linux +1 -1
diff -u --recursive ppp-2.4.0-orig/pppstats/Makefile.linux ppp-2.4.0-hacked/pppstats/Makefile.linux
@@ -22,7 +22,7 @@
 	$(INSTALL) -c -m 444 pppstats.8 $(MANDIR)/man8/pppstats.8
 
 pppstats: $(PPPSTATSRCS)
-	$(CC) $(CFLAGS) -o pppstats pppstats.c $(LIBS)
+	$(CC) $(CFLAGS) $(LDFLAGS) -o pppstats pppstats.c $(LIBS)
 
 clean:
 	rm -f pppstats *~ #* core
Signature · 3 lines
-- 

	http://www.penguinpowered.com/~vii
#3 lore raw
In reply to John Fremlin
Quoted 7 lines · Albert D. Cahalan
Quoted 5 lines · John Fremlin

When the IP address of an interface changes, TCP connections with the old source address are useless. Applications are not notified of this and time out ordinarily, just as if nothing had happened. This is behaviour isn't very helpful when you have a dynamic IP and know you're probably not going to get the old one back. In that case, you

Quoted 7 lines

...

Quoted 3 lines

I patched userspace ppp-2.4.0 to use this functionality. It would be better if SIOCKILLADDR were not used until we are sure that the new IP is in fact different from the old one, but pppd in demand mode would

I get the same IP about 2/3 of the time, so it is pretty important to avoid killing connections until after the new IP is known.

I prefer it when the IP is killed as soon as possible so that I can see when the connection is lost (ssh sessions get killed etc.)

Another reason for killing as soon as possible is the last-ack problem. When the other end goes away suddenly TCP often gets into last-ack state. This means it'll retransmit a FIN until it times out or the other end answers. Each such retransmitted FIN triggers a new dialin, which can get quite costly when you don't have flat rate (like still most of Europe). With your approach (waiting until the new IP is known) it would cost at least another dialin in this case.

When you have flatrate your way may be better of course, so a final user space solution could switch it via a pppd flag.

[I agree that the user space way is better than my kernel hacks]

-Andi

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/

#4 lore raw
In reply to John Fremlin
Quoted 5 lines · Andi Kleen
Quoted 2 lines

I get the same IP about 2/3 of the time, so it is pretty important to avoid killing connections until after the new IP is known.

I prefer it when the IP is killed as soon as possible so that I can see when the connection is lost (ssh sessions get killed etc.)

I like it when I get the same IP back and can continue an ssh session. My line drops regularly in mid session.

Unfortunately getting the same IP is rare now, so I've been toying with running a PPP tunnel through a fixed host out on the net. The tunnel would be dropped and recreated with each new connection. My local link IP would change, but the tunnel IP would not so connections to other places, ssh etc. would all be from the tunnel IP.

The important thing is that the tunnel is destroyed and recreated (it has to be, it is over different underlying link addresses). I do not want that to destroy the connections from the tunnelled address.

-- Jamie
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/

#5 lore raw
Quoted 5 lines · Jamie Lokier

Unfortunately getting the same IP is rare now, so I've been toying with running a PPP tunnel through a fixed host out on the net. The tunnel would be dropped and recreated with each new connection. My local link IP would change, but the tunnel IP would not so connections to other places, ssh etc. would all be from the tunnel IP.

ciped is great for this. I use it to tunnel ssh from my home dialup to work. Very stable, and with cipe's shared keys, there's nothing too taxing about setting it up.

I just have a call to /etc/init.d/ciped restart in my ppp up script.

freeswan was another way I looked at , but ip/sec was horrible at the time and didn't (maybe still doesn't) deal with dynamic ip assignment nicely.

Cheers,

Mark

Signature · 10 lines
+-------------------------------------------------------------------------+
Mark Cooke                  The views expressed above are mine and are not
Systems Programmer          necessarily representative of university policy
University Of Birmingham    URL: http://www.sr.bham.ac.uk/~mpc/
+-------------------------------------------------------------------------+

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/
#6 lore raw
In reply to John Fremlin
Quoted 3 lines · Jamie Lokier

The important thing is that the tunnel is destroyed and recreated (it has to be, it is over different underlying link addresses). I do not want that to destroy the connections from the tunnelled address.

Just do not set IFF_DYNAMIC on the tunnel interface then, that is why it is a flag and not hardcoded.

-Andi

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/

#7 lore raw
In reply to John Fremlin

Jamie Lokier <[email protected]> writes:

[...]

Quoted 4 lines

The important thing is that the tunnel is destroyed and recreated (it has to be, it is over different underlying link addresses). I do not want that to destroy the connections from the tunnelled address.

No connections at all will be destroyed by my patch unless you enable the new killoldaddr pppd option.

Signature · 5 lines
	http://www.penguinpowered.com/~vii
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/
#8 lore raw
In reply to John Fremlin
Quoted 1 line · Jamie Lokier

Unfortunately getting the same IP is rare now, so I've been toying with

Pretty much dependant of the type of equipment and the configuration used at the ISP's servers. I use two ISPs when I'm back in Germany of which the one always and the other one never gives me the same IP when I reconnect within some short time.

(Guess which one I prefer ...)

Ralf
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/