l2_packet_freebsd.c
Go to the documentation of this file.
00001 /*
00002  * WPA Supplicant - Layer2 packet handling with FreeBSD
00003  * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
00004  * Copyright (c) 2005, Sam Leffler <sam@errno.com>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License version 2 as
00008  * published by the Free Software Foundation.
00009  *
00010  * Alternatively, this software may be distributed under the terms of BSD
00011  * license.
00012  *
00013  * See README and COPYING for more details.
00014  */
00015 
00016 #include "includes.h"
00017 #if defined(__APPLE__) || defined(__GLIBC__)
00018 #include <net/bpf.h>
00019 #endif /* __APPLE__ */
00020 #include <pcap.h>
00021 
00022 #include <sys/ioctl.h>
00023 #include <sys/sysctl.h>
00024 
00025 #include <net/if.h>
00026 #include <net/if_dl.h>
00027 #include <net/route.h>
00028 #include <netinet/in.h>
00029 
00030 #include "common.h"
00031 #include "eloop.h"
00032 #include "l2_packet.h"
00033 
00034 
00035 static const u8 pae_group_addr[ETH_ALEN] =
00036 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
00037 
00038 struct l2_packet_data {
00039         pcap_t *pcap;
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 data
00046                      * buffers */
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 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
00058                    const u8 *buf, size_t len)
00059 {
00060         if (!l2->l2_hdr) {
00061                 int ret;
00062                 struct l2_ethhdr *eth = os_malloc(sizeof(*eth) + len);
00063                 if (eth == NULL)
00064                         return -1;
00065                 os_memcpy(eth->h_dest, dst_addr, ETH_ALEN);
00066                 os_memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
00067                 eth->h_proto = htons(proto);
00068                 os_memcpy(eth + 1, buf, len);
00069                 ret = pcap_inject(l2->pcap, (u8 *) eth, len + sizeof(*eth));
00070                 os_free(eth);
00071                 return ret;
00072         } else
00073                 return pcap_inject(l2->pcap, buf, len);
00074 }
00075 
00076 
00077 static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
00078 {
00079         struct l2_packet_data *l2 = eloop_ctx;
00080         pcap_t *pcap = sock_ctx;
00081         struct pcap_pkthdr hdr;
00082         const u_char *packet;
00083         struct l2_ethhdr *ethhdr;
00084         unsigned char *buf;
00085         size_t len;
00086 
00087         packet = pcap_next(pcap, &hdr);
00088 
00089         if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
00090                 return;
00091 
00092         ethhdr = (struct l2_ethhdr *) packet;
00093         if (l2->l2_hdr) {
00094                 buf = (unsigned char *) ethhdr;
00095                 len = hdr.caplen;
00096         } else {
00097                 buf = (unsigned char *) (ethhdr + 1);
00098                 len = hdr.caplen - sizeof(*ethhdr);
00099         }
00100         l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
00101 }
00102 
00103 
00104 static int l2_packet_init_libpcap(struct l2_packet_data *l2,
00105                                   unsigned short protocol)
00106 {
00107         bpf_u_int32 pcap_maskp, pcap_netp;
00108         char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE];
00109         struct bpf_program pcap_fp;
00110 
00111         pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
00112         l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
00113         if (l2->pcap == NULL) {
00114                 fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
00115                 fprintf(stderr, "ifname='%s'\n", l2->ifname);
00116                 return -1;
00117         }
00118         if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
00119             pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
00120                 fprintf(stderr, "pcap_set_datalink(DLT_EN10MB): %s\n",
00121                         pcap_geterr(l2->pcap));
00122                 return -1;
00123         }
00124         os_snprintf(pcap_filter, sizeof(pcap_filter),
00125                     "not ether src " MACSTR " and "
00126                     "( ether dst " MACSTR " or ether dst " MACSTR " ) and "
00127                     "ether proto 0x%x",
00128                     MAC2STR(l2->own_addr), /* do not receive own packets */
00129                     MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
00130                     protocol);
00131         if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
00132                 fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
00133                 return -1;
00134         }
00135 
00136         if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
00137                 fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
00138                 return -1;
00139         }
00140 
00141         pcap_freecode(&pcap_fp);
00142         /*
00143          * When libpcap uses BPF we must enable "immediate mode" to
00144          * receive frames right away; otherwise the system may
00145          * buffer them for us.
00146          */
00147         {
00148                 unsigned int on = 1;
00149                 if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
00150                         fprintf(stderr, "%s: cannot enable immediate mode on "
00151                                 "interface %s: %s\n",
00152                                 __func__, l2->ifname, strerror(errno));
00153                         /* XXX should we fail? */
00154                 }
00155         }
00156 
00157         eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
00158                                  l2_packet_receive, l2, l2->pcap);
00159 
00160         return 0;
00161 }
00162 
00163 
00164 static int eth_get(const char *device, u8 ea[ETH_ALEN])
00165 {
00166         struct if_msghdr *ifm;
00167         struct sockaddr_dl *sdl;
00168         u_char *p, *buf;
00169         size_t len;
00170         int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
00171 
00172         if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
00173                 return -1;
00174         if ((buf = os_malloc(len)) == NULL)
00175                 return -1;
00176         if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
00177                 os_free(buf);
00178                 return -1;
00179         }
00180         for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
00181                 ifm = (struct if_msghdr *)p;
00182                 sdl = (struct sockaddr_dl *)(ifm + 1);
00183                 if (ifm->ifm_type != RTM_IFINFO ||
00184                     (ifm->ifm_addrs & RTA_IFP) == 0)
00185                         continue;
00186                 if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
00187                     os_memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
00188                         continue;
00189                 os_memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
00190                 break;
00191         }
00192         os_free(buf);
00193 
00194         if (p >= buf + len) {
00195                 errno = ESRCH;
00196                 return -1;
00197         }
00198         return 0;
00199 }
00200 
00201 
00202 struct l2_packet_data * l2_packet_init(
00203         const char *ifname, const u8 *own_addr, unsigned short protocol,
00204         void (*rx_callback)(void *ctx, const u8 *src_addr,
00205                             const u8 *buf, size_t len),
00206         void *rx_callback_ctx, int l2_hdr)
00207 {
00208         struct l2_packet_data *l2;
00209 
00210         l2 = os_zalloc(sizeof(struct l2_packet_data));
00211         if (l2 == NULL)
00212                 return NULL;
00213         os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
00214         l2->rx_callback = rx_callback;
00215         l2->rx_callback_ctx = rx_callback_ctx;
00216         l2->l2_hdr = l2_hdr;
00217 
00218         if (eth_get(l2->ifname, l2->own_addr) < 0) {
00219                 fprintf(stderr, "Failed to get link-level address for "
00220                         "interface '%s'.\n", l2->ifname);
00221                 os_free(l2);
00222                 return NULL;
00223         }
00224 
00225         if (l2_packet_init_libpcap(l2, protocol)) {
00226                 os_free(l2);
00227                 return NULL;
00228         }
00229 
00230         return l2;
00231 }
00232 
00233 
00234 void l2_packet_deinit(struct l2_packet_data *l2)
00235 {
00236         if (l2 != NULL) {
00237                 if (l2->pcap) {
00238                         eloop_unregister_read_sock(
00239                                 pcap_get_selectable_fd(l2->pcap));
00240                         pcap_close(l2->pcap);
00241                 }
00242                 os_free(l2);
00243         }
00244 }
00245 
00246 
00247 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
00248 {
00249         pcap_if_t *devs, *dev;
00250         struct pcap_addr *addr;
00251         struct sockaddr_in *saddr;
00252         int found = 0;
00253         char err[PCAP_ERRBUF_SIZE + 1];
00254 
00255         if (pcap_findalldevs(&devs, err) < 0) {
00256                 wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
00257                 return -1;
00258         }
00259 
00260         for (dev = devs; dev && !found; dev = dev->next) {
00261                 if (os_strcmp(dev->name, l2->ifname) != 0)
00262                         continue;
00263 
00264                 addr = dev->addresses;
00265                 while (addr) {
00266                         saddr = (struct sockaddr_in *) addr->addr;
00267                         if (saddr && saddr->sin_family == AF_INET) {
00268                                 os_strlcpy(buf, inet_ntoa(saddr->sin_addr),
00269                                            len);
00270                                 found = 1;
00271                                 break;
00272                         }
00273                         addr = addr->next;
00274                 }
00275         }
00276 
00277         pcap_freealldevs(devs);
00278 
00279         return found ? 0 : -1;
00280 }
00281 
00282 
00283 void l2_packet_notify_auth_start(struct l2_packet_data *l2)
00284 {
00285 }


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:34:35