xref: /freebsd/contrib/tcpdump/print-null.c (revision 29292c17af721e5dabd42f1d89b511352ed945ed)
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_ =
2629292c17SSam Leffler     "@(#) $Header: /tcpdump/master/tcpdump/print-null.c,v 1.53.2.2 2005/05/19 07:26:18 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 
7529292c17SSam Leffler const struct tok bsd_af_values[] = {
7629292c17SSam Leffler         { BSD_AF_INET, "IPv4" },
7729292c17SSam Leffler         { BSD_AF_NS, "NS" },
7829292c17SSam Leffler         { BSD_AF_ISO, "ISO" },
7929292c17SSam Leffler         { BSD_AF_APPLETALK, "Appletalk" },
8029292c17SSam Leffler         { BSD_AF_IPX, "IPX" },
8129292c17SSam Leffler         { BSD_AF_INET6_BSD, "IPv6" },
8229292c17SSam Leffler         { BSD_AF_INET6_FREEBSD, "IPv6" },
8329292c17SSam Leffler         { BSD_AF_INET6_DARWIN, "IPv6" },
8429292c17SSam Leffler         { 0, NULL}
8529292c17SSam Leffler };
864edb46e9SPaul Traina 
87943ee2b1SBill Fenner 
88943ee2b1SBill Fenner /*
89943ee2b1SBill Fenner  * Byte-swap a 32-bit number.
90943ee2b1SBill Fenner  * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
91943ee2b1SBill Fenner  * big-endian platforms.)
92943ee2b1SBill Fenner  */
93943ee2b1SBill Fenner #define	SWAPLONG(y) \
94943ee2b1SBill Fenner ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
954edb46e9SPaul Traina 
9629292c17SSam Leffler static inline void
9729292c17SSam Leffler null_hdr_print(u_int family, u_int length)
9829292c17SSam Leffler {
9929292c17SSam Leffler 	if (!qflag) {
10029292c17SSam Leffler 		(void)printf("AF %s (%u)",
10129292c17SSam Leffler 			tok2str(bsd_af_values,"Unknown",family),family);
10229292c17SSam Leffler 	} else {
10329292c17SSam Leffler 		(void)printf("%s",
10429292c17SSam Leffler 			tok2str(bsd_af_values,"Unknown AF %u",family));
10529292c17SSam Leffler 	}
10629292c17SSam Leffler 
10729292c17SSam Leffler 	(void)printf(", length %u: ", length);
10829292c17SSam Leffler }
10929292c17SSam Leffler 
110cc391cceSBruce M Simpson /*
111cc391cceSBruce M Simpson  * This is the top level routine of the printer.  'p' points
112cc391cceSBruce M Simpson  * to the ether header of the packet, 'h->ts' is the timestamp,
113c1ad1296SSam Leffler  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
114cc391cceSBruce M Simpson  * is the number of bytes actually captured.
115cc391cceSBruce M Simpson  */
116cc391cceSBruce M Simpson u_int
117cc391cceSBruce M Simpson null_if_print(const struct pcap_pkthdr *h, const u_char *p)
1184edb46e9SPaul Traina {
1194edb46e9SPaul Traina 	u_int length = h->len;
1204edb46e9SPaul Traina 	u_int caplen = h->caplen;
121943ee2b1SBill Fenner 	u_int family;
1224edb46e9SPaul Traina 
123cc391cceSBruce M Simpson 	if (caplen < NULL_HDRLEN) {
124cc391cceSBruce M Simpson 		printf("[|null]");
125cc391cceSBruce M Simpson 		return (NULL_HDRLEN);
126cc391cceSBruce M Simpson 	}
1274edb46e9SPaul Traina 
128943ee2b1SBill Fenner 	memcpy((char *)&family, (char *)p, sizeof(family));
129943ee2b1SBill Fenner 
130943ee2b1SBill Fenner 	/*
131943ee2b1SBill Fenner 	 * This isn't necessarily in our host byte order; if this is
132943ee2b1SBill Fenner 	 * a DLT_LOOP capture, it's in network byte order, and if
133943ee2b1SBill Fenner 	 * this is a DLT_NULL capture from a machine with the opposite
134943ee2b1SBill Fenner 	 * byte-order, it's in the opposite byte order from ours.
135943ee2b1SBill Fenner 	 *
136943ee2b1SBill Fenner 	 * If the upper 16 bits aren't all zero, assume it's byte-swapped.
137943ee2b1SBill Fenner 	 */
138943ee2b1SBill Fenner 	if ((family & 0xFFFF0000) != 0)
139943ee2b1SBill Fenner 		family = SWAPLONG(family);
140943ee2b1SBill Fenner 
14129292c17SSam Leffler 	if (eflag)
14229292c17SSam Leffler 		null_hdr_print(family, length);
14329292c17SSam Leffler 
1444edb46e9SPaul Traina 	length -= NULL_HDRLEN;
145cc391cceSBruce M Simpson 	caplen -= NULL_HDRLEN;
146cc391cceSBruce M Simpson 	p += NULL_HDRLEN;
1474edb46e9SPaul Traina 
148cc391cceSBruce M Simpson 	switch (family) {
149cc391cceSBruce M Simpson 
150cc391cceSBruce M Simpson 	case BSD_AF_INET:
151c1ad1296SSam Leffler 		ip_print(gndo, p, length);
152a88113a8SBill Fenner 		break;
153cc391cceSBruce M Simpson 
154a88113a8SBill Fenner #ifdef INET6
155cc391cceSBruce M Simpson 	case BSD_AF_INET6_BSD:
156cc391cceSBruce M Simpson 	case BSD_AF_INET6_FREEBSD:
157cc391cceSBruce M Simpson 	case BSD_AF_INET6_DARWIN:
158cc391cceSBruce M Simpson 		ip6_print(p, length);
159a88113a8SBill Fenner 		break;
160cc391cceSBruce M Simpson #endif
161cc391cceSBruce M Simpson 
162cc391cceSBruce M Simpson 	case BSD_AF_ISO:
163cc391cceSBruce M Simpson 		isoclns_print(p, length, caplen);
164cc391cceSBruce M Simpson 		break;
165cc391cceSBruce M Simpson 
166cc391cceSBruce M Simpson 	case BSD_AF_APPLETALK:
167cc391cceSBruce M Simpson 		atalk_print(p, length);
168cc391cceSBruce M Simpson 		break;
169cc391cceSBruce M Simpson 
170cc391cceSBruce M Simpson 	case BSD_AF_IPX:
171cc391cceSBruce M Simpson 		ipx_print(p, length);
172cc391cceSBruce M Simpson 		break;
173cc391cceSBruce M Simpson 
174a88113a8SBill Fenner 	default:
175cc391cceSBruce M Simpson 		/* unknown AF_ value */
176cc391cceSBruce M Simpson 		if (!eflag)
17729292c17SSam Leffler 			null_hdr_print(family, length + NULL_HDRLEN);
178cc391cceSBruce M Simpson 		if (!xflag && !qflag)
179cc391cceSBruce M Simpson 			default_print(p, caplen);
180a88113a8SBill Fenner 	}
1814edb46e9SPaul Traina 
182cc391cceSBruce M Simpson 	return (NULL_HDRLEN);
1834edb46e9SPaul Traina }
1844edb46e9SPaul Traina 
185c1ad1296SSam Leffler /*
186c1ad1296SSam Leffler  * Local Variables:
187c1ad1296SSam Leffler  * c-style: whitesmith
188c1ad1296SSam Leffler  * c-basic-offset: 8
189c1ad1296SSam Leffler  * End:
190c1ad1296SSam Leffler  */
191