l2_packet_pcap.c 9.4 KB

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