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.116 2008-09-16 18:42:29 guy Exp $ (LBL)";
30 #include <sys/param.h> /* optionally get BSD define */
31 #ifdef HAVE_ZEROCOPY_BPF
35 #include <sys/socket.h>
37 * <net/bpf.h> defines ioctls, but doesn't include <sys/ioccom.h>.
39 * We include <sys/ioctl.h> as it might be necessary to declare ioctl();
40 * at least on *BSD and Mac OS X, it also defines various SIOC ioctls -
41 * we could include <sys/sockio.h>, but if we're already including
42 * <sys/ioctl.h>, which includes <sys/sockio.h> on those platforms,
43 * there's not much point in doing so.
45 * If we have <sys/ioccom.h>, we include it as well, to handle systems
46 * such as Solaris which don't arrange to include <sys/ioccom.h> if you
47 * include <sys/ioctl.h>
49 #include <sys/ioctl.h>
50 #ifdef HAVE_SYS_IOCCOM_H
51 #include <sys/ioccom.h>
53 #include <sys/utsname.h>
55 #ifdef HAVE_ZEROCOPY_BPF
56 #include <machine/atomic.h>
64 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
65 * native OS version, as we need "struct bpf_config" from it.
67 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
69 #include <sys/types.h>
72 * Prevent bpf.h from redefining the DLT_ values to their
73 * IFT_ values, as we're going to return the standard libpcap
74 * values, not IBM's non-standard IFT_ values.
80 #include <net/if_types.h> /* for IFT_ values */
81 #include <sys/sysconfig.h>
82 #include <sys/device.h>
83 #include <sys/cfgodm.h>
87 #define domakedev makedev64
88 #define getmajor major64
89 #define bpf_hdr bpf_hdr32
91 #define domakedev makedev
92 #define getmajor major
93 #endif /* __64BIT__ */
95 #define BPF_NAME "bpf"
97 #define DRIVER_PATH "/usr/lib/drivers"
98 #define BPF_NODE "/dev/bpf"
99 static int bpfloadedflag
= 0;
100 static int odmlockid
= 0;
102 static int bpf_load(char *errbuf
);
119 #ifdef HAVE_NET_IF_MEDIA_H
120 # include <net/if_media.h>
123 #include "pcap-int.h"
126 #include "pcap-dag.h"
127 #endif /* HAVE_DAG_API */
130 #include "pcap-snf.h"
131 #endif /* HAVE_SNF_API */
133 #ifdef HAVE_OS_PROTO_H
134 #include "os-proto.h"
138 # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
139 #define HAVE_BSD_IEEE80211
142 # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
143 static int find_802_11(struct bpf_dltlist
*);
145 # ifdef HAVE_BSD_IEEE80211
146 static int monitor_mode(pcap_t
*, int);
149 # if defined(__APPLE__)
150 static void remove_en(pcap_t
*);
151 static void remove_802_11(pcap_t
*);
154 # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
156 #endif /* BIOCGDLTLIST */
159 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
160 * don't get DLT_DOCSIS defined.
163 #define DLT_DOCSIS 143
167 * On OS X, we don't even get any of the 802.11-plus-radio-header DLT_'s
168 * defined, even though some of them are used by various Airport drivers.
170 #ifndef DLT_PRISM_HEADER
171 #define DLT_PRISM_HEADER 119
173 #ifndef DLT_AIRONET_HEADER
174 #define DLT_AIRONET_HEADER 120
176 #ifndef DLT_IEEE802_11_RADIO
177 #define DLT_IEEE802_11_RADIO 127
179 #ifndef DLT_IEEE802_11_RADIO_AVS
180 #define DLT_IEEE802_11_RADIO_AVS 163
183 static int pcap_can_set_rfmon_bpf(pcap_t
*p
);
184 static int pcap_activate_bpf(pcap_t
*p
);
185 static int pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
);
186 static int pcap_setdirection_bpf(pcap_t
*, pcap_direction_t
);
187 static int pcap_set_datalink_bpf(pcap_t
*p
, int dlt
);
189 #ifdef HAVE_ZEROCOPY_BPF
191 * For zerocopy bpf, we need to override the setnonblock/getnonblock routines
192 * so we don't call select(2) if the pcap handle is in non-blocking mode. We
193 * preserve the timeout supplied by pcap_open functions to make sure it
194 * does not get clobbered if the pcap handle moves between blocking and non-
198 pcap_getnonblock_zbuf(pcap_t
*p
, char *errbuf
)
201 * Use a negative value for the timeout to represent that the
202 * pcap handle is in non-blocking mode.
204 return (p
->md
.timeout
< 0);
208 pcap_setnonblock_zbuf(pcap_t
*p
, int nonblock
, char *errbuf
)
211 * Map each value to the corresponding 2's complement, to
212 * preserve the timeout value provided with pcap_set_timeout.
213 * (from pcap-linux.c).
216 if (p
->md
.timeout
>= 0) {
218 * Timeout is non-negative, so we're not already
219 * in non-blocking mode; set it to the 2's
220 * complement, to make it negative, as an
221 * indication that we're in non-blocking mode.
223 p
->md
.timeout
= p
->md
.timeout
* -1 - 1;
226 if (p
->md
.timeout
< 0) {
228 * Timeout is negative, so we're not already
229 * in blocking mode; reverse the previous
230 * operation, to make the timeout non-negative
233 p
->md
.timeout
= (p
->md
.timeout
+ 1) * -1;
240 * Zero-copy specific close method. Un-map the shared buffers then call
241 * pcap_cleanup_live_common.
244 pcap_cleanup_zbuf(pcap_t
*p
)
247 * Delete the mappings. Note that p->buffer gets initialized to one
248 * of the mmapped regions in this case, so do not try and free it
249 * directly; null it out so that pcap_cleanup_live_common() doesn't
252 if (p
->md
.zbuf1
!= MAP_FAILED
&& p
->md
.zbuf1
!= NULL
)
253 (void) munmap(p
->md
.zbuf1
, p
->md
.zbufsize
);
254 if (p
->md
.zbuf2
!= MAP_FAILED
&& p
->md
.zbuf2
!= NULL
)
255 (void) munmap(p
->md
.zbuf2
, p
->md
.zbufsize
);
257 pcap_cleanup_live_common(p
);
261 * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
262 * shared memory buffers.
264 * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer,
265 * and set up p->buffer and cc to reflect one if available. Notice that if
266 * there was no prior buffer, we select zbuf1 as this will be the first
267 * buffer filled for a fresh BPF session.
270 pcap_next_zbuf_shm(pcap_t
*p
, int *cc
)
272 struct bpf_zbuf_header
*bzh
;
274 if (p
->md
.zbuffer
== p
->md
.zbuf2
|| p
->md
.zbuffer
== NULL
) {
275 bzh
= (struct bpf_zbuf_header
*)p
->md
.zbuf1
;
276 if (bzh
->bzh_user_gen
!=
277 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
279 p
->md
.zbuffer
= (u_char
*)p
->md
.zbuf1
;
280 p
->buffer
= p
->md
.zbuffer
+ sizeof(*bzh
);
281 *cc
= bzh
->bzh_kernel_len
;
284 } else if (p
->md
.zbuffer
== p
->md
.zbuf1
) {
285 bzh
= (struct bpf_zbuf_header
*)p
->md
.zbuf2
;
286 if (bzh
->bzh_user_gen
!=
287 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
289 p
->md
.zbuffer
= (u_char
*)p
->md
.zbuf2
;
290 p
->buffer
= p
->md
.zbuffer
+ sizeof(*bzh
);
291 *cc
= bzh
->bzh_kernel_len
;
300 * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
301 * select() for data or a timeout, and possibly force rotation of the buffer
302 * in the event we time out or are in immediate mode. Invoke the shared
303 * memory check before doing system calls in order to avoid doing avoidable
307 pcap_next_zbuf(pcap_t
*p
, int *cc
)
316 #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000))
318 * Start out by seeing whether anything is waiting by checking the
319 * next shared memory buffer for data.
321 data
= pcap_next_zbuf_shm(p
, cc
);
325 * If a previous sleep was interrupted due to signal delivery, make
326 * sure that the timeout gets adjusted accordingly. This requires
327 * that we analyze when the timeout should be been expired, and
328 * subtract the current time from that. If after this operation,
329 * our timeout is less then or equal to zero, handle it like a
332 tmout
= p
->md
.timeout
;
334 (void) clock_gettime(CLOCK_MONOTONIC
, &cur
);
335 if (p
->md
.interrupted
&& p
->md
.timeout
) {
336 expire
= TSTOMILLI(&p
->md
.firstsel
) + p
->md
.timeout
;
337 tmout
= expire
- TSTOMILLI(&cur
);
340 p
->md
.interrupted
= 0;
341 data
= pcap_next_zbuf_shm(p
, cc
);
344 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
345 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
346 "BIOCROTZBUF: %s", strerror(errno
));
349 return (pcap_next_zbuf_shm(p
, cc
));
353 * No data in the buffer, so must use select() to wait for data or
354 * the next timeout. Note that we only call select if the handle
355 * is in blocking mode.
357 if (p
->md
.timeout
>= 0) {
359 FD_SET(p
->fd
, &r_set
);
361 tv
.tv_sec
= tmout
/ 1000;
362 tv
.tv_usec
= (tmout
* 1000) % 1000000;
364 r
= select(p
->fd
+ 1, &r_set
, NULL
, NULL
,
365 p
->md
.timeout
!= 0 ? &tv
: NULL
);
366 if (r
< 0 && errno
== EINTR
) {
367 if (!p
->md
.interrupted
&& p
->md
.timeout
) {
368 p
->md
.interrupted
= 1;
369 p
->md
.firstsel
= cur
;
373 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
374 "select: %s", strerror(errno
));
378 p
->md
.interrupted
= 0;
380 * Check again for data, which may exist now that we've either been
381 * woken up as a result of data or timed out. Try the "there's data"
382 * case first since it doesn't require a system call.
384 data
= pcap_next_zbuf_shm(p
, cc
);
388 * Try forcing a buffer rotation to dislodge timed out or immediate
391 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
392 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
393 "BIOCROTZBUF: %s", strerror(errno
));
396 return (pcap_next_zbuf_shm(p
, cc
));
400 * Notify kernel that we are done with the buffer. We don't reset zbuffer so
401 * that we know which buffer to use next time around.
404 pcap_ack_zbuf(pcap_t
*p
)
407 atomic_store_rel_int(&p
->md
.bzh
->bzh_user_gen
,
408 p
->md
.bzh
->bzh_kernel_gen
);
416 pcap_create(const char *device
, char *ebuf
)
421 if (strstr(device
, "dag"))
422 return (dag_create(device
, ebuf
));
423 #endif /* HAVE_DAG_API */
425 if (strstr(device
, "snf"))
426 return (snf_create(device
, ebuf
));
427 #endif /* HAVE_SNF_API */
429 p
= pcap_create_common(device
, ebuf
);
433 p
->activate_op
= pcap_activate_bpf
;
434 p
->can_set_rfmon_op
= pcap_can_set_rfmon_bpf
;
439 * On success, returns a file descriptor for a BPF device.
440 * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf.
446 #ifdef HAVE_CLONING_BPF
447 static const char device
[] = "/dev/bpf";
450 char device
[sizeof "/dev/bpf0000000000"];
455 * Load the bpf driver, if it isn't already loaded,
456 * and create the BPF device entries, if they don't
459 if (bpf_load(p
->errbuf
) == PCAP_ERROR
)
463 #ifdef HAVE_CLONING_BPF
464 if ((fd
= open(device
, O_RDWR
)) == -1 &&
465 (errno
!= EACCES
|| (fd
= open(device
, O_RDONLY
)) == -1)) {
467 fd
= PCAP_ERROR_PERM_DENIED
;
470 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
471 "(cannot open device) %s: %s", device
, pcap_strerror(errno
));
475 * Go through all the minors and find one that isn't in use.
478 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
480 * Initially try a read/write open (to allow the inject
481 * method to work). If that fails due to permission
482 * issues, fall back to read-only. This allows a
483 * non-root user to be granted specific access to pcap
484 * capabilities via file permissions.
486 * XXX - we should have an API that has a flag that
487 * controls whether to open read-only or read-write,
488 * so that denial of permission to send (or inability
489 * to send, if sending packets isn't supported on
490 * the device in question) can be indicated at open
493 fd
= open(device
, O_RDWR
);
494 if (fd
== -1 && errno
== EACCES
)
495 fd
= open(device
, O_RDONLY
);
496 } while (fd
< 0 && errno
== EBUSY
);
499 * XXX better message for all minors used
508 * /dev/bpf0 doesn't exist, which
509 * means we probably have no BPF
512 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
513 "(there are no BPF devices)");
516 * We got EBUSY on at least one
517 * BPF device, so we have BPF
518 * devices, but all the ones
519 * that exist are busy.
521 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
522 "(all BPF devices are busy)");
528 * Got EACCES on the last device we tried,
529 * and EBUSY on all devices before that,
532 fd
= PCAP_ERROR_PERM_DENIED
;
533 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
534 "(cannot open BPF device) %s: %s", device
,
535 pcap_strerror(errno
));
540 * Some other problem.
543 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
544 "(cannot open BPF device) %s: %s", device
,
545 pcap_strerror(errno
));
556 get_dlt_list(int fd
, int v
, struct bpf_dltlist
*bdlp
, char *ebuf
)
558 memset(bdlp
, 0, sizeof(*bdlp
));
559 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) == 0) {
563 bdlp
->bfl_list
= (u_int
*) malloc(sizeof(u_int
) * (bdlp
->bfl_len
+ 1));
564 if (bdlp
->bfl_list
== NULL
) {
565 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
566 pcap_strerror(errno
));
570 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) < 0) {
571 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
572 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
573 free(bdlp
->bfl_list
);
578 * OK, for real Ethernet devices, add DLT_DOCSIS to the
579 * list, so that an application can let you choose it,
580 * in case you're capturing DOCSIS traffic that a Cisco
581 * Cable Modem Termination System is putting out onto
582 * an Ethernet (it doesn't put an Ethernet header onto
583 * the wire, it puts raw DOCSIS frames out on the wire
584 * inside the low-level Ethernet framing).
586 * A "real Ethernet device" is defined here as a device
587 * that has a link-layer type of DLT_EN10MB and that has
588 * no alternate link-layer types; that's done to exclude
589 * 802.11 interfaces (which might or might not be the
590 * right thing to do, but I suspect it is - Ethernet <->
591 * 802.11 bridges would probably badly mishandle frames
592 * that don't have Ethernet headers).
594 * On Solaris with BPF, Ethernet devices also offer
595 * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
596 * treat it as an indication that the device isn't an
599 if (v
== DLT_EN10MB
) {
601 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
602 if (bdlp
->bfl_list
[i
] != DLT_EN10MB
604 && bdlp
->bfl_list
[i
] != DLT_IPNET
613 * We reserved one more slot at the end of
616 bdlp
->bfl_list
[bdlp
->bfl_len
] = DLT_DOCSIS
;
622 * EINVAL just means "we don't support this ioctl on
623 * this device"; don't treat it as an error.
625 if (errno
!= EINVAL
) {
626 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
627 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
636 pcap_can_set_rfmon_bpf(pcap_t
*p
)
638 #if defined(__APPLE__)
639 struct utsname osinfo
;
643 struct bpf_dltlist bdl
;
647 * The joys of monitor mode on OS X.
649 * Prior to 10.4, it's not supported at all.
651 * In 10.4, if adapter enN supports monitor mode, there's a
652 * wltN adapter corresponding to it; you open it, instead of
653 * enN, to get monitor mode. You get whatever link-layer
654 * headers it supplies.
656 * In 10.5, and, we assume, later releases, if adapter enN
657 * supports monitor mode, it offers, among its selectable
658 * DLT_ values, values that let you get the 802.11 header;
659 * selecting one of those values puts the adapter into monitor
660 * mode (i.e., you can't get 802.11 headers except in monitor
661 * mode, and you can't get Ethernet headers in monitor mode).
663 if (uname(&osinfo
) == -1) {
665 * Can't get the OS version; just say "no".
670 * We assume osinfo.sysname is "Darwin", because
671 * __APPLE__ is defined. We just check the version.
673 if (osinfo
.release
[0] < '8' && osinfo
.release
[1] == '.') {
675 * 10.3 (Darwin 7.x) or earlier.
676 * Monitor mode not supported.
680 if (osinfo
.release
[0] == '8' && osinfo
.release
[1] == '.') {
682 * 10.4 (Darwin 8.x). s/en/wlt/, and check
683 * whether the device exists.
685 if (strncmp(p
->opt
.source
, "en", 2) != 0) {
687 * Not an enN device; no monitor mode.
691 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
693 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
694 "socket: %s", pcap_strerror(errno
));
697 strlcpy(ifr
.ifr_name
, "wlt", sizeof(ifr
.ifr_name
));
698 strlcat(ifr
.ifr_name
, p
->opt
.source
+ 2, sizeof(ifr
.ifr_name
));
699 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
712 * Everything else is 10.5 or later; for those,
713 * we just open the enN device, and check whether
714 * we have any 802.11 devices.
716 * First, open a BPF device.
720 return (fd
); /* fd is the appropriate error code */
723 * Now bind to the device.
725 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
, sizeof(ifr
.ifr_name
));
726 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
731 * There's no such device.
734 return (PCAP_ERROR_NO_SUCH_DEVICE
);
738 * Return a "network down" indication, so that
739 * the application can report that rather than
740 * saying we had a mysterious failure and
741 * suggest that they report a problem to the
742 * libpcap developers.
745 return (PCAP_ERROR_IFACE_NOT_UP
);
748 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
750 p
->opt
.source
, pcap_strerror(errno
));
757 * We know the default link type -- now determine all the DLTs
758 * this interface supports. If this fails with EINVAL, it's
759 * not fatal; we just don't get to use the feature later.
760 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
761 * as the default DLT for this adapter.)
763 if (get_dlt_list(fd
, DLT_NULL
, &bdl
, p
->errbuf
) == PCAP_ERROR
) {
767 if (find_802_11(&bdl
) != -1) {
769 * We have an 802.11 DLT, so we can set monitor mode.
776 #endif /* BIOCGDLTLIST */
778 #elif defined(HAVE_BSD_IEEE80211)
781 ret
= monitor_mode(p
, 0);
782 if (ret
== PCAP_ERROR_RFMON_NOTSUP
)
783 return (0); /* not an error, just a "can't do" */
785 return (1); /* success */
793 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
798 * "ps_recv" counts packets handed to the filter, not packets
799 * that passed the filter. This includes packets later dropped
800 * because we ran out of buffer space.
802 * "ps_drop" counts packets dropped inside the BPF device
803 * because we ran out of buffer space. It doesn't count
804 * packets dropped by the interface driver. It counts
805 * only packets that passed the filter.
807 * Both statistics include packets not yet read from the kernel
808 * by libpcap, and thus not yet seen by the application.
810 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
811 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
812 pcap_strerror(errno
));
816 ps
->ps_recv
= s
.bs_recv
;
817 ps
->ps_drop
= s
.bs_drop
;
823 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
827 register u_char
*bp
, *ep
;
832 #ifdef HAVE_ZEROCOPY_BPF
838 * Has "pcap_breakloop()" been called?
842 * Yes - clear the flag that indicates that it
843 * has, and return PCAP_ERROR_BREAK to indicate
844 * that we were told to break out of the loop.
847 return (PCAP_ERROR_BREAK
);
852 * When reading without zero-copy from a file descriptor, we
853 * use a single buffer and return a length of data in the
854 * buffer. With zero-copy, we update the p->buffer pointer
855 * to point at whatever underlying buffer contains the next
856 * data and update cc to reflect the data found in the
859 #ifdef HAVE_ZEROCOPY_BPF
860 if (p
->md
.zerocopy
) {
861 if (p
->buffer
!= NULL
)
863 i
= pcap_next_zbuf(p
, &cc
);
871 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
874 /* Don't choke when we get ptraced */
883 * Sigh. More AIX wonderfulness.
885 * For some unknown reason the uiomove()
886 * operation in the bpf kernel extension
887 * used to copy the buffer into user
888 * space sometimes returns EFAULT. I have
889 * no idea why this is the case given that
890 * a kernel debugger shows the user buffer
891 * is correct. This problem appears to
892 * be mostly mitigated by the memset of
893 * the buffer before it is first used.
894 * Very strange.... Shaun Clowes
896 * In any case this means that we shouldn't
897 * treat EFAULT as a fatal error; as we
898 * don't have an API for returning
899 * a "some packets were dropped since
900 * the last packet you saw" indication,
901 * we just ignore EFAULT and keep reading.
911 * The device on which we're capturing
914 * XXX - we should really return
915 * PCAP_ERROR_IFACE_NOT_UP, but
916 * pcap_dispatch() etc. aren't
917 * defined to retur that.
919 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
920 "The interface went down");
923 #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
925 * Due to a SunOS bug, after 2^31 bytes, the kernel
926 * file offset overflows and read fails with EINVAL.
927 * The lseek() to 0 will fix things.
930 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
932 (void)lseek(p
->fd
, 0L, SEEK_SET
);
938 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
939 pcap_strerror(errno
));
947 * Loop through each packet.
949 #define bhp ((struct bpf_hdr *)bp)
955 register int caplen
, hdrlen
;
958 * Has "pcap_breakloop()" been called?
959 * If so, return immediately - if we haven't read any
960 * packets, clear the flag and return PCAP_ERROR_BREAK
961 * to indicate that we were told to break out of the loop,
962 * otherwise leave the flag set, so that the *next* call
963 * will break out of the loop without having read any
964 * packets, and return the number of packets we've
970 return (PCAP_ERROR_BREAK
);
978 caplen
= bhp
->bh_caplen
;
979 hdrlen
= bhp
->bh_hdrlen
;
982 * Short-circuit evaluation: if using BPF filter
983 * in kernel, no need to do it now - we already know
984 * the packet passed the filter.
987 * Note: the filter code was generated assuming
988 * that p->fddipad was the amount of padding
989 * before the header, as that's what's required
990 * in the kernel, so we run the filter before
991 * skipping that padding.
995 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
996 struct pcap_pkthdr pkthdr
;
998 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
1001 * AIX's BPF returns seconds/nanoseconds time
1002 * stamps, not seconds/microseconds time stamps.
1004 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
1006 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
1010 pkthdr
.caplen
= caplen
- pad
;
1013 if (bhp
->bh_datalen
> pad
)
1014 pkthdr
.len
= bhp
->bh_datalen
- pad
;
1019 pkthdr
.caplen
= caplen
;
1020 pkthdr
.len
= bhp
->bh_datalen
;
1022 (*callback
)(user
, &pkthdr
, datap
);
1023 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1024 if (++n
>= cnt
&& cnt
> 0) {
1033 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1042 pcap_inject_bpf(pcap_t
*p
, const void *buf
, size_t size
)
1046 ret
= write(p
->fd
, buf
, size
);
1048 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
1050 * In Mac OS X, there's a bug wherein setting the
1051 * BIOCSHDRCMPLT flag causes writes to fail; see,
1054 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
1056 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
1057 * assume it's due to that bug, and turn off that flag
1058 * and try again. If we succeed, it either means that
1059 * somebody applied the fix from that URL, or other patches
1062 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
1064 * and are running a Darwin kernel with those fixes, or
1065 * that Apple fixed the problem in some OS X release.
1067 u_int spoof_eth_src
= 0;
1069 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1070 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1071 "send: can't turn off BIOCSHDRCMPLT: %s",
1072 pcap_strerror(errno
));
1073 return (PCAP_ERROR
);
1077 * Now try the write again.
1079 ret
= write(p
->fd
, buf
, size
);
1081 #endif /* __APPLE__ */
1083 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
1084 pcap_strerror(errno
));
1085 return (PCAP_ERROR
);
1092 bpf_odminit(char *errbuf
)
1096 if (odm_initialize() == -1) {
1097 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1098 errstr
= "Unknown error";
1099 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1100 "bpf_load: odm_initialize failed: %s",
1102 return (PCAP_ERROR
);
1105 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
1106 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1107 errstr
= "Unknown error";
1108 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1109 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
1111 (void)odm_terminate();
1112 return (PCAP_ERROR
);
1119 bpf_odmcleanup(char *errbuf
)
1123 if (odm_unlock(odmlockid
) == -1) {
1124 if (errbuf
!= NULL
) {
1125 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1126 errstr
= "Unknown error";
1127 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1128 "bpf_load: odm_unlock failed: %s",
1131 return (PCAP_ERROR
);
1134 if (odm_terminate() == -1) {
1135 if (errbuf
!= NULL
) {
1136 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1137 errstr
= "Unknown error";
1138 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1139 "bpf_load: odm_terminate failed: %s",
1142 return (PCAP_ERROR
);
1149 bpf_load(char *errbuf
)
1153 int numminors
, i
, rc
;
1156 struct bpf_config cfg_bpf
;
1157 struct cfg_load cfg_ld
;
1158 struct cfg_kmod cfg_km
;
1161 * This is very very close to what happens in the real implementation
1162 * but I've fixed some (unlikely) bug situations.
1167 if (bpf_odminit(errbuf
) == PCAP_ERROR
)
1168 return (PCAP_ERROR
);
1170 major
= genmajor(BPF_NAME
);
1172 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1173 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
1174 (void)bpf_odmcleanup(NULL
);
1175 return (PCAP_ERROR
);
1178 minors
= getminor(major
, &numminors
, BPF_NAME
);
1180 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
1182 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1183 "bpf_load: genminor failed: %s",
1184 pcap_strerror(errno
));
1185 (void)bpf_odmcleanup(NULL
);
1186 return (PCAP_ERROR
);
1190 if (bpf_odmcleanup(errbuf
) == PCAP_ERROR
)
1191 return (PCAP_ERROR
);
1193 rc
= stat(BPF_NODE
"0", &sbuf
);
1194 if (rc
== -1 && errno
!= ENOENT
) {
1195 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1196 "bpf_load: can't stat %s: %s",
1197 BPF_NODE
"0", pcap_strerror(errno
));
1198 return (PCAP_ERROR
);
1201 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
1202 for (i
= 0; i
< BPF_MINORS
; i
++) {
1203 sprintf(buf
, "%s%d", BPF_NODE
, i
);
1205 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
1206 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1207 "bpf_load: can't mknod %s: %s",
1208 buf
, pcap_strerror(errno
));
1209 return (PCAP_ERROR
);
1214 /* Check if the driver is loaded */
1215 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
1217 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
1218 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
1219 (cfg_ld
.kmid
== 0)) {
1220 /* Driver isn't loaded, load it now */
1221 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
1222 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1223 "bpf_load: could not load driver: %s",
1225 return (PCAP_ERROR
);
1229 /* Configure the driver */
1230 cfg_km
.cmd
= CFG_INIT
;
1231 cfg_km
.kmid
= cfg_ld
.kmid
;
1232 cfg_km
.mdilen
= sizeof(cfg_bpf
);
1233 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
1234 for (i
= 0; i
< BPF_MINORS
; i
++) {
1235 cfg_bpf
.devno
= domakedev(major
, i
);
1236 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
1237 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1238 "bpf_load: could not configure driver: %s",
1240 return (PCAP_ERROR
);
1251 * Turn off rfmon mode if necessary.
1254 pcap_cleanup_bpf(pcap_t
*p
)
1256 #ifdef HAVE_BSD_IEEE80211
1258 struct ifmediareq req
;
1262 if (p
->md
.must_do_on_close
!= 0) {
1264 * There's something we have to do when closing this
1267 #ifdef HAVE_BSD_IEEE80211
1268 if (p
->md
.must_do_on_close
& MUST_CLEAR_RFMON
) {
1270 * We put the interface into rfmon mode;
1271 * take it out of rfmon mode.
1273 * XXX - if somebody else wants it in rfmon
1274 * mode, this code cannot know that, so it'll take
1275 * it out of rfmon mode.
1277 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
1280 "Can't restore interface flags (socket() failed: %s).\n"
1281 "Please adjust manually.\n",
1284 memset(&req
, 0, sizeof(req
));
1285 strncpy(req
.ifm_name
, p
->md
.device
,
1286 sizeof(req
.ifm_name
));
1287 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1289 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
1290 "Please adjust manually.\n",
1293 if (req
.ifm_current
& IFM_IEEE80211_MONITOR
) {
1295 * Rfmon mode is currently on;
1298 memset(&ifr
, 0, sizeof(ifr
));
1299 (void)strncpy(ifr
.ifr_name
,
1301 sizeof(ifr
.ifr_name
));
1303 req
.ifm_current
& ~IFM_IEEE80211_MONITOR
;
1304 if (ioctl(sock
, SIOCSIFMEDIA
,
1307 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
1308 "Please adjust manually.\n",
1316 #endif /* HAVE_BSD_IEEE80211 */
1319 * Take this pcap out of the list of pcaps for which we
1320 * have to take the interface out of some mode.
1322 pcap_remove_from_pcaps_to_close(p
);
1323 p
->md
.must_do_on_close
= 0;
1326 #ifdef HAVE_ZEROCOPY_BPF
1328 * In zero-copy mode, p->buffer is just a pointer into one of the two
1329 * memory-mapped buffers, so no need to free it.
1331 if (p
->md
.zerocopy
) {
1332 if (p
->md
.zbuf1
!= MAP_FAILED
&& p
->md
.zbuf1
!= NULL
)
1333 munmap(p
->md
.zbuf1
, p
->md
.zbufsize
);
1334 if (p
->md
.zbuf2
!= MAP_FAILED
&& p
->md
.zbuf2
!= NULL
)
1335 munmap(p
->md
.zbuf2
, p
->md
.zbufsize
);
1338 if (p
->md
.device
!= NULL
) {
1340 p
->md
.device
= NULL
;
1342 pcap_cleanup_live_common(p
);
1346 check_setif_failure(pcap_t
*p
, int error
)
1354 if (error
== ENXIO
) {
1356 * No such device exists.
1359 if (p
->opt
.rfmon
&& strncmp(p
->opt
.source
, "wlt", 3) == 0) {
1361 * Monitor mode was requested, and we're trying
1362 * to open a "wltN" device. Assume that this
1363 * is 10.4 and that we were asked to open an
1364 * "enN" device; if that device exists, return
1365 * "monitor mode not supported on the device".
1367 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1369 strlcpy(ifr
.ifr_name
, "en",
1370 sizeof(ifr
.ifr_name
));
1371 strlcat(ifr
.ifr_name
, p
->opt
.source
+ 3,
1372 sizeof(ifr
.ifr_name
));
1373 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
1375 * We assume this failed because
1376 * the underlying device doesn't
1379 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1380 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1381 "SIOCGIFFLAGS on %s failed: %s",
1382 ifr
.ifr_name
, pcap_strerror(errno
));
1385 * The underlying "enN" device
1386 * exists, but there's no
1387 * corresponding "wltN" device;
1388 * that means that the "enN"
1389 * device doesn't support
1390 * monitor mode, probably because
1391 * it's an Ethernet device rather
1392 * than a wireless device.
1394 err
= PCAP_ERROR_RFMON_NOTSUP
;
1399 * We can't find out whether there's
1400 * an underlying "enN" device, so
1401 * just report "no such device".
1403 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1404 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1405 "socket() failed: %s",
1406 pcap_strerror(errno
));
1414 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF failed: %s",
1415 pcap_strerror(errno
));
1416 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1417 } else if (errno
== ENETDOWN
) {
1419 * Return a "network down" indication, so that
1420 * the application can report that rather than
1421 * saying we had a mysterious failure and
1422 * suggest that they report a problem to the
1423 * libpcap developers.
1425 return (PCAP_ERROR_IFACE_NOT_UP
);
1428 * Some other error; fill in the error string, and
1429 * return PCAP_ERROR.
1431 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1432 p
->opt
.source
, pcap_strerror(errno
));
1433 return (PCAP_ERROR
);
1438 * Default capture buffer size.
1439 * 32K isn't very much for modern machines with fast networks; we
1440 * pick .5M, as that's the maximum on at least some systems with BPF.
1442 #define DEFAULT_BUFSIZE 524288
1445 pcap_activate_bpf(pcap_t
*p
)
1450 struct bpf_version bv
;
1453 char *wltdev
= NULL
;
1456 struct bpf_dltlist bdl
;
1457 #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1460 #endif /* BIOCGDLTLIST */
1461 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1462 u_int spoof_eth_src
= 1;
1465 struct bpf_insn total_insn
;
1466 struct bpf_program total_prog
;
1467 struct utsname osinfo
;
1468 int have_osinfo
= 0;
1469 #ifdef HAVE_ZEROCOPY_BPF
1471 u_int bufmode
, zbufmax
;
1482 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
1483 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
1484 pcap_strerror(errno
));
1485 status
= PCAP_ERROR
;
1488 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
1489 bv
.bv_minor
< BPF_MINOR_VERSION
) {
1490 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1491 "kernel bpf filter out of date");
1492 status
= PCAP_ERROR
;
1496 p
->md
.device
= strdup(p
->opt
.source
);
1497 if (p
->md
.device
== NULL
) {
1498 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
1499 pcap_strerror(errno
));
1500 status
= PCAP_ERROR
;
1505 * Attempt to find out the version of the OS on which we're running.
1507 if (uname(&osinfo
) == 0)
1512 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1513 * of why we check the version number.
1518 * We assume osinfo.sysname is "Darwin", because
1519 * __APPLE__ is defined. We just check the version.
1521 if (osinfo
.release
[0] < '8' &&
1522 osinfo
.release
[1] == '.') {
1524 * 10.3 (Darwin 7.x) or earlier.
1526 status
= PCAP_ERROR_RFMON_NOTSUP
;
1529 if (osinfo
.release
[0] == '8' &&
1530 osinfo
.release
[1] == '.') {
1532 * 10.4 (Darwin 8.x). s/en/wlt/
1534 if (strncmp(p
->opt
.source
, "en", 2) != 0) {
1536 * Not an enN device; check
1537 * whether the device even exists.
1539 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1541 strlcpy(ifr
.ifr_name
,
1543 sizeof(ifr
.ifr_name
));
1544 if (ioctl(sockfd
, SIOCGIFFLAGS
,
1545 (char *)&ifr
) < 0) {
1553 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1556 "SIOCGIFFLAGS failed: %s",
1557 pcap_strerror(errno
));
1559 status
= PCAP_ERROR_RFMON_NOTSUP
;
1563 * We can't find out whether
1564 * the device exists, so just
1565 * report "no such device".
1567 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1570 "socket() failed: %s",
1571 pcap_strerror(errno
));
1575 wltdev
= malloc(strlen(p
->opt
.source
) + 2);
1576 if (wltdev
== NULL
) {
1577 (void)snprintf(p
->errbuf
,
1578 PCAP_ERRBUF_SIZE
, "malloc: %s",
1579 pcap_strerror(errno
));
1580 status
= PCAP_ERROR
;
1583 strcpy(wltdev
, "wlt");
1584 strcat(wltdev
, p
->opt
.source
+ 2);
1585 free(p
->opt
.source
);
1586 p
->opt
.source
= wltdev
;
1589 * Everything else is 10.5 or later; for those,
1590 * we just open the enN device, and set the DLT.
1594 #endif /* __APPLE__ */
1595 #ifdef HAVE_ZEROCOPY_BPF
1597 * If the BPF extension to set buffer mode is present, try setting
1598 * the mode to zero-copy. If that fails, use regular buffering. If
1599 * it succeeds but other setup fails, return an error to the user.
1601 bufmode
= BPF_BUFMODE_ZBUF
;
1602 if (ioctl(fd
, BIOCSETBUFMODE
, (caddr_t
)&bufmode
) == 0) {
1604 * We have zerocopy BPF; use it.
1609 * Set the cleanup and set/get nonblocking mode ops
1610 * as appropriate for zero-copy mode.
1612 p
->cleanup_op
= pcap_cleanup_zbuf
;
1613 p
->setnonblock_op
= pcap_setnonblock_zbuf
;
1614 p
->getnonblock_op
= pcap_getnonblock_zbuf
;
1617 * How to pick a buffer size: first, query the maximum buffer
1618 * size supported by zero-copy. This also lets us quickly
1619 * determine whether the kernel generally supports zero-copy.
1620 * Then, if a buffer size was specified, use that, otherwise
1621 * query the default buffer size, which reflects kernel
1622 * policy for a desired default. Round to the nearest page
1625 if (ioctl(fd
, BIOCGETZMAX
, (caddr_t
)&zbufmax
) < 0) {
1626 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGETZMAX: %s",
1627 pcap_strerror(errno
));
1631 if (p
->opt
.buffer_size
!= 0) {
1633 * A buffer size was explicitly specified; use it.
1635 v
= p
->opt
.buffer_size
;
1637 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1638 v
< DEFAULT_BUFSIZE
)
1639 v
= DEFAULT_BUFSIZE
;
1642 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
1644 p
->md
.zbufsize
= roundup(v
, getpagesize());
1645 if (p
->md
.zbufsize
> zbufmax
)
1646 p
->md
.zbufsize
= zbufmax
;
1647 p
->md
.zbuf1
= mmap(NULL
, p
->md
.zbufsize
, PROT_READ
| PROT_WRITE
,
1649 p
->md
.zbuf2
= mmap(NULL
, p
->md
.zbufsize
, PROT_READ
| PROT_WRITE
,
1651 if (p
->md
.zbuf1
== MAP_FAILED
|| p
->md
.zbuf2
== MAP_FAILED
) {
1652 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "mmap: %s",
1653 pcap_strerror(errno
));
1656 bzero(&bz
, sizeof(bz
));
1657 bz
.bz_bufa
= p
->md
.zbuf1
;
1658 bz
.bz_bufb
= p
->md
.zbuf2
;
1659 bz
.bz_buflen
= p
->md
.zbufsize
;
1660 if (ioctl(fd
, BIOCSETZBUF
, (caddr_t
)&bz
) < 0) {
1661 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETZBUF: %s",
1662 pcap_strerror(errno
));
1665 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
, sizeof(ifr
.ifr_name
));
1666 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
1667 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1668 p
->opt
.source
, pcap_strerror(errno
));
1671 v
= p
->md
.zbufsize
- sizeof(struct bpf_zbuf_header
);
1676 * We don't have zerocopy BPF.
1677 * Set the buffer size.
1679 if (p
->opt
.buffer_size
!= 0) {
1681 * A buffer size was explicitly specified; use it.
1683 if (ioctl(fd
, BIOCSBLEN
,
1684 (caddr_t
)&p
->opt
.buffer_size
) < 0) {
1685 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1686 "BIOCSBLEN: %s: %s", p
->opt
.source
,
1687 pcap_strerror(errno
));
1688 status
= PCAP_ERROR
;
1693 * Now bind to the device.
1695 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
1696 sizeof(ifr
.ifr_name
));
1697 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
1698 status
= check_setif_failure(p
, errno
);
1703 * No buffer size was explicitly specified.
1705 * Try finding a good size for the buffer;
1706 * DEFAULT_BUFSIZE may be too big, so keep
1707 * cutting it in half until we find a size
1708 * that works, or run out of sizes to try.
1709 * If the default is larger, don't make it smaller.
1711 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1712 v
< DEFAULT_BUFSIZE
)
1713 v
= DEFAULT_BUFSIZE
;
1714 for ( ; v
!= 0; v
>>= 1) {
1716 * Ignore the return value - this is because the
1717 * call fails on BPF systems that don't have
1718 * kernel malloc. And if the call fails, it's
1719 * no big deal, we just continue to use the
1720 * standard buffer size.
1722 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
1724 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
1725 sizeof(ifr
.ifr_name
));
1726 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
1727 break; /* that size worked; we're done */
1729 if (errno
!= ENOBUFS
) {
1730 status
= check_setif_failure(p
, errno
);
1736 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1737 "BIOCSBLEN: %s: No buffer size worked",
1739 status
= PCAP_ERROR
;
1745 /* Get the data link layer type. */
1746 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
1747 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
1748 pcap_strerror(errno
));
1749 status
= PCAP_ERROR
;
1755 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
1778 * We don't know what to map this to yet.
1780 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
1782 status
= PCAP_ERROR
;
1786 #if _BSDI_VERSION - 0 >= 199510
1787 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
1802 case 12: /*DLT_C_HDLC*/
1810 * We know the default link type -- now determine all the DLTs
1811 * this interface supports. If this fails with EINVAL, it's
1812 * not fatal; we just don't get to use the feature later.
1814 if (get_dlt_list(fd
, v
, &bdl
, p
->errbuf
) == -1) {
1815 status
= PCAP_ERROR
;
1818 p
->dlt_count
= bdl
.bfl_len
;
1819 p
->dlt_list
= bdl
.bfl_list
;
1823 * Monitor mode fun, continued.
1825 * For 10.5 and, we're assuming, later releases, as noted above,
1826 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
1827 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
1828 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
1831 * Therefore, if the user asked for monitor mode, we filter out
1832 * the DLT_EN10MB value, as you can't get that in monitor mode,
1833 * and, if the user didn't ask for monitor mode, we filter out
1834 * the 802.11 DLT_ values, because selecting those will turn
1835 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
1836 * radio DLT_ value is offered, we try to select that, otherwise
1837 * we try to select DLT_IEEE802_11.
1840 if (isdigit((unsigned)osinfo
.release
[0]) &&
1841 (osinfo
.release
[0] == '9' ||
1842 isdigit((unsigned)osinfo
.release
[1]))) {
1844 * 10.5 (Darwin 9.x), or later.
1846 new_dlt
= find_802_11(&bdl
);
1847 if (new_dlt
!= -1) {
1849 * We have at least one 802.11 DLT_ value,
1850 * so this is an 802.11 interface.
1851 * new_dlt is the best of the 802.11
1852 * DLT_ values in the list.
1856 * Our caller wants monitor mode.
1857 * Purge DLT_EN10MB from the list
1858 * of link-layer types, as selecting
1859 * it will keep monitor mode off.
1864 * If the new mode we want isn't
1865 * the default mode, attempt to
1866 * select the new mode.
1869 if (ioctl(p
->fd
, BIOCSDLT
,
1881 * Our caller doesn't want
1882 * monitor mode. Unless this
1883 * is being done by pcap_open_live(),
1884 * purge the 802.11 link-layer types
1885 * from the list, as selecting
1886 * one of them will turn monitor
1895 * The caller requested monitor
1896 * mode, but we have no 802.11
1897 * link-layer types, so they
1900 status
= PCAP_ERROR_RFMON_NOTSUP
;
1906 #elif defined(HAVE_BSD_IEEE80211)
1908 * *BSD with the new 802.11 ioctls.
1909 * Do we want monitor mode?
1913 * Try to put the interface into monitor mode.
1915 status
= monitor_mode(p
, 1);
1924 * We're in monitor mode.
1925 * Try to find the best 802.11 DLT_ value and, if we
1926 * succeed, try to switch to that mode if we're not
1927 * already in that mode.
1929 new_dlt
= find_802_11(&bdl
);
1930 if (new_dlt
!= -1) {
1932 * We have at least one 802.11 DLT_ value.
1933 * new_dlt is the best of the 802.11
1934 * DLT_ values in the list.
1936 * If the new mode we want isn't the default mode,
1937 * attempt to select the new mode.
1940 if (ioctl(p
->fd
, BIOCSDLT
, &new_dlt
) != -1) {
1942 * We succeeded; make this the
1950 #endif /* various platforms */
1951 #endif /* BIOCGDLTLIST */
1954 * If this is an Ethernet device, and we don't have a DLT_ list,
1955 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
1956 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
1957 * do, but there's not much we can do about that without finding
1958 * some other way of determining whether it's an Ethernet or 802.11
1961 if (v
== DLT_EN10MB
&& p
->dlt_count
== 0) {
1962 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
1964 * If that fails, just leave the list empty.
1966 if (p
->dlt_list
!= NULL
) {
1967 p
->dlt_list
[0] = DLT_EN10MB
;
1968 p
->dlt_list
[1] = DLT_DOCSIS
;
1974 p
->fddipad
= PCAP_FDDIPAD
;
1980 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1982 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
1983 * the link-layer source address isn't forcibly overwritten.
1984 * (Should we ignore errors? Should we do this only if
1985 * we're open for writing?)
1987 * XXX - I seem to remember some packet-sending bug in some
1988 * BSDs - check CVS log for "bpf.c"?
1990 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1991 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1992 "BIOCSHDRCMPLT: %s", pcap_strerror(errno
));
1993 status
= PCAP_ERROR
;
1998 #ifdef HAVE_ZEROCOPY_BPF
1999 if (p
->md
.timeout
!= 0 && !p
->md
.zerocopy
) {
2001 if (p
->md
.timeout
) {
2004 * XXX - is this seconds/nanoseconds in AIX?
2005 * (Treating it as such doesn't fix the timeout
2006 * problem described below.)
2008 * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
2009 * 64-bit userland - it takes, as an argument, a
2010 * "struct BPF_TIMEVAL", which has 32-bit tv_sec
2011 * and tv_usec, rather than a "struct timeval".
2013 * If this platform defines "struct BPF_TIMEVAL",
2014 * we check whether the structure size in BIOCSRTIMEOUT
2015 * is that of a "struct timeval" and, if not, we use
2016 * a "struct BPF_TIMEVAL" rather than a "struct timeval".
2017 * (That way, if the bug is fixed in a future release,
2018 * we will still do the right thing.)
2021 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2022 struct BPF_TIMEVAL bpf_to
;
2024 if (IOCPARM_LEN(BIOCSRTIMEOUT
) != sizeof(struct timeval
)) {
2025 bpf_to
.tv_sec
= p
->md
.timeout
/ 1000;
2026 bpf_to
.tv_usec
= (p
->md
.timeout
* 1000) % 1000000;
2027 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&bpf_to
) < 0) {
2028 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2029 "BIOCSRTIMEOUT: %s", pcap_strerror(errno
));
2030 status
= PCAP_ERROR
;
2035 to
.tv_sec
= p
->md
.timeout
/ 1000;
2036 to
.tv_usec
= (p
->md
.timeout
* 1000) % 1000000;
2037 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
2038 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2039 "BIOCSRTIMEOUT: %s", pcap_strerror(errno
));
2040 status
= PCAP_ERROR
;
2043 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2049 #ifdef BIOCIMMEDIATE
2051 * Darren Reed notes that
2053 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
2054 * timeout appears to be ignored and it waits until the buffer
2055 * is filled before returning. The result of not having it
2056 * set is almost worse than useless if your BPF filter
2057 * is reducing things to only a few packets (i.e. one every
2060 * so we turn BIOCIMMEDIATE mode on if this is AIX.
2062 * We don't turn it on for other platforms, as that means we
2063 * get woken up for every packet, which may not be what we want;
2064 * in the Winter 1993 USENIX paper on BPF, they say:
2066 * Since a process might want to look at every packet on a
2067 * network and the time between packets can be only a few
2068 * microseconds, it is not possible to do a read system call
2069 * per packet and BPF must collect the data from several
2070 * packets and return it as a unit when the monitoring
2071 * application does a read.
2073 * which I infer is the reason for the timeout - it means we
2074 * wait that amount of time, in the hopes that more packets
2075 * will arrive and we'll get them all with one read.
2077 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
2078 * BSDs) causes the timeout to be ignored.
2080 * On the other hand, some platforms (e.g., Linux) don't support
2081 * timeouts, they just hand stuff to you as soon as it arrives;
2082 * if that doesn't cause a problem on those platforms, it may
2083 * be OK to have BIOCIMMEDIATE mode on BSD as well.
2085 * (Note, though, that applications may depend on the read
2086 * completing, even if no packets have arrived, when the timeout
2087 * expires, e.g. GUI applications that have to check for input
2088 * while waiting for packets to arrive; a non-zero timeout
2089 * prevents "select()" from working right on FreeBSD and
2090 * possibly other BSDs, as the timer doesn't start until a
2091 * "read()" is done, so the timer isn't in effect if the
2092 * application is blocked on a "select()", and the "select()"
2093 * doesn't get woken up for a BPF device until the buffer
2097 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
2098 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCIMMEDIATE: %s",
2099 pcap_strerror(errno
));
2100 status
= PCAP_ERROR
;
2103 #endif /* BIOCIMMEDIATE */
2106 if (p
->opt
.promisc
) {
2107 /* set promiscuous mode, just warn if it fails */
2108 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
2109 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
2110 pcap_strerror(errno
));
2111 status
= PCAP_WARNING_PROMISC_NOTSUP
;
2115 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
2116 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
2117 pcap_strerror(errno
));
2118 status
= PCAP_ERROR
;
2122 #ifdef HAVE_ZEROCOPY_BPF
2123 if (!p
->md
.zerocopy
) {
2125 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
2126 if (p
->buffer
== NULL
) {
2127 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
2128 pcap_strerror(errno
));
2129 status
= PCAP_ERROR
;
2133 /* For some strange reason this seems to prevent the EFAULT
2134 * problems we have experienced from AIX BPF. */
2135 memset(p
->buffer
, 0x0, p
->bufsize
);
2137 #ifdef HAVE_ZEROCOPY_BPF
2142 * If there's no filter program installed, there's
2143 * no indication to the kernel of what the snapshot
2144 * length should be, so no snapshotting is done.
2146 * Therefore, when we open the device, we install
2147 * an "accept everything" filter with the specified
2150 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
2153 total_insn
.k
= p
->snapshot
;
2155 total_prog
.bf_len
= 1;
2156 total_prog
.bf_insns
= &total_insn
;
2157 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
2158 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2159 pcap_strerror(errno
));
2160 status
= PCAP_ERROR
;
2165 * On most BPF platforms, either you can do a "select()" or
2166 * "poll()" on a BPF file descriptor and it works correctly,
2167 * or you can do it and it will return "readable" if the
2168 * hold buffer is full but not if the timeout expires *and*
2169 * a non-blocking read will, if the hold buffer is empty
2170 * but the store buffer isn't empty, rotate the buffers
2171 * and return what packets are available.
2173 * In the latter case, the fact that a non-blocking read
2174 * will give you the available packets means you can work
2175 * around the failure of "select()" and "poll()" to wake up
2176 * and return "readable" when the timeout expires by using
2177 * the timeout as the "select()" or "poll()" timeout, putting
2178 * the BPF descriptor into non-blocking mode, and read from
2179 * it regardless of whether "select()" reports it as readable
2182 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
2183 * won't wake up and return "readable" if the timer expires
2184 * and non-blocking reads return EWOULDBLOCK if the hold
2185 * buffer is empty, even if the store buffer is non-empty.
2187 * This means the workaround in question won't work.
2189 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2190 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2191 * here". On all other BPF platforms, we set it to the FD for
2192 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2193 * read will, if the hold buffer is empty and the store buffer
2194 * isn't empty, rotate the buffers and return what packets are
2195 * there (and in sufficiently recent versions of OpenBSD
2196 * "select()" and "poll()" should work correctly).
2198 * XXX - what about AIX?
2200 p
->selectable_fd
= p
->fd
; /* assume select() works until we know otherwise */
2203 * We can check what OS this is.
2205 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0) {
2206 if (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
2207 strncmp(osinfo
.release
, "4.4-", 4) == 0)
2208 p
->selectable_fd
= -1;
2212 p
->read_op
= pcap_read_bpf
;
2213 p
->inject_op
= pcap_inject_bpf
;
2214 p
->setfilter_op
= pcap_setfilter_bpf
;
2215 p
->setdirection_op
= pcap_setdirection_bpf
;
2216 p
->set_datalink_op
= pcap_set_datalink_bpf
;
2217 p
->getnonblock_op
= pcap_getnonblock_fd
;
2218 p
->setnonblock_op
= pcap_setnonblock_fd
;
2219 p
->stats_op
= pcap_stats_bpf
;
2220 p
->cleanup_op
= pcap_cleanup_bpf
;
2224 pcap_cleanup_bpf(p
);
2229 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
2232 if (dag_platform_finddevs(alldevsp
, errbuf
) < 0)
2234 #endif /* HAVE_DAG_API */
2236 if (snf_platform_finddevs(alldevsp
, errbuf
) < 0)
2238 #endif /* HAVE_SNF_API */
2243 #ifdef HAVE_BSD_IEEE80211
2245 monitor_mode(pcap_t
*p
, int set
)
2248 struct ifmediareq req
;
2254 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
2256 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "can't open socket: %s",
2257 pcap_strerror(errno
));
2258 return (PCAP_ERROR
);
2261 memset(&req
, 0, sizeof req
);
2262 strncpy(req
.ifm_name
, p
->opt
.source
, sizeof req
.ifm_name
);
2265 * Find out how many media types we have.
2267 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2269 * Can't get the media types.
2275 * There's no such device.
2278 return (PCAP_ERROR_NO_SUCH_DEVICE
);
2282 * Interface doesn't support SIOC{G,S}IFMEDIA.
2285 return (PCAP_ERROR_RFMON_NOTSUP
);
2288 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2289 "SIOCGIFMEDIA 1: %s", pcap_strerror(errno
));
2291 return (PCAP_ERROR
);
2294 if (req
.ifm_count
== 0) {
2299 return (PCAP_ERROR_RFMON_NOTSUP
);
2303 * Allocate a buffer to hold all the media types, and
2304 * get the media types.
2306 media_list
= malloc(req
.ifm_count
* sizeof(int));
2307 if (media_list
== NULL
) {
2308 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
2309 pcap_strerror(errno
));
2311 return (PCAP_ERROR
);
2313 req
.ifm_ulist
= media_list
;
2314 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2315 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCGIFMEDIA: %s",
2316 pcap_strerror(errno
));
2319 return (PCAP_ERROR
);
2323 * Look for an 802.11 "automatic" media type.
2324 * We assume that all 802.11 adapters have that media type,
2325 * and that it will carry the monitor mode supported flag.
2328 for (i
= 0; i
< req
.ifm_count
; i
++) {
2329 if (IFM_TYPE(media_list
[i
]) == IFM_IEEE80211
2330 && IFM_SUBTYPE(media_list
[i
]) == IFM_AUTO
) {
2331 /* OK, does it do monitor mode? */
2332 if (media_list
[i
] & IFM_IEEE80211_MONITOR
) {
2341 * This adapter doesn't support monitor mode.
2344 return (PCAP_ERROR_RFMON_NOTSUP
);
2349 * Don't just check whether we can enable monitor mode,
2350 * do so, if it's not already enabled.
2352 if ((req
.ifm_current
& IFM_IEEE80211_MONITOR
) == 0) {
2354 * Monitor mode isn't currently on, so turn it on,
2355 * and remember that we should turn it off when the
2360 * If we haven't already done so, arrange to have
2361 * "pcap_close_all()" called when we exit.
2363 if (!pcap_do_addexit(p
)) {
2365 * "atexit()" failed; don't put the interface
2366 * in monitor mode, just give up.
2368 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2371 return (PCAP_ERROR
);
2373 memset(&ifr
, 0, sizeof(ifr
));
2374 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
2375 sizeof(ifr
.ifr_name
));
2376 ifr
.ifr_media
= req
.ifm_current
| IFM_IEEE80211_MONITOR
;
2377 if (ioctl(sock
, SIOCSIFMEDIA
, &ifr
) == -1) {
2378 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2379 "SIOCSIFMEDIA: %s", pcap_strerror(errno
));
2381 return (PCAP_ERROR
);
2384 p
->md
.must_do_on_close
|= MUST_CLEAR_RFMON
;
2387 * Add this to the list of pcaps to close when we exit.
2389 pcap_add_to_pcaps_to_close(p
);
2394 #endif /* HAVE_BSD_IEEE80211 */
2396 #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
2398 * Check whether we have any 802.11 link-layer types; return the best
2399 * of the 802.11 link-layer types if we find one, and return -1
2402 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
2403 * best 802.11 link-layer type; any of the other 802.11-plus-radio
2404 * headers are second-best; 802.11 with no radio information is
2408 find_802_11(struct bpf_dltlist
*bdlp
)
2414 * Scan the list of DLT_ values, looking for 802.11 values,
2415 * and, if we find any, choose the best of them.
2418 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
2419 switch (bdlp
->bfl_list
[i
]) {
2421 case DLT_IEEE802_11
:
2423 * 802.11, but no radio.
2425 * Offer this, and select it as the new mode
2426 * unless we've already found an 802.11
2427 * header with radio information.
2430 new_dlt
= bdlp
->bfl_list
[i
];
2433 case DLT_PRISM_HEADER
:
2434 case DLT_AIRONET_HEADER
:
2435 case DLT_IEEE802_11_RADIO_AVS
:
2437 * 802.11 with radio, but not radiotap.
2439 * Offer this, and select it as the new mode
2440 * unless we've already found the radiotap DLT_.
2442 if (new_dlt
!= DLT_IEEE802_11_RADIO
)
2443 new_dlt
= bdlp
->bfl_list
[i
];
2446 case DLT_IEEE802_11_RADIO
:
2448 * 802.11 with radiotap.
2450 * Offer this, and select it as the new mode.
2452 new_dlt
= bdlp
->bfl_list
[i
];
2465 #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
2467 #if defined(__APPLE__) && defined(BIOCGDLTLIST)
2469 * Remove DLT_EN10MB from the list of DLT_ values, as we're in monitor mode,
2470 * and DLT_EN10MB isn't supported in monitor mode.
2473 remove_en(pcap_t
*p
)
2478 * Scan the list of DLT_ values and discard DLT_EN10MB.
2481 for (i
= 0; i
< p
->dlt_count
; i
++) {
2482 switch (p
->dlt_list
[i
]) {
2486 * Don't offer this one.
2492 * Just copy this mode over.
2498 * Copy this DLT_ value to its new position.
2500 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2505 * Set the DLT_ count to the number of entries we copied.
2511 * Remove 802.11 link-layer types from the list of DLT_ values, as
2512 * we're not in monitor mode, and those DLT_ values will switch us
2516 remove_802_11(pcap_t
*p
)
2521 * Scan the list of DLT_ values and discard 802.11 values.
2524 for (i
= 0; i
< p
->dlt_count
; i
++) {
2525 switch (p
->dlt_list
[i
]) {
2527 case DLT_IEEE802_11
:
2528 case DLT_PRISM_HEADER
:
2529 case DLT_AIRONET_HEADER
:
2530 case DLT_IEEE802_11_RADIO
:
2531 case DLT_IEEE802_11_RADIO_AVS
:
2533 * 802.11. Don't offer this one.
2539 * Just copy this mode over.
2545 * Copy this DLT_ value to its new position.
2547 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2552 * Set the DLT_ count to the number of entries we copied.
2556 #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
2559 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
2562 * Free any user-mode filter we might happen to have installed.
2564 pcap_freecode(&p
->fcode
);
2567 * Try to install the kernel filter.
2569 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) == 0) {
2573 p
->md
.use_bpf
= 1; /* filtering in the kernel */
2576 * Discard any previously-received packets, as they might
2577 * have passed whatever filter was formerly in effect, but
2578 * might not pass this filter (BIOCSETF discards packets
2579 * buffered in the kernel, so you can lose packets in any
2589 * If it failed with EINVAL, that's probably because the program
2590 * is invalid or too big. Validate it ourselves; if we like it
2591 * (we currently allow backward branches, to support protochain),
2592 * run it in userland. (There's no notion of "too big" for
2595 * Otherwise, just give up.
2596 * XXX - if the copy of the program into the kernel failed,
2597 * we will get EINVAL rather than, say, EFAULT on at least
2600 if (errno
!= EINVAL
) {
2601 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2602 pcap_strerror(errno
));
2607 * install_bpf_program() validates the program.
2609 * XXX - what if we already have a filter in the kernel?
2611 if (install_bpf_program(p
, fp
) < 0)
2613 p
->md
.use_bpf
= 0; /* filtering in userland */
2618 * Set direction flag: Which packets do we accept on a forwarding
2619 * single device? IN, OUT or both?
2622 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
2624 #if defined(BIOCSDIRECTION)
2627 direction
= (d
== PCAP_D_IN
) ? BPF_D_IN
:
2628 ((d
== PCAP_D_OUT
) ? BPF_D_OUT
: BPF_D_INOUT
);
2629 if (ioctl(p
->fd
, BIOCSDIRECTION
, &direction
) == -1) {
2630 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2631 "Cannot set direction to %s: %s",
2632 (d
== PCAP_D_IN
) ? "PCAP_D_IN" :
2633 ((d
== PCAP_D_OUT
) ? "PCAP_D_OUT" : "PCAP_D_INOUT"),
2638 #elif defined(BIOCSSEESENT)
2642 * We don't support PCAP_D_OUT.
2644 if (d
== PCAP_D_OUT
) {
2645 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2646 "Setting direction to PCAP_D_OUT is not supported on BPF");
2650 seesent
= (d
== PCAP_D_INOUT
);
2651 if (ioctl(p
->fd
, BIOCSSEESENT
, &seesent
) == -1) {
2652 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2653 "Cannot set direction to %s: %s",
2654 (d
== PCAP_D_INOUT
) ? "PCAP_D_INOUT" : "PCAP_D_IN",
2660 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2661 "This system doesn't support BIOCSSEESENT, so the direction can't be set");
2667 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
2670 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
2671 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2672 "Cannot set DLT %d: %s", dlt
, strerror(errno
));