Skip to contentSkip to conversation

slirp: Adding IPv6 support to Qemu -net user mode

PATCH Superseded Samuel Thibault 6 participants 51 messages Feb 22, 2016 – Mar 8, 2016 Networking

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v9 cover slirp: Adding IPv6 support to Qemu -net user mode

Hello,

This is another respin of IPv6 in Qemu -net user mode.

These patches add ICMPv6, NDP, make UDP and TCP compatible with IPv6, and add TFTP over IPv6.

Difference with version 8 is:
- Fix freeing random number generator
- Fix coding style
- Fix prefix lengths in in6_equal_dns macro

Here is a summary of the patches:
Guillaume Subiron (7):
slirp: Adding IPv6, ICMPv6 Echo and NDP autoconfiguration
slirp: Adding IPv6 UDP support
slirp: Factorizing tcpiphdr structure with an union
slirp: Generalizing and neutralizing various TCP functions before
adding IPv6 stuff
slirp: Reindent after refactoring
slirp: Handle IPv6 in TCP functions
slirp: Adding IPv6 address for DNS relay

Thomas Huth (1):
slirp: Add IPv6 support to the TFTP code

Yann Bordenave (2):
slirp: Adding ICMPv6 error sending
qapi-schema, qemu-options & slirp: Adding Qemu options for IPv6
addresses

 net/net.c           |  31 ++++
 net/slirp.c         |  69 ++++++++-
 qapi-schema.json    |  40 +++--
 qemu-options.hx     |  18 ++-
 slirp/Makefile.objs |   6 +-
 slirp/cksum.c       |  25 ++++
 slirp/if.c          |   2 +-
 slirp/if.h          |   4 +-
 slirp/ip6.h         | 142 ++++++++++++++++++
 slirp/ip6_icmp.c    | 411 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 slirp/ip6_icmp.h    | 213 +++++++++++++++++++++++++++
 slirp/ip6_input.c   |  73 ++++++++++
 slirp/ip6_output.c  |  38 +++++
 slirp/ip_icmp.c     |  12 +-
 slirp/ip_icmp.h     |   4 +-
 slirp/ip_input.c    |  10 +-
 slirp/libslirp.h    |   8 +-
 slirp/mbuf.c        |   4 +-
 slirp/ndp_table.c   |  84 +++++++++++
 slirp/slirp.c       |  81 +++++++++--
 slirp/slirp.h       |  43 +++++-
 slirp/socket.c      |  54 ++++++-
 slirp/socket.h      |  13 ++
 slirp/tcp.h         |   2 +
 slirp/tcp_input.c   | 181 ++++++++++++++++-------
 slirp/tcp_output.c  |  51 +++++--
 slirp/tcp_subr.c    | 114 +++++++++++----
 slirp/tcp_timer.c   |   3 +-
 slirp/tcpip.h       |  40 ++++-
 slirp/tftp.c        | 133 +++++++++--------
 slirp/tftp.h        |   7 +-
 slirp/udp.c         |  19 ++-
 slirp/udp.h         |   5 +
 slirp/udp6.c        | 167 +++++++++++++++++++++
 34 files changed, 1877 insertions(+), 230 deletions(-)
 create mode 100644 slirp/ip6.h
 create mode 100644 slirp/ip6_icmp.c
 create mode 100644 slirp/ip6_icmp.h
 create mode 100644 slirp/ip6_input.c
 create mode 100644 slirp/ip6_output.c
 create mode 100644 slirp/ndp_table.c
 create mode 100644 slirp/udp6.c
Signature · 1 line
2.7.0
#20 lore raw

PATCH v9 8/10 slirp: Adding IPv6 address for DNS relay

From: Guillaume Subiron <[email protected]>

This patch adds an IPv6 address to the DNS relay. in6_equal_dns() is developed using this Slirp attribute. sotranslate_in/out/accept() are also updated to manage the IPv6 case so the guest can be able to join the host using one of the Slirp addresses.

For now this only points to localhost. Further development will be needed to automatically fetch the IPv6 address from resolv.conf, and announce this via RDNSS.

---
 slirp/ip6.h    |  6 +++++-
 slirp/slirp.c  |  1 +
 slirp/slirp.h  |  1 +
 slirp/socket.c | 32 ++++++++++++++++++++++++++++++++
 4 files changed, 39 insertions(+), 1 deletion(-)
slirp/ip6.h +5 -1
index 55e52a1..c2df8d3 100644
@@ -73,7 +73,11 @@ static inline bool in6_equal_mach(const struct in6_addr *a,
   || (in6_equal_net(a, &(struct in6_addr)LINKLOCAL_ADDR, 64)\
       && in6_equal_mach(a, &slirp->vhost_addr6, 64)))
 
-#define in6_equal_dns(a) 0
+#define in6_equal_dns(a)\
+    ((in6_equal_net(a, &slirp->vprefix_addr6, slirp->vprefix_len)\
+      && in6_equal_mach(a, &slirp->vnameserver_addr6, slirp->vprefix_len))\
+  || (in6_equal_net(a, &(struct in6_addr)LINKLOCAL_ADDR, 64)\
+      && in6_equal_mach(a, &slirp->vnameserver_addr6, 64)))
 
 #define in6_equal_host(a)\
     (in6_equal_router(a) || in6_equal_dns(a))
slirp/slirp.c +1 -0
index d908422..a201a5a 100644
@@ -234,6 +234,7 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
     slirp->bootp_filename = g_strdup(bootfile);
     slirp->vdhcp_startaddr = vdhcp_start;
     slirp->vnameserver_addr = vnameserver;
+    inet_pton(AF_INET6, "fec0::3", &slirp->vnameserver_addr6);
 
     if (vdnssearch) {
         translate_dnssearch(slirp, vdnssearch);
slirp/slirp.h +1 -0
index 967d93e..a830353 100644
@@ -232,6 +232,7 @@ struct Slirp {
     struct in6_addr vhost_addr6;
     struct in_addr vdhcp_startaddr;
     struct in_addr vnameserver_addr;
+    struct in6_addr vnameserver_addr6;
 
     struct in_addr client_ipaddr;
     char client_hostname[33];
slirp/socket.c +32 -0
index d4b02c8..0661fa9 100644
@@ -747,6 +747,7 @@ void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
 {
     Slirp *slirp = so->slirp;
     struct sockaddr_in *sin = (struct sockaddr_in *)addr;
+    struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
 
     switch (addr->ss_family) {
     case AF_INET:
@@ -767,6 +768,19 @@ void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
             ntohs(sin->sin_port), inet_ntoa(sin->sin_addr)));
         break;
 
+    case AF_INET6:
+        if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6,
+                    slirp->vprefix_len)) {
+            if (in6_equal(&so->so_faddr6, &slirp->vnameserver_addr6)) {
+                /*if (get_dns_addr(&addr) < 0) {*/ /* TODO */
+                    sin6->sin6_addr = in6addr_loopback;
+                /*}*/
+            } else {
+                sin6->sin6_addr = in6addr_loopback;
+            }
+        }
+        break;
+
     default:
         break;
     }
@@ -776,6 +790,7 @@ void sotranslate_in(struct socket *so, struct sockaddr_storage *addr)
 {
     Slirp *slirp = so->slirp;
     struct sockaddr_in *sin = (struct sockaddr_in *)addr;
+    struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
 
     switch (addr->ss_family) {
     case AF_INET:
@@ -792,6 +807,16 @@ void sotranslate_in(struct socket *so, struct sockaddr_storage *addr)
         }
         break;
 
+    case AF_INET6:
+        if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6,
+                    slirp->vprefix_len)) {
+            if (in6_equal(&sin6->sin6_addr, &in6addr_loopback)
+                    || !in6_equal(&so->so_faddr6, &slirp->vhost_addr6)) {
+                sin6->sin6_addr = so->so_faddr6;
+            }
+        }
+        break;
+
     default:
         break;
     }
@@ -813,6 +838,13 @@ void sotranslate_accept(struct socket *so)
         }
         break;
 
+   case AF_INET6:
+        if (in6_equal(&so->so_faddr6, &in6addr_any) ||
+                in6_equal(&so->so_faddr6, &in6addr_loopback)) {
+           so->so_faddr6 = slirp->vhost_addr6;
+        }
+        break;
+
     default:
         break;
     }
Signature · 1 line
2.7.0
#22 lore raw

PATCH v9 9/10 qapi-schema, qemu-options & slirp: Adding Qemu options for IPv6 addresses

In reply to Samuel Thibault

From: Yann Bordenave <[email protected]>

This patch adds parameters to manage some new options in the qemu -net command.
Slirp IPv6 address, network prefix, and DNS IPv6 address can be given in argument to the qemu command.
Defaults parameters are respectively fec0::2, fec0::, /64 and fec0::3.

---
 net/net.c        | 31 +++++++++++++++++++++++++
 net/slirp.c      | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 qapi-schema.json | 40 ++++++++++++++++++++------------
 qemu-options.hx  | 18 +++++++++++++--
 slirp/libslirp.h |  8 ++++---
 slirp/slirp.c    | 16 +++++++------
 6 files changed, 150 insertions(+), 32 deletions(-)
net/net.c +31 -0
index aebf753..d67904f 100644
@@ -1060,6 +1060,37 @@ int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
     int ret = -1;
 
     {
+        /* Parse convenience option format ip6-net=fec0::0[/64] */
+        const char *ip6_net = qemu_opt_get(opts, "ip6-net");
+
+        if (ip6_net) {
+            char buf[strlen(ip6_net) + 1];
+
+            if (get_str_sep(buf, sizeof(buf), &ip6_net, '/') < 0) {
+                /* Default 64bit prefix length.  */
+                qemu_opt_set(opts, "ip6-prefix", ip6_net, &error_abort);
+                qemu_opt_set_number(opts, "ip6-prefixlen", 64, &error_abort);
+            } else {
+                /* User-specified prefix length.  */
+                unsigned long len;
+                int err;
+
+                qemu_opt_set(opts, "ip6-prefix", buf, &error_abort);
+                err = qemu_strtoul(ip6_net, NULL, 10, &len);
+
+                if (err) {
+                    error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
+                              "ip6-prefix", "a number");
+                } else {
+                    qemu_opt_set_number(opts, "ip6-prefixlen", len,
+                                        &error_abort);
+                }
+            }
+            qemu_opt_unset(opts, "ip6-net");
+        }
+    }
+
+    {
         OptsVisitor *ov = opts_visitor_new(opts);
 
         net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
net/slirp.c +63 -6

107 lines not on this page · raw message

qapi-schema.json +26 -14
index 8d04897..a62c2ec 100644
@@ -2399,6 +2399,14 @@
 # @dnssearch: #optional list of DNS suffixes to search, passed as DHCP option
 #             to the guest
 #
+# @ip6-prefix: #optional IPv6 network prefix (default is fec0::) (since 2.6)
+#
+# @ip6-prefixlen: #optional IPv6 network prefix length (default is 64) (since 2.6)
+#
+# @ip6-host: #optional guest-visible IPv6 address of the host (since 2.6)
+#
+# @ip6-dns: #optional guest-visible IPv6 address of the virtual nameserver (since 2.6)
+#
 # @smb: #optional root directory of the built-in SMB server
 #
 # @smbserver: #optional IP address of the built-in SMB server
@@ -2412,20 +2420,24 @@
 ##
 { 'struct': 'NetdevUserOptions',
   'data': {
-    '*hostname':  'str',
-    '*restrict':  'bool',
-    '*ip':        'str',
-    '*net':       'str',
-    '*host':      'str',
-    '*tftp':      'str',
-    '*bootfile':  'str',
-    '*dhcpstart': 'str',
-    '*dns':       'str',
-    '*dnssearch': ['String'],
-    '*smb':       'str',
-    '*smbserver': 'str',
-    '*hostfwd':   ['String'],
-    '*guestfwd':  ['String'] } }
+    '*hostname':        'str',
+    '*restrict':        'bool',
+    '*ip':              'str',
+    '*net':             'str',
+    '*host':            'str',
+    '*tftp':            'str',
+    '*bootfile':        'str',
+    '*dhcpstart':       'str',
+    '*dns':             'str',
+    '*dnssearch':       ['String'],
+    '*ip6-prefix':      'str',
+    '*ip6-prefixlen':   'int',
+    '*ip6-host':        'str',
+    '*ip6-dns':         'str',
+    '*smb':             'str',
+    '*smbserver':       'str',
+    '*hostfwd':         ['String'],
+    '*guestfwd':        ['String'] } }
 
 ##
 # @NetdevTapOptions
qemu-options.hx +16 -2
index 2f0465e..a2c1214 100644
@@ -1546,8 +1546,9 @@ DEF("smb", HAS_ARG, QEMU_OPTION_smb, "", QEMU_ARCH_ALL)
 
 DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
 #ifdef CONFIG_SLIRP
-    "-netdev user,id=str[,net=addr[/mask]][,host=addr][,restrict=on|off]\n"
-    "         [,hostname=host][,dhcpstart=addr][,dns=addr][,dnssearch=domain][,tftp=dir]\n"
+    "-netdev user,id=str[,net=addr[/mask]][,host=addr][,ip6-net=addr[/int]]\n"
+    "         [,ip6-host=addr][,restrict=on|off][,hostname=host][,dhcpstart=addr]\n"
+    "         [,dns=addr][,ip6-dns=addr][,dnssearch=domain][,tftp=dir]\n"
     "         [,bootfile=f][,hostfwd=rule][,guestfwd=rule]"
 #ifndef _WIN32
                                              "[,smb=dir[,smbserver=addr]]\n"
@@ -1704,6 +1705,14 @@ either in the form a.b.c.d or as number of valid top-most bits. Default is
 Specify the guest-visible address of the host. Default is the 2nd IP in the
 guest network, i.e. x.x.x.2.
 
+@item ip6-net=@var{addr}[/@var{int}]
+Set IPv6 network address the guest will see. Optionally specify the prefix
+size, as number of valid top-most bits. Default is fec0::/64.
+
+@item ip6-host=@var{addr}
+Specify the guest-visible IPv6 address of the host. Default is the 2nd IPv6 in
+the guest network, i.e. xxxx::2.
+
 @item restrict=on|off
 If this option is enabled, the guest will be isolated, i.e. it will not be
 able to contact the host and no guest IP packets will be routed over the host
@@ -1721,6 +1730,11 @@ Specify the guest-visible address of the virtual nameserver. The address must
 be different from the host address. Default is the 3rd IP in the guest network,
 i.e. x.x.x.3.
 
+@item ip6-dns=@var{addr}
+Specify the guest-visible address of the IPv6 virtual nameserver. The address
+must be different from the host address. Default is the 3rd IP in the guest
+network, i.e. xxxx::3.
+
 @item dnssearch=@var{domain}
 Provides an entry for the domain-search list sent by the built-in
 DHCP server. More than one domain suffix can be transmitted by specifying
slirp/libslirp.h +5 -3
index 5bdcbd5..c4b25c9 100644
@@ -10,9 +10,11 @@ int get_dns_addr(struct in_addr *pdns_addr);
 
 Slirp *slirp_init(int restricted, struct in_addr vnetwork,
                   struct in_addr vnetmask, struct in_addr vhost,
-                  const char *vhostname, const char *tftp_path,
-                  const char *bootfile, struct in_addr vdhcp_start,
-                  struct in_addr vnameserver, const char **vdnssearch,
+                  struct in6_addr vprefix_addr6, uint8_t vprefix_len,
+                  struct in6_addr vhost6, const char *vhostname,
+                  const char *tftp_path, const char *bootfile,
+                  struct in_addr vdhcp_start, struct in_addr vnameserver,
+                  struct in6_addr vnameserver6, const char **vdnssearch,
                   void *opaque);
 void slirp_cleanup(Slirp *slirp);
 
slirp/slirp.c +9 -7
index a201a5a..9ccf415 100644
@@ -201,9 +201,11 @@ static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
 
 Slirp *slirp_init(int restricted, struct in_addr vnetwork,
                   struct in_addr vnetmask, struct in_addr vhost,
-                  const char *vhostname, const char *tftp_path,
-                  const char *bootfile, struct in_addr vdhcp_start,
-                  struct in_addr vnameserver, const char **vdnssearch,
+                  struct in6_addr vprefix_addr6, uint8_t vprefix_len,
+                  struct in6_addr vhost6, const char *vhostname,
+                  const char *tftp_path, const char *bootfile,
+                  struct in_addr vdhcp_start, struct in_addr vnameserver,
+                  struct in6_addr vnameserver6, const char **vdnssearch,
                   void *opaque)
 {
     Slirp *slirp = g_malloc0(sizeof(Slirp));
@@ -223,9 +225,9 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
     slirp->vnetwork_addr = vnetwork;
     slirp->vnetwork_mask = vnetmask;
     slirp->vhost_addr = vhost;
-    inet_pton(AF_INET6, "fec0::0", &slirp->vprefix_addr6);
-    slirp->vprefix_len = 64;
-    inet_pton(AF_INET6, "fec0::2", &slirp->vhost_addr6);
+    slirp->vprefix_addr6 = vprefix_addr6;
+    slirp->vprefix_len = vprefix_len;
+    slirp->vhost_addr6 = vhost6;
     if (vhostname) {
         pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname),
                 vhostname);
@@ -234,7 +236,7 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
     slirp->bootp_filename = g_strdup(bootfile);
     slirp->vdhcp_startaddr = vdhcp_start;
     slirp->vnameserver_addr = vnameserver;
-    inet_pton(AF_INET6, "fec0::3", &slirp->vnameserver_addr6);
+    slirp->vnameserver_addr6 = vnameserver6;
 
     if (vdnssearch) {
         translate_dnssearch(slirp, vdnssearch);
Signature · 1 line
2.7.0
#8 lore raw

PATCH v9 3/10 slirp: Adding IPv6 UDP support

In reply to Samuel Thibault

From: Guillaume Subiron <[email protected]>

This adds the sin6 case in the fhost and lhost unions and related macros. It adds udp6_input() and udp6_output().
It adds the IPv6 case in sorecvfrom().
Finally, udp_input() is called by ip6_input().

---
 slirp/Makefile.objs |   2 +-
 slirp/ip6_input.c   |   2 +-
 slirp/socket.c      |   5 ++
 slirp/socket.h      |   6 ++
 slirp/udp.h         |   5 ++
 slirp/udp6.c        | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 175 insertions(+), 2 deletions(-)
 create mode 100644 slirp/udp6.c
slirp/Makefile.objs +1 -1
index 4e3a289..6748e4f 100644
@@ -1,5 +1,5 @@
 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
 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 bootp.o tftp.o arp_table.o \
+common-obj-y += tcp_subr.o tcp_timer.o udp.o udp6.o bootp.o tftp.o arp_table.o \
                 ndp_table.o
slirp/ip6_input.c +1 -1
index b6a438d..9ca6d32 100644
@@ -58,7 +58,7 @@ void ip6_input(struct mbuf *m)
         icmp6_send_error(m, ICMP6_UNREACH, ICMP6_UNREACH_NO_ROUTE);
         break;
     case IPPROTO_UDP:
-        icmp6_send_error(m, ICMP6_UNREACH, ICMP6_UNREACH_NO_ROUTE);
+        udp6_input(m);
         break;
     case IPPROTO_ICMPV6:
         icmp6_input(m);
slirp/socket.c +5 -0
index 32b1ba3..b79ddec 100644
@@ -541,7 +541,12 @@ sorecvfrom(struct socket *so)
 	                   (struct sockaddr_in *) &daddr,
 	                   so->so_iptos);
 	        break;
