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
270
cmd/ping/main.c
Normal file
270
cmd/ping/main.c
Normal file
|
|
@ -0,0 +1,270 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
// ping -- ICMP echo, over a SchOS ping socket.
|
||||
//
|
||||
// This is a first-party program rather than toybox's ping, and the reason is
|
||||
// worth recording because it is not "ours is better". toybox's ping reads its
|
||||
// replies with recvmsg(2) and asks for the TTL through an IP_RECVTTL control
|
||||
// message, and SchOS has neither: no recvmsg, no ancillary data. Both are owed
|
||||
// -- recvmsg is item 1 of docs/plan/foundations.md, where it is wanted for
|
||||
// SCM_RIGHTS -- and neither should be rushed to make one applet run.
|
||||
//
|
||||
// So this uses sendto/recvfrom, which the kernel does have, and proves the ICMP
|
||||
// socket end to end in the meantime. When recvmsg lands, toybox's ping will
|
||||
// work and this can go.
|
||||
//
|
||||
// A "ping socket" is socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP): the kernel owns
|
||||
// the IP header and the caller reads and writes the ICMP message alone. See
|
||||
// inet_icmp_recv_cb in kernel/net/inet.c.
|
||||
|
||||
#include "types.h"
|
||||
#include "user.h"
|
||||
#include "socket.h"
|
||||
|
||||
#define ICMP_ECHO 8
|
||||
#define ICMP_ECHOREPLY 0
|
||||
#define DEFAULT_COUNT 4
|
||||
#define PAYLOAD 56 // what every other ping sends
|
||||
|
||||
struct icmphdr {
|
||||
uchar type;
|
||||
uchar code;
|
||||
ushort checksum;
|
||||
ushort id;
|
||||
ushort seq;
|
||||
};
|
||||
|
||||
// lib/ is raw syscall stubs -- no libc, so not even memset. Everything here is
|
||||
// hand-rolled for the same reason cmd/schinit is.
|
||||
static void
|
||||
zero(void *p, int n)
|
||||
{
|
||||
char *b = p;
|
||||
while (n-- > 0) *b++ = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
puts_(const char *s)
|
||||
{
|
||||
int n = 0;
|
||||
while (s[n]) n++;
|
||||
write(1, (void *)s, n);
|
||||
}
|
||||
|
||||
static void
|
||||
putn_(int v)
|
||||
{
|
||||
char buf[12];
|
||||
int i = 0, neg = 0;
|
||||
if (v < 0) { neg = 1; v = -v; }
|
||||
if (v == 0) buf[i++] = '0';
|
||||
while (v) { buf[i++] = (char)('0' + v % 10); v /= 10; }
|
||||
if (neg) buf[i++] = '-';
|
||||
while (i--) write(1, &buf[i], 1);
|
||||
}
|
||||
|
||||
// The Internet checksum: one's complement of the one's complement sum of every
|
||||
// 16-bit word. Computed over the ICMP message with the checksum field zeroed,
|
||||
// which is why it is filled in last.
|
||||
static ushort
|
||||
cksum(const void *p, int n)
|
||||
{
|
||||
const uchar *b = p;
|
||||
uint sum = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i + 1 < n; i += 2)
|
||||
sum += (uint)((b[i] << 8) | b[i + 1]);
|
||||
if (i < n)
|
||||
sum += (uint)(b[i] << 8);
|
||||
while (sum >> 16)
|
||||
sum = (sum & 0xFFFF) + (sum >> 16);
|
||||
return (ushort)(~sum & 0xFFFF);
|
||||
}
|
||||
|
||||
static ushort
|
||||
hton16(ushort v)
|
||||
{
|
||||
return (ushort)((v >> 8) | (v << 8));
|
||||
}
|
||||
|
||||
// Dotted quad to a network-byte-order address. Returns 0 on a malformed one,
|
||||
// which is indistinguishable from 0.0.0.0 and is fine: nobody pings that.
|
||||
static uint
|
||||
parse_ip(const char *s, int *ok)
|
||||
{
|
||||
uint out = 0;
|
||||
int part, i, digits;
|
||||
|
||||
*ok = 0;
|
||||
for (i = 0; i < 4; i++) {
|
||||
part = 0; digits = 0;
|
||||
while (*s >= '0' && *s <= '9') {
|
||||
part = part * 10 + (*s - '0');
|
||||
if (part > 255) return 0;
|
||||
s++; digits++;
|
||||
}
|
||||
if (digits == 0) return 0;
|
||||
out |= (uint)part << (8 * i); // little-endian host, so LSB first
|
||||
if (i < 3) {
|
||||
if (*s != '.') return 0;
|
||||
s++;
|
||||
}
|
||||
}
|
||||
if (*s != '\0') return 0;
|
||||
*ok = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
static void
|
||||
print_ip(uint addr)
|
||||
{
|
||||
for (int i = 0; i < 4; i++) {
|
||||
putn_((int)((addr >> (8 * i)) & 0xFF));
|
||||
if (i < 3) puts_(".");
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
char target[64];
|
||||
int count = DEFAULT_COUNT;
|
||||
int ok, sent = 0, recvd = 0;
|
||||
|
||||
if (argc < 2) {
|
||||
puts_("usage: ping <host> [count]\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
// "localhost" without a resolver. The kernel has no DNS client reachable
|
||||
// from userland, and the one name worth special-casing is the one that
|
||||
// always means the same thing.
|
||||
const char *host = argv[1];
|
||||
if (host[0] == 'l' && host[1] == 'o' && host[2] == 'c' &&
|
||||
host[3] == 'a' && host[4] == 'l' && host[5] == 'h' &&
|
||||
host[6] == 'o' && host[7] == 's' && host[8] == 't' && host[9] == 0)
|
||||
host = "127.0.0.1";
|
||||
|
||||
{ int i = 0; while (host[i] && i < 63) { target[i] = host[i]; i++; } target[i] = 0; }
|
||||
|
||||
uint addr = parse_ip(target, &ok);
|
||||
if (!ok) {
|
||||
puts_("ping: not a dotted-quad address: ");
|
||||
puts_(argv[1]);
|
||||
puts_("\nping: there is no resolver yet; use an address\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
if (argc > 2) {
|
||||
count = 0;
|
||||
for (const char *p = argv[2]; *p >= '0' && *p <= '9'; p++)
|
||||
count = count * 10 + (*p - '0');
|
||||
if (count <= 0) count = DEFAULT_COUNT;
|
||||
}
|
||||
|
||||
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
|
||||
if (fd < 0) {
|
||||
puts_("ping: socket: ");
|
||||
putn_(fd);
|
||||
puts_("\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
puts_("PING ");
|
||||
print_ip(addr);
|
||||
puts_(" ");
|
||||
putn_(PAYLOAD);
|
||||
puts_(" bytes of data.\n");
|
||||
|
||||
ushort id = (ushort)getpid();
|
||||
|
||||
for (int seq = 1; seq <= count; seq++) {
|
||||
char out[sizeof(struct icmphdr) + PAYLOAD];
|
||||
struct icmphdr *ih = (struct icmphdr *)out;
|
||||
|
||||
zero(out, (int)sizeof(out));
|
||||
ih->type = ICMP_ECHO;
|
||||
ih->code = 0;
|
||||
ih->id = hton16(id);
|
||||
ih->seq = hton16((ushort)seq);
|
||||
for (int i = 0; i < PAYLOAD; i++)
|
||||
out[sizeof(*ih) + i] = (char)('a' + (i % 26));
|
||||
ih->checksum = 0;
|
||||
// The checksum is stored as it is computed: cksum() already works in
|
||||
// network order, so byte-swapping it here would send a wrong one.
|
||||
ih->checksum = hton16(cksum(out, (int)sizeof(out)));
|
||||
|
||||
struct sockaddr_in sa;
|
||||
zero(&sa, (int)sizeof(sa));
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_port = 0;
|
||||
sa.sin_addr = addr;
|
||||
|
||||
int t0 = uptime();
|
||||
int n = sendto(fd, out, (int)sizeof(out), 0,
|
||||
(struct sockaddr *)&sa, sizeof(sa));
|
||||
if (n < 0) {
|
||||
puts_("ping: sendto: ");
|
||||
putn_(n);
|
||||
puts_("\n");
|
||||
break;
|
||||
}
|
||||
sent++;
|
||||
|
||||
// Wait for a reply, but not forever. uptime() counts 10 ms ticks, so
|
||||
// 100 of them is one second -- the same timeout every ping uses.
|
||||
char in[256];
|
||||
int got = -1, waited = 0;
|
||||
struct sockaddr_in from;
|
||||
int fromlen = sizeof(from);
|
||||
|
||||
while (waited < 100) {
|
||||
got = recvfrom(fd, in, (int)sizeof(in), 0,
|
||||
(struct sockaddr *)&from, &fromlen);
|
||||
if (got > 0) break;
|
||||
sleep(1);
|
||||
waited = uptime() - t0;
|
||||
}
|
||||
|
||||
if (got <= 0) {
|
||||
puts_("Request timeout for icmp_seq ");
|
||||
putn_(seq);
|
||||
puts_("\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
struct icmphdr *rh = (struct icmphdr *)in;
|
||||
if (rh->type != ICMP_ECHOREPLY || hton16(rh->id) != id) {
|
||||
puts_("ping: reply for somebody else, ignored\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
recvd++;
|
||||
putn_(got);
|
||||
puts_(" bytes from ");
|
||||
print_ip(from.sin_addr);
|
||||
puts_(": icmp_seq=");
|
||||
putn_((int)hton16(rh->seq));
|
||||
puts_(" time=");
|
||||
putn_((uptime() - t0) * 10);
|
||||
puts_(" ms\n");
|
||||
|
||||
if (seq < count)
|
||||
sleep(100); // one second between packets
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
puts_("--- ");
|
||||
print_ip(addr);
|
||||
puts_(" ping statistics ---\n");
|
||||
putn_(sent);
|
||||
puts_(" packets transmitted, ");
|
||||
putn_(recvd);
|
||||
puts_(" received, ");
|
||||
putn_(sent ? (sent - recvd) * 100 / sent : 0);
|
||||
puts_("% packet loss\n");
|
||||
|
||||
exit();
|
||||
}
|
||||
|
|
@ -150,7 +150,23 @@ test_errors(void)
|
|||
"connect() to a name nobody bound fails");
|
||||
close(s);
|
||||
|
||||
check(socket(AF_INET, SOCK_STREAM, 0) < 0, "AF_INET is refused, not faked");
|
||||
// This used to assert that AF_INET was *refused*, which was true when this
|
||||
// file was written and stopped being true the day AF_INET landed. It kept
|
||||
// passing anyway, and the reason is worth keeping: socktest runs seconds
|
||||
// after boot, DHCP had not bound yet, and inet_new() refused every socket
|
||||
// until it did -- reported as ENFILE. So the check saw "< 0" and called it
|
||||
// a pass for a reason that had nothing to do with what it was testing.
|
||||
//
|
||||
// Two lessons, both already written down elsewhere in this tree and both
|
||||
// learned again here: a test that asserts a *failure* has to say which
|
||||
// failure, and a green suite is not evidence until something has checked
|
||||
// that it can go red.
|
||||
//
|
||||
// What is left are the refusals that are unconditionally true, whatever the
|
||||
// link is doing.
|
||||
check(socket(99, SOCK_STREAM, 0) < 0, "an unknown address family is refused");
|
||||
check(socket(AF_INET, SOCK_STREAM, 99) < 0,
|
||||
"AF_INET with a nonsense protocol is refused");
|
||||
check(socket(AF_UNIX, SOCK_DGRAM, 0) < 0, "SOCK_DGRAM is refused, not faked");
|
||||
|
||||
// read/write on a plain file descriptor must not be mistaken for a socket,
|
||||
|
|
@ -283,6 +299,109 @@ test_big(void)
|
|||
check(ok, "and in the right order");
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Socket options.
|
||||
//
|
||||
// Both calls used to succeed unconditionally: setsockopt was `return 0;` and
|
||||
// getsockopt wrote a zero for whatever was asked. So the only check that proves
|
||||
// anything is a *round trip* -- set a value, read it back, and see the value
|
||||
// rather than a zero -- plus one option that must be refused. A stub passes
|
||||
// "does setsockopt return 0"; it cannot pass both of these at once.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#define T_SOL_SOCKET 1
|
||||
#define T_SO_REUSEADDR 2
|
||||
#define T_SO_TYPE 3
|
||||
#define T_SO_RCVBUF 8
|
||||
#define T_SO_NOSUCH 0x7ffe // not an option Linux defines either
|
||||
|
||||
static void
|
||||
test_sockopt(void)
|
||||
{
|
||||
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
int val, len;
|
||||
|
||||
puts_("-- socket options\n");
|
||||
if (fd < 0) { check(0, "socket()"); return; }
|
||||
|
||||
// SO_TYPE must report what this socket actually is, not zero.
|
||||
val = -1; len = sizeof(val);
|
||||
check(getsockopt(fd, T_SOL_SOCKET, T_SO_TYPE, &val, &len) == 0 &&
|
||||
val == SOCK_STREAM, "getsockopt SO_TYPE says SOCK_STREAM");
|
||||
|
||||
// The round trip. A stub that ignores the set and zeroes the get fails here.
|
||||
val = 1;
|
||||
check(setsockopt(fd, T_SOL_SOCKET, T_SO_REUSEADDR, &val, sizeof(val)) == 0,
|
||||
"setsockopt SO_REUSEADDR accepted");
|
||||
val = -1; len = sizeof(val);
|
||||
check(getsockopt(fd, T_SOL_SOCKET, T_SO_REUSEADDR, &val, &len) == 0 &&
|
||||
val == 1, "getsockopt SO_REUSEADDR reads back 1");
|
||||
|
||||
// And back off again, so the answer is not just "always non-zero".
|
||||
val = 0;
|
||||
setsockopt(fd, T_SOL_SOCKET, T_SO_REUSEADDR, &val, sizeof(val));
|
||||
val = -1; len = sizeof(val);
|
||||
check(getsockopt(fd, T_SOL_SOCKET, T_SO_REUSEADDR, &val, &len) == 0 &&
|
||||
val == 0, "and reads back 0 once cleared");
|
||||
|
||||
// A real buffer size, not zero.
|
||||
val = -1; len = sizeof(val);
|
||||
check(getsockopt(fd, T_SOL_SOCKET, T_SO_RCVBUF, &val, &len) == 0 && val > 0,
|
||||
"getsockopt SO_RCVBUF is a real size");
|
||||
|
||||
// The refusal. This is the half that a permissive stub cannot fake.
|
||||
val = 1;
|
||||
check(setsockopt(fd, T_SOL_SOCKET, T_SO_NOSUCH, &val, sizeof(val)) < 0,
|
||||
"setsockopt of an unknown option is refused");
|
||||
len = sizeof(val);
|
||||
check(getsockopt(fd, T_SOL_SOCKET, T_SO_NOSUCH, &val, &len) < 0,
|
||||
"getsockopt of an unknown option is refused");
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// SOCK_NONBLOCK, end to end.
|
||||
//
|
||||
// Checking that socket() accepts the flag proves nothing -- it was accepted
|
||||
// before, and discarded. What proves it is a read that *returns* on a socket
|
||||
// with nothing in it, where a blocking socket would sleep forever.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#define T_SOCK_NONBLOCK 0x800
|
||||
#define T_F_GETFL 3
|
||||
#define T_O_NONBLOCK 04000
|
||||
|
||||
static void
|
||||
test_nonblock(void)
|
||||
{
|
||||
int sv[2];
|
||||
char c;
|
||||
|
||||
puts_("-- SOCK_NONBLOCK\n");
|
||||
|
||||
if (socketpair(AF_UNIX, SOCK_STREAM | T_SOCK_NONBLOCK, 0, sv) < 0) {
|
||||
check(0, "socketpair(SOCK_NONBLOCK)");
|
||||
return;
|
||||
}
|
||||
|
||||
check((fcntl(sv[0], T_F_GETFL, 0) & T_O_NONBLOCK) != 0,
|
||||
"fcntl(F_GETFL) reports O_NONBLOCK");
|
||||
|
||||
// Nothing has been written, so a blocking read would never return. If this
|
||||
// test hangs, the flag was dropped -- which is exactly what it used to be.
|
||||
check(read(sv[0], &c, 1) < 0, "read on an empty non-blocking socket returns");
|
||||
|
||||
// And it still carries data, so the flag did not break the socket.
|
||||
write(sv[1], "x", 1);
|
||||
c = 0;
|
||||
check(read(sv[0], &c, 1) == 1 && c == 'x', "and still delivers a byte");
|
||||
|
||||
close(sv[0]);
|
||||
close(sv[1]);
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
|
|
@ -293,6 +412,8 @@ main(void)
|
|||
test_eof();
|
||||
test_socketpair();
|
||||
test_big();
|
||||
test_sockopt();
|
||||
test_nonblock();
|
||||
|
||||
if (failures == 0)
|
||||
puts_("socktest: ALL CHECKS PASSED\n");
|
||||
|
|
|
|||
Loading…
Reference in a new issue