xref: /freebsd/contrib/tcpdump/print-null.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
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 
24*ee67461eSJoseph Mingrone #include <config.h>
254edb46e9SPaul Traina 
26*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
274edb46e9SPaul Traina 
28*ee67461eSJoseph Mingrone #define ND_LONGJMP_FROM_TCHECK
293340d773SGleb Smirnoff #include "netdissect.h"
30*ee67461eSJoseph Mingrone #include "extract.h"
31a5779b6eSRui Paulo #include "af.h"
32943ee2b1SBill Fenner 
33*ee67461eSJoseph Mingrone 
34699fc314SBill Fenner /*
35943ee2b1SBill Fenner  * The DLT_NULL packet header is 4 bytes long. It contains a host-byte-order
36943ee2b1SBill Fenner  * 32-bit integer that specifies the family, e.g. AF_INET.
37943ee2b1SBill Fenner  *
38943ee2b1SBill Fenner  * Note here that "host" refers to the host on which the packets were
39943ee2b1SBill Fenner  * captured; that isn't necessarily *this* host.
40943ee2b1SBill Fenner  *
41943ee2b1SBill Fenner  * The OpenBSD DLT_LOOP packet header is the same, except that the integer
42943ee2b1SBill Fenner  * is in network byte order.
43699fc314SBill Fenner  */
44699fc314SBill Fenner #define	NULL_HDRLEN 4
45699fc314SBill Fenner 
46943ee2b1SBill Fenner /*
47943ee2b1SBill Fenner  * Byte-swap a 32-bit number.
48943ee2b1SBill Fenner  * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
49943ee2b1SBill Fenner  * big-endian platforms.)
50943ee2b1SBill Fenner  */
51943ee2b1SBill Fenner #define	SWAPLONG(y) \
52943ee2b1SBill Fenner ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
534edb46e9SPaul Traina 
54*ee67461eSJoseph Mingrone static void
null_hdr_print(netdissect_options * ndo,uint32_t family,u_int length)55*ee67461eSJoseph Mingrone null_hdr_print(netdissect_options *ndo, uint32_t family, u_int length)
5629292c17SSam Leffler {
573c602fabSXin LI 	if (!ndo->ndo_qflag) {
58*ee67461eSJoseph Mingrone 		ND_PRINT("AF %s (%u)",
59*ee67461eSJoseph Mingrone 			tok2str(bsd_af_values,"Unknown",family),family);
6029292c17SSam Leffler 	} else {
61*ee67461eSJoseph Mingrone 		ND_PRINT("%s",
62*ee67461eSJoseph Mingrone 			tok2str(bsd_af_values,"Unknown AF %u",family));
6329292c17SSam Leffler 	}
6429292c17SSam Leffler 
65*ee67461eSJoseph Mingrone 	ND_PRINT(", length %u: ", length);
6629292c17SSam Leffler }
6729292c17SSam Leffler 
68cc391cceSBruce M Simpson /*
69cc391cceSBruce M Simpson  * This is the top level routine of the printer.  'p' points
70cc391cceSBruce M Simpson  * to the ether header of the packet, 'h->ts' is the timestamp,
71c1ad1296SSam Leffler  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
72cc391cceSBruce M Simpson  * is the number of bytes actually captured.
73cc391cceSBruce M Simpson  */
74*ee67461eSJoseph Mingrone void
null_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)753c602fabSXin LI null_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
764edb46e9SPaul Traina {
774edb46e9SPaul Traina 	u_int length = h->len;
784edb46e9SPaul Traina 	u_int caplen = h->caplen;
79*ee67461eSJoseph Mingrone 	uint32_t family;
804edb46e9SPaul Traina 
81*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "null";
82*ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(p, NULL_HDRLEN);
83*ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len += NULL_HDRLEN;
844edb46e9SPaul Traina 
85*ee67461eSJoseph Mingrone 	family = GET_HE_U_4(p);
86943ee2b1SBill Fenner 
87943ee2b1SBill Fenner 	/*
88943ee2b1SBill Fenner 	 * This isn't necessarily in our host byte order; if this is
89943ee2b1SBill Fenner 	 * a DLT_LOOP capture, it's in network byte order, and if
90943ee2b1SBill Fenner 	 * this is a DLT_NULL capture from a machine with the opposite
91943ee2b1SBill Fenner 	 * byte-order, it's in the opposite byte order from ours.
92943ee2b1SBill Fenner 	 *
93943ee2b1SBill Fenner 	 * If the upper 16 bits aren't all zero, assume it's byte-swapped.
94943ee2b1SBill Fenner 	 */
95943ee2b1SBill Fenner 	if ((family & 0xFFFF0000) != 0)
96943ee2b1SBill Fenner 		family = SWAPLONG(family);
97943ee2b1SBill Fenner 
983c602fabSXin LI 	if (ndo->ndo_eflag)
993c602fabSXin LI 		null_hdr_print(ndo, family, length);
10029292c17SSam Leffler 
1014edb46e9SPaul Traina 	length -= NULL_HDRLEN;
102cc391cceSBruce M Simpson 	caplen -= NULL_HDRLEN;
103cc391cceSBruce M Simpson 	p += NULL_HDRLEN;
1044edb46e9SPaul Traina 
105cc391cceSBruce M Simpson 	switch (family) {
106cc391cceSBruce M Simpson 
107abf25193SMax Laier 	case BSD_AFNUM_INET:
1083c602fabSXin LI 		ip_print(ndo, p, length);
109a88113a8SBill Fenner 		break;
110cc391cceSBruce M Simpson 
111abf25193SMax Laier 	case BSD_AFNUM_INET6_BSD:
112abf25193SMax Laier 	case BSD_AFNUM_INET6_FREEBSD:
113abf25193SMax Laier 	case BSD_AFNUM_INET6_DARWIN:
1143c602fabSXin LI 		ip6_print(ndo, p, length);
115a88113a8SBill Fenner 		break;
116cc391cceSBruce M Simpson 
117abf25193SMax Laier 	case BSD_AFNUM_ISO:
1180bff6a5aSEd Maste 		isoclns_print(ndo, p, length);
119cc391cceSBruce M Simpson 		break;
120cc391cceSBruce M Simpson 
121abf25193SMax Laier 	case BSD_AFNUM_APPLETALK:
1223c602fabSXin LI 		atalk_print(ndo, p, length);
123cc391cceSBruce M Simpson 		break;
124cc391cceSBruce M Simpson 
125abf25193SMax Laier 	case BSD_AFNUM_IPX:
1263c602fabSXin LI 		ipx_print(ndo, p, length);
127cc391cceSBruce M Simpson 		break;
128cc391cceSBruce M Simpson 
129a88113a8SBill Fenner 	default:
130cc391cceSBruce M Simpson 		/* unknown AF_ value */
1313c602fabSXin LI 		if (!ndo->ndo_eflag)
1323c602fabSXin LI 			null_hdr_print(ndo, family, length + NULL_HDRLEN);
1333c602fabSXin LI 		if (!ndo->ndo_suppress_default_print)
1343c602fabSXin LI 			ND_DEFAULTPRINT(p, caplen);
135a88113a8SBill Fenner 	}
1364edb46e9SPaul Traina }
137