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