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.49 2000-04-28 11:38:38 itojun Exp $ (LBL)";
30 #include <sys/param.h>
32 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <netinet/if_ether.h>
42 #include <netinet/in_systm.h>
43 #include <netinet/ip.h>
44 #include <netinet/ip_var.h>
45 #include <netinet/udp.h>
46 #include <netinet/udp_var.h>
47 #include <netinet/tcp.h>
53 #include <netinet/ip6.h>
56 #include "interface.h"
57 #include "addrtoname.h"
58 #include "ethertype.h"
60 const u_char
*packetp
;
61 const u_char
*snapend
;
64 ether_print(register const u_char
*bp
, u_int length
)
66 register const struct ether_header
*ep
;
68 ep
= (const struct ether_header
*)bp
;
70 (void)printf("%s %s %d: ",
71 etheraddr_string(ESRC(ep
)),
72 etheraddr_string(EDST(ep
)),
75 (void)printf("%s %s %s %d: ",
76 etheraddr_string(ESRC(ep
)),
77 etheraddr_string(EDST(ep
)),
78 etherproto_string(ep
->ether_type
),
82 static u_short extracted_ethertype
;
85 * This is the top level routine of the printer. 'p' is the points
86 * to the ether header of the packet, 'h->tv' is the timestamp,
87 * 'h->length' is the length of the packet off the wire, and 'h->caplen'
88 * is the number of bytes actually captured.
91 ether_if_print(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*p
)
93 u_int caplen
= h
->caplen
;
94 u_int length
= h
->len
;
95 struct ether_header
*ep
;
100 if (caplen
< sizeof(struct ether_header
)) {
106 ether_print(p
, length
);
109 * Some printers want to get back at the ethernet addresses,
110 * and/or check that they're not walking off the end of the packet.
111 * Rather than pass them all the way down, we set these globals.
114 snapend
= p
+ caplen
;
116 length
-= sizeof(struct ether_header
);
117 caplen
-= sizeof(struct ether_header
);
118 ep
= (struct ether_header
*)p
;
119 p
+= sizeof(struct ether_header
);
121 ether_type
= ntohs(ep
->ether_type
);
124 * Is it (gag) an 802.3 encapsulation?
126 extracted_ethertype
= 0;
127 if (ether_type
<= ETHERMTU
) {
128 /* Try to print the LLC-layer header & higher layers */
129 if (llc_print(p
, length
, caplen
, ESRC(ep
), EDST(ep
)) == 0) {
130 /* ether_type not known, print raw packet */
132 ether_print((u_char
*)ep
, length
);
133 if (extracted_ethertype
) {
135 etherproto_string(htons(extracted_ethertype
)));
137 if (!xflag
&& !qflag
)
138 default_print(p
, caplen
);
140 } else if (ether_encap_print(ether_type
, p
, length
, caplen
) == 0) {
141 /* ether_type not known, print raw packet */
143 ether_print((u_char
*)ep
, length
+ sizeof(*ep
));
144 if (!xflag
&& !qflag
)
145 default_print(p
, caplen
);
148 default_print(p
, caplen
);
154 * Prints the packet encapsulated in an Ethernet data segment
155 * (or an equivalent encapsulation), given the Ethernet type code.
157 * Returns non-zero if it can do so, zero if the ethertype is unknown.
159 * Stuffs the ether type into a global for the benefit of lower layers
160 * that might want to know what it is.
164 ether_encap_print(u_short ethertype
, const u_char
*p
,
165 u_int length
, u_int caplen
)
168 extracted_ethertype
= ethertype
;
178 ip6_print(p
, length
);
183 case ETHERTYPE_REVARP
:
184 arp_print(p
, length
, caplen
);
188 decnet_print(p
, length
, caplen
);
191 case ETHERTYPE_ATALK
:
193 fputs("et1 ", stdout
);
194 atalk_print(p
, length
);
198 aarp_print(p
, length
);
201 case ETHERTYPE_8021Q
:
202 printf("802.1Q vlan#%d P%d%s",
203 ntohs(*(u_int16_t
*)p
) & 0xfff,
204 ntohs(*(u_int16_t
*)p
) >> 13,
205 (ntohs(*(u_int16_t
*)p
) & 0x1000) ? " CFI" : "");
206 ethertype
= ntohs(*(u_int16_t
*)(p
+ 2));
210 if (ethertype
> ETHERMTU
)
213 extracted_ethertype
= 0;
215 if (llc_print(p
, length
, caplen
, p
- 18, p
- 12) == 0) {
216 /* ether_type not known, print raw packet */
218 ether_print(p
- 18, length
+ 4);
219 if (extracted_ethertype
) {
221 etherproto_string(htons(extracted_ethertype
)));
223 if (!xflag
&& !qflag
)
224 default_print(p
- 18, caplen
+ 4);
228 case ETHERTYPE_PPPOED
:
229 case ETHERTYPE_PPPOES
:
230 pppoe_print(p
, length
);
235 case ETHERTYPE_MOPRC
:
236 case ETHERTYPE_MOPDL
:
237 /* default_print for now */