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

@ -261,6 +261,25 @@ sockread(struct socket *s, char *addr, int n)
if(s->domain == AF_INET)
return inet_read(s->inet, addr, n);
// Netlink never blocks: the answer was generated when the request was sent,
// so an empty buffer means the caller has already drained this dump rather
// than that more is coming. Blocking here would hang a getifaddrs() that
// asked one question too many.
if(s->domain == AF_NETLINK){
acquire(&stab.lock);
uint have = s->nwrite - s->nread;
if(have == 0){
release(&stab.lock);
return -EAGAIN;
}
if((uint)n > have)
n = (int)have;
memmove(addr, s->data + s->nread, (uint)n);
s->nread += (uint)n;
release(&stab.lock);
return n;
}
acquire(&stab.lock);
while(s->nread == s->nwrite && !s->peer_closed){
if(myproc()->killed || s->state == SS_FREE){
@ -294,6 +313,20 @@ sockwrite(struct socket *s, char *addr, int n)
if(s->domain == AF_INET)
return inet_write(s->inet, addr, n);
// A netlink request is answered immediately, into this socket's own buffer
// rather than a peer's -- there is no peer. Doing the work here is what lets
// the read that follows never block, which matters because musl reads with
// MSG_DONTWAIT and treats a short read as the end of the dump.
if(s->domain == AF_NETLINK){
acquire(&stab.lock);
s->nread = s->nwrite = 0;
int got = netlink_request(addr, (uint)n, s->data, SOCKBUF);
if(got > 0)
s->nwrite = (uint)got;
release(&stab.lock);
return n; /* the request was consumed whole */
}
acquire(&stab.lock);
// Data goes into the peer's buffer, because that is where the peer will
@ -340,12 +373,22 @@ socknew(int domain, int type, int proto)
{
struct socket *s;
if(domain != AF_UNIX && domain != AF_INET)
if(domain != AF_UNIX && domain != AF_INET && domain != AF_NETLINK)
return 0;
// AF_UNIX is stream-only here; AF_INET has datagrams too, and inet_new is
// what decides whether it can serve the type asked for.
if(domain == AF_UNIX && type != SOCK_STREAM)
return 0;
// AF_NETLINK carries whole messages and musl asks for SOCK_RAW; SOCK_DGRAM is
// accepted too because Linux does. NETLINK_ROUTE is the only protocol served
// -- see kernel/net/netlink.c, which answers RTM_GETLINK and RTM_GETADDR and
// nothing else.
if(domain == AF_NETLINK){
if(type != SOCK_RAW && type != SOCK_DGRAM)
return 0;
if(proto != NETLINK_ROUTE)
return 0;
}
acquire(&stab.lock);
s = alloc_locked(type);