Skip to contentSkip to conversation

slirp: Update forwarding IP address if guest receiver non-default IP

RFC PATCH In review Alexey Kardashevskiy 3 participants 5 messages Feb 1, 2018 – Mar 7, 2018

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 slirp: Update forwarding IP address if guest receiver non-default IP

If we run QEMU with -netdev user,id=USER0,hostfwd=tcp::2222-:22, it starts a DHCP server and starts allocating client IPs from 10.0.2.15 so this is what the guest normally receives. Since QEMU automatically adds the DHCP starting address into the forwarding table, everything works. This is the table before guest started:

(qemu) info usernet
VLAN -1 (USER0):

  Protocol[State]    FD  Source Address  Port   Dest. Address  Port RecvQ SendQ
  TCP[HOST_FORWARD]  11               *  2222       10.0.2.15    22     0     0

However if the guest happens to have DHCP lease (for example, 10.0.2.16), the forwarding stops working. The guest can still reach the outer world (which is expected).

This updates the forwarding table when QEMU confirms the requested IP to the guest.

---

Does this look any useful?

Sure I can remove /var/lib/dhcp/dhclient.enp0s1.leases in the guest or start QEMU with the DHCP start address equal to what the guest wants to reserve but it is quite confusing why such a simple config just does not work.

Found this with the brand new Ubuntu 17.10 which runs dhcp and something called "netplan" and the guest ends up with 2 IPs from 10.0.2.x network. After disabling netplan, the lease remains and it is not 10.0.2.15 but rather .16 or .17.

Comments? Thanks.

---
 slirp/libslirp.h |  2 ++
 slirp/bootp.c    |  2 ++
 slirp/slirp.c    | 27 +++++++++++++++++++++++++++
 3 files changed, 31 insertions(+)
slirp/libslirp.h +2 -0
index 540b3e5..6779081 100644
@@ -33,6 +33,8 @@ int slirp_add_hostfwd(Slirp *slirp, int is_udp,
                       struct in_addr guest_addr, int guest_port);
 int slirp_remove_hostfwd(Slirp *slirp, int is_udp,
                          struct in_addr host_addr, int host_port);
+void slirp_update_hostfwd(Slirp *slirp, struct in_addr old_guest_addr,
+                          struct in_addr new_guest_addr);
 int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
                    struct in_addr *guest_addr, int guest_port);
 
slirp/bootp.c +2 -0
index 5dd1a41..5876004 100644
@@ -225,6 +225,8 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     /* Update ARP table for this IP address */
     arp_table_add(slirp, daddr.sin_addr.s_addr, client_ethaddr);
 
+    slirp_update_hostfwd(slirp, slirp->vdhcp_startaddr, daddr.sin_addr);
+
     saddr.sin_addr = slirp->vhost_addr;
     saddr.sin_port = htons(BOOTP_SERVER);
 
slirp/slirp.c +27 -0
index 1cb6b07..a9d8a16 100644
@@ -1061,6 +1061,33 @@ int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
     return 0;
 }
 
