Skip to contentSkip to conversation

User Networking: Enable removal of redirections

PATCH In review Alexander Graf 3 participants 10 messages May 26, 2009 – May 29, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 User Networking: Enable removal of redirections

Using the new host_net_redir command you can easily create redirections on the fly while your VM is running.

While that's great, it's missing the removal of redirections, in case you want to have a port closed again at a later point in time.

This patch adds support for removal of redirections.

---
 monitor.c        |    5 +++--
 net.c            |   43 ++++++++++++++++++++++++++++++++++++++++++-
 net.h            |    2 +-
 slirp/libslirp.h |    1 +
 slirp/slirp.c    |   23 +++++++++++++++++++++++
 vl.c             |    2 +-
 6 files changed, 71 insertions(+), 5 deletions(-)
monitor.c +3 -2
index 0f38c71..dbab3de 100644
@@ -1759,8 +1759,9 @@ static const mon_cmd_t mon_cmds[] = {
     { "host_net_remove", "is", net_host_device_remove,
       "vlan_id name", "remove host VLAN client" },
 #ifdef CONFIG_SLIRP
-    { "host_net_redir", "s", net_slirp_redir,
-      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)" },
+    { "host_net_redir", "ss?", net_slirp_redir,
+      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)\n"
+      "host_net_redir remove [tcp:|udp:]host-port -- remove redirection" },
 #endif
     { "balloon", "i", do_balloon,
       "target", "request VM to change it's memory allocation (in MB)" },
net.c +42 -1

55 lines not on this page · raw message

net.h +1 -1
index 41a3082..feee021 100644
@@ -112,7 +112,7 @@ int net_client_init(const char *device, const char *p);
 void net_client_uninit(NICInfo *nd);
 int net_client_parse(const char *str);
 void net_slirp_smb(const char *exported_dir);
-void net_slirp_redir(Monitor *mon, const char *redir_str);
+void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2);
 void net_cleanup(void);
 int slirp_is_inited(void);
 void net_client_check(void);
slirp/libslirp.h +1 -0
index a1cd70e..6fc2c32 100644
@@ -18,6 +18,7 @@ void slirp_input(const uint8_t *pkt, int pkt_len);
 int slirp_can_output(void);
 void slirp_output(const uint8_t *pkt, int pkt_len);
 
+int slirp_redir_rm(int is_udp, int host_port);
 int slirp_redir(int is_udp, int host_port,
                 struct in_addr guest_addr, int guest_port);
 int slirp_add_exec(int do_pty, const void *args, int addr_low_byte,
slirp/slirp.c +23 -0
index 04d3ded..33397c0 100644
@@ -734,6 +734,29 @@ void if_encap(const uint8_t *ip_data, int ip_data_len)
     }
 }
 
+/* Unlistens a redirection
+ *
+ * Return value: number of redirs removed */
+int slirp_redir_rm(int is_udp, int host_port)
+{
+    struct socket *so;
+    struct socket *head = (is_udp ? &udb : &tcb);
+    int fport = htons(host_port);
+    int n = 0;
+
+ loop_again:
+    for (so = head->so_next; so != head; so = so->so_next) {
+        if (so->so_fport == fport) {
+            close(so->s);
+            sofree(so);
+            n++;
+            goto loop_again;
+        }
+    }
+
+    return n;
+}
+
 int slirp_redir(int is_udp, int host_port,
                 struct in_addr guest_addr, int guest_port)
 {
vl.c +1 -1
index 2c1f0e0..25dfdd7 100644
@@ -5153,7 +5153,7 @@ int main(int argc, char **argv, char **envp)
                 break;
 #endif
             case QEMU_OPTION_redir:
-                net_slirp_redir(NULL, optarg);
+                net_slirp_redir(NULL, optarg, NULL);
                 break;
 #endif
             case QEMU_OPTION_bt:
Signature · 1 line
1.6.0.2
#1 lore raw

PATCH 1/1 User networking: Show active connections

In case you're wondering what connections exactly you have open or maybe redir'ed in the past, you can't really find out from qemu right now.

This patch enables you to see all current connections the host only networking holds open, so you can kill them using the previous patch.

---
 monitor.c        |    3 ++-
 net.c            |   44 ++++++++++++++++++++++++++++++++++++++++++++
 slirp/libslirp.h |    4 ++++
 slirp/slirp.c    |   24 ++++++++++++++++++++++++
 4 files changed, 74 insertions(+), 1 deletions(-)
monitor.c +2 -1
index dbab3de..443f6d4 100644
@@ -1761,7 +1761,8 @@ static const mon_cmd_t mon_cmds[] = {
 #ifdef CONFIG_SLIRP
     { "host_net_redir", "ss?", net_slirp_redir,
       "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)\n"
-      "host_net_redir remove [tcp:|udp:]host-port -- remove redirection" },
+      "host_net_redir remove [tcp:|udp:]host-port -- remove redirection\n"
+      "host_net_redir list -- show all redirections" },
 #endif
     { "balloon", "i", do_balloon,
       "target", "request VM to change it's memory allocation (in MB)" },
net.c +44 -0

56 lines not on this page · raw message

slirp/libslirp.h +4 -0
index 6fc2c32..b2313b4 100644
@@ -18,6 +18,10 @@ void slirp_input(const uint8_t *pkt, int pkt_len);
 int slirp_can_output(void);
 void slirp_output(const uint8_t *pkt, int pkt_len);
 
+void slirp_redir_loop(void (*func)(void *opaque, int is_udp,
+                                  struct in_addr *laddr, u_int lport,              
+                                  struct in_addr *faddr, u_int fport),
+                     void *opaque);
 int slirp_redir_rm(int is_udp, int host_port);
 int slirp_redir(int is_udp, int host_port,
                 struct in_addr guest_addr, int guest_port);
slirp/slirp.c +24 -0
index 33397c0..9cab731 100644
@@ -734,6 +734,30 @@ void if_encap(const uint8_t *ip_data, int ip_data_len)
     }
 }
 
