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