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