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