netcmd.c 18 KB

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