l2_packet_pcap.c

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

Generated on Sat Nov 21 23:16:52 2009 for hostapd by  doxygen 1.6.1