1*c1d255d3SCy Schubert /*
2*c1d255d3SCy Schubert * WPA Supplicant - Layer2 packet handling with libpcap/libdnet and WinPcap
3*c1d255d3SCy Schubert * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
4*c1d255d3SCy Schubert *
5*c1d255d3SCy Schubert * This software may be distributed under the terms of the BSD license.
6*c1d255d3SCy Schubert * See README for more details.
7*c1d255d3SCy Schubert */
8*c1d255d3SCy Schubert
9*c1d255d3SCy Schubert #include "includes.h"
10*c1d255d3SCy Schubert #ifndef CONFIG_NATIVE_WINDOWS
11*c1d255d3SCy Schubert #include <sys/ioctl.h>
12*c1d255d3SCy Schubert #endif /* CONFIG_NATIVE_WINDOWS */
13*c1d255d3SCy Schubert #include <pcap.h>
14*c1d255d3SCy Schubert #ifndef CONFIG_WINPCAP
15*c1d255d3SCy Schubert #include <dnet.h>
16*c1d255d3SCy Schubert #endif /* CONFIG_WINPCAP */
17*c1d255d3SCy Schubert
18*c1d255d3SCy Schubert #include "common.h"
19*c1d255d3SCy Schubert #include "eloop.h"
20*c1d255d3SCy Schubert #include "l2_packet.h"
21*c1d255d3SCy Schubert
22*c1d255d3SCy Schubert
23*c1d255d3SCy Schubert static const u8 pae_group_addr[ETH_ALEN] =
24*c1d255d3SCy Schubert { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
25*c1d255d3SCy Schubert
26*c1d255d3SCy Schubert struct l2_packet_data {
27*c1d255d3SCy Schubert pcap_t *pcap;
28*c1d255d3SCy Schubert #ifdef CONFIG_WINPCAP
29*c1d255d3SCy Schubert unsigned int num_fast_poll;
30*c1d255d3SCy Schubert #else /* CONFIG_WINPCAP */
31*c1d255d3SCy Schubert eth_t *eth;
32*c1d255d3SCy Schubert #endif /* CONFIG_WINPCAP */
33*c1d255d3SCy Schubert char ifname[100];
34*c1d255d3SCy Schubert u8 own_addr[ETH_ALEN];
35*c1d255d3SCy Schubert void (*rx_callback)(void *ctx, const u8 *src_addr,
36*c1d255d3SCy Schubert const u8 *buf, size_t len);
37*c1d255d3SCy Schubert void *rx_callback_ctx;
38*c1d255d3SCy Schubert int l2_hdr; /* whether to include layer 2 (Ethernet) header in calls
39*c1d255d3SCy Schubert * to rx_callback */
40*c1d255d3SCy Schubert };
41*c1d255d3SCy Schubert
42*c1d255d3SCy Schubert
l2_packet_get_own_addr(struct l2_packet_data * l2,u8 * addr)43*c1d255d3SCy Schubert int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
44*c1d255d3SCy Schubert {
45*c1d255d3SCy Schubert os_memcpy(addr, l2->own_addr, ETH_ALEN);
46*c1d255d3SCy Schubert return 0;
47*c1d255d3SCy Schubert }
48*c1d255d3SCy Schubert
49*c1d255d3SCy Schubert
50*c1d255d3SCy Schubert #ifndef CONFIG_WINPCAP
l2_packet_init_libdnet(struct l2_packet_data * l2)51*c1d255d3SCy Schubert static int l2_packet_init_libdnet(struct l2_packet_data *l2)
52*c1d255d3SCy Schubert {
53*c1d255d3SCy Schubert eth_addr_t own_addr;
54*c1d255d3SCy Schubert
55*c1d255d3SCy Schubert l2->eth = eth_open(l2->ifname);
56*c1d255d3SCy Schubert if (!l2->eth) {
57*c1d255d3SCy Schubert wpa_printf(MSG_ERROR,
58*c1d255d3SCy Schubert "Failed to open interface '%s' - eth_open: %s",
59*c1d255d3SCy Schubert l2->ifname, strerror(errno));
60*c1d255d3SCy Schubert return -1;
61*c1d255d3SCy Schubert }
62*c1d255d3SCy Schubert
63*c1d255d3SCy Schubert if (eth_get(l2->eth, &own_addr) < 0) {
64*c1d255d3SCy Schubert wpa_printf(MSG_ERROR,
65*c1d255d3SCy Schubert "Failed to get own hw address from interface '%s' - eth_get: %s",
66*c1d255d3SCy Schubert l2->ifname, strerror(errno));
67*c1d255d3SCy Schubert eth_close(l2->eth);
68*c1d255d3SCy Schubert l2->eth = NULL;
69*c1d255d3SCy Schubert return -1;
70*c1d255d3SCy Schubert }
71*c1d255d3SCy Schubert os_memcpy(l2->own_addr, own_addr.data, ETH_ALEN);
72*c1d255d3SCy Schubert
73*c1d255d3SCy Schubert return 0;
74*c1d255d3SCy Schubert }
75*c1d255d3SCy Schubert #endif /* CONFIG_WINPCAP */
76*c1d255d3SCy Schubert
77*c1d255d3SCy Schubert
l2_packet_send(struct l2_packet_data * l2,const u8 * dst_addr,u16 proto,const u8 * buf,size_t len)78*c1d255d3SCy Schubert int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
79*c1d255d3SCy Schubert const u8 *buf, size_t len)
80*c1d255d3SCy Schubert {
81*c1d255d3SCy Schubert int ret;
82*c1d255d3SCy Schubert struct l2_ethhdr *eth;
83*c1d255d3SCy Schubert
84*c1d255d3SCy Schubert if (l2 == NULL)
85*c1d255d3SCy Schubert return -1;
86*c1d255d3SCy Schubert
87*c1d255d3SCy Schubert if (l2->l2_hdr) {
88*c1d255d3SCy Schubert #ifdef CONFIG_WINPCAP
89*c1d255d3SCy Schubert ret = pcap_sendpacket(l2->pcap, buf, len);
90*c1d255d3SCy Schubert #else /* CONFIG_WINPCAP */
91*c1d255d3SCy Schubert ret = eth_send(l2->eth, buf, len);
92*c1d255d3SCy Schubert #endif /* CONFIG_WINPCAP */
93*c1d255d3SCy Schubert } else {
94*c1d255d3SCy Schubert size_t mlen = sizeof(*eth) + len;
95*c1d255d3SCy Schubert eth = os_malloc(mlen);
96*c1d255d3SCy Schubert if (eth == NULL)
97*c1d255d3SCy Schubert return -1;
98*c1d255d3SCy Schubert
99*c1d255d3SCy Schubert os_memcpy(eth->h_dest, dst_addr, ETH_ALEN);
100*c1d255d3SCy Schubert os_memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
101*c1d255d3SCy Schubert eth->h_proto = htons(proto);
102*c1d255d3SCy Schubert os_memcpy(eth + 1, buf, len);
103*c1d255d3SCy Schubert
104*c1d255d3SCy Schubert #ifdef CONFIG_WINPCAP
105*c1d255d3SCy Schubert ret = pcap_sendpacket(l2->pcap, (u8 *) eth, mlen);
106*c1d255d3SCy Schubert #else /* CONFIG_WINPCAP */
107*c1d255d3SCy Schubert ret = eth_send(l2->eth, (u8 *) eth, mlen);
108*c1d255d3SCy Schubert #endif /* CONFIG_WINPCAP */
109*c1d255d3SCy Schubert
110*c1d255d3SCy Schubert os_free(eth);
111*c1d255d3SCy Schubert }
112*c1d255d3SCy Schubert
113*c1d255d3SCy Schubert return ret;
114*c1d255d3SCy Schubert }
115*c1d255d3SCy Schubert
116*c1d255d3SCy Schubert
117*c1d255d3SCy Schubert #ifndef CONFIG_WINPCAP
l2_packet_receive(int sock,void * eloop_ctx,void * sock_ctx)118*c1d255d3SCy Schubert static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
119*c1d255d3SCy Schubert {
120*c1d255d3SCy Schubert struct l2_packet_data *l2 = eloop_ctx;
121*c1d255d3SCy Schubert pcap_t *pcap = sock_ctx;
122*c1d255d3SCy Schubert struct pcap_pkthdr hdr;
123*c1d255d3SCy Schubert const u_char *packet;
124*c1d255d3SCy Schubert struct l2_ethhdr *ethhdr;
125*c1d255d3SCy Schubert unsigned char *buf;
126*c1d255d3SCy Schubert size_t len;
127*c1d255d3SCy Schubert
128*c1d255d3SCy Schubert packet = pcap_next(pcap, &hdr);
129*c1d255d3SCy Schubert
130*c1d255d3SCy Schubert if (!l2->rx_callback || !packet || hdr.caplen < sizeof(*ethhdr))
131*c1d255d3SCy Schubert return;
132*c1d255d3SCy Schubert
133*c1d255d3SCy Schubert ethhdr = (struct l2_ethhdr *) packet;
134*c1d255d3SCy Schubert if (l2->l2_hdr) {
135*c1d255d3SCy Schubert buf = (unsigned char *) ethhdr;
136*c1d255d3SCy Schubert len = hdr.caplen;
137*c1d255d3SCy Schubert } else {
138*c1d255d3SCy Schubert buf = (unsigned char *) (ethhdr + 1);
139*c1d255d3SCy Schubert len = hdr.caplen - sizeof(*ethhdr);
140*c1d255d3SCy Schubert }
141*c1d255d3SCy Schubert l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
142*c1d255d3SCy Schubert }
143*c1d255d3SCy Schubert #endif /* CONFIG_WINPCAP */
144*c1d255d3SCy Schubert
145*c1d255d3SCy Schubert
146*c1d255d3SCy Schubert #ifdef CONFIG_WINPCAP
l2_packet_receive_cb(u_char * user,const struct pcap_pkthdr * hdr,const u_char * pkt_data)147*c1d255d3SCy Schubert static void l2_packet_receive_cb(u_char *user, const struct pcap_pkthdr *hdr,
148*c1d255d3SCy Schubert const u_char *pkt_data)
149*c1d255d3SCy Schubert {
150*c1d255d3SCy Schubert struct l2_packet_data *l2 = (struct l2_packet_data *) user;
151*c1d255d3SCy Schubert struct l2_ethhdr *ethhdr;
152*c1d255d3SCy Schubert unsigned char *buf;
153*c1d255d3SCy Schubert size_t len;
154*c1d255d3SCy Schubert
155*c1d255d3SCy Schubert if (!l2->rx_callback || !pkt_data || hdr->caplen < sizeof(*ethhdr))
156*c1d255d3SCy Schubert return;
157*c1d255d3SCy Schubert
158*c1d255d3SCy Schubert ethhdr = (struct l2_ethhdr *) pkt_data;
159*c1d255d3SCy Schubert if (l2->l2_hdr) {
160*c1d255d3SCy Schubert buf = (unsigned char *) ethhdr;
161*c1d255d3SCy Schubert len = hdr->caplen;
162*c1d255d3SCy Schubert } else {
163*c1d255d3SCy Schubert buf = (unsigned char *) (ethhdr + 1);
164*c1d255d3SCy Schubert len = hdr->caplen - sizeof(*ethhdr);
165*c1d255d3SCy Schubert }
166*c1d255d3SCy Schubert l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
167*c1d255d3SCy Schubert /*
168*c1d255d3SCy Schubert * Use shorter poll interval for 3 seconds to reduce latency during key
169*c1d255d3SCy Schubert * handshake.
170*c1d255d3SCy Schubert */
171*c1d255d3SCy Schubert l2->num_fast_poll = 3 * 50;
172*c1d255d3SCy Schubert }
173*c1d255d3SCy Schubert
174*c1d255d3SCy Schubert
l2_packet_receive_timeout(void * eloop_ctx,void * timeout_ctx)175*c1d255d3SCy Schubert static void l2_packet_receive_timeout(void *eloop_ctx, void *timeout_ctx)
176*c1d255d3SCy Schubert {
177*c1d255d3SCy Schubert struct l2_packet_data *l2 = eloop_ctx;
178*c1d255d3SCy Schubert pcap_t *pcap = timeout_ctx;
179*c1d255d3SCy Schubert int timeout;
180*c1d255d3SCy Schubert
181*c1d255d3SCy Schubert if (l2->num_fast_poll > 0) {
182*c1d255d3SCy Schubert timeout = 20000;
183*c1d255d3SCy Schubert l2->num_fast_poll--;
184*c1d255d3SCy Schubert } else
185*c1d255d3SCy Schubert timeout = 100000;
186*c1d255d3SCy Schubert
187*c1d255d3SCy Schubert /* Register new timeout before calling l2_packet_receive() since
188*c1d255d3SCy Schubert * receive handler may free this l2_packet instance (which will
189*c1d255d3SCy Schubert * cancel this timeout). */
190*c1d255d3SCy Schubert eloop_register_timeout(0, timeout, l2_packet_receive_timeout,
191*c1d255d3SCy Schubert l2, pcap);
192*c1d255d3SCy Schubert pcap_dispatch(pcap, 10, l2_packet_receive_cb, (u_char *) l2);
193*c1d255d3SCy Schubert }
194*c1d255d3SCy Schubert #endif /* CONFIG_WINPCAP */
195*c1d255d3SCy Schubert
196*c1d255d3SCy Schubert
l2_packet_init_libpcap(struct l2_packet_data * l2,unsigned short protocol)197*c1d255d3SCy Schubert static int l2_packet_init_libpcap(struct l2_packet_data *l2,
198*c1d255d3SCy Schubert unsigned short protocol)
199*c1d255d3SCy Schubert {
200*c1d255d3SCy Schubert bpf_u_int32 pcap_maskp, pcap_netp;
201*c1d255d3SCy Schubert char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE];
202*c1d255d3SCy Schubert struct bpf_program pcap_fp;
203*c1d255d3SCy Schubert
204*c1d255d3SCy Schubert #ifdef CONFIG_WINPCAP
205*c1d255d3SCy Schubert char ifname[128];
206*c1d255d3SCy Schubert os_snprintf(ifname, sizeof(ifname), "\\Device\\NPF_%s", l2->ifname);
207*c1d255d3SCy Schubert pcap_lookupnet(ifname, &pcap_netp, &pcap_maskp, pcap_err);
208*c1d255d3SCy Schubert l2->pcap = pcap_open_live(ifname, 2500, 0, 10, pcap_err);
209*c1d255d3SCy Schubert if (l2->pcap == NULL) {
210*c1d255d3SCy Schubert fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
211*c1d255d3SCy Schubert fprintf(stderr, "ifname='%s'\n", ifname);
212*c1d255d3SCy Schubert return -1;
213*c1d255d3SCy Schubert }
214*c1d255d3SCy Schubert if (pcap_setnonblock(l2->pcap, 1, pcap_err) < 0)
215*c1d255d3SCy Schubert fprintf(stderr, "pcap_setnonblock: %s\n",
216*c1d255d3SCy Schubert pcap_geterr(l2->pcap));
217*c1d255d3SCy Schubert #else /* CONFIG_WINPCAP */
218*c1d255d3SCy Schubert pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
219*c1d255d3SCy Schubert l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
220*c1d255d3SCy Schubert if (l2->pcap == NULL) {
221*c1d255d3SCy Schubert fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
222*c1d255d3SCy Schubert fprintf(stderr, "ifname='%s'\n", l2->ifname);
223*c1d255d3SCy Schubert return -1;
224*c1d255d3SCy Schubert }
225*c1d255d3SCy Schubert if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
226*c1d255d3SCy Schubert pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
227*c1d255d3SCy Schubert fprintf(stderr, "pcap_set_datalink(DLT_EN10MB): %s\n",
228*c1d255d3SCy Schubert pcap_geterr(l2->pcap));
229*c1d255d3SCy Schubert return -1;
230*c1d255d3SCy Schubert }
231*c1d255d3SCy Schubert #endif /* CONFIG_WINPCAP */
232*c1d255d3SCy Schubert os_snprintf(pcap_filter, sizeof(pcap_filter),
233*c1d255d3SCy Schubert "not ether src " MACSTR " and "
234*c1d255d3SCy Schubert "( ether dst " MACSTR " or ether dst " MACSTR " ) and "
235*c1d255d3SCy Schubert "ether proto 0x%x",
236*c1d255d3SCy Schubert MAC2STR(l2->own_addr), /* do not receive own packets */
237*c1d255d3SCy Schubert MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
238*c1d255d3SCy Schubert protocol);
239*c1d255d3SCy Schubert if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
240*c1d255d3SCy Schubert fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
241*c1d255d3SCy Schubert return -1;
242*c1d255d3SCy Schubert }
243*c1d255d3SCy Schubert
244*c1d255d3SCy Schubert if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
245*c1d255d3SCy Schubert fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
246*c1d255d3SCy Schubert return -1;
247*c1d255d3SCy Schubert }
248*c1d255d3SCy Schubert
249*c1d255d3SCy Schubert pcap_freecode(&pcap_fp);
250*c1d255d3SCy Schubert #ifdef BIOCIMMEDIATE
251*c1d255d3SCy Schubert /*
252*c1d255d3SCy Schubert * When libpcap uses BPF we must enable "immediate mode" to
253*c1d255d3SCy Schubert * receive frames right away; otherwise the system may
254*c1d255d3SCy Schubert * buffer them for us.
255*c1d255d3SCy Schubert */
256*c1d255d3SCy Schubert {
257*c1d255d3SCy Schubert unsigned int on = 1;
258*c1d255d3SCy Schubert if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
259*c1d255d3SCy Schubert fprintf(stderr, "%s: cannot enable immediate mode on "
260*c1d255d3SCy Schubert "interface %s: %s\n",
261*c1d255d3SCy Schubert __func__, l2->ifname, strerror(errno));
262*c1d255d3SCy Schubert /* XXX should we fail? */
263*c1d255d3SCy Schubert }
264*c1d255d3SCy Schubert }
265*c1d255d3SCy Schubert #endif /* BIOCIMMEDIATE */
266*c1d255d3SCy Schubert
267*c1d255d3SCy Schubert #ifdef CONFIG_WINPCAP
268*c1d255d3SCy Schubert eloop_register_timeout(0, 100000, l2_packet_receive_timeout,
269*c1d255d3SCy Schubert l2, l2->pcap);
270*c1d255d3SCy Schubert #else /* CONFIG_WINPCAP */
271*c1d255d3SCy Schubert eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
272*c1d255d3SCy Schubert l2_packet_receive, l2, l2->pcap);
273*c1d255d3SCy Schubert #endif /* CONFIG_WINPCAP */
274*c1d255d3SCy Schubert
275*c1d255d3SCy Schubert return 0;
276*c1d255d3SCy Schubert }
277*c1d255d3SCy Schubert
278*c1d255d3SCy Schubert
l2_packet_init(const char * ifname,const u8 * own_addr,unsigned short protocol,void (* rx_callback)(void * ctx,const u8 * src_addr,const u8 * buf,size_t len),void * rx_callback_ctx,int l2_hdr)279*c1d255d3SCy Schubert struct l2_packet_data * l2_packet_init(
280*c1d255d3SCy Schubert const char *ifname, const u8 *own_addr, unsigned short protocol,
281*c1d255d3SCy Schubert void (*rx_callback)(void *ctx, const u8 *src_addr,
282*c1d255d3SCy Schubert const u8 *buf, size_t len),
283*c1d255d3SCy Schubert void *rx_callback_ctx, int l2_hdr)
284*c1d255d3SCy Schubert {
285*c1d255d3SCy Schubert struct l2_packet_data *l2;
286*c1d255d3SCy Schubert
287*c1d255d3SCy Schubert l2 = os_zalloc(sizeof(struct l2_packet_data));
288*c1d255d3SCy Schubert if (l2 == NULL)
289*c1d255d3SCy Schubert return NULL;
290*c1d255d3SCy Schubert os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
291*c1d255d3SCy Schubert l2->rx_callback = rx_callback;
292*c1d255d3SCy Schubert l2->rx_callback_ctx = rx_callback_ctx;
293*c1d255d3SCy Schubert l2->l2_hdr = l2_hdr;
294*c1d255d3SCy Schubert
295*c1d255d3SCy Schubert #ifdef CONFIG_WINPCAP
296*c1d255d3SCy Schubert if (own_addr)
297*c1d255d3SCy Schubert os_memcpy(l2->own_addr, own_addr, ETH_ALEN);
298*c1d255d3SCy Schubert #else /* CONFIG_WINPCAP */
299*c1d255d3SCy Schubert if (l2_packet_init_libdnet(l2))
300*c1d255d3SCy Schubert return NULL;
301*c1d255d3SCy Schubert #endif /* CONFIG_WINPCAP */
302*c1d255d3SCy Schubert
303*c1d255d3SCy Schubert if (l2_packet_init_libpcap(l2, protocol)) {
304*c1d255d3SCy Schubert #ifndef CONFIG_WINPCAP
305*c1d255d3SCy Schubert eth_close(l2->eth);
306*c1d255d3SCy Schubert #endif /* CONFIG_WINPCAP */
307*c1d255d3SCy Schubert os_free(l2);
308*c1d255d3SCy Schubert return NULL;
309*c1d255d3SCy Schubert }
310*c1d255d3SCy Schubert
311*c1d255d3SCy Schubert return l2;
312*c1d255d3SCy Schubert }
313*c1d255d3SCy Schubert
314*c1d255d3SCy Schubert
l2_packet_init_bridge(const char * br_ifname,const char * ifname,const u8 * own_addr,unsigned short protocol,void (* rx_callback)(void * ctx,const u8 * src_addr,const u8 * buf,size_t len),void * rx_callback_ctx,int l2_hdr)315*c1d255d3SCy Schubert struct l2_packet_data * l2_packet_init_bridge(
316*c1d255d3SCy Schubert const char *br_ifname, const char *ifname, const u8 *own_addr,
317*c1d255d3SCy Schubert unsigned short protocol,
318*c1d255d3SCy Schubert void (*rx_callback)(void *ctx, const u8 *src_addr,
319*c1d255d3SCy Schubert const u8 *buf, size_t len),
320*c1d255d3SCy Schubert void *rx_callback_ctx, int l2_hdr)
321*c1d255d3SCy Schubert {
322*c1d255d3SCy Schubert return l2_packet_init(br_ifname, own_addr, protocol, rx_callback,
323*c1d255d3SCy Schubert rx_callback_ctx, l2_hdr);
324*c1d255d3SCy Schubert }
325*c1d255d3SCy Schubert
326*c1d255d3SCy Schubert
l2_packet_deinit(struct l2_packet_data * l2)327*c1d255d3SCy Schubert void l2_packet_deinit(struct l2_packet_data *l2)
328*c1d255d3SCy Schubert {
329*c1d255d3SCy Schubert if (l2 == NULL)
330*c1d255d3SCy Schubert return;
331*c1d255d3SCy Schubert
332*c1d255d3SCy Schubert #ifdef CONFIG_WINPCAP
333*c1d255d3SCy Schubert eloop_cancel_timeout(l2_packet_receive_timeout, l2, l2->pcap);
334*c1d255d3SCy Schubert #else /* CONFIG_WINPCAP */
335*c1d255d3SCy Schubert if (l2->eth)
336*c1d255d3SCy Schubert eth_close(l2->eth);
337*c1d255d3SCy Schubert eloop_unregister_read_sock(pcap_get_selectable_fd(l2->pcap));
338*c1d255d3SCy Schubert #endif /* CONFIG_WINPCAP */
339*c1d255d3SCy Schubert if (l2->pcap)
340*c1d255d3SCy Schubert pcap_close(l2->pcap);
341*c1d255d3SCy Schubert os_free(l2);
342*c1d255d3SCy Schubert }
343*c1d255d3SCy Schubert
344*c1d255d3SCy Schubert
l2_packet_get_ip_addr(struct l2_packet_data * l2,char * buf,size_t len)345*c1d255d3SCy Schubert int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
346*c1d255d3SCy Schubert {
347*c1d255d3SCy Schubert pcap_if_t *devs, *dev;
348*c1d255d3SCy Schubert struct pcap_addr *addr;
349*c1d255d3SCy Schubert struct sockaddr_in *saddr;
350*c1d255d3SCy Schubert int found = 0;
351*c1d255d3SCy Schubert char err[PCAP_ERRBUF_SIZE + 1];
352*c1d255d3SCy Schubert
353*c1d255d3SCy Schubert if (pcap_findalldevs(&devs, err) < 0) {
354*c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
355*c1d255d3SCy Schubert return -1;
356*c1d255d3SCy Schubert }
357*c1d255d3SCy Schubert
358*c1d255d3SCy Schubert for (dev = devs; dev && !found; dev = dev->next) {
359*c1d255d3SCy Schubert if (os_strcmp(dev->name, l2->ifname) != 0)
360*c1d255d3SCy Schubert continue;
361*c1d255d3SCy Schubert
362*c1d255d3SCy Schubert addr = dev->addresses;
363*c1d255d3SCy Schubert while (addr) {
364*c1d255d3SCy Schubert saddr = (struct sockaddr_in *) addr->addr;
365*c1d255d3SCy Schubert if (saddr && saddr->sin_family == AF_INET) {
366*c1d255d3SCy Schubert os_strlcpy(buf, inet_ntoa(saddr->sin_addr),
367*c1d255d3SCy Schubert len);
368*c1d255d3SCy Schubert found = 1;
369*c1d255d3SCy Schubert break;
370*c1d255d3SCy Schubert }
371*c1d255d3SCy Schubert addr = addr->next;
372*c1d255d3SCy Schubert }
373*c1d255d3SCy Schubert }
374*c1d255d3SCy Schubert
375*c1d255d3SCy Schubert pcap_freealldevs(devs);
376*c1d255d3SCy Schubert
377*c1d255d3SCy Schubert return found ? 0 : -1;
378*c1d255d3SCy Schubert }
379*c1d255d3SCy Schubert
380*c1d255d3SCy Schubert
l2_packet_notify_auth_start(struct l2_packet_data * l2)381*c1d255d3SCy Schubert void l2_packet_notify_auth_start(struct l2_packet_data *l2)
382*c1d255d3SCy Schubert {
383*c1d255d3SCy Schubert #ifdef CONFIG_WINPCAP
384*c1d255d3SCy Schubert /*
385*c1d255d3SCy Schubert * Use shorter poll interval for 3 seconds to reduce latency during key
386*c1d255d3SCy Schubert * handshake.
387*c1d255d3SCy Schubert */
388*c1d255d3SCy Schubert l2->num_fast_poll = 3 * 50;
389*c1d255d3SCy Schubert eloop_cancel_timeout(l2_packet_receive_timeout, l2, l2->pcap);
390*c1d255d3SCy Schubert eloop_register_timeout(0, 10000, l2_packet_receive_timeout,
391*c1d255d3SCy Schubert l2, l2->pcap);
392*c1d255d3SCy Schubert #endif /* CONFIG_WINPCAP */
393*c1d255d3SCy Schubert }
394*c1d255d3SCy Schubert
395*c1d255d3SCy Schubert
l2_packet_set_packet_filter(struct l2_packet_data * l2,enum l2_packet_filter_type type)396*c1d255d3SCy Schubert int l2_packet_set_packet_filter(struct l2_packet_data *l2,
397*c1d255d3SCy Schubert enum l2_packet_filter_type type)
398*c1d255d3SCy Schubert {
399*c1d255d3SCy Schubert return -1;
400*c1d255d3SCy Schubert }
401