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