netcmd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /* Network commands: netinfo, netrecv, netsend, netip, pong (responder) and
  2. * ping (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. #include "dhcp.h"
  8. #include "dns.h"
  9. #include "netlog.h"
  10. /* Shared frame buffers (file scope, not stack: the 4 KB process stack can't
  11. hold a full-size frame, and keeping them out of BSS-as-much-as-possible
  12. avoids bloating the kernel past its 0x49000 limit). Max Ethernet frame is
  13. 1518 bytes (14 header + 1500 payload + 4 VLAN). */
  14. #define NET_FRAME_MAX 1518
  15. static unsigned char net_rx_buf[NET_FRAME_MAX];
  16. static unsigned char net_reply_buf[NET_FRAME_MAX];
  17. static void put_hex2(unsigned char v)
  18. {
  19. static const char hex[] = "0123456789ABCDEF";
  20. io_putchar(hex[(v >> 4) & 0xF]);
  21. io_putchar(hex[v & 0xF]);
  22. }
  23. static void put_dec(unsigned int v)
  24. {
  25. char b[12];
  26. int i = 0;
  27. if (v == 0) {
  28. io_putchar('0');
  29. return;
  30. }
  31. while (v) {
  32. b[i++] = (char)('0' + (v % 10));
  33. v /= 10;
  34. }
  35. while (i)
  36. io_putchar(b[--i]);
  37. }
  38. static void put_mac(const unsigned char *m)
  39. {
  40. unsigned int i;
  41. for (i = 0; i < 6; i++) {
  42. if (i)
  43. io_putchar(':');
  44. put_hex2(m[i]);
  45. }
  46. }
  47. static void put_ip(const unsigned char *ip)
  48. {
  49. unsigned int i;
  50. for (i = 0; i < 4; i++) {
  51. if (i)
  52. io_putchar('.');
  53. put_dec(ip[i]);
  54. }
  55. }
  56. static int ip_eq(const unsigned char *a, const unsigned char *b)
  57. {
  58. return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
  59. }
  60. /* ---- netinfo ---- */
  61. void program_netinfo_main(void)
  62. {
  63. const struct netdev *nd = netdev_active();
  64. unsigned char ip[4], mask[4], gw[4], server[4];
  65. unsigned int lease = 0;
  66. if (!nd) {
  67. io_putstr("netinfo: no NIC found\n");
  68. return;
  69. }
  70. io_putstr("netdev: ");
  71. io_putstr(nd->name);
  72. io_putstr(" (");
  73. io_putstr(nd->chip);
  74. io_putstr(")\n");
  75. nd->print_info();
  76. net_get_ip(ip);
  77. net_get_mask(mask);
  78. net_get_gateway(gw);
  79. io_putstr("ip=");
  80. put_ip(ip);
  81. if (mask[0] || mask[1] || mask[2] || mask[3]) {
  82. io_putstr("/");
  83. put_ip(mask);
  84. }
  85. io_putchar('\n');
  86. if (gw[0] || gw[1] || gw[2] || gw[3]) {
  87. io_putstr("gw=");
  88. put_ip(gw);
  89. io_putchar('\n');
  90. }
  91. {
  92. unsigned char dns[4];
  93. net_get_dns(dns);
  94. if (dns[0] || dns[1] || dns[2] || dns[3]) {
  95. io_putstr("dns=");
  96. put_ip(dns);
  97. io_putchar('\n');
  98. }
  99. }
  100. if (dhcp_last_info(server, &lease)) {
  101. io_putstr("dhcp: server ");
  102. put_ip(server);
  103. io_putstr(" lease ");
  104. put_dec(lease);
  105. io_putstr("s\n");
  106. }
  107. }
  108. /* ---- dhcp ---- */
  109. void program_dhcp_main(void)
  110. {
  111. switch (dhcp_run(4000)) {
  112. case 0:
  113. break; /* dhcp_run() printed the lease */
  114. case -1:
  115. /* message already printed */
  116. break;
  117. default:
  118. io_putstr("dhcp: no NIC\n");
  119. break;
  120. }
  121. }
  122. /* ---- netregs: driver register dump ---- */
  123. void program_netregs_main(void)
  124. {
  125. const struct netdev *nd = netdev_active();
  126. if (!nd) {
  127. io_putstr("netregs: no NIC found\n");
  128. return;
  129. }
  130. if (!nd->dump_regs) {
  131. io_putstr("netregs: no register dump for ");
  132. io_putstr(nd->name);
  133. io_putchar('\n');
  134. return;
  135. }
  136. nd->dump_regs();
  137. }
  138. /* ---- netrecv ---- */
  139. void program_netrecv_main(void)
  140. {
  141. const struct netdev *nd = netdev_active();
  142. unsigned int deadline, count = 0;
  143. if (!nd) {
  144. io_putstr("netrecv: no NIC found\n");
  145. return;
  146. }
  147. io_putstr("netrecv: listening (8s)...\n");
  148. netdev_acquire();
  149. deadline = get_ticks_ms() + 8000;
  150. while (get_ticks_ms() < deadline) {
  151. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  152. unsigned int i;
  153. if (len < 0)
  154. continue;
  155. net_decode_frame(net_rx_buf, (unsigned int)len);
  156. io_putchar('\n');
  157. /* Raw hex dump for closer inspection. */
  158. for (i = 0; i < (unsigned int)len; i++) {
  159. put_hex2(net_rx_buf[i]);
  160. io_putchar(' ');
  161. if ((i & 15) == 15)
  162. io_putchar('\n');
  163. }
  164. if (((unsigned int)len & 15) != 0)
  165. io_putchar('\n');
  166. if (++count >= 32)
  167. break;
  168. }
  169. io_putstr("netrecv: ");
  170. put_dec(count);
  171. io_putstr(" frame(s)\n");
  172. netdev_release();
  173. }
  174. /* ---- netsend ---- */
  175. void program_netsend_main(void)
  176. {
  177. const struct netdev *nd = netdev_active();
  178. unsigned char frame[64];
  179. unsigned char mac[6];
  180. unsigned int i;
  181. if (!nd) {
  182. io_putstr("netsend: no NIC found\n");
  183. return;
  184. }
  185. nd->get_mac(mac);
  186. /* Broadcast test frame: dst=ff:ff:ff:ff:ff:ff, src=our MAC,
  187. ethertype 0x88B5 (local experimental), payload "NEBS". */
  188. for (i = 0; i < 6; i++)
  189. frame[i] = 0xFF;
  190. for (i = 0; i < 6; i++)
  191. frame[6 + i] = mac[i];
  192. frame[12] = 0x88;
  193. frame[13] = 0xB5;
  194. frame[14] = 'N';
  195. frame[15] = 'E';
  196. frame[16] = 'B';
  197. frame[17] = 'S';
  198. for (i = 18; i < 64; i++)
  199. frame[i] = 0x00;
  200. if (nd->tx(frame, sizeof(frame)) != 0) {
  201. io_putstr("netsend: transmit failed\n");
  202. return;
  203. }
  204. io_putstr("netsend: sent 64-byte broadcast frame\n");
  205. }
  206. /* ---- netip ---- */
  207. void program_netip_main(const char *arg)
  208. {
  209. unsigned char ip[4];
  210. if (arg[0] == '\0') {
  211. net_print_ip();
  212. io_putchar('\n');
  213. return;
  214. }
  215. if (net_parse_ip(arg, ip) != 0) {
  216. io_putstr("netip: usage netip <a.b.c.d>\n");
  217. return;
  218. }
  219. net_set_ip(ip);
  220. io_putstr("ip set to ");
  221. net_print_ip();
  222. io_putchar('\n');
  223. }
  224. /* ---- pong: answer ARP/ICMP for our IP (responder mode) ---- */
  225. void program_pong_main(void)
  226. {
  227. const struct netdev *nd = netdev_active();
  228. unsigned int deadline, count = 0;
  229. if (!nd) {
  230. io_putstr("pong: no NIC found\n");
  231. return;
  232. }
  233. io_putstr("pong: answering ARP/ICMP for 30s (ip=");
  234. net_print_ip();
  235. io_putstr(")...\n");
  236. netdev_acquire();
  237. deadline = get_ticks_ms() + 30000;
  238. while (get_ticks_ms() < deadline) {
  239. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  240. int rlen;
  241. if (len < 0)
  242. continue;
  243. rlen = net_build_reply(net_rx_buf, (unsigned int)len,
  244. net_reply_buf, NET_FRAME_MAX);
  245. if (rlen > 0) {
  246. if (nd->tx(net_reply_buf, (unsigned int)rlen) == 0) {
  247. io_putstr("reply: ");
  248. net_decode_frame(net_reply_buf, (unsigned int)rlen);
  249. io_putchar('\n');
  250. count++;
  251. }
  252. }
  253. }
  254. io_putstr("pong: done (");
  255. put_dec(count);
  256. io_putstr(" replies)\n");
  257. netdev_release();
  258. }
  259. /* ---- netlog: send a text line to the LAN as a UDP datagram ---- */
  260. void program_netlog_main(const char *arg)
  261. {
  262. static const unsigned char bcast_mac[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  263. unsigned char dst[4];
  264. unsigned int len = 0;
  265. if (arg[0] == '\0') {
  266. io_putstr("netlog: usage netlog <text>\n");
  267. return;
  268. }
  269. while (arg[len] && len < NET_UDP_MAX)
  270. len++;
  271. netlog_get_host(dst);
  272. if (net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT,
  273. arg, len) != 0) {
  274. io_putstr("netlog: send failed\n");
  275. return;
  276. }
  277. io_putstr("netlog: sent ");
  278. put_dec(len);
  279. io_putstr(" bytes to UDP port ");
  280. put_dec(NETLOG_PORT);
  281. io_putchar('\n');
  282. }
  283. /* ---- netconsole: the debug-output mirror (see netlog.c) ---- */
  284. static int arg_is(const char *arg, const char *word)
  285. {
  286. unsigned int i = 0;
  287. while (arg[i] && word[i] && arg[i] == word[i])
  288. i++;
  289. return arg[i] == '\0' && word[i] == '\0';
  290. }
  291. void program_netconsole_main(const char *arg)
  292. {
  293. unsigned char ip[4];
  294. if (arg[0] == '\0') {
  295. io_putstr("netconsole: mirror ");
  296. io_putstr(netlog_get_enabled() ? "on" : "off");
  297. io_putstr(", host ");
  298. netlog_get_host(ip);
  299. put_ip(ip);
  300. io_putstr(", udp port ");
  301. put_dec(NETLOG_PORT);
  302. io_putchar('\n');
  303. io_putstr("netconsole: usage netconsole on|off|host <a.b.c.d>\n");
  304. return;
  305. }
  306. if (arg_is(arg, "on")) {
  307. netlog_set_enabled(1);
  308. io_putstr("netconsole: mirror on\n");
  309. return;
  310. }
  311. if (arg_is(arg, "off")) {
  312. netlog_set_enabled(0);
  313. io_putstr("netconsole: mirror off\n");
  314. return;
  315. }
  316. if (arg[0] == 'h' && arg[1] == 'o' && arg[2] == 's' && arg[3] == 't') {
  317. const char *p = arg + 4;
  318. while (*p == ' ')
  319. p++;
  320. if (*p == '\0' || net_parse_ip(p, ip) != 0) {
  321. io_putstr("netconsole: usage netconsole host <a.b.c.d>\n");
  322. return;
  323. }
  324. netlog_set_host(ip);
  325. io_putstr("netconsole: host ");
  326. put_ip(ip);
  327. io_putchar('\n');
  328. return;
  329. }
  330. io_putstr("netconsole: usage netconsole on|off|host <a.b.c.d>\n");
  331. }
  332. /* ---- netreset: ask the driver for a full NIC re-init ---- */
  333. void program_netreset_main(void)
  334. {
  335. const struct netdev *nd = netdev_active();
  336. if (!nd) {
  337. io_putstr("netreset: no NIC found\n");
  338. return;
  339. }
  340. if (!nd->reset) {
  341. io_putstr("netreset: not supported by ");
  342. io_putstr(nd->name);
  343. io_putchar('\n');
  344. return;
  345. }
  346. io_putstr("netreset: re-initializing ");
  347. io_putstr(nd->name);
  348. io_putstr("...\n");
  349. nd->reset();
  350. nd->print_info();
  351. }
  352. /* ---- netloop: PHY loopback self-test (no cable/peer needed) ---- */
  353. static void netloop_run(void)
  354. {
  355. const struct netdev *nd = netdev_active();
  356. unsigned char frame[64], mac[6];
  357. unsigned int i, deadline;
  358. int got = 0;
  359. if (!nd) {
  360. io_putstr("netloop: no NIC found\n");
  361. return;
  362. }
  363. if (!nd->set_loopback) {
  364. io_putstr("netloop: not supported by ");
  365. io_putstr(nd->name);
  366. io_putchar('\n');
  367. return;
  368. }
  369. nd->get_mac(mac);
  370. for (i = 0; i < 6; i++)
  371. frame[i] = 0xFF; /* broadcast */
  372. for (i = 0; i < 6; i++)
  373. frame[6 + i] = mac[i];
  374. frame[12] = 0x88;
  375. frame[13] = 0xB6; /* local experimental */
  376. frame[14] = 'N'; frame[15] = 'E'; frame[16] = 'B';
  377. frame[17] = 'O'; frame[18] = 'S'; frame[19] = 'L';
  378. frame[20] = 'O'; frame[21] = 'O';
  379. for (i = 22; i < 64; i++)
  380. frame[i] = 0x00;
  381. io_putstr("netloop: PHY loopback on, sending...\n");
  382. nd->set_loopback(1);
  383. if (nd->tx(frame, sizeof(frame)) != 0) {
  384. io_putstr("netloop: transmit failed\n");
  385. nd->set_loopback(0);
  386. return;
  387. }
  388. deadline = get_ticks_ms() + 1000;
  389. while (get_ticks_ms() < deadline) {
  390. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  391. if (len < 0)
  392. continue;
  393. if (len >= 22 &&
  394. net_rx_buf[12] == 0x88 && net_rx_buf[13] == 0xB6 &&
  395. net_rx_buf[14] == 'N' && net_rx_buf[15] == 'E' &&
  396. net_rx_buf[16] == 'B' && net_rx_buf[17] == 'O' &&
  397. net_rx_buf[18] == 'S') {
  398. got = 1;
  399. break;
  400. }
  401. }
  402. nd->set_loopback(0);
  403. io_putstr(got ? "netloop: PASS (frame came back)\n"
  404. : "netloop: FAIL (no frame came back)\n");
  405. }
  406. void program_netloop_main(void)
  407. {
  408. netdev_acquire();
  409. netloop_run();
  410. netdev_release();
  411. }
  412. /* ---- ping: active ARP + ICMP echo ---- */
  413. #define PING_COUNT 4
  414. #define PING_ID 0x4E42 /* "NB" */
  415. #define PING_WAIT_MS 1000
  416. static unsigned int ping_sent_ms[PING_COUNT];
  417. /* ARP request "who has target? tell my_ip" -> 42-byte frame. */
  418. static unsigned int build_arp_request(const unsigned char *my_mac,
  419. const unsigned char *my_ip,
  420. const unsigned char *target_ip,
  421. unsigned char *out)
  422. {
  423. unsigned char *a = out + 14;
  424. unsigned int i;
  425. for (i = 0; i < 6; i++)
  426. out[i] = 0xFF; /* broadcast */
  427. for (i = 0; i < 6; i++)
  428. out[6 + i] = my_mac[i];
  429. out[12] = 0x08;
  430. out[13] = 0x06;
  431. a[0] = 0; a[1] = 1; /* htype Ethernet */
  432. a[2] = 0x08; a[3] = 0; /* ptype IPv4 */
  433. a[4] = 6; a[5] = 4;
  434. a[6] = 0; a[7] = 1; /* opcode request */
  435. for (i = 0; i < 6; i++) /* sha */
  436. a[8 + i] = my_mac[i];
  437. for (i = 0; i < 4; i++) /* spa */
  438. a[14 + i] = my_ip[i];
  439. for (i = 0; i < 6; i++) /* tha */
  440. a[18 + i] = 0;
  441. for (i = 0; i < 4; i++) /* tpa */
  442. a[24 + i] = target_ip[i];
  443. return 42;
  444. }
  445. /* 1 + peer MAC if frame is an ARP reply from target_ip. */
  446. static int parse_arp_reply(const unsigned char *f, unsigned int len,
  447. const unsigned char *target_ip, unsigned char *mac)
  448. {
  449. const unsigned char *a = f + 14;
  450. unsigned int i;
  451. if (len < 14 + 28)
  452. return 0;
  453. if ((((unsigned int)f[12] << 8) | f[13]) != ET_ARP)
  454. return 0;
  455. if ((((unsigned int)a[6] << 8) | a[7]) != 2) /* opcode reply */
  456. return 0;
  457. if (a[4] != 6 || a[5] != 4)
  458. return 0;
  459. if (!ip_eq(a + 14, target_ip)) /* sender IP == target */
  460. return 0;
  461. for (i = 0; i < 6; i++)
  462. mac[i] = a[8 + i];
  463. return 1;
  464. }
  465. /* ICMP echo request (14 + 20 + 16 bytes; the driver pads to 60). */
  466. static unsigned int build_icmp_echo(const unsigned char *src_mac,
  467. const unsigned char *dst_mac,
  468. const unsigned char *src_ip,
  469. const unsigned char *dst_ip,
  470. unsigned short seq, unsigned char *out)
  471. {
  472. unsigned char *ip = out + 14;
  473. unsigned char *ic = ip + 20;
  474. unsigned char *pl = ic + 8;
  475. unsigned short cs;
  476. unsigned int i;
  477. for (i = 0; i < 6; i++)
  478. out[i] = dst_mac[i];
  479. for (i = 0; i < 6; i++)
  480. out[6 + i] = src_mac[i];
  481. out[12] = 0x08;
  482. out[13] = 0x00;
  483. for (i = 0; i < 20; i++)
  484. ip[i] = 0;
  485. ip[0] = 0x45;
  486. ip[2] = 0; ip[3] = 36; /* total length */
  487. ip[8] = 64; /* TTL */
  488. ip[9] = IPPROTO_ICMP;
  489. for (i = 0; i < 4; i++)
  490. ip[12 + i] = src_ip[i];
  491. for (i = 0; i < 4; i++)
  492. ip[16 + i] = dst_ip[i];
  493. cs = net_checksum(ip, 20);
  494. ip[10] = (unsigned char)(cs >> 8);
  495. ip[11] = (unsigned char)(cs & 0xFF);
  496. ic[0] = ICMP_ECHO_REQUEST;
  497. ic[1] = 0;
  498. ic[2] = 0; ic[3] = 0;
  499. ic[4] = (unsigned char)(PING_ID >> 8);
  500. ic[5] = (unsigned char)(PING_ID & 0xFF);
  501. ic[6] = (unsigned char)(seq >> 8);
  502. ic[7] = (unsigned char)(seq & 0xFF);
  503. pl[0] = 'n'; pl[1] = 'e'; pl[2] = 'b'; pl[3] = 'o';
  504. pl[4] = 's'; pl[5] = 0; pl[6] = 0; pl[7] = 0;
  505. cs = net_checksum(ic, 16);
  506. ic[2] = (unsigned char)(cs >> 8);
  507. ic[3] = (unsigned char)(cs & 0xFF);
  508. return 14 + 20 + 16;
  509. }
  510. /* 1 + ICMP echo-reply seq for our IP from peer_ip. */
  511. static int parse_icmp_reply(const unsigned char *f, unsigned int len,
  512. const unsigned char *my_ip,
  513. const unsigned char *peer_ip,
  514. unsigned short *seq)
  515. {
  516. const unsigned char *ip = f + 14;
  517. const unsigned char *ic;
  518. unsigned int ihl;
  519. unsigned short ip_len, id;
  520. if (len < 14 + 20)
  521. return 0;
  522. if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
  523. return 0;
  524. if ((ip[0] >> 4) != 4)
  525. return 0;
  526. ihl = (unsigned int)(ip[0] & 0x0F) * 4;
  527. if (ihl < 20 || ip[9] != IPPROTO_ICMP)
  528. return 0;
  529. ip_len = ((unsigned short)ip[2] << 8) | ip[3];
  530. if (ip_len < ihl + 8 || (unsigned int)ip_len > len - 14)
  531. return 0;
  532. if (!ip_eq(ip + 12, peer_ip)) /* src == peer */
  533. return 0;
  534. if (!ip_eq(ip + 16, my_ip)) /* dst == us */
  535. return 0;
  536. ic = ip + ihl;
  537. if (ic[0] != ICMP_ECHO_REPLY || ic[1] != 0)
  538. return 0;
  539. id = ((unsigned short)ic[4] << 8) | ic[5];
  540. if (id != PING_ID)
  541. return 0;
  542. *seq = ((unsigned short)ic[6] << 8) | ic[7];
  543. if (*seq < 1 || *seq > PING_COUNT)
  544. return 0;
  545. return 1;
  546. }
  547. /* Resolve target_ip's MAC by ARP (3 tries, 1 s each).
  548. * Returns 0 = resolved, -1 = no reply, -2 = transmit failed.
  549. * Prints the frames seen while waiting (capped) so a silent peer, a filtered
  550. * reply and a reply we refuse to parse can be told apart on screen. */
  551. static int ping_arp(const struct netdev *nd, const unsigned char *my_mac,
  552. const unsigned char *my_ip, const unsigned char *target_ip,
  553. unsigned char *target_mac)
  554. {
  555. unsigned char frame[64];
  556. unsigned int tries, seen = 0;
  557. for (tries = 0; tries < 3; tries++) {
  558. unsigned int deadline = get_ticks_ms() + PING_WAIT_MS;
  559. unsigned int flen = build_arp_request(my_mac, my_ip, target_ip, frame);
  560. if (nd->tx(frame, flen) != 0)
  561. return -2;
  562. io_putstr("ping: ARP request sent (try ");
  563. put_dec(tries + 1);
  564. io_putstr("/3)\n");
  565. while (get_ticks_ms() < deadline) {
  566. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  567. if (len < 0)
  568. continue;
  569. if (seen < 8) {
  570. io_putstr("ping: rx ");
  571. net_decode_frame(net_rx_buf, (unsigned int)len);
  572. io_putchar('\n');
  573. seen++;
  574. }
  575. if (parse_arp_reply(net_rx_buf, (unsigned int)len,
  576. target_ip, target_mac))
  577. return 0;
  578. }
  579. }
  580. io_putstr("ping: saw ");
  581. put_dec(seen);
  582. io_putstr(" frame(s) while waiting\n");
  583. return -1;
  584. }
  585. static void ping_run(const char *arg)
  586. {
  587. const struct netdev *nd;
  588. unsigned char target[4], my_ip[4], my_mac[6], target_mac[6];
  589. unsigned int i, replies = 0;
  590. if (arg[0] == '\0') {
  591. io_putstr("ping: usage ping <host|a.b.c.d>\n");
  592. return;
  593. }
  594. nd = netdev_active();
  595. if (!nd) {
  596. io_putstr("ping: no NIC found\n");
  597. return;
  598. }
  599. /* Dotted IP, or a name to resolve first. */
  600. if (net_parse_ip(arg, target) != 0) {
  601. if (dns_resolve(arg, target) != 0)
  602. return;
  603. }
  604. nd->get_mac(my_mac);
  605. net_get_ip(my_ip);
  606. /* With a mask and gateway known, off-subnet targets go via the router;
  607. * we can only ARP for the next hop. */
  608. {
  609. unsigned char mask[4], gw[4], hop[4];
  610. unsigned int j, off = 0;
  611. net_get_mask(mask);
  612. net_get_gateway(gw);
  613. for (j = 0; j < 4; j++)
  614. hop[j] = target[j];
  615. if ((mask[0] || mask[1] || mask[2] || mask[3]) &&
  616. (gw[0] || gw[1] || gw[2] || gw[3])) {
  617. for (j = 0; j < 4; j++) {
  618. if ((target[j] & mask[j]) != (my_ip[j] & mask[j]))
  619. off = 1;
  620. }
  621. }
  622. if (off) {
  623. for (j = 0; j < 4; j++)
  624. hop[j] = gw[j];
  625. io_putstr("ping: ");
  626. put_ip(target);
  627. io_putstr(" via ");
  628. put_ip(hop);
  629. io_putchar('\n');
  630. }
  631. {
  632. int r = ping_arp(nd, my_mac, my_ip, hop, target_mac);
  633. if (r == -2) {
  634. io_putstr("ping: transmit failed (see netregs)\n");
  635. return;
  636. }
  637. if (r != 0) {
  638. io_putstr("ping: no ARP reply from ");
  639. put_ip(hop);
  640. io_putchar('\n');
  641. return;
  642. }
  643. }
  644. }
  645. io_putstr("ping: ");
  646. put_ip(target);
  647. io_putstr(" is-at ");
  648. put_mac(target_mac);
  649. io_putchar('\n');
  650. for (i = 0; i < PING_COUNT; i++) {
  651. unsigned char frame[64];
  652. unsigned int flen, deadline;
  653. int got = 0;
  654. flen = build_icmp_echo(my_mac, target_mac, my_ip, target,
  655. (unsigned short)(i + 1), frame);
  656. ping_sent_ms[i] = get_ticks_ms();
  657. if (nd->tx(frame, flen) != 0) {
  658. io_putstr("ping: transmit failed\n");
  659. return;
  660. }
  661. deadline = get_ticks_ms() + PING_WAIT_MS;
  662. while (get_ticks_ms() < deadline && !got) {
  663. unsigned short seq;
  664. int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
  665. unsigned int rtt;
  666. if (len < 0)
  667. continue;
  668. if (!parse_icmp_reply(net_rx_buf, (unsigned int)len,
  669. my_ip, target, &seq))
  670. continue;
  671. rtt = get_ticks_ms() - ping_sent_ms[seq - 1];
  672. io_putstr("reply: seq=");
  673. put_dec(seq);
  674. io_putstr(" rtt=");
  675. if (rtt == 0)
  676. io_putstr("<1");
  677. else
  678. put_dec(rtt);
  679. io_putstr("ms\n");
  680. got = 1;
  681. replies++;
  682. }
  683. if (!got) {
  684. io_putstr("ping: timeout, seq=");
  685. put_dec(i + 1);
  686. io_putchar('\n');
  687. }
  688. }
  689. io_putstr("ping: ");
  690. put_dec(replies);
  691. io_putstr("/");
  692. put_dec(PING_COUNT);
  693. io_putstr(" replies from ");
  694. put_ip(target);
  695. io_putchar('\n');
  696. }
  697. void program_ping_main(const char *arg)
  698. {
  699. netdev_acquire();
  700. ping_run(arg);
  701. netdev_release();
  702. }