uipc_mbufdebug.c revision 1.6 1 1.6 msaitoh /* $NetBSD: uipc_mbufdebug.c,v 1.6 2018/10/12 05:49:38 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.6 msaitoh __KERNEL_RCSID(0, "$NetBSD: uipc_mbufdebug.c,v 1.6 2018/10/12 05:49:38 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.6 msaitoh if (opt_c == true) {
148 1.1 msaitoh return m->m_len;
149 1.1 msaitoh }
150 1.1 msaitoh
151 1.1 msaitoh if ((m->m_flags & M_PKTHDR) != 0)
152 1.1 msaitoh return m->m_pkthdr.len;
153 1.1 msaitoh
154 1.1 msaitoh pktlen = 0;
155 1.1 msaitoh for (m0 = m; m0 != NULL; m0 = m0->m_next)
156 1.1 msaitoh pktlen += m0->m_len;
157 1.1 msaitoh
158 1.1 msaitoh return pktlen;
159 1.1 msaitoh }
160 1.1 msaitoh
161 1.1 msaitoh static char *
162 1.1 msaitoh str_ethaddr(const uint8_t *ap)
163 1.1 msaitoh {
164 1.1 msaitoh static char buf[3 * ETHER_ADDR_LEN];
165 1.1 msaitoh
166 1.1 msaitoh return ether_snprintf(buf, sizeof(buf), ap);
167 1.1 msaitoh }
168 1.1 msaitoh
169 1.1 msaitoh static char *
170 1.1 msaitoh str_ipaddr(const struct in_addr *ap)
171 1.1 msaitoh {
172 1.1 msaitoh static char buf[INET_ADDRSTRLEN];
173 1.1 msaitoh
174 1.1 msaitoh return IN_PRINT(buf, ap);
175 1.1 msaitoh }
176 1.1 msaitoh
177 1.1 msaitoh static char *
178 1.1 msaitoh str_ip6addr(const struct in6_addr *ap)
179 1.1 msaitoh {
180 1.1 msaitoh static char buf[INET6_ADDRSTRLEN];
181 1.1 msaitoh
182 1.1 msaitoh return IN6_PRINT(buf, ap);
183 1.1 msaitoh }
184 1.1 msaitoh
185 1.1 msaitoh static const char *
186 1.1 msaitoh str_ipproto(const uint8_t proto)
187 1.1 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.1 msaitoh if (h.address == CISCO_MULTICAST) {
412 1.1 msaitoh (*pr)("MULTICAST)\n");
413 1.1 msaitoh }
414 1.1 msaitoh else {
415 1.1 msaitoh (*pr)("UNICAST)\n");
416 1.1 msaitoh }
417 1.1 msaitoh (*pr)("SPPP: Protocol = %d(", protocol);
418 1.1 msaitoh switch (protocol) {
419 1.1 msaitoh case CISCO_KEEPALIVE:
420 1.1 msaitoh (*pr)("Keepalive)\n");
421 1.1 msaitoh break;
422 1.1 msaitoh case ETHERTYPE_IP:
423 1.1 msaitoh (*pr)("IP)\n");
424 1.1 msaitoh return m_examine_ip(m, off, modif, pr);
425 1.1 msaitoh break;
426 1.1 msaitoh case ETHERTYPE_IPV6:
427 1.1 msaitoh (*pr)("IPv6)\n");
428 1.1 msaitoh return m_examine_ip6(m, off, modif, pr);
429 1.1 msaitoh break;
430 1.1 msaitoh default:
431 1.1 msaitoh (*pr)("unknown)\n");
432 1.1 msaitoh break;
433 1.1 msaitoh }
434 1.1 msaitoh break;
435 1.1 msaitoh default:
436 1.1 msaitoh (*pr)("unknown)\n", h.address);
437 1.1 msaitoh break;
438 1.1 msaitoh }
439 1.1 msaitoh
440 1.1 msaitoh (*pr)("No parser for address %d, protocol %d\n", h.address, protocol);
441 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
442 1.1 msaitoh }
443 1.1 msaitoh
444 1.6 msaitoh void
445 1.1 msaitoh m_examine_arp(const struct mbuf *m, int off, const char *modif,
446 1.1 msaitoh void (*pr)(const char *, ...))
447 1.1 msaitoh {
448 1.1 msaitoh unsigned int pktlen;
449 1.1 msaitoh struct arphdr ar;
450 1.1 msaitoh uint16_t hrd, op;
451 1.1 msaitoh struct in_addr isaddr, itaddr;
452 1.1 msaitoh uint8_t esaddr[ETHER_ADDR_LEN], etaddr[ETHER_ADDR_LEN];
453 1.1 msaitoh
454 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
455 1.1 msaitoh if (pktlen < sizeof(ar)) {
456 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
457 1.6 msaitoh pktlen, sizeof(ar));
458 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
459 1.1 msaitoh }
460 1.1 msaitoh
461 1.1 msaitoh if (m_peek_data(m, off, sizeof(ar), (void *)(&ar)) < 0) {
462 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
463 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
464 1.1 msaitoh }
465 1.1 msaitoh off += sizeof(ar);
466 1.1 msaitoh
467 1.1 msaitoh hrd = ntohs(ar.ar_hrd);
468 1.1 msaitoh (*pr)("ARP: AddressType = %u(", hrd);
469 1.1 msaitoh switch (hrd) {
470 1.1 msaitoh case ARPHRD_ETHER:
471 1.1 msaitoh (*pr)("ETHER)\n");
472 1.1 msaitoh break;
473 1.1 msaitoh case ARPHRD_IEEE802:
474 1.1 msaitoh (*pr)("IEEE802)\n");
475 1.1 msaitoh break;
476 1.1 msaitoh default:
477 1.1 msaitoh (*pr)("unknown)\n");
478 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
479 1.1 msaitoh break;
480 1.1 msaitoh }
481 1.1 msaitoh (*pr)("ARP: Protocol Address Format = %u\n", ntohs(ar.ar_pro));
482 1.1 msaitoh (*pr)("ARP: Protocol Address Length = %u\n", ar.ar_pln);
483 1.1 msaitoh (*pr)("ARP: H/W Address Length = %u\n", ar.ar_hln);
484 1.1 msaitoh op = ntohs(ar.ar_op);
485 1.1 msaitoh (*pr)("ARP: Operation = %u(", op);
486 1.1 msaitoh switch (op) {
487 1.1 msaitoh case ARPOP_REQUEST:
488 1.1 msaitoh (*pr)("REQUEST)\n");
489 1.1 msaitoh break;
490 1.1 msaitoh case ARPOP_REPLY:
491 1.1 msaitoh (*pr)("REPLY)\n");
492 1.1 msaitoh break;
493 1.1 msaitoh case ARPOP_REVREQUEST:
494 1.1 msaitoh (*pr)("REVREQUEST)\n");
495 1.1 msaitoh break;
496 1.1 msaitoh case ARPOP_REVREPLY:
497 1.1 msaitoh (*pr)("REVREPLY)\n");
498 1.1 msaitoh break;
499 1.1 msaitoh case ARPOP_INVREQUEST:
500 1.1 msaitoh (*pr)("INVREQUEST)\n");
501 1.1 msaitoh break;
502 1.1 msaitoh case ARPOP_INVREPLY:
503 1.1 msaitoh (*pr)("INVREPLY)\n");
504 1.1 msaitoh break;
505 1.1 msaitoh }
506 1.1 msaitoh
507 1.1 msaitoh if (ar.ar_hln == 0 || ar.ar_pln == 0 ||
508 1.1 msaitoh ar.ar_hln != sizeof(esaddr) || ar.ar_pln != sizeof(isaddr)) {
509 1.1 msaitoh (*pr)("Cannot parse.\n");
510 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
511 1.1 msaitoh }
512 1.1 msaitoh
513 1.1 msaitoh if (m_peek_data(m, off, sizeof(esaddr), (void *)(esaddr)) < 0) {
514 1.1 msaitoh (*pr)("Cannot read payload\n");
515 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
516 1.1 msaitoh }
517 1.1 msaitoh off += sizeof(esaddr);
518 1.1 msaitoh (*pr)("ARP: Ether Src = %s\n", str_ethaddr(esaddr));
519 1.1 msaitoh
520 1.1 msaitoh if (m_peek_data(m, off, sizeof(isaddr), (void *)(&isaddr)) < 0) {
521 1.1 msaitoh (*pr)("Cannot read payload\n");
522 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
523 1.1 msaitoh }
524 1.1 msaitoh off += sizeof(isaddr);
525 1.1 msaitoh (*pr)("ARP: IP Src = %s\n", str_ipaddr(&isaddr));
526 1.1 msaitoh
527 1.1 msaitoh if (m_peek_data(m, off, sizeof(etaddr), (void *)(etaddr)) < 0) {
528 1.1 msaitoh (*pr)("Cannot read payload\n");
529 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
530 1.1 msaitoh }
531 1.1 msaitoh off += sizeof(etaddr);
532 1.1 msaitoh (*pr)("ARP: Ether Tgt = %s\n", str_ethaddr(etaddr));
533 1.1 msaitoh
534 1.1 msaitoh if (m_peek_data(m, off, sizeof(itaddr), (void *)(&itaddr)) < 0) {
535 1.1 msaitoh (*pr)("Cannot read payload\n");
536 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
537 1.1 msaitoh }
538 1.1 msaitoh off += sizeof(itaddr);
539 1.1 msaitoh (*pr)("ARP: IP Tgt = %s\n", str_ipaddr(&itaddr));
540 1.1 msaitoh
541 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
542 1.1 msaitoh }
543 1.1 msaitoh
544 1.6 msaitoh void
545 1.1 msaitoh m_examine_ip(const struct mbuf *m, int off, const char *modif,
546 1.1 msaitoh void (*pr)(const char *, ...))
547 1.1 msaitoh {
548 1.1 msaitoh unsigned int pktlen;
549 1.1 msaitoh struct ip ip;
550 1.1 msaitoh uint16_t offset;
551 1.1 msaitoh
552 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
553 1.1 msaitoh if (pktlen < sizeof(ip)) {
554 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
555 1.6 msaitoh pktlen, sizeof(ip));
556 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
557 1.1 msaitoh }
558 1.1 msaitoh
559 1.1 msaitoh if (m_peek_data(m, off, sizeof(ip), (void *)(&ip)) < 0) {
560 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
561 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
562 1.1 msaitoh }
563 1.1 msaitoh off += sizeof(ip);
564 1.1 msaitoh
565 1.1 msaitoh (*pr)("IP: Version = %u\n", ip.ip_v);
566 1.1 msaitoh (*pr)("IP: Header Length = %u\n", (ip.ip_hl << 2));
567 1.1 msaitoh (*pr)("IP: ToS = 0x%02x\n", ip.ip_tos);
568 1.1 msaitoh (*pr)("IP: Packet Length = %u\n", ntohs(ip.ip_len));
569 1.1 msaitoh (*pr)("IP: ID = %u\n", ntohs(ip.ip_id));
570 1.1 msaitoh offset = ntohs(ip.ip_off);
571 1.1 msaitoh (*pr)("IP: Offset = %u\n", (offset & IP_OFFMASK));
572 1.1 msaitoh if (offset & IP_RF) {
573 1.1 msaitoh (*pr)("IP: Flag 0x%04x (reserved)\n", IP_RF);
574 1.1 msaitoh }
575 1.1 msaitoh if (offset & IP_EF) {
576 1.1 msaitoh (*pr)("IP: Flag 0x%04x (evil flag)\n", IP_EF);
577 1.1 msaitoh }
578 1.1 msaitoh if (offset & IP_DF) {
579 1.1 msaitoh (*pr)("IP: Flag 0x%04x (don't fragment)\n", IP_DF);
580 1.1 msaitoh }
581 1.1 msaitoh if (offset & IP_MF) {
582 1.1 msaitoh (*pr)("IP: Flag 0x%04x (more fragment)\n", IP_MF);
583 1.1 msaitoh }
584 1.1 msaitoh (*pr)("IP: TTL = %u\n", ip.ip_ttl);
585 1.1 msaitoh (*pr)("IP: protocol = %u(%s)\n", ip.ip_p, str_ipproto(ip.ip_p));
586 1.6 msaitoh (*pr)("IP: checksum = 0x%04x\n", ntohs(ip.ip_sum));
587 1.1 msaitoh (*pr)("IP: Src = %s\n", str_ipaddr(&ip.ip_src));
588 1.1 msaitoh (*pr)("IP: Dst = %s\n", str_ipaddr(&ip.ip_dst));
589 1.1 msaitoh
590 1.1 msaitoh
591 1.1 msaitoh switch (ip.ip_p) {
592 1.1 msaitoh case IPPROTO_ICMP:
593 1.1 msaitoh return m_examine_icmp(m, off, modif, pr);
594 1.1 msaitoh break;
595 1.1 msaitoh case IPPROTO_TCP:
596 1.1 msaitoh return m_examine_tcp(m, off, modif, pr);
597 1.1 msaitoh break;
598 1.1 msaitoh case IPPROTO_UDP:
599 1.1 msaitoh return m_examine_udp(m, off, modif, pr);
600 1.1 msaitoh break;
601 1.1 msaitoh default:
602 1.1 msaitoh break;
603 1.1 msaitoh }
604 1.1 msaitoh
605 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
606 1.1 msaitoh }
607 1.1 msaitoh
608 1.6 msaitoh void
609 1.1 msaitoh m_examine_icmp(const struct mbuf *m, int off, const char *modif,
610 1.1 msaitoh void (*pr)(const char *, ...))
611 1.1 msaitoh {
612 1.1 msaitoh unsigned int pktlen;
613 1.1 msaitoh struct icmp icmphdr;
614 1.1 msaitoh
615 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
616 1.1 msaitoh if (pktlen < sizeof(icmphdr)) {
617 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
618 1.6 msaitoh pktlen, sizeof(icmphdr));
619 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
620 1.1 msaitoh }
621 1.1 msaitoh
622 1.1 msaitoh if (m_peek_data(m, off, sizeof(icmphdr), (void *)(&icmphdr)) < 0) {
623 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
624 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
625 1.1 msaitoh }
626 1.1 msaitoh off += sizeof(icmphdr);
627 1.1 msaitoh
628 1.1 msaitoh (*pr)("ICMP: Type = %u(", icmphdr.icmp_type);
629 1.1 msaitoh switch (icmphdr.icmp_type) {
630 1.1 msaitoh case ICMP_ECHOREPLY:
631 1.1 msaitoh (*pr)("Echo Reply)\n");
632 1.1 msaitoh break;
633 1.1 msaitoh case ICMP_UNREACH:
634 1.1 msaitoh (*pr)("Destination Unreachable)\n");
635 1.1 msaitoh break;
636 1.1 msaitoh case ICMP_SOURCEQUENCH:
637 1.1 msaitoh (*pr)("Source Quench)\n");
638 1.1 msaitoh break;
639 1.1 msaitoh case ICMP_REDIRECT:
640 1.1 msaitoh (*pr)("Redirect)\n");
641 1.1 msaitoh break;
642 1.1 msaitoh case ICMP_TIMXCEED:
643 1.1 msaitoh (*pr)("Time Exceeded)\n");
644 1.1 msaitoh break;
645 1.1 msaitoh default:
646 1.1 msaitoh (*pr)("unknown)\n");
647 1.1 msaitoh break;
648 1.1 msaitoh }
649 1.1 msaitoh (*pr)("ICMP: Code = %d\n", icmphdr.icmp_code);
650 1.1 msaitoh
651 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
652 1.1 msaitoh }
653 1.1 msaitoh
654 1.6 msaitoh void
655 1.1 msaitoh m_examine_ip6(const struct mbuf *m, int off, const char *modif,
656 1.1 msaitoh void (*pr)(const char *, ...))
657 1.1 msaitoh {
658 1.1 msaitoh unsigned int pktlen;
659 1.1 msaitoh struct ip6_hdr ip6;
660 1.1 msaitoh struct ip6_hbh hbh;
661 1.1 msaitoh int hbhlen;
662 1.1 msaitoh uint32_t flow;
663 1.1 msaitoh uint8_t vfc;
664 1.1 msaitoh uint8_t nxt;
665 1.1 msaitoh
666 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
667 1.1 msaitoh if (pktlen < sizeof(ip6)) {
668 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
669 1.6 msaitoh pktlen, sizeof(ip6));
670 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
671 1.1 msaitoh }
672 1.1 msaitoh
673 1.1 msaitoh if (m_peek_data(m, off, sizeof(ip6), (void *)(&ip6)) < 0) {
674 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
675 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
676 1.1 msaitoh }
677 1.1 msaitoh off += sizeof(ip6);
678 1.1 msaitoh
679 1.1 msaitoh vfc = ip6.ip6_vfc;
680 1.1 msaitoh (*pr)("IPv6: Version = %u\n", (vfc & IPV6_VERSION_MASK) >> 4);
681 1.1 msaitoh flow = ntohl(ip6.ip6_flow);
682 1.1 msaitoh (*pr)("IPv6: Flow INFO = 0x%07x\n", flow & IPV6_FLOWINFO_MASK);
683 1.5 msaitoh (*pr)("IPv6: Payload Length = %u\n", ntohs(ip6.ip6_plen));
684 1.1 msaitoh nxt = ip6.ip6_nxt;
685 1.1 msaitoh (*pr)("IPv6: Next Header = %u(%s)\n", nxt, str_ipproto(nxt));
686 1.1 msaitoh (*pr)("IPv6: Hop Limit = %u\n", ip6.ip6_hlim);
687 1.1 msaitoh (*pr)("IPv6: Src = %s\n", str_ip6addr(&ip6.ip6_src));
688 1.1 msaitoh (*pr)("IPv6: Dst = %s\n", str_ip6addr(&ip6.ip6_dst));
689 1.1 msaitoh
690 1.1 msaitoh /* Strip Hop-by-Hop options */
691 1.1 msaitoh if (nxt == IPPROTO_HOPOPTS) {
692 1.1 msaitoh if (m_peek_data(m, off, sizeof(hbh), (void *)(&hbh)) < 0) {
693 1.1 msaitoh (*pr)("Cannot read option\n");
694 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
695 1.1 msaitoh }
696 1.1 msaitoh hbhlen = (hbh.ip6h_len + 1) << 3;
697 1.1 msaitoh nxt = hbh.ip6h_nxt;
698 1.1 msaitoh off += hbhlen;
699 1.1 msaitoh
700 1.1 msaitoh (*pr)("IPv6: Stripped Hop-by-Hop\n");
701 1.1 msaitoh (*pr)("IPv6: Next Header = %u(%s)\n", nxt, str_ipproto(nxt));
702 1.1 msaitoh }
703 1.1 msaitoh
704 1.1 msaitoh switch (nxt) {
705 1.1 msaitoh case IPPROTO_IPV6_ICMP:
706 1.1 msaitoh return m_examine_icmp6(m, off, modif, pr);
707 1.1 msaitoh break;
708 1.1 msaitoh case IPPROTO_TCP:
709 1.1 msaitoh return m_examine_tcp(m, off, modif, pr);
710 1.1 msaitoh break;
711 1.1 msaitoh case IPPROTO_UDP:
712 1.1 msaitoh return m_examine_udp(m, off, modif, pr);
713 1.1 msaitoh break;
714 1.1 msaitoh default:
715 1.1 msaitoh break;
716 1.1 msaitoh }
717 1.1 msaitoh
718 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
719 1.1 msaitoh }
720 1.1 msaitoh
721 1.6 msaitoh void
722 1.1 msaitoh m_examine_icmp6(const struct mbuf *m, int off, const char *modif,
723 1.1 msaitoh void (*pr)(const char *, ...))
724 1.1 msaitoh {
725 1.1 msaitoh unsigned int pktlen;
726 1.1 msaitoh struct icmp6_hdr icmp6;
727 1.1 msaitoh
728 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
729 1.1 msaitoh if (pktlen < sizeof(icmp6)) {
730 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
731 1.6 msaitoh pktlen, sizeof(icmp6));
732 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
733 1.1 msaitoh }
734 1.1 msaitoh
735 1.1 msaitoh if (m_peek_data(m, off, sizeof(icmp6), (void *)(&icmp6)) < 0) {
736 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
737 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
738 1.1 msaitoh }
739 1.1 msaitoh off += sizeof(icmp6);
740 1.1 msaitoh
741 1.1 msaitoh (*pr)("ICMP6: Type = %u(", icmp6.icmp6_type);
742 1.1 msaitoh switch (icmp6.icmp6_type) {
743 1.1 msaitoh case ICMP6_DST_UNREACH:
744 1.1 msaitoh (*pr)("Destination Unreachable)\n");
745 1.1 msaitoh break;
746 1.1 msaitoh case ICMP6_PACKET_TOO_BIG:
747 1.1 msaitoh (*pr)("Packet Too Big)\n");
748 1.1 msaitoh break;
749 1.1 msaitoh case ICMP6_TIME_EXCEEDED:
750 1.1 msaitoh (*pr)("Time Exceeded)\n");
751 1.1 msaitoh break;
752 1.1 msaitoh case ICMP6_PARAM_PROB:
753 1.1 msaitoh (*pr)("Parameter Problem)\n");
754 1.1 msaitoh break;
755 1.1 msaitoh case ICMP6_ECHO_REQUEST:
756 1.1 msaitoh (*pr)("Echo Request)\n");
757 1.1 msaitoh break;
758 1.1 msaitoh case ICMP6_ECHO_REPLY:
759 1.1 msaitoh (*pr)("Echo Reply)\n");
760 1.1 msaitoh break;
761 1.1 msaitoh
762 1.1 msaitoh case MLD_LISTENER_QUERY:
763 1.1 msaitoh (*pr)("MLD Listener Query)\n");
764 1.1 msaitoh break;
765 1.1 msaitoh case MLD_LISTENER_REPORT:
766 1.1 msaitoh (*pr)("MLD Listener Report)\n");
767 1.1 msaitoh break;
768 1.1 msaitoh case MLD_LISTENER_DONE:
769 1.1 msaitoh (*pr)("MLD Listener Done)\n");
770 1.1 msaitoh break;
771 1.1 msaitoh
772 1.1 msaitoh case ND_ROUTER_SOLICIT:
773 1.1 msaitoh (*pr)("Router Solicitation)\n");
774 1.1 msaitoh break;
775 1.1 msaitoh case ND_ROUTER_ADVERT:
776 1.1 msaitoh (*pr)("Router Advertizement)\n");
777 1.1 msaitoh break;
778 1.1 msaitoh case ND_NEIGHBOR_SOLICIT:
779 1.1 msaitoh (*pr)("Neighbor Solicitation)\n");
780 1.1 msaitoh break;
781 1.1 msaitoh case ND_NEIGHBOR_ADVERT:
782 1.1 msaitoh (*pr)("Neighbor Advertizement)\n");
783 1.1 msaitoh break;
784 1.1 msaitoh case ND_REDIRECT:
785 1.1 msaitoh (*pr)("Redirect)\n");
786 1.1 msaitoh break;
787 1.1 msaitoh
788 1.1 msaitoh default:
789 1.1 msaitoh (*pr)("unknown)\n");
790 1.1 msaitoh break;
791 1.1 msaitoh }
792 1.1 msaitoh (*pr)("ICMP6: Code = %u\n", icmp6.icmp6_code);
793 1.1 msaitoh
794 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
795 1.1 msaitoh }
796 1.1 msaitoh
797 1.6 msaitoh void
798 1.1 msaitoh m_examine_tcp(const struct mbuf *m, int off, const char *modif,
799 1.1 msaitoh void (*pr)(const char *, ...))
800 1.1 msaitoh {
801 1.1 msaitoh unsigned int pktlen;
802 1.1 msaitoh struct tcphdr tcp;
803 1.1 msaitoh
804 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
805 1.1 msaitoh if (pktlen < sizeof(tcp)) {
806 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
807 1.6 msaitoh pktlen, sizeof(tcp));
808 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
809 1.1 msaitoh }
810 1.1 msaitoh
811 1.1 msaitoh if (m_peek_data(m, off, sizeof(tcp), (void *)(&tcp)) < 0) {
812 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
813 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
814 1.1 msaitoh }
815 1.1 msaitoh off += sizeof(tcp);
816 1.1 msaitoh
817 1.1 msaitoh (*pr)("TCP: Src = %u\n", ntohs(tcp.th_sport));
818 1.1 msaitoh (*pr)("TCP: Dst = %u\n", ntohs(tcp.th_dport));
819 1.1 msaitoh (*pr)("TCP: Seq. = %u\n", ntohl(tcp.th_seq));
820 1.1 msaitoh (*pr)("TCP: Ack. = %u\n", ntohl(tcp.th_ack));
821 1.4 msaitoh (*pr)("TCP: Header Length = %u\n", tcp.th_off << 2);
822 1.1 msaitoh if (tcp.th_flags) {
823 1.1 msaitoh (*pr)("TCP: Flags 0x%02x : ", tcp.th_flags);
824 1.1 msaitoh if (tcp.th_flags & TH_FIN)
825 1.1 msaitoh (*pr)("FIN ");
826 1.1 msaitoh if (tcp.th_flags & TH_SYN)
827 1.1 msaitoh (*pr)("SYN ");
828 1.1 msaitoh if (tcp.th_flags & TH_RST)
829 1.1 msaitoh (*pr)("RST ");
830 1.1 msaitoh if (tcp.th_flags & TH_PUSH)
831 1.1 msaitoh (*pr)("PUSH ");
832 1.1 msaitoh if (tcp.th_flags & TH_URG)
833 1.1 msaitoh (*pr)("URG ");
834 1.1 msaitoh if (tcp.th_flags & TH_ECE)
835 1.1 msaitoh (*pr)("ECE ");
836 1.1 msaitoh if (tcp.th_flags & TH_CWR)
837 1.1 msaitoh (*pr)("CWR ");
838 1.1 msaitoh (*pr)("\n");
839 1.1 msaitoh }
840 1.1 msaitoh (*pr)("TCP: Windows Size = %u\n", ntohs(tcp.th_win));
841 1.6 msaitoh (*pr)("TCP: checksum = 0x%04x\n", ntohs(tcp.th_sum));
842 1.1 msaitoh (*pr)("TCP: Urgent Pointer = %u\n", ntohs(tcp.th_urp));
843 1.1 msaitoh
844 1.6 msaitoh int len;
845 1.6 msaitoh len = (tcp.th_off << 2) - sizeof(struct tcphdr);
846 1.6 msaitoh if (len > 0) {
847 1.6 msaitoh uint8_t *bufp, *op, opt, optlen;
848 1.6 msaitoh
849 1.6 msaitoh bufp = malloc(len, M_TEMP, M_DONTWAIT);
850 1.6 msaitoh if ((bufp == NULL) || (m_peek_data(m, off, len, bufp) < 0)) {
851 1.6 msaitoh (*pr)("%s: cannot read TCP option\n", __func__);
852 1.6 msaitoh if (bufp != NULL)
853 1.6 msaitoh free(bufp, M_TEMP);
854 1.6 msaitoh return m_examine_hex(m, off, modif, pr);
855 1.6 msaitoh }
856 1.6 msaitoh off += len;
857 1.6 msaitoh op = bufp;
858 1.6 msaitoh
859 1.6 msaitoh while (len > 0) {
860 1.6 msaitoh opt = op[0];
861 1.6 msaitoh if (opt == TCPOPT_EOL)
862 1.6 msaitoh break;
863 1.6 msaitoh if (opt == TCPOPT_NOP) {
864 1.6 msaitoh (*pr)("TCP: OPTION: NOP\n");
865 1.6 msaitoh op++;
866 1.6 msaitoh len--;
867 1.6 msaitoh continue;
868 1.6 msaitoh }
869 1.6 msaitoh if (opt == TCPOPT_PAD) {
870 1.6 msaitoh (*pr)("TCP: OPTION: PAD\n");
871 1.6 msaitoh op++;
872 1.6 msaitoh len--;
873 1.6 msaitoh continue;
874 1.6 msaitoh }
875 1.6 msaitoh optlen = op[1];
876 1.6 msaitoh if (optlen == 0)
877 1.6 msaitoh break;
878 1.6 msaitoh
879 1.6 msaitoh if (opt == TCPOPT_MAXSEG && optlen == TCPOLEN_MAXSEG) {
880 1.6 msaitoh uint16_t mss;
881 1.6 msaitoh
882 1.6 msaitoh bcopy(op + 2, &mss, sizeof(mss));
883 1.6 msaitoh (*pr)("TCP: OPTION: MSS = %d\n",
884 1.6 msaitoh ntohs(mss));
885 1.6 msaitoh
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_WINDOW
890 1.6 msaitoh && optlen == TCPOLEN_WINDOW) {
891 1.6 msaitoh (*pr)("TCP: OPTION: wscale = %d\n", op[2]);
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_SACK_PERMITTED
896 1.6 msaitoh && optlen == TCPOLEN_SACK_PERMITTED) {
897 1.6 msaitoh (*pr)("TCP: OPTION: SACK OK\n");
898 1.6 msaitoh op += optlen;
899 1.6 msaitoh len -= optlen;
900 1.6 msaitoh continue;
901 1.6 msaitoh } else if (opt == TCPOPT_TIMESTAMP
902 1.6 msaitoh && optlen == TCPOLEN_TIMESTAMP) {
903 1.6 msaitoh uint32_t ts_val, ts_ecr;
904 1.6 msaitoh
905 1.6 msaitoh memcpy(&ts_val, op + 2, sizeof(ts_val));
906 1.6 msaitoh memcpy(&ts_ecr, op + 6, sizeof(ts_ecr));
907 1.6 msaitoh (*pr)("TCP: OPTION: TIMESTAMP = %u, "
908 1.6 msaitoh "ECR = %u\n",
909 1.6 msaitoh ntohl(ts_val), ntohl(ts_ecr));
910 1.6 msaitoh op += optlen;
911 1.6 msaitoh len -= optlen;
912 1.6 msaitoh continue;
913 1.6 msaitoh } else {
914 1.6 msaitoh (*pr)("TCP: OPTION: unknown (%d, len = %d)\n",
915 1.6 msaitoh opt, optlen);
916 1.6 msaitoh op += optlen;
917 1.6 msaitoh len -= optlen;
918 1.6 msaitoh continue;
919 1.6 msaitoh }
920 1.6 msaitoh }
921 1.6 msaitoh free(bufp, M_TEMP);
922 1.6 msaitoh }
923 1.6 msaitoh
924 1.6 msaitoh if (off < pktlen)
925 1.6 msaitoh m_examine_hex(m, off, modif, pr);
926 1.6 msaitoh
927 1.6 msaitoh return;
928 1.1 msaitoh }
929 1.1 msaitoh
930 1.6 msaitoh void
931 1.1 msaitoh m_examine_udp(const struct mbuf *m, int off, const char *modif,
932 1.1 msaitoh void (*pr)(const char *, ...))
933 1.1 msaitoh {
934 1.1 msaitoh unsigned int pktlen;
935 1.1 msaitoh struct udphdr udp;
936 1.1 msaitoh
937 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
938 1.1 msaitoh if (pktlen < sizeof(udp)) {
939 1.6 msaitoh (*pr)("%s: too short mbuf chain (%u < %u)\n", __func__,
940 1.6 msaitoh pktlen, sizeof(udp));
941 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
942 1.1 msaitoh }
943 1.1 msaitoh
944 1.1 msaitoh if (m_peek_data(m, off, sizeof(udp), (void *)(&udp)) < 0) {
945 1.1 msaitoh (*pr)("%s: cannot read header\n", __func__);
946 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
947 1.1 msaitoh }
948 1.1 msaitoh off += sizeof(udp);
949 1.1 msaitoh
950 1.1 msaitoh (*pr)("UDP: Src = %u\n", ntohs(udp.uh_sport));
951 1.1 msaitoh (*pr)("UDP: Dst = %u\n", ntohs(udp.uh_dport));
952 1.1 msaitoh (*pr)("UDP: Length = %u\n", ntohs(udp.uh_ulen));
953 1.1 msaitoh
954 1.1 msaitoh return m_examine_hex(m, off, modif, pr);
955 1.1 msaitoh }
956 1.1 msaitoh
957 1.6 msaitoh void
958 1.1 msaitoh m_examine_hex(const struct mbuf *m, int off, const char *modif,
959 1.1 msaitoh void (*pr)(const char *, ...))
960 1.1 msaitoh {
961 1.1 msaitoh unsigned int pktlen;
962 1.1 msaitoh int newline = 0;
963 1.1 msaitoh uint8_t v;
964 1.1 msaitoh
965 1.1 msaitoh pktlen = m_peek_len(m, modif) - off;
966 1.1 msaitoh if (pktlen > EXAMINE_HEX_LIMIT)
967 1.1 msaitoh pktlen = EXAMINE_HEX_LIMIT;
968 1.1 msaitoh
969 1.1 msaitoh if (pktlen == 0)
970 1.1 msaitoh return;
971 1.1 msaitoh
972 1.1 msaitoh (*pr)("offset %04d: ", off);
973 1.1 msaitoh while (pktlen > 0) {
974 1.1 msaitoh if (m_peek_data(m, off, sizeof(v), (void *)(&v)) < 0)
975 1.1 msaitoh break;
976 1.1 msaitoh pktlen --;
977 1.1 msaitoh off++;
978 1.1 msaitoh newline++;
979 1.1 msaitoh
980 1.1 msaitoh (*pr)("%02x", v);
981 1.1 msaitoh if (pktlen == 0)
982 1.1 msaitoh break;
983 1.1 msaitoh
984 1.1 msaitoh if ((newline % EXAMINE_HEX_COL) == 0) {
985 1.1 msaitoh (*pr)("\n");
986 1.1 msaitoh (*pr)("offset %04d: ", off);
987 1.1 msaitoh }
988 1.1 msaitoh else {
989 1.1 msaitoh (*pr)(" ");
990 1.1 msaitoh }
991 1.1 msaitoh }
992 1.1 msaitoh (*pr)("\n");
993 1.1 msaitoh }
994 1.1 msaitoh
995 1.1 msaitoh void
996 1.1 msaitoh m_examine(const struct mbuf *m, int af, const char *modif,
997 1.1 msaitoh void (*pr)(const char *, ...))
998 1.1 msaitoh {
999 1.1 msaitoh if (m == NULL)
1000 1.1 msaitoh return;
1001 1.1 msaitoh
1002 1.1 msaitoh if (pr == NULL)
1003 1.1 msaitoh return;
1004 1.1 msaitoh
1005 1.1 msaitoh switch (af) {
1006 1.1 msaitoh case AF_UNSPEC:
1007 1.1 msaitoh return m_examine_hex(m, 0, modif, pr);
1008 1.1 msaitoh break;
1009 1.1 msaitoh case AF_ETHER:
1010 1.1 msaitoh return m_examine_ether(m, 0, modif, pr);
1011 1.1 msaitoh break;
1012 1.1 msaitoh case AF_ARP:
1013 1.1 msaitoh return m_examine_arp(m, 0, modif, pr);
1014 1.1 msaitoh break;
1015 1.1 msaitoh case AF_INET:
1016 1.1 msaitoh return m_examine_ip(m, 0, modif, pr);
1017 1.1 msaitoh break;
1018 1.1 msaitoh case AF_INET6:
1019 1.1 msaitoh return m_examine_ip6(m, 0, modif, pr);
1020 1.1 msaitoh break;
1021 1.1 msaitoh default:
1022 1.1 msaitoh (*pr)("No parser for AF %d\n", af);
1023 1.1 msaitoh return m_examine_hex(m, 0, modif, pr);
1024 1.1 msaitoh break;
1025 1.1 msaitoh }
1026 1.1 msaitoh
1027 1.1 msaitoh /* not reached */
1028 1.1 msaitoh return;
1029 1.1 msaitoh }
1030