+static void slirp_do_update_hostfwd(Slirp *slirp, struct socket *head,
+                                    struct in_addr old_guest_addr,
+                                    struct in_addr new_guest_addr)
+{
+    struct socket *so;
+    char oldaddr[17], newaddr[17];
+
+    for (so = head->so_next; so != head; so = so->so_next) {
+        if ((so->so_state & SS_HOSTFWD) &&
+            so->lhost.sin.sin_addr.s_addr == old_guest_addr.s_addr) {
+            strncpy(oldaddr, inet_ntoa(old_guest_addr), sizeof(oldaddr) - 1);
+            strncpy(newaddr, inet_ntoa(new_guest_addr), sizeof(newaddr) - 1);
+            DEBUG_ARGS((dfd, "Updating forwarding from %s:%d to %s:%d\n",
+                       oldaddr, ntohs(so->lhost.sin.sin_port),
+                       newaddr, ntohs(so->lhost.sin.sin_port)));
+            so->lhost.sin.sin_addr = new_guest_addr;
+        }
+    }
+}
+
+void slirp_update_hostfwd(Slirp *slirp, struct in_addr old_guest_addr,
+                          struct in_addr new_guest_addr)
+{
+    slirp_do_update_hostfwd(slirp, &slirp->udb, old_guest_addr, new_guest_addr);
+    slirp_do_update_hostfwd(slirp, &slirp->tcb, old_guest_addr, new_guest_addr);
+}
+
 int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
                    struct in_addr *guest_addr, int guest_port)
 {
Signature · 1 line
2.11.0
#1 lore raw
Quoted 15 lines · Alexey Kardashevskiy

If we run QEMU with -netdev user,id=USER0,hostfwd=tcp::2222-:22, it starts a DHCP server and starts allocating client IPs from 10.0.2.15 so this is what the guest normally receives. Since QEMU automatically adds the DHCP starting address into the forwarding table, everything works. This is the table before guest started:

(qemu) info usernet
VLAN -1 (USER0):

  Protocol[State]    FD  Source Address  Port   Dest. Address  Port RecvQ SendQ
  TCP[HOST_FORWARD]  11               *  2222       10.0.2.15    22     0     0

However if the guest happens to have DHCP lease (for example, 10.0.2.16), the forwarding stops working. The guest can still reach the outer world (which is expected).

Quoted 7 lines

This updates the forwarding table when QEMU confirms the requested IP to the guest.

---

Does this look any useful?

It does not seem like it does very much but .... :)

Quoted 84 lines

Sure I can remove /var/lib/dhcp/dhclient.enp0s1.leases in the guest or start QEMU with the DHCP start address equal to what the guest wants to reserve but it is quite confusing why such a simple config just does not work.

Found this with the brand new Ubuntu 17.10 which runs dhcp and something called "netplan" and the guest ends up with 2 IPs from 10.0.2.x network. After disabling netplan, the lease remains and it is not 10.0.2.15 but rather .16 or .17.

Comments? Thanks.

---
 slirp/libslirp.h |  2 ++
 slirp/bootp.c    |  2 ++
 slirp/slirp.c    | 27 +++++++++++++++++++++++++++
 3 files changed, 31 insertions(+)
slirp/libslirp.h +2 -0
index 540b3e5..6779081 100644
@@ -33,6 +33,8 @@ int slirp_add_hostfwd(Slirp *slirp, int is_udp,
                       struct in_addr guest_addr, int guest_port);
 int slirp_remove_hostfwd(Slirp *slirp, int is_udp,
                          struct in_addr host_addr, int host_port);
+void slirp_update_hostfwd(Slirp *slirp, struct in_addr old_guest_addr,
+                          struct in_addr new_guest_addr);
 int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
                    struct in_addr *guest_addr, int guest_port);
 
slirp/bootp.c +2 -0
index 5dd1a41..5876004 100644
@@ -225,6 +225,8 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     /* Update ARP table for this IP address */
     arp_table_add(slirp, daddr.sin_addr.s_addr, client_ethaddr);
 
+    slirp_update_hostfwd(slirp, slirp->vdhcp_startaddr, daddr.sin_addr);
+
     saddr.sin_addr = slirp->vhost_addr;
     saddr.sin_port = htons(BOOTP_SERVER);
 
slirp/slirp.c +27 -0
index 1cb6b07..a9d8a16 100644
@@ -1061,6 +1061,33 @@ int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
     return 0;
 }
 
