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
34 #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"
125 #ifdef HAVE_OS_PROTO_H
126 #include "os-proto.h"
130 * Later versions of NetBSD stick padding in front of FDDI frames
131 * to align the IP header on a 4-byte boundary.
133 #if defined(__NetBSD__) && __NetBSD_Version__ > 106000000
134 #define PCAP_FDDIPAD 3
138 * Private data for capturing on BPF devices.
145 #ifdef HAVE_ZEROCOPY_BPF
147 * Zero-copy read buffer -- for zero-copy BPF. 'buffer' above will
148 * alternative between these two actual mmap'd buffers as required.
149 * As there is a header on the front size of the mmap'd buffer, only
150 * some of the buffer is exposed to libpcap as a whole via bufsize;
151 * zbufsize is the true size. zbuffer tracks the current zbuf
152 * assocated with buffer so that it can be used to decide which the
153 * next buffer to read will be.
155 u_char
*zbuf1
, *zbuf2
, *zbuffer
;
159 struct timespec firstsel
;
161 * If there's currently a buffer being actively processed, then it is
162 * referenced here; 'buffer' is also pointed at it, but offset by the
163 * size of the header.
165 struct bpf_zbuf_header
*bzh
;
166 int nonblock
; /* true if in nonblocking mode */
167 #endif /* HAVE_ZEROCOPY_BPF */
169 char *device
; /* device name */
170 int filtering_in_kernel
; /* using kernel filter */
171 int must_do_on_close
; /* stuff we must do when we close */
175 * Stuff to do when we close.
177 #define MUST_CLEAR_RFMON 0x00000001 /* clear rfmon (monitor) mode */
180 # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
181 #define HAVE_BSD_IEEE80211
184 # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
185 static int find_802_11(struct bpf_dltlist
*);
187 # ifdef HAVE_BSD_IEEE80211
188 static int monitor_mode(pcap_t
*, int);
191 # if defined(__APPLE__)
192 static void remove_en(pcap_t
*);
193 static void remove_802_11(pcap_t
*);
196 # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
198 #endif /* BIOCGDLTLIST */
200 #if defined(sun) && defined(LIFNAMSIZ) && defined(lifr_zoneid)
205 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
206 * don't get DLT_DOCSIS defined.
209 #define DLT_DOCSIS 143
213 * On OS X, we don't even get any of the 802.11-plus-radio-header DLT_'s
214 * defined, even though some of them are used by various Airport drivers.
216 #ifndef DLT_PRISM_HEADER
217 #define DLT_PRISM_HEADER 119
219 #ifndef DLT_AIRONET_HEADER
220 #define DLT_AIRONET_HEADER 120
222 #ifndef DLT_IEEE802_11_RADIO
223 #define DLT_IEEE802_11_RADIO 127
225 #ifndef DLT_IEEE802_11_RADIO_AVS
226 #define DLT_IEEE802_11_RADIO_AVS 163
229 static int pcap_can_set_rfmon_bpf(pcap_t
*p
);
230 static int pcap_activate_bpf(pcap_t
*p
);
231 static int pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
);
232 static int pcap_setdirection_bpf(pcap_t
*, pcap_direction_t
);
233 static int pcap_set_datalink_bpf(pcap_t
*p
, int dlt
);
236 * For zerocopy bpf, the setnonblock/getnonblock routines need to modify
237 * pb->nonblock so we don't call select(2) if the pcap handle is in non-
241 pcap_getnonblock_bpf(pcap_t
*p
, char *errbuf
)
243 #ifdef HAVE_ZEROCOPY_BPF
244 struct pcap_bpf
*pb
= p
->private;
247 return (pb
->nonblock
);
249 return (pcap_getnonblock_fd(p
, errbuf
));
253 pcap_setnonblock_bpf(pcap_t
*p
, int nonblock
, char *errbuf
)
255 #ifdef HAVE_ZEROCOPY_BPF
256 struct pcap_bpf
*pb
= p
->private;
259 pb
->nonblock
= nonblock
;
263 return (pcap_setnonblock_fd(p
, nonblock
, errbuf
));
266 #ifdef HAVE_ZEROCOPY_BPF
268 * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
269 * shared memory buffers.
271 * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer,
272 * and set up p->buffer and cc to reflect one if available. Notice that if
273 * there was no prior buffer, we select zbuf1 as this will be the first
274 * buffer filled for a fresh BPF session.
277 pcap_next_zbuf_shm(pcap_t
*p
, int *cc
)
279 struct pcap_bpf
*pb
= p
->private;
280 struct bpf_zbuf_header
*bzh
;
282 if (pb
->zbuffer
== pb
->zbuf2
|| pb
->zbuffer
== NULL
) {
283 bzh
= (struct bpf_zbuf_header
*)pb
->zbuf1
;
284 if (bzh
->bzh_user_gen
!=
285 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
287 pb
->zbuffer
= (u_char
*)pb
->zbuf1
;
288 p
->buffer
= pb
->zbuffer
+ sizeof(*bzh
);
289 *cc
= bzh
->bzh_kernel_len
;
292 } else if (pb
->zbuffer
== pb
->zbuf1
) {
293 bzh
= (struct bpf_zbuf_header
*)pb
->zbuf2
;
294 if (bzh
->bzh_user_gen
!=
295 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
297 pb
->zbuffer
= (u_char
*)pb
->zbuf2
;
298 p
->buffer
= pb
->zbuffer
+ sizeof(*bzh
);
299 *cc
= bzh
->bzh_kernel_len
;
308 * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
309 * select() for data or a timeout, and possibly force rotation of the buffer
310 * in the event we time out or are in immediate mode. Invoke the shared
311 * memory check before doing system calls in order to avoid doing avoidable
315 pcap_next_zbuf(pcap_t
*p
, int *cc
)
317 struct pcap_bpf
*pb
= p
->private;
325 #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000))
327 * Start out by seeing whether anything is waiting by checking the
328 * next shared memory buffer for data.
330 data
= pcap_next_zbuf_shm(p
, cc
);
334 * If a previous sleep was interrupted due to signal delivery, make
335 * sure that the timeout gets adjusted accordingly. This requires
336 * that we analyze when the timeout should be been expired, and
337 * subtract the current time from that. If after this operation,
338 * our timeout is less then or equal to zero, handle it like a
341 tmout
= p
->opt
.timeout
;
343 (void) clock_gettime(CLOCK_MONOTONIC
, &cur
);
344 if (pb
->interrupted
&& p
->opt
.timeout
) {
345 expire
= TSTOMILLI(&pb
->firstsel
) + p
->opt
.timeout
;
346 tmout
= expire
- TSTOMILLI(&cur
);
350 data
= pcap_next_zbuf_shm(p
, cc
);
353 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
354 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
355 "BIOCROTZBUF: %s", strerror(errno
));
358 return (pcap_next_zbuf_shm(p
, cc
));
362 * No data in the buffer, so must use select() to wait for data or
363 * the next timeout. Note that we only call select if the handle
364 * is in blocking mode.
368 FD_SET(p
->fd
, &r_set
);
370 tv
.tv_sec
= tmout
/ 1000;
371 tv
.tv_usec
= (tmout
* 1000) % 1000000;
373 r
= select(p
->fd
+ 1, &r_set
, NULL
, NULL
,
374 p
->opt
.timeout
!= 0 ? &tv
: NULL
);
375 if (r
< 0 && errno
== EINTR
) {
376 if (!pb
->interrupted
&& p
->opt
.timeout
) {
382 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
383 "select: %s", strerror(errno
));
389 * Check again for data, which may exist now that we've either been
390 * woken up as a result of data or timed out. Try the "there's data"
391 * case first since it doesn't require a system call.
393 data
= pcap_next_zbuf_shm(p
, cc
);
397 * Try forcing a buffer rotation to dislodge timed out or immediate
400 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
401 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
402 "BIOCROTZBUF: %s", strerror(errno
));
405 return (pcap_next_zbuf_shm(p
, cc
));
409 * Notify kernel that we are done with the buffer. We don't reset zbuffer so
410 * that we know which buffer to use next time around.
413 pcap_ack_zbuf(pcap_t
*p
)
415 struct pcap_bpf
*pb
= p
->private;
417 atomic_store_rel_int(&pb
->bzh
->bzh_user_gen
,
418 pb
->bzh
->bzh_kernel_gen
);
423 #endif /* HAVE_ZEROCOPY_BPF */
426 pcap_create_interface(const char *device
, char *ebuf
)
430 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_bpf
));
434 p
->activate_op
= pcap_activate_bpf
;
435 p
->can_set_rfmon_op
= pcap_can_set_rfmon_bpf
;
440 * On success, returns a file descriptor for a BPF device.
441 * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf.
447 #ifdef HAVE_CLONING_BPF
448 static const char device
[] = "/dev/bpf";
451 char device
[sizeof "/dev/bpf0000000000"];
456 * Load the bpf driver, if it isn't already loaded,
457 * and create the BPF device entries, if they don't
460 if (bpf_load(p
->errbuf
) == PCAP_ERROR
)
464 #ifdef HAVE_CLONING_BPF
465 if ((fd
= open(device
, O_RDWR
)) == -1 &&
466 (errno
!= EACCES
|| (fd
= open(device
, O_RDONLY
)) == -1)) {
468 fd
= PCAP_ERROR_PERM_DENIED
;
471 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
472 "(cannot open device) %s: %s", device
, pcap_strerror(errno
));
476 * Go through all the minors and find one that isn't in use.
479 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
481 * Initially try a read/write open (to allow the inject
482 * method to work). If that fails due to permission
483 * issues, fall back to read-only. This allows a
484 * non-root user to be granted specific access to pcap
485 * capabilities via file permissions.
487 * XXX - we should have an API that has a flag that
488 * controls whether to open read-only or read-write,
489 * so that denial of permission to send (or inability
490 * to send, if sending packets isn't supported on
491 * the device in question) can be indicated at open
494 fd
= open(device
, O_RDWR
);
495 if (fd
== -1 && errno
== EACCES
)
496 fd
= open(device
, O_RDONLY
);
497 } while (fd
< 0 && errno
== EBUSY
);
500 * XXX better message for all minors used
509 * /dev/bpf0 doesn't exist, which
510 * means we probably have no BPF
513 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
514 "(there are no BPF devices)");
517 * We got EBUSY on at least one
518 * BPF device, so we have BPF
519 * devices, but all the ones
520 * that exist are busy.
522 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
523 "(all BPF devices are busy)");
529 * Got EACCES on the last device we tried,
530 * and EBUSY on all devices before that,
533 fd
= PCAP_ERROR_PERM_DENIED
;
534 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
535 "(cannot open BPF device) %s: %s", device
,
536 pcap_strerror(errno
));
541 * Some other problem.
544 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
545 "(cannot open BPF device) %s: %s", device
,
546 pcap_strerror(errno
));
557 get_dlt_list(int fd
, int v
, struct bpf_dltlist
*bdlp
, char *ebuf
)
559 memset(bdlp
, 0, sizeof(*bdlp
));
560 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) == 0) {
564 bdlp
->bfl_list
= (u_int
*) malloc(sizeof(u_int
) * (bdlp
->bfl_len
+ 1));
565 if (bdlp
->bfl_list
== NULL
) {
566 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
567 pcap_strerror(errno
));
571 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) < 0) {
572 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
573 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
574 free(bdlp
->bfl_list
);
579 * OK, for real Ethernet devices, add DLT_DOCSIS to the
580 * list, so that an application can let you choose it,
581 * in case you're capturing DOCSIS traffic that a Cisco
582 * Cable Modem Termination System is putting out onto
583 * an Ethernet (it doesn't put an Ethernet header onto
584 * the wire, it puts raw DOCSIS frames out on the wire
585 * inside the low-level Ethernet framing).
587 * A "real Ethernet device" is defined here as a device
588 * that has a link-layer type of DLT_EN10MB and that has
589 * no alternate link-layer types; that's done to exclude
590 * 802.11 interfaces (which might or might not be the
591 * right thing to do, but I suspect it is - Ethernet <->
592 * 802.11 bridges would probably badly mishandle frames
593 * that don't have Ethernet headers).
595 * On Solaris with BPF, Ethernet devices also offer
596 * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
597 * treat it as an indication that the device isn't an
600 if (v
== DLT_EN10MB
) {
602 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
603 if (bdlp
->bfl_list
[i
] != DLT_EN10MB
605 && bdlp
->bfl_list
[i
] != DLT_IPNET
614 * We reserved one more slot at the end of
617 bdlp
->bfl_list
[bdlp
->bfl_len
] = DLT_DOCSIS
;
623 * EINVAL just means "we don't support this ioctl on
624 * this device"; don't treat it as an error.
626 if (errno
!= EINVAL
) {
627 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
628 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
637 pcap_can_set_rfmon_bpf(pcap_t
*p
)
639 #if defined(__APPLE__)
640 struct utsname osinfo
;
644 struct bpf_dltlist bdl
;
648 * The joys of monitor mode on OS X.
650 * Prior to 10.4, it's not supported at all.
652 * In 10.4, if adapter enN supports monitor mode, there's a
653 * wltN adapter corresponding to it; you open it, instead of
654 * enN, to get monitor mode. You get whatever link-layer
655 * headers it supplies.
657 * In 10.5, and, we assume, later releases, if adapter enN
658 * supports monitor mode, it offers, among its selectable
659 * DLT_ values, values that let you get the 802.11 header;
660 * selecting one of those values puts the adapter into monitor
661 * mode (i.e., you can't get 802.11 headers except in monitor
662 * mode, and you can't get Ethernet headers in monitor mode).
664 if (uname(&osinfo
) == -1) {
666 * Can't get the OS version; just say "no".
671 * We assume osinfo.sysname is "Darwin", because
672 * __APPLE__ is defined. We just check the version.
674 if (osinfo
.release
[0] < '8' && osinfo
.release
[1] == '.') {
676 * 10.3 (Darwin 7.x) or earlier.
677 * Monitor mode not supported.
681 if (osinfo
.release
[0] == '8' && osinfo
.release
[1] == '.') {
683 * 10.4 (Darwin 8.x). s/en/wlt/, and check
684 * whether the device exists.
686 if (strncmp(p
->opt
.source
, "en", 2) != 0) {
688 * Not an enN device; no monitor mode.
692 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
694 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
695 "socket: %s", pcap_strerror(errno
));
698 strlcpy(ifr
.ifr_name
, "wlt", sizeof(ifr
.ifr_name
));
699 strlcat(ifr
.ifr_name
, p
->opt
.source
+ 2, sizeof(ifr
.ifr_name
));
700 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
713 * Everything else is 10.5 or later; for those,
714 * we just open the enN device, and check whether
715 * we have any 802.11 devices.
717 * First, open a BPF device.
721 return (fd
); /* fd is the appropriate error code */
724 * Now bind to the device.
726 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
, sizeof(ifr
.ifr_name
));
727 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
732 * There's no such device.
735 return (PCAP_ERROR_NO_SUCH_DEVICE
);
739 * Return a "network down" indication, so that
740 * the application can report that rather than
741 * saying we had a mysterious failure and
742 * suggest that they report a problem to the
743 * libpcap developers.
746 return (PCAP_ERROR_IFACE_NOT_UP
);
749 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
751 p
->opt
.source
, pcap_strerror(errno
));
758 * We know the default link type -- now determine all the DLTs
759 * this interface supports. If this fails with EINVAL, it's
760 * not fatal; we just don't get to use the feature later.
761 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
762 * as the default DLT for this adapter.)
764 if (get_dlt_list(fd
, DLT_NULL
, &bdl
, p
->errbuf
) == PCAP_ERROR
) {
768 if (find_802_11(&bdl
) != -1) {
770 * We have an 802.11 DLT, so we can set monitor mode.
777 #endif /* BIOCGDLTLIST */
779 #elif defined(HAVE_BSD_IEEE80211)
782 ret
= monitor_mode(p
, 0);
783 if (ret
== PCAP_ERROR_RFMON_NOTSUP
)
784 return (0); /* not an error, just a "can't do" */
786 return (1); /* success */
794 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
799 * "ps_recv" counts packets handed to the filter, not packets
800 * that passed the filter. This includes packets later dropped
801 * because we ran out of buffer space.
803 * "ps_drop" counts packets dropped inside the BPF device
804 * because we ran out of buffer space. It doesn't count
805 * packets dropped by the interface driver. It counts
806 * only packets that passed the filter.
808 * Both statistics include packets not yet read from the kernel
809 * by libpcap, and thus not yet seen by the application.
811 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
812 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
813 pcap_strerror(errno
));
817 ps
->ps_recv
= s
.bs_recv
;
818 ps
->ps_drop
= s
.bs_drop
;
824 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
826 struct pcap_bpf
*pb
= p
->private;
829 register u_char
*bp
, *ep
;
834 #ifdef HAVE_ZEROCOPY_BPF
840 * Has "pcap_breakloop()" been called?
844 * Yes - clear the flag that indicates that it
845 * has, and return PCAP_ERROR_BREAK to indicate
846 * that we were told to break out of the loop.
849 return (PCAP_ERROR_BREAK
);
854 * When reading without zero-copy from a file descriptor, we
855 * use a single buffer and return a length of data in the
856 * buffer. With zero-copy, we update the p->buffer pointer
857 * to point at whatever underlying buffer contains the next
858 * data and update cc to reflect the data found in the
861 #ifdef HAVE_ZEROCOPY_BPF
863 if (p
->buffer
!= NULL
)
865 i
= pcap_next_zbuf(p
, &cc
);
873 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
876 /* Don't choke when we get ptraced */
885 * Sigh. More AIX wonderfulness.
887 * For some unknown reason the uiomove()
888 * operation in the bpf kernel extension
889 * used to copy the buffer into user
890 * space sometimes returns EFAULT. I have
891 * no idea why this is the case given that
892 * a kernel debugger shows the user buffer
893 * is correct. This problem appears to
894 * be mostly mitigated by the memset of
895 * the buffer before it is first used.
896 * Very strange.... Shaun Clowes
898 * In any case this means that we shouldn't
899 * treat EFAULT as a fatal error; as we
900 * don't have an API for returning
901 * a "some packets were dropped since
902 * the last packet you saw" indication,
903 * we just ignore EFAULT and keep reading.
913 * The device on which we're capturing
916 * XXX - we should really return
917 * PCAP_ERROR_IFACE_NOT_UP, but
918 * pcap_dispatch() etc. aren't
919 * defined to retur that.
921 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
922 "The interface went down");
925 #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
927 * Due to a SunOS bug, after 2^31 bytes, the kernel
928 * file offset overflows and read fails with EINVAL.
929 * The lseek() to 0 will fix things.
932 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
934 (void)lseek(p
->fd
, 0L, SEEK_SET
);
940 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
941 pcap_strerror(errno
));
949 * Loop through each packet.
951 #define bhp ((struct bpf_hdr *)bp)
957 register int caplen
, hdrlen
;
960 * Has "pcap_breakloop()" been called?
961 * If so, return immediately - if we haven't read any
962 * packets, clear the flag and return PCAP_ERROR_BREAK
963 * to indicate that we were told to break out of the loop,
964 * otherwise leave the flag set, so that the *next* call
965 * will break out of the loop without having read any
966 * packets, and return the number of packets we've
973 * ep is set based on the return value of read(),
974 * but read() from a BPF device doesn't necessarily
975 * return a value that's a multiple of the alignment
976 * value for BPF_WORDALIGN(). However, whenever we
977 * increment bp, we round up the increment value by
978 * a value rounded up by BPF_WORDALIGN(), so we
979 * could increment bp past ep after processing the
980 * last packet in the buffer.
982 * We treat ep < bp as an indication that this
983 * happened, and just set p->cc to 0.
989 return (PCAP_ERROR_BREAK
);
994 caplen
= bhp
->bh_caplen
;
995 hdrlen
= bhp
->bh_hdrlen
;
998 * Short-circuit evaluation: if using BPF filter
999 * in kernel, no need to do it now - we already know
1000 * the packet passed the filter.
1003 * Note: the filter code was generated assuming
1004 * that p->fddipad was the amount of padding
1005 * before the header, as that's what's required
1006 * in the kernel, so we run the filter before
1007 * skipping that padding.
1010 if (pb
->filtering_in_kernel
||
1011 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
1012 struct pcap_pkthdr pkthdr
;
1014 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
1017 * AIX's BPF returns seconds/nanoseconds time
1018 * stamps, not seconds/microseconds time stamps.
1020 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
1022 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
1026 pkthdr
.caplen
= caplen
- pad
;
1029 if (bhp
->bh_datalen
> pad
)
1030 pkthdr
.len
= bhp
->bh_datalen
- pad
;
1035 pkthdr
.caplen
= caplen
;
1036 pkthdr
.len
= bhp
->bh_datalen
;
1038 (*callback
)(user
, &pkthdr
, datap
);
1039 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1040 if (++n
>= cnt
&& cnt
> 0) {
1044 * See comment above about p->cc < 0.
1054 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1063 pcap_inject_bpf(pcap_t
*p
, const void *buf
, size_t size
)
1067 ret
= write(p
->fd
, buf
, size
);
1069 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
1071 * In Mac OS X, there's a bug wherein setting the
1072 * BIOCSHDRCMPLT flag causes writes to fail; see,
1075 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
1077 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
1078 * assume it's due to that bug, and turn off that flag
1079 * and try again. If we succeed, it either means that
1080 * somebody applied the fix from that URL, or other patches
1083 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
1085 * and are running a Darwin kernel with those fixes, or
1086 * that Apple fixed the problem in some OS X release.
1088 u_int spoof_eth_src
= 0;
1090 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1091 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1092 "send: can't turn off BIOCSHDRCMPLT: %s",
1093 pcap_strerror(errno
));
1094 return (PCAP_ERROR
);
1098 * Now try the write again.
1100 ret
= write(p
->fd
, buf
, size
);
1102 #endif /* __APPLE__ */
1104 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
1105 pcap_strerror(errno
));
1106 return (PCAP_ERROR
);
1113 bpf_odminit(char *errbuf
)
1117 if (odm_initialize() == -1) {
1118 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1119 errstr
= "Unknown error";
1120 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1121 "bpf_load: odm_initialize failed: %s",
1123 return (PCAP_ERROR
);
1126 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
1127 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1128 errstr
= "Unknown error";
1129 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1130 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
1132 (void)odm_terminate();
1133 return (PCAP_ERROR
);
1140 bpf_odmcleanup(char *errbuf
)
1144 if (odm_unlock(odmlockid
) == -1) {
1145 if (errbuf
!= NULL
) {
1146 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1147 errstr
= "Unknown error";
1148 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1149 "bpf_load: odm_unlock failed: %s",
1152 return (PCAP_ERROR
);
1155 if (odm_terminate() == -1) {
1156 if (errbuf
!= NULL
) {
1157 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1158 errstr
= "Unknown error";
1159 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1160 "bpf_load: odm_terminate failed: %s",
1163 return (PCAP_ERROR
);
1170 bpf_load(char *errbuf
)
1174 int numminors
, i
, rc
;
1177 struct bpf_config cfg_bpf
;
1178 struct cfg_load cfg_ld
;
1179 struct cfg_kmod cfg_km
;
1182 * This is very very close to what happens in the real implementation
1183 * but I've fixed some (unlikely) bug situations.
1188 if (bpf_odminit(errbuf
) == PCAP_ERROR
)
1189 return (PCAP_ERROR
);
1191 major
= genmajor(BPF_NAME
);
1193 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1194 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
1195 (void)bpf_odmcleanup(NULL
);
1196 return (PCAP_ERROR
);
1199 minors
= getminor(major
, &numminors
, BPF_NAME
);
1201 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
1203 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1204 "bpf_load: genminor failed: %s",
1205 pcap_strerror(errno
));
1206 (void)bpf_odmcleanup(NULL
);
1207 return (PCAP_ERROR
);
1211 if (bpf_odmcleanup(errbuf
) == PCAP_ERROR
)
1212 return (PCAP_ERROR
);
1214 rc
= stat(BPF_NODE
"0", &sbuf
);
1215 if (rc
== -1 && errno
!= ENOENT
) {
1216 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1217 "bpf_load: can't stat %s: %s",
1218 BPF_NODE
"0", pcap_strerror(errno
));
1219 return (PCAP_ERROR
);
1222 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
1223 for (i
= 0; i
< BPF_MINORS
; i
++) {
1224 sprintf(buf
, "%s%d", BPF_NODE
, i
);
1226 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
1227 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1228 "bpf_load: can't mknod %s: %s",
1229 buf
, pcap_strerror(errno
));
1230 return (PCAP_ERROR
);
1235 /* Check if the driver is loaded */
1236 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
1238 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
1239 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
1240 (cfg_ld
.kmid
== 0)) {
1241 /* Driver isn't loaded, load it now */
1242 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
1243 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1244 "bpf_load: could not load driver: %s",
1246 return (PCAP_ERROR
);
1250 /* Configure the driver */
1251 cfg_km
.cmd
= CFG_INIT
;
1252 cfg_km
.kmid
= cfg_ld
.kmid
;
1253 cfg_km
.mdilen
= sizeof(cfg_bpf
);
1254 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
1255 for (i
= 0; i
< BPF_MINORS
; i
++) {
1256 cfg_bpf
.devno
= domakedev(major
, i
);
1257 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
1258 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1259 "bpf_load: could not configure driver: %s",
1261 return (PCAP_ERROR
);
1272 * Turn off rfmon mode if necessary.
1275 pcap_cleanup_bpf(pcap_t
*p
)
1277 struct pcap_bpf
*pb
= p
->private;
1278 #ifdef HAVE_BSD_IEEE80211
1280 struct ifmediareq req
;
1284 if (pb
->must_do_on_close
!= 0) {
1286 * There's something we have to do when closing this
1289 #ifdef HAVE_BSD_IEEE80211
1290 if (pb
->must_do_on_close
& MUST_CLEAR_RFMON
) {
1292 * We put the interface into rfmon mode;
1293 * take it out of rfmon mode.
1295 * XXX - if somebody else wants it in rfmon
1296 * mode, this code cannot know that, so it'll take
1297 * it out of rfmon mode.
1299 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
1302 "Can't restore interface flags (socket() failed: %s).\n"
1303 "Please adjust manually.\n",
1306 memset(&req
, 0, sizeof(req
));
1307 strncpy(req
.ifm_name
, pb
->device
,
1308 sizeof(req
.ifm_name
));
1309 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1311 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
1312 "Please adjust manually.\n",
1315 if (req
.ifm_current
& IFM_IEEE80211_MONITOR
) {
1317 * Rfmon mode is currently on;
1320 memset(&ifr
, 0, sizeof(ifr
));
1321 (void)strncpy(ifr
.ifr_name
,
1323 sizeof(ifr
.ifr_name
));
1325 req
.ifm_current
& ~IFM_IEEE80211_MONITOR
;
1326 if (ioctl(sock
, SIOCSIFMEDIA
,
1329 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
1330 "Please adjust manually.\n",
1338 #endif /* HAVE_BSD_IEEE80211 */
1341 * Take this pcap out of the list of pcaps for which we
1342 * have to take the interface out of some mode.
1344 pcap_remove_from_pcaps_to_close(p
);
1345 pb
->must_do_on_close
= 0;
1348 #ifdef HAVE_ZEROCOPY_BPF
1351 * Delete the mappings. Note that p->buffer gets
1352 * initialized to one of the mmapped regions in
1353 * this case, so do not try and free it directly;
1354 * null it out so that pcap_cleanup_live_common()
1355 * doesn't try to free it.
1357 if (pb
->zbuf1
!= MAP_FAILED
&& pb
->zbuf1
!= NULL
)
1358 (void) munmap(pb
->zbuf1
, pb
->zbufsize
);
1359 if (pb
->zbuf2
!= MAP_FAILED
&& pb
->zbuf2
!= NULL
)
1360 (void) munmap(pb
->zbuf2
, pb
->zbufsize
);
1364 if (pb
->device
!= NULL
) {
1368 pcap_cleanup_live_common(p
);
1372 check_setif_failure(pcap_t
*p
, int error
)
1380 if (error
== ENXIO
) {
1382 * No such device exists.
1385 if (p
->opt
.rfmon
&& strncmp(p
->opt
.source
, "wlt", 3) == 0) {
1387 * Monitor mode was requested, and we're trying
1388 * to open a "wltN" device. Assume that this
1389 * is 10.4 and that we were asked to open an
1390 * "enN" device; if that device exists, return
1391 * "monitor mode not supported on the device".
1393 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1395 strlcpy(ifr
.ifr_name
, "en",
1396 sizeof(ifr
.ifr_name
));
1397 strlcat(ifr
.ifr_name
, p
->opt
.source
+ 3,
1398 sizeof(ifr
.ifr_name
));
1399 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
1401 * We assume this failed because
1402 * the underlying device doesn't
1405 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1406 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1407 "SIOCGIFFLAGS on %s failed: %s",
1408 ifr
.ifr_name
, pcap_strerror(errno
));
1411 * The underlying "enN" device
1412 * exists, but there's no
1413 * corresponding "wltN" device;
1414 * that means that the "enN"
1415 * device doesn't support
1416 * monitor mode, probably because
1417 * it's an Ethernet device rather
1418 * than a wireless device.
1420 err
= PCAP_ERROR_RFMON_NOTSUP
;
1425 * We can't find out whether there's
1426 * an underlying "enN" device, so
1427 * just report "no such device".
1429 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1430 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1431 "socket() failed: %s",
1432 pcap_strerror(errno
));
1440 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF failed: %s",
1441 pcap_strerror(errno
));
1442 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1443 } else if (errno
== ENETDOWN
) {
1445 * Return a "network down" indication, so that
1446 * the application can report that rather than
1447 * saying we had a mysterious failure and
1448 * suggest that they report a problem to the
1449 * libpcap developers.
1451 return (PCAP_ERROR_IFACE_NOT_UP
);
1454 * Some other error; fill in the error string, and
1455 * return PCAP_ERROR.
1457 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1458 p
->opt
.source
, pcap_strerror(errno
));
1459 return (PCAP_ERROR
);
1464 * Default capture buffer size.
1465 * 32K isn't very much for modern machines with fast networks; we
1466 * pick .5M, as that's the maximum on at least some systems with BPF.
1468 * However, on AIX 3.5, the larger buffer sized caused unrecoverable
1469 * read failures under stress, so we leave it as 32K; yet another
1470 * place where AIX's BPF is broken.
1473 #define DEFAULT_BUFSIZE 32768
1475 #define DEFAULT_BUFSIZE 524288
1479 pcap_activate_bpf(pcap_t
*p
)
1481 struct pcap_bpf
*pb
= p
->private;
1487 char *ifrname
= ifr
.lifr_name
;
1488 const size_t ifnamsiz
= sizeof(ifr
.lifr_name
);
1491 char *ifrname
= ifr
.ifr_name
;
1492 const size_t ifnamsiz
= sizeof(ifr
.ifr_name
);
1494 struct bpf_version bv
;
1497 char *wltdev
= NULL
;
1500 struct bpf_dltlist bdl
;
1501 #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1504 #endif /* BIOCGDLTLIST */
1505 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1506 u_int spoof_eth_src
= 1;
1509 struct bpf_insn total_insn
;
1510 struct bpf_program total_prog
;
1511 struct utsname osinfo
;
1512 int have_osinfo
= 0;
1513 #ifdef HAVE_ZEROCOPY_BPF
1515 u_int bufmode
, zbufmax
;
1526 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
1527 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
1528 pcap_strerror(errno
));
1529 status
= PCAP_ERROR
;
1532 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
1533 bv
.bv_minor
< BPF_MINOR_VERSION
) {
1534 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1535 "kernel bpf filter out of date");
1536 status
= PCAP_ERROR
;
1540 #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid)
1542 * Check if the given source network device has a '/' separated
1543 * zonename prefix string. The zonename prefixed source device
1544 * can be used by libpcap consumers to capture network traffic
1545 * in non-global zones from the global zone on Solaris 11 and
1546 * above. If the zonename prefix is present then we strip the
1547 * prefix and pass the zone ID as part of lifr_zoneid.
1549 if ((zonesep
= strchr(p
->opt
.source
, '/')) != NULL
) {
1550 char zonename
[ZONENAME_MAX
];
1554 znamelen
= zonesep
- p
->opt
.source
;
1555 (void) strlcpy(zonename
, p
->opt
.source
, znamelen
+ 1);
1556 lnamep
= strdup(zonesep
+ 1);
1557 ifr
.lifr_zoneid
= getzoneidbyname(zonename
);
1558 free(p
->opt
.source
);
1559 p
->opt
.source
= lnamep
;
1563 pb
->device
= strdup(p
->opt
.source
);
1564 if (pb
->device
== NULL
) {
1565 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
1566 pcap_strerror(errno
));
1567 status
= PCAP_ERROR
;
1572 * Attempt to find out the version of the OS on which we're running.
1574 if (uname(&osinfo
) == 0)
1579 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1580 * of why we check the version number.
1585 * We assume osinfo.sysname is "Darwin", because
1586 * __APPLE__ is defined. We just check the version.
1588 if (osinfo
.release
[0] < '8' &&
1589 osinfo
.release
[1] == '.') {
1591 * 10.3 (Darwin 7.x) or earlier.
1593 status
= PCAP_ERROR_RFMON_NOTSUP
;
1596 if (osinfo
.release
[0] == '8' &&
1597 osinfo
.release
[1] == '.') {
1599 * 10.4 (Darwin 8.x). s/en/wlt/
1601 if (strncmp(p
->opt
.source
, "en", 2) != 0) {
1603 * Not an enN device; check
1604 * whether the device even exists.
1606 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1609 p
->opt
.source
, ifnamsiz
);
1610 if (ioctl(sockfd
, SIOCGIFFLAGS
,
1611 (char *)&ifr
) < 0) {
1619 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1622 "SIOCGIFFLAGS failed: %s",
1623 pcap_strerror(errno
));
1625 status
= PCAP_ERROR_RFMON_NOTSUP
;
1629 * We can't find out whether
1630 * the device exists, so just
1631 * report "no such device".
1633 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1636 "socket() failed: %s",
1637 pcap_strerror(errno
));
1641 wltdev
= malloc(strlen(p
->opt
.source
) + 2);
1642 if (wltdev
== NULL
) {
1643 (void)snprintf(p
->errbuf
,
1644 PCAP_ERRBUF_SIZE
, "malloc: %s",
1645 pcap_strerror(errno
));
1646 status
= PCAP_ERROR
;
1649 strcpy(wltdev
, "wlt");
1650 strcat(wltdev
, p
->opt
.source
+ 2);
1651 free(p
->opt
.source
);
1652 p
->opt
.source
= wltdev
;
1655 * Everything else is 10.5 or later; for those,
1656 * we just open the enN device, and set the DLT.
1660 #endif /* __APPLE__ */
1661 #ifdef HAVE_ZEROCOPY_BPF
1663 * If the BPF extension to set buffer mode is present, try setting
1664 * the mode to zero-copy. If that fails, use regular buffering. If
1665 * it succeeds but other setup fails, return an error to the user.
1667 bufmode
= BPF_BUFMODE_ZBUF
;
1668 if (ioctl(fd
, BIOCSETBUFMODE
, (caddr_t
)&bufmode
) == 0) {
1670 * We have zerocopy BPF; use it.
1675 * How to pick a buffer size: first, query the maximum buffer
1676 * size supported by zero-copy. This also lets us quickly
1677 * determine whether the kernel generally supports zero-copy.
1678 * Then, if a buffer size was specified, use that, otherwise
1679 * query the default buffer size, which reflects kernel
1680 * policy for a desired default. Round to the nearest page
1683 if (ioctl(fd
, BIOCGETZMAX
, (caddr_t
)&zbufmax
) < 0) {
1684 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGETZMAX: %s",
1685 pcap_strerror(errno
));
1689 if (p
->opt
.buffer_size
!= 0) {
1691 * A buffer size was explicitly specified; use it.
1693 v
= p
->opt
.buffer_size
;
1695 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1696 v
< DEFAULT_BUFSIZE
)
1697 v
= DEFAULT_BUFSIZE
;
1700 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
1702 pb
->zbufsize
= roundup(v
, getpagesize());
1703 if (pb
->zbufsize
> zbufmax
)
1704 pb
->zbufsize
= zbufmax
;
1705 pb
->zbuf1
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
1707 pb
->zbuf2
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
1709 if (pb
->zbuf1
== MAP_FAILED
|| pb
->zbuf2
== MAP_FAILED
) {
1710 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "mmap: %s",
1711 pcap_strerror(errno
));
1714 memset(&bz
, 0, sizeof(bz
)); /* bzero() deprecated, replaced with memset() */
1715 bz
.bz_bufa
= pb
->zbuf1
;
1716 bz
.bz_bufb
= pb
->zbuf2
;
1717 bz
.bz_buflen
= pb
->zbufsize
;
1718 if (ioctl(fd
, BIOCSETZBUF
, (caddr_t
)&bz
) < 0) {
1719 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETZBUF: %s",
1720 pcap_strerror(errno
));
1723 (void)strncpy(ifrname
, p
->opt
.source
, ifnamsiz
);
1724 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
1725 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1726 p
->opt
.source
, pcap_strerror(errno
));
1729 v
= pb
->zbufsize
- sizeof(struct bpf_zbuf_header
);
1734 * We don't have zerocopy BPF.
1735 * Set the buffer size.
1737 if (p
->opt
.buffer_size
!= 0) {
1739 * A buffer size was explicitly specified; use it.
1741 if (ioctl(fd
, BIOCSBLEN
,
1742 (caddr_t
)&p
->opt
.buffer_size
) < 0) {
1743 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1744 "BIOCSBLEN: %s: %s", p
->opt
.source
,
1745 pcap_strerror(errno
));
1746 status
= PCAP_ERROR
;
1751 * Now bind to the device.
1753 (void)strncpy(ifrname
, p
->opt
.source
, ifnamsiz
);
1755 if (ioctl(fd
, BIOCSETLIF
, (caddr_t
)&ifr
) < 0)
1757 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0)
1760 status
= check_setif_failure(p
, errno
);
1765 * No buffer size was explicitly specified.
1767 * Try finding a good size for the buffer;
1768 * DEFAULT_BUFSIZE may be too big, so keep
1769 * cutting it in half until we find a size
1770 * that works, or run out of sizes to try.
1771 * If the default is larger, don't make it smaller.
1773 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1774 v
< DEFAULT_BUFSIZE
)
1775 v
= DEFAULT_BUFSIZE
;
1776 for ( ; v
!= 0; v
>>= 1) {
1778 * Ignore the return value - this is because the
1779 * call fails on BPF systems that don't have
1780 * kernel malloc. And if the call fails, it's
1781 * no big deal, we just continue to use the
1782 * standard buffer size.
1784 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
1786 (void)strncpy(ifrname
, p
->opt
.source
, ifnamsiz
);
1788 if (ioctl(fd
, BIOCSETLIF
, (caddr_t
)&ifr
) >= 0)
1790 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
1792 break; /* that size worked; we're done */
1794 if (errno
!= ENOBUFS
) {
1795 status
= check_setif_failure(p
, errno
);
1801 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1802 "BIOCSBLEN: %s: No buffer size worked",
1804 status
= PCAP_ERROR
;
1810 /* Get the data link layer type. */
1811 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
1812 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
1813 pcap_strerror(errno
));
1814 status
= PCAP_ERROR
;
1820 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
1843 * We don't know what to map this to yet.
1845 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
1847 status
= PCAP_ERROR
;
1851 #if _BSDI_VERSION - 0 >= 199510
1852 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
1867 case 12: /*DLT_C_HDLC*/
1875 * We know the default link type -- now determine all the DLTs
1876 * this interface supports. If this fails with EINVAL, it's
1877 * not fatal; we just don't get to use the feature later.
1879 if (get_dlt_list(fd
, v
, &bdl
, p
->errbuf
) == -1) {
1880 status
= PCAP_ERROR
;
1883 p
->dlt_count
= bdl
.bfl_len
;
1884 p
->dlt_list
= bdl
.bfl_list
;
1888 * Monitor mode fun, continued.
1890 * For 10.5 and, we're assuming, later releases, as noted above,
1891 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
1892 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
1893 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
1896 * Therefore, if the user asked for monitor mode, we filter out
1897 * the DLT_EN10MB value, as you can't get that in monitor mode,
1898 * and, if the user didn't ask for monitor mode, we filter out
1899 * the 802.11 DLT_ values, because selecting those will turn
1900 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
1901 * radio DLT_ value is offered, we try to select that, otherwise
1902 * we try to select DLT_IEEE802_11.
1905 if (isdigit((unsigned)osinfo
.release
[0]) &&
1906 (osinfo
.release
[0] == '9' ||
1907 isdigit((unsigned)osinfo
.release
[1]))) {
1909 * 10.5 (Darwin 9.x), or later.
1911 new_dlt
= find_802_11(&bdl
);
1912 if (new_dlt
!= -1) {
1914 * We have at least one 802.11 DLT_ value,
1915 * so this is an 802.11 interface.
1916 * new_dlt is the best of the 802.11
1917 * DLT_ values in the list.
1921 * Our caller wants monitor mode.
1922 * Purge DLT_EN10MB from the list
1923 * of link-layer types, as selecting
1924 * it will keep monitor mode off.
1929 * If the new mode we want isn't
1930 * the default mode, attempt to
1931 * select the new mode.
1934 if (ioctl(p
->fd
, BIOCSDLT
,
1946 * Our caller doesn't want
1947 * monitor mode. Unless this
1948 * is being done by pcap_open_live(),
1949 * purge the 802.11 link-layer types
1950 * from the list, as selecting
1951 * one of them will turn monitor
1960 * The caller requested monitor
1961 * mode, but we have no 802.11
1962 * link-layer types, so they
1965 status
= PCAP_ERROR_RFMON_NOTSUP
;
1971 #elif defined(HAVE_BSD_IEEE80211)
1973 * *BSD with the new 802.11 ioctls.
1974 * Do we want monitor mode?
1978 * Try to put the interface into monitor mode.
1980 status
= monitor_mode(p
, 1);
1989 * We're in monitor mode.
1990 * Try to find the best 802.11 DLT_ value and, if we
1991 * succeed, try to switch to that mode if we're not
1992 * already in that mode.
1994 new_dlt
= find_802_11(&bdl
);
1995 if (new_dlt
!= -1) {
1997 * We have at least one 802.11 DLT_ value.
1998 * new_dlt is the best of the 802.11
1999 * DLT_ values in the list.
2001 * If the new mode we want isn't the default mode,
2002 * attempt to select the new mode.
2005 if (ioctl(p
->fd
, BIOCSDLT
, &new_dlt
) != -1) {
2007 * We succeeded; make this the
2015 #endif /* various platforms */
2016 #endif /* BIOCGDLTLIST */
2019 * If this is an Ethernet device, and we don't have a DLT_ list,
2020 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
2021 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
2022 * do, but there's not much we can do about that without finding
2023 * some other way of determining whether it's an Ethernet or 802.11
2026 if (v
== DLT_EN10MB
&& p
->dlt_count
== 0) {
2027 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
2029 * If that fails, just leave the list empty.
2031 if (p
->dlt_list
!= NULL
) {
2032 p
->dlt_list
[0] = DLT_EN10MB
;
2033 p
->dlt_list
[1] = DLT_DOCSIS
;
2039 p
->fddipad
= PCAP_FDDIPAD
;
2045 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
2047 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
2048 * the link-layer source address isn't forcibly overwritten.
2049 * (Should we ignore errors? Should we do this only if
2050 * we're open for writing?)
2052 * XXX - I seem to remember some packet-sending bug in some
2053 * BSDs - check CVS log for "bpf.c"?
2055 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
2056 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2057 "BIOCSHDRCMPLT: %s", pcap_strerror(errno
));
2058 status
= PCAP_ERROR
;
2063 #ifdef HAVE_ZEROCOPY_BPF
2065 * In zero-copy mode, we just use the timeout in select().
2066 * XXX - what if we're in non-blocking mode and the *application*
2067 * is using select() or poll() or kqueues or....?
2069 if (p
->opt
.timeout
&& !pb
->zerocopy
) {
2071 if (p
->opt
.timeout
) {
2074 * XXX - is this seconds/nanoseconds in AIX?
2075 * (Treating it as such doesn't fix the timeout
2076 * problem described below.)
2078 * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
2079 * 64-bit userland - it takes, as an argument, a
2080 * "struct BPF_TIMEVAL", which has 32-bit tv_sec
2081 * and tv_usec, rather than a "struct timeval".
2083 * If this platform defines "struct BPF_TIMEVAL",
2084 * we check whether the structure size in BIOCSRTIMEOUT
2085 * is that of a "struct timeval" and, if not, we use
2086 * a "struct BPF_TIMEVAL" rather than a "struct timeval".
2087 * (That way, if the bug is fixed in a future release,
2088 * we will still do the right thing.)
2091 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2092 struct BPF_TIMEVAL bpf_to
;
2094 if (IOCPARM_LEN(BIOCSRTIMEOUT
) != sizeof(struct timeval
)) {
2095 bpf_to
.tv_sec
= p
->opt
.timeout
/ 1000;
2096 bpf_to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2097 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&bpf_to
) < 0) {
2098 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2099 "BIOCSRTIMEOUT: %s", pcap_strerror(errno
));
2100 status
= PCAP_ERROR
;
2105 to
.tv_sec
= p
->opt
.timeout
/ 1000;
2106 to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2107 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
2108 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2109 "BIOCSRTIMEOUT: %s", pcap_strerror(errno
));
2110 status
= PCAP_ERROR
;
2113 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2118 #ifdef BIOCIMMEDIATE
2120 * Darren Reed notes that
2122 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
2123 * timeout appears to be ignored and it waits until the buffer
2124 * is filled before returning. The result of not having it
2125 * set is almost worse than useless if your BPF filter
2126 * is reducing things to only a few packets (i.e. one every
2129 * so we turn BIOCIMMEDIATE mode on if this is AIX.
2131 * We don't, by default, turn it on for other platforms, as that
2132 * means we get woken up for every packet, which may not be what
2133 * we want; we only turn it on if requested.
2136 if (p
->opt
.immediate
) {
2139 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
2140 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2141 "BIOCIMMEDIATE: %s", pcap_strerror(errno
));
2142 status
= PCAP_ERROR
;
2148 #endif /* BIOCIMMEDIATE */
2150 if (p
->opt
.promisc
) {
2151 /* set promiscuous mode, just warn if it fails */
2152 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
2153 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
2154 pcap_strerror(errno
));
2155 status
= PCAP_WARNING_PROMISC_NOTSUP
;
2159 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
2160 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
2161 pcap_strerror(errno
));
2162 status
= PCAP_ERROR
;
2166 #ifdef HAVE_ZEROCOPY_BPF
2167 if (!pb
->zerocopy
) {
2169 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
2170 if (p
->buffer
== NULL
) {
2171 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
2172 pcap_strerror(errno
));
2173 status
= PCAP_ERROR
;
2177 /* For some strange reason this seems to prevent the EFAULT
2178 * problems we have experienced from AIX BPF. */
2179 memset(p
->buffer
, 0x0, p
->bufsize
);
2181 #ifdef HAVE_ZEROCOPY_BPF
2186 * If there's no filter program installed, there's
2187 * no indication to the kernel of what the snapshot
2188 * length should be, so no snapshotting is done.
2190 * Therefore, when we open the device, we install
2191 * an "accept everything" filter with the specified
2194 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
2197 total_insn
.k
= p
->snapshot
;
2199 total_prog
.bf_len
= 1;
2200 total_prog
.bf_insns
= &total_insn
;
2201 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
2202 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2203 pcap_strerror(errno
));
2204 status
= PCAP_ERROR
;
2209 * On most BPF platforms, either you can do a "select()" or
2210 * "poll()" on a BPF file descriptor and it works correctly,
2211 * or you can do it and it will return "readable" if the
2212 * hold buffer is full but not if the timeout expires *and*
2213 * a non-blocking read will, if the hold buffer is empty
2214 * but the store buffer isn't empty, rotate the buffers
2215 * and return what packets are available.
2217 * In the latter case, the fact that a non-blocking read
2218 * will give you the available packets means you can work
2219 * around the failure of "select()" and "poll()" to wake up
2220 * and return "readable" when the timeout expires by using
2221 * the timeout as the "select()" or "poll()" timeout, putting
2222 * the BPF descriptor into non-blocking mode, and read from
2223 * it regardless of whether "select()" reports it as readable
2226 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
2227 * won't wake up and return "readable" if the timer expires
2228 * and non-blocking reads return EWOULDBLOCK if the hold
2229 * buffer is empty, even if the store buffer is non-empty.
2231 * This means the workaround in question won't work.
2233 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2234 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2235 * here". On all other BPF platforms, we set it to the FD for
2236 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2237 * read will, if the hold buffer is empty and the store buffer
2238 * isn't empty, rotate the buffers and return what packets are
2239 * there (and in sufficiently recent versions of OpenBSD
2240 * "select()" and "poll()" should work correctly).
2242 * XXX - what about AIX?
2244 p
->selectable_fd
= p
->fd
; /* assume select() works until we know otherwise */
2247 * We can check what OS this is.
2249 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0) {
2250 if (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
2251 strncmp(osinfo
.release
, "4.4-", 4) == 0)
2252 p
->selectable_fd
= -1;
2256 p
->read_op
= pcap_read_bpf
;
2257 p
->inject_op
= pcap_inject_bpf
;
2258 p
->setfilter_op
= pcap_setfilter_bpf
;
2259 p
->setdirection_op
= pcap_setdirection_bpf
;
2260 p
->set_datalink_op
= pcap_set_datalink_bpf
;
2261 p
->getnonblock_op
= pcap_getnonblock_bpf
;
2262 p
->setnonblock_op
= pcap_setnonblock_bpf
;
2263 p
->stats_op
= pcap_stats_bpf
;
2264 p
->cleanup_op
= pcap_cleanup_bpf
;
2268 pcap_cleanup_bpf(p
);
2273 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
2278 #ifdef HAVE_BSD_IEEE80211
2280 monitor_mode(pcap_t
*p
, int set
)
2282 struct pcap_bpf
*pb
= p
->private;
2284 struct ifmediareq req
;
2290 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
2292 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "can't open socket: %s",
2293 pcap_strerror(errno
));
2294 return (PCAP_ERROR
);
2297 memset(&req
, 0, sizeof req
);
2298 strncpy(req
.ifm_name
, p
->opt
.source
, sizeof req
.ifm_name
);
2301 * Find out how many media types we have.
2303 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2305 * Can't get the media types.
2311 * There's no such device.
2314 return (PCAP_ERROR_NO_SUCH_DEVICE
);
2318 * Interface doesn't support SIOC{G,S}IFMEDIA.
2321 return (PCAP_ERROR_RFMON_NOTSUP
);
2324 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2325 "SIOCGIFMEDIA 1: %s", pcap_strerror(errno
));
2327 return (PCAP_ERROR
);
2330 if (req
.ifm_count
== 0) {
2335 return (PCAP_ERROR_RFMON_NOTSUP
);
2339 * Allocate a buffer to hold all the media types, and
2340 * get the media types.
2342 media_list
= malloc(req
.ifm_count
* sizeof(int));
2343 if (media_list
== NULL
) {
2344 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
2345 pcap_strerror(errno
));
2347 return (PCAP_ERROR
);
2349 req
.ifm_ulist
= media_list
;
2350 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2351 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCGIFMEDIA: %s",
2352 pcap_strerror(errno
));
2355 return (PCAP_ERROR
);
2359 * Look for an 802.11 "automatic" media type.
2360 * We assume that all 802.11 adapters have that media type,
2361 * and that it will carry the monitor mode supported flag.
2364 for (i
= 0; i
< req
.ifm_count
; i
++) {
2365 if (IFM_TYPE(media_list
[i
]) == IFM_IEEE80211
2366 && IFM_SUBTYPE(media_list
[i
]) == IFM_AUTO
) {
2367 /* OK, does it do monitor mode? */
2368 if (media_list
[i
] & IFM_IEEE80211_MONITOR
) {
2377 * This adapter doesn't support monitor mode.
2380 return (PCAP_ERROR_RFMON_NOTSUP
);
2385 * Don't just check whether we can enable monitor mode,
2386 * do so, if it's not already enabled.
2388 if ((req
.ifm_current
& IFM_IEEE80211_MONITOR
) == 0) {
2390 * Monitor mode isn't currently on, so turn it on,
2391 * and remember that we should turn it off when the
2396 * If we haven't already done so, arrange to have
2397 * "pcap_close_all()" called when we exit.
2399 if (!pcap_do_addexit(p
)) {
2401 * "atexit()" failed; don't put the interface
2402 * in monitor mode, just give up.
2404 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2407 return (PCAP_ERROR
);
2409 memset(&ifr
, 0, sizeof(ifr
));
2410 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
2411 sizeof(ifr
.ifr_name
));
2412 ifr
.ifr_media
= req
.ifm_current
| IFM_IEEE80211_MONITOR
;
2413 if (ioctl(sock
, SIOCSIFMEDIA
, &ifr
) == -1) {
2414 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2415 "SIOCSIFMEDIA: %s", pcap_strerror(errno
));
2417 return (PCAP_ERROR
);
2420 pb
->must_do_on_close
|= MUST_CLEAR_RFMON
;
2423 * Add this to the list of pcaps to close when we exit.
2425 pcap_add_to_pcaps_to_close(p
);
2430 #endif /* HAVE_BSD_IEEE80211 */
2432 #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
2434 * Check whether we have any 802.11 link-layer types; return the best
2435 * of the 802.11 link-layer types if we find one, and return -1
2438 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
2439 * best 802.11 link-layer type; any of the other 802.11-plus-radio
2440 * headers are second-best; 802.11 with no radio information is
2444 find_802_11(struct bpf_dltlist
*bdlp
)
2450 * Scan the list of DLT_ values, looking for 802.11 values,
2451 * and, if we find any, choose the best of them.
2454 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
2455 switch (bdlp
->bfl_list
[i
]) {
2457 case DLT_IEEE802_11
:
2459 * 802.11, but no radio.
2461 * Offer this, and select it as the new mode
2462 * unless we've already found an 802.11
2463 * header with radio information.
2466 new_dlt
= bdlp
->bfl_list
[i
];
2469 case DLT_PRISM_HEADER
:
2470 case DLT_AIRONET_HEADER
:
2471 case DLT_IEEE802_11_RADIO_AVS
:
2473 * 802.11 with radio, but not radiotap.
2475 * Offer this, and select it as the new mode
2476 * unless we've already found the radiotap DLT_.
2478 if (new_dlt
!= DLT_IEEE802_11_RADIO
)
2479 new_dlt
= bdlp
->bfl_list
[i
];
2482 case DLT_IEEE802_11_RADIO
:
2484 * 802.11 with radiotap.
2486 * Offer this, and select it as the new mode.
2488 new_dlt
= bdlp
->bfl_list
[i
];
2501 #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
2503 #if defined(__APPLE__) && defined(BIOCGDLTLIST)
2505 * Remove DLT_EN10MB from the list of DLT_ values, as we're in monitor mode,
2506 * and DLT_EN10MB isn't supported in monitor mode.
2509 remove_en(pcap_t
*p
)
2514 * Scan the list of DLT_ values and discard DLT_EN10MB.
2517 for (i
= 0; i
< p
->dlt_count
; i
++) {
2518 switch (p
->dlt_list
[i
]) {
2522 * Don't offer this one.
2528 * Just copy this mode over.
2534 * Copy this DLT_ value to its new position.
2536 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2541 * Set the DLT_ count to the number of entries we copied.
2547 * Remove 802.11 link-layer types from the list of DLT_ values, as
2548 * we're not in monitor mode, and those DLT_ values will switch us
2552 remove_802_11(pcap_t
*p
)
2557 * Scan the list of DLT_ values and discard 802.11 values.
2560 for (i
= 0; i
< p
->dlt_count
; i
++) {
2561 switch (p
->dlt_list
[i
]) {
2563 case DLT_IEEE802_11
:
2564 case DLT_PRISM_HEADER
:
2565 case DLT_AIRONET_HEADER
:
2566 case DLT_IEEE802_11_RADIO
:
2567 case DLT_IEEE802_11_RADIO_AVS
:
2569 * 802.11. Don't offer this one.
2575 * Just copy this mode over.
2581 * Copy this DLT_ value to its new position.
2583 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2588 * Set the DLT_ count to the number of entries we copied.
2592 #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
2595 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
2597 struct pcap_bpf
*pb
= p
->private;
2600 * Free any user-mode filter we might happen to have installed.
2602 pcap_freecode(&p
->fcode
);
2605 * Try to install the kernel filter.
2607 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) == 0) {
2611 pb
->filtering_in_kernel
= 1; /* filtering in the kernel */
2614 * Discard any previously-received packets, as they might
2615 * have passed whatever filter was formerly in effect, but
2616 * might not pass this filter (BIOCSETF discards packets
2617 * buffered in the kernel, so you can lose packets in any
2627 * If it failed with EINVAL, that's probably because the program
2628 * is invalid or too big. Validate it ourselves; if we like it
2629 * (we currently allow backward branches, to support protochain),
2630 * run it in userland. (There's no notion of "too big" for
2633 * Otherwise, just give up.
2634 * XXX - if the copy of the program into the kernel failed,
2635 * we will get EINVAL rather than, say, EFAULT on at least
2638 if (errno
!= EINVAL
) {
2639 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2640 pcap_strerror(errno
));
2645 * install_bpf_program() validates the program.
2647 * XXX - what if we already have a filter in the kernel?
2649 if (install_bpf_program(p
, fp
) < 0)
2651 pb
->filtering_in_kernel
= 0; /* filtering in userland */
2656 * Set direction flag: Which packets do we accept on a forwarding
2657 * single device? IN, OUT or both?
2660 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
2662 #if defined(BIOCSDIRECTION)
2665 direction
= (d
== PCAP_D_IN
) ? BPF_D_IN
:
2666 ((d
== PCAP_D_OUT
) ? BPF_D_OUT
: BPF_D_INOUT
);
2667 if (ioctl(p
->fd
, BIOCSDIRECTION
, &direction
) == -1) {
2668 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2669 "Cannot set direction to %s: %s",
2670 (d
== PCAP_D_IN
) ? "PCAP_D_IN" :
2671 ((d
== PCAP_D_OUT
) ? "PCAP_D_OUT" : "PCAP_D_INOUT"),
2676 #elif defined(BIOCSSEESENT)
2680 * We don't support PCAP_D_OUT.
2682 if (d
== PCAP_D_OUT
) {
2683 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2684 "Setting direction to PCAP_D_OUT is not supported on BPF");
2688 seesent
= (d
== PCAP_D_INOUT
);
2689 if (ioctl(p
->fd
, BIOCSSEESENT
, &seesent
) == -1) {
2690 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2691 "Cannot set direction to %s: %s",
2692 (d
== PCAP_D_INOUT
) ? "PCAP_D_INOUT" : "PCAP_D_IN",
2698 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2699 "This system doesn't support BIOCSSEESENT, so the direction can't be set");
2705 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
2708 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
2709 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2710 "Cannot set DLT %d: %s", dlt
, strerror(errno
));