Skip to contentSkip to conversation

Marry slirp and qemu character device.

PATCH In review Gleb Natapov 3 participants 11 messages Jan 8, 2009

lore
Share & tools
lore
#0 lore raw

v3 cover Marry slirp and qemu character device.

Windows compilation should be fixed now.

Trap TCP connection to special IP/port inside slirp and redirect its traffic to a qemu character device. Add option to prevent connections through slirp to outside world. Add migration support for "special" sockets. This allow slirp to be used for secure communication between host and guest management agents.

---

Gleb Natapov (5):
Add support for vmchannel socket migration. Add "restrict" and "ip" option to "user" net option Add slirp_restrict option.
Add vmchannel command line option.
Redirect slirp traffic to/from qemu character device.

 net.c             |   14 ++
 slirp/bootp.c     |   22 ++--
 slirp/ip_input.c  |   21 ++++
 slirp/libslirp.h  |    7 +
 slirp/main.h      |    3 +
 slirp/misc.c      |    2 
 slirp/sbuf.c      |    2 
 slirp/slirp.c     |  300 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 slirp/socket.c    |  101 ++++++++++++++----
 slirp/socket.h    |    2 
 slirp/tcp_input.c |   11 ++
 slirp/tcp_subr.c  |    5 +
 slirp/udp.c       |    3 +
 vl.c              |   75 +++++++++++++
 14 files changed, 525 insertions(+), 43 deletions(-)
Signature · 1 line
	Gleb.
#1 lore raw

PATCH v3 1/5 Redirect slirp traffic to/from qemu character device.

---

 slirp/libslirp.h |    5 ++-
 slirp/main.h     |    1 +
 slirp/misc.c     |    2 +
 slirp/sbuf.c     |    2 +
 slirp/slirp.c    |   63 +++++++++++++++++++++++++++++++++-
 slirp/socket.c   |  101 +++++++++++++++++++++++++++++++++++++++++++-----------
 slirp/socket.h   |    2 +
 slirp/tcp_subr.c |    5 +++
 8 files changed, 156 insertions(+), 25 deletions(-)
slirp/libslirp.h +4 -1
index 7e4cfa9..16a817a 100644
@@ -20,13 +20,16 @@ void slirp_output(const uint8_t *pkt, int pkt_len);
 
 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 char *args, int addr_low_byte,
+int slirp_add_exec(int do_pty, const void *args, int addr_low_byte,
                    int guest_port);
 
 extern const char *tftp_prefix;
 extern char slirp_hostname[33];
 
 void slirp_stats(void);
+void slirp_socket_recv(int addr_low_byte, int guest_port, const uint8_t *buf,
+		int size);
+size_t slirp_socket_can_recv(int addr_low_byte, int guest_port);
 
 #ifdef __cplusplus
 }
slirp/main.h +1 -0
index c01adda..3ef2996 100644
@@ -51,3 +51,4 @@ extern uint8_t client_ethaddr[6];
 #endif
 
 void if_encap(const uint8_t *ip_data, int ip_data_len);
+ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags);
slirp/misc.c +1 -1
index 9ff3176..1ec8b55 100644
@@ -169,7 +169,7 @@ add_exec(ex_ptr, do_pty, exec, addr, port)
 	(*ex_ptr)->ex_fport = port;
 	(*ex_ptr)->ex_addr = addr;
 	(*ex_ptr)->ex_pty = do_pty;
-	(*ex_ptr)->ex_exec = strdup(exec);
+	(*ex_ptr)->ex_exec = (do_pty == 3) ? exec : strdup(exec);
 	(*ex_ptr)->ex_next = tmp_ptr;
 	return 0;
 }
slirp/sbuf.c +1 -1
index b0e0838..2e6e2b2 100644
@@ -108,7 +108,7 @@ sbappend(so, m)
 	 * ottherwise it'll arrive out of order, and hence corrupt
 	 */
 	if (!so->so_rcv.sb_cc)
