net: AF_NETLINK, so getifaddrs() works for everything and not just one program

fastfetch reported "getifaddrs(&ifAddrStruct) failed", and the right answer to
that is not a patch to fastfetch. Asking what interfaces a machine has is
something every networked program does, and on Linux there is exactly one way:
a NETLINK_ROUTE socket. musl implements getifaddrs() over it, so a libc call
that looks like a syscall is really a small protocol conversation -- and without
this side of it every caller fails the same way.

kernel/net/netlink.c is a responder rather than a general socket family. No
event stream, no multicast groups, nothing subscribes to link state: a request
arrives, the whole answer is generated into the socket's buffer, and reads drain
it. That shape is chosen, not a shortcut -- getifaddrs sends a dump and reads
until NLMSG_DONE with MSG_DONTWAIT, so generating at send time means a read
never blocks and the non-blocking flag is honoured for free.

RTM_GETLINK returns one RTM_NEWLINK per interface with IFLA_IFNAME and
IFLA_ADDRESS; RTM_GETADDR returns one RTM_NEWADDR per address with IFA_ADDRESS,
IFA_LOCAL and IFA_LABEL. Anything else gets a bare NLMSG_DONE, because the
caller's next move is a read either way and an unexpected error message is worse
than nothing to read.

The data is not netlink's. Interfaces come from the netdev class and addresses
from lwIP's netifs, exactly as /proc/netif reads them; nothing in the file knows
what an e1000 is. Interface indices matter more than they look -- getifaddrs
matches address records to link records by index, so the two dumps have to
agree. Loopback is 1, netdevs follow from 2.

Verified by reading it back through musl rather than by inspecting the bytes:

    lo     flags=0x49    fam=-1
    eth0   flags=0x1043  fam=17                    (AF_PACKET, the MAC)
    lo     flags=0x49    fam=2  addr=127.0.0.1  mask=255.0.0.0
    eth0   flags=0x1043  fam=2  addr=10.0.2.15  mask=255.255.255.0

Correct flags, addresses and netmasks, and the link records pair with the
address records.

One thing this does not close: fastfetch's Local IP module defaults to
default-route-only, which calls ffNetifGetDefaultRouteIfName() and reads
/proc/net/route. SchOS has no routing table and no such file, so the module
still shows nothing -- now for a reason that is about routing rather than about
enumeration. That is the next file to provide.

socktest still passes; the AF_UNIX and AF_INET paths are untouched.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
This commit is contained in:
Nexoniarz 2026-09-12 19:23:34 +02:00
commit f395c39704
9 changed files with 462 additions and 6 deletions

View file

@ -145,6 +145,38 @@ net_is_ready(void)
return net_ready;
}
// An interface's address and netmask, in network byte order, for callers that
// have no business including lwIP's headers -- kernel/net/netlink.c is the one
// that needs them. Zero when nothing is bound yet, which is a real answer: an
// interface without a lease has no address to report.
uint
netif_addr4(struct netif *nif)
{
return nif ? ip4_addr_get_u32(netif_ip4_addr(nif)) : 0;
}
uint
netif_mask4(struct netif *nif)
{
return nif ? ip4_addr_get_u32(netif_ip4_netmask(nif)) : 0;
}
// Network to host and back, for a 32-bit quantity. The kernel is little-endian
// and the wire is big-endian; lwIP's own macros are not reachable outside the
// files that include its headers.
uint
ntohl_u32(uint v)
{
return ((v & 0xFF) << 24) | ((v & 0xFF00) << 8) |
((v >> 8) & 0xFF00) | ((v >> 24) & 0xFF);
}
uint
htonl_u32(uint v)
{
return ntohl_u32(v);
}
static void
net_thread(void)
{