[Openvpn-devel,v8] route: use the net_ API for routes on FreeBSD and DragonFly

Message ID 20261203161057.11303-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v8] route: use the net_ API for routes on FreeBSD and DragonFly |

Commit Message

Gert Doering Dec. 3, 2026, 4:10 p.m. UTC
  From: Razvan Cojocaru <razvanc@mailbox.org>

Add and delete IPv4 and IPv6 routes through net_route_v4/v6_add/del()
on FreeBSD and DragonFly, as on Linux, instead of running route(8)
from route.c. Build networking_freebsd.c for DragonFly as well; -fib
is only passed on FreeBSD, where --route-table is now supported.

A non-contiguous IPv4 netmask now gets an error instead of an invalid
prefix length. networking_freebsd.c logs route commands at D_ROUTE, as
route.c did. The shared error messages lose their "Linux" prefix.

Change-Id: I601454a511c9aac4f37d8629b1692266cdbd6885
Signed-off-by: Razvan Cojocaru <razvanc@mailbox.org>
Acked-by: Antonio Quartulli <antonio@mandelbit.com>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2154
---

This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2154
This mail reflects revision 8 of this Change.

Acked-by according to Gerrit (reflected above):
Antonio Quartulli <antonio@mandelbit.com>
  

Patch

diff --git a/doc/man-sections/vpn-network-options.rst b/doc/man-sections/vpn-network-options.rst
index 960c6ad..f282982 100644
--- a/doc/man-sections/vpn-network-options.rst
+++ b/doc/man-sections/vpn-network-options.rst
@@ -399,7 +399,10 @@ 
   table of the operating system, but with this option,
   a user defined routing table can be used instead.
 
-  (Supported on Linux only, on other platforms this is a no-op).
+  On FreeBSD, the table id is the FIB number. Without this option, routes
+  go to the FIB OpenVPN runs in (see :code:`setfib(1)`).
+
+  (Supported on Linux and FreeBSD only, on other platforms this is a no-op).
 
 --route args
   Add route to routing table after connection is established. Multiple
diff --git a/src/openvpn/networking.h b/src/openvpn/networking.h
index f42ef4b..0cb5565 100644
--- a/src/openvpn/networking.h
+++ b/src/openvpn/networking.h
@@ -33,7 +33,7 @@ 
 #include "networking_sitnl.h"
 #elif ENABLE_IPROUTE
 #include "networking_iproute2.h"
-#elif defined(TARGET_FREEBSD)
+#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
 typedef void *openvpn_net_ctx_t;
 typedef char openvpn_net_iface_t;
 #else  /* ifdef ENABLE_SITNL */
@@ -239,7 +239,8 @@ 
 
 #endif /* ENABLE_SITNL || ENABLE_IPROUTE */
 
-#if defined(ENABLE_SITNL) || defined(ENABLE_IPROUTE) || defined(TARGET_FREEBSD)
+#if defined(ENABLE_SITNL) || defined(ENABLE_IPROUTE) || defined(TARGET_FREEBSD) \
+    || defined(TARGET_DRAGONFLY)
 /**
  * Add a route for an IPv4 address/network
  *
@@ -308,7 +309,7 @@ 
                      const struct in6_addr *gw, const openvpn_net_iface_t *iface, int table,
                      int metric);
 
-#endif /* ENABLE_SITNL || ENABLE_IPROUTE || TARGET_FREEBSD */
+#endif /* ENABLE_SITNL || ENABLE_IPROUTE || TARGET_FREEBSD || TARGET_DRAGONFLY */
 
 #if defined(ENABLE_SITNL) || defined(ENABLE_IPROUTE)
 
diff --git a/src/openvpn/networking_freebsd.c b/src/openvpn/networking_freebsd.c
index abcc026..1727bb5 100644
--- a/src/openvpn/networking_freebsd.c
+++ b/src/openvpn/networking_freebsd.c
@@ -6,7 +6,7 @@ 
 #include "run_command.h"
 #include "networking.h"
 