-	   ret = send(so->s, m->m_data, m->m_len, 0);
+	   ret = slirp_send(so, m->m_data, m->m_len, 0);
 
 	if (ret <= 0) {
 		/*
slirp/slirp.c +62 -1

77 lines not on this page · raw message

slirp/socket.c +80 -21

164 lines not on this page · raw message

slirp/socket.h +2 -0
index 5edea90..72b473d 100644
@@ -87,5 +87,7 @@ void soisfconnecting _P((register struct socket *));
 void soisfconnected _P((register struct socket *));
 void soisfdisconnected _P((struct socket *));
 void sofwdrain _P((struct socket *));
+size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np);
+int soreadbuf(struct socket *so, const char *buf, int size);
 
 #endif /* _SOCKET_H_ */
slirp/tcp_subr.c +5 -0
index bce07a6..12abebe 100644
@@ -1281,6 +1281,11 @@ tcp_ctl(so)
 		for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
 			if (ex_ptr->ex_fport == so->so_fport &&
 			    command == ex_ptr->ex_addr) {
+				if (ex_ptr->ex_pty == 3) {
+					so->s = -1;
+					so->extra = ex_ptr->ex_exec;
+					return 1;
+				}
 				do_pty = ex_ptr->ex_pty;
 				goto do_exec;
 			}
#2 lore raw

PATCH v3 2/5 Add vmchannel command line option.

In reply to Gleb Natapov

---

 vl.c |   75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 74 insertions(+), 1 deletions(-)
vl.c +74 -1
index 959be62..c076375 100644
@@ -207,6 +207,13 @@ static int full_screen = 0;
 static int no_frame = 0;
 #endif
 int no_quit = 0;
+#if defined(CONFIG_SLIRP)
+#define MAX_VMCHANNEL_DEVICES 4
+struct VMChannel {
+    CharDriverState *hd;
+    int port;
+} vmchannel_hds[MAX_VMCHANNEL_DEVICES];
+#endif
 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 #ifdef TARGET_I386
@@ -3948,6 +3955,9 @@ static void help(int exitcode)
            "-monitor dev    redirect the monitor to char device 'dev'\n"
            "-serial dev     redirect the serial port to char device 'dev'\n"
            "-parallel dev   redirect the parallel port to char device 'dev'\n"
+#if defined(CONFIG_SLIRP)
+	   "-vmchannel di:DI,dev  redirect the vmchannel device with device id DI, to char device 'dev'\n"
+#endif
            "-pidfile file   Write PID to 'file'\n"
            "-S              freeze CPU at startup (use 'c' to start execution)\n"
            "-s              wait gdb connection to port\n"
@@ -4062,6 +4072,9 @@ enum {
     QEMU_OPTION_monitor,
     QEMU_OPTION_serial,
     QEMU_OPTION_parallel,
+#if defined(CONFIG_SLIRP)
+    QEMU_OPTION_vmchannel,
+#endif
     QEMU_OPTION_loadvm,
     QEMU_OPTION_full_screen,
     QEMU_OPTION_no_frame,
@@ -4171,6 +4184,9 @@ static const QEMUOption qemu_options[] = {
     { "monitor", HAS_ARG, QEMU_OPTION_monitor },
     { "serial", HAS_ARG, QEMU_OPTION_serial },
     { "parallel", HAS_ARG, QEMU_OPTION_parallel },
+#if defined(CONFIG_SLIRP)
+    { "vmchannel", 1, QEMU_OPTION_vmchannel },
+#endif
     { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
     { "full-screen", 0, QEMU_OPTION_full_screen },
 #ifdef CONFIG_SDL
@@ -4469,6 +4485,20 @@ static void termsig_setup(void)
 
 #endif
 
+#if defined(CONFIG_SLIRP)
+static int vmchannel_can_read(void *opaque)
+{
+    struct VMChannel *vmc = (struct VMChannel*)opaque;
+    return slirp_socket_can_recv(4, vmc->port);
+}
+
+static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
+{
+    struct VMChannel *vmc = (struct VMChannel*)opaque;
+    slirp_socket_recv(4, vmc->port, buf, size);
+}
+#endif
+
 int main(int argc, char **argv, char **envp)
 {
 #ifdef CONFIG_GDBSTUB
@@ -4496,6 +4526,8 @@ int main(int argc, char **argv, char **envp)
     int serial_device_index;
     const char *parallel_devices[MAX_PARALLEL_PORTS];
     int parallel_device_index;
+    char *vmchannel_devices[MAX_VMCHANNEL_DEVICES];
+    int vmchannel_device_index;
     const char *loadvm = NULL;
     QEMUMachine *machine;
     const char *cpu_model;
@@ -4569,6 +4601,10 @@ int main(int argc, char **argv, char **envp)
         parallel_devices[i] = NULL;
     parallel_device_index = 0;
 
+    for(i = 0; i < MAX_VMCHANNEL_DEVICES; i++)
+    	vmchannel_devices[i] = NULL;
+    vmchannel_device_index = 0;
+
     usb_devices_index = 0;
 
     nb_net_clients = 0;
@@ -4963,7 +4999,15 @@ int main(int argc, char **argv, char **envp)
                 parallel_devices[parallel_device_index] = optarg;
                 parallel_device_index++;
                 break;
-	    case QEMU_OPTION_loadvm:
+#if defined(CONFIG_SLIRP)
+            case QEMU_OPTION_vmchannel:
+                if (vmchannel_device_index >= MAX_VMCHANNEL_DEVICES) {
+                    fprintf(stderr, "qemu: too many vmchannel devices\n");
+                    exit(1);
+                }
+                vmchannel_devices[vmchannel_device_index++] = optarg;
+#endif
+            case QEMU_OPTION_loadvm:
 		loadvm = optarg;
 		break;
             case QEMU_OPTION_full_screen:
@@ -5468,6 +5512,35 @@ int main(int argc, char **argv, char **envp)
         }
     }
 
+#if defined(CONFIG_SLIRP)
+    for(i = 0; i < vmchannel_device_index; i++) {
+        char *devname = vmchannel_devices[i];
+        long port;
+        char name[20];
+
+        if (!devname)
+            continue;
+
+        port = strtol(devname, &devname, 10);
+        devname++;
+        if (port < 1 || port > 65535) {
+            fprintf(stderr, "vmchannel: wrong port number\n");
+            exit(1);
+        }
+        snprintf(name, 20, "vmchannel%ld\n", port);
+        vmchannel_hds[i].hd = qemu_chr_open(name, devname);
+        if (!vmchannel_hds[i].hd) {
+            fprintf(stderr, "qemu: could not open vmchannel device '%s'\n",
+                    devname);
+            exit(1);
+        }
+        vmchannel_hds[i].port = port;
+        slirp_add_exec(3, vmchannel_hds[i].hd, 4, port);
+        qemu_chr_add_handlers(vmchannel_hds[i].hd, vmchannel_can_read,
+                vmchannel_read, NULL, &vmchannel_hds[i]);
+    }
+#endif
+
     machine->init(ram_size, vga_ram_size, boot_devices, ds,
                   kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
 
#3 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] Add vmchannel command line option.

Quoted 35 lines · Gleb Natapov

---

 vl.c |   75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 74 insertions(+), 1 deletions(-)
vl.c +10 -0
index 959be62..c076375 100644
@@ -207,6 +207,13 @@ static int full_screen = 0;
 static int no_frame = 0;
 #endif
 int no_quit = 0;
+#if defined(CONFIG_SLIRP)
+#define MAX_VMCHANNEL_DEVICES 4
+struct VMChannel {
+    CharDriverState *hd;
+    int port;
+} vmchannel_hds[MAX_VMCHANNEL_DEVICES];
+#endif
 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 #ifdef TARGET_I386
@@ -3948,6 +3955,9 @@ static void help(int exitcode)
            "-monitor dev    redirect the monitor to char device 'dev'\n"
            "-serial dev     redirect the serial port to char device 'dev'\n"
            "-parallel dev   redirect the parallel port to char device 'dev'\n"
+#if defined(CONFIG_SLIRP)
+	   "-vmchannel di:DI,dev  redirect the vmchannel device with device id DI, to char device 'dev'\n"
+#endif
            "-pidfile file   Write PID to 'file'\n"
            "-S              freeze CPU at startup (use 'c' to start execution)\n"
            "-s              wait gdb connection to port\n"

vl.c enum { · line 4078

Quoted 7 lines
vl.c +3 -0
@@ -4062,6 +4072,9 @@ enum {
     QEMU_OPTION_monitor,
     QEMU_OPTION_serial,
     QEMU_OPTION_parallel,
+#if defined(CONFIG_SLIRP)
+    QEMU_OPTION_vmchannel,
+#endif
 

I think this would make more sense as an option to -net user. For instance:

-net user,port=1024,chardev=unix:foo.sock

Not the best syntax, but you get the idea. What do you think?

It may be useful to use this functionality for other things too.

Regards,

Anthony Liguroi

#4 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] Add vmchannel command line option.

Quoted 51 lines · Anthony Liguori
Quoted 40 lines · Gleb Natapov

---

vl.c | 75
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-

1 files changed, 74 insertions(+), 1 deletions(-)
vl.c +9 -0
index 959be62..c076375 100644
@@ -207,6 +207,13 @@ static int full_screen = 0;
 static int no_frame = 0;
 #endif
 int no_quit = 0;
+#if defined(CONFIG_SLIRP)
+#define MAX_VMCHANNEL_DEVICES 4
+struct VMChannel {
+    CharDriverState *hd;
+    int port;
+} vmchannel_hds[MAX_VMCHANNEL_DEVICES];
+#endif
 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 #ifdef TARGET_I386
@@ -3948,6 +3955,9 @@ static void help(int exitcode)
           "-monitor dev    redirect the monitor to char device 'dev'\n"
           "-serial dev     redirect the serial port to char device 
           'dev'\n"
           "-parallel dev   redirect the parallel port to char device 
           'dev'\n"
+#if defined(CONFIG_SLIRP)
+	   "-vmchannel di:DI,dev  redirect the vmchannel device with device 

id DI, to char device 'dev'\n"

vl.c +1 -0
+#endif
           "-pidfile file   Write PID to 'file'\n"
           "-S              freeze CPU at startup (use 'c' to start 
           execution)\n"
           "-s              wait gdb connection to port\n"

vl.c enum { · line 4078

Quoted 7 lines
vl.c +3 -0
@@ -4062,6 +4072,9 @@ enum {
    QEMU_OPTION_monitor,
    QEMU_OPTION_serial,
    QEMU_OPTION_parallel,
+#if defined(CONFIG_SLIRP)
+    QEMU_OPTION_vmchannel,
+#endif
 

I think this would make more sense as an option to -net user. For instance:

Quoted 5 lines

-net user,port=1024,chardev=unix:foo.sock

Not the best syntax, but you get the idea. What do you think?

It may be useful to use this functionality for other things too.

If I'm understanding correctly, the VM channel stuff has a hard requirement for Slirp/user net setup, so could you just go one step further and use the full char device syntax with -net, thus eliminating the redundant 'user:' bit ?

-net char:[CHAR-DEV-OPTIONS],port=1024

Or

-net vmchan:[CHAR-DEV-OPTIONS],port=1024

Where 'CHAR-DEV-OPTIONS' is any of the stuff valid for -serial/-parallal char device options

Daniel

Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#5 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] Add vmchannel command line option.

Quoted 66 lines · Daniel P. Berrange
Quoted 52 lines · Anthony Liguori
Quoted 40 lines · Gleb Natapov

---

vl.c | 75
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-

1 files changed, 74 insertions(+), 1 deletions(-)
vl.c +9 -0
index 959be62..c076375 100644
@@ -207,6 +207,13 @@ static int full_screen = 0;
 static int no_frame = 0;
 #endif
 int no_quit = 0;
+#if defined(CONFIG_SLIRP)
+#define MAX_VMCHANNEL_DEVICES 4
+struct VMChannel {
+    CharDriverState *hd;
+    int port;
+} vmchannel_hds[MAX_VMCHANNEL_DEVICES];
+#endif
 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 #ifdef TARGET_I386
@@ -3948,6 +3955,9 @@ static void help(int exitcode)
           "-monitor dev    redirect the monitor to char device 'dev'\n"
           "-serial dev     redirect the serial port to char device 
           'dev'\n"
           "-parallel dev   redirect the parallel port to char device 
           'dev'\n"
+#if defined(CONFIG_SLIRP)
+	   "-vmchannel di:DI,dev  redirect the vmchannel device with device 

id DI, to char device 'dev'\n"

vl.c +1 -0
+#endif
           "-pidfile file   Write PID to 'file'\n"
           "-S              freeze CPU at startup (use 'c' to start 
           execution)\n"
           "-s              wait gdb connection to port\n"

vl.c enum { · line 4079

Quoted 8 lines
vl.c +3 -0
@@ -4062,6 +4072,9 @@ enum {
    QEMU_OPTION_monitor,
    QEMU_OPTION_serial,
    QEMU_OPTION_parallel,
+#if defined(CONFIG_SLIRP)
+    QEMU_OPTION_vmchannel,
+#endif
 
 

I think this would make more sense as an option to -net user. For instance:

Quoted 6 lines

-net user,port=1024,chardev=unix:foo.sock

Not the best syntax, but you get the idea. What do you think?

It may be useful to use this functionality for other things too.

If I'm understanding correctly, the VM channel stuff has a hard requirement for Slirp/user net setup, so could you just go one step further and use the full char device syntax with -net, thus eliminating the redundant 'user:' bit ?

Quoted 9 lines
   -net char:[CHAR-DEV-OPTIONS],port=1024

Or

   -net vmchan:[CHAR-DEV-OPTIONS],port=1024

Where 'CHAR-DEV-OPTIONS' is any of the stuff valid for -serial/-parallal char device options

Yeah, that would be nice too. My main point was moving it from a standalone option to something that was part of net. I'd avoid the name "vmchan" though.

Regards,

Anthony Liguori

Quoted 2 lines

Daniel

#6 lore raw

PATCH v3 3/5 Add slirp_restrict option.

In reply to Gleb Natapov

Add "slirp firewall" to permit connection only to vmchannel addresses.

---

 net.c             |    6 +++---
 slirp/bootp.c     |   22 ++++++++++++----------
 slirp/ip_input.c  |   21 +++++++++++++++++++++
 slirp/libslirp.h  |    2 +-
 slirp/main.h      |    2 ++
 slirp/slirp.c     |   10 ++++++++--
 slirp/tcp_input.c |   11 ++++++++++-
 slirp/udp.c       |    3 +++
 8 files changed, 60 insertions(+), 17 deletions(-)
net.c +3 -3
index 15f9153..2399557 100644
@@ -482,7 +482,7 @@ static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
 {
     if (!slirp_inited) {
         slirp_inited = 1;
-        slirp_init();
+        slirp_init(0, NULL);
     }
     slirp_vc = qemu_new_vlan_client(vlan, model, name,
                                     slirp_receive, NULL, NULL);
@@ -500,7 +500,7 @@ void net_slirp_redir(const char *redir_str)
 
     if (!slirp_inited) {
         slirp_inited = 1;
-        slirp_init();
+        slirp_init(0, NULL);
     }
 
     p = redir_str;
@@ -586,7 +586,7 @@ void net_slirp_smb(const char *exported_dir)
 
     if (!slirp_inited) {
         slirp_inited = 1;
-        slirp_init();
+        slirp_init(0, NULL);
     }
 
     /* XXX: better tmp dir construction */
slirp/bootp.c +12 -10
index 750fae3..bf704ab 100644
@@ -219,16 +219,18 @@ static void bootp_reply(struct bootp_t *bp)
         *q++ = 0xff;
         *q++ = 0x00;
 
-        *q++ = RFC1533_GATEWAY;
-        *q++ = 4;
-        memcpy(q, &saddr.sin_addr, 4);
-        q += 4;
-
-        *q++ = RFC1533_DNS;
-        *q++ = 4;
-        dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
-        memcpy(q, &dns_addr, 4);
-        q += 4;
+        if (!slirp_restrict) {
+            *q++ = RFC1533_GATEWAY;
+            *q++ = 4;
+            memcpy(q, &saddr.sin_addr, 4);
+            q += 4;
+
+            *q++ = RFC1533_DNS;
+            *q++ = 4;
+            dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
+            memcpy(q, &dns_addr, 4);
+            q += 4;
+        }
 
         *q++ = RFC2132_LEASE_TIME;
         *q++ = 4;
slirp/ip_input.c +21 -0
index b046840..73cb00e 100644
@@ -136,6 +136,27 @@ ip_input(m)
 		STAT(ipstat.ips_tooshort++);
 		goto bad;
 	}
+
+    if (slirp_restrict) {
+        if (memcmp(&ip->ip_dst.s_addr, &special_addr, 3)) {
+            if (ip->ip_dst.s_addr == 0xffffffff && ip->ip_p != IPPROTO_UDP)
+                goto bad;
+        } else {
+            int host = ntohl(ip->ip_dst.s_addr) & 0xff;
+            struct ex_list *ex_ptr;
+
+            if (host == 0xff)
+                goto bad;
+
+            for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
+                if (ex_ptr->ex_addr == host)
+                    break;
+
+            if (!ex_ptr)
+                goto bad;
+        }
+    }
+
 	/* Should drop packet if mbuf too long? hmmm... */
 	if (m->m_len > ip->ip_len)
 	   m_adj(m, ip->ip_len - m->m_len);
slirp/libslirp.h +1 -1
index 16a817a..6c5db54 100644
@@ -5,7 +5,7 @@
 extern "C" {
 #endif
 
-void slirp_init(void);
+void slirp_init(int restrict, char *special_ip);
 
 void slirp_select_fill(int *pnfds,
                        fd_set *readfds, fd_set *writefds, fd_set *xfds);
slirp/main.h +2 -0
index 3ef2996..b492614 100644
@@ -44,6 +44,8 @@ extern int towrite_max;
 extern int ppp_exit;
 extern int tcp_keepintvl;
 extern uint8_t client_ethaddr[6];
+extern char *slirp_special_ip;
+extern int slirp_restrict;
 
 #define PROTO_SLIP 0x1
 #ifdef USE_PPP
slirp/slirp.c +8 -2
index f6f94e4..ce4a1b7 100644
@@ -46,6 +46,8 @@ static struct in_addr client_ipaddr;
 
 static const uint8_t zero_ethaddr[6] = { 0, 0, 0, 0, 0, 0 };
 
+char *slirp_special_ip = CTL_SPECIAL;
+int slirp_restrict;
 int do_slowtimo;
 int link_up;
 struct timeval tt;
@@ -164,7 +166,7 @@ static void slirp_cleanup(void)
 }
 #endif
 
-void slirp_init(void)
+void slirp_init(int restrict, char *special_ip)
 {
     //    debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
 
@@ -177,6 +179,7 @@ void slirp_init(void)
 #endif
 
     link_up = 1;
+    slirp_restrict = restrict;
 
     if_init();
     ip_init();
@@ -192,7 +195,10 @@ void slirp_init(void)
         fprintf (stderr, "Warning: No DNS servers found\n");
     }
 
-    inet_aton(CTL_SPECIAL, &special_addr);
+    if (special_ip)
+        slirp_special_ip = special_ip;
+
+    inet_aton(slirp_special_ip, &special_addr);
     alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
     getouraddr();
 }
slirp/tcp_input.c +10 -1
index 17a9387..408875e 100644
@@ -253,6 +253,7 @@ tcp_input(m, iphlen, inso)
 	u_long tiwin;
 	int ret;
 /*	int ts_present = 0; */
+    struct ex_list *ex_ptr;
 
 	DEBUG_CALL("tcp_input");
 	DEBUG_ARGS((dfd," m = %8lx  iphlen = %2d  inso = %lx\n",
@@ -363,6 +364,15 @@ tcp_input(m, iphlen, inso)
 	m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
 	m->m_len  -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
 
+    if (slirp_restrict) {
+        for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
+            if (ex_ptr->ex_fport == ti->ti_dport &&
+                    (ntohl(ti->ti_dst.s_addr) & 0xff) == ex_ptr->ex_addr)
+                break;
+
+        if (!ex_ptr)
+            goto drop;
+    }
 	/*
 	 * Locate pcb for segment.
 	 */
@@ -646,7 +656,6 @@ findso:
 #endif
               {
 		/* May be an add exec */
-		struct ex_list *ex_ptr;
 		for(ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
 		  if(ex_ptr->ex_fport == so->so_fport &&
 		     lastbyte == ex_ptr->ex_addr) {
slirp/udp.c +3 -0
index 8030326..ca353b7 100644
@@ -158,6 +158,9 @@ udp_input(m, iphlen)
             goto bad;
         }
 
+        if (slirp_restrict)
+            goto bad;
+
         /*
          *  handle TFTP
          */
#7 lore raw

PATCH v3 4/5 Add "restrict" and "ip" option to "user" net option

In reply to Gleb Natapov

Expose new slirp capabilities to user through a command line options.

---

 net.c |   14 +++++++++++---
 1 files changed, 11 insertions(+), 3 deletions(-)
net.c +11 -3
index 2399557..237d3ef 100644
@@ -446,6 +446,8 @@ ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
 /* slirp network adapter */
 
 static int slirp_inited;
+static int slirp_restrict;
+static char *slirp_ip;
 static VLANClientState *slirp_vc;
 
 int slirp_can_output(void)
@@ -482,7 +484,7 @@ static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
 {
     if (!slirp_inited) {
         slirp_inited = 1;
-        slirp_init(0, NULL);
+        slirp_init(slirp_restrict, slirp_ip);
     }
     slirp_vc = qemu_new_vlan_client(vlan, model, name,
                                     slirp_receive, NULL, NULL);
@@ -500,7 +502,7 @@ void net_slirp_redir(const char *redir_str)
 
     if (!slirp_inited) {
         slirp_inited = 1;
-        slirp_init(0, NULL);
+        slirp_init(slirp_restrict, slirp_ip);
     }
 
     p = redir_str;
@@ -586,7 +588,7 @@ void net_slirp_smb(const char *exported_dir)
 
     if (!slirp_inited) {
         slirp_inited = 1;
-        slirp_init(0, NULL);
+        slirp_init(slirp_restrict, slirp_ip);
     }
 
     /* XXX: better tmp dir construction */
@@ -1553,6 +1555,12 @@ int net_client_init(const char *device, const char *p)
         if (get_param_value(buf, sizeof(buf), "hostname", p)) {
             pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
         }
+        if (get_param_value(buf, sizeof(buf), "restrict", p)) {
+            slirp_restrict = (buf[0] == 'y') ? 1 : 0;
+        }
+        if (get_param_value(buf, sizeof(buf), "ip", p)) {
+            slirp_ip = strdup(buf);
+        }
         vlan->nb_host_devs++;
         ret = net_slirp_init(vlan, device, name);
     } else
#8 lore raw

PATCH v3 5/5 Add support for vmchannel socket migration.

In reply to Gleb Natapov

---

 slirp/slirp.c |  227 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 227 insertions(+), 0 deletions(-)
slirp/slirp.c +227 -0
index ce4a1b7..c1361ab 100644
@@ -23,6 +23,7 @@
  */
 #include "qemu-common.h"
 #include "slirp.h"
+#include "hw/hw.h"
 
 /* host address */
 struct in_addr our_addr;
@@ -166,6 +167,9 @@ static void slirp_cleanup(void)
 }
 #endif
 
+static void slirp_state_save(QEMUFile *f, void *opaque);
+static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
+
 void slirp_init(int restrict, char *special_ip)
 {
     //    debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
@@ -201,6 +205,7 @@ void slirp_init(int restrict, char *special_ip)
     inet_aton(slirp_special_ip, &special_addr);
     alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
     getouraddr();
+    register_savevm("slirp", 0, 1, slirp_state_save, slirp_state_load, NULL);
 }
 
 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
@@ -809,3 +814,225 @@ void slirp_socket_recv(int addr_low_byte, int guest_port, const uint8_t *buf,
     if (ret > 0)
         tcp_output(sototcpcb(so));
 }
+
+static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
+{
+    int i;
+
+    qemu_put_sbe16(f, tp->t_state);
+    for (i = 0; i < TCPT_NTIMERS; i++)
+        qemu_put_sbe16(f, tp->t_timer[i]);
+    qemu_put_sbe16(f, tp->t_rxtshift);
+    qemu_put_sbe16(f, tp->t_rxtcur);
+    qemu_put_sbe16(f, tp->t_dupacks);
+    qemu_put_be16(f, tp->t_maxseg);
+    qemu_put_sbyte(f, tp->t_force);
+    qemu_put_be16(f, tp->t_flags);
+    qemu_put_be32(f, tp->snd_una);
+    qemu_put_be32(f, tp->snd_nxt);
+    qemu_put_be32(f, tp->snd_up);
+    qemu_put_be32(f, tp->snd_wl1);
+    qemu_put_be32(f, tp->snd_wl2);
+    qemu_put_be32(f, tp->iss);
+    qemu_put_be32(f, tp->snd_wnd);
+    qemu_put_be32(f, tp->rcv_wnd);
+    qemu_put_be32(f, tp->rcv_nxt);
+    qemu_put_be32(f, tp->rcv_up);
+    qemu_put_be32(f, tp->irs);
+    qemu_put_be32(f, tp->rcv_adv);
+    qemu_put_be32(f, tp->snd_max);
+    qemu_put_be32(f, tp->snd_cwnd);
+    qemu_put_be32(f, tp->snd_ssthresh);
+    qemu_put_sbe16(f, tp->t_idle);
+    qemu_put_sbe16(f, tp->t_rtt);
+    qemu_put_be32(f, tp->t_rtseq);
+    qemu_put_sbe16(f, tp->t_srtt);
+    qemu_put_sbe16(f, tp->t_rttvar);
+    qemu_put_be16(f, tp->t_rttmin);
+    qemu_put_be32(f, tp->max_sndwnd);
+    qemu_put_byte(f, tp->t_oobflags);
+    qemu_put_byte(f, tp->t_iobc);
+    qemu_put_sbe16(f, tp->t_softerror);
+    qemu_put_byte(f, tp->snd_scale);
+    qemu_put_byte(f, tp->rcv_scale);
+    qemu_put_byte(f, tp->request_r_scale);
+    qemu_put_byte(f, tp->requested_s_scale);
+    qemu_put_be32(f, tp->ts_recent);
+    qemu_put_be32(f, tp->ts_recent_age);
+    qemu_put_be32(f, tp->last_ack_sent);
+}
+
+static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf)
+{
+    uint32_t off;
+
+    qemu_put_be32(f, sbuf->sb_cc);
+    qemu_put_be32(f, sbuf->sb_datalen);
+    off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data);
+    qemu_put_sbe32(f, off);
+    off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data);
+    qemu_put_sbe32(f, off);
+    qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
+}
+
+static void slirp_socket_save(QEMUFile *f, struct socket *so)
+{
+    qemu_put_be32(f, so->so_urgc);
+    qemu_put_be32(f, so->so_faddr.s_addr);
+    qemu_put_be32(f, so->so_laddr.s_addr);
+    qemu_put_be16(f, so->so_fport);
+    qemu_put_be16(f, so->so_lport);
+    qemu_put_byte(f, so->so_iptos);
+    qemu_put_byte(f, so->so_emu);
+    qemu_put_byte(f, so->so_type);
+    qemu_put_be32(f, so->so_state);
+    slirp_sbuf_save(f, &so->so_rcv);
+    slirp_sbuf_save(f, &so->so_snd);
+    slirp_tcp_save(f, so->so_tcpcb);
+}
+
+static void slirp_state_save(QEMUFile *f, void *opaque)
+{
+    struct ex_list *ex_ptr;
+
+    for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
+        if (ex_ptr->ex_pty == 3) {
+            struct socket *so;
+            so = slirp_find_ctl_socket(ex_ptr->ex_addr, ntohs(ex_ptr->ex_fport));
+            if (!so)
+                continue;
+
+            qemu_put_byte(f, 42);
+            slirp_socket_save(f, so);
+        }
+    qemu_put_byte(f, 0);
+}
+
+static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp)
+{
+    int i;
+
+    tp->t_state = qemu_get_sbe16(f);
+    for (i = 0; i < TCPT_NTIMERS; i++)
+        tp->t_timer[i] = qemu_get_sbe16(f);
+    tp->t_rxtshift = qemu_get_sbe16(f);
+    tp->t_rxtcur = qemu_get_sbe16(f);
+    tp->t_dupacks = qemu_get_sbe16(f);
+    tp->t_maxseg = qemu_get_be16(f);
+    tp->t_force = qemu_get_sbyte(f);
+    tp->t_flags = qemu_get_be16(f);
+    tp->snd_una = qemu_get_be32(f);
+    tp->snd_nxt = qemu_get_be32(f);
+    tp->snd_up = qemu_get_be32(f);
+    tp->snd_wl1 = qemu_get_be32(f);
+    tp->snd_wl2 = qemu_get_be32(f);
+    tp->iss = qemu_get_be32(f);
+    tp->snd_wnd = qemu_get_be32(f);
+    tp->rcv_wnd = qemu_get_be32(f);
+    tp->rcv_nxt = qemu_get_be32(f);
+    tp->rcv_up = qemu_get_be32(f);
+    tp->irs = qemu_get_be32(f);
+    tp->rcv_adv = qemu_get_be32(f);
+    tp->snd_max = qemu_get_be32(f);
+    tp->snd_cwnd = qemu_get_be32(f);
+    tp->snd_ssthresh = qemu_get_be32(f);
+    tp->t_idle = qemu_get_sbe16(f);
+    tp->t_rtt = qemu_get_sbe16(f);
+    tp->t_rtseq = qemu_get_be32(f);
+    tp->t_srtt = qemu_get_sbe16(f);
+    tp->t_rttvar = qemu_get_sbe16(f);
+    tp->t_rttmin = qemu_get_be16(f);
+    tp->max_sndwnd = qemu_get_be32(f);
+    tp->t_oobflags = qemu_get_byte(f);
+    tp->t_iobc = qemu_get_byte(f);
+    tp->t_softerror = qemu_get_sbe16(f);
+    tp->snd_scale = qemu_get_byte(f);
+    tp->rcv_scale = qemu_get_byte(f);
+    tp->request_r_scale = qemu_get_byte(f);
+    tp->requested_s_scale = qemu_get_byte(f);
+    tp->ts_recent = qemu_get_be32(f);
+    tp->ts_recent_age = qemu_get_be32(f);
+    tp->last_ack_sent = qemu_get_be32(f);
+    tcp_template(tp);
+}
+
+static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf)
+{
+    uint32_t off, sb_cc, sb_datalen;
+
+    sb_cc = qemu_get_be32(f);
+    sb_datalen = qemu_get_be32(f);
+
+    sbreserve(sbuf, sb_datalen);
+
+    if (sbuf->sb_datalen != sb_datalen)
+        return -ENOMEM;
+
+    sbuf->sb_cc = sb_cc;
+
+    off = qemu_get_sbe32(f);
+    sbuf->sb_wptr = sbuf->sb_data + off;
+    off = qemu_get_sbe32(f);
+    sbuf->sb_rptr = sbuf->sb_data + off;
+    qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
+
+    return 0;
+}
+
+static int slirp_socket_load(QEMUFile *f, struct socket *so)
+{
+    if (tcp_attach(so) < 0)
+        return -ENOMEM;
+
+    so->so_urgc = qemu_get_be32(f);
+    so->so_faddr.s_addr = qemu_get_be32(f);
+    so->so_laddr.s_addr = qemu_get_be32(f);
+    so->so_fport = qemu_get_be16(f);
+    so->so_lport = qemu_get_be16(f);
+    so->so_iptos = qemu_get_byte(f);
+    so->so_emu = qemu_get_byte(f);
+    so->so_type = qemu_get_byte(f);
+    so->so_state = qemu_get_be32(f);
+    if (slirp_sbuf_load(f, &so->so_rcv) < 0)
+        return -ENOMEM;
+    if (slirp_sbuf_load(f, &so->so_snd) < 0)
+        return -ENOMEM;
+    slirp_tcp_load(f, so->so_tcpcb);
+
+    return 0;
+}
+
+static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
+{
+    struct ex_list *ex_ptr;
+    int r;
+
+    while ((r = qemu_get_byte(f))) {
+        int ret;
+        struct socket *so = socreate();
+
+        if (!so)
+            return -ENOMEM;
+
+        ret = slirp_socket_load(f, so);
+
+        if (ret < 0)
+            return ret;
+
+        if ((so->so_faddr.s_addr & htonl(0xffffff00)) != special_addr.s_addr)
+            return -EINVAL;
+
+        for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
+            if (ex_ptr->ex_pty == 3 &&
+                    (ntohl(so->so_faddr.s_addr) & 0xff) == ex_ptr->ex_addr &&
+                    so->so_fport == ex_ptr->ex_fport)
+                break;
+
+        if (!ex_ptr)
+            return -EINVAL;
+
+        so->extra = ex_ptr->ex_exec;
+    }
+
+    return 0;
+}
#9 lore raw
In reply to Gleb Natapov
Quoted 8 lines · Gleb Natapov

Windows compilation should be fixed now.

Trap TCP connection to special IP/port inside slirp and redirect its traffic to a qemu character device. Add option to prevent connections through slirp to outside world. Add migration support for "special" sockets. This allow slirp to be used for secure communication between host and guest management agents.

Applied everything but #2. I'm not necessarily opposed to #2 but I wanted to see what you thought about making the syntax a bit more generic.

Thanks.

Regards,

Anthony Liguori

Quoted 27 lines

---

Gleb Natapov (5):
Add support for vmchannel socket migration. Add "restrict" and "ip" option to "user" net option Add slirp_restrict option.
Add vmchannel command line option.
Redirect slirp traffic to/from qemu character device.

 net.c             |   14 ++
 slirp/bootp.c     |   22 ++--
 slirp/ip_input.c  |   21 ++++
 slirp/libslirp.h  |    7 +
 slirp/main.h      |    3 +
 slirp/misc.c      |    2 
 slirp/sbuf.c      |    2 
 slirp/slirp.c     |  300 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 slirp/socket.c    |  101 ++++++++++++++----
 slirp/socket.h    |    2 
 slirp/tcp_input.c |   11 ++
 slirp/tcp_subr.c  |    5 +
 slirp/udp.c       |    3 +
 vl.c              |   75 +++++++++++++
 14 files changed, 525 insertions(+), 43 deletions(-)
#10 lore raw
Quoted 10 lines · Anthony Liguori
Quoted 8 lines · Gleb Natapov

Windows compilation should be fixed now.

Trap TCP connection to special IP/port inside slirp and redirect its traffic to a qemu character device. Add option to prevent connections through slirp to outside world. Add migration support for "special" sockets. This allow slirp to be used for secure communication between host and guest management agents.

Quoted 4 lines

Applied everything but #2. I'm not necessarily opposed to #2 but I wanted to see what you thought about making the syntax a bit more generic.

I think the syntax you propose make sense. I'll send a new patch.

--
Gleb.