Skip to contentSkip to conversation

slirp: Handle whole 127.0.0.0/8 network as local addresses.

PATCH Superseded Anders Waldenborg 3 participants 7 messages Jun 12, 2012 – Jul 14, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 slirp: Handle whole 127.0.0.0/8 network as local addresses.

Changes so translation of remote address to the host's ip address in the virtual network happens for all addresses in the 127.0.0.0/8 network, not just 127.0.0.1.

This fixes hostfwd bound to addresses such as 127.0.0.2 works

---
 slirp/main.h     |    1 +
 slirp/slirp.c    |    3 +++
 slirp/tcp_subr.c |    4 +++-
 3 files changed, 7 insertions(+), 1 deletions(-)
slirp/main.h +1 -0
index 028df4b..bf601e2 100644
@@ -31,6 +31,7 @@ extern char *exec_shell;
 extern u_int curtime;
 extern fd_set *global_readfds, *global_writefds, *global_xfds;
 extern struct in_addr loopback_addr;
+extern in_addr_t loopback_mask;
 extern char *username;
 extern char *socket_path;
 extern int towrite_max;
slirp/slirp.c +3 -0
index 90473eb..dbb83ec 100644
@@ -29,6 +29,8 @@
 
 /* host loopback address */
 struct in_addr loopback_addr;
+/* host loopback network mask */
+in_addr_t loopback_mask;
 
 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
 static const uint8_t special_ethaddr[ETH_ALEN] = {
@@ -191,6 +193,7 @@ static void slirp_init_once(void)
 #endif
 
     loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
+    loopback_mask = htonl(0xff000000); /* 255.0.0.0 */
 }
 
 static void slirp_state_save(QEMUFile *f, void *opaque);
slirp/tcp_subr.c +3 -1
index 0a545c4..71f2b19 100644
@@ -435,7 +435,9 @@ tcp_connect(struct socket *inso)
 	so->so_fport = addr.sin_port;
 	so->so_faddr = addr.sin_addr;
 	/* Translate connections from localhost to the real hostname */
-	if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
+	if ((so->so_faddr.s_addr == 0) ||
+	    ((so->so_faddr.s_addr & loopback_mask) ==
+	     (loopback_addr.s_addr & loopback_mask)))
 	   so->so_faddr = slirp->vhost_addr;
 
 	/* Close the accept() socket, set right state */
Signature · 1 line
1.7.2.5
#1 lore raw
Quoted 5 lines · Anders Waldenborg

Changes so translation of remote address to the host's ip address in the virtual network happens for all addresses in the 127.0.0.0/8 network, not just 127.0.0.1.

This fixes hostfwd bound to addresses such as 127.0.0.2 works

Looks good, just two nits below.

Quoted 34 lines
---
 slirp/main.h     |    1 +
 slirp/slirp.c    |    3 +++
 slirp/tcp_subr.c |    4 +++-
 3 files changed, 7 insertions(+), 1 deletions(-)
slirp/main.h +1 -0
index 028df4b..bf601e2 100644
@@ -31,6 +31,7 @@ extern char *exec_shell;
 extern u_int curtime;
 extern fd_set *global_readfds, *global_writefds, *global_xfds;
 extern struct in_addr loopback_addr;
+extern in_addr_t loopback_mask;
 extern char *username;
 extern char *socket_path;
 extern int towrite_max;
slirp/slirp.c +2 -0
index 90473eb..dbb83ec 100644
@@ -29,6 +29,8 @@
 
 /* host loopback address */
 struct in_addr loopback_addr;
+/* host loopback network mask */
+in_addr_t loopback_mask;
 
 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
 static const uint8_t special_ethaddr[ETH_ALEN] = {

slirp/slirp.c static void slirp_init_once(void) · line 196

Quoted 4 lines
slirp/slirp.c +1 -0
@@ -191,6 +193,7 @@ static void slirp_init_once(void)
 #endif
 
     loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
+    loopback_mask = htonl(0xff000000); /* 255.0.0.0 */

IN_CLASSA_NET would be clearer.

slirp/tcp_subr.c tcp_connect(struct socket *inso) · line 438

Quoted 13 lines
slirp/slirp.c +0 -0
 }
 
 static void slirp_state_save(QEMUFile *f, void *opaque);
slirp/tcp_subr.c +1 -1
index 0a545c4..71f2b19 100644
@@ -435,7 +435,9 @@ tcp_connect(struct socket *inso)
 	so->so_fport = addr.sin_port;
 	so->so_faddr = addr.sin_addr;
 	/* Translate connections from localhost to the real hostname */
