2 * Copyright (c) 1993, 1994, 1995, 1996, 1998
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 #include <sys/param.h> /* optionally get BSD define */
29 #ifdef HAVE_ZEROCOPY_BPF
32 #include <sys/socket.h>
35 * <net/bpf.h> defines ioctls, but doesn't include <sys/ioccom.h>.
37 * We include <sys/ioctl.h> as it might be necessary to declare ioctl();
38 * at least on *BSD and Mac OS X, it also defines various SIOC ioctls -
39 * we could include <sys/sockio.h>, but if we're already including
40 * <sys/ioctl.h>, which includes <sys/sockio.h> on those platforms,
41 * there's not much point in doing so.
43 * If we have <sys/ioccom.h>, we include it as well, to handle systems
44 * such as Solaris which don't arrange to include <sys/ioccom.h> if you
45 * include <sys/ioctl.h>
47 #include <sys/ioctl.h>
48 #ifdef HAVE_SYS_IOCCOM_H
49 #include <sys/ioccom.h>
51 #include <sys/utsname.h>
53 #ifdef HAVE_ZEROCOPY_BPF
54 #include <machine/atomic.h>
62 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
63 * native OS version, as we need "struct bpf_config" from it.
65 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
67 #include <sys/types.h>
70 * Prevent bpf.h from redefining the DLT_ values to their
71 * IFT_ values, as we're going to return the standard libpcap
72 * values, not IBM's non-standard IFT_ values.
78 #include <net/if_types.h> /* for IFT_ values */
79 #include <sys/sysconfig.h>
80 #include <sys/device.h>
81 #include <sys/cfgodm.h>
85 #define domakedev makedev64
86 #define getmajor major64
87 #define bpf_hdr bpf_hdr32
89 #define domakedev makedev
90 #define getmajor major
91 #endif /* __64BIT__ */
93 #define BPF_NAME "bpf"
95 #define DRIVER_PATH "/usr/lib/drivers"
96 #define BPF_NODE "/dev/bpf"
97 static int bpfloadedflag = 0;
98 static int odmlockid = 0;
100 static int bpf_load(char *errbuf);
117 #ifdef HAVE_NET_IF_MEDIA_H
118 # include <net/if_media.h>
121 #include "pcap-int.h"
123 #ifdef HAVE_OS_PROTO_H
124 #include "os-proto.h"
128 * Later versions of NetBSD stick padding in front of FDDI frames
129 * to align the IP header on a 4-byte boundary.
131 #if defined(__NetBSD__) && __NetBSD_Version__ > 106000000
132 #define PCAP_FDDIPAD 3
136 * Private data for capturing on BPF devices.
143 #ifdef HAVE_ZEROCOPY_BPF
145 * Zero-copy read buffer -- for zero-copy BPF. 'buffer' above will
146 * alternative between these two actual mmap'd buffers as required.
147 * As there is a header on the front size of the mmap'd buffer, only
148 * some of the buffer is exposed to libpcap as a whole via bufsize;
149 * zbufsize is the true size. zbuffer tracks the current zbuf
150 * assocated with buffer so that it can be used to decide which the
151 * next buffer to read will be.
153 u_char *zbuf1, *zbuf2, *zbuffer;
157 struct timespec firstsel;
159 * If there's currently a buffer being actively processed, then it is
160 * referenced here; 'buffer' is also pointed at it, but offset by the
161 * size of the header.
163 struct bpf_zbuf_header *bzh;
164 int nonblock; /* true if in nonblocking mode */
165 #endif /* HAVE_ZEROCOPY_BPF */
167 char *device; /* device name */
168 int filtering_in_kernel; /* using kernel filter */
169 int must_do_on_close; /* stuff we must do when we close */
173 * Stuff to do when we close.
175 #define MUST_CLEAR_RFMON 0x00000001 /* clear rfmon (monitor) mode */
178 # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
179 #define HAVE_BSD_IEEE80211
182 # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
183 static int find_802_11(struct bpf_dltlist *);
185 # ifdef HAVE_BSD_IEEE80211
186 static int monitor_mode(pcap_t *, int);
189 # if defined(__APPLE__)
190 static void remove_en(pcap_t *);
191 static void remove_802_11(pcap_t *);
194 # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
196 #endif /* BIOCGDLTLIST */
198 #if defined(sun) && defined(LIFNAMSIZ) && defined(lifr_zoneid)
203 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
204 * don't get DLT_DOCSIS defined.
207 #define DLT_DOCSIS 143
211 * On OS X, we don't even get any of the 802.11-plus-radio-header DLT_'s
212 * defined, even though some of them are used by various Airport drivers.
214 #ifndef DLT_PRISM_HEADER
215 #define DLT_PRISM_HEADER 119
217 #ifndef DLT_AIRONET_HEADER
218 #define DLT_AIRONET_HEADER 120
220 #ifndef DLT_IEEE802_11_RADIO
221 #define DLT_IEEE802_11_RADIO 127
223 #ifndef DLT_IEEE802_11_RADIO_AVS
224 #define DLT_IEEE802_11_RADIO_AVS 163
227 static int pcap_can_set_rfmon_bpf(pcap_t *p);
228 static int pcap_activate_bpf(pcap_t *p);
229 static int pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp);
230 static int pcap_setdirection_bpf(pcap_t *, pcap_direction_t);
231 static int pcap_set_datalink_bpf(pcap_t *p, int dlt);
234 * For zerocopy bpf, the setnonblock/getnonblock routines need to modify
235 * pb->nonblock so we don't call select(2) if the pcap handle is in non-
239 pcap_getnonblock_bpf(pcap_t *p, char *errbuf)
241 #ifdef HAVE_ZEROCOPY_BPF
242 struct pcap_bpf *pb = p->priv;
245 return (pb->nonblock);
247 return (pcap_getnonblock_fd(p, errbuf));
251 pcap_setnonblock_bpf(pcap_t *p, int nonblock, char *errbuf)
253 #ifdef HAVE_ZEROCOPY_BPF
254 struct pcap_bpf *pb = p->priv;
257 pb->nonblock = nonblock;
261 return (pcap_setnonblock_fd(p, nonblock, errbuf));
264 #ifdef HAVE_ZEROCOPY_BPF
266 * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
267 * shared memory buffers.
269 * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer,
270 * and set up p->buffer and cc to reflect one if available. Notice that if
271 * there was no prior buffer, we select zbuf1 as this will be the first
272 * buffer filled for a fresh BPF session.
275 pcap_next_zbuf_shm(pcap_t *p, int *cc)
277 struct pcap_bpf *pb = p->priv;
278 struct bpf_zbuf_header *bzh;
280 if (pb->zbuffer == pb->zbuf2 || pb->zbuffer == NULL) {
281 bzh = (struct bpf_zbuf_header *)pb->zbuf1;
282 if (bzh->bzh_user_gen !=
283 atomic_load_acq_int(&bzh->bzh_kernel_gen)) {
285 pb->zbuffer = (u_char *)pb->zbuf1;
286 p->buffer = pb->zbuffer + sizeof(*bzh);
287 *cc = bzh->bzh_kernel_len;
290 } else if (pb->zbuffer == pb->zbuf1) {
291 bzh = (struct bpf_zbuf_header *)pb->zbuf2;
292 if (bzh->bzh_user_gen !=
293 atomic_load_acq_int(&bzh->bzh_kernel_gen)) {
295 pb->zbuffer = (u_char *)pb->zbuf2;
296 p->buffer = pb->zbuffer + sizeof(*bzh);
297 *cc = bzh->bzh_kernel_len;
306 * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
307 * select() for data or a timeout, and possibly force rotation of the buffer
308 * in the event we time out or are in immediate mode. Invoke the shared
309 * memory check before doing system calls in order to avoid doing avoidable
313 pcap_next_zbuf(pcap_t *p, int *cc)
315 struct pcap_bpf *pb = p->priv;
323 #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000))
325 * Start out by seeing whether anything is waiting by checking the
326 * next shared memory buffer for data.
328 data = pcap_next_zbuf_shm(p, cc);
332 * If a previous sleep was interrupted due to signal delivery, make
333 * sure that the timeout gets adjusted accordingly. This requires
334 * that we analyze when the timeout should be been expired, and
335 * subtract the current time from that. If after this operation,
336 * our timeout is less then or equal to zero, handle it like a
339 tmout = p->opt.timeout;
341 (void) clock_gettime(CLOCK_MONOTONIC, &cur);
342 if (pb->interrupted && p->opt.timeout) {
343 expire = TSTOMILLI(&pb->firstsel) + p->opt.timeout;
344 tmout = expire - TSTOMILLI(&cur);
348 data = pcap_next_zbuf_shm(p, cc);
351 if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) {
352 (void) snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
353 "BIOCROTZBUF: %s", strerror(errno));
356 return (pcap_next_zbuf_shm(p, cc));
360 * No data in the buffer, so must use select() to wait for data or
361 * the next timeout. Note that we only call select if the handle
362 * is in blocking mode.
366 FD_SET(p->fd, &r_set);
368 tv.tv_sec = tmout / 1000;
369 tv.tv_usec = (tmout * 1000) % 1000000;
371 r = select(p->fd + 1, &r_set, NULL, NULL,
372 p->opt.timeout != 0 ? &tv : NULL);
373 if (r < 0 && errno == EINTR) {
374 if (!pb->interrupted && p->opt.timeout) {
380 (void) snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
381 "select: %s", strerror(errno));
387 * Check again for data, which may exist now that we've either been
388 * woken up as a result of data or timed out. Try the "there's data"
389 * case first since it doesn't require a system call.
391 data = pcap_next_zbuf_shm(p, cc);
395 * Try forcing a buffer rotation to dislodge timed out or immediate
398 if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) {
399 (void) snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
400 "BIOCROTZBUF: %s", strerror(errno));
403 return (pcap_next_zbuf_shm(p, cc));
407 * Notify kernel that we are done with the buffer. We don't reset zbuffer so
408 * that we know which buffer to use next time around.
411 pcap_ack_zbuf(pcap_t *p)
413 struct pcap_bpf *pb = p->priv;
415 atomic_store_rel_int(&pb->bzh->bzh_user_gen,
416 pb->bzh->bzh_kernel_gen);
421 #endif /* HAVE_ZEROCOPY_BPF */
424 pcap_create_interface(const char *device, char *ebuf)
428 p = pcap_create_common(device, ebuf, sizeof (struct pcap_bpf));
432 p->activate_op = pcap_activate_bpf;
433 p->can_set_rfmon_op = pcap_can_set_rfmon_bpf;
436 * We claim that we support microsecond and nanosecond time
439 p->tstamp_precision_count = 2;
440 p->tstamp_precision_list = malloc(2 * sizeof(u_int));
441 if (p->tstamp_precision_list == NULL) {
442 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
443 pcap_strerror(errno));
447 p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
448 p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
449 #endif /* BIOCSTSTAMP */
454 * On success, returns a file descriptor for a BPF device.
455 * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf.
461 #ifdef HAVE_CLONING_BPF
462 static const char device[] = "/dev/bpf";
465 char device[sizeof "/dev/bpf0000000000"];
470 * Load the bpf driver, if it isn't already loaded,
471 * and create the BPF device entries, if they don't
474 if (bpf_load(p->errbuf) == PCAP_ERROR)
478 #ifdef HAVE_CLONING_BPF
479 if ((fd = open(device, O_RDWR)) == -1 &&
480 (errno != EACCES || (fd = open(device, O_RDONLY)) == -1)) {
482 fd = PCAP_ERROR_PERM_DENIED;
485 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
486 "(cannot open device) %s: %s", device, pcap_strerror(errno));
490 * Go through all the minors and find one that isn't in use.
493 (void)snprintf(device, sizeof(device), "/dev/bpf%d", n++);
495 * Initially try a read/write open (to allow the inject
496 * method to work). If that fails due to permission
497 * issues, fall back to read-only. This allows a
498 * non-root user to be granted specific access to pcap
499 * capabilities via file permissions.
501 * XXX - we should have an API that has a flag that
502 * controls whether to open read-only or read-write,
503 * so that denial of permission to send (or inability
504 * to send, if sending packets isn't supported on
505 * the device in question) can be indicated at open
508 fd = open(device, O_RDWR);
509 if (fd == -1 && errno == EACCES)
510 fd = open(device, O_RDONLY);
511 } while (fd < 0 && errno == EBUSY);
514 * XXX better message for all minors used
523 * /dev/bpf0 doesn't exist, which
524 * means we probably have no BPF
527 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
528 "(there are no BPF devices)");
531 * We got EBUSY on at least one
532 * BPF device, so we have BPF
533 * devices, but all the ones
534 * that exist are busy.
536 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
537 "(all BPF devices are busy)");
543 * Got EACCES on the last device we tried,
544 * and EBUSY on all devices before that,
547 fd = PCAP_ERROR_PERM_DENIED;
548 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
549 "(cannot open BPF device) %s: %s", device,
550 pcap_strerror(errno));
555 * Some other problem.
558 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
559 "(cannot open BPF device) %s: %s", device,
560 pcap_strerror(errno));
571 get_dlt_list(int fd, int v, struct bpf_dltlist *bdlp, char *ebuf)
573 memset(bdlp, 0, sizeof(*bdlp));
574 if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) == 0) {
578 bdlp->bfl_list = (u_int *) malloc(sizeof(u_int) * (bdlp->bfl_len + 1));
579 if (bdlp->bfl_list == NULL) {
580 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
581 pcap_strerror(errno));
585 if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) < 0) {
586 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
587 "BIOCGDLTLIST: %s", pcap_strerror(errno));
588 free(bdlp->bfl_list);
593 * OK, for real Ethernet devices, add DLT_DOCSIS to the
594 * list, so that an application can let you choose it,
595 * in case you're capturing DOCSIS traffic that a Cisco
596 * Cable Modem Termination System is putting out onto
597 * an Ethernet (it doesn't put an Ethernet header onto
598 * the wire, it puts raw DOCSIS frames out on the wire
599 * inside the low-level Ethernet framing).
601 * A "real Ethernet device" is defined here as a device
602 * that has a link-layer type of DLT_EN10MB and that has
603 * no alternate link-layer types; that's done to exclude
604 * 802.11 interfaces (which might or might not be the
605 * right thing to do, but I suspect it is - Ethernet <->
606 * 802.11 bridges would probably badly mishandle frames
607 * that don't have Ethernet headers).
609 * On Solaris with BPF, Ethernet devices also offer
610 * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
611 * treat it as an indication that the device isn't an
614 if (v == DLT_EN10MB) {
616 for (i = 0; i < bdlp->bfl_len; i++) {
617 if (bdlp->bfl_list[i] != DLT_EN10MB
619 && bdlp->bfl_list[i] != DLT_IPNET
628 * We reserved one more slot at the end of
631 bdlp->bfl_list[bdlp->bfl_len] = DLT_DOCSIS;
637 * EINVAL just means "we don't support this ioctl on
638 * this device"; don't treat it as an error.
640 if (errno != EINVAL) {
641 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
642 "BIOCGDLTLIST: %s", pcap_strerror(errno));
651 pcap_can_set_rfmon_bpf(pcap_t *p)
653 #if defined(__APPLE__)
654 struct utsname osinfo;
658 struct bpf_dltlist bdl;
662 * The joys of monitor mode on OS X.
664 * Prior to 10.4, it's not supported at all.
666 * In 10.4, if adapter enN supports monitor mode, there's a
667 * wltN adapter corresponding to it; you open it, instead of
668 * enN, to get monitor mode. You get whatever link-layer
669 * headers it supplies.
671 * In 10.5, and, we assume, later releases, if adapter enN
672 * supports monitor mode, it offers, among its selectable
673 * DLT_ values, values that let you get the 802.11 header;
674 * selecting one of those values puts the adapter into monitor
675 * mode (i.e., you can't get 802.11 headers except in monitor
676 * mode, and you can't get Ethernet headers in monitor mode).
678 if (uname(&osinfo) == -1) {
680 * Can't get the OS version; just say "no".
685 * We assume osinfo.sysname is "Darwin", because
686 * __APPLE__ is defined. We just check the version.
688 if (osinfo.release[0] < '8' && osinfo.release[1] == '.') {
690 * 10.3 (Darwin 7.x) or earlier.
691 * Monitor mode not supported.
695 if (osinfo.release[0] == '8' && osinfo.release[1] == '.') {
697 * 10.4 (Darwin 8.x). s/en/wlt/, and check
698 * whether the device exists.
700 if (strncmp(p->opt.source, "en", 2) != 0) {
702 * Not an enN device; no monitor mode.
706 fd = socket(AF_INET, SOCK_DGRAM, 0);
708 (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
709 "socket: %s", pcap_strerror(errno));
712 strlcpy(ifr.ifr_name, "wlt", sizeof(ifr.ifr_name));
713 strlcat(ifr.ifr_name, p->opt.source + 2, sizeof(ifr.ifr_name));
714 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
727 * Everything else is 10.5 or later; for those,
728 * we just open the enN device, and check whether
729 * we have any 802.11 devices.
731 * First, open a BPF device.
735 return (fd); /* fd is the appropriate error code */
738 * Now bind to the device.
740 (void)strncpy(ifr.ifr_name, p->opt.source, sizeof(ifr.ifr_name));
741 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
746 * There's no such device.
749 return (PCAP_ERROR_NO_SUCH_DEVICE);
753 * Return a "network down" indication, so that
754 * the application can report that rather than
755 * saying we had a mysterious failure and
756 * suggest that they report a problem to the
757 * libpcap developers.
760 return (PCAP_ERROR_IFACE_NOT_UP);
763 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
765 p->opt.source, pcap_strerror(errno));
772 * We know the default link type -- now determine all the DLTs
773 * this interface supports. If this fails with EINVAL, it's
774 * not fatal; we just don't get to use the feature later.
775 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
776 * as the default DLT for this adapter.)
778 if (get_dlt_list(fd, DLT_NULL, &bdl, p->errbuf) == PCAP_ERROR) {
782 if (find_802_11(&bdl) != -1) {
784 * We have an 802.11 DLT, so we can set monitor mode.
791 #endif /* BIOCGDLTLIST */
793 #elif defined(HAVE_BSD_IEEE80211)
796 ret = monitor_mode(p, 0);
797 if (ret == PCAP_ERROR_RFMON_NOTSUP)
798 return (0); /* not an error, just a "can't do" */
800 return (1); /* success */
808 pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps)
813 * "ps_recv" counts packets handed to the filter, not packets
814 * that passed the filter. This includes packets later dropped
815 * because we ran out of buffer space.
817 * "ps_drop" counts packets dropped inside the BPF device
818 * because we ran out of buffer space. It doesn't count
819 * packets dropped by the interface driver. It counts
820 * only packets that passed the filter.
822 * Both statistics include packets not yet read from the kernel
823 * by libpcap, and thus not yet seen by the application.
825 if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) {
826 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGSTATS: %s",
827 pcap_strerror(errno));
831 ps->ps_recv = s.bs_recv;
832 ps->ps_drop = s.bs_drop;
838 pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
840 struct pcap_bpf *pb = p->priv;
843 register u_char *bp, *ep;
848 #ifdef HAVE_ZEROCOPY_BPF
854 * Has "pcap_breakloop()" been called?
858 * Yes - clear the flag that indicates that it
859 * has, and return PCAP_ERROR_BREAK to indicate
860 * that we were told to break out of the loop.
863 return (PCAP_ERROR_BREAK);
868 * When reading without zero-copy from a file descriptor, we
869 * use a single buffer and return a length of data in the
870 * buffer. With zero-copy, we update the p->buffer pointer
871 * to point at whatever underlying buffer contains the next
872 * data and update cc to reflect the data found in the
875 #ifdef HAVE_ZEROCOPY_BPF
877 if (p->buffer != NULL)
879 i = pcap_next_zbuf(p, &cc);
887 cc = read(p->fd, (char *)p->buffer, p->bufsize);
890 /* Don't choke when we get ptraced */
899 * Sigh. More AIX wonderfulness.
901 * For some unknown reason the uiomove()
902 * operation in the bpf kernel extension
903 * used to copy the buffer into user
904 * space sometimes returns EFAULT. I have
905 * no idea why this is the case given that
906 * a kernel debugger shows the user buffer
907 * is correct. This problem appears to
908 * be mostly mitigated by the memset of
909 * the buffer before it is first used.
910 * Very strange.... Shaun Clowes
912 * In any case this means that we shouldn't
913 * treat EFAULT as a fatal error; as we
914 * don't have an API for returning
915 * a "some packets were dropped since
916 * the last packet you saw" indication,
917 * we just ignore EFAULT and keep reading.
927 * The device on which we're capturing
930 * XXX - we should really return
931 * PCAP_ERROR_IFACE_NOT_UP, but
932 * pcap_dispatch() etc. aren't
933 * defined to retur that.
935 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
936 "The interface went down");
939 #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
941 * Due to a SunOS bug, after 2^31 bytes, the kernel
942 * file offset overflows and read fails with EINVAL.
943 * The lseek() to 0 will fix things.
946 if (lseek(p->fd, 0L, SEEK_CUR) +
948 (void)lseek(p->fd, 0L, SEEK_SET);
954 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read: %s",
955 pcap_strerror(errno));
963 * Loop through each packet.
966 #define bhp ((struct bpf_xhdr *)bp)
968 #define bhp ((struct bpf_hdr *)bp)
975 register int caplen, hdrlen;
978 * Has "pcap_breakloop()" been called?
979 * If so, return immediately - if we haven't read any
980 * packets, clear the flag and return PCAP_ERROR_BREAK
981 * to indicate that we were told to break out of the loop,
982 * otherwise leave the flag set, so that the *next* call
983 * will break out of the loop without having read any
984 * packets, and return the number of packets we've
991 * ep is set based on the return value of read(),
992 * but read() from a BPF device doesn't necessarily
993 * return a value that's a multiple of the alignment
994 * value for BPF_WORDALIGN(). However, whenever we
995 * increment bp, we round up the increment value by
996 * a value rounded up by BPF_WORDALIGN(), so we
997 * could increment bp past ep after processing the
998 * last packet in the buffer.
1000 * We treat ep < bp as an indication that this
1001 * happened, and just set p->cc to 0.
1007 return (PCAP_ERROR_BREAK);
1012 caplen = bhp->bh_caplen;
1013 hdrlen = bhp->bh_hdrlen;
1014 datap = bp + hdrlen;
1016 * Short-circuit evaluation: if using BPF filter
1017 * in kernel, no need to do it now - we already know
1018 * the packet passed the filter.
1021 * Note: the filter code was generated assuming
1022 * that p->fddipad was the amount of padding
1023 * before the header, as that's what's required
1024 * in the kernel, so we run the filter before
1025 * skipping that padding.
1028 if (pb->filtering_in_kernel ||
1029 bpf_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) {
1030 struct pcap_pkthdr pkthdr;
1034 bt.sec = bhp->bh_tstamp.bt_sec;
1035 bt.frac = bhp->bh_tstamp.bt_frac;
1036 if (p->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) {
1039 bintime2timespec(&bt, &ts);
1040 pkthdr.ts.tv_sec = ts.tv_sec;
1041 pkthdr.ts.tv_usec = ts.tv_nsec;
1045 bintime2timeval(&bt, &tv);
1046 pkthdr.ts.tv_sec = tv.tv_sec;
1047 pkthdr.ts.tv_usec = tv.tv_usec;
1050 pkthdr.ts.tv_sec = bhp->bh_tstamp.tv_sec;
1053 * AIX's BPF returns seconds/nanoseconds time
1054 * stamps, not seconds/microseconds time stamps.
1056 pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec/1000;
1058 pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec;
1060 #endif /* BIOCSTSTAMP */
1063 pkthdr.caplen = caplen - pad;
1066 if (bhp->bh_datalen > pad)
1067 pkthdr.len = bhp->bh_datalen - pad;
1072 pkthdr.caplen = caplen;
1073 pkthdr.len = bhp->bh_datalen;
1075 (*callback)(user, &pkthdr, datap);
1076 bp += BPF_WORDALIGN(caplen + hdrlen);
1077 if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
1081 * See comment above about p->cc < 0.
1091 bp += BPF_WORDALIGN(caplen + hdrlen);
1100 pcap_inject_bpf(pcap_t *p, const void *buf, size_t size)
1104 ret = write(p->fd, buf, size);
1106 if (ret == -1 && errno == EAFNOSUPPORT) {
1108 * In Mac OS X, there's a bug wherein setting the
1109 * BIOCSHDRCMPLT flag causes writes to fail; see,
1112 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
1114 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
1115 * assume it's due to that bug, and turn off that flag
1116 * and try again. If we succeed, it either means that
1117 * somebody applied the fix from that URL, or other patches
1120 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
1122 * and are running a Darwin kernel with those fixes, or
1123 * that Apple fixed the problem in some OS X release.
1125 u_int spoof_eth_src = 0;
1127 if (ioctl(p->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
1128 (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1129 "send: can't turn off BIOCSHDRCMPLT: %s",
1130 pcap_strerror(errno));
1131 return (PCAP_ERROR);
1135 * Now try the write again.
1137 ret = write(p->fd, buf, size);
1139 #endif /* __APPLE__ */
1141 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
1142 pcap_strerror(errno));
1143 return (PCAP_ERROR);
1150 bpf_odminit(char *errbuf)
1154 if (odm_initialize() == -1) {
1155 if (odm_err_msg(odmerrno, &errstr) == -1)
1156 errstr = "Unknown error";
1157 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1158 "bpf_load: odm_initialize failed: %s",
1160 return (PCAP_ERROR);
1163 if ((odmlockid = odm_lock("/etc/objrepos/config_lock", ODM_WAIT)) == -1) {
1164 if (odm_err_msg(odmerrno, &errstr) == -1)
1165 errstr = "Unknown error";
1166 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1167 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
1169 (void)odm_terminate();
1170 return (PCAP_ERROR);
1177 bpf_odmcleanup(char *errbuf)
1181 if (odm_unlock(odmlockid) == -1) {
1182 if (errbuf != NULL) {
1183 if (odm_err_msg(odmerrno, &errstr) == -1)
1184 errstr = "Unknown error";
1185 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1186 "bpf_load: odm_unlock failed: %s",
1189 return (PCAP_ERROR);
1192 if (odm_terminate() == -1) {
1193 if (errbuf != NULL) {
1194 if (odm_err_msg(odmerrno, &errstr) == -1)
1195 errstr = "Unknown error";
1196 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1197 "bpf_load: odm_terminate failed: %s",
1200 return (PCAP_ERROR);
1207 bpf_load(char *errbuf)
1211 int numminors, i, rc;
1214 struct bpf_config cfg_bpf;
1215 struct cfg_load cfg_ld;
1216 struct cfg_kmod cfg_km;
1219 * This is very very close to what happens in the real implementation
1220 * but I've fixed some (unlikely) bug situations.
1225 if (bpf_odminit(errbuf) == PCAP_ERROR)
1226 return (PCAP_ERROR);
1228 major = genmajor(BPF_NAME);
1230 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1231 "bpf_load: genmajor failed: %s", pcap_strerror(errno));
1232 (void)bpf_odmcleanup(NULL);
1233 return (PCAP_ERROR);
1236 minors = getminor(major, &numminors, BPF_NAME);
1238 minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1);
1240 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1241 "bpf_load: genminor failed: %s",
1242 pcap_strerror(errno));
1243 (void)bpf_odmcleanup(NULL);
1244 return (PCAP_ERROR);
1248 if (bpf_odmcleanup(errbuf) == PCAP_ERROR)
1249 return (PCAP_ERROR);
1251 rc = stat(BPF_NODE "0", &sbuf);
1252 if (rc == -1 && errno != ENOENT) {
1253 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1254 "bpf_load: can't stat %s: %s",
1255 BPF_NODE "0", pcap_strerror(errno));
1256 return (PCAP_ERROR);
1259 if (rc == -1 || getmajor(sbuf.st_rdev) != major) {
1260 for (i = 0; i < BPF_MINORS; i++) {
1261 sprintf(buf, "%s%d", BPF_NODE, i);
1263 if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) {
1264 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1265 "bpf_load: can't mknod %s: %s",
1266 buf, pcap_strerror(errno));
1267 return (PCAP_ERROR);
1272 /* Check if the driver is loaded */
1273 memset(&cfg_ld, 0x0, sizeof(cfg_ld));
1275 sprintf(cfg_ld.path, "%s/%s", DRIVER_PATH, BPF_NAME);
1276 if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) ||
1277 (cfg_ld.kmid == 0)) {
1278 /* Driver isn't loaded, load it now */
1279 if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) {
1280 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1281 "bpf_load: could not load driver: %s",
1283 return (PCAP_ERROR);
1287 /* Configure the driver */
1288 cfg_km.cmd = CFG_INIT;
1289 cfg_km.kmid = cfg_ld.kmid;
1290 cfg_km.mdilen = sizeof(cfg_bpf);
1291 cfg_km.mdiptr = (void *)&cfg_bpf;
1292 for (i = 0; i < BPF_MINORS; i++) {
1293 cfg_bpf.devno = domakedev(major, i);
1294 if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) {
1295 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1296 "bpf_load: could not configure driver: %s",
1298 return (PCAP_ERROR);
1309 * Turn off rfmon mode if necessary.
1312 pcap_cleanup_bpf(pcap_t *p)
1314 struct pcap_bpf *pb = p->priv;
1315 #ifdef HAVE_BSD_IEEE80211
1317 struct ifmediareq req;
1321 if (pb->must_do_on_close != 0) {
1323 * There's something we have to do when closing this
1326 #ifdef HAVE_BSD_IEEE80211
1327 if (pb->must_do_on_close & MUST_CLEAR_RFMON) {
1329 * We put the interface into rfmon mode;
1330 * take it out of rfmon mode.
1332 * XXX - if somebody else wants it in rfmon
1333 * mode, this code cannot know that, so it'll take
1334 * it out of rfmon mode.
1336 sock = socket(AF_INET, SOCK_DGRAM, 0);
1339 "Can't restore interface flags (socket() failed: %s).\n"
1340 "Please adjust manually.\n",
1343 memset(&req, 0, sizeof(req));
1344 strncpy(req.ifm_name, pb->device,
1345 sizeof(req.ifm_name));
1346 if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
1348 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
1349 "Please adjust manually.\n",
1352 if (req.ifm_current & IFM_IEEE80211_MONITOR) {
1354 * Rfmon mode is currently on;
1357 memset(&ifr, 0, sizeof(ifr));
1358 (void)strncpy(ifr.ifr_name,
1360 sizeof(ifr.ifr_name));
1362 req.ifm_current & ~IFM_IEEE80211_MONITOR;
1363 if (ioctl(sock, SIOCSIFMEDIA,
1366 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
1367 "Please adjust manually.\n",
1375 #endif /* HAVE_BSD_IEEE80211 */
1378 * Take this pcap out of the list of pcaps for which we
1379 * have to take the interface out of some mode.
1381 pcap_remove_from_pcaps_to_close(p);
1382 pb->must_do_on_close = 0;
1385 #ifdef HAVE_ZEROCOPY_BPF
1388 * Delete the mappings. Note that p->buffer gets
1389 * initialized to one of the mmapped regions in
1390 * this case, so do not try and free it directly;
1391 * null it out so that pcap_cleanup_live_common()
1392 * doesn't try to free it.
1394 if (pb->zbuf1 != MAP_FAILED && pb->zbuf1 != NULL)
1395 (void) munmap(pb->zbuf1, pb->zbufsize);
1396 if (pb->zbuf2 != MAP_FAILED && pb->zbuf2 != NULL)
1397 (void) munmap(pb->zbuf2, pb->zbufsize);
1402 if (pb->device != NULL) {
1406 pcap_cleanup_live_common(p);
1410 check_setif_failure(pcap_t *p, int error)
1418 if (error == ENXIO) {
1420 * No such device exists.
1423 if (p->opt.rfmon && strncmp(p->opt.source, "wlt", 3) == 0) {
1425 * Monitor mode was requested, and we're trying
1426 * to open a "wltN" device. Assume that this
1427 * is 10.4 and that we were asked to open an
1428 * "enN" device; if that device exists, return
1429 * "monitor mode not supported on the device".
1431 fd = socket(AF_INET, SOCK_DGRAM, 0);
1433 strlcpy(ifr.ifr_name, "en",
1434 sizeof(ifr.ifr_name));
1435 strlcat(ifr.ifr_name, p->opt.source + 3,
1436 sizeof(ifr.ifr_name));
1437 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
1439 * We assume this failed because
1440 * the underlying device doesn't
1443 err = PCAP_ERROR_NO_SUCH_DEVICE;
1444 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1445 "SIOCGIFFLAGS on %s failed: %s",
1446 ifr.ifr_name, pcap_strerror(errno));
1449 * The underlying "enN" device
1450 * exists, but there's no
1451 * corresponding "wltN" device;
1452 * that means that the "enN"
1453 * device doesn't support
1454 * monitor mode, probably because
1455 * it's an Ethernet device rather
1456 * than a wireless device.
1458 err = PCAP_ERROR_RFMON_NOTSUP;
1463 * We can't find out whether there's
1464 * an underlying "enN" device, so
1465 * just report "no such device".
1467 err = PCAP_ERROR_NO_SUCH_DEVICE;
1468 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1469 "socket() failed: %s",
1470 pcap_strerror(errno));
1478 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF failed: %s",
1479 pcap_strerror(errno));
1480 return (PCAP_ERROR_NO_SUCH_DEVICE);
1481 } else if (errno == ENETDOWN) {
1483 * Return a "network down" indication, so that
1484 * the application can report that rather than
1485 * saying we had a mysterious failure and
1486 * suggest that they report a problem to the
1487 * libpcap developers.
1489 return (PCAP_ERROR_IFACE_NOT_UP);
1492 * Some other error; fill in the error string, and
1493 * return PCAP_ERROR.
1495 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s",
1496 p->opt.source, pcap_strerror(errno));
1497 return (PCAP_ERROR);
1502 * Default capture buffer size.
1503 * 32K isn't very much for modern machines with fast networks; we
1504 * pick .5M, as that's the maximum on at least some systems with BPF.
1506 * However, on AIX 3.5, the larger buffer sized caused unrecoverable
1507 * read failures under stress, so we leave it as 32K; yet another
1508 * place where AIX's BPF is broken.
1511 #define DEFAULT_BUFSIZE 32768
1513 #define DEFAULT_BUFSIZE 524288
1517 pcap_activate_bpf(pcap_t *p)
1519 struct pcap_bpf *pb = p->priv;
1521 #ifdef HAVE_BSD_IEEE80211
1528 char *ifrname = ifr.lifr_name;
1529 const size_t ifnamsiz = sizeof(ifr.lifr_name);
1532 char *ifrname = ifr.ifr_name;
1533 const size_t ifnamsiz = sizeof(ifr.ifr_name);
1535 struct bpf_version bv;
1538 char *wltdev = NULL;
1541 struct bpf_dltlist bdl;
1542 #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1545 #endif /* BIOCGDLTLIST */
1546 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1547 u_int spoof_eth_src = 1;
1550 struct bpf_insn total_insn;
1551 struct bpf_program total_prog;
1552 struct utsname osinfo;
1555 if (strstr(device, "dag")) {
1556 return dag_open_live(device, snaplen, promisc, to_ms, ebuf);
1558 #endif /* HAVE_DAG_API */
1561 memset(&bdl, 0, sizeof(bdl));
1562 int have_osinfo = 0;
1563 #ifdef HAVE_ZEROCOPY_BPF
1565 u_int bufmode, zbufmax;
1576 if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) {
1577 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCVERSION: %s",
1578 pcap_strerror(errno));
1579 status = PCAP_ERROR;
1582 if (bv.bv_major != BPF_MAJOR_VERSION ||
1583 bv.bv_minor < BPF_MINOR_VERSION) {
1584 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1585 "kernel bpf filter out of date");
1586 status = PCAP_ERROR;
1590 #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid)
1592 * Check if the given source network device has a '/' separated
1593 * zonename prefix string. The zonename prefixed source device
1594 * can be used by libpcap consumers to capture network traffic
1595 * in non-global zones from the global zone on Solaris 11 and
1596 * above. If the zonename prefix is present then we strip the
1597 * prefix and pass the zone ID as part of lifr_zoneid.
1599 if ((zonesep = strchr(p->opt.source, '/')) != NULL) {
1600 char zonename[ZONENAME_MAX];
1604 znamelen = zonesep - p->opt.source;
1605 (void) strlcpy(zonename, p->opt.source, znamelen + 1);
1606 lnamep = strdup(zonesep + 1);
1607 ifr.lifr_zoneid = getzoneidbyname(zonename);
1608 free(p->opt.source);
1609 p->opt.source = lnamep;
1613 pb->device = strdup(p->opt.source);
1614 if (pb->device == NULL) {
1615 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "strdup: %s",
1616 pcap_strerror(errno));
1617 status = PCAP_ERROR;
1622 * Try finding a good size for the buffer; 32768 may be too
1623 * big, so keep cutting it in half until we find a size
1624 * that works, or run out of sizes to try. If the default
1625 * is larger, don't make it smaller.
1627 * XXX - there should be a user-accessible hook to set the
1628 * initial buffer size.
1629 * Attempt to find out the version of the OS on which we're running.
1631 if (uname(&osinfo) == 0)
1636 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1637 * of why we check the version number.
1642 * We assume osinfo.sysname is "Darwin", because
1643 * __APPLE__ is defined. We just check the version.
1645 if (osinfo.release[0] < '8' &&
1646 osinfo.release[1] == '.') {
1648 * 10.3 (Darwin 7.x) or earlier.
1650 status = PCAP_ERROR_RFMON_NOTSUP;
1653 if (osinfo.release[0] == '8' &&
1654 osinfo.release[1] == '.') {
1656 * 10.4 (Darwin 8.x). s/en/wlt/
1658 if (strncmp(p->opt.source, "en", 2) != 0) {
1660 * Not an enN device; check
1661 * whether the device even exists.
1663 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1666 p->opt.source, ifnamsiz);
1667 if (ioctl(sockfd, SIOCGIFFLAGS,
1668 (char *)&ifr) < 0) {
1676 status = PCAP_ERROR_NO_SUCH_DEVICE;
1679 "SIOCGIFFLAGS failed: %s",
1680 pcap_strerror(errno));
1682 status = PCAP_ERROR_RFMON_NOTSUP;
1686 * We can't find out whether
1687 * the device exists, so just
1688 * report "no such device".
1690 status = PCAP_ERROR_NO_SUCH_DEVICE;
1693 "socket() failed: %s",
1694 pcap_strerror(errno));
1698 wltdev = malloc(strlen(p->opt.source) + 2);
1699 if (wltdev == NULL) {
1700 (void)snprintf(p->errbuf,
1701 PCAP_ERRBUF_SIZE, "malloc: %s",
1702 pcap_strerror(errno));
1703 status = PCAP_ERROR;
1706 strcpy(wltdev, "wlt");
1707 strcat(wltdev, p->opt.source + 2);
1708 free(p->opt.source);
1709 p->opt.source = wltdev;
1712 * Everything else is 10.5 or later; for those,
1713 * we just open the enN device, and set the DLT.
1717 #endif /* __APPLE__ */
1718 #ifdef HAVE_ZEROCOPY_BPF
1720 * If the BPF extension to set buffer mode is present, try setting
1721 * the mode to zero-copy. If that fails, use regular buffering. If
1722 * it succeeds but other setup fails, return an error to the user.
1724 bufmode = BPF_BUFMODE_ZBUF;
1725 if (ioctl(fd, BIOCSETBUFMODE, (caddr_t)&bufmode) == 0) {
1727 * We have zerocopy BPF; use it.
1732 * How to pick a buffer size: first, query the maximum buffer
1733 * size supported by zero-copy. This also lets us quickly
1734 * determine whether the kernel generally supports zero-copy.
1735 * Then, if a buffer size was specified, use that, otherwise
1736 * query the default buffer size, which reflects kernel
1737 * policy for a desired default. Round to the nearest page
1740 if (ioctl(fd, BIOCGETZMAX, (caddr_t)&zbufmax) < 0) {
1741 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGETZMAX: %s",
1742 pcap_strerror(errno));
1743 status = PCAP_ERROR;
1747 if (p->opt.buffer_size != 0) {
1749 * A buffer size was explicitly specified; use it.
1751 v = p->opt.buffer_size;
1753 if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) ||
1754 v < DEFAULT_BUFSIZE)
1755 v = DEFAULT_BUFSIZE;
1758 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
1760 pb->zbufsize = roundup(v, getpagesize());
1761 if (pb->zbufsize > zbufmax)
1762 pb->zbufsize = zbufmax;
1763 pb->zbuf1 = mmap(NULL, pb->zbufsize, PROT_READ | PROT_WRITE,
1765 pb->zbuf2 = mmap(NULL, pb->zbufsize, PROT_READ | PROT_WRITE,
1767 if (pb->zbuf1 == MAP_FAILED || pb->zbuf2 == MAP_FAILED) {
1768 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "mmap: %s",
1769 pcap_strerror(errno));
1770 status = PCAP_ERROR;
1773 memset(&bz, 0, sizeof(bz)); /* bzero() deprecated, replaced with memset() */
1774 bz.bz_bufa = pb->zbuf1;
1775 bz.bz_bufb = pb->zbuf2;
1776 bz.bz_buflen = pb->zbufsize;
1777 if (ioctl(fd, BIOCSETZBUF, (caddr_t)&bz) < 0) {
1778 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETZBUF: %s",
1779 pcap_strerror(errno));
1780 status = PCAP_ERROR;
1783 (void)strncpy(ifrname, p->opt.source, ifnamsiz);
1784 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
1785 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s",
1786 p->opt.source, pcap_strerror(errno));
1787 status = PCAP_ERROR;
1790 v = pb->zbufsize - sizeof(struct bpf_zbuf_header);
1795 * We don't have zerocopy BPF.
1796 * Set the buffer size.
1798 if (p->opt.buffer_size != 0) {
1800 * A buffer size was explicitly specified; use it.
1802 if (ioctl(fd, BIOCSBLEN,
1803 (caddr_t)&p->opt.buffer_size) < 0) {
1804 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1805 "BIOCSBLEN: %s: %s", p->opt.source,
1806 pcap_strerror(errno));
1807 status = PCAP_ERROR;
1812 * Now bind to the device.
1814 (void)strncpy(ifrname, p->opt.source, ifnamsiz);
1816 if (ioctl(fd, BIOCSETLIF, (caddr_t)&ifr) < 0)
1818 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0)
1821 status = check_setif_failure(p, errno);
1826 * No buffer size was explicitly specified.
1828 * Try finding a good size for the buffer;
1829 * DEFAULT_BUFSIZE may be too big, so keep
1830 * cutting it in half until we find a size
1831 * that works, or run out of sizes to try.
1832 * If the default is larger, don't make it smaller.
1834 if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) ||
1835 v < DEFAULT_BUFSIZE)
1836 v = DEFAULT_BUFSIZE;
1837 for ( ; v != 0; v >>= 1) {
1839 * Ignore the return value - this is because the
1840 * call fails on BPF systems that don't have
1841 * kernel malloc. And if the call fails, it's
1842 * no big deal, we just continue to use the
1843 * standard buffer size.
1845 (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
1847 (void)strncpy(ifrname, p->opt.source, ifnamsiz);
1849 if (ioctl(fd, BIOCSETLIF, (caddr_t)&ifr) >= 0)
1851 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
1853 break; /* that size worked; we're done */
1855 if (errno != ENOBUFS) {
1856 status = check_setif_failure(p, errno);
1862 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1863 "BIOCSBLEN: %s: No buffer size worked",
1865 status = PCAP_ERROR;
1872 /* Get the data link layer type. */
1873 if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
1874 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGDLT: %s",
1875 pcap_strerror(errno));
1876 status = PCAP_ERROR;
1882 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
1905 * We don't know what to map this to yet.
1907 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown interface type %u",
1909 status = PCAP_ERROR;
1913 #if _BSDI_VERSION - 0 >= 199510
1914 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
1929 case 12: /*DLT_C_HDLC*/
1937 * We know the default link type -- now determine all the DLTs
1938 * this interface supports. If this fails with EINVAL, it's
1939 * not fatal; we just don't get to use the feature later.
1941 if (get_dlt_list(fd, v, &bdl, p->errbuf) == -1) {
1942 status = PCAP_ERROR;
1945 p->dlt_count = bdl.bfl_len;
1946 p->dlt_list = bdl.bfl_list;
1950 * Monitor mode fun, continued.
1952 * For 10.5 and, we're assuming, later releases, as noted above,
1953 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
1954 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
1955 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
1958 * Therefore, if the user asked for monitor mode, we filter out
1959 * the DLT_EN10MB value, as you can't get that in monitor mode,
1960 * and, if the user didn't ask for monitor mode, we filter out
1961 * the 802.11 DLT_ values, because selecting those will turn
1962 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
1963 * radio DLT_ value is offered, we try to select that, otherwise
1964 * we try to select DLT_IEEE802_11.
1967 if (isdigit((unsigned)osinfo.release[0]) &&
1968 (osinfo.release[0] == '9' ||
1969 isdigit((unsigned)osinfo.release[1]))) {
1971 * 10.5 (Darwin 9.x), or later.
1973 new_dlt = find_802_11(&bdl);
1974 if (new_dlt != -1) {
1976 * We have at least one 802.11 DLT_ value,
1977 * so this is an 802.11 interface.
1978 * new_dlt is the best of the 802.11
1979 * DLT_ values in the list.
1983 * Our caller wants monitor mode.
1984 * Purge DLT_EN10MB from the list
1985 * of link-layer types, as selecting
1986 * it will keep monitor mode off.
1991 * If the new mode we want isn't
1992 * the default mode, attempt to
1993 * select the new mode.
1996 if (ioctl(p->fd, BIOCSDLT,
2008 * Our caller doesn't want
2009 * monitor mode. Unless this
2010 * is being done by pcap_open_live(),
2011 * purge the 802.11 link-layer types
2012 * from the list, as selecting
2013 * one of them will turn monitor
2022 * The caller requested monitor
2023 * mode, but we have no 802.11
2024 * link-layer types, so they
2027 status = PCAP_ERROR_RFMON_NOTSUP;
2033 #elif defined(HAVE_BSD_IEEE80211)
2035 * *BSD with the new 802.11 ioctls.
2036 * Do we want monitor mode?
2040 * Try to put the interface into monitor mode.
2042 retv = monitor_mode(p, 1);
2052 * We're in monitor mode.
2053 * Try to find the best 802.11 DLT_ value and, if we
2054 * succeed, try to switch to that mode if we're not
2055 * already in that mode.
2057 new_dlt = find_802_11(&bdl);
2058 if (new_dlt != -1) {
2060 * We have at least one 802.11 DLT_ value.
2061 * new_dlt is the best of the 802.11
2062 * DLT_ values in the list.
2064 * If the new mode we want isn't the default mode,
2065 * attempt to select the new mode.
2068 if (ioctl(p->fd, BIOCSDLT, &new_dlt) != -1) {
2070 * We succeeded; make this the
2078 #endif /* various platforms */
2079 #endif /* BIOCGDLTLIST */
2082 * If this is an Ethernet device, and we don't have a DLT_ list,
2083 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
2084 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
2085 * do, but there's not much we can do about that without finding
2086 * some other way of determining whether it's an Ethernet or 802.11
2089 if (v == DLT_EN10MB && p->dlt_count == 0) {
2090 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
2092 * If that fails, just leave the list empty.
2094 if (p->dlt_list != NULL) {
2095 p->dlt_list[0] = DLT_EN10MB;
2096 p->dlt_list[1] = DLT_DOCSIS;
2102 p->fddipad = PCAP_FDDIPAD;
2108 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
2110 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
2111 * the link-layer source address isn't forcibly overwritten.
2112 * (Should we ignore errors? Should we do this only if
2113 * we're open for writing?)
2115 * XXX - I seem to remember some packet-sending bug in some
2116 * BSDs - check CVS log for "bpf.c"?
2118 if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
2119 (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2120 "BIOCSHDRCMPLT: %s", pcap_strerror(errno));
2121 status = PCAP_ERROR;
2126 #ifdef HAVE_ZEROCOPY_BPF
2128 * In zero-copy mode, we just use the timeout in select().
2129 * XXX - what if we're in non-blocking mode and the *application*
2130 * is using select() or poll() or kqueues or....?
2132 if (p->opt.timeout && !pb->zerocopy) {
2134 if (p->opt.timeout) {
2137 * XXX - is this seconds/nanoseconds in AIX?
2138 * (Treating it as such doesn't fix the timeout
2139 * problem described below.)
2141 * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
2142 * 64-bit userland - it takes, as an argument, a
2143 * "struct BPF_TIMEVAL", which has 32-bit tv_sec
2144 * and tv_usec, rather than a "struct timeval".
2146 * If this platform defines "struct BPF_TIMEVAL",
2147 * we check whether the structure size in BIOCSRTIMEOUT
2148 * is that of a "struct timeval" and, if not, we use
2149 * a "struct BPF_TIMEVAL" rather than a "struct timeval".
2150 * (That way, if the bug is fixed in a future release,
2151 * we will still do the right thing.)
2154 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2155 struct BPF_TIMEVAL bpf_to;
2157 if (IOCPARM_LEN(BIOCSRTIMEOUT) != sizeof(struct timeval)) {
2158 bpf_to.tv_sec = p->opt.timeout / 1000;
2159 bpf_to.tv_usec = (p->opt.timeout * 1000) % 1000000;
2160 if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&bpf_to) < 0) {
2161 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2162 "BIOCSRTIMEOUT: %s", pcap_strerror(errno));
2163 status = PCAP_ERROR;
2168 to.tv_sec = p->opt.timeout / 1000;
2169 to.tv_usec = (p->opt.timeout * 1000) % 1000000;
2170 if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) {
2171 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2172 "BIOCSRTIMEOUT: %s", pcap_strerror(errno));
2173 status = PCAP_ERROR;
2176 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2181 #ifdef BIOCIMMEDIATE
2183 * Darren Reed notes that
2185 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
2186 * timeout appears to be ignored and it waits until the buffer
2187 * is filled before returning. The result of not having it
2188 * set is almost worse than useless if your BPF filter
2189 * is reducing things to only a few packets (i.e. one every
2192 * so we always turn BIOCIMMEDIATE mode on if this is AIX.
2194 * For other platforms, we don't turn immediate mode on by default,
2195 * as that would mean we get woken up for every packet, which
2196 * probably isn't what you want for a packet sniffer.
2198 * We set immediate mode if the caller requested it by calling
2199 * pcap_set_immediate() before calling pcap_activate().
2202 if (p->opt.immediate) {
2205 if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) {
2206 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2207 "BIOCIMMEDIATE: %s", pcap_strerror(errno));
2208 status = PCAP_ERROR;
2214 #else /* BIOCIMMEDIATE */
2215 if (p->opt.immediate) {
2217 * We don't support immediate mode. Fail.
2219 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Immediate mode not supported");
2220 status = PCAP_ERROR;
2223 #endif /* BIOCIMMEDIATE */
2225 if (p->opt.promisc) {
2226 /* set promiscuous mode, just warn if it fails */
2227 if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) {
2228 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCPROMISC: %s",
2229 pcap_strerror(errno));
2230 status = PCAP_WARNING_PROMISC_NOTSUP;
2236 if (ioctl(p->fd, BIOCSTSTAMP, &v) < 0) {
2237 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSTSTAMP: %s",
2238 pcap_strerror(errno));
2239 status = PCAP_ERROR;
2242 #endif /* BIOCSTSTAMP */
2244 if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
2245 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGBLEN: %s",
2246 pcap_strerror(errno));
2247 status = PCAP_ERROR;
2251 #ifdef HAVE_ZEROCOPY_BPF
2252 if (!pb->zerocopy) {
2254 p->buffer = (u_char *)malloc(p->bufsize);
2255 if (p->buffer == NULL) {
2256 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s",
2257 pcap_strerror(errno));
2258 status = PCAP_ERROR;
2262 /* For some strange reason this seems to prevent the EFAULT
2263 * problems we have experienced from AIX BPF. */
2264 memset(p->buffer, 0x0, p->bufsize);
2266 #ifdef HAVE_ZEROCOPY_BPF
2271 * If there's no filter program installed, there's
2272 * no indication to the kernel of what the snapshot
2273 * length should be, so no snapshotting is done.
2275 * Therefore, when we open the device, we install
2276 * an "accept everything" filter with the specified
2279 total_insn.code = (u_short)(BPF_RET | BPF_K);
2282 total_insn.k = p->snapshot;
2284 total_prog.bf_len = 1;
2285 total_prog.bf_insns = &total_insn;
2286 if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) {
2287 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s",
2288 pcap_strerror(errno));
2289 status = PCAP_ERROR;
2294 * On most BPF platforms, either you can do a "select()" or
2295 * "poll()" on a BPF file descriptor and it works correctly,
2296 * or you can do it and it will return "readable" if the
2297 * hold buffer is full but not if the timeout expires *and*
2298 * a non-blocking read will, if the hold buffer is empty
2299 * but the store buffer isn't empty, rotate the buffers
2300 * and return what packets are available.
2302 * In the latter case, the fact that a non-blocking read
2303 * will give you the available packets means you can work
2304 * around the failure of "select()" and "poll()" to wake up
2305 * and return "readable" when the timeout expires by using
2306 * the timeout as the "select()" or "poll()" timeout, putting
2307 * the BPF descriptor into non-blocking mode, and read from
2308 * it regardless of whether "select()" reports it as readable
2311 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
2312 * won't wake up and return "readable" if the timer expires
2313 * and non-blocking reads return EWOULDBLOCK if the hold
2314 * buffer is empty, even if the store buffer is non-empty.
2316 * This means the workaround in question won't work.
2318 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2319 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2320 * here". On all other BPF platforms, we set it to the FD for
2321 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2322 * read will, if the hold buffer is empty and the store buffer
2323 * isn't empty, rotate the buffers and return what packets are
2324 * there (and in sufficiently recent versions of OpenBSD
2325 * "select()" and "poll()" should work correctly).
2327 * XXX - what about AIX?
2329 p->selectable_fd = p->fd; /* assume select() works until we know otherwise */
2332 * We can check what OS this is.
2334 if (strcmp(osinfo.sysname, "FreeBSD") == 0) {
2335 if (strncmp(osinfo.release, "4.3-", 4) == 0 ||
2336 strncmp(osinfo.release, "4.4-", 4) == 0)
2337 p->selectable_fd = -1;
2341 p->read_op = pcap_read_bpf;
2342 p->inject_op = pcap_inject_bpf;
2343 p->setfilter_op = pcap_setfilter_bpf;
2344 p->setdirection_op = pcap_setdirection_bpf;
2345 p->set_datalink_op = pcap_set_datalink_bpf;
2346 p->getnonblock_op = pcap_getnonblock_bpf;
2347 p->setnonblock_op = pcap_setnonblock_bpf;
2348 p->stats_op = pcap_stats_bpf;
2349 p->cleanup_op = pcap_cleanup_bpf;
2353 pcap_cleanup_bpf(p);
2358 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
2363 #ifdef HAVE_BSD_IEEE80211
2365 monitor_mode(pcap_t *p, int set)
2367 struct pcap_bpf *pb = p->priv;
2369 struct ifmediareq req;
2375 sock = socket(AF_INET, SOCK_DGRAM, 0);
2377 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't open socket: %s",
2378 pcap_strerror(errno));
2379 return (PCAP_ERROR);
2382 memset(&req, 0, sizeof req);
2383 strncpy(req.ifm_name, p->opt.source, sizeof req.ifm_name);
2386 * Find out how many media types we have.
2388 if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
2390 * Can't get the media types.
2396 * There's no such device.
2399 return (PCAP_ERROR_NO_SUCH_DEVICE);
2403 * Interface doesn't support SIOC{G,S}IFMEDIA.
2406 return (PCAP_ERROR_RFMON_NOTSUP);
2409 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2410 "SIOCGIFMEDIA 1: %s", pcap_strerror(errno));
2412 return (PCAP_ERROR);
2415 if (req.ifm_count == 0) {
2420 return (PCAP_ERROR_RFMON_NOTSUP);
2424 * Allocate a buffer to hold all the media types, and
2425 * get the media types.
2427 media_list = malloc(req.ifm_count * sizeof(int));
2428 if (media_list == NULL) {
2429 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s",
2430 pcap_strerror(errno));
2432 return (PCAP_ERROR);
2434 req.ifm_ulist = media_list;
2435 if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
2436 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFMEDIA: %s",
2437 pcap_strerror(errno));
2440 return (PCAP_ERROR);
2444 * Look for an 802.11 "automatic" media type.
2445 * We assume that all 802.11 adapters have that media type,
2446 * and that it will carry the monitor mode supported flag.
2449 for (i = 0; i < req.ifm_count; i++) {
2450 if (IFM_TYPE(media_list[i]) == IFM_IEEE80211
2451 && IFM_SUBTYPE(media_list[i]) == IFM_AUTO) {
2452 /* OK, does it do monitor mode? */
2453 if (media_list[i] & IFM_IEEE80211_MONITOR) {
2462 * This adapter doesn't support monitor mode.
2465 return (PCAP_ERROR_RFMON_NOTSUP);
2470 * Don't just check whether we can enable monitor mode,
2471 * do so, if it's not already enabled.
2473 if ((req.ifm_current & IFM_IEEE80211_MONITOR) == 0) {
2475 * Monitor mode isn't currently on, so turn it on,
2476 * and remember that we should turn it off when the
2481 * If we haven't already done so, arrange to have
2482 * "pcap_close_all()" called when we exit.
2484 if (!pcap_do_addexit(p)) {
2486 * "atexit()" failed; don't put the interface
2487 * in monitor mode, just give up.
2489 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2492 return (PCAP_ERROR);
2494 memset(&ifr, 0, sizeof(ifr));
2495 (void)strncpy(ifr.ifr_name, p->opt.source,
2496 sizeof(ifr.ifr_name));
2497 ifr.ifr_media = req.ifm_current | IFM_IEEE80211_MONITOR;
2498 if (ioctl(sock, SIOCSIFMEDIA, &ifr) == -1) {
2499 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2500 "SIOCSIFMEDIA: %s", pcap_strerror(errno));
2502 return (PCAP_ERROR);
2505 pb->must_do_on_close |= MUST_CLEAR_RFMON;
2508 * Add this to the list of pcaps to close when we exit.
2510 pcap_add_to_pcaps_to_close(p);
2515 #endif /* HAVE_BSD_IEEE80211 */
2517 #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
2519 * Check whether we have any 802.11 link-layer types; return the best
2520 * of the 802.11 link-layer types if we find one, and return -1
2523 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
2524 * best 802.11 link-layer type; any of the other 802.11-plus-radio
2525 * headers are second-best; 802.11 with no radio information is
2529 find_802_11(struct bpf_dltlist *bdlp)
2535 * Scan the list of DLT_ values, looking for 802.11 values,
2536 * and, if we find any, choose the best of them.
2539 for (i = 0; i < bdlp->bfl_len; i++) {
2540 switch (bdlp->bfl_list[i]) {
2542 case DLT_IEEE802_11:
2544 * 802.11, but no radio.
2546 * Offer this, and select it as the new mode
2547 * unless we've already found an 802.11
2548 * header with radio information.
2551 new_dlt = bdlp->bfl_list[i];
2554 case DLT_PRISM_HEADER:
2555 case DLT_AIRONET_HEADER:
2556 case DLT_IEEE802_11_RADIO_AVS:
2558 * 802.11 with radio, but not radiotap.
2560 * Offer this, and select it as the new mode
2561 * unless we've already found the radiotap DLT_.
2563 if (new_dlt != DLT_IEEE802_11_RADIO)
2564 new_dlt = bdlp->bfl_list[i];
2567 case DLT_IEEE802_11_RADIO:
2569 * 802.11 with radiotap.
2571 * Offer this, and select it as the new mode.
2573 new_dlt = bdlp->bfl_list[i];
2586 #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
2588 #if defined(__APPLE__) && defined(BIOCGDLTLIST)
2590 * Remove DLT_EN10MB from the list of DLT_ values, as we're in monitor mode,
2591 * and DLT_EN10MB isn't supported in monitor mode.
2594 remove_en(pcap_t *p)
2599 * Scan the list of DLT_ values and discard DLT_EN10MB.
2602 for (i = 0; i < p->dlt_count; i++) {
2603 switch (p->dlt_list[i]) {
2607 * Don't offer this one.
2613 * Just copy this mode over.
2619 * Copy this DLT_ value to its new position.
2621 p->dlt_list[j] = p->dlt_list[i];
2626 * Set the DLT_ count to the number of entries we copied.
2632 * Remove 802.11 link-layer types from the list of DLT_ values, as
2633 * we're not in monitor mode, and those DLT_ values will switch us
2637 remove_802_11(pcap_t *p)
2642 * Scan the list of DLT_ values and discard 802.11 values.
2645 for (i = 0; i < p->dlt_count; i++) {
2646 switch (p->dlt_list[i]) {
2648 case DLT_IEEE802_11:
2649 case DLT_PRISM_HEADER:
2650 case DLT_AIRONET_HEADER:
2651 case DLT_IEEE802_11_RADIO:
2652 case DLT_IEEE802_11_RADIO_AVS:
2654 * 802.11. Don't offer this one.
2660 * Just copy this mode over.
2666 * Copy this DLT_ value to its new position.
2668 p->dlt_list[j] = p->dlt_list[i];
2673 * Set the DLT_ count to the number of entries we copied.
2677 #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
2680 pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp)
2682 struct pcap_bpf *pb = p->priv;
2685 * Free any user-mode filter we might happen to have installed.
2687 pcap_freecode(&p->fcode);
2690 * Try to install the kernel filter.
2692 if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) == 0) {
2696 pb->filtering_in_kernel = 1; /* filtering in the kernel */
2699 * Discard any previously-received packets, as they might
2700 * have passed whatever filter was formerly in effect, but
2701 * might not pass this filter (BIOCSETF discards packets
2702 * buffered in the kernel, so you can lose packets in any
2712 * If it failed with EINVAL, that's probably because the program
2713 * is invalid or too big. Validate it ourselves; if we like it
2714 * (we currently allow backward branches, to support protochain),
2715 * run it in userland. (There's no notion of "too big" for
2718 * Otherwise, just give up.
2719 * XXX - if the copy of the program into the kernel failed,
2720 * we will get EINVAL rather than, say, EFAULT on at least
2723 if (errno != EINVAL) {
2724 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s",
2725 pcap_strerror(errno));
2730 * install_bpf_program() validates the program.
2732 * XXX - what if we already have a filter in the kernel?
2734 if (install_bpf_program(p, fp) < 0)
2736 pb->filtering_in_kernel = 0; /* filtering in userland */
2741 * Set direction flag: Which packets do we accept on a forwarding
2742 * single device? IN, OUT or both?
2745 pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d)
2747 #if defined(BIOCSDIRECTION)
2750 direction = (d == PCAP_D_IN) ? BPF_D_IN :
2751 ((d == PCAP_D_OUT) ? BPF_D_OUT : BPF_D_INOUT);
2752 if (ioctl(p->fd, BIOCSDIRECTION, &direction) == -1) {
2753 (void) snprintf(p->errbuf, sizeof(p->errbuf),
2754 "Cannot set direction to %s: %s",
2755 (d == PCAP_D_IN) ? "PCAP_D_IN" :
2756 ((d == PCAP_D_OUT) ? "PCAP_D_OUT" : "PCAP_D_INOUT"),
2761 #elif defined(BIOCSSEESENT)
2765 * We don't support PCAP_D_OUT.
2767 if (d == PCAP_D_OUT) {
2768 snprintf(p->errbuf, sizeof(p->errbuf),
2769 "Setting direction to PCAP_D_OUT is not supported on BPF");
2773 seesent = (d == PCAP_D_INOUT);
2774 if (ioctl(p->fd, BIOCSSEESENT, &seesent) == -1) {
2775 (void) snprintf(p->errbuf, sizeof(p->errbuf),
2776 "Cannot set direction to %s: %s",
2777 (d == PCAP_D_INOUT) ? "PCAP_D_INOUT" : "PCAP_D_IN",
2783 (void) snprintf(p->errbuf, sizeof(p->errbuf),
2784 "This system doesn't support BIOCSSEESENT, so the direction can't be set");
2790 pcap_set_datalink_bpf(pcap_t *p, int dlt)
2793 if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) {
2794 (void) snprintf(p->errbuf, sizeof(p->errbuf),
2795 "Cannot set DLT %d: %s", dlt, strerror(errno));