]> The Tcpdump Group git mirrors - tcpdump/blob - print-ether.c
reworked print-ether to use netdissect
[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 #ifndef lint
22 static const char rcsid[] _U_ =
23 "@(#) $Header: /tcpdump/master/tcpdump/print-ether.c,v 1.106 2008-02-06 10:47:53 guy Exp $ (LBL)";
24 #endif
25
26 #define NETDISSECT_REWORKED
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <tcpdump-stdinc.h>
32
33 #include <stdio.h>
34 #include <pcap.h>
35
36 #include "interface.h"
37 #include "extract.h"
38 #include "addrtoname.h"
39 #include "ethertype.h"
40
41 #include "ether.h"
42
43 const struct tok ethertype_values[] = {
44 { ETHERTYPE_IP, "IPv4" },
45 { ETHERTYPE_MPLS, "MPLS unicast" },
46 { ETHERTYPE_MPLS_MULTI, "MPLS multicast" },
47 { ETHERTYPE_IPV6, "IPv6" },
48 { ETHERTYPE_8021Q, "802.1Q" },
49 { ETHERTYPE_VMAN, "VMAN" },
50 { ETHERTYPE_PUP, "PUP" },
51 { ETHERTYPE_ARP, "ARP"},
52 { ETHERTYPE_REVARP, "Reverse ARP"},
53 { ETHERTYPE_NS, "NS" },
54 { ETHERTYPE_SPRITE, "Sprite" },
55 { ETHERTYPE_TRAIL, "Trail" },
56 { ETHERTYPE_MOPDL, "MOP DL" },
57 { ETHERTYPE_MOPRC, "MOP RC" },
58 { ETHERTYPE_DN, "DN" },
59 { ETHERTYPE_LAT, "LAT" },
60 { ETHERTYPE_SCA, "SCA" },
61 { ETHERTYPE_TEB, "TEB" },
62 { ETHERTYPE_LANBRIDGE, "Lanbridge" },
63 { ETHERTYPE_DECDNS, "DEC DNS" },
64 { ETHERTYPE_DECDTS, "DEC DTS" },
65 { ETHERTYPE_VEXP, "VEXP" },
66 { ETHERTYPE_VPROD, "VPROD" },
67 { ETHERTYPE_ATALK, "Appletalk" },
68 { ETHERTYPE_AARP, "Appletalk ARP" },
69 { ETHERTYPE_IPX, "IPX" },
70 { ETHERTYPE_PPP, "PPP" },
71 { ETHERTYPE_MPCP, "MPCP" },
72 { ETHERTYPE_SLOW, "Slow Protocols" },
73 { ETHERTYPE_PPPOED, "PPPoE D" },
74 { ETHERTYPE_PPPOES, "PPPoE S" },
75 { ETHERTYPE_EAPOL, "EAPOL" },
76 { ETHERTYPE_RRCP, "RRCP" },
77 { ETHERTYPE_JUMBO, "Jumbo" },
78 { ETHERTYPE_LOOPBACK, "Loopback" },
79 { ETHERTYPE_ISO, "OSI" },
80 { ETHERTYPE_GRE_ISO, "GRE-OSI" },
81 { ETHERTYPE_CFM_OLD, "CFM (old)" },
82 { ETHERTYPE_CFM, "CFM" },
83 { ETHERTYPE_LLDP, "LLDP" },
84 { 0, NULL}
85 };
86
87 static inline void
88 ether_hdr_print(netdissect_options *ndo,
89 const u_char *bp, u_int length)
90 {
91 register const struct ether_header *ep;
92 u_int16_t ether_type;
93
94 ep = (const struct ether_header *)bp;
95
96 (void)printf("%s > %s",
97 etheraddr_string(ESRC(ep)),
98 etheraddr_string(EDST(ep)));
99
100 ether_type = EXTRACT_16BITS(&ep->ether_type);
101 if (!ndo->ndo_qflag) {
102 if (ether_type <= ETHERMTU)
103 (void)printf(", 802.3");
104 else
105 (void)printf(", ethertype %s (0x%04x)",
106 tok2str(ethertype_values,"Unknown", ether_type),
107 ether_type);
108 } else {
109 if (ether_type <= ETHERMTU)
110 (void)printf(", 802.3");
111 else
112 (void)printf(", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", ether_type));
113 }
114
115 (void)printf(", length %u: ", length);
116 }
117
118 /*
119 * Print an Ethernet frame.
120 * This might be encapsulated within another frame; we might be passed
121 * a pointer to a function that can print header information for that
122 * frame's protocol, and an argument to pass to that function.
123 */
124 void
125 ether_print(netdissect_options *ndo,
126 const u_char *p, u_int length, u_int caplen,
127 void (*print_encap_header)(netdissect_options *ndo, const u_char *), const u_char *encap_header_arg)
128 {
129 struct ether_header *ep;
130 u_int orig_length;
131 u_short ether_type;
132 u_short extracted_ether_type;
133
134 if (caplen < ETHER_HDRLEN || length < ETHER_HDRLEN) {
135 printf("[|ether]");
136 return;
137 }
138
139 if (ndo->ndo_eflag) {
140 if (print_encap_header != NULL)
141 (*print_encap_header)(ndo, encap_header_arg);
142 ether_hdr_print(ndo, p, length);
143 }
144 orig_length = length;
145
146 length -= ETHER_HDRLEN;
147 caplen -= ETHER_HDRLEN;
148 ep = (struct ether_header *)p;
149 p += ETHER_HDRLEN;
150
151 ether_type = EXTRACT_16BITS(&ep->ether_type);
152
153 recurse:
154 /*
155 * Is it (gag) an 802.3 encapsulation?
156 */
157 if (ether_type <= ETHERMTU) {
158 /* Try to print the LLC-layer header & higher layers */
159 if (llc_print(p, length, caplen, ESRC(ep), EDST(ep),
160 &extracted_ether_type) == 0) {
161 /* ether_type not known, print raw packet */
162 if (!ndo->ndo_eflag) {
163 if (print_encap_header != NULL)
164 (*print_encap_header)(ndo, encap_header_arg);
165 ether_hdr_print(ndo, (u_char *)ep, orig_length);
166 }
167
168 if (!ndo->ndo_suppress_default_print)
169 ndo->ndo_default_print(ndo, p, caplen);
170 }
171 } else if (ether_type == ETHERTYPE_8021Q) {
172 /*
173 * Print VLAN information, and then go back and process
174 * the enclosed type field.
175 */
176 if (caplen < 4 || length < 4) {
177 printf("[|vlan]");
178 return;
179 }
180 if (ndo->ndo_eflag) {
181 u_int16_t tag = EXTRACT_16BITS(p);
182
183 printf("vlan %u, p %u%s, ",
184 tag & 0xfff,
185 tag >> 13,
186 (tag & 0x1000) ? ", CFI" : "");
187 }
188
189 ether_type = EXTRACT_16BITS(p + 2);
190 if (ndo->ndo_eflag && ether_type > ETHERMTU)
191 printf("ethertype %s, ", tok2str(ethertype_values,"0x%04x", ether_type));
192 p += 4;
193 length -= 4;
194 caplen -= 4;
195 goto recurse;
196 } else if (ether_type == ETHERTYPE_JUMBO) {
197 /*
198 * Alteon jumbo frames.
199 * See
200 *
201 * http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
202 *
203 * which indicates that, following the type field,
204 * there's an LLC header and payload.
205 */
206 /* Try to print the LLC-layer header & higher layers */
207 if (llc_print(p, length, caplen, ESRC(ep), EDST(ep),
208 &extracted_ether_type) == 0) {
209 /* ether_type not known, print raw packet */
210 if (!ndo->ndo_eflag) {
211 if (print_encap_header != NULL)
212 (*print_encap_header)(ndo, encap_header_arg);
213 ether_hdr_print(ndo, (u_char *)ep, orig_length);
214 }
215
216 if (!ndo->ndo_suppress_default_print)
217 ndo->ndo_default_print(ndo, p, caplen);
218 }
219 } else {
220 if (ethertype_print(ndo, ether_type, p, length, caplen) == 0) {
221 /* ether_type not known, print raw packet */
222 if (!ndo->ndo_eflag) {
223 if (print_encap_header != NULL)
224 (*print_encap_header)(ndo, encap_header_arg);
225 ether_hdr_print(ndo, (u_char *)ep, orig_length);
226 }
227
228 if (!ndo->ndo_suppress_default_print)
229 ndo->ndo_default_print(ndo, p, caplen);
230 }
231 }
232 }
233
234 /*
235 * This is the top level routine of the printer. 'p' points
236 * to the ether header of the packet, 'h->ts' is the timestamp,
237 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
238 * is the number of bytes actually captured.
239 */
240 u_int
241 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
242 const u_char *p)
243 {
244 ether_print(ndo, p, h->len, h->caplen, NULL, NULL);
245
246 return (ETHER_HDRLEN);
247 }
248
249 /*
250 * Prints the packet payload, given an Ethernet type code for the payload's
251 * protocol.
252 *
253 * Returns non-zero if it can do so, zero if the ethertype is unknown.
254 */
255
256 int
257 ethertype_print(netdissect_options *ndo,
258 u_short ether_type, const u_char *p,
259 u_int length, u_int caplen)
260 {
261 switch (ether_type) {
262
263 case ETHERTYPE_IP:
264 ip_print(ndo, p, length);
265 return (1);
266
267 #ifdef INET6
268 case ETHERTYPE_IPV6:
269 ip6_print(/*ndo,*/ p, length);
270 return (1);
271 #endif /*INET6*/
272
273 case ETHERTYPE_ARP:
274 case ETHERTYPE_REVARP:
275 arp_print(ndo, p, length, caplen);
276 return (1);
277
278 case ETHERTYPE_DN:
279 decnet_print(/*ndo,*/p, length, caplen);
280 return (1);
281
282 case ETHERTYPE_ATALK:
283 if (ndo->ndo_vflag)
284 fputs("et1 ", stdout);
285 atalk_print(/*ndo,*/p, length);
286 return (1);
287
288 case ETHERTYPE_AARP:
289 aarp_print(/*ndo,*/p, length);
290 return (1);
291
292 case ETHERTYPE_IPX:
293 printf("(NOV-ETHII) ");
294 ipx_print(/*ndo,*/p, length);
295 return (1);
296
297 case ETHERTYPE_ISO:
298 isoclns_print(/*ndo,*/p+1, length-1, length-1);
299 return(1);
300
301 case ETHERTYPE_PPPOED:
302 case ETHERTYPE_PPPOES:
303 case ETHERTYPE_PPPOED2:
304 case ETHERTYPE_PPPOES2:
305 pppoe_print(/*ndo,*/p, length);
306 return (1);
307
308 case ETHERTYPE_EAPOL:
309 eap_print(ndo, p, length);
310 return (1);
311
312 case ETHERTYPE_RRCP:
313 rrcp_print(ndo, p - 14 , length + 14);
314 return (1);
315
316 case ETHERTYPE_PPP:
317 if (length) {
318 printf(": ");
319 ppp_print(/*ndo,*/p, length);
320 }
321 return (1);
322
323 case ETHERTYPE_MPCP:
324 mpcp_print(/*ndo,*/p, length);
325 return (1);
326
327 case ETHERTYPE_SLOW:
328 slow_print(/*ndo,*/p, length);
329 return (1);
330
331 case ETHERTYPE_CFM:
332 case ETHERTYPE_CFM_OLD:
333 cfm_print(/*ndo,*/p, length);
334 return (1);
335
336 case ETHERTYPE_LLDP:
337 lldp_print(/*ndo,*/p, length);
338 return (1);
339
340 case ETHERTYPE_LOOPBACK:
341 return (1);
342
343 case ETHERTYPE_MPLS:
344 case ETHERTYPE_MPLS_MULTI:
345 mpls_print(/*ndo,*/p, length);
346 return (1);
347
348 case ETHERTYPE_LAT:
349 case ETHERTYPE_SCA:
350 case ETHERTYPE_MOPRC:
351 case ETHERTYPE_MOPDL:
352 /* default_print for now */
353 default:
354 return (0);
355 }
356 }
357
358
359 /*
360 * Local Variables:
361 * c-style: whitesmith
362 * c-basic-offset: 8
363 * End:
364 */
365