+static void _slirp_redir_loop(void (*func)(void *opaque, int is_udp,
+                                           struct in_addr *laddr, u_int lport,
+                                           struct in_addr *faddr, u_int fport),
+                              void *opaque, int is_udp)
+{
+    struct socket *head = (is_udp ? &udb : &tcb);
+    struct socket *so;
+
+    for (so = head->so_next; so != head; so = so->so_next) {
+        func(opaque, is_udp,
+             &so->so_laddr, ntohs(so->so_lport),
+             &so->so_faddr, ntohs(so->so_fport));
+    }
+}
+
+void slirp_redir_loop(void (*func)(void *opaque, int is_udp,
+                                  struct in_addr *laddr, u_int lport,
+                                  struct in_addr *faddr, u_int fport),
+                     void *opaque)
+{
+    _slirp_redir_loop(func, opaque, 0);
+    _slirp_redir_loop(func, opaque, 1);
+}
+
 /* Unlistens a redirection
  *
  * Return value: number of redirs removed */
Signature · 1 line
1.6.0.2
#2 lore raw

[Qemu-devel] Re: [PATCH] User networking: Show active connections

Quoted 21 lines · Alexander Graf

In case you're wondering what connections exactly you have open or maybe redir'ed in the past, you can't really find out from qemu right now.

This patch enables you to see all current connections the host only networking holds open, so you can kill them using the previous patch.

---
 monitor.c        |    3 ++-
 net.c            |   44 ++++++++++++++++++++++++++++++++++++++++++++
 slirp/libslirp.h |    4 ++++
 slirp/slirp.c    |   24 ++++++++++++++++++++++++
 4 files changed, 74 insertions(+), 1 deletions(-)
monitor.c +0 -0
index dbab3de..443f6d4 100644

