uipc_mbufdebug.c revision 1.7 1 1.7 msaitoh /* $NetBSD: uipc_mbufdebug.c,v 1.7 2018/10/18 05:44:19 msaitoh Exp $ */
2 1.1 msaitoh
3 1.1 msaitoh /*
4 1.1 msaitoh * Copyright (C) 2017 Internet Initiative Japan Inc.
5 1.1 msaitoh * All rights reserved.
6 1.1 msaitoh *
7 1.1 msaitoh * Redistribution and use in source and binary forms, with or without
8 1.1 msaitoh * modification, are permitted provided that the following conditions
9 1.1 msaitoh * are met:
10 1.1 msaitoh * 1. Redistributions of source code must retain the above copyright
11 1.1 msaitoh * notice, this list of conditions and the following disclaimer.
12 1.1 msaitoh * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 msaitoh * notice, this list of conditions and the following disclaimer in the
14 1.1 msaitoh * documentation and/or other materials provided with the distribution.
15 1.1 msaitoh *
16 1.1 msaitoh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 msaitoh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 msaitoh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 msaitoh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 msaitoh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 msaitoh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 msaitoh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 msaitoh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 msaitoh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 msaitoh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 msaitoh * POSSIBILITY OF SUCH DAMAGE.
27 1.1 msaitoh */
28 1.1 msaitoh
29 1.1 msaitoh #include <sys/cdefs.h>
30 1.7 msaitoh __KERNEL_RCSID(0, "$NetBSD: uipc_mbufdebug.c,v 1.7 2018/10/18 05:44:19 msaitoh Exp $");
31 1.1 msaitoh
32 1.1 msaitoh #include <sys/param.h>
33 1.1 msaitoh #include <sys/systm.h>
34 1.1 msaitoh #include <sys/proc.h>
35 1.1 msaitoh #include <sys/malloc.h>
36 1.1 msaitoh #include <sys/mbuf.h>
37 1.1 msaitoh
38 1.1 msaitoh #include <net/if.h>
39 1.1 msaitoh #include <net/if_ether.h>
40 1.1 msaitoh #include <net/ppp_defs.h>
41 1.1 msaitoh #include <net/if_arp.h>
42 1.1 msaitoh
43 1.1 msaitoh #include <netinet/in.h>
44 1.1 msaitoh #include <netinet/in_systm.h>
45 1.1 msaitoh #include <netinet/ip.h>
46 1.1 msaitoh #include <netinet/ip_icmp.h>
47 1.1 msaitoh #include <netinet/ip6.h>
48 1.1 msaitoh #include <netinet/icmp6.h>
49 1.1 msaitoh #include <netinet/if_inarp.h>
50 1.1 msaitoh #include <netinet/tcp.h>
51 1.1 msaitoh #include <netinet/udp.h>
52 1.1 msaitoh
53 1.1 msaitoh #define EXAMINE_HEX_LIMIT 128
54 1.1 msaitoh #define EXAMINE_HEX_COL 4
55 1.1 msaitoh
56 1.1 msaitoh /* mbuf operations without change of mbuf chain */
57 1.1 msaitoh static int m_peek_data(const struct mbuf *, int, int, void *);
58 1.1 msaitoh static unsigned int m_peek_len(const struct mbuf *, const char *);
59 1.1 msaitoh
60 1.1 msaitoh /* utility */
61 1.1 msaitoh static char *str_ethaddr(const uint8_t *);
62 1.1 msaitoh static char *str_ipaddr(const struct in_addr *);
63 1.1 msaitoh static char *str_ip6addr(const struct in6_addr *);
64 1.1 msaitoh static const char *str_ipproto(const uint8_t);
65 1.1 msaitoh
66 1.1 msaitoh /* header structure for some protocol */
67 1.1 msaitoh struct pppoehdr {
68 1.1 msaitoh uint8_t vertype;
69 1.1 msaitoh uint8_t code;
70 1.1 msaitoh uint16_t session;
71 1.1 msaitoh uint16_t plen;
72 1.1 msaitoh } __attribute__((__packed__));
73 1.1 msaitoh
74 1.1 msaitoh struct pppoetag {
75 1.1 msaitoh uint16_t tag;
76 1.1 msaitoh uint16_t len;
77 1.1 msaitoh } __attribute__((__packed__));
78 1.1 msaitoh
79 1.1 msaitoh #define PPPOE_TAG_EOL 0x0000
80 1.1 msaitoh #define PPPOE_CODE_PADI 0x09 /* Active Discovery Initiation */
81 1.1 msaitoh #define PPPOE_CODE_PADO 0x07 /* Active Discovery Offer */
82 1.1 msaitoh #define PPPOE_CODE_PADR 0x19 /* Active Discovery Request */
83 1.1 msaitoh #define PPPOE_CODE_PADS 0x65 /* Active Discovery Session confirmation */
84 1.1 msaitoh #define PPPOE_CODE_PADT 0xA7 /* Active Discovery Terminate */
85 1.1 msaitoh
86 1.1 msaitoh struct ppp_header {
87 1.1 msaitoh uint8_t address;
88 1.1 msaitoh uint8_t control;
89 1.1 msaitoh uint16_t protocol;
90 1.1 msaitoh } __attribute__((__packed__));
91 1.1 msaitoh
92 1.1 msaitoh #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
93 1.1 msaitoh #define CISCO_UNICAST 0x0f /* Cisco unicast address */
94 1.1 msaitoh #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
95 1.1 msaitoh
96 1.1 msaitoh #ifndef NELEMS
97 1.1 msaitoh #define NELEMS(elem) ((sizeof(elem))/(sizeof((elem)[0])))
98 1.1 msaitoh #endif
99 1.1 msaitoh
100 1.1 msaitoh static int
101 1.1 msaitoh m_peek_data(const struct mbuf *m, int off, int len, void *vp)
102 1.1 msaitoh {
103 1.1 msaitoh unsigned int count;
104 1.1 msaitoh char *cp = vp;
105 1.1 msaitoh
106 1.1 msaitoh if (off < 0 || len < 0)
107 1.1 msaitoh return -1;
108 1.1 msaitoh
109 1.1 msaitoh while (off > 0) {
110 1.1 msaitoh if (m == 0)
111 1.1 msaitoh return -1;
112 1.1 msaitoh if (off < m->m_len)
113 1.1 msaitoh break;
114 1.1 msaitoh off -= m->m_len;
115 1.1 msaitoh m = m->m_next;
116 1.1 msaitoh }
117 1.1 msaitoh while (len > 0) {
118 1.1 msaitoh if (m == 0)
119 1.1 msaitoh return -1;
120 1.3 riastrad count = uimin(m->m_len - off, len);
121 1.1 msaitoh memcpy(cp, mtod(m, char *) + off, count);
122 1.1 msaitoh len -= count;
123 1.1 msaitoh cp += count;
124 1.1 msaitoh off = 0;
125 1.1 msaitoh m = m->m_next;
126 1.1 msaitoh }
127 1.1 msaitoh
128 1.1 msaitoh return 0;
129 1.1 msaitoh }
130 1.1 msaitoh
131 1.1 msaitoh static unsigned int
132 1.1 msaitoh m_peek_len(const struct mbuf *m, const char *modif)
133 1.1 msaitoh {
134 1.1 msaitoh const struct mbuf *m0;
135 1.1 msaitoh unsigned int pktlen;
136 1.6 msaitoh bool opt_c = false;
137 1.1 msaitoh unsigned char ch;
138 1.1 msaitoh
139 1.6 msaitoh while (modif && (ch = *(modif++)) != '\0') {
140 1.1 msaitoh switch (ch) {
141 1.1 msaitoh case 'c':
142 1.6 msaitoh opt_c = true;
143 1.1 msaitoh break;
144 1.1 msaitoh }
145 1.1 msaitoh }
146 1.1 msaitoh
147 1.7 msaitoh if (opt_c == true)
148 1.1 msaitoh return m->m_len;
149 1.1 msaitoh
150 1.1 msaitoh if ((m->m_flags & M_PKTHDR) != 0)
151 1.1 msaitoh return m->m_pkthdr.len;
152 1.1 msaitoh
153 1.1 msaitoh pktlen = 0;
154 1.1 msaitoh for (m0 = m; m0 != NULL; m0 = m0->m_next)
155 1.1 msaitoh pktlen += m0->m_len;
156 1.1 msaitoh
157 1.1 msaitoh return pktlen;
158 1.1 msaitoh }
159 1.1 msaitoh
160 1.1 msaitoh static char *
161 1.1 msaitoh str_ethaddr(const uint8_t *ap)
162 1.1 msaitoh {
163 1.1 msaitoh static char buf[3 * ETHER_ADDR_LEN];
164 1.1 msaitoh
165 1.1 msaitoh return ether_snprintf(buf, sizeof(buf), ap);
166 1.1 msaitoh }
167 1.1 msaitoh
168 1.1 msaitoh static char *
169 1.1 msaitoh str_ipaddr(const struct in_addr *ap)
170 1.1 msaitoh {
171 1.1 msaitoh static char buf[INET_ADDRSTRLEN];
172 1.1 msaitoh
173 1.1 msaitoh return IN_PRINT(buf, ap);
174 1.1 msaitoh }
175 1.1 msaitoh
176 1.1 msaitoh static char *
177 1.1 msaitoh str_ip6addr(const struct in6_addr *ap)
178 1.1 msaitoh {
179 1.1 msaitoh static char buf[INET6_ADDRSTRLEN];
180 1.1 msaitoh
181 1.1 msaitoh return IN6_PRINT(buf, ap);
182 1.1 msaitoh }
183 1.1 msaitoh
184 1.1 msaitoh static const char *
185 1.1 msaitoh str_ipproto(const uint8_t proto)
186 1.1 msaitoh {
187 1.7 msaitoh
188 1.1 msaitoh switch (proto) {
189 1.1 msaitoh case IPPROTO_HOPOPTS:
190 1.1 msaitoh return ("IPv6 Hop-by-Hop");
191 1.1 msaitoh break;
192 1.1 msaitoh case IPPROTO_TCP:
193 1.1 msaitoh return("TCP");
194 1.1 msaitoh break;
195 1.1 msaitoh case IPPROTO_UDP:
196 1.1 msaitoh return("UDP");
197 1.1 msaitoh break;
198 1.1 msaitoh case IPPROTO_ICMP:
199 1.1 msaitoh return("ICMP");
200 1.1 msaitoh break;
201 1.1 msaitoh case IPPROTO_IGMP:
202 1.1 msaitoh return("IGMP");
203 1.1 msaitoh break;
204 1.1 msaitoh case IPPROTO_ESP:
205 1.1 msaitoh return("ESP");
206 1.1 msaitoh break;
207 1.1 msaitoh case IPPROTO_AH:
208 1.1 msaitoh return("AH");
209 1.1 msaitoh break;
210 1.1 msaitoh case IPPROTO_IPV6_ICMP:
211 1.1 msaitoh return("ICMP6");
212 1.1 msaitoh default:
213 1.1 msaitoh return("unknown");
214 1.1 msaitoh break;
215 1.1 msaitoh }
216 1.1 msaitoh
217 1.1 msaitoh /* not reached */
218 1.1 msaitoh return NULL;
219 1.1 msaitoh }
220 1.1 msaitoh
221 1.6 msaitoh void
222 1.1 msaitoh m_examine_ether(const struct mbuf *m, int off, const char *modif,
223 1.1 msaitoh void (*pr)(const char *, ...))
224 1.1 msaitoh {
225 1.1 msaitoh struct ether_header eh;
226 1.1 msaitoh unsigned int pktlen;
227 1.1 msaitoh
228 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
229 1.1 msaitoh if (pktlen < sizeof(eh)) {
230 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
231 1.6 msaitoh pktlen, sizeof(eh));
232 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
233 1.1 msaitoh }
234 1.1 msaitoh
235 1.1 msaitoh if (m_peek_data(m, off, sizeof(eh), (void *)(&eh)) < 0) {
236 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
237 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
238 1.1 msaitoh }
239 1.1 msaitoh off += sizeof(eh);
240 1.1 msaitoh
241 1.1 msaitoh (*pr)("ETHER: DST = %s\n", str_ethaddr(eh.ether_dhost));
242 1.1 msaitoh (*pr)("ETHER: SRC = %s\n", str_ethaddr(eh.ether_shost));
243 1.1 msaitoh
244 1.1 msaitoh (*pr)("ETHER: TYPE = 0x%04x(", ntohs(eh.ether_type));
245 1.1 msaitoh switch (ntohs(eh.ether_type)) {
246 1.1 msaitoh case ETHERTYPE_PPPOE:
247 1.1 msaitoh (*pr)("PPPoE)\n");
248 1.1 msaitoh return m_examine_pppoe(m, off, modif, pr);
249 1.1 msaitoh break;
250 1.1 msaitoh case ETHERTYPE_ARP:
251 1.1 msaitoh (*pr)("ARP)\n");
252 1.1 msaitoh return m_examine_arp(m, off, modif, pr);
253 1.1 msaitoh break;
254 1.1 msaitoh case ETHERTYPE_IP:
255 1.1 msaitoh (*pr)("IPv4)\n");
256 1.1 msaitoh return m_examine_ip(m, off, modif, pr);
257 1.1 msaitoh break;
258 1.1 msaitoh case ETHERTYPE_IPV6:
259 1.1 msaitoh (*pr)("IPv6)\n");
260 1.1 msaitoh return m_examine_ip6(m, off, modif, pr);
261 1.1 msaitoh break;
262 1.1 msaitoh default:
263 1.1 msaitoh (*pr)("unknown)\n");
264 1.1 msaitoh break;
265 1.1 msaitoh }
266 1.1 msaitoh
267 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
268 1.1 msaitoh }
269 1.1 msaitoh
270 1.6 msaitoh void
271 1.1 msaitoh m_examine_pppoe(const struct mbuf *m, int off, const char *modif,
272 1.1 msaitoh void (*pr)(const char *, ...))
273 1.1 msaitoh {
274 1.1 msaitoh struct pppoehdr ph;
275 1.1 msaitoh struct pppoetag pt;
276 1.1 msaitoh unsigned int pktlen;
277 1.1 msaitoh uint8_t vt;
278 1.1 msaitoh
279 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
280 1.1 msaitoh if (pktlen < sizeof(ph)) {
281 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u, %u)\n", __func__,
282 1.6 msaitoh pktlen, sizeof(ph));
283 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
284 1.1 msaitoh }
285 1.1 msaitoh
286 1.1 msaitoh if (m_peek_data(m, off, sizeof(ph), (void *)(&ph)) < 0) {
287 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
288 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
289 1.1 msaitoh }
290 1.1 msaitoh off += sizeof(ph);
291 1.1 msaitoh
292 1.1 msaitoh while (off + sizeof(pt) > pktlen) {
293 1.1 msaitoh if (m_peek_data(m, off, sizeof(pt), (void *)(&pt)) < 0) {
294 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
295 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
296 1.1 msaitoh }
297 1.1 msaitoh off += sizeof(pt);
298 1.1 msaitoh
299 1.1 msaitoh if (ntohs(pt.tag) == PPPOE_TAG_EOL)
300 1.1 msaitoh break;
301 1.1 msaitoh off += ntohs(pt.len);
302 1.1 msaitoh }
303 1.1 msaitoh
304 1.1 msaitoh vt = ph.vertype;
305 1.1 msaitoh
306 1.1 msaitoh (*pr)("PPPoE: Version = %u\n", ((vt >> 4) & 0xff));
307 1.1 msaitoh (*pr)("PPPoE: Type = %u\n", (vt & 0xff));
308 1.1 msaitoh (*pr)("PPPoE: Code = %u(", ph.code);
309 1.1 msaitoh switch (ph.code) {
310 1.1 msaitoh case 0:
311 1.1 msaitoh (*pr)("DATA");
312 1.1 msaitoh break;
313 1.1 msaitoh case PPPOE_CODE_PADI:
314 1.1 msaitoh (*pr)("PADI");
315 1.1 msaitoh break;
316 1.1 msaitoh case PPPOE_CODE_PADO:
317 1.1 msaitoh (*pr)("PADO");
318 1.1 msaitoh break;
319 1.1 msaitoh case PPPOE_CODE_PADS:
320 1.1 msaitoh (*pr)("PADS");
321 1.1 msaitoh break;
322 1.1 msaitoh case PPPOE_CODE_PADT:
323 1.1 msaitoh (*pr)("PADT");
324 1.1 msaitoh break;
325 1.1 msaitoh default:
326 1.1 msaitoh (*pr)("unknown");
327 1.1 msaitoh break;
328 1.1 msaitoh }
329 1.1 msaitoh (*pr)(")\n");
330 1.1 msaitoh
331 1.1 msaitoh (*pr)("PPPoE: Session = 0x%04x\n", ntohs(ph.session));
332 1.1 msaitoh (*pr)("PPPoE: Payload Length = %u\n", ntohs(ph.plen));
333 1.1 msaitoh
334 1.1 msaitoh switch (ph.code) {
335 1.1 msaitoh case PPPOE_CODE_PADI:
336 1.1 msaitoh case PPPOE_CODE_PADO:
337 1.1 msaitoh case PPPOE_CODE_PADS:
338 1.1 msaitoh case PPPOE_CODE_PADT:
339 1.1 msaitoh (*pr)("No parser for PPPoE control frame.\n");
340 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
341 1.1 msaitoh break;
342 1.1 msaitoh }
343 1.1 msaitoh
344 1.1 msaitoh if (ph.code != 0) {
345 1.1 msaitoh (*pr)("Unknown PPPoE code.\n");
346 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
347 1.1 msaitoh }
348 1.1 msaitoh
349 1.1 msaitoh return m_examine_ppp(m, off, modif, pr);
350 1.1 msaitoh }
351 1.1 msaitoh
352 1.6 msaitoh void
353 1.1 msaitoh m_examine_ppp(const struct mbuf *m, int off, const char *modif,
354 1.1 msaitoh void (*pr)(const char *, ...))
355 1.1 msaitoh {
356 1.1 msaitoh struct ppp_header h;
357 1.1 msaitoh unsigned int pktlen;
358 1.1 msaitoh uint16_t protocol;
359 1.1 msaitoh
360 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
361 1.1 msaitoh if (pktlen < sizeof(h)) {
362 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
363 1.6 msaitoh pktlen, sizeof(h));
364 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
365 1.1 msaitoh }
366 1.1 msaitoh
367 1.1 msaitoh if (m_peek_data(m, off, sizeof(h), (void *)(&h)) < 0) {
368 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
369 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
370 1.1 msaitoh }
371 1.1 msaitoh off += sizeof(h);
372 1.1 msaitoh
373 1.1 msaitoh protocol = ntohs(h.protocol);
374 1.1 msaitoh
375 1.1 msaitoh (*pr)("SPPP: Address = %d(", h.address);
376 1.1 msaitoh switch (h.address) {
377 1.1 msaitoh case PPP_ALLSTATIONS:
378 1.1 msaitoh (*pr)("ALLSTATIONS)\n");
379 1.1 msaitoh (*pr)("SPPP: Protocol = %d(", protocol);
380 1.1 msaitoh switch (protocol) {
381 1.1 msaitoh case PPP_LCP:
382 1.1 msaitoh (*pr)("LCP)\n");
383 1.1 msaitoh break;
384 1.1 msaitoh case PPP_PAP:
385 1.1 msaitoh (*pr)("PAP)\n");
386 1.1 msaitoh break;
387 1.1 msaitoh case PPP_CHAP:
388 1.1 msaitoh (*pr)("CHAP)\n");
389 1.1 msaitoh break;
390 1.1 msaitoh case PPP_IPCP:
391 1.1 msaitoh (*pr)("IPCP)\n");
392 1.1 msaitoh break;
393 1.1 msaitoh case PPP_IPV6CP:
394 1.1 msaitoh (*pr)("IPV6CP)\n");
395 1.1 msaitoh break;
396 1.1 msaitoh case PPP_IP:
397 1.1 msaitoh (*pr)("IP)\n");
398 1.1 msaitoh return m_examine_ip(m, off, modif, pr);
399 1.1 msaitoh break;
400 1.1 msaitoh case PPP_IPV6:
401 1.1 msaitoh (*pr)("IPv6)\n");
402 1.1 msaitoh return m_examine_ip6(m, off, modif, pr);
403 1.1 msaitoh break;
404 1.1 msaitoh default:
405 1.1 msaitoh (*pr)("unknown)\n");
406 1.1 msaitoh break;
407 1.1 msaitoh }
408 1.1 msaitoh break;
409 1.1 msaitoh case CISCO_MULTICAST:
410 1.1 msaitoh case CISCO_UNICAST:
411 1.7 msaitoh if (h.address == CISCO_MULTICAST)
412 1.1 msaitoh (*pr)("MULTICAST)\n");
413 1.7 msaitoh else
414 1.1 msaitoh (*pr)("UNICAST)\n");
415 1.7 msaitoh
416 1.1 msaitoh (*pr)("SPPP: Protocol = %d(", protocol);
417 1.1 msaitoh switch (protocol) {
418 1.1 msaitoh case CISCO_KEEPALIVE:
419 1.1 msaitoh (*pr)("Keepalive)\n");
420 1.1 msaitoh break;
421 1.1 msaitoh case ETHERTYPE_IP:
422 1.1 msaitoh (*pr)("IP)\n");
423 1.1 msaitoh return m_examine_ip(m, off, modif, pr);
424 1.1 msaitoh break;
425 1.1 msaitoh case ETHERTYPE_IPV6:
426 1.1 msaitoh (*pr)("IPv6)\n");
427 1.1 msaitoh return m_examine_ip6(m, off, modif, pr);
428 1.1 msaitoh break;
429 1.1 msaitoh default:
430 1.1 msaitoh (*pr)("unknown)\n");
431 1.1 msaitoh break;
432 1.1 msaitoh }
433 1.1 msaitoh break;
434 1.1 msaitoh default:
435 1.1 msaitoh (*pr)("unknown)\n", h.address);
436 1.1 msaitoh break;
437 1.1 msaitoh }
438 1.1 msaitoh
439 1.1 msaitoh (*pr)("No parser for address %d, protocol %d\n", h.address, protocol);
440 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
441 1.1 msaitoh }
442 1.1 msaitoh
443 1.6 msaitoh void
444 1.1 msaitoh m_examine_arp(const struct mbuf *m, int off, const char *modif,
445 1.1 msaitoh void (*pr)(const char *, ...))
446 1.1 msaitoh {
447 1.1 msaitoh unsigned int pktlen;
448 1.1 msaitoh struct arphdr ar;
449 1.1 msaitoh uint16_t hrd, op;
450 1.1 msaitoh struct in_addr isaddr, itaddr;
451 1.1 msaitoh uint8_t esaddr[ETHER_ADDR_LEN], etaddr[ETHER_ADDR_LEN];
452 1.1 msaitoh
453 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
454 1.1 msaitoh if (pktlen < sizeof(ar)) {
455 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
456 1.6 msaitoh pktlen, sizeof(ar));
457 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
458 1.1 msaitoh }
459 1.1 msaitoh
460 1.1 msaitoh if (m_peek_data(m, off, sizeof(ar), (void *)(&ar)) < 0) {
461 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
462 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
463 1.1 msaitoh }
464 1.1 msaitoh off += sizeof(ar);
465 1.1 msaitoh
466 1.1 msaitoh hrd = ntohs(ar.ar_hrd);
467 1.1 msaitoh (*pr)("ARP: AddressType = %u(", hrd);
468 1.1 msaitoh switch (hrd) {
469 1.1 msaitoh case ARPHRD_ETHER:
470 1.1 msaitoh (*pr)("ETHER)\n");
471 1.1 msaitoh break;
472 1.1 msaitoh case ARPHRD_IEEE802:
473 1.1 msaitoh (*pr)("IEEE802)\n");
474 1.1 msaitoh break;
475 1.1 msaitoh default:
476 1.1 msaitoh (*pr)("unknown)\n");
477 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
478 1.1 msaitoh break;
479 1.1 msaitoh }
480 1.1 msaitoh (*pr)("ARP: Protocol Address Format = %u\n", ntohs(ar.ar_pro));
481 1.1 msaitoh (*pr)("ARP: Protocol Address Length = %u\n", ar.ar_pln);
482 1.1 msaitoh (*pr)("ARP: H/W Address Length = %u\n", ar.ar_hln);
483 1.1 msaitoh op = ntohs(ar.ar_op);
484 1.1 msaitoh (*pr)("ARP: Operation = %u(", op);
485 1.1 msaitoh switch (op) {
486 1.1 msaitoh case ARPOP_REQUEST:
487 1.1 msaitoh (*pr)("REQUEST)\n");
488 1.1 msaitoh break;
489 1.1 msaitoh case ARPOP_REPLY:
490 1.1 msaitoh (*pr)("REPLY)\n");
491 1.1 msaitoh break;
492 1.1 msaitoh case ARPOP_REVREQUEST:
493 1.1 msaitoh (*pr)("REVREQUEST)\n");
494 1.1 msaitoh break;
495 1.1 msaitoh case ARPOP_REVREPLY:
496 1.1 msaitoh (*pr)("REVREPLY)\n");
497 1.1 msaitoh break;
498 1.1 msaitoh case ARPOP_INVREQUEST:
499 1.1 msaitoh (*pr)("INVREQUEST)\n");
500 1.1 msaitoh break;
501 1.1 msaitoh case ARPOP_INVREPLY:
502 1.1 msaitoh (*pr)("INVREPLY)\n");
503 1.1 msaitoh break;
504 1.1 msaitoh }
505 1.1 msaitoh
506 1.1 msaitoh if (ar.ar_hln == 0 || ar.ar_pln == 0 ||
507 1.1 msaitoh ar.ar_hln != sizeof(esaddr) || ar.ar_pln != sizeof(isaddr)) {
508 1.1 msaitoh (*pr)("Cannot parse.\n");
509 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
510 1.1 msaitoh }
511 1.1 msaitoh
512 1.1 msaitoh if (m_peek_data(m, off, sizeof(esaddr), (void *)(esaddr)) < 0) {
513 1.1 msaitoh (*pr)("Cannot read payload\n");
514 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
515 1.1 msaitoh }
516 1.1 msaitoh off += sizeof(esaddr);
517 1.1 msaitoh (*pr)("ARP: Ether Src = %s\n", str_ethaddr(esaddr));
518 1.1 msaitoh
519 1.1 msaitoh if (m_peek_data(m, off, sizeof(isaddr), (void *)(&isaddr)) < 0) {
520 1.1 msaitoh (*pr)("Cannot read payload\n");
521 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
522 1.1 msaitoh }
523 1.1 msaitoh off += sizeof(isaddr);
524 1.1 msaitoh (*pr)("ARP: IP Src = %s\n", str_ipaddr(&isaddr));
525 1.1 msaitoh
526 1.1 msaitoh if (m_peek_data(m, off, sizeof(etaddr), (void *)(etaddr)) < 0) {
527 1.1 msaitoh (*pr)("Cannot read payload\n");
528 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
529 1.1 msaitoh }
530 1.1 msaitoh off += sizeof(etaddr);
531 1.1 msaitoh (*pr)("ARP: Ether Tgt = %s\n", str_ethaddr(etaddr));
532 1.1 msaitoh
533 1.1 msaitoh if (m_peek_data(m, off, sizeof(itaddr), (void *)(&itaddr)) < 0) {
534 1.1 msaitoh (*pr)("Cannot read payload\n");
535 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
536 1.1 msaitoh }
537 1.1 msaitoh off += sizeof(itaddr);
538 1.1 msaitoh (*pr)("ARP: IP Tgt = %s\n", str_ipaddr(&itaddr));
539 1.1 msaitoh
540 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
541 1.1 msaitoh }
542 1.1 msaitoh
543 1.6 msaitoh void
544 1.1 msaitoh m_examine_ip(const struct mbuf *m, int off, const char *modif,
545 1.1 msaitoh void (*pr)(const char *, ...))
546 1.1 msaitoh {
547 1.1 msaitoh unsigned int pktlen;
548 1.1 msaitoh struct ip ip;
549 1.1 msaitoh uint16_t offset;
550 1.1 msaitoh
551 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
552 1.1 msaitoh if (pktlen < sizeof(ip)) {
553 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
554 1.6 msaitoh pktlen, sizeof(ip));
555 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
556 1.1 msaitoh }
557 1.1 msaitoh
558 1.1 msaitoh if (m_peek_data(m, off, sizeof(ip), (void *)(&ip)) < 0) {
559 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
560 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
561 1.1 msaitoh }
562 1.1 msaitoh off += sizeof(ip);
563 1.1 msaitoh
564 1.1 msaitoh (*pr)("IP: Version = %u\n", ip.ip_v);
565 1.1 msaitoh (*pr)("IP: Header Length = %u\n", (ip.ip_hl << 2));
566 1.1 msaitoh (*pr)("IP: ToS = 0x%02x\n", ip.ip_tos);
567 1.1 msaitoh (*pr)("IP: Packet Length = %u\n", ntohs(ip.ip_len));
568 1.1 msaitoh (*pr)("IP: ID = %u\n", ntohs(ip.ip_id));
569 1.1 msaitoh offset = ntohs(ip.ip_off);
570 1.1 msaitoh (*pr)("IP: Offset = %u\n", (offset & IP_OFFMASK));
571 1.7 msaitoh if (offset & IP_RF)
572 1.1 msaitoh (*pr)("IP: Flag 0x%04x (reserved)\n", IP_RF);
573 1.7 msaitoh if (offset & IP_EF)
574 1.1 msaitoh (*pr)("IP: Flag 0x%04x (evil flag)\n", IP_EF);
575 1.7 msaitoh if (offset & IP_DF)
576 1.1 msaitoh (*pr)("IP: Flag 0x%04x (don't fragment)\n", IP_DF);
577 1.7 msaitoh if (offset & IP_MF)
578 1.1 msaitoh (*pr)("IP: Flag 0x%04x (more fragment)\n", IP_MF);
579 1.1 msaitoh (*pr)("IP: TTL = %u\n", ip.ip_ttl);
580 1.1 msaitoh (*pr)("IP: protocol = %u(%s)\n", ip.ip_p, str_ipproto(ip.ip_p));
581 1.6 msaitoh (*pr)("IP: checksum = 0x%04x\n", ntohs(ip.ip_sum));
582 1.1 msaitoh (*pr)("IP: Src = %s\n", str_ipaddr(&ip.ip_src));
583 1.1 msaitoh (*pr)("IP: Dst = %s\n", str_ipaddr(&ip.ip_dst));
584 1.1 msaitoh
585 1.1 msaitoh switch (ip.ip_p) {
586 1.1 msaitoh case IPPROTO_ICMP:
587 1.1 msaitoh return m_examine_icmp(m, off, modif, pr);
588 1.1 msaitoh break;
589 1.1 msaitoh case IPPROTO_TCP:
590 1.1 msaitoh return m_examine_tcp(m, off, modif, pr);
591 1.1 msaitoh break;
592 1.1 msaitoh case IPPROTO_UDP:
593 1.1 msaitoh return m_examine_udp(m, off, modif, pr);
594 1.1 msaitoh break;
595 1.1 msaitoh default:
596 1.1 msaitoh break;
597 1.1 msaitoh }
598 1.1 msaitoh
599 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
600 1.1 msaitoh }
601 1.1 msaitoh
602 1.6 msaitoh void
603 1.1 msaitoh m_examine_icmp(const struct mbuf *m, int off, const char *modif,
604 1.1 msaitoh void (*pr)(const char *, ...))
605 1.1 msaitoh {
606 1.1 msaitoh unsigned int pktlen;
607 1.1 msaitoh struct icmp icmphdr;
608 1.1 msaitoh
609 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
610 1.1 msaitoh if (pktlen < sizeof(icmphdr)) {
611 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
612 1.6 msaitoh pktlen, sizeof(icmphdr));
613 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
614 1.1 msaitoh }
615 1.1 msaitoh
616 1.1 msaitoh if (m_peek_data(m, off, sizeof(icmphdr), (void *)(&icmphdr)) < 0) {
617 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
618 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
619 1.1 msaitoh }
620 1.1 msaitoh off += sizeof(icmphdr);
621 1.1 msaitoh
622 1.1 msaitoh (*pr)("ICMP: Type = %u(", icmphdr.icmp_type);
623 1.1 msaitoh switch (icmphdr.icmp_type) {
624 1.1 msaitoh case ICMP_ECHOREPLY:
625 1.1 msaitoh (*pr)("Echo Reply)\n");
626 1.1 msaitoh break;
627 1.1 msaitoh case ICMP_UNREACH:
628 1.1 msaitoh (*pr)("Destination Unreachable)\n");
629 1.1 msaitoh break;
630 1.1 msaitoh case ICMP_SOURCEQUENCH:
631 1.1 msaitoh (*pr)("Source Quench)\n");
632 1.1 msaitoh break;
633 1.1 msaitoh case ICMP_REDIRECT:
634 1.1 msaitoh (*pr)("Redirect)\n");
635 1.1 msaitoh break;
636 1.1 msaitoh case ICMP_TIMXCEED:
637 1.1 msaitoh (*pr)("Time Exceeded)\n");
638 1.1 msaitoh break;
639 1.1 msaitoh default:
640 1.1 msaitoh (*pr)("unknown)\n");
641 1.1 msaitoh break;
642 1.1 msaitoh }
643 1.1 msaitoh (*pr)("ICMP: Code = %d\n", icmphdr.icmp_code);
644 1.1 msaitoh
645 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
646 1.1 msaitoh }
647 1.1 msaitoh
648 1.6 msaitoh void
649 1.1 msaitoh m_examine_ip6(const struct mbuf *m, int off, const char *modif,
650 1.1 msaitoh void (*pr)(const char *, ...))
651 1.1 msaitoh {
652 1.1 msaitoh unsigned int pktlen;
653 1.1 msaitoh struct ip6_hdr ip6;
654 1.1 msaitoh struct ip6_hbh hbh;
655 1.1 msaitoh int hbhlen;
656 1.1 msaitoh uint32_t flow;
657 1.1 msaitoh uint8_t vfc;
658 1.1 msaitoh uint8_t nxt;
659 1.1 msaitoh
660 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
661 1.1 msaitoh if (pktlen < sizeof(ip6)) {
662 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
663 1.6 msaitoh pktlen, sizeof(ip6));
664 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
665 1.1 msaitoh }
666 1.1 msaitoh
667 1.1 msaitoh if (m_peek_data(m, off, sizeof(ip6), (void *)(&ip6)) < 0) {
668 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
669 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
670 1.1 msaitoh }
671 1.1 msaitoh off += sizeof(ip6);
672 1.1 msaitoh
673 1.1 msaitoh vfc = ip6.ip6_vfc;
674 1.1 msaitoh (*pr)("IPv6: Version = %u\n", (vfc & IPV6_VERSION_MASK) >> 4);
675 1.1 msaitoh flow = ntohl(ip6.ip6_flow);
676 1.1 msaitoh (*pr)("IPv6: Flow INFO = 0x%07x\n", flow & IPV6_FLOWINFO_MASK);
677 1.5 msaitoh (*pr)("IPv6: Payload Length = %u\n", ntohs(ip6.ip6_plen));
678 1.1 msaitoh nxt = ip6.ip6_nxt;
679 1.1 msaitoh (*pr)("IPv6: Next Header = %u(%s)\n", nxt, str_ipproto(nxt));
680 1.1 msaitoh (*pr)("IPv6: Hop Limit = %u\n", ip6.ip6_hlim);
681 1.1 msaitoh (*pr)("IPv6: Src = %s\n", str_ip6addr(&ip6.ip6_src));
682 1.1 msaitoh (*pr)("IPv6: Dst = %s\n", str_ip6addr(&ip6.ip6_dst));
683 1.1 msaitoh
684 1.1 msaitoh /* Strip Hop-by-Hop options */
685 1.1 msaitoh if (nxt == IPPROTO_HOPOPTS) {
686 1.1 msaitoh if (m_peek_data(m, off, sizeof(hbh), (void *)(&hbh)) < 0) {
687 1.1 msaitoh (*pr)("Cannot read option\n");
688 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
689 1.1 msaitoh }
690 1.1 msaitoh hbhlen = (hbh.ip6h_len + 1) << 3;
691 1.1 msaitoh nxt = hbh.ip6h_nxt;
692 1.1 msaitoh off += hbhlen;
693 1.1 msaitoh
694 1.1 msaitoh (*pr)("IPv6: Stripped Hop-by-Hop\n");
695 1.1 msaitoh (*pr)("IPv6: Next Header = %u(%s)\n", nxt, str_ipproto(nxt));
696 1.1 msaitoh }
697 1.1 msaitoh
698 1.1 msaitoh switch (nxt) {
699 1.1 msaitoh case IPPROTO_IPV6_ICMP:
700 1.1 msaitoh return m_examine_icmp6(m, off, modif, pr);
701 1.1 msaitoh break;
702 1.1 msaitoh case IPPROTO_TCP:
703 1.1 msaitoh return m_examine_tcp(m, off, modif, pr);
704 1.1 msaitoh break;
705 1.1 msaitoh case IPPROTO_UDP:
706 1.1 msaitoh return m_examine_udp(m, off, modif, pr);
707 1.1 msaitoh break;
708 1.1 msaitoh default:
709 1.1 msaitoh break;
710 1.1 msaitoh }
711 1.1 msaitoh
712 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
713 1.1 msaitoh }
714 1.1 msaitoh
715 1.6 msaitoh void
716 1.1 msaitoh m_examine_icmp6(const struct mbuf *m, int off, const char *modif,
717 1.1 msaitoh void (*pr)(const char *, ...))
718 1.1 msaitoh {
719 1.1 msaitoh unsigned int pktlen;
720 1.1 msaitoh struct icmp6_hdr icmp6;
721 1.1 msaitoh
722 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
723 1.1 msaitoh if (pktlen < sizeof(icmp6)) {
724 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
725 1.6 msaitoh pktlen, sizeof(icmp6));
726 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
727 1.1 msaitoh }
728 1.1 msaitoh
729 1.1 msaitoh if (m_peek_data(m, off, sizeof(icmp6), (void *)(&icmp6)) < 0) {
730 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
731 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
732 1.1 msaitoh }
733 1.1 msaitoh off += sizeof(icmp6);
734 1.1 msaitoh
735 1.1 msaitoh (*pr)("ICMP6: Type = %u(", icmp6.icmp6_type);
736 1.1 msaitoh switch (icmp6.icmp6_type) {
737 1.1 msaitoh case ICMP6_DST_UNREACH:
738 1.1 msaitoh (*pr)("Destination Unreachable)\n");
739 1.1 msaitoh break;
740 1.1 msaitoh case ICMP6_PACKET_TOO_BIG:
741 1.1 msaitoh (*pr)("Packet Too Big)\n");
742 1.1 msaitoh break;
743 1.1 msaitoh case ICMP6_TIME_EXCEEDED:
744 1.1 msaitoh (*pr)("Time Exceeded)\n");
745 1.1 msaitoh break;
746 1.1 msaitoh case ICMP6_PARAM_PROB:
747 1.1 msaitoh (*pr)("Parameter Problem)\n");
748 1.1 msaitoh break;
749 1.1 msaitoh case ICMP6_ECHO_REQUEST:
750 1.1 msaitoh (*pr)("Echo Request)\n");
751 1.1 msaitoh break;
752 1.1 msaitoh case ICMP6_ECHO_REPLY:
753 1.1 msaitoh (*pr)("Echo Reply)\n");
754 1.1 msaitoh break;
755 1.1 msaitoh
756 1.1 msaitoh case MLD_LISTENER_QUERY:
757 1.1 msaitoh (*pr)("MLD Listener Query)\n");
758 1.1 msaitoh break;
759 1.1 msaitoh case MLD_LISTENER_REPORT:
760 1.1 msaitoh (*pr)("MLD Listener Report)\n");
761 1.1 msaitoh break;
762 1.1 msaitoh case MLD_LISTENER_DONE:
763 1.1 msaitoh (*pr)("MLD Listener Done)\n");
764 1.1 msaitoh break;
765 1.1 msaitoh
766 1.1 msaitoh case ND_ROUTER_SOLICIT:
767 1.1 msaitoh (*pr)("Router Solicitation)\n");
768 1.1 msaitoh break;
769 1.1 msaitoh case ND_ROUTER_ADVERT:
770 1.1 msaitoh (*pr)("Router Advertizement)\n");
771 1.1 msaitoh break;
772 1.1 msaitoh case ND_NEIGHBOR_SOLICIT:
773 1.1 msaitoh (*pr)("Neighbor Solicitation)\n");
774 1.1 msaitoh break;
775 1.1 msaitoh case ND_NEIGHBOR_ADVERT:
776 1.1 msaitoh (*pr)("Neighbor Advertizement)\n");
777 1.1 msaitoh break;
778 1.1 msaitoh case ND_REDIRECT:
779 1.1 msaitoh (*pr)("Redirect)\n");
780 1.1 msaitoh break;
781 1.1 msaitoh
782 1.1 msaitoh default:
783 1.1 msaitoh (*pr)("unknown)\n");
784 1.1 msaitoh break;
785 1.1 msaitoh }
786 1.1 msaitoh (*pr)("ICMP6: Code = %u\n", icmp6.icmp6_code);
787 1.1 msaitoh
788 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
789 1.1 msaitoh }
790 1.1 msaitoh
791 1.6 msaitoh void
792 1.1 msaitoh m_examine_tcp(const struct mbuf *m, int off, const char *modif,
793 1.1 msaitoh void (*pr)(const char *, ...))
794 1.1 msaitoh {
795 1.1 msaitoh unsigned int pktlen;
796 1.1 msaitoh struct tcphdr tcp;
797 1.1 msaitoh
798 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
799 1.1 msaitoh if (pktlen < sizeof(tcp)) {
800 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
801 1.6 msaitoh pktlen, sizeof(tcp));
802 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
803 1.1 msaitoh }
804 1.1 msaitoh
805 1.1 msaitoh if (m_peek_data(m, off, sizeof(tcp), (void *)(&tcp)) < 0) {
806 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
807 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
808 1.1 msaitoh }
809 1.1 msaitoh off += sizeof(tcp);
810 1.1 msaitoh
811 1.1 msaitoh (*pr)("TCP: Src = %u\n", ntohs(tcp.th_sport));
812 1.1 msaitoh (*pr)("TCP: Dst = %u\n", ntohs(tcp.th_dport));
813 1.1 msaitoh (*pr)("TCP: Seq. = %u\n", ntohl(tcp.th_seq));
814 1.1 msaitoh (*pr)("TCP: Ack. = %u\n", ntohl(tcp.th_ack));
815 1.4 msaitoh (*pr)("TCP: Header Length = %u\n", tcp.th_off << 2);
816 1.1 msaitoh if (tcp.th_flags) {
817 1.1 msaitoh (*pr)("TCP: Flags 0x%02x : ", tcp.th_flags);
818 1.1 msaitoh if (tcp.th_flags & TH_FIN)
819 1.1 msaitoh (*pr)("FIN ");
820 1.1 msaitoh if (tcp.th_flags & TH_SYN)
821 1.1 msaitoh (*pr)("SYN ");
822 1.1 msaitoh if (tcp.th_flags & TH_RST)
823 1.1 msaitoh (*pr)("RST ");
824 1.1 msaitoh if (tcp.th_flags & TH_PUSH)
825 1.1 msaitoh (*pr)("PUSH ");
826 1.1 msaitoh if (tcp.th_flags & TH_URG)
827 1.1 msaitoh (*pr)("URG ");
828 1.1 msaitoh if (tcp.th_flags & TH_ECE)
829 1.1 msaitoh (*pr)("ECE ");
830 1.1 msaitoh if (tcp.th_flags & TH_CWR)
831 1.1 msaitoh (*pr)("CWR ");
832 1.1 msaitoh (*pr)("\n");
833 1.1 msaitoh }
834 1.1 msaitoh (*pr)("TCP: Windows Size = %u\n", ntohs(tcp.th_win));
835 1.6 msaitoh (*pr)("TCP: checksum = 0x%04x\n", ntohs(tcp.th_sum));
836 1.1 msaitoh (*pr)("TCP: Urgent Pointer = %u\n", ntohs(tcp.th_urp));
837 1.1 msaitoh
838 1.6 msaitoh int len;
839 1.6 msaitoh len = (tcp.th_off << 2) - sizeof(struct tcphdr);
840 1.6 msaitoh if (len > 0) {
841 1.6 msaitoh uint8_t *bufp, *op, opt, optlen;
842 1.6 msaitoh
843 1.6 msaitoh bufp = malloc(len, M_TEMP, M_DONTWAIT);
844 1.6 msaitoh if ((bufp == NULL) || (m_peek_data(m, off, len, bufp) < 0)) {
845 1.6 msaitoh (*pr)("%s: cannot read TCP option\n", __func__);
846 1.6 msaitoh if (bufp != NULL)
847 1.6 msaitoh free(bufp, M_TEMP);
848 1.6 msaitoh return m_examine_hex(m, off, modif, pr);
849 1.6 msaitoh }
850 1.6 msaitoh off += len;
851 1.6 msaitoh op = bufp;
852 1.6 msaitoh
853 1.6 msaitoh while (len > 0) {
854 1.6 msaitoh opt = op[0];
855 1.6 msaitoh if (opt == TCPOPT_EOL)
856 1.6 msaitoh break;
857 1.6 msaitoh if (opt == TCPOPT_NOP) {
858 1.6 msaitoh (*pr)("TCP: OPTION: NOP\n");
859 1.6 msaitoh op++;
860 1.6 msaitoh len--;
861 1.6 msaitoh continue;
862 1.6 msaitoh }
863 1.6 msaitoh if (opt == TCPOPT_PAD) {
864 1.6 msaitoh (*pr)("TCP: OPTION: PAD\n");
865 1.6 msaitoh op++;
866 1.6 msaitoh len--;
867 1.6 msaitoh continue;
868 1.6 msaitoh }
869 1.6 msaitoh optlen = op[1];
870 1.6 msaitoh if (optlen == 0)
871 1.6 msaitoh break;
872 1.6 msaitoh
873 1.6 msaitoh if (opt == TCPOPT_MAXSEG && optlen == TCPOLEN_MAXSEG) {
874 1.6 msaitoh uint16_t mss;
875 1.6 msaitoh
876 1.6 msaitoh bcopy(op + 2, &mss, sizeof(mss));
877 1.6 msaitoh (*pr)("TCP: OPTION: MSS = %d\n",
878 1.6 msaitoh ntohs(mss));
879 1.6 msaitoh
880 1.6 msaitoh op += optlen;
881 1.6 msaitoh len -= optlen;
882 1.6 msaitoh continue;
883 1.6 msaitoh } else if (opt == TCPOPT_WINDOW
884 1.6 msaitoh && optlen == TCPOLEN_WINDOW) {
885 1.6 msaitoh (*pr)("TCP: OPTION: wscale = %d\n", op[2]);
886 1.6 msaitoh op += optlen;
887 1.6 msaitoh len -= optlen;
888 1.6 msaitoh continue;
889 1.6 msaitoh } else if (opt == TCPOPT_SACK_PERMITTED
890 1.6 msaitoh && optlen == TCPOLEN_SACK_PERMITTED) {
891 1.6 msaitoh (*pr)("TCP: OPTION: SACK OK\n");
892 1.6 msaitoh op += optlen;
893 1.6 msaitoh len -= optlen;
894 1.6 msaitoh continue;
895 1.6 msaitoh } else if (opt == TCPOPT_TIMESTAMP
896 1.6 msaitoh && optlen == TCPOLEN_TIMESTAMP) {
897 1.6 msaitoh uint32_t ts_val, ts_ecr;
898 1.6 msaitoh
899 1.6 msaitoh memcpy(&ts_val, op + 2, sizeof(ts_val));
900 1.6 msaitoh memcpy(&ts_ecr, op + 6, sizeof(ts_ecr));
901 1.6 msaitoh (*pr)("TCP: OPTION: TIMESTAMP = %u, "
902 1.6 msaitoh "ECR = %u\n",
903 1.6 msaitoh ntohl(ts_val), ntohl(ts_ecr));
904 1.6 msaitoh op += optlen;
905 1.6 msaitoh len -= optlen;
906 1.6 msaitoh continue;
907 1.6 msaitoh } else {
908 1.6 msaitoh (*pr)("TCP: OPTION: unknown (%d, len = %d)\n",
909 1.6 msaitoh opt, optlen);
910 1.6 msaitoh op += optlen;
911 1.6 msaitoh len -= optlen;
912 1.6 msaitoh continue;
913 1.6 msaitoh }
914 1.6 msaitoh }
915 1.6 msaitoh free(bufp, M_TEMP);
916 1.6 msaitoh }
917 1.6 msaitoh
918 1.6 msaitoh if (off < pktlen)
919 1.6 msaitoh m_examine_hex(m, off, modif, pr);
920 1.6 msaitoh
921 1.6 msaitoh return;
922 1.1 msaitoh }
923 1.1 msaitoh
924 1.6 msaitoh void
925 1.1 msaitoh m_examine_udp(const struct mbuf *m, int off, const char *modif,
926 1.1 msaitoh void (*pr)(const char *, ...))
927 1.1 msaitoh {
928 1.1 msaitoh unsigned int pktlen;
929 1.1 msaitoh struct udphdr udp;
930 1.1 msaitoh
931 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
932 1.1 msaitoh if (pktlen < sizeof(udp)) {
933 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
934 1.6 msaitoh pktlen, sizeof(udp));
935 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
936 1.1 msaitoh }
937 1.1 msaitoh
938 1.1 msaitoh if (m_peek_data(m, off, sizeof(udp), (void *)(&udp)) < 0) {
939 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
940 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
941 1.1 msaitoh }
942 1.1 msaitoh off += sizeof(udp);
943 1.1 msaitoh
944 1.1 msaitoh (*pr)("UDP: Src = %u\n", ntohs(udp.uh_sport));
945 1.1 msaitoh (*pr)("UDP: Dst = %u\n", ntohs(udp.uh_dport));
946 1.1 msaitoh (*pr)("UDP: Length = %u\n", ntohs(udp.uh_ulen));
947 1.1 msaitoh
948 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
949 1.1 msaitoh }
950 1.1 msaitoh
951 1.6 msaitoh void
952 1.1 msaitoh m_examine_hex(const struct mbuf *m, int off, const char *modif,
953 1.1 msaitoh void (*pr)(const char *, ...))
954 1.1 msaitoh {
955 1.1 msaitoh unsigned int pktlen;
956 1.1 msaitoh int newline = 0;
957 1.1 msaitoh uint8_t v;
958 1.1 msaitoh
959 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
960 1.1 msaitoh if (pktlen > EXAMINE_HEX_LIMIT)
961 1.1 msaitoh pktlen = EXAMINE_HEX_LIMIT;
962 1.1 msaitoh
963 1.1 msaitoh if (pktlen == 0)
964 1.1 msaitoh return;
965 1.1 msaitoh
966 1.1 msaitoh (*pr)("offset %04d: ", off);
967 1.1 msaitoh while (pktlen > 0) {
968 1.1 msaitoh if (m_peek_data(m, off, sizeof(v), (void *)(&v)) < 0)
969 1.1 msaitoh break;
970 1.1 msaitoh pktlen --;
971 1.1 msaitoh off++;
972 1.1 msaitoh newline++;
973 1.1 msaitoh
974 1.1 msaitoh (*pr)("%02x", v);
975 1.1 msaitoh if (pktlen == 0)
976 1.1 msaitoh break;
977 1.1 msaitoh
978 1.1 msaitoh if ((newline % EXAMINE_HEX_COL) == 0) {
979 1.1 msaitoh (*pr)("\n");
980 1.1 msaitoh (*pr)("offset %04d: ", off);
981 1.7 msaitoh } else
982 1.1 msaitoh (*pr)(" ");
983 1.1 msaitoh }
984 1.1 msaitoh (*pr)("\n");
985 1.1 msaitoh }
986 1.1 msaitoh
987 1.1 msaitoh void
988 1.1 msaitoh m_examine(const struct mbuf *m, int af, const char *modif,
989 1.1 msaitoh void (*pr)(const char *, ...))
990 1.1 msaitoh {
991 1.1 msaitoh if (m == NULL)
992 1.1 msaitoh return;
993 1.1 msaitoh
994 1.1 msaitoh if (pr == NULL)
995 1.1 msaitoh return;
996 1.1 msaitoh
997 1.1 msaitoh switch (af) {
998 1.1 msaitoh case AF_UNSPEC:
999 1.1 msaitoh return m_examine_hex(m, 0, modif, pr);
1000 1.1 msaitoh break;
1001 1.1 msaitoh case AF_ETHER:
1002 1.1 msaitoh return m_examine_ether(m, 0, modif, pr);
1003 1.1 msaitoh break;
1004 1.1 msaitoh case AF_ARP:
1005 1.1 msaitoh return m_examine_arp(m, 0, modif, pr);
1006 1.1 msaitoh break;
1007 1.1 msaitoh case AF_INET:
1008 1.1 msaitoh return m_examine_ip(m, 0, modif, pr);
1009 1.1 msaitoh break;
1010 1.1 msaitoh case AF_INET6:
1011 1.1 msaitoh return m_examine_ip6(m, 0, modif, pr);
1012 1.1 msaitoh break;
1013 1.1 msaitoh default:
1014 1.1 msaitoh (*pr)("No parser for AF %d\n", af);
1015 1.1 msaitoh return m_examine_hex(m, 0, modif, pr);
1016 1.1 msaitoh break;
1017 1.1 msaitoh }
1018 1.1 msaitoh
1019 1.1 msaitoh /* not reached */
1020 1.1 msaitoh return;
1021 1.1 msaitoh }
1022