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