networking: add and implement net_addr_ll_set() API
PATCH 1/2 networking: add and implement net_addr_ll_set() API
When running in TAP mode we may need to set the LL address of the interface, if requested by the user.
This operation was overlooked when implementing the networking API and it still relies on iproute/net-tools being installed.
Basically this means that when compiling OpenVPN on a system without iproute2/net-tools and the user uses the "lladdr" config directive, OpenVPN will fail to se the LL address of the interface.
With this patch a new API is introduced and it is implemented for both SITNL and iproute2 backends.
--- src/openvpn/misc.h | 7 +++++++ src/openvpn/networking.h | 12 +++++++++++ src/openvpn/networking_iproute2.c | 21 +++++++++++++++++++ src/openvpn/networking_sitnl.c | 35 +++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+)
src/openvpn/misc.h +7 -0
@@ -211,4 +211,11 @@ get_num_elements(const char *string, char delimiter);struct buffer prepend_dir (const char *dir,const char *path,struct gc_arena *gc); +#define _STRINGIFY (S ) #S +#define MAC_FMT _STRINGIFY(%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx) +#define MAC_PRINT_ARG (_mac) _mac[0], _mac[1], _mac[2], \ + _mac[3], _mac[4], _mac[5] +#define MAC_SCAN_ARG (_mac) &_mac[0], &_mac[1], &_mac[2], \ + &_mac[3], &_mac[4], &_mac[5] +#endif /* ifndef MISC_H */
src/openvpn/networking.h +12 -0
@@ -103,6 +103,18 @@ int net_iface_up(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,int net_iface_mtu_set (openvpn_net_ctx_t *ctx,const openvpn_net_iface_t *iface,uint32_t mtu); +/** +* Set the Link Layer (Ethernet) address of the TAP interface +* +* @param ctx the implementation specific context +* @param iface the interface to modify +* @param addr the new address to set (expected ETH_ALEN bytes (6)) +* +* @return 0 on success, a negative error code otherwise +*/ +int net_addr_ll_set (openvpn_net_ctx_t *ctx,const openvpn_net_iface_t *iface, +uint8_t *addr); +/** * Add an IPv4 address to an interface *
src/openvpn/networking_iproute2.c +21 -0
@@ -93,6 +93,27 @@ net_iface_mtu_set(openvpn_net_ctx_t *ctx, const char *iface, uint32_t mtu)return 0 ; } +int +net_addr_ll_set (openvpn_net_ctx_t *ctx,const openvpn_net_iface_t *iface, +uint8_t *addr) +{ +struct argv argv =argv_new (); +int ret =0 ; + +argv_printf (&argv, +"%s link set addr " MAC_FMT " dev %s" , + iproute_path,MAC_PRINT_ARG (addr), iface); + +argv_msg (M_INFO , &argv); +if (!openvpn_execve_check (&argv, ctx->es,M_WARN , +"Linux ip link set addr failed" )) + ret =-1 ; + +argv_free (&argv); + +return ret; +} +int net_addr_v4_add (openvpn_net_ctx_t *ctx,const char *iface,const in_addr_t *addr,int prefixlen)
src/openvpn/networking_sitnl.c +35 -0
@@ -30,6 +30,7 @@#include "errlevel.h" #include "buffer.h" +#include "misc.h" #include "networking.h" #include <errno.h>
@@ -723,6 +724,40 @@ err:return ret; } +int +net_addr_ll_set (openvpn_net_ctx_t *ctx,const openvpn_net_iface_t *iface, +uint8_t *addr) +{ +struct sitnl_link_req req; +int ifindex, ret =-1 ; + +CLEAR (req); + + ifindex =if_nametoindex (iface); +if (ifindex ==0 ) + { +msg (M_WARN |M_ERRNO ,"%s: rtnl: cannot get ifindex for %s" , __func__, + iface); +return -1 ; + } + + req.n.nlmsg_len =NLMSG_LENGTH (sizeof (req.i)); + req.n.nlmsg_flags =NLM_F_REQUEST ; + req.n.nlmsg_type =RTM_NEWLINK ; + + req.i.ifi_family =AF_PACKET ; + req.i.ifi_index = ifindex; + +SITNL_ADDATTR (&req.n,sizeof (req),IFLA_ADDRESS , addr,ETH_ALEN ); + +msg (M_INFO ,"%s: lladdr " MAC_FMT " for %s" , __func__,MAC_PRINT_ARG (addr), + iface); + + ret =sitnl_send (&req.n,0 ,0 ,NULL ,NULL ); +err: +return ret; +} + static int sitnl_addr_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family, const inet_address_t *local, const inet_address_t *remote,
Signature · 1 line
2.32.0
PATCH 2/2 set_lladdr: use networking API net_addr_ll_set() on Linux
Make sure that set_addr() uses the proper networking backend when setting the LL address of a TAP interface.
This operation was overlooked while implementing the networking APIs on the Linux platform.
---
This patch (along with 1/2) has been tested on buildbots, but I couldn't run any specific test on any *BSD or Windows platform.
Linux with both iproute2 and sitnl works as expected.
src/openvpn/init.c | 5 +++-- src/openvpn/lladdr.c | 30 +++++++++++++----------------- src/openvpn/lladdr.h | 3 ++- 3 files changed, 18 insertions(+), 20 deletions(-)
src/openvpn/init.c +3 -2
@@ -1176,7 +1176,7 @@ do_persist_tuntap(const struct options *options, openvpn_net_ctx_t *ctx) ctx);if (options->persist_mode && options->lladdr) { -set_lladdr (options->dev, options->lladdr,NULL ); +set_lladdr (ctx, options->dev, options->lladdr,NULL ); }return true;#else /* ifdef ENABLE_FEATURE_TUN_PERSIST */
@@ -1853,7 +1853,8 @@ do_open_tun(struct context *c)/* set the hardware address */ if (c->options.lladdr) { -set_lladdr (c->c1.tuntap->actual_name, c->options.lladdr, c->c2.es); +set_lladdr (&c->net_ctx, c->c1.tuntap->actual_name, c->options.lladdr, + c->c2.es); }/* do ifconfig */
src/openvpn/lladdr.c +13 -17
@@ -15,10 +15,9 @@#include "lladdr.h" int -set_lladdr (const char *ifname,const char *lladdr, +set_lladdr (openvpn_net_ctx_t *ctx,const char *ifname,const char *lladdr,const struct env_set *es) { -struct argv argv =argv_new ();int r;if (!ifname || !lladdr)
@@ -27,17 +26,13 @@ set_lladdr(const char *ifname, const char *lladdr, }#if defined(TARGET_LINUX ) -#ifdef ENABLE_IPROUTE -argv_printf (&argv, -"%s link set addr %s dev %s" , - iproute_path, lladdr, ifname); -#else -argv_printf (&argv, -"%s %s hw ether %s" , -IFCONFIG_PATH , - ifname, lladdr); -#endif -#elif defined(TARGET_SOLARIS ) +uint8_t addr[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,
@@ -57,18 +52,19 @@ set_lladdr(const char *ifname, const char *lladdr,"%s %s ether %s" ,IFCONFIG_PATH , ifname, lladdr); -#else /* if defined(TARGET_LINUX) */ +#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_LINUX) */ - +#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) */ +if (r) {msg (M_INFO ,"TUN/TAP link layer address set to %s" , lladdr); } -argv_free (&argv);return r; }
src/openvpn/lladdr.h +2 -1
@@ -3,6 +3,7 @@*/ #include "misc.h" +#include "networking.h" -int set_lladdr (const char *ifname,const char *lladdr, +int set_lladdr (openvpn_net_ctx_t *ctx,const char *ifname,const char *lladdr,const struct env_set *es);
Signature · 1 line
2.32.0
HI,
Quoted 1 line · Antonio Quartulli
Reported-by Jan Hugo Prins <[email protected]...>
Jan Hugo, have you been able to test these two patches?
I could set up a test rig myself, but since I do not experience the issue, it's always more useful if an affected user reports "yes, it works for me now" :-)
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...PATCH v2 1/2 networking: add and implement net_addr_ll_set() API
In reply to Antonio QuartulliWhen running in TAP mode we may need to set the LL address of the interface, if requested by the user.
This operation was overlooked when implementing the networking API and it still relies on iproute/net-tools being installed.
Basically this means that when compiling OpenVPN on a system without iproute2/net-tools and the user uses the "lladdr" config directive, OpenVPN will fail to se the LL address of the interface.
With this patch a new API is introduced and it is implemented for both SITNL and iproute2 backends.
---
Changes from v1:
* addemissing brackets around oneliner if-block
src/openvpn/misc.h | 7 +++++++ src/openvpn/networking.h | 12 +++++++++++ src/openvpn/networking_iproute2.c | 23 ++++++++++++++++++++ src/openvpn/networking_sitnl.c | 35 +++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+)
src/openvpn/misc.h +7 -0
@@ -211,4 +211,11 @@ get_num_elements(const char *string, char delimiter);struct buffer prepend_dir (const char *dir,const char *path,struct gc_arena *gc); +#define _STRINGIFY (S ) #S +#define MAC_FMT _STRINGIFY(%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx) +#define MAC_PRINT_ARG (_mac) _mac[0], _mac[1], _mac[2], \ + _mac[3], _mac[4], _mac[5] +#define MAC_SCAN_ARG (_mac) &_mac[0], &_mac[1], &_mac[2], \ + &_mac[3], &_mac[4], &_mac[5] +#endif /* ifndef MISC_H */
src/openvpn/networking.h +12 -0
@@ -103,6 +103,18 @@ int net_iface_up(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,int net_iface_mtu_set (openvpn_net_ctx_t *ctx,const openvpn_net_iface_t *iface,uint32_t mtu); +/** +* Set the Link Layer (Ethernet) address of the TAP interface +* +* @param ctx the implementation specific context +* @param iface the interface to modify +* @param addr the new address to set (expected ETH_ALEN bytes (6)) +* +* @return 0 on success, a negative error code otherwise +*/ +int net_addr_ll_set (openvpn_net_ctx_t *ctx,const openvpn_net_iface_t *iface, +uint8_t *addr); +/** * Add an IPv4 address to an interface *
src/openvpn/networking_iproute2.c +23 -0
@@ -93,6 +93,29 @@ net_iface_mtu_set(openvpn_net_ctx_t *ctx, const char *iface, uint32_t mtu)return 0 ; } +int +net_addr_ll_set (openvpn_net_ctx_t *ctx,const openvpn_net_iface_t *iface, +uint8_t *addr) +{ +struct argv argv =argv_new (); +int ret =0 ; + +argv_printf (&argv, +"%s link set addr " MAC_FMT " dev %s" , + iproute_path,MAC_PRINT_ARG (addr), iface); + +argv_msg (M_INFO , &argv); +if (!openvpn_execve_check (&argv, ctx->es,M_WARN , +"Linux ip link set addr failed" )) + { + ret =-1 ; + } + +argv_free (&argv); + +return ret; +} +int net_addr_v4_add (openvpn_net_ctx_t *ctx,const char *iface,const in_addr_t *addr,int prefixlen)
src/openvpn/networking_sitnl.c +35 -0
@@ -30,6 +30,7 @@#include "errlevel.h" #include "buffer.h" +#include "misc.h" #include "networking.h" #include <errno.h>
@@ -723,6 +724,40 @@ err:return ret; } +int +net_addr_ll_set (openvpn_net_ctx_t *ctx,const openvpn_net_iface_t *iface, +uint8_t *addr) +{ +struct sitnl_link_req req; +int ifindex, ret =-1 ; + +CLEAR (req); + + ifindex =if_nametoindex (iface); +if (ifindex ==0 ) + { +msg (M_WARN |M_ERRNO ,"%s: rtnl: cannot get ifindex for %s" , __func__, + iface); +return -1 ; + } + + req.n.nlmsg_len =NLMSG_LENGTH (sizeof (req.i)); + req.n.nlmsg_flags =NLM_F_REQUEST ; + req.n.nlmsg_type =RTM_NEWLINK ; + + req.i.ifi_family =AF_PACKET ; + req.i.ifi_index = ifindex; + +SITNL_ADDATTR (&req.n,sizeof (req),IFLA_ADDRESS , addr,ETH_ALEN ); + +msg (M_INFO ,"%s: lladdr " MAC_FMT " for %s" , __func__,MAC_PRINT_ARG (addr), + iface); + + ret =sitnl_send (&req.n,0 ,0 ,NULL ,NULL ); +err: +return ret; +} + static int sitnl_addr_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family, const inet_address_t *local, const inet_address_t *remote,
Signature · 1 line
2.32.0
This basically does not give me much to test, as it just adds functions that are not called yet. Those look reasonable, and the result passes compilation on Linux and FreeBSD.
I do not like the "MAC_FMT" bit very much - it looks like "yeah, I saw this in another project, and it looks cool", but it introduces an extra "what is it doing here?" for the two cases where it is used (and the extra _STRINGIFY() does not help make it easier to follow). But we discussed this on IRC, and seems we need to disagree here...
NOTE: I accidentially pushed out v1 of the patch, with the "wrong" brackets. We'll sort this out.
NOTE2: this is a bugfix (omission), so it is intended to go to release/2.5 as well - but with the wrong patch merged to master, I think I'll just get them all in, and then squash together the 3 patches into a single one for 2.5.
Your patch has been applied to the master branch.
commit 98f524cbd58d24d09dee26160d7386d710c3564f
Author: Antonio Quartulli Date: Fri Sep 3 18:11:12 2021 +0200
networking: add and implement net_addr_ll_set() API
Signed-off-by: Antonio Quartulli <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg22792.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
PATCH 1/1 networking: add missing brackets
Our codestyle demands having brackets also around if-blocks made up by one line only.
In patch "networking: add and implement net_addr_ll_set() API" this rule was not respected and a new one-line-if-block was added with no brackets.
Fix this by adding the related brackets as needed.
--- src/openvpn/networking_iproute2.c | 2 ++ 1 file changed, 2 insertions(+)
src/openvpn/networking_iproute2.c +2 -0
@@ -107,7 +107,9 @@ net_addr_ll_set(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,argv_msg (M_INFO , &argv);if (!openvpn_execve_check (&argv, ctx->es,M_WARN ,"Linux ip link set addr failed" )) + { ret =-1 ; + }argv_free (&argv);
Signature · 1 line
2.32.0
[Openvpn-devel] [PATCH applied] Re: networking: add missing brackets
This is one of the more silly fixup patches, given that we already had a fixed v2 on the list - apologies.
Your patch has been applied to the master branch.
I'll bring this to 2.5 as well, but squashed together with the "real" patch.
commit cb5d29461e6e734a2250b984b8423d39f7b9ddaa
Author: Antonio Quartulli Date: Wed Sep 29 13:59:05 2021 +0200
networking: add missing brackets
Signed-off-by: Antonio Quartulli <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg22908.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: set_lladdr: use networking API net_addr_ll_set() on Linux
In reply to Antonio QuartulliStared at the code, tested on FreeBSD, Linux/iproute2 and Linux/sitnl, MAC addresses are properly set.
As discussed on IRC a while ago, I have added brackets to the ugly assignment
r = (net_addr_ll_set(ctx, ifname, addr) == 0);
and I have added the "Tested-By:" line from Jan Hugo Prins.
Your patch has been applied to the master branch, and pulled up (3 squashed commits) to release/2.5
commit 7205cdd8508be0ec9a83ea2e012e2a495157cad0 (master) commit e2fded0f72765467fd0d602562bbf41d48c58d39 (release/2.5)
Author: Antonio Quartulli Date: Fri Sep 3 18:11:13 2021 +0200
set_lladdr: use networking API net_addr_ll_set() on Linux
Signed-off-by: Antonio Quartulli <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg22791.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering