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.114 2008-09-16 07:45:11 guy Exp $ (LBL)";
30 #include <sys/param.h> /* optionally get BSD define */
31 #ifdef HAVE_ZEROCOPY_BPF
35 #include <sys/timeb.h>
36 #include <sys/socket.h>
38 #include <sys/ioctl.h>
39 #include <sys/utsname.h>
41 #ifdef HAVE_ZEROCOPY_BPF
42 #include <machine/atomic.h>
50 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
51 * native OS version, as we need "struct bpf_config" from it.
53 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
55 #include <sys/types.h>
58 * Prevent bpf.h from redefining the DLT_ values to their
59 * IFT_ values, as we're going to return the standard libpcap
60 * values, not IBM's non-standard IFT_ values.
66 #include <net/if_types.h> /* for IFT_ values */
67 #include <sys/sysconfig.h>
68 #include <sys/device.h>
69 #include <sys/cfgodm.h>
73 #define domakedev makedev64
74 #define getmajor major64
75 #define bpf_hdr bpf_hdr32
77 #define domakedev makedev
78 #define getmajor major
79 #endif /* __64BIT__ */
81 #define BPF_NAME "bpf"
83 #define DRIVER_PATH "/usr/lib/drivers"
84 #define BPF_NODE "/dev/bpf"
85 static int bpfloadedflag
= 0;
86 static int odmlockid
= 0;
102 #ifdef HAVE_NET_IF_MEDIA_H
103 # include <net/if_media.h>
106 #include "pcap-int.h"
109 #include "pcap-dag.h"
110 #endif /* HAVE_DAG_API */
112 #ifdef HAVE_OS_PROTO_H
113 #include "os-proto.h"
117 # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
118 #define HAVE_BSD_IEEE80211
121 # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
122 static int find_802_11(struct bpf_dltlist
*);
124 # ifdef HAVE_BSD_IEEE80211
125 static int monitor_mode(pcap_t
*, int);
128 # if defined(__APPLE__)
129 static void remove_en(pcap_t
*);
130 static void remove_802_11(pcap_t
*);
133 # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
135 #endif /* BIOCGDLTLIST */
138 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
139 * don't get DLT_DOCSIS defined.
142 #define DLT_DOCSIS 143
146 * On OS X, we don't even get any of the 802.11-plus-radio-header DLT_'s
147 * defined, even though some of them are used by various Airport drivers.
149 #ifndef DLT_PRISM_HEADER
150 #define DLT_PRISM_HEADER 119
152 #ifndef DLT_AIRONET_HEADER
153 #define DLT_AIRONET_HEADER 120
155 #ifndef DLT_IEEE802_11_RADIO
156 #define DLT_IEEE802_11_RADIO 127
158 #ifndef DLT_IEEE802_11_RADIO_AVS
159 #define DLT_IEEE802_11_RADIO_AVS 163
162 static int pcap_can_set_rfmon_bpf(pcap_t
*p
);
163 static int pcap_activate_bpf(pcap_t
*p
);
164 static int pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
);
165 static int pcap_setdirection_bpf(pcap_t
*, pcap_direction_t
);
166 static int pcap_set_datalink_bpf(pcap_t
*p
, int dlt
);
168 #ifdef HAVE_ZEROCOPY_BPF
170 * For zerocopy bpf, we need to override the setnonblock/getnonblock routines
171 * so we don't call select(2) if the pcap handle is in non-blocking mode. We
172 * preserve the timeout supplied by pcap_open functions to make sure it
173 * does not get clobbered if the pcap handle moves between blocking and non-
177 pcap_getnonblock_zbuf(pcap_t
*p
, char *errbuf
)
180 * Use a negative value for the timeout to represent that the
181 * pcap handle is in non-blocking mode.
183 return (p
->md
.timeout
< 0);
187 pcap_setnonblock_zbuf(pcap_t
*p
, int nonblock
, char *errbuf
)
190 * Map each value to the corresponding 2's complement, to
191 * preserve the timeout value provided with pcap_set_timeout.
192 * (from pcap-linux.c).
195 if (p
->md
.timeout
> 0)
196 p
->md
.timeout
= p
->md
.timeout
* -1 - 1;
198 if (p
->md
.timeout
< 0)
199 p
->md
.timeout
= (p
->md
.timeout
+ 1) * -1;
204 * Zero-copy specific close method. Un-map the shared buffers then call
205 * pcap_cleanup_live_common.
208 pcap_close_zbuf(pcap_t
*p
)
211 * Check to see if this pcap instance was using the zerocopy buffer
212 * mode. If it was, delete the mappings. Note that p->buffer
213 * gets initialized to one of the mmaped regions in this case, so
214 * do not try and free it directly.
216 * If the regular buffer mode was selected, then it is safe to free
219 if (p
->md
.zerocopy
== 0) {
220 pcap_cleanup_live_common(p
);
223 if (p
->md
.zbuf1
!= MAP_FAILED
&& p
->md
.zbuf1
!= NULL
)
224 (void) munmap(p
->md
.zbuf1
, p
->md
.zbufsize
);
225 if (p
->md
.zbuf2
!= MAP_FAILED
&& p
->md
.zbuf2
!= NULL
)
226 (void) munmap(p
->md
.zbuf2
, p
->md
.zbufsize
);
228 pcap_cleanup_live_common(p
);
232 * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
233 * shared memory buffers.
235 * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer,
236 * and set up p->buffer and cc to reflect one if available. Notice that if
237 * there was no prior buffer, we select zbuf1 as this will be the first
238 * buffer filled for a fresh BPF session.
241 pcap_next_zbuf_shm(pcap_t
*p
, int *cc
)
243 struct bpf_zbuf_header
*bzh
;
245 if (p
->md
.zbuffer
== p
->md
.zbuf2
|| p
->md
.zbuffer
== NULL
) {
246 bzh
= (struct bpf_zbuf_header
*)p
->md
.zbuf1
;
247 if (bzh
->bzh_user_gen
!=
248 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
250 p
->md
.zbuffer
= (u_char
*)p
->md
.zbuf1
;
251 p
->buffer
= p
->md
.zbuffer
+ sizeof(*bzh
);
252 *cc
= bzh
->bzh_kernel_len
;
255 } else if (p
->md
.zbuffer
== p
->md
.zbuf1
) {
256 bzh
= (struct bpf_zbuf_header
*)p
->md
.zbuf2
;
257 if (bzh
->bzh_user_gen
!=
258 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
260 p
->md
.zbuffer
= (u_char
*)p
->md
.zbuf2
;
261 p
->buffer
= p
->md
.zbuffer
+ sizeof(*bzh
);
262 *cc
= bzh
->bzh_kernel_len
;
271 * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
272 * select() for data or a timeout, and possibly force rotation of the buffer
273 * in the event we time out or are in immediate mode. Invoke the shared
274 * memory check before doing system calls in order to avoid doing avoidable
278 pcap_next_zbuf(pcap_t
*p
, int *cc
)
287 #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000))
289 * Start out by seeing whether anything is waiting by checking the
290 * next shared memory buffer for data.
292 data
= pcap_next_zbuf_shm(p
, cc
);
296 * If a previous sleep was interrupted due to signal delivery, make
297 * sure that the timeout gets adjusted accordingly. This requires
298 * that we analyze when the timeout should be been expired, and
299 * subtract the current time from that. If after this operation,
300 * our timeout is less then or equal to zero, handle it like a
303 tmout
= p
->md
.timeout
;
305 (void) clock_gettime(CLOCK_MONOTONIC
, &cur
);
306 if (p
->md
.interrupted
&& p
->md
.timeout
) {
307 expire
= TSTOMILLI(&p
->md
.firstsel
) + p
->md
.timeout
;
308 tmout
= expire
- TSTOMILLI(&cur
);
311 p
->md
.interrupted
= 0;
312 data
= pcap_next_zbuf_shm(p
, cc
);
315 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
316 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
317 "BIOCROTZBUF: %s", strerror(errno
));
320 return (pcap_next_zbuf_shm(p
, cc
));
324 * No data in the buffer, so must use select() to wait for data or
325 * the next timeout. Note that we only call select if the handle
326 * is in blocking mode.
328 if (p
->md
.timeout
>= 0) {
330 FD_SET(p
->fd
, &r_set
);
332 tv
.tv_sec
= tmout
/ 1000;
333 tv
.tv_usec
= (tmout
* 1000) % 1000000;
335 r
= select(p
->fd
+ 1, &r_set
, NULL
, NULL
,
336 p
->md
.timeout
!= 0 ? &tv
: NULL
);
337 if (r
< 0 && errno
== EINTR
) {
338 if (!p
->md
.interrupted
&& p
->md
.timeout
) {
339 p
->md
.interrupted
= 1;
340 p
->md
.firstsel
= cur
;
344 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
345 "select: %s", strerror(errno
));
349 p
->md
.interrupted
= 0;
351 * Check again for data, which may exist now that we've either been
352 * woken up as a result of data or timed out. Try the "there's data"
353 * case first since it doesn't require a system call.
355 data
= pcap_next_zbuf_shm(p
, cc
);
359 * Try forcing a buffer rotation to dislodge timed out or immediate
362 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
363 (void) snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
364 "BIOCROTZBUF: %s", strerror(errno
));
367 return (pcap_next_zbuf_shm(p
, cc
));
371 * Notify kernel that we are done with the buffer. We don't reset zbuffer so
372 * that we know which buffer to use next time around.
375 pcap_ack_zbuf(pcap_t
*p
)
378 atomic_store_rel_int(&p
->md
.bzh
->bzh_user_gen
,
379 p
->md
.bzh
->bzh_kernel_gen
);
387 pcap_create(const char *device
, char *ebuf
)
392 if (strstr(device
, "dag"))
393 return (dag_create(device
, ebuf
));
394 #endif /* HAVE_DAG_API */
396 p
= pcap_create_common(device
, ebuf
);
400 p
->activate_op
= pcap_activate_bpf
;
401 #ifdef HAVE_ZEROCOPY_BPF
402 p
->cleanup_op
= pcap_close_zbuf
;
403 p
->setnonblock_op
= pcap_setnonblock_zbuf
;
404 p
->getnonblock_op
= pcap_getnonblock_zbuf
;
406 p
->can_set_rfmon_op
= pcap_can_set_rfmon_bpf
;
414 #ifdef HAVE_CLONING_BPF
415 static const char device
[] = "/dev/bpf";
418 char device
[sizeof "/dev/bpf0000000000"];
423 * Load the bpf driver, if it isn't already loaded,
424 * and create the BPF device entries, if they don't
427 if (bpf_load(p
->errbuf
) == -1)
431 #ifdef HAVE_CLONING_BPF
432 if ((fd
= open(device
, O_RDWR
)) == -1 &&
433 (errno
!= EACCES
|| (fd
= open(device
, O_RDONLY
)) == -1)) {
435 fd
= PCAP_ERROR_PERM_DENIED
;
438 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
439 "(cannot open device) %s: %s", device
, pcap_strerror(errno
));
443 * Go through all the minors and find one that isn't in use.
446 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
448 * Initially try a read/write open (to allow the inject
449 * method to work). If that fails due to permission
450 * issues, fall back to read-only. This allows a
451 * non-root user to be granted specific access to pcap
452 * capabilities via file permissions.
454 * XXX - we should have an API that has a flag that
455 * controls whether to open read-only or read-write,
456 * so that denial of permission to send (or inability
457 * to send, if sending packets isn't supported on
458 * the device in question) can be indicated at open
461 fd
= open(device
, O_RDWR
);
462 if (fd
== -1 && errno
== EACCES
)
463 fd
= open(device
, O_RDONLY
);
464 } while (fd
< 0 && errno
== EBUSY
);
467 * XXX better message for all minors used
471 fd
= PCAP_ERROR_PERM_DENIED
;
474 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "(no devices found) %s: %s",
475 device
, pcap_strerror(errno
));
484 get_dlt_list(int fd
, int v
, struct bpf_dltlist
*bdlp
, char *ebuf
)
486 memset(bdlp
, 0, sizeof(*bdlp
));
487 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) == 0) {
491 bdlp
->bfl_list
= (u_int
*) malloc(sizeof(u_int
) * (bdlp
->bfl_len
+ 1));
492 if (bdlp
->bfl_list
== NULL
) {
493 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
494 pcap_strerror(errno
));
498 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) < 0) {
499 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
500 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
501 free(bdlp
->bfl_list
);
506 * OK, for real Ethernet devices, add DLT_DOCSIS to the
507 * list, so that an application can let you choose it,
508 * in case you're capturing DOCSIS traffic that a Cisco
509 * Cable Modem Termination System is putting out onto
510 * an Ethernet (it doesn't put an Ethernet header onto
511 * the wire, it puts raw DOCSIS frames out on the wire
512 * inside the low-level Ethernet framing).
514 * A "real Ethernet device" is defined here as a device
515 * that has a link-layer type of DLT_EN10MB and that has
516 * no alternate link-layer types; that's done to exclude
517 * 802.11 interfaces (which might or might not be the
518 * right thing to do, but I suspect it is - Ethernet <->
519 * 802.11 bridges would probably badly mishandle frames
520 * that don't have Ethernet headers).
522 if (v
== DLT_EN10MB
) {
524 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
525 if (bdlp
->bfl_list
[i
] != DLT_EN10MB
) {
532 * We reserved one more slot at the end of
535 bdlp
->bfl_list
[bdlp
->bfl_len
] = DLT_DOCSIS
;
541 * EINVAL just means "we don't support this ioctl on
542 * this device"; don't treat it as an error.
544 if (errno
!= EINVAL
) {
545 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
546 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
555 pcap_can_set_rfmon_bpf(pcap_t
*p
)
557 #if defined(__APPLE__)
558 struct utsname osinfo
;
562 struct bpf_dltlist bdl
;
566 * The joys of monitor mode on OS X.
568 * Prior to 10.4, it's not supported at all.
570 * In 10.4, if adapter enN supports monitor mode, there's a
571 * wltN adapter corresponding to it; you open it, instead of
572 * enN, to get monitor mode. You get whatever link-layer
573 * headers it supplies.
575 * In 10.5, and, we assume, later releases, if adapter enN
576 * supports monitor mode, it offers, among its selectable
577 * DLT_ values, values that let you get the 802.11 header;
578 * selecting one of those values puts the adapter into monitor
579 * mode (i.e., you can't get 802.11 headers except in monitor
580 * mode, and you can't get Ethernet headers in monitor mode).
582 if (uname(&osinfo
) == -1) {
584 * Can't get the OS version; just say "no".
589 * We assume osinfo.sysname is "Darwin", because
590 * __APPLE__ is defined. We just check the version.
592 if (osinfo
.release
[0] < '8' && osinfo
.release
[1] == '.') {
594 * 10.3 (Darwin 7.x) or earlier.
595 * Monitor mode not supported.
599 if (osinfo
.release
[0] == '8' && osinfo
.release
[1] == '.') {
601 * 10.4 (Darwin 8.x). s/en/wlt/, and check
602 * whether the device exists.
604 if (strncmp(p
->opt
.source
, "en", 2) != 0) {
606 * Not an enN device; no monitor mode.
610 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
612 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
613 "socket: %s", pcap_strerror(errno
));
616 strlcpy(ifr
.ifr_name
, "wlt", sizeof(ifr
.ifr_name
));
617 strlcat(ifr
.ifr_name
, p
->opt
.source
+ 2, sizeof(ifr
.ifr_name
));
618 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
631 * Everything else is 10.5 or later; for those,
632 * we just open the enN device, and check whether
633 * we have any 802.11 devices.
635 * First, open a BPF device.
642 * Now bind to the device.
644 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
, sizeof(ifr
.ifr_name
));
645 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
646 if (errno
== ENETDOWN
) {
648 * Return a "network down" indication, so that
649 * the application can report that rather than
650 * saying we had a mysterious failure and
651 * suggest that they report a problem to the
652 * libpcap developers.
655 return (PCAP_ERROR_IFACE_NOT_UP
);
657 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
659 p
->opt
.source
, pcap_strerror(errno
));
666 * We know the default link type -- now determine all the DLTs
667 * this interface supports. If this fails with EINVAL, it's
668 * not fatal; we just don't get to use the feature later.
669 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
670 * as the default DLT for this adapter.)
672 if (get_dlt_list(fd
, DLT_NULL
, &bdl
, p
->errbuf
) == -1) {
676 if (find_802_11(&bdl
) != -1) {
678 * We have an 802.11 DLT, so we can set monitor mode.
685 #endif /* BIOCGDLTLIST */
687 #elif defined(HAVE_BSD_IEEE80211)
690 ret
= monitor_mode(p
, 0);
691 if (ret
== PCAP_ERROR_RFMON_NOTSUP
)
692 return (0); /* not an error, just a "can't do" */
694 return (1); /* success */
702 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
707 * "ps_recv" counts packets handed to the filter, not packets
708 * that passed the filter. This includes packets later dropped
709 * because we ran out of buffer space.
711 * "ps_drop" counts packets dropped inside the BPF device
712 * because we ran out of buffer space. It doesn't count
713 * packets dropped by the interface driver. It counts
714 * only packets that passed the filter.
716 * Both statistics include packets not yet read from the kernel
717 * by libpcap, and thus not yet seen by the application.
719 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
720 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
721 pcap_strerror(errno
));
725 ps
->ps_recv
= s
.bs_recv
;
726 ps
->ps_drop
= s
.bs_drop
;
731 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
735 register u_char
*bp
, *ep
;
740 #ifdef HAVE_ZEROCOPY_BPF
746 * Has "pcap_breakloop()" been called?
750 * Yes - clear the flag that indicates that it
751 * has, and return -2 to indicate that we were
752 * told to break out of the loop.
760 * When reading without zero-copy from a file descriptor, we
761 * use a single buffer and return a length of data in the
762 * buffer. With zero-copy, we update the p->buffer pointer
763 * to point at whatever underlying buffer contains the next
764 * data and update cc to reflect the data found in the
767 #ifdef HAVE_ZEROCOPY_BPF
768 if (p
->md
.zerocopy
) {
769 if (p
->buffer
!= NULL
)
771 i
= pcap_next_zbuf(p
, &cc
);
779 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
782 /* Don't choke when we get ptraced */
791 * Sigh. More AIX wonderfulness.
793 * For some unknown reason the uiomove()
794 * operation in the bpf kernel extension
795 * used to copy the buffer into user
796 * space sometimes returns EFAULT. I have
797 * no idea why this is the case given that
798 * a kernel debugger shows the user buffer
799 * is correct. This problem appears to
800 * be mostly mitigated by the memset of
801 * the buffer before it is first used.
802 * Very strange.... Shaun Clowes
804 * In any case this means that we shouldn't
805 * treat EFAULT as a fatal error; as we
806 * don't have an API for returning
807 * a "some packets were dropped since
808 * the last packet you saw" indication,
809 * we just ignore EFAULT and keep reading.
816 #if defined(sun) && !defined(BSD)
818 * Due to a SunOS bug, after 2^31 bytes, the kernel
819 * file offset overflows and read fails with EINVAL.
820 * The lseek() to 0 will fix things.
823 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
825 (void)lseek(p
->fd
, 0L, SEEK_SET
);
831 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
832 pcap_strerror(errno
));
840 * Loop through each packet.
842 #define bhp ((struct bpf_hdr *)bp)
848 register int caplen
, hdrlen
;
851 * Has "pcap_breakloop()" been called?
852 * If so, return immediately - if we haven't read any
853 * packets, clear the flag and return -2 to indicate
854 * that we were told to break out of the loop, otherwise
855 * leave the flag set, so that the *next* call will break
856 * out of the loop without having read any packets, and
857 * return the number of packets we've processed so far.
870 caplen
= bhp
->bh_caplen
;
871 hdrlen
= bhp
->bh_hdrlen
;
874 * Short-circuit evaluation: if using BPF filter
875 * in kernel, no need to do it now - we already know
876 * the packet passed the filter.
879 * Note: the filter code was generated assuming
880 * that p->fddipad was the amount of padding
881 * before the header, as that's what's required
882 * in the kernel, so we run the filter before
883 * skipping that padding.
887 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
888 struct pcap_pkthdr pkthdr
;
890 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
893 * AIX's BPF returns seconds/nanoseconds time
894 * stamps, not seconds/microseconds time stamps.
896 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
898 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
902 pkthdr
.caplen
= caplen
- pad
;
905 if (bhp
->bh_datalen
> pad
)
906 pkthdr
.len
= bhp
->bh_datalen
- pad
;
911 pkthdr
.caplen
= caplen
;
912 pkthdr
.len
= bhp
->bh_datalen
;
914 (*callback
)(user
, &pkthdr
, datap
);
915 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
916 if (++n
>= cnt
&& cnt
> 0) {
925 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
934 pcap_inject_bpf(pcap_t
*p
, const void *buf
, size_t size
)
938 ret
= write(p
->fd
, buf
, size
);
940 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
942 * In Mac OS X, there's a bug wherein setting the
943 * BIOCSHDRCMPLT flag causes writes to fail; see,
946 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
948 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
949 * assume it's due to that bug, and turn off that flag
950 * and try again. If we succeed, it either means that
951 * somebody applied the fix from that URL, or other patches
954 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
956 * and are running a Darwin kernel with those fixes, or
957 * that Apple fixed the problem in some OS X release.
959 u_int spoof_eth_src
= 0;
961 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
962 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
963 "send: can't turn off BIOCSHDRCMPLT: %s",
964 pcap_strerror(errno
));
969 * Now try the write again.
971 ret
= write(p
->fd
, buf
, size
);
973 #endif /* __APPLE__ */
975 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
976 pcap_strerror(errno
));
984 bpf_odminit(char *errbuf
)
988 if (odm_initialize() == -1) {
989 if (odm_err_msg(odmerrno
, &errstr
) == -1)
990 errstr
= "Unknown error";
991 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
992 "bpf_load: odm_initialize failed: %s",
997 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
998 if (odm_err_msg(odmerrno
, &errstr
) == -1)
999 errstr
= "Unknown error";
1000 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1001 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
1010 bpf_odmcleanup(char *errbuf
)
1014 if (odm_unlock(odmlockid
) == -1) {
1015 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1016 errstr
= "Unknown error";
1017 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1018 "bpf_load: odm_unlock failed: %s",
1023 if (odm_terminate() == -1) {
1024 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1025 errstr
= "Unknown error";
1026 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1027 "bpf_load: odm_terminate failed: %s",
1036 bpf_load(char *errbuf
)
1040 int numminors
, i
, rc
;
1043 struct bpf_config cfg_bpf
;
1044 struct cfg_load cfg_ld
;
1045 struct cfg_kmod cfg_km
;
1048 * This is very very close to what happens in the real implementation
1049 * but I've fixed some (unlikely) bug situations.
1054 if (bpf_odminit(errbuf
) != 0)
1057 major
= genmajor(BPF_NAME
);
1059 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1060 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
1064 minors
= getminor(major
, &numminors
, BPF_NAME
);
1066 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
1068 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1069 "bpf_load: genminor failed: %s",
1070 pcap_strerror(errno
));
1075 if (bpf_odmcleanup(errbuf
))
1078 rc
= stat(BPF_NODE
"0", &sbuf
);
1079 if (rc
== -1 && errno
!= ENOENT
) {
1080 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1081 "bpf_load: can't stat %s: %s",
1082 BPF_NODE
"0", pcap_strerror(errno
));
1086 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
1087 for (i
= 0; i
< BPF_MINORS
; i
++) {
1088 sprintf(buf
, "%s%d", BPF_NODE
, i
);
1090 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
1091 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1092 "bpf_load: can't mknod %s: %s",
1093 buf
, pcap_strerror(errno
));
1099 /* Check if the driver is loaded */
1100 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
1102 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
1103 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
1104 (cfg_ld
.kmid
== 0)) {
1105 /* Driver isn't loaded, load it now */
1106 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
1107 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1108 "bpf_load: could not load driver: %s",
1114 /* Configure the driver */
1115 cfg_km
.cmd
= CFG_INIT
;
1116 cfg_km
.kmid
= cfg_ld
.kmid
;
1117 cfg_km
.mdilen
= sizeof(cfg_bpf
);
1118 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
1119 for (i
= 0; i
< BPF_MINORS
; i
++) {
1120 cfg_bpf
.devno
= domakedev(major
, i
);
1121 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
1122 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1123 "bpf_load: could not configure driver: %s",
1136 * Turn off rfmon mode if necessary.
1139 pcap_cleanup_bpf(pcap_t
*p
)
1141 #ifdef HAVE_BSD_IEEE80211
1143 struct ifmediareq req
;
1147 if (p
->md
.must_clear
!= 0) {
1149 * There's something we have to do when closing this
1152 #ifdef HAVE_BSD_IEEE80211
1153 if (p
->md
.must_clear
& MUST_CLEAR_RFMON
) {
1155 * We put the interface into rfmon mode;
1156 * take it out of rfmon mode.
1158 * XXX - if somebody else wants it in rfmon
1159 * mode, this code cannot know that, so it'll take
1160 * it out of rfmon mode.
1162 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
1165 "Can't restore interface flags (socket() failed: %s).\n"
1166 "Please adjust manually.\n",
1169 memset(&req
, 0, sizeof(req
));
1170 strncpy(req
.ifm_name
, p
->md
.device
,
1171 sizeof(req
.ifm_name
));
1172 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1174 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
1175 "Please adjust manually.\n",
1178 if (req
.ifm_current
& IFM_IEEE80211_MONITOR
) {
1180 * Rfmon mode is currently on;
1183 memset(&ifr
, 0, sizeof(ifr
));
1184 (void)strncpy(ifr
.ifr_name
,
1186 sizeof(ifr
.ifr_name
));
1188 req
.ifm_current
& ~IFM_IEEE80211_MONITOR
;
1189 if (ioctl(sock
, SIOCSIFMEDIA
,
1192 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
1193 "Please adjust manually.\n",
1201 #endif /* HAVE_BSD_IEEE80211 */
1204 * Take this pcap out of the list of pcaps for which we
1205 * have to take the interface out of some mode.
1207 pcap_remove_from_pcaps_to_close(p
);
1208 p
->md
.must_clear
= 0;
1211 #ifdef HAVE_ZEROCOPY_BPF
1213 * In zero-copy mode, p->buffer is just a pointer into one of the two
1214 * memory-mapped buffers, so no need to free it.
1216 if (p
->md
.zerocopy
) {
1217 if (p
->md
.zbuf1
!= MAP_FAILED
&& p
->md
.zbuf1
!= NULL
)
1218 munmap(p
->md
.zbuf1
, p
->md
.zbufsize
);
1219 if (p
->md
.zbuf2
!= MAP_FAILED
&& p
->md
.zbuf2
!= NULL
)
1220 munmap(p
->md
.zbuf2
, p
->md
.zbufsize
);
1223 if (p
->md
.device
!= NULL
) {
1225 p
->md
.device
= NULL
;
1227 pcap_cleanup_live_common(p
);
1231 check_setif_failure(pcap_t
*p
, int error
)
1239 if (error
== ENXIO
) {
1241 * No such device exists.
1244 if (p
->opt
.rfmon
&& strncmp(p
->opt
.source
, "wlt", 3) == 0) {
1246 * Monitor mode was requested, and we're trying
1247 * to open a "wltN" device. Assume that this
1248 * is 10.4 and that we were asked to open an
1249 * "enN" device; if that device exists, return
1250 * "monitor mode not supported on the device".
1252 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1254 strlcpy(ifr
.ifr_name
, "en",
1255 sizeof(ifr
.ifr_name
));
1256 strlcat(ifr
.ifr_name
, p
->opt
.source
+ 3,
1257 sizeof(ifr
.ifr_name
));
1258 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
1260 * We assume this failed because
1261 * the underlying device doesn't
1264 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1265 strcpy(p
->errbuf
, "");
1268 * The underlying "enN" device
1269 * exists, but there's no
1270 * corresponding "wltN" device;
1271 * that means that the "enN"
1272 * device doesn't support
1273 * monitor mode, probably because
1274 * it's an Ethernet device rather
1275 * than a wireless device.
1277 err
= PCAP_ERROR_RFMON_NOTSUP
;
1282 * We can't find out whether there's
1283 * an underlying "enN" device, so
1284 * just report "no such device".
1286 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1287 strcpy(p
->errbuf
, "");
1295 strcpy(p
->errbuf
, "");
1296 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1297 } else if (errno
== ENETDOWN
) {
1299 * Return a "network down" indication, so that
1300 * the application can report that rather than
1301 * saying we had a mysterious failure and
1302 * suggest that they report a problem to the
1303 * libpcap developers.
1305 return (PCAP_ERROR_IFACE_NOT_UP
);
1308 * Some other error; fill in the error string, and
1309 * return PCAP_ERROR.
1311 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1312 p
->opt
.source
, pcap_strerror(errno
));
1313 return (PCAP_ERROR
);
1318 pcap_activate_bpf(pcap_t
*p
)
1323 struct bpf_version bv
;
1326 char *wltdev
= NULL
;
1329 struct bpf_dltlist bdl
;
1330 #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1333 #endif /* BIOCGDLTLIST */
1334 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1335 u_int spoof_eth_src
= 1;
1338 struct bpf_insn total_insn
;
1339 struct bpf_program total_prog
;
1340 struct utsname osinfo
;
1341 int have_osinfo
= 0;
1342 #ifdef HAVE_ZEROCOPY_BPF
1344 u_int bufmode
, zbufmax
;
1355 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
1356 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
1357 pcap_strerror(errno
));
1358 status
= PCAP_ERROR
;
1361 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
1362 bv
.bv_minor
< BPF_MINOR_VERSION
) {
1363 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1364 "kernel bpf filter out of date");
1365 status
= PCAP_ERROR
;
1369 p
->md
.device
= strdup(p
->opt
.source
);
1370 if (p
->md
.device
== NULL
) {
1371 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
1372 pcap_strerror(errno
));
1373 status
= PCAP_ERROR
;
1378 * Attempt to find out the version of the OS on which we're running.
1380 if (uname(&osinfo
) == 0)
1385 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1386 * of why we check the version number.
1391 * We assume osinfo.sysname is "Darwin", because
1392 * __APPLE__ is defined. We just check the version.
1394 if (osinfo
.release
[0] < '8' &&
1395 osinfo
.release
[1] == '.') {
1397 * 10.3 (Darwin 7.x) or earlier.
1399 status
= PCAP_ERROR_RFMON_NOTSUP
;
1402 if (osinfo
.release
[0] == '8' &&
1403 osinfo
.release
[1] == '.') {
1405 * 10.4 (Darwin 8.x). s/en/wlt/
1407 if (strncmp(p
->opt
.source
, "en", 2) != 0) {
1409 * Not an enN device; check
1410 * whether the device even exists.
1412 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1414 strlcpy(ifr
.ifr_name
,
1416 sizeof(ifr
.ifr_name
));
1417 if (ioctl(sockfd
, SIOCGIFFLAGS
,
1418 (char *)&ifr
) < 0) {
1426 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1427 strcpy(p
->errbuf
, "");
1429 status
= PCAP_ERROR_RFMON_NOTSUP
;
1433 * We can't find out whether
1434 * the device exists, so just
1435 * report "no such device".
1437 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1438 strcpy(p
->errbuf
, "");
1442 wltdev
= malloc(strlen(p
->opt
.source
) + 2);
1443 if (wltdev
== NULL
) {
1444 (void)snprintf(p
->errbuf
,
1445 PCAP_ERRBUF_SIZE
, "malloc: %s",
1446 pcap_strerror(errno
));
1447 status
= PCAP_ERROR
;
1450 strcpy(wltdev
, "wlt");
1451 strcat(wltdev
, p
->opt
.source
+ 2);
1452 free(p
->opt
.source
);
1453 p
->opt
.source
= wltdev
;
1456 * Everything else is 10.5 or later; for those,
1457 * we just open the enN device, and set the DLT.
1461 #endif /* __APPLE__ */
1462 #ifdef HAVE_ZEROCOPY_BPF
1464 * If the BPF extension to set buffer mode is present, try setting
1465 * the mode to zero-copy. If that fails, use regular buffering. If
1466 * it succeeds but other setup fails, return an error to the user.
1468 bufmode
= BPF_BUFMODE_ZBUF
;
1469 if (ioctl(fd
, BIOCSETBUFMODE
, (caddr_t
)&bufmode
) == 0) {
1471 * We have zerocopy BPF; use it.
1476 * How to pick a buffer size: first, query the maximum buffer
1477 * size supported by zero-copy. This also lets us quickly
1478 * determine whether the kernel generally supports zero-copy.
1479 * Then, if a buffer size was specified, use that, otherwise
1480 * query the default buffer size, which reflects kernel
1481 * policy for a desired default. Round to the nearest page
1484 if (ioctl(fd
, BIOCGETZMAX
, (caddr_t
)&zbufmax
) < 0) {
1485 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGETZMAX: %s",
1486 pcap_strerror(errno
));
1490 if (p
->opt
.buffer_size
!= 0) {
1492 * A buffer size was explicitly specified; use it.
1494 v
= p
->opt
.buffer_size
;
1496 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1501 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
1503 p
->md
.zbufsize
= roundup(v
, getpagesize());
1504 if (p
->md
.zbufsize
> zbufmax
)
1505 p
->md
.zbufsize
= zbufmax
;
1506 p
->md
.zbuf1
= mmap(NULL
, p
->md
.zbufsize
, PROT_READ
| PROT_WRITE
,
1508 p
->md
.zbuf2
= mmap(NULL
, p
->md
.zbufsize
, PROT_READ
| PROT_WRITE
,
1510 if (p
->md
.zbuf1
== MAP_FAILED
|| p
->md
.zbuf2
== MAP_FAILED
) {
1511 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "mmap: %s",
1512 pcap_strerror(errno
));
1515 bzero(&bz
, sizeof(bz
));
1516 bz
.bz_bufa
= p
->md
.zbuf1
;
1517 bz
.bz_bufb
= p
->md
.zbuf2
;
1518 bz
.bz_buflen
= p
->md
.zbufsize
;
1519 if (ioctl(fd
, BIOCSETZBUF
, (caddr_t
)&bz
) < 0) {
1520 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETZBUF: %s",
1521 pcap_strerror(errno
));
1524 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
, sizeof(ifr
.ifr_name
));
1525 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
1526 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1527 p
->opt
.source
, pcap_strerror(errno
));
1530 v
= p
->md
.zbufsize
- sizeof(struct bpf_zbuf_header
);
1535 * We don't have zerocopy BPF.
1536 * Set the buffer size.
1538 if (p
->opt
.buffer_size
!= 0) {
1540 * A buffer size was explicitly specified; use it.
1542 if (ioctl(fd
, BIOCSBLEN
,
1543 (caddr_t
)&p
->opt
.buffer_size
) < 0) {
1544 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1545 "BIOCSBLEN: %s: %s", p
->opt
.source
,
1546 pcap_strerror(errno
));
1547 status
= PCAP_ERROR
;
1552 * Now bind to the device.
1554 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
1555 sizeof(ifr
.ifr_name
));
1556 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
1557 status
= check_setif_failure(p
, errno
);
1562 * No buffer size was explicitly specified.
1564 * Try finding a good size for the buffer; 32768 may
1565 * be too big, so keep cutting it in half until we
1566 * find a size that works, or run out of sizes to try.
1567 * If the default is larger, don't make it smaller.
1569 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1572 for ( ; v
!= 0; v
>>= 1) {
1574 * Ignore the return value - this is because the
1575 * call fails on BPF systems that don't have
1576 * kernel malloc. And if the call fails, it's
1577 * no big deal, we just continue to use the
1578 * standard buffer size.
1580 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
1582 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
1583 sizeof(ifr
.ifr_name
));
1584 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
1585 break; /* that size worked; we're done */
1587 if (errno
!= ENOBUFS
) {
1588 status
= check_setif_failure(p
, errno
);
1594 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1595 "BIOCSBLEN: %s: No buffer size worked",
1597 status
= PCAP_ERROR
;
1603 /* Get the data link layer type. */
1604 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
1605 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
1606 pcap_strerror(errno
));
1607 status
= PCAP_ERROR
;
1613 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
1636 * We don't know what to map this to yet.
1638 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
1640 status
= PCAP_ERROR
;
1644 #if _BSDI_VERSION - 0 >= 199510
1645 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
1660 case 12: /*DLT_C_HDLC*/
1668 * We know the default link type -- now determine all the DLTs
1669 * this interface supports. If this fails with EINVAL, it's
1670 * not fatal; we just don't get to use the feature later.
1672 if (get_dlt_list(fd
, v
, &bdl
, p
->errbuf
) == -1) {
1673 status
= PCAP_ERROR
;
1676 p
->dlt_count
= bdl
.bfl_len
;
1677 p
->dlt_list
= bdl
.bfl_list
;
1681 * Monitor mode fun, continued.
1683 * For 10.5 and, we're assuming, later releases, as noted above,
1684 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
1685 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
1686 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
1689 * Therefore, if the user asked for monitor mode, we filter out
1690 * the DLT_EN10MB value, as you can't get that in monitor mode,
1691 * and, if the user didn't ask for monitor mode, we filter out
1692 * the 802.11 DLT_ values, because selecting those will turn
1693 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
1694 * radio DLT_ value is offered, we try to select that, otherwise
1695 * we try to select DLT_IEEE802_11.
1698 if (isdigit((unsigned)osinfo
.release
[0]) &&
1699 (osinfo
.release
[0] == '9' ||
1700 isdigit((unsigned)osinfo
.release
[1]))) {
1702 * 10.5 (Darwin 9.x), or later.
1704 new_dlt
= find_802_11(&bdl
);
1705 if (new_dlt
!= -1) {
1707 * We have at least one 802.11 DLT_ value,
1708 * so this is an 802.11 interface.
1709 * new_dlt is the best of the 802.11
1710 * DLT_ values in the list.
1714 * Our caller wants monitor mode.
1715 * Purge DLT_EN10MB from the list
1716 * of link-layer types, as selecting
1717 * it will keep monitor mode off.
1722 * If the new mode we want isn't
1723 * the default mode, attempt to
1724 * select the new mode.
1727 if (ioctl(p
->fd
, BIOCSDLT
,
1739 * Our caller doesn't want
1740 * monitor mode. Unless this
1741 * is being done by pcap_open_live(),
1742 * purge the 802.11 link-layer types
1743 * from the list, as selecting
1744 * one of them will turn monitor
1753 * The caller requested monitor
1754 * mode, but we have no 802.11
1755 * link-layer types, so they
1758 status
= PCAP_ERROR_RFMON_NOTSUP
;
1764 #elif defined(HAVE_BSD_IEEE80211)
1766 * *BSD with the new 802.11 ioctls.
1767 * Do we want monitor mode?
1771 * Try to put the interface into monitor mode.
1773 status
= monitor_mode(p
, 1);
1782 * We're in monitor mode.
1783 * Try to find the best 802.11 DLT_ value and, if we
1784 * succeed, try to switch to that mode if we're not
1785 * already in that mode.
1787 new_dlt
= find_802_11(&bdl
);
1788 if (new_dlt
!= -1) {
1790 * We have at least one 802.11 DLT_ value.
1791 * new_dlt is the best of the 802.11
1792 * DLT_ values in the list.
1794 * If the new mode we want isn't the default mode,
1795 * attempt to select the new mode.
1798 if (ioctl(p
->fd
, BIOCSDLT
, &new_dlt
) != -1) {
1800 * We succeeded; make this the
1808 #endif /* various platforms */
1809 #endif /* BIOCGDLTLIST */
1812 * If this is an Ethernet device, and we don't have a DLT_ list,
1813 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
1814 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
1815 * do, but there's not much we can do about that without finding
1816 * some other way of determining whether it's an Ethernet or 802.11
1819 if (v
== DLT_EN10MB
&& p
->dlt_count
== 0) {
1820 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
1822 * If that fails, just leave the list empty.
1824 if (p
->dlt_list
!= NULL
) {
1825 p
->dlt_list
[0] = DLT_EN10MB
;
1826 p
->dlt_list
[1] = DLT_DOCSIS
;
1832 p
->fddipad
= PCAP_FDDIPAD
;
1838 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1840 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
1841 * the link-layer source address isn't forcibly overwritten.
1842 * (Should we ignore errors? Should we do this only if
1843 * we're open for writing?)
1845 * XXX - I seem to remember some packet-sending bug in some
1846 * BSDs - check CVS log for "bpf.c"?
1848 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1849 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1850 "BIOCSHDRCMPLT: %s", pcap_strerror(errno
));
1851 status
= PCAP_ERROR
;
1856 #ifdef HAVE_ZEROCOPY_BPF
1857 if (p
->md
.timeout
!= 0 && !p
->md
.zerocopy
) {
1859 if (p
->md
.timeout
) {
1862 * XXX - is this seconds/nanoseconds in AIX?
1863 * (Treating it as such doesn't fix the timeout
1864 * problem described below.)
1867 to
.tv_sec
= p
->md
.timeout
/ 1000;
1868 to
.tv_usec
= (p
->md
.timeout
* 1000) % 1000000;
1869 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
1870 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT: %s",
1871 pcap_strerror(errno
));
1872 status
= PCAP_ERROR
;
1878 #ifdef BIOCIMMEDIATE
1880 * Darren Reed notes that
1882 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
1883 * timeout appears to be ignored and it waits until the buffer
1884 * is filled before returning. The result of not having it
1885 * set is almost worse than useless if your BPF filter
1886 * is reducing things to only a few packets (i.e. one every
1889 * so we turn BIOCIMMEDIATE mode on if this is AIX.
1891 * We don't turn it on for other platforms, as that means we
1892 * get woken up for every packet, which may not be what we want;
1893 * in the Winter 1993 USENIX paper on BPF, they say:
1895 * Since a process might want to look at every packet on a
1896 * network and the time between packets can be only a few
1897 * microseconds, it is not possible to do a read system call
1898 * per packet and BPF must collect the data from several
1899 * packets and return it as a unit when the monitoring
1900 * application does a read.
1902 * which I infer is the reason for the timeout - it means we
1903 * wait that amount of time, in the hopes that more packets
1904 * will arrive and we'll get them all with one read.
1906 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
1907 * BSDs) causes the timeout to be ignored.
1909 * On the other hand, some platforms (e.g., Linux) don't support
1910 * timeouts, they just hand stuff to you as soon as it arrives;
1911 * if that doesn't cause a problem on those platforms, it may
1912 * be OK to have BIOCIMMEDIATE mode on BSD as well.
1914 * (Note, though, that applications may depend on the read
1915 * completing, even if no packets have arrived, when the timeout
1916 * expires, e.g. GUI applications that have to check for input
1917 * while waiting for packets to arrive; a non-zero timeout
1918 * prevents "select()" from working right on FreeBSD and
1919 * possibly other BSDs, as the timer doesn't start until a
1920 * "read()" is done, so the timer isn't in effect if the
1921 * application is blocked on a "select()", and the "select()"
1922 * doesn't get woken up for a BPF device until the buffer
1926 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
1927 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCIMMEDIATE: %s",
1928 pcap_strerror(errno
));
1929 status
= PCAP_ERROR
;
1932 #endif /* BIOCIMMEDIATE */
1935 if (p
->opt
.promisc
) {
1936 /* set promiscuous mode, just warn if it fails */
1937 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
1938 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
1939 pcap_strerror(errno
));
1940 status
= PCAP_WARNING_PROMISC_NOTSUP
;
1944 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
1945 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
1946 pcap_strerror(errno
));
1947 status
= PCAP_ERROR
;
1951 #ifdef HAVE_ZEROCOPY_BPF
1952 if (!p
->md
.zerocopy
) {
1954 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
1955 if (p
->buffer
== NULL
) {
1956 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
1957 pcap_strerror(errno
));
1958 status
= PCAP_ERROR
;
1962 /* For some strange reason this seems to prevent the EFAULT
1963 * problems we have experienced from AIX BPF. */
1964 memset(p
->buffer
, 0x0, p
->bufsize
);
1966 #ifdef HAVE_ZEROCOPY_BPF
1971 * If there's no filter program installed, there's
1972 * no indication to the kernel of what the snapshot
1973 * length should be, so no snapshotting is done.
1975 * Therefore, when we open the device, we install
1976 * an "accept everything" filter with the specified
1979 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
1982 total_insn
.k
= p
->snapshot
;
1984 total_prog
.bf_len
= 1;
1985 total_prog
.bf_insns
= &total_insn
;
1986 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
1987 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
1988 pcap_strerror(errno
));
1989 status
= PCAP_ERROR
;
1994 * On most BPF platforms, either you can do a "select()" or
1995 * "poll()" on a BPF file descriptor and it works correctly,
1996 * or you can do it and it will return "readable" if the
1997 * hold buffer is full but not if the timeout expires *and*
1998 * a non-blocking read will, if the hold buffer is empty
1999 * but the store buffer isn't empty, rotate the buffers
2000 * and return what packets are available.
2002 * In the latter case, the fact that a non-blocking read
2003 * will give you the available packets means you can work
2004 * around the failure of "select()" and "poll()" to wake up
2005 * and return "readable" when the timeout expires by using
2006 * the timeout as the "select()" or "poll()" timeout, putting
2007 * the BPF descriptor into non-blocking mode, and read from
2008 * it regardless of whether "select()" reports it as readable
2011 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
2012 * won't wake up and return "readable" if the timer expires
2013 * and non-blocking reads return EWOULDBLOCK if the hold
2014 * buffer is empty, even if the store buffer is non-empty.
2016 * This means the workaround in question won't work.
2018 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2019 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2020 * here". On all other BPF platforms, we set it to the FD for
2021 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2022 * read will, if the hold buffer is empty and the store buffer
2023 * isn't empty, rotate the buffers and return what packets are
2024 * there (and in sufficiently recent versions of OpenBSD
2025 * "select()" and "poll()" should work correctly).
2027 * XXX - what about AIX?
2029 p
->selectable_fd
= p
->fd
; /* assume select() works until we know otherwise */
2032 * We can check what OS this is.
2034 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0) {
2035 if (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
2036 strncmp(osinfo
.release
, "4.4-", 4) == 0)
2037 p
->selectable_fd
= -1;
2041 p
->read_op
= pcap_read_bpf
;
2042 p
->inject_op
= pcap_inject_bpf
;
2043 p
->setfilter_op
= pcap_setfilter_bpf
;
2044 p
->setdirection_op
= pcap_setdirection_bpf
;
2045 p
->set_datalink_op
= pcap_set_datalink_bpf
;
2046 p
->getnonblock_op
= pcap_getnonblock_fd
;
2047 p
->setnonblock_op
= pcap_setnonblock_fd
;
2048 p
->stats_op
= pcap_stats_bpf
;
2049 p
->cleanup_op
= pcap_cleanup_bpf
;
2053 pcap_cleanup_bpf(p
);
2058 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
2061 if (dag_platform_finddevs(alldevsp
, errbuf
) < 0)
2063 #endif /* HAVE_DAG_API */
2068 #ifdef HAVE_BSD_IEEE80211
2070 monitor_mode(pcap_t
*p
, int set
)
2073 struct ifmediareq req
;
2079 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
2081 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "can't open socket: %s",
2082 pcap_strerror(errno
));
2083 return (PCAP_ERROR
);
2086 memset(&req
, 0, sizeof req
);
2087 strncpy(req
.ifm_name
, p
->opt
.source
, sizeof req
.ifm_name
);
2090 * Find out how many media types we have.
2092 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2094 * Can't get the media types.
2096 if (errno
== EINVAL
) {
2098 * Interface doesn't support SIOC{G,S}IFMEDIA.
2101 return (PCAP_ERROR_RFMON_NOTSUP
);
2103 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCGIFMEDIA 1: %s",
2104 pcap_strerror(errno
));
2106 return (PCAP_ERROR
);
2108 if (req
.ifm_count
== 0) {
2113 return (PCAP_ERROR_RFMON_NOTSUP
);
2117 * Allocate a buffer to hold all the media types, and
2118 * get the media types.
2120 media_list
= malloc(req
.ifm_count
* sizeof(int));
2121 if (media_list
== NULL
) {
2122 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
2123 pcap_strerror(errno
));
2125 return (PCAP_ERROR
);
2127 req
.ifm_ulist
= media_list
;
2128 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2129 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCGIFMEDIA: %s",
2130 pcap_strerror(errno
));
2133 return (PCAP_ERROR
);
2137 * Look for an 802.11 "automatic" media type.
2138 * We assume that all 802.11 adapters have that media type,
2139 * and that it will carry the monitor mode supported flag.
2142 for (i
= 0; i
< req
.ifm_count
; i
++) {
2143 if (IFM_TYPE(media_list
[i
]) == IFM_IEEE80211
2144 && IFM_SUBTYPE(media_list
[i
]) == IFM_AUTO
) {
2145 /* OK, does it do monitor mode? */
2146 if (media_list
[i
] & IFM_IEEE80211_MONITOR
) {
2155 * This adapter doesn't support monitor mode.
2158 return (PCAP_ERROR_RFMON_NOTSUP
);
2163 * Don't just check whether we can enable monitor mode,
2164 * do so, if it's not already enabled.
2166 if ((req
.ifm_current
& IFM_IEEE80211_MONITOR
) == 0) {
2168 * Monitor mode isn't currently on, so turn it on,
2169 * and remember that we should turn it off when the
2174 * If we haven't already done so, arrange to have
2175 * "pcap_close_all()" called when we exit.
2177 if (!pcap_do_addexit(p
)) {
2179 * "atexit()" failed; don't put the interface
2180 * in monitor mode, just give up.
2182 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2185 return (PCAP_ERROR
);
2187 memset(&ifr
, 0, sizeof(ifr
));
2188 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
,
2189 sizeof(ifr
.ifr_name
));
2190 ifr
.ifr_media
= req
.ifm_current
| IFM_IEEE80211_MONITOR
;
2191 if (ioctl(sock
, SIOCSIFMEDIA
, &ifr
) == -1) {
2192 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2193 "SIOCSIFMEDIA: %s", pcap_strerror(errno
));
2195 return (PCAP_ERROR
);
2198 p
->md
.must_clear
|= MUST_CLEAR_RFMON
;
2201 * Add this to the list of pcaps to close when we exit.
2203 pcap_add_to_pcaps_to_close(p
);
2208 #endif /* HAVE_BSD_IEEE80211 */
2210 #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
2212 * Check whether we have any 802.11 link-layer types; return the best
2213 * of the 802.11 link-layer types if we find one, and return -1
2216 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
2217 * best 802.11 link-layer type; any of the other 802.11-plus-radio
2218 * headers are second-best; 802.11 with no radio information is
2222 find_802_11(struct bpf_dltlist
*bdlp
)
2228 * Scan the list of DLT_ values, looking for 802.11 values,
2229 * and, if we find any, choose the best of them.
2232 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
2233 switch (bdlp
->bfl_list
[i
]) {
2235 case DLT_IEEE802_11
:
2237 * 802.11, but no radio.
2239 * Offer this, and select it as the new mode
2240 * unless we've already found an 802.11
2241 * header with radio information.
2244 new_dlt
= bdlp
->bfl_list
[i
];
2247 case DLT_PRISM_HEADER
:
2248 case DLT_AIRONET_HEADER
:
2249 case DLT_IEEE802_11_RADIO_AVS
:
2251 * 802.11 with radio, but not radiotap.
2253 * Offer this, and select it as the new mode
2254 * unless we've already found the radiotap DLT_.
2256 if (new_dlt
!= DLT_IEEE802_11_RADIO
)
2257 new_dlt
= bdlp
->bfl_list
[i
];
2260 case DLT_IEEE802_11_RADIO
:
2262 * 802.11 with radiotap.
2264 * Offer this, and select it as the new mode.
2266 new_dlt
= bdlp
->bfl_list
[i
];
2279 #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
2281 #if defined(__APPLE__) && defined(BIOCGDLTLIST)
2283 * Remove DLT_EN10MB from the list of DLT_ values.
2286 remove_en(pcap_t
*p
)
2291 * Scan the list of DLT_ values and discard DLT_EN10MB.
2294 for (i
= 0; i
< p
->dlt_count
; i
++) {
2295 switch (p
->dlt_list
[i
]) {
2299 * Don't offer this one.
2305 * Just copy this mode over.
2311 * Copy this DLT_ value to its new position.
2313 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2318 * Set the DLT_ count to the number of entries we copied.
2324 * Remove DLT_EN10MB from the list of DLT_ values, and look for the
2325 * best 802.11 link-layer type in that list and return it.
2326 * Radiotap is better than anything else; 802.11 with any other radio
2327 * header is better than 802.11 with no radio header.
2330 remove_802_11(pcap_t
*p
)
2335 * Scan the list of DLT_ values and discard 802.11 values.
2338 for (i
= 0; i
< p
->dlt_count
; i
++) {
2339 switch (p
->dlt_list
[i
]) {
2341 case DLT_IEEE802_11
:
2342 case DLT_PRISM_HEADER
:
2343 case DLT_AIRONET_HEADER
:
2344 case DLT_IEEE802_11_RADIO
:
2345 case DLT_IEEE802_11_RADIO_AVS
:
2347 * 802.11. Don't offer this one.
2353 * Just copy this mode over.
2359 * Copy this DLT_ value to its new position.
2361 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2366 * Set the DLT_ count to the number of entries we copied.
2370 #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
2373 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
2376 * Free any user-mode filter we might happen to have installed.
2378 pcap_freecode(&p
->fcode
);
2381 * Try to install the kernel filter.
2383 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) == 0) {
2387 p
->md
.use_bpf
= 1; /* filtering in the kernel */
2390 * Discard any previously-received packets, as they might
2391 * have passed whatever filter was formerly in effect, but
2392 * might not pass this filter (BIOCSETF discards packets
2393 * buffered in the kernel, so you can lose packets in any
2403 * If it failed with EINVAL, that's probably because the program
2404 * is invalid or too big. Validate it ourselves; if we like it
2405 * (we currently allow backward branches, to support protochain),
2406 * run it in userland. (There's no notion of "too big" for
2409 * Otherwise, just give up.
2410 * XXX - if the copy of the program into the kernel failed,
2411 * we will get EINVAL rather than, say, EFAULT on at least
2414 if (errno
!= EINVAL
) {
2415 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2416 pcap_strerror(errno
));
2421 * install_bpf_program() validates the program.
2423 * XXX - what if we already have a filter in the kernel?
2425 if (install_bpf_program(p
, fp
) < 0)
2427 p
->md
.use_bpf
= 0; /* filtering in userland */
2432 * Set direction flag: Which packets do we accept on a forwarding
2433 * single device? IN, OUT or both?
2436 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
2438 #if defined(BIOCSDIRECTION)
2441 direction
= (d
== PCAP_D_IN
) ? BPF_D_IN
:
2442 ((d
== PCAP_D_OUT
) ? BPF_D_OUT
: BPF_D_INOUT
);
2443 if (ioctl(p
->fd
, BIOCSDIRECTION
, &direction
) == -1) {
2444 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2445 "Cannot set direction to %s: %s",
2446 (d
== PCAP_D_IN
) ? "PCAP_D_IN" :
2447 ((d
== PCAP_D_OUT
) ? "PCAP_D_OUT" : "PCAP_D_INOUT"),
2452 #elif defined(BIOCSSEESENT)
2456 * We don't support PCAP_D_OUT.
2458 if (d
== PCAP_D_OUT
) {
2459 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2460 "Setting direction to PCAP_D_OUT is not supported on BPF");
2464 seesent
= (d
== PCAP_D_INOUT
);
2465 if (ioctl(p
->fd
, BIOCSSEESENT
, &seesent
) == -1) {
2466 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2467 "Cannot set direction to %s: %s",
2468 (d
== PCAP_D_INOUT
) ? "PCAP_D_INOUT" : "PCAP_D_IN",
2474 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2475 "This system doesn't support BIOCSSEESENT, so the direction can't be set");
2481 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
2484 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
2485 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2486 "Cannot set DLT %d: %s", dlt
, strerror(errno
));