From: Frank Lichtenheld Date: Fri, 24 Jul 2026 22:08:32 +0000 (+0200) Subject: lladdr: Clean up code and BSD support X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1e918232e20c4d09b4e0dcdbf91fbb5d5cb9cb23;p=thirdparty%2Fopenvpn.git lladdr: Clean up code and BSD support cppcheck complained about the creation of a useless variable on Windows. With the old code this was not fixable in a good way. So rewrite the whole code to hopefully be much more readable. While testing this we also found out that the old code was not really working on Solaris, so disable support for that. Gert Doering contributed support for NetBSD. Github: closes OpenVPN/openvpn#1034 Change-Id: Ibe7b3c17176c97a1cbb4d9cc87735b05e62c4f43 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1675 Message-Id: <20260724220839.26402-1-gert@greenie.muc.de> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg37863.html Signed-off-by: Gert Doering --- diff --git a/src/openvpn/lladdr.c b/src/openvpn/lladdr.c index d8bcad95f..195245f1e 100644 --- a/src/openvpn/lladdr.c +++ b/src/openvpn/lladdr.c @@ -13,42 +13,55 @@ #include "lladdr.h" #include "proto.h" +#ifdef TARGET_LINUX +static int +set_lladdr_linux(openvpn_net_ctx_t *ctx, const char *ifname, const char *lladdr) +{ + uint8_t addr[OPENVPN_ETH_ALEN]; + + sscanf(lladdr, MAC_FMT, MAC_SCAN_ARG(addr)); + return (net_addr_ll_set(ctx, ifname, addr) == 0); +} +#else /* TARGET_LINUX */ + +#if defined(TARGET_OPENBSD) || defined(TARGET_FREEBSD) || defined(TARGET_DARWIN) +#define IFCONFIG_LLADDR_FMT "%s %s lladdr %s" +#elif defined(TARGET_NETBSD) +#define IFCONFIG_LLADDR_FMT "%s %s link %s active" +#endif +static int +set_lladdr_ifconfig(const char *ifname, const char *lladdr, const struct env_set *es) +{ +#ifdef IFCONFIG_LLADDR_FMT + struct argv argv = argv_new(); + argv_printf(&argv, IFCONFIG_LLADDR_FMT, IFCONFIG_PATH, ifname, lladdr); + argv_msg(M_INFO, &argv); + int r = openvpn_execve_check(&argv, es, M_WARN, "ERROR: Unable to set link layer address."); + argv_free(&argv); + return r; +#else + msg(M_WARN, + "Sorry, but I don't know how to configure link layer addresses on this operating system."); + return -1; +#endif +} +#endif /* TARGET_LINUX */ + int set_lladdr(openvpn_net_ctx_t *ctx, const char *ifname, const char *lladdr, const struct env_set *es) { - int r; - if (!ifname || !lladdr) { return -1; } #if defined(TARGET_LINUX) - uint8_t addr[OPENVPN_ETH_ALEN]; - - sscanf(lladdr, MAC_FMT, MAC_SCAN_ARG(addr)); - r = (net_addr_ll_set(ctx, ifname, addr) == 0); -#else /* if defined(TARGET_LINUX) */ - struct argv argv = argv_new(); -#if defined(TARGET_SOLARIS) - argv_printf(&argv, "%s %s ether %s", IFCONFIG_PATH, ifname, lladdr); -#elif defined(TARGET_OPENBSD) - argv_printf(&argv, "%s %s lladdr %s", IFCONFIG_PATH, ifname, lladdr); -#elif defined(TARGET_DARWIN) - argv_printf(&argv, "%s %s lladdr %s", IFCONFIG_PATH, ifname, lladdr); -#elif defined(TARGET_FREEBSD) - argv_printf(&argv, "%s %s ether %s", IFCONFIG_PATH, ifname, lladdr); -#else /* if defined(TARGET_SOLARIS) */ - msg(M_WARN, - "Sorry, but I don't know how to configure link layer addresses on this operating system."); - return -1; -#endif /* if defined(TARGET_SOLARIS) */ - argv_msg(M_INFO, &argv); - r = openvpn_execve_check(&argv, es, M_WARN, "ERROR: Unable to set link layer address."); - argv_free(&argv); -#endif /* if defined(TARGET_LINUX) */ + int r = set_lladdr_linux(ctx, ifname, lladdr); +#else + int r = set_lladdr_ifconfig(ifname, lladdr, es); +#endif - if (r) + if (r > 0) { msg(M_INFO, "TUN/TAP link layer address set to %s", lladdr); }