l2_packet_pcap.c 9.2 KB

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