Ver Fonte

Add ARP and ICMP support

Johannes Zellner há 1 mês atrás
pai
commit
e835d0f6c6
6 ficheiros alterados com 559 adições e 15 exclusões
  1. 6 3
      Makefile
  2. 43 5
      ne2000.c
  3. 342 0
      net.c
  4. 35 0
      net.h
  5. 81 7
      netcmd.c
  6. 52 0
      terminal.c

+ 6 - 3
Makefile

@@ -131,7 +131,10 @@ lspci.o: lspci.c io.h pci.h
 ne2000.o: ne2000.c ne2000.h pci.h timer.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-netcmd.o: netcmd.c io.h timer.h ne2000.h
+netcmd.o: netcmd.c io.h timer.h ne2000.h net.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+net.o: net.c net.h io.h ne2000.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 blkdev.o: blkdev.c blkdev.h ata.h disk.h
@@ -197,7 +200,7 @@ switch.o: switch.S
 process.o: process.c process.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o ne2000.o netcmd.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o ne2000.o netcmd.o net.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -346,4 +349,4 @@ dev: kernel.bin kexec_send
 	./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o ne2000.o netcmd.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o ne2000.o netcmd.o net.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send

+ 43 - 5
ne2000.c

@@ -45,6 +45,7 @@
 /* ISR bits. */
 #define ISR_PTX    0x02
 #define ISR_TXE    0x08
+#define ISR_OVW    0x10
 #define ISR_RST    0x80
 
 /* RCR bits. */
@@ -214,9 +215,15 @@ int ne2000_init(void)
 	nwrite(NE_ISR, 0xFF);
 	nwrite(NE_IMR, 0x00);
 
-	/* Page 1: set CURR to the first free page. */
+	/* Page 1: write the MAC to PAR0-5 (so unicast frames to us are
+	   accepted even if promiscuous mode is unreliable), then set CURR. */
 	nwrite(NE_CR, CR_NODMA | CR_START | CR_PAGE1);
-	nwrite(0x07, RX_START + 1);
+	{
+		unsigned int i;
+		for (i = 0; i < 6; i++)
+			nwrite((unsigned char)(NE_PSTART + i), mac[i]);
+	}
+	nwrite(0x07, RX_START + 1);   /* CURR = first free page */
 
 	/* Page 0, start the NIC. */
 	nwrite(NE_CR, CR_NODMA | CR_START);
@@ -253,6 +260,22 @@ int ne2000_rx(unsigned char *buf, unsigned int maxlen)
 
 		/* BNRY (page 0) and CURR (page 1). */
 		nwrite(NE_CR, CR_NODMA | CR_START);
+
+		/* Ring overflow: the card wrapped and clobbered unread frames,
+		   so the "next" pointers are unreliable. Discard the whole ring
+		   and start fresh, or we'd re-read stale frames (duplicates). */
+		if (nread(NE_ISR) & ISR_OVW) {
+			nwrite(NE_CR, CR_NODMA | CR_START | CR_PAGE1);
+			cur = nread(0x07);
+			nwrite(NE_CR, CR_NODMA | CR_START);
+			if (cur == RX_START)
+				nwrite(NE_BNRY, RX_STOP - 1);
+			else
+				nwrite(NE_BNRY, cur - 1);
+			nwrite(NE_ISR, ISR_OVW);
+			return -1;
+		}
+
 		bnry = nread(NE_BNRY);
 		nwrite(NE_CR, CR_NODMA | CR_START | CR_PAGE1);
 		cur = nread(0x07);