+	    case AF_INET6:
+	        udp6_output(so, m, (struct sockaddr_in6 *) &saddr,
+	                    (struct sockaddr_in6 *) &daddr);
+	        break;
 	    default:
+	        g_assert_not_reached();
 	        break;
 	    }
 	  } /* rx error */
slirp/socket.h +6 -0
index bcebce1..e9c9b05 100644
@@ -34,17 +34,23 @@ struct socket {
   union {   /* foreign host */
       struct sockaddr_storage ss;
       struct sockaddr_in sin;
+      struct sockaddr_in6 sin6;
   } fhost;
 #define so_faddr fhost.sin.sin_addr
 #define so_fport fhost.sin.sin_port
+#define so_faddr6 fhost.sin6.sin6_addr
+#define so_fport6 fhost.sin6.sin6_port
 #define so_ffamily fhost.ss.ss_family
 
   union {   /* local host */
       struct sockaddr_storage ss;
       struct sockaddr_in sin;
+      struct sockaddr_in6 sin6;
   } lhost;
 #define so_laddr lhost.sin.sin_addr
 #define so_lport lhost.sin.sin_port
+#define so_laddr6 lhost.sin6.sin6_addr
+#define so_lport6 lhost.sin6.sin6_port
 #define so_lfamily lhost.ss.ss_family
 
   uint8_t	so_iptos;	/* Type of service */
slirp/udp.h +5 -0
index 2f9de38..10cc780 100644
@@ -83,4 +83,9 @@ struct socket * udp_listen(Slirp *, uint32_t, u_int, uint32_t, u_int,
 int udp_output(struct socket *so, struct mbuf *m,
                 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
                 int iptos);
+
+void udp6_input(register struct mbuf *);
+int udp6_output(struct socket *so, struct mbuf *m,
+                struct sockaddr_in6 *saddr, struct sockaddr_in6 *daddr);
+
 #endif
slirp/udp6.c new file +157 -0

157 lines not on this page · raw message

Signature · 1 line
2.7.0
#28 lore raw

PATCH v9 10/10 slirp: Add IPv6 support to the TFTP code

In reply to Samuel Thibault

From: Thomas Huth <[email protected]>

Add the handler code for incoming TFTP packets to udp6_input(), and make sure that the TFTP code can send packets with both, udp_output() and udp6_output() by introducing a wrapper function called tftp_udp_output().

---
 slirp/tftp.c | 133 +++++++++++++++++++++++++++++++++--------------------------
 slirp/tftp.h |   7 ++--
 slirp/udp.c  |  16 ++++---
 slirp/udp6.c |  16 +++++--
 4 files changed, 100 insertions(+), 72 deletions(-)
slirp/tftp.c +74 -59
index abb0106..25ad6ef 100644
@@ -46,7 +46,8 @@ static void tftp_session_terminate(struct tftp_session *spt)
     spt->slirp = NULL;
 }
 
-static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
+static int tftp_session_allocate(Slirp *slirp, struct sockaddr_storage *srcsas,
+                                 struct tftp_t *tp)
 {
   struct tftp_session *spt;
   int k;
@@ -68,7 +69,7 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
 
  found:
   memset(spt, 0, sizeof(*spt));
-  memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
+  spt->client_addr = *srcsas;
   spt->fd = -1;
   spt->client_port = tp->udp.uh_sport;
   spt->slirp = slirp;
@@ -78,7 +79,8 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
   return k;
 }
 
-static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
+static int tftp_session_find(Slirp *slirp, struct sockaddr_storage *srcsas,
+                             struct tftp_t *tp)
 {
   struct tftp_session *spt;
   int k;
@@ -87,7 +89,7 @@ static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
     spt = &slirp->tftp_sessions[k];
 
     if (tftp_session_in_use(spt)) {
-      if (!memcmp(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip))) {
+      if (sockaddr_equal(&spt->client_addr, srcsas)) {
 	if (spt->client_port == tp->udp.uh_sport) {
 	  return k;
 	}
@@ -120,11 +122,53 @@ static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
     return bytes_read;
 }
 
+static struct tftp_t *tftp_prep_mbuf_data(struct tftp_session *spt,
+                                          struct mbuf *m)
+{
+    struct tftp_t *tp;
+
+    memset(m->m_data, 0, m->m_size);
+
+    m->m_data += IF_MAXLINKHDR;
+    if (spt->client_addr.ss_family == AF_INET6) {
+        m->m_data += sizeof(struct ip6);
+    } else {
+        m->m_data += sizeof(struct ip);
+    }
+    tp = (void *)m->m_data;
+    m->m_data += sizeof(struct udphdr);
+
+    return tp;
+}
+
+static void tftp_udp_output(struct tftp_session *spt, struct mbuf *m,
+                            struct tftp_t *recv_tp)
+{
+    if (spt->client_addr.ss_family == AF_INET6) {
+        struct sockaddr_in6 sa6, da6;
+
+        sa6.sin6_addr = spt->slirp->vhost_addr6;
+        sa6.sin6_port = recv_tp->udp.uh_dport;
+        da6.sin6_addr = ((struct sockaddr_in6 *)&spt->client_addr)->sin6_addr;
+        da6.sin6_port = spt->client_port;
+
+        udp6_output(NULL, m, &sa6, &da6);
+    } else {
+        struct sockaddr_in sa4, da4;
+
+        sa4.sin_addr = spt->slirp->vhost_addr;
+        sa4.sin_port = recv_tp->udp.uh_dport;
+        da4.sin_addr = ((struct sockaddr_in *)&spt->client_addr)->sin_addr;
+        da4.sin_port = spt->client_port;
+
+        udp_output(NULL, m, &sa4, &da4, IPTOS_LOWDELAY);
+    }
+}
+
 static int tftp_send_oack(struct tftp_session *spt,
                           const char *keys[], uint32_t values[], int nb,
                           struct tftp_t *recv_tp)
 {
-    struct sockaddr_in saddr, daddr;
     struct mbuf *m;
     struct tftp_t *tp;
     int i, n = 0;
@@ -132,13 +176,9 @@ static int tftp_send_oack(struct tftp_session *spt,
     m = m_get(spt->slirp);
 
     if (!m)
-	return -1;
-
-    memset(m->m_data, 0, m->m_size);
+        return -1;
 
-    m->m_data += IF_MAXLINKHDR;
-    tp = (void *)m->m_data;
-    m->m_data += sizeof(struct udpiphdr);
+    tp = tftp_prep_mbuf_data(spt, m);
 
     tp->tp_op = htons(TFTP_OACK);
     for (i = 0; i < nb; i++) {
@@ -148,15 +188,8 @@ static int tftp_send_oack(struct tftp_session *spt,
                       values[i]) + 1;
     }
 
-    saddr.sin_addr = recv_tp->ip.ip_dst;
-    saddr.sin_port = recv_tp->udp.uh_dport;
-
-    daddr.sin_addr = spt->client_ip;
-    daddr.sin_port = spt->client_port;
-
-    m->m_len = sizeof(struct tftp_t) - 514 + n -
-        sizeof(struct ip) - sizeof(struct udphdr);
-    udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
+    m->m_len = sizeof(struct tftp_t) - 514 + n - sizeof(struct udphdr);
+    tftp_udp_output(spt, m, recv_tp);
 
     return 0;
 }
@@ -165,7 +198,6 @@ static void tftp_send_error(struct tftp_session *spt,
                             uint16_t errorcode, const char *msg,
                             struct tftp_t *recv_tp)
 {
-  struct sockaddr_in saddr, daddr;
   struct mbuf *m;
   struct tftp_t *tp;
 
@@ -177,24 +209,15 @@ static void tftp_send_error(struct tftp_session *spt,
 
   memset(m->m_data, 0, m->m_size);
 
-  m->m_data += IF_MAXLINKHDR;
-  tp = (void *)m->m_data;
-  m->m_data += sizeof(struct udpiphdr);
+  tp = tftp_prep_mbuf_data(spt, m);
 
   tp->tp_op = htons(TFTP_ERROR);
   tp->x.tp_error.tp_error_code = htons(errorcode);
   pstrcpy((char *)tp->x.tp_error.tp_msg, sizeof(tp->x.tp_error.tp_msg), msg);
 
-  saddr.sin_addr = recv_tp->ip.ip_dst;
-  saddr.sin_port = recv_tp->udp.uh_dport;
-
-  daddr.sin_addr = spt->client_ip;
-  daddr.sin_port = spt->client_port;
-
-  m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg) -
-        sizeof(struct ip) - sizeof(struct udphdr);
-
-  udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
+  m->m_len = sizeof(struct tftp_t) - 514 + 3 + strlen(msg)
+             - sizeof(struct udphdr);
+  tftp_udp_output(spt, m, recv_tp);
 
 out:
   tftp_session_terminate(spt);
@@ -203,7 +226,6 @@ out:
 static void tftp_send_next_block(struct tftp_session *spt,
                                  struct tftp_t *recv_tp)
 {
-  struct sockaddr_in saddr, daddr;
   struct mbuf *m;
   struct tftp_t *tp;
   int nobytes;
@@ -216,19 +238,11 @@ static void tftp_send_next_block(struct tftp_session *spt,
 
   memset(m->m_data, 0, m->m_size);
 
-  m->m_data += IF_MAXLINKHDR;
-  tp = (void *)m->m_data;
-  m->m_data += sizeof(struct udpiphdr);
+  tp = tftp_prep_mbuf_data(spt, m);
 
   tp->tp_op = htons(TFTP_DATA);
   tp->x.tp_data.tp_block_nr = htons((spt->block_nr + 1) & 0xffff);
 
-  saddr.sin_addr = recv_tp->ip.ip_dst;
-  saddr.sin_port = recv_tp->udp.uh_dport;
-
-  daddr.sin_addr = spt->client_ip;
-  daddr.sin_port = spt->client_port;
-
   nobytes = tftp_read_data(spt, spt->block_nr, tp->x.tp_data.tp_buf, 512);
 
   if (nobytes < 0) {
@@ -241,10 +255,8 @@ static void tftp_send_next_block(struct tftp_session *spt,
     return;
   }
 
-  m->m_len = sizeof(struct tftp_t) - (512 - nobytes) -
-        sizeof(struct ip) - sizeof(struct udphdr);
-
-  udp_output(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
+  m->m_len = sizeof(struct tftp_t) - (512 - nobytes) - sizeof(struct udphdr);
+  tftp_udp_output(spt, m, recv_tp);
 
   if (nobytes == 512) {
     tftp_session_update(spt);
@@ -256,7 +268,8 @@ static void tftp_send_next_block(struct tftp_session *spt,
   spt->block_nr++;
 }
 
-static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
+static void tftp_handle_rrq(Slirp *slirp, struct sockaddr_storage *srcsas,
+                            struct tftp_t *tp, int pktlen)
 {
   struct tftp_session *spt;
   int s, k;
@@ -267,12 +280,12 @@ static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
   int nb_options = 0;
 
   /* check if a session already exists and if so terminate it */
-  s = tftp_session_find(slirp, tp);
+  s = tftp_session_find(slirp, srcsas, tp);
   if (s >= 0) {
     tftp_session_terminate(&slirp->tftp_sessions[s]);
   }
 
-  s = tftp_session_allocate(slirp, tp);
+  s = tftp_session_allocate(slirp, srcsas, tp);
 
   if (s < 0) {
     return;
@@ -397,11 +410,12 @@ static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
   tftp_send_next_block(spt, tp);
 }
 
-static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
+static void tftp_handle_ack(Slirp *slirp, struct sockaddr_storage *srcsas,
+                            struct tftp_t *tp, int pktlen)
 {
   int s;
 
-  s = tftp_session_find(slirp, tp);
+  s = tftp_session_find(slirp, srcsas, tp);
 
   if (s < 0) {
     return;
@@ -410,11 +424,12 @@ static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
   tftp_send_next_block(&slirp->tftp_sessions[s], tp);
 }
 
-static void tftp_handle_error(Slirp *slirp, struct tftp_t *tp, int pktlen)
+static void tftp_handle_error(Slirp *slirp, struct sockaddr_storage *srcsas,
+                              struct tftp_t *tp, int pktlen)
 {
   int s;
 
-  s = tftp_session_find(slirp, tp);
+  s = tftp_session_find(slirp, srcsas, tp);
 
   if (s < 0) {
     return;
@@ -423,21 +438,21 @@ static void tftp_handle_error(Slirp *slirp, struct tftp_t *tp, int pktlen)
   tftp_session_terminate(&slirp->tftp_sessions[s]);
 }
 
-void tftp_input(struct mbuf *m)
+void tftp_input(struct sockaddr_storage *srcsas, struct mbuf *m)
 {
   struct tftp_t *tp = (struct tftp_t *)m->m_data;
 
   switch(ntohs(tp->tp_op)) {
   case TFTP_RRQ:
-    tftp_handle_rrq(m->slirp, tp, m->m_len);
+    tftp_handle_rrq(m->slirp, srcsas, tp, m->m_len);
     break;
 
   case TFTP_ACK:
-    tftp_handle_ack(m->slirp, tp, m->m_len);
+    tftp_handle_ack(m->slirp, srcsas, tp, m->m_len);
     break;
 
   case TFTP_ERROR:
-    tftp_handle_error(m->slirp, tp, m->m_len);
+    tftp_handle_error(m->slirp, srcsas, tp, m->m_len);
     break;
   }
 }
slirp/tftp.h +3 -4
index e1cc24b..1cb1adf 100644
@@ -16,7 +16,6 @@
 #define TFTP_FILENAME_MAX 512
 
 struct tftp_t {
-  struct ip ip;
   struct udphdr udp;
   uint16_t tp_op;
   union {
@@ -30,20 +29,20 @@ struct tftp_t {
     } tp_error;
     char tp_buf[512 + 2];
   } x;
-};
+} __attribute__((packed));
 
 struct tftp_session {
     Slirp *slirp;
     char *filename;
     int fd;
 
-    struct in_addr client_ip;
+    struct sockaddr_storage client_addr;
     uint16_t client_port;
     uint32_t block_nr;
 
     int timestamp;
 };
 
-void tftp_input(struct mbuf *m);
+void tftp_input(struct sockaddr_storage *srcsas, struct mbuf *m);
 
 #endif
slirp/udp.c +10 -6
index be012fb..247024f 100644
@@ -128,6 +128,11 @@ udp_input(register struct mbuf *m, int iphlen)
 	  }
 	}
 
+	lhost.ss_family = AF_INET;
+	lhost4 = (struct sockaddr_in *) &lhost;
+	lhost4->sin_addr = ip->ip_src;
+	lhost4->sin_port = uh->uh_sport;
+
         /*
          *  handle DHCP/BOOTP
          */
@@ -143,7 +148,11 @@ udp_input(register struct mbuf *m, int iphlen)
          */
         if (ntohs(uh->uh_dport) == TFTP_SERVER &&
             ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) {
-            tftp_input(m);
+            m->m_data += iphlen;
+            m->m_len -= iphlen;
+            tftp_input(&lhost, m);
+            m->m_data -= iphlen;
+            m->m_len += iphlen;
             goto bad;
         }
 
@@ -154,11 +163,6 @@ udp_input(register struct mbuf *m, int iphlen)
 	/*
 	 * Locate pcb for datagram.
 	 */
-	lhost.ss_family = AF_INET;
-	lhost4 = (struct sockaddr_in *) &lhost;
-	lhost4->sin_addr = ip->ip_src;
-	lhost4->sin_port = uh->uh_sport;
-
 	so = solookup(&slirp->udp_last_so, &slirp->udb, &lhost, NULL);
 
 	if (so == NULL) {
slirp/udp6.c +13 -3
index 8952de2..690c4e1 100644
@@ -55,14 +55,24 @@ void udp6_input(struct mbuf *m)
      */
     save_ip = *ip;
 
-    /* TODO handle DHCP/BOOTP */
-    /* TODO handle TFTP */
-
     /* Locate pcb for datagram. */
     lhost.sin6_family = AF_INET6;
     lhost.sin6_addr = ip->ip_src;
     lhost.sin6_port = uh->uh_sport;
 
+    /* TODO handle DHCP/BOOTP */
+
+    /* handle TFTP */
+    if (ntohs(uh->uh_dport) == TFTP_SERVER &&
+        !memcmp(ip->ip_dst.s6_addr, slirp->vhost_addr6.s6_addr, 16)) {
+        m->m_data += hlen;
+        m->m_len -= hlen;
+        tftp_input((struct sockaddr_storage *)&lhost, m);
+        m->m_data -= hlen;
+        m->m_len += hlen;
+        goto bad;
+    }
+
     so = solookup(&slirp->udp_last_so, &slirp->udb,
                   (struct sockaddr_storage *) &lhost, NULL);
 
Signature · 1 line
2.7.0
#5 lore raw

PATCH v9 2/10 slirp: Adding ICMPv6 error sending

In reply to Samuel Thibault

From: Yann Bordenave <[email protected]>

Disambiguation : icmp_error is renamed into icmp_send_error, since it doesn't manage errors, but only sends ICMP Error messages.

Adding icmp6_send_error to send ICMPv6 Error messages. This function is simpler than the v4 version.
Adding some calls in various functions to send ICMP errors, when a received packet is too big, or when its hop limit is 0.

---
 slirp/ip6_icmp.c  | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 slirp/ip6_icmp.h  | 10 +++++++++
 slirp/ip6_input.c | 13 ++++++-----
 slirp/ip_icmp.c   | 12 +++++------
 slirp/ip_icmp.h   |  4 ++--
 slirp/ip_input.c  |  8 +++----
 slirp/socket.c    |  4 ++--
 slirp/tcp_input.c |  2 +-
 slirp/udp.c       |  3 ++-
 9 files changed, 99 insertions(+), 21 deletions(-)
slirp/ip6_icmp.c +64 -0
index 029d297..1cc65db 100644
@@ -59,6 +59,70 @@ static void icmp6_send_echoreply(struct mbuf *m, Slirp *slirp, struct ip6 *ip,
     ip6_output(NULL, t, 0);
 }
 
+void icmp6_send_error(struct mbuf *m, uint8_t type, uint8_t code)
+{
+    Slirp *slirp = m->slirp;
+    struct mbuf *t;
+    struct ip6 *ip = mtod(m, struct ip6 *);
+
+    char addrstr[INET6_ADDRSTRLEN];
+    DEBUG_CALL("icmp_send_error");
+    DEBUG_ARGS((dfd, " type = %d, code = %d\n", type, code));
+
+    if (IN6_IS_ADDR_MULTICAST(&ip->ip_src) ||
+            IN6_IS_ADDR_UNSPECIFIED(&ip->ip_src)) {
+        /* TODO icmp error? */
+        return;
+    }
+
+    t = m_get(slirp);
+
+    /* IPv6 packet */
+    struct ip6 *rip = mtod(t, struct ip6 *);
+    rip->ip_src = (struct in6_addr)LINKLOCAL_ADDR;
+    rip->ip_dst = ip->ip_src;
+    inet_ntop(AF_INET6, &rip->ip_dst, addrstr, INET6_ADDRSTRLEN);
+    DEBUG_ARG("target = %s", addrstr);
+
+    rip->ip_nh = IPPROTO_ICMPV6;
+    const int error_data_len = min(m->m_len,
+            IF_MTU - (sizeof(struct ip6) + ICMP6_ERROR_MINLEN));
+    rip->ip_pl = htons(ICMP6_ERROR_MINLEN + error_data_len);
+    t->m_len = sizeof(struct ip6) + ntohs(rip->ip_pl);
+
+    /* ICMPv6 packet */
+    t->m_data += sizeof(struct ip6);
+    struct icmp6 *ricmp = mtod(t, struct icmp6 *);
+    ricmp->icmp6_type = type;
+    ricmp->icmp6_code = code;
+    ricmp->icmp6_cksum = 0;
+
+    switch (type) {
+    case ICMP6_UNREACH:
+    case ICMP6_TIMXCEED:
+        ricmp->icmp6_err.unused = 0;
+        break;
+    case ICMP6_TOOBIG:
+        ricmp->icmp6_err.mtu = htonl(IF_MTU);
+        break;
+    case ICMP6_PARAMPROB:
+        /* TODO: Handle this case */
+        break;
+    default:
+        g_assert_not_reached();
+        break;
+    }
+    t->m_data += ICMP6_ERROR_MINLEN;
+    memcpy(t->m_data, m->m_data, error_data_len);
+
+    /* Checksum */
+    t->m_data -= ICMP6_ERROR_MINLEN;
+    t->m_data -= sizeof(struct ip6);
+    ricmp->icmp6_cksum = ip6_cksum(t);
+
+    ip6_output(NULL, t, 0);
+}
+
 /*
  * Send NDP Router Advertisement
  */
slirp/ip6_icmp.h +10 -0
index b2c40d6..9460bf8 100644
@@ -19,6 +19,12 @@ struct icmp6_echo { /* Echo Messages */
     uint16_t seq_num;
 };
 
+union icmp6_error_body {
+    uint32_t unused;
+    uint32_t pointer;
+    uint32_t mtu;
+};
+
 /*
  * NDP Messages
  */
@@ -82,6 +88,7 @@ struct icmp6 {
     uint8_t     icmp6_code;         /* type sub code */
     uint16_t    icmp6_cksum;        /* ones complement cksum of struct */
     union {
+        union icmp6_error_body error_body;
         struct icmp6_echo echo;
         struct ndp_rs ndp_rs;
         struct ndp_ra ndp_ra;
@@ -89,6 +96,7 @@ struct icmp6 {
         struct ndp_na ndp_na;
         struct ndp_redirect ndp_redirect;
     } icmp6_body;
+#define icmp6_err icmp6_body.error_body
 #define icmp6_echo icmp6_body.echo
 #define icmp6_nrs icmp6_body.ndp_rs
 #define icmp6_nra icmp6_body.ndp_ra
@@ -98,6 +106,7 @@ struct icmp6 {
 } QEMU_PACKED;
 
 #define ICMP6_MINLEN    4
+#define ICMP6_ERROR_MINLEN  8
 #define ICMP6_ECHO_MINLEN   8
 #define ICMP6_NDP_RS_MINLEN 8
 #define ICMP6_NDP_RA_MINLEN 16
@@ -197,6 +206,7 @@ struct ndpopt {
 void icmp6_init(Slirp *slirp);
 void icmp6_cleanup(Slirp *slirp);
 void icmp6_input(struct mbuf *);
+void icmp6_send_error(struct mbuf *m, uint8_t type, uint8_t code);
 void ndp_send_ra(Slirp *slirp);
 void ndp_send_ns(Slirp *slirp, struct in6_addr addr);
 
slirp/ip6_input.c +8 -5
index df249f1..b6a438d 100644
@@ -39,9 +39,14 @@ void ip6_input(struct mbuf *m)
         goto bad;
     }
 
+    if (ntohs(ip6->ip_pl) > IF_MTU) {
+        icmp6_send_error(m, ICMP6_TOOBIG, 0);
+        goto bad;
+    }
+
     /* check ip_ttl for a correct ICMP reply */
     if (ip6->ip_hl == 0) {
-        /*icmp_error(m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl");*/
+        icmp6_send_error(m, ICMP6_TIMXCEED, ICMP6_TIMXCEED_INTRANS);
         goto bad;
     }
 
@@ -49,14 +54,12 @@ void ip6_input(struct mbuf *m)
      * Switch out to protocol's input routine.
      */
     switch (ip6->ip_nh) {
-#if 0
     case IPPROTO_TCP:
-        tcp_input(m, hlen, (struct socket *)NULL);
+        icmp6_send_error(m, ICMP6_UNREACH, ICMP6_UNREACH_NO_ROUTE);
         break;
     case IPPROTO_UDP:
-        udp_input(m, hlen);
+        icmp6_send_error(m, ICMP6_UNREACH, ICMP6_UNREACH_NO_ROUTE);
         break;
-#endif
     case IPPROTO_ICMPV6:
         icmp6_input(m);
         break;
slirp/ip_icmp.c +6 -6
index ace3982..590dada 100644
@@ -38,7 +38,7 @@
 /* Be nice and tell them it's just a pseudo-ping packet */
 static const char icmp_ping_msg[] = "This is a pseudo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
 
-/* list of actions for icmp_error() on RX of an icmp message */
+/* list of actions for icmp_send_error() on RX of an icmp message */
 static const int icmp_flush[19] = {
 /*  ECHO REPLY (0)  */   0,
 		         1,
@@ -101,7 +101,7 @@ static int icmp_send(struct socket *so, struct mbuf *m, int hlen)
                (struct sockaddr *)&addr, sizeof(addr)) == -1) {
         DEBUG_MISC((dfd, "icmp_input icmp sendto tx errno = %d-%s\n",
                     errno, strerror(errno)));
-        icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
+        icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
         icmp_detach(so);
     }
 
@@ -189,7 +189,7 @@ icmp_input(struct mbuf *m, int hlen)
 		(struct sockaddr *)&addr, sizeof(addr)) == -1) {
 	DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
 		    errno,strerror(errno)));
-	icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
+	icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
 	udp_detach(so);
       }
     } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
@@ -235,7 +235,7 @@ end_error:
 
 #define ICMP_MAXDATALEN (IP_MSS-28)
 void
-icmp_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
+icmp_send_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
            const char *message)
 {
   unsigned hlen, shlen, s_ip_len;
@@ -243,7 +243,7 @@ icmp_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
   register struct icmp *icp;
   register struct mbuf *m;
 
-  DEBUG_CALL("icmp_error");
+  DEBUG_CALL("icmp_send_error");
   DEBUG_ARG("msrc = %p", msrc);
   DEBUG_ARG("msrc_len = %d", msrc->m_len);
 
@@ -433,7 +433,7 @@ void icmp_receive(struct socket *so)
         }
         DEBUG_MISC((dfd, " udp icmp rx errno = %d-%s\n", errno,
                     strerror(errno)));
-        icmp_error(so->so_m, ICMP_UNREACH, error_code, 0, strerror(errno));
+        icmp_send_error(so->so_m, ICMP_UNREACH, error_code, 0, strerror(errno));
     } else {
         icmp_reflect(so->so_m);
         so->so_m = NULL; /* Don't m_free() it again! */
slirp/ip_icmp.h +2 -2
index be4426b..846761d 100644
@@ -156,8 +156,8 @@ struct icmp {
 void icmp_init(Slirp *slirp);
 void icmp_cleanup(Slirp *slirp);
 void icmp_input(struct mbuf *, int);
-void icmp_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
-                const char *message);
+void icmp_send_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
+                     const char *message);
 void icmp_reflect(struct mbuf *);
 void icmp_receive(struct socket *so);
 void icmp_detach(struct socket *so);
slirp/ip_input.c +4 -4
index e4855ae..16fb2cb 100644
@@ -132,9 +132,9 @@ ip_input(struct mbuf *m)
 	   m_adj(m, ip->ip_len - m->m_len);
 
 	/* check ip_ttl for a correct ICMP reply */
-	if(ip->ip_ttl==0) {
-	  icmp_error(m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl");
-	  goto bad;
+	if (ip->ip_ttl == 0) {
+	    icmp_send_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, "ttl");
+	    goto bad;
 	}
 
 	/*
@@ -637,7 +637,7 @@ typedef uint32_t n_time;
 	}
 	return (0);
 bad:
- 	icmp_error(m, type, code, 0, 0);
+	icmp_send_error(m, type, code, 0, 0);
 
 	return (1);
 }
slirp/socket.c +2 -2
index 2b5453e..32b1ba3 100644
@@ -463,7 +463,7 @@ sorecvfrom(struct socket *so)
 
 	    DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
 			errno,strerror(errno)));
-	    icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
+	    icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
 	  } else {
 	    icmp_reflect(so->so_m);
             so->so_m = NULL; /* Don't m_free() it again! */
@@ -511,7 +511,7 @@ sorecvfrom(struct socket *so)
 	    else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
 
 	    DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
-	    icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
+	    icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
 	    m_free(m);
 	  } else {
 	  /*
slirp/tcp_input.c +1 -1
index 2027a75..5f845da 100644
@@ -608,7 +608,7 @@ findso:
 	      m->m_data -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
 	      m->m_len  += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
 	      *ip=save_ip;
-	      icmp_error(m, ICMP_UNREACH,code, 0,strerror(errno));
+	      icmp_send_error(m, ICMP_UNREACH, code, 0, strerror(errno));
 	    }
             tcp_close(tp);
 	    m_free(m);
slirp/udp.c +2 -1
index 6b39cab..be012fb 100644
@@ -209,7 +209,8 @@ udp_input(register struct mbuf *m, int iphlen)
 	  m->m_data -= iphlen;
 	  *ip=save_ip;
 	  DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
-	  icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
+	  icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
+	                  strerror(errno));
 	  goto bad;
 	}
 
Signature · 1 line
2.7.0
#12 lore raw

PATCH v9 4/10 slirp: Factorizing tcpiphdr structure with an union

In reply to Samuel Thibault

From: Guillaume Subiron <[email protected]>

This patch factorizes the tcpiphdr structure to put the IPv4 fields in an union, for addition of version 6 in further patch.
Using some macros, retrocompatibility of the existing code is assured.

This patch also fixes the SLIRP_MSIZE and margin computation in various functions, and makes them compatible with the new tcpiphdr structure, whose size will be bigger than sizeof(struct tcphdr) + sizeof(struct ip)

---
 slirp/if.h         |  4 ++--
 slirp/mbuf.c       |  4 ++--
 slirp/slirp.c      | 15 ++++++++-------
 slirp/socket.c     | 13 ++++++++++++-
 slirp/tcp_input.c  | 31 ++++++++++++++++++++-----------
 slirp/tcp_output.c | 18 +++++++++++++-----
 slirp/tcp_subr.c   | 31 ++++++++++++++++++++++---------
 slirp/tcpip.h      | 31 +++++++++++++++++++++++--------
 8 files changed, 102 insertions(+), 45 deletions(-)
slirp/if.h +2 -2
index 3327023..c7a5c57 100644
@@ -17,7 +17,7 @@
 #define IF_MRU 1500
 #define	IF_COMP IF_AUTOCOMP	/* Flags for compression */
 
-/* 2 for alignment, 14 for ethernet, 40 for TCP/IP */
-#define IF_MAXLINKHDR (2 + 14 + 40)
+/* 2 for alignment, 14 for ethernet */
+#define IF_MAXLINKHDR (2 + ETH_HLEN)
 
 #endif
slirp/mbuf.c +2 -2
index c959758..d688dd4 100644
@@ -22,9 +22,9 @@
 
 /*
  * Find a nice value for msize
- * XXX if_maxlinkhdr already in mtu
  */
-#define SLIRP_MSIZE (IF_MTU + IF_MAXLINKHDR + offsetof(struct mbuf, m_dat) + 6)
+#define SLIRP_MSIZE\
+    (offsetof(struct mbuf, m_dat) + IF_MAXLINKHDR + TCPIPHDR_DELTA + IF_MTU)
 
 void
 m_init(Slirp *slirp)
slirp/slirp.c +8 -7
index c986e0b..3e83ede 100644
@@ -756,15 +756,16 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
         m = m_get(slirp);
         if (!m)
             return;
-        /* Note: we add to align the IP header */
-        if (M_FREEROOM(m) < pkt_len + 2) {
-            m_inc(m, pkt_len + 2);
+        /* Note: we add 2 to align the IP header on 4 bytes,
+         * and add the margin for the tcpiphdr overhead  */
+        if (M_FREEROOM(m) < pkt_len + TCPIPHDR_DELTA + 2) {
+            m_inc(m, pkt_len + TCPIPHDR_DELTA + 2);
         }
-        m->m_len = pkt_len + 2;
-        memcpy(m->m_data + 2, pkt, pkt_len);
+        m->m_len = pkt_len + TCPIPHDR_DELTA + 2;
+        memcpy(m->m_data + TCPIPHDR_DELTA + 2, pkt, pkt_len);
 
-        m->m_data += 2 + ETH_HLEN;
-        m->m_len -= 2 + ETH_HLEN;
+        m->m_data += TCPIPHDR_DELTA + 2 + ETH_HLEN;
+        m->m_len -= TCPIPHDR_DELTA + 2 + ETH_HLEN;
 
         if (proto == ETH_P_IP) {
             ip_input(m);
slirp/socket.c +12 -1
index b79ddec..d4b02c8 100644
@@ -483,7 +483,18 @@ sorecvfrom(struct socket *so)
 	  if (!m) {
 	      return;
 	  }
-	  m->m_data += IF_MAXLINKHDR;
+	  switch (so->so_ffamily) {
+	  case AF_INET:
+	      m->m_data += IF_MAXLINKHDR + sizeof(struct udpiphdr);
+	      break;
+	  case AF_INET6:
+	      m->m_data += IF_MAXLINKHDR + sizeof(struct ip6)
+	                                 + sizeof(struct udphdr);
+	      break;
+	  default:
+	      g_assert_not_reached();
+	      break;
+	  }
 
 	  /*
 	   * XXX Shouldn't FIONREAD packets destined for port 53,
slirp/tcp_input.c +20 -11
index 5f845da..a9de7d1 100644
@@ -256,11 +256,6 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
 	}
 	slirp = m->slirp;
 
-	/*
-	 * Get IP and TCP header together in first mbuf.
-	 * Note: IP leaves IP header in first mbuf.
-	 */
-	ti = mtod(m, struct tcpiphdr *);
 	if (iphlen > sizeof(struct ip )) {
 	  ip_stripoptions(m, (struct mbuf *)0);
 	  iphlen=sizeof(struct ip );
@@ -277,14 +272,28 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
 	save_ip.ip_len+= iphlen;
 
 	/*
+	 * Get IP and TCP header together in first mbuf.
+	 * Note: IP leaves IP header in first mbuf.
+	 */
+	m->m_data -= sizeof(struct tcpiphdr) - sizeof(struct ip)
+	                                     - sizeof(struct tcphdr);
+	m->m_len += sizeof(struct tcpiphdr) - sizeof(struct ip)
+	                                    - sizeof(struct tcphdr);
+	ti = mtod(m, struct tcpiphdr *);
+
+	/*
 	 * Checksum extended TCP header and data.
 	 */
-	tlen = ((struct ip *)ti)->ip_len;
-        tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = NULL;
-        memset(&ti->ti_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
-	ti->ti_x1 = 0;
+	tlen = ip->ip_len;
+	tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = NULL;
+	memset(&ti->ih_mbuf, 0 , sizeof(struct mbuf_ptr));
+	memset(&ti->ti, 0, sizeof(ti->ti));
+	ti->ti_x0 = 0;
+	ti->ti_src = save_ip.ip_src;
+	ti->ti_dst = save_ip.ip_dst;
+	ti->ti_pr = save_ip.ip_p;
 	ti->ti_len = htons((uint16_t)tlen);
-	len = sizeof(struct ip ) + tlen;
+	len = ((sizeof(struct tcpiphdr) - sizeof(struct tcphdr)) + tlen);
 	if(cksum(m, len)) {
 	  goto drop;
 	}
@@ -1475,7 +1484,7 @@ tcp_mss(struct tcpcb *tp, u_int offer)
 	DEBUG_ARG("tp = %p", tp);
 	DEBUG_ARG("offer = %d", offer);
 
-	mss = min(IF_MTU, IF_MRU) - sizeof(struct tcpiphdr);
+	mss = min(IF_MTU, IF_MRU) - sizeof(struct tcphdr) + sizeof(struct ip);
 	if (offer)
 		mss = min(mss, offer);
 	mss = max(mss, 32);
slirp/tcp_output.c +13 -5
index 34e4d2e..7fc6a87 100644
@@ -448,15 +448,23 @@ send:
 	 */
 	m->m_len = hdrlen + len; /* XXX Needed? m_len should be correct */
 
-    {
+	struct tcpiphdr tcpiph_save = *(mtod(m, struct tcpiphdr *));
+	m->m_data += sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
+	                                     - sizeof(struct ip);
+	m->m_len  -= sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
+	                                     - sizeof(struct ip);
+	struct ip *ip = mtod(m, struct ip *);
 
-	((struct ip *)ti)->ip_len = m->m_len;
+	ip->ip_len = m->m_len;
+	ip->ip_dst = tcpiph_save.ti_dst;
+	ip->ip_src = tcpiph_save.ti_src;
+	ip->ip_p = tcpiph_save.ti_pr;
 
-	((struct ip *)ti)->ip_ttl = IPDEFTTL;
-	((struct ip *)ti)->ip_tos = so->so_iptos;
+	ip->ip_ttl = IPDEFTTL;
+	ip->ip_tos = so->so_iptos;
 
 	error = ip_output(so, m);
-    }
+
 	if (error) {
 out:
 		return (error);
slirp/tcp_subr.c +22 -9
index b1aa1f2..cd021df 100644
@@ -76,9 +76,10 @@ tcp_template(struct tcpcb *tp)
 	register struct tcpiphdr *n = &tp->t_template;
 
 	n->ti_mbuf = NULL;
-	n->ti_x1 = 0;
+	memset(&n->ti, 0, sizeof(n->ti));
+	n->ti_x0 = 0;
 	n->ti_pr = IPPROTO_TCP;
-	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
+	n->ti_len = htons(sizeof(struct tcphdr));
 	n->ti_src = so->so_faddr;
 	n->ti_dst = so->so_laddr;
 	n->ti_sport = so->so_fport;
@@ -131,6 +132,7 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
 		m->m_data += IF_MAXLINKHDR;
 		*mtod(m, struct tcpiphdr *) = *ti;
 		ti = mtod(m, struct tcpiphdr *);
+		memset(&ti->ti, 0, sizeof(ti->ti));
 		flags = TH_ACK;
 	} else {
 		/*
@@ -150,8 +152,8 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
 	tlen += sizeof (struct tcpiphdr);
 	m->m_len = tlen;
 
-        ti->ti_mbuf = NULL;
-	ti->ti_x1 = 0;
+	ti->ti_mbuf = NULL;
+	ti->ti_x0 = 0;
 	ti->ti_seq = htonl(seq);
 	ti->ti_ack = htonl(ack);
 	ti->ti_x2 = 0;
@@ -164,12 +166,23 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
 	ti->ti_urp = 0;
 	ti->ti_sum = 0;
 	ti->ti_sum = cksum(m, tlen);
-	((struct ip *)ti)->ip_len = tlen;
 
-	if(flags & TH_RST)
-	  ((struct ip *)ti)->ip_ttl = MAXTTL;
-	else
-	  ((struct ip *)ti)->ip_ttl = IPDEFTTL;
+	struct tcpiphdr tcpiph_save = *(mtod(m, struct tcpiphdr *));
+	m->m_data += sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
+	                                     - sizeof(struct ip);
+	m->m_len  -= sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
+	                                     - sizeof(struct ip);
+	struct ip *ip = mtod(m, struct ip *);
+	ip->ip_len = tlen;
+	ip->ip_dst = tcpiph_save.ti_dst;
+	ip->ip_src = tcpiph_save.ti_src;
+	ip->ip_p = tcpiph_save.ti_pr;
+
+	if (flags & TH_RST) {
+		ip->ip_ttl = MAXTTL;
+	} else {
+		ip->ip_ttl = IPDEFTTL;
+	}
 
 	(void) ip_output((struct socket *)0, m);
 }
slirp/tcpip.h +23 -8
index 7974ce3..3c5d127 100644
@@ -37,15 +37,23 @@
  * Tcp+ip header, after ip options removed.
  */
 struct tcpiphdr {
-	struct 	ipovly ti_i;		/* overlaid ip structure */
-	struct	tcphdr ti_t;		/* tcp header */
+    struct mbuf_ptr ih_mbuf;	/* backpointer to mbuf */
+    union {
+        struct {
+            struct  in_addr ih_src; /* source internet address */
+            struct  in_addr ih_dst; /* destination internet address */
+            uint8_t ih_x1;          /* (unused) */
+            uint8_t ih_pr;          /* protocol */
+        } ti_i4;
+    } ti;
+    uint16_t    ti_x0;
+    uint16_t    ti_len;             /* protocol length */
+    struct      tcphdr ti_t;        /* tcp header */
 };
-#define	ti_mbuf		ti_i.ih_mbuf.mptr
-#define	ti_x1		ti_i.ih_x1
-#define	ti_pr		ti_i.ih_pr
-#define	ti_len		ti_i.ih_len
-#define	ti_src		ti_i.ih_src
-#define	ti_dst		ti_i.ih_dst
+#define	ti_mbuf		ih_mbuf.mptr
+#define	ti_pr		ti.ti_i4.ih_pr
+#define	ti_src		ti.ti_i4.ih_src
+#define	ti_dst		ti.ti_i4.ih_dst
 #define	ti_sport	ti_t.th_sport
 #define	ti_dport	ti_t.th_dport
 #define	ti_seq		ti_t.th_seq
@@ -65,6 +73,13 @@ struct tcpiphdr {
 #define tcpfrag_list_end(F, T) (tcpiphdr2qlink(F) == (struct qlink*)(T))
 #define tcpfrag_list_empty(T) ((T)->seg_next == (struct tcpiphdr*)(T))
 
+/* This is the difference between the size of a tcpiphdr structure, and the
+ * size of actual ip+tcp headers, rounded up since we need to align data.  */
+#define TCPIPHDR_DELTA\
+    (max(0,\
+         (sizeof(struct tcpiphdr)\
+          - sizeof(struct ip) - sizeof(struct tcphdr) + 3) & ~3))
+
 /*
  * Just a clean way to get to the first byte
  * of the packet
Signature · 1 line
2.7.0
#15 lore raw

PATCH v9 6/10 slirp: Reindent after refactoring

In reply to Samuel Thibault

From: Guillaume Subiron <[email protected]>

No code change.

---
 slirp/tcp_input.c  | 92 +++++++++++++++++++++++++++---------------------------
 slirp/tcp_output.c | 29 +++++++++--------
 slirp/tcp_subr.c   | 50 ++++++++++++++---------------
 3 files changed, 85 insertions(+), 86 deletions(-)
slirp/tcp_input.c +46 -46
index a2f1044..6427c90 100644
@@ -258,48 +258,48 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso, unsigned short af)
 
 	switch (af) {
 	case AF_INET:
-	if (iphlen > sizeof(struct ip )) {
-	  ip_stripoptions(m, (struct mbuf *)0);
-	  iphlen=sizeof(struct ip );
-	}
-	/* XXX Check if too short */
+	    if (iphlen > sizeof(struct ip)) {
+	        ip_stripoptions(m, (struct mbuf *)0);
+	        iphlen = sizeof(struct ip);
+	    }
+	    /* XXX Check if too short */
 
 
-	/*
-	 * Save a copy of the IP header in case we want restore it
-	 * for sending an ICMP error message in response.
-	 */
-	ip=mtod(m, struct ip *);
-	save_ip = *ip;
-	save_ip.ip_len+= iphlen;
+	    /*
+	     * Save a copy of the IP header in case we want restore it
+	     * for sending an ICMP error message in response.
+	     */
+	    ip = mtod(m, struct ip *);
+	    save_ip = *ip;
+	    save_ip.ip_len += iphlen;
 
-	/*
-	 * Get IP and TCP header together in first mbuf.
-	 * Note: IP leaves IP header in first mbuf.
-	 */
-	m->m_data -= sizeof(struct tcpiphdr) - sizeof(struct ip)
-	                                     - sizeof(struct tcphdr);
-	m->m_len += sizeof(struct tcpiphdr) - sizeof(struct ip)
-	                                    - sizeof(struct tcphdr);
-	ti = mtod(m, struct tcpiphdr *);
+	    /*
+	     * Get IP and TCP header together in first mbuf.
+	     * Note: IP leaves IP header in first mbuf.
+	     */
+	    m->m_data -= sizeof(struct tcpiphdr) - sizeof(struct ip)
+	                                         - sizeof(struct tcphdr);
+	    m->m_len += sizeof(struct tcpiphdr) - sizeof(struct ip)
+	                                        - sizeof(struct tcphdr);
+	    ti = mtod(m, struct tcpiphdr *);
 
-	/*
-	 * Checksum extended TCP header and data.
-	 */
-	tlen = ip->ip_len;
-	tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = NULL;
-	memset(&ti->ih_mbuf, 0 , sizeof(struct mbuf_ptr));
-	memset(&ti->ti, 0, sizeof(ti->ti));
-	ti->ti_x0 = 0;
-	ti->ti_src = save_ip.ip_src;
-	ti->ti_dst = save_ip.ip_dst;
-	ti->ti_pr = save_ip.ip_p;
-	ti->ti_len = htons((uint16_t)tlen);
-	len = ((sizeof(struct tcpiphdr) - sizeof(struct tcphdr)) + tlen);
-	if(cksum(m, len)) {
-	  goto drop;
-	}
-	break;
+	    /*
+	     * Checksum extended TCP header and data.
+	     */
+	    tlen = ip->ip_len;
+	    tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = NULL;
+	    memset(&ti->ih_mbuf, 0 , sizeof(struct mbuf_ptr));
+	    memset(&ti->ti, 0, sizeof(ti->ti));
+	    ti->ti_x0 = 0;
+	    ti->ti_src = save_ip.ip_src;
+	    ti->ti_dst = save_ip.ip_dst;
+	    ti->ti_pr = save_ip.ip_p;
+	    ti->ti_len = htons((uint16_t)tlen);
+	    len = ((sizeof(struct tcpiphdr) - sizeof(struct tcphdr)) + tlen);
+	    if (cksum(m, len)) {
+	        goto drop;
+	    }
+	    break;
 
 	default:
 	    g_assert_not_reached();
@@ -343,12 +343,12 @@ findso:
 	fhost.ss_family = af;
 	switch (af) {
 	case AF_INET:
-	lhost4 = (struct sockaddr_in *) &lhost;
-	lhost4->sin_addr = ti->ti_src;
-	lhost4->sin_port = ti->ti_sport;
-	fhost4 = (struct sockaddr_in *) &fhost;
-	fhost4->sin_addr = ti->ti_dst;
-	fhost4->sin_port = ti->ti_dport;
+	    lhost4 = (struct sockaddr_in *) &lhost;
+	    lhost4->sin_addr = ti->ti_src;
+	    lhost4->sin_port = ti->ti_sport;
+	    fhost4 = (struct sockaddr_in *) &fhost;
+	    fhost4->sin_addr = ti->ti_dst;
+	    fhost4->sin_port = ti->ti_dport;
 	    break;
 	default:
 	    g_assert_not_reached();
@@ -591,8 +591,8 @@ findso:
 	   * tcp_ctl once connected, otherwise connect
 	   */
 	  if (af == AF_INET &&
-	      (so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
-	      slirp->vnetwork_addr.s_addr) {
+	         (so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
+	         slirp->vnetwork_addr.s_addr) {
 	    if (so->so_faddr.s_addr != slirp->vhost_addr.s_addr &&
 		so->so_faddr.s_addr != slirp->vnameserver_addr.s_addr) {
 		/* May be an add exec */
slirp/tcp_output.c +14 -15
index 32201fb..eec8902 100644
@@ -452,21 +452,20 @@ send:
 
 	switch (so->so_ffamily) {
 	case AF_INET:
-	m->m_data += sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
-	                                     - sizeof(struct ip);
-	m->m_len  -= sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
-	                                     - sizeof(struct ip);
-	ip = mtod(m, struct ip *);
-
-	ip->ip_len = m->m_len;
-	ip->ip_dst = tcpiph_save.ti_dst;
-	ip->ip_src = tcpiph_save.ti_src;
-	ip->ip_p = tcpiph_save.ti_pr;
-
-	ip->ip_ttl = IPDEFTTL;
-	ip->ip_tos = so->so_iptos;
-
-	error = ip_output(so, m);
+	    m->m_data += sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
+	                                         - sizeof(struct ip);
+	    m->m_len  -= sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
+	                                         - sizeof(struct ip);
+	    ip = mtod(m, struct ip *);
+
+	    ip->ip_len = m->m_len;
+	    ip->ip_dst = tcpiph_save.ti_dst;
+	    ip->ip_src = tcpiph_save.ti_src;
+	    ip->ip_p = tcpiph_save.ti_pr;
+
+	    ip->ip_ttl = IPDEFTTL;
+	    ip->ip_tos = so->so_iptos;
+	    error = ip_output(so, m);
 	    break;
 
 	default:
slirp/tcp_subr.c +25 -25

68 lines not on this page · raw message

Signature · 1 line
2.7.0
#19 lore raw

PATCH v9 7/10 slirp: Handle IPv6 in TCP functions

In reply to Samuel Thibault

From: Guillaume Subiron <[email protected]>

This patch adds IPv6 case in TCP functions refactored by the last patches.
This also adds IPv6 pseudo-header in tcpiphdr structure. Finally, tcp_input() is called by ip6_input().

---
 slirp/ip6_input.c  |  3 ++-
 slirp/tcp.h        |  2 ++
 slirp/tcp_input.c  | 58 +++++++++++++++++++++++++++++++++++++++++++-----------
 slirp/tcp_output.c | 16 +++++++++++++++
 slirp/tcp_subr.c   | 32 ++++++++++++++++++++++++++++--
 slirp/tcpip.h      |  9 +++++++++
 6 files changed, 106 insertions(+), 14 deletions(-)
slirp/ip6_input.c +2 -1
index 9ca6d32..c0b11e7 100644
@@ -55,7 +55,8 @@ void ip6_input(struct mbuf *m)
      */
     switch (ip6->ip_nh) {
     case IPPROTO_TCP:
-        icmp6_send_error(m, ICMP6_UNREACH, ICMP6_UNREACH_NO_ROUTE);
+        NTOHS(ip6->ip_pl);
+        tcp_input(m, sizeof(struct ip6), (struct socket *)NULL, AF_INET6);
         break;
     case IPPROTO_UDP:
         udp6_input(m);
slirp/tcp.h +2 -0
index 2e2b403..61befcd 100644
@@ -106,6 +106,8 @@ struct tcphdr {
  */
 #undef TCP_MSS
 #define	TCP_MSS	1460
+#undef TCP6_MSS
+#define TCP6_MSS 1440
 
 #undef TCP_MAXWIN
 #define	TCP_MAXWIN	65535	/* largest value for (unscaled) window */
slirp/tcp_input.c +47 -11

118 lines not on this page · raw message

slirp/tcp_output.c +16 -0
index eec8902..99b0a9b 100644
@@ -63,6 +63,7 @@ tcp_output(struct tcpcb *tp)
 	register struct mbuf *m;
 	register struct tcpiphdr *ti, tcpiph_save;
 	struct ip *ip;
+	struct ip6 *ip6;
 	u_char opt[MAX_TCPOPTLEN];
 	unsigned optlen, hdrlen;
 	int idle, sendalot;
@@ -468,6 +469,21 @@ send:
 	    error = ip_output(so, m);
 	    break;
 
+	case AF_INET6:
+	    m->m_data += sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
+	                                         - sizeof(struct ip6);
+	    m->m_len  -= sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
+	                                         - sizeof(struct ip6);
+	    ip6 = mtod(m, struct ip6 *);
+
+	    ip6->ip_pl = tcpiph_save.ti_len;
+	    ip6->ip_dst = tcpiph_save.ti_dst6;
+	    ip6->ip_src = tcpiph_save.ti_src6;
+	    ip6->ip_nh = tcpiph_save.ti_nh6;
+
+	    error = ip6_output(so, m, 0);
+	    break;
+
 	default:
 	    g_assert_not_reached();
 	}
slirp/tcp_subr.c +30 -2
index c16b29d..dbfd2c6 100644
@@ -88,6 +88,15 @@ tcp_template(struct tcpcb *tp)
 	    n->ti_dport = so->so_lport;
 	    break;
 
+	case AF_INET6:
+	    n->ti_nh6 = IPPROTO_TCP;
+	    n->ti_len = htons(sizeof(struct tcphdr));
+	    n->ti_src6 = so->so_faddr6;
+	    n->ti_dst6 = so->so_laddr6;
+	    n->ti_sport = so->so_fport6;
+	    n->ti_dport = so->so_lport6;
+	    break;
+
 	default:
 	    g_assert_not_reached();
 	}
@@ -156,6 +165,10 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
 		    xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, uint32_t);
 		    xchg(ti->ti_dport, ti->ti_sport, uint16_t);
 		    break;
+		case AF_INET6:
+		    xchg(ti->ti_dst6, ti->ti_src6, struct in6_addr);
+		    xchg(ti->ti_dport, ti->ti_sport, uint16_t);
+		    break;
 		default:
 		    g_assert_not_reached();
 		}
@@ -182,6 +195,7 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
 
 	struct tcpiphdr tcpiph_save = *(mtod(m, struct tcpiphdr *));
 	struct ip *ip;
+	struct ip6 *ip6;
 
 	switch (af) {
 	case AF_INET:
@@ -201,7 +215,21 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
 	        ip->ip_ttl = IPDEFTTL;
 	    }
 
-	    (void) ip_output((struct socket *)0, m);
+	    ip_output(NULL, m);
+	    break;
+
+	case AF_INET6:
+	    m->m_data += sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
+	                                         - sizeof(struct ip6);
+	    m->m_len  -= sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
+	                                         - sizeof(struct ip6);
+	    ip6 = mtod(m, struct ip6 *);
+	    ip6->ip_pl = tlen;
+	    ip6->ip_dst = tcpiph_save.ti_dst6;
+	    ip6->ip_src = tcpiph_save.ti_src6;
+	    ip6->ip_nh = tcpiph_save.ti_nh6;
+
+	    ip6_output(NULL, m, 0);
 	    break;
 
 	default:
@@ -225,7 +253,7 @@ tcp_newtcpcb(struct socket *so)
 
 	memset((char *) tp, 0, sizeof(struct tcpcb));
 	tp->seg_next = tp->seg_prev = (struct tcpiphdr*)tp;
-	tp->t_maxseg = TCP_MSS;
+	tp->t_maxseg = (so->so_ffamily == AF_INET) ? TCP_MSS : TCP6_MSS;
 
 	tp->t_flags = TCP_DO_RFC1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
 	tp->t_socket = so;
slirp/tcpip.h +9 -0
index 3c5d127..124b4a9 100644
@@ -45,6 +45,12 @@ struct tcpiphdr {
             uint8_t ih_x1;          /* (unused) */
             uint8_t ih_pr;          /* protocol */
         } ti_i4;
+        struct {
+            struct  in6_addr ih_src;
+            struct  in6_addr ih_dst;
+            uint8_t ih_x1;
+            uint8_t ih_nh;
+        } ti_i6;
     } ti;
     uint16_t    ti_x0;
     uint16_t    ti_len;             /* protocol length */
@@ -54,6 +60,9 @@ struct tcpiphdr {
 #define	ti_pr		ti.ti_i4.ih_pr
 #define	ti_src		ti.ti_i4.ih_src
 #define	ti_dst		ti.ti_i4.ih_dst
+#define	ti_src6		ti.ti_i6.ih_src
+#define	ti_dst6		ti.ti_i6.ih_dst
+#define	ti_nh6		ti.ti_i6.ih_nh
 #define	ti_sport	ti_t.th_sport
 #define	ti_dport	ti_t.th_dport
 #define	ti_seq		ti_t.th_seq
Signature · 1 line
2.7.0
#14 lore raw

PATCH v9 5/10 slirp: Generalizing and neutralizing various TCP functions before adding IPv6 stuff

In reply to Samuel Thibault

From: Guillaume Subiron <[email protected]>

Basically, this patch adds some switch in various TCP functions to prepare them for the IPv6 case.

To have something to "switch" in tcp_input() and tcp_respond(), a new argument is used to give them the sa_family of the addresses they are working on.

This patch does not include the entailed reindentation, to make proofread easier. Reindentation is adressed in the following no-op patch.

---
 slirp/ip_input.c   |  2 +-
 slirp/slirp.c      |  6 ++++--
 slirp/slirp.h      |  5 +++--
 slirp/tcp_input.c  | 62 ++++++++++++++++++++++++++++++++++++++++++------------
 slirp/tcp_output.c | 14 +++++++++---
 slirp/tcp_subr.c   | 37 +++++++++++++++++++++++++-------
 slirp/tcp_timer.c  |  3 ++-
 7 files changed, 99 insertions(+), 30 deletions(-)
slirp/ip_input.c +1 -1
index 16fb2cb..12f173d 100644
@@ -200,7 +200,7 @@ ip_input(struct mbuf *m)
 	 */
 	switch (ip->ip_p) {
 	 case IPPROTO_TCP:
-		tcp_input(m, hlen, (struct socket *)NULL);
+		tcp_input(m, hlen, (struct socket *)NULL, AF_INET);
 		break;
 	 case IPPROTO_UDP:
 		udp_input(m, hlen);
slirp/slirp.c +4 -2
index 3e83ede..d908422 100644
@@ -576,7 +576,8 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
                         /*
                          * Continue tcp_input
                          */
-                        tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
+                        tcp_input((struct mbuf *)NULL, sizeof(struct ip), so,
+                                  so->so_ffamily);
                         /* continue; */
                     } else {
                         ret = sowrite(so);
@@ -625,7 +626,8 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
                         }
 
                     }
-                    tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
+                    tcp_input((struct mbuf *)NULL, sizeof(struct ip), so,
+                              so->so_ffamily);
                 } /* SS_ISFCONNECTING */
 #endif
             }
slirp/slirp.h +3 -2
index 15775bc..967d93e 100644
@@ -349,7 +349,7 @@ void ip6_input(struct mbuf *);
 int ip6_output(struct socket *, struct mbuf *, int fast);
 
 /* tcp_input.c */
-void tcp_input(register struct mbuf *, int, struct socket *);
+void tcp_input(register struct mbuf *, int, struct socket *, unsigned short af);
 int tcp_mss(register struct tcpcb *, u_int);
 
 /* tcp_output.c */
@@ -360,7 +360,8 @@ void tcp_setpersist(register struct tcpcb *);
 void tcp_init(Slirp *);
 void tcp_cleanup(Slirp *);
 void tcp_template(struct tcpcb *);
-void tcp_respond(struct tcpcb *, register struct tcpiphdr *, register struct mbuf *, tcp_seq, tcp_seq, int);
+void tcp_respond(struct tcpcb *, register struct tcpiphdr *,
+        register struct mbuf *, tcp_seq, tcp_seq, int, unsigned short);
 struct tcpcb * tcp_newtcpcb(struct socket *);
 struct tcpcb * tcp_close(register struct tcpcb *);
 void tcp_sockclosed(struct tcpcb *);
slirp/tcp_input.c +49 -13

130 lines not on this page · raw message

slirp/tcp_output.c +11 -3
index 7fc6a87..32201fb 100644
@@ -61,7 +61,8 @@ tcp_output(struct tcpcb *tp)
 	register long len, win;
 	int off, flags, error;
 	register struct mbuf *m;
-	register struct tcpiphdr *ti;
+	register struct tcpiphdr *ti, tcpiph_save;
+	struct ip *ip;
 	u_char opt[MAX_TCPOPTLEN];
 	unsigned optlen, hdrlen;
 	int idle, sendalot;
@@ -447,13 +448,15 @@ send:
 	 * the template, but need a way to checksum without them.
 	 */
 	m->m_len = hdrlen + len; /* XXX Needed? m_len should be correct */
+	tcpiph_save = *mtod(m, struct tcpiphdr *);
 
-	struct tcpiphdr tcpiph_save = *(mtod(m, struct tcpiphdr *));
+	switch (so->so_ffamily) {
+	case AF_INET:
 	m->m_data += sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
 	                                     - sizeof(struct ip);
 	m->m_len  -= sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
 	                                     - sizeof(struct ip);
-	struct ip *ip = mtod(m, struct ip *);
+	ip = mtod(m, struct ip *);
 
 	ip->ip_len = m->m_len;
 	ip->ip_dst = tcpiph_save.ti_dst;
@@ -464,6 +467,11 @@ send:
 	ip->ip_tos = so->so_iptos;
 
 	error = ip_output(so, m);
+	    break;
+
+	default:
+	    g_assert_not_reached();
+	}
 
 	if (error) {
 out:
slirp/tcp_subr.c +29 -8
index cd021df..ee101e2 100644
@@ -78,12 +78,19 @@ tcp_template(struct tcpcb *tp)
 	n->ti_mbuf = NULL;
 	memset(&n->ti, 0, sizeof(n->ti));
 	n->ti_x0 = 0;
+	switch (so->so_ffamily) {
+	case AF_INET:
 	n->ti_pr = IPPROTO_TCP;
 	n->ti_len = htons(sizeof(struct tcphdr));
 	n->ti_src = so->so_faddr;
 	n->ti_dst = so->so_laddr;
 	n->ti_sport = so->so_fport;
 	n->ti_dport = so->so_lport;
+	    break;
+
+	default:
+	    g_assert_not_reached();
+	}
 
 	n->ti_seq = 0;
 	n->ti_ack = 0;
@@ -110,7 +117,7 @@ tcp_template(struct tcpcb *tp)
  */
 void
 tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
-            tcp_seq ack, tcp_seq seq, int flags)
+            tcp_seq ack, tcp_seq seq, int flags, unsigned short af)
 {
 	register int tlen;
 	int win = 0;
@@ -144,8 +151,14 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
 		m->m_len = sizeof (struct tcpiphdr);
 		tlen = 0;
 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
+		switch (af) {
+		case AF_INET:
 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, uint32_t);
 		xchg(ti->ti_dport, ti->ti_sport, uint16_t);
+		    break;
+		default:
+		    g_assert_not_reached();
+		}
 #undef xchg
 	}
 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
@@ -168,11 +181,15 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
 	ti->ti_sum = cksum(m, tlen);
 
 	struct tcpiphdr tcpiph_save = *(mtod(m, struct tcpiphdr *));
+	struct ip *ip;
+
+	switch (af) {
+	case AF_INET:
 	m->m_data += sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
 	                                     - sizeof(struct ip);
 	m->m_len  -= sizeof(struct tcpiphdr) - sizeof(struct tcphdr)
 	                                     - sizeof(struct ip);
-	struct ip *ip = mtod(m, struct ip *);
+	ip = mtod(m, struct ip *);
 	ip->ip_len = tlen;
 	ip->ip_dst = tcpiph_save.ti_dst;
 	ip->ip_src = tcpiph_save.ti_src;
@@ -185,6 +202,11 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
 	}
 
 	(void) ip_output((struct socket *)0, m);
+	    break;
+
+	default:
+	    g_assert_not_reached();
+	}
 }
 
 /*
@@ -388,8 +410,8 @@ void tcp_connect(struct socket *inso)
 {
     Slirp *slirp = inso->slirp;
     struct socket *so;
-    struct sockaddr_in addr;
-    socklen_t addrlen = sizeof(struct sockaddr_in);
+    struct sockaddr_storage addr;
+    socklen_t addrlen = sizeof(struct sockaddr_storage);
     struct tcpcb *tp;
     int s, opt;
 
@@ -414,9 +436,8 @@ void tcp_connect(struct socket *inso)
             free(so); /* NOT sofree */
             return;
         }
-        so->so_lfamily = AF_INET;
-        so->so_laddr = inso->so_laddr;
-        so->so_lport = inso->so_lport;
+        so->lhost = inso->lhost;
+        so->so_ffamily = inso->so_ffamily;
     }
 
     tcp_mss(sototcpcb(so), 0);
@@ -432,7 +453,7 @@ void tcp_connect(struct socket *inso)
     qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
     socket_set_nodelay(s);
 
-    so->fhost.sin = addr;
+    so->fhost.ss = addr;
     sotranslate_accept(so);
 
     /* Close the accept() socket, set right state */
slirp/tcp_timer.c +2 -1
index 1214c2e..8f5dd77 100644
@@ -278,7 +278,8 @@ tcp_timers(register struct tcpcb *tp, int timer)
 			 * correspondent TCP to respond.
 			 */
 			tcp_respond(tp, &tp->t_template, (struct mbuf *)NULL,
-			    tp->rcv_nxt, tp->snd_una - 1, 0);
+			    tp->rcv_nxt, tp->snd_una - 1, 0,
+			    tp->t_socket->so_ffamily);
 			tp->t_timer[TCPT_KEEP] = TCPTV_KEEPINTVL;
 		} else
 			tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_IDLE;
Signature · 1 line
2.7.0
#1 lore raw

PATCH v9 1/10 slirp: Adding IPv6, ICMPv6 Echo and NDP autoconfiguration

In reply to Samuel Thibault

From: Guillaume Subiron <[email protected]>

This patch adds the functions needed to handle IPv6 packets. ICMPv6 and NDP headers are implemented.

Slirp is now able to send NDP Router or Neighbor Advertisement when it receives Router or Neighbor Solicitation. Using a 64bit-sized IPv6 prefix, the guest is now able to perform stateless autoconfiguration (SLAAC) and to compute its IPv6 address.

This patch adds an ndp_table, mainly inspired by arp_table, to keep an NDP cache and manage network address resolution.
Slirp regularly sends NDP Neighbor Advertisement, as recommended by the RFC, to make the guest refresh its route.

This also adds ip6_cksum() to compute ICMPv6 checksums using IPv6 pseudo-header.

Some #define ETH_* are moved upper in slirp.h to make them accessible to other slirp/*.h

---
 slirp/Makefile.objs |   6 +-
 slirp/cksum.c       |  25 ++++
 slirp/if.c          |   2 +-
 slirp/ip6.h         | 138 +++++++++++++++++++++
 slirp/ip6_icmp.c    | 347 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 slirp/ip6_icmp.h    | 203 ++++++++++++++++++++++++++++++
 slirp/ip6_input.c   |  69 +++++++++++
 slirp/ip6_output.c  |  38 ++++++
 slirp/ndp_table.c   |  84 +++++++++++++
 slirp/slirp.c       |  51 +++++++-
 slirp/slirp.h       |  37 ++++++
 slirp/socket.h      |   7 ++
 12 files changed, 1001 insertions(+), 6 deletions(-)
 create mode 100644 slirp/ip6.h
 create mode 100644 slirp/ip6_icmp.c
 create mode 100644 slirp/ip6_icmp.h
 create mode 100644 slirp/ip6_input.c
 create mode 100644 slirp/ip6_output.c
 create mode 100644 slirp/ndp_table.c
slirp/Makefile.objs +4 -2
index 2daa9dc..4e3a289 100644
@@ -1,3 +1,5 @@
-common-obj-y = cksum.o if.o ip_icmp.o ip_input.o ip_output.o dnssearch.o
+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
 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 bootp.o tftp.o arp_table.o
+common-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o arp_table.o \
+                ndp_table.o
slirp/cksum.c +25 -0
index bc0d017..2ad0e65 100644
@@ -138,3 +138,28 @@ cont:
 	REDUCE;
 	return (~sum & 0xffff);
 }
+
+int ip6_cksum(struct mbuf *m)
+{
+    /* TODO: Optimize this by being able to pass the ip6_pseudohdr to cksum
+     * separately from the mbuf */
+    struct ip6 save_ip, *ip = mtod(m, struct ip6 *);
+    struct ip6_pseudohdr *ih = mtod(m, struct ip6_pseudohdr *);
+    int sum;
+
+    save_ip = *ip;
+
+    ih->ih_src = save_ip.ip_src;
+    ih->ih_dst = save_ip.ip_dst;
+    ih->ih_pl = htonl((uint32_t)ntohs(save_ip.ip_pl));
+    ih->ih_zero_hi = 0;
+    ih->ih_zero_lo = 0;
+    ih->ih_nh = save_ip.ip_nh;
+
+    sum = cksum(m, ((int)sizeof(struct ip6_pseudohdr))
+                    + ntohl(ih->ih_pl));
+
+    *ip = save_ip;
+
+    return sum;
+}
slirp/if.c +1 -1
index 93d7cc0..2e21f43 100644
@@ -194,7 +194,7 @@ void if_start(Slirp *slirp)
 
         /* Try to send packet unless it already expired */
         if (ifm->expiration_date >= now && !if_encap(slirp, ifm)) {
-            /* Packet is delayed due to pending ARP resolution */
+            /* Packet is delayed due to pending ARP or NDP resolution */
             continue;
         }
 
slirp/ip6.h new file +138 -0

138 lines not on this page · raw message

slirp/ip6_icmp.c new file +347 -0

347 lines not on this page · raw message

slirp/ip6_icmp.h new file +203 -0

203 lines not on this page · raw message

slirp/ip6_input.c new file +69 -0

69 lines not on this page · raw message

slirp/ip6_output.c new file +38 -0
new file mode 100644
index 0000000..2dd80f4
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2013
+ * Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
+ */
+
+#include "slirp.h"
+
+/* Number of packets queued before we start sending
+ * (to prevent allocing too many mbufs) */
+#define IF6_THRESH 10
+
+/*
+ * IPv6 output. The packet in mbuf chain m contains a IP header
+ */
+int ip6_output(struct socket *so, struct mbuf *m, int fast)
+{
+    struct ip6 *ip = mtod(m, struct ip6 *);
+
+    DEBUG_CALL("ip6_output");
+    DEBUG_ARG("so = %lx", (long)so);
+    DEBUG_ARG("m = %lx", (long)m);
+
+    /* Fill IPv6 header */
+    ip->ip_v = IP6VERSION;
+    ip->ip_hl = IP6_HOP_LIMIT;
+    ip->ip_tc_hi = 0;
+    ip->ip_tc_lo = 0;
+    ip->ip_fl_hi = 0;
+    ip->ip_fl_lo = 0;
+
+    if (fast) {
+        if_encap(m->slirp, m);
+    } else {
+        if_output(so, m);
+    }
+
+    return 0;
+}
slirp/ndp_table.c new file +84 -0

84 lines not on this page · raw message

slirp/slirp.c +48 -3

101 lines not on this page · raw message

slirp/slirp.h +37 -0
index 239fe29..15775bc 100644
@@ -131,6 +131,8 @@ void free(void *ptr);
 #include <sys/stropts.h>
 #endif
 
+#include <glib.h>
+
 #include "debug.h"
 
 #include "qemu/queue.h"
@@ -139,12 +141,14 @@ void free(void *ptr);
 
 #include "libslirp.h"
 #include "ip.h"
+#include "ip6.h"
 #include "tcp.h"
 #include "tcp_timer.h"
 #include "tcp_var.h"
 #include "tcpip.h"
 #include "udp.h"
 #include "ip_icmp.h"
+#include "ip6_icmp.h"
 #include "mbuf.h"
 #include "sbuf.h"
 #include "socket.h"
@@ -196,6 +200,23 @@ void arp_table_add(Slirp *slirp, uint32_t ip_addr, uint8_t ethaddr[ETH_ALEN]);
 bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
                       uint8_t out_ethaddr[ETH_ALEN]);
 
+struct ndpentry {
+    unsigned char   eth_addr[ETH_ALEN];     /* sender hardware address */
+    struct in6_addr ip_addr;                /* sender IP address       */
+} QEMU_PACKED;
+
+#define NDP_TABLE_SIZE 16
+
+typedef struct NdpTable {
+    struct ndpentry table[NDP_TABLE_SIZE];
+    int next_victim;
+} NdpTable;
+
+void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
+                   uint8_t ethaddr[ETH_ALEN]);
+bool ndp_table_search(Slirp *slirp, struct in6_addr ip_addr,
+                      uint8_t out_ethaddr[ETH_ALEN]);
+
 struct Slirp {
     QTAILQ_ENTRY(Slirp) entry;
     u_int time_fasttimo;
@@ -206,6 +227,9 @@ struct Slirp {
     struct in_addr vnetwork_addr;
     struct in_addr vnetwork_mask;
     struct in_addr vhost_addr;
+    struct in6_addr vprefix_addr6;
+    uint8_t vprefix_len;
+    struct in6_addr vhost_addr6;
     struct in_addr vdhcp_startaddr;
     struct in_addr vnameserver_addr;
 
@@ -254,6 +278,10 @@ struct Slirp {
     struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
 
     ArpTable arp_table;
+    NdpTable ndp_table;
+
+    GRand *grand;
+    QEMUTimer *ra_timer;
 
     void *opaque;
 };
@@ -296,6 +324,7 @@ int translate_dnssearch(Slirp *s, const char ** names);
 
 /* cksum.c */
 int cksum(struct mbuf *m, int len);
+int ip6_cksum(struct mbuf *m);
 
 /* if.c */
 void if_init(Slirp *);
@@ -311,6 +340,14 @@ void ip_stripoptions(register struct mbuf *, struct mbuf *);
 /* ip_output.c */
 int ip_output(struct socket *, struct mbuf *);
 
+/* ip6_input.c */
+void ip6_init(Slirp *);
+void ip6_cleanup(Slirp *);
+void ip6_input(struct mbuf *);
+
+/* ip6_output */
+int ip6_output(struct socket *, struct mbuf *, int fast);
+
 /* tcp_input.c */
 void tcp_input(register struct mbuf *, int, struct socket *);
 int tcp_mss(register struct tcpcb *, u_int);
slirp/socket.h +7 -0
index c4afc94..bcebce1 100644
@@ -102,6 +102,13 @@ static inline int sockaddr_equal(struct sockaddr_storage *a,
         return a4->sin_addr.s_addr == b4->sin_addr.s_addr
                && a4->sin_port == b4->sin_port;
     }
+    case AF_INET6:
+    {
+        struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) a;
+        struct sockaddr_in6 *b6 = (struct sockaddr_in6 *) b;
+        return (in6_equal(&a6->sin6_addr, &b6->sin6_addr)
+                && a6->sin6_port == b6->sin6_port);
+    }
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.7.0
Reviewed-by on v9 4/10
#13 lore raw

Re: [Qemu-devel] [PATCHv9 04/10] slirp: Factorizing tcpiphdr structure with an union

In reply to Samuel Thibault
Quoted 12 lines · Samuel Thibault

From: Guillaume Subiron <[email protected]>

This patch factorizes the tcpiphdr structure to put the IPv4 fields in an union, for addition of version 6 in further patch.
Using some macros, retrocompatibility of the existing code is assured.

This patch also fixes the SLIRP_MSIZE and margin computation in various functions, and makes them compatible with the new tcpiphdr structure, whose size will be bigger than sizeof(struct tcphdr) + sizeof(struct ip)

Quoted 10 lines
---
 slirp/if.h         |  4 ++--
 slirp/mbuf.c       |  4 ++--
 slirp/slirp.c      | 15 ++++++++-------
 slirp/socket.c     | 13 ++++++++++++-
 slirp/tcp_input.c  | 31 ++++++++++++++++++++-----------
 slirp/tcp_output.c | 18 +++++++++++++-----
 slirp/tcp_subr.c   | 31 ++++++++++++++++++++++---------
 slirp/tcpip.h      | 31 +++++++++++++++++++++++--------
 8 files changed, 102 insertions(+), 45 deletions(-)

Sounds reasonable to me now.

Reviewed-by on v9 8/10
#21 lore raw

Re: [Qemu-devel] [PATCHv9 08/10] slirp: Adding IPv6 address for DNS relay

In reply to Samuel Thibault
Quoted 11 lines · Samuel Thibault

From: Guillaume Subiron <[email protected]>

This patch adds an IPv6 address to the DNS relay. in6_equal_dns() is developed using this Slirp attribute. sotranslate_in/out/accept() are also updated to manage the IPv6 case so the guest can be able to join the host using one of the Slirp addresses.

For now this only points to localhost. Further development will be needed to automatically fetch the IPv6 address from resolv.conf, and announce this via RDNSS.

Quoted 8 lines
---
 slirp/ip6.h    |  6 +++++-
 slirp/slirp.c  |  1 +
 slirp/slirp.h  |  1 +
 slirp/socket.c | 32 ++++++++++++++++++++++++++++++++
 4 files changed, 39 insertions(+), 1 deletion(-)
#23 lore raw

Re: [Qemu-devel] [PATCHv9 09/10] qapi-schema, qemu-options & slirp: Adding Qemu options for IPv6 addresses

In reply to Samuel Thibault
Quoted 18 lines · Samuel Thibault

From: Yann Bordenave <[email protected]>

This patch adds parameters to manage some new options in the qemu -net command.
Slirp IPv6 address, network prefix, and DNS IPv6 address can be given in argument to the qemu command.
Defaults parameters are respectively fec0::2, fec0::, /64 and fec0::3.

---
 net/net.c        | 31 +++++++++++++++++++++++++
 net/slirp.c      | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 qapi-schema.json | 40 ++++++++++++++++++++------------
 qemu-options.hx  | 18 +++++++++++++--
 slirp/libslirp.h |  8 ++++---
 slirp/slirp.c    | 16 +++++++------
 6 files changed, 150 insertions(+), 32 deletions(-)

...

Quoted 46 lines
net/slirp.c +13 -2
index 6b51fbc..bd712bc 100644
@@ -36,6 +36,7 @@
 #include "qemu/error-report.h"
 #include "qemu/sockets.h"
 #include "slirp/libslirp.h"
+#include "slirp/ip6.h"
 #include "sysemu/char.h"
 
 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
@@ -135,10 +136,13 @@ static NetClientInfo net_slirp_info = {
 static int net_slirp_init(NetClientState *peer, const char *model,
                           const char *name, int restricted,
                           const char *vnetwork, const char *vhost,
+                          const char *vprefix6, int vprefix6_len,
+                          const char *vhost6,
                           const char *vhostname, const char *tftp_export,
                           const char *bootfile, const char *vdhcp_start,
-                          const char *vnameserver, const char *smb_export,
-                          const char *vsmbserver, const char **dnssearch)
+                          const char *vnameserver, const char *vnameserver6,
+                          const char *smb_export, const char *vsmbserver,
+                          const char **dnssearch)
 {
     /* default settings according to historic slirp */
     struct in_addr net  = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
@@ -146,6 +150,9 @@ static int net_slirp_init(NetClientState *peer, const char *model,
     struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
     struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
     struct in_addr dns  = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
+    struct in6_addr ip6_prefix;
+    struct in6_addr ip6_host;
+    struct in6_addr ip6_dns;
 #ifndef _WIN32
     struct in_addr smbsrv = { .s_addr = 0 };
 #endif
@@ -235,6 +242,52 @@ static int net_slirp_init(NetClientState *peer, const char *model,
     }
 #endif
 
+
+    if (!vprefix6) {
+        vprefix6 = "fec0::";
+    }

net/slirp.c static int net_slirp_init(NetClientState *peer, const char *model, · line 258

Quoted 10 lines
net/slirp.c +10 -0
+    if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
+        return -1;
+    }
+
+    if (!vprefix6_len) {
+        vprefix6_len = 64;
+    }
+    if (vprefix6_len < 0 || vprefix6_len > 128) {
+        return -1;
+    }

I think you could also immediately check for vprefix6_len > 126 here already, then you could drop the two checks for 126 below. A prefix that is bigger than 126 does not make much sense anyway, so I think it should be ok to always disallow this (especially since there will be at least three hosts in this network - the host, the guest, and the DNS server).

Quoted 19 lines
net/slirp.c +19 -0
+    if (vhost6) {
+        if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
+            return -1;
+        }
+        if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
+            return -1;
+        }
+    } else {
+        if (vprefix6_len > 126) {
+            return -1;
+        }
+        ip6_host = ip6_prefix;
+        ip6_host.s6_addr[15] |= 2;
+    }
+
+    if (vnameserver6) {
+        if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
+            return -1;
+        }

net/slirp.c static int net_slirp_init(NetClientState *peer, const char *model, · line 288

Quoted 10 lines
net/slirp.c +10 -0
+        if (!in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
+            return -1;
+        }
+    } else {
+        if (vprefix6_len > 126) {
+            return -1;
+        }
+        ip6_dns = ip6_prefix;
+        ip6_dns.s6_addr[15] |= 3;
+    }

The other parts of the patch looks fine to me now.

Thomas

#29 lore raw
In reply to Samuel Thibault
Quoted 6 lines · Samuel Thibault

Hello,

This is another respin of IPv6 in Qemu -net user mode.

These patches add ICMPv6, NDP, make UDP and TCP compatible with IPv6, and add TFTP over IPv6.

*ping*

Jan, Jason,

could you please have a look at this series? Would be cool to include it for 2.6 so that we'd finally have IPv6 support in Slirp, too. As far as I could see, the patches look fine now - there was just one rather cosmetic issue left in patch 9/10, but it should also be ok in the current shape already, I think, so IMHO no need for a respin.

Thomas

#30 lore raw
Quoted 10 lines · Thomas Huth
Quoted 6 lines · Samuel Thibault

Hello,

This is another respin of IPv6 in Qemu -net user mode.

These patches add ICMPv6, NDP, make UDP and TCP compatible with IPv6, and add TFTP over IPv6.

*ping*

Quoted 7 lines

Jan, Jason,

could you please have a look at this series? Would be cool to include it for 2.6 so that we'd finally have IPv6 support in Slirp, too. As far as I could see, the patches look fine now - there was just one rather cosmetic issue left in patch 9/10, but it should also be ok in the current shape already, I think, so IMHO no need for a respin.

As indicated before, we need someone else than me to manage the slirp system. I'm out of bandwidth for this task, sorry.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA ITP SES-DE
Corporate Competence Center Embedded Linux
#31 lore raw

Hello,

Quoted 13 lines · Jan Kiszka, on Fri 04 Mar 2016 16:50:32 +0100, wrote:
Quoted 12 lines · Thomas Huth
Quoted 6 lines · Samuel Thibault

Hello,

This is another respin of IPv6 in Qemu -net user mode.

These patches add ICMPv6, NDP, make UDP and TCP compatible with IPv6, and add TFTP over IPv6.

*ping*

Jan, Jason,

Quoted 8 lines
Quoted 5 lines

could you please have a look at this series? Would be cool to include it for 2.6 so that we'd finally have IPv6 support in Slirp, too. As far as I could see, the patches look fine now - there was just one rather cosmetic issue left in patch 9/10, but it should also be ok in the current shape already, I think, so IMHO no need for a respin.

As indicated before, we need someone else than me to manage the slirp system. I'm out of bandwidth for this task, sorry.

Ok, Jason?

I'm really surprised that a patch that has been reviewed two times (by me then by Thomas) seems so difficult to get it. I understand that it's far from trivial, but I have concerns with how qemu can manage contributions.

Samuel

#32 lore raw
Quoted 22 lines · Samuel Thibault

Hello,

Quoted 12 lines · Jan Kiszka, on Fri 04 Mar 2016 16:50:32 +0100, wrote:
Quoted 11 lines · Thomas Huth
Quoted 6 lines · Samuel Thibault

Hello,

This is another respin of IPv6 in Qemu -net user mode.

These patches add ICMPv6, NDP, make UDP and TCP compatible with IPv6, and add TFTP over IPv6.

*ping*

Jan, Jason,

Quoted 7 lines
Quoted 5 lines

could you please have a look at this series? Would be cool to include it for 2.6 so that we'd finally have IPv6 support in Slirp, too. As far as I could see, the patches look fine now - there was just one rather cosmetic issue left in patch 9/10, but it should also be ok in the current shape already, I think, so IMHO no need for a respin.

As indicated before, we need someone else than me to manage the slirp system. I'm out of bandwidth for this task, sorry.

Quoted 8 lines

Ok, Jason?

I'm really surprised that a patch that has been reviewed two times (by me then by Thomas) seems so difficult to get it. I understand that it's far from trivial, but I have concerns with how qemu can manage contributions.

Samuel

Sorry. I wasn't aware of needing a new maintainer for slirp before. Will look at this series soon and take slirp until we have a new maintainer.

Btw, do you want to be the maintainer of slirp? (Or maybe Tohmas want to do this?)

Thanks

#40 lore raw
In reply to Samuel Thibault
Quoted 8 lines · Samuel Thibault

Hello,

This is another respin of IPv6 in Qemu -net user mode.

These patches add ICMPv6, NDP, make UDP and TCP compatible with IPv6, and add TFTP over IPv6.

Thanks a lot for the patches. Looks good overall, but see some issues:

- the series does not build on w32:
In file included from ./slirp/slirp.h:126:0,
from stubs/slirp.c:3:
./slirp/ip6.h:9:24: fatal error: netinet/in.h: No such file or directory compilation terminated.
- Lots of checkpatch warnings, let's try to silent it.
- The patches do not apply to master cleanly.
- I expects a unit-test for this. You may want to have a look at the pxe-test in tests/, I think it could be extended to test ipv6 slirp somehow.

And also some nits, see individual patches.

Quoted 67 lines

Difference with version 8 is:
- Fix freeing random number generator
- Fix coding style
- Fix prefix lengths in in6_equal_dns macro

Here is a summary of the patches:
Guillaume Subiron (7):
slirp: Adding IPv6, ICMPv6 Echo and NDP autoconfiguration
slirp: Adding IPv6 UDP support
slirp: Factorizing tcpiphdr structure with an union
slirp: Generalizing and neutralizing various TCP functions before
adding IPv6 stuff
slirp: Reindent after refactoring
slirp: Handle IPv6 in TCP functions
slirp: Adding IPv6 address for DNS relay

Thomas Huth (1):
slirp: Add IPv6 support to the TFTP code

Yann Bordenave (2):
slirp: Adding ICMPv6 error sending
qapi-schema, qemu-options & slirp: Adding Qemu options for IPv6
addresses

 net/net.c           |  31 ++++
 net/slirp.c         |  69 ++++++++-
 qapi-schema.json    |  40 +++--
 qemu-options.hx     |  18 ++-
 slirp/Makefile.objs |   6 +-
 slirp/cksum.c       |  25 ++++
 slirp/if.c          |   2 +-
 slirp/if.h          |   4 +-
 slirp/ip6.h         | 142 ++++++++++++++++++
 slirp/ip6_icmp.c    | 411 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 slirp/ip6_icmp.h    | 213 +++++++++++++++++++++++++++
 slirp/ip6_input.c   |  73 ++++++++++
 slirp/ip6_output.c  |  38 +++++
 slirp/ip_icmp.c     |  12 +-
 slirp/ip_icmp.h     |   4 +-
 slirp/ip_input.c    |  10 +-
 slirp/libslirp.h    |   8 +-
 slirp/mbuf.c        |   4 +-
 slirp/ndp_table.c   |  84 +++++++++++
 slirp/slirp.c       |  81 +++++++++--
 slirp/slirp.h       |  43 +++++-
 slirp/socket.c      |  54 ++++++-
 slirp/socket.h      |  13 ++
 slirp/tcp.h         |   2 +
 slirp/tcp_input.c   | 181 ++++++++++++++++-------
 slirp/tcp_output.c  |  51 +++++--
 slirp/tcp_subr.c    | 114 +++++++++++----
 slirp/tcp_timer.c   |   3 +-
 slirp/tcpip.h       |  40 ++++-
 slirp/tftp.c        | 133 +++++++++--------
 slirp/tftp.h        |   7 +-
 slirp/udp.c         |  19 ++-
 slirp/udp.h         |   5 +
 slirp/udp6.c        | 167 +++++++++++++++++++++
 34 files changed, 1877 insertions(+), 230 deletions(-)
 create mode 100644 slirp/ip6.h
 create mode 100644 slirp/ip6_icmp.c
 create mode 100644 slirp/ip6_icmp.h
 create mode 100644 slirp/ip6_input.c
 create mode 100644 slirp/ip6_output.c
 create mode 100644 slirp/ndp_table.c
 create mode 100644 slirp/udp6.c
#2 lore raw

Re: [Qemu-devel] [PATCHv9 01/10] slirp: Adding IPv6, ICMPv6 Echo and NDP autoconfiguration

In reply to Samuel Thibault
Quoted 45 lines · Samuel Thibault

From: Guillaume Subiron <[email protected]>

This patch adds the functions needed to handle IPv6 packets. ICMPv6 and NDP headers are implemented.

Slirp is now able to send NDP Router or Neighbor Advertisement when it receives Router or Neighbor Solicitation. Using a 64bit-sized IPv6 prefix, the guest is now able to perform stateless autoconfiguration (SLAAC) and to compute its IPv6 address.

This patch adds an ndp_table, mainly inspired by arp_table, to keep an NDP cache and manage network address resolution.
Slirp regularly sends NDP Neighbor Advertisement, as recommended by the RFC, to make the guest refresh its route.

This also adds ip6_cksum() to compute ICMPv6 checksums using IPv6 pseudo-header.

Some #define ETH_* are moved upper in slirp.h to make them accessible to other slirp/*.h

---
 slirp/Makefile.objs |   6 +-
 slirp/cksum.c       |  25 ++++
 slirp/if.c          |   2 +-
 slirp/ip6.h         | 138 +++++++++++++++++++++
 slirp/ip6_icmp.c    | 347 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 slirp/ip6_icmp.h    | 203 ++++++++++++++++++++++++++++++
 slirp/ip6_input.c   |  69 +++++++++++
 slirp/ip6_output.c  |  38 ++++++
 slirp/ndp_table.c   |  84 +++++++++++++
 slirp/slirp.c       |  51 +++++++-
 slirp/slirp.h       |  37 ++++++
 slirp/socket.h      |   7 ++
 12 files changed, 1001 insertions(+), 6 deletions(-)
 create mode 100644 slirp/ip6.h
 create mode 100644 slirp/ip6_icmp.c
 create mode 100644 slirp/ip6_icmp.h
 create mode 100644 slirp/ip6_input.c
 create mode 100644 slirp/ip6_output.c
 create mode 100644 slirp/ndp_table.c

[...]

slirp/ip6_input.c line 59

Quoted 13 lines
slirp/ip6_input.c new file +13 -0
+
+    /*
+     * Switch out to protocol's input routine.
+     */
+    switch (ip6->ip_nh) {
+#if 0
+    case IPPROTO_TCP:
+        tcp_input(m, hlen, (struct socket *)NULL);
+        break;
+    case IPPROTO_UDP:
+        udp_input(m, hlen);
+        break;
+#endif

This looks odd, why need this?

#6 lore raw

Re: [Qemu-devel] [PATCHv9 02/10] slirp: Adding ICMPv6 error sending

In reply to Samuel Thibault
Quoted 9 lines · Samuel Thibault

From: Yann Bordenave <[email protected]>

Disambiguation : icmp_error is renamed into icmp_send_error, since it doesn't manage errors, but only sends ICMP Error messages.

Adding icmp6_send_error to send ICMPv6 Error messages. This function is simpler than the v4 version.
Adding some calls in various functions to send ICMP errors, when a received packet is too big, or when its hop limit is 0.

Let's split this into two patches to avoid mixing renaming and new function introduction.

#9 lore raw

Re: [Qemu-devel] [PATCHv9 03/10] slirp: Adding IPv6 UDP support

In reply to Samuel Thibault
Quoted 219 lines · Samuel Thibault

From: Guillaume Subiron <[email protected]>

This adds the sin6 case in the fhost and lhost unions and related macros. It adds udp6_input() and udp6_output().
It adds the IPv6 case in sorecvfrom().
Finally, udp_input() is called by ip6_input().

---
 slirp/Makefile.objs |   2 +-
 slirp/ip6_input.c   |   2 +-
 slirp/socket.c      |   5 ++
 slirp/socket.h      |   6 ++
 slirp/udp.h         |   5 ++
 slirp/udp6.c        | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 175 insertions(+), 2 deletions(-)
 create mode 100644 slirp/udp6.c
slirp/Makefile.objs +1 -1
index 4e3a289..6748e4f 100644
@@ -1,5 +1,5 @@
 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
 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 bootp.o tftp.o arp_table.o \
+common-obj-y += tcp_subr.o tcp_timer.o udp.o udp6.o bootp.o tftp.o arp_table.o \
                 ndp_table.o
slirp/ip6_input.c +1 -1
index b6a438d..9ca6d32 100644
@@ -58,7 +58,7 @@ void ip6_input(struct mbuf *m)
         icmp6_send_error(m, ICMP6_UNREACH, ICMP6_UNREACH_NO_ROUTE);
         break;
     case IPPROTO_UDP:
-        icmp6_send_error(m, ICMP6_UNREACH, ICMP6_UNREACH_NO_ROUTE);
+        udp6_input(m);
         break;
     case IPPROTO_ICMPV6:
         icmp6_input(m);
slirp/socket.c +5 -0
index 32b1ba3..b79ddec 100644
@@ -541,7 +541,12 @@ sorecvfrom(struct socket *so)
 	                   (struct sockaddr_in *) &daddr,
 	                   so->so_iptos);
 	        break;
+	    case AF_INET6:
+	        udp6_output(so, m, (struct sockaddr_in6 *) &saddr,
+	                    (struct sockaddr_in6 *) &daddr);
+	        break;
 	    default:
+	        g_assert_not_reached();
 	        break;
 	    }
 	  } /* rx error */
slirp/socket.h +6 -0
index bcebce1..e9c9b05 100644
@@ -34,17 +34,23 @@ struct socket {
   union {   /* foreign host */
       struct sockaddr_storage ss;
       struct sockaddr_in sin;
+      struct sockaddr_in6 sin6;
   } fhost;
 #define so_faddr fhost.sin.sin_addr
 #define so_fport fhost.sin.sin_port
+#define so_faddr6 fhost.sin6.sin6_addr
+#define so_fport6 fhost.sin6.sin6_port
 #define so_ffamily fhost.ss.ss_family
 
   union {   /* local host */
       struct sockaddr_storage ss;
       struct sockaddr_in sin;
+      struct sockaddr_in6 sin6;
   } lhost;
 #define so_laddr lhost.sin.sin_addr
 #define so_lport lhost.sin.sin_port
+#define so_laddr6 lhost.sin6.sin6_addr
+#define so_lport6 lhost.sin6.sin6_port
 #define so_lfamily lhost.ss.ss_family
 
   uint8_t	so_iptos;	/* Type of service */
slirp/udp.h +5 -0
index 2f9de38..10cc780 100644
@@ -83,4 +83,9 @@ struct socket * udp_listen(Slirp *, uint32_t, u_int, uint32_t, u_int,
 int udp_output(struct socket *so, struct mbuf *m,
                 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
                 int iptos);
+
+void udp6_input(register struct mbuf *);
+int udp6_output(struct socket *so, struct mbuf *m,
+                struct sockaddr_in6 *saddr, struct sockaddr_in6 *daddr);
+
 #endif
slirp/udp6.c new file +110 -0
new file mode 100644
index 0000000..8952de2
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2013
+ * Guillaume Subiron
+ */
+
+#include "slirp.h"
+#include "qemu/osdep.h"
+#include "udp.h"
+
+void udp6_input(struct mbuf *m)
+{
+    Slirp *slirp = m->slirp;
+    struct ip6 *ip, save_ip;
+    struct udphdr *uh;
+    int hlen = sizeof(struct ip6);
+    int len;
+    struct socket *so;
+    struct sockaddr_in6 lhost;
+
+    DEBUG_CALL("udp6_input");
+    DEBUG_ARG("m = %lx", (long)m);
+
+    if (slirp->restricted) {
+        goto bad;
+    }
+
+    ip = mtod(m, struct ip6 *);
+    m->m_len -= hlen;
+    m->m_data += hlen;
+    uh = mtod(m, struct udphdr *);
+    m->m_len += hlen;
+    m->m_data -= hlen;
+
+    if (ip6_cksum(m)) {
+        goto bad;
+    }
+
+    len = ntohs((uint16_t)uh->uh_ulen);
+
+    /*
+     * Make mbuf data length reflect UDP length.
+     * If not enough data to reflect UDP length, drop.
+     */
+    if (ntohs(ip->ip_pl) != len) {
+        if (len > ntohs(ip->ip_pl)) {
+            goto bad;
+        }
+        m_adj(m, len - ntohs(ip->ip_pl));
+        ip->ip_pl = htons(len);
+    }
+
+    /*
+     * Save a copy of the IP header in case we want restore it
+     * for sending an ICMP error message in response.
+     */
+    save_ip = *ip;
+
+    /* TODO handle DHCP/BOOTP */
+    /* TODO handle TFTP */
+
+    /* Locate pcb for datagram. */
+    lhost.sin6_family = AF_INET6;
+    lhost.sin6_addr = ip->ip_src;
+    lhost.sin6_port = uh->uh_sport;
+
+    so = solookup(&slirp->udp_last_so, &slirp->udb,
+                  (struct sockaddr_storage *) &lhost, NULL);
+
+    if (so == NULL) {
+        /* If there's no socket for this packet, create one. */
+        so = socreate(slirp);
+        if (!so) {
+            goto bad;
+        }
+        if (udp_attach(so, AF_INET6) == -1) {
+            DEBUG_MISC((dfd, " udp6_attach errno = %d-%s\n",
+                        errno, strerror(errno)));
+            sofree(so);
+            goto bad;
+        }
+
+        /* Setup fields */
+        so->so_lfamily = AF_INET6;
+        so->so_laddr6 = ip->ip_src;
+        so->so_lport6 = uh->uh_sport;
+    }
+
+    so->so_ffamily = AF_INET6;
+    so->so_faddr6 = ip->ip_dst; /* XXX */
+    so->so_fport6 = uh->uh_dport; /* XXX */
+
+    hlen += sizeof(struct udphdr);
+    m->m_len -= hlen;
+    m->m_data += hlen;
+
+    /*
+     * Now we sendto() the packet.
+     */
+    if (sosendto(so, m) == -1) {
+        m->m_len += hlen;
+        m->m_data -= hlen;
+        *ip = save_ip;
+        DEBUG_MISC((dfd, "udp tx errno = %d-%s\n", errno, strerror(errno)));
+        /* TODO: ICMPv6 error */
+        /*icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));*/
+        goto bad;
+    }
+
+    m_free(so->so_m);   /* used for ICMP if error on sorecvfrom */
+

slirp/udp6.c line 120

Quoted 10 lines
slirp/udp6.c new file +10 -0
+    /* restore the orig mbuf packet */
+    m->m_len += hlen;
+    m->m_data -= hlen;
+    *ip = save_ip;
+    so->so_m = m;
+
+    return;
+bad:
+    m_free(m);
+}

Part of the codes looks duplicated with ipv4 version. Any chances to merge them like tcp?

#16 lore raw

Re: [Qemu-devel] [PATCHv9 06/10] slirp: Reindent after refactoring

In reply to Samuel Thibault
Quoted 11 lines · Samuel Thibault

From: Guillaume Subiron <[email protected]>

No code change.

---
 slirp/tcp_input.c  | 92 +++++++++++++++++++++++++++---------------------------
 slirp/tcp_output.c | 29 +++++++++--------
 slirp/tcp_subr.c   | 50 ++++++++++++++---------------

How about other files in slirp/ ?

#24 lore raw

Re: [Qemu-devel] [PATCHv9 09/10] qapi-schema, qemu-options & slirp: Adding Qemu options for IPv6 addresses

In reply to Samuel Thibault
Quoted 21 lines · Samuel Thibault

From: Yann Bordenave <[email protected]>

This patch adds parameters to manage some new options in the qemu -net command.
Slirp IPv6 address, network prefix, and DNS IPv6 address can be given in argument to the qemu command.
Defaults parameters are respectively fec0::2, fec0::, /64 and fec0::3.

---
 net/net.c        | 31 +++++++++++++++++++++++++
 net/slirp.c      | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 qapi-schema.json | 40 ++++++++++++++++++++------------
 qemu-options.hx  | 18 +++++++++++++--
 slirp/libslirp.h |  8 ++++---
 slirp/slirp.c    | 16 +++++++------
 6 files changed, 150 insertions(+), 32 deletions(-)
net/net.c +0 -0
index aebf753..d67904f 100644

[...]

Quoted 47 lines
 
     while (slirp_configs) {
qapi-schema.json +16 -14
index 8d04897..a62c2ec 100644
@@ -2399,6 +2399,14 @@
 # @dnssearch: #optional list of DNS suffixes to search, passed as DHCP option
 #             to the guest
 #
+# @ip6-prefix: #optional IPv6 network prefix (default is fec0::) (since 2.6)
+#
+# @ip6-prefixlen: #optional IPv6 network prefix length (default is 64) (since 2.6)
+#
+# @ip6-host: #optional guest-visible IPv6 address of the host (since 2.6)
+#
+# @ip6-dns: #optional guest-visible IPv6 address of the virtual nameserver (since 2.6)
+#
 # @smb: #optional root directory of the built-in SMB server
 #
 # @smbserver: #optional IP address of the built-in SMB server
@@ -2412,20 +2420,24 @@
 ##
 { 'struct': 'NetdevUserOptions',
   'data': {
-    '*hostname':  'str',
-    '*restrict':  'bool',
-    '*ip':        'str',
-    '*net':       'str',
-    '*host':      'str',
-    '*tftp':      'str',
-    '*bootfile':  'str',
-    '*dhcpstart': 'str',
-    '*dns':       'str',
-    '*dnssearch': ['String'],
-    '*smb':       'str',
-    '*smbserver': 'str',
-    '*hostfwd':   ['String'],
-    '*guestfwd':  ['String'] } }
+    '*hostname':        'str',
+    '*restrict':        'bool',
+    '*ip':              'str',
+    '*net':             'str',
+    '*host':            'str',
+    '*tftp':            'str',
+    '*bootfile':        'str',
+    '*dhcpstart':       'str',

qapi-schema.json line 2440

Quoted 10 lines
qapi-schema.json +10 -0
+    '*dns':             'str',
+    '*dnssearch':       ['String'],
+    '*ip6-prefix':      'str',
+    '*ip6-prefixlen':   'int',
+    '*ip6-host':        'str',
+    '*ip6-dns':         'str',
+    '*smb':             'str',
+    '*smbserver':       'str',
+    '*hostfwd':         ['String'],
+    '*guestfwd':        ['String'] } }

Please do unnecessary changes like this since it will cause extra effort for git blame and other things.

#33 lore raw
In reply to Jason Wang
Quoted 2 lines · Jason Wang, on Mon 07 Mar 2016 13:03:11 +0800, wrote:

Btw, do you want to be the maintainer of slirp? (Or maybe Tohmas want to do this?)

I already said I was fine with it, yes, and submitted a MAINTAINERS patch towards that, without any response.

Samuel

#41 lore raw
In reply to Jason Wang
Quoted 1 line · Jason Wang, on Mon 07 Mar 2016 14:48:16 +0800, wrote:

- Lots of checkpatch warnings, let's try to silent it.

They are indentation issues, yes. They are already existing in slirp/ . Whenever new code was added we sticked with the qemu style, but for patched code we prefered to stick to the existing indentation, since otherwise it'd mean reindenting it all to keep the functions readable at all. Do you what to see the whole slirp directory be reindented once for good before this gets applied? Personally I just don't care which way or the other.

Quoted 1 line

- The patches do not apply to master cleanly.

It did at the time I sent them...

Quoted 2 lines

- I expects a unit-test for this. You may want to have a look at the pxe-test in tests/, I think it could be extended to test ipv6 slirp somehow.

It doesn't seem so simple to me. In the case of PXE, you have a guest implementation of PXE inside the BIOS. In the case of IPv6, I don't think you have a guest implementation of IPv6 in the BIOS... So we'd need to embed some guest that would do the IPv6 stuff. At best we can make a qtest_start(), and that's about it.

I'm awfully tired of all of this. This work has been done 3 years ago, has already seen 9 iterations. Had it not been something important to my eyes (being able to easily test ipv6), I would have abandoned a long time ago.

At the university of Bordeaux, we are planning to add a FOSS course with students participating to existing FOSS projects. I'm afraid we will just not be able to make them work on qemu at all.

Samuel

#3 lore raw

Re: [Qemu-devel] [PATCHv9 01/10] slirp: Adding IPv6, ICMPv6 Echo and NDP autoconfiguration

In reply to Jason Wang
Quoted 11 lines · Jason Wang, on Mon 07 Mar 2016 14:53:54 +0800, wrote:

slirp/ip6_input.c line 59

Quoted 9 lines
slirp/ip6_input.c new file +9 -0
+    switch (ip6->ip_nh) {
+#if 0
+    case IPPROTO_TCP:
+        tcp_input(m, hlen, (struct socket *)NULL);
+        break;
+    case IPPROTO_UDP:
+        udp_input(m, hlen);
+        break;
+#endif

This looks odd, why need this?

To pave the way for the patches after that. We can drop that from this patch and move to the other ones if you will, I just believe it makes reading this one easier since it explains how tcp and udp will be plugged here.

Samuel

#10 lore raw

Re: [Qemu-devel] [PATCHv9 03/10] slirp: Adding IPv6 UDP support

In reply to Jason Wang
Quoted 2 lines · Jason Wang, on Mon 07 Mar 2016 14:57:11 +0800, wrote:

Part of the codes looks duplicated with ipv4 version. Any chances to merge them like tcp?

The duplication is actually quite small: accessing the ip length is different, filling the address is different, etc. So there are differences all along the code. Is it really worth the factorization? It seems to me that it would actually *reduce* lisibility.

Samuel

#17 lore raw

Re: [Qemu-devel] [PATCHv9 06/10] slirp: Reindent after refactoring

In reply to Jason Wang
Quoted 13 lines · Jason Wang, on Mon 07 Mar 2016 15:02:53 +0800, wrote:
Quoted 11 lines · Samuel Thibault

From: Guillaume Subiron <[email protected]>

No code change.

---
 slirp/tcp_input.c  | 92 +++++++++++++++++++++++++++---------------------------
 slirp/tcp_output.c | 29 +++++++++--------
 slirp/tcp_subr.c   | 50 ++++++++++++++---------------
Quoted 1 line

How about other files in slirp/ ?

What do you mean? This patch is just meant to reindent the code after 'if's and 'case's have been added without reindentation to make it more readable.

Do you mean reindenting the whole slirp? That's quite another work...

Samuel

#25 lore raw

Re: [Qemu-devel] [PATCHv9 09/10] qapi-schema, qemu-options & slirp: Adding Qemu options for IPv6 addresses

In reply to Jason Wang
Quoted 2 lines · Jason Wang, on Mon 07 Mar 2016 15:05:05 +0800, wrote:

Please do unnecessary changes like this since it will cause extra effort for git blame and other things.

Which is exactly why we avoided to fix all the checkpatch warnings by leaving existing non-qemu-style indentation.

Samuel

#26 lore raw

Re: [Qemu-devel] [PATCHv9 09/10] qapi-schema, qemu-options & slirp: Adding Qemu options for IPv6 addresses

In reply to Jason Wang
Quoted 2 lines · Jason Wang, on Mon 07 Mar 2016 15:05:05 +0800, wrote:

Please do unnecessary changes like this since it will cause extra effort for git blame and other things.

Ok. I just do hope that no other reviewer will come and say that we should fix the alignment of the parameters.

Samuel

#46 lore raw
In reply to Jason Wang
Quoted 4 lines · Jason Wang, on Mon 07 Mar 2016 14:48:16 +0800, wrote:

- the series does not build on w32:
In file included from ./slirp/slirp.h:126:0,
from stubs/slirp.c:3:
./slirp/ip6.h:9:24: fatal error: netinet/in.h: No such file or directory

Is it the only issue? Does it build if you add #ifndef _WIN32 around that include?

Samuel

#42 lore raw
In reply to Samuel Thibault
Quoted 10 lines · Samuel Thibault
Quoted 1 line · Jason Wang, on Mon 07 Mar 2016 14:48:16 +0800, wrote:

- Lots of checkpatch warnings, let's try to silent it.

They are indentation issues, yes. They are already existing in slirp/ . Whenever new code was added we sticked with the qemu style, but for patched code we prefered to stick to the existing indentation, since otherwise it'd mean reindenting it all to keep the functions readable at all. Do you what to see the whole slirp directory be reindented once for good before this gets applied? Personally I just don't care which way or the other.

IMHO we should reindent the whole slirp folder to the correct QEMU coding style at one point in time, or this topic will pop up again and again for each future patch to the slirp sources in the future. But I think it should be postponed until the IPv6 patches have been included to avoid yet another unnecessary reiteration.

Quoted 8 lines
Quoted 2 lines

- I expects a unit-test for this. You may want to have a look at the pxe-test in tests/, I think it could be extended to test ipv6 slirp somehow.

It doesn't seem so simple to me. In the case of PXE, you have a guest implementation of PXE inside the BIOS. In the case of IPv6, I don't think you have a guest implementation of IPv6 in the BIOS... So we'd need to embed some guest that would do the IPv6 stuff. At best we can make a qtest_start(), and that's about it.

Maybe we can use qemu-system-pppc64 for this ... the SLOF firmware has support for IPv6 already. I can have a look at this if you like.

Quoted 4 lines

I'm awfully tired of all of this. This work has been done 3 years ago, has already seen 9 iterations. Had it not been something important to my eyes (being able to easily test ipv6), I would have abandoned a long time ago.

I can fully understand you ... it's always the problems with parts of QEMU where there is no active maintainer available - the other parts of QEMU normally work much better. Since Jan does not have time for slirp anymore, we really urgently need a new maintainer for that code ... (but let's discuss that in the other mail thread).

Thomas

#34 lore raw
In reply to Samuel Thibault
Quoted 6 lines · Samuel Thibault
Quoted 2 lines · Jason Wang, on Mon 07 Mar 2016 13:03:11 +0800, wrote:

Btw, do you want to be the maintainer of slirp? (Or maybe Tohmas want to do this?)

I already said I was fine with it, yes, and submitted a MAINTAINERS patch towards that, without any response.

Sorry if I should have missed that, you would have my ack. Can you resend or provide a pointer?

Thanks,
Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA ITP SES-DE
Corporate Competence Center Embedded Linux
#35 lore raw
Quoted 10 lines · Jan Kiszka, on Mon 07 Mar 2016 13:06:20 +0100, wrote:
Quoted 6 lines · Samuel Thibault
Quoted 2 lines · Jason Wang, on Mon 07 Mar 2016 13:03:11 +0800, wrote:

Btw, do you want to be the maintainer of slirp? (Or maybe Tohmas want to do this?)

I already said I was fine with it, yes, and submitted a MAINTAINERS patch towards that, without any response.

Sorry if I should have missed that, you would have my ack. Can you resend or provide a pointer?

I've resent you the mail dated
Date: Thu, 6 Aug 2015 13:10:14 +0200

Samuel

#36 lore raw
In reply to Jan Kiszka
Quoted 10 lines · Jan Kiszka
Quoted 6 lines · Samuel Thibault
Quoted 2 lines · Jason Wang, on Mon 07 Mar 2016 13:03:11 +0800, wrote:

Btw, do you want to be the maintainer of slirp? (Or maybe Tohmas want to do this?)

I already said I was fine with it, yes, and submitted a MAINTAINERS patch towards that, without any response.

Sorry if I should have missed that, you would have my ack. Can you resend or provide a pointer?

Patch is available here:

https://patchwork.ozlabs.org/patch/504661/

It has even been ack'd by Stefan at that point in time. Could somebody from the maintainers please pick this patch up? Jason (via net)? Paolo (via misc)? Jan (via slirp), if you can find some spare minutes?

Thomas

#37 lore raw
Quoted 12 lines · Thomas Huth
Quoted 10 lines · Jan Kiszka
Quoted 6 lines · Samuel Thibault
Quoted 2 lines · Jason Wang, on Mon 07 Mar 2016 13:03:11 +0800, wrote:

Btw, do you want to be the maintainer of slirp? (Or maybe Tohmas want to do this?)

I already said I was fine with it, yes, and submitted a MAINTAINERS patch towards that, without any response.

Sorry if I should have missed that, you would have my ack. Can you resend or provide a pointer?

Quoted 7 lines

Patch is available here:

https://patchwork.ozlabs.org/patch/504661/

It has even been ack'd by Stefan at that point in time. Could somebody from the maintainers please pick this patch up? Jason (via net)? Paolo (via misc)? Jan (via slirp), if you can find some spare minutes?

My ack is there, so Samuel should simply send pulls to Peter directly from now on, with the first one including this patch.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA ITP SES-DE
Corporate Competence Center Embedded Linux
#7 lore raw

Re: [Qemu-devel] [PATCHv9 02/10] slirp: Adding ICMPv6 error sending

In reply to Jason Wang
Quoted 6 lines · Jason Wang, on Mon 07 Mar 2016 14:55:52 +0800, wrote:
Quoted 5 lines · Samuel Thibault

From: Yann Bordenave <[email protected]>

Disambiguation : icmp_error is renamed into icmp_send_error, since it doesn't manage errors, but only sends ICMP Error messages.

Quoted 7 lines
Quoted 4 lines

Adding icmp6_send_error to send ICMPv6 Error messages. This function is simpler than the v4 version.
Adding some calls in various functions to send ICMP errors, when a received packet is too big, or when its hop limit is 0.

Let's split this into two patches to avoid mixing renaming and new function introduction.

Ok, done so.

Samuel

#43 lore raw
In reply to Thomas Huth
Quoted 5 lines · Thomas Huth, on Mon 07 Mar 2016 13:00:48 +0100, wrote:
Quoted 4 lines · Samuel Thibault
Quoted 2 lines · Jason Wang, on Mon 07 Mar 2016 14:48:16 +0800, wrote:

- I expects a unit-test for this. You may want to have a look at the pxe-test in tests/, I think it could be extended to test ipv6 slirp somehow.

Quoted 8 lines
Quoted 5 lines

It doesn't seem so simple to me. In the case of PXE, you have a guest implementation of PXE inside the BIOS. In the case of IPv6, I don't think you have a guest implementation of IPv6 in the BIOS... So we'd need to embed some guest that would do the IPv6 stuff. At best we can make a qtest_start(), and that's about it.

Maybe we can use qemu-system-pppc64 for this ... the SLOF firmware has support for IPv6 already.

Ah, nice :)

Quoted 1 line

I can have a look at this if you like.

Yes.

I'm wondering whether that will be in time for the 2.6 window, however. It seems more important to me to get the code more widely tested than adding the unit-test.

Samuel

#47 lore raw
In reply to Samuel Thibault
Quoted 8 lines · Samuel Thibault
Quoted 4 lines · Jason Wang, on Mon 07 Mar 2016 14:48:16 +0800, wrote:

- the series does not build on w32:
In file included from ./slirp/slirp.h:126:0,
from stubs/slirp.c:3:
./slirp/ip6.h:9:24: fatal error: netinet/in.h: No such file or directory

Is it the only issue? Does it build if you add #ifndef _WIN32 around that include?

Is that "#include <netinet/in.h>" required in ip6.h at all? It also seems to compile if I omit that include completely here...

Anyway, I just tried with my mingw32 cross-compiler, and once this is fixed, compilation succeeds, but there are some warnings:

net/slirp.c: In function 'net_slirp_init':
net/slirp.c:249:5: warning: implicit declaration of function 'inet_pton' [-Wimplicit-function-declaration]

     if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
     ^
slirp/ip6_icmp.c: In function 'icmp6_send_error':
slirp/ip6_icmp.c:84:5: warning: implicit declaration of function 'inet_ntop' [-Wimplicit-function-declaration]
     inet_ntop(AF_INET6, &rip->ip_dst, addrstr, INET6_ADDRSTRLEN);
     ^
qemu/slirp/ndp_table.c: In function 'ndp_table_add':
qemu/slirp/ndp_table.c:16:5: warning: implicit declaration of function 'inet_ntop' [-Wimplicit-function-declaration]
     inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);

The warnings go away when I use this piece of code in the beginning of the affected files:

#ifdef _WIN32
#define _WIN32_WINNT _WIN32_WINNT_WIN6
#include <ws2tcpip.h>
#endif

Thomas

#27 lore raw

Re: [Qemu-devel] [PATCHv9 09/10] qapi-schema, qemu-options & slirp: Adding Qemu options for IPv6 addresses

In reply to Samuel Thibault
Quoted 6 lines · Samuel Thibault
Quoted 2 lines · Jason Wang, on Mon 07 Mar 2016 15:05:05 +0800, wrote:

Please do unnecessary changes like this since it will cause extra effort for git blame and other things.

Ok. I just do hope that no other reviewer will come and say that we should fix the alignment of the parameters.

Alignment doesn't matter; in fact, most qapi .json files use
'name': 'type

with a single space after : for every parameter, rather than trying to align things. It doesn't bother me either way (or I would have pointed it out sooner), but reindenting is more churn than I would personally do.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#48 lore raw
In reply to Thomas Huth

Hello,

Quoted 2 lines · Thomas Huth, on Mon 07 Mar 2016 16:37:59 +0100, wrote:

Is that "#include <netinet/in.h>" required in ip6.h at all? It also seems to compile if I omit that include completely here...

Well, it makes sense to put it in ip6.h. That said, since ip6.h is only included through slirp.h which includes netinet/in.h appropriately, we can indeed drop it from ip6.h

Quoted 15 lines

Anyway, I just tried with my mingw32 cross-compiler, and once this is fixed, compilation succeeds, but there are some warnings:

net/slirp.c: In function 'net_slirp_init':
net/slirp.c:249:5: warning: implicit declaration of function 'inet_pton' [-Wimplicit-function-declaration]

     if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
     ^
slirp/ip6_icmp.c: In function 'icmp6_send_error':
slirp/ip6_icmp.c:84:5: warning: implicit declaration of function 'inet_ntop' [-Wimplicit-function-declaration]
     inet_ntop(AF_INET6, &rip->ip_dst, addrstr, INET6_ADDRSTRLEN);
     ^
qemu/slirp/ndp_table.c: In function 'ndp_table_add':
qemu/slirp/ndp_table.c:16:5: warning: implicit declaration of function 'inet_ntop' [-Wimplicit-function-declaration]
     inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);
