Home | History | Annotate | Line # | Download | only in dist
pcap-bpf.c revision 1.8
      1  1.8  christos /*	$NetBSD: pcap-bpf.c,v 1.8 2018/09/03 15:26:43 christos Exp $	*/
      2  1.3  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1993, 1994, 1995, 1996, 1998
      5  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that: (1) source code distributions
      9  1.1  christos  * retain the above copyright notice and this paragraph in its entirety, (2)
     10  1.1  christos  * distributions including binary code include the above copyright notice and
     11  1.1  christos  * this paragraph in its entirety in the documentation or other materials
     12  1.1  christos  * provided with the distribution, and (3) all advertising materials mentioning
     13  1.1  christos  * features or use of this software display the following acknowledgement:
     14  1.1  christos  * ``This product includes software developed by the University of California,
     15  1.1  christos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     16  1.1  christos  * the University nor the names of its contributors may be used to endorse
     17  1.1  christos  * or promote products derived from this software without specific prior
     18  1.1  christos  * written permission.
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     20  1.1  christos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     21  1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     22  1.1  christos  */
     23  1.5  christos 
     24  1.5  christos #include <sys/cdefs.h>
     25  1.8  christos __RCSID("$NetBSD: pcap-bpf.c,v 1.8 2018/09/03 15:26:43 christos Exp $");
     26  1.1  christos 
     27  1.1  christos #ifdef HAVE_CONFIG_H
     28  1.8  christos #include <config.h>
     29  1.1  christos #endif
     30  1.1  christos 
     31  1.1  christos #include <sys/param.h>			/* optionally get BSD define */
     32  1.1  christos #include <sys/socket.h>
     33  1.3  christos #include <time.h>
     34  1.1  christos /*
     35  1.1  christos  * <net/bpf.h> defines ioctls, but doesn't include <sys/ioccom.h>.
     36  1.1  christos  *
     37  1.1  christos  * We include <sys/ioctl.h> as it might be necessary to declare ioctl();
     38  1.8  christos  * at least on *BSD and macOS, it also defines various SIOC ioctls -
     39  1.1  christos  * we could include <sys/sockio.h>, but if we're already including
     40  1.1  christos  * <sys/ioctl.h>, which includes <sys/sockio.h> on those platforms,
     41  1.1  christos  * there's not much point in doing so.
     42  1.1  christos  *
     43  1.1  christos  * If we have <sys/ioccom.h>, we include it as well, to handle systems
     44  1.1  christos  * such as Solaris which don't arrange to include <sys/ioccom.h> if you
     45  1.1  christos  * include <sys/ioctl.h>
     46  1.1  christos  */
     47  1.1  christos #include <sys/ioctl.h>
     48  1.1  christos #ifdef HAVE_SYS_IOCCOM_H
     49  1.1  christos #include <sys/ioccom.h>
     50  1.1  christos #endif
     51  1.1  christos #include <sys/utsname.h>
     52  1.2  christos #ifdef __NetBSD__
     53  1.2  christos #include <paths.h>
     54  1.2  christos #endif
     55  1.1  christos 
     56  1.7  christos #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
     57  1.7  christos /*
     58  1.7  christos  * Add support for capturing on FreeBSD usbusN interfaces.
     59  1.7  christos  */
     60  1.7  christos static const char usbus_prefix[] = "usbus";
     61  1.7  christos #define USBUS_PREFIX_LEN	(sizeof(usbus_prefix) - 1)
     62  1.7  christos #include <dirent.h>
     63  1.7  christos #endif
     64  1.7  christos 
     65  1.1  christos #include <net/if.h>
     66  1.1  christos 
     67  1.1  christos #ifdef _AIX
     68  1.1  christos 
     69  1.1  christos /*
     70  1.1  christos  * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
     71  1.1  christos  * native OS version, as we need "struct bpf_config" from it.
     72  1.1  christos  */
     73  1.1  christos #define PCAP_DONT_INCLUDE_PCAP_BPF_H
     74  1.1  christos 
     75  1.1  christos #include <sys/types.h>
     76  1.1  christos 
     77  1.1  christos /*
     78  1.1  christos  * Prevent bpf.h from redefining the DLT_ values to their
     79  1.1  christos  * IFT_ values, as we're going to return the standard libpcap
     80  1.1  christos  * values, not IBM's non-standard IFT_ values.
     81  1.1  christos  */
     82  1.1  christos #undef _AIX
     83  1.1  christos #include <net/bpf.h>
     84  1.1  christos #define _AIX
     85  1.1  christos 
     86  1.8  christos /*
     87  1.8  christos  * If both BIOCROTZBUF and BPF_BUFMODE_ZBUF are defined, we have
     88  1.8  christos  * zero-copy BPF.
     89  1.8  christos  */
     90  1.8  christos #if defined(BIOCROTZBUF) && defined(BPF_BUFMODE_ZBUF)
     91  1.8  christos   #define HAVE_ZEROCOPY_BPF
     92  1.8  christos   #include <sys/mman.h>
     93  1.8  christos   #include <machine/atomic.h>
     94  1.8  christos #endif
     95  1.8  christos 
     96  1.1  christos #include <net/if_types.h>		/* for IFT_ values */
     97  1.1  christos #include <sys/sysconfig.h>
     98  1.1  christos #include <sys/device.h>
     99  1.1  christos #include <sys/cfgodm.h>
    100  1.1  christos #include <cf.h>
    101  1.1  christos 
    102  1.1  christos #ifdef __64BIT__
    103  1.1  christos #define domakedev makedev64
    104  1.1  christos #define getmajor major64
    105  1.1  christos #define bpf_hdr bpf_hdr32
    106  1.1  christos #else /* __64BIT__ */
    107  1.1  christos #define domakedev makedev
    108  1.1  christos #define getmajor major
    109  1.1  christos #endif /* __64BIT__ */
    110  1.1  christos 
    111  1.1  christos #define BPF_NAME "bpf"
    112  1.1  christos #define BPF_MINORS 4
    113  1.1  christos #define DRIVER_PATH "/usr/lib/drivers"
    114  1.1  christos #define BPF_NODE "/dev/bpf"
    115  1.1  christos static int bpfloadedflag = 0;
    116  1.1  christos static int odmlockid = 0;
    117  1.1  christos 
    118  1.1  christos static int bpf_load(char *errbuf);
    119  1.1  christos 
    120  1.1  christos #else /* _AIX */
    121  1.1  christos 
    122  1.1  christos #include <net/bpf.h>
    123  1.1  christos 
    124  1.1  christos #endif /* _AIX */
    125  1.1  christos 
    126  1.1  christos #include <ctype.h>
    127  1.1  christos #include <fcntl.h>
    128  1.1  christos #include <errno.h>
    129  1.1  christos #include <netdb.h>
    130  1.1  christos #include <stdio.h>
    131  1.1  christos #include <stdlib.h>
    132  1.1  christos #include <string.h>
    133  1.1  christos #include <unistd.h>
    134  1.1  christos 
    135  1.8  christos #ifdef SIOCGIFMEDIA
    136  1.1  christos # include <net/if_media.h>
    137  1.1  christos #endif
    138  1.1  christos 
    139  1.1  christos #include "pcap-int.h"
    140  1.1  christos 
    141  1.1  christos #ifdef HAVE_OS_PROTO_H
    142  1.1  christos #include "os-proto.h"
    143  1.1  christos #endif
    144  1.1  christos 
    145  1.4  christos /*
    146  1.4  christos  * Later versions of NetBSD stick padding in front of FDDI frames
    147  1.4  christos  * to align the IP header on a 4-byte boundary.
    148  1.4  christos  */
    149  1.4  christos #if defined(__NetBSD__) && __NetBSD_Version__ > 106000000
    150  1.4  christos #define       PCAP_FDDIPAD 3
    151  1.4  christos #endif
    152  1.4  christos 
    153  1.4  christos /*
    154  1.4  christos  * Private data for capturing on BPF devices.
    155  1.4  christos  */
    156  1.4  christos struct pcap_bpf {
    157  1.4  christos #ifdef HAVE_ZEROCOPY_BPF
    158  1.4  christos 	/*
    159  1.4  christos 	 * Zero-copy read buffer -- for zero-copy BPF.  'buffer' above will
    160  1.4  christos 	 * alternative between these two actual mmap'd buffers as required.
    161  1.4  christos 	 * As there is a header on the front size of the mmap'd buffer, only
    162  1.4  christos 	 * some of the buffer is exposed to libpcap as a whole via bufsize;
    163  1.4  christos 	 * zbufsize is the true size.  zbuffer tracks the current zbuf
    164  1.4  christos 	 * assocated with buffer so that it can be used to decide which the
    165  1.4  christos 	 * next buffer to read will be.
    166  1.4  christos 	 */
    167  1.4  christos 	u_char *zbuf1, *zbuf2, *zbuffer;
    168  1.4  christos 	u_int zbufsize;
    169  1.4  christos 	u_int zerocopy;
    170  1.4  christos 	u_int interrupted;
    171  1.4  christos 	struct timespec firstsel;
    172  1.4  christos 	/*
    173  1.4  christos 	 * If there's currently a buffer being actively processed, then it is
    174  1.4  christos 	 * referenced here; 'buffer' is also pointed at it, but offset by the
    175  1.4  christos 	 * size of the header.
    176  1.4  christos 	 */
    177  1.4  christos 	struct bpf_zbuf_header *bzh;
    178  1.4  christos 	int nonblock;		/* true if in nonblocking mode */
    179  1.4  christos #endif /* HAVE_ZEROCOPY_BPF */
    180  1.4  christos 
    181  1.4  christos 	char *device;		/* device name */
    182  1.4  christos 	int filtering_in_kernel; /* using kernel filter */
    183  1.4  christos 	int must_do_on_close;	/* stuff we must do when we close */
    184  1.4  christos };
    185  1.4  christos 
    186  1.4  christos /*
    187  1.4  christos  * Stuff to do when we close.
    188  1.4  christos  */
    189  1.4  christos #define MUST_CLEAR_RFMON	0x00000001	/* clear rfmon (monitor) mode */
    190  1.7  christos #define MUST_DESTROY_USBUS	0x00000002	/* destroy usbusN interface */
    191  1.4  christos 
    192  1.1  christos #ifdef BIOCGDLTLIST
    193  1.1  christos # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
    194  1.1  christos #define HAVE_BSD_IEEE80211
    195  1.7  christos 
    196  1.7  christos /*
    197  1.7  christos  * The ifm_ulist member of a struct ifmediareq is an int * on most systems,
    198  1.7  christos  * but it's a uint64_t on newer versions of OpenBSD.
    199  1.7  christos  *
    200  1.7  christos  * We check this by checking whether IFM_GMASK is defined and > 2^32-1.
    201  1.7  christos  */
    202  1.7  christos #  if defined(IFM_GMASK) && IFM_GMASK > 0xFFFFFFFF
    203  1.7  christos #    define IFM_ULIST_TYPE	uint64_t
    204  1.7  christos #  else
    205  1.7  christos #    define IFM_ULIST_TYPE	int
    206  1.7  christos #  endif
    207  1.1  christos # endif
    208  1.1  christos 
    209  1.1  christos # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
    210  1.1  christos static int find_802_11(struct bpf_dltlist *);
    211  1.1  christos 
    212  1.1  christos #  ifdef HAVE_BSD_IEEE80211
    213  1.1  christos static int monitor_mode(pcap_t *, int);
    214  1.1  christos #  endif
    215  1.1  christos 
    216  1.1  christos #  if defined(__APPLE__)
    217  1.1  christos static void remove_en(pcap_t *);
    218  1.1  christos static void remove_802_11(pcap_t *);
    219  1.1  christos #  endif
    220  1.1  christos 
    221  1.1  christos # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
    222  1.1  christos 
    223  1.1  christos #endif /* BIOCGDLTLIST */
    224  1.1  christos 
    225  1.3  christos #if defined(sun) && defined(LIFNAMSIZ) && defined(lifr_zoneid)
    226  1.3  christos #include <zone.h>
    227  1.3  christos #endif
    228  1.3  christos 
    229  1.1  christos /*
    230  1.1  christos  * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
    231  1.1  christos  * don't get DLT_DOCSIS defined.
    232  1.1  christos  */
    233  1.1  christos #ifndef DLT_DOCSIS
    234  1.1  christos #define DLT_DOCSIS	143
    235  1.1  christos #endif
    236  1.1  christos 
    237  1.1  christos /*
    238  1.8  christos  * In some versions of macOS, we might not even get any of the
    239  1.8  christos  * 802.11-plus-radio-header DLT_'s defined, even though some
    240  1.8  christos  * of them are used by various Airport drivers in those versions.
    241  1.1  christos  */
    242  1.1  christos #ifndef DLT_PRISM_HEADER
    243  1.1  christos #define DLT_PRISM_HEADER	119
    244  1.1  christos #endif
    245  1.1  christos #ifndef DLT_AIRONET_HEADER
    246  1.1  christos #define DLT_AIRONET_HEADER	120
    247  1.1  christos #endif
    248  1.1  christos #ifndef DLT_IEEE802_11_RADIO
    249  1.1  christos #define DLT_IEEE802_11_RADIO	127
    250  1.1  christos #endif
    251  1.1  christos #ifndef DLT_IEEE802_11_RADIO_AVS
    252  1.1  christos #define DLT_IEEE802_11_RADIO_AVS 163
    253  1.1  christos #endif
    254  1.1  christos 
    255  1.1  christos static int pcap_can_set_rfmon_bpf(pcap_t *p);
    256  1.1  christos static int pcap_activate_bpf(pcap_t *p);
    257  1.1  christos static int pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp);
    258  1.1  christos static int pcap_setdirection_bpf(pcap_t *, pcap_direction_t);
    259  1.1  christos static int pcap_set_datalink_bpf(pcap_t *p, int dlt);
    260  1.1  christos 
    261  1.1  christos /*
    262  1.3  christos  * For zerocopy bpf, the setnonblock/getnonblock routines need to modify
    263  1.4  christos  * pb->nonblock so we don't call select(2) if the pcap handle is in non-
    264  1.4  christos  * blocking mode.
    265  1.1  christos  */
    266  1.1  christos static int
    267  1.8  christos pcap_getnonblock_bpf(pcap_t *p)
    268  1.6  christos {
    269  1.3  christos #ifdef HAVE_ZEROCOPY_BPF
    270  1.4  christos 	struct pcap_bpf *pb = p->priv;
    271  1.4  christos 
    272  1.4  christos 	if (pb->zerocopy)
    273  1.4  christos 		return (pb->nonblock);
    274  1.3  christos #endif
    275  1.8  christos 	return (pcap_getnonblock_fd(p));
    276  1.1  christos }
    277  1.1  christos 
    278  1.1  christos static int
    279  1.8  christos pcap_setnonblock_bpf(pcap_t *p, int nonblock)
    280  1.6  christos {
    281  1.3  christos #ifdef HAVE_ZEROCOPY_BPF
    282  1.4  christos 	struct pcap_bpf *pb = p->priv;
    283  1.4  christos 
    284  1.4  christos 	if (pb->zerocopy) {
    285  1.4  christos 		pb->nonblock = nonblock;
    286  1.3  christos 		return (0);
    287  1.1  christos 	}
    288  1.3  christos #endif
    289  1.8  christos 	return (pcap_setnonblock_fd(p, nonblock));
    290  1.1  christos }
    291  1.1  christos 
    292  1.3  christos #ifdef HAVE_ZEROCOPY_BPF
    293  1.1  christos /*
    294  1.1  christos  * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
    295  1.1  christos  * shared memory buffers.
    296  1.1  christos  *
    297  1.1  christos  * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer,
    298  1.1  christos  * and set up p->buffer and cc to reflect one if available.  Notice that if
    299  1.1  christos  * there was no prior buffer, we select zbuf1 as this will be the first
    300  1.1  christos  * buffer filled for a fresh BPF session.
    301  1.1  christos  */
    302  1.1  christos static int
    303  1.1  christos pcap_next_zbuf_shm(pcap_t *p, int *cc)
    304  1.1  christos {
    305  1.4  christos 	struct pcap_bpf *pb = p->priv;
    306  1.1  christos 	struct bpf_zbuf_header *bzh;
    307  1.1  christos 
    308  1.4  christos 	if (pb->zbuffer == pb->zbuf2 || pb->zbuffer == NULL) {
    309  1.4  christos 		bzh = (struct bpf_zbuf_header *)pb->zbuf1;
    310  1.1  christos 		if (bzh->bzh_user_gen !=
    311  1.1  christos 		    atomic_load_acq_int(&bzh->bzh_kernel_gen)) {
    312  1.4  christos 			pb->bzh = bzh;
    313  1.4  christos 			pb->zbuffer = (u_char *)pb->zbuf1;
    314  1.4  christos 			p->buffer = pb->zbuffer + sizeof(*bzh);
    315  1.1  christos 			*cc = bzh->bzh_kernel_len;
    316  1.1  christos 			return (1);
    317  1.1  christos 		}
    318  1.4  christos 	} else if (pb->zbuffer == pb->zbuf1) {
    319  1.4  christos 		bzh = (struct bpf_zbuf_header *)pb->zbuf2;
    320  1.1  christos 		if (bzh->bzh_user_gen !=
    321  1.1  christos 		    atomic_load_acq_int(&bzh->bzh_kernel_gen)) {
    322  1.4  christos 			pb->bzh = bzh;
    323  1.4  christos 			pb->zbuffer = (u_char *)pb->zbuf2;
    324  1.4  christos   			p->buffer = pb->zbuffer + sizeof(*bzh);
    325  1.1  christos 			*cc = bzh->bzh_kernel_len;
    326  1.1  christos 			return (1);
    327  1.1  christos 		}
    328  1.1  christos 	}
    329  1.1  christos 	*cc = 0;
    330  1.1  christos 	return (0);
    331  1.1  christos }
    332  1.1  christos 
    333  1.1  christos /*
    334  1.1  christos  * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
    335  1.1  christos  * select() for data or a timeout, and possibly force rotation of the buffer
    336  1.1  christos  * in the event we time out or are in immediate mode.  Invoke the shared
    337  1.1  christos  * memory check before doing system calls in order to avoid doing avoidable
    338  1.1  christos  * work.
    339  1.1  christos  */
    340  1.1  christos static int
    341  1.1  christos pcap_next_zbuf(pcap_t *p, int *cc)
    342  1.1  christos {
    343  1.4  christos 	struct pcap_bpf *pb = p->priv;
    344  1.1  christos 	struct bpf_zbuf bz;
    345  1.1  christos 	struct timeval tv;
    346  1.1  christos 	struct timespec cur;
    347  1.1  christos 	fd_set r_set;
    348  1.1  christos 	int data, r;
    349  1.1  christos 	int expire, tmout;
    350  1.1  christos 
    351  1.1  christos #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000))
    352  1.1  christos 	/*
    353  1.1  christos 	 * Start out by seeing whether anything is waiting by checking the
    354  1.1  christos 	 * next shared memory buffer for data.
    355  1.1  christos 	 */
    356  1.1  christos 	data = pcap_next_zbuf_shm(p, cc);
    357  1.1  christos 	if (data)
    358  1.1  christos 		return (data);
    359  1.1  christos 	/*
    360  1.1  christos 	 * If a previous sleep was interrupted due to signal delivery, make
    361  1.1  christos 	 * sure that the timeout gets adjusted accordingly.  This requires
    362  1.1  christos 	 * that we analyze when the timeout should be been expired, and
    363  1.1  christos 	 * subtract the current time from that.  If after this operation,
    364  1.1  christos 	 * our timeout is less then or equal to zero, handle it like a
    365  1.1  christos 	 * regular timeout.
    366  1.1  christos 	 */
    367  1.4  christos 	tmout = p->opt.timeout;
    368  1.1  christos 	if (tmout)
    369  1.1  christos 		(void) clock_gettime(CLOCK_MONOTONIC, &cur);
    370  1.4  christos 	if (pb->interrupted && p->opt.timeout) {
    371  1.4  christos 		expire = TSTOMILLI(&pb->firstsel) + p->opt.timeout;
    372  1.1  christos 		tmout = expire - TSTOMILLI(&cur);
    373  1.1  christos #undef TSTOMILLI
    374  1.1  christos 		if (tmout <= 0) {
    375  1.4  christos 			pb->interrupted = 0;
    376  1.1  christos 			data = pcap_next_zbuf_shm(p, cc);
    377  1.1  christos 			if (data)
    378  1.1  christos 				return (data);
    379  1.1  christos 			if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) {
    380  1.8  christos 				pcap_fmt_errmsg_for_errno(p->errbuf,
    381  1.8  christos 				    PCAP_ERRBUF_SIZE, errno, "BIOCROTZBUF");
    382  1.1  christos 				return (PCAP_ERROR);
    383  1.1  christos 			}
    384  1.1  christos 			return (pcap_next_zbuf_shm(p, cc));
    385  1.1  christos 		}
    386  1.1  christos 	}
    387  1.1  christos 	/*
    388  1.1  christos 	 * No data in the buffer, so must use select() to wait for data or
    389  1.1  christos 	 * the next timeout.  Note that we only call select if the handle
    390  1.1  christos 	 * is in blocking mode.
    391  1.1  christos 	 */
    392  1.4  christos 	if (!pb->nonblock) {
    393  1.1  christos 		FD_ZERO(&r_set);
    394  1.1  christos 		FD_SET(p->fd, &r_set);
    395  1.1  christos 		if (tmout != 0) {
    396  1.1  christos 			tv.tv_sec = tmout / 1000;
    397  1.1  christos 			tv.tv_usec = (tmout * 1000) % 1000000;
    398  1.1  christos 		}
    399  1.1  christos 		r = select(p->fd + 1, &r_set, NULL, NULL,
    400  1.4  christos 		    p->opt.timeout != 0 ? &tv : NULL);
    401  1.1  christos 		if (r < 0 && errno == EINTR) {
    402  1.4  christos 			if (!pb->interrupted && p->opt.timeout) {
    403  1.4  christos 				pb->interrupted = 1;
    404  1.4  christos 				pb->firstsel = cur;
    405  1.1  christos 			}
    406  1.1  christos 			return (0);
    407  1.1  christos 		} else if (r < 0) {
    408  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
    409  1.8  christos 			    errno, "select");
    410  1.1  christos 			return (PCAP_ERROR);
    411  1.1  christos 		}
    412  1.1  christos 	}
    413  1.4  christos 	pb->interrupted = 0;
    414  1.1  christos 	/*
    415  1.1  christos 	 * Check again for data, which may exist now that we've either been
    416  1.1  christos 	 * woken up as a result of data or timed out.  Try the "there's data"
    417  1.1  christos 	 * case first since it doesn't require a system call.
    418  1.1  christos 	 */
    419  1.1  christos 	data = pcap_next_zbuf_shm(p, cc);
    420  1.1  christos 	if (data)
    421  1.1  christos 		return (data);
    422  1.1  christos 	/*
    423  1.1  christos 	 * Try forcing a buffer rotation to dislodge timed out or immediate
    424  1.1  christos 	 * data.
    425  1.1  christos 	 */
    426  1.1  christos 	if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) {
    427  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
    428  1.8  christos 		    errno, "BIOCROTZBUF");
    429  1.1  christos 		return (PCAP_ERROR);
    430  1.1  christos 	}
    431  1.1  christos 	return (pcap_next_zbuf_shm(p, cc));
    432  1.1  christos }
    433  1.1  christos 
    434  1.1  christos /*
    435  1.1  christos  * Notify kernel that we are done with the buffer.  We don't reset zbuffer so
    436  1.1  christos  * that we know which buffer to use next time around.
    437  1.1  christos  */
    438  1.1  christos static int
    439  1.1  christos pcap_ack_zbuf(pcap_t *p)
    440  1.1  christos {
    441  1.4  christos 	struct pcap_bpf *pb = p->priv;
    442  1.1  christos 
    443  1.4  christos 	atomic_store_rel_int(&pb->bzh->bzh_user_gen,
    444  1.4  christos 	    pb->bzh->bzh_kernel_gen);
    445  1.4  christos 	pb->bzh = NULL;
    446  1.1  christos 	p->buffer = NULL;
    447  1.1  christos 	return (0);
    448  1.1  christos }
    449  1.3  christos #endif /* HAVE_ZEROCOPY_BPF */
    450  1.1  christos 
    451  1.1  christos pcap_t *
    452  1.7  christos pcap_create_interface(const char *device _U_, char *ebuf)
    453  1.1  christos {
    454  1.1  christos 	pcap_t *p;
    455  1.1  christos 
    456  1.7  christos 	p = pcap_create_common(ebuf, sizeof (struct pcap_bpf));
    457  1.1  christos 	if (p == NULL)
    458  1.1  christos 		return (NULL);
    459  1.1  christos 
    460  1.1  christos 	p->activate_op = pcap_activate_bpf;
    461  1.1  christos 	p->can_set_rfmon_op = pcap_can_set_rfmon_bpf;
    462  1.7  christos #ifdef BIOCSTSTAMP
    463  1.7  christos 	/*
    464  1.7  christos 	 * We claim that we support microsecond and nanosecond time
    465  1.7  christos 	 * stamps.
    466  1.7  christos 	 */
    467  1.7  christos 	p->tstamp_precision_count = 2;
    468  1.7  christos 	p->tstamp_precision_list = malloc(2 * sizeof(u_int));
    469  1.7  christos 	if (p->tstamp_precision_list == NULL) {
    470  1.8  christos 		pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno,
    471  1.8  christos 		    "malloc");
    472  1.7  christos 		free(p);
    473  1.7  christos 		return (NULL);
    474  1.7  christos 	}
    475  1.7  christos 	p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
    476  1.7  christos 	p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
    477  1.7  christos #endif /* BIOCSTSTAMP */
    478  1.1  christos 	return (p);
    479  1.1  christos }
    480  1.1  christos 
    481  1.3  christos /*
    482  1.3  christos  * On success, returns a file descriptor for a BPF device.
    483  1.3  christos  * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf.
    484  1.3  christos  */
    485  1.1  christos static int
    486  1.7  christos bpf_open(char *errbuf)
    487  1.1  christos {
    488  1.8  christos 	int fd = -1;
    489  1.8  christos 	static const char cloning_device[] = "/dev/bpf";
    490  1.1  christos 	int n = 0;
    491  1.1  christos 	char device[sizeof "/dev/bpf0000000000"];
    492  1.8  christos 	static int no_cloning_bpf = 0;
    493  1.1  christos 
    494  1.1  christos #ifdef _AIX
    495  1.1  christos 	/*
    496  1.1  christos 	 * Load the bpf driver, if it isn't already loaded,
    497  1.1  christos 	 * and create the BPF device entries, if they don't
    498  1.1  christos 	 * already exist.
    499  1.1  christos 	 */
    500  1.7  christos 	if (bpf_load(errbuf) == PCAP_ERROR)
    501  1.1  christos 		return (PCAP_ERROR);
    502  1.1  christos #endif
    503  1.1  christos 
    504  1.8  christos 	/*
    505  1.8  christos 	 * First, unless we've already tried opening /dev/bpf and
    506  1.8  christos 	 * gotten ENOENT, try opening /dev/bpf.
    507  1.8  christos 	 * If it fails with ENOENT, remember that, so we don't try
    508  1.8  christos 	 * again, and try /dev/bpfN.
    509  1.8  christos 	 */
    510  1.8  christos 	if (!no_cloning_bpf &&
    511  1.8  christos 	    (fd = open(cloning_device, O_RDWR)) == -1 &&
    512  1.8  christos 	    ((errno != EACCES && errno != ENOENT) ||
    513  1.8  christos 	     (fd = open(cloning_device, O_RDONLY)) == -1)) {
    514  1.8  christos 		if (errno != ENOENT) {
    515  1.8  christos 			if (errno == EACCES)
    516  1.8  christos 				fd = PCAP_ERROR_PERM_DENIED;
    517  1.8  christos 			else
    518  1.8  christos 				fd = PCAP_ERROR;
    519  1.8  christos 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
    520  1.8  christos 			    errno, "(cannot open device) %s", cloning_device);
    521  1.8  christos 			return (fd);
    522  1.8  christos 		}
    523  1.8  christos 		no_cloning_bpf = 1;
    524  1.1  christos 	}
    525  1.8  christos 
    526  1.8  christos 	if (no_cloning_bpf) {
    527  1.1  christos 		/*
    528  1.8  christos 		 * We don't have /dev/bpf.
    529  1.8  christos 		 * Go through all the /dev/bpfN minors and find one
    530  1.8  christos 		 * that isn't in use.
    531  1.8  christos 		 */
    532  1.8  christos 		do {
    533  1.8  christos 			(void)pcap_snprintf(device, sizeof(device), "/dev/bpf%d", n++);
    534  1.8  christos 			/*
    535  1.8  christos 			 * Initially try a read/write open (to allow the inject
    536  1.8  christos 			 * method to work).  If that fails due to permission
    537  1.8  christos 			 * issues, fall back to read-only.  This allows a
    538  1.8  christos 			 * non-root user to be granted specific access to pcap
    539  1.8  christos 			 * capabilities via file permissions.
    540  1.8  christos 			 *
    541  1.8  christos 			 * XXX - we should have an API that has a flag that
    542  1.8  christos 			 * controls whether to open read-only or read-write,
    543  1.8  christos 			 * so that denial of permission to send (or inability
    544  1.8  christos 			 * to send, if sending packets isn't supported on
    545  1.8  christos 			 * the device in question) can be indicated at open
    546  1.8  christos 			 * time.
    547  1.8  christos 			 */
    548  1.8  christos 			fd = open(device, O_RDWR);
    549  1.8  christos 			if (fd == -1 && errno == EACCES)
    550  1.8  christos 				fd = open(device, O_RDONLY);
    551  1.8  christos 		} while (fd < 0 && errno == EBUSY);
    552  1.8  christos 	}
    553  1.1  christos 
    554  1.1  christos 	/*
    555  1.1  christos 	 * XXX better message for all minors used
    556  1.1  christos 	 */
    557  1.1  christos 	if (fd < 0) {
    558  1.3  christos 		switch (errno) {
    559  1.3  christos 
    560  1.3  christos 		case ENOENT:
    561  1.3  christos 			fd = PCAP_ERROR;
    562  1.3  christos 			if (n == 1) {
    563  1.3  christos 				/*
    564  1.3  christos 				 * /dev/bpf0 doesn't exist, which
    565  1.3  christos 				 * means we probably have no BPF
    566  1.3  christos 				 * devices.
    567  1.3  christos 				 */
    568  1.7  christos 				pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
    569  1.3  christos 				    "(there are no BPF devices)");
    570  1.3  christos 			} else {
    571  1.3  christos 				/*
    572  1.3  christos 				 * We got EBUSY on at least one
    573  1.3  christos 				 * BPF device, so we have BPF
    574  1.3  christos 				 * devices, but all the ones
    575  1.3  christos 				 * that exist are busy.
    576  1.3  christos 				 */
    577  1.7  christos 				pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
    578  1.3  christos 				    "(all BPF devices are busy)");
    579  1.3  christos 			}
    580  1.3  christos 			break;
    581  1.3  christos 
    582  1.3  christos 		case EACCES:
    583  1.3  christos 			/*
    584  1.3  christos 			 * Got EACCES on the last device we tried,
    585  1.3  christos 			 * and EBUSY on all devices before that,
    586  1.3  christos 			 * if any.
    587  1.3  christos 			 */
    588  1.1  christos 			fd = PCAP_ERROR_PERM_DENIED;
    589  1.8  christos 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
    590  1.8  christos 			    errno, "(cannot open BPF device) %s", device);
    591  1.3  christos 			break;
    592  1.3  christos 
    593  1.3  christos 		default:
    594  1.3  christos 			/*
    595  1.3  christos 			 * Some other problem.
    596  1.3  christos 			 */
    597  1.1  christos 			fd = PCAP_ERROR;
    598  1.8  christos 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
    599  1.8  christos 			    errno, "(cannot open BPF device) %s", device);
    600  1.3  christos 			break;
    601  1.3  christos 		}
    602  1.1  christos 	}
    603  1.1  christos 
    604  1.1  christos 	return (fd);
    605  1.1  christos }
    606  1.1  christos 
    607  1.7  christos /*
    608  1.7  christos  * Open and bind to a device; used if we're not actually going to use
    609  1.7  christos  * the device, but are just testing whether it can be opened, or opening
    610  1.7  christos  * it to get information about it.
    611  1.7  christos  *
    612  1.7  christos  * Returns an error code on failure (always negative), and an FD for
    613  1.7  christos  * the now-bound BPF device on success (always non-negative).
    614  1.7  christos  */
    615  1.7  christos static int
    616  1.7  christos bpf_open_and_bind(const char *name, char *errbuf)
    617  1.7  christos {
    618  1.7  christos 	int fd;
    619  1.7  christos 	struct ifreq ifr;
    620  1.7  christos 
    621  1.7  christos 	/*
    622  1.7  christos 	 * First, open a BPF device.
    623  1.7  christos 	 */
    624  1.7  christos 	fd = bpf_open(errbuf);
    625  1.7  christos 	if (fd < 0)
    626  1.7  christos 		return (fd);	/* fd is the appropriate error code */
    627  1.7  christos 
    628  1.7  christos 	/*
    629  1.7  christos 	 * Now bind to the device.
    630  1.7  christos 	 */
    631  1.7  christos 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    632  1.7  christos 	if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
    633  1.7  christos 		switch (errno) {
    634  1.7  christos 
    635  1.7  christos 		case ENXIO:
    636  1.7  christos 			/*
    637  1.7  christos 			 * There's no such device.
    638  1.7  christos 			 */
    639  1.7  christos 			close(fd);
    640  1.7  christos 			return (PCAP_ERROR_NO_SUCH_DEVICE);
    641  1.7  christos 
    642  1.7  christos 		case ENETDOWN:
    643  1.7  christos 			/*
    644  1.7  christos 			 * Return a "network down" indication, so that
    645  1.7  christos 			 * the application can report that rather than
    646  1.7  christos 			 * saying we had a mysterious failure and
    647  1.7  christos 			 * suggest that they report a problem to the
    648  1.7  christos 			 * libpcap developers.
    649  1.7  christos 			 */
    650  1.7  christos 			close(fd);
    651  1.7  christos 			return (PCAP_ERROR_IFACE_NOT_UP);
    652  1.7  christos 
    653  1.7  christos 		default:
    654  1.8  christos 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
    655  1.8  christos 			    errno, "BIOCSETIF: %s", name);
    656  1.7  christos 			close(fd);
    657  1.7  christos 			return (PCAP_ERROR);
    658  1.7  christos 		}
    659  1.7  christos 	}
    660  1.7  christos 
    661  1.7  christos 	/*
    662  1.7  christos 	 * Success.
    663  1.7  christos 	 */
    664  1.7  christos 	return (fd);
    665  1.7  christos }
    666  1.7  christos 
    667  1.1  christos #ifdef BIOCGDLTLIST
    668  1.1  christos static int
    669  1.1  christos get_dlt_list(int fd, int v, struct bpf_dltlist *bdlp, char *ebuf)
    670  1.1  christos {
    671  1.1  christos 	memset(bdlp, 0, sizeof(*bdlp));
    672  1.1  christos 	if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) == 0) {
    673  1.1  christos 		u_int i;
    674  1.1  christos 		int is_ethernet;
    675  1.1  christos 
    676  1.1  christos 		bdlp->bfl_list = (u_int *) malloc(sizeof(u_int) * (bdlp->bfl_len + 1));
    677  1.1  christos 		if (bdlp->bfl_list == NULL) {
    678  1.8  christos 			pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
    679  1.8  christos 			    errno, "malloc");
    680  1.1  christos 			return (PCAP_ERROR);
    681  1.1  christos 		}
    682  1.1  christos 
    683  1.1  christos 		if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) < 0) {
    684  1.8  christos 			pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
    685  1.8  christos 			    errno, "BIOCGDLTLIST");
    686  1.1  christos 			free(bdlp->bfl_list);
    687  1.1  christos 			return (PCAP_ERROR);
    688  1.1  christos 		}
    689  1.1  christos 
    690  1.1  christos 		/*
    691  1.1  christos 		 * OK, for real Ethernet devices, add DLT_DOCSIS to the
    692  1.1  christos 		 * list, so that an application can let you choose it,
    693  1.1  christos 		 * in case you're capturing DOCSIS traffic that a Cisco
    694  1.1  christos 		 * Cable Modem Termination System is putting out onto
    695  1.1  christos 		 * an Ethernet (it doesn't put an Ethernet header onto
    696  1.1  christos 		 * the wire, it puts raw DOCSIS frames out on the wire
    697  1.1  christos 		 * inside the low-level Ethernet framing).
    698  1.1  christos 		 *
    699  1.1  christos 		 * A "real Ethernet device" is defined here as a device
    700  1.1  christos 		 * that has a link-layer type of DLT_EN10MB and that has
    701  1.1  christos 		 * no alternate link-layer types; that's done to exclude
    702  1.1  christos 		 * 802.11 interfaces (which might or might not be the
    703  1.1  christos 		 * right thing to do, but I suspect it is - Ethernet <->
    704  1.1  christos 		 * 802.11 bridges would probably badly mishandle frames
    705  1.1  christos 		 * that don't have Ethernet headers).
    706  1.1  christos 		 *
    707  1.1  christos 		 * On Solaris with BPF, Ethernet devices also offer
    708  1.1  christos 		 * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
    709  1.1  christos 		 * treat it as an indication that the device isn't an
    710  1.1  christos 		 * Ethernet.
    711  1.1  christos 		 */
    712  1.1  christos 		if (v == DLT_EN10MB) {
    713  1.1  christos 			is_ethernet = 1;
    714  1.1  christos 			for (i = 0; i < bdlp->bfl_len; i++) {
    715  1.1  christos 				if (bdlp->bfl_list[i] != DLT_EN10MB
    716  1.1  christos #ifdef DLT_IPNET
    717  1.1  christos 				    && bdlp->bfl_list[i] != DLT_IPNET
    718  1.1  christos #endif
    719  1.1  christos 				    ) {
    720  1.1  christos 					is_ethernet = 0;
    721  1.1  christos 					break;
    722  1.1  christos 				}
    723  1.1  christos 			}
    724  1.1  christos 			if (is_ethernet) {
    725  1.1  christos 				/*
    726  1.1  christos 				 * We reserved one more slot at the end of
    727  1.1  christos 				 * the list.
    728  1.1  christos 				 */
    729  1.1  christos 				bdlp->bfl_list[bdlp->bfl_len] = DLT_DOCSIS;
    730  1.1  christos 				bdlp->bfl_len++;
    731  1.1  christos 			}
    732  1.1  christos 		}
    733  1.1  christos 	} else {
    734  1.1  christos 		/*
    735  1.1  christos 		 * EINVAL just means "we don't support this ioctl on
    736  1.1  christos 		 * this device"; don't treat it as an error.
    737  1.1  christos 		 */
    738  1.1  christos 		if (errno != EINVAL) {
    739  1.8  christos 			pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
    740  1.8  christos 			    errno, "BIOCGDLTLIST");
    741  1.1  christos 			return (PCAP_ERROR);
    742  1.1  christos 		}
    743  1.1  christos 	}
    744  1.1  christos 	return (0);
    745  1.1  christos }
    746  1.1  christos #endif
    747  1.1  christos 
    748  1.1  christos static int
    749  1.1  christos pcap_can_set_rfmon_bpf(pcap_t *p)
    750  1.1  christos {
    751  1.1  christos #if defined(__APPLE__)
    752  1.1  christos 	struct utsname osinfo;
    753  1.1  christos 	struct ifreq ifr;
    754  1.1  christos 	int fd;
    755  1.1  christos #ifdef BIOCGDLTLIST
    756  1.1  christos 	struct bpf_dltlist bdl;
    757  1.1  christos #endif
    758  1.1  christos 
    759  1.1  christos 	/*
    760  1.8  christos 	 * The joys of monitor mode on Mac OS X/OS X/macOS.
    761  1.1  christos 	 *
    762  1.1  christos 	 * Prior to 10.4, it's not supported at all.
    763  1.1  christos 	 *
    764  1.1  christos 	 * In 10.4, if adapter enN supports monitor mode, there's a
    765  1.1  christos 	 * wltN adapter corresponding to it; you open it, instead of
    766  1.1  christos 	 * enN, to get monitor mode.  You get whatever link-layer
    767  1.1  christos 	 * headers it supplies.
    768  1.1  christos 	 *
    769  1.1  christos 	 * In 10.5, and, we assume, later releases, if adapter enN
    770  1.1  christos 	 * supports monitor mode, it offers, among its selectable
    771  1.1  christos 	 * DLT_ values, values that let you get the 802.11 header;
    772  1.1  christos 	 * selecting one of those values puts the adapter into monitor
    773  1.1  christos 	 * mode (i.e., you can't get 802.11 headers except in monitor
    774  1.1  christos 	 * mode, and you can't get Ethernet headers in monitor mode).
    775  1.1  christos 	 */
    776  1.1  christos 	if (uname(&osinfo) == -1) {
    777  1.1  christos 		/*
    778  1.1  christos 		 * Can't get the OS version; just say "no".
    779  1.1  christos 		 */
    780  1.1  christos 		return (0);
    781  1.1  christos 	}
    782  1.1  christos 	/*
    783  1.1  christos 	 * We assume osinfo.sysname is "Darwin", because
    784  1.1  christos 	 * __APPLE__ is defined.  We just check the version.
    785  1.1  christos 	 */
    786  1.1  christos 	if (osinfo.release[0] < '8' && osinfo.release[1] == '.') {
    787  1.1  christos 		/*
    788  1.1  christos 		 * 10.3 (Darwin 7.x) or earlier.
    789  1.1  christos 		 * Monitor mode not supported.
    790  1.1  christos 		 */
    791  1.1  christos 		return (0);
    792  1.1  christos 	}
    793  1.1  christos 	if (osinfo.release[0] == '8' && osinfo.release[1] == '.') {
    794  1.1  christos 		/*
    795  1.1  christos 		 * 10.4 (Darwin 8.x).  s/en/wlt/, and check
    796  1.1  christos 		 * whether the device exists.
    797  1.1  christos 		 */
    798  1.7  christos 		if (strncmp(p->opt.device, "en", 2) != 0) {
    799  1.1  christos 			/*
    800  1.1  christos 			 * Not an enN device; no monitor mode.
    801  1.1  christos 			 */
    802  1.1  christos 			return (0);
    803  1.1  christos 		}
    804  1.1  christos 		fd = socket(AF_INET, SOCK_DGRAM, 0);
    805  1.1  christos 		if (fd == -1) {
    806  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
    807  1.8  christos 			    errno, "socket");
    808  1.1  christos 			return (PCAP_ERROR);
    809  1.1  christos 		}
    810  1.1  christos 		strlcpy(ifr.ifr_name, "wlt", sizeof(ifr.ifr_name));
    811  1.7  christos 		strlcat(ifr.ifr_name, p->opt.device + 2, sizeof(ifr.ifr_name));
    812  1.1  christos 		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
    813  1.1  christos 			/*
    814  1.1  christos 			 * No such device?
    815  1.1  christos 			 */
    816  1.1  christos 			close(fd);
    817  1.1  christos 			return (0);
    818  1.1  christos 		}
    819  1.1  christos 		close(fd);
    820  1.1  christos 		return (1);
    821  1.1  christos 	}
    822  1.1  christos 
    823  1.1  christos #ifdef BIOCGDLTLIST
    824  1.1  christos 	/*
    825  1.1  christos 	 * Everything else is 10.5 or later; for those,
    826  1.1  christos 	 * we just open the enN device, and check whether
    827  1.1  christos 	 * we have any 802.11 devices.
    828  1.1  christos 	 *
    829  1.1  christos 	 * First, open a BPF device.
    830  1.1  christos 	 */
    831  1.7  christos 	fd = bpf_open(p->errbuf);
    832  1.1  christos 	if (fd < 0)
    833  1.3  christos 		return (fd);	/* fd is the appropriate error code */
    834  1.1  christos 
    835  1.1  christos 	/*
    836  1.1  christos 	 * Now bind to the device.
    837  1.1  christos 	 */
    838  1.7  christos 	(void)strncpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name));
    839  1.1  christos 	if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
    840  1.3  christos 		switch (errno) {
    841  1.3  christos 
    842  1.3  christos 		case ENXIO:
    843  1.3  christos 			/*
    844  1.3  christos 			 * There's no such device.
    845  1.3  christos 			 */
    846  1.3  christos 			close(fd);
    847  1.3  christos 			return (PCAP_ERROR_NO_SUCH_DEVICE);
    848  1.3  christos 
    849  1.3  christos 		case ENETDOWN:
    850  1.1  christos 			/*
    851  1.1  christos 			 * Return a "network down" indication, so that
    852  1.1  christos 			 * the application can report that rather than
    853  1.1  christos 			 * saying we had a mysterious failure and
    854  1.1  christos 			 * suggest that they report a problem to the
    855  1.1  christos 			 * libpcap developers.
    856  1.1  christos 			 */
    857  1.1  christos 			close(fd);
    858  1.1  christos 			return (PCAP_ERROR_IFACE_NOT_UP);
    859  1.3  christos 
    860  1.3  christos 		default:
    861  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
    862  1.8  christos 			    errno, "BIOCSETIF: %s", p->opt.device);
    863  1.1  christos 			close(fd);
    864  1.1  christos 			return (PCAP_ERROR);
    865  1.1  christos 		}
    866  1.1  christos 	}
    867  1.1  christos 
    868  1.1  christos 	/*
    869  1.1  christos 	 * We know the default link type -- now determine all the DLTs
    870  1.1  christos 	 * this interface supports.  If this fails with EINVAL, it's
    871  1.1  christos 	 * not fatal; we just don't get to use the feature later.
    872  1.1  christos 	 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
    873  1.1  christos 	 * as the default DLT for this adapter.)
    874  1.1  christos 	 */
    875  1.1  christos 	if (get_dlt_list(fd, DLT_NULL, &bdl, p->errbuf) == PCAP_ERROR) {
    876  1.1  christos 		close(fd);
    877  1.1  christos 		return (PCAP_ERROR);
    878  1.1  christos 	}
    879  1.1  christos 	if (find_802_11(&bdl) != -1) {
    880  1.1  christos 		/*
    881  1.1  christos 		 * We have an 802.11 DLT, so we can set monitor mode.
    882  1.1  christos 		 */
    883  1.1  christos 		free(bdl.bfl_list);
    884  1.1  christos 		close(fd);
    885  1.1  christos 		return (1);
    886  1.1  christos 	}
    887  1.1  christos 	free(bdl.bfl_list);
    888  1.7  christos 	close(fd);
    889  1.1  christos #endif /* BIOCGDLTLIST */
    890  1.1  christos 	return (0);
    891  1.1  christos #elif defined(HAVE_BSD_IEEE80211)
    892  1.1  christos 	int ret;
    893  1.1  christos 
    894  1.1  christos 	ret = monitor_mode(p, 0);
    895  1.1  christos 	if (ret == PCAP_ERROR_RFMON_NOTSUP)
    896  1.1  christos 		return (0);	/* not an error, just a "can't do" */
    897  1.1  christos 	if (ret == 0)
    898  1.1  christos 		return (1);	/* success */
    899  1.1  christos 	return (ret);
    900  1.1  christos #else
    901  1.1  christos 	return (0);
    902  1.1  christos #endif
    903  1.1  christos }
    904  1.1  christos 
    905  1.1  christos static int
    906  1.1  christos pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps)
    907  1.1  christos {
    908  1.1  christos 	struct bpf_stat s;
    909  1.1  christos 
    910  1.1  christos 	/*
    911  1.1  christos 	 * "ps_recv" counts packets handed to the filter, not packets
    912  1.1  christos 	 * that passed the filter.  This includes packets later dropped
    913  1.1  christos 	 * because we ran out of buffer space.
    914  1.1  christos 	 *
    915  1.1  christos 	 * "ps_drop" counts packets dropped inside the BPF device
    916  1.1  christos 	 * because we ran out of buffer space.  It doesn't count
    917  1.1  christos 	 * packets dropped by the interface driver.  It counts
    918  1.1  christos 	 * only packets that passed the filter.
    919  1.1  christos 	 *
    920  1.1  christos 	 * Both statistics include packets not yet read from the kernel
    921  1.1  christos 	 * by libpcap, and thus not yet seen by the application.
    922  1.1  christos 	 */
    923  1.1  christos 	if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) {
    924  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
    925  1.8  christos 		    errno, "BIOCGSTATS");
    926  1.1  christos 		return (PCAP_ERROR);
    927  1.1  christos 	}
    928  1.1  christos 
    929  1.1  christos 	ps->ps_recv = s.bs_recv;
    930  1.1  christos 	ps->ps_drop = s.bs_drop;
    931  1.1  christos 	ps->ps_ifdrop = 0;
    932  1.1  christos 	return (0);
    933  1.1  christos }
    934  1.1  christos 
    935  1.1  christos static int
    936  1.1  christos pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
    937  1.1  christos {
    938  1.4  christos 	struct pcap_bpf *pb = p->priv;
    939  1.1  christos 	int cc;
    940  1.1  christos 	int n = 0;
    941  1.1  christos 	register u_char *bp, *ep;
    942  1.1  christos 	u_char *datap;
    943  1.1  christos #ifdef PCAP_FDDIPAD
    944  1.2  christos 	register u_int pad;
    945  1.1  christos #endif
    946  1.1  christos #ifdef HAVE_ZEROCOPY_BPF
    947  1.1  christos 	int i;
    948  1.1  christos #endif
    949  1.1  christos 
    950  1.1  christos  again:
    951  1.1  christos 	/*
    952  1.1  christos 	 * Has "pcap_breakloop()" been called?
    953  1.1  christos 	 */
    954  1.1  christos 	if (p->break_loop) {
    955  1.1  christos 		/*
    956  1.1  christos 		 * Yes - clear the flag that indicates that it
    957  1.1  christos 		 * has, and return PCAP_ERROR_BREAK to indicate
    958  1.1  christos 		 * that we were told to break out of the loop.
    959  1.1  christos 		 */
    960  1.1  christos 		p->break_loop = 0;
    961  1.1  christos 		return (PCAP_ERROR_BREAK);
    962  1.1  christos 	}
    963  1.1  christos 	cc = p->cc;
    964  1.1  christos 	if (p->cc == 0) {
    965  1.1  christos 		/*
    966  1.1  christos 		 * When reading without zero-copy from a file descriptor, we
    967  1.1  christos 		 * use a single buffer and return a length of data in the
    968  1.1  christos 		 * buffer.  With zero-copy, we update the p->buffer pointer
    969  1.1  christos 		 * to point at whatever underlying buffer contains the next
    970  1.1  christos 		 * data and update cc to reflect the data found in the
    971  1.1  christos 		 * buffer.
    972  1.1  christos 		 */
    973  1.1  christos #ifdef HAVE_ZEROCOPY_BPF
    974  1.4  christos 		if (pb->zerocopy) {
    975  1.1  christos 			if (p->buffer != NULL)
    976  1.1  christos 				pcap_ack_zbuf(p);
    977  1.1  christos 			i = pcap_next_zbuf(p, &cc);
    978  1.1  christos 			if (i == 0)
    979  1.1  christos 				goto again;
    980  1.1  christos 			if (i < 0)
    981  1.1  christos 				return (PCAP_ERROR);
    982  1.1  christos 		} else
    983  1.1  christos #endif
    984  1.1  christos 		{
    985  1.7  christos 			cc = read(p->fd, p->buffer, p->bufsize);
    986  1.1  christos 		}
    987  1.1  christos 		if (cc < 0) {
    988  1.1  christos 			/* Don't choke when we get ptraced */
    989  1.1  christos 			switch (errno) {
    990  1.1  christos 
    991  1.1  christos 			case EINTR:
    992  1.1  christos 				goto again;
    993  1.1  christos 
    994  1.1  christos #ifdef _AIX
    995  1.1  christos 			case EFAULT:
    996  1.1  christos 				/*
    997  1.1  christos 				 * Sigh.  More AIX wonderfulness.
    998  1.1  christos 				 *
    999  1.1  christos 				 * For some unknown reason the uiomove()
   1000  1.1  christos 				 * operation in the bpf kernel extension
   1001  1.1  christos 				 * used to copy the buffer into user
   1002  1.1  christos 				 * space sometimes returns EFAULT. I have
   1003  1.1  christos 				 * no idea why this is the case given that
   1004  1.1  christos 				 * a kernel debugger shows the user buffer
   1005  1.1  christos 				 * is correct. This problem appears to
   1006  1.1  christos 				 * be mostly mitigated by the memset of
   1007  1.1  christos 				 * the buffer before it is first used.
   1008  1.1  christos 				 * Very strange.... Shaun Clowes
   1009  1.1  christos 				 *
   1010  1.1  christos 				 * In any case this means that we shouldn't
   1011  1.1  christos 				 * treat EFAULT as a fatal error; as we
   1012  1.1  christos 				 * don't have an API for returning
   1013  1.1  christos 				 * a "some packets were dropped since
   1014  1.1  christos 				 * the last packet you saw" indication,
   1015  1.1  christos 				 * we just ignore EFAULT and keep reading.
   1016  1.1  christos 				 */
   1017  1.1  christos 				goto again;
   1018  1.1  christos #endif
   1019  1.1  christos 
   1020  1.1  christos 			case EWOULDBLOCK:
   1021  1.1  christos 				return (0);
   1022  1.1  christos 
   1023  1.1  christos 			case ENXIO:
   1024  1.1  christos 				/*
   1025  1.1  christos 				 * The device on which we're capturing
   1026  1.1  christos 				 * went away.
   1027  1.1  christos 				 *
   1028  1.1  christos 				 * XXX - we should really return
   1029  1.1  christos 				 * PCAP_ERROR_IFACE_NOT_UP, but
   1030  1.1  christos 				 * pcap_dispatch() etc. aren't
   1031  1.1  christos 				 * defined to retur that.
   1032  1.1  christos 				 */
   1033  1.7  christos 				pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
   1034  1.1  christos 				    "The interface went down");
   1035  1.1  christos 				return (PCAP_ERROR);
   1036  1.1  christos 
   1037  1.1  christos #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
   1038  1.1  christos 			/*
   1039  1.1  christos 			 * Due to a SunOS bug, after 2^31 bytes, the kernel
   1040  1.1  christos 			 * file offset overflows and read fails with EINVAL.
   1041  1.1  christos 			 * The lseek() to 0 will fix things.
   1042  1.1  christos 			 */
   1043  1.1  christos 			case EINVAL:
   1044  1.1  christos 				if (lseek(p->fd, 0L, SEEK_CUR) +
   1045  1.1  christos 				    p->bufsize < 0) {
   1046  1.1  christos 					(void)lseek(p->fd, 0L, SEEK_SET);
   1047  1.1  christos 					goto again;
   1048  1.1  christos 				}
   1049  1.1  christos 				/* fall through */
   1050  1.1  christos #endif
   1051  1.1  christos 			}
   1052  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1053  1.8  christos 			    errno, "read");
   1054  1.1  christos 			return (PCAP_ERROR);
   1055  1.1  christos 		}
   1056  1.7  christos 		bp = (u_char *)p->buffer;
   1057  1.1  christos 	} else
   1058  1.1  christos 		bp = p->bp;
   1059  1.1  christos 
   1060  1.1  christos 	/*
   1061  1.1  christos 	 * Loop through each packet.
   1062  1.1  christos 	 */
   1063  1.7  christos #ifdef BIOCSTSTAMP
   1064  1.7  christos #define bhp ((struct bpf_xhdr *)bp)
   1065  1.7  christos #else
   1066  1.1  christos #define bhp ((struct bpf_hdr *)bp)
   1067  1.7  christos #endif
   1068  1.1  christos 	ep = bp + cc;
   1069  1.1  christos #ifdef PCAP_FDDIPAD
   1070  1.1  christos 	pad = p->fddipad;
   1071  1.1  christos #endif
   1072  1.1  christos 	while (bp < ep) {
   1073  1.2  christos 		register u_int caplen, hdrlen;
   1074  1.1  christos 
   1075  1.1  christos 		/*
   1076  1.1  christos 		 * Has "pcap_breakloop()" been called?
   1077  1.1  christos 		 * If so, return immediately - if we haven't read any
   1078  1.1  christos 		 * packets, clear the flag and return PCAP_ERROR_BREAK
   1079  1.1  christos 		 * to indicate that we were told to break out of the loop,
   1080  1.1  christos 		 * otherwise leave the flag set, so that the *next* call
   1081  1.1  christos 		 * will break out of the loop without having read any
   1082  1.1  christos 		 * packets, and return the number of packets we've
   1083  1.1  christos 		 * processed so far.
   1084  1.1  christos 		 */
   1085  1.1  christos 		if (p->break_loop) {
   1086  1.3  christos 			p->bp = bp;
   1087  1.3  christos 			p->cc = ep - bp;
   1088  1.3  christos 			/*
   1089  1.3  christos 			 * ep is set based on the return value of read(),
   1090  1.3  christos 			 * but read() from a BPF device doesn't necessarily
   1091  1.3  christos 			 * return a value that's a multiple of the alignment
   1092  1.3  christos 			 * value for BPF_WORDALIGN().  However, whenever we
   1093  1.3  christos 			 * increment bp, we round up the increment value by
   1094  1.3  christos 			 * a value rounded up by BPF_WORDALIGN(), so we
   1095  1.3  christos 			 * could increment bp past ep after processing the
   1096  1.3  christos 			 * last packet in the buffer.
   1097  1.3  christos 			 *
   1098  1.3  christos 			 * We treat ep < bp as an indication that this
   1099  1.3  christos 			 * happened, and just set p->cc to 0.
   1100  1.3  christos 			 */
   1101  1.3  christos 			if (p->cc < 0)
   1102  1.3  christos 				p->cc = 0;
   1103  1.1  christos 			if (n == 0) {
   1104  1.1  christos 				p->break_loop = 0;
   1105  1.1  christos 				return (PCAP_ERROR_BREAK);
   1106  1.3  christos 			} else
   1107  1.1  christos 				return (n);
   1108  1.1  christos 		}
   1109  1.1  christos 
   1110  1.1  christos 		caplen = bhp->bh_caplen;
   1111  1.1  christos 		hdrlen = bhp->bh_hdrlen;
   1112  1.1  christos 		datap = bp + hdrlen;
   1113  1.1  christos 		/*
   1114  1.1  christos 		 * Short-circuit evaluation: if using BPF filter
   1115  1.1  christos 		 * in kernel, no need to do it now - we already know
   1116  1.1  christos 		 * the packet passed the filter.
   1117  1.1  christos 		 *
   1118  1.1  christos #ifdef PCAP_FDDIPAD
   1119  1.1  christos 		 * Note: the filter code was generated assuming
   1120  1.1  christos 		 * that p->fddipad was the amount of padding
   1121  1.1  christos 		 * before the header, as that's what's required
   1122  1.1  christos 		 * in the kernel, so we run the filter before
   1123  1.1  christos 		 * skipping that padding.
   1124  1.1  christos #endif
   1125  1.1  christos 		 */
   1126  1.4  christos 		if (pb->filtering_in_kernel ||
   1127  1.1  christos 		    bpf_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) {
   1128  1.1  christos 			struct pcap_pkthdr pkthdr;
   1129  1.7  christos #ifdef BIOCSTSTAMP
   1130  1.7  christos 			struct bintime bt;
   1131  1.1  christos 
   1132  1.7  christos 			bt.sec = bhp->bh_tstamp.bt_sec;
   1133  1.7  christos 			bt.frac = bhp->bh_tstamp.bt_frac;
   1134  1.7  christos 			if (p->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) {
   1135  1.7  christos 				struct timespec ts;
   1136  1.7  christos 
   1137  1.7  christos 				bintime2timespec(&bt, &ts);
   1138  1.7  christos 				pkthdr.ts.tv_sec = ts.tv_sec;
   1139  1.7  christos 				pkthdr.ts.tv_usec = ts.tv_nsec;
   1140  1.7  christos 			} else {
   1141  1.7  christos 				struct timeval tv;
   1142  1.7  christos 
   1143  1.7  christos 				bintime2timeval(&bt, &tv);
   1144  1.7  christos 				pkthdr.ts.tv_sec = tv.tv_sec;
   1145  1.7  christos 				pkthdr.ts.tv_usec = tv.tv_usec;
   1146  1.7  christos 			}
   1147  1.7  christos #else
   1148  1.1  christos 			pkthdr.ts.tv_sec = bhp->bh_tstamp.tv_sec;
   1149  1.1  christos #ifdef _AIX
   1150  1.1  christos 			/*
   1151  1.1  christos 			 * AIX's BPF returns seconds/nanoseconds time
   1152  1.1  christos 			 * stamps, not seconds/microseconds time stamps.
   1153  1.1  christos 			 */
   1154  1.1  christos 			pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec/1000;
   1155  1.1  christos #else
   1156  1.1  christos 			pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec;
   1157  1.1  christos #endif
   1158  1.7  christos #endif /* BIOCSTSTAMP */
   1159  1.1  christos #ifdef PCAP_FDDIPAD
   1160  1.1  christos 			if (caplen > pad)
   1161  1.1  christos 				pkthdr.caplen = caplen - pad;
   1162  1.1  christos 			else
   1163  1.1  christos 				pkthdr.caplen = 0;
   1164  1.1  christos 			if (bhp->bh_datalen > pad)
   1165  1.1  christos 				pkthdr.len = bhp->bh_datalen - pad;
   1166  1.1  christos 			else
   1167  1.1  christos 				pkthdr.len = 0;
   1168  1.1  christos 			datap += pad;
   1169  1.1  christos #else
   1170  1.1  christos 			pkthdr.caplen = caplen;
   1171  1.1  christos 			pkthdr.len = bhp->bh_datalen;
   1172  1.1  christos #endif
   1173  1.1  christos 			(*callback)(user, &pkthdr, datap);
   1174  1.1  christos 			bp += BPF_WORDALIGN(caplen + hdrlen);
   1175  1.5  christos 			if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
   1176  1.1  christos 				p->bp = bp;
   1177  1.1  christos 				p->cc = ep - bp;
   1178  1.3  christos 				/*
   1179  1.3  christos 				 * See comment above about p->cc < 0.
   1180  1.3  christos 				 */
   1181  1.3  christos 				if (p->cc < 0)
   1182  1.3  christos 					p->cc = 0;
   1183  1.1  christos 				return (n);
   1184  1.1  christos 			}
   1185  1.1  christos 		} else {
   1186  1.1  christos 			/*
   1187  1.1  christos 			 * Skip this packet.
   1188  1.1  christos 			 */
   1189  1.1  christos 			bp += BPF_WORDALIGN(caplen + hdrlen);
   1190  1.1  christos 		}
   1191  1.1  christos 	}
   1192  1.1  christos #undef bhp
   1193  1.1  christos 	p->cc = 0;
   1194  1.1  christos 	return (n);
   1195  1.1  christos }
   1196  1.1  christos 
   1197  1.1  christos static int
   1198  1.1  christos pcap_inject_bpf(pcap_t *p, const void *buf, size_t size)
   1199  1.1  christos {
   1200  1.1  christos 	int ret;
   1201  1.1  christos 
   1202  1.1  christos 	ret = write(p->fd, buf, size);
   1203  1.1  christos #ifdef __APPLE__
   1204  1.1  christos 	if (ret == -1 && errno == EAFNOSUPPORT) {
   1205  1.1  christos 		/*
   1206  1.8  christos 		 * In some versions of macOS, there's a bug wherein setting
   1207  1.8  christos 		 * the BIOCSHDRCMPLT flag causes writes to fail; see, for
   1208  1.8  christos 		 * example:
   1209  1.1  christos 		 *
   1210  1.1  christos 		 *	http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
   1211  1.1  christos 		 *
   1212  1.8  christos 		 * So, if, on macOS, we get EAFNOSUPPORT from the write, we
   1213  1.1  christos 		 * assume it's due to that bug, and turn off that flag
   1214  1.1  christos 		 * and try again.  If we succeed, it either means that
   1215  1.1  christos 		 * somebody applied the fix from that URL, or other patches
   1216  1.1  christos 		 * for that bug from
   1217  1.1  christos 		 *
   1218  1.1  christos 		 *	http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
   1219  1.1  christos 		 *
   1220  1.1  christos 		 * and are running a Darwin kernel with those fixes, or
   1221  1.8  christos 		 * that Apple fixed the problem in some macOS release.
   1222  1.1  christos 		 */
   1223  1.1  christos 		u_int spoof_eth_src = 0;
   1224  1.1  christos 
   1225  1.1  christos 		if (ioctl(p->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
   1226  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1227  1.8  christos 			    errno, "send: can't turn off BIOCSHDRCMPLT");
   1228  1.1  christos 			return (PCAP_ERROR);
   1229  1.1  christos 		}
   1230  1.1  christos 
   1231  1.1  christos 		/*
   1232  1.1  christos 		 * Now try the write again.
   1233  1.1  christos 		 */
   1234  1.1  christos 		ret = write(p->fd, buf, size);
   1235  1.1  christos 	}
   1236  1.1  christos #endif /* __APPLE__ */
   1237  1.1  christos 	if (ret == -1) {
   1238  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1239  1.8  christos 		    errno, "send");
   1240  1.1  christos 		return (PCAP_ERROR);
   1241  1.1  christos 	}
   1242  1.1  christos 	return (ret);
   1243  1.1  christos }
   1244  1.1  christos 
   1245  1.1  christos #ifdef _AIX
   1246  1.1  christos static int
   1247  1.1  christos bpf_odminit(char *errbuf)
   1248  1.1  christos {
   1249  1.1  christos 	char *errstr;
   1250  1.1  christos 
   1251  1.1  christos 	if (odm_initialize() == -1) {
   1252  1.1  christos 		if (odm_err_msg(odmerrno, &errstr) == -1)
   1253  1.1  christos 			errstr = "Unknown error";
   1254  1.7  christos 		pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
   1255  1.1  christos 		    "bpf_load: odm_initialize failed: %s",
   1256  1.1  christos 		    errstr);
   1257  1.1  christos 		return (PCAP_ERROR);
   1258  1.1  christos 	}
   1259  1.1  christos 
   1260  1.1  christos 	if ((odmlockid = odm_lock("/etc/objrepos/config_lock", ODM_WAIT)) == -1) {
   1261  1.1  christos 		if (odm_err_msg(odmerrno, &errstr) == -1)
   1262  1.1  christos 			errstr = "Unknown error";
   1263  1.7  christos 		pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
   1264  1.1  christos 		    "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
   1265  1.1  christos 		    errstr);
   1266  1.1  christos 		(void)odm_terminate();
   1267  1.1  christos 		return (PCAP_ERROR);
   1268  1.1  christos 	}
   1269  1.1  christos 
   1270  1.1  christos 	return (0);
   1271  1.1  christos }
   1272  1.1  christos 
   1273  1.1  christos static int
   1274  1.1  christos bpf_odmcleanup(char *errbuf)
   1275  1.1  christos {
   1276  1.1  christos 	char *errstr;
   1277  1.1  christos 
   1278  1.1  christos 	if (odm_unlock(odmlockid) == -1) {
   1279  1.1  christos 		if (errbuf != NULL) {
   1280  1.1  christos 			if (odm_err_msg(odmerrno, &errstr) == -1)
   1281  1.1  christos 				errstr = "Unknown error";
   1282  1.7  christos 			pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
   1283  1.1  christos 			    "bpf_load: odm_unlock failed: %s",
   1284  1.1  christos 			    errstr);
   1285  1.1  christos 		}
   1286  1.1  christos 		return (PCAP_ERROR);
   1287  1.1  christos 	}
   1288  1.1  christos 
   1289  1.1  christos 	if (odm_terminate() == -1) {
   1290  1.1  christos 		if (errbuf != NULL) {
   1291  1.1  christos 			if (odm_err_msg(odmerrno, &errstr) == -1)
   1292  1.1  christos 				errstr = "Unknown error";
   1293  1.7  christos 			pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
   1294  1.1  christos 			    "bpf_load: odm_terminate failed: %s",
   1295  1.1  christos 			    errstr);
   1296  1.1  christos 		}
   1297  1.1  christos 		return (PCAP_ERROR);
   1298  1.1  christos 	}
   1299  1.1  christos 
   1300  1.1  christos 	return (0);
   1301  1.1  christos }
   1302  1.1  christos 
   1303  1.1  christos static int
   1304  1.1  christos bpf_load(char *errbuf)
   1305  1.1  christos {
   1306  1.1  christos 	long major;
   1307  1.1  christos 	int *minors;
   1308  1.1  christos 	int numminors, i, rc;
   1309  1.1  christos 	char buf[1024];
   1310  1.1  christos 	struct stat sbuf;
   1311  1.1  christos 	struct bpf_config cfg_bpf;
   1312  1.1  christos 	struct cfg_load cfg_ld;
   1313  1.1  christos 	struct cfg_kmod cfg_km;
   1314  1.1  christos 
   1315  1.1  christos 	/*
   1316  1.1  christos 	 * This is very very close to what happens in the real implementation
   1317  1.1  christos 	 * but I've fixed some (unlikely) bug situations.
   1318  1.1  christos 	 */
   1319  1.1  christos 	if (bpfloadedflag)
   1320  1.1  christos 		return (0);
   1321  1.1  christos 
   1322  1.1  christos 	if (bpf_odminit(errbuf) == PCAP_ERROR)
   1323  1.1  christos 		return (PCAP_ERROR);
   1324  1.1  christos 
   1325  1.1  christos 	major = genmajor(BPF_NAME);
   1326  1.1  christos 	if (major == -1) {
   1327  1.8  christos 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
   1328  1.8  christos 		    errno, "bpf_load: genmajor failed");
   1329  1.1  christos 		(void)bpf_odmcleanup(NULL);
   1330  1.1  christos 		return (PCAP_ERROR);
   1331  1.1  christos 	}
   1332  1.1  christos 
   1333  1.1  christos 	minors = getminor(major, &numminors, BPF_NAME);
   1334  1.1  christos 	if (!minors) {
   1335  1.1  christos 		minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1);
   1336  1.1  christos 		if (!minors) {
   1337  1.8  christos 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
   1338  1.8  christos 			    errno, "bpf_load: genminor failed");
   1339  1.1  christos 			(void)bpf_odmcleanup(NULL);
   1340  1.1  christos 			return (PCAP_ERROR);
   1341  1.1  christos 		}
   1342  1.1  christos 	}
   1343  1.1  christos 
   1344  1.1  christos 	if (bpf_odmcleanup(errbuf) == PCAP_ERROR)
   1345  1.1  christos 		return (PCAP_ERROR);
   1346  1.1  christos 
   1347  1.1  christos 	rc = stat(BPF_NODE "0", &sbuf);
   1348  1.1  christos 	if (rc == -1 && errno != ENOENT) {
   1349  1.8  christos 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
   1350  1.8  christos 		    errno, "bpf_load: can't stat %s", BPF_NODE "0");
   1351  1.1  christos 		return (PCAP_ERROR);
   1352  1.1  christos 	}
   1353  1.1  christos 
   1354  1.1  christos 	if (rc == -1 || getmajor(sbuf.st_rdev) != major) {
   1355  1.1  christos 		for (i = 0; i < BPF_MINORS; i++) {
   1356  1.8  christos 			pcap_snprintf(buf, sizeof(buf), "%s%d", BPF_NODE, i);
   1357  1.1  christos 			unlink(buf);
   1358  1.1  christos 			if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) {
   1359  1.8  christos 				pcap_fmt_errmsg_for_errno(errbuf,
   1360  1.8  christos 				    PCAP_ERRBUF_SIZE, errno,
   1361  1.8  christos 				    "bpf_load: can't mknod %s", buf);
   1362  1.1  christos 				return (PCAP_ERROR);
   1363  1.1  christos 			}
   1364  1.1  christos 		}
   1365  1.1  christos 	}
   1366  1.1  christos 
   1367  1.1  christos 	/* Check if the driver is loaded */
   1368  1.1  christos 	memset(&cfg_ld, 0x0, sizeof(cfg_ld));
   1369  1.1  christos 	cfg_ld.path = buf;
   1370  1.8  christos 	pcap_snprintf(cfg_ld.path, sizeof(cfg_ld.path), "%s/%s", DRIVER_PATH, BPF_NAME);
   1371  1.1  christos 	if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) ||
   1372  1.1  christos 	    (cfg_ld.kmid == 0)) {
   1373  1.1  christos 		/* Driver isn't loaded, load it now */
   1374  1.1  christos 		if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) {
   1375  1.8  christos 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
   1376  1.8  christos 			    errno, "bpf_load: could not load driver");
   1377  1.1  christos 			return (PCAP_ERROR);
   1378  1.1  christos 		}
   1379  1.1  christos 	}
   1380  1.1  christos 
   1381  1.1  christos 	/* Configure the driver */
   1382  1.1  christos 	cfg_km.cmd = CFG_INIT;
   1383  1.1  christos 	cfg_km.kmid = cfg_ld.kmid;
   1384  1.1  christos 	cfg_km.mdilen = sizeof(cfg_bpf);
   1385  1.1  christos 	cfg_km.mdiptr = (void *)&cfg_bpf;
   1386  1.1  christos 	for (i = 0; i < BPF_MINORS; i++) {
   1387  1.1  christos 		cfg_bpf.devno = domakedev(major, i);
   1388  1.1  christos 		if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) {
   1389  1.8  christos 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
   1390  1.8  christos 			    errno, "bpf_load: could not configure driver");
   1391  1.1  christos 			return (PCAP_ERROR);
   1392  1.1  christos 		}
   1393  1.1  christos 	}
   1394  1.1  christos 
   1395  1.1  christos 	bpfloadedflag = 1;
   1396  1.1  christos 
   1397  1.1  christos 	return (0);
   1398  1.1  christos }
   1399  1.1  christos #endif
   1400  1.1  christos 
   1401  1.1  christos /*
   1402  1.7  christos  * Undo any operations done when opening the device when necessary.
   1403  1.1  christos  */
   1404  1.1  christos static void
   1405  1.1  christos pcap_cleanup_bpf(pcap_t *p)
   1406  1.1  christos {
   1407  1.4  christos 	struct pcap_bpf *pb = p->priv;
   1408  1.1  christos #ifdef HAVE_BSD_IEEE80211
   1409  1.1  christos 	int sock;
   1410  1.1  christos 	struct ifmediareq req;
   1411  1.1  christos 	struct ifreq ifr;
   1412  1.1  christos #endif
   1413  1.1  christos 
   1414  1.4  christos 	if (pb->must_do_on_close != 0) {
   1415  1.1  christos 		/*
   1416  1.1  christos 		 * There's something we have to do when closing this
   1417  1.1  christos 		 * pcap_t.
   1418  1.1  christos 		 */
   1419  1.1  christos #ifdef HAVE_BSD_IEEE80211
   1420  1.4  christos 		if (pb->must_do_on_close & MUST_CLEAR_RFMON) {
   1421  1.1  christos 			/*
   1422  1.1  christos 			 * We put the interface into rfmon mode;
   1423  1.1  christos 			 * take it out of rfmon mode.
   1424  1.1  christos 			 *
   1425  1.1  christos 			 * XXX - if somebody else wants it in rfmon
   1426  1.1  christos 			 * mode, this code cannot know that, so it'll take
   1427  1.1  christos 			 * it out of rfmon mode.
   1428  1.1  christos 			 */
   1429  1.1  christos 			sock = socket(AF_INET, SOCK_DGRAM, 0);
   1430  1.1  christos 			if (sock == -1) {
   1431  1.1  christos 				fprintf(stderr,
   1432  1.1  christos 				    "Can't restore interface flags (socket() failed: %s).\n"
   1433  1.1  christos 				    "Please adjust manually.\n",
   1434  1.1  christos 				    strerror(errno));
   1435  1.1  christos 			} else {
   1436  1.1  christos 				memset(&req, 0, sizeof(req));
   1437  1.4  christos 				strncpy(req.ifm_name, pb->device,
   1438  1.1  christos 				    sizeof(req.ifm_name));
   1439  1.1  christos 				if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
   1440  1.1  christos 					fprintf(stderr,
   1441  1.1  christos 					    "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
   1442  1.1  christos 					    "Please adjust manually.\n",
   1443  1.1  christos 					    strerror(errno));
   1444  1.1  christos 				} else {
   1445  1.1  christos 					if (req.ifm_current & IFM_IEEE80211_MONITOR) {
   1446  1.1  christos 						/*
   1447  1.1  christos 						 * Rfmon mode is currently on;
   1448  1.1  christos 						 * turn it off.
   1449  1.1  christos 						 */
   1450  1.1  christos 						memset(&ifr, 0, sizeof(ifr));
   1451  1.1  christos 						(void)strncpy(ifr.ifr_name,
   1452  1.4  christos 						    pb->device,
   1453  1.1  christos 						    sizeof(ifr.ifr_name));
   1454  1.1  christos 						ifr.ifr_media =
   1455  1.1  christos 						    req.ifm_current & ~IFM_IEEE80211_MONITOR;
   1456  1.1  christos 						if (ioctl(sock, SIOCSIFMEDIA,
   1457  1.1  christos 						    &ifr) == -1) {
   1458  1.1  christos 							fprintf(stderr,
   1459  1.1  christos 							    "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
   1460  1.1  christos 							    "Please adjust manually.\n",
   1461  1.1  christos 							    strerror(errno));
   1462  1.1  christos 						}
   1463  1.1  christos 					}
   1464  1.1  christos 				}
   1465  1.1  christos 				close(sock);
   1466  1.1  christos 			}
   1467  1.1  christos 		}
   1468  1.1  christos #endif /* HAVE_BSD_IEEE80211 */
   1469  1.1  christos 
   1470  1.7  christos #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
   1471  1.7  christos 		/*
   1472  1.7  christos 		 * Attempt to destroy the usbusN interface that we created.
   1473  1.7  christos 		 */
   1474  1.7  christos 		if (pb->must_do_on_close & MUST_DESTROY_USBUS) {
   1475  1.7  christos 			if (if_nametoindex(pb->device) > 0) {
   1476  1.7  christos 				int s;
   1477  1.7  christos 
   1478  1.7  christos 				s = socket(AF_LOCAL, SOCK_DGRAM, 0);
   1479  1.7  christos 				if (s >= 0) {
   1480  1.7  christos 					strlcpy(ifr.ifr_name, pb->device,
   1481  1.7  christos 					    sizeof(ifr.ifr_name));
   1482  1.7  christos 					ioctl(s, SIOCIFDESTROY, &ifr);
   1483  1.7  christos 					close(s);
   1484  1.7  christos 				}
   1485  1.7  christos 			}
   1486  1.7  christos 		}
   1487  1.7  christos #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
   1488  1.1  christos 		/*
   1489  1.1  christos 		 * Take this pcap out of the list of pcaps for which we
   1490  1.1  christos 		 * have to take the interface out of some mode.
   1491  1.1  christos 		 */
   1492  1.1  christos 		pcap_remove_from_pcaps_to_close(p);
   1493  1.4  christos 		pb->must_do_on_close = 0;
   1494  1.1  christos 	}
   1495  1.1  christos 
   1496  1.1  christos #ifdef HAVE_ZEROCOPY_BPF
   1497  1.4  christos 	if (pb->zerocopy) {
   1498  1.3  christos 		/*
   1499  1.3  christos 		 * Delete the mappings.  Note that p->buffer gets
   1500  1.3  christos 		 * initialized to one of the mmapped regions in
   1501  1.3  christos 		 * this case, so do not try and free it directly;
   1502  1.3  christos 		 * null it out so that pcap_cleanup_live_common()
   1503  1.3  christos 		 * doesn't try to free it.
   1504  1.3  christos 		 */
   1505  1.4  christos 		if (pb->zbuf1 != MAP_FAILED && pb->zbuf1 != NULL)
   1506  1.4  christos 			(void) munmap(pb->zbuf1, pb->zbufsize);
   1507  1.4  christos 		if (pb->zbuf2 != MAP_FAILED && pb->zbuf2 != NULL)
   1508  1.4  christos 			(void) munmap(pb->zbuf2, pb->zbufsize);
   1509  1.3  christos 		p->buffer = NULL;
   1510  1.1  christos 	}
   1511  1.1  christos #endif
   1512  1.4  christos 	if (pb->device != NULL) {
   1513  1.4  christos 		free(pb->device);
   1514  1.4  christos 		pb->device = NULL;
   1515  1.1  christos 	}
   1516  1.1  christos 	pcap_cleanup_live_common(p);
   1517  1.1  christos }
   1518  1.1  christos 
   1519  1.1  christos static int
   1520  1.1  christos check_setif_failure(pcap_t *p, int error)
   1521  1.1  christos {
   1522  1.1  christos #ifdef __APPLE__
   1523  1.1  christos 	int fd;
   1524  1.1  christos 	struct ifreq ifr;
   1525  1.1  christos 	int err;
   1526  1.1  christos #endif
   1527  1.1  christos 
   1528  1.1  christos 	if (error == ENXIO) {
   1529  1.1  christos 		/*
   1530  1.1  christos 		 * No such device exists.
   1531  1.1  christos 		 */
   1532  1.1  christos #ifdef __APPLE__
   1533  1.7  christos 		if (p->opt.rfmon && strncmp(p->opt.device, "wlt", 3) == 0) {
   1534  1.1  christos 			/*
   1535  1.1  christos 			 * Monitor mode was requested, and we're trying
   1536  1.1  christos 			 * to open a "wltN" device.  Assume that this
   1537  1.1  christos 			 * is 10.4 and that we were asked to open an
   1538  1.1  christos 			 * "enN" device; if that device exists, return
   1539  1.1  christos 			 * "monitor mode not supported on the device".
   1540  1.1  christos 			 */
   1541  1.1  christos 			fd = socket(AF_INET, SOCK_DGRAM, 0);
   1542  1.1  christos 			if (fd != -1) {
   1543  1.1  christos 				strlcpy(ifr.ifr_name, "en",
   1544  1.1  christos 				    sizeof(ifr.ifr_name));
   1545  1.7  christos 				strlcat(ifr.ifr_name, p->opt.device + 3,
   1546  1.1  christos 				    sizeof(ifr.ifr_name));
   1547  1.1  christos 				if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
   1548  1.1  christos 					/*
   1549  1.1  christos 					 * We assume this failed because
   1550  1.1  christos 					 * the underlying device doesn't
   1551  1.1  christos 					 * exist.
   1552  1.1  christos 					 */
   1553  1.1  christos 					err = PCAP_ERROR_NO_SUCH_DEVICE;
   1554  1.8  christos 					pcap_fmt_errmsg_for_errno(p->errbuf,
   1555  1.8  christos 					    PCAP_ERRBUF_SIZE, errno,
   1556  1.8  christos 					    "SIOCGIFFLAGS on %s failed",
   1557  1.8  christos 					    ifr.ifr_name);
   1558  1.1  christos 				} else {
   1559  1.1  christos 					/*
   1560  1.1  christos 					 * The underlying "enN" device
   1561  1.1  christos 					 * exists, but there's no
   1562  1.1  christos 					 * corresponding "wltN" device;
   1563  1.1  christos 					 * that means that the "enN"
   1564  1.1  christos 					 * device doesn't support
   1565  1.1  christos 					 * monitor mode, probably because
   1566  1.1  christos 					 * it's an Ethernet device rather
   1567  1.1  christos 					 * than a wireless device.
   1568  1.1  christos 					 */
   1569  1.1  christos 					err = PCAP_ERROR_RFMON_NOTSUP;
   1570  1.1  christos 				}
   1571  1.1  christos 				close(fd);
   1572  1.1  christos 			} else {
   1573  1.1  christos 				/*
   1574  1.1  christos 				 * We can't find out whether there's
   1575  1.1  christos 				 * an underlying "enN" device, so
   1576  1.1  christos 				 * just report "no such device".
   1577  1.1  christos 				 */
   1578  1.1  christos 				err = PCAP_ERROR_NO_SUCH_DEVICE;
   1579  1.8  christos 				pcap_fmt_errmsg_for_errno(p->errbuf,
   1580  1.8  christos 				    errno, PCAP_ERRBUF_SIZE,
   1581  1.8  christos 				    "socket() failed");
   1582  1.1  christos 			}
   1583  1.1  christos 			return (err);
   1584  1.1  christos 		}
   1585  1.1  christos #endif
   1586  1.1  christos 		/*
   1587  1.1  christos 		 * No such device.
   1588  1.1  christos 		 */
   1589  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1590  1.8  christos 		    errno, "BIOCSETIF failed");
   1591  1.1  christos 		return (PCAP_ERROR_NO_SUCH_DEVICE);
   1592  1.1  christos 	} else if (errno == ENETDOWN) {
   1593  1.1  christos 		/*
   1594  1.1  christos 		 * Return a "network down" indication, so that
   1595  1.1  christos 		 * the application can report that rather than
   1596  1.1  christos 		 * saying we had a mysterious failure and
   1597  1.1  christos 		 * suggest that they report a problem to the
   1598  1.1  christos 		 * libpcap developers.
   1599  1.1  christos 		 */
   1600  1.1  christos 		return (PCAP_ERROR_IFACE_NOT_UP);
   1601  1.1  christos 	} else {
   1602  1.1  christos 		/*
   1603  1.1  christos 		 * Some other error; fill in the error string, and
   1604  1.1  christos 		 * return PCAP_ERROR.
   1605  1.1  christos 		 */
   1606  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1607  1.8  christos 		    errno, "BIOCSETIF: %s", p->opt.device);
   1608  1.1  christos 		return (PCAP_ERROR);
   1609  1.1  christos 	}
   1610  1.1  christos }
   1611  1.1  christos 
   1612  1.1  christos /*
   1613  1.1  christos  * Default capture buffer size.
   1614  1.1  christos  * 32K isn't very much for modern machines with fast networks; we
   1615  1.1  christos  * pick .5M, as that's the maximum on at least some systems with BPF.
   1616  1.3  christos  *
   1617  1.3  christos  * However, on AIX 3.5, the larger buffer sized caused unrecoverable
   1618  1.3  christos  * read failures under stress, so we leave it as 32K; yet another
   1619  1.3  christos  * place where AIX's BPF is broken.
   1620  1.1  christos  */
   1621  1.3  christos #ifdef _AIX
   1622  1.3  christos #define DEFAULT_BUFSIZE	32768
   1623  1.3  christos #else
   1624  1.1  christos #define DEFAULT_BUFSIZE	524288
   1625  1.3  christos #endif
   1626  1.1  christos 
   1627  1.1  christos static int
   1628  1.1  christos pcap_activate_bpf(pcap_t *p)
   1629  1.1  christos {
   1630  1.4  christos 	struct pcap_bpf *pb = p->priv;
   1631  1.1  christos 	int status = 0;
   1632  1.5  christos #ifdef HAVE_BSD_IEEE80211
   1633  1.5  christos 	int retv;
   1634  1.5  christos #endif
   1635  1.1  christos 	int fd;
   1636  1.3  christos #ifdef LIFNAMSIZ
   1637  1.3  christos 	char *zonesep;
   1638  1.3  christos 	struct lifreq ifr;
   1639  1.3  christos 	char *ifrname = ifr.lifr_name;
   1640  1.3  christos 	const size_t ifnamsiz = sizeof(ifr.lifr_name);
   1641  1.3  christos #else
   1642  1.1  christos 	struct ifreq ifr;
   1643  1.3  christos 	char *ifrname = ifr.ifr_name;
   1644  1.3  christos 	const size_t ifnamsiz = sizeof(ifr.ifr_name);
   1645  1.3  christos #endif
   1646  1.1  christos 	struct bpf_version bv;
   1647  1.1  christos #ifdef __APPLE__
   1648  1.1  christos 	int sockfd;
   1649  1.1  christos 	char *wltdev = NULL;
   1650  1.1  christos #endif
   1651  1.1  christos #ifdef BIOCGDLTLIST
   1652  1.1  christos 	struct bpf_dltlist bdl;
   1653  1.1  christos #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
   1654  1.2  christos 	u_int new_dlt;
   1655  1.1  christos #endif
   1656  1.1  christos #endif /* BIOCGDLTLIST */
   1657  1.1  christos #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
   1658  1.1  christos 	u_int spoof_eth_src = 1;
   1659  1.1  christos #endif
   1660  1.1  christos 	u_int v;
   1661  1.1  christos 	struct bpf_insn total_insn;
   1662  1.1  christos 	struct bpf_program total_prog;
   1663  1.1  christos 	struct utsname osinfo;
   1664  1.1  christos 	int have_osinfo = 0;
   1665  1.1  christos #ifdef HAVE_ZEROCOPY_BPF
   1666  1.1  christos 	struct bpf_zbuf bz;
   1667  1.1  christos 	u_int bufmode, zbufmax;
   1668  1.1  christos #endif
   1669  1.1  christos 
   1670  1.7  christos 	fd = bpf_open(p->errbuf);
   1671  1.1  christos 	if (fd < 0) {
   1672  1.1  christos 		status = fd;
   1673  1.1  christos 		goto bad;
   1674  1.1  christos 	}
   1675  1.1  christos 
   1676  1.1  christos 	p->fd = fd;
   1677  1.1  christos 
   1678  1.1  christos 	if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) {
   1679  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1680  1.8  christos 		    errno, "BIOCVERSION");
   1681  1.1  christos 		status = PCAP_ERROR;
   1682  1.1  christos 		goto bad;
   1683  1.1  christos 	}
   1684  1.1  christos 	if (bv.bv_major != BPF_MAJOR_VERSION ||
   1685  1.1  christos 	    bv.bv_minor < BPF_MINOR_VERSION) {
   1686  1.7  christos 		pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
   1687  1.1  christos 		    "kernel bpf filter out of date");
   1688  1.1  christos 		status = PCAP_ERROR;
   1689  1.1  christos 		goto bad;
   1690  1.1  christos 	}
   1691  1.1  christos 
   1692  1.8  christos 	/*
   1693  1.8  christos 	 * Turn a negative snapshot value (invalid), a snapshot value of
   1694  1.8  christos 	 * 0 (unspecified), or a value bigger than the normal maximum
   1695  1.8  christos 	 * value, into the maximum allowed value.
   1696  1.8  christos 	 *
   1697  1.8  christos 	 * If some application really *needs* a bigger snapshot
   1698  1.8  christos 	 * length, we should just increase MAXIMUM_SNAPLEN.
   1699  1.8  christos 	 */
   1700  1.8  christos 	if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
   1701  1.8  christos 		p->snapshot = MAXIMUM_SNAPLEN;
   1702  1.8  christos 
   1703  1.3  christos #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid)
   1704  1.3  christos 	/*
   1705  1.6  christos 	 * Retrieve the zoneid of the zone we are currently executing in.
   1706  1.6  christos 	 */
   1707  1.6  christos 	if ((ifr.lifr_zoneid = getzoneid()) == -1) {
   1708  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1709  1.8  christos 		    errno, "getzoneid()");
   1710  1.6  christos 		status = PCAP_ERROR;
   1711  1.6  christos 		goto bad;
   1712  1.6  christos 	}
   1713  1.6  christos 	/*
   1714  1.6  christos 	 * Check if the given source datalink name has a '/' separated
   1715  1.6  christos 	 * zonename prefix string.  The zonename prefixed source datalink can
   1716  1.6  christos 	 * be used by pcap consumers in the Solaris global zone to capture
   1717  1.6  christos 	 * traffic on datalinks in non-global zones.  Non-global zones
   1718  1.6  christos 	 * do not have access to datalinks outside of their own namespace.
   1719  1.3  christos 	 */
   1720  1.7  christos 	if ((zonesep = strchr(p->opt.device, '/')) != NULL) {
   1721  1.6  christos 		char path_zname[ZONENAME_MAX];
   1722  1.3  christos 		int  znamelen;
   1723  1.3  christos 		char *lnamep;
   1724  1.3  christos 
   1725  1.6  christos 		if (ifr.lifr_zoneid != GLOBAL_ZONEID) {
   1726  1.7  christos 			pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
   1727  1.6  christos 			    "zonename/linkname only valid in global zone.");
   1728  1.6  christos 			status = PCAP_ERROR;
   1729  1.6  christos 			goto bad;
   1730  1.6  christos 		}
   1731  1.7  christos 		znamelen = zonesep - p->opt.device;
   1732  1.7  christos 		(void) strlcpy(path_zname, p->opt.device, znamelen + 1);
   1733  1.6  christos 		ifr.lifr_zoneid = getzoneidbyname(path_zname);
   1734  1.6  christos 		if (ifr.lifr_zoneid == -1) {
   1735  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1736  1.8  christos 			    errno, "getzoneidbyname(%s)", path_zname);
   1737  1.6  christos 			status = PCAP_ERROR;
   1738  1.6  christos 			goto bad;
   1739  1.6  christos 		}
   1740  1.3  christos 		lnamep = strdup(zonesep + 1);
   1741  1.7  christos 		if (lnamep == NULL) {
   1742  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1743  1.8  christos 			    errno, "strdup");
   1744  1.7  christos 			status = PCAP_ERROR;
   1745  1.7  christos 			goto bad;
   1746  1.7  christos 		}
   1747  1.7  christos 		free(p->opt.device);
   1748  1.7  christos 		p->opt.device = lnamep;
   1749  1.3  christos 	}
   1750  1.3  christos #endif
   1751  1.3  christos 
   1752  1.7  christos 	pb->device = strdup(p->opt.device);
   1753  1.4  christos 	if (pb->device == NULL) {
   1754  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1755  1.8  christos 		    errno, "strdup");
   1756  1.1  christos 		status = PCAP_ERROR;
   1757  1.1  christos 		goto bad;
   1758  1.1  christos 	}
   1759  1.1  christos 
   1760  1.1  christos 	/*
   1761  1.1  christos 	 * Attempt to find out the version of the OS on which we're running.
   1762  1.1  christos 	 */
   1763  1.1  christos 	if (uname(&osinfo) == 0)
   1764  1.1  christos 		have_osinfo = 1;
   1765  1.1  christos 
   1766  1.1  christos #ifdef __APPLE__
   1767  1.1  christos 	/*
   1768  1.1  christos 	 * See comment in pcap_can_set_rfmon_bpf() for an explanation
   1769  1.1  christos 	 * of why we check the version number.
   1770  1.1  christos 	 */
   1771  1.1  christos 	if (p->opt.rfmon) {
   1772  1.1  christos 		if (have_osinfo) {
   1773  1.1  christos 			/*
   1774  1.1  christos 			 * We assume osinfo.sysname is "Darwin", because
   1775  1.1  christos 			 * __APPLE__ is defined.  We just check the version.
   1776  1.1  christos 			 */
   1777  1.1  christos 			if (osinfo.release[0] < '8' &&
   1778  1.1  christos 			    osinfo.release[1] == '.') {
   1779  1.1  christos 				/*
   1780  1.1  christos 				 * 10.3 (Darwin 7.x) or earlier.
   1781  1.1  christos 				 */
   1782  1.1  christos 				status = PCAP_ERROR_RFMON_NOTSUP;
   1783  1.1  christos 				goto bad;
   1784  1.1  christos 			}
   1785  1.1  christos 			if (osinfo.release[0] == '8' &&
   1786  1.1  christos 			    osinfo.release[1] == '.') {
   1787  1.1  christos 				/*
   1788  1.1  christos 				 * 10.4 (Darwin 8.x).  s/en/wlt/
   1789  1.1  christos 				 */
   1790  1.7  christos 				if (strncmp(p->opt.device, "en", 2) != 0) {
   1791  1.1  christos 					/*
   1792  1.1  christos 					 * Not an enN device; check
   1793  1.1  christos 					 * whether the device even exists.
   1794  1.1  christos 					 */
   1795  1.1  christos 					sockfd = socket(AF_INET, SOCK_DGRAM, 0);
   1796  1.1  christos 					if (sockfd != -1) {
   1797  1.3  christos 						strlcpy(ifrname,
   1798  1.7  christos 						    p->opt.device, ifnamsiz);
   1799  1.1  christos 						if (ioctl(sockfd, SIOCGIFFLAGS,
   1800  1.1  christos 						    (char *)&ifr) < 0) {
   1801  1.1  christos 							/*
   1802  1.1  christos 							 * We assume this
   1803  1.1  christos 							 * failed because
   1804  1.1  christos 							 * the underlying
   1805  1.1  christos 							 * device doesn't
   1806  1.1  christos 							 * exist.
   1807  1.1  christos 							 */
   1808  1.1  christos 							status = PCAP_ERROR_NO_SUCH_DEVICE;
   1809  1.8  christos 							pcap_fmt_errmsg_for_errno(p->errbuf,
   1810  1.1  christos 							    PCAP_ERRBUF_SIZE,
   1811  1.8  christos 							    errno,
   1812  1.8  christos 							    "SIOCGIFFLAGS failed");
   1813  1.1  christos 						} else
   1814  1.1  christos 							status = PCAP_ERROR_RFMON_NOTSUP;
   1815  1.1  christos 						close(sockfd);
   1816  1.1  christos 					} else {
   1817  1.1  christos 						/*
   1818  1.1  christos 						 * We can't find out whether
   1819  1.1  christos 						 * the device exists, so just
   1820  1.1  christos 						 * report "no such device".
   1821  1.1  christos 						 */
   1822  1.1  christos 						status = PCAP_ERROR_NO_SUCH_DEVICE;
   1823  1.8  christos 						pcap_fmt_errmsg_for_errno(p->errbuf,
   1824  1.8  christos 						    PCAP_ERRBUF_SIZE, errno,
   1825  1.8  christos 						    "socket() failed");
   1826  1.1  christos 					}
   1827  1.1  christos 					goto bad;
   1828  1.1  christos 				}
   1829  1.7  christos 				wltdev = malloc(strlen(p->opt.device) + 2);
   1830  1.1  christos 				if (wltdev == NULL) {
   1831  1.8  christos 					pcap_fmt_errmsg_for_errno(p->errbuf,
   1832  1.8  christos 					    PCAP_ERRBUF_SIZE, errno,
   1833  1.8  christos 					    "malloc");
   1834  1.1  christos 					status = PCAP_ERROR;
   1835  1.1  christos 					goto bad;
   1836  1.1  christos 				}
   1837  1.1  christos 				strcpy(wltdev, "wlt");
   1838  1.7  christos 				strcat(wltdev, p->opt.device + 2);
   1839  1.7  christos 				free(p->opt.device);
   1840  1.7  christos 				p->opt.device = wltdev;
   1841  1.1  christos 			}
   1842  1.1  christos 			/*
   1843  1.1  christos 			 * Everything else is 10.5 or later; for those,
   1844  1.1  christos 			 * we just open the enN device, and set the DLT.
   1845  1.1  christos 			 */
   1846  1.1  christos 		}
   1847  1.1  christos 	}
   1848  1.1  christos #endif /* __APPLE__ */
   1849  1.7  christos 
   1850  1.7  christos 	/*
   1851  1.7  christos 	 * If this is FreeBSD, and the device name begins with "usbus",
   1852  1.7  christos 	 * try to create the interface if it's not available.
   1853  1.7  christos 	 */
   1854  1.7  christos #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
   1855  1.7  christos 	if (strncmp(p->opt.device, usbus_prefix, USBUS_PREFIX_LEN) == 0) {
   1856  1.7  christos 		/*
   1857  1.7  christos 		 * Do we already have an interface with that name?
   1858  1.7  christos 		 */
   1859  1.7  christos 		if (if_nametoindex(p->opt.device) == 0) {
   1860  1.7  christos 			/*
   1861  1.7  christos 			 * No.  We need to create it, and, if we
   1862  1.7  christos 			 * succeed, remember that we should destroy
   1863  1.7  christos 			 * it when the pcap_t is closed.
   1864  1.7  christos 			 */
   1865  1.7  christos 			int s;
   1866  1.7  christos 
   1867  1.7  christos 			/*
   1868  1.7  christos 			 * Open a socket to use for ioctls to
   1869  1.7  christos 			 * create the interface.
   1870  1.7  christos 			 */
   1871  1.7  christos 			s = socket(AF_LOCAL, SOCK_DGRAM, 0);
   1872  1.7  christos 			if (s < 0) {
   1873  1.8  christos 				pcap_fmt_errmsg_for_errno(p->errbuf,
   1874  1.8  christos 				    PCAP_ERRBUF_SIZE, errno,
   1875  1.8  christos 				    "Can't open socket");
   1876  1.7  christos 				status = PCAP_ERROR;
   1877  1.7  christos 				goto bad;
   1878  1.7  christos 			}
   1879  1.7  christos 
   1880  1.7  christos 			/*
   1881  1.7  christos 			 * If we haven't already done so, arrange to have
   1882  1.7  christos 			 * "pcap_close_all()" called when we exit.
   1883  1.7  christos 			 */
   1884  1.7  christos 			if (!pcap_do_addexit(p)) {
   1885  1.7  christos 				/*
   1886  1.7  christos 				 * "atexit()" failed; don't create the
   1887  1.7  christos 				 * interface, just give up.
   1888  1.7  christos 				 */
   1889  1.7  christos 				pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
   1890  1.7  christos 				     "atexit failed");
   1891  1.7  christos 				close(s);
   1892  1.7  christos 				status = PCAP_ERROR;
   1893  1.7  christos 				goto bad;
   1894  1.7  christos 			}
   1895  1.7  christos 
   1896  1.7  christos 			/*
   1897  1.7  christos 			 * Create the interface.
   1898  1.7  christos 			 */
   1899  1.7  christos 			strlcpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name));
   1900  1.7  christos 			if (ioctl(s, SIOCIFCREATE2, &ifr) < 0) {
   1901  1.7  christos 				if (errno == EINVAL) {
   1902  1.7  christos 					pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
   1903  1.7  christos 					    "Invalid USB bus interface %s",
   1904  1.7  christos 					    p->opt.device);
   1905  1.7  christos 				} else {
   1906  1.8  christos 					pcap_fmt_errmsg_for_errno(p->errbuf,
   1907  1.8  christos 					    PCAP_ERRBUF_SIZE, errno,
   1908  1.8  christos 					    "Can't create interface for %s",
   1909  1.8  christos 					    p->opt.device);
   1910  1.7  christos 				}
   1911  1.7  christos 				close(s);
   1912  1.7  christos 				status = PCAP_ERROR;
   1913  1.7  christos 				goto bad;
   1914  1.7  christos 			}
   1915  1.7  christos 
   1916  1.7  christos 			/*
   1917  1.7  christos 			 * Make sure we clean this up when we close.
   1918  1.7  christos 			 */
   1919  1.7  christos 			pb->must_do_on_close |= MUST_DESTROY_USBUS;
   1920  1.7  christos 
   1921  1.7  christos 			/*
   1922  1.7  christos 			 * Add this to the list of pcaps to close when we exit.
   1923  1.7  christos 			 */
   1924  1.7  christos 			pcap_add_to_pcaps_to_close(p);
   1925  1.7  christos 		}
   1926  1.7  christos 	}
   1927  1.7  christos #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
   1928  1.7  christos 
   1929  1.1  christos #ifdef HAVE_ZEROCOPY_BPF
   1930  1.1  christos 	/*
   1931  1.1  christos 	 * If the BPF extension to set buffer mode is present, try setting
   1932  1.1  christos 	 * the mode to zero-copy.  If that fails, use regular buffering.  If
   1933  1.1  christos 	 * it succeeds but other setup fails, return an error to the user.
   1934  1.1  christos 	 */
   1935  1.1  christos 	bufmode = BPF_BUFMODE_ZBUF;
   1936  1.1  christos 	if (ioctl(fd, BIOCSETBUFMODE, (caddr_t)&bufmode) == 0) {
   1937  1.1  christos 		/*
   1938  1.1  christos 		 * We have zerocopy BPF; use it.
   1939  1.1  christos 		 */
   1940  1.4  christos 		pb->zerocopy = 1;
   1941  1.1  christos 
   1942  1.1  christos 		/*
   1943  1.1  christos 		 * How to pick a buffer size: first, query the maximum buffer
   1944  1.1  christos 		 * size supported by zero-copy.  This also lets us quickly
   1945  1.1  christos 		 * determine whether the kernel generally supports zero-copy.
   1946  1.1  christos 		 * Then, if a buffer size was specified, use that, otherwise
   1947  1.1  christos 		 * query the default buffer size, which reflects kernel
   1948  1.1  christos 		 * policy for a desired default.  Round to the nearest page
   1949  1.1  christos 		 * size.
   1950  1.1  christos 		 */
   1951  1.1  christos 		if (ioctl(fd, BIOCGETZMAX, (caddr_t)&zbufmax) < 0) {
   1952  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1953  1.8  christos 			    errno, "BIOCGETZMAX");
   1954  1.5  christos 			status = PCAP_ERROR;
   1955  1.1  christos 			goto bad;
   1956  1.1  christos 		}
   1957  1.1  christos 
   1958  1.1  christos 		if (p->opt.buffer_size != 0) {
   1959  1.1  christos 			/*
   1960  1.1  christos 			 * A buffer size was explicitly specified; use it.
   1961  1.1  christos 			 */
   1962  1.1  christos 			v = p->opt.buffer_size;
   1963  1.1  christos 		} else {
   1964  1.1  christos 			if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) ||
   1965  1.1  christos 			    v < DEFAULT_BUFSIZE)
   1966  1.1  christos 				v = DEFAULT_BUFSIZE;
   1967  1.1  christos 		}
   1968  1.1  christos #ifndef roundup
   1969  1.1  christos #define roundup(x, y)   ((((x)+((y)-1))/(y))*(y))  /* to any y */
   1970  1.1  christos #endif
   1971  1.4  christos 		pb->zbufsize = roundup(v, getpagesize());
   1972  1.4  christos 		if (pb->zbufsize > zbufmax)
   1973  1.4  christos 			pb->zbufsize = zbufmax;
   1974  1.4  christos 		pb->zbuf1 = mmap(NULL, pb->zbufsize, PROT_READ | PROT_WRITE,
   1975  1.1  christos 		    MAP_ANON, -1, 0);
   1976  1.4  christos 		pb->zbuf2 = mmap(NULL, pb->zbufsize, PROT_READ | PROT_WRITE,
   1977  1.1  christos 		    MAP_ANON, -1, 0);
   1978  1.4  christos 		if (pb->zbuf1 == MAP_FAILED || pb->zbuf2 == MAP_FAILED) {
   1979  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1980  1.8  christos 			    errno, "mmap");
   1981  1.5  christos 			status = PCAP_ERROR;
   1982  1.1  christos 			goto bad;
   1983  1.1  christos 		}
   1984  1.4  christos 		memset(&bz, 0, sizeof(bz)); /* bzero() deprecated, replaced with memset() */
   1985  1.4  christos 		bz.bz_bufa = pb->zbuf1;
   1986  1.4  christos 		bz.bz_bufb = pb->zbuf2;
   1987  1.4  christos 		bz.bz_buflen = pb->zbufsize;
   1988  1.1  christos 		if (ioctl(fd, BIOCSETZBUF, (caddr_t)&bz) < 0) {
   1989  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1990  1.8  christos 			    errno, "BIOCSETZBUF");
   1991  1.5  christos 			status = PCAP_ERROR;
   1992  1.1  christos 			goto bad;
   1993  1.1  christos 		}
   1994  1.7  christos 		(void)strncpy(ifrname, p->opt.device, ifnamsiz);
   1995  1.1  christos 		if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
   1996  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   1997  1.8  christos 			    errno, "BIOCSETIF: %s", p->opt.device);
   1998  1.5  christos 			status = PCAP_ERROR;
   1999  1.1  christos 			goto bad;
   2000  1.1  christos 		}
   2001  1.4  christos 		v = pb->zbufsize - sizeof(struct bpf_zbuf_header);
   2002  1.1  christos 	} else
   2003  1.1  christos #endif
   2004  1.1  christos 	{
   2005  1.1  christos 		/*
   2006  1.1  christos 		 * We don't have zerocopy BPF.
   2007  1.1  christos 		 * Set the buffer size.
   2008  1.1  christos 		 */
   2009  1.1  christos 		if (p->opt.buffer_size != 0) {
   2010  1.1  christos 			/*
   2011  1.1  christos 			 * A buffer size was explicitly specified; use it.
   2012  1.1  christos 			 */
   2013  1.1  christos 			if (ioctl(fd, BIOCSBLEN,
   2014  1.1  christos 			    (caddr_t)&p->opt.buffer_size) < 0) {
   2015  1.8  christos 				pcap_fmt_errmsg_for_errno(p->errbuf,
   2016  1.8  christos 				    PCAP_ERRBUF_SIZE, errno,
   2017  1.8  christos 				    "BIOCSBLEN: %s", p->opt.device);
   2018  1.1  christos 				status = PCAP_ERROR;
   2019  1.1  christos 				goto bad;
   2020  1.1  christos 			}
   2021  1.1  christos 
   2022  1.1  christos 			/*
   2023  1.1  christos 			 * Now bind to the device.
   2024  1.1  christos 			 */
   2025  1.7  christos 			(void)strncpy(ifrname, p->opt.device, ifnamsiz);
   2026  1.3  christos #ifdef BIOCSETLIF
   2027  1.3  christos 			if (ioctl(fd, BIOCSETLIF, (caddr_t)&ifr) < 0)
   2028  1.3  christos #else
   2029  1.3  christos 			if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0)
   2030  1.3  christos #endif
   2031  1.3  christos 			{
   2032  1.1  christos 				status = check_setif_failure(p, errno);
   2033  1.1  christos 				goto bad;
   2034  1.1  christos 			}
   2035  1.1  christos 		} else {
   2036  1.1  christos 			/*
   2037  1.1  christos 			 * No buffer size was explicitly specified.
   2038  1.1  christos 			 *
   2039  1.1  christos 			 * Try finding a good size for the buffer;
   2040  1.1  christos 			 * DEFAULT_BUFSIZE may be too big, so keep
   2041  1.1  christos 			 * cutting it in half until we find a size
   2042  1.1  christos 			 * that works, or run out of sizes to try.
   2043  1.1  christos 			 * If the default is larger, don't make it smaller.
   2044  1.1  christos 			 */
   2045  1.1  christos 			if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) ||
   2046  1.1  christos 			    v < DEFAULT_BUFSIZE)
   2047  1.1  christos 				v = DEFAULT_BUFSIZE;
   2048  1.1  christos 			for ( ; v != 0; v >>= 1) {
   2049  1.1  christos 				/*
   2050  1.1  christos 				 * Ignore the return value - this is because the
   2051  1.1  christos 				 * call fails on BPF systems that don't have
   2052  1.1  christos 				 * kernel malloc.  And if the call fails, it's
   2053  1.1  christos 				 * no big deal, we just continue to use the
   2054  1.1  christos 				 * standard buffer size.
   2055  1.1  christos 				 */
   2056  1.1  christos 				(void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
   2057  1.1  christos 
   2058  1.7  christos 				(void)strncpy(ifrname, p->opt.device, ifnamsiz);
   2059  1.3  christos #ifdef BIOCSETLIF
   2060  1.3  christos 				if (ioctl(fd, BIOCSETLIF, (caddr_t)&ifr) >= 0)
   2061  1.3  christos #else
   2062  1.1  christos 				if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
   2063  1.3  christos #endif
   2064  1.1  christos 					break;	/* that size worked; we're done */
   2065  1.1  christos 
   2066  1.1  christos 				if (errno != ENOBUFS) {
   2067  1.1  christos 					status = check_setif_failure(p, errno);
   2068  1.1  christos 					goto bad;
   2069  1.1  christos 				}
   2070  1.1  christos 			}
   2071  1.1  christos 
   2072  1.1  christos 			if (v == 0) {
   2073  1.7  christos 				pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
   2074  1.1  christos 				    "BIOCSBLEN: %s: No buffer size worked",
   2075  1.7  christos 				    p->opt.device);
   2076  1.1  christos 				status = PCAP_ERROR;
   2077  1.1  christos 				goto bad;
   2078  1.1  christos 			}
   2079  1.1  christos 		}
   2080  1.1  christos 	}
   2081  1.1  christos 
   2082  1.1  christos 	/* Get the data link layer type. */
   2083  1.1  christos 	if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
   2084  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   2085  1.8  christos 		    errno, "BIOCGDLT");
   2086  1.1  christos 		status = PCAP_ERROR;
   2087  1.1  christos 		goto bad;
   2088  1.1  christos 	}
   2089  1.1  christos 
   2090  1.1  christos #ifdef _AIX
   2091  1.1  christos 	/*
   2092  1.1  christos 	 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
   2093  1.1  christos 	 */
   2094  1.1  christos 	switch (v) {
   2095  1.1  christos 
   2096  1.1  christos 	case IFT_ETHER:
   2097  1.1  christos 	case IFT_ISO88023:
   2098  1.1  christos 		v = DLT_EN10MB;
   2099  1.1  christos 		break;
   2100  1.1  christos 
   2101  1.1  christos 	case IFT_FDDI:
   2102  1.1  christos 		v = DLT_FDDI;
   2103  1.1  christos 		break;
   2104  1.1  christos 
   2105  1.1  christos 	case IFT_ISO88025:
   2106  1.1  christos 		v = DLT_IEEE802;
   2107  1.1  christos 		break;
   2108  1.1  christos 
   2109  1.1  christos 	case IFT_LOOP:
   2110  1.1  christos 		v = DLT_NULL;
   2111  1.1  christos 		break;
   2112  1.1  christos 
   2113  1.1  christos 	default:
   2114  1.1  christos 		/*
   2115  1.1  christos 		 * We don't know what to map this to yet.
   2116  1.1  christos 		 */
   2117  1.7  christos 		pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown interface type %u",
   2118  1.1  christos 		    v);
   2119  1.1  christos 		status = PCAP_ERROR;
   2120  1.1  christos 		goto bad;
   2121  1.1  christos 	}
   2122  1.1  christos #endif
   2123  1.1  christos #if _BSDI_VERSION - 0 >= 199510
   2124  1.1  christos 	/* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
   2125  1.1  christos 	switch (v) {
   2126  1.1  christos 
   2127  1.1  christos 	case DLT_SLIP:
   2128  1.1  christos 		v = DLT_SLIP_BSDOS;
   2129  1.1  christos 		break;
   2130  1.1  christos 
   2131  1.1  christos 	case DLT_PPP:
   2132  1.1  christos 		v = DLT_PPP_BSDOS;
   2133  1.1  christos 		break;
   2134  1.1  christos 
   2135  1.1  christos 	case 11:	/*DLT_FR*/
   2136  1.1  christos 		v = DLT_FRELAY;
   2137  1.1  christos 		break;
   2138  1.1  christos 
   2139  1.1  christos 	case 12:	/*DLT_C_HDLC*/
   2140  1.1  christos 		v = DLT_CHDLC;
   2141  1.1  christos 		break;
   2142  1.1  christos 	}
   2143  1.1  christos #endif
   2144  1.1  christos 
   2145  1.1  christos #ifdef BIOCGDLTLIST
   2146  1.1  christos 	/*
   2147  1.1  christos 	 * We know the default link type -- now determine all the DLTs
   2148  1.1  christos 	 * this interface supports.  If this fails with EINVAL, it's
   2149  1.1  christos 	 * not fatal; we just don't get to use the feature later.
   2150  1.1  christos 	 */
   2151  1.1  christos 	if (get_dlt_list(fd, v, &bdl, p->errbuf) == -1) {
   2152  1.1  christos 		status = PCAP_ERROR;
   2153  1.1  christos 		goto bad;
   2154  1.1  christos 	}
   2155  1.1  christos 	p->dlt_count = bdl.bfl_len;
   2156  1.1  christos 	p->dlt_list = bdl.bfl_list;
   2157  1.1  christos 
   2158  1.1  christos #ifdef __APPLE__
   2159  1.1  christos 	/*
   2160  1.1  christos 	 * Monitor mode fun, continued.
   2161  1.1  christos 	 *
   2162  1.1  christos 	 * For 10.5 and, we're assuming, later releases, as noted above,
   2163  1.1  christos 	 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
   2164  1.1  christos 	 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
   2165  1.1  christos 	 * DLT_ value.  Choosing one of the 802.11 DLT_ values will turn
   2166  1.1  christos 	 * monitor mode on.
   2167  1.1  christos 	 *
   2168  1.1  christos 	 * Therefore, if the user asked for monitor mode, we filter out
   2169  1.1  christos 	 * the DLT_EN10MB value, as you can't get that in monitor mode,
   2170  1.1  christos 	 * and, if the user didn't ask for monitor mode, we filter out
   2171  1.1  christos 	 * the 802.11 DLT_ values, because selecting those will turn
   2172  1.1  christos 	 * monitor mode on.  Then, for monitor mode, if an 802.11-plus-
   2173  1.1  christos 	 * radio DLT_ value is offered, we try to select that, otherwise
   2174  1.1  christos 	 * we try to select DLT_IEEE802_11.
   2175  1.1  christos 	 */
   2176  1.1  christos 	if (have_osinfo) {
   2177  1.1  christos 		if (isdigit((unsigned)osinfo.release[0]) &&
   2178  1.1  christos 		     (osinfo.release[0] == '9' ||
   2179  1.1  christos 		     isdigit((unsigned)osinfo.release[1]))) {
   2180  1.1  christos 			/*
   2181  1.1  christos 			 * 10.5 (Darwin 9.x), or later.
   2182  1.1  christos 			 */
   2183  1.1  christos 			new_dlt = find_802_11(&bdl);
   2184  1.1  christos 			if (new_dlt != -1) {
   2185  1.1  christos 				/*
   2186  1.1  christos 				 * We have at least one 802.11 DLT_ value,
   2187  1.1  christos 				 * so this is an 802.11 interface.
   2188  1.1  christos 				 * new_dlt is the best of the 802.11
   2189  1.1  christos 				 * DLT_ values in the list.
   2190  1.1  christos 				 */
   2191  1.1  christos 				if (p->opt.rfmon) {
   2192  1.1  christos 					/*
   2193  1.1  christos 					 * Our caller wants monitor mode.
   2194  1.1  christos 					 * Purge DLT_EN10MB from the list
   2195  1.1  christos 					 * of link-layer types, as selecting
   2196  1.1  christos 					 * it will keep monitor mode off.
   2197  1.1  christos 					 */
   2198  1.1  christos 					remove_en(p);
   2199  1.1  christos 
   2200  1.1  christos 					/*
   2201  1.1  christos 					 * If the new mode we want isn't
   2202  1.1  christos 					 * the default mode, attempt to
   2203  1.1  christos 					 * select the new mode.
   2204  1.1  christos 					 */
   2205  1.7  christos 					if ((u_int)new_dlt != v) {
   2206  1.1  christos 						if (ioctl(p->fd, BIOCSDLT,
   2207  1.1  christos 						    &new_dlt) != -1) {
   2208  1.1  christos 							/*
   2209  1.1  christos 							 * We succeeded;
   2210  1.1  christos 							 * make this the
   2211  1.1  christos 							 * new DLT_ value.
   2212  1.1  christos 							 */
   2213  1.1  christos 							v = new_dlt;
   2214  1.1  christos 						}
   2215  1.1  christos 					}
   2216  1.1  christos 				} else {
   2217  1.1  christos 					/*
   2218  1.1  christos 					 * Our caller doesn't want
   2219  1.1  christos 					 * monitor mode.  Unless this
   2220  1.1  christos 					 * is being done by pcap_open_live(),
   2221  1.1  christos 					 * purge the 802.11 link-layer types
   2222  1.1  christos 					 * from the list, as selecting
   2223  1.1  christos 					 * one of them will turn monitor
   2224  1.1  christos 					 * mode on.
   2225  1.1  christos 					 */
   2226  1.1  christos 					if (!p->oldstyle)
   2227  1.1  christos 						remove_802_11(p);
   2228  1.1  christos 				}
   2229  1.1  christos 			} else {
   2230  1.1  christos 				if (p->opt.rfmon) {
   2231  1.1  christos 					/*
   2232  1.1  christos 					 * The caller requested monitor
   2233  1.1  christos 					 * mode, but we have no 802.11
   2234  1.1  christos 					 * link-layer types, so they
   2235  1.1  christos 					 * can't have it.
   2236  1.1  christos 					 */
   2237  1.1  christos 					status = PCAP_ERROR_RFMON_NOTSUP;
   2238  1.1  christos 					goto bad;
   2239  1.1  christos 				}
   2240  1.1  christos 			}
   2241  1.1  christos 		}
   2242  1.1  christos 	}
   2243  1.1  christos #elif defined(HAVE_BSD_IEEE80211)
   2244  1.1  christos 	/*
   2245  1.1  christos 	 * *BSD with the new 802.11 ioctls.
   2246  1.1  christos 	 * Do we want monitor mode?
   2247  1.1  christos 	 */
   2248  1.1  christos 	if (p->opt.rfmon) {
   2249  1.1  christos 		/*
   2250  1.1  christos 		 * Try to put the interface into monitor mode.
   2251  1.1  christos 		 */
   2252  1.5  christos 		retv = monitor_mode(p, 1);
   2253  1.5  christos 		if (retv != 0) {
   2254  1.1  christos 			/*
   2255  1.1  christos 			 * We failed.
   2256  1.1  christos 			 */
   2257  1.5  christos 			status = retv;
   2258  1.1  christos 			goto bad;
   2259  1.1  christos 		}
   2260  1.1  christos 
   2261  1.1  christos 		/*
   2262  1.1  christos 		 * We're in monitor mode.
   2263  1.1  christos 		 * Try to find the best 802.11 DLT_ value and, if we
   2264  1.1  christos 		 * succeed, try to switch to that mode if we're not
   2265  1.1  christos 		 * already in that mode.
   2266  1.1  christos 		 */
   2267  1.1  christos 		new_dlt = find_802_11(&bdl);
   2268  1.2  christos 		if (new_dlt != (unsigned)-1) {
   2269  1.1  christos 			/*
   2270  1.1  christos 			 * We have at least one 802.11 DLT_ value.
   2271  1.1  christos 			 * new_dlt is the best of the 802.11
   2272  1.1  christos 			 * DLT_ values in the list.
   2273  1.1  christos 			 *
   2274  1.1  christos 			 * If the new mode we want isn't the default mode,
   2275  1.1  christos 			 * attempt to select the new mode.
   2276  1.1  christos 			 */
   2277  1.7  christos 			if ((u_int)new_dlt != v) {
   2278  1.1  christos 				if (ioctl(p->fd, BIOCSDLT, &new_dlt) != -1) {
   2279  1.1  christos 					/*
   2280  1.1  christos 					 * We succeeded; make this the
   2281  1.1  christos 					 * new DLT_ value.
   2282  1.1  christos 					 */
   2283  1.1  christos 					v = new_dlt;
   2284  1.1  christos 				}
   2285  1.1  christos 			}
   2286  1.1  christos 		}
   2287  1.1  christos 	}
   2288  1.1  christos #endif /* various platforms */
   2289  1.1  christos #endif /* BIOCGDLTLIST */
   2290  1.1  christos 
   2291  1.1  christos 	/*
   2292  1.1  christos 	 * If this is an Ethernet device, and we don't have a DLT_ list,
   2293  1.1  christos 	 * give it a list with DLT_EN10MB and DLT_DOCSIS.  (That'd give
   2294  1.1  christos 	 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
   2295  1.1  christos 	 * do, but there's not much we can do about that without finding
   2296  1.1  christos 	 * some other way of determining whether it's an Ethernet or 802.11
   2297  1.1  christos 	 * device.)
   2298  1.1  christos 	 */
   2299  1.1  christos 	if (v == DLT_EN10MB && p->dlt_count == 0) {
   2300  1.1  christos 		p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
   2301  1.1  christos 		/*
   2302  1.1  christos 		 * If that fails, just leave the list empty.
   2303  1.1  christos 		 */
   2304  1.1  christos 		if (p->dlt_list != NULL) {
   2305  1.1  christos 			p->dlt_list[0] = DLT_EN10MB;
   2306  1.1  christos 			p->dlt_list[1] = DLT_DOCSIS;
   2307  1.1  christos 			p->dlt_count = 2;
   2308  1.1  christos 		}
   2309  1.1  christos 	}
   2310  1.1  christos #ifdef PCAP_FDDIPAD
   2311  1.1  christos 	if (v == DLT_FDDI)
   2312  1.1  christos 		p->fddipad = PCAP_FDDIPAD;
   2313  1.1  christos 	else
   2314  1.4  christos #endif
   2315  1.1  christos 		p->fddipad = 0;
   2316  1.1  christos 	p->linktype = v;
   2317  1.1  christos 
   2318  1.1  christos #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
   2319  1.1  christos 	/*
   2320  1.1  christos 	 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
   2321  1.1  christos 	 * the link-layer source address isn't forcibly overwritten.
   2322  1.1  christos 	 * (Should we ignore errors?  Should we do this only if
   2323  1.1  christos 	 * we're open for writing?)
   2324  1.1  christos 	 *
   2325  1.1  christos 	 * XXX - I seem to remember some packet-sending bug in some
   2326  1.1  christos 	 * BSDs - check CVS log for "bpf.c"?
   2327  1.1  christos 	 */
   2328  1.1  christos 	if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
   2329  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   2330  1.8  christos 		    errno, "BIOCSHDRCMPLT");
   2331  1.1  christos 		status = PCAP_ERROR;
   2332  1.1  christos 		goto bad;
   2333  1.1  christos 	}
   2334  1.1  christos #endif
   2335  1.1  christos 	/* set timeout */
   2336  1.1  christos #ifdef HAVE_ZEROCOPY_BPF
   2337  1.4  christos 	/*
   2338  1.4  christos 	 * In zero-copy mode, we just use the timeout in select().
   2339  1.4  christos 	 * XXX - what if we're in non-blocking mode and the *application*
   2340  1.4  christos 	 * is using select() or poll() or kqueues or....?
   2341  1.4  christos 	 */
   2342  1.4  christos 	if (p->opt.timeout && !pb->zerocopy) {
   2343  1.1  christos #else
   2344  1.4  christos 	if (p->opt.timeout) {
   2345  1.1  christos #endif
   2346  1.1  christos 		/*
   2347  1.1  christos 		 * XXX - is this seconds/nanoseconds in AIX?
   2348  1.1  christos 		 * (Treating it as such doesn't fix the timeout
   2349  1.1  christos 		 * problem described below.)
   2350  1.1  christos 		 *
   2351  1.1  christos 		 * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
   2352  1.1  christos 		 * 64-bit userland - it takes, as an argument, a
   2353  1.1  christos 		 * "struct BPF_TIMEVAL", which has 32-bit tv_sec
   2354  1.1  christos 		 * and tv_usec, rather than a "struct timeval".
   2355  1.1  christos 		 *
   2356  1.1  christos 		 * If this platform defines "struct BPF_TIMEVAL",
   2357  1.1  christos 		 * we check whether the structure size in BIOCSRTIMEOUT
   2358  1.1  christos 		 * is that of a "struct timeval" and, if not, we use
   2359  1.1  christos 		 * a "struct BPF_TIMEVAL" rather than a "struct timeval".
   2360  1.1  christos 		 * (That way, if the bug is fixed in a future release,
   2361  1.1  christos 		 * we will still do the right thing.)
   2362  1.1  christos 		 */
   2363  1.1  christos 		struct timeval to;
   2364  1.1  christos #ifdef HAVE_STRUCT_BPF_TIMEVAL
   2365  1.1  christos 		struct BPF_TIMEVAL bpf_to;
   2366  1.1  christos 
   2367  1.1  christos 		if (IOCPARM_LEN(BIOCSRTIMEOUT) != sizeof(struct timeval)) {
   2368  1.4  christos 			bpf_to.tv_sec = p->opt.timeout / 1000;
   2369  1.4  christos 			bpf_to.tv_usec = (p->opt.timeout * 1000) % 1000000;
   2370  1.1  christos 			if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&bpf_to) < 0) {
   2371  1.8  christos 				pcap_fmt_errmsg_for_errno(p->errbuf,
   2372  1.8  christos 				    errno, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT");
   2373  1.1  christos 				status = PCAP_ERROR;
   2374  1.1  christos 				goto bad;
   2375  1.1  christos 			}
   2376  1.1  christos 		} else {
   2377  1.1  christos #endif
   2378  1.4  christos 			to.tv_sec = p->opt.timeout / 1000;
   2379  1.4  christos 			to.tv_usec = (p->opt.timeout * 1000) % 1000000;
   2380  1.1  christos 			if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) {
   2381  1.8  christos 				pcap_fmt_errmsg_for_errno(p->errbuf,
   2382  1.8  christos 				    errno, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT");
   2383  1.1  christos 				status = PCAP_ERROR;
   2384  1.1  christos 				goto bad;
   2385  1.1  christos 			}
   2386  1.1  christos #ifdef HAVE_STRUCT_BPF_TIMEVAL
   2387  1.1  christos 		}
   2388  1.1  christos #endif
   2389  1.1  christos 	}
   2390  1.1  christos 
   2391  1.1  christos #ifdef	BIOCIMMEDIATE
   2392  1.1  christos 	/*
   2393  1.1  christos 	 * Darren Reed notes that
   2394  1.1  christos 	 *
   2395  1.1  christos 	 *	On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
   2396  1.1  christos 	 *	timeout appears to be ignored and it waits until the buffer
   2397  1.1  christos 	 *	is filled before returning.  The result of not having it
   2398  1.1  christos 	 *	set is almost worse than useless if your BPF filter
   2399  1.1  christos 	 *	is reducing things to only a few packets (i.e. one every
   2400  1.1  christos 	 *	second or so).
   2401  1.1  christos 	 *
   2402  1.4  christos 	 * so we always turn BIOCIMMEDIATE mode on if this is AIX.
   2403  1.4  christos 	 *
   2404  1.4  christos 	 * For other platforms, we don't turn immediate mode on by default,
   2405  1.4  christos 	 * as that would mean we get woken up for every packet, which
   2406  1.4  christos 	 * probably isn't what you want for a packet sniffer.
   2407  1.1  christos 	 *
   2408  1.4  christos 	 * We set immediate mode if the caller requested it by calling
   2409  1.4  christos 	 * pcap_set_immediate() before calling pcap_activate().
   2410  1.4  christos 	 */
   2411  1.4  christos #ifndef _AIX
   2412  1.4  christos 	if (p->opt.immediate) {
   2413  1.4  christos #endif /* _AIX */
   2414  1.4  christos 		v = 1;
   2415  1.4  christos 		if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) {
   2416  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   2417  1.8  christos 			    errno, "BIOCIMMEDIATE");
   2418  1.4  christos 			status = PCAP_ERROR;
   2419  1.4  christos 			goto bad;
   2420  1.4  christos 		}
   2421  1.4  christos #ifndef _AIX
   2422  1.4  christos 	}
   2423  1.4  christos #endif /* _AIX */
   2424  1.4  christos #else /* BIOCIMMEDIATE */
   2425  1.4  christos 	if (p->opt.immediate) {
   2426  1.4  christos 		/*
   2427  1.4  christos 		 * We don't support immediate mode.  Fail.
   2428  1.4  christos 		 */
   2429  1.7  christos 		pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Immediate mode not supported");
   2430  1.1  christos 		status = PCAP_ERROR;
   2431  1.1  christos 		goto bad;
   2432  1.1  christos 	}
   2433  1.4  christos #endif /* BIOCIMMEDIATE */
   2434  1.1  christos 
   2435  1.1  christos 	if (p->opt.promisc) {
   2436  1.1  christos 		/* set promiscuous mode, just warn if it fails */
   2437  1.1  christos 		if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) {
   2438  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   2439  1.8  christos 			    errno, "BIOCPROMISC");
   2440  1.1  christos 			status = PCAP_WARNING_PROMISC_NOTSUP;
   2441  1.1  christos 		}
   2442  1.1  christos 	}
   2443  1.1  christos 
   2444  1.7  christos #ifdef BIOCSTSTAMP
   2445  1.7  christos 	v = BPF_T_BINTIME;
   2446  1.7  christos 	if (ioctl(p->fd, BIOCSTSTAMP, &v) < 0) {
   2447  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   2448  1.8  christos 		    errno, "BIOCSTSTAMP");
   2449  1.7  christos 		status = PCAP_ERROR;
   2450  1.7  christos 		goto bad;
   2451  1.7  christos 	}
   2452  1.7  christos #endif /* BIOCSTSTAMP */
   2453  1.7  christos 
   2454  1.1  christos 	if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
   2455  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   2456  1.8  christos 		    errno, "BIOCGBLEN");
   2457  1.1  christos 		status = PCAP_ERROR;
   2458  1.1  christos 		goto bad;
   2459  1.1  christos 	}
   2460  1.1  christos 	p->bufsize = v;
   2461  1.1  christos #ifdef HAVE_ZEROCOPY_BPF
   2462  1.4  christos 	if (!pb->zerocopy) {
   2463  1.1  christos #endif
   2464  1.7  christos 	p->buffer = malloc(p->bufsize);
   2465  1.1  christos 	if (p->buffer == NULL) {
   2466  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   2467  1.8  christos 		    errno, "malloc");
   2468  1.1  christos 		status = PCAP_ERROR;
   2469  1.1  christos 		goto bad;
   2470  1.1  christos 	}
   2471  1.1  christos #ifdef _AIX
   2472  1.1  christos 	/* For some strange reason this seems to prevent the EFAULT
   2473  1.1  christos 	 * problems we have experienced from AIX BPF. */
   2474  1.1  christos 	memset(p->buffer, 0x0, p->bufsize);
   2475  1.1  christos #endif
   2476  1.1  christos #ifdef HAVE_ZEROCOPY_BPF
   2477  1.1  christos 	}
   2478  1.1  christos #endif
   2479  1.1  christos 
   2480  1.1  christos 	/*
   2481  1.1  christos 	 * If there's no filter program installed, there's
   2482  1.1  christos 	 * no indication to the kernel of what the snapshot
   2483  1.1  christos 	 * length should be, so no snapshotting is done.
   2484  1.1  christos 	 *
   2485  1.1  christos 	 * Therefore, when we open the device, we install
   2486  1.1  christos 	 * an "accept everything" filter with the specified
   2487  1.1  christos 	 * snapshot length.
   2488  1.1  christos 	 */
   2489  1.1  christos 	total_insn.code = (u_short)(BPF_RET | BPF_K);
   2490  1.1  christos 	total_insn.jt = 0;
   2491  1.1  christos 	total_insn.jf = 0;
   2492  1.1  christos 	total_insn.k = p->snapshot;
   2493  1.1  christos 
   2494  1.1  christos 	total_prog.bf_len = 1;
   2495  1.1  christos 	total_prog.bf_insns = &total_insn;
   2496  1.1  christos 	if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) {
   2497  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   2498  1.8  christos 		    errno, "BIOCSETF");
   2499  1.1  christos 		status = PCAP_ERROR;
   2500  1.1  christos 		goto bad;
   2501  1.1  christos 	}
   2502  1.1  christos 
   2503  1.1  christos 	/*
   2504  1.1  christos 	 * On most BPF platforms, either you can do a "select()" or
   2505  1.1  christos 	 * "poll()" on a BPF file descriptor and it works correctly,
   2506  1.1  christos 	 * or you can do it and it will return "readable" if the
   2507  1.1  christos 	 * hold buffer is full but not if the timeout expires *and*
   2508  1.1  christos 	 * a non-blocking read will, if the hold buffer is empty
   2509  1.1  christos 	 * but the store buffer isn't empty, rotate the buffers
   2510  1.1  christos 	 * and return what packets are available.
   2511  1.1  christos 	 *
   2512  1.1  christos 	 * In the latter case, the fact that a non-blocking read
   2513  1.1  christos 	 * will give you the available packets means you can work
   2514  1.1  christos 	 * around the failure of "select()" and "poll()" to wake up
   2515  1.1  christos 	 * and return "readable" when the timeout expires by using
   2516  1.1  christos 	 * the timeout as the "select()" or "poll()" timeout, putting
   2517  1.1  christos 	 * the BPF descriptor into non-blocking mode, and read from
   2518  1.1  christos 	 * it regardless of whether "select()" reports it as readable
   2519  1.1  christos 	 * or not.
   2520  1.1  christos 	 *
   2521  1.1  christos 	 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
   2522  1.1  christos 	 * won't wake up and return "readable" if the timer expires
   2523  1.1  christos 	 * and non-blocking reads return EWOULDBLOCK if the hold
   2524  1.1  christos 	 * buffer is empty, even if the store buffer is non-empty.
   2525  1.1  christos 	 *
   2526  1.1  christos 	 * This means the workaround in question won't work.
   2527  1.1  christos 	 *
   2528  1.1  christos 	 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
   2529  1.1  christos 	 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
   2530  1.1  christos 	 * here".  On all other BPF platforms, we set it to the FD for
   2531  1.1  christos 	 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
   2532  1.1  christos 	 * read will, if the hold buffer is empty and the store buffer
   2533  1.1  christos 	 * isn't empty, rotate the buffers and return what packets are
   2534  1.1  christos 	 * there (and in sufficiently recent versions of OpenBSD
   2535  1.1  christos 	 * "select()" and "poll()" should work correctly).
   2536  1.1  christos 	 *
   2537  1.1  christos 	 * XXX - what about AIX?
   2538  1.1  christos 	 */
   2539  1.1  christos 	p->selectable_fd = p->fd;	/* assume select() works until we know otherwise */
   2540  1.1  christos 	if (have_osinfo) {
   2541  1.1  christos 		/*
   2542  1.1  christos 		 * We can check what OS this is.
   2543  1.1  christos 		 */
   2544  1.1  christos 		if (strcmp(osinfo.sysname, "FreeBSD") == 0) {
   2545  1.1  christos 			if (strncmp(osinfo.release, "4.3-", 4) == 0 ||
   2546  1.1  christos 			     strncmp(osinfo.release, "4.4-", 4) == 0)
   2547  1.1  christos 				p->selectable_fd = -1;
   2548  1.1  christos 		}
   2549  1.1  christos 	}
   2550  1.1  christos 
   2551  1.1  christos 	p->read_op = pcap_read_bpf;
   2552  1.1  christos 	p->inject_op = pcap_inject_bpf;
   2553  1.1  christos 	p->setfilter_op = pcap_setfilter_bpf;
   2554  1.1  christos 	p->setdirection_op = pcap_setdirection_bpf;
   2555  1.1  christos 	p->set_datalink_op = pcap_set_datalink_bpf;
   2556  1.3  christos 	p->getnonblock_op = pcap_getnonblock_bpf;
   2557  1.3  christos 	p->setnonblock_op = pcap_setnonblock_bpf;
   2558  1.1  christos 	p->stats_op = pcap_stats_bpf;
   2559  1.1  christos 	p->cleanup_op = pcap_cleanup_bpf;
   2560  1.1  christos 
   2561  1.1  christos 	return (status);
   2562  1.1  christos  bad:
   2563  1.4  christos 	pcap_cleanup_bpf(p);
   2564  1.1  christos 	return (status);
   2565  1.1  christos }
   2566  1.1  christos 
   2567  1.7  christos /*
   2568  1.7  christos  * Not all interfaces can be bound to by BPF, so try to bind to
   2569  1.7  christos  * the specified interface; return 0 if we fail with
   2570  1.7  christos  * PCAP_ERROR_NO_SUCH_DEVICE (which means we got an ENXIO when we tried
   2571  1.7  christos  * to bind, which means this interface isn't in the list of interfaces
   2572  1.7  christos  * attached to BPF) and 1 otherwise.
   2573  1.7  christos  */
   2574  1.7  christos static int
   2575  1.7  christos check_bpf_bindable(const char *name)
   2576  1.7  christos {
   2577  1.7  christos 	int fd;
   2578  1.7  christos 	char errbuf[PCAP_ERRBUF_SIZE];
   2579  1.7  christos 
   2580  1.8  christos 	/*
   2581  1.8  christos 	 * On macOS, we don't do this check if the device name begins
   2582  1.8  christos 	 * with "wlt"; at least some versions of macOS (actually, it
   2583  1.8  christos 	 * was called "Mac OS X" then...) offer monitor mode capturing
   2584  1.8  christos 	 * by having a separate "monitor mode" device for each wireless
   2585  1.8  christos 	 * adapter, rather than by implementing the ioctls that
   2586  1.8  christos 	 * {Free,Net,Open,DragonFly}BSD provide. Opening that device
   2587  1.8  christos 	 * puts the adapter into monitor mode, which, at least for
   2588  1.8  christos 	 * some adapters, causes them to deassociate from the network
   2589  1.8  christos 	 * with which they're associated.
   2590  1.8  christos 	 *
   2591  1.8  christos 	 * Instead, we try to open the corresponding "en" device (so
   2592  1.8  christos 	 * that we don't end up with, for users without sufficient
   2593  1.8  christos 	 * privilege to open capture devices, a list of adapters that
   2594  1.8  christos 	 * only includes the wlt devices).
   2595  1.8  christos 	 */
   2596  1.8  christos #ifdef __APPLE__
   2597  1.8  christos 	if (strncmp(name, "wlt", 3) == 0) {
   2598  1.8  christos 		char *en_name;
   2599  1.8  christos 		size_t en_name_len;
   2600  1.8  christos 
   2601  1.8  christos 		/*
   2602  1.8  christos 		 * Try to allocate a buffer for the "en"
   2603  1.8  christos 		 * device's name.
   2604  1.8  christos 		 */
   2605  1.8  christos 		en_name_len = strlen(name) - 1;
   2606  1.8  christos 		en_name = malloc(en_name_len + 1);
   2607  1.8  christos 		if (en_name == NULL) {
   2608  1.8  christos 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
   2609  1.8  christos 			    errno, "malloc");
   2610  1.8  christos 			return (-1);
   2611  1.8  christos 		}
   2612  1.8  christos 		strcpy(en_name, "en");
   2613  1.8  christos 		strcat(en_name, name + 3);
   2614  1.8  christos 		fd = bpf_open_and_bind(en_name, errbuf);
   2615  1.8  christos 		free(en_name);
   2616  1.8  christos 	} else
   2617  1.8  christos #endif /* __APPLE */
   2618  1.7  christos 	fd = bpf_open_and_bind(name, errbuf);
   2619  1.7  christos 	if (fd < 0) {
   2620  1.7  christos 		/*
   2621  1.7  christos 		 * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
   2622  1.7  christos 		 */
   2623  1.7  christos 		if (fd == PCAP_ERROR_NO_SUCH_DEVICE) {
   2624  1.7  christos 			/*
   2625  1.7  christos 			 * Yes, so we can't bind to this because it's
   2626  1.7  christos 			 * not something supported by BPF.
   2627  1.7  christos 			 */
   2628  1.7  christos 			return (0);
   2629  1.7  christos 		}
   2630  1.7  christos 		/*
   2631  1.7  christos 		 * No, so we don't know whether it's supported or not;
   2632  1.7  christos 		 * say it is, so that the user can at least try to
   2633  1.7  christos 		 * open it and report the error (which is probably
   2634  1.7  christos 		 * "you don't have permission to open BPF devices";
   2635  1.7  christos 		 * reporting those interfaces means users will ask
   2636  1.7  christos 		 * "why am I getting a permissions error when I try
   2637  1.7  christos 		 * to capture" rather than "why am I not seeing any
   2638  1.7  christos 		 * interfaces", making the underlying problem clearer).
   2639  1.7  christos 		 */
   2640  1.7  christos 		return (1);
   2641  1.7  christos 	}
   2642  1.7  christos 
   2643  1.7  christos 	/*
   2644  1.7  christos 	 * Success.
   2645  1.7  christos 	 */
   2646  1.7  christos 	close(fd);
   2647  1.7  christos 	return (1);
   2648  1.7  christos }
   2649  1.7  christos 
   2650  1.7  christos #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
   2651  1.7  christos static int
   2652  1.8  christos get_usb_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
   2653  1.8  christos {
   2654  1.8  christos 	/*
   2655  1.8  christos 	 * XXX - if there's a way to determine whether there's something
   2656  1.8  christos 	 * plugged into a given USB bus, use that to determine whether
   2657  1.8  christos 	 * this device is "connected" or not.
   2658  1.8  christos 	 */
   2659  1.8  christos 	return (0);
   2660  1.8  christos }
   2661  1.8  christos 
   2662  1.8  christos static int
   2663  1.8  christos finddevs_usb(pcap_if_list_t *devlistp, char *errbuf)
   2664  1.7  christos {
   2665  1.7  christos 	DIR *usbdir;
   2666  1.7  christos 	struct dirent *usbitem;
   2667  1.7  christos 	size_t name_max;
   2668  1.7  christos 	char *name;
   2669  1.7  christos 
   2670  1.7  christos 	/*
   2671  1.7  christos 	 * We might have USB sniffing support, so try looking for USB
   2672  1.7  christos 	 * interfaces.
   2673  1.7  christos 	 *
   2674  1.7  christos 	 * We want to report a usbusN device for each USB bus, but
   2675  1.7  christos 	 * usbusN interfaces might, or might not, exist for them -
   2676  1.7  christos 	 * we create one if there isn't already one.
   2677  1.7  christos 	 *
   2678  1.7  christos 	 * So, instead, we look in /dev/usb for all buses and create
   2679  1.7  christos 	 * a "usbusN" device for each one.
   2680  1.7  christos 	 */
   2681  1.7  christos 	usbdir = opendir("/dev/usb");
   2682  1.7  christos 	if (usbdir == NULL) {
   2683  1.7  christos 		/*
   2684  1.7  christos 		 * Just punt.
   2685  1.7  christos 		 */
   2686  1.7  christos 		return (0);
   2687  1.7  christos 	}
   2688  1.7  christos 
   2689  1.7  christos 	/*
   2690  1.7  christos 	 * Leave enough room for a 32-bit (10-digit) bus number.
   2691  1.7  christos 	 * Yes, that's overkill, but we won't be using
   2692  1.7  christos 	 * the buffer very long.
   2693  1.7  christos 	 */
   2694  1.7  christos 	name_max = USBUS_PREFIX_LEN + 10 + 1;
   2695  1.7  christos 	name = malloc(name_max);
   2696  1.7  christos 	if (name == NULL) {
   2697  1.7  christos 		closedir(usbdir);
   2698  1.7  christos 		return (0);
   2699  1.7  christos 	}
   2700  1.7  christos 	while ((usbitem = readdir(usbdir)) != NULL) {
   2701  1.7  christos 		char *p;
   2702  1.7  christos 		size_t busnumlen;
   2703  1.7  christos 
   2704  1.7  christos 		if (strcmp(usbitem->d_name, ".") == 0 ||
   2705  1.7  christos 		    strcmp(usbitem->d_name, "..") == 0) {
   2706  1.7  christos 			/*
   2707  1.7  christos 			 * Ignore these.
   2708  1.7  christos 			 */
   2709  1.7  christos 			continue;
   2710  1.7  christos 		}
   2711  1.7  christos 		p = strchr(usbitem->d_name, '.');
   2712  1.7  christos 		if (p == NULL)
   2713  1.7  christos 			continue;
   2714  1.7  christos 		busnumlen = p - usbitem->d_name;
   2715  1.7  christos 		memcpy(name, usbus_prefix, USBUS_PREFIX_LEN);
   2716  1.7  christos 		memcpy(name + USBUS_PREFIX_LEN, usbitem->d_name, busnumlen);
   2717  1.7  christos 		*(name + USBUS_PREFIX_LEN + busnumlen) = '\0';
   2718  1.8  christos 		/*
   2719  1.8  christos 		 * There's an entry in this directory for every USB device,
   2720  1.8  christos 		 * not for every bus; if there's more than one device on
   2721  1.8  christos 		 * the bus, there'll be more than one entry for that bus,
   2722  1.8  christos 		 * so we need to avoid adding multiple capture devices
   2723  1.8  christos 		 * for each bus.
   2724  1.8  christos 		 */
   2725  1.8  christos 		if (find_or_add_dev(devlistp, name, PCAP_IF_UP,
   2726  1.8  christos 		    get_usb_if_flags, NULL, errbuf) == NULL) {
   2727  1.7  christos 			free(name);
   2728  1.7  christos 			closedir(usbdir);
   2729  1.8  christos 			return (PCAP_ERROR);
   2730  1.7  christos 		}
   2731  1.7  christos 	}
   2732  1.7  christos 	free(name);
   2733  1.7  christos 	closedir(usbdir);
   2734  1.7  christos 	return (0);
   2735  1.7  christos }
   2736  1.7  christos #endif
   2737  1.7  christos 
   2738  1.8  christos /*
   2739  1.8  christos  * Get additional flags for a device, using SIOCGIFMEDIA.
   2740  1.8  christos  */
   2741  1.8  christos #ifdef SIOCGIFMEDIA
   2742  1.8  christos static int
   2743  1.8  christos get_if_flags(const char *name, bpf_u_int32 *flags, char *errbuf)
   2744  1.8  christos {
   2745  1.8  christos 	int sock;
   2746  1.8  christos 	struct ifmediareq req;
   2747  1.8  christos 
   2748  1.8  christos 	sock = socket(AF_INET, SOCK_DGRAM, 0);
   2749  1.8  christos 	if (sock == -1) {
   2750  1.8  christos 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno,
   2751  1.8  christos 		    "Can't create socket to get media information for %s",
   2752  1.8  christos 		    name);
   2753  1.8  christos 		return (-1);
   2754  1.8  christos 	}
   2755  1.8  christos 	memset(&req, 0, sizeof(req));
   2756  1.8  christos 	strncpy(req.ifm_name, name, sizeof(req.ifm_name));
   2757  1.8  christos 	if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
   2758  1.8  christos 		if (errno == EOPNOTSUPP || errno == EINVAL || errno == ENOTTY ||
   2759  1.8  christos 		    errno == ENODEV) {
   2760  1.8  christos 			/*
   2761  1.8  christos 			 * Not supported, so we can't provide any
   2762  1.8  christos 			 * additional information.  Assume that
   2763  1.8  christos 			 * this means that "connected" vs.
   2764  1.8  christos 			 * "disconnected" doesn't apply.
   2765  1.8  christos 			 */
   2766  1.8  christos 			*flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
   2767  1.8  christos 			close(sock);
   2768  1.8  christos 			return (0);
   2769  1.8  christos 		}
   2770  1.8  christos 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno,
   2771  1.8  christos 		    "SIOCGIFMEDIA on %s failed", name);
   2772  1.8  christos 		close(sock);
   2773  1.8  christos 		return (-1);
   2774  1.8  christos 	}
   2775  1.8  christos 	close(sock);
   2776  1.8  christos 
   2777  1.8  christos 	/*
   2778  1.8  christos 	 * OK, what type of network is this?
   2779  1.8  christos 	 */
   2780  1.8  christos 	switch (IFM_TYPE(req.ifm_active)) {
   2781  1.8  christos 
   2782  1.8  christos 	case IFM_IEEE80211:
   2783  1.8  christos 		/*
   2784  1.8  christos 		 * Wireless.
   2785  1.8  christos 		 */
   2786  1.8  christos 		*flags |= PCAP_IF_WIRELESS;
   2787  1.8  christos 		break;
   2788  1.8  christos 	}
   2789  1.8  christos 
   2790  1.8  christos 	/*
   2791  1.8  christos 	 * Do we know whether it's connected?
   2792  1.8  christos 	 */
   2793  1.8  christos 	if (req.ifm_status & IFM_AVALID) {
   2794  1.8  christos 		/*
   2795  1.8  christos 		 * Yes.
   2796  1.8  christos 		 */
   2797  1.8  christos 		if (req.ifm_status & IFM_ACTIVE) {
   2798  1.8  christos 			/*
   2799  1.8  christos 			 * It's connected.
   2800  1.8  christos 			 */
   2801  1.8  christos 			*flags |= PCAP_IF_CONNECTION_STATUS_CONNECTED;
   2802  1.8  christos 		} else {
   2803  1.8  christos 			/*
   2804  1.8  christos 			 * It's disconnected.
   2805  1.8  christos 			 */
   2806  1.8  christos 			*flags |= PCAP_IF_CONNECTION_STATUS_DISCONNECTED;
   2807  1.8  christos 		}
   2808  1.8  christos 	}
   2809  1.8  christos 	return (0);
   2810  1.8  christos }
   2811  1.8  christos #else
   2812  1.8  christos static int
   2813  1.8  christos get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
   2814  1.8  christos {
   2815  1.8  christos 	/*
   2816  1.8  christos 	 * Nothing we can do other than mark loopback devices as "the
   2817  1.8  christos 	 * connected/disconnected status doesn't apply".
   2818  1.8  christos 	 *
   2819  1.8  christos 	 * XXX - on Solaris, can we do what the dladm command does,
   2820  1.8  christos 	 * i.e. get a connected/disconnected indication from a kstat?
   2821  1.8  christos 	 * (Note that you can also get the link speed, and possibly
   2822  1.8  christos 	 * other information, from a kstat as well.)
   2823  1.8  christos 	 */
   2824  1.8  christos 	if (*flags & PCAP_IF_LOOPBACK) {
   2825  1.8  christos 		/*
   2826  1.8  christos 		 * Loopback devices aren't wireless, and "connected"/
   2827  1.8  christos 		 * "disconnected" doesn't apply to them.
   2828  1.8  christos 		 */
   2829  1.8  christos 		*flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
   2830  1.8  christos 		return (0);
   2831  1.8  christos 	}
   2832  1.8  christos 	return (0);
   2833  1.8  christos }
   2834  1.8  christos #endif
   2835  1.8  christos 
   2836  1.1  christos int
   2837  1.8  christos pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
   2838  1.1  christos {
   2839  1.7  christos 	/*
   2840  1.7  christos 	 * Get the list of regular interfaces first.
   2841  1.7  christos 	 */
   2842  1.8  christos 	if (pcap_findalldevs_interfaces(devlistp, errbuf, check_bpf_bindable,
   2843  1.8  christos 	    get_if_flags) == -1)
   2844  1.7  christos 		return (-1);	/* failure */
   2845  1.7  christos 
   2846  1.7  christos #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
   2847  1.8  christos 	if (finddevs_usb(devlistp, errbuf) == -1)
   2848  1.7  christos 		return (-1);
   2849  1.7  christos #endif
   2850  1.7  christos 
   2851  1.1  christos 	return (0);
   2852  1.1  christos }
   2853  1.1  christos 
   2854  1.1  christos #ifdef HAVE_BSD_IEEE80211
   2855  1.1  christos static int
   2856  1.1  christos monitor_mode(pcap_t *p, int set)
   2857  1.1  christos {
   2858  1.4  christos 	struct pcap_bpf *pb = p->priv;
   2859  1.1  christos 	int sock;
   2860  1.1  christos 	struct ifmediareq req;
   2861  1.7  christos 	IFM_ULIST_TYPE *media_list;
   2862  1.1  christos 	int i;
   2863  1.1  christos 	int can_do;
   2864  1.1  christos 	struct ifreq ifr;
   2865  1.1  christos 
   2866  1.1  christos 	sock = socket(AF_INET, SOCK_DGRAM, 0);
   2867  1.1  christos 	if (sock == -1) {
   2868  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   2869  1.8  christos 		    errno, "can't open socket");
   2870  1.1  christos 		return (PCAP_ERROR);
   2871  1.1  christos 	}
   2872  1.1  christos 
   2873  1.1  christos 	memset(&req, 0, sizeof req);
   2874  1.7  christos 	strncpy(req.ifm_name, p->opt.device, sizeof req.ifm_name);
   2875  1.1  christos 
   2876  1.1  christos 	/*
   2877  1.1  christos 	 * Find out how many media types we have.
   2878  1.1  christos 	 */
   2879  1.1  christos 	if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
   2880  1.1  christos 		/*
   2881  1.1  christos 		 * Can't get the media types.
   2882  1.1  christos 		 */
   2883  1.3  christos 		switch (errno) {
   2884  1.3  christos 
   2885  1.3  christos 		case ENXIO:
   2886  1.3  christos 			/*
   2887  1.3  christos 			 * There's no such device.
   2888  1.3  christos 			 */
   2889  1.3  christos 			close(sock);
   2890  1.3  christos 			return (PCAP_ERROR_NO_SUCH_DEVICE);
   2891  1.3  christos 
   2892  1.3  christos 		case EINVAL:
   2893  1.1  christos 			/*
   2894  1.1  christos 			 * Interface doesn't support SIOC{G,S}IFMEDIA.
   2895  1.1  christos 			 */
   2896  1.1  christos 			close(sock);
   2897  1.1  christos 			return (PCAP_ERROR_RFMON_NOTSUP);
   2898  1.3  christos 
   2899  1.3  christos 		default:
   2900  1.8  christos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   2901  1.8  christos 			    errno, "SIOCGIFMEDIA 1");
   2902  1.3  christos 			close(sock);
   2903  1.3  christos 			return (PCAP_ERROR);
   2904  1.1  christos 		}
   2905  1.1  christos 	}
   2906  1.1  christos 	if (req.ifm_count == 0) {
   2907  1.1  christos 		/*
   2908  1.1  christos 		 * No media types.
   2909  1.1  christos 		 */
   2910  1.1  christos 		close(sock);
   2911  1.1  christos 		return (PCAP_ERROR_RFMON_NOTSUP);
   2912  1.1  christos 	}
   2913  1.1  christos 
   2914  1.1  christos 	/*
   2915  1.1  christos 	 * Allocate a buffer to hold all the media types, and
   2916  1.1  christos 	 * get the media types.
   2917  1.1  christos 	 */
   2918  1.7  christos 	media_list = malloc(req.ifm_count * sizeof(*media_list));
   2919  1.1  christos 	if (media_list == NULL) {
   2920  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   2921  1.8  christos 		    errno, "malloc");
   2922  1.1  christos 		close(sock);
   2923  1.1  christos 		return (PCAP_ERROR);
   2924  1.1  christos 	}
   2925  1.1  christos 	req.ifm_ulist = media_list;
   2926  1.1  christos 	if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
   2927  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   2928  1.8  christos 		    errno, "SIOCGIFMEDIA");
   2929  1.1  christos 		free(media_list);
   2930  1.1  christos 		close(sock);
   2931  1.1  christos 		return (PCAP_ERROR);
   2932  1.1  christos 	}
   2933  1.1  christos 
   2934  1.1  christos 	/*
   2935  1.1  christos 	 * Look for an 802.11 "automatic" media type.
   2936  1.1  christos 	 * We assume that all 802.11 adapters have that media type,
   2937  1.1  christos 	 * and that it will carry the monitor mode supported flag.
   2938  1.1  christos 	 */
   2939  1.1  christos 	can_do = 0;
   2940  1.1  christos 	for (i = 0; i < req.ifm_count; i++) {
   2941  1.1  christos 		if (IFM_TYPE(media_list[i]) == IFM_IEEE80211
   2942  1.1  christos 		    && IFM_SUBTYPE(media_list[i]) == IFM_AUTO) {
   2943  1.1  christos 			/* OK, does it do monitor mode? */
   2944  1.1  christos 			if (media_list[i] & IFM_IEEE80211_MONITOR) {
   2945  1.1  christos 				can_do = 1;
   2946  1.1  christos 				break;
   2947  1.1  christos 			}
   2948  1.1  christos 		}
   2949  1.1  christos 	}
   2950  1.1  christos 	free(media_list);
   2951  1.1  christos 	if (!can_do) {
   2952  1.1  christos 		/*
   2953  1.1  christos 		 * This adapter doesn't support monitor mode.
   2954  1.1  christos 		 */
   2955  1.1  christos 		close(sock);
   2956  1.1  christos 		return (PCAP_ERROR_RFMON_NOTSUP);
   2957  1.1  christos 	}
   2958  1.1  christos 
   2959  1.1  christos 	if (set) {
   2960  1.1  christos 		/*
   2961  1.1  christos 		 * Don't just check whether we can enable monitor mode,
   2962  1.1  christos 		 * do so, if it's not already enabled.
   2963  1.1  christos 		 */
   2964  1.1  christos 		if ((req.ifm_current & IFM_IEEE80211_MONITOR) == 0) {
   2965  1.1  christos 			/*
   2966  1.1  christos 			 * Monitor mode isn't currently on, so turn it on,
   2967  1.1  christos 			 * and remember that we should turn it off when the
   2968  1.1  christos 			 * pcap_t is closed.
   2969  1.1  christos 			 */
   2970  1.1  christos 
   2971  1.1  christos 			/*
   2972  1.1  christos 			 * If we haven't already done so, arrange to have
   2973  1.1  christos 			 * "pcap_close_all()" called when we exit.
   2974  1.1  christos 			 */
   2975  1.1  christos 			if (!pcap_do_addexit(p)) {
   2976  1.1  christos 				/*
   2977  1.1  christos 				 * "atexit()" failed; don't put the interface
   2978  1.1  christos 				 * in monitor mode, just give up.
   2979  1.1  christos 				 */
   2980  1.1  christos 				close(sock);
   2981  1.1  christos 				return (PCAP_ERROR);
   2982  1.1  christos 			}
   2983  1.1  christos 			memset(&ifr, 0, sizeof(ifr));
   2984  1.7  christos 			(void)strncpy(ifr.ifr_name, p->opt.device,
   2985  1.1  christos 			    sizeof(ifr.ifr_name));
   2986  1.1  christos 			ifr.ifr_media = req.ifm_current | IFM_IEEE80211_MONITOR;
   2987  1.1  christos 			if (ioctl(sock, SIOCSIFMEDIA, &ifr) == -1) {
   2988  1.8  christos 				pcap_fmt_errmsg_for_errno(p->errbuf,
   2989  1.8  christos 				    PCAP_ERRBUF_SIZE, errno, "SIOCSIFMEDIA");
   2990  1.1  christos 				close(sock);
   2991  1.1  christos 				return (PCAP_ERROR);
   2992  1.1  christos 			}
   2993  1.1  christos 
   2994  1.4  christos 			pb->must_do_on_close |= MUST_CLEAR_RFMON;
   2995  1.1  christos 
   2996  1.1  christos 			/*
   2997  1.1  christos 			 * Add this to the list of pcaps to close when we exit.
   2998  1.1  christos 			 */
   2999  1.1  christos 			pcap_add_to_pcaps_to_close(p);
   3000  1.1  christos 		}
   3001  1.1  christos 	}
   3002  1.1  christos 	return (0);
   3003  1.1  christos }
   3004  1.1  christos #endif /* HAVE_BSD_IEEE80211 */
   3005  1.1  christos 
   3006  1.1  christos #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
   3007  1.1  christos /*
   3008  1.1  christos  * Check whether we have any 802.11 link-layer types; return the best
   3009  1.1  christos  * of the 802.11 link-layer types if we find one, and return -1
   3010  1.1  christos  * otherwise.
   3011  1.1  christos  *
   3012  1.1  christos  * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
   3013  1.1  christos  * best 802.11 link-layer type; any of the other 802.11-plus-radio
   3014  1.1  christos  * headers are second-best; 802.11 with no radio information is
   3015  1.1  christos  * the least good.
   3016  1.1  christos  */
   3017  1.1  christos static int
   3018  1.1  christos find_802_11(struct bpf_dltlist *bdlp)
   3019  1.1  christos {
   3020  1.1  christos 	int new_dlt;
   3021  1.2  christos 	u_int i;
   3022  1.1  christos 
   3023  1.1  christos 	/*
   3024  1.1  christos 	 * Scan the list of DLT_ values, looking for 802.11 values,
   3025  1.1  christos 	 * and, if we find any, choose the best of them.
   3026  1.1  christos 	 */
   3027  1.1  christos 	new_dlt = -1;
   3028  1.1  christos 	for (i = 0; i < bdlp->bfl_len; i++) {
   3029  1.1  christos 		switch (bdlp->bfl_list[i]) {
   3030  1.1  christos 
   3031  1.1  christos 		case DLT_IEEE802_11:
   3032  1.1  christos 			/*
   3033  1.1  christos 			 * 802.11, but no radio.
   3034  1.1  christos 			 *
   3035  1.1  christos 			 * Offer this, and select it as the new mode
   3036  1.1  christos 			 * unless we've already found an 802.11
   3037  1.1  christos 			 * header with radio information.
   3038  1.1  christos 			 */
   3039  1.1  christos 			if (new_dlt == -1)
   3040  1.1  christos 				new_dlt = bdlp->bfl_list[i];
   3041  1.1  christos 			break;
   3042  1.1  christos 
   3043  1.1  christos 		case DLT_PRISM_HEADER:
   3044  1.1  christos 		case DLT_AIRONET_HEADER:
   3045  1.1  christos 		case DLT_IEEE802_11_RADIO_AVS:
   3046  1.1  christos 			/*
   3047  1.1  christos 			 * 802.11 with radio, but not radiotap.
   3048  1.1  christos 			 *
   3049  1.1  christos 			 * Offer this, and select it as the new mode
   3050  1.1  christos 			 * unless we've already found the radiotap DLT_.
   3051  1.1  christos 			 */
   3052  1.1  christos 			if (new_dlt != DLT_IEEE802_11_RADIO)
   3053  1.1  christos 				new_dlt = bdlp->bfl_list[i];
   3054  1.1  christos 			break;
   3055  1.1  christos 
   3056  1.1  christos 		case DLT_IEEE802_11_RADIO:
   3057  1.1  christos 			/*
   3058  1.1  christos 			 * 802.11 with radiotap.
   3059  1.1  christos 			 *
   3060  1.1  christos 			 * Offer this, and select it as the new mode.
   3061  1.1  christos 			 */
   3062  1.1  christos 			new_dlt = bdlp->bfl_list[i];
   3063  1.1  christos 			break;
   3064  1.1  christos 
   3065  1.1  christos 		default:
   3066  1.1  christos 			/*
   3067  1.1  christos 			 * Not 802.11.
   3068  1.1  christos 			 */
   3069  1.1  christos 			break;
   3070  1.1  christos 		}
   3071  1.1  christos 	}
   3072  1.1  christos 
   3073  1.1  christos 	return (new_dlt);
   3074  1.1  christos }
   3075  1.1  christos #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
   3076  1.1  christos 
   3077  1.1  christos #if defined(__APPLE__) && defined(BIOCGDLTLIST)
   3078  1.1  christos /*
   3079  1.1  christos  * Remove DLT_EN10MB from the list of DLT_ values, as we're in monitor mode,
   3080  1.1  christos  * and DLT_EN10MB isn't supported in monitor mode.
   3081  1.1  christos  */
   3082  1.1  christos static void
   3083  1.1  christos remove_en(pcap_t *p)
   3084  1.1  christos {
   3085  1.1  christos 	int i, j;
   3086  1.1  christos 
   3087  1.1  christos 	/*
   3088  1.1  christos 	 * Scan the list of DLT_ values and discard DLT_EN10MB.
   3089  1.1  christos 	 */
   3090  1.1  christos 	j = 0;
   3091  1.1  christos 	for (i = 0; i < p->dlt_count; i++) {
   3092  1.1  christos 		switch (p->dlt_list[i]) {
   3093  1.1  christos 
   3094  1.1  christos 		case DLT_EN10MB:
   3095  1.1  christos 			/*
   3096  1.1  christos 			 * Don't offer this one.
   3097  1.1  christos 			 */
   3098  1.1  christos 			continue;
   3099  1.1  christos 
   3100  1.1  christos 		default:
   3101  1.1  christos 			/*
   3102  1.1  christos 			 * Just copy this mode over.
   3103  1.1  christos 			 */
   3104  1.1  christos 			break;
   3105  1.1  christos 		}
   3106  1.1  christos 
   3107  1.1  christos 		/*
   3108  1.1  christos 		 * Copy this DLT_ value to its new position.
   3109  1.1  christos 		 */
   3110  1.1  christos 		p->dlt_list[j] = p->dlt_list[i];
   3111  1.1  christos 		j++;
   3112  1.1  christos 	}
   3113  1.1  christos 
   3114  1.1  christos 	/*
   3115  1.1  christos 	 * Set the DLT_ count to the number of entries we copied.
   3116  1.1  christos 	 */
   3117  1.1  christos 	p->dlt_count = j;
   3118  1.1  christos }
   3119  1.1  christos 
   3120  1.1  christos /*
   3121  1.1  christos  * Remove 802.11 link-layer types from the list of DLT_ values, as
   3122  1.1  christos  * we're not in monitor mode, and those DLT_ values will switch us
   3123  1.1  christos  * to monitor mode.
   3124  1.1  christos  */
   3125  1.1  christos static void
   3126  1.1  christos remove_802_11(pcap_t *p)
   3127  1.1  christos {
   3128  1.1  christos 	int i, j;
   3129  1.1  christos 
   3130  1.1  christos 	/*
   3131  1.1  christos 	 * Scan the list of DLT_ values and discard 802.11 values.
   3132  1.1  christos 	 */
   3133  1.1  christos 	j = 0;
   3134  1.1  christos 	for (i = 0; i < p->dlt_count; i++) {
   3135  1.1  christos 		switch (p->dlt_list[i]) {
   3136  1.1  christos 
   3137  1.1  christos 		case DLT_IEEE802_11:
   3138  1.1  christos 		case DLT_PRISM_HEADER:
   3139  1.1  christos 		case DLT_AIRONET_HEADER:
   3140  1.1  christos 		case DLT_IEEE802_11_RADIO:
   3141  1.1  christos 		case DLT_IEEE802_11_RADIO_AVS:
   3142  1.1  christos 			/*
   3143  1.1  christos 			 * 802.11.  Don't offer this one.
   3144  1.1  christos 			 */
   3145  1.1  christos 			continue;
   3146  1.1  christos 
   3147  1.1  christos 		default:
   3148  1.1  christos 			/*
   3149  1.1  christos 			 * Just copy this mode over.
   3150  1.1  christos 			 */
   3151  1.1  christos 			break;
   3152  1.1  christos 		}
   3153  1.1  christos 
   3154  1.1  christos 		/*
   3155  1.1  christos 		 * Copy this DLT_ value to its new position.
   3156  1.1  christos 		 */
   3157  1.1  christos 		p->dlt_list[j] = p->dlt_list[i];
   3158  1.1  christos 		j++;
   3159  1.1  christos 	}
   3160  1.1  christos 
   3161  1.1  christos 	/*
   3162  1.1  christos 	 * Set the DLT_ count to the number of entries we copied.
   3163  1.1  christos 	 */
   3164  1.1  christos 	p->dlt_count = j;
   3165  1.1  christos }
   3166  1.1  christos #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
   3167  1.1  christos 
   3168  1.1  christos static int
   3169  1.1  christos pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp)
   3170  1.1  christos {
   3171  1.4  christos 	struct pcap_bpf *pb = p->priv;
   3172  1.4  christos 
   3173  1.1  christos 	/*
   3174  1.1  christos 	 * Free any user-mode filter we might happen to have installed.
   3175  1.1  christos 	 */
   3176  1.1  christos 	pcap_freecode(&p->fcode);
   3177  1.1  christos 
   3178  1.1  christos 	/*
   3179  1.1  christos 	 * Try to install the kernel filter.
   3180  1.1  christos 	 */
   3181  1.1  christos 	if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) == 0) {
   3182  1.1  christos 		/*
   3183  1.1  christos 		 * It worked.
   3184  1.1  christos 		 */
   3185  1.4  christos 		pb->filtering_in_kernel = 1;	/* filtering in the kernel */
   3186  1.1  christos 
   3187  1.1  christos 		/*
   3188  1.1  christos 		 * Discard any previously-received packets, as they might
   3189  1.1  christos 		 * have passed whatever filter was formerly in effect, but
   3190  1.1  christos 		 * might not pass this filter (BIOCSETF discards packets
   3191  1.1  christos 		 * buffered in the kernel, so you can lose packets in any
   3192  1.1  christos 		 * case).
   3193  1.1  christos 		 */
   3194  1.1  christos 		p->cc = 0;
   3195  1.1  christos 		return (0);
   3196  1.1  christos 	}
   3197  1.1  christos 
   3198  1.1  christos 	/*
   3199  1.1  christos 	 * We failed.
   3200  1.1  christos 	 *
   3201  1.1  christos 	 * If it failed with EINVAL, that's probably because the program
   3202  1.1  christos 	 * is invalid or too big.  Validate it ourselves; if we like it
   3203  1.1  christos 	 * (we currently allow backward branches, to support protochain),
   3204  1.1  christos 	 * run it in userland.  (There's no notion of "too big" for
   3205  1.1  christos 	 * userland.)
   3206  1.1  christos 	 *
   3207  1.1  christos 	 * Otherwise, just give up.
   3208  1.1  christos 	 * XXX - if the copy of the program into the kernel failed,
   3209  1.1  christos 	 * we will get EINVAL rather than, say, EFAULT on at least
   3210  1.1  christos 	 * some kernels.
   3211  1.1  christos 	 */
   3212  1.1  christos 	if (errno != EINVAL) {
   3213  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
   3214  1.8  christos 		    errno, "BIOCSETF");
   3215  1.1  christos 		return (-1);
   3216  1.1  christos 	}
   3217  1.1  christos 
   3218  1.1  christos 	/*
   3219  1.1  christos 	 * install_bpf_program() validates the program.
   3220  1.1  christos 	 *
   3221  1.1  christos 	 * XXX - what if we already have a filter in the kernel?
   3222  1.1  christos 	 */
   3223  1.1  christos 	if (install_bpf_program(p, fp) < 0)
   3224  1.1  christos 		return (-1);
   3225  1.4  christos 	pb->filtering_in_kernel = 0;	/* filtering in userland */
   3226  1.1  christos 	return (0);
   3227  1.1  christos }
   3228  1.1  christos 
   3229  1.1  christos /*
   3230  1.1  christos  * Set direction flag: Which packets do we accept on a forwarding
   3231  1.1  christos  * single device? IN, OUT or both?
   3232  1.1  christos  */
   3233  1.1  christos static int
   3234  1.1  christos pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d)
   3235  1.1  christos {
   3236  1.1  christos #if defined(BIOCSDIRECTION)
   3237  1.1  christos 	u_int direction;
   3238  1.1  christos 
   3239  1.1  christos 	direction = (d == PCAP_D_IN) ? BPF_D_IN :
   3240  1.1  christos 	    ((d == PCAP_D_OUT) ? BPF_D_OUT : BPF_D_INOUT);
   3241  1.1  christos 	if (ioctl(p->fd, BIOCSDIRECTION, &direction) == -1) {
   3242  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
   3243  1.8  christos 		    errno, "Cannot set direction to %s",
   3244  1.1  christos 		        (d == PCAP_D_IN) ? "PCAP_D_IN" :
   3245  1.8  christos 			((d == PCAP_D_OUT) ? "PCAP_D_OUT" : "PCAP_D_INOUT"));
   3246  1.1  christos 		return (-1);
   3247  1.1  christos 	}
   3248  1.1  christos 	return (0);
   3249  1.1  christos #elif defined(BIOCSSEESENT)
   3250  1.1  christos 	u_int seesent;
   3251  1.1  christos 
   3252  1.1  christos 	/*
   3253  1.1  christos 	 * We don't support PCAP_D_OUT.
   3254  1.1  christos 	 */
   3255  1.1  christos 	if (d == PCAP_D_OUT) {
   3256  1.7  christos 		pcap_snprintf(p->errbuf, sizeof(p->errbuf),
   3257  1.1  christos 		    "Setting direction to PCAP_D_OUT is not supported on BPF");
   3258  1.1  christos 		return -1;
   3259  1.1  christos 	}
   3260  1.1  christos 
   3261  1.1  christos 	seesent = (d == PCAP_D_INOUT);
   3262  1.1  christos 	if (ioctl(p->fd, BIOCSSEESENT, &seesent) == -1) {
   3263  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
   3264  1.8  christos 		    errno, "Cannot set direction to %s",
   3265  1.8  christos 		    (d == PCAP_D_INOUT) ? "PCAP_D_INOUT" : "PCAP_D_IN");
   3266  1.1  christos 		return (-1);
   3267  1.1  christos 	}
   3268  1.1  christos 	return (0);
   3269  1.1  christos #else
   3270  1.7  christos 	(void) pcap_snprintf(p->errbuf, sizeof(p->errbuf),
   3271  1.1  christos 	    "This system doesn't support BIOCSSEESENT, so the direction can't be set");
   3272  1.1  christos 	return (-1);
   3273  1.1  christos #endif
   3274  1.1  christos }
   3275  1.1  christos 
   3276  1.1  christos static int
   3277  1.1  christos pcap_set_datalink_bpf(pcap_t *p, int dlt)
   3278  1.1  christos {
   3279  1.1  christos #ifdef BIOCSDLT
   3280  1.1  christos 	if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) {
   3281  1.8  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
   3282  1.8  christos 		    errno, "Cannot set DLT %d", dlt);
   3283  1.1  christos 		return (-1);
   3284  1.1  christos 	}
   3285  1.1  christos #endif
   3286  1.1  christos 	return (0);
   3287  1.1  christos }
   3288  1.8  christos 
   3289  1.8  christos /*
   3290  1.8  christos  * Platform-specific information.
   3291  1.8  christos  */
   3292  1.8  christos const char *
   3293  1.8  christos pcap_lib_version(void)
   3294  1.8  christos {
   3295  1.8  christos #ifdef HAVE_ZEROCOPY_BPF
   3296  1.8  christos 	return (PCAP_VERSION_STRING " (with zerocopy support)");
   3297  1.8  christos #else
   3298  1.8  christos 	return (PCAP_VERSION_STRING);
   3299  1.8  christos #endif
   3300  1.8  christos }
   3301