net: the socket layer stops saying yes to things it did not do, and ping works
setsockopt was `return 0;` and getsockopt wrote a zero for whatever was asked. The comment said it was so a server setting SO_REUSEADDR and treating failure as fatal would not fail. That trade is the wrong way round: a caller told "no" adapts, a caller told "yes" changes its behaviour on the strength of it -- sets SO_RCVTIMEO and then blocks forever in a read it believes will time out, sets TCP_NODELAY and reports latency numbers that measure Nagle. The failure is silent, remote from the call, and indistinguishable from a bug in the stack. Six options are honoured now and everything else is ENOPROTOOPT. SO_REUSEADDR, SO_KEEPALIVE, SO_BROADCAST and TCP_NODELAY reach lwIP's pcb; SO_SNDBUF/SO_RCVBUF are accepted and report the real size on read. SO_REUSEADDR is implemented rather than refused precisely because of the original comment's concern -- it is the one option a server really does set and really does check. accept4 becomes its own table entry rather than an alias for accept. It has to be: accept takes three arguments, so a shared handler reading a fourth register reads whatever the caller last left there and invents a SOCK_NONBLOCK out of stack litter. SOCK_CLOEXEC and SOCK_NONBLOCK are now honoured by socket(), socketpair() and accept4() -- a dropped CLOEXEC leaks a listening socket across every exec, and a dropped NONBLOCK has the caller block forever in what it believes is a poll loop. ICMP ping sockets: socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP), on lwIP's raw API, with the callback stripping the variable-length IP header. Only echo replies are eaten, and that restriction is load-bearing -- raw_input runs before lwIP's own icmp_input, so consuming every ICMP message would swallow the echo *requests* and this machine would silently stop answering pings from outside. Loopback, so 127.0.0.1 is a real destination. LWIP_NETIF_LOOPBACK is the half that is easy to miss: without it a packet addressed to an interface's own address goes out of the wire and never comes back. With NO_SYS=1 nothing drains the loopback queue, so the network thread calls netif_poll_all(). cmd/ping is first-party rather than toybox's, and the reason is a limitation not a preference: toybox reads replies with recvmsg(2) and wants the TTL through an IP_RECVTTL control message, and SchOS has neither. recvmsg is item 1 of docs/plan/foundations.md, where it is wanted for SCM_RIGHTS anyway, and should not be rushed to make one applet run. Two bugs found on the way. Every AF_INET socket failed with ENFILE until DHCP bound, because inet_new() demanded an *address* before handing out a socket and reported it as "file table overflow"; net_is_ready() (lwIP up) is now distinct from net_is_up() (DHCP bound). And that bug was hiding a bad test: socktest asserted "AF_INET is refused, not faked", true when written, false since AF_INET landed, and green the whole time because the suite runs before DHCP binds and the ENFILE made the refusal look real. Fixing the kernel is what turned it red. Verified: ping to 127.0.0.1, localhost, the gateway, 1.1.1.1 and 9.9.9.9, all 0% loss; socktest ALL CHECKS PASSED with ten new checks, including a setsockopt round trip and an unknown option that must be refused -- a permissive stub cannot pass both at once. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
This commit is contained in:
parent
fc0a7c6f4e
commit
c362e308d0
13 changed files with 903 additions and 36 deletions
|
|
@ -159,6 +159,88 @@ sockdup(struct socket *s)
|
|||
release(&stab.lock);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Socket options
|
||||
//
|
||||
// Only the options that are genuinely honoured appear here; everything else is
|
||||
// answered ENOPROTOOPT by the switch's default. That is deliberate and it is a
|
||||
// change of policy: both calls used to succeed unconditionally, which is the
|
||||
// worst of the three possible answers. A caller told "no" adapts; a caller told
|
||||
// "yes" falsely proceeds on the strength of it -- setting SO_RCVTIMEO and then
|
||||
// blocking forever in a read it believes will time out.
|
||||
//
|
||||
// The flags are recorded here as well as pushed into lwIP so that getsockopt
|
||||
// has one answer rather than two, and so that an AF_UNIX socket -- which has no
|
||||
// pcb and for which none of these mean anything -- can still give back what was
|
||||
// set, as POSIX requires.
|
||||
int
|
||||
sockopt_set(struct socket *s, int level, int name, int val)
|
||||
{
|
||||
int r = 0;
|
||||
|
||||
acquire(&stab.lock);
|
||||
if (level == SOL_SOCKET) {
|
||||
switch (name) {
|
||||
case SO_REUSEADDR: s->o_reuseaddr = !!val; break;
|
||||
case SO_KEEPALIVE: s->o_keepalive = !!val; break;
|
||||
case SO_BROADCAST: s->o_broadcast = !!val; break;
|
||||
// Accepted and left at the size we have. Not the same lie as before:
|
||||
// Linux is explicitly free to clamp these, and sockopt_get below reports
|
||||
// the real figure rather than the requested one.
|
||||
case SO_SNDBUF:
|
||||
case SO_RCVBUF: release(&stab.lock); return 0;
|
||||
default: release(&stab.lock); return -ENOPROTOOPT;
|
||||
}
|
||||
} else if (level == IPPROTO_TCP && name == TCP_NODELAY) {
|
||||
if (s->type != SOCK_STREAM) { release(&stab.lock); return -EINVAL; }
|
||||
s->o_nodelay = !!val;
|
||||
} else {
|
||||
release(&stab.lock);
|
||||
return -ENOPROTOOPT;
|
||||
}
|
||||
int inet = (s->domain == AF_INET);
|
||||
void *h = s->inet;
|
||||
release(&stab.lock);
|
||||
|
||||
// Outside the table lock: inet_setopt takes the lwIP lock, which is a
|
||||
// sleeplock, and the same ordering rule the rest of this file follows.
|
||||
if (inet && h)
|
||||
r = inet_setopt(h, level, name, val);
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
sockopt_get(struct socket *s, int level, int name, int *out)
|
||||
{
|
||||
int r = 0;
|
||||
|
||||
acquire(&stab.lock);
|
||||
if (level == IPPROTO_TCP && name == TCP_NODELAY) {
|
||||
*out = s->o_nodelay;
|
||||
} else if (level != SOL_SOCKET) {
|
||||
r = -ENOPROTOOPT;
|
||||
} else {
|
||||
switch (name) {
|
||||
case SO_TYPE: *out = s->type; break;
|
||||
case SO_ACCEPTCONN: *out = (s->state == SS_LISTENING); break;
|
||||
case SO_REUSEADDR: *out = s->o_reuseaddr; break;
|
||||
case SO_KEEPALIVE: *out = s->o_keepalive; break;
|
||||
case SO_BROADCAST: *out = s->o_broadcast; break;
|
||||
// The real buffer, not a number somebody asked for.
|
||||
case SO_SNDBUF:
|
||||
case SO_RCVBUF: *out = SOCKBUF; break;
|
||||
// An honest zero rather than the blanket one it replaces: connect() here
|
||||
// is blocking and reports failure as its own return value, so there is no
|
||||
// deferred error for SO_ERROR to carry. It becomes a real field the day a
|
||||
// non-blocking connect exists.
|
||||
case SO_ERROR: *out = 0; break;
|
||||
default: r = -ENOPROTOOPT; break;
|
||||
}
|
||||
}
|
||||
release(&stab.lock);
|
||||
return r;
|
||||
}
|
||||
|
||||
const char *
|
||||
sockname(struct socket *s)
|
||||
{
|
||||
|
|
@ -254,7 +336,7 @@ sockwrite(struct socket *s, char *addr, int n)
|
|||
// ---------------------------------------------------------------------------
|
||||
|
||||
struct socket *
|
||||
socknew(int domain, int type)
|
||||
socknew(int domain, int type, int proto)
|
||||
{
|
||||
struct socket *s;
|
||||
|
||||
|
|
@ -276,7 +358,7 @@ socknew(int domain, int type)
|
|||
if(domain == AF_INET){
|
||||
// Outside the table lock: inet_new takes the lwIP lock, which is a
|
||||
// sleeplock, and sleeping while holding a spinlock is a panic.
|
||||
s->inet = inet_new(type);
|
||||
s->inet = inet_new(type, proto);
|
||||
if(s->inet == 0){
|
||||
sockclose(s);
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Reference in a new issue