Quoted 6 lines

The warnings go away when I use this piece of code in the beginning of the affected files:

#ifdef _WIN32
#define _WIN32_WINNT _WIN32_WINNT_WIN6
#include <ws2tcpip.h>
#endif

Eeeww, so windows only provides inet_pton since Vista?! Ok, for now I'll just disable using inet_ntop/pton when built with compatibility before vista, i.e. notably

#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
hardcode fec0::
#endif

and refuse to configure the prefix, host and dns.

Samuel

#39 lore raw
In reply to Samuel Thibault
Quoted 8 lines · Samuel Thibault
Quoted 2 lines · Jason Wang, on Mon 07 Mar 2016 13:03:11 +0800, wrote:

Btw, do you want to be the maintainer of slirp? (Or maybe Tohmas want to do this?)

I already said I was fine with it, yes, and submitted a MAINTAINERS patch towards that, without any response.

Samuel

For some reason, I miss this. Will pick the patch.

Thanks

#4 lore raw

Re: [Qemu-devel] [PATCHv9 01/10] slirp: Adding IPv6, ICMPv6 Echo and NDP autoconfiguration

In reply to Samuel Thibault
Quoted 11 lines · Samuel Thibault
Quoted 10 lines · Jason Wang, on Mon 07 Mar 2016 14:53:54 +0800, wrote:

