Skip to main content
3 of 7
added 248 characters in body
grawity
  • 16.3k
  • 1
  • 34
  • 54

If I have some software that wants to add an IP to the host, how could that software discover that the IP is taken. Eg using the ip CLI?

The same way it adds the IP address.

If the program uses Netlink (rtnl), then after adding the rtnl_address object it can get the same object and check its flags – 'tentative' means it is still undergoing Duplicate Address Detection, 'dadfailed' means DAD finished and found a collision, absence of both flags means DAD finished and no collisions were found.

The program doesn't need to wait for a specific time. The kernel's DAD code already has the necessary timers. The program only needs to keep checking (e.g. every second) until the 'tentative' flag disappears. But Netlink also provides change notifications (like in ip monitor addr) so the program will automatically keep receiving the address object every time its flags change, without having to poll.

(There is nothing special that the program needs to do in order to activate DAD; the kernel automatically performs it any time an address is added.)

If the program uses the ip CLI, then to begin with, it should stop using the ip CLI and start using interfaces meant for machine use, namely Netlink. The ip CLI is an interface for human consumption. But if that's not possible then the program should use ip -json to query the addresses, parsing the output as JSON and using the same logic as for Netlink (i.e. wait until 'tentative' has disappeared and then check for presence or absence of 'dadfailed').

If the program uses the ip CLI on a platform where there's no -json option (either because it's an ancient iproute2 version or because it's the Busybox imitation), then sorry but that's already in the "works on my computer" territory.

And if the program uses the old Linux 2.4 era IPv6-specific ioctl and /proc/net interfaces, then AFAIK it has no way to obtain this information, and it should be ported to Netlink – although even ip -json would already be an upgrade.

grawity
  • 16.3k
  • 1
  • 34
  • 54