Broadcast works but broadcast address not set

linux-networking

Quick question

Is the good practice for getting a broadcast address to use system calls like ioctl() or getifaddrs(), or are they obsolete/useless because of the following two reasons?

  1. I can make a binary OR between the netmask and the IP
  2. The broadcast address is not always set anyway

Details

I am playing with some UDP broadcast code, both on my Arch Linux machine and on some Ubuntu Docker containers.

I see that some interfaces have the broadcast address set, for instance my wifi interface on Arch Linux (from $ ip addr):

inet 192.168.0.11/24 brd 192.168.0.255 scope global wlp4s0

And some don't, for instance the eth0 interface on an Ubuntu Docker container:

inet 172.17.0.5/16 scope global eth0

However, if I send packets to 172.17.255.255, they are actually broadcasted to other machines on the network.

On this question it is said that it is not necessary to set the broadcast address manually. And here, somebody seems to imply that Docker purposely doesn't set the broadcast address, for some reason I don't undertand.

So I get to wonder: what is the right way to detect the broadcast address for a network interface?

Best Answer

It seems like the broadcast address is somehow an "independent" field in the interface definition (at least on some linux systems I tried), but it should not be used for directed broadcasts, as it could be unset.

If unset, the broadcast address is wrongly read as "0.0.0.0" by commands such as ioctl() and we end up in this situation.

So I would again mention this question and support the idea that the broadcast address should be computed from the ip and the netmask. The broadcast address should merely be considered an indication for outputs of commands such as ip addr or ifconfig.