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