netinet: Auto add loopback routes to interface addresses
authorAaron LI <aly@aaronly.me>
Mon, 18 Mar 2024 03:43:41 +0000 (11:43 +0800)
committerAaron LI <aly@aaronly.me>
Wed, 20 Mar 2024 15:33:32 +0000 (23:33 +0800)
commit951ecd7f55e2edd33258ad6d9593c9f81c92b42e
tree2a211fc9df025f32314de9c7a269e29c7bc251ae
parent650ba6fbdd2d4b0bcc6a1976a6630ada8e96b53f
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
sys/net/if.c
sys/net/if_var.h
sys/netinet/in.c
sys/netinet/in_rmx.c