xref: /freebsd/contrib/tcpdump/print-ether.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
14edb46e9SPaul Traina /*
2a1c2090eSBill Fenner  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
34edb46e9SPaul Traina  *	The Regents of the University of California.  All rights reserved.
44edb46e9SPaul Traina  *
54edb46e9SPaul Traina  * Redistribution and use in source and binary forms, with or without
64edb46e9SPaul Traina  * modification, are permitted provided that: (1) source code distributions
74edb46e9SPaul Traina  * retain the above copyright notice and this paragraph in its entirety, (2)
84edb46e9SPaul Traina  * distributions including binary code include the above copyright notice and
94edb46e9SPaul Traina  * this paragraph in its entirety in the documentation or other materials
104edb46e9SPaul Traina  * provided with the distribution, and (3) all advertising materials mentioning
114edb46e9SPaul Traina  * features or use of this software display the following acknowledgement:
124edb46e9SPaul Traina  * ``This product includes software developed by the University of California,
134edb46e9SPaul Traina  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
144edb46e9SPaul Traina  * the University nor the names of its contributors may be used to endorse
154edb46e9SPaul Traina  * or promote products derived from this software without specific prior
164edb46e9SPaul Traina  * written permission.
174edb46e9SPaul Traina  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
184edb46e9SPaul Traina  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
194edb46e9SPaul Traina  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
204edb46e9SPaul Traina  */
21a88113a8SBill Fenner 
223340d773SGleb Smirnoff /* \summary: Ethernet printer */
233340d773SGleb Smirnoff 
24ee67461eSJoseph Mingrone #include <config.h>
254edb46e9SPaul Traina 
26ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
274edb46e9SPaul Traina 
28ee67461eSJoseph Mingrone #define ND_LONGJMP_FROM_TCHECK
293340d773SGleb Smirnoff #include "netdissect.h"
3027df3f5dSRui Paulo #include "extract.h"
314edb46e9SPaul Traina #include "addrtoname.h"
324edb46e9SPaul Traina #include "ethertype.h"
33ee67461eSJoseph Mingrone 
34ee67461eSJoseph Mingrone /*
35ee67461eSJoseph Mingrone  * Structure of an Ethernet header.
36ee67461eSJoseph Mingrone  */
37ee67461eSJoseph Mingrone struct	ether_header {
38ee67461eSJoseph Mingrone 	nd_mac_addr	ether_dhost;
39ee67461eSJoseph Mingrone 	nd_mac_addr	ether_shost;
40ee67461eSJoseph Mingrone 	nd_uint16_t	ether_length_type;
41ee67461eSJoseph Mingrone };
42ee67461eSJoseph Mingrone 
43ee67461eSJoseph Mingrone /*
44ee67461eSJoseph Mingrone  * Length of an Ethernet header; note that some compilers may pad
45ee67461eSJoseph Mingrone  * "struct ether_header" to a multiple of 4 bytes, for example, so
46ee67461eSJoseph Mingrone  * "sizeof (struct ether_header)" may not give the right answer.
47ee67461eSJoseph Mingrone  */
48ee67461eSJoseph Mingrone #define ETHER_HDRLEN		14
494edb46e9SPaul Traina 
50cc391cceSBruce M Simpson const struct tok ethertype_values[] = {
51cc391cceSBruce M Simpson     { ETHERTYPE_IP,		"IPv4" },
52cc391cceSBruce M Simpson     { ETHERTYPE_MPLS,		"MPLS unicast" },
53cc391cceSBruce M Simpson     { ETHERTYPE_MPLS_MULTI,	"MPLS multicast" },
54cc391cceSBruce M Simpson     { ETHERTYPE_IPV6,		"IPv6" },
55cc391cceSBruce M Simpson     { ETHERTYPE_8021Q,		"802.1Q" },
56cac3dcd5SXin LI     { ETHERTYPE_8021Q9100,	"802.1Q-9100" },
57cac3dcd5SXin LI     { ETHERTYPE_8021QinQ,	"802.1Q-QinQ" },
58cac3dcd5SXin LI     { ETHERTYPE_8021Q9200,	"802.1Q-9200" },
59ee67461eSJoseph Mingrone     { ETHERTYPE_MACSEC,		"802.1AE MACsec" },
60cc391cceSBruce M Simpson     { ETHERTYPE_VMAN,		"VMAN" },
61cc391cceSBruce M Simpson     { ETHERTYPE_PUP,            "PUP" },
62cc391cceSBruce M Simpson     { ETHERTYPE_ARP,            "ARP"},
63cc391cceSBruce M Simpson     { ETHERTYPE_REVARP,         "Reverse ARP"},
64cc391cceSBruce M Simpson     { ETHERTYPE_NS,             "NS" },
65cc391cceSBruce M Simpson     { ETHERTYPE_SPRITE,         "Sprite" },
66cc391cceSBruce M Simpson     { ETHERTYPE_TRAIL,          "Trail" },
67cc391cceSBruce M Simpson     { ETHERTYPE_MOPDL,          "MOP DL" },
68cc391cceSBruce M Simpson     { ETHERTYPE_MOPRC,          "MOP RC" },
69cc391cceSBruce M Simpson     { ETHERTYPE_DN,             "DN" },
70cc391cceSBruce M Simpson     { ETHERTYPE_LAT,            "LAT" },
71cc391cceSBruce M Simpson     { ETHERTYPE_SCA,            "SCA" },
7227df3f5dSRui Paulo     { ETHERTYPE_TEB,            "TEB" },
73cc391cceSBruce M Simpson     { ETHERTYPE_LANBRIDGE,      "Lanbridge" },
74cc391cceSBruce M Simpson     { ETHERTYPE_DECDNS,         "DEC DNS" },
75cc391cceSBruce M Simpson     { ETHERTYPE_DECDTS,         "DEC DTS" },
76cc391cceSBruce M Simpson     { ETHERTYPE_VEXP,           "VEXP" },
77cc391cceSBruce M Simpson     { ETHERTYPE_VPROD,          "VPROD" },
78cc391cceSBruce M Simpson     { ETHERTYPE_ATALK,          "Appletalk" },
79cc391cceSBruce M Simpson     { ETHERTYPE_AARP,           "Appletalk ARP" },
80cc391cceSBruce M Simpson     { ETHERTYPE_IPX,            "IPX" },
81cc391cceSBruce M Simpson     { ETHERTYPE_PPP,            "PPP" },
82a5779b6eSRui Paulo     { ETHERTYPE_MPCP,           "MPCP" },
8317cb103cSSam Leffler     { ETHERTYPE_SLOW,           "Slow Protocols" },
84cc391cceSBruce M Simpson     { ETHERTYPE_PPPOED,         "PPPoE D" },
85cc391cceSBruce M Simpson     { ETHERTYPE_PPPOES,         "PPPoE S" },
86c1ad1296SSam Leffler     { ETHERTYPE_EAPOL,          "EAPOL" },
87ee67461eSJoseph Mingrone     { ETHERTYPE_REALTEK,        "Realtek protocols" },
88d03c0883SXin LI     { ETHERTYPE_MS_NLB_HB,      "MS NLB heartbeat" },
89c1ad1296SSam Leffler     { ETHERTYPE_JUMBO,          "Jumbo" },
90ee67461eSJoseph Mingrone     { ETHERTYPE_NSH,            "NSH" },
91cc391cceSBruce M Simpson     { ETHERTYPE_LOOPBACK,       "Loopback" },
92c1ad1296SSam Leffler     { ETHERTYPE_ISO,            "OSI" },
93c1ad1296SSam Leffler     { ETHERTYPE_GRE_ISO,        "GRE-OSI" },
94a5779b6eSRui Paulo     { ETHERTYPE_CFM_OLD,        "CFM (old)" },
95a5779b6eSRui Paulo     { ETHERTYPE_CFM,            "CFM" },
963c602fabSXin LI     { ETHERTYPE_IEEE1905_1,     "IEEE1905.1" },
97a5779b6eSRui Paulo     { ETHERTYPE_LLDP,           "LLDP" },
98d09a7e67SXin LI     { ETHERTYPE_TIPC,           "TIPC"},
993c602fabSXin LI     { ETHERTYPE_GEONET_OLD,     "GeoNet (old)"},
1003c602fabSXin LI     { ETHERTYPE_GEONET,         "GeoNet"},
1013c602fabSXin LI     { ETHERTYPE_CALM_FAST,      "CALM FAST"},
1023c602fabSXin LI     { ETHERTYPE_AOE,            "AoE" },
103ee67461eSJoseph Mingrone     { ETHERTYPE_PTP,            "PTP" },
104ee67461eSJoseph Mingrone     { ETHERTYPE_ARISTA,         "Arista Vendor Specific Protocol" },
105cc391cceSBruce M Simpson     { 0, NULL}
106cc391cceSBruce M Simpson };
107cc391cceSBruce M Simpson 
108ee67461eSJoseph Mingrone static void
ether_addresses_print(netdissect_options * ndo,const u_char * src,const u_char * dst)109ee67461eSJoseph Mingrone ether_addresses_print(netdissect_options *ndo, const u_char *src,
110ee67461eSJoseph Mingrone 		      const u_char *dst)
1114edb46e9SPaul Traina {
112ee67461eSJoseph Mingrone 	ND_PRINT("%s > %s, ",
113ee67461eSJoseph Mingrone 		 GET_ETHERADDR_STRING(src), GET_ETHERADDR_STRING(dst));
1144edb46e9SPaul Traina }
1154edb46e9SPaul Traina 
116ee67461eSJoseph Mingrone static void
ether_type_print(netdissect_options * ndo,uint16_t type)117ee67461eSJoseph Mingrone ether_type_print(netdissect_options *ndo, uint16_t type)
118ee67461eSJoseph Mingrone {
119ee67461eSJoseph Mingrone 	if (!ndo->ndo_qflag)
120ee67461eSJoseph Mingrone 		ND_PRINT("ethertype %s (0x%04x)",
121ee67461eSJoseph Mingrone 			 tok2str(ethertype_values, "Unknown", type), type);
122ee67461eSJoseph Mingrone 	else
123ee67461eSJoseph Mingrone 		ND_PRINT("%s",
124ee67461eSJoseph Mingrone 			 tok2str(ethertype_values, "Unknown Ethertype (0x%04x)", type));
125cc391cceSBruce M Simpson }
126cc391cceSBruce M Simpson 
12727df3f5dSRui Paulo /*
128ee67461eSJoseph Mingrone  * Common code for printing Ethernet frames.
1293340d773SGleb Smirnoff  *
130ee67461eSJoseph Mingrone  * It can handle Ethernet headers with extra tag information inserted
131ee67461eSJoseph Mingrone  * after the destination and source addresses, as is inserted by some
132ee67461eSJoseph Mingrone  * switch chips, and extra encapsulation header information before
133ee67461eSJoseph Mingrone  * printing Ethernet header information (such as a LANE ID for ATM LANE).
13427df3f5dSRui Paulo  */
135ee67461eSJoseph Mingrone static u_int
ether_common_print(netdissect_options * ndo,const u_char * p,u_int length,u_int caplen,void (* print_switch_tag)(netdissect_options * ndo,const u_char *),u_int switch_tag_len,void (* print_encap_header)(netdissect_options * ndo,const u_char *),const u_char * encap_header_arg)136ee67461eSJoseph Mingrone ether_common_print(netdissect_options *ndo, const u_char *p, u_int length,
137ee67461eSJoseph Mingrone     u_int caplen,
138ee67461eSJoseph Mingrone     void (*print_switch_tag)(netdissect_options *ndo, const u_char *),
139ee67461eSJoseph Mingrone     u_int switch_tag_len,
140ee67461eSJoseph Mingrone     void (*print_encap_header)(netdissect_options *ndo, const u_char *),
141ee67461eSJoseph Mingrone     const u_char *encap_header_arg)
1424edb46e9SPaul Traina {
143ee67461eSJoseph Mingrone 	const struct ether_header *ehp;
14427df3f5dSRui Paulo 	u_int orig_length;
1453340d773SGleb Smirnoff 	u_int hdrlen;
146ee67461eSJoseph Mingrone 	u_short length_type;
147ee67461eSJoseph Mingrone 	int printed_length;
1483340d773SGleb Smirnoff 	int llc_hdrlen;
1493340d773SGleb Smirnoff 	struct lladdr_info src, dst;
1504edb46e9SPaul Traina 
151ee67461eSJoseph Mingrone 	if (length < caplen) {
152ee67461eSJoseph Mingrone 		ND_PRINT("[length %u < caplen %u]", length, caplen);
153ee67461eSJoseph Mingrone 		nd_print_invalid(ndo);
154ee67461eSJoseph Mingrone 		return length;
1553340d773SGleb Smirnoff 	}
156ee67461eSJoseph Mingrone 	if (caplen < ETHER_HDRLEN + switch_tag_len) {
157ee67461eSJoseph Mingrone 		nd_print_trunc(ndo);
158ee67461eSJoseph Mingrone 		return caplen;
159ee67461eSJoseph Mingrone 	}
160ee67461eSJoseph Mingrone 
161ee67461eSJoseph Mingrone 	if (print_encap_header != NULL)
162ee67461eSJoseph Mingrone 		(*print_encap_header)(ndo, encap_header_arg);
163ee67461eSJoseph Mingrone 
164ee67461eSJoseph Mingrone 	orig_length = length;
165ee67461eSJoseph Mingrone 
166ee67461eSJoseph Mingrone 	/*
167ee67461eSJoseph Mingrone 	 * Get the source and destination addresses, skip past them,
168ee67461eSJoseph Mingrone 	 * and print them if we're printing the link-layer header.
169ee67461eSJoseph Mingrone 	 */
170ee67461eSJoseph Mingrone 	ehp = (const struct ether_header *)p;
171ee67461eSJoseph Mingrone 	src.addr = ehp->ether_shost;
172ee67461eSJoseph Mingrone 	src.addr_string = etheraddr_string;
173ee67461eSJoseph Mingrone 	dst.addr = ehp->ether_dhost;
174ee67461eSJoseph Mingrone 	dst.addr_string = etheraddr_string;
175ee67461eSJoseph Mingrone 
176ee67461eSJoseph Mingrone 	length -= 2*MAC_ADDR_LEN;
177ee67461eSJoseph Mingrone 	caplen -= 2*MAC_ADDR_LEN;
178ee67461eSJoseph Mingrone 	p += 2*MAC_ADDR_LEN;
179ee67461eSJoseph Mingrone 	hdrlen = 2*MAC_ADDR_LEN;
180ee67461eSJoseph Mingrone 
181ee67461eSJoseph Mingrone 	if (ndo->ndo_eflag)
182ee67461eSJoseph Mingrone 		ether_addresses_print(ndo, src.addr, dst.addr);
183ee67461eSJoseph Mingrone 
184ee67461eSJoseph Mingrone 	/*
185ee67461eSJoseph Mingrone 	 * Print the switch tag, if we have one, and skip past it.
186ee67461eSJoseph Mingrone 	 */
187ee67461eSJoseph Mingrone 	if (print_switch_tag != NULL)
188ee67461eSJoseph Mingrone 		(*print_switch_tag)(ndo, p);
189ee67461eSJoseph Mingrone 
190ee67461eSJoseph Mingrone 	length -= switch_tag_len;
191ee67461eSJoseph Mingrone 	caplen -= switch_tag_len;
192ee67461eSJoseph Mingrone 	p += switch_tag_len;
193ee67461eSJoseph Mingrone 	hdrlen += switch_tag_len;
194ee67461eSJoseph Mingrone 
195ee67461eSJoseph Mingrone 	/*
196ee67461eSJoseph Mingrone 	 * Get the length/type field, skip past it, and print it
197ee67461eSJoseph Mingrone 	 * if we're printing the link-layer header.
198ee67461eSJoseph Mingrone 	 */
199ee67461eSJoseph Mingrone recurse:
200ee67461eSJoseph Mingrone 	length_type = GET_BE_U_2(p);
201ee67461eSJoseph Mingrone 
202ee67461eSJoseph Mingrone 	length -= 2;
203ee67461eSJoseph Mingrone 	caplen -= 2;
204ee67461eSJoseph Mingrone 	p += 2;
205ee67461eSJoseph Mingrone 	hdrlen += 2;
206ee67461eSJoseph Mingrone 
207ee67461eSJoseph Mingrone 	/*
208ee67461eSJoseph Mingrone 	 * Process 802.1AE MACsec headers.
209ee67461eSJoseph Mingrone 	 */
210ee67461eSJoseph Mingrone 	printed_length = 0;
211ee67461eSJoseph Mingrone 	if (length_type == ETHERTYPE_MACSEC) {
212ee67461eSJoseph Mingrone 		/*
213ee67461eSJoseph Mingrone 		 * MACsec, aka IEEE 802.1AE-2006
214ee67461eSJoseph Mingrone 		 * Print the header, and try to print the payload if it's not encrypted
215ee67461eSJoseph Mingrone 		 */
216ee67461eSJoseph Mingrone 		if (ndo->ndo_eflag) {
217ee67461eSJoseph Mingrone 			ether_type_print(ndo, length_type);
218ee67461eSJoseph Mingrone 			ND_PRINT(", length %u: ", orig_length);
219ee67461eSJoseph Mingrone 			printed_length = 1;
220ee67461eSJoseph Mingrone 		}
221ee67461eSJoseph Mingrone 
222ee67461eSJoseph Mingrone 		int ret = macsec_print(ndo, &p, &length, &caplen, &hdrlen,
223ee67461eSJoseph Mingrone 				       &src, &dst);
224ee67461eSJoseph Mingrone 
225ee67461eSJoseph Mingrone 		if (ret == 0) {
226ee67461eSJoseph Mingrone 			/* Payload is encrypted; print it as raw data. */
227ee67461eSJoseph Mingrone 			if (!ndo->ndo_suppress_default_print)
228ee67461eSJoseph Mingrone 				ND_DEFAULTPRINT(p, caplen);
229ee67461eSJoseph Mingrone 			return hdrlen;
230ee67461eSJoseph Mingrone 		} else if (ret > 0) {
231ee67461eSJoseph Mingrone 			/* Problem printing the header; just quit. */
232ee67461eSJoseph Mingrone 			return ret;
233ee67461eSJoseph Mingrone 		} else {
234ee67461eSJoseph Mingrone 			/*
235ee67461eSJoseph Mingrone 			 * Keep processing type/length fields.
236ee67461eSJoseph Mingrone 			 */
237ee67461eSJoseph Mingrone 			length_type = GET_BE_U_2(p);
238ee67461eSJoseph Mingrone 
239*0a7e5f1fSJoseph Mingrone 			ND_ICHECK_U(caplen, <, 2);
240ee67461eSJoseph Mingrone 			length -= 2;
241ee67461eSJoseph Mingrone 			caplen -= 2;
242ee67461eSJoseph Mingrone 			p += 2;
243ee67461eSJoseph Mingrone 			hdrlen += 2;
244ee67461eSJoseph Mingrone 		}
245ee67461eSJoseph Mingrone 	}
246ee67461eSJoseph Mingrone 
247ee67461eSJoseph Mingrone 	/*
248ee67461eSJoseph Mingrone 	 * Process VLAN tag types.
249ee67461eSJoseph Mingrone 	 */
250ee67461eSJoseph Mingrone 	while (length_type == ETHERTYPE_8021Q  ||
251ee67461eSJoseph Mingrone 		length_type == ETHERTYPE_8021Q9100 ||
252ee67461eSJoseph Mingrone 		length_type == ETHERTYPE_8021Q9200 ||
253ee67461eSJoseph Mingrone 		length_type == ETHERTYPE_8021QinQ) {
254ee67461eSJoseph Mingrone 		/*
255ee67461eSJoseph Mingrone 		 * It has a VLAN tag.
256ee67461eSJoseph Mingrone 		 * Print VLAN information, and then go back and process
257ee67461eSJoseph Mingrone 		 * the enclosed type field.
258ee67461eSJoseph Mingrone 		 */
259ee67461eSJoseph Mingrone 		if (caplen < 4) {
260ee67461eSJoseph Mingrone 			ndo->ndo_protocol = "vlan";
261ee67461eSJoseph Mingrone 			nd_print_trunc(ndo);
262ee67461eSJoseph Mingrone 			return hdrlen + caplen;
263ee67461eSJoseph Mingrone 		}
264ee67461eSJoseph Mingrone 		if (length < 4) {
265ee67461eSJoseph Mingrone 			ndo->ndo_protocol = "vlan";
266ee67461eSJoseph Mingrone 			nd_print_trunc(ndo);
267ee67461eSJoseph Mingrone 			return hdrlen + length;
268ee67461eSJoseph Mingrone 		}
269ee67461eSJoseph Mingrone 		if (ndo->ndo_eflag) {
270ee67461eSJoseph Mingrone 			uint16_t tag = GET_BE_U_2(p);
271ee67461eSJoseph Mingrone 
272ee67461eSJoseph Mingrone 			ether_type_print(ndo, length_type);
273ee67461eSJoseph Mingrone 			if (!printed_length) {
274ee67461eSJoseph Mingrone 				ND_PRINT(", length %u: ", orig_length);
275ee67461eSJoseph Mingrone 				printed_length = 1;
276ee67461eSJoseph Mingrone 			} else
277ee67461eSJoseph Mingrone 				ND_PRINT(", ");
278ee67461eSJoseph Mingrone 			ND_PRINT("%s, ", ieee8021q_tci_string(tag));
279ee67461eSJoseph Mingrone 		}
280ee67461eSJoseph Mingrone 
281ee67461eSJoseph Mingrone 		length_type = GET_BE_U_2(p + 2);
282ee67461eSJoseph Mingrone 		p += 4;
283ee67461eSJoseph Mingrone 		length -= 4;
284ee67461eSJoseph Mingrone 		caplen -= 4;
285ee67461eSJoseph Mingrone 		hdrlen += 4;
286ee67461eSJoseph Mingrone 	}
287ee67461eSJoseph Mingrone 
288ee67461eSJoseph Mingrone 	/*
289ee67461eSJoseph Mingrone 	 * We now have the final length/type field.
290ee67461eSJoseph Mingrone 	 */
291ee67461eSJoseph Mingrone 	if (length_type <= MAX_ETHERNET_LENGTH_VAL) {
292ee67461eSJoseph Mingrone 		/*
293ee67461eSJoseph Mingrone 		 * It's a length field, containing the length of the
294ee67461eSJoseph Mingrone 		 * remaining payload; use it as such, as long as
295ee67461eSJoseph Mingrone 		 * it's not too large (bigger than the actual payload).
296ee67461eSJoseph Mingrone 		 */
297ee67461eSJoseph Mingrone 		if (length_type < length) {
298ee67461eSJoseph Mingrone 			length = length_type;
299ee67461eSJoseph Mingrone 			if (caplen > length)
300ee67461eSJoseph Mingrone 				caplen = length;
301ee67461eSJoseph Mingrone 		}
302ee67461eSJoseph Mingrone 
303ee67461eSJoseph Mingrone 		/*
304ee67461eSJoseph Mingrone 		 * Cut off the snapshot length to the end of the
305ee67461eSJoseph Mingrone 		 * payload.
306ee67461eSJoseph Mingrone 		 */
307ee67461eSJoseph Mingrone 		if (!nd_push_snaplen(ndo, p, length)) {
308ee67461eSJoseph Mingrone 			(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
309ee67461eSJoseph Mingrone 				"%s: can't push snaplen on buffer stack", __func__);
3104edb46e9SPaul Traina 		}
3114edb46e9SPaul Traina 
312cac3dcd5SXin LI 		if (ndo->ndo_eflag) {
313ee67461eSJoseph Mingrone 			ND_PRINT("802.3");
314ee67461eSJoseph Mingrone 			if (!printed_length)
315ee67461eSJoseph Mingrone 				ND_PRINT(", length %u: ", length);
31627df3f5dSRui Paulo 		}
3174edb46e9SPaul Traina 
3184edb46e9SPaul Traina 		/*
319ee67461eSJoseph Mingrone 		 * An LLC header follows the length.  Print that and
320ee67461eSJoseph Mingrone 		 * higher layers.
3214edb46e9SPaul Traina 		 */
3223340d773SGleb Smirnoff 		llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
3233340d773SGleb Smirnoff 		if (llc_hdrlen < 0) {
3243340d773SGleb Smirnoff 			/* packet type not known, print raw packet */
325cac3dcd5SXin LI 			if (!ndo->ndo_suppress_default_print)
3263c602fabSXin LI 				ND_DEFAULTPRINT(p, caplen);
3273340d773SGleb Smirnoff 			llc_hdrlen = -llc_hdrlen;
3284edb46e9SPaul Traina 		}
3293340d773SGleb Smirnoff 		hdrlen += llc_hdrlen;
330ee67461eSJoseph Mingrone 		nd_pop_packet_info(ndo);
3313340d773SGleb Smirnoff 	} else if (length_type == ETHERTYPE_JUMBO) {
33227df3f5dSRui Paulo 		/*
333ee67461eSJoseph Mingrone 		 * It's a type field, with the type for Alteon jumbo frames.
33427df3f5dSRui Paulo 		 * See
33527df3f5dSRui Paulo 		 *
336ee67461eSJoseph Mingrone 		 *	https://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
33727df3f5dSRui Paulo 		 *
33827df3f5dSRui Paulo 		 * which indicates that, following the type field,
33927df3f5dSRui Paulo 		 * there's an LLC header and payload.
34027df3f5dSRui Paulo 		 */
34127df3f5dSRui Paulo 		/* Try to print the LLC-layer header & higher layers */
3423340d773SGleb Smirnoff 		llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
3433340d773SGleb Smirnoff 		if (llc_hdrlen < 0) {
3443340d773SGleb Smirnoff 			/* packet type not known, print raw packet */
345cac3dcd5SXin LI 			if (!ndo->ndo_suppress_default_print)
3463c602fabSXin LI 				ND_DEFAULTPRINT(p, caplen);
3473340d773SGleb Smirnoff 			llc_hdrlen = -llc_hdrlen;
3484edb46e9SPaul Traina 		}
3493340d773SGleb Smirnoff 		hdrlen += llc_hdrlen;
350ee67461eSJoseph Mingrone 	} else if (length_type == ETHERTYPE_ARISTA) {
351ee67461eSJoseph Mingrone 		if (caplen < 2) {
352ee67461eSJoseph Mingrone 			ND_PRINT("[|arista]");
353ee67461eSJoseph Mingrone 			return hdrlen + caplen;
354ee67461eSJoseph Mingrone 		}
355ee67461eSJoseph Mingrone 		if (length < 2) {
356ee67461eSJoseph Mingrone 			ND_PRINT("[|arista]");
357ee67461eSJoseph Mingrone 			return hdrlen + length;
358ee67461eSJoseph Mingrone 		}
359ee67461eSJoseph Mingrone 		ether_type_print(ndo, length_type);
360ee67461eSJoseph Mingrone 		ND_PRINT(", length %u: ", orig_length);
361ee67461eSJoseph Mingrone 		int bytesConsumed = arista_ethertype_print(ndo, p, length);
362ee67461eSJoseph Mingrone 		if (bytesConsumed > 0) {
363ee67461eSJoseph Mingrone 			p += bytesConsumed;
364ee67461eSJoseph Mingrone 			length -= bytesConsumed;
365ee67461eSJoseph Mingrone 			caplen -= bytesConsumed;
366ee67461eSJoseph Mingrone 			hdrlen += bytesConsumed;
367ee67461eSJoseph Mingrone 			goto recurse;
36827df3f5dSRui Paulo 		} else {
369ee67461eSJoseph Mingrone 			/* subtype/version not known, print raw packet */
370ee67461eSJoseph Mingrone 			if (!ndo->ndo_eflag && length_type > MAX_ETHERNET_LENGTH_VAL) {
371ee67461eSJoseph Mingrone 				ether_addresses_print(ndo, src.addr, dst.addr);
372ee67461eSJoseph Mingrone 				ether_type_print(ndo, length_type);
373ee67461eSJoseph Mingrone 				ND_PRINT(", length %u: ", orig_length);
374ee67461eSJoseph Mingrone 			}
375ee67461eSJoseph Mingrone 			 if (!ndo->ndo_suppress_default_print)
376ee67461eSJoseph Mingrone 				 ND_DEFAULTPRINT(p, caplen);
377ee67461eSJoseph Mingrone 		}
378ee67461eSJoseph Mingrone 	} else {
379ee67461eSJoseph Mingrone 		/*
380ee67461eSJoseph Mingrone 		 * It's a type field with some other value.
381ee67461eSJoseph Mingrone 		 */
382ee67461eSJoseph Mingrone 		if (ndo->ndo_eflag) {
383ee67461eSJoseph Mingrone 			ether_type_print(ndo, length_type);
384ee67461eSJoseph Mingrone 			if (!printed_length)
385ee67461eSJoseph Mingrone 				ND_PRINT(", length %u: ", orig_length);
386ee67461eSJoseph Mingrone 			else
387ee67461eSJoseph Mingrone 				ND_PRINT(", ");
388ee67461eSJoseph Mingrone 		}
3893340d773SGleb Smirnoff 		if (ethertype_print(ndo, length_type, p, length, caplen, &src, &dst) == 0) {
3903340d773SGleb Smirnoff 			/* type not known, print raw packet */
391cac3dcd5SXin LI 			if (!ndo->ndo_eflag) {
392ee67461eSJoseph Mingrone 				/*
393ee67461eSJoseph Mingrone 				 * We didn't print the full link-layer
394ee67461eSJoseph Mingrone 				 * header, as -e wasn't specified, so
395ee67461eSJoseph Mingrone 				 * print only the source and destination
396ee67461eSJoseph Mingrone 				 * MAC addresses and the final Ethernet
397ee67461eSJoseph Mingrone 				 * type.
398ee67461eSJoseph Mingrone 				 */
399ee67461eSJoseph Mingrone 				ether_addresses_print(ndo, src.addr, dst.addr);
400ee67461eSJoseph Mingrone 				ether_type_print(ndo, length_type);
401ee67461eSJoseph Mingrone 				ND_PRINT(", length %u: ", orig_length);
40227df3f5dSRui Paulo 			}
40327df3f5dSRui Paulo 
404cac3dcd5SXin LI 			if (!ndo->ndo_suppress_default_print)
4053c602fabSXin LI 				ND_DEFAULTPRINT(p, caplen);
40627df3f5dSRui Paulo 		}
40727df3f5dSRui Paulo 	}
408ee67461eSJoseph Mingrone invalid:
409ee67461eSJoseph Mingrone 	return hdrlen;
410ee67461eSJoseph Mingrone }
411ee67461eSJoseph Mingrone 
412ee67461eSJoseph Mingrone /*
413*0a7e5f1fSJoseph Mingrone  * Print an Ethernet frame while specifying a non-standard Ethernet header
414ee67461eSJoseph Mingrone  * length.
415ee67461eSJoseph Mingrone  * This might be encapsulated within another frame; we might be passed
416ee67461eSJoseph Mingrone  * a pointer to a function that can print header information for that
417ee67461eSJoseph Mingrone  * frame's protocol, and an argument to pass to that function.
418ee67461eSJoseph Mingrone  *
419ee67461eSJoseph Mingrone  * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
420ee67461eSJoseph Mingrone  */
421ee67461eSJoseph Mingrone u_int
ether_switch_tag_print(netdissect_options * ndo,const u_char * p,u_int length,u_int caplen,void (* print_switch_tag)(netdissect_options *,const u_char *),u_int switch_tag_len)422ee67461eSJoseph Mingrone ether_switch_tag_print(netdissect_options *ndo, const u_char *p, u_int length,
423ee67461eSJoseph Mingrone     u_int caplen,
424ee67461eSJoseph Mingrone     void (*print_switch_tag)(netdissect_options *, const u_char *),
425ee67461eSJoseph Mingrone     u_int switch_tag_len)
426ee67461eSJoseph Mingrone {
427ee67461eSJoseph Mingrone 	return ether_common_print(ndo, p, length, caplen, print_switch_tag,
428ee67461eSJoseph Mingrone 				  switch_tag_len, NULL, NULL);
429ee67461eSJoseph Mingrone }
430ee67461eSJoseph Mingrone 
431ee67461eSJoseph Mingrone /*
432ee67461eSJoseph Mingrone  * Print an Ethernet frame.
433ee67461eSJoseph Mingrone  * This might be encapsulated within another frame; we might be passed
434ee67461eSJoseph Mingrone  * a pointer to a function that can print header information for that
435ee67461eSJoseph Mingrone  * frame's protocol, and an argument to pass to that function.
436ee67461eSJoseph Mingrone  *
437ee67461eSJoseph Mingrone  * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
438ee67461eSJoseph Mingrone  */
439ee67461eSJoseph Mingrone u_int
ether_print(netdissect_options * ndo,const u_char * p,u_int length,u_int caplen,void (* print_encap_header)(netdissect_options * ndo,const u_char *),const u_char * encap_header_arg)440ee67461eSJoseph Mingrone ether_print(netdissect_options *ndo,
441ee67461eSJoseph Mingrone 	    const u_char *p, u_int length, u_int caplen,
442ee67461eSJoseph Mingrone 	    void (*print_encap_header)(netdissect_options *ndo, const u_char *),
443ee67461eSJoseph Mingrone 	    const u_char *encap_header_arg)
444ee67461eSJoseph Mingrone {
445ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "ether";
446ee67461eSJoseph Mingrone 	return ether_common_print(ndo, p, length, caplen, NULL, 0,
447ee67461eSJoseph Mingrone 				  print_encap_header, encap_header_arg);
448cc391cceSBruce M Simpson }
449cc391cceSBruce M Simpson 
450cc391cceSBruce M Simpson /*
451cc391cceSBruce M Simpson  * This is the top level routine of the printer.  'p' points
4523340d773SGleb Smirnoff  * to the ether header of the packet, 'h->len' is the length
4533340d773SGleb Smirnoff  * of the packet off the wire, and 'h->caplen' is the number
4543340d773SGleb Smirnoff  * of bytes actually captured.
455cc391cceSBruce M Simpson  */
456ee67461eSJoseph Mingrone void
ether_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)457cac3dcd5SXin LI ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
458cac3dcd5SXin LI 	       const u_char *p)
459cc391cceSBruce M Simpson {
460ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "ether";
461ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len +=
462ee67461eSJoseph Mingrone 		ether_print(ndo, p, h->len, h->caplen, NULL, NULL);
4634edb46e9SPaul Traina }
4644edb46e9SPaul Traina 
4654edb46e9SPaul Traina /*
466cac3dcd5SXin LI  * This is the top level routine of the printer.  'p' points
4673340d773SGleb Smirnoff  * to the ether header of the packet, 'h->len' is the length
4683340d773SGleb Smirnoff  * of the packet off the wire, and 'h->caplen' is the number
4693340d773SGleb Smirnoff  * of bytes actually captured.
470cac3dcd5SXin LI  *
471cac3dcd5SXin LI  * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
472cac3dcd5SXin LI  * before the Ethernet header.
473cac3dcd5SXin LI  */
474ee67461eSJoseph Mingrone void
netanalyzer_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)475cac3dcd5SXin LI netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
476cac3dcd5SXin LI 		     const u_char *p)
477cac3dcd5SXin LI {
478cac3dcd5SXin LI 	/*
479cac3dcd5SXin LI 	 * Fail if we don't have enough data for the Hilscher pseudo-header.
480cac3dcd5SXin LI 	 */
481ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "netanalyzer";
482ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(p, 4);
483cac3dcd5SXin LI 
484cac3dcd5SXin LI 	/* Skip the pseudo-header. */
485ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len += 4;
486ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len +=
487ee67461eSJoseph Mingrone 		ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL);
488cac3dcd5SXin LI }
489cac3dcd5SXin LI 
490cac3dcd5SXin LI /*
491cac3dcd5SXin LI  * This is the top level routine of the printer.  'p' points
4923340d773SGleb Smirnoff  * to the ether header of the packet, 'h->len' is the length
4933340d773SGleb Smirnoff  * of the packet off the wire, and 'h->caplen' is the number
4943340d773SGleb Smirnoff  * of bytes actually captured.
495cac3dcd5SXin LI  *
496cac3dcd5SXin LI  * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
497cac3dcd5SXin LI  * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
498cac3dcd5SXin LI  * before the Ethernet header.
499cac3dcd5SXin LI  */
500ee67461eSJoseph Mingrone void
netanalyzer_transparent_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)501cac3dcd5SXin LI netanalyzer_transparent_if_print(netdissect_options *ndo,
502cac3dcd5SXin LI 				 const struct pcap_pkthdr *h,
503cac3dcd5SXin LI 				 const u_char *p)
504cac3dcd5SXin LI {
505cac3dcd5SXin LI 	/*
506cac3dcd5SXin LI 	 * Fail if we don't have enough data for the Hilscher pseudo-header,
507cac3dcd5SXin LI 	 * preamble, and SOF.
508cac3dcd5SXin LI 	 */
509ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "netanalyzer_transparent";
510ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(p, 12);
511cac3dcd5SXin LI 
512cac3dcd5SXin LI 	/* Skip the pseudo-header, preamble, and SOF. */
513ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len += 12;
514ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len +=
515ee67461eSJoseph Mingrone 		ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL);
516cac3dcd5SXin LI }
517cac3dcd5SXin LI 
518cac3dcd5SXin LI /*
51927df3f5dSRui Paulo  * Prints the packet payload, given an Ethernet type code for the payload's
52027df3f5dSRui Paulo  * protocol.
5214edb46e9SPaul Traina  *
5224edb46e9SPaul Traina  * Returns non-zero if it can do so, zero if the ethertype is unknown.
5234edb46e9SPaul Traina  */
5244edb46e9SPaul Traina 
5254edb46e9SPaul Traina int
ethertype_print(netdissect_options * ndo,u_short ether_type,const u_char * p,u_int length,u_int caplen,const struct lladdr_info * src,const struct lladdr_info * dst)526cac3dcd5SXin LI ethertype_print(netdissect_options *ndo,
527cac3dcd5SXin LI 		u_short ether_type, const u_char *p,
5283340d773SGleb Smirnoff 		u_int length, u_int caplen,
5293340d773SGleb Smirnoff 		const struct lladdr_info *src, const struct lladdr_info *dst)
5304edb46e9SPaul Traina {
531cc391cceSBruce M Simpson 	switch (ether_type) {
5324edb46e9SPaul Traina 
5334edb46e9SPaul Traina 	case ETHERTYPE_IP:
534cac3dcd5SXin LI 		ip_print(ndo, p, length);
5354edb46e9SPaul Traina 		return (1);
5364edb46e9SPaul Traina 
537a88113a8SBill Fenner 	case ETHERTYPE_IPV6:
538cac3dcd5SXin LI 		ip6_print(ndo, p, length);
539a88113a8SBill Fenner 		return (1);
540a88113a8SBill Fenner 
5414edb46e9SPaul Traina 	case ETHERTYPE_ARP:
5424edb46e9SPaul Traina 	case ETHERTYPE_REVARP:
543cac3dcd5SXin LI 		arp_print(ndo, p, length, caplen);
5444edb46e9SPaul Traina 		return (1);
5454edb46e9SPaul Traina 
5464edb46e9SPaul Traina 	case ETHERTYPE_DN:
5473c602fabSXin LI 		decnet_print(ndo, p, length, caplen);
5484edb46e9SPaul Traina 		return (1);
5494edb46e9SPaul Traina 
5504edb46e9SPaul Traina 	case ETHERTYPE_ATALK:
551cac3dcd5SXin LI 		if (ndo->ndo_vflag)
552ee67461eSJoseph Mingrone 			ND_PRINT("et1 ");
5533c602fabSXin LI 		atalk_print(ndo, p, length);
5544edb46e9SPaul Traina 		return (1);
5554edb46e9SPaul Traina 
5564edb46e9SPaul Traina 	case ETHERTYPE_AARP:
5573c602fabSXin LI 		aarp_print(ndo, p, length);
5584edb46e9SPaul Traina 		return (1);
5594edb46e9SPaul Traina 
5600ca3c3e0SPaul Traina 	case ETHERTYPE_IPX:
561ee67461eSJoseph Mingrone 		ND_PRINT("(NOV-ETHII) ");
5623c602fabSXin LI 		ipx_print(ndo, p, length);
5630ca3c3e0SPaul Traina 		return (1);
5640ca3c3e0SPaul Traina 
565c1ad1296SSam Leffler 	case ETHERTYPE_ISO:
5663340d773SGleb Smirnoff 		if (length == 0 || caplen == 0) {
567ee67461eSJoseph Mingrone 			ndo->ndo_protocol = "isoclns";
568ee67461eSJoseph Mingrone 			nd_print_trunc(ndo);
5693340d773SGleb Smirnoff 			return (1);
5703340d773SGleb Smirnoff 		}
571ee67461eSJoseph Mingrone 		/* At least one byte is required */
572ee67461eSJoseph Mingrone 		/* FIXME: Reference for this byte? */
573ee67461eSJoseph Mingrone 		ND_TCHECK_LEN(p, 1);
5740bff6a5aSEd Maste 		isoclns_print(ndo, p + 1, length - 1);
575c1ad1296SSam Leffler 		return(1);
576c1ad1296SSam Leffler 
577a88113a8SBill Fenner 	case ETHERTYPE_PPPOED:
578a88113a8SBill Fenner 	case ETHERTYPE_PPPOES:
579f1f9d42aSJulian Elischer 	case ETHERTYPE_PPPOED2:
580f1f9d42aSJulian Elischer 	case ETHERTYPE_PPPOES2:
5813c602fabSXin LI 		pppoe_print(ndo, p, length);
582e8fdcab3SBrian Somers 		return (1);
583e8fdcab3SBrian Somers 
584c1ad1296SSam Leffler 	case ETHERTYPE_EAPOL:
585ee67461eSJoseph Mingrone 		eapol_print(ndo, p);
586c1ad1296SSam Leffler 		return (1);
587c1ad1296SSam Leffler 
588ee67461eSJoseph Mingrone 	case ETHERTYPE_REALTEK:
589ee67461eSJoseph Mingrone 		rtl_print(ndo, p, length, src, dst);
590a5779b6eSRui Paulo 		return (1);
591a5779b6eSRui Paulo 
592a1c2090eSBill Fenner 	case ETHERTYPE_PPP:
593a1c2090eSBill Fenner 		if (length) {
594ee67461eSJoseph Mingrone 			ND_PRINT(": ");
5953c602fabSXin LI 			ppp_print(ndo, p, length);
596a1c2090eSBill Fenner 		}
597a1c2090eSBill Fenner 		return (1);
598a1c2090eSBill Fenner 
599a5779b6eSRui Paulo 	case ETHERTYPE_MPCP:
6003c602fabSXin LI 		mpcp_print(ndo, p, length);
601a5779b6eSRui Paulo 		return (1);
602a5779b6eSRui Paulo 
60317cb103cSSam Leffler 	case ETHERTYPE_SLOW:
6043c602fabSXin LI 		slow_print(ndo, p, length);
60517cb103cSSam Leffler 		return (1);
60617cb103cSSam Leffler 
607a5779b6eSRui Paulo 	case ETHERTYPE_CFM:
608a5779b6eSRui Paulo 	case ETHERTYPE_CFM_OLD:
6093c602fabSXin LI 		cfm_print(ndo, p, length);
610a5779b6eSRui Paulo 		return (1);
611a5779b6eSRui Paulo 
612a5779b6eSRui Paulo 	case ETHERTYPE_LLDP:
6133c602fabSXin LI 		lldp_print(ndo, p, length);
614a5779b6eSRui Paulo 		return (1);
615a5779b6eSRui Paulo 
616ee67461eSJoseph Mingrone 	case ETHERTYPE_NSH:
617ee67461eSJoseph Mingrone 		nsh_print(ndo, p, length);
618ee67461eSJoseph Mingrone 		return (1);
619ee67461eSJoseph Mingrone 
620cc391cceSBruce M Simpson 	case ETHERTYPE_LOOPBACK:
6213c602fabSXin LI 		loopback_print(ndo, p, length);
62229292c17SSam Leffler 		return (1);
623cc391cceSBruce M Simpson 
624a1c2090eSBill Fenner 	case ETHERTYPE_MPLS:
625a1c2090eSBill Fenner 	case ETHERTYPE_MPLS_MULTI:
6263c602fabSXin LI 		mpls_print(ndo, p, length);
627a1c2090eSBill Fenner 		return (1);
628a1c2090eSBill Fenner 
629d09a7e67SXin LI 	case ETHERTYPE_TIPC:
630d09a7e67SXin LI 		tipc_print(ndo, p, length, caplen);
631d09a7e67SXin LI 		return (1);
632d09a7e67SXin LI 
633d03c0883SXin LI 	case ETHERTYPE_MS_NLB_HB:
6343c602fabSXin LI 		msnlb_print(ndo, p);
6353c602fabSXin LI 		return (1);
6363c602fabSXin LI 
6373c602fabSXin LI 	case ETHERTYPE_GEONET_OLD:
6383c602fabSXin LI 	case ETHERTYPE_GEONET:
6393340d773SGleb Smirnoff 		geonet_print(ndo, p, length, src);
6403c602fabSXin LI 		return (1);
6413c602fabSXin LI 
6423c602fabSXin LI 	case ETHERTYPE_CALM_FAST:
6433340d773SGleb Smirnoff 		calm_fast_print(ndo, p, length, src);
6443c602fabSXin LI 		return (1);
6453c602fabSXin LI 
6463c602fabSXin LI 	case ETHERTYPE_AOE:
6473c602fabSXin LI 		aoe_print(ndo, p, length);
648d03c0883SXin LI 		return (1);
649d03c0883SXin LI 
650ee67461eSJoseph Mingrone 	case ETHERTYPE_PTP:
651ee67461eSJoseph Mingrone 		ptp_print(ndo, p, length);
6523340d773SGleb Smirnoff 		return (1);
6533340d773SGleb Smirnoff 
6544edb46e9SPaul Traina 	case ETHERTYPE_LAT:
6554edb46e9SPaul Traina 	case ETHERTYPE_SCA:
6564edb46e9SPaul Traina 	case ETHERTYPE_MOPRC:
6574edb46e9SPaul Traina 	case ETHERTYPE_MOPDL:
6583c602fabSXin LI 	case ETHERTYPE_IEEE1905_1:
6594edb46e9SPaul Traina 		/* default_print for now */
6604edb46e9SPaul Traina 	default:
6614edb46e9SPaul Traina 		return (0);
6624edb46e9SPaul Traina 	}
6634edb46e9SPaul Traina }
664