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.7 2017/01/24 23:29:14 christos Exp $"); 25 #endif 26 27 #ifdef HAVE_CONFIG_H 28 #include "config.h" 29 #endif 30 31 #include <netdissect-stdinc.h> 32 33 #include "netdissect.h" 34 #include "extract.h" 35 #include "addrtoname.h" 36 #include "ethertype.h" 37 #include "ether.h" 38 39 const struct tok ethertype_values[] = { 40 { ETHERTYPE_IP, "IPv4" }, 41 { ETHERTYPE_MPLS, "MPLS unicast" }, 42 { ETHERTYPE_MPLS_MULTI, "MPLS multicast" }, 43 { ETHERTYPE_IPV6, "IPv6" }, 44 { ETHERTYPE_8021Q, "802.1Q" }, 45 { ETHERTYPE_8021Q9100, "802.1Q-9100" }, 46 { ETHERTYPE_8021QinQ, "802.1Q-QinQ" }, 47 { ETHERTYPE_8021Q9200, "802.1Q-9200" }, 48 { ETHERTYPE_VMAN, "VMAN" }, 49 { ETHERTYPE_PUP, "PUP" }, 50 { ETHERTYPE_ARP, "ARP"}, 51 { ETHERTYPE_REVARP, "Reverse ARP"}, 52 { ETHERTYPE_NS, "NS" }, 53 { ETHERTYPE_SPRITE, "Sprite" }, 54 { ETHERTYPE_TRAIL, "Trail" }, 55 { ETHERTYPE_MOPDL, "MOP DL" }, 56 { ETHERTYPE_MOPRC, "MOP RC" }, 57 { ETHERTYPE_DN, "DN" }, 58 { ETHERTYPE_LAT, "LAT" }, 59 { ETHERTYPE_SCA, "SCA" }, 60 { ETHERTYPE_TEB, "TEB" }, 61 { ETHERTYPE_LANBRIDGE, "Lanbridge" }, 62 { ETHERTYPE_DECDNS, "DEC DNS" }, 63 { ETHERTYPE_DECDTS, "DEC DTS" }, 64 { ETHERTYPE_VEXP, "VEXP" }, 65 { ETHERTYPE_VPROD, "VPROD" }, 66 { ETHERTYPE_ATALK, "Appletalk" }, 67 { ETHERTYPE_AARP, "Appletalk ARP" }, 68 { ETHERTYPE_IPX, "IPX" }, 69 { ETHERTYPE_PPP, "PPP" }, 70 { ETHERTYPE_MPCP, "MPCP" }, 71 { ETHERTYPE_SLOW, "Slow Protocols" }, 72 { ETHERTYPE_PPPOED, "PPPoE D" }, 73 { ETHERTYPE_PPPOES, "PPPoE S" }, 74 { ETHERTYPE_EAPOL, "EAPOL" }, 75 { ETHERTYPE_RRCP, "RRCP" }, 76 { ETHERTYPE_MS_NLB_HB, "MS NLB heartbeat" }, 77 { ETHERTYPE_JUMBO, "Jumbo" }, 78 { ETHERTYPE_LOOPBACK, "Loopback" }, 79 { ETHERTYPE_ISO, "OSI" }, 80 { ETHERTYPE_GRE_ISO, "GRE-OSI" }, 81 { ETHERTYPE_CFM_OLD, "CFM (old)" }, 82 { ETHERTYPE_CFM, "CFM" }, 83 { ETHERTYPE_IEEE1905_1, "IEEE1905.1" }, 84 { ETHERTYPE_LLDP, "LLDP" }, 85 { ETHERTYPE_TIPC, "TIPC"}, 86 { ETHERTYPE_GEONET_OLD, "GeoNet (old)"}, 87 { ETHERTYPE_GEONET, "GeoNet"}, 88 { ETHERTYPE_CALM_FAST, "CALM FAST"}, 89 { ETHERTYPE_AOE, "AoE" }, 90 { ETHERTYPE_MEDSA, "MEDSA" }, 91 { 0, NULL} 92 }; 93 94 static inline void 95 ether_hdr_print(netdissect_options *ndo, 96 const u_char *bp, u_int length) 97 { 98 register const struct ether_header *ep; 99 uint16_t length_type; 100 101 ep = (const struct ether_header *)bp; 102 103 ND_PRINT((ndo, "%s > %s", 104 etheraddr_string(ndo, ESRC(ep)), 105 etheraddr_string(ndo, EDST(ep)))); 106 107 length_type = EXTRACT_16BITS(&ep->ether_length_type); 108 if (!ndo->ndo_qflag) { 109 if (length_type <= ETHERMTU) { 110 ND_PRINT((ndo, ", 802.3")); 111 length = length_type; 112 } else 113 ND_PRINT((ndo, ", ethertype %s (0x%04x)", 114 tok2str(ethertype_values,"Unknown", length_type), 115 length_type)); 116 } else { 117 if (length_type <= ETHERMTU) { 118 ND_PRINT((ndo, ", 802.3")); 119 length = length_type; 120 } else 121 ND_PRINT((ndo, ", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", length_type))); 122 } 123 124 ND_PRINT((ndo, ", length %u: ", length)); 125 } 126 127 /* 128 * Print an Ethernet frame. 129 * This might be encapsulated within another frame; we might be passed 130 * a pointer to a function that can print header information for that 131 * frame's protocol, and an argument to pass to that function. 132 */ 133 u_int 134 ether_print(netdissect_options *ndo, 135 const u_char *p, u_int length, u_int caplen, 136 void (*print_encap_header)(netdissect_options *ndo, const u_char *), const u_char *encap_header_arg) 137 { 138 const struct ether_header *ep; 139 u_int orig_length; 140 u_short length_type; 141 u_int hdrlen; 142 int llc_hdrlen; 143 144 if (caplen < ETHER_HDRLEN) { 145 ND_PRINT((ndo, "[|ether]")); 146 return (caplen); 147 } 148 if (length < ETHER_HDRLEN) { 149 ND_PRINT((ndo, "[|ether]")); 150 return (length); 151 } 152 153 if (ndo->ndo_eflag) { 154 if (print_encap_header != NULL) 155 (*print_encap_header)(ndo, encap_header_arg); 156 ether_hdr_print(ndo, p, length); 157 } 158 orig_length = length; 159 160 length -= ETHER_HDRLEN; 161 caplen -= ETHER_HDRLEN; 162 ep = (const struct ether_header *)p; 163 p += ETHER_HDRLEN; 164 hdrlen = ETHER_HDRLEN; 165 166 length_type = EXTRACT_16BITS(&ep->ether_length_type); 167 168 recurse: 169 /* 170 * Is it (gag) an 802.3 encapsulation? 171 */ 172 if (length_type <= ETHERMTU) { 173 /* Try to print the LLC-layer header & higher layers */ 174 llc_hdrlen = llc_print(ndo, p, length, caplen, ESRC(ep), EDST(ep)); 175 if (llc_hdrlen < 0) { 176 /* packet type not known, print raw packet */ 177 if (!ndo->ndo_suppress_default_print) 178 ND_DEFAULTPRINT(p, caplen); 179 llc_hdrlen = -llc_hdrlen; 180 } 181 hdrlen += llc_hdrlen; 182 } else if (length_type == ETHERTYPE_8021Q || 183 length_type == ETHERTYPE_8021Q9100 || 184 length_type == ETHERTYPE_8021Q9200 || 185 length_type == ETHERTYPE_8021QinQ) { 186 /* 187 * Print VLAN information, and then go back and process 188 * the enclosed type field. 189 */ 190 if (caplen < 4) { 191 ND_PRINT((ndo, "[|vlan]")); 192 return (hdrlen + caplen); 193 } 194 if (length < 4) { 195 ND_PRINT((ndo, "[|vlan]")); 196 return (hdrlen + length); 197 } 198 if (ndo->ndo_eflag) { 199 uint16_t tag = EXTRACT_16BITS(p); 200 201 ND_PRINT((ndo, "%s, ", ieee8021q_tci_string(tag))); 202 } 203 204 length_type = EXTRACT_16BITS(p + 2); 205 if (ndo->ndo_eflag && length_type > ETHERMTU) 206 ND_PRINT((ndo, "ethertype %s, ", tok2str(ethertype_values,"0x%04x", length_type))); 207 p += 4; 208 length -= 4; 209 caplen -= 4; 210 hdrlen += 4; 211 goto recurse; 212 } else if (length_type == ETHERTYPE_JUMBO) { 213 /* 214 * Alteon jumbo frames. 215 * See 216 * 217 * http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01 218 * 219 * which indicates that, following the type field, 220 * there's an LLC header and payload. 221 */ 222 /* Try to print the LLC-layer header & higher layers */ 223 llc_hdrlen = llc_print(ndo, p, length, caplen, ESRC(ep), EDST(ep)); 224 if (llc_hdrlen < 0) { 225 /* packet type not known, print raw packet */ 226 if (!ndo->ndo_suppress_default_print) 227 ND_DEFAULTPRINT(p, caplen); 228 llc_hdrlen = -llc_hdrlen; 229 } 230 hdrlen += llc_hdrlen; 231 } else { 232 if (ethertype_print(ndo, length_type, p, length, caplen) == 0) { 233 /* type not known, print raw packet */ 234 if (!ndo->ndo_eflag) { 235 if (print_encap_header != NULL) 236 (*print_encap_header)(ndo, encap_header_arg); 237 ether_hdr_print(ndo, (const u_char *)ep, orig_length); 238 } 239 240 if (!ndo->ndo_suppress_default_print) 241 ND_DEFAULTPRINT(p, caplen); 242 } 243 } 244 return (hdrlen); 245 } 246 247 /* 248 * This is the top level routine of the printer. 'p' points 249 * to the ether header of the packet, 'h->len' is the length 250 * of the packet off the wire, and 'h->caplen' is the number 251 * of bytes actually captured. 252 */ 253 u_int 254 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, 255 const u_char *p) 256 { 257 return (ether_print(ndo, p, h->len, h->caplen, NULL, NULL)); 258 } 259 260 /* 261 * This is the top level routine of the printer. 'p' points 262 * to the ether header of the packet, 'h->len' is the length 263 * of the packet off the wire, and 'h->caplen' is the number 264 * of bytes actually captured. 265 * 266 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header 267 * before the Ethernet header. 268 */ 269 u_int 270 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, 271 const u_char *p) 272 { 273 /* 274 * Fail if we don't have enough data for the Hilscher pseudo-header. 275 */ 276 if (h->len < 4 || h->caplen < 4) { 277 ND_PRINT((ndo, "[|netanalyzer]")); 278 return (h->caplen); 279 } 280 281 /* Skip the pseudo-header. */ 282 return (4 + ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, 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_TRANSPARENT, which has a 4-byte 292 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF 293 * before the Ethernet header. 294 */ 295 u_int 296 netanalyzer_transparent_if_print(netdissect_options *ndo, 297 const struct pcap_pkthdr *h, 298 const u_char *p) 299 { 300 /* 301 * Fail if we don't have enough data for the Hilscher pseudo-header, 302 * preamble, and SOF. 303 */ 304 if (h->len < 12 || h->caplen < 12) { 305 ND_PRINT((ndo, "[|netanalyzer-transparent]")); 306 return (h->caplen); 307 } 308 309 /* Skip the pseudo-header, preamble, and SOF. */ 310 return (12 + ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL)); 311 } 312 313 /* 314 * Prints the packet payload, given an Ethernet type code for the payload's 315 * protocol. 316 * 317 * Returns non-zero if it can do so, zero if the ethertype is unknown. 318 */ 319 320 int 321 ethertype_print(netdissect_options *ndo, 322 u_short ether_type, const u_char *p, 323 u_int length, u_int caplen) 324 { 325 switch (ether_type) { 326 327 case ETHERTYPE_IP: 328 ip_print(ndo, p, length); 329 return (1); 330 331 case ETHERTYPE_IPV6: 332 ip6_print(ndo, p, length); 333 return (1); 334 335 case ETHERTYPE_ARP: 336 case ETHERTYPE_REVARP: 337 arp_print(ndo, p, length, caplen); 338 return (1); 339 340 case ETHERTYPE_DN: 341 decnet_print(ndo, p, length, caplen); 342 return (1); 343 344 case ETHERTYPE_ATALK: 345 if (ndo->ndo_vflag) 346 ND_PRINT((ndo, "et1 ")); 347 atalk_print(ndo, p, length); 348 return (1); 349 350 case ETHERTYPE_AARP: 351 aarp_print(ndo, p, length); 352 return (1); 353 354 case ETHERTYPE_IPX: 355 ND_PRINT((ndo, "(NOV-ETHII) ")); 356 ipx_print(ndo, p, length); 357 return (1); 358 359 case ETHERTYPE_ISO: 360 isoclns_print(ndo, p + 1, length - 1, length - 1); 361 return(1); 362 363 case ETHERTYPE_PPPOED: 364 case ETHERTYPE_PPPOES: 365 case ETHERTYPE_PPPOED2: 366 case ETHERTYPE_PPPOES2: 367 pppoe_print(ndo, p, length); 368 return (1); 369 370 case ETHERTYPE_EAPOL: 371 eap_print(ndo, p, length); 372 return (1); 373 374 case ETHERTYPE_RRCP: 375 rrcp_print(ndo, p - 14 , length + 14); 376 return (1); 377 378 case ETHERTYPE_PPP: 379 if (length) { 380 ND_PRINT((ndo, ": ")); 381 ppp_print(ndo, p, length); 382 } 383 return (1); 384 385 case ETHERTYPE_MPCP: 386 mpcp_print(ndo, p, length); 387 return (1); 388 389 case ETHERTYPE_SLOW: 390 slow_print(ndo, p, length); 391 return (1); 392 393 case ETHERTYPE_CFM: 394 case ETHERTYPE_CFM_OLD: 395 cfm_print(ndo, p, length); 396 return (1); 397 398 case ETHERTYPE_LLDP: 399 lldp_print(ndo, p, length); 400 return (1); 401 402 case ETHERTYPE_LOOPBACK: 403 loopback_print(ndo, p, length); 404 return (1); 405 406 case ETHERTYPE_MPLS: 407 case ETHERTYPE_MPLS_MULTI: 408 mpls_print(ndo, p, length); 409 return (1); 410 411 case ETHERTYPE_TIPC: 412 tipc_print(ndo, p, length, caplen); 413 return (1); 414 415 case ETHERTYPE_MS_NLB_HB: 416 msnlb_print(ndo, p); 417 return (1); 418 419 case ETHERTYPE_GEONET_OLD: 420 case ETHERTYPE_GEONET: 421 geonet_print(ndo, p-14, p, length); 422 return (1); 423 424 case ETHERTYPE_CALM_FAST: 425 calm_fast_print(ndo, p-14, p, length); 426 return (1); 427 428 case ETHERTYPE_AOE: 429 aoe_print(ndo, p, length); 430 return (1); 431 432 case ETHERTYPE_MEDSA: 433 medsa_print(ndo, p, length, caplen); 434 return (1); 435 436 case ETHERTYPE_LAT: 437 case ETHERTYPE_SCA: 438 case ETHERTYPE_MOPRC: 439 case ETHERTYPE_MOPDL: 440 case ETHERTYPE_IEEE1905_1: 441 /* default_print for now */ 442 default: 443 return (0); 444 } 445 } 446 447 448 /* 449 * Local Variables: 450 * c-style: whitesmith 451 * c-basic-offset: 8 452 * End: 453 */ 454 455