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 #ifdef HAVE_ZEROCOPY_BPF
52 #include <machine/atomic.h>
60 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
61 * native OS version, as we need "struct bpf_config" from it.
63 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
65 #include <sys/types.h>
68 * Prevent bpf.h from redefining the DLT_ values to their
69 * IFT_ values, as we're going to return the standard libpcap
70 * values, not IBM's non-standard IFT_ values.
76 #include <net/if_types.h> /* for IFT_ values */
77 #include <sys/sysconfig.h>
78 #include <sys/device.h>
79 #include <sys/cfgodm.h>
83 #define domakedev makedev64
84 #define getmajor major64
85 #define bpf_hdr bpf_hdr32
87 #define domakedev makedev
88 #define getmajor major
89 #endif /* __64BIT__ */
91 #define BPF_NAME "bpf"
93 #define DRIVER_PATH "/usr/lib/drivers"
94 #define BPF_NODE "/dev/bpf"
95 static int bpfloadedflag
= 0;
96 static int odmlockid
= 0;
98 static int bpf_load(char *errbuf
);
115 #ifdef HAVE_NET_IF_MEDIA_H
116 # include <net/if_media.h>
119 #include "pcap-int.h"
121 #ifdef HAVE_OS_PROTO_H
122 #include "os-proto.h"
126 * Later versions of NetBSD stick padding in front of FDDI frames
127 * to align the IP header on a 4-byte boundary.
129 #if defined(__NetBSD__) && __NetBSD_Version__ > 106000000
130 #define PCAP_FDDIPAD 3
134 * Private data for capturing on BPF devices.
141 #ifdef HAVE_ZEROCOPY_BPF
143 * Zero-copy read buffer -- for zero-copy BPF. 'buffer' above will
144 * alternative between these two actual mmap'd buffers as required.
145 * As there is a header on the front size of the mmap'd buffer, only
146 * some of the buffer is exposed to libpcap as a whole via bufsize;
147 * zbufsize is the true size. zbuffer tracks the current zbuf
148 * assocated with buffer so that it can be used to decide which the
149 * next buffer to read will be.
151 u_char
*zbuf1
, *zbuf2
, *zbuffer
;
155 struct timespec firstsel
;
157 * If there's currently a buffer being actively processed, then it is
158 * referenced here; 'buffer' is also pointed at it, but offset by the
159 * size of the header.
161 struct bpf_zbuf_header
*bzh
;
162 int nonblock
; /* true if in nonblocking mode */
163 #endif /* HAVE_ZEROCOPY_BPF */
165 char *device
; /* device name */
166 int filtering_in_kernel
; /* using kernel filter */
167 int must_do_on_close
; /* stuff we must do when we close */
171 * Stuff to do when we close.
173 #define MUST_CLEAR_RFMON 0x00000001 /* clear rfmon (monitor) mode */
176 # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
177 #define HAVE_BSD_IEEE80211
180 # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
181 static int find_802_11(struct bpf_dltlist
*);
183 # ifdef HAVE_BSD_IEEE80211
184 static int monitor_mode(pcap_t
*, int);
187 # if defined(__APPLE__)
188 static void remove_en(pcap_t
*);
189 static void remove_802_11(pcap_t
*);
192 # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
194 #endif /* BIOCGDLTLIST */
196 #if defined(sun) && defined(LIFNAMSIZ) && defined(lifr_zoneid)
201 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
202 * don't get DLT_DOCSIS defined.
205 #define DLT_DOCSIS 143
209 * On OS X, we don't even get any of the 802.11-plus-radio-header DLT_'s
210 * defined, even though some of them are used by various Airport drivers.
212 #ifndef DLT_PRISM_HEADER
213 #define DLT_PRISM_HEADER 119
215 #ifndef DLT_AIRONET_HEADER
216 #define DLT_AIRONET_HEADER 120
218 #ifndef DLT_IEEE802_11_RADIO
219 #define DLT_IEEE802_11_RADIO 127
221 #ifndef DLT_IEEE802_11_RADIO_AVS
222 #define DLT_IEEE802_11_RADIO_AVS 163
225 static int pcap_can_set_rfmon_bpf(pcap_t
*p
);
226 static int pcap_activate_bpf(pcap_t
*p
);
227 static int pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
);
228 static int pcap_setdirection_bpf(pcap_t
*, pcap_direction_t
);
229 static int pcap_set_datalink_bpf(pcap_t
*p
, int dlt
);
232 * For zerocopy bpf, the setnonblock/getnonblock routines need to modify
233 * pb->nonblock so we don't call select(2) if the pcap handle is in non-
237 pcap_getnonblock_bpf(pcap_t
*p
, char *errbuf
)
239 #ifdef HAVE_ZEROCOPY_BPF
240 struct pcap_bpf
*pb
= p
->priv
;
243 return (pb
->nonblock
);
245 return (pcap_getnonblock_fd(p
, errbuf
));
249 pcap_setnonblock_bpf(pcap_t
*p
, int nonblock
, char *errbuf
)
251 #ifdef HAVE_ZEROCOPY_BPF
252 struct pcap_bpf
*pb
= p
->priv
;
255 pb
->nonblock
= nonblock
;
259 return (pcap_setnonblock_fd(p
, nonblock
, errbuf
));
262 #ifdef HAVE_ZEROCOPY_BPF
264 * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
265 * shared memory buffers.
267 * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer,
268 * and set up p->buffer and cc to reflect one if available. Notice that if
269 * there was no prior buffer, we select zbuf1 as this will be the first
270 * buffer filled for a fresh BPF session.
273 pcap_next_zbuf_shm(pcap_t
*p
, int *cc
)
275 struct pcap_bpf
*pb
= p
->priv
;
276 struct bpf_zbuf_header
*bzh
;
278 if (pb
->zbuffer
== pb
->zbuf2
|| pb
->zbuffer
== NULL
) {
279 bzh
= (struct bpf_zbuf_header
*)pb
->zbuf1
;
280 if (bzh
->bzh_user_gen
!=
281 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
283 pb
->zbuffer
= (u_char
*)pb
->zbuf1
;
284 p
->buffer
= pb
->zbuffer
+ sizeof(*bzh
);
285 *cc
= bzh
->bzh_kernel_len
;
288 } else if (pb
->zbuffer
== pb
->zbuf1
) {
289 bzh
= (struct bpf_zbuf_header
*)pb
->zbuf2
;
290 if (bzh
->bzh_user_gen
!=
291 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
293 pb
->zbuffer
= (u_char
*)pb
->zbuf2
;
294 p
->buffer
= pb
->zbuffer
+ sizeof(*bzh
);
295 *cc
= bzh
->bzh_kernel_len
;
304 * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
305 * select() for data or a timeout, and possibly force rotation of the buffer
306 * in the event we time out or are in immediate mode. Invoke the shared
307 * memory check before doing system calls in order to avoid doing avoidable
311 pcap_next_zbuf(pcap_t
*p
, int *cc
)
313 struct pcap_bpf
*pb
= p
->priv
;
321 #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000))
323 * Start out by seeing whether anything is waiting by checking the
324 * next shared memory buffer for data.
326 data
= pcap_next_zbuf_shm(p
, cc
);
330 * If a previous sleep was interrupted due to signal delivery, make
331 * sure that the timeout gets adjusted accordingly. This requires
332 * that we analyze when the timeout should be been expired, and
333 * subtract the current time from that. If after this operation,
334 * our timeout is less then or equal to zero, handle it like a
337 tmout
= p
->opt
.timeout
;
339 (void) clock_gettime(CLOCK_MONOTONIC
, &cur
);
340 if (pb
->interrupted
&& p
->opt
.timeout
) {
341 expire
= TSTOMILLI(&pb
->firstsel
) + p
->opt
.timeout
;
342 tmout
= expire
- TSTOMILLI(&cur
);
346 data
= pcap_next_zbuf_shm(p
, cc
);
349 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
350 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
351 "BIOCROTZBUF: %s", strerror(errno
));
354 return (pcap_next_zbuf_shm(p
, cc
));
358 * No data in the buffer, so must use select() to wait for data or
359 * the next timeout. Note that we only call select if the handle
360 * is in blocking mode.
364 FD_SET(p
->fd
, &r_set
);
366 tv
.tv_sec
= tmout
/ 1000;
367 tv
.tv_usec
= (tmout
* 1000) % 1000000;
369 r
= select(p
->fd
+ 1, &r_set
, NULL
, NULL
,
370 p
->opt
.timeout
!= 0 ? &tv
: NULL
);
371 if (r
< 0 && errno
== EINTR
) {
372 if (!pb
->interrupted
&& p
->opt
.timeout
) {
378 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
379 "select: %s", strerror(errno
));
385 * Check again for data, which may exist now that we've either been
386 * woken up as a result of data or timed out. Try the "there's data"
387 * case first since it doesn't require a system call.
389 data
= pcap_next_zbuf_shm(p
, cc
);
393 * Try forcing a buffer rotation to dislodge timed out or immediate
396 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
397 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
398 "BIOCROTZBUF: %s", strerror(errno
));
401 return (pcap_next_zbuf_shm(p
, cc
));
405 * Notify kernel that we are done with the buffer. We don't reset zbuffer so
406 * that we know which buffer to use next time around.
409 pcap_ack_zbuf(pcap_t
*p
)
411 struct pcap_bpf
*pb
= p
->priv
;
413 atomic_store_rel_int(&pb
->bzh
->bzh_user_gen
,
414 pb
->bzh
->bzh_kernel_gen
);
419 #endif /* HAVE_ZEROCOPY_BPF */
422 pcap_create_interface(const char *device
, char *ebuf
)
426 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_bpf
));
430 p
->activate_op
= pcap_activate_bpf
;
431 p
->can_set_rfmon_op
= pcap_can_set_rfmon_bpf
;
436 * On success, returns a file descriptor for a BPF device.
437 * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf.
443 #ifdef HAVE_CLONING_BPF
444 static const char device
[] = "/dev/bpf";
447 char device
[sizeof "/dev/bpf0000000000"];
452 * Load the bpf driver, if it isn't already loaded,
453 * and create the BPF device entries, if they don't
456 if (bpf_load(p
->errbuf
) == PCAP_ERROR
)
460 #ifdef HAVE_CLONING_BPF
461 if ((fd
= open(device
, O_RDWR
)) == -1 &&
462 (errno
!= EACCES
|| (fd
= open(device
, O_RDONLY
)) == -1)) {
464 fd
= PCAP_ERROR_PERM_DENIED
;
467 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
468 "(cannot open device) %s: %s", device
, pcap_strerror(errno
));
472 * Go through all the minors and find one that isn't in use.
475 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
477 * Initially try a read/write open (to allow the inject
478 * method to work). If that fails due to permission
479 * issues, fall back to read-only. This allows a
480 * non-root user to be granted specific access to pcap
481 * capabilities via file permissions.
483 * XXX - we should have an API that has a flag that
484 * controls whether to open read-only or read-write,
485 * so that denial of permission to send (or inability
486 * to send, if sending packets isn't supported on
487 * the device in question) can be indicated at open
490 fd
= open(device
, O_RDWR
);
491 if (fd
== -1 && errno
== EACCES
)
492 fd
= open(device
, O_RDONLY
);
493 } while (fd
< 0 && errno
== EBUSY
);
496 * XXX better message for all minors used
505 * /dev/bpf0 doesn't exist, which
506 * means we probably have no BPF
509 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
510 "(there are no BPF devices)");
513 * We got EBUSY on at least one
514 * BPF device, so we have BPF
515 * devices, but all the ones
516 * that exist are busy.
518 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
519 "(all BPF devices are busy)");
525 * Got EACCES on the last device we tried,
526 * and EBUSY on all devices before that,
529 fd
= PCAP_ERROR_PERM_DENIED
;
530 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
531 "(cannot open BPF device) %s: %s", device
,
532 pcap_strerror(errno
));
537 * Some other problem.
540 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
541 "(cannot open BPF device) %s: %s", device
,
542 pcap_strerror(errno
));
553 get_dlt_list(int fd
, int v
, struct bpf_dltlist
*bdlp
, char *ebuf
)
555 memset(bdlp
, 0, sizeof(*bdlp
));
556 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) == 0) {
560 bdlp
->bfl_list
= (u_int
*) malloc(sizeof(u_int
) * (bdlp
->bfl_len
+ 1));
561 if (bdlp
->bfl_list
== NULL
) {
562 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
563 pcap_strerror(errno
));
567 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) < 0) {
568 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
569 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
570 free(bdlp
->bfl_list
);
575 * OK, for real Ethernet devices, add DLT_DOCSIS to the
576 * list, so that an application can let you choose it,
577 * in case you're capturing DOCSIS traffic that a Cisco
578 * Cable Modem Termination System is putting out onto
579 * an Ethernet (it doesn't put an Ethernet header onto
580 * the wire, it puts raw DOCSIS frames out on the wire
581 * inside the low-level Ethernet framing).
583 * A "real Ethernet device" is defined here as a device
584 * that has a link-layer type of DLT_EN10MB and that has
585 * no alternate link-layer types; that's done to exclude
586 * 802.11 interfaces (which might or might not be the
587 * right thing to do, but I suspect it is - Ethernet <->
588 * 802.11 bridges would probably badly mishandle frames
589 * that don't have Ethernet headers).
591 * On Solaris with BPF, Ethernet devices also offer
592 * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
593 * treat it as an indication that the device isn't an
596 if (v
== DLT_EN10MB
) {
598 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
599 if (bdlp
->bfl_list
[i
] != DLT_EN10MB
601 && bdlp
->bfl_list
[i
] != DLT_IPNET
610 * We reserved one more slot at the end of
613 bdlp
->bfl_list
[bdlp
->bfl_len
] = DLT_DOCSIS
;
619 * EINVAL just means "we don't support this ioctl on
620 * this device"; don't treat it as an error.
622 if (errno
!= EINVAL
) {
623 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
624 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
633 pcap_can_set_rfmon_bpf(pcap_t
*p
)
635 #if defined(__APPLE__)
636 struct utsname osinfo
;
640 struct bpf_dltlist bdl
;
644 * The joys of monitor mode on OS X.
646 * Prior to 10.4, it's not supported at all.
648 * In 10.4, if adapter enN supports monitor mode, there's a
649 * wltN adapter corresponding to it; you open it, instead of
650 * enN, to get monitor mode. You get whatever link-layer
651 * headers it supplies.
653 * In 10.5, and, we assume, later releases, if adapter enN
654 * supports monitor mode, it offers, among its selectable
655 * DLT_ values, values that let you get the 802.11 header;
656 * selecting one of those values puts the adapter into monitor
657 * mode (i.e., you can't get 802.11 headers except in monitor
658 * mode, and you can't get Ethernet headers in monitor mode).
660 if (uname(&osinfo
) == -1) {
662 * Can't get the OS version; just say "no".
667 * We assume osinfo.sysname is "Darwin", because
668 * __APPLE__ is defined. We just check the version.
670 if (osinfo
.release
[0] < '8' && osinfo
.release
[1] == '.') {
672 * 10.3 (Darwin 7.x) or earlier.
673 * Monitor mode not supported.
677 if (osinfo
.release
[0] == '8' && osinfo
.release
[1] == '.') {
679 * 10.4 (Darwin 8.x). s/en/wlt/, and check
680 * whether the device exists.
682 if (strncmp(p
->opt
.source
, "en", 2) != 0) {
684 * Not an enN device; no monitor mode.
688 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
690 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
691 "socket: %s", pcap_strerror(errno
));
694 strlcpy(ifr
.ifr_name
, "wlt", sizeof(ifr
.ifr_name
));
695 strlcat(ifr
.ifr_name
, p
->opt
.source
+ 2, sizeof(ifr
.ifr_name
));
696 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
709 * Everything else is 10.5 or later; for those,
710 * we just open the enN device, and check whether
711 * we have any 802.11 devices.
713 * First, open a BPF device.
717 return (fd
); /* fd is the appropriate error code */
720 * Now bind to the device.
722 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
, sizeof(ifr
.ifr_name
));
723 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
728 * There's no such device.
731 return (PCAP_ERROR_NO_SUCH_DEVICE
);
735 * Return a "network down" indication, so that
736 * the application can report that rather than
737 * saying we had a mysterious failure and
738 * suggest that they report a problem to the
739 * libpcap developers.
742 return (PCAP_ERROR_IFACE_NOT_UP
);
745 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
747 p
->opt
.source
, pcap_strerror(errno
));
754 * We know the default link type -- now determine all the DLTs
755 * this interface supports. If this fails with EINVAL, it's
756 * not fatal; we just don't get to use the feature later.
757 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
758 * as the default DLT for this adapter.)
760 if (get_dlt_list(fd
, DLT_NULL
, &bdl
, p
->errbuf
) == PCAP_ERROR
) {
764 if (find_802_11(&bdl
) != -1) {
766 * We have an 802.11 DLT, so we can set monitor mode.
773 #endif /* BIOCGDLTLIST */
775 #elif defined(HAVE_BSD_IEEE80211)
778 ret
= monitor_mode(p
, 0);
779 if (ret
== PCAP_ERROR_RFMON_NOTSUP
)
780 return (0); /* not an error, just a "can't do" */
782 return (1); /* success */
790 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
795 * "ps_recv" counts packets handed to the filter, not packets
796 * that passed the filter. This includes packets later dropped
797 * because we ran out of buffer space.
799 * "ps_drop" counts packets dropped inside the BPF device
800 * because we ran out of buffer space. It doesn't count
801 * packets dropped by the interface driver. It counts
802 * only packets that passed the filter.
804 * Both statistics include packets not yet read from the kernel
805 * by libpcap, and thus not yet seen by the application.
807 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
808 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
809 pcap_strerror(errno
));
813 ps
->ps_recv
= s
.bs_recv
;
814 ps
->ps_drop
= s
.bs_drop
;
820 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
822 struct pcap_bpf
*pb
= p
->priv
;
825 register u_char
*bp
, *ep
;
830 #ifdef HAVE_ZEROCOPY_BPF
836 * Has "pcap_breakloop()" been called?
840 * Yes - clear the flag that indicates that it
841 * has, and return PCAP_ERROR_BREAK to indicate
842 * that we were told to break out of the loop.
845 return (PCAP_ERROR_BREAK
);
850 * When reading without zero-copy from a file descriptor, we
851 * use a single buffer and return a length of data in the
852 * buffer. With zero-copy, we update the p->buffer pointer
853 * to point at whatever underlying buffer contains the next
854 * data and update cc to reflect the data found in the
857 #ifdef HAVE_ZEROCOPY_BPF
859 if (p
->buffer
!= NULL
)
861 i
= pcap_next_zbuf(p
, &cc
);
869 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
872 /* Don't choke when we get ptraced */
881 * Sigh. More AIX wonderfulness.
883 * For some unknown reason the uiomove()
884 * operation in the bpf kernel extension
885 * used to copy the buffer into user
886 * space sometimes returns EFAULT. I have
887 * no idea why this is the case given that
888 * a kernel debugger shows the user buffer
889 * is correct. This problem appears to
890 * be mostly mitigated by the memset of
891 * the buffer before it is first used.
892 * Very strange.... Shaun Clowes
894 * In any case this means that we shouldn't
895 * treat EFAULT as a fatal error; as we
896 * don't have an API for returning
897 * a "some packets were dropped since
898 * the last packet you saw" indication,
899 * we just ignore EFAULT and keep reading.
909 * The device on which we're capturing
912 * XXX - we should really return
913 * PCAP_ERROR_IFACE_NOT_UP, but
914 * pcap_dispatch() etc. aren't
915 * defined to retur that.
917 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
918 "The interface went down");
921 #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
923 * Due to a SunOS bug, after 2^31 bytes, the kernel
924 * file offset overflows and read fails with EINVAL.
925 * The lseek() to 0 will fix things.
928 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
930 (void)lseek(p
->fd
, 0L, SEEK_SET
);
936 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
937 pcap_strerror(errno
));
945 * Loop through each packet.
947 #define bhp ((struct bpf_hdr *)bp)
953 register int caplen
, hdrlen
;
956 * Has "pcap_breakloop()" been called?
957 * If so, return immediately - if we haven't read any
958 * packets, clear the flag and return PCAP_ERROR_BREAK
959 * to indicate that we were told to break out of the loop,
960 * otherwise leave the flag set, so that the *next* call
961 * will break out of the loop without having read any
962 * packets, and return the number of packets we've
969 * ep is set based on the return value of read(),
970 * but read() from a BPF device doesn't necessarily
971 * return a value that's a multiple of the alignment
972 * value for BPF_WORDALIGN(). However, whenever we
973 * increment bp, we round up the increment value by
974 * a value rounded up by BPF_WORDALIGN(), so we
975 * could increment bp past ep after processing the
976 * last packet in the buffer.
978 * We treat ep < bp as an indication that this
979 * happened, and just set p->cc to 0.
985 return (PCAP_ERROR_BREAK
);
990 caplen
= bhp
->bh_caplen
;
991 hdrlen
= bhp
->bh_hdrlen
;
994 * Short-circuit evaluation: if using BPF filter
995 * in kernel, no need to do it now - we already know
996 * the packet passed the filter.
999 * Note: the filter code was generated assuming
1000 * that p->fddipad was the amount of padding
1001 * before the header, as that's what's required
1002 * in the kernel, so we run the filter before
1003 * skipping that padding.
1006 if (pb
->filtering_in_kernel
||
1007 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
1008 struct pcap_pkthdr pkthdr
;
1010 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
1013 * AIX's BPF returns seconds/nanoseconds time
1014 * stamps, not seconds/microseconds time stamps.
1016 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
1018 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
1022 pkthdr
.caplen
= caplen
- pad
;
1025 if (bhp
->bh_datalen
> pad
)
1026 pkthdr
.len
= bhp
->bh_datalen
- pad
;
1031 pkthdr
.caplen
= caplen
;
1032 pkthdr
.len
= bhp
->bh_datalen
;
1034 (*callback
)(user
, &pkthdr
, datap
);
1035 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1036 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
1040 * See comment above about p->cc < 0.
1050 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1059 pcap_inject_bpf(pcap_t
*p
, const void *buf
, size_t size
)
1063 ret
= write(p
->fd
, buf
, size
);
1065 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
1067 * In Mac OS X, there's a bug wherein setting the
1068 * BIOCSHDRCMPLT flag causes writes to fail; see,
1071 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
1073 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
1074 * assume it's due to that bug, and turn off that flag
1075 * and try again. If we succeed, it either means that
1076 * somebody applied the fix from that URL, or other patches
1079 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
1081 * and are running a Darwin kernel with those fixes, or
1082 * that Apple fixed the problem in some OS X release.
1084 u_int spoof_eth_src
= 0;
1086 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1087 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1088 "send: can't turn off BIOCSHDRCMPLT: %s",
1089 pcap_strerror(errno
));
1090 return (PCAP_ERROR
);
1094 * Now try the write again.
1096 ret
= write(p
->fd
, buf
, size
);
1098 #endif /* __APPLE__ */
1100 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
1101 pcap_strerror(errno
));
1102 return (PCAP_ERROR
);
1109 bpf_odminit(char *errbuf
)
1113 if (odm_initialize() == -1) {
1114 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1115 errstr
= "Unknown error";
1116 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1117 "bpf_load: odm_initialize failed: %s",
1119 return (PCAP_ERROR
);
1122 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
1123 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1124 errstr
= "Unknown error";
1125 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1126 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
1128 (void)odm_terminate();
1129 return (PCAP_ERROR
);
1136 bpf_odmcleanup(char *errbuf
)
1140 if (odm_unlock(odmlockid
) == -1) {
1141 if (errbuf
!= NULL
) {
1142 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1143 errstr
= "Unknown error";
1144 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1145 "bpf_load: odm_unlock failed: %s",
1148 return (PCAP_ERROR
);
1151 if (odm_terminate() == -1) {
1152 if (errbuf
!= NULL
) {
1153 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1154 errstr
= "Unknown error";
1155 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1156 "bpf_load: odm_terminate failed: %s",
1159 return (PCAP_ERROR
);
1166 bpf_load(char *errbuf
)
1170 int numminors
, i
, rc
;
1173 struct bpf_config cfg_bpf
;
1174 struct cfg_load cfg_ld
;
1175 struct cfg_kmod cfg_km
;
1178 * This is very very close to what happens in the real implementation
1179 * but I've fixed some (unlikely) bug situations.
1184 if (bpf_odminit(errbuf
) == PCAP_ERROR
)
1185 return (PCAP_ERROR
);
1187 major
= genmajor(BPF_NAME
);
1189 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1190 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
1191 (void)bpf_odmcleanup(NULL
);
1192 return (PCAP_ERROR
);
1195 minors
= getminor(major
, &numminors
, BPF_NAME
);
1197 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
1199 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1200 "bpf_load: genminor failed: %s",
1201 pcap_strerror(errno
));
1202 (void)bpf_odmcleanup(NULL
);
1203 return (PCAP_ERROR
);
1207 if (bpf_odmcleanup(errbuf
) == PCAP_ERROR
)
1208 return (PCAP_ERROR
);
1210 rc
= stat(BPF_NODE
"0", &sbuf
);
1211 if (rc
== -1 && errno
!= ENOENT
) {
1212 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1213 "bpf_load: can't stat %s: %s",
1214 BPF_NODE
"0", pcap_strerror(errno
));
1215 return (PCAP_ERROR
);
1218 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
1219 for (i
= 0; i
< BPF_MINORS
; i
++) {
1220 sprintf(buf
, "%s%d", BPF_NODE
, i
);
1222 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
1223 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1224 "bpf_load: can't mknod %s: %s",
1225 buf
, pcap_strerror(errno
));
1226 return (PCAP_ERROR
);
1231 /* Check if the driver is loaded */
1232 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
1234 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
1235 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
1236 (cfg_ld
.kmid
== 0)) {
1237 /* Driver isn't loaded, load it now */
1238 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
1239 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1240 "bpf_load: could not load driver: %s",
1242 return (PCAP_ERROR
);
1246 /* Configure the driver */
1247 cfg_km
.cmd
= CFG_INIT
;
1248 cfg_km
.kmid
= cfg_ld
.kmid
;
1249 cfg_km
.mdilen
= sizeof(cfg_bpf
);
1250 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
1251 for (i
= 0; i
< BPF_MINORS
; i
++) {
1252 cfg_bpf
.devno
= domakedev(major
, i
);
1253 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
1254 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1255 "bpf_load: could not configure driver: %s",
1257 return (PCAP_ERROR
);
1268 * Turn off rfmon mode if necessary.
1271 pcap_cleanup_bpf(pcap_t
*p
)
1273 struct pcap_bpf
*pb
= p
->priv
;
1274 #ifdef HAVE_BSD_IEEE80211
1276 struct ifmediareq req
;
1280 if (pb
->must_do_on_close
!= 0) {
1282 * There's something we have to do when closing this
1285 #ifdef HAVE_BSD_IEEE80211
1286 if (pb
->must_do_on_close
& MUST_CLEAR_RFMON
) {
1288 * We put the interface into rfmon mode;
1289 * take it out of rfmon mode.
1291 * XXX - if somebody else wants it in rfmon
1292 * mode, this code cannot know that, so it'll take
1293 * it out of rfmon mode.
1295 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
1298 "Can't restore interface flags (socket() failed: %s).\n"
1299 "Please adjust manually.\n",
1302 memset(&req
, 0, sizeof(req
));
1303 strncpy(req
.ifm_name
, pb
->device
,
1304 sizeof(req
.ifm_name
));
1305 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1307 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
1308 "Please adjust manually.\n",
1311 if (req
.ifm_current
& IFM_IEEE80211_MONITOR
) {
1313 * Rfmon mode is currently on;
1316 memset(&ifr
, 0, sizeof(ifr
));
1317 (void)strncpy(ifr
.ifr_name
,
1319 sizeof(ifr
.ifr_name
));
1321 req
.ifm_current
& ~IFM_IEEE80211_MONITOR
;
1322 if (ioctl(sock
, SIOCSIFMEDIA
,
1325 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
1326 "Please adjust manually.\n",
1334 #endif /* HAVE_BSD_IEEE80211 */
1337 * Take this pcap out of the list of pcaps for which we
1338 * have to take the interface out of some mode.
1340 pcap_remove_from_pcaps_to_close(p
);
1341 pb
->must_do_on_close
= 0;
1344 #ifdef HAVE_ZEROCOPY_BPF
1347 * Delete the mappings. Note that p->buffer gets
1348 * initialized to one of the mmapped regions in
1349 * this case, so do not try and free it directly;
1350 * null it out so that pcap_cleanup_live_common()
1351 * doesn't try to free it.
1353 if (pb
->zbuf1
!= MAP_FAILED
&& pb
->zbuf1
!= NULL
)
1354 (void) munmap(pb
->zbuf1
, pb
->zbufsize
);
1355 if (pb
->zbuf2
!= MAP_FAILED
&& pb
->zbuf2
!= NULL
)
1356 (void) munmap(pb
->zbuf2
, pb
->zbufsize
);
1360 if (pb
->device
!= NULL
) {
1364 pcap_cleanup_live_common(p
);
1368 check_setif_failure(pcap_t
*p
, int error
)
1376 if (error
== ENXIO
) {
1378 * No such device exists.
1381 if (p
->opt
.rfmon
&& strncmp(p
->opt
.source
, "wlt", 3) == 0) {
1383 * Monitor mode was requested, and we're trying
1384 * to open a "wltN" device. Assume that this
1385 * is 10.4 and that we were asked to open an
1386 * "enN" device; if that device exists, return
1387 * "monitor mode not supported on the device".
1389 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1391 strlcpy(ifr
.ifr_name
, "en",
1392 sizeof(ifr
.ifr_name
));
1393 strlcat(ifr
.ifr_name
, p
->opt
.source
+ 3,
1394 sizeof(ifr
.ifr_name
));
1395 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
1397 * We assume this failed because
1398 * the underlying device doesn't
1401 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1402 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1403 "SIOCGIFFLAGS on %s failed: %s",
1404 ifr
.ifr_name
, pcap_strerror(errno
));
1407 * The underlying "enN" device
1408 * exists, but there's no
1409 * corresponding "wltN" device;
1410 * that means that the "enN"
1411 * device doesn't support
1412 * monitor mode, probably because
1413 * it's an Ethernet device rather
1414 * than a wireless device.
1416 err
= PCAP_ERROR_RFMON_NOTSUP
;
1421 * We can't find out whether there's
1422 * an underlying "enN" device, so
1423 * just report "no such device".
1425 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1426 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1427 "socket() failed: %s",
1428 pcap_strerror(errno
));
1436 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF failed: %s",
1437 pcap_strerror(errno
));
1438 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1439 } else if (errno
== ENETDOWN
) {
1441 * Return a "network down" indication, so that
1442 * the application can report that rather than
1443 * saying we had a mysterious failure and
1444 * suggest that they report a problem to the
1445 * libpcap developers.
1447 return (PCAP_ERROR_IFACE_NOT_UP
);
1450 * Some other error; fill in the error string, and
1451 * return PCAP_ERROR.
1453 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1454 p
->opt
.source
, pcap_strerror(errno
));
1455 return (PCAP_ERROR
);
1460 * Default capture buffer size.
1461 * 32K isn't very much for modern machines with fast networks; we
1462 * pick .5M, as that's the maximum on at least some systems with BPF.
1464 * However, on AIX 3.5, the larger buffer sized caused unrecoverable
1465 * read failures under stress, so we leave it as 32K; yet another
1466 * place where AIX's BPF is broken.
1469 #define DEFAULT_BUFSIZE 32768
1471 #define DEFAULT_BUFSIZE 524288
1475 pcap_activate_bpf(pcap_t
*p
)
1477 struct pcap_bpf
*pb
= p
->priv
;
1479 #ifdef HAVE_BSD_IEEE80211
1486 char *ifrname
= ifr
.lifr_name
;
1487 const size_t ifnamsiz
= sizeof(ifr
.lifr_name
);
1490 char *ifrname
= ifr
.ifr_name
;
1491 const size_t ifnamsiz
= sizeof(ifr
.ifr_name
);
1493 struct bpf_version bv
;
1496 char *wltdev
= NULL
;
1499 struct bpf_dltlist bdl
;
1500 #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1503 #endif /* BIOCGDLTLIST */
1504 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1505 u_int spoof_eth_src
= 1;
1508 struct bpf_insn total_insn
;
1509 struct bpf_program total_prog
;
1510 struct utsname osinfo
;
1511 int have_osinfo
= 0;
1512 #ifdef HAVE_ZEROCOPY_BPF
1514 u_int bufmode
, zbufmax
;
1525 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
1526 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
1527 pcap_strerror(errno
));
1528 status
= PCAP_ERROR
;
1531 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
1532 bv
.bv_minor
< BPF_MINOR_VERSION
) {
1533 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1534 "kernel bpf filter out of date");
1535 status
= PCAP_ERROR
;
1539 #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid)
1541 * Retrieve the zoneid of the zone we are currently executing in.
1543 if ((ifr
.lifr_zoneid
= getzoneid()) == -1) {
1544 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "getzoneid(): %s",
1545 pcap_strerror(errno
));
1546 status
= PCAP_ERROR
;
1550 * Check if the given source datalink name has a '/' separated
1551 * zonename prefix string. The zonename prefixed source datalink can
1552 * be used by pcap consumers in the Solaris global zone to capture
1553 * traffic on datalinks in non-global zones. Non-global zones
1554 * do not have access to datalinks outside of their own namespace.
1556 if ((zonesep
= strchr(p
->opt
.source
, '/')) != NULL
) {
1557 char path_zname
[ZONENAME_MAX
];
1561 if (ifr
.lifr_zoneid
!= GLOBAL_ZONEID
) {
1562 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1563 "zonename/linkname only valid in global zone.");
1564 status
= PCAP_ERROR
;
1567 znamelen
= zonesep
- p
->opt
.source
;
1568 (void) strlcpy(path_zname
, p
->opt
.source
, znamelen
+ 1);
1569 ifr
.lifr_zoneid
= getzoneidbyname(path_zname
);
1570 if (ifr
.lifr_zoneid
== -1) {
1571 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1572 "getzoneidbyname(%s): %s", path_zname
,
1573 pcap_strerror(errno
));
1574 status
= PCAP_ERROR
;
1577 lnamep
= strdup(zonesep
+ 1);
1578 free(p
->opt
.source
);
1579 p
->opt
.source
= lnamep
;
1583 pb
->device
= strdup(p
->opt
.source
);
1584 if (pb
->device
== NULL
) {
1585 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
1586 pcap_strerror(errno
));
1587 status
= PCAP_ERROR
;
1592 * Attempt to find out the version of the OS on which we're running.
1594 if (uname(&osinfo
) == 0)
1599 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1600 * of why we check the version number.
1605 * We assume osinfo.sysname is "Darwin", because
1606 * __APPLE__ is defined. We just check the version.
1608 if (osinfo
.release
[0] < '8' &&
1609 osinfo
.release
[1] == '.') {
1611 * 10.3 (Darwin 7.x) or earlier.
1613 status
= PCAP_ERROR_RFMON_NOTSUP
;
1616 if (osinfo
.release
[0] == '8' &&
1617 osinfo
.release
[1] == '.') {
1619 * 10.4 (Darwin 8.x). s/en/wlt/
1621 if (strncmp(p
->opt
.source
, "en", 2) != 0) {
1623 * Not an enN device; check
1624 * whether the device even exists.
1626 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1629 p
->opt
.source
, ifnamsiz
);
1630 if (ioctl(sockfd
, SIOCGIFFLAGS
,
1631 (char *)&ifr
) < 0) {
1639 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1642 "SIOCGIFFLAGS failed: %s",
1643 pcap_strerror(errno
));
1645 status
= PCAP_ERROR_RFMON_NOTSUP
;
1649 * We can't find out whether
1650 * the device exists, so just
1651 * report "no such device".
1653 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1656 "socket() failed: %s",
1657 pcap_strerror(errno
));
1661 wltdev
= malloc(strlen(p
->opt
.source
) + 2);
1662 if (wltdev
== NULL
) {
1663 (void)snprintf(p
->errbuf
,
1664 PCAP_ERRBUF_SIZE
, "malloc: %s",
1665 pcap_strerror(errno
));
1666 status
= PCAP_ERROR
;
1669 strcpy(wltdev
, "wlt");
1670 strcat(wltdev
, p
->opt
.source
+ 2);
1671 free(p
->opt
.source
);
1672 p
->opt
.source
= wltdev
;
1675 * Everything else is 10.5 or later; for those,
1676 * we just open the enN device, and set the DLT.
1680 #endif /* __APPLE__ */
1681 #ifdef HAVE_ZEROCOPY_BPF
1683 * If the BPF extension to set buffer mode is present, try setting
1684 * the mode to zero-copy. If that fails, use regular buffering. If
1685 * it succeeds but other setup fails, return an error to the user.
1687 bufmode
= BPF_BUFMODE_ZBUF
;
1688 if (ioctl(fd
, BIOCSETBUFMODE
, (caddr_t
)&bufmode
) == 0) {
1690 * We have zerocopy BPF; use it.
1695 * How to pick a buffer size: first, query the maximum buffer
1696 * size supported by zero-copy. This also lets us quickly
1697 * determine whether the kernel generally supports zero-copy.
1698 * Then, if a buffer size was specified, use that, otherwise
1699 * query the default buffer size, which reflects kernel
1700 * policy for a desired default. Round to the nearest page
1703 if (ioctl(fd
, BIOCGETZMAX
, (caddr_t
)&zbufmax
) < 0) {
1704 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGETZMAX: %s",
1705 pcap_strerror(errno
));
1706 status
= PCAP_ERROR
;
1710 if (p
->opt
.buffer_size
!= 0) {
1712 * A buffer size was explicitly specified; use it.
1714 v
= p
->opt
.buffer_size
;
1716 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1717 v
< DEFAULT_BUFSIZE
)
1718 v
= DEFAULT_BUFSIZE
;
1721 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
1723 pb
->zbufsize
= roundup(v
, getpagesize());
1724 if (pb
->zbufsize
> zbufmax
)
1725 pb
->zbufsize
= zbufmax
;
1726 pb
->zbuf1
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
1728 pb
->zbuf2
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
1730 if (pb
->zbuf1
== MAP_FAILED
|| pb
->zbuf2
== MAP_FAILED
) {
1731 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "mmap: %s",
1732 pcap_strerror(errno
));
1733 status
= PCAP_ERROR
;
1736 memset(&bz
, 0, sizeof(bz
)); /* bzero() deprecated, replaced with memset() */
1737 bz
.bz_bufa
= pb
->zbuf1
;
1738 bz
.bz_bufb
= pb
->zbuf2
;
1739 bz
.bz_buflen
= pb
->zbufsize
;
1740 if (ioctl(fd
, BIOCSETZBUF
, (caddr_t
)&bz
) < 0) {
1741 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETZBUF: %s",
1742 pcap_strerror(errno
));
1743 status
= PCAP_ERROR
;
1746 (void)strncpy(ifrname
, p
->opt
.source
, ifnamsiz
);
1747 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
1748 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1749 p
->opt
.source
, pcap_strerror(errno
));
1750 status
= PCAP_ERROR
;
1753 v
= pb
->zbufsize
- sizeof(struct bpf_zbuf_header
);
1758 * We don't have zerocopy BPF.
1759 * Set the buffer size.
1761 if (p
->opt
.buffer_size
!= 0) {
1763 * A buffer size was explicitly specified; use it.
1765 if (ioctl(fd
, BIOCSBLEN
,
1766 (caddr_t
)&p
->opt
.buffer_size
) < 0) {
1767 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1768 "BIOCSBLEN: %s: %s", p
->opt
.source
,
1769 pcap_strerror(errno
));
1770 status
= PCAP_ERROR
;
1775 * Now bind to the device.
1777 (void)strncpy(ifrname
, p
->opt
.source
, ifnamsiz
);
1779 if (ioctl(fd
, BIOCSETLIF
, (caddr_t
)&ifr
) < 0)
1781 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0)
1784 status
= check_setif_failure(p
, errno
);
1789 * No buffer size was explicitly specified.
1791 * Try finding a good size for the buffer;
1792 * DEFAULT_BUFSIZE may be too big, so keep
1793 * cutting it in half until we find a size
1794 * that works, or run out of sizes to try.
1795 * If the default is larger, don't make it smaller.
1797 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1798 v
< DEFAULT_BUFSIZE
)
1799 v
= DEFAULT_BUFSIZE
;
1800 for ( ; v
!= 0; v
>>= 1) {
1802 * Ignore the return value - this is because the
1803 * call fails on BPF systems that don't have
1804 * kernel malloc. And if the call fails, it's
1805 * no big deal, we just continue to use the
1806 * standard buffer size.
1808 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
1810 (void)strncpy(ifrname
, p
->opt
.source
, ifnamsiz
);
1812 if (ioctl(fd
, BIOCSETLIF
, (caddr_t
)&ifr
) >= 0)
1814 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
1816 break; /* that size worked; we're done */
1818 if (errno
!= ENOBUFS
) {
1819 status
= check_setif_failure(p
, errno
);
1825 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1826 "BIOCSBLEN: %s: No buffer size worked",
1828 status
= PCAP_ERROR
;
1834 /* Get the data link layer type. */
1835 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
1836 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
1837 pcap_strerror(errno
));
1838 status
= PCAP_ERROR
;
1844 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
1867 * We don't know what to map this to yet.
1869 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
1871 status
= PCAP_ERROR
;
1875 #if _BSDI_VERSION - 0 >= 199510
1876 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
1891 case 12: /*DLT_C_HDLC*/
1899 * We know the default link type -- now determine all the DLTs
1900 * this interface supports. If this fails with EINVAL, it's
1901 * not fatal; we just don't get to use the feature later.
1903 if (get_dlt_list(fd
, v
, &bdl
, p
->errbuf
) == -1) {
1904 status
= PCAP_ERROR
;
1907 p
->dlt_count
= bdl
.bfl_len
;
1908 p
->dlt_list
= bdl
.bfl_list
;
1912 * Monitor mode fun, continued.
1914 * For 10.5 and, we're assuming, later releases, as noted above,
1915 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
1916 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
1917 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
1920 * Therefore, if the user asked for monitor mode, we filter out
1921 * the DLT_EN10MB value, as you can't get that in monitor mode,
1922 * and, if the user didn't ask for monitor mode, we filter out
1923 * the 802.11 DLT_ values, because selecting those will turn
1924 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
1925 * radio DLT_ value is offered, we try to select that, otherwise
1926 * we try to select DLT_IEEE802_11.
1929 if (isdigit((unsigned)osinfo
.release
[0]) &&
1930 (osinfo
.release
[0] == '9' ||
1931 isdigit((unsigned)osinfo
.release
[1]))) {
1933 * 10.5 (Darwin 9.x), or later.
1935 new_dlt
= find_802_11(&bdl
);
1936 if (new_dlt
!= -1) {
1938 * We have at least one 802.11 DLT_ value,
1939 * so this is an 802.11 interface.
1940 * new_dlt is the best of the 802.11
1941 * DLT_ values in the list.
1945 * Our caller wants monitor mode.
1946 * Purge DLT_EN10MB from the list
1947 * of link-layer types, as selecting
1948 * it will keep monitor mode off.
1953 * If the new mode we want isn't
1954 * the default mode, attempt to
1955 * select the new mode.
1958 if (ioctl(p
->fd
, BIOCSDLT
,
1970 * Our caller doesn't want
1971 * monitor mode. Unless this
1972 * is being done by pcap_open_live(),
1973 * purge the 802.11 link-layer types
1974 * from the list, as selecting
1975 * one of them will turn monitor
1984 * The caller requested monitor
1985 * mode, but we have no 802.11
1986 * link-layer types, so they
1989 status
= PCAP_ERROR_RFMON_NOTSUP
;
1995 #elif defined(HAVE_BSD_IEEE80211)
1997 * *BSD with the new 802.11 ioctls.
1998 * Do we want monitor mode?
2002 * Try to put the interface into monitor mode.
2004 retv
= monitor_mode(p
, 1);
2014 * We're in monitor mode.
2015 * Try to find the best 802.11 DLT_ value and, if we
2016 * succeed, try to switch to that mode if we're not
2017 * already in that mode.
2019 new_dlt
= find_802_11(&bdl
);
2020 if (new_dlt
!= -1) {
2022 * We have at least one 802.11 DLT_ value.
2023 * new_dlt is the best of the 802.11
2024 * DLT_ values in the list.
2026 * If the new mode we want isn't the default mode,
2027 * attempt to select the new mode.
2030 if (ioctl(p
->fd
, BIOCSDLT
, &new_dlt
) != -1) {
2032 * We succeeded; make this the
2040 #endif /* various platforms */
2041 #endif /* BIOCGDLTLIST */
2044 * If this is an Ethernet device, and we don't have a DLT_ list,
2045 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
2046 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
2047 * do, but there's not much we can do about that without finding
2048 * some other way of determining whether it's an Ethernet or 802.11
2051 if (v
== DLT_EN10MB
&& p
->dlt_count
== 0) {
2052 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
2054 * If that fails, just leave the list empty.
2056 if (p
->dlt_list
!= NULL
) {
2057 p
->dlt_list
[0] = DLT_EN10MB
;
2058 p
->dlt_list
[1] = DLT_DOCSIS
;
2064 p
->fddipad
= PCAP_FDDIPAD
;
2070 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
2072 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
2073 * the link-layer source address isn't forcibly overwritten.
2074 * (Should we ignore errors? Should we do this only if
2075 * we're open for writing?)
2077 * XXX - I seem to remember some packet-sending bug in some
2078 * BSDs - check CVS log for "bpf.c"?
2080 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
2081 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2082 "BIOCSHDRCMPLT: %s", pcap_strerror(errno
));
2083 status
= PCAP_ERROR
;
2088 #ifdef HAVE_ZEROCOPY_BPF
2090 * In zero-copy mode, we just use the timeout in select().
2091 * XXX - what if we're in non-blocking mode and the *application*
2092 * is using select() or poll() or kqueues or....?
2094 if (p
->opt
.timeout
&& !pb
->zerocopy
) {
2096 if (p
->opt
.timeout
) {
2099 * XXX - is this seconds/nanoseconds in AIX?
2100 * (Treating it as such doesn't fix the timeout
2101 * problem described below.)
2103 * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
2104 * 64-bit userland - it takes, as an argument, a
2105 * "struct BPF_TIMEVAL", which has 32-bit tv_sec
2106 * and tv_usec, rather than a "struct timeval".
2108 * If this platform defines "struct BPF_TIMEVAL",
2109 * we check whether the structure size in BIOCSRTIMEOUT
2110 * is that of a "struct timeval" and, if not, we use
2111 * a "struct BPF_TIMEVAL" rather than a "struct timeval".
2112 * (That way, if the bug is fixed in a future release,
2113 * we will still do the right thing.)
2116 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2117 struct BPF_TIMEVAL bpf_to
;
2119 if (IOCPARM_LEN(BIOCSRTIMEOUT
) != sizeof(struct timeval
)) {
2120 bpf_to
.tv_sec
= p
->opt
.timeout
/ 1000;
2121 bpf_to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2122 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&bpf_to
) < 0) {
2123 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2124 "BIOCSRTIMEOUT: %s", pcap_strerror(errno
));
2125 status
= PCAP_ERROR
;
2130 to
.tv_sec
= p
->opt
.timeout
/ 1000;
2131 to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2132 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
2133 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2134 "BIOCSRTIMEOUT: %s", pcap_strerror(errno
));
2135 status
= PCAP_ERROR
;
2138 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2143 #ifdef BIOCIMMEDIATE
2145 * Darren Reed notes that
2147 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
2148 * timeout appears to be ignored and it waits until the buffer
2149 * is filled before returning. The result of not having it
2150 * set is almost worse than useless if your BPF filter
2151 * is reducing things to only a few packets (i.e. one every
2154 * so we always turn BIOCIMMEDIATE mode on if this is AIX.
2156 * For other platforms, we don't turn immediate mode on by default,
2157 * as that would mean we get woken up for every packet, which
2158 * probably isn't what you want for a packet sniffer.
2160 * We set immediate mode if the caller requested it by calling
2161 * pcap_set_immediate() before calling pcap_activate().
2164 if (p
->opt
.immediate
) {
2167 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
2168 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2169 "BIOCIMMEDIATE: %s", pcap_strerror(errno
));
2170 status
= PCAP_ERROR
;
2176 #else /* BIOCIMMEDIATE */
2177 if (p
->opt
.immediate
) {
2179 * We don't support immediate mode. Fail.
2181 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Immediate mode not supported");
2182 status
= PCAP_ERROR
;
2185 #endif /* BIOCIMMEDIATE */
2187 if (p
->opt
.promisc
) {
2188 /* set promiscuous mode, just warn if it fails */
2189 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
2190 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
2191 pcap_strerror(errno
));
2192 status
= PCAP_WARNING_PROMISC_NOTSUP
;
2196 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
2197 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
2198 pcap_strerror(errno
));
2199 status
= PCAP_ERROR
;
2203 #ifdef HAVE_ZEROCOPY_BPF
2204 if (!pb
->zerocopy
) {
2206 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
2207 if (p
->buffer
== NULL
) {
2208 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
2209 pcap_strerror(errno
));
2210 status
= PCAP_ERROR
;
2214 /* For some strange reason this seems to prevent the EFAULT
2215 * problems we have experienced from AIX BPF. */
2216 memset(p
->buffer
, 0x0, p
->bufsize
);
2218 #ifdef HAVE_ZEROCOPY_BPF
2223 * If there's no filter program installed, there's
2224 * no indication to the kernel of what the snapshot
2225 * length should be, so no snapshotting is done.
2227 * Therefore, when we open the device, we install
2228 * an "accept everything" filter with the specified
2231 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
2234 total_insn
.k
= p
->snapshot
;
2236 total_prog
.bf_len
= 1;
2237 total_prog
.bf_insns
= &total_insn
;
2238 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
2239 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2240 pcap_strerror(errno
));
2241 status
= PCAP_ERROR
;
2246 * On most BPF platforms, either you can do a "select()" or
2247 * "poll()" on a BPF file descriptor and it works correctly,
2248 * or you can do it and it will return "readable" if the
2249 * hold buffer is full but not if the timeout expires *and*
2250 * a non-blocking read will, if the hold buffer is empty
2251 * but the store buffer isn't empty, rotate the buffers
2252 * and return what packets are available.
2254 * In the latter case, the fact that a non-blocking read
2255 * will give you the available packets means you can work
2256 * around the failure of "select()" and "poll()" to wake up
2257 * and return "readable" when the timeout expires by using
2258 * the timeout as the "select()" or "poll()" timeout, putting
2259 * the BPF descriptor into non-blocking mode, and read from
2260 * it regardless of whether "select()" reports it as readable
2263 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
2264 * won't wake up and return "readable" if the timer expires
2265 * and non-blocking reads return EWOULDBLOCK if the hold
2266 * buffer is empty, even if the store buffer is non-empty.
2268 * This means the workaround in question won't work.
2270 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2271 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2272 * here". On all other BPF platforms, we set it to the FD for
2273 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2274 * read will, if the hold buffer is empty and the store buffer
2275 * isn't empty, rotate the buffers and return what packets are
2276 * there (and in sufficiently recent versions of OpenBSD
2277 * "select()" and "poll()" should work correctly).
2279 * XXX - what about AIX?
2281 p
->selectable_fd
= p
->fd
; /* assume select() works until we know otherwise */
2284 * We can check what OS this is.
2286 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0) {
2287 if (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
2288 strncmp(osinfo
.release
, "4.4-", 4) == 0)
2289 p
->selectable_fd
= -1;
2293 p
->read_op
= pcap_read_bpf
;
2294 p
->inject_op
= pcap_inject_bpf
;
2295 p
->setfilter_op
= pcap_setfilter_bpf
;
2296 p
->setdirection_op
= pcap_setdirection_bpf
;
2297 p
->set_datalink_op
= pcap_set_datalink_bpf
;
2298 p
->getnonblock_op
= pcap_getnonblock_bpf
;
2299 p
->setnonblock_op
= pcap_setnonblock_bpf
;
2300 p
->stats_op
= pcap_stats_bpf
;
2301 p
->cleanup_op
= pcap_cleanup_bpf
;
2305 pcap_cleanup_bpf(p
);
2310 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
2315 #ifdef HAVE_BSD_IEEE80211
2317 monitor_mode(pcap_t
*p
, int set
)
2319 struct pcap_bpf
*pb
= p
->priv
;
2321 struct ifmediareq req
;
2327 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
2329 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "can't open socket: %s",
2330 pcap_strerror(errno
));
2331 return (PCAP_ERROR
);
2334 memset(&req
, 0, sizeof req
);
2335 strncpy(req
.ifm_name
, p
->opt
.source
, sizeof req
.ifm_name
);
2338 * Find out how many media types we have.
2340 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2342 * Can't get the media types.
2348 * There's no such device.
2351 return (PCAP_ERROR_NO_SUCH_DEVICE
);
2355 * Interface doesn't support SIOC{G,S}IFMEDIA.
2358 return (PCAP_ERROR_RFMON_NOTSUP
);
2361 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2362 "SIOCGIFMEDIA 1: %s", pcap_strerror(errno
));
2364 return (PCAP_ERROR
);
2367 if (req
.ifm_count
== 0) {
2372 return (PCAP_ERROR_RFMON_NOTSUP
);
2376 * Allocate a buffer to hold all the media types, and
2377 * get the media types.
2379 media_list
= malloc(req
.ifm_count
* sizeof(int));
2380 if (media_list
== NULL
) {
2381 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
2382 pcap_strerror(errno
));
2384 return (PCAP_ERROR
);
2386 req
.ifm_ulist
= media_list
;
2387 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2388 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCGIFMEDIA: %s",
2389 pcap_strerror(errno
));
2392 return (PCAP_ERROR
);
2396 * Look for an 802.11 "automatic" media type.
2397 * We assume that all 802.11 adapters have that media type,
2398 * and that it will carry the monitor mode supported flag.
2401 for (i
= 0; i
< req
.ifm_count
; i
++) {
2402 if (IFM_TYPE(media_list
[i
]) == IFM_IEEE80211
2403 && IFM_SUBTYPE(media_list
[i
]) == IFM_AUTO
) {
2404 /* OK, does it do monitor mode? */
2405 if (media_list
[i
] & IFM_IEEE80211_MONITOR
) {
2414 * This adapter doesn't support monitor mode.
2417 return (PCAP_ERROR_RFMON_NOTSUP
);
2422 * Don't just check whether we can enable monitor mode,
2423 * do so, if it's not already enabled.
2425 if ((req
.ifm_current
& IFM_IEEE80211_MONITOR
) == 0) {
2427 * Monitor mode isn't currently on, so turn it on,
2428 * and remember that we should turn it off when the
2433 * If we haven't already done so, arrange to have
2434 * "pcap_close_all()" called when we exit.
2436 if (!pcap_do_addexit(p
)) {
2438 * "atexit()" failed; don't put the interface
2439 * in monitor mode, just give up.
2441 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2444 return (PCAP_ERROR
);
2446 memset(&ifr
, 0, sizeof(ifr
));
2447 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
2448 sizeof(ifr
.ifr_name
));
2449 ifr
.ifr_media
= req
.ifm_current
| IFM_IEEE80211_MONITOR
;
2450 if (ioctl(sock
, SIOCSIFMEDIA
, &ifr
) == -1) {
2451 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2452 "SIOCSIFMEDIA: %s", pcap_strerror(errno
));
2454 return (PCAP_ERROR
);
2457 pb
->must_do_on_close
|= MUST_CLEAR_RFMON
;
2460 * Add this to the list of pcaps to close when we exit.
2462 pcap_add_to_pcaps_to_close(p
);
2467 #endif /* HAVE_BSD_IEEE80211 */
2469 #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
2471 * Check whether we have any 802.11 link-layer types; return the best
2472 * of the 802.11 link-layer types if we find one, and return -1
2475 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
2476 * best 802.11 link-layer type; any of the other 802.11-plus-radio
2477 * headers are second-best; 802.11 with no radio information is
2481 find_802_11(struct bpf_dltlist
*bdlp
)
2487 * Scan the list of DLT_ values, looking for 802.11 values,
2488 * and, if we find any, choose the best of them.
2491 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
2492 switch (bdlp
->bfl_list
[i
]) {
2494 case DLT_IEEE802_11
:
2496 * 802.11, but no radio.
2498 * Offer this, and select it as the new mode
2499 * unless we've already found an 802.11
2500 * header with radio information.
2503 new_dlt
= bdlp
->bfl_list
[i
];
2506 case DLT_PRISM_HEADER
:
2507 case DLT_AIRONET_HEADER
:
2508 case DLT_IEEE802_11_RADIO_AVS
:
2510 * 802.11 with radio, but not radiotap.
2512 * Offer this, and select it as the new mode
2513 * unless we've already found the radiotap DLT_.
2515 if (new_dlt
!= DLT_IEEE802_11_RADIO
)
2516 new_dlt
= bdlp
->bfl_list
[i
];
2519 case DLT_IEEE802_11_RADIO
:
2521 * 802.11 with radiotap.
2523 * Offer this, and select it as the new mode.
2525 new_dlt
= bdlp
->bfl_list
[i
];
2538 #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
2540 #if defined(__APPLE__) && defined(BIOCGDLTLIST)
2542 * Remove DLT_EN10MB from the list of DLT_ values, as we're in monitor mode,
2543 * and DLT_EN10MB isn't supported in monitor mode.
2546 remove_en(pcap_t
*p
)
2551 * Scan the list of DLT_ values and discard DLT_EN10MB.
2554 for (i
= 0; i
< p
->dlt_count
; i
++) {
2555 switch (p
->dlt_list
[i
]) {
2559 * Don't offer this one.
2565 * Just copy this mode over.
2571 * Copy this DLT_ value to its new position.
2573 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2578 * Set the DLT_ count to the number of entries we copied.
2584 * Remove 802.11 link-layer types from the list of DLT_ values, as
2585 * we're not in monitor mode, and those DLT_ values will switch us
2589 remove_802_11(pcap_t
*p
)
2594 * Scan the list of DLT_ values and discard 802.11 values.
2597 for (i
= 0; i
< p
->dlt_count
; i
++) {
2598 switch (p
->dlt_list
[i
]) {
2600 case DLT_IEEE802_11
:
2601 case DLT_PRISM_HEADER
:
2602 case DLT_AIRONET_HEADER
:
2603 case DLT_IEEE802_11_RADIO
:
2604 case DLT_IEEE802_11_RADIO_AVS
:
2606 * 802.11. Don't offer this one.
2612 * Just copy this mode over.
2618 * Copy this DLT_ value to its new position.
2620 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2625 * Set the DLT_ count to the number of entries we copied.
2629 #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
2632 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
2634 struct pcap_bpf
*pb
= p
->priv
;
2637 * Free any user-mode filter we might happen to have installed.
2639 pcap_freecode(&p
->fcode
);
2642 * Try to install the kernel filter.
2644 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) == 0) {
2648 pb
->filtering_in_kernel
= 1; /* filtering in the kernel */
2651 * Discard any previously-received packets, as they might
2652 * have passed whatever filter was formerly in effect, but
2653 * might not pass this filter (BIOCSETF discards packets
2654 * buffered in the kernel, so you can lose packets in any
2664 * If it failed with EINVAL, that's probably because the program
2665 * is invalid or too big. Validate it ourselves; if we like it
2666 * (we currently allow backward branches, to support protochain),
2667 * run it in userland. (There's no notion of "too big" for
2670 * Otherwise, just give up.
2671 * XXX - if the copy of the program into the kernel failed,
2672 * we will get EINVAL rather than, say, EFAULT on at least
2675 if (errno
!= EINVAL
) {
2676 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2677 pcap_strerror(errno
));
2682 * install_bpf_program() validates the program.
2684 * XXX - what if we already have a filter in the kernel?
2686 if (install_bpf_program(p
, fp
) < 0)
2688 pb
->filtering_in_kernel
= 0; /* filtering in userland */
2693 * Set direction flag: Which packets do we accept on a forwarding
2694 * single device? IN, OUT or both?
2697 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
2699 #if defined(BIOCSDIRECTION)
2702 direction
= (d
== PCAP_D_IN
) ? BPF_D_IN
:
2703 ((d
== PCAP_D_OUT
) ? BPF_D_OUT
: BPF_D_INOUT
);
2704 if (ioctl(p
->fd
, BIOCSDIRECTION
, &direction
) == -1) {
2705 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2706 "Cannot set direction to %s: %s",
2707 (d
== PCAP_D_IN
) ? "PCAP_D_IN" :
2708 ((d
== PCAP_D_OUT
) ? "PCAP_D_OUT" : "PCAP_D_INOUT"),
2713 #elif defined(BIOCSSEESENT)
2717 * We don't support PCAP_D_OUT.
2719 if (d
== PCAP_D_OUT
) {
2720 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2721 "Setting direction to PCAP_D_OUT is not supported on BPF");
2725 seesent
= (d
== PCAP_D_INOUT
);
2726 if (ioctl(p
->fd
, BIOCSSEESENT
, &seesent
) == -1) {
2727 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2728 "Cannot set direction to %s: %s",
2729 (d
== PCAP_D_INOUT
) ? "PCAP_D_INOUT" : "PCAP_D_IN",
2735 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2736 "This system doesn't support BIOCSSEESENT, so the direction can't be set");
2742 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
2745 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
2746 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2747 "Cannot set DLT %d: %s", dlt
, strerror(errno
));