@@ -297,6 +320,9 @@ int ne2000_rx(unsigned char *buf, unsigned int maxlen)
 
 int ne2000_tx(const unsigned char *buf, unsigned int len)
 {
+	static unsigned char pad[60];
+	const unsigned char *p = buf;
+	unsigned int txlen = len;
 	unsigned int t0;
 	unsigned char isr, tsr;
 
@@ -305,14 +331,26 @@ int ne2000_tx(const unsigned char *buf, unsigned int len)
 	if (len < 14 || len > 1514)
 		return -1;
 
+	/* Pad to the 60-byte Ethernet minimum (the NIC does not auto-pad;
+	   a runt frame would be dropped by the switch/peer). */
+	if (len < 60) {
+		unsigned int i;
+		for (i = 0; i < len; i++)
+			pad[i] = buf[i];
+		for (i = len; i < 60; i++)
+			pad[i] = 0;
+		p = pad;
+		txlen = 60;
+	}
+
 	/* Copy the frame into on-card SRAM. */
-	remote_write((unsigned short)(TX_PAGE << 8), buf, (unsigned short)len);
+	remote_write((unsigned short)(TX_PAGE << 8), p, (unsigned short)txlen);
 
 	/* Program transmit page + byte count. */
 	nwrite(NE_CR, CR_NODMA | CR_START);
 	nwrite(NE_TPSR, TX_PAGE);
-	nwrite(NE_TBCR0, len & 0xFF);
-	nwrite(NE_TBCR1, (len >> 8) & 0xFF);
+	nwrite(NE_TBCR0, txlen & 0xFF);
+	nwrite(NE_TBCR1, (txlen >> 8) & 0xFF);
 
 	/* Clear TX status bits, then fire. */
 	nwrite(NE_ISR, ISR_PTX | ISR_TXE);

+ 342 - 0
net.c

@@ -0,0 +1,342 @@
+/* Minimal network stack: ARP + ICMP echo (ping) over the NE2000 driver.
+   Freestanding, no malloc; fixed-size buffers and a single configured IP. */
+
+#include "io.h"
+#include "ne2000.h"
+#include "net.h"
+
+static unsigned char my_ip[4] = { 192, 168, 0, 99 };
+
+/* ---- small print helpers (framebuffer console) ---- */
+
+static void print_hex2(unsigned int v)
+{
+	static const char hex[] = "0123456789ABCDEF";
+	io_putchar(hex[(v >> 4) & 0xF]);
+	io_putchar(hex[v & 0xF]);
+}
+
+static void print_hex4(unsigned int v)
+{
+	print_hex2((v >> 8) & 0xFF);
+	print_hex2(v & 0xFF);
+}
+
+static void print_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 print_mac(const unsigned char *m)
+{
+	unsigned int i;
+	for (i = 0; i < 6; i++) {
+		if (i)
+			io_putchar(':');
+		print_hex2(m[i]);
+	}
+}
+
+static void print_ip(const unsigned char *p)
+{
+	unsigned int i;
+	for (i = 0; i < 4; i++) {
+		if (i)
+			io_putchar('.');
+		print_dec(p[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];
+}
+
+/* Internet checksum (16-bit one's complement, network byte order). */
+static unsigned short checksum(const void *data, unsigned int len)
+{
+	const unsigned char *p = (const unsigned char *)data;
+	unsigned int sum = 0;
+
+	while (len > 1) {
+		sum += ((unsigned int)p[0] << 8) | p[1];
+		p += 2;
+		len -= 2;
+	}
+	if (len)
+		sum += (unsigned int)p[0] << 8;
+	while (sum >> 16)
+		sum = (sum & 0xFFFFu) + (sum >> 16);
+	return (unsigned short)(~sum);
+}
+
+/* ---- IP address config ---- */
+
+void net_set_ip(const unsigned char ip[4])
+{
+	unsigned int i;
+	for (i = 0; i < 4; i++)
+		my_ip[i] = ip[i];
+}
+
+void net_get_ip(unsigned char ip[4])
+{
+	unsigned int i;
+	for (i = 0; i < 4; i++)
+		ip[i] = my_ip[i];
+}
+
+int net_parse_ip(const char *s, unsigned char ip[4])
+{
+	unsigned int i, val = 0, nparts = 0;
+	int have_digit = 0;
+
+	for (i = 0; ; i++) {
+		char c = s[i];
+		if (c >= '0' && c <= '9') {
+			val = val * 10 + (unsigned int)(c - '0');
+			if (val > 255)
+				return -1;
+			have_digit = 1;
+		} else if (c == '.' || c == '\0') {
+			if (!have_digit || nparts >= 4)
+				return -1;
+			ip[nparts++] = (unsigned char)val;
+			val = 0;
+			have_digit = 0;
+			if (c == '\0')
+				break;
+		} else {
+			return -1;
+		}
+	}
+	return (nparts == 4) ? 0 : -1;
+}
+
+void net_print_ip(void)
+{
+	print_ip(my_ip);
+}
+
+/* ---- ethertype naming ---- */
+
+const char *net_ethertype_name(unsigned short et)
+{
+	switch (et) {
+	case ET_IPV4: return "IPv4";
+	case ET_ARP:  return "ARP";
+	case ET_IPV6: return "IPv6";
+	default:      return 0;
+	}
+}
+
+/* ---- frame decoding ---- */
+
+void net_decode_frame(const unsigned char *f, unsigned int len)
+{
+	unsigned short et;
+	const char *name;
+
+	if (len < 14) {
+		io_putstr("(short frame len=");
+		print_dec(len);
+		io_putstr(")");
+		return;
+	}
+
+	et = ((unsigned short)f[12] << 8) | f[13];
+
+	print_mac(f + 6);
+	io_putstr(" -> ");
+	print_mac(f);
+	io_putstr("  ");
+	name = net_ethertype_name(et);
+	if (name)
+		io_putstr(name);
+	else {
+		io_putstr("0x");
+		print_hex4(et);
+	}
+	io_putstr(" len=");
+	print_dec(len);
+
+	if (et == ET_ARP && len >= 14 + 28) {
+		const unsigned char *a = f + 14;
+		unsigned short op = ((unsigned short)a[6] << 8) | a[7];
+		if (op == 1) {
+			io_putstr("  who-has ");
+			print_ip(a + 24);
+			io_putstr(" tell ");
+			print_ip(a + 14);
+		} else if (op == 2) {
+			io_putstr("  ");
+			print_ip(a + 14);
+			io_putstr(" is-at ");
+			print_mac(a + 8);
+		}
+	} else if (et == ET_IPV4 && len >= 14 + 20) {
+		const unsigned char *ip = f + 14;
+		unsigned int ihl = (unsigned int)(ip[0] & 0x0F) * 4;
+		unsigned char proto = ip[9];
+		io_putstr("  ");
+		print_ip(ip + 12);
+		io_putstr(" -> ");
+		print_ip(ip + 16);
+		io_putstr(" proto=");
+		print_dec(proto);
+		if (proto == IPPROTO_ICMP && ihl >= 20 && len >= 14 + ihl + 8) {
+			const unsigned char *ic = ip + ihl;
+			io_putstr("  ICMP type=");
+			print_dec(ic[0]);
+			io_putstr(" code=");
+			print_dec(ic[1]);
+		}
+	}
+}
+
+/* ---- reply generation ---- */
+
+int net_build_reply(const unsigned char *frame, unsigned int len,
+		    unsigned char *out, unsigned int maxout)
+{
+	unsigned char mac[6];
+	unsigned short et;
+	unsigned int i;
+
+	if (len < 14)
+		return 0;
+	et = ((unsigned short)frame[12] << 8) | frame[13];
+
+	ne2000_get_mac(mac);
+
+	/* ARP request for our IP -> ARP reply. */
+	if (et == ET_ARP && len >= 14 + 28) {
+		const unsigned char *a = frame + 14;
+		unsigned short op = ((unsigned short)a[6] << 8) | a[7];
+
+		if (op != 1)
+			return 0;
+		if ((((unsigned short)a[0] << 8) | a[1]) != 1)   /* htype Ethernet */
+			return 0;
+		if ((((unsigned short)a[2] << 8) | a[3]) != ET_IPV4)
+			return 0;
+		if (a[4] != 6 || a[5] != 4)
+			return 0;
+		if (!ip_eq(a + 24, my_ip))                       /* target != us */
+			return 0;
+
+		for (i = 0; i < 6; i++)
+			out[i] = frame[6 + i];            /* dst = requester */
+		for (i = 0; i < 6; i++)
+			out[6 + i] = mac[i];              /* src = us */
+		out[12] = 0x08; out[13] = 0x06;       /* ethertype ARP */
+
+		{
+			unsigned char *r = out + 14;
+			r[0] = 0x00; r[1] = 0x01;         /* htype Ethernet */
+			r[2] = 0x08; r[3] = 0x00;         /* ptype IPv4 */
+			r[4] = 6; r[5] = 4;
+			r[6] = 0x00; r[7] = 0x02;         /* opcode reply */
+			for (i = 0; i < 6; i++)
+				r[8 + i] = mac[i];        /* sha */
+			for (i = 0; i < 4; i++)
+				r[14 + i] = my_ip[i];     /* spa */
+			for (i = 0; i < 6; i++)
+				r[18 + i] = a[8 + i];     /* tha = requester MAC */
+			for (i = 0; i < 4; i++)
+				r[24 + i] = a[14 + i];    /* tpa = requester IP */
+		}
+		return 42;
+	}
+
+	/* ICMP echo request for our IP -> ICMP echo reply. */
+	if (et == ET_IPV4 && len >= 14 + 20) {
+		const unsigned char *ip = frame + 14;
+		unsigned int ihl, icmp_len, reply_len;
+		unsigned short ip_len;
+		const unsigned char *ic;
+		unsigned char *rip, *ric;
+
+		if ((ip[0] >> 4) != 4)
+			return 0;
+		ihl = (unsigned int)(ip[0] & 0x0F) * 4;
+		if (ihl < 20)
+			return 0;
+		if (ip[9] != IPPROTO_ICMP)
+			return 0;
+		if (!ip_eq(ip + 16, my_ip))
+			return 0;
+
+		ip_len = ((unsigned short)ip[2] << 8) | ip[3];
+		if (ip_len < ihl || ip_len > len - 14)
+			return 0;
+		if (len < 14 + ihl + 8)
+			return 0;
+
+		ic = ip + ihl;
+		if (ic[0] != ICMP_ECHO_REQUEST)
+			return 0;
+
+		icmp_len = ip_len - ihl;
+		reply_len = 14 + ip_len;
+		if (reply_len > maxout)
+			return 0;
+
+		/* Ethernet header. */
+		for (i = 0; i < 6; i++)
+			out[i] = frame[6 + i];
+		for (i = 0; i < 6; i++)
+			out[6 + i] = mac[i];
+		out[12] = 0x08; out[13] = 0x00;       /* ethertype IPv4 */
+
+		/* IPv4 header (IHL 5, no options). */
+		rip = out + 14;
+		rip[0] = 0x45;
+		rip[1] = 0x00;
+		rip[2] = ip[2]; rip[3] = ip[3];       /* total length (same) */
+		rip[4] = ip[4]; rip[5] = ip[5];       /* id */
+		rip[6] = 0x00; rip[7] = 0x00;         /* flags/frag */
+		rip[8] = 64;                          /* TTL */
+		rip[9] = IPPROTO_ICMP;
+		rip[10] = 0; rip[11] = 0;             /* checksum (computed below) */
+		for (i = 0; i < 4; i++)
+			rip[12 + i] = my_ip[i];         /* src = us */
+		for (i = 0; i < 4; i++)
+			rip[16 + i] = ip[12 + i];       /* dst = requester */
+
+		/* ICMP: copy the whole request, then fix type/code/checksum. */
+		ric = rip + ihl;
+		for (i = 0; i < icmp_len; i++)
+			ric[i] = ic[i];
+		ric[0] = ICMP_ECHO_REPLY;
+		ric[1] = 0;
+		ric[2] = 0; ric[3] = 0;
+
+		{
+			unsigned short cs = checksum(rip, ihl);
+			rip[10] = (unsigned char)(cs >> 8);
+			rip[11] = (unsigned char)(cs & 0xFF);
+		}
+		{
+			unsigned short cs = checksum(ric, icmp_len);
+			ric[2] = (unsigned char)(cs >> 8);
+			ric[3] = (unsigned char)(cs & 0xFF);
+		}
+
+		return (int)reply_len;
+	}
+
+	return 0;
+}

+ 35 - 0
net.h

@@ -0,0 +1,35 @@
+/* Minimal network stack: ARP + ICMP echo (ping) over the NE2000 driver.
+   Parsing, decoding, reply generation, and the card's IPv4 address. */
+#ifndef NET_H
+#define NET_H
+
+/* Ethernet ethertypes. */
+#define ET_IPV4 0x0800
+#define ET_ARP  0x0806
+#define ET_IPV6 0x86DD
+
+/* IPv4 protocol numbers. */
+#define IPPROTO_ICMP 1
+#define IPPROTO_UDP  17
+#define IPPROTO_TCP  6
+
+/* ICMP message types. */
+#define ICMP_ECHO_REPLY   0
+#define ICMP_ECHO_REQUEST 8
+
+void net_set_ip(const unsigned char ip[4]);
+void net_get_ip(unsigned char ip[4]);
+int  net_parse_ip(const char *s, unsigned char ip[4]);  /* 0 on success, -1 on error */
+void net_print_ip(void);
+
+const char *net_ethertype_name(unsigned short et);
+
+/* Print one decoded frame summary line (dst/src/ethertype + ARP/IP/ICMP). */
+void net_decode_frame(const unsigned char *frame, unsigned int len);
+
+/* Build a reply to an ARP request or ICMP echo request addressed to us.
+   Returns reply length (> 0) or 0 if the frame needs no reply. */
+int net_build_reply(const unsigned char *frame, unsigned int len,
+		    unsigned char *out, unsigned int maxout);
+
+#endif

+ 81 - 7
netcmd.c

@@ -1,8 +1,17 @@
-/* Network test commands: netinfo, netrecv, netsend. */
+/* Network test commands: netinfo, netrecv, netsend, netip, ping. */
 
 #include "io.h"
 #include "timer.h"
 #include "ne2000.h"
+#include "net.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 0x30000 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)
 {
@@ -32,11 +41,15 @@ void program_netinfo_main(void)
 {
 	ne2000_init();
 	ne2000_print_info();
+	if (ne2000_present()) {
+		io_putstr("ip=");
+		net_print_ip();
+		io_putchar('\n');
+	}
 }
 
 void program_netrecv_main(void)
 {
-	unsigned char buf[2048];
 	unsigned int deadline, count = 0;
 
 	ne2000_init();
@@ -49,17 +62,18 @@ void program_netrecv_main(void)
 	deadline = get_ticks_ms() + 8000;
 
 	while (get_ticks_ms() < deadline) {
-		int len = ne2000_rx(buf, sizeof(buf));
+		int len = ne2000_rx(net_rx_buf, NET_FRAME_MAX);
 		unsigned int i;
 
 		if (len < 0)
 			continue;
 
-		io_putstr("rx ");
-		put_dec((unsigned int)len);
-		io_putstr(" bytes:\n");
+		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(buf[i]);
+			put_hex2(net_rx_buf[i]);
 			io_putchar(' ');
 			if ((i & 15) == 15)
 				io_putchar('\n');
@@ -111,3 +125,63 @@ void program_netsend_main(void)
 	}
 	io_putstr("netsend: sent 64-byte broadcast frame\n");
 }
+
+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');
+}
+
+void program_ping_main(void)
+{
+	unsigned int deadline, count = 0;
+
+	ne2000_init();
+	if (!ne2000_present()) {
+		io_putstr("ping: no NIC found\n");
+		return;
+	}
+
+	io_putstr("ping: answering ARP/ICMP for 30s (ip=");
+	net_print_ip();
+	io_putstr(")...\n");
+
+	deadline = get_ticks_ms() + 30000;
+	while (get_ticks_ms() < deadline) {
+		int len = ne2000_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 (ne2000_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");
+}

+ 52 - 0
terminal.c

@@ -34,6 +34,8 @@ extern void program_lspci_main(void);
 extern void program_netinfo_main(void);
 extern void program_netrecv_main(void);
 extern void program_netsend_main(void);
+extern void program_netip_main(const char *arg);
+extern void program_ping_main(void);
 
 /* Print the prompt (with the current directory) and set line_start. */
 static void term_print_prompt(struct term *t, int first)
@@ -310,6 +312,49 @@ static int is_cmd_netsend(const char *line, unsigned int line_len)
 	return (i == line_len);
 }
 
+static int is_cmd_netip(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
+{
+	unsigned int i = 0, j = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 5 > line_len)
+		return 0;
+	if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
+	    line[i+3] != 'i' || line[i+4] != 'p')
+		return 0;
+	i += 5;
+	if (i < line_len && line[i] != ' ')
+		return 0;  /* "netip" must be a whole word */
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len) {
+		if (buf_size > 0)
+			arg_buf[0] = '\0';
+		return 1;
+	}
+	while (i < line_len && j + 1 < buf_size)
+		arg_buf[j++] = line[i++];
+	arg_buf[j] = '\0';
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
+static int is_cmd_ping(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 4 > line_len)
+		return 0;
+	if (line[i] != 'p' || line[i+1] != 'i' || line[i+2] != 'n' || line[i+3] != 'g')
+		return 0;
+	i += 4;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
 static int is_cmd_space(const char *line, unsigned int line_len)
 {
 	unsigned int i = 0;
@@ -898,6 +943,7 @@ void term_run(struct term *t)
 					char rm_fname[RM_FNAME_MAX];
 					char ls_path[LS_PATH_MAX];
 					char format_dev[FORMAT_DEV_MAX];
+					char netip_arg[16];
 					char resolved[VFS_PATH_MAX];
 					char save_resolved[SAVE_ARGS_MAX + VFS_PATH_MAX];
 					unsigned int kill_pid;
@@ -929,6 +975,10 @@ void term_run(struct term *t)
 						term_spawn(t, "netrecv", program_netrecv_main);
 					} else if (is_cmd_netsend(t->line, t->line_len)) {
 						term_spawn(t, "netsend", program_netsend_main);
+					} else if (is_cmd_netip(t->line, t->line_len, netip_arg, sizeof(netip_arg))) {
+						term_spawn_arg(t, "netip", program_netip_main, netip_arg);
+					} else if (is_cmd_ping(t->line, t->line_len)) {
+						term_spawn(t, "ping", program_ping_main);
 					} else if (is_cmd_storage(t->line, t->line_len)) {
 						term_spawn(t, "storage", program_storage_main);
 					} else if (is_cmd_clock(t->line, t->line_len)) {
@@ -1026,6 +1076,8 @@ void term_run(struct term *t)
 						io_putstr("netinfo\n");
 						io_putstr("netrecv\n");
 						io_putstr("netsend\n");
+						io_putstr("netip [a.b.c.d]\n");
+						io_putstr("ping\n");
 						io_putstr("ps\n");
 						io_putstr("kill <pid>\n");
 						io_putstr("kexec\n");