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