+static void slirp_do_update_hostfwd(Slirp *slirp, struct socket *head,
+                                    struct in_addr old_guest_addr,
+                                    struct in_addr new_guest_addr)
+{
+    struct socket *so;
+    char oldaddr[17], newaddr[17];
+
+    for (so = head->so_next; so != head; so = so->so_next) {
+        if ((so->so_state & SS_HOSTFWD) &&
+            so->lhost.sin.sin_addr.s_addr == old_guest_addr.s_addr) {
+            strncpy(oldaddr, inet_ntoa(old_guest_addr), sizeof(oldaddr) - 1);
+            strncpy(newaddr, inet_ntoa(new_guest_addr), sizeof(newaddr) - 1);
+            DEBUG_ARGS((dfd, "Updating forwarding from %s:%d to %s:%d\n",
+                       oldaddr, ntohs(so->lhost.sin.sin_port),
+                       newaddr, ntohs(so->lhost.sin.sin_port)));
+            so->lhost.sin.sin_addr = new_guest_addr;
+        }
+    }
+}
+
+void slirp_update_hostfwd(Slirp *slirp, struct in_addr old_guest_addr,
+                          struct in_addr new_guest_addr)
+{
+    slirp_do_update_hostfwd(slirp, &slirp->udb, old_guest_addr, new_guest_addr);
+    slirp_do_update_hostfwd(slirp, &slirp->tcb, old_guest_addr, new_guest_addr);
+}
+
 int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
                    struct in_addr *guest_addr, int guest_port)
 {
Signature · 1 line
Alexey
#2 lore raw
Quoted 16 lines · Alexey Kardashevskiy
Quoted 15 lines · Alexey Kardashevskiy

If we run QEMU with -netdev user,id=USER0,hostfwd=tcp::2222-:22, it starts a DHCP server and starts allocating client IPs from 10.0.2.15 so this is what the guest normally receives. Since QEMU automatically adds the DHCP starting address into the forwarding table, everything works. This is the table before guest started:

(qemu) info usernet
VLAN -1 (USER0):

  Protocol[State]    FD  Source Address  Port   Dest. Address  Port RecvQ SendQ
  TCP[HOST_FORWARD]  11               *  2222       10.0.2.15    22     0     0

However if the guest happens to have DHCP lease (for example, 10.0.2.16), the forwarding stops working. The guest can still reach the outer world (which is expected).

Quoted 7 lines
Quoted 7 lines

This updates the forwarding table when QEMU confirms the requested IP to the guest.

---

Does this look any useful?

Ping, anyone?

Quoted 91 lines

It does not seem like it does very much but .... :)

Quoted 84 lines

Sure I can remove /var/lib/dhcp/dhclient.enp0s1.leases in the guest or start QEMU with the DHCP start address equal to what the guest wants to reserve but it is quite confusing why such a simple config just does not work.

Found this with the brand new Ubuntu 17.10 which runs dhcp and something called "netplan" and the guest ends up with 2 IPs from 10.0.2.x network. After disabling netplan, the lease remains and it is not 10.0.2.15 but rather .16 or .17.

Comments? Thanks.

---
 slirp/libslirp.h |  2 ++
 slirp/bootp.c    |  2 ++
 slirp/slirp.c    | 27 +++++++++++++++++++++++++++
 3 files changed, 31 insertions(+)
slirp/libslirp.h +2 -0
index 540b3e5..6779081 100644
@@ -33,6 +33,8 @@ int slirp_add_hostfwd(Slirp *slirp, int is_udp,
                       struct in_addr guest_addr, int guest_port);
 int slirp_remove_hostfwd(Slirp *slirp, int is_udp,
                          struct in_addr host_addr, int host_port);
+void slirp_update_hostfwd(Slirp *slirp, struct in_addr old_guest_addr,
+                          struct in_addr new_guest_addr);
 int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
                    struct in_addr *guest_addr, int guest_port);
 
slirp/bootp.c +2 -0
index 5dd1a41..5876004 100644
@@ -225,6 +225,8 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     /* Update ARP table for this IP address */
     arp_table_add(slirp, daddr.sin_addr.s_addr, client_ethaddr);
 
+    slirp_update_hostfwd(slirp, slirp->vdhcp_startaddr, daddr.sin_addr);
+
     saddr.sin_addr = slirp->vhost_addr;
     saddr.sin_port = htons(BOOTP_SERVER);
 
slirp/slirp.c +27 -0
index 1cb6b07..a9d8a16 100644
@@ -1061,6 +1061,33 @@ int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
     return 0;
 }
 
