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