l2_packet_winpcap.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * WPA Supplicant - Layer2 packet handling with WinPcap RX thread
  3. * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. *
  14. * This l2_packet implementation is explicitly for WinPcap and Windows events.
  15. * l2_packet_pcap.c has support for WinPcap, but it requires polling to receive
  16. * frames which means relatively long latency for EAPOL RX processing. The
  17. * implementation here uses a separate thread to allow WinPcap to be receiving
  18. * all the time to reduce latency for EAPOL receiving from about 100 ms to 3 ms
  19. * when comparing l2_packet_pcap.c to l2_packet_winpcap.c. Extra sleep of 50 ms
  20. * is added in to receive thread whenever no EAPOL frames has been received for
  21. * a while. Whenever an EAPOL handshake is expected, this sleep is removed.
  22. *
  23. * The RX thread receives a frame and signals main thread through Windows event
  24. * about the availability of a new frame. Processing the received frame is
  25. * synchronized with pair of Windows events so that no extra buffer or queuing
  26. * mechanism is needed. This implementation requires Windows specific event
  27. * loop implementation, i.e., eloop_win.c.
  28. *
  29. * WinPcap has pcap_getevent() that could, in theory at least, be used to
  30. * implement this kind of waiting with a simpler single-thread design. However,
  31. * that event handle is not really signaled immediately when receiving each
  32. * frame, so it does not really work for this kind of use.
  33. */
  34. #include "includes.h"
  35. #include <pcap.h>
  36. #include "common.h"
  37. #include "eloop.h"
  38. #include "l2_packet.h"
  39. static const u8 pae_group_addr[ETH_ALEN] =
  40. { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
  41. /*
  42. * Number of pcap_dispatch() iterations to do without extra wait after each
  43. * received EAPOL packet or authentication notification. This is used to reduce
  44. * latency for EAPOL receive.
  45. */
  46. static const size_t no_wait_count = 750;
  47. struct l2_packet_data {
  48. pcap_t *pcap;
  49. unsigned int num_fast_poll;
  50. char ifname[100];
  51. u8 own_addr[ETH_ALEN];
  52. void (*rx_callback)(void *ctx, const u8 *src_addr,
  53. const u8 *buf, size_t len);
  54. void *rx_callback_ctx;
  55. int l2_hdr; /* whether to include layer 2 (Ethernet) header in calls to
  56. * rx_callback and l2_packet_send() */
  57. int running;
  58. HANDLE rx_avail, rx_done, rx_thread, rx_thread_done, rx_notify;
  59. u8 *rx_buf, *rx_src;
  60. size_t rx_len;
  61. size_t rx_no_wait;
  62. };
  63. int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
  64. {
  65. os_memcpy(addr, l2->own_addr, ETH_ALEN);
  66. return 0;
  67. }
  68. int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
  69. const u8 *buf, size_t len)
  70. {
  71. int ret;
  72. struct l2_ethhdr *eth;
  73. if (l2 == NULL)
  74. return -1;
  75. if (l2->l2_hdr) {
  76. ret = pcap_sendpacket(l2->pcap, buf, len);
  77. } else {
  78. size_t mlen = sizeof(*eth) + len;
  79. eth = os_malloc(mlen);
  80. if (eth == NULL)
  81. return -1;
  82. os_memcpy(eth->h_dest, dst_addr, ETH_ALEN);
  83. os_memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
  84. eth->h_proto = htons(proto);
  85. os_memcpy(eth + 1, buf, len);
  86. ret = pcap_sendpacket(l2->pcap, (u8 *) eth, mlen);
  87. os_free(eth);
  88. }
  89. return ret;
  90. }
  91. /* pcap_dispatch() callback for the RX thread */
  92. static void l2_packet_receive_cb(u_char *user, const struct pcap_pkthdr *hdr,
  93. const u_char *pkt_data)
  94. {
  95. struct l2_packet_data *l2 = (struct l2_packet_data *) user;
  96. struct l2_ethhdr *ethhdr;
  97. if (pkt_data == NULL || hdr->caplen < sizeof(*ethhdr))
  98. return;
  99. ethhdr = (struct l2_ethhdr *) pkt_data;
  100. if (l2->l2_hdr) {
  101. l2->rx_buf = (u8 *) ethhdr;
  102. l2->rx_len = hdr->caplen;
  103. } else {
  104. l2->rx_buf = (u8 *) (ethhdr + 1);
  105. l2->rx_len = hdr->caplen - sizeof(*ethhdr);
  106. }
  107. l2->rx_src = ethhdr->h_source;
  108. SetEvent(l2->rx_avail);
  109. WaitForSingleObject(l2->rx_done, INFINITE);
  110. ResetEvent(l2->rx_done);
  111. l2->rx_no_wait = no_wait_count;
  112. }
  113. /* main RX loop that is running in a separate thread */
  114. static DWORD WINAPI l2_packet_receive_thread(LPVOID arg)
  115. {
  116. struct l2_packet_data *l2 = arg;
  117. while (l2->running) {
  118. pcap_dispatch(l2->pcap, 1, l2_packet_receive_cb,
  119. (u_char *) l2);
  120. if (l2->rx_no_wait > 0)
  121. l2->rx_no_wait--;
  122. if (WaitForSingleObject(l2->rx_notify,
  123. l2->rx_no_wait ? 0 : 50) ==
  124. WAIT_OBJECT_0) {
  125. l2->rx_no_wait = no_wait_count;
  126. ResetEvent(l2->rx_notify);
  127. }
  128. }
  129. SetEvent(l2->rx_thread_done);
  130. ExitThread(0);
  131. return 0;
  132. }
  133. /* main thread RX event handler */
  134. static void l2_packet_rx_event(void *eloop_data, void *user_data)
  135. {
  136. struct l2_packet_data *l2 = eloop_data;
  137. l2->rx_callback(l2->rx_callback_ctx, l2->rx_src, l2->rx_buf,
  138. l2->rx_len);
  139. ResetEvent(l2->rx_avail);
  140. SetEvent(l2->rx_done);
  141. }
  142. static int l2_packet_init_libpcap(struct l2_packet_data *l2,
  143. unsigned short protocol)
  144. {
  145. bpf_u_int32 pcap_maskp, pcap_netp;
  146. char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE];
  147. struct bpf_program pcap_fp;
  148. pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
  149. l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 1, pcap_err);
  150. if (l2->pcap == NULL) {
  151. fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
  152. fprintf(stderr, "ifname='%s'\n", l2->ifname);
  153. return -1;
  154. }
  155. os_snprintf(pcap_filter, sizeof(pcap_filter),
  156. "not ether src " MACSTR " and "
  157. "( ether dst " MACSTR " or ether dst " MACSTR " ) and "
  158. "ether proto 0x%x",
  159. MAC2STR(l2->own_addr), /* do not receive own packets */
  160. MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
  161. protocol);
  162. if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
  163. fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
  164. return -1;
  165. }
  166. if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
  167. fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
  168. return -1;
  169. }
  170. pcap_freecode(&pcap_fp);
  171. return 0;
  172. }
  173. struct l2_packet_data * l2_packet_init(
  174. const char *ifname, const u8 *own_addr, unsigned short protocol,
  175. void (*rx_callback)(void *ctx, const u8 *src_addr,
  176. const u8 *buf, size_t len),
  177. void *rx_callback_ctx, int l2_hdr)
  178. {
  179. struct l2_packet_data *l2;
  180. DWORD thread_id;
  181. l2 = os_zalloc(sizeof(struct l2_packet_data));
  182. if (l2 == NULL)
  183. return NULL;
  184. if (os_strncmp(ifname, "\\Device\\NPF_", 12) == 0)
  185. os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
  186. else
  187. os_snprintf(l2->ifname, sizeof(l2->ifname), "\\Device\\NPF_%s",
  188. ifname);
  189. l2->rx_callback = rx_callback;
  190. l2->rx_callback_ctx = rx_callback_ctx;
  191. l2->l2_hdr = l2_hdr;
  192. if (own_addr)
  193. os_memcpy(l2->own_addr, own_addr, ETH_ALEN);
  194. if (l2_packet_init_libpcap(l2, protocol)) {
  195. os_free(l2);
  196. return NULL;
  197. }
  198. l2->rx_avail = CreateEvent(NULL, TRUE, FALSE, NULL);
  199. l2->rx_done = CreateEvent(NULL, TRUE, FALSE, NULL);
  200. l2->rx_notify = CreateEvent(NULL, TRUE, FALSE, NULL);
  201. if (l2->rx_avail == NULL || l2->rx_done == NULL ||
  202. l2->rx_notify == NULL) {
  203. CloseHandle(l2->rx_avail);
  204. CloseHandle(l2->rx_done);
  205. CloseHandle(l2->rx_notify);
  206. pcap_close(l2->pcap);
  207. os_free(l2);
  208. return NULL;
  209. }
  210. eloop_register_event(l2->rx_avail, sizeof(l2->rx_avail),
  211. l2_packet_rx_event, l2, NULL);
  212. l2->running = 1;
  213. l2->rx_thread = CreateThread(NULL, 0, l2_packet_receive_thread, l2, 0,
  214. &thread_id);
  215. return l2;
  216. }
  217. static void l2_packet_deinit_timeout(void *eloop_ctx, void *timeout_ctx)
  218. {
  219. struct l2_packet_data *l2 = eloop_ctx;
  220. if (l2->rx_thread_done &&
  221. WaitForSingleObject(l2->rx_thread_done, 2000) != WAIT_OBJECT_0) {
  222. wpa_printf(MSG_DEBUG, "l2_packet_winpcap: RX thread did not "
  223. "exit - kill it\n");
  224. TerminateThread(l2->rx_thread, 0);
  225. }
  226. CloseHandle(l2->rx_thread_done);
  227. CloseHandle(l2->rx_thread);
  228. if (l2->pcap)
  229. pcap_close(l2->pcap);
  230. eloop_unregister_event(l2->rx_avail, sizeof(l2->rx_avail));
  231. CloseHandle(l2->rx_avail);
  232. CloseHandle(l2->rx_done);
  233. CloseHandle(l2->rx_notify);
  234. os_free(l2);
  235. }
  236. void l2_packet_deinit(struct l2_packet_data *l2)
  237. {
  238. if (l2 == NULL)
  239. return;
  240. l2->rx_thread_done = CreateEvent(NULL, TRUE, FALSE, NULL);
  241. l2->running = 0;
  242. pcap_breakloop(l2->pcap);
  243. /*
  244. * RX thread may be waiting in l2_packet_receive_cb() for l2->rx_done
  245. * event and this event is set in l2_packet_rx_event(). However,
  246. * l2_packet_deinit() may end up being called from l2->rx_callback(),
  247. * so we need to return from here and complete deinitialization in
  248. * a registered timeout to avoid having to forcefully kill the RX
  249. * thread.
  250. */
  251. eloop_register_timeout(0, 0, l2_packet_deinit_timeout, l2, NULL);
  252. }
  253. int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
  254. {
  255. pcap_if_t *devs, *dev;
  256. struct pcap_addr *addr;
  257. struct sockaddr_in *saddr;
  258. int found = 0;
  259. char err[PCAP_ERRBUF_SIZE + 1];
  260. if (pcap_findalldevs(&devs, err) < 0) {
  261. wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
  262. return -1;
  263. }
  264. for (dev = devs; dev && !found; dev = dev->next) {
  265. if (os_strcmp(dev->name, l2->ifname) != 0)
  266. continue;
  267. addr = dev->addresses;
  268. while (addr) {
  269. saddr = (struct sockaddr_in *) addr->addr;
  270. if (saddr && saddr->sin_family == AF_INET) {
  271. os_strlcpy(buf, inet_ntoa(saddr->sin_addr),
  272. len);
  273. found = 1;
  274. break;
  275. }
  276. addr = addr->next;
  277. }
  278. }
  279. pcap_freealldevs(devs);
  280. return found ? 0 : -1;
  281. }
  282. void l2_packet_notify_auth_start(struct l2_packet_data *l2)
  283. {
  284. if (l2)
  285. SetEvent(l2->rx_notify);
  286. }