-#if defined(TARGET_FREEBSD)
+#if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
 
 static int
 net_route_v4(const char *op, const in_addr_t *dst, int prefixlen, const in_addr_t *gw,
@@ -33,14 +33,16 @@ 
     {
         argv_printf_cat(&argv, "-iface %s", iface);
     }
+#if defined(TARGET_FREEBSD) /* DragonFly has no multiple FIB support */
     /* without -fib, route(8) uses the FIB of the process */
     if (table >= 0)
     {
         argv_printf_cat(&argv, "-fib %d", table);
     }
+#endif
 
-    argv_msg(M_INFO, &argv);
-    bool status = openvpn_execve_check(&argv, NULL, 0, "ERROR: FreeBSD route command failed");
+    argv_msg(D_ROUTE, &argv);
+    const bool status = openvpn_execve_check(&argv, NULL, 0, "ERROR: route command failed");
 
     argv_free(&argv);
 
@@ -75,13 +77,15 @@ 
     {
         argv_printf_cat(&argv, "-iface %s", iface);
     }
+#if defined(TARGET_FREEBSD) /* DragonFly has no multiple FIB support */
     if (table >= 0)
     {
         argv_printf_cat(&argv, "-fib %d", table);
     }
+#endif
 
-    argv_msg(M_INFO, &argv);
-    bool status = openvpn_execve_check(&argv, NULL, 0, "ERROR: FreeBSD route command failed");
+    argv_msg(D_ROUTE, &argv);
+    const bool status = openvpn_execve_check(&argv, NULL, 0, "ERROR: route command failed");
 
     argv_free(&argv);
 
@@ -116,4 +120,4 @@ 
     return net_route_v6("delete", dst, prefixlen, gw, iface, table, metric);
 }
 
-#endif /* if defined(TARGET_FREEBSD) */
+#endif /* if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) */
diff --git a/src/openvpn/options.c b/src/openvpn/options.c
index e51ece7..adff841 100644
--- a/src/openvpn/options.c
+++ b/src/openvpn/options.c
@@ -226,7 +226,7 @@ 
     "                    pass --ifconfig parms by environment to scripts.\n"
     "--ifconfig-nowarn : Don't warn if the --ifconfig option on this side of the\n"
     "                    connection doesn't match the remote side.\n"
-#ifdef TARGET_LINUX
+#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
     "--route-table table_id : Specify a custom routing table for use with --route(-ipv6).\n"
     "                           If not specified, the id of the default routing table will be used.\n"
 #endif