monitor.c static const mon_cmd_t mon_cmds[] = { · line 1765

Quoted 6 lines
monitor.c +2 -1
@@ -1761,7 +1761,8 @@ static const mon_cmd_t mon_cmds[] = {
 #ifdef CONFIG_SLIRP
     { "host_net_redir", "ss?", net_slirp_redir,
       "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)\n"
-      "host_net_redir remove [tcp:|udp:]host-port -- remove redirection" },
+      "host_net_redir remove [tcp:|udp:]host-port -- remove redirection\n"
+      "host_net_redir list -- show all redirections" },

info redir (or using the new term: info hostfwd)? Would look a bit more consistent.

Jan

#3 lore raw
In reply to Alexander Graf
Quoted 7 lines · Alexander Graf

Using the new host_net_redir command you can easily create redirections on the fly while your VM is running.

While that's great, it's missing the removal of redirections, in case you want to have a port closed again at a later point in time.

This patch adds support for removal of redirections.

Cool. I was just too lazy to hack on slirp for this so far, but now you did it.

Quoted 16 lines
---
 monitor.c        |    5 +++--
 net.c            |   43 ++++++++++++++++++++++++++++++++++++++++++-
 net.h            |    2 +-
 slirp/libslirp.h |    1 +
 slirp/slirp.c    |   23 +++++++++++++++++++++++
 vl.c             |    2 +-
 6 files changed, 71 insertions(+), 5 deletions(-)
monitor.c +0 -0
index 0f38c71..dbab3de 100644

monitor.c static const mon_cmd_t mon_cmds[] = { · line 1763

Quoted 7 lines
monitor.c +2 -2
@@ -1759,8 +1759,9 @@ static const mon_cmd_t mon_cmds[] = {
     { "host_net_remove", "is", net_host_device_remove,
       "vlan_id name", "remove host VLAN client" },
 #ifdef CONFIG_SLIRP
-    { "host_net_redir", "s", net_slirp_redir,
-      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)" },
+    { "host_net_redir", "ss?", net_slirp_redir,
+      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)\n"

If going for a single command, this should rather look like this:

"[tcp|udp]:host-port:[guest-host]:guest-port |
remove [tcp|udp]:host-port"

BTW, I didn't refactor this interface along with the command line changes, but I should actually. To remain consistent, we should call it host_net_fwd (-redir will be replaced with hostfwd=<rule>) or even take the chance and split it into host_net_fwd_add and host_net_fwd_remove.

monitor.c static const mon_cmd_t mon_cmds[] = { · line 1767

Quoted 4 lines
monitor.c +1 -0
+      "host_net_redir remove [tcp:|udp:]host-port -- remove redirection" },
 #endif
     { "balloon", "i", do_balloon,
       "target", "request VM to change it's memory allocation (in MB)" },

Jan

#4 lore raw
Quoted 37 lines · Jan Kiszka
Quoted 7 lines · Alexander Graf

Using the new host_net_redir command you can easily create redirections on the fly while your VM is running.

While that's great, it's missing the removal of redirections, in case you want to have a port closed again at a later point in time.

This patch adds support for removal of redirections.

Cool. I was just too lazy to hack on slirp for this so far, but now you did it.

Quoted 15 lines
---
 monitor.c        |    5 +++--
 net.c            |   43 ++++++++++++++++++++++++++++++++++++++++++-
 net.h            |    2 +-
 slirp/libslirp.h |    1 +
 slirp/slirp.c    |   23 +++++++++++++++++++++++
 vl.c             |    2 +-
 6 files changed, 71 insertions(+), 5 deletions(-)
monitor.c +0 -0
index 0f38c71..dbab3de 100644

monitor.c static const mon_cmd_t mon_cmds[] = { · line 1763

Quoted 7 lines
monitor.c +2 -2
@@ -1759,8 +1759,9 @@ static const mon_cmd_t mon_cmds[] = {
     { "host_net_remove", "is", net_host_device_remove,
       "vlan_id name", "remove host VLAN client" },
 #ifdef CONFIG_SLIRP
-    { "host_net_redir", "s", net_slirp_redir,
-      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)" },
+    { "host_net_redir", "ss?", net_slirp_redir,
+      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)\n"

If going for a single command, this should rather look like this:

Quoted 12 lines

"[tcp|udp]:host-port:[guest-host]:guest-port |
remove [tcp|udp]:host-port"

BTW, I didn't refactor this interface along with the command line changes, but I should actually. To remain consistent, we should call it host_net_fwd (-redir will be replaced with hostfwd=<rule>) or even take the chance and split it into host_net_fwd_add and host_net_fwd_remove.

Quoted 4 lines
monitor.c +1 -0
+      "host_net_redir remove [tcp:|udp:]host-port -- remove redirection" },
 #endif
     { "balloon", "i", do_balloon,
       "target", "request VM to change it's memory allocation (in MB)" },

Before I forget: You also have to update to monitor-related documentation when adding/changing commands.

Jan

#5 lore raw
Quoted 55 lines · Jan Kiszka
Quoted 39 lines · Jan Kiszka
Quoted 7 lines · Alexander Graf

Using the new host_net_redir command you can easily create redirections on the fly while your VM is running.

While that's great, it's missing the removal of redirections, in case you want to have a port closed again at a later point in time.

This patch adds support for removal of redirections.

Cool. I was just too lazy to hack on slirp for this so far, but now you did it.

Quoted 23 lines

---

monitor.c        |    5 +++--
net.c            |   43 ++++++++++++++++++++++++++++++++++++++++++-
net.h            |    2 +-
slirp/libslirp.h |    1 +
slirp/slirp.c    |   23 +++++++++++++++++++++++
vl.c             |    2 +-
6 files changed, 71 insertions(+), 5 deletions(-)
monitor.c +2 -2
index 0f38c71..dbab3de 100644
@@ -1759,8 +1759,9 @@ static const mon_cmd_t mon_cmds[] = {
    { "host_net_remove", "is", net_host_device_remove,
      "vlan_id name", "remove host VLAN client" },
 #ifdef CONFIG_SLIRP
-    { "host_net_redir", "s", net_slirp_redir,
-      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect  
 TCP or UDP connections from host to guest (requires -net user)" },
+    { "host_net_redir", "ss?", net_slirp_redir,
+      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect  
Quoted 1 line

TCP or UDP connections from host to guest (requires -net user)\n"

If going for a single command, this should rather look like this:

Quoted 14 lines

"[tcp|udp]:host-port:[guest-host]:guest-port |
remove [tcp|udp]:host-port"

BTW, I didn't refactor this interface along with the command line changes, but I should actually. To remain consistent, we should call it host_net_fwd (-redir will be replaced with hostfwd=<rule>) or even take the chance and split it into host_net_fwd_add and host_net_fwd_remove.

Quoted 6 lines

+ "host_net_redir remove [tcp:|udp:]host-port -- remove
redirection" },
#endif
{ "balloon", "i", do_balloon,
"target", "request VM to change it's memory allocation (in
MB)" },

Quoted 2 lines

Before I forget: You also have to update to monitor-related documentation when adding/changing commands.

What monitor-related documentation? I did a grep -R host_net_redir qemu and didn't find any other occurence of that command :o.

Alex

#6 lore raw
Quoted 59 lines · Alexander Graf
Quoted 56 lines · Jan Kiszka
Quoted 54 lines · Jan Kiszka
Quoted 8 lines · Alexander Graf

Using the new host_net_redir command you can easily create redirections on the fly while your VM is running.

While that's great, it's missing the removal of redirections, in case you
want to have a port closed again at a later point in time.

This patch adds support for removal of redirections.

Cool. I was just too lazy to hack on slirp for this so far, but now you did it.

Quoted 23 lines

---

monitor.c        |    5 +++--
net.c            |   43 ++++++++++++++++++++++++++++++++++++++++++-
net.h            |    2 +-
slirp/libslirp.h |    1 +
slirp/slirp.c    |   23 +++++++++++++++++++++++
vl.c             |    2 +-
6 files changed, 71 insertions(+), 5 deletions(-)
monitor.c +2 -2
index 0f38c71..dbab3de 100644
@@ -1759,8 +1759,9 @@ static const mon_cmd_t mon_cmds[] = {
    { "host_net_remove", "is", net_host_device_remove,
      "vlan_id name", "remove host VLAN client" },
 #ifdef CONFIG_SLIRP
-    { "host_net_redir", "s", net_slirp_redir,
-      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP
 or UDP connections from host to guest (requires -net user)" },
+    { "host_net_redir", "ss?", net_slirp_redir,
+      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP
Quoted 1 line

or UDP connections from host to guest (requires -net user)\n"

If going for a single command, this should rather look like this:

"[tcp|udp]:host-port:[guest-host]:guest-port |
remove [tcp|udp]:host-port"

BTW, I didn't refactor this interface along with the command line changes, but I should actually. To remain consistent, we should call it host_net_fwd (-redir will be replaced with hostfwd=<rule>) or even take the chance and split it into host_net_fwd_add and host_net_fwd_remove.

Quoted 6 lines

+ "host_net_redir remove [tcp:|udp:]host-port -- remove
redirection" },
#endif
{ "balloon", "i", do_balloon,
"target", "request VM to change it's memory allocation (in
MB)" },

Quoted 5 lines
Quoted 2 lines

Before I forget: You also have to update to monitor-related documentation when adding/changing commands.

What monitor-related documentation? I did a grep -R host_net_redir qemu and didn't find any other occurence of that command :o.

Yeah, I also failed in the discipline of properly updating qemu-doc.texi when adding host_net_redir. :->

I think Blueswirl recently posted patch to fold monitor options and documentations into one file just like we already have for the command line. Would be helpful if that get merged soon

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#7 lore raw
In reply to Alexander Graf
Quoted 137 lines · Alexander Graf

Using the new host_net_redir command you can easily create redirections on the fly while your VM is running.

While that's great, it's missing the removal of redirections, in case you want to have a port closed again at a later point in time.

This patch adds support for removal of redirections.

---
 monitor.c        |    5 +++--
 net.c            |   43 ++++++++++++++++++++++++++++++++++++++++++-
 net.h            |    2 +-
 slirp/libslirp.h |    1 +
 slirp/slirp.c    |   23 +++++++++++++++++++++++
 vl.c             |    2 +-
 6 files changed, 71 insertions(+), 5 deletions(-)
monitor.c +3 -2
index 0f38c71..dbab3de 100644
@@ -1759,8 +1759,9 @@ static const mon_cmd_t mon_cmds[] = {
     { "host_net_remove", "is", net_host_device_remove,
       "vlan_id name", "remove host VLAN client" },
 #ifdef CONFIG_SLIRP
-    { "host_net_redir", "s", net_slirp_redir,
-      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)" },
+    { "host_net_redir", "ss?", net_slirp_redir,
+      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)\n"
+      "host_net_redir remove [tcp:|udp:]host-port -- remove redirection" },
 #endif
     { "balloon", "i", do_balloon,
       "target", "request VM to change it's memory allocation (in MB)" },
net.c +42 -1
index 31ee95a..de019b3 100644
@@ -568,7 +568,43 @@ static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
     return 0;
 }
 
-void net_slirp_redir(Monitor *mon, const char *redir_str)
+static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
+{
+    int host_port;
+    char buf[256] = "";
+    const char *p = port_str;
+    int is_udp = 0;
+    int n;
+
+    if (!mon)
+        return;
+
+    if (!port_str || !port_str[0])
+        goto fail_syntax;
+
+    get_str_sep(buf, sizeof(buf), &p, ':');
+
+    if (!strcmp(buf, "tcp") || buf[0] == '\0') {
+        is_udp = 0;
+    } else if (!strcmp(buf, "udp")) {
+        is_udp = 1;
+    } else {
+        goto fail_syntax;
+    }
+
+    host_port = atoi(p);
+
+    n = slirp_redir_rm(is_udp, host_port);
+
+    monitor_printf(mon, "removed %d redirections to %s port %d\n", n,
+                        is_udp ? "udp" : "tcp", host_port);
+    return;
+
+ fail_syntax:
+    monitor_printf(mon, "invalid format\n");
+}
+
+void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2)
 {
     int is_udp;
     char buf[256], *r;
@@ -581,6 +617,11 @@ void net_slirp_redir(Monitor *mon, const char *redir_str)
         slirp_init(slirp_restrict, slirp_ip);
     }
 
+    if (!strcmp(redir_str, "remove")) {
+        net_slirp_redir_rm(mon, redir_opt2);
+        return;
+    }
+
     p = redir_str;
     if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
         goto fail_syntax;
net.h +1 -1
index 41a3082..feee021 100644
@@ -112,7 +112,7 @@ int net_client_init(const char *device, const char *p);
 void net_client_uninit(NICInfo *nd);
 int net_client_parse(const char *str);
 void net_slirp_smb(const char *exported_dir);
-void net_slirp_redir(Monitor *mon, const char *redir_str);
+void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2);
 void net_cleanup(void);
 int slirp_is_inited(void);
 void net_client_check(void);
slirp/libslirp.h +1 -0
index a1cd70e..6fc2c32 100644
@@ -18,6 +18,7 @@ void slirp_input(const uint8_t *pkt, int pkt_len);
 int slirp_can_output(void);
 void slirp_output(const uint8_t *pkt, int pkt_len);
 
+int slirp_redir_rm(int is_udp, int host_port);
 int slirp_redir(int is_udp, int host_port,
                 struct in_addr guest_addr, int guest_port);
 int slirp_add_exec(int do_pty, const void *args, int addr_low_byte,
slirp/slirp.c +9 -0
index 04d3ded..33397c0 100644
@@ -734,6 +734,29 @@ void if_encap(const uint8_t *ip_data, int ip_data_len)
     }
 }
 
+/* Unlistens a redirection
+ *
+ * Return value: number of redirs removed */
+int slirp_redir_rm(int is_udp, int host_port)
+{
+    struct socket *so;
+    struct socket *head = (is_udp ? &udb : &tcb);
+    int fport = htons(host_port);
+    int n = 0;

slirp/slirp.c void if_encap(const uint8_t *ip_data, int ip_data_len) · line 755

Quoted 10 lines
slirp/slirp.c +10 -0
+
+ loop_again:
+    for (so = head->so_next; so != head; so = so->so_next) {
+        if (so->so_fport == fport) {
+            close(so->s);
+            sofree(so);
+            n++;
+            goto loop_again;
+        }
+    }

Unfortunately, this does not only target host->guest redirection sockets but also sockets slirp uses for NAT'ing guest originated connections. The same applies to your "host_net_redir list". So giving this in user hand, unwanted damaged can be caused to guest network sessions. What we need is a tag in struct socket to identify static redirection sockets.

What's your current plan regarding these two commits? We should coordinate as my work touches the same area.

Jan

#8 lore raw
Quoted 157 lines · Jan Kiszka
Quoted 156 lines · Alexander Graf

Using the new host_net_redir command you can easily create redirections on the fly while your VM is running.

While that's great, it's missing the removal of redirections, in case you want to have a port closed again at a later point in time.

This patch adds support for removal of redirections.

---

monitor.c        |    5 +++--
net.c            |   43 ++++++++++++++++++++++++++++++++++++++++++-
net.h            |    2 +-
slirp/libslirp.h |    1 +
slirp/slirp.c    |   23 +++++++++++++++++++++++
vl.c             |    2 +-
6 files changed, 71 insertions(+), 5 deletions(-)
monitor.c +2 -2
index 0f38c71..dbab3de 100644
@@ -1759,8 +1759,9 @@ static const mon_cmd_t mon_cmds[] = {
    { "host_net_remove", "is", net_host_device_remove,
      "vlan_id name", "remove host VLAN client" },
 #ifdef CONFIG_SLIRP
-    { "host_net_redir", "s", net_slirp_redir,
-      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP  
 or UDP connections from host to guest (requires -net user)" },
+    { "host_net_redir", "ss?", net_slirp_redir,
+      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP  

or UDP connections from host to guest (requires -net user)\n"
+ "host_net_redir remove [tcp:|udp:]host-port -- remove
redirection" },
#endif
{ "balloon", "i", do_balloon,
"target", "request VM to change it's memory allocation (in
MB)" },

net.c +37 -1
index 31ee95a..de019b3 100644
@@ -568,7 +568,43 @@ static int net_slirp_init(VLANState *vlan,  
 const char *model, const char *name)
    return 0;
 }
 
-void net_slirp_redir(Monitor *mon, const char *redir_str)
+static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
+{
+    int host_port;
+    char buf[256] = "";
+    const char *p = port_str;
+    int is_udp = 0;
+    int n;
+
+    if (!mon)
+        return;
+
+    if (!port_str || !port_str[0])
+        goto fail_syntax;
+
+    get_str_sep(buf, sizeof(buf), &p, ':');
+
+    if (!strcmp(buf, "tcp") || buf[0] == '\0') {
+        is_udp = 0;
+    } else if (!strcmp(buf, "udp")) {
+        is_udp = 1;
+    } else {
+        goto fail_syntax;
+    }
+
+    host_port = atoi(p);
+
+    n = slirp_redir_rm(is_udp, host_port);
+
+    monitor_printf(mon, "removed %d redirections to %s port %d\n",  
 n,
+                        is_udp ? "udp" : "tcp", host_port);
+    return;
+
+ fail_syntax:
+    monitor_printf(mon, "invalid format\n");
+}
+
+void net_slirp_redir(Monitor *mon, const char *redir_str, const  
 char *redir_opt2)
{
    int is_udp;
    char buf[256], *r;
net.c +5 -0
@@ -581,6 +617,11 @@ void net_slirp_redir(Monitor *mon, const char  
 *redir_str)
        slirp_init(slirp_restrict, slirp_ip);
    }
 
+    if (!strcmp(redir_str, "remove")) {
+        net_slirp_redir_rm(mon, redir_opt2);
+        return;
+    }
+
    p = redir_str;
    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
        goto fail_syntax;
net.h +1 -1
index 41a3082..feee021 100644
@@ -112,7 +112,7 @@ int net_client_init(const char *device, const  
 char *p);
 void net_client_uninit(NICInfo *nd);
 int net_client_parse(const char *str);
 void net_slirp_smb(const char *exported_dir);
-void net_slirp_redir(Monitor *mon, const char *redir_str);
+void net_slirp_redir(Monitor *mon, const char *redir_str, const  
 char *redir_opt2);
 void net_cleanup(void);

int slirp_is_inited(void);
void net_client_check(void);

slirp/libslirp.h +1 -0
index a1cd70e..6fc2c32 100644
@@ -18,6 +18,7 @@ void slirp_input(const uint8_t *pkt, int pkt_len);
 int slirp_can_output(void);
 void slirp_output(const uint8_t *pkt, int pkt_len);
 
+int slirp_redir_rm(int is_udp, int host_port);
 int slirp_redir(int is_udp, int host_port,
                struct in_addr guest_addr, int guest_port);
 int slirp_add_exec(int do_pty, const void *args, int addr_low_byte,
slirp/slirp.c +17 -0
index 04d3ded..33397c0 100644
@@ -734,6 +734,29 @@ void if_encap(const uint8_t *ip_data, int  
 ip_data_len)
    }
 }
 
+/* Unlistens a redirection
+ *
+ * Return value: number of redirs removed */
+int slirp_redir_rm(int is_udp, int host_port)
+{
+    struct socket *so;
+    struct socket *head = (is_udp ? &udb : &tcb);
+    int fport = htons(host_port);
+    int n = 0;
+
+ loop_again:
+    for (so = head->so_next; so != head; so = so->so_next) {
+        if (so->so_fport == fport) {
+            close(so->s);
+            sofree(so);
+            n++;
+            goto loop_again;
Quoted 8 lines

slirp/slirp.c void if_encap(const uint8_t *ip_data, int · line 756

Quoted 2 lines
slirp/slirp.c +2 -0
+        }
+    }

Unfortunately, this does not only target host->guest redirection sockets but also sockets slirp uses for NAT'ing guest originated connections. The same applies to your "host_net_redir list". So giving this in user hand, unwanted damaged can be caused to guest network sessions. What we need is a tag in struct socket to identify static redirection sockets.

Right - with the current infrastructure there's no easy way to find out if a socket was created for incoming (listen) or outgoing purposes FWIW. So a flag might make sense.

I'm not really sure if that works out for UDP sockets though.

Quoted 2 lines

What's your current plan regarding these two commits? We should coordinate as my work touches the same area.

Eh - good question. I'd like to get some means of removing and listing redirs upstream. The best case for me would be if you'd take that into your trees so I don't have to worry about it :-).

Alex

#9 lore raw
Quoted 168 lines · Alexander Graf
Quoted 156 lines · Jan Kiszka
Quoted 155 lines · Alexander Graf

Using the new host_net_redir command you can easily create redirections on the fly while your VM is running.

While that's great, it's missing the removal of redirections, in case you
want to have a port closed again at a later point in time.

This patch adds support for removal of redirections.

---

monitor.c        |    5 +++--
net.c            |   43 ++++++++++++++++++++++++++++++++++++++++++-
net.h            |    2 +-
slirp/libslirp.h |    1 +
slirp/slirp.c    |   23 +++++++++++++++++++++++
vl.c             |    2 +-
6 files changed, 71 insertions(+), 5 deletions(-)
monitor.c +2 -2
index 0f38c71..dbab3de 100644
@@ -1759,8 +1759,9 @@ static const mon_cmd_t mon_cmds[] = {
    { "host_net_remove", "is", net_host_device_remove,
      "vlan_id name", "remove host VLAN client" },
 #ifdef CONFIG_SLIRP
-    { "host_net_redir", "s", net_slirp_redir,
-      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP
 or UDP connections from host to guest (requires -net user)" },
+    { "host_net_redir", "ss?", net_slirp_redir,
+      "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP

or UDP connections from host to guest (requires -net user)\n"
+ "host_net_redir remove [tcp:|udp:]host-port -- remove
redirection" },
#endif
{ "balloon", "i", do_balloon,
"target", "request VM to change it's memory allocation (in MB)" },

net.c +37 -1
index 31ee95a..de019b3 100644
@@ -568,7 +568,43 @@ static int net_slirp_init(VLANState *vlan, const
 char *model, const char *name)
    return 0;
 }
 
-void net_slirp_redir(Monitor *mon, const char *redir_str)
+static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
+{
+    int host_port;
+    char buf[256] = "";
+    const char *p = port_str;
+    int is_udp = 0;
+    int n;
+
+    if (!mon)
+        return;
+
+    if (!port_str || !port_str[0])
+        goto fail_syntax;
+
+    get_str_sep(buf, sizeof(buf), &p, ':');
+
+    if (!strcmp(buf, "tcp") || buf[0] == '\0') {
+        is_udp = 0;
+    } else if (!strcmp(buf, "udp")) {
+        is_udp = 1;
+    } else {
+        goto fail_syntax;
+    }
+
+    host_port = atoi(p);
+
+    n = slirp_redir_rm(is_udp, host_port);
+
+    monitor_printf(mon, "removed %d redirections to %s port %d\n", n,
+                        is_udp ? "udp" : "tcp", host_port);
+    return;
+
+ fail_syntax:
+    monitor_printf(mon, "invalid format\n");
+}
+
+void net_slirp_redir(Monitor *mon, const char *redir_str, const char
 *redir_opt2)
 {
    int is_udp;
    char buf[256], *r;
net.c +5 -0
@@ -581,6 +617,11 @@ void net_slirp_redir(Monitor *mon, const char
 *redir_str)
        slirp_init(slirp_restrict, slirp_ip);
    }
 
+    if (!strcmp(redir_str, "remove")) {
+        net_slirp_redir_rm(mon, redir_opt2);
+        return;
+    }
+
    p = redir_str;
    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
        goto fail_syntax;
net.h +1 -1
index 41a3082..feee021 100644
@@ -112,7 +112,7 @@ int net_client_init(const char *device, const
 char *p);
 void net_client_uninit(NICInfo *nd);
 int net_client_parse(const char *str);
 void net_slirp_smb(const char *exported_dir);
-void net_slirp_redir(Monitor *mon, const char *redir_str);
+void net_slirp_redir(Monitor *mon, const char *redir_str, const char
 *redir_opt2);
 void net_cleanup(void);

int slirp_is_inited(void);
void net_client_check(void);

slirp/libslirp.h +1 -0
index a1cd70e..6fc2c32 100644
@@ -18,6 +18,7 @@ void slirp_input(const uint8_t *pkt, int pkt_len);
 int slirp_can_output(void);
 void slirp_output(const uint8_t *pkt, int pkt_len);
 
+int slirp_redir_rm(int is_udp, int host_port);
 int slirp_redir(int is_udp, int host_port,
                struct in_addr guest_addr, int guest_port);
 int slirp_add_exec(int do_pty, const void *args, int addr_low_byte,
slirp/slirp.c +17 -0
index 04d3ded..33397c0 100644
@@ -734,6 +734,29 @@ void if_encap(const uint8_t *ip_data, int
 ip_data_len)
    }
 }
 
+/* Unlistens a redirection
+ *
+ * Return value: number of redirs removed */
+int slirp_redir_rm(int is_udp, int host_port)
+{
+    struct socket *so;
+    struct socket *head = (is_udp ? &udb : &tcb);
+    int fport = htons(host_port);
+    int n = 0;
+
+ loop_again:
+    for (so = head->so_next; so != head; so = so->so_next) {
+        if (so->so_fport == fport) {
+            close(so->s);
+            sofree(so);
+            n++;
+            goto loop_again;
Quoted 8 lines

slirp/slirp.c void if_encap(const uint8_t *ip_data, int · line 756

Quoted 2 lines
slirp/slirp.c +2 -0
+        }
+    }

Unfortunately, this does not only target host->guest redirection sockets but also sockets slirp uses for NAT'ing guest originated connections. The same applies to your "host_net_redir list". So giving this in user hand, unwanted damaged can be caused to guest network sessions. What we need is a tag in struct socket to identify static redirection sockets.

Quoted 5 lines

Right - with the current infrastructure there's no easy way to find out if a socket was created for incoming (listen) or outgoing purposes FWIW. So a flag might make sense.

I'm not really sure if that works out for UDP sockets though.

Why not? We will tag the socket during tcp/udp_listen, right after socreate e.g.

Quoted 7 lines
Quoted 3 lines

What's your current plan regarding these two commits? We should coordinate as my work touches the same area.

Eh - good question. I'd like to get some means of removing and listing redirs upstream. The best case for me would be if you'd take that into your trees so I don't have to worry about it :-).

I was afraid you'll say this :). As you patches were already merged, I have to deal with them earlier than expected anyway. OK, will pick this up.

Jan