]> The Tcpdump Group git mirrors - tcpdump/blob - print-ether.c
OpenFlow: Have a function for each message type.
[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 &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 length -= 2;
240 caplen -= 2;
241 p += 2;
242 hdrlen += 2;
243 }
244 }
245
246 /*
247 * Process VLAN tag types.
248 */
249 while (length_type == ETHERTYPE_8021Q ||
250 length_type == ETHERTYPE_8021Q9100 ||
251 length_type == ETHERTYPE_8021Q9200 ||
252 length_type == ETHERTYPE_8021QinQ) {
253 /*
254 * It has a VLAN tag.
255 * Print VLAN information, and then go back and process
256 * the enclosed type field.
257 */
258 if (caplen < 4) {
259 ndo->ndo_protocol = "vlan";
260 nd_print_trunc(ndo);
261 return hdrlen + caplen;
262 }
263 if (length < 4) {
264 ndo->ndo_protocol = "vlan";
265 nd_print_trunc(ndo);
266 return hdrlen + length;
267 }
268 if (ndo->ndo_eflag) {
269 uint16_t tag = GET_BE_U_2(p);
270
271 ether_type_print(ndo, length_type);
272 if (!printed_length) {
273 ND_PRINT(", length %u: ", orig_length);
274 printed_length = 1;
275 } else
276 ND_PRINT(", ");
277 ND_PRINT("%s, ", ieee8021q_tci_string(tag));
278 }
279
280 length_type = GET_BE_U_2(p + 2);
281 p += 4;
282 length -= 4;
283 caplen -= 4;
284 hdrlen += 4;
285 }
286
287 /*
288 * We now have the final length/type field.
289 */
290 if (length_type <= MAX_ETHERNET_LENGTH_VAL) {
291 /*
292 * It's a length field, containing the length of the
293 * remaining payload; use it as such, as long as
294 * it's not too large (bigger than the actual payload).
295 */
296 if (length_type < length) {
297 length = length_type;
298 if (caplen > length)
299 caplen = length;
300 }
301
302 /*
303 * Cut off the snapshot length to the end of the
304 * payload.
305 */
306 nd_push_snapend(ndo, p + length);
307
308 if (ndo->ndo_eflag) {
309 ND_PRINT("802.3");
310 if (!printed_length)
311 ND_PRINT(", length %u: ", length);
312 }
313
314 /*
315 * An LLC header follows the length. Print that and
316 * higher layers.
317 */
318 llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
319 if (llc_hdrlen < 0) {
320 /* packet type not known, print raw packet */
321 if (!ndo->ndo_suppress_default_print)
322 ND_DEFAULTPRINT(p, caplen);
323 llc_hdrlen = -llc_hdrlen;
324 }
325 hdrlen += llc_hdrlen;
326 nd_pop_packet_info(ndo);
327 } else if (length_type == ETHERTYPE_JUMBO) {
328 /*
329 * It's a type field, with the type for Alteon jumbo frames.
330 * See
331 *
332 * https://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
333 *
334 * which indicates that, following the type field,
335 * there's an LLC header and payload.
336 */
337 /* Try to print the LLC-layer header & higher layers */
338 llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
339 if (llc_hdrlen < 0) {
340 /* packet type not known, print raw packet */
341 if (!ndo->ndo_suppress_default_print)
342 ND_DEFAULTPRINT(p, caplen);
343 llc_hdrlen = -llc_hdrlen;
344 }
345 hdrlen += llc_hdrlen;
346 } else if (length_type == ETHERTYPE_ARISTA) {
347 if (caplen < 2) {
348 ND_PRINT("[|arista]");
349 return hdrlen + caplen;
350 }
351 if (length < 2) {
352 ND_PRINT("[|arista]");
353 return hdrlen + length;
354 }
355 ether_type_print(ndo, length_type);
356 ND_PRINT(", length %u: ", orig_length);
357 int bytesConsumed = arista_ethertype_print(ndo, p, length);
358 if (bytesConsumed > 0) {
359 p += bytesConsumed;
360 length -= bytesConsumed;
361 caplen -= bytesConsumed;
362 hdrlen += bytesConsumed;
363 goto recurse;
364 } else {
365 /* subtype/version not known, print raw packet */
366 if (!ndo->ndo_eflag && length_type > MAX_ETHERNET_LENGTH_VAL) {
367 ether_addresses_print(ndo, src.addr, dst.addr);
368 ether_type_print(ndo, length_type);
369 ND_PRINT(", length %u: ", orig_length);
370 }
371 if (!ndo->ndo_suppress_default_print)
372 ND_DEFAULTPRINT(p, caplen);
373 }
374 } else {
375 /*
376 * It's a type field with some other value.
377 */
378 if (ndo->ndo_eflag) {
379 ether_type_print(ndo, length_type);
380 if (!printed_length)
381 ND_PRINT(", length %u: ", orig_length);
382 else
383 ND_PRINT(", ");
384 }
385 if (ethertype_print(ndo, length_type, p, length, caplen, &src, &dst) == 0) {
386 /* type not known, print raw packet */
387 if (!ndo->ndo_eflag) {
388 /*
389 * We didn't print the full link-layer
390 * header, as -e wasn't specified, so
391 * print only the source and destination
392 * MAC addresses and the final Ethernet
393 * type.
394 */
395 ether_addresses_print(ndo, src.addr, dst.addr);
396 ether_type_print(ndo, length_type);
397 ND_PRINT(", length %u: ", orig_length);
398 }
399
400 if (!ndo->ndo_suppress_default_print)
401 ND_DEFAULTPRINT(p, caplen);
402 }
403 }
404 return hdrlen;
405 }
406
407 /*
408 * Print an Ethernet frame while specyfing a non-standard Ethernet header
409 * length.
410 * This might be encapsulated within another frame; we might be passed
411 * a pointer to a function that can print header information for that
412 * frame's protocol, and an argument to pass to that function.
413 *
414 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
415 */
416 u_int
417 ether_switch_tag_print(netdissect_options *ndo, const u_char *p, u_int length,
418 u_int caplen,
419 void (*print_switch_tag)(netdissect_options *, const u_char *),
420 u_int switch_tag_len)
421 {
422 return ether_common_print(ndo, p, length, caplen, print_switch_tag,
423 switch_tag_len, NULL, NULL);
424 }
425
426 /*
427 * Print an Ethernet frame.
428 * This might be encapsulated within another frame; we might be passed
429 * a pointer to a function that can print header information for that
430 * frame's protocol, and an argument to pass to that function.
431 *
432 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
433 */
434 u_int
435 ether_print(netdissect_options *ndo,
436 const u_char *p, u_int length, u_int caplen,
437 void (*print_encap_header)(netdissect_options *ndo, const u_char *),
438 const u_char *encap_header_arg)
439 {
440 ndo->ndo_protocol = "ether";
441 return ether_common_print(ndo, p, length, caplen, NULL, 0,
442 print_encap_header, encap_header_arg);
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 void
452 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
453 const u_char *p)
454 {
455 ndo->ndo_protocol = "ether";
456 ndo->ndo_ll_hdr_len +=
457 ether_print(ndo, p, h->len, h->caplen, NULL, NULL);
458 }
459
460 /*
461 * This is the top level routine of the printer. 'p' points
462 * to the ether header of the packet, 'h->len' is the length
463 * of the packet off the wire, and 'h->caplen' is the number
464 * of bytes actually captured.
465 *
466 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
467 * before the Ethernet header.
468 */
469 void
470 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
471 const u_char *p)
472 {
473 /*
474 * Fail if we don't have enough data for the Hilscher pseudo-header.
475 */
476 ndo->ndo_protocol = "netanalyzer";
477 if (h->caplen < 4) {
478 ndo->ndo_ll_hdr_len += h->caplen;
479 nd_print_trunc(ndo);
480 return;
481 }
482
483 /* Skip the pseudo-header. */
484 ndo->ndo_ll_hdr_len += 4;
485 ndo->ndo_ll_hdr_len +=
486 ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL);
487 }
488
489 /*
490 * This is the top level routine of the printer. 'p' points
491 * to the ether header of the packet, 'h->len' is the length
492 * of the packet off the wire, and 'h->caplen' is the number
493 * of bytes actually captured.
494 *
495 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
496 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
497 * before the Ethernet header.
498 */
499 void
500 netanalyzer_transparent_if_print(netdissect_options *ndo,
501 const struct pcap_pkthdr *h,
502 const u_char *p)
503 {
504 /*
505 * Fail if we don't have enough data for the Hilscher pseudo-header,
506 * preamble, and SOF.
507 */
508 ndo->ndo_protocol = "netanalyzer_transparent";
509 if (h->caplen < 12) {
510 ndo->ndo_ll_hdr_len += h->caplen;
511 nd_print_trunc(ndo);
512 return;
513 }
514
515 /* Skip the pseudo-header, preamble, and SOF. */
516 ndo->ndo_ll_hdr_len += 12;
517 ndo->ndo_ll_hdr_len +=
518 ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL);
519 }
520
521 /*
522 * Prints the packet payload, given an Ethernet type code for the payload's
523 * protocol.
524 *
525 * Returns non-zero if it can do so, zero if the ethertype is unknown.
526 */
527
528 int
529 ethertype_print(netdissect_options *ndo,
530 u_short ether_type, const u_char *p,
531 u_int length, u_int caplen,
532 const struct lladdr_info *src, const struct lladdr_info *dst)
533 {
534 switch (ether_type) {
535
536 case ETHERTYPE_IP:
537 ip_print(ndo, p, length);
538 return (1);
539
540 case ETHERTYPE_IPV6:
541 ip6_print(ndo, p, length);
542 return (1);
543
544 case ETHERTYPE_ARP:
545 case ETHERTYPE_REVARP:
546 arp_print(ndo, p, length, caplen);
547 return (1);
548
549 case ETHERTYPE_DN:
550 decnet_print(ndo, p, length, caplen);
551 return (1);
552
553 case ETHERTYPE_ATALK:
554 if (ndo->ndo_vflag)
555 ND_PRINT("et1 ");
556 atalk_print(ndo, p, length);
557 return (1);
558
559 case ETHERTYPE_AARP:
560 aarp_print(ndo, p, length);
561 return (1);
562
563 case ETHERTYPE_IPX:
564 ND_PRINT("(NOV-ETHII) ");
565 ipx_print(ndo, p, length);
566 return (1);
567
568 case ETHERTYPE_ISO:
569 if (length == 0 || caplen == 0) {
570 ndo->ndo_protocol = "isoclns";
571 nd_print_trunc(ndo);
572 return (1);
573 }
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_RRCP:
589 rrcp_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 }