xref: /openbsd-src/usr.sbin/tcpdump/print-null.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: print-null.c,v 1.14 2001/06/25 19:53:54 provos Exp $	*/
2 
3 /*
4  * Copyright (c) 1991, 1993, 1994, 1995, 1996, 1997
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  */
23 
24 #ifndef lint
25 static const char rcsid[] =
26     "@(#) $Header: /home/cvs/src/usr.sbin/tcpdump/print-null.c,v 1.14 2001/06/25 19:53:54 provos Exp $ (LBL)";
27 #endif
28 
29 #include <sys/param.h>
30 #include <sys/time.h>
31 #include <sys/socket.h>
32 #include <sys/file.h>
33 #include <sys/ioctl.h>
34 
35 #ifdef __STDC__
36 struct mbuf;
37 struct rtentry;
38 #endif
39 #include <net/if.h>
40 
41 #include <netinet/in.h>
42 #include <netinet/in_systm.h>
43 #include <netinet/ip.h>
44 #include <netinet/if_ether.h>
45 #include <netinet/ip_var.h>
46 #include <netinet/udp.h>
47 #include <netinet/udp_var.h>
48 #include <netinet/tcp.h>
49 
50 #include <pcap.h>
51 #include <stdio.h>
52 #include <string.h>
53 
54 #ifdef INET6
55 #include <netinet/ip6.h>
56 #endif
57 
58 #include "interface.h"
59 #include "addrtoname.h"
60 
61 #ifndef AF_NS
62 #define AF_NS		6		/* XEROX NS protocols */
63 #endif
64 
65 /*
66  * The DLT_NULL packet header is 4 bytes long. It contains a network
67  * order 32 bit integer that specifies the family, e.g. AF_INET
68  */
69 #define	NULL_HDRLEN 4
70 
71 static void
72 null_print(const u_char *p, const struct ip *ip, u_int length)
73 {
74 	u_int family;
75 
76 	memcpy((char *)&family, (char *)p, sizeof(family));
77 	family = ntohl(family);
78 
79 	if (nflag && family != AF_LINK) {
80 		/* XXX just dump the header */
81 		return;
82 	}
83 	switch (family) {
84 
85 	case AF_INET:
86 		printf("ip: ");
87 		break;
88 
89 #ifdef INET6
90 	case AF_INET6:
91 		printf("ip6: ");
92 		break;
93 #endif
94 
95 	case AF_NS:
96 		printf("ns: ");
97 		break;
98 
99 #ifdef __OpenBSD__
100 	case AF_LINK:
101 		ether_print(p + NULL_HDRLEN, length);
102 		break;
103 #endif
104 
105 	default:
106 		printf("AF %d: ", family);
107 		break;
108 	}
109 }
110 
111 void
112 null_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
113 {
114 	u_int length = h->len;
115 	u_int caplen = h->caplen;
116 	u_int family;
117 
118 #ifdef __OpenBSD__
119 	struct ether_header *ep;
120 	u_short ether_type;
121 	extern u_short extracted_ethertype;
122 #endif
123 
124 	memcpy((char *)&family, (char *)p, sizeof(family));
125 	family = ntohl(family);
126 
127 	ts_print(&h->ts);
128 
129 	/*
130 	 * Some printers want to get back at the link level addresses,
131 	 * and/or check that they're not walking off the end of the packet.
132 	 * Rather than pass them all the way down, we set these globals.
133 	 */
134 	packetp = p;
135 	snapend = p + caplen;
136 
137 	length -= NULL_HDRLEN;
138 
139 	if (eflag)
140 		null_print(p, (struct ip *)(p + NULL_HDRLEN), length);
141 
142 	switch (family) {
143 	case AF_INET:
144 		ip_print(p + NULL_HDRLEN, length);
145 		break;
146 
147 #ifdef INET6
148 	case AF_INET6:
149 		ip6_print(p + NULL_HDRLEN, length);
150 		break;
151 #endif /*INET6*/
152 
153 #ifdef __OpenBSD__
154 	case AF_LINK:
155 		if (caplen < sizeof(struct ether_header) + NULL_HDRLEN) {
156 			printf("[|ether]");
157 			goto out;
158 		}
159 
160 		length -= sizeof(struct ether_header);
161 		caplen -= sizeof(struct ether_header);
162 		ep = (struct ether_header *)(p + NULL_HDRLEN);
163 		p += NULL_HDRLEN + sizeof(struct ether_header);
164 		packetp += sizeof(struct ether_header);
165 		ether_type = ntohs(ep->ether_type);
166 
167 		extracted_ethertype = 0;
168 		if (ether_type <= ETHERMTU) {
169 			/* Try to print the LLC-layer header & higher layers */
170 			if (llc_print(p, length, caplen, ESRC(ep),
171 			    EDST(ep)) == 0) {
172 				/* ether_type not known, print raw packet */
173 				if (!eflag)
174 					ether_print((u_char *)ep, length);
175 				if (extracted_ethertype) {
176 					printf("(LLC %s) ",
177 					       etherproto_string(htons(extracted_ethertype)));
178 				}
179 				if (!xflag && !qflag)
180 					default_print(p, caplen);
181 			}
182 		} else if (ether_encap_print(ether_type, p, length,
183 		           caplen) == 0) {
184 			/* ether_type not known, print raw packet */
185 			if (!eflag)
186 				ether_print((u_char *)ep, length +
187 				    sizeof(*ep));
188 			if (!xflag && !qflag)
189 				default_print(p, caplen);
190 		}
191 		break;
192 #endif /* __OpenBSD__ */
193 	}
194 
195 	if (xflag)
196 		default_print((const u_char *)(packetp + NULL_HDRLEN),
197 		    caplen - NULL_HDRLEN);
198  out:
199 	putchar('\n');
200 }
201 
202