]> The Tcpdump Group git mirrors - tcpdump/blob - print-ether.c
Treat the length field in an Ethernet header as such.
[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 ND_PRINT("ethertype %s, ", tok2str(ethertype_values,"0x%04x", length_type));
259 p += 4;
260 length -= 4;
261 caplen -= 4;
262 hdrlen += 4;
263 goto recurse;
264 } else if (length_type == ETHERTYPE_JUMBO) {
265 /*
266 * Alteon jumbo frames.
267 * See
268 *
269 * http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
270 *
271 * which indicates that, following the type field,
272 * there's an LLC header and payload.
273 */
274 /* Try to print the LLC-layer header & higher layers */
275 llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
276 if (llc_hdrlen < 0) {
277 /* packet type not known, print raw packet */
278 if (!ndo->ndo_suppress_default_print)
279 ND_DEFAULTPRINT(p, caplen);
280 llc_hdrlen = -llc_hdrlen;
281 }
282 hdrlen += llc_hdrlen;
283 } else {
284 if (ethertype_print(ndo, length_type, p, length, caplen, &src, &dst) == 0) {
285 /* type not known, print raw packet */
286 if (!ndo->ndo_eflag) {
287 if (print_encap_header != NULL)
288 (*print_encap_header)(ndo, encap_header_arg);
289 ether_hdr_print(ndo, (const u_char *)ehp, orig_length,
290 hdrlen);
291 }
292
293 if (!ndo->ndo_suppress_default_print)
294 ND_DEFAULTPRINT(p, caplen);
295 }
296 }
297 return (hdrlen);
298 }
299
300 /*
301 * Print an Ethernet frame.
302 * This might be encapsulated within another frame; we might be passed
303 * a pointer to a function that can print header information for that
304 * frame's protocol, and an argument to pass to that function.
305 *
306 * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
307 */
308 u_int
309 ether_print(netdissect_options *ndo,
310 const u_char *p, u_int length, u_int caplen,
311 void (*print_encap_header)(netdissect_options *ndo, const u_char *),
312 const u_char *encap_header_arg)
313 {
314 return (ether_hdr_len_print(ndo, p, length, caplen,
315 print_encap_header, encap_header_arg,
316 ETHER_HDRLEN));
317 }
318
319 /*
320 * This is the top level routine of the printer. 'p' points
321 * to the ether header of the packet, 'h->len' is the length
322 * of the packet off the wire, and 'h->caplen' is the number
323 * of bytes actually captured.
324 */
325 u_int
326 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
327 const u_char *p)
328 {
329 ndo->ndo_protocol = "ether_if";
330 return (ether_print(ndo, p, h->len, h->caplen, NULL, NULL));
331 }
332
333 /*
334 * This is the top level routine of the printer. 'p' points
335 * to the ether header of the packet, 'h->len' is the length
336 * of the packet off the wire, and 'h->caplen' is the number
337 * of bytes actually captured.
338 *
339 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
340 * before the Ethernet header.
341 */
342 u_int
343 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
344 const u_char *p)
345 {
346 /*
347 * Fail if we don't have enough data for the Hilscher pseudo-header.
348 */
349 ndo->ndo_protocol = "netanalyzer_if";
350 if (h->caplen < 4) {
351 nd_print_trunc(ndo);
352 return (h->caplen);
353 }
354
355 /* Skip the pseudo-header. */
356 return (4 + ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL));
357 }
358
359 /*
360 * This is the top level routine of the printer. 'p' points
361 * to the ether header of the packet, 'h->len' is the length
362 * of the packet off the wire, and 'h->caplen' is the number
363 * of bytes actually captured.
364 *
365 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
366 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
367 * before the Ethernet header.
368 */
369 u_int
370 netanalyzer_transparent_if_print(netdissect_options *ndo,
371 const struct pcap_pkthdr *h,
372 const u_char *p)
373 {
374 /*
375 * Fail if we don't have enough data for the Hilscher pseudo-header,
376 * preamble, and SOF.
377 */
378 ndo->ndo_protocol = "netanalyzer_transparent_if";
379 if (h->caplen < 12) {
380 nd_print_trunc(ndo);
381 return (h->caplen);
382 }
383
384 /* Skip the pseudo-header, preamble, and SOF. */
385 return (12 + ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL));
386 }
387
388 /*
389 * Prints the packet payload, given an Ethernet type code for the payload's
390 * protocol.
391 *
392 * Returns non-zero if it can do so, zero if the ethertype is unknown.
393 */
394
395 int
396 ethertype_print(netdissect_options *ndo,
397 u_short ether_type, const u_char *p,
398 u_int length, u_int caplen,
399 const struct lladdr_info *src, const struct lladdr_info *dst)
400 {
401 switch (ether_type) {
402
403 case ETHERTYPE_IP:
404 ip_print(ndo, p, length);
405 return (1);
406
407 case ETHERTYPE_IPV6:
408 ip6_print(ndo, p, length);
409 return (1);
410
411 case ETHERTYPE_ARP:
412 case ETHERTYPE_REVARP:
413 arp_print(ndo, p, length, caplen);
414 return (1);
415
416 case ETHERTYPE_DN:
417 decnet_print(ndo, p, length, caplen);
418 return (1);
419
420 case ETHERTYPE_ATALK:
421 if (ndo->ndo_vflag)
422 ND_PRINT("et1 ");
423 atalk_print(ndo, p, length);
424 return (1);
425
426 case ETHERTYPE_AARP:
427 aarp_print(ndo, p, length);
428 return (1);
429
430 case ETHERTYPE_IPX:
431 ND_PRINT("(NOV-ETHII) ");
432 ipx_print(ndo, p, length);
433 return (1);
434
435 case ETHERTYPE_ISO:
436 if (length == 0 || caplen == 0) {
437 ndo->ndo_protocol = "isoclns";
438 nd_print_trunc(ndo);
439 return (1);
440 }
441 isoclns_print(ndo, p + 1, length - 1);
442 return(1);
443
444 case ETHERTYPE_PPPOED:
445 case ETHERTYPE_PPPOES:
446 case ETHERTYPE_PPPOED2:
447 case ETHERTYPE_PPPOES2:
448 pppoe_print(ndo, p, length);
449 return (1);
450
451 case ETHERTYPE_EAPOL:
452 eap_print(ndo, p, length);
453 return (1);
454
455 case ETHERTYPE_RRCP:
456 rrcp_print(ndo, p, length, src, dst);
457 return (1);
458
459 case ETHERTYPE_PPP:
460 if (length) {
461 ND_PRINT(": ");
462 ppp_print(ndo, p, length);
463 }
464 return (1);
465
466 case ETHERTYPE_MPCP:
467 mpcp_print(ndo, p, length);
468 return (1);
469
470 case ETHERTYPE_SLOW:
471 slow_print(ndo, p, length);
472 return (1);
473
474 case ETHERTYPE_CFM:
475 case ETHERTYPE_CFM_OLD:
476 cfm_print(ndo, p, length);
477 return (1);
478
479 case ETHERTYPE_LLDP:
480 lldp_print(ndo, p, length);
481 return (1);
482
483 case ETHERTYPE_NSH:
484 nsh_print(ndo, p, length);
485 return (1);
486
487 case ETHERTYPE_LOOPBACK:
488 loopback_print(ndo, p, length);
489 return (1);
490
491 case ETHERTYPE_MPLS:
492 case ETHERTYPE_MPLS_MULTI:
493 mpls_print(ndo, p, length);
494 return (1);
495
496 case ETHERTYPE_TIPC:
497 tipc_print(ndo, p, length, caplen);
498 return (1);
499
500 case ETHERTYPE_MS_NLB_HB:
501 msnlb_print(ndo, p);
502 return (1);
503
504 case ETHERTYPE_GEONET_OLD:
505 case ETHERTYPE_GEONET:
506 geonet_print(ndo, p, length, src);
507 return (1);
508
509 case ETHERTYPE_CALM_FAST:
510 calm_fast_print(ndo, p, length, src);
511 return (1);
512
513 case ETHERTYPE_AOE:
514 aoe_print(ndo, p, length);
515 return (1);
516
517 case ETHERTYPE_LAT:
518 case ETHERTYPE_SCA:
519 case ETHERTYPE_MOPRC:
520 case ETHERTYPE_MOPDL:
521 case ETHERTYPE_IEEE1905_1:
522 /* default_print for now */
523 default:
524 return (0);
525 }
526 }