1# Lws routing 2 3lws is mainly built around POSIX sockets and operates from the 4information available from those. But in some cases, it needs to go 5a step further and monitor and understand the device routing table. 6 7## Recognizing loss of routability 8 9On mobile devices, switching between interfaces and losing / regaining 10connections quickly is a given. But POSIX sockets do not act like 11that, the socket remains connected until something times it out if it 12no longer has a route to its peer, and the tcp timeouts can be in the 13order of minutes. 14 15In order to do better, lws must monitor and understand how the routing 16table relates to existing connections, dynamically. 17 18## Linux: netlink 19 20For linux-based devices you can build in netlink-based route monitoring 21with `-DLWS_WITH_NETLINK=1`, lws aquires a copy of the routing table 22when the context / pt starts up and modifies it according to netlink 23messages from then on. 24 25On Linux routing table events do not take much care about backing out 26changes made on interface up by, eg, NetworkManager. So lws also 27monitors for link / interface down to remove the related routes. 28 29## Actions in lws based on routing table 30 31Both server and client connections now store their peer sockaddr in the 32wsi, and when the routing table changes, all active wsi on a pt are 33checked against the routing table to confirm the peer is still 34routable. 35 36For example if there is no net route matching the peer and no gateway, 37the connection is invalidated and closed. Similarly, if we are 38removing the highest priority gateway route, all connections to a peer 39without a net route match are invalidated. However connections with 40an unaffected matching net route like 127.0.0.0/8 are left alone. 41 42## Intergration to other subsystems 43 44If SMD is built in, on any route change a NETWORK message 45`{"rt":"add|del"}` is issued. 46 47If SMD is built in, on any route change involving a gateway, a NETWORK 48message `{"trigger":"cpdcheck", "src":"gw-change"}` is issued. If 49Captive Portal Detection is built in, this will cause a new captive 50portal detection sequence. 51 52