]> The Tcpdump Group git mirrors - tcpdump/blob - print-ether.c
Add support for the Marvell Extended Distributed Switch Architecture header
[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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <tcpdump-stdinc.h>
27
28 #include "interface.h"
29 #include "extract.h"
30 #include "addrtoname.h"
31 #include "ethertype.h"
32 #include "ether.h"
33
34 const struct tok ethertype_values[] = {
35 { ETHERTYPE_IP, "IPv4" },
36 { ETHERTYPE_MPLS, "MPLS unicast" },
37 { ETHERTYPE_MPLS_MULTI, "MPLS multicast" },
38 { ETHERTYPE_IPV6, "IPv6" },
39 { ETHERTYPE_8021Q, "802.1Q" },
40 { ETHERTYPE_8021Q9100, "802.1Q-9100" },
41 { ETHERTYPE_8021QinQ, "802.1Q-QinQ" },
42 { ETHERTYPE_8021Q9200, "802.1Q-9200" },
43 { ETHERTYPE_VMAN, "VMAN" },
44 { ETHERTYPE_PUP, "PUP" },
45 { ETHERTYPE_ARP, "ARP"},
46 { ETHERTYPE_REVARP, "Reverse ARP"},
47 { ETHERTYPE_NS, "NS" },
48 { ETHERTYPE_SPRITE, "Sprite" },
49 { ETHERTYPE_TRAIL, "Trail" },
50 { ETHERTYPE_MOPDL, "MOP DL" },
51 { ETHERTYPE_MOPRC, "MOP RC" },
52 { ETHERTYPE_DN, "DN" },
53 { ETHERTYPE_LAT, "LAT" },
54 { ETHERTYPE_SCA, "SCA" },
55 { ETHERTYPE_TEB, "TEB" },
56 { ETHERTYPE_LANBRIDGE, "Lanbridge" },
57 { ETHERTYPE_DECDNS, "DEC DNS" },
58 { ETHERTYPE_DECDTS, "DEC DTS" },
59 { ETHERTYPE_VEXP, "VEXP" },
60 { ETHERTYPE_VPROD, "VPROD" },
61 { ETHERTYPE_ATALK, "Appletalk" },
62 { ETHERTYPE_AARP, "Appletalk ARP" },
63 { ETHERTYPE_IPX, "IPX" },
64 { ETHERTYPE_PPP, "PPP" },
65 { ETHERTYPE_MPCP, "MPCP" },
66 { ETHERTYPE_SLOW, "Slow Protocols" },
67 { ETHERTYPE_PPPOED, "PPPoE D" },
68 { ETHERTYPE_PPPOES, "PPPoE S" },
69 { ETHERTYPE_EAPOL, "EAPOL" },
70 { ETHERTYPE_RRCP, "RRCP" },
71 { ETHERTYPE_MS_NLB_HB, "MS NLB heartbeat" },
72 { ETHERTYPE_JUMBO, "Jumbo" },
73 { ETHERTYPE_LOOPBACK, "Loopback" },
74 { ETHERTYPE_ISO, "OSI" },
75 { ETHERTYPE_GRE_ISO, "GRE-OSI" },
76 { ETHERTYPE_CFM_OLD, "CFM (old)" },
77 { ETHERTYPE_CFM, "CFM" },
78 { ETHERTYPE_IEEE1905_1, "IEEE1905.1" },
79 { ETHERTYPE_LLDP, "LLDP" },
80 { ETHERTYPE_TIPC, "TIPC"},
81 { ETHERTYPE_GEONET_OLD, "GeoNet (old)"},
82 { ETHERTYPE_GEONET, "GeoNet"},
83 { ETHERTYPE_CALM_FAST, "CALM FAST"},
84 { ETHERTYPE_AOE, "AoE" },
85 { ETHERTYPE_MEDSA, "MEDSA" },
86 { 0, NULL}
87 };
88
89 static inline void
90 ether_hdr_print(netdissect_options *ndo,
91 const u_char *bp, u_int length)
92 {
93 register const struct ether_header *ep;
94 uint16_t ether_type;
95
96 ep = (const struct ether_header *)bp;
97
98 ND_PRINT((ndo, "%s > %s",
99 etheraddr_string(ndo, ESRC(ep)),
100 etheraddr_string(ndo, EDST(ep))));
101
102 ether_type = EXTRACT_16BITS(&ep->ether_type);
103 if (!ndo->ndo_qflag) {
104 if (ether_type <= ETHERMTU)
105 ND_PRINT((ndo, ", 802.3"));
106 else
107 ND_PRINT((ndo, ", ethertype %s (0x%04x)",
108 tok2str(ethertype_values,"Unknown", ether_type),
109 ether_type));
110 } else {
111 if (ether_type <= ETHERMTU)
112 ND_PRINT((ndo, ", 802.3"));
113 else
114 ND_PRINT((ndo, ", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", ether_type)));
115 }
116
117 ND_PRINT((ndo, ", length %u: ", length));
118 }
119
120 /*
121 * Print an Ethernet frame.
122 * This might be encapsulated within another frame; we might be passed
123 * a pointer to a function that can print header information for that
124 * frame's protocol, and an argument to pass to that function.
125 */
126 u_int
127 ether_print(netdissect_options *ndo,
128 const u_char *p, u_int length, u_int caplen,
129 void (*print_encap_header)(netdissect_options *ndo, const u_char *), const u_char *encap_header_arg)
130 {
131 struct ether_header *ep;
132 u_int orig_length;
133 u_short ether_type;
134 u_int hdrlen;
135 int llc_hdrlen;
136
137 if (caplen < ETHER_HDRLEN) {
138 ND_PRINT((ndo, "[|ether]"));
139 return (caplen);
140 }
141 if (length < ETHER_HDRLEN) {
142 ND_PRINT((ndo, "[|ether]"));
143 return (length);
144 }
145
146 if (ndo->ndo_eflag) {
147 if (print_encap_header != NULL)
148 (*print_encap_header)(ndo, encap_header_arg);
149 ether_hdr_print(ndo, p, length);
150 }
151 orig_length = length;
152
153 length -= ETHER_HDRLEN;
154 caplen -= ETHER_HDRLEN;
155 ep = (struct ether_header *)p;
156 p += ETHER_HDRLEN;
157 hdrlen = ETHER_HDRLEN;
158
159 ether_type = EXTRACT_16BITS(&ep->ether_type);
160
161 recurse:
162 /*
163 * Is it (gag) an 802.3 encapsulation?
164 */
165 if (ether_type <= ETHERMTU) {
166 /* Try to print the LLC-layer header & higher layers */
167 llc_hdrlen = llc_print(ndo, p, length, caplen, ESRC(ep), EDST(ep));
168 if (llc_hdrlen < 0) {
169 /* packet type not known, print raw packet */
170 if (!ndo->ndo_suppress_default_print)
171 ND_DEFAULTPRINT(p, caplen);
172 llc_hdrlen = -llc_hdrlen;
173 }
174 hdrlen += llc_hdrlen;
175 } else if (ether_type == ETHERTYPE_8021Q ||
176 ether_type == ETHERTYPE_8021Q9100 ||
177 ether_type == ETHERTYPE_8021Q9200 ||
178 ether_type == ETHERTYPE_8021QinQ) {
179 /*
180 * Print VLAN information, and then go back and process
181 * the enclosed type field.
182 */
183 if (caplen < 4) {
184 ND_PRINT((ndo, "[|vlan]"));
185 return (hdrlen + caplen);
186 }
187 if (length < 4) {
188 ND_PRINT((ndo, "[|vlan]"));
189 return (hdrlen + length);
190 }
191 if (ndo->ndo_eflag) {
192 uint16_t tag = EXTRACT_16BITS(p);
193
194 ND_PRINT((ndo, "%s, ", ieee8021q_tci_string(tag)));
195 }
196
197 ether_type = EXTRACT_16BITS(p + 2);
198 if (ndo->ndo_eflag && ether_type > ETHERMTU)
199 ND_PRINT((ndo, "ethertype %s, ", tok2str(ethertype_values,"0x%04x", ether_type)));
200 p += 4;
201 length -= 4;
202 caplen -= 4;
203 hdrlen += 4;
204 goto recurse;
205 } else if (ether_type == ETHERTYPE_JUMBO) {
206 /*
207 * Alteon jumbo frames.
208 * See
209 *
210 * http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
211 *
212 * which indicates that, following the type field,
213 * there's an LLC header and payload.
214 */
215 /* Try to print the LLC-layer header & higher layers */
216 llc_hdrlen = llc_print(ndo, p, length, caplen, ESRC(ep), EDST(ep));
217 if (llc_hdrlen < 0) {
218 /* packet type not known, print raw packet */
219 if (!ndo->ndo_suppress_default_print)
220 ND_DEFAULTPRINT(p, caplen);
221 llc_hdrlen = -llc_hdrlen;
222 }
223 hdrlen += llc_hdrlen;
224 } else {
225 if (ethertype_print(ndo, ether_type, p, length, caplen) == 0) {
226 /* ether_type not known, print raw packet */
227 if (!ndo->ndo_eflag) {
228 if (print_encap_header != NULL)
229 (*print_encap_header)(ndo, encap_header_arg);
230 ether_hdr_print(ndo, (u_char *)ep, orig_length);
231 }
232
233 if (!ndo->ndo_suppress_default_print)
234 ND_DEFAULTPRINT(p, caplen);
235 }
236 }
237 return (hdrlen);
238 }
239
240 /*
241 * This is the top level routine of the printer. 'p' points
242 * to the ether header of the packet, 'h->ts' is the timestamp,
243 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
244 * is the number of bytes actually captured.
245 */
246 u_int
247 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
248 const u_char *p)
249 {
250 return (ether_print(ndo, p, h->len, h->caplen, NULL, NULL));
251 }
252
253 /*
254 * This is the top level routine of the printer. 'p' points
255 * to the ether header of the packet, 'h->ts' is the timestamp,
256 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
257 * is the number of bytes actually captured.
258 *
259 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
260 * before the Ethernet header.
261 */
262 u_int
263 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
264 const u_char *p)
265 {
266 /*
267 * Fail if we don't have enough data for the Hilscher pseudo-header.
268 */
269 if (h->len < 4 || h->caplen < 4) {
270 ND_PRINT((ndo, "[|netanalyzer]"));
271 return (h->caplen);
272 }
273
274 /* Skip the pseudo-header. */
275 return (4 + ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL));
276 }
277
278 /*
279 * This is the top level routine of the printer. 'p' points
280 * to the ether header of the packet, 'h->ts' is the timestamp,
281 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
282 * is the number of bytes actually captured.
283 *
284 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
285 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
286 * before the Ethernet header.
287 */
288 u_int
289 netanalyzer_transparent_if_print(netdissect_options *ndo,
290 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 * preamble, and SOF.
296 */
297 if (h->len < 12 || h->caplen < 12) {
298 ND_PRINT((ndo, "[|netanalyzer-transparent]"));
299 return (h->caplen);
300 }
301
302 /* Skip the pseudo-header, preamble, and SOF. */
303 return (12 + ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL));
304 }
305
306 /*
307 * Prints the packet payload, given an Ethernet type code for the payload's
308 * protocol.
309 *
310 * Returns non-zero if it can do so, zero if the ethertype is unknown.
311 */
312
313 int
314 ethertype_print(netdissect_options *ndo,
315 u_short ether_type, const u_char *p,
316 u_int length, u_int caplen)
317 {
318 switch (ether_type) {
319
320 case ETHERTYPE_IP:
321 ip_print(ndo, p, length);
322 return (1);
323
324 case ETHERTYPE_IPV6:
325 ip6_print(ndo, p, length);
326 return (1);
327
328 case ETHERTYPE_ARP:
329 case ETHERTYPE_REVARP:
330 arp_print(ndo, p, length, caplen);
331 return (1);
332
333 case ETHERTYPE_DN:
334 decnet_print(ndo, p, length, caplen);
335 return (1);
336
337 case ETHERTYPE_ATALK:
338 if (ndo->ndo_vflag)
339 ND_PRINT((ndo, "et1 "));
340 atalk_print(ndo, p, length);
341 return (1);
342
343 case ETHERTYPE_AARP:
344 aarp_print(ndo, p, length);
345 return (1);
346
347 case ETHERTYPE_IPX:
348 ND_PRINT((ndo, "(NOV-ETHII) "));
349 ipx_print(ndo, p, length);
350 return (1);
351
352 case ETHERTYPE_ISO:
353 isoclns_print(ndo, p + 1, length - 1, length - 1);
354 return(1);
355
356 case ETHERTYPE_PPPOED:
357 case ETHERTYPE_PPPOES:
358 case ETHERTYPE_PPPOED2:
359 case ETHERTYPE_PPPOES2:
360 pppoe_print(ndo, p, length);
361 return (1);
362
363 case ETHERTYPE_EAPOL:
364 eap_print(ndo, p, length);
365 return (1);
366
367 case ETHERTYPE_RRCP:
368 rrcp_print(ndo, p - 14 , length + 14);
369 return (1);
370
371 case ETHERTYPE_PPP:
372 if (length) {
373 ND_PRINT((ndo, ": "));
374 ppp_print(ndo, p, length);
375 }
376 return (1);
377
378 case ETHERTYPE_MPCP:
379 mpcp_print(ndo, p, length);
380 return (1);
381
382 case ETHERTYPE_SLOW:
383 slow_print(ndo, p, length);
384 return (1);
385
386 case ETHERTYPE_CFM:
387 case ETHERTYPE_CFM_OLD:
388 cfm_print(ndo, p, length);
389 return (1);
390
391 case ETHERTYPE_LLDP:
392 lldp_print(ndo, p, length);
393 return (1);
394
395 case ETHERTYPE_LOOPBACK:
396 loopback_print(ndo, p, length);
397 return (1);
398
399 case ETHERTYPE_MPLS:
400 case ETHERTYPE_MPLS_MULTI:
401 mpls_print(ndo, p, length);
402 return (1);
403
404 case ETHERTYPE_TIPC:
405 tipc_print(ndo, p, length, caplen);
406 return (1);
407
408 case ETHERTYPE_MS_NLB_HB:
409 msnlb_print(ndo, p);
410 return (1);
411
412 case ETHERTYPE_GEONET_OLD:
413 case ETHERTYPE_GEONET:
414 geonet_print(ndo, p-14, p, length);
415 return (1);
416
417 case ETHERTYPE_CALM_FAST:
418 calm_fast_print(ndo, p-14, p, length);
419 return (1);
420
421 case ETHERTYPE_AOE:
422 aoe_print(ndo, p, length);
423 return (1);
424
425 case ETHERTYPE_MEDSA:
426 medsa_print(ndo, p, length, caplen);
427 return (1);
428
429 case ETHERTYPE_LAT:
430 case ETHERTYPE_SCA:
431 case ETHERTYPE_MOPRC:
432 case ETHERTYPE_MOPDL:
433 case ETHERTYPE_IEEE1905_1:
434 /* default_print for now */
435 default:
436 return (0);
437 }
438 }
439
440
441 /*
442 * Local Variables:
443 * c-style: whitesmith
444 * c-basic-offset: 8
445 * End:
446 */
447