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.111 2008-07-01 08:02:33 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 if (errno
== ENETDOWN
) {
418 * Return a "network down" indication, so that
419 * the application can report that rather than
420 * saying we had a mysterious failure and
421 * suggest that they report a problem to the
422 * libpcap developers.
425 return (PCAP_ERROR_IFACE_NOT_UP
);
427 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
429 p
->opt
.source
, pcap_strerror(errno
));
436 * We know the default link type -- now determine all the DLTs
437 * this interface supports. If this fails with EINVAL, it's
438 * not fatal; we just don't get to use the feature later.
439 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
440 * as the default DLT for this adapter.)
442 if (get_dlt_list(fd
, DLT_NULL
, &bdl
, p
->errbuf
) == -1) {
446 if (find_802_11(&bdl
) != -1) {
448 * We have an 802.11 DLT, so we can set monitor mode.
455 #endif /* BIOCGDLTLIST */
457 #elif defined(HAVE_BSD_IEEE80211)
460 ret
= monitor_mode(p
, 0);
461 if (ret
== PCAP_ERROR_RFMON_NOTSUP
)
462 return (0); /* not an error, just a "can't do" */
464 return (1); /* success */
472 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
477 * "ps_recv" counts packets handed to the filter, not packets
478 * that passed the filter. This includes packets later dropped
479 * because we ran out of buffer space.
481 * "ps_drop" counts packets dropped inside the BPF device
482 * because we ran out of buffer space. It doesn't count
483 * packets dropped by the interface driver. It counts
484 * only packets that passed the filter.
486 * Both statistics include packets not yet read from the kernel
487 * by libpcap, and thus not yet seen by the application.
489 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
490 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
491 pcap_strerror(errno
));
495 ps
->ps_recv
= s
.bs_recv
;
496 ps
->ps_drop
= s
.bs_drop
;
501 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
505 register u_char
*bp
, *ep
;
513 * Has "pcap_breakloop()" been called?
517 * Yes - clear the flag that indicates that it
518 * has, and return -2 to indicate that we were
519 * told to break out of the loop.
526 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
528 /* Don't choke when we get ptraced */
537 * Sigh. More AIX wonderfulness.
539 * For some unknown reason the uiomove()
540 * operation in the bpf kernel extension
541 * used to copy the buffer into user
542 * space sometimes returns EFAULT. I have
543 * no idea why this is the case given that
544 * a kernel debugger shows the user buffer
545 * is correct. This problem appears to
546 * be mostly mitigated by the memset of
547 * the buffer before it is first used.
548 * Very strange.... Shaun Clowes
550 * In any case this means that we shouldn't
551 * treat EFAULT as a fatal error; as we
552 * don't have an API for returning
553 * a "some packets were dropped since
554 * the last packet you saw" indication,
555 * we just ignore EFAULT and keep reading.
562 #if defined(sun) && !defined(BSD)
564 * Due to a SunOS bug, after 2^31 bytes, the kernel
565 * file offset overflows and read fails with EINVAL.
566 * The lseek() to 0 will fix things.
569 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
571 (void)lseek(p
->fd
, 0L, SEEK_SET
);
577 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
578 pcap_strerror(errno
));
586 * Loop through each packet.
588 #define bhp ((struct bpf_hdr *)bp)
594 register int caplen
, hdrlen
;
597 * Has "pcap_breakloop()" been called?
598 * If so, return immediately - if we haven't read any
599 * packets, clear the flag and return -2 to indicate
600 * that we were told to break out of the loop, otherwise
601 * leave the flag set, so that the *next* call will break
602 * out of the loop without having read any packets, and
603 * return the number of packets we've processed so far.
616 caplen
= bhp
->bh_caplen
;
617 hdrlen
= bhp
->bh_hdrlen
;
620 * Short-circuit evaluation: if using BPF filter
621 * in kernel, no need to do it now - we already know
622 * the packet passed the filter.
625 * Note: the filter code was generated assuming
626 * that p->fddipad was the amount of padding
627 * before the header, as that's what's required
628 * in the kernel, so we run the filter before
629 * skipping that padding.
633 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
634 struct pcap_pkthdr pkthdr
;
636 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
639 * AIX's BPF returns seconds/nanoseconds time
640 * stamps, not seconds/microseconds time stamps.
642 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
644 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
648 pkthdr
.caplen
= caplen
- pad
;
651 if (bhp
->bh_datalen
> pad
)
652 pkthdr
.len
= bhp
->bh_datalen
- pad
;
657 pkthdr
.caplen
= caplen
;
658 pkthdr
.len
= bhp
->bh_datalen
;
660 (*callback
)(user
, &pkthdr
, datap
);
661 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
662 if (++n
>= cnt
&& cnt
> 0) {
671 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
680 pcap_inject_bpf(pcap_t
*p
, const void *buf
, size_t size
)
684 ret
= write(p
->fd
, buf
, size
);
686 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
688 * In Mac OS X, there's a bug wherein setting the
689 * BIOCSHDRCMPLT flag causes writes to fail; see,
692 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
694 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
695 * assume it's due to that bug, and turn off that flag
696 * and try again. If we succeed, it either means that
697 * somebody applied the fix from that URL, or other patches
700 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
702 * and are running a Darwin kernel with those fixes, or
703 * that Apple fixed the problem in some OS X release.
705 u_int spoof_eth_src
= 0;
707 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
708 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
709 "send: can't turn off BIOCSHDRCMPLT: %s",
710 pcap_strerror(errno
));
715 * Now try the write again.
717 ret
= write(p
->fd
, buf
, size
);
719 #endif /* __APPLE__ */
721 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
722 pcap_strerror(errno
));
730 bpf_odminit(char *errbuf
)
734 if (odm_initialize() == -1) {
735 if (odm_err_msg(odmerrno
, &errstr
) == -1)
736 errstr
= "Unknown error";
737 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
738 "bpf_load: odm_initialize failed: %s",
743 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
744 if (odm_err_msg(odmerrno
, &errstr
) == -1)
745 errstr
= "Unknown error";
746 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
747 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
756 bpf_odmcleanup(char *errbuf
)
760 if (odm_unlock(odmlockid
) == -1) {
761 if (odm_err_msg(odmerrno
, &errstr
) == -1)
762 errstr
= "Unknown error";
763 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
764 "bpf_load: odm_unlock failed: %s",
769 if (odm_terminate() == -1) {
770 if (odm_err_msg(odmerrno
, &errstr
) == -1)
771 errstr
= "Unknown error";
772 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
773 "bpf_load: odm_terminate failed: %s",
782 bpf_load(char *errbuf
)
786 int numminors
, i
, rc
;
789 struct bpf_config cfg_bpf
;
790 struct cfg_load cfg_ld
;
791 struct cfg_kmod cfg_km
;
794 * This is very very close to what happens in the real implementation
795 * but I've fixed some (unlikely) bug situations.
800 if (bpf_odminit(errbuf
) != 0)
803 major
= genmajor(BPF_NAME
);
805 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
806 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
810 minors
= getminor(major
, &numminors
, BPF_NAME
);
812 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
814 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
815 "bpf_load: genminor failed: %s",
816 pcap_strerror(errno
));
821 if (bpf_odmcleanup(errbuf
))
824 rc
= stat(BPF_NODE
"0", &sbuf
);
825 if (rc
== -1 && errno
!= ENOENT
) {
826 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
827 "bpf_load: can't stat %s: %s",
828 BPF_NODE
"0", pcap_strerror(errno
));
832 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
833 for (i
= 0; i
< BPF_MINORS
; i
++) {
834 sprintf(buf
, "%s%d", BPF_NODE
, i
);
836 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
837 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
838 "bpf_load: can't mknod %s: %s",
839 buf
, pcap_strerror(errno
));
845 /* Check if the driver is loaded */
846 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
848 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
849 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
850 (cfg_ld
.kmid
== 0)) {
851 /* Driver isn't loaded, load it now */
852 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
853 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
854 "bpf_load: could not load driver: %s",
860 /* Configure the driver */
861 cfg_km
.cmd
= CFG_INIT
;
862 cfg_km
.kmid
= cfg_ld
.kmid
;
863 cfg_km
.mdilen
= sizeof(cfg_bpf
);
864 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
865 for (i
= 0; i
< BPF_MINORS
; i
++) {
866 cfg_bpf
.devno
= domakedev(major
, i
);
867 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
868 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
869 "bpf_load: could not configure driver: %s",
882 * Turn off rfmon mode if necessary.
885 pcap_cleanup_bpf(pcap_t
*p
)
887 #ifdef HAVE_BSD_IEEE80211
889 struct ifmediareq req
;
893 if (p
->md
.must_clear
!= 0) {
895 * There's something we have to do when closing this
898 #ifdef HAVE_BSD_IEEE80211
899 if (p
->md
.must_clear
& MUST_CLEAR_RFMON
) {
901 * We put the interface into rfmon mode;
902 * take it out of rfmon mode.
904 * XXX - if somebody else wants it in rfmon
905 * mode, this code cannot know that, so it'll take
906 * it out of rfmon mode.
908 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
911 "Can't restore interface flags (socket() failed: %s).\n"
912 "Please adjust manually.\n",
915 memset(&req
, 0, sizeof(req
));
916 strncpy(req
.ifm_name
, p
->md
.device
,
917 sizeof(req
.ifm_name
));
918 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
920 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
921 "Please adjust manually.\n",
924 if (req
.ifm_current
& IFM_IEEE80211_MONITOR
) {
926 * Rfmon mode is currently on;
929 memset(&ifr
, 0, sizeof(ifr
));
930 (void)strncpy(ifr
.ifr_name
,
932 sizeof(ifr
.ifr_name
));
934 req
.ifm_current
& ~IFM_IEEE80211_MONITOR
;
935 if (ioctl(sock
, SIOCSIFMEDIA
,
938 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
939 "Please adjust manually.\n",
947 #endif /* HAVE_BSD_IEEE80211 */
950 * Take this pcap out of the list of pcaps for which we
951 * have to take the interface out of some mode.
953 pcap_remove_from_pcaps_to_close(p
);
954 p
->md
.must_clear
= 0;
957 if (p
->md
.device
!= NULL
) {
961 pcap_cleanup_live_common(p
);
965 check_setif_failure(pcap_t
*p
, int error
)
973 if (error
== ENXIO
) {
975 * No such device exists.
978 if (p
->opt
.rfmon
&& strncmp(p
->opt
.source
, "wlt", 3) == 0) {
980 * Monitor mode was requested, and we're trying
981 * to open a "wltN" device. Assume that this
982 * is 10.4 and that we were asked to open an
983 * "enN" device; if that device exists, return
984 * "monitor mode not supported on the device".
986 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
988 strlcpy(ifr
.ifr_name
, "en",
989 sizeof(ifr
.ifr_name
));
990 strlcat(ifr
.ifr_name
, p
->opt
.source
+ 3,
991 sizeof(ifr
.ifr_name
));
992 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
994 * We assume this failed because
995 * the underlying device doesn't
998 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
999 strcpy(p
->errbuf
, "");
1002 * The underlying "enN" device
1003 * exists, but there's no
1004 * corresponding "wltN" device;
1005 * that means that the "enN"
1006 * device doesn't support
1007 * monitor mode, probably because
1008 * it's an Ethernet device rather
1009 * than a wireless device.
1011 err
= PCAP_ERROR_RFMON_NOTSUP
;
1016 * We can't find out whether there's
1017 * an underlying "enN" device, so
1018 * just report "no such device".
1020 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1021 strcpy(p
->errbuf
, "");
1029 strcpy(p
->errbuf
, "");
1030 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1031 } else if (errno
== ENETDOWN
) {
1033 * Return a "network down" indication, so that
1034 * the application can report that rather than
1035 * saying we had a mysterious failure and
1036 * suggest that they report a problem to the
1037 * libpcap developers.
1039 return (PCAP_ERROR_IFACE_NOT_UP
);
1042 * Some other error; fill in the error string, and
1043 * return PCAP_ERROR.
1045 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1046 p
->opt
.source
, pcap_strerror(errno
));
1047 return (PCAP_ERROR
);
1052 pcap_activate_bpf(pcap_t
*p
)
1057 struct bpf_version bv
;
1060 char *wltdev
= NULL
;
1063 struct bpf_dltlist bdl
;
1064 #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1067 #endif /* BIOCGDLTLIST */
1068 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1069 u_int spoof_eth_src
= 1;
1072 struct bpf_insn total_insn
;
1073 struct bpf_program total_prog
;
1074 struct utsname osinfo
;
1075 int have_osinfo
= 0;
1085 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
1086 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
1087 pcap_strerror(errno
));
1088 status
= PCAP_ERROR
;
1091 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
1092 bv
.bv_minor
< BPF_MINOR_VERSION
) {
1093 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1094 "kernel bpf filter out of date");
1095 status
= PCAP_ERROR
;
1099 p
->md
.device
= strdup(p
->opt
.source
);
1100 if (p
->md
.device
== NULL
) {
1101 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
1102 pcap_strerror(errno
));
1103 status
= PCAP_ERROR
;
1108 * Attempt to find out the version of the OS on which we're running.
1110 if (uname(&osinfo
) == 0)
1115 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1116 * of why we check the version number.
1121 * We assume osinfo.sysname is "Darwin", because
1122 * __APPLE__ is defined. We just check the version.
1124 if (osinfo
.release
[0] < '8' &&
1125 osinfo
.release
[1] == '.') {
1127 * 10.3 (Darwin 7.x) or earlier.
1129 status
= PCAP_ERROR_RFMON_NOTSUP
;
1132 if (osinfo
.release
[0] == '8' &&
1133 osinfo
.release
[1] == '.') {
1135 * 10.4 (Darwin 8.x). s/en/wlt/
1137 if (strncmp(p
->opt
.source
, "en", 2) != 0) {
1139 * Not an enN device; check
1140 * whether the device even exists.
1142 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1144 strlcpy(ifr
.ifr_name
,
1146 sizeof(ifr
.ifr_name
));
1147 if (ioctl(sockfd
, SIOCGIFFLAGS
,
1148 (char *)&ifr
) < 0) {
1156 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1157 strcpy(p
->errbuf
, "");
1159 status
= PCAP_ERROR_RFMON_NOTSUP
;
1163 * We can't find out whether
1164 * the device exists, so just
1165 * report "no such device".
1167 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1168 strcpy(p
->errbuf
, "");
1172 wltdev
= malloc(strlen(p
->opt
.source
) + 2);
1173 if (wltdev
== NULL
) {
1174 (void)snprintf(p
->errbuf
,
1175 PCAP_ERRBUF_SIZE
, "malloc: %s",
1176 pcap_strerror(errno
));
1177 status
= PCAP_ERROR
;
1180 strcpy(wltdev
, "wlt");
1181 strcat(wltdev
, p
->opt
.source
+ 2);
1182 free(p
->opt
.source
);
1183 p
->opt
.source
= wltdev
;
1186 * Everything else is 10.5 or later; for those,
1187 * we just open the enN device, and set the DLT.
1191 #endif /* __APPLE__ */
1194 * Set the buffer size.
1196 if (p
->opt
.buffer_size
!= 0) {
1198 * A buffer size was explicitly specified; use it.
1200 if (ioctl(fd
, BIOCSBLEN
, (caddr_t
)&p
->opt
.buffer_size
) < 0) {
1201 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1202 "BIOCSBLEN: %s: %s", p
->opt
.source
,
1203 pcap_strerror(errno
));
1204 status
= PCAP_ERROR
;
1209 * Now bind to the device.
1211 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
1212 sizeof(ifr
.ifr_name
));
1213 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
1214 status
= check_setif_failure(p
, errno
);
1219 * No buffer size was explicitly specified.
1221 * Try finding a good size for the buffer; 32768 may
1222 * be too big, so keep cutting it in half until we
1223 * find a size that works, or run out of sizes to try.
1224 * If the default is larger, don't make it smaller.
1226 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) || v
< 32768)
1228 for ( ; v
!= 0; v
>>= 1) {
1230 * Ignore the return value - this is because the
1231 * call fails on BPF systems that don't have
1232 * kernel malloc. And if the call fails, it's
1233 * no big deal, we just continue to use the
1234 * standard buffer size.
1236 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
1238 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
1239 sizeof(ifr
.ifr_name
));
1240 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
1241 break; /* that size worked; we're done */
1243 if (errno
!= ENOBUFS
) {
1244 status
= check_setif_failure(p
, errno
);
1250 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1251 "BIOCSBLEN: %s: No buffer size worked",
1253 status
= PCAP_ERROR
;
1258 /* Get the data link layer type. */
1259 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
1260 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
1261 pcap_strerror(errno
));
1262 status
= PCAP_ERROR
;
1268 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
1291 * We don't know what to map this to yet.
1293 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
1295 status
= PCAP_ERROR
;
1299 #if _BSDI_VERSION - 0 >= 199510
1300 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
1315 case 12: /*DLT_C_HDLC*/
1323 * We know the default link type -- now determine all the DLTs
1324 * this interface supports. If this fails with EINVAL, it's
1325 * not fatal; we just don't get to use the feature later.
1327 if (get_dlt_list(fd
, v
, &bdl
, p
->errbuf
) == -1) {
1328 status
= PCAP_ERROR
;
1331 p
->dlt_count
= bdl
.bfl_len
;
1332 p
->dlt_list
= bdl
.bfl_list
;
1336 * Monitor mode fun, continued.
1338 * For 10.5 and, we're assuming, later releases, as noted above,
1339 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
1340 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
1341 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
1344 * Therefore, if the user asked for monitor mode, we filter out
1345 * the DLT_EN10MB value, as you can't get that in monitor mode,
1346 * and, if the user didn't ask for monitor mode, we filter out
1347 * the 802.11 DLT_ values, because selecting those will turn
1348 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
1349 * radio DLT_ value is offered, we try to select that, otherwise
1350 * we try to select DLT_IEEE802_11.
1353 if (isdigit((unsigned)osinfo
.release
[0]) &&
1354 (osinfo
.release
[0] == '9' ||
1355 isdigit((unsigned)osinfo
.release
[1]))) {
1357 * 10.5 (Darwin 9.x), or later.
1359 new_dlt
= find_802_11(&bdl
);
1360 if (new_dlt
!= -1) {
1362 * We have at least one 802.11 DLT_ value,
1363 * so this is an 802.11 interface.
1364 * new_dlt is the best of the 802.11
1365 * DLT_ values in the list.
1369 * Our caller wants monitor mode.
1370 * Purge DLT_EN10MB from the list
1371 * of link-layer types, as selecting
1372 * it will keep monitor mode off.
1377 * If the new mode we want isn't
1378 * the default mode, attempt to
1379 * select the new mode.
1382 if (ioctl(p
->fd
, BIOCSDLT
,
1394 * Our caller doesn't want
1395 * monitor mode. Unless this
1396 * is being done by pcap_open_live(),
1397 * purge the 802.11 link-layer types
1398 * from the list, as selecting
1399 * one of them will turn monitor
1408 * The caller requested monitor
1409 * mode, but we have no 802.11
1410 * link-layer types, so they
1413 status
= PCAP_ERROR_RFMON_NOTSUP
;
1419 #elif defined(HAVE_BSD_IEEE80211)
1421 * *BSD with the new 802.11 ioctls.
1422 * Do we want monitor mode?
1426 * Try to put the interface into monitor mode.
1428 status
= monitor_mode(p
, 1);
1437 * We're in monitor mode.
1438 * Try to find the best 802.11 DLT_ value and, if we
1439 * succeed, try to switch to that mode if we're not
1440 * already in that mode.
1442 new_dlt
= find_802_11(&bdl
);
1443 if (new_dlt
!= -1) {
1445 * We have at least one 802.11 DLT_ value.
1446 * new_dlt is the best of the 802.11
1447 * DLT_ values in the list.
1449 * If the new mode we want isn't the default mode,
1450 * attempt to select the new mode.
1453 if (ioctl(p
->fd
, BIOCSDLT
, &new_dlt
) != -1) {
1455 * We succeeded; make this the
1463 #endif /* various platforms */
1464 #endif /* BIOCGDLTLIST */
1467 * If this is an Ethernet device, and we don't have a DLT_ list,
1468 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
1469 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
1470 * do, but there's not much we can do about that without finding
1471 * some other way of determining whether it's an Ethernet or 802.11
1474 if (v
== DLT_EN10MB
&& p
->dlt_count
== 0) {
1475 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
1477 * If that fails, just leave the list empty.
1479 if (p
->dlt_list
!= NULL
) {
1480 p
->dlt_list
[0] = DLT_EN10MB
;
1481 p
->dlt_list
[1] = DLT_DOCSIS
;
1487 p
->fddipad
= PCAP_FDDIPAD
;
1493 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1495 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
1496 * the link-layer source address isn't forcibly overwritten.
1497 * (Should we ignore errors? Should we do this only if
1498 * we're open for writing?)
1500 * XXX - I seem to remember some packet-sending bug in some
1501 * BSDs - check CVS log for "bpf.c"?
1503 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1504 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1505 "BIOCSHDRCMPLT: %s", pcap_strerror(errno
));
1506 status
= PCAP_ERROR
;
1511 if (p
->md
.timeout
!= 0) {
1513 * XXX - is this seconds/nanoseconds in AIX?
1514 * (Treating it as such doesn't fix the timeout
1515 * problem described below.)
1518 to
.tv_sec
= p
->md
.timeout
/ 1000;
1519 to
.tv_usec
= (p
->md
.timeout
* 1000) % 1000000;
1520 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
1521 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT: %s",
1522 pcap_strerror(errno
));
1523 status
= PCAP_ERROR
;
1529 #ifdef BIOCIMMEDIATE
1531 * Darren Reed notes that
1533 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
1534 * timeout appears to be ignored and it waits until the buffer
1535 * is filled before returning. The result of not having it
1536 * set is almost worse than useless if your BPF filter
1537 * is reducing things to only a few packets (i.e. one every
1540 * so we turn BIOCIMMEDIATE mode on if this is AIX.
1542 * We don't turn it on for other platforms, as that means we
1543 * get woken up for every packet, which may not be what we want;
1544 * in the Winter 1993 USENIX paper on BPF, they say:
1546 * Since a process might want to look at every packet on a
1547 * network and the time between packets can be only a few
1548 * microseconds, it is not possible to do a read system call
1549 * per packet and BPF must collect the data from several
1550 * packets and return it as a unit when the monitoring
1551 * application does a read.
1553 * which I infer is the reason for the timeout - it means we
1554 * wait that amount of time, in the hopes that more packets
1555 * will arrive and we'll get them all with one read.
1557 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
1558 * BSDs) causes the timeout to be ignored.
1560 * On the other hand, some platforms (e.g., Linux) don't support
1561 * timeouts, they just hand stuff to you as soon as it arrives;
1562 * if that doesn't cause a problem on those platforms, it may
1563 * be OK to have BIOCIMMEDIATE mode on BSD as well.
1565 * (Note, though, that applications may depend on the read
1566 * completing, even if no packets have arrived, when the timeout
1567 * expires, e.g. GUI applications that have to check for input
1568 * while waiting for packets to arrive; a non-zero timeout
1569 * prevents "select()" from working right on FreeBSD and
1570 * possibly other BSDs, as the timer doesn't start until a
1571 * "read()" is done, so the timer isn't in effect if the
1572 * application is blocked on a "select()", and the "select()"
1573 * doesn't get woken up for a BPF device until the buffer
1577 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
1578 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCIMMEDIATE: %s",
1579 pcap_strerror(errno
));
1580 status
= PCAP_ERROR
;
1583 #endif /* BIOCIMMEDIATE */
1586 if (p
->opt
.promisc
) {
1587 /* set promiscuous mode, just warn if it fails */
1588 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
1589 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
1590 pcap_strerror(errno
));
1591 status
= PCAP_WARNING_PROMISC_NOTSUP
;
1595 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
1596 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
1597 pcap_strerror(errno
));
1598 status
= PCAP_ERROR
;
1602 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
1603 if (p
->buffer
== NULL
) {
1604 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
1605 pcap_strerror(errno
));
1606 status
= PCAP_ERROR
;
1610 /* For some strange reason this seems to prevent the EFAULT
1611 * problems we have experienced from AIX BPF. */
1612 memset(p
->buffer
, 0x0, p
->bufsize
);
1616 * If there's no filter program installed, there's
1617 * no indication to the kernel of what the snapshot
1618 * length should be, so no snapshotting is done.
1620 * Therefore, when we open the device, we install
1621 * an "accept everything" filter with the specified
1624 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
1627 total_insn
.k
= p
->snapshot
;
1629 total_prog
.bf_len
= 1;
1630 total_prog
.bf_insns
= &total_insn
;
1631 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
1632 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
1633 pcap_strerror(errno
));
1634 status
= PCAP_ERROR
;
1639 * On most BPF platforms, either you can do a "select()" or
1640 * "poll()" on a BPF file descriptor and it works correctly,
1641 * or you can do it and it will return "readable" if the
1642 * hold buffer is full but not if the timeout expires *and*
1643 * a non-blocking read will, if the hold buffer is empty
1644 * but the store buffer isn't empty, rotate the buffers
1645 * and return what packets are available.
1647 * In the latter case, the fact that a non-blocking read
1648 * will give you the available packets means you can work
1649 * around the failure of "select()" and "poll()" to wake up
1650 * and return "readable" when the timeout expires by using
1651 * the timeout as the "select()" or "poll()" timeout, putting
1652 * the BPF descriptor into non-blocking mode, and read from
1653 * it regardless of whether "select()" reports it as readable
1656 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
1657 * won't wake up and return "readable" if the timer expires
1658 * and non-blocking reads return EWOULDBLOCK if the hold
1659 * buffer is empty, even if the store buffer is non-empty.
1661 * This means the workaround in question won't work.
1663 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
1664 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
1665 * here". On all other BPF platforms, we set it to the FD for
1666 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
1667 * read will, if the hold buffer is empty and the store buffer
1668 * isn't empty, rotate the buffers and return what packets are
1669 * there (and in sufficiently recent versions of OpenBSD
1670 * "select()" and "poll()" should work correctly).
1672 * XXX - what about AIX?
1674 p
->selectable_fd
= p
->fd
; /* assume select() works until we know otherwise */
1677 * We can check what OS this is.
1679 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0) {
1680 if (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
1681 strncmp(osinfo
.release
, "4.4-", 4) == 0)
1682 p
->selectable_fd
= -1;
1686 p
->read_op
= pcap_read_bpf
;
1687 p
->inject_op
= pcap_inject_bpf
;
1688 p
->setfilter_op
= pcap_setfilter_bpf
;
1689 p
->setdirection_op
= pcap_setdirection_bpf
;
1690 p
->set_datalink_op
= pcap_set_datalink_bpf
;
1691 p
->getnonblock_op
= pcap_getnonblock_fd
;
1692 p
->setnonblock_op
= pcap_setnonblock_fd
;
1693 p
->stats_op
= pcap_stats_bpf
;
1694 p
->cleanup_op
= pcap_cleanup_bpf
;
1698 pcap_cleanup_bpf(p
);
1703 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
1706 if (dag_platform_finddevs(alldevsp
, errbuf
) < 0)
1708 #endif /* HAVE_DAG_API */
1713 #ifdef HAVE_BSD_IEEE80211
1715 monitor_mode(pcap_t
*p
, int set
)
1718 struct ifmediareq req
;
1724 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
1726 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "can't open socket: %s",
1727 pcap_strerror(errno
));
1728 return (PCAP_ERROR
);
1731 memset(&req
, 0, sizeof req
);
1732 strncpy(req
.ifm_name
, p
->opt
.source
, sizeof req
.ifm_name
);
1735 * Find out how many media types we have.
1737 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1739 * Can't get the media types.
1741 if (errno
== EINVAL
) {
1743 * Interface doesn't support SIOC{G,S}IFMEDIA.
1746 return (PCAP_ERROR_RFMON_NOTSUP
);
1748 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCGIFMEDIA 1: %s",
1749 pcap_strerror(errno
));
1751 return (PCAP_ERROR
);
1753 if (req
.ifm_count
== 0) {
1758 return (PCAP_ERROR_RFMON_NOTSUP
);
1762 * Allocate a buffer to hold all the media types, and
1763 * get the media types.
1765 media_list
= malloc(req
.ifm_count
* sizeof(int));
1766 if (media_list
== NULL
) {
1767 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
1768 pcap_strerror(errno
));
1770 return (PCAP_ERROR
);
1772 req
.ifm_ulist
= media_list
;
1773 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1774 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCGIFMEDIA: %s",
1775 pcap_strerror(errno
));
1778 return (PCAP_ERROR
);
1782 * Look for an 802.11 "automatic" media type.
1783 * We assume that all 802.11 adapters have that media type,
1784 * and that it will carry the monitor mode supported flag.
1787 for (i
= 0; i
< req
.ifm_count
; i
++) {
1788 if (IFM_TYPE(media_list
[i
]) == IFM_IEEE80211
1789 && IFM_SUBTYPE(media_list
[i
]) == IFM_AUTO
) {
1790 /* OK, does it do monitor mode? */
1791 if (media_list
[i
] & IFM_IEEE80211_MONITOR
) {
1800 * This adapter doesn't support monitor mode.
1803 return (PCAP_ERROR_RFMON_NOTSUP
);
1808 * Don't just check whether we can enable monitor mode,
1809 * do so, if it's not already enabled.
1811 if ((req
.ifm_current
& IFM_IEEE80211_MONITOR
) == 0) {
1813 * Monitor mode isn't currently on, so turn it on,
1814 * and remember that we should turn it off when the
1819 * If we haven't already done so, arrange to have
1820 * "pcap_close_all()" called when we exit.
1822 if (!pcap_do_addexit(p
)) {
1824 * "atexit()" failed; don't put the interface
1825 * in monitor mode, just give up.
1827 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1830 return (PCAP_ERROR
);
1832 memset(&ifr
, 0, sizeof(ifr
));
1833 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
1834 sizeof(ifr
.ifr_name
));
1835 ifr
.ifr_media
= req
.ifm_current
| IFM_IEEE80211_MONITOR
;
1836 if (ioctl(sock
, SIOCSIFMEDIA
, &ifr
) == -1) {
1837 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1838 "SIOCSIFMEDIA: %s", pcap_strerror(errno
));
1840 return (PCAP_ERROR
);
1843 p
->md
.must_clear
|= MUST_CLEAR_RFMON
;
1846 * Add this to the list of pcaps to close when we exit.
1848 pcap_add_to_pcaps_to_close(p
);
1853 #endif /* HAVE_BSD_IEEE80211 */
1855 #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
1857 * Check whether we have any 802.11 link-layer types; return the best
1858 * of the 802.11 link-layer types if we find one, and return -1
1861 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
1862 * best 802.11 link-layer type; any of the other 802.11-plus-radio
1863 * headers are second-best; 802.11 with no radio information is
1867 find_802_11(struct bpf_dltlist
*bdlp
)
1873 * Scan the list of DLT_ values, looking for 802.11 values,
1874 * and, if we find any, choose the best of them.
1877 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
1878 switch (bdlp
->bfl_list
[i
]) {
1880 case DLT_IEEE802_11
:
1882 * 802.11, but no radio.
1884 * Offer this, and select it as the new mode
1885 * unless we've already found an 802.11
1886 * header with radio information.
1889 new_dlt
= bdlp
->bfl_list
[i
];
1892 case DLT_PRISM_HEADER
:
1893 case DLT_AIRONET_HEADER
:
1894 case DLT_IEEE802_11_RADIO_AVS
:
1896 * 802.11 with radio, but not radiotap.
1898 * Offer this, and select it as the new mode
1899 * unless we've already found the radiotap DLT_.
1901 if (new_dlt
!= DLT_IEEE802_11_RADIO
)
1902 new_dlt
= bdlp
->bfl_list
[i
];
1905 case DLT_IEEE802_11_RADIO
:
1907 * 802.11 with radiotap.
1909 * Offer this, and select it as the new mode.
1911 new_dlt
= bdlp
->bfl_list
[i
];
1924 #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
1926 #if defined(__APPLE__) && defined(BIOCGDLTLIST)
1928 * Remove DLT_EN10MB from the list of DLT_ values.
1931 remove_en(pcap_t
*p
)
1936 * Scan the list of DLT_ values and discard DLT_EN10MB.
1939 for (i
= 0; i
< p
->dlt_count
; i
++) {
1940 switch (p
->dlt_list
[i
]) {
1944 * Don't offer this one.
1950 * Just copy this mode over.
1956 * Copy this DLT_ value to its new position.
1958 p
->dlt_list
[j
] = p
->dlt_list
[i
];
1963 * Set the DLT_ count to the number of entries we copied.
1969 * Remove DLT_EN10MB from the list of DLT_ values, and look for the
1970 * best 802.11 link-layer type in that list and return it.
1971 * Radiotap is better than anything else; 802.11 with any other radio
1972 * header is better than 802.11 with no radio header.
1975 remove_802_11(pcap_t
*p
)
1980 * Scan the list of DLT_ values and discard 802.11 values.
1983 for (i
= 0; i
< p
->dlt_count
; i
++) {
1984 switch (p
->dlt_list
[i
]) {
1986 case DLT_IEEE802_11
:
1987 case DLT_PRISM_HEADER
:
1988 case DLT_AIRONET_HEADER
:
1989 case DLT_IEEE802_11_RADIO
:
1990 case DLT_IEEE802_11_RADIO_AVS
:
1992 * 802.11. Don't offer this one.
1998 * Just copy this mode over.
2004 * Copy this DLT_ value to its new position.
2006 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2011 * Set the DLT_ count to the number of entries we copied.
2015 #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
2018 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
2021 * Free any user-mode filter we might happen to have installed.
2023 pcap_freecode(&p
->fcode
);
2026 * Try to install the kernel filter.
2028 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) == 0) {
2032 p
->md
.use_bpf
= 1; /* filtering in the kernel */
2035 * Discard any previously-received packets, as they might
2036 * have passed whatever filter was formerly in effect, but
2037 * might not pass this filter (BIOCSETF discards packets
2038 * buffered in the kernel, so you can lose packets in any
2048 * If it failed with EINVAL, that's probably because the program
2049 * is invalid or too big. Validate it ourselves; if we like it
2050 * (we currently allow backward branches, to support protochain),
2051 * run it in userland. (There's no notion of "too big" for
2054 * Otherwise, just give up.
2055 * XXX - if the copy of the program into the kernel failed,
2056 * we will get EINVAL rather than, say, EFAULT on at least
2059 if (errno
!= EINVAL
) {
2060 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2061 pcap_strerror(errno
));
2066 * install_bpf_program() validates the program.
2068 * XXX - what if we already have a filter in the kernel?
2070 if (install_bpf_program(p
, fp
) < 0)
2072 p
->md
.use_bpf
= 0; /* filtering in userland */
2077 * Set direction flag: Which packets do we accept on a forwarding
2078 * single device? IN, OUT or both?
2081 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
2083 #if defined(BIOCSDIRECTION)
2086 direction
= (d
== PCAP_D_IN
) ? BPF_D_IN
:
2087 ((d
== PCAP_D_OUT
) ? BPF_D_OUT
: BPF_D_INOUT
);
2088 if (ioctl(p
->fd
, BIOCSDIRECTION
, &direction
) == -1) {
2089 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2090 "Cannot set direction to %s: %s",
2091 (d
== PCAP_D_IN
) ? "PCAP_D_IN" :
2092 ((d
== PCAP_D_OUT
) ? "PCAP_D_OUT" : "PCAP_D_INOUT"),
2097 #elif defined(BIOCSSEESENT)
2101 * We don't support PCAP_D_OUT.
2103 if (d
== PCAP_D_OUT
) {
2104 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2105 "Setting direction to PCAP_D_OUT is not supported on BPF");
2109 seesent
= (d
== PCAP_D_INOUT
);
2110 if (ioctl(p
->fd
, BIOCSSEESENT
, &seesent
) == -1) {
2111 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2112 "Cannot set direction to %s: %s",
2113 (d
== PCAP_D_INOUT
) ? "PCAP_D_INOUT" : "PCAP_D_IN",
2119 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2120 "This system doesn't support BIOCSSEESENT, so the direction can't be set");
2126 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
2129 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
2130 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2131 "Cannot set DLT %d: %s", dlt
, strerror(errno
));