pcap-bpf.c revision 167035
138465Smsmith/*
238465Smsmith * Copyright (c) 1993, 1994, 1995, 1996, 1998
338465Smsmith *	The Regents of the University of California.  All rights reserved.
438465Smsmith *
538465Smsmith * Redistribution and use in source and binary forms, with or without
638465Smsmith * modification, are permitted provided that: (1) source code distributions
738465Smsmith * retain the above copyright notice and this paragraph in its entirety, (2)
838465Smsmith * distributions including binary code include the above copyright notice and
938465Smsmith * this paragraph in its entirety in the documentation or other materials
1038465Smsmith * provided with the distribution, and (3) all advertising materials mentioning
1138465Smsmith * features or use of this software display the following acknowledgement:
1238465Smsmith * ``This product includes software developed by the University of California,
1338465Smsmith * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1438465Smsmith * the University nor the names of its contributors may be used to endorse
1538465Smsmith * or promote products derived from this software without specific prior
1638465Smsmith * written permission.
1738465Smsmith * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1838465Smsmith * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1938465Smsmith * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2038465Smsmith *
2138465Smsmith * $FreeBSD: head/contrib/libpcap/pcap-bpf.c 167035 2007-02-26 22:24:14Z jkim $
2238465Smsmith */
2338465Smsmith#ifndef lint
2438465Smsmithstatic const char rcsid[] _U_ =
2538465Smsmith    "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.86.2.8 2005/07/10 10:55:31 guy Exp $ (LBL)";
2638465Smsmith#endif
27119483Sobrien
28119483Sobrien#ifdef HAVE_CONFIG_H
29119483Sobrien#include "config.h"
3038465Smsmith#endif
3138465Smsmith
3238465Smsmith#include <sys/param.h>			/* optionally get BSD define */
3338465Smsmith#include <sys/time.h>
3438465Smsmith#include <sys/timeb.h>
3538465Smsmith#include <sys/socket.h>
3638465Smsmith#include <sys/file.h>
3738465Smsmith#include <sys/ioctl.h>
38146698Sjhb#include <sys/utsname.h>
39146698Sjhb
40146698Sjhb#include <net/if.h>
41294986Ssmh
42278602Sian#ifdef _AIX
4338465Smsmith
4438465Smsmith/*
45146698Sjhb * Make "pcap.h" not include "pcap-bpf.h"; we are going to include the
46146698Sjhb * native OS version, as we need "struct bpf_config" from it.
47146698Sjhb */
4838465Smsmith#define PCAP_DONT_INCLUDE_PCAP_BPF_H
4938465Smsmith
50294986Ssmh#include <sys/types.h>
5138465Smsmith
5238465Smsmith/*
5338465Smsmith * Prevent bpf.h from redefining the DLT_ values to their
5439660Smsmith * IFT_ values, as we're going to return the standard libpcap
55294986Ssmh * values, not IBM's non-standard IFT_ values.
56278602Sian */
57278602Sian#undef _AIX
58278602Sian#include <net/bpf.h>
5939660Smsmith#define _AIX
6040214Speter
6138465Smsmith#include <net/if_types.h>		/* for IFT_ values */
6238465Smsmith#include <sys/sysconfig.h>
6338465Smsmith#include <sys/device.h>
6440214Speter#include <sys/cfgodm.h>
6540214Speter#include <cf.h>
6640214Speter
6740214Speter#ifdef __64BIT__
6840214Speter#define domakedev makedev64
6940214Speter#define getmajor major64
7040214Speter#define bpf_hdr bpf_hdr32
7140214Speter#else /* __64BIT__ */
72146698Sjhb#define domakedev makedev
73146698Sjhb#define getmajor major
74146698Sjhb#endif /* __64BIT__ */
7539660Smsmith
7639660Smsmith#define BPF_NAME "bpf"
7740214Speter#define BPF_MINORS 4
7840214Speter#define DRIVER_PATH "/usr/lib/drivers"
7940214Speter#define BPF_NODE "/dev/bpf"
8039660Smsmithstatic int bpfloadedflag = 0;
8139660Smsmithstatic int odmlockid = 0;
82146698Sjhb
83146698Sjhb#else /* _AIX */
84146698Sjhb
85146698Sjhb#include <net/bpf.h>
86146698Sjhb
87146698Sjhb#endif /* _AIX */
88146698Sjhb
89146698Sjhb#include <ctype.h>
90146698Sjhb#include <errno.h>
91146698Sjhb#include <netdb.h>
92146698Sjhb#include <stdio.h>
93146698Sjhb#include <stdlib.h>
94146698Sjhb#include <string.h>
95146698Sjhb#include <unistd.h>
96146698Sjhb
97146698Sjhb#include "pcap-int.h"
9839660Smsmith
9939660Smsmith#ifdef HAVE_DAG_API
10038465Smsmith#include "pcap-dag.h"
10138465Smsmith#endif /* HAVE_DAG_API */
10238465Smsmith
10338465Smsmith#ifdef HAVE_OS_PROTO_H
10438465Smsmith#include "os-proto.h"
10538465Smsmith#endif
10638465Smsmith
107241299Savg#include "gencode.h"	/* for "no_optimize" */
10838465Smsmith
10938465Smsmithstatic int pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp);
11038465Smsmithstatic int pcap_setdirection_bpf(pcap_t *, pcap_direction_t);
111241299Savgstatic int pcap_set_datalink_bpf(pcap_t *p, int dlt);
112241299Savg
11338465Smsmithstatic int
11438465Smsmithpcap_stats_bpf(pcap_t *p, struct pcap_stat *ps)
11538465Smsmith{
11638465Smsmith	struct bpf_stat s;
11738465Smsmith
11838465Smsmith	/*
11938465Smsmith	 * "ps_recv" counts packets handed to the filter, not packets
12038465Smsmith	 * that passed the filter.  This includes packets later dropped
12138465Smsmith	 * because we ran out of buffer space.
12238465Smsmith	 *
123241299Savg	 * "ps_drop" counts packets dropped inside the BPF device
124241299Savg	 * because we ran out of buffer space.  It doesn't count
12538465Smsmith	 * packets dropped by the interface driver.  It counts
12638465Smsmith	 * only packets that passed the filter.
12738465Smsmith	 *
12838465Smsmith	 * Both statistics include packets not yet read from the kernel
12938465Smsmith	 * by libpcap, and thus not yet seen by the application.
13038465Smsmith	 */
13138465Smsmith	if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) {
13238465Smsmith		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGSTATS: %s",
13338465Smsmith		    pcap_strerror(errno));
134241299Savg		return (-1);
13538465Smsmith	}
13638465Smsmith
13738465Smsmith	ps->ps_recv = s.bs_recv;
138241299Savg	ps->ps_drop = s.bs_drop;
13938465Smsmith	return (0);
140241299Savg}
141241299Savg
14238465Smsmithstatic int
14338465Smsmithpcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
14438465Smsmith{
145146698Sjhb	int cc;
146146698Sjhb	int n = 0;
147146698Sjhb	register u_char *bp, *ep;
14838465Smsmith	u_char *datap;
149146698Sjhb	struct bpf_insn *fcode;
15038465Smsmith#ifdef PCAP_FDDIPAD
15138465Smsmith	register int pad;
152146698Sjhb#endif
15338465Smsmith
15438465Smsmith	fcode = p->md.use_bpf ? NULL : p->fcode.bf_insns;
155146698Sjhb again:
156146698Sjhb	/*
15738465Smsmith	 * Has "pcap_breakloop()" been called?
15838465Smsmith	 */
15938465Smsmith	if (p->break_loop) {
160146698Sjhb		/*
16138465Smsmith		 * Yes - clear the flag that indicates that it
16238465Smsmith		 * has, and return -2 to indicate that we were
163146698Sjhb		 * told to break out of the loop.
16438465Smsmith		 */
165294986Ssmh		p->break_loop = 0;
16638465Smsmith		return (-2);
167294986Ssmh	}
168294986Ssmh	cc = p->cc;
169294986Ssmh	if (p->cc == 0) {
170294986Ssmh		cc = read(p->fd, (char *)p->buffer, p->bufsize);
171294986Ssmh		if (cc < 0) {
172294986Ssmh			/* Don't choke when we get ptraced */
17338465Smsmith			switch (errno) {
17438465Smsmith
175294986Ssmh			case EINTR:
176294986Ssmh				goto again;
177294986Ssmh
17840214Speter#ifdef _AIX
17938465Smsmith			case EFAULT:
180294986Ssmh				/*
18138465Smsmith				 * Sigh.  More AIX wonderfulness.
182146698Sjhb				 *
183146698Sjhb				 * For some unknown reason the uiomove()
184294986Ssmh				 * operation in the bpf kernel extension
185146698Sjhb				 * used to copy the buffer into user
186146698Sjhb				 * space sometimes returns EFAULT. I have
187146698Sjhb				 * no idea why this is the case given that
188146698Sjhb				 * a kernel debugger shows the user buffer
189294986Ssmh				 * is correct. This problem appears to
190146698Sjhb				 * be mostly mitigated by the memset of
191146698Sjhb				 * the buffer before it is first used.
192146698Sjhb				 * Very strange.... Shaun Clowes
193294986Ssmh				 *
194146698Sjhb				 * In any case this means that we shouldn't
195146698Sjhb				 * treat EFAULT as a fatal error; as we
196146698Sjhb				 * don't have an API for returning
197146698Sjhb				 * a "some packets were dropped since
198294986Ssmh				 * the last packet you saw" indication,
199294986Ssmh				 * we just ignore EFAULT and keep reading.
200294986Ssmh				 */
201294986Ssmh				goto again;
202294986Ssmh#endif
203294986Ssmh
204146698Sjhb			case EWOULDBLOCK:
205146698Sjhb				return (0);
206146698Sjhb#if defined(sun) && !defined(BSD)
207146698Sjhb			/*
208294986Ssmh			 * Due to a SunOS bug, after 2^31 bytes, the kernel
209294986Ssmh			 * file offset overflows and read fails with EINVAL.
210294986Ssmh			 * The lseek() to 0 will fix things.
211294986Ssmh			 */
212294986Ssmh			case EINVAL:
213294986Ssmh				if (lseek(p->fd, 0L, SEEK_CUR) +
214294986Ssmh				    p->bufsize < 0) {
215294986Ssmh					(void)lseek(p->fd, 0L, SEEK_SET);
216294986Ssmh					goto again;
217294986Ssmh				}
218294986Ssmh				/* fall through */
219146698Sjhb#endif
220146698Sjhb			}
221146698Sjhb			snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read: %s",
222294986Ssmh			    pcap_strerror(errno));
223146698Sjhb			return (-1);
224294986Ssmh		}
225146698Sjhb		bp = p->buffer;
226146698Sjhb	} else
227294986Ssmh		bp = p->bp;
228146698Sjhb
229146698Sjhb	/*
230146698Sjhb	 * Loop through each packet.
231146698Sjhb	 */
232146698Sjhb#define bhp ((struct bpf_hdr *)bp)
233146698Sjhb	ep = bp + cc;
234146698Sjhb#ifdef PCAP_FDDIPAD
235146698Sjhb	pad = p->fddipad;
236146698Sjhb#endif
237294986Ssmh	while (bp < ep) {
238146698Sjhb		register int caplen, hdrlen;
239146698Sjhb
240146698Sjhb		/*
241146698Sjhb		 * Has "pcap_breakloop()" been called?
242146698Sjhb		 * If so, return immediately - if we haven't read any
243148516Sbrian		 * packets, clear the flag and return -2 to indicate
244146698Sjhb		 * that we were told to break out of the loop, otherwise
245146698Sjhb		 * leave the flag set, so that the *next* call will break
246294986Ssmh		 * out of the loop without having read any packets, and
247294986Ssmh		 * return the number of packets we've processed so far.
248294986Ssmh		 */
249294986Ssmh		if (p->break_loop) {
250294986Ssmh			if (n == 0) {
251294986Ssmh				p->break_loop = 0;
252294986Ssmh				return (-2);
253294986Ssmh			} else {
254294986Ssmh				p->bp = bp;
255294986Ssmh				p->cc = ep - bp;
256294986Ssmh				return (n);
257294986Ssmh			}
258294986Ssmh		}
259294986Ssmh
260294986Ssmh		caplen = bhp->bh_caplen;
261294986Ssmh		hdrlen = bhp->bh_hdrlen;
262294986Ssmh		datap = bp + hdrlen;
263294986Ssmh		/*
264294986Ssmh		 * Short-circuit evaluation: if using BPF filter
265294986Ssmh		 * in kernel, no need to do it now.
266294986Ssmh		 *
267294986Ssmh#ifdef PCAP_FDDIPAD
268241299Savg		 * Note: the filter code was generated assuming
269294986Ssmh		 * that p->fddipad was the amount of padding
270146698Sjhb		 * before the header, as that's what's required
271294986Ssmh		 * in the kernel, so we run the filter before
272294986Ssmh		 * skipping that padding.
273294986Ssmh#endif
274146698Sjhb		 */
275146698Sjhb		if (fcode == NULL ||
276294986Ssmh		    bpf_filter(fcode, datap, bhp->bh_datalen, caplen)) {
277146698Sjhb			struct pcap_pkthdr pkthdr;
278278602Sian
279278602Sian			pkthdr.ts.tv_sec = bhp->bh_tstamp.tv_sec;
280278602Sian#ifdef _AIX
281278602Sian			/*
282278602Sian			 * AIX's BPF returns seconds/nanoseconds time
283278602Sian			 * stamps, not seconds/microseconds time stamps.
284278602Sian			 */
285278602Sian			pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec/1000;
286278602Sian#else
287278602Sian			pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec;
288278602Sian#endif
289278602Sian#ifdef PCAP_FDDIPAD
290278602Sian			if (caplen > pad)
291278602Sian				pkthdr.caplen = caplen - pad;
292278602Sian			else
293278602Sian				pkthdr.caplen = 0;
294278602Sian			if (bhp->bh_datalen > pad)
295278602Sian				pkthdr.len = bhp->bh_datalen - pad;
296278602Sian			else
297278602Sian				pkthdr.len = 0;
298278602Sian			datap += pad;
299278602Sian#else
300278602Sian			pkthdr.caplen = caplen;
301278602Sian			pkthdr.len = bhp->bh_datalen;
302278602Sian#endif
303			(*callback)(user, &pkthdr, datap);
304			bp += BPF_WORDALIGN(caplen + hdrlen);
305			if (++n >= cnt && cnt > 0) {
306				p->bp = bp;
307				p->cc = ep - bp;
308				return (n);
309			}
310		} else {
311			/*
312			 * Skip this packet.
313			 */
314			bp += BPF_WORDALIGN(caplen + hdrlen);
315		}
316	}
317#undef bhp
318	p->cc = 0;
319	return (n);
320}
321
322static int
323pcap_inject_bpf(pcap_t *p, const void *buf, size_t size)
324{
325	int ret;
326
327	ret = write(p->fd, buf, size);
328#ifdef __APPLE__
329	if (ret == -1 && errno == EAFNOSUPPORT) {
330		/*
331		 * In Mac OS X, there's a bug wherein setting the
332		 * BIOCSHDRCMPLT flag causes writes to fail; see,
333		 * for example:
334		 *
335		 *	http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
336		 *
337		 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
338		 * assume it's due to that bug, and turn off that flag
339		 * and try again.  If we succeed, it either means that
340		 * somebody applied the fix from that URL, or other patches
341		 * for that bug from
342		 *
343		 *	http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
344		 *
345		 * and are running a Darwin kernel with those fixes, or
346		 * that Apple fixed the problem in some OS X release.
347		 */
348		u_int spoof_eth_src = 0;
349
350		if (ioctl(p->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
351			(void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
352			    "send: can't turn off BIOCSHDRCMPLT: %s",
353			    pcap_strerror(errno));
354			return (-1);
355		}
356
357		/*
358		 * Now try the write again.
359		 */
360		ret = write(p->fd, buf, size);
361	}
362#endif /* __APPLE__ */
363	if (ret == -1) {
364		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
365		    pcap_strerror(errno));
366		return (-1);
367	}
368	return (ret);
369}
370
371#ifdef _AIX
372static int
373bpf_odminit(char *errbuf)
374{
375	char *errstr;
376
377	if (odm_initialize() == -1) {
378		if (odm_err_msg(odmerrno, &errstr) == -1)
379			errstr = "Unknown error";
380		snprintf(errbuf, PCAP_ERRBUF_SIZE,
381		    "bpf_load: odm_initialize failed: %s",
382		    errstr);
383		return (-1);
384	}
385
386	if ((odmlockid = odm_lock("/etc/objrepos/config_lock", ODM_WAIT)) == -1) {
387		if (odm_err_msg(odmerrno, &errstr) == -1)
388			errstr = "Unknown error";
389		snprintf(errbuf, PCAP_ERRBUF_SIZE,
390		    "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
391		    errstr);
392		return (-1);
393	}
394
395	return (0);
396}
397
398static int
399bpf_odmcleanup(char *errbuf)
400{
401	char *errstr;
402
403	if (odm_unlock(odmlockid) == -1) {
404		if (odm_err_msg(odmerrno, &errstr) == -1)
405			errstr = "Unknown error";
406		snprintf(errbuf, PCAP_ERRBUF_SIZE,
407		    "bpf_load: odm_unlock failed: %s",
408		    errstr);
409		return (-1);
410	}
411
412	if (odm_terminate() == -1) {
413		if (odm_err_msg(odmerrno, &errstr) == -1)
414			errstr = "Unknown error";
415		snprintf(errbuf, PCAP_ERRBUF_SIZE,
416		    "bpf_load: odm_terminate failed: %s",
417		    errstr);
418		return (-1);
419	}
420
421	return (0);
422}
423
424static int
425bpf_load(char *errbuf)
426{
427	long major;
428	int *minors;
429	int numminors, i, rc;
430	char buf[1024];
431	struct stat sbuf;
432	struct bpf_config cfg_bpf;
433	struct cfg_load cfg_ld;
434	struct cfg_kmod cfg_km;
435
436	/*
437	 * This is very very close to what happens in the real implementation
438	 * but I've fixed some (unlikely) bug situations.
439	 */
440	if (bpfloadedflag)
441		return (0);
442
443	if (bpf_odminit(errbuf) != 0)
444		return (-1);
445
446	major = genmajor(BPF_NAME);
447	if (major == -1) {
448		snprintf(errbuf, PCAP_ERRBUF_SIZE,
449		    "bpf_load: genmajor failed: %s", pcap_strerror(errno));
450		return (-1);
451	}
452
453	minors = getminor(major, &numminors, BPF_NAME);
454	if (!minors) {
455		minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1);
456		if (!minors) {
457			snprintf(errbuf, PCAP_ERRBUF_SIZE,
458			    "bpf_load: genminor failed: %s",
459			    pcap_strerror(errno));
460			return (-1);
461		}
462	}
463
464	if (bpf_odmcleanup(errbuf))
465		return (-1);
466
467	rc = stat(BPF_NODE "0", &sbuf);
468	if (rc == -1 && errno != ENOENT) {
469		snprintf(errbuf, PCAP_ERRBUF_SIZE,
470		    "bpf_load: can't stat %s: %s",
471		    BPF_NODE "0", pcap_strerror(errno));
472		return (-1);
473	}
474
475	if (rc == -1 || getmajor(sbuf.st_rdev) != major) {
476		for (i = 0; i < BPF_MINORS; i++) {
477			sprintf(buf, "%s%d", BPF_NODE, i);
478			unlink(buf);
479			if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) {
480				snprintf(errbuf, PCAP_ERRBUF_SIZE,
481				    "bpf_load: can't mknod %s: %s",
482				    buf, pcap_strerror(errno));
483				return (-1);
484			}
485		}
486	}
487
488	/* Check if the driver is loaded */
489	memset(&cfg_ld, 0x0, sizeof(cfg_ld));
490	cfg_ld.path = buf;
491	sprintf(cfg_ld.path, "%s/%s", DRIVER_PATH, BPF_NAME);
492	if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) ||
493	    (cfg_ld.kmid == 0)) {
494		/* Driver isn't loaded, load it now */
495		if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) {
496			snprintf(errbuf, PCAP_ERRBUF_SIZE,
497			    "bpf_load: could not load driver: %s",
498			    strerror(errno));
499			return (-1);
500		}
501	}
502
503	/* Configure the driver */
504	cfg_km.cmd = CFG_INIT;
505	cfg_km.kmid = cfg_ld.kmid;
506	cfg_km.mdilen = sizeof(cfg_bpf);
507	cfg_km.mdiptr = (void *)&cfg_bpf;
508	for (i = 0; i < BPF_MINORS; i++) {
509		cfg_bpf.devno = domakedev(major, i);
510		if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) {
511			snprintf(errbuf, PCAP_ERRBUF_SIZE,
512			    "bpf_load: could not configure driver: %s",
513			    strerror(errno));
514			return (-1);
515		}
516	}
517
518	bpfloadedflag = 1;
519
520	return (0);
521}
522#endif
523
524static inline int
525bpf_open(pcap_t *p, char *errbuf)
526{
527	int fd;
528	int n = 0;
529	char device[sizeof "/dev/bpf0000000000"];
530
531#ifdef _AIX
532	/*
533	 * Load the bpf driver, if it isn't already loaded,
534	 * and create the BPF device entries, if they don't
535	 * already exist.
536	 */
537	if (bpf_load(errbuf) == -1)
538		return (-1);
539#endif
540
541	/*
542	 * Go through all the minors and find one that isn't in use.
543	 */
544	do {
545		(void)snprintf(device, sizeof(device), "/dev/bpf%d", n++);
546		/*
547		 * Initially try a read/write open (to allow the inject
548		 * method to work).  If that fails due to permission
549		 * issues, fall back to read-only.  This allows a
550		 * non-root user to be granted specific access to pcap
551		 * capabilities via file permissions.
552		 *
553		 * XXX - we should have an API that has a flag that
554		 * controls whether to open read-only or read-write,
555		 * so that denial of permission to send (or inability
556		 * to send, if sending packets isn't supported on
557		 * the device in question) can be indicated at open
558		 * time.
559		 */
560		fd = open(device, O_RDWR);
561		if (fd == -1 && errno == EACCES)
562			fd = open(device, O_RDONLY);
563	} while (fd < 0 && errno == EBUSY);
564
565	/*
566	 * XXX better message for all minors used
567	 */
568	if (fd < 0)
569		snprintf(errbuf, PCAP_ERRBUF_SIZE, "(no devices found) %s: %s",
570		    device, pcap_strerror(errno));
571
572	return (fd);
573}
574
575/*
576 * We include the OS's <net/bpf.h>, not our "pcap-bpf.h", so we probably
577 * don't get DLT_DOCSIS defined.
578 */
579#ifndef DLT_DOCSIS
580#define DLT_DOCSIS	143
581#endif
582
583pcap_t *
584pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
585    char *ebuf)
586{
587	int fd;
588	struct ifreq ifr;
589	struct bpf_version bv;
590#ifdef BIOCGDLTLIST
591	struct bpf_dltlist bdl;
592#endif
593#if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
594	u_int spoof_eth_src = 1;
595#endif
596	u_int v;
597	pcap_t *p;
598	struct bpf_insn total_insn;
599	struct bpf_program total_prog;
600	struct utsname osinfo;
601
602#ifdef HAVE_DAG_API
603	if (strstr(device, "dag")) {
604		return dag_open_live(device, snaplen, promisc, to_ms, ebuf);
605	}
606#endif /* HAVE_DAG_API */
607
608#ifdef BIOCGDLTLIST
609	memset(&bdl, 0, sizeof(bdl));
610#endif
611
612	p = (pcap_t *)malloc(sizeof(*p));
613	if (p == NULL) {
614		snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
615		    pcap_strerror(errno));
616		return (NULL);
617	}
618	memset(p, 0, sizeof(*p));
619	fd = bpf_open(p, ebuf);
620	if (fd < 0)
621		goto bad;
622
623	p->fd = fd;
624	p->snapshot = snaplen;
625
626	if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) {
627		snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCVERSION: %s",
628		    pcap_strerror(errno));
629		goto bad;
630	}
631	if (bv.bv_major != BPF_MAJOR_VERSION ||
632	    bv.bv_minor < BPF_MINOR_VERSION) {
633		snprintf(ebuf, PCAP_ERRBUF_SIZE,
634		    "kernel bpf filter out of date");
635		goto bad;
636	}
637
638	/*
639	 * Try finding a good size for the buffer; 32768 may be too
640	 * big, so keep cutting it in half until we find a size
641	 * that works, or run out of sizes to try.  If the default
642	 * is larger, don't make it smaller.
643	 *
644	 * XXX - there should be a user-accessible hook to set the
645	 * initial buffer size.
646	 */
647	if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) || v < 32768)
648		v = 32768;
649	for ( ; v != 0; v >>= 1) {
650		/* Ignore the return value - this is because the call fails
651		 * on BPF systems that don't have kernel malloc.  And if
652		 * the call fails, it's no big deal, we just continue to
653		 * use the standard buffer size.
654		 */
655		(void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
656
657		(void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
658		if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
659			break;	/* that size worked; we're done */
660
661		if (errno != ENOBUFS) {
662			snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s",
663			    device, pcap_strerror(errno));
664			goto bad;
665		}
666	}
667
668	if (v == 0) {
669		snprintf(ebuf, PCAP_ERRBUF_SIZE,
670			 "BIOCSBLEN: %s: No buffer size worked", device);
671		goto bad;
672	}
673
674	/* Get the data link layer type. */
675	if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
676		snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCGDLT: %s",
677		    pcap_strerror(errno));
678		goto bad;
679	}
680#ifdef _AIX
681	/*
682	 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
683	 */
684	switch (v) {
685
686	case IFT_ETHER:
687	case IFT_ISO88023:
688		v = DLT_EN10MB;
689		break;
690
691	case IFT_FDDI:
692		v = DLT_FDDI;
693		break;
694
695	case IFT_ISO88025:
696		v = DLT_IEEE802;
697		break;
698
699	case IFT_LOOP:
700		v = DLT_NULL;
701		break;
702
703	default:
704		/*
705		 * We don't know what to map this to yet.
706		 */
707		snprintf(ebuf, PCAP_ERRBUF_SIZE, "unknown interface type %u",
708		    v);
709		goto bad;
710	}
711#endif
712#if _BSDI_VERSION - 0 >= 199510
713	/* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
714	switch (v) {
715
716	case DLT_SLIP:
717		v = DLT_SLIP_BSDOS;
718		break;
719
720	case DLT_PPP:
721		v = DLT_PPP_BSDOS;
722		break;
723
724	case 11:	/*DLT_FR*/
725		v = DLT_FRELAY;
726		break;
727
728	case 12:	/*DLT_C_HDLC*/
729		v = DLT_CHDLC;
730		break;
731	}
732#endif
733#ifdef PCAP_FDDIPAD
734	if (v == DLT_FDDI)
735		p->fddipad = PCAP_FDDIPAD;
736	else
737		p->fddipad = 0;
738#endif
739	p->linktype = v;
740
741#ifdef BIOCGDLTLIST
742	/*
743	 * We know the default link type -- now determine all the DLTs
744	 * this interface supports.  If this fails with EINVAL, it's
745	 * not fatal; we just don't get to use the feature later.
746	 */
747	if (ioctl(fd, BIOCGDLTLIST, (caddr_t)&bdl) == 0) {
748		u_int i;
749		int is_ethernet;
750
751		bdl.bfl_list = (u_int *) malloc(sizeof(u_int) * (bdl.bfl_len + 1));
752		if (bdl.bfl_list == NULL) {
753			(void)snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
754			    pcap_strerror(errno));
755			goto bad;
756		}
757
758		if (ioctl(fd, BIOCGDLTLIST, (caddr_t)&bdl) < 0) {
759			(void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
760			    "BIOCGDLTLIST: %s", pcap_strerror(errno));
761			free(bdl.bfl_list);
762			goto bad;
763		}
764
765		/*
766		 * OK, for real Ethernet devices, add DLT_DOCSIS to the
767		 * list, so that an application can let you choose it,
768		 * in case you're capturing DOCSIS traffic that a Cisco
769		 * Cable Modem Termination System is putting out onto
770		 * an Ethernet (it doesn't put an Ethernet header onto
771		 * the wire, it puts raw DOCSIS frames out on the wire
772		 * inside the low-level Ethernet framing).
773		 *
774		 * A "real Ethernet device" is defined here as a device
775		 * that has a link-layer type of DLT_EN10MB and that has
776		 * no alternate link-layer types; that's done to exclude
777		 * 802.11 interfaces (which might or might not be the
778		 * right thing to do, but I suspect it is - Ethernet <->
779		 * 802.11 bridges would probably badly mishandle frames
780		 * that don't have Ethernet headers).
781		 */
782		if (p->linktype == DLT_EN10MB) {
783			is_ethernet = 1;
784			for (i = 0; i < bdl.bfl_len; i++) {
785				if (bdl.bfl_list[i] != DLT_EN10MB) {
786					is_ethernet = 0;
787					break;
788				}
789			}
790			if (is_ethernet) {
791				/*
792				 * We reserved one more slot at the end of
793				 * the list.
794				 */
795				bdl.bfl_list[bdl.bfl_len] = DLT_DOCSIS;
796				bdl.bfl_len++;
797			}
798		}
799		p->dlt_count = bdl.bfl_len;
800		p->dlt_list = bdl.bfl_list;
801	} else {
802		if (errno != EINVAL) {
803			(void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
804			    "BIOCGDLTLIST: %s", pcap_strerror(errno));
805			goto bad;
806		}
807	}
808#endif
809
810	/*
811	 * If this is an Ethernet device, and we don't have a DLT_ list,
812	 * give it a list with DLT_EN10MB and DLT_DOCSIS.  (That'd give
813	 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
814	 * do, but there's not much we can do about that without finding
815	 * some other way of determining whether it's an Ethernet or 802.11
816	 * device.)
817	 */
818	if (p->linktype == DLT_EN10MB && p->dlt_count == 0) {
819		p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
820		/*
821		 * If that fails, just leave the list empty.
822		 */
823		if (p->dlt_list != NULL) {
824			p->dlt_list[0] = DLT_EN10MB;
825			p->dlt_list[1] = DLT_DOCSIS;
826			p->dlt_count = 2;
827		}
828	}
829
830#if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
831	/*
832	 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
833	 * the link-layer source address isn't forcibly overwritten.
834	 * (Should we ignore errors?  Should we do this only if
835	 * we're open for writing?)
836	 *
837	 * XXX - I seem to remember some packet-sending bug in some
838	 * BSDs - check CVS log for "bpf.c"?
839	 */
840	if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
841		(void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
842		    "BIOCSHDRCMPLT: %s", pcap_strerror(errno));
843		goto bad;
844	}
845#endif
846	/* set timeout */
847	if (to_ms != 0) {
848		/*
849		 * XXX - is this seconds/nanoseconds in AIX?
850		 * (Treating it as such doesn't fix the timeout
851		 * problem described below.)
852		 */
853		struct timeval to;
854		to.tv_sec = to_ms / 1000;
855		to.tv_usec = (to_ms * 1000) % 1000000;
856		if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) {
857			snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT: %s",
858			    pcap_strerror(errno));
859			goto bad;
860		}
861	}
862
863#ifdef _AIX
864#ifdef	BIOCIMMEDIATE
865	/*
866	 * Darren Reed notes that
867	 *
868	 *	On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
869	 *	timeout appears to be ignored and it waits until the buffer
870	 *	is filled before returning.  The result of not having it
871	 *	set is almost worse than useless if your BPF filter
872	 *	is reducing things to only a few packets (i.e. one every
873	 *	second or so).
874	 *
875	 * so we turn BIOCIMMEDIATE mode on if this is AIX.
876	 *
877	 * We don't turn it on for other platforms, as that means we
878	 * get woken up for every packet, which may not be what we want;
879	 * in the Winter 1993 USENIX paper on BPF, they say:
880	 *
881	 *	Since a process might want to look at every packet on a
882	 *	network and the time between packets can be only a few
883	 *	microseconds, it is not possible to do a read system call
884	 *	per packet and BPF must collect the data from several
885	 *	packets and return it as a unit when the monitoring
886	 *	application does a read.
887	 *
888	 * which I infer is the reason for the timeout - it means we
889	 * wait that amount of time, in the hopes that more packets
890	 * will arrive and we'll get them all with one read.
891	 *
892	 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
893	 * BSDs) causes the timeout to be ignored.
894	 *
895	 * On the other hand, some platforms (e.g., Linux) don't support
896	 * timeouts, they just hand stuff to you as soon as it arrives;
897	 * if that doesn't cause a problem on those platforms, it may
898	 * be OK to have BIOCIMMEDIATE mode on BSD as well.
899	 *
900	 * (Note, though, that applications may depend on the read
901	 * completing, even if no packets have arrived, when the timeout
902	 * expires, e.g. GUI applications that have to check for input
903	 * while waiting for packets to arrive; a non-zero timeout
904	 * prevents "select()" from working right on FreeBSD and
905	 * possibly other BSDs, as the timer doesn't start until a
906	 * "read()" is done, so the timer isn't in effect if the
907	 * application is blocked on a "select()", and the "select()"
908	 * doesn't get woken up for a BPF device until the buffer
909	 * fills up.)
910	 */
911	v = 1;
912	if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) {
913		snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCIMMEDIATE: %s",
914		    pcap_strerror(errno));
915		goto bad;
916	}
917#endif	/* BIOCIMMEDIATE */
918#endif	/* _AIX */
919
920	if (promisc) {
921		/* set promiscuous mode, okay if it fails */
922		if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) {
923			snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCPROMISC: %s",
924			    pcap_strerror(errno));
925		}
926	}
927
928	if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
929		snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCGBLEN: %s",
930		    pcap_strerror(errno));
931		goto bad;
932	}
933	p->bufsize = v;
934	p->buffer = (u_char *)malloc(p->bufsize);
935	if (p->buffer == NULL) {
936		snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
937		    pcap_strerror(errno));
938		goto bad;
939	}
940#ifdef _AIX
941	/* For some strange reason this seems to prevent the EFAULT
942	 * problems we have experienced from AIX BPF. */
943	memset(p->buffer, 0x0, p->bufsize);
944#endif
945
946	/*
947	 * If there's no filter program installed, there's
948	 * no indication to the kernel of what the snapshot
949	 * length should be, so no snapshotting is done.
950	 *
951	 * Therefore, when we open the device, we install
952	 * an "accept everything" filter with the specified
953	 * snapshot length.
954	 */
955	total_insn.code = (u_short)(BPF_RET | BPF_K);
956	total_insn.jt = 0;
957	total_insn.jf = 0;
958	total_insn.k = snaplen;
959
960	total_prog.bf_len = 1;
961	total_prog.bf_insns = &total_insn;
962	if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) {
963		snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s",
964		    pcap_strerror(errno));
965		goto bad;
966	}
967
968	/*
969	 * On most BPF platforms, either you can do a "select()" or
970	 * "poll()" on a BPF file descriptor and it works correctly,
971	 * or you can do it and it will return "readable" if the
972	 * hold buffer is full but not if the timeout expires *and*
973	 * a non-blocking read will, if the hold buffer is empty
974	 * but the store buffer isn't empty, rotate the buffers
975	 * and return what packets are available.
976	 *
977	 * In the latter case, the fact that a non-blocking read
978	 * will give you the available packets means you can work
979	 * around the failure of "select()" and "poll()" to wake up
980	 * and return "readable" when the timeout expires by using
981	 * the timeout as the "select()" or "poll()" timeout, putting
982	 * the BPF descriptor into non-blocking mode, and read from
983	 * it regardless of whether "select()" reports it as readable
984	 * or not.
985	 *
986	 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
987	 * won't wake up and return "readable" if the timer expires
988	 * and non-blocking reads return EWOULDBLOCK if the hold
989	 * buffer is empty, even if the store buffer is non-empty.
990	 *
991	 * This means the workaround in question won't work.
992	 *
993	 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
994	 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
995	 * here".  On all other BPF platforms, we set it to the FD for
996	 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
997	 * read will, if the hold buffer is empty and the store buffer
998	 * isn't empty, rotate the buffers and return what packets are
999	 * there (and in sufficiently recent versions of OpenBSD
1000	 * "select()" and "poll()" should work correctly).
1001	 *
1002	 * XXX - what about AIX?
1003	 */
1004	p->selectable_fd = p->fd;	/* assume select() works until we know otherwise */
1005	if (uname(&osinfo) == 0) {
1006		/*
1007		 * We can check what OS this is.
1008		 */
1009		if (strcmp(osinfo.sysname, "FreeBSD") == 0) {
1010			if (strncmp(osinfo.release, "4.3-", 4) == 0 ||
1011			     strncmp(osinfo.release, "4.4-", 4) == 0)
1012				p->selectable_fd = -1;
1013		}
1014	}
1015
1016	p->read_op = pcap_read_bpf;
1017	p->inject_op = pcap_inject_bpf;
1018	p->setfilter_op = pcap_setfilter_bpf;
1019	p->setdirection_op = pcap_setdirection_bpf;
1020	p->set_datalink_op = pcap_set_datalink_bpf;
1021	p->getnonblock_op = pcap_getnonblock_fd;
1022	p->setnonblock_op = pcap_setnonblock_fd;
1023	p->stats_op = pcap_stats_bpf;
1024	p->close_op = pcap_close_common;
1025
1026	return (p);
1027 bad:
1028	(void)close(fd);
1029	if (p->dlt_list != NULL)
1030		free(p->dlt_list);
1031	free(p);
1032	return (NULL);
1033}
1034
1035int
1036pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
1037{
1038#ifdef HAVE_DAG_API
1039	if (dag_platform_finddevs(alldevsp, errbuf) < 0)
1040		return (-1);
1041#endif /* HAVE_DAG_API */
1042
1043	return (0);
1044}
1045
1046static int
1047pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp)
1048{
1049	/*
1050	 * It looks that BPF code generated by gen_protochain() is not
1051	 * compatible with some of kernel BPF code (for example BSD/OS 3.1).
1052	 * Take a safer side for now.
1053	 */
1054	if (no_optimize) {
1055		/*
1056		 * XXX - what if we already have a filter in the kernel?
1057		 */
1058		if (install_bpf_program(p, fp) < 0)
1059			return (-1);
1060		p->md.use_bpf = 0;	/* filtering in userland */
1061		return (0);
1062	}
1063
1064	/*
1065	 * Free any user-mode filter we might happen to have installed.
1066	 */
1067	pcap_freecode(&p->fcode);
1068
1069	/*
1070	 * Try to install the kernel filter.
1071	 */
1072	if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) {
1073		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s",
1074		    pcap_strerror(errno));
1075		return (-1);
1076	}
1077	p->md.use_bpf = 1;	/* filtering in the kernel */
1078
1079	/*
1080	 * Discard any previously-received packets, as they might have
1081	 * passed whatever filter was formerly in effect, but might
1082	 * not pass this filter (BIOCSETF discards packets buffered
1083	 * in the kernel, so you can lose packets in any case).
1084	 */
1085	p->cc = 0;
1086	return (0);
1087}
1088
1089/*
1090 * Set direction flag: Which packets do we accept on a forwarding
1091 * single device? IN, OUT or both?
1092 */
1093static int
1094pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d)
1095{
1096#if defined(BIOCSDIRECTION)
1097	u_int direction;
1098
1099	direction = (d == PCAP_D_IN) ? BPF_D_IN :
1100	    ((d == PCAP_D_OUT) ? BPF_D_OUT : BPF_D_INOUT);
1101	if (ioctl(p->fd, BIOCSDIRECTION, &direction) == -1) {
1102		(void) snprintf(p->errbuf, sizeof(p->errbuf),
1103		    "Cannot set direction to %s: %s",
1104		        (d == PCAP_D_IN) ? "PCAP_D_IN" :
1105			((d == PCAP_D_OUT) ? "PCAP_D_OUT" : "PCAP_D_INOUT"),
1106			strerror(errno));
1107		return (-1);
1108	}
1109	return (0);
1110#elif defined(BIOCSSEESENT)
1111	u_int seesent;
1112
1113	/*
1114	 * We don't support PCAP_D_OUT.
1115	 */
1116	if (d == PCAP_D_OUT) {
1117		snprintf(p->errbuf, sizeof(p->errbuf),
1118		    "Setting direction to PCAP_D_OUT is not supported on BPF");
1119		return -1;
1120	}
1121
1122	seesent = (d == PCAP_D_INOUT);
1123	if (ioctl(p->fd, BIOCSSEESENT, &seesent) == -1) {
1124		(void) snprintf(p->errbuf, sizeof(p->errbuf),
1125		    "Cannot set direction to %s: %s",
1126		        (d == PCAP_D_INOUT) ? "PCAP_D_INOUT" : "PCAP_D_IN",
1127			strerror(errno));
1128		return (-1);
1129	}
1130	return (0);
1131#else
1132	(void) snprintf(p->errbuf, sizeof(p->errbuf),
1133	    "This system doesn't support BIOCSSEESENT, so the direction can't be set");
1134	return (-1);
1135#endif
1136}
1137
1138static int
1139pcap_set_datalink_bpf(pcap_t *p, int dlt)
1140{
1141#ifdef BIOCSDLT
1142	if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) {
1143		(void) snprintf(p->errbuf, sizeof(p->errbuf),
1144		    "Cannot set DLT %d: %s", dlt, strerror(errno));
1145		return (-1);
1146	}
1147#endif
1148	return (0);
1149}
1150