User Networking: Enable removal of redirections
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
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
@@ -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
@@ -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
@@ -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
[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.
Signed-off-by Alexander Graf <[email protected]>--- 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
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
Quoted 7 lines · Alexander Graf
Using the new
host_net_redircommand 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
Signed-off-by Alexander Graf <[email protected]>--- 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
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
Quoted 37 lines · Jan Kiszka
Quoted 7 lines · Alexander Graf
Using the new
host_net_redircommand 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
Signed-off-by Alexander Graf <[email protected]>--- 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
-0monitor.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 intohost_net_fwd_addandhost_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
Quoted 55 lines · Jan Kiszka
Quoted 39 lines · Jan Kiszka
Quoted 7 lines · Alexander Graf
Using the new
host_net_redircommand 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
Signed-off-by Alexander Graf <[email protected]>---
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@@ -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", "redirectQuoted 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 intohost_net_fwd_addandhost_net_fwd_remove.Quoted 6 lines
+ "
host_net_redirremove [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
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_redircommand 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
Signed-off-by Alexander Graf <[email protected]>---
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@@ -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 TCPQuoted 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 intohost_net_fwd_addandhost_net_fwd_remove.Quoted 6 lines
+ "
host_net_redirremove [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_redirqemu 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
Quoted 137 lines · Alexander Graf
Using the new
host_net_redircommand 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.
Signed-off-by Alexander Graf <[email protected]>--- 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@@ -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@@ -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@@ -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@@ -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@@ -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
Quoted 157 lines · Jan Kiszka
Quoted 156 lines · Alexander Graf
Using the new
host_net_redircommand 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.
Signed-off-by Alexander Graf <[email protected]>---
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@@ -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 TCPor UDP connections from host to guest (requires -net user)\n"
+ "host_net_redirremove [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@@ -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@@ -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);
voidnet_client_check(void);slirp/libslirp.h +1
-0@@ -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@@ -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->guestredirection sockets but also sockets slirp uses for NAT'ing guest originated connections. The same applies to your "host_net_redirlist". 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
Quoted 168 lines · Alexander Graf
Quoted 156 lines · Jan Kiszka
Quoted 155 lines · Alexander Graf
Using the new
host_net_redircommand 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.
Signed-off-by Alexander Graf <[email protected]>---
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@@ -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 TCPor UDP connections from host to guest (requires -net user)\n"
+ "host_net_redirremove [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@@ -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@@ -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);
voidnet_client_check(void);slirp/libslirp.h +1
-0@@ -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@@ -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->guestredirection sockets but also sockets slirp uses for NAT'ing guest originated connections. The same applies to your "host_net_redirlist". 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