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