I have a subnet /24 (256 public IP addresses). Currently, I assign IP .255 to an LXD container (ubuntu) and it seems working normally. I wonder if someone sends a UDP message to the host that has IP .255 over the internet, will that message get broadcasted to all of my other IP?
You need to understand some things.
First, a host only knows about the Limited Broadcast address (
255.255.255.255), and its own network broadcast address (the last address in the network as defined by the network mask configured on the host). Packets sent to either of those addresses will not travel beyond the network on which they are sent.
For any addresses outside the network of the host, the host has no idea if the address is a host, network, or broadcast address. The host only knows it is an IPv4 address (IPv6 does not have broadcast). For example, a destination
/23 (or smaller) network will have valid
.255 host addresses.
The final router in the path is directly connected to the destination network and will know the destination network mask, so it will determine if the destination address is a broadcast or network address. By default, such a router will drop packets sent to the network or broadcast address of a directly connected network. The router could be configured to allow a broadcast destination (directed broadcast), but that would be foolish because it facilitates DoS attacks on the network (broadcasts interrupt every host on a network, and are considered wasteful and obsolete, which is why they were eliminated in IPv6).
The notion of .255 as an IPv4 broadcast address exists only within the /24 net it belongs to. The global Internet does not know or care whether that address happens to be the broadcast address of the destination network. Therefore an IP packet sent to that address will be routed normally until it reaches the router responsible for final delivery to that address range, ie. which is either directly connected to the L2 network carrying the /24 net or which has a NAT rule transforming it to a different destination address.
What happens next depends on the configuration on that router.
- If the destination router is directly connected to an Ethernet segment carrying the /24 IPv4 net in question and configured to forward directed broadcasts to it, and does not do NAT then yes, the packet will be broadcasted to all hosts on that segment. (Note that it will not be broadcasted to all IP addresses, as your question seems to imply. It will be sent to the broadcast MAC address, but with its original .255 destination IP address. This will cause all network interfaces on the segment to receive it, but each receiving host will see that it is addressed to the broadcast IP address, not to its unicast address, and be free to ignore it for that reason.)
- In most cases however the destination router will be configured to drop packets to the directed broadcast address. This is the default since RFC2644.
- Often public IPv4 addresses are transformed into private ones before delivery via NAT. In that case, the .255 address is treated no differently from any other. It is looked up in the NAT table and replaced by whatever actual destination address was found there. No broadcast happens. So the .255 address is not considered a broadcast address in that case.
Those are the most frequent cases. Others are possible but can be solved along the same lines.