+static void slirp_do_update_hostfwd(Slirp *slirp, struct socket *head,
+                                    struct in_addr old_guest_addr,
+                                    struct in_addr new_guest_addr)
+{
+    struct socket *so;
+    char oldaddr[17], newaddr[17];
+
+    for (so = head->so_next; so != head; so = so->so_next) {
+        if ((so->so_state & SS_HOSTFWD) &&
+            so->lhost.sin.sin_addr.s_addr == old_guest_addr.s_addr) {
+            strncpy(oldaddr, inet_ntoa(old_guest_addr), sizeof(oldaddr) - 1);
+            strncpy(newaddr, inet_ntoa(new_guest_addr), sizeof(newaddr) - 1);
+            DEBUG_ARGS((dfd, "Updating forwarding from %s:%d to %s:%d\n",
+                       oldaddr, ntohs(so->lhost.sin.sin_port),
+                       newaddr, ntohs(so->lhost.sin.sin_port)));
+            so->lhost.sin.sin_addr = new_guest_addr;
+        }
+    }
+}
+
+void slirp_update_hostfwd(Slirp *slirp, struct in_addr old_guest_addr,
+                          struct in_addr new_guest_addr)
+{
+    slirp_do_update_hostfwd(slirp, &slirp->udb, old_guest_addr, new_guest_addr);
+    slirp_do_update_hostfwd(slirp, &slirp->tcb, old_guest_addr, new_guest_addr);
+}
+
 int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
                    struct in_addr *guest_addr, int guest_port)
 {
Signature · 1 line
Alexey
#3 lore raw
Quoted 20 lines · Alexey Kardashevskiy
Quoted 19 lines · Alexey Kardashevskiy
Quoted 18 lines · Alexey Kardashevskiy

If we run QEMU with -netdev user,id=USER0,hostfwd=tcp::2222-:22, it starts a DHCP server and starts allocating client IPs from 10.0.2.15 so this is what the guest normally receives. Since QEMU automatically adds the DHCP starting address into the forwarding table, everything works. This is the table before guest started:

(qemu) info usernet
VLAN -1 (USER0):

  Protocol[State]    FD  Source Address  Port   Dest. Address  Port RecvQ SendQ
  TCP[HOST_FORWARD]  11               *  2222       10.0.2.15    22     0     0

However if the guest happens to have DHCP lease (for example, 10.0.2.16), the forwarding stops working. The guest can still reach the outer world (which is expected).

This updates the forwarding table when QEMU confirms the requested IP to the guest.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

---

Does this look any useful?

Ping, anyone?

Maybe you should make sure to put the SLIRP maintainer on CC: ?

Quoted 93 lines
Quoted 91 lines

It does not seem like it does very much but .... :)

Quoted 84 lines

Sure I can remove /var/lib/dhcp/dhclient.enp0s1.leases in the guest or start QEMU with the DHCP start address equal to what the guest wants to reserve but it is quite confusing why such a simple config just does not work.

Found this with the brand new Ubuntu 17.10 which runs dhcp and something called "netplan" and the guest ends up with 2 IPs from 10.0.2.x network. After disabling netplan, the lease remains and it is not 10.0.2.15 but rather .16 or .17.

Comments? Thanks.

---
 slirp/libslirp.h |  2 ++
 slirp/bootp.c    |  2 ++
 slirp/slirp.c    | 27 +++++++++++++++++++++++++++
 3 files changed, 31 insertions(+)
slirp/libslirp.h +2 -0
index 540b3e5..6779081 100644
@@ -33,6 +33,8 @@ int slirp_add_hostfwd(Slirp *slirp, int is_udp,
                       struct in_addr guest_addr, int guest_port);
 int slirp_remove_hostfwd(Slirp *slirp, int is_udp,
                          struct in_addr host_addr, int host_port);
+void slirp_update_hostfwd(Slirp *slirp, struct in_addr old_guest_addr,
+                          struct in_addr new_guest_addr);
 int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
                    struct in_addr *guest_addr, int guest_port);
 
