xref: /netbsd-src/external/bsd/tcpdump/dist/print-ether.c (revision f14316bcbc544b96a93e884bc5c2b15fd60e22ae)
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 #include <sys/cdefs.h>
22 #ifndef lint
23 #if 0
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 #else
27 __RCSID("$NetBSD: print-ether.c,v 1.4 2013/12/31 17:33:31 christos Exp $");
28 #endif
29 #endif
30 
31 #define NETDISSECT_REWORKED
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <tcpdump-stdinc.h>
37 
38 #include <stdio.h>
39 #include <pcap.h>
40 
41 #include "interface.h"
42 #include "extract.h"
43 #include "addrtoname.h"
44 #include "ethertype.h"
45 #include "ether.h"
46 
47 const struct tok ethertype_values[] = {
48     { ETHERTYPE_IP,		"IPv4" },
49     { ETHERTYPE_MPLS,		"MPLS unicast" },
50     { ETHERTYPE_MPLS_MULTI,	"MPLS multicast" },
51     { ETHERTYPE_IPV6,		"IPv6" },
52     { ETHERTYPE_8021Q,		"802.1Q" },
53     { ETHERTYPE_8021Q9100,	"802.1Q-9100" },
54     { ETHERTYPE_8021QinQ,	"802.1Q-QinQ" },
55     { ETHERTYPE_8021Q9200,	"802.1Q-9200" },
56     { ETHERTYPE_VMAN,		"VMAN" },
57     { ETHERTYPE_PUP,            "PUP" },
58     { ETHERTYPE_ARP,            "ARP"},
59     { ETHERTYPE_REVARP,         "Reverse ARP"},
60     { ETHERTYPE_NS,             "NS" },
61     { ETHERTYPE_SPRITE,         "Sprite" },
62     { ETHERTYPE_TRAIL,          "Trail" },
63     { ETHERTYPE_MOPDL,          "MOP DL" },
64     { ETHERTYPE_MOPRC,          "MOP RC" },
65     { ETHERTYPE_DN,             "DN" },
66     { ETHERTYPE_LAT,            "LAT" },
67     { ETHERTYPE_SCA,            "SCA" },
68     { ETHERTYPE_TEB,            "TEB" },
69     { ETHERTYPE_LANBRIDGE,      "Lanbridge" },
70     { ETHERTYPE_DECDNS,         "DEC DNS" },
71     { ETHERTYPE_DECDTS,         "DEC DTS" },
72     { ETHERTYPE_VEXP,           "VEXP" },
73     { ETHERTYPE_VPROD,          "VPROD" },
74     { ETHERTYPE_ATALK,          "Appletalk" },
75     { ETHERTYPE_AARP,           "Appletalk ARP" },
76     { ETHERTYPE_IPX,            "IPX" },
77     { ETHERTYPE_PPP,            "PPP" },
78     { ETHERTYPE_MPCP,           "MPCP" },
79     { ETHERTYPE_SLOW,           "Slow Protocols" },
80     { ETHERTYPE_PPPOED,         "PPPoE D" },
81     { ETHERTYPE_PPPOES,         "PPPoE S" },
82     { ETHERTYPE_EAPOL,          "EAPOL" },
83     { ETHERTYPE_RRCP,           "RRCP" },
84     { ETHERTYPE_MS_NLB_HB,      "MS NLB heartbeat" },
85     { ETHERTYPE_JUMBO,          "Jumbo" },
86     { ETHERTYPE_LOOPBACK,       "Loopback" },
87     { ETHERTYPE_ISO,            "OSI" },
88     { ETHERTYPE_GRE_ISO,        "GRE-OSI" },
89     { ETHERTYPE_CFM_OLD,        "CFM (old)" },
90     { ETHERTYPE_CFM,            "CFM" },
91     { ETHERTYPE_LLDP,           "LLDP" },
92     { ETHERTYPE_TIPC,           "TIPC"},
93     { ETHERTYPE_GEONET_OLD,     "GeoNet (old)"},
94     { ETHERTYPE_GEONET,         "GeoNet"},
95     { ETHERTYPE_CALM_FAST,      "CALM FAST"},
96     { 0, NULL}
97 };
98 
99 static inline void
100 ether_hdr_print(netdissect_options *ndo,
101                 const u_char *bp, u_int length)
102 {
103 	register const struct ether_header *ep;
104 	u_int16_t ether_type;
105 
106 	ep = (const struct ether_header *)bp;
107 
108 	(void)ND_PRINT((ndo, "%s > %s",
109 		     etheraddr_string(ESRC(ep)),
110 		     etheraddr_string(EDST(ep))));
111 
112 	ether_type = EXTRACT_16BITS(&ep->ether_type);
113 	if (!ndo->ndo_qflag) {
114 	        if (ether_type <= ETHERMTU)
115 		          (void)ND_PRINT((ndo, ", 802.3"));
116                 else
117 		          (void)ND_PRINT((ndo, ", ethertype %s (0x%04x)",
118 				       tok2str(ethertype_values,"Unknown", ether_type),
119                                        ether_type));
120         } else {
121                 if (ether_type <= ETHERMTU)
122                           (void)ND_PRINT((ndo, ", 802.3"));
123                 else
124                           (void)ND_PRINT((ndo, ", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", ether_type)));
125         }
126 
127 	(void)ND_PRINT((ndo, ", length %u: ", length));
128 }
129 
130 /*
131  * Print an Ethernet frame.
132  * This might be encapsulated within another frame; we might be passed
133  * a pointer to a function that can print header information for that
134  * frame's protocol, and an argument to pass to that function.
135  */
136 void
137 ether_print(netdissect_options *ndo,
138             const u_char *p, u_int length, u_int caplen,
139             void (*print_encap_header)(netdissect_options *ndo, const u_char *), const u_char *encap_header_arg)
140 {
141 	struct ether_header *ep;
142 	u_int orig_length;
143 	u_short ether_type;
144 	u_short extracted_ether_type;
145 
146 	if (caplen < ETHER_HDRLEN || length < ETHER_HDRLEN) {
147 		ND_PRINT((ndo, "[|ether]"));
148 		return;
149 	}
150 
151 	if (ndo->ndo_eflag) {
152 		if (print_encap_header != NULL)
153 			(*print_encap_header)(ndo, encap_header_arg);
154 		ether_hdr_print(ndo, p, length);
155 	}
156 	orig_length = length;
157 
158 	length -= ETHER_HDRLEN;
159 	caplen -= ETHER_HDRLEN;
160 	ep = (struct ether_header *)p;
161 	p += ETHER_HDRLEN;
162 
163 	ether_type = EXTRACT_16BITS(&ep->ether_type);
164 
165 recurse:
166 	/*
167 	 * Is it (gag) an 802.3 encapsulation?
168 	 */
169 	if (ether_type <= ETHERMTU) {
170 		/* Try to print the LLC-layer header & higher layers */
171 		if (llc_print(p, length, caplen, ESRC(ep), EDST(ep),
172 		    &extracted_ether_type) == 0) {
173 			/* ether_type not known, print raw packet */
174 			if (!ndo->ndo_eflag) {
175 				if (print_encap_header != NULL)
176 					(*print_encap_header)(ndo, encap_header_arg);
177 				ether_hdr_print(ndo, (u_char *)ep, orig_length);
178 			}
179 
180 			if (!ndo->ndo_suppress_default_print)
181 				ndo->ndo_default_print(ndo, p, caplen);
182 		}
183 	} else if (ether_type == ETHERTYPE_8021Q  ||
184                 ether_type == ETHERTYPE_8021Q9100 ||
185                 ether_type == ETHERTYPE_8021Q9200 ||
186                 ether_type == ETHERTYPE_8021QinQ) {
187 		/*
188 		 * Print VLAN information, and then go back and process
189 		 * the enclosed type field.
190 		 */
191 		if (caplen < 4 || length < 4) {
192 			ND_PRINT((ndo, "[|vlan]"));
193 			return;
194 		}
195 	        if (ndo->ndo_eflag) {
196 	        	u_int16_t tag = EXTRACT_16BITS(p);
197 
198 			ND_PRINT((ndo, "vlan %u, p %u%s, ",
199 			    tag & 0xfff,
200 			    tag >> 13,
201 			    (tag & 0x1000) ? ", CFI" : ""));
202 		}
203 
204 		ether_type = EXTRACT_16BITS(p + 2);
205 		if (ndo->ndo_eflag && ether_type > ETHERMTU)
206 			ND_PRINT((ndo, "ethertype %s, ", tok2str(ethertype_values,"0x%04x", ether_type)));
207 		p += 4;
208 		length -= 4;
209 		caplen -= 4;
210 		goto recurse;
211 	} else if (ether_type == ETHERTYPE_JUMBO) {
212 		/*
213 		 * Alteon jumbo frames.
214 		 * See
215 		 *
216 		 *	http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
217 		 *
218 		 * which indicates that, following the type field,
219 		 * there's an LLC header and payload.
220 		 */
221 		/* Try to print the LLC-layer header & higher layers */
222 		if (llc_print(p, length, caplen, ESRC(ep), EDST(ep),
223 		    &extracted_ether_type) == 0) {
224 			/* ether_type not known, print raw packet */
225 			if (!ndo->ndo_eflag) {
226 				if (print_encap_header != NULL)
227 					(*print_encap_header)(ndo, encap_header_arg);
228 				ether_hdr_print(ndo, (u_char *)ep, orig_length);
229 			}
230 
231 			if (!ndo->ndo_suppress_default_print)
232 				ndo->ndo_default_print(ndo, p, caplen);
233 		}
234 	} else {
235 		if (ethertype_print(ndo, ether_type, p, length, caplen) == 0) {
236 			/* ether_type not known, print raw packet */
237 			if (!ndo->ndo_eflag) {
238 				if (print_encap_header != NULL)
239 					(*print_encap_header)(ndo, encap_header_arg);
240 				ether_hdr_print(ndo, (u_char *)ep, orig_length);
241 			}
242 
243 			if (!ndo->ndo_suppress_default_print)
244 				ndo->ndo_default_print(ndo, p, caplen);
245 		}
246 	}
247 }
248 
249 /*
250  * This is the top level routine of the printer.  'p' points
251  * to the ether header of the packet, 'h->ts' is the timestamp,
252  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
253  * is the number of bytes actually captured.
254  */
255 u_int
256 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
257                const u_char *p)
258 {
259 	ether_print(ndo, p, h->len, h->caplen, NULL, NULL);
260 
261 	return (ETHER_HDRLEN);
262 }
263 
264 /*
265  * This is the top level routine of the printer.  'p' points
266  * to the ether header of the packet, 'h->ts' is the timestamp,
267  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
268  * is the number of bytes actually captured.
269  *
270  * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
271  * before the Ethernet header.
272  */
273 u_int
274 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
275                      const u_char *p)
276 {
277 	/*
278 	 * Fail if we don't have enough data for the Hilscher pseudo-header.
279 	 */
280 	if (h->len < 4 || h->caplen < 4) {
281 		printf("[|netanalyzer]");
282 		return (h->caplen);
283 	}
284 
285 	/* Skip the pseudo-header. */
286 	ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL);
287 
288 	return (4 + ETHER_HDRLEN);
289 }
290 
291 /*
292  * This is the top level routine of the printer.  'p' points
293  * to the ether header of the packet, 'h->ts' is the timestamp,
294  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
295  * is the number of bytes actually captured.
296  *
297  * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
298  * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
299  * before the Ethernet header.
300  */
301 u_int
302 netanalyzer_transparent_if_print(netdissect_options *ndo,
303                                  const struct pcap_pkthdr *h,
304                                  const u_char *p)
305 {
306 	/*
307 	 * Fail if we don't have enough data for the Hilscher pseudo-header,
308 	 * preamble, and SOF.
309 	 */
310 	if (h->len < 12 || h->caplen < 12) {
311 		printf("[|netanalyzer-transparent]");
312 		return (h->caplen);
313 	}
314 
315 	/* Skip the pseudo-header, preamble, and SOF. */
316 	ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL);
317 
318 	return (12 + ETHER_HDRLEN);
319 }
320 
321 /*
322  * Prints the packet payload, given an Ethernet type code for the payload's
323  * protocol.
324  *
325  * Returns non-zero if it can do so, zero if the ethertype is unknown.
326  */
327 
328 int
329 ethertype_print(netdissect_options *ndo,
330                 u_short ether_type, const u_char *p,
331                 u_int length, u_int caplen)
332 {
333 	switch (ether_type) {
334 
335 	case ETHERTYPE_IP:
336 	        ip_print(ndo, p, length);
337 		return (1);
338 
339 #ifdef INET6
340 	case ETHERTYPE_IPV6:
341 		ip6_print(ndo, p, length);
342 		return (1);
343 #endif /*INET6*/
344 
345 	case ETHERTYPE_ARP:
346 	case ETHERTYPE_REVARP:
347   	        arp_print(ndo, p, length, caplen);
348 		return (1);
349 
350 	case ETHERTYPE_DN:
351 		decnet_print(/*ndo,*/p, length, caplen);
352 		return (1);
353 
354 	case ETHERTYPE_ATALK:
355 		if (ndo->ndo_vflag)
356 			fputs("et1 ", stdout);
357 		atalk_print(/*ndo,*/p, length);
358 		return (1);
359 
360 	case ETHERTYPE_AARP:
361 		aarp_print(/*ndo,*/p, length);
362 		return (1);
363 
364 	case ETHERTYPE_IPX:
365 		ND_PRINT((ndo, "(NOV-ETHII) "));
366 		ipx_print(/*ndo,*/p, length);
367 		return (1);
368 
369         case ETHERTYPE_ISO:
370                 isoclns_print(/*ndo,*/p+1, length-1, length-1);
371                 return(1);
372 
373 	case ETHERTYPE_PPPOED:
374 	case ETHERTYPE_PPPOES:
375 	case ETHERTYPE_PPPOED2:
376 	case ETHERTYPE_PPPOES2:
377 		pppoe_print(/*ndo,*/p, length);
378 		return (1);
379 
380 	case ETHERTYPE_EAPOL:
381 	        eap_print(ndo, p, length);
382 		return (1);
383 
384 	case ETHERTYPE_RRCP:
385 	        rrcp_print(ndo, p - 14 , length + 14);
386 		return (1);
387 
388 	case ETHERTYPE_PPP:
389 		if (length) {
390 			printf(": ");
391 			ppp_print(/*ndo,*/p, length);
392 		}
393 		return (1);
394 
395 	case ETHERTYPE_MPCP:
396 	        mpcp_print(/*ndo,*/p, length);
397 		return (1);
398 
399 	case ETHERTYPE_SLOW:
400 	        slow_print(/*ndo,*/p, length);
401 		return (1);
402 
403 	case ETHERTYPE_CFM:
404 	case ETHERTYPE_CFM_OLD:
405 	        cfm_print(/*ndo,*/p, length);
406 		return (1);
407 
408 	case ETHERTYPE_LLDP:
409 	        lldp_print(/*ndo,*/p, length);
410 		return (1);
411 
412         case ETHERTYPE_LOOPBACK:
413                 return (1);
414 
415 	case ETHERTYPE_MPLS:
416 	case ETHERTYPE_MPLS_MULTI:
417 		mpls_print(/*ndo,*/p, length);
418 		return (1);
419 
420 	case ETHERTYPE_TIPC:
421 		tipc_print(ndo, p, length, caplen);
422 		return (1);
423 
424 	case ETHERTYPE_MS_NLB_HB:
425 		msnlb_print(ndo, p);
426 		return (1);
427 
428         case ETHERTYPE_GEONET_OLD:
429         case ETHERTYPE_GEONET:
430                 geonet_print(ndo, p-14, p, length);
431                 return (1);
432 
433         case ETHERTYPE_CALM_FAST:
434                 calm_fast_print(ndo, p-14, p, length);
435                 return (1);
436 
437 	case ETHERTYPE_LAT:
438 	case ETHERTYPE_SCA:
439 	case ETHERTYPE_MOPRC:
440 	case ETHERTYPE_MOPDL:
441 		/* default_print for now */
442 	default:
443 		return (0);
444 	}
445 }
446 
447 
448 /*
449  * Local Variables:
450  * c-style: whitesmith
451  * c-basic-offset: 8
452  * End:
453  */
454 
455