1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3*8b26181fSAndroid Build Coastguard Worker * Copyright (c) 2005 - 2010 CACE Technologies, Davis (California)
4*8b26181fSAndroid Build Coastguard Worker * All rights reserved.
5*8b26181fSAndroid Build Coastguard Worker *
6*8b26181fSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
7*8b26181fSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
8*8b26181fSAndroid Build Coastguard Worker * are met:
9*8b26181fSAndroid Build Coastguard Worker *
10*8b26181fSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
11*8b26181fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
12*8b26181fSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
13*8b26181fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
14*8b26181fSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
15*8b26181fSAndroid Build Coastguard Worker * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16*8b26181fSAndroid Build Coastguard Worker * nor the names of its contributors may be used to endorse or promote
17*8b26181fSAndroid Build Coastguard Worker * products derived from this software without specific prior written
18*8b26181fSAndroid Build Coastguard Worker * permission.
19*8b26181fSAndroid Build Coastguard Worker *
20*8b26181fSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*8b26181fSAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*8b26181fSAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23*8b26181fSAndroid Build Coastguard Worker * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24*8b26181fSAndroid Build Coastguard Worker * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25*8b26181fSAndroid Build Coastguard Worker * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26*8b26181fSAndroid Build Coastguard Worker * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27*8b26181fSAndroid Build Coastguard Worker * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28*8b26181fSAndroid Build Coastguard Worker * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29*8b26181fSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30*8b26181fSAndroid Build Coastguard Worker * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*8b26181fSAndroid Build Coastguard Worker *
32*8b26181fSAndroid Build Coastguard Worker */
33*8b26181fSAndroid Build Coastguard Worker
34*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
35*8b26181fSAndroid Build Coastguard Worker #include <config.h>
36*8b26181fSAndroid Build Coastguard Worker #endif
37*8b26181fSAndroid Build Coastguard Worker
38*8b26181fSAndroid Build Coastguard Worker #include <errno.h>
39*8b26181fSAndroid Build Coastguard Worker #include <limits.h> /* for INT_MAX */
40*8b26181fSAndroid Build Coastguard Worker #define PCAP_DONT_INCLUDE_PCAP_BPF_H
41*8b26181fSAndroid Build Coastguard Worker #include <Packet32.h>
42*8b26181fSAndroid Build Coastguard Worker #include <pcap-int.h>
43*8b26181fSAndroid Build Coastguard Worker #include <pcap/dlt.h>
44*8b26181fSAndroid Build Coastguard Worker
45*8b26181fSAndroid Build Coastguard Worker /*
46*8b26181fSAndroid Build Coastguard Worker * XXX - Packet32.h defines bpf_program, so we can't include
47*8b26181fSAndroid Build Coastguard Worker * <pcap/bpf.h>, which also defines it; that's why we define
48*8b26181fSAndroid Build Coastguard Worker * PCAP_DONT_INCLUDE_PCAP_BPF_H,
49*8b26181fSAndroid Build Coastguard Worker *
50*8b26181fSAndroid Build Coastguard Worker * However, no header in the WinPcap or Npcap SDKs defines the
51*8b26181fSAndroid Build Coastguard Worker * macros for BPF code, so we have to define them ourselves.
52*8b26181fSAndroid Build Coastguard Worker */
53*8b26181fSAndroid Build Coastguard Worker #define BPF_RET 0x06
54*8b26181fSAndroid Build Coastguard Worker #define BPF_K 0x00
55*8b26181fSAndroid Build Coastguard Worker
56*8b26181fSAndroid Build Coastguard Worker /* Old-school MinGW have these headers in a different place.
57*8b26181fSAndroid Build Coastguard Worker */
58*8b26181fSAndroid Build Coastguard Worker #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
59*8b26181fSAndroid Build Coastguard Worker #include <ddk/ntddndis.h>
60*8b26181fSAndroid Build Coastguard Worker #include <ddk/ndis.h>
61*8b26181fSAndroid Build Coastguard Worker #else
62*8b26181fSAndroid Build Coastguard Worker #include <ntddndis.h> /* MSVC/TDM-MinGW/MinGW64 */
63*8b26181fSAndroid Build Coastguard Worker #endif
64*8b26181fSAndroid Build Coastguard Worker
65*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DAG_API
66*8b26181fSAndroid Build Coastguard Worker #include <dagnew.h>
67*8b26181fSAndroid Build Coastguard Worker #include <dagapi.h>
68*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_DAG_API */
69*8b26181fSAndroid Build Coastguard Worker
70*8b26181fSAndroid Build Coastguard Worker #include "diag-control.h"
71*8b26181fSAndroid Build Coastguard Worker
72*8b26181fSAndroid Build Coastguard Worker #include "pcap-airpcap.h"
73*8b26181fSAndroid Build Coastguard Worker
74*8b26181fSAndroid Build Coastguard Worker static int pcap_setfilter_npf(pcap_t *, struct bpf_program *);
75*8b26181fSAndroid Build Coastguard Worker static int pcap_setfilter_win32_dag(pcap_t *, struct bpf_program *);
76*8b26181fSAndroid Build Coastguard Worker static int pcap_getnonblock_npf(pcap_t *);
77*8b26181fSAndroid Build Coastguard Worker static int pcap_setnonblock_npf(pcap_t *, int);
78*8b26181fSAndroid Build Coastguard Worker
79*8b26181fSAndroid Build Coastguard Worker /*dimension of the buffer in the pcap_t structure*/
80*8b26181fSAndroid Build Coastguard Worker #define WIN32_DEFAULT_USER_BUFFER_SIZE 256000
81*8b26181fSAndroid Build Coastguard Worker
82*8b26181fSAndroid Build Coastguard Worker /*dimension of the buffer in the kernel driver NPF */
83*8b26181fSAndroid Build Coastguard Worker #define WIN32_DEFAULT_KERNEL_BUFFER_SIZE 1000000
84*8b26181fSAndroid Build Coastguard Worker
85*8b26181fSAndroid Build Coastguard Worker /* Equivalent to ntohs(), but a lot faster under Windows */
86*8b26181fSAndroid Build Coastguard Worker #define SWAPS(_X) ((_X & 0xff) << 8) | (_X >> 8)
87*8b26181fSAndroid Build Coastguard Worker
88*8b26181fSAndroid Build Coastguard Worker /*
89*8b26181fSAndroid Build Coastguard Worker * Private data for capturing on WinPcap/Npcap devices.
90*8b26181fSAndroid Build Coastguard Worker */
91*8b26181fSAndroid Build Coastguard Worker struct pcap_win {
92*8b26181fSAndroid Build Coastguard Worker ADAPTER *adapter; /* the packet32 ADAPTER for the device */
93*8b26181fSAndroid Build Coastguard Worker int nonblock;
94*8b26181fSAndroid Build Coastguard Worker int rfmon_selfstart; /* a flag tells whether the monitor mode is set by itself */
95*8b26181fSAndroid Build Coastguard Worker int filtering_in_kernel; /* using kernel filter */
96*8b26181fSAndroid Build Coastguard Worker
97*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DAG_API
98*8b26181fSAndroid Build Coastguard Worker int dag_fcs_bits; /* Number of checksum bits from link layer */
99*8b26181fSAndroid Build Coastguard Worker #endif
100*8b26181fSAndroid Build Coastguard Worker
101*8b26181fSAndroid Build Coastguard Worker #ifdef ENABLE_REMOTE
102*8b26181fSAndroid Build Coastguard Worker int samp_npkt; /* parameter needed for sampling, with '1 out of N' method has been requested */
103*8b26181fSAndroid Build Coastguard Worker struct timeval samp_time; /* parameter needed for sampling, with '1 every N ms' method has been requested */
104*8b26181fSAndroid Build Coastguard Worker #endif
105*8b26181fSAndroid Build Coastguard Worker };
106*8b26181fSAndroid Build Coastguard Worker
107*8b26181fSAndroid Build Coastguard Worker /*
108*8b26181fSAndroid Build Coastguard Worker * Define stub versions of the monitor-mode support routines if this
109*8b26181fSAndroid Build Coastguard Worker * isn't Npcap. HAVE_NPCAP_PACKET_API is defined by Npcap but not
110*8b26181fSAndroid Build Coastguard Worker * WinPcap.
111*8b26181fSAndroid Build Coastguard Worker */
112*8b26181fSAndroid Build Coastguard Worker #ifndef HAVE_NPCAP_PACKET_API
113*8b26181fSAndroid Build Coastguard Worker static int
PacketIsMonitorModeSupported(PCHAR AdapterName _U_)114*8b26181fSAndroid Build Coastguard Worker PacketIsMonitorModeSupported(PCHAR AdapterName _U_)
115*8b26181fSAndroid Build Coastguard Worker {
116*8b26181fSAndroid Build Coastguard Worker /*
117*8b26181fSAndroid Build Coastguard Worker * We don't support monitor mode.
118*8b26181fSAndroid Build Coastguard Worker */
119*8b26181fSAndroid Build Coastguard Worker return (0);
120*8b26181fSAndroid Build Coastguard Worker }
121*8b26181fSAndroid Build Coastguard Worker
122*8b26181fSAndroid Build Coastguard Worker static int
PacketSetMonitorMode(PCHAR AdapterName _U_,int mode _U_)123*8b26181fSAndroid Build Coastguard Worker PacketSetMonitorMode(PCHAR AdapterName _U_, int mode _U_)
124*8b26181fSAndroid Build Coastguard Worker {
125*8b26181fSAndroid Build Coastguard Worker /*
126*8b26181fSAndroid Build Coastguard Worker * This should never be called, as PacketIsMonitorModeSupported()
127*8b26181fSAndroid Build Coastguard Worker * will return 0, meaning "we don't support monitor mode, so
128*8b26181fSAndroid Build Coastguard Worker * don't try to turn it on or off".
129*8b26181fSAndroid Build Coastguard Worker */
130*8b26181fSAndroid Build Coastguard Worker return (0);
131*8b26181fSAndroid Build Coastguard Worker }
132*8b26181fSAndroid Build Coastguard Worker
133*8b26181fSAndroid Build Coastguard Worker static int
PacketGetMonitorMode(PCHAR AdapterName _U_)134*8b26181fSAndroid Build Coastguard Worker PacketGetMonitorMode(PCHAR AdapterName _U_)
135*8b26181fSAndroid Build Coastguard Worker {
136*8b26181fSAndroid Build Coastguard Worker /*
137*8b26181fSAndroid Build Coastguard Worker * This should fail, so that pcap_activate_npf() returns
138*8b26181fSAndroid Build Coastguard Worker * PCAP_ERROR_RFMON_NOTSUP if our caller requested monitor
139*8b26181fSAndroid Build Coastguard Worker * mode.
140*8b26181fSAndroid Build Coastguard Worker */
141*8b26181fSAndroid Build Coastguard Worker return (-1);
142*8b26181fSAndroid Build Coastguard Worker }
143*8b26181fSAndroid Build Coastguard Worker #endif
144*8b26181fSAndroid Build Coastguard Worker
145*8b26181fSAndroid Build Coastguard Worker /*
146*8b26181fSAndroid Build Coastguard Worker * Sigh. PacketRequest() will have made a DeviceIoControl()
147*8b26181fSAndroid Build Coastguard Worker * call to the NPF driver to perform the OID request, with a
148*8b26181fSAndroid Build Coastguard Worker * BIOCQUERYOID ioctl. The kernel code should get back one
149*8b26181fSAndroid Build Coastguard Worker * of NDIS_STATUS_INVALID_OID, NDIS_STATUS_NOT_SUPPORTED,
150*8b26181fSAndroid Build Coastguard Worker * or NDIS_STATUS_NOT_RECOGNIZED if the OID request isn't
151*8b26181fSAndroid Build Coastguard Worker * supported by the OS or the driver, but that doesn't seem
152*8b26181fSAndroid Build Coastguard Worker * to make it to the caller of PacketRequest() in a
153*8b26181fSAndroid Build Coastguard Worker * reliable fashion.
154*8b26181fSAndroid Build Coastguard Worker */
155*8b26181fSAndroid Build Coastguard Worker #define NDIS_STATUS_INVALID_OID 0xc0010017
156*8b26181fSAndroid Build Coastguard Worker #define NDIS_STATUS_NOT_SUPPORTED 0xc00000bb /* STATUS_NOT_SUPPORTED */
157*8b26181fSAndroid Build Coastguard Worker #define NDIS_STATUS_NOT_RECOGNIZED 0x00010001
158*8b26181fSAndroid Build Coastguard Worker
159*8b26181fSAndroid Build Coastguard Worker static int
oid_get_request(ADAPTER * adapter,bpf_u_int32 oid,void * data,size_t * lenp,char * errbuf)160*8b26181fSAndroid Build Coastguard Worker oid_get_request(ADAPTER *adapter, bpf_u_int32 oid, void *data, size_t *lenp,
161*8b26181fSAndroid Build Coastguard Worker char *errbuf)
162*8b26181fSAndroid Build Coastguard Worker {
163*8b26181fSAndroid Build Coastguard Worker PACKET_OID_DATA *oid_data_arg;
164*8b26181fSAndroid Build Coastguard Worker
165*8b26181fSAndroid Build Coastguard Worker /*
166*8b26181fSAndroid Build Coastguard Worker * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
167*8b26181fSAndroid Build Coastguard Worker * It should be big enough to hold "*lenp" bytes of data; it
168*8b26181fSAndroid Build Coastguard Worker * will actually be slightly larger, as PACKET_OID_DATA has a
169*8b26181fSAndroid Build Coastguard Worker * 1-byte data array at the end, standing in for the variable-length
170*8b26181fSAndroid Build Coastguard Worker * data that's actually there.
171*8b26181fSAndroid Build Coastguard Worker */
172*8b26181fSAndroid Build Coastguard Worker oid_data_arg = malloc(sizeof (PACKET_OID_DATA) + *lenp);
173*8b26181fSAndroid Build Coastguard Worker if (oid_data_arg == NULL) {
174*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
175*8b26181fSAndroid Build Coastguard Worker "Couldn't allocate argument buffer for PacketRequest");
176*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
177*8b26181fSAndroid Build Coastguard Worker }
178*8b26181fSAndroid Build Coastguard Worker
179*8b26181fSAndroid Build Coastguard Worker /*
180*8b26181fSAndroid Build Coastguard Worker * No need to copy the data - we're doing a fetch.
181*8b26181fSAndroid Build Coastguard Worker */
182*8b26181fSAndroid Build Coastguard Worker oid_data_arg->Oid = oid;
183*8b26181fSAndroid Build Coastguard Worker oid_data_arg->Length = (ULONG)(*lenp); /* XXX - check for ridiculously large value? */
184*8b26181fSAndroid Build Coastguard Worker if (!PacketRequest(adapter, FALSE, oid_data_arg)) {
185*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(errbuf, PCAP_ERRBUF_SIZE,
186*8b26181fSAndroid Build Coastguard Worker GetLastError(), "Error calling PacketRequest");
187*8b26181fSAndroid Build Coastguard Worker free(oid_data_arg);
188*8b26181fSAndroid Build Coastguard Worker return (-1);
189*8b26181fSAndroid Build Coastguard Worker }
190*8b26181fSAndroid Build Coastguard Worker
191*8b26181fSAndroid Build Coastguard Worker /*
192*8b26181fSAndroid Build Coastguard Worker * Get the length actually supplied.
193*8b26181fSAndroid Build Coastguard Worker */
194*8b26181fSAndroid Build Coastguard Worker *lenp = oid_data_arg->Length;
195*8b26181fSAndroid Build Coastguard Worker
196*8b26181fSAndroid Build Coastguard Worker /*
197*8b26181fSAndroid Build Coastguard Worker * Copy back the data we fetched.
198*8b26181fSAndroid Build Coastguard Worker */
199*8b26181fSAndroid Build Coastguard Worker memcpy(data, oid_data_arg->Data, *lenp);
200*8b26181fSAndroid Build Coastguard Worker free(oid_data_arg);
201*8b26181fSAndroid Build Coastguard Worker return (0);
202*8b26181fSAndroid Build Coastguard Worker }
203*8b26181fSAndroid Build Coastguard Worker
204*8b26181fSAndroid Build Coastguard Worker static int
pcap_stats_npf(pcap_t * p,struct pcap_stat * ps)205*8b26181fSAndroid Build Coastguard Worker pcap_stats_npf(pcap_t *p, struct pcap_stat *ps)
206*8b26181fSAndroid Build Coastguard Worker {
207*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
208*8b26181fSAndroid Build Coastguard Worker struct bpf_stat bstats;
209*8b26181fSAndroid Build Coastguard Worker
210*8b26181fSAndroid Build Coastguard Worker /*
211*8b26181fSAndroid Build Coastguard Worker * Try to get statistics.
212*8b26181fSAndroid Build Coastguard Worker *
213*8b26181fSAndroid Build Coastguard Worker * (Please note - "struct pcap_stat" is *not* the same as
214*8b26181fSAndroid Build Coastguard Worker * WinPcap's "struct bpf_stat". It might currently have the
215*8b26181fSAndroid Build Coastguard Worker * same layout, but let's not cheat.
216*8b26181fSAndroid Build Coastguard Worker *
217*8b26181fSAndroid Build Coastguard Worker * Note also that we don't fill in ps_capt, as we might have
218*8b26181fSAndroid Build Coastguard Worker * been called by code compiled against an earlier version of
219*8b26181fSAndroid Build Coastguard Worker * WinPcap that didn't have ps_capt, in which case filling it
220*8b26181fSAndroid Build Coastguard Worker * in would stomp on whatever comes after the structure passed
221*8b26181fSAndroid Build Coastguard Worker * to us.
222*8b26181fSAndroid Build Coastguard Worker */
223*8b26181fSAndroid Build Coastguard Worker if (!PacketGetStats(pw->adapter, &bstats)) {
224*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
225*8b26181fSAndroid Build Coastguard Worker GetLastError(), "PacketGetStats error");
226*8b26181fSAndroid Build Coastguard Worker return (-1);
227*8b26181fSAndroid Build Coastguard Worker }
228*8b26181fSAndroid Build Coastguard Worker ps->ps_recv = bstats.bs_recv;
229*8b26181fSAndroid Build Coastguard Worker ps->ps_drop = bstats.bs_drop;
230*8b26181fSAndroid Build Coastguard Worker
231*8b26181fSAndroid Build Coastguard Worker /*
232*8b26181fSAndroid Build Coastguard Worker * XXX - PacketGetStats() doesn't fill this in, so we just
233*8b26181fSAndroid Build Coastguard Worker * return 0.
234*8b26181fSAndroid Build Coastguard Worker */
235*8b26181fSAndroid Build Coastguard Worker #if 0
236*8b26181fSAndroid Build Coastguard Worker ps->ps_ifdrop = bstats.ps_ifdrop;
237*8b26181fSAndroid Build Coastguard Worker #else
238*8b26181fSAndroid Build Coastguard Worker ps->ps_ifdrop = 0;
239*8b26181fSAndroid Build Coastguard Worker #endif
240*8b26181fSAndroid Build Coastguard Worker
241*8b26181fSAndroid Build Coastguard Worker return (0);
242*8b26181fSAndroid Build Coastguard Worker }
243*8b26181fSAndroid Build Coastguard Worker
244*8b26181fSAndroid Build Coastguard Worker /*
245*8b26181fSAndroid Build Coastguard Worker * Win32-only routine for getting statistics.
246*8b26181fSAndroid Build Coastguard Worker *
247*8b26181fSAndroid Build Coastguard Worker * This way is definitely safer than passing the pcap_stat * from the userland.
248*8b26181fSAndroid Build Coastguard Worker * In fact, there could happen than the user allocates a variable which is not
249*8b26181fSAndroid Build Coastguard Worker * big enough for the new structure, and the library will write in a zone
250*8b26181fSAndroid Build Coastguard Worker * which is not allocated to this variable.
251*8b26181fSAndroid Build Coastguard Worker *
252*8b26181fSAndroid Build Coastguard Worker * In this way, we're pretty sure we are writing on memory allocated to this
253*8b26181fSAndroid Build Coastguard Worker * variable.
254*8b26181fSAndroid Build Coastguard Worker *
255*8b26181fSAndroid Build Coastguard Worker * XXX - but this is the wrong way to handle statistics. Instead, we should
256*8b26181fSAndroid Build Coastguard Worker * have an API that returns data in a form like the Options section of a
257*8b26181fSAndroid Build Coastguard Worker * pcapng Interface Statistics Block:
258*8b26181fSAndroid Build Coastguard Worker *
259*8b26181fSAndroid Build Coastguard Worker * 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
260*8b26181fSAndroid Build Coastguard Worker *
261*8b26181fSAndroid Build Coastguard Worker * which would let us add new statistics straightforwardly and indicate which
262*8b26181fSAndroid Build Coastguard Worker * statistics we are and are *not* providing, rather than having to provide
263*8b26181fSAndroid Build Coastguard Worker * possibly-bogus values for statistics we can't provide.
264*8b26181fSAndroid Build Coastguard Worker */
265*8b26181fSAndroid Build Coastguard Worker static struct pcap_stat *
pcap_stats_ex_npf(pcap_t * p,int * pcap_stat_size)266*8b26181fSAndroid Build Coastguard Worker pcap_stats_ex_npf(pcap_t *p, int *pcap_stat_size)
267*8b26181fSAndroid Build Coastguard Worker {
268*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
269*8b26181fSAndroid Build Coastguard Worker struct bpf_stat bstats;
270*8b26181fSAndroid Build Coastguard Worker
271*8b26181fSAndroid Build Coastguard Worker *pcap_stat_size = sizeof (p->stat);
272*8b26181fSAndroid Build Coastguard Worker
273*8b26181fSAndroid Build Coastguard Worker /*
274*8b26181fSAndroid Build Coastguard Worker * Try to get statistics.
275*8b26181fSAndroid Build Coastguard Worker *
276*8b26181fSAndroid Build Coastguard Worker * (Please note - "struct pcap_stat" is *not* the same as
277*8b26181fSAndroid Build Coastguard Worker * WinPcap's "struct bpf_stat". It might currently have the
278*8b26181fSAndroid Build Coastguard Worker * same layout, but let's not cheat.)
279*8b26181fSAndroid Build Coastguard Worker */
280*8b26181fSAndroid Build Coastguard Worker if (!PacketGetStatsEx(pw->adapter, &bstats)) {
281*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
282*8b26181fSAndroid Build Coastguard Worker GetLastError(), "PacketGetStatsEx error");
283*8b26181fSAndroid Build Coastguard Worker return (NULL);
284*8b26181fSAndroid Build Coastguard Worker }
285*8b26181fSAndroid Build Coastguard Worker p->stat.ps_recv = bstats.bs_recv;
286*8b26181fSAndroid Build Coastguard Worker p->stat.ps_drop = bstats.bs_drop;
287*8b26181fSAndroid Build Coastguard Worker p->stat.ps_ifdrop = bstats.ps_ifdrop;
288*8b26181fSAndroid Build Coastguard Worker /*
289*8b26181fSAndroid Build Coastguard Worker * Just in case this is ever compiled for a target other than
290*8b26181fSAndroid Build Coastguard Worker * Windows, which is somewhere between extremely unlikely and
291*8b26181fSAndroid Build Coastguard Worker * impossible.
292*8b26181fSAndroid Build Coastguard Worker */
293*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
294*8b26181fSAndroid Build Coastguard Worker p->stat.ps_capt = bstats.bs_capt;
295*8b26181fSAndroid Build Coastguard Worker #endif
296*8b26181fSAndroid Build Coastguard Worker return (&p->stat);
297*8b26181fSAndroid Build Coastguard Worker }
298*8b26181fSAndroid Build Coastguard Worker
299*8b26181fSAndroid Build Coastguard Worker /* Set the dimension of the kernel-level capture buffer */
300*8b26181fSAndroid Build Coastguard Worker static int
pcap_setbuff_npf(pcap_t * p,int dim)301*8b26181fSAndroid Build Coastguard Worker pcap_setbuff_npf(pcap_t *p, int dim)
302*8b26181fSAndroid Build Coastguard Worker {
303*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
304*8b26181fSAndroid Build Coastguard Worker
305*8b26181fSAndroid Build Coastguard Worker if(PacketSetBuff(pw->adapter,dim)==FALSE)
306*8b26181fSAndroid Build Coastguard Worker {
307*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: not enough memory to allocate the kernel buffer");
308*8b26181fSAndroid Build Coastguard Worker return (-1);
309*8b26181fSAndroid Build Coastguard Worker }
310*8b26181fSAndroid Build Coastguard Worker return (0);
311*8b26181fSAndroid Build Coastguard Worker }
312*8b26181fSAndroid Build Coastguard Worker
313*8b26181fSAndroid Build Coastguard Worker /* Set the driver working mode */
314*8b26181fSAndroid Build Coastguard Worker static int
pcap_setmode_npf(pcap_t * p,int mode)315*8b26181fSAndroid Build Coastguard Worker pcap_setmode_npf(pcap_t *p, int mode)
316*8b26181fSAndroid Build Coastguard Worker {
317*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
318*8b26181fSAndroid Build Coastguard Worker
319*8b26181fSAndroid Build Coastguard Worker if(PacketSetMode(pw->adapter,mode)==FALSE)
320*8b26181fSAndroid Build Coastguard Worker {
321*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: working mode not recognized");
322*8b26181fSAndroid Build Coastguard Worker return (-1);
323*8b26181fSAndroid Build Coastguard Worker }
324*8b26181fSAndroid Build Coastguard Worker
325*8b26181fSAndroid Build Coastguard Worker return (0);
326*8b26181fSAndroid Build Coastguard Worker }
327*8b26181fSAndroid Build Coastguard Worker
328*8b26181fSAndroid Build Coastguard Worker /*set the minimum amount of data that will release a read call*/
329*8b26181fSAndroid Build Coastguard Worker static int
pcap_setmintocopy_npf(pcap_t * p,int size)330*8b26181fSAndroid Build Coastguard Worker pcap_setmintocopy_npf(pcap_t *p, int size)
331*8b26181fSAndroid Build Coastguard Worker {
332*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
333*8b26181fSAndroid Build Coastguard Worker
334*8b26181fSAndroid Build Coastguard Worker if(PacketSetMinToCopy(pw->adapter, size)==FALSE)
335*8b26181fSAndroid Build Coastguard Worker {
336*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: unable to set the requested mintocopy size");
337*8b26181fSAndroid Build Coastguard Worker return (-1);
338*8b26181fSAndroid Build Coastguard Worker }
339*8b26181fSAndroid Build Coastguard Worker return (0);
340*8b26181fSAndroid Build Coastguard Worker }
341*8b26181fSAndroid Build Coastguard Worker
342*8b26181fSAndroid Build Coastguard Worker static HANDLE
pcap_getevent_npf(pcap_t * p)343*8b26181fSAndroid Build Coastguard Worker pcap_getevent_npf(pcap_t *p)
344*8b26181fSAndroid Build Coastguard Worker {
345*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
346*8b26181fSAndroid Build Coastguard Worker
347*8b26181fSAndroid Build Coastguard Worker return (PacketGetReadEvent(pw->adapter));
348*8b26181fSAndroid Build Coastguard Worker }
349*8b26181fSAndroid Build Coastguard Worker
350*8b26181fSAndroid Build Coastguard Worker static int
pcap_oid_get_request_npf(pcap_t * p,bpf_u_int32 oid,void * data,size_t * lenp)351*8b26181fSAndroid Build Coastguard Worker pcap_oid_get_request_npf(pcap_t *p, bpf_u_int32 oid, void *data, size_t *lenp)
352*8b26181fSAndroid Build Coastguard Worker {
353*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
354*8b26181fSAndroid Build Coastguard Worker
355*8b26181fSAndroid Build Coastguard Worker return (oid_get_request(pw->adapter, oid, data, lenp, p->errbuf));
356*8b26181fSAndroid Build Coastguard Worker }
357*8b26181fSAndroid Build Coastguard Worker
358*8b26181fSAndroid Build Coastguard Worker static int
pcap_oid_set_request_npf(pcap_t * p,bpf_u_int32 oid,const void * data,size_t * lenp)359*8b26181fSAndroid Build Coastguard Worker pcap_oid_set_request_npf(pcap_t *p, bpf_u_int32 oid, const void *data,
360*8b26181fSAndroid Build Coastguard Worker size_t *lenp)
361*8b26181fSAndroid Build Coastguard Worker {
362*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
363*8b26181fSAndroid Build Coastguard Worker PACKET_OID_DATA *oid_data_arg;
364*8b26181fSAndroid Build Coastguard Worker
365*8b26181fSAndroid Build Coastguard Worker /*
366*8b26181fSAndroid Build Coastguard Worker * Allocate a PACKET_OID_DATA structure to hand to PacketRequest().
367*8b26181fSAndroid Build Coastguard Worker * It should be big enough to hold "*lenp" bytes of data; it
368*8b26181fSAndroid Build Coastguard Worker * will actually be slightly larger, as PACKET_OID_DATA has a
369*8b26181fSAndroid Build Coastguard Worker * 1-byte data array at the end, standing in for the variable-length
370*8b26181fSAndroid Build Coastguard Worker * data that's actually there.
371*8b26181fSAndroid Build Coastguard Worker */
372*8b26181fSAndroid Build Coastguard Worker oid_data_arg = malloc(sizeof (PACKET_OID_DATA) + *lenp);
373*8b26181fSAndroid Build Coastguard Worker if (oid_data_arg == NULL) {
374*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
375*8b26181fSAndroid Build Coastguard Worker "Couldn't allocate argument buffer for PacketRequest");
376*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
377*8b26181fSAndroid Build Coastguard Worker }
378*8b26181fSAndroid Build Coastguard Worker
379*8b26181fSAndroid Build Coastguard Worker oid_data_arg->Oid = oid;
380*8b26181fSAndroid Build Coastguard Worker oid_data_arg->Length = (ULONG)(*lenp); /* XXX - check for ridiculously large value? */
381*8b26181fSAndroid Build Coastguard Worker memcpy(oid_data_arg->Data, data, *lenp);
382*8b26181fSAndroid Build Coastguard Worker if (!PacketRequest(pw->adapter, TRUE, oid_data_arg)) {
383*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
384*8b26181fSAndroid Build Coastguard Worker GetLastError(), "Error calling PacketRequest");
385*8b26181fSAndroid Build Coastguard Worker free(oid_data_arg);
386*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
387*8b26181fSAndroid Build Coastguard Worker }
388*8b26181fSAndroid Build Coastguard Worker
389*8b26181fSAndroid Build Coastguard Worker /*
390*8b26181fSAndroid Build Coastguard Worker * Get the length actually copied.
391*8b26181fSAndroid Build Coastguard Worker */
392*8b26181fSAndroid Build Coastguard Worker *lenp = oid_data_arg->Length;
393*8b26181fSAndroid Build Coastguard Worker
394*8b26181fSAndroid Build Coastguard Worker /*
395*8b26181fSAndroid Build Coastguard Worker * No need to copy the data - we're doing a set.
396*8b26181fSAndroid Build Coastguard Worker */
397*8b26181fSAndroid Build Coastguard Worker free(oid_data_arg);
398*8b26181fSAndroid Build Coastguard Worker return (0);
399*8b26181fSAndroid Build Coastguard Worker }
400*8b26181fSAndroid Build Coastguard Worker
401*8b26181fSAndroid Build Coastguard Worker static u_int
pcap_sendqueue_transmit_npf(pcap_t * p,pcap_send_queue * queue,int sync)402*8b26181fSAndroid Build Coastguard Worker pcap_sendqueue_transmit_npf(pcap_t *p, pcap_send_queue *queue, int sync)
403*8b26181fSAndroid Build Coastguard Worker {
404*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
405*8b26181fSAndroid Build Coastguard Worker u_int res;
406*8b26181fSAndroid Build Coastguard Worker
407*8b26181fSAndroid Build Coastguard Worker res = PacketSendPackets(pw->adapter,
408*8b26181fSAndroid Build Coastguard Worker queue->buffer,
409*8b26181fSAndroid Build Coastguard Worker queue->len,
410*8b26181fSAndroid Build Coastguard Worker (BOOLEAN)sync);
411*8b26181fSAndroid Build Coastguard Worker
412*8b26181fSAndroid Build Coastguard Worker if(res != queue->len){
413*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
414*8b26181fSAndroid Build Coastguard Worker GetLastError(), "Error queueing packets");
415*8b26181fSAndroid Build Coastguard Worker }
416*8b26181fSAndroid Build Coastguard Worker
417*8b26181fSAndroid Build Coastguard Worker return (res);
418*8b26181fSAndroid Build Coastguard Worker }
419*8b26181fSAndroid Build Coastguard Worker
420*8b26181fSAndroid Build Coastguard Worker static int
pcap_setuserbuffer_npf(pcap_t * p,int size)421*8b26181fSAndroid Build Coastguard Worker pcap_setuserbuffer_npf(pcap_t *p, int size)
422*8b26181fSAndroid Build Coastguard Worker {
423*8b26181fSAndroid Build Coastguard Worker unsigned char *new_buff;
424*8b26181fSAndroid Build Coastguard Worker
425*8b26181fSAndroid Build Coastguard Worker if (size<=0) {
426*8b26181fSAndroid Build Coastguard Worker /* Bogus parameter */
427*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
428*8b26181fSAndroid Build Coastguard Worker "Error: invalid size %d",size);
429*8b26181fSAndroid Build Coastguard Worker return (-1);
430*8b26181fSAndroid Build Coastguard Worker }
431*8b26181fSAndroid Build Coastguard Worker
432*8b26181fSAndroid Build Coastguard Worker /* Allocate the buffer */
433*8b26181fSAndroid Build Coastguard Worker new_buff=(unsigned char*)malloc(sizeof(char)*size);
434*8b26181fSAndroid Build Coastguard Worker
435*8b26181fSAndroid Build Coastguard Worker if (!new_buff) {
436*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
437*8b26181fSAndroid Build Coastguard Worker "Error: not enough memory");
438*8b26181fSAndroid Build Coastguard Worker return (-1);
439*8b26181fSAndroid Build Coastguard Worker }
440*8b26181fSAndroid Build Coastguard Worker
441*8b26181fSAndroid Build Coastguard Worker free(p->buffer);
442*8b26181fSAndroid Build Coastguard Worker
443*8b26181fSAndroid Build Coastguard Worker p->buffer=new_buff;
444*8b26181fSAndroid Build Coastguard Worker p->bufsize=size;
445*8b26181fSAndroid Build Coastguard Worker
446*8b26181fSAndroid Build Coastguard Worker return (0);
447*8b26181fSAndroid Build Coastguard Worker }
448*8b26181fSAndroid Build Coastguard Worker
449*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_NPCAP_PACKET_API
450*8b26181fSAndroid Build Coastguard Worker /*
451*8b26181fSAndroid Build Coastguard Worker * Kernel dump mode isn't supported in Npcap; calls to PacketSetDumpName(),
452*8b26181fSAndroid Build Coastguard Worker * PacketSetDumpLimits(), and PacketIsDumpEnded() will get compile-time
453*8b26181fSAndroid Build Coastguard Worker * deprecation warnings.
454*8b26181fSAndroid Build Coastguard Worker *
455*8b26181fSAndroid Build Coastguard Worker * Avoid calling them; just return errors indicating that kernel dump
456*8b26181fSAndroid Build Coastguard Worker * mode isn't supported in Npcap.
457*8b26181fSAndroid Build Coastguard Worker */
458*8b26181fSAndroid Build Coastguard Worker static int
pcap_live_dump_npf(pcap_t * p,char * filename _U_,int maxsize _U_,int maxpacks _U_)459*8b26181fSAndroid Build Coastguard Worker pcap_live_dump_npf(pcap_t *p, char *filename _U_, int maxsize _U_,
460*8b26181fSAndroid Build Coastguard Worker int maxpacks _U_)
461*8b26181fSAndroid Build Coastguard Worker {
462*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
463*8b26181fSAndroid Build Coastguard Worker "Npcap doesn't support kernel dump mode");
464*8b26181fSAndroid Build Coastguard Worker return (-1);
465*8b26181fSAndroid Build Coastguard Worker }
466*8b26181fSAndroid Build Coastguard Worker static int
pcap_live_dump_ended_npf(pcap_t * p,int sync)467*8b26181fSAndroid Build Coastguard Worker pcap_live_dump_ended_npf(pcap_t *p, int sync)
468*8b26181fSAndroid Build Coastguard Worker {
469*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
470*8b26181fSAndroid Build Coastguard Worker "Npcap doesn't support kernel dump mode");
471*8b26181fSAndroid Build Coastguard Worker return (-1);
472*8b26181fSAndroid Build Coastguard Worker }
473*8b26181fSAndroid Build Coastguard Worker #else /* HAVE_NPCAP_PACKET_API */
474*8b26181fSAndroid Build Coastguard Worker static int
pcap_live_dump_npf(pcap_t * p,char * filename,int maxsize,int maxpacks)475*8b26181fSAndroid Build Coastguard Worker pcap_live_dump_npf(pcap_t *p, char *filename, int maxsize, int maxpacks)
476*8b26181fSAndroid Build Coastguard Worker {
477*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
478*8b26181fSAndroid Build Coastguard Worker BOOLEAN res;
479*8b26181fSAndroid Build Coastguard Worker
480*8b26181fSAndroid Build Coastguard Worker /* Set the packet driver in dump mode */
481*8b26181fSAndroid Build Coastguard Worker res = PacketSetMode(pw->adapter, PACKET_MODE_DUMP);
482*8b26181fSAndroid Build Coastguard Worker if(res == FALSE){
483*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
484*8b26181fSAndroid Build Coastguard Worker "Error setting dump mode");
485*8b26181fSAndroid Build Coastguard Worker return (-1);
486*8b26181fSAndroid Build Coastguard Worker }
487*8b26181fSAndroid Build Coastguard Worker
488*8b26181fSAndroid Build Coastguard Worker /* Set the name of the dump file */
489*8b26181fSAndroid Build Coastguard Worker res = PacketSetDumpName(pw->adapter, filename, (int)strlen(filename));
490*8b26181fSAndroid Build Coastguard Worker if(res == FALSE){
491*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
492*8b26181fSAndroid Build Coastguard Worker "Error setting kernel dump file name");
493*8b26181fSAndroid Build Coastguard Worker return (-1);
494*8b26181fSAndroid Build Coastguard Worker }
495*8b26181fSAndroid Build Coastguard Worker
496*8b26181fSAndroid Build Coastguard Worker /* Set the limits of the dump file */
497*8b26181fSAndroid Build Coastguard Worker res = PacketSetDumpLimits(pw->adapter, maxsize, maxpacks);
498*8b26181fSAndroid Build Coastguard Worker if(res == FALSE) {
499*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
500*8b26181fSAndroid Build Coastguard Worker "Error setting dump limit");
501*8b26181fSAndroid Build Coastguard Worker return (-1);
502*8b26181fSAndroid Build Coastguard Worker }
503*8b26181fSAndroid Build Coastguard Worker
504*8b26181fSAndroid Build Coastguard Worker return (0);
505*8b26181fSAndroid Build Coastguard Worker }
506*8b26181fSAndroid Build Coastguard Worker
507*8b26181fSAndroid Build Coastguard Worker static int
pcap_live_dump_ended_npf(pcap_t * p,int sync)508*8b26181fSAndroid Build Coastguard Worker pcap_live_dump_ended_npf(pcap_t *p, int sync)
509*8b26181fSAndroid Build Coastguard Worker {
510*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
511*8b26181fSAndroid Build Coastguard Worker
512*8b26181fSAndroid Build Coastguard Worker return (PacketIsDumpEnded(pw->adapter, (BOOLEAN)sync));
513*8b26181fSAndroid Build Coastguard Worker }
514*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_NPCAP_PACKET_API */
515*8b26181fSAndroid Build Coastguard Worker
516*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_AIRPCAP_API
517*8b26181fSAndroid Build Coastguard Worker static PAirpcapHandle
pcap_get_airpcap_handle_npf(pcap_t * p)518*8b26181fSAndroid Build Coastguard Worker pcap_get_airpcap_handle_npf(pcap_t *p)
519*8b26181fSAndroid Build Coastguard Worker {
520*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
521*8b26181fSAndroid Build Coastguard Worker
522*8b26181fSAndroid Build Coastguard Worker return (PacketGetAirPcapHandle(pw->adapter));
523*8b26181fSAndroid Build Coastguard Worker }
524*8b26181fSAndroid Build Coastguard Worker #else /* HAVE_AIRPCAP_API */
525*8b26181fSAndroid Build Coastguard Worker static PAirpcapHandle
pcap_get_airpcap_handle_npf(pcap_t * p _U_)526*8b26181fSAndroid Build Coastguard Worker pcap_get_airpcap_handle_npf(pcap_t *p _U_)
527*8b26181fSAndroid Build Coastguard Worker {
528*8b26181fSAndroid Build Coastguard Worker return (NULL);
529*8b26181fSAndroid Build Coastguard Worker }
530*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_AIRPCAP_API */
531*8b26181fSAndroid Build Coastguard Worker
532*8b26181fSAndroid Build Coastguard Worker static int
pcap_read_npf(pcap_t * p,int cnt,pcap_handler callback,u_char * user)533*8b26181fSAndroid Build Coastguard Worker pcap_read_npf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
534*8b26181fSAndroid Build Coastguard Worker {
535*8b26181fSAndroid Build Coastguard Worker PACKET Packet;
536*8b26181fSAndroid Build Coastguard Worker int cc;
537*8b26181fSAndroid Build Coastguard Worker int n;
538*8b26181fSAndroid Build Coastguard Worker register u_char *bp, *ep;
539*8b26181fSAndroid Build Coastguard Worker u_char *datap;
540*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
541*8b26181fSAndroid Build Coastguard Worker
542*8b26181fSAndroid Build Coastguard Worker cc = p->cc;
543*8b26181fSAndroid Build Coastguard Worker if (cc == 0) {
544*8b26181fSAndroid Build Coastguard Worker /*
545*8b26181fSAndroid Build Coastguard Worker * Has "pcap_breakloop()" been called?
546*8b26181fSAndroid Build Coastguard Worker */
547*8b26181fSAndroid Build Coastguard Worker if (p->break_loop) {
548*8b26181fSAndroid Build Coastguard Worker /*
549*8b26181fSAndroid Build Coastguard Worker * Yes - clear the flag that indicates that it
550*8b26181fSAndroid Build Coastguard Worker * has, and return PCAP_ERROR_BREAK to indicate
551*8b26181fSAndroid Build Coastguard Worker * that we were told to break out of the loop.
552*8b26181fSAndroid Build Coastguard Worker */
553*8b26181fSAndroid Build Coastguard Worker p->break_loop = 0;
554*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_BREAK);
555*8b26181fSAndroid Build Coastguard Worker }
556*8b26181fSAndroid Build Coastguard Worker
557*8b26181fSAndroid Build Coastguard Worker /*
558*8b26181fSAndroid Build Coastguard Worker * Capture the packets.
559*8b26181fSAndroid Build Coastguard Worker *
560*8b26181fSAndroid Build Coastguard Worker * The PACKET structure had a bunch of extra stuff for
561*8b26181fSAndroid Build Coastguard Worker * Windows 9x/Me, but the only interesting data in it
562*8b26181fSAndroid Build Coastguard Worker * in the versions of Windows that we support is just
563*8b26181fSAndroid Build Coastguard Worker * a copy of p->buffer, a copy of p->buflen, and the
564*8b26181fSAndroid Build Coastguard Worker * actual number of bytes read returned from
565*8b26181fSAndroid Build Coastguard Worker * PacketReceivePacket(), none of which has to be
566*8b26181fSAndroid Build Coastguard Worker * retained from call to call, so we just keep one on
567*8b26181fSAndroid Build Coastguard Worker * the stack.
568*8b26181fSAndroid Build Coastguard Worker */
569*8b26181fSAndroid Build Coastguard Worker PacketInitPacket(&Packet, (BYTE *)p->buffer, p->bufsize);
570*8b26181fSAndroid Build Coastguard Worker if (!PacketReceivePacket(pw->adapter, &Packet, TRUE)) {
571*8b26181fSAndroid Build Coastguard Worker /*
572*8b26181fSAndroid Build Coastguard Worker * Did the device go away?
573*8b26181fSAndroid Build Coastguard Worker * If so, the error we get can either be
574*8b26181fSAndroid Build Coastguard Worker * ERROR_GEN_FAILURE or ERROR_DEVICE_REMOVED.
575*8b26181fSAndroid Build Coastguard Worker */
576*8b26181fSAndroid Build Coastguard Worker DWORD errcode = GetLastError();
577*8b26181fSAndroid Build Coastguard Worker
578*8b26181fSAndroid Build Coastguard Worker if (errcode == ERROR_GEN_FAILURE ||
579*8b26181fSAndroid Build Coastguard Worker errcode == ERROR_DEVICE_REMOVED) {
580*8b26181fSAndroid Build Coastguard Worker /*
581*8b26181fSAndroid Build Coastguard Worker * The device on which we're capturing
582*8b26181fSAndroid Build Coastguard Worker * went away, or it became unusable
583*8b26181fSAndroid Build Coastguard Worker * by NPF due to a suspend/resume.
584*8b26181fSAndroid Build Coastguard Worker *
585*8b26181fSAndroid Build Coastguard Worker * ERROR_GEN_FAILURE comes from
586*8b26181fSAndroid Build Coastguard Worker * STATUS_UNSUCCESSFUL, as well as some
587*8b26181fSAndroid Build Coastguard Worker * other NT status codes that the Npcap
588*8b26181fSAndroid Build Coastguard Worker * driver is unlikely to return.
589*8b26181fSAndroid Build Coastguard Worker * XXX - hopefully no other error
590*8b26181fSAndroid Build Coastguard Worker * conditions are indicated by this.
591*8b26181fSAndroid Build Coastguard Worker *
592*8b26181fSAndroid Build Coastguard Worker * ERROR_DEVICE_REMOVED comes from
593*8b26181fSAndroid Build Coastguard Worker * STATUS_DEVICE_REMOVED.
594*8b26181fSAndroid Build Coastguard Worker *
595*8b26181fSAndroid Build Coastguard Worker * We report the Windows status code
596*8b26181fSAndroid Build Coastguard Worker * name and the corresponding NT status
597*8b26181fSAndroid Build Coastguard Worker * code name, for the benefit of attempts
598*8b26181fSAndroid Build Coastguard Worker * to debug cases where this error is
599*8b26181fSAndroid Build Coastguard Worker * reported when the device *wasn't*
600*8b26181fSAndroid Build Coastguard Worker * removed, either because it's not
601*8b26181fSAndroid Build Coastguard Worker * removable, it's removable but wasn't
602*8b26181fSAndroid Build Coastguard Worker * removed, or it's a device that doesn't
603*8b26181fSAndroid Build Coastguard Worker * correspond to a physical device.
604*8b26181fSAndroid Build Coastguard Worker *
605*8b26181fSAndroid Build Coastguard Worker * XXX - we really should return an
606*8b26181fSAndroid Build Coastguard Worker * appropriate error for that, but
607*8b26181fSAndroid Build Coastguard Worker * pcap_dispatch() etc. aren't
608*8b26181fSAndroid Build Coastguard Worker * documented as having error returns
609*8b26181fSAndroid Build Coastguard Worker * other than PCAP_ERROR or PCAP_ERROR_BREAK.
610*8b26181fSAndroid Build Coastguard Worker */
611*8b26181fSAndroid Build Coastguard Worker const char *errcode_msg;
612*8b26181fSAndroid Build Coastguard Worker
613*8b26181fSAndroid Build Coastguard Worker if (errcode == ERROR_GEN_FAILURE)
614*8b26181fSAndroid Build Coastguard Worker errcode_msg = "ERROR_GEN_FAILURE/STATUS_UNSUCCESSFUL";
615*8b26181fSAndroid Build Coastguard Worker else
616*8b26181fSAndroid Build Coastguard Worker errcode_msg = "ERROR_DEVICE_REMOVED/STATUS_DEVICE_REMOVED";
617*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
618*8b26181fSAndroid Build Coastguard Worker "The interface disappeared (error code %s)",
619*8b26181fSAndroid Build Coastguard Worker errcode_msg);
620*8b26181fSAndroid Build Coastguard Worker } else {
621*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf,
622*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, errcode,
623*8b26181fSAndroid Build Coastguard Worker "PacketReceivePacket error");
624*8b26181fSAndroid Build Coastguard Worker }
625*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
626*8b26181fSAndroid Build Coastguard Worker }
627*8b26181fSAndroid Build Coastguard Worker
628*8b26181fSAndroid Build Coastguard Worker cc = Packet.ulBytesReceived;
629*8b26181fSAndroid Build Coastguard Worker
630*8b26181fSAndroid Build Coastguard Worker bp = p->buffer;
631*8b26181fSAndroid Build Coastguard Worker }
632*8b26181fSAndroid Build Coastguard Worker else
633*8b26181fSAndroid Build Coastguard Worker bp = p->bp;
634*8b26181fSAndroid Build Coastguard Worker
635*8b26181fSAndroid Build Coastguard Worker /*
636*8b26181fSAndroid Build Coastguard Worker * Loop through each packet.
637*8b26181fSAndroid Build Coastguard Worker *
638*8b26181fSAndroid Build Coastguard Worker * This assumes that a single buffer of packets will have
639*8b26181fSAndroid Build Coastguard Worker * <= INT_MAX packets, so the packet count doesn't overflow.
640*8b26181fSAndroid Build Coastguard Worker */
641*8b26181fSAndroid Build Coastguard Worker #define bhp ((struct bpf_hdr *)bp)
642*8b26181fSAndroid Build Coastguard Worker n = 0;
643*8b26181fSAndroid Build Coastguard Worker ep = bp + cc;
644*8b26181fSAndroid Build Coastguard Worker for (;;) {
645*8b26181fSAndroid Build Coastguard Worker register u_int caplen, hdrlen;
646*8b26181fSAndroid Build Coastguard Worker
647*8b26181fSAndroid Build Coastguard Worker /*
648*8b26181fSAndroid Build Coastguard Worker * Has "pcap_breakloop()" been called?
649*8b26181fSAndroid Build Coastguard Worker * If so, return immediately - if we haven't read any
650*8b26181fSAndroid Build Coastguard Worker * packets, clear the flag and return PCAP_ERROR_BREAK
651*8b26181fSAndroid Build Coastguard Worker * to indicate that we were told to break out of the loop,
652*8b26181fSAndroid Build Coastguard Worker * otherwise leave the flag set, so that the *next* call
653*8b26181fSAndroid Build Coastguard Worker * will break out of the loop without having read any
654*8b26181fSAndroid Build Coastguard Worker * packets, and return the number of packets we've
655*8b26181fSAndroid Build Coastguard Worker * processed so far.
656*8b26181fSAndroid Build Coastguard Worker */
657*8b26181fSAndroid Build Coastguard Worker if (p->break_loop) {
658*8b26181fSAndroid Build Coastguard Worker if (n == 0) {
659*8b26181fSAndroid Build Coastguard Worker p->break_loop = 0;
660*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_BREAK);
661*8b26181fSAndroid Build Coastguard Worker } else {
662*8b26181fSAndroid Build Coastguard Worker p->bp = bp;
663*8b26181fSAndroid Build Coastguard Worker p->cc = (int) (ep - bp);
664*8b26181fSAndroid Build Coastguard Worker return (n);
665*8b26181fSAndroid Build Coastguard Worker }
666*8b26181fSAndroid Build Coastguard Worker }
667*8b26181fSAndroid Build Coastguard Worker if (bp >= ep)
668*8b26181fSAndroid Build Coastguard Worker break;
669*8b26181fSAndroid Build Coastguard Worker
670*8b26181fSAndroid Build Coastguard Worker caplen = bhp->bh_caplen;
671*8b26181fSAndroid Build Coastguard Worker hdrlen = bhp->bh_hdrlen;
672*8b26181fSAndroid Build Coastguard Worker datap = bp + hdrlen;
673*8b26181fSAndroid Build Coastguard Worker
674*8b26181fSAndroid Build Coastguard Worker /*
675*8b26181fSAndroid Build Coastguard Worker * Short-circuit evaluation: if using BPF filter
676*8b26181fSAndroid Build Coastguard Worker * in kernel, no need to do it now - we already know
677*8b26181fSAndroid Build Coastguard Worker * the packet passed the filter.
678*8b26181fSAndroid Build Coastguard Worker *
679*8b26181fSAndroid Build Coastguard Worker * XXX - pcap_filter() should always return TRUE if
680*8b26181fSAndroid Build Coastguard Worker * handed a null pointer for the program, but it might
681*8b26181fSAndroid Build Coastguard Worker * just try to "run" the filter, so we check here.
682*8b26181fSAndroid Build Coastguard Worker */
683*8b26181fSAndroid Build Coastguard Worker if (pw->filtering_in_kernel ||
684*8b26181fSAndroid Build Coastguard Worker p->fcode.bf_insns == NULL ||
685*8b26181fSAndroid Build Coastguard Worker pcap_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) {
686*8b26181fSAndroid Build Coastguard Worker #ifdef ENABLE_REMOTE
687*8b26181fSAndroid Build Coastguard Worker switch (p->rmt_samp.method) {
688*8b26181fSAndroid Build Coastguard Worker
689*8b26181fSAndroid Build Coastguard Worker case PCAP_SAMP_1_EVERY_N:
690*8b26181fSAndroid Build Coastguard Worker pw->samp_npkt = (pw->samp_npkt + 1) % p->rmt_samp.value;
691*8b26181fSAndroid Build Coastguard Worker
692*8b26181fSAndroid Build Coastguard Worker /* Discard all packets that are not '1 out of N' */
693*8b26181fSAndroid Build Coastguard Worker if (pw->samp_npkt != 0) {
694*8b26181fSAndroid Build Coastguard Worker bp += Packet_WORDALIGN(caplen + hdrlen);
695*8b26181fSAndroid Build Coastguard Worker continue;
696*8b26181fSAndroid Build Coastguard Worker }
697*8b26181fSAndroid Build Coastguard Worker break;
698*8b26181fSAndroid Build Coastguard Worker
699*8b26181fSAndroid Build Coastguard Worker case PCAP_SAMP_FIRST_AFTER_N_MS:
700*8b26181fSAndroid Build Coastguard Worker {
701*8b26181fSAndroid Build Coastguard Worker struct pcap_pkthdr *pkt_header = (struct pcap_pkthdr*) bp;
702*8b26181fSAndroid Build Coastguard Worker
703*8b26181fSAndroid Build Coastguard Worker /*
704*8b26181fSAndroid Build Coastguard Worker * Check if the timestamp of the arrived
705*8b26181fSAndroid Build Coastguard Worker * packet is smaller than our target time.
706*8b26181fSAndroid Build Coastguard Worker */
707*8b26181fSAndroid Build Coastguard Worker if (pkt_header->ts.tv_sec < pw->samp_time.tv_sec ||
708*8b26181fSAndroid Build Coastguard Worker (pkt_header->ts.tv_sec == pw->samp_time.tv_sec && pkt_header->ts.tv_usec < pw->samp_time.tv_usec)) {
709*8b26181fSAndroid Build Coastguard Worker bp += Packet_WORDALIGN(caplen + hdrlen);
710*8b26181fSAndroid Build Coastguard Worker continue;
711*8b26181fSAndroid Build Coastguard Worker }
712*8b26181fSAndroid Build Coastguard Worker
713*8b26181fSAndroid Build Coastguard Worker /*
714*8b26181fSAndroid Build Coastguard Worker * The arrived packet is suitable for being
715*8b26181fSAndroid Build Coastguard Worker * delivered to our caller, so let's update
716*8b26181fSAndroid Build Coastguard Worker * the target time.
717*8b26181fSAndroid Build Coastguard Worker */
718*8b26181fSAndroid Build Coastguard Worker pw->samp_time.tv_usec = pkt_header->ts.tv_usec + p->rmt_samp.value * 1000;
719*8b26181fSAndroid Build Coastguard Worker if (pw->samp_time.tv_usec > 1000000) {
720*8b26181fSAndroid Build Coastguard Worker pw->samp_time.tv_sec = pkt_header->ts.tv_sec + pw->samp_time.tv_usec / 1000000;
721*8b26181fSAndroid Build Coastguard Worker pw->samp_time.tv_usec = pw->samp_time.tv_usec % 1000000;
722*8b26181fSAndroid Build Coastguard Worker }
723*8b26181fSAndroid Build Coastguard Worker }
724*8b26181fSAndroid Build Coastguard Worker }
725*8b26181fSAndroid Build Coastguard Worker #endif /* ENABLE_REMOTE */
726*8b26181fSAndroid Build Coastguard Worker
727*8b26181fSAndroid Build Coastguard Worker /*
728*8b26181fSAndroid Build Coastguard Worker * XXX A bpf_hdr matches a pcap_pkthdr.
729*8b26181fSAndroid Build Coastguard Worker */
730*8b26181fSAndroid Build Coastguard Worker (*callback)(user, (struct pcap_pkthdr*)bp, datap);
731*8b26181fSAndroid Build Coastguard Worker bp += Packet_WORDALIGN(caplen + hdrlen);
732*8b26181fSAndroid Build Coastguard Worker if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
733*8b26181fSAndroid Build Coastguard Worker p->bp = bp;
734*8b26181fSAndroid Build Coastguard Worker p->cc = (int) (ep - bp);
735*8b26181fSAndroid Build Coastguard Worker return (n);
736*8b26181fSAndroid Build Coastguard Worker }
737*8b26181fSAndroid Build Coastguard Worker } else {
738*8b26181fSAndroid Build Coastguard Worker /*
739*8b26181fSAndroid Build Coastguard Worker * Skip this packet.
740*8b26181fSAndroid Build Coastguard Worker */
741*8b26181fSAndroid Build Coastguard Worker bp += Packet_WORDALIGN(caplen + hdrlen);
742*8b26181fSAndroid Build Coastguard Worker }
743*8b26181fSAndroid Build Coastguard Worker }
744*8b26181fSAndroid Build Coastguard Worker #undef bhp
745*8b26181fSAndroid Build Coastguard Worker p->cc = 0;
746*8b26181fSAndroid Build Coastguard Worker return (n);
747*8b26181fSAndroid Build Coastguard Worker }
748*8b26181fSAndroid Build Coastguard Worker
749*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DAG_API
750*8b26181fSAndroid Build Coastguard Worker static int
pcap_read_win32_dag(pcap_t * p,int cnt,pcap_handler callback,u_char * user)751*8b26181fSAndroid Build Coastguard Worker pcap_read_win32_dag(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
752*8b26181fSAndroid Build Coastguard Worker {
753*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
754*8b26181fSAndroid Build Coastguard Worker PACKET Packet;
755*8b26181fSAndroid Build Coastguard Worker u_char *dp = NULL;
756*8b26181fSAndroid Build Coastguard Worker int packet_len = 0, caplen = 0;
757*8b26181fSAndroid Build Coastguard Worker struct pcap_pkthdr pcap_header;
758*8b26181fSAndroid Build Coastguard Worker u_char *endofbuf;
759*8b26181fSAndroid Build Coastguard Worker int n = 0;
760*8b26181fSAndroid Build Coastguard Worker dag_record_t *header;
761*8b26181fSAndroid Build Coastguard Worker unsigned erf_record_len;
762*8b26181fSAndroid Build Coastguard Worker ULONGLONG ts;
763*8b26181fSAndroid Build Coastguard Worker int cc;
764*8b26181fSAndroid Build Coastguard Worker unsigned swt;
765*8b26181fSAndroid Build Coastguard Worker unsigned dfp = pw->adapter->DagFastProcess;
766*8b26181fSAndroid Build Coastguard Worker
767*8b26181fSAndroid Build Coastguard Worker cc = p->cc;
768*8b26181fSAndroid Build Coastguard Worker if (cc == 0) /* Get new packets only if we have processed all the ones of the previous read */
769*8b26181fSAndroid Build Coastguard Worker {
770*8b26181fSAndroid Build Coastguard Worker /*
771*8b26181fSAndroid Build Coastguard Worker * Get new packets from the network.
772*8b26181fSAndroid Build Coastguard Worker *
773*8b26181fSAndroid Build Coastguard Worker * The PACKET structure had a bunch of extra stuff for
774*8b26181fSAndroid Build Coastguard Worker * Windows 9x/Me, but the only interesting data in it
775*8b26181fSAndroid Build Coastguard Worker * in the versions of Windows that we support is just
776*8b26181fSAndroid Build Coastguard Worker * a copy of p->buffer, a copy of p->buflen, and the
777*8b26181fSAndroid Build Coastguard Worker * actual number of bytes read returned from
778*8b26181fSAndroid Build Coastguard Worker * PacketReceivePacket(), none of which has to be
779*8b26181fSAndroid Build Coastguard Worker * retained from call to call, so we just keep one on
780*8b26181fSAndroid Build Coastguard Worker * the stack.
781*8b26181fSAndroid Build Coastguard Worker */
782*8b26181fSAndroid Build Coastguard Worker PacketInitPacket(&Packet, (BYTE *)p->buffer, p->bufsize);
783*8b26181fSAndroid Build Coastguard Worker if (!PacketReceivePacket(pw->adapter, &Packet, TRUE)) {
784*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read error: PacketReceivePacket failed");
785*8b26181fSAndroid Build Coastguard Worker return (-1);
786*8b26181fSAndroid Build Coastguard Worker }
787*8b26181fSAndroid Build Coastguard Worker
788*8b26181fSAndroid Build Coastguard Worker cc = Packet.ulBytesReceived;
789*8b26181fSAndroid Build Coastguard Worker if(cc == 0)
790*8b26181fSAndroid Build Coastguard Worker /* The timeout has expired but we no packets arrived */
791*8b26181fSAndroid Build Coastguard Worker return (0);
792*8b26181fSAndroid Build Coastguard Worker header = (dag_record_t*)pw->adapter->DagBuffer;
793*8b26181fSAndroid Build Coastguard Worker }
794*8b26181fSAndroid Build Coastguard Worker else
795*8b26181fSAndroid Build Coastguard Worker header = (dag_record_t*)p->bp;
796*8b26181fSAndroid Build Coastguard Worker
797*8b26181fSAndroid Build Coastguard Worker endofbuf = (char*)header + cc;
798*8b26181fSAndroid Build Coastguard Worker
799*8b26181fSAndroid Build Coastguard Worker /*
800*8b26181fSAndroid Build Coastguard Worker * This can conceivably process more than INT_MAX packets,
801*8b26181fSAndroid Build Coastguard Worker * which would overflow the packet count, causing it either
802*8b26181fSAndroid Build Coastguard Worker * to look like a negative number, and thus cause us to
803*8b26181fSAndroid Build Coastguard Worker * return a value that looks like an error, or overflow
804*8b26181fSAndroid Build Coastguard Worker * back into positive territory, and thus cause us to
805*8b26181fSAndroid Build Coastguard Worker * return a too-low count.
806*8b26181fSAndroid Build Coastguard Worker *
807*8b26181fSAndroid Build Coastguard Worker * Therefore, if the packet count is unlimited, we clip
808*8b26181fSAndroid Build Coastguard Worker * it at INT_MAX; this routine is not expected to
809*8b26181fSAndroid Build Coastguard Worker * process packets indefinitely, so that's not an issue.
810*8b26181fSAndroid Build Coastguard Worker */
811*8b26181fSAndroid Build Coastguard Worker if (PACKET_COUNT_IS_UNLIMITED(cnt))
812*8b26181fSAndroid Build Coastguard Worker cnt = INT_MAX;
813*8b26181fSAndroid Build Coastguard Worker
814*8b26181fSAndroid Build Coastguard Worker /*
815*8b26181fSAndroid Build Coastguard Worker * Cycle through the packets
816*8b26181fSAndroid Build Coastguard Worker */
817*8b26181fSAndroid Build Coastguard Worker do
818*8b26181fSAndroid Build Coastguard Worker {
819*8b26181fSAndroid Build Coastguard Worker erf_record_len = SWAPS(header->rlen);
820*8b26181fSAndroid Build Coastguard Worker if((char*)header + erf_record_len > endofbuf)
821*8b26181fSAndroid Build Coastguard Worker break;
822*8b26181fSAndroid Build Coastguard Worker
823*8b26181fSAndroid Build Coastguard Worker /* Increase the number of captured packets */
824*8b26181fSAndroid Build Coastguard Worker p->stat.ps_recv++;
825*8b26181fSAndroid Build Coastguard Worker
826*8b26181fSAndroid Build Coastguard Worker /* Find the beginning of the packet */
827*8b26181fSAndroid Build Coastguard Worker dp = ((u_char *)header) + dag_record_size;
828*8b26181fSAndroid Build Coastguard Worker
829*8b26181fSAndroid Build Coastguard Worker /* Determine actual packet len */
830*8b26181fSAndroid Build Coastguard Worker switch(header->type)
831*8b26181fSAndroid Build Coastguard Worker {
832*8b26181fSAndroid Build Coastguard Worker case TYPE_ATM:
833*8b26181fSAndroid Build Coastguard Worker packet_len = ATM_SNAPLEN;
834*8b26181fSAndroid Build Coastguard Worker caplen = ATM_SNAPLEN;
835*8b26181fSAndroid Build Coastguard Worker dp += 4;
836*8b26181fSAndroid Build Coastguard Worker
837*8b26181fSAndroid Build Coastguard Worker break;
838*8b26181fSAndroid Build Coastguard Worker
839*8b26181fSAndroid Build Coastguard Worker case TYPE_ETH:
840*8b26181fSAndroid Build Coastguard Worker swt = SWAPS(header->wlen);
841*8b26181fSAndroid Build Coastguard Worker packet_len = swt - (pw->dag_fcs_bits);
842*8b26181fSAndroid Build Coastguard Worker caplen = erf_record_len - dag_record_size - 2;
843*8b26181fSAndroid Build Coastguard Worker if (caplen > packet_len)
844*8b26181fSAndroid Build Coastguard Worker {
845*8b26181fSAndroid Build Coastguard Worker caplen = packet_len;
846*8b26181fSAndroid Build Coastguard Worker }
847*8b26181fSAndroid Build Coastguard Worker dp += 2;
848*8b26181fSAndroid Build Coastguard Worker
849*8b26181fSAndroid Build Coastguard Worker break;
850*8b26181fSAndroid Build Coastguard Worker
851*8b26181fSAndroid Build Coastguard Worker case TYPE_HDLC_POS:
852*8b26181fSAndroid Build Coastguard Worker swt = SWAPS(header->wlen);
853*8b26181fSAndroid Build Coastguard Worker packet_len = swt - (pw->dag_fcs_bits);
854*8b26181fSAndroid Build Coastguard Worker caplen = erf_record_len - dag_record_size;
855*8b26181fSAndroid Build Coastguard Worker if (caplen > packet_len)
856*8b26181fSAndroid Build Coastguard Worker {
857*8b26181fSAndroid Build Coastguard Worker caplen = packet_len;
858*8b26181fSAndroid Build Coastguard Worker }
859*8b26181fSAndroid Build Coastguard Worker
860*8b26181fSAndroid Build Coastguard Worker break;
861*8b26181fSAndroid Build Coastguard Worker }
862*8b26181fSAndroid Build Coastguard Worker
863*8b26181fSAndroid Build Coastguard Worker if(caplen > p->snapshot)
864*8b26181fSAndroid Build Coastguard Worker caplen = p->snapshot;
865*8b26181fSAndroid Build Coastguard Worker
866*8b26181fSAndroid Build Coastguard Worker /*
867*8b26181fSAndroid Build Coastguard Worker * Has "pcap_breakloop()" been called?
868*8b26181fSAndroid Build Coastguard Worker * If so, return immediately - if we haven't read any
869*8b26181fSAndroid Build Coastguard Worker * packets, clear the flag and return -2 to indicate
870*8b26181fSAndroid Build Coastguard Worker * that we were told to break out of the loop, otherwise
871*8b26181fSAndroid Build Coastguard Worker * leave the flag set, so that the *next* call will break
872*8b26181fSAndroid Build Coastguard Worker * out of the loop without having read any packets, and
873*8b26181fSAndroid Build Coastguard Worker * return the number of packets we've processed so far.
874*8b26181fSAndroid Build Coastguard Worker */
875*8b26181fSAndroid Build Coastguard Worker if (p->break_loop)
876*8b26181fSAndroid Build Coastguard Worker {
877*8b26181fSAndroid Build Coastguard Worker if (n == 0)
878*8b26181fSAndroid Build Coastguard Worker {
879*8b26181fSAndroid Build Coastguard Worker p->break_loop = 0;
880*8b26181fSAndroid Build Coastguard Worker return (-2);
881*8b26181fSAndroid Build Coastguard Worker }
882*8b26181fSAndroid Build Coastguard Worker else
883*8b26181fSAndroid Build Coastguard Worker {
884*8b26181fSAndroid Build Coastguard Worker p->bp = (char*)header;
885*8b26181fSAndroid Build Coastguard Worker p->cc = endofbuf - (char*)header;
886*8b26181fSAndroid Build Coastguard Worker return (n);
887*8b26181fSAndroid Build Coastguard Worker }
888*8b26181fSAndroid Build Coastguard Worker }
889*8b26181fSAndroid Build Coastguard Worker
890*8b26181fSAndroid Build Coastguard Worker if(!dfp)
891*8b26181fSAndroid Build Coastguard Worker {
892*8b26181fSAndroid Build Coastguard Worker /* convert between timestamp formats */
893*8b26181fSAndroid Build Coastguard Worker ts = header->ts;
894*8b26181fSAndroid Build Coastguard Worker pcap_header.ts.tv_sec = (int)(ts >> 32);
895*8b26181fSAndroid Build Coastguard Worker ts = (ts & 0xffffffffi64) * 1000000;
896*8b26181fSAndroid Build Coastguard Worker ts += 0x80000000; /* rounding */
897*8b26181fSAndroid Build Coastguard Worker pcap_header.ts.tv_usec = (int)(ts >> 32);
898*8b26181fSAndroid Build Coastguard Worker if (pcap_header.ts.tv_usec >= 1000000) {
899*8b26181fSAndroid Build Coastguard Worker pcap_header.ts.tv_usec -= 1000000;
900*8b26181fSAndroid Build Coastguard Worker pcap_header.ts.tv_sec++;
901*8b26181fSAndroid Build Coastguard Worker }
902*8b26181fSAndroid Build Coastguard Worker }
903*8b26181fSAndroid Build Coastguard Worker
904*8b26181fSAndroid Build Coastguard Worker /* No underlying filtering system. We need to filter on our own */
905*8b26181fSAndroid Build Coastguard Worker if (p->fcode.bf_insns)
906*8b26181fSAndroid Build Coastguard Worker {
907*8b26181fSAndroid Build Coastguard Worker if (pcap_filter(p->fcode.bf_insns, dp, packet_len, caplen) == 0)
908*8b26181fSAndroid Build Coastguard Worker {
909*8b26181fSAndroid Build Coastguard Worker /* Move to next packet */
910*8b26181fSAndroid Build Coastguard Worker header = (dag_record_t*)((char*)header + erf_record_len);
911*8b26181fSAndroid Build Coastguard Worker continue;
912*8b26181fSAndroid Build Coastguard Worker }
913*8b26181fSAndroid Build Coastguard Worker }
914*8b26181fSAndroid Build Coastguard Worker
915*8b26181fSAndroid Build Coastguard Worker /* Fill the header for the user supplied callback function */
916*8b26181fSAndroid Build Coastguard Worker pcap_header.caplen = caplen;
917*8b26181fSAndroid Build Coastguard Worker pcap_header.len = packet_len;
918*8b26181fSAndroid Build Coastguard Worker
919*8b26181fSAndroid Build Coastguard Worker /* Call the callback function */
920*8b26181fSAndroid Build Coastguard Worker (*callback)(user, &pcap_header, dp);
921*8b26181fSAndroid Build Coastguard Worker
922*8b26181fSAndroid Build Coastguard Worker /* Move to next packet */
923*8b26181fSAndroid Build Coastguard Worker header = (dag_record_t*)((char*)header + erf_record_len);
924*8b26181fSAndroid Build Coastguard Worker
925*8b26181fSAndroid Build Coastguard Worker /* Stop if the number of packets requested by user has been reached*/
926*8b26181fSAndroid Build Coastguard Worker if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt))
927*8b26181fSAndroid Build Coastguard Worker {
928*8b26181fSAndroid Build Coastguard Worker p->bp = (char*)header;
929*8b26181fSAndroid Build Coastguard Worker p->cc = endofbuf - (char*)header;
930*8b26181fSAndroid Build Coastguard Worker return (n);
931*8b26181fSAndroid Build Coastguard Worker }
932*8b26181fSAndroid Build Coastguard Worker }
933*8b26181fSAndroid Build Coastguard Worker while((u_char*)header < endofbuf);
934*8b26181fSAndroid Build Coastguard Worker
935*8b26181fSAndroid Build Coastguard Worker return (1);
936*8b26181fSAndroid Build Coastguard Worker }
937*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_DAG_API */
938*8b26181fSAndroid Build Coastguard Worker
939*8b26181fSAndroid Build Coastguard Worker /* Send a packet to the network */
940*8b26181fSAndroid Build Coastguard Worker static int
pcap_inject_npf(pcap_t * p,const void * buf,int size)941*8b26181fSAndroid Build Coastguard Worker pcap_inject_npf(pcap_t *p, const void *buf, int size)
942*8b26181fSAndroid Build Coastguard Worker {
943*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
944*8b26181fSAndroid Build Coastguard Worker PACKET pkt;
945*8b26181fSAndroid Build Coastguard Worker
946*8b26181fSAndroid Build Coastguard Worker PacketInitPacket(&pkt, (PVOID)buf, size);
947*8b26181fSAndroid Build Coastguard Worker if(PacketSendPacket(pw->adapter,&pkt,TRUE) == FALSE) {
948*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
949*8b26181fSAndroid Build Coastguard Worker GetLastError(), "send error: PacketSendPacket failed");
950*8b26181fSAndroid Build Coastguard Worker return (-1);
951*8b26181fSAndroid Build Coastguard Worker }
952*8b26181fSAndroid Build Coastguard Worker
953*8b26181fSAndroid Build Coastguard Worker /*
954*8b26181fSAndroid Build Coastguard Worker * We assume it all got sent if "PacketSendPacket()" succeeded.
955*8b26181fSAndroid Build Coastguard Worker * "pcap_inject()" is expected to return the number of bytes
956*8b26181fSAndroid Build Coastguard Worker * sent.
957*8b26181fSAndroid Build Coastguard Worker */
958*8b26181fSAndroid Build Coastguard Worker return (size);
959*8b26181fSAndroid Build Coastguard Worker }
960*8b26181fSAndroid Build Coastguard Worker
961*8b26181fSAndroid Build Coastguard Worker static void
pcap_cleanup_npf(pcap_t * p)962*8b26181fSAndroid Build Coastguard Worker pcap_cleanup_npf(pcap_t *p)
963*8b26181fSAndroid Build Coastguard Worker {
964*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
965*8b26181fSAndroid Build Coastguard Worker
966*8b26181fSAndroid Build Coastguard Worker if (pw->adapter != NULL) {
967*8b26181fSAndroid Build Coastguard Worker PacketCloseAdapter(pw->adapter);
968*8b26181fSAndroid Build Coastguard Worker pw->adapter = NULL;
969*8b26181fSAndroid Build Coastguard Worker }
970*8b26181fSAndroid Build Coastguard Worker if (pw->rfmon_selfstart)
971*8b26181fSAndroid Build Coastguard Worker {
972*8b26181fSAndroid Build Coastguard Worker PacketSetMonitorMode(p->opt.device, 0);
973*8b26181fSAndroid Build Coastguard Worker }
974*8b26181fSAndroid Build Coastguard Worker pcap_cleanup_live_common(p);
975*8b26181fSAndroid Build Coastguard Worker }
976*8b26181fSAndroid Build Coastguard Worker
977*8b26181fSAndroid Build Coastguard Worker static void
pcap_breakloop_npf(pcap_t * p)978*8b26181fSAndroid Build Coastguard Worker pcap_breakloop_npf(pcap_t *p)
979*8b26181fSAndroid Build Coastguard Worker {
980*8b26181fSAndroid Build Coastguard Worker pcap_breakloop_common(p);
981*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
982*8b26181fSAndroid Build Coastguard Worker
983*8b26181fSAndroid Build Coastguard Worker /* XXX - what if this fails? */
984*8b26181fSAndroid Build Coastguard Worker SetEvent(PacketGetReadEvent(pw->adapter));
985*8b26181fSAndroid Build Coastguard Worker }
986*8b26181fSAndroid Build Coastguard Worker
987*8b26181fSAndroid Build Coastguard Worker /*
988*8b26181fSAndroid Build Coastguard Worker * These are NTSTATUS values:
989*8b26181fSAndroid Build Coastguard Worker *
990*8b26181fSAndroid Build Coastguard Worker * https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/87fba13e-bf06-450e-83b1-9241dc81e781
991*8b26181fSAndroid Build Coastguard Worker *
992*8b26181fSAndroid Build Coastguard Worker * with the "Customer" bit set. If a driver returns them, they are not
993*8b26181fSAndroid Build Coastguard Worker * mapped to Windows error values in userland; they're returned by
994*8b26181fSAndroid Build Coastguard Worker * GetLastError().
995*8b26181fSAndroid Build Coastguard Worker *
996*8b26181fSAndroid Build Coastguard Worker * Note that "driver" here includes the Npcap NPF driver, as various
997*8b26181fSAndroid Build Coastguard Worker * versions would take NT status values and set the "Customer" bit
998*8b26181fSAndroid Build Coastguard Worker * before returning the status code. The commit message for the
999*8b26181fSAndroid Build Coastguard Worker * change that started doing that is
1000*8b26181fSAndroid Build Coastguard Worker *
1001*8b26181fSAndroid Build Coastguard Worker * Returned a customer-defined NTSTATUS in OID requests to avoid
1002*8b26181fSAndroid Build Coastguard Worker * NTSTATUS-to-Win32 Error code translation.
1003*8b26181fSAndroid Build Coastguard Worker *
1004*8b26181fSAndroid Build Coastguard Worker * but I don't know why the goal was to avoid that translation.
1005*8b26181fSAndroid Build Coastguard Worker *
1006*8b26181fSAndroid Build Coastguard Worker * Attempting to set the hardware filter on a Microsoft Surface Pro's
1007*8b26181fSAndroid Build Coastguard Worker * Mobile Broadband Adapter returns an error that appears to be
1008*8b26181fSAndroid Build Coastguard Worker * NDIS_STATUS_NOT_SUPPORTED ORed with the "Customer" bit, so it's
1009*8b26181fSAndroid Build Coastguard Worker * probably indicating that it doesn't support that.
1010*8b26181fSAndroid Build Coastguard Worker *
1011*8b26181fSAndroid Build Coastguard Worker * It is likely that there are other devices which throw spurious errors,
1012*8b26181fSAndroid Build Coastguard Worker * at which point this will need refactoring to efficiently check against
1013*8b26181fSAndroid Build Coastguard Worker * a list, but for now we can just check this one value. Perhaps the
1014*8b26181fSAndroid Build Coastguard Worker * right way to do this is compare against various NDIS errors with
1015*8b26181fSAndroid Build Coastguard Worker * the "customer" bit ORed in.
1016*8b26181fSAndroid Build Coastguard Worker */
1017*8b26181fSAndroid Build Coastguard Worker #define NT_STATUS_CUSTOMER_DEFINED 0x20000000
1018*8b26181fSAndroid Build Coastguard Worker
1019*8b26181fSAndroid Build Coastguard Worker static int
pcap_activate_npf(pcap_t * p)1020*8b26181fSAndroid Build Coastguard Worker pcap_activate_npf(pcap_t *p)
1021*8b26181fSAndroid Build Coastguard Worker {
1022*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
1023*8b26181fSAndroid Build Coastguard Worker NetType type;
1024*8b26181fSAndroid Build Coastguard Worker int res;
1025*8b26181fSAndroid Build Coastguard Worker int status = 0;
1026*8b26181fSAndroid Build Coastguard Worker struct bpf_insn total_insn;
1027*8b26181fSAndroid Build Coastguard Worker struct bpf_program total_prog;
1028*8b26181fSAndroid Build Coastguard Worker
1029*8b26181fSAndroid Build Coastguard Worker if (p->opt.rfmon) {
1030*8b26181fSAndroid Build Coastguard Worker /*
1031*8b26181fSAndroid Build Coastguard Worker * Monitor mode is supported on Windows Vista and later.
1032*8b26181fSAndroid Build Coastguard Worker */
1033*8b26181fSAndroid Build Coastguard Worker if (PacketGetMonitorMode(p->opt.device) == 1)
1034*8b26181fSAndroid Build Coastguard Worker {
1035*8b26181fSAndroid Build Coastguard Worker pw->rfmon_selfstart = 0;
1036*8b26181fSAndroid Build Coastguard Worker }
1037*8b26181fSAndroid Build Coastguard Worker else
1038*8b26181fSAndroid Build Coastguard Worker {
1039*8b26181fSAndroid Build Coastguard Worker if ((res = PacketSetMonitorMode(p->opt.device, 1)) != 1)
1040*8b26181fSAndroid Build Coastguard Worker {
1041*8b26181fSAndroid Build Coastguard Worker pw->rfmon_selfstart = 0;
1042*8b26181fSAndroid Build Coastguard Worker // Monitor mode is not supported.
1043*8b26181fSAndroid Build Coastguard Worker if (res == 0)
1044*8b26181fSAndroid Build Coastguard Worker {
1045*8b26181fSAndroid Build Coastguard Worker return PCAP_ERROR_RFMON_NOTSUP;
1046*8b26181fSAndroid Build Coastguard Worker }
1047*8b26181fSAndroid Build Coastguard Worker else
1048*8b26181fSAndroid Build Coastguard Worker {
1049*8b26181fSAndroid Build Coastguard Worker return PCAP_ERROR;
1050*8b26181fSAndroid Build Coastguard Worker }
1051*8b26181fSAndroid Build Coastguard Worker }
1052*8b26181fSAndroid Build Coastguard Worker else
1053*8b26181fSAndroid Build Coastguard Worker {
1054*8b26181fSAndroid Build Coastguard Worker pw->rfmon_selfstart = 1;
1055*8b26181fSAndroid Build Coastguard Worker }
1056*8b26181fSAndroid Build Coastguard Worker }
1057*8b26181fSAndroid Build Coastguard Worker }
1058*8b26181fSAndroid Build Coastguard Worker
1059*8b26181fSAndroid Build Coastguard Worker /* Init Winsock if it hasn't already been initialized */
1060*8b26181fSAndroid Build Coastguard Worker pcap_wsockinit();
1061*8b26181fSAndroid Build Coastguard Worker
1062*8b26181fSAndroid Build Coastguard Worker pw->adapter = PacketOpenAdapter(p->opt.device);
1063*8b26181fSAndroid Build Coastguard Worker
1064*8b26181fSAndroid Build Coastguard Worker if (pw->adapter == NULL)
1065*8b26181fSAndroid Build Coastguard Worker {
1066*8b26181fSAndroid Build Coastguard Worker DWORD errcode = GetLastError();
1067*8b26181fSAndroid Build Coastguard Worker
1068*8b26181fSAndroid Build Coastguard Worker /*
1069*8b26181fSAndroid Build Coastguard Worker * What error did we get when trying to open the adapter?
1070*8b26181fSAndroid Build Coastguard Worker */
1071*8b26181fSAndroid Build Coastguard Worker switch (errcode) {
1072*8b26181fSAndroid Build Coastguard Worker
1073*8b26181fSAndroid Build Coastguard Worker case ERROR_BAD_UNIT:
1074*8b26181fSAndroid Build Coastguard Worker /*
1075*8b26181fSAndroid Build Coastguard Worker * There's no such device.
1076*8b26181fSAndroid Build Coastguard Worker * There's nothing to add, so clear the error
1077*8b26181fSAndroid Build Coastguard Worker * message.
1078*8b26181fSAndroid Build Coastguard Worker */
1079*8b26181fSAndroid Build Coastguard Worker p->errbuf[0] = '\0';
1080*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_NO_SUCH_DEVICE);
1081*8b26181fSAndroid Build Coastguard Worker
1082*8b26181fSAndroid Build Coastguard Worker case ERROR_ACCESS_DENIED:
1083*8b26181fSAndroid Build Coastguard Worker /*
1084*8b26181fSAndroid Build Coastguard Worker * There is, but we don't have permission to
1085*8b26181fSAndroid Build Coastguard Worker * use it.
1086*8b26181fSAndroid Build Coastguard Worker *
1087*8b26181fSAndroid Build Coastguard Worker * XXX - we currently get ERROR_BAD_UNIT if the
1088*8b26181fSAndroid Build Coastguard Worker * user says "no" to the UAC prompt.
1089*8b26181fSAndroid Build Coastguard Worker */
1090*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1091*8b26181fSAndroid Build Coastguard Worker "The helper program for \"Admin-only Mode\" must be allowed to make changes to your device");
1092*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_PERM_DENIED);
1093*8b26181fSAndroid Build Coastguard Worker
1094*8b26181fSAndroid Build Coastguard Worker default:
1095*8b26181fSAndroid Build Coastguard Worker /*
1096*8b26181fSAndroid Build Coastguard Worker * Unknown - report details.
1097*8b26181fSAndroid Build Coastguard Worker */
1098*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
1099*8b26181fSAndroid Build Coastguard Worker errcode, "Error opening adapter");
1100*8b26181fSAndroid Build Coastguard Worker if (pw->rfmon_selfstart)
1101*8b26181fSAndroid Build Coastguard Worker {
1102*8b26181fSAndroid Build Coastguard Worker PacketSetMonitorMode(p->opt.device, 0);
1103*8b26181fSAndroid Build Coastguard Worker }
1104*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1105*8b26181fSAndroid Build Coastguard Worker }
1106*8b26181fSAndroid Build Coastguard Worker }
1107*8b26181fSAndroid Build Coastguard Worker
1108*8b26181fSAndroid Build Coastguard Worker /*get network type*/
1109*8b26181fSAndroid Build Coastguard Worker if(PacketGetNetType (pw->adapter,&type) == FALSE)
1110*8b26181fSAndroid Build Coastguard Worker {
1111*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
1112*8b26181fSAndroid Build Coastguard Worker GetLastError(), "Cannot determine the network type");
1113*8b26181fSAndroid Build Coastguard Worker goto bad;
1114*8b26181fSAndroid Build Coastguard Worker }
1115*8b26181fSAndroid Build Coastguard Worker
1116*8b26181fSAndroid Build Coastguard Worker /*Set the linktype*/
1117*8b26181fSAndroid Build Coastguard Worker switch (type.LinkType)
1118*8b26181fSAndroid Build Coastguard Worker {
1119*8b26181fSAndroid Build Coastguard Worker /*
1120*8b26181fSAndroid Build Coastguard Worker * NDIS-defined medium types.
1121*8b26181fSAndroid Build Coastguard Worker */
1122*8b26181fSAndroid Build Coastguard Worker case NdisMedium802_3:
1123*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_EN10MB;
1124*8b26181fSAndroid Build Coastguard Worker /*
1125*8b26181fSAndroid Build Coastguard Worker * This is (presumably) a real Ethernet capture; give it a
1126*8b26181fSAndroid Build Coastguard Worker * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1127*8b26181fSAndroid Build Coastguard Worker * that an application can let you choose it, in case you're
1128*8b26181fSAndroid Build Coastguard Worker * capturing DOCSIS traffic that a Cisco Cable Modem
1129*8b26181fSAndroid Build Coastguard Worker * Termination System is putting out onto an Ethernet (it
1130*8b26181fSAndroid Build Coastguard Worker * doesn't put an Ethernet header onto the wire, it puts raw
1131*8b26181fSAndroid Build Coastguard Worker * DOCSIS frames out on the wire inside the low-level
1132*8b26181fSAndroid Build Coastguard Worker * Ethernet framing).
1133*8b26181fSAndroid Build Coastguard Worker */
1134*8b26181fSAndroid Build Coastguard Worker p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
1135*8b26181fSAndroid Build Coastguard Worker /*
1136*8b26181fSAndroid Build Coastguard Worker * If that fails, just leave the list empty.
1137*8b26181fSAndroid Build Coastguard Worker */
1138*8b26181fSAndroid Build Coastguard Worker if (p->dlt_list != NULL) {
1139*8b26181fSAndroid Build Coastguard Worker p->dlt_list[0] = DLT_EN10MB;
1140*8b26181fSAndroid Build Coastguard Worker p->dlt_list[1] = DLT_DOCSIS;
1141*8b26181fSAndroid Build Coastguard Worker p->dlt_count = 2;
1142*8b26181fSAndroid Build Coastguard Worker }
1143*8b26181fSAndroid Build Coastguard Worker break;
1144*8b26181fSAndroid Build Coastguard Worker
1145*8b26181fSAndroid Build Coastguard Worker case NdisMedium802_5:
1146*8b26181fSAndroid Build Coastguard Worker /*
1147*8b26181fSAndroid Build Coastguard Worker * Token Ring.
1148*8b26181fSAndroid Build Coastguard Worker */
1149*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_IEEE802;
1150*8b26181fSAndroid Build Coastguard Worker break;
1151*8b26181fSAndroid Build Coastguard Worker
1152*8b26181fSAndroid Build Coastguard Worker case NdisMediumFddi:
1153*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_FDDI;
1154*8b26181fSAndroid Build Coastguard Worker break;
1155*8b26181fSAndroid Build Coastguard Worker
1156*8b26181fSAndroid Build Coastguard Worker case NdisMediumWan:
1157*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_EN10MB;
1158*8b26181fSAndroid Build Coastguard Worker break;
1159*8b26181fSAndroid Build Coastguard Worker
1160*8b26181fSAndroid Build Coastguard Worker case NdisMediumArcnetRaw:
1161*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_ARCNET;
1162*8b26181fSAndroid Build Coastguard Worker break;
1163*8b26181fSAndroid Build Coastguard Worker
1164*8b26181fSAndroid Build Coastguard Worker case NdisMediumArcnet878_2:
1165*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_ARCNET;
1166*8b26181fSAndroid Build Coastguard Worker break;
1167*8b26181fSAndroid Build Coastguard Worker
1168*8b26181fSAndroid Build Coastguard Worker case NdisMediumAtm:
1169*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_ATM_RFC1483;
1170*8b26181fSAndroid Build Coastguard Worker break;
1171*8b26181fSAndroid Build Coastguard Worker
1172*8b26181fSAndroid Build Coastguard Worker case NdisMediumWirelessWan:
1173*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_RAW;
1174*8b26181fSAndroid Build Coastguard Worker break;
1175*8b26181fSAndroid Build Coastguard Worker
1176*8b26181fSAndroid Build Coastguard Worker case NdisMediumIP:
1177*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_RAW;
1178*8b26181fSAndroid Build Coastguard Worker break;
1179*8b26181fSAndroid Build Coastguard Worker
1180*8b26181fSAndroid Build Coastguard Worker /*
1181*8b26181fSAndroid Build Coastguard Worker * Npcap-defined medium types.
1182*8b26181fSAndroid Build Coastguard Worker */
1183*8b26181fSAndroid Build Coastguard Worker case NdisMediumNull:
1184*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_NULL;
1185*8b26181fSAndroid Build Coastguard Worker break;
1186*8b26181fSAndroid Build Coastguard Worker
1187*8b26181fSAndroid Build Coastguard Worker case NdisMediumCHDLC:
1188*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_CHDLC;
1189*8b26181fSAndroid Build Coastguard Worker break;
1190*8b26181fSAndroid Build Coastguard Worker
1191*8b26181fSAndroid Build Coastguard Worker case NdisMediumPPPSerial:
1192*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_PPP_SERIAL;
1193*8b26181fSAndroid Build Coastguard Worker break;
1194*8b26181fSAndroid Build Coastguard Worker
1195*8b26181fSAndroid Build Coastguard Worker case NdisMediumBare80211:
1196*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_IEEE802_11;
1197*8b26181fSAndroid Build Coastguard Worker break;
1198*8b26181fSAndroid Build Coastguard Worker
1199*8b26181fSAndroid Build Coastguard Worker case NdisMediumRadio80211:
1200*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_IEEE802_11_RADIO;
1201*8b26181fSAndroid Build Coastguard Worker break;
1202*8b26181fSAndroid Build Coastguard Worker
1203*8b26181fSAndroid Build Coastguard Worker case NdisMediumPpi:
1204*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_PPI;
1205*8b26181fSAndroid Build Coastguard Worker break;
1206*8b26181fSAndroid Build Coastguard Worker
1207*8b26181fSAndroid Build Coastguard Worker default:
1208*8b26181fSAndroid Build Coastguard Worker /*
1209*8b26181fSAndroid Build Coastguard Worker * An unknown medium type is assumed to supply Ethernet
1210*8b26181fSAndroid Build Coastguard Worker * headers; if not, the user will have to report it,
1211*8b26181fSAndroid Build Coastguard Worker * so that the medium type and link-layer header type
1212*8b26181fSAndroid Build Coastguard Worker * can be determined. If we were to fail here, we
1213*8b26181fSAndroid Build Coastguard Worker * might get the link-layer type in the error, but
1214*8b26181fSAndroid Build Coastguard Worker * the user wouldn't get a capture, so we wouldn't
1215*8b26181fSAndroid Build Coastguard Worker * be able to determine the link-layer type; we report
1216*8b26181fSAndroid Build Coastguard Worker * a warning with the link-layer type, so at least
1217*8b26181fSAndroid Build Coastguard Worker * some programs will report the warning.
1218*8b26181fSAndroid Build Coastguard Worker */
1219*8b26181fSAndroid Build Coastguard Worker p->linktype = DLT_EN10MB;
1220*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1221*8b26181fSAndroid Build Coastguard Worker "Unknown NdisMedium value %d, defaulting to DLT_EN10MB",
1222*8b26181fSAndroid Build Coastguard Worker type.LinkType);
1223*8b26181fSAndroid Build Coastguard Worker status = PCAP_WARNING;
1224*8b26181fSAndroid Build Coastguard Worker break;
1225*8b26181fSAndroid Build Coastguard Worker }
1226*8b26181fSAndroid Build Coastguard Worker
1227*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_PACKET_GET_TIMESTAMP_MODES
1228*8b26181fSAndroid Build Coastguard Worker /*
1229*8b26181fSAndroid Build Coastguard Worker * Set the timestamp type.
1230*8b26181fSAndroid Build Coastguard Worker * (Yes, we require PacketGetTimestampModes(), not just
1231*8b26181fSAndroid Build Coastguard Worker * PacketSetTimestampMode(). If we have the former, we
1232*8b26181fSAndroid Build Coastguard Worker * have the latter, unless somebody's using a version
1233*8b26181fSAndroid Build Coastguard Worker * of Npcap that they've hacked to provide the former
1234*8b26181fSAndroid Build Coastguard Worker * but not the latter; if they've done that, either
1235*8b26181fSAndroid Build Coastguard Worker * they're confused or they're trolling us.)
1236*8b26181fSAndroid Build Coastguard Worker */
1237*8b26181fSAndroid Build Coastguard Worker switch (p->opt.tstamp_type) {
1238*8b26181fSAndroid Build Coastguard Worker
1239*8b26181fSAndroid Build Coastguard Worker case PCAP_TSTAMP_HOST_HIPREC_UNSYNCED:
1240*8b26181fSAndroid Build Coastguard Worker /*
1241*8b26181fSAndroid Build Coastguard Worker * Better than low-res, but *not* synchronized with
1242*8b26181fSAndroid Build Coastguard Worker * the OS clock.
1243*8b26181fSAndroid Build Coastguard Worker */
1244*8b26181fSAndroid Build Coastguard Worker if (!PacketSetTimestampMode(pw->adapter, TIMESTAMPMODE_SINGLE_SYNCHRONIZATION))
1245*8b26181fSAndroid Build Coastguard Worker {
1246*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
1247*8b26181fSAndroid Build Coastguard Worker GetLastError(), "Cannot set the time stamp mode to TIMESTAMPMODE_SINGLE_SYNCHRONIZATION");
1248*8b26181fSAndroid Build Coastguard Worker goto bad;
1249*8b26181fSAndroid Build Coastguard Worker }
1250*8b26181fSAndroid Build Coastguard Worker break;
1251*8b26181fSAndroid Build Coastguard Worker
1252*8b26181fSAndroid Build Coastguard Worker case PCAP_TSTAMP_HOST_LOWPREC:
1253*8b26181fSAndroid Build Coastguard Worker /*
1254*8b26181fSAndroid Build Coastguard Worker * Low-res, but synchronized with the OS clock.
1255*8b26181fSAndroid Build Coastguard Worker */
1256*8b26181fSAndroid Build Coastguard Worker if (!PacketSetTimestampMode(pw->adapter, TIMESTAMPMODE_QUERYSYSTEMTIME))
1257*8b26181fSAndroid Build Coastguard Worker {
1258*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
1259*8b26181fSAndroid Build Coastguard Worker GetLastError(), "Cannot set the time stamp mode to TIMESTAMPMODE_QUERYSYSTEMTIME");
1260*8b26181fSAndroid Build Coastguard Worker goto bad;
1261*8b26181fSAndroid Build Coastguard Worker }
1262*8b26181fSAndroid Build Coastguard Worker break;
1263*8b26181fSAndroid Build Coastguard Worker
1264*8b26181fSAndroid Build Coastguard Worker case PCAP_TSTAMP_HOST_HIPREC:
1265*8b26181fSAndroid Build Coastguard Worker /*
1266*8b26181fSAndroid Build Coastguard Worker * High-res, and synchronized with the OS clock.
1267*8b26181fSAndroid Build Coastguard Worker */
1268*8b26181fSAndroid Build Coastguard Worker if (!PacketSetTimestampMode(pw->adapter, TIMESTAMPMODE_QUERYSYSTEMTIME_PRECISE))
1269*8b26181fSAndroid Build Coastguard Worker {
1270*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
1271*8b26181fSAndroid Build Coastguard Worker GetLastError(), "Cannot set the time stamp mode to TIMESTAMPMODE_QUERYSYSTEMTIME_PRECISE");
1272*8b26181fSAndroid Build Coastguard Worker goto bad;
1273*8b26181fSAndroid Build Coastguard Worker }
1274*8b26181fSAndroid Build Coastguard Worker break;
1275*8b26181fSAndroid Build Coastguard Worker
1276*8b26181fSAndroid Build Coastguard Worker case PCAP_TSTAMP_HOST:
1277*8b26181fSAndroid Build Coastguard Worker /*
1278*8b26181fSAndroid Build Coastguard Worker * XXX - do whatever the default is, for now.
1279*8b26181fSAndroid Build Coastguard Worker * Set to the highest resolution that's synchronized
1280*8b26181fSAndroid Build Coastguard Worker * with the system clock?
1281*8b26181fSAndroid Build Coastguard Worker */
1282*8b26181fSAndroid Build Coastguard Worker break;
1283*8b26181fSAndroid Build Coastguard Worker }
1284*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_PACKET_GET_TIMESTAMP_MODES */
1285*8b26181fSAndroid Build Coastguard Worker
1286*8b26181fSAndroid Build Coastguard Worker /*
1287*8b26181fSAndroid Build Coastguard Worker * Turn a negative snapshot value (invalid), a snapshot value of
1288*8b26181fSAndroid Build Coastguard Worker * 0 (unspecified), or a value bigger than the normal maximum
1289*8b26181fSAndroid Build Coastguard Worker * value, into the maximum allowed value.
1290*8b26181fSAndroid Build Coastguard Worker *
1291*8b26181fSAndroid Build Coastguard Worker * If some application really *needs* a bigger snapshot
1292*8b26181fSAndroid Build Coastguard Worker * length, we should just increase MAXIMUM_SNAPLEN.
1293*8b26181fSAndroid Build Coastguard Worker */
1294*8b26181fSAndroid Build Coastguard Worker if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
1295*8b26181fSAndroid Build Coastguard Worker p->snapshot = MAXIMUM_SNAPLEN;
1296*8b26181fSAndroid Build Coastguard Worker
1297*8b26181fSAndroid Build Coastguard Worker /* Set promiscuous mode */
1298*8b26181fSAndroid Build Coastguard Worker if (p->opt.promisc)
1299*8b26181fSAndroid Build Coastguard Worker {
1300*8b26181fSAndroid Build Coastguard Worker
1301*8b26181fSAndroid Build Coastguard Worker if (PacketSetHwFilter(pw->adapter,NDIS_PACKET_TYPE_PROMISCUOUS) == FALSE)
1302*8b26181fSAndroid Build Coastguard Worker {
1303*8b26181fSAndroid Build Coastguard Worker DWORD errcode = GetLastError();
1304*8b26181fSAndroid Build Coastguard Worker
1305*8b26181fSAndroid Build Coastguard Worker /*
1306*8b26181fSAndroid Build Coastguard Worker * Suppress spurious error generated by non-compiant
1307*8b26181fSAndroid Build Coastguard Worker * MS Surface mobile adapters that appear to
1308*8b26181fSAndroid Build Coastguard Worker * return NDIS_STATUS_NOT_SUPPORTED for attempts
1309*8b26181fSAndroid Build Coastguard Worker * to set the hardware filter.
1310*8b26181fSAndroid Build Coastguard Worker *
1311*8b26181fSAndroid Build Coastguard Worker * It appears to be reporting NDIS_STATUS_NOT_SUPPORTED,
1312*8b26181fSAndroid Build Coastguard Worker * but with the NT status value "Customer" bit set;
1313*8b26181fSAndroid Build Coastguard Worker * the Npcap NPF driver sets that bit in some cases.
1314*8b26181fSAndroid Build Coastguard Worker *
1315*8b26181fSAndroid Build Coastguard Worker * If we knew that this meant "promiscuous mode
1316*8b26181fSAndroid Build Coastguard Worker * isn't supported", we could add a "promiscuous
1317*8b26181fSAndroid Build Coastguard Worker * mode isn't supported" error code and return
1318*8b26181fSAndroid Build Coastguard Worker * that, but:
1319*8b26181fSAndroid Build Coastguard Worker *
1320*8b26181fSAndroid Build Coastguard Worker * 1) we don't know that it means that
1321*8b26181fSAndroid Build Coastguard Worker * rather than meaning "we reject attempts
1322*8b26181fSAndroid Build Coastguard Worker * to set the filter, even though the NDIS
1323*8b26181fSAndroid Build Coastguard Worker * specifications say you shouldn't do that"
1324*8b26181fSAndroid Build Coastguard Worker *
1325*8b26181fSAndroid Build Coastguard Worker * and
1326*8b26181fSAndroid Build Coastguard Worker *
1327*8b26181fSAndroid Build Coastguard Worker * 2) other interface types that don't
1328*8b26181fSAndroid Build Coastguard Worker * support promiscuous mode, at least
1329*8b26181fSAndroid Build Coastguard Worker * on UN*Xes, just silently ignore
1330*8b26181fSAndroid Build Coastguard Worker * attempts to set promiscuous mode
1331*8b26181fSAndroid Build Coastguard Worker *
1332*8b26181fSAndroid Build Coastguard Worker * and rejecting it with an error could disrupt
1333*8b26181fSAndroid Build Coastguard Worker * attempts to capture, as many programs (tcpdump,
1334*8b26181fSAndroid Build Coastguard Worker * *shark) default to promiscuous mode.
1335*8b26181fSAndroid Build Coastguard Worker *
1336*8b26181fSAndroid Build Coastguard Worker * Alternatively, we could return the "promiscuous
1337*8b26181fSAndroid Build Coastguard Worker * mode not supported" *warning* value, so that
1338*8b26181fSAndroid Build Coastguard Worker * correct code will either ignore it or report
1339*8b26181fSAndroid Build Coastguard Worker * it and continue capturing. (This may require
1340*8b26181fSAndroid Build Coastguard Worker * a pcap_init() flag to request that return
1341*8b26181fSAndroid Build Coastguard Worker * value, so that old incorrect programs that
1342*8b26181fSAndroid Build Coastguard Worker * assume a non-zero return from pcap_activate()
1343*8b26181fSAndroid Build Coastguard Worker * is an error don't break.)
1344*8b26181fSAndroid Build Coastguard Worker */
1345*8b26181fSAndroid Build Coastguard Worker if (errcode != (NDIS_STATUS_NOT_SUPPORTED|NT_STATUS_CUSTOMER_DEFINED))
1346*8b26181fSAndroid Build Coastguard Worker {
1347*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf,
1348*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, errcode,
1349*8b26181fSAndroid Build Coastguard Worker "failed to set hardware filter to promiscuous mode");
1350*8b26181fSAndroid Build Coastguard Worker goto bad;
1351*8b26181fSAndroid Build Coastguard Worker }
1352*8b26181fSAndroid Build Coastguard Worker }
1353*8b26181fSAndroid Build Coastguard Worker }
1354*8b26181fSAndroid Build Coastguard Worker else
1355*8b26181fSAndroid Build Coastguard Worker {
1356*8b26181fSAndroid Build Coastguard Worker /*
1357*8b26181fSAndroid Build Coastguard Worker * NDIS_PACKET_TYPE_ALL_LOCAL selects "All packets sent by
1358*8b26181fSAndroid Build Coastguard Worker * installed protocols and all packets indicated by the NIC",
1359*8b26181fSAndroid Build Coastguard Worker * but if no protocol drivers (like TCP/IP) are installed,
1360*8b26181fSAndroid Build Coastguard Worker * NDIS_PACKET_TYPE_DIRECTED, NDIS_PACKET_TYPE_BROADCAST,
1361*8b26181fSAndroid Build Coastguard Worker * and NDIS_PACKET_TYPE_MULTICAST are needed to capture
1362*8b26181fSAndroid Build Coastguard Worker * incoming frames.
1363*8b26181fSAndroid Build Coastguard Worker */
1364*8b26181fSAndroid Build Coastguard Worker if (PacketSetHwFilter(pw->adapter,
1365*8b26181fSAndroid Build Coastguard Worker NDIS_PACKET_TYPE_ALL_LOCAL |
1366*8b26181fSAndroid Build Coastguard Worker NDIS_PACKET_TYPE_DIRECTED |
1367*8b26181fSAndroid Build Coastguard Worker NDIS_PACKET_TYPE_BROADCAST |
1368*8b26181fSAndroid Build Coastguard Worker NDIS_PACKET_TYPE_MULTICAST) == FALSE)
1369*8b26181fSAndroid Build Coastguard Worker {
1370*8b26181fSAndroid Build Coastguard Worker DWORD errcode = GetLastError();
1371*8b26181fSAndroid Build Coastguard Worker
1372*8b26181fSAndroid Build Coastguard Worker /*
1373*8b26181fSAndroid Build Coastguard Worker * Suppress spurious error generated by non-compiant
1374*8b26181fSAndroid Build Coastguard Worker * MS Surface mobile adapters.
1375*8b26181fSAndroid Build Coastguard Worker */
1376*8b26181fSAndroid Build Coastguard Worker if (errcode != (NDIS_STATUS_NOT_SUPPORTED|NT_STATUS_CUSTOMER_DEFINED))
1377*8b26181fSAndroid Build Coastguard Worker {
1378*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf,
1379*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, errcode,
1380*8b26181fSAndroid Build Coastguard Worker "failed to set hardware filter to non-promiscuous mode");
1381*8b26181fSAndroid Build Coastguard Worker goto bad;
1382*8b26181fSAndroid Build Coastguard Worker }
1383*8b26181fSAndroid Build Coastguard Worker }
1384*8b26181fSAndroid Build Coastguard Worker }
1385*8b26181fSAndroid Build Coastguard Worker
1386*8b26181fSAndroid Build Coastguard Worker /* Set the buffer size */
1387*8b26181fSAndroid Build Coastguard Worker p->bufsize = WIN32_DEFAULT_USER_BUFFER_SIZE;
1388*8b26181fSAndroid Build Coastguard Worker
1389*8b26181fSAndroid Build Coastguard Worker if(!(pw->adapter->Flags & INFO_FLAG_DAG_CARD))
1390*8b26181fSAndroid Build Coastguard Worker {
1391*8b26181fSAndroid Build Coastguard Worker /*
1392*8b26181fSAndroid Build Coastguard Worker * Traditional Adapter
1393*8b26181fSAndroid Build Coastguard Worker */
1394*8b26181fSAndroid Build Coastguard Worker /*
1395*8b26181fSAndroid Build Coastguard Worker * If the buffer size wasn't explicitly set, default to
1396*8b26181fSAndroid Build Coastguard Worker * WIN32_DEFAULT_KERNEL_BUFFER_SIZE.
1397*8b26181fSAndroid Build Coastguard Worker */
1398*8b26181fSAndroid Build Coastguard Worker if (p->opt.buffer_size == 0)
1399*8b26181fSAndroid Build Coastguard Worker p->opt.buffer_size = WIN32_DEFAULT_KERNEL_BUFFER_SIZE;
1400*8b26181fSAndroid Build Coastguard Worker
1401*8b26181fSAndroid Build Coastguard Worker if(PacketSetBuff(pw->adapter,p->opt.buffer_size)==FALSE)
1402*8b26181fSAndroid Build Coastguard Worker {
1403*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "driver error: not enough memory to allocate the kernel buffer");
1404*8b26181fSAndroid Build Coastguard Worker goto bad;
1405*8b26181fSAndroid Build Coastguard Worker }
1406*8b26181fSAndroid Build Coastguard Worker
1407*8b26181fSAndroid Build Coastguard Worker p->buffer = malloc(p->bufsize);
1408*8b26181fSAndroid Build Coastguard Worker if (p->buffer == NULL)
1409*8b26181fSAndroid Build Coastguard Worker {
1410*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1411*8b26181fSAndroid Build Coastguard Worker errno, "malloc");
1412*8b26181fSAndroid Build Coastguard Worker goto bad;
1413*8b26181fSAndroid Build Coastguard Worker }
1414*8b26181fSAndroid Build Coastguard Worker
1415*8b26181fSAndroid Build Coastguard Worker if (p->opt.immediate)
1416*8b26181fSAndroid Build Coastguard Worker {
1417*8b26181fSAndroid Build Coastguard Worker /* tell the driver to copy the buffer as soon as data arrives */
1418*8b26181fSAndroid Build Coastguard Worker if(PacketSetMinToCopy(pw->adapter,0)==FALSE)
1419*8b26181fSAndroid Build Coastguard Worker {
1420*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf,
1421*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, GetLastError(),
1422*8b26181fSAndroid Build Coastguard Worker "Error calling PacketSetMinToCopy");
1423*8b26181fSAndroid Build Coastguard Worker goto bad;
1424*8b26181fSAndroid Build Coastguard Worker }
1425*8b26181fSAndroid Build Coastguard Worker }
1426*8b26181fSAndroid Build Coastguard Worker else
1427*8b26181fSAndroid Build Coastguard Worker {
1428*8b26181fSAndroid Build Coastguard Worker /* tell the driver to copy the buffer only if it contains at least 16K */
1429*8b26181fSAndroid Build Coastguard Worker if(PacketSetMinToCopy(pw->adapter,16000)==FALSE)
1430*8b26181fSAndroid Build Coastguard Worker {
1431*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf,
1432*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, GetLastError(),
1433*8b26181fSAndroid Build Coastguard Worker "Error calling PacketSetMinToCopy");
1434*8b26181fSAndroid Build Coastguard Worker goto bad;
1435*8b26181fSAndroid Build Coastguard Worker }
1436*8b26181fSAndroid Build Coastguard Worker }
1437*8b26181fSAndroid Build Coastguard Worker } else {
1438*8b26181fSAndroid Build Coastguard Worker /*
1439*8b26181fSAndroid Build Coastguard Worker * Dag Card
1440*8b26181fSAndroid Build Coastguard Worker */
1441*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DAG_API
1442*8b26181fSAndroid Build Coastguard Worker /*
1443*8b26181fSAndroid Build Coastguard Worker * We have DAG support.
1444*8b26181fSAndroid Build Coastguard Worker */
1445*8b26181fSAndroid Build Coastguard Worker LONG status;
1446*8b26181fSAndroid Build Coastguard Worker HKEY dagkey;
1447*8b26181fSAndroid Build Coastguard Worker DWORD lptype;
1448*8b26181fSAndroid Build Coastguard Worker DWORD lpcbdata;
1449*8b26181fSAndroid Build Coastguard Worker int postype = 0;
1450*8b26181fSAndroid Build Coastguard Worker char keyname[512];
1451*8b26181fSAndroid Build Coastguard Worker
1452*8b26181fSAndroid Build Coastguard Worker snprintf(keyname, sizeof(keyname), "%s\\CardParams\\%s",
1453*8b26181fSAndroid Build Coastguard Worker "SYSTEM\\CurrentControlSet\\Services\\DAG",
1454*8b26181fSAndroid Build Coastguard Worker strstr(_strlwr(p->opt.device), "dag"));
1455*8b26181fSAndroid Build Coastguard Worker do
1456*8b26181fSAndroid Build Coastguard Worker {
1457*8b26181fSAndroid Build Coastguard Worker status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &dagkey);
1458*8b26181fSAndroid Build Coastguard Worker if(status != ERROR_SUCCESS)
1459*8b26181fSAndroid Build Coastguard Worker break;
1460*8b26181fSAndroid Build Coastguard Worker
1461*8b26181fSAndroid Build Coastguard Worker status = RegQueryValueEx(dagkey,
1462*8b26181fSAndroid Build Coastguard Worker "PosType",
1463*8b26181fSAndroid Build Coastguard Worker NULL,
1464*8b26181fSAndroid Build Coastguard Worker &lptype,
1465*8b26181fSAndroid Build Coastguard Worker (char*)&postype,
1466*8b26181fSAndroid Build Coastguard Worker &lpcbdata);
1467*8b26181fSAndroid Build Coastguard Worker
1468*8b26181fSAndroid Build Coastguard Worker if(status != ERROR_SUCCESS)
1469*8b26181fSAndroid Build Coastguard Worker {
1470*8b26181fSAndroid Build Coastguard Worker postype = 0;
1471*8b26181fSAndroid Build Coastguard Worker }
1472*8b26181fSAndroid Build Coastguard Worker
1473*8b26181fSAndroid Build Coastguard Worker RegCloseKey(dagkey);
1474*8b26181fSAndroid Build Coastguard Worker }
1475*8b26181fSAndroid Build Coastguard Worker while(FALSE);
1476*8b26181fSAndroid Build Coastguard Worker
1477*8b26181fSAndroid Build Coastguard Worker
1478*8b26181fSAndroid Build Coastguard Worker p->snapshot = PacketSetSnapLen(pw->adapter, p->snapshot);
1479*8b26181fSAndroid Build Coastguard Worker
1480*8b26181fSAndroid Build Coastguard Worker /* Set the length of the FCS associated to any packet. This value
1481*8b26181fSAndroid Build Coastguard Worker * will be subtracted to the packet length */
1482*8b26181fSAndroid Build Coastguard Worker pw->dag_fcs_bits = pw->adapter->DagFcsLen;
1483*8b26181fSAndroid Build Coastguard Worker #else /* HAVE_DAG_API */
1484*8b26181fSAndroid Build Coastguard Worker /*
1485*8b26181fSAndroid Build Coastguard Worker * No DAG support.
1486*8b26181fSAndroid Build Coastguard Worker */
1487*8b26181fSAndroid Build Coastguard Worker goto bad;
1488*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_DAG_API */
1489*8b26181fSAndroid Build Coastguard Worker }
1490*8b26181fSAndroid Build Coastguard Worker
1491*8b26181fSAndroid Build Coastguard Worker /*
1492*8b26181fSAndroid Build Coastguard Worker * If there's no filter program installed, there's
1493*8b26181fSAndroid Build Coastguard Worker * no indication to the kernel of what the snapshot
1494*8b26181fSAndroid Build Coastguard Worker * length should be, so no snapshotting is done.
1495*8b26181fSAndroid Build Coastguard Worker *
1496*8b26181fSAndroid Build Coastguard Worker * Therefore, when we open the device, we install
1497*8b26181fSAndroid Build Coastguard Worker * an "accept everything" filter with the specified
1498*8b26181fSAndroid Build Coastguard Worker * snapshot length.
1499*8b26181fSAndroid Build Coastguard Worker */
1500*8b26181fSAndroid Build Coastguard Worker total_insn.code = (u_short)(BPF_RET | BPF_K);
1501*8b26181fSAndroid Build Coastguard Worker total_insn.jt = 0;
1502*8b26181fSAndroid Build Coastguard Worker total_insn.jf = 0;
1503*8b26181fSAndroid Build Coastguard Worker total_insn.k = p->snapshot;
1504*8b26181fSAndroid Build Coastguard Worker
1505*8b26181fSAndroid Build Coastguard Worker total_prog.bf_len = 1;
1506*8b26181fSAndroid Build Coastguard Worker total_prog.bf_insns = &total_insn;
1507*8b26181fSAndroid Build Coastguard Worker if (!PacketSetBpf(pw->adapter, &total_prog)) {
1508*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
1509*8b26181fSAndroid Build Coastguard Worker GetLastError(), "PacketSetBpf");
1510*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
1511*8b26181fSAndroid Build Coastguard Worker goto bad;
1512*8b26181fSAndroid Build Coastguard Worker }
1513*8b26181fSAndroid Build Coastguard Worker
1514*8b26181fSAndroid Build Coastguard Worker PacketSetReadTimeout(pw->adapter, p->opt.timeout);
1515*8b26181fSAndroid Build Coastguard Worker
1516*8b26181fSAndroid Build Coastguard Worker /* disable loopback capture if requested */
1517*8b26181fSAndroid Build Coastguard Worker if (p->opt.nocapture_local)
1518*8b26181fSAndroid Build Coastguard Worker {
1519*8b26181fSAndroid Build Coastguard Worker if (!PacketSetLoopbackBehavior(pw->adapter, NPF_DISABLE_LOOPBACK))
1520*8b26181fSAndroid Build Coastguard Worker {
1521*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1522*8b26181fSAndroid Build Coastguard Worker "Unable to disable the capture of loopback packets.");
1523*8b26181fSAndroid Build Coastguard Worker goto bad;
1524*8b26181fSAndroid Build Coastguard Worker }
1525*8b26181fSAndroid Build Coastguard Worker }
1526*8b26181fSAndroid Build Coastguard Worker
1527*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DAG_API
1528*8b26181fSAndroid Build Coastguard Worker if(pw->adapter->Flags & INFO_FLAG_DAG_CARD)
1529*8b26181fSAndroid Build Coastguard Worker {
1530*8b26181fSAndroid Build Coastguard Worker /* install dag specific handlers for read and setfilter */
1531*8b26181fSAndroid Build Coastguard Worker p->read_op = pcap_read_win32_dag;
1532*8b26181fSAndroid Build Coastguard Worker p->setfilter_op = pcap_setfilter_win32_dag;
1533*8b26181fSAndroid Build Coastguard Worker }
1534*8b26181fSAndroid Build Coastguard Worker else
1535*8b26181fSAndroid Build Coastguard Worker {
1536*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_DAG_API */
1537*8b26181fSAndroid Build Coastguard Worker /* install traditional npf handlers for read and setfilter */
1538*8b26181fSAndroid Build Coastguard Worker p->read_op = pcap_read_npf;
1539*8b26181fSAndroid Build Coastguard Worker p->setfilter_op = pcap_setfilter_npf;
1540*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DAG_API
1541*8b26181fSAndroid Build Coastguard Worker }
1542*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_DAG_API */
1543*8b26181fSAndroid Build Coastguard Worker p->setdirection_op = NULL; /* Not implemented. */
1544*8b26181fSAndroid Build Coastguard Worker /* XXX - can this be implemented on some versions of Windows? */
1545*8b26181fSAndroid Build Coastguard Worker p->inject_op = pcap_inject_npf;
1546*8b26181fSAndroid Build Coastguard Worker p->set_datalink_op = NULL; /* can't change data link type */
1547*8b26181fSAndroid Build Coastguard Worker p->getnonblock_op = pcap_getnonblock_npf;
1548*8b26181fSAndroid Build Coastguard Worker p->setnonblock_op = pcap_setnonblock_npf;
1549*8b26181fSAndroid Build Coastguard Worker p->stats_op = pcap_stats_npf;
1550*8b26181fSAndroid Build Coastguard Worker p->breakloop_op = pcap_breakloop_npf;
1551*8b26181fSAndroid Build Coastguard Worker p->stats_ex_op = pcap_stats_ex_npf;
1552*8b26181fSAndroid Build Coastguard Worker p->setbuff_op = pcap_setbuff_npf;
1553*8b26181fSAndroid Build Coastguard Worker p->setmode_op = pcap_setmode_npf;
1554*8b26181fSAndroid Build Coastguard Worker p->setmintocopy_op = pcap_setmintocopy_npf;
1555*8b26181fSAndroid Build Coastguard Worker p->getevent_op = pcap_getevent_npf;
1556*8b26181fSAndroid Build Coastguard Worker p->oid_get_request_op = pcap_oid_get_request_npf;
1557*8b26181fSAndroid Build Coastguard Worker p->oid_set_request_op = pcap_oid_set_request_npf;
1558*8b26181fSAndroid Build Coastguard Worker p->sendqueue_transmit_op = pcap_sendqueue_transmit_npf;
1559*8b26181fSAndroid Build Coastguard Worker p->setuserbuffer_op = pcap_setuserbuffer_npf;
1560*8b26181fSAndroid Build Coastguard Worker p->live_dump_op = pcap_live_dump_npf;
1561*8b26181fSAndroid Build Coastguard Worker p->live_dump_ended_op = pcap_live_dump_ended_npf;
1562*8b26181fSAndroid Build Coastguard Worker p->get_airpcap_handle_op = pcap_get_airpcap_handle_npf;
1563*8b26181fSAndroid Build Coastguard Worker p->cleanup_op = pcap_cleanup_npf;
1564*8b26181fSAndroid Build Coastguard Worker
1565*8b26181fSAndroid Build Coastguard Worker /*
1566*8b26181fSAndroid Build Coastguard Worker * XXX - this is only done because WinPcap supported
1567*8b26181fSAndroid Build Coastguard Worker * pcap_fileno() returning the hFile HANDLE from the
1568*8b26181fSAndroid Build Coastguard Worker * ADAPTER structure. We make no general guarantees
1569*8b26181fSAndroid Build Coastguard Worker * that the caller can do anything useful with it.
1570*8b26181fSAndroid Build Coastguard Worker *
1571*8b26181fSAndroid Build Coastguard Worker * (Not that we make any general guarantee of that
1572*8b26181fSAndroid Build Coastguard Worker * sort on UN*X, either, any more, given that not
1573*8b26181fSAndroid Build Coastguard Worker * all capture devices are regular OS network
1574*8b26181fSAndroid Build Coastguard Worker * interfaces.)
1575*8b26181fSAndroid Build Coastguard Worker */
1576*8b26181fSAndroid Build Coastguard Worker p->handle = pw->adapter->hFile;
1577*8b26181fSAndroid Build Coastguard Worker
1578*8b26181fSAndroid Build Coastguard Worker return (status);
1579*8b26181fSAndroid Build Coastguard Worker bad:
1580*8b26181fSAndroid Build Coastguard Worker pcap_cleanup_npf(p);
1581*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1582*8b26181fSAndroid Build Coastguard Worker }
1583*8b26181fSAndroid Build Coastguard Worker
1584*8b26181fSAndroid Build Coastguard Worker /*
1585*8b26181fSAndroid Build Coastguard Worker * Check if rfmon mode is supported on the pcap_t for Windows systems.
1586*8b26181fSAndroid Build Coastguard Worker */
1587*8b26181fSAndroid Build Coastguard Worker static int
pcap_can_set_rfmon_npf(pcap_t * p)1588*8b26181fSAndroid Build Coastguard Worker pcap_can_set_rfmon_npf(pcap_t *p)
1589*8b26181fSAndroid Build Coastguard Worker {
1590*8b26181fSAndroid Build Coastguard Worker return (PacketIsMonitorModeSupported(p->opt.device) == 1);
1591*8b26181fSAndroid Build Coastguard Worker }
1592*8b26181fSAndroid Build Coastguard Worker
1593*8b26181fSAndroid Build Coastguard Worker /*
1594*8b26181fSAndroid Build Coastguard Worker * Get a list of time stamp types.
1595*8b26181fSAndroid Build Coastguard Worker */
1596*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_PACKET_GET_TIMESTAMP_MODES
1597*8b26181fSAndroid Build Coastguard Worker static int
get_ts_types(const char * device,pcap_t * p,char * ebuf)1598*8b26181fSAndroid Build Coastguard Worker get_ts_types(const char *device, pcap_t *p, char *ebuf)
1599*8b26181fSAndroid Build Coastguard Worker {
1600*8b26181fSAndroid Build Coastguard Worker char *device_copy = NULL;
1601*8b26181fSAndroid Build Coastguard Worker ADAPTER *adapter = NULL;
1602*8b26181fSAndroid Build Coastguard Worker ULONG num_ts_modes;
1603*8b26181fSAndroid Build Coastguard Worker BOOL ret;
1604*8b26181fSAndroid Build Coastguard Worker DWORD error = ERROR_SUCCESS;
1605*8b26181fSAndroid Build Coastguard Worker ULONG *modes = NULL;
1606*8b26181fSAndroid Build Coastguard Worker int status = 0;
1607*8b26181fSAndroid Build Coastguard Worker
1608*8b26181fSAndroid Build Coastguard Worker do {
1609*8b26181fSAndroid Build Coastguard Worker /*
1610*8b26181fSAndroid Build Coastguard Worker * First, find out how many time stamp modes we have.
1611*8b26181fSAndroid Build Coastguard Worker * To do that, we have to open the adapter.
1612*8b26181fSAndroid Build Coastguard Worker *
1613*8b26181fSAndroid Build Coastguard Worker * XXX - PacketOpenAdapter() takes a non-const pointer
1614*8b26181fSAndroid Build Coastguard Worker * as an argument, so we make a copy of the argument and
1615*8b26181fSAndroid Build Coastguard Worker * pass that to it.
1616*8b26181fSAndroid Build Coastguard Worker */
1617*8b26181fSAndroid Build Coastguard Worker device_copy = strdup(device);
1618*8b26181fSAndroid Build Coastguard Worker if (device_copy == NULL) {
1619*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno, "malloc");
1620*8b26181fSAndroid Build Coastguard Worker status = -1;
1621*8b26181fSAndroid Build Coastguard Worker break;
1622*8b26181fSAndroid Build Coastguard Worker }
1623*8b26181fSAndroid Build Coastguard Worker
1624*8b26181fSAndroid Build Coastguard Worker adapter = PacketOpenAdapter(device_copy);
1625*8b26181fSAndroid Build Coastguard Worker if (adapter == NULL)
1626*8b26181fSAndroid Build Coastguard Worker {
1627*8b26181fSAndroid Build Coastguard Worker error = GetLastError();
1628*8b26181fSAndroid Build Coastguard Worker /*
1629*8b26181fSAndroid Build Coastguard Worker * If we can't open the device now, we won't be
1630*8b26181fSAndroid Build Coastguard Worker * able to later, either.
1631*8b26181fSAndroid Build Coastguard Worker *
1632*8b26181fSAndroid Build Coastguard Worker * If the error is something that indicates
1633*8b26181fSAndroid Build Coastguard Worker * that the device doesn't exist, or that they
1634*8b26181fSAndroid Build Coastguard Worker * don't have permission to open the device - or
1635*8b26181fSAndroid Build Coastguard Worker * perhaps that they don't have permission to get
1636*8b26181fSAndroid Build Coastguard Worker * a list of devices, if PacketOpenAdapter() does
1637*8b26181fSAndroid Build Coastguard Worker * that - the user will find that out when they try
1638*8b26181fSAndroid Build Coastguard Worker * to activate the device; just return an empty
1639*8b26181fSAndroid Build Coastguard Worker * list of time stamp types.
1640*8b26181fSAndroid Build Coastguard Worker *
1641*8b26181fSAndroid Build Coastguard Worker * Treating either of those as errors will, for
1642*8b26181fSAndroid Build Coastguard Worker * example, cause "tcpdump -i <number>" to fail,
1643*8b26181fSAndroid Build Coastguard Worker * because it first tries to pass the interface
1644*8b26181fSAndroid Build Coastguard Worker * name to pcap_create() and pcap_activate(),
1645*8b26181fSAndroid Build Coastguard Worker * in order to handle OSes where interfaces can
1646*8b26181fSAndroid Build Coastguard Worker * have names that are just numbers (stand up
1647*8b26181fSAndroid Build Coastguard Worker * and say hello, Linux!), and, if pcap_activate()
1648*8b26181fSAndroid Build Coastguard Worker * fails with a "no such device" error, checks
1649*8b26181fSAndroid Build Coastguard Worker * whether the interface name is a valid number
1650*8b26181fSAndroid Build Coastguard Worker * and, if so, tries to use it as an index in
1651*8b26181fSAndroid Build Coastguard Worker * the list of interfaces.
1652*8b26181fSAndroid Build Coastguard Worker *
1653*8b26181fSAndroid Build Coastguard Worker * That means pcap_create() must succeed even
1654*8b26181fSAndroid Build Coastguard Worker * for interfaces that don't exist, with the
1655*8b26181fSAndroid Build Coastguard Worker * failure occurring at pcap_activate() time.
1656*8b26181fSAndroid Build Coastguard Worker */
1657*8b26181fSAndroid Build Coastguard Worker if (error == ERROR_BAD_UNIT ||
1658*8b26181fSAndroid Build Coastguard Worker error == ERROR_ACCESS_DENIED) {
1659*8b26181fSAndroid Build Coastguard Worker p->tstamp_type_count = 0;
1660*8b26181fSAndroid Build Coastguard Worker p->tstamp_type_list = NULL;
1661*8b26181fSAndroid Build Coastguard Worker status = 0;
1662*8b26181fSAndroid Build Coastguard Worker } else {
1663*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(ebuf,
1664*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, error,
1665*8b26181fSAndroid Build Coastguard Worker "Error opening adapter");
1666*8b26181fSAndroid Build Coastguard Worker status = -1;
1667*8b26181fSAndroid Build Coastguard Worker }
1668*8b26181fSAndroid Build Coastguard Worker break;
1669*8b26181fSAndroid Build Coastguard Worker }
1670*8b26181fSAndroid Build Coastguard Worker
1671*8b26181fSAndroid Build Coastguard Worker /*
1672*8b26181fSAndroid Build Coastguard Worker * Get the total number of time stamp modes.
1673*8b26181fSAndroid Build Coastguard Worker *
1674*8b26181fSAndroid Build Coastguard Worker * The buffer for PacketGetTimestampModes() is
1675*8b26181fSAndroid Build Coastguard Worker * a sequence of 1 or more ULONGs. What's
1676*8b26181fSAndroid Build Coastguard Worker * passed to PacketGetTimestampModes() should have
1677*8b26181fSAndroid Build Coastguard Worker * the total number of ULONGs in the first ULONG;
1678*8b26181fSAndroid Build Coastguard Worker * what's returned *from* PacketGetTimestampModes()
1679*8b26181fSAndroid Build Coastguard Worker * has the total number of time stamp modes in
1680*8b26181fSAndroid Build Coastguard Worker * the first ULONG.
1681*8b26181fSAndroid Build Coastguard Worker *
1682*8b26181fSAndroid Build Coastguard Worker * Yes, that means if there are N time stamp
1683*8b26181fSAndroid Build Coastguard Worker * modes, the first ULONG should be set to N+1
1684*8b26181fSAndroid Build Coastguard Worker * on input, and will be set to N on output.
1685*8b26181fSAndroid Build Coastguard Worker *
1686*8b26181fSAndroid Build Coastguard Worker * We first make a call to PacketGetTimestampModes()
1687*8b26181fSAndroid Build Coastguard Worker * with a pointer to a single ULONG set to 1; the
1688*8b26181fSAndroid Build Coastguard Worker * call should fail with ERROR_MORE_DATA (unless
1689*8b26181fSAndroid Build Coastguard Worker * there are *no* modes, but that should never
1690*8b26181fSAndroid Build Coastguard Worker * happen), and that ULONG should be set to the
1691*8b26181fSAndroid Build Coastguard Worker * number of modes.
1692*8b26181fSAndroid Build Coastguard Worker */
1693*8b26181fSAndroid Build Coastguard Worker num_ts_modes = 1;
1694*8b26181fSAndroid Build Coastguard Worker ret = PacketGetTimestampModes(adapter, &num_ts_modes);
1695*8b26181fSAndroid Build Coastguard Worker if (!ret) {
1696*8b26181fSAndroid Build Coastguard Worker /*
1697*8b26181fSAndroid Build Coastguard Worker * OK, it failed. Did it fail with
1698*8b26181fSAndroid Build Coastguard Worker * ERROR_MORE_DATA?
1699*8b26181fSAndroid Build Coastguard Worker */
1700*8b26181fSAndroid Build Coastguard Worker error = GetLastError();
1701*8b26181fSAndroid Build Coastguard Worker if (error != ERROR_MORE_DATA) {
1702*8b26181fSAndroid Build Coastguard Worker /*
1703*8b26181fSAndroid Build Coastguard Worker * No, did it fail with ERROR_INVALID_FUNCTION?
1704*8b26181fSAndroid Build Coastguard Worker */
1705*8b26181fSAndroid Build Coastguard Worker if (error == ERROR_INVALID_FUNCTION) {
1706*8b26181fSAndroid Build Coastguard Worker /*
1707*8b26181fSAndroid Build Coastguard Worker * This is probably due to
1708*8b26181fSAndroid Build Coastguard Worker * the driver with which Packet.dll
1709*8b26181fSAndroid Build Coastguard Worker * communicates being older, or
1710*8b26181fSAndroid Build Coastguard Worker * being a WinPcap driver, so
1711*8b26181fSAndroid Build Coastguard Worker * that it doesn't support
1712*8b26181fSAndroid Build Coastguard Worker * BIOCGTIMESTAMPMODES.
1713*8b26181fSAndroid Build Coastguard Worker *
1714*8b26181fSAndroid Build Coastguard Worker * Tell the user to try uninstalling
1715*8b26181fSAndroid Build Coastguard Worker * Npcap - and WinPcap if installed -
1716*8b26181fSAndroid Build Coastguard Worker * and re-installing it, to flush
1717*8b26181fSAndroid Build Coastguard Worker * out all older drivers.
1718*8b26181fSAndroid Build Coastguard Worker */
1719*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1720*8b26181fSAndroid Build Coastguard Worker "PacketGetTimestampModes() failed with ERROR_INVALID_FUNCTION; try uninstalling Npcap, and WinPcap if installed, and re-installing it from npcap.com");
1721*8b26181fSAndroid Build Coastguard Worker status = -1;
1722*8b26181fSAndroid Build Coastguard Worker break;
1723*8b26181fSAndroid Build Coastguard Worker }
1724*8b26181fSAndroid Build Coastguard Worker
1725*8b26181fSAndroid Build Coastguard Worker /*
1726*8b26181fSAndroid Build Coastguard Worker * No, some other error. Fail.
1727*8b26181fSAndroid Build Coastguard Worker */
1728*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(ebuf,
1729*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, error,
1730*8b26181fSAndroid Build Coastguard Worker "Error calling PacketGetTimestampModes");
1731*8b26181fSAndroid Build Coastguard Worker status = -1;
1732*8b26181fSAndroid Build Coastguard Worker break;
1733*8b26181fSAndroid Build Coastguard Worker }
1734*8b26181fSAndroid Build Coastguard Worker }
1735*8b26181fSAndroid Build Coastguard Worker /* else (ret == TRUE)
1736*8b26181fSAndroid Build Coastguard Worker * Unexpected success. Let's act like we got ERROR_MORE_DATA.
1737*8b26181fSAndroid Build Coastguard Worker * If it doesn't work, we'll hit some other error condition farther on.
1738*8b26181fSAndroid Build Coastguard Worker */
1739*8b26181fSAndroid Build Coastguard Worker
1740*8b26181fSAndroid Build Coastguard Worker /* If the driver reports no modes supported *and*
1741*8b26181fSAndroid Build Coastguard Worker * ERROR_MORE_DATA, something is seriously wrong.
1742*8b26181fSAndroid Build Coastguard Worker * We *could* ignore the error and continue without supporting
1743*8b26181fSAndroid Build Coastguard Worker * settable timestamp modes, but that would hide a bug.
1744*8b26181fSAndroid Build Coastguard Worker */
1745*8b26181fSAndroid Build Coastguard Worker if (num_ts_modes == 0) {
1746*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1747*8b26181fSAndroid Build Coastguard Worker "PacketGetTimestampModes() reports 0 modes supported.");
1748*8b26181fSAndroid Build Coastguard Worker status = -1;
1749*8b26181fSAndroid Build Coastguard Worker break;
1750*8b26181fSAndroid Build Coastguard Worker }
1751*8b26181fSAndroid Build Coastguard Worker
1752*8b26181fSAndroid Build Coastguard Worker /*
1753*8b26181fSAndroid Build Coastguard Worker * Yes, so we now know how many types to fetch.
1754*8b26181fSAndroid Build Coastguard Worker *
1755*8b26181fSAndroid Build Coastguard Worker * The buffer needs to have one ULONG for the
1756*8b26181fSAndroid Build Coastguard Worker * count and num_ts_modes ULONGs for the
1757*8b26181fSAndroid Build Coastguard Worker * num_ts_modes time stamp types.
1758*8b26181fSAndroid Build Coastguard Worker */
1759*8b26181fSAndroid Build Coastguard Worker modes = (ULONG *)malloc((1 + num_ts_modes) * sizeof(ULONG));
1760*8b26181fSAndroid Build Coastguard Worker if (modes == NULL) {
1761*8b26181fSAndroid Build Coastguard Worker /* Out of memory. */
1762*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno, "malloc");
1763*8b26181fSAndroid Build Coastguard Worker status = -1;
1764*8b26181fSAndroid Build Coastguard Worker break;
1765*8b26181fSAndroid Build Coastguard Worker }
1766*8b26181fSAndroid Build Coastguard Worker modes[0] = 1 + num_ts_modes;
1767*8b26181fSAndroid Build Coastguard Worker if (!PacketGetTimestampModes(adapter, modes)) {
1768*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(ebuf,
1769*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, GetLastError(),
1770*8b26181fSAndroid Build Coastguard Worker "Error calling PacketGetTimestampModes");
1771*8b26181fSAndroid Build Coastguard Worker status = -1;
1772*8b26181fSAndroid Build Coastguard Worker break;
1773*8b26181fSAndroid Build Coastguard Worker }
1774*8b26181fSAndroid Build Coastguard Worker if (modes[0] != num_ts_modes) {
1775*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1776*8b26181fSAndroid Build Coastguard Worker "First PacketGetTimestampModes() call gives %lu modes, second call gives %lu modes",
1777*8b26181fSAndroid Build Coastguard Worker num_ts_modes, modes[0]);
1778*8b26181fSAndroid Build Coastguard Worker status = -1;
1779*8b26181fSAndroid Build Coastguard Worker break;
1780*8b26181fSAndroid Build Coastguard Worker }
1781*8b26181fSAndroid Build Coastguard Worker
1782*8b26181fSAndroid Build Coastguard Worker /*
1783*8b26181fSAndroid Build Coastguard Worker * Allocate a buffer big enough for
1784*8b26181fSAndroid Build Coastguard Worker * PCAP_TSTAMP_HOST (default) plus
1785*8b26181fSAndroid Build Coastguard Worker * the explicitly specified modes.
1786*8b26181fSAndroid Build Coastguard Worker */
1787*8b26181fSAndroid Build Coastguard Worker p->tstamp_type_list = malloc((1 + num_ts_modes) * sizeof(u_int));
1788*8b26181fSAndroid Build Coastguard Worker if (p->tstamp_type_list == NULL) {
1789*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno, "malloc");
1790*8b26181fSAndroid Build Coastguard Worker status = -1;
1791*8b26181fSAndroid Build Coastguard Worker break;
1792*8b26181fSAndroid Build Coastguard Worker }
1793*8b26181fSAndroid Build Coastguard Worker u_int num_ts_types = 0;
1794*8b26181fSAndroid Build Coastguard Worker p->tstamp_type_list[num_ts_types] =
1795*8b26181fSAndroid Build Coastguard Worker PCAP_TSTAMP_HOST;
1796*8b26181fSAndroid Build Coastguard Worker num_ts_types++;
1797*8b26181fSAndroid Build Coastguard Worker for (ULONG i = 0; i < num_ts_modes; i++) {
1798*8b26181fSAndroid Build Coastguard Worker switch (modes[i + 1]) {
1799*8b26181fSAndroid Build Coastguard Worker
1800*8b26181fSAndroid Build Coastguard Worker case TIMESTAMPMODE_SINGLE_SYNCHRONIZATION:
1801*8b26181fSAndroid Build Coastguard Worker /*
1802*8b26181fSAndroid Build Coastguard Worker * Better than low-res,
1803*8b26181fSAndroid Build Coastguard Worker * but *not* synchronized
1804*8b26181fSAndroid Build Coastguard Worker * with the OS clock.
1805*8b26181fSAndroid Build Coastguard Worker */
1806*8b26181fSAndroid Build Coastguard Worker p->tstamp_type_list[num_ts_types] =
1807*8b26181fSAndroid Build Coastguard Worker PCAP_TSTAMP_HOST_HIPREC_UNSYNCED;
1808*8b26181fSAndroid Build Coastguard Worker num_ts_types++;
1809*8b26181fSAndroid Build Coastguard Worker break;
1810*8b26181fSAndroid Build Coastguard Worker
1811*8b26181fSAndroid Build Coastguard Worker case TIMESTAMPMODE_QUERYSYSTEMTIME:
1812*8b26181fSAndroid Build Coastguard Worker /*
1813*8b26181fSAndroid Build Coastguard Worker * Low-res, but synchronized
1814*8b26181fSAndroid Build Coastguard Worker * with the OS clock.
1815*8b26181fSAndroid Build Coastguard Worker */
1816*8b26181fSAndroid Build Coastguard Worker p->tstamp_type_list[num_ts_types] =
1817*8b26181fSAndroid Build Coastguard Worker PCAP_TSTAMP_HOST_LOWPREC;
1818*8b26181fSAndroid Build Coastguard Worker num_ts_types++;
1819*8b26181fSAndroid Build Coastguard Worker break;
1820*8b26181fSAndroid Build Coastguard Worker
1821*8b26181fSAndroid Build Coastguard Worker case TIMESTAMPMODE_QUERYSYSTEMTIME_PRECISE:
1822*8b26181fSAndroid Build Coastguard Worker /*
1823*8b26181fSAndroid Build Coastguard Worker * High-res, and synchronized
1824*8b26181fSAndroid Build Coastguard Worker * with the OS clock.
1825*8b26181fSAndroid Build Coastguard Worker */
1826*8b26181fSAndroid Build Coastguard Worker p->tstamp_type_list[num_ts_types] =
1827*8b26181fSAndroid Build Coastguard Worker PCAP_TSTAMP_HOST_HIPREC;
1828*8b26181fSAndroid Build Coastguard Worker num_ts_types++;
1829*8b26181fSAndroid Build Coastguard Worker break;
1830*8b26181fSAndroid Build Coastguard Worker
1831*8b26181fSAndroid Build Coastguard Worker default:
1832*8b26181fSAndroid Build Coastguard Worker /*
1833*8b26181fSAndroid Build Coastguard Worker * Unknown, so we can't
1834*8b26181fSAndroid Build Coastguard Worker * report it.
1835*8b26181fSAndroid Build Coastguard Worker */
1836*8b26181fSAndroid Build Coastguard Worker break;
1837*8b26181fSAndroid Build Coastguard Worker }
1838*8b26181fSAndroid Build Coastguard Worker }
1839*8b26181fSAndroid Build Coastguard Worker p->tstamp_type_count = num_ts_types;
1840*8b26181fSAndroid Build Coastguard Worker } while (0);
1841*8b26181fSAndroid Build Coastguard Worker
1842*8b26181fSAndroid Build Coastguard Worker /* Clean up temporary allocations */
1843*8b26181fSAndroid Build Coastguard Worker if (device_copy != NULL) {
1844*8b26181fSAndroid Build Coastguard Worker free(device_copy);
1845*8b26181fSAndroid Build Coastguard Worker }
1846*8b26181fSAndroid Build Coastguard Worker if (modes != NULL) {
1847*8b26181fSAndroid Build Coastguard Worker free(modes);
1848*8b26181fSAndroid Build Coastguard Worker }
1849*8b26181fSAndroid Build Coastguard Worker if (adapter != NULL) {
1850*8b26181fSAndroid Build Coastguard Worker PacketCloseAdapter(adapter);
1851*8b26181fSAndroid Build Coastguard Worker }
1852*8b26181fSAndroid Build Coastguard Worker
1853*8b26181fSAndroid Build Coastguard Worker return status;
1854*8b26181fSAndroid Build Coastguard Worker }
1855*8b26181fSAndroid Build Coastguard Worker #else /* HAVE_PACKET_GET_TIMESTAMP_MODES */
1856*8b26181fSAndroid Build Coastguard Worker static int
get_ts_types(const char * device _U_,pcap_t * p _U_,char * ebuf _U_)1857*8b26181fSAndroid Build Coastguard Worker get_ts_types(const char *device _U_, pcap_t *p _U_, char *ebuf _U_)
1858*8b26181fSAndroid Build Coastguard Worker {
1859*8b26181fSAndroid Build Coastguard Worker /*
1860*8b26181fSAndroid Build Coastguard Worker * Nothing to fetch, so it always "succeeds".
1861*8b26181fSAndroid Build Coastguard Worker */
1862*8b26181fSAndroid Build Coastguard Worker return 0;
1863*8b26181fSAndroid Build Coastguard Worker }
1864*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_PACKET_GET_TIMESTAMP_MODES */
1865*8b26181fSAndroid Build Coastguard Worker
1866*8b26181fSAndroid Build Coastguard Worker pcap_t *
pcap_create_interface(const char * device _U_,char * ebuf)1867*8b26181fSAndroid Build Coastguard Worker pcap_create_interface(const char *device _U_, char *ebuf)
1868*8b26181fSAndroid Build Coastguard Worker {
1869*8b26181fSAndroid Build Coastguard Worker pcap_t *p;
1870*8b26181fSAndroid Build Coastguard Worker
1871*8b26181fSAndroid Build Coastguard Worker p = PCAP_CREATE_COMMON(ebuf, struct pcap_win);
1872*8b26181fSAndroid Build Coastguard Worker if (p == NULL)
1873*8b26181fSAndroid Build Coastguard Worker return (NULL);
1874*8b26181fSAndroid Build Coastguard Worker
1875*8b26181fSAndroid Build Coastguard Worker p->activate_op = pcap_activate_npf;
1876*8b26181fSAndroid Build Coastguard Worker p->can_set_rfmon_op = pcap_can_set_rfmon_npf;
1877*8b26181fSAndroid Build Coastguard Worker
1878*8b26181fSAndroid Build Coastguard Worker if (get_ts_types(device, p, ebuf) == -1) {
1879*8b26181fSAndroid Build Coastguard Worker pcap_close(p);
1880*8b26181fSAndroid Build Coastguard Worker return (NULL);
1881*8b26181fSAndroid Build Coastguard Worker }
1882*8b26181fSAndroid Build Coastguard Worker return (p);
1883*8b26181fSAndroid Build Coastguard Worker }
1884*8b26181fSAndroid Build Coastguard Worker
1885*8b26181fSAndroid Build Coastguard Worker static int
pcap_setfilter_npf(pcap_t * p,struct bpf_program * fp)1886*8b26181fSAndroid Build Coastguard Worker pcap_setfilter_npf(pcap_t *p, struct bpf_program *fp)
1887*8b26181fSAndroid Build Coastguard Worker {
1888*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
1889*8b26181fSAndroid Build Coastguard Worker
1890*8b26181fSAndroid Build Coastguard Worker if(PacketSetBpf(pw->adapter,fp)==FALSE){
1891*8b26181fSAndroid Build Coastguard Worker /*
1892*8b26181fSAndroid Build Coastguard Worker * Kernel filter not installed.
1893*8b26181fSAndroid Build Coastguard Worker *
1894*8b26181fSAndroid Build Coastguard Worker * XXX - we don't know whether this failed because:
1895*8b26181fSAndroid Build Coastguard Worker *
1896*8b26181fSAndroid Build Coastguard Worker * the kernel rejected the filter program as invalid,
1897*8b26181fSAndroid Build Coastguard Worker * in which case we should fall back on userland
1898*8b26181fSAndroid Build Coastguard Worker * filtering;
1899*8b26181fSAndroid Build Coastguard Worker *
1900*8b26181fSAndroid Build Coastguard Worker * the kernel rejected the filter program as too big,
1901*8b26181fSAndroid Build Coastguard Worker * in which case we should again fall back on
1902*8b26181fSAndroid Build Coastguard Worker * userland filtering;
1903*8b26181fSAndroid Build Coastguard Worker *
1904*8b26181fSAndroid Build Coastguard Worker * there was some other problem, in which case we
1905*8b26181fSAndroid Build Coastguard Worker * should probably report an error.
1906*8b26181fSAndroid Build Coastguard Worker *
1907*8b26181fSAndroid Build Coastguard Worker * For NPF devices, the Win32 status will be
1908*8b26181fSAndroid Build Coastguard Worker * STATUS_INVALID_DEVICE_REQUEST for invalid
1909*8b26181fSAndroid Build Coastguard Worker * filters, but I don't know what it'd be for
1910*8b26181fSAndroid Build Coastguard Worker * other problems, and for some other devices
1911*8b26181fSAndroid Build Coastguard Worker * it might not be set at all.
1912*8b26181fSAndroid Build Coastguard Worker *
1913*8b26181fSAndroid Build Coastguard Worker * So we just fall back on userland filtering in
1914*8b26181fSAndroid Build Coastguard Worker * all cases.
1915*8b26181fSAndroid Build Coastguard Worker */
1916*8b26181fSAndroid Build Coastguard Worker
1917*8b26181fSAndroid Build Coastguard Worker /*
1918*8b26181fSAndroid Build Coastguard Worker * install_bpf_program() validates the program.
1919*8b26181fSAndroid Build Coastguard Worker *
1920*8b26181fSAndroid Build Coastguard Worker * XXX - what if we already have a filter in the kernel?
1921*8b26181fSAndroid Build Coastguard Worker */
1922*8b26181fSAndroid Build Coastguard Worker if (install_bpf_program(p, fp) < 0)
1923*8b26181fSAndroid Build Coastguard Worker return (-1);
1924*8b26181fSAndroid Build Coastguard Worker pw->filtering_in_kernel = 0; /* filtering in userland */
1925*8b26181fSAndroid Build Coastguard Worker return (0);
1926*8b26181fSAndroid Build Coastguard Worker }
1927*8b26181fSAndroid Build Coastguard Worker
1928*8b26181fSAndroid Build Coastguard Worker /*
1929*8b26181fSAndroid Build Coastguard Worker * It worked.
1930*8b26181fSAndroid Build Coastguard Worker */
1931*8b26181fSAndroid Build Coastguard Worker pw->filtering_in_kernel = 1; /* filtering in the kernel */
1932*8b26181fSAndroid Build Coastguard Worker
1933*8b26181fSAndroid Build Coastguard Worker /*
1934*8b26181fSAndroid Build Coastguard Worker * Discard any previously-received packets, as they might have
1935*8b26181fSAndroid Build Coastguard Worker * passed whatever filter was formerly in effect, but might
1936*8b26181fSAndroid Build Coastguard Worker * not pass this filter (BIOCSETF discards packets buffered
1937*8b26181fSAndroid Build Coastguard Worker * in the kernel, so you can lose packets in any case).
1938*8b26181fSAndroid Build Coastguard Worker */
1939*8b26181fSAndroid Build Coastguard Worker p->cc = 0;
1940*8b26181fSAndroid Build Coastguard Worker return (0);
1941*8b26181fSAndroid Build Coastguard Worker }
1942*8b26181fSAndroid Build Coastguard Worker
1943*8b26181fSAndroid Build Coastguard Worker /*
1944*8b26181fSAndroid Build Coastguard Worker * We filter at user level, since the kernel driver doesn't process the packets
1945*8b26181fSAndroid Build Coastguard Worker */
1946*8b26181fSAndroid Build Coastguard Worker static int
pcap_setfilter_win32_dag(pcap_t * p,struct bpf_program * fp)1947*8b26181fSAndroid Build Coastguard Worker pcap_setfilter_win32_dag(pcap_t *p, struct bpf_program *fp) {
1948*8b26181fSAndroid Build Coastguard Worker
1949*8b26181fSAndroid Build Coastguard Worker if(!fp)
1950*8b26181fSAndroid Build Coastguard Worker {
1951*8b26181fSAndroid Build Coastguard Worker pcap_strlcpy(p->errbuf, "setfilter: No filter specified", sizeof(p->errbuf));
1952*8b26181fSAndroid Build Coastguard Worker return (-1);
1953*8b26181fSAndroid Build Coastguard Worker }
1954*8b26181fSAndroid Build Coastguard Worker
1955*8b26181fSAndroid Build Coastguard Worker /* Install a user level filter */
1956*8b26181fSAndroid Build Coastguard Worker if (install_bpf_program(p, fp) < 0)
1957*8b26181fSAndroid Build Coastguard Worker return (-1);
1958*8b26181fSAndroid Build Coastguard Worker
1959*8b26181fSAndroid Build Coastguard Worker return (0);
1960*8b26181fSAndroid Build Coastguard Worker }
1961*8b26181fSAndroid Build Coastguard Worker
1962*8b26181fSAndroid Build Coastguard Worker static int
pcap_getnonblock_npf(pcap_t * p)1963*8b26181fSAndroid Build Coastguard Worker pcap_getnonblock_npf(pcap_t *p)
1964*8b26181fSAndroid Build Coastguard Worker {
1965*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
1966*8b26181fSAndroid Build Coastguard Worker
1967*8b26181fSAndroid Build Coastguard Worker /*
1968*8b26181fSAndroid Build Coastguard Worker * XXX - if there were a PacketGetReadTimeout() call, we
1969*8b26181fSAndroid Build Coastguard Worker * would use it, and return 1 if the timeout is -1
1970*8b26181fSAndroid Build Coastguard Worker * and 0 otherwise.
1971*8b26181fSAndroid Build Coastguard Worker */
1972*8b26181fSAndroid Build Coastguard Worker return (pw->nonblock);
1973*8b26181fSAndroid Build Coastguard Worker }
1974*8b26181fSAndroid Build Coastguard Worker
1975*8b26181fSAndroid Build Coastguard Worker static int
pcap_setnonblock_npf(pcap_t * p,int nonblock)1976*8b26181fSAndroid Build Coastguard Worker pcap_setnonblock_npf(pcap_t *p, int nonblock)
1977*8b26181fSAndroid Build Coastguard Worker {
1978*8b26181fSAndroid Build Coastguard Worker struct pcap_win *pw = p->priv;
1979*8b26181fSAndroid Build Coastguard Worker int newtimeout;
1980*8b26181fSAndroid Build Coastguard Worker
1981*8b26181fSAndroid Build Coastguard Worker if (nonblock) {
1982*8b26181fSAndroid Build Coastguard Worker /*
1983*8b26181fSAndroid Build Coastguard Worker * Set the packet buffer timeout to -1 for non-blocking
1984*8b26181fSAndroid Build Coastguard Worker * mode.
1985*8b26181fSAndroid Build Coastguard Worker */
1986*8b26181fSAndroid Build Coastguard Worker newtimeout = -1;
1987*8b26181fSAndroid Build Coastguard Worker } else {
1988*8b26181fSAndroid Build Coastguard Worker /*
1989*8b26181fSAndroid Build Coastguard Worker * Restore the timeout set when the device was opened.
1990*8b26181fSAndroid Build Coastguard Worker * (Note that this may be -1, in which case we're not
1991*8b26181fSAndroid Build Coastguard Worker * really leaving non-blocking mode. However, although
1992*8b26181fSAndroid Build Coastguard Worker * the timeout argument to pcap_set_timeout() and
1993*8b26181fSAndroid Build Coastguard Worker * pcap_open_live() is an int, you're not supposed to
1994*8b26181fSAndroid Build Coastguard Worker * supply a negative value, so that "shouldn't happen".)
1995*8b26181fSAndroid Build Coastguard Worker */
1996*8b26181fSAndroid Build Coastguard Worker newtimeout = p->opt.timeout;
1997*8b26181fSAndroid Build Coastguard Worker }
1998*8b26181fSAndroid Build Coastguard Worker if (!PacketSetReadTimeout(pw->adapter, newtimeout)) {
1999*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(p->errbuf, PCAP_ERRBUF_SIZE,
2000*8b26181fSAndroid Build Coastguard Worker GetLastError(), "PacketSetReadTimeout");
2001*8b26181fSAndroid Build Coastguard Worker return (-1);
2002*8b26181fSAndroid Build Coastguard Worker }
2003*8b26181fSAndroid Build Coastguard Worker pw->nonblock = (newtimeout == -1);
2004*8b26181fSAndroid Build Coastguard Worker return (0);
2005*8b26181fSAndroid Build Coastguard Worker }
2006*8b26181fSAndroid Build Coastguard Worker
2007*8b26181fSAndroid Build Coastguard Worker static int
pcap_add_if_npf(pcap_if_list_t * devlistp,char * name,bpf_u_int32 flags,const char * description,char * errbuf)2008*8b26181fSAndroid Build Coastguard Worker pcap_add_if_npf(pcap_if_list_t *devlistp, char *name, bpf_u_int32 flags,
2009*8b26181fSAndroid Build Coastguard Worker const char *description, char *errbuf)
2010*8b26181fSAndroid Build Coastguard Worker {
2011*8b26181fSAndroid Build Coastguard Worker pcap_if_t *curdev;
2012*8b26181fSAndroid Build Coastguard Worker npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
2013*8b26181fSAndroid Build Coastguard Worker LONG if_addr_size;
2014*8b26181fSAndroid Build Coastguard Worker int res = 0;
2015*8b26181fSAndroid Build Coastguard Worker
2016*8b26181fSAndroid Build Coastguard Worker if_addr_size = MAX_NETWORK_ADDRESSES;
2017*8b26181fSAndroid Build Coastguard Worker
2018*8b26181fSAndroid Build Coastguard Worker /*
2019*8b26181fSAndroid Build Coastguard Worker * Add an entry for this interface, with no addresses.
2020*8b26181fSAndroid Build Coastguard Worker */
2021*8b26181fSAndroid Build Coastguard Worker curdev = add_dev(devlistp, name, flags, description, errbuf);
2022*8b26181fSAndroid Build Coastguard Worker if (curdev == NULL) {
2023*8b26181fSAndroid Build Coastguard Worker /*
2024*8b26181fSAndroid Build Coastguard Worker * Failure.
2025*8b26181fSAndroid Build Coastguard Worker */
2026*8b26181fSAndroid Build Coastguard Worker return (-1);
2027*8b26181fSAndroid Build Coastguard Worker }
2028*8b26181fSAndroid Build Coastguard Worker
2029*8b26181fSAndroid Build Coastguard Worker /*
2030*8b26181fSAndroid Build Coastguard Worker * Get the list of addresses for the interface.
2031*8b26181fSAndroid Build Coastguard Worker */
2032*8b26181fSAndroid Build Coastguard Worker if (!PacketGetNetInfoEx((void *)name, if_addrs, &if_addr_size)) {
2033*8b26181fSAndroid Build Coastguard Worker /*
2034*8b26181fSAndroid Build Coastguard Worker * Failure.
2035*8b26181fSAndroid Build Coastguard Worker *
2036*8b26181fSAndroid Build Coastguard Worker * We don't return an error, because this can happen with
2037*8b26181fSAndroid Build Coastguard Worker * NdisWan interfaces, and we want to supply them even
2038*8b26181fSAndroid Build Coastguard Worker * if we can't supply their addresses.
2039*8b26181fSAndroid Build Coastguard Worker *
2040*8b26181fSAndroid Build Coastguard Worker * We return an entry with an empty address list.
2041*8b26181fSAndroid Build Coastguard Worker */
2042*8b26181fSAndroid Build Coastguard Worker return (0);
2043*8b26181fSAndroid Build Coastguard Worker }
2044*8b26181fSAndroid Build Coastguard Worker
2045*8b26181fSAndroid Build Coastguard Worker /*
2046*8b26181fSAndroid Build Coastguard Worker * Now add the addresses.
2047*8b26181fSAndroid Build Coastguard Worker */
2048*8b26181fSAndroid Build Coastguard Worker while (if_addr_size-- > 0) {
2049*8b26181fSAndroid Build Coastguard Worker /*
2050*8b26181fSAndroid Build Coastguard Worker * "curdev" is an entry for this interface; add an entry for
2051*8b26181fSAndroid Build Coastguard Worker * this address to its list of addresses.
2052*8b26181fSAndroid Build Coastguard Worker */
2053*8b26181fSAndroid Build Coastguard Worker res = add_addr_to_dev(curdev,
2054*8b26181fSAndroid Build Coastguard Worker (struct sockaddr *)&if_addrs[if_addr_size].IPAddress,
2055*8b26181fSAndroid Build Coastguard Worker sizeof (struct sockaddr_storage),
2056*8b26181fSAndroid Build Coastguard Worker (struct sockaddr *)&if_addrs[if_addr_size].SubnetMask,
2057*8b26181fSAndroid Build Coastguard Worker sizeof (struct sockaddr_storage),
2058*8b26181fSAndroid Build Coastguard Worker (struct sockaddr *)&if_addrs[if_addr_size].Broadcast,
2059*8b26181fSAndroid Build Coastguard Worker sizeof (struct sockaddr_storage),
2060*8b26181fSAndroid Build Coastguard Worker NULL,
2061*8b26181fSAndroid Build Coastguard Worker 0,
2062*8b26181fSAndroid Build Coastguard Worker errbuf);
2063*8b26181fSAndroid Build Coastguard Worker if (res == -1) {
2064*8b26181fSAndroid Build Coastguard Worker /*
2065*8b26181fSAndroid Build Coastguard Worker * Failure.
2066*8b26181fSAndroid Build Coastguard Worker */
2067*8b26181fSAndroid Build Coastguard Worker break;
2068*8b26181fSAndroid Build Coastguard Worker }
2069*8b26181fSAndroid Build Coastguard Worker }
2070*8b26181fSAndroid Build Coastguard Worker
2071*8b26181fSAndroid Build Coastguard Worker return (res);
2072*8b26181fSAndroid Build Coastguard Worker }
2073*8b26181fSAndroid Build Coastguard Worker
2074*8b26181fSAndroid Build Coastguard Worker static int
get_if_flags(const char * name,bpf_u_int32 * flags,char * errbuf)2075*8b26181fSAndroid Build Coastguard Worker get_if_flags(const char *name, bpf_u_int32 *flags, char *errbuf)
2076*8b26181fSAndroid Build Coastguard Worker {
2077*8b26181fSAndroid Build Coastguard Worker char *name_copy;
2078*8b26181fSAndroid Build Coastguard Worker ADAPTER *adapter;
2079*8b26181fSAndroid Build Coastguard Worker int status;
2080*8b26181fSAndroid Build Coastguard Worker size_t len;
2081*8b26181fSAndroid Build Coastguard Worker NDIS_HARDWARE_STATUS hardware_status;
2082*8b26181fSAndroid Build Coastguard Worker #ifdef OID_GEN_PHYSICAL_MEDIUM
2083*8b26181fSAndroid Build Coastguard Worker NDIS_PHYSICAL_MEDIUM phys_medium;
2084*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 gen_physical_medium_oids[] = {
2085*8b26181fSAndroid Build Coastguard Worker #ifdef OID_GEN_PHYSICAL_MEDIUM_EX
2086*8b26181fSAndroid Build Coastguard Worker OID_GEN_PHYSICAL_MEDIUM_EX,
2087*8b26181fSAndroid Build Coastguard Worker #endif
2088*8b26181fSAndroid Build Coastguard Worker OID_GEN_PHYSICAL_MEDIUM
2089*8b26181fSAndroid Build Coastguard Worker };
2090*8b26181fSAndroid Build Coastguard Worker #define N_GEN_PHYSICAL_MEDIUM_OIDS (sizeof gen_physical_medium_oids / sizeof gen_physical_medium_oids[0])
2091*8b26181fSAndroid Build Coastguard Worker size_t i;
2092*8b26181fSAndroid Build Coastguard Worker #endif /* OID_GEN_PHYSICAL_MEDIUM */
2093*8b26181fSAndroid Build Coastguard Worker #ifdef OID_GEN_LINK_STATE
2094*8b26181fSAndroid Build Coastguard Worker NDIS_LINK_STATE link_state;
2095*8b26181fSAndroid Build Coastguard Worker #endif
2096*8b26181fSAndroid Build Coastguard Worker int connect_status;
2097*8b26181fSAndroid Build Coastguard Worker
2098*8b26181fSAndroid Build Coastguard Worker if (*flags & PCAP_IF_LOOPBACK) {
2099*8b26181fSAndroid Build Coastguard Worker /*
2100*8b26181fSAndroid Build Coastguard Worker * Loopback interface, so the connection status doesn't
2101*8b26181fSAndroid Build Coastguard Worker * apply. and it's not wireless (or wired, for that
2102*8b26181fSAndroid Build Coastguard Worker * matter...). We presume it's up and running.
2103*8b26181fSAndroid Build Coastguard Worker */
2104*8b26181fSAndroid Build Coastguard Worker *flags |= PCAP_IF_UP | PCAP_IF_RUNNING | PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
2105*8b26181fSAndroid Build Coastguard Worker return (0);
2106*8b26181fSAndroid Build Coastguard Worker }
2107*8b26181fSAndroid Build Coastguard Worker
2108*8b26181fSAndroid Build Coastguard Worker /*
2109*8b26181fSAndroid Build Coastguard Worker * We need to open the adapter to get this information.
2110*8b26181fSAndroid Build Coastguard Worker *
2111*8b26181fSAndroid Build Coastguard Worker * XXX - PacketOpenAdapter() takes a non-const pointer
2112*8b26181fSAndroid Build Coastguard Worker * as an argument, so we make a copy of the argument and
2113*8b26181fSAndroid Build Coastguard Worker * pass that to it.
2114*8b26181fSAndroid Build Coastguard Worker */
2115*8b26181fSAndroid Build Coastguard Worker name_copy = strdup(name);
2116*8b26181fSAndroid Build Coastguard Worker adapter = PacketOpenAdapter(name_copy);
2117*8b26181fSAndroid Build Coastguard Worker free(name_copy);
2118*8b26181fSAndroid Build Coastguard Worker if (adapter == NULL) {
2119*8b26181fSAndroid Build Coastguard Worker /*
2120*8b26181fSAndroid Build Coastguard Worker * Give up; if they try to open this device, it'll fail.
2121*8b26181fSAndroid Build Coastguard Worker */
2122*8b26181fSAndroid Build Coastguard Worker return (0);
2123*8b26181fSAndroid Build Coastguard Worker }
2124*8b26181fSAndroid Build Coastguard Worker
2125*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_AIRPCAP_API
2126*8b26181fSAndroid Build Coastguard Worker /*
2127*8b26181fSAndroid Build Coastguard Worker * Airpcap.sys do not support the below 'OID_GEN_x' values.
2128*8b26181fSAndroid Build Coastguard Worker * Just set these flags (and none of the '*flags' entered with).
2129*8b26181fSAndroid Build Coastguard Worker */
2130*8b26181fSAndroid Build Coastguard Worker if (PacketGetAirPcapHandle(adapter)) {
2131*8b26181fSAndroid Build Coastguard Worker /*
2132*8b26181fSAndroid Build Coastguard Worker * Must be "up" and "running" if the above if succeeded.
2133*8b26181fSAndroid Build Coastguard Worker */
2134*8b26181fSAndroid Build Coastguard Worker *flags = PCAP_IF_UP | PCAP_IF_RUNNING;
2135*8b26181fSAndroid Build Coastguard Worker
2136*8b26181fSAndroid Build Coastguard Worker /*
2137*8b26181fSAndroid Build Coastguard Worker * An airpcap device is a wireless device (duh!)
2138*8b26181fSAndroid Build Coastguard Worker */
2139*8b26181fSAndroid Build Coastguard Worker *flags |= PCAP_IF_WIRELESS;
2140*8b26181fSAndroid Build Coastguard Worker
2141*8b26181fSAndroid Build Coastguard Worker /*
2142*8b26181fSAndroid Build Coastguard Worker * A "network association state" makes no sense for airpcap.
2143*8b26181fSAndroid Build Coastguard Worker */
2144*8b26181fSAndroid Build Coastguard Worker *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
2145*8b26181fSAndroid Build Coastguard Worker PacketCloseAdapter(adapter);
2146*8b26181fSAndroid Build Coastguard Worker return (0);
2147*8b26181fSAndroid Build Coastguard Worker }
2148*8b26181fSAndroid Build Coastguard Worker #endif
2149*8b26181fSAndroid Build Coastguard Worker
2150*8b26181fSAndroid Build Coastguard Worker /*
2151*8b26181fSAndroid Build Coastguard Worker * Get the hardware status, and derive "up" and "running" from
2152*8b26181fSAndroid Build Coastguard Worker * that.
2153*8b26181fSAndroid Build Coastguard Worker */
2154*8b26181fSAndroid Build Coastguard Worker len = sizeof (hardware_status);
2155*8b26181fSAndroid Build Coastguard Worker status = oid_get_request(adapter, OID_GEN_HARDWARE_STATUS,
2156*8b26181fSAndroid Build Coastguard Worker &hardware_status, &len, errbuf);
2157*8b26181fSAndroid Build Coastguard Worker if (status == 0) {
2158*8b26181fSAndroid Build Coastguard Worker switch (hardware_status) {
2159*8b26181fSAndroid Build Coastguard Worker
2160*8b26181fSAndroid Build Coastguard Worker case NdisHardwareStatusReady:
2161*8b26181fSAndroid Build Coastguard Worker /*
2162*8b26181fSAndroid Build Coastguard Worker * "Available and capable of sending and receiving
2163*8b26181fSAndroid Build Coastguard Worker * data over the wire", so up and running.
2164*8b26181fSAndroid Build Coastguard Worker */
2165*8b26181fSAndroid Build Coastguard Worker *flags |= PCAP_IF_UP | PCAP_IF_RUNNING;
2166*8b26181fSAndroid Build Coastguard Worker break;
2167*8b26181fSAndroid Build Coastguard Worker
2168*8b26181fSAndroid Build Coastguard Worker case NdisHardwareStatusInitializing:
2169*8b26181fSAndroid Build Coastguard Worker case NdisHardwareStatusReset:
2170*8b26181fSAndroid Build Coastguard Worker /*
2171*8b26181fSAndroid Build Coastguard Worker * "Initializing" or "Resetting", so up, but
2172*8b26181fSAndroid Build Coastguard Worker * not running.
2173*8b26181fSAndroid Build Coastguard Worker */
2174*8b26181fSAndroid Build Coastguard Worker *flags |= PCAP_IF_UP;
2175*8b26181fSAndroid Build Coastguard Worker break;
2176*8b26181fSAndroid Build Coastguard Worker
2177*8b26181fSAndroid Build Coastguard Worker case NdisHardwareStatusClosing:
2178*8b26181fSAndroid Build Coastguard Worker case NdisHardwareStatusNotReady:
2179*8b26181fSAndroid Build Coastguard Worker /*
2180*8b26181fSAndroid Build Coastguard Worker * "Closing" or "Not ready", so neither up nor
2181*8b26181fSAndroid Build Coastguard Worker * running.
2182*8b26181fSAndroid Build Coastguard Worker */
2183*8b26181fSAndroid Build Coastguard Worker break;
2184*8b26181fSAndroid Build Coastguard Worker
2185*8b26181fSAndroid Build Coastguard Worker default:
2186*8b26181fSAndroid Build Coastguard Worker /*
2187*8b26181fSAndroid Build Coastguard Worker * Unknown.
2188*8b26181fSAndroid Build Coastguard Worker */
2189*8b26181fSAndroid Build Coastguard Worker break;
2190*8b26181fSAndroid Build Coastguard Worker }
2191*8b26181fSAndroid Build Coastguard Worker } else {
2192*8b26181fSAndroid Build Coastguard Worker /*
2193*8b26181fSAndroid Build Coastguard Worker * Can't get the hardware status, so assume both up and
2194*8b26181fSAndroid Build Coastguard Worker * running.
2195*8b26181fSAndroid Build Coastguard Worker */
2196*8b26181fSAndroid Build Coastguard Worker *flags |= PCAP_IF_UP | PCAP_IF_RUNNING;
2197*8b26181fSAndroid Build Coastguard Worker }
2198*8b26181fSAndroid Build Coastguard Worker
2199*8b26181fSAndroid Build Coastguard Worker /*
2200*8b26181fSAndroid Build Coastguard Worker * Get the network type.
2201*8b26181fSAndroid Build Coastguard Worker */
2202*8b26181fSAndroid Build Coastguard Worker #ifdef OID_GEN_PHYSICAL_MEDIUM
2203*8b26181fSAndroid Build Coastguard Worker /*
2204*8b26181fSAndroid Build Coastguard Worker * Try the OIDs we have for this, in order.
2205*8b26181fSAndroid Build Coastguard Worker */
2206*8b26181fSAndroid Build Coastguard Worker for (i = 0; i < N_GEN_PHYSICAL_MEDIUM_OIDS; i++) {
2207*8b26181fSAndroid Build Coastguard Worker len = sizeof (phys_medium);
2208*8b26181fSAndroid Build Coastguard Worker status = oid_get_request(adapter, gen_physical_medium_oids[i],
2209*8b26181fSAndroid Build Coastguard Worker &phys_medium, &len, errbuf);
2210*8b26181fSAndroid Build Coastguard Worker if (status == 0) {
2211*8b26181fSAndroid Build Coastguard Worker /*
2212*8b26181fSAndroid Build Coastguard Worker * Success.
2213*8b26181fSAndroid Build Coastguard Worker */
2214*8b26181fSAndroid Build Coastguard Worker break;
2215*8b26181fSAndroid Build Coastguard Worker }
2216*8b26181fSAndroid Build Coastguard Worker /*
2217*8b26181fSAndroid Build Coastguard Worker * Failed. We can't determine whether it failed
2218*8b26181fSAndroid Build Coastguard Worker * because that particular OID isn't supported
2219*8b26181fSAndroid Build Coastguard Worker * or because some other problem occurred, so we
2220*8b26181fSAndroid Build Coastguard Worker * just drive on and try the next OID.
2221*8b26181fSAndroid Build Coastguard Worker */
2222*8b26181fSAndroid Build Coastguard Worker }
2223*8b26181fSAndroid Build Coastguard Worker if (status == 0) {
2224*8b26181fSAndroid Build Coastguard Worker /*
2225*8b26181fSAndroid Build Coastguard Worker * We got the physical medium.
2226*8b26181fSAndroid Build Coastguard Worker *
2227*8b26181fSAndroid Build Coastguard Worker * XXX - we might want to check for NdisPhysicalMediumWiMax
2228*8b26181fSAndroid Build Coastguard Worker * and NdisPhysicalMediumNative802_15_4 being
2229*8b26181fSAndroid Build Coastguard Worker * part of the enum, and check for those in the "wireless"
2230*8b26181fSAndroid Build Coastguard Worker * case.
2231*8b26181fSAndroid Build Coastguard Worker */
2232*8b26181fSAndroid Build Coastguard Worker DIAG_OFF_ENUM_SWITCH
2233*8b26181fSAndroid Build Coastguard Worker switch (phys_medium) {
2234*8b26181fSAndroid Build Coastguard Worker
2235*8b26181fSAndroid Build Coastguard Worker case NdisPhysicalMediumWirelessLan:
2236*8b26181fSAndroid Build Coastguard Worker case NdisPhysicalMediumWirelessWan:
2237*8b26181fSAndroid Build Coastguard Worker case NdisPhysicalMediumNative802_11:
2238*8b26181fSAndroid Build Coastguard Worker case NdisPhysicalMediumBluetooth:
2239*8b26181fSAndroid Build Coastguard Worker case NdisPhysicalMediumUWB:
2240*8b26181fSAndroid Build Coastguard Worker case NdisPhysicalMediumIrda:
2241*8b26181fSAndroid Build Coastguard Worker /*
2242*8b26181fSAndroid Build Coastguard Worker * Wireless.
2243*8b26181fSAndroid Build Coastguard Worker */
2244*8b26181fSAndroid Build Coastguard Worker *flags |= PCAP_IF_WIRELESS;
2245*8b26181fSAndroid Build Coastguard Worker break;
2246*8b26181fSAndroid Build Coastguard Worker
2247*8b26181fSAndroid Build Coastguard Worker default:
2248*8b26181fSAndroid Build Coastguard Worker /*
2249*8b26181fSAndroid Build Coastguard Worker * Not wireless or unknown
2250*8b26181fSAndroid Build Coastguard Worker */
2251*8b26181fSAndroid Build Coastguard Worker break;
2252*8b26181fSAndroid Build Coastguard Worker }
2253*8b26181fSAndroid Build Coastguard Worker DIAG_ON_ENUM_SWITCH
2254*8b26181fSAndroid Build Coastguard Worker }
2255*8b26181fSAndroid Build Coastguard Worker #endif
2256*8b26181fSAndroid Build Coastguard Worker
2257*8b26181fSAndroid Build Coastguard Worker /*
2258*8b26181fSAndroid Build Coastguard Worker * Get the connection status.
2259*8b26181fSAndroid Build Coastguard Worker */
2260*8b26181fSAndroid Build Coastguard Worker #ifdef OID_GEN_LINK_STATE
2261*8b26181fSAndroid Build Coastguard Worker len = sizeof(link_state);
2262*8b26181fSAndroid Build Coastguard Worker status = oid_get_request(adapter, OID_GEN_LINK_STATE, &link_state,
2263*8b26181fSAndroid Build Coastguard Worker &len, errbuf);
2264*8b26181fSAndroid Build Coastguard Worker if (status == 0) {
2265*8b26181fSAndroid Build Coastguard Worker /*
2266*8b26181fSAndroid Build Coastguard Worker * NOTE: this also gives us the receive and transmit
2267*8b26181fSAndroid Build Coastguard Worker * link state.
2268*8b26181fSAndroid Build Coastguard Worker */
2269*8b26181fSAndroid Build Coastguard Worker switch (link_state.MediaConnectState) {
2270*8b26181fSAndroid Build Coastguard Worker
2271*8b26181fSAndroid Build Coastguard Worker case MediaConnectStateConnected:
2272*8b26181fSAndroid Build Coastguard Worker /*
2273*8b26181fSAndroid Build Coastguard Worker * It's connected.
2274*8b26181fSAndroid Build Coastguard Worker */
2275*8b26181fSAndroid Build Coastguard Worker *flags |= PCAP_IF_CONNECTION_STATUS_CONNECTED;
2276*8b26181fSAndroid Build Coastguard Worker break;
2277*8b26181fSAndroid Build Coastguard Worker
2278*8b26181fSAndroid Build Coastguard Worker case MediaConnectStateDisconnected:
2279*8b26181fSAndroid Build Coastguard Worker /*
2280*8b26181fSAndroid Build Coastguard Worker * It's disconnected.
2281*8b26181fSAndroid Build Coastguard Worker */
2282*8b26181fSAndroid Build Coastguard Worker *flags |= PCAP_IF_CONNECTION_STATUS_DISCONNECTED;
2283*8b26181fSAndroid Build Coastguard Worker break;
2284*8b26181fSAndroid Build Coastguard Worker
2285*8b26181fSAndroid Build Coastguard Worker case MediaConnectStateUnknown:
2286*8b26181fSAndroid Build Coastguard Worker default:
2287*8b26181fSAndroid Build Coastguard Worker /*
2288*8b26181fSAndroid Build Coastguard Worker * It's unknown whether it's connected or not.
2289*8b26181fSAndroid Build Coastguard Worker */
2290*8b26181fSAndroid Build Coastguard Worker break;
2291*8b26181fSAndroid Build Coastguard Worker }
2292*8b26181fSAndroid Build Coastguard Worker }
2293*8b26181fSAndroid Build Coastguard Worker #else
2294*8b26181fSAndroid Build Coastguard Worker /*
2295*8b26181fSAndroid Build Coastguard Worker * OID_GEN_LINK_STATE isn't supported because it's not in our SDK.
2296*8b26181fSAndroid Build Coastguard Worker */
2297*8b26181fSAndroid Build Coastguard Worker status = -1;
2298*8b26181fSAndroid Build Coastguard Worker #endif
2299*8b26181fSAndroid Build Coastguard Worker if (status == -1) {
2300*8b26181fSAndroid Build Coastguard Worker /*
2301*8b26181fSAndroid Build Coastguard Worker * OK, OID_GEN_LINK_STATE didn't work, try
2302*8b26181fSAndroid Build Coastguard Worker * OID_GEN_MEDIA_CONNECT_STATUS.
2303*8b26181fSAndroid Build Coastguard Worker */
2304*8b26181fSAndroid Build Coastguard Worker status = oid_get_request(adapter, OID_GEN_MEDIA_CONNECT_STATUS,
2305*8b26181fSAndroid Build Coastguard Worker &connect_status, &len, errbuf);
2306*8b26181fSAndroid Build Coastguard Worker if (status == 0) {
2307*8b26181fSAndroid Build Coastguard Worker switch (connect_status) {
2308*8b26181fSAndroid Build Coastguard Worker
2309*8b26181fSAndroid Build Coastguard Worker case NdisMediaStateConnected:
2310*8b26181fSAndroid Build Coastguard Worker /*
2311*8b26181fSAndroid Build Coastguard Worker * It's connected.
2312*8b26181fSAndroid Build Coastguard Worker */
2313*8b26181fSAndroid Build Coastguard Worker *flags |= PCAP_IF_CONNECTION_STATUS_CONNECTED;
2314*8b26181fSAndroid Build Coastguard Worker break;
2315*8b26181fSAndroid Build Coastguard Worker
2316*8b26181fSAndroid Build Coastguard Worker case NdisMediaStateDisconnected:
2317*8b26181fSAndroid Build Coastguard Worker /*
2318*8b26181fSAndroid Build Coastguard Worker * It's disconnected.
2319*8b26181fSAndroid Build Coastguard Worker */
2320*8b26181fSAndroid Build Coastguard Worker *flags |= PCAP_IF_CONNECTION_STATUS_DISCONNECTED;
2321*8b26181fSAndroid Build Coastguard Worker break;
2322*8b26181fSAndroid Build Coastguard Worker }
2323*8b26181fSAndroid Build Coastguard Worker }
2324*8b26181fSAndroid Build Coastguard Worker }
2325*8b26181fSAndroid Build Coastguard Worker PacketCloseAdapter(adapter);
2326*8b26181fSAndroid Build Coastguard Worker return (0);
2327*8b26181fSAndroid Build Coastguard Worker }
2328*8b26181fSAndroid Build Coastguard Worker
2329*8b26181fSAndroid Build Coastguard Worker int
pcap_platform_finddevs(pcap_if_list_t * devlistp,char * errbuf)2330*8b26181fSAndroid Build Coastguard Worker pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
2331*8b26181fSAndroid Build Coastguard Worker {
2332*8b26181fSAndroid Build Coastguard Worker int ret = 0;
2333*8b26181fSAndroid Build Coastguard Worker const char *desc;
2334*8b26181fSAndroid Build Coastguard Worker char *AdaptersName;
2335*8b26181fSAndroid Build Coastguard Worker ULONG NameLength;
2336*8b26181fSAndroid Build Coastguard Worker char *name;
2337*8b26181fSAndroid Build Coastguard Worker
2338*8b26181fSAndroid Build Coastguard Worker /*
2339*8b26181fSAndroid Build Coastguard Worker * Find out how big a buffer we need.
2340*8b26181fSAndroid Build Coastguard Worker *
2341*8b26181fSAndroid Build Coastguard Worker * This call should always return FALSE; if the error is
2342*8b26181fSAndroid Build Coastguard Worker * ERROR_INSUFFICIENT_BUFFER, NameLength will be set to
2343*8b26181fSAndroid Build Coastguard Worker * the size of the buffer we need, otherwise there's a
2344*8b26181fSAndroid Build Coastguard Worker * problem, and NameLength should be set to 0.
2345*8b26181fSAndroid Build Coastguard Worker *
2346*8b26181fSAndroid Build Coastguard Worker * It shouldn't require NameLength to be set, but,
2347*8b26181fSAndroid Build Coastguard Worker * at least as of WinPcap 4.1.3, it checks whether
2348*8b26181fSAndroid Build Coastguard Worker * NameLength is big enough before it checks for a
2349*8b26181fSAndroid Build Coastguard Worker * NULL buffer argument, so, while it'll still do
2350*8b26181fSAndroid Build Coastguard Worker * the right thing if NameLength is uninitialized and
2351*8b26181fSAndroid Build Coastguard Worker * whatever junk happens to be there is big enough
2352*8b26181fSAndroid Build Coastguard Worker * (because the pointer argument will be null), it's
2353*8b26181fSAndroid Build Coastguard Worker * still reading an uninitialized variable.
2354*8b26181fSAndroid Build Coastguard Worker */
2355*8b26181fSAndroid Build Coastguard Worker NameLength = 0;
2356*8b26181fSAndroid Build Coastguard Worker if (!PacketGetAdapterNames(NULL, &NameLength))
2357*8b26181fSAndroid Build Coastguard Worker {
2358*8b26181fSAndroid Build Coastguard Worker DWORD last_error = GetLastError();
2359*8b26181fSAndroid Build Coastguard Worker
2360*8b26181fSAndroid Build Coastguard Worker if (last_error != ERROR_INSUFFICIENT_BUFFER)
2361*8b26181fSAndroid Build Coastguard Worker {
2362*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(errbuf, PCAP_ERRBUF_SIZE,
2363*8b26181fSAndroid Build Coastguard Worker last_error, "PacketGetAdapterNames");
2364*8b26181fSAndroid Build Coastguard Worker return (-1);
2365*8b26181fSAndroid Build Coastguard Worker }
2366*8b26181fSAndroid Build Coastguard Worker }
2367*8b26181fSAndroid Build Coastguard Worker
2368*8b26181fSAndroid Build Coastguard Worker if (NameLength <= 0)
2369*8b26181fSAndroid Build Coastguard Worker return 0;
2370*8b26181fSAndroid Build Coastguard Worker AdaptersName = (char*) malloc(NameLength);
2371*8b26181fSAndroid Build Coastguard Worker if (AdaptersName == NULL)
2372*8b26181fSAndroid Build Coastguard Worker {
2373*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot allocate enough memory to list the adapters.");
2374*8b26181fSAndroid Build Coastguard Worker return (-1);
2375*8b26181fSAndroid Build Coastguard Worker }
2376*8b26181fSAndroid Build Coastguard Worker
2377*8b26181fSAndroid Build Coastguard Worker if (!PacketGetAdapterNames(AdaptersName, &NameLength)) {
2378*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(errbuf, PCAP_ERRBUF_SIZE,
2379*8b26181fSAndroid Build Coastguard Worker GetLastError(), "PacketGetAdapterNames");
2380*8b26181fSAndroid Build Coastguard Worker free(AdaptersName);
2381*8b26181fSAndroid Build Coastguard Worker return (-1);
2382*8b26181fSAndroid Build Coastguard Worker }
2383*8b26181fSAndroid Build Coastguard Worker
2384*8b26181fSAndroid Build Coastguard Worker /*
2385*8b26181fSAndroid Build Coastguard Worker * "PacketGetAdapterNames()" returned a list of
2386*8b26181fSAndroid Build Coastguard Worker * null-terminated ASCII interface name strings,
2387*8b26181fSAndroid Build Coastguard Worker * terminated by a null string, followed by a list
2388*8b26181fSAndroid Build Coastguard Worker * of null-terminated ASCII interface description
2389*8b26181fSAndroid Build Coastguard Worker * strings, terminated by a null string.
2390*8b26181fSAndroid Build Coastguard Worker * This means there are two ASCII nulls at the end
2391*8b26181fSAndroid Build Coastguard Worker * of the first list.
2392*8b26181fSAndroid Build Coastguard Worker *
2393*8b26181fSAndroid Build Coastguard Worker * Find the end of the first list; that's the
2394*8b26181fSAndroid Build Coastguard Worker * beginning of the second list.
2395*8b26181fSAndroid Build Coastguard Worker */
2396*8b26181fSAndroid Build Coastguard Worker desc = &AdaptersName[0];
2397*8b26181fSAndroid Build Coastguard Worker while (*desc != '\0' || *(desc + 1) != '\0')
2398*8b26181fSAndroid Build Coastguard Worker desc++;
2399*8b26181fSAndroid Build Coastguard Worker
2400*8b26181fSAndroid Build Coastguard Worker /*
2401*8b26181fSAndroid Build Coastguard Worker * Found it - "desc" points to the first of the two
2402*8b26181fSAndroid Build Coastguard Worker * nulls at the end of the list of names, so the
2403*8b26181fSAndroid Build Coastguard Worker * first byte of the list of descriptions is two bytes
2404*8b26181fSAndroid Build Coastguard Worker * after it.
2405*8b26181fSAndroid Build Coastguard Worker */
2406*8b26181fSAndroid Build Coastguard Worker desc += 2;
2407*8b26181fSAndroid Build Coastguard Worker
2408*8b26181fSAndroid Build Coastguard Worker /*
2409*8b26181fSAndroid Build Coastguard Worker * Loop over the elements in the first list.
2410*8b26181fSAndroid Build Coastguard Worker */
2411*8b26181fSAndroid Build Coastguard Worker name = &AdaptersName[0];
2412*8b26181fSAndroid Build Coastguard Worker while (*name != '\0') {
2413*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 flags = 0;
2414*8b26181fSAndroid Build Coastguard Worker
2415*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_AIRPCAP_API
2416*8b26181fSAndroid Build Coastguard Worker /*
2417*8b26181fSAndroid Build Coastguard Worker * Is this an AirPcap device?
2418*8b26181fSAndroid Build Coastguard Worker * If so, ignore it; it'll get added later, by the
2419*8b26181fSAndroid Build Coastguard Worker * AirPcap code.
2420*8b26181fSAndroid Build Coastguard Worker */
2421*8b26181fSAndroid Build Coastguard Worker if (device_is_airpcap(name, errbuf) == 1) {
2422*8b26181fSAndroid Build Coastguard Worker name += strlen(name) + 1;
2423*8b26181fSAndroid Build Coastguard Worker desc += strlen(desc) + 1;
2424*8b26181fSAndroid Build Coastguard Worker continue;
2425*8b26181fSAndroid Build Coastguard Worker }
2426*8b26181fSAndroid Build Coastguard Worker #endif
2427*8b26181fSAndroid Build Coastguard Worker
2428*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_PACKET_IS_LOOPBACK_ADAPTER
2429*8b26181fSAndroid Build Coastguard Worker /*
2430*8b26181fSAndroid Build Coastguard Worker * Is this a loopback interface?
2431*8b26181fSAndroid Build Coastguard Worker */
2432*8b26181fSAndroid Build Coastguard Worker if (PacketIsLoopbackAdapter(name)) {
2433*8b26181fSAndroid Build Coastguard Worker /* Yes */
2434*8b26181fSAndroid Build Coastguard Worker flags |= PCAP_IF_LOOPBACK;
2435*8b26181fSAndroid Build Coastguard Worker }
2436*8b26181fSAndroid Build Coastguard Worker #endif
2437*8b26181fSAndroid Build Coastguard Worker /*
2438*8b26181fSAndroid Build Coastguard Worker * Get additional flags.
2439*8b26181fSAndroid Build Coastguard Worker */
2440*8b26181fSAndroid Build Coastguard Worker if (get_if_flags(name, &flags, errbuf) == -1) {
2441*8b26181fSAndroid Build Coastguard Worker /*
2442*8b26181fSAndroid Build Coastguard Worker * Failure.
2443*8b26181fSAndroid Build Coastguard Worker */
2444*8b26181fSAndroid Build Coastguard Worker ret = -1;
2445*8b26181fSAndroid Build Coastguard Worker break;
2446*8b26181fSAndroid Build Coastguard Worker }
2447*8b26181fSAndroid Build Coastguard Worker
2448*8b26181fSAndroid Build Coastguard Worker /*
2449*8b26181fSAndroid Build Coastguard Worker * Add an entry for this interface.
2450*8b26181fSAndroid Build Coastguard Worker */
2451*8b26181fSAndroid Build Coastguard Worker if (pcap_add_if_npf(devlistp, name, flags, desc,
2452*8b26181fSAndroid Build Coastguard Worker errbuf) == -1) {
2453*8b26181fSAndroid Build Coastguard Worker /*
2454*8b26181fSAndroid Build Coastguard Worker * Failure.
2455*8b26181fSAndroid Build Coastguard Worker */
2456*8b26181fSAndroid Build Coastguard Worker ret = -1;
2457*8b26181fSAndroid Build Coastguard Worker break;
2458*8b26181fSAndroid Build Coastguard Worker }
2459*8b26181fSAndroid Build Coastguard Worker name += strlen(name) + 1;
2460*8b26181fSAndroid Build Coastguard Worker desc += strlen(desc) + 1;
2461*8b26181fSAndroid Build Coastguard Worker }
2462*8b26181fSAndroid Build Coastguard Worker
2463*8b26181fSAndroid Build Coastguard Worker free(AdaptersName);
2464*8b26181fSAndroid Build Coastguard Worker return (ret);
2465*8b26181fSAndroid Build Coastguard Worker }
2466*8b26181fSAndroid Build Coastguard Worker
2467*8b26181fSAndroid Build Coastguard Worker /*
2468*8b26181fSAndroid Build Coastguard Worker * Return the name of a network interface attached to the system, or NULL
2469*8b26181fSAndroid Build Coastguard Worker * if none can be found. The interface must be configured up; the
2470*8b26181fSAndroid Build Coastguard Worker * lowest unit number is preferred; loopback is ignored.
2471*8b26181fSAndroid Build Coastguard Worker *
2472*8b26181fSAndroid Build Coastguard Worker * In the best of all possible worlds, this would be the same as on
2473*8b26181fSAndroid Build Coastguard Worker * UN*X, but there may be software that expects this to return a
2474*8b26181fSAndroid Build Coastguard Worker * full list of devices after the first device.
2475*8b26181fSAndroid Build Coastguard Worker */
2476*8b26181fSAndroid Build Coastguard Worker #define ADAPTERSNAME_LEN 8192
2477*8b26181fSAndroid Build Coastguard Worker char *
pcap_lookupdev(char * errbuf)2478*8b26181fSAndroid Build Coastguard Worker pcap_lookupdev(char *errbuf)
2479*8b26181fSAndroid Build Coastguard Worker {
2480*8b26181fSAndroid Build Coastguard Worker DWORD dwVersion;
2481*8b26181fSAndroid Build Coastguard Worker DWORD dwWindowsMajorVersion;
2482*8b26181fSAndroid Build Coastguard Worker
2483*8b26181fSAndroid Build Coastguard Worker /*
2484*8b26181fSAndroid Build Coastguard Worker * We disable this in "new API" mode, because 1) in WinPcap/Npcap,
2485*8b26181fSAndroid Build Coastguard Worker * it may return UTF-16 strings, for backwards-compatibility
2486*8b26181fSAndroid Build Coastguard Worker * reasons, and we're also disabling the hack to make that work,
2487*8b26181fSAndroid Build Coastguard Worker * for not-going-past-the-end-of-a-string reasons, and 2) we
2488*8b26181fSAndroid Build Coastguard Worker * want its behavior to be consistent.
2489*8b26181fSAndroid Build Coastguard Worker *
2490*8b26181fSAndroid Build Coastguard Worker * In addition, it's not thread-safe, so we've marked it as
2491*8b26181fSAndroid Build Coastguard Worker * deprecated.
2492*8b26181fSAndroid Build Coastguard Worker */
2493*8b26181fSAndroid Build Coastguard Worker if (pcap_new_api) {
2494*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
2495*8b26181fSAndroid Build Coastguard Worker "pcap_lookupdev() is deprecated and is not supported in programs calling pcap_init()");
2496*8b26181fSAndroid Build Coastguard Worker return (NULL);
2497*8b26181fSAndroid Build Coastguard Worker }
2498*8b26181fSAndroid Build Coastguard Worker
2499*8b26181fSAndroid Build Coastguard Worker /* disable MSVC's GetVersion() deprecated warning here */
2500*8b26181fSAndroid Build Coastguard Worker DIAG_OFF_DEPRECATION
2501*8b26181fSAndroid Build Coastguard Worker dwVersion = GetVersion(); /* get the OS version */
2502*8b26181fSAndroid Build Coastguard Worker DIAG_ON_DEPRECATION
2503*8b26181fSAndroid Build Coastguard Worker dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
2504*8b26181fSAndroid Build Coastguard Worker
2505*8b26181fSAndroid Build Coastguard Worker if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4) {
2506*8b26181fSAndroid Build Coastguard Worker /*
2507*8b26181fSAndroid Build Coastguard Worker * Windows 95, 98, ME.
2508*8b26181fSAndroid Build Coastguard Worker */
2509*8b26181fSAndroid Build Coastguard Worker ULONG NameLength = ADAPTERSNAME_LEN;
2510*8b26181fSAndroid Build Coastguard Worker static char AdaptersName[ADAPTERSNAME_LEN];
2511*8b26181fSAndroid Build Coastguard Worker
2512*8b26181fSAndroid Build Coastguard Worker if (PacketGetAdapterNames(AdaptersName,&NameLength) )
2513*8b26181fSAndroid Build Coastguard Worker return (AdaptersName);
2514*8b26181fSAndroid Build Coastguard Worker else
2515*8b26181fSAndroid Build Coastguard Worker return NULL;
2516*8b26181fSAndroid Build Coastguard Worker } else {
2517*8b26181fSAndroid Build Coastguard Worker /*
2518*8b26181fSAndroid Build Coastguard Worker * Windows NT (NT 4.0 and later).
2519*8b26181fSAndroid Build Coastguard Worker * Convert the names to Unicode for backward compatibility.
2520*8b26181fSAndroid Build Coastguard Worker */
2521*8b26181fSAndroid Build Coastguard Worker ULONG NameLength = ADAPTERSNAME_LEN;
2522*8b26181fSAndroid Build Coastguard Worker static WCHAR AdaptersName[ADAPTERSNAME_LEN];
2523*8b26181fSAndroid Build Coastguard Worker size_t BufferSpaceLeft;
2524*8b26181fSAndroid Build Coastguard Worker char *tAstr;
2525*8b26181fSAndroid Build Coastguard Worker WCHAR *Unameptr;
2526*8b26181fSAndroid Build Coastguard Worker char *Adescptr;
2527*8b26181fSAndroid Build Coastguard Worker size_t namelen, i;
2528*8b26181fSAndroid Build Coastguard Worker WCHAR *TAdaptersName = (WCHAR*)malloc(ADAPTERSNAME_LEN * sizeof(WCHAR));
2529*8b26181fSAndroid Build Coastguard Worker int NAdapts = 0;
2530*8b26181fSAndroid Build Coastguard Worker
2531*8b26181fSAndroid Build Coastguard Worker if(TAdaptersName == NULL)
2532*8b26181fSAndroid Build Coastguard Worker {
2533*8b26181fSAndroid Build Coastguard Worker (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, "memory allocation failure");
2534*8b26181fSAndroid Build Coastguard Worker return NULL;
2535*8b26181fSAndroid Build Coastguard Worker }
2536*8b26181fSAndroid Build Coastguard Worker
2537*8b26181fSAndroid Build Coastguard Worker if ( !PacketGetAdapterNames((PTSTR)TAdaptersName,&NameLength) )
2538*8b26181fSAndroid Build Coastguard Worker {
2539*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_win32_err(errbuf, PCAP_ERRBUF_SIZE,
2540*8b26181fSAndroid Build Coastguard Worker GetLastError(), "PacketGetAdapterNames");
2541*8b26181fSAndroid Build Coastguard Worker free(TAdaptersName);
2542*8b26181fSAndroid Build Coastguard Worker return NULL;
2543*8b26181fSAndroid Build Coastguard Worker }
2544*8b26181fSAndroid Build Coastguard Worker
2545*8b26181fSAndroid Build Coastguard Worker
2546*8b26181fSAndroid Build Coastguard Worker BufferSpaceLeft = ADAPTERSNAME_LEN * sizeof(WCHAR);
2547*8b26181fSAndroid Build Coastguard Worker tAstr = (char*)TAdaptersName;
2548*8b26181fSAndroid Build Coastguard Worker Unameptr = AdaptersName;
2549*8b26181fSAndroid Build Coastguard Worker
2550*8b26181fSAndroid Build Coastguard Worker /*
2551*8b26181fSAndroid Build Coastguard Worker * Convert the device names to Unicode into AdapterName.
2552*8b26181fSAndroid Build Coastguard Worker */
2553*8b26181fSAndroid Build Coastguard Worker do {
2554*8b26181fSAndroid Build Coastguard Worker /*
2555*8b26181fSAndroid Build Coastguard Worker * Length of the name, including the terminating
2556*8b26181fSAndroid Build Coastguard Worker * NUL.
2557*8b26181fSAndroid Build Coastguard Worker */
2558*8b26181fSAndroid Build Coastguard Worker namelen = strlen(tAstr) + 1;
2559*8b26181fSAndroid Build Coastguard Worker
2560*8b26181fSAndroid Build Coastguard Worker /*
2561*8b26181fSAndroid Build Coastguard Worker * Do we have room for the name in the Unicode
2562*8b26181fSAndroid Build Coastguard Worker * buffer?
2563*8b26181fSAndroid Build Coastguard Worker */
2564*8b26181fSAndroid Build Coastguard Worker if (BufferSpaceLeft < namelen * sizeof(WCHAR)) {
2565*8b26181fSAndroid Build Coastguard Worker /*
2566*8b26181fSAndroid Build Coastguard Worker * No.
2567*8b26181fSAndroid Build Coastguard Worker */
2568*8b26181fSAndroid Build Coastguard Worker goto quit;
2569*8b26181fSAndroid Build Coastguard Worker }
2570*8b26181fSAndroid Build Coastguard Worker BufferSpaceLeft -= namelen * sizeof(WCHAR);
2571*8b26181fSAndroid Build Coastguard Worker
2572*8b26181fSAndroid Build Coastguard Worker /*
2573*8b26181fSAndroid Build Coastguard Worker * Copy the name, converting ASCII to Unicode.
2574*8b26181fSAndroid Build Coastguard Worker * namelen includes the NUL, so we copy it as
2575*8b26181fSAndroid Build Coastguard Worker * well.
2576*8b26181fSAndroid Build Coastguard Worker */
2577*8b26181fSAndroid Build Coastguard Worker for (i = 0; i < namelen; i++)
2578*8b26181fSAndroid Build Coastguard Worker *Unameptr++ = *tAstr++;
2579*8b26181fSAndroid Build Coastguard Worker
2580*8b26181fSAndroid Build Coastguard Worker /*
2581*8b26181fSAndroid Build Coastguard Worker * Count this adapter.
2582*8b26181fSAndroid Build Coastguard Worker */
2583*8b26181fSAndroid Build Coastguard Worker NAdapts++;
2584*8b26181fSAndroid Build Coastguard Worker } while (namelen != 1);
2585*8b26181fSAndroid Build Coastguard Worker
2586*8b26181fSAndroid Build Coastguard Worker /*
2587*8b26181fSAndroid Build Coastguard Worker * Copy the descriptions, but don't convert them from
2588*8b26181fSAndroid Build Coastguard Worker * ASCII to Unicode.
2589*8b26181fSAndroid Build Coastguard Worker */
2590*8b26181fSAndroid Build Coastguard Worker Adescptr = (char *)Unameptr;
2591*8b26181fSAndroid Build Coastguard Worker while(NAdapts--)
2592*8b26181fSAndroid Build Coastguard Worker {
2593*8b26181fSAndroid Build Coastguard Worker size_t desclen;
2594*8b26181fSAndroid Build Coastguard Worker
2595*8b26181fSAndroid Build Coastguard Worker desclen = strlen(tAstr) + 1;
2596*8b26181fSAndroid Build Coastguard Worker
2597*8b26181fSAndroid Build Coastguard Worker /*
2598*8b26181fSAndroid Build Coastguard Worker * Do we have room for the name in the Unicode
2599*8b26181fSAndroid Build Coastguard Worker * buffer?
2600*8b26181fSAndroid Build Coastguard Worker */
2601*8b26181fSAndroid Build Coastguard Worker if (BufferSpaceLeft < desclen) {
2602*8b26181fSAndroid Build Coastguard Worker /*
2603*8b26181fSAndroid Build Coastguard Worker * No.
2604*8b26181fSAndroid Build Coastguard Worker */
2605*8b26181fSAndroid Build Coastguard Worker goto quit;
2606*8b26181fSAndroid Build Coastguard Worker }
2607*8b26181fSAndroid Build Coastguard Worker
2608*8b26181fSAndroid Build Coastguard Worker /*
2609*8b26181fSAndroid Build Coastguard Worker * Just copy the ASCII string.
2610*8b26181fSAndroid Build Coastguard Worker * namelen includes the NUL, so we copy it as
2611*8b26181fSAndroid Build Coastguard Worker * well.
2612*8b26181fSAndroid Build Coastguard Worker */
2613*8b26181fSAndroid Build Coastguard Worker memcpy(Adescptr, tAstr, desclen);
2614*8b26181fSAndroid Build Coastguard Worker Adescptr += desclen;
2615*8b26181fSAndroid Build Coastguard Worker tAstr += desclen;
2616*8b26181fSAndroid Build Coastguard Worker BufferSpaceLeft -= desclen;
2617*8b26181fSAndroid Build Coastguard Worker }
2618*8b26181fSAndroid Build Coastguard Worker
2619*8b26181fSAndroid Build Coastguard Worker quit:
2620*8b26181fSAndroid Build Coastguard Worker free(TAdaptersName);
2621*8b26181fSAndroid Build Coastguard Worker return (char *)(AdaptersName);
2622*8b26181fSAndroid Build Coastguard Worker }
2623*8b26181fSAndroid Build Coastguard Worker }
2624*8b26181fSAndroid Build Coastguard Worker
2625*8b26181fSAndroid Build Coastguard Worker /*
2626*8b26181fSAndroid Build Coastguard Worker * We can't use the same code that we use on UN*X, as that's doing
2627*8b26181fSAndroid Build Coastguard Worker * UN*X-specific calls.
2628*8b26181fSAndroid Build Coastguard Worker *
2629*8b26181fSAndroid Build Coastguard Worker * We don't just fetch the entire list of devices, search for the
2630*8b26181fSAndroid Build Coastguard Worker * particular device, and use its first IPv4 address, as that's too
2631*8b26181fSAndroid Build Coastguard Worker * much work to get just one device's netmask.
2632*8b26181fSAndroid Build Coastguard Worker */
2633*8b26181fSAndroid Build Coastguard Worker int
pcap_lookupnet(const char * device,bpf_u_int32 * netp,bpf_u_int32 * maskp,char * errbuf)2634*8b26181fSAndroid Build Coastguard Worker pcap_lookupnet(const char *device, bpf_u_int32 *netp, bpf_u_int32 *maskp,
2635*8b26181fSAndroid Build Coastguard Worker char *errbuf)
2636*8b26181fSAndroid Build Coastguard Worker {
2637*8b26181fSAndroid Build Coastguard Worker /*
2638*8b26181fSAndroid Build Coastguard Worker * We need only the first IPv4 address, so we must scan the array returned by PacketGetNetInfo()
2639*8b26181fSAndroid Build Coastguard Worker * in order to skip non IPv4 (i.e. IPv6 addresses)
2640*8b26181fSAndroid Build Coastguard Worker */
2641*8b26181fSAndroid Build Coastguard Worker npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
2642*8b26181fSAndroid Build Coastguard Worker LONG if_addr_size = MAX_NETWORK_ADDRESSES;
2643*8b26181fSAndroid Build Coastguard Worker struct sockaddr_in *t_addr;
2644*8b26181fSAndroid Build Coastguard Worker LONG i;
2645*8b26181fSAndroid Build Coastguard Worker
2646*8b26181fSAndroid Build Coastguard Worker if (!PacketGetNetInfoEx((void *)device, if_addrs, &if_addr_size)) {
2647*8b26181fSAndroid Build Coastguard Worker *netp = *maskp = 0;
2648*8b26181fSAndroid Build Coastguard Worker return (0);
2649*8b26181fSAndroid Build Coastguard Worker }
2650*8b26181fSAndroid Build Coastguard Worker
2651*8b26181fSAndroid Build Coastguard Worker for(i = 0; i < if_addr_size; i++)
2652*8b26181fSAndroid Build Coastguard Worker {
2653*8b26181fSAndroid Build Coastguard Worker if(if_addrs[i].IPAddress.ss_family == AF_INET)
2654*8b26181fSAndroid Build Coastguard Worker {
2655*8b26181fSAndroid Build Coastguard Worker t_addr = (struct sockaddr_in *) &(if_addrs[i].IPAddress);
2656*8b26181fSAndroid Build Coastguard Worker *netp = t_addr->sin_addr.S_un.S_addr;
2657*8b26181fSAndroid Build Coastguard Worker t_addr = (struct sockaddr_in *) &(if_addrs[i].SubnetMask);
2658*8b26181fSAndroid Build Coastguard Worker *maskp = t_addr->sin_addr.S_un.S_addr;
2659*8b26181fSAndroid Build Coastguard Worker
2660*8b26181fSAndroid Build Coastguard Worker *netp &= *maskp;
2661*8b26181fSAndroid Build Coastguard Worker return (0);
2662*8b26181fSAndroid Build Coastguard Worker }
2663*8b26181fSAndroid Build Coastguard Worker
2664*8b26181fSAndroid Build Coastguard Worker }
2665*8b26181fSAndroid Build Coastguard Worker
2666*8b26181fSAndroid Build Coastguard Worker *netp = *maskp = 0;
2667*8b26181fSAndroid Build Coastguard Worker return (0);
2668*8b26181fSAndroid Build Coastguard Worker }
2669*8b26181fSAndroid Build Coastguard Worker
2670*8b26181fSAndroid Build Coastguard Worker static const char *pcap_lib_version_string;
2671*8b26181fSAndroid Build Coastguard Worker
2672*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_VERSION_H
2673*8b26181fSAndroid Build Coastguard Worker /*
2674*8b26181fSAndroid Build Coastguard Worker * libpcap being built for Windows, as part of a WinPcap/Npcap source
2675*8b26181fSAndroid Build Coastguard Worker * tree. Include version.h from that source tree to get the WinPcap/Npcap
2676*8b26181fSAndroid Build Coastguard Worker * version.
2677*8b26181fSAndroid Build Coastguard Worker *
2678*8b26181fSAndroid Build Coastguard Worker * XXX - it'd be nice if we could somehow generate the WinPcap/Npcap version
2679*8b26181fSAndroid Build Coastguard Worker * number when building as part of WinPcap/Npcap. (It'd be nice to do so
2680*8b26181fSAndroid Build Coastguard Worker * for the packet.dll version number as well.)
2681*8b26181fSAndroid Build Coastguard Worker */
2682*8b26181fSAndroid Build Coastguard Worker #include "../../version.h"
2683*8b26181fSAndroid Build Coastguard Worker
2684*8b26181fSAndroid Build Coastguard Worker static const char pcap_version_string[] =
2685*8b26181fSAndroid Build Coastguard Worker WINPCAP_PRODUCT_NAME " version " WINPCAP_VER_STRING ", based on " PCAP_VERSION_STRING;
2686*8b26181fSAndroid Build Coastguard Worker
2687*8b26181fSAndroid Build Coastguard Worker const char *
pcap_lib_version(void)2688*8b26181fSAndroid Build Coastguard Worker pcap_lib_version(void)
2689*8b26181fSAndroid Build Coastguard Worker {
2690*8b26181fSAndroid Build Coastguard Worker if (pcap_lib_version_string == NULL) {
2691*8b26181fSAndroid Build Coastguard Worker /*
2692*8b26181fSAndroid Build Coastguard Worker * Generate the version string.
2693*8b26181fSAndroid Build Coastguard Worker */
2694*8b26181fSAndroid Build Coastguard Worker const char *packet_version_string = PacketGetVersion();
2695*8b26181fSAndroid Build Coastguard Worker
2696*8b26181fSAndroid Build Coastguard Worker if (strcmp(WINPCAP_VER_STRING, packet_version_string) == 0) {
2697*8b26181fSAndroid Build Coastguard Worker /*
2698*8b26181fSAndroid Build Coastguard Worker * WinPcap/Npcap version string and packet.dll version
2699*8b26181fSAndroid Build Coastguard Worker * string are the same; just report the WinPcap/Npcap
2700*8b26181fSAndroid Build Coastguard Worker * version.
2701*8b26181fSAndroid Build Coastguard Worker */
2702*8b26181fSAndroid Build Coastguard Worker pcap_lib_version_string = pcap_version_string;
2703*8b26181fSAndroid Build Coastguard Worker } else {
2704*8b26181fSAndroid Build Coastguard Worker /*
2705*8b26181fSAndroid Build Coastguard Worker * WinPcap/Npcap version string and packet.dll version
2706*8b26181fSAndroid Build Coastguard Worker * string are different; that shouldn't be the
2707*8b26181fSAndroid Build Coastguard Worker * case (the two libraries should come from the
2708*8b26181fSAndroid Build Coastguard Worker * same version of WinPcap/Npcap), so we report both
2709*8b26181fSAndroid Build Coastguard Worker * versions.
2710*8b26181fSAndroid Build Coastguard Worker */
2711*8b26181fSAndroid Build Coastguard Worker char *full_pcap_version_string;
2712*8b26181fSAndroid Build Coastguard Worker
2713*8b26181fSAndroid Build Coastguard Worker if (pcap_asprintf(&full_pcap_version_string,
2714*8b26181fSAndroid Build Coastguard Worker WINPCAP_PRODUCT_NAME " version " WINPCAP_VER_STRING " (packet.dll version %s), based on " PCAP_VERSION_STRING,
2715*8b26181fSAndroid Build Coastguard Worker packet_version_string) != -1) {
2716*8b26181fSAndroid Build Coastguard Worker /* Success */
2717*8b26181fSAndroid Build Coastguard Worker pcap_lib_version_string = full_pcap_version_string;
2718*8b26181fSAndroid Build Coastguard Worker }
2719*8b26181fSAndroid Build Coastguard Worker }
2720*8b26181fSAndroid Build Coastguard Worker }
2721*8b26181fSAndroid Build Coastguard Worker return (pcap_lib_version_string);
2722*8b26181fSAndroid Build Coastguard Worker }
2723*8b26181fSAndroid Build Coastguard Worker
2724*8b26181fSAndroid Build Coastguard Worker #else /* HAVE_VERSION_H */
2725*8b26181fSAndroid Build Coastguard Worker
2726*8b26181fSAndroid Build Coastguard Worker /*
2727*8b26181fSAndroid Build Coastguard Worker * libpcap being built for Windows, not as part of a WinPcap/Npcap source
2728*8b26181fSAndroid Build Coastguard Worker * tree.
2729*8b26181fSAndroid Build Coastguard Worker */
2730*8b26181fSAndroid Build Coastguard Worker const char *
pcap_lib_version(void)2731*8b26181fSAndroid Build Coastguard Worker pcap_lib_version(void)
2732*8b26181fSAndroid Build Coastguard Worker {
2733*8b26181fSAndroid Build Coastguard Worker if (pcap_lib_version_string == NULL) {
2734*8b26181fSAndroid Build Coastguard Worker /*
2735*8b26181fSAndroid Build Coastguard Worker * Generate the version string. Report the packet.dll
2736*8b26181fSAndroid Build Coastguard Worker * version.
2737*8b26181fSAndroid Build Coastguard Worker */
2738*8b26181fSAndroid Build Coastguard Worker char *full_pcap_version_string;
2739*8b26181fSAndroid Build Coastguard Worker
2740*8b26181fSAndroid Build Coastguard Worker if (pcap_asprintf(&full_pcap_version_string,
2741*8b26181fSAndroid Build Coastguard Worker PCAP_VERSION_STRING " (packet.dll version %s)",
2742*8b26181fSAndroid Build Coastguard Worker PacketGetVersion()) != -1) {
2743*8b26181fSAndroid Build Coastguard Worker /* Success */
2744*8b26181fSAndroid Build Coastguard Worker pcap_lib_version_string = full_pcap_version_string;
2745*8b26181fSAndroid Build Coastguard Worker }
2746*8b26181fSAndroid Build Coastguard Worker }
2747*8b26181fSAndroid Build Coastguard Worker return (pcap_lib_version_string);
2748*8b26181fSAndroid Build Coastguard Worker }
2749*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_VERSION_H */
2750