Skip to contentSkip to conversation

Route-Gateway redirection for IPv6 - finally

Openvpn-devel In review Gert Doering 2 participants 39 messages Sep 11, 2015 – Oct 6, 2015

lore
Share & tools
lore
#0 lore raw

Hi,

this is the block of code that is missing for 2.4 on the IPv6/routing side (correctly handle IPv6 routes that overlap with the IPv6 address of the VPN server) - if it's in, I'm ready to go...

01+02 are refactoring without functional code changes, to make the IPv6 data structures more similar to the IPv4 stuff (James changed all the IPv4 just after the IPv6 payload stuff was merged...).

03+04+05+06 are the grunt of the change set - basic infrastructure for detecting the IPv6 default gateway, implementation for Linux, actual handling of overlapping IPv6 routes + VPN server address and gateway redirection ("insert /128 host route").

07 is a trivial bonus patch to implement "--redirect-gateway ipv6"

08 is the implementation for all BSDs, MacOS and OpenSolaris 09 makes the /128 routes actually work on OpenSolaris

10 is the implementation for Windows (Vista and up).
THIS BREAKS WINDOWS XP COMPATIBILITY (and might need some additional tending depending on MinGW versions).

The patch set has fairly thoroughly tested on all(!) our supported OSes, but there are so many possible variants of IPv6 routing setups that there will still be combinations that break - we'll have to test, and fix them as we go.

Arne: I'm fairly sure some "#ifndef TARGET_ANDROID" clauses will be needed - that stuff isn't needed on Android, and might get in the way, or at least confuse users. Please feel free to send patch #11 :-)

#1 lore raw

PATCH 1/10 refactor struct route_ipv6, bring in line with struct route_ipv4 again

adjust "struct route_ipv6" (and all users) to reflect changes to "struct route_ipv4" done in commit 7fb0e07e, namely:

- new member "r6->flags"
- "r6->defined" becomes "r6->flags & RT_DEFINED"
- "r6->metric_defined" becomes "r6->flags & RT_METRIC_DEFINED"
- route addition status is stored in "r6->flags & RT_ADDED"

---
 src/openvpn/route.c | 35 ++++++++++++++++++-----------------
 src/openvpn/route.h |  3 +--
 src/openvpn/tun.c   |  6 ++----
 3 files changed, 21 insertions(+), 23 deletions(-)
src/openvpn/route.c +18 -17
index ee7c0de..0f91652 100644
@@ -374,7 +374,7 @@ init_route_ipv6 (struct route_ipv6 *r6,
 	         const struct route_ipv6_option *r6o,
 	         const struct route_ipv6_list *rl6 )
 {
-  r6->defined = false;
+  CLEAR (*r6);
 
   if ( !get_ipv6_addr( r6o->prefix, &r6->network, &r6->netbits, NULL, M_WARN ))
     goto fail;
@@ -399,7 +399,6 @@ init_route_ipv6 (struct route_ipv6 *r6,
 
   /* metric */
 
-  r6->metric_defined = false;
   r6->metric = -1;
   if (is_route_parm_defined (r6o->metric))
     {
@@ -411,22 +410,21 @@ init_route_ipv6 (struct route_ipv6 *r6,
 	       r6o->metric);
 	  goto fail;
 	}
-      r6->metric_defined = true;
+      r6->flags |= RT_METRIC_DEFINED;
     }
   else if (rl6->default_metric_defined)
     {
       r6->metric = rl6->default_metric;
-      r6->metric_defined = true;
+      r6->flags |= RT_METRIC_DEFINED;
     }
 
-  r6->defined = true;
+  r6->flags |= RT_DEFINED;
 
   return true;
 
  fail:
   msg (M_WARN, PACKAGE_NAME " ROUTE: failed to parse/resolve route for host/network: %s",
        r6o->prefix);
-  r6->defined = false;
   return false;
 }
 
