Auto configuration between AP and router depending upon WAN link status

I have been playing with openwrt origin/master on a netgear r6220 router.
I would like to build an image that does a better job of "auto-sensing" the probable mode of operation (access-point or router).

For example, if the WAN port is up and a DHCP server can be found on the WAN network, I would like openwrt should operate as a NAT router and choose a LAN network address to be not a subset of the WAN network (rather than a pre-configured static address)
If on the otherhand WAN is down, a search on the LAN for a DHCP server should be done. If a DHCP server is present, openwrt should get its local address (for ssh/LUCI) from there and disable dnsmasq. If a DHCP server is not present, openwrt should start dnsmasq with a preconfigured static address.

So far I understand (perhaps incorrectly) that netifd is responsible for receiving network connected/not connected notifications and bringing up the interfaces according to the configuration in /etc/config/network. It would seem the right way to approach this is to get netifd to run some stuff when WAN gets connected or disconnected.
do you think this can this be done with a /lib/network/*.sh?
what sort of docs should i study to get an idea on how to implement this or something like it?

1 Like

Just as a side note, after booting up, WAN will always be down - until it gets brought up, which makes it hard to detect this reliably.

ISPs also have semi-regular maintenance windows[1], while the WAN connection will be down as well - if your router then suddenly notices and decides to become an AP, you not only lose internet access, but also internal connectivity with little chance to recover (and you may want to access your "router's", now suddenly APs, management interface to check why the internet is down, which is hard to accomplish when the "router" has lost its own IP address, waiting for a non-existing DHCPd to assign one).

Edit: If there is one device in your network, which mustn't ever be confused about its role, it's the router (mostly if to start DHCPd/ DHCPv6-server services and which IPs to hand out) - both no DHCPd being present in your network[2] or (even worse) multiple DHCPd's in a single physical network are absolutely fatal.

[1] this can easily be an hour or two a month, without advanced (or any-) notice
[2] unless all clients are statically configured, which would just shift the decision you don't want to make for your single router or handful of APs, to dozens of clients

1 Like

Those comments driving additional thinking, you may want to poll the switch to see if there is a plugged-in, active cable condition on the port.

Or just use a button, which for many people serve “no” function.

so a hotplug down event would occur when the maint window begins?
is the hotpug event equivalent to changes in the status i get with

swconfig dev switch0 port 4 get link
which i think is carrier sense?
i am suprised to hear that carrier sense would drop. do they cycle power on their side of the interface?