xref: /freebsd/contrib/tcpdump/print-null.c (revision cc391cce1125c1163b0cc425704a4279ca0dbe9a)
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.
20a88113a8SBill Fenner  *
21a88113a8SBill Fenner  * $FreeBSD$
224edb46e9SPaul Traina  */
234edb46e9SPaul Traina 
244edb46e9SPaul Traina #ifndef lint
25cc391cceSBruce M Simpson static const char rcsid[] _U_ =
26cc391cceSBruce M Simpson     "@(#) $Header: /tcpdump/master/tcpdump/print-null.c,v 1.49.2.2 2003/11/16 08:51:36 guy Exp $ (LBL)";
27a88113a8SBill Fenner #endif
28a88113a8SBill Fenner 
29a88113a8SBill Fenner #ifdef HAVE_CONFIG_H
30a88113a8SBill Fenner #include "config.h"
314edb46e9SPaul Traina #endif
324edb46e9SPaul Traina 
33cc391cceSBruce M Simpson #include <tcpdump-stdinc.h>
344edb46e9SPaul Traina 
352ebf6c05SBill Fenner #include <pcap.h>
364edb46e9SPaul Traina #include <stdio.h>
374edb46e9SPaul Traina #include <string.h>
384edb46e9SPaul Traina 
392ebf6c05SBill Fenner #include "interface.h"
40699fc314SBill Fenner #include "addrtoname.h"
414edb46e9SPaul Traina 
42943ee2b1SBill Fenner #include "ip.h"
43943ee2b1SBill Fenner #ifdef INET6
44943ee2b1SBill Fenner #include "ip6.h"
45943ee2b1SBill Fenner #endif
46943ee2b1SBill Fenner 
47699fc314SBill Fenner /*
48943ee2b1SBill Fenner  * The DLT_NULL packet header is 4 bytes long. It contains a host-byte-order
49943ee2b1SBill Fenner  * 32-bit integer that specifies the family, e.g. AF_INET.
50943ee2b1SBill Fenner  *
51943ee2b1SBill Fenner  * Note here that "host" refers to the host on which the packets were
52943ee2b1SBill Fenner  * captured; that isn't necessarily *this* host.
53943ee2b1SBill Fenner  *
54943ee2b1SBill Fenner  * The OpenBSD DLT_LOOP packet header is the same, except that the integer
55943ee2b1SBill Fenner  * is in network byte order.
56699fc314SBill Fenner  */
57699fc314SBill Fenner #define	NULL_HDRLEN 4
58699fc314SBill Fenner 
59cc391cceSBruce M Simpson /*
60cc391cceSBruce M Simpson  * BSD AF_ values.
61cc391cceSBruce M Simpson  *
62cc391cceSBruce M Simpson  * Unfortunately, the BSDs don't all use the same value for AF_INET6,
63cc391cceSBruce M Simpson  * so, because we want to be able to read captures from all of the BSDs,
64cc391cceSBruce M Simpson  * we check for all of them.
65cc391cceSBruce M Simpson  */
66cc391cceSBruce M Simpson #define BSD_AF_INET		2
67cc391cceSBruce M Simpson #define BSD_AF_NS		6		/* XEROX NS protocols */
68cc391cceSBruce M Simpson #define BSD_AF_ISO		7
69cc391cceSBruce M Simpson #define BSD_AF_APPLETALK	16
70cc391cceSBruce M Simpson #define BSD_AF_IPX		23
71cc391cceSBruce M Simpson #define BSD_AF_INET6_BSD	24	/* OpenBSD (and probably NetBSD), BSD/OS */
72cc391cceSBruce M Simpson #define BSD_AF_INET6_FREEBSD	28
73cc391cceSBruce M Simpson #define BSD_AF_INET6_DARWIN	30
74cc391cceSBruce M Simpson 
754edb46e9SPaul Traina static void
76943ee2b1SBill Fenner null_print(u_int family, u_int length)
774edb46e9SPaul Traina {
78943ee2b1SBill Fenner 	if (nflag)
79943ee2b1SBill Fenner 		printf("AF %u ", family);
80943ee2b1SBill Fenner 	else {
814edb46e9SPaul Traina 		switch (family) {
824edb46e9SPaul Traina 
83cc391cceSBruce M Simpson 		case BSD_AF_INET:
84943ee2b1SBill Fenner 			printf("ip ");
854edb46e9SPaul Traina 			break;
864edb46e9SPaul Traina 
87a88113a8SBill Fenner #ifdef INET6
88cc391cceSBruce M Simpson 		case BSD_AF_INET6_BSD:
89cc391cceSBruce M Simpson 		case BSD_AF_INET6_FREEBSD:
90cc391cceSBruce M Simpson 		case BSD_AF_INET6_DARWIN:
91943ee2b1SBill Fenner 			printf("ip6 ");
92a88113a8SBill Fenner 			break;
93a88113a8SBill Fenner #endif
94a88113a8SBill Fenner 
95cc391cceSBruce M Simpson 		case BSD_AF_NS:
96943ee2b1SBill Fenner 			printf("ns ");
974edb46e9SPaul Traina 			break;
984edb46e9SPaul Traina 
99cc391cceSBruce M Simpson 		case BSD_AF_ISO:
100cc391cceSBruce M Simpson 			printf("osi ");
101cc391cceSBruce M Simpson 			break;
102cc391cceSBruce M Simpson 
103cc391cceSBruce M Simpson 		case BSD_AF_APPLETALK:
104cc391cceSBruce M Simpson 			printf("atalk ");
105cc391cceSBruce M Simpson 			break;
106cc391cceSBruce M Simpson 
107cc391cceSBruce M Simpson 		case BSD_AF_IPX:
108cc391cceSBruce M Simpson 			printf("ipx ");
109cc391cceSBruce M Simpson 			break;
110cc391cceSBruce M Simpson 
1114edb46e9SPaul Traina 		default:
112943ee2b1SBill Fenner 			printf("AF %u ", family);
1134edb46e9SPaul Traina 			break;
1144edb46e9SPaul Traina 		}
1154edb46e9SPaul Traina 	}
116943ee2b1SBill Fenner 	printf("%d: ", length);
117943ee2b1SBill Fenner }
118943ee2b1SBill Fenner 
119943ee2b1SBill Fenner /*
120943ee2b1SBill Fenner  * Byte-swap a 32-bit number.
121943ee2b1SBill Fenner  * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
122943ee2b1SBill Fenner  * big-endian platforms.)
123943ee2b1SBill Fenner  */
124943ee2b1SBill Fenner #define	SWAPLONG(y) \
125943ee2b1SBill Fenner ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1264edb46e9SPaul Traina 
127cc391cceSBruce M Simpson /*
128cc391cceSBruce M Simpson  * This is the top level routine of the printer.  'p' points
129cc391cceSBruce M Simpson  * to the ether header of the packet, 'h->ts' is the timestamp,
130cc391cceSBruce M Simpson  * 'h->length' is the length of the packet off the wire, and 'h->caplen'
131cc391cceSBruce M Simpson  * is the number of bytes actually captured.
132cc391cceSBruce M Simpson  */
133cc391cceSBruce M Simpson u_int
134cc391cceSBruce M Simpson null_if_print(const struct pcap_pkthdr *h, const u_char *p)
1354edb46e9SPaul Traina {
1364edb46e9SPaul Traina 	u_int length = h->len;
1374edb46e9SPaul Traina 	u_int caplen = h->caplen;
138943ee2b1SBill Fenner 	u_int family;
1394edb46e9SPaul Traina 
140cc391cceSBruce M Simpson 	if (caplen < NULL_HDRLEN) {
141cc391cceSBruce M Simpson 		printf("[|null]");
142cc391cceSBruce M Simpson 		return (NULL_HDRLEN);
143cc391cceSBruce M Simpson 	}
1444edb46e9SPaul Traina 
145943ee2b1SBill Fenner 	memcpy((char *)&family, (char *)p, sizeof(family));
146943ee2b1SBill Fenner 
147943ee2b1SBill Fenner 	/*
148943ee2b1SBill Fenner 	 * This isn't necessarily in our host byte order; if this is
149943ee2b1SBill Fenner 	 * a DLT_LOOP capture, it's in network byte order, and if
150943ee2b1SBill Fenner 	 * this is a DLT_NULL capture from a machine with the opposite
151943ee2b1SBill Fenner 	 * byte-order, it's in the opposite byte order from ours.
152943ee2b1SBill Fenner 	 *
153943ee2b1SBill Fenner 	 * If the upper 16 bits aren't all zero, assume it's byte-swapped.
154943ee2b1SBill Fenner 	 */
155943ee2b1SBill Fenner 	if ((family & 0xFFFF0000) != 0)
156943ee2b1SBill Fenner 		family = SWAPLONG(family);
157943ee2b1SBill Fenner 
1584edb46e9SPaul Traina 	length -= NULL_HDRLEN;
159cc391cceSBruce M Simpson 	caplen -= NULL_HDRLEN;
160cc391cceSBruce M Simpson 	p += NULL_HDRLEN;
1614edb46e9SPaul Traina 
1624edb46e9SPaul Traina 	if (eflag)
163943ee2b1SBill Fenner 		null_print(family, length);
1644edb46e9SPaul Traina 
165cc391cceSBruce M Simpson 	switch (family) {
166cc391cceSBruce M Simpson 
167cc391cceSBruce M Simpson 	case BSD_AF_INET:
168cc391cceSBruce M Simpson 		ip_print(p, length);
169a88113a8SBill Fenner 		break;
170cc391cceSBruce M Simpson 
171a88113a8SBill Fenner #ifdef INET6
172cc391cceSBruce M Simpson 	case BSD_AF_INET6_BSD:
173cc391cceSBruce M Simpson 	case BSD_AF_INET6_FREEBSD:
174cc391cceSBruce M Simpson 	case BSD_AF_INET6_DARWIN:
175cc391cceSBruce M Simpson 		ip6_print(p, length);
176a88113a8SBill Fenner 		break;
177cc391cceSBruce M Simpson #endif
178cc391cceSBruce M Simpson 
179cc391cceSBruce M Simpson 	case BSD_AF_ISO:
180cc391cceSBruce M Simpson 		isoclns_print(p, length, caplen);
181cc391cceSBruce M Simpson 		break;
182cc391cceSBruce M Simpson 
183cc391cceSBruce M Simpson 	case BSD_AF_APPLETALK:
184cc391cceSBruce M Simpson 		atalk_print(p, length);
185cc391cceSBruce M Simpson 		break;
186cc391cceSBruce M Simpson 
187cc391cceSBruce M Simpson 	case BSD_AF_IPX:
188cc391cceSBruce M Simpson 		ipx_print(p, length);
189cc391cceSBruce M Simpson 		break;
190cc391cceSBruce M Simpson 
191a88113a8SBill Fenner 	default:
192cc391cceSBruce M Simpson 		/* unknown AF_ value */
193cc391cceSBruce M Simpson 		if (!eflag)
194cc391cceSBruce M Simpson 			null_print(family, length + NULL_HDRLEN);
195cc391cceSBruce M Simpson 		if (!xflag && !qflag)
196cc391cceSBruce M Simpson 			default_print(p, caplen);
197a88113a8SBill Fenner 	}
1984edb46e9SPaul Traina 
199cc391cceSBruce M Simpson 	return (NULL_HDRLEN);
2004edb46e9SPaul Traina }
2014edb46e9SPaul Traina 
202