slirp: add SOCKS5 support
cover slirp: add SOCKS5 support
This patch implements the SOCKS5 client part for "-net user" backend.
It allows to route all internet traffic of the virtual machine to a SOCKS5 server.
But all the local traffic (to the host) is sent to the host. It is needed because this SOCKS5 client doesn't route UDP traffic, and this allows to use the host DNS server.
I've tested this using public SOCKS5 proxy list found on the WEB, and using TOR server on my host.
Used with TOR, all the TCP connections are sent to the TOR network and this allows to insert a virtual machine directly in the TOR network without needing more configuration in the virtual machine.
But be aware that all DNS requests will be sent to the host that can forward them to internet with its own IP address. So confidentiality will not be as good as with the TOR browser which hides in the TOR network all the DNS requests.
If you want to test this:
- with a public SOCKS5 server, ask google for "socks5 proxy address" and start QEMU with, for instance:
qemu-system-x86_64 -net nic,model=e1000 -net user,proxy-server=46.105.121.37:63066 ...
if needed, you can provide user/password with "-net user,...,proxy-user=user,proxy-passwd=passwd"
- with a local TOR proxy:
sudo systemctl start tor
qemu-system-x86_64 -net nic,model=e1000 -net user,proxy-server=localhost:9050 ...
You can check your IP address is the one of the proxy by connecting to http://check.torproject.org with a browser inside the VM.
Laurent Vivier (1):
slirp: add SOCKS5 support
net/slirp.c | 30 +++++- qapi-schema.json | 3 + qemu-options.hx | 11 ++ slirp/Makefile.objs | 2 +- slirp/ip_icmp.c | 2 +- slirp/libslirp.h | 3 + slirp/slirp.c | 66 +++++++++++- slirp/slirp.h | 6 ++ slirp/socket.h | 4 + slirp/socks5.c | 283 ++++++++++++++++++++++++++++++++++++++++++++++++++++ slirp/socks5.h | 85 ++++++++++++++++ slirp/tcp_subr.c | 21 +++- slirp/udp.c | 9 ++ slirp/udp6.c | 2 +- 14 files changed, 515 insertions(+), 12 deletions(-) create mode 100644 slirp/socks5.c create mode 100644 slirp/socks5.h
Signature · 1 line
2.9.3
PATCH 1/1 slirp: add SOCKS5 support
When the VM is used behind a firewall, This allows to use a SOCKS5 proxy server to connect the VM IP stack directly to the Internet.
This implementation doesn't manage UDP packets, so they are simply dropped (as with restrict=on), except for the localhost as we need it for DNS.
--- net/slirp.c | 30 +++++- qapi-schema.json | 3 + qemu-options.hx | 11 ++ slirp/Makefile.objs | 2 +- slirp/ip_icmp.c | 2 +- slirp/libslirp.h | 3 + slirp/slirp.c | 66 +++++++++++- slirp/slirp.h | 6 ++ slirp/socket.h | 4 + slirp/socks5.c | 283 ++++++++++++++++++++++++++++++++++++++++++++++++++++ slirp/socks5.h | 85 ++++++++++++++++ slirp/tcp_subr.c | 21 +++- slirp/udp.c | 9 ++ slirp/udp6.c | 2 +- 14 files changed, 515 insertions(+), 12 deletions(-) create mode 100644 slirp/socks5.c create mode 100644 slirp/socks5.h
net/slirp.c +28 -2
@@ -139,6 +139,26 @@ static void net_slirp_cleanup(NetClientState *nc)QTAILQ_REMOVE (&slirp_stacks, s, entry); } +static int net_slirp_add_proxy (SlirpState *s,const char *proxy_server, +const char *proxy_user,const char *proxy_passwd) +{ +InetSocketAddress *addr; +int ret; + +if (proxy_server ==NULL ) { +return 0 ; + } + + addr =inet_parse (proxy_server, &error_fatal); + + ret =slirp_add_proxy (s->slirp, addr->host,atoi (addr->port), + proxy_user, proxy_passwd); + +qapi_free_InetSocketAddress (addr); + +return ret; +} +static NetClientInfo net_slirp_info = { .type =NET_CLIENT_DRIVER_USER , .size =sizeof (SlirpState),
@@ -155,7 +175,8 @@ static int net_slirp_init(NetClientState *peer, const char *model, const char *bootfile, const char *vdhcp_start, const char *vnameserver, const char *vnameserver6, const char *smb_export, const char *vsmbserver, - const char **dnssearch) + const char **dnssearch, const char *proxy_server, + const char *proxy_user, const char *proxy_passwd) { /* default settings according to historic slirp */ struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
@@ -361,6 +382,10 @@ static int net_slirp_init(NetClientState *peer, const char *model, }#endif +if (net_slirp_add_proxy (s, proxy_server, proxy_user, proxy_passwd) <0 ) { + goto error; + } + s->exit_notifier.notify = slirp_smb_exit;qemu_add_exit_notifier (&s->exit_notifier);return 0 ;
@@ -878,7 +903,8 @@ int net_init_slirp(const Netdev *netdev, const char *name, user->ipv6_host, user->hostname, user->tftp, user->bootfile, user->dhcpstart, user->dns, user->ipv6_dns, user->smb, - user->smbserver, dnssearch); + user->smbserver, dnssearch, user->proxy_server, + user->proxy_user, user->proxy_passwd);while (slirp_configs) { config = slirp_configs;
qapi-schema.json +3 -0
@@ -3680,6 +3680,9 @@ '*ipv6-dns': 'str', '*smb': 'str', '*smbserver': 'str', + '*proxy-server': 'str', + '*proxy-user': 'str', + '*proxy-passwd': 'str', '*hostfwd': ['String'], '*guestfwd': ['String'] } }
qemu-options.hx +11 -0
@@ -1645,6 +1645,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, #ifndef _WIN32 "[,smb=dir[,smbserver=addr]]\n" #endif + " [,proxy-server=addr:port[,proxy-user=user,proxy-passwd=passwd]]\n" " configure a user mode network backend with ID 'str',\n" " its DHCP server and optional services\n" #endif
@@ -1883,6 +1884,16 @@ Note that a SAMBA server must be installed on the host OS. QEMU was tested successfully with smbd versions from Red Hat 9, Fedora Core 3 and OpenSUSE 11.x. +@item proxy-server=@var{addr}:@var{port}[,proxy-user=@var{user},proxy-passwd=@var{passwd}]] +If you provide a SOCKS5 proxy server address @var{addr} and a port number @var{port}, +QEMU will use it to connect to Internet. If the proxy server needs an user id and a password +the values are provided with proxy-user and proxy-passwd. + +For example, to connect to a TOR proxy server on the host, use the following: +@example +qemu-system-i386 -net user,proxy-server=localhost:9050 +@end example + @item hostfwd=[tcp|udp]:[@var{hostaddr}]:@var{hostport}-[@var{guestaddr}]:@var{guestport} Redirect incoming TCP or UDP connections to the host port @var{hostport} to the guest IP address @var{guestaddr} on guest port @var{guestport}. If
slirp/Makefile.objs +1 -1
@@ -2,4 +2,4 @@ common-obj-y = cksum.o if.o ip_icmp.o ip6_icmp.o ip6_input.o ip6_output.o \ ip_input.o ip_output.o dnssearch.o dhcpv6.o common-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o common-obj-y += tcp_subr.o tcp_timer.o udp.o udp6.o bootp.o tftp.o arp_table.o \ - ndp_table.o + ndp_table.o socks5.o
slirp/ip_icmp.c +1 -1
@@ -154,7 +154,7 @@ icmp_input(struct mbuf *m, int hlen) ip->ip_len += hlen;/* since ip_input subtracts this */ if (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) {icmp_reflect (m); - }else if (slirp->restricted) { + }else if (slirp->restricted || slirp->proxy_server) { goto freeit; }else {struct socket *so;
slirp/libslirp.h +3 -0
@@ -26,6 +26,9 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error);void slirp_input (Slirp *slirp,const uint8_t *pkt,int pkt_len); +int slirp_add_proxy (Slirp *slirp,const char *server,int port, +const char *user,const char *passwd); +/* you must provide the following functions: */ void slirp_output (void *opaque,const uint8_t *pkt,int pkt_len);
slirp/slirp.c +62 -4
97 lines not on this page · raw message
slirp/slirp.h +6 -0
@@ -214,6 +214,12 @@ struct Slirp {char *tftp_prefix;struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX ]; +/* proxy */ +char *proxy_server; +int proxy_port; +char *proxy_user; +char *proxy_passwd; +ArpTable arp_table;NdpTable ndp_table;
slirp/socket.h +4 -0
@@ -8,6 +8,8 @@#ifndef SLIRP_SOCKET_H #define SLIRP_SOCKET_H +#include "socks5.h" +#define SO_EXPIRE 240000#define SO_EXPIREFAST 10000
@@ -70,6 +72,8 @@ struct socket {struct sbuf so_rcv;/* Receive buffer */ struct sbuf so_snd;/* Send buffer */ void * extra;/* Extra pointer */ + +socks5_state_t so_proxy_state; };
slirp/socks5.c new file +283 -0
283 lines not on this page · raw message
slirp/socks5.h new file +85 -0
85 lines not on this page · raw message
slirp/tcp_subr.c +18 -3
@@ -40,6 +40,7 @@#include "qemu/osdep.h" #include "slirp.h" +#include "socks5.h" /* patchable/settable parameters for tcp */ /* Don't do rfc1323 performance enhancements */
@@ -394,11 +395,21 @@ tcp_sockclosed(struct tcpcb *tp)int tcp_fconnect (struct socket *so,unsigned short af) {int ret=0 ; +Slirp *slirp = so->slirp;DEBUG_CALL ("tcp_fconnect" );DEBUG_ARG ("so = %p" , so); - ret = so->s =qemu_socket (af,SOCK_STREAM ,0 ); +/* local traffic doesn't go to the proxy */ +if (slirp->proxy_server && + !(af ==AF_INET && + (so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) == + slirp->vnetwork_addr.s_addr)) { + ret = so->s =socks5_socket (&so->so_proxy_state); + }else { + ret = so->s =qemu_socket (af,SOCK_STREAM ,0 ); + } +if (ret >=0 ) {int opt, s=so->s;struct sockaddr_storage addr;
@@ -413,8 +424,12 @@ int tcp_fconnect(struct socket *so, unsigned short af)sotranslate_out (so, &addr);/* We don't care what port we get */ - ret =connect (s, (struct sockaddr *)&addr,sockaddr_size (&addr)); - +if (so->so_proxy_state) { + ret =socks5_connect (s, slirp->proxy_server, slirp->proxy_port, + &so->so_proxy_state); + }else { + ret =connect (s, (struct sockaddr *)&addr,sockaddr_size (&addr)); + }/* * If it's not in progress, it failed, so we just return 0, * without clearing SS_NOFDREF
slirp/udp.c +9 -0
@@ -160,6 +160,15 @@ udp_input(register struct mbuf *m, int iphlen) goto bad; } +/* as our SOCKS5 client is not able to route UDP packets, +* only allow local UDP traffic (we need it for DNS) +*/ +if (slirp->proxy_server && + (ip->ip_dst.s_addr & slirp->vnetwork_mask.s_addr) != + slirp->vnetwork_addr.s_addr) { + goto bad; + } +/* * Locate pcb for datagram. */
slirp/udp6.c +1 -1
@@ -22,7 +22,7 @@ void udp6_input(struct mbuf *m)DEBUG_CALL ("udp6_input" );DEBUG_ARG ("m = %lx" , (long )m); -if (slirp->restricted) { +if (slirp->restricted || slirp->proxy_server) { goto bad; }
Signature · 1 line
2.9.3
Quoted 2 lines · Laurent Vivier
When the VM is used behind a firewall, This allows to use a SOCKS5 proxy server to connect the VM IP stack
"allows to $verb" is not idiomatic English; the correct forms are generally "allows $subject to $verb" or "allows ${verb}ing". In this case, I'd lean towards "this allows the use of a SOCKS5 proxy server"
Quoted 11 lines
directly to the Internet.
This implementation doesn't manage UDP packets, so they are simply dropped (as with restrict=on), except for the localhost as we need it for DNS.
Signed-off-by Laurent Vivier <[email protected]>---
+++
b/qapi-schema.json
qapi-schema.json line 3685
Quoted 6 lines
qapi-schema.json +3
-0@@ -3680,6 +3680,9 @@ '*ipv6-dns': 'str', '*smb': 'str', '*smbserver': 'str', + '*proxy-server': 'str', + '*proxy-user': 'str', + '*proxy-passwd': 'str',
Why can't we spell this out as password, instead of abbreviating? Should this hook into the "secrets object" framework so that someone does not have to pass the password in plaintext?
qapi-schema.json line 3687
Quoted 2 lines
qapi-schema.json +0
-0'*hostfwd': ['String'], '*guestfwd': ['String'] } }
Missing documentation.
Do we want all three proxy elements to be in a substruct? The difference is between:
{ ... "smb": "foo", "proxy-server": "bar", "proxy-user": "noone", "proxy-passwd": "hello" }
and a substruct:
{ ... "smb": "foo", "proxy": { "server": "bar", "user", "noone", "passwd": "hello" } }
qemu-options.hx Note that a SAMBA server must be installed on the host OS. · line 1887
Quoted 2 lines
qemu-options.hx +1
-0+@item proxy-server=@var{addr}:@var{port}[,proxy-user=@var{user},proxy-passwd=@var{passwd}]]
Yes, you DEFINITELY need to hook into the "secrets object" framework to avoid having to pass a password in plaintext on the command line. Dan Berrange may have more advice on doing that.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 7 lines · Eric Blake
Quoted 2 lines · Laurent Vivier
When the VM is used behind a firewall, This allows to use a SOCKS5 proxy server to connect the VM IP stack
"allows to $verb" is not idiomatic English; the correct forms are generally "allows $subject to $verb" or "allows ${verb}ing". In this case, I'd lean towards "this allows the use of a SOCKS5 proxy server"
Thank you, and sorry.
Quoted 10 lines
Quoted 9 lines
directly to the Internet.
This implementation doesn't manage UDP packets, so they are simply dropped (as with restrict=on), except for the localhost as we need it for DNS.
Signed-off-by Laurent Vivier <[email protected]>---
Quoted 10 lines
qapi-schema.json line 3685
Quoted 8 lines
+++
b/qapi-schema.jsonqapi-schema.json +3
-0@@ -3680,6 +3680,9 @@ '*ipv6-dns': 'str', '*smb': 'str', '*smbserver': 'str', + '*proxy-server': 'str', + '*proxy-user': 'str', + '*proxy-passwd': 'str',Why can't we spell this out as password, instead of abbreviating?
because of unix command "passwd" ;) . I will fix.
Quoted 7 lines
Should this hook into the "secrets object" framework so that someone does not have to pass the password in plaintext?
qapi-schema.json line 3687
Quoted 2 lines
qapi-schema.json +0
-0'*hostfwd': ['String'], '*guestfwd': ['String'] } }Missing documentation.
I will fix.
Quoted 10 lines
Do we want all three proxy elements to be in a substruct? The difference is between:
{ ... "smb": "foo", "proxy-server": "bar", "proxy-user": "noone", "proxy-passwd": "hello" }
and a substruct:
{ ... "smb": "foo", "proxy": { "server": "bar", "user", "noone", "passwd": "hello" } }
yes, substruct looks better.
Quoted 8 lines
qemu-options.hx Note that a SAMBA server must be installed on the host OS. · line 1887
Quoted 3 lines
qemu-options.hx +1
-0+@item proxy-server=@var{addr}:@var{port}[,proxy-user=@var{user},proxy-passwd=@var{passwd}]]Yes, you DEFINITELY need to hook into the "secrets object" framework to avoid having to pass a password in plaintext on the command line. Dan Berrange may have more advice on doing that.
OK
Thank you for the review.
Laurent
Quoted 17 lines · Eric Blake
Quoted 2 lines · Laurent Vivier
When the VM is used behind a firewall, This allows to use a SOCKS5 proxy server to connect the VM IP stack
"allows to $verb" is not idiomatic English; the correct forms are generally "allows $subject to $verb" or "allows ${verb}ing". In this case, I'd lean towards "this allows the use of a SOCKS5 proxy server"
Quoted 8 lines
directly to the Internet.
This implementation doesn't manage UDP packets, so they are simply dropped (as with restrict=on), except for the localhost as we need it for DNS.
Signed-off-by Laurent Vivier <[email protected]>---
Quoted 12 lines
qapi-schema.json line 3685
Quoted 8 lines
+++
b/qapi-schema.jsonqapi-schema.json +3
-0@@ -3680,6 +3680,9 @@ '*ipv6-dns': 'str', '*smb': 'str', '*smbserver': 'str', + '*proxy-server': 'str', + '*proxy-user': 'str', + '*proxy-passwd': 'str',Why can't we spell this out as password, instead of abbreviating? Should this hook into the "secrets object" framework so that someone does not have to pass the password in plaintext?
Yes.
Quoted 5 lines
qemu-options.hx Note that a SAMBA server must be installed on the host OS. · line 1887
Quoted 1 line
qemu-options.hx +1
-0+@item proxy-server=@var{addr}:@var{port}[,proxy-user=@var{user},proxy-passwd=@var{passwd}]]Yes, you DEFINITELY need to hook into the "secrets object" framework to avoid having to pass a password in plaintext on the command line. Dan Berrange may have more advice on doing that.
Agreed, this needs to use the secrets framework.
Rename 'proxy-password' to 'proxy-password-secret'. It'll provide the ID of a secret's object. Given that you can use qcrypto_secret_lookup_as_utf8() to get the associated password data. There's a few examples in the code eg crypto/tlscredsx509.c is a fairly simple example. Ping me if you want more help
Regards,
Daniel
Signature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|
Quoted 39 lines · Daniel P. Berrange
Quoted 17 lines · Eric Blake
Quoted 2 lines · Laurent Vivier
When the VM is used behind a firewall, This allows to use a SOCKS5 proxy server to connect the VM IP stack
"allows to $verb" is not idiomatic English; the correct forms are generally "allows $subject to $verb" or "allows ${verb}ing". In this case, I'd lean towards "this allows the use of a SOCKS5 proxy server"
Quoted 8 lines
directly to the Internet.
This implementation doesn't manage UDP packets, so they are simply dropped (as with restrict=on), except for the localhost as we need it for DNS.
Signed-off-by Laurent Vivier <[email protected]>---
Quoted 12 lines
qapi-schema.json line 3685
Quoted 8 lines
+++
b/qapi-schema.jsonqapi-schema.json +3
-0@@ -3680,6 +3680,9 @@ '*ipv6-dns': 'str', '*smb': 'str', '*smbserver': 'str', + '*proxy-server': 'str', + '*proxy-user': 'str', + '*proxy-passwd': 'str',Why can't we spell this out as password, instead of abbreviating? Should this hook into the "secrets object" framework so that someone does not have to pass the password in plaintext?
Yes.
Quoted 5 lines
qemu-options.hx Note that a SAMBA server must be installed on the host OS. · line 1887
Quoted 1 line
qemu-options.hx +1
-0+@item proxy-server=@var{addr}:@var{port}[,proxy-user=@var{user},proxy-passwd=@var{passwd}]]Yes, you DEFINITELY need to hook into the "secrets object" framework to avoid having to pass a password in plaintext on the command line. Dan Berrange may have more advice on doing that.
Quoted 7 lines
Agreed, this needs to use the secrets framework.
Rename 'proxy-password' to 'proxy-password-secret'. It'll provide the ID of a secret's object. Given that you can use
qcrypto_secret_lookup_as_utf8()to get the associated password data. There's a few examples in the code egcrypto/tlscredsx509.cis a fairly simple example. Ping me if you want more help
Please see the v2: https://patchwork.ozlabs.org/patch/744497/
I forgot the to cc' you and Eric.
Laurent