net: a TCP/IP stack, and AF_INET on the sockets that were already there
lwIP 2.2.0, fetched by ports/lwip and compiled straight into the kernel. Not written from scratch, and the reason is not the packet formats: it is congestion control, retransmission, window management and twenty years of corner cases that only show up against a real peer. Not FreeBSD's netinet either, though the licence would have allowed it -- that stack is not a library, it reaches into mbufs, epoch(9), netisr, VNET and uma(9), so adopting it means porting FreeBSD's kernel infrastructure first. lwIP was written for exactly this situation: a small kernel with a raw Ethernet driver and a port layer of a few hundred lines. It is kernel code, so nothing is installed into the sysroot and there is no second build system: the top-level Makefile compiles the files it wants against our port headers. The file list is explicit rather than a wildcard, because lwIP ships IPv6, 6LoWPAN, PPP, SLIP, DNS-SD and a dozen apps in the same tree, and building what is not configured yields either dead code or a link error naming a port function nobody meant to write. Its sources are unmodified and its warnings are silenced for its files only -- patching them would mean maintaining a fork. The port layer is ours: a lock, a clock, the netif that hands frames to e1000, lwipopts.h, and a shim/ directory of tiny headers so lwIP's <string.h> and friends resolve to the kernel's functions instead of the host's, whose prototypes disagree and would otherwise be found on the default include path. NO_SYS=1, so lwIP has no threads of its own and one kernel thread feeds it. A thread rather than the interrupt handler, because a call into lwIP can reach the driver, the driver can sleep, and an interrupt handler cannot -- so the interrupt still only clears the cause register and the thread polls. kthread_create is new and generally useful: a process with a kernel-only page directory, no files, no cwd, no parent, dispatched from forkret, which can therefore sleep and be preempted like anything else. DHCP obtains an address at boot and the console says which one. AF_INET went in exactly where phase 2 said it would. socknew dispatches on the domain, kernel/net/inet.c implements the internet side on lwIP's raw API, and not one syscall changed shape: bind, connect, listen, accept, read and write reach the same descriptors they always did. TCP and UDP both work. cmd/nettest opens a connection, writes, and reads the answer back. Two things are owed and named here so they are not discovered later: recvfrom does not yet report the source address, and the socket paths poll on the timer tick instead of sleeping on the socket. Both are follow-ups, not redesigns. NOTICE carries lwIP's BSD-3 text. Clause 3 is no-endorsement, not the old advertising clause, so it sits with Apache-2.0 without difficulty. Verified: the tree builds clean, and the headless boot check passes with 27 [ok] and no failures with the stack compiled in and the thread running -- which is the part that would break first if netinit deadlocked or the thread starved anything. An end-to-end connection test belongs in that suite and is the next commit. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
03cc316001
commit
c0806d1b64
30 changed files with 1922 additions and 19 deletions
|
|
@ -4,10 +4,12 @@
|
|||
// between a syscall's user-supplied `struct sockaddr` and the path string the
|
||||
// socket layer actually wants, plus the file-descriptor bookkeeping.
|
||||
//
|
||||
// Only AF_UNIX is answered. AF_INET is recognised and refused with
|
||||
// Two families now. AF_UNIX is answered by kernel/net/unix.c, AF_INET by
|
||||
// kernel/net/inet.c on top of lwIP; the socket layer dispatches on the domain
|
||||
// the socket was created with, so this file's only extra job is to read the
|
||||
// right shape of sockaddr out of user memory. AF_INET6 is still refused with
|
||||
// EAFNOSUPPORT rather than EINVAL, because a program that probes for IPv6 and
|
||||
// falls back to IPv4 needs to be able to tell "this address family does not
|
||||
// exist here" from "you passed nonsense".
|
||||
// falls back to IPv4 needs to tell "not here" from "you passed nonsense".
|
||||
|
||||
#include "types.h"
|
||||
#include "defs.h"
|
||||
|
|
@ -112,14 +114,16 @@ SYSCALL_DEFINE(socket)
|
|||
argint(1, &type);
|
||||
argint(2, &protocol);
|
||||
|
||||
if (domain != AF_UNIX)
|
||||
if (domain != AF_UNIX && domain != AF_INET)
|
||||
return -EAFNOSUPPORT;
|
||||
|
||||
// SOCK_CLOEXEC / SOCK_NONBLOCK ride in the high bits of `type`. Neither
|
||||
// is honoured yet, but masking them off means a musl program that always
|
||||
// sets SOCK_CLOEXEC still gets a socket instead of EINVAL.
|
||||
type &= 0xFF;
|
||||
if (type != SOCK_STREAM)
|
||||
if (domain == AF_UNIX && type != SOCK_STREAM)
|
||||
return -EPROTOTYPE;
|
||||
if (domain == AF_INET && type != SOCK_STREAM && type != SOCK_DGRAM)
|
||||
return -EPROTOTYPE;
|
||||
if (protocol != 0)
|
||||
return -EPROTONOSUPPORT;
|
||||
|
|
@ -134,6 +138,36 @@ SYSCALL_DEFINE(socket)
|
|||
return fd;
|
||||
}
|
||||
|
||||
// Pull an address and port out of a user `struct sockaddr_in`.
|
||||
//
|
||||
// Both come back in network byte order and stay that way: lwIP wants them that
|
||||
// way, so converting here and back again would be two swaps that cancel.
|
||||
static int
|
||||
fetch_sin(addr_t uaddr, int ulen, uint *addr, ushort *port)
|
||||
{
|
||||
struct sockaddr_in sa;
|
||||
|
||||
if (ulen < (int)sizeof(sa))
|
||||
return -EINVAL;
|
||||
if (copy_from_user(&sa, uaddr, sizeof(sa)) < 0)
|
||||
return -EFAULT;
|
||||
if (sa.sin_family != AF_INET)
|
||||
return -EAFNOSUPPORT;
|
||||
|
||||
*addr = sa.sin_addr;
|
||||
*port = sa.sin_port;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Network byte order to host, for a port. The kernel is little-endian and the
|
||||
// wire is big-endian; lwIP's own ntohs is not reachable from here without
|
||||
// dragging its headers into the syscall layer.
|
||||
static ushort
|
||||
port_from_net(ushort p)
|
||||
{
|
||||
return (ushort)(((p & 0xFF) << 8) | ((p >> 8) & 0xFF));
|
||||
}
|
||||
|
||||
SYSCALL_DEFINE(bind)
|
||||
{
|
||||
struct socket *s;
|
||||
|
|
@ -146,6 +180,14 @@ SYSCALL_DEFINE(bind)
|
|||
argaddr(1, &uaddr);
|
||||
argint(2, &ulen);
|
||||
|
||||
if (sock_domain(s) == AF_INET) {
|
||||
uint ip;
|
||||
ushort port;
|
||||
if ((r = fetch_sin(uaddr, ulen, &ip, &port)) < 0)
|
||||
return r;
|
||||
return sockbind_in(s, ip, port_from_net(port));
|
||||
}
|
||||
|
||||
if ((r = fetch_sun_path(uaddr, ulen, path)) < 0)
|
||||
return r;
|
||||
return sockbind(s, path);
|
||||
|
|
@ -174,6 +216,14 @@ SYSCALL_DEFINE(connect)
|
|||
argaddr(1, &uaddr);
|
||||
argint(2, &ulen);
|
||||
|
||||
if (sock_domain(s) == AF_INET) {
|
||||
uint ip;
|
||||
ushort port;
|
||||
if ((r = fetch_sin(uaddr, ulen, &ip, &port)) < 0)
|
||||
return r;
|
||||
return sockconnect_in(s, ip, port_from_net(port));
|
||||
}
|
||||
|
||||
if ((r = fetch_sun_path(uaddr, ulen, path)) < 0)
|
||||
return r;
|
||||
return sockconnect(s, path);
|
||||
|
|
|
|||
Loading…
Reference in a new issue