2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2008 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.
35 static const char rcsid
[] _U_
=
36 "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.42 2008-05-21 22:15:25 gianluca Exp $ (LBL)";
45 #include <ddk/ntddndis.h>
47 #endif /*__MINGW64__*/
50 #endif /*__MINGW32__*/
54 #endif /* HAVE_DAG_API */
57 #define errno (*_errno())
58 #endif /* __MINGW32__ */
60 static int pcap_setfilter_win32_npf(pcap_t
*, struct bpf_program
*);
61 static int pcap_setfilter_win32_dag(pcap_t
*, struct bpf_program
*);
62 static int pcap_getnonblock_win32(pcap_t
*, char *);
63 static int pcap_setnonblock_win32(pcap_t
*, int, char *);
65 /*dimension of the buffer in the pcap_t structure*/
66 #define WIN32_DEFAULT_USER_BUFFER_SIZE 256000
68 /*dimension of the buffer in the kernel driver NPF */
69 #define WIN32_DEFAULT_KERNEL_BUFFER_SIZE 1000000
71 /* Equivalent to ntohs(), but a lot faster under Windows */
72 #define SWAPS(_X) ((_X & 0xff) << 8) | (_X >> 8)
75 * Private data for capturing on WinPcap devices.
81 int dag_fcs_bits
; /* Number of checksum bits from link layer */
86 * Header that the WinPcap driver associates to the packets.
90 struct timeval bh_tstamp
; /* time stamp */
91 bpf_u_int32 bh_caplen
; /* length of captured portion */
92 bpf_u_int32 bh_datalen
; /* original length of packet */
93 u_short bh_hdrlen
; /* length of bpf header (this struct
94 plus alignment padding) */
97 CRITICAL_SECTION g_PcapCompileCriticalSection
;
105 if (dwReason
== DLL_PROCESS_ATTACH
)
107 InitializeCriticalSection(&g_PcapCompileCriticalSection
);
117 WORD wVersionRequested
;
120 wVersionRequested
= MAKEWORD( 1, 1);
121 err
= WSAStartup( wVersionRequested
, &wsaData
);
131 pcap_stats_win32(pcap_t
*p
, struct pcap_stat
*ps
)
134 if(PacketGetStats(p
->adapter
, (struct bpf_stat
*)ps
) != TRUE
){
135 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "PacketGetStats error: %s", pcap_win32strerror());
142 /* Set the dimension of the kernel-level capture buffer */
144 pcap_setbuff_win32(pcap_t
*p
, int dim
)
146 if(PacketSetBuff(p
->adapter
,dim
)==FALSE
)
148 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
154 /* Set the driver working mode */
156 pcap_setmode_win32(pcap_t
*p
, int mode
)
158 if(PacketSetMode(p
->adapter
,mode
)==FALSE
)
160 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: working mode not recognized");
167 /*set the minimum amount of data that will release a read call*/
169 pcap_setmintocopy_win32(pcap_t
*p
, int size
)
171 if(PacketSetMinToCopy(p
->adapter
, size
)==FALSE
)
173 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: unable to set the requested mintocopy size");
180 pcap_read_win32_npf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
184 register u_char
*bp
, *ep
;
189 * Has "pcap_breakloop()" been called?
193 * Yes - clear the flag that indicates that it
194 * has, and return -2 to indicate that we were
195 * told to break out of the loop.
201 /* capture the packets */
202 if(PacketReceivePacket(p
->adapter
,p
->Packet
,TRUE
)==FALSE
){
203 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
207 cc
= p
->Packet
->ulBytesReceived
;
209 bp
= p
->Packet
->Buffer
;
215 * Loop through each packet.
217 #define bhp ((struct bpf_hdr *)bp)
220 register int caplen
, hdrlen
;
223 * Has "pcap_breakloop()" been called?
224 * If so, return immediately - if we haven't read any
225 * packets, clear the flag and return -2 to indicate
226 * that we were told to break out of the loop, otherwise
227 * leave the flag set, so that the *next* call will break
228 * out of the loop without having read any packets, and
229 * return the number of packets we've processed so far.
244 caplen
= bhp
->bh_caplen
;
245 hdrlen
= bhp
->bh_hdrlen
;
248 * XXX A bpf_hdr matches a pcap_pkthdr.
250 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, bp
+ hdrlen
);
251 bp
+= Packet_WORDALIGN(caplen
+ hdrlen
);
252 if (++n
>= cnt
&& cnt
> 0) {
265 pcap_read_win32_dag(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
267 struct pcap_win
*pw
= p
->private;
269 int packet_len
= 0, caplen
= 0;
270 struct pcap_pkthdr pcap_header
;
273 dag_record_t
*header
;
274 unsigned erf_record_len
;
278 unsigned dfp
= p
->adapter
->DagFastProcess
;
281 if (cc
== 0) /* Get new packets only if we have processed all the ones of the previous read */
283 /* Get new packets from the network */
284 if(PacketReceivePacket(p
->adapter
, p
->Packet
, TRUE
)==FALSE
){
285 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read error: PacketReceivePacket failed");
289 cc
= p
->Packet
->ulBytesReceived
;
291 /* The timeout has expired but we no packets arrived */
293 header
= (dag_record_t
*)p
->adapter
->DagBuffer
;
296 header
= (dag_record_t
*)p
->bp
;
298 endofbuf
= (char*)header
+ cc
;
301 * Cycle through the packets
305 erf_record_len
= SWAPS(header
->rlen
);
306 if((char*)header
+ erf_record_len
> endofbuf
)
309 /* Increase the number of captured packets */
312 /* Find the beginning of the packet */
313 dp
= ((u_char
*)header
) + dag_record_size
;
315 /* Determine actual packet len */
319 packet_len
= ATM_SNAPLEN
;
320 caplen
= ATM_SNAPLEN
;
326 swt
= SWAPS(header
->wlen
);
327 packet_len
= swt
- (pw
->dag_fcs_bits
);
328 caplen
= erf_record_len
- dag_record_size
- 2;
329 if (caplen
> packet_len
)
338 swt
= SWAPS(header
->wlen
);
339 packet_len
= swt
- (pw
->dag_fcs_bits
);
340 caplen
= erf_record_len
- dag_record_size
;
341 if (caplen
> packet_len
)
349 if(caplen
> p
->snapshot
)
350 caplen
= p
->snapshot
;
353 * Has "pcap_breakloop()" been called?
354 * If so, return immediately - if we haven't read any
355 * packets, clear the flag and return -2 to indicate
356 * that we were told to break out of the loop, otherwise
357 * leave the flag set, so that the *next* call will break
358 * out of the loop without having read any packets, and
359 * return the number of packets we've processed so far.
370 p
->bp
= (char*)header
;
371 p
->cc
= endofbuf
- (char*)header
;
378 /* convert between timestamp formats */
380 pcap_header
.ts
.tv_sec
= (int)(ts
>> 32);
381 ts
= (ts
& 0xffffffffi
64) * 1000000;
382 ts
+= 0x80000000; /* rounding */
383 pcap_header
.ts
.tv_usec
= (int)(ts
>> 32);
384 if (pcap_header
.ts
.tv_usec
>= 1000000) {
385 pcap_header
.ts
.tv_usec
-= 1000000;
386 pcap_header
.ts
.tv_sec
++;
390 /* No underlaying filtering system. We need to filter on our own */
391 if (p
->fcode
.bf_insns
)
393 if (bpf_filter(p
->fcode
.bf_insns
, dp
, packet_len
, caplen
) == 0)
395 /* Move to next packet */
396 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
401 /* Fill the header for the user suppplied callback function */
402 pcap_header
.caplen
= caplen
;
403 pcap_header
.len
= packet_len
;
405 /* Call the callback function */
406 (*callback
)(user
, &pcap_header
, dp
);
408 /* Move to next packet */
409 header
= (dag_record_t
*)((char*)header
+ erf_record_len
);
411 /* Stop if the number of packets requested by user has been reached*/
412 if (++n
>= cnt
&& cnt
> 0)
414 p
->bp
= (char*)header
;
415 p
->cc
= endofbuf
- (char*)header
;
419 while((u_char
*)header
< endofbuf
);
423 #endif /* HAVE_DAG_API */
425 /* Send a packet to the network */
427 pcap_inject_win32(pcap_t
*p
, const void *buf
, size_t size
){
428 LPPACKET PacketToSend
;
430 PacketToSend
=PacketAllocatePacket();
432 if (PacketToSend
== NULL
)
434 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketAllocatePacket failed");
438 PacketInitPacket(PacketToSend
,(PVOID
)buf
,size
);
439 if(PacketSendPacket(p
->adapter
,PacketToSend
,TRUE
) == FALSE
){
440 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send error: PacketSendPacket failed");
441 PacketFreePacket(PacketToSend
);
445 PacketFreePacket(PacketToSend
);
448 * We assume it all got sent if "PacketSendPacket()" succeeded.
449 * "pcap_inject()" is expected to return the number of bytes
456 pcap_cleanup_win32(pcap_t
*p
)
458 if (p
->adapter
!= NULL
) {
459 PacketCloseAdapter(p
->adapter
);
463 PacketFreePacket(p
->Packet
);
466 pcap_cleanup_live_common(p
);
470 pcap_activate_win32(pcap_t
*p
)
472 struct pcap_win
*pw
= p
->private;
477 * No monitor mode on Windows. It could be done on
478 * Vista with drivers that support the native 802.11
479 * mechanism and monitor mode.
481 return (PCAP_ERROR_RFMON_NOTSUP
);
487 p
->adapter
= PacketOpenAdapter(p
->opt
.source
);
489 if (p
->adapter
== NULL
)
491 /* Adapter detected but we are not able to open it. Return failure. */
492 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Error opening adapter: %s", pcap_win32strerror());
497 if(PacketGetNetType (p
->adapter
,&type
) == FALSE
)
499 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Cannot determine the network type: %s", pcap_win32strerror());
504 switch (type
.LinkType
)
507 p
->linktype
= DLT_EN10MB
;
510 case NdisMedium802_3
:
511 p
->linktype
= DLT_EN10MB
;
513 * This is (presumably) a real Ethernet capture; give it a
514 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
515 * that an application can let you choose it, in case you're
516 * capturing DOCSIS traffic that a Cisco Cable Modem
517 * Termination System is putting out onto an Ethernet (it
518 * doesn't put an Ethernet header onto the wire, it puts raw
519 * DOCSIS frames out on the wire inside the low-level
522 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
524 * If that fails, just leave the list empty.
526 if (p
->dlt_list
!= NULL
) {
527 p
->dlt_list
[0] = DLT_EN10MB
;
528 p
->dlt_list
[1] = DLT_DOCSIS
;
534 p
->linktype
= DLT_FDDI
;
537 case NdisMedium802_5
:
538 p
->linktype
= DLT_IEEE802
;
541 case NdisMediumArcnetRaw
:
542 p
->linktype
= DLT_ARCNET
;
545 case NdisMediumArcnet878_2
:
546 p
->linktype
= DLT_ARCNET
;
550 p
->linktype
= DLT_ATM_RFC1483
;
553 case NdisMediumCHDLC
:
554 p
->linktype
= DLT_CHDLC
;
557 case NdisMediumPPPSerial
:
558 p
->linktype
= DLT_PPP_SERIAL
;
562 p
->linktype
= DLT_NULL
;
565 case NdisMediumBare80211
:
566 p
->linktype
= DLT_IEEE802_11
;
569 case NdisMediumRadio80211
:
570 p
->linktype
= DLT_IEEE802_11_RADIO
;
574 p
->linktype
= DLT_PPI
;
578 p
->linktype
= DLT_EN10MB
; /*an unknown adapter is assumed to be ethernet*/
582 /* Set promiscuous mode */
586 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_PROMISCUOUS
) == FALSE
)
588 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to promiscuous mode");
594 if (PacketSetHwFilter(p
->adapter
,NDIS_PACKET_TYPE_ALL_LOCAL
) == FALSE
)
596 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to set hardware filter to non-promiscuous mode");
601 /* Set the buffer size */
602 p
->bufsize
= WIN32_DEFAULT_USER_BUFFER_SIZE
;
604 /* allocate Packet structure used during the capture */
605 if((p
->Packet
= PacketAllocatePacket())==NULL
)
607 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "failed to allocate the PACKET structure");
611 if(!(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
))
614 * Traditional Adapter
617 * If the buffer size wasn't explicitly set, default to
618 * WIN32_DEFAULT_USER_BUFFER_SIZE.
620 if (p
->opt
.buffer_size
== 0)
621 p
->opt
.buffer_size
= WIN32_DEFAULT_KERNEL_BUFFER_SIZE
;
623 if(PacketSetBuff(p
->adapter
,p
->opt
.buffer_size
)==FALSE
)
625 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "driver error: not enough memory to allocate the kernel buffer");
629 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
630 if (p
->buffer
== NULL
)
632 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s", pcap_strerror(errno
));
636 PacketInitPacket(p
->Packet
,(BYTE
*)p
->buffer
,p
->bufsize
);
638 /* tell the driver to copy the buffer only if it contains at least 16K */
639 if(PacketSetMinToCopy(p
->adapter
,16000)==FALSE
)
641 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,"Error calling PacketSetMinToCopy: %s", pcap_win32strerror());
658 snprintf(keyname
, sizeof(keyname
), "%s\\CardParams\\%s",
659 "SYSTEM\\CurrentControlSet\\Services\\DAG",
660 strstr(_strlwr(p
->opt
.source
), "dag"));
663 status
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, keyname
, 0, KEY_READ
, &dagkey
);
664 if(status
!= ERROR_SUCCESS
)
667 status
= RegQueryValueEx(dagkey
,
674 if(status
!= ERROR_SUCCESS
)
684 p
->snapshot
= PacketSetSnapLen(p
->adapter
, snaplen
);
686 /* Set the length of the FCS associated to any packet. This value
687 * will be subtracted to the packet length */
688 pw
->dag_fcs_bits
= p
->adapter
->DagFcsLen
;
692 #endif /* HAVE_DAG_API */
694 PacketSetReadTimeout(p
->adapter
, p
->opt
.timeout
);
697 if(p
->adapter
->Flags
& INFO_FLAG_DAG_CARD
)
699 /* install dag specific handlers for read and setfilter */
700 p
->read_op
= pcap_read_win32_dag
;
701 p
->setfilter_op
= pcap_setfilter_win32_dag
;
705 #endif /* HAVE_DAG_API */
706 /* install traditional npf handlers for read and setfilter */
707 p
->read_op
= pcap_read_win32_npf
;
708 p
->setfilter_op
= pcap_setfilter_win32_npf
;
711 #endif /* HAVE_DAG_API */
712 p
->setdirection_op
= NULL
; /* Not implemented. */
713 /* XXX - can this be implemented on some versions of Windows? */
714 p
->inject_op
= pcap_inject_win32
;
715 p
->set_datalink_op
= NULL
; /* can't change data link type */
716 p
->getnonblock_op
= pcap_getnonblock_win32
;
717 p
->setnonblock_op
= pcap_setnonblock_win32
;
718 p
->stats_op
= pcap_stats_win32
;
719 p
->setbuff_op
= pcap_setbuff_win32
;
720 p
->setmode_op
= pcap_setmode_win32
;
721 p
->setmintocopy_op
= pcap_setmintocopy_win32
;
722 p
->cleanup_op
= pcap_cleanup_win32
;
726 pcap_cleanup_win32(p
);
731 pcap_create_interface(const char *device
, char *ebuf
)
735 if (strlen(device
) == 1)
738 * It's probably a unicode string
739 * Convert to ascii and pass it to pcap_create_common
741 * This wonderful hack is needed because pcap_lookupdev still returns
742 * unicode strings, and it's used by windump when no device is specified
743 * in the command line
748 length
= wcslen((wchar_t*)device
);
750 deviceAscii
= (char*)malloc(length
+ 1);
752 if (deviceAscii
== NULL
)
754 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Malloc failed");
758 snprintf(deviceAscii
, length
+ 1, "%ws", (wchar_t*)device
);
759 p
= pcap_create_common(deviceAscii
, ebuf
, sizeof (struct pcap_win
));
764 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_win
));
770 p
->activate_op
= pcap_activate_win32
;
775 pcap_setfilter_win32_npf(pcap_t
*p
, struct bpf_program
*fp
)
777 if(PacketSetBpf(p
->adapter
,fp
)==FALSE
){
779 * Kernel filter not installed.
780 * XXX - fall back on userland filtering, as is done
781 * on other platforms?
783 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Driver error: cannot set bpf filter: %s", pcap_win32strerror());
788 * Discard any previously-received packets, as they might have
789 * passed whatever filter was formerly in effect, but might
790 * not pass this filter (BIOCSETF discards packets buffered
791 * in the kernel, so you can lose packets in any case).
798 * We filter at user level, since the kernel driver does't process the packets
801 pcap_setfilter_win32_dag(pcap_t
*p
, struct bpf_program
*fp
) {
805 strncpy(p
->errbuf
, "setfilter: No filter specified", sizeof(p
->errbuf
));
809 /* Install a user level filter */
810 if (install_bpf_program(p
, fp
) < 0)
812 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
813 "setfilter, unable to install the filter: %s", pcap_strerror(errno
));
821 pcap_getnonblock_win32(pcap_t
*p
, char *errbuf
)
823 struct pcap_win
*pw
= p
->private;
826 * XXX - if there were a PacketGetReadTimeout() call, we
827 * would use it, and return 1 if the timeout is -1
830 return (pw
->nonblock
);
834 pcap_setnonblock_win32(pcap_t
*p
, int nonblock
, char *errbuf
)
836 struct pcap_win
*pw
= p
->private;
841 * Set the read timeout to -1 for non-blocking mode.
846 * Restore the timeout set when the device was opened.
847 * (Note that this may be -1, in which case we're not
848 * really leaving non-blocking mode.)
850 newtimeout
= p
->opt
.timeout
;
852 if (!PacketSetReadTimeout(p
->adapter
, newtimeout
)) {
853 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
854 "PacketSetReadTimeout: %s", pcap_win32strerror());
857 pw
->nonblock
= (newtimeout
== -1);
861 /*platform-dependent routine to add devices other than NDIS interfaces*/
863 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)