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