netkexec.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* Kernel hot-reload over UDP: the host pushes kernel.bin as datagrams, the
  2. * board writes them into the kexec staging buffer, verifies the checksum and
  3. * jumps (kexec.c has the serial variant).
  4. *
  5. * Protocol (little-endian; UDP to NETKEXEC_PORT; the board replies to the
  6. * sender's IP/MAC/port):
  7. *
  8. * host -> board header "KXN2" entry kernel_size pack_size csum (20 bytes)
  9. * host -> board data "KXND" offset len payload... (12 + len)
  10. * board -> host ack "KXNA" next_offset status (12 bytes)
  11. * status 0 = keep going, 1 = done, 2 = bad checksum
  12. * status 3 = persisted, 4 = persist failed
  13. *
  14. * The runtime pack replacement keeps the old 16-byte "KXNP" header
  15. * (magic, entry, size, csum) with the raw NBPAK1 pack as data.
  16. *
  17. * The persistent install uses "KXI2": the KXN2 header plus a 16-byte target
  18. * name, and a full NBROOT1 rootfs image (header + gzip pack) instead of the
  19. * raw gzip payload. After the receive the board writes P2/P1, acks
  20. * ACK_INSTALLED/ACK_INSTALL_FAIL and then jumps with the gzip part.
  21. *
  22. * The board auto-detects the header from the WM loop (netkexec_pending());
  23. * program_netkexec_main() then runs stop-and-wait: every datagram is acked
  24. * with the next expected offset, so a lost or duplicated datagram costs one
  25. * retransmit. Datagrams addressed to the broadcast IP are accepted too, so
  26. * the host can push without knowing whether the board is up yet. While the
  27. * receive owns the NIC it keeps answering ARP/ICMP, otherwise the host's
  28. * neighbour entry expires in the middle of a long upload and the host drops
  29. * every datagram while sendto() still succeeds.
  30. */
  31. #include "install.h"
  32. #include "io.h"
  33. #include "kexec.h"
  34. #include "net.h"
  35. #include "netcore.h"
  36. #include "netdev.h"
  37. #include "tcp.h"
  38. #include "process.h"
  39. #include "serial.h"
  40. #include "timer.h"
  41. #include "netkexec.h"
  42. #include "mm.h"
  43. #include "ramfs.h"
  44. #include "rootfs.h"
  45. #include "vfs.h"
  46. #define NETKEXEC_PORT 19288
  47. #define NETKEXEC_DATA_MAX 1400
  48. #define X_MAGIC_HDR 0x324E584Bu /* "KXN2": kernel + rootfs */
  49. #define X_MAGIC_PAY 0x504E584Bu /* "KXNP": a command pack for /ramfs */
  50. #define X_MAGIC_INST 0x3249584Bu /* "KXI2": persist kernel + rootfs */
  51. #define X_MAGIC_DATA 0x444E584Bu /* "KXND" */
  52. #define X_MAGIC_ACK 0x414E584Bu /* "KXNA" */
  53. #define ACK_CONTINUE 0
  54. #define ACK_DONE 1
  55. #define ACK_BADSUM 2
  56. #define ACK_INSTALLED 3
  57. #define ACK_INSTALL_FAIL 4
  58. static unsigned char rxbuf[1520]; /* one full Ethernet frame */
  59. static unsigned char replybuf[1520];
  60. static unsigned char ackbuf[12];
  61. /* Page-allocated staging for a payload transfer (freed after the swap). */
  62. static unsigned int pay_buf;
  63. static unsigned int pay_size;
  64. static int net_payload_alloc(unsigned int size)
  65. {
  66. pay_size = size;
  67. pay_buf = mm_alloc_pages((size + 0xFFFu) >> 12);
  68. return pay_buf != 0;
  69. }
  70. static void net_payload_free(void)
  71. {
  72. unsigned int i;
  73. for (i = 0; i * 0x1000 < pay_size; i++)
  74. mm_free_page(pay_buf + i * 0x1000);
  75. pay_buf = 0;
  76. }
  77. /* Pending transfer (filled by netkexec_pending). */
  78. static int have_header;
  79. static int payload_mode; /* header was KXNP: replace the ramfs instead */
  80. static int install_mode; /* header was KXI2: persist to a block device */
  81. static char x_install_target[16];
  82. static unsigned int x_entry, x_size, x_csum;
  83. static unsigned int x_kernel_size, x_pack_size;
  84. static unsigned char peer_mac[6], peer_ip[4];
  85. static unsigned int peer_port;
  86. static unsigned int rd32(const unsigned char *p)
  87. {
  88. return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
  89. ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
  90. }
  91. static void wr32(unsigned char *p, unsigned int v)
  92. {
  93. p[0] = (unsigned char)(v & 0xFF);
  94. p[1] = (unsigned char)((v >> 8) & 0xFF);
  95. p[2] = (unsigned char)((v >> 16) & 0xFF);
  96. p[3] = (unsigned char)((v >> 24) & 0xFF);
  97. }
  98. /* Parse `f` as a UDP datagram to NETKEXEC_PORT; fill payload + peer address.
  99. * The management link may have a different address from the active test NIC
  100. * (ASIX 192.168.50.2 while e1000e is 192.168.178.240), so accept any
  101. * destination IP here: the KXN2/KXND magic and checksum are the gate.
  102. * Returns 1 on match. */
  103. static int parse_udp(const unsigned char *f, unsigned int len,
  104. const unsigned char **payload, unsigned int *plen,
  105. unsigned char p_mac[6], unsigned char p_ip[4],
  106. unsigned int *p_port)
  107. {
  108. const unsigned char *ip = f + 14;
  109. const unsigned char *udp;
  110. unsigned int i, ihl, ip_len;
  111. if (len < 14 + 20 + 8)
  112. return 0;
  113. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  114. return 0;
  115. if ((ip[0] >> 4) != 4)
  116. return 0;
  117. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  118. if (ihl < 20 || ip[9] != IPPROTO_UDP)
  119. return 0;
  120. ip_len = ((unsigned int)ip[2] << 8) | ip[3];
  121. if (ip_len < ihl + 8 || 14 + ip_len > len)
  122. return 0;
  123. udp = ip + ihl;
  124. if ((((unsigned int)udp[2] << 8) | udp[3]) != NETKEXEC_PORT)
  125. return 0;
  126. for (i = 0; i < 6; i++)
  127. p_mac[i] = f[6 + i];
  128. for (i = 0; i < 4; i++)
  129. p_ip[i] = ip[12 + i];
  130. *p_port = ((unsigned int)udp[0] << 8) | udp[1];
  131. *payload = udp + 8;
  132. *plen = ip_len - ihl - 8;
  133. return *plen > 0;
  134. }
  135. static int netkexec_poll_nd(const struct netdev *nd, int capture)
  136. {
  137. unsigned int n;
  138. for (n = 0; n < 8; n++) {
  139. int len = netdev_rx_frame(nd, rxbuf, sizeof(rxbuf));
  140. if (len < 0)
  141. break;
  142. if (capture && netkexec_capture_frame(rxbuf,
  143. (unsigned int)len))
  144. return 1;
  145. /* Not a kexec datagram: stay reachable by answering ARP and
  146. * ICMP echo requests for our IP (so the board can be pinged,
  147. * and a host can resolve its MAC, without running a
  148. * command). Until an address is configured there is nothing
  149. * to answer for. Use the receiving NIC's MAC: this also
  150. * runs for the active NIC when the management link is a
  151. * different device. A driver's link-local default address
  152. * (ASIX) is answerable even when the active NIC has none. */
  153. if (net_is_configured() || nd->default_ip) {
  154. int rlen = net_build_reply_dev(nd, rxbuf,
  155. (unsigned int)len,
  156. replybuf,
  157. sizeof(replybuf));
  158. if (rlen > 0)
  159. netdev_tx_frame(nd, replybuf, (unsigned int)rlen);
  160. }
  161. }
  162. return 0;
  163. }
  164. int netkexec_pending(void)
  165. {
  166. const struct netdev *mgmt, *act;
  167. if (kexec_busy || netdev_in_use() || tcp_client_count() != 0 ||
  168. net_stack_busy())
  169. return 0;
  170. if (have_header)
  171. return 1;
  172. mgmt = netdev_mgmt();
  173. act = netdev_active();
  174. if (!mgmt && !act)
  175. return 0;
  176. /* KXN2/KXNP headers and the netmon console use the management
  177. * link (ASIX when present), so switching the active driver to
  178. * e1000e for testing does not cut the upload/debug path. The
  179. * active NIC still gets its ARP/ICMP responder polled. */
  180. if (mgmt && netkexec_poll_nd(mgmt, 1))
  181. return 1;
  182. if (act && act != mgmt)
  183. netkexec_poll_nd(act, 0);
  184. return 0;
  185. }
  186. /* Validate one received frame as a KXN2/KXNP header and stash the transfer.
  187. * Returns 1 when it was captured. The WebDAV server calls this while it
  188. * owns the receiver, so `make net-dev` hands the NIC back without waiting
  189. * for the host's header retransmits. */
  190. int netkexec_capture_frame(const unsigned char *frame, unsigned int len)
  191. {
  192. const unsigned char *payload;
  193. unsigned int plen, port, magic, i;
  194. unsigned char p_mac[6], p_ip[4];
  195. if (have_header)
  196. return 0;
  197. if (!parse_udp(frame, len, &payload, &plen, p_mac, p_ip, &port))
  198. return 0;
  199. if (plen < 16)
  200. return 0;
  201. magic = rd32(payload);
  202. if (magic != X_MAGIC_HDR && magic != X_MAGIC_PAY &&
  203. magic != X_MAGIC_INST)
  204. return 0;
  205. x_entry = rd32(payload + 4);
  206. payload_mode = 0;
  207. install_mode = 0;
  208. if (magic == X_MAGIC_PAY) {
  209. x_size = rd32(payload + 8);
  210. x_csum = rd32(payload + 12);
  211. x_kernel_size = 0;
  212. x_pack_size = 0;
  213. payload_mode = 1;
  214. } else {
  215. if (plen < (magic == X_MAGIC_INST ? 36u : 20u))
  216. return 0;
  217. x_kernel_size = rd32(payload + 8);
  218. x_pack_size = rd32(payload + 12);
  219. x_csum = rd32(payload + 16);
  220. x_size = x_kernel_size + x_pack_size;
  221. if (x_kernel_size == 0 || x_pack_size > KEXEC_MAX ||
  222. x_size < x_kernel_size || x_size > KEXEC_MAX)
  223. return 0;
  224. if (magic == X_MAGIC_INST) {
  225. unsigned int t;
  226. for (t = 0; t < sizeof(x_install_target) - 1; t++) {
  227. char c = (char)payload[20 + t];
  228. if (c == '\0')
  229. break;
  230. x_install_target[t] = c;
  231. }
  232. x_install_target[t] = '\0';
  233. if (!x_install_target[0])
  234. return 0;
  235. install_mode = 1;
  236. }
  237. }
  238. if (x_size == 0 || x_size > KEXEC_MAX)
  239. return 0;
  240. for (i = 0; i < 6; i++)
  241. peer_mac[i] = p_mac[i];
  242. for (i = 0; i < 4; i++)
  243. peer_ip[i] = p_ip[i];
  244. peer_port = port;
  245. have_header = 1;
  246. return 1;
  247. }
  248. static void send_ack(const struct netdev *nd, unsigned int next_offset,
  249. unsigned int status)
  250. {
  251. unsigned char src[4];
  252. if (!nd)
  253. return;
  254. net_get_ip(src);
  255. wr32(ackbuf, X_MAGIC_ACK);
  256. wr32(ackbuf + 4, next_offset);
  257. wr32(ackbuf + 8, status);
  258. net_udp_send_from_dev(nd, src, peer_mac, peer_ip, NETKEXEC_PORT,
  259. peer_port, ackbuf, sizeof(ackbuf));
  260. }
  261. static int netkexec_recv(unsigned int stage_base)
  262. {
  263. volatile unsigned char *stage = (volatile unsigned char *)stage_base;
  264. const struct netdev *nd = netdev_mgmt();
  265. unsigned int offset = 0, retries = 0, sum = 0;
  266. unsigned int deadline = get_ticks_ms() + 3000;
  267. unsigned int hard_deadline = get_ticks_ms() + 60000;
  268. if (!nd)
  269. return -1;
  270. kexec_total = x_size;
  271. kexec_progress = 0;
  272. send_ack(nd, 0, ACK_CONTINUE); /* header ok, send from offset 0 */
  273. while (offset < x_size) {
  274. const unsigned char *payload;
  275. unsigned int plen, doff, dlen;
  276. unsigned char p_mac[6], p_ip[4];
  277. unsigned int p_port;
  278. int len, i;
  279. /* A host that trickles one datagram at a time can keep the
  280. * per-packet deadline alive forever, holding the NIC (and
  281. * the auto-detect loop) hostage. Cap the whole transfer as
  282. * well. */
  283. if ((int)(get_ticks_ms() - hard_deadline) > 0)
  284. return -1;
  285. if ((int)(get_ticks_ms() - deadline) > 0) {
  286. if (++retries > 10)
  287. return -1; /* host gave up */
  288. send_ack(nd, offset, ACK_CONTINUE);
  289. deadline = get_ticks_ms() + 1000;
  290. continue;
  291. }
  292. len = netdev_rx_frame(nd, rxbuf, sizeof(rxbuf));
  293. if (len < 0) {
  294. /* Yield so the WM and terminal keep running while we
  295. * wait for the next datagram; this used to busy-spin
  296. * with the BKL held and froze the UI. */
  297. process_sleep_ms(1);
  298. continue;
  299. }
  300. /* We own the NIC for the whole transfer, so the idle loop's
  301. * ARP/ICMP responder cannot run. Keep answering here: a
  302. * multi-megabyte upload lasts long enough for the host's
  303. * neighbour entry to go STALE, and once its probes go
  304. * unanswered the host drops every datagram while sendto()
  305. * still returns success (the transfer would stall at a
  306. * random offset until the host gives up). */
  307. if (!parse_udp(rxbuf, (unsigned int)len, &payload, &plen,
  308. p_mac, p_ip, &p_port) ||
  309. plen < 12 || rd32(payload) != X_MAGIC_DATA) {
  310. net_arp_input(rxbuf, (unsigned int)len);
  311. if (net_is_configured() || nd->default_ip) {
  312. int rlen = net_build_reply_dev(nd, rxbuf,
  313. (unsigned int)len,
  314. replybuf,
  315. sizeof(replybuf));
  316. if (rlen > 0)
  317. netdev_tx_frame(nd, replybuf, (unsigned int)rlen);
  318. }
  319. /* Yield after a frame that was not ours (ARP/ICMP or
  320. * a stray datagram), so a broadcast storm cannot keep
  321. * the BKL for the whole transfer. */
  322. process_sleep_ms(1);
  323. continue;
  324. }
  325. doff = rd32(payload + 4);
  326. dlen = rd32(payload + 8);
  327. if (dlen > NETKEXEC_DATA_MAX || plen < 12 + dlen)
  328. continue;
  329. if (doff == offset) {
  330. for (i = 0; i < (int)dlen; i++) {
  331. stage[offset + i] = payload[12 + i];
  332. sum += payload[12 + i];
  333. }
  334. for (i = 0; i < 6; i++)
  335. peer_mac[i] = p_mac[i];
  336. for (i = 0; i < 4; i++)
  337. peer_ip[i] = p_ip[i];
  338. peer_port = p_port;
  339. offset += dlen;
  340. kexec_progress = offset;
  341. retries = 0;
  342. deadline = get_ticks_ms() + 1000;
  343. if ((offset & 0x3FFF) == 0)
  344. io_putchar('.');
  345. }
  346. if (offset < x_size)
  347. send_ack(nd, offset, ACK_CONTINUE); /* acks duplicates */
  348. }
  349. if (sum != x_csum) {
  350. send_ack(nd, offset, ACK_BADSUM);
  351. return -1;
  352. }
  353. send_ack(nd, x_size, ACK_DONE);
  354. return 0;
  355. }
  356. void program_netkexec_main(void)
  357. {
  358. if (!have_header) {
  359. io_putstr("netkexec: no transfer pending\n");
  360. kexec_busy = 0;
  361. return;
  362. }
  363. if (tcp_client_count() != 0) {
  364. io_putstr("netkexec: NIC busy (close network clients first)\n");
  365. kexec_busy = 0;
  366. return;
  367. }
  368. have_header = 0;
  369. netdev_acquire();
  370. io_putstr("netkexec: receiving ");
  371. io_puthex(x_size);
  372. io_putstr(" bytes over UDP...\n");
  373. if (payload_mode) {
  374. unsigned int buf = 0;
  375. if (!net_payload_alloc(x_size)) {
  376. io_putstr("netkexec: out of memory (staging)\n");
  377. netdev_release();
  378. kexec_busy = 0;
  379. return;
  380. }
  381. buf = pay_buf;
  382. if (netkexec_recv(buf) != 0) {
  383. io_putstr("\nnetkexec: aborted (timeout or checksum)\n");
  384. net_payload_free();
  385. netdev_release();
  386. kexec_busy = 0;
  387. return;
  388. }
  389. io_putstr("\nnetkexec: received ");
  390. io_puthex(x_size);
  391. io_putstr(" bytes, ");
  392. if (ramfs_replace((const void *)buf, x_size) != 0) {
  393. io_putstr("not a valid pack (unchanged)\n");
  394. net_payload_free(); /* the ramfs kept the old pack */
  395. } else {
  396. vfs_refresh_ramfs();
  397. io_putstr("pack installed (");
  398. io_puthex(ramfs_count());
  399. io_putstr(" files)\n");
  400. pay_buf = 0; /* ownership moved to the ramfs */
  401. pay_size = 0;
  402. }
  403. netdev_release();
  404. kexec_busy = 0;
  405. return;
  406. }
  407. if (!kexec_stage_alloc(x_size)) {
  408. io_putstr("netkexec: out of memory (staging)\n");
  409. netdev_release();
  410. kexec_busy = 0;
  411. return;
  412. }
  413. if (netkexec_recv(kexec_stage) != 0) {
  414. io_putstr("\nnetkexec: aborted (timeout or checksum)\n");
  415. kexec_stage_free();
  416. netdev_release();
  417. kexec_busy = 0;
  418. return;
  419. }
  420. io_putstr("\nnetkexec: received kernel ");
  421. io_puthex(x_kernel_size);
  422. io_putstr(" bytes, rootfs ");
  423. io_puthex(x_pack_size);
  424. io_putstr(" bytes, entry ");
  425. io_puthex(x_entry);
  426. io_putchar('\n');
  427. if (install_mode) {
  428. const struct rootfs_hdr *rh =
  429. (const struct rootfs_hdr *)(kexec_stage + x_kernel_size);
  430. int r;
  431. if (x_pack_size < ROOTFS_HDR_SIZE ||
  432. rh->pack_bytes > x_pack_size - ROOTFS_HDR_SIZE) {
  433. io_putstr("netinstall: bad rootfs image\n");
  434. send_ack(netdev_mgmt(), x_size, ACK_INSTALL_FAIL);
  435. kexec_stage_free();
  436. netdev_release();
  437. kexec_busy = 0;
  438. return;
  439. }
  440. r = install_ram_image(x_install_target,
  441. (const void *)kexec_stage, x_kernel_size,
  442. rh, x_pack_size);
  443. if (r != 0) {
  444. io_putstr("netinstall: failed; disk not updated\n");
  445. send_ack(netdev_mgmt(), x_size, ACK_INSTALL_FAIL);
  446. kexec_stage_free();
  447. netdev_release();
  448. kexec_busy = 0;
  449. return;
  450. }
  451. send_ack(netdev_mgmt(), x_size, ACK_INSTALLED);
  452. io_putstr("netinstall: persisted to ");
  453. io_putstr(x_install_target);
  454. io_putstr(" -> jumping\n");
  455. kexec_jump(x_entry, x_kernel_size,
  456. (unsigned int)rh + ROOTFS_HDR_SIZE,
  457. rh->pack_bytes);
  458. }
  459. io_putstr(" -> jumping\n");
  460. kexec_jump(x_entry, x_kernel_size, kexec_stage + x_kernel_size,
  461. x_pack_size); /* never returns */
  462. }