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