@@ -5293,8 +5293,8 @@ 
     }
     else if (streq(p[0], "route-table") && p[1] && !p[2])
     {
-#ifndef ENABLE_SITNL
-        msg(M_WARN, "NOTE: --route-table is supported only on Linux when SITNL is built-in");
+#if !defined(ENABLE_SITNL) && !defined(TARGET_FREEBSD)
+        msg(M_WARN, "NOTE: --route-table is supported only on FreeBSD, and on Linux when SITNL is built-in");
 #endif
         VERIFY_PERMISSION(OPT_P_ROUTE_TABLE);
         if (!atoi_constrained(p[1], &options->route_default_table_id, p[0], 0, INT_MAX,
diff --git a/src/openvpn/route.c b/src/openvpn/route.c
index 82cb7f0..8b5f968 100644
--- a/src/openvpn/route.c
+++ b/src/openvpn/route.c
@@ -103,6 +103,11 @@ 
 #define RTA_SUCCESS 1 /* route addition succeeded */
 #define RTA_EEXIST  2 /* route not added as it already exists */
 
+/* Platforms that add and delete routes through the net_ API (networking.h) */
+#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
+#define ROUTE_NET_API
+#endif
+
 #ifndef TARGET_ANDROID
 static bool
 add_bypass_address(struct route_bypass *rb, const in_addr_t a)
@@ -1433,7 +1438,7 @@ 
 
 /* Return true if the "on-link" form of the route should be used.  This is when the gateway for
  * a route is specified as an interface rather than an address. */
-#if defined(TARGET_LINUX) || defined(_WIN32) || defined(TARGET_DARWIN)
+#if defined(ROUTE_NET_API) || defined(_WIN32) || defined(TARGET_DARWIN)
 static inline bool
 is_on_link(const struct route_ipv4 *r, const unsigned int flags,
            const struct route_gateway_info *rgi)
@@ -1459,7 +1464,7 @@ 
     struct argv argv = argv_new();
     struct gc_arena gc = gc_new();
 
-#if !defined(TARGET_LINUX)
+#if !defined(ROUTE_NET_API)
     const char *network = print_in_addr_t(r->network, 0, &gc);
 #if !defined(TARGET_AIX)
     const char *netmask = print_in_addr_t(r->netmask, 0, &gc);
@@ -1468,7 +1473,7 @@ 
 #endif
 
 
-#if defined(TARGET_LINUX)
+#if defined(ROUTE_NET_API)
     const char *iface = NULL;
     int metric = -1;
 
@@ -1482,20 +1487,29 @@ 
         metric = r->metric;
     }
 
-
-    status = RTA_SUCCESS;
-    int ret = net_route_v4_add(ctx, &r->network, netmask_to_netbits2(r->netmask), &r->gateway,
-                               iface, r->table_id, metric);
-    if (ret == -EEXIST)
+    const int netbits = netmask_to_netbits2(r->netmask);
+    if (netbits < 0)
     {
-        msg(D_ROUTE, "NOTE: Linux route add command failed because route exists");
-        status = RTA_EEXIST;
-    }
-    else if (ret < 0)
-    {
-        msg(M_WARN, "ERROR: Linux route add command failed");
+        msg(M_WARN, "ERROR: cannot add route with non-contiguous netmask %s",
+            print_in_addr_t(r->netmask, 0, &gc));
         status = RTA_ERROR;
     }
+    else
+    {
+        status = RTA_SUCCESS;
+        const int ret = net_route_v4_add(ctx, &r->network, netbits, &r->gateway, iface,
+                                         r->table_id, metric);
+        if (ret == -EEXIST)
+        {
+            msg(D_ROUTE, "NOTE: route add command failed because route exists");
+            status = RTA_EEXIST;
+        }
+        else if (ret < 0)
+        {
+            msg(M_WARN, "ERROR: route add command failed");
+            status = RTA_ERROR;
+        }
+    }
 
 #elif defined(TARGET_ANDROID)
     char out[128];
@@ -1597,44 +1611,6 @@ 
     bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: Solaris route add command failed");
     status = ret ? RTA_SUCCESS : RTA_ERROR;
 
-#elif defined(TARGET_FREEBSD)
-
-    argv_printf(&argv, "%s add", ROUTE_PATH);
-
-#if 0
-    if (r->flags & RT_METRIC_DEFINED)
-    {
-        argv_printf_cat(&argv, "-rtt %d", r->metric);
-    }
-#endif
-
-    argv_printf_cat(&argv, "-net %s %s %s", network, gateway, netmask);
-
-    /* FIXME -- add on-link support for FreeBSD */
-
-    argv_msg(D_ROUTE, &argv);
-    bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: FreeBSD route add command failed");
-    status = ret ? RTA_SUCCESS : RTA_ERROR;
-
-#elif defined(TARGET_DRAGONFLY)
-
-    argv_printf(&argv, "%s add", ROUTE_PATH);
-
-#if 0
-    if (r->flags & RT_METRIC_DEFINED)
-    {
-        argv_printf_cat(&argv, "-rtt %d", r->metric);
-    }
-#endif
-
-    argv_printf_cat(&argv, "-net %s %s %s", network, gateway, netmask);
-
-    /* FIXME -- add on-link support for Dragonfly */
-
-    argv_msg(D_ROUTE, &argv);
-    bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: DragonFly route add command failed");
-    status = ret ? RTA_SUCCESS : RTA_ERROR;
-
 #elif defined(TARGET_DARWIN)
 
     argv_printf(&argv, "%s add", ROUTE_PATH);
@@ -1700,10 +1676,10 @@ 
     bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: Haiku inet route add command failed");
     status = ret ? RTA_SUCCESS : RTA_ERROR;
 
-#else  /* if defined(TARGET_LINUX) */
+#else  /* if defined(ROUTE_NET_API) */
     msg(M_FATAL,
         "Sorry, but I don't know how to do 'route' commands on this operating system.  Try putting your routes in a --route-up script");
-#endif /* if defined(TARGET_LINUX) */
+#endif /* if defined(ROUTE_NET_API) */
 
     if (status == RTA_SUCCESS)
     {
@@ -1777,8 +1753,7 @@ 
     const char *network = print_in6_addr(r6->network, 0, &gc);
     const char *gateway = print_in6_addr(r6->gateway, 0, &gc);
 
-#if defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) \
-    || defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
+#if defined(TARGET_DARWIN) || 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
@@ -1830,7 +1805,7 @@ 
         goto done;
     }
 
-#if defined(TARGET_LINUX)
+#if defined(ROUTE_NET_API)
     int metric = -1;
     if ((r6->flags & RT_METRIC_DEFINED) && (r6->metric > 0))
     {
@@ -1842,12 +1817,12 @@ 
                                device, r6->table_id, metric);
     if (ret == -EEXIST)
     {
-        msg(D_ROUTE, "NOTE: Linux route add command failed because route exists");
+        msg(D_ROUTE, "NOTE: route add command failed because route exists");
         status = RTA_EEXIST;
     }
     else if (ret < 0)
     {
-        msg(M_WARN, "ERROR: Linux route add command failed");
+        msg(M_WARN, "ERROR: route add command failed");
         status = RTA_ERROR;
     }
 
@@ -1893,23 +1868,6 @@ 
     bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: Solaris route add -inet6 command failed");
     status = ret ? RTA_SUCCESS : RTA_ERROR;
 
-#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
-
-    argv_printf(&argv, "%s add -inet6 %s/%d", ROUTE_PATH, network, r6->netbits);
-
-    if (gateway_needed)
-    {
-        argv_printf_cat(&argv, "%s", gateway);
-    }
-    else
-    {
-        argv_printf_cat(&argv, "-iface %s", device);
-    }
-
-    argv_msg(D_ROUTE, &argv);
-    bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: *BSD route add -inet6 command failed");
-    status = ret ? RTA_SUCCESS : RTA_ERROR;
-
 #elif defined(TARGET_DARWIN)
 
     argv_printf(&argv, "%s add -inet6 %s -prefixlen %d", ROUTE_PATH, network, r6->netbits);
@@ -1960,10 +1918,10 @@ 
     bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: Haiku inet6 route add command failed");
     status = ret ? RTA_SUCCESS : RTA_ERROR;
 
-#else  /* if defined(TARGET_LINUX) */
+#else  /* if defined(ROUTE_NET_API) */
     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 /* if defined(TARGET_LINUX) */
+#endif /* if defined(ROUTE_NET_API) */
 
 done:
     if (status == RTA_SUCCESS)
@@ -1986,13 +1944,13 @@ 
 delete_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags,
              const struct route_gateway_info *rgi, const struct env_set *es, openvpn_net_ctx_t *ctx)
 {
-#if !defined(TARGET_LINUX)
+#if !defined(ROUTE_NET_API)
     const char *network;
 #if !defined(TARGET_AIX)
     const char *netmask;
 #endif
     const char *gateway;
-#else /* if !defined(TARGET_LINUX) */
+#else /* if !defined(ROUTE_NET_API) */
     int metric;
 #endif
 
@@ -2004,7 +1962,7 @@ 
     struct gc_arena gc = gc_new();
     struct argv argv = argv_new();
 
-#if !defined(TARGET_LINUX)
+#if !defined(ROUTE_NET_API)
     network = print_in_addr_t(r->network, 0, &gc);
 #if !defined(TARGET_AIX)
     netmask = print_in_addr_t(r->netmask, 0, &gc);
@@ -2013,7 +1971,7 @@ 
 #endif
 
 
-#if defined(TARGET_LINUX)
+#if defined(ROUTE_NET_API)
     metric = -1;
     if (r->flags & RT_METRIC_DEFINED)
     {
@@ -2024,7 +1982,7 @@ 
                          r->table_id, metric)
         < 0)
     {
-        msg(M_WARN, "ERROR: Linux route delete command failed");
+        msg(M_WARN, "ERROR: route delete command failed");
     }
 #elif defined(_WIN32)
 
@@ -2074,20 +2032,6 @@ 
     argv_msg(D_ROUTE, &argv);
     openvpn_execve_check(&argv, es, 0, "ERROR: Solaris route delete command failed");
 
-#elif defined(TARGET_FREEBSD)
-
-    argv_printf(&argv, "%s delete -net %s %s %s", ROUTE_PATH, network, gateway, netmask);
-
-    argv_msg(D_ROUTE, &argv);
-    openvpn_execve_check(&argv, es, 0, "ERROR: FreeBSD route delete command failed");
-
-#elif defined(TARGET_DRAGONFLY)
-
-    argv_printf(&argv, "%s delete -net %s %s %s", ROUTE_PATH, network, gateway, netmask);
-
-    argv_msg(D_ROUTE, &argv);
-    openvpn_execve_check(&argv, es, 0, "ERROR: DragonFly route delete command failed");
-
 #elif defined(TARGET_DARWIN)
 
     if (is_on_link(r, flags, rgi))
@@ -2135,10 +2079,10 @@ 
     argv_msg(D_ROUTE, &argv);
     openvpn_execve_check(&argv, es, 0, "ERROR: Haiku inet route delete command failed");
 
-#else  /* if defined(TARGET_LINUX) */
+#else  /* if defined(ROUTE_NET_API) */
     msg(M_FATAL,
         "Sorry, but I don't know how to do 'route' commands on this operating system.  Try putting your routes in a --route-up script");
-#endif /* if defined(TARGET_LINUX) */
+#endif /* if defined(ROUTE_NET_API) */
 
     r->flags &= ~RT_ADDED;
     argv_free(&argv);
@@ -2159,7 +2103,7 @@ 
     }
 
 #if !defined(_WIN32)
