]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-bpf.c
8c11b51db118114f66caf6601efb13f67af0773e
2 * Copyright (c) 1993, 1994, 1995, 1996, 1998
3 * The Regents of the University of California. All rights reserved.
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
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.
22 static const char rcsid
[] =
23 "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.34 2000-07-10 04:50:05 assar Exp $ (LBL)";
26 #include <sys/param.h> /* optionally get BSD define */
28 #include <sys/timeb.h>
29 #include <sys/socket.h>
31 #include <sys/ioctl.h>
46 #ifdef HAVE_OS_PROTO_H
53 pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
57 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
58 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
59 pcap_strerror(errno
));
63 ps
->ps_recv
= s
.bs_recv
;
64 ps
->ps_drop
= s
.bs_drop
;
69 pcap_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
73 register u_char
*bp
, *ep
;
78 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
80 /* Don't choke when we get ptraced */
88 #if defined(sun) && !defined(BSD)
90 * Due to a SunOS bug, after 2^31 bytes, the kernel
91 * file offset overflows and read fails with EINVAL.
92 * The lseek() to 0 will fix things.
95 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
97 (void)lseek(p
->fd
, 0L, SEEK_SET
);
103 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
104 pcap_strerror(errno
));
112 * Loop through each packet.
114 #define bhp ((struct bpf_hdr *)bp)
117 register int caplen
, hdrlen
;
118 caplen
= bhp
->bh_caplen
;
119 hdrlen
= bhp
->bh_hdrlen
;
121 * XXX A bpf_hdr matches a pcap_pkthdr.
123 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, bp
+ hdrlen
);
124 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
125 if (++n
>= cnt
&& cnt
> 0) {
137 bpf_open(pcap_t
*p
, char *errbuf
)
141 char device
[sizeof "/dev/bpf0000000000"];
144 * Go through all the minors and find one that isn't in use.
147 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
148 fd
= open(device
, O_RDONLY
);
149 } while (fd
< 0 && errno
== EBUSY
);
152 * XXX better message for all minors used
155 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "(no devices found) %s: %s",
156 device
, pcap_strerror(errno
));
162 pcap_open_live(char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
166 struct bpf_version bv
;
170 p
= (pcap_t
*)malloc(sizeof(*p
));
172 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
173 pcap_strerror(errno
));
176 bzero(p
, sizeof(*p
));
177 fd
= bpf_open(p
, ebuf
);
182 p
->snapshot
= snaplen
;
184 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
185 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
186 pcap_strerror(errno
));
189 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
190 bv
.bv_minor
< BPF_MINOR_VERSION
) {
191 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
192 "kernel bpf filter out of date");
195 v
= 32768; /* XXX this should be a user-accessible hook */
196 /* try finding a good size for the buffer */
198 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
200 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
201 if ((ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) &&
202 (errno
!= ENOBUFS
)) {
203 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
204 errno
, device
, pcap_strerror(errno
));
208 } while (fd
< 0 && errno
== ENOBUFS
);
210 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
212 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
213 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
214 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s: %s",
215 device
, pcap_strerror(errno
));
218 /* Get the data link layer type. */
219 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
220 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
221 pcap_strerror(errno
));
231 #if _BSDI_VERSION - 0 >= 199510
232 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
247 case 12: /*DLT_C_HDLC*/
257 to
.tv_sec
= to_ms
/ 1000;
258 to
.tv_usec
= (to_ms
* 1000) % 1000000;
259 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
260 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT: %s",
261 pcap_strerror(errno
));
268 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
269 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCIMMEDIATE: %s",
270 pcap_strerror(errno
));
273 #endif /* BIOCIMMEDIATE */
276 /* set promiscuous mode, okay if it fails */
277 (void)ioctl(p
->fd
, BIOCPROMISC
, NULL
);
279 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
280 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
281 pcap_strerror(errno
));
285 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
286 if (p
->buffer
== NULL
) {
287 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
288 pcap_strerror(errno
));
300 pcap_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
303 * It looks that BPF code generated by gen_protochain() is not
304 * compatible with some of kernel BPF code (for example BSD/OS 3.1).
305 * Take a safer side for now.
309 else if (p
->sf
.rfile
!= NULL
)
311 else if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) < 0) {
312 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
313 pcap_strerror(errno
));