]> git.saurik.com Git - apple/network_cmds.git/blob - tcpdump.tproj/print-ether.c
network_cmds-77.tar.gz
[apple/network_cmds.git] / tcpdump.tproj / print-ether.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that: (1) source code distributions
30 * retain the above copyright notice and this paragraph in its entirety, (2)
31 * distributions including binary code include the above copyright notice and
32 * this paragraph in its entirety in the documentation or other materials
33 * provided with the distribution, and (3) all advertising materials mentioning
34 * features or use of this software display the following acknowledgement:
35 * ``This product includes software developed by the University of California,
36 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
37 * the University nor the names of its contributors may be used to endorse
38 * or promote products derived from this software without specific prior
39 * written permission.
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
41 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
43 */
44 #ifndef lint
45 static const char rcsid[] =
46 "@(#) $Header: /cvs/Darwin/Commands/NeXT/network_cmds/tcpdump.tproj/print-ether.c,v 1.1.1.1 1999/05/02 03:58:33 wsanchez Exp $ (LBL)";
47 #endif
48
49 #include <sys/param.h>
50 #include <sys/time.h>
51 #include <sys/socket.h>
52
53 #if __STDC__
54 struct mbuf;
55 struct rtentry;
56 #endif
57 #include <net/if.h>
58
59 #include <netinet/in.h>
60 #include <netinet/if_ether.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/udp.h>
65 #include <netinet/udp_var.h>
66 #include <netinet/tcp.h>
67 #include <netinet/tcpip.h>
68
69 #include <stdio.h>
70 #include <pcap.h>
71
72 #include "interface.h"
73 #include "addrtoname.h"
74 #include "ethertype.h"
75
76 const u_char *packetp;
77 const u_char *snapend;
78
79 static inline void
80 ether_print(register const u_char *bp, u_int length)
81 {
82 register const struct ether_header *ep;
83
84 ep = (const struct ether_header *)bp;
85 if (qflag)
86 (void)printf("%s %s %d: ",
87 etheraddr_string(ESRC(ep)),
88 etheraddr_string(EDST(ep)),
89 length);
90 else
91 (void)printf("%s %s %s %d: ",
92 etheraddr_string(ESRC(ep)),
93 etheraddr_string(EDST(ep)),
94 etherproto_string(ep->ether_type),
95 length);
96 }
97
98 /*
99 * This is the top level routine of the printer. 'p' is the points
100 * to the ether header of the packet, 'tvp' is the timestamp,
101 * 'length' is the length of the packet off the wire, and 'caplen'
102 * is the number of bytes actually captured.
103 */
104 void
105 ether_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
106 {
107 u_int caplen = h->caplen;
108 u_int length = h->len;
109 struct ether_header *ep;
110 u_short ether_type;
111 extern u_short extracted_ethertype;
112
113 ts_print(&h->ts);
114
115 if (caplen < sizeof(struct ether_header)) {
116 printf("[|ether]");
117 goto out;
118 }
119
120 if (eflag)
121 ether_print(p, length);
122
123 /*
124 * Some printers want to get back at the ethernet addresses,
125 * and/or check that they're not walking off the end of the packet.
126 * Rather than pass them all the way down, we set these globals.
127 */
128 packetp = p;
129 snapend = p + caplen;
130
131 length -= sizeof(struct ether_header);
132 caplen -= sizeof(struct ether_header);
133 ep = (struct ether_header *)p;
134 p += sizeof(struct ether_header);
135
136 ether_type = ntohs(ep->ether_type);
137
138 /*
139 * Is it (gag) an 802.3 encapsulation?
140 */
141 extracted_ethertype = 0;
142 if (ether_type < ETHERMTU) {
143 /* Try to print the LLC-layer header & higher layers */
144 if (llc_print(p, length, caplen, ESRC(ep), EDST(ep)) == 0) {
145 /* ether_type not known, print raw packet */
146 if (!eflag)
147 ether_print((u_char *)ep, length);
148 if (extracted_ethertype) {
149 printf("(LLC %s) ",
150 etherproto_string(htons(extracted_ethertype)));
151 }
152 if (!xflag && !qflag)
153 default_print(p, caplen);
154 }
155 } else if (ether_encap_print(ether_type, p, length, caplen) == 0) {
156 /* ether_type not known, print raw packet */
157 if (!eflag)
158 ether_print((u_char *)ep, length + sizeof(*ep));
159 if (!xflag && !qflag)
160 default_print(p, caplen);
161 }
162 if (xflag)
163 default_print(p, caplen);
164 out:
165 putchar('\n');
166 }
167
168 /*
169 * Prints the packet encapsulated in an Ethernet data segment
170 * (or an equivalent encapsulation), given the Ethernet type code.
171 *
172 * Returns non-zero if it can do so, zero if the ethertype is unknown.
173 *
174 * Stuffs the ether type into a global for the benefit of lower layers
175 * that might want to know what it is.
176 */
177
178 u_short extracted_ethertype;
179
180 int
181 ether_encap_print(u_short ethertype, const u_char *p,
182 u_int length, u_int caplen)
183 {
184 extracted_ethertype = ethertype;
185
186 switch (ethertype) {
187
188 case ETHERTYPE_IP:
189 ip_print(p, length);
190 return (1);
191
192 case ETHERTYPE_ARP:
193 case ETHERTYPE_REVARP:
194 arp_print(p, length, caplen);
195 return (1);
196
197 case ETHERTYPE_DN:
198 decnet_print(p, length, caplen);
199 return (1);
200
201 case ETHERTYPE_ATALK:
202 if (vflag)
203 fputs("et1 ", stdout);
204 atalk_print(p, length);
205 return (1);
206
207 case ETHERTYPE_AARP:
208 aarp_print(p, length);
209 return (1);
210
211 case ETHERTYPE_LAT:
212 case ETHERTYPE_SCA:
213 case ETHERTYPE_MOPRC:
214 case ETHERTYPE_MOPDL:
215 /* default_print for now */
216 default:
217 return (0);
218 }
219 }