netcmd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /* Network commands: netinfo, netrecv, netsend, netip, ping (responder) and
  2. * netping (active ARP + ICMP echo), over the netdev NIC layer. */
  3. #include "io.h"
  4. #include "timer.h"
  5. #include "netdev.h"
  6. #include "net.h"
  7. /* Shared frame buffers (file scope, not stack: the 4 KB process stack can't
  8. hold a full-size frame, and keeping them out of BSS-as-much-as-possible
  9. avoids bloating the kernel past its 0x40000 limit). Max Ethernet frame is
  10. 1518 bytes (14 header + 1500 payload + 4 VLAN). */
  11. #define NET_FRAME_MAX 1518
  12. static unsigned char net_rx_buf[NET_FRAME_MAX];
  13. static unsigned char net_reply_buf[NET_FRAME_MAX];
  14. static void put_hex2(unsigned char v)
  15. {
  16. static const char hex[] = "0123456789ABCDEF";
  17. io_putchar(hex[(v >> 4) & 0xF]);
  18. io_putchar(hex[v & 0xF]);
  19. }
  20. static void put_dec(unsigned int v)
  21. {
  22. char b[12];
  23. int i = 0;
  24. if (v == 0) {
  25. io_putchar('0');
  26. return;
  27. }
  28. while (v) {
  29. b[i++] = (char)('0' + (v % 10));
  30. v /= 10;
  31. }
  32. while (i)
  33. io_putchar(b[--i]);
  34. }
  35. static void put_mac(const unsigned char *m)
  36. {
  37. unsigned int i;
  38. for (i = 0; i < 6; i++) {
  39. if (i)
  40. io_putchar(':');
  41. put_hex2(m[i]);
  42. }
  43. }
  44. static void put_ip(const unsigned char *ip)
  45. {
  46. unsigned int i;
  47. for (i = 0; i < 4; i++) {
  48. if (i)
  49. io_putchar('.');
  50. put_dec(ip[i]);
  51. }
  52. }
  53. static int ip_eq(const unsigned char *a, const unsigned char *b)
  54. {
  55. return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
  56. }
  57. /* ---- netinfo ---- */
  58. void program_netinfo_main(void)
  59. {
  60. const struct netdev *nd = netdev_active();
  61. if (!nd) {
  62. io_putstr("netinfo: no NIC found\n");
  63. return;
  64. }
  65. io_putstr("netdev: ");
  66. io_putstr(nd->name);
  67. io_putstr(" (");
  68. io_putstr(nd->chip);
  69. io_putstr(")\n");
  70. nd->print_info();
  71. io_putstr("ip=");
  72. net_print_ip();
  73. io_putchar('\n');
  74. }
  75. /* ---- netregs: driver register dump ---- */
  76. void program_netregs_main(void)
  77. {
  78. const struct netdev *nd = netdev_active();
  79. if (!nd) {
  80. io_putstr("netregs: no NIC found\n");
  81. return;
  82. }
  83. if (!nd->dump_regs) {
  84. io_putstr("netregs: no register dump for ");
  85. io_putstr(nd->name);
  86. io_putchar('\n');
  87. return;
  88. }
  89. nd->dump_regs();
  90. }
  91. /* ---- netrecv ---- */
  92. void program_netrecv_main(void)
  93. {
  94. const struct netdev *nd = netdev_active();
  95. unsigned int deadline, count = 0;
  96. if (!nd) {
  97. io_putstr("netrecv: no NIC found\n");
  98. return;
  99. }
  100. io_putstr("netrecv: listening (8s)...\n");
  101. netdev_acquire();
  102. deadline = get_ticks_ms() + 8000;
  103. while (get_ticks_ms() < deadline) {
  104. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  105. unsigned int i;
  106. if (len < 0)
  107. continue;
  108. net_decode_frame(net_rx_buf, (unsigned int)len);
  109. io_putchar('\n');
  110. /* Raw hex dump for closer inspection. */
  111. for (i = 0; i < (unsigned int)len; i++) {
  112. put_hex2(net_rx_buf[i]);
  113. io_putchar(' ');
  114. if ((i & 15) == 15)
  115. io_putchar('\n');
  116. }
  117. if (((unsigned int)len & 15) != 0)
  118. io_putchar('\n');
  119. if (++count >= 32)
  120. break;
  121. }
  122. io_putstr("netrecv: ");
  123. put_dec(count);
  124. io_putstr(" frame(s)\n");
  125. netdev_release();
  126. }
  127. /* ---- netsend ---- */
  128. void program_netsend_main(void)
  129. {
  130. const struct netdev *nd = netdev_active();
  131. unsigned char frame[64];
  132. unsigned char mac[6];
  133. unsigned int i;
  134. if (!nd) {
  135. io_putstr("netsend: no NIC found\n");
  136. return;
  137. }
  138. nd->get_mac(mac);
  139. /* Broadcast test frame: dst=ff:ff:ff:ff:ff:ff, src=our MAC,
  140. ethertype 0x88B5 (local experimental), payload "NEBS". */
  141. for (i = 0; i < 6; i++)
  142. frame[i] = 0xFF;
  143. for (i = 0; i < 6; i++)
  144. frame[6 + i] = mac[i];
  145. frame[12] = 0x88;
  146. frame[13] = 0xB5;
  147. frame[14] = 'N';
  148. frame[15] = 'E';
  149. frame[16] = 'B';
  150. frame[17] = 'S';
  151. for (i = 18; i < 64; i++)
  152. frame[i] = 0x00;
  153. if (nd->tx(frame, sizeof(frame)) != 0) {
  154. io_putstr("netsend: transmit failed\n");
  155. return;
  156. }
  157. io_putstr("netsend: sent 64-byte broadcast frame\n");
  158. }
  159. /* ---- netip ---- */
  160. void program_netip_main(const char *arg)
  161. {
  162. unsigned char ip[4];
  163. if (arg[0] == '\0') {
  164. net_print_ip();
  165. io_putchar('\n');
  166. return;
  167. }
  168. if (net_parse_ip(arg, ip) != 0) {
  169. io_putstr("netip: usage netip <a.b.c.d>\n");
  170. return;
  171. }
  172. net_set_ip(ip);
  173. io_putstr("ip set to ");
  174. net_print_ip();
  175. io_putchar('\n');
  176. }
  177. /* ---- ping: answer ARP/ICMP for our IP (responder mode) ---- */
  178. void program_ping_main(void)
  179. {
  180. const struct netdev *nd = netdev_active();
  181. unsigned int deadline, count = 0;
  182. if (!nd) {
  183. io_putstr("ping: no NIC found\n");
  184. return;
  185. }
  186. io_putstr("ping: answering ARP/ICMP for 30s (ip=");
  187. net_print_ip();
  188. io_putstr(")...\n");
  189. netdev_acquire();
  190. deadline = get_ticks_ms() + 30000;
  191. while (get_ticks_ms() < deadline) {
  192. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  193. int rlen;
  194. if (len < 0)
  195. continue;
  196. rlen = net_build_reply(net_rx_buf, (unsigned int)len,
  197. net_reply_buf, NET_FRAME_MAX);
  198. if (rlen > 0) {
  199. if (nd->tx(net_reply_buf, (unsigned int)rlen) == 0) {
  200. io_putstr("reply: ");
  201. net_decode_frame(net_reply_buf, (unsigned int)rlen);
  202. io_putchar('\n');
  203. count++;
  204. }
  205. }
  206. }
  207. io_putstr("ping: done (");
  208. put_dec(count);
  209. io_putstr(" replies)\n");
  210. netdev_release();
  211. }
  212. /* ---- netreset: ask the driver for a full NIC re-init ---- */
  213. void program_netreset_main(void)
  214. {
  215. const struct netdev *nd = netdev_active();
  216. if (!nd) {
  217. io_putstr("netreset: no NIC found\n");
  218. return;
  219. }
  220. if (!nd->reset) {
  221. io_putstr("netreset: not supported by ");
  222. io_putstr(nd->name);
  223. io_putchar('\n');
  224. return;
  225. }
  226. io_putstr("netreset: re-initializing ");
  227. io_putstr(nd->name);
  228. io_putstr("...\n");
  229. nd->reset();
  230. nd->print_info();
  231. }
  232. /* ---- netloop: PHY loopback self-test (no cable/peer needed) ---- */
  233. static void netloop_run(void)
  234. {
  235. const struct netdev *nd = netdev_active();
  236. unsigned char frame[64], mac[6];
  237. unsigned int i, deadline;
  238. int got = 0;
  239. if (!nd) {
  240. io_putstr("netloop: no NIC found\n");
  241. return;
  242. }
  243. if (!nd->set_loopback) {
  244. io_putstr("netloop: not supported by ");
  245. io_putstr(nd->name);
  246. io_putchar('\n');
  247. return;
  248. }
  249. nd->get_mac(mac);
  250. for (i = 0; i < 6; i++)
  251. frame[i] = 0xFF; /* broadcast */
  252. for (i = 0; i < 6; i++)
  253. frame[6 + i] = mac[i];
  254. frame[12] = 0x88;
  255. frame[13] = 0xB6; /* local experimental */
  256. frame[14] = 'N'; frame[15] = 'E'; frame[16] = 'B';
  257. frame[17] = 'O'; frame[18] = 'S'; frame[19] = 'L';
  258. frame[20] = 'O'; frame[21] = 'O';
  259. for (i = 22; i < 64; i++)
  260. frame[i] = 0x00;
  261. io_putstr("netloop: PHY loopback on, sending...\n");
  262. nd->set_loopback(1);
  263. if (nd->tx(frame, sizeof(frame)) != 0) {
  264. io_putstr("netloop: transmit failed\n");
  265. nd->set_loopback(0);
  266. return;
  267. }
  268. deadline = get_ticks_ms() + 1000;
  269. while (get_ticks_ms() < deadline) {
  270. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  271. if (len < 0)
  272. continue;
  273. if (len >= 22 &&
  274. net_rx_buf[12] == 0x88 && net_rx_buf[13] == 0xB6 &&
  275. net_rx_buf[14] == 'N' && net_rx_buf[15] == 'E' &&
  276. net_rx_buf[16] == 'B' && net_rx_buf[17] == 'O' &&
  277. net_rx_buf[18] == 'S') {
  278. got = 1;
  279. break;
  280. }
  281. }
  282. nd->set_loopback(0);
  283. io_putstr(got ? "netloop: PASS (frame came back)\n"
  284. : "netloop: FAIL (no frame came back)\n");
  285. }
  286. void program_netloop_main(void)
  287. {
  288. netdev_acquire();
  289. netloop_run();
  290. netdev_release();
  291. }
  292. /* ---- netping: active ARP + ICMP echo ---- */
  293. #define NETPING_COUNT 4
  294. #define NETPING_ID 0x4E42 /* "NB" */
  295. #define NETPING_WAIT_MS 1000
  296. static unsigned int netping_sent_ms[NETPING_COUNT];
  297. /* ARP request "who has target? tell my_ip" -> 42-byte frame. */
  298. static unsigned int build_arp_request(const unsigned char *my_mac,
  299. const unsigned char *my_ip,
  300. const unsigned char *target_ip,
  301. unsigned char *out)
  302. {
  303. unsigned char *a = out + 14;
  304. unsigned int i;
  305. for (i = 0; i < 6; i++)
  306. out[i] = 0xFF; /* broadcast */
  307. for (i = 0; i < 6; i++)
  308. out[6 + i] = my_mac[i];
  309. out[12] = 0x08;
  310. out[13] = 0x06;
  311. a[0] = 0; a[1] = 1; /* htype Ethernet */
  312. a[2] = 0x08; a[3] = 0; /* ptype IPv4 */
  313. a[4] = 6; a[5] = 4;
  314. a[6] = 0; a[7] = 1; /* opcode request */
  315. for (i = 0; i < 6; i++) /* sha */
  316. a[8 + i] = my_mac[i];
  317. for (i = 0; i < 4; i++) /* spa */
  318. a[14 + i] = my_ip[i];
  319. for (i = 0; i < 6; i++) /* tha */
  320. a[18 + i] = 0;
  321. for (i = 0; i < 4; i++) /* tpa */
  322. a[24 + i] = target_ip[i];
  323. return 42;
  324. }
  325. /* 1 + peer MAC if frame is an ARP reply from target_ip. */
  326. static int parse_arp_reply(const unsigned char *f, unsigned int len,
  327. const unsigned char *target_ip, unsigned char *mac)
  328. {
  329. const unsigned char *a = f + 14;
  330. unsigned int i;
  331. if (len < 14 + 28)
  332. return 0;
  333. if ((((unsigned int)f[12] << 8) | f[13]) != ET_ARP)
  334. return 0;
  335. if ((((unsigned int)a[6] << 8) | a[7]) != 2) /* opcode reply */
  336. return 0;
  337. if (a[4] != 6 || a[5] != 4)
  338. return 0;
  339. if (!ip_eq(a + 14, target_ip)) /* sender IP == target */
  340. return 0;
  341. for (i = 0; i < 6; i++)
  342. mac[i] = a[8 + i];
  343. return 1;
  344. }
  345. /* ICMP echo request (14 + 20 + 16 bytes; the driver pads to 60). */
  346. static unsigned int build_icmp_echo(const unsigned char *src_mac,
  347. const unsigned char *dst_mac,
  348. const unsigned char *src_ip,
  349. const unsigned char *dst_ip,
  350. unsigned short seq, unsigned char *out)
  351. {
  352. unsigned char *ip = out + 14;
  353. unsigned char *ic = ip + 20;
  354. unsigned char *pl = ic + 8;
  355. unsigned short cs;
  356. unsigned int i;
  357. for (i = 0; i < 6; i++)
  358. out[i] = dst_mac[i];
  359. for (i = 0; i < 6; i++)
  360. out[6 + i] = src_mac[i];
  361. out[12] = 0x08;
  362. out[13] = 0x00;
  363. for (i = 0; i < 20; i++)
  364. ip[i] = 0;
  365. ip[0] = 0x45;
  366. ip[2] = 0; ip[3] = 36; /* total length */
  367. ip[8] = 64; /* TTL */
  368. ip[9] = IPPROTO_ICMP;
  369. for (i = 0; i < 4; i++)
  370. ip[12 + i] = src_ip[i];
  371. for (i = 0; i < 4; i++)
  372. ip[16 + i] = dst_ip[i];
  373. cs = net_checksum(ip, 20);
  374. ip[10] = (unsigned char)(cs >> 8);
  375. ip[11] = (unsigned char)(cs & 0xFF);
  376. ic[0] = ICMP_ECHO_REQUEST;
  377. ic[1] = 0;
  378. ic[2] = 0; ic[3] = 0;
  379. ic[4] = (unsigned char)(NETPING_ID >> 8);
  380. ic[5] = (unsigned char)(NETPING_ID & 0xFF);
  381. ic[6] = (unsigned char)(seq >> 8);
  382. ic[7] = (unsigned char)(seq & 0xFF);
  383. pl[0] = 'n'; pl[1] = 'e'; pl[2] = 'b'; pl[3] = 'o';
  384. pl[4] = 's'; pl[5] = 0; pl[6] = 0; pl[7] = 0;
  385. cs = net_checksum(ic, 16);
  386. ic[2] = (unsigned char)(cs >> 8);
  387. ic[3] = (unsigned char)(cs & 0xFF);
  388. return 14 + 20 + 16;
  389. }
  390. /* 1 + ICMP echo-reply seq for our IP from peer_ip. */
  391. static int parse_icmp_reply(const unsigned char *f, unsigned int len,
  392. const unsigned char *my_ip,
  393. const unsigned char *peer_ip,
  394. unsigned short *seq)
  395. {
  396. const unsigned char *ip = f + 14;
  397. const unsigned char *ic;
  398. unsigned int ihl;
  399. unsigned short ip_len, id;
  400. if (len < 14 + 20)
  401. return 0;
  402. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  403. return 0;
  404. if ((ip[0] >> 4) != 4)
  405. return 0;
  406. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  407. if (ihl < 20 || ip[9] != IPPROTO_ICMP)
  408. return 0;
  409. ip_len = ((unsigned short)ip[2] << 8) | ip[3];
  410. if (ip_len < ihl + 8 || (unsigned int)ip_len > len - 14)
  411. return 0;
  412. if (!ip_eq(ip + 12, peer_ip)) /* src == peer */
  413. return 0;
  414. if (!ip_eq(ip + 16, my_ip)) /* dst == us */
  415. return 0;
  416. ic = ip + ihl;
  417. if (ic[0] != ICMP_ECHO_REPLY || ic[1] != 0)
  418. return 0;
  419. id = ((unsigned short)ic[4] << 8) | ic[5];
  420. if (id != NETPING_ID)
  421. return 0;
  422. *seq = ((unsigned short)ic[6] << 8) | ic[7];
  423. if (*seq < 1 || *seq > NETPING_COUNT)
  424. return 0;
  425. return 1;
  426. }
  427. /* Resolve target_ip's MAC by ARP (3 tries, 1 s each).
  428. * Returns 0 = resolved, -1 = no reply, -2 = transmit failed.
  429. * Prints the frames seen while waiting (capped) so a silent peer, a filtered
  430. * reply and a reply we refuse to parse can be told apart on screen. */
  431. static int netping_arp(const struct netdev *nd, const unsigned char *my_mac,
  432. const unsigned char *my_ip, const unsigned char *target_ip,
  433. unsigned char *target_mac)
  434. {
  435. unsigned char frame[64];
  436. unsigned int tries, seen = 0;
  437. for (tries = 0; tries < 3; tries++) {
  438. unsigned int deadline = get_ticks_ms() + NETPING_WAIT_MS;
  439. unsigned int flen = build_arp_request(my_mac, my_ip, target_ip, frame);
  440. if (nd->tx(frame, flen) != 0)
  441. return -2;
  442. io_putstr("netping: ARP request sent (try ");
  443. put_dec(tries + 1);
  444. io_putstr("/3)\n");
  445. while (get_ticks_ms() < deadline) {
  446. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  447. if (len < 0)
  448. continue;
  449. if (seen < 8) {
  450. io_putstr("netping: rx ");
  451. net_decode_frame(net_rx_buf, (unsigned int)len);
  452. io_putchar('\n');
  453. seen++;
  454. }
  455. if (parse_arp_reply(net_rx_buf, (unsigned int)len,
  456. target_ip, target_mac))
  457. return 0;
  458. }
  459. }
  460. io_putstr("netping: saw ");
  461. put_dec(seen);
  462. io_putstr(" frame(s) while waiting\n");
  463. return -1;
  464. }
  465. static void netping_run(const char *arg)
  466. {
  467. const struct netdev *nd;
  468. unsigned char target[4], my_ip[4], my_mac[6], target_mac[6];
  469. unsigned int i, replies = 0;
  470. if (arg[0] == '\0' || net_parse_ip(arg, target) != 0) {
  471. io_putstr("netping: usage netping <a.b.c.d>\n");
  472. return;
  473. }
  474. nd = netdev_active();
  475. if (!nd) {
  476. io_putstr("netping: no NIC found\n");
  477. return;
  478. }
  479. nd->get_mac(my_mac);
  480. net_get_ip(my_ip);
  481. {
  482. int r = netping_arp(nd, my_mac, my_ip, target, target_mac);
  483. if (r == -2) {
  484. io_putstr("netping: transmit failed (see netregs)\n");
  485. return;
  486. }
  487. if (r != 0) {
  488. io_putstr("netping: no ARP reply from ");
  489. put_ip(target);
  490. io_putchar('\n');
  491. return;
  492. }
  493. }
  494. io_putstr("netping: ");
  495. put_ip(target);
  496. io_putstr(" is-at ");
  497. put_mac(target_mac);
  498. io_putchar('\n');
  499. for (i = 0; i < NETPING_COUNT; i++) {
  500. unsigned char frame[64];
  501. unsigned int flen, deadline;
  502. int got = 0;
  503. flen = build_icmp_echo(my_mac, target_mac, my_ip, target,
  504. (unsigned short)(i + 1), frame);
  505. netping_sent_ms[i] = get_ticks_ms();
  506. if (nd->tx(frame, flen) != 0) {
  507. io_putstr("netping: transmit failed\n");
  508. return;
  509. }
  510. deadline = get_ticks_ms() + NETPING_WAIT_MS;
  511. while (get_ticks_ms() < deadline && !got) {
  512. unsigned short seq;
  513. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  514. unsigned int rtt;
  515. if (len < 0)
  516. continue;
  517. if (!parse_icmp_reply(net_rx_buf, (unsigned int)len,
  518. my_ip, target, &seq))
  519. continue;
  520. rtt = get_ticks_ms() - netping_sent_ms[seq - 1];
  521. io_putstr("reply: seq=");
  522. put_dec(seq);
  523. io_putstr(" rtt=");
  524. if (rtt == 0)
  525. io_putstr("<1");
  526. else
  527. put_dec(rtt);
  528. io_putstr("ms\n");
  529. got = 1;
  530. replies++;
  531. }
  532. if (!got) {
  533. io_putstr("netping: timeout, seq=");
  534. put_dec(i + 1);
  535. io_putchar('\n');
  536. }
  537. }
  538. io_putstr("netping: ");
  539. put_dec(replies);
  540. io_putstr("/");
  541. put_dec(NETPING_COUNT);
  542. io_putstr(" replies from ");
  543. put_ip(target);
  544. io_putchar('\n');
  545. }
  546. void program_netping_main(const char *arg)
  547. {
  548. netdev_acquire();
  549. netping_run(arg);
  550. netdev_release();
  551. }