xref: /dflybsd-src/contrib/tcpdump/print-null.c (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
141c99275SPeter Avalos /*
241c99275SPeter Avalos  * Copyright (c) 1991, 1993, 1994, 1995, 1996, 1997
341c99275SPeter Avalos  *	The Regents of the University of California.  All rights reserved.
441c99275SPeter Avalos  *
541c99275SPeter Avalos  * Redistribution and use in source and binary forms, with or without
641c99275SPeter Avalos  * modification, are permitted provided that: (1) source code distributions
741c99275SPeter Avalos  * retain the above copyright notice and this paragraph in its entirety, (2)
841c99275SPeter Avalos  * distributions including binary code include the above copyright notice and
941c99275SPeter Avalos  * this paragraph in its entirety in the documentation or other materials
1041c99275SPeter Avalos  * provided with the distribution, and (3) all advertising materials mentioning
1141c99275SPeter Avalos  * features or use of this software display the following acknowledgement:
1241c99275SPeter Avalos  * ``This product includes software developed by the University of California,
1341c99275SPeter Avalos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1441c99275SPeter Avalos  * the University nor the names of its contributors may be used to endorse
1541c99275SPeter Avalos  * or promote products derived from this software without specific prior
1641c99275SPeter Avalos  * written permission.
1741c99275SPeter Avalos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1841c99275SPeter Avalos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1941c99275SPeter Avalos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2041c99275SPeter Avalos  */
2141c99275SPeter Avalos 
22411677aeSAaron LI /* \summary: BSD loopback device printer */
2341c99275SPeter Avalos 
2441c99275SPeter Avalos #ifdef HAVE_CONFIG_H
25*ed775ee7SAntonio Huete Jimenez #include <config.h>
2641c99275SPeter Avalos #endif
2741c99275SPeter Avalos 
28*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
2941c99275SPeter Avalos 
30*ed775ee7SAntonio Huete Jimenez #define ND_LONGJMP_FROM_TCHECK
31411677aeSAaron LI #include "netdissect.h"
32*ed775ee7SAntonio Huete Jimenez #include "extract.h"
33ea7b4bf5SPeter Avalos #include "af.h"
3441c99275SPeter Avalos 
35*ed775ee7SAntonio Huete Jimenez 
3641c99275SPeter Avalos /*
3741c99275SPeter Avalos  * The DLT_NULL packet header is 4 bytes long. It contains a host-byte-order
3841c99275SPeter Avalos  * 32-bit integer that specifies the family, e.g. AF_INET.
3941c99275SPeter Avalos  *
4041c99275SPeter Avalos  * Note here that "host" refers to the host on which the packets were
4141c99275SPeter Avalos  * captured; that isn't necessarily *this* host.
4241c99275SPeter Avalos  *
4341c99275SPeter Avalos  * The OpenBSD DLT_LOOP packet header is the same, except that the integer
4441c99275SPeter Avalos  * is in network byte order.
4541c99275SPeter Avalos  */
4641c99275SPeter Avalos #define	NULL_HDRLEN 4
4741c99275SPeter Avalos 
4841c99275SPeter Avalos /*
4941c99275SPeter Avalos  * Byte-swap a 32-bit number.
5041c99275SPeter Avalos  * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
5141c99275SPeter Avalos  * big-endian platforms.)
5241c99275SPeter Avalos  */
5341c99275SPeter Avalos #define	SWAPLONG(y) \
5441c99275SPeter Avalos ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
5541c99275SPeter Avalos 
56*ed775ee7SAntonio Huete Jimenez static void
null_hdr_print(netdissect_options * ndo,uint32_t family,u_int length)57*ed775ee7SAntonio Huete Jimenez null_hdr_print(netdissect_options *ndo, uint32_t family, u_int length)
5841c99275SPeter Avalos {
59411677aeSAaron LI 	if (!ndo->ndo_qflag) {
60*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("AF %s (%u)",
61*ed775ee7SAntonio Huete Jimenez 			tok2str(bsd_af_values,"Unknown",family),family);
6241c99275SPeter Avalos 	} else {
63*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%s",
64*ed775ee7SAntonio Huete Jimenez 			tok2str(bsd_af_values,"Unknown AF %u",family));
6541c99275SPeter Avalos 	}
6641c99275SPeter Avalos 
67*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", length %u: ", length);
6841c99275SPeter Avalos }
6941c99275SPeter Avalos 
7041c99275SPeter Avalos /*
7141c99275SPeter Avalos  * This is the top level routine of the printer.  'p' points
7241c99275SPeter Avalos  * to the ether header of the packet, 'h->ts' is the timestamp,
7341c99275SPeter Avalos  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
7441c99275SPeter Avalos  * is the number of bytes actually captured.
7541c99275SPeter Avalos  */
76*ed775ee7SAntonio Huete Jimenez void
null_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)77411677aeSAaron LI null_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
7841c99275SPeter Avalos {
7941c99275SPeter Avalos 	u_int length = h->len;
8041c99275SPeter Avalos 	u_int caplen = h->caplen;
81*ed775ee7SAntonio Huete Jimenez 	uint32_t family;
8241c99275SPeter Avalos 
83*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "null";
84*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_LEN(p, NULL_HDRLEN);
85*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_ll_hdr_len += NULL_HDRLEN;
8641c99275SPeter Avalos 
87*ed775ee7SAntonio Huete Jimenez 	family = GET_HE_U_4(p);
8841c99275SPeter Avalos 
8941c99275SPeter Avalos 	/*
9041c99275SPeter Avalos 	 * This isn't necessarily in our host byte order; if this is
9141c99275SPeter Avalos 	 * a DLT_LOOP capture, it's in network byte order, and if
9241c99275SPeter Avalos 	 * this is a DLT_NULL capture from a machine with the opposite
9341c99275SPeter Avalos 	 * byte-order, it's in the opposite byte order from ours.
9441c99275SPeter Avalos 	 *
9541c99275SPeter Avalos 	 * If the upper 16 bits aren't all zero, assume it's byte-swapped.
9641c99275SPeter Avalos 	 */
9741c99275SPeter Avalos 	if ((family & 0xFFFF0000) != 0)
9841c99275SPeter Avalos 		family = SWAPLONG(family);
9941c99275SPeter Avalos 
100411677aeSAaron LI 	if (ndo->ndo_eflag)
101411677aeSAaron LI 		null_hdr_print(ndo, family, length);
10241c99275SPeter Avalos 
10341c99275SPeter Avalos 	length -= NULL_HDRLEN;
10441c99275SPeter Avalos 	caplen -= NULL_HDRLEN;
10541c99275SPeter Avalos 	p += NULL_HDRLEN;
10641c99275SPeter Avalos 
10741c99275SPeter Avalos 	switch (family) {
10841c99275SPeter Avalos 
10941c99275SPeter Avalos 	case BSD_AFNUM_INET:
110411677aeSAaron LI 		ip_print(ndo, p, length);
11141c99275SPeter Avalos 		break;
11241c99275SPeter Avalos 
11341c99275SPeter Avalos 	case BSD_AFNUM_INET6_BSD:
11441c99275SPeter Avalos 	case BSD_AFNUM_INET6_FREEBSD:
11541c99275SPeter Avalos 	case BSD_AFNUM_INET6_DARWIN:
116411677aeSAaron LI 		ip6_print(ndo, p, length);
11741c99275SPeter Avalos 		break;
11841c99275SPeter Avalos 
11941c99275SPeter Avalos 	case BSD_AFNUM_ISO:
120411677aeSAaron LI 		isoclns_print(ndo, p, length);
12141c99275SPeter Avalos 		break;
12241c99275SPeter Avalos 
12341c99275SPeter Avalos 	case BSD_AFNUM_APPLETALK:
124411677aeSAaron LI 		atalk_print(ndo, p, length);
12541c99275SPeter Avalos 		break;
12641c99275SPeter Avalos 
12741c99275SPeter Avalos 	case BSD_AFNUM_IPX:
128411677aeSAaron LI 		ipx_print(ndo, p, length);
12941c99275SPeter Avalos 		break;
13041c99275SPeter Avalos 
13141c99275SPeter Avalos 	default:
13241c99275SPeter Avalos 		/* unknown AF_ value */
133411677aeSAaron LI 		if (!ndo->ndo_eflag)
134411677aeSAaron LI 			null_hdr_print(ndo, family, length + NULL_HDRLEN);
135411677aeSAaron LI 		if (!ndo->ndo_suppress_default_print)
136411677aeSAaron LI 			ND_DEFAULTPRINT(p, caplen);
13741c99275SPeter Avalos 	}
13841c99275SPeter Avalos }
139