netcmd.c 18 KB

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