l2_packet_winpcap.c revision c5ec7f57ead87efa365800228aa0b09a12d9e6c4
18d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* 28d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * WPA Supplicant - Layer2 packet handling with WinPcap RX thread 38d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi> 48d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 5c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * This software may be distributed under the terms of the BSD license. 6c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * See README for more details. 78d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 88d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This l2_packet implementation is explicitly for WinPcap and Windows events. 98d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * l2_packet_pcap.c has support for WinPcap, but it requires polling to receive 108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * frames which means relatively long latency for EAPOL RX processing. The 118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * implementation here uses a separate thread to allow WinPcap to be receiving 128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * all the time to reduce latency for EAPOL receiving from about 100 ms to 3 ms 138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * when comparing l2_packet_pcap.c to l2_packet_winpcap.c. Extra sleep of 50 ms 148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * is added in to receive thread whenever no EAPOL frames has been received for 158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * a while. Whenever an EAPOL handshake is expected, this sleep is removed. 168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * The RX thread receives a frame and signals main thread through Windows event 188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * about the availability of a new frame. Processing the received frame is 198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * synchronized with pair of Windows events so that no extra buffer or queuing 208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * mechanism is needed. This implementation requires Windows specific event 218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * loop implementation, i.e., eloop_win.c. 228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * WinPcap has pcap_getevent() that could, in theory at least, be used to 248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * implement this kind of waiting with a simpler single-thread design. However, 258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * that event handle is not really signaled immediately when receiving each 268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * frame, so it does not really work for this kind of use. 278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "includes.h" 308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include <pcap.h> 318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "common.h" 338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "eloop.h" 348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "l2_packet.h" 358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic const u8 pae_group_addr[ETH_ALEN] = 388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 }; 398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* 418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Number of pcap_dispatch() iterations to do without extra wait after each 428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * received EAPOL packet or authentication notification. This is used to reduce 438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * latency for EAPOL receive. 448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic const size_t no_wait_count = 750; 468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct l2_packet_data { 488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt pcap_t *pcap; 498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt unsigned int num_fast_poll; 508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt char ifname[100]; 518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt u8 own_addr[ETH_ALEN]; 528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt void (*rx_callback)(void *ctx, const u8 *src_addr, 538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const u8 *buf, size_t len); 548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt void *rx_callback_ctx; 558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt int l2_hdr; /* whether to include layer 2 (Ethernet) header in calls to 568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * rx_callback and l2_packet_send() */ 578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt int running; 588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt HANDLE rx_avail, rx_done, rx_thread, rx_thread_done, rx_notify; 598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt u8 *rx_buf, *rx_src; 608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt size_t rx_len; 618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt size_t rx_no_wait; 628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}; 638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr) 668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memcpy(addr, l2->own_addr, ETH_ALEN); 688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return 0; 698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto, 738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const u8 *buf, size_t len) 748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt int ret; 768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct l2_ethhdr *eth; 778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (l2 == NULL) 798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (l2->l2_hdr) { 828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ret = pcap_sendpacket(l2->pcap, buf, len); 838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } else { 848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt size_t mlen = sizeof(*eth) + len; 858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt eth = os_malloc(mlen); 868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (eth == NULL) 878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memcpy(eth->h_dest, dst_addr, ETH_ALEN); 908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memcpy(eth->h_source, l2->own_addr, ETH_ALEN); 918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt eth->h_proto = htons(proto); 928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memcpy(eth + 1, buf, len); 938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ret = pcap_sendpacket(l2->pcap, (u8 *) eth, mlen); 948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(eth); 958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return ret; 988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* pcap_dispatch() callback for the RX thread */ 1028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void l2_packet_receive_cb(u_char *user, const struct pcap_pkthdr *hdr, 1038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const u_char *pkt_data) 1048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 1058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct l2_packet_data *l2 = (struct l2_packet_data *) user; 1068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct l2_ethhdr *ethhdr; 1078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (pkt_data == NULL || hdr->caplen < sizeof(*ethhdr)) 1098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return; 1108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ethhdr = (struct l2_ethhdr *) pkt_data; 1128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (l2->l2_hdr) { 1138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_buf = (u8 *) ethhdr; 1148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_len = hdr->caplen; 1158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } else { 1168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_buf = (u8 *) (ethhdr + 1); 1178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_len = hdr->caplen - sizeof(*ethhdr); 1188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_src = ethhdr->h_source; 1208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SetEvent(l2->rx_avail); 1218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt WaitForSingleObject(l2->rx_done, INFINITE); 1228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ResetEvent(l2->rx_done); 1238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_no_wait = no_wait_count; 1248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 1258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* main RX loop that is running in a separate thread */ 1288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic DWORD WINAPI l2_packet_receive_thread(LPVOID arg) 1298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 1308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct l2_packet_data *l2 = arg; 1318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt while (l2->running) { 1338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt pcap_dispatch(l2->pcap, 1, l2_packet_receive_cb, 1348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt (u_char *) l2); 1358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (l2->rx_no_wait > 0) 1368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_no_wait--; 1378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (WaitForSingleObject(l2->rx_notify, 1388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_no_wait ? 0 : 50) == 1398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt WAIT_OBJECT_0) { 1408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_no_wait = no_wait_count; 1418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ResetEvent(l2->rx_notify); 1428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SetEvent(l2->rx_thread_done); 1458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ExitThread(0); 1468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return 0; 1478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 1488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* main thread RX event handler */ 1518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void l2_packet_rx_event(void *eloop_data, void *user_data) 1528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 1538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct l2_packet_data *l2 = eloop_data; 1548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_callback(l2->rx_callback_ctx, l2->rx_src, l2->rx_buf, 1558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_len); 1568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ResetEvent(l2->rx_avail); 1578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SetEvent(l2->rx_done); 1588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 1598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic int l2_packet_init_libpcap(struct l2_packet_data *l2, 1628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt unsigned short protocol) 1638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 1648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt bpf_u_int32 pcap_maskp, pcap_netp; 1658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE]; 1668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct bpf_program pcap_fp; 1678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err); 1698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 1, pcap_err); 1708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (l2->pcap == NULL) { 1718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt fprintf(stderr, "pcap_open_live: %s\n", pcap_err); 1728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt fprintf(stderr, "ifname='%s'\n", l2->ifname); 1738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 1748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_snprintf(pcap_filter, sizeof(pcap_filter), 1768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "not ether src " MACSTR " and " 1778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "( ether dst " MACSTR " or ether dst " MACSTR " ) and " 1788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "ether proto 0x%x", 1798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt MAC2STR(l2->own_addr), /* do not receive own packets */ 1808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt MAC2STR(l2->own_addr), MAC2STR(pae_group_addr), 1818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt protocol); 1828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) { 1838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap)); 1848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 1858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) { 1888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap)); 1898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 1908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt pcap_freecode(&pcap_fp); 1938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return 0; 1958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 1968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct l2_packet_data * l2_packet_init( 1998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const char *ifname, const u8 *own_addr, unsigned short protocol, 2008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt void (*rx_callback)(void *ctx, const u8 *src_addr, 2018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const u8 *buf, size_t len), 2028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt void *rx_callback_ctx, int l2_hdr) 2038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 2048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct l2_packet_data *l2; 2058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt DWORD thread_id; 2068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2 = os_zalloc(sizeof(struct l2_packet_data)); 2088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (l2 == NULL) 2098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 2108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (os_strncmp(ifname, "\\Device\\NPF_", 12) == 0) 2118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname)); 2128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt else 2138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_snprintf(l2->ifname, sizeof(l2->ifname), "\\Device\\NPF_%s", 2148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ifname); 2158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_callback = rx_callback; 2168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_callback_ctx = rx_callback_ctx; 2178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->l2_hdr = l2_hdr; 2188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (own_addr) 2208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_memcpy(l2->own_addr, own_addr, ETH_ALEN); 2218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (l2_packet_init_libpcap(l2, protocol)) { 2238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(l2); 2248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 2258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_avail = CreateEvent(NULL, TRUE, FALSE, NULL); 2288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_done = CreateEvent(NULL, TRUE, FALSE, NULL); 2298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_notify = CreateEvent(NULL, TRUE, FALSE, NULL); 2308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (l2->rx_avail == NULL || l2->rx_done == NULL || 2318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_notify == NULL) { 2328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt CloseHandle(l2->rx_avail); 2338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt CloseHandle(l2->rx_done); 2348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt CloseHandle(l2->rx_notify); 2358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt pcap_close(l2->pcap); 2368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(l2); 2378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 2388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt eloop_register_event(l2->rx_avail, sizeof(l2->rx_avail), 2418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2_packet_rx_event, l2, NULL); 2428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->running = 1; 2448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_thread = CreateThread(NULL, 0, l2_packet_receive_thread, l2, 0, 2458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt &thread_id); 2468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return l2; 2488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 2498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void l2_packet_deinit_timeout(void *eloop_ctx, void *timeout_ctx) 2528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 2538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct l2_packet_data *l2 = eloop_ctx; 2548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (l2->rx_thread_done && 2568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt WaitForSingleObject(l2->rx_thread_done, 2000) != WAIT_OBJECT_0) { 2578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt wpa_printf(MSG_DEBUG, "l2_packet_winpcap: RX thread did not " 2588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "exit - kill it\n"); 2598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt TerminateThread(l2->rx_thread, 0); 2608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt CloseHandle(l2->rx_thread_done); 2628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt CloseHandle(l2->rx_thread); 2638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (l2->pcap) 2648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt pcap_close(l2->pcap); 2658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt eloop_unregister_event(l2->rx_avail, sizeof(l2->rx_avail)); 2668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt CloseHandle(l2->rx_avail); 2678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt CloseHandle(l2->rx_done); 2688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt CloseHandle(l2->rx_notify); 2698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_free(l2); 2708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 2718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid l2_packet_deinit(struct l2_packet_data *l2) 2748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 2758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (l2 == NULL) 2768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return; 2778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->rx_thread_done = CreateEvent(NULL, TRUE, FALSE, NULL); 2798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt l2->running = 0; 2818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt pcap_breakloop(l2->pcap); 2828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* 2848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * RX thread may be waiting in l2_packet_receive_cb() for l2->rx_done 2858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * event and this event is set in l2_packet_rx_event(). However, 2868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * l2_packet_deinit() may end up being called from l2->rx_callback(), 2878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * so we need to return from here and complete deinitialization in 2888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * a registered timeout to avoid having to forcefully kill the RX 2898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * thread. 2908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 2918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt eloop_register_timeout(0, 0, l2_packet_deinit_timeout, l2, NULL); 2928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 2938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len) 2968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 2978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt pcap_if_t *devs, *dev; 2988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct pcap_addr *addr; 2998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct sockaddr_in *saddr; 3008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt int found = 0; 3018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt char err[PCAP_ERRBUF_SIZE + 1]; 3028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (pcap_findalldevs(&devs, err) < 0) { 3048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err); 3058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return -1; 3068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 3078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt for (dev = devs; dev && !found; dev = dev->next) { 3098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (os_strcmp(dev->name, l2->ifname) != 0) 3108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt continue; 3118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt addr = dev->addresses; 3138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt while (addr) { 3148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt saddr = (struct sockaddr_in *) addr->addr; 3158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (saddr && saddr->sin_family == AF_INET) { 3168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os_strlcpy(buf, inet_ntoa(saddr->sin_addr), 3178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt len); 3188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt found = 1; 3198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt break; 3208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 3218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt addr = addr->next; 3228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 3238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 3248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt pcap_freealldevs(devs); 3268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return found ? 0 : -1; 3288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 3298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid l2_packet_notify_auth_start(struct l2_packet_data *l2) 3328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 3338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (l2) 3348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt SetEvent(l2->rx_notify); 3358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 336