]> The Tcpdump Group git mirrors - libpcap/blob - pcap-snoop.c
Add a "close" function pointer to the pcap_t structure, which handles
[libpcap] / pcap-snoop.c
1 /*
2 * Copyright (c) 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/libpcap/pcap-snoop.c,v 1.40 2003-07-25 03:25:47 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/file.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <sys/time.h>
35
36 #include <net/raw.h>
37 #include <net/if.h>
38
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
42 #include <netinet/if_ether.h>
43 #include <netinet/ip_var.h>
44 #include <netinet/udp.h>
45 #include <netinet/udp_var.h>
46 #include <netinet/tcp.h>
47 #include <netinet/tcpip.h>
48
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #include "pcap-int.h"
56
57 #ifdef HAVE_OS_PROTO_H
58 #include "os-proto.h"
59 #endif
60
61 int
62 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
63 {
64 int cc;
65 register struct snoopheader *sh;
66 register int datalen;
67 register int caplen;
68 register u_char *cp;
69
70 again:
71 cc = read(p->fd, (char *)p->buffer, p->bufsize);
72 if (cc < 0) {
73 /* Don't choke when we get ptraced */
74 switch (errno) {
75
76 case EINTR:
77 goto again;
78
79 case EWOULDBLOCK:
80 return (0); /* XXX */
81 }
82 snprintf(p->errbuf, sizeof(p->errbuf),
83 "read: %s", pcap_strerror(errno));
84 return (-1);
85 }
86 sh = (struct snoopheader *)p->buffer;
87 datalen = sh->snoop_packetlen;
88 caplen = (datalen < p->snapshot) ? datalen : p->snapshot;
89 cp = (u_char *)(sh + 1) + p->offset; /* XXX */
90
91 if (p->fcode.bf_insns == NULL ||
92 bpf_filter(p->fcode.bf_insns, cp, datalen, caplen)) {
93 struct pcap_pkthdr h;
94 ++p->md.stat.ps_recv;
95 h.ts.tv_sec = sh->snoop_timestamp.tv_sec;
96 h.ts.tv_usec = sh->snoop_timestamp.tv_usec;
97 h.len = datalen;
98 h.caplen = caplen;
99 (*callback)(user, &h, cp);
100 return (1);
101 }
102 return (0);
103 }
104
105 int
106 pcap_stats(pcap_t *p, struct pcap_stat *ps)
107 {
108 register struct rawstats *rs;
109 struct rawstats rawstats;
110
111 rs = &rawstats;
112 memset(rs, 0, sizeof(*rs));
113 if (ioctl(p->fd, SIOCRAWSTATS, (char *)rs) < 0) {
114 snprintf(p->errbuf, sizeof(p->errbuf),
115 "SIOCRAWSTATS: %s", pcap_strerror(errno));
116 return (-1);
117 }
118
119 /*
120 * "ifdrops" are those dropped by the network interface
121 * due to resource shortages or hardware errors.
122 *
123 * "sbdrops" are those dropped due to socket buffer limits.
124 *
125 * As filter is done in userland, "sbdrops" counts packets
126 * regardless of whether they would've passed the filter.
127 *
128 * XXX - does this count *all* Snoop or Drain sockets,
129 * rather than just this socket? If not, why does it have
130 * both Snoop and Drain statistics?
131 */
132 p->md.stat.ps_drop =
133 rs->rs_snoop.ss_ifdrops + rs->rs_snoop.ss_sbdrops +
134 rs->rs_drain.ds_ifdrops + rs->rs_drain.ds_sbdrops;
135
136 /*
137 * "ps_recv" counts only packets that passed the filter.
138 * As filtering is done in userland, this does not include
139 * packets dropped because we ran out of buffer space.
140 */
141 *ps = p->md.stat;
142 return (0);
143 }
144
145 static void
146 pcap_close_snoop(pcap_t *p)
147 {
148 if (p->buffer != NULL)
149 free(p->buffer);
150 if (p->fd >= 0)
151 close(p->fd);
152 }
153
154 /* XXX can't disable promiscuous */
155 pcap_t *
156 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
157 char *ebuf)
158 {
159 int fd;
160 struct sockaddr_raw sr;
161 struct snoopfilter sf;
162 u_int v;
163 int ll_hdrlen;
164 int snooplen;
165 pcap_t *p;
166 struct ifreq ifr;
167
168 p = (pcap_t *)malloc(sizeof(*p));
169 if (p == NULL) {
170 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
171 pcap_strerror(errno));
172 return (NULL);
173 }
174 memset(p, 0, sizeof(*p));
175 fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_SNOOP);
176 if (fd < 0) {
177 snprintf(ebuf, PCAP_ERRBUF_SIZE, "snoop socket: %s",
178 pcap_strerror(errno));
179 goto bad;
180 }
181 p->fd = fd;
182 memset(&sr, 0, sizeof(sr));
183 sr.sr_family = AF_RAW;
184 (void)strncpy(sr.sr_ifname, device, sizeof(sr.sr_ifname));
185 if (bind(fd, (struct sockaddr *)&sr, sizeof(sr))) {
186 snprintf(ebuf, PCAP_ERRBUF_SIZE, "snoop bind: %s",
187 pcap_strerror(errno));
188 goto bad;
189 }
190 memset(&sf, 0, sizeof(sf));
191 if (ioctl(fd, SIOCADDSNOOP, &sf) < 0) {
192 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCADDSNOOP: %s",
193 pcap_strerror(errno));
194 goto bad;
195 }
196 v = 64 * 1024;
197 (void)setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&v, sizeof(v));
198 /*
199 * XXX hack - map device name to link layer type
200 */
201 if (strncmp("et", device, 2) == 0 || /* Challenge 10 Mbit */
202 strncmp("ec", device, 2) == 0 || /* Indigo/Indy 10 Mbit,
203 O2 10/100 */
204 strncmp("ef", device, 2) == 0 || /* O200/2000 10/100 Mbit */
205 strncmp("eg", device, 2) == 0 || /* Octane/O2xxx/O3xxx Gigabit */
206 strncmp("gfe", device, 3) == 0 || /* GIO 100 Mbit */
207 strncmp("fxp", device, 3) == 0 || /* Challenge VME Enet */
208 strncmp("ep", device, 2) == 0 || /* Challenge 8x10 Mbit EPLEX */
209 strncmp("vfe", device, 3) == 0 || /* Challenge VME 100Mbit */
210 strncmp("fa", device, 2) == 0 ||
211 strncmp("qaa", device, 3) == 0 ||
212 strncmp("cip", device, 3) == 0 ||
213 strncmp("el", device, 2) == 0) {
214 p->linktype = DLT_EN10MB;
215 p->offset = RAW_HDRPAD(sizeof(struct ether_header));
216 ll_hdrlen = sizeof(struct ether_header);
217 } else if (strncmp("ipg", device, 3) == 0 ||
218 strncmp("rns", device, 3) == 0 || /* O2/200/2000 FDDI */
219 strncmp("xpi", device, 3) == 0) {
220 p->linktype = DLT_FDDI;
221 p->offset = 3; /* XXX yeah? */
222 ll_hdrlen = 13;
223 } else if (strncmp("ppp", device, 3) == 0) {
224 p->linktype = DLT_RAW;
225 ll_hdrlen = 0; /* DLT_RAW meaning "no PPP header, just the IP packet"? */
226 } else if (strncmp("lo", device, 2) == 0) {
227 p->linktype = DLT_NULL;
228 ll_hdrlen = 4; /* is this just like BSD's loopback device? */
229 } else {
230 snprintf(ebuf, PCAP_ERRBUF_SIZE,
231 "snoop: unknown physical layer type");
232 goto bad;
233 }
234 #ifdef SIOCGIFMTU
235 /*
236 * XXX - IRIX appears to give you an error if you try to set the
237 * capture length to be greater than the MTU, so let's try to get
238 * the MTU first and, if that succeeds, trim the snap length
239 * to be no greater than the MTU.
240 */
241 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
242 if (ioctl(fd, SIOCGIFMTU, (char *)&ifr) < 0) {
243 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCGIFMTU: %s",
244 pcap_strerror(errno));
245 goto bad;
246 }
247 /*
248 * OK, we got it.
249 *
250 * XXX - some versions of IRIX 6.5 define "ifr_mtu" and have an
251 * "ifru_metric" member of the "ifr_ifru" union in an "ifreq"
252 * structure, others don't.
253 *
254 * I've no idea what's going on, so, if "ifr_mtu" isn't defined,
255 * we define it as "ifr_metric", as using that field appears to
256 * work on the versions that lack "ifr_mtu" (and, on those that
257 * don't lack it, "ifru_metric" and "ifru_mtu" are both "int"
258 * members of the "ifr_ifru" union, which suggests that they
259 * may be interchangeable in this case).
260 */
261 #ifndef ifr_mtu
262 #define ifr_mtu ifr_metric
263 #endif
264 if (snaplen > ifr.ifr_mtu + ll_hdrlen)
265 snaplen = ifr.ifr_mtu + ll_hdrlen;
266 #endif
267
268 /*
269 * The argument to SIOCSNOOPLEN is the number of link-layer
270 * payload bytes to capture - it doesn't count link-layer
271 * header bytes.
272 */
273 snooplen = snaplen - ll_hdrlen;
274 if (snooplen < 0)
275 snooplen = 0;
276 if (ioctl(fd, SIOCSNOOPLEN, &snooplen) < 0) {
277 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCSNOOPLEN: %s",
278 pcap_strerror(errno));
279 goto bad;
280 }
281 p->snapshot = snaplen;
282 v = 1;
283 if (ioctl(fd, SIOCSNOOPING, &v) < 0) {
284 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCSNOOPING: %s",
285 pcap_strerror(errno));
286 goto bad;
287 }
288
289 p->bufsize = 4096; /* XXX */
290 p->buffer = (u_char *)malloc(p->bufsize);
291 if (p->buffer == NULL) {
292 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
293 pcap_strerror(errno));
294 goto bad;
295 }
296
297 p->close_op = pcap_close_snoop;
298
299 return (p);
300 bad:
301 (void)close(fd);
302 free(p);
303 return (NULL);
304 }
305
306 int
307 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
308 {
309 return (0);
310 }
311
312 int
313 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
314 {
315
316 if (install_bpf_program(p, fp) < 0)
317 return (-1);
318 return (0);
319 }
320
321 int
322 pcap_set_datalink_platform(pcap_t *p, int dlt)
323 {
324 return (0);
325 }