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 #include <config.h> 25 26 #include "netdissect-stdinc.h" 27 28 #define ND_LONGJMP_FROM_TCHECK 29 #include "netdissect.h" 30 #include "extract.h" 31 #include "addrtoname.h" 32 #include "ethertype.h" 33 34 /* 35 * Structure of an Ethernet header. 36 */ 37 struct ether_header { 38 nd_mac_addr ether_dhost; 39 nd_mac_addr ether_shost; 40 nd_uint16_t ether_length_type; 41 }; 42 43 /* 44 * Length of an Ethernet header; note that some compilers may pad 45 * "struct ether_header" to a multiple of 4 bytes, for example, so 46 * "sizeof (struct ether_header)" may not give the right answer. 47 */ 48 #define ETHER_HDRLEN 14 49 50 const struct tok ethertype_values[] = { 51 { ETHERTYPE_IP, "IPv4" }, 52 { ETHERTYPE_MPLS, "MPLS unicast" }, 53 { ETHERTYPE_MPLS_MULTI, "MPLS multicast" }, 54 { ETHERTYPE_IPV6, "IPv6" }, 55 { ETHERTYPE_8021Q, "802.1Q" }, 56 { ETHERTYPE_8021Q9100, "802.1Q-9100" }, 57 { ETHERTYPE_8021QinQ, "802.1Q-QinQ" }, 58 { ETHERTYPE_8021Q9200, "802.1Q-9200" }, 59 { ETHERTYPE_MACSEC, "802.1AE MACsec" }, 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_REALTEK, "Realtek protocols" }, 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 (length < caplen) { 152 ND_PRINT("[length %u < caplen %u]", length, caplen); 153 nd_print_invalid(ndo); 154 return length; 155 } 156 if (caplen < ETHER_HDRLEN + switch_tag_len) { 157 nd_print_trunc(ndo); 158 return caplen; 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 &src, &dst); 224 225 if (ret == 0) { 226 /* Payload is encrypted; print it as raw data. */ 227 if (!ndo->ndo_suppress_default_print) 228 ND_DEFAULTPRINT(p, caplen); 229 return hdrlen; 230 } else if (ret > 0) { 231 /* Problem printing the header; just quit. */ 232 return ret; 233 } else { 234 /* 235 * Keep processing type/length fields. 236 */ 237 length_type = GET_BE_U_2(p); 238 239 ND_ICHECK_U(caplen, <, 2); 240 length -= 2; 241 caplen -= 2; 242 p += 2; 243 hdrlen += 2; 244 } 245 } 246 247 /* 248 * Process VLAN tag types. 249 */ 250 while (length_type == ETHERTYPE_8021Q || 251 length_type == ETHERTYPE_8021Q9100 || 252 length_type == ETHERTYPE_8021Q9200 || 253 length_type == ETHERTYPE_8021QinQ) { 254 /* 255 * It has a VLAN tag. 256 * Print VLAN information, and then go back and process 257 * the enclosed type field. 258 */ 259 if (caplen < 4) { 260 ndo->ndo_protocol = "vlan"; 261 nd_print_trunc(ndo); 262 return hdrlen + caplen; 263 } 264 if (length < 4) { 265 ndo->ndo_protocol = "vlan"; 266 nd_print_trunc(ndo); 267 return hdrlen + length; 268 } 269 if (ndo->ndo_eflag) { 270 uint16_t tag = GET_BE_U_2(p); 271 272 ether_type_print(ndo, length_type); 273 if (!printed_length) { 274 ND_PRINT(", length %u: ", orig_length); 275 printed_length = 1; 276 } else 277 ND_PRINT(", "); 278 ND_PRINT("%s, ", ieee8021q_tci_string(tag)); 279 } 280 281 length_type = GET_BE_U_2(p + 2); 282 p += 4; 283 length -= 4; 284 caplen -= 4; 285 hdrlen += 4; 286 } 287 288 /* 289 * We now have the final length/type field. 290 */ 291 if (length_type <= MAX_ETHERNET_LENGTH_VAL) { 292 /* 293 * It's a length field, containing the length of the 294 * remaining payload; use it as such, as long as 295 * it's not too large (bigger than the actual payload). 296 */ 297 if (length_type < length) { 298 length = length_type; 299 if (caplen > length) 300 caplen = length; 301 } 302 303 /* 304 * Cut off the snapshot length to the end of the 305 * payload. 306 */ 307 if (!nd_push_snaplen(ndo, p, length)) { 308 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, 309 "%s: can't push snaplen on buffer stack", __func__); 310 } 311 312 if (ndo->ndo_eflag) { 313 ND_PRINT("802.3"); 314 if (!printed_length) 315 ND_PRINT(", length %u: ", length); 316 } 317 318 /* 319 * An LLC header follows the length. Print that and 320 * higher layers. 321 */ 322 llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst); 323 if (llc_hdrlen < 0) { 324 /* packet type not known, print raw packet */ 325 if (!ndo->ndo_suppress_default_print) 326 ND_DEFAULTPRINT(p, caplen); 327 llc_hdrlen = -llc_hdrlen; 328 } 329 hdrlen += llc_hdrlen; 330 nd_pop_packet_info(ndo); 331 } else if (length_type == ETHERTYPE_JUMBO) { 332 /* 333 * It's a type field, with the type for Alteon jumbo frames. 334 * See 335 * 336 * https://tools.ietf.org/html/draft-ietf-isis-ext-eth-01 337 * 338 * which indicates that, following the type field, 339 * there's an LLC header and payload. 340 */ 341 /* Try to print the LLC-layer header & higher layers */ 342 llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst); 343 if (llc_hdrlen < 0) { 344 /* packet type not known, print raw packet */ 345 if (!ndo->ndo_suppress_default_print) 346 ND_DEFAULTPRINT(p, caplen); 347 llc_hdrlen = -llc_hdrlen; 348 } 349 hdrlen += llc_hdrlen; 350 } else if (length_type == ETHERTYPE_ARISTA) { 351 if (caplen < 2) { 352 ND_PRINT("[|arista]"); 353 return hdrlen + caplen; 354 } 355 if (length < 2) { 356 ND_PRINT("[|arista]"); 357 return hdrlen + length; 358 } 359 ether_type_print(ndo, length_type); 360 ND_PRINT(", length %u: ", orig_length); 361 int bytesConsumed = arista_ethertype_print(ndo, p, length); 362 if (bytesConsumed > 0) { 363 p += bytesConsumed; 364 length -= bytesConsumed; 365 caplen -= bytesConsumed; 366 hdrlen += bytesConsumed; 367 goto recurse; 368 } else { 369 /* subtype/version not known, print raw packet */ 370 if (!ndo->ndo_eflag && length_type > MAX_ETHERNET_LENGTH_VAL) { 371 ether_addresses_print(ndo, src.addr, dst.addr); 372 ether_type_print(ndo, length_type); 373 ND_PRINT(", length %u: ", orig_length); 374 } 375 if (!ndo->ndo_suppress_default_print) 376 ND_DEFAULTPRINT(p, caplen); 377 } 378 } else { 379 /* 380 * It's a type field with some other value. 381 */ 382 if (ndo->ndo_eflag) { 383 ether_type_print(ndo, length_type); 384 if (!printed_length) 385 ND_PRINT(", length %u: ", orig_length); 386 else 387 ND_PRINT(", "); 388 } 389 if (ethertype_print(ndo, length_type, p, length, caplen, &src, &dst) == 0) { 390 /* type not known, print raw packet */ 391 if (!ndo->ndo_eflag) { 392 /* 393 * We didn't print the full link-layer 394 * header, as -e wasn't specified, so 395 * print only the source and destination 396 * MAC addresses and the final Ethernet 397 * type. 398 */ 399 ether_addresses_print(ndo, src.addr, dst.addr); 400 ether_type_print(ndo, length_type); 401 ND_PRINT(", length %u: ", orig_length); 402 } 403 404 if (!ndo->ndo_suppress_default_print) 405 ND_DEFAULTPRINT(p, caplen); 406 } 407 } 408 invalid: 409 return hdrlen; 410 } 411 412 /* 413 * Print an Ethernet frame while specifying a non-standard Ethernet header 414 * length. 415 * This might be encapsulated within another frame; we might be passed 416 * a pointer to a function that can print header information for that 417 * frame's protocol, and an argument to pass to that function. 418 * 419 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p. 420 */ 421 u_int 422 ether_switch_tag_print(netdissect_options *ndo, const u_char *p, u_int length, 423 u_int caplen, 424 void (*print_switch_tag)(netdissect_options *, const u_char *), 425 u_int switch_tag_len) 426 { 427 return ether_common_print(ndo, p, length, caplen, print_switch_tag, 428 switch_tag_len, NULL, NULL); 429 } 430 431 /* 432 * Print an Ethernet frame. 433 * This might be encapsulated within another frame; we might be passed 434 * a pointer to a function that can print header information for that 435 * frame's protocol, and an argument to pass to that function. 436 * 437 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p. 438 */ 439 u_int 440 ether_print(netdissect_options *ndo, 441 const u_char *p, u_int length, u_int caplen, 442 void (*print_encap_header)(netdissect_options *ndo, const u_char *), 443 const u_char *encap_header_arg) 444 { 445 ndo->ndo_protocol = "ether"; 446 return ether_common_print(ndo, p, length, caplen, NULL, 0, 447 print_encap_header, encap_header_arg); 448 } 449 450 /* 451 * This is the top level routine of the printer. 'p' points 452 * to the ether header of the packet, 'h->len' is the length 453 * of the packet off the wire, and 'h->caplen' is the number 454 * of bytes actually captured. 455 */ 456 void 457 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, 458 const u_char *p) 459 { 460 ndo->ndo_protocol = "ether"; 461 ndo->ndo_ll_hdr_len += 462 ether_print(ndo, p, h->len, h->caplen, NULL, NULL); 463 } 464 465 /* 466 * This is the top level routine of the printer. 'p' points 467 * to the ether header of the packet, 'h->len' is the length 468 * of the packet off the wire, and 'h->caplen' is the number 469 * of bytes actually captured. 470 * 471 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header 472 * before the Ethernet header. 473 */ 474 void 475 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, 476 const u_char *p) 477 { 478 /* 479 * Fail if we don't have enough data for the Hilscher pseudo-header. 480 */ 481 ndo->ndo_protocol = "netanalyzer"; 482 ND_TCHECK_LEN(p, 4); 483 484 /* Skip the pseudo-header. */ 485 ndo->ndo_ll_hdr_len += 4; 486 ndo->ndo_ll_hdr_len += 487 ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL); 488 } 489 490 /* 491 * This is the top level routine of the printer. 'p' points 492 * to the ether header of the packet, 'h->len' is the length 493 * of the packet off the wire, and 'h->caplen' is the number 494 * of bytes actually captured. 495 * 496 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte 497 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF 498 * before the Ethernet header. 499 */ 500 void 501 netanalyzer_transparent_if_print(netdissect_options *ndo, 502 const struct pcap_pkthdr *h, 503 const u_char *p) 504 { 505 /* 506 * Fail if we don't have enough data for the Hilscher pseudo-header, 507 * preamble, and SOF. 508 */ 509 ndo->ndo_protocol = "netanalyzer_transparent"; 510 ND_TCHECK_LEN(p, 12); 511 512 /* Skip the pseudo-header, preamble, and SOF. */ 513 ndo->ndo_ll_hdr_len += 12; 514 ndo->ndo_ll_hdr_len += 515 ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL); 516 } 517 518 /* 519 * Prints the packet payload, given an Ethernet type code for the payload's 520 * protocol. 521 * 522 * Returns non-zero if it can do so, zero if the ethertype is unknown. 523 */ 524 525 int 526 ethertype_print(netdissect_options *ndo, 527 u_short ether_type, const u_char *p, 528 u_int length, u_int caplen, 529 const struct lladdr_info *src, const struct lladdr_info *dst) 530 { 531 switch (ether_type) { 532 533 case ETHERTYPE_IP: 534 ip_print(ndo, p, length); 535 return (1); 536 537 case ETHERTYPE_IPV6: 538 ip6_print(ndo, p, length); 539 return (1); 540 541 case ETHERTYPE_ARP: 542 case ETHERTYPE_REVARP: 543 arp_print(ndo, p, length, caplen); 544 return (1); 545 546 case ETHERTYPE_DN: 547 decnet_print(ndo, p, length, caplen); 548 return (1); 549 550 case ETHERTYPE_ATALK: 551 if (ndo->ndo_vflag) 552 ND_PRINT("et1 "); 553 atalk_print(ndo, p, length); 554 return (1); 555 556 case ETHERTYPE_AARP: 557 aarp_print(ndo, p, length); 558 return (1); 559 560 case ETHERTYPE_IPX: 561 ND_PRINT("(NOV-ETHII) "); 562 ipx_print(ndo, p, length); 563 return (1); 564 565 case ETHERTYPE_ISO: 566 if (length == 0 || caplen == 0) { 567 ndo->ndo_protocol = "isoclns"; 568 nd_print_trunc(ndo); 569 return (1); 570 } 571 /* At least one byte is required */ 572 /* FIXME: Reference for this byte? */ 573 ND_TCHECK_LEN(p, 1); 574 isoclns_print(ndo, p + 1, length - 1); 575 return(1); 576 577 case ETHERTYPE_PPPOED: 578 case ETHERTYPE_PPPOES: 579 case ETHERTYPE_PPPOED2: 580 case ETHERTYPE_PPPOES2: 581 pppoe_print(ndo, p, length); 582 return (1); 583 584 case ETHERTYPE_EAPOL: 585 eapol_print(ndo, p); 586 return (1); 587 588 case ETHERTYPE_REALTEK: 589 rtl_print(ndo, p, length, src, dst); 590 return (1); 591 592 case ETHERTYPE_PPP: 593 if (length) { 594 ND_PRINT(": "); 595 ppp_print(ndo, p, length); 596 } 597 return (1); 598 599 case ETHERTYPE_MPCP: 600 mpcp_print(ndo, p, length); 601 return (1); 602 603 case ETHERTYPE_SLOW: 604 slow_print(ndo, p, length); 605 return (1); 606 607 case ETHERTYPE_CFM: 608 case ETHERTYPE_CFM_OLD: 609 cfm_print(ndo, p, length); 610 return (1); 611 612 case ETHERTYPE_LLDP: 613 lldp_print(ndo, p, length); 614 return (1); 615 616 case ETHERTYPE_NSH: 617 nsh_print(ndo, p, length); 618 return (1); 619 620 case ETHERTYPE_LOOPBACK: 621 loopback_print(ndo, p, length); 622 return (1); 623 624 case ETHERTYPE_MPLS: 625 case ETHERTYPE_MPLS_MULTI: 626 mpls_print(ndo, p, length); 627 return (1); 628 629 case ETHERTYPE_TIPC: 630 tipc_print(ndo, p, length, caplen); 631 return (1); 632 633 case ETHERTYPE_MS_NLB_HB: 634 msnlb_print(ndo, p); 635 return (1); 636 637 case ETHERTYPE_GEONET_OLD: 638 case ETHERTYPE_GEONET: 639 geonet_print(ndo, p, length, src); 640 return (1); 641 642 case ETHERTYPE_CALM_FAST: 643 calm_fast_print(ndo, p, length, src); 644 return (1); 645 646 case ETHERTYPE_AOE: 647 aoe_print(ndo, p, length); 648 return (1); 649 650 case ETHERTYPE_PTP: 651 ptp_print(ndo, p, length); 652 return (1); 653 654 case ETHERTYPE_LAT: 655 case ETHERTYPE_SCA: 656 case ETHERTYPE_MOPRC: 657 case ETHERTYPE_MOPDL: 658 case ETHERTYPE_IEEE1905_1: 659 /* default_print for now */ 660 default: 661 return (0); 662 } 663 } 664