aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / tools / ck_imac_bsdp.c
| 1 | /* ck_imac_bsdp.c -- LAB-ONLY Apple NetBoot answerer for the 2017 iMac: lets the iMac start |
| 2 | * NETLDR.EFI (native/boot/net_loader.c) straight from the Spark, with no stick, when it is |
| 3 | * powered on with the N key held. Hosted C (POSIX sockets), tooling only. No Python. |
| 4 | * |
| 5 | * Two small servers in one process: |
| 6 | * BSDP (UDP 67) Apple's Boot Service Discovery Protocol: DHCP INFORM packets with vendor |
| 7 | * class "AAPLBSDPC/..." and BSDP sub-options in option 43. INFORM[LIST] is |
| 8 | * answered with one image (AIENOS, the default); INFORM[SELECT] naming that |
| 9 | * image and this server is answered with the boot file name and this |
| 10 | * server's address (siaddr). Nothing else is answered: never DISCOVER or |
| 11 | * REQUEST (the router stays the DHCP server), never another vendor class. |
| 12 | * TFTP (UDP 69) read-only, one file: RRQ for BOOT_NAME (octet mode) gets the booter; the |
| 13 | * blksize and tsize options (RFC 2348/2349) are honoured. Every other request |
| 14 | * gets an error. Nothing is ever written. |
| 15 | * Wire reference: Apple bootp project (bsdp.h, bootpd) as traced in "Analysis of the Use of |
| 16 | * the Boot Server Discovery Protocol in NetBoot" (afp548): option 43 sub-options |
| 17 | * 1 message type (1 LIST, 2 SELECT, 3 FAILED), 3 server id, 4 priority, 5 reply port, |
| 18 | * 7 default image, 8 selected image, 9 image list (id[4] len[1] name), 130 machine name. |
| 19 | * Answers are DHCP ACKs (also carrying subnet mask and router) |
| 20 | * with option 60 "AAPLBSDPC" sent to the client's ciaddr, port 68 or its reply port. |
| 21 | * |
| 22 | * UNAUTHENTICATED: the iMac firmware runs whatever booter it is handed. Anyone on the LAN can |
| 23 | * answer first. What makes this safe enough for the lab is the booter: NETLDR.EFI starts only |
| 24 | * kernels signed with the lab key. A forged booter is not prevented here. |
| 25 | * |
| 26 | * ck_imac_bsdp serve --booter FILE --server-ip A.B.C.D [--mac XX:..] [--bsdp-port N] |
| 27 | * [--tftp-port N] [--reply-source-port N] [--seconds N] |
| 28 | * --mac answers only that client. Ports 67/69 need CAP_NET_BIND_SERVICE (setcap) or root. |
| 29 | * --reply-source-port N sends the BSDP answers (and the --also-broadcast copy) from UDP port N |
| 30 | * instead of from the port the requests arrive on (--bsdp-port). By default the answers leave |
| 31 | * from the listening socket, source port = --bsdp-port, exactly as before. A different N binds a |
| 32 | * separate send-only socket; requests are still read on --bsdp-port and TFTP is unchanged. |
| 33 | * This is a LAN compatibility setting, not a protocol rule: on the lab LAN the packets with |
| 34 | * source port 67 to the iMac's port 68 were lost between the Spark and the iMac, while the |
| 35 | * same answers from source port 1067 arrived (confirmed on this one iMac, 2026-10-30). Do not |
| 36 | * read it as "BSDP answers must come from a port other than 67", and this is not a generic |
| 37 | * DHCP service. N must differ from --tftp-port; N equal to --bsdp-port behaves as the default. |
| 38 | * Log lines go to stdout with a UTC timestamp. Exit 0 at the deadline, 2 usage, 3 socket. */ |
| 39 | #define _DEFAULT_SOURCE |
| 40 | #include <arpa/inet.h> |
| 41 | #include <errno.h> |
| 42 | #include <fcntl.h> |
| 43 | #include <netinet/in.h> |
| 44 | #include <poll.h> |
| 45 | #include <stdarg.h> |
| 46 | #include <stdint.h> |
| 47 | #include <stdio.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <string.h> |
| 50 | #include <strings.h> |
| 51 | #include <sys/socket.h> |
| 52 | #include <sys/stat.h> |
| 53 | #include <time.h> |
| 54 | #include <unistd.h> |
| 55 | |
| 56 | #define BOOT_NAME "aienos/netldr.efi" |
| 57 | #define IMAGE_NAME "AIENOS" |
| 58 | /* Image id: install image (bit 31: the firmware fetches the booter and runs it, no network |
| 59 | * disk), kind 1 (Mac OS X class) in bits 24..30, index 1 (< 4096: local to this server). |
| 60 | * The working ISC/FOG Intel Mac configs all use the install bit. */ |
| 61 | #define IMAGE_ID 0x81000001u |
| 62 | #define DHCP_MAGIC 0x63825363u |
| 63 | #define BOOTP_MIN 240 |
| 64 | /* Reply buffer: the worst case (255-byte client id echoed, 64-byte root path, 255-byte option 43) is |
| 65 | * about 850 bytes, past the old 576. The iMac advertises a 1500-byte DHCP maximum (option 57). */ |
| 66 | #define REPLY_MAX 1024 |
| 67 | #define BLK_MAX 1428 |
| 68 | |
| 69 | static uint8_t *booter; |
| 70 | static size_t booter_len; |
| 71 | static struct in_addr server_ip, netmask, router; |
| 72 | static int also_bcast; |
| 73 | static uint8_t only_mac[6]; |
| 74 | static int have_mac; |
| 75 | |
| 76 | static void logf_(const char *fmt, ...) |
| 77 | { |
| 78 | char ts[32]; |
| 79 | time_t t = time(0); |
| 80 | struct tm tm; |
| 81 | if (!gmtime_r(&t, &tm) || !strftime(ts, sizeof ts, "%Y-%m-%dT%H:%M:%SZ", &tm)) |
| 82 | snprintf(ts, sizeof ts, "t=%lld", (long long)t); /* a clock gmtime cannot convert */ |
| 83 | va_list ap; |
| 84 | va_start(ap, fmt); |
| 85 | printf("%s ", ts); |
| 86 | vprintf(fmt, ap); |
| 87 | printf("\n"); |
| 88 | fflush(stdout); |
| 89 | va_end(ap); |
| 90 | } |
| 91 | |
| 92 | static int bind_udp(uint16_t port, int bcast) |
| 93 | { |
| 94 | int one = 1, s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 95 | struct sockaddr_in a = {.sin_family = AF_INET, .sin_port = htons(port), .sin_addr.s_addr = htonl(INADDR_ANY)}; |
| 96 | if (s < 0 || setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one) != 0 || |
| 97 | (bcast && setsockopt(s, SOL_SOCKET, SO_BROADCAST, &one, sizeof one) != 0) || |
| 98 | bind(s, (struct sockaddr *)&a, sizeof a) != 0) { |
| 99 | fprintf(stderr, "ck_imac_bsdp: bind UDP %u: %s\n", port, strerror(errno)); |
| 100 | return -1; |
| 101 | } |
| 102 | return s; |
| 103 | } |
| 104 | |
| 105 | /* ---- BSDP ---- */ |
| 106 | |
| 107 | /* Find DHCP option code in opts[0..n); returns its value and length, or 0. */ |
| 108 | static const uint8_t *opt_find(const uint8_t *o, size_t n, uint8_t code, uint8_t *len) |
| 109 | { |
| 110 | size_t i = 0; |
| 111 | while (i < n) { |
| 112 | uint8_t c = o[i]; |
| 113 | if (c == 255) |
| 114 | break; |
| 115 | if (c == 0) { |
| 116 | i++; |
| 117 | continue; |
| 118 | } |
| 119 | if (i + 2 > n || i + 2 + o[i + 1] > n) |
| 120 | break; |
| 121 | if (c == code) { |
| 122 | *len = o[i + 1]; |
| 123 | return o + i + 2; |
| 124 | } |
| 125 | i += 2 + (size_t)o[i + 1]; |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static size_t opt_put(uint8_t *p, size_t at, size_t cap, uint8_t code, const void *v, uint8_t len) |
| 131 | { |
| 132 | if (at + 2u + len > cap) |
| 133 | return cap + 1; |
| 134 | p[at] = code; |
| 135 | p[at + 1] = len; |
| 136 | memcpy(p + at + 2, v, len); |
| 137 | return at + 2u + len; |
| 138 | } |
| 139 | |
| 140 | static void put32(uint8_t *p, uint32_t v) |
| 141 | { |
| 142 | p[0] = (uint8_t)(v >> 24); |
| 143 | p[1] = (uint8_t)(v >> 16); |
| 144 | p[2] = (uint8_t)(v >> 8); |
| 145 | p[3] = (uint8_t)v; |
| 146 | } |
| 147 | |
| 148 | static uint32_t get32(const uint8_t *p) |
| 149 | { |
| 150 | return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]; |
| 151 | } |
| 152 | |
| 153 | static void bsdp_packet(int s, const uint8_t *in, size_t n) |
| 154 | { |
| 155 | if (n < BOOTP_MIN || in[0] != 1 || in[1] != 1 || in[2] != 6 || get32(in + 236) != DHCP_MAGIC) |
| 156 | return; |
| 157 | const uint8_t *opts = in + BOOTP_MIN, *v; |
| 158 | size_t on = n - BOOTP_MIN; |
| 159 | uint8_t l = 0; |
| 160 | if (!(v = opt_find(opts, on, 53, &l)) || l != 1 || v[0] != 8) /* INFORM only */ |
| 161 | return; |
| 162 | if (!(v = opt_find(opts, on, 60, &l)) || l < 10 || memcmp(v, "AAPLBSDPC/", 10) != 0) |
| 163 | return; |
| 164 | char vclass[64]; |
| 165 | snprintf(vclass, sizeof vclass, "%.*s", (int)l, (const char *)v); |
| 166 | const uint8_t *mac = in + 28; |
| 167 | if (have_mac && memcmp(mac, only_mac, 6) != 0) |
| 168 | return; |
| 169 | const uint8_t *vo = opt_find(opts, on, 43, &l); |
| 170 | uint8_t vl = l, ml = 0, sl = 0, pl = 0; |
| 171 | const uint8_t *mt = vo ? opt_find(vo, vl, 1, &ml) : 0; |
| 172 | if (!mt || ml != 1) |
| 173 | return; |
| 174 | struct in_addr ci; |
| 175 | memcpy(&ci.s_addr, in + 12, 4); |
| 176 | if (ci.s_addr == 0) |
| 177 | return; |
| 178 | uint16_t port = 68; |
| 179 | const uint8_t *rp = opt_find(vo, vl, 5, &pl); |
| 180 | if (rp && pl == 2 && ((rp[0] << 8) | rp[1]) >= 1024) |
| 181 | port = (uint16_t)((rp[0] << 8) | rp[1]); |
| 182 | |
| 183 | uint8_t out[REPLY_MAX] = {0}, sub[255]; |
| 184 | size_t sn = 0, at = BOOTP_MIN; |
| 185 | out[0] = 2; |
| 186 | out[1] = 1; |
| 187 | out[2] = 6; |
| 188 | memcpy(out + 4, in + 4, 4); /* xid */ |
| 189 | memcpy(out + 12, in + 12, 4); /* ciaddr */ |
| 190 | memcpy(out + 20, &server_ip.s_addr, 4); |
| 191 | memcpy(out + 28, in + 28, 16); /* chaddr */ |
| 192 | put32(out + 236, DHCP_MAGIC); |
| 193 | uint8_t ack = 5; |
| 194 | at = opt_put(out, at, sizeof out, 53, &ack, 1); |
| 195 | at = opt_put(out, at, sizeof out, 54, &server_ip.s_addr, 4); |
| 196 | /* RFC 6842 section 3: a client identifier in the request is echoed byte for byte, and only then. */ |
| 197 | uint8_t cl = 0; |
| 198 | const uint8_t *cid = opt_find(opts, on, 61, &cl); |
| 199 | if (cid) |
| 200 | at = opt_put(out, at, sizeof out, 61, cid, cl); |
| 201 | at = opt_put(out, at, sizeof out, 60, "AAPLBSDPC", 9); |
| 202 | /* Subnet mask and router, as the ISC config known to boot Intel Macs forces into the |
| 203 | * answer (its parameter list 1,3,17,43,60); the iMac only asked for 43 and 60. */ |
| 204 | at = opt_put(out, at, sizeof out, 1, &netmask.s_addr, 4); |
| 205 | if (router.s_addr) |
| 206 | at = opt_put(out, at, sizeof out, 3, &router.s_addr, 4); |
| 207 | if (mt[0] == 1) { /* LIST */ |
| 208 | uint8_t id[4]; |
| 209 | put32(id, IMAGE_ID); |
| 210 | sub[sn++] = 1, sub[sn++] = 1, sub[sn++] = 1; |
| 211 | sub[sn++] = 4, sub[sn++] = 2, sub[sn++] = 0x80, sub[sn++] = 0x00; |
| 212 | sub[sn++] = 7, sub[sn++] = 4, memcpy(sub + sn, id, 4), sn += 4; |
| 213 | sub[sn++] = 9, sub[sn++] = (uint8_t)(5 + strlen(IMAGE_NAME)); |
| 214 | memcpy(sub + sn, id, 4), sn += 4; |
| 215 | sub[sn++] = (uint8_t)strlen(IMAGE_NAME); |
| 216 | memcpy(sub + sn, IMAGE_NAME, strlen(IMAGE_NAME)), sn += strlen(IMAGE_NAME); |
| 217 | logf_("LIST from %s %02x:%02x:%02x:%02x:%02x:%02x (%s): offered image %s", inet_ntoa(ci), mac[0], mac[1], |
| 218 | mac[2], mac[3], mac[4], mac[5], vclass, IMAGE_NAME); |
| 219 | } else if (mt[0] == 2) { /* SELECT */ |
| 220 | const uint8_t *sel = opt_find(vo, vl, 8, &sl), *sid; |
| 221 | uint8_t il = 0; |
| 222 | if (!sel || sl != 4 || get32(sel) != IMAGE_ID) |
| 223 | return; |
| 224 | if ((sid = opt_find(vo, vl, 3, &il)) && (il != 4 || memcmp(sid, &server_ip.s_addr, 4) != 0)) |
| 225 | return; /* the client chose another server */ |
| 226 | sub[sn++] = 1, sub[sn++] = 1, sub[sn++] = 2; |
| 227 | sub[sn++] = 8, sub[sn++] = 4, memcpy(sub + sn, sel, 4), sn += 4; |
| 228 | sub[sn++] = 130, sub[sn++] = 6, memcpy(sub + sn, "AIENOS", 6), sn += 6; /* machine name */ |
| 229 | snprintf((char *)out + 44, 64, "spark"); |
| 230 | snprintf((char *)out + 108, 128, "%s", BOOT_NAME); |
| 231 | char root[64]; |
| 232 | snprintf(root, sizeof root, "tftp:%s:/aienos/none", inet_ntoa(server_ip)); |
| 233 | at = opt_put(out, at, sizeof out, 17, root, (uint8_t)strlen(root)); |
| 234 | logf_("SELECT from %s %02x:%02x:%02x:%02x:%02x:%02x: boot file %s from %s", inet_ntoa(ci), mac[0], mac[1], |
| 235 | mac[2], mac[3], mac[4], mac[5], BOOT_NAME, inet_ntoa(server_ip)); |
| 236 | } else { |
| 237 | logf_("BSDP message type %u from %s ignored", mt[0], inet_ntoa(ci)); |
| 238 | return; |
| 239 | } |
| 240 | at = opt_put(out, at, sizeof out, 43, sub, (uint8_t)sn); |
| 241 | if (at + 1 > sizeof out) { |
| 242 | logf_("BSDP answer to %s does not fit in %d bytes; not sent", inet_ntoa(ci), REPLY_MAX); |
| 243 | return; |
| 244 | } |
| 245 | out[at++] = 255; |
| 246 | struct sockaddr_in to = {.sin_family = AF_INET, .sin_port = htons(port), .sin_addr = ci}; |
| 247 | if (sendto(s, out, at < 300 ? 300 : at, 0, (struct sockaddr *)&to, sizeof to) < 0) |
| 248 | logf_("BSDP send to %s failed: %s", inet_ntoa(ci), strerror(errno)); |
| 249 | /* --also-broadcast: a second copy to everyone on the segment, for firmware that does not |
| 250 | * take the unicast; other DHCP clients drop it (chaddr is the iMac's). */ |
| 251 | to.sin_addr.s_addr = htonl(INADDR_BROADCAST); |
| 252 | if (also_bcast && sendto(s, out, at < 300 ? 300 : at, 0, (struct sockaddr *)&to, sizeof to) < 0) |
| 253 | logf_("BSDP broadcast copy failed: %s", strerror(errno)); |
| 254 | } |
| 255 | |
| 256 | /* ---- TFTP (read-only, one file) ---- */ |
| 257 | |
| 258 | static void tftp_error(int s, const struct sockaddr_in *to, uint16_t code, const char *msg) |
| 259 | { |
| 260 | uint8_t b[128]; |
| 261 | size_t n = 4 + strlen(msg) + 1; |
| 262 | b[0] = 0, b[1] = 5, b[2] = (uint8_t)(code >> 8), b[3] = (uint8_t)code; |
| 263 | memcpy(b + 4, msg, strlen(msg) + 1); |
| 264 | sendto(s, b, n, 0, (const struct sockaddr *)to, sizeof *to); |
| 265 | } |
| 266 | |
| 267 | /* Send b[0..n) and wait for ACK of block; 0 acked, -1 gave up. */ |
| 268 | static int tftp_xfer(int s, const struct sockaddr_in *peer, const uint8_t *b, size_t n, uint16_t block) |
| 269 | { |
| 270 | uint8_t r[516]; |
| 271 | for (int tries = 0; tries < 6; tries++) { |
| 272 | if (send(s, b, n, 0) < 0) |
| 273 | return -1; |
| 274 | struct pollfd p = {s, POLLIN, 0}; |
| 275 | while (poll(&p, 1, 1000) > 0) { |
| 276 | struct sockaddr_in from; |
| 277 | socklen_t fl = sizeof from; |
| 278 | ssize_t k = recvfrom(s, r, sizeof r, 0, (struct sockaddr *)&from, &fl); |
| 279 | if (k < 4 || from.sin_addr.s_addr != peer->sin_addr.s_addr || from.sin_port != peer->sin_port) |
| 280 | continue; |
| 281 | if (r[1] == 5) |
| 282 | return -1; |
| 283 | if (r[1] == 4 && ((r[2] << 8) | r[3]) == block) |
| 284 | return 0; |
| 285 | } |
| 286 | } |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | /* Appends name NUL val NUL to the option answer buf[0..*at) when both fit in |
| 291 | * cap bytes. Returns 0 and advances *at, or -1 and leaves *at unchanged. */ |
| 292 | static int oack_put(uint8_t *buf, size_t cap, size_t *at, const char *name, const char *val) |
| 293 | { |
| 294 | size_t a = strlen(name) + 1, b = strlen(val) + 1; |
| 295 | if (*at > cap || a > cap - *at || b > cap - *at - a) |
| 296 | return -1; |
| 297 | memcpy(buf + *at, name, a); |
| 298 | memcpy(buf + *at + a, val, b); |
| 299 | *at += a + b; |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static void tftp_request(const uint8_t *in, size_t n, const struct sockaddr_in *peer) |
| 304 | { |
| 305 | int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 306 | struct sockaddr_in me = {.sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_ANY)}; |
| 307 | if (s < 0 || bind(s, (struct sockaddr *)&me, sizeof me) != 0 || |
| 308 | connect(s, (const struct sockaddr *)peer, sizeof *peer) != 0) { |
| 309 | if (s >= 0) |
| 310 | close(s); |
| 311 | return; |
| 312 | } |
| 313 | char ip[INET_ADDRSTRLEN]; |
| 314 | inet_ntop(AF_INET, &peer->sin_addr, ip, sizeof ip); |
| 315 | /* RRQ: 0 1 filename 0 mode 0 [option 0 value 0]... */ |
| 316 | const char *f = (const char *)in + 2, *end = (const char *)in + n; |
| 317 | const char *mode = memchr(f, 0, (size_t)(end - f)) ? f + strlen(f) + 1 : 0; |
| 318 | if (in[1] != 1 || !mode || mode >= end || !memchr(mode, 0, (size_t)(end - mode))) { |
| 319 | tftp_error(s, peer, 4, "read requests only"); |
| 320 | close(s); |
| 321 | return; |
| 322 | } |
| 323 | while (*f == '/') |
| 324 | f++; |
| 325 | if (strcmp(f, BOOT_NAME) != 0 || strcasecmp(mode, "octet") != 0) { |
| 326 | logf_("TFTP %s asked for \"%s\" (%s): refused", ip, f, mode); |
| 327 | tftp_error(s, peer, 1, "file not found"); |
| 328 | close(s); |
| 329 | return; |
| 330 | } |
| 331 | size_t blk = 512; |
| 332 | uint8_t oack[128]; |
| 333 | size_t on = 2; |
| 334 | int have_blk = 0, have_tsize = 0; |
| 335 | oack[0] = 0, oack[1] = 6; |
| 336 | for (const char *o = mode + strlen(mode) + 1; o < end && memchr(o, 0, (size_t)(end - o));) { |
| 337 | const char *val = o + strlen(o) + 1; |
| 338 | if (val >= end || !memchr(val, 0, (size_t)(end - val))) |
| 339 | break; |
| 340 | char num[24]; |
| 341 | /* Each option is answered at most once (RFC 2347 options do not repeat); a |
| 342 | * repeated one is ignored, and blksize takes effect only when acknowledged. */ |
| 343 | if (!have_blk && strcasecmp(o, "blksize") == 0) { |
| 344 | long b = strtol(val, 0, 10); |
| 345 | if (b >= 8) { |
| 346 | size_t want = (size_t)(b > BLK_MAX ? BLK_MAX : b); |
| 347 | snprintf(num, sizeof num, "%zu", want); |
| 348 | if (oack_put(oack, sizeof oack, &on, "blksize", num) == 0) |
| 349 | blk = want, have_blk = 1; |
| 350 | } |
| 351 | } else if (!have_tsize && strcasecmp(o, "tsize") == 0) { |
| 352 | snprintf(num, sizeof num, "%zu", booter_len); |
| 353 | if (oack_put(oack, sizeof oack, &on, "tsize", num) == 0) |
| 354 | have_tsize = 1; |
| 355 | } |
| 356 | o = val + strlen(val) + 1; |
| 357 | } |
| 358 | logf_("TFTP %s: sending %s (%zu bytes, block %zu)", ip, BOOT_NAME, booter_len, blk); |
| 359 | if (on > 2 && tftp_xfer(s, peer, oack, on, 0) != 0) { |
| 360 | logf_("TFTP %s: no ACK for the option answer; gave up", ip); |
| 361 | close(s); |
| 362 | return; |
| 363 | } |
| 364 | static uint8_t b[4 + BLK_MAX]; |
| 365 | uint16_t block = 1; |
| 366 | for (size_t off = 0;; off += blk, block++) { |
| 367 | size_t k = off < booter_len ? (booter_len - off < blk ? booter_len - off : blk) : 0; |
| 368 | b[0] = 0, b[1] = 3, b[2] = (uint8_t)(block >> 8), b[3] = (uint8_t)block; |
| 369 | memcpy(b + 4, booter + off, k); |
| 370 | if (tftp_xfer(s, peer, b, 4 + k, block) != 0) { |
| 371 | logf_("TFTP %s: transfer stopped at byte %zu", ip, off); |
| 372 | break; |
| 373 | } |
| 374 | if (k < blk) { |
| 375 | logf_("TFTP %s: %s delivered", ip, BOOT_NAME); |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | close(s); |
| 380 | } |
| 381 | |
| 382 | static int usage(void) |
| 383 | { |
| 384 | fprintf(stderr, "usage: ck_imac_bsdp serve --booter FILE --server-ip A.B.C.D [--mac XX:XX:XX:XX:XX:XX]\n" |
| 385 | " [--netmask A.B.C.D] [--router A.B.C.D] [--also-broadcast yes|no]\n" |
| 386 | " [--bsdp-port N] [--tftp-port N] [--reply-source-port N] [--seconds N]\n"); |
| 387 | return 2; |
| 388 | } |
| 389 | |
| 390 | int main(int argc, char **argv) |
| 391 | { |
| 392 | const char *path = 0; |
| 393 | long bport = 67, tport = 69, rport = 0, secs = 0; /* rport 0: answers leave from the listening socket */ |
| 394 | netmask.s_addr = htonl(0xffffff00u); |
| 395 | if (argc < 2 || strcmp(argv[1], "serve") != 0) |
| 396 | return usage(); |
| 397 | for (int i = 2; i < argc; i++) { |
| 398 | const char *a = argv[i], *v = i + 1 < argc ? argv[i + 1] : 0; |
| 399 | if (!v) |
| 400 | return usage(); |
| 401 | if (strcmp(a, "--booter") == 0) |
| 402 | path = v; |
| 403 | else if (strcmp(a, "--server-ip") == 0) { |
| 404 | if (inet_pton(AF_INET, v, &server_ip) != 1) |
| 405 | return usage(); |
| 406 | } else if (strcmp(a, "--netmask") == 0) { |
| 407 | if (inet_pton(AF_INET, v, &netmask) != 1) |
| 408 | return usage(); |
| 409 | } else if (strcmp(a, "--router") == 0) { |
| 410 | if (inet_pton(AF_INET, v, &router) != 1) |
| 411 | return usage(); |
| 412 | } else if (strcmp(a, "--also-broadcast") == 0) { |
| 413 | also_bcast = strcmp(v, "yes") == 0; |
| 414 | } else if (strcmp(a, "--mac") == 0) { |
| 415 | unsigned m[6]; |
| 416 | if (sscanf(v, "%2x:%2x:%2x:%2x:%2x:%2x", &m[0], &m[1], &m[2], &m[3], &m[4], &m[5]) != 6) |
| 417 | return usage(); |
| 418 | for (int k = 0; k < 6; k++) |
| 419 | only_mac[k] = (uint8_t)m[k]; |
| 420 | have_mac = 1; |
| 421 | } else if (strcmp(a, "--bsdp-port") == 0) |
| 422 | bport = strtol(v, 0, 10); |
| 423 | else if (strcmp(a, "--tftp-port") == 0) |
| 424 | tport = strtol(v, 0, 10); |
| 425 | else if (strcmp(a, "--reply-source-port") == 0) { |
| 426 | char *e; |
| 427 | rport = strtol(v, &e, 10); |
| 428 | if (*v == 0 || *e != 0 || rport < 1 || rport > 65535) |
| 429 | return usage(); |
| 430 | } else if (strcmp(a, "--seconds") == 0) |
| 431 | secs = strtol(v, 0, 10); |
| 432 | else |
| 433 | return usage(); |
| 434 | i++; |
| 435 | } |
| 436 | if (!path || !server_ip.s_addr || bport < 1 || bport > 65535 || tport < 1 || tport > 65535 || secs < 0 || rport == tport) |
| 437 | return usage(); |
| 438 | struct stat st; |
| 439 | int fd = open(path, O_RDONLY | O_CLOEXEC); |
| 440 | if (fd < 0 || fstat(fd, &st) != 0 || st.st_size < 2 || st.st_size > 16 * 1024 * 1024) { |
| 441 | fprintf(stderr, "ck_imac_bsdp: cannot use booter %s\n", path); |
| 442 | return 2; |
| 443 | } |
| 444 | booter_len = (size_t)st.st_size; |
| 445 | booter = malloc(booter_len); |
| 446 | if (!booter || read(fd, booter, booter_len) != (ssize_t)booter_len || booter[0] != 'M' || booter[1] != 'Z') { |
| 447 | fprintf(stderr, "ck_imac_bsdp: %s is not a readable PE image\n", path); |
| 448 | return 2; |
| 449 | } |
| 450 | close(fd); |
| 451 | int bs = bind_udp((uint16_t)bport, 1), ts = bind_udp((uint16_t)tport, 0); |
| 452 | if (bs < 0 || ts < 0) |
| 453 | return 3; |
| 454 | /* BSDP answers leave from rs: the listening socket unless --reply-source-port names another port. */ |
| 455 | int rs = bs; |
| 456 | if (rport && rport != bport && (rs = bind_udp((uint16_t)rport, 1)) < 0) |
| 457 | return 3; |
| 458 | logf_("serving BSDP on %ld and TFTP on %ld as %s; booter %s (%zu bytes)%s", bport, tport, inet_ntoa(server_ip), |
| 459 | path, booter_len, have_mac ? "; one client only" : ""); |
| 460 | if (rs != bs) |
| 461 | logf_("BSDP answers are sent from UDP source port %ld (--reply-source-port)", rport); |
| 462 | time_t end = secs ? time(0) + secs : 0; |
| 463 | uint8_t buf[1500]; |
| 464 | while (!end || time(0) < end) { |
| 465 | struct pollfd p[2] = {{bs, POLLIN, 0}, {ts, POLLIN, 0}}; |
| 466 | if (poll(p, 2, 500) <= 0) |
| 467 | continue; |
| 468 | struct sockaddr_in from; |
| 469 | socklen_t fl = sizeof from; |
| 470 | if (p[0].revents & POLLIN) { |
| 471 | ssize_t k = recvfrom(bs, buf, sizeof buf, 0, (struct sockaddr *)&from, &fl); |
| 472 | if (k > 0) |
| 473 | bsdp_packet(rs, buf, (size_t)k); |
| 474 | } |
| 475 | fl = sizeof from; |
| 476 | if (p[1].revents & POLLIN) { |
| 477 | ssize_t k = recvfrom(ts, buf, sizeof buf - 1, 0, (struct sockaddr *)&from, &fl); |
| 478 | if (k >= 4) { |
| 479 | buf[k] = 0; |
| 480 | tftp_request(buf, (size_t)k, &from); |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | logf_("deadline reached"); |
| 485 | return 0; |
| 486 | } |