l2_packet_pcap.c 9.4 KB

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