netinet: Auto add loopback routes to interface addresses
When adding an address to an interface, a loopback route will be created
to route the traffic targeting it through the lo0 interface. The
loopback route will also be removed when the corresponding interface
address is deleted.
In principle, this can help simplify the network input and output paths
by avoiding special handling of local addresses. That deserves some
further efforts.
Nonetheless, an immediate benefit goes to wg(4). Now we can access
(e.g., ping) from localhost a wg(4) interface's IPv4 addresses.
Previously it was broken because wg(4) is a virtual device and doesn't
have/use ARP. For Ethernet devices, the ether_output() function will
redirect the traffic targeting its own address to the loopback interface
by calling if_simloop().
Derived from FreeBSD; the major commits are:
- This patch adds a host route to an interface address ...
https://github.com/freebsd/freebsd-src/commit/
ebc90701ac6c1f814c5bd6f3e19f0113ebe06156
- Self pointing routes are installed for configured interface addr ...
https://github.com/freebsd/freebsd-src/commit/
9bb7d0f47a3265a1782a64837ae93dde4c550a6a
- Unify loopback route switching
https://github.com/freebsd/freebsd-src/commit/
59c180c35c1ddc1d544a1c73c789d126a8fbac78
- Fix rt_ifa selection during loopback route insertion process
https://github.com/freebsd/freebsd-src/commit/
2ad7ed6e4a84448234e9aaef3c9fa884848f7387
Discussed-with: dillon