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_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_RRCP
, "RRCP" },
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_MEDSA
, "MEDSA" },
108 ether_hdr_print(netdissect_options
*ndo
,
109 const u_char
*bp
, u_int length
)
111 const struct ether_header
*ehp
;
112 uint16_t length_type
;
114 ehp
= (const struct ether_header
*)bp
;
117 etheraddr_string(ndo
, ehp
->ether_shost
),
118 etheraddr_string(ndo
, ehp
->ether_dhost
));
120 length_type
= EXTRACT_BE_U_2(ehp
->ether_length_type
);
121 if (!ndo
->ndo_qflag
) {
122 if (length_type
<= MAX_ETHERNET_LENGTH_VAL
) {
124 length
= length_type
;
126 ND_PRINT(", ethertype %s (0x%04x)",
127 tok2str(ethertype_values
,"Unknown", length_type
),
130 if (length_type
<= MAX_ETHERNET_LENGTH_VAL
) {
132 length
= length_type
;
134 ND_PRINT(", %s", tok2str(ethertype_values
,"Unknown Ethertype (0x%04x)", length_type
));
137 ND_PRINT(", length %u: ", length
);
141 * Print an Ethernet frame.
142 * This might be encapsulated within another frame; we might be passed
143 * a pointer to a function that can print header information for that
144 * frame's protocol, and an argument to pass to that function.
146 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
149 ether_print(netdissect_options
*ndo
,
150 const u_char
*p
, u_int length
, u_int caplen
,
151 void (*print_encap_header
)(netdissect_options
*ndo
, const u_char
*), const u_char
*encap_header_arg
)
153 const struct ether_header
*ehp
;
158 struct lladdr_info src
, dst
;
160 ndo
->ndo_protocol
= "ether";
161 if (caplen
< ETHER_HDRLEN
) {
162 ND_PRINT("[|ether]");
165 if (length
< ETHER_HDRLEN
) {
166 ND_PRINT("[|ether]");
170 if (ndo
->ndo_eflag
) {
171 if (print_encap_header
!= NULL
)
172 (*print_encap_header
)(ndo
, encap_header_arg
);
173 ether_hdr_print(ndo
, p
, length
);
175 orig_length
= length
;
177 length
-= ETHER_HDRLEN
;
178 caplen
-= ETHER_HDRLEN
;
179 ehp
= (const struct ether_header
*)p
;
181 hdrlen
= ETHER_HDRLEN
;
183 src
.addr
= ehp
->ether_shost
;
184 src
.addr_string
= etheraddr_string
;
185 dst
.addr
= ehp
->ether_dhost
;
186 dst
.addr_string
= etheraddr_string
;
187 length_type
= EXTRACT_BE_U_2(ehp
->ether_length_type
);
191 * Is it (gag) an 802.3 encapsulation?
193 if (length_type
<= MAX_ETHERNET_LENGTH_VAL
) {
194 /* Try to print the LLC-layer header & higher layers */
195 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, &src
, &dst
);
196 if (llc_hdrlen
< 0) {
197 /* packet type not known, print raw packet */
198 if (!ndo
->ndo_suppress_default_print
)
199 ND_DEFAULTPRINT(p
, caplen
);
200 llc_hdrlen
= -llc_hdrlen
;
202 hdrlen
+= llc_hdrlen
;
203 } else if (length_type
== ETHERTYPE_8021Q
||
204 length_type
== ETHERTYPE_8021Q9100
||
205 length_type
== ETHERTYPE_8021Q9200
||
206 length_type
== ETHERTYPE_8021QinQ
) {
208 * Print VLAN information, and then go back and process
209 * the enclosed type field.
213 return (hdrlen
+ caplen
);
217 return (hdrlen
+ length
);
219 if (ndo
->ndo_eflag
) {
220 uint16_t tag
= EXTRACT_BE_U_2(p
);
222 ND_PRINT("%s, ", ieee8021q_tci_string(tag
));
225 length_type
= EXTRACT_BE_U_2(p
+ 2);
226 if (ndo
->ndo_eflag
&& length_type
> MAX_ETHERNET_LENGTH_VAL
)
227 ND_PRINT("ethertype %s, ", tok2str(ethertype_values
,"0x%04x", length_type
));
233 } else if (length_type
== ETHERTYPE_JUMBO
) {
235 * Alteon jumbo frames.
238 * http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
240 * which indicates that, following the type field,
241 * there's an LLC header and payload.
243 /* Try to print the LLC-layer header & higher layers */
244 llc_hdrlen
= llc_print(ndo
, p
, length
, caplen
, &src
, &dst
);
245 if (llc_hdrlen
< 0) {
246 /* packet type not known, print raw packet */
247 if (!ndo
->ndo_suppress_default_print
)
248 ND_DEFAULTPRINT(p
, caplen
);
249 llc_hdrlen
= -llc_hdrlen
;
251 hdrlen
+= llc_hdrlen
;
253 if (ethertype_print(ndo
, length_type
, p
, length
, caplen
, &src
, &dst
) == 0) {
254 /* type not known, print raw packet */
255 if (!ndo
->ndo_eflag
) {
256 if (print_encap_header
!= NULL
)
257 (*print_encap_header
)(ndo
, encap_header_arg
);
258 ether_hdr_print(ndo
, (const u_char
*)ehp
, orig_length
);
261 if (!ndo
->ndo_suppress_default_print
)
262 ND_DEFAULTPRINT(p
, caplen
);
269 * This is the top level routine of the printer. 'p' points
270 * to the ether header of the packet, 'h->len' is the length
271 * of the packet off the wire, and 'h->caplen' is the number
272 * of bytes actually captured.
275 ether_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
278 ndo
->ndo_protocol
= "ether_if";
279 return (ether_print(ndo
, p
, h
->len
, h
->caplen
, NULL
, NULL
));
283 * This is the top level routine of the printer. 'p' points
284 * to the ether header of the packet, 'h->len' is the length
285 * of the packet off the wire, and 'h->caplen' is the number
286 * of bytes actually captured.
288 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
289 * before the Ethernet header.
292 netanalyzer_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
296 * Fail if we don't have enough data for the Hilscher pseudo-header.
298 ndo
->ndo_protocol
= "netanalyzer_if";
299 if (h
->len
< 4 || h
->caplen
< 4) {
300 ND_PRINT("[|netanalyzer]");
304 /* Skip the pseudo-header. */
305 return (4 + ether_print(ndo
, p
+ 4, h
->len
- 4, h
->caplen
- 4, NULL
, NULL
));
309 * This is the top level routine of the printer. 'p' points
310 * to the ether header of the packet, 'h->len' is the length
311 * of the packet off the wire, and 'h->caplen' is the number
312 * of bytes actually captured.
314 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
315 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
316 * before the Ethernet header.
319 netanalyzer_transparent_if_print(netdissect_options
*ndo
,
320 const struct pcap_pkthdr
*h
,
324 * Fail if we don't have enough data for the Hilscher pseudo-header,
327 ndo
->ndo_protocol
= "netanalyzer_transparent_if";
328 if (h
->len
< 12 || h
->caplen
< 12) {
329 ND_PRINT("[|netanalyzer-transparent]");
333 /* Skip the pseudo-header, preamble, and SOF. */
334 return (12 + ether_print(ndo
, p
+ 12, h
->len
- 12, h
->caplen
- 12, NULL
, NULL
));
338 * Prints the packet payload, given an Ethernet type code for the payload's
341 * Returns non-zero if it can do so, zero if the ethertype is unknown.
345 ethertype_print(netdissect_options
*ndo
,
346 u_short ether_type
, const u_char
*p
,
347 u_int length
, u_int caplen
,
348 const struct lladdr_info
*src
, const struct lladdr_info
*dst
)
350 switch (ether_type
) {
353 ip_print(ndo
, p
, length
);
357 ip6_print(ndo
, p
, length
);
361 case ETHERTYPE_REVARP
:
362 arp_print(ndo
, p
, length
, caplen
);
366 decnet_print(ndo
, p
, length
, caplen
);
369 case ETHERTYPE_ATALK
:
372 atalk_print(ndo
, p
, length
);
376 aarp_print(ndo
, p
, length
);
380 ND_PRINT("(NOV-ETHII) ");
381 ipx_print(ndo
, p
, length
);
385 if (length
== 0 || caplen
== 0) {
389 isoclns_print(ndo
, p
+ 1, length
- 1);
392 case ETHERTYPE_PPPOED
:
393 case ETHERTYPE_PPPOES
:
394 case ETHERTYPE_PPPOED2
:
395 case ETHERTYPE_PPPOES2
:
396 pppoe_print(ndo
, p
, length
);
399 case ETHERTYPE_EAPOL
:
400 eap_print(ndo
, p
, length
);
404 rrcp_print(ndo
, p
, length
, src
, dst
);
410 ppp_print(ndo
, p
, length
);
415 mpcp_print(ndo
, p
, length
);
419 slow_print(ndo
, p
, length
);
423 case ETHERTYPE_CFM_OLD
:
424 cfm_print(ndo
, p
, length
);
428 lldp_print(ndo
, p
, length
);
432 nsh_print(ndo
, p
, length
);
435 case ETHERTYPE_LOOPBACK
:
436 loopback_print(ndo
, p
, length
);
440 case ETHERTYPE_MPLS_MULTI
:
441 mpls_print(ndo
, p
, length
);
445 tipc_print(ndo
, p
, length
, caplen
);
448 case ETHERTYPE_MS_NLB_HB
:
452 case ETHERTYPE_GEONET_OLD
:
453 case ETHERTYPE_GEONET
:
454 geonet_print(ndo
, p
, length
, src
);
457 case ETHERTYPE_CALM_FAST
:
458 calm_fast_print(ndo
, p
, length
, src
);
462 aoe_print(ndo
, p
, length
);
465 case ETHERTYPE_MEDSA
:
466 medsa_print(ndo
, p
, length
, caplen
, src
, dst
);
471 case ETHERTYPE_MOPRC
:
472 case ETHERTYPE_MOPDL
:
473 case ETHERTYPE_IEEE1905_1
:
474 /* default_print for now */