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 #include "netdissect.h"
32 #include "addrtoname.h"
33 #include "ethertype.h"
36 * Structure of an Ethernet header.
39 nd_mac_addr ether_dhost
;
40 nd_mac_addr ether_shost
;
41 nd_uint16_t ether_length_type
;
45 * Length of an Ethernet header; note that some compilers may pad
46 * "struct ether_header" to a multiple of 4 bytes, for example, so
47 * "sizeof (struct ether_header)" may not give the right answer.
49 #define ETHER_HDRLEN 14
51 const struct tok ethertype_values
[] = {
52 { ETHERTYPE_IP
, "IPv4" },
53 { ETHERTYPE_MPLS
, "MPLS unicast" },
54 { ETHERTYPE_MPLS_MULTI
, "MPLS multicast" },
55 { ETHERTYPE_IPV6
, "IPv6" },
56 { ETHERTYPE_8021Q
, "802.1Q" },
57 { ETHERTYPE_8021Q9100
, "802.1Q-9100" },
58 { ETHERTYPE_8021QinQ
, "802.1Q-QinQ" },
59 { ETHERTYPE_8021Q9200
, "802.1Q-9200" },
60 { ETHERTYPE_MACSEC
, "802.1AE MACsec" },
61 { ETHERTYPE_VMAN
, "VMAN" },
62 { ETHERTYPE_PUP
, "PUP" },
63 { ETHERTYPE_ARP
, "ARP"},
64 { ETHERTYPE_REVARP
, "Reverse ARP"},
65 { ETHERTYPE_NS
, "NS" },
66 { ETHERTYPE_SPRITE
, "Sprite" },
67 { ETHERTYPE_TRAIL
, "Trail" },
68 { ETHERTYPE_MOPDL
, "MOP DL" },
69 { ETHERTYPE_MOPRC
, "MOP RC" },
70 { ETHERTYPE_DN
, "DN" },
71 { ETHERTYPE_LAT
, "LAT" },
72 { ETHERTYPE_SCA
, "SCA" },
73 { ETHERTYPE_TEB
, "TEB" },
74 { ETHERTYPE_LANBRIDGE
, "Lanbridge" },
75 { ETHERTYPE_DECDNS
, "DEC DNS" },
76 { ETHERTYPE_DECDTS
, "DEC DTS" },
77 { ETHERTYPE_VEXP
, "VEXP" },
78 { ETHERTYPE_VPROD
, "VPROD" },
79 { ETHERTYPE_ATALK
, "Appletalk" },
80 { ETHERTYPE_AARP
, "Appletalk ARP" },
81 { ETHERTYPE_IPX
, "IPX" },
82 { ETHERTYPE_PPP
, "PPP" },
83 { ETHERTYPE_MPCP
, "MPCP" },
84 { ETHERTYPE_SLOW
, "Slow Protocols" },
85 { ETHERTYPE_PPPOED
, "PPPoE D" },
86 { ETHERTYPE_PPPOES
, "PPPoE S" },
87 { ETHERTYPE_EAPOL
, "EAPOL" },
88 { ETHERTYPE_RRCP
, "RRCP" },
89 { ETHERTYPE_MS_NLB_HB
, "MS NLB heartbeat" },
90 { ETHERTYPE_JUMBO
, "Jumbo" },
91 { ETHERTYPE_NSH
, "NSH" },
92 { ETHERTYPE_LOOPBACK
, "Loopback" },
93 { ETHERTYPE_ISO
, "OSI" },
94 { ETHERTYPE_GRE_ISO
, "GRE-OSI" },
95 { ETHERTYPE_CFM_OLD
, "CFM (old)" },
96 { ETHERTYPE_CFM
, "CFM" },
97 { ETHERTYPE_IEEE1905_1
, "IEEE1905.1" },
98 { ETHERTYPE_LLDP
, "LLDP" },
99 { ETHERTYPE_TIPC
, "TIPC"},
100 { ETHERTYPE_GEONET_OLD
, "GeoNet (old)"},
101 { ETHERTYPE_GEONET
, "GeoNet"},
102 { ETHERTYPE_CALM_FAST
, "CALM FAST"},
103 { ETHERTYPE_AOE
, "AoE" },
104 { ETHERTYPE_PTP
, "PTP" },
105 { ETHERTYPE_ARISTA
, "Arista Vendor Specific Protocol" },
110 ether_addresses_print(netdissect_options
*ndo
, const u_char
*src
,
113 ND_PRINT("%s > %s, ",
114 GET_ETHERADDR_STRING(src
), GET_ETHERADDR_STRING(dst
));
118 ether_type_print(netdissect_options
*ndo
, uint16_t type
)
121 ND_PRINT("ethertype %s (0x%04x)",
122 tok2str(ethertype_values
, "Unknown", type
), type
);
125 tok2str(ethertype_values
, "Unknown Ethertype (0x%04x)", type
));
129 * Common code for printing Ethernet frames.
131 * It can handle Ethernet headers with extra tag information inserted
132 * after the destination and source addresses, as is inserted by some
133 * switch chips, and extra encapsulation header information before
134 * printing Ethernet header information (such as a LANE ID for ATM LANE).
137 ether_common_print(netdissect_options
*ndo
, const u_char
*p
, u_int length
,
139 void (*print_switch_tag
)(netdissect_options
*ndo
, const u_char
*),
140 u_int switch_tag_len
,
141 void (*print_encap_header
)(netdissect_options
*ndo
, const u_char
*),
142 const u_char
*encap_header_arg
, u_int do_incr_ll_hdr_len
)
144 const struct ether_header
*ehp
;
150 struct lladdr_info src
, dst
;
152 if (caplen
< ETHER_HDRLEN
+ switch_tag_len
) {
154 if (do_incr_ll_hdr_len
)
155 ndo
->ndo_ll_hdr_len
+= caplen
;
158 if (length
< ETHER_HDRLEN
+ switch_tag_len
) {
160 if (do_incr_ll_hdr_len
)
161 ndo
->ndo_ll_hdr_len
+= length
;
165 if (print_encap_header
!= NULL
)
166 (*print_encap_header
)(ndo
, encap_header_arg
);
168 orig_length
= length
;
171 * Get the source and destination addresses, skip past them,
172 * and print them if we're printing the link-layer header.
174 ehp
= (const struct ether_header
*)p
;
175 src
.addr
= ehp
->ether_shost
;
176 src
.addr_string
= etheraddr_string
;
177 dst
.addr
= ehp
->ether_dhost
;
178 dst
.addr_string
= etheraddr_string
;
180 length
-= 2*MAC_ADDR_LEN
;
181 caplen
-= 2*MAC_ADDR_LEN
;
183 hdrlen
= 2*MAC_ADDR_LEN
;
186 ether_addresses_print(ndo
, src
.addr
, dst
.addr
);
189 * Print the switch tag, if we have one, and skip past it.
191 if (print_switch_tag
!= NULL
)
192 (*print_switch_tag
)(ndo
, p
);
194 length
-= switch_tag_len
;
195 caplen
-= switch_tag_len
;
197 hdrlen
+= switch_tag_len
;
200 * Get the length/type field, skip past it, and print it
201 * if we're printing the link-layer header.
204 length_type
= GET_BE_U_2(p
);
212 * Process 802.1AE MACsec headers.
215 if (length_type
== ETHERTYPE_MACSEC
) {
217 * MACsec, aka IEEE 802.1AE-2006
218 * Print the header, and try to print the payload if it's not encrypted
220 if (ndo
->ndo_eflag
) {
221 ether_type_print(ndo
, length_type
);
222 ND_PRINT(", length %u: ", orig_length
);
226 int ret
= macsec_print(ndo
, &p
, &length
, &caplen
, &hdrlen
,
230 /* Payload is encrypted; print it as raw data. */
231 if (!ndo
->ndo_suppress_default_print
)
232 ND_DEFAULTPRINT(p
, caplen
);
233 if (do_incr_ll_hdr_len
)
234 ndo
->ndo_ll_hdr_len
+= hdrlen
;
236 } else if (ret
> 0) {
237 /* Problem printing the header; just quit. */
238 if (do_incr_ll_hdr_len
)
239 ndo
->ndo_ll_hdr_len
+= ret
;
243 * Keep processing type/length fields.
245 length_type
= GET_BE_U_2(p
);
255 * Process VLAN tag types.
257 while (length_type
== ETHERTYPE_8021Q
||
258 length_type
== ETHERTYPE_8021Q9100
||
259 length_type
== ETHERTYPE_8021Q9200
||
260 length_type
== ETHERTYPE_8021QinQ
) {
263 * Print VLAN information, and then go back and process
264 * the enclosed type field.
267 ndo
->ndo_protocol
= "vlan";
269 if (do_incr_ll_hdr_len
)
270 ndo
->ndo_ll_hdr_len
+= hdrlen
+ caplen
;
274 ndo
->ndo_protocol
= "vlan";
276 if (do_incr_ll_hdr_len
)
277 ndo
->ndo_ll_hdr_len
+= hdrlen
+ length
;
280 if (ndo
->ndo_eflag
) {
281 uint16_t tag
= GET_BE_U_2(p
);
283 ether_type_print(ndo
, length_type
);
284 if (!printed_length
) {
285 ND_PRINT(", length %u: ", orig_length
);
289 ND_PRINT("%s, ", ieee8021q_tci_string(tag
));
292 length_type
= GET_BE_U_2(p
+ 2);
300 * We now have the final length/type field.
302 if (length_type
<= MAX_ETHERNET_LENGTH_VAL
) {
304 * It's a length field, containing the length of the
305 * remaining payload; use it as such, as long as
306 * it's not too large (bigger than the actual payload).
308 if (length_type
< length
) {
309 length
= length_type
;
315 * Cut off the snapshot length to the end of the
318 nd_push_snapend(ndo
, p
+ length
);
320 if (ndo
->ndo_eflag
) {
323 ND_PRINT(", length %u: ", length
);
327 * An LLC header follows the length. Print that and
330 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, &src
, &dst
);
331 if (llc_hdrlen
< 0) {
332 /* packet type not known, print raw packet */
333 if (!ndo
->ndo_suppress_default_print
)
334 ND_DEFAULTPRINT(p
, caplen
);
335 llc_hdrlen
= -llc_hdrlen
;
337 hdrlen
+= llc_hdrlen
;
338 nd_pop_packet_info(ndo
);
339 } else if (length_type
== ETHERTYPE_JUMBO
) {
341 * It's a type field, with the type for Alteon jumbo frames.
344 * https://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
346 * which indicates that, following the type field,
347 * there's an LLC header and payload.
349 /* Try to print the LLC-layer header & higher layers */
350 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, &src
, &dst
);
351 if (llc_hdrlen
< 0) {
352 /* packet type not known, print raw packet */
353 if (!ndo
->ndo_suppress_default_print
)
354 ND_DEFAULTPRINT(p
, caplen
);
355 llc_hdrlen
= -llc_hdrlen
;
357 hdrlen
+= llc_hdrlen
;
358 } else if (length_type
== ETHERTYPE_ARISTA
) {
360 ND_PRINT("[|arista]");
361 if (do_incr_ll_hdr_len
)
362 ndo
->ndo_ll_hdr_len
+= hdrlen
+ caplen
;
366 ND_PRINT("[|arista]");
367 if (do_incr_ll_hdr_len
)
368 ndo
->ndo_ll_hdr_len
+= hdrlen
+ length
;
371 ether_type_print(ndo
, length_type
);
372 ND_PRINT(", length %u: ", orig_length
);
373 int bytesConsumed
= arista_ethertype_print(ndo
, p
, length
);
374 if (bytesConsumed
> 0) {
376 length
-= bytesConsumed
;
377 caplen
-= bytesConsumed
;
378 hdrlen
+= bytesConsumed
;
381 /* subtype/version not known, print raw packet */
382 if (!ndo
->ndo_eflag
&& length_type
> MAX_ETHERNET_LENGTH_VAL
) {
383 ether_addresses_print(ndo
, src
.addr
, dst
.addr
);
384 ether_type_print(ndo
, length_type
);
385 ND_PRINT(", length %u: ", orig_length
);
387 if (!ndo
->ndo_suppress_default_print
)
388 ND_DEFAULTPRINT(p
, caplen
);
392 * It's a type field with some other value.
394 if (ndo
->ndo_eflag
) {
395 ether_type_print(ndo
, length_type
);
397 ND_PRINT(", length %u: ", orig_length
);
401 if (ethertype_print(ndo
, length_type
, p
, length
, caplen
, &src
, &dst
) == 0) {
402 /* type not known, print raw packet */
403 if (!ndo
->ndo_eflag
) {
405 * We didn't print the full link-layer
406 * header, as -e wasn't specified, so
407 * print only the source and destination
408 * MAC addresses and the final Ethernet
411 ether_addresses_print(ndo
, src
.addr
, dst
.addr
);
412 ether_type_print(ndo
, length_type
);
413 ND_PRINT(", length %u: ", orig_length
);
416 if (!ndo
->ndo_suppress_default_print
)
417 ND_DEFAULTPRINT(p
, caplen
);
420 if (do_incr_ll_hdr_len
)
421 ndo
->ndo_ll_hdr_len
+= hdrlen
;
426 * Print an Ethernet frame while specyfing a non-standard Ethernet header
428 * This might be encapsulated within another frame; we might be passed
429 * a pointer to a function that can print header information for that
430 * frame's protocol, and an argument to pass to that function.
432 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
435 ether_switch_tag_print(netdissect_options
*ndo
, const u_char
*p
, u_int length
,
437 void (*print_switch_tag
)(netdissect_options
*, const u_char
*),
438 u_int switch_tag_len
, u_int do_incr_ll_hdr_len
)
440 ether_common_print(ndo
, p
, length
, caplen
, print_switch_tag
,
441 switch_tag_len
, NULL
, NULL
, do_incr_ll_hdr_len
);
445 * Print an Ethernet frame.
446 * This might be encapsulated within another frame; we might be passed
447 * a pointer to a function that can print header information for that
448 * frame's protocol, and an argument to pass to that function.
450 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
453 ether_print(netdissect_options
*ndo
,
454 const u_char
*p
, u_int length
, u_int caplen
,
455 void (*print_encap_header
)(netdissect_options
*ndo
, const u_char
*),
456 const u_char
*encap_header_arg
, u_int do_incr_ll_hdr_len
)
458 ndo
->ndo_protocol
= "ether";
459 ether_common_print(ndo
, p
, length
, caplen
, NULL
, 0,
460 print_encap_header
, encap_header_arg
, do_incr_ll_hdr_len
);
464 * This is the top level routine of the printer. 'p' points
465 * to the ether header of the packet, 'h->len' is the length
466 * of the packet off the wire, and 'h->caplen' is the number
467 * of bytes actually captured.
470 ether_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
473 ndo
->ndo_protocol
= "ether";
474 ndo
->ndo_ll_hdr_len
+= 0;
476 ether_print(ndo
, p
, h
->len
, h
->caplen
, NULL
, NULL
, TRUE
);
480 * This is the top level routine of the printer. 'p' points
481 * to the ether header of the packet, 'h->len' is the length
482 * of the packet off the wire, and 'h->caplen' is the number
483 * of bytes actually captured.
485 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
486 * before the Ethernet header.
489 netanalyzer_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
493 * Fail if we don't have enough data for the Hilscher pseudo-header.
495 ndo
->ndo_protocol
= "netanalyzer";
497 ndo
->ndo_ll_hdr_len
+= h
->caplen
;
501 ndo
->ndo_ll_hdr_len
+= 4;
503 /* Skip the pseudo-header. */
504 ether_print(ndo
, p
+ 4, h
->len
- 4, h
->caplen
- 4, NULL
, NULL
, TRUE
);
508 * This is the top level routine of the printer. 'p' points
509 * to the ether header of the packet, 'h->len' is the length
510 * of the packet off the wire, and 'h->caplen' is the number
511 * of bytes actually captured.
513 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
514 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
515 * before the Ethernet header.
518 netanalyzer_transparent_if_print(netdissect_options
*ndo
,
519 const struct pcap_pkthdr
*h
,
523 * Fail if we don't have enough data for the Hilscher pseudo-header,
526 ndo
->ndo_protocol
= "netanalyzer_transparent";
527 if (h
->caplen
< 12) {
528 ndo
->ndo_ll_hdr_len
+= h
->caplen
;
532 ndo
->ndo_ll_hdr_len
+= 12;
534 /* Skip the pseudo-header, preamble, and SOF. */
535 ether_print(ndo
, p
+ 12, h
->len
- 12, h
->caplen
- 12, NULL
, NULL
, TRUE
);
539 * Prints the packet payload, given an Ethernet type code for the payload's
542 * Returns non-zero if it can do so, zero if the ethertype is unknown.
546 ethertype_print(netdissect_options
*ndo
,
547 u_short ether_type
, const u_char
*p
,
548 u_int length
, u_int caplen
,
549 const struct lladdr_info
*src
, const struct lladdr_info
*dst
)
551 switch (ether_type
) {
554 ip_print(ndo
, p
, length
);
558 ip6_print(ndo
, p
, length
);
562 case ETHERTYPE_REVARP
:
563 arp_print(ndo
, p
, length
, caplen
);
567 decnet_print(ndo
, p
, length
, caplen
);
570 case ETHERTYPE_ATALK
:
573 atalk_print(ndo
, p
, length
);
577 aarp_print(ndo
, p
, length
);
581 ND_PRINT("(NOV-ETHII) ");
582 ipx_print(ndo
, p
, length
);
586 if (length
== 0 || caplen
== 0) {
587 ndo
->ndo_protocol
= "isoclns";
591 isoclns_print(ndo
, p
+ 1, length
- 1);
594 case ETHERTYPE_PPPOED
:
595 case ETHERTYPE_PPPOES
:
596 case ETHERTYPE_PPPOED2
:
597 case ETHERTYPE_PPPOES2
:
598 pppoe_print(ndo
, p
, length
);
601 case ETHERTYPE_EAPOL
:
606 rrcp_print(ndo
, p
, length
, src
, dst
);
612 ppp_print(ndo
, p
, length
);
617 mpcp_print(ndo
, p
, length
);
621 slow_print(ndo
, p
, length
);
625 case ETHERTYPE_CFM_OLD
:
626 cfm_print(ndo
, p
, length
);
630 lldp_print(ndo
, p
, length
);
634 nsh_print(ndo
, p
, length
);
637 case ETHERTYPE_LOOPBACK
:
638 loopback_print(ndo
, p
, length
);
642 case ETHERTYPE_MPLS_MULTI
:
643 mpls_print(ndo
, p
, length
);
647 tipc_print(ndo
, p
, length
, caplen
);
650 case ETHERTYPE_MS_NLB_HB
:
654 case ETHERTYPE_GEONET_OLD
:
655 case ETHERTYPE_GEONET
:
656 geonet_print(ndo
, p
, length
, src
);
659 case ETHERTYPE_CALM_FAST
:
660 calm_fast_print(ndo
, p
, length
, src
);
664 aoe_print(ndo
, p
, length
);
668 ptp_print(ndo
, p
, length
);
673 case ETHERTYPE_MOPRC
:
674 case ETHERTYPE_MOPDL
:
675 case ETHERTYPE_IEEE1905_1
:
676 /* default_print for now */