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
);
225 /* Payload is encrypted; print it as raw data. */
226 if (!ndo
->ndo_suppress_default_print
)
227 ND_DEFAULTPRINT(p
, caplen
);
229 } else if (ret
> 0) {
230 /* Problem printing the header; just quit. */
234 * Keep processing type/length fields.
236 length_type
= GET_BE_U_2(p
);
246 * Process VLAN tag types.
248 while (length_type
== ETHERTYPE_8021Q
||
249 length_type
== ETHERTYPE_8021Q9100
||
250 length_type
== ETHERTYPE_8021Q9200
||
251 length_type
== ETHERTYPE_8021QinQ
) {
254 * Print VLAN information, and then go back and process
255 * the enclosed type field.
258 ndo
->ndo_protocol
= "vlan";
260 return (hdrlen
+ caplen
);
263 ndo
->ndo_protocol
= "vlan";
265 return (hdrlen
+ length
);
267 if (ndo
->ndo_eflag
) {
268 uint16_t tag
= GET_BE_U_2(p
);
270 ether_type_print(ndo
, length_type
);
271 if (!printed_length
) {
272 ND_PRINT(", length %u: ", orig_length
);
276 ND_PRINT("%s, ", ieee8021q_tci_string(tag
));
279 length_type
= GET_BE_U_2(p
+ 2);
287 * We now have the final length/type field.
289 if (length_type
<= MAX_ETHERNET_LENGTH_VAL
) {
291 * It's a length field, containing the length of the
292 * remaining payload; use it as such, as long as
293 * it's not too large (bigger than the actual payload).
295 if (length_type
< length
) {
296 length
= length_type
;
302 * Cut off the snapshot length to the end of the
305 nd_push_snapend(ndo
, p
+ length
);
307 if (ndo
->ndo_eflag
) {
310 ND_PRINT(", length %u: ", length
);
314 * An LLC header follows the length. Print that and
317 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, &src
, &dst
);
318 if (llc_hdrlen
< 0) {
319 /* packet type not known, print raw packet */
320 if (!ndo
->ndo_suppress_default_print
)
321 ND_DEFAULTPRINT(p
, caplen
);
322 llc_hdrlen
= -llc_hdrlen
;
324 hdrlen
+= llc_hdrlen
;
325 nd_pop_packet_info(ndo
);
326 } else if (length_type
== ETHERTYPE_JUMBO
) {
328 * It's a type field, with the type for Alteon jumbo frames.
331 * https://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
333 * which indicates that, following the type field,
334 * there's an LLC header and payload.
336 /* Try to print the LLC-layer header & higher layers */
337 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, &src
, &dst
);
338 if (llc_hdrlen
< 0) {
339 /* packet type not known, print raw packet */
340 if (!ndo
->ndo_suppress_default_print
)
341 ND_DEFAULTPRINT(p
, caplen
);
342 llc_hdrlen
= -llc_hdrlen
;
344 hdrlen
+= llc_hdrlen
;
345 } else if (length_type
== ETHERTYPE_ARISTA
) {
347 ND_PRINT("[|arista]");
348 return (hdrlen
+ caplen
);
351 ND_PRINT("[|arista]");
352 return (hdrlen
+ length
);
354 ether_type_print(ndo
, length_type
);
355 ND_PRINT(", length %u: ", orig_length
);
356 int bytesConsumed
= arista_ethertype_print(ndo
, p
, length
);
357 if (bytesConsumed
> 0) {
359 length
-= bytesConsumed
;
360 caplen
-= bytesConsumed
;
361 hdrlen
+= bytesConsumed
;
364 /* subtype/version not known, print raw packet */
365 if (!ndo
->ndo_eflag
&& length_type
> MAX_ETHERNET_LENGTH_VAL
) {
366 ether_addresses_print(ndo
, src
.addr
, dst
.addr
);
367 ether_type_print(ndo
, length_type
);
368 ND_PRINT(", length %u: ", orig_length
);
370 if (!ndo
->ndo_suppress_default_print
)
371 ND_DEFAULTPRINT(p
, caplen
);
375 * It's a type field with some other value.
377 if (ndo
->ndo_eflag
) {
378 ether_type_print(ndo
, length_type
);
380 ND_PRINT(", length %u: ", orig_length
);
384 if (ethertype_print(ndo
, length_type
, p
, length
, caplen
, &src
, &dst
) == 0) {
385 /* type not known, print raw packet */
386 if (!ndo
->ndo_eflag
) {
388 * We didn't print the full link-layer
389 * header, as -e wasn't specified, so
390 * print only the source and destination
391 * MAC addresses and the final Ethernet
394 ether_addresses_print(ndo
, src
.addr
, dst
.addr
);
395 ether_type_print(ndo
, length_type
);
396 ND_PRINT(", length %u: ", orig_length
);
399 if (!ndo
->ndo_suppress_default_print
)
400 ND_DEFAULTPRINT(p
, caplen
);
407 * Print an Ethernet frame while specyfing a non-standard Ethernet header
409 * This might be encapsulated within another frame; we might be passed
410 * a pointer to a function that can print header information for that
411 * frame's protocol, and an argument to pass to that function.
413 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
416 ether_switch_tag_print(netdissect_options
*ndo
, const u_char
*p
, u_int length
,
418 void (*print_switch_tag
)(netdissect_options
*, const u_char
*),
419 u_int switch_tag_len
)
421 return (ether_common_print(ndo
, p
, length
, caplen
, print_switch_tag
,
422 switch_tag_len
, NULL
, NULL
));
426 * Print an Ethernet frame.
427 * This might be encapsulated within another frame; we might be passed
428 * a pointer to a function that can print header information for that
429 * frame's protocol, and an argument to pass to that function.
431 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
434 ether_print(netdissect_options
*ndo
,
435 const u_char
*p
, u_int length
, u_int caplen
,
436 void (*print_encap_header
)(netdissect_options
*ndo
, const u_char
*),
437 const u_char
*encap_header_arg
)
439 ndo
->ndo_protocol
= "ether";
440 return (ether_common_print(ndo
, p
, length
, caplen
, NULL
, 0,
441 print_encap_header
, encap_header_arg
));
445 * This is the top level routine of the printer. 'p' points
446 * to the ether header of the packet, 'h->len' is the length
447 * of the packet off the wire, and 'h->caplen' is the number
448 * of bytes actually captured.
451 ether_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
454 ndo
->ndo_protocol
= "ether_if";
455 return (ether_print(ndo
, p
, h
->len
, h
->caplen
, NULL
, NULL
));
459 * This is the top level routine of the printer. 'p' points
460 * to the ether header of the packet, 'h->len' is the length
461 * of the packet off the wire, and 'h->caplen' is the number
462 * of bytes actually captured.
464 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
465 * before the Ethernet header.
468 netanalyzer_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
472 * Fail if we don't have enough data for the Hilscher pseudo-header.
474 ndo
->ndo_protocol
= "netanalyzer_if";
480 /* Skip the pseudo-header. */
481 return (4 + ether_print(ndo
, p
+ 4, h
->len
- 4, h
->caplen
- 4, NULL
, NULL
));
485 * This is the top level routine of the printer. 'p' points
486 * to the ether header of the packet, 'h->len' is the length
487 * of the packet off the wire, and 'h->caplen' is the number
488 * of bytes actually captured.
490 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
491 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
492 * before the Ethernet header.
495 netanalyzer_transparent_if_print(netdissect_options
*ndo
,
496 const struct pcap_pkthdr
*h
,
500 * Fail if we don't have enough data for the Hilscher pseudo-header,
503 ndo
->ndo_protocol
= "netanalyzer_transparent_if";
504 if (h
->caplen
< 12) {
509 /* Skip the pseudo-header, preamble, and SOF. */
510 return (12 + ether_print(ndo
, p
+ 12, h
->len
- 12, h
->caplen
- 12, NULL
, NULL
));
514 * Prints the packet payload, given an Ethernet type code for the payload's
517 * Returns non-zero if it can do so, zero if the ethertype is unknown.
521 ethertype_print(netdissect_options
*ndo
,
522 u_short ether_type
, const u_char
*p
,
523 u_int length
, u_int caplen
,
524 const struct lladdr_info
*src
, const struct lladdr_info
*dst
)
526 switch (ether_type
) {
529 ip_print(ndo
, p
, length
);
533 ip6_print(ndo
, p
, length
);
537 case ETHERTYPE_REVARP
:
538 arp_print(ndo
, p
, length
, caplen
);
542 decnet_print(ndo
, p
, length
, caplen
);
545 case ETHERTYPE_ATALK
:
548 atalk_print(ndo
, p
, length
);
552 aarp_print(ndo
, p
, length
);
556 ND_PRINT("(NOV-ETHII) ");
557 ipx_print(ndo
, p
, length
);
561 if (length
== 0 || caplen
== 0) {
562 ndo
->ndo_protocol
= "isoclns";
566 isoclns_print(ndo
, p
+ 1, length
- 1);
569 case ETHERTYPE_PPPOED
:
570 case ETHERTYPE_PPPOES
:
571 case ETHERTYPE_PPPOED2
:
572 case ETHERTYPE_PPPOES2
:
573 pppoe_print(ndo
, p
, length
);
576 case ETHERTYPE_EAPOL
:
577 eap_print(ndo
, p
, length
);
581 rrcp_print(ndo
, p
, length
, src
, dst
);
587 ppp_print(ndo
, p
, length
);
592 mpcp_print(ndo
, p
, length
);
596 slow_print(ndo
, p
, length
);
600 case ETHERTYPE_CFM_OLD
:
601 cfm_print(ndo
, p
, length
);
605 lldp_print(ndo
, p
, length
);
609 nsh_print(ndo
, p
, length
);
612 case ETHERTYPE_LOOPBACK
:
613 loopback_print(ndo
, p
, length
);
617 case ETHERTYPE_MPLS_MULTI
:
618 mpls_print(ndo
, p
, length
);
622 tipc_print(ndo
, p
, length
, caplen
);
625 case ETHERTYPE_MS_NLB_HB
:
629 case ETHERTYPE_GEONET_OLD
:
630 case ETHERTYPE_GEONET
:
631 geonet_print(ndo
, p
, length
, src
);
634 case ETHERTYPE_CALM_FAST
:
635 calm_fast_print(ndo
, p
, length
, src
);
639 aoe_print(ndo
, p
, length
);
643 ptp_print(ndo
, p
, length
);
648 case ETHERTYPE_MOPRC
:
649 case ETHERTYPE_MOPDL
:
650 case ETHERTYPE_IEEE1905_1
:
651 /* default_print for now */