]> The Tcpdump Group git mirrors - tcpdump/blob - print-ether.c
Don't overwrite packet data when checking the signature.
[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 <netdissect-stdinc.h>
27
28 #include "netdissect.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 length_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 length_type = EXTRACT_16BITS(&ep->ether_length_type);
103 if (!ndo->ndo_qflag) {
104 if (length_type <= ETHERMTU) {
105 ND_PRINT((ndo, ", 802.3"));
106 length = length_type;
107 } else
108 ND_PRINT((ndo, ", ethertype %s (0x%04x)",
109 tok2str(ethertype_values,"Unknown", length_type),
110 length_type));
111 } else {
112 if (length_type <= ETHERMTU) {
113 ND_PRINT((ndo, ", 802.3"));
114 length = length_type;
115 } else
116 ND_PRINT((ndo, ", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", length_type)));
117 }
118
119 ND_PRINT((ndo, ", length %u: ", length));
120 }
121
122 /*
123 * Print an Ethernet frame.
124 * This might be encapsulated within another frame; we might be passed
125 * a pointer to a function that can print header information for that
126 * frame's protocol, and an argument to pass to that function.
127 */
128 u_int
129 ether_print(netdissect_options *ndo,
130 const u_char *p, u_int length, u_int caplen,
131 void (*print_encap_header)(netdissect_options *ndo, const u_char *), const u_char *encap_header_arg)
132 {
133 const struct ether_header *ep;
134 u_int orig_length;
135 u_short length_type;
136 u_int hdrlen;
137 int llc_hdrlen;
138
139 if (caplen < ETHER_HDRLEN) {
140 ND_PRINT((ndo, "[|ether]"));
141 return (caplen);
142 }
143 if (length < ETHER_HDRLEN) {
144 ND_PRINT((ndo, "[|ether]"));
145 return (length);
146 }
147
148 if (ndo->ndo_eflag) {
149 if (print_encap_header != NULL)
150 (*print_encap_header)(ndo, encap_header_arg);
151 ether_hdr_print(ndo, p, length);
152 }
153 orig_length = length;
154
155 length -= ETHER_HDRLEN;
156 caplen -= ETHER_HDRLEN;
157 ep = (const struct ether_header *)p;
158 p += ETHER_HDRLEN;
159 hdrlen = ETHER_HDRLEN;
160
161 length_type = EXTRACT_16BITS(&ep->ether_length_type);
162
163 recurse:
164 /*
165 * Is it (gag) an 802.3 encapsulation?
166 */
167 if (length_type <= ETHERMTU) {
168 /* Try to print the LLC-layer header & higher layers */
169 llc_hdrlen = llc_print(ndo, p, length, caplen, ESRC(ep), EDST(ep));
170 if (llc_hdrlen < 0) {
171 /* packet type not known, print raw packet */
172 if (!ndo->ndo_suppress_default_print)
173 ND_DEFAULTPRINT(p, caplen);
174 llc_hdrlen = -llc_hdrlen;
175 }
176 hdrlen += llc_hdrlen;
177 } else if (length_type == ETHERTYPE_8021Q ||
178 length_type == ETHERTYPE_8021Q9100 ||
179 length_type == ETHERTYPE_8021Q9200 ||
180 length_type == ETHERTYPE_8021QinQ) {
181 /*
182 * Print VLAN information, and then go back and process
183 * the enclosed type field.
184 */
185 if (caplen < 4) {
186 ND_PRINT((ndo, "[|vlan]"));
187 return (hdrlen + caplen);
188 }
189 if (length < 4) {
190 ND_PRINT((ndo, "[|vlan]"));
191 return (hdrlen + length);
192 }
193 if (ndo->ndo_eflag) {
194 uint16_t tag = EXTRACT_16BITS(p);
195
196 ND_PRINT((ndo, "%s, ", ieee8021q_tci_string(tag)));
197 }
198
199 length_type = EXTRACT_16BITS(p + 2);
200 if (ndo->ndo_eflag && length_type > ETHERMTU)
201 ND_PRINT((ndo, "ethertype %s, ", tok2str(ethertype_values,"0x%04x", length_type)));
202 p += 4;
203 length -= 4;
204 caplen -= 4;
205 hdrlen += 4;
206 goto recurse;
207 } else if (length_type == ETHERTYPE_JUMBO) {
208 /*
209 * Alteon jumbo frames.
210 * See
211 *
212 * http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
213 *
214 * which indicates that, following the type field,
215 * there's an LLC header and payload.
216 */
217 /* Try to print the LLC-layer header & higher layers */
218 llc_hdrlen = llc_print(ndo, p, length, caplen, ESRC(ep), EDST(ep));
219 if (llc_hdrlen < 0) {
220 /* packet type not known, print raw packet */
221 if (!ndo->ndo_suppress_default_print)
222 ND_DEFAULTPRINT(p, caplen);
223 llc_hdrlen = -llc_hdrlen;
224 }
225 hdrlen += llc_hdrlen;
226 } else {
227 if (ethertype_print(ndo, length_type, p, length, caplen) == 0) {
228 /* type not known, print raw packet */
229 if (!ndo->ndo_eflag) {
230 if (print_encap_header != NULL)
231 (*print_encap_header)(ndo, encap_header_arg);
232 ether_hdr_print(ndo, (const u_char *)ep, orig_length);
233 }
234
235 if (!ndo->ndo_suppress_default_print)
236 ND_DEFAULTPRINT(p, caplen);
237 }
238 }
239 return (hdrlen);
240 }
241
242 /*
243 * This is the top level routine of the printer. 'p' points
244 * to the ether header of the packet, 'h->len' is the length
245 * of the packet off the wire, and 'h->caplen' is the number
246 * of bytes actually captured.
247 */
248 u_int
249 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
250 const u_char *p)
251 {
252 return (ether_print(ndo, p, h->len, h->caplen, NULL, NULL));
253 }
254
255 /*
256 * This is the top level routine of the printer. 'p' points
257 * to the ether header of the packet, 'h->len' is the length
258 * of the packet off the wire, and 'h->caplen' is the number
259 * of bytes actually captured.
260 *
261 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
262 * before the Ethernet header.
263 */
264 u_int
265 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
266 const u_char *p)
267 {
268 /*
269 * Fail if we don't have enough data for the Hilscher pseudo-header.
270 */
271 if (h->len < 4 || h->caplen < 4) {
272 ND_PRINT((ndo, "[|netanalyzer]"));
273 return (h->caplen);
274 }
275
276 /* Skip the pseudo-header. */
277 return (4 + ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL));
278 }
279
280 /*
281 * This is the top level routine of the printer. 'p' points
282 * to the ether header of the packet, 'h->len' is the length
283 * of the packet off the wire, and 'h->caplen' is the number
284 * of bytes actually captured.
285 *
286 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
287 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
288 * before the Ethernet header.
289 */
290 u_int
291 netanalyzer_transparent_if_print(netdissect_options *ndo,
292 const struct pcap_pkthdr *h,
293 const u_char *p)
294 {
295 /*
296 * Fail if we don't have enough data for the Hilscher pseudo-header,
297 * preamble, and SOF.
298 */
299 if (h->len < 12 || h->caplen < 12) {
300 ND_PRINT((ndo, "[|netanalyzer-transparent]"));
301 return (h->caplen);
302 }
303
304 /* Skip the pseudo-header, preamble, and SOF. */
305 return (12 + ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL));
306 }
307
308 /*
309 * Prints the packet payload, given an Ethernet type code for the payload's
310 * protocol.
311 *
312 * Returns non-zero if it can do so, zero if the ethertype is unknown.
313 */
314
315 int
316 ethertype_print(netdissect_options *ndo,
317 u_short ether_type, const u_char *p,
318 u_int length, u_int caplen)
319 {
320 switch (ether_type) {
321
322 case ETHERTYPE_IP:
323 ip_print(ndo, p, length);
324 return (1);
325
326 case ETHERTYPE_IPV6:
327 ip6_print(ndo, p, length);
328 return (1);
329
330 case ETHERTYPE_ARP:
331 case ETHERTYPE_REVARP:
332 arp_print(ndo, p, length, caplen);
333 return (1);
334
335 case ETHERTYPE_DN:
336 decnet_print(ndo, p, length, caplen);
337 return (1);
338
339 case ETHERTYPE_ATALK:
340 if (ndo->ndo_vflag)
341 ND_PRINT((ndo, "et1 "));
342 atalk_print(ndo, p, length);
343 return (1);
344
345 case ETHERTYPE_AARP:
346 aarp_print(ndo, p, length);
347 return (1);
348
349 case ETHERTYPE_IPX:
350 ND_PRINT((ndo, "(NOV-ETHII) "));
351 ipx_print(ndo, p, length);
352 return (1);
353
354 case ETHERTYPE_ISO:
355 isoclns_print(ndo, p + 1, length - 1, length - 1);
356 return(1);
357
358 case ETHERTYPE_PPPOED:
359 case ETHERTYPE_PPPOES:
360 case ETHERTYPE_PPPOED2:
361 case ETHERTYPE_PPPOES2:
362 pppoe_print(ndo, p, length);
363 return (1);
364
365 case ETHERTYPE_EAPOL:
366 eap_print(ndo, p, length);
367 return (1);
368
369 case ETHERTYPE_RRCP:
370 rrcp_print(ndo, p - 14 , length + 14);
371 return (1);
372
373 case ETHERTYPE_PPP:
374 if (length) {
375 ND_PRINT((ndo, ": "));
376 ppp_print(ndo, p, length);
377 }
378 return (1);
379
380 case ETHERTYPE_MPCP:
381 mpcp_print(ndo, p, length);
382 return (1);
383
384 case ETHERTYPE_SLOW:
385 slow_print(ndo, p, length);
386 return (1);
387
388 case ETHERTYPE_CFM:
389 case ETHERTYPE_CFM_OLD:
390 cfm_print(ndo, p, length);
391 return (1);
392
393 case ETHERTYPE_LLDP:
394 lldp_print(ndo, p, length);
395 return (1);
396
397 case ETHERTYPE_LOOPBACK:
398 loopback_print(ndo, p, length);
399 return (1);
400
401 case ETHERTYPE_MPLS:
402 case ETHERTYPE_MPLS_MULTI:
403 mpls_print(ndo, p, length);
404 return (1);
405
406 case ETHERTYPE_TIPC:
407 tipc_print(ndo, p, length, caplen);
408 return (1);
409
410 case ETHERTYPE_MS_NLB_HB:
411 msnlb_print(ndo, p);
412 return (1);
413
414 case ETHERTYPE_GEONET_OLD:
415 case ETHERTYPE_GEONET:
416 geonet_print(ndo, p-14, p, length);
417 return (1);
418
419 case ETHERTYPE_CALM_FAST:
420 calm_fast_print(ndo, p-14, p, length);
421 return (1);
422
423 case ETHERTYPE_AOE:
424 aoe_print(ndo, p, length);
425 return (1);
426
427 case ETHERTYPE_MEDSA:
428 medsa_print(ndo, p, length, caplen);
429 return (1);
430
431 case ETHERTYPE_LAT:
432 case ETHERTYPE_SCA:
433 case ETHERTYPE_MOPRC:
434 case ETHERTYPE_MOPDL:
435 case ETHERTYPE_IEEE1905_1:
436 /* default_print for now */
437 default:
438 return (0);
439 }
440 }
441
442
443 /*
444 * Local Variables:
445 * c-style: whitesmith
446 * c-basic-offset: 8
447 * End:
448 */
449