xref: /netbsd-src/external/bsd/tcpdump/dist/print-ether.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
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 
22 #include <sys/cdefs.h>
23 #ifndef lint
24 __RCSID("$NetBSD: print-ether.c,v 1.9 2017/09/08 14:01:13 christos Exp $");
25 #endif
26 
27 /* \summary: Ethernet printer */
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <netdissect-stdinc.h>
34 
35 #include "netdissect.h"
36 #include "extract.h"
37 #include "addrtoname.h"
38 #include "ethertype.h"
39 #include "ether.h"
40 
41 const struct tok ethertype_values[] = {
42     { ETHERTYPE_IP,		"IPv4" },
43     { ETHERTYPE_MPLS,		"MPLS unicast" },
44     { ETHERTYPE_MPLS_MULTI,	"MPLS multicast" },
45     { ETHERTYPE_IPV6,		"IPv6" },
46     { ETHERTYPE_8021Q,		"802.1Q" },
47     { ETHERTYPE_8021Q9100,	"802.1Q-9100" },
48     { ETHERTYPE_8021QinQ,	"802.1Q-QinQ" },
49     { ETHERTYPE_8021Q9200,	"802.1Q-9200" },
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_MS_NLB_HB,      "MS NLB heartbeat" },
79     { ETHERTYPE_JUMBO,          "Jumbo" },
80     { ETHERTYPE_LOOPBACK,       "Loopback" },
81     { ETHERTYPE_ISO,            "OSI" },
82     { ETHERTYPE_GRE_ISO,        "GRE-OSI" },
83     { ETHERTYPE_CFM_OLD,        "CFM (old)" },
84     { ETHERTYPE_CFM,            "CFM" },
85     { ETHERTYPE_IEEE1905_1,     "IEEE1905.1" },
86     { ETHERTYPE_LLDP,           "LLDP" },
87     { ETHERTYPE_TIPC,           "TIPC"},
88     { ETHERTYPE_GEONET_OLD,     "GeoNet (old)"},
89     { ETHERTYPE_GEONET,         "GeoNet"},
90     { ETHERTYPE_CALM_FAST,      "CALM FAST"},
91     { ETHERTYPE_AOE,            "AoE" },
92     { ETHERTYPE_MEDSA,          "MEDSA" },
93     { 0, NULL}
94 };
95 
96 static inline void
97 ether_hdr_print(netdissect_options *ndo,
98                 const u_char *bp, u_int length)
99 {
100 	register const struct ether_header *ep;
101 	uint16_t length_type;
102 
103 	ep = (const struct ether_header *)bp;
104 
105 	ND_PRINT((ndo, "%s > %s",
106 		     etheraddr_string(ndo, ESRC(ep)),
107 		     etheraddr_string(ndo, EDST(ep))));
108 
109 	length_type = EXTRACT_16BITS(&ep->ether_length_type);
110 	if (!ndo->ndo_qflag) {
111 	        if (length_type <= ETHERMTU) {
112 		        ND_PRINT((ndo, ", 802.3"));
113 			length = length_type;
114 		} else
115 		        ND_PRINT((ndo, ", ethertype %s (0x%04x)",
116 				       tok2str(ethertype_values,"Unknown", length_type),
117                                        length_type));
118         } else {
119                 if (length_type <= ETHERMTU) {
120                         ND_PRINT((ndo, ", 802.3"));
121 			length = length_type;
122 		} else
123                         ND_PRINT((ndo, ", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", length_type)));
124         }
125 
126 	ND_PRINT((ndo, ", length %u: ", length));
127 }
128 
129 /*
130  * Print an Ethernet frame.
131  * This might be encapsulated within another frame; we might be passed
132  * a pointer to a function that can print header information for that
133  * frame's protocol, and an argument to pass to that function.
134  *
135  * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
136  */
137 u_int
138 ether_print(netdissect_options *ndo,
139             const u_char *p, u_int length, u_int caplen,
140             void (*print_encap_header)(netdissect_options *ndo, const u_char *), const u_char *encap_header_arg)
141 {
142 	const struct ether_header *ep;
143 	u_int orig_length;
144 	u_short length_type;
145 	u_int hdrlen;
146 	int llc_hdrlen;
147 	struct lladdr_info src, dst;
148 
149 	if (caplen < ETHER_HDRLEN) {
150 		ND_PRINT((ndo, "[|ether]"));
151 		return (caplen);
152 	}
153 	if (length < ETHER_HDRLEN) {
154 		ND_PRINT((ndo, "[|ether]"));
155 		return (length);
156 	}
157 
158 	if (ndo->ndo_eflag) {
159 		if (print_encap_header != NULL)
160 			(*print_encap_header)(ndo, encap_header_arg);
161 		ether_hdr_print(ndo, p, length);
162 	}
163 	orig_length = length;
164 
165 	length -= ETHER_HDRLEN;
166 	caplen -= ETHER_HDRLEN;
167 	ep = (const struct ether_header *)p;
168 	p += ETHER_HDRLEN;
169 	hdrlen = ETHER_HDRLEN;
170 
171 	src.addr = ESRC(ep);
172 	src.addr_string = etheraddr_string;
173 	dst.addr = EDST(ep);
174 	dst.addr_string = etheraddr_string;
175 	length_type = EXTRACT_16BITS(&ep->ether_length_type);
176 
177 recurse:
178 	/*
179 	 * Is it (gag) an 802.3 encapsulation?
180 	 */
181 	if (length_type <= ETHERMTU) {
182 		/* Try to print the LLC-layer header & higher layers */
183 		llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
184 		if (llc_hdrlen < 0) {
185 			/* packet type not known, print raw packet */
186 			if (!ndo->ndo_suppress_default_print)
187 				ND_DEFAULTPRINT(p, caplen);
188 			llc_hdrlen = -llc_hdrlen;
189 		}
190 		hdrlen += llc_hdrlen;
191 	} else if (length_type == ETHERTYPE_8021Q  ||
192                 length_type == ETHERTYPE_8021Q9100 ||
193                 length_type == ETHERTYPE_8021Q9200 ||
194                 length_type == ETHERTYPE_8021QinQ) {
195 		/*
196 		 * Print VLAN information, and then go back and process
197 		 * the enclosed type field.
198 		 */
199 		if (caplen < 4) {
200 			ND_PRINT((ndo, "[|vlan]"));
201 			return (hdrlen + caplen);
202 		}
203 		if (length < 4) {
204 			ND_PRINT((ndo, "[|vlan]"));
205 			return (hdrlen + length);
206 		}
207 	        if (ndo->ndo_eflag) {
208 			uint16_t tag = EXTRACT_16BITS(p);
209 
210 			ND_PRINT((ndo, "%s, ", ieee8021q_tci_string(tag)));
211 		}
212 
213 		length_type = EXTRACT_16BITS(p + 2);
214 		if (ndo->ndo_eflag && length_type > ETHERMTU)
215 			ND_PRINT((ndo, "ethertype %s, ", tok2str(ethertype_values,"0x%04x", length_type)));
216 		p += 4;
217 		length -= 4;
218 		caplen -= 4;
219 		hdrlen += 4;
220 		goto recurse;
221 	} else if (length_type == ETHERTYPE_JUMBO) {
222 		/*
223 		 * Alteon jumbo frames.
224 		 * See
225 		 *
226 		 *	http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
227 		 *
228 		 * which indicates that, following the type field,
229 		 * there's an LLC header and payload.
230 		 */
231 		/* Try to print the LLC-layer header & higher layers */
232 		llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
233 		if (llc_hdrlen < 0) {
234 			/* packet type not known, print raw packet */
235 			if (!ndo->ndo_suppress_default_print)
236 				ND_DEFAULTPRINT(p, caplen);
237 			llc_hdrlen = -llc_hdrlen;
238 		}
239 		hdrlen += llc_hdrlen;
240 	} else {
241 		if (ethertype_print(ndo, length_type, p, length, caplen, &src, &dst) == 0) {
242 			/* type not known, print raw packet */
243 			if (!ndo->ndo_eflag) {
244 				if (print_encap_header != NULL)
245 					(*print_encap_header)(ndo, encap_header_arg);
246 				ether_hdr_print(ndo, (const u_char *)ep, orig_length);
247 			}
248 
249 			if (!ndo->ndo_suppress_default_print)
250 				ND_DEFAULTPRINT(p, caplen);
251 		}
252 	}
253 	return (hdrlen);
254 }
255 
256 /*
257  * This is the top level routine of the printer.  'p' points
258  * to the ether header of the packet, 'h->len' is the length
259  * of the packet off the wire, and 'h->caplen' is the number
260  * of bytes actually captured.
261  */
262 u_int
263 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
264                const u_char *p)
265 {
266 	return (ether_print(ndo, p, h->len, h->caplen, NULL, NULL));
267 }
268 
269 /*
270  * This is the top level routine of the printer.  'p' points
271  * to the ether header of the packet, 'h->len' is the length
272  * of the packet off the wire, and 'h->caplen' is the number
273  * of bytes actually captured.
274  *
275  * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
276  * before the Ethernet header.
277  */
278 u_int
279 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
280                      const u_char *p)
281 {
282 	/*
283 	 * Fail if we don't have enough data for the Hilscher pseudo-header.
284 	 */
285 	if (h->len < 4 || h->caplen < 4) {
286 		ND_PRINT((ndo, "[|netanalyzer]"));
287 		return (h->caplen);
288 	}
289 
290 	/* Skip the pseudo-header. */
291 	return (4 + ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL));
292 }
293 
294 /*
295  * This is the top level routine of the printer.  'p' points
296  * to the ether header of the packet, 'h->len' is the length
297  * of the packet off the wire, and 'h->caplen' is the number
298  * of bytes actually captured.
299  *
300  * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
301  * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
302  * before the Ethernet header.
303  */
304 u_int
305 netanalyzer_transparent_if_print(netdissect_options *ndo,
306                                  const struct pcap_pkthdr *h,
307                                  const u_char *p)
308 {
309 	/*
310 	 * Fail if we don't have enough data for the Hilscher pseudo-header,
311 	 * preamble, and SOF.
312 	 */
313 	if (h->len < 12 || h->caplen < 12) {
314 		ND_PRINT((ndo, "[|netanalyzer-transparent]"));
315 		return (h->caplen);
316 	}
317 
318 	/* Skip the pseudo-header, preamble, and SOF. */
319 	return (12 + ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL));
320 }
321 
322 /*
323  * Prints the packet payload, given an Ethernet type code for the payload's
324  * protocol.
325  *
326  * Returns non-zero if it can do so, zero if the ethertype is unknown.
327  */
328 
329 int
330 ethertype_print(netdissect_options *ndo,
331                 u_short ether_type, const u_char *p,
332                 u_int length, u_int caplen,
333                 const struct lladdr_info *src, const struct lladdr_info *dst)
334 {
335 	switch (ether_type) {
336 
337 	case ETHERTYPE_IP:
338 	        ip_print(ndo, p, length);
339 		return (1);
340 
341 	case ETHERTYPE_IPV6:
342 		ip6_print(ndo, p, length);
343 		return (1);
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 			ND_PRINT((ndo, "et1 "));
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 		if (length == 0 || caplen == 0) {
371 			ND_PRINT((ndo, " [|osi]"));
372 			return (1);
373 		}
374 		isoclns_print(ndo, p + 1, length - 1);
375 		return(1);
376 
377 	case ETHERTYPE_PPPOED:
378 	case ETHERTYPE_PPPOES:
379 	case ETHERTYPE_PPPOED2:
380 	case ETHERTYPE_PPPOES2:
381 		pppoe_print(ndo, p, length);
382 		return (1);
383 
384 	case ETHERTYPE_EAPOL:
385 	        eap_print(ndo, p, length);
386 		return (1);
387 
388 	case ETHERTYPE_RRCP:
389 	        rrcp_print(ndo, p, length, src, dst);
390 		return (1);
391 
392 	case ETHERTYPE_PPP:
393 		if (length) {
394 			ND_PRINT((ndo, ": "));
395 			ppp_print(ndo, p, length);
396 		}
397 		return (1);
398 
399 	case ETHERTYPE_MPCP:
400 	        mpcp_print(ndo, p, length);
401 		return (1);
402 
403 	case ETHERTYPE_SLOW:
404 	        slow_print(ndo, p, length);
405 		return (1);
406 
407 	case ETHERTYPE_CFM:
408 	case ETHERTYPE_CFM_OLD:
409 		cfm_print(ndo, p, length);
410 		return (1);
411 
412 	case ETHERTYPE_LLDP:
413 		lldp_print(ndo, p, length);
414 		return (1);
415 
416         case ETHERTYPE_LOOPBACK:
417 		loopback_print(ndo, p, length);
418                 return (1);
419 
420 	case ETHERTYPE_MPLS:
421 	case ETHERTYPE_MPLS_MULTI:
422 		mpls_print(ndo, p, length);
423 		return (1);
424 
425 	case ETHERTYPE_TIPC:
426 		tipc_print(ndo, p, length, caplen);
427 		return (1);
428 
429 	case ETHERTYPE_MS_NLB_HB:
430 		msnlb_print(ndo, p);
431 		return (1);
432 
433         case ETHERTYPE_GEONET_OLD:
434         case ETHERTYPE_GEONET:
435                 geonet_print(ndo, p, length, src);
436                 return (1);
437 
438         case ETHERTYPE_CALM_FAST:
439                 calm_fast_print(ndo, p, length, src);
440                 return (1);
441 
442 	case ETHERTYPE_AOE:
443 		aoe_print(ndo, p, length);
444 		return (1);
445 
446 	case ETHERTYPE_MEDSA:
447 		medsa_print(ndo, p, length, caplen, src, dst);
448 		return (1);
449 
450 	case ETHERTYPE_LAT:
451 	case ETHERTYPE_SCA:
452 	case ETHERTYPE_MOPRC:
453 	case ETHERTYPE_MOPDL:
454 	case ETHERTYPE_IEEE1905_1:
455 		/* default_print for now */
456 	default:
457 		return (0);
458 	}
459 }
460 
461 
462 /*
463  * Local Variables:
464  * c-style: whitesmith
465  * c-basic-offset: 8
466  * End:
467  */
468 
469