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
)
144 const struct ether_header
*ehp
;
150 struct lladdr_info src
, dst
;
152 if (caplen
< ETHER_HDRLEN
+ switch_tag_len
) {
156 if (length
< 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
= etheraddr_string
;
173 dst
.addr
= ehp
->ether_dhost
;
174 dst
.addr_string
= etheraddr_string
;
176 length
-= 2*MAC_ADDR_LEN
;
177 caplen
-= 2*MAC_ADDR_LEN
;
179 hdrlen
= 2*MAC_ADDR_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
);
247 * Process VLAN tag types.
249 while (length_type
== ETHERTYPE_8021Q
||
250 length_type
== ETHERTYPE_8021Q9100
||
251 length_type
== ETHERTYPE_8021Q9200
||
252 length_type
== ETHERTYPE_8021QinQ
) {
255 * Print VLAN information, and then go back and process
256 * the enclosed type field.
259 ndo
->ndo_protocol
= "vlan";
261 return hdrlen
+ caplen
;
264 ndo
->ndo_protocol
= "vlan";
266 return hdrlen
+ length
;
268 if (ndo
->ndo_eflag
) {
269 uint16_t tag
= GET_BE_U_2(p
);
271 ether_type_print(ndo
, length_type
);
272 if (!printed_length
) {
273 ND_PRINT(", length %u: ", orig_length
);
277 ND_PRINT("%s, ", ieee8021q_tci_string(tag
));
280 length_type
= GET_BE_U_2(p
+ 2);
288 * We now have the final length/type field.
290 if (length_type
<= MAX_ETHERNET_LENGTH_VAL
) {
292 * It's a length field, containing the length of the
293 * remaining payload; use it as such, as long as
294 * it's not too large (bigger than the actual payload).
296 if (length_type
< length
) {
297 length
= length_type
;
303 * Cut off the snapshot length to the end of the
306 nd_push_snapend(ndo
, p
+ length
);
308 if (ndo
->ndo_eflag
) {
311 ND_PRINT(", length %u: ", length
);
315 * An LLC header follows the length. Print that and
318 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, &src
, &dst
);
319 if (llc_hdrlen
< 0) {
320 /* packet type not known, print raw packet */
321 if (!ndo
->ndo_suppress_default_print
)
322 ND_DEFAULTPRINT(p
, caplen
);
323 llc_hdrlen
= -llc_hdrlen
;
325 hdrlen
+= llc_hdrlen
;
326 nd_pop_packet_info(ndo
);
327 } else if (length_type
== ETHERTYPE_JUMBO
) {
329 * It's a type field, with the type for Alteon jumbo frames.
332 * https://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
334 * which indicates that, following the type field,
335 * there's an LLC header and payload.
337 /* Try to print the LLC-layer header & higher layers */
338 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, &src
, &dst
);
339 if (llc_hdrlen
< 0) {
340 /* packet type not known, print raw packet */
341 if (!ndo
->ndo_suppress_default_print
)
342 ND_DEFAULTPRINT(p
, caplen
);
343 llc_hdrlen
= -llc_hdrlen
;
345 hdrlen
+= llc_hdrlen
;
346 } else if (length_type
== ETHERTYPE_ARISTA
) {
348 ND_PRINT("[|arista]");
349 return hdrlen
+ caplen
;
352 ND_PRINT("[|arista]");
353 return hdrlen
+ length
;
355 ether_type_print(ndo
, length_type
);
356 ND_PRINT(", length %u: ", orig_length
);
357 int bytesConsumed
= arista_ethertype_print(ndo
, p
, length
);
358 if (bytesConsumed
> 0) {
360 length
-= bytesConsumed
;
361 caplen
-= bytesConsumed
;
362 hdrlen
+= bytesConsumed
;
365 /* subtype/version not known, print raw packet */
366 if (!ndo
->ndo_eflag
&& length_type
> MAX_ETHERNET_LENGTH_VAL
) {
367 ether_addresses_print(ndo
, src
.addr
, dst
.addr
);
368 ether_type_print(ndo
, length_type
);
369 ND_PRINT(", length %u: ", orig_length
);
371 if (!ndo
->ndo_suppress_default_print
)
372 ND_DEFAULTPRINT(p
, caplen
);
376 * It's a type field with some other value.
378 if (ndo
->ndo_eflag
) {
379 ether_type_print(ndo
, length_type
);
381 ND_PRINT(", length %u: ", orig_length
);
385 if (ethertype_print(ndo
, length_type
, p
, length
, caplen
, &src
, &dst
) == 0) {
386 /* type not known, print raw packet */
387 if (!ndo
->ndo_eflag
) {
389 * We didn't print the full link-layer
390 * header, as -e wasn't specified, so
391 * print only the source and destination
392 * MAC addresses and the final Ethernet
395 ether_addresses_print(ndo
, src
.addr
, dst
.addr
);
396 ether_type_print(ndo
, length_type
);
397 ND_PRINT(", length %u: ", orig_length
);
400 if (!ndo
->ndo_suppress_default_print
)
401 ND_DEFAULTPRINT(p
, caplen
);
408 * Print an Ethernet frame while specyfing a non-standard Ethernet header
410 * This might be encapsulated within another frame; we might be passed
411 * a pointer to a function that can print header information for that
412 * frame's protocol, and an argument to pass to that function.
414 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
417 ether_switch_tag_print(netdissect_options
*ndo
, const u_char
*p
, u_int length
,
419 void (*print_switch_tag
)(netdissect_options
*, const u_char
*),
420 u_int switch_tag_len
)
422 return ether_common_print(ndo
, p
, length
, caplen
, print_switch_tag
,
423 switch_tag_len
, NULL
, NULL
);
427 * Print an Ethernet frame.
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_print(netdissect_options
*ndo
,
436 const u_char
*p
, u_int length
, u_int caplen
,
437 void (*print_encap_header
)(netdissect_options
*ndo
, const u_char
*),
438 const u_char
*encap_header_arg
)
440 ndo
->ndo_protocol
= "ether";
441 return ether_common_print(ndo
, p
, length
, caplen
, NULL
, 0,
442 print_encap_header
, encap_header_arg
);
446 * This is the top level routine of the printer. 'p' points
447 * to the ether header of the packet, 'h->len' is the length
448 * of the packet off the wire, and 'h->caplen' is the number
449 * of bytes actually captured.
452 ether_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
455 ndo
->ndo_protocol
= "ether";
456 ndo
->ndo_ll_hdr_len
+=
457 ether_print(ndo
, p
, h
->len
, h
->caplen
, NULL
, NULL
);
461 * This is the top level routine of the printer. 'p' points
462 * to the ether header of the packet, 'h->len' is the length
463 * of the packet off the wire, and 'h->caplen' is the number
464 * of bytes actually captured.
466 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
467 * before the Ethernet header.
470 netanalyzer_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
474 * Fail if we don't have enough data for the Hilscher pseudo-header.
476 ndo
->ndo_protocol
= "netanalyzer";
478 ndo
->ndo_ll_hdr_len
+= h
->caplen
;
483 /* Skip the pseudo-header. */
484 ndo
->ndo_ll_hdr_len
+= 4;
485 ndo
->ndo_ll_hdr_len
+=
486 ether_print(ndo
, p
+ 4, h
->len
- 4, h
->caplen
- 4, NULL
, NULL
);
490 * This is the top level routine of the printer. 'p' points
491 * to the ether header of the packet, 'h->len' is the length
492 * of the packet off the wire, and 'h->caplen' is the number
493 * of bytes actually captured.
495 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
496 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
497 * before the Ethernet header.
500 netanalyzer_transparent_if_print(netdissect_options
*ndo
,
501 const struct pcap_pkthdr
*h
,
505 * Fail if we don't have enough data for the Hilscher pseudo-header,
508 ndo
->ndo_protocol
= "netanalyzer_transparent";
509 if (h
->caplen
< 12) {
510 ndo
->ndo_ll_hdr_len
+= h
->caplen
;
515 /* Skip the pseudo-header, preamble, and SOF. */
516 ndo
->ndo_ll_hdr_len
+= 12;
517 ndo
->ndo_ll_hdr_len
+=
518 ether_print(ndo
, p
+ 12, h
->len
- 12, h
->caplen
- 12, NULL
, NULL
);
522 * Prints the packet payload, given an Ethernet type code for the payload's
525 * Returns non-zero if it can do so, zero if the ethertype is unknown.
529 ethertype_print(netdissect_options
*ndo
,
530 u_short ether_type
, const u_char
*p
,
531 u_int length
, u_int caplen
,
532 const struct lladdr_info
*src
, const struct lladdr_info
*dst
)
534 switch (ether_type
) {
537 ip_print(ndo
, p
, length
);
541 ip6_print(ndo
, p
, length
);
545 case ETHERTYPE_REVARP
:
546 arp_print(ndo
, p
, length
, caplen
);
550 decnet_print(ndo
, p
, length
, caplen
);
553 case ETHERTYPE_ATALK
:
556 atalk_print(ndo
, p
, length
);
560 aarp_print(ndo
, p
, length
);
564 ND_PRINT("(NOV-ETHII) ");
565 ipx_print(ndo
, p
, length
);
569 if (length
== 0 || caplen
== 0) {
570 ndo
->ndo_protocol
= "isoclns";
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
:
589 rrcp_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 */