slirp/bootp.c +2 -0
index 5dd1a41..5876004 100644
@@ -225,6 +225,8 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     /* Update ARP table for this IP address */
     arp_table_add(slirp, daddr.sin_addr.s_addr, client_ethaddr);
 
+    slirp_update_hostfwd(slirp, slirp->vdhcp_startaddr, daddr.sin_addr);
+
     saddr.sin_addr = slirp->vhost_addr;
     saddr.sin_port = htons(BOOTP_SERVER);
 
slirp/slirp.c +27 -0
index 1cb6b07..a9d8a16 100644
@@ -1061,6 +1061,33 @@ int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
     return 0;
 }
 
+static void slirp_do_update_hostfwd(Slirp *slirp, struct socket *head,
+                                    struct in_addr old_guest_addr,
+                                    struct in_addr new_guest_addr)
+{
+    struct socket *so;
+    char oldaddr[17], newaddr[17];
+
+    for (so = head->so_next; so != head; so = so->so_next) {
+        if ((so->so_state & SS_HOSTFWD) &&
+            so->lhost.sin.sin_addr.s_addr == old_guest_addr.s_addr) {
+            strncpy(oldaddr, inet_ntoa(old_guest_addr), sizeof(oldaddr) - 1);
+            strncpy(newaddr, inet_ntoa(new_guest_addr), sizeof(newaddr) - 1);
+            DEBUG_ARGS((dfd, "Updating forwarding from %s:%d to %s:%d\n",
+                       oldaddr, ntohs(so->lhost.sin.sin_port),
+                       newaddr, ntohs(so->lhost.sin.sin_port)));
+            so->lhost.sin.sin_addr = new_guest_addr;
+        }
+    }
+}
+
+void slirp_update_hostfwd(Slirp *slirp, struct in_addr old_guest_addr,
+                          struct in_addr new_guest_addr)
+{
+    slirp_do_update_hostfwd(slirp, &slirp->udb, old_guest_addr, new_guest_addr);
+    slirp_do_update_hostfwd(slirp, &slirp->tcb, old_guest_addr, new_guest_addr);
+}
+
 int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
                    struct in_addr *guest_addr, int guest_port)
 {
#4 lore raw

Hello,

Quoted 21 lines · Thomas Huth, on mer. 07 mars 2018 07:30:29 +0100, wrote:
Quoted 20 lines · Alexey Kardashevskiy
Quoted 19 lines · Alexey Kardashevskiy
Quoted 18 lines · Alexey Kardashevskiy

If we run QEMU with -netdev user,id=USER0,hostfwd=tcp::2222-:22, it starts a DHCP server and starts allocating client IPs from 10.0.2.15 so this is what the guest normally receives. Since QEMU automatically adds the DHCP starting address into the forwarding table, everything works. This is the table before guest started:

(qemu) info usernet
VLAN -1 (USER0):

  Protocol[State]    FD  Source Address  Port   Dest. Address  Port RecvQ SendQ
  TCP[HOST_FORWARD]  11               *  2222       10.0.2.15    22     0     0

However if the guest happens to have DHCP lease (for example, 10.0.2.16), the forwarding stops working. The guest can still reach the outer world (which is expected).

This updates the forwarding table when QEMU confirms the requested IP to the guest.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

---

Does this look any useful?

Ping, anyone?

Maybe you should make sure to put the SLIRP maintainer on CC: ?

That would work much better to catch my attention indeed :)

I'm afraid this will be a nack. What you basically propose is "the last DHCP lease wins". There can be setups where it is expected that it's the first DHCP lease which should get the forward, as documented actually ("If guestaddr is not specified, its value is x.x.x.15"). In your case, you can always set to hostfwd=tcp::2222-10.0.2.16:22. If your guest doesn't have predictable DHCP behavior, better use a static IP assignment rather than introducing into qemu something which looks rather undefined to me ("last DHCP lease wins").

Samuel