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