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