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 #include <sys/cdefs.h> 23 #ifndef lint 24 __RCSID("$NetBSD: print-ether.c,v 1.10 2023/08/17 20:19:40 christos Exp $"); 25 #endif 26 27 /* \summary: Ethernet printer */ 28 29 #ifdef HAVE_CONFIG_H 30 #include <config.h> 31 #endif 32 33 #include "netdissect-stdinc.h" 34 35 #define ND_LONGJMP_FROM_TCHECK 36 #include "netdissect.h" 37 #include "extract.h" 38 #include "addrtoname.h" 39 #include "ethertype.h" 40 41 /* 42 * Structure of an Ethernet header. 43 */ 44 struct ether_header { 45 nd_mac_addr ether_dhost; 46 nd_mac_addr ether_shost; 47 nd_uint16_t ether_length_type; 48 }; 49 50 /* 51 * Length of an Ethernet header; note that some compilers may pad 52 * "struct ether_header" to a multiple of 4 bytes, for example, so 53 * "sizeof (struct ether_header)" may not give the right answer. 54 */ 55 #define ETHER_HDRLEN 14 56 57 const struct tok ethertype_values[] = { 58 { ETHERTYPE_IP, "IPv4" }, 59 { ETHERTYPE_MPLS, "MPLS unicast" }, 60 { ETHERTYPE_MPLS_MULTI, "MPLS multicast" }, 61 { ETHERTYPE_IPV6, "IPv6" }, 62 { ETHERTYPE_8021Q, "802.1Q" }, 63 { ETHERTYPE_8021Q9100, "802.1Q-9100" }, 64 { ETHERTYPE_8021QinQ, "802.1Q-QinQ" }, 65 { ETHERTYPE_8021Q9200, "802.1Q-9200" }, 66 { ETHERTYPE_MACSEC, "802.1AE MACsec" }, 67 { ETHERTYPE_VMAN, "VMAN" }, 68 { ETHERTYPE_PUP, "PUP" }, 69 { ETHERTYPE_ARP, "ARP"}, 70 { ETHERTYPE_REVARP, "Reverse ARP"}, 71 { ETHERTYPE_NS, "NS" }, 72 { ETHERTYPE_SPRITE, "Sprite" }, 73 { ETHERTYPE_TRAIL, "Trail" }, 74 { ETHERTYPE_MOPDL, "MOP DL" }, 75 { ETHERTYPE_MOPRC, "MOP RC" }, 76 { ETHERTYPE_DN, "DN" }, 77 { ETHERTYPE_LAT, "LAT" }, 78 { ETHERTYPE_SCA, "SCA" }, 79 { ETHERTYPE_TEB, "TEB" }, 80 { ETHERTYPE_LANBRIDGE, "Lanbridge" }, 81 { ETHERTYPE_DECDNS, "DEC DNS" }, 82 { ETHERTYPE_DECDTS, "DEC DTS" }, 83 { ETHERTYPE_VEXP, "VEXP" }, 84 { ETHERTYPE_VPROD, "VPROD" }, 85 { ETHERTYPE_ATALK, "Appletalk" }, 86 { ETHERTYPE_AARP, "Appletalk ARP" }, 87 { ETHERTYPE_IPX, "IPX" }, 88 { ETHERTYPE_PPP, "PPP" }, 89 { ETHERTYPE_MPCP, "MPCP" }, 90 { ETHERTYPE_SLOW, "Slow Protocols" }, 91 { ETHERTYPE_PPPOED, "PPPoE D" }, 92 { ETHERTYPE_PPPOES, "PPPoE S" }, 93 { ETHERTYPE_EAPOL, "EAPOL" }, 94 { ETHERTYPE_REALTEK, "Realtek protocols" }, 95 { ETHERTYPE_MS_NLB_HB, "MS NLB heartbeat" }, 96 { ETHERTYPE_JUMBO, "Jumbo" }, 97 { ETHERTYPE_NSH, "NSH" }, 98 { ETHERTYPE_LOOPBACK, "Loopback" }, 99 { ETHERTYPE_ISO, "OSI" }, 100 { ETHERTYPE_GRE_ISO, "GRE-OSI" }, 101 { ETHERTYPE_CFM_OLD, "CFM (old)" }, 102 { ETHERTYPE_CFM, "CFM" }, 103 { ETHERTYPE_IEEE1905_1, "IEEE1905.1" }, 104 { ETHERTYPE_LLDP, "LLDP" }, 105 { ETHERTYPE_TIPC, "TIPC"}, 106 { ETHERTYPE_GEONET_OLD, "GeoNet (old)"}, 107 { ETHERTYPE_GEONET, "GeoNet"}, 108 { ETHERTYPE_CALM_FAST, "CALM FAST"}, 109 { ETHERTYPE_AOE, "AoE" }, 110 { ETHERTYPE_PTP, "PTP" }, 111 { ETHERTYPE_ARISTA, "Arista Vendor Specific Protocol" }, 112 { 0, NULL} 113 }; 114 115 static void 116 ether_addresses_print(netdissect_options *ndo, const u_char *src, 117 const u_char *dst) 118 { 119 ND_PRINT("%s > %s, ", 120 GET_ETHERADDR_STRING(src), GET_ETHERADDR_STRING(dst)); 121 } 122 123 static void 124 ether_type_print(netdissect_options *ndo, uint16_t type) 125 { 126 if (!ndo->ndo_qflag) 127 ND_PRINT("ethertype %s (0x%04x)", 128 tok2str(ethertype_values, "Unknown", type), type); 129 else 130 ND_PRINT("%s", 131 tok2str(ethertype_values, "Unknown Ethertype (0x%04x)", type)); 132 } 133 134 /* 135 * Common code for printing Ethernet frames. 136 * 137 * It can handle Ethernet headers with extra tag information inserted 138 * after the destination and source addresses, as is inserted by some 139 * switch chips, and extra encapsulation header information before 140 * printing Ethernet header information (such as a LANE ID for ATM LANE). 141 */ 142 static u_int 143 ether_common_print(netdissect_options *ndo, const u_char *p, u_int length, 144 u_int caplen, 145 void (*print_switch_tag)(netdissect_options *ndo, const u_char *), 146 u_int switch_tag_len, 147 void (*print_encap_header)(netdissect_options *ndo, const u_char *), 148 const u_char *encap_header_arg) 149 { 150 const struct ether_header *ehp; 151 u_int orig_length; 152 u_int hdrlen; 153 u_short length_type; 154 int printed_length; 155 int llc_hdrlen; 156 struct lladdr_info src, dst; 157 158 if (length < caplen) { 159 ND_PRINT("[length %u < caplen %u]", length, caplen); 160 nd_print_invalid(ndo); 161 return length; 162 } 163 if (caplen < ETHER_HDRLEN + switch_tag_len) { 164 nd_print_trunc(ndo); 165 return caplen; 166 } 167 168 if (print_encap_header != NULL) 169 (*print_encap_header)(ndo, encap_header_arg); 170 171 orig_length = length; 172 173 /* 174 * Get the source and destination addresses, skip past them, 175 * and print them if we're printing the link-layer header. 176 */ 177 ehp = (const struct ether_header *)p; 178 src.addr = ehp->ether_shost; 179 src.addr_string = etheraddr_string; 180 dst.addr = ehp->ether_dhost; 181 dst.addr_string = etheraddr_string; 182 183 length -= 2*MAC_ADDR_LEN; 184 caplen -= 2*MAC_ADDR_LEN; 185 p += 2*MAC_ADDR_LEN; 186 hdrlen = 2*MAC_ADDR_LEN; 187 188 if (ndo->ndo_eflag) 189 ether_addresses_print(ndo, src.addr, dst.addr); 190 191 /* 192 * Print the switch tag, if we have one, and skip past it. 193 */ 194 if (print_switch_tag != NULL) 195 (*print_switch_tag)(ndo, p); 196 197 length -= switch_tag_len; 198 caplen -= switch_tag_len; 199 p += switch_tag_len; 200 hdrlen += switch_tag_len; 201 202 /* 203 * Get the length/type field, skip past it, and print it 204 * if we're printing the link-layer header. 205 */ 206 recurse: 207 length_type = GET_BE_U_2(p); 208 209 length -= 2; 210 caplen -= 2; 211 p += 2; 212 hdrlen += 2; 213 214 /* 215 * Process 802.1AE MACsec headers. 216 */ 217 printed_length = 0; 218 if (length_type == ETHERTYPE_MACSEC) { 219 /* 220 * MACsec, aka IEEE 802.1AE-2006 221 * Print the header, and try to print the payload if it's not encrypted 222 */ 223 if (ndo->ndo_eflag) { 224 ether_type_print(ndo, length_type); 225 ND_PRINT(", length %u: ", orig_length); 226 printed_length = 1; 227 } 228 229 int ret = macsec_print(ndo, &p, &length, &caplen, &hdrlen, 230 &src, &dst); 231 232 if (ret == 0) { 233 /* Payload is encrypted; print it as raw data. */ 234 if (!ndo->ndo_suppress_default_print) 235 ND_DEFAULTPRINT(p, caplen); 236 return hdrlen; 237 } else if (ret > 0) { 238 /* Problem printing the header; just quit. */ 239 return ret; 240 } else { 241 /* 242 * Keep processing type/length fields. 243 */ 244 length_type = GET_BE_U_2(p); 245 246 ND_LCHECK_U(caplen, 2); 247 length -= 2; 248 caplen -= 2; 249 p += 2; 250 hdrlen += 2; 251 } 252 } 253 254 /* 255 * Process VLAN tag types. 256 */ 257 while (length_type == ETHERTYPE_8021Q || 258 length_type == ETHERTYPE_8021Q9100 || 259 length_type == ETHERTYPE_8021Q9200 || 260 length_type == ETHERTYPE_8021QinQ) { 261 /* 262 * It has a VLAN tag. 263 * Print VLAN information, and then go back and process 264 * the enclosed type field. 265 */ 266 if (caplen < 4) { 267 ndo->ndo_protocol = "vlan"; 268 nd_print_trunc(ndo); 269 return hdrlen + caplen; 270 } 271 if (length < 4) { 272 ndo->ndo_protocol = "vlan"; 273 nd_print_trunc(ndo); 274 return hdrlen + length; 275 } 276 if (ndo->ndo_eflag) { 277 uint16_t tag = GET_BE_U_2(p); 278 279 ether_type_print(ndo, length_type); 280 if (!printed_length) { 281 ND_PRINT(", length %u: ", orig_length); 282 printed_length = 1; 283 } else 284 ND_PRINT(", "); 285 ND_PRINT("%s, ", ieee8021q_tci_string(tag)); 286 } 287 288 length_type = GET_BE_U_2(p + 2); 289 p += 4; 290 length -= 4; 291 caplen -= 4; 292 hdrlen += 4; 293 } 294 295 /* 296 * We now have the final length/type field. 297 */ 298 if (length_type <= MAX_ETHERNET_LENGTH_VAL) { 299 /* 300 * It's a length field, containing the length of the 301 * remaining payload; use it as such, as long as 302 * it's not too large (bigger than the actual payload). 303 */ 304 if (length_type < length) { 305 length = length_type; 306 if (caplen > length) 307 caplen = length; 308 } 309 310 /* 311 * Cut off the snapshot length to the end of the 312 * payload. 313 */ 314 if (!nd_push_snaplen(ndo, p, length)) { 315 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, 316 "%s: can't push snaplen on buffer stack", __func__); 317 } 318 319 if (ndo->ndo_eflag) { 320 ND_PRINT("802.3"); 321 if (!printed_length) 322 ND_PRINT(", length %u: ", length); 323 } 324 325 /* 326 * An LLC header follows the length. Print that and 327 * higher layers. 328 */ 329 llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst); 330 if (llc_hdrlen < 0) { 331 /* packet type not known, print raw packet */ 332 if (!ndo->ndo_suppress_default_print) 333 ND_DEFAULTPRINT(p, caplen); 334 llc_hdrlen = -llc_hdrlen; 335 } 336 hdrlen += llc_hdrlen; 337 nd_pop_packet_info(ndo); 338 } else if (length_type == ETHERTYPE_JUMBO) { 339 /* 340 * It's a type field, with the type for Alteon jumbo frames. 341 * See 342 * 343 * https://tools.ietf.org/html/draft-ietf-isis-ext-eth-01 344 * 345 * which indicates that, following the type field, 346 * there's an LLC header and payload. 347 */ 348 /* Try to print the LLC-layer header & higher layers */ 349 llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst); 350 if (llc_hdrlen < 0) { 351 /* packet type not known, print raw packet */ 352 if (!ndo->ndo_suppress_default_print) 353 ND_DEFAULTPRINT(p, caplen); 354 llc_hdrlen = -llc_hdrlen; 355 } 356 hdrlen += llc_hdrlen; 357 } else if (length_type == ETHERTYPE_ARISTA) { 358 if (caplen < 2) { 359 ND_PRINT("[|arista]"); 360 return hdrlen + caplen; 361 } 362 if (length < 2) { 363 ND_PRINT("[|arista]"); 364 return hdrlen + length; 365 } 366 ether_type_print(ndo, length_type); 367 ND_PRINT(", length %u: ", orig_length); 368 int bytesConsumed = arista_ethertype_print(ndo, p, length); 369 if (bytesConsumed > 0) { 370 p += bytesConsumed; 371 length -= bytesConsumed; 372 caplen -= bytesConsumed; 373 hdrlen += bytesConsumed; 374 goto recurse; 375 } else { 376 /* subtype/version not known, print raw packet */ 377 if (!ndo->ndo_eflag && length_type > MAX_ETHERNET_LENGTH_VAL) { 378 ether_addresses_print(ndo, src.addr, dst.addr); 379 ether_type_print(ndo, length_type); 380 ND_PRINT(", length %u: ", orig_length); 381 } 382 if (!ndo->ndo_suppress_default_print) 383 ND_DEFAULTPRINT(p, caplen); 384 } 385 } else { 386 /* 387 * It's a type field with some other value. 388 */ 389 if (ndo->ndo_eflag) { 390 ether_type_print(ndo, length_type); 391 if (!printed_length) 392 ND_PRINT(", length %u: ", orig_length); 393 else 394 ND_PRINT(", "); 395 } 396 if (ethertype_print(ndo, length_type, p, length, caplen, &src, &dst) == 0) { 397 /* type not known, print raw packet */ 398 if (!ndo->ndo_eflag) { 399 /* 400 * We didn't print the full link-layer 401 * header, as -e wasn't specified, so 402 * print only the source and destination 403 * MAC addresses and the final Ethernet 404 * type. 405 */ 406 ether_addresses_print(ndo, src.addr, dst.addr); 407 ether_type_print(ndo, length_type); 408 ND_PRINT(", length %u: ", orig_length); 409 } 410 411 if (!ndo->ndo_suppress_default_print) 412 ND_DEFAULTPRINT(p, caplen); 413 } 414 } 415 invalid: 416 return hdrlen; 417 } 418 419 /* 420 * Print an Ethernet frame while specyfing a non-standard Ethernet header 421 * length. 422 * This might be encapsulated within another frame; we might be passed 423 * a pointer to a function that can print header information for that 424 * frame's protocol, and an argument to pass to that function. 425 * 426 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p. 427 */ 428 u_int 429 ether_switch_tag_print(netdissect_options *ndo, const u_char *p, u_int length, 430 u_int caplen, 431 void (*print_switch_tag)(netdissect_options *, const u_char *), 432 u_int switch_tag_len) 433 { 434 return ether_common_print(ndo, p, length, caplen, print_switch_tag, 435 switch_tag_len, NULL, NULL); 436 } 437 438 /* 439 * Print an Ethernet frame. 440 * This might be encapsulated within another frame; we might be passed 441 * a pointer to a function that can print header information for that 442 * frame's protocol, and an argument to pass to that function. 443 * 444 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p. 445 */ 446 u_int 447 ether_print(netdissect_options *ndo, 448 const u_char *p, u_int length, u_int caplen, 449 void (*print_encap_header)(netdissect_options *ndo, const u_char *), 450 const u_char *encap_header_arg) 451 { 452 ndo->ndo_protocol = "ether"; 453 return ether_common_print(ndo, p, length, caplen, NULL, 0, 454 print_encap_header, encap_header_arg); 455 } 456 457 /* 458 * This is the top level routine of the printer. 'p' points 459 * to the ether header of the packet, 'h->len' is the length 460 * of the packet off the wire, and 'h->caplen' is the number 461 * of bytes actually captured. 462 */ 463 void 464 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, 465 const u_char *p) 466 { 467 ndo->ndo_protocol = "ether"; 468 ndo->ndo_ll_hdr_len += 469 ether_print(ndo, p, h->len, h->caplen, NULL, NULL); 470 } 471 472 /* 473 * This is the top level routine of the printer. 'p' points 474 * to the ether header of the packet, 'h->len' is the length 475 * of the packet off the wire, and 'h->caplen' is the number 476 * of bytes actually captured. 477 * 478 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header 479 * before the Ethernet header. 480 */ 481 void 482 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, 483 const u_char *p) 484 { 485 /* 486 * Fail if we don't have enough data for the Hilscher pseudo-header. 487 */ 488 ndo->ndo_protocol = "netanalyzer"; 489 ND_TCHECK_LEN(p, 4); 490 491 /* Skip the pseudo-header. */ 492 ndo->ndo_ll_hdr_len += 4; 493 ndo->ndo_ll_hdr_len += 494 ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL); 495 } 496 497 /* 498 * This is the top level routine of the printer. 'p' points 499 * to the ether header of the packet, 'h->len' is the length 500 * of the packet off the wire, and 'h->caplen' is the number 501 * of bytes actually captured. 502 * 503 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte 504 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF 505 * before the Ethernet header. 506 */ 507 void 508 netanalyzer_transparent_if_print(netdissect_options *ndo, 509 const struct pcap_pkthdr *h, 510 const u_char *p) 511 { 512 /* 513 * Fail if we don't have enough data for the Hilscher pseudo-header, 514 * preamble, and SOF. 515 */ 516 ndo->ndo_protocol = "netanalyzer_transparent"; 517 ND_TCHECK_LEN(p, 12); 518 519 /* Skip the pseudo-header, preamble, and SOF. */ 520 ndo->ndo_ll_hdr_len += 12; 521 ndo->ndo_ll_hdr_len += 522 ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL); 523 } 524 525 /* 526 * Prints the packet payload, given an Ethernet type code for the payload's 527 * protocol. 528 * 529 * Returns non-zero if it can do so, zero if the ethertype is unknown. 530 */ 531 532 int 533 ethertype_print(netdissect_options *ndo, 534 u_short ether_type, const u_char *p, 535 u_int length, u_int caplen, 536 const struct lladdr_info *src, const struct lladdr_info *dst) 537 { 538 switch (ether_type) { 539 540 case ETHERTYPE_IP: 541 ip_print(ndo, p, length); 542 return (1); 543 544 case ETHERTYPE_IPV6: 545 ip6_print(ndo, p, length); 546 return (1); 547 548 case ETHERTYPE_ARP: 549 case ETHERTYPE_REVARP: 550 arp_print(ndo, p, length, caplen); 551 return (1); 552 553 case ETHERTYPE_DN: 554 decnet_print(ndo, p, length, caplen); 555 return (1); 556 557 case ETHERTYPE_ATALK: 558 if (ndo->ndo_vflag) 559 ND_PRINT("et1 "); 560 atalk_print(ndo, p, length); 561 return (1); 562 563 case ETHERTYPE_AARP: 564 aarp_print(ndo, p, length); 565 return (1); 566 567 case ETHERTYPE_IPX: 568 ND_PRINT("(NOV-ETHII) "); 569 ipx_print(ndo, p, length); 570 return (1); 571 572 case ETHERTYPE_ISO: 573 if (length == 0 || caplen == 0) { 574 ndo->ndo_protocol = "isoclns"; 575 nd_print_trunc(ndo); 576 return (1); 577 } 578 /* At least one byte is required */ 579 /* FIXME: Reference for this byte? */ 580 ND_TCHECK_LEN(p, 1); 581 isoclns_print(ndo, p + 1, length - 1); 582 return(1); 583 584 case ETHERTYPE_PPPOED: 585 case ETHERTYPE_PPPOES: 586 case ETHERTYPE_PPPOED2: 587 case ETHERTYPE_PPPOES2: 588 pppoe_print(ndo, p, length); 589 return (1); 590 591 case ETHERTYPE_EAPOL: 592 eapol_print(ndo, p); 593 return (1); 594 595 case ETHERTYPE_REALTEK: 596 rtl_print(ndo, p, length, src, dst); 597 return (1); 598 599 case ETHERTYPE_PPP: 600 if (length) { 601 ND_PRINT(": "); 602 ppp_print(ndo, p, length); 603 } 604 return (1); 605 606 case ETHERTYPE_MPCP: 607 mpcp_print(ndo, p, length); 608 return (1); 609 610 case ETHERTYPE_SLOW: 611 slow_print(ndo, p, length); 612 return (1); 613 614 case ETHERTYPE_CFM: 615 case ETHERTYPE_CFM_OLD: 616 cfm_print(ndo, p, length); 617 return (1); 618 619 case ETHERTYPE_LLDP: 620 lldp_print(ndo, p, length); 621 return (1); 622 623 case ETHERTYPE_NSH: 624 nsh_print(ndo, p, length); 625 return (1); 626 627 case ETHERTYPE_LOOPBACK: 628 loopback_print(ndo, p, length); 629 return (1); 630 631 case ETHERTYPE_MPLS: 632 case ETHERTYPE_MPLS_MULTI: 633 mpls_print(ndo, p, length); 634 return (1); 635 636 case ETHERTYPE_TIPC: 637 tipc_print(ndo, p, length, caplen); 638 return (1); 639 640 case ETHERTYPE_MS_NLB_HB: 641 msnlb_print(ndo, p); 642 return (1); 643 644 case ETHERTYPE_GEONET_OLD: 645 case ETHERTYPE_GEONET: 646 geonet_print(ndo, p, length, src); 647 return (1); 648 649 case ETHERTYPE_CALM_FAST: 650 calm_fast_print(ndo, p, length, src); 651 return (1); 652 653 case ETHERTYPE_AOE: 654 aoe_print(ndo, p, length); 655 return (1); 656 657 case ETHERTYPE_PTP: 658 ptp_print(ndo, p, length); 659 return (1); 660 661 case ETHERTYPE_LAT: 662 case ETHERTYPE_SCA: 663 case ETHERTYPE_MOPRC: 664 case ETHERTYPE_MOPDL: 665 case ETHERTYPE_IEEE1905_1: 666 /* default_print for now */ 667 default: 668 return (0); 669 } 670 } 671