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.
26 #include <sys/param.h> /* optionally get BSD define */
27 #ifdef HAVE_ZEROCOPY_BPF
30 #include <sys/socket.h>
33 * <net/bpf.h> defines ioctls, but doesn't include <sys/ioccom.h>.
35 * We include <sys/ioctl.h> as it might be necessary to declare ioctl();
36 * at least on *BSD and Mac OS X, it also defines various SIOC ioctls -
37 * we could include <sys/sockio.h>, but if we're already including
38 * <sys/ioctl.h>, which includes <sys/sockio.h> on those platforms,
39 * there's not much point in doing so.
41 * If we have <sys/ioccom.h>, we include it as well, to handle systems
42 * such as Solaris which don't arrange to include <sys/ioccom.h> if you
43 * include <sys/ioctl.h>
45 #include <sys/ioctl.h>
46 #ifdef HAVE_SYS_IOCCOM_H
47 #include <sys/ioccom.h>
49 #include <sys/utsname.h>
51 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
53 * Add support for capturing on FreeBSD usbusN interfaces.
55 static const char usbus_prefix
[] = "usbus";
56 #define USBUS_PREFIX_LEN (sizeof(usbus_prefix) - 1)
60 #ifdef HAVE_ZEROCOPY_BPF
61 #include <machine/atomic.h>
69 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
70 * native OS version, as we need "struct bpf_config" from it.
72 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
74 #include <sys/types.h>
77 * Prevent bpf.h from redefining the DLT_ values to their
78 * IFT_ values, as we're going to return the standard libpcap
79 * values, not IBM's non-standard IFT_ values.
85 #include <net/if_types.h> /* for IFT_ values */
86 #include <sys/sysconfig.h>
87 #include <sys/device.h>
88 #include <sys/cfgodm.h>
92 #define domakedev makedev64
93 #define getmajor major64
94 #define bpf_hdr bpf_hdr32
96 #define domakedev makedev
97 #define getmajor major
98 #endif /* __64BIT__ */
100 #define BPF_NAME "bpf"
102 #define DRIVER_PATH "/usr/lib/drivers"
103 #define BPF_NODE "/dev/bpf"
104 static int bpfloadedflag
= 0;
105 static int odmlockid
= 0;
107 static int bpf_load(char *errbuf
);
124 #ifdef HAVE_NET_IF_MEDIA_H
125 # include <net/if_media.h>
128 #include "pcap-int.h"
130 #ifdef HAVE_OS_PROTO_H
131 #include "os-proto.h"
135 * Later versions of NetBSD stick padding in front of FDDI frames
136 * to align the IP header on a 4-byte boundary.
138 #if defined(__NetBSD__) && __NetBSD_Version__ > 106000000
139 #define PCAP_FDDIPAD 3
143 * Private data for capturing on BPF devices.
150 #ifdef HAVE_ZEROCOPY_BPF
152 * Zero-copy read buffer -- for zero-copy BPF. 'buffer' above will
153 * alternative between these two actual mmap'd buffers as required.
154 * As there is a header on the front size of the mmap'd buffer, only
155 * some of the buffer is exposed to libpcap as a whole via bufsize;
156 * zbufsize is the true size. zbuffer tracks the current zbuf
157 * assocated with buffer so that it can be used to decide which the
158 * next buffer to read will be.
160 u_char
*zbuf1
, *zbuf2
, *zbuffer
;
164 struct timespec firstsel
;
166 * If there's currently a buffer being actively processed, then it is
167 * referenced here; 'buffer' is also pointed at it, but offset by the
168 * size of the header.
170 struct bpf_zbuf_header
*bzh
;
171 int nonblock
; /* true if in nonblocking mode */
172 #endif /* HAVE_ZEROCOPY_BPF */
174 char *device
; /* device name */
175 int filtering_in_kernel
; /* using kernel filter */
176 int must_do_on_close
; /* stuff we must do when we close */
180 * Stuff to do when we close.
182 #define MUST_CLEAR_RFMON 0x00000001 /* clear rfmon (monitor) mode */
183 #define MUST_DESTROY_USBUS 0x00000002 /* destroy usbusN interface */
186 # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
187 #define HAVE_BSD_IEEE80211
190 # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
191 static int find_802_11(struct bpf_dltlist
*);
193 # ifdef HAVE_BSD_IEEE80211
194 static int monitor_mode(pcap_t
*, int);
197 # if defined(__APPLE__)
198 static void remove_en(pcap_t
*);
199 static void remove_802_11(pcap_t
*);
202 # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
204 #endif /* BIOCGDLTLIST */
206 #if defined(sun) && defined(LIFNAMSIZ) && defined(lifr_zoneid)
211 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
212 * don't get DLT_DOCSIS defined.
215 #define DLT_DOCSIS 143
219 * On OS X, we don't even get any of the 802.11-plus-radio-header DLT_'s
220 * defined, even though some of them are used by various Airport drivers.
222 #ifndef DLT_PRISM_HEADER
223 #define DLT_PRISM_HEADER 119
225 #ifndef DLT_AIRONET_HEADER
226 #define DLT_AIRONET_HEADER 120
228 #ifndef DLT_IEEE802_11_RADIO
229 #define DLT_IEEE802_11_RADIO 127
231 #ifndef DLT_IEEE802_11_RADIO_AVS
232 #define DLT_IEEE802_11_RADIO_AVS 163
235 static int pcap_can_set_rfmon_bpf(pcap_t
*p
);
236 static int pcap_activate_bpf(pcap_t
*p
);
237 static int pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
);
238 static int pcap_setdirection_bpf(pcap_t
*, pcap_direction_t
);
239 static int pcap_set_datalink_bpf(pcap_t
*p
, int dlt
);
242 * For zerocopy bpf, the setnonblock/getnonblock routines need to modify
243 * pb->nonblock so we don't call select(2) if the pcap handle is in non-
247 pcap_getnonblock_bpf(pcap_t
*p
, char *errbuf
)
249 #ifdef HAVE_ZEROCOPY_BPF
250 struct pcap_bpf
*pb
= p
->priv
;
253 return (pb
->nonblock
);
255 return (pcap_getnonblock_fd(p
, errbuf
));
259 pcap_setnonblock_bpf(pcap_t
*p
, int nonblock
, char *errbuf
)
261 #ifdef HAVE_ZEROCOPY_BPF
262 struct pcap_bpf
*pb
= p
->priv
;
265 pb
->nonblock
= nonblock
;
269 return (pcap_setnonblock_fd(p
, nonblock
, errbuf
));
272 #ifdef HAVE_ZEROCOPY_BPF
274 * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
275 * shared memory buffers.
277 * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer,
278 * and set up p->buffer and cc to reflect one if available. Notice that if
279 * there was no prior buffer, we select zbuf1 as this will be the first
280 * buffer filled for a fresh BPF session.
283 pcap_next_zbuf_shm(pcap_t
*p
, int *cc
)
285 struct pcap_bpf
*pb
= p
->priv
;
286 struct bpf_zbuf_header
*bzh
;
288 if (pb
->zbuffer
== pb
->zbuf2
|| pb
->zbuffer
== NULL
) {
289 bzh
= (struct bpf_zbuf_header
*)pb
->zbuf1
;
290 if (bzh
->bzh_user_gen
!=
291 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
293 pb
->zbuffer
= (u_char
*)pb
->zbuf1
;
294 p
->buffer
= pb
->zbuffer
+ sizeof(*bzh
);
295 *cc
= bzh
->bzh_kernel_len
;
298 } else if (pb
->zbuffer
== pb
->zbuf1
) {
299 bzh
= (struct bpf_zbuf_header
*)pb
->zbuf2
;
300 if (bzh
->bzh_user_gen
!=
301 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
303 pb
->zbuffer
= (u_char
*)pb
->zbuf2
;
304 p
->buffer
= pb
->zbuffer
+ sizeof(*bzh
);
305 *cc
= bzh
->bzh_kernel_len
;
314 * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
315 * select() for data or a timeout, and possibly force rotation of the buffer
316 * in the event we time out or are in immediate mode. Invoke the shared
317 * memory check before doing system calls in order to avoid doing avoidable
321 pcap_next_zbuf(pcap_t
*p
, int *cc
)
323 struct pcap_bpf
*pb
= p
->priv
;
331 #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000))
333 * Start out by seeing whether anything is waiting by checking the
334 * next shared memory buffer for data.
336 data
= pcap_next_zbuf_shm(p
, cc
);
340 * If a previous sleep was interrupted due to signal delivery, make
341 * sure that the timeout gets adjusted accordingly. This requires
342 * that we analyze when the timeout should be been expired, and
343 * subtract the current time from that. If after this operation,
344 * our timeout is less then or equal to zero, handle it like a
347 tmout
= p
->opt
.timeout
;
349 (void) clock_gettime(CLOCK_MONOTONIC
, &cur
);
350 if (pb
->interrupted
&& p
->opt
.timeout
) {
351 expire
= TSTOMILLI(&pb
->firstsel
) + p
->opt
.timeout
;
352 tmout
= expire
- TSTOMILLI(&cur
);
356 data
= pcap_next_zbuf_shm(p
, cc
);
359 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
360 (void) pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
361 "BIOCROTZBUF: %s", strerror(errno
));
364 return (pcap_next_zbuf_shm(p
, cc
));
368 * No data in the buffer, so must use select() to wait for data or
369 * the next timeout. Note that we only call select if the handle
370 * is in blocking mode.
374 FD_SET(p
->fd
, &r_set
);
376 tv
.tv_sec
= tmout
/ 1000;
377 tv
.tv_usec
= (tmout
* 1000) % 1000000;
379 r
= select(p
->fd
+ 1, &r_set
, NULL
, NULL
,
380 p
->opt
.timeout
!= 0 ? &tv
: NULL
);
381 if (r
< 0 && errno
== EINTR
) {
382 if (!pb
->interrupted
&& p
->opt
.timeout
) {
388 (void) pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
389 "select: %s", strerror(errno
));
395 * Check again for data, which may exist now that we've either been
396 * woken up as a result of data or timed out. Try the "there's data"
397 * case first since it doesn't require a system call.
399 data
= pcap_next_zbuf_shm(p
, cc
);
403 * Try forcing a buffer rotation to dislodge timed out or immediate
406 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
407 (void) pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
408 "BIOCROTZBUF: %s", strerror(errno
));
411 return (pcap_next_zbuf_shm(p
, cc
));
415 * Notify kernel that we are done with the buffer. We don't reset zbuffer so
416 * that we know which buffer to use next time around.
419 pcap_ack_zbuf(pcap_t
*p
)
421 struct pcap_bpf
*pb
= p
->priv
;
423 atomic_store_rel_int(&pb
->bzh
->bzh_user_gen
,
424 pb
->bzh
->bzh_kernel_gen
);
429 #endif /* HAVE_ZEROCOPY_BPF */
432 pcap_create_interface(const char *device _U_
, char *ebuf
)
436 p
= pcap_create_common(ebuf
, sizeof (struct pcap_bpf
));
440 p
->activate_op
= pcap_activate_bpf
;
441 p
->can_set_rfmon_op
= pcap_can_set_rfmon_bpf
;
446 * On success, returns a file descriptor for a BPF device.
447 * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf.
450 bpf_open(char *errbuf
)
453 #ifdef HAVE_CLONING_BPF
454 static const char device
[] = "/dev/bpf";
457 char device
[sizeof "/dev/bpf0000000000"];
462 * Load the bpf driver, if it isn't already loaded,
463 * and create the BPF device entries, if they don't
466 if (bpf_load(errbuf
) == PCAP_ERROR
)
470 #ifdef HAVE_CLONING_BPF
471 if ((fd
= open(device
, O_RDWR
)) == -1 &&
472 (errno
!= EACCES
|| (fd
= open(device
, O_RDONLY
)) == -1)) {
474 fd
= PCAP_ERROR_PERM_DENIED
;
477 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
478 "(cannot open device) %s: %s", device
, pcap_strerror(errno
));
482 * Go through all the minors and find one that isn't in use.
485 (void)pcap_snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
487 * Initially try a read/write open (to allow the inject
488 * method to work). If that fails due to permission
489 * issues, fall back to read-only. This allows a
490 * non-root user to be granted specific access to pcap
491 * capabilities via file permissions.
493 * XXX - we should have an API that has a flag that
494 * controls whether to open read-only or read-write,
495 * so that denial of permission to send (or inability
496 * to send, if sending packets isn't supported on
497 * the device in question) can be indicated at open
500 fd
= open(device
, O_RDWR
);
501 if (fd
== -1 && errno
== EACCES
)
502 fd
= open(device
, O_RDONLY
);
503 } while (fd
< 0 && errno
== EBUSY
);
506 * XXX better message for all minors used
515 * /dev/bpf0 doesn't exist, which
516 * means we probably have no BPF
519 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
520 "(there are no BPF devices)");
523 * We got EBUSY on at least one
524 * BPF device, so we have BPF
525 * devices, but all the ones
526 * that exist are busy.
528 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
529 "(all BPF devices are busy)");
535 * Got EACCES on the last device we tried,
536 * and EBUSY on all devices before that,
539 fd
= PCAP_ERROR_PERM_DENIED
;
540 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
541 "(cannot open BPF device) %s: %s", device
,
542 pcap_strerror(errno
));
547 * Some other problem.
550 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
551 "(cannot open BPF device) %s: %s", device
,
552 pcap_strerror(errno
));
562 * Open and bind to a device; used if we're not actually going to use
563 * the device, but are just testing whether it can be opened, or opening
564 * it to get information about it.
566 * Returns an error code on failure (always negative), and an FD for
567 * the now-bound BPF device on success (always non-negative).
570 bpf_open_and_bind(const char *name
, char *errbuf
)
575 fd
= bpf_open(errbuf
);
577 return (fd
); /* fd is the appropriate error code */
580 * Now bind to the device.
582 (void)strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
583 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
588 * There's no such device.
591 return (PCAP_ERROR_NO_SUCH_DEVICE
);
595 * Return a "network down" indication, so that
596 * the application can report that rather than
597 * saying we had a mysterious failure and
598 * suggest that they report a problem to the
599 * libpcap developers.
602 return (PCAP_ERROR_IFACE_NOT_UP
);
605 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
606 "BIOCSETIF: %s: %s", name
, pcap_strerror(errno
));
620 get_dlt_list(int fd
, int v
, struct bpf_dltlist
*bdlp
, char *ebuf
)
622 memset(bdlp
, 0, sizeof(*bdlp
));
623 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) == 0) {
627 bdlp
->bfl_list
= (u_int
*) malloc(sizeof(u_int
) * (bdlp
->bfl_len
+ 1));
628 if (bdlp
->bfl_list
== NULL
) {
629 (void)pcap_snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
630 pcap_strerror(errno
));
634 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) < 0) {
635 (void)pcap_snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
636 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
637 free(bdlp
->bfl_list
);
642 * OK, for real Ethernet devices, add DLT_DOCSIS to the
643 * list, so that an application can let you choose it,
644 * in case you're capturing DOCSIS traffic that a Cisco
645 * Cable Modem Termination System is putting out onto
646 * an Ethernet (it doesn't put an Ethernet header onto
647 * the wire, it puts raw DOCSIS frames out on the wire
648 * inside the low-level Ethernet framing).
650 * A "real Ethernet device" is defined here as a device
651 * that has a link-layer type of DLT_EN10MB and that has
652 * no alternate link-layer types; that's done to exclude
653 * 802.11 interfaces (which might or might not be the
654 * right thing to do, but I suspect it is - Ethernet <->
655 * 802.11 bridges would probably badly mishandle frames
656 * that don't have Ethernet headers).
658 * On Solaris with BPF, Ethernet devices also offer
659 * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
660 * treat it as an indication that the device isn't an
663 if (v
== DLT_EN10MB
) {
665 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
666 if (bdlp
->bfl_list
[i
] != DLT_EN10MB
668 && bdlp
->bfl_list
[i
] != DLT_IPNET
677 * We reserved one more slot at the end of
680 bdlp
->bfl_list
[bdlp
->bfl_len
] = DLT_DOCSIS
;
686 * EINVAL just means "we don't support this ioctl on
687 * this device"; don't treat it as an error.
689 if (errno
!= EINVAL
) {
690 (void)pcap_snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
691 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
700 pcap_can_set_rfmon_bpf(pcap_t
*p
)
702 #if defined(__APPLE__)
703 struct utsname osinfo
;
707 struct bpf_dltlist bdl
;
711 * The joys of monitor mode on OS X.
713 * Prior to 10.4, it's not supported at all.
715 * In 10.4, if adapter enN supports monitor mode, there's a
716 * wltN adapter corresponding to it; you open it, instead of
717 * enN, to get monitor mode. You get whatever link-layer
718 * headers it supplies.
720 * In 10.5, and, we assume, later releases, if adapter enN
721 * supports monitor mode, it offers, among its selectable
722 * DLT_ values, values that let you get the 802.11 header;
723 * selecting one of those values puts the adapter into monitor
724 * mode (i.e., you can't get 802.11 headers except in monitor
725 * mode, and you can't get Ethernet headers in monitor mode).
727 if (uname(&osinfo
) == -1) {
729 * Can't get the OS version; just say "no".
734 * We assume osinfo.sysname is "Darwin", because
735 * __APPLE__ is defined. We just check the version.
737 if (osinfo
.release
[0] < '8' && osinfo
.release
[1] == '.') {
739 * 10.3 (Darwin 7.x) or earlier.
740 * Monitor mode not supported.
744 if (osinfo
.release
[0] == '8' && osinfo
.release
[1] == '.') {
746 * 10.4 (Darwin 8.x). s/en/wlt/, and check
747 * whether the device exists.
749 if (strncmp(p
->opt
.device
, "en", 2) != 0) {
751 * Not an enN device; no monitor mode.
755 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
757 (void)pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
758 "socket: %s", pcap_strerror(errno
));
761 strlcpy(ifr
.ifr_name
, "wlt", sizeof(ifr
.ifr_name
));
762 strlcat(ifr
.ifr_name
, p
->opt
.device
+ 2, sizeof(ifr
.ifr_name
));
763 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
776 * Everything else is 10.5 or later; for those,
777 * we just open the enN device, and check whether
778 * we have any 802.11 devices.
780 * First, open a BPF device.
782 fd
= bpf_open(p
->errbuf
);
784 return (fd
); /* fd is the appropriate error code */
787 * Now bind to the device.
789 (void)strncpy(ifr
.ifr_name
, p
->opt
.device
, sizeof(ifr
.ifr_name
));
790 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
795 * There's no such device.
798 return (PCAP_ERROR_NO_SUCH_DEVICE
);
802 * Return a "network down" indication, so that
803 * the application can report that rather than
804 * saying we had a mysterious failure and
805 * suggest that they report a problem to the
806 * libpcap developers.
809 return (PCAP_ERROR_IFACE_NOT_UP
);
812 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
814 p
->opt
.device
, pcap_strerror(errno
));
821 * We know the default link type -- now determine all the DLTs
822 * this interface supports. If this fails with EINVAL, it's
823 * not fatal; we just don't get to use the feature later.
824 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
825 * as the default DLT for this adapter.)
827 if (get_dlt_list(fd
, DLT_NULL
, &bdl
, p
->errbuf
) == PCAP_ERROR
) {
831 if (find_802_11(&bdl
) != -1) {
833 * We have an 802.11 DLT, so we can set monitor mode.
841 #endif /* BIOCGDLTLIST */
843 #elif defined(HAVE_BSD_IEEE80211)
846 ret
= monitor_mode(p
, 0);
847 if (ret
== PCAP_ERROR_RFMON_NOTSUP
)
848 return (0); /* not an error, just a "can't do" */
850 return (1); /* success */
858 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
863 * "ps_recv" counts packets handed to the filter, not packets
864 * that passed the filter. This includes packets later dropped
865 * because we ran out of buffer space.
867 * "ps_drop" counts packets dropped inside the BPF device
868 * because we ran out of buffer space. It doesn't count
869 * packets dropped by the interface driver. It counts
870 * only packets that passed the filter.
872 * Both statistics include packets not yet read from the kernel
873 * by libpcap, and thus not yet seen by the application.
875 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
876 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
877 pcap_strerror(errno
));
881 ps
->ps_recv
= s
.bs_recv
;
882 ps
->ps_drop
= s
.bs_drop
;
888 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
890 struct pcap_bpf
*pb
= p
->priv
;
893 register u_char
*bp
, *ep
;
898 #ifdef HAVE_ZEROCOPY_BPF
904 * Has "pcap_breakloop()" been called?
908 * Yes - clear the flag that indicates that it
909 * has, and return PCAP_ERROR_BREAK to indicate
910 * that we were told to break out of the loop.
913 return (PCAP_ERROR_BREAK
);
918 * When reading without zero-copy from a file descriptor, we
919 * use a single buffer and return a length of data in the
920 * buffer. With zero-copy, we update the p->buffer pointer
921 * to point at whatever underlying buffer contains the next
922 * data and update cc to reflect the data found in the
925 #ifdef HAVE_ZEROCOPY_BPF
927 if (p
->buffer
!= NULL
)
929 i
= pcap_next_zbuf(p
, &cc
);
937 cc
= read(p
->fd
, p
->buffer
, p
->bufsize
);
940 /* Don't choke when we get ptraced */
949 * Sigh. More AIX wonderfulness.
951 * For some unknown reason the uiomove()
952 * operation in the bpf kernel extension
953 * used to copy the buffer into user
954 * space sometimes returns EFAULT. I have
955 * no idea why this is the case given that
956 * a kernel debugger shows the user buffer
957 * is correct. This problem appears to
958 * be mostly mitigated by the memset of
959 * the buffer before it is first used.
960 * Very strange.... Shaun Clowes
962 * In any case this means that we shouldn't
963 * treat EFAULT as a fatal error; as we
964 * don't have an API for returning
965 * a "some packets were dropped since
966 * the last packet you saw" indication,
967 * we just ignore EFAULT and keep reading.
977 * The device on which we're capturing
980 * XXX - we should really return
981 * PCAP_ERROR_IFACE_NOT_UP, but
982 * pcap_dispatch() etc. aren't
983 * defined to retur that.
985 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
986 "The interface went down");
989 #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
991 * Due to a SunOS bug, after 2^31 bytes, the kernel
992 * file offset overflows and read fails with EINVAL.
993 * The lseek() to 0 will fix things.
996 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
998 (void)lseek(p
->fd
, 0L, SEEK_SET
);
1004 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
1005 pcap_strerror(errno
));
1006 return (PCAP_ERROR
);
1008 bp
= (u_char
*)p
->buffer
;
1013 * Loop through each packet.
1015 #define bhp ((struct bpf_hdr *)bp)
1021 register int caplen
, hdrlen
;
1024 * Has "pcap_breakloop()" been called?
1025 * If so, return immediately - if we haven't read any
1026 * packets, clear the flag and return PCAP_ERROR_BREAK
1027 * to indicate that we were told to break out of the loop,
1028 * otherwise leave the flag set, so that the *next* call
1029 * will break out of the loop without having read any
1030 * packets, and return the number of packets we've
1033 if (p
->break_loop
) {
1037 * ep is set based on the return value of read(),
1038 * but read() from a BPF device doesn't necessarily
1039 * return a value that's a multiple of the alignment
1040 * value for BPF_WORDALIGN(). However, whenever we
1041 * increment bp, we round up the increment value by
1042 * a value rounded up by BPF_WORDALIGN(), so we
1043 * could increment bp past ep after processing the
1044 * last packet in the buffer.
1046 * We treat ep < bp as an indication that this
1047 * happened, and just set p->cc to 0.
1053 return (PCAP_ERROR_BREAK
);
1058 caplen
= bhp
->bh_caplen
;
1059 hdrlen
= bhp
->bh_hdrlen
;
1060 datap
= bp
+ hdrlen
;
1062 * Short-circuit evaluation: if using BPF filter
1063 * in kernel, no need to do it now - we already know
1064 * the packet passed the filter.
1067 * Note: the filter code was generated assuming
1068 * that p->fddipad was the amount of padding
1069 * before the header, as that's what's required
1070 * in the kernel, so we run the filter before
1071 * skipping that padding.
1074 if (pb
->filtering_in_kernel
||
1075 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
1076 struct pcap_pkthdr pkthdr
;
1078 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
1081 * AIX's BPF returns seconds/nanoseconds time
1082 * stamps, not seconds/microseconds time stamps.
1084 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
1086 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
1090 pkthdr
.caplen
= caplen
- pad
;
1093 if (bhp
->bh_datalen
> pad
)
1094 pkthdr
.len
= bhp
->bh_datalen
- pad
;
1099 pkthdr
.caplen
= caplen
;
1100 pkthdr
.len
= bhp
->bh_datalen
;
1102 (*callback
)(user
, &pkthdr
, datap
);
1103 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1104 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
1108 * See comment above about p->cc < 0.
1118 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1127 pcap_inject_bpf(pcap_t
*p
, const void *buf
, size_t size
)
1131 ret
= write(p
->fd
, buf
, size
);
1133 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
1135 * In Mac OS X, there's a bug wherein setting the
1136 * BIOCSHDRCMPLT flag causes writes to fail; see,
1139 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
1141 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
1142 * assume it's due to that bug, and turn off that flag
1143 * and try again. If we succeed, it either means that
1144 * somebody applied the fix from that URL, or other patches
1147 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
1149 * and are running a Darwin kernel with those fixes, or
1150 * that Apple fixed the problem in some OS X release.
1152 u_int spoof_eth_src
= 0;
1154 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1155 (void)pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1156 "send: can't turn off BIOCSHDRCMPLT: %s",
1157 pcap_strerror(errno
));
1158 return (PCAP_ERROR
);
1162 * Now try the write again.
1164 ret
= write(p
->fd
, buf
, size
);
1166 #endif /* __APPLE__ */
1168 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
1169 pcap_strerror(errno
));
1170 return (PCAP_ERROR
);
1177 bpf_odminit(char *errbuf
)
1181 if (odm_initialize() == -1) {
1182 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1183 errstr
= "Unknown error";
1184 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1185 "bpf_load: odm_initialize failed: %s",
1187 return (PCAP_ERROR
);
1190 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
1191 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1192 errstr
= "Unknown error";
1193 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1194 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
1196 (void)odm_terminate();
1197 return (PCAP_ERROR
);
1204 bpf_odmcleanup(char *errbuf
)
1208 if (odm_unlock(odmlockid
) == -1) {
1209 if (errbuf
!= NULL
) {
1210 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1211 errstr
= "Unknown error";
1212 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1213 "bpf_load: odm_unlock failed: %s",
1216 return (PCAP_ERROR
);
1219 if (odm_terminate() == -1) {
1220 if (errbuf
!= NULL
) {
1221 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1222 errstr
= "Unknown error";
1223 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1224 "bpf_load: odm_terminate failed: %s",
1227 return (PCAP_ERROR
);
1234 bpf_load(char *errbuf
)
1238 int numminors
, i
, rc
;
1241 struct bpf_config cfg_bpf
;
1242 struct cfg_load cfg_ld
;
1243 struct cfg_kmod cfg_km
;
1246 * This is very very close to what happens in the real implementation
1247 * but I've fixed some (unlikely) bug situations.
1252 if (bpf_odminit(errbuf
) == PCAP_ERROR
)
1253 return (PCAP_ERROR
);
1255 major
= genmajor(BPF_NAME
);
1257 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1258 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
1259 (void)bpf_odmcleanup(NULL
);
1260 return (PCAP_ERROR
);
1263 minors
= getminor(major
, &numminors
, BPF_NAME
);
1265 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
1267 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1268 "bpf_load: genminor failed: %s",
1269 pcap_strerror(errno
));
1270 (void)bpf_odmcleanup(NULL
);
1271 return (PCAP_ERROR
);
1275 if (bpf_odmcleanup(errbuf
) == PCAP_ERROR
)
1276 return (PCAP_ERROR
);
1278 rc
= stat(BPF_NODE
"0", &sbuf
);
1279 if (rc
== -1 && errno
!= ENOENT
) {
1280 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1281 "bpf_load: can't stat %s: %s",
1282 BPF_NODE
"0", pcap_strerror(errno
));
1283 return (PCAP_ERROR
);
1286 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
1287 for (i
= 0; i
< BPF_MINORS
; i
++) {
1288 sprintf(buf
, "%s%d", BPF_NODE
, i
);
1290 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
1291 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1292 "bpf_load: can't mknod %s: %s",
1293 buf
, pcap_strerror(errno
));
1294 return (PCAP_ERROR
);
1299 /* Check if the driver is loaded */
1300 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
1302 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
1303 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
1304 (cfg_ld
.kmid
== 0)) {
1305 /* Driver isn't loaded, load it now */
1306 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
1307 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1308 "bpf_load: could not load driver: %s",
1310 return (PCAP_ERROR
);
1314 /* Configure the driver */
1315 cfg_km
.cmd
= CFG_INIT
;
1316 cfg_km
.kmid
= cfg_ld
.kmid
;
1317 cfg_km
.mdilen
= sizeof(cfg_bpf
);
1318 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
1319 for (i
= 0; i
< BPF_MINORS
; i
++) {
1320 cfg_bpf
.devno
= domakedev(major
, i
);
1321 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
1322 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1323 "bpf_load: could not configure driver: %s",
1325 return (PCAP_ERROR
);
1336 * Undo any operations done when opening the device when necessary.
1339 pcap_cleanup_bpf(pcap_t
*p
)
1341 struct pcap_bpf
*pb
= p
->priv
;
1342 #ifdef HAVE_BSD_IEEE80211
1344 struct ifmediareq req
;
1348 if (pb
->must_do_on_close
!= 0) {
1350 * There's something we have to do when closing this
1353 #ifdef HAVE_BSD_IEEE80211
1354 if (pb
->must_do_on_close
& MUST_CLEAR_RFMON
) {
1356 * We put the interface into rfmon mode;
1357 * take it out of rfmon mode.
1359 * XXX - if somebody else wants it in rfmon
1360 * mode, this code cannot know that, so it'll take
1361 * it out of rfmon mode.
1363 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
1366 "Can't restore interface flags (socket() failed: %s).\n"
1367 "Please adjust manually.\n",
1370 memset(&req
, 0, sizeof(req
));
1371 strncpy(req
.ifm_name
, pb
->device
,
1372 sizeof(req
.ifm_name
));
1373 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1375 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
1376 "Please adjust manually.\n",
1379 if (req
.ifm_current
& IFM_IEEE80211_MONITOR
) {
1381 * Rfmon mode is currently on;
1384 memset(&ifr
, 0, sizeof(ifr
));
1385 (void)strncpy(ifr
.ifr_name
,
1387 sizeof(ifr
.ifr_name
));
1389 req
.ifm_current
& ~IFM_IEEE80211_MONITOR
;
1390 if (ioctl(sock
, SIOCSIFMEDIA
,
1393 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
1394 "Please adjust manually.\n",
1402 #endif /* HAVE_BSD_IEEE80211 */
1404 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
1406 * Attempt to destroy the usbusN interface that we created.
1408 if (pb
->must_do_on_close
& MUST_DESTROY_USBUS
) {
1409 if (if_nametoindex(pb
->device
) > 0) {
1412 s
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
1414 strlcpy(ifr
.ifr_name
, pb
->device
,
1415 sizeof(ifr
.ifr_name
));
1416 ioctl(s
, SIOCIFDESTROY
, &ifr
);
1421 #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
1423 * Take this pcap out of the list of pcaps for which we
1424 * have to take the interface out of some mode.
1426 pcap_remove_from_pcaps_to_close(p
);
1427 pb
->must_do_on_close
= 0;
1430 #ifdef HAVE_ZEROCOPY_BPF
1433 * Delete the mappings. Note that p->buffer gets
1434 * initialized to one of the mmapped regions in
1435 * this case, so do not try and free it directly;
1436 * null it out so that pcap_cleanup_live_common()
1437 * doesn't try to free it.
1439 if (pb
->zbuf1
!= MAP_FAILED
&& pb
->zbuf1
!= NULL
)
1440 (void) munmap(pb
->zbuf1
, pb
->zbufsize
);
1441 if (pb
->zbuf2
!= MAP_FAILED
&& pb
->zbuf2
!= NULL
)
1442 (void) munmap(pb
->zbuf2
, pb
->zbufsize
);
1446 if (pb
->device
!= NULL
) {
1450 pcap_cleanup_live_common(p
);
1454 check_setif_failure(pcap_t
*p
, int error
)
1462 if (error
== ENXIO
) {
1464 * No such device exists.
1467 if (p
->opt
.rfmon
&& strncmp(p
->opt
.device
, "wlt", 3) == 0) {
1469 * Monitor mode was requested, and we're trying
1470 * to open a "wltN" device. Assume that this
1471 * is 10.4 and that we were asked to open an
1472 * "enN" device; if that device exists, return
1473 * "monitor mode not supported on the device".
1475 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1477 strlcpy(ifr
.ifr_name
, "en",
1478 sizeof(ifr
.ifr_name
));
1479 strlcat(ifr
.ifr_name
, p
->opt
.device
+ 3,
1480 sizeof(ifr
.ifr_name
));
1481 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
1483 * We assume this failed because
1484 * the underlying device doesn't
1487 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1488 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1489 "SIOCGIFFLAGS on %s failed: %s",
1490 ifr
.ifr_name
, pcap_strerror(errno
));
1493 * The underlying "enN" device
1494 * exists, but there's no
1495 * corresponding "wltN" device;
1496 * that means that the "enN"
1497 * device doesn't support
1498 * monitor mode, probably because
1499 * it's an Ethernet device rather
1500 * than a wireless device.
1502 err
= PCAP_ERROR_RFMON_NOTSUP
;
1507 * We can't find out whether there's
1508 * an underlying "enN" device, so
1509 * just report "no such device".
1511 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1512 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1513 "socket() failed: %s",
1514 pcap_strerror(errno
));
1522 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF failed: %s",
1523 pcap_strerror(errno
));
1524 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1525 } else if (errno
== ENETDOWN
) {
1527 * Return a "network down" indication, so that
1528 * the application can report that rather than
1529 * saying we had a mysterious failure and
1530 * suggest that they report a problem to the
1531 * libpcap developers.
1533 return (PCAP_ERROR_IFACE_NOT_UP
);
1536 * Some other error; fill in the error string, and
1537 * return PCAP_ERROR.
1539 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1540 p
->opt
.device
, pcap_strerror(errno
));
1541 return (PCAP_ERROR
);
1546 * Default capture buffer size.
1547 * 32K isn't very much for modern machines with fast networks; we
1548 * pick .5M, as that's the maximum on at least some systems with BPF.
1550 * However, on AIX 3.5, the larger buffer sized caused unrecoverable
1551 * read failures under stress, so we leave it as 32K; yet another
1552 * place where AIX's BPF is broken.
1555 #define DEFAULT_BUFSIZE 32768
1557 #define DEFAULT_BUFSIZE 524288
1561 pcap_activate_bpf(pcap_t
*p
)
1563 struct pcap_bpf
*pb
= p
->priv
;
1565 #ifdef HAVE_BSD_IEEE80211
1572 char *ifrname
= ifr
.lifr_name
;
1573 const size_t ifnamsiz
= sizeof(ifr
.lifr_name
);
1576 char *ifrname
= ifr
.ifr_name
;
1577 const size_t ifnamsiz
= sizeof(ifr
.ifr_name
);
1579 struct bpf_version bv
;
1582 char *wltdev
= NULL
;
1585 struct bpf_dltlist bdl
;
1586 #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1589 #endif /* BIOCGDLTLIST */
1590 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1591 u_int spoof_eth_src
= 1;
1594 struct bpf_insn total_insn
;
1595 struct bpf_program total_prog
;
1596 struct utsname osinfo
;
1597 int have_osinfo
= 0;
1598 #ifdef HAVE_ZEROCOPY_BPF
1600 u_int bufmode
, zbufmax
;
1603 fd
= bpf_open(p
->errbuf
);
1611 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
1612 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
1613 pcap_strerror(errno
));
1614 status
= PCAP_ERROR
;
1617 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
1618 bv
.bv_minor
< BPF_MINOR_VERSION
) {
1619 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1620 "kernel bpf filter out of date");
1621 status
= PCAP_ERROR
;
1625 #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid)
1627 * Retrieve the zoneid of the zone we are currently executing in.
1629 if ((ifr
.lifr_zoneid
= getzoneid()) == -1) {
1630 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "getzoneid(): %s",
1631 pcap_strerror(errno
));
1632 status
= PCAP_ERROR
;
1636 * Check if the given source datalink name has a '/' separated
1637 * zonename prefix string. The zonename prefixed source datalink can
1638 * be used by pcap consumers in the Solaris global zone to capture
1639 * traffic on datalinks in non-global zones. Non-global zones
1640 * do not have access to datalinks outside of their own namespace.
1642 if ((zonesep
= strchr(p
->opt
.device
, '/')) != NULL
) {
1643 char path_zname
[ZONENAME_MAX
];
1647 if (ifr
.lifr_zoneid
!= GLOBAL_ZONEID
) {
1648 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1649 "zonename/linkname only valid in global zone.");
1650 status
= PCAP_ERROR
;
1653 znamelen
= zonesep
- p
->opt
.device
;
1654 (void) strlcpy(path_zname
, p
->opt
.device
, znamelen
+ 1);
1655 ifr
.lifr_zoneid
= getzoneidbyname(path_zname
);
1656 if (ifr
.lifr_zoneid
== -1) {
1657 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1658 "getzoneidbyname(%s): %s", path_zname
,
1659 pcap_strerror(errno
));
1660 status
= PCAP_ERROR
;
1663 lnamep
= strdup(zonesep
+ 1);
1664 if (lnamep
== NULL
) {
1665 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
1666 pcap_strerror(errno
));
1667 status
= PCAP_ERROR
;
1670 free(p
->opt
.device
);
1671 p
->opt
.device
= lnamep
;
1675 pb
->device
= strdup(p
->opt
.device
);
1676 if (pb
->device
== NULL
) {
1677 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
1678 pcap_strerror(errno
));
1679 status
= PCAP_ERROR
;
1684 * Attempt to find out the version of the OS on which we're running.
1686 if (uname(&osinfo
) == 0)
1691 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1692 * of why we check the version number.
1697 * We assume osinfo.sysname is "Darwin", because
1698 * __APPLE__ is defined. We just check the version.
1700 if (osinfo
.release
[0] < '8' &&
1701 osinfo
.release
[1] == '.') {
1703 * 10.3 (Darwin 7.x) or earlier.
1705 status
= PCAP_ERROR_RFMON_NOTSUP
;
1708 if (osinfo
.release
[0] == '8' &&
1709 osinfo
.release
[1] == '.') {
1711 * 10.4 (Darwin 8.x). s/en/wlt/
1713 if (strncmp(p
->opt
.device
, "en", 2) != 0) {
1715 * Not an enN device; check
1716 * whether the device even exists.
1718 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1721 p
->opt
.device
, ifnamsiz
);
1722 if (ioctl(sockfd
, SIOCGIFFLAGS
,
1723 (char *)&ifr
) < 0) {
1731 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1732 pcap_snprintf(p
->errbuf
,
1734 "SIOCGIFFLAGS failed: %s",
1735 pcap_strerror(errno
));
1737 status
= PCAP_ERROR_RFMON_NOTSUP
;
1741 * We can't find out whether
1742 * the device exists, so just
1743 * report "no such device".
1745 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1746 pcap_snprintf(p
->errbuf
,
1748 "socket() failed: %s",
1749 pcap_strerror(errno
));
1753 wltdev
= malloc(strlen(p
->opt
.device
) + 2);
1754 if (wltdev
== NULL
) {
1755 (void)pcap_snprintf(p
->errbuf
,
1756 PCAP_ERRBUF_SIZE
, "malloc: %s",
1757 pcap_strerror(errno
));
1758 status
= PCAP_ERROR
;
1761 strcpy(wltdev
, "wlt");
1762 strcat(wltdev
, p
->opt
.device
+ 2);
1763 free(p
->opt
.device
);
1764 p
->opt
.device
= wltdev
;
1767 * Everything else is 10.5 or later; for those,
1768 * we just open the enN device, and set the DLT.
1772 #endif /* __APPLE__ */
1775 * If this is FreeBSD, and the device name begins with "usbus",
1776 * try to create the interface if it's not available.
1778 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
1779 if (strncmp(p
->opt
.device
, usbus_prefix
, USBUS_PREFIX_LEN
) == 0) {
1781 * Do we already have an interface with that name?
1783 if (if_nametoindex(p
->opt
.device
) == 0) {
1785 * No. We need to create it, and, if we
1786 * succeed, remember that we should destroy
1787 * it when the pcap_t is closed.
1792 * Open a socket to use for ioctls to
1793 * create the interface.
1795 s
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
1797 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1798 "Can't open socket: %s",
1799 pcap_strerror(errno
));
1800 status
= PCAP_ERROR
;
1805 * If we haven't already done so, arrange to have
1806 * "pcap_close_all()" called when we exit.
1808 if (!pcap_do_addexit(p
)) {
1810 * "atexit()" failed; don't create the
1811 * interface, just give up.
1814 status
= PCAP_ERROR
;
1819 * Create the interface.
1821 strlcpy(ifr
.ifr_name
, p
->opt
.device
, sizeof(ifr
.ifr_name
));
1822 if (ioctl(s
, SIOCIFCREATE2
, &ifr
) < 0) {
1823 if (errno
== EINVAL
) {
1824 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1825 "Invalid USB bus interface %s",
1828 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1829 "Can't create interface for %s: %s",
1830 p
->opt
.device
, pcap_strerror(errno
));
1833 status
= PCAP_ERROR
;
1838 * Make sure we clean this up when we close.
1840 pb
->must_do_on_close
|= MUST_DESTROY_USBUS
;
1843 * Add this to the list of pcaps to close when we exit.
1845 pcap_add_to_pcaps_to_close(p
);
1848 #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
1850 #ifdef HAVE_ZEROCOPY_BPF
1852 * If the BPF extension to set buffer mode is present, try setting
1853 * the mode to zero-copy. If that fails, use regular buffering. If
1854 * it succeeds but other setup fails, return an error to the user.
1856 bufmode
= BPF_BUFMODE_ZBUF
;
1857 if (ioctl(fd
, BIOCSETBUFMODE
, (caddr_t
)&bufmode
) == 0) {
1859 * We have zerocopy BPF; use it.
1864 * How to pick a buffer size: first, query the maximum buffer
1865 * size supported by zero-copy. This also lets us quickly
1866 * determine whether the kernel generally supports zero-copy.
1867 * Then, if a buffer size was specified, use that, otherwise
1868 * query the default buffer size, which reflects kernel
1869 * policy for a desired default. Round to the nearest page
1872 if (ioctl(fd
, BIOCGETZMAX
, (caddr_t
)&zbufmax
) < 0) {
1873 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGETZMAX: %s",
1874 pcap_strerror(errno
));
1875 status
= PCAP_ERROR
;
1879 if (p
->opt
.buffer_size
!= 0) {
1881 * A buffer size was explicitly specified; use it.
1883 v
= p
->opt
.buffer_size
;
1885 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1886 v
< DEFAULT_BUFSIZE
)
1887 v
= DEFAULT_BUFSIZE
;
1890 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
1892 pb
->zbufsize
= roundup(v
, getpagesize());
1893 if (pb
->zbufsize
> zbufmax
)
1894 pb
->zbufsize
= zbufmax
;
1895 pb
->zbuf1
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
1897 pb
->zbuf2
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
1899 if (pb
->zbuf1
== MAP_FAILED
|| pb
->zbuf2
== MAP_FAILED
) {
1900 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "mmap: %s",
1901 pcap_strerror(errno
));
1902 status
= PCAP_ERROR
;
1905 memset(&bz
, 0, sizeof(bz
)); /* bzero() deprecated, replaced with memset() */
1906 bz
.bz_bufa
= pb
->zbuf1
;
1907 bz
.bz_bufb
= pb
->zbuf2
;
1908 bz
.bz_buflen
= pb
->zbufsize
;
1909 if (ioctl(fd
, BIOCSETZBUF
, (caddr_t
)&bz
) < 0) {
1910 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETZBUF: %s",
1911 pcap_strerror(errno
));
1912 status
= PCAP_ERROR
;
1915 (void)strncpy(ifrname
, p
->opt
.device
, ifnamsiz
);
1916 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
1917 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1918 p
->opt
.device
, pcap_strerror(errno
));
1919 status
= PCAP_ERROR
;
1922 v
= pb
->zbufsize
- sizeof(struct bpf_zbuf_header
);
1927 * We don't have zerocopy BPF.
1928 * Set the buffer size.
1930 if (p
->opt
.buffer_size
!= 0) {
1932 * A buffer size was explicitly specified; use it.
1934 if (ioctl(fd
, BIOCSBLEN
,
1935 (caddr_t
)&p
->opt
.buffer_size
) < 0) {
1936 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1937 "BIOCSBLEN: %s: %s", p
->opt
.device
,
1938 pcap_strerror(errno
));
1939 status
= PCAP_ERROR
;
1944 * Now bind to the device.
1946 (void)strncpy(ifrname
, p
->opt
.device
, ifnamsiz
);
1948 if (ioctl(fd
, BIOCSETLIF
, (caddr_t
)&ifr
) < 0)
1950 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0)
1953 status
= check_setif_failure(p
, errno
);
1958 * No buffer size was explicitly specified.
1960 * Try finding a good size for the buffer;
1961 * DEFAULT_BUFSIZE may be too big, so keep
1962 * cutting it in half until we find a size
1963 * that works, or run out of sizes to try.
1964 * If the default is larger, don't make it smaller.
1966 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1967 v
< DEFAULT_BUFSIZE
)
1968 v
= DEFAULT_BUFSIZE
;
1969 for ( ; v
!= 0; v
>>= 1) {
1971 * Ignore the return value - this is because the
1972 * call fails on BPF systems that don't have
1973 * kernel malloc. And if the call fails, it's
1974 * no big deal, we just continue to use the
1975 * standard buffer size.
1977 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
1979 (void)strncpy(ifrname
, p
->opt
.device
, ifnamsiz
);
1981 if (ioctl(fd
, BIOCSETLIF
, (caddr_t
)&ifr
) >= 0)
1983 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
1985 break; /* that size worked; we're done */
1987 if (errno
!= ENOBUFS
) {
1988 status
= check_setif_failure(p
, errno
);
1994 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1995 "BIOCSBLEN: %s: No buffer size worked",
1997 status
= PCAP_ERROR
;
2003 /* Get the data link layer type. */
2004 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
2005 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
2006 pcap_strerror(errno
));
2007 status
= PCAP_ERROR
;
2013 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
2036 * We don't know what to map this to yet.
2038 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
2040 status
= PCAP_ERROR
;
2044 #if _BSDI_VERSION - 0 >= 199510
2045 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
2060 case 12: /*DLT_C_HDLC*/
2068 * We know the default link type -- now determine all the DLTs
2069 * this interface supports. If this fails with EINVAL, it's
2070 * not fatal; we just don't get to use the feature later.
2072 if (get_dlt_list(fd
, v
, &bdl
, p
->errbuf
) == -1) {
2073 status
= PCAP_ERROR
;
2076 p
->dlt_count
= bdl
.bfl_len
;
2077 p
->dlt_list
= bdl
.bfl_list
;
2081 * Monitor mode fun, continued.
2083 * For 10.5 and, we're assuming, later releases, as noted above,
2084 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
2085 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
2086 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
2089 * Therefore, if the user asked for monitor mode, we filter out
2090 * the DLT_EN10MB value, as you can't get that in monitor mode,
2091 * and, if the user didn't ask for monitor mode, we filter out
2092 * the 802.11 DLT_ values, because selecting those will turn
2093 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
2094 * radio DLT_ value is offered, we try to select that, otherwise
2095 * we try to select DLT_IEEE802_11.
2098 if (isdigit((unsigned)osinfo
.release
[0]) &&
2099 (osinfo
.release
[0] == '9' ||
2100 isdigit((unsigned)osinfo
.release
[1]))) {
2102 * 10.5 (Darwin 9.x), or later.
2104 new_dlt
= find_802_11(&bdl
);
2105 if (new_dlt
!= -1) {
2107 * We have at least one 802.11 DLT_ value,
2108 * so this is an 802.11 interface.
2109 * new_dlt is the best of the 802.11
2110 * DLT_ values in the list.
2114 * Our caller wants monitor mode.
2115 * Purge DLT_EN10MB from the list
2116 * of link-layer types, as selecting
2117 * it will keep monitor mode off.
2122 * If the new mode we want isn't
2123 * the default mode, attempt to
2124 * select the new mode.
2127 if (ioctl(p
->fd
, BIOCSDLT
,
2139 * Our caller doesn't want
2140 * monitor mode. Unless this
2141 * is being done by pcap_open_live(),
2142 * purge the 802.11 link-layer types
2143 * from the list, as selecting
2144 * one of them will turn monitor
2153 * The caller requested monitor
2154 * mode, but we have no 802.11
2155 * link-layer types, so they
2158 status
= PCAP_ERROR_RFMON_NOTSUP
;
2164 #elif defined(HAVE_BSD_IEEE80211)
2166 * *BSD with the new 802.11 ioctls.
2167 * Do we want monitor mode?
2171 * Try to put the interface into monitor mode.
2173 retv
= monitor_mode(p
, 1);
2183 * We're in monitor mode.
2184 * Try to find the best 802.11 DLT_ value and, if we
2185 * succeed, try to switch to that mode if we're not
2186 * already in that mode.
2188 new_dlt
= find_802_11(&bdl
);
2189 if (new_dlt
!= -1) {
2191 * We have at least one 802.11 DLT_ value.
2192 * new_dlt is the best of the 802.11
2193 * DLT_ values in the list.
2195 * If the new mode we want isn't the default mode,
2196 * attempt to select the new mode.
2199 if (ioctl(p
->fd
, BIOCSDLT
, &new_dlt
) != -1) {
2201 * We succeeded; make this the
2209 #endif /* various platforms */
2210 #endif /* BIOCGDLTLIST */
2213 * If this is an Ethernet device, and we don't have a DLT_ list,
2214 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
2215 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
2216 * do, but there's not much we can do about that without finding
2217 * some other way of determining whether it's an Ethernet or 802.11
2220 if (v
== DLT_EN10MB
&& p
->dlt_count
== 0) {
2221 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
2223 * If that fails, just leave the list empty.
2225 if (p
->dlt_list
!= NULL
) {
2226 p
->dlt_list
[0] = DLT_EN10MB
;
2227 p
->dlt_list
[1] = DLT_DOCSIS
;
2233 p
->fddipad
= PCAP_FDDIPAD
;
2239 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
2241 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
2242 * the link-layer source address isn't forcibly overwritten.
2243 * (Should we ignore errors? Should we do this only if
2244 * we're open for writing?)
2246 * XXX - I seem to remember some packet-sending bug in some
2247 * BSDs - check CVS log for "bpf.c"?
2249 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
2250 (void)pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2251 "BIOCSHDRCMPLT: %s", pcap_strerror(errno
));
2252 status
= PCAP_ERROR
;
2257 #ifdef HAVE_ZEROCOPY_BPF
2259 * In zero-copy mode, we just use the timeout in select().
2260 * XXX - what if we're in non-blocking mode and the *application*
2261 * is using select() or poll() or kqueues or....?
2263 if (p
->opt
.timeout
&& !pb
->zerocopy
) {
2265 if (p
->opt
.timeout
) {
2268 * XXX - is this seconds/nanoseconds in AIX?
2269 * (Treating it as such doesn't fix the timeout
2270 * problem described below.)
2272 * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
2273 * 64-bit userland - it takes, as an argument, a
2274 * "struct BPF_TIMEVAL", which has 32-bit tv_sec
2275 * and tv_usec, rather than a "struct timeval".
2277 * If this platform defines "struct BPF_TIMEVAL",
2278 * we check whether the structure size in BIOCSRTIMEOUT
2279 * is that of a "struct timeval" and, if not, we use
2280 * a "struct BPF_TIMEVAL" rather than a "struct timeval".
2281 * (That way, if the bug is fixed in a future release,
2282 * we will still do the right thing.)
2285 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2286 struct BPF_TIMEVAL bpf_to
;
2288 if (IOCPARM_LEN(BIOCSRTIMEOUT
) != sizeof(struct timeval
)) {
2289 bpf_to
.tv_sec
= p
->opt
.timeout
/ 1000;
2290 bpf_to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2291 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&bpf_to
) < 0) {
2292 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2293 "BIOCSRTIMEOUT: %s", pcap_strerror(errno
));
2294 status
= PCAP_ERROR
;
2299 to
.tv_sec
= p
->opt
.timeout
/ 1000;
2300 to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2301 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
2302 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2303 "BIOCSRTIMEOUT: %s", pcap_strerror(errno
));
2304 status
= PCAP_ERROR
;
2307 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2312 #ifdef BIOCIMMEDIATE
2314 * Darren Reed notes that
2316 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
2317 * timeout appears to be ignored and it waits until the buffer
2318 * is filled before returning. The result of not having it
2319 * set is almost worse than useless if your BPF filter
2320 * is reducing things to only a few packets (i.e. one every
2323 * so we always turn BIOCIMMEDIATE mode on if this is AIX.
2325 * For other platforms, we don't turn immediate mode on by default,
2326 * as that would mean we get woken up for every packet, which
2327 * probably isn't what you want for a packet sniffer.
2329 * We set immediate mode if the caller requested it by calling
2330 * pcap_set_immediate() before calling pcap_activate().
2333 if (p
->opt
.immediate
) {
2336 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
2337 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2338 "BIOCIMMEDIATE: %s", pcap_strerror(errno
));
2339 status
= PCAP_ERROR
;
2345 #else /* BIOCIMMEDIATE */
2346 if (p
->opt
.immediate
) {
2348 * We don't support immediate mode. Fail.
2350 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Immediate mode not supported");
2351 status
= PCAP_ERROR
;
2354 #endif /* BIOCIMMEDIATE */
2356 if (p
->opt
.promisc
) {
2357 /* set promiscuous mode, just warn if it fails */
2358 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
2359 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
2360 pcap_strerror(errno
));
2361 status
= PCAP_WARNING_PROMISC_NOTSUP
;
2365 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
2366 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
2367 pcap_strerror(errno
));
2368 status
= PCAP_ERROR
;
2372 #ifdef HAVE_ZEROCOPY_BPF
2373 if (!pb
->zerocopy
) {
2375 p
->buffer
= malloc(p
->bufsize
);
2376 if (p
->buffer
== NULL
) {
2377 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
2378 pcap_strerror(errno
));
2379 status
= PCAP_ERROR
;
2383 /* For some strange reason this seems to prevent the EFAULT
2384 * problems we have experienced from AIX BPF. */
2385 memset(p
->buffer
, 0x0, p
->bufsize
);
2387 #ifdef HAVE_ZEROCOPY_BPF
2392 * If there's no filter program installed, there's
2393 * no indication to the kernel of what the snapshot
2394 * length should be, so no snapshotting is done.
2396 * Therefore, when we open the device, we install
2397 * an "accept everything" filter with the specified
2400 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
2403 total_insn
.k
= p
->snapshot
;
2405 total_prog
.bf_len
= 1;
2406 total_prog
.bf_insns
= &total_insn
;
2407 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
2408 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2409 pcap_strerror(errno
));
2410 status
= PCAP_ERROR
;
2415 * On most BPF platforms, either you can do a "select()" or
2416 * "poll()" on a BPF file descriptor and it works correctly,
2417 * or you can do it and it will return "readable" if the
2418 * hold buffer is full but not if the timeout expires *and*
2419 * a non-blocking read will, if the hold buffer is empty
2420 * but the store buffer isn't empty, rotate the buffers
2421 * and return what packets are available.
2423 * In the latter case, the fact that a non-blocking read
2424 * will give you the available packets means you can work
2425 * around the failure of "select()" and "poll()" to wake up
2426 * and return "readable" when the timeout expires by using
2427 * the timeout as the "select()" or "poll()" timeout, putting
2428 * the BPF descriptor into non-blocking mode, and read from
2429 * it regardless of whether "select()" reports it as readable
2432 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
2433 * won't wake up and return "readable" if the timer expires
2434 * and non-blocking reads return EWOULDBLOCK if the hold
2435 * buffer is empty, even if the store buffer is non-empty.
2437 * This means the workaround in question won't work.
2439 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2440 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2441 * here". On all other BPF platforms, we set it to the FD for
2442 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2443 * read will, if the hold buffer is empty and the store buffer
2444 * isn't empty, rotate the buffers and return what packets are
2445 * there (and in sufficiently recent versions of OpenBSD
2446 * "select()" and "poll()" should work correctly).
2448 * XXX - what about AIX?
2450 p
->selectable_fd
= p
->fd
; /* assume select() works until we know otherwise */
2453 * We can check what OS this is.
2455 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0) {
2456 if (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
2457 strncmp(osinfo
.release
, "4.4-", 4) == 0)
2458 p
->selectable_fd
= -1;
2462 p
->read_op
= pcap_read_bpf
;
2463 p
->inject_op
= pcap_inject_bpf
;
2464 p
->setfilter_op
= pcap_setfilter_bpf
;
2465 p
->setdirection_op
= pcap_setdirection_bpf
;
2466 p
->set_datalink_op
= pcap_set_datalink_bpf
;
2467 p
->getnonblock_op
= pcap_getnonblock_bpf
;
2468 p
->setnonblock_op
= pcap_setnonblock_bpf
;
2469 p
->stats_op
= pcap_stats_bpf
;
2470 p
->cleanup_op
= pcap_cleanup_bpf
;
2474 pcap_cleanup_bpf(p
);
2479 * Not all interfaces can be bound to by BPF, so try to bind to
2480 * the specified interface; return 0 if we fail with
2481 * PCAP_ERROR_NO_SUCH_DEVICE (which means we got an ENXIO when we tried
2482 * to bind, which means this interface isn't in the list of interfaces
2483 * attached to BPF) and 1 otherwise.
2486 check_bpf_bindable(const char *name
)
2489 char errbuf
[PCAP_ERRBUF_SIZE
];
2491 fd
= bpf_open_and_bind(name
, errbuf
);
2494 * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
2496 if (fd
== PCAP_ERROR_NO_SUCH_DEVICE
) {
2498 * Yes, so we can't bind to this because it's
2499 * not something supported by BPF.
2504 * No, so we don't know whether it's supported or not;
2505 * say it is, so that the user can at least try to
2506 * open it and report the error (which is probably
2507 * "you don't have permission to open BPF devices";
2508 * reporting those interfaces means users will ask
2509 * "why am I getting a permissions error when I try
2510 * to capture" rather than "why am I not seeing any
2511 * interfaces", making the underlying problem clearer).
2524 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
2527 * Get the list of regular interfaces first.
2529 if (pcap_findalldevs_interfaces(alldevsp
, errbuf
, check_bpf_bindable
) == -1)
2530 return (-1); /* failure */
2532 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
2534 * We might have USB sniffing support, so try looking for USB
2537 * We want to report a usbusN device for each USB bus, but
2538 * usbusN interfaces might, or might not, exist for them -
2539 * we create one if there isn't already one.
2541 * So, instead, we look in /dev/usb for all buses and create
2542 * a "usbusN" device for each one.
2545 struct dirent
*usbitem
;
2549 usbdir
= opendir("/dev/usb");
2550 if (usbdir
== NULL
) {
2558 * Leave enough room for a 32-bit (10-digit) bus number.
2559 * Yes, that's overkill, but we won't be using
2560 * the buffer very long.
2562 name_max
= USBUS_PREFIX_LEN
+ 10 + 1;
2563 name
= malloc(name_max
);
2568 while ((usbitem
= readdir(usbdir
)) != NULL
) {
2573 if (strcmp(usbitem
->d_name
, ".") == 0 ||
2574 strcmp(usbitem
->d_name
, "..") == 0) {
2580 p
= strchr(usbitem
->d_name
, '.');
2583 busnumlen
= p
- usbitem
->d_name
;
2584 memcpy(name
, usbus_prefix
, USBUS_PREFIX_LEN
);
2585 memcpy(name
+ USBUS_PREFIX_LEN
, usbitem
->d_name
, busnumlen
);
2586 *(name
+ USBUS_PREFIX_LEN
+ busnumlen
) = '\0';
2587 err
= pcap_add_if(alldevsp
, name
, PCAP_IF_UP
, NULL
, errbuf
);
2601 #ifdef HAVE_BSD_IEEE80211
2603 monitor_mode(pcap_t
*p
, int set
)
2605 struct pcap_bpf
*pb
= p
->priv
;
2607 struct ifmediareq req
;
2613 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
2615 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "can't open socket: %s",
2616 pcap_strerror(errno
));
2617 return (PCAP_ERROR
);
2620 memset(&req
, 0, sizeof req
);
2621 strncpy(req
.ifm_name
, p
->opt
.device
, sizeof req
.ifm_name
);
2624 * Find out how many media types we have.
2626 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2628 * Can't get the media types.
2634 * There's no such device.
2637 return (PCAP_ERROR_NO_SUCH_DEVICE
);
2641 * Interface doesn't support SIOC{G,S}IFMEDIA.
2644 return (PCAP_ERROR_RFMON_NOTSUP
);
2647 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2648 "SIOCGIFMEDIA 1: %s", pcap_strerror(errno
));
2650 return (PCAP_ERROR
);
2653 if (req
.ifm_count
== 0) {
2658 return (PCAP_ERROR_RFMON_NOTSUP
);
2662 * Allocate a buffer to hold all the media types, and
2663 * get the media types.
2665 media_list
= malloc(req
.ifm_count
* sizeof(int));
2666 if (media_list
== NULL
) {
2667 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
2668 pcap_strerror(errno
));
2670 return (PCAP_ERROR
);
2672 req
.ifm_ulist
= media_list
;
2673 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2674 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCGIFMEDIA: %s",
2675 pcap_strerror(errno
));
2678 return (PCAP_ERROR
);
2682 * Look for an 802.11 "automatic" media type.
2683 * We assume that all 802.11 adapters have that media type,
2684 * and that it will carry the monitor mode supported flag.
2687 for (i
= 0; i
< req
.ifm_count
; i
++) {
2688 if (IFM_TYPE(media_list
[i
]) == IFM_IEEE80211
2689 && IFM_SUBTYPE(media_list
[i
]) == IFM_AUTO
) {
2690 /* OK, does it do monitor mode? */
2691 if (media_list
[i
] & IFM_IEEE80211_MONITOR
) {
2700 * This adapter doesn't support monitor mode.
2703 return (PCAP_ERROR_RFMON_NOTSUP
);
2708 * Don't just check whether we can enable monitor mode,
2709 * do so, if it's not already enabled.
2711 if ((req
.ifm_current
& IFM_IEEE80211_MONITOR
) == 0) {
2713 * Monitor mode isn't currently on, so turn it on,
2714 * and remember that we should turn it off when the
2719 * If we haven't already done so, arrange to have
2720 * "pcap_close_all()" called when we exit.
2722 if (!pcap_do_addexit(p
)) {
2724 * "atexit()" failed; don't put the interface
2725 * in monitor mode, just give up.
2728 return (PCAP_ERROR
);
2730 memset(&ifr
, 0, sizeof(ifr
));
2731 (void)strncpy(ifr
.ifr_name
, p
->opt
.device
,
2732 sizeof(ifr
.ifr_name
));
2733 ifr
.ifr_media
= req
.ifm_current
| IFM_IEEE80211_MONITOR
;
2734 if (ioctl(sock
, SIOCSIFMEDIA
, &ifr
) == -1) {
2735 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2736 "SIOCSIFMEDIA: %s", pcap_strerror(errno
));
2738 return (PCAP_ERROR
);
2741 pb
->must_do_on_close
|= MUST_CLEAR_RFMON
;
2744 * Add this to the list of pcaps to close when we exit.
2746 pcap_add_to_pcaps_to_close(p
);
2751 #endif /* HAVE_BSD_IEEE80211 */
2753 #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
2755 * Check whether we have any 802.11 link-layer types; return the best
2756 * of the 802.11 link-layer types if we find one, and return -1
2759 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
2760 * best 802.11 link-layer type; any of the other 802.11-plus-radio
2761 * headers are second-best; 802.11 with no radio information is
2765 find_802_11(struct bpf_dltlist
*bdlp
)
2771 * Scan the list of DLT_ values, looking for 802.11 values,
2772 * and, if we find any, choose the best of them.
2775 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
2776 switch (bdlp
->bfl_list
[i
]) {
2778 case DLT_IEEE802_11
:
2780 * 802.11, but no radio.
2782 * Offer this, and select it as the new mode
2783 * unless we've already found an 802.11
2784 * header with radio information.
2787 new_dlt
= bdlp
->bfl_list
[i
];
2790 case DLT_PRISM_HEADER
:
2791 case DLT_AIRONET_HEADER
:
2792 case DLT_IEEE802_11_RADIO_AVS
:
2794 * 802.11 with radio, but not radiotap.
2796 * Offer this, and select it as the new mode
2797 * unless we've already found the radiotap DLT_.
2799 if (new_dlt
!= DLT_IEEE802_11_RADIO
)
2800 new_dlt
= bdlp
->bfl_list
[i
];
2803 case DLT_IEEE802_11_RADIO
:
2805 * 802.11 with radiotap.
2807 * Offer this, and select it as the new mode.
2809 new_dlt
= bdlp
->bfl_list
[i
];
2822 #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
2824 #if defined(__APPLE__) && defined(BIOCGDLTLIST)
2826 * Remove DLT_EN10MB from the list of DLT_ values, as we're in monitor mode,
2827 * and DLT_EN10MB isn't supported in monitor mode.
2830 remove_en(pcap_t
*p
)
2835 * Scan the list of DLT_ values and discard DLT_EN10MB.
2838 for (i
= 0; i
< p
->dlt_count
; i
++) {
2839 switch (p
->dlt_list
[i
]) {
2843 * Don't offer this one.
2849 * Just copy this mode over.
2855 * Copy this DLT_ value to its new position.
2857 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2862 * Set the DLT_ count to the number of entries we copied.
2868 * Remove 802.11 link-layer types from the list of DLT_ values, as
2869 * we're not in monitor mode, and those DLT_ values will switch us
2873 remove_802_11(pcap_t
*p
)
2878 * Scan the list of DLT_ values and discard 802.11 values.
2881 for (i
= 0; i
< p
->dlt_count
; i
++) {
2882 switch (p
->dlt_list
[i
]) {
2884 case DLT_IEEE802_11
:
2885 case DLT_PRISM_HEADER
:
2886 case DLT_AIRONET_HEADER
:
2887 case DLT_IEEE802_11_RADIO
:
2888 case DLT_IEEE802_11_RADIO_AVS
:
2890 * 802.11. Don't offer this one.
2896 * Just copy this mode over.
2902 * Copy this DLT_ value to its new position.
2904 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2909 * Set the DLT_ count to the number of entries we copied.
2913 #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
2916 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
2918 struct pcap_bpf
*pb
= p
->priv
;
2921 * Free any user-mode filter we might happen to have installed.
2923 pcap_freecode(&p
->fcode
);
2926 * Try to install the kernel filter.
2928 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) == 0) {
2932 pb
->filtering_in_kernel
= 1; /* filtering in the kernel */
2935 * Discard any previously-received packets, as they might
2936 * have passed whatever filter was formerly in effect, but
2937 * might not pass this filter (BIOCSETF discards packets
2938 * buffered in the kernel, so you can lose packets in any
2948 * If it failed with EINVAL, that's probably because the program
2949 * is invalid or too big. Validate it ourselves; if we like it
2950 * (we currently allow backward branches, to support protochain),
2951 * run it in userland. (There's no notion of "too big" for
2954 * Otherwise, just give up.
2955 * XXX - if the copy of the program into the kernel failed,
2956 * we will get EINVAL rather than, say, EFAULT on at least
2959 if (errno
!= EINVAL
) {
2960 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2961 pcap_strerror(errno
));
2966 * install_bpf_program() validates the program.
2968 * XXX - what if we already have a filter in the kernel?
2970 if (install_bpf_program(p
, fp
) < 0)
2972 pb
->filtering_in_kernel
= 0; /* filtering in userland */
2977 * Set direction flag: Which packets do we accept on a forwarding
2978 * single device? IN, OUT or both?
2981 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
2983 #if defined(BIOCSDIRECTION)
2986 direction
= (d
== PCAP_D_IN
) ? BPF_D_IN
:
2987 ((d
== PCAP_D_OUT
) ? BPF_D_OUT
: BPF_D_INOUT
);
2988 if (ioctl(p
->fd
, BIOCSDIRECTION
, &direction
) == -1) {
2989 (void) pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2990 "Cannot set direction to %s: %s",
2991 (d
== PCAP_D_IN
) ? "PCAP_D_IN" :
2992 ((d
== PCAP_D_OUT
) ? "PCAP_D_OUT" : "PCAP_D_INOUT"),
2997 #elif defined(BIOCSSEESENT)
3001 * We don't support PCAP_D_OUT.
3003 if (d
== PCAP_D_OUT
) {
3004 pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3005 "Setting direction to PCAP_D_OUT is not supported on BPF");
3009 seesent
= (d
== PCAP_D_INOUT
);
3010 if (ioctl(p
->fd
, BIOCSSEESENT
, &seesent
) == -1) {
3011 (void) pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3012 "Cannot set direction to %s: %s",
3013 (d
== PCAP_D_INOUT
) ? "PCAP_D_INOUT" : "PCAP_D_IN",
3019 (void) pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3020 "This system doesn't support BIOCSSEESENT, so the direction can't be set");
3026 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
3029 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
3030 (void) pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3031 "Cannot set DLT %d: %s", dlt
, strerror(errno
));