5

I have an ap0 interface configured for systemd-networkd which is designed to be enabled/disabled dynamically by an application. I would like this same application to bind to an IP address associated with ap0 also. However, if the application disables the ap0 interface then its bind will naturally fail.

Is there an approach where my application could stay bound to its address, and for that address to remain bindable across ap0 going up and down, but available on ap0 when it is up? I have been experimenting with network bonding and bridging, but haven't been very successful.

0

2 Answers 2

10

If the app supports it (or if it's your app, or if you're willing to patch it), there's a setsockopt flag IP_FREEBIND in Linux 2.4+ (and FreeBSD) which allows binding a socket to an address that doesn't (currently) exist. That gives exactly the behavior you're asking for: when the interface is up, your app will get connections on that address, but when it's down there's no error condition.

Recent versions of Apache, for instance, support this by adding the freebind option to a Listen directive.

3
  • Thanks for this. Unfortunately it doesn’t appear that I have that level of control over the bind. I’m using the Rust Warp library. It might be a good feature for me to PR on that library though. That said, I’m also binding via socat which isn’t tolerant of the address being unavailable either. Commented Jun 15 at 20:57
  • 4
    @ChristopherHunt You can try setting the sysctl options net.ipv4.ip_nonlocal_bind and new.ipv6.ip_nonlocal_bind. Enabling them basically enables IP_FREEBIND for all sockets on your system and therefore does not require to change the programs. Commented Jun 15 at 22:04
  • @MarcelKrüger I didn't know that! I think it would be fair to make it into an answer.
    – hobbs
    Commented Jun 16 at 6:13
4

Answering my own question, this does appear to work, but I'm keen to learn of any other methods:

  1. I create a bridge, br0 - it has 192.168.3.1 and DHCP configured
  2. I create a dummy interface, du0, and associate it with the br0 bridge.
  3. I create the ap0 interface and associate it with the same br0 bridge.

I can now do what I want to the ap0 interface and it won't affect anything bound to 192.168.3.1.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .