Lab 3: Dual-NIC FIB Router, RFC 1350 TFTP Framing & NFSv3 Bootargs Verifier
Operate signed APT/dpkg package state machines, isolate ARMv7 cross-compilation pkg-config sysroots from host x86_64 headers, route multi-homed WAN + static bench Ethernet subnets without dual-gateway blackholes, and verify RFC 1350 TFTP plus NFSv3 rootfs network boot.
Prerequisites in this module
What ./check.sh verifies
- Builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror.
- Fix compiler warnings or errors in files/core.c.
- Archives core.o into libcore.a and links independent TAP verifier.
- Preserve public declarations in files/core.h so tests.c links.
step 1/15 · inspect starter files and specification
A broken network-boot bench silently wastes hours of board bring-up: one stray gateway4 on the USB-Ethernet dongle kills internet access, off-by-one 512-byte TFTP framing truncates exact-multiple Device Trees, and missing ,vers=3,tcp or no_root_squash panics the Linux kernel on root mount. This studio builds a preflight C11 verifier catching all three before power-on.
learner@ksa:~/gl-e00/lab-m03ls -lastarter/Makefilestarter/cli.cstarter/core.cstarter/core.hlearner@ksa:~/gl-e00/lab-m03starter: starter/Makefile
learner@ksa:~/gl-e00/lab-m03$ bat starter/Makefile File: starter/Makefile CC_BIN = gcc AR = ar CFLAGS_ALL = -std=c11 -Wall -Wextra -Werror -O2 LDLIBS = all: libcore.a lab_cli libcore.a: core.o $(AR) rcs $@ $^ lab_cli: cli.o libcore.a $(CC_BIN) $(CFLAGS_ALL) cli.o libcore.a $(LDLIBS) -o $@ %.o: %.c core.h $(CC_BIN) $(CFLAGS_ALL) -c $< -o $@ .PHONY: all clean clean: rm -f core.o cli.o libcore.a lab_clistarter: starter/cli.c
learner@ksa:~/gl-e00/lab-m03$ bat starter/cli.c File: starter/cli.c #define _POSIX_C_SOURCE 200809L #include "core.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a; uint64_t y = *(const uint64_t *)b; return (x > y) - (x < y); } static uint64_t now_ns(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec; } static const ksa_route_t CLEAN_FIB[2] = { {"wlan0", 0, 0x00000000U, 0, 0x0A000202U, 600}, {"enx0", 1, 0xC0A80A00U, 24, 0x00000000U, 100}, }; static const ksa_tftp_frame_t TFTP_1024[6] = { {3U, 1U, 512U}, {4U, 1U, 0U}, {3U, 2U, 512U}, {4U, 2U, 0U}, {3U, 3U, 0U}, {4U, 3U, 0U}, }; static const char *EXP_OK = "/srv/nfs/rootfs 192.168.10.0/24(rw,sync,no_root_squash,no_subtree_check)"; static const char *BA_OK = "console=ttymxc0,115200 root=/dev/nfs " "nfsroot=192.168.10.10:/srv/nfs/rootfs,vers=3,tcp rw " "ip=192.168.10.50:192.168.10.10::255.255.255.0::eth0:off"; static int run_bench(void) { uint64_t samples[40]; volatile int sink = 0; for (int w = 0; w < 5; ++w) { sink += ksa_validate_dual_nic_fib(CLEAN_FIB, 2, 0xC0A80A00U); sink += ksa_verify_tftp_session(1024U, TFTP_1024, 6); sink += ksa_validate_nfs_boot(EXP_OK, BA_OK); } for (int s = 0; s < 40; ++s) { uint64_t t0 = now_ns(); for (int it = 0; it < 250; ++it) { sink += ksa_validate_dual_nic_fib(CLEAN_FIB, 2, 0xC0A80A00U); sink += ksa_verify_tftp_session(1024U, TFTP_1024, 6); sink += ksa_validate_nfs_boot(EXP_OK, BA_OK); } samples[s] = (now_ns() - t0) / 250ULL; } qsort(samples, 40, sizeof(samples[0]), cmp_u64); printf("BENCH lab_m03_tftp_nfs_bootargs clock=CLOCK_MONOTONIC warmup=5 samples=40 " "p50_ns=%llu p95_ns=%llu p99_ns=%llu sink=%d\n", (unsigned long long)samples[20], (unsigned long long)samples[38], (unsigned long long)samples[39], sink); return 0; } int main(int argc, char **argv) { if (argc > 1 && strcmp(argv[1], "--bench") == 0) { return run_bench(); } int rc_fib = ksa_validate_dual_nic_fib(CLEAN_FIB, 2, 0xC0A80A00U); int rc_tftp = ksa_verify_tftp_session(1024U, TFTP_1024, 6); int rc_nfs = ksa_validate_nfs_boot(EXP_OK, BA_OK); printf("dual_nic_fib: rc=%d\n", rc_fib); printf("tftp_1024B_3blk_eof: rc=%d\n", rc_tftp); printf("nfs_exports_bootargs: rc=%d\n", rc_nfs); return (rc_fib == 0 && rc_tftp == 0 && rc_nfs == 0) ? 0 : 1; }starter: starter/core.c
learner@ksa:~/gl-e00/lab-m03$ bat starter/core.c File: starter/core.c #define _POSIX_C_SOURCE 200809L #include "core.h" #include <stdio.h> #include <stdlib.h> #include <string.h> static int parse_ipv4_u32(const char *s, uint32_t *out) { unsigned int a, b, c, d; char tail; if (sscanf(s, "%u.%u.%u.%u%c", &a, &b, &c, &d, &tail) != 4) return -1; if (a > 255U || b > 255U || c > 255U || d > 255U) return -1; *out = (a << 24) | (b << 16) | (c << 8) | d; return 0; } int ksa_validate_dual_nic_fib(const ksa_route_t *routes, size_t count, uint32_t bench_subnet_24) { if (!routes || count == 0) return -1; int wan_defaults = 0; int has_bench_link = 0; for (size_t i = 0; i < count; ++i) { if (routes[i].prefix_len == 0) { if (0 && routes[i].is_bench_nic) { return -2; /* BUG: permits default gateway on isolated bench NIC! */ } if (routes[i].gateway != 0U) { wan_defaults++; } } else if (routes[i].is_bench_nic && routes[i].prefix_len == 24) { if ((routes[i].prefix & 0xFFFFFF00U) == (bench_subnet_24 & 0xFFFFFF00U)) { if (routes[i].gateway != 0U) return -3; /* Bench /24 must be scope link */ has_bench_link = 1; } } } if (wan_defaults != 1) return -4; if (!has_bench_link) return -5; return 0; } int ksa_verify_tftp_session(size_t image_bytes, const ksa_tftp_frame_t *frames, size_t frame_count) { if (!frames) return -1; size_t expected_blocks = (image_bytes + 511U) / 512U; /* BUG: drops 0-byte EOF packet when image_bytes % 512 == 0! */ if (frame_count != expected_blocks * 2U) return -2; for (size_t b = 1; b <= expected_blocks; ++b) { const ksa_tftp_frame_t *data_f = &frames[(b - 1U) * 2U]; const ksa_tftp_frame_t *ack_f = &frames[(b - 1U) * 2U + 1U]; if (data_f->opcode != 3U || data_f->block_num != (uint16_t)b) return -3; size_t exp_len = (b < expected_blocks) ? 512U : (image_bytes % 512U); if (data_f->payload_len != exp_len) return -4; if (ack_f->opcode != 4U || ack_f->block_num != (uint16_t)b || ack_f->payload_len != 0U) { return -5; } } return 0; } int ksa_validate_nfs_boot(const char *exports_line, const char *bootargs) { if (!exports_line || !bootargs) return -1; char exp_dir[128] = {0}, spec[160] = {0}, extra[32] = {0}; int n = sscanf(exports_line, "%127s %159s %31s", exp_dir, spec, extra); if (n < 2) return -2; if (n >= 3 && extra[0] == '(') return -3; /* Space before '(' wildcard bug! */ char *lp = strchr(spec, '('); char *rp = lp ? strchr(lp + 1, ')') : NULL; if (!lp || !rp || lp == spec) return -4; char cidr[64] = {0}, opts[128] = {0}; memcpy(cidr, spec, (size_t)(lp - spec)); memcpy(opts, lp + 1, (size_t)(rp - lp - 1)); if (!strstr(opts, "rw") || !strstr(opts, "no_root_squash")) return -5; char *slash = strchr(cidr, '/'); if (!slash) return -6; *slash = '\0'; int plen = atoi(slash + 1); uint32_t net_ip = 0; if (plen < 8 || plen > 30 || parse_ipv4_u32(cidr, &net_ip) != 0) return -6; uint32_t mask = 0xFFFFFFFFU << (32 - plen); if (!strstr(bootargs, "root=/dev/nfs")) return -7; const char *nr = strstr(bootargs, "nfsroot="); if (!nr) return -8; nr += 8; const char *sp = strchr(nr, ' '); char nr_tok[192] = {0}; size_t nr_len = sp ? (size_t)(sp - nr) : strlen(nr); if (nr_len >= sizeof(nr_tok)) return -8; memcpy(nr_tok, nr, nr_len); if (0 && (!strstr(nr_tok, ",vers=3") || !strstr(nr_tok, ",tcp"))) return -9; /* BUG: forgets vers=3,tcp check! */ char *colon = strchr(nr_tok, ':'); char *comma = colon ? strchr(colon + 1, ',') : NULL; if (!colon || !comma) return -10; *colon = '\0'; *comma = '\0'; uint32_t srv_ip = 0; if (parse_ipv4_u32(nr_tok, &srv_ip) != 0) return -11; if ((srv_ip & mask) != (net_ip & mask)) return -12; if (strcmp(colon + 1, exp_dir) != 0) return -13; return 0; }starter: starter/core.h
learner@ksa:~/gl-e00/lab-m03$ bat starter/core.h File: starter/core.h #ifndef KSA_M03_NETBOOT_CORE_H #define KSA_M03_NETBOOT_CORE_H #include <stddef.h> #include <stdint.h> #ifdef __cplusplus extern "C" { #endif typedef struct { const char *iface; int is_bench_nic; uint32_t prefix; int prefix_len; uint32_t gateway; int metric; } ksa_route_t; typedef struct { uint16_t opcode; /* 3 = DATA, 4 = ACK */ uint16_t block_num; /* 1-based */ uint16_t payload_len; } ksa_tftp_frame_t; /* Validates a dual-NIC host routing table: * - Exactly 1 WAN default route (prefix_len == 0, is_bench_nic == 0, gateway != 0) * - ZERO default routes on bench NIC (is_bench_nic != 0 && prefix_len == 0 -> error) * - Bench NIC has an on-link route (gateway == 0) matching bench_subnet/24. */ int ksa_validate_dual_nic_fib(const ksa_route_t *routes, size_t count, uint32_t bench_subnet_24); /* Validates an RFC 1350 TFTP DATA/ACK lockstep trace for an image of image_bytes: * - Expected DATA blocks = (image_bytes / 512) + 1 * - Each block b=1..K has DATA(op=3, blk=b) followed by ACK(op=4, blk=b) * - Blocks 1..K-1 have payload_len == 512; block K has payload_len == image_bytes % 512 (<512). */ int ksa_verify_tftp_session(size_t image_bytes, const ksa_tftp_frame_t *frames, size_t frame_count); /* Cross-validates /etc/exports line against Linux kernel bootargs: * - Rejects space before '(' in exports, requires rw & no_root_squash * - Requires root=/dev/nfs and nfsroot=<srv>:<path>,vers=3,tcp * - Confirms <path> matches export directory and <srv> lies inside exported CIDR. */ int ksa_validate_nfs_boot(const char *exports_line, const char *bootargs); #ifdef __cplusplus } #endif #endifstep 2/15 · 2. learning objectives
- Audit multi-homed FIB tables to forbid
0.0.0.0/0gateways on isolated bench NICs while confirming/24on-link reachability. - Validate RFC 1350
DATA/ACKlockstep sequences including theN / 512 + 1zero-byte EOF packet invariant. - Cross-validate
/etc/exports(rw,no_root_squash, no space before() againstLinux kernelnfsroot=...,vers=3,tcpbootargs.
step 3/15 · 3. environment requirements
All automated verification steps run in unprivileged user space on a POSIX Linux host using gcc -std=c11 -Wall -Wextra -Werror and clock_gettime(CLOCK_MONOTONIC). Recorded transcripts in captures.json were generated on x86_64 running kernel 7.0.0-42-generic and prove protocol, state-machine, and memory invariants only. Physical bus timing, DMA cache coherency, and board peripheral behavior require target validation on Linux kernel 6.12 LTS (ARMv7-A Cortex-A7).
step 4/15 · 4. required software and hardware
- Host workstation:
gcc(-std=c11 -Wall -Wextra -Werror), GNUmake,ar(binutils), and POSIX/bin/sh. - Target environment (optional hardware step, Section 13):
i.MX6ULLARMv7-Aboard wired via100BASE-TXEthernet (ENET1/ENET2) to a host static192.168.10.10/24USB-Ethernet adapter runningtftpd-hpaandnfs-kernel-server(vers3=y).
step 5/15 · 5. complete executable commands
learner@ksa:~/gl-e00/lab-m03$ bat script.sh File: script.sh make -B -C files ./files/lab_cli ./files/lab_cli --bench sh check.shstep 6/15 · 6. annotated source code
files/core.c enforces the production invariants that starter/core.c breaks:
learner@ksa:~/gl-e00/lab-m03$ bat example.c File: example.c size_t expected_blocks = (image_bytes / 512U) + 1U; if (frame_count != expected_blocks * 2U) return -2; ... size_t exp_len = (b < expected_blocks) ? 512U : (image_bytes % 512U);starter/core.c introduces three authentic network-boot bugs: (1) ksa_validate_dual_nic_fib() ignores default routes attached to is_bench_nic; (2) ksa_verify_tftp_session() computes (image_bytes + 511U) / 512U (ceil), failing whenever image_bytes is an exact multiple of 512 bytes (1,024 B needs 3 blocks, not 2!); and (3) ksa_validate_nfs_boot() omits verifying ,vers=3 and ,tcp on nfsroot=.
step 7/15 · 7. expected output
Passing files/ run (sh check.sh, exit code 0):
learner@ksa:~/gl-e00/lab-m03$ bat listing.txt File: listing.txt TAP version 13 1..5 ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror ok 2 - archives core.o into libcore.a and links independent TAP verifier ok 3 - rejects competing default gateway on bench NIC while preserving WAN 0.0.0.0/0 and /24 link ok 4 - verifies RFC 1350 TFTP DATA/ACK lockstep including trailing 0-byte EOF on exact 512B multiples ok 5 - cross-checks /etc/exports whitespace and no_root_squash against kernel nfsroot=...,vers=3,tcp # pass 5/5CLI verification output (./files/lab_cli):
learner@ksa:~/gl-e00/lab-m03$ bat listing.txt File: listing.txt dual_nic_fib: rc=0 tftp_1024B_3blk_eof: rc=0 nfs_exports_bootargs: rc=0Defective starter/ run (exit code 1): emits FAIL: diagnostics on the broken contract assertions.
step 8/15 · 8. explanation of observed behavior
Restoring all three invariants makes libcore.a pass all 5 TAP version 13 checks cleanly: dual_gw routing tables are rejected before blackholing WAN traffic, 1,024-byte TFTP transfers require the trailing 0-byte block 3 EOF packet, and /etc/exports + bootargs agree on path, subnet CIDR, no_root_squash, and vers=3,tcp.
step 9/15 · 9. experiment variations
- Near transfer: Extend
ksa_validate_nfs_boot()to parse theip=<client>:<srv>:<gw>:<mask>:...parameter and reject non-empty<gw>values when<client>and<srv>sit on an isolated/24bench. - Far transfer: Write a live Linux workstation preflight script that feeds
ip -4 route show,/etc/default/tftpd-hpa,/proc/fs/nfsd/versions, and/etc/exportsintolab_clibefore launchingpicocom.
step 10/15 · 10. performance measurements
Recorded host benchmark (./files/lab_cli --bench, CLOCK_MONOTONIC, warmup=5, N=40 batches):
learner@ksa:~/gl-e00/lab-m03$ bat listing.txt File: listing.txt BENCH lab_m03_tftp_nfs_bootargs clock=CLOCK_MONOTONIC warmup=5 samples=40 p50_ns=752 p95_ns=801 p99_ns=807 sink=0CLOCK_MONOTONIC host benchmark numbers measure in-memory C11 validation speed (~nanoseconds), not 100BASE-TX wire propagation, UDP socket round-trip time, or host SSD /srv/nfs/rootfs read latency.
step 11/15 · 11. troubleshooting
| Symptom | Root cause | Fix | ||
|---|---|---|---|---|
not ok 3 - rejects competing default gateway... | routes[i].is_bench_nic check on prefix_len == 0 is disabled, allowing wan_defaults or bench default route. | Return -2 immediately if routes[i].prefix_len == 0 && routes[i].is_bench_nic. | ||
not ok 4 - verifies RFC 1350 TFTP DATA/ACK lockstep... | Used ceiling division (image_bytes + 511U) / 512U, yielding 2 blocks instead of 3 for 1,024 B. | Use (image_bytes / 512U) + 1U so exact 512 B multiples include the mandatory 0-byte EOF block. | ||
not ok 5 - cross-checks /etc/exports whitespace... | Missing check for ,vers=3 and ,tcp inside nfsroot= token. | Return -9 if `!strstr(nr_tok, ",vers=3") | !strstr(nr_tok, ",tcp")`. |
step 12/15 · 12. guided questions
- Why does
(image_bytes + 511U) / 512Uagree with(image_bytes / 512U) + 1Uon700bytes (2blocks) yet disagree on0bytes (0vs1) and1,024bytes (2vs3)?
- *Hint 1:* How does an RFC 1350 receiver know that the sender has no further blocks after receiving a full
512-byteDATApacket? - *Hint 2:* A
512-byteDATApacket ALWAYS means 'at least one more packet follows', so every remainderr = image_bytes % 512(includingr = 0) ends with a final block of lengthr < 512, givingfloor(N / 512) + 1blocks for everyN >= 0!
- In
ksa_validate_nfs_boot(), how doessscanf(exports_line, "%127s %159s %31s", exp_dir, spec, extra)detect/srv/nfs/rootfs 192.168.10.0/24 (rw,...)?
- *Hint 1:* What gets stored into
specandextrawhen a space separates192.168.10.0/24from(rw,...)? - *Hint 2:* When a space precedes
(,specreceives"192.168.10.0/24"(with no'(') andextrareceives"(rw,...)", son >= 3 && extra[0] == '('catches the wildcard leak immediately!
step 13/15 · 13. independent challenges
Add RFC 2348 blksize option-negotiation support (OACK opcode 6, e.g., blksize=1468) to ksa_verify_tftp_session_ext() and benchmark 512 B vs 1468 B block counts for an 8 MiB zImage.
step 14/15 · 14. solutions and explanations
The reference files/core.c forbids prefix_len == 0 on is_bench_nic interfaces, computes (image_bytes / 512U) + 1U blocks for RFC 1350 TFTP EOF framing, and enforces tight cidr(rw,...,no_root_squash) + ,vers=3,tcp consistency across /etc/exports and bootargs.
step 15/15 · run verification and inspect output
Run the verification command and confirm the output matches the expected contract.
learner@ksa:~/gl-e00/lab-m03./files/lab_clilearner@ksa:~/gl-e00/lab-m03dual_nic_fib: rc=0tftp_1024B_3blk_eof: rc=0nfs_exports_bootargs: rc=0Progressive hints & solution
hint 1
Compare starter/Makefile against the module lessons. Focus first on: Builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror.
hint 2
Compile with -std=c11 -Wall -Wextra -Werror, check every system-call return value before touching output buffers, and verify cleanup on error paths.
solution
warning: Reveal the reference solution files? Running ./check.sh against your own attempt first builds stronger retention.
solution: files/Makefile
learner@ksa:~/gl-e00/lab-m03$ bat files/Makefile File: files/Makefile CC_BIN = gcc AR = ar CFLAGS_ALL = -std=c11 -Wall -Wextra -Werror -O2 LDLIBS = all: libcore.a lab_cli libcore.a: core.o $(AR) rcs $@ $^ lab_cli: cli.o libcore.a $(CC_BIN) $(CFLAGS_ALL) cli.o libcore.a $(LDLIBS) -o $@ %.o: %.c core.h $(CC_BIN) $(CFLAGS_ALL) -c $< -o $@ .PHONY: all clean clean: rm -f core.o cli.o libcore.a lab_clisolution: files/cli.c
learner@ksa:~/gl-e00/lab-m03$ bat files/cli.c File: files/cli.c #define _POSIX_C_SOURCE 200809L #include "core.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a; uint64_t y = *(const uint64_t *)b; return (x > y) - (x < y); } static uint64_t now_ns(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec; } static const ksa_route_t CLEAN_FIB[2] = { {"wlan0", 0, 0x00000000U, 0, 0x0A000202U, 600}, {"enx0", 1, 0xC0A80A00U, 24, 0x00000000U, 100}, }; static const ksa_tftp_frame_t TFTP_1024[6] = { {3U, 1U, 512U}, {4U, 1U, 0U}, {3U, 2U, 512U}, {4U, 2U, 0U}, {3U, 3U, 0U}, {4U, 3U, 0U}, }; static const char *EXP_OK = "/srv/nfs/rootfs 192.168.10.0/24(rw,sync,no_root_squash,no_subtree_check)"; static const char *BA_OK = "console=ttymxc0,115200 root=/dev/nfs " "nfsroot=192.168.10.10:/srv/nfs/rootfs,vers=3,tcp rw " "ip=192.168.10.50:192.168.10.10::255.255.255.0::eth0:off"; static int run_bench(void) { uint64_t samples[40]; volatile int sink = 0; for (int w = 0; w < 5; ++w) { sink += ksa_validate_dual_nic_fib(CLEAN_FIB, 2, 0xC0A80A00U); sink += ksa_verify_tftp_session(1024U, TFTP_1024, 6); sink += ksa_validate_nfs_boot(EXP_OK, BA_OK); } for (int s = 0; s < 40; ++s) { uint64_t t0 = now_ns(); for (int it = 0; it < 250; ++it) { sink += ksa_validate_dual_nic_fib(CLEAN_FIB, 2, 0xC0A80A00U); sink += ksa_verify_tftp_session(1024U, TFTP_1024, 6); sink += ksa_validate_nfs_boot(EXP_OK, BA_OK); } samples[s] = (now_ns() - t0) / 250ULL; } qsort(samples, 40, sizeof(samples[0]), cmp_u64); printf("BENCH lab_m03_tftp_nfs_bootargs clock=CLOCK_MONOTONIC warmup=5 samples=40 " "p50_ns=%llu p95_ns=%llu p99_ns=%llu sink=%d\n", (unsigned long long)samples[20], (unsigned long long)samples[38], (unsigned long long)samples[39], sink); return 0; } int main(int argc, char **argv) { if (argc > 1 && strcmp(argv[1], "--bench") == 0) { return run_bench(); } int rc_fib = ksa_validate_dual_nic_fib(CLEAN_FIB, 2, 0xC0A80A00U); int rc_tftp = ksa_verify_tftp_session(1024U, TFTP_1024, 6); int rc_nfs = ksa_validate_nfs_boot(EXP_OK, BA_OK); printf("dual_nic_fib: rc=%d\n", rc_fib); printf("tftp_1024B_3blk_eof: rc=%d\n", rc_tftp); printf("nfs_exports_bootargs: rc=%d\n", rc_nfs); return (rc_fib == 0 && rc_tftp == 0 && rc_nfs == 0) ? 0 : 1; }solution: files/core.c
learner@ksa:~/gl-e00/lab-m03$ bat files/core.c File: files/core.c #define _POSIX_C_SOURCE 200809L #include "core.h" #include <stdio.h> #include <stdlib.h> #include <string.h> static int parse_ipv4_u32(const char *s, uint32_t *out) { unsigned int a, b, c, d; char tail; if (sscanf(s, "%u.%u.%u.%u%c", &a, &b, &c, &d, &tail) != 4) return -1; if (a > 255U || b > 255U || c > 255U || d > 255U) return -1; *out = (a << 24) | (b << 16) | (c << 8) | d; return 0; } int ksa_validate_dual_nic_fib(const ksa_route_t *routes, size_t count, uint32_t bench_subnet_24) { if (!routes || count == 0) return -1; int wan_defaults = 0; int has_bench_link = 0; for (size_t i = 0; i < count; ++i) { if (routes[i].prefix_len == 0) { if (routes[i].is_bench_nic) { return -2; /* Fatal: default route configured on isolated bench NIC! */ } if (routes[i].gateway != 0U) { wan_defaults++; } } else if (routes[i].is_bench_nic && routes[i].prefix_len == 24) { if ((routes[i].prefix & 0xFFFFFF00U) == (bench_subnet_24 & 0xFFFFFF00U)) { if (routes[i].gateway != 0U) return -3; /* Bench /24 must be scope link */ has_bench_link = 1; } } } if (wan_defaults != 1) return -4; if (!has_bench_link) return -5; return 0; } int ksa_verify_tftp_session(size_t image_bytes, const ksa_tftp_frame_t *frames, size_t frame_count) { if (!frames) return -1; size_t expected_blocks = (image_bytes / 512U) + 1U; if (frame_count != expected_blocks * 2U) return -2; for (size_t b = 1; b <= expected_blocks; ++b) { const ksa_tftp_frame_t *data_f = &frames[(b - 1U) * 2U]; const ksa_tftp_frame_t *ack_f = &frames[(b - 1U) * 2U + 1U]; if (data_f->opcode != 3U || data_f->block_num != (uint16_t)b) return -3; size_t exp_len = (b < expected_blocks) ? 512U : (image_bytes % 512U); if (data_f->payload_len != exp_len) return -4; if (ack_f->opcode != 4U || ack_f->block_num != (uint16_t)b || ack_f->payload_len != 0U) { return -5; } } return 0; } int ksa_validate_nfs_boot(const char *exports_line, const char *bootargs) { if (!exports_line || !bootargs) return -1; char exp_dir[128] = {0}, spec[160] = {0}, extra[32] = {0}; int n = sscanf(exports_line, "%127s %159s %31s", exp_dir, spec, extra); if (n < 2) return -2; if (n >= 3 && extra[0] == '(') return -3; /* Space before '(' wildcard bug! */ char *lp = strchr(spec, '('); char *rp = lp ? strchr(lp + 1, ')') : NULL; if (!lp || !rp || lp == spec) return -4; char cidr[64] = {0}, opts[128] = {0}; memcpy(cidr, spec, (size_t)(lp - spec)); memcpy(opts, lp + 1, (size_t)(rp - lp - 1)); if (!strstr(opts, "rw") || !strstr(opts, "no_root_squash")) return -5; char *slash = strchr(cidr, '/'); if (!slash) return -6; *slash = '\0'; int plen = atoi(slash + 1); uint32_t net_ip = 0; if (plen < 8 || plen > 30 || parse_ipv4_u32(cidr, &net_ip) != 0) return -6; uint32_t mask = 0xFFFFFFFFU << (32 - plen); if (!strstr(bootargs, "root=/dev/nfs")) return -7; const char *nr = strstr(bootargs, "nfsroot="); if (!nr) return -8; nr += 8; const char *sp = strchr(nr, ' '); char nr_tok[192] = {0}; size_t nr_len = sp ? (size_t)(sp - nr) : strlen(nr); if (nr_len >= sizeof(nr_tok)) return -8; memcpy(nr_tok, nr, nr_len); if (!strstr(nr_tok, ",vers=3") || !strstr(nr_tok, ",tcp")) return -9; char *colon = strchr(nr_tok, ':'); char *comma = colon ? strchr(colon + 1, ',') : NULL; if (!colon || !comma) return -10; *colon = '\0'; *comma = '\0'; uint32_t srv_ip = 0; if (parse_ipv4_u32(nr_tok, &srv_ip) != 0) return -11; if ((srv_ip & mask) != (net_ip & mask)) return -12; if (strcmp(colon + 1, exp_dir) != 0) return -13; return 0; }solution: files/core.h
learner@ksa:~/gl-e00/lab-m03$ bat files/core.h File: files/core.h #ifndef KSA_M03_NETBOOT_CORE_H #define KSA_M03_NETBOOT_CORE_H #include <stddef.h> #include <stdint.h> #ifdef __cplusplus extern "C" { #endif typedef struct { const char *iface; int is_bench_nic; uint32_t prefix; int prefix_len; uint32_t gateway; int metric; } ksa_route_t; typedef struct { uint16_t opcode; /* 3 = DATA, 4 = ACK */ uint16_t block_num; /* 1-based */ uint16_t payload_len; } ksa_tftp_frame_t; /* Validates a dual-NIC host routing table: * - Exactly 1 WAN default route (prefix_len == 0, is_bench_nic == 0, gateway != 0) * - ZERO default routes on bench NIC (is_bench_nic != 0 && prefix_len == 0 -> error) * - Bench NIC has an on-link route (gateway == 0) matching bench_subnet/24. */ int ksa_validate_dual_nic_fib(const ksa_route_t *routes, size_t count, uint32_t bench_subnet_24); /* Validates an RFC 1350 TFTP DATA/ACK lockstep trace for an image of image_bytes: * - Expected DATA blocks = (image_bytes / 512) + 1 * - Each block b=1..K has DATA(op=3, blk=b) followed by ACK(op=4, blk=b) * - Blocks 1..K-1 have payload_len == 512; block K has payload_len == image_bytes % 512 (<512). */ int ksa_verify_tftp_session(size_t image_bytes, const ksa_tftp_frame_t *frames, size_t frame_count); /* Cross-validates /etc/exports line against Linux kernel bootargs: * - Rejects space before '(' in exports, requires rw & no_root_squash * - Requires root=/dev/nfs and nfsroot=<srv>:<path>,vers=3,tcp * - Confirms <path> matches export directory and <srv> lies inside exported CIDR. */ int ksa_validate_nfs_boot(const char *exports_line, const char *bootargs); #ifdef __cplusplus } #endif #endifRun the check
From labs/lab_m03_tftp_nfs_bootargs/, run ./check.sh. A complete solution emits TAP version 13 with every test point marked ok and exits 0:
learner@ksa:~/gl-e00/lab-m03./check.shTAP version 131..5ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werrorok 2 - archives core.o into libcore.a and links independent TAP verifierok 3 - rejects competing default gateway on bench NIC while preserving WAN 0.0.0.0/0 and /24 linkok 4 - verifies RFC 1350 TFTP DATA/ACK lockstep including trailing 0-byte EOF on exact 512B multiplesok 5 - cross-checks /etc/exports whitespace and no_root_squash against kernel nfsroot=...,vers=3,tcp# pass 5/5On an incomplete starter tree, ./check.sh exits non-zero and marks the unmet assertion not ok with an indented YAML diagnostic block:
learner@ksa:~/gl-e00/lab-m03./check.shTAP version 131..5ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werrorok 2 - archives core.o into libcore.a and links independent TAP verifierok 3 - rejects competing default gateway on bench NIC while preserving WAN 0.0.0.0/0 and /24 linknot ok 4 - verifies RFC 1350 TFTP DATA/ACK lockstep including trailing 0-byte EOF on exact 512B multiplesFAIL: compute expected_blocks = (image_bytes / 512U) + 1U so 1024B requires 3 blocks ending with 0 bytes --- message: 'got tftp_session check failed' hint: 'compute expected_blocks = (image_bytes / 512U) + 1U so 1024B requires 3 blocks ending with 0 bytes' ...ok 5 - cross-checks /etc/exports whitespace and no_root_squash against kernel nfsroot=...,vers=3,tcp# pass 4/5note: How to read TAP diagnostics: 1..N states the total test plan; each ok k confirms a passed check; not ok k pinpoints the failing check alongside message: (what went wrong) and hint: (where to look next) between --- and ....
inspect harness: check.sh
learner@ksa:~/gl-e00/lab-m03$ bat check.sh File: check.sh #!/bin/sh set -u cd "$(dirname "$0")" || exit 1 PASS=0 WORK=$(mktemp -d "${TMPDIR:-/tmp}/ksa_lab.XXXXXX") || exit 1 trap 'rm -rf "$WORK"' EXIT HUP INT TERM cp -R files "$WORK/files" printf 'TAP version 13\n1..5\n' if make -B -C "$WORK/files" >"$WORK/build.log" 2>&1; then printf 'ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror\n' PASS=$((PASS + 1)) else printf 'not ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror\n' printf 'FAIL: fix compiler warnings or errors in files/core.c\n' fi if [ -f "$WORK/files/libcore.a" ] && gcc -std=c11 -Wall -Wextra -Werror -O2 -I"$WORK/files" tests.c "$WORK/files/libcore.a" -o "$WORK/verify" >>"$WORK/build.log" 2>&1; then printf 'ok 2 - archives core.o into libcore.a and links independent TAP verifier\n' PASS=$((PASS + 1)) else printf 'not ok 2 - archives core.o into libcore.a and links independent TAP verifier\n' printf 'FAIL: preserve public declarations in files/core.h so tests.c links\n' fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 1 2>&1); then printf 'ok 3 - rejects competing default gateway on bench NIC while preserving WAN 0.0.0.0/0 and /24 link\n' PASS=$((PASS + 1)) else printf 'not ok 3 - rejects competing default gateway on bench NIC while preserving WAN 0.0.0.0/0 and /24 link\n' printf 'FAIL: reject routes[i].prefix_len == 0 when routes[i].is_bench_nic != 0 and require wan_defaults == 1\n' printf " ---\n message: 'got %s'\n hint: 'reject routes[i].prefix_len == 0 when routes[i].is_bench_nic != 0 and require wan_defaults == 1'\n ...\n" "${OUT:-build_failed}" fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 2 2>&1); then printf 'ok 4 - verifies RFC 1350 TFTP DATA/ACK lockstep including trailing 0-byte EOF on exact 512B multiples\n' PASS=$((PASS + 1)) else printf 'not ok 4 - verifies RFC 1350 TFTP DATA/ACK lockstep including trailing 0-byte EOF on exact 512B multiples\n' printf 'FAIL: compute expected_blocks = (image_bytes / 512U) + 1U so 1024B requires 3 blocks ending with 0 bytes\n' printf " ---\n message: 'got %s'\n hint: 'compute expected_blocks = (image_bytes / 512U) + 1U so 1024B requires 3 blocks ending with 0 bytes'\n ...\n" "${OUT:-build_failed}" fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 3 2>&1); then printf 'ok 5 - cross-checks /etc/exports whitespace and no_root_squash against kernel nfsroot=...,vers=3,tcp\n' PASS=$((PASS + 1)) else printf 'not ok 5 - cross-checks /etc/exports whitespace and no_root_squash against kernel nfsroot=...,vers=3,tcp\n' printf 'FAIL: reject space before ( in /etc/exports and require no_root_squash plus ,vers=3 and ,tcp in nfsroot=\n' printf " ---\n message: 'got %s'\n hint: 'reject space before ( in /etc/exports and require no_root_squash plus ,vers=3 and ,tcp in nfsroot='\n ...\n" "${OUT:-build_failed}" fi printf '# pass %d/5\n' "$PASS" [ "$PASS" -eq 5 ]Definition of done
Verification checklist
status: 0/3 checks verified
Troubleshooting
Compiler fails under -Werror on sign conversion or unused args
Use exact POSIX return types (ssize_t for read()/write(), size_t for byte counts) rather than relaxing flags.
./check.sh reports missing files in files/
Invoke ./check.sh from inside labs/lab_m03_tftp_nfs_bootargs/ so relative paths to files/ resolve directly.