| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724 |
- /* Network commands: netinfo, netrecv, netsend, netip, ping (responder) and
- * netping (active ARP + ICMP echo), over the netdev NIC layer. */
- #include "io.h"
- #include "timer.h"
- #include "netdev.h"
- #include "net.h"
- #include "netlog.h"
- /* Shared frame buffers (file scope, not stack: the 4 KB process stack can't
- hold a full-size frame, and keeping them out of BSS-as-much-as-possible
- avoids bloating the kernel past its 0x40000 limit). Max Ethernet frame is
- 1518 bytes (14 header + 1500 payload + 4 VLAN). */
- #define NET_FRAME_MAX 1518
- static unsigned char net_rx_buf[NET_FRAME_MAX];
- static unsigned char net_reply_buf[NET_FRAME_MAX];
- static void put_hex2(unsigned char v)
- {
- static const char hex[] = "0123456789ABCDEF";
- io_putchar(hex[(v >> 4) & 0xF]);
- io_putchar(hex[v & 0xF]);
- }
- static void put_dec(unsigned int v)
- {
- char b[12];
- int i = 0;
- if (v == 0) {
- io_putchar('0');
- return;
- }
- while (v) {
- b[i++] = (char)('0' + (v % 10));
- v /= 10;
- }
- while (i)
- io_putchar(b[--i]);
- }
- static void put_mac(const unsigned char *m)
- {
- unsigned int i;
- for (i = 0; i < 6; i++) {
- if (i)
- io_putchar(':');
- put_hex2(m[i]);
- }
- }
- static void put_ip(const unsigned char *ip)
- {
- unsigned int i;
- for (i = 0; i < 4; i++) {
- if (i)
- io_putchar('.');
- put_dec(ip[i]);
- }
- }
- static int ip_eq(const unsigned char *a, const unsigned char *b)
- {
- return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
- }
- /* ---- netinfo ---- */
- void program_netinfo_main(void)
- {
- const struct netdev *nd = netdev_active();
- if (!nd) {
- io_putstr("netinfo: no NIC found\n");
- return;
- }
- io_putstr("netdev: ");
- io_putstr(nd->name);
- io_putstr(" (");
- io_putstr(nd->chip);
- io_putstr(")\n");
- nd->print_info();
- io_putstr("ip=");
- net_print_ip();
- io_putchar('\n');
- }
- /* ---- netregs: driver register dump ---- */
- void program_netregs_main(void)
- {
- const struct netdev *nd = netdev_active();
- if (!nd) {
- io_putstr("netregs: no NIC found\n");
- return;
- }
- if (!nd->dump_regs) {
- io_putstr("netregs: no register dump for ");
- io_putstr(nd->name);
- io_putchar('\n');
- return;
- }
- nd->dump_regs();
- }
- /* ---- netrecv ---- */
- void program_netrecv_main(void)
- {
- const struct netdev *nd = netdev_active();
- unsigned int deadline, count = 0;
- if (!nd) {
- io_putstr("netrecv: no NIC found\n");
- return;
- }
- io_putstr("netrecv: listening (8s)...\n");
- netdev_acquire();
- deadline = get_ticks_ms() + 8000;
- while (get_ticks_ms() < deadline) {
- int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
- unsigned int i;
- if (len < 0)
- continue;
- net_decode_frame(net_rx_buf, (unsigned int)len);
- io_putchar('\n');
- /* Raw hex dump for closer inspection. */
- for (i = 0; i < (unsigned int)len; i++) {
- put_hex2(net_rx_buf[i]);
- io_putchar(' ');
- if ((i & 15) == 15)
- io_putchar('\n');
- }
- if (((unsigned int)len & 15) != 0)
- io_putchar('\n');
- if (++count >= 32)
- break;
- }
- io_putstr("netrecv: ");
- put_dec(count);
- io_putstr(" frame(s)\n");
- netdev_release();
- }
- /* ---- netsend ---- */
- void program_netsend_main(void)
- {
- const struct netdev *nd = netdev_active();
- unsigned char frame[64];
- unsigned char mac[6];
- unsigned int i;
- if (!nd) {
- io_putstr("netsend: no NIC found\n");
- return;
- }
- nd->get_mac(mac);
- /* Broadcast test frame: dst=ff:ff:ff:ff:ff:ff, src=our MAC,
- ethertype 0x88B5 (local experimental), payload "NEBS". */
- for (i = 0; i < 6; i++)
- frame[i] = 0xFF;
- for (i = 0; i < 6; i++)
- frame[6 + i] = mac[i];
- frame[12] = 0x88;
- frame[13] = 0xB5;
- frame[14] = 'N';
- frame[15] = 'E';
- frame[16] = 'B';
- frame[17] = 'S';
- for (i = 18; i < 64; i++)
- frame[i] = 0x00;
- if (nd->tx(frame, sizeof(frame)) != 0) {
- io_putstr("netsend: transmit failed\n");
- return;
- }
- io_putstr("netsend: sent 64-byte broadcast frame\n");
- }
- /* ---- netip ---- */
- void program_netip_main(const char *arg)
- {
- unsigned char ip[4];
- if (arg[0] == '\0') {
- net_print_ip();
- io_putchar('\n');
- return;
- }
- if (net_parse_ip(arg, ip) != 0) {
- io_putstr("netip: usage netip <a.b.c.d>\n");
- return;
- }
- net_set_ip(ip);
- io_putstr("ip set to ");
- net_print_ip();
- io_putchar('\n');
- }
- /* ---- ping: answer ARP/ICMP for our IP (responder mode) ---- */
- void program_ping_main(void)
- {
- const struct netdev *nd = netdev_active();
- unsigned int deadline, count = 0;
- if (!nd) {
- io_putstr("ping: no NIC found\n");
- return;
- }
- io_putstr("ping: answering ARP/ICMP for 30s (ip=");
- net_print_ip();
- io_putstr(")...\n");
- netdev_acquire();
- deadline = get_ticks_ms() + 30000;
- while (get_ticks_ms() < deadline) {
- int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
- int rlen;
- if (len < 0)
- continue;
- rlen = net_build_reply(net_rx_buf, (unsigned int)len,
- net_reply_buf, NET_FRAME_MAX);
- if (rlen > 0) {
- if (nd->tx(net_reply_buf, (unsigned int)rlen) == 0) {
- io_putstr("reply: ");
- net_decode_frame(net_reply_buf, (unsigned int)rlen);
- io_putchar('\n');
- count++;
- }
- }
- }
- io_putstr("ping: done (");
- put_dec(count);
- io_putstr(" replies)\n");
- netdev_release();
- }
- /* ---- netlog: send a text line to the LAN as a UDP datagram ---- */
- void program_netlog_main(const char *arg)
- {
- static const unsigned char bcast_mac[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
- unsigned char dst[4];
- unsigned int len = 0;
- if (arg[0] == '\0') {
- io_putstr("netlog: usage netlog <text>\n");
- return;
- }
- while (arg[len] && len < NET_UDP_MAX)
- len++;
- netlog_get_host(dst);
- if (net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT,
- arg, len) != 0) {
- io_putstr("netlog: send failed\n");
- return;
- }
- io_putstr("netlog: sent ");
- put_dec(len);
- io_putstr(" bytes to UDP port ");
- put_dec(NETLOG_PORT);
- io_putchar('\n');
- }
- /* ---- netconsole: the debug-output mirror (see netlog.c) ---- */
- static int arg_is(const char *arg, const char *word)
- {
- unsigned int i = 0;
- while (arg[i] && word[i] && arg[i] == word[i])
- i++;
- return arg[i] == '\0' && word[i] == '\0';
- }
- void program_netconsole_main(const char *arg)
- {
- unsigned char ip[4];
- if (arg[0] == '\0') {
- io_putstr("netconsole: mirror ");
- io_putstr(netlog_get_enabled() ? "on" : "off");
- io_putstr(", host ");
- netlog_get_host(ip);
- put_ip(ip);
- io_putstr(", udp port ");
- put_dec(NETLOG_PORT);
- io_putchar('\n');
- io_putstr("netconsole: usage netconsole on|off|host <a.b.c.d>\n");
- return;
- }
- if (arg_is(arg, "on")) {
- netlog_set_enabled(1);
- io_putstr("netconsole: mirror on\n");
- return;
- }
- if (arg_is(arg, "off")) {
- netlog_set_enabled(0);
- io_putstr("netconsole: mirror off\n");
- return;
- }
- if (arg[0] == 'h' && arg[1] == 'o' && arg[2] == 's' && arg[3] == 't') {
- const char *p = arg + 4;
- while (*p == ' ')
- p++;
- if (*p == '\0' || net_parse_ip(p, ip) != 0) {
- io_putstr("netconsole: usage netconsole host <a.b.c.d>\n");
- return;
- }
- netlog_set_host(ip);
- io_putstr("netconsole: host ");
- put_ip(ip);
- io_putchar('\n');
- return;
- }
- io_putstr("netconsole: usage netconsole on|off|host <a.b.c.d>\n");
- }
- /* ---- netreset: ask the driver for a full NIC re-init ---- */
- void program_netreset_main(void)
- {
- const struct netdev *nd = netdev_active();
- if (!nd) {
- io_putstr("netreset: no NIC found\n");
- return;
- }
- if (!nd->reset) {
- io_putstr("netreset: not supported by ");
- io_putstr(nd->name);
- io_putchar('\n');
- return;
- }
- io_putstr("netreset: re-initializing ");
- io_putstr(nd->name);
- io_putstr("...\n");
- nd->reset();
- nd->print_info();
- }
- /* ---- netloop: PHY loopback self-test (no cable/peer needed) ---- */
- static void netloop_run(void)
- {
- const struct netdev *nd = netdev_active();
- unsigned char frame[64], mac[6];
- unsigned int i, deadline;
- int got = 0;
- if (!nd) {
- io_putstr("netloop: no NIC found\n");
- return;
- }
- if (!nd->set_loopback) {
- io_putstr("netloop: not supported by ");
- io_putstr(nd->name);
- io_putchar('\n');
- return;
- }
- nd->get_mac(mac);
- for (i = 0; i < 6; i++)
- frame[i] = 0xFF; /* broadcast */
- for (i = 0; i < 6; i++)
- frame[6 + i] = mac[i];
- frame[12] = 0x88;
- frame[13] = 0xB6; /* local experimental */
- frame[14] = 'N'; frame[15] = 'E'; frame[16] = 'B';
- frame[17] = 'O'; frame[18] = 'S'; frame[19] = 'L';
- frame[20] = 'O'; frame[21] = 'O';
- for (i = 22; i < 64; i++)
- frame[i] = 0x00;
- io_putstr("netloop: PHY loopback on, sending...\n");
- nd->set_loopback(1);
- if (nd->tx(frame, sizeof(frame)) != 0) {
- io_putstr("netloop: transmit failed\n");
- nd->set_loopback(0);
- return;
- }
- deadline = get_ticks_ms() + 1000;
- while (get_ticks_ms() < deadline) {
- int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
- if (len < 0)
- continue;
- if (len >= 22 &&
- net_rx_buf[12] == 0x88 && net_rx_buf[13] == 0xB6 &&
- net_rx_buf[14] == 'N' && net_rx_buf[15] == 'E' &&
- net_rx_buf[16] == 'B' && net_rx_buf[17] == 'O' &&
- net_rx_buf[18] == 'S') {
- got = 1;
- break;
- }
- }
- nd->set_loopback(0);
- io_putstr(got ? "netloop: PASS (frame came back)\n"
- : "netloop: FAIL (no frame came back)\n");
- }
- void program_netloop_main(void)
- {
- netdev_acquire();
- netloop_run();
- netdev_release();
- }
- /* ---- netping: active ARP + ICMP echo ---- */
- #define NETPING_COUNT 4
- #define NETPING_ID 0x4E42 /* "NB" */
- #define NETPING_WAIT_MS 1000
- static unsigned int netping_sent_ms[NETPING_COUNT];
- /* ARP request "who has target? tell my_ip" -> 42-byte frame. */
- static unsigned int build_arp_request(const unsigned char *my_mac,
- const unsigned char *my_ip,
- const unsigned char *target_ip,
- unsigned char *out)
- {
- unsigned char *a = out + 14;
- unsigned int i;
- for (i = 0; i < 6; i++)
- out[i] = 0xFF; /* broadcast */
- for (i = 0; i < 6; i++)
- out[6 + i] = my_mac[i];
- out[12] = 0x08;
- out[13] = 0x06;
- a[0] = 0; a[1] = 1; /* htype Ethernet */
- a[2] = 0x08; a[3] = 0; /* ptype IPv4 */
- a[4] = 6; a[5] = 4;
- a[6] = 0; a[7] = 1; /* opcode request */
- for (i = 0; i < 6; i++) /* sha */
- a[8 + i] = my_mac[i];
- for (i = 0; i < 4; i++) /* spa */
- a[14 + i] = my_ip[i];
- for (i = 0; i < 6; i++) /* tha */
- a[18 + i] = 0;
- for (i = 0; i < 4; i++) /* tpa */
- a[24 + i] = target_ip[i];
- return 42;
- }
- /* 1 + peer MAC if frame is an ARP reply from target_ip. */
- static int parse_arp_reply(const unsigned char *f, unsigned int len,
- const unsigned char *target_ip, unsigned char *mac)
- {
- const unsigned char *a = f + 14;
- unsigned int i;
- if (len < 14 + 28)
- return 0;
- if ((((unsigned int)f[12] << 8) | f[13]) != ET_ARP)
- return 0;
- if ((((unsigned int)a[6] << 8) | a[7]) != 2) /* opcode reply */
- return 0;
- if (a[4] != 6 || a[5] != 4)
- return 0;
- if (!ip_eq(a + 14, target_ip)) /* sender IP == target */
- return 0;
- for (i = 0; i < 6; i++)
- mac[i] = a[8 + i];
- return 1;
- }
- /* ICMP echo request (14 + 20 + 16 bytes; the driver pads to 60). */
- static unsigned int build_icmp_echo(const unsigned char *src_mac,
- const unsigned char *dst_mac,
- const unsigned char *src_ip,
- const unsigned char *dst_ip,
- unsigned short seq, unsigned char *out)
- {
- unsigned char *ip = out + 14;
- unsigned char *ic = ip + 20;
- unsigned char *pl = ic + 8;
- unsigned short cs;
- unsigned int i;
- for (i = 0; i < 6; i++)
- out[i] = dst_mac[i];
- for (i = 0; i < 6; i++)
- out[6 + i] = src_mac[i];
- out[12] = 0x08;
- out[13] = 0x00;
- for (i = 0; i < 20; i++)
- ip[i] = 0;
- ip[0] = 0x45;
- ip[2] = 0; ip[3] = 36; /* total length */
- ip[8] = 64; /* TTL */
- ip[9] = IPPROTO_ICMP;
- for (i = 0; i < 4; i++)
- ip[12 + i] = src_ip[i];
- for (i = 0; i < 4; i++)
- ip[16 + i] = dst_ip[i];
- cs = net_checksum(ip, 20);
- ip[10] = (unsigned char)(cs >> 8);
- ip[11] = (unsigned char)(cs & 0xFF);
- ic[0] = ICMP_ECHO_REQUEST;
- ic[1] = 0;
- ic[2] = 0; ic[3] = 0;
- ic[4] = (unsigned char)(NETPING_ID >> 8);
- ic[5] = (unsigned char)(NETPING_ID & 0xFF);
- ic[6] = (unsigned char)(seq >> 8);
- ic[7] = (unsigned char)(seq & 0xFF);
- pl[0] = 'n'; pl[1] = 'e'; pl[2] = 'b'; pl[3] = 'o';
- pl[4] = 's'; pl[5] = 0; pl[6] = 0; pl[7] = 0;
- cs = net_checksum(ic, 16);
- ic[2] = (unsigned char)(cs >> 8);
- ic[3] = (unsigned char)(cs & 0xFF);
- return 14 + 20 + 16;
- }
- /* 1 + ICMP echo-reply seq for our IP from peer_ip. */
- static int parse_icmp_reply(const unsigned char *f, unsigned int len,
- const unsigned char *my_ip,
- const unsigned char *peer_ip,
- unsigned short *seq)
- {
- const unsigned char *ip = f + 14;
- const unsigned char *ic;
- unsigned int ihl;
- unsigned short ip_len, id;
- if (len < 14 + 20)
- return 0;
- if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
- return 0;
- if ((ip[0] >> 4) != 4)
- return 0;
- ihl = (unsigned int)(ip[0] & 0x0F) * 4;
- if (ihl < 20 || ip[9] != IPPROTO_ICMP)
- return 0;
- ip_len = ((unsigned short)ip[2] << 8) | ip[3];
- if (ip_len < ihl + 8 || (unsigned int)ip_len > len - 14)
- return 0;
- if (!ip_eq(ip + 12, peer_ip)) /* src == peer */
- return 0;
- if (!ip_eq(ip + 16, my_ip)) /* dst == us */
- return 0;
- ic = ip + ihl;
- if (ic[0] != ICMP_ECHO_REPLY || ic[1] != 0)
- return 0;
- id = ((unsigned short)ic[4] << 8) | ic[5];
- if (id != NETPING_ID)
- return 0;
- *seq = ((unsigned short)ic[6] << 8) | ic[7];
- if (*seq < 1 || *seq > NETPING_COUNT)
- return 0;
- return 1;
- }
- /* Resolve target_ip's MAC by ARP (3 tries, 1 s each).
- * Returns 0 = resolved, -1 = no reply, -2 = transmit failed.
- * Prints the frames seen while waiting (capped) so a silent peer, a filtered
- * reply and a reply we refuse to parse can be told apart on screen. */
- static int netping_arp(const struct netdev *nd, const unsigned char *my_mac,
- const unsigned char *my_ip, const unsigned char *target_ip,
- unsigned char *target_mac)
- {
- unsigned char frame[64];
- unsigned int tries, seen = 0;
- for (tries = 0; tries < 3; tries++) {
- unsigned int deadline = get_ticks_ms() + NETPING_WAIT_MS;
- unsigned int flen = build_arp_request(my_mac, my_ip, target_ip, frame);
- if (nd->tx(frame, flen) != 0)
- return -2;
- io_putstr("netping: ARP request sent (try ");
- put_dec(tries + 1);
- io_putstr("/3)\n");
- while (get_ticks_ms() < deadline) {
- int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
- if (len < 0)
- continue;
- if (seen < 8) {
- io_putstr("netping: rx ");
- net_decode_frame(net_rx_buf, (unsigned int)len);
- io_putchar('\n');
- seen++;
- }
- if (parse_arp_reply(net_rx_buf, (unsigned int)len,
- target_ip, target_mac))
- return 0;
- }
- }
- io_putstr("netping: saw ");
- put_dec(seen);
- io_putstr(" frame(s) while waiting\n");
- return -1;
- }
- static void netping_run(const char *arg)
- {
- const struct netdev *nd;
- unsigned char target[4], my_ip[4], my_mac[6], target_mac[6];
- unsigned int i, replies = 0;
- if (arg[0] == '\0' || net_parse_ip(arg, target) != 0) {
- io_putstr("netping: usage netping <a.b.c.d>\n");
- return;
- }
- nd = netdev_active();
- if (!nd) {
- io_putstr("netping: no NIC found\n");
- return;
- }
- nd->get_mac(my_mac);
- net_get_ip(my_ip);
- {
- int r = netping_arp(nd, my_mac, my_ip, target, target_mac);
- if (r == -2) {
- io_putstr("netping: transmit failed (see netregs)\n");
- return;
- }
- if (r != 0) {
- io_putstr("netping: no ARP reply from ");
- put_ip(target);
- io_putchar('\n');
- return;
- }
- }
- io_putstr("netping: ");
- put_ip(target);
- io_putstr(" is-at ");
- put_mac(target_mac);
- io_putchar('\n');
- for (i = 0; i < NETPING_COUNT; i++) {
- unsigned char frame[64];
- unsigned int flen, deadline;
- int got = 0;
- flen = build_icmp_echo(my_mac, target_mac, my_ip, target,
- (unsigned short)(i + 1), frame);
- netping_sent_ms[i] = get_ticks_ms();
- if (nd->tx(frame, flen) != 0) {
- io_putstr("netping: transmit failed\n");
- return;
- }
- deadline = get_ticks_ms() + NETPING_WAIT_MS;
- while (get_ticks_ms() < deadline && !got) {
- unsigned short seq;
- int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
- unsigned int rtt;
- if (len < 0)
- continue;
- if (!parse_icmp_reply(net_rx_buf, (unsigned int)len,
- my_ip, target, &seq))
- continue;
- rtt = get_ticks_ms() - netping_sent_ms[seq - 1];
- io_putstr("reply: seq=");
- put_dec(seq);
- io_putstr(" rtt=");
- if (rtt == 0)
- io_putstr("<1");
- else
- put_dec(rtt);
- io_putstr("ms\n");
- got = 1;
- replies++;
- }
- if (!got) {
- io_putstr("netping: timeout, seq=");
- put_dec(i + 1);
- io_putchar('\n');
- }
- }
- io_putstr("netping: ");
- put_dec(replies);
- io_putstr("/");
- put_dec(NETPING_COUNT);
- io_putstr(" replies from ");
- put_ip(target);
- io_putchar('\n');
- }
- void program_netping_main(const char *arg)
- {
- netdev_acquire();
- netping_run(arg);
- netdev_release();
- }
|