-	if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
+	if ((so->so_faddr.s_addr == 0) ||

The inner braces are unneeded, also below (wouldn't mind if the above remark didn't require a v2 anyway).

slirp/tcp_subr.c tcp_connect(struct socket *inso) · line 443

Quoted 6 lines
slirp/tcp_subr.c +2 -0
+	    ((so->so_faddr.s_addr & loopback_mask) ==
+	     (loopback_addr.s_addr & loopback_mask)))
 	   so->so_faddr = slirp->vhost_addr;
 
 	/* Close the accept() socket, set right state */

Thanks,
Jan

#2 lore raw

PATCH v2 1/1 slirp: Handle whole 127.0.0.0/8 network as local addresses.

Changes so translation of remote address to the host's ip address in the virtual network happens for all addresses in the 127.0.0.0/8 network, not just 127.0.0.1.

This fixes so that hostfwd bound to addresses such as 127.0.0.2 works.

---

Thanks for the review!

Patch updated according to comments.

Notice that the surrounding code in tcp_subr.c uses tabs for indentation. Should I still use space as the coding style mandates (and which makes checkpatch happy)?

 slirp/main.h     |    1 +
 slirp/slirp.c    |    3 +++
 slirp/tcp_subr.c |    6 ++++--
 3 files changed, 8 insertions(+), 2 deletions(-)
slirp/main.h +1 -0
index 028df4b..bf601e2 100644
@@ -31,6 +31,7 @@ extern char *exec_shell;
 extern u_int curtime;
 extern fd_set *global_readfds, *global_writefds, *global_xfds;
 extern struct in_addr loopback_addr;
+extern in_addr_t loopback_mask;
 extern char *username;
 extern char *socket_path;
 extern int towrite_max;
slirp/slirp.c +3 -0
index 90473eb..9787104 100644
@@ -29,6 +29,8 @@
 
 /* host loopback address */
 struct in_addr loopback_addr;
+/* host loopback network mask */
+in_addr_t loopback_mask;
 
 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
 static const uint8_t special_ethaddr[ETH_ALEN] = {
@@ -191,6 +193,7 @@ static void slirp_init_once(void)
 #endif
 
     loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
+    loopback_mask = htonl(IN_CLASSA_NET);
 }
 
 static void slirp_state_save(QEMUFile *f, void *opaque);
slirp/tcp_subr.c +4 -2
index 0a545c4..064b5e8 100644
@@ -435,8 +435,10 @@ tcp_connect(struct socket *inso)
 	so->so_fport = addr.sin_port;
 	so->so_faddr = addr.sin_addr;
 	/* Translate connections from localhost to the real hostname */
