]>
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
[] _U_
=
23 "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.74 2004-03-23 19:18:04 guy 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>
36 #include <sys/utsname.h>
43 * Make "pcap.h" not include "pcap-bpf.h"; we are going to include the
44 * native OS version, as we need "struct bpf_config" from it.
46 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
48 #include <sys/types.h>
51 * Prevent bpf.h from redefining the DLT_ values to their
52 * IFT_ values, as we're going to return the standard libpcap
53 * values, not IBM's non-standard IFT_ values.
59 #include <net/if_types.h> /* for IFT_ values */
60 #include <sys/sysconfig.h>
61 #include <sys/device.h>
66 #define domakedev makedev64
67 #define getmajor major64
68 #define bpf_hdr bpf_hdr32
70 #define domakedev makedev
71 #define getmajor major
72 #endif /* __64BIT__ */
74 #define BPF_NAME "bpf"
76 #define DRIVER_PATH "/usr/lib/drivers"
77 #define BPF_NODE "/dev/bpf"
78 static int bpfloadedflag
= 0;
79 static int odmlockid
= 0;
99 #endif /* HAVE_DAG_API */
101 #ifdef HAVE_OS_PROTO_H
102 #include "os-proto.h"
105 #include "gencode.h" /* for "no_optimize" */
107 static int pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
);
108 static int pcap_set_datalink_bpf(pcap_t
*p
, int dlt
);
111 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
116 * "ps_recv" counts packets handed to the filter, not packets
117 * that passed the filter. This includes packets later dropped
118 * because we ran out of buffer space.
120 * "ps_drop" counts packets dropped inside the BPF device
121 * because we ran out of buffer space. It doesn't count
122 * packets dropped by the interface driver. It counts
123 * only packets that passed the filter.
125 * Both statistics include packets not yet read from the kernel
126 * by libpcap, and thus not yet seen by the application.
128 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
129 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
130 pcap_strerror(errno
));
134 ps
->ps_recv
= s
.bs_recv
;
135 ps
->ps_drop
= s
.bs_drop
;
140 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
144 register u_char
*bp
, *ep
;
145 struct bpf_insn
*fcode
;
147 fcode
= p
->md
.use_bpf
? NULL
: p
->fcode
.bf_insns
;
150 * Has "pcap_breakloop()" been called?
154 * Yes - clear the flag that indicates that it
155 * has, and return -2 to indicate that we were
156 * told to break out of the loop.
163 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
165 /* Don't choke when we get ptraced */
174 * Sigh. More AIX wonderfulness.
176 * For some unknown reason the uiomove()
177 * operation in the bpf kernel extension
178 * used to copy the buffer into user
179 * space sometimes returns EFAULT. I have
180 * no idea why this is the case given that
181 * a kernel debugger shows the user buffer
182 * is correct. This problem appears to
183 * be mostly mitigated by the memset of
184 * the buffer before it is first used.
185 * Very strange.... Shaun Clowes
187 * In any case this means that we shouldn't
188 * treat EFAULT as a fatal error; as we
189 * don't have an API for returning
190 * a "some packets were dropped since
191 * the last packet you saw" indication,
192 * we just ignore EFAULT and keep reading.
199 #if defined(sun) && !defined(BSD)
201 * Due to a SunOS bug, after 2^31 bytes, the kernel
202 * file offset overflows and read fails with EINVAL.
203 * The lseek() to 0 will fix things.
206 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
208 (void)lseek(p
->fd
, 0L, SEEK_SET
);
214 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
215 pcap_strerror(errno
));
223 * Loop through each packet.
225 #define bhp ((struct bpf_hdr *)bp)
228 register int caplen
, hdrlen
;
231 * Has "pcap_breakloop()" been called?
232 * If so, return immediately - if we haven't read any
233 * packets, clear the flag and return -2 to indicate
234 * that we were told to break out of the loop, otherwise
235 * leave the flag set, so that the *next* call will break
236 * out of the loop without having read any packets, and
237 * return the number of packets we've processed so far.
250 caplen
= bhp
->bh_caplen
;
251 hdrlen
= bhp
->bh_hdrlen
;
253 * Short-circuit evaluation: if using BPF filter
254 * in kernel, no need to do it now.
257 bpf_filter(fcode
, bp
+ hdrlen
, bhp
->bh_datalen
, caplen
)) {
260 * AIX's BPF returns seconds/nanoseconds time
261 * stamps, not seconds/microseconds time stamps.
263 * XXX - I'm guessing here that it's a "struct
264 * timestamp"; if not, this code won't compile,
265 * but, if not, you want to send us a bug report
266 * and fall back on using DLPI. It's not as if
267 * BPF used to work right on AIX before this
268 * change; this change attempts to fix the fact
271 bhp
->bh_tstamp
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
274 * XXX A bpf_hdr matches a pcap_pkthdr.
276 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, bp
+ hdrlen
);
277 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
278 if (++n
>= cnt
&& cnt
> 0) {
287 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
296 pcap_inject_bpf(pcap_t
*p
, const void *buf
, size_t size
)
301 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
302 * the link-layer source address isn't forcibly overwritten?
303 * (Ignore errors? Return errors if not "sorry, that ioctl
306 * XXX - I seem to remember some packet-sending bug in some
307 * BSDs - check CVS log for "bpf.c"?
309 ret
= write(p
->fd
, buf
, size
);
311 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
312 pcap_strerror(errno
));
320 bpf_odminit(char *errbuf
)
324 if (odm_initialize() == -1) {
325 if (odm_err_msg(odmerrno
, &errstr
) == -1)
326 errstr
= "Unknown error";
327 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
328 "bpf_load: odm_initialize failed: %s",
333 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
334 if (odm_err_msg(odmerrno
, &errstr
) == -1)
335 errstr
= "Unknown error";
336 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
337 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
346 bpf_odmcleanup(char *errbuf
)
350 if (odm_unlock(odmlockid
) == -1) {
351 if (odm_err_msg(odmerrno
, &errstr
) == -1)
352 errstr
= "Unknown error";
353 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
354 "bpf_load: odm_unlock failed: %s",
359 if (odm_terminate() == -1) {
360 if (odm_err_msg(odmerrno
, &errstr
) == -1)
361 errstr
= "Unknown error";
362 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
363 "bpf_load: odm_terminate failed: %s",
372 bpf_load(char *errbuf
)
376 int numminors
, i
, rc
;
379 struct bpf_config cfg_bpf
;
380 struct cfg_load cfg_ld
;
381 struct cfg_kmod cfg_km
;
384 * This is very very close to what happens in the real implementation
385 * but I've fixed some (unlikely) bug situations.
390 if (bpf_odminit(errbuf
) != 0)
393 major
= genmajor(BPF_NAME
);
395 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
396 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
400 minors
= getminor(major
, &numminors
, BPF_NAME
);
402 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
404 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
405 "bpf_load: genminor failed: %s",
406 pcap_strerror(errno
));
411 if (bpf_odmcleanup(errbuf
))
414 rc
= stat(BPF_NODE
"0", &sbuf
);
415 if (rc
== -1 && errno
!= ENOENT
) {
416 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
417 "bpf_load: can't stat %s: %s",
418 BPF_NODE
"0", pcap_strerror(errno
));
422 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
423 for (i
= 0; i
< BPF_MINORS
; i
++) {
424 sprintf(buf
, "%s%d", BPF_NODE
, i
);
426 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
427 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
428 "bpf_load: can't mknod %s: %s",
429 buf
, pcap_strerror(errno
));
435 /* Check if the driver is loaded */
436 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
438 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
439 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
440 (cfg_ld
.kmid
== 0)) {
441 /* Driver isn't loaded, load it now */
442 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
443 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
444 "bpf_load: could not load driver: %s",
450 /* Configure the driver */
451 cfg_km
.cmd
= CFG_INIT
;
452 cfg_km
.kmid
= cfg_ld
.kmid
;
453 cfg_km
.mdilen
= sizeof(cfg_bpf
);
454 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
455 for (i
= 0; i
< BPF_MINORS
; i
++) {
456 cfg_bpf
.devno
= domakedev(major
, i
);
457 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
458 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
459 "bpf_load: could not configure driver: %s",
472 bpf_open(pcap_t
*p
, char *errbuf
)
476 char device
[sizeof "/dev/bpf0000000000"];
480 * Load the bpf driver, if it isn't already loaded,
481 * and create the BPF device entries, if they don't
484 if (bpf_load(errbuf
) == -1)
489 * Go through all the minors and find one that isn't in use.
492 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
494 * Initially try a read/write open (to allow the inject
495 * method to work). If that fails due to permission
496 * issues, fall back to read-only. This allows a
497 * non-root user to be granted specific access to pcap
498 * capabilities via file permissions.
500 * XXX - we should have an API that has a flag that
501 * controls whether to open read-only or read-write,
502 * so that denial of permission to send (or inability
503 * to send, if sending packets isn't supported on
504 * the device in question) can be indicated at open
507 fd
= open(device
, O_RDWR
);
508 if (fd
== -1 && errno
== EACCES
)
509 fd
= open(device
, O_RDONLY
);
510 } while (fd
< 0 && errno
== EBUSY
);
513 * XXX better message for all minors used
516 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "(no devices found) %s: %s",
517 device
, pcap_strerror(errno
));
523 pcap_close_bpf(pcap_t
*p
)
525 if (p
->buffer
!= NULL
)
532 * We include the OS's <net/bpf.h>, not our "pcap-bpf.h", so we probably
533 * don't get DLT_DOCSIS defined.
536 #define DLT_DOCSIS 143
540 * XXX - on AIX, IBM's tcpdump (and perhaps the incompatible-with-everybody-
541 * else's libpcap in AIX 5.1) appears to forcibly load the BPF driver
542 * if it's not already loaded, and to create the BPF devices if they
545 * It'd be nice if we could do the same, although the code to do so
546 * might be version-dependent, alas (the way to do it isn't necessarily
550 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
555 struct bpf_version bv
;
557 struct bpf_dltlist bdl
;
561 struct utsname osinfo
;
564 if (strstr(device
, "dag")) {
565 return dag_open_live(device
, snaplen
, promisc
, to_ms
, ebuf
);
567 #endif /* HAVE_DAG_API */
570 memset(&bdl
, 0, sizeof(bdl
));
573 p
= (pcap_t
*)malloc(sizeof(*p
));
575 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
576 pcap_strerror(errno
));
579 memset(p
, 0, sizeof(*p
));
580 fd
= bpf_open(p
, ebuf
);
585 p
->snapshot
= snaplen
;
587 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
588 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
589 pcap_strerror(errno
));
592 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
593 bv
.bv_minor
< BPF_MINOR_VERSION
) {
594 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
595 "kernel bpf filter out of date");
600 * Try finding a good size for the buffer; 32768 may be too
601 * big, so keep cutting it in half until we find a size
602 * that works, or run out of sizes to try. If the default
603 * is larger, don't make it smaller.
605 * XXX - there should be a user-accessible hook to set the
606 * initial buffer size.
608 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) || v
< 32768)
610 for ( ; v
!= 0; v
>>= 1) {
611 /* Ignore the return value - this is because the call fails
612 * on BPF systems that don't have kernel malloc. And if
613 * the call fails, it's no big deal, we just continue to
614 * use the standard buffer size.
616 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
618 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
619 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
620 break; /* that size worked; we're done */
622 if (errno
!= ENOBUFS
) {
623 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
624 device
, pcap_strerror(errno
));
630 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
631 "BIOCSBLEN: %s: No buffer size worked", device
);
635 /* Get the data link layer type. */
636 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
637 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
638 pcap_strerror(errno
));
643 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
666 * We don't know what to map this to yet.
668 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
673 #if _BSDI_VERSION - 0 >= 199510
674 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
689 case 12: /*DLT_C_HDLC*/
698 * We know the default link type -- now determine all the DLTs
699 * this interface supports. If this fails with EINVAL, it's
700 * not fatal; we just don't get to use the feature later.
702 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)&bdl
) == 0) {
706 bdl
.bfl_list
= (u_int
*) malloc(sizeof(u_int
) * bdl
.bfl_len
+ 1);
707 if (bdl
.bfl_list
== NULL
) {
708 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
709 pcap_strerror(errno
));
713 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)&bdl
) < 0) {
714 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
715 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
721 * OK, for real Ethernet devices, add DLT_DOCSIS to the
722 * list, so that an application can let you choose it,
723 * in case you're capturing DOCSIS traffic that a Cisco
724 * Cable Modem Termination System is putting out onto
725 * an Ethernet (it doesn't put an Ethernet header onto
726 * the wire, it puts raw DOCSIS frames out on the wire
727 * inside the low-level Ethernet framing).
729 * A "real Ethernet device" is defined here as a device
730 * that has a link-layer type of DLT_EN10MB and that has
731 * no alternate link-layer types; that's done to exclude
732 * 802.11 interfaces (which might or might not be the
733 * right thing to do, but I suspect it is - Ethernet <->
734 * 802.11 bridges would probably badly mishandle frames
735 * that don't have Ethernet headers).
737 if (p
->linktype
== DLT_EN10MB
) {
739 for (i
= 0; i
< bdl
.bfl_len
; i
++) {
740 if (bdl
.bfl_list
!= DLT_EN10MB
) {
747 * We reserved one more slot at the end of
750 bdl
.bfl_list
[bdl
.bfl_len
] = DLT_DOCSIS
;
754 p
->dlt_count
= bdl
.bfl_len
;
755 p
->dlt_list
= bdl
.bfl_list
;
757 if (errno
!= EINVAL
) {
758 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
759 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
766 * If this is an Ethernet device, and we don't have a DLT_ list,
767 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
768 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
769 * do, but there's not much we can do about that without finding
770 * some other way of determining whether it's an Ethernet or 802.11
773 if (p
->linktype
== DLT_EN10MB
&& p
->dlt_count
== 0) {
774 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
776 * If that fails, just leave the list empty.
778 if (p
->dlt_list
!= NULL
) {
779 p
->dlt_list
[0] = DLT_EN10MB
;
780 p
->dlt_list
[1] = DLT_DOCSIS
;
788 * XXX - is this seconds/nanoseconds in AIX?
789 * (Treating it as such doesn't fix the timeout
790 * problem described below.)
793 to
.tv_sec
= to_ms
/ 1000;
794 to
.tv_usec
= (to_ms
* 1000) % 1000000;
795 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
796 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT: %s",
797 pcap_strerror(errno
));
805 * Darren Reed notes that
807 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
808 * timeout appears to be ignored and it waits until the buffer
809 * is filled before returning. The result of not having it
810 * set is almost worse than useless if your BPF filter
811 * is reducing things to only a few packets (i.e. one every
814 * so we turn BIOCIMMEDIATE mode on if this is AIX.
816 * We don't turn it on for other platforms, as that means we
817 * get woken up for every packet, which may not be what we want;
818 * in the Winter 1993 USENIX paper on BPF, they say:
820 * Since a process might want to look at every packet on a
821 * network and the time between packets can be only a few
822 * microseconds, it is not possible to do a read system call
823 * per packet and BPF must collect the data from several
824 * packets and return it as a unit when the monitoring
825 * application does a read.
827 * which I infer is the reason for the timeout - it means we
828 * wait that amount of time, in the hopes that more packets
829 * will arrive and we'll get them all with one read.
831 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
832 * BSDs) causes the timeout to be ignored.
834 * On the other hand, some platforms (e.g., Linux) don't support
835 * timeouts, they just hand stuff to you as soon as it arrives;
836 * if that doesn't cause a problem on those platforms, it may
837 * be OK to have BIOCIMMEDIATE mode on BSD as well.
839 * (Note, though, that applications may depend on the read
840 * completing, even if no packets have arrived, when the timeout
841 * expires, e.g. GUI applications that have to check for input
842 * while waiting for packets to arrive; a non-zero timeout
843 * prevents "select()" from working right on FreeBSD and
844 * possibly other BSDs, as the timer doesn't start until a
845 * "read()" is done, so the timer isn't in effect if the
846 * application is blocked on a "select()", and the "select()"
847 * doesn't get woken up for a BPF device until the buffer
851 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
852 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCIMMEDIATE: %s",
853 pcap_strerror(errno
));
856 #endif /* BIOCIMMEDIATE */
860 /* set promiscuous mode, okay if it fails */
861 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
862 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
863 pcap_strerror(errno
));
867 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
868 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
869 pcap_strerror(errno
));
873 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
874 if (p
->buffer
== NULL
) {
875 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
876 pcap_strerror(errno
));
880 /* For some strange reason this seems to prevent the EFAULT
881 * problems we have experienced from AIX BPF. */
882 memset(p
->buffer
, 0x0, p
->bufsize
);
886 * On most BPF platforms, either you can do a "select()" or
887 * "poll()" on a BPF file descriptor and it works correctly,
888 * or you can do it and it will return "readable" if the
889 * hold buffer is full but not if the timeout expires *and*
890 * a non-blocking read will, if the hold buffer is empty
891 * but the store buffer isn't empty, rotate the buffers
892 * and return what packets are available.
894 * In the latter case, the fact that a non-blocking read
895 * will give you the available packets means you can work
896 * around the failure of "select()" and "poll()" to wake up
897 * and return "readable" when the timeout expires by using
898 * the timeout as the "select()" or "poll()" timeout, putting
899 * the BPF descriptor into non-blocking mode, and read from
900 * it regardless of whether "select()" reports it as readable
903 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
904 * won't wake up and return "readable" if the timer expires
905 * and non-blocking reads return EWOULDBLOCK if the hold
906 * buffer is empty, even if the store buffer is non-empty.
908 * This means the workaround in question won't work.
910 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
911 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
912 * here". On all other BPF platforms, we set it to the FD for
913 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
914 * read will, if the hold buffer is empty and the store buffer
915 * isn't empty, rotate the buffers and return what packets are
916 * there (and in sufficiently recent versions of OpenBSD
917 * "select()" and "poll()" should work correctly).
919 * XXX - what about AIX?
921 if (uname(&osinfo
) == 0) {
923 * We can check what OS this is.
925 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0 &&
926 (strcmp(osinfo
.release
, "4.3") == 0 ||
927 strcmp(osinfo
.release
, "4.4") == 0))
928 p
->selectable_fd
= -1;
930 p
->selectable_fd
= p
->fd
;
933 * We can't find out what OS this is, so assume we can
934 * do a "select()" or "poll()".
936 p
->selectable_fd
= p
->fd
;
939 p
->read_op
= pcap_read_bpf
;
940 p
->inject_op
= pcap_inject_bpf
;
941 p
->setfilter_op
= pcap_setfilter_bpf
;
942 p
->set_datalink_op
= pcap_set_datalink_bpf
;
943 p
->getnonblock_op
= pcap_getnonblock_fd
;
944 p
->setnonblock_op
= pcap_setnonblock_fd
;
945 p
->stats_op
= pcap_stats_bpf
;
946 p
->close_op
= pcap_close_bpf
;
951 if (p
->dlt_list
!= NULL
)
958 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
961 if (dag_platform_finddevs(alldevsp
, errbuf
) < 0)
963 #endif /* HAVE_DAG_API */
969 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
972 * It looks that BPF code generated by gen_protochain() is not
973 * compatible with some of kernel BPF code (for example BSD/OS 3.1).
974 * Take a safer side for now.
978 * XXX - what if we already have a filter in the kernel?
980 if (install_bpf_program(p
, fp
) < 0)
982 p
->md
.use_bpf
= 0; /* filtering in userland */
987 * Free any user-mode filter we might happen to have installed.
989 pcap_freecode(&p
->fcode
);
992 * Try to install the kernel filter.
994 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) < 0) {
995 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
996 pcap_strerror(errno
));
999 p
->md
.use_bpf
= 1; /* filtering in the kernel */
1004 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
1007 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
1008 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
1009 "Cannot set DLT %d: %s", dlt
, strerror(errno
));