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