]> The Tcpdump Group git mirrors - tcpdump/blob - print-ether.c
33c58e28b51922005b4f05dc916fb52525836f00
[tcpdump] / print-ether.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
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 #ifndef lint
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)";
24 #endif
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/socket.h>
33
34 struct mbuf;
35 struct rtentry;
36 #include <net/if.h>
37
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>
45
46 #include <stdio.h>
47 #include <pcap.h>
48
49 #ifdef INET6
50 #include <netinet/ip6.h>
51 #endif
52
53 #include "interface.h"
54 #include "addrtoname.h"
55 #include "ethertype.h"
56
57 #include "ether.h"
58
59 const u_char *packetp;
60 const u_char *snapend;
61
62 static inline void
63 ether_print(register const u_char *bp, u_int length)
64 {
65 register const struct ether_header *ep;
66
67 ep = (const struct ether_header *)bp;
68 if (qflag)
69 (void)printf("%s %s %d: ",
70 etheraddr_string(ESRC(ep)),
71 etheraddr_string(EDST(ep)),
72 length);
73 else
74 (void)printf("%s %s %s %d: ",
75 etheraddr_string(ESRC(ep)),
76 etheraddr_string(EDST(ep)),
77 etherproto_string(ep->ether_type),
78 length);
79 }
80
81 static u_short extracted_ethertype;
82
83 /*
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.
88 */
89 void
90 ether_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
91 {
92 u_int caplen = h->caplen;
93 u_int length = h->len;
94 struct ether_header *ep;
95 u_short ether_type;
96
97 ts_print(&h->ts);
98
99 if (caplen < sizeof(struct ether_header)) {
100 printf("[|ether]");
101 goto out;
102 }
103
104 if (eflag)
105 ether_print(p, length);
106
107 /*
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.
111 */
112 packetp = p;
113 snapend = p + caplen;
114
115 length -= sizeof(struct ether_header);
116 caplen -= sizeof(struct ether_header);
117 ep = (struct ether_header *)p;
118 p += sizeof(struct ether_header);
119
120 ether_type = ntohs(ep->ether_type);
121
122 /*
123 * Is it (gag) an 802.3 encapsulation?
124 */
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 */
130 if (!eflag)
131 ether_print((u_char *)ep, length);
132 if (extracted_ethertype) {
133 printf("(LLC %s) ",
134 etherproto_string(htons(extracted_ethertype)));
135 }
136 if (!xflag && !qflag)
137 default_print(p, caplen);
138 }
139 } else if (ether_encap_print(ether_type, p, length, caplen) == 0) {
140 /* ether_type not known, print raw packet */
141 if (!eflag)
142 ether_print((u_char *)ep, length + sizeof(*ep));
143 if (!xflag && !qflag)
144 default_print(p, caplen);
145 }
146 if (xflag)
147 default_print(p, caplen);
148 out:
149 putchar('\n');
150 }
151
152 /*
153 * Prints the packet encapsulated in an Ethernet data segment
154 * (or an equivalent encapsulation), given the Ethernet type code.
155 *
156 * Returns non-zero if it can do so, zero if the ethertype is unknown.
157 *
158 * Stuffs the ether type into a global for the benefit of lower layers
159 * that might want to know what it is.
160 */
161
162 int
163 ether_encap_print(u_short ethertype, const u_char *p,
164 u_int length, u_int caplen)
165 {
166 recurse:
167 extracted_ethertype = ethertype;
168
169 switch (ethertype) {
170
171 case ETHERTYPE_IP:
172 ip_print(p, length);
173 return (1);
174
175 #ifdef INET6
176 case ETHERTYPE_IPV6:
177 ip6_print(p, length);
178 return (1);
179 #endif /*INET6*/
180
181 case ETHERTYPE_ARP:
182 case ETHERTYPE_REVARP:
183 arp_print(p, length, caplen);
184 return (1);
185
186 case ETHERTYPE_DN:
187 decnet_print(p, length, caplen);
188 return (1);
189
190 case ETHERTYPE_ATALK:
191 if (vflag)
192 fputs("et1 ", stdout);
193 atalk_print(p, length);
194 return (1);
195
196 case ETHERTYPE_AARP:
197 aarp_print(p, length);
198 return (1);
199
200 case ETHERTYPE_IPX:
201 ipx_print(p, length);
202 return (1);
203
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));
210 p += 4;
211 length -= 4;
212 caplen -= 4;
213 if (ethertype > ETHERMTU)
214 goto recurse;
215
216 extracted_ethertype = 0;
217
218 if (llc_print(p, length, caplen, p - 18, p - 12) == 0) {
219 /* ether_type not known, print raw packet */
220 if (!eflag)
221 ether_print(p - 18, length + 4);
222 if (extracted_ethertype) {
223 printf("(LLC %s) ",
224 etherproto_string(htons(extracted_ethertype)));
225 }
226 if (!xflag && !qflag)
227 default_print(p - 18, caplen + 4);
228 }
229 return (1);
230
231 case ETHERTYPE_PPPOED:
232 case ETHERTYPE_PPPOES:
233 pppoe_print(p, length);
234 return (1);
235
236 case ETHERTYPE_LAT:
237 case ETHERTYPE_SCA:
238 case ETHERTYPE_MOPRC:
239 case ETHERTYPE_MOPDL:
240 /* default_print for now */
241 default:
242 return (0);
243 }
244 }