]> The Tcpdump Group git mirrors - tcpdump/blob - print-ether.c
Get rid of unneeded incomplete definitions of "struct mbuf" and "struct
[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 #ifndef lint
22 static const char rcsid[] =
23 "@(#) $Header: /tcpdump/master/tcpdump/print-ether.c,v 1.69 2002-06-01 23:50:31 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 #include <netinet/in.h>
35
36 #include <stdio.h>
37 #include <pcap.h>
38
39 #include "interface.h"
40 #include "addrtoname.h"
41 #include "ethertype.h"
42
43 #include "ether.h"
44
45 const u_char *packetp;
46 const u_char *snapend;
47
48 static inline void
49 ether_hdr_print(register const u_char *bp, u_int length)
50 {
51 register const struct ether_header *ep;
52
53 ep = (const struct ether_header *)bp;
54 if (qflag)
55 (void)printf("%s %s %d: ",
56 etheraddr_string(ESRC(ep)),
57 etheraddr_string(EDST(ep)),
58 length);
59 else
60 (void)printf("%s %s %s %d: ",
61 etheraddr_string(ESRC(ep)),
62 etheraddr_string(EDST(ep)),
63 etherproto_string(ep->ether_type),
64 length);
65 }
66
67 void
68 ether_print(const u_char *p, u_int length, u_int caplen)
69 {
70 struct ether_header *ep;
71 u_short ether_type;
72 u_short extracted_ethertype;
73
74 if (caplen < ETHER_HDRLEN) {
75 printf("[|ether]");
76 return;
77 }
78
79 if (eflag)
80 ether_hdr_print(p, length);
81
82 /*
83 * Some printers want to get back at the ethernet addresses,
84 * and/or check that they're not walking off the end of the packet.
85 * Rather than pass them all the way down, we set these globals.
86 */
87 packetp = p;
88 snapend = p + caplen;
89
90 length -= ETHER_HDRLEN;
91 caplen -= ETHER_HDRLEN;
92 ep = (struct ether_header *)p;
93 p += ETHER_HDRLEN;
94
95 ether_type = ntohs(ep->ether_type);
96
97 /*
98 * Is it (gag) an 802.3 encapsulation?
99 */
100 extracted_ethertype = 0;
101 if (ether_type <= ETHERMTU) {
102 /* Try to print the LLC-layer header & higher layers */
103 if (llc_print(p, length, caplen, ESRC(ep), EDST(ep),
104 &extracted_ethertype) == 0) {
105 /* ether_type not known, print raw packet */
106 if (!eflag)
107 ether_hdr_print((u_char *)ep, length + ETHER_HDRLEN);
108 if (extracted_ethertype) {
109 printf("(LLC %s) ",
110 etherproto_string(htons(extracted_ethertype)));
111 }
112 if (!xflag && !qflag)
113 default_print(p, caplen);
114 }
115 } else if (ether_encap_print(ether_type, p, length, caplen,
116 &extracted_ethertype) == 0) {
117 /* ether_type not known, print raw packet */
118 if (!eflag)
119 ether_hdr_print((u_char *)ep, length + ETHER_HDRLEN);
120 if (!xflag && !qflag)
121 default_print(p, caplen);
122 }
123 }
124
125 /*
126 * This is the top level routine of the printer. 'p' is the points
127 * to the ether header of the packet, 'h->tv' is the timestamp,
128 * 'h->length' is the length of the packet off the wire, and 'h->caplen'
129 * is the number of bytes actually captured.
130 */
131 void
132 ether_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
133 {
134 u_int caplen = h->caplen;
135 u_int length = h->len;
136
137 ++infodelay;
138 ts_print(&h->ts);
139
140 ether_print(p, length, caplen);
141
142 /*
143 * If "-x" was specified, print stuff past the Ethernet header,
144 * if there's anything to print.
145 */
146 if (xflag && caplen > ETHER_HDRLEN)
147 default_print(p + ETHER_HDRLEN, caplen - ETHER_HDRLEN);
148
149 putchar('\n');
150
151 --infodelay;
152 if (infoprint)
153 info(0);
154 }
155
156 /*
157 * Prints the packet encapsulated in an Ethernet data segment
158 * (or an equivalent encapsulation), given the Ethernet type code.
159 *
160 * Returns non-zero if it can do so, zero if the ethertype is unknown.
161 *
162 * The Ethernet type code is passed through a pointer; if it was
163 * ETHERTYPE_8021Q, it gets updated to be the Ethernet type of
164 * the 802.1Q payload, for the benefit of lower layers that might
165 * want to know what it is.
166 */
167
168 int
169 ether_encap_print(u_short ethertype, const u_char *p,
170 u_int length, u_int caplen, u_short *extracted_ethertype)
171 {
172 recurse:
173 *extracted_ethertype = ethertype;
174
175 switch (ethertype) {
176
177 case ETHERTYPE_IP:
178 ip_print(p, length);
179 return (1);
180
181 #ifdef INET6
182 case ETHERTYPE_IPV6:
183 ip6_print(p, length);
184 return (1);
185 #endif /*INET6*/
186
187 case ETHERTYPE_ARP:
188 case ETHERTYPE_REVARP:
189 arp_print(p, length, caplen);
190 return (1);
191
192 case ETHERTYPE_DN:
193 decnet_print(p, length, caplen);
194 return (1);
195
196 case ETHERTYPE_ATALK:
197 if (vflag)
198 fputs("et1 ", stdout);
199 atalk_print(p, length);
200 return (1);
201
202 case ETHERTYPE_AARP:
203 aarp_print(p, length);
204 return (1);
205
206 case ETHERTYPE_IPX:
207 printf("(NOV-ETHII) ");
208 ipx_print(p, length);
209 return (1);
210
211 case ETHERTYPE_8021Q:
212 printf("802.1Q vlan#%d P%d%s ",
213 ntohs(*(u_int16_t *)p) & 0xfff,
214 ntohs(*(u_int16_t *)p) >> 13,
215 (ntohs(*(u_int16_t *)p) & 0x1000) ? " CFI" : "");
216 ethertype = ntohs(*(u_int16_t *)(p + 2));
217 p += 4;
218 length -= 4;
219 caplen -= 4;
220 if (ethertype > ETHERMTU)
221 goto recurse;
222
223 *extracted_ethertype = 0;
224
225 if (llc_print(p, length, caplen, p - 18, p - 12,
226 extracted_ethertype) == 0) {
227 /* ether_type not known, print raw packet */
228 if (!eflag)
229 ether_hdr_print(p - 18, length + 4);
230 if (*extracted_ethertype) {
231 printf("(LLC %s) ",
232 etherproto_string(htons(*extracted_ethertype)));
233 }
234 if (!xflag && !qflag)
235 default_print(p - 18, caplen + 4);
236 }
237 return (1);
238
239 case ETHERTYPE_PPPOED:
240 case ETHERTYPE_PPPOES:
241 pppoe_print(p, length);
242 return (1);
243
244 case ETHERTYPE_PPP:
245 printf("ppp");
246 if (length) {
247 printf(": ");
248 ppp_print(p, length);
249 }
250 return (1);
251
252 case ETHERTYPE_MPLS:
253 case ETHERTYPE_MPLS_MULTI:
254 mpls_print(p, length);
255 return (1);
256
257 case ETHERTYPE_LAT:
258 case ETHERTYPE_SCA:
259 case ETHERTYPE_MOPRC:
260 case ETHERTYPE_MOPDL:
261 /* default_print for now */
262 default:
263 return (0);
264 }
265 }