2 * Copyright (c) 1993, 1994, 1995, 1996, 1998
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 static const char rcsid
[] _U_
=
23 "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.99.2.11 2008-04-14 20:41:51 guy Exp $ (LBL)";
30 #include <sys/param.h> /* optionally get BSD define */
32 #include <sys/timeb.h>
33 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <sys/utsname.h>
43 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
44 * native OS version, as we need "struct bpf_config" from it.
46 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
48 #include <sys/types.h>
51 * Prevent bpf.h from redefining the DLT_ values to their
52 * IFT_ values, as we're going to return the standard libpcap
53 * values, not IBM's non-standard IFT_ values.
59 #include <net/if_types.h> /* for IFT_ values */
60 #include <sys/sysconfig.h>
61 #include <sys/device.h>
62 #include <sys/cfgodm.h>
66 #define domakedev makedev64
67 #define getmajor major64
68 #define bpf_hdr bpf_hdr32
70 #define domakedev makedev
71 #define getmajor major
72 #endif /* __64BIT__ */
74 #define BPF_NAME "bpf"
76 #define DRIVER_PATH "/usr/lib/drivers"
77 #define BPF_NODE "/dev/bpf"
78 static int bpfloadedflag
= 0;
79 static int odmlockid
= 0;
95 #ifdef HAVE_NET_IF_MEDIA_H
96 # include <net/if_media.h>
102 #include "pcap-dag.h"
103 #endif /* HAVE_DAG_API */
105 #ifdef HAVE_OS_PROTO_H
106 #include "os-proto.h"
110 # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
111 #define HAVE_BSD_IEEE80211
114 # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
115 static int find_802_11(struct bpf_dltlist
*);
117 # ifdef HAVE_BSD_IEEE80211
118 static int monitor_mode(pcap_t
*, int);
121 # if defined(__APPLE__)
122 static void remove_en(pcap_t
*);
123 static void remove_802_11(pcap_t
*);
126 # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
128 #endif /* BIOCGDLTLIST */
131 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
132 * don't get DLT_DOCSIS defined.
135 #define DLT_DOCSIS 143
139 * On OS X, we don't even get any of the 802.11-plus-radio-header DLT_'s
140 * defined, even though some of them are used by various Airport drivers.
142 #ifndef DLT_PRISM_HEADER
143 #define DLT_PRISM_HEADER 119
145 #ifndef DLT_AIRONET_HEADER
146 #define DLT_AIRONET_HEADER 120
148 #ifndef DLT_IEEE802_11_RADIO
149 #define DLT_IEEE802_11_RADIO 127
151 #ifndef DLT_IEEE802_11_RADIO_AVS
152 #define DLT_IEEE802_11_RADIO_AVS 163
155 static int pcap_can_set_rfmon_bpf(pcap_t
*p
);
156 static int pcap_activate_bpf(pcap_t
*p
);
157 static int pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
);
158 static int pcap_setdirection_bpf(pcap_t
*, pcap_direction_t
);
159 static int pcap_set_datalink_bpf(pcap_t
*p
, int dlt
);
162 pcap_create(const char *device
, char *ebuf
)
167 if (strstr(device
, "dag"))
168 return (dag_create(device
, ebuf
));
169 #endif /* HAVE_DAG_API */
171 p
= pcap_create_common(device
, ebuf
);
175 p
->activate_op
= pcap_activate_bpf
;
176 p
->can_set_rfmon_op
= pcap_can_set_rfmon_bpf
;
184 #ifdef HAVE_CLONING_BPF
185 static const char device
[] = "/dev/bpf";
188 char device
[sizeof "/dev/bpf0000000000"];
193 * Load the bpf driver, if it isn't already loaded,
194 * and create the BPF device entries, if they don't
197 if (bpf_load(p
->errbuf
) == -1)
201 #ifdef HAVE_CLONING_BPF
202 if ((fd
= open(device
, O_RDWR
)) == -1 &&
203 (errno
!= EACCES
|| (fd
= open(device
, O_RDONLY
)) == -1)) {
205 fd
= PCAP_ERROR_PERM_DENIED
;
208 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
209 "(cannot open device) %s: %s", device
, pcap_strerror(errno
));
213 * Go through all the minors and find one that isn't in use.
216 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
218 * Initially try a read/write open (to allow the inject
219 * method to work). If that fails due to permission
220 * issues, fall back to read-only. This allows a
221 * non-root user to be granted specific access to pcap
222 * capabilities via file permissions.
224 * XXX - we should have an API that has a flag that
225 * controls whether to open read-only or read-write,
226 * so that denial of permission to send (or inability
227 * to send, if sending packets isn't supported on
228 * the device in question) can be indicated at open
231 fd
= open(device
, O_RDWR
);
232 if (fd
== -1 && errno
== EACCES
)
233 fd
= open(device
, O_RDONLY
);
234 } while (fd
< 0 && errno
== EBUSY
);
237 * XXX better message for all minors used
241 fd
= PCAP_ERROR_PERM_DENIED
;
244 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "(no devices found) %s: %s",
245 device
, pcap_strerror(errno
));
254 get_dlt_list(int fd
, int v
, struct bpf_dltlist
*bdlp
, char *ebuf
)
256 memset(bdlp
, 0, sizeof(*bdlp
));
257 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) == 0) {
261 bdlp
->bfl_list
= (u_int
*) malloc(sizeof(u_int
) * (bdlp
->bfl_len
+ 1));
262 if (bdlp
->bfl_list
== NULL
) {
263 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
264 pcap_strerror(errno
));
268 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) < 0) {
269 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
270 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
271 free(bdlp
->bfl_list
);
276 * OK, for real Ethernet devices, add DLT_DOCSIS to the
277 * list, so that an application can let you choose it,
278 * in case you're capturing DOCSIS traffic that a Cisco
279 * Cable Modem Termination System is putting out onto
280 * an Ethernet (it doesn't put an Ethernet header onto
281 * the wire, it puts raw DOCSIS frames out on the wire
282 * inside the low-level Ethernet framing).
284 * A "real Ethernet device" is defined here as a device
285 * that has a link-layer type of DLT_EN10MB and that has
286 * no alternate link-layer types; that's done to exclude
287 * 802.11 interfaces (which might or might not be the
288 * right thing to do, but I suspect it is - Ethernet <->
289 * 802.11 bridges would probably badly mishandle frames
290 * that don't have Ethernet headers).
292 if (v
== DLT_EN10MB
) {
294 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
295 if (bdlp
->bfl_list
[i
] != DLT_EN10MB
) {
302 * We reserved one more slot at the end of
305 bdlp
->bfl_list
[bdlp
->bfl_len
] = DLT_DOCSIS
;
311 * EINVAL just means "we don't support this ioctl on
312 * this device"; don't treat it as an error.
314 if (errno
!= EINVAL
) {
315 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
316 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
325 pcap_can_set_rfmon_bpf(pcap_t
*p
)
327 #if defined(__APPLE__)
328 struct utsname osinfo
;
332 struct bpf_dltlist bdl
;
336 * The joys of monitor mode on OS X.
338 * Prior to 10.4, it's not supported at all.
340 * In 10.4, if adapter enN supports monitor mode, there's a
341 * wltN adapter corresponding to it; you open it, instead of
342 * enN, to get monitor mode. You get whatever link-layer
343 * headers it supplies.
345 * In 10.5, and, we assume, later releases, if adapter enN
346 * supports monitor mode, it offers, among its selectable
347 * DLT_ values, values that let you get the 802.11 header;
348 * selecting one of those values puts the adapter into monitor
349 * mode (i.e., you can't get 802.11 headers except in monitor
350 * mode, and you can't get Ethernet headers in monitor mode).
352 if (uname(&osinfo
) == -1) {
354 * Can't get the OS version; just say "no".
359 * We assume osinfo.sysname is "Darwin", because
360 * __APPLE__ is defined. We just check the version.
362 if (osinfo
.release
[0] < '8' && osinfo
.release
[1] == '.') {
364 * 10.3 (Darwin 7.x) or earlier.
365 * Monitor mode not supported.
369 if (osinfo
.release
[0] == '8' && osinfo
.release
[1] == '.') {
371 * 10.4 (Darwin 8.x). s/en/wlt/, and check
372 * whether the device exists.
374 if (strncmp(p
->opt
.source
, "en", 2) != 0) {
376 * Not an enN device; no monitor mode.
380 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
382 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
383 "socket: %s", pcap_strerror(errno
));
386 strlcpy(ifr
.ifr_name
, "wlt", sizeof(ifr
.ifr_name
));
387 strlcat(ifr
.ifr_name
, p
->opt
.source
+ 2, sizeof(ifr
.ifr_name
));
388 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
401 * Everything else is 10.5 or later; for those,
402 * we just open the enN device, and check whether
403 * we have any 802.11 devices.
405 * First, open a BPF device.
412 * Now bind to the device.
414 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
, sizeof(ifr
.ifr_name
));
415 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
416 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
418 p
->opt
.source
, pcap_strerror(errno
));
424 * We know the default link type -- now determine all the DLTs
425 * this interface supports. If this fails with EINVAL, it's
426 * not fatal; we just don't get to use the feature later.
427 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
428 * as the default DLT for this adapter.)
430 if (get_dlt_list(fd
, DLT_NULL
, &bdl
, p
->errbuf
) == -1) {
434 if (find_802_11(&bdl
) != -1) {
436 * We have an 802.11 DLT, so we can set monitor mode.
443 #endif /* BIOCGDLTLIST */
445 #elif defined(HAVE_BSD_IEEE80211)
448 ret
= monitor_mode(p
, 0);
449 if (ret
== PCAP_ERROR_RFMON_NOTSUP
)
450 return (0); /* not an error, just a "can't do" */
452 return (1); /* success */
460 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
465 * "ps_recv" counts packets handed to the filter, not packets
466 * that passed the filter. This includes packets later dropped
467 * because we ran out of buffer space.
469 * "ps_drop" counts packets dropped inside the BPF device
470 * because we ran out of buffer space. It doesn't count
471 * packets dropped by the interface driver. It counts
472 * only packets that passed the filter.
474 * Both statistics include packets not yet read from the kernel
475 * by libpcap, and thus not yet seen by the application.
477 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
478 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
479 pcap_strerror(errno
));
483 ps
->ps_recv
= s
.bs_recv
;
484 ps
->ps_drop
= s
.bs_drop
;
489 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
493 register u_char
*bp
, *ep
;
501 * Has "pcap_breakloop()" been called?
505 * Yes - clear the flag that indicates that it
506 * has, and return -2 to indicate that we were
507 * told to break out of the loop.
514 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
516 /* Don't choke when we get ptraced */
525 * Sigh. More AIX wonderfulness.
527 * For some unknown reason the uiomove()
528 * operation in the bpf kernel extension
529 * used to copy the buffer into user
530 * space sometimes returns EFAULT. I have
531 * no idea why this is the case given that
532 * a kernel debugger shows the user buffer
533 * is correct. This problem appears to
534 * be mostly mitigated by the memset of
535 * the buffer before it is first used.
536 * Very strange.... Shaun Clowes
538 * In any case this means that we shouldn't
539 * treat EFAULT as a fatal error; as we
540 * don't have an API for returning
541 * a "some packets were dropped since
542 * the last packet you saw" indication,
543 * we just ignore EFAULT and keep reading.
550 #if defined(sun) && !defined(BSD)
552 * Due to a SunOS bug, after 2^31 bytes, the kernel
553 * file offset overflows and read fails with EINVAL.
554 * The lseek() to 0 will fix things.
557 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
559 (void)lseek(p
->fd
, 0L, SEEK_SET
);
565 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
566 pcap_strerror(errno
));
574 * Loop through each packet.
576 #define bhp ((struct bpf_hdr *)bp)
582 register int caplen
, hdrlen
;
585 * Has "pcap_breakloop()" been called?
586 * If so, return immediately - if we haven't read any
587 * packets, clear the flag and return -2 to indicate
588 * that we were told to break out of the loop, otherwise
589 * leave the flag set, so that the *next* call will break
590 * out of the loop without having read any packets, and
591 * return the number of packets we've processed so far.
604 caplen
= bhp
->bh_caplen
;
605 hdrlen
= bhp
->bh_hdrlen
;
608 * Short-circuit evaluation: if using BPF filter
609 * in kernel, no need to do it now - we already know
610 * the packet passed the filter.
613 * Note: the filter code was generated assuming
614 * that p->fddipad was the amount of padding
615 * before the header, as that's what's required
616 * in the kernel, so we run the filter before
617 * skipping that padding.
621 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
622 struct pcap_pkthdr pkthdr
;
624 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
627 * AIX's BPF returns seconds/nanoseconds time
628 * stamps, not seconds/microseconds time stamps.
630 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
632 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
636 pkthdr
.caplen
= caplen
- pad
;
639 if (bhp
->bh_datalen
> pad
)
640 pkthdr
.len
= bhp
->bh_datalen
- pad
;
645 pkthdr
.caplen
= caplen
;
646 pkthdr
.len
= bhp
->bh_datalen
;
648 (*callback
)(user
, &pkthdr
, datap
);
649 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
650 if (++n
>= cnt
&& cnt
> 0) {
659 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
668 pcap_inject_bpf(pcap_t
*p
, const void *buf
, size_t size
)
672 ret
= write(p
->fd
, buf
, size
);
674 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
676 * In Mac OS X, there's a bug wherein setting the
677 * BIOCSHDRCMPLT flag causes writes to fail; see,
680 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
682 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
683 * assume it's due to that bug, and turn off that flag
684 * and try again. If we succeed, it either means that
685 * somebody applied the fix from that URL, or other patches
688 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
690 * and are running a Darwin kernel with those fixes, or
691 * that Apple fixed the problem in some OS X release.
693 u_int spoof_eth_src
= 0;
695 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
696 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
697 "send: can't turn off BIOCSHDRCMPLT: %s",
698 pcap_strerror(errno
));
703 * Now try the write again.
705 ret
= write(p
->fd
, buf
, size
);
707 #endif /* __APPLE__ */
709 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
710 pcap_strerror(errno
));
718 bpf_odminit(char *errbuf
)
722 if (odm_initialize() == -1) {
723 if (odm_err_msg(odmerrno
, &errstr
) == -1)
724 errstr
= "Unknown error";
725 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
726 "bpf_load: odm_initialize failed: %s",
731 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
732 if (odm_err_msg(odmerrno
, &errstr
) == -1)
733 errstr
= "Unknown error";
734 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
735 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
744 bpf_odmcleanup(char *errbuf
)
748 if (odm_unlock(odmlockid
) == -1) {
749 if (odm_err_msg(odmerrno
, &errstr
) == -1)
750 errstr
= "Unknown error";
751 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
752 "bpf_load: odm_unlock failed: %s",
757 if (odm_terminate() == -1) {
758 if (odm_err_msg(odmerrno
, &errstr
) == -1)
759 errstr
= "Unknown error";
760 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
761 "bpf_load: odm_terminate failed: %s",
770 bpf_load(char *errbuf
)
774 int numminors
, i
, rc
;
777 struct bpf_config cfg_bpf
;
778 struct cfg_load cfg_ld
;
779 struct cfg_kmod cfg_km
;
782 * This is very very close to what happens in the real implementation
783 * but I've fixed some (unlikely) bug situations.
788 if (bpf_odminit(errbuf
) != 0)
791 major
= genmajor(BPF_NAME
);
793 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
794 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
798 minors
= getminor(major
, &numminors
, BPF_NAME
);
800 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
802 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
803 "bpf_load: genminor failed: %s",
804 pcap_strerror(errno
));
809 if (bpf_odmcleanup(errbuf
))
812 rc
= stat(BPF_NODE
"0", &sbuf
);
813 if (rc
== -1 && errno
!= ENOENT
) {
814 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
815 "bpf_load: can't stat %s: %s",
816 BPF_NODE
"0", pcap_strerror(errno
));
820 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
821 for (i
= 0; i
< BPF_MINORS
; i
++) {
822 sprintf(buf
, "%s%d", BPF_NODE
, i
);
824 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
825 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
826 "bpf_load: can't mknod %s: %s",
827 buf
, pcap_strerror(errno
));
833 /* Check if the driver is loaded */
834 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
836 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
837 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
838 (cfg_ld
.kmid
== 0)) {
839 /* Driver isn't loaded, load it now */
840 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
841 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
842 "bpf_load: could not load driver: %s",
848 /* Configure the driver */
849 cfg_km
.cmd
= CFG_INIT
;
850 cfg_km
.kmid
= cfg_ld
.kmid
;
851 cfg_km
.mdilen
= sizeof(cfg_bpf
);
852 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
853 for (i
= 0; i
< BPF_MINORS
; i
++) {
854 cfg_bpf
.devno
= domakedev(major
, i
);
855 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
856 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
857 "bpf_load: could not configure driver: %s",
870 * Turn off rfmon mode if necessary.
873 pcap_cleanup_bpf(pcap_t
*p
)
875 #ifdef HAVE_BSD_IEEE80211
877 struct ifmediareq req
;
881 if (p
->md
.must_clear
!= 0) {
883 * There's something we have to do when closing this
886 #ifdef HAVE_BSD_IEEE80211
887 if (p
->md
.must_clear
& MUST_CLEAR_RFMON
) {
889 * We put the interface into rfmon mode;
890 * take it out of rfmon mode.
892 * XXX - if somebody else wants it in rfmon
893 * mode, this code cannot know that, so it'll take
894 * it out of rfmon mode.
896 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
899 "Can't restore interface flags (socket() failed: %s).\n"
900 "Please adjust manually.\n",
903 memset(&req
, 0, sizeof(req
));
904 strncpy(req
.ifm_name
, p
->md
.device
,
905 sizeof(req
.ifm_name
));
906 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
908 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
909 "Please adjust manually.\n",
912 if (req
.ifm_current
& IFM_IEEE80211_MONITOR
) {
914 * Rfmon mode is currently on;
917 memset(&ifr
, 0, sizeof(ifr
));
918 (void)strncpy(ifr
.ifr_name
,
920 sizeof(ifr
.ifr_name
));
922 req
.ifm_current
& ~IFM_IEEE80211_MONITOR
;
923 if (ioctl(sock
, SIOCSIFMEDIA
,
926 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
927 "Please adjust manually.\n",
935 #endif /* HAVE_BSD_IEEE80211 */
938 * Take this pcap out of the list of pcaps for which we
939 * have to take the interface out of some mode.
941 pcap_remove_from_pcaps_to_close(p
);
942 p
->md
.must_clear
= 0;
945 if (p
->md
.device
!= NULL
) {
949 pcap_cleanup_live_common(p
);
953 check_setif_failure(pcap_t
*p
, int error
)
961 if (error
== ENXIO
) {
963 * No such device exists.
966 if (p
->opt
.rfmon
&& strncmp(p
->opt
.source
, "wlt", 3) == 0) {
968 * Monitor mode was requested, and we're trying
969 * to open a "wltN" device. Assume that this
970 * is 10.4 and that we were asked to open an
971 * "enN" device; if that device exists, return
972 * "monitor mode not supported on the device".
974 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
976 strlcpy(ifr
.ifr_name
, "en",
977 sizeof(ifr
.ifr_name
));
978 strlcat(ifr
.ifr_name
, p
->opt
.source
+ 3,
979 sizeof(ifr
.ifr_name
));
980 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
982 * We assume this failed because
983 * the underlying device doesn't
986 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
987 strcpy(p
->errbuf
, "");
990 * The underlying "enN" device
991 * exists, but there's no
992 * corresponding "wltN" device;
993 * that means that the "enN"
994 * device doesn't support
995 * monitor mode, probably because
996 * it's an Ethernet device rather
997 * than a wireless device.
999 err
= PCAP_ERROR_RFMON_NOTSUP
;
1004 * We can't find out whether there's
1005 * an underlying "enN" device, so
1006 * just report "no such device".
1008 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1009 strcpy(p
->errbuf
, "");
1017 strcpy(p
->errbuf
, "");
1018 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1021 * Some other error; fill in the error string, and
1022 * return PCAP_ERROR.
1024 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1025 p
->opt
.source
, pcap_strerror(errno
));
1026 return (PCAP_ERROR
);
1031 pcap_activate_bpf(pcap_t
*p
)
1036 struct bpf_version bv
;
1039 char *wltdev
= NULL
;
1042 struct bpf_dltlist bdl
;
1043 #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1046 #endif /* BIOCGDLTLIST */
1047 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1048 u_int spoof_eth_src
= 1;
1051 struct bpf_insn total_insn
;
1052 struct bpf_program total_prog
;
1053 struct utsname osinfo
;
1054 int have_osinfo
= 0;
1064 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
1065 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
1066 pcap_strerror(errno
));
1067 status
= PCAP_ERROR
;
1070 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
1071 bv
.bv_minor
< BPF_MINOR_VERSION
) {
1072 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1073 "kernel bpf filter out of date");
1074 status
= PCAP_ERROR
;
1078 p
->md
.device
= strdup(p
->opt
.source
);
1079 if (p
->md
.device
== NULL
) {
1080 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
1081 pcap_strerror(errno
));
1082 status
= PCAP_ERROR
;
1087 * Attempt to find out the version of the OS on which we're running.
1089 if (uname(&osinfo
) == 0)
1094 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1095 * of why we check the version number.
1100 * We assume osinfo.sysname is "Darwin", because
1101 * __APPLE__ is defined. We just check the version.
1103 if (osinfo
.release
[0] < '8' &&
1104 osinfo
.release
[1] == '.') {
1106 * 10.3 (Darwin 7.x) or earlier.
1108 status
= PCAP_ERROR_RFMON_NOTSUP
;
1111 if (osinfo
.release
[0] == '8' &&
1112 osinfo
.release
[1] == '.') {
1114 * 10.4 (Darwin 8.x). s/en/wlt/
1116 if (strncmp(p
->opt
.source
, "en", 2) != 0) {
1118 * Not an enN device; check
1119 * whether the device even exists.
1121 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1123 strlcpy(ifr
.ifr_name
,
1125 sizeof(ifr
.ifr_name
));
1126 if (ioctl(sockfd
, SIOCGIFFLAGS
,
1127 (char *)&ifr
) < 0) {
1135 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1136 strcpy(p
->errbuf
, "");
1138 status
= PCAP_ERROR_RFMON_NOTSUP
;
1142 * We can't find out whether
1143 * the device exists, so just
1144 * report "no such device".
1146 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1147 strcpy(p
->errbuf
, "");
1151 wltdev
= malloc(strlen(p
->opt
.source
) + 2);
1152 if (wltdev
== NULL
) {
1153 (void)snprintf(p
->errbuf
,
1154 PCAP_ERRBUF_SIZE
, "malloc: %s",
1155 pcap_strerror(errno
));
1156 status
= PCAP_ERROR
;
1159 strcpy(wltdev
, "wlt");
1160 strcat(wltdev
, p
->opt
.source
+ 2);
1161 free(p
->opt
.source
);
1162 p
->opt
.source
= wltdev
;
1165 * Everything else is 10.5 or later; for those,
1166 * we just open the enN device, and set the DLT.
1170 #endif /* __APPLE__ */
1173 * Set the buffer size.
1175 if (p
->opt
.buffer_size
!= 0) {
1177 * A buffer size was explicitly specified; use it.
1179 if (ioctl(fd
, BIOCSBLEN
, (caddr_t
)&p
->opt
.buffer_size
) < 0) {
1180 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1181 "BIOCSBLEN: %s: %s", p
->opt
.source
,
1182 pcap_strerror(errno
));
1183 status
= PCAP_ERROR
;
1188 * Now bind to the device.
1190 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
1191 sizeof(ifr
.ifr_name
));
1192 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
1193 status
= check_setif_failure(p
, errno
);
1198 * No buffer size was explicitly specified.
1200 * Try finding a good size for the buffer; 32768 may
1201 * be too big, so keep cutting it in half until we
1202 * find a size that works, or run out of sizes to try.
1203 * If the default is larger, don't make it smaller.
1205 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) || v
< 32768)
1207 for ( ; v
!= 0; v
>>= 1) {
1209 * Ignore the return value - this is because the
1210 * call fails on BPF systems that don't have
1211 * kernel malloc. And if the call fails, it's
1212 * no big deal, we just continue to use the
1213 * standard buffer size.
1215 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
1217 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
1218 sizeof(ifr
.ifr_name
));
1219 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
1220 break; /* that size worked; we're done */
1222 if (errno
!= ENOBUFS
) {
1223 status
= check_setif_failure(p
, errno
);
1229 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1230 "BIOCSBLEN: %s: No buffer size worked",
1232 status
= PCAP_ERROR
;
1237 /* Get the data link layer type. */
1238 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
1239 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
1240 pcap_strerror(errno
));
1241 status
= PCAP_ERROR
;
1247 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
1270 * We don't know what to map this to yet.
1272 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
1274 status
= PCAP_ERROR
;
1278 #if _BSDI_VERSION - 0 >= 199510
1279 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
1294 case 12: /*DLT_C_HDLC*/
1302 * We know the default link type -- now determine all the DLTs
1303 * this interface supports. If this fails with EINVAL, it's
1304 * not fatal; we just don't get to use the feature later.
1306 if (get_dlt_list(fd
, v
, &bdl
, p
->errbuf
) == -1) {
1307 status
= PCAP_ERROR
;
1310 p
->dlt_count
= bdl
.bfl_len
;
1311 p
->dlt_list
= bdl
.bfl_list
;
1315 * Monitor mode fun, continued.
1317 * For 10.5 and, we're assuming, later releases, as noted above,
1318 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
1319 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
1320 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
1323 * Therefore, if the user asked for monitor mode, we filter out
1324 * the DLT_EN10MB value, as you can't get that in monitor mode,
1325 * and, if the user didn't ask for monitor mode, we filter out
1326 * the 802.11 DLT_ values, because selecting those will turn
1327 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
1328 * radio DLT_ value is offered, we try to select that, otherwise
1329 * we try to select DLT_IEEE802_11.
1332 if (isdigit((unsigned)osinfo
.release
[0]) &&
1333 (osinfo
.release
[0] == '9' ||
1334 isdigit((unsigned)osinfo
.release
[1]))) {
1336 * 10.5 (Darwin 9.x), or later.
1338 new_dlt
= find_802_11(&bdl
);
1339 if (new_dlt
!= -1) {
1341 * We have at least one 802.11 DLT_ value,
1342 * so this is an 802.11 interface.
1343 * new_dlt is the best of the 802.11
1344 * DLT_ values in the list.
1348 * Our caller wants monitor mode.
1349 * Purge DLT_EN10MB from the list
1350 * of link-layer types, as selecting
1351 * it will keep monitor mode off.
1356 * If the new mode we want isn't
1357 * the default mode, attempt to
1358 * select the new mode.
1361 if (ioctl(p
->fd
, BIOCSDLT
,
1373 * Our caller doesn't want
1374 * monitor mode. Unless this
1375 * is being done by pcap_open_live(),
1376 * purge the 802.11 link-layer types
1377 * from the list, as selecting
1378 * one of them will turn monitor
1387 * The caller requested monitor
1388 * mode, but we have no 802.11
1389 * link-layer types, so they
1392 status
= PCAP_ERROR_RFMON_NOTSUP
;
1398 #elif defined(HAVE_BSD_IEEE80211)
1400 * *BSD with the new 802.11 ioctls.
1401 * Do we want monitor mode?
1405 * Try to put the interface into monitor mode.
1407 status
= monitor_mode(p
, 1);
1416 * We're in monitor mode.
1417 * Try to find the best 802.11 DLT_ value and, if we
1418 * succeed, try to switch to that mode if we're not
1419 * already in that mode.
1421 new_dlt
= find_802_11(&bdl
);
1422 if (new_dlt
!= -1) {
1424 * We have at least one 802.11 DLT_ value.
1425 * new_dlt is the best of the 802.11
1426 * DLT_ values in the list.
1428 * If the new mode we want isn't the default mode,
1429 * attempt to select the new mode.
1432 if (ioctl(p
->fd
, BIOCSDLT
, &new_dlt
) != -1) {
1434 * We succeeded; make this the
1442 #endif /* various platforms */
1443 #endif /* BIOCGDLTLIST */
1446 * If this is an Ethernet device, and we don't have a DLT_ list,
1447 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
1448 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
1449 * do, but there's not much we can do about that without finding
1450 * some other way of determining whether it's an Ethernet or 802.11
1453 if (v
== DLT_EN10MB
&& p
->dlt_count
== 0) {
1454 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
1456 * If that fails, just leave the list empty.
1458 if (p
->dlt_list
!= NULL
) {
1459 p
->dlt_list
[0] = DLT_EN10MB
;
1460 p
->dlt_list
[1] = DLT_DOCSIS
;
1466 p
->fddipad
= PCAP_FDDIPAD
;
1472 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1474 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
1475 * the link-layer source address isn't forcibly overwritten.
1476 * (Should we ignore errors? Should we do this only if
1477 * we're open for writing?)
1479 * XXX - I seem to remember some packet-sending bug in some
1480 * BSDs - check CVS log for "bpf.c"?
1482 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1483 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1484 "BIOCSHDRCMPLT: %s", pcap_strerror(errno
));
1485 status
= PCAP_ERROR
;
1490 if (p
->md
.timeout
!= 0) {
1492 * XXX - is this seconds/nanoseconds in AIX?
1493 * (Treating it as such doesn't fix the timeout
1494 * problem described below.)
1497 to
.tv_sec
= p
->md
.timeout
/ 1000;
1498 to
.tv_usec
= (p
->md
.timeout
* 1000) % 1000000;
1499 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
1500 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT: %s",
1501 pcap_strerror(errno
));
1502 status
= PCAP_ERROR
;
1508 #ifdef BIOCIMMEDIATE
1510 * Darren Reed notes that
1512 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
1513 * timeout appears to be ignored and it waits until the buffer
1514 * is filled before returning. The result of not having it
1515 * set is almost worse than useless if your BPF filter
1516 * is reducing things to only a few packets (i.e. one every
1519 * so we turn BIOCIMMEDIATE mode on if this is AIX.
1521 * We don't turn it on for other platforms, as that means we
1522 * get woken up for every packet, which may not be what we want;
1523 * in the Winter 1993 USENIX paper on BPF, they say:
1525 * Since a process might want to look at every packet on a
1526 * network and the time between packets can be only a few
1527 * microseconds, it is not possible to do a read system call
1528 * per packet and BPF must collect the data from several
1529 * packets and return it as a unit when the monitoring
1530 * application does a read.
1532 * which I infer is the reason for the timeout - it means we
1533 * wait that amount of time, in the hopes that more packets
1534 * will arrive and we'll get them all with one read.
1536 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
1537 * BSDs) causes the timeout to be ignored.
1539 * On the other hand, some platforms (e.g., Linux) don't support
1540 * timeouts, they just hand stuff to you as soon as it arrives;
1541 * if that doesn't cause a problem on those platforms, it may
1542 * be OK to have BIOCIMMEDIATE mode on BSD as well.
1544 * (Note, though, that applications may depend on the read
1545 * completing, even if no packets have arrived, when the timeout
1546 * expires, e.g. GUI applications that have to check for input
1547 * while waiting for packets to arrive; a non-zero timeout
1548 * prevents "select()" from working right on FreeBSD and
1549 * possibly other BSDs, as the timer doesn't start until a
1550 * "read()" is done, so the timer isn't in effect if the
1551 * application is blocked on a "select()", and the "select()"
1552 * doesn't get woken up for a BPF device until the buffer
1556 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
1557 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCIMMEDIATE: %s",
1558 pcap_strerror(errno
));
1559 status
= PCAP_ERROR
;
1562 #endif /* BIOCIMMEDIATE */
1565 if (p
->opt
.promisc
) {
1566 /* set promiscuous mode, just warn if it fails */
1567 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
1568 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
1569 pcap_strerror(errno
));
1570 status
= PCAP_WARNING_PROMISC_NOTSUP
;
1574 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
1575 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
1576 pcap_strerror(errno
));
1577 status
= PCAP_ERROR
;
1581 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
1582 if (p
->buffer
== NULL
) {
1583 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
1584 pcap_strerror(errno
));
1585 status
= PCAP_ERROR
;
1589 /* For some strange reason this seems to prevent the EFAULT
1590 * problems we have experienced from AIX BPF. */
1591 memset(p
->buffer
, 0x0, p
->bufsize
);
1595 * If there's no filter program installed, there's
1596 * no indication to the kernel of what the snapshot
1597 * length should be, so no snapshotting is done.
1599 * Therefore, when we open the device, we install
1600 * an "accept everything" filter with the specified
1603 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
1606 total_insn
.k
= p
->snapshot
;
1608 total_prog
.bf_len
= 1;
1609 total_prog
.bf_insns
= &total_insn
;
1610 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
1611 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
1612 pcap_strerror(errno
));
1613 status
= PCAP_ERROR
;
1618 * On most BPF platforms, either you can do a "select()" or
1619 * "poll()" on a BPF file descriptor and it works correctly,
1620 * or you can do it and it will return "readable" if the
1621 * hold buffer is full but not if the timeout expires *and*
1622 * a non-blocking read will, if the hold buffer is empty
1623 * but the store buffer isn't empty, rotate the buffers
1624 * and return what packets are available.
1626 * In the latter case, the fact that a non-blocking read
1627 * will give you the available packets means you can work
1628 * around the failure of "select()" and "poll()" to wake up
1629 * and return "readable" when the timeout expires by using
1630 * the timeout as the "select()" or "poll()" timeout, putting
1631 * the BPF descriptor into non-blocking mode, and read from
1632 * it regardless of whether "select()" reports it as readable
1635 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
1636 * won't wake up and return "readable" if the timer expires
1637 * and non-blocking reads return EWOULDBLOCK if the hold
1638 * buffer is empty, even if the store buffer is non-empty.
1640 * This means the workaround in question won't work.
1642 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
1643 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
1644 * here". On all other BPF platforms, we set it to the FD for
1645 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
1646 * read will, if the hold buffer is empty and the store buffer
1647 * isn't empty, rotate the buffers and return what packets are
1648 * there (and in sufficiently recent versions of OpenBSD
1649 * "select()" and "poll()" should work correctly).
1651 * XXX - what about AIX?
1653 p
->selectable_fd
= p
->fd
; /* assume select() works until we know otherwise */
1656 * We can check what OS this is.
1658 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0) {
1659 if (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
1660 strncmp(osinfo
.release
, "4.4-", 4) == 0)
1661 p
->selectable_fd
= -1;
1665 p
->read_op
= pcap_read_bpf
;
1666 p
->inject_op
= pcap_inject_bpf
;
1667 p
->setfilter_op
= pcap_setfilter_bpf
;
1668 p
->setdirection_op
= pcap_setdirection_bpf
;
1669 p
->set_datalink_op
= pcap_set_datalink_bpf
;
1670 p
->getnonblock_op
= pcap_getnonblock_fd
;
1671 p
->setnonblock_op
= pcap_setnonblock_fd
;
1672 p
->stats_op
= pcap_stats_bpf
;
1673 p
->cleanup_op
= pcap_cleanup_bpf
;
1677 pcap_cleanup_bpf(p
);
1682 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
1685 if (dag_platform_finddevs(alldevsp
, errbuf
) < 0)
1687 #endif /* HAVE_DAG_API */
1692 #ifdef HAVE_BSD_IEEE80211
1694 monitor_mode(pcap_t
*p
, int set
)
1697 struct ifmediareq req
;
1703 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
1705 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "can't open socket: %s",
1706 pcap_strerror(errno
));
1707 return (PCAP_ERROR
);
1710 memset(&req
, 0, sizeof req
);
1711 strncpy(req
.ifm_name
, p
->opt
.source
, sizeof req
.ifm_name
);
1714 * Find out how many media types we have.
1716 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1718 * Can't get the media types.
1720 if (errno
== EINVAL
) {
1722 * Interface doesn't support SIOC{G,S}IFMEDIA.
1725 return (PCAP_ERROR_RFMON_NOTSUP
);
1727 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCGIFMEDIA 1: %s",
1728 pcap_strerror(errno
));
1730 return (PCAP_ERROR
);
1732 if (req
.ifm_count
== 0) {
1737 return (PCAP_ERROR_RFMON_NOTSUP
);
1741 * Allocate a buffer to hold all the media types, and
1742 * get the media types.
1744 media_list
= malloc(req
.ifm_count
* sizeof(int));
1745 if (media_list
== NULL
) {
1746 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
1747 pcap_strerror(errno
));
1749 return (PCAP_ERROR
);
1751 req
.ifm_ulist
= media_list
;
1752 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1753 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCGIFMEDIA: %s",
1754 pcap_strerror(errno
));
1757 return (PCAP_ERROR
);
1761 * Look for an 802.11 "automatic" media type.
1762 * We assume that all 802.11 adapters have that media type,
1763 * and that it will carry the monitor mode supported flag.
1766 for (i
= 0; i
< req
.ifm_count
; i
++) {
1767 if (IFM_TYPE(media_list
[i
]) == IFM_IEEE80211
1768 && IFM_SUBTYPE(media_list
[i
]) == IFM_AUTO
) {
1769 /* OK, does it do monitor mode? */
1770 if (media_list
[i
] & IFM_IEEE80211_MONITOR
) {
1779 * This adapter doesn't support monitor mode.
1782 return (PCAP_ERROR_RFMON_NOTSUP
);
1787 * Don't just check whether we can enable monitor mode,
1788 * do so, if it's not already enabled.
1790 if ((req
.ifm_current
& IFM_IEEE80211_MONITOR
) == 0) {
1792 * Monitor mode isn't currently on, so turn it on,
1793 * and remember that we should turn it off when the
1798 * If we haven't already done so, arrange to have
1799 * "pcap_close_all()" called when we exit.
1801 if (!pcap_do_addexit(p
)) {
1803 * "atexit()" failed; don't put the interface
1804 * in monitor mode, just give up.
1806 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1809 return (PCAP_ERROR
);
1811 memset(&ifr
, 0, sizeof(ifr
));
1812 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
1813 sizeof(ifr
.ifr_name
));
1814 ifr
.ifr_media
= req
.ifm_current
| IFM_IEEE80211_MONITOR
;
1815 if (ioctl(sock
, SIOCSIFMEDIA
, &ifr
) == -1) {
1816 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1817 "SIOCSIFMEDIA: %s", pcap_strerror(errno
));
1819 return (PCAP_ERROR
);
1822 p
->md
.must_clear
|= MUST_CLEAR_RFMON
;
1825 * Add this to the list of pcaps to close when we exit.
1827 pcap_add_to_pcaps_to_close(p
);
1832 #endif /* HAVE_BSD_IEEE80211 */
1834 #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
1836 * Check whether we have any 802.11 link-layer types; return the best
1837 * of the 802.11 link-layer types if we find one, and return -1
1840 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
1841 * best 802.11 link-layer type; any of the other 802.11-plus-radio
1842 * headers are second-best; 802.11 with no radio information is
1846 find_802_11(struct bpf_dltlist
*bdlp
)
1852 * Scan the list of DLT_ values, looking for 802.11 values,
1853 * and, if we find any, choose the best of them.
1856 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
1857 switch (bdlp
->bfl_list
[i
]) {
1859 case DLT_IEEE802_11
:
1861 * 802.11, but no radio.
1863 * Offer this, and select it as the new mode
1864 * unless we've already found an 802.11
1865 * header with radio information.
1868 new_dlt
= bdlp
->bfl_list
[i
];
1871 case DLT_PRISM_HEADER
:
1872 case DLT_AIRONET_HEADER
:
1873 case DLT_IEEE802_11_RADIO_AVS
:
1875 * 802.11 with radio, but not radiotap.
1877 * Offer this, and select it as the new mode
1878 * unless we've already found the radiotap DLT_.
1880 if (new_dlt
!= DLT_IEEE802_11_RADIO
)
1881 new_dlt
= bdlp
->bfl_list
[i
];
1884 case DLT_IEEE802_11_RADIO
:
1886 * 802.11 with radiotap.
1888 * Offer this, and select it as the new mode.
1890 new_dlt
= bdlp
->bfl_list
[i
];
1903 #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
1905 #if defined(__APPLE__) && defined(BIOCGDLTLIST)
1907 * Remove DLT_EN10MB from the list of DLT_ values.
1910 remove_en(pcap_t
*p
)
1915 * Scan the list of DLT_ values and discard DLT_EN10MB.
1918 for (i
= 0; i
< p
->dlt_count
; i
++) {
1919 switch (p
->dlt_list
[i
]) {
1923 * Don't offer this one.
1929 * Just copy this mode over.
1935 * Copy this DLT_ value to its new position.
1937 p
->dlt_list
[j
] = p
->dlt_list
[i
];
1942 * Set the DLT_ count to the number of entries we copied.
1948 * Remove DLT_EN10MB from the list of DLT_ values, and look for the
1949 * best 802.11 link-layer type in that list and return it.
1950 * Radiotap is better than anything else; 802.11 with any other radio
1951 * header is better than 802.11 with no radio header.
1954 remove_802_11(pcap_t
*p
)
1959 * Scan the list of DLT_ values and discard 802.11 values.
1962 for (i
= 0; i
< p
->dlt_count
; i
++) {
1963 switch (p
->dlt_list
[i
]) {
1965 case DLT_IEEE802_11
:
1966 case DLT_PRISM_HEADER
:
1967 case DLT_AIRONET_HEADER
:
1968 case DLT_IEEE802_11_RADIO
:
1969 case DLT_IEEE802_11_RADIO_AVS
:
1971 * 802.11. Don't offer this one.
1977 * Just copy this mode over.
1983 * Copy this DLT_ value to its new position.
1985 p
->dlt_list
[j
] = p
->dlt_list
[i
];
1990 * Set the DLT_ count to the number of entries we copied.
1994 #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
1997 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
2000 * Free any user-mode filter we might happen to have installed.
2002 pcap_freecode(&p
->fcode
);
2005 * Try to install the kernel filter.
2007 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) == 0) {
2011 p
->md
.use_bpf
= 1; /* filtering in the kernel */
2014 * Discard any previously-received packets, as they might
2015 * have passed whatever filter was formerly in effect, but
2016 * might not pass this filter (BIOCSETF discards packets
2017 * buffered in the kernel, so you can lose packets in any
2027 * If it failed with EINVAL, that's probably because the program
2028 * is invalid or too big. Validate it ourselves; if we like it
2029 * (we currently allow backward branches, to support protochain),
2030 * run it in userland. (There's no notion of "too big" for
2033 * Otherwise, just give up.
2034 * XXX - if the copy of the program into the kernel failed,
2035 * we will get EINVAL rather than, say, EFAULT on at least
2038 if (errno
!= EINVAL
) {
2039 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2040 pcap_strerror(errno
));
2045 * install_bpf_program() validates the program.
2047 * XXX - what if we already have a filter in the kernel?
2049 if (install_bpf_program(p
, fp
) < 0)
2051 p
->md
.use_bpf
= 0; /* filtering in userland */
2056 * Set direction flag: Which packets do we accept on a forwarding
2057 * single device? IN, OUT or both?
2060 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
2062 #if defined(BIOCSDIRECTION)
2065 direction
= (d
== PCAP_D_IN
) ? BPF_D_IN
:
2066 ((d
== PCAP_D_OUT
) ? BPF_D_OUT
: BPF_D_INOUT
);
2067 if (ioctl(p
->fd
, BIOCSDIRECTION
, &direction
) == -1) {
2068 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2069 "Cannot set direction to %s: %s",
2070 (d
== PCAP_D_IN
) ? "PCAP_D_IN" :
2071 ((d
== PCAP_D_OUT
) ? "PCAP_D_OUT" : "PCAP_D_INOUT"),
2076 #elif defined(BIOCSSEESENT)
2080 * We don't support PCAP_D_OUT.
2082 if (d
== PCAP_D_OUT
) {
2083 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2084 "Setting direction to PCAP_D_OUT is not supported on BPF");
2088 seesent
= (d
== PCAP_D_INOUT
);
2089 if (ioctl(p
->fd
, BIOCSSEESENT
, &seesent
) == -1) {
2090 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2091 "Cannot set direction to %s: %s",
2092 (d
== PCAP_D_INOUT
) ? "PCAP_D_INOUT" : "PCAP_D_IN",
2098 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2099 "This system doesn't support BIOCSSEESENT, so the direction can't be set");
2105 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
2108 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
2109 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2110 "Cannot set DLT %d: %s", dlt
, strerror(errno
));