]> The Tcpdump Group git mirrors - tcpdump/blob - print-ether.c
Revert "Ethernet: Remove two useless tests"
[tcpdump] / print-ether.c
1 /*
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.
4 *
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
16 * written permission.
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.
20 */
21
22 /* \summary: Ethernet printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "netdissect-stdinc.h"
29
30 #include "netdissect.h"
31 #include "extract.h"
32 #include "addrtoname.h"
33 #include "ethertype.h"
34
35 /*
36 * Structure of an Ethernet header.
37 */
38 struct ether_header {
39 nd_mac_addr ether_dhost;
40 nd_mac_addr ether_shost;
41 nd_uint16_t ether_length_type;
42 };
43
44 /*
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.
48 */
49 #define ETHER_HDRLEN 14
50
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" },
104 { 0, NULL}
105 };
106
107 static void
108 ether_hdr_print(netdissect_options *ndo,
109 const u_char *bp, u_int length)
110 {
111 const struct ether_header *ehp;
112 uint16_t length_type;
113
114 ehp = (const struct ether_header *)bp;
115
116 ND_PRINT("%s > %s",
117 etheraddr_string(ndo, ehp->ether_shost),
118 etheraddr_string(ndo, ehp->ether_dhost));
119
120 length_type = EXTRACT_BE_U_2(ehp->ether_length_type);
121 if (!ndo->ndo_qflag) {
122 if (length_type <= MAX_ETHERNET_LENGTH_VAL) {
123 ND_PRINT(", 802.3");
124 length = length_type;
125 } else
126 ND_PRINT(", ethertype %s (0x%04x)",
127 tok2str(ethertype_values,"Unknown", length_type),
128 length_type);
129 } else {
130 if (length_type <= MAX_ETHERNET_LENGTH_VAL) {
131 ND_PRINT(", 802.3");
132 length = length_type;
133 } else
134 ND_PRINT(", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", length_type));
135 }
136
137 ND_PRINT(", length %u: ", length);
138 }
139
140 /*
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.
145 *
146 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
147 */
148 u_int
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 *),
152 const u_char *encap_header_arg)
153 {
154 const struct ether_header *ehp;
155 u_int orig_length;
156 u_short length_type;
157 u_int hdrlen;
158 int llc_hdrlen;
159 struct lladdr_info src, dst;
160
161 ndo->ndo_protocol = "ether";
162 if (caplen < ETHER_HDRLEN) {
163 nd_print_trunc(ndo);
164 return (caplen);
165 }
166 if (length < ETHER_HDRLEN) {
167 nd_print_trunc(ndo);
168 return (length);
169 }
170
171 if (ndo->ndo_eflag) {
172 if (print_encap_header != NULL)
173 (*print_encap_header)(ndo, encap_header_arg);
174 ether_hdr_print(ndo, p, length);
175 }
176 orig_length = length;
177
178 length -= ETHER_HDRLEN;
179 caplen -= ETHER_HDRLEN;
180 ehp = (const struct ether_header *)p;
181 p += ETHER_HDRLEN;
182 hdrlen = ETHER_HDRLEN;
183
184 src.addr = ehp->ether_shost;
185 src.addr_string = etheraddr_string;
186 dst.addr = ehp->ether_dhost;
187 dst.addr_string = etheraddr_string;
188 length_type = EXTRACT_BE_U_2(ehp->ether_length_type);
189
190 recurse:
191 /*
192 * Is it (gag) an 802.3 encapsulation?
193 */
194 if (length_type <= MAX_ETHERNET_LENGTH_VAL) {
195 /* Try to print the LLC-layer header & higher layers */
196 llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
197 if (llc_hdrlen < 0) {
198 /* packet type not known, print raw packet */
199 if (!ndo->ndo_suppress_default_print)
200 ND_DEFAULTPRINT(p, caplen);
201 llc_hdrlen = -llc_hdrlen;
202 }
203 hdrlen += llc_hdrlen;
204 } else if (length_type == ETHERTYPE_8021Q ||
205 length_type == ETHERTYPE_8021Q9100 ||
206 length_type == ETHERTYPE_8021Q9200 ||
207 length_type == ETHERTYPE_8021QinQ) {
208 /*
209 * Print VLAN information, and then go back and process
210 * the enclosed type field.
211 */
212 if (caplen < 4) {
213 ndo->ndo_protocol = "vlan";
214 nd_print_trunc(ndo);
215 return (hdrlen + caplen);
216 }
217 if (length < 4) {
218 ndo->ndo_protocol = "vlan";
219 nd_print_trunc(ndo);
220 return (hdrlen + length);
221 }
222 if (ndo->ndo_eflag) {
223 uint16_t tag = EXTRACT_BE_U_2(p);
224
225 ND_PRINT("%s, ", ieee8021q_tci_string(tag));
226 }
227
228 length_type = EXTRACT_BE_U_2(p + 2);
229 if (ndo->ndo_eflag && length_type > MAX_ETHERNET_LENGTH_VAL)
230 ND_PRINT("ethertype %s, ", tok2str(ethertype_values,"0x%04x", length_type));
231 p += 4;
232 length -= 4;
233 caplen -= 4;
234 hdrlen += 4;
235 goto recurse;
236 } else if (length_type == ETHERTYPE_JUMBO) {
237 /*
238 * Alteon jumbo frames.
239 * See
240 *
241 * http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
242 *
243 * which indicates that, following the type field,
244 * there's an LLC header and payload.
245 */
246 /* Try to print the LLC-layer header & higher layers */
247 llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
248 if (llc_hdrlen < 0) {
249 /* packet type not known, print raw packet */
250 if (!ndo->ndo_suppress_default_print)
251 ND_DEFAULTPRINT(p, caplen);
252 llc_hdrlen = -llc_hdrlen;
253 }
254 hdrlen += llc_hdrlen;
255 } else {
256 if (ethertype_print(ndo, length_type, p, length, caplen, &src, &dst) == 0) {
257 /* type not known, print raw packet */
258 if (!ndo->ndo_eflag) {
259 if (print_encap_header != NULL)
260 (*print_encap_header)(ndo, encap_header_arg);
261 ether_hdr_print(ndo, (const u_char *)ehp, orig_length);
262 }
263
264 if (!ndo->ndo_suppress_default_print)
265 ND_DEFAULTPRINT(p, caplen);
266 }
267 }
268 return (hdrlen);
269 }
270
271 /*
272 * This is the top level routine of the printer. 'p' points
273 * to the ether header of the packet, 'h->len' is the length
274 * of the packet off the wire, and 'h->caplen' is the number
275 * of bytes actually captured.
276 */
277 u_int
278 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
279 const u_char *p)
280 {
281 ndo->ndo_protocol = "ether_if";
282 return (ether_print(ndo, p, h->len, h->caplen, NULL, NULL));
283 }
284
285 /*
286 * This is the top level routine of the printer. 'p' points
287 * to the ether header of the packet, 'h->len' is the length
288 * of the packet off the wire, and 'h->caplen' is the number
289 * of bytes actually captured.
290 *
291 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
292 * before the Ethernet header.
293 */
294 u_int
295 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
296 const u_char *p)
297 {
298 /*
299 * Fail if we don't have enough data for the Hilscher pseudo-header.
300 */
301 ndo->ndo_protocol = "netanalyzer_if";
302 if (h->caplen < 4) {
303 nd_print_trunc(ndo);
304 return (h->caplen);
305 }
306
307 /* Skip the pseudo-header. */
308 return (4 + ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL));
309 }
310
311 /*
312 * This is the top level routine of the printer. 'p' points
313 * to the ether header of the packet, 'h->len' is the length
314 * of the packet off the wire, and 'h->caplen' is the number
315 * of bytes actually captured.
316 *
317 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
318 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
319 * before the Ethernet header.
320 */
321 u_int
322 netanalyzer_transparent_if_print(netdissect_options *ndo,
323 const struct pcap_pkthdr *h,
324 const u_char *p)
325 {
326 /*
327 * Fail if we don't have enough data for the Hilscher pseudo-header,
328 * preamble, and SOF.
329 */
330 ndo->ndo_protocol = "netanalyzer_transparent_if";
331 if (h->caplen < 12) {
332 nd_print_trunc(ndo);
333 return (h->caplen);
334 }
335
336 /* Skip the pseudo-header, preamble, and SOF. */
337 return (12 + ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL));
338 }
339
340 /*
341 * Prints the packet payload, given an Ethernet type code for the payload's
342 * protocol.
343 *
344 * Returns non-zero if it can do so, zero if the ethertype is unknown.
345 */
346
347 int
348 ethertype_print(netdissect_options *ndo,
349 u_short ether_type, const u_char *p,
350 u_int length, u_int caplen,
351 const struct lladdr_info *src, const struct lladdr_info *dst)
352 {
353 switch (ether_type) {
354
355 case ETHERTYPE_IP:
356 ip_print(ndo, p, length);
357 return (1);
358
359 case ETHERTYPE_IPV6:
360 ip6_print(ndo, p, length);
361 return (1);
362
363 case ETHERTYPE_ARP:
364 case ETHERTYPE_REVARP:
365 arp_print(ndo, p, length, caplen);
366 return (1);
367
368 case ETHERTYPE_DN:
369 decnet_print(ndo, p, length, caplen);
370 return (1);
371
372 case ETHERTYPE_ATALK:
373 if (ndo->ndo_vflag)
374 ND_PRINT("et1 ");
375 atalk_print(ndo, p, length);
376 return (1);
377
378 case ETHERTYPE_AARP:
379 aarp_print(ndo, p, length);
380 return (1);
381
382 case ETHERTYPE_IPX:
383 ND_PRINT("(NOV-ETHII) ");
384 ipx_print(ndo, p, length);
385 return (1);
386
387 case ETHERTYPE_ISO:
388 if (length == 0 || caplen == 0) {
389 ndo->ndo_protocol = "isoclns";
390 nd_print_trunc(ndo);
391 return (1);
392 }
393 isoclns_print(ndo, p + 1, length - 1);
394 return(1);
395
396 case ETHERTYPE_PPPOED:
397 case ETHERTYPE_PPPOES:
398 case ETHERTYPE_PPPOED2:
399 case ETHERTYPE_PPPOES2:
400 pppoe_print(ndo, p, length);
401 return (1);
402
403 case ETHERTYPE_EAPOL:
404 eap_print(ndo, p, length);
405 return (1);
406
407 case ETHERTYPE_RRCP:
408 rrcp_print(ndo, p, length, src, dst);
409 return (1);
410
411 case ETHERTYPE_PPP:
412 if (length) {
413 ND_PRINT(": ");
414 ppp_print(ndo, p, length);
415 }
416 return (1);
417
418 case ETHERTYPE_MPCP:
419 mpcp_print(ndo, p, length);
420 return (1);
421
422 case ETHERTYPE_SLOW:
423 slow_print(ndo, p, length);
424 return (1);
425
426 case ETHERTYPE_CFM:
427 case ETHERTYPE_CFM_OLD:
428 cfm_print(ndo, p, length);
429 return (1);
430
431 case ETHERTYPE_LLDP:
432 lldp_print(ndo, p, length);
433 return (1);
434
435 case ETHERTYPE_NSH:
436 nsh_print(ndo, p, length);
437 return (1);
438
439 case ETHERTYPE_LOOPBACK:
440 loopback_print(ndo, p, length);
441 return (1);
442
443 case ETHERTYPE_MPLS:
444 case ETHERTYPE_MPLS_MULTI:
445 mpls_print(ndo, p, length);
446 return (1);
447
448 case ETHERTYPE_TIPC:
449 tipc_print(ndo, p, length, caplen);
450 return (1);
451
452 case ETHERTYPE_MS_NLB_HB:
453 msnlb_print(ndo, p);
454 return (1);
455
456 case ETHERTYPE_GEONET_OLD:
457 case ETHERTYPE_GEONET:
458 geonet_print(ndo, p, length, src);
459 return (1);
460
461 case ETHERTYPE_CALM_FAST:
462 calm_fast_print(ndo, p, length, src);
463 return (1);
464
465 case ETHERTYPE_AOE:
466 aoe_print(ndo, p, length);
467 return (1);
468
469 case ETHERTYPE_MEDSA:
470 medsa_print(ndo, p, length, caplen, src, dst);
471 return (1);
472
473 case ETHERTYPE_LAT:
474 case ETHERTYPE_SCA:
475 case ETHERTYPE_MOPRC:
476 case ETHERTYPE_MOPDL:
477 case ETHERTYPE_IEEE1905_1:
478 /* default_print for now */
479 default:
480 return (0);
481 }
482 }