]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-bpf.c
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.48.2.3 2003-02-26 06:42:53 fenner Exp $ (LBL)";
30 #include <sys/param.h> /* optionally get BSD define */
32 #include <sys/timeb.h>
33 #include <sys/socket.h>
35 #include <sys/ioctl.h>
40 * XXX - I'm guessing here AIX defines IFT_ values in <net/if_types.h>,
41 * as BSD does. If not, this code won't compile, but, if not, you
42 * want to send us a bug report and fall back on using DLPI.
43 * It's not as if BPF used to work right on AIX before this change;
44 * this change attempts to fix the fact that it didn't....
46 #include <net/if_types.h> /* for IFT_ values */
59 #ifdef HAVE_OS_PROTO_H
66 pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
71 * "ps_recv" counts packets handed to the filter, not packets
72 * that passed the filter. This includes packets later dropped
73 * because we ran out of buffer space.
75 * "ps_drop" counts packets dropped inside the BPF device
76 * because we ran out of buffer space. It doesn't count
77 * packets dropped by the interface driver. It counts
78 * only packets that passed the filter.
80 * Both statistics include packets not yet read from the kernel
81 * by libpcap, and thus not yet seen by the application.
83 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
84 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
85 pcap_strerror(errno
));
89 ps
->ps_recv
= s
.bs_recv
;
90 ps
->ps_drop
= s
.bs_drop
;
95 pcap_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
99 register u_char
*bp
, *ep
;
104 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
106 /* Don't choke when we get ptraced */
114 #if defined(sun) && !defined(BSD)
116 * Due to a SunOS bug, after 2^31 bytes, the kernel
117 * file offset overflows and read fails with EINVAL.
118 * The lseek() to 0 will fix things.
121 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
123 (void)lseek(p
->fd
, 0L, SEEK_SET
);
129 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
130 pcap_strerror(errno
));
138 * Loop through each packet.
140 #define bhp ((struct bpf_hdr *)bp)
143 register int caplen
, hdrlen
;
144 caplen
= bhp
->bh_caplen
;
145 hdrlen
= bhp
->bh_hdrlen
;
147 * XXX A bpf_hdr matches a pcap_pkthdr.
151 * AIX's BPF returns seconds/nanoseconds time stamps, not
152 * seconds/microseconds time stamps.
154 * XXX - I'm guessing here that it's a "struct timestamp";
155 * if not, this code won't compile, but, if not, you
156 * want to send us a bug report and fall back on using
157 * DLPI. It's not as if BPF used to work right on
158 * AIX before this change; this change attempts to fix
159 * the fact that it didn't....
161 bhp
->bh_tstamp
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
163 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, bp
+ hdrlen
);
164 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
165 if (++n
>= cnt
&& cnt
> 0) {
177 bpf_open(pcap_t
*p
, char *errbuf
)
181 char device
[sizeof "/dev/bpf0000000000"];
184 * Go through all the minors and find one that isn't in use.
187 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
188 fd
= open(device
, O_RDONLY
);
189 } while (fd
< 0 && errno
== EBUSY
);
192 * XXX better message for all minors used
195 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "(no devices found) %s: %s",
196 device
, pcap_strerror(errno
));
202 pcap_open_live(char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
206 struct bpf_version bv
;
210 p
= (pcap_t
*)malloc(sizeof(*p
));
212 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
213 pcap_strerror(errno
));
216 memset(p
, 0, sizeof(*p
));
217 fd
= bpf_open(p
, ebuf
);
222 p
->snapshot
= snaplen
;
224 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
225 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
226 pcap_strerror(errno
));
229 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
230 bv
.bv_minor
< BPF_MINOR_VERSION
) {
231 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
232 "kernel bpf filter out of date");
237 * Try finding a good size for the buffer; 32768 may be too
238 * big, so keep cutting it in half until we find a size
239 * that works, or run out of sizes to try. If the default
240 * is larger, don't make it smaller.
242 * XXX - there should be a user-accessible hook to set the
243 * initial buffer size.
245 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) || v
< 32768)
247 for ( ; v
!= 0; v
>>= 1) {
248 /* Ignore the return value - this is because the call fails
249 * on BPF systems that don't have kernel malloc. And if
250 * the call fails, it's no big deal, we just continue to
251 * use the standard buffer size.
253 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
255 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
256 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
257 break; /* that size worked; we're done */
259 if (errno
!= ENOBUFS
) {
260 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
261 device
, pcap_strerror(errno
));
267 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
268 "BIOCSBLEN: %s: No buffer size worked", device
);
272 /* Get the data link layer type. */
273 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
274 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
275 pcap_strerror(errno
));
280 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
299 * We don't know what to map this to yet.
301 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
306 #if _BSDI_VERSION - 0 >= 199510
307 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
322 case 12: /*DLT_C_HDLC*/
332 to
.tv_sec
= to_ms
/ 1000;
333 to
.tv_usec
= (to_ms
* 1000) % 1000000;
334 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
335 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT: %s",
336 pcap_strerror(errno
));
344 * Darren Reed notes that
346 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
347 * timeout appears to be ignored and it waits until the buffer
348 * is filled before returning. The result of not having it
349 * set is almost worse than useless if your BPF filter
350 * is reducing things to only a few packets (i.e. one every
353 * so we turn BIOCIMMEDIATE mode on if this is AIX.
355 * We don't turn it on for other platforms, as that means we
356 * get woken up for every packet, which may not be what we want;
357 * in the Winter 1993 USENIX paper on BPF, they say:
359 * Since a process might want to look at every packet on a
360 * network and the time between packets can be only a few
361 * microseconds, it is not possible to do a read system call
362 * per packet and BPF must collect the data from several
363 * packets and return it as a unit when the monitoring
364 * application does a read.
366 * which I infer is the reason for the timeout - it means we
367 * wait that amount of time, in the hopes that more packets
368 * will arrive and we'll get them all with one read.
370 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
371 * BSDs) causes the timeout to be ignored.
373 * On the other hand, some platforms (e.g., Linux) don't support
374 * timeouts, they just hand stuff to you as soon as it arrives;
375 * if that doesn't cause a problem on those platforms, it may
376 * be OK to have BIOCIMMEDIATE mode on BSD as well.
378 * (Note, though, that applications may depend on the read
379 * completing, even if no packets have arrived, when the timeout
380 * expires, e.g. GUI applications that have to check for input
381 * while waiting for packets to arrive; a non-zero timeout
382 * prevents "select()" from working right on FreeBSD and
383 * possibly other BSDs, as the timer doesn't start until a
384 * "read()" is done, so the timer isn't in effect if the
385 * application is blocked on a "select()", and the "select()"
386 * doesn't get woken up for a BPF device until the buffer
390 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
391 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCIMMEDIATE: %s",
392 pcap_strerror(errno
));
395 #endif /* BIOCIMMEDIATE */
399 /* set promiscuous mode, okay if it fails */
400 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
401 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
402 pcap_strerror(errno
));
406 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
407 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
408 pcap_strerror(errno
));
412 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
413 if (p
->buffer
== NULL
) {
414 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
415 pcap_strerror(errno
));
427 pcap_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
430 * It looks that BPF code generated by gen_protochain() is not
431 * compatible with some of kernel BPF code (for example BSD/OS 3.1).
432 * Take a safer side for now.
435 if (install_bpf_program(p
, fp
) < 0)
437 } else if (p
->sf
.rfile
!= NULL
) {
438 if (install_bpf_program(p
, fp
) < 0)
440 } else if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) < 0) {
441 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
442 pcap_strerror(errno
));