l2_packet_winpcap.c 9.4 KB

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