-#if !defined(TARGET_LINUX)
+#if !defined(ROUTE_NET_API)
     const char *gateway;
 #endif
 #if !defined(TARGET_SOLARIS)
@@ -2186,12 +2130,11 @@ 
     struct argv argv = argv_new();
 
     network = print_in6_addr(r6->network, 0, &gc);
-#if !defined(TARGET_LINUX) && !defined(_WIN32)
+#if !defined(ROUTE_NET_API) && !defined(_WIN32)
     gateway = print_in6_addr(r6->gateway, 0, &gc);
 #endif
 
-#if defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) \
-    || defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
+#if defined(TARGET_DARWIN) || 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
@@ -2209,7 +2152,7 @@ 
 
     msg(D_ROUTE, "delete_route_ipv6(%s/%u)", network, r6->netbits);
 
-#if defined(TARGET_LINUX)
+#if defined(ROUTE_NET_API)
     int metric = -1;
     if ((r6->flags & RT_METRIC_DEFINED) && (r6->metric > 0))
     {
@@ -2220,7 +2163,7 @@ 
                          device, r6->table_id, metric)
         < 0)
     {
-        msg(M_WARN, "ERROR: Linux route v6 delete command failed");
+        msg(M_WARN, "ERROR: route v6 delete command failed");
     }
 
 #elif defined(_WIN32)
