Bugfix: Set broadcast address on interface
PATCH 1/1 Bugfix: Set broadcast address on interface
Hi,
Quoted 4 lines · Sebastian Marsching
This patch fixes this issue both when using iproute (by telling iproute to set the broadcast address based on the local address and prefix) and when using Netlink (by calculating the correct broadcast address and setting it).
Should Linux not be able to auto-calculate that, based on IP address and netmask?
No other OS needs explicit setting of broadcast address in 2025...
Can you show the difference in "ip address show" or "ifconfig" output before/after your change? Is this on a client or server?
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]...Hi Gert,
Quoted 8 lines · Sebastian Marsching
Quoted 4 lines
This patch fixes this issue both when using iproute (by telling iproute to set the broadcast address based on the local address and prefix) and when using Netlink (by calculating the correct broadcast address and setting it).
Should Linux not be able to auto-calculate that, based on IP address and netmask?
You would think so, but it does not…
Quoted 4 lines
No other OS needs explicit setting of broadcast address in 2025...
Can you show the difference in "ip address show" or "ifconfig" output before/after your change? Is this on a client or server?
Output of ifconfig without the change (IPv6 address anonymized by me):
tap0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.22.56.178 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx prefixlen 64 scopeid 0x0<global>
inet6 fe80::800b:93ff:fe07:a32a prefixlen 64 scopeid 0x20<link>
ether f6:f6:91:dc:83:af txqueuelen 1000 (Ethernet)
RX packets 108 bytes 32404 (32.4 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 14 bytes 1172 (1.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0With the change:
tap0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.22.56.178 netmask 255.255.255.0 broadcast 172.22.56.255
inet6 xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx prefixlen 64 scopeid 0x0<global>
inet6 fe80::f0f2:e4ff:fe4f:bfff prefixlen 64 scopeid 0x20<link>
ether f2:f2:e4:4f:bf:ff txqueuelen 1000 (Ethernet)
RX packets 12 bytes 1200 (1.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11 bytes 962 (962.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0When using ip address show instead, the difference is that without the change
inet 172.22.56.178/24 scope global tap0
is shown, while with the change
inet 172.22.56.178/24 brd 172.22.56.255 scope global tap0
is shown.
In fact, systemd-networkd also does it. When configuring an IP address,
1. it calls the code that calculates the broadcast address at https://github.com/systemd/systemd/blob/5bf7438ff025ae05daf1b706f204f31373d5ab82/src/network/networkd-address.c#L1667,
2. it calculates the broadcast address (if not specified explicitly) at https://github.com/systemd/systemd/blob/5bf7438ff025ae05daf1b706f204f31373d5ab82/src/network/networkd-address.c#L408,
3. and finally it send the broadcast address to the kernel at https://github.com/systemd/systemd/blob/5bf7438ff025ae05daf1b706f204f31373d5ab82/src/network/networkd-address.c#L1555.
It implements the way how the broadcast address is calculated slightly differently. For my patch, I took the inspiration from how iproute2 implements it instead.
Best regards,
Sebastian
Hi,
Quoted 3 lines · Sebastian Marsching
Quoted 2 lines
Should Linux not be able to auto-calculate that, based on IP address and netmask?
You would think so, but it does not???
It seems so. Thanks for the output examples. Weird.
[..]
Quoted 1 line
It implements the way how the broadcast address is calculated slightly differently. For my patch, I took the inspiration from how iproute2 implements it instead.
It's a bit unfortunate that *this* piece of code does not have the netmask around anymore (having converted it carefully to a /netbits value...)
I'll leave this to Antonio to ACK, as it's all his code.
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]...Hi,
Review sitnl_addr_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family, · line 810
Quoted 8 lines · Sebastian Marsching
(quoted diff) +4
-0@@ -803,6 +804,16 @@ sitnl_addr_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family, SITNL_ADDATTR(&req.n, sizeof(req), IFA_LOCAL, local, size); } + if (af_family == AF_INET && local && !remote && prefixlen <= 30) + { + broadcast = *local; + for (i = 31; i >= prefixlen; i--)
Ah, well, I guess the integer police will come after us... so in modern parts of OpenVPN we go for local C99 declarations, *and* we take extra care to make it all -Wsign-conversion safe, so I think this needs to be
+ for (unsigned int i = 31; i >= prefixlen; i--)
(and no extra "int i" declaration on top)
This said, I see we have a "netbits_to_netmask(bits)" function in route.h -> please use that, it should be something like
+ if (af_family == AF_INET && local && !remote && prefixlen <= 30)
+ {
+ inet_address_t broadcast = *local; + broadcast.ipv4 |= ~netbits_to_netmask(prefixlen); + ...
(maybe with an htonl() wrapped around)
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]...Quoted 10 lines
This said, I see we have a "
netbits_to_netmask(bits)" function inroute.h-> please use that, it should be something like(quoted diff) +5
-0+ if (af_family == AF_INET && local && !remote && prefixlen <= 30) + { + inet_address_t broadcast = *local; + broadcast.ipv4 |= ~netbits_to_netmask(prefixlen); + ...(maybe with an
htonl()wrapped around)
Thanks for pointing me to the netbits_to_netmask function. Using this function, the code looks so much cleaner. See the updated patch below.
Hi,
Quoted 1 line · Sebastian Marsching
Thanks for pointing me to the
netbits_to_netmaskfunction. Using this function, the code looks so much cleaner. See the updated patch below.
Close...
Quoted 9 lines
(quoted diff) +1
-0@@ -760,6 +761,7 @@ sitnl_addr_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family, { struct sitnl_addr_req req; uint32_t size; + inet_address_t broadcast; int ret = -EINVAL; CLEAR(req);
Review sitnl_addr_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family, · line 812
Quoted 8 lines
(quoted diff) +5
-0@@ -803,6 +805,13 @@ sitnl_addr_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family, SITNL_ADDATTR(&req.n, sizeof(req), IFA_LOCAL, local, size); } + if (af_family == AF_INET && local && !remote && prefixlen <= 30) + { + broadcast = *local; + broadcast.ipv4 |= htonl(~netbits_to_netmask(prefixlen)); + SITNL_ADDATTR(&req.n, sizeof(req), IFA_BROADCAST, &broadcast, size);
... but we do want local-scoped things to be declared locally (C99), so
+ inet_address_t broadcast = *local;
here (SITNL_ADDATTR() will copy the struct content, not the reference).
When sending a v3, can you send that with "git send-email -v3" in its own mail, "--in-reply-to <this_thread>"? This makes it easy to see which version is the latest.
thanks,
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 v3 1/1 Bugfix: Set broadcast address on interface.
This fixes a problem that was introduced in OpenVPN 2.5. Previously, the ifconfig utility was used for adding the local address to an interface. This utility automatically sets the correct broadcast address based on the given unicast address and netmask.
Due to switching to iproute and Netlink, this does not happen automatically any longer, which means that applications that rely on broadcasts do not work correctly.
This patch fixes this issue both when using iproute (by telling iproute to set the broadcast address based on the local address and prefix) and when using Netlink (by calculating the correct broadcast address and setting it).
--- src/openvpn/networking_iproute2.c | 2 +- src/openvpn/networking_sitnl.c | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-)
src/openvpn/networking_iproute2.c +1 -1
@@ -150,7 +150,7 @@ net_addr_v4_add(openvpn_net_ctx_t *ctx, const char *iface, const in_addr_t *addrconst char *addr_str =print_in_addr_t (*addr,0 , &ctx->gc); -argv_printf (&argv,"%s addr add dev %s %s/%d" , iproute_path, iface, addr_str, prefixlen); +argv_printf (&argv,"%s addr add dev %s %s/%d broadcast +" , iproute_path, iface, addr_str, prefixlen);argv_msg (M_INFO , &argv);openvpn_execve_check (&argv, ctx->es,S_FATAL ,"Linux ip addr add failed" );
src/openvpn/networking_sitnl.c +8 -0
@@ -31,6 +31,7 @@#include "misc.h" #include "networking.h" #include "proto.h" +#include "route.h" #include <errno.h> #include <string.h>
@@ -803,6 +804,13 @@ sitnl_addr_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family,SITNL_ADDATTR (&req.n,sizeof (req),IFA_LOCAL , local, size); } +if (af_family ==AF_INET && local && !remote && prefixlen <=30 ) + { +inet_address_t broadcast = *local; + broadcast.ipv4 |=htonl (~netbits_to_netmask (prefixlen)); +SITNL_ADDATTR (&req.n,sizeof (req),IFA_BROADCAST , &broadcast, size); + } + ret =sitnl_send (&req.n,0 ,0 ,NULL ,NULL );if (ret == -EEXIST ) {
Signature · 1 line
2.50.0
Re: [Openvpn-devel] [PATCH v3] Bugfix: Set broadcast address on interface.
Hi,
Quoted 9 lines · Sebastian Marsching
This fixes a problem that was introduced in OpenVPN 2.5. Previously, the ifconfig utility was used for adding the local address to an interface. This utility automatically sets the correct broadcast address based on the given unicast address and netmask.
Due to switching to iproute and Netlink, this does not happen automatically any longer, which means that applications that rely on broadcasts do not work correctly.
iproute2 was already supported before 2.5 (but probably it needed to be explicitly activated at compile time), therefore I'd say this bug already existed :)
I went through the kernel code and I can clearly see the difference in APIs: the old SIOCSIFADDR ioctl (used by net-tools/ifconfig) takes care of computing the broadcast address:
1230 if (!(dev->flags & IFF_POINTOPOINT)) {
1231 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address); 1232 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen); 1233 if ((dev->flags & IFF_BROADCAST) &&
1234 ifa->ifa_prefixlen < 31)
1235 ifa->ifa_broadcast = ifa->ifa_address |
1236 ~ifa->ifa_mask;
1237 }
However, this logic does not exist in the more modern netlink code, therefore it was a somehow conscious decision.
Quoted 4 lines
This patch fixes this issue both when using iproute (by telling iproute to set the broadcast address based on the local address and prefix) and when using Netlink (by calculating the correct broadcast address and setting it).
I imagine this is the behaviour the average OpenVPN user expects (although probably only few of them needs a brd address), therefore thanks for fixing this.
Quoted 33 lines
Signed-off-by Sebastian Marsching <[email protected]...>--- src/openvpn/networking_iproute2.c | 2 +- src/openvpn/networking_sitnl.c | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-)src/openvpn/networking_iproute2.c +1
-1@@ -150,7 +150,7 @@ net_addr_v4_add(openvpn_net_ctx_t *ctx, const char *iface, const in_addr_t *addrconst char *addr_str =print_in_addr_t (*addr,0 , &ctx->gc); -argv_printf (&argv,"%s addr add dev %s %s/%d" , iproute_path, iface, addr_str, prefixlen); +argv_printf (&argv,"%s addr add dev %s %s/%d broadcast +" , iproute_path, iface, addr_str, prefixlen);argv_msg (M_INFO , &argv);openvpn_execve_check (&argv, ctx->es,S_FATAL ,"Linux ip addr add failed" );src/openvpn/networking_sitnl.c +1
-0@@ -31,6 +31,7 @@#include "misc.h" #include "networking.h" #include "proto.h" +#include "route.h" #include <errno.h> #include <string.h>
src/openvpn/networking_sitnl.c sitnl_addr_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family, · line 813
Quoted 10 lines
src/openvpn/networking_sitnl.c +7
-0@@ -803,6 +804,13 @@ sitnl_addr_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family,SITNL_ADDATTR (&req.n,sizeof (req),IFA_LOCAL , local, size); } +if (af_family ==AF_INET && local && !remote && prefixlen <=30 ) + { +inet_address_t broadcast = *local; + broadcast.ipv4 |=htonl (~netbits_to_netmask (prefixlen)); +SITNL_ADDATTR (&req.n,sizeof (req),IFA_BROADCAST , &broadcast, size); + } +
The code looks good and makes sense to me (nice to see how concise a patch extending sitnl can be :)).
Thanks Gert for complaining about the style earlier on.
I haven't tested it, but we have a buildbot army which will tell us if some esoteric bug is hidden away.
Regards,
Quoted 3 lines
src/openvpn/networking_sitnl.c +0
-0ret =sitnl_send (&req.n,0 ,0 ,NULL ,NULL );if (ret == -EEXIST ) {
Signature · 1 line
Antonio Quartulli
[Openvpn-devel] [PATCH applied] Re: Bugfix: Set broadcast address on interface.
In reply to Sebastian MarschingI have tested this on Linux "without DCO" and "with DCO", client and server. The non-DCO server version isn't really helpful as it puts all tun interface into p2p mode ("routing happens inside OpenVPN"), but the TAP interfaces shows the expected "broadcast" setting - as does the "tun / topology subnet" client. On the DCO "tun" servers the handling of routes/iroutes is different, and the expected netmask + brd shows up. And the tests still pass :-)
Your patch has been applied to the master and release/2.6 branch (whether it's "a bugfix" stands to be debated - but it is not very intrusive and has passed all my system tests, and if the lack of broadcast did break people's working 2.5 setups they will well consider it a "bug"... so, "bugfix")
commit 0df0edc49ce4acb96c8e16bdf0fcee1eedfd91f0 (master) commit 4e46e727217cf58c7c671e36e8417e469c4e3b1d (release/2.6)
Author: Sebastian Marsching Date: Mon Sep 15 13:05:07 2025 +0200
Bugfix: Set broadcast address on interface.
Signed-off-by: Sebastian Marsching <[email protected]...>
Acked-by: Antonio Quartulli <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg33131.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering