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.
22 static const char rcsid
[] _U_
=
23 "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.116 2008-09-16 18:42:29 guy Exp $ (LBL)";
30 #include <sys/param.h> /* optionally get BSD define */
31 #ifdef HAVE_ZEROCOPY_BPF
34 #include <sys/socket.h>
37 * <net/bpf.h> defines ioctls, but doesn't include <sys/ioccom.h>.
39 * We include <sys/ioctl.h> as it might be necessary to declare ioctl();
40 * at least on *BSD and Mac OS X, it also defines various SIOC ioctls -
41 * we could include <sys/sockio.h>, but if we're already including
42 * <sys/ioctl.h>, which includes <sys/sockio.h> on those platforms,
43 * there's not much point in doing so.
45 * If we have <sys/ioccom.h>, we include it as well, to handle systems
46 * such as Solaris which don't arrange to include <sys/ioccom.h> if you
47 * include <sys/ioctl.h>
49 #include <sys/ioctl.h>
50 #ifdef HAVE_SYS_IOCCOM_H
51 #include <sys/ioccom.h>
53 #include <sys/utsname.h>
55 #ifdef HAVE_ZEROCOPY_BPF
56 #include <machine/atomic.h>
64 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
65 * native OS version, as we need "struct bpf_config" from it.
67 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
69 #include <sys/types.h>
72 * Prevent bpf.h from redefining the DLT_ values to their
73 * IFT_ values, as we're going to return the standard libpcap
74 * values, not IBM's non-standard IFT_ values.
80 #include <net/if_types.h> /* for IFT_ values */
81 #include <sys/sysconfig.h>
82 #include <sys/device.h>
83 #include <sys/cfgodm.h>
87 #define domakedev makedev64
88 #define getmajor major64
89 #define bpf_hdr bpf_hdr32
91 #define domakedev makedev
92 #define getmajor major
93 #endif /* __64BIT__ */
95 #define BPF_NAME "bpf"
97 #define DRIVER_PATH "/usr/lib/drivers"
98 #define BPF_NODE "/dev/bpf"
99 static int bpfloadedflag
= 0;
100 static int odmlockid
= 0;
102 static int bpf_load(char *errbuf
);
119 #ifdef HAVE_NET_IF_MEDIA_H
120 # include <net/if_media.h>
123 #include "pcap-int.h"
126 #include "pcap-dag.h"
127 #endif /* HAVE_DAG_API */
130 #include "pcap-snf.h"
131 #endif /* HAVE_SNF_API */
133 #ifdef HAVE_OS_PROTO_H
134 #include "os-proto.h"
138 # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
139 #define HAVE_BSD_IEEE80211
142 # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
143 static int find_802_11(struct bpf_dltlist
*);
145 # ifdef HAVE_BSD_IEEE80211
146 static int monitor_mode(pcap_t
*, int);
149 # if defined(__APPLE__)
150 static void remove_en(pcap_t
*);
151 static void remove_802_11(pcap_t
*);
154 # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
156 #endif /* BIOCGDLTLIST */
159 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
160 * don't get DLT_DOCSIS defined.
163 #define DLT_DOCSIS 143
167 * On OS X, we don't even get any of the 802.11-plus-radio-header DLT_'s
168 * defined, even though some of them are used by various Airport drivers.
170 #ifndef DLT_PRISM_HEADER
171 #define DLT_PRISM_HEADER 119
173 #ifndef DLT_AIRONET_HEADER
174 #define DLT_AIRONET_HEADER 120
176 #ifndef DLT_IEEE802_11_RADIO
177 #define DLT_IEEE802_11_RADIO 127
179 #ifndef DLT_IEEE802_11_RADIO_AVS
180 #define DLT_IEEE802_11_RADIO_AVS 163
183 static int pcap_can_set_rfmon_bpf(pcap_t
*p
);
184 static int pcap_activate_bpf(pcap_t
*p
);
185 static int pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
);
186 static int pcap_setdirection_bpf(pcap_t
*, pcap_direction_t
);
187 static int pcap_set_datalink_bpf(pcap_t
*p
, int dlt
);
190 * For zerocopy bpf, the setnonblock/getnonblock routines need to modify
191 * p->md.timeout so we don't call select(2) if the pcap handle is in non-
192 * blocking mode. We preserve the timeout supplied by pcap_open functions
193 * to make sure it does not get clobbered if the pcap handle moves between
194 * blocking and non-blocking mode.
197 pcap_getnonblock_bpf(pcap_t
*p
, char *errbuf
)
199 #ifdef HAVE_ZEROCOPY_BPF
200 if (p
->md
.zerocopy
) {
202 * Use a negative value for the timeout to represent that the
203 * pcap handle is in non-blocking mode.
205 return (p
->md
.timeout
< 0);
208 return (pcap_getnonblock_fd(p
, errbuf
));
212 pcap_setnonblock_bpf(pcap_t
*p
, int nonblock
, char *errbuf
)
214 #ifdef HAVE_ZEROCOPY_BPF
215 if (p
->md
.zerocopy
) {
217 * Map each value to the corresponding 2's complement, to
218 * preserve the timeout value provided with pcap_set_timeout.
219 * (from pcap-linux.c).
222 if (p
->md
.timeout
>= 0) {
224 * Timeout is non-negative, so we're not
225 * currently in non-blocking mode; set it
226 * to the 2's complement, to make it
227 * negative, as an indication that we're
228 * in non-blocking mode.
230 p
->md
.timeout
= p
->md
.timeout
* -1 - 1;
233 if (p
->md
.timeout
< 0) {
235 * Timeout is negative, so we're currently
236 * in blocking mode; reverse the previous
237 * operation, to make the timeout non-negative
240 p
->md
.timeout
= (p
->md
.timeout
+ 1) * -1;
246 return (pcap_setnonblock_fd(p
, nonblock
, errbuf
));
249 #ifdef HAVE_ZEROCOPY_BPF
251 * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
252 * shared memory buffers.
254 * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer,
255 * and set up p->buffer and cc to reflect one if available. Notice that if
256 * there was no prior buffer, we select zbuf1 as this will be the first
257 * buffer filled for a fresh BPF session.
260 pcap_next_zbuf_shm(pcap_t
*p
, int *cc
)
262 struct bpf_zbuf_header
*bzh
;
264 if (p
->md
.zbuffer
== p
->md
.zbuf2
|| p
->md
.zbuffer
== NULL
) {
265 bzh
= (struct bpf_zbuf_header
*)p
->md
.zbuf1
;
266 if (bzh
->bzh_user_gen
!=
267 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
269 p
->md
.zbuffer
= (u_char
*)p
->md
.zbuf1
;
270 p
->buffer
= p
->md
.zbuffer
+ sizeof(*bzh
);
271 *cc
= bzh
->bzh_kernel_len
;
274 } else if (p
->md
.zbuffer
== p
->md
.zbuf1
) {
275 bzh
= (struct bpf_zbuf_header
*)p
->md
.zbuf2
;
276 if (bzh
->bzh_user_gen
!=
277 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
279 p
->md
.zbuffer
= (u_char
*)p
->md
.zbuf2
;
280 p
->buffer
= p
->md
.zbuffer
+ sizeof(*bzh
);
281 *cc
= bzh
->bzh_kernel_len
;
290 * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
291 * select() for data or a timeout, and possibly force rotation of the buffer
292 * in the event we time out or are in immediate mode. Invoke the shared
293 * memory check before doing system calls in order to avoid doing avoidable
297 pcap_next_zbuf(pcap_t
*p
, int *cc
)
306 #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000))
308 * Start out by seeing whether anything is waiting by checking the
309 * next shared memory buffer for data.
311 data
= pcap_next_zbuf_shm(p
, cc
);
315 * If a previous sleep was interrupted due to signal delivery, make
316 * sure that the timeout gets adjusted accordingly. This requires
317 * that we analyze when the timeout should be been expired, and
318 * subtract the current time from that. If after this operation,
319 * our timeout is less then or equal to zero, handle it like a
322 tmout
= p
->md
.timeout
;
324 (void) clock_gettime(CLOCK_MONOTONIC
, &cur
);
325 if (p
->md
.interrupted
&& p
->md
.timeout
) {
326 expire
= TSTOMILLI(&p
->md
.firstsel
) + p
->md
.timeout
;
327 tmout
= expire
- TSTOMILLI(&cur
);
330 p
->md
.interrupted
= 0;
331 data
= pcap_next_zbuf_shm(p
, cc
);
334 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
335 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
336 "BIOCROTZBUF: %s", strerror(errno
));
339 return (pcap_next_zbuf_shm(p
, cc
));
343 * No data in the buffer, so must use select() to wait for data or
344 * the next timeout. Note that we only call select if the handle
345 * is in blocking mode.
347 if (p
->md
.timeout
>= 0) {
349 FD_SET(p
->fd
, &r_set
);
351 tv
.tv_sec
= tmout
/ 1000;
352 tv
.tv_usec
= (tmout
* 1000) % 1000000;
354 r
= select(p
->fd
+ 1, &r_set
, NULL
, NULL
,
355 p
->md
.timeout
!= 0 ? &tv
: NULL
);
356 if (r
< 0 && errno
== EINTR
) {
357 if (!p
->md
.interrupted
&& p
->md
.timeout
) {
358 p
->md
.interrupted
= 1;
359 p
->md
.firstsel
= cur
;
363 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
364 "select: %s", strerror(errno
));
368 p
->md
.interrupted
= 0;
370 * Check again for data, which may exist now that we've either been
371 * woken up as a result of data or timed out. Try the "there's data"
372 * case first since it doesn't require a system call.
374 data
= pcap_next_zbuf_shm(p
, cc
);
378 * Try forcing a buffer rotation to dislodge timed out or immediate
381 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
382 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
383 "BIOCROTZBUF: %s", strerror(errno
));
386 return (pcap_next_zbuf_shm(p
, cc
));
390 * Notify kernel that we are done with the buffer. We don't reset zbuffer so
391 * that we know which buffer to use next time around.
394 pcap_ack_zbuf(pcap_t
*p
)
397 atomic_store_rel_int(&p
->md
.bzh
->bzh_user_gen
,
398 p
->md
.bzh
->bzh_kernel_gen
);
403 #endif /* HAVE_ZEROCOPY_BPF */
406 pcap_create(const char *device
, char *ebuf
)
411 if (strstr(device
, "dag"))
412 return (dag_create(device
, ebuf
));
413 #endif /* HAVE_DAG_API */
415 if (strstr(device
, "snf"))
416 return (snf_create(device
, ebuf
));
417 #endif /* HAVE_SNF_API */
419 p
= pcap_create_common(device
, ebuf
);
423 p
->activate_op
= pcap_activate_bpf
;
424 p
->can_set_rfmon_op
= pcap_can_set_rfmon_bpf
;
429 * On success, returns a file descriptor for a BPF device.
430 * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf.
436 #ifdef HAVE_CLONING_BPF
437 static const char device
[] = "/dev/bpf";
440 char device
[sizeof "/dev/bpf0000000000"];
445 * Load the bpf driver, if it isn't already loaded,
446 * and create the BPF device entries, if they don't
449 if (bpf_load(p
->errbuf
) == PCAP_ERROR
)
453 #ifdef HAVE_CLONING_BPF
454 if ((fd
= open(device
, O_RDWR
)) == -1 &&
455 (errno
!= EACCES
|| (fd
= open(device
, O_RDONLY
)) == -1)) {
457 fd
= PCAP_ERROR_PERM_DENIED
;
460 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
461 "(cannot open device) %s: %s", device
, pcap_strerror(errno
));
465 * Go through all the minors and find one that isn't in use.
468 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
470 * Initially try a read/write open (to allow the inject
471 * method to work). If that fails due to permission
472 * issues, fall back to read-only. This allows a
473 * non-root user to be granted specific access to pcap
474 * capabilities via file permissions.
476 * XXX - we should have an API that has a flag that
477 * controls whether to open read-only or read-write,
478 * so that denial of permission to send (or inability
479 * to send, if sending packets isn't supported on
480 * the device in question) can be indicated at open
483 fd
= open(device
, O_RDWR
);
484 if (fd
== -1 && errno
== EACCES
)
485 fd
= open(device
, O_RDONLY
);
486 } while (fd
< 0 && errno
== EBUSY
);
489 * XXX better message for all minors used
498 * /dev/bpf0 doesn't exist, which
499 * means we probably have no BPF
502 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
503 "(there are no BPF devices)");
506 * We got EBUSY on at least one
507 * BPF device, so we have BPF
508 * devices, but all the ones
509 * that exist are busy.
511 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
512 "(all BPF devices are busy)");
518 * Got EACCES on the last device we tried,
519 * and EBUSY on all devices before that,
522 fd
= PCAP_ERROR_PERM_DENIED
;
523 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
524 "(cannot open BPF device) %s: %s", device
,
525 pcap_strerror(errno
));
530 * Some other problem.
533 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
534 "(cannot open BPF device) %s: %s", device
,
535 pcap_strerror(errno
));
546 get_dlt_list(int fd
, int v
, struct bpf_dltlist
*bdlp
, char *ebuf
)
548 memset(bdlp
, 0, sizeof(*bdlp
));
549 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) == 0) {
553 bdlp
->bfl_list
= (u_int
*) malloc(sizeof(u_int
) * (bdlp
->bfl_len
+ 1));
554 if (bdlp
->bfl_list
== NULL
) {
555 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
556 pcap_strerror(errno
));
560 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) < 0) {
561 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
562 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
563 free(bdlp
->bfl_list
);
568 * OK, for real Ethernet devices, add DLT_DOCSIS to the
569 * list, so that an application can let you choose it,
570 * in case you're capturing DOCSIS traffic that a Cisco
571 * Cable Modem Termination System is putting out onto
572 * an Ethernet (it doesn't put an Ethernet header onto
573 * the wire, it puts raw DOCSIS frames out on the wire
574 * inside the low-level Ethernet framing).
576 * A "real Ethernet device" is defined here as a device
577 * that has a link-layer type of DLT_EN10MB and that has
578 * no alternate link-layer types; that's done to exclude
579 * 802.11 interfaces (which might or might not be the
580 * right thing to do, but I suspect it is - Ethernet <->
581 * 802.11 bridges would probably badly mishandle frames
582 * that don't have Ethernet headers).
584 * On Solaris with BPF, Ethernet devices also offer
585 * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
586 * treat it as an indication that the device isn't an
589 if (v
== DLT_EN10MB
) {
591 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
592 if (bdlp
->bfl_list
[i
] != DLT_EN10MB
594 && bdlp
->bfl_list
[i
] != DLT_IPNET
603 * We reserved one more slot at the end of
606 bdlp
->bfl_list
[bdlp
->bfl_len
] = DLT_DOCSIS
;
612 * EINVAL just means "we don't support this ioctl on
613 * this device"; don't treat it as an error.
615 if (errno
!= EINVAL
) {
616 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
617 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
626 pcap_can_set_rfmon_bpf(pcap_t
*p
)
628 #if defined(__APPLE__)
629 struct utsname osinfo
;
633 struct bpf_dltlist bdl
;
637 * The joys of monitor mode on OS X.
639 * Prior to 10.4, it's not supported at all.
641 * In 10.4, if adapter enN supports monitor mode, there's a
642 * wltN adapter corresponding to it; you open it, instead of
643 * enN, to get monitor mode. You get whatever link-layer
644 * headers it supplies.
646 * In 10.5, and, we assume, later releases, if adapter enN
647 * supports monitor mode, it offers, among its selectable
648 * DLT_ values, values that let you get the 802.11 header;
649 * selecting one of those values puts the adapter into monitor
650 * mode (i.e., you can't get 802.11 headers except in monitor
651 * mode, and you can't get Ethernet headers in monitor mode).
653 if (uname(&osinfo
) == -1) {
655 * Can't get the OS version; just say "no".
660 * We assume osinfo.sysname is "Darwin", because
661 * __APPLE__ is defined. We just check the version.
663 if (osinfo
.release
[0] < '8' && osinfo
.release
[1] == '.') {
665 * 10.3 (Darwin 7.x) or earlier.
666 * Monitor mode not supported.
670 if (osinfo
.release
[0] == '8' && osinfo
.release
[1] == '.') {
672 * 10.4 (Darwin 8.x). s/en/wlt/, and check
673 * whether the device exists.
675 if (strncmp(p
->opt
.source
, "en", 2) != 0) {
677 * Not an enN device; no monitor mode.
681 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
683 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
684 "socket: %s", pcap_strerror(errno
));
687 strlcpy(ifr
.ifr_name
, "wlt", sizeof(ifr
.ifr_name
));
688 strlcat(ifr
.ifr_name
, p
->opt
.source
+ 2, sizeof(ifr
.ifr_name
));
689 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
702 * Everything else is 10.5 or later; for those,
703 * we just open the enN device, and check whether
704 * we have any 802.11 devices.
706 * First, open a BPF device.
710 return (fd
); /* fd is the appropriate error code */
713 * Now bind to the device.
715 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
, sizeof(ifr
.ifr_name
));
716 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
721 * There's no such device.
724 return (PCAP_ERROR_NO_SUCH_DEVICE
);
728 * Return a "network down" indication, so that
729 * the application can report that rather than
730 * saying we had a mysterious failure and
731 * suggest that they report a problem to the
732 * libpcap developers.
735 return (PCAP_ERROR_IFACE_NOT_UP
);
738 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
740 p
->opt
.source
, pcap_strerror(errno
));
747 * We know the default link type -- now determine all the DLTs
748 * this interface supports. If this fails with EINVAL, it's
749 * not fatal; we just don't get to use the feature later.
750 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
751 * as the default DLT for this adapter.)
753 if (get_dlt_list(fd
, DLT_NULL
, &bdl
, p
->errbuf
) == PCAP_ERROR
) {
757 if (find_802_11(&bdl
) != -1) {
759 * We have an 802.11 DLT, so we can set monitor mode.
766 #endif /* BIOCGDLTLIST */
768 #elif defined(HAVE_BSD_IEEE80211)
771 ret
= monitor_mode(p
, 0);
772 if (ret
== PCAP_ERROR_RFMON_NOTSUP
)
773 return (0); /* not an error, just a "can't do" */
775 return (1); /* success */
783 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
788 * "ps_recv" counts packets handed to the filter, not packets
789 * that passed the filter. This includes packets later dropped
790 * because we ran out of buffer space.
792 * "ps_drop" counts packets dropped inside the BPF device
793 * because we ran out of buffer space. It doesn't count
794 * packets dropped by the interface driver. It counts
795 * only packets that passed the filter.
797 * Both statistics include packets not yet read from the kernel
798 * by libpcap, and thus not yet seen by the application.
800 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
801 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
802 pcap_strerror(errno
));
806 ps
->ps_recv
= s
.bs_recv
;
807 ps
->ps_drop
= s
.bs_drop
;
813 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
817 register u_char
*bp
, *ep
;
822 #ifdef HAVE_ZEROCOPY_BPF
828 * Has "pcap_breakloop()" been called?
832 * Yes - clear the flag that indicates that it
833 * has, and return PCAP_ERROR_BREAK to indicate
834 * that we were told to break out of the loop.
837 return (PCAP_ERROR_BREAK
);
842 * When reading without zero-copy from a file descriptor, we
843 * use a single buffer and return a length of data in the
844 * buffer. With zero-copy, we update the p->buffer pointer
845 * to point at whatever underlying buffer contains the next
846 * data and update cc to reflect the data found in the
849 #ifdef HAVE_ZEROCOPY_BPF
850 if (p
->md
.zerocopy
) {
851 if (p
->buffer
!= NULL
)
853 i
= pcap_next_zbuf(p
, &cc
);
861 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
864 /* Don't choke when we get ptraced */
873 * Sigh. More AIX wonderfulness.
875 * For some unknown reason the uiomove()
876 * operation in the bpf kernel extension
877 * used to copy the buffer into user
878 * space sometimes returns EFAULT. I have
879 * no idea why this is the case given that
880 * a kernel debugger shows the user buffer
881 * is correct. This problem appears to
882 * be mostly mitigated by the memset of
883 * the buffer before it is first used.
884 * Very strange.... Shaun Clowes
886 * In any case this means that we shouldn't
887 * treat EFAULT as a fatal error; as we
888 * don't have an API for returning
889 * a "some packets were dropped since
890 * the last packet you saw" indication,
891 * we just ignore EFAULT and keep reading.
901 * The device on which we're capturing
904 * XXX - we should really return
905 * PCAP_ERROR_IFACE_NOT_UP, but
906 * pcap_dispatch() etc. aren't
907 * defined to retur that.
909 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
910 "The interface went down");
913 #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
915 * Due to a SunOS bug, after 2^31 bytes, the kernel
916 * file offset overflows and read fails with EINVAL.
917 * The lseek() to 0 will fix things.
920 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
922 (void)lseek(p
->fd
, 0L, SEEK_SET
);
928 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
929 pcap_strerror(errno
));
937 * Loop through each packet.
939 #define bhp ((struct bpf_hdr *)bp)
945 register int caplen
, hdrlen
;
948 * Has "pcap_breakloop()" been called?
949 * If so, return immediately - if we haven't read any
950 * packets, clear the flag and return PCAP_ERROR_BREAK
951 * to indicate that we were told to break out of the loop,
952 * otherwise leave the flag set, so that the *next* call
953 * will break out of the loop without having read any
954 * packets, and return the number of packets we've
961 * ep is set based on the return value of read(),
962 * but read() from a BPF device doesn't necessarily
963 * return a value that's a multiple of the alignment
964 * value for BPF_WORDALIGN(). However, whenever we
965 * increment bp, we round up the increment value by
966 * a value rounded up by BPF_WORDALIGN(), so we
967 * could increment bp past ep after processing the
968 * last packet in the buffer.
970 * We treat ep < bp as an indication that this
971 * happened, and just set p->cc to 0.
977 return (PCAP_ERROR_BREAK
);
982 caplen
= bhp
->bh_caplen
;
983 hdrlen
= bhp
->bh_hdrlen
;
986 * Short-circuit evaluation: if using BPF filter
987 * in kernel, no need to do it now - we already know
988 * the packet passed the filter.
991 * Note: the filter code was generated assuming
992 * that p->fddipad was the amount of padding
993 * before the header, as that's what's required
994 * in the kernel, so we run the filter before
995 * skipping that padding.
999 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
1000 struct pcap_pkthdr pkthdr
;
1002 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
1005 * AIX's BPF returns seconds/nanoseconds time
1006 * stamps, not seconds/microseconds time stamps.
1008 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
1010 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
1014 pkthdr
.caplen
= caplen
- pad
;
1017 if (bhp
->bh_datalen
> pad
)
1018 pkthdr
.len
= bhp
->bh_datalen
- pad
;
1023 pkthdr
.caplen
= caplen
;
1024 pkthdr
.len
= bhp
->bh_datalen
;
1026 (*callback
)(user
, &pkthdr
, datap
);
1027 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1028 if (++n
>= cnt
&& cnt
> 0) {
1032 * See comment above about p->cc < 0.
1042 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1051 pcap_inject_bpf(pcap_t
*p
, const void *buf
, size_t size
)
1055 ret
= write(p
->fd
, buf
, size
);
1057 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
1059 * In Mac OS X, there's a bug wherein setting the
1060 * BIOCSHDRCMPLT flag causes writes to fail; see,
1063 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
1065 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
1066 * assume it's due to that bug, and turn off that flag
1067 * and try again. If we succeed, it either means that
1068 * somebody applied the fix from that URL, or other patches
1071 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
1073 * and are running a Darwin kernel with those fixes, or
1074 * that Apple fixed the problem in some OS X release.
1076 u_int spoof_eth_src
= 0;
1078 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1079 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1080 "send: can't turn off BIOCSHDRCMPLT: %s",
1081 pcap_strerror(errno
));
1082 return (PCAP_ERROR
);
1086 * Now try the write again.
1088 ret
= write(p
->fd
, buf
, size
);
1090 #endif /* __APPLE__ */
1092 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
1093 pcap_strerror(errno
));
1094 return (PCAP_ERROR
);
1101 bpf_odminit(char *errbuf
)
1105 if (odm_initialize() == -1) {
1106 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1107 errstr
= "Unknown error";
1108 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1109 "bpf_load: odm_initialize failed: %s",
1111 return (PCAP_ERROR
);
1114 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
1115 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1116 errstr
= "Unknown error";
1117 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1118 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
1120 (void)odm_terminate();
1121 return (PCAP_ERROR
);
1128 bpf_odmcleanup(char *errbuf
)
1132 if (odm_unlock(odmlockid
) == -1) {
1133 if (errbuf
!= NULL
) {
1134 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1135 errstr
= "Unknown error";
1136 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1137 "bpf_load: odm_unlock failed: %s",
1140 return (PCAP_ERROR
);
1143 if (odm_terminate() == -1) {
1144 if (errbuf
!= NULL
) {
1145 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1146 errstr
= "Unknown error";
1147 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1148 "bpf_load: odm_terminate failed: %s",
1151 return (PCAP_ERROR
);
1158 bpf_load(char *errbuf
)
1162 int numminors
, i
, rc
;
1165 struct bpf_config cfg_bpf
;
1166 struct cfg_load cfg_ld
;
1167 struct cfg_kmod cfg_km
;
1170 * This is very very close to what happens in the real implementation
1171 * but I've fixed some (unlikely) bug situations.
1176 if (bpf_odminit(errbuf
) == PCAP_ERROR
)
1177 return (PCAP_ERROR
);
1179 major
= genmajor(BPF_NAME
);
1181 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1182 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
1183 (void)bpf_odmcleanup(NULL
);
1184 return (PCAP_ERROR
);
1187 minors
= getminor(major
, &numminors
, BPF_NAME
);
1189 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
1191 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1192 "bpf_load: genminor failed: %s",
1193 pcap_strerror(errno
));
1194 (void)bpf_odmcleanup(NULL
);
1195 return (PCAP_ERROR
);
1199 if (bpf_odmcleanup(errbuf
) == PCAP_ERROR
)
1200 return (PCAP_ERROR
);
1202 rc
= stat(BPF_NODE
"0", &sbuf
);
1203 if (rc
== -1 && errno
!= ENOENT
) {
1204 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1205 "bpf_load: can't stat %s: %s",
1206 BPF_NODE
"0", pcap_strerror(errno
));
1207 return (PCAP_ERROR
);
1210 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
1211 for (i
= 0; i
< BPF_MINORS
; i
++) {
1212 sprintf(buf
, "%s%d", BPF_NODE
, i
);
1214 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
1215 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1216 "bpf_load: can't mknod %s: %s",
1217 buf
, pcap_strerror(errno
));
1218 return (PCAP_ERROR
);
1223 /* Check if the driver is loaded */
1224 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
1226 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
1227 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
1228 (cfg_ld
.kmid
== 0)) {
1229 /* Driver isn't loaded, load it now */
1230 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
1231 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1232 "bpf_load: could not load driver: %s",
1234 return (PCAP_ERROR
);
1238 /* Configure the driver */
1239 cfg_km
.cmd
= CFG_INIT
;
1240 cfg_km
.kmid
= cfg_ld
.kmid
;
1241 cfg_km
.mdilen
= sizeof(cfg_bpf
);
1242 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
1243 for (i
= 0; i
< BPF_MINORS
; i
++) {
1244 cfg_bpf
.devno
= domakedev(major
, i
);
1245 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
1246 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1247 "bpf_load: could not configure driver: %s",
1249 return (PCAP_ERROR
);
1260 * Turn off rfmon mode if necessary.
1263 pcap_cleanup_bpf(pcap_t
*p
)
1265 #ifdef HAVE_BSD_IEEE80211
1267 struct ifmediareq req
;
1271 if (p
->md
.must_do_on_close
!= 0) {
1273 * There's something we have to do when closing this
1276 #ifdef HAVE_BSD_IEEE80211
1277 if (p
->md
.must_do_on_close
& MUST_CLEAR_RFMON
) {
1279 * We put the interface into rfmon mode;
1280 * take it out of rfmon mode.
1282 * XXX - if somebody else wants it in rfmon
1283 * mode, this code cannot know that, so it'll take
1284 * it out of rfmon mode.
1286 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
1289 "Can't restore interface flags (socket() failed: %s).\n"
1290 "Please adjust manually.\n",
1293 memset(&req
, 0, sizeof(req
));
1294 strncpy(req
.ifm_name
, p
->md
.device
,
1295 sizeof(req
.ifm_name
));
1296 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1298 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
1299 "Please adjust manually.\n",
1302 if (req
.ifm_current
& IFM_IEEE80211_MONITOR
) {
1304 * Rfmon mode is currently on;
1307 memset(&ifr
, 0, sizeof(ifr
));
1308 (void)strncpy(ifr
.ifr_name
,
1310 sizeof(ifr
.ifr_name
));
1312 req
.ifm_current
& ~IFM_IEEE80211_MONITOR
;
1313 if (ioctl(sock
, SIOCSIFMEDIA
,
1316 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
1317 "Please adjust manually.\n",
1325 #endif /* HAVE_BSD_IEEE80211 */
1328 * Take this pcap out of the list of pcaps for which we
1329 * have to take the interface out of some mode.
1331 pcap_remove_from_pcaps_to_close(p
);
1332 p
->md
.must_do_on_close
= 0;
1335 #ifdef HAVE_ZEROCOPY_BPF
1336 if (p
->md
.zerocopy
) {
1338 * Delete the mappings. Note that p->buffer gets
1339 * initialized to one of the mmapped regions in
1340 * this case, so do not try and free it directly;
1341 * null it out so that pcap_cleanup_live_common()
1342 * doesn't try to free it.
1344 if (p
->md
.zbuf1
!= MAP_FAILED
&& p
->md
.zbuf1
!= NULL
)
1345 (void) munmap(p
->md
.zbuf1
, p
->md
.zbufsize
);
1346 if (p
->md
.zbuf2
!= MAP_FAILED
&& p
->md
.zbuf2
!= NULL
)
1347 (void) munmap(p
->md
.zbuf2
, p
->md
.zbufsize
);
1351 if (p
->md
.device
!= NULL
) {
1353 p
->md
.device
= NULL
;
1355 pcap_cleanup_live_common(p
);
1359 check_setif_failure(pcap_t
*p
, int error
)
1367 if (error
== ENXIO
) {
1369 * No such device exists.
1372 if (p
->opt
.rfmon
&& strncmp(p
->opt
.source
, "wlt", 3) == 0) {
1374 * Monitor mode was requested, and we're trying
1375 * to open a "wltN" device. Assume that this
1376 * is 10.4 and that we were asked to open an
1377 * "enN" device; if that device exists, return
1378 * "monitor mode not supported on the device".
1380 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1382 strlcpy(ifr
.ifr_name
, "en",
1383 sizeof(ifr
.ifr_name
));
1384 strlcat(ifr
.ifr_name
, p
->opt
.source
+ 3,
1385 sizeof(ifr
.ifr_name
));
1386 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
1388 * We assume this failed because
1389 * the underlying device doesn't
1392 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1393 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1394 "SIOCGIFFLAGS on %s failed: %s",
1395 ifr
.ifr_name
, pcap_strerror(errno
));
1398 * The underlying "enN" device
1399 * exists, but there's no
1400 * corresponding "wltN" device;
1401 * that means that the "enN"
1402 * device doesn't support
1403 * monitor mode, probably because
1404 * it's an Ethernet device rather
1405 * than a wireless device.
1407 err
= PCAP_ERROR_RFMON_NOTSUP
;
1412 * We can't find out whether there's
1413 * an underlying "enN" device, so
1414 * just report "no such device".
1416 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1417 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1418 "socket() failed: %s",
1419 pcap_strerror(errno
));
1427 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF failed: %s",
1428 pcap_strerror(errno
));
1429 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1430 } else if (errno
== ENETDOWN
) {
1432 * Return a "network down" indication, so that
1433 * the application can report that rather than
1434 * saying we had a mysterious failure and
1435 * suggest that they report a problem to the
1436 * libpcap developers.
1438 return (PCAP_ERROR_IFACE_NOT_UP
);
1441 * Some other error; fill in the error string, and
1442 * return PCAP_ERROR.
1444 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1445 p
->opt
.source
, pcap_strerror(errno
));
1446 return (PCAP_ERROR
);
1451 * Default capture buffer size.
1452 * 32K isn't very much for modern machines with fast networks; we
1453 * pick .5M, as that's the maximum on at least some systems with BPF.
1455 #define DEFAULT_BUFSIZE 524288
1458 pcap_activate_bpf(pcap_t
*p
)
1463 struct bpf_version bv
;
1466 char *wltdev
= NULL
;
1469 struct bpf_dltlist bdl
;
1470 #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1473 #endif /* BIOCGDLTLIST */
1474 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1475 u_int spoof_eth_src
= 1;
1478 struct bpf_insn total_insn
;
1479 struct bpf_program total_prog
;
1480 struct utsname osinfo
;
1481 int have_osinfo
= 0;
1482 #ifdef HAVE_ZEROCOPY_BPF
1484 u_int bufmode
, zbufmax
;
1495 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
1496 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
1497 pcap_strerror(errno
));
1498 status
= PCAP_ERROR
;
1501 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
1502 bv
.bv_minor
< BPF_MINOR_VERSION
) {
1503 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1504 "kernel bpf filter out of date");
1505 status
= PCAP_ERROR
;
1509 p
->md
.device
= strdup(p
->opt
.source
);
1510 if (p
->md
.device
== NULL
) {
1511 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
1512 pcap_strerror(errno
));
1513 status
= PCAP_ERROR
;
1518 * Attempt to find out the version of the OS on which we're running.
1520 if (uname(&osinfo
) == 0)
1525 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1526 * of why we check the version number.
1531 * We assume osinfo.sysname is "Darwin", because
1532 * __APPLE__ is defined. We just check the version.
1534 if (osinfo
.release
[0] < '8' &&
1535 osinfo
.release
[1] == '.') {
1537 * 10.3 (Darwin 7.x) or earlier.
1539 status
= PCAP_ERROR_RFMON_NOTSUP
;
1542 if (osinfo
.release
[0] == '8' &&
1543 osinfo
.release
[1] == '.') {
1545 * 10.4 (Darwin 8.x). s/en/wlt/
1547 if (strncmp(p
->opt
.source
, "en", 2) != 0) {
1549 * Not an enN device; check
1550 * whether the device even exists.
1552 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1554 strlcpy(ifr
.ifr_name
,
1556 sizeof(ifr
.ifr_name
));
1557 if (ioctl(sockfd
, SIOCGIFFLAGS
,
1558 (char *)&ifr
) < 0) {
1566 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1569 "SIOCGIFFLAGS failed: %s",
1570 pcap_strerror(errno
));
1572 status
= PCAP_ERROR_RFMON_NOTSUP
;
1576 * We can't find out whether
1577 * the device exists, so just
1578 * report "no such device".
1580 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1583 "socket() failed: %s",
1584 pcap_strerror(errno
));
1588 wltdev
= malloc(strlen(p
->opt
.source
) + 2);
1589 if (wltdev
== NULL
) {
1590 (void)snprintf(p
->errbuf
,
1591 PCAP_ERRBUF_SIZE
, "malloc: %s",
1592 pcap_strerror(errno
));
1593 status
= PCAP_ERROR
;
1596 strcpy(wltdev
, "wlt");
1597 strcat(wltdev
, p
->opt
.source
+ 2);
1598 free(p
->opt
.source
);
1599 p
->opt
.source
= wltdev
;
1602 * Everything else is 10.5 or later; for those,
1603 * we just open the enN device, and set the DLT.
1607 #endif /* __APPLE__ */
1608 #ifdef HAVE_ZEROCOPY_BPF
1610 * If the BPF extension to set buffer mode is present, try setting
1611 * the mode to zero-copy. If that fails, use regular buffering. If
1612 * it succeeds but other setup fails, return an error to the user.
1614 bufmode
= BPF_BUFMODE_ZBUF
;
1615 if (ioctl(fd
, BIOCSETBUFMODE
, (caddr_t
)&bufmode
) == 0) {
1617 * We have zerocopy BPF; use it.
1622 * How to pick a buffer size: first, query the maximum buffer
1623 * size supported by zero-copy. This also lets us quickly
1624 * determine whether the kernel generally supports zero-copy.
1625 * Then, if a buffer size was specified, use that, otherwise
1626 * query the default buffer size, which reflects kernel
1627 * policy for a desired default. Round to the nearest page
1630 if (ioctl(fd
, BIOCGETZMAX
, (caddr_t
)&zbufmax
) < 0) {
1631 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGETZMAX: %s",
1632 pcap_strerror(errno
));
1636 if (p
->opt
.buffer_size
!= 0) {
1638 * A buffer size was explicitly specified; use it.
1640 v
= p
->opt
.buffer_size
;
1642 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1643 v
< DEFAULT_BUFSIZE
)
1644 v
= DEFAULT_BUFSIZE
;
1647 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
1649 p
->md
.zbufsize
= roundup(v
, getpagesize());
1650 if (p
->md
.zbufsize
> zbufmax
)
1651 p
->md
.zbufsize
= zbufmax
;
1652 p
->md
.zbuf1
= mmap(NULL
, p
->md
.zbufsize
, PROT_READ
| PROT_WRITE
,
1654 p
->md
.zbuf2
= mmap(NULL
, p
->md
.zbufsize
, PROT_READ
| PROT_WRITE
,
1656 if (p
->md
.zbuf1
== MAP_FAILED
|| p
->md
.zbuf2
== MAP_FAILED
) {
1657 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "mmap: %s",
1658 pcap_strerror(errno
));
1661 bzero(&bz
, sizeof(bz
));
1662 bz
.bz_bufa
= p
->md
.zbuf1
;
1663 bz
.bz_bufb
= p
->md
.zbuf2
;
1664 bz
.bz_buflen
= p
->md
.zbufsize
;
1665 if (ioctl(fd
, BIOCSETZBUF
, (caddr_t
)&bz
) < 0) {
1666 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETZBUF: %s",
1667 pcap_strerror(errno
));
1670 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
, sizeof(ifr
.ifr_name
));
1671 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
1672 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1673 p
->opt
.source
, pcap_strerror(errno
));
1676 v
= p
->md
.zbufsize
- sizeof(struct bpf_zbuf_header
);
1681 * We don't have zerocopy BPF.
1682 * Set the buffer size.
1684 if (p
->opt
.buffer_size
!= 0) {
1686 * A buffer size was explicitly specified; use it.
1688 if (ioctl(fd
, BIOCSBLEN
,
1689 (caddr_t
)&p
->opt
.buffer_size
) < 0) {
1690 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1691 "BIOCSBLEN: %s: %s", p
->opt
.source
,
1692 pcap_strerror(errno
));
1693 status
= PCAP_ERROR
;
1698 * Now bind to the device.
1700 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
1701 sizeof(ifr
.ifr_name
));
1702 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
1703 status
= check_setif_failure(p
, errno
);
1708 * No buffer size was explicitly specified.
1710 * Try finding a good size for the buffer;
1711 * DEFAULT_BUFSIZE may be too big, so keep
1712 * cutting it in half until we find a size
1713 * that works, or run out of sizes to try.
1714 * If the default is larger, don't make it smaller.
1716 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1717 v
< DEFAULT_BUFSIZE
)
1718 v
= DEFAULT_BUFSIZE
;
1719 for ( ; v
!= 0; v
>>= 1) {
1721 * Ignore the return value - this is because the
1722 * call fails on BPF systems that don't have
1723 * kernel malloc. And if the call fails, it's
1724 * no big deal, we just continue to use the
1725 * standard buffer size.
1727 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
1729 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
1730 sizeof(ifr
.ifr_name
));
1731 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
1732 break; /* that size worked; we're done */
1734 if (errno
!= ENOBUFS
) {
1735 status
= check_setif_failure(p
, errno
);
1741 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1742 "BIOCSBLEN: %s: No buffer size worked",
1744 status
= PCAP_ERROR
;
1750 /* Get the data link layer type. */
1751 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
1752 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
1753 pcap_strerror(errno
));
1754 status
= PCAP_ERROR
;
1760 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
1783 * We don't know what to map this to yet.
1785 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
1787 status
= PCAP_ERROR
;
1791 #if _BSDI_VERSION - 0 >= 199510
1792 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
1807 case 12: /*DLT_C_HDLC*/
1815 * We know the default link type -- now determine all the DLTs
1816 * this interface supports. If this fails with EINVAL, it's
1817 * not fatal; we just don't get to use the feature later.
1819 if (get_dlt_list(fd
, v
, &bdl
, p
->errbuf
) == -1) {
1820 status
= PCAP_ERROR
;
1823 p
->dlt_count
= bdl
.bfl_len
;
1824 p
->dlt_list
= bdl
.bfl_list
;
1828 * Monitor mode fun, continued.
1830 * For 10.5 and, we're assuming, later releases, as noted above,
1831 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
1832 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
1833 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
1836 * Therefore, if the user asked for monitor mode, we filter out
1837 * the DLT_EN10MB value, as you can't get that in monitor mode,
1838 * and, if the user didn't ask for monitor mode, we filter out
1839 * the 802.11 DLT_ values, because selecting those will turn
1840 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
1841 * radio DLT_ value is offered, we try to select that, otherwise
1842 * we try to select DLT_IEEE802_11.
1845 if (isdigit((unsigned)osinfo
.release
[0]) &&
1846 (osinfo
.release
[0] == '9' ||
1847 isdigit((unsigned)osinfo
.release
[1]))) {
1849 * 10.5 (Darwin 9.x), or later.
1851 new_dlt
= find_802_11(&bdl
);
1852 if (new_dlt
!= -1) {
1854 * We have at least one 802.11 DLT_ value,
1855 * so this is an 802.11 interface.
1856 * new_dlt is the best of the 802.11
1857 * DLT_ values in the list.
1861 * Our caller wants monitor mode.
1862 * Purge DLT_EN10MB from the list
1863 * of link-layer types, as selecting
1864 * it will keep monitor mode off.
1869 * If the new mode we want isn't
1870 * the default mode, attempt to
1871 * select the new mode.
1874 if (ioctl(p
->fd
, BIOCSDLT
,
1886 * Our caller doesn't want
1887 * monitor mode. Unless this
1888 * is being done by pcap_open_live(),
1889 * purge the 802.11 link-layer types
1890 * from the list, as selecting
1891 * one of them will turn monitor
1900 * The caller requested monitor
1901 * mode, but we have no 802.11
1902 * link-layer types, so they
1905 status
= PCAP_ERROR_RFMON_NOTSUP
;
1911 #elif defined(HAVE_BSD_IEEE80211)
1913 * *BSD with the new 802.11 ioctls.
1914 * Do we want monitor mode?
1918 * Try to put the interface into monitor mode.
1920 status
= monitor_mode(p
, 1);
1929 * We're in monitor mode.
1930 * Try to find the best 802.11 DLT_ value and, if we
1931 * succeed, try to switch to that mode if we're not
1932 * already in that mode.
1934 new_dlt
= find_802_11(&bdl
);
1935 if (new_dlt
!= -1) {
1937 * We have at least one 802.11 DLT_ value.
1938 * new_dlt is the best of the 802.11
1939 * DLT_ values in the list.
1941 * If the new mode we want isn't the default mode,
1942 * attempt to select the new mode.
1945 if (ioctl(p
->fd
, BIOCSDLT
, &new_dlt
) != -1) {
1947 * We succeeded; make this the
1955 #endif /* various platforms */
1956 #endif /* BIOCGDLTLIST */
1959 * If this is an Ethernet device, and we don't have a DLT_ list,
1960 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
1961 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
1962 * do, but there's not much we can do about that without finding
1963 * some other way of determining whether it's an Ethernet or 802.11
1966 if (v
== DLT_EN10MB
&& p
->dlt_count
== 0) {
1967 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
1969 * If that fails, just leave the list empty.
1971 if (p
->dlt_list
!= NULL
) {
1972 p
->dlt_list
[0] = DLT_EN10MB
;
1973 p
->dlt_list
[1] = DLT_DOCSIS
;
1979 p
->fddipad
= PCAP_FDDIPAD
;
1985 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1987 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
1988 * the link-layer source address isn't forcibly overwritten.
1989 * (Should we ignore errors? Should we do this only if
1990 * we're open for writing?)
1992 * XXX - I seem to remember some packet-sending bug in some
1993 * BSDs - check CVS log for "bpf.c"?
1995 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1996 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1997 "BIOCSHDRCMPLT: %s", pcap_strerror(errno
));
1998 status
= PCAP_ERROR
;
2003 #ifdef HAVE_ZEROCOPY_BPF
2004 if (p
->md
.timeout
!= 0 && !p
->md
.zerocopy
) {
2006 if (p
->md
.timeout
) {
2009 * XXX - is this seconds/nanoseconds in AIX?
2010 * (Treating it as such doesn't fix the timeout
2011 * problem described below.)
2013 * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
2014 * 64-bit userland - it takes, as an argument, a
2015 * "struct BPF_TIMEVAL", which has 32-bit tv_sec
2016 * and tv_usec, rather than a "struct timeval".
2018 * If this platform defines "struct BPF_TIMEVAL",
2019 * we check whether the structure size in BIOCSRTIMEOUT
2020 * is that of a "struct timeval" and, if not, we use
2021 * a "struct BPF_TIMEVAL" rather than a "struct timeval".
2022 * (That way, if the bug is fixed in a future release,
2023 * we will still do the right thing.)
2026 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2027 struct BPF_TIMEVAL bpf_to
;
2029 if (IOCPARM_LEN(BIOCSRTIMEOUT
) != sizeof(struct timeval
)) {
2030 bpf_to
.tv_sec
= p
->md
.timeout
/ 1000;
2031 bpf_to
.tv_usec
= (p
->md
.timeout
* 1000) % 1000000;
2032 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&bpf_to
) < 0) {
2033 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2034 "BIOCSRTIMEOUT: %s", pcap_strerror(errno
));
2035 status
= PCAP_ERROR
;
2040 to
.tv_sec
= p
->md
.timeout
/ 1000;
2041 to
.tv_usec
= (p
->md
.timeout
* 1000) % 1000000;
2042 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
2043 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2044 "BIOCSRTIMEOUT: %s", pcap_strerror(errno
));
2045 status
= PCAP_ERROR
;
2048 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2054 #ifdef BIOCIMMEDIATE
2056 * Darren Reed notes that
2058 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
2059 * timeout appears to be ignored and it waits until the buffer
2060 * is filled before returning. The result of not having it
2061 * set is almost worse than useless if your BPF filter
2062 * is reducing things to only a few packets (i.e. one every
2065 * so we turn BIOCIMMEDIATE mode on if this is AIX.
2067 * We don't turn it on for other platforms, as that means we
2068 * get woken up for every packet, which may not be what we want;
2069 * in the Winter 1993 USENIX paper on BPF, they say:
2071 * Since a process might want to look at every packet on a
2072 * network and the time between packets can be only a few
2073 * microseconds, it is not possible to do a read system call
2074 * per packet and BPF must collect the data from several
2075 * packets and return it as a unit when the monitoring
2076 * application does a read.
2078 * which I infer is the reason for the timeout - it means we
2079 * wait that amount of time, in the hopes that more packets
2080 * will arrive and we'll get them all with one read.
2082 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
2083 * BSDs) causes the timeout to be ignored.
2085 * On the other hand, some platforms (e.g., Linux) don't support
2086 * timeouts, they just hand stuff to you as soon as it arrives;
2087 * if that doesn't cause a problem on those platforms, it may
2088 * be OK to have BIOCIMMEDIATE mode on BSD as well.
2090 * (Note, though, that applications may depend on the read
2091 * completing, even if no packets have arrived, when the timeout
2092 * expires, e.g. GUI applications that have to check for input
2093 * while waiting for packets to arrive; a non-zero timeout
2094 * prevents "select()" from working right on FreeBSD and
2095 * possibly other BSDs, as the timer doesn't start until a
2096 * "read()" is done, so the timer isn't in effect if the
2097 * application is blocked on a "select()", and the "select()"
2098 * doesn't get woken up for a BPF device until the buffer
2102 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
2103 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCIMMEDIATE: %s",
2104 pcap_strerror(errno
));
2105 status
= PCAP_ERROR
;
2108 #endif /* BIOCIMMEDIATE */
2111 if (p
->opt
.promisc
) {
2112 /* set promiscuous mode, just warn if it fails */
2113 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
2114 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
2115 pcap_strerror(errno
));
2116 status
= PCAP_WARNING_PROMISC_NOTSUP
;
2120 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
2121 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
2122 pcap_strerror(errno
));
2123 status
= PCAP_ERROR
;
2127 #ifdef HAVE_ZEROCOPY_BPF
2128 if (!p
->md
.zerocopy
) {
2130 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
2131 if (p
->buffer
== NULL
) {
2132 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
2133 pcap_strerror(errno
));
2134 status
= PCAP_ERROR
;
2138 /* For some strange reason this seems to prevent the EFAULT
2139 * problems we have experienced from AIX BPF. */
2140 memset(p
->buffer
, 0x0, p
->bufsize
);
2142 #ifdef HAVE_ZEROCOPY_BPF
2147 * If there's no filter program installed, there's
2148 * no indication to the kernel of what the snapshot
2149 * length should be, so no snapshotting is done.
2151 * Therefore, when we open the device, we install
2152 * an "accept everything" filter with the specified
2155 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
2158 total_insn
.k
= p
->snapshot
;
2160 total_prog
.bf_len
= 1;
2161 total_prog
.bf_insns
= &total_insn
;
2162 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
2163 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2164 pcap_strerror(errno
));
2165 status
= PCAP_ERROR
;
2170 * On most BPF platforms, either you can do a "select()" or
2171 * "poll()" on a BPF file descriptor and it works correctly,
2172 * or you can do it and it will return "readable" if the
2173 * hold buffer is full but not if the timeout expires *and*
2174 * a non-blocking read will, if the hold buffer is empty
2175 * but the store buffer isn't empty, rotate the buffers
2176 * and return what packets are available.
2178 * In the latter case, the fact that a non-blocking read
2179 * will give you the available packets means you can work
2180 * around the failure of "select()" and "poll()" to wake up
2181 * and return "readable" when the timeout expires by using
2182 * the timeout as the "select()" or "poll()" timeout, putting
2183 * the BPF descriptor into non-blocking mode, and read from
2184 * it regardless of whether "select()" reports it as readable
2187 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
2188 * won't wake up and return "readable" if the timer expires
2189 * and non-blocking reads return EWOULDBLOCK if the hold
2190 * buffer is empty, even if the store buffer is non-empty.
2192 * This means the workaround in question won't work.
2194 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2195 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2196 * here". On all other BPF platforms, we set it to the FD for
2197 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2198 * read will, if the hold buffer is empty and the store buffer
2199 * isn't empty, rotate the buffers and return what packets are
2200 * there (and in sufficiently recent versions of OpenBSD
2201 * "select()" and "poll()" should work correctly).
2203 * XXX - what about AIX?
2205 p
->selectable_fd
= p
->fd
; /* assume select() works until we know otherwise */
2208 * We can check what OS this is.
2210 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0) {
2211 if (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
2212 strncmp(osinfo
.release
, "4.4-", 4) == 0)
2213 p
->selectable_fd
= -1;
2217 p
->read_op
= pcap_read_bpf
;
2218 p
->inject_op
= pcap_inject_bpf
;
2219 p
->setfilter_op
= pcap_setfilter_bpf
;
2220 p
->setdirection_op
= pcap_setdirection_bpf
;
2221 p
->set_datalink_op
= pcap_set_datalink_bpf
;
2222 p
->getnonblock_op
= pcap_getnonblock_bpf
;
2223 p
->setnonblock_op
= pcap_setnonblock_bpf
;
2224 p
->stats_op
= pcap_stats_bpf
;
2225 p
->cleanup_op
= pcap_cleanup_bpf
;
2229 pcap_cleanup_bpf(p
);
2234 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
2237 if (dag_platform_finddevs(alldevsp
, errbuf
) < 0)
2239 #endif /* HAVE_DAG_API */
2241 if (snf_platform_finddevs(alldevsp
, errbuf
) < 0)
2243 #endif /* HAVE_SNF_API */
2248 #ifdef HAVE_BSD_IEEE80211
2250 monitor_mode(pcap_t
*p
, int set
)
2253 struct ifmediareq req
;
2259 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
2261 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "can't open socket: %s",
2262 pcap_strerror(errno
));
2263 return (PCAP_ERROR
);
2266 memset(&req
, 0, sizeof req
);
2267 strncpy(req
.ifm_name
, p
->opt
.source
, sizeof req
.ifm_name
);
2270 * Find out how many media types we have.
2272 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2274 * Can't get the media types.
2280 * There's no such device.
2283 return (PCAP_ERROR_NO_SUCH_DEVICE
);
2287 * Interface doesn't support SIOC{G,S}IFMEDIA.
2290 return (PCAP_ERROR_RFMON_NOTSUP
);
2293 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2294 "SIOCGIFMEDIA 1: %s", pcap_strerror(errno
));
2296 return (PCAP_ERROR
);
2299 if (req
.ifm_count
== 0) {
2304 return (PCAP_ERROR_RFMON_NOTSUP
);
2308 * Allocate a buffer to hold all the media types, and
2309 * get the media types.
2311 media_list
= malloc(req
.ifm_count
* sizeof(int));
2312 if (media_list
== NULL
) {
2313 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
2314 pcap_strerror(errno
));
2316 return (PCAP_ERROR
);
2318 req
.ifm_ulist
= media_list
;
2319 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2320 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCGIFMEDIA: %s",
2321 pcap_strerror(errno
));
2324 return (PCAP_ERROR
);
2328 * Look for an 802.11 "automatic" media type.
2329 * We assume that all 802.11 adapters have that media type,
2330 * and that it will carry the monitor mode supported flag.
2333 for (i
= 0; i
< req
.ifm_count
; i
++) {
2334 if (IFM_TYPE(media_list
[i
]) == IFM_IEEE80211
2335 && IFM_SUBTYPE(media_list
[i
]) == IFM_AUTO
) {
2336 /* OK, does it do monitor mode? */
2337 if (media_list
[i
] & IFM_IEEE80211_MONITOR
) {
2346 * This adapter doesn't support monitor mode.
2349 return (PCAP_ERROR_RFMON_NOTSUP
);
2354 * Don't just check whether we can enable monitor mode,
2355 * do so, if it's not already enabled.
2357 if ((req
.ifm_current
& IFM_IEEE80211_MONITOR
) == 0) {
2359 * Monitor mode isn't currently on, so turn it on,
2360 * and remember that we should turn it off when the
2365 * If we haven't already done so, arrange to have
2366 * "pcap_close_all()" called when we exit.
2368 if (!pcap_do_addexit(p
)) {
2370 * "atexit()" failed; don't put the interface
2371 * in monitor mode, just give up.
2373 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2376 return (PCAP_ERROR
);
2378 memset(&ifr
, 0, sizeof(ifr
));
2379 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
2380 sizeof(ifr
.ifr_name
));
2381 ifr
.ifr_media
= req
.ifm_current
| IFM_IEEE80211_MONITOR
;
2382 if (ioctl(sock
, SIOCSIFMEDIA
, &ifr
) == -1) {
2383 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2384 "SIOCSIFMEDIA: %s", pcap_strerror(errno
));
2386 return (PCAP_ERROR
);
2389 p
->md
.must_do_on_close
|= MUST_CLEAR_RFMON
;
2392 * Add this to the list of pcaps to close when we exit.
2394 pcap_add_to_pcaps_to_close(p
);
2399 #endif /* HAVE_BSD_IEEE80211 */
2401 #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
2403 * Check whether we have any 802.11 link-layer types; return the best
2404 * of the 802.11 link-layer types if we find one, and return -1
2407 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
2408 * best 802.11 link-layer type; any of the other 802.11-plus-radio
2409 * headers are second-best; 802.11 with no radio information is
2413 find_802_11(struct bpf_dltlist
*bdlp
)
2419 * Scan the list of DLT_ values, looking for 802.11 values,
2420 * and, if we find any, choose the best of them.
2423 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
2424 switch (bdlp
->bfl_list
[i
]) {
2426 case DLT_IEEE802_11
:
2428 * 802.11, but no radio.
2430 * Offer this, and select it as the new mode
2431 * unless we've already found an 802.11
2432 * header with radio information.
2435 new_dlt
= bdlp
->bfl_list
[i
];
2438 case DLT_PRISM_HEADER
:
2439 case DLT_AIRONET_HEADER
:
2440 case DLT_IEEE802_11_RADIO_AVS
:
2442 * 802.11 with radio, but not radiotap.
2444 * Offer this, and select it as the new mode
2445 * unless we've already found the radiotap DLT_.
2447 if (new_dlt
!= DLT_IEEE802_11_RADIO
)
2448 new_dlt
= bdlp
->bfl_list
[i
];
2451 case DLT_IEEE802_11_RADIO
:
2453 * 802.11 with radiotap.
2455 * Offer this, and select it as the new mode.
2457 new_dlt
= bdlp
->bfl_list
[i
];
2470 #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
2472 #if defined(__APPLE__) && defined(BIOCGDLTLIST)
2474 * Remove DLT_EN10MB from the list of DLT_ values, as we're in monitor mode,
2475 * and DLT_EN10MB isn't supported in monitor mode.
2478 remove_en(pcap_t
*p
)
2483 * Scan the list of DLT_ values and discard DLT_EN10MB.
2486 for (i
= 0; i
< p
->dlt_count
; i
++) {
2487 switch (p
->dlt_list
[i
]) {
2491 * Don't offer this one.
2497 * Just copy this mode over.
2503 * Copy this DLT_ value to its new position.
2505 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2510 * Set the DLT_ count to the number of entries we copied.
2516 * Remove 802.11 link-layer types from the list of DLT_ values, as
2517 * we're not in monitor mode, and those DLT_ values will switch us
2521 remove_802_11(pcap_t
*p
)
2526 * Scan the list of DLT_ values and discard 802.11 values.
2529 for (i
= 0; i
< p
->dlt_count
; i
++) {
2530 switch (p
->dlt_list
[i
]) {
2532 case DLT_IEEE802_11
:
2533 case DLT_PRISM_HEADER
:
2534 case DLT_AIRONET_HEADER
:
2535 case DLT_IEEE802_11_RADIO
:
2536 case DLT_IEEE802_11_RADIO_AVS
:
2538 * 802.11. Don't offer this one.
2544 * Just copy this mode over.
2550 * Copy this DLT_ value to its new position.
2552 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2557 * Set the DLT_ count to the number of entries we copied.
2561 #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
2564 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
2567 * Free any user-mode filter we might happen to have installed.
2569 pcap_freecode(&p
->fcode
);
2572 * Try to install the kernel filter.
2574 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) == 0) {
2578 p
->md
.use_bpf
= 1; /* filtering in the kernel */
2581 * Discard any previously-received packets, as they might
2582 * have passed whatever filter was formerly in effect, but
2583 * might not pass this filter (BIOCSETF discards packets
2584 * buffered in the kernel, so you can lose packets in any
2594 * If it failed with EINVAL, that's probably because the program
2595 * is invalid or too big. Validate it ourselves; if we like it
2596 * (we currently allow backward branches, to support protochain),
2597 * run it in userland. (There's no notion of "too big" for
2600 * Otherwise, just give up.
2601 * XXX - if the copy of the program into the kernel failed,
2602 * we will get EINVAL rather than, say, EFAULT on at least
2605 if (errno
!= EINVAL
) {
2606 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2607 pcap_strerror(errno
));
2612 * install_bpf_program() validates the program.
2614 * XXX - what if we already have a filter in the kernel?
2616 if (install_bpf_program(p
, fp
) < 0)
2618 p
->md
.use_bpf
= 0; /* filtering in userland */
2623 * Set direction flag: Which packets do we accept on a forwarding
2624 * single device? IN, OUT or both?
2627 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
2629 #if defined(BIOCSDIRECTION)
2632 direction
= (d
== PCAP_D_IN
) ? BPF_D_IN
:
2633 ((d
== PCAP_D_OUT
) ? BPF_D_OUT
: BPF_D_INOUT
);
2634 if (ioctl(p
->fd
, BIOCSDIRECTION
, &direction
) == -1) {
2635 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2636 "Cannot set direction to %s: %s",
2637 (d
== PCAP_D_IN
) ? "PCAP_D_IN" :
2638 ((d
== PCAP_D_OUT
) ? "PCAP_D_OUT" : "PCAP_D_INOUT"),
2643 #elif defined(BIOCSSEESENT)
2647 * We don't support PCAP_D_OUT.
2649 if (d
== PCAP_D_OUT
) {
2650 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2651 "Setting direction to PCAP_D_OUT is not supported on BPF");
2655 seesent
= (d
== PCAP_D_INOUT
);
2656 if (ioctl(p
->fd
, BIOCSSEESENT
, &seesent
) == -1) {
2657 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2658 "Cannot set direction to %s: %s",
2659 (d
== PCAP_D_INOUT
) ? "PCAP_D_INOUT" : "PCAP_D_IN",
2665 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2666 "This system doesn't support BIOCSSEESENT, so the direction can't be set");
2672 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
2675 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
2676 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2677 "Cannot set DLT %d: %s", dlt
, strerror(errno
));