-	if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
-	   so->so_faddr = slirp->vhost_addr;
+    if (so->so_faddr.s_addr == 0 ||
+        (so->so_faddr.s_addr & loopback_mask) ==
+        (loopback_addr.s_addr & loopback_mask))
+        so->so_faddr = slirp->vhost_addr;
 
 	/* Close the accept() socket, set right state */
 	if (inso->so_state & SS_FACCEPTONCE) {
Signature · 1 line
1.7.2.5
#3 lore raw
Quoted 9 lines · Anders Waldenborg

Changes so translation of remote address to the host's ip address in the virtual network happens for all addresses in the 127.0.0.0/8 network, not just 127.0.0.1.

This fixes so that hostfwd bound to addresses such as 127.0.0.2 works.

---

Quoted 7 lines

Thanks for the review!

Patch updated according to comments.

Notice that the surrounding code in tcp_subr.c uses tabs for indentation. Should I still use space as the coding style mandates (and which makes checkpatch happy)?

Yes, the goal is to get rid of tabs.

Quoted 44 lines
 slirp/main.h     |    1 +
 slirp/slirp.c    |    3 +++
 slirp/tcp_subr.c |    6 ++++--
 3 files changed, 8 insertions(+), 2 deletions(-)
slirp/main.h +1 -0
index 028df4b..bf601e2 100644
@@ -31,6 +31,7 @@ extern char *exec_shell;
 extern u_int curtime;
 extern fd_set *global_readfds, *global_writefds, *global_xfds;
 extern struct in_addr loopback_addr;
+extern in_addr_t loopback_mask;
 extern char *username;
 extern char *socket_path;
 extern int towrite_max;
slirp/slirp.c +3 -0
index 90473eb..9787104 100644
@@ -29,6 +29,8 @@
 
 /* host loopback address */
 struct in_addr loopback_addr;
+/* host loopback network mask */
+in_addr_t loopback_mask;
 
 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
 static const uint8_t special_ethaddr[ETH_ALEN] = {
@@ -191,6 +193,7 @@ static void slirp_init_once(void)
 #endif
 
     loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
+    loopback_mask = htonl(IN_CLASSA_NET);
 }
 
 static void slirp_state_save(QEMUFile *f, void *opaque);
slirp/tcp_subr.c +0 -0
index 0a545c4..064b5e8 100644

slirp/tcp_subr.c tcp_connect(struct socket *inso) · line 440

Quoted 8 lines
slirp/tcp_subr.c +3 -2
@@ -435,8 +435,10 @@ tcp_connect(struct socket *inso)
        so->so_fport = addr.sin_port;
        so->so_faddr = addr.sin_addr;
        /* Translate connections from localhost to the real hostname */
-       if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
-          so->so_faddr = slirp->vhost_addr;
+    if (so->so_faddr.s_addr == 0 ||
+        (so->so_faddr.s_addr & loopback_mask) ==
+        (loopback_addr.s_addr & loopback_mask))

Please add braces.

Quoted 8 lines
slirp/tcp_subr.c +1 -1
+        so->so_faddr = slirp->vhost_addr;
 
        /* Close the accept() socket, set right state */
        if (inso->so_state & SS_FACCEPTONCE) {
--

1.7.2.5

#4 lore raw

PATCH v3 1/1 slirp: Handle whole 127.0.0.0/8 network as local addresses.

Changes so translation of remote address to the host's ip address in the virtual network happens for all addresses in the 127.0.0.0/8 network, not just 127.0.0.1.

This fixes so that hostfwd bound to addresses such as 127.0.0.2 works.

---

Quoted 5 lines · Blue Swirl
Quoted 3 lines

Notice that the surrounding code in tcp_subr.c uses tabs for indentation. Should I still use space as the coding style mandates (and which makes checkpatch happy)?

Yes, the goal is to get rid of tabs.

OK. Thanks for clarifacion and review!

Patch updated.

 slirp/main.h     |    1 +
 slirp/slirp.c    |    3 +++
 slirp/tcp_subr.c |    7 +++++--
 3 files changed, 9 insertions(+), 2 deletions(-)
slirp/main.h +1 -0
index 028df4b..bf601e2 100644
@@ -31,6 +31,7 @@ extern char *exec_shell;
 extern u_int curtime;
 extern fd_set *global_readfds, *global_writefds, *global_xfds;
 extern struct in_addr loopback_addr;
+extern in_addr_t loopback_mask;
 extern char *username;
 extern char *socket_path;
 extern int towrite_max;
slirp/slirp.c +3 -0
index 90473eb..9787104 100644
@@ -29,6 +29,8 @@
 
 /* host loopback address */
 struct in_addr loopback_addr;
+/* host loopback network mask */
+in_addr_t loopback_mask;
 
 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
 static const uint8_t special_ethaddr[ETH_ALEN] = {
@@ -191,6 +193,7 @@ static void slirp_init_once(void)
 #endif
 
     loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
+    loopback_mask = htonl(IN_CLASSA_NET);
 }
 
 static void slirp_state_save(QEMUFile *f, void *opaque);
slirp/tcp_subr.c +5 -2
index 0a545c4..6b01f74 100644
@@ -435,8 +435,11 @@ tcp_connect(struct socket *inso)
 	so->so_fport = addr.sin_port;
 	so->so_faddr = addr.sin_addr;
 	/* Translate connections from localhost to the real hostname */
-	if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
-	   so->so_faddr = slirp->vhost_addr;
+    if (so->so_faddr.s_addr == 0 ||
+        (so->so_faddr.s_addr & loopback_mask) ==
+        (loopback_addr.s_addr & loopback_mask)) {
+        so->so_faddr = slirp->vhost_addr;
+    }
 
 	/* Close the accept() socket, set right state */
 	if (inso->so_state & SS_FACCEPTONCE) {
Signature · 1 line
1.7.2.5
#5 lore raw
Quoted 63 lines · Anders Waldenborg

Changes so translation of remote address to the host's ip address in the virtual network happens for all addresses in the 127.0.0.0/8 network, not just 127.0.0.1.

This fixes so that hostfwd bound to addresses such as 127.0.0.2 works.

---

Quoted 5 lines · Blue Swirl
Quoted 3 lines

Notice that the surrounding code in tcp_subr.c uses tabs for indentation. Should I still use space as the coding style mandates (and which makes checkpatch happy)?

Yes, the goal is to get rid of tabs.

OK. Thanks for clarifacion and review!

Patch updated.

 slirp/main.h     |    1 +
 slirp/slirp.c    |    3 +++
 slirp/tcp_subr.c |    7 +++++--
 3 files changed, 9 insertions(+), 2 deletions(-)
slirp/main.h +1 -0
index 028df4b..bf601e2 100644
@@ -31,6 +31,7 @@ extern char *exec_shell;
 extern u_int curtime;
 extern fd_set *global_readfds, *global_writefds, *global_xfds;
 extern struct in_addr loopback_addr;
+extern in_addr_t loopback_mask;
 extern char *username;
 extern char *socket_path;
 extern int towrite_max;
slirp/slirp.c +3 -0
index 90473eb..9787104 100644
@@ -29,6 +29,8 @@
 
 /* host loopback address */
 struct in_addr loopback_addr;
+/* host loopback network mask */
+in_addr_t loopback_mask;
 
 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
 static const uint8_t special_ethaddr[ETH_ALEN] = {
@@ -191,6 +193,7 @@ static void slirp_init_once(void)
 #endif
 
     loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
+    loopback_mask = htonl(IN_CLASSA_NET);
 }
 
 static void slirp_state_save(QEMUFile *f, void *opaque);
slirp/tcp_subr.c +0 -0
index 0a545c4..6b01f74 100644

slirp/tcp_subr.c tcp_connect(struct socket *inso) · line 442

Quoted 10 lines
slirp/tcp_subr.c +5 -2
@@ -435,8 +435,11 @@ tcp_connect(struct socket *inso)
 	so->so_fport = addr.sin_port;
 	so->so_faddr = addr.sin_addr;
 	/* Translate connections from localhost to the real hostname */
-	if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
-	   so->so_faddr = slirp->vhost_addr;
+    if (so->so_faddr.s_addr == 0 ||
+        (so->so_faddr.s_addr & loopback_mask) ==
+        (loopback_addr.s_addr & loopback_mask)) {
+        so->so_faddr = slirp->vhost_addr;
+    }

Dropping tabs is desired, but as long as slirp is such a mess coding style wise, I prefer keeping the indention depth consistent. I fixed this up while merging it.

Out of curiosity: Which host platform requires this? Linux always reports 127.0.0.1 as source, even when you bind the hostfwd rule to a different localhost address.

Jan

#6 lore raw
Quoted 79 lines · Jan Kiszka
Quoted 63 lines · Anders Waldenborg

Changes so translation of remote address to the host's ip address in the virtual network happens for all addresses in the 127.0.0.0/8 network, not just 127.0.0.1.

This fixes so that hostfwd bound to addresses such as 127.0.0.2 works.

---

Quoted 5 lines · Blue Swirl
Quoted 3 lines

Notice that the surrounding code in tcp_subr.c uses tabs for indentation. Should I still use space as the coding style mandates (and which makes checkpatch happy)?

Yes, the goal is to get rid of tabs.

OK. Thanks for clarifacion and review!

Patch updated.

 slirp/main.h     |    1 +
 slirp/slirp.c    |    3 +++
 slirp/tcp_subr.c |    7 +++++--
 3 files changed, 9 insertions(+), 2 deletions(-)
slirp/main.h +1 -0
index 028df4b..bf601e2 100644
@@ -31,6 +31,7 @@ extern char *exec_shell;
 extern u_int curtime;
 extern fd_set *global_readfds, *global_writefds, *global_xfds;
 extern struct in_addr loopback_addr;
+extern in_addr_t loopback_mask;
 extern char *username;
 extern char *socket_path;
 extern int towrite_max;
slirp/slirp.c +3 -0
index 90473eb..9787104 100644
@@ -29,6 +29,8 @@
 
 /* host loopback address */
 struct in_addr loopback_addr;
+/* host loopback network mask */
+in_addr_t loopback_mask;
 
 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
 static const uint8_t special_ethaddr[ETH_ALEN] = {
@@ -191,6 +193,7 @@ static void slirp_init_once(void)
 #endif
 
     loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
+    loopback_mask = htonl(IN_CLASSA_NET);
 }
 
 static void slirp_state_save(QEMUFile *f, void *opaque);
slirp/tcp_subr.c +0 -0
index 0a545c4..6b01f74 100644

slirp/tcp_subr.c tcp_connect(struct socket *inso) · line 442

Quoted 10 lines
slirp/tcp_subr.c +5 -2
@@ -435,8 +435,11 @@ tcp_connect(struct socket *inso)
 	so->so_fport = addr.sin_port;
 	so->so_faddr = addr.sin_addr;
 	/* Translate connections from localhost to the real hostname */
-	if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
-	   so->so_faddr = slirp->vhost_addr;
+    if (so->so_faddr.s_addr == 0 ||
+        (so->so_faddr.s_addr & loopback_mask) ==
+        (loopback_addr.s_addr & loopback_mask)) {
+        so->so_faddr = slirp->vhost_addr;
+    }

Dropping tabs is desired, but as long as slirp is such a mess coding style wise, I prefer keeping the indention depth consistent. I fixed this up while merging it.

Quoted 3 lines

Out of curiosity: Which host platform requires this? Linux always reports 127.0.0.1 as source, even when you bind the hostfwd rule to a different localhost address.

Ah, found the test case: binding the host-side initiator of the connection to some other localhost address provides a before-after effect.

Jan