#define PLANCK_FEATURE_IPV4 1 #include #include #include #include #include /* * Static IPv4 from the kernel command line — the fleet contract (the fleet's VM definition): * * planck.iface=eth0 planck.ipv4=10.0.2.15/24 planck.gateway=10.0.2.2 * * DIRECT IOCTLS, NOTHING ELSE CAN WORK HERE: PID 1 configures the interface the way * ifconfig itself does — SIOCSIFADDR / SIOCSIFNETMASK / SIOCSIFFLAGS / SIOCADDRT on an * AF_INET socket — because at ::builtin:ipv4 time nothing else has started yet. * * It parses the cmdline, applies it, logs, and RETURNS: nothing here may block, PID 1 runs * it before anything else. * * LOOPBACK FIRST, ALWAYS: lo comes up before anything else (the kernel gives it 127.0.0.1/8 * itself when it goes up). Kernel services talk to themselves over it: nfsd registers with * rpcbind on 127.0.0.1 and, with lo down, waits out every retry (~4 minutes at start). * * Missing planck.ipv4= is not an error: the box simply boots without a pre-configured * interface (say, under a harness that wants DHCP) and says so on the console. */ static int ipv4_configured; /* Bring lo up: one flag; the kernel assigns 127.0.0.1/8 on the way. */ static void planck_lo_up(void) { struct ifreq ifr = { 0 }; int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (fd < 0) return; strncpy(ifr.ifr_name, "lo", sizeof(ifr.ifr_name) - 1); if (ioctl(fd, SIOCGIFFLAGS, &ifr) == 0) { ifr.ifr_flags |= IFF_UP; if (ioctl(fd, SIOCSIFFLAGS, &ifr) == 0) log_msg("planck-net: lo up"); } close(fd); } /* Parse a dotted quad at *p; advances *p past it. Returns 0 on success. */ static int parse_quad(const char **p, unsigned int *out) { unsigned int addr = 0; int octet; for (octet = 0; octet < 4; octet++) { unsigned int v = 0; int digits = 0; while (**p >= '0' && **p <= '9' && digits < 3) { v = v * 10 + (unsigned int)(**p - '0'); (*p)++; digits++; } if (!digits || v > 255) return -1; addr = (addr << 8) | v; if (octet < 3) { if (**p != '.') return -1; (*p)++; } } *out = addr; return 0; } /* Find "planck.=" in the cmdline and copy its value (space-terminated). */ static int cmdline_value(const char *cmdline, const char *key, char *out, size_t out_size) { const char *p = cmdline; size_t key_len = strlen(key); size_t at = 0; while ((p = strstr(p, key)) != NULL) { if ((p == cmdline || p[-1] == ' ') && p[key_len] == '=') { p += key_len + 1; while (*p && *p != ' ' && *p != '\n' && at + 1 < out_size) out[at++] = *p++; out[at] = 0; return at > 0 ? 0 : -1; } p += key_len; } return -1; } static void planck_ipv4_configure_from_cmdline(void) { char cmdline[MAX_LINE]; char iface[MAX_TTY] = "eth0"; char value[64]; char msg[128]; unsigned int addr, gateway, prefix = 0, netmask; const char *p; struct ifreq ifr = { 0 }; struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr; struct rtentry rt = { 0 }; int fd, cmdline_fd; ssize_t n; if (ipv4_configured) return; ipv4_configured = 1; planck_lo_up(); cmdline_fd = open("/proc/cmdline", O_RDONLY); if (cmdline_fd < 0) { log_msg("planck-net: no /proc/cmdline"); return; } n = read(cmdline_fd, cmdline, sizeof(cmdline) - 1); close(cmdline_fd); if (n <= 0) return; cmdline[n] = 0; if (cmdline_value(cmdline, "planck.ipv4", value, sizeof(value)) < 0) { log_msg("planck-net: no planck.ipv4= on cmdline, leaving interfaces alone"); return; } p = value; if (parse_quad(&p, &addr) < 0 || *p != '/') { log_msg("planck-net: bad planck.ipv4 (want a.b.c.d/prefix)"); return; } p++; while (*p >= '0' && *p <= '9') prefix = prefix * 10 + (unsigned int)(*p++ - '0'); if (prefix < 1 || prefix > 32) { log_msg("planck-net: bad planck.ipv4 prefix"); return; } netmask = prefix == 32 ? 0xFFFFFFFFu : ~(0xFFFFFFFFu >> prefix); cmdline_value(cmdline, "planck.iface", iface, sizeof(iface)); /* Network ioctls are family-level; a required UDP descriptor is sufficient. */ fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (fd < 0) return; strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1); sin->sin_family = AF_INET; sin->sin_addr.s_addr = htonl(addr); if (ioctl(fd, SIOCSIFADDR, &ifr) < 0) { planck_cat(msg, sizeof(msg), planck_cat(msg, sizeof(msg), 0, "planck-net: no "), iface); log_msg(msg); close(fd); return; } sin->sin_addr.s_addr = htonl(netmask); ioctl(fd, SIOCSIFNETMASK, &ifr); ioctl(fd, SIOCGIFFLAGS, &ifr); ifr.ifr_flags |= IFF_UP; ioctl(fd, SIOCSIFFLAGS, &ifr); if (cmdline_value(cmdline, "planck.gateway", value, sizeof(value)) == 0) { p = value; if (parse_quad(&p, &gateway) == 0) { ((struct sockaddr_in *)&rt.rt_gateway)->sin_family = AF_INET; ((struct sockaddr_in *)&rt.rt_gateway)->sin_addr.s_addr = htonl(gateway); ((struct sockaddr_in *)&rt.rt_dst)->sin_family = AF_INET; ((struct sockaddr_in *)&rt.rt_genmask)->sin_family = AF_INET; rt.rt_flags = RTF_UP | RTF_GATEWAY; ioctl(fd, SIOCADDRT, &rt); /* Announce the interface: one UDP datagram to the gateway's discard port so * the L2 fabric learns our MAC immediately — qemu slirp's hostfwd cannot * deliver inbound connections until the guest has transmitted once, and a * real switch benefits from early address learning the same way. */ { unsigned char announce = 0; struct sockaddr_in dst = { .sin_family = AF_INET, .sin_port = htons(9), .sin_addr.s_addr = htonl(gateway), }; sendto(fd, &announce, sizeof(announce), 0, (struct sockaddr *)&dst, sizeof(dst)); } } } close(fd); { size_t at = planck_cat(msg, sizeof(msg), 0, "planck-net: "); at = planck_cat(msg, sizeof(msg), at, iface); at = planck_cat(msg, sizeof(msg), at, " "); at = planck_cat(msg, sizeof(msg), at, value[0] ? "configured, gw set" : "configured"); log_msg(msg); } }