xref: /freebsd/contrib/tcpdump/print-null.c (revision 943ee2b15a8e008b2087495c2986bac507caef79)
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
252ebf6c05SBill Fenner static const char rcsid[] =
26943ee2b1SBill Fenner     "@(#) $Header: /tcpdump/master/tcpdump/print-null.c,v 1.40 2000/12/16 22:00:50 guy Exp $ (LBL)";
27a88113a8SBill Fenner #endif
28a88113a8SBill Fenner 
29a88113a8SBill Fenner #ifdef HAVE_CONFIG_H
30a88113a8SBill Fenner #include "config.h"
314edb46e9SPaul Traina #endif
324edb46e9SPaul Traina 
334edb46e9SPaul Traina #include <sys/param.h>
344edb46e9SPaul Traina #include <sys/time.h>
354edb46e9SPaul Traina #include <sys/socket.h>
364edb46e9SPaul Traina #include <sys/file.h>
374edb46e9SPaul Traina #include <sys/ioctl.h>
384edb46e9SPaul Traina 
394edb46e9SPaul Traina struct mbuf;
404edb46e9SPaul Traina struct rtentry;
414edb46e9SPaul Traina 
424edb46e9SPaul Traina #include <netinet/in.h>
434edb46e9SPaul Traina 
442ebf6c05SBill Fenner #include <pcap.h>
454edb46e9SPaul Traina #include <stdio.h>
464edb46e9SPaul Traina #include <string.h>
474edb46e9SPaul Traina 
482ebf6c05SBill Fenner #include "interface.h"
49699fc314SBill Fenner #include "addrtoname.h"
504edb46e9SPaul Traina 
51943ee2b1SBill Fenner #include "ip.h"
52943ee2b1SBill Fenner #ifdef INET6
53943ee2b1SBill Fenner #include "ip6.h"
54943ee2b1SBill Fenner #endif
55943ee2b1SBill Fenner 
562ebf6c05SBill Fenner #ifndef AF_NS
572ebf6c05SBill Fenner #define AF_NS		6		/* XEROX NS protocols */
582ebf6c05SBill Fenner #endif
592ebf6c05SBill Fenner 
60699fc314SBill Fenner /*
61943ee2b1SBill Fenner  * The DLT_NULL packet header is 4 bytes long. It contains a host-byte-order
62943ee2b1SBill Fenner  * 32-bit integer that specifies the family, e.g. AF_INET.
63943ee2b1SBill Fenner  *
64943ee2b1SBill Fenner  * Note here that "host" refers to the host on which the packets were
65943ee2b1SBill Fenner  * captured; that isn't necessarily *this* host.
66943ee2b1SBill Fenner  *
67943ee2b1SBill Fenner  * The OpenBSD DLT_LOOP packet header is the same, except that the integer
68943ee2b1SBill Fenner  * is in network byte order.
69699fc314SBill Fenner  */
70699fc314SBill Fenner #define	NULL_HDRLEN 4
71699fc314SBill Fenner 
724edb46e9SPaul Traina static void
73943ee2b1SBill Fenner null_print(u_int family, u_int length)
744edb46e9SPaul Traina {
75943ee2b1SBill Fenner 	if (nflag)
76943ee2b1SBill Fenner 		printf("AF %u ", family);
77943ee2b1SBill Fenner 	else {
784edb46e9SPaul Traina 		switch (family) {
794edb46e9SPaul Traina 
804edb46e9SPaul Traina 		case AF_INET:
81943ee2b1SBill Fenner 			printf("ip ");
824edb46e9SPaul Traina 			break;
834edb46e9SPaul Traina 
84a88113a8SBill Fenner #ifdef INET6
85a88113a8SBill Fenner 		case AF_INET6:
86943ee2b1SBill Fenner 			printf("ip6 ");
87a88113a8SBill Fenner 			break;
88a88113a8SBill Fenner #endif
89a88113a8SBill Fenner 
904edb46e9SPaul Traina 		case AF_NS:
91943ee2b1SBill Fenner 			printf("ns ");
924edb46e9SPaul Traina 			break;
934edb46e9SPaul Traina 
944edb46e9SPaul Traina 		default:
95943ee2b1SBill Fenner 			printf("AF %u ", family);
964edb46e9SPaul Traina 			break;
974edb46e9SPaul Traina 		}
984edb46e9SPaul Traina 	}
99943ee2b1SBill Fenner 	printf("%d: ", length);
100943ee2b1SBill Fenner }
101943ee2b1SBill Fenner 
102943ee2b1SBill Fenner /*
103943ee2b1SBill Fenner  * Byte-swap a 32-bit number.
104943ee2b1SBill Fenner  * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
105943ee2b1SBill Fenner  * big-endian platforms.)
106943ee2b1SBill Fenner  */
107943ee2b1SBill Fenner #define	SWAPLONG(y) \
108943ee2b1SBill Fenner ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1094edb46e9SPaul Traina 
1104edb46e9SPaul Traina void
1114edb46e9SPaul Traina null_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
1124edb46e9SPaul Traina {
1134edb46e9SPaul Traina 	u_int length = h->len;
1144edb46e9SPaul Traina 	u_int caplen = h->caplen;
1154edb46e9SPaul Traina 	const struct ip *ip;
116943ee2b1SBill Fenner 	u_int family;
1174edb46e9SPaul Traina 
1184edb46e9SPaul Traina 	ts_print(&h->ts);
1194edb46e9SPaul Traina 
120943ee2b1SBill Fenner 	memcpy((char *)&family, (char *)p, sizeof(family));
121943ee2b1SBill Fenner 
122943ee2b1SBill Fenner 	/*
123943ee2b1SBill Fenner 	 * This isn't necessarily in our host byte order; if this is
124943ee2b1SBill Fenner 	 * a DLT_LOOP capture, it's in network byte order, and if
125943ee2b1SBill Fenner 	 * this is a DLT_NULL capture from a machine with the opposite
126943ee2b1SBill Fenner 	 * byte-order, it's in the opposite byte order from ours.
127943ee2b1SBill Fenner 	 *
128943ee2b1SBill Fenner 	 * If the upper 16 bits aren't all zero, assume it's byte-swapped.
129943ee2b1SBill Fenner 	 */
130943ee2b1SBill Fenner 	if ((family & 0xFFFF0000) != 0)
131943ee2b1SBill Fenner 		family = SWAPLONG(family);
132943ee2b1SBill Fenner 
1334edb46e9SPaul Traina 	/*
1344edb46e9SPaul Traina 	 * Some printers want to get back at the link level addresses,
1354edb46e9SPaul Traina 	 * and/or check that they're not walking off the end of the packet.
1364edb46e9SPaul Traina 	 * Rather than pass them all the way down, we set these globals.
1374edb46e9SPaul Traina 	 */
1384edb46e9SPaul Traina 	packetp = p;
1394edb46e9SPaul Traina 	snapend = p + caplen;
1404edb46e9SPaul Traina 
1414edb46e9SPaul Traina 	length -= NULL_HDRLEN;
1424edb46e9SPaul Traina 
1434edb46e9SPaul Traina 	ip = (struct ip *)(p + NULL_HDRLEN);
1444edb46e9SPaul Traina 
1454edb46e9SPaul Traina 	if (eflag)
146943ee2b1SBill Fenner 		null_print(family, length);
1474edb46e9SPaul Traina 
148943ee2b1SBill Fenner 	switch (IP_V(ip)) {
149a88113a8SBill Fenner 	case 4:
1504edb46e9SPaul Traina 		ip_print((const u_char *)ip, length);
151a88113a8SBill Fenner 		break;
152a88113a8SBill Fenner #ifdef INET6
153a88113a8SBill Fenner 	case 6:
154a88113a8SBill Fenner 		ip6_print((const u_char *)ip, length);
155a88113a8SBill Fenner 		break;
156a88113a8SBill Fenner #endif /* INET6 */
157a88113a8SBill Fenner 	default:
158943ee2b1SBill Fenner 		printf("ip v%d", IP_V(ip));
159a88113a8SBill Fenner 		break;
160a88113a8SBill Fenner 	}
1614edb46e9SPaul Traina 
1624edb46e9SPaul Traina 	if (xflag)
1634edb46e9SPaul Traina 		default_print((const u_char *)ip, caplen - NULL_HDRLEN);
1644edb46e9SPaul Traina 	putchar('\n');
1654edb46e9SPaul Traina }
1664edb46e9SPaul Traina 
167