xref: /freebsd/contrib/tcpdump/print-ether.c (revision 71fe318b852b8dfb3e799cb12ef184750f7f8eac)
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * $FreeBSD$
22  */
23 #ifndef lint
24 static const char rcsid[] =
25     "@(#) $Header: /tcpdump/master/tcpdump/print-ether.c,v 1.65 2001/07/04 22:03:14 fenner Exp $ (LBL)";
26 #endif
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/socket.h>
35 
36 struct mbuf;
37 struct rtentry;
38 
39 #include <netinet/in.h>
40 
41 #include <stdio.h>
42 #include <pcap.h>
43 
44 #include "interface.h"
45 #include "addrtoname.h"
46 #include "ethertype.h"
47 
48 #include "ether.h"
49 
50 const u_char *packetp;
51 const u_char *snapend;
52 
53 static inline void
54 ether_print(register const u_char *bp, u_int length)
55 {
56 	register const struct ether_header *ep;
57 
58 	ep = (const struct ether_header *)bp;
59 	if (qflag)
60 		(void)printf("%s %s %d: ",
61 			     etheraddr_string(ESRC(ep)),
62 			     etheraddr_string(EDST(ep)),
63 			     length);
64 	else
65 		(void)printf("%s %s %s %d: ",
66 			     etheraddr_string(ESRC(ep)),
67 			     etheraddr_string(EDST(ep)),
68 			     etherproto_string(ep->ether_type),
69 			     length);
70 }
71 
72 /*
73  * This is the top level routine of the printer.  'p' is the points
74  * to the ether header of the packet, 'h->tv' is the timestamp,
75  * 'h->length' is the length of the packet off the wire, and 'h->caplen'
76  * is the number of bytes actually captured.
77  */
78 void
79 ether_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
80 {
81 	u_int caplen = h->caplen;
82 	u_int length = h->len;
83 	struct ether_header *ep;
84 	u_short ether_type;
85 	u_short extracted_ethertype;
86 
87 	++infodelay;
88 	ts_print(&h->ts);
89 
90 	if (caplen < ETHER_HDRLEN) {
91 		printf("[|ether]");
92 		goto out;
93 	}
94 
95 	if (eflag)
96 		ether_print(p, length);
97 
98 	/*
99 	 * Some printers want to get back at the ethernet addresses,
100 	 * and/or check that they're not walking off the end of the packet.
101 	 * Rather than pass them all the way down, we set these globals.
102 	 */
103 	packetp = p;
104 	snapend = p + caplen;
105 
106 	length -= ETHER_HDRLEN;
107 	caplen -= ETHER_HDRLEN;
108 	ep = (struct ether_header *)p;
109 	p += ETHER_HDRLEN;
110 
111 	ether_type = ntohs(ep->ether_type);
112 
113 	/*
114 	 * Is it (gag) an 802.3 encapsulation?
115 	 */
116 	extracted_ethertype = 0;
117 	if (ether_type <= ETHERMTU) {
118 		/* Try to print the LLC-layer header & higher layers */
119 		if (llc_print(p, length, caplen, ESRC(ep), EDST(ep),
120 		    &extracted_ethertype) == 0) {
121 			/* ether_type not known, print raw packet */
122 			if (!eflag)
123 				ether_print((u_char *)ep, length + ETHER_HDRLEN);
124 			if (extracted_ethertype) {
125 				printf("(LLC %s) ",
126 			       etherproto_string(htons(extracted_ethertype)));
127 			}
128 			if (!xflag && !qflag)
129 				default_print(p, caplen);
130 		}
131 	} else if (ether_encap_print(ether_type, p, length, caplen,
132 	    &extracted_ethertype) == 0) {
133 		/* ether_type not known, print raw packet */
134 		if (!eflag)
135 			ether_print((u_char *)ep, length + ETHER_HDRLEN);
136 		if (!xflag && !qflag)
137 			default_print(p, caplen);
138 	}
139 	if (xflag)
140 		default_print(p, caplen);
141  out:
142 	putchar('\n');
143 	--infodelay;
144 	if (infoprint)
145 		info(0);
146 }
147 
148 /*
149  * Prints the packet encapsulated in an Ethernet data segment
150  * (or an equivalent encapsulation), given the Ethernet type code.
151  *
152  * Returns non-zero if it can do so, zero if the ethertype is unknown.
153  *
154  * The Ethernet type code is passed through a pointer; if it was
155  * ETHERTYPE_8021Q, it gets updated to be the Ethernet type of
156  * the 802.1Q payload, for the benefit of lower layers that might
157  * want to know what it is.
158  */
159 
160 int
161 ether_encap_print(u_short ethertype, const u_char *p,
162     u_int length, u_int caplen, u_short *extracted_ethertype)
163 {
164  recurse:
165 	*extracted_ethertype = ethertype;
166 
167 	switch (ethertype) {
168 
169 	case ETHERTYPE_IP:
170 		ip_print(p, length);
171 		return (1);
172 
173 #ifdef INET6
174 	case ETHERTYPE_IPV6:
175 		ip6_print(p, length);
176 		return (1);
177 #endif /*INET6*/
178 
179 	case ETHERTYPE_ARP:
180 	case ETHERTYPE_REVARP:
181 		arp_print(p, length, caplen);
182 		return (1);
183 
184 	case ETHERTYPE_DN:
185 		decnet_print(p, length, caplen);
186 		return (1);
187 
188 	case ETHERTYPE_ATALK:
189 		if (vflag)
190 			fputs("et1 ", stdout);
191 		atalk_print(p, length);
192 		return (1);
193 
194 	case ETHERTYPE_AARP:
195 		aarp_print(p, length);
196 		return (1);
197 
198 	case ETHERTYPE_IPX:
199 		ipx_print(p, length);
200 		return (1);
201 
202 	case ETHERTYPE_8021Q:
203 		printf("802.1Q vlan#%d P%d%s ",
204 		       ntohs(*(u_int16_t *)p) & 0xfff,
205 		       ntohs(*(u_int16_t *)p) >> 13,
206 		       (ntohs(*(u_int16_t *)p) & 0x1000) ? " CFI" : "");
207 		ethertype = ntohs(*(u_int16_t *)(p + 2));
208 		p += 4;
209 		length -= 4;
210 		caplen -= 4;
211 		if (ethertype > ETHERMTU)
212 			goto recurse;
213 
214 		*extracted_ethertype = 0;
215 
216 		if (llc_print(p, length, caplen, p - 18, p - 12,
217 		    extracted_ethertype) == 0) {
218 			/* ether_type not known, print raw packet */
219 			if (!eflag)
220 				ether_print(p - 18, length + 4);
221 			if (*extracted_ethertype) {
222 				printf("(LLC %s) ",
223 			       etherproto_string(htons(*extracted_ethertype)));
224 			}
225 			if (!xflag && !qflag)
226 				default_print(p - 18, caplen + 4);
227 		}
228 		return (1);
229 
230 	case ETHERTYPE_PPPOED:
231 	case ETHERTYPE_PPPOES:
232 	case ETHERTYPE_PPPOED2:
233 	case ETHERTYPE_PPPOES2:
234 		pppoe_print(p, length);
235  		return (1);
236 
237 	case ETHERTYPE_PPP:
238 		printf("ppp");
239 		if (length) {
240 			printf(": ");
241 			ppp_print(p, length);
242 		}
243 		return (1);
244 
245 	case ETHERTYPE_MPLS:
246 	case ETHERTYPE_MPLS_MULTI:
247 		mpls_print(p, length);
248 		return (1);
249 
250 	case ETHERTYPE_LAT:
251 	case ETHERTYPE_SCA:
252 	case ETHERTYPE_MOPRC:
253 	case ETHERTYPE_MOPDL:
254 		/* default_print for now */
255 	default:
256 		return (0);
257 	}
258 }
259