]>
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.85 2005-02-24 08:59:38 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
;
146 struct bpf_insn
*fcode
;
151 fcode
= p
->md
.use_bpf
? NULL
: p
->fcode
.bf_insns
;
154 * Has "pcap_breakloop()" been called?
158 * Yes - clear the flag that indicates that it
159 * has, and return -2 to indicate that we were
160 * told to break out of the loop.
167 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
169 /* Don't choke when we get ptraced */
178 * Sigh. More AIX wonderfulness.
180 * For some unknown reason the uiomove()
181 * operation in the bpf kernel extension
182 * used to copy the buffer into user
183 * space sometimes returns EFAULT. I have
184 * no idea why this is the case given that
185 * a kernel debugger shows the user buffer
186 * is correct. This problem appears to
187 * be mostly mitigated by the memset of
188 * the buffer before it is first used.
189 * Very strange.... Shaun Clowes
191 * In any case this means that we shouldn't
192 * treat EFAULT as a fatal error; as we
193 * don't have an API for returning
194 * a "some packets were dropped since
195 * the last packet you saw" indication,
196 * we just ignore EFAULT and keep reading.
203 #if defined(sun) && !defined(BSD)
205 * Due to a SunOS bug, after 2^31 bytes, the kernel
206 * file offset overflows and read fails with EINVAL.
207 * The lseek() to 0 will fix things.
210 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
212 (void)lseek(p
->fd
, 0L, SEEK_SET
);
218 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
219 pcap_strerror(errno
));
227 * Loop through each packet.
229 #define bhp ((struct bpf_hdr *)bp)
235 register int caplen
, hdrlen
;
238 * Has "pcap_breakloop()" been called?
239 * If so, return immediately - if we haven't read any
240 * packets, clear the flag and return -2 to indicate
241 * that we were told to break out of the loop, otherwise
242 * leave the flag set, so that the *next* call will break
243 * out of the loop without having read any packets, and
244 * return the number of packets we've processed so far.
257 caplen
= bhp
->bh_caplen
;
258 hdrlen
= bhp
->bh_hdrlen
;
261 * Short-circuit evaluation: if using BPF filter
262 * in kernel, no need to do it now.
265 * Note: the filter code was generated assuming
266 * that p->fddipad was the amount of padding
267 * before the header, as that's what's required
268 * in the kernel, so we run the filter before
269 * skipping that padding.
273 bpf_filter(fcode
, datap
, bhp
->bh_datalen
, caplen
)) {
274 struct pcap_pkthdr pkthdr
;
276 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
279 * AIX's BPF returns seconds/nanoseconds time
280 * stamps, not seconds/microseconds time stamps.
282 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
284 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
288 pkthdr
.caplen
= caplen
- pad
;
291 if (bhp
->bh_datalen
> pad
)
292 pkthdr
.len
= bhp
->bh_datalen
- pad
;
297 pkthdr
.caplen
= caplen
;
298 pkthdr
.len
= bhp
->bh_datalen
;
300 (*callback
)(user
, &pkthdr
, datap
);
301 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
302 if (++n
>= cnt
&& cnt
> 0) {
311 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
320 pcap_inject_bpf(pcap_t
*p
, const void *buf
, size_t size
)
324 ret
= write(p
->fd
, buf
, size
);
326 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
328 * In Mac OS X, there's a bug wherein setting the
329 * BIOCSHDRCMPLT flag causes writes to fail; see,
332 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
334 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
335 * assume it's due to that bug, and turn off that flag
336 * and try again. If we succeed, it either means that
337 * somebody applied the fix from that URL, or other patches
340 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
342 * and are running a Darwin kernel with those fixes, or
343 * that Apple fixed the problem in some OS X release.
345 u_int spoof_eth_src
= 0;
347 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
348 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
349 "send: can't turn off BIOCSHDRCMPLT: %s",
350 pcap_strerror(errno
));
355 * Now try the write again.
357 ret
= write(p
->fd
, buf
, size
);
359 #endif /* __APPLE__ */
361 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
362 pcap_strerror(errno
));
370 bpf_odminit(char *errbuf
)
374 if (odm_initialize() == -1) {
375 if (odm_err_msg(odmerrno
, &errstr
) == -1)
376 errstr
= "Unknown error";
377 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
378 "bpf_load: odm_initialize failed: %s",
383 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
384 if (odm_err_msg(odmerrno
, &errstr
) == -1)
385 errstr
= "Unknown error";
386 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
387 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
396 bpf_odmcleanup(char *errbuf
)
400 if (odm_unlock(odmlockid
) == -1) {
401 if (odm_err_msg(odmerrno
, &errstr
) == -1)
402 errstr
= "Unknown error";
403 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
404 "bpf_load: odm_unlock failed: %s",
409 if (odm_terminate() == -1) {
410 if (odm_err_msg(odmerrno
, &errstr
) == -1)
411 errstr
= "Unknown error";
412 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
413 "bpf_load: odm_terminate failed: %s",
422 bpf_load(char *errbuf
)
426 int numminors
, i
, rc
;
429 struct bpf_config cfg_bpf
;
430 struct cfg_load cfg_ld
;
431 struct cfg_kmod cfg_km
;
434 * This is very very close to what happens in the real implementation
435 * but I've fixed some (unlikely) bug situations.
440 if (bpf_odminit(errbuf
) != 0)
443 major
= genmajor(BPF_NAME
);
445 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
446 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
450 minors
= getminor(major
, &numminors
, BPF_NAME
);
452 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
454 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
455 "bpf_load: genminor failed: %s",
456 pcap_strerror(errno
));
461 if (bpf_odmcleanup(errbuf
))
464 rc
= stat(BPF_NODE
"0", &sbuf
);
465 if (rc
== -1 && errno
!= ENOENT
) {
466 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
467 "bpf_load: can't stat %s: %s",
468 BPF_NODE
"0", pcap_strerror(errno
));
472 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
473 for (i
= 0; i
< BPF_MINORS
; i
++) {
474 sprintf(buf
, "%s%d", BPF_NODE
, i
);
476 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
477 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
478 "bpf_load: can't mknod %s: %s",
479 buf
, pcap_strerror(errno
));
485 /* Check if the driver is loaded */
486 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
488 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
489 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
490 (cfg_ld
.kmid
== 0)) {
491 /* Driver isn't loaded, load it now */
492 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
493 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
494 "bpf_load: could not load driver: %s",
500 /* Configure the driver */
501 cfg_km
.cmd
= CFG_INIT
;
502 cfg_km
.kmid
= cfg_ld
.kmid
;
503 cfg_km
.mdilen
= sizeof(cfg_bpf
);
504 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
505 for (i
= 0; i
< BPF_MINORS
; i
++) {
506 cfg_bpf
.devno
= domakedev(major
, i
);
507 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
508 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
509 "bpf_load: could not configure driver: %s",
522 bpf_open(pcap_t
*p
, char *errbuf
)
526 char device
[sizeof "/dev/bpf0000000000"];
530 * Load the bpf driver, if it isn't already loaded,
531 * and create the BPF device entries, if they don't
534 if (bpf_load(errbuf
) == -1)
539 * Go through all the minors and find one that isn't in use.
542 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
544 * Initially try a read/write open (to allow the inject
545 * method to work). If that fails due to permission
546 * issues, fall back to read-only. This allows a
547 * non-root user to be granted specific access to pcap
548 * capabilities via file permissions.
550 * XXX - we should have an API that has a flag that
551 * controls whether to open read-only or read-write,
552 * so that denial of permission to send (or inability
553 * to send, if sending packets isn't supported on
554 * the device in question) can be indicated at open
557 fd
= open(device
, O_RDWR
);
558 if (fd
== -1 && errno
== EACCES
)
559 fd
= open(device
, O_RDONLY
);
560 } while (fd
< 0 && errno
== EBUSY
);
563 * XXX better message for all minors used
566 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "(no devices found) %s: %s",
567 device
, pcap_strerror(errno
));
573 * We include the OS's <net/bpf.h>, not our "pcap-bpf.h", so we probably
574 * don't get DLT_DOCSIS defined.
577 #define DLT_DOCSIS 143
581 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
586 struct bpf_version bv
;
588 struct bpf_dltlist bdl
;
590 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
591 u_int spoof_eth_src
= 1;
595 struct bpf_insn total_insn
;
596 struct bpf_program total_prog
;
597 struct utsname osinfo
;
600 if (strstr(device
, "dag")) {
601 return dag_open_live(device
, snaplen
, promisc
, to_ms
, ebuf
);
603 #endif /* HAVE_DAG_API */
606 memset(&bdl
, 0, sizeof(bdl
));
609 p
= (pcap_t
*)malloc(sizeof(*p
));
611 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
612 pcap_strerror(errno
));
615 memset(p
, 0, sizeof(*p
));
616 fd
= bpf_open(p
, ebuf
);
621 p
->snapshot
= snaplen
;
623 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
624 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
625 pcap_strerror(errno
));
628 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
629 bv
.bv_minor
< BPF_MINOR_VERSION
) {
630 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
631 "kernel bpf filter out of date");
636 * Try finding a good size for the buffer; 32768 may be too
637 * big, so keep cutting it in half until we find a size
638 * that works, or run out of sizes to try. If the default
639 * is larger, don't make it smaller.
641 * XXX - there should be a user-accessible hook to set the
642 * initial buffer size.
644 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) || v
< 32768)
646 for ( ; v
!= 0; v
>>= 1) {
647 /* Ignore the return value - this is because the call fails
648 * on BPF systems that don't have kernel malloc. And if
649 * the call fails, it's no big deal, we just continue to
650 * use the standard buffer size.
652 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
654 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
655 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
656 break; /* that size worked; we're done */
658 if (errno
!= ENOBUFS
) {
659 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
660 device
, pcap_strerror(errno
));
666 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
667 "BIOCSBLEN: %s: No buffer size worked", device
);
671 /* Get the data link layer type. */
672 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
673 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
674 pcap_strerror(errno
));
679 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
702 * We don't know what to map this to yet.
704 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
709 #if _BSDI_VERSION - 0 >= 199510
710 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
725 case 12: /*DLT_C_HDLC*/
732 p
->fddipad
= PCAP_FDDIPAD
:
740 * We know the default link type -- now determine all the DLTs
741 * this interface supports. If this fails with EINVAL, it's
742 * not fatal; we just don't get to use the feature later.
744 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)&bdl
) == 0) {
748 bdl
.bfl_list
= (u_int
*) malloc(sizeof(u_int
) * bdl
.bfl_len
+ 1);
749 if (bdl
.bfl_list
== NULL
) {
750 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
751 pcap_strerror(errno
));
755 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)&bdl
) < 0) {
756 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
757 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
763 * OK, for real Ethernet devices, add DLT_DOCSIS to the
764 * list, so that an application can let you choose it,
765 * in case you're capturing DOCSIS traffic that a Cisco
766 * Cable Modem Termination System is putting out onto
767 * an Ethernet (it doesn't put an Ethernet header onto
768 * the wire, it puts raw DOCSIS frames out on the wire
769 * inside the low-level Ethernet framing).
771 * A "real Ethernet device" is defined here as a device
772 * that has a link-layer type of DLT_EN10MB and that has
773 * no alternate link-layer types; that's done to exclude
774 * 802.11 interfaces (which might or might not be the
775 * right thing to do, but I suspect it is - Ethernet <->
776 * 802.11 bridges would probably badly mishandle frames
777 * that don't have Ethernet headers).
779 if (p
->linktype
== DLT_EN10MB
) {
781 for (i
= 0; i
< bdl
.bfl_len
; i
++) {
782 if (bdl
.bfl_list
[i
] != DLT_EN10MB
) {
789 * We reserved one more slot at the end of
792 bdl
.bfl_list
[bdl
.bfl_len
] = DLT_DOCSIS
;
796 p
->dlt_count
= bdl
.bfl_len
;
797 p
->dlt_list
= bdl
.bfl_list
;
799 if (errno
!= EINVAL
) {
800 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
801 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
808 * If this is an Ethernet device, and we don't have a DLT_ list,
809 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
810 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
811 * do, but there's not much we can do about that without finding
812 * some other way of determining whether it's an Ethernet or 802.11
815 if (p
->linktype
== DLT_EN10MB
&& p
->dlt_count
== 0) {
816 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
818 * If that fails, just leave the list empty.
820 if (p
->dlt_list
!= NULL
) {
821 p
->dlt_list
[0] = DLT_EN10MB
;
822 p
->dlt_list
[1] = DLT_DOCSIS
;
827 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
829 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
830 * the link-layer source address isn't forcibly overwritten.
831 * (Should we ignore errors? Should we do this only if
832 * we're open for writing?)
834 * XXX - I seem to remember some packet-sending bug in some
835 * BSDs - check CVS log for "bpf.c"?
837 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
838 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
839 "BIOCSHDRCMPLT: %s", pcap_strerror(errno
));
846 * XXX - is this seconds/nanoseconds in AIX?
847 * (Treating it as such doesn't fix the timeout
848 * problem described below.)
851 to
.tv_sec
= to_ms
/ 1000;
852 to
.tv_usec
= (to_ms
* 1000) % 1000000;
853 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
854 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT: %s",
855 pcap_strerror(errno
));
863 * Darren Reed notes that
865 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
866 * timeout appears to be ignored and it waits until the buffer
867 * is filled before returning. The result of not having it
868 * set is almost worse than useless if your BPF filter
869 * is reducing things to only a few packets (i.e. one every
872 * so we turn BIOCIMMEDIATE mode on if this is AIX.
874 * We don't turn it on for other platforms, as that means we
875 * get woken up for every packet, which may not be what we want;
876 * in the Winter 1993 USENIX paper on BPF, they say:
878 * Since a process might want to look at every packet on a
879 * network and the time between packets can be only a few
880 * microseconds, it is not possible to do a read system call
881 * per packet and BPF must collect the data from several
882 * packets and return it as a unit when the monitoring
883 * application does a read.
885 * which I infer is the reason for the timeout - it means we
886 * wait that amount of time, in the hopes that more packets
887 * will arrive and we'll get them all with one read.
889 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
890 * BSDs) causes the timeout to be ignored.
892 * On the other hand, some platforms (e.g., Linux) don't support
893 * timeouts, they just hand stuff to you as soon as it arrives;
894 * if that doesn't cause a problem on those platforms, it may
895 * be OK to have BIOCIMMEDIATE mode on BSD as well.
897 * (Note, though, that applications may depend on the read
898 * completing, even if no packets have arrived, when the timeout
899 * expires, e.g. GUI applications that have to check for input
900 * while waiting for packets to arrive; a non-zero timeout
901 * prevents "select()" from working right on FreeBSD and
902 * possibly other BSDs, as the timer doesn't start until a
903 * "read()" is done, so the timer isn't in effect if the
904 * application is blocked on a "select()", and the "select()"
905 * doesn't get woken up for a BPF device until the buffer
909 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
910 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCIMMEDIATE: %s",
911 pcap_strerror(errno
));
914 #endif /* BIOCIMMEDIATE */
918 /* set promiscuous mode, okay if it fails */
919 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
920 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
921 pcap_strerror(errno
));
925 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
926 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
927 pcap_strerror(errno
));
931 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
932 if (p
->buffer
== NULL
) {
933 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
934 pcap_strerror(errno
));
938 /* For some strange reason this seems to prevent the EFAULT
939 * problems we have experienced from AIX BPF. */
940 memset(p
->buffer
, 0x0, p
->bufsize
);
944 * If there's no filter program installed, there's
945 * no indication to the kernel of what the snapshot
946 * length should be, so no snapshotting is done.
948 * Therefore, when we open the device, we install
949 * an "accept everything" filter with the specified
952 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
955 total_insn
.k
= snaplen
;
957 total_prog
.bf_len
= 1;
958 total_prog
.bf_insns
= &total_insn
;
959 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
960 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
961 pcap_strerror(errno
));
966 * On most BPF platforms, either you can do a "select()" or
967 * "poll()" on a BPF file descriptor and it works correctly,
968 * or you can do it and it will return "readable" if the
969 * hold buffer is full but not if the timeout expires *and*
970 * a non-blocking read will, if the hold buffer is empty
971 * but the store buffer isn't empty, rotate the buffers
972 * and return what packets are available.
974 * In the latter case, the fact that a non-blocking read
975 * will give you the available packets means you can work
976 * around the failure of "select()" and "poll()" to wake up
977 * and return "readable" when the timeout expires by using
978 * the timeout as the "select()" or "poll()" timeout, putting
979 * the BPF descriptor into non-blocking mode, and read from
980 * it regardless of whether "select()" reports it as readable
983 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
984 * won't wake up and return "readable" if the timer expires
985 * and non-blocking reads return EWOULDBLOCK if the hold
986 * buffer is empty, even if the store buffer is non-empty.
988 * This means the workaround in question won't work.
990 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
991 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
992 * here". On all other BPF platforms, we set it to the FD for
993 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
994 * read will, if the hold buffer is empty and the store buffer
995 * isn't empty, rotate the buffers and return what packets are
996 * there (and in sufficiently recent versions of OpenBSD
997 * "select()" and "poll()" should work correctly).
999 * XXX - what about AIX?
1001 if (uname(&osinfo
) == 0) {
1003 * We can check what OS this is.
1005 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0 &&
1006 (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
1007 strncmp(osinfo
.release
, "4.4-", 4) == 0))
1008 p
->selectable_fd
= -1;
1010 p
->selectable_fd
= p
->fd
;
1013 * We can't find out what OS this is, so assume we can
1014 * do a "select()" or "poll()".
1016 p
->selectable_fd
= p
->fd
;
1019 p
->read_op
= pcap_read_bpf
;
1020 p
->inject_op
= pcap_inject_bpf
;
1021 p
->setfilter_op
= pcap_setfilter_bpf
;
1022 p
->set_datalink_op
= pcap_set_datalink_bpf
;
1023 p
->getnonblock_op
= pcap_getnonblock_fd
;
1024 p
->setnonblock_op
= pcap_setnonblock_fd
;
1025 p
->stats_op
= pcap_stats_bpf
;
1026 p
->close_op
= pcap_close_common
;
1031 if (p
->dlt_list
!= NULL
)
1038 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
1041 if (dag_platform_finddevs(alldevsp
, errbuf
) < 0)
1043 #endif /* HAVE_DAG_API */
1049 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
1052 * It looks that BPF code generated by gen_protochain() is not
1053 * compatible with some of kernel BPF code (for example BSD/OS 3.1).
1054 * Take a safer side for now.
1058 * XXX - what if we already have a filter in the kernel?
1060 if (install_bpf_program(p
, fp
) < 0)
1062 p
->md
.use_bpf
= 0; /* filtering in userland */
1067 * Free any user-mode filter we might happen to have installed.
1069 pcap_freecode(&p
->fcode
);
1072 * Try to install the kernel filter.
1074 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) < 0) {
1075 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
1076 pcap_strerror(errno
));
1079 p
->md
.use_bpf
= 1; /* filtering in the kernel */
1084 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
1087 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
1088 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
1089 "Cannot set DLT %d: %s", dlt
, strerror(errno
));