@@ -2242,22 +2185,6 @@ 
     argv_msg(D_ROUTE, &argv);
     openvpn_execve_check(&argv, es, 0, "ERROR: Solaris route delete -inet6 command failed");
 
-#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
-
-    argv_printf(&argv, "%s delete -inet6 %s/%d", ROUTE_PATH, network, r6->netbits);
-
-    if (gateway_needed)
-    {
-        argv_printf_cat(&argv, "%s", gateway);
-    }
-    else
-    {
-        argv_printf_cat(&argv, "-iface %s", device);
-    }
-
-    argv_msg(D_ROUTE, &argv);
-    openvpn_execve_check(&argv, es, 0, "ERROR: *BSD route delete -inet6 command failed");
-
 #elif defined(TARGET_DARWIN)
 
     argv_printf(&argv, "%s delete -inet6 %s -prefixlen %d", ROUTE_PATH, network, r6->netbits);
@@ -2310,10 +2237,10 @@ 
     argv_msg(D_ROUTE, &argv);
     openvpn_execve_check(&argv, es, 0, "ERROR: Haiku inet6 route delete command failed");
 
-#else  /* if defined(TARGET_LINUX) */
+#else  /* if defined(ROUTE_NET_API) */
     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-down script");
-#endif /* if defined(TARGET_LINUX) */
+#endif /* if defined(ROUTE_NET_API) */
 
     argv_free(&argv);
     gc_free(&gc);