xref: /freebsd/contrib/tcpdump/print-ether.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
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[] _U_ =
25     "@(#) $Header: /tcpdump/master/tcpdump/print-ether.c,v 1.106 2008-02-06 10:47:53 guy Exp $ (LBL)";
26 #endif
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <tcpdump-stdinc.h>
33 
34 #include <stdio.h>
35 #include <pcap.h>
36 
37 #include "interface.h"
38 #include "extract.h"
39 #include "addrtoname.h"
40 #include "ethertype.h"
41 
42 #include "ether.h"
43 
44 const struct tok ethertype_values[] = {
45     { ETHERTYPE_IP,		"IPv4" },
46     { ETHERTYPE_MPLS,		"MPLS unicast" },
47     { ETHERTYPE_MPLS_MULTI,	"MPLS multicast" },
48     { ETHERTYPE_IPV6,		"IPv6" },
49     { ETHERTYPE_8021Q,		"802.1Q" },
50     { ETHERTYPE_VMAN,		"VMAN" },
51     { ETHERTYPE_PUP,            "PUP" },
52     { ETHERTYPE_ARP,            "ARP"},
53     { ETHERTYPE_REVARP,         "Reverse ARP"},
54     { ETHERTYPE_NS,             "NS" },
55     { ETHERTYPE_SPRITE,         "Sprite" },
56     { ETHERTYPE_TRAIL,          "Trail" },
57     { ETHERTYPE_MOPDL,          "MOP DL" },
58     { ETHERTYPE_MOPRC,          "MOP RC" },
59     { ETHERTYPE_DN,             "DN" },
60     { ETHERTYPE_LAT,            "LAT" },
61     { ETHERTYPE_SCA,            "SCA" },
62     { ETHERTYPE_TEB,            "TEB" },
63     { ETHERTYPE_LANBRIDGE,      "Lanbridge" },
64     { ETHERTYPE_DECDNS,         "DEC DNS" },
65     { ETHERTYPE_DECDTS,         "DEC DTS" },
66     { ETHERTYPE_VEXP,           "VEXP" },
67     { ETHERTYPE_VPROD,          "VPROD" },
68     { ETHERTYPE_ATALK,          "Appletalk" },
69     { ETHERTYPE_AARP,           "Appletalk ARP" },
70     { ETHERTYPE_IPX,            "IPX" },
71     { ETHERTYPE_PPP,            "PPP" },
72     { ETHERTYPE_MPCP,           "MPCP" },
73     { ETHERTYPE_SLOW,           "Slow Protocols" },
74     { ETHERTYPE_PPPOED,         "PPPoE D" },
75     { ETHERTYPE_PPPOES,         "PPPoE S" },
76     { ETHERTYPE_EAPOL,          "EAPOL" },
77     { ETHERTYPE_RRCP,           "RRCP" },
78     { ETHERTYPE_JUMBO,          "Jumbo" },
79     { ETHERTYPE_LOOPBACK,       "Loopback" },
80     { ETHERTYPE_ISO,            "OSI" },
81     { ETHERTYPE_GRE_ISO,        "GRE-OSI" },
82     { ETHERTYPE_CFM_OLD,        "CFM (old)" },
83     { ETHERTYPE_CFM,            "CFM" },
84     { ETHERTYPE_LLDP,           "LLDP" },
85     { 0, NULL}
86 };
87 
88 static inline void
89 ether_hdr_print(register const u_char *bp, u_int length)
90 {
91 	register const struct ether_header *ep;
92 	u_int16_t ether_type;
93 
94 	ep = (const struct ether_header *)bp;
95 
96 	(void)printf("%s > %s",
97 		     etheraddr_string(ESRC(ep)),
98 		     etheraddr_string(EDST(ep)));
99 
100 	ether_type = EXTRACT_16BITS(&ep->ether_type);
101 	if (!qflag) {
102 	        if (ether_type <= ETHERMTU)
103 		          (void)printf(", 802.3");
104                 else
105 		          (void)printf(", ethertype %s (0x%04x)",
106 				       tok2str(ethertype_values,"Unknown", ether_type),
107                                        ether_type);
108         } else {
109                 if (ether_type <= ETHERMTU)
110                           (void)printf(", 802.3");
111                 else
112                           (void)printf(", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", ether_type));
113         }
114 
115 	(void)printf(", length %u: ", length);
116 }
117 
118 /*
119  * Print an Ethernet frame.
120  * This might be encapsulated within another frame; we might be passed
121  * a pointer to a function that can print header information for that
122  * frame's protocol, and an argument to pass to that function.
123  */
124 void
125 ether_print(const u_char *p, u_int length, u_int caplen,
126     void (*print_encap_header)(const u_char *), const u_char *encap_header_arg)
127 {
128 	struct ether_header *ep;
129 	u_int orig_length;
130 	u_short ether_type;
131 	u_short extracted_ether_type;
132 
133 	if (caplen < ETHER_HDRLEN || length < ETHER_HDRLEN) {
134 		printf("[|ether]");
135 		return;
136 	}
137 
138 	if (eflag) {
139 		if (print_encap_header != NULL)
140 			(*print_encap_header)(encap_header_arg);
141 		ether_hdr_print(p, length);
142 	}
143 	orig_length = length;
144 
145 	length -= ETHER_HDRLEN;
146 	caplen -= ETHER_HDRLEN;
147 	ep = (struct ether_header *)p;
148 	p += ETHER_HDRLEN;
149 
150 	ether_type = EXTRACT_16BITS(&ep->ether_type);
151 
152 recurse:
153 	/*
154 	 * Is it (gag) an 802.3 encapsulation?
155 	 */
156 	if (ether_type <= ETHERMTU) {
157 		/* Try to print the LLC-layer header & higher layers */
158 		if (llc_print(p, length, caplen, ESRC(ep), EDST(ep),
159 		    &extracted_ether_type) == 0) {
160 			/* ether_type not known, print raw packet */
161 			if (!eflag) {
162 				if (print_encap_header != NULL)
163 					(*print_encap_header)(encap_header_arg);
164 				ether_hdr_print((u_char *)ep, orig_length);
165 			}
166 
167 			if (!suppress_default_print)
168 				default_print(p, caplen);
169 		}
170 	} else if (ether_type == ETHERTYPE_8021Q) {
171 		/*
172 		 * Print VLAN information, and then go back and process
173 		 * the enclosed type field.
174 		 */
175 		if (caplen < 4 || length < 4) {
176 			printf("[|vlan]");
177 			return;
178 		}
179 	        if (eflag) {
180 	        	u_int16_t tag = EXTRACT_16BITS(p);
181 
182 			printf("vlan %u, p %u%s, ",
183 			    tag & 0xfff,
184 			    tag >> 13,
185 			    (tag & 0x1000) ? ", CFI" : "");
186 		}
187 
188 		ether_type = EXTRACT_16BITS(p + 2);
189 		if (eflag && ether_type > ETHERMTU)
190 			printf("ethertype %s, ", tok2str(ethertype_values,"0x%04x", ether_type));
191 		p += 4;
192 		length -= 4;
193 		caplen -= 4;
194 		goto recurse;
195 	} else if (ether_type == ETHERTYPE_JUMBO) {
196 		/*
197 		 * Alteon jumbo frames.
198 		 * See
199 		 *
200 		 *	http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
201 		 *
202 		 * which indicates that, following the type field,
203 		 * there's an LLC header and payload.
204 		 */
205 		/* Try to print the LLC-layer header & higher layers */
206 		if (llc_print(p, length, caplen, ESRC(ep), EDST(ep),
207 		    &extracted_ether_type) == 0) {
208 			/* ether_type not known, print raw packet */
209 			if (!eflag) {
210 				if (print_encap_header != NULL)
211 					(*print_encap_header)(encap_header_arg);
212 				ether_hdr_print((u_char *)ep, orig_length);
213 			}
214 
215 			if (!suppress_default_print)
216 				default_print(p, caplen);
217 		}
218 	} else {
219 		if (ethertype_print(ether_type, p, length, caplen) == 0) {
220 			/* ether_type not known, print raw packet */
221 			if (!eflag) {
222 				if (print_encap_header != NULL)
223 					(*print_encap_header)(encap_header_arg);
224 				ether_hdr_print((u_char *)ep, orig_length);
225 			}
226 
227 			if (!suppress_default_print)
228 				default_print(p, caplen);
229 		}
230 	}
231 }
232 
233 /*
234  * This is the top level routine of the printer.  'p' points
235  * to the ether header of the packet, 'h->ts' is the timestamp,
236  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
237  * is the number of bytes actually captured.
238  */
239 u_int
240 ether_if_print(const struct pcap_pkthdr *h, const u_char *p)
241 {
242 	ether_print(p, h->len, h->caplen, NULL, NULL);
243 
244 	return (ETHER_HDRLEN);
245 }
246 
247 /*
248  * Prints the packet payload, given an Ethernet type code for the payload's
249  * protocol.
250  *
251  * Returns non-zero if it can do so, zero if the ethertype is unknown.
252  */
253 
254 int
255 ethertype_print(u_short ether_type, const u_char *p, u_int length, u_int caplen)
256 {
257 	switch (ether_type) {
258 
259 	case ETHERTYPE_IP:
260 	        ip_print(gndo, p, length);
261 		return (1);
262 
263 #ifdef INET6
264 	case ETHERTYPE_IPV6:
265 		ip6_print(p, length);
266 		return (1);
267 #endif /*INET6*/
268 
269 	case ETHERTYPE_ARP:
270 	case ETHERTYPE_REVARP:
271   	        arp_print(gndo, p, length, caplen);
272 		return (1);
273 
274 	case ETHERTYPE_DN:
275 		decnet_print(p, length, caplen);
276 		return (1);
277 
278 	case ETHERTYPE_ATALK:
279 		if (vflag)
280 			fputs("et1 ", stdout);
281 		atalk_print(p, length);
282 		return (1);
283 
284 	case ETHERTYPE_AARP:
285 		aarp_print(p, length);
286 		return (1);
287 
288 	case ETHERTYPE_IPX:
289 		printf("(NOV-ETHII) ");
290 		ipx_print(p, length);
291 		return (1);
292 
293         case ETHERTYPE_ISO:
294                 isoclns_print(p+1, length-1, length-1);
295                 return(1);
296 
297 	case ETHERTYPE_PPPOED:
298 	case ETHERTYPE_PPPOES:
299 	case ETHERTYPE_PPPOED2:
300 	case ETHERTYPE_PPPOES2:
301 		pppoe_print(p, length);
302 		return (1);
303 
304 	case ETHERTYPE_EAPOL:
305 	        eap_print(gndo, p, length);
306 		return (1);
307 
308 	case ETHERTYPE_RRCP:
309 	        rrcp_print(gndo, p - 14 , length + 14);
310 		return (1);
311 
312 	case ETHERTYPE_PPP:
313 		if (length) {
314 			printf(": ");
315 			ppp_print(p, length);
316 		}
317 		return (1);
318 
319 	case ETHERTYPE_MPCP:
320 	        mpcp_print(p, length);
321 		return (1);
322 
323 	case ETHERTYPE_SLOW:
324 	        slow_print(p, length);
325 		return (1);
326 
327 	case ETHERTYPE_CFM:
328 	case ETHERTYPE_CFM_OLD:
329 	        cfm_print(p, length);
330 		return (1);
331 
332 	case ETHERTYPE_LLDP:
333 	        lldp_print(p, length);
334 		return (1);
335 
336         case ETHERTYPE_LOOPBACK:
337                 return (1);
338 
339 	case ETHERTYPE_MPLS:
340 	case ETHERTYPE_MPLS_MULTI:
341 		mpls_print(p, length);
342 		return (1);
343 
344 	case ETHERTYPE_LAT:
345 	case ETHERTYPE_SCA:
346 	case ETHERTYPE_MOPRC:
347 	case ETHERTYPE_MOPDL:
348 		/* default_print for now */
349 	default:
350 		return (0);
351 	}
352 }
353 
354 
355 /*
356  * Local Variables:
357  * c-style: whitesmith
358  * c-basic-offset: 8
359  * End:
360  */
361 
362