]> The Tcpdump Group git mirrors - tcpdump/blob - print-ether.c
Merge pull request #772 from Mister-X-/fix-status-codes
[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_PTP, "PTP" },
104 { ETHERTYPE_ARISTA, "Arista Vendor Specific Protocol" },
105 { 0, NULL}
106 };
107
108 static void
109 ether_addresses_print(netdissect_options *ndo, const u_char *src,
110 const u_char *dst)
111 {
112 ND_PRINT("%s > %s, ",
113 GET_ETHERADDR_STRING(src), GET_ETHERADDR_STRING(dst));
114 }
115
116 static void
117 ether_type_print(netdissect_options *ndo, uint16_t type)
118 {
119 if (!ndo->ndo_qflag)
120 ND_PRINT("ethertype %s (0x%04x)",
121 tok2str(ethertype_values, "Unknown", type), type);
122 else
123 ND_PRINT("%s",
124 tok2str(ethertype_values, "Unknown Ethertype (0x%04x)", type));
125 }
126
127 /*
128 * Common code for printing Ethernet frames.
129 *
130 * It can handle Ethernet headers with extra tag information inserted
131 * after the destination and source addresses, as is inserted by some
132 * switch chips, and extra encapsulation header information before
133 * printing Ethernet header information (such as a LANE ID for ATM LANE).
134 */
135 static u_int
136 ether_common_print(netdissect_options *ndo, const u_char *p, u_int length,
137 u_int caplen,
138 void (*print_switch_tag)(netdissect_options *ndo, const u_char *),
139 u_int switch_tag_len,
140 void (*print_encap_header)(netdissect_options *ndo, const u_char *),
141 const u_char *encap_header_arg)
142 {
143 const struct ether_header *ehp;
144 u_int orig_length;
145 u_int hdrlen;
146 u_short length_type;
147 int printed_length;
148 int llc_hdrlen;
149 struct lladdr_info src, dst;
150
151 if (caplen < ETHER_HDRLEN + switch_tag_len) {
152 nd_print_trunc(ndo);
153 return (caplen);
154 }
155 if (length < ETHER_HDRLEN + switch_tag_len) {
156 nd_print_trunc(ndo);
157 return (length);
158 }
159
160 if (print_encap_header != NULL)
161 (*print_encap_header)(ndo, encap_header_arg);
162
163 orig_length = length;
164
165 /*
166 * Get the source and destination addresses, skip past them,
167 * and print them if we're printing the link-layer header.
168 */
169 ehp = (const struct ether_header *)p;
170 src.addr = ehp->ether_shost;
171 src.addr_string = etheraddr_string;
172 dst.addr = ehp->ether_dhost;
173 dst.addr_string = etheraddr_string;
174
175 length -= 2*MAC_ADDR_LEN;
176 caplen -= 2*MAC_ADDR_LEN;
177 p += 2*MAC_ADDR_LEN;
178 hdrlen = 2*MAC_ADDR_LEN;
179
180 if (ndo->ndo_eflag)
181 ether_addresses_print(ndo, src.addr, dst.addr);
182
183 /*
184 * Print the switch tag, if we have one, and skip past it.
185 */
186 if (print_switch_tag != NULL)
187 (*print_switch_tag)(ndo, p);
188
189 length -= switch_tag_len;
190 caplen -= switch_tag_len;
191 p += switch_tag_len;
192 hdrlen += switch_tag_len;
193
194 /*
195 * Get the length/type field, skip past it, and print it
196 * if we're printing the link-layer header.
197 */
198 recurse:
199 length_type = GET_BE_U_2(p);
200
201 length -= 2;
202 caplen -= 2;
203 p += 2;
204 hdrlen += 2;
205
206 /*
207 * Process VLAN tag types.
208 */
209 printed_length = 0;
210 while (length_type == ETHERTYPE_8021Q ||
211 length_type == ETHERTYPE_8021Q9100 ||
212 length_type == ETHERTYPE_8021Q9200 ||
213 length_type == ETHERTYPE_8021QinQ) {
214 /*
215 * It has a VLAN tag.
216 * Print VLAN information, and then go back and process
217 * the enclosed type field.
218 */
219 if (caplen < 4) {
220 ndo->ndo_protocol = "vlan";
221 nd_print_trunc(ndo);
222 return (hdrlen + caplen);
223 }
224 if (length < 4) {
225 ndo->ndo_protocol = "vlan";
226 nd_print_trunc(ndo);
227 return (hdrlen + length);
228 }
229 if (ndo->ndo_eflag) {
230 uint16_t tag = GET_BE_U_2(p);
231
232 ether_type_print(ndo, length_type);
233 if (!printed_length) {
234 ND_PRINT(", length %u: ", orig_length);
235 printed_length = 1;
236 } else
237 ND_PRINT(", ");
238 ND_PRINT("%s, ", ieee8021q_tci_string(tag));
239 }
240
241 length_type = GET_BE_U_2(p + 2);
242 p += 4;
243 length -= 4;
244 caplen -= 4;
245 hdrlen += 4;
246 }
247
248 /*
249 * We now have the final length/type field.
250 */
251 if (length_type <= MAX_ETHERNET_LENGTH_VAL) {
252 /*
253 * It's a length field, containing the length of the
254 * remaining payload; use it as such, as long as
255 * it's not too large (bigger than the actual payload).
256 */
257 if (length_type < length) {
258 length = length_type;
259 if (caplen > length)
260 caplen = length;
261 }
262
263 /*
264 * Cut off the snapshot length to the end of the
265 * payload.
266 */
267 nd_push_snapend(ndo, p + length);
268
269 if (ndo->ndo_eflag) {
270 ND_PRINT("802.3");
271 if (!printed_length)
272 ND_PRINT(", length %u: ", length);
273 }
274
275 /*
276 * An LLC header follows the length. Print that and
277 * higher layers.
278 */
279 llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
280 if (llc_hdrlen < 0) {
281 /* packet type not known, print raw packet */
282 if (!ndo->ndo_suppress_default_print)
283 ND_DEFAULTPRINT(p, caplen);
284 llc_hdrlen = -llc_hdrlen;
285 }
286 hdrlen += llc_hdrlen;
287 nd_pop_packet_info(ndo);
288 } else if (length_type == ETHERTYPE_JUMBO) {
289 /*
290 * It's a type field, with the type for Alteon jumbo frames.
291 * See
292 *
293 * https://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
294 *
295 * which indicates that, following the type field,
296 * there's an LLC header and payload.
297 */
298 /* Try to print the LLC-layer header & higher layers */
299 llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
300 if (llc_hdrlen < 0) {
301 /* packet type not known, print raw packet */
302 if (!ndo->ndo_suppress_default_print)
303 ND_DEFAULTPRINT(p, caplen);
304 llc_hdrlen = -llc_hdrlen;
305 }
306 hdrlen += llc_hdrlen;
307 } else if (length_type == ETHERTYPE_ARISTA) {
308 if (caplen < 2) {
309 ND_PRINT("[|arista]");
310 return (hdrlen + caplen);
311 }
312 if (length < 2) {
313 ND_PRINT("[|arista]");
314 return (hdrlen + length);
315 }
316 ether_type_print(ndo, length_type);
317 ND_PRINT(", length %u: ", orig_length);
318 int bytesConsumed = arista_ethertype_print(ndo, p, length);
319 if (bytesConsumed > 0) {
320 p += bytesConsumed;
321 length -= bytesConsumed;
322 caplen -= bytesConsumed;
323 hdrlen += bytesConsumed;
324 goto recurse;
325 } else {
326 /* subtype/version not known, print raw packet */
327 if (!ndo->ndo_eflag && length_type > MAX_ETHERNET_LENGTH_VAL) {
328 ether_addresses_print(ndo, src.addr, dst.addr);
329 ether_type_print(ndo, length_type);
330 ND_PRINT(", length %u: ", orig_length);
331 }
332 if (!ndo->ndo_suppress_default_print)
333 ND_DEFAULTPRINT(p, caplen);
334 }
335 } else {
336 /*
337 * It's a type field with some other value.
338 */
339 if (ndo->ndo_eflag) {
340 ether_type_print(ndo, length_type);
341 if (!printed_length)
342 ND_PRINT(", length %u: ", orig_length);
343 else
344 ND_PRINT(", ");
345 }
346 if (ethertype_print(ndo, length_type, p, length, caplen, &src, &dst) == 0) {
347 /* type not known, print raw packet */
348 if (!ndo->ndo_eflag) {
349 /*
350 * We didn't print the full link-layer
351 * header, as -e wasn't specified, so
352 * print only the source and destination
353 * MAC addresses and the final Ethernet
354 * type.
355 */
356 ether_addresses_print(ndo, src.addr, dst.addr);
357 ether_type_print(ndo, length_type);
358 ND_PRINT(", length %u: ", orig_length);
359 }
360
361 if (!ndo->ndo_suppress_default_print)
362 ND_DEFAULTPRINT(p, caplen);
363 }
364 }
365 return (hdrlen);
366 }
367
368 /*
369 * Print an Ethernet frame while specyfing a non-standard Ethernet header
370 * length.
371 * This might be encapsulated within another frame; we might be passed
372 * a pointer to a function that can print header information for that
373 * frame's protocol, and an argument to pass to that function.
374 *
375 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
376 */
377 u_int
378 ether_switch_tag_print(netdissect_options *ndo, const u_char *p, u_int length,
379 u_int caplen,
380 void (*print_switch_tag)(netdissect_options *, const u_char *),
381 u_int switch_tag_len)
382 {
383 return (ether_common_print(ndo, p, length, caplen, print_switch_tag,
384 switch_tag_len, NULL, NULL));
385 }
386
387 /*
388 * Print an Ethernet frame.
389 * This might be encapsulated within another frame; we might be passed
390 * a pointer to a function that can print header information for that
391 * frame's protocol, and an argument to pass to that function.
392 *
393 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
394 */
395 u_int
396 ether_print(netdissect_options *ndo,
397 const u_char *p, u_int length, u_int caplen,
398 void (*print_encap_header)(netdissect_options *ndo, const u_char *),
399 const u_char *encap_header_arg)
400 {
401 ndo->ndo_protocol = "ether";
402 return (ether_common_print(ndo, p, length, caplen, NULL, 0,
403 print_encap_header, encap_header_arg));
404 }
405
406 /*
407 * This is the top level routine of the printer. 'p' points
408 * to the ether header of the packet, 'h->len' is the length
409 * of the packet off the wire, and 'h->caplen' is the number
410 * of bytes actually captured.
411 */
412 u_int
413 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
414 const u_char *p)
415 {
416 ndo->ndo_protocol = "ether_if";
417 return (ether_print(ndo, p, h->len, h->caplen, NULL, NULL));
418 }
419
420 /*
421 * This is the top level routine of the printer. 'p' points
422 * to the ether header of the packet, 'h->len' is the length
423 * of the packet off the wire, and 'h->caplen' is the number
424 * of bytes actually captured.
425 *
426 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
427 * before the Ethernet header.
428 */
429 u_int
430 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
431 const u_char *p)
432 {
433 /*
434 * Fail if we don't have enough data for the Hilscher pseudo-header.
435 */
436 ndo->ndo_protocol = "netanalyzer_if";
437 if (h->caplen < 4) {
438 nd_print_trunc(ndo);
439 return (h->caplen);
440 }
441
442 /* Skip the pseudo-header. */
443 return (4 + ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL));
444 }
445
446 /*
447 * This is the top level routine of the printer. 'p' points
448 * to the ether header of the packet, 'h->len' is the length
449 * of the packet off the wire, and 'h->caplen' is the number
450 * of bytes actually captured.
451 *
452 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
453 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
454 * before the Ethernet header.
455 */
456 u_int
457 netanalyzer_transparent_if_print(netdissect_options *ndo,
458 const struct pcap_pkthdr *h,
459 const u_char *p)
460 {
461 /*
462 * Fail if we don't have enough data for the Hilscher pseudo-header,
463 * preamble, and SOF.
464 */
465 ndo->ndo_protocol = "netanalyzer_transparent_if";
466 if (h->caplen < 12) {
467 nd_print_trunc(ndo);
468 return (h->caplen);
469 }
470
471 /* Skip the pseudo-header, preamble, and SOF. */
472 return (12 + ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL));
473 }
474
475 /*
476 * Prints the packet payload, given an Ethernet type code for the payload's
477 * protocol.
478 *
479 * Returns non-zero if it can do so, zero if the ethertype is unknown.
480 */
481
482 int
483 ethertype_print(netdissect_options *ndo,
484 u_short ether_type, const u_char *p,
485 u_int length, u_int caplen,
486 const struct lladdr_info *src, const struct lladdr_info *dst)
487 {
488 switch (ether_type) {
489
490 case ETHERTYPE_IP:
491 ip_print(ndo, p, length);
492 return (1);
493
494 case ETHERTYPE_IPV6:
495 ip6_print(ndo, p, length);
496 return (1);
497
498 case ETHERTYPE_ARP:
499 case ETHERTYPE_REVARP:
500 arp_print(ndo, p, length, caplen);
501 return (1);
502
503 case ETHERTYPE_DN:
504 decnet_print(ndo, p, length, caplen);
505 return (1);
506
507 case ETHERTYPE_ATALK:
508 if (ndo->ndo_vflag)
509 ND_PRINT("et1 ");
510 atalk_print(ndo, p, length);
511 return (1);
512
513 case ETHERTYPE_AARP:
514 aarp_print(ndo, p, length);
515 return (1);
516
517 case ETHERTYPE_IPX:
518 ND_PRINT("(NOV-ETHII) ");
519 ipx_print(ndo, p, length);
520 return (1);
521
522 case ETHERTYPE_ISO:
523 if (length == 0 || caplen == 0) {
524 ndo->ndo_protocol = "isoclns";
525 nd_print_trunc(ndo);
526 return (1);
527 }
528 isoclns_print(ndo, p + 1, length - 1);
529 return(1);
530
531 case ETHERTYPE_PPPOED:
532 case ETHERTYPE_PPPOES:
533 case ETHERTYPE_PPPOED2:
534 case ETHERTYPE_PPPOES2:
535 pppoe_print(ndo, p, length);
536 return (1);
537
538 case ETHERTYPE_EAPOL:
539 eap_print(ndo, p, length);
540 return (1);
541
542 case ETHERTYPE_RRCP:
543 rrcp_print(ndo, p, length, src, dst);
544 return (1);
545
546 case ETHERTYPE_PPP:
547 if (length) {
548 ND_PRINT(": ");
549 ppp_print(ndo, p, length);
550 }
551 return (1);
552
553 case ETHERTYPE_MPCP:
554 mpcp_print(ndo, p, length);
555 return (1);
556
557 case ETHERTYPE_SLOW:
558 slow_print(ndo, p, length);
559 return (1);
560
561 case ETHERTYPE_CFM:
562 case ETHERTYPE_CFM_OLD:
563 cfm_print(ndo, p, length);
564 return (1);
565
566 case ETHERTYPE_LLDP:
567 lldp_print(ndo, p, length);
568 return (1);
569
570 case ETHERTYPE_NSH:
571 nsh_print(ndo, p, length);
572 return (1);
573
574 case ETHERTYPE_LOOPBACK:
575 loopback_print(ndo, p, length);
576 return (1);
577
578 case ETHERTYPE_MPLS:
579 case ETHERTYPE_MPLS_MULTI:
580 mpls_print(ndo, p, length);
581 return (1);
582
583 case ETHERTYPE_TIPC:
584 tipc_print(ndo, p, length, caplen);
585 return (1);
586
587 case ETHERTYPE_MS_NLB_HB:
588 msnlb_print(ndo, p);
589 return (1);
590
591 case ETHERTYPE_GEONET_OLD:
592 case ETHERTYPE_GEONET:
593 geonet_print(ndo, p, length, src);
594 return (1);
595
596 case ETHERTYPE_CALM_FAST:
597 calm_fast_print(ndo, p, length, src);
598 return (1);
599
600 case ETHERTYPE_AOE:
601 aoe_print(ndo, p, length);
602 return (1);
603
604 case ETHERTYPE_PTP:
605 ptp_print(ndo, p, length);
606 return (1);
607
608 case ETHERTYPE_LAT:
609 case ETHERTYPE_SCA:
610 case ETHERTYPE_MOPRC:
611 case ETHERTYPE_MOPDL:
612 case ETHERTYPE_IEEE1905_1:
613 /* default_print for now */
614 default:
615 return (0);
616 }
617 }