]> The Tcpdump Group git mirrors - tcpdump/blob - print-ether.c
Merge pull request #603 from qsn/macsec
[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_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" },
106 { 0, NULL}
107 };
108
109 static void
110 ether_addresses_print(netdissect_options *ndo, const u_char *src,
111 const u_char *dst)
112 {
113 ND_PRINT("%s > %s, ",
114 GET_ETHERADDR_STRING(src), GET_ETHERADDR_STRING(dst));
115 }
116
117 static void
118 ether_type_print(netdissect_options *ndo, uint16_t type)
119 {
120 if (!ndo->ndo_qflag)
121 ND_PRINT("ethertype %s (0x%04x)",
122 tok2str(ethertype_values, "Unknown", type), type);
123 else
124 ND_PRINT("%s",
125 tok2str(ethertype_values, "Unknown Ethertype (0x%04x)", type));
126 }
127
128 /*
129 * Common code for printing Ethernet frames.
130 *
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).
135 */
136 static u_int
137 ether_common_print(netdissect_options *ndo, const u_char *p, u_int length,
138 u_int caplen,
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)
143 {
144 const struct ether_header *ehp;
145 u_int orig_length;
146 u_int hdrlen;
147 u_short length_type;
148 int printed_length;
149 int llc_hdrlen;
150 struct lladdr_info src, dst;
151
152 if (caplen < ETHER_HDRLEN + switch_tag_len) {
153 nd_print_trunc(ndo);
154 return (caplen);
155 }
156 if (length < ETHER_HDRLEN + switch_tag_len) {
157 nd_print_trunc(ndo);
158 return (length);
159 }
160
161 if (print_encap_header != NULL)
162 (*print_encap_header)(ndo, encap_header_arg);
163
164 orig_length = length;
165
166 /*
167 * Get the source and destination addresses, skip past them,
168 * and print them if we're printing the link-layer header.
169 */
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;
175
176 length -= 2*MAC_ADDR_LEN;
177 caplen -= 2*MAC_ADDR_LEN;
178 p += 2*MAC_ADDR_LEN;
179 hdrlen = 2*MAC_ADDR_LEN;
180
181 if (ndo->ndo_eflag)
182 ether_addresses_print(ndo, src.addr, dst.addr);
183
184 /*
185 * Print the switch tag, if we have one, and skip past it.
186 */
187 if (print_switch_tag != NULL)
188 (*print_switch_tag)(ndo, p);
189
190 length -= switch_tag_len;
191 caplen -= switch_tag_len;
192 p += switch_tag_len;
193 hdrlen += switch_tag_len;
194
195 /*
196 * Get the length/type field, skip past it, and print it
197 * if we're printing the link-layer header.
198 */
199 recurse:
200 length_type = GET_BE_U_2(p);
201
202 length -= 2;
203 caplen -= 2;
204 p += 2;
205 hdrlen += 2;
206
207 /*
208 * Process 802.1AE MACsec headers.
209 */
210 printed_length = 0;
211 if (length_type == ETHERTYPE_MACSEC) {
212 /*
213 * MACsec, aka IEEE 802.1AE-2006
214 * Print the header, and try to print the payload if it's not encrypted
215 */
216 if (ndo->ndo_eflag) {
217 ether_type_print(ndo, length_type);
218 ND_PRINT(", length %u: ", orig_length);
219 printed_length = 1;
220 }
221
222 int ret = macsec_print(ndo, &p, &length, &caplen, &hdrlen);
223
224 if (ret == 0) {
225 /* Payload is encrypted; print it as raw data. */
226 if (!ndo->ndo_suppress_default_print)
227 ND_DEFAULTPRINT(p, caplen);
228 return (hdrlen);
229 } else if (ret > 0) {
230 /* Problem printing the header; just quit. */
231 return (ret);
232 } else {
233 /*
234 * Keep processing type/length fields.
235 */
236 length_type = GET_BE_U_2(p);
237
238 length -= 2;
239 caplen -= 2;
240 p += 2;
241 hdrlen += 2;
242 }
243 }
244
245 /*
246 * Process VLAN tag types.
247 */
248 while (length_type == ETHERTYPE_8021Q ||
249 length_type == ETHERTYPE_8021Q9100 ||
250 length_type == ETHERTYPE_8021Q9200 ||
251 length_type == ETHERTYPE_8021QinQ) {
252 /*
253 * It has a VLAN tag.
254 * Print VLAN information, and then go back and process
255 * the enclosed type field.
256 */
257 if (caplen < 4) {
258 ndo->ndo_protocol = "vlan";
259 nd_print_trunc(ndo);
260 return (hdrlen + caplen);
261 }
262 if (length < 4) {
263 ndo->ndo_protocol = "vlan";
264 nd_print_trunc(ndo);
265 return (hdrlen + length);
266 }
267 if (ndo->ndo_eflag) {
268 uint16_t tag = GET_BE_U_2(p);
269
270 ether_type_print(ndo, length_type);
271 if (!printed_length) {
272 ND_PRINT(", length %u: ", orig_length);
273 printed_length = 1;
274 } else
275 ND_PRINT(", ");
276 ND_PRINT("%s, ", ieee8021q_tci_string(tag));
277 }
278
279 length_type = GET_BE_U_2(p + 2);
280 p += 4;
281 length -= 4;
282 caplen -= 4;
283 hdrlen += 4;
284 }
285
286 /*
287 * We now have the final length/type field.
288 */
289 if (length_type <= MAX_ETHERNET_LENGTH_VAL) {
290 /*
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).
294 */
295 if (length_type < length) {
296 length = length_type;
297 if (caplen > length)
298 caplen = length;
299 }
300
301 /*
302 * Cut off the snapshot length to the end of the
303 * payload.
304 */
305 nd_push_snapend(ndo, p + length);
306
307 if (ndo->ndo_eflag) {
308 ND_PRINT("802.3");
309 if (!printed_length)
310 ND_PRINT(", length %u: ", length);
311 }
312
313 /*
314 * An LLC header follows the length. Print that and
315 * higher layers.
316 */
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;
323 }
324 hdrlen += llc_hdrlen;
325 nd_pop_packet_info(ndo);
326 } else if (length_type == ETHERTYPE_JUMBO) {
327 /*
328 * It's a type field, with the type for Alteon jumbo frames.
329 * See
330 *
331 * https://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
332 *
333 * which indicates that, following the type field,
334 * there's an LLC header and payload.
335 */
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;
343 }
344 hdrlen += llc_hdrlen;
345 } else if (length_type == ETHERTYPE_ARISTA) {
346 if (caplen < 2) {
347 ND_PRINT("[|arista]");
348 return (hdrlen + caplen);
349 }
350 if (length < 2) {
351 ND_PRINT("[|arista]");
352 return (hdrlen + length);
353 }
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) {
358 p += bytesConsumed;
359 length -= bytesConsumed;
360 caplen -= bytesConsumed;
361 hdrlen += bytesConsumed;
362 goto recurse;
363 } else {
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);
369 }
370 if (!ndo->ndo_suppress_default_print)
371 ND_DEFAULTPRINT(p, caplen);
372 }
373 } else {
374 /*
375 * It's a type field with some other value.
376 */
377 if (ndo->ndo_eflag) {
378 ether_type_print(ndo, length_type);
379 if (!printed_length)
380 ND_PRINT(", length %u: ", orig_length);
381 else
382 ND_PRINT(", ");
383 }
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) {
387 /*
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
392 * type.
393 */
394 ether_addresses_print(ndo, src.addr, dst.addr);
395 ether_type_print(ndo, length_type);
396 ND_PRINT(", length %u: ", orig_length);
397 }
398
399 if (!ndo->ndo_suppress_default_print)
400 ND_DEFAULTPRINT(p, caplen);
401 }
402 }
403 return (hdrlen);
404 }
405
406 /*
407 * Print an Ethernet frame while specyfing a non-standard Ethernet header
408 * length.
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.
412 *
413 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
414 */
415 u_int
416 ether_switch_tag_print(netdissect_options *ndo, const u_char *p, u_int length,
417 u_int caplen,
418 void (*print_switch_tag)(netdissect_options *, const u_char *),
419 u_int switch_tag_len)
420 {
421 return (ether_common_print(ndo, p, length, caplen, print_switch_tag,
422 switch_tag_len, NULL, NULL));
423 }
424
425 /*
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.
430 *
431 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
432 */
433 u_int
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)
438 {
439 ndo->ndo_protocol = "ether";
440 return (ether_common_print(ndo, p, length, caplen, NULL, 0,
441 print_encap_header, encap_header_arg));
442 }
443
444 /*
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.
449 */
450 u_int
451 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
452 const u_char *p)
453 {
454 ndo->ndo_protocol = "ether_if";
455 return (ether_print(ndo, p, h->len, h->caplen, NULL, NULL));
456 }
457
458 /*
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.
463 *
464 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
465 * before the Ethernet header.
466 */
467 u_int
468 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
469 const u_char *p)
470 {
471 /*
472 * Fail if we don't have enough data for the Hilscher pseudo-header.
473 */
474 ndo->ndo_protocol = "netanalyzer_if";
475 if (h->caplen < 4) {
476 nd_print_trunc(ndo);
477 return (h->caplen);
478 }
479
480 /* Skip the pseudo-header. */
481 return (4 + ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL));
482 }
483
484 /*
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.
489 *
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.
493 */
494 u_int
495 netanalyzer_transparent_if_print(netdissect_options *ndo,
496 const struct pcap_pkthdr *h,
497 const u_char *p)
498 {
499 /*
500 * Fail if we don't have enough data for the Hilscher pseudo-header,
501 * preamble, and SOF.
502 */
503 ndo->ndo_protocol = "netanalyzer_transparent_if";
504 if (h->caplen < 12) {
505 nd_print_trunc(ndo);
506 return (h->caplen);
507 }
508
509 /* Skip the pseudo-header, preamble, and SOF. */
510 return (12 + ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL));
511 }
512
513 /*
514 * Prints the packet payload, given an Ethernet type code for the payload's
515 * protocol.
516 *
517 * Returns non-zero if it can do so, zero if the ethertype is unknown.
518 */
519
520 int
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)
525 {
526 switch (ether_type) {
527
528 case ETHERTYPE_IP:
529 ip_print(ndo, p, length);
530 return (1);
531
532 case ETHERTYPE_IPV6:
533 ip6_print(ndo, p, length);
534 return (1);
535
536 case ETHERTYPE_ARP:
537 case ETHERTYPE_REVARP:
538 arp_print(ndo, p, length, caplen);
539 return (1);
540
541 case ETHERTYPE_DN:
542 decnet_print(ndo, p, length, caplen);
543 return (1);
544
545 case ETHERTYPE_ATALK:
546 if (ndo->ndo_vflag)
547 ND_PRINT("et1 ");
548 atalk_print(ndo, p, length);
549 return (1);
550
551 case ETHERTYPE_AARP:
552 aarp_print(ndo, p, length);
553 return (1);
554
555 case ETHERTYPE_IPX:
556 ND_PRINT("(NOV-ETHII) ");
557 ipx_print(ndo, p, length);
558 return (1);
559
560 case ETHERTYPE_ISO:
561 if (length == 0 || caplen == 0) {
562 ndo->ndo_protocol = "isoclns";
563 nd_print_trunc(ndo);
564 return (1);
565 }
566 isoclns_print(ndo, p + 1, length - 1);
567 return(1);
568
569 case ETHERTYPE_PPPOED:
570 case ETHERTYPE_PPPOES:
571 case ETHERTYPE_PPPOED2:
572 case ETHERTYPE_PPPOES2:
573 pppoe_print(ndo, p, length);
574 return (1);
575
576 case ETHERTYPE_EAPOL:
577 eap_print(ndo, p, length);
578 return (1);
579
580 case ETHERTYPE_RRCP:
581 rrcp_print(ndo, p, length, src, dst);
582 return (1);
583
584 case ETHERTYPE_PPP:
585 if (length) {
586 ND_PRINT(": ");
587 ppp_print(ndo, p, length);
588 }
589 return (1);
590
591 case ETHERTYPE_MPCP:
592 mpcp_print(ndo, p, length);
593 return (1);
594
595 case ETHERTYPE_SLOW:
596 slow_print(ndo, p, length);
597 return (1);
598
599 case ETHERTYPE_CFM:
600 case ETHERTYPE_CFM_OLD:
601 cfm_print(ndo, p, length);
602 return (1);
603
604 case ETHERTYPE_LLDP:
605 lldp_print(ndo, p, length);
606 return (1);
607
608 case ETHERTYPE_NSH:
609 nsh_print(ndo, p, length);
610 return (1);
611
612 case ETHERTYPE_LOOPBACK:
613 loopback_print(ndo, p, length);
614 return (1);
615
616 case ETHERTYPE_MPLS:
617 case ETHERTYPE_MPLS_MULTI:
618 mpls_print(ndo, p, length);
619 return (1);
620
621 case ETHERTYPE_TIPC:
622 tipc_print(ndo, p, length, caplen);
623 return (1);
624
625 case ETHERTYPE_MS_NLB_HB:
626 msnlb_print(ndo, p);
627 return (1);
628
629 case ETHERTYPE_GEONET_OLD:
630 case ETHERTYPE_GEONET:
631 geonet_print(ndo, p, length, src);
632 return (1);
633
634 case ETHERTYPE_CALM_FAST:
635 calm_fast_print(ndo, p, length, src);
636 return (1);
637
638 case ETHERTYPE_AOE:
639 aoe_print(ndo, p, length);
640 return (1);
641
642 case ETHERTYPE_PTP:
643 ptp_print(ndo, p, length);
644 return (1);
645
646 case ETHERTYPE_LAT:
647 case ETHERTYPE_SCA:
648 case ETHERTYPE_MOPRC:
649 case ETHERTYPE_MOPDL:
650 case ETHERTYPE_IEEE1905_1:
651 /* default_print for now */
652 default:
653 return (0);
654 }
655 }