pcap-bpf.c revision 235426
117683Spst/*
239291Sfenner * Copyright (c) 1993, 1994, 1995, 1996, 1998
317683Spst *	The Regents of the University of California.  All rights reserved.
417683Spst *
517683Spst * Redistribution and use in source and binary forms, with or without
617683Spst * modification, are permitted provided that: (1) source code distributions
717683Spst * retain the above copyright notice and this paragraph in its entirety, (2)
817683Spst * distributions including binary code include the above copyright notice and
917683Spst * this paragraph in its entirety in the documentation or other materials
1017683Spst * provided with the distribution, and (3) all advertising materials mentioning
1117683Spst * features or use of this software display the following acknowledgement:
1217683Spst * ``This product includes software developed by the University of California,
1317683Spst * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1417683Spst * the University nor the names of its contributors may be used to endorse
1517683Spst * or promote products derived from this software without specific prior
1617683Spst * written permission.
1717683Spst * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1817683Spst * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1917683Spst * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20162020Ssam *
21162020Ssam * $FreeBSD: head/contrib/libpcap/pcap-bpf.c 235426 2012-05-14 05:12:56Z delphij $
2217683Spst */
2317683Spst#ifndef lint
24127664Sbmsstatic const char rcsid[] _U_ =
25214518Srpaulo    "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.116 2008-09-16 18:42:29 guy Exp $ (LBL)";
2617683Spst#endif
2717683Spst
2875107Sfenner#ifdef HAVE_CONFIG_H
2975107Sfenner#include "config.h"
3075107Sfenner#endif
3175107Sfenner
3217683Spst#include <sys/param.h>			/* optionally get BSD define */
33190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF
34183102Scsjp#include <sys/mman.h>
35190225Srpaulo#endif
3617683Spst#include <sys/socket.h>
37235426Sdelphij#include <time.h>
38214518Srpaulo/*
39214518Srpaulo * <net/bpf.h> defines ioctls, but doesn't include <sys/ioccom.h>.
40214518Srpaulo *
41214518Srpaulo * We include <sys/ioctl.h> as it might be necessary to declare ioctl();
42214518Srpaulo * at least on *BSD and Mac OS X, it also defines various SIOC ioctls -
43214518Srpaulo * we could include <sys/sockio.h>, but if we're already including
44214518Srpaulo * <sys/ioctl.h>, which includes <sys/sockio.h> on those platforms,
45214518Srpaulo * there's not much point in doing so.
46214518Srpaulo *
47214518Srpaulo * If we have <sys/ioccom.h>, we include it as well, to handle systems
48214518Srpaulo * such as Solaris which don't arrange to include <sys/ioccom.h> if you
49214518Srpaulo * include <sys/ioctl.h>
50214518Srpaulo */
5117683Spst#include <sys/ioctl.h>
52214518Srpaulo#ifdef HAVE_SYS_IOCCOM_H
53214518Srpaulo#include <sys/ioccom.h>
54214518Srpaulo#endif
55127664Sbms#include <sys/utsname.h>
5617683Spst
57190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF
58190225Srpaulo#include <machine/atomic.h>
59190225Srpaulo#endif
60190225Srpaulo
6117683Spst#include <net/if.h>
62127664Sbms
6398530Sfenner#ifdef _AIX
64127664Sbms
6598530Sfenner/*
66190225Srpaulo * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
67127664Sbms * native OS version, as we need "struct bpf_config" from it.
6898530Sfenner */
69127664Sbms#define PCAP_DONT_INCLUDE_PCAP_BPF_H
70127664Sbms
71127664Sbms#include <sys/types.h>
72127664Sbms
73127664Sbms/*
74127664Sbms * Prevent bpf.h from redefining the DLT_ values to their
75127664Sbms * IFT_ values, as we're going to return the standard libpcap
76127664Sbms * values, not IBM's non-standard IFT_ values.
77127664Sbms */
78127664Sbms#undef _AIX
79127664Sbms#include <net/bpf.h>
80127664Sbms#define _AIX
81127664Sbms
8298530Sfenner#include <net/if_types.h>		/* for IFT_ values */
83127664Sbms#include <sys/sysconfig.h>
84127664Sbms#include <sys/device.h>
85147894Ssam#include <sys/cfgodm.h>
86127664Sbms#include <cf.h>
8717683Spst
88127664Sbms#ifdef __64BIT__
89127664Sbms#define domakedev makedev64
90127664Sbms#define getmajor major64
91127664Sbms#define bpf_hdr bpf_hdr32
92127664Sbms#else /* __64BIT__ */
93127664Sbms#define domakedev makedev
94127664Sbms#define getmajor major
95127664Sbms#endif /* __64BIT__ */
96127664Sbms
97127664Sbms#define BPF_NAME "bpf"
98127664Sbms#define BPF_MINORS 4
99127664Sbms#define DRIVER_PATH "/usr/lib/drivers"
100127664Sbms#define BPF_NODE "/dev/bpf"
101127664Sbmsstatic int bpfloadedflag = 0;
102127664Sbmsstatic int odmlockid = 0;
103127664Sbms
104214518Srpaulostatic int bpf_load(char *errbuf);
105214518Srpaulo
106127664Sbms#else /* _AIX */
107127664Sbms
108127664Sbms#include <net/bpf.h>
109127664Sbms
110127664Sbms#endif /* _AIX */
111127664Sbms
11217683Spst#include <ctype.h>
113214518Srpaulo#include <fcntl.h>
11417683Spst#include <errno.h>
11517683Spst#include <netdb.h>
11617683Spst#include <stdio.h>
11717683Spst#include <stdlib.h>
11817683Spst#include <string.h>
11917683Spst#include <unistd.h>
12017683Spst
121190225Srpaulo#ifdef HAVE_NET_IF_MEDIA_H
122190225Srpaulo# include <net/if_media.h>
123190225Srpaulo#endif
124190225Srpaulo
12517683Spst#include "pcap-int.h"
12617683Spst
127127664Sbms#ifdef HAVE_DAG_API
128127664Sbms#include "pcap-dag.h"
129127664Sbms#endif /* HAVE_DAG_API */
130127664Sbms
131214518Srpaulo#ifdef HAVE_SNF_API
132214518Srpaulo#include "pcap-snf.h"
133214518Srpaulo#endif /* HAVE_SNF_API */
134214518Srpaulo
13517683Spst#ifdef HAVE_OS_PROTO_H
13617683Spst#include "os-proto.h"
13717683Spst#endif
13817683Spst
139190225Srpaulo#ifdef BIOCGDLTLIST
140190225Srpaulo# if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
141190225Srpaulo#define HAVE_BSD_IEEE80211
142190225Srpaulo# endif
14356889Sfenner
144190225Srpaulo# if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
145190225Srpaulostatic int find_802_11(struct bpf_dltlist *);
146190225Srpaulo
147190225Srpaulo#  ifdef HAVE_BSD_IEEE80211
148190225Srpaulostatic int monitor_mode(pcap_t *, int);
149190225Srpaulo#  endif
150190225Srpaulo
151190225Srpaulo#  if defined(__APPLE__)
152190225Srpaulostatic void remove_en(pcap_t *);
153190225Srpaulostatic void remove_802_11(pcap_t *);
154190225Srpaulo#  endif
155190225Srpaulo
156190225Srpaulo# endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
157190225Srpaulo
158190225Srpaulo#endif /* BIOCGDLTLIST */
159190225Srpaulo
160190225Srpaulo/*
161190225Srpaulo * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
162190225Srpaulo * don't get DLT_DOCSIS defined.
163190225Srpaulo */
164190225Srpaulo#ifndef DLT_DOCSIS
165190225Srpaulo#define DLT_DOCSIS	143
166190225Srpaulo#endif
167190225Srpaulo
168190225Srpaulo/*
169190225Srpaulo * On OS X, we don't even get any of the 802.11-plus-radio-header DLT_'s
170190225Srpaulo * defined, even though some of them are used by various Airport drivers.
171190225Srpaulo */
172190225Srpaulo#ifndef DLT_PRISM_HEADER
173190225Srpaulo#define DLT_PRISM_HEADER	119
174190225Srpaulo#endif
175190225Srpaulo#ifndef DLT_AIRONET_HEADER
176190225Srpaulo#define DLT_AIRONET_HEADER	120
177190225Srpaulo#endif
178190225Srpaulo#ifndef DLT_IEEE802_11_RADIO
179190225Srpaulo#define DLT_IEEE802_11_RADIO	127
180190225Srpaulo#endif
181190225Srpaulo#ifndef DLT_IEEE802_11_RADIO_AVS
182190225Srpaulo#define DLT_IEEE802_11_RADIO_AVS 163
183190225Srpaulo#endif
184190225Srpaulo
185190225Srpaulostatic int pcap_can_set_rfmon_bpf(pcap_t *p);
186190225Srpaulostatic int pcap_activate_bpf(pcap_t *p);
187127664Sbmsstatic int pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp);
188162012Ssamstatic int pcap_setdirection_bpf(pcap_t *, pcap_direction_t);
189127664Sbmsstatic int pcap_set_datalink_bpf(pcap_t *p, int dlt);
190127664Sbms
191190225Srpaulo/*
192235426Sdelphij * For zerocopy bpf, the setnonblock/getnonblock routines need to modify
193235426Sdelphij * p->md.timeout so we don't call select(2) if the pcap handle is in non-
194235426Sdelphij * blocking mode.  We preserve the timeout supplied by pcap_open functions
195235426Sdelphij * to make sure it does not get clobbered if the pcap handle moves between
196235426Sdelphij * blocking and non-blocking mode.
197190225Srpaulo */
198127664Sbmsstatic int
199235426Sdelphijpcap_getnonblock_bpf(pcap_t *p, char *errbuf)
200190225Srpaulo{
201235426Sdelphij#ifdef HAVE_ZEROCOPY_BPF
202235426Sdelphij	if (p->md.zerocopy) {
203235426Sdelphij		/*
204235426Sdelphij		 * Use a negative value for the timeout to represent that the
205235426Sdelphij		 * pcap handle is in non-blocking mode.
206235426Sdelphij		 */
207235426Sdelphij		return (p->md.timeout < 0);
208235426Sdelphij	}
209235426Sdelphij#endif
210235426Sdelphij	return (pcap_getnonblock_fd(p, errbuf));
211190225Srpaulo}
212190225Srpaulo
213190225Srpaulostatic int
214235426Sdelphijpcap_setnonblock_bpf(pcap_t *p, int nonblock, char *errbuf)
215190225Srpaulo{
216235426Sdelphij#ifdef HAVE_ZEROCOPY_BPF
217235426Sdelphij	if (p->md.zerocopy) {
218235426Sdelphij		/*
219235426Sdelphij		 * Map each value to the corresponding 2's complement, to
220235426Sdelphij		 * preserve the timeout value provided with pcap_set_timeout.
221235426Sdelphij		 * (from pcap-linux.c).
222235426Sdelphij		 */
223235426Sdelphij		if (nonblock) {
224235426Sdelphij			if (p->md.timeout >= 0) {
225235426Sdelphij				/*
226235426Sdelphij				 * Timeout is non-negative, so we're not
227235426Sdelphij				 * currently in non-blocking mode; set it
228235426Sdelphij				 * to the 2's complement, to make it
229235426Sdelphij				 * negative, as an indication that we're
230235426Sdelphij				 * in non-blocking mode.
231235426Sdelphij				 */
232235426Sdelphij				p->md.timeout = p->md.timeout * -1 - 1;
233235426Sdelphij			}
234235426Sdelphij		} else {
235235426Sdelphij			if (p->md.timeout < 0) {
236235426Sdelphij				/*
237235426Sdelphij				 * Timeout is negative, so we're currently
238235426Sdelphij				 * in blocking mode; reverse the previous
239235426Sdelphij				 * operation, to make the timeout non-negative
240235426Sdelphij				 * again.
241235426Sdelphij				 */
242235426Sdelphij				p->md.timeout = (p->md.timeout + 1) * -1;
243235426Sdelphij			}
244214518Srpaulo		}
245235426Sdelphij		return (0);
246214518Srpaulo	}
247235426Sdelphij#endif
248235426Sdelphij	return (pcap_setnonblock_fd(p, nonblock, errbuf));
249190225Srpaulo}
250190225Srpaulo
251235426Sdelphij#ifdef HAVE_ZEROCOPY_BPF
252190225Srpaulo/*
253190225Srpaulo * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
254190225Srpaulo * shared memory buffers.
255190225Srpaulo *
256190225Srpaulo * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer,
257190225Srpaulo * and set up p->buffer and cc to reflect one if available.  Notice that if
258190225Srpaulo * there was no prior buffer, we select zbuf1 as this will be the first
259190225Srpaulo * buffer filled for a fresh BPF session.
260190225Srpaulo */
261190225Srpaulostatic int
262190225Srpaulopcap_next_zbuf_shm(pcap_t *p, int *cc)
263190225Srpaulo{
264190225Srpaulo	struct bpf_zbuf_header *bzh;
265190225Srpaulo
266190225Srpaulo	if (p->md.zbuffer == p->md.zbuf2 || p->md.zbuffer == NULL) {
267190225Srpaulo		bzh = (struct bpf_zbuf_header *)p->md.zbuf1;
268190225Srpaulo		if (bzh->bzh_user_gen !=
269190225Srpaulo		    atomic_load_acq_int(&bzh->bzh_kernel_gen)) {
270190225Srpaulo			p->md.bzh = bzh;
271190225Srpaulo			p->md.zbuffer = (u_char *)p->md.zbuf1;
272190225Srpaulo			p->buffer = p->md.zbuffer + sizeof(*bzh);
273190225Srpaulo			*cc = bzh->bzh_kernel_len;
274190225Srpaulo			return (1);
275190225Srpaulo		}
276190225Srpaulo	} else if (p->md.zbuffer == p->md.zbuf1) {
277190225Srpaulo		bzh = (struct bpf_zbuf_header *)p->md.zbuf2;
278190225Srpaulo		if (bzh->bzh_user_gen !=
279190225Srpaulo		    atomic_load_acq_int(&bzh->bzh_kernel_gen)) {
280190225Srpaulo			p->md.bzh = bzh;
281190225Srpaulo			p->md.zbuffer = (u_char *)p->md.zbuf2;
282190225Srpaulo  			p->buffer = p->md.zbuffer + sizeof(*bzh);
283190225Srpaulo			*cc = bzh->bzh_kernel_len;
284190225Srpaulo			return (1);
285190225Srpaulo		}
286190225Srpaulo	}
287190225Srpaulo	*cc = 0;
288190225Srpaulo	return (0);
289190225Srpaulo}
290190225Srpaulo
291190225Srpaulo/*
292190225Srpaulo * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
293190225Srpaulo * select() for data or a timeout, and possibly force rotation of the buffer
294190225Srpaulo * in the event we time out or are in immediate mode.  Invoke the shared
295190225Srpaulo * memory check before doing system calls in order to avoid doing avoidable
296190225Srpaulo * work.
297190225Srpaulo */
298190225Srpaulostatic int
299190225Srpaulopcap_next_zbuf(pcap_t *p, int *cc)
300190225Srpaulo{
301190225Srpaulo	struct bpf_zbuf bz;
302190225Srpaulo	struct timeval tv;
303190225Srpaulo	struct timespec cur;
304190225Srpaulo	fd_set r_set;
305190225Srpaulo	int data, r;
306190225Srpaulo	int expire, tmout;
307190225Srpaulo
308190225Srpaulo#define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000))
309190225Srpaulo	/*
310190225Srpaulo	 * Start out by seeing whether anything is waiting by checking the
311190225Srpaulo	 * next shared memory buffer for data.
312190225Srpaulo	 */
313190225Srpaulo	data = pcap_next_zbuf_shm(p, cc);
314190225Srpaulo	if (data)
315190225Srpaulo		return (data);
316190225Srpaulo	/*
317190225Srpaulo	 * If a previous sleep was interrupted due to signal delivery, make
318190225Srpaulo	 * sure that the timeout gets adjusted accordingly.  This requires
319190225Srpaulo	 * that we analyze when the timeout should be been expired, and
320190225Srpaulo	 * subtract the current time from that.  If after this operation,
321190225Srpaulo	 * our timeout is less then or equal to zero, handle it like a
322190225Srpaulo	 * regular timeout.
323190225Srpaulo	 */
324190225Srpaulo	tmout = p->md.timeout;
325190225Srpaulo	if (tmout)
326190225Srpaulo		(void) clock_gettime(CLOCK_MONOTONIC, &cur);
327190225Srpaulo	if (p->md.interrupted && p->md.timeout) {
328190225Srpaulo		expire = TSTOMILLI(&p->md.firstsel) + p->md.timeout;
329190225Srpaulo		tmout = expire - TSTOMILLI(&cur);
330190225Srpaulo#undef TSTOMILLI
331190225Srpaulo		if (tmout <= 0) {
332190225Srpaulo			p->md.interrupted = 0;
333190225Srpaulo			data = pcap_next_zbuf_shm(p, cc);
334190225Srpaulo			if (data)
335190225Srpaulo				return (data);
336190225Srpaulo			if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) {
337190225Srpaulo				(void) snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
338190225Srpaulo				    "BIOCROTZBUF: %s", strerror(errno));
339190225Srpaulo				return (PCAP_ERROR);
340190225Srpaulo			}
341190225Srpaulo			return (pcap_next_zbuf_shm(p, cc));
342190225Srpaulo		}
343190225Srpaulo	}
344190225Srpaulo	/*
345190225Srpaulo	 * No data in the buffer, so must use select() to wait for data or
346190225Srpaulo	 * the next timeout.  Note that we only call select if the handle
347190225Srpaulo	 * is in blocking mode.
348190225Srpaulo	 */
349190225Srpaulo	if (p->md.timeout >= 0) {
350190225Srpaulo		FD_ZERO(&r_set);
351190225Srpaulo		FD_SET(p->fd, &r_set);
352190225Srpaulo		if (tmout != 0) {
353190225Srpaulo			tv.tv_sec = tmout / 1000;
354190225Srpaulo			tv.tv_usec = (tmout * 1000) % 1000000;
355190225Srpaulo		}
356190225Srpaulo		r = select(p->fd + 1, &r_set, NULL, NULL,
357190225Srpaulo		    p->md.timeout != 0 ? &tv : NULL);
358190225Srpaulo		if (r < 0 && errno == EINTR) {
359190225Srpaulo			if (!p->md.interrupted && p->md.timeout) {
360190225Srpaulo				p->md.interrupted = 1;
361190225Srpaulo				p->md.firstsel = cur;
362190225Srpaulo			}
363190225Srpaulo			return (0);
364190225Srpaulo		} else if (r < 0) {
365190225Srpaulo			(void) snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
366190225Srpaulo			    "select: %s", strerror(errno));
367190225Srpaulo			return (PCAP_ERROR);
368190225Srpaulo		}
369190225Srpaulo	}
370190225Srpaulo	p->md.interrupted = 0;
371190225Srpaulo	/*
372190225Srpaulo	 * Check again for data, which may exist now that we've either been
373190225Srpaulo	 * woken up as a result of data or timed out.  Try the "there's data"
374190225Srpaulo	 * case first since it doesn't require a system call.
375190225Srpaulo	 */
376190225Srpaulo	data = pcap_next_zbuf_shm(p, cc);
377190225Srpaulo	if (data)
378190225Srpaulo		return (data);
379190225Srpaulo	/*
380190225Srpaulo	 * Try forcing a buffer rotation to dislodge timed out or immediate
381190225Srpaulo	 * data.
382190225Srpaulo	 */
383190225Srpaulo	if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) {
384190225Srpaulo		(void) snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
385190225Srpaulo		    "BIOCROTZBUF: %s", strerror(errno));
386190225Srpaulo		return (PCAP_ERROR);
387190225Srpaulo	}
388190225Srpaulo	return (pcap_next_zbuf_shm(p, cc));
389190225Srpaulo}
390190225Srpaulo
391190225Srpaulo/*
392190225Srpaulo * Notify kernel that we are done with the buffer.  We don't reset zbuffer so
393190225Srpaulo * that we know which buffer to use next time around.
394190225Srpaulo */
395190225Srpaulostatic int
396190225Srpaulopcap_ack_zbuf(pcap_t *p)
397190225Srpaulo{
398190225Srpaulo
399190225Srpaulo	atomic_store_rel_int(&p->md.bzh->bzh_user_gen,
400190225Srpaulo	    p->md.bzh->bzh_kernel_gen);
401190225Srpaulo	p->md.bzh = NULL;
402190225Srpaulo	p->buffer = NULL;
403190225Srpaulo	return (0);
404190225Srpaulo}
405235426Sdelphij#endif /* HAVE_ZEROCOPY_BPF */
406190225Srpaulo
407190225Srpaulopcap_t *
408190225Srpaulopcap_create(const char *device, char *ebuf)
409190225Srpaulo{
410190225Srpaulo	pcap_t *p;
411190225Srpaulo
412190225Srpaulo#ifdef HAVE_DAG_API
413190225Srpaulo	if (strstr(device, "dag"))
414190225Srpaulo		return (dag_create(device, ebuf));
415190225Srpaulo#endif /* HAVE_DAG_API */
416214518Srpaulo#ifdef HAVE_SNF_API
417214518Srpaulo	if (strstr(device, "snf"))
418214518Srpaulo		return (snf_create(device, ebuf));
419214518Srpaulo#endif /* HAVE_SNF_API */
420190225Srpaulo
421190225Srpaulo	p = pcap_create_common(device, ebuf);
422190225Srpaulo	if (p == NULL)
423190225Srpaulo		return (NULL);
424190225Srpaulo
425190225Srpaulo	p->activate_op = pcap_activate_bpf;
426190225Srpaulo	p->can_set_rfmon_op = pcap_can_set_rfmon_bpf;
427190225Srpaulo	return (p);
428190225Srpaulo}
429190225Srpaulo
430235426Sdelphij/*
431235426Sdelphij * On success, returns a file descriptor for a BPF device.
432235426Sdelphij * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf.
433235426Sdelphij */
434190225Srpaulostatic int
435190225Srpaulobpf_open(pcap_t *p)
436190225Srpaulo{
437190225Srpaulo	int fd;
438190225Srpaulo#ifdef HAVE_CLONING_BPF
439190225Srpaulo	static const char device[] = "/dev/bpf";
440190225Srpaulo#else
441190225Srpaulo	int n = 0;
442190225Srpaulo	char device[sizeof "/dev/bpf0000000000"];
443190225Srpaulo#endif
444190225Srpaulo
445190225Srpaulo#ifdef _AIX
446190225Srpaulo	/*
447190225Srpaulo	 * Load the bpf driver, if it isn't already loaded,
448190225Srpaulo	 * and create the BPF device entries, if they don't
449190225Srpaulo	 * already exist.
450190225Srpaulo	 */
451190225Srpaulo	if (bpf_load(p->errbuf) == PCAP_ERROR)
452190225Srpaulo		return (PCAP_ERROR);
453190225Srpaulo#endif
454190225Srpaulo
455190225Srpaulo#ifdef HAVE_CLONING_BPF
456190225Srpaulo	if ((fd = open(device, O_RDWR)) == -1 &&
457190225Srpaulo	    (errno != EACCES || (fd = open(device, O_RDONLY)) == -1)) {
458190225Srpaulo		if (errno == EACCES)
459190225Srpaulo			fd = PCAP_ERROR_PERM_DENIED;
460190225Srpaulo		else
461190225Srpaulo			fd = PCAP_ERROR;
462190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
463190225Srpaulo		  "(cannot open device) %s: %s", device, pcap_strerror(errno));
464190225Srpaulo	}
465190225Srpaulo#else
466190225Srpaulo	/*
467190225Srpaulo	 * Go through all the minors and find one that isn't in use.
468190225Srpaulo	 */
469190225Srpaulo	do {
470190225Srpaulo		(void)snprintf(device, sizeof(device), "/dev/bpf%d", n++);
471190225Srpaulo		/*
472190225Srpaulo		 * Initially try a read/write open (to allow the inject
473190225Srpaulo		 * method to work).  If that fails due to permission
474190225Srpaulo		 * issues, fall back to read-only.  This allows a
475190225Srpaulo		 * non-root user to be granted specific access to pcap
476190225Srpaulo		 * capabilities via file permissions.
477190225Srpaulo		 *
478190225Srpaulo		 * XXX - we should have an API that has a flag that
479190225Srpaulo		 * controls whether to open read-only or read-write,
480190225Srpaulo		 * so that denial of permission to send (or inability
481190225Srpaulo		 * to send, if sending packets isn't supported on
482190225Srpaulo		 * the device in question) can be indicated at open
483190225Srpaulo		 * time.
484190225Srpaulo		 */
485190225Srpaulo		fd = open(device, O_RDWR);
486190225Srpaulo		if (fd == -1 && errno == EACCES)
487190225Srpaulo			fd = open(device, O_RDONLY);
488190225Srpaulo	} while (fd < 0 && errno == EBUSY);
489190225Srpaulo
490190225Srpaulo	/*
491190225Srpaulo	 * XXX better message for all minors used
492190225Srpaulo	 */
493190225Srpaulo	if (fd < 0) {
494235426Sdelphij		switch (errno) {
495235426Sdelphij
496235426Sdelphij		case ENOENT:
497235426Sdelphij			fd = PCAP_ERROR;
498235426Sdelphij			if (n == 1) {
499235426Sdelphij				/*
500235426Sdelphij				 * /dev/bpf0 doesn't exist, which
501235426Sdelphij				 * means we probably have no BPF
502235426Sdelphij				 * devices.
503235426Sdelphij				 */
504235426Sdelphij				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
505235426Sdelphij				    "(there are no BPF devices)");
506235426Sdelphij			} else {
507235426Sdelphij				/*
508235426Sdelphij				 * We got EBUSY on at least one
509235426Sdelphij				 * BPF device, so we have BPF
510235426Sdelphij				 * devices, but all the ones
511235426Sdelphij				 * that exist are busy.
512235426Sdelphij				 */
513235426Sdelphij				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
514235426Sdelphij				    "(all BPF devices are busy)");
515235426Sdelphij			}
516235426Sdelphij			break;
517235426Sdelphij
518235426Sdelphij		case EACCES:
519235426Sdelphij			/*
520235426Sdelphij			 * Got EACCES on the last device we tried,
521235426Sdelphij			 * and EBUSY on all devices before that,
522235426Sdelphij			 * if any.
523235426Sdelphij			 */
524190225Srpaulo			fd = PCAP_ERROR_PERM_DENIED;
525235426Sdelphij			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
526235426Sdelphij			    "(cannot open BPF device) %s: %s", device,
527235426Sdelphij			    pcap_strerror(errno));
528235426Sdelphij			break;
529235426Sdelphij
530235426Sdelphij		default:
531235426Sdelphij			/*
532235426Sdelphij			 * Some other problem.
533235426Sdelphij			 */
534190225Srpaulo			fd = PCAP_ERROR;
535235426Sdelphij			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
536235426Sdelphij			    "(cannot open BPF device) %s: %s", device,
537235426Sdelphij			    pcap_strerror(errno));
538235426Sdelphij			break;
539235426Sdelphij		}
540190225Srpaulo	}
541190225Srpaulo#endif
542190225Srpaulo
543190225Srpaulo	return (fd);
544190225Srpaulo}
545190225Srpaulo
546190225Srpaulo#ifdef BIOCGDLTLIST
547190225Srpaulostatic int
548190225Srpauloget_dlt_list(int fd, int v, struct bpf_dltlist *bdlp, char *ebuf)
549190225Srpaulo{
550190225Srpaulo	memset(bdlp, 0, sizeof(*bdlp));
551190225Srpaulo	if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) == 0) {
552190225Srpaulo		u_int i;
553190225Srpaulo		int is_ethernet;
554190225Srpaulo
555190225Srpaulo		bdlp->bfl_list = (u_int *) malloc(sizeof(u_int) * (bdlp->bfl_len + 1));
556190225Srpaulo		if (bdlp->bfl_list == NULL) {
557190225Srpaulo			(void)snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
558190225Srpaulo			    pcap_strerror(errno));
559190225Srpaulo			return (PCAP_ERROR);
560190225Srpaulo		}
561190225Srpaulo
562190225Srpaulo		if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) < 0) {
563190225Srpaulo			(void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
564190225Srpaulo			    "BIOCGDLTLIST: %s", pcap_strerror(errno));
565190225Srpaulo			free(bdlp->bfl_list);
566190225Srpaulo			return (PCAP_ERROR);
567190225Srpaulo		}
568190225Srpaulo
569190225Srpaulo		/*
570190225Srpaulo		 * OK, for real Ethernet devices, add DLT_DOCSIS to the
571190225Srpaulo		 * list, so that an application can let you choose it,
572190225Srpaulo		 * in case you're capturing DOCSIS traffic that a Cisco
573190225Srpaulo		 * Cable Modem Termination System is putting out onto
574190225Srpaulo		 * an Ethernet (it doesn't put an Ethernet header onto
575190225Srpaulo		 * the wire, it puts raw DOCSIS frames out on the wire
576190225Srpaulo		 * inside the low-level Ethernet framing).
577190225Srpaulo		 *
578190225Srpaulo		 * A "real Ethernet device" is defined here as a device
579190225Srpaulo		 * that has a link-layer type of DLT_EN10MB and that has
580190225Srpaulo		 * no alternate link-layer types; that's done to exclude
581190225Srpaulo		 * 802.11 interfaces (which might or might not be the
582190225Srpaulo		 * right thing to do, but I suspect it is - Ethernet <->
583190225Srpaulo		 * 802.11 bridges would probably badly mishandle frames
584190225Srpaulo		 * that don't have Ethernet headers).
585214518Srpaulo		 *
586214518Srpaulo		 * On Solaris with BPF, Ethernet devices also offer
587214518Srpaulo		 * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
588214518Srpaulo		 * treat it as an indication that the device isn't an
589214518Srpaulo		 * Ethernet.
590190225Srpaulo		 */
591190225Srpaulo		if (v == DLT_EN10MB) {
592190225Srpaulo			is_ethernet = 1;
593190225Srpaulo			for (i = 0; i < bdlp->bfl_len; i++) {
594214518Srpaulo				if (bdlp->bfl_list[i] != DLT_EN10MB
595214518Srpaulo#ifdef DLT_IPNET
596214518Srpaulo				    && bdlp->bfl_list[i] != DLT_IPNET
597214518Srpaulo#endif
598214518Srpaulo				    ) {
599190225Srpaulo					is_ethernet = 0;
600190225Srpaulo					break;
601190225Srpaulo				}
602190225Srpaulo			}
603190225Srpaulo			if (is_ethernet) {
604190225Srpaulo				/*
605190225Srpaulo				 * We reserved one more slot at the end of
606190225Srpaulo				 * the list.
607190225Srpaulo				 */
608190225Srpaulo				bdlp->bfl_list[bdlp->bfl_len] = DLT_DOCSIS;
609190225Srpaulo				bdlp->bfl_len++;
610190225Srpaulo			}
611190225Srpaulo		}
612190225Srpaulo	} else {
613190225Srpaulo		/*
614190225Srpaulo		 * EINVAL just means "we don't support this ioctl on
615190225Srpaulo		 * this device"; don't treat it as an error.
616190225Srpaulo		 */
617190225Srpaulo		if (errno != EINVAL) {
618190225Srpaulo			(void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
619190225Srpaulo			    "BIOCGDLTLIST: %s", pcap_strerror(errno));
620190225Srpaulo			return (PCAP_ERROR);
621190225Srpaulo		}
622190225Srpaulo	}
623190225Srpaulo	return (0);
624190225Srpaulo}
625190225Srpaulo#endif
626190225Srpaulo
627190225Srpaulostatic int
628190225Srpaulopcap_can_set_rfmon_bpf(pcap_t *p)
629190225Srpaulo{
630190225Srpaulo#if defined(__APPLE__)
631190225Srpaulo	struct utsname osinfo;
632190225Srpaulo	struct ifreq ifr;
633190225Srpaulo	int fd;
634190225Srpaulo#ifdef BIOCGDLTLIST
635190225Srpaulo	struct bpf_dltlist bdl;
636190225Srpaulo#endif
637190225Srpaulo
638190225Srpaulo	/*
639190225Srpaulo	 * The joys of monitor mode on OS X.
640190225Srpaulo	 *
641190225Srpaulo	 * Prior to 10.4, it's not supported at all.
642190225Srpaulo	 *
643190225Srpaulo	 * In 10.4, if adapter enN supports monitor mode, there's a
644190225Srpaulo	 * wltN adapter corresponding to it; you open it, instead of
645190225Srpaulo	 * enN, to get monitor mode.  You get whatever link-layer
646190225Srpaulo	 * headers it supplies.
647190225Srpaulo	 *
648190225Srpaulo	 * In 10.5, and, we assume, later releases, if adapter enN
649190225Srpaulo	 * supports monitor mode, it offers, among its selectable
650190225Srpaulo	 * DLT_ values, values that let you get the 802.11 header;
651190225Srpaulo	 * selecting one of those values puts the adapter into monitor
652190225Srpaulo	 * mode (i.e., you can't get 802.11 headers except in monitor
653190225Srpaulo	 * mode, and you can't get Ethernet headers in monitor mode).
654190225Srpaulo	 */
655190225Srpaulo	if (uname(&osinfo) == -1) {
656190225Srpaulo		/*
657190225Srpaulo		 * Can't get the OS version; just say "no".
658190225Srpaulo		 */
659190225Srpaulo		return (0);
660190225Srpaulo	}
661190225Srpaulo	/*
662190225Srpaulo	 * We assume osinfo.sysname is "Darwin", because
663190225Srpaulo	 * __APPLE__ is defined.  We just check the version.
664190225Srpaulo	 */
665190225Srpaulo	if (osinfo.release[0] < '8' && osinfo.release[1] == '.') {
666190225Srpaulo		/*
667190225Srpaulo		 * 10.3 (Darwin 7.x) or earlier.
668190225Srpaulo		 * Monitor mode not supported.
669190225Srpaulo		 */
670190225Srpaulo		return (0);
671190225Srpaulo	}
672190225Srpaulo	if (osinfo.release[0] == '8' && osinfo.release[1] == '.') {
673190225Srpaulo		/*
674190225Srpaulo		 * 10.4 (Darwin 8.x).  s/en/wlt/, and check
675190225Srpaulo		 * whether the device exists.
676190225Srpaulo		 */
677190225Srpaulo		if (strncmp(p->opt.source, "en", 2) != 0) {
678190225Srpaulo			/*
679190225Srpaulo			 * Not an enN device; no monitor mode.
680190225Srpaulo			 */
681190225Srpaulo			return (0);
682190225Srpaulo		}
683190225Srpaulo		fd = socket(AF_INET, SOCK_DGRAM, 0);
684190225Srpaulo		if (fd == -1) {
685190225Srpaulo			(void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
686190225Srpaulo			    "socket: %s", pcap_strerror(errno));
687190225Srpaulo			return (PCAP_ERROR);
688190225Srpaulo		}
689190225Srpaulo		strlcpy(ifr.ifr_name, "wlt", sizeof(ifr.ifr_name));
690190225Srpaulo		strlcat(ifr.ifr_name, p->opt.source + 2, sizeof(ifr.ifr_name));
691190225Srpaulo		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
692190225Srpaulo			/*
693190225Srpaulo			 * No such device?
694190225Srpaulo			 */
695190225Srpaulo			close(fd);
696190225Srpaulo			return (0);
697190225Srpaulo		}
698190225Srpaulo		close(fd);
699190225Srpaulo		return (1);
700190225Srpaulo	}
701190225Srpaulo
702190225Srpaulo#ifdef BIOCGDLTLIST
703190225Srpaulo	/*
704190225Srpaulo	 * Everything else is 10.5 or later; for those,
705190225Srpaulo	 * we just open the enN device, and check whether
706190225Srpaulo	 * we have any 802.11 devices.
707190225Srpaulo	 *
708190225Srpaulo	 * First, open a BPF device.
709190225Srpaulo	 */
710190225Srpaulo	fd = bpf_open(p);
711190225Srpaulo	if (fd < 0)
712235426Sdelphij		return (fd);	/* fd is the appropriate error code */
713190225Srpaulo
714190225Srpaulo	/*
715190225Srpaulo	 * Now bind to the device.
716190225Srpaulo	 */
717190225Srpaulo	(void)strncpy(ifr.ifr_name, p->opt.source, sizeof(ifr.ifr_name));
718190225Srpaulo	if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
719235426Sdelphij		switch (errno) {
720235426Sdelphij
721235426Sdelphij		case ENXIO:
722190225Srpaulo			/*
723235426Sdelphij			 * There's no such device.
724235426Sdelphij			 */
725235426Sdelphij			close(fd);
726235426Sdelphij			return (PCAP_ERROR_NO_SUCH_DEVICE);
727235426Sdelphij
728235426Sdelphij		case ENETDOWN:
729235426Sdelphij			/*
730190225Srpaulo			 * Return a "network down" indication, so that
731190225Srpaulo			 * the application can report that rather than
732190225Srpaulo			 * saying we had a mysterious failure and
733190225Srpaulo			 * suggest that they report a problem to the
734190225Srpaulo			 * libpcap developers.
735190225Srpaulo			 */
736190225Srpaulo			close(fd);
737190225Srpaulo			return (PCAP_ERROR_IFACE_NOT_UP);
738235426Sdelphij
739235426Sdelphij		default:
740190225Srpaulo			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
741190225Srpaulo			    "BIOCSETIF: %s: %s",
742190225Srpaulo			    p->opt.source, pcap_strerror(errno));
743190225Srpaulo			close(fd);
744190225Srpaulo			return (PCAP_ERROR);
745190225Srpaulo		}
746190225Srpaulo	}
747190225Srpaulo
748190225Srpaulo	/*
749190225Srpaulo	 * We know the default link type -- now determine all the DLTs
750190225Srpaulo	 * this interface supports.  If this fails with EINVAL, it's
751190225Srpaulo	 * not fatal; we just don't get to use the feature later.
752190225Srpaulo	 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
753190225Srpaulo	 * as the default DLT for this adapter.)
754190225Srpaulo	 */
755190225Srpaulo	if (get_dlt_list(fd, DLT_NULL, &bdl, p->errbuf) == PCAP_ERROR) {
756190225Srpaulo		close(fd);
757190225Srpaulo		return (PCAP_ERROR);
758190225Srpaulo	}
759190225Srpaulo	if (find_802_11(&bdl) != -1) {
760190225Srpaulo		/*
761190225Srpaulo		 * We have an 802.11 DLT, so we can set monitor mode.
762190225Srpaulo		 */
763190225Srpaulo		free(bdl.bfl_list);
764190225Srpaulo		close(fd);
765190225Srpaulo		return (1);
766190225Srpaulo	}
767190225Srpaulo	free(bdl.bfl_list);
768190225Srpaulo#endif /* BIOCGDLTLIST */
769190225Srpaulo	return (0);
770190225Srpaulo#elif defined(HAVE_BSD_IEEE80211)
771190225Srpaulo	int ret;
772190225Srpaulo
773190225Srpaulo	ret = monitor_mode(p, 0);
774190225Srpaulo	if (ret == PCAP_ERROR_RFMON_NOTSUP)
775190225Srpaulo		return (0);	/* not an error, just a "can't do" */
776190225Srpaulo	if (ret == 0)
777190225Srpaulo		return (1);	/* success */
778190225Srpaulo	return (ret);
779190225Srpaulo#else
780190225Srpaulo	return (0);
781190225Srpaulo#endif
782190225Srpaulo}
783190225Srpaulo
784190225Srpaulostatic int
785127664Sbmspcap_stats_bpf(pcap_t *p, struct pcap_stat *ps)
78617683Spst{
78717683Spst	struct bpf_stat s;
78817683Spst
78998530Sfenner	/*
79098530Sfenner	 * "ps_recv" counts packets handed to the filter, not packets
79198530Sfenner	 * that passed the filter.  This includes packets later dropped
79298530Sfenner	 * because we ran out of buffer space.
79398530Sfenner	 *
79498530Sfenner	 * "ps_drop" counts packets dropped inside the BPF device
79598530Sfenner	 * because we ran out of buffer space.  It doesn't count
79698530Sfenner	 * packets dropped by the interface driver.  It counts
79798530Sfenner	 * only packets that passed the filter.
79898530Sfenner	 *
79998530Sfenner	 * Both statistics include packets not yet read from the kernel
80098530Sfenner	 * by libpcap, and thus not yet seen by the application.
80198530Sfenner	 */
80217683Spst	if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) {
80375107Sfenner		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGSTATS: %s",
80475107Sfenner		    pcap_strerror(errno));
805190225Srpaulo		return (PCAP_ERROR);
80617683Spst	}
80717683Spst
80817683Spst	ps->ps_recv = s.bs_recv;
80917683Spst	ps->ps_drop = s.bs_drop;
810214518Srpaulo	ps->ps_ifdrop = 0;
81117683Spst	return (0);
81217683Spst}
81317683Spst
814127664Sbmsstatic int
815127664Sbmspcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
81617683Spst{
81717683Spst	int cc;
81817683Spst	int n = 0;
81917683Spst	register u_char *bp, *ep;
820146768Ssam	u_char *datap;
821146768Ssam#ifdef PCAP_FDDIPAD
822146768Ssam	register int pad;
823146768Ssam#endif
824190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF
825190225Srpaulo	int i;
826190225Srpaulo#endif
82717683Spst
82817683Spst again:
829127664Sbms	/*
830127664Sbms	 * Has "pcap_breakloop()" been called?
831127664Sbms	 */
832127664Sbms	if (p->break_loop) {
833127664Sbms		/*
834127664Sbms		 * Yes - clear the flag that indicates that it
835190225Srpaulo		 * has, and return PCAP_ERROR_BREAK to indicate
836190225Srpaulo		 * that we were told to break out of the loop.
837127664Sbms		 */
838127664Sbms		p->break_loop = 0;
839190225Srpaulo		return (PCAP_ERROR_BREAK);
840127664Sbms	}
84117683Spst	cc = p->cc;
84217683Spst	if (p->cc == 0) {
843183102Scsjp		/*
844183102Scsjp		 * When reading without zero-copy from a file descriptor, we
845183102Scsjp		 * use a single buffer and return a length of data in the
846183102Scsjp		 * buffer.  With zero-copy, we update the p->buffer pointer
847183102Scsjp		 * to point at whatever underlying buffer contains the next
848183102Scsjp		 * data and update cc to reflect the data found in the
849183102Scsjp		 * buffer.
850183102Scsjp		 */
851190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF
852190225Srpaulo		if (p->md.zerocopy) {
853183102Scsjp			if (p->buffer != NULL)
854183102Scsjp				pcap_ack_zbuf(p);
855183102Scsjp			i = pcap_next_zbuf(p, &cc);
856183102Scsjp			if (i == 0)
857183102Scsjp				goto again;
858183102Scsjp			if (i < 0)
859190225Srpaulo				return (PCAP_ERROR);
860183102Scsjp		} else
861183102Scsjp#endif
862190225Srpaulo		{
863183102Scsjp			cc = read(p->fd, (char *)p->buffer, p->bufsize);
864190225Srpaulo		}
86517683Spst		if (cc < 0) {
86617683Spst			/* Don't choke when we get ptraced */
86717683Spst			switch (errno) {
86817683Spst
86917683Spst			case EINTR:
87017683Spst				goto again;
87117683Spst
872127664Sbms#ifdef _AIX
873127664Sbms			case EFAULT:
874127664Sbms				/*
875127664Sbms				 * Sigh.  More AIX wonderfulness.
876127664Sbms				 *
877127664Sbms				 * For some unknown reason the uiomove()
878127664Sbms				 * operation in the bpf kernel extension
879190225Srpaulo				 * used to copy the buffer into user
880127664Sbms				 * space sometimes returns EFAULT. I have
881127664Sbms				 * no idea why this is the case given that
882190225Srpaulo				 * a kernel debugger shows the user buffer
883190225Srpaulo				 * is correct. This problem appears to
884190225Srpaulo				 * be mostly mitigated by the memset of
885190225Srpaulo				 * the buffer before it is first used.
886127664Sbms				 * Very strange.... Shaun Clowes
887127664Sbms				 *
888190225Srpaulo				 * In any case this means that we shouldn't
889127664Sbms				 * treat EFAULT as a fatal error; as we
890127664Sbms				 * don't have an API for returning
891127664Sbms				 * a "some packets were dropped since
892127664Sbms				 * the last packet you saw" indication,
893127664Sbms				 * we just ignore EFAULT and keep reading.
894127664Sbms				 */
895127664Sbms				goto again;
896190225Srpaulo#endif
897190225Srpaulo
89817683Spst			case EWOULDBLOCK:
89917683Spst				return (0);
900214518Srpaulo
901214518Srpaulo			case ENXIO:
902214518Srpaulo				/*
903214518Srpaulo				 * The device on which we're capturing
904214518Srpaulo				 * went away.
905214518Srpaulo				 *
906214518Srpaulo				 * XXX - we should really return
907214518Srpaulo				 * PCAP_ERROR_IFACE_NOT_UP, but
908214518Srpaulo				 * pcap_dispatch() etc. aren't
909214518Srpaulo				 * defined to retur that.
910214518Srpaulo				 */
911214518Srpaulo				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
912214518Srpaulo				    "The interface went down");
913214518Srpaulo				return (PCAP_ERROR);
914214518Srpaulo
915214518Srpaulo#if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
91617683Spst			/*
91717683Spst			 * Due to a SunOS bug, after 2^31 bytes, the kernel
91817683Spst			 * file offset overflows and read fails with EINVAL.
91917683Spst			 * The lseek() to 0 will fix things.
92017683Spst			 */
92117683Spst			case EINVAL:
92217683Spst				if (lseek(p->fd, 0L, SEEK_CUR) +
92317683Spst				    p->bufsize < 0) {
92417683Spst					(void)lseek(p->fd, 0L, SEEK_SET);
92517683Spst					goto again;
92617683Spst				}
92717683Spst				/* fall through */
92817683Spst#endif
92917683Spst			}
93075107Sfenner			snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read: %s",
93175107Sfenner			    pcap_strerror(errno));
932190225Srpaulo			return (PCAP_ERROR);
93317683Spst		}
93417683Spst		bp = p->buffer;
93517683Spst	} else
93617683Spst		bp = p->bp;
93717683Spst
93817683Spst	/*
93917683Spst	 * Loop through each packet.
94017683Spst	 */
94117683Spst#define bhp ((struct bpf_hdr *)bp)
94217683Spst	ep = bp + cc;
943146768Ssam#ifdef PCAP_FDDIPAD
944146768Ssam	pad = p->fddipad;
945146768Ssam#endif
94617683Spst	while (bp < ep) {
94717683Spst		register int caplen, hdrlen;
948127664Sbms
949127664Sbms		/*
950127664Sbms		 * Has "pcap_breakloop()" been called?
951127664Sbms		 * If so, return immediately - if we haven't read any
952190225Srpaulo		 * packets, clear the flag and return PCAP_ERROR_BREAK
953190225Srpaulo		 * to indicate that we were told to break out of the loop,
954190225Srpaulo		 * otherwise leave the flag set, so that the *next* call
955190225Srpaulo		 * will break out of the loop without having read any
956190225Srpaulo		 * packets, and return the number of packets we've
957190225Srpaulo		 * processed so far.
958127664Sbms		 */
959127664Sbms		if (p->break_loop) {
960235426Sdelphij			p->bp = bp;
961235426Sdelphij			p->cc = ep - bp;
962235426Sdelphij			/*
963235426Sdelphij			 * ep is set based on the return value of read(),
964235426Sdelphij			 * but read() from a BPF device doesn't necessarily
965235426Sdelphij			 * return a value that's a multiple of the alignment
966235426Sdelphij			 * value for BPF_WORDALIGN().  However, whenever we
967235426Sdelphij			 * increment bp, we round up the increment value by
968235426Sdelphij			 * a value rounded up by BPF_WORDALIGN(), so we
969235426Sdelphij			 * could increment bp past ep after processing the
970235426Sdelphij			 * last packet in the buffer.
971235426Sdelphij			 *
972235426Sdelphij			 * We treat ep < bp as an indication that this
973235426Sdelphij			 * happened, and just set p->cc to 0.
974235426Sdelphij			 */
975235426Sdelphij			if (p->cc < 0)
976235426Sdelphij				p->cc = 0;
977127664Sbms			if (n == 0) {
978127664Sbms				p->break_loop = 0;
979190225Srpaulo				return (PCAP_ERROR_BREAK);
980235426Sdelphij			} else
981127664Sbms				return (n);
982127664Sbms		}
983127664Sbms
98417683Spst		caplen = bhp->bh_caplen;
98517683Spst		hdrlen = bhp->bh_hdrlen;
986146768Ssam		datap = bp + hdrlen;
98717683Spst		/*
988127664Sbms		 * Short-circuit evaluation: if using BPF filter
989190225Srpaulo		 * in kernel, no need to do it now - we already know
990190225Srpaulo		 * the packet passed the filter.
991146768Ssam		 *
992146768Ssam#ifdef PCAP_FDDIPAD
993146768Ssam		 * Note: the filter code was generated assuming
994146768Ssam		 * that p->fddipad was the amount of padding
995146768Ssam		 * before the header, as that's what's required
996146768Ssam		 * in the kernel, so we run the filter before
997146768Ssam		 * skipping that padding.
998146768Ssam#endif
99917683Spst		 */
1000190225Srpaulo		if (p->md.use_bpf ||
1001190225Srpaulo		    bpf_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) {
1002146768Ssam			struct pcap_pkthdr pkthdr;
1003146768Ssam
1004146768Ssam			pkthdr.ts.tv_sec = bhp->bh_tstamp.tv_sec;
100598530Sfenner#ifdef _AIX
1006127664Sbms			/*
1007127664Sbms			 * AIX's BPF returns seconds/nanoseconds time
1008127664Sbms			 * stamps, not seconds/microseconds time stamps.
1009127664Sbms			 */
1010146768Ssam			pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec/1000;
1011146768Ssam#else
1012146768Ssam			pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec;
101398530Sfenner#endif
1014146768Ssam#ifdef PCAP_FDDIPAD
1015146768Ssam			if (caplen > pad)
1016146768Ssam				pkthdr.caplen = caplen - pad;
1017146768Ssam			else
1018146768Ssam				pkthdr.caplen = 0;
1019146768Ssam			if (bhp->bh_datalen > pad)
1020146768Ssam				pkthdr.len = bhp->bh_datalen - pad;
1021146768Ssam			else
1022146768Ssam				pkthdr.len = 0;
1023146768Ssam			datap += pad;
1024146768Ssam#else
1025146768Ssam			pkthdr.caplen = caplen;
1026146768Ssam			pkthdr.len = bhp->bh_datalen;
1027146768Ssam#endif
1028146768Ssam			(*callback)(user, &pkthdr, datap);
1029127664Sbms			bp += BPF_WORDALIGN(caplen + hdrlen);
1030127664Sbms			if (++n >= cnt && cnt > 0) {
1031127664Sbms				p->bp = bp;
1032127664Sbms				p->cc = ep - bp;
1033235426Sdelphij				/*
1034235426Sdelphij				 * See comment above about p->cc < 0.
1035235426Sdelphij				 */
1036235426Sdelphij				if (p->cc < 0)
1037235426Sdelphij					p->cc = 0;
1038127664Sbms				return (n);
1039127664Sbms			}
1040127664Sbms		} else {
1041127664Sbms			/*
1042127664Sbms			 * Skip this packet.
1043127664Sbms			 */
1044127664Sbms			bp += BPF_WORDALIGN(caplen + hdrlen);
104517683Spst		}
104617683Spst	}
104717683Spst#undef bhp
104817683Spst	p->cc = 0;
104917683Spst	return (n);
105017683Spst}
105117683Spst
1052146768Ssamstatic int
1053146768Ssampcap_inject_bpf(pcap_t *p, const void *buf, size_t size)
1054146768Ssam{
1055146768Ssam	int ret;
1056146768Ssam
1057146768Ssam	ret = write(p->fd, buf, size);
1058146768Ssam#ifdef __APPLE__
1059146768Ssam	if (ret == -1 && errno == EAFNOSUPPORT) {
1060146768Ssam		/*
1061146768Ssam		 * In Mac OS X, there's a bug wherein setting the
1062146768Ssam		 * BIOCSHDRCMPLT flag causes writes to fail; see,
1063146768Ssam		 * for example:
1064146768Ssam		 *
1065146768Ssam		 *	http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
1066146768Ssam		 *
1067146768Ssam		 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
1068146768Ssam		 * assume it's due to that bug, and turn off that flag
1069146768Ssam		 * and try again.  If we succeed, it either means that
1070146768Ssam		 * somebody applied the fix from that URL, or other patches
1071146768Ssam		 * for that bug from
1072146768Ssam		 *
1073146768Ssam		 *	http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
1074146768Ssam		 *
1075146768Ssam		 * and are running a Darwin kernel with those fixes, or
1076146768Ssam		 * that Apple fixed the problem in some OS X release.
1077146768Ssam		 */
1078146768Ssam		u_int spoof_eth_src = 0;
1079146768Ssam
1080146768Ssam		if (ioctl(p->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
1081146768Ssam			(void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1082146768Ssam			    "send: can't turn off BIOCSHDRCMPLT: %s",
1083146768Ssam			    pcap_strerror(errno));
1084190225Srpaulo			return (PCAP_ERROR);
1085146768Ssam		}
1086146768Ssam
1087146768Ssam		/*
1088146768Ssam		 * Now try the write again.
1089146768Ssam		 */
1090146768Ssam		ret = write(p->fd, buf, size);
1091146768Ssam	}
1092146768Ssam#endif /* __APPLE__ */
1093146768Ssam	if (ret == -1) {
1094146768Ssam		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
1095146768Ssam		    pcap_strerror(errno));
1096190225Srpaulo		return (PCAP_ERROR);
1097146768Ssam	}
1098146768Ssam	return (ret);
1099146768Ssam}
1100146768Ssam
1101127664Sbms#ifdef _AIX
1102190225Srpaulostatic int
1103127664Sbmsbpf_odminit(char *errbuf)
1104127664Sbms{
1105127664Sbms	char *errstr;
1106127664Sbms
1107127664Sbms	if (odm_initialize() == -1) {
1108127664Sbms		if (odm_err_msg(odmerrno, &errstr) == -1)
1109127664Sbms			errstr = "Unknown error";
1110127664Sbms		snprintf(errbuf, PCAP_ERRBUF_SIZE,
1111127664Sbms		    "bpf_load: odm_initialize failed: %s",
1112127664Sbms		    errstr);
1113190225Srpaulo		return (PCAP_ERROR);
1114127664Sbms	}
1115127664Sbms
1116127664Sbms	if ((odmlockid = odm_lock("/etc/objrepos/config_lock", ODM_WAIT)) == -1) {
1117127664Sbms		if (odm_err_msg(odmerrno, &errstr) == -1)
1118127664Sbms			errstr = "Unknown error";
1119127664Sbms		snprintf(errbuf, PCAP_ERRBUF_SIZE,
1120127664Sbms		    "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
1121127664Sbms		    errstr);
1122214518Srpaulo		(void)odm_terminate();
1123190225Srpaulo		return (PCAP_ERROR);
1124127664Sbms	}
1125127664Sbms
1126127664Sbms	return (0);
1127127664Sbms}
1128127664Sbms
1129190225Srpaulostatic int
1130127664Sbmsbpf_odmcleanup(char *errbuf)
1131127664Sbms{
1132127664Sbms	char *errstr;
1133127664Sbms
1134127664Sbms	if (odm_unlock(odmlockid) == -1) {
1135214518Srpaulo		if (errbuf != NULL) {
1136214518Srpaulo			if (odm_err_msg(odmerrno, &errstr) == -1)
1137214518Srpaulo				errstr = "Unknown error";
1138214518Srpaulo			snprintf(errbuf, PCAP_ERRBUF_SIZE,
1139214518Srpaulo			    "bpf_load: odm_unlock failed: %s",
1140214518Srpaulo			    errstr);
1141214518Srpaulo		}
1142190225Srpaulo		return (PCAP_ERROR);
1143127664Sbms	}
1144127664Sbms
1145127664Sbms	if (odm_terminate() == -1) {
1146214518Srpaulo		if (errbuf != NULL) {
1147214518Srpaulo			if (odm_err_msg(odmerrno, &errstr) == -1)
1148214518Srpaulo				errstr = "Unknown error";
1149214518Srpaulo			snprintf(errbuf, PCAP_ERRBUF_SIZE,
1150214518Srpaulo			    "bpf_load: odm_terminate failed: %s",
1151214518Srpaulo			    errstr);
1152214518Srpaulo		}
1153190225Srpaulo		return (PCAP_ERROR);
1154127664Sbms	}
1155127664Sbms
1156127664Sbms	return (0);
1157127664Sbms}
1158127664Sbms
1159127664Sbmsstatic int
1160127664Sbmsbpf_load(char *errbuf)
1161127664Sbms{
1162127664Sbms	long major;
1163127664Sbms	int *minors;
1164127664Sbms	int numminors, i, rc;
1165127664Sbms	char buf[1024];
1166127664Sbms	struct stat sbuf;
1167127664Sbms	struct bpf_config cfg_bpf;
1168127664Sbms	struct cfg_load cfg_ld;
1169127664Sbms	struct cfg_kmod cfg_km;
1170127664Sbms
1171127664Sbms	/*
1172127664Sbms	 * This is very very close to what happens in the real implementation
1173127664Sbms	 * but I've fixed some (unlikely) bug situations.
1174127664Sbms	 */
1175127664Sbms	if (bpfloadedflag)
1176127664Sbms		return (0);
1177127664Sbms
1178190225Srpaulo	if (bpf_odminit(errbuf) == PCAP_ERROR)
1179190225Srpaulo		return (PCAP_ERROR);
1180127664Sbms
1181127664Sbms	major = genmajor(BPF_NAME);
1182127664Sbms	if (major == -1) {
1183127664Sbms		snprintf(errbuf, PCAP_ERRBUF_SIZE,
1184127664Sbms		    "bpf_load: genmajor failed: %s", pcap_strerror(errno));
1185214518Srpaulo		(void)bpf_odmcleanup(NULL);
1186190225Srpaulo		return (PCAP_ERROR);
1187127664Sbms	}
1188127664Sbms
1189127664Sbms	minors = getminor(major, &numminors, BPF_NAME);
1190127664Sbms	if (!minors) {
1191127664Sbms		minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1);
1192127664Sbms		if (!minors) {
1193127664Sbms			snprintf(errbuf, PCAP_ERRBUF_SIZE,
1194127664Sbms			    "bpf_load: genminor failed: %s",
1195127664Sbms			    pcap_strerror(errno));
1196214518Srpaulo			(void)bpf_odmcleanup(NULL);
1197190225Srpaulo			return (PCAP_ERROR);
1198127664Sbms		}
1199127664Sbms	}
1200127664Sbms
1201190225Srpaulo	if (bpf_odmcleanup(errbuf) == PCAP_ERROR)
1202190225Srpaulo		return (PCAP_ERROR);
1203127664Sbms
1204127664Sbms	rc = stat(BPF_NODE "0", &sbuf);
1205127664Sbms	if (rc == -1 && errno != ENOENT) {
1206127664Sbms		snprintf(errbuf, PCAP_ERRBUF_SIZE,
1207127664Sbms		    "bpf_load: can't stat %s: %s",
1208127664Sbms		    BPF_NODE "0", pcap_strerror(errno));
1209190225Srpaulo		return (PCAP_ERROR);
1210127664Sbms	}
1211127664Sbms
1212127664Sbms	if (rc == -1 || getmajor(sbuf.st_rdev) != major) {
1213127664Sbms		for (i = 0; i < BPF_MINORS; i++) {
1214127664Sbms			sprintf(buf, "%s%d", BPF_NODE, i);
1215127664Sbms			unlink(buf);
1216127664Sbms			if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) {
1217127664Sbms				snprintf(errbuf, PCAP_ERRBUF_SIZE,
1218127664Sbms				    "bpf_load: can't mknod %s: %s",
1219127664Sbms				    buf, pcap_strerror(errno));
1220190225Srpaulo				return (PCAP_ERROR);
1221127664Sbms			}
1222127664Sbms		}
1223127664Sbms	}
1224127664Sbms
1225127664Sbms	/* Check if the driver is loaded */
1226127664Sbms	memset(&cfg_ld, 0x0, sizeof(cfg_ld));
1227127664Sbms	cfg_ld.path = buf;
1228127664Sbms	sprintf(cfg_ld.path, "%s/%s", DRIVER_PATH, BPF_NAME);
1229127664Sbms	if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) ||
1230127664Sbms	    (cfg_ld.kmid == 0)) {
1231127664Sbms		/* Driver isn't loaded, load it now */
1232127664Sbms		if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) {
1233127664Sbms			snprintf(errbuf, PCAP_ERRBUF_SIZE,
1234127664Sbms			    "bpf_load: could not load driver: %s",
1235127664Sbms			    strerror(errno));
1236190225Srpaulo			return (PCAP_ERROR);
1237127664Sbms		}
1238127664Sbms	}
1239127664Sbms
1240127664Sbms	/* Configure the driver */
1241127664Sbms	cfg_km.cmd = CFG_INIT;
1242127664Sbms	cfg_km.kmid = cfg_ld.kmid;
1243127664Sbms	cfg_km.mdilen = sizeof(cfg_bpf);
1244190225Srpaulo	cfg_km.mdiptr = (void *)&cfg_bpf;
1245127664Sbms	for (i = 0; i < BPF_MINORS; i++) {
1246127664Sbms		cfg_bpf.devno = domakedev(major, i);
1247127664Sbms		if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) {
1248127664Sbms			snprintf(errbuf, PCAP_ERRBUF_SIZE,
1249127664Sbms			    "bpf_load: could not configure driver: %s",
1250127664Sbms			    strerror(errno));
1251190225Srpaulo			return (PCAP_ERROR);
1252127664Sbms		}
1253127664Sbms	}
1254190225Srpaulo
1255127664Sbms	bpfloadedflag = 1;
1256127664Sbms
1257127664Sbms	return (0);
1258127664Sbms}
1259127664Sbms#endif
1260127664Sbms
1261190225Srpaulo/*
1262190225Srpaulo * Turn off rfmon mode if necessary.
1263190225Srpaulo */
1264190225Srpaulostatic void
1265190225Srpaulopcap_cleanup_bpf(pcap_t *p)
126617683Spst{
1267190225Srpaulo#ifdef HAVE_BSD_IEEE80211
1268190225Srpaulo	int sock;
1269190225Srpaulo	struct ifmediareq req;
1270190225Srpaulo	struct ifreq ifr;
1271172680Smlaier#endif
127217683Spst
1273214518Srpaulo	if (p->md.must_do_on_close != 0) {
1274190225Srpaulo		/*
1275190225Srpaulo		 * There's something we have to do when closing this
1276190225Srpaulo		 * pcap_t.
1277190225Srpaulo		 */
1278190225Srpaulo#ifdef HAVE_BSD_IEEE80211
1279214518Srpaulo		if (p->md.must_do_on_close & MUST_CLEAR_RFMON) {
1280190225Srpaulo			/*
1281190225Srpaulo			 * We put the interface into rfmon mode;
1282190225Srpaulo			 * take it out of rfmon mode.
1283190225Srpaulo			 *
1284190225Srpaulo			 * XXX - if somebody else wants it in rfmon
1285190225Srpaulo			 * mode, this code cannot know that, so it'll take
1286190225Srpaulo			 * it out of rfmon mode.
1287190225Srpaulo			 */
1288190225Srpaulo			sock = socket(AF_INET, SOCK_DGRAM, 0);
1289190225Srpaulo			if (sock == -1) {
1290190225Srpaulo				fprintf(stderr,
1291190225Srpaulo				    "Can't restore interface flags (socket() failed: %s).\n"
1292190225Srpaulo				    "Please adjust manually.\n",
1293190225Srpaulo				    strerror(errno));
1294190225Srpaulo			} else {
1295190225Srpaulo				memset(&req, 0, sizeof(req));
1296190225Srpaulo				strncpy(req.ifm_name, p->md.device,
1297190225Srpaulo				    sizeof(req.ifm_name));
1298190225Srpaulo				if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
1299190225Srpaulo					fprintf(stderr,
1300190225Srpaulo					    "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
1301190225Srpaulo					    "Please adjust manually.\n",
1302190225Srpaulo					    strerror(errno));
1303190225Srpaulo				} else {
1304190225Srpaulo					if (req.ifm_current & IFM_IEEE80211_MONITOR) {
1305190225Srpaulo						/*
1306190225Srpaulo						 * Rfmon mode is currently on;
1307190225Srpaulo						 * turn it off.
1308190225Srpaulo						 */
1309190225Srpaulo						memset(&ifr, 0, sizeof(ifr));
1310190225Srpaulo						(void)strncpy(ifr.ifr_name,
1311190225Srpaulo						    p->md.device,
1312190225Srpaulo						    sizeof(ifr.ifr_name));
1313190225Srpaulo						ifr.ifr_media =
1314190225Srpaulo						    req.ifm_current & ~IFM_IEEE80211_MONITOR;
1315190225Srpaulo						if (ioctl(sock, SIOCSIFMEDIA,
1316190225Srpaulo						    &ifr) == -1) {
1317190225Srpaulo							fprintf(stderr,
1318190225Srpaulo							    "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
1319190225Srpaulo							    "Please adjust manually.\n",
1320190225Srpaulo							    strerror(errno));
1321190225Srpaulo						}
1322190225Srpaulo					}
1323190225Srpaulo				}
1324190225Srpaulo				close(sock);
1325190225Srpaulo			}
1326190225Srpaulo		}
1327190225Srpaulo#endif /* HAVE_BSD_IEEE80211 */
1328127664Sbms
1329146768Ssam		/*
1330190225Srpaulo		 * Take this pcap out of the list of pcaps for which we
1331190225Srpaulo		 * have to take the interface out of some mode.
1332146768Ssam		 */
1333190225Srpaulo		pcap_remove_from_pcaps_to_close(p);
1334214518Srpaulo		p->md.must_do_on_close = 0;
1335190225Srpaulo	}
133617683Spst
1337190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF
1338190225Srpaulo	if (p->md.zerocopy) {
1339235426Sdelphij		/*
1340235426Sdelphij		 * Delete the mappings.  Note that p->buffer gets
1341235426Sdelphij		 * initialized to one of the mmapped regions in
1342235426Sdelphij		 * this case, so do not try and free it directly;
1343235426Sdelphij		 * null it out so that pcap_cleanup_live_common()
1344235426Sdelphij		 * doesn't try to free it.
1345235426Sdelphij		 */
1346190225Srpaulo		if (p->md.zbuf1 != MAP_FAILED && p->md.zbuf1 != NULL)
1347235426Sdelphij			(void) munmap(p->md.zbuf1, p->md.zbufsize);
1348190225Srpaulo		if (p->md.zbuf2 != MAP_FAILED && p->md.zbuf2 != NULL)
1349235426Sdelphij			(void) munmap(p->md.zbuf2, p->md.zbufsize);
1350222654Scsjp		p->buffer = NULL;
1351235426Sdelphij		p->buffer = NULL;
1352190225Srpaulo	}
1353172680Smlaier#endif
1354190225Srpaulo	if (p->md.device != NULL) {
1355190225Srpaulo		free(p->md.device);
1356190225Srpaulo		p->md.device = NULL;
1357190225Srpaulo	}
1358190225Srpaulo	pcap_cleanup_live_common(p);
135917683Spst}
136017683Spst
1361190225Srpaulostatic int
1362190225Srpaulocheck_setif_failure(pcap_t *p, int error)
1363190225Srpaulo{
1364190225Srpaulo#ifdef __APPLE__
1365190225Srpaulo	int fd;
1366190225Srpaulo	struct ifreq ifr;
1367190225Srpaulo	int err;
1368146768Ssam#endif
1369146768Ssam
1370190225Srpaulo	if (error == ENXIO) {
1371190225Srpaulo		/*
1372190225Srpaulo		 * No such device exists.
1373190225Srpaulo		 */
1374190225Srpaulo#ifdef __APPLE__
1375190225Srpaulo		if (p->opt.rfmon && strncmp(p->opt.source, "wlt", 3) == 0) {
1376190225Srpaulo			/*
1377190225Srpaulo			 * Monitor mode was requested, and we're trying
1378190225Srpaulo			 * to open a "wltN" device.  Assume that this
1379190225Srpaulo			 * is 10.4 and that we were asked to open an
1380190225Srpaulo			 * "enN" device; if that device exists, return
1381190225Srpaulo			 * "monitor mode not supported on the device".
1382190225Srpaulo			 */
1383190225Srpaulo			fd = socket(AF_INET, SOCK_DGRAM, 0);
1384190225Srpaulo			if (fd != -1) {
1385190225Srpaulo				strlcpy(ifr.ifr_name, "en",
1386190225Srpaulo				    sizeof(ifr.ifr_name));
1387190225Srpaulo				strlcat(ifr.ifr_name, p->opt.source + 3,
1388190225Srpaulo				    sizeof(ifr.ifr_name));
1389190225Srpaulo				if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
1390190225Srpaulo					/*
1391190225Srpaulo					 * We assume this failed because
1392190225Srpaulo					 * the underlying device doesn't
1393190225Srpaulo					 * exist.
1394190225Srpaulo					 */
1395190225Srpaulo					err = PCAP_ERROR_NO_SUCH_DEVICE;
1396214518Srpaulo					snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1397214518Srpaulo					    "SIOCGIFFLAGS on %s failed: %s",
1398214518Srpaulo					    ifr.ifr_name, pcap_strerror(errno));
1399190225Srpaulo				} else {
1400190225Srpaulo					/*
1401190225Srpaulo					 * The underlying "enN" device
1402190225Srpaulo					 * exists, but there's no
1403190225Srpaulo					 * corresponding "wltN" device;
1404190225Srpaulo					 * that means that the "enN"
1405190225Srpaulo					 * device doesn't support
1406190225Srpaulo					 * monitor mode, probably because
1407190225Srpaulo					 * it's an Ethernet device rather
1408190225Srpaulo					 * than a wireless device.
1409190225Srpaulo					 */
1410190225Srpaulo					err = PCAP_ERROR_RFMON_NOTSUP;
1411190225Srpaulo				}
1412190225Srpaulo				close(fd);
1413190225Srpaulo			} else {
1414190225Srpaulo				/*
1415190225Srpaulo				 * We can't find out whether there's
1416190225Srpaulo				 * an underlying "enN" device, so
1417190225Srpaulo				 * just report "no such device".
1418190225Srpaulo				 */
1419190225Srpaulo				err = PCAP_ERROR_NO_SUCH_DEVICE;
1420214518Srpaulo				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1421214518Srpaulo				    "socket() failed: %s",
1422214518Srpaulo				    pcap_strerror(errno));
1423190225Srpaulo			}
1424190225Srpaulo			return (err);
1425190225Srpaulo		}
1426190225Srpaulo#endif
1427190225Srpaulo		/*
1428190225Srpaulo		 * No such device.
1429190225Srpaulo		 */
1430214518Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF failed: %s",
1431214518Srpaulo		    pcap_strerror(errno));
1432190225Srpaulo		return (PCAP_ERROR_NO_SUCH_DEVICE);
1433190225Srpaulo	} else if (errno == ENETDOWN) {
1434190225Srpaulo		/*
1435190225Srpaulo		 * Return a "network down" indication, so that
1436190225Srpaulo		 * the application can report that rather than
1437190225Srpaulo		 * saying we had a mysterious failure and
1438190225Srpaulo		 * suggest that they report a problem to the
1439190225Srpaulo		 * libpcap developers.
1440190225Srpaulo		 */
1441190225Srpaulo		return (PCAP_ERROR_IFACE_NOT_UP);
1442190225Srpaulo	} else {
1443190225Srpaulo		/*
1444190225Srpaulo		 * Some other error; fill in the error string, and
1445190225Srpaulo		 * return PCAP_ERROR.
1446190225Srpaulo		 */
1447190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s",
1448190225Srpaulo		    p->opt.source, pcap_strerror(errno));
1449190225Srpaulo		return (PCAP_ERROR);
1450190225Srpaulo	}
1451190225Srpaulo}
1452190225Srpaulo
1453214518Srpaulo/*
1454214518Srpaulo * Default capture buffer size.
1455214518Srpaulo * 32K isn't very much for modern machines with fast networks; we
1456214518Srpaulo * pick .5M, as that's the maximum on at least some systems with BPF.
1457214518Srpaulo */
1458214518Srpaulo#define DEFAULT_BUFSIZE	524288
1459214518Srpaulo
1460190225Srpaulostatic int
1461190225Srpaulopcap_activate_bpf(pcap_t *p)
146217683Spst{
1463190225Srpaulo	int status = 0;
146417683Spst	int fd;
1465235426Sdelphij#ifdef LIFNAMSIZ
1466235426Sdelphij	struct lifreq ifr;
1467235426Sdelphij	char *ifrname = ifr.lifr_name;
1468235426Sdelphij	const size_t ifnamsiz = sizeof(ifr.lifr_name);
1469235426Sdelphij#else
147017683Spst	struct ifreq ifr;
1471235426Sdelphij	char *ifrname = ifr.ifr_name;
1472235426Sdelphij	const size_t ifnamsiz = sizeof(ifr.ifr_name);
1473235426Sdelphij#endif
147417683Spst	struct bpf_version bv;
1475190225Srpaulo#ifdef __APPLE__
1476190225Srpaulo	int sockfd;
1477190225Srpaulo	char *wltdev = NULL;
1478190225Srpaulo#endif
1479127664Sbms#ifdef BIOCGDLTLIST
1480109839Sfenner	struct bpf_dltlist bdl;
1481190225Srpaulo#if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1482190225Srpaulo	int new_dlt;
1483127664Sbms#endif
1484190225Srpaulo#endif /* BIOCGDLTLIST */
1485146768Ssam#if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1486146768Ssam	u_int spoof_eth_src = 1;
1487146768Ssam#endif
148817683Spst	u_int v;
1489146768Ssam	struct bpf_insn total_insn;
1490146768Ssam	struct bpf_program total_prog;
1491127664Sbms	struct utsname osinfo;
149217683Spst
1493127664Sbms#ifdef HAVE_DAG_API
1494127664Sbms	if (strstr(device, "dag")) {
1495127664Sbms		return dag_open_live(device, snaplen, promisc, to_ms, ebuf);
1496127664Sbms	}
1497127664Sbms#endif /* HAVE_DAG_API */
1498109839Sfenner
1499127664Sbms#ifdef BIOCGDLTLIST
1500127664Sbms	memset(&bdl, 0, sizeof(bdl));
1501190225Srpaulo	int have_osinfo = 0;
1502190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF
1503190225Srpaulo	struct bpf_zbuf bz;
1504190225Srpaulo	u_int bufmode, zbufmax;
1505127664Sbms#endif
1506127664Sbms
1507190225Srpaulo	fd = bpf_open(p);
1508190225Srpaulo	if (fd < 0) {
1509190225Srpaulo		status = fd;
1510190225Srpaulo		goto bad;
151117683Spst	}
151217683Spst
151317683Spst	p->fd = fd;
151417683Spst
151517683Spst	if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) {
1516190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCVERSION: %s",
151775107Sfenner		    pcap_strerror(errno));
1518190225Srpaulo		status = PCAP_ERROR;
151917683Spst		goto bad;
152017683Spst	}
152117683Spst	if (bv.bv_major != BPF_MAJOR_VERSION ||
152217683Spst	    bv.bv_minor < BPF_MINOR_VERSION) {
1523190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
152475107Sfenner		    "kernel bpf filter out of date");
1525190225Srpaulo		status = PCAP_ERROR;
152617683Spst		goto bad;
152717683Spst	}
152875107Sfenner
1529190225Srpaulo	p->md.device = strdup(p->opt.source);
1530190225Srpaulo	if (p->md.device == NULL) {
1531190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "strdup: %s",
1532190225Srpaulo		     pcap_strerror(errno));
1533190225Srpaulo		status = PCAP_ERROR;
1534190225Srpaulo		goto bad;
1535190225Srpaulo	}
1536190225Srpaulo
153775107Sfenner	/*
1538190225Srpaulo	 * Try finding a good size for the buffer; 32768 may be too
1539190225Srpaulo	 * big, so keep cutting it in half until we find a size
1540190225Srpaulo	 * that works, or run out of sizes to try.  If the default
1541190225Srpaulo	 * is larger, don't make it smaller.
1542190225Srpaulo	 *
1543190225Srpaulo	 * XXX - there should be a user-accessible hook to set the
1544190225Srpaulo	 * initial buffer size.
1545190225Srpaulo	 * Attempt to find out the version of the OS on which we're running.
1546190225Srpaulo	 */
1547190225Srpaulo	if (uname(&osinfo) == 0)
1548190225Srpaulo		have_osinfo = 1;
1549190225Srpaulo
1550190225Srpaulo#ifdef __APPLE__
1551190225Srpaulo	/*
1552190225Srpaulo	 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1553190225Srpaulo	 * of why we check the version number.
1554190225Srpaulo	 */
1555190225Srpaulo	if (p->opt.rfmon) {
1556190225Srpaulo		if (have_osinfo) {
1557190225Srpaulo			/*
1558190225Srpaulo			 * We assume osinfo.sysname is "Darwin", because
1559190225Srpaulo			 * __APPLE__ is defined.  We just check the version.
1560190225Srpaulo			 */
1561190225Srpaulo			if (osinfo.release[0] < '8' &&
1562190225Srpaulo			    osinfo.release[1] == '.') {
1563190225Srpaulo				/*
1564190225Srpaulo				 * 10.3 (Darwin 7.x) or earlier.
1565190225Srpaulo				 */
1566190225Srpaulo				status = PCAP_ERROR_RFMON_NOTSUP;
1567190225Srpaulo				goto bad;
1568190225Srpaulo			}
1569190225Srpaulo			if (osinfo.release[0] == '8' &&
1570190225Srpaulo			    osinfo.release[1] == '.') {
1571190225Srpaulo				/*
1572190225Srpaulo				 * 10.4 (Darwin 8.x).  s/en/wlt/
1573190225Srpaulo				 */
1574190225Srpaulo				if (strncmp(p->opt.source, "en", 2) != 0) {
1575190225Srpaulo					/*
1576190225Srpaulo					 * Not an enN device; check
1577190225Srpaulo					 * whether the device even exists.
1578190225Srpaulo					 */
1579190225Srpaulo					sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1580190225Srpaulo					if (sockfd != -1) {
1581235426Sdelphij						strlcpy(ifrname,
1582235426Sdelphij						    p->opt.source, ifnamsiz);
1583190225Srpaulo						if (ioctl(sockfd, SIOCGIFFLAGS,
1584190225Srpaulo						    (char *)&ifr) < 0) {
1585190225Srpaulo							/*
1586190225Srpaulo							 * We assume this
1587190225Srpaulo							 * failed because
1588190225Srpaulo							 * the underlying
1589190225Srpaulo							 * device doesn't
1590190225Srpaulo							 * exist.
1591190225Srpaulo							 */
1592190225Srpaulo							status = PCAP_ERROR_NO_SUCH_DEVICE;
1593214518Srpaulo							snprintf(p->errbuf,
1594214518Srpaulo							    PCAP_ERRBUF_SIZE,
1595214518Srpaulo							    "SIOCGIFFLAGS failed: %s",
1596214518Srpaulo							    pcap_strerror(errno));
1597190225Srpaulo						} else
1598190225Srpaulo							status = PCAP_ERROR_RFMON_NOTSUP;
1599190225Srpaulo						close(sockfd);
1600190225Srpaulo					} else {
1601190225Srpaulo						/*
1602190225Srpaulo						 * We can't find out whether
1603190225Srpaulo						 * the device exists, so just
1604190225Srpaulo						 * report "no such device".
1605190225Srpaulo						 */
1606190225Srpaulo						status = PCAP_ERROR_NO_SUCH_DEVICE;
1607214518Srpaulo						snprintf(p->errbuf,
1608214518Srpaulo						    PCAP_ERRBUF_SIZE,
1609214518Srpaulo						    "socket() failed: %s",
1610214518Srpaulo						    pcap_strerror(errno));
1611190225Srpaulo					}
1612190225Srpaulo					goto bad;
1613190225Srpaulo				}
1614190225Srpaulo				wltdev = malloc(strlen(p->opt.source) + 2);
1615190225Srpaulo				if (wltdev == NULL) {
1616190225Srpaulo					(void)snprintf(p->errbuf,
1617190225Srpaulo					    PCAP_ERRBUF_SIZE, "malloc: %s",
1618190225Srpaulo					    pcap_strerror(errno));
1619190225Srpaulo					status = PCAP_ERROR;
1620190225Srpaulo					goto bad;
1621190225Srpaulo				}
1622190225Srpaulo				strcpy(wltdev, "wlt");
1623190225Srpaulo				strcat(wltdev, p->opt.source + 2);
1624190225Srpaulo				free(p->opt.source);
1625190225Srpaulo				p->opt.source = wltdev;
1626190225Srpaulo			}
1627190225Srpaulo			/*
1628190225Srpaulo			 * Everything else is 10.5 or later; for those,
1629190225Srpaulo			 * we just open the enN device, and set the DLT.
1630190225Srpaulo			 */
1631190225Srpaulo		}
1632190225Srpaulo	}
1633190225Srpaulo#endif /* __APPLE__ */
1634190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF
1635190225Srpaulo	/*
1636183102Scsjp	 * If the BPF extension to set buffer mode is present, try setting
1637183102Scsjp	 * the mode to zero-copy.  If that fails, use regular buffering.  If
1638183102Scsjp	 * it succeeds but other setup fails, return an error to the user.
163939291Sfenner	 */
1640183102Scsjp	bufmode = BPF_BUFMODE_ZBUF;
1641183102Scsjp	if (ioctl(fd, BIOCSETBUFMODE, (caddr_t)&bufmode) == 0) {
1642190225Srpaulo		/*
1643190225Srpaulo		 * We have zerocopy BPF; use it.
1644190225Srpaulo		 */
1645190225Srpaulo		p->md.zerocopy = 1;
1646183102Scsjp
1647183102Scsjp		/*
1648183102Scsjp		 * How to pick a buffer size: first, query the maximum buffer
1649183102Scsjp		 * size supported by zero-copy.  This also lets us quickly
1650183102Scsjp		 * determine whether the kernel generally supports zero-copy.
1651190225Srpaulo		 * Then, if a buffer size was specified, use that, otherwise
1652190225Srpaulo		 * query the default buffer size, which reflects kernel
1653183102Scsjp		 * policy for a desired default.  Round to the nearest page
1654183102Scsjp		 * size.
165575107Sfenner		 */
1656183102Scsjp		if (ioctl(fd, BIOCGETZMAX, (caddr_t)&zbufmax) < 0) {
1657190225Srpaulo			snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGETZMAX: %s",
1658183102Scsjp			    pcap_strerror(errno));
1659183102Scsjp			goto bad;
1660183102Scsjp		}
1661190225Srpaulo
1662190225Srpaulo		if (p->opt.buffer_size != 0) {
1663190225Srpaulo			/*
1664190225Srpaulo			 * A buffer size was explicitly specified; use it.
1665190225Srpaulo			 */
1666190225Srpaulo			v = p->opt.buffer_size;
1667190225Srpaulo		} else {
1668190225Srpaulo			if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) ||
1669214518Srpaulo			    v < DEFAULT_BUFSIZE)
1670214518Srpaulo				v = DEFAULT_BUFSIZE;
1671190225Srpaulo		}
1672183102Scsjp#ifndef roundup
1673190225Srpaulo#define roundup(x, y)   ((((x)+((y)-1))/(y))*(y))  /* to any y */
1674183102Scsjp#endif
1675190225Srpaulo		p->md.zbufsize = roundup(v, getpagesize());
1676190225Srpaulo		if (p->md.zbufsize > zbufmax)
1677190225Srpaulo			p->md.zbufsize = zbufmax;
1678190225Srpaulo		p->md.zbuf1 = mmap(NULL, p->md.zbufsize, PROT_READ | PROT_WRITE,
1679183102Scsjp		    MAP_ANON, -1, 0);
1680190225Srpaulo		p->md.zbuf2 = mmap(NULL, p->md.zbufsize, PROT_READ | PROT_WRITE,
1681183102Scsjp		    MAP_ANON, -1, 0);
1682190225Srpaulo		if (p->md.zbuf1 == MAP_FAILED || p->md.zbuf2 == MAP_FAILED) {
1683190225Srpaulo			snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "mmap: %s",
1684183102Scsjp			    pcap_strerror(errno));
1685183102Scsjp			goto bad;
1686183102Scsjp		}
1687183102Scsjp		bzero(&bz, sizeof(bz));
1688190225Srpaulo		bz.bz_bufa = p->md.zbuf1;
1689190225Srpaulo		bz.bz_bufb = p->md.zbuf2;
1690190225Srpaulo		bz.bz_buflen = p->md.zbufsize;
1691183102Scsjp		if (ioctl(fd, BIOCSETZBUF, (caddr_t)&bz) < 0) {
1692190225Srpaulo			snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETZBUF: %s",
1693183102Scsjp			    pcap_strerror(errno));
1694183102Scsjp			goto bad;
1695183102Scsjp		}
1696235426Sdelphij		(void)strncpy(ifrname, p->opt.source, ifnamsiz);
1697183102Scsjp		if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
1698190225Srpaulo			snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s",
1699190225Srpaulo			    p->opt.source, pcap_strerror(errno));
170075107Sfenner			goto bad;
170175107Sfenner		}
1702190225Srpaulo		v = p->md.zbufsize - sizeof(struct bpf_zbuf_header);
1703190225Srpaulo	} else
1704183102Scsjp#endif
1705190225Srpaulo	{
1706183102Scsjp		/*
1707190225Srpaulo		 * We don't have zerocopy BPF.
1708190225Srpaulo		 * Set the buffer size.
1709183102Scsjp		 */
1710190225Srpaulo		if (p->opt.buffer_size != 0) {
1711190225Srpaulo			/*
1712190225Srpaulo			 * A buffer size was explicitly specified; use it.
1713183102Scsjp			 */
1714190225Srpaulo			if (ioctl(fd, BIOCSBLEN,
1715190225Srpaulo			    (caddr_t)&p->opt.buffer_size) < 0) {
1716190225Srpaulo				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1717190225Srpaulo				    "BIOCSBLEN: %s: %s", p->opt.source,
1718190225Srpaulo				    pcap_strerror(errno));
1719190225Srpaulo				status = PCAP_ERROR;
1720190225Srpaulo				goto bad;
1721190225Srpaulo			}
1722183102Scsjp
1723190225Srpaulo			/*
1724190225Srpaulo			 * Now bind to the device.
1725190225Srpaulo			 */
1726235426Sdelphij			(void)strncpy(ifrname, p->opt.source, ifnamsiz);
1727235426Sdelphij#ifdef BIOCSETLIF
1728235426Sdelphij			if (ioctl(fd, BIOCSETLIF, (caddr_t)&ifr) < 0)
1729235426Sdelphij#else
1730235426Sdelphij			if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0)
1731235426Sdelphij#endif
1732235426Sdelphij			{
1733190225Srpaulo				status = check_setif_failure(p, errno);
1734190225Srpaulo				goto bad;
1735190225Srpaulo			}
1736190225Srpaulo		} else {
1737190225Srpaulo			/*
1738190225Srpaulo			 * No buffer size was explicitly specified.
1739190225Srpaulo			 *
1740214518Srpaulo			 * Try finding a good size for the buffer;
1741214518Srpaulo			 * DEFAULT_BUFSIZE may be too big, so keep
1742214518Srpaulo			 * cutting it in half until we find a size
1743214518Srpaulo			 * that works, or run out of sizes to try.
1744190225Srpaulo			 * If the default is larger, don't make it smaller.
1745190225Srpaulo			 */
1746190225Srpaulo			if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) ||
1747214518Srpaulo			    v < DEFAULT_BUFSIZE)
1748214518Srpaulo				v = DEFAULT_BUFSIZE;
1749190225Srpaulo			for ( ; v != 0; v >>= 1) {
1750190225Srpaulo				/*
1751190225Srpaulo				 * Ignore the return value - this is because the
1752190225Srpaulo				 * call fails on BPF systems that don't have
1753190225Srpaulo				 * kernel malloc.  And if the call fails, it's
1754190225Srpaulo				 * no big deal, we just continue to use the
1755190225Srpaulo				 * standard buffer size.
1756190225Srpaulo				 */
1757190225Srpaulo				(void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
1758183102Scsjp
1759235426Sdelphij				(void)strncpy(ifrname, p->opt.source, ifnamsiz);
1760235426Sdelphij#ifdef BIOCSETLIF
1761235426Sdelphij				if (ioctl(fd, BIOCSETLIF, (caddr_t)&ifr) >= 0)
1762235426Sdelphij#else
1763190225Srpaulo				if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
1764235426Sdelphij#endif
1765190225Srpaulo					break;	/* that size worked; we're done */
1766190225Srpaulo
1767190225Srpaulo				if (errno != ENOBUFS) {
1768190225Srpaulo					status = check_setif_failure(p, errno);
1769190225Srpaulo					goto bad;
1770190225Srpaulo				}
1771190225Srpaulo			}
1772190225Srpaulo
1773190225Srpaulo			if (v == 0) {
1774190225Srpaulo				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1775190225Srpaulo				    "BIOCSBLEN: %s: No buffer size worked",
1776190225Srpaulo				    p->opt.source);
1777190225Srpaulo				status = PCAP_ERROR;
1778183102Scsjp				goto bad;
1779183102Scsjp			}
1780183102Scsjp		}
178117683Spst	}
1782183102Scsjp#endif
178375107Sfenner
178417683Spst	/* Get the data link layer type. */
178517683Spst	if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
1786190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGDLT: %s",
178775107Sfenner		    pcap_strerror(errno));
1788190225Srpaulo		status = PCAP_ERROR;
178917683Spst		goto bad;
179017683Spst	}
1791190225Srpaulo
179298530Sfenner#ifdef _AIX
179398530Sfenner	/*
179498530Sfenner	 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
179598530Sfenner	 */
179656889Sfenner	switch (v) {
179798530Sfenner
179898530Sfenner	case IFT_ETHER:
179998530Sfenner	case IFT_ISO88023:
180098530Sfenner		v = DLT_EN10MB;
180198530Sfenner		break;
180298530Sfenner
180398530Sfenner	case IFT_FDDI:
180498530Sfenner		v = DLT_FDDI;
180598530Sfenner		break;
180698530Sfenner
180798530Sfenner	case IFT_ISO88025:
180898530Sfenner		v = DLT_IEEE802;
180998530Sfenner		break;
181098530Sfenner
1811127664Sbms	case IFT_LOOP:
1812127664Sbms		v = DLT_NULL;
1813127664Sbms		break;
1814127664Sbms
181598530Sfenner	default:
181675107Sfenner		/*
181798530Sfenner		 * We don't know what to map this to yet.
181875107Sfenner		 */
1819190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown interface type %u",
182098530Sfenner		    v);
1821190225Srpaulo		status = PCAP_ERROR;
182298530Sfenner		goto bad;
182356889Sfenner	}
182456889Sfenner#endif
182539291Sfenner#if _BSDI_VERSION - 0 >= 199510
182639291Sfenner	/* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
182739291Sfenner	switch (v) {
182839291Sfenner
182939291Sfenner	case DLT_SLIP:
183039291Sfenner		v = DLT_SLIP_BSDOS;
183139291Sfenner		break;
183239291Sfenner
183339291Sfenner	case DLT_PPP:
183439291Sfenner		v = DLT_PPP_BSDOS;
183539291Sfenner		break;
183656889Sfenner
183756889Sfenner	case 11:	/*DLT_FR*/
1838127664Sbms		v = DLT_FRELAY;
183956889Sfenner		break;
184056889Sfenner
184156889Sfenner	case 12:	/*DLT_C_HDLC*/
184256889Sfenner		v = DLT_CHDLC;
184356889Sfenner		break;
184439291Sfenner	}
184539291Sfenner#endif
184617683Spst
1847127664Sbms#ifdef BIOCGDLTLIST
1848109839Sfenner	/*
1849127664Sbms	 * We know the default link type -- now determine all the DLTs
1850127664Sbms	 * this interface supports.  If this fails with EINVAL, it's
1851127664Sbms	 * not fatal; we just don't get to use the feature later.
1852109839Sfenner	 */
1853190225Srpaulo	if (get_dlt_list(fd, v, &bdl, p->errbuf) == -1) {
1854190225Srpaulo		status = PCAP_ERROR;
1855190225Srpaulo		goto bad;
1856190225Srpaulo	}
1857190225Srpaulo	p->dlt_count = bdl.bfl_len;
1858190225Srpaulo	p->dlt_list = bdl.bfl_list;
1859146768Ssam
1860190225Srpaulo#ifdef __APPLE__
1861190225Srpaulo	/*
1862190225Srpaulo	 * Monitor mode fun, continued.
1863190225Srpaulo	 *
1864190225Srpaulo	 * For 10.5 and, we're assuming, later releases, as noted above,
1865190225Srpaulo	 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
1866190225Srpaulo	 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
1867190225Srpaulo	 * DLT_ value.  Choosing one of the 802.11 DLT_ values will turn
1868190225Srpaulo	 * monitor mode on.
1869190225Srpaulo	 *
1870190225Srpaulo	 * Therefore, if the user asked for monitor mode, we filter out
1871190225Srpaulo	 * the DLT_EN10MB value, as you can't get that in monitor mode,
1872190225Srpaulo	 * and, if the user didn't ask for monitor mode, we filter out
1873190225Srpaulo	 * the 802.11 DLT_ values, because selecting those will turn
1874190225Srpaulo	 * monitor mode on.  Then, for monitor mode, if an 802.11-plus-
1875190225Srpaulo	 * radio DLT_ value is offered, we try to select that, otherwise
1876190225Srpaulo	 * we try to select DLT_IEEE802_11.
1877190225Srpaulo	 */
1878190225Srpaulo	if (have_osinfo) {
1879190225Srpaulo		if (isdigit((unsigned)osinfo.release[0]) &&
1880190225Srpaulo		     (osinfo.release[0] == '9' ||
1881190225Srpaulo		     isdigit((unsigned)osinfo.release[1]))) {
1882190225Srpaulo			/*
1883190225Srpaulo			 * 10.5 (Darwin 9.x), or later.
1884190225Srpaulo			 */
1885190225Srpaulo			new_dlt = find_802_11(&bdl);
1886190225Srpaulo			if (new_dlt != -1) {
1887190225Srpaulo				/*
1888190225Srpaulo				 * We have at least one 802.11 DLT_ value,
1889190225Srpaulo				 * so this is an 802.11 interface.
1890190225Srpaulo				 * new_dlt is the best of the 802.11
1891190225Srpaulo				 * DLT_ values in the list.
1892190225Srpaulo				 */
1893190225Srpaulo				if (p->opt.rfmon) {
1894190225Srpaulo					/*
1895190225Srpaulo					 * Our caller wants monitor mode.
1896190225Srpaulo					 * Purge DLT_EN10MB from the list
1897190225Srpaulo					 * of link-layer types, as selecting
1898190225Srpaulo					 * it will keep monitor mode off.
1899190225Srpaulo					 */
1900190225Srpaulo					remove_en(p);
1901190225Srpaulo
1902190225Srpaulo					/*
1903190225Srpaulo					 * If the new mode we want isn't
1904190225Srpaulo					 * the default mode, attempt to
1905190225Srpaulo					 * select the new mode.
1906190225Srpaulo					 */
1907190225Srpaulo					if (new_dlt != v) {
1908190225Srpaulo						if (ioctl(p->fd, BIOCSDLT,
1909190225Srpaulo						    &new_dlt) != -1) {
1910190225Srpaulo							/*
1911190225Srpaulo							 * We succeeded;
1912190225Srpaulo							 * make this the
1913190225Srpaulo							 * new DLT_ value.
1914190225Srpaulo							 */
1915190225Srpaulo							v = new_dlt;
1916190225Srpaulo						}
1917190225Srpaulo					}
1918190225Srpaulo				} else {
1919190225Srpaulo					/*
1920190225Srpaulo					 * Our caller doesn't want
1921190225Srpaulo					 * monitor mode.  Unless this
1922190225Srpaulo					 * is being done by pcap_open_live(),
1923190225Srpaulo					 * purge the 802.11 link-layer types
1924190225Srpaulo					 * from the list, as selecting
1925190225Srpaulo					 * one of them will turn monitor
1926190225Srpaulo					 * mode on.
1927190225Srpaulo					 */
1928190225Srpaulo					if (!p->oldstyle)
1929190225Srpaulo						remove_802_11(p);
1930190225Srpaulo				}
1931190225Srpaulo			} else {
1932190225Srpaulo				if (p->opt.rfmon) {
1933190225Srpaulo					/*
1934190225Srpaulo					 * The caller requested monitor
1935190225Srpaulo					 * mode, but we have no 802.11
1936190225Srpaulo					 * link-layer types, so they
1937190225Srpaulo					 * can't have it.
1938190225Srpaulo					 */
1939190225Srpaulo					status = PCAP_ERROR_RFMON_NOTSUP;
1940190225Srpaulo					goto bad;
1941190225Srpaulo				}
1942190225Srpaulo			}
1943109839Sfenner		}
1944190225Srpaulo	}
1945190225Srpaulo#elif defined(HAVE_BSD_IEEE80211)
1946190225Srpaulo	/*
1947190225Srpaulo	 * *BSD with the new 802.11 ioctls.
1948190225Srpaulo	 * Do we want monitor mode?
1949190225Srpaulo	 */
1950190225Srpaulo	if (p->opt.rfmon) {
1951190225Srpaulo		/*
1952190225Srpaulo		 * Try to put the interface into monitor mode.
1953190225Srpaulo		 */
1954190225Srpaulo		status = monitor_mode(p, 1);
1955190225Srpaulo		if (status != 0) {
1956190225Srpaulo			/*
1957190225Srpaulo			 * We failed.
1958190225Srpaulo			 */
1959109839Sfenner			goto bad;
1960109839Sfenner		}
1961109839Sfenner
1962146768Ssam		/*
1963190225Srpaulo		 * We're in monitor mode.
1964190225Srpaulo		 * Try to find the best 802.11 DLT_ value and, if we
1965190225Srpaulo		 * succeed, try to switch to that mode if we're not
1966190225Srpaulo		 * already in that mode.
1967146768Ssam		 */
1968190225Srpaulo		new_dlt = find_802_11(&bdl);
1969190225Srpaulo		if (new_dlt != -1) {
1970190225Srpaulo			/*
1971190225Srpaulo			 * We have at least one 802.11 DLT_ value.
1972190225Srpaulo			 * new_dlt is the best of the 802.11
1973190225Srpaulo			 * DLT_ values in the list.
1974190225Srpaulo			 *
1975190225Srpaulo			 * If the new mode we want isn't the default mode,
1976190225Srpaulo			 * attempt to select the new mode.
1977190225Srpaulo			 */
1978190225Srpaulo			if (new_dlt != v) {
1979190225Srpaulo				if (ioctl(p->fd, BIOCSDLT, &new_dlt) != -1) {
1980190225Srpaulo					/*
1981190225Srpaulo					 * We succeeded; make this the
1982190225Srpaulo					 * new DLT_ value.
1983190225Srpaulo					 */
1984190225Srpaulo					v = new_dlt;
1985146768Ssam				}
1986146768Ssam			}
1987146768Ssam		}
1988109839Sfenner	}
1989190225Srpaulo#endif /* various platforms */
1990190225Srpaulo#endif /* BIOCGDLTLIST */
1991109839Sfenner
1992146768Ssam	/*
1993146768Ssam	 * If this is an Ethernet device, and we don't have a DLT_ list,
1994146768Ssam	 * give it a list with DLT_EN10MB and DLT_DOCSIS.  (That'd give
1995146768Ssam	 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
1996146768Ssam	 * do, but there's not much we can do about that without finding
1997146768Ssam	 * some other way of determining whether it's an Ethernet or 802.11
1998146768Ssam	 * device.)
1999146768Ssam	 */
2000190225Srpaulo	if (v == DLT_EN10MB && p->dlt_count == 0) {
2001146768Ssam		p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
2002146768Ssam		/*
2003146768Ssam		 * If that fails, just leave the list empty.
2004146768Ssam		 */
2005146768Ssam		if (p->dlt_list != NULL) {
2006146768Ssam			p->dlt_list[0] = DLT_EN10MB;
2007146768Ssam			p->dlt_list[1] = DLT_DOCSIS;
2008146768Ssam			p->dlt_count = 2;
2009146768Ssam		}
2010146768Ssam	}
2011190225Srpaulo#ifdef PCAP_FDDIPAD
2012190225Srpaulo	if (v == DLT_FDDI)
2013190225Srpaulo		p->fddipad = PCAP_FDDIPAD;
2014190225Srpaulo	else
2015190225Srpaulo		p->fddipad = 0;
2016190225Srpaulo#endif
2017190225Srpaulo	p->linktype = v;
2018190225Srpaulo
2019146768Ssam#if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
2020146768Ssam	/*
2021146768Ssam	 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
2022146768Ssam	 * the link-layer source address isn't forcibly overwritten.
2023146768Ssam	 * (Should we ignore errors?  Should we do this only if
2024146768Ssam	 * we're open for writing?)
2025146768Ssam	 *
2026146768Ssam	 * XXX - I seem to remember some packet-sending bug in some
2027146768Ssam	 * BSDs - check CVS log for "bpf.c"?
2028146768Ssam	 */
2029146768Ssam	if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
2030190225Srpaulo		(void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2031146768Ssam		    "BIOCSHDRCMPLT: %s", pcap_strerror(errno));
2032190225Srpaulo		status = PCAP_ERROR;
2033146768Ssam		goto bad;
2034146768Ssam	}
2035146768Ssam#endif
203617683Spst	/* set timeout */
2037190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF
2038190225Srpaulo	if (p->md.timeout != 0 && !p->md.zerocopy) {
2039190225Srpaulo#else
2040190225Srpaulo	if (p->md.timeout) {
2041190225Srpaulo#endif
2042127664Sbms		/*
2043127664Sbms		 * XXX - is this seconds/nanoseconds in AIX?
2044127664Sbms		 * (Treating it as such doesn't fix the timeout
2045127664Sbms		 * problem described below.)
2046214518Srpaulo		 *
2047214518Srpaulo		 * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
2048214518Srpaulo		 * 64-bit userland - it takes, as an argument, a
2049214518Srpaulo		 * "struct BPF_TIMEVAL", which has 32-bit tv_sec
2050214518Srpaulo		 * and tv_usec, rather than a "struct timeval".
2051214518Srpaulo		 *
2052214518Srpaulo		 * If this platform defines "struct BPF_TIMEVAL",
2053214518Srpaulo		 * we check whether the structure size in BIOCSRTIMEOUT
2054214518Srpaulo		 * is that of a "struct timeval" and, if not, we use
2055214518Srpaulo		 * a "struct BPF_TIMEVAL" rather than a "struct timeval".
2056214518Srpaulo		 * (That way, if the bug is fixed in a future release,
2057214518Srpaulo		 * we will still do the right thing.)
2058127664Sbms		 */
205917683Spst		struct timeval to;
2060214518Srpaulo#ifdef HAVE_STRUCT_BPF_TIMEVAL
2061214518Srpaulo		struct BPF_TIMEVAL bpf_to;
2062214518Srpaulo
2063214518Srpaulo		if (IOCPARM_LEN(BIOCSRTIMEOUT) != sizeof(struct timeval)) {
2064214518Srpaulo			bpf_to.tv_sec = p->md.timeout / 1000;
2065214518Srpaulo			bpf_to.tv_usec = (p->md.timeout * 1000) % 1000000;
2066214518Srpaulo			if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&bpf_to) < 0) {
2067214518Srpaulo				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2068214518Srpaulo				    "BIOCSRTIMEOUT: %s", pcap_strerror(errno));
2069214518Srpaulo				status = PCAP_ERROR;
2070214518Srpaulo				goto bad;
2071214518Srpaulo			}
2072214518Srpaulo		} else {
2073214518Srpaulo#endif
2074214518Srpaulo			to.tv_sec = p->md.timeout / 1000;
2075214518Srpaulo			to.tv_usec = (p->md.timeout * 1000) % 1000000;
2076214518Srpaulo			if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) {
2077214518Srpaulo				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2078214518Srpaulo				    "BIOCSRTIMEOUT: %s", pcap_strerror(errno));
2079214518Srpaulo				status = PCAP_ERROR;
2080214518Srpaulo				goto bad;
2081214518Srpaulo			}
2082214518Srpaulo#ifdef HAVE_STRUCT_BPF_TIMEVAL
208317683Spst		}
2084214518Srpaulo#endif
208517683Spst	}
208675107Sfenner
208775107Sfenner#ifdef _AIX
208875107Sfenner#ifdef	BIOCIMMEDIATE
208975107Sfenner	/*
209075107Sfenner	 * Darren Reed notes that
209175107Sfenner	 *
209275107Sfenner	 *	On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
209375107Sfenner	 *	timeout appears to be ignored and it waits until the buffer
209475107Sfenner	 *	is filled before returning.  The result of not having it
209575107Sfenner	 *	set is almost worse than useless if your BPF filter
209675107Sfenner	 *	is reducing things to only a few packets (i.e. one every
209775107Sfenner	 *	second or so).
209875107Sfenner	 *
209975107Sfenner	 * so we turn BIOCIMMEDIATE mode on if this is AIX.
210075107Sfenner	 *
210175107Sfenner	 * We don't turn it on for other platforms, as that means we
210275107Sfenner	 * get woken up for every packet, which may not be what we want;
210375107Sfenner	 * in the Winter 1993 USENIX paper on BPF, they say:
210475107Sfenner	 *
210575107Sfenner	 *	Since a process might want to look at every packet on a
210675107Sfenner	 *	network and the time between packets can be only a few
210775107Sfenner	 *	microseconds, it is not possible to do a read system call
210875107Sfenner	 *	per packet and BPF must collect the data from several
210975107Sfenner	 *	packets and return it as a unit when the monitoring
211075107Sfenner	 *	application does a read.
211175107Sfenner	 *
211275107Sfenner	 * which I infer is the reason for the timeout - it means we
211375107Sfenner	 * wait that amount of time, in the hopes that more packets
211475107Sfenner	 * will arrive and we'll get them all with one read.
211575107Sfenner	 *
211675107Sfenner	 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
211775107Sfenner	 * BSDs) causes the timeout to be ignored.
211875107Sfenner	 *
211975107Sfenner	 * On the other hand, some platforms (e.g., Linux) don't support
212075107Sfenner	 * timeouts, they just hand stuff to you as soon as it arrives;
212175107Sfenner	 * if that doesn't cause a problem on those platforms, it may
212275107Sfenner	 * be OK to have BIOCIMMEDIATE mode on BSD as well.
212375107Sfenner	 *
212475107Sfenner	 * (Note, though, that applications may depend on the read
212575107Sfenner	 * completing, even if no packets have arrived, when the timeout
212675107Sfenner	 * expires, e.g. GUI applications that have to check for input
212775107Sfenner	 * while waiting for packets to arrive; a non-zero timeout
212875107Sfenner	 * prevents "select()" from working right on FreeBSD and
212975107Sfenner	 * possibly other BSDs, as the timer doesn't start until a
213075107Sfenner	 * "read()" is done, so the timer isn't in effect if the
213175107Sfenner	 * application is blocked on a "select()", and the "select()"
213275107Sfenner	 * doesn't get woken up for a BPF device until the buffer
213375107Sfenner	 * fills up.)
213475107Sfenner	 */
213575107Sfenner	v = 1;
213675107Sfenner	if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) {
2137190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCIMMEDIATE: %s",
213875107Sfenner		    pcap_strerror(errno));
2139190225Srpaulo		status = PCAP_ERROR;
214075107Sfenner		goto bad;
214175107Sfenner	}
214275107Sfenner#endif	/* BIOCIMMEDIATE */
214375107Sfenner#endif	/* _AIX */
214475107Sfenner
2145190225Srpaulo	if (p->opt.promisc) {
2146190225Srpaulo		/* set promiscuous mode, just warn if it fails */
214798530Sfenner		if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) {
2148190225Srpaulo			snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCPROMISC: %s",
214998530Sfenner			    pcap_strerror(errno));
2150190225Srpaulo			status = PCAP_WARNING_PROMISC_NOTSUP;
215198530Sfenner		}
215298530Sfenner	}
215317683Spst
215417683Spst	if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
2155190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGBLEN: %s",
215675107Sfenner		    pcap_strerror(errno));
2157190225Srpaulo		status = PCAP_ERROR;
215817683Spst		goto bad;
215917683Spst	}
216017683Spst	p->bufsize = v;
2161190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF
2162190225Srpaulo	if (!p->md.zerocopy) {
2163183102Scsjp#endif
2164190225Srpaulo	p->buffer = (u_char *)malloc(p->bufsize);
2165190225Srpaulo	if (p->buffer == NULL) {
2166190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s",
2167190225Srpaulo		    pcap_strerror(errno));
2168190225Srpaulo		status = PCAP_ERROR;
2169190225Srpaulo		goto bad;
2170190225Srpaulo	}
2171127664Sbms#ifdef _AIX
2172190225Srpaulo	/* For some strange reason this seems to prevent the EFAULT
2173190225Srpaulo	 * problems we have experienced from AIX BPF. */
2174190225Srpaulo	memset(p->buffer, 0x0, p->bufsize);
2175127664Sbms#endif
2176190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF
2177183102Scsjp	}
2178183102Scsjp#endif
217917683Spst
2180127664Sbms	/*
2181146768Ssam	 * If there's no filter program installed, there's
2182146768Ssam	 * no indication to the kernel of what the snapshot
2183146768Ssam	 * length should be, so no snapshotting is done.
2184146768Ssam	 *
2185146768Ssam	 * Therefore, when we open the device, we install
2186146768Ssam	 * an "accept everything" filter with the specified
2187146768Ssam	 * snapshot length.
2188146768Ssam	 */
2189146768Ssam	total_insn.code = (u_short)(BPF_RET | BPF_K);
2190146768Ssam	total_insn.jt = 0;
2191146768Ssam	total_insn.jf = 0;
2192190225Srpaulo	total_insn.k = p->snapshot;
2193146768Ssam
2194146768Ssam	total_prog.bf_len = 1;
2195146768Ssam	total_prog.bf_insns = &total_insn;
2196146768Ssam	if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) {
2197190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s",
2198146768Ssam		    pcap_strerror(errno));
2199190225Srpaulo		status = PCAP_ERROR;
2200146768Ssam		goto bad;
2201146768Ssam	}
2202146768Ssam
2203146768Ssam	/*
2204127664Sbms	 * On most BPF platforms, either you can do a "select()" or
2205127664Sbms	 * "poll()" on a BPF file descriptor and it works correctly,
2206127664Sbms	 * or you can do it and it will return "readable" if the
2207127664Sbms	 * hold buffer is full but not if the timeout expires *and*
2208127664Sbms	 * a non-blocking read will, if the hold buffer is empty
2209127664Sbms	 * but the store buffer isn't empty, rotate the buffers
2210127664Sbms	 * and return what packets are available.
2211127664Sbms	 *
2212127664Sbms	 * In the latter case, the fact that a non-blocking read
2213127664Sbms	 * will give you the available packets means you can work
2214127664Sbms	 * around the failure of "select()" and "poll()" to wake up
2215127664Sbms	 * and return "readable" when the timeout expires by using
2216127664Sbms	 * the timeout as the "select()" or "poll()" timeout, putting
2217127664Sbms	 * the BPF descriptor into non-blocking mode, and read from
2218127664Sbms	 * it regardless of whether "select()" reports it as readable
2219127664Sbms	 * or not.
2220127664Sbms	 *
2221127664Sbms	 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
2222127664Sbms	 * won't wake up and return "readable" if the timer expires
2223127664Sbms	 * and non-blocking reads return EWOULDBLOCK if the hold
2224127664Sbms	 * buffer is empty, even if the store buffer is non-empty.
2225127664Sbms	 *
2226127664Sbms	 * This means the workaround in question won't work.
2227127664Sbms	 *
2228127664Sbms	 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2229127664Sbms	 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2230127664Sbms	 * here".  On all other BPF platforms, we set it to the FD for
2231127664Sbms	 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2232127664Sbms	 * read will, if the hold buffer is empty and the store buffer
2233127664Sbms	 * isn't empty, rotate the buffers and return what packets are
2234127664Sbms	 * there (and in sufficiently recent versions of OpenBSD
2235127664Sbms	 * "select()" and "poll()" should work correctly).
2236127664Sbms	 *
2237127664Sbms	 * XXX - what about AIX?
2238127664Sbms	 */
2239147894Ssam	p->selectable_fd = p->fd;	/* assume select() works until we know otherwise */
2240190225Srpaulo	if (have_osinfo) {
2241127664Sbms		/*
2242127664Sbms		 * We can check what OS this is.
2243127664Sbms		 */
2244147894Ssam		if (strcmp(osinfo.sysname, "FreeBSD") == 0) {
2245147894Ssam			if (strncmp(osinfo.release, "4.3-", 4) == 0 ||
2246147894Ssam			     strncmp(osinfo.release, "4.4-", 4) == 0)
2247147894Ssam				p->selectable_fd = -1;
2248147894Ssam		}
2249127664Sbms	}
2250127664Sbms
2251127664Sbms	p->read_op = pcap_read_bpf;
2252146768Ssam	p->inject_op = pcap_inject_bpf;
2253127664Sbms	p->setfilter_op = pcap_setfilter_bpf;
2254147894Ssam	p->setdirection_op = pcap_setdirection_bpf;
2255127664Sbms	p->set_datalink_op = pcap_set_datalink_bpf;
2256235426Sdelphij	p->getnonblock_op = pcap_getnonblock_bpf;
2257235426Sdelphij	p->setnonblock_op = pcap_setnonblock_bpf;
2258127664Sbms	p->stats_op = pcap_stats_bpf;
2259190225Srpaulo	p->cleanup_op = pcap_cleanup_bpf;
2260127664Sbms
2261190225Srpaulo	return (status);
226217683Spst bad:
2263190225Srpaulo 	pcap_cleanup_bpf(p);
2264190225Srpaulo	return (status);
226517683Spst}
226617683Spst
226717683Spstint
2268127664Sbmspcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
226917683Spst{
2270127664Sbms#ifdef HAVE_DAG_API
2271127664Sbms	if (dag_platform_finddevs(alldevsp, errbuf) < 0)
2272127664Sbms		return (-1);
2273127664Sbms#endif /* HAVE_DAG_API */
2274214518Srpaulo#ifdef HAVE_SNF_API
2275214518Srpaulo	if (snf_platform_finddevs(alldevsp, errbuf) < 0)
2276214518Srpaulo		return (-1);
2277214518Srpaulo#endif /* HAVE_SNF_API */
2278127664Sbms
2279127664Sbms	return (0);
2280127664Sbms}
2281127664Sbms
2282190225Srpaulo#ifdef HAVE_BSD_IEEE80211
2283127664Sbmsstatic int
2284190225Srpaulomonitor_mode(pcap_t *p, int set)
2285127664Sbms{
2286190225Srpaulo	int sock;
2287190225Srpaulo	struct ifmediareq req;
2288190225Srpaulo	int *media_list;
2289190225Srpaulo	int i;
2290190225Srpaulo	int can_do;
2291190225Srpaulo	struct ifreq ifr;
2292190225Srpaulo
2293190225Srpaulo	sock = socket(AF_INET, SOCK_DGRAM, 0);
2294190225Srpaulo	if (sock == -1) {
2295190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't open socket: %s",
2296190225Srpaulo		    pcap_strerror(errno));
2297190225Srpaulo		return (PCAP_ERROR);
2298190225Srpaulo	}
2299190225Srpaulo
2300190225Srpaulo	memset(&req, 0, sizeof req);
2301190225Srpaulo	strncpy(req.ifm_name, p->opt.source, sizeof req.ifm_name);
2302190225Srpaulo
230356889Sfenner	/*
2304190225Srpaulo	 * Find out how many media types we have.
230556889Sfenner	 */
2306190225Srpaulo	if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
2307127664Sbms		/*
2308190225Srpaulo		 * Can't get the media types.
2309127664Sbms		 */
2310235426Sdelphij		switch (errno) {
2311235426Sdelphij
2312235426Sdelphij		case ENXIO:
2313190225Srpaulo			/*
2314235426Sdelphij			 * There's no such device.
2315235426Sdelphij			 */
2316235426Sdelphij			close(sock);
2317235426Sdelphij			return (PCAP_ERROR_NO_SUCH_DEVICE);
2318235426Sdelphij
2319235426Sdelphij		case EINVAL:
2320235426Sdelphij			/*
2321190225Srpaulo			 * Interface doesn't support SIOC{G,S}IFMEDIA.
2322190225Srpaulo			 */
2323190225Srpaulo			close(sock);
2324190225Srpaulo			return (PCAP_ERROR_RFMON_NOTSUP);
2325235426Sdelphij
2326235426Sdelphij		default:
2327235426Sdelphij			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2328235426Sdelphij			    "SIOCGIFMEDIA 1: %s", pcap_strerror(errno));
2329235426Sdelphij			close(sock);
2330235426Sdelphij			return (PCAP_ERROR);
2331190225Srpaulo		}
2332127664Sbms	}
2333190225Srpaulo	if (req.ifm_count == 0) {
2334190225Srpaulo		/*
2335190225Srpaulo		 * No media types.
2336190225Srpaulo		 */
2337190225Srpaulo		close(sock);
2338190225Srpaulo		return (PCAP_ERROR_RFMON_NOTSUP);
2339190225Srpaulo	}
2340127664Sbms
2341127664Sbms	/*
2342190225Srpaulo	 * Allocate a buffer to hold all the media types, and
2343190225Srpaulo	 * get the media types.
2344190225Srpaulo	 */
2345190225Srpaulo	media_list = malloc(req.ifm_count * sizeof(int));
2346190225Srpaulo	if (media_list == NULL) {
2347190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s",
2348190225Srpaulo		    pcap_strerror(errno));
2349190225Srpaulo		close(sock);
2350190225Srpaulo		return (PCAP_ERROR);
2351190225Srpaulo	}
2352190225Srpaulo	req.ifm_ulist = media_list;
2353190225Srpaulo	if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
2354190225Srpaulo		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFMEDIA: %s",
2355190225Srpaulo		    pcap_strerror(errno));
2356190225Srpaulo		free(media_list);
2357190225Srpaulo		close(sock);
2358190225Srpaulo		return (PCAP_ERROR);
2359190225Srpaulo	}
2360190225Srpaulo
2361190225Srpaulo	/*
2362190225Srpaulo	 * Look for an 802.11 "automatic" media type.
2363190225Srpaulo	 * We assume that all 802.11 adapters have that media type,
2364190225Srpaulo	 * and that it will carry the monitor mode supported flag.
2365190225Srpaulo	 */
2366190225Srpaulo	can_do = 0;
2367190225Srpaulo	for (i = 0; i < req.ifm_count; i++) {
2368190225Srpaulo		if (IFM_TYPE(media_list[i]) == IFM_IEEE80211
2369190225Srpaulo		    && IFM_SUBTYPE(media_list[i]) == IFM_AUTO) {
2370190225Srpaulo			/* OK, does it do monitor mode? */
2371190225Srpaulo			if (media_list[i] & IFM_IEEE80211_MONITOR) {
2372190225Srpaulo				can_do = 1;
2373190225Srpaulo				break;
2374190225Srpaulo			}
2375190225Srpaulo		}
2376190225Srpaulo	}
2377190225Srpaulo	free(media_list);
2378190225Srpaulo	if (!can_do) {
2379190225Srpaulo		/*
2380190225Srpaulo		 * This adapter doesn't support monitor mode.
2381190225Srpaulo		 */
2382190225Srpaulo		close(sock);
2383190225Srpaulo		return (PCAP_ERROR_RFMON_NOTSUP);
2384190225Srpaulo	}
2385190225Srpaulo
2386190225Srpaulo	if (set) {
2387190225Srpaulo		/*
2388190225Srpaulo		 * Don't just check whether we can enable monitor mode,
2389190225Srpaulo		 * do so, if it's not already enabled.
2390190225Srpaulo		 */
2391190225Srpaulo		if ((req.ifm_current & IFM_IEEE80211_MONITOR) == 0) {
2392190225Srpaulo			/*
2393190225Srpaulo			 * Monitor mode isn't currently on, so turn it on,
2394190225Srpaulo			 * and remember that we should turn it off when the
2395190225Srpaulo			 * pcap_t is closed.
2396190225Srpaulo			 */
2397190225Srpaulo
2398190225Srpaulo			/*
2399190225Srpaulo			 * If we haven't already done so, arrange to have
2400190225Srpaulo			 * "pcap_close_all()" called when we exit.
2401190225Srpaulo			 */
2402190225Srpaulo			if (!pcap_do_addexit(p)) {
2403190225Srpaulo				/*
2404190225Srpaulo				 * "atexit()" failed; don't put the interface
2405190225Srpaulo				 * in monitor mode, just give up.
2406190225Srpaulo				 */
2407190225Srpaulo				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2408190225Srpaulo				     "atexit failed");
2409190225Srpaulo				close(sock);
2410190225Srpaulo				return (PCAP_ERROR);
2411190225Srpaulo			}
2412190225Srpaulo			memset(&ifr, 0, sizeof(ifr));
2413190225Srpaulo			(void)strncpy(ifr.ifr_name, p->opt.source,
2414190225Srpaulo			    sizeof(ifr.ifr_name));
2415190225Srpaulo			ifr.ifr_media = req.ifm_current | IFM_IEEE80211_MONITOR;
2416190225Srpaulo			if (ioctl(sock, SIOCSIFMEDIA, &ifr) == -1) {
2417190225Srpaulo				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2418190225Srpaulo				     "SIOCSIFMEDIA: %s", pcap_strerror(errno));
2419190225Srpaulo				close(sock);
2420190225Srpaulo				return (PCAP_ERROR);
2421190225Srpaulo			}
2422190225Srpaulo
2423214518Srpaulo			p->md.must_do_on_close |= MUST_CLEAR_RFMON;
2424190225Srpaulo
2425190225Srpaulo			/*
2426190225Srpaulo			 * Add this to the list of pcaps to close when we exit.
2427190225Srpaulo			 */
2428190225Srpaulo			pcap_add_to_pcaps_to_close(p);
2429190225Srpaulo		}
2430190225Srpaulo	}
2431190225Srpaulo	return (0);
2432190225Srpaulo}
2433190225Srpaulo#endif /* HAVE_BSD_IEEE80211 */
2434190225Srpaulo
2435190225Srpaulo#if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
2436190225Srpaulo/*
2437190225Srpaulo * Check whether we have any 802.11 link-layer types; return the best
2438190225Srpaulo * of the 802.11 link-layer types if we find one, and return -1
2439190225Srpaulo * otherwise.
2440190225Srpaulo *
2441190225Srpaulo * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
2442190225Srpaulo * best 802.11 link-layer type; any of the other 802.11-plus-radio
2443190225Srpaulo * headers are second-best; 802.11 with no radio information is
2444190225Srpaulo * the least good.
2445190225Srpaulo */
2446190225Srpaulostatic int
2447190225Srpaulofind_802_11(struct bpf_dltlist *bdlp)
2448190225Srpaulo{
2449190225Srpaulo	int new_dlt;
2450190225Srpaulo	int i;
2451190225Srpaulo
2452190225Srpaulo	/*
2453190225Srpaulo	 * Scan the list of DLT_ values, looking for 802.11 values,
2454190225Srpaulo	 * and, if we find any, choose the best of them.
2455190225Srpaulo	 */
2456190225Srpaulo	new_dlt = -1;
2457190225Srpaulo	for (i = 0; i < bdlp->bfl_len; i++) {
2458190225Srpaulo		switch (bdlp->bfl_list[i]) {
2459190225Srpaulo
2460190225Srpaulo		case DLT_IEEE802_11:
2461190225Srpaulo			/*
2462190225Srpaulo			 * 802.11, but no radio.
2463190225Srpaulo			 *
2464190225Srpaulo			 * Offer this, and select it as the new mode
2465190225Srpaulo			 * unless we've already found an 802.11
2466190225Srpaulo			 * header with radio information.
2467190225Srpaulo			 */
2468190225Srpaulo			if (new_dlt == -1)
2469190225Srpaulo				new_dlt = bdlp->bfl_list[i];
2470190225Srpaulo			break;
2471190225Srpaulo
2472190225Srpaulo		case DLT_PRISM_HEADER:
2473190225Srpaulo		case DLT_AIRONET_HEADER:
2474190225Srpaulo		case DLT_IEEE802_11_RADIO_AVS:
2475190225Srpaulo			/*
2476190225Srpaulo			 * 802.11 with radio, but not radiotap.
2477190225Srpaulo			 *
2478190225Srpaulo			 * Offer this, and select it as the new mode
2479190225Srpaulo			 * unless we've already found the radiotap DLT_.
2480190225Srpaulo			 */
2481190225Srpaulo			if (new_dlt != DLT_IEEE802_11_RADIO)
2482190225Srpaulo				new_dlt = bdlp->bfl_list[i];
2483190225Srpaulo			break;
2484190225Srpaulo
2485190225Srpaulo		case DLT_IEEE802_11_RADIO:
2486190225Srpaulo			/*
2487190225Srpaulo			 * 802.11 with radiotap.
2488190225Srpaulo			 *
2489190225Srpaulo			 * Offer this, and select it as the new mode.
2490190225Srpaulo			 */
2491190225Srpaulo			new_dlt = bdlp->bfl_list[i];
2492190225Srpaulo			break;
2493190225Srpaulo
2494190225Srpaulo		default:
2495190225Srpaulo			/*
2496190225Srpaulo			 * Not 802.11.
2497190225Srpaulo			 */
2498190225Srpaulo			break;
2499190225Srpaulo		}
2500190225Srpaulo	}
2501190225Srpaulo
2502190225Srpaulo	return (new_dlt);
2503190225Srpaulo}
2504190225Srpaulo#endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
2505190225Srpaulo
2506190225Srpaulo#if defined(__APPLE__) && defined(BIOCGDLTLIST)
2507190225Srpaulo/*
2508214518Srpaulo * Remove DLT_EN10MB from the list of DLT_ values, as we're in monitor mode,
2509214518Srpaulo * and DLT_EN10MB isn't supported in monitor mode.
2510190225Srpaulo */
2511190225Srpaulostatic void
2512190225Srpauloremove_en(pcap_t *p)
2513190225Srpaulo{
2514190225Srpaulo	int i, j;
2515190225Srpaulo
2516190225Srpaulo	/*
2517190225Srpaulo	 * Scan the list of DLT_ values and discard DLT_EN10MB.
2518190225Srpaulo	 */
2519190225Srpaulo	j = 0;
2520190225Srpaulo	for (i = 0; i < p->dlt_count; i++) {
2521190225Srpaulo		switch (p->dlt_list[i]) {
2522190225Srpaulo
2523190225Srpaulo		case DLT_EN10MB:
2524190225Srpaulo			/*
2525190225Srpaulo			 * Don't offer this one.
2526190225Srpaulo			 */
2527190225Srpaulo			continue;
2528190225Srpaulo
2529190225Srpaulo		default:
2530190225Srpaulo			/*
2531190225Srpaulo			 * Just copy this mode over.
2532190225Srpaulo			 */
2533190225Srpaulo			break;
2534190225Srpaulo		}
2535190225Srpaulo
2536190225Srpaulo		/*
2537190225Srpaulo		 * Copy this DLT_ value to its new position.
2538190225Srpaulo		 */
2539190225Srpaulo		p->dlt_list[j] = p->dlt_list[i];
2540190225Srpaulo		j++;
2541190225Srpaulo	}
2542190225Srpaulo
2543190225Srpaulo	/*
2544190225Srpaulo	 * Set the DLT_ count to the number of entries we copied.
2545190225Srpaulo	 */
2546190225Srpaulo	p->dlt_count = j;
2547190225Srpaulo}
2548190225Srpaulo
2549190225Srpaulo/*
2550214518Srpaulo * Remove 802.11 link-layer types from the list of DLT_ values, as
2551214518Srpaulo * we're not in monitor mode, and those DLT_ values will switch us
2552214518Srpaulo * to monitor mode.
2553190225Srpaulo */
2554190225Srpaulostatic void
2555190225Srpauloremove_802_11(pcap_t *p)
2556190225Srpaulo{
2557190225Srpaulo	int i, j;
2558190225Srpaulo
2559190225Srpaulo	/*
2560190225Srpaulo	 * Scan the list of DLT_ values and discard 802.11 values.
2561190225Srpaulo	 */
2562190225Srpaulo	j = 0;
2563190225Srpaulo	for (i = 0; i < p->dlt_count; i++) {
2564190225Srpaulo		switch (p->dlt_list[i]) {
2565190225Srpaulo
2566190225Srpaulo		case DLT_IEEE802_11:
2567190225Srpaulo		case DLT_PRISM_HEADER:
2568190225Srpaulo		case DLT_AIRONET_HEADER:
2569190225Srpaulo		case DLT_IEEE802_11_RADIO:
2570190225Srpaulo		case DLT_IEEE802_11_RADIO_AVS:
2571190225Srpaulo			/*
2572190225Srpaulo			 * 802.11.  Don't offer this one.
2573190225Srpaulo			 */
2574190225Srpaulo			continue;
2575190225Srpaulo
2576190225Srpaulo		default:
2577190225Srpaulo			/*
2578190225Srpaulo			 * Just copy this mode over.
2579190225Srpaulo			 */
2580190225Srpaulo			break;
2581190225Srpaulo		}
2582190225Srpaulo
2583190225Srpaulo		/*
2584190225Srpaulo		 * Copy this DLT_ value to its new position.
2585190225Srpaulo		 */
2586190225Srpaulo		p->dlt_list[j] = p->dlt_list[i];
2587190225Srpaulo		j++;
2588190225Srpaulo	}
2589190225Srpaulo
2590190225Srpaulo	/*
2591190225Srpaulo	 * Set the DLT_ count to the number of entries we copied.
2592190225Srpaulo	 */
2593190225Srpaulo	p->dlt_count = j;
2594190225Srpaulo}
2595190225Srpaulo#endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
2596190225Srpaulo
2597190225Srpaulostatic int
2598190225Srpaulopcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp)
2599190225Srpaulo{
2600190225Srpaulo	/*
2601127664Sbms	 * Free any user-mode filter we might happen to have installed.
2602127664Sbms	 */
2603127664Sbms	pcap_freecode(&p->fcode);
2604127664Sbms
2605127664Sbms	/*
2606127664Sbms	 * Try to install the kernel filter.
2607127664Sbms	 */
2608190225Srpaulo	if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) == 0) {
2609190225Srpaulo		/*
2610190225Srpaulo		 * It worked.
2611190225Srpaulo		 */
2612190225Srpaulo		p->md.use_bpf = 1;	/* filtering in the kernel */
2613190225Srpaulo
2614190225Srpaulo		/*
2615190225Srpaulo		 * Discard any previously-received packets, as they might
2616190225Srpaulo		 * have passed whatever filter was formerly in effect, but
2617190225Srpaulo		 * might not pass this filter (BIOCSETF discards packets
2618190225Srpaulo		 * buffered in the kernel, so you can lose packets in any
2619190225Srpaulo		 * case).
2620190225Srpaulo		 */
2621190225Srpaulo		p->cc = 0;
2622190225Srpaulo		return (0);
2623190225Srpaulo	}
2624190225Srpaulo
2625190225Srpaulo	/*
2626190225Srpaulo	 * We failed.
2627190225Srpaulo	 *
2628190225Srpaulo	 * If it failed with EINVAL, that's probably because the program
2629190225Srpaulo	 * is invalid or too big.  Validate it ourselves; if we like it
2630190225Srpaulo	 * (we currently allow backward branches, to support protochain),
2631190225Srpaulo	 * run it in userland.  (There's no notion of "too big" for
2632190225Srpaulo	 * userland.)
2633190225Srpaulo	 *
2634190225Srpaulo	 * Otherwise, just give up.
2635190225Srpaulo	 * XXX - if the copy of the program into the kernel failed,
2636190225Srpaulo	 * we will get EINVAL rather than, say, EFAULT on at least
2637190225Srpaulo	 * some kernels.
2638190225Srpaulo	 */
2639190225Srpaulo	if (errno != EINVAL) {
264075107Sfenner		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s",
264175107Sfenner		    pcap_strerror(errno));
264217683Spst		return (-1);
264317683Spst	}
2644146768Ssam
2645146768Ssam	/*
2646190225Srpaulo	 * install_bpf_program() validates the program.
2647190225Srpaulo	 *
2648190225Srpaulo	 * XXX - what if we already have a filter in the kernel?
2649146768Ssam	 */
2650190225Srpaulo	if (install_bpf_program(p, fp) < 0)
2651190225Srpaulo		return (-1);
2652190225Srpaulo	p->md.use_bpf = 0;	/* filtering in userland */
265317683Spst	return (0);
265417683Spst}
2655109839Sfenner
2656147894Ssam/*
2657147894Ssam * Set direction flag: Which packets do we accept on a forwarding
2658147894Ssam * single device? IN, OUT or both?
2659147894Ssam */
2660127664Sbmsstatic int
2661162012Ssampcap_setdirection_bpf(pcap_t *p, pcap_direction_t d)
2662147894Ssam{
2663167035Sjkim#if defined(BIOCSDIRECTION)
2664167035Sjkim	u_int direction;
2665167035Sjkim
2666167035Sjkim	direction = (d == PCAP_D_IN) ? BPF_D_IN :
2667167035Sjkim	    ((d == PCAP_D_OUT) ? BPF_D_OUT : BPF_D_INOUT);
2668167035Sjkim	if (ioctl(p->fd, BIOCSDIRECTION, &direction) == -1) {
2669167035Sjkim		(void) snprintf(p->errbuf, sizeof(p->errbuf),
2670167035Sjkim		    "Cannot set direction to %s: %s",
2671167035Sjkim		        (d == PCAP_D_IN) ? "PCAP_D_IN" :
2672167035Sjkim			((d == PCAP_D_OUT) ? "PCAP_D_OUT" : "PCAP_D_INOUT"),
2673167035Sjkim			strerror(errno));
2674167035Sjkim		return (-1);
2675167035Sjkim	}
2676167035Sjkim	return (0);
2677167035Sjkim#elif defined(BIOCSSEESENT)
2678147894Ssam	u_int seesent;
2679147894Ssam
2680147894Ssam	/*
2681162012Ssam	 * We don't support PCAP_D_OUT.
2682147894Ssam	 */
2683162012Ssam	if (d == PCAP_D_OUT) {
2684147894Ssam		snprintf(p->errbuf, sizeof(p->errbuf),
2685162012Ssam		    "Setting direction to PCAP_D_OUT is not supported on BPF");
2686147894Ssam		return -1;
2687147894Ssam	}
2688167035Sjkim
2689162012Ssam	seesent = (d == PCAP_D_INOUT);
2690147894Ssam	if (ioctl(p->fd, BIOCSSEESENT, &seesent) == -1) {
2691147894Ssam		(void) snprintf(p->errbuf, sizeof(p->errbuf),
2692147894Ssam		    "Cannot set direction to %s: %s",
2693162012Ssam		        (d == PCAP_D_INOUT) ? "PCAP_D_INOUT" : "PCAP_D_IN",
2694147894Ssam			strerror(errno));
2695147894Ssam		return (-1);
2696147894Ssam	}
2697147894Ssam	return (0);
2698147894Ssam#else
2699147894Ssam	(void) snprintf(p->errbuf, sizeof(p->errbuf),
2700147894Ssam	    "This system doesn't support BIOCSSEESENT, so the direction can't be set");
2701147894Ssam	return (-1);
2702147894Ssam#endif
2703147894Ssam}
2704147894Ssam
2705147894Ssamstatic int
2706127664Sbmspcap_set_datalink_bpf(pcap_t *p, int dlt)
2707109839Sfenner{
2708127664Sbms#ifdef BIOCSDLT
2709109839Sfenner	if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) {
2710109839Sfenner		(void) snprintf(p->errbuf, sizeof(p->errbuf),
2711109839Sfenner		    "Cannot set DLT %d: %s", dlt, strerror(errno));
2712127664Sbms		return (-1);
2713109839Sfenner	}
2714127664Sbms#endif
2715127664Sbms	return (0);
2716109839Sfenner}
2717