xref: /openbsd-src/usr.sbin/tcpdump/print-ether.c (revision d4c5fc9dc00f5a9cadd8c2de4e52d85d3c1c6003)
1 /*	$OpenBSD: print-ether.c,v 1.32 2018/04/03 01:57:31 dlg Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 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 #include <sys/time.h>
25 #include <sys/socket.h>
26 
27 struct mbuf;
28 struct rtentry;
29 #include <net/if.h>
30 
31 #include <netinet/in.h>
32 #include <netinet/if_ether.h>
33 #include <netinet/ip.h>
34 #include <netinet/ip_var.h>
35 #include <netinet/udp.h>
36 #include <netinet/udp_var.h>
37 #include <netinet/tcp.h>
38 
39 #include <stdio.h>
40 #include <pcap.h>
41 
42 #ifdef INET6
43 #include <netinet/ip6.h>
44 #endif
45 
46 #include "interface.h"
47 #include "addrtoname.h"
48 #include "ethertype.h"
49 #include "extract.h"
50 
51 const u_char *packetp;
52 const u_char *snapend;
53 
54 void ether_macctl(const u_char *, u_int);
55 
56 void
57 ether_print(const u_char *bp, u_int length)
58 {
59 	const struct ether_header *ep;
60 
61 	ep = (const struct ether_header *)bp;
62 	if (qflag) {
63 		TCHECK2(*ep, 12);
64 		(void)printf("%s %s %d: ",
65 			     etheraddr_string(ESRC(ep)),
66 			     etheraddr_string(EDST(ep)),
67 			     length);
68 	} else {
69 		TCHECK2(*ep, 14);
70 		(void)printf("%s %s %s %d: ",
71 			     etheraddr_string(ESRC(ep)),
72 			     etheraddr_string(EDST(ep)),
73 			     etherproto_string(ep->ether_type),
74 			     length);
75 	}
76 	return;
77 trunc:
78 	printf("[|ether] ");
79 }
80 
81 u_short extracted_ethertype;
82 
83 /*
84  * This is the top level routine of the printer.  'p' is the points
85  * to the ether header of the packet, 'tvp' is the timestamp,
86  * 'length' is the length of the packet off the wire, and 'caplen'
87  * is the number of bytes actually captured.
88  */
89 void
90 ether_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
91 {
92 	ts_print(&h->ts);
93 
94 	/*
95 	 * Some printers want to get back at the ethernet addresses,
96 	 * and/or check that they're not walking off the end of the packet.
97 	 * Rather than pass them all the way down, we set these globals.
98 	 */
99 	snapend = p + h->caplen;
100 
101 	ether_tryprint(p, h->len, 1);
102 }
103 
104 void
105 ether_tryprint(const u_char *p, u_int length, int first_header)
106 {
107 	struct ether_header *ep;
108 	u_int caplen = snapend - p;
109 	u_short ether_type;
110 
111 	if (caplen < sizeof(struct ether_header)) {
112 		printf("[|ether]");
113 		goto out;
114 	}
115 
116 	if (eflag)
117 		ether_print(p, length);
118 
119 	packetp = p;
120 	length -= sizeof(struct ether_header);
121 	caplen -= sizeof(struct ether_header);
122 	ep = (struct ether_header *)p;
123 	p += sizeof(struct ether_header);
124 
125 	ether_type = ntohs(ep->ether_type);
126 
127 	/*
128 	 * Is it (gag) an 802.3 encapsulation?
129 	 */
130 	extracted_ethertype = 0;
131 	if (ether_type <= ETHERMTU) {
132 		/* Try to print the LLC-layer header & higher layers */
133 		if (llc_print(p, length, caplen, ESRC(ep), EDST(ep)) == 0) {
134 			/* ether_type not known, print raw packet */
135 			if (!eflag)
136 				ether_print((u_char *)ep, length);
137 			if (extracted_ethertype) {
138 				printf("(LLC %s) ",
139 			       etherproto_string(htons(extracted_ethertype)));
140 			}
141 			if (!xflag && !qflag) {
142 				if (eflag)
143 					default_print(packetp,
144 					    snapend - packetp);
145 				else
146 					default_print(p, caplen);
147 			}
148 		}
149 	} else if (ether_encap_print(ether_type, p, length, caplen) == 0) {
150 		/* ether_type not known, print raw packet */
151 		if (!eflag)
152 			ether_print((u_char *)ep, length + sizeof(*ep));
153 		if (!xflag && !qflag) {
154 			if (eflag)
155 				default_print(packetp, snapend - packetp);
156 			else
157 				default_print(p, caplen);
158 		}
159 	}
160 	if (xflag && first_header) {
161 		if (eflag)
162 			default_print(packetp, snapend - packetp);
163 		else
164 			default_print(p, caplen);
165 	}
166  out:
167 	if (first_header)
168 		putchar('\n');
169 }
170 
171 /*
172  * Prints the packet encapsulated in an Ethernet data segment
173  * (or an equivalent encapsulation), given the Ethernet type code.
174  *
175  * Returns non-zero if it can do so, zero if the ethertype is unknown.
176  *
177  * Stuffs the ether type into a global for the benefit of lower layers
178  * that might want to know what it is.
179  */
180 
181 int
182 ether_encap_print(u_short ethertype, const u_char *p,
183     u_int length, u_int caplen)
184 {
185 	uint16_t vlan, pri, vid;
186 recurse:
187 	extracted_ethertype = ethertype;
188 
189 	switch (ethertype) {
190 
191 	case ETHERTYPE_IP:
192 		ip_print(p, length);
193 		return (1);
194 
195 #ifdef INET6
196 	case ETHERTYPE_IPV6:
197 		ip6_print(p, length);
198 		return (1);
199 #endif /*INET6*/
200 
201 	case ETHERTYPE_ARP:
202 	case ETHERTYPE_REVARP:
203 		arp_print(p, length, caplen);
204 		return (1);
205 
206 	case ETHERTYPE_DN:
207 		decnet_print(p, length, caplen);
208 		return (1);
209 
210 	case ETHERTYPE_ATALK:
211 		if (vflag)
212 			fputs("et1 ", stdout);
213 		atalk_print_llap(p, length);
214 		return (1);
215 
216 	case ETHERTYPE_AARP:
217 		aarp_print(p, length);
218 		return (1);
219 
220 	case ETHERTYPE_8021Q:
221 		printf("802.1Q ");
222 	case ETHERTYPE_QINQ:
223 		if (ethertype == ETHERTYPE_QINQ)
224 			printf("QinQ s");
225 
226 		/* XXX caplen check */
227 
228 		vlan = ntohs(*(unsigned short*)p);
229 		vid = vlan & 0xfff;
230 		pri = vlan >> 13;
231 		if (pri <= 1)
232 			pri = !pri;
233 
234 		printf("vid %d pri %d%s", vid, pri,
235 		    vlan & 0x1000 ? " cfi " : " ");
236 		ethertype = ntohs(*(unsigned short*)(p+2));
237 		p += 4;
238 		length -= 4;
239 		caplen -= 4;
240 		if (ethertype > ETHERMTU)
241 			goto recurse;
242 
243 		extracted_ethertype = 0;
244 
245 		if (llc_print(p, length, caplen, p-18, p-12) == 0) {
246 			/* ether_type not known, print raw packet */
247 			if (!eflag)
248 				ether_print(p-18, length+4);
249 			if (extracted_ethertype) {
250 				printf("(LLC %s) ",
251 				etherproto_string(htons(extracted_ethertype)));
252 			}
253 			if (!xflag && !qflag)
254 				default_print(p-18, caplen+4);
255 		}
256 		return (1);
257 
258 #ifdef PPP
259 	case ETHERTYPE_PPPOEDISC:
260 	case ETHERTYPE_PPPOE:
261 		pppoe_if_print(ethertype, p, length, caplen);
262 		return (1);
263 #endif
264 
265 	case ETHERTYPE_FLOWCONTROL:
266 		ether_macctl(p, length);
267 		return (1);
268 
269 	case ETHERTYPE_MPLS:
270 	case ETHERTYPE_MPLS_MCAST:
271 		mpls_print(p, length);
272 		return (1);
273 
274 	case ETHERTYPE_LLDP:
275 		lldp_print(p, length);
276 		return (1);
277 
278 	case ETHERTYPE_SLOW:
279 		slow_print(p, length);
280 		return (1);
281 
282 	case ETHERTYPE_LAT:
283 	case ETHERTYPE_SCA:
284 	case ETHERTYPE_MOPRC:
285 	case ETHERTYPE_MOPDL:
286 		/* default_print for now */
287 	default:
288 		return (0);
289 	}
290 }
291 
292 void
293 ether_macctl(const u_char *p, u_int length)
294 {
295 	printf("MACCTL");
296 
297 	if (length < 2)
298 		goto trunc;
299 	if (EXTRACT_16BITS(p) == 0x0001) {
300 		u_int plen;
301 
302 		printf(" PAUSE");
303 
304 		length -= 2;
305 		p += 2;
306 		if (length < 2)
307 			goto trunc;
308 		plen = 512 * EXTRACT_16BITS(p);
309 		printf(" quanta %u", plen);
310 	} else {
311 		printf(" unknown-opcode(0x%04x)", EXTRACT_16BITS(p));
312 	}
313 	return;
314 
315 trunc:
316 	printf("[|MACCTL]");
317 }
318