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