@@ -1168,7 +1166,7 @@ static void
 setenv_route_ipv6 (struct env_set *es, const struct route_ipv6 *r6, int i)
 {
   struct gc_arena gc = gc_new ();
-  if (r6->defined)
+  if (r6->flags & RT_DEFINED)
     {
       struct buffer name1 = alloc_buf_gc( 256, &gc );
       struct buffer val = alloc_buf_gc( 256, &gc );
@@ -1545,7 +1543,7 @@ add_route_ipv6 (struct route_ipv6 *r6, const struct tuntap *tt, unsigned int fla
 
   bool gateway_needed = false;
 
-  if (!r6->defined)
+  if (! (r6->flags & RT_DEFINED) )
     return;
 
   gc_init (&gc);
@@ -1576,7 +1574,7 @@ add_route_ipv6 (struct route_ipv6 *r6, const struct tuntap *tt, unsigned int fla
    * gateway unless the route is to be an on-link network
    */
   if ( tt->type == DEV_TYPE_TAP &&
-                  !(r6->metric_defined && r6->metric == 0 ) )
+                  !( (r6->flags & RT_METRIC_DEFINED) && r6->metric == 0 ) )
     {
       gateway_needed = true;
     }
@@ -1590,7 +1588,7 @@ add_route_ipv6 (struct route_ipv6 *r6, const struct tuntap *tt, unsigned int fla
 	      device);
   if (gateway_needed)
     argv_printf_cat (&argv, "via %s", gateway);
-  if (r6->metric_defined && r6->metric > 0 )
+  if ( (r6->flags & RT_METRIC_DEFINED) && r6->metric > 0 )
     argv_printf_cat (&argv, " metric %d", r6->metric);
 
 #else
@@ -1601,7 +1599,7 @@ add_route_ipv6 (struct route_ipv6 *r6, const struct tuntap *tt, unsigned int fla
 	      device);
   if (gateway_needed)
     argv_printf_cat (&argv, "gw %s", gateway);
-  if (r6->metric_defined && r6->metric > 0 )
+  if ( (r6->flags & RT_METRIC_DEFINED) && r6->metric > 0 )
     argv_printf_cat (&argv, " metric %d", r6->metric);
 #endif  /*ENABLE_IPROUTE*/
   argv_msg (D_ROUTE, &argv);
@@ -1635,7 +1633,7 @@ add_route_ipv6 (struct route_ipv6 *r6, const struct tuntap *tt, unsigned int fla
 	argv_printf_cat( &argv, " %s", gateway );
 
 #if 0
-  if (r->metric_defined)
+  if (r6->flags & RT_METRIC_DEFINED)
     argv_printf_cat (&argv, " METRIC %d", r->metric);
 #endif
 
@@ -1727,7 +1725,10 @@ add_route_ipv6 (struct route_ipv6 *r6, const struct tuntap *tt, unsigned int fla
   msg (M_FATAL, "Sorry, but I don't know how to do 'route ipv6' commands on this operating system.  Try putting your routes in a --route-up script");
 #endif
 
-  r6->defined = status;
+  if (status)
+    r6->flags |= RT_ADDED;
+  else
+    r6->flags &= ~RT_ADDED;
   argv_reset (&argv);
   gc_free (&gc);
 }
@@ -1916,7 +1917,7 @@ delete_route_ipv6 (const struct route_ipv6 *r6, const struct tuntap *tt, unsigne
   const char *device = tt->actual_name;
   bool gateway_needed = false;
 
-  if (!r6->defined)
+  if ((r6->flags & (RT_DEFINED|RT_ADDED)) != (RT_DEFINED|RT_ADDED))
     return;
 
   gc_init (&gc);
@@ -1938,7 +1939,7 @@ delete_route_ipv6 (const struct route_ipv6 *r6, const struct tuntap *tt, unsigne
    * delete, otherwise some OSes will refuse to delete the route
    */
   if ( tt->type == DEV_TYPE_TAP &&
-                  !(r6->metric_defined && r6->metric == 0 ) )
+                  !( (r6->flags & RT_METRIC_DEFINED) && r6->metric == 0 ) )
     {
       gateway_needed = true;
     }
@@ -1961,7 +1962,7 @@ delete_route_ipv6 (const struct route_ipv6 *r6, const struct tuntap *tt, unsigne
 	      device);
   if (gateway_needed)
     argv_printf_cat (&argv, "gw %s", gateway);
-  if (r6->metric_defined && r6->metric > 0 )
+  if ( (r6->flags & RT_METRIC_DEFINED) && r6->metric > 0 )
     argv_printf_cat (&argv, " metric %d", r6->metric);
 #endif  /*ENABLE_IPROUTE*/
   argv_msg (D_ROUTE, &argv);
@@ -1989,7 +1990,7 @@ delete_route_ipv6 (const struct route_ipv6 *r6, const struct tuntap *tt, unsigne
 	argv_printf_cat( &argv, " %s", gateway );
 
 #if 0
-  if (r->metric_defined)
+  if (r6->flags & RT_METRIC_DEFINED)
     argv_printf_cat (&argv, "METRIC %d", r->metric);
 #endif
 
src/openvpn/route.h +1 -2
index 3cec08d..13882a4 100644
@@ -123,11 +123,10 @@ struct route_ipv4 {
 
 struct route_ipv6 {
   struct route_ipv6 *next;
-  bool defined;
+  unsigned int flags;				/* RT_ flags, see route_ipv4 */
   struct in6_addr network;
   unsigned int netbits;
   struct in6_addr gateway;
-  bool metric_defined;
   int metric;
 };
 
src/openvpn/tun.c +2 -4
index 13e3826..766a73c 100644
@@ -607,12 +607,11 @@ void add_route_connected_v6_net(struct tuntap * tt,
 {
     struct route_ipv6 r6;
 
-    r6.defined = true;
     r6.network = tt->local_ipv6;
     r6.netbits = tt->netbits_ipv6;
     r6.gateway = tt->local_ipv6;
     r6.metric  = 0;			/* connected route */
-    r6.metric_defined = true;
+    r6.flags   = RT_DEFINED | RT_METRIC_DEFINED;
     add_route_ipv6 (&r6, tt, 0, es);
 }
 
@@ -621,12 +620,11 @@ void delete_route_connected_v6_net(struct tuntap * tt,
 {
     struct route_ipv6 r6;
 
-    r6.defined = true;
     r6.network = tt->local_ipv6;
     r6.netbits = tt->netbits_ipv6;
     r6.gateway = tt->local_ipv6;
     r6.metric  = 0;			/* connected route */
-    r6.metric_defined = true;
+    r6.flags   = RT_DEFINED | RT_ADDED | RT_METRIC_DEFINED;
     delete_route_ipv6 (&r6, tt, 0, es);
 }
 #endif
Signature · 1 line
2.3.6
#4 lore raw

PATCH 2/10 refactor struct route_ipv6_list, bring in line with struct route_list again

In reply to Gert Doering

adjust "struct route_ipv6_list" (and all users) to reflect changes to "struct route_list" done in commit 7fb0e07e, namely:

 - new member "rl6->iflags"            (RL_* flags)
 - new member "rl6->spec_flags"        (RTSA_* flags)
 - new member "rl6->remote_host_ipv6"  (--remote address we're talking to)
 - "rl6->routes_added"
              --> "rl6->iflags & RL_ROUTES_ADDED"
 - "rl6->did_redirect_default_gateway"
              --> "rl6->iflags & RL_DID_REDIRECT_DEFAULT_GATEWAY"
 - "rl6->did_local"
              --> "rl6->iflags & RL_DID_LOCAL"
 - "rl6->remote_endpoint_defined"
              --> "rl6->spec_flags & RTSA_REMOTE_ENDPOINT"
 - "rl6->default_metric_defined"
              --> "rl6->spec_flags & RTSA_DEFAULT_METRIC"

deviating from IPv4 route_list, there is no "route_special_addr spec" sub-struct, because it's not considered useful (rl->spec.flags becomes rl6->spec_flags, the IPv6 equivalent of everything else in rl->spec just lives inside struct route_ipv6_list)

---
 src/openvpn/route.c | 19 ++++++++-----------
 src/openvpn/route.h | 16 ++++++++--------
 2 files changed, 16 insertions(+), 19 deletions(-)
src/openvpn/route.c +8 -11
index 0f91652..6b2af3c 100644
@@ -387,7 +387,7 @@ init_route_ipv6 (struct route_ipv6 *r6,
 	  msg( M_WARN, PACKAGE_NAME "ROUTE6: cannot parse gateway spec '%s'", r6o->gateway );
         }
     }
-  else if (rl6->remote_endpoint_defined)
+  else if (rl6->spec_flags & RTSA_REMOTE_ENDPOINT)
     {
       r6->gateway = rl6->remote_endpoint_ipv6;
     }
@@ -412,7 +412,7 @@ init_route_ipv6 (struct route_ipv6 *r6,
 	}
       r6->flags |= RT_METRIC_DEFINED;
     }
-  else if (rl6->default_metric_defined)
+  else if (rl6->spec_flags & RTSA_DEFAULT_METRIC)
     {
       r6->metric = rl6->default_metric;
       r6->flags |= RT_METRIC_DEFINED;
@@ -671,7 +671,7 @@ init_route_ipv6_list (struct route_ipv6_list *rl6,
   if (default_metric >= 0 )
     {
       rl6->default_metric = default_metric;
-      rl6->default_metric_defined = true;
+      rl6->spec_flags |= RTSA_DEFAULT_METRIC;
     }
 
   /* "default_gateway" is stuff for "redirect-gateway", which we don't
@@ -686,7 +686,7 @@ init_route_ipv6_list (struct route_ipv6_list *rl6,
       if ( inet_pton( AF_INET6, remote_endpoint, 
 			&rl6->remote_endpoint_ipv6) == 1 )
         {
-	  rl6->remote_endpoint_defined = true;
+	  rl6->spec_flags |= RTSA_REMOTE_ENDPOINT;
         }
       else
 	{
@@ -694,9 +694,6 @@ init_route_ipv6_list (struct route_ipv6_list *rl6,
           ret = false;
 	}
     }
-  else
-    rl6->remote_endpoint_defined = false;
-
 
   /* parse the routes from opt6 to rl6 */
   {
@@ -1003,7 +1000,7 @@ add_routes (struct route_list *rl, struct route_ipv6_list *rl6, const struct tun
 	}
       rl->iflags |= RL_ROUTES_ADDED;
     }
-  if (rl6 && !rl6->routes_added)
+  if (rl6 && !(rl6->iflags & RL_ROUTES_ADDED) )
     {
       struct route_ipv6 *r;
       for (r = rl6->routes_ipv6; r; r = r->next)
@@ -1012,7 +1009,7 @@ add_routes (struct route_list *rl, struct route_ipv6_list *rl6, const struct tun
 	    delete_route_ipv6 (r, tt, flags, es);
 	  add_route_ipv6 (r, tt, flags, es);
 	}
-      rl6->routes_added = true;
+      rl6->iflags |= RL_ROUTES_ADDED;
     }
 }
 
@@ -1037,14 +1034,14 @@ delete_routes (struct route_list *rl, struct route_ipv6_list *rl6,
       clear_route_list (rl);
     }
 
-  if ( rl6 && rl6->routes_added )
+  if ( rl6 && (rl6->iflags & RL_ROUTES_ADDED) )
     {
       struct route_ipv6 *r6;
       for (r6 = rl6->routes_ipv6; r6; r6 = r6->next)
 	{
 	  delete_route_ipv6 (r6, tt, flags, es);
 	}
-      rl6->routes_added = false;
+      rl6->iflags &= ~RL_ROUTES_ADDED;
     }
 
   if ( rl6 )
src/openvpn/route.h +8 -8
index 13882a4..7e96a2f 100644
@@ -103,7 +103,7 @@ struct route_ipv6_option {
 };
 
 struct route_ipv6_option_list {
-  unsigned int flags;
+  unsigned int flags;		/* RG_x flags, see route_option-list */
   struct route_ipv6_option *routes_ipv6;
   struct gc_arena *gc;
 };
@@ -131,14 +131,14 @@ struct route_ipv6 {
 };
 
 struct route_ipv6_list {
-  bool routes_added;
-  unsigned int flags;
+  unsigned int iflags;			/* RL_ flags, see route_list */
+
+  unsigned int spec_flags;		/* RTSA_ flags, route_special_addr */
+  struct in6_addr remote_endpoint_ipv6;	/* inside tun */
+  struct in6_addr remote_host_ipv6;	/* --remote address */
   int default_metric;
-  bool default_metric_defined;
-  struct in6_addr remote_endpoint_ipv6;
-  bool remote_endpoint_defined;
-  bool did_redirect_default_gateway;			/* TODO (?) */
-  bool did_local;					/* TODO (?) */
+
+  unsigned int flags;			/* RG_x flags, see route_option_list */
   struct route_ipv6 *routes_ipv6;
   struct gc_arena gc;
 };
Signature · 1 line
2.3.6
#7 lore raw

PATCH 3/10 Add route_ipv6_gateway* data structures for rgi6 support.

In reply to Gert Doering

route_gateway_address -> route_ipv6_gateway_address
route_gateway_info -> route_ipv6_gateway_info

---
 src/openvpn/route.h | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
src/openvpn/route.h +28 -0
index 7e96a2f..5ab5f98 100644
@@ -177,6 +177,34 @@ struct route_gateway_info {
   struct route_gateway_address addrs[RGI_N_ADDRESSES]; /* local addresses attached to iface */
 };
 
+struct route_ipv6_gateway_address {
+  struct in6_addr addr_ipv6;
+  int netbits_ipv6;
+};
+
+struct route_ipv6_gateway_info {
+/* RGI_ flags used as in route_gateway_info */
+  unsigned int flags;
+
+  /* gateway interface */
+# ifdef WIN32
+  DWORD adapter_index;  /* interface or ~0 if undefined */
+#else
+  char iface[16]; /* interface name (null terminated), may be empty */
+#endif
+
+  /* gateway interface hardware address */
+  uint8_t hwaddr[6];
+
+  /* gateway/router address */
+  struct route_ipv6_gateway_address gateway;
+
+  /* address/netmask pairs bound to interface */
+# define RGI_N_ADDRESSES 8
+  int n_addrs; /* len of addrs, may be 0 */
+  struct route_ipv6_gateway_address addrs[RGI_N_ADDRESSES]; /* local addresses attached to iface */
+};
+
 struct route_list {
 # define RL_DID_REDIRECT_DEFAULT_GATEWAY (1<<0)
 # define RL_DID_LOCAL                    (1<<1)
Signature · 1 line
2.3.6
#10 lore raw

PATCH 4/10 Create basic infrastructure for IPv6 default gateway handling / redirection.

In reply to Gert Doering

- introduce get_default_gateway_ipv6() and add stub functions with the implementation plan to the 4 major code blocks here (Windows, Linux/Android, *BSD and Solaris, "others")

- add &rgi6 to print_default_gateway(), and teach it to print v4, v6 or both, depending on the calling environment

- unlike IPv4 (today), get_default_gateway_ipv6() is passed the actual target IPv6 address of the server we're looking for, so we can handle more complicated routing setups ("default to eth0, vpn server to ppp0") correctly

- consequently, --get-default-gateway has an optional parameter now, the IPv6 address to look up (for debugging)

---
 src/openvpn/init.c    |  4 ++-
 src/openvpn/options.c |  9 ++++--
 src/openvpn/route.c   | 76 +++++++++++++++++++++++++++++++++++++++++++++++++--
 src/openvpn/route.h   |  6 +++-
 4 files changed, 88 insertions(+), 7 deletions(-)
src/openvpn/init.c +3 -1
index fe00918..4c17905 100644
@@ -639,8 +639,10 @@ init_static (void)
 #ifdef TEST_GET_DEFAULT_GATEWAY
   {
     struct route_gateway_info rgi;
+    struct route_ipv6_gateway_info rgi6;
     get_default_gateway(&rgi);
-    print_default_gateway(M_INFO, &rgi);
+    get_default_gateway_ipv6(&rgi6, NULL);
+    print_default_gateway(M_INFO, &rgi, &rgi6);
     return false;
   }
 #endif
src/openvpn/options.c +7 -2
index caf1394..93ea415 100644
@@ -4156,12 +4156,17 @@ add_option (struct options *options,
       read_config_file (options, p[1], level, file, line, msglevel, permission_mask, option_types_found, es);
     }
 #if defined(ENABLE_DEBUG) && !defined(ENABLE_SMALL)
-  else if (streq (p[0], "show-gateway") && !p[1])
+  else if (streq (p[0], "show-gateway") && !p[2])
     {
       struct route_gateway_info rgi;
+      struct route_ipv6_gateway_info rgi6;
+      struct in6_addr remote = IN6ADDR_ANY_INIT;
       VERIFY_PERMISSION (OPT_P_GENERAL);
+      if (p[1])
+	  get_ipv6_addr (p[1], &remote, NULL, NULL, M_WARN);
       get_default_gateway(&rgi);
-      print_default_gateway(M_INFO, &rgi);
+      get_default_gateway_ipv6(&rgi6, &remote);
+      print_default_gateway(M_INFO, &rgi, &rgi6);
       openvpn_exit (OPENVPN_EXIT_STATUS_GOOD); /* exit point */
     }
 #endif
src/openvpn/route.c +73 -3

120 lines not on this page · raw message

src/openvpn/route.h +5 -1
index 5ab5f98..95cf99f 100644
@@ -300,7 +300,11 @@ void setenv_routes_ipv6 (struct env_set *es, const struct route_ipv6_list *rl6);
 bool is_special_addr (const char *addr_str);
 
 void get_default_gateway (struct route_gateway_info *rgi);
-void print_default_gateway(const int msglevel, const struct route_gateway_info *rgi);
+void get_default_gateway_ipv6 (struct route_ipv6_gateway_info *rgi,
+				struct in6_addr *dest);
+void print_default_gateway(const int msglevel,
+                           const struct route_gateway_info *rgi,
+                           const struct route_ipv6_gateway_info *rgi6);
 
 /*
  * Test if addr is reachable via a local interface (return ILA_LOCAL),
Signature · 1 line
2.3.6
#14 lore raw

PATCH 5/10 get_default_gateway_ipv6(): Linux / Netlink implementation.

In reply to Gert Doering
---
 src/openvpn/route.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 135 insertions(+), 12 deletions(-)
src/openvpn/route.c +135 -12
index fd8209d..cca5cfe 100644
@@ -45,6 +45,10 @@
 
 #include "memdbg.h"
 
+#if defined(TARGET_LINUX) || defined(TARGET_ANDROID)
+#include <linux/rtnetlink.h>		/* RTM_GETROUTE etc. */
+#endif
+
 #ifdef WIN32
 #define METRIC_NOT_USED ((DWORD)-1)
 #endif
@@ -1115,10 +1119,9 @@ print_default_gateway(const int msglevel,
     {
       struct buffer out = alloc_buf_gc (256, &gc);
       buf_printf (&out, "ROUTE6_GATEWAY");
+      buf_printf (&out, " %s", print_in6_addr (rgi6->gateway.addr_ipv6, 0, &gc));
       if (rgi6->flags & RGI_ON_LINK)
 	buf_printf (&out, " ON_LINK");
-      else
-	buf_printf (&out, " %s", print_in6_addr (rgi6->gateway.addr_ipv6, 0, &gc));
       if (rgi6->flags & RGI_NETMASK_DEFINED)
 	buf_printf (&out, "/%d", rgi6->gateway.netbits_ipv6);
 #ifdef WIN32
@@ -2659,22 +2662,142 @@ get_default_gateway (struct route_gateway_info *rgi)
   gc_free (&gc);
 }
 
-/* IPv6 implementation using netlink (TBD)
+/* IPv6 implementation using netlink
+ * http://www.linuxjournal.com/article/7356
+ * netlink(3), netlink(7), rtnetlink(7)
+ * http://www.virtualbox.org/svn/vbox/trunk/src/VBox/NetworkServices/NAT/rtmon_linux.c
  */
+struct rtreq {
+	struct nlmsghdr nh;
+	struct rtmsg rtm;
+	char attrbuf[512];
+};
+
 void
 get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
 			 struct in6_addr *dest)
 {
-    msg(D_ROUTE, "no support for get_default_gateway_ipv6() on this system (Linux and Android)");
+    int nls = -1;
+    struct rtreq rtreq;
+    struct rtattr *rta;
+
+    char rtbuf[2000];
+    ssize_t ssize;
+
     CLEAR(*rgi6);
-    struct in6_addr g = IN6ADDR_LOOPBACK_INIT;
-
-    rgi6->flags = RGI_ADDR_DEFINED | RGI_IFACE_DEFINED | RGI_HWADDR_DEFINED | 
-    		RGI_NETMASK_DEFINED;
-    rgi6->gateway.addr_ipv6 = g;
-    rgi6->gateway.netbits_ipv6 = 64;
-    memcpy( rgi6->hwaddr, "\1\2\3\4\5\6", 6 );
-    strcpy( rgi6->iface, "eth1" );
+
+    nls = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
+    if ( nls < 0 )
+	{ msg(M_WARN|M_ERRNO, "GDG6: socket() failed" ); goto done; }
+
+    /* bind() is not needed, no unsolicited msgs coming in */
+
+    /* this will give us the best match only, not a full dump */
+    CLEAR(rtreq);
+    rtreq.nh.nlmsg_type = RTM_GETROUTE;
+    rtreq.nh.nlmsg_flags = NLM_F_REQUEST;			/* XXX */
+    rtreq.rtm.rtm_family = AF_INET6;
+    rtreq.rtm.rtm_src_len = 0;
+    rtreq.rtm.rtm_dst_len = 128;				/* exact dst */
+    rtreq.rtm.rtm_table = RT_TABLE_MAIN;
+    rtreq.rtm.rtm_protocol = RTPROT_UNSPEC;
+    rtreq.nh.nlmsg_len = NLMSG_SPACE(sizeof(rtreq.rtm));
+
+    /* set RTA_DST for target IPv6 address we want */
+    rta = (struct rtattr *)(((char *) &rtreq)+NLMSG_ALIGN(rtreq.nh.nlmsg_len));
+    rta->rta_type = RTA_DST;
+    rta->rta_len = RTA_LENGTH(16);
+    rtreq.nh.nlmsg_len = NLMSG_ALIGN(rtreq.nh.nlmsg_len) +
+                                             RTA_LENGTH(16);
+
+    if ( dest == NULL )			/* ::, unspecified */
+	memset( RTA_DATA(rta), 0, 16 );	/* :: = all-zero */
+    else
+	memcpy( RTA_DATA(rta), (void *)dest, 16 );
+
+    /* send and receive reply */
+    if ( send( nls, &rtreq, rtreq.nh.nlmsg_len, 0 ) < 0 )
+	{ msg(M_WARN|M_ERRNO, "GDG6: send() failed" ); goto done; }
+
+    ssize = recv(nls, rtbuf, sizeof(rtbuf), MSG_TRUNC);
+
+    if (ssize < 0)
+	{ msg(M_WARN|M_ERRNO, "GDG6: recv() failed" ); goto done; }
+
+    if (ssize > sizeof(rtbuf))
+    {
+	msg(M_WARN, "get_default_gateway_ipv6: returned message too big for buffer (%d>%d)", (int)ssize, (int)sizeof(rtbuf) );
+	goto done;
+    }
+
+    struct nlmsghdr *nh;
+
+    for (nh = (struct nlmsghdr *)rtbuf;
+         NLMSG_OK(nh, ssize);
+         nh = NLMSG_NEXT(nh, ssize))
+    {
+	struct rtmsg *rtm;
+	int attrlen;
+
+	if (nh->nlmsg_type == NLMSG_DONE) { break; }
+
+	if (nh->nlmsg_type == NLMSG_ERROR) {
+	    struct nlmsgerr *ne = (struct nlmsgerr *)NLMSG_DATA(nh);
+	    msg(M_WARN, "GDG6: NLSMG_ERROR: error %d\n", ne->error);
+	    break;
+	}
+
+	if (nh->nlmsg_type != RTM_NEWROUTE) {
+	    /* shouldn't happen */
+	    msg(M_WARN, "GDG6: unexpected msg_type %d", nh->nlmsg_type );
+	    continue;
+	}
+
+	rtm = (struct rtmsg *)NLMSG_DATA(nh);
+	attrlen = RTM_PAYLOAD(nh);
+
+	/* we're only looking for routes in the main table, as "we have
+	 * no IPv6" will lead to a lookup result in "Local" (::/0 reject)
+	 */
+	if (rtm->rtm_family != AF_INET6 ||
+	    rtm->rtm_table != RT_TABLE_MAIN)
+		{ continue; }		/* we're not interested */
+
+	for (rta = RTM_RTA(rtm);
+	     RTA_OK(rta, attrlen);
+	     rta = RTA_NEXT(rta, attrlen))
+	{
+	    if (rta->rta_type == RTA_GATEWAY) {
+		if ( RTA_PAYLOAD(rta) != sizeof(struct in6_addr) )
+			{ msg(M_WARN, "GDG6: RTA_GW size mismatch"); continue; }
+		rgi6->gateway.addr_ipv6 = *(struct in6_addr*) RTA_DATA(rta);
+		rgi6->flags |= RGI_ADDR_DEFINED;
+	    }
+	    else if (rta->rta_type == RTA_OIF) {
+		char ifname[IF_NAMESIZE+1];
+		int oif;
+		if ( RTA_PAYLOAD(rta) != sizeof(oif) )
+			{ msg(M_WARN, "GDG6: oif size mismatch"); continue; }
+
+		memcpy(&oif, RTA_DATA(rta), sizeof(oif));
+		if_indextoname(oif,ifname);
+		strncpy( rgi6->iface, ifname, sizeof(rgi6->iface)-1 );
+		rgi6->flags |= RGI_IFACE_DEFINED;
+	    }
+	}
+    }
+
+    /* if we have an interface but no gateway, the destination is on-link */
+    if ( ( rgi6->flags & (RGI_IFACE_DEFINED|RGI_ADDR_DEFINED) ) ==
+              RGI_IFACE_DEFINED )
+    {
+	rgi6->flags |= (RGI_ADDR_DEFINED | RGI_ON_LINK);
+	rgi6->gateway.addr_ipv6 = *dest;
+    }
+
+  done:
+    if (nls >= 0)
+	close (nls);
 }
 
 #elif defined(TARGET_DARWIN) || defined(TARGET_SOLARIS) || \
Signature · 1 line
2.3.6
#18 lore raw

PATCH 6/10 Implement handling of overlapping IPv6 routes with IPv6 remote VPN server address

In reply to Gert Doering

- socket.[ch]: add link_socket_current_remote_ipv6() helper to extract current address of remote VPN server (if IPv6, NULL otherwise), IPv6 equivalent to link_socket_current_remote()

- init.c: pass remote VPN server address to init_route_ipv6_list() (link_socket_current_remote_ipv6())

- route.h: add route_ipv6_gateway_info to route_ipv6_list, and reorder structures so that this actually compiles. Add iface/adapter_index to struct route_ipv6 (for non-tun/tap routes).

- route.[ch]: add "const" to *dest argument to get_default_gateway_ipv6()

- route.c: add route_ipv6_match_host() helper to check whether an IPv6 address is matched by a given "route_ipv6" IPv6 route)

- route.c: init_route_ipv6_list()
- call get_default_gateway_ipv6()
- check to-be-installed IPv6 routes against VPN server address (if IPv6)
- if an overlap is seen, add a host route for the VPN server address via the just-discovered gateway to the list of IPv6 routes to be installed (rl6->routes_ipv6)
- warn if overlap is detected but platform code has not been able to discover IPv6 default gateway

- route.c: add_route_ipv6() / delete_route_ipv6(): set "device" to "external default gateway interface" (r6->iface) instead of TUN/TAP device (if set), which nicely enables arbitrary gateway/interface combinations for Linux

- ssl.c: add "IV_RGI6=1" to push-peer-info data to let server know we can handle pushed IPv6 routes that overlap with server IPv6 address

- tun.c: when adding/removing on-link routes, CLEAR(r6) first to ensure new struct route_ipv6 members are cleared

Tested on Linux with iproute2 and /bin/route, on eth and tun routes.

---
 src/openvpn/init.c   |   4 +-
 src/openvpn/route.c  | 132 ++++++++++++++++++++++++++++++++++++++++++++++-----
 src/openvpn/route.h  |  36 ++++++++------
 src/openvpn/socket.c |  22 +++++++++
 src/openvpn/socket.h |   2 +
 src/openvpn/ssl.c    |   3 ++
 src/openvpn/tun.c    |   2 +
 7 files changed, 175 insertions(+), 26 deletions(-)
src/openvpn/init.c +3 -1
index 4c17905..0416e35 100644
@@ -1179,6 +1179,7 @@ do_init_route_list (const struct options *options,
 static void
 do_init_route_ipv6_list (const struct options *options,
 		    struct route_ipv6_list *route_ipv6_list,
+		    const struct link_socket_info *link_socket_info,
 		    bool fatal,
 		    struct env_set *es)
 {
@@ -1198,6 +1199,7 @@ do_init_route_ipv6_list (const struct options *options,
 			options->routes_ipv6,
 			gw,
 			metric,
+			link_socket_current_remote_ipv6 (link_socket_info),
 			es))
     {
       if (fatal)
@@ -1391,7 +1393,7 @@ do_open_tun (struct context *c)
       if (c->options.routes && c->c1.route_list && c->c2.link_socket)
 	do_init_route_list (&c->options, c->c1.route_list, &c->c2.link_socket->info, false, c->c2.es);
       if (c->options.routes_ipv6 && c->c1.route_ipv6_list )
-	do_init_route_ipv6_list (&c->options, c->c1.route_ipv6_list, false, c->c2.es);
+	do_init_route_ipv6_list (&c->options, c->c1.route_ipv6_list, &c->c2.link_socket->info, false, c->c2.es);
 
       /* do ifconfig */
       if (!c->options.ifconfig_noexec
src/openvpn/route.c +121 -11

226 lines not on this page · raw message

src/openvpn/route.h +22 -14
index 95cf99f..4bbcdb7 100644
@@ -128,19 +128,12 @@ struct route_ipv6 {
   unsigned int netbits;
   struct in6_addr gateway;
   int metric;
-};
-
-struct route_ipv6_list {
-  unsigned int iflags;			/* RL_ flags, see route_list */
-
-  unsigned int spec_flags;		/* RTSA_ flags, route_special_addr */
-  struct in6_addr remote_endpoint_ipv6;	/* inside tun */
-  struct in6_addr remote_host_ipv6;	/* --remote address */
-  int default_metric;
-
-  unsigned int flags;			/* RG_x flags, see route_option_list */
-  struct route_ipv6 *routes_ipv6;
-  struct gc_arena gc;
+  /* gateway interface */
+# ifdef WIN32
+  DWORD adapter_index;		/* interface or ~0 if undefined */
+#else
+  char * iface;			/* interface name (null terminated) */
+#endif
 };
 
 
@@ -218,6 +211,20 @@ struct route_list {
   struct gc_arena gc;
 };
 
+struct route_ipv6_list {
+  unsigned int iflags;			/* RL_ flags, see route_list */
+
+  unsigned int spec_flags;		/* RTSA_ flags, route_special_addr */
+  struct in6_addr remote_endpoint_ipv6;	/* inside tun */
+  struct in6_addr remote_host_ipv6;	/* --remote address */
+  int default_metric;
+
+  struct route_ipv6_gateway_info rgi6;
+  unsigned int flags;			/* RG_x flags, see route_option_list */
+  struct route_ipv6 *routes_ipv6;
+  struct gc_arena gc;
+};
+
 #if P2MP
 /* internal OpenVPN route */
 struct iroute {
@@ -274,6 +281,7 @@ bool init_route_ipv6_list (struct route_ipv6_list *rl6,
 		      const struct route_ipv6_option_list *opt6,
 		      const char *remote_endpoint,
 		      int default_metric,
+		      const struct in6_addr *remote_host,
 		      struct env_set *es);
 
 void route_list_add_vpn_gateway (struct route_list *rl,
@@ -301,7 +309,7 @@ bool is_special_addr (const char *addr_str);
 
 void get_default_gateway (struct route_gateway_info *rgi);
 void get_default_gateway_ipv6 (struct route_ipv6_gateway_info *rgi,
-				struct in6_addr *dest);
+				const struct in6_addr *dest);
 void print_default_gateway(const int msglevel,
                            const struct route_gateway_info *rgi,
                            const struct route_ipv6_gateway_info *rgi6);
src/openvpn/socket.c +22 -0
index 7f889b1..dcff7a3 100644
@@ -2143,6 +2143,28 @@ link_socket_current_remote (const struct link_socket_info *info)
     return 0;
 }
 
+const struct in6_addr *
+link_socket_current_remote_ipv6 (const struct link_socket_info *info)
+{
+  const struct link_socket_addr *lsa = info->lsa;
+
+/* This logic supports "redirect-gateway" semantic,
+ * for PF_INET6 routes over PF_INET6 endpoints
+ *
+ * For --remote entries with multiple addresses this
+ * only return the actual endpoint we have sucessfully connected to
+ */
+  if (lsa->actual.dest.addr.sa.sa_family != AF_INET6)
+    return NULL;
+
+  if (link_socket_actual_defined (&lsa->actual))
+    return &(lsa->actual.dest.addr.in6.sin6_addr);
+  else if (lsa->current_remote)
+    return &(((struct sockaddr_in6*)lsa->current_remote->ai_addr) ->sin6_addr);
+  else
+    return NULL;
+}
+
 /*
  * Return a status string describing socket state.
  */
src/openvpn/socket.h +2 -0
index 8e157c6..49cfab6 100644
@@ -419,6 +419,8 @@ void bad_address_length (int actual, int expected);
  */
 #define IPV4_INVALID_ADDR 0xffffffff
 in_addr_t link_socket_current_remote (const struct link_socket_info *info);
+const struct in6_addr * link_socket_current_remote_ipv6
+				     (const struct link_socket_info *info);
 
 void link_socket_connection_initiated (const struct buffer *buf,
 				       struct link_socket_info *info,
src/openvpn/ssl.c +3 -0
index 4e44410..54a3e09 100644
@@ -1853,6 +1853,9 @@ push_peer_info(struct buffer *buf, struct tls_session *session)
       comp_generate_peer_info_string(&session->opt->comp_options, &out);
 #endif
 
+      /* support for redirecting IPv6 gateway */
+      buf_printf(&out, "IV_RGI6=1\n");
+
       if (session->opt->push_peer_info_detail >= 2)
         {
 	  /* push mac addr */
src/openvpn/tun.c +2 -0
index 766a73c..24a61ec 100644
@@ -607,6 +607,7 @@ void add_route_connected_v6_net(struct tuntap * tt,
 {
     struct route_ipv6 r6;
 
+    CLEAR(r6);
     r6.network = tt->local_ipv6;
     r6.netbits = tt->netbits_ipv6;
     r6.gateway = tt->local_ipv6;
@@ -620,6 +621,7 @@ void delete_route_connected_v6_net(struct tuntap * tt,
 {
     struct route_ipv6 r6;
 
+    CLEAR(r6);
     r6.network = tt->local_ipv6;
     r6.netbits = tt->netbits_ipv6;
     r6.gateway = tt->local_ipv6;
Signature · 1 line
2.3.6
#23 lore raw

PATCH 7/10 Implement '--redirect-gateway ipv6'

In reply to Gert Doering

Add "ipv6" and "!ipv4" sub-options to "--redirect-gateway" option.

This is done in the same way as in the OpenVPN 3 code base, so "--redirect-gateway ipv6" will redirect both IPv4 and IPv6 - if you want v6-only, use "--redirect-gateway ipv6 !ipv4".

The actual implementation is much simpler than for IPv4 - we just add a few extra routes to the route_ipv6_option_list and leave it to init_route_ipv6_list() to figure out whether there is an overlap with IPv6 transport, and if yes, insert a host route to the VPN server via the current IPv6 default gateway.

---
 doc/openvpn.8         | 11 +++++++++++
 src/openvpn/init.c    | 15 +++++++++++++++
 src/openvpn/options.c |  7 +++++++
 3 files changed, 33 insertions(+)
doc/openvpn.8 +11 -0
index 0692a80..98378aa 100644
@@ -1240,6 +1240,17 @@ on non-Windows clients).
 Block access to local LAN when the tunnel is active, except for
 the LAN gateway itself.  This is accomplished by routing the local
 LAN (except for the LAN gateway address) into the tunnel.
+
+.B ipv6 --
+Redirect IPv6 routing into the tunnel.  This works similar to the
+.B def1
+flag, that is, more specific IPv6 routes are added (2000::/4, 3000::/4),
+covering the whole IPv6 unicast space.
+
+.B !ipv4 --
+Do not redirect IPv4 traffic - typically used in the flag pair
+.B "ipv6 !ipv4"
+to redirect IPv6-only.
 .\"*********************************************************
 .TP
 .B \-\-link\-mtu n
src/openvpn/init.c +15 -0
index 0416e35..c47f4a8 100644
@@ -1195,6 +1195,21 @@ do_init_route_ipv6_list (const struct options *options,
   if (options->route_default_metric)
     metric = options->route_default_metric;
 
+  /* redirect (IPv6) gateway to VPN?  if yes, add a few more specifics
+   */
+  if ( options->routes_ipv6->flags & RG_REROUTE_GW )
+    {
+      char *opt_list[] = { "::/3", "2000::/4", "3000::/4", "fc00::/7", NULL };
+      int i;
+
+      for (i=0; opt_list[i]; i++)
+	{
+	  add_route_ipv6_to_option_list( options->routes_ipv6,
+		      string_alloc (opt_list[i], options->routes_ipv6->gc),
+		      NULL, NULL );
+	}
+    }
+
   if (!init_route_ipv6_list (route_ipv6_list,
 			options->routes_ipv6,
 			gw,
src/openvpn/options.c +7 -0
index 93ea415..dc8985b 100644
@@ -5380,6 +5380,13 @@ add_option (struct options *options,
 	    options->routes->flags |= RG_BYPASS_DNS;
 	  else if (streq (p[j], "block-local"))
 	    options->routes->flags |= RG_BLOCK_LOCAL;
+	  else if (streq (p[j], "ipv6"))
+	    {
+	      rol6_check_alloc (options);
+	      options->routes_ipv6->flags |= RG_REROUTE_GW;
+	    }
+	  else if (streq (p[j], "!ipv4"))
+	    options->routes->flags &= ~RG_REROUTE_GW;
 	  else
 	    {
 	      msg (msglevel, "unknown --%s flag: %s", p[0], p[j]);
Signature · 1 line
2.3.6
#29 lore raw

PATCH 8/10 get_default_gateway_ipv6(): *BSD / MacOS / Solaris PF_ROUTE implementation

In reply to Gert Doering

As for IPv4, a common implementation for all (supported) BSD families and Solaris. Supporting the latter requires separate implementations for IPv4 and IPv6, unfortunately, so it's quite a bit of duplicate code.

Further, extend add_route_ipv6() and delete_route_ipv6() to handle link-local gateway addresses that require "gateway + interface" in scoped notation ("fe80::1%em0").

Tested on FreeBSD 7.4/amd64, 9.3/sparc64, 10.0/amd64, NetBSD 5.1/amd64, OpenBSD 4.9/i386, MacOS X 10.5/32 and 10.10/64 and OpenSolaris 10.11/i386.

---
 src/openvpn/route.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 183 insertions(+), 4 deletions(-)
src/openvpn/route.c +183 -4
index 555c3a6..29dd3fa 100644
@@ -1677,6 +1677,24 @@ add_route_ipv6 (struct route_ipv6 *r6, const struct tuntap *tt, unsigned int fla
   network = print_in6_addr_netbits_only( r6->network, r6->netbits, &gc);
   gateway = print_in6_addr( r6->gateway, 0, &gc);
 
+#if defined(TARGET_DARWIN) || \
+	defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) || \
+	defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
+
+  /* the BSD platforms cannot specify gateway and interface independently,
+   * but for link-local destinations, we MUST specify the interface, so
+   * we build a combined "$gateway%$interface" gateway string
+   */
+  if ( r6->iface != NULL && gateway_needed &&
+       IN6_IS_ADDR_LINKLOCAL(&r6->gateway) )		/* fe80::...%intf */
+    {
+      int len = strlen(gateway) + 1 + strlen(r6->iface)+1;
+      char * tmp = gc_malloc( len, true, &gc );
+      snprintf( tmp, len, "%s%%%s", gateway, r6->iface );
+      gateway = tmp;
+    }
+#endif
+
   if ( !tt->ipv6 )
     {
       msg( M_INFO, "add_route_ipv6(): not adding %s/%d, no IPv6 on if %s",
@@ -2059,6 +2077,24 @@ delete_route_ipv6 (const struct route_ipv6 *r6, const struct tuntap *tt, unsigne
   network = print_in6_addr_netbits_only( r6->network, r6->netbits, &gc);
   gateway = print_in6_addr( r6->gateway, 0, &gc);
 
+#if defined(TARGET_DARWIN) || \
+	defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) || \
+	defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
+
+  /* the BSD platforms cannot specify gateway and interface independently,
+   * but for link-local destinations, we MUST specify the interface, so
+   * we build a combined "$gateway%$interface" gateway string
+   */
+  if ( r6->iface != NULL && gateway_needed &&
+       IN6_IS_ADDR_LINKLOCAL(&r6->gateway) )		/* fe80::...%intf */
+    {
+      int len = strlen(gateway) + 1 + strlen(r6->iface)+1;
+      char * tmp = gc_malloc( len, true, &gc );
+      snprintf( tmp, len, "%s%%%s", gateway, r6->iface );
+      gateway = tmp;
+    }
+#endif
+
   if ( !tt->ipv6 )
     {
       msg( M_INFO, "delete_route_ipv6(): not deleting %s/%d, no IPv6 on if %s",
@@ -2948,14 +2984,14 @@ struct rtmsg {
 #if defined(TARGET_SOLARIS)
 #define NEXTADDR(w, u) \
         if (rtm_addrs & (w)) {\
-            l = ROUNDUP(sizeof(struct sockaddr_in)); memmove(cp, &(u), l); cp += l;\
+            l = ROUNDUP(sizeof(u)); memmove(cp, &(u), l); cp += l;\
         }
 
 #define ADVANCE(x, n) (x += ROUNDUP(sizeof(struct sockaddr_in)))
 #else
 #define NEXTADDR(w, u) \
         if (rtm_addrs & (w)) {\
-            l = ROUNDUP(u.sa_len); memmove(cp, &(u), l); cp += l;\
+            l = ROUNDUP( ((struct sockaddr *)&(u))->sa_len); memmove(cp, &(u), l); cp += l;\
         }
 
 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
@@ -3152,14 +3188,157 @@ get_default_gateway (struct route_gateway_info *rgi)
   gc_free (&gc);
 }
 
-/* BSD implementation using routing socket (as does IPv4) (TBD)
+/* BSD implementation using routing socket (as does IPv4)
+ * (the code duplication is somewhat unavoidable if we want this to
+ * work on OpenSolaris as well.  *sigh*)
  */
+
+/* Solaris has no length field - this is ugly, but less #ifdef in total
+ */
+#if defined(TARGET_SOLARIS)
+# undef ADVANCE
+# define ADVANCE(x, n) (x += ROUNDUP(sizeof(struct sockaddr_in6)))
+#endif
+
 void
 get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
 			 const struct in6_addr *dest)
 {
-    msg(D_ROUTE, "no support for get_default_gateway_ipv6() on this system (BSD and OSX)");
+
+    struct rtmsg m_rtmsg;
+    int sockfd = -1;
+    int seq, l, pid, rtm_addrs, i;
+    struct sockaddr_in6 so_dst, so_mask;
+    char *cp = m_rtmsg.m_space;
+    struct sockaddr *gate = NULL, *ifp = NULL, *sa;
+    struct rt_msghdr *rtm_aux;
+
     CLEAR(*rgi6);
+
+    /* setup data to send to routing socket */
+    pid = getpid();
+    seq = 0;
+    rtm_addrs = RTA_DST | RTA_NETMASK | RTA_IFP;
+
+    bzero(&m_rtmsg, sizeof(m_rtmsg));
+    bzero(&so_dst, sizeof(so_dst));
+    bzero(&so_mask, sizeof(so_mask));
+    bzero(&rtm, sizeof(struct rt_msghdr));
+
+    rtm.rtm_type = RTM_GET;
+    rtm.rtm_flags = RTF_UP;
+    rtm.rtm_version = RTM_VERSION;
+    rtm.rtm_seq = ++seq;
+
+    so_dst.sin6_family = AF_INET6;
+    so_mask.sin6_family = AF_INET6;
+
+    if ( dest != NULL &&		/* specific host? */
+	 !IN6_IS_ADDR_UNSPECIFIED(dest) )
+    {
+	so_dst.sin6_addr = *dest;
+	/* :: needs /0 "netmask", host route wants "no netmask */
+	rtm_addrs &= ~RTA_NETMASK;
+    }
+
+    rtm.rtm_addrs = rtm_addrs;
+
+#ifndef TARGET_SOLARIS
+    so_dst.sin6_len = sizeof(struct sockaddr_in6);
+    so_mask.sin6_len = sizeof(struct sockaddr_in6);
+#endif
+
+    NEXTADDR(RTA_DST, so_dst);
+    NEXTADDR(RTA_NETMASK, so_mask);
+
+    rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
+
+    /* transact with routing socket */
+    sockfd = socket(PF_ROUTE, SOCK_RAW, 0);
+    if (sockfd < 0)
+    {
+	msg (M_WARN, "GDG6: socket #1 failed");
+	goto done;
+    }
+    if (write(sockfd, (char *)&m_rtmsg, l) < 0)
+    {
+	msg (M_WARN, "GDG6: problem writing to routing socket");
+	goto done;
+    }
+
+    do
+    {
+	l = read(sockfd, (char *)&m_rtmsg, sizeof(m_rtmsg));
+    }
+    while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
+
+    close(sockfd);
+    sockfd = -1;
+
+    /* extract return data from routing socket */
+    rtm_aux = &rtm;
+    cp = ((char *)(rtm_aux + 1));
+    if (rtm_aux->rtm_addrs)
+    {
+	for (i = 1; i; i <<= 1)
+	{
+	    if (i & rtm_aux->rtm_addrs)
+	    {
+		sa = (struct sockaddr *)cp;
+		if (i == RTA_GATEWAY )
+		    gate = sa;
+		else if (i == RTA_IFP)
+		    ifp = sa;
+		ADVANCE(cp, sa);
+	    }
+	}
+    }
+    else
+      goto done;
+
+    /* get gateway addr and interface name */
+    if (gate != NULL )
+    {
+	struct sockaddr_in6 * s6 = (struct sockaddr_in6 *)gate;
+	struct in6_addr gw = s6->sin6_addr;
+
+#ifndef TARGET_SOLARIS
+	/* You do not really want to know... from FreeBSD's route.c
+	 * (KAME encodes the 16 bit scope_id in s6_addr[2] + [3],
+         * but for a correct link-local address these must be :0000: )
+	 */
+	if ( gate->sa_len == sizeof(struct sockaddr_in6) &&
+	     IN6_IS_ADDR_LINKLOCAL(&gw) )
+        {
+	    gw.s6_addr[2] = gw.s6_addr[3] = 0;
+	}
+
+	if ( gate->sa_len != sizeof(struct sockaddr_in6) ||
+	     IN6_IS_ADDR_UNSPECIFIED(&gw) )
+	{
+	    rgi6->flags |= RGI_ON_LINK;
+	}
+	else
+#endif
+
+	rgi6->gateway.addr_ipv6 = gw;
+	rgi6->flags |= RGI_ADDR_DEFINED;
+
+	if (ifp)
+	{
+	    /* get interface name */
+	    const struct sockaddr_dl *adl = (struct sockaddr_dl *) ifp;
+	    if (adl->sdl_nlen && adl->sdl_nlen < sizeof(rgi6->iface))
+	    {
+		memcpy (rgi6->iface, adl->sdl_data, adl->sdl_nlen);
+		rgi6->flags |= RGI_IFACE_DEFINED;
+	    }
+	}
+    }
+
+  done:
+    if (sockfd >= 0)
+	close(sockfd);
 }
 
 #undef max
Signature · 1 line
2.3.6
#32 lore raw

PATCH 9/10 Fix IPv6 host routes to LAN gateway on OpenSolaris

In reply to Gert Doering

The tun/tap routes need to be set with "metric 0", while this will prevent routes to LAN gateways from being installed. So, set metric 0 only if no other interface is requested...

(Note: OpenSolaris can not specify host+interface gateways, so we just use the GW addresses - it seems to still work for fe80:: addresses, however it is done internally. NUD maybe?)

---
 src/openvpn/route.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
src/openvpn/route.c +6 -1
index 29dd3fa..d97968c 100644
@@ -1800,12 +1800,17 @@ add_route_ipv6 (struct route_ipv6 *r6, const struct tuntap *tt, unsigned int fla
    * packets will just disappear somewhere.  So we use "0" now...
    */
 
-  argv_printf (&argv, "%s add -inet6 %s/%d %s 0",
+  argv_printf (&argv, "%s add -inet6 %s/%d %s",
 		ROUTE_PATH,
 		network,
 		r6->netbits,
 		gateway );
 
+  /* only append 0 for "tun/tap" routes, not for "gateway on other interface"
+   */
+  if ( !r6->iface )
+     argv_printf_cat (&argv, "0");
+
   argv_msg (D_ROUTE, &argv);
   status = openvpn_execve_check (&argv, es, 0, "ERROR: Solaris route add -inet6 command failed");
 
Signature · 1 line
2.3.6
#35 lore raw

PATCH 10/10 get_default_gateway_ipv6(): Win32 implementation using GetBestRoute2()

In reply to Gert Doering

To get access to that functionality, bump Windows API level for MinGW compilation from NTDDI_WINXP/_WIN32_WINNT_WINXP to ..._VISTA, and shuffle around WIN32 includes a bit in syshead.h

MinGW 32 seems to be broken regarding MIB_TCP_STATE enum, so add typedef for that - surrounding #ifdefs found by googling do not work yet -> TODO!

Extend add_route_ipv6() and delete_route_ipv6() to handle routes not on the tap adapter but on ifindex-addressed interfaces ("interface=nn"), and while at it, fix deletion of IPv6 routes with gateway address.

NOTE: this breaks Windows XP compatibility as GetBestRoute2() is not available there, so even when not using IPv6, the binary will not run.

(Lightly) tested on Win7/64.

---
 configure.ac          |  2 +-
 src/openvpn/route.c   | 92 +++++++++++++++++++++++++++++++++++++++++++++++----
 src/openvpn/syshead.h |  7 +++-
 3 files changed, 93 insertions(+), 8 deletions(-)
configure.ac +1 -1
index 51ef93b..0a8255c 100644
@@ -348,7 +348,7 @@ case "$host" in
 		AC_DEFINE([TARGET_WIN32], [1], [Are we running WIN32?])
 		AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["W"], [Target prefix])
 		CPPFLAGS="${CPPFLAGS} -DWIN32_LEAN_AND_MEAN"
-		CPPFLAGS="${CPPFLAGS} -DNTDDI_VERSION=NTDDI_WINXP -D_WIN32_WINNT=_WIN32_WINNT_WINXP"
+		CPPFLAGS="${CPPFLAGS} -DNTDDI_VERSION=NTDDI_VISTA -D_WIN32_WINNT=_WIN32_WINNT_VISTA"
 		WIN32=yes
 		;;
 	*-*-dragonfly*)
src/openvpn/route.c +86 -6

129 lines not on this page · raw message

src/openvpn/syshead.h +6 -1
index ff0bf41..e00af48 100644
@@ -358,8 +358,13 @@
 #endif /* TARGET_DARWIN */
 
 #ifdef WIN32
-#include <iphlpapi.h>
+ // Missing declarations for MinGW 32.
+ // #if !defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 2
+ typedef int MIB_TCP_STATE;
+ // #endif
+#include <naptypes.h>
 #include <ntddndis.h>
+#include <iphlpapi.h>
 #include <wininet.h>
 #include <shellapi.h>
 /* The following two headers are needed of PF_INET6 */
Signature · 1 line
2.3.6
#24 lore raw

Re: [Openvpn-devel] [PATCH 07/10] Implement '--redirect-gateway ipv6'

In reply to Gert Doering

Am 11.09.15 um 17:33 schrieb Gert Doering:

Quoted 6 lines

Add "ipv6" and "!ipv4" sub-options to "--redirect-gateway" option.

This is done in the same way as in the OpenVPN 3 code base, so "--redirect-gateway ipv6" will redirect both IPv4 and IPv6 - if you want v6-only, use "--redirect-gateway ipv6 !ipv4".

Current 2.3 code will exit with an error on seeing the ipv6 keyword.

Reading the source code this is equivalent to

redirect-gateway
route-ipv6 ::/3
route-ipv6 2000::/4
route-ipv6 3000::/4
route-ipv6 fc00::/7

which also works in 2.3. What do we offer as config advice for users?

(It is good that this works otherwise my code will break configs that use a remote ipv6-enabled-server)

Also the documentation is slightly wrong.

Sidenote: What about IPv6 multicast?

Arne

#2 lore raw

Re: [Openvpn-devel] [PATCH 01/10] refactor struct route_ipv6, bring in line with struct route_ipv4 again

In reply to Gert Doering

Am 11.09.15 um 17:33 schrieb Gert Doering:

Quoted 9 lines

adjust "struct route_ipv6" (and all users) to reflect changes to "struct route_ipv4" done in commit 7fb0e07e, namely:

- new member "r6->flags"
- "r6->defined" becomes "r6->flags & RT_DEFINED"
- "r6->metric_defined" becomes "r6->flags & RT_METRIC_DEFINED"
- route addition status is stored in "r6->flags & RT_ADDED"

ACK.

Arne

#19 lore raw

Re: [Openvpn-devel] [PATCH 06/10] Implement handling of overlapping IPv6 routes with IPv6 remote VPN server address

In reply to Gert Doering

Am 11.09.15 um 17:33 schrieb Gert Doering:

Quoted 30 lines

- socket.[ch]: add link_socket_current_remote_ipv6() helper to extract current address of remote VPN server (if IPv6, NULL otherwise), IPv6 equivalent to link_socket_current_remote()

- init.c: pass remote VPN server address to init_route_ipv6_list() (link_socket_current_remote_ipv6())

- route.h: add route_ipv6_gateway_info to route_ipv6_list, and reorder structures so that this actually compiles. Add iface/adapter_index to struct route_ipv6 (for non-tun/tap routes).

- route.[ch]: add "const" to *dest argument to get_default_gateway_ipv6()

- route.c: add route_ipv6_match_host() helper to check whether an IPv6 address is matched by a given "route_ipv6" IPv6 route)

- route.c: init_route_ipv6_list()
- call get_default_gateway_ipv6()
- check to-be-installed IPv6 routes against VPN server address (if IPv6)
- if an overlap is seen, add a host route for the VPN server address via the just-discovered gateway to the list of IPv6 routes to be installed (rl6->routes_ipv6)
- warn if overlap is detected but platform code has not been able to discover IPv6 default gateway

- route.c: add_route_ipv6() / delete_route_ipv6(): set "device" to "external default gateway interface" (r6->iface) instead of TUN/TAP device (if set), which nicely enables arbitrary gateway/interface combinations for Linux

Quoted 8 lines

- ssl.c: add "IV_RGI6=1" to push-peer-info data to let server know we can handle pushed IPv6 routes that overlap with server IPv6 address

- tun.c: when adding/removing on-link routes, CLEAR(r6) first to ensure new struct route_ipv6 members are cleared

Tested on Linux with iproute2 and /bin/route, on eth and tun routes.

I think we may need an (explicit) opt out of the mechanism. Currently the code that inserts the IPv6 host route will always run.

Arne

#5 lore raw

Re: [Openvpn-devel] [PATCH 02/10] refactor struct route_ipv6_list, bring in line with struct route_list again

In reply to Gert Doering

Am 11.09.15 um 17:33 schrieb Gert Doering:

Quoted 3 lines

adjust "struct route_ipv6_list" (and all users) to reflect changes to "struct route_list" done in commit 7fb0e07e, namely:

ACK

#11 lore raw

Re: [Openvpn-devel] [PATCH 04/10] Create basic infrastructure for IPv6 default gateway handling / redirection.

In reply to Gert Doering

Am 11.09.15 um 17:33 schrieb Gert Doering:

Quoted 7 lines

- introduce get_default_gateway_ipv6() and add stub functions with the implementation plan to the 4 major code blocks here (Windows, Linux/Android, *BSD and Solaris, "others")

- add &rgi6 to print_default_gateway(), and teach it to print v4, v6 or both, depending on the calling environment

Quoted 7 lines

- unlike IPv4 (today), get_default_gateway_ipv6() is passed the actual target IPv6 address of the server we're looking for, so we can handle more complicated routing setups ("default to eth0, vpn server to ppp0") correctly

- consequently, --get-default-gateway has an optional parameter now, the IPv6 address to look up (for debugging)

We should document this.

ACK.

#20 lore raw

Re: [Openvpn-devel] [PATCH 06/10] Implement handling of overlapping IPv6 routes with IPv6 remote VPN server address

In reply to Arne Schwabe

Hi,

Quoted 2 lines · Arne Schwabe

I think we may need an (explicit) opt out of the mechanism. Currently the code that inserts the IPv6 host route will always run.

It will only insert the host route if you connect over IPv6, and you try to install IPv6 routes that overlap with the server address (need_remote_ipv6_route=true in init_route_ipv6_list()). If there is no overlap, or you connect over IPv4, it won't do anything (just re-tested to ensure that I did not forget the "we don't need this now" case :-) )

That it auto-activates is intentional - with IPv4, if you do not set --redirect-private, and have overlapping pushed routes with the VPN server address, it will silently fail due to recursive routing (unless you have a VPN API to do the right thing). So I wanted this to do the right thing automatically on the other platforms as well...

But I can be convinced to add an opt-out switch - what use case do you have in mind where this would be needed? In some cases, the logic is not actually *necessary* (if you have a more-specific route for the VPN server than what is pushed), but it won't do *harm* in that case either (tested with /64 routes and pushing /32).

(In any case, thanks for reviewing the first few patches in the set and the feedback)

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#12 lore raw

Re: [Openvpn-devel] [PATCH 04/10] Create basic infrastructure for IPv6 default gateway handling / redirection.

In reply to Arne Schwabe

Hi,

Quoted 6 lines · Arne Schwabe
Quoted 2 lines

- consequently, --get-default-gateway has an optional parameter now, the IPv6 address to look up (for debugging)

We should document this.

ACK.

Yes, right you are. I'll add --show-gateway to openvpn.8 - right now it seems to be totally undocumented, with or without the option :-)

(The commit message is also not correct here, as the option is called "--show-gateway" - to be fixed on commit)

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#25 lore raw

Re: [Openvpn-devel] [PATCH 07/10] Implement '--redirect-gateway ipv6'

In reply to Arne Schwabe

Hi,

Quoted 8 lines · Arne Schwabe

Am 11.09.15 um 17:33 schrieb Gert Doering:

Quoted 5 lines

Add "ipv6" and "!ipv4" sub-options to "--redirect-gateway" option.

This is done in the same way as in the OpenVPN 3 code base, so "--redirect-gateway ipv6" will redirect both IPv4 and IPv6 - if you want v6-only, use "--redirect-gateway ipv6 !ipv4".

Current 2.3 code will exit with an error on seeing the ipv6 keyword.

True. This inconsistency has been there since 3 introduced the keyword...

Quoted 9 lines

Reading the source code this is equivalent to

redirect-gateway
route-ipv6 ::/3
route-ipv6 2000::/4
route-ipv6 3000::/4
route-ipv6 fc00::/7

which also works in 2.3. What do we offer as config advice for users?

Right now, pushing "route-ipv6 2000::/3" or the equivalent of the above.

What about adding this part of the patch set to 2.3? It does not need the "discover and redirect gateway for IPv6" infrastructure - as it basically is just a macro anyway..

Quoted 4 lines

(It is good that this works otherwise my code will break configs that use a remote ipv6-enabled-server)

Also the documentation is slightly wrong.

What's wrong?

Quoted 1 line

Sidenote: What about IPv6 multicast?

Right now, our IPv6 multicast support is pretty much non-existing, both link-local or "routed wide area". I have not personally run into any issues that needed IPv6 multicast support, and can't remember any open trac tickets, so I'm not sure what we'd need...

(Our IPv4 multicast support is also fairly limited - if a packet hits the p2mp server, it is replicated to all clients, and that's it. No IGMP, PIM, or anything more smart... for IPv4, there *is* a trac ticket, but is classified as "wish list")

Extra Side-Note: link-local address support is also not there today.

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#3 lore raw

[Openvpn-devel] [PATCH applied] Re: refactor struct route_ipv6, bring in line with struct route_ipv4 again

In reply to Gert Doering

Patch has been applied to the master branch.

commit c3ef2d2333fb73f8a6d460d96523d23f89e56ba2

Author: Gert Doering
Date:   Fri Sep 11 17:33:38 2015 +0200
     refactor struct route_ipv6, bring in line with struct route_ipv4 again

     Signed-off-by: Gert Doering <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: http://article.gmane.org/gmane.network.openvpn.devel/10083

--
kind regards,

Gert Doering

#6 lore raw

[Openvpn-devel] [PATCH applied] Re: refactor struct route_ipv6_list, bring in line with struct route_list again

In reply to Gert Doering

Patch has been applied to the master branch.

commit 0ad73859420379ec45e159e5e7bd5bb7be9382fe

Author: Gert Doering
Date:   Fri Sep 11 17:33:39 2015 +0200
     refactor struct route_ipv6_list, bring in line with struct route_list again

     Signed-off-by: Gert Doering <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: http://article.gmane.org/gmane.network.openvpn.devel/10084

--
kind regards,

Gert Doering

#9 lore raw

[Openvpn-devel] [PATCH applied] Re: Add route_ipv6_gateway* data structures for rgi6 support.

In reply to Gert Doering

Patch has been applied to the master branch.

commit c0da18cd7cca481fd918620331540e565f11ce23

Author: Gert Doering
Date:   Fri Sep 11 17:33:40 2015 +0200
     Add route_ipv6_gateway* data structures for rgi6 support.

     Signed-off-by: Gert Doering <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: http://article.gmane.org/gmane.network.openvpn.devel/10088

--
kind regards,

Gert Doering

#15 lore raw

Re: [Openvpn-devel] [PATCH 05/10] get_default_gateway_ipv6(): Linux / Netlink implementation.

In reply to Gert Doering
+    CLEAR(rtreq);
+    rtreq.nh.nlmsg_type = RTM_GETROUTE;
+    rtreq.nh.nlmsg_flags = NLM_F_REQUEST;			/* XXX */

There should an indication why this is XXX, some kind of comment.

Quoted 34 lines
src/openvpn/route.c +34 -0
+    rtreq.rtm.rtm_family = AF_INET6;
+    rtreq.rtm.rtm_src_len = 0;
+    rtreq.rtm.rtm_dst_len = 128;				/* exact dst */
+    rtreq.rtm.rtm_table = RT_TABLE_MAIN;
+    rtreq.rtm.rtm_protocol = RTPROT_UNSPEC;
+    rtreq.nh.nlmsg_len = NLMSG_SPACE(sizeof(rtreq.rtm));
+
+    /* set RTA_DST for target IPv6 address we want */
+    rta = (struct rtattr *)(((char *) &rtreq)+NLMSG_ALIGN(rtreq.nh.nlmsg_len));
+    rta->rta_type = RTA_DST;
+    rta->rta_len = RTA_LENGTH(16);
+    rtreq.nh.nlmsg_len = NLMSG_ALIGN(rtreq.nh.nlmsg_len) +
+                                             RTA_LENGTH(16);
+
+    if ( dest == NULL )			/* ::, unspecified */
+	memset( RTA_DATA(rta), 0, 16 );	/* :: = all-zero */
+    else
+	memcpy( RTA_DATA(rta), (void *)dest, 16 );
+
+    /* send and receive reply */
+    if ( send( nls, &rtreq, rtreq.nh.nlmsg_len, 0 ) < 0 )
+	{ msg(M_WARN|M_ERRNO, "GDG6: send() failed" ); goto done; }
+
+    ssize = recv(nls, rtbuf, sizeof(rtbuf), MSG_TRUNC);
+
+    if (ssize < 0)
+	{ msg(M_WARN|M_ERRNO, "GDG6: recv() failed" ); goto done; }
+
+    if (ssize > sizeof(rtbuf))
+    {
+	msg(M_WARN, "get_default_gateway_ipv6: returned message too big for buffer (%d>%d)", (int)ssize, (int)sizeof(rtbuf) );
+	goto done;
+    }
+

src/openvpn/route.c get_default_gateway (struct route_gateway_info *rgi) · line 2742

Quoted 10 lines
src/openvpn/route.c +10 -0
+    struct nlmsghdr *nh;
+
+    for (nh = (struct nlmsghdr *)rtbuf;
+         NLMSG_OK(nh, ssize);
+         nh = NLMSG_NEXT(nh, ssize))
+    {
+	struct rtmsg *rtm;
+	int attrlen;
+
+	if (nh->nlmsg_type == NLMSG_DONE) { break; }

The coding style here is different ithan in other parts of OpenVPN.

Quoted 13 lines
src/openvpn/route.c +13 -0
+
+	if (nh->nlmsg_type == NLMSG_ERROR) {
+	    struct nlmsgerr *ne = (struct nlmsgerr *)NLMSG_DATA(nh);
+	    msg(M_WARN, "GDG6: NLSMG_ERROR: error %d\n", ne->error);
+	    break;
+	}
+
+	if (nh->nlmsg_type != RTM_NEWROUTE) {
+	    /* shouldn't happen */
+	    msg(M_WARN, "GDG6: unexpected msg_type %d", nh->nlmsg_type );
+	    continue;
+	}
+

src/openvpn/route.c get_default_gateway (struct route_gateway_info *rgi) · line 2765

Quoted 10 lines
src/openvpn/route.c +10 -0
+	rtm = (struct rtmsg *)NLMSG_DATA(nh);
+	attrlen = RTM_PAYLOAD(nh);
+
+	/* we're only looking for routes in the main table, as "we have
+	 * no IPv6" will lead to a lookup result in "Local" (::/0 reject)
+	 */
+	if (rtm->rtm_family != AF_INET6 ||
+	    rtm->rtm_table != RT_TABLE_MAIN)
+		{ continue; }		/* we're not interested */
+

We night want to revisit this with more complex routing scenarios. Android for example uses the main routing table only to jump more specific routing tables. Other system might do the same.

Quoted 26 lines
src/openvpn/route.c +25 -0
+	for (rta = RTM_RTA(rtm);
+	     RTA_OK(rta, attrlen);
+	     rta = RTA_NEXT(rta, attrlen))
+	{
+	    if (rta->rta_type == RTA_GATEWAY) {
+		if ( RTA_PAYLOAD(rta) != sizeof(struct in6_addr) )
+			{ msg(M_WARN, "GDG6: RTA_GW size mismatch"); continue; }
+		rgi6->gateway.addr_ipv6 = *(struct in6_addr*) RTA_DATA(rta);
+		rgi6->flags |= RGI_ADDR_DEFINED;
+	    }
+	    else if (rta->rta_type == RTA_OIF) {
+		char ifname[IF_NAMESIZE+1];
+		int oif;
+		if ( RTA_PAYLOAD(rta) != sizeof(oif) )
+			{ msg(M_WARN, "GDG6: oif size mismatch"); continue; }
+
+		memcpy(&oif, RTA_DATA(rta), sizeof(oif));
+		if_indextoname(oif,ifname);
+		strncpy( rgi6->iface, ifname, sizeof(rgi6->iface)-1 );
+		rgi6->flags |= RGI_IFACE_DEFINED;
+	    }
+	}
+    }
+
+    /* if we have an interface but no gateway, the destination is on-link */

src/openvpn/route.c get_default_gateway (struct route_gateway_info *rgi) · line 2800

Quoted 10 lines
src/openvpn/route.c +10 -0
+    if ( ( rgi6->flags & (RGI_IFACE_DEFINED|RGI_ADDR_DEFINED) ) ==
+              RGI_IFACE_DEFINED )
+    {
+	rgi6->flags |= (RGI_ADDR_DEFINED | RGI_ON_LINK);
+	rgi6->gateway.addr_ipv6 = *dest;
+    }
+
+  done:
+    if (nls >= 0)
+	close (nls);

Aside from these comment, this gets an ACK from me.

Arne

#13 lore raw

[Openvpn-devel] [PATCH applied] Re: Create basic infrastructure for IPv6 default gateway handling / redirection.

In reply to Gert Doering

Patch has been applied to the master branch, with an addition to openvpn.8 (see below).

commit d8a8656f1a8721f56a08439afe24916beadfef55

Author: Gert Doering
Date:   Fri Sep 11 17:33:41 2015 +0200
     Create basic infrastructure for IPv6 default gateway handling / redirection.

     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: http://article.gmane.org/gmane.network.openvpn.devel/10087
     Signed-off-by: Gert Doering <[email protected]...>

According to Arne's comment, openvpn.8 has been extended as follows, to actually document --show-gateway

doc/openvpn.8 +9 -0
@@ -5616,6 +5616,15 @@ module will be queried.
 .B \-\-verb
 option can be used BEFORE this option to produce debugging information.
 .\"*********************************************************
+.SS Standalone Debug Options:
+.\"*********************************************************
+.TP
+.B \-\-show\-gateway [v6target]
+(Standalone)
+Show current IPv4 and IPv6 default gateway and interface towards the
+gateway (if the protocol in question is enabled).  If an IPv6 address
+is passed as argument, the IPv6 route for this host is reported.
+.\"*********************************************************
 .SS IPv6 Related Options
 .\"*********************************************************
 The following options exist to support IPv6 tunneling in peer-to-peer

--
kind regards,

Gert Doering

#21 lore raw

Re: [Openvpn-devel] [PATCH 06/10] Implement handling of overlapping IPv6 routes with IPv6 remote VPN server address

In reply to Gert Doering

+ * "good enough")
+ */
+static bool
+route_ipv6_match_host( const struct route_ipv6 *r6,
+ const struct in6_addr *host )
+{

+    int bits = r6->netbits;
+    int i;
+    unsigned int mask;

+
+ if ( bits<0 || bits>128 )
+ return false;

bits should be unisgned int since r6->netbits is unsigned int and also drop the bits <0

Rest looks fine to me. So ACK.

Arne

#26 lore raw

Re: [Openvpn-devel] [PATCH 07/10] Implement '--redirect-gateway ipv6'

In reply to Gert Doering

Am 11.09.15 um 16:33 schrieb Gert Doering:

Quoted 47 lines

Add "ipv6" and "!ipv4" sub-options to "--redirect-gateway" option.

This is done in the same way as in the OpenVPN 3 code base, so "--redirect-gateway ipv6" will redirect both IPv4 and IPv6 - if you want v6-only, use "--redirect-gateway ipv6 !ipv4".

The actual implementation is much simpler than for IPv4 - we just add a few extra routes to the route_ipv6_option_list and leave it to init_route_ipv6_list() to figure out whether there is an overlap with IPv6 transport, and if yes, insert a host route to the VPN server via the current IPv6 default gateway.

---
 doc/openvpn.8         | 11 +++++++++++
 src/openvpn/init.c    | 15 +++++++++++++++
 src/openvpn/options.c |  7 +++++++
 3 files changed, 33 insertions(+)
doc/openvpn.8 +11 -0
index 0692a80..98378aa 100644
@@ -1240,6 +1240,17 @@ on non-Windows clients).
 Block access to local LAN when the tunnel is active, except for
 the LAN gateway itself.  This is accomplished by routing the local
 LAN (except for the LAN gateway address) into the tunnel.
+
+.B ipv6 --
+Redirect IPv6 routing into the tunnel.  This works similar to the
+.B def1
+flag, that is, more specific IPv6 routes are added (2000::/4, 3000::/4),
+covering the whole IPv6 unicast space.
+
+.B !ipv4 --
+Do not redirect IPv4 traffic - typically used in the flag pair
+.B "ipv6 !ipv4"
+to redirect IPv6-only.
 .\"*********************************************************
 .TP
 .B \-\-link\-mtu n
src/openvpn/init.c +0 -0
index 0416e35..c47f4a8 100644
@@ -1195,6 +1195,21 @@ do_init_route_ipv6_list (const struct options *options,
   if (options->route_default_metric)

src/openvpn/init.c do_init_route_ipv6_list (const struct options *options, · line 1205

Quoted 10 lines
src/openvpn/init.c +7 -0
     metric = options->route_default_metric;
 
+  /* redirect (IPv6) gateway to VPN?  if yes, add a few more specifics
+   */
+  if ( options->routes_ipv6->flags & RG_REROUTE_GW )
+    {
+      char *opt_list[] = { "::/3", "2000::/4", "3000::/4", "fc00::/7", NULL };
+      int i;
+
 

I cannot judge how good or bad this default list is. I usually have don the the ::/1 and 8000::/1 stuff.

Otherwise the patch is fine, so ACK.

Arne

#27 lore raw

Re: [Openvpn-devel] [PATCH 07/10] Implement '--redirect-gateway ipv6'

Hi,

Quoted 8 lines · Arne Schwabe

src/openvpn/init.c do_init_route_ipv6_list (const struct options *options, · line 1204

Quoted 6 lines
src/openvpn/init.c +5 -0
+  if ( options->routes_ipv6->flags & RG_REROUTE_GW )
+    {
+      char *opt_list[] = { "::/3", "2000::/4", "3000::/4", "fc00::/7", NULL };
+      int i;
+

I cannot judge how good or bad this default list is. I usually have don the the ::/1 and 8000::/1 stuff.

I have seen "2000::/3" used in the wild - some older CentOs boxes just did not like ::/0 as default. This is why...

4::/4 to e::/4 are not covered right now, as this is not IPv6 unicast space (it is very likely to eventually be, but then this is easily extended).

Technically, 0::/4 and 1::/4 are not either, but I have seen those used internally.

We could go for ::/1, 8000::/1 plus 2000::/4 to cover the 2000::/3 case *and* all the address space available, no matter which weird networks are used... or be totally correct and remove the ::/3, to cover *only* the officially designated space.

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#33 lore raw

Re: [Openvpn-devel] [PATCH 09/10] Fix IPv6 host routes to LAN gateway on OpenSolaris

In reply to Gert Doering

Am 11.09.15 um 16:33 schrieb Gert Doering:

Quoted 7 lines

The tun/tap routes need to be set with "metric 0", while this will prevent routes to LAN gateways from being installed. So, set metric 0 only if no other interface is requested...

(Note: OpenSolaris can not specify host+interface gateways, so we just use the GW addresses - it seems to still work for fe80:: addresses, however it is done internally. NUD maybe?)

ACK. You could also copy the commit message to the comment in the code to make it more clear what is going on there.

Arne

#30 lore raw

Re: [Openvpn-devel] [PATCH 08/10] get_default_gateway_ipv6(): *BSD / MacOS / Solaris PF_ROUTE implementation

In reply to Gert Doering

Am 11.09.15 um 16:33 schrieb Gert Doering:

Quoted 5 lines

As for IPv4, a common implementation for all (supported) BSD families and Solaris. Supporting the latter requires separate implementations for IPv4 and IPv6, unfortunately, so it's quite a bit of duplicate code.

Quoted 7 lines

Further, extend add_route_ipv6() and delete_route_ipv6() to handle link-local gateway addresses that require "gateway + interface" in scoped notation ("fe80::1%em0").

Tested on FreeBSD 7.4/amd64, 9.3/sparc64, 10.0/amd64, NetBSD 5.1/amd64, OpenBSD 4.9/i386, MacOS X 10.5/32 and 10.10/64 and OpenSolaris 10.11/i386.

Code looks good and tested on OS X 10.10.5

Arne

#16 lore raw

Re: [Openvpn-devel] [PATCH 05/10] get_default_gateway_ipv6(): Linux / Netlink implementation.

In reply to Arne Schwabe

Hi,

thanks for your review. Some comments before I go committing :-)

Quoted 6 lines · Arne Schwabe
src/openvpn/route.c +3 -0
+    CLEAR(rtreq);
+    rtreq.nh.nlmsg_type = RTM_GETROUTE;
+    rtreq.nh.nlmsg_flags = NLM_F_REQUEST;			/* XXX */

There should an indication why this is XXX, some kind of comment.

Thanks. This is indeed a leftover. I copied that code from

http://www.virtualbox.org/svn/vbox/trunk/src/VBox/NetworkServices/NAT/rtmon_linux.c

which uses NLM_F_REQUEST | NLM_F_ROOT here, and the "XXX" was a reminder for myself to figure out which is the right thing to do.

netlink(7) documents this field as:

Standard flag bits in nlmsg_flags
----------------------------------------------------------
NLM_F_REQUEST Must be set on all request messages.
NLM_F_MULTI The message is part of a multipart mes-
sage terminated by NLMSG_DONE.
NLM_F_ACK Request for an acknowledgment on success.
NLM_F_ECHO Echo this request.

Additional flag bits for GET requests
--------------------------------------------------------------------
NLM_F_ROOT Return the complete table instead of a single entry.
NLM_F_MATCH Return all entries matching criteria passed in mes-
sage content. Not implemented yet.
NLM_F_ATOMIC Return an atomic snapshot of the table.
NLM_F_DUMP Convenience macro; equivalent to
(NLM_F_ROOT|NLM_F_MATCH).

so, for virtualbox use ("we want the full table"), NLM_F_ROOT is right, and for the way I tackle this ("just give me the matching route"), NLM_F_REQUEST *only* is the right thing.

I'll remove the /* XXX */ and point to netlink(7) for explanations.

[..]

Quoted 2 lines

src/openvpn/route.c get_default_gateway (struct route_gateway_info *rgi) · line 2742

Quoted 1 line
src/openvpn/route.c +1 -0
+	if (nh->nlmsg_type == NLMSG_DONE) { break; }

The coding style here is different ithan in other parts of OpenVPN.

As this is new 2.4-only code, and we want to change the coding style anyway, I've decided to add new code in the new style already *if* it is a completely new function (which this is). "In the middle of a function" code bits will be kept old-style.

I agree that this leads to somewhat inconsistent intermediate style, but it is less reformatting, and 2.4 is "near" (I hope... Steffan? :-) )

Quoted 11 lines

src/openvpn/route.c get_default_gateway (struct route_gateway_info *rgi) · line 2765

Quoted 8 lines
src/openvpn/route.c +8 -0
+
+	/* we're only looking for routes in the main table, as "we have
+	 * no IPv6" will lead to a lookup result in "Local" (::/0 reject)
+	 */
+	if (rtm->rtm_family != AF_INET6 ||
+	    rtm->rtm_table != RT_TABLE_MAIN)
+		{ continue; }		/* we're not interested */
+

We night want to revisit this with more complex routing scenarios. Android for example uses the main routing table only to jump more specific routing tables. Other system might do the same.

I expect I'll have to revisit get_route_gateway_ipv6() on all the platforms eventually - right now, it works for the "easy case". If someone uses multiple routing tables, it will need adjustments on Linux and *BSD.

OTOH, we currently don't do multiple routing tables at all - neither for ifconfig, nor for route addition, so this would need some serious thought how to do this properly (which route table to use for openvpn itself, which table to install tun routes into, etc.). I think there is quite interesting possibilities to make things work even more nice (like, roaming to new interfaces for the "outside" connection, while the tun just stays up, without having to adjust host routes). We're not there today yet, though.

So - on Android, that code is not exactly relevant (it will print what it finds, which I'm actually somewhat curious to see :-) - but the result will not be used after your #ifndef TARGET_ANDROID patch goes in) and for other platforms, I do keep that in mind and will tackle it when someone comes up with a use case I can look into.

Quoted 1 line

Aside from these comment, this gets an ACK from me.

Thanks for the review.

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#17 lore raw

[Openvpn-devel] [PATCH applied] Re: get_default_gateway_ipv6(): Linux / Netlink implementation.

In reply to Gert Doering

Patch has been applied to the master branch.

commit 3128abcfdd1eb293b10e4d0bfdb0805728538563

Author: Gert Doering
Date:   Fri Sep 11 17:33:42 2015 +0200
     get_default_gateway_ipv6(): Linux / Netlink implementation.

     Signed-off-by: Gert Doering <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: http://article.gmane.org/gmane.network.openvpn.devel/10091

--
kind regards,

Gert Doering

#22 lore raw

[Openvpn-devel] [PATCH applied] Re: Implement handling of overlapping IPv6 routes with IPv6 remote VPN server address

In reply to Gert Doering

Patch has been applied to the master branch, with the change Arne has proposed ("because it makes sense" :) ), making "bits" inside route_ipv6_match_host() unsigned and drop the "if (bits<0)" check.

commit 3ddb56433b1fa0f20565dfda13a647459c06251a

Author: Gert Doering
Date:   Fri Sep 11 17:33:43 2015 +0200
     Implement handling of overlapping IPv6 routes with IPv6 remote VPN server address

     Signed-off-by: Gert Doering <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: http://article.gmane.org/gmane.network.openvpn.devel/10089

--
kind regards,

Gert Doering

#28 lore raw

[Openvpn-devel] [PATCH applied] Re: Implement '--redirect-gateway ipv6'

In reply to Gert Doering

Patch has been applied to the master branch.

I'll prepare a patch for 2.3 (so "--redirect-gateway ipv6" will work on all "common" versions in the future - 2.3.x, 2.4.x, 3.x).

commit d227929b5db049ca6efbef9fb7d84be5e545b41d

Author: Gert Doering
Date:   Fri Sep 11 17:33:44 2015 +0200
     Implement '--redirect-gateway ipv6'

     Signed-off-by: Gert Doering <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: http://article.gmane.org/gmane.network.openvpn.devel/10086

--
kind regards,

Gert Doering

#31 lore raw

[Openvpn-devel] [PATCH applied] Re: get_default_gateway_ipv6(): *BSD / MacOS / Solaris PF_ROUTE implementation

In reply to Gert Doering

Patch has been applied to the master branch.

As for Linux, we'll revisit the "multiple routing tables" topic when a use case arrives (and we've built generic infrastructure to handle this)

commit 2ff366f78a2bf8d0a2744db9b59f7274b671a042

Author: Gert Doering
Date:   Fri Sep 11 17:33:45 2015 +0200
     get_default_gateway_ipv6(): *BSD / MacOS / Solaris PF_ROUTE implementation

     Signed-off-by: Gert Doering <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: http://article.gmane.org/gmane.network.openvpn.devel/10082

--
kind regards,

Gert Doering

#34 lore raw

[Openvpn-devel] [PATCH applied] Re: Fix IPv6 host routes to LAN gateway on OpenSolaris

In reply to Gert Doering

As per Arne's suggestion, I've rewritten the comment quite a bit (it already explained about "metric 0", but not about "it still
works on OpenSolaris"). Also removed the leftover "this has not been
tested" comment about route deletion on OpenSolaris (it has!).

No functional changes.

Patch has been applied to the master branch.

commit fa5697f022110f557710f709c9ac0a3420bb073c

Author: Gert Doering
Date:   Fri Sep 11 17:33:46 2015 +0200
     Fix IPv6 host routes to LAN gateway on OpenSolaris

     Signed-off-by: Gert Doering <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Message-Id: <[email protected]...>
     URL: http://article.gmane.org/gmane.network.openvpn.devel/10092

--
kind regards,

Gert Doering

#36 lore raw

PATCH 1/1 Add custom check for inet_pton()/inet_ntop() on MinGW/WIN32

In reply to Gert Doering

More recent MinGW versions have these functions (if compiling at _VISTA level or higher), but the normal AC_CHECK_FUNCS() check does not find them because the necessary header file is not #include'd and the libws2_32 not linked - and our compat functions are incompatible with the definitions in <ws2tcpip.h>, so compilation fails.

Fix with a custom AC_LINK_IFELSE()/AC_LANG_PROGRAM() construct.

---
 configure.ac | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
configure.ac +23 -1
index 0a8255c..2e651d8 100644
@@ -632,7 +632,7 @@ AC_SUBST([SOCKETS_LIBS])
 
 old_LIBS="${LIBS}"
 LIBS="${LIBS} ${SOCKETS_LIBS}"
-AC_CHECK_FUNCS([sendmsg recvmsg inet_ntop inet_pton])
+AC_CHECK_FUNCS([sendmsg recvmsg])
 # Windows use stdcall for winsock so we cannot auto detect these
 m4_define(
 	[SOCKET_FUNCS],
@@ -644,6 +644,27 @@ m4_define(
 	[setsockopt getsockopt getsockname poll]dnl
 )
 if test "${WIN32}" = "yes"; then
+# normal autoconf function checking does not find inet_ntop/inet_pton
+# because they need to include the actual header file and link ws2_32.dll
+	LIBS="${LIBS} -lws2_32"
+	AC_MSG_CHECKING([for MinGW inet_ntop()/inet_pton()])
+	AC_LINK_IFELSE(
+		[AC_LANG_PROGRAM(
+			[[
+#include <ws2tcpip.h>
+			]],
+			[[
+int r = (int) inet_ntop (0, NULL, NULL, 0);
+    r += inet_pton(AF_INET, NULL, NULL);
+return r;
+			]]
+		)],
+		[AC_MSG_RESULT([OK])
+		 AC_DEFINE([HAVE_INET_NTOP],[1],[MinGW inet_ntop])
+		 AC_DEFINE([HAVE_INET_PTON],[1],[MinGW inet_pton])
+		],
+		[AC_MSG_RESULT([not found])]
+	)
 	m4_foreach(
 		[F],
 		m4_split(SOCKET_FUNCS SOCKET_OPT_FUNCS),
@@ -651,6 +672,7 @@ if test "${WIN32}" = "yes"; then
 			AC_DEFINE([UF], [1], [Win32 builtin])
 	)
 else
+	AC_CHECK_FUNCS([inet_ntop inet_pton])
 	AC_CHECK_FUNCS(
 		SOCKET_FUNCS,
 		,
Signature · 1 line
2.4.6
#38 lore raw

[Openvpn-devel] [PATCH applied] Re: get_default_gateway_ipv6(): Win32 implementation using GetBestRoute2()

In reply to Gert Doering

Lazy-ACK rules have been applied ("nobody screamed for two weeks", the code is for a single platform, does not involve buffer management or crypto, and is trivial enough).

The "get default gateway" part has been tested by Samuli (mingw), Heiko (cygwin) and Lev (MSVC), the "... and install proper routes" part needs more real-world exposure...

Patch has been applied to the master branch.

commit 5fcd49336812053aa1503078c0ebb72a2737a6b8

Author: Gert Doering
Date:   Fri Sep 11 17:33:47 2015 +0200
     get_default_gateway_ipv6(): Win32 implementation using GetBestRoute2()

     Signed-off-by: Gert Doering <[email protected]...>
     Lazy-ACK-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: http://article.gmane.org/gmane.network.openvpn.devel/10085

--
kind regards,

Gert Doering

#37 lore raw

[Openvpn-devel] [PATCH applied] Re: Add custom check for inet_pton()/inet_ntop() on MinGW/WIN32

In reply to Gert Doering

Lazy-ACK rules have been applied ("nobody screamed for two weeks", and it got tested on various mingw versions plus cygwin).

Patch has been applied to the master branch.

commit f96baabc6cf10edddedda1819a27a6927f274d8e

Author: Gert Doering
Date:   Tue Sep 22 22:31:24 2015 +0200
     Add custom check for inet_pton()/inet_ntop() on MinGW/WIN32

     Signed-off-by: Gert Doering <[email protected]...>
     Lazy-ACK-by: Gert Doering <[email protected]...>
     Tested-by: Heiko Hund <[email protected]...>
     Tested-by: Samuli Seppänen <[email protected]...>
     Message-Id: <[email protected]...>
     URL: http://article.gmane.org/gmane.network.openvpn.devel/10165

--
kind regards,

Gert Doering