diff options
-rw-r--r-- | src/kernel/arch/amd64/driver/rtl8139.c | 7 | ||||
-rw-r--r-- | src/user/app/ethdump/ethdump.c | 29 |
2 files changed, 30 insertions, 6 deletions
diff --git a/src/kernel/arch/amd64/driver/rtl8139.c b/src/kernel/arch/amd64/driver/rtl8139.c index d9e5af0..3f6d07f 100644 --- a/src/kernel/arch/amd64/driver/rtl8139.c +++ b/src/kernel/arch/amd64/driver/rtl8139.c @@ -92,7 +92,7 @@ void rtl8139_irq(void) { } static int try_rx(struct pagedir *pages, void __user *dest, size_t dlen) { - uint16_t flags, size, pktsize; + uint16_t flags, size; /* bit 0 - Rx Buffer Empty */ if (port_in8(iobase + CMD) & 1) return -1; @@ -107,17 +107,16 @@ static int try_rx(struct pagedir *pages, void __user *dest, size_t dlen) { size = *(uint16_t*)(rxbuf + rxpos); rxpos += 2; if (size == 0) panic_invalid_state(); - pktsize = size - 4; // kprintf("packet size 0x%x, flags 0x%x, rxpos %x\n", size, flags, rxpos - 4); - virt_cpy_to(pages, dest, rxbuf + rxpos, pktsize); + virt_cpy_to(pages, dest, rxbuf + rxpos, size); rxpos += size; rxpos = (rxpos + 3) & ~3; while (rxpos >= rxbuf_baselen) rxpos -= rxbuf_baselen; /* the device adds the 0x10 back, it's supposed to avoid overflow */ port_out16(iobase + CAPR, rxpos - 0x10); - return pktsize; + return size; } static void accept(struct vfs_request *req) { diff --git a/src/user/app/ethdump/ethdump.c b/src/user/app/ethdump/ethdump.c index 8ac1cbf..50dd1f9 100644 --- a/src/user/app/ethdump/ethdump.c +++ b/src/user/app/ethdump/ethdump.c @@ -77,9 +77,27 @@ static void parse_ipv4(const uint8_t *buf, size_t len) { } } +/* https://www.w3.org/TR/PNG/#D-CRCAppendix */ +uint32_t crc_table[256]; +static uint32_t crc32(const uint8_t *buf, size_t len) { + if (!crc_table[1]) { + eprintf("building crc cache"); + for (int i = 0; i < 256; i++) { + uint32_t c = i; + for (int j = 0; j < 8; j++) + c = ((c&1) ? 0xedb88320 : 0) ^ (c >> 1); + crc_table[i] = c; + } + } + + uint32_t c = 0xFFFFFFFF; + for (size_t i = 0; i < len; i++) + c = crc_table[(c ^ buf[i]) & 0xff] ^ (c >> 8); + return ~c; +} + static void parse_ethernet(const uint8_t *buf, size_t len) { uint8_t dmac[6], smac[6]; - uint16_t ethertype; if (len < 60) return; for (int i = 0; i < 6; i++) dmac[i] = buf[i]; @@ -89,7 +107,14 @@ static void parse_ethernet(const uint8_t *buf, size_t len) { printf("to %02x:%02x:%02x:%02x:%02x:%02x\n", dmac[0], dmac[1], dmac[2], dmac[3], dmac[4], dmac[5]); - ethertype = (buf[12] << 8) | buf[13]; + /* byte order switched on purpose */ + uint32_t crc = (buf[len - 1] << 24) + | (buf[len - 2] << 16) + | (buf[len - 3] << 8) + | (buf[len - 4] << 0); + printf("fcf %x, crc %x\n", crc, crc32(buf, len - 4)); + + uint16_t ethertype = (buf[12] << 8) | buf[13]; if (ethertype == 0x800) { printf("ethertype %u - IPv4\n", ethertype); parse_ipv4(buf + 14, len - 14); |