l2_packet_pcap.c
Go to the documentation of this file.
00001 /*
00002  * WPA Supplicant - Layer2 packet handling with libpcap/libdnet and WinPcap
00003  * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "includes.h"
00016 #ifndef CONFIG_NATIVE_WINDOWS
00017 #include <sys/ioctl.h>
00018 #endif /* CONFIG_NATIVE_WINDOWS */
00019 #include <pcap.h>
00020 #ifndef CONFIG_WINPCAP
00021 #include <dnet.h>
00022 #endif /* CONFIG_WINPCAP */
00023 
00024 #include "common.h"
00025 #include "eloop.h"
00026 #include "l2_packet.h"
00027 
00028 
00029 static const u8 pae_group_addr[ETH_ALEN] =
00030 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
00031 
00032 struct l2_packet_data {
00033         pcap_t *pcap;
00034 #ifdef CONFIG_WINPCAP
00035         unsigned int num_fast_poll;
00036 #else /* CONFIG_WINPCAP */
00037         eth_t *eth;
00038 #endif /* CONFIG_WINPCAP */
00039         char ifname[100];
00040         u8 own_addr[ETH_ALEN];
00041         void (*rx_callback)(void *ctx, const u8 *src_addr,
00042                             const u8 *buf, size_t len);
00043         void *rx_callback_ctx;
00044         int l2_hdr; /* whether to include layer 2 (Ethernet) header in calls
00045                         * to rx_callback */
00046 };
00047 
00048 
00049 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
00050 {
00051         os_memcpy(addr, l2->own_addr, ETH_ALEN);
00052         return 0;
00053 }
00054 
00055 
00056 #ifndef CONFIG_WINPCAP
00057 static int l2_packet_init_libdnet(struct l2_packet_data *l2)
00058 {
00059         eth_addr_t own_addr;
00060 
00061         l2->eth = eth_open(l2->ifname);
00062         if (!l2->eth) {
00063                 printf("Failed to open interface '%s'.\n", l2->ifname);
00064                 perror("eth_open");
00065                 return -1;
00066         }
00067 
00068         if (eth_get(l2->eth, &own_addr) < 0) {
00069                 printf("Failed to get own hw address from interface '%s'.\n",
00070                        l2->ifname);
00071                 perror("eth_get");
00072                 eth_close(l2->eth);
00073                 l2->eth = NULL;
00074                 return -1;
00075         }
00076         os_memcpy(l2->own_addr, own_addr.data, ETH_ALEN);
00077 
00078         return 0;
00079 }
00080 #endif /* CONFIG_WINPCAP */
00081 
00082 
00083 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
00084                    const u8 *buf, size_t len)
00085 {
00086         int ret;
00087         struct l2_ethhdr *eth;
00088 
00089         if (l2 == NULL)
00090                 return -1;
00091 
00092         if (l2->l2_hdr) {
00093 #ifdef CONFIG_WINPCAP
00094                 ret = pcap_sendpacket(l2->pcap, buf, len);
00095 #else /* CONFIG_WINPCAP */
00096                 ret = eth_send(l2->eth, buf, len);
00097 #endif /* CONFIG_WINPCAP */
00098         } else {
00099                 size_t mlen = sizeof(*eth) + len;
00100                 eth = os_malloc(mlen);
00101                 if (eth == NULL)
00102                         return -1;
00103 
00104                 os_memcpy(eth->h_dest, dst_addr, ETH_ALEN);
00105                 os_memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
00106                 eth->h_proto = htons(proto);
00107                 os_memcpy(eth + 1, buf, len);
00108 
00109 #ifdef CONFIG_WINPCAP
00110                 ret = pcap_sendpacket(l2->pcap, (u8 *) eth, mlen);
00111 #else /* CONFIG_WINPCAP */
00112                 ret = eth_send(l2->eth, (u8 *) eth, mlen);
00113 #endif /* CONFIG_WINPCAP */
00114 
00115                 os_free(eth);
00116         }
00117 
00118         return ret;
00119 }
00120 
00121 
00122 #ifndef CONFIG_WINPCAP
00123 static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
00124 {
00125         struct l2_packet_data *l2 = eloop_ctx;
00126         pcap_t *pcap = sock_ctx;
00127         struct pcap_pkthdr hdr;
00128         const u_char *packet;
00129         struct l2_ethhdr *ethhdr;
00130         unsigned char *buf;
00131         size_t len;
00132 
00133         packet = pcap_next(pcap, &hdr);
00134 
00135         if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
00136                 return;
00137 
00138         ethhdr = (struct l2_ethhdr *) packet;
00139         if (l2->l2_hdr) {
00140                 buf = (unsigned char *) ethhdr;
00141                 len = hdr.caplen;
00142         } else {
00143                 buf = (unsigned char *) (ethhdr + 1);
00144                 len = hdr.caplen - sizeof(*ethhdr);
00145         }
00146         l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
00147 }
00148 #endif /* CONFIG_WINPCAP */
00149 
00150 
00151 #ifdef CONFIG_WINPCAP
00152 static void l2_packet_receive_cb(u_char *user, const struct pcap_pkthdr *hdr,
00153                                  const u_char *pkt_data)
00154 {
00155         struct l2_packet_data *l2 = (struct l2_packet_data *) user;
00156         struct l2_ethhdr *ethhdr;
00157         unsigned char *buf;
00158         size_t len;
00159 
00160         if (pkt_data == NULL || hdr->caplen < sizeof(*ethhdr))
00161                 return;
00162 
00163         ethhdr = (struct l2_ethhdr *) pkt_data;
00164         if (l2->l2_hdr) {
00165                 buf = (unsigned char *) ethhdr;
00166                 len = hdr->caplen;
00167         } else {
00168                 buf = (unsigned char *) (ethhdr + 1);
00169                 len = hdr->caplen - sizeof(*ethhdr);
00170         }
00171         l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
00172         /*
00173          * Use shorter poll interval for 3 seconds to reduce latency during key
00174          * handshake.
00175          */
00176         l2->num_fast_poll = 3 * 50;
00177 }
00178 
00179 
00180 static void l2_packet_receive_timeout(void *eloop_ctx, void *timeout_ctx)
00181 {
00182         struct l2_packet_data *l2 = eloop_ctx;
00183         pcap_t *pcap = timeout_ctx;
00184         int timeout;
00185 
00186         if (l2->num_fast_poll > 0) {
00187                 timeout = 20000;
00188                 l2->num_fast_poll--;
00189         } else
00190                 timeout = 100000;
00191 
00192         /* Register new timeout before calling l2_packet_receive() since
00193          * receive handler may free this l2_packet instance (which will
00194          * cancel this timeout). */
00195         eloop_register_timeout(0, timeout, l2_packet_receive_timeout,
00196                                l2, pcap);
00197         pcap_dispatch(pcap, 10, l2_packet_receive_cb, (u_char *) l2);
00198 }
00199 #endif /* CONFIG_WINPCAP */
00200 
00201 
00202 static int l2_packet_init_libpcap(struct l2_packet_data *l2,
00203                                   unsigned short protocol)
00204 {
00205         bpf_u_int32 pcap_maskp, pcap_netp;
00206         char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE];
00207         struct bpf_program pcap_fp;
00208 
00209 #ifdef CONFIG_WINPCAP
00210         char ifname[128];
00211         os_snprintf(ifname, sizeof(ifname), "\\Device\\NPF_%s", l2->ifname);
00212         pcap_lookupnet(ifname, &pcap_netp, &pcap_maskp, pcap_err);
00213         l2->pcap = pcap_open_live(ifname, 2500, 0, 10, pcap_err);
00214         if (l2->pcap == NULL) {
00215                 fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
00216                 fprintf(stderr, "ifname='%s'\n", ifname);
00217                 return -1;
00218         }
00219         if (pcap_setnonblock(l2->pcap, 1, pcap_err) < 0)
00220                 fprintf(stderr, "pcap_setnonblock: %s\n",
00221                         pcap_geterr(l2->pcap));
00222 #else /* CONFIG_WINPCAP */
00223         pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
00224         l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
00225         if (l2->pcap == NULL) {
00226                 fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
00227                 fprintf(stderr, "ifname='%s'\n", l2->ifname);
00228                 return -1;
00229         }
00230         if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
00231             pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
00232                 fprintf(stderr, "pcap_set_datalink(DLT_EN10MB): %s\n",
00233                         pcap_geterr(l2->pcap));
00234                 return -1;
00235         }
00236 #endif /* CONFIG_WINPCAP */
00237         os_snprintf(pcap_filter, sizeof(pcap_filter),
00238                     "not ether src " MACSTR " and "
00239                     "( ether dst " MACSTR " or ether dst " MACSTR " ) and "
00240                     "ether proto 0x%x",
00241                     MAC2STR(l2->own_addr), /* do not receive own packets */
00242                     MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
00243                     protocol);
00244         if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
00245                 fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
00246                 return -1;
00247         }
00248 
00249         if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
00250                 fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
00251                 return -1;
00252         }
00253 
00254         pcap_freecode(&pcap_fp);
00255 #ifdef BIOCIMMEDIATE
00256         /*
00257          * When libpcap uses BPF we must enable "immediate mode" to
00258          * receive frames right away; otherwise the system may
00259          * buffer them for us.
00260          */
00261         {
00262                 unsigned int on = 1;
00263                 if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
00264                         fprintf(stderr, "%s: cannot enable immediate mode on "
00265                                 "interface %s: %s\n",
00266                                 __func__, l2->ifname, strerror(errno));
00267                         /* XXX should we fail? */
00268                 }
00269         }
00270 #endif /* BIOCIMMEDIATE */
00271 
00272 #ifdef CONFIG_WINPCAP
00273         eloop_register_timeout(0, 100000, l2_packet_receive_timeout,
00274                                l2, l2->pcap);
00275 #else /* CONFIG_WINPCAP */
00276         eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
00277                                  l2_packet_receive, l2, l2->pcap);
00278 #endif /* CONFIG_WINPCAP */
00279 
00280         return 0;
00281 }
00282 
00283 
00284 struct l2_packet_data * l2_packet_init(
00285         const char *ifname, const u8 *own_addr, unsigned short protocol,
00286         void (*rx_callback)(void *ctx, const u8 *src_addr,
00287                             const u8 *buf, size_t len),
00288         void *rx_callback_ctx, int l2_hdr)
00289 {
00290         struct l2_packet_data *l2;
00291 
00292         l2 = os_zalloc(sizeof(struct l2_packet_data));
00293         if (l2 == NULL)
00294                 return NULL;
00295         os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
00296         l2->rx_callback = rx_callback;
00297         l2->rx_callback_ctx = rx_callback_ctx;
00298         l2->l2_hdr = l2_hdr;
00299 
00300 #ifdef CONFIG_WINPCAP
00301         if (own_addr)
00302                 os_memcpy(l2->own_addr, own_addr, ETH_ALEN);
00303 #else /* CONFIG_WINPCAP */
00304         if (l2_packet_init_libdnet(l2))
00305                 return NULL;
00306 #endif /* CONFIG_WINPCAP */
00307 
00308         if (l2_packet_init_libpcap(l2, protocol)) {
00309 #ifndef CONFIG_WINPCAP
00310                 eth_close(l2->eth);
00311 #endif /* CONFIG_WINPCAP */
00312                 os_free(l2);
00313                 return NULL;
00314         }
00315 
00316         return l2;
00317 }
00318 
00319 
00320 void l2_packet_deinit(struct l2_packet_data *l2)
00321 {
00322         if (l2 == NULL)
00323                 return;
00324 
00325 #ifdef CONFIG_WINPCAP
00326         eloop_cancel_timeout(l2_packet_receive_timeout, l2, l2->pcap);
00327 #else /* CONFIG_WINPCAP */
00328         if (l2->eth)
00329                 eth_close(l2->eth);
00330         eloop_unregister_read_sock(pcap_get_selectable_fd(l2->pcap));
00331 #endif /* CONFIG_WINPCAP */
00332         if (l2->pcap)
00333                 pcap_close(l2->pcap);
00334         os_free(l2);
00335 }
00336 
00337 
00338 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
00339 {
00340         pcap_if_t *devs, *dev;
00341         struct pcap_addr *addr;
00342         struct sockaddr_in *saddr;
00343         int found = 0;
00344         char err[PCAP_ERRBUF_SIZE + 1];
00345 
00346         if (pcap_findalldevs(&devs, err) < 0) {
00347                 wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
00348                 return -1;
00349         }
00350 
00351         for (dev = devs; dev && !found; dev = dev->next) {
00352                 if (os_strcmp(dev->name, l2->ifname) != 0)
00353                         continue;
00354 
00355                 addr = dev->addresses;
00356                 while (addr) {
00357                         saddr = (struct sockaddr_in *) addr->addr;
00358                         if (saddr && saddr->sin_family == AF_INET) {
00359                                 os_strlcpy(buf, inet_ntoa(saddr->sin_addr),
00360                                            len);
00361                                 found = 1;
00362                                 break;
00363                         }
00364                         addr = addr->next;
00365                 }
00366         }
00367 
00368         pcap_freealldevs(devs);
00369 
00370         return found ? 0 : -1;
00371 }
00372 
00373 
00374 void l2_packet_notify_auth_start(struct l2_packet_data *l2)
00375 {
00376 #ifdef CONFIG_WINPCAP
00377         /*
00378          * Use shorter poll interval for 3 seconds to reduce latency during key
00379          * handshake.
00380          */
00381         l2->num_fast_poll = 3 * 50;
00382         eloop_cancel_timeout(l2_packet_receive_timeout, l2, l2->pcap);
00383         eloop_register_timeout(0, 10000, l2_packet_receive_timeout,
00384                                l2, l2->pcap);
00385 #endif /* CONFIG_WINPCAP */
00386 }


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Jan 2 2014 11:26:38