l2_packet_pcap.c

Go to the documentation of this file.
00001 
00016 #include <stdlib.h>
00017 #include <stdio.h>
00018 #include <string.h>
00019 #ifndef CONFIG_NATIVE_WINDOWS
00020 #include <sys/ioctl.h>
00021 #endif /* CONFIG_NATIVE_WINDOWS */
00022 #include <errno.h>
00023 #include <pcap.h>
00024 #ifndef CONFIG_WINPCAP
00025 #include <dnet.h>
00026 #endif /* CONFIG_WINPCAP */
00027 #if defined(__linux__) || defined(__CYGWIN__)
00028 #include <arpa/inet.h>
00029 #include <sys/socket.h>
00030 #endif /* __linux__ || __CYGWIN__ */
00031 
00032 #include "common.h"
00033 #include "eloop.h"
00034 #include "l2_packet.h"
00035 
00036 
00037 static const u8 pae_group_addr[ETH_ALEN] =
00038 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
00039 
00040 struct l2_packet_data {
00041         pcap_t *pcap;
00042 #ifdef CONFIG_WINPCAP
00043         unsigned int num_fast_poll;
00044 #else /* CONFIG_WINPCAP */
00045         eth_t *eth;
00046 #endif /* CONFIG_WINPCAP */
00047         char ifname[100];
00048         u8 own_addr[ETH_ALEN];
00049         void (*rx_callback)(void *ctx, const u8 *src_addr,
00050                             const u8 *buf, size_t len);
00051         void *rx_callback_ctx;
00052         int l2_hdr; /* whether to include layer 2 (Ethernet) header in calls
00053                         * to rx_callback */
00054 };
00055 
00056 
00057 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
00058 {
00059         memcpy(addr, l2->own_addr, ETH_ALEN);
00060         return 0;
00061 }
00062 
00063 
00064 #ifndef CONFIG_WINPCAP
00065 static int l2_packet_init_libdnet(struct l2_packet_data *l2)
00066 {
00067         eth_addr_t own_addr;
00068 
00069         l2->eth = eth_open(l2->ifname);
00070         if (!l2->eth) {
00071                 printf("Failed to open interface '%s'.\n", l2->ifname);
00072                 perror("eth_open");
00073                 return -1;
00074         }
00075 
00076         if (eth_get(l2->eth, &own_addr) < 0) {
00077                 printf("Failed to get own hw address from interface '%s'.\n",
00078                        l2->ifname);
00079                 perror("eth_get");
00080                 eth_close(l2->eth);
00081                 l2->eth = NULL;
00082                 return -1;
00083         }
00084         memcpy(l2->own_addr, own_addr.data, ETH_ALEN);
00085 
00086         return 0;
00087 }
00088 #endif /* CONFIG_WINPCAP */
00089 
00090 
00091 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
00092                    const u8 *buf, size_t len)
00093 {
00094         int ret;
00095         struct l2_ethhdr *eth;
00096 
00097         if (l2 == NULL)
00098                 return -1;
00099 
00100         if (l2->l2_hdr) {
00101 #ifdef CONFIG_WINPCAP
00102                 ret = pcap_sendpacket(l2->pcap, buf, len);
00103 #else /* CONFIG_WINPCAP */
00104                 ret = eth_send(l2->eth, buf, len);
00105 #endif /* CONFIG_WINPCAP */
00106         } else {
00107                 size_t mlen = sizeof(*eth) + len;
00108                 eth = malloc(mlen);
00109                 if (eth == NULL)
00110                         return -1;
00111 
00112                 memcpy(eth->h_dest, dst_addr, ETH_ALEN);
00113                 memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
00114                 eth->h_proto = htons(proto);
00115                 memcpy(eth + 1, buf, len);
00116 
00117 #ifdef CONFIG_WINPCAP
00118                 ret = pcap_sendpacket(l2->pcap, (u8 *) eth, mlen);
00119 #else /* CONFIG_WINPCAP */
00120                 ret = eth_send(l2->eth, (u8 *) eth, mlen);
00121 #endif /* CONFIG_WINPCAP */
00122 
00123                 free(eth);
00124         }
00125 
00126         return ret;
00127 }
00128 
00129 
00130 #ifndef CONFIG_WINPCAP
00131 static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
00132 {
00133         struct l2_packet_data *l2 = eloop_ctx;
00134         pcap_t *pcap = sock_ctx;
00135         struct pcap_pkthdr hdr;
00136         const u_char *packet;
00137         struct l2_ethhdr *ethhdr;
00138         unsigned char *buf;
00139         size_t len;
00140 
00141         packet = pcap_next(pcap, &hdr);
00142 
00143         if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
00144                 return;
00145 
00146         ethhdr = (struct l2_ethhdr *) packet;
00147         if (l2->l2_hdr) {
00148                 buf = (unsigned char *) ethhdr;
00149                 len = hdr.caplen;
00150         } else {
00151                 buf = (unsigned char *) (ethhdr + 1);
00152                 len = hdr.caplen - sizeof(*ethhdr);
00153         }
00154         l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
00155 }
00156 #endif /* CONFIG_WINPCAP */
00157 
00158 
00159 #ifdef CONFIG_WINPCAP
00160 static void l2_packet_receive_cb(u_char *user, const struct pcap_pkthdr *hdr,
00161                                  const u_char *pkt_data)
00162 {
00163         struct l2_packet_data *l2 = (struct l2_packet_data *) user;
00164         struct l2_ethhdr *ethhdr;
00165         unsigned char *buf;
00166         size_t len;
00167 
00168         if (pkt_data == NULL || hdr->caplen < sizeof(*ethhdr))
00169                 return;
00170 
00171         ethhdr = (struct l2_ethhdr *) pkt_data;
00172         if (l2->l2_hdr) {
00173                 buf = (unsigned char *) ethhdr;
00174                 len = hdr->caplen;
00175         } else {
00176                 buf = (unsigned char *) (ethhdr + 1);
00177                 len = hdr->caplen - sizeof(*ethhdr);
00178         }
00179         l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
00180         /*
00181          * Use shorter poll interval for 3 seconds to reduce latency during key
00182          * handshake.
00183          */
00184         l2->num_fast_poll = 3 * 50;
00185 }
00186 
00187 
00188 static void l2_packet_receive_timeout(void *eloop_ctx, void *timeout_ctx)
00189 {
00190         struct l2_packet_data *l2 = eloop_ctx;
00191         pcap_t *pcap = timeout_ctx;
00192         int timeout;
00193 
00194         if (l2->num_fast_poll > 0) {
00195                 timeout = 20000;
00196                 l2->num_fast_poll--;
00197         } else
00198                 timeout = 100000;
00199 
00200         /* Register new timeout before calling l2_packet_receive() since
00201          * receive handler may free this l2_packet instance (which will
00202          * cancel this timeout). */
00203         eloop_register_timeout(0, timeout, l2_packet_receive_timeout,
00204                                l2, pcap);
00205         pcap_dispatch(pcap, 10, l2_packet_receive_cb, (u_char *) l2);
00206 }
00207 #endif /* CONFIG_WINPCAP */
00208 
00209 
00210 static int l2_packet_init_libpcap(struct l2_packet_data *l2,
00211                                   unsigned short protocol)
00212 {
00213         bpf_u_int32 pcap_maskp, pcap_netp;
00214         char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE];
00215         struct bpf_program pcap_fp;
00216 
00217         pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
00218         l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
00219         if (l2->pcap == NULL) {
00220                 fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
00221                 fprintf(stderr, "ifname='%s'\n", l2->ifname);
00222                 return -1;
00223         }
00224 #ifdef CONFIG_WINPCAP
00225         if (pcap_setnonblock(l2->pcap, 1, pcap_err) < 0)
00226                 fprintf(stderr, "pcap_setnonblock: %s\n",
00227                         pcap_geterr(l2->pcap));
00228 #else /* CONFIG_WINPCAP */
00229         if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
00230             pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
00231                 fprintf(stderr, "pcap_set_datalinke(DLT_EN10MB): %s\n",
00232                         pcap_geterr(l2->pcap));
00233                 return -1;
00234         }
00235 #endif /* CONFIG_WINPCAP */
00236         snprintf(pcap_filter, sizeof(pcap_filter),
00237                  "not ether src " MACSTR " and "
00238                  "( ether dst " MACSTR " or ether dst " MACSTR " ) and "
00239                  "ether proto 0x%x",
00240                  MAC2STR(l2->own_addr), /* do not receive own packets */
00241                  MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
00242                  protocol);
00243         if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
00244                 fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
00245                 return -1;
00246         }
00247 
00248         if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
00249                 fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
00250                 return -1;
00251         }
00252 
00253         pcap_freecode(&pcap_fp);
00254 #ifdef BIOCIMMEDIATE
00255         /*
00256          * When libpcap uses BPF we must enable "immediate mode" to
00257          * receive frames right away; otherwise the system may
00258          * buffer them for us.
00259          */
00260         {
00261                 unsigned int on = 1;
00262                 if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
00263                         fprintf(stderr, "%s: cannot enable immediate mode on "
00264                                 "interface %s: %s\n",
00265                                 __func__, l2->ifname, strerror(errno));
00266                         /* XXX should we fail? */
00267                 }
00268         }
00269 #endif /* BIOCIMMEDIATE */
00270 
00271 #ifdef CONFIG_WINPCAP
00272         eloop_register_timeout(0, 100000, l2_packet_receive_timeout,
00273                                l2, l2->pcap);
00274 #else /* CONFIG_WINPCAP */
00275         eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
00276                                  l2_packet_receive, l2, l2->pcap);
00277 #endif /* CONFIG_WINPCAP */
00278 
00279         return 0;
00280 }
00281 
00282 
00283 struct l2_packet_data * l2_packet_init(
00284         const char *ifname, const u8 *own_addr, unsigned short protocol,
00285         void (*rx_callback)(void *ctx, const u8 *src_addr,
00286                             const u8 *buf, size_t len),
00287         void *rx_callback_ctx, int l2_hdr)
00288 {
00289         struct l2_packet_data *l2;
00290 
00291         l2 = malloc(sizeof(struct l2_packet_data));
00292         if (l2 == NULL)
00293                 return NULL;
00294         memset(l2, 0, sizeof(*l2));
00295         strncpy(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                 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                 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         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 (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                                 snprintf(buf, len, "%s",
00360                                          inet_ntoa(saddr->sin_addr));
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 }
00387 

Generated on Sat May 6 21:13:37 2006 for wpa_supplicant by  doxygen 1.4.2