slirp/ip6_input.c line 59

Quoted 9 lines
slirp/ip6_input.c new file +9 -0
+    switch (ip6->ip_nh) {
+#if 0
+    case IPPROTO_TCP:
+        tcp_input(m, hlen, (struct socket *)NULL);
+        break;
+    case IPPROTO_UDP:
+        udp_input(m, hlen);
+        break;
+#endif

This looks odd, why need this?

Quoted 6 lines

To pave the way for the patches after that. We can drop that from this patch and move to the other ones if you will, I just believe it makes reading this one easier since it explains how tcp and udp will be plugged here.

Samuel

This lead warnings in checkpatch, maybe it's better to use comment instead?

#11 lore raw

Re: [Qemu-devel] [PATCHv9 03/10] slirp: Adding IPv6 UDP support

In reply to Samuel Thibault
Quoted 10 lines · Samuel Thibault
Quoted 2 lines · Jason Wang, on Mon 07 Mar 2016 14:57:11 +0800, wrote:

Part of the codes looks duplicated with ipv4 version. Any chances to merge them like tcp?

The duplication is actually quite small: accessing the ip length is different, filling the address is different, etc. So there are differences all along the code. Is it really worth the factorization? It seems to me that it would actually *reduce* lisibility.

Samuel

I see, so it's ok to leave this patch as is.

#18 lore raw

Re: [Qemu-devel] [PATCHv9 06/10] slirp: Reindent after refactoring

In reply to Samuel Thibault
Quoted 13 lines · Samuel Thibault
Quoted 12 lines · Jason Wang, on Mon 07 Mar 2016 15:02:53 +0800, wrote:
Quoted 11 lines · Samuel Thibault

From: Guillaume Subiron <[email protected]>

No code change.

---
 slirp/tcp_input.c  | 92 +++++++++++++++++++++++++++---------------------------
 slirp/tcp_output.c | 29 +++++++++--------
 slirp/tcp_subr.c   | 50 ++++++++++++++---------------
Quoted 9 lines
Quoted 1 line

How about other files in slirp/ ?

What do you mean? This patch is just meant to reindent the code after 'if's and 'case's have been added without reindentation to make it more readable.

Do you mean reindenting the whole slirp? That's quite another work...

Samuel

I'm asking since I'm not sure partial reindenting is a the best choice for us. The patch itself is ok.

#49 lore raw
In reply to Samuel Thibault
Quoted 7 lines · Samuel Thibault
Quoted 4 lines · Jason Wang, on Mon 07 Mar 2016 14:48:16 +0800, wrote:

- the series does not build on w32:
In file included from ./slirp/slirp.h:126:0,
from stubs/slirp.c:3:
./slirp/ip6.h:9:24: fatal error: netinet/in.h: No such file or directory

Is it the only issue? Does it build if you add #ifndef _WIN32 around that include?

Build works but get some warnings:

slirp/ip6_icmp.c: In function 'icmp6_send_error':
slirp/ip6_icmp.c:84:5: warning: implicit declaration of function 'inet_ntop' [-Wimplicit-function-declaration]

     inet_ntop(AF_INET6, &rip->ip_dst, addrstr, INET6_ADDRSTRLEN);
     ^

slirp/ip6_icmp.c:84:5: warning: nested extern declaration of 'inet_ntop' [-Wnested-externs]

and

slirp/slirp.c: In function 'slirp_init':
slirp/slirp.c:226:5: warning: implicit declaration of function 'inet_pton' [-Wimplicit-function-declaration]

     inet_pton(AF_INET6, "fec0::0", &slirp->vprefix_addr6);
     ^

slirp/slirp.c:226:5: warning: nested extern declaration of 'inet_pton' [-Wnested-externs]

(btw, you may refer http://wiki.qemu.org/Hosts/W32 to test this yourself).

Quoted 3 lines

Samuel

#44 lore raw
In reply to Samuel Thibault
Quoted 9 lines · Samuel Thibault
Quoted 1 line · Jason Wang, on Mon 07 Mar 2016 14:48:16 +0800, wrote:

- Lots of checkpatch warnings, let's try to silent it.

They are indentation issues, yes. They are already existing in slirp/ . Whenever new code was added we sticked with the qemu style, but for patched code we prefered to stick to the existing indentation, since otherwise it'd mean reindenting it all to keep the functions readable at all. Do you what to see the whole slirp directory be reindented once for good before this gets applied? Personally I just don't care which way or the other.

Not only for indentation issue, e.g:

./scripts/checkpatch.pl 0001-slirp-Adding-IPv6-ICMPv6-Echo-and-NDP-autoconfigurat.patch
ERROR: return is not a function, parentheses are not required #177: FILE: slirp/ip6.h:65:
+ return (a->s6_addr[prefix_len / 8] & ((1U << (8 - (prefix_len %
8))) - 1))

WARNING: line over 80 characters
#270: FILE: slirp/ip6_icmp.c:14:
+#define NDP_Interval g_rand_int_range(slirp->grand, NDP_MinRtrAdvInterval, NDP_MaxRtrAdvInterval)

WARNING: if this code is redundant consider removing it #870: FILE: slirp/ip6_input.c:52:
+#if 0

total: 1 errors, 2 warnings, 1121 lines checked

0001-slirp-Adding-IPv6-ICMPv6-Echo-and-NDP-autoconfigurat.patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Quoted 3 lines
Quoted 2 lines

- The patches do not apply to master cleanly.

It did at the time I sent them...

Right, but not now, so please rebase.

Quoted 8 lines
Quoted 3 lines

- I expects a unit-test for this. You may want to have a look at the pxe-test in tests/, I think it could be extended to test ipv6 slirp somehow.

It doesn't seem so simple to me. In the case of PXE, you have a guest implementation of PXE inside the BIOS. In the case of IPv6, I don't think you have a guest implementation of IPv6 in the BIOS... So we'd need to embed some guest that would do the IPv6 stuff. At best we can make a qtest_start(), and that's about it.

Haven't think about this deeply, but at least from the source code, ipxe did support ipv6. But I agree this could be done in the future.

Quoted 5 lines

I'm awfully tired of all of this. This work has been done 3 years ago, has already seen 9 iterations. Had it not been something important to my eyes (being able to easily test ipv6), I would have abandoned a long time ago.

Sorry for this, I know how tired of this, and thanks a lot for not giving up. I think the patch is pretty ready to be merged after one more iteration.

Quoted 6 lines

At the university of Bordeaux, we are planning to add a FOSS course with students participating to existing FOSS projects. I'm afraid we will just not be able to make them work on qemu at all.

Samuel

I see, we still have time before hard freeze. So let's try to make it for 2.6.

#38 lore raw
In reply to Jan Kiszka
Quoted 14 lines · Jan Kiszka
Quoted 13 lines · Thomas Huth
Quoted 8 lines · Jan Kiszka
Quoted 5 lines · Samuel Thibault
Quoted 2 lines · Jason Wang, on Mon 07 Mar 2016 13:03:11 +0800, wrote:

Btw, do you want to be the maintainer of slirp? (Or maybe Tohmas want to do this?)

I already said I was fine with it, yes, and submitted a MAINTAINERS patch towards that, without any response.

Sorry if I should have missed that, you would have my ack. Can you resend or provide a pointer?

Patch is available here:

https://patchwork.ozlabs.org/patch/504661/

Quoted 8 lines
Quoted 3 lines

It has even been ack'd by Stefan at that point in time. Could somebody from the maintainers please pick this patch up? Jason (via net)? Paolo (via misc)? Jan (via slirp), if you can find some spare minutes?

My ack is there, so Samuel should simply send pulls to Peter directly from now on, with the first one including this patch.

Jan

Plan to pick the patch, but this looks also fine with me.

#45 lore raw
In reply to Jason Wang
Quoted 6 lines · Jason Wang, on Tue 08 Mar 2016 10:48:27 +0800, wrote:

./scripts/checkpatch.pl 0001-slirp-Adding-IPv6-ICMPv6-Echo-and-NDP-autoconfigurat.patch
ERROR: return is not a function, parentheses are not required #177: FILE: slirp/ip6.h:65:
+ return (a->s6_addr[prefix_len / 8] & ((1U << (8 - (prefix_len %
8))) - 1))

This is a false positive, the parentheses here are part of the expression.

Quoted 4 lines

WARNING: line over 80 characters
#270: FILE: slirp/ip6_icmp.c:14:
+#define NDP_Interval g_rand_int_range(slirp->grand, NDP_MinRtrAdvInterval, NDP_MaxRtrAdvInterval)

That was a real one yes, introduced when I was asked to use g_rand_int_range, I have fixed it already.

Quoted 3 lines

WARNING: if this code is redundant consider removing it #870: FILE: slirp/ip6_input.c:52:
+#if 0

That's what we discuss in the other mail. Yes, we can use comments. It just looks odd that we are talking about some code which goes away in a subsequent patch :)

Quoted 4 lines
Quoted 2 lines
Quoted 1 line

- The patches do not apply to master cleanly.

It did at the time I sent them...

Right, but not now, so please rebase.

Sure... Already done so.

Samuel

#50 lore raw
In reply to Jason Wang
Quoted 13 lines · Jason Wang, on Tue 08 Mar 2016 10:15:57 +0800, wrote:

Build works but get some warnings:

slirp/ip6_icmp.c: In function 'icmp6_send_error':
slirp/ip6_icmp.c:84:5: warning: implicit declaration of function 'inet_ntop' [-Wimplicit-function-declaration]

     inet_ntop(AF_INET6, &rip->ip_dst, addrstr, INET6_ADDRSTRLEN);
     ^

slirp/ip6_icmp.c:84:5: warning: nested extern declaration of 'inet_ntop' [-Wnested-externs]

and

Quoted 7 lines

slirp/slirp.c: In function 'slirp_init':
slirp/slirp.c:226:5: warning: implicit declaration of function 'inet_pton' [-Wimplicit-function-declaration]

     inet_pton(AF_INET6, "fec0::0", &slirp->vprefix_addr6);
     ^

slirp/slirp.c:226:5: warning: nested extern declaration of 'inet_pton' [-Wnested-externs]

Thomas reported them in another mail, yes. I have changed that into not using inet_pton/ntop when built with windows compatibility before Vista.

Quoted 1 line

(btw, you may refer http://wiki.qemu.org/Hosts/W32 to test this yourself).

I've tried it yesterday, yes, but now qemu needs glib & such, so it'd need cross-building it...

Samuel