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