]> The Tcpdump Group git mirrors - tcpdump/blob - print-ether.c
refactored ip_print() so that chained header parser (ESP/AH) can
[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.95 2005-04-06 21:32:39 mcr Exp $ (LBL)";
24 #endif
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <tcpdump-stdinc.h>
31
32 #include <stdio.h>
33 #include <pcap.h>
34
35 #include "interface.h"
36 #include "addrtoname.h"
37 #include "ethertype.h"
38
39 #include "ether.h"
40 #include "llc.h"
41
42 const struct tok ethertype_values[] = {
43 /* not really ethertypes but PIDs that are used
44 in the SNAP printer - its more convenient
45 to put them into a single tokentable */
46 { PID_RFC2684_ETH_FCS, "Ethernet + FCS" },
47 { PID_RFC2684_ETH_NOFCS, "Ethernet no FCS" },
48 { PID_RFC2684_802_4_FCS, "802.4 + FCS" },
49 { PID_RFC2684_802_4_NOFCS, "w/o FCS" },
50 { PID_RFC2684_802_5_FCS, "Tokenring + FCS" },
51 { PID_RFC2684_802_5_NOFCS, "Tokenring no FCS" },
52 { PID_RFC2684_FDDI_FCS, "FDDI + FCS" },
53 { PID_RFC2684_FDDI_NOFCS, "FDDI no FCS" },
54 { PID_RFC2684_802_6_FCS, "802.6 + FCS" },
55 { PID_RFC2684_802_6_NOFCS, "802.6 no FCS" },
56 { PID_RFC2684_BPDU, "BPDU" },
57 /* the real Ethertypes */
58 { ETHERTYPE_IP, "IPv4" },
59 { ETHERTYPE_MPLS, "MPLS unicast" },
60 { ETHERTYPE_MPLS_MULTI, "MPLS multicast" },
61 { ETHERTYPE_IPV6, "IPv6" },
62 { ETHERTYPE_8021Q, "802.1Q" },
63 { ETHERTYPE_VMAN, "VMAN" },
64 { ETHERTYPE_PUP, "PUP" },
65 { ETHERTYPE_ARP, "ARP"},
66 { ETHERTYPE_REVARP , "Reverse ARP"},
67 { ETHERTYPE_NS, "NS" },
68 { ETHERTYPE_SPRITE, "Sprite" },
69 { ETHERTYPE_TRAIL, "Trail" },
70 { ETHERTYPE_MOPDL, "MOP DL" },
71 { ETHERTYPE_MOPRC, "MOP RC" },
72 { ETHERTYPE_DN, "DN" },
73 { ETHERTYPE_LAT, "LAT" },
74 { ETHERTYPE_SCA, "SCA" },
75 { ETHERTYPE_LANBRIDGE, "Lanbridge" },
76 { ETHERTYPE_DECDNS, "DEC DNS" },
77 { ETHERTYPE_DECDTS, "DEC DTS" },
78 { ETHERTYPE_VEXP, "VEXP" },
79 { ETHERTYPE_VPROD, "VPROD" },
80 { ETHERTYPE_ATALK, "Appletalk" },
81 { ETHERTYPE_AARP, "Appletalk ARP" },
82 { ETHERTYPE_IPX, "IPX" },
83 { ETHERTYPE_PPP, "PPP" },
84 { ETHERTYPE_PPPOED, "PPPoE D" },
85 { ETHERTYPE_PPPOES, "PPPoE S" },
86 { ETHERTYPE_EAPOL, "EAPOL" },
87 { ETHERTYPE_JUMBO, "Jumbo" },
88 { ETHERTYPE_LOOPBACK, "Loopback" },
89 { ETHERTYPE_ISO, "OSI" },
90 { ETHERTYPE_GRE_ISO, "GRE-OSI" },
91 { 0, NULL}
92 };
93
94 static inline void
95 ether_hdr_print(register const u_char *bp, u_int length)
96 {
97 register const struct ether_header *ep;
98 ep = (const struct ether_header *)bp;
99
100 (void)printf("%s > %s",
101 etheraddr_string(ESRC(ep)),
102 etheraddr_string(EDST(ep)));
103
104 if (!qflag) {
105 if (ntohs(ep->ether_type) <= ETHERMTU)
106 (void)printf(", 802.3");
107 else
108 (void)printf(", ethertype %s (0x%04x)",
109 tok2str(ethertype_values,"Unknown", ntohs(ep->ether_type)),
110 ntohs(ep->ether_type));
111 } else {
112 if (ntohs(ep->ether_type) <= ETHERMTU)
113 (void)printf(", 802.3");
114 else
115 (void)printf(", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", ntohs(ep->ether_type)));
116 }
117
118 (void)printf(", length %u: ", length);
119 }
120
121 void
122 ether_print(const u_char *p, u_int length, u_int caplen)
123 {
124 struct ether_header *ep;
125 u_short ether_type;
126 u_short extracted_ether_type;
127
128 if (caplen < ETHER_HDRLEN) {
129 printf("[|ether]");
130 return;
131 }
132
133 if (eflag)
134 ether_hdr_print(p, length);
135
136 length -= ETHER_HDRLEN;
137 caplen -= ETHER_HDRLEN;
138 ep = (struct ether_header *)p;
139 p += ETHER_HDRLEN;
140
141 ether_type = ntohs(ep->ether_type);
142
143 /*
144 * Is it (gag) an 802.3 encapsulation?
145 */
146 extracted_ether_type = 0;
147 if (ether_type <= ETHERMTU) {
148 /* Try to print the LLC-layer header & higher layers */
149 if (llc_print(p, length, caplen, ESRC(ep), EDST(ep),
150 &extracted_ether_type) == 0) {
151 /* ether_type not known, print raw packet */
152 if (!eflag)
153 ether_hdr_print((u_char *)ep, length + ETHER_HDRLEN);
154
155 if (!xflag && !qflag)
156 default_print(p, caplen);
157 }
158 } else if (ether_encap_print(ether_type, p, length, caplen,
159 &extracted_ether_type) == 0) {
160 /* ether_type not known, print raw packet */
161 if (!eflag)
162 ether_hdr_print((u_char *)ep, length + ETHER_HDRLEN);
163
164 if (!xflag && !qflag)
165 default_print(p, caplen);
166 }
167 }
168
169 /*
170 * This is the top level routine of the printer. 'p' points
171 * to the ether header of the packet, 'h->ts' is the timestamp,
172 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
173 * is the number of bytes actually captured.
174 */
175 u_int
176 ether_if_print(const struct pcap_pkthdr *h, const u_char *p)
177 {
178 ether_print(p, h->len, h->caplen);
179
180 return (ETHER_HDRLEN);
181 }
182
183 /*
184 * Prints the packet encapsulated in an Ethernet data segment
185 * (or an equivalent encapsulation), given the Ethernet type code.
186 *
187 * Returns non-zero if it can do so, zero if the ethertype is unknown.
188 *
189 * The Ethernet type code is passed through a pointer; if it was
190 * ETHERTYPE_8021Q, it gets updated to be the Ethernet type of
191 * the 802.1Q payload, for the benefit of lower layers that might
192 * want to know what it is.
193 */
194
195 int
196 ether_encap_print(u_short ether_type, const u_char *p,
197 u_int length, u_int caplen, u_short *extracted_ether_type)
198 {
199 recurse:
200 *extracted_ether_type = ether_type;
201
202 switch (ether_type) {
203
204 case ETHERTYPE_IP:
205 ip_print(gndo, p, length);
206 return (1);
207
208 #ifdef INET6
209 case ETHERTYPE_IPV6:
210 ip6_print(p, length);
211 return (1);
212 #endif /*INET6*/
213
214 case ETHERTYPE_ARP:
215 case ETHERTYPE_REVARP:
216 arp_print(gndo, p, length, caplen);
217 return (1);
218
219 case ETHERTYPE_DN:
220 decnet_print(p, length, caplen);
221 return (1);
222
223 case ETHERTYPE_ATALK:
224 if (vflag)
225 fputs("et1 ", stdout);
226 atalk_print(p, length);
227 return (1);
228
229 case ETHERTYPE_AARP:
230 aarp_print(p, length);
231 return (1);
232
233 case ETHERTYPE_IPX:
234 printf("(NOV-ETHII) ");
235 ipx_print(p, length);
236 return (1);
237
238 case ETHERTYPE_8021Q:
239 if (eflag)
240 printf("vlan %u, p %u%s, ",
241 ntohs(*(u_int16_t *)p) & 0xfff,
242 ntohs(*(u_int16_t *)p) >> 13,
243 (ntohs(*(u_int16_t *)p) & 0x1000) ? ", CFI" : "");
244
245 ether_type = ntohs(*(u_int16_t *)(p + 2));
246 p += 4;
247 length -= 4;
248 caplen -= 4;
249
250 if (ether_type > ETHERMTU) {
251 if (eflag)
252 printf("ethertype %s, ",
253 tok2str(ethertype_values,"0x%04x", ether_type));
254 goto recurse;
255 }
256
257 *extracted_ether_type = 0;
258
259 if (llc_print(p, length, caplen, p - 18, p - 12,
260 extracted_ether_type) == 0) {
261 ether_hdr_print(p - 18, length + 4);
262 }
263
264 if (!xflag && !qflag)
265 default_print(p - 18, caplen + 4);
266
267 return (1);
268
269 case ETHERTYPE_JUMBO:
270 ether_type = ntohs(*(u_int16_t *)(p));
271 p += 2;
272 length -= 2;
273 caplen -= 2;
274
275 if (ether_type > ETHERMTU) {
276 if (eflag)
277 printf("ethertype %s, ",
278 tok2str(ethertype_values,"0x%04x", ether_type));
279 goto recurse;
280 }
281
282 *extracted_ether_type = 0;
283
284 if (llc_print(p, length, caplen, p - 16, p - 10,
285 extracted_ether_type) == 0) {
286 ether_hdr_print(p - 16, length + 2);
287 }
288
289 if (!xflag && !qflag)
290 default_print(p - 16, caplen + 2);
291
292 return (1);
293
294 case ETHERTYPE_ISO:
295 isoclns_print(p+1, length-1, length-1);
296 return(1);
297
298 case ETHERTYPE_PPPOED:
299 case ETHERTYPE_PPPOES:
300 pppoe_print(p, length);
301 return (1);
302
303 case ETHERTYPE_EAPOL:
304 eap_print(gndo, p, length);
305 return (1);
306
307 case ETHERTYPE_PPP:
308 if (length) {
309 printf(": ");
310 ppp_print(p, length);
311 }
312 return (1);
313
314 case ETHERTYPE_LOOPBACK:
315 return (0);
316
317 case ETHERTYPE_MPLS:
318 case ETHERTYPE_MPLS_MULTI:
319 mpls_print(p, length);
320 return (1);
321
322 case ETHERTYPE_LAT:
323 case ETHERTYPE_SCA:
324 case ETHERTYPE_MOPRC:
325 case ETHERTYPE_MOPDL:
326 /* default_print for now */
327 default:
328 return (0);
329 }
330 }
331
332
333 /*
334 * Local Variables:
335 * c-style: whitesmith
336 * c-basic-offset: 8
337 * End:
338 */
339