[Openvpn-devel,v6] networking_freebsd: fix route command and return value details

Message ID 20261121174810.12250-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v6] networking_freebsd: fix route command and return value details |

Commit Message

Gert Doering Nov. 21, 2026, 5:48 p.m. UTC
  From: Razvan Cojocaru <razvanc@mailbox.org>

- Return -1 on failure, as networking.h documents, instead of 1.
- Add the interface as scope to link-local IPv6 gateways.
- Use "delete" and "-inet6", as route.c does. DragonFly's route(8)
  has no "del" or "-6".

All current callers (DCO iroutes) ignore the return value.

Change-Id: I3e6043178935fdd05e6ea78c411dac5decb7ac64
Signed-off-by: Razvan Cojocaru <razvanc@mailbox.org>
Acked-by: Gert Doering <gert@greenie.muc.de>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2153
---

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/+/2153
This mail reflects revision 6 of this Change.

Acked-by according to Gerrit (reflected above):
Gert Doering <gert@greenie.muc.de>
  

Patch

diff --git a/src/openvpn/networking_freebsd.c b/src/openvpn/networking_freebsd.c
index 8ba6326..abcc026 100644
--- a/src/openvpn/networking_freebsd.c
+++ b/src/openvpn/networking_freebsd.c
@@ -12,30 +12,26 @@ 
 net_route_v4(const char *op, const in_addr_t *dst, int prefixlen, const in_addr_t *gw,
              const char *iface, int table, int metric)
 {
-    char buf1[INET_ADDRSTRLEN], buf2[INET_ADDRSTRLEN];
-    in_addr_t _dst, _gw;
+    char buf[INET_ADDRSTRLEN];
+    in_addr_t _dst = ntohl(*dst);
     struct argv argv = argv_new();
 
     ASSERT(gw || iface);
 
-    _dst = ntohl(*dst);
-
     /* if we have a gateway (GW != NULL) install route to gateway IP
      * if not, install "connected" route to interface
      * (needed to make 'ifconfig-push IPs outside server subnet' work)
      */
+    argv_printf(&argv, "%s %s -net %s/%d", ROUTE_PATH, op,
+                inet_ntop(AF_INET, &_dst, buf, sizeof(buf)), prefixlen);
     if (gw)
     {
-        _gw = ntohl(*gw);
-        argv_printf(&argv, "%s %s -net %s/%d %s", ROUTE_PATH, op,
-                    inet_ntop(AF_INET, &_dst, buf1, sizeof(buf1)), prefixlen,
-                    inet_ntop(AF_INET, &_gw, buf2, sizeof(buf2)));
+        in_addr_t _gw = ntohl(*gw);
+        argv_printf_cat(&argv, "%s", inet_ntop(AF_INET, &_gw, buf, sizeof(buf)));
     }
     else
     {
-        argv_printf(&argv, "%s %s -net %s/%d -iface %s", ROUTE_PATH, op,
-                    inet_ntop(AF_INET, &_dst, buf1, sizeof(buf1)), prefixlen,
-                    iface);
+        argv_printf_cat(&argv, "-iface %s", iface);
     }
     /* without -fib, route(8) uses the FIB of the process */
     if (table >= 0)
@@ -48,14 +44,14 @@ 
 
     argv_free(&argv);
 
-    return (!status);
+    return status ? 0 : -1;
 }
 
 static int
 net_route_v6(const char *op, const struct in6_addr *dst, int prefixlen, const struct in6_addr *gw,
              const char *iface, int table, int metric)
 {
-    char buf1[INET6_ADDRSTRLEN], buf2[INET6_ADDRSTRLEN];
+    char buf[INET6_ADDRSTRLEN];
     struct argv argv = argv_new();
 
     ASSERT(gw || iface);
@@ -64,30 +60,32 @@ 
      * if not, install "connected" route to interface
      * (needed to make 'ifconfig-push IPs outside server subnet' work)
      */
-    if (gw)
+    argv_printf(&argv, "%s %s -inet6 %s/%d", ROUTE_PATH, op,
+                inet_ntop(AF_INET6, dst, buf, sizeof(buf)), prefixlen);
+    if (gw && iface && IN6_IS_ADDR_LINKLOCAL(gw))
     {
-        argv_printf(&argv, "%s -6 %s -net %s/%d %s", ROUTE_PATH, op,
-                    inet_ntop(AF_INET6, dst, buf1, sizeof(buf1)), prefixlen,
-                    inet_ntop(AF_INET6, gw, buf2, sizeof(buf2)));
+        /* a link-local gateway needs the interface as scope */
+        argv_printf_cat(&argv, "%s%%%s", inet_ntop(AF_INET6, gw, buf, sizeof(buf)), iface);
+    }
+    else if (gw)
+    {
+        argv_printf_cat(&argv, "%s", inet_ntop(AF_INET6, gw, buf, sizeof(buf)));
     }
     else
     {
-        argv_printf(&argv, "%s -6 %s -net %s/%d -iface %s", ROUTE_PATH, op,
-                    inet_ntop(AF_INET6, dst, buf1, sizeof(buf1)), prefixlen,
-                    iface);
+        argv_printf_cat(&argv, "-iface %s", iface);
     }
     if (table >= 0)
     {
         argv_printf_cat(&argv, "-fib %d", table);
     }
 
-
     argv_msg(M_INFO, &argv);
     bool status = openvpn_execve_check(&argv, NULL, 0, "ERROR: FreeBSD route command failed");
 
     argv_free(&argv);
 
-    return (!status);
+    return status ? 0 : -1;
 }
 
 int
@@ -108,14 +106,14 @@ 
 net_route_v4_del(const openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen, const in_addr_t *gw,
                  const char *iface, int table, int metric)
 {
-    return net_route_v4("del", dst, prefixlen, gw, iface, table, metric);
+    return net_route_v4("delete", dst, prefixlen, gw, iface, table, metric);
 }
 
 int
 net_route_v6_del(const openvpn_net_ctx_t *ctx, const struct in6_addr *dst, int prefixlen,
                  const struct in6_addr *gw, const char *iface, int table, int metric)
 {
-    return net_route_v6("del", dst, prefixlen, gw, iface, table, metric);
+    return net_route_v6("delete", dst, prefixlen, gw, iface, table, metric);
 }
 
 #endif /* if defined(TARGET_FREEBSD) */