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