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.
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
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.
22 /* \summary: Ethernet printer */
26 #include "netdissect-stdinc.h"
28 #define ND_LONGJMP_FROM_TCHECK
29 #include "netdissect.h"
31 #include "addrtoname.h"
32 #include "ethertype.h"
35 * Structure of an Ethernet header.
40 nd_uint16_t ether_length_type
;
44 * Length of an Ethernet header; note that some compilers may pad
45 * "struct ether_header" to a multiple of 4 bytes, for example, so
46 * "sizeof (struct ether_header)" may not give the right answer.
48 #define ETHER_HDRLEN 14
50 const struct tok ethertype_values
[] = {
51 { ETHERTYPE_IP
, "IPv4" },
52 { ETHERTYPE_MPLS
, "MPLS unicast" },
53 { ETHERTYPE_MPLS_MULTI
, "MPLS multicast" },
54 { ETHERTYPE_IPV6
, "IPv6" },
55 { ETHERTYPE_8021Q
, "802.1Q" },
56 { ETHERTYPE_8021Q9100
, "802.1Q-9100" },
57 { ETHERTYPE_8021QinQ
, "802.1Q-QinQ" },
58 { ETHERTYPE_8021Q9200
, "802.1Q-9200" },
59 { ETHERTYPE_MACSEC
, "802.1AE MACsec" },
60 { ETHERTYPE_VMAN
, "VMAN" },
61 { ETHERTYPE_PUP
, "PUP" },
62 { ETHERTYPE_ARP
, "ARP"},
63 { ETHERTYPE_REVARP
, "Reverse ARP"},
64 { ETHERTYPE_NS
, "NS" },
65 { ETHERTYPE_SPRITE
, "Sprite" },
66 { ETHERTYPE_TRAIL
, "Trail" },
67 { ETHERTYPE_MOPDL
, "MOP DL" },
68 { ETHERTYPE_MOPRC
, "MOP RC" },
69 { ETHERTYPE_DN
, "DN" },
70 { ETHERTYPE_LAT
, "LAT" },
71 { ETHERTYPE_SCA
, "SCA" },
72 { ETHERTYPE_TEB
, "TEB" },
73 { ETHERTYPE_LANBRIDGE
, "Lanbridge" },
74 { ETHERTYPE_DECDNS
, "DEC DNS" },
75 { ETHERTYPE_DECDTS
, "DEC DTS" },
76 { ETHERTYPE_VEXP
, "VEXP" },
77 { ETHERTYPE_VPROD
, "VPROD" },
78 { ETHERTYPE_ATALK
, "Appletalk" },
79 { ETHERTYPE_AARP
, "Appletalk ARP" },
80 { ETHERTYPE_IPX
, "IPX" },
81 { ETHERTYPE_PPP
, "PPP" },
82 { ETHERTYPE_MPCP
, "MPCP" },
83 { ETHERTYPE_SLOW
, "Slow Protocols" },
84 { ETHERTYPE_PPPOED
, "PPPoE D" },
85 { ETHERTYPE_PPPOES
, "PPPoE S" },
86 { ETHERTYPE_EAPOL
, "EAPOL" },
87 { ETHERTYPE_REALTEK
, "Realtek protocols" },
88 { ETHERTYPE_MS_NLB_HB
, "MS NLB heartbeat" },
89 { ETHERTYPE_JUMBO
, "Jumbo" },
90 { ETHERTYPE_NSH
, "NSH" },
91 { ETHERTYPE_LOOPBACK
, "Loopback" },
92 { ETHERTYPE_ISO
, "OSI" },
93 { ETHERTYPE_GRE_ISO
, "GRE-OSI" },
94 { ETHERTYPE_CFM_OLD
, "CFM (old)" },
95 { ETHERTYPE_CFM
, "CFM" },
96 { ETHERTYPE_IEEE1905_1
, "IEEE1905.1" },
97 { ETHERTYPE_LLDP
, "LLDP" },
98 { ETHERTYPE_TIPC
, "TIPC"},
99 { ETHERTYPE_GEONET_OLD
, "GeoNet (old)"},
100 { ETHERTYPE_GEONET
, "GeoNet"},
101 { ETHERTYPE_CALM_FAST
, "CALM FAST"},
102 { ETHERTYPE_AOE
, "AoE" },
103 { ETHERTYPE_PTP
, "PTP" },
104 { ETHERTYPE_ARISTA
, "Arista Vendor Specific Protocol" },
109 ether_addresses_print(netdissect_options
*ndo
, const u_char
*src
,
112 ND_PRINT("%s > %s, ",
113 GET_MAC48_STRING(src
), GET_MAC48_STRING(dst
));
117 ether_type_print(netdissect_options
*ndo
, uint16_t type
)
120 ND_PRINT("ethertype %s (0x%04x)",
121 tok2str(ethertype_values
, "Unknown", type
), type
);
124 tok2str(ethertype_values
, "Unknown Ethertype (0x%04x)", type
));
128 * Common code for printing Ethernet frames.
130 * It can handle Ethernet headers with extra tag information inserted
131 * after the destination and source addresses, as is inserted by some
132 * switch chips, and extra encapsulation header information before
133 * printing Ethernet header information (such as a LANE ID for ATM LANE).
136 ether_common_print(netdissect_options
*ndo
, const u_char
*p
, u_int length
,
138 void (*print_switch_tag
)(netdissect_options
*ndo
, const u_char
*),
139 u_int switch_tag_len
,
140 void (*print_encap_header
)(netdissect_options
*ndo
, const u_char
*),
141 const u_char
*encap_header_arg
)
143 const struct ether_header
*ehp
;
149 struct lladdr_info src
, dst
;
151 if (length
< caplen
) {
152 ND_PRINT("[length %u < caplen %u]", length
, caplen
);
153 nd_print_invalid(ndo
);
156 if (caplen
< ETHER_HDRLEN
+ switch_tag_len
) {
161 if (print_encap_header
!= NULL
)
162 (*print_encap_header
)(ndo
, encap_header_arg
);
164 orig_length
= length
;
167 * Get the source and destination addresses, skip past them,
168 * and print them if we're printing the link-layer header.
170 ehp
= (const struct ether_header
*)p
;
171 src
.addr
= ehp
->ether_shost
;
172 src
.addr_string
= mac48_string
;
173 dst
.addr
= ehp
->ether_dhost
;
174 dst
.addr_string
= mac48_string
;
176 length
-= 2*MAC48_LEN
;
177 caplen
-= 2*MAC48_LEN
;
179 hdrlen
= 2*MAC48_LEN
;
182 ether_addresses_print(ndo
, src
.addr
, dst
.addr
);
185 * Print the switch tag, if we have one, and skip past it.
187 if (print_switch_tag
!= NULL
)
188 (*print_switch_tag
)(ndo
, p
);
190 length
-= switch_tag_len
;
191 caplen
-= switch_tag_len
;
193 hdrlen
+= switch_tag_len
;
196 * Get the length/type field, skip past it, and print it
197 * if we're printing the link-layer header.
200 length_type
= GET_BE_U_2(p
);
208 * Process 802.1AE MACsec headers.
211 if (length_type
== ETHERTYPE_MACSEC
) {
213 * MACsec, aka IEEE 802.1AE-2006
214 * Print the header, and try to print the payload if it's not encrypted
216 if (ndo
->ndo_eflag
) {
217 ether_type_print(ndo
, length_type
);
218 ND_PRINT(", length %u: ", orig_length
);
222 int ret
= macsec_print(ndo
, &p
, &length
, &caplen
, &hdrlen
,
226 /* Payload is encrypted; print it as raw data. */
227 if (!ndo
->ndo_suppress_default_print
)
228 ND_DEFAULTPRINT(p
, caplen
);
230 } else if (ret
> 0) {
231 /* Problem printing the header; just quit. */
235 * Keep processing type/length fields.
237 length_type
= GET_BE_U_2(p
);
239 ND_ICHECK_U(caplen
, <, 2);
248 * Process VLAN tag types.
250 while (length_type
== ETHERTYPE_8021Q
||
251 length_type
== ETHERTYPE_8021Q9100
||
252 length_type
== ETHERTYPE_8021Q9200
||
253 length_type
== ETHERTYPE_8021QinQ
) {
256 * Print VLAN information, and then go back and process
257 * the enclosed type field.
260 ndo
->ndo_protocol
= "vlan";
262 return hdrlen
+ caplen
;
265 ndo
->ndo_protocol
= "vlan";
267 return hdrlen
+ length
;
269 if (ndo
->ndo_eflag
) {
270 uint16_t tag
= GET_BE_U_2(p
);
272 ether_type_print(ndo
, length_type
);
273 if (!printed_length
) {
274 ND_PRINT(", length %u: ", orig_length
);
278 ND_PRINT("%s, ", ieee8021q_tci_string(tag
));
281 length_type
= GET_BE_U_2(p
+ 2);
289 * We now have the final length/type field.
291 if (length_type
<= MAX_ETHERNET_LENGTH_VAL
) {
293 * It's a length field, containing the length of the
294 * remaining payload; use it as such, as long as
295 * it's not too large (bigger than the actual payload).
297 if (length_type
< length
) {
298 length
= length_type
;
304 * Cut off the snapshot length to the end of the
307 if (!nd_push_snaplen(ndo
, p
, length
)) {
308 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
309 "%s: can't push snaplen on buffer stack", __func__
);
312 if (ndo
->ndo_eflag
) {
315 ND_PRINT(", length %u: ", length
);
319 * An LLC header follows the length. Print that and
322 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, &src
, &dst
);
323 if (llc_hdrlen
< 0) {
324 /* packet type not known, print raw packet */
325 if (!ndo
->ndo_suppress_default_print
)
326 ND_DEFAULTPRINT(p
, caplen
);
327 llc_hdrlen
= -llc_hdrlen
;
329 hdrlen
+= llc_hdrlen
;
330 nd_pop_packet_info(ndo
);
331 } else if (length_type
== ETHERTYPE_JUMBO
) {
333 * It's a type field, with the type for Alteon jumbo frames.
336 * https://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
338 * which indicates that, following the type field,
339 * there's an LLC header and payload.
341 /* Try to print the LLC-layer header & higher layers */
342 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, &src
, &dst
);
343 if (llc_hdrlen
< 0) {
344 /* packet type not known, print raw packet */
345 if (!ndo
->ndo_suppress_default_print
)
346 ND_DEFAULTPRINT(p
, caplen
);
347 llc_hdrlen
= -llc_hdrlen
;
349 hdrlen
+= llc_hdrlen
;
350 } else if (length_type
== ETHERTYPE_ARISTA
) {
352 ND_PRINT("[|arista]");
353 return hdrlen
+ caplen
;
356 ND_PRINT("[|arista]");
357 return hdrlen
+ length
;
359 ether_type_print(ndo
, length_type
);
360 ND_PRINT(", length %u: ", orig_length
);
361 int bytesConsumed
= arista_ethertype_print(ndo
, p
, length
);
362 if (bytesConsumed
> 0) {
364 length
-= bytesConsumed
;
365 caplen
-= bytesConsumed
;
366 hdrlen
+= bytesConsumed
;
369 /* subtype/version not known, print raw packet */
370 if (!ndo
->ndo_eflag
&& length_type
> MAX_ETHERNET_LENGTH_VAL
) {
371 ether_addresses_print(ndo
, src
.addr
, dst
.addr
);
372 ether_type_print(ndo
, length_type
);
373 ND_PRINT(", length %u: ", orig_length
);
375 if (!ndo
->ndo_suppress_default_print
)
376 ND_DEFAULTPRINT(p
, caplen
);
380 * It's a type field with some other value.
382 if (ndo
->ndo_eflag
) {
383 ether_type_print(ndo
, length_type
);
385 ND_PRINT(", length %u: ", orig_length
);
389 if (ethertype_print(ndo
, length_type
, p
, length
, caplen
, &src
, &dst
) == 0) {
390 /* type not known, print raw packet */
391 if (!ndo
->ndo_eflag
) {
393 * We didn't print the full link-layer
394 * header, as -e wasn't specified, so
395 * print only the source and destination
396 * MAC addresses and the final Ethernet
399 ether_addresses_print(ndo
, src
.addr
, dst
.addr
);
400 ether_type_print(ndo
, length_type
);
401 ND_PRINT(", length %u: ", orig_length
);
404 if (!ndo
->ndo_suppress_default_print
)
405 ND_DEFAULTPRINT(p
, caplen
);
413 * Print an Ethernet frame while specifying a non-standard Ethernet header
415 * This might be encapsulated within another frame; we might be passed
416 * a pointer to a function that can print header information for that
417 * frame's protocol, and an argument to pass to that function.
419 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
422 ether_switch_tag_print(netdissect_options
*ndo
, const u_char
*p
, u_int length
,
424 void (*print_switch_tag
)(netdissect_options
*, const u_char
*),
425 u_int switch_tag_len
)
427 return ether_common_print(ndo
, p
, length
, caplen
, print_switch_tag
,
428 switch_tag_len
, NULL
, NULL
);
432 * Print an Ethernet frame.
433 * This might be encapsulated within another frame; we might be passed
434 * a pointer to a function that can print header information for that
435 * frame's protocol, and an argument to pass to that function.
437 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
440 ether_print(netdissect_options
*ndo
,
441 const u_char
*p
, u_int length
, u_int caplen
,
442 void (*print_encap_header
)(netdissect_options
*ndo
, const u_char
*),
443 const u_char
*encap_header_arg
)
445 ndo
->ndo_protocol
= "ether";
446 return ether_common_print(ndo
, p
, length
, caplen
, NULL
, 0,
447 print_encap_header
, encap_header_arg
);
451 * This is the top level routine of the printer. 'p' points
452 * to the ether header of the packet, 'h->len' is the length
453 * of the packet off the wire, and 'h->caplen' is the number
454 * of bytes actually captured.
457 ether_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
460 ndo
->ndo_protocol
= "ether";
461 ndo
->ndo_ll_hdr_len
+=
462 ether_print(ndo
, p
, h
->len
, h
->caplen
, NULL
, NULL
);
466 * This is the top level routine of the printer. 'p' points
467 * to the ether header of the packet, 'h->len' is the length
468 * of the packet off the wire, and 'h->caplen' is the number
469 * of bytes actually captured.
471 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
472 * before the Ethernet header.
475 netanalyzer_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
479 * Fail if we don't have enough data for the Hilscher pseudo-header.
481 ndo
->ndo_protocol
= "netanalyzer";
484 /* Skip the pseudo-header. */
485 ndo
->ndo_ll_hdr_len
+= 4;
486 ndo
->ndo_ll_hdr_len
+=
487 ether_print(ndo
, p
+ 4, h
->len
- 4, h
->caplen
- 4, NULL
, NULL
);
491 * This is the top level routine of the printer. 'p' points
492 * to the ether header of the packet, 'h->len' is the length
493 * of the packet off the wire, and 'h->caplen' is the number
494 * of bytes actually captured.
496 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
497 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
498 * before the Ethernet header.
501 netanalyzer_transparent_if_print(netdissect_options
*ndo
,
502 const struct pcap_pkthdr
*h
,
506 * Fail if we don't have enough data for the Hilscher pseudo-header,
509 ndo
->ndo_protocol
= "netanalyzer_transparent";
510 ND_TCHECK_LEN(p
, 12);
512 /* Skip the pseudo-header, preamble, and SOF. */
513 ndo
->ndo_ll_hdr_len
+= 12;
514 ndo
->ndo_ll_hdr_len
+=
515 ether_print(ndo
, p
+ 12, h
->len
- 12, h
->caplen
- 12, NULL
, NULL
);
519 * Prints the packet payload, given an Ethernet type code for the payload's
522 * Returns non-zero if it can do so, zero if the ethertype is unknown.
526 ethertype_print(netdissect_options
*ndo
,
527 u_short ether_type
, const u_char
*p
,
528 u_int length
, u_int caplen
,
529 const struct lladdr_info
*src
, const struct lladdr_info
*dst
)
531 switch (ether_type
) {
534 ip_print(ndo
, p
, length
);
538 ip6_print(ndo
, p
, length
);
542 case ETHERTYPE_REVARP
:
543 arp_print(ndo
, p
, length
, caplen
);
547 decnet_print(ndo
, p
, length
, caplen
);
550 case ETHERTYPE_ATALK
:
553 atalk_print(ndo
, p
, length
);
557 aarp_print(ndo
, p
, length
);
561 ND_PRINT("(NOV-ETHII) ");
562 ipx_print(ndo
, p
, length
);
566 if (length
== 0 || caplen
== 0) {
567 ndo
->ndo_protocol
= "isoclns";
571 /* At least one byte is required */
572 /* FIXME: Reference for this byte? */
574 isoclns_print(ndo
, p
+ 1, length
- 1);
577 case ETHERTYPE_PPPOED
:
578 case ETHERTYPE_PPPOES
:
579 case ETHERTYPE_PPPOED2
:
580 case ETHERTYPE_PPPOES2
:
581 pppoe_print(ndo
, p
, length
);
584 case ETHERTYPE_EAPOL
:
588 case ETHERTYPE_REALTEK
:
589 rtl_print(ndo
, p
, length
, src
, dst
);
595 ppp_print(ndo
, p
, length
);
600 mpcp_print(ndo
, p
, length
);
604 slow_print(ndo
, p
, length
);
608 case ETHERTYPE_CFM_OLD
:
609 cfm_print(ndo
, p
, length
);
613 lldp_print(ndo
, p
, length
);
617 nsh_print(ndo
, p
, length
);
620 case ETHERTYPE_LOOPBACK
:
621 loopback_print(ndo
, p
, length
);
625 case ETHERTYPE_MPLS_MULTI
:
626 mpls_print(ndo
, p
, length
);
630 tipc_print(ndo
, p
, length
, caplen
);
633 case ETHERTYPE_MS_NLB_HB
:
637 case ETHERTYPE_GEONET_OLD
:
638 case ETHERTYPE_GEONET
:
639 geonet_print(ndo
, p
, length
, src
);
642 case ETHERTYPE_CALM_FAST
:
643 calm_fast_print(ndo
, p
, length
, src
);
647 aoe_print(ndo
, p
, length
);
651 ptp_print(ndo
, p
, length
);
656 case ETHERTYPE_MOPRC
:
657 case ETHERTYPE_MOPDL
:
658 case ETHERTYPE_IEEE1905_1
:
659 /* default_print for now */