2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
22 static const char rcsid
[] =
23 "@(#) $Header: /tcpdump/master/tcpdump/print-ether.c,v 1.52 2000-09-23 08:03:35 guy Exp $ (LBL)";
30 #include <sys/param.h>
32 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <netinet/in_systm.h>
40 #include <netinet/ip.h>
41 #include <netinet/ip_var.h>
42 #include <netinet/udp.h>
43 #include <netinet/udp_var.h>
44 #include <netinet/tcp.h>
50 #include <netinet/ip6.h>
53 #include "interface.h"
54 #include "addrtoname.h"
55 #include "ethertype.h"
59 const u_char
*packetp
;
60 const u_char
*snapend
;
63 ether_print(register const u_char
*bp
, u_int length
)
65 register const struct ether_header
*ep
;
67 ep
= (const struct ether_header
*)bp
;
69 (void)printf("%s %s %d: ",
70 etheraddr_string(ESRC(ep
)),
71 etheraddr_string(EDST(ep
)),
74 (void)printf("%s %s %s %d: ",
75 etheraddr_string(ESRC(ep
)),
76 etheraddr_string(EDST(ep
)),
77 etherproto_string(ep
->ether_type
),
81 static u_short extracted_ethertype
;
84 * This is the top level routine of the printer. 'p' is the points
85 * to the ether header of the packet, 'h->tv' is the timestamp,
86 * 'h->length' is the length of the packet off the wire, and 'h->caplen'
87 * is the number of bytes actually captured.
90 ether_if_print(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*p
)
92 u_int caplen
= h
->caplen
;
93 u_int length
= h
->len
;
94 struct ether_header
*ep
;
99 if (caplen
< sizeof(struct ether_header
)) {
105 ether_print(p
, length
);
108 * Some printers want to get back at the ethernet addresses,
109 * and/or check that they're not walking off the end of the packet.
110 * Rather than pass them all the way down, we set these globals.
113 snapend
= p
+ caplen
;
115 length
-= sizeof(struct ether_header
);
116 caplen
-= sizeof(struct ether_header
);
117 ep
= (struct ether_header
*)p
;
118 p
+= sizeof(struct ether_header
);
120 ether_type
= ntohs(ep
->ether_type
);
123 * Is it (gag) an 802.3 encapsulation?
125 extracted_ethertype
= 0;
126 if (ether_type
<= ETHERMTU
) {
127 /* Try to print the LLC-layer header & higher layers */
128 if (llc_print(p
, length
, caplen
, ESRC(ep
), EDST(ep
)) == 0) {
129 /* ether_type not known, print raw packet */
131 ether_print((u_char
*)ep
, length
);
132 if (extracted_ethertype
) {
134 etherproto_string(htons(extracted_ethertype
)));
136 if (!xflag
&& !qflag
)
137 default_print(p
, caplen
);
139 } else if (ether_encap_print(ether_type
, p
, length
, caplen
) == 0) {
140 /* ether_type not known, print raw packet */
142 ether_print((u_char
*)ep
, length
+ sizeof(*ep
));
143 if (!xflag
&& !qflag
)
144 default_print(p
, caplen
);
147 default_print(p
, caplen
);
153 * Prints the packet encapsulated in an Ethernet data segment
154 * (or an equivalent encapsulation), given the Ethernet type code.
156 * Returns non-zero if it can do so, zero if the ethertype is unknown.
158 * Stuffs the ether type into a global for the benefit of lower layers
159 * that might want to know what it is.
163 ether_encap_print(u_short ethertype
, const u_char
*p
,
164 u_int length
, u_int caplen
)
167 extracted_ethertype
= ethertype
;
177 ip6_print(p
, length
);
182 case ETHERTYPE_REVARP
:
183 arp_print(p
, length
, caplen
);
187 decnet_print(p
, length
, caplen
);
190 case ETHERTYPE_ATALK
:
192 fputs("et1 ", stdout
);
193 atalk_print(p
, length
);
197 aarp_print(p
, length
);
201 ipx_print(p
, length
);
204 case ETHERTYPE_8021Q
:
205 printf("802.1Q vlan#%d P%d%s",
206 ntohs(*(u_int16_t
*)p
) & 0xfff,
207 ntohs(*(u_int16_t
*)p
) >> 13,
208 (ntohs(*(u_int16_t
*)p
) & 0x1000) ? " CFI" : "");
209 ethertype
= ntohs(*(u_int16_t
*)(p
+ 2));
213 if (ethertype
> ETHERMTU
)
216 extracted_ethertype
= 0;
218 if (llc_print(p
, length
, caplen
, p
- 18, p
- 12) == 0) {
219 /* ether_type not known, print raw packet */
221 ether_print(p
- 18, length
+ 4);
222 if (extracted_ethertype
) {
224 etherproto_string(htons(extracted_ethertype
)));
226 if (!xflag
&& !qflag
)
227 default_print(p
- 18, caplen
+ 4);
231 case ETHERTYPE_PPPOED
:
232 case ETHERTYPE_PPPOES
:
233 pppoe_print(p
, length
);
238 case ETHERTYPE_MOPRC
:
239 case ETHERTYPE_MOPDL
:
240 /* default_print for now */