]>
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.98 2007-06-11 10:04:25 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>
40 #if defined(__NetBSD__) && defined(_PATH_BPF)
41 #define HAVE_CLONING_BPF
42 #endif /* __NetBSD__ && _PATH_BPF */
43 #endif /* HAVE_PATHS_H */
50 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
51 * native OS version, as we need "struct bpf_config" from it.
53 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
55 #include <sys/types.h>
58 * Prevent bpf.h from redefining the DLT_ values to their
59 * IFT_ values, as we're going to return the standard libpcap
60 * values, not IBM's non-standard IFT_ values.
66 #include <net/if_types.h> /* for IFT_ values */
67 #include <sys/sysconfig.h>
68 #include <sys/device.h>
69 #include <sys/cfgodm.h>
73 #define domakedev makedev64
74 #define getmajor major64
75 #define bpf_hdr bpf_hdr32
77 #define domakedev makedev
78 #define getmajor major
79 #endif /* __64BIT__ */
81 #define BPF_NAME "bpf"
83 #define DRIVER_PATH "/usr/lib/drivers"
84 #define BPF_NODE "/dev/bpf"
85 static int bpfloadedflag
= 0;
86 static int odmlockid
= 0;
102 #include "pcap-int.h"
105 #include "pcap-dag.h"
106 #endif /* HAVE_DAG_API */
108 #ifdef HAVE_OS_PROTO_H
109 #include "os-proto.h"
112 #include "gencode.h" /* for "no_optimize" */
114 static int pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
);
115 static int pcap_setdirection_bpf(pcap_t
*, pcap_direction_t
);
116 static int pcap_set_datalink_bpf(pcap_t
*p
, int dlt
);
119 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
124 * "ps_recv" counts packets handed to the filter, not packets
125 * that passed the filter. This includes packets later dropped
126 * because we ran out of buffer space.
128 * "ps_drop" counts packets dropped inside the BPF device
129 * because we ran out of buffer space. It doesn't count
130 * packets dropped by the interface driver. It counts
131 * only packets that passed the filter.
133 * Both statistics include packets not yet read from the kernel
134 * by libpcap, and thus not yet seen by the application.
136 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
137 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
138 pcap_strerror(errno
));
142 ps
->ps_recv
= s
.bs_recv
;
143 ps
->ps_drop
= s
.bs_drop
;
148 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
152 register u_char
*bp
, *ep
;
154 struct bpf_insn
*fcode
;
159 fcode
= p
->md
.use_bpf
? NULL
: p
->fcode
.bf_insns
;
162 * Has "pcap_breakloop()" been called?
166 * Yes - clear the flag that indicates that it
167 * has, and return -2 to indicate that we were
168 * told to break out of the loop.
175 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
177 /* Don't choke when we get ptraced */
186 * Sigh. More AIX wonderfulness.
188 * For some unknown reason the uiomove()
189 * operation in the bpf kernel extension
190 * used to copy the buffer into user
191 * space sometimes returns EFAULT. I have
192 * no idea why this is the case given that
193 * a kernel debugger shows the user buffer
194 * is correct. This problem appears to
195 * be mostly mitigated by the memset of
196 * the buffer before it is first used.
197 * Very strange.... Shaun Clowes
199 * In any case this means that we shouldn't
200 * treat EFAULT as a fatal error; as we
201 * don't have an API for returning
202 * a "some packets were dropped since
203 * the last packet you saw" indication,
204 * we just ignore EFAULT and keep reading.
211 #if defined(sun) && !defined(BSD)
213 * Due to a SunOS bug, after 2^31 bytes, the kernel
214 * file offset overflows and read fails with EINVAL.
215 * The lseek() to 0 will fix things.
218 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
220 (void)lseek(p
->fd
, 0L, SEEK_SET
);
226 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
227 pcap_strerror(errno
));
235 * Loop through each packet.
237 #define bhp ((struct bpf_hdr *)bp)
243 register int caplen
, hdrlen
;
246 * Has "pcap_breakloop()" been called?
247 * If so, return immediately - if we haven't read any
248 * packets, clear the flag and return -2 to indicate
249 * that we were told to break out of the loop, otherwise
250 * leave the flag set, so that the *next* call will break
251 * out of the loop without having read any packets, and
252 * return the number of packets we've processed so far.
265 caplen
= bhp
->bh_caplen
;
266 hdrlen
= bhp
->bh_hdrlen
;
269 * Short-circuit evaluation: if using BPF filter
270 * in kernel, no need to do it now.
273 * Note: the filter code was generated assuming
274 * that p->fddipad was the amount of padding
275 * before the header, as that's what's required
276 * in the kernel, so we run the filter before
277 * skipping that padding.
281 bpf_filter(fcode
, datap
, bhp
->bh_datalen
, caplen
)) {
282 struct pcap_pkthdr pkthdr
;
284 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
287 * AIX's BPF returns seconds/nanoseconds time
288 * stamps, not seconds/microseconds time stamps.
290 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
292 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
296 pkthdr
.caplen
= caplen
- pad
;
299 if (bhp
->bh_datalen
> pad
)
300 pkthdr
.len
= bhp
->bh_datalen
- pad
;
305 pkthdr
.caplen
= caplen
;
306 pkthdr
.len
= bhp
->bh_datalen
;
308 (*callback
)(user
, &pkthdr
, datap
);
309 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
310 if (++n
>= cnt
&& cnt
> 0) {
319 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
328 pcap_inject_bpf(pcap_t
*p
, const void *buf
, size_t size
)
332 ret
= write(p
->fd
, buf
, size
);
334 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
336 * In Mac OS X, there's a bug wherein setting the
337 * BIOCSHDRCMPLT flag causes writes to fail; see,
340 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
342 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
343 * assume it's due to that bug, and turn off that flag
344 * and try again. If we succeed, it either means that
345 * somebody applied the fix from that URL, or other patches
348 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
350 * and are running a Darwin kernel with those fixes, or
351 * that Apple fixed the problem in some OS X release.
353 u_int spoof_eth_src
= 0;
355 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
356 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
357 "send: can't turn off BIOCSHDRCMPLT: %s",
358 pcap_strerror(errno
));
363 * Now try the write again.
365 ret
= write(p
->fd
, buf
, size
);
367 #endif /* __APPLE__ */
369 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
370 pcap_strerror(errno
));
378 bpf_odminit(char *errbuf
)
382 if (odm_initialize() == -1) {
383 if (odm_err_msg(odmerrno
, &errstr
) == -1)
384 errstr
= "Unknown error";
385 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
386 "bpf_load: odm_initialize failed: %s",
391 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
392 if (odm_err_msg(odmerrno
, &errstr
) == -1)
393 errstr
= "Unknown error";
394 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
395 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
404 bpf_odmcleanup(char *errbuf
)
408 if (odm_unlock(odmlockid
) == -1) {
409 if (odm_err_msg(odmerrno
, &errstr
) == -1)
410 errstr
= "Unknown error";
411 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
412 "bpf_load: odm_unlock failed: %s",
417 if (odm_terminate() == -1) {
418 if (odm_err_msg(odmerrno
, &errstr
) == -1)
419 errstr
= "Unknown error";
420 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
421 "bpf_load: odm_terminate failed: %s",
430 bpf_load(char *errbuf
)
434 int numminors
, i
, rc
;
437 struct bpf_config cfg_bpf
;
438 struct cfg_load cfg_ld
;
439 struct cfg_kmod cfg_km
;
442 * This is very very close to what happens in the real implementation
443 * but I've fixed some (unlikely) bug situations.
448 if (bpf_odminit(errbuf
) != 0)
451 major
= genmajor(BPF_NAME
);
453 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
454 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
458 minors
= getminor(major
, &numminors
, BPF_NAME
);
460 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
462 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
463 "bpf_load: genminor failed: %s",
464 pcap_strerror(errno
));
469 if (bpf_odmcleanup(errbuf
))
472 rc
= stat(BPF_NODE
"0", &sbuf
);
473 if (rc
== -1 && errno
!= ENOENT
) {
474 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
475 "bpf_load: can't stat %s: %s",
476 BPF_NODE
"0", pcap_strerror(errno
));
480 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
481 for (i
= 0; i
< BPF_MINORS
; i
++) {
482 sprintf(buf
, "%s%d", BPF_NODE
, i
);
484 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
485 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
486 "bpf_load: can't mknod %s: %s",
487 buf
, pcap_strerror(errno
));
493 /* Check if the driver is loaded */
494 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
496 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
497 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
498 (cfg_ld
.kmid
== 0)) {
499 /* Driver isn't loaded, load it now */
500 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
501 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
502 "bpf_load: could not load driver: %s",
508 /* Configure the driver */
509 cfg_km
.cmd
= CFG_INIT
;
510 cfg_km
.kmid
= cfg_ld
.kmid
;
511 cfg_km
.mdilen
= sizeof(cfg_bpf
);
512 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
513 for (i
= 0; i
< BPF_MINORS
; i
++) {
514 cfg_bpf
.devno
= domakedev(major
, i
);
515 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
516 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
517 "bpf_load: could not configure driver: %s",
530 bpf_open(pcap_t
*p
, char *errbuf
)
533 #ifdef HAVE_CLONING_BPF
534 static const char device
[] = _PATH_BPF
;
537 char device
[sizeof "/dev/bpf0000000000"];
542 * Load the bpf driver, if it isn't already loaded,
543 * and create the BPF device entries, if they don't
546 if (bpf_load(errbuf
) == -1)
550 #ifdef HAVE_CLONING_BPF
551 if ((fd
= open(device
, O_RDWR
)) == -1 &&
552 (errno
!= EACCES
|| (fd
= open(device
, O_RDONLY
)) == -1))
553 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
554 "(cannot open device) %s: %s", device
, pcap_strerror(errno
));
557 * Go through all the minors and find one that isn't in use.
560 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
562 * Initially try a read/write open (to allow the inject
563 * method to work). If that fails due to permission
564 * issues, fall back to read-only. This allows a
565 * non-root user to be granted specific access to pcap
566 * capabilities via file permissions.
568 * XXX - we should have an API that has a flag that
569 * controls whether to open read-only or read-write,
570 * so that denial of permission to send (or inability
571 * to send, if sending packets isn't supported on
572 * the device in question) can be indicated at open
575 fd
= open(device
, O_RDWR
);
576 if (fd
== -1 && errno
== EACCES
)
577 fd
= open(device
, O_RDONLY
);
578 } while (fd
< 0 && errno
== EBUSY
);
581 * XXX better message for all minors used
584 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "(no devices found) %s: %s",
585 device
, pcap_strerror(errno
));
592 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
593 * don't get DLT_DOCSIS defined.
596 #define DLT_DOCSIS 143
600 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
605 struct bpf_version bv
;
607 struct bpf_dltlist bdl
;
609 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
610 u_int spoof_eth_src
= 1;
614 struct bpf_insn total_insn
;
615 struct bpf_program total_prog
;
616 struct utsname osinfo
;
619 if (strstr(device
, "dag")) {
620 return dag_open_live(device
, snaplen
, promisc
, to_ms
, ebuf
);
622 #endif /* HAVE_DAG_API */
625 memset(&bdl
, 0, sizeof(bdl
));
628 p
= (pcap_t
*)malloc(sizeof(*p
));
630 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
631 pcap_strerror(errno
));
634 memset(p
, 0, sizeof(*p
));
635 fd
= bpf_open(p
, ebuf
);
640 p
->snapshot
= snaplen
;
642 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
643 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
644 pcap_strerror(errno
));
647 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
648 bv
.bv_minor
< BPF_MINOR_VERSION
) {
649 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
650 "kernel bpf filter out of date");
655 * Try finding a good size for the buffer; 32768 may be too
656 * big, so keep cutting it in half until we find a size
657 * that works, or run out of sizes to try. If the default
658 * is larger, don't make it smaller.
660 * XXX - there should be a user-accessible hook to set the
661 * initial buffer size.
663 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) || v
< 32768)
665 for ( ; v
!= 0; v
>>= 1) {
666 /* Ignore the return value - this is because the call fails
667 * on BPF systems that don't have kernel malloc. And if
668 * the call fails, it's no big deal, we just continue to
669 * use the standard buffer size.
671 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
673 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
674 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
675 break; /* that size worked; we're done */
677 if (errno
!= ENOBUFS
) {
678 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
679 device
, pcap_strerror(errno
));
685 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
686 "BIOCSBLEN: %s: No buffer size worked", device
);
690 /* Get the data link layer type. */
691 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
692 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
693 pcap_strerror(errno
));
698 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
721 * We don't know what to map this to yet.
723 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
728 #if _BSDI_VERSION - 0 >= 199510
729 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
744 case 12: /*DLT_C_HDLC*/
751 p
->fddipad
= PCAP_FDDIPAD
;
759 * We know the default link type -- now determine all the DLTs
760 * this interface supports. If this fails with EINVAL, it's
761 * not fatal; we just don't get to use the feature later.
763 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)&bdl
) == 0) {
767 bdl
.bfl_list
= (u_int
*) malloc(sizeof(u_int
) * (bdl
.bfl_len
+ 1));
768 if (bdl
.bfl_list
== NULL
) {
769 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
770 pcap_strerror(errno
));
774 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)&bdl
) < 0) {
775 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
776 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
782 * OK, for real Ethernet devices, add DLT_DOCSIS to the
783 * list, so that an application can let you choose it,
784 * in case you're capturing DOCSIS traffic that a Cisco
785 * Cable Modem Termination System is putting out onto
786 * an Ethernet (it doesn't put an Ethernet header onto
787 * the wire, it puts raw DOCSIS frames out on the wire
788 * inside the low-level Ethernet framing).
790 * A "real Ethernet device" is defined here as a device
791 * that has a link-layer type of DLT_EN10MB and that has
792 * no alternate link-layer types; that's done to exclude
793 * 802.11 interfaces (which might or might not be the
794 * right thing to do, but I suspect it is - Ethernet <->
795 * 802.11 bridges would probably badly mishandle frames
796 * that don't have Ethernet headers).
798 if (p
->linktype
== DLT_EN10MB
) {
800 for (i
= 0; i
< bdl
.bfl_len
; i
++) {
801 if (bdl
.bfl_list
[i
] != DLT_EN10MB
) {
808 * We reserved one more slot at the end of
811 bdl
.bfl_list
[bdl
.bfl_len
] = DLT_DOCSIS
;
815 p
->dlt_count
= bdl
.bfl_len
;
816 p
->dlt_list
= bdl
.bfl_list
;
818 if (errno
!= EINVAL
) {
819 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
820 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
827 * If this is an Ethernet device, and we don't have a DLT_ list,
828 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
829 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
830 * do, but there's not much we can do about that without finding
831 * some other way of determining whether it's an Ethernet or 802.11
834 if (p
->linktype
== DLT_EN10MB
&& p
->dlt_count
== 0) {
835 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
837 * If that fails, just leave the list empty.
839 if (p
->dlt_list
!= NULL
) {
840 p
->dlt_list
[0] = DLT_EN10MB
;
841 p
->dlt_list
[1] = DLT_DOCSIS
;
846 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
848 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
849 * the link-layer source address isn't forcibly overwritten.
850 * (Should we ignore errors? Should we do this only if
851 * we're open for writing?)
853 * XXX - I seem to remember some packet-sending bug in some
854 * BSDs - check CVS log for "bpf.c"?
856 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
857 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
858 "BIOCSHDRCMPLT: %s", pcap_strerror(errno
));
865 * XXX - is this seconds/nanoseconds in AIX?
866 * (Treating it as such doesn't fix the timeout
867 * problem described below.)
870 to
.tv_sec
= to_ms
/ 1000;
871 to
.tv_usec
= (to_ms
* 1000) % 1000000;
872 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
873 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT: %s",
874 pcap_strerror(errno
));
882 * Darren Reed notes that
884 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
885 * timeout appears to be ignored and it waits until the buffer
886 * is filled before returning. The result of not having it
887 * set is almost worse than useless if your BPF filter
888 * is reducing things to only a few packets (i.e. one every
891 * so we turn BIOCIMMEDIATE mode on if this is AIX.
893 * We don't turn it on for other platforms, as that means we
894 * get woken up for every packet, which may not be what we want;
895 * in the Winter 1993 USENIX paper on BPF, they say:
897 * Since a process might want to look at every packet on a
898 * network and the time between packets can be only a few
899 * microseconds, it is not possible to do a read system call
900 * per packet and BPF must collect the data from several
901 * packets and return it as a unit when the monitoring
902 * application does a read.
904 * which I infer is the reason for the timeout - it means we
905 * wait that amount of time, in the hopes that more packets
906 * will arrive and we'll get them all with one read.
908 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
909 * BSDs) causes the timeout to be ignored.
911 * On the other hand, some platforms (e.g., Linux) don't support
912 * timeouts, they just hand stuff to you as soon as it arrives;
913 * if that doesn't cause a problem on those platforms, it may
914 * be OK to have BIOCIMMEDIATE mode on BSD as well.
916 * (Note, though, that applications may depend on the read
917 * completing, even if no packets have arrived, when the timeout
918 * expires, e.g. GUI applications that have to check for input
919 * while waiting for packets to arrive; a non-zero timeout
920 * prevents "select()" from working right on FreeBSD and
921 * possibly other BSDs, as the timer doesn't start until a
922 * "read()" is done, so the timer isn't in effect if the
923 * application is blocked on a "select()", and the "select()"
924 * doesn't get woken up for a BPF device until the buffer
928 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
929 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCIMMEDIATE: %s",
930 pcap_strerror(errno
));
933 #endif /* BIOCIMMEDIATE */
937 /* set promiscuous mode, okay if it fails */
938 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
939 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
940 pcap_strerror(errno
));
944 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
945 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
946 pcap_strerror(errno
));
950 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
951 if (p
->buffer
== NULL
) {
952 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
953 pcap_strerror(errno
));
957 /* For some strange reason this seems to prevent the EFAULT
958 * problems we have experienced from AIX BPF. */
959 memset(p
->buffer
, 0x0, p
->bufsize
);
963 * If there's no filter program installed, there's
964 * no indication to the kernel of what the snapshot
965 * length should be, so no snapshotting is done.
967 * Therefore, when we open the device, we install
968 * an "accept everything" filter with the specified
971 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
974 total_insn
.k
= snaplen
;
976 total_prog
.bf_len
= 1;
977 total_prog
.bf_insns
= &total_insn
;
978 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
979 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
980 pcap_strerror(errno
));
985 * On most BPF platforms, either you can do a "select()" or
986 * "poll()" on a BPF file descriptor and it works correctly,
987 * or you can do it and it will return "readable" if the
988 * hold buffer is full but not if the timeout expires *and*
989 * a non-blocking read will, if the hold buffer is empty
990 * but the store buffer isn't empty, rotate the buffers
991 * and return what packets are available.
993 * In the latter case, the fact that a non-blocking read
994 * will give you the available packets means you can work
995 * around the failure of "select()" and "poll()" to wake up
996 * and return "readable" when the timeout expires by using
997 * the timeout as the "select()" or "poll()" timeout, putting
998 * the BPF descriptor into non-blocking mode, and read from
999 * it regardless of whether "select()" reports it as readable
1002 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
1003 * won't wake up and return "readable" if the timer expires
1004 * and non-blocking reads return EWOULDBLOCK if the hold
1005 * buffer is empty, even if the store buffer is non-empty.
1007 * This means the workaround in question won't work.
1009 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
1010 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
1011 * here". On all other BPF platforms, we set it to the FD for
1012 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
1013 * read will, if the hold buffer is empty and the store buffer
1014 * isn't empty, rotate the buffers and return what packets are
1015 * there (and in sufficiently recent versions of OpenBSD
1016 * "select()" and "poll()" should work correctly).
1018 * XXX - what about AIX?
1020 p
->selectable_fd
= p
->fd
; /* assume select() works until we know otherwise */
1021 if (uname(&osinfo
) == 0) {
1023 * We can check what OS this is.
1025 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0) {
1026 if (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
1027 strncmp(osinfo
.release
, "4.4-", 4) == 0)
1028 p
->selectable_fd
= -1;
1032 p
->read_op
= pcap_read_bpf
;
1033 p
->inject_op
= pcap_inject_bpf
;
1034 p
->setfilter_op
= pcap_setfilter_bpf
;
1035 p
->setdirection_op
= pcap_setdirection_bpf
;
1036 p
->set_datalink_op
= pcap_set_datalink_bpf
;
1037 p
->getnonblock_op
= pcap_getnonblock_fd
;
1038 p
->setnonblock_op
= pcap_setnonblock_fd
;
1039 p
->stats_op
= pcap_stats_bpf
;
1040 p
->close_op
= pcap_close_common
;
1045 if (p
->dlt_list
!= NULL
)
1052 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
1055 if (dag_platform_finddevs(alldevsp
, errbuf
) < 0)
1057 #endif /* HAVE_DAG_API */
1063 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
1066 * It looks that BPF code generated by gen_protochain() is not
1067 * compatible with some of kernel BPF code (for example BSD/OS 3.1).
1068 * Take a safer side for now.
1072 * XXX - what if we already have a filter in the kernel?
1074 if (install_bpf_program(p
, fp
) < 0)
1076 p
->md
.use_bpf
= 0; /* filtering in userland */
1081 * Free any user-mode filter we might happen to have installed.
1083 pcap_freecode(&p
->fcode
);
1086 * Try to install the kernel filter.
1088 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) < 0) {
1089 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
1090 pcap_strerror(errno
));
1093 p
->md
.use_bpf
= 1; /* filtering in the kernel */
1096 * Discard any previously-received packets, as they might have
1097 * passed whatever filter was formerly in effect, but might
1098 * not pass this filter (BIOCSETF discards packets buffered
1099 * in the kernel, so you can lose packets in any case).
1106 * Set direction flag: Which packets do we accept on a forwarding
1107 * single device? IN, OUT or both?
1110 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
1112 #if defined(BIOCSDIRECTION)
1115 direction
= (d
== PCAP_D_IN
) ? BPF_D_IN
:
1116 ((d
== PCAP_D_OUT
) ? BPF_D_OUT
: BPF_D_INOUT
);
1117 if (ioctl(p
->fd
, BIOCSDIRECTION
, &direction
) == -1) {
1118 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
1119 "Cannot set direction to %s: %s",
1120 (d
== PCAP_D_IN
) ? "PCAP_D_IN" :
1121 ((d
== PCAP_D_OUT
) ? "PCAP_D_OUT" : "PCAP_D_INOUT"),
1126 #elif defined(BIOCSSEESENT)
1130 * We don't support PCAP_D_OUT.
1132 if (d
== PCAP_D_OUT
) {
1133 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
1134 "Setting direction to PCAP_D_OUT is not supported on BPF");
1138 seesent
= (d
== PCAP_D_INOUT
);
1139 if (ioctl(p
->fd
, BIOCSSEESENT
, &seesent
) == -1) {
1140 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
1141 "Cannot set direction to %s: %s",
1142 (d
== PCAP_D_INOUT
) ? "PCAP_D_INOUT" : "PCAP_D_IN",
1148 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
1149 "This system doesn't support BIOCSSEESENT, so the direction can't be set");
1155 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
1158 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
1159 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
1160 "Cannot set DLT %d: %s", dlt
, strerror(errno
));