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