xref: /openbsd-src/usr.sbin/tcpdump/print-null.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: print-null.c,v 1.18 2008/12/29 10:02:55 michele 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     "@(#) $Id: print-null.c,v 1.18 2008/12/29 10:02:55 michele 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 struct mbuf;
36 struct rtentry;
37 #include <net/if.h>
38 
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
42 #include <netinet/if_ether.h>
43 #include <netinet/ip_var.h>
44 #include <netinet/udp.h>
45 #include <netinet/udp_var.h>
46 #include <netinet/tcp.h>
47 
48 #include <pcap.h>
49 #include <stdio.h>
50 #include <string.h>
51 
52 #ifdef INET6
53 #include <netinet/ip6.h>
54 #endif
55 
56 #include "interface.h"
57 #include "addrtoname.h"
58 
59 #ifndef AF_NS
60 #define AF_NS		6		/* XEROX NS protocols */
61 #endif
62 
63 /*
64  * The DLT_NULL packet header is 4 bytes long. It contains a host
65  * order 32 bit integer that specifies the family, e.g. AF_INET
66  */
67 #define	NULL_HDRLEN 4
68 
69 static void
70 null_print(const u_char *p, const struct ip *ip, u_int length)
71 {
72 	u_int family;
73 
74 	memcpy((char *)&family, (char *)p, sizeof(family));
75 
76 	if (nflag && family != AF_LINK) {
77 		/* XXX just dump the header */
78 		return;
79 	}
80 	switch (family) {
81 
82 	case AF_INET:
83 		printf("ip: ");
84 		break;
85 
86 #ifdef INET6
87 	case AF_INET6:
88 		printf("ip6: ");
89 		break;
90 #endif
91 
92 	case AF_NS:
93 		printf("ns: ");
94 		break;
95 
96 #ifdef __OpenBSD__
97 	case AF_LINK:
98 		ether_print(p + NULL_HDRLEN, length);
99 		break;
100 #endif
101 	case AF_MPLS:
102 		printf("mpls: ");
103 		break;
104 
105 	default:
106 		printf("AF %d: ", family);
107 		break;
108 	}
109 }
110 
111 void
112 loop_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
113 {
114 	*(u_int *)p = ntohl(*(u_int *)p);
115 
116 	null_if_print(user, h, p);
117 }
118 
119 void
120 null_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
121 {
122 	u_int length = h->len;
123 	u_int caplen = h->caplen;
124 	u_int family = *(u_int *)p;
125 
126 #ifdef __OpenBSD__
127 	struct ether_header *ep;
128 	u_short ether_type;
129 	extern u_short extracted_ethertype;
130 #endif
131 
132 	ts_print(&h->ts);
133 
134 	/*
135 	 * Some printers want to get back at the link level addresses,
136 	 * and/or check that they're not walking off the end of the packet.
137 	 * Rather than pass them all the way down, we set these globals.
138 	 */
139 	packetp = p;
140 	snapend = p + caplen;
141 
142 	length -= NULL_HDRLEN;
143 
144 	if (eflag)
145 		null_print(p, (struct ip *)(p + NULL_HDRLEN), length);
146 
147 	switch (family) {
148 	case AF_INET:
149 		ip_print(p + NULL_HDRLEN, length);
150 		break;
151 
152 #ifdef INET6
153 	case AF_INET6:
154 		ip6_print(p + NULL_HDRLEN, length);
155 		break;
156 #endif /*INET6*/
157 
158 	case AF_MPLS:
159 		mpls_print(p + NULL_HDRLEN, length);
160 		break;
161 
162 #ifdef __OpenBSD__
163 	case AF_LINK:
164 		if (caplen < sizeof(struct ether_header) + NULL_HDRLEN) {
165 			printf("[|ether]");
166 			goto out;
167 		}
168 
169 		length -= sizeof(struct ether_header);
170 		caplen -= sizeof(struct ether_header);
171 		ep = (struct ether_header *)(p + NULL_HDRLEN);
172 		p += NULL_HDRLEN + sizeof(struct ether_header);
173 		packetp += sizeof(struct ether_header);
174 		ether_type = ntohs(ep->ether_type);
175 
176 		extracted_ethertype = 0;
177 		if (ether_type <= ETHERMTU) {
178 			/* Try to print the LLC-layer header & higher layers */
179 			if (llc_print(p, length, caplen, ESRC(ep),
180 			    EDST(ep)) == 0) {
181 				/* ether_type not known, print raw packet */
182 				if (!eflag)
183 					ether_print((u_char *)ep, length);
184 				if (extracted_ethertype) {
185 					printf("(LLC %s) ",
186 					       etherproto_string(htons(extracted_ethertype)));
187 				}
188 				if (!xflag && !qflag)
189 					default_print(p, caplen);
190 			}
191 		} else if (ether_encap_print(ether_type, p, length,
192 		           caplen) == 0) {
193 			/* ether_type not known, print raw packet */
194 			if (!eflag)
195 				ether_print((u_char *)ep, length +
196 				    sizeof(*ep));
197 			if (!xflag && !qflag)
198 				default_print(p, caplen);
199 		}
200 		break;
201 #endif /* __OpenBSD__ */
202 	}
203 
204 	if (xflag)
205 		default_print((const u_char *)(packetp + NULL_HDRLEN),
206 		    caplen - NULL_HDRLEN);
207  out:
208 	putchar('\n');
209 }
210 
211