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 */
28 #include "netdissect-stdinc.h"
30 #define ND_LONGJMP_FROM_TCHECK
31 #include "netdissect.h"
33 #include "addrtoname.h"
34 #include "ethertype.h"
37 * Structure of an Ethernet header.
40 nd_mac_addr ether_dhost
;
41 nd_mac_addr ether_shost
;
42 nd_uint16_t ether_length_type
;
46 * Length of an Ethernet header; note that some compilers may pad
47 * "struct ether_header" to a multiple of 4 bytes, for example, so
48 * "sizeof (struct ether_header)" may not give the right answer.
50 #define ETHER_HDRLEN 14
52 const struct tok ethertype_values
[] = {
53 { ETHERTYPE_IP
, "IPv4" },
54 { ETHERTYPE_MPLS
, "MPLS unicast" },
55 { ETHERTYPE_MPLS_MULTI
, "MPLS multicast" },
56 { ETHERTYPE_IPV6
, "IPv6" },
57 { ETHERTYPE_8021Q
, "802.1Q" },
58 { ETHERTYPE_8021Q9100
, "802.1Q-9100" },
59 { ETHERTYPE_8021QinQ
, "802.1Q-QinQ" },
60 { ETHERTYPE_8021Q9200
, "802.1Q-9200" },
61 { ETHERTYPE_MACSEC
, "802.1AE MACsec" },
62 { ETHERTYPE_VMAN
, "VMAN" },
63 { ETHERTYPE_PUP
, "PUP" },
64 { ETHERTYPE_ARP
, "ARP"},
65 { ETHERTYPE_REVARP
, "Reverse ARP"},
66 { ETHERTYPE_NS
, "NS" },
67 { ETHERTYPE_SPRITE
, "Sprite" },
68 { ETHERTYPE_TRAIL
, "Trail" },
69 { ETHERTYPE_MOPDL
, "MOP DL" },
70 { ETHERTYPE_MOPRC
, "MOP RC" },
71 { ETHERTYPE_DN
, "DN" },
72 { ETHERTYPE_LAT
, "LAT" },
73 { ETHERTYPE_SCA
, "SCA" },
74 { ETHERTYPE_TEB
, "TEB" },
75 { ETHERTYPE_LANBRIDGE
, "Lanbridge" },
76 { ETHERTYPE_DECDNS
, "DEC DNS" },
77 { ETHERTYPE_DECDTS
, "DEC DTS" },
78 { ETHERTYPE_VEXP
, "VEXP" },
79 { ETHERTYPE_VPROD
, "VPROD" },
80 { ETHERTYPE_ATALK
, "Appletalk" },
81 { ETHERTYPE_AARP
, "Appletalk ARP" },
82 { ETHERTYPE_IPX
, "IPX" },
83 { ETHERTYPE_PPP
, "PPP" },
84 { ETHERTYPE_MPCP
, "MPCP" },
85 { ETHERTYPE_SLOW
, "Slow Protocols" },
86 { ETHERTYPE_PPPOED
, "PPPoE D" },
87 { ETHERTYPE_PPPOES
, "PPPoE S" },
88 { ETHERTYPE_EAPOL
, "EAPOL" },
89 { ETHERTYPE_RRCP
, "RRCP" },
90 { ETHERTYPE_MS_NLB_HB
, "MS NLB heartbeat" },
91 { ETHERTYPE_JUMBO
, "Jumbo" },
92 { ETHERTYPE_NSH
, "NSH" },
93 { ETHERTYPE_LOOPBACK
, "Loopback" },
94 { ETHERTYPE_ISO
, "OSI" },
95 { ETHERTYPE_GRE_ISO
, "GRE-OSI" },
96 { ETHERTYPE_CFM_OLD
, "CFM (old)" },
97 { ETHERTYPE_CFM
, "CFM" },
98 { ETHERTYPE_IEEE1905_1
, "IEEE1905.1" },
99 { ETHERTYPE_LLDP
, "LLDP" },
100 { ETHERTYPE_TIPC
, "TIPC"},
101 { ETHERTYPE_GEONET_OLD
, "GeoNet (old)"},
102 { ETHERTYPE_GEONET
, "GeoNet"},
103 { ETHERTYPE_CALM_FAST
, "CALM FAST"},
104 { ETHERTYPE_AOE
, "AoE" },
105 { ETHERTYPE_PTP
, "PTP" },
106 { ETHERTYPE_ARISTA
, "Arista Vendor Specific Protocol" },
111 ether_addresses_print(netdissect_options
*ndo
, const u_char
*src
,
114 ND_PRINT("%s > %s, ",
115 GET_ETHERADDR_STRING(src
), GET_ETHERADDR_STRING(dst
));
119 ether_type_print(netdissect_options
*ndo
, uint16_t type
)
122 ND_PRINT("ethertype %s (0x%04x)",
123 tok2str(ethertype_values
, "Unknown", type
), type
);
126 tok2str(ethertype_values
, "Unknown Ethertype (0x%04x)", type
));
130 * Common code for printing Ethernet frames.
132 * It can handle Ethernet headers with extra tag information inserted
133 * after the destination and source addresses, as is inserted by some
134 * switch chips, and extra encapsulation header information before
135 * printing Ethernet header information (such as a LANE ID for ATM LANE).
138 ether_common_print(netdissect_options
*ndo
, const u_char
*p
, u_int length
,
140 void (*print_switch_tag
)(netdissect_options
*ndo
, const u_char
*),
141 u_int switch_tag_len
,
142 void (*print_encap_header
)(netdissect_options
*ndo
, const u_char
*),
143 const u_char
*encap_header_arg
)
145 const struct ether_header
*ehp
;
151 struct lladdr_info src
, dst
;
153 if (caplen
< ETHER_HDRLEN
+ switch_tag_len
) {
157 if (length
< ETHER_HDRLEN
+ switch_tag_len
) {
162 if (print_encap_header
!= NULL
)
163 (*print_encap_header
)(ndo
, encap_header_arg
);
165 orig_length
= length
;
168 * Get the source and destination addresses, skip past them,
169 * and print them if we're printing the link-layer header.
171 ehp
= (const struct ether_header
*)p
;
172 src
.addr
= ehp
->ether_shost
;
173 src
.addr_string
= etheraddr_string
;
174 dst
.addr
= ehp
->ether_dhost
;
175 dst
.addr_string
= etheraddr_string
;
177 length
-= 2*MAC_ADDR_LEN
;
178 caplen
-= 2*MAC_ADDR_LEN
;
180 hdrlen
= 2*MAC_ADDR_LEN
;
183 ether_addresses_print(ndo
, src
.addr
, dst
.addr
);
186 * Print the switch tag, if we have one, and skip past it.
188 if (print_switch_tag
!= NULL
)
189 (*print_switch_tag
)(ndo
, p
);
191 length
-= switch_tag_len
;
192 caplen
-= switch_tag_len
;
194 hdrlen
+= switch_tag_len
;
197 * Get the length/type field, skip past it, and print it
198 * if we're printing the link-layer header.
201 length_type
= GET_BE_U_2(p
);
209 * Process 802.1AE MACsec headers.
212 if (length_type
== ETHERTYPE_MACSEC
) {
214 * MACsec, aka IEEE 802.1AE-2006
215 * Print the header, and try to print the payload if it's not encrypted
217 if (ndo
->ndo_eflag
) {
218 ether_type_print(ndo
, length_type
);
219 ND_PRINT(", length %u: ", orig_length
);
223 int ret
= macsec_print(ndo
, &p
, &length
, &caplen
, &hdrlen
,
227 /* Payload is encrypted; print it as raw data. */
228 if (!ndo
->ndo_suppress_default_print
)
229 ND_DEFAULTPRINT(p
, caplen
);
231 } else if (ret
> 0) {
232 /* Problem printing the header; just quit. */
236 * Keep processing type/length fields.
238 length_type
= GET_BE_U_2(p
);
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 nd_push_snapend(ndo
, p
+ length
);
309 if (ndo
->ndo_eflag
) {
312 ND_PRINT(", length %u: ", length
);
316 * An LLC header follows the length. Print that and
319 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, &src
, &dst
);
320 if (llc_hdrlen
< 0) {
321 /* packet type not known, print raw packet */
322 if (!ndo
->ndo_suppress_default_print
)
323 ND_DEFAULTPRINT(p
, caplen
);
324 llc_hdrlen
= -llc_hdrlen
;
326 hdrlen
+= llc_hdrlen
;
327 nd_pop_packet_info(ndo
);
328 } else if (length_type
== ETHERTYPE_JUMBO
) {
330 * It's a type field, with the type for Alteon jumbo frames.
333 * https://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
335 * which indicates that, following the type field,
336 * there's an LLC header and payload.
338 /* Try to print the LLC-layer header & higher layers */
339 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, &src
, &dst
);
340 if (llc_hdrlen
< 0) {
341 /* packet type not known, print raw packet */
342 if (!ndo
->ndo_suppress_default_print
)
343 ND_DEFAULTPRINT(p
, caplen
);
344 llc_hdrlen
= -llc_hdrlen
;
346 hdrlen
+= llc_hdrlen
;
347 } else if (length_type
== ETHERTYPE_ARISTA
) {
349 ND_PRINT("[|arista]");
350 return hdrlen
+ caplen
;
353 ND_PRINT("[|arista]");
354 return hdrlen
+ length
;
356 ether_type_print(ndo
, length_type
);
357 ND_PRINT(", length %u: ", orig_length
);
358 int bytesConsumed
= arista_ethertype_print(ndo
, p
, length
);
359 if (bytesConsumed
> 0) {
361 length
-= bytesConsumed
;
362 caplen
-= bytesConsumed
;
363 hdrlen
+= bytesConsumed
;
366 /* subtype/version not known, print raw packet */
367 if (!ndo
->ndo_eflag
&& length_type
> MAX_ETHERNET_LENGTH_VAL
) {
368 ether_addresses_print(ndo
, src
.addr
, dst
.addr
);
369 ether_type_print(ndo
, length_type
);
370 ND_PRINT(", length %u: ", orig_length
);
372 if (!ndo
->ndo_suppress_default_print
)
373 ND_DEFAULTPRINT(p
, caplen
);
377 * It's a type field with some other value.
379 if (ndo
->ndo_eflag
) {
380 ether_type_print(ndo
, length_type
);
382 ND_PRINT(", length %u: ", orig_length
);
386 if (ethertype_print(ndo
, length_type
, p
, length
, caplen
, &src
, &dst
) == 0) {
387 /* type not known, print raw packet */
388 if (!ndo
->ndo_eflag
) {
390 * We didn't print the full link-layer
391 * header, as -e wasn't specified, so
392 * print only the source and destination
393 * MAC addresses and the final Ethernet
396 ether_addresses_print(ndo
, src
.addr
, dst
.addr
);
397 ether_type_print(ndo
, length_type
);
398 ND_PRINT(", length %u: ", orig_length
);
401 if (!ndo
->ndo_suppress_default_print
)
402 ND_DEFAULTPRINT(p
, caplen
);
409 * Print an Ethernet frame while specyfing a non-standard Ethernet header
411 * This might be encapsulated within another frame; we might be passed
412 * a pointer to a function that can print header information for that
413 * frame's protocol, and an argument to pass to that function.
415 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
418 ether_switch_tag_print(netdissect_options
*ndo
, const u_char
*p
, u_int length
,
420 void (*print_switch_tag
)(netdissect_options
*, const u_char
*),
421 u_int switch_tag_len
)
423 return ether_common_print(ndo
, p
, length
, caplen
, print_switch_tag
,
424 switch_tag_len
, NULL
, NULL
);
428 * Print an Ethernet frame.
429 * This might be encapsulated within another frame; we might be passed
430 * a pointer to a function that can print header information for that
431 * frame's protocol, and an argument to pass to that function.
433 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
436 ether_print(netdissect_options
*ndo
,
437 const u_char
*p
, u_int length
, u_int caplen
,
438 void (*print_encap_header
)(netdissect_options
*ndo
, const u_char
*),
439 const u_char
*encap_header_arg
)
441 ndo
->ndo_protocol
= "ether";
442 return ether_common_print(ndo
, p
, length
, caplen
, NULL
, 0,
443 print_encap_header
, encap_header_arg
);
447 * This is the top level routine of the printer. 'p' points
448 * to the ether header of the packet, 'h->len' is the length
449 * of the packet off the wire, and 'h->caplen' is the number
450 * of bytes actually captured.
453 ether_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
456 ndo
->ndo_protocol
= "ether";
457 ndo
->ndo_ll_hdr_len
+=
458 ether_print(ndo
, p
, h
->len
, h
->caplen
, NULL
, NULL
);
462 * This is the top level routine of the printer. 'p' points
463 * to the ether header of the packet, 'h->len' is the length
464 * of the packet off the wire, and 'h->caplen' is the number
465 * of bytes actually captured.
467 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
468 * before the Ethernet header.
471 netanalyzer_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
475 * Fail if we don't have enough data for the Hilscher pseudo-header.
477 ndo
->ndo_protocol
= "netanalyzer";
480 /* Skip the pseudo-header. */
481 ndo
->ndo_ll_hdr_len
+= 4;
482 ndo
->ndo_ll_hdr_len
+=
483 ether_print(ndo
, p
+ 4, h
->len
- 4, h
->caplen
- 4, NULL
, NULL
);
487 * This is the top level routine of the printer. 'p' points
488 * to the ether header of the packet, 'h->len' is the length
489 * of the packet off the wire, and 'h->caplen' is the number
490 * of bytes actually captured.
492 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
493 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
494 * before the Ethernet header.
497 netanalyzer_transparent_if_print(netdissect_options
*ndo
,
498 const struct pcap_pkthdr
*h
,
502 * Fail if we don't have enough data for the Hilscher pseudo-header,
505 ndo
->ndo_protocol
= "netanalyzer_transparent";
506 ND_TCHECK_LEN(p
, 12);
508 /* Skip the pseudo-header, preamble, and SOF. */
509 ndo
->ndo_ll_hdr_len
+= 12;
510 ndo
->ndo_ll_hdr_len
+=
511 ether_print(ndo
, p
+ 12, h
->len
- 12, h
->caplen
- 12, NULL
, NULL
);
515 * Prints the packet payload, given an Ethernet type code for the payload's
518 * Returns non-zero if it can do so, zero if the ethertype is unknown.
522 ethertype_print(netdissect_options
*ndo
,
523 u_short ether_type
, const u_char
*p
,
524 u_int length
, u_int caplen
,
525 const struct lladdr_info
*src
, const struct lladdr_info
*dst
)
527 switch (ether_type
) {
530 ip_print(ndo
, p
, length
);
534 ip6_print(ndo
, p
, length
);
538 case ETHERTYPE_REVARP
:
539 arp_print(ndo
, p
, length
, caplen
);
543 decnet_print(ndo
, p
, length
, caplen
);
546 case ETHERTYPE_ATALK
:
549 atalk_print(ndo
, p
, length
);
553 aarp_print(ndo
, p
, length
);
557 ND_PRINT("(NOV-ETHII) ");
558 ipx_print(ndo
, p
, length
);
562 if (length
== 0 || caplen
== 0) {
563 ndo
->ndo_protocol
= "isoclns";
567 /* At least one byte is required */
568 /* FIXME: Reference for this byte? */
570 isoclns_print(ndo
, p
+ 1, length
- 1);
573 case ETHERTYPE_PPPOED
:
574 case ETHERTYPE_PPPOES
:
575 case ETHERTYPE_PPPOED2
:
576 case ETHERTYPE_PPPOES2
:
577 pppoe_print(ndo
, p
, length
);
580 case ETHERTYPE_EAPOL
:
585 rrcp_print(ndo
, p
, length
, src
, dst
);
591 ppp_print(ndo
, p
, length
);
596 mpcp_print(ndo
, p
, length
);
600 slow_print(ndo
, p
, length
);
604 case ETHERTYPE_CFM_OLD
:
605 cfm_print(ndo
, p
, length
);
609 lldp_print(ndo
, p
, length
);
613 nsh_print(ndo
, p
, length
);
616 case ETHERTYPE_LOOPBACK
:
617 loopback_print(ndo
, p
, length
);
621 case ETHERTYPE_MPLS_MULTI
:
622 mpls_print(ndo
, p
, length
);
626 tipc_print(ndo
, p
, length
, caplen
);
629 case ETHERTYPE_MS_NLB_HB
:
633 case ETHERTYPE_GEONET_OLD
:
634 case ETHERTYPE_GEONET
:
635 geonet_print(ndo
, p
, length
, src
);
638 case ETHERTYPE_CALM_FAST
:
639 calm_fast_print(ndo
, p
, length
, src
);
643 aoe_print(ndo
, p
, length
);
647 ptp_print(ndo
, p
, length
);
652 case ETHERTYPE_MOPRC
:
653 case ETHERTYPE_MOPDL
:
654 case ETHERTYPE_IEEE1905_1
:
655 /* default_print for now */