2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2010 CACE Technologies, Davis (California)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include "pcap-airpcap.h"
44 /* Default size of the buffer we allocate in userland. */
45 #define AIRPCAP_DEFAULT_USER_BUFFER_SIZE 256000
47 /* Default size of the buffer for the AirPcap adapter. */
48 #define AIRPCAP_DEFAULT_KERNEL_BUFFER_SIZE 1000000
51 // We load the AirPcap DLL dynamically, so that the code will
52 // work whether you have it installed or not, and there don't
53 // have to be two different versions of the library, one linked
54 // to the AirPcap library and one not linked to it.
56 static pcap_code_handle_t airpcap_lib
;
58 typedef PCHAR (*AirpcapGetLastErrorHandler
)(PAirpcapHandle
);
59 typedef BOOL (*AirpcapGetDeviceListHandler
)(PAirpcapDeviceDescription
*, PCHAR
);
60 typedef VOID (*AirpcapFreeDeviceListHandler
)(PAirpcapDeviceDescription
);
61 typedef PAirpcapHandle (*AirpcapOpenHandler
)(PCHAR
, PCHAR
);
62 typedef VOID (*AirpcapCloseHandler
)(PAirpcapHandle
);
63 typedef BOOL (*AirpcapSetDeviceMacFlagsHandler
)(PAirpcapHandle
, UINT
);
64 typedef BOOL (*AirpcapSetLinkTypeHandler
)(PAirpcapHandle
, AirpcapLinkType
);
65 typedef BOOL (*AirpcapGetLinkTypeHandler
)(PAirpcapHandle
, PAirpcapLinkType
);
66 typedef BOOL (*AirpcapSetKernelBufferHandler
)(PAirpcapHandle
, UINT
);
67 typedef BOOL (*AirpcapSetFilterHandler
)(PAirpcapHandle
, PVOID
, UINT
);
68 typedef BOOL (*AirpcapSetMinToCopyHandler
)(PAirpcapHandle
, UINT
);
69 typedef BOOL (*AirpcapGetReadEventHandler
)(PAirpcapHandle
, HANDLE
*);
70 typedef BOOL (*AirpcapReadHandler
)(PAirpcapHandle
, PBYTE
, UINT
, PUINT
);
71 typedef BOOL (*AirpcapWriteHandler
)(PAirpcapHandle
, PCHAR
, ULONG
);
72 typedef BOOL (*AirpcapGetStatsHandler
)(PAirpcapHandle
, PAirpcapStats
);
74 static AirpcapGetLastErrorHandler p_AirpcapGetLastError
;
75 static AirpcapGetDeviceListHandler p_AirpcapGetDeviceList
;
76 static AirpcapFreeDeviceListHandler p_AirpcapFreeDeviceList
;
77 static AirpcapOpenHandler p_AirpcapOpen
;
78 static AirpcapCloseHandler p_AirpcapClose
;
79 static AirpcapSetDeviceMacFlagsHandler p_AirpcapSetDeviceMacFlags
;
80 static AirpcapSetLinkTypeHandler p_AirpcapSetLinkType
;
81 static AirpcapGetLinkTypeHandler p_AirpcapGetLinkType
;
82 static AirpcapSetKernelBufferHandler p_AirpcapSetKernelBuffer
;
83 static AirpcapSetFilterHandler p_AirpcapSetFilter
;
84 static AirpcapSetMinToCopyHandler p_AirpcapSetMinToCopy
;
85 static AirpcapGetReadEventHandler p_AirpcapGetReadEvent
;
86 static AirpcapReadHandler p_AirpcapRead
;
87 static AirpcapWriteHandler p_AirpcapWrite
;
88 static AirpcapGetStatsHandler p_AirpcapGetStats
;
92 AIRPCAP_API_UNLOADED
= 0,
94 AIRPCAP_API_CANNOT_LOAD
,
96 } AIRPCAP_API_LOAD_STATUS
;
98 static AIRPCAP_API_LOAD_STATUS airpcap_load_status
;
101 * NOTE: this function should be called by the pcap functions that can
102 * theoretically deal with the AirPcap library for the first time,
103 * namely listing the adapters and creating a pcap_t for an adapter.
104 * All the other ones (activate, close, read, write, set parameters)
105 * work on a pcap_t for an AirPcap device, meaning we've already
106 * created the pcap_t and thus have loaded the functions, so we do
107 * not need to call this function.
109 static AIRPCAP_API_LOAD_STATUS
110 load_airpcap_functions(void)
112 AIRPCAP_API_LOAD_STATUS current_status
;
115 * We don't use a mutex because there's no place that
116 * we can guarantee we'll be called before any threads
117 * other than the main thread exists. (For example,
118 * this might be a static library, so we can't arrange
119 * to be called by DllMain(), and there's no guarantee
120 * that the application called pcap_init() - which is
121 * supposed to be called only from one thread - so
122 * we can't arrange to be called from it.)
124 * If nobody's tried to load it yet, mark it as
125 * loading; in any case, return the status before
128 current_status
= InterlockedCompareExchange((LONG
*)&airpcap_load_status
,
129 AIRPCAP_API_LOADING
, AIRPCAP_API_UNLOADED
);
132 * If the status was AIRPCAP_API_UNLOADED, we've set it
133 * to AIRPCAP_API_LOADING, because we're going to be
134 * the ones to load the library but current_status is
135 * AIRPCAP_API_UNLOADED.
137 * if it was AIRPCAP_API_LOADING, meaning somebody else
138 * was trying to load it, spin until they finish and
139 * set the status to a value reflecting whether they
142 while (current_status
== AIRPCAP_API_LOADING
) {
143 current_status
= InterlockedCompareExchange((LONG
*)&airpcap_load_status
,
144 AIRPCAP_API_LOADING
, AIRPCAP_API_LOADING
);
149 * At this point, current_status is either:
151 * AIRPCAP_API_LOADED, in which case another thread
152 * loaded the library, so we're done;
154 * AIRPCAP_API_CANNOT_LOAD, in which another thread
155 * tried and failed to load the library, so we're
156 * done - we won't try it ourselves;
158 * AIRPCAP_API_LOADING, in which case *we're* the
159 * ones loading it, and should now try to do so.
161 if (current_status
== AIRPCAP_API_LOADED
)
162 return AIRPCAP_API_LOADED
;
164 if (current_status
== AIRPCAP_API_CANNOT_LOAD
)
165 return AIRPCAP_API_CANNOT_LOAD
;
168 * Start out assuming we can't load it.
170 current_status
= AIRPCAP_API_CANNOT_LOAD
;
172 airpcap_lib
= pcap_load_code("airpcap.dll");
173 if (airpcap_lib
!= NULL
) {
175 * OK, we've loaded the library; now try to find the
176 * functions we need in it.
178 p_AirpcapGetLastError
= (AirpcapGetLastErrorHandler
) pcap_find_function(airpcap_lib
, "AirpcapGetLastError");
179 p_AirpcapGetDeviceList
= (AirpcapGetDeviceListHandler
) pcap_find_function(airpcap_lib
, "AirpcapGetDeviceList");
180 p_AirpcapFreeDeviceList
= (AirpcapFreeDeviceListHandler
) pcap_find_function(airpcap_lib
, "AirpcapFreeDeviceList");
181 p_AirpcapOpen
= (AirpcapOpenHandler
) pcap_find_function(airpcap_lib
, "AirpcapOpen");
182 p_AirpcapClose
= (AirpcapCloseHandler
) pcap_find_function(airpcap_lib
, "AirpcapClose");
183 p_AirpcapSetDeviceMacFlags
= (AirpcapSetDeviceMacFlagsHandler
) pcap_find_function(airpcap_lib
, "AirpcapSetDeviceMacFlags");
184 p_AirpcapSetLinkType
= (AirpcapSetLinkTypeHandler
) pcap_find_function(airpcap_lib
, "AirpcapSetLinkType");
185 p_AirpcapGetLinkType
= (AirpcapGetLinkTypeHandler
) pcap_find_function(airpcap_lib
, "AirpcapGetLinkType");
186 p_AirpcapSetKernelBuffer
= (AirpcapSetKernelBufferHandler
) pcap_find_function(airpcap_lib
, "AirpcapSetKernelBuffer");
187 p_AirpcapSetFilter
= (AirpcapSetFilterHandler
) pcap_find_function(airpcap_lib
, "AirpcapSetFilter");
188 p_AirpcapSetMinToCopy
= (AirpcapSetMinToCopyHandler
) pcap_find_function(airpcap_lib
, "AirpcapSetMinToCopy");
189 p_AirpcapGetReadEvent
= (AirpcapGetReadEventHandler
) pcap_find_function(airpcap_lib
, "AirpcapGetReadEvent");
190 p_AirpcapRead
= (AirpcapReadHandler
) pcap_find_function(airpcap_lib
, "AirpcapRead");
191 p_AirpcapWrite
= (AirpcapWriteHandler
) pcap_find_function(airpcap_lib
, "AirpcapWrite");
192 p_AirpcapGetStats
= (AirpcapGetStatsHandler
) pcap_find_function(airpcap_lib
, "AirpcapGetStats");
195 // Make sure that we found everything
197 if (p_AirpcapGetLastError
!= NULL
&&
198 p_AirpcapGetDeviceList
!= NULL
&&
199 p_AirpcapFreeDeviceList
!= NULL
&&
200 p_AirpcapOpen
!= NULL
&&
201 p_AirpcapClose
!= NULL
&&
202 p_AirpcapSetDeviceMacFlags
!= NULL
&&
203 p_AirpcapSetLinkType
!= NULL
&&
204 p_AirpcapGetLinkType
!= NULL
&&
205 p_AirpcapSetKernelBuffer
!= NULL
&&
206 p_AirpcapSetFilter
!= NULL
&&
207 p_AirpcapSetMinToCopy
!= NULL
&&
208 p_AirpcapGetReadEvent
!= NULL
&&
209 p_AirpcapRead
!= NULL
&&
210 p_AirpcapWrite
!= NULL
&&
211 p_AirpcapGetStats
!= NULL
) {
213 * We have all we need.
215 current_status
= AIRPCAP_API_LOADED
;
219 if (current_status
!= AIRPCAP_API_LOADED
) {
221 * We failed; if we found the DLL, close the
224 if (airpcap_lib
!= NULL
) {
225 FreeLibrary(airpcap_lib
);
231 * Now set the status appropriately - and atomically.
233 InterlockedExchange((LONG
*)&airpcap_load_status
, current_status
);
235 return current_status
;
239 * Private data for capturing on AirPcap devices.
241 struct pcap_airpcap
{
242 PAirpcapHandle adapter
;
243 int filtering_in_kernel
;
247 struct pcap_stat stat
;
251 airpcap_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
253 struct pcap_airpcap
*pa
= p
->priv
;
255 if (!p_AirpcapSetFilter(pa
->adapter
, fp
->bf_insns
,
256 fp
->bf_len
* sizeof(struct bpf_insn
))) {
258 * Kernel filter not installed.
260 * XXX - we don't know whether this failed because:
262 * the kernel rejected the filter program as invalid,
263 * in which case we should fall back on userland
266 * the kernel rejected the filter program as too big,
267 * in which case we should again fall back on
268 * userland filtering;
270 * there was some other problem, in which case we
271 * should probably report an error;
273 * So we just fall back on userland filtering in
278 * install_bpf_program() validates the program.
280 * XXX - what if we already have a filter in the kernel?
282 if (install_bpf_program(p
, fp
) < 0)
284 pa
->filtering_in_kernel
= 0; /* filtering in userland */
291 pa
->filtering_in_kernel
= 1; /* filtering in the kernel */
294 * Discard any previously-received packets, as they might have
295 * passed whatever filter was formerly in effect, but might
296 * not pass this filter (BIOCSETF discards packets buffered
297 * in the kernel, so you can lose packets in any case).
304 airpcap_set_datalink(pcap_t
*p
, int dlt
)
306 struct pcap_airpcap
*pa
= p
->priv
;
307 AirpcapLinkType type
;
311 case DLT_IEEE802_11_RADIO
:
312 type
= AIRPCAP_LT_802_11_PLUS_RADIO
;
316 type
= AIRPCAP_LT_802_11_PLUS_PPI
;
320 type
= AIRPCAP_LT_802_11
;
324 /* This can't happen; just return. */
327 if (!p_AirpcapSetLinkType(pa
->adapter
, type
)) {
328 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
329 "AirpcapSetLinkType() failed: %s",
330 p_AirpcapGetLastError(pa
->adapter
));
338 airpcap_getnonblock(pcap_t
*p
)
340 struct pcap_airpcap
*pa
= p
->priv
;
342 return (pa
->nonblock
);
346 airpcap_setnonblock(pcap_t
*p
, int nonblock
)
348 struct pcap_airpcap
*pa
= p
->priv
;
353 * Set the packet buffer timeout to -1 for non-blocking
359 * Restore the timeout set when the device was opened.
360 * (Note that this may be -1, in which case we're not
361 * really leaving non-blocking mode. However, although
362 * the timeout argument to pcap_set_timeout() and
363 * pcap_open_live() is an int, you're not supposed to
364 * supply a negative value, so that "shouldn't happen".)
366 newtimeout
= p
->opt
.timeout
;
368 pa
->read_timeout
= newtimeout
;
369 pa
->nonblock
= (newtimeout
== -1);
374 airpcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
376 struct pcap_airpcap
*pa
= p
->priv
;
380 * Try to get statistics.
382 if (!p_AirpcapGetStats(pa
->adapter
, &tas
)) {
383 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
384 "AirpcapGetStats() failed: %s",
385 p_AirpcapGetLastError(pa
->adapter
));
389 ps
->ps_drop
= tas
.Drops
;
390 ps
->ps_recv
= tas
.Recvs
;
391 ps
->ps_ifdrop
= tas
.IfDrops
;
397 * Win32-only routine for getting statistics.
399 * This way is definitely safer than passing the pcap_stat * from the userland.
400 * In fact, there could happen than the user allocates a variable which is not
401 * big enough for the new structure, and the library will write in a zone
402 * which is not allocated to this variable.
404 * In this way, we're pretty sure we are writing on memory allocated to this
407 * XXX - but this is the wrong way to handle statistics. Instead, we should
408 * have an API that returns data in a form like the Options section of a
409 * pcapng Interface Statistics Block:
411 * https://xml2rfc.tools.ietf.org/cgi-bin/xml2rfc.cgi?url=https://raw.githubusercontent.com/pcapng/pcapng/master/draft-tuexen-opsawg-pcapng.xml&modeAsFormat=html/ascii&type=ascii#rfc.section.4.6
413 * which would let us add new statistics straightforwardly and indicate which
414 * statistics we are and are *not* providing, rather than having to provide
415 * possibly-bogus values for statistics we can't provide.
417 static struct pcap_stat
*
418 airpcap_stats_ex(pcap_t
*p
, int *pcap_stat_size
)
420 struct pcap_airpcap
*pa
= p
->priv
;
423 *pcap_stat_size
= sizeof (p
->stat
);
426 * Try to get statistics.
428 if (!p_AirpcapGetStats(pa
->adapter
, &tas
)) {
429 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
430 "AirpcapGetStats() failed: %s",
431 p_AirpcapGetLastError(pa
->adapter
));
435 p
->stat
.ps_recv
= tas
.Recvs
;
436 p
->stat
.ps_drop
= tas
.Drops
;
437 p
->stat
.ps_ifdrop
= tas
.IfDrops
;
439 * Just in case this is ever compiled for a target other than
440 * Windows, which is extremely unlikely at best.
443 p
->stat
.ps_capt
= tas
.Capt
;
448 /* Set the dimension of the kernel-level capture buffer */
450 airpcap_setbuff(pcap_t
*p
, int dim
)
452 struct pcap_airpcap
*pa
= p
->priv
;
454 if (!p_AirpcapSetKernelBuffer(pa
->adapter
, dim
)) {
455 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
456 "AirpcapSetKernelBuffer() failed: %s",
457 p_AirpcapGetLastError(pa
->adapter
));
463 /* Set the driver working mode */
465 airpcap_setmode(pcap_t
*p
, int mode
)
467 if (mode
!= MODE_CAPT
) {
468 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
469 "Only MODE_CAPT is supported on an AirPcap adapter");
475 /*set the minimum amount of data that will release a read call*/
477 airpcap_setmintocopy(pcap_t
*p
, int size
)
479 struct pcap_airpcap
*pa
= p
->priv
;
481 if (!p_AirpcapSetMinToCopy(pa
->adapter
, size
)) {
482 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
483 "AirpcapSetMinToCopy() failed: %s",
484 p_AirpcapGetLastError(pa
->adapter
));
491 airpcap_getevent(pcap_t
*p
)
493 struct pcap_airpcap
*pa
= p
->priv
;
495 return (pa
->read_event
);
499 airpcap_oid_get_request(pcap_t
*p
, bpf_u_int32 oid _U_
, void *data _U_
,
502 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
503 "Getting OID values is not supported on an AirPcap adapter");
508 airpcap_oid_set_request(pcap_t
*p
, bpf_u_int32 oid _U_
, const void *data _U_
,
511 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
512 "Setting OID values is not supported on an AirPcap adapter");
517 airpcap_sendqueue_transmit(pcap_t
*p
, pcap_send_queue
*queue _U_
, int sync _U_
)
519 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
520 "Cannot queue packets for transmission on an AirPcap adapter");
525 airpcap_setuserbuffer(pcap_t
*p
, int size
)
527 unsigned char *new_buff
;
530 /* Bogus parameter */
531 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
532 "Error: invalid size %d",size
);
536 /* Allocate the buffer */
537 new_buff
= (unsigned char *)malloc(sizeof(char)*size
);
540 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
541 "Error: not enough memory");
547 p
->buffer
= new_buff
;
554 airpcap_live_dump(pcap_t
*p
, char *filename _U_
, int maxsize _U_
,
557 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
558 "AirPcap adapters don't support live dump");
563 airpcap_live_dump_ended(pcap_t
*p
, int sync _U_
)
565 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
566 "AirPcap adapters don't support live dump");
570 static PAirpcapHandle
571 airpcap_get_airpcap_handle(pcap_t
*p
)
573 struct pcap_airpcap
*pa
= p
->priv
;
575 return (pa
->adapter
);
579 airpcap_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
581 struct pcap_airpcap
*pa
= p
->priv
;
584 register u_char
*bp
, *ep
;
591 * Has "pcap_breakloop()" been called?
595 * Yes - clear the flag that indicates that it
596 * has, and return PCAP_ERROR_BREAK to indicate
597 * that we were told to break out of the loop.
600 return (PCAP_ERROR_BREAK
);
604 // If we're not in non-blocking mode, wait for data to
607 if (pa
->read_timeout
!= -1) {
608 WaitForSingleObject(pa
->read_event
,
609 (pa
->read_timeout
==0 )? INFINITE
: pa
->read_timeout
);
614 // p_AirpcapRead doesn't block.
616 if (!p_AirpcapRead(pa
->adapter
, (PBYTE
)p
->buffer
,
617 p
->bufsize
, &bytes_read
)) {
618 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
619 "AirpcapRead() failed: %s",
620 p_AirpcapGetLastError(pa
->adapter
));
629 * Loop through each packet.
631 * This assumes that a single buffer of packets will have
632 * <= INT_MAX packets, so the packet count doesn't overflow.
634 #define bhp ((AirpcapBpfHeader *)bp)
638 register u_int caplen
, hdrlen
;
641 * Has "pcap_breakloop()" been called?
642 * If so, return immediately - if we haven't read any
643 * packets, clear the flag and return PCAP_ERROR_BREAK
644 * to indicate that we were told to break out of the loop,
645 * otherwise leave the flag set, so that the *next* call
646 * will break out of the loop without having read any
647 * packets, and return the number of packets we've
653 return (PCAP_ERROR_BREAK
);
656 p
->cc
= (int) (ep
- bp
);
663 caplen
= bhp
->Caplen
;
664 hdrlen
= bhp
->Hdrlen
;
667 * Short-circuit evaluation: if using BPF filter
668 * in the AirPcap adapter, no need to do it now -
669 * we already know the packet passed the filter.
671 if (pa
->filtering_in_kernel
||
672 p
->fcode
.bf_insns
== NULL
||
673 pcap_filter(p
->fcode
.bf_insns
, datap
, bhp
->Originallen
, caplen
)) {
674 struct pcap_pkthdr pkthdr
;
676 pkthdr
.ts
.tv_sec
= bhp
->TsSec
;
677 pkthdr
.ts
.tv_usec
= bhp
->TsUsec
;
678 pkthdr
.caplen
= caplen
;
679 pkthdr
.len
= bhp
->Originallen
;
680 (*callback
)(user
, &pkthdr
, datap
);
681 bp
+= AIRPCAP_WORDALIGN(caplen
+ hdrlen
);
682 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
684 p
->cc
= (int)(ep
- bp
);
691 bp
+= AIRPCAP_WORDALIGN(caplen
+ hdrlen
);
700 airpcap_inject(pcap_t
*p
, const void *buf
, int size
)
702 struct pcap_airpcap
*pa
= p
->priv
;
705 * XXX - the second argument to AirpcapWrite() *should* have
706 * been declared as a const pointer - a write function that
707 * stomps on what it writes is *extremely* rude - but such
708 * is life. We assume it is, in fact, not going to write on
711 if (!p_AirpcapWrite(pa
->adapter
, (void *)buf
, size
)) {
712 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
713 "AirpcapWrite() failed: %s",
714 p_AirpcapGetLastError(pa
->adapter
));
719 * We assume it all got sent if "AirpcapWrite()" succeeded.
720 * "pcap_inject()" is expected to return the number of bytes
727 airpcap_cleanup(pcap_t
*p
)
729 struct pcap_airpcap
*pa
= p
->priv
;
731 if (pa
->adapter
!= NULL
) {
732 p_AirpcapClose(pa
->adapter
);
735 pcap_cleanup_live_common(p
);
739 airpcap_breakloop(pcap_t
*p
)
743 pcap_breakloop_common(p
);
744 struct pcap_airpcap
*pa
= p
->priv
;
746 /* XXX - what if either of these fail? */
748 * XXX - will SetEvent() force a wakeup and, if so, will
749 * the AirPcap read code handle that sanely?
751 if (!p_AirpcapGetReadEvent(pa
->adapter
, &read_event
))
753 SetEvent(read_event
);
757 airpcap_activate(pcap_t
*p
)
759 struct pcap_airpcap
*pa
= p
->priv
;
760 char *device
= p
->opt
.device
;
761 char airpcap_errbuf
[AIRPCAP_ERRBUF_SIZE
];
763 AirpcapLinkType link_type
;
765 pa
->adapter
= p_AirpcapOpen(device
, airpcap_errbuf
);
766 if (pa
->adapter
== NULL
) {
767 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "%s", airpcap_errbuf
);
772 * Set monitor mode appropriately.
773 * Always turn off the "ACK frames sent to the card" mode.
776 status
= p_AirpcapSetDeviceMacFlags(pa
->adapter
,
777 AIRPCAP_MF_MONITOR_MODE_ON
);
779 status
= p_AirpcapSetDeviceMacFlags(pa
->adapter
,
780 AIRPCAP_MF_ACK_FRAMES_ON
);
782 p_AirpcapClose(pa
->adapter
);
783 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
784 "AirpcapSetDeviceMacFlags() failed: %s",
785 p_AirpcapGetLastError(pa
->adapter
));
790 * Turn a negative snapshot value (invalid), a snapshot value of
791 * 0 (unspecified), or a value bigger than the normal maximum
792 * value, into the maximum allowed value.
794 * If some application really *needs* a bigger snapshot
795 * length, we should just increase MAXIMUM_SNAPLEN.
797 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
798 p
->snapshot
= MAXIMUM_SNAPLEN
;
801 * If the buffer size wasn't explicitly set, default to
802 * AIRPCAP_DEFAULT_KERNEL_BUFFER_SIZE.
804 if (p
->opt
.buffer_size
== 0)
805 p
->opt
.buffer_size
= AIRPCAP_DEFAULT_KERNEL_BUFFER_SIZE
;
807 if (!p_AirpcapSetKernelBuffer(pa
->adapter
, p
->opt
.buffer_size
)) {
808 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
809 "AirpcapSetKernelBuffer() failed: %s",
810 p_AirpcapGetLastError(pa
->adapter
));
814 if(!p_AirpcapGetReadEvent(pa
->adapter
, &pa
->read_event
)) {
815 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
816 "AirpcapGetReadEvent() failed: %s",
817 p_AirpcapGetLastError(pa
->adapter
));
821 /* Set the buffer size */
822 p
->bufsize
= AIRPCAP_DEFAULT_USER_BUFFER_SIZE
;
823 p
->buffer
= malloc(p
->bufsize
);
824 if (p
->buffer
== NULL
) {
825 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
830 if (p
->opt
.immediate
) {
831 /* Tell the driver to copy the buffer as soon as data arrives. */
832 if (!p_AirpcapSetMinToCopy(pa
->adapter
, 0)) {
833 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
834 "AirpcapSetMinToCopy() failed: %s",
835 p_AirpcapGetLastError(pa
->adapter
));
840 * Tell the driver to copy the buffer only if it contains
843 if (!p_AirpcapSetMinToCopy(pa
->adapter
, 16000)) {
844 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
845 "AirpcapSetMinToCopy() failed: %s",
846 p_AirpcapGetLastError(pa
->adapter
));
852 * Find out what the default link-layer header type is,
853 * and set p->datalink to that.
855 * We don't force it to another value because there
856 * might be some programs using WinPcap/Npcap that,
857 * when capturing on AirPcap devices, assume the
858 * default value set with the AirPcap configuration
859 * program is what you get.
861 * The out-of-the-box default appears to be radiotap.
863 if (!p_AirpcapGetLinkType(pa
->adapter
, &link_type
)) {
865 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
866 "AirpcapGetLinkType() failed: %s",
867 p_AirpcapGetLastError(pa
->adapter
));
872 case AIRPCAP_LT_802_11_PLUS_RADIO
:
873 p
->linktype
= DLT_IEEE802_11_RADIO
;
876 case AIRPCAP_LT_802_11_PLUS_PPI
:
877 p
->linktype
= DLT_PPI
;
880 case AIRPCAP_LT_802_11
:
881 p
->linktype
= DLT_IEEE802_11
;
884 case AIRPCAP_LT_UNKNOWN
:
887 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
888 "AirpcapGetLinkType() returned unknown link type %u",
894 * Now provide a list of all the supported types; we
895 * assume they all work. We put radiotap at the top,
896 * followed by PPI, followed by "no radio metadata".
898 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 3);
899 if (p
->dlt_list
== NULL
)
901 p
->dlt_list
[0] = DLT_IEEE802_11_RADIO
;
902 p
->dlt_list
[1] = DLT_PPI
;
903 p
->dlt_list
[2] = DLT_IEEE802_11
;
906 p
->read_op
= airpcap_read
;
907 p
->inject_op
= airpcap_inject
;
908 p
->setfilter_op
= airpcap_setfilter
;
909 p
->setdirection_op
= NULL
; /* Not implemented. */
910 p
->set_datalink_op
= airpcap_set_datalink
;
911 p
->getnonblock_op
= airpcap_getnonblock
;
912 p
->setnonblock_op
= airpcap_setnonblock
;
913 p
->breakloop_op
= airpcap_breakloop
;
914 p
->stats_op
= airpcap_stats
;
915 p
->stats_ex_op
= airpcap_stats_ex
;
916 p
->setbuff_op
= airpcap_setbuff
;
917 p
->setmode_op
= airpcap_setmode
;
918 p
->setmintocopy_op
= airpcap_setmintocopy
;
919 p
->getevent_op
= airpcap_getevent
;
920 p
->oid_get_request_op
= airpcap_oid_get_request
;
921 p
->oid_set_request_op
= airpcap_oid_set_request
;
922 p
->sendqueue_transmit_op
= airpcap_sendqueue_transmit
;
923 p
->setuserbuffer_op
= airpcap_setuserbuffer
;
924 p
->live_dump_op
= airpcap_live_dump
;
925 p
->live_dump_ended_op
= airpcap_live_dump_ended
;
926 p
->get_airpcap_handle_op
= airpcap_get_airpcap_handle
;
927 p
->cleanup_op
= airpcap_cleanup
;
936 * Monitor mode is supported.
939 airpcap_can_set_rfmon(pcap_t
*p
)
945 device_is_airpcap(const char *device
, char *ebuf
)
947 static const char airpcap_prefix
[] = "\\\\.\\airpcap";
950 * We don't determine this by calling AirpcapGetDeviceList()
951 * and looking at the list, as that appears to be a costly
954 * Instead, we just check whether it begins with "\\.\airpcap".
956 if (strncmp(device
, airpcap_prefix
, sizeof airpcap_prefix
- 1) == 0) {
958 * Yes, it's an AirPcap device.
964 * No, it's not an AirPcap device.
970 airpcap_create(const char *device
, char *ebuf
, int *is_ours
)
976 * This can be called before we've tried loading the library,
977 * so do so if we haven't already tried to do so.
979 if (load_airpcap_functions() != AIRPCAP_API_LOADED
) {
981 * We assume this means that we don't have the AirPcap
982 * software installed, which probably means we don't
983 * have an AirPcap device.
985 * Don't treat that as an error.
992 * Is this an AirPcap device?
994 ret
= device_is_airpcap(device
, ebuf
);
1005 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_airpcap
);
1009 p
->activate_op
= airpcap_activate
;
1010 p
->can_set_rfmon_op
= airpcap_can_set_rfmon
;
1015 * Add all AirPcap devices.
1018 airpcap_findalldevs(pcap_if_list_t
*devlistp
, char *errbuf
)
1020 AirpcapDeviceDescription
*airpcap_devices
, *airpcap_device
;
1021 char airpcap_errbuf
[AIRPCAP_ERRBUF_SIZE
];
1024 * This can be called before we've tried loading the library,
1025 * so do so if we haven't already tried to do so.
1027 if (load_airpcap_functions() != AIRPCAP_API_LOADED
) {
1029 * XXX - unless the error is "no such DLL", report this
1030 * as an error rather than as "no AirPcap devices"?
1035 if (!p_AirpcapGetDeviceList(&airpcap_devices
, airpcap_errbuf
)) {
1036 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1037 "AirpcapGetDeviceList() failed: %s", airpcap_errbuf
);
1041 for (airpcap_device
= airpcap_devices
; airpcap_device
!= NULL
;
1042 airpcap_device
= airpcap_device
->next
) {
1043 if (add_dev(devlistp
, airpcap_device
->Name
, 0,
1044 airpcap_device
->Description
, errbuf
) == NULL
) {
1048 p_AirpcapFreeDeviceList(airpcap_devices
);
1052 p_AirpcapFreeDeviceList(airpcap_devices
);