xref: /freebsd/contrib/tcpdump/print-null.c (revision ee67461e)
14edb46e9SPaul Traina /*
2699fc314SBill Fenner  * Copyright (c) 1991, 1993, 1994, 1995, 1996, 1997
34edb46e9SPaul Traina  *	The Regents of the University of California.  All rights reserved.
44edb46e9SPaul Traina  *
54edb46e9SPaul Traina  * Redistribution and use in source and binary forms, with or without
64edb46e9SPaul Traina  * modification, are permitted provided that: (1) source code distributions
74edb46e9SPaul Traina  * retain the above copyright notice and this paragraph in its entirety, (2)
84edb46e9SPaul Traina  * distributions including binary code include the above copyright notice and
94edb46e9SPaul Traina  * this paragraph in its entirety in the documentation or other materials
104edb46e9SPaul Traina  * provided with the distribution, and (3) all advertising materials mentioning
114edb46e9SPaul Traina  * features or use of this software display the following acknowledgement:
124edb46e9SPaul Traina  * ``This product includes software developed by the University of California,
134edb46e9SPaul Traina  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
144edb46e9SPaul Traina  * the University nor the names of its contributors may be used to endorse
154edb46e9SPaul Traina  * or promote products derived from this software without specific prior
164edb46e9SPaul Traina  * written permission.
174edb46e9SPaul Traina  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
184edb46e9SPaul Traina  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
194edb46e9SPaul Traina  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
204edb46e9SPaul Traina  */
214edb46e9SPaul Traina 
223340d773SGleb Smirnoff /* \summary: BSD loopback device printer */
233340d773SGleb Smirnoff 
24a88113a8SBill Fenner #ifdef HAVE_CONFIG_H
25ee67461eSJoseph Mingrone #include <config.h>
264edb46e9SPaul Traina #endif
274edb46e9SPaul Traina 
28ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
294edb46e9SPaul Traina 
30ee67461eSJoseph Mingrone #define ND_LONGJMP_FROM_TCHECK
313340d773SGleb Smirnoff #include "netdissect.h"
32ee67461eSJoseph Mingrone #include "extract.h"
33a5779b6eSRui Paulo #include "af.h"
34943ee2b1SBill Fenner 
35ee67461eSJoseph Mingrone 
36699fc314SBill Fenner /*
37943ee2b1SBill Fenner  * The DLT_NULL packet header is 4 bytes long. It contains a host-byte-order
38943ee2b1SBill Fenner  * 32-bit integer that specifies the family, e.g. AF_INET.
39943ee2b1SBill Fenner  *
40943ee2b1SBill Fenner  * Note here that "host" refers to the host on which the packets were
41943ee2b1SBill Fenner  * captured; that isn't necessarily *this* host.
42943ee2b1SBill Fenner  *
43943ee2b1SBill Fenner  * The OpenBSD DLT_LOOP packet header is the same, except that the integer
44943ee2b1SBill Fenner  * is in network byte order.
45699fc314SBill Fenner  */
46699fc314SBill Fenner #define	NULL_HDRLEN 4
47699fc314SBill Fenner 
48943ee2b1SBill Fenner /*
49943ee2b1SBill Fenner  * Byte-swap a 32-bit number.
50943ee2b1SBill Fenner  * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
51943ee2b1SBill Fenner  * big-endian platforms.)
52943ee2b1SBill Fenner  */
53943ee2b1SBill Fenner #define	SWAPLONG(y) \
54943ee2b1SBill Fenner ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
554edb46e9SPaul Traina 
56ee67461eSJoseph Mingrone static void
null_hdr_print(netdissect_options * ndo,uint32_t family,u_int length)57ee67461eSJoseph Mingrone null_hdr_print(netdissect_options *ndo, uint32_t family, u_int length)
5829292c17SSam Leffler {
593c602fabSXin LI 	if (!ndo->ndo_qflag) {
60ee67461eSJoseph Mingrone 		ND_PRINT("AF %s (%u)",
61ee67461eSJoseph Mingrone 			tok2str(bsd_af_values,"Unknown",family),family);
6229292c17SSam Leffler 	} else {
63ee67461eSJoseph Mingrone 		ND_PRINT("%s",
64ee67461eSJoseph Mingrone 			tok2str(bsd_af_values,"Unknown AF %u",family));
6529292c17SSam Leffler 	}
6629292c17SSam Leffler 
67ee67461eSJoseph Mingrone 	ND_PRINT(", length %u: ", length);
6829292c17SSam Leffler }
6929292c17SSam Leffler 
70cc391cceSBruce M Simpson /*
71cc391cceSBruce M Simpson  * This is the top level routine of the printer.  'p' points
72cc391cceSBruce M Simpson  * to the ether header of the packet, 'h->ts' is the timestamp,
73c1ad1296SSam Leffler  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
74cc391cceSBruce M Simpson  * is the number of bytes actually captured.
75cc391cceSBruce M Simpson  */
76ee67461eSJoseph Mingrone void
null_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)773c602fabSXin LI null_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
784edb46e9SPaul Traina {
794edb46e9SPaul Traina 	u_int length = h->len;
804edb46e9SPaul Traina 	u_int caplen = h->caplen;
81ee67461eSJoseph Mingrone 	uint32_t family;
824edb46e9SPaul Traina 
83ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "null";
84ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(p, NULL_HDRLEN);
85ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len += NULL_HDRLEN;
864edb46e9SPaul Traina 
87ee67461eSJoseph Mingrone 	family = GET_HE_U_4(p);
88943ee2b1SBill Fenner 
89943ee2b1SBill Fenner 	/*
90943ee2b1SBill Fenner 	 * This isn't necessarily in our host byte order; if this is
91943ee2b1SBill Fenner 	 * a DLT_LOOP capture, it's in network byte order, and if
92943ee2b1SBill Fenner 	 * this is a DLT_NULL capture from a machine with the opposite
93943ee2b1SBill Fenner 	 * byte-order, it's in the opposite byte order from ours.
94943ee2b1SBill Fenner 	 *
95943ee2b1SBill Fenner 	 * If the upper 16 bits aren't all zero, assume it's byte-swapped.
96943ee2b1SBill Fenner 	 */
97943ee2b1SBill Fenner 	if ((family & 0xFFFF0000) != 0)
98943ee2b1SBill Fenner 		family = SWAPLONG(family);
99943ee2b1SBill Fenner 
1003c602fabSXin LI 	if (ndo->ndo_eflag)
1013c602fabSXin LI 		null_hdr_print(ndo, family, length);
10229292c17SSam Leffler 
1034edb46e9SPaul Traina 	length -= NULL_HDRLEN;
104cc391cceSBruce M Simpson 	caplen -= NULL_HDRLEN;
105cc391cceSBruce M Simpson 	p += NULL_HDRLEN;
1064edb46e9SPaul Traina 
107cc391cceSBruce M Simpson 	switch (family) {
108cc391cceSBruce M Simpson 
109abf25193SMax Laier 	case BSD_AFNUM_INET:
1103c602fabSXin LI 		ip_print(ndo, p, length);
111a88113a8SBill Fenner 		break;
112cc391cceSBruce M Simpson 
113abf25193SMax Laier 	case BSD_AFNUM_INET6_BSD:
114abf25193SMax Laier 	case BSD_AFNUM_INET6_FREEBSD:
115abf25193SMax Laier 	case BSD_AFNUM_INET6_DARWIN:
1163c602fabSXin LI 		ip6_print(ndo, p, length);
117a88113a8SBill Fenner 		break;
118cc391cceSBruce M Simpson 
119abf25193SMax Laier 	case BSD_AFNUM_ISO:
1200bff6a5aSEd Maste 		isoclns_print(ndo, p, length);
121cc391cceSBruce M Simpson 		break;
122cc391cceSBruce M Simpson 
123abf25193SMax Laier 	case BSD_AFNUM_APPLETALK:
1243c602fabSXin LI 		atalk_print(ndo, p, length);
125cc391cceSBruce M Simpson 		break;
126cc391cceSBruce M Simpson 
127abf25193SMax Laier 	case BSD_AFNUM_IPX:
1283c602fabSXin LI 		ipx_print(ndo, p, length);
129cc391cceSBruce M Simpson 		break;
130cc391cceSBruce M Simpson 
131a88113a8SBill Fenner 	default:
132cc391cceSBruce M Simpson 		/* unknown AF_ value */
1333c602fabSXin LI 		if (!ndo->ndo_eflag)
1343c602fabSXin LI 			null_hdr_print(ndo, family, length + NULL_HDRLEN);
1353c602fabSXin LI 		if (!ndo->ndo_suppress_default_print)
1363c602fabSXin LI 			ND_DEFAULTPRINT(p, caplen);
137a88113a8SBill Fenner 	}
1384edb46e9SPaul Traina }
139