1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2019 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #include "py/mphal.h"
28 #include "shared/netutils/netutils.h"
29
get_be16(const uint8_t * buf)30 static uint32_t get_be16(const uint8_t *buf) {
31 return buf[0] << 8 | buf[1];
32 }
33
get_be32(const uint8_t * buf)34 static uint32_t get_be32(const uint8_t *buf) {
35 return buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
36 }
37
dump_hex_bytes(const mp_print_t * print,size_t len,const uint8_t * buf)38 static void dump_hex_bytes(const mp_print_t *print, size_t len, const uint8_t *buf) {
39 for (size_t i = 0; i < len; ++i) {
40 mp_printf(print, " %02x", buf[i]);
41 }
42 }
43
ethertype_str(uint16_t type)44 static const char *ethertype_str(uint16_t type) {
45 // A value between 0x0000 - 0x05dc (inclusive) indicates a length, not type
46 switch (type) {
47 case 0x0800:
48 return "IPv4";
49 case 0x0806:
50 return "ARP";
51 case 0x86dd:
52 return "IPv6";
53 default:
54 return NULL;
55 }
56 }
57
netutils_ethernet_trace(const mp_print_t * print,size_t len,const uint8_t * buf,unsigned int flags)58 void netutils_ethernet_trace(const mp_print_t *print, size_t len, const uint8_t *buf, unsigned int flags) {
59 mp_printf(print, "[% 8d] ETH%cX len=%u", mp_hal_ticks_ms(), flags & NETUTILS_TRACE_IS_TX ? 'T' : 'R', len);
60 mp_printf(print, " dst=%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
61 mp_printf(print, " src=%02x:%02x:%02x:%02x:%02x:%02x", buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
62
63 const char *ethertype = ethertype_str(buf[12] << 8 | buf[13]);
64 if (ethertype) {
65 mp_printf(print, " type=%s", ethertype);
66 } else {
67 mp_printf(print, " type=0x%04x", buf[12] << 8 | buf[13]);
68 }
69 if (len > 14) {
70 len -= 14;
71 buf += 14;
72 if (buf[-2] == 0x08 && buf[-1] == 0x00 && buf[0] == 0x45) {
73 // IPv4 packet
74 len = get_be16(buf + 2);
75 mp_printf(print, " srcip=%u.%u.%u.%u dstip=%u.%u.%u.%u",
76 buf[12], buf[13], buf[14], buf[15],
77 buf[16], buf[17], buf[18], buf[19]);
78 uint8_t prot = buf[9];
79 buf += 20;
80 len -= 20;
81 if (prot == 6) {
82 // TCP packet
83 uint16_t srcport = get_be16(buf);
84 uint16_t dstport = get_be16(buf + 2);
85 uint32_t seqnum = get_be32(buf + 4);
86 uint32_t acknum = get_be32(buf + 8);
87 uint16_t dataoff_flags = get_be16(buf + 12);
88 uint16_t winsz = get_be16(buf + 14);
89 mp_printf(print, " TCP srcport=%u dstport=%u seqnum=%u acknum=%u dataoff=%u flags=%x winsz=%u",
90 srcport, dstport, (unsigned)seqnum, (unsigned)acknum, dataoff_flags >> 12, dataoff_flags & 0x1ff, winsz);
91 buf += 20;
92 len -= 20;
93 if (dataoff_flags >> 12 > 5) {
94 mp_printf(print, " opts=");
95 size_t opts_len = ((dataoff_flags >> 12) - 5) * 4;
96 dump_hex_bytes(print, opts_len, buf);
97 buf += opts_len;
98 len -= opts_len;
99 }
100 } else if (prot == 17) {
101 // UDP packet
102 uint16_t srcport = get_be16(buf);
103 uint16_t dstport = get_be16(buf + 2);
104 mp_printf(print, " UDP srcport=%u dstport=%u", srcport, dstport);
105 len = get_be16(buf + 4);
106 buf += 8;
107 if ((srcport == 67 && dstport == 68) || (srcport == 68 && dstport == 67)) {
108 // DHCP
109 if (srcport == 67) {
110 mp_printf(print, " DHCPS");
111 } else {
112 mp_printf(print, " DHCPC");
113 }
114 dump_hex_bytes(print, 12 + 16 + 16 + 64, buf);
115 size_t n = 12 + 16 + 16 + 64 + 128;
116 len -= n;
117 buf += n;
118 mp_printf(print, " opts:");
119 switch (buf[6]) {
120 case 1:
121 mp_printf(print, " DISCOVER");
122 break;
123 case 2:
124 mp_printf(print, " OFFER");
125 break;
126 case 3:
127 mp_printf(print, " REQUEST");
128 break;
129 case 4:
130 mp_printf(print, " DECLINE");
131 break;
132 case 5:
133 mp_printf(print, " ACK");
134 break;
135 case 6:
136 mp_printf(print, " NACK");
137 break;
138 case 7:
139 mp_printf(print, " RELEASE");
140 break;
141 case 8:
142 mp_printf(print, " INFORM");
143 break;
144 }
145 }
146 } else {
147 // Non-UDP packet
148 mp_printf(print, " prot=%u", prot);
149 }
150 } else if (buf[-2] == 0x86 && buf[-1] == 0xdd && (buf[0] >> 4) == 6) {
151 // IPv6 packet
152 uint32_t h = get_be32(buf);
153 uint16_t l = get_be16(buf + 4);
154 mp_printf(print, " tclass=%u flow=%u len=%u nexthdr=%u hoplimit=%u", (unsigned)((h >> 20) & 0xff), (unsigned)(h & 0xfffff), l, buf[6], buf[7]);
155 mp_printf(print, " srcip=");
156 dump_hex_bytes(print, 16, buf + 8);
157 mp_printf(print, " dstip=");
158 dump_hex_bytes(print, 16, buf + 24);
159 buf += 40;
160 len -= 40;
161 }
162 if (flags & NETUTILS_TRACE_PAYLOAD) {
163 mp_printf(print, " data=");
164 dump_hex_bytes(print, len, buf);
165 }
166 }
167 if (flags & NETUTILS_TRACE_NEWLINE) {
168 mp_printf(print, "\n");
169 }
170 }
171