driver_hostap.c
Go to the documentation of this file.
00001 /*
00002  * Driver interaction with Linux Host AP driver
00003  * Copyright (c) 2003-2005, 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 #include <sys/ioctl.h>
00017 
00018 #include "wireless_copy.h"
00019 #include "common.h"
00020 #include "driver.h"
00021 #include "driver_wext.h"
00022 #include "eloop.h"
00023 #include "driver_hostap.h"
00024 
00025 
00026 #ifdef HOSTAPD
00027 
00028 #include <net/if_arp.h>
00029 #include <netpacket/packet.h>
00030 
00031 #include "priv_netlink.h"
00032 #include "netlink.h"
00033 #include "linux_ioctl.h"
00034 #include "common/ieee802_11_defs.h"
00035 
00036 
00037 /* MTU to be set for the wlan#ap device; this is mainly needed for IEEE 802.1X
00038  * frames that might be longer than normal default MTU and they are not
00039  * fragmented */
00040 #define HOSTAPD_MTU 2290
00041 
00042 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
00043 
00044 struct hostap_driver_data {
00045         struct hostapd_data *hapd;
00046 
00047         char iface[IFNAMSIZ + 1];
00048         int sock; /* raw packet socket for driver access */
00049         int ioctl_sock; /* socket for ioctl() use */
00050         struct netlink_data *netlink;
00051 
00052         int we_version;
00053 
00054         u8 *generic_ie;
00055         size_t generic_ie_len;
00056         u8 *wps_ie;
00057         size_t wps_ie_len;
00058 };
00059 
00060 
00061 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
00062                          int len);
00063 static int hostap_set_iface_flags(void *priv, int dev_up);
00064 
00065 static void handle_data(struct hostap_driver_data *drv, u8 *buf, size_t len,
00066                         u16 stype)
00067 {
00068         struct ieee80211_hdr *hdr;
00069         u16 fc, ethertype;
00070         u8 *pos, *sa;
00071         size_t left;
00072         union wpa_event_data event;
00073 
00074         if (len < sizeof(struct ieee80211_hdr))
00075                 return;
00076 
00077         hdr = (struct ieee80211_hdr *) buf;
00078         fc = le_to_host16(hdr->frame_control);
00079 
00080         if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
00081                 printf("Not ToDS data frame (fc=0x%04x)\n", fc);
00082                 return;
00083         }
00084 
00085         sa = hdr->addr2;
00086         os_memset(&event, 0, sizeof(event));
00087         event.rx_from_unknown.frame = buf;
00088         event.rx_from_unknown.len = len;
00089         wpa_supplicant_event(drv->hapd, EVENT_RX_FROM_UNKNOWN, &event);
00090 
00091         pos = (u8 *) (hdr + 1);
00092         left = len - sizeof(*hdr);
00093 
00094         if (left < sizeof(rfc1042_header)) {
00095                 printf("Too short data frame\n");
00096                 return;
00097         }
00098 
00099         if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
00100                 printf("Data frame with no RFC1042 header\n");
00101                 return;
00102         }
00103         pos += sizeof(rfc1042_header);
00104         left -= sizeof(rfc1042_header);
00105 
00106         if (left < 2) {
00107                 printf("No ethertype in data frame\n");
00108                 return;
00109         }
00110 
00111         ethertype = WPA_GET_BE16(pos);
00112         pos += 2;
00113         left -= 2;
00114         switch (ethertype) {
00115         case ETH_P_PAE:
00116                 drv_event_eapol_rx(drv->hapd, sa, pos, left);
00117                 break;
00118 
00119         default:
00120                 printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
00121                 break;
00122         }
00123 }
00124 
00125 
00126 static void handle_tx_callback(struct hostap_driver_data *drv, u8 *buf,
00127                                size_t len, int ok)
00128 {
00129         struct ieee80211_hdr *hdr;
00130         u16 fc;
00131         union wpa_event_data event;
00132 
00133         hdr = (struct ieee80211_hdr *) buf;
00134         fc = le_to_host16(hdr->frame_control);
00135 
00136         os_memset(&event, 0, sizeof(event));
00137         event.tx_status.type = WLAN_FC_GET_TYPE(fc);
00138         event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
00139         event.tx_status.dst = hdr->addr1;
00140         event.tx_status.data = buf;
00141         event.tx_status.data_len = len;
00142         event.tx_status.ack = ok;
00143         wpa_supplicant_event(drv->hapd, EVENT_TX_STATUS, &event);
00144 }
00145 
00146 
00147 static void handle_frame(struct hostap_driver_data *drv, u8 *buf, size_t len)
00148 {
00149         struct ieee80211_hdr *hdr;
00150         u16 fc, extra_len, type, stype;
00151         unsigned char *extra = NULL;
00152         size_t data_len = len;
00153         int ver;
00154         union wpa_event_data event;
00155 
00156         /* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
00157          * these to user space */
00158         if (len < 24) {
00159                 wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
00160                            (unsigned long) len);
00161                 return;
00162         }
00163 
00164         hdr = (struct ieee80211_hdr *) buf;
00165         fc = le_to_host16(hdr->frame_control);
00166         type = WLAN_FC_GET_TYPE(fc);
00167         stype = WLAN_FC_GET_STYPE(fc);
00168 
00169         if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
00170                 wpa_hexdump(MSG_MSGDUMP, "Received management frame",
00171                             buf, len);
00172         }
00173 
00174         ver = fc & WLAN_FC_PVER;
00175 
00176         /* protocol version 3 is reserved for indicating extra data after the
00177          * payload, version 2 for indicating ACKed frame (TX callbacks), and
00178          * version 1 for indicating failed frame (no ACK, TX callbacks) */
00179         if (ver == 3) {
00180                 u8 *pos = buf + len - 2;
00181                 extra_len = WPA_GET_LE16(pos);
00182                 printf("extra data in frame (elen=%d)\n", extra_len);
00183                 if ((size_t) extra_len + 2 > len) {
00184                         printf("  extra data overflow\n");
00185                         return;
00186                 }
00187                 len -= extra_len + 2;
00188                 extra = buf + len;
00189         } else if (ver == 1 || ver == 2) {
00190                 handle_tx_callback(drv, buf, data_len, ver == 2 ? 1 : 0);
00191                 return;
00192         } else if (ver != 0) {
00193                 printf("unknown protocol version %d\n", ver);
00194                 return;
00195         }
00196 
00197         switch (type) {
00198         case WLAN_FC_TYPE_MGMT:
00199                 os_memset(&event, 0, sizeof(event));
00200                 event.rx_mgmt.frame = buf;
00201                 event.rx_mgmt.frame_len = data_len;
00202                 wpa_supplicant_event(drv->hapd, EVENT_RX_MGMT, &event);
00203                 break;
00204         case WLAN_FC_TYPE_CTRL:
00205                 wpa_printf(MSG_DEBUG, "CTRL");
00206                 break;
00207         case WLAN_FC_TYPE_DATA:
00208                 wpa_printf(MSG_DEBUG, "DATA");
00209                 handle_data(drv, buf, data_len, stype);
00210                 break;
00211         default:
00212                 wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
00213                 break;
00214         }
00215 }
00216 
00217 
00218 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
00219 {
00220         struct hostap_driver_data *drv = eloop_ctx;
00221         int len;
00222         unsigned char buf[3000];
00223 
00224         len = recv(sock, buf, sizeof(buf), 0);
00225         if (len < 0) {
00226                 perror("recv");
00227                 return;
00228         }
00229 
00230         handle_frame(drv, buf, len);
00231 }
00232 
00233 
00234 static int hostap_init_sockets(struct hostap_driver_data *drv, u8 *own_addr)
00235 {
00236         struct ifreq ifr;
00237         struct sockaddr_ll addr;
00238 
00239         drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
00240         if (drv->sock < 0) {
00241                 perror("socket[PF_PACKET,SOCK_RAW]");
00242                 return -1;
00243         }
00244 
00245         if (eloop_register_read_sock(drv->sock, handle_read, drv, NULL)) {
00246                 printf("Could not register read socket\n");
00247                 return -1;
00248         }
00249 
00250         memset(&ifr, 0, sizeof(ifr));
00251         snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
00252         if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
00253                 perror("ioctl(SIOCGIFINDEX)");
00254                 return -1;
00255         }
00256 
00257         if (hostap_set_iface_flags(drv, 1)) {
00258                 return -1;
00259         }
00260 
00261         memset(&addr, 0, sizeof(addr));
00262         addr.sll_family = AF_PACKET;
00263         addr.sll_ifindex = ifr.ifr_ifindex;
00264         wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
00265                    addr.sll_ifindex);
00266 
00267         if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
00268                 perror("bind");
00269                 return -1;
00270         }
00271 
00272         return linux_get_ifhwaddr(drv->sock, drv->iface, own_addr);
00273 }
00274 
00275 
00276 static int hostap_send_mlme(void *priv, const u8 *msg, size_t len)
00277 {
00278         struct hostap_driver_data *drv = priv;
00279         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) msg;
00280         int res;
00281 
00282         /* Request TX callback */
00283         hdr->frame_control |= host_to_le16(BIT(1));
00284         res = send(drv->sock, msg, len, 0);
00285         hdr->frame_control &= ~host_to_le16(BIT(1));
00286 
00287         return res;
00288 }
00289 
00290 
00291 static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
00292                              size_t data_len, int encrypt, const u8 *own_addr)
00293 {
00294         struct hostap_driver_data *drv = priv;
00295         struct ieee80211_hdr *hdr;
00296         size_t len;
00297         u8 *pos;
00298         int res;
00299 
00300         len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
00301         hdr = os_zalloc(len);
00302         if (hdr == NULL) {
00303                 printf("malloc() failed for hostapd_send_data(len=%lu)\n",
00304                        (unsigned long) len);
00305                 return -1;
00306         }
00307 
00308         hdr->frame_control =
00309                 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
00310         hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
00311         if (encrypt)
00312                 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
00313         memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
00314         memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
00315         memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
00316 
00317         pos = (u8 *) (hdr + 1);
00318         memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
00319         pos += sizeof(rfc1042_header);
00320         *((u16 *) pos) = htons(ETH_P_PAE);
00321         pos += 2;
00322         memcpy(pos, data, data_len);
00323 
00324         res = hostap_send_mlme(drv, (u8 *) hdr, len);
00325         if (res < 0) {
00326                 wpa_printf(MSG_ERROR, "hostap_send_eapol - packet len: %lu - "
00327                            "failed: %d (%s)",
00328                            (unsigned long) len, errno, strerror(errno));
00329         }
00330         free(hdr);
00331 
00332         return res;
00333 }
00334 
00335 
00336 static int hostap_sta_set_flags(void *priv, const u8 *addr,
00337                                 int total_flags, int flags_or, int flags_and)
00338 {
00339         struct hostap_driver_data *drv = priv;
00340         struct prism2_hostapd_param param;
00341 
00342         if (flags_or & WPA_STA_AUTHORIZED)
00343                 flags_or = BIT(5); /* WLAN_STA_AUTHORIZED */
00344         if (!(flags_and & WPA_STA_AUTHORIZED))
00345                 flags_and = ~BIT(5);
00346         else
00347                 flags_and = ~0;
00348         memset(&param, 0, sizeof(param));
00349         param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
00350         memcpy(param.sta_addr, addr, ETH_ALEN);
00351         param.u.set_flags_sta.flags_or = flags_or;
00352         param.u.set_flags_sta.flags_and = flags_and;
00353         return hostapd_ioctl(drv, &param, sizeof(param));
00354 }
00355 
00356 
00357 static int hostap_set_iface_flags(void *priv, int dev_up)
00358 {
00359         struct hostap_driver_data *drv = priv;
00360         struct ifreq ifr;
00361         char ifname[IFNAMSIZ];
00362 
00363         os_snprintf(ifname, IFNAMSIZ, "%sap", drv->iface);
00364         if (linux_set_iface_flags(drv->ioctl_sock, ifname, dev_up) < 0)
00365                 return -1;
00366 
00367         if (dev_up) {
00368                 memset(&ifr, 0, sizeof(ifr));
00369                 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
00370                 ifr.ifr_mtu = HOSTAPD_MTU;
00371                 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
00372                         perror("ioctl[SIOCSIFMTU]");
00373                         printf("Setting MTU failed - trying to survive with "
00374                                "current value\n");
00375                 }
00376         }
00377 
00378         return 0;
00379 }
00380 
00381 
00382 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
00383                          int len)
00384 {
00385         struct hostap_driver_data *drv = priv;
00386         struct iwreq iwr;
00387 
00388         memset(&iwr, 0, sizeof(iwr));
00389         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
00390         iwr.u.data.pointer = (caddr_t) param;
00391         iwr.u.data.length = len;
00392 
00393         if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
00394                 perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
00395                 return -1;
00396         }
00397 
00398         return 0;
00399 }
00400 
00401 
00402 static int wpa_driver_hostap_set_key(const char *ifname, void *priv,
00403                                      enum wpa_alg alg, const u8 *addr,
00404                                      int key_idx, int set_tx,
00405                                      const u8 *seq, size_t seq_len,
00406                                      const u8 *key, size_t key_len)
00407 {
00408         struct hostap_driver_data *drv = priv;
00409         struct prism2_hostapd_param *param;
00410         u8 *buf;
00411         size_t blen;
00412         int ret = 0;
00413 
00414         blen = sizeof(*param) + key_len;
00415         buf = os_zalloc(blen);
00416         if (buf == NULL)
00417                 return -1;
00418 
00419         param = (struct prism2_hostapd_param *) buf;
00420         param->cmd = PRISM2_SET_ENCRYPTION;
00421         if (addr == NULL)
00422                 memset(param->sta_addr, 0xff, ETH_ALEN);
00423         else
00424                 memcpy(param->sta_addr, addr, ETH_ALEN);
00425         switch (alg) {
00426         case WPA_ALG_NONE:
00427                 os_strlcpy((char *) param->u.crypt.alg, "NONE",
00428                            HOSTAP_CRYPT_ALG_NAME_LEN);
00429                 break;
00430         case WPA_ALG_WEP:
00431                 os_strlcpy((char *) param->u.crypt.alg, "WEP",
00432                            HOSTAP_CRYPT_ALG_NAME_LEN);
00433                 break;
00434         case WPA_ALG_TKIP:
00435                 os_strlcpy((char *) param->u.crypt.alg, "TKIP",
00436                            HOSTAP_CRYPT_ALG_NAME_LEN);
00437                 break;
00438         case WPA_ALG_CCMP:
00439                 os_strlcpy((char *) param->u.crypt.alg, "CCMP",
00440                            HOSTAP_CRYPT_ALG_NAME_LEN);
00441                 break;
00442         default:
00443                 os_free(buf);
00444                 return -1;
00445         }
00446         param->u.crypt.flags = set_tx ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
00447         param->u.crypt.idx = key_idx;
00448         param->u.crypt.key_len = key_len;
00449         memcpy((u8 *) (param + 1), key, key_len);
00450 
00451         if (hostapd_ioctl(drv, param, blen)) {
00452                 printf("Failed to set encryption.\n");
00453                 ret = -1;
00454         }
00455         free(buf);
00456 
00457         return ret;
00458 }
00459 
00460 
00461 static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
00462                              int idx, u8 *seq)
00463 {
00464         struct hostap_driver_data *drv = priv;
00465         struct prism2_hostapd_param *param;
00466         u8 *buf;
00467         size_t blen;
00468         int ret = 0;
00469 
00470         blen = sizeof(*param) + 32;
00471         buf = os_zalloc(blen);
00472         if (buf == NULL)
00473                 return -1;
00474 
00475         param = (struct prism2_hostapd_param *) buf;
00476         param->cmd = PRISM2_GET_ENCRYPTION;
00477         if (addr == NULL)
00478                 memset(param->sta_addr, 0xff, ETH_ALEN);
00479         else
00480                 memcpy(param->sta_addr, addr, ETH_ALEN);
00481         param->u.crypt.idx = idx;
00482 
00483         if (hostapd_ioctl(drv, param, blen)) {
00484                 printf("Failed to get encryption.\n");
00485                 ret = -1;
00486         } else {
00487                 memcpy(seq, param->u.crypt.seq, 8);
00488         }
00489         free(buf);
00490 
00491         return ret;
00492 }
00493 
00494 
00495 static int hostap_ioctl_prism2param(void *priv, int param, int value)
00496 {
00497         struct hostap_driver_data *drv = priv;
00498         struct iwreq iwr;
00499         int *i;
00500 
00501         memset(&iwr, 0, sizeof(iwr));
00502         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
00503         i = (int *) iwr.u.name;
00504         *i++ = param;
00505         *i++ = value;
00506 
00507         if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
00508                 perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
00509                 return -1;
00510         }
00511 
00512         return 0;
00513 }
00514 
00515 
00516 static int hostap_set_ieee8021x(void *priv, struct wpa_bss_params *params)
00517 {
00518         struct hostap_driver_data *drv = priv;
00519         int enabled = params->enabled;
00520 
00521         /* enable kernel driver support for IEEE 802.1X */
00522         if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
00523                 printf("Could not setup IEEE 802.1X support in kernel driver."
00524                        "\n");
00525                 return -1;
00526         }
00527 
00528         if (!enabled)
00529                 return 0;
00530 
00531         /* use host driver implementation of encryption to allow
00532          * individual keys and passing plaintext EAPOL frames */
00533         if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
00534             hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
00535                 printf("Could not setup host-based encryption in kernel "
00536                        "driver.\n");
00537                 return -1;
00538         }
00539 
00540         return 0;
00541 }
00542 
00543 
00544 static int hostap_set_privacy(void *priv, int enabled)
00545 {
00546         struct hostap_drvier_data *drv = priv;
00547 
00548         return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
00549                                         enabled);
00550 }
00551 
00552 
00553 static int hostap_set_ssid(void *priv, const u8 *buf, int len)
00554 {
00555         struct hostap_driver_data *drv = priv;
00556         struct iwreq iwr;
00557 
00558         memset(&iwr, 0, sizeof(iwr));
00559         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
00560         iwr.u.essid.flags = 1; /* SSID active */
00561         iwr.u.essid.pointer = (caddr_t) buf;
00562         iwr.u.essid.length = len + 1;
00563 
00564         if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
00565                 perror("ioctl[SIOCSIWESSID]");
00566                 printf("len=%d\n", len);
00567                 return -1;
00568         }
00569 
00570         return 0;
00571 }
00572 
00573 
00574 static int hostap_flush(void *priv)
00575 {
00576         struct hostap_driver_data *drv = priv;
00577         struct prism2_hostapd_param param;
00578 
00579         memset(&param, 0, sizeof(param));
00580         param.cmd = PRISM2_HOSTAPD_FLUSH;
00581         return hostapd_ioctl(drv, &param, sizeof(param));
00582 }
00583 
00584 
00585 static int hostap_read_sta_data(void *priv,
00586                                 struct hostap_sta_driver_data *data,
00587                                 const u8 *addr)
00588 {
00589         struct hostap_driver_data *drv = priv;
00590         char buf[1024], line[128], *pos;
00591         FILE *f;
00592         unsigned long val;
00593 
00594         memset(data, 0, sizeof(*data));
00595         snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
00596                  drv->iface, MAC2STR(addr));
00597 
00598         f = fopen(buf, "r");
00599         if (!f)
00600                 return -1;
00601         /* Need to read proc file with in one piece, so use large enough
00602          * buffer. */
00603         setbuffer(f, buf, sizeof(buf));
00604 
00605         while (fgets(line, sizeof(line), f)) {
00606                 pos = strchr(line, '=');
00607                 if (!pos)
00608                         continue;
00609                 *pos++ = '\0';
00610                 val = strtoul(pos, NULL, 10);
00611                 if (strcmp(line, "rx_packets") == 0)
00612                         data->rx_packets = val;
00613                 else if (strcmp(line, "tx_packets") == 0)
00614                         data->tx_packets = val;
00615                 else if (strcmp(line, "rx_bytes") == 0)
00616                         data->rx_bytes = val;
00617                 else if (strcmp(line, "tx_bytes") == 0)
00618                         data->tx_bytes = val;
00619         }
00620 
00621         fclose(f);
00622 
00623         return 0;
00624 }
00625 
00626 
00627 static int hostap_sta_add(void *priv, struct hostapd_sta_add_params *params)
00628 {
00629         struct hostap_driver_data *drv = priv;
00630         struct prism2_hostapd_param param;
00631         int tx_supp_rates = 0;
00632         size_t i;
00633 
00634 #define WLAN_RATE_1M BIT(0)
00635 #define WLAN_RATE_2M BIT(1)
00636 #define WLAN_RATE_5M5 BIT(2)
00637 #define WLAN_RATE_11M BIT(3)
00638 
00639         for (i = 0; i < params->supp_rates_len; i++) {
00640                 if ((params->supp_rates[i] & 0x7f) == 2)
00641                         tx_supp_rates |= WLAN_RATE_1M;
00642                 if ((params->supp_rates[i] & 0x7f) == 4)
00643                         tx_supp_rates |= WLAN_RATE_2M;
00644                 if ((params->supp_rates[i] & 0x7f) == 11)
00645                         tx_supp_rates |= WLAN_RATE_5M5;
00646                 if ((params->supp_rates[i] & 0x7f) == 22)
00647                         tx_supp_rates |= WLAN_RATE_11M;
00648         }
00649 
00650         memset(&param, 0, sizeof(param));
00651         param.cmd = PRISM2_HOSTAPD_ADD_STA;
00652         memcpy(param.sta_addr, params->addr, ETH_ALEN);
00653         param.u.add_sta.aid = params->aid;
00654         param.u.add_sta.capability = params->capability;
00655         param.u.add_sta.tx_supp_rates = tx_supp_rates;
00656         return hostapd_ioctl(drv, &param, sizeof(param));
00657 }
00658 
00659 
00660 static int hostap_sta_remove(void *priv, const u8 *addr)
00661 {
00662         struct hostap_driver_data *drv = priv;
00663         struct prism2_hostapd_param param;
00664 
00665         hostap_sta_set_flags(drv, addr, 0, 0, ~WPA_STA_AUTHORIZED);
00666 
00667         memset(&param, 0, sizeof(param));
00668         param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
00669         memcpy(param.sta_addr, addr, ETH_ALEN);
00670         if (hostapd_ioctl(drv, &param, sizeof(param))) {
00671                 printf("Could not remove station from kernel driver.\n");
00672                 return -1;
00673         }
00674         return 0;
00675 }
00676 
00677 
00678 static int hostap_get_inact_sec(void *priv, const u8 *addr)
00679 {
00680         struct hostap_driver_data *drv = priv;
00681         struct prism2_hostapd_param param;
00682 
00683         memset(&param, 0, sizeof(param));
00684         param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
00685         memcpy(param.sta_addr, addr, ETH_ALEN);
00686         if (hostapd_ioctl(drv, &param, sizeof(param))) {
00687                 return -1;
00688         }
00689 
00690         return param.u.get_info_sta.inactive_sec;
00691 }
00692 
00693 
00694 static int hostap_sta_clear_stats(void *priv, const u8 *addr)
00695 {
00696         struct hostap_driver_data *drv = priv;
00697         struct prism2_hostapd_param param;
00698 
00699         memset(&param, 0, sizeof(param));
00700         param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
00701         memcpy(param.sta_addr, addr, ETH_ALEN);
00702         if (hostapd_ioctl(drv, &param, sizeof(param))) {
00703                 return -1;
00704         }
00705 
00706         return 0;
00707 }
00708 
00709 
00710 static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
00711 {
00712         struct prism2_hostapd_param *param;
00713         int res;
00714         size_t blen, elem_len;
00715 
00716         elem_len = drv->generic_ie_len + drv->wps_ie_len;
00717         blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
00718         if (blen < sizeof(*param))
00719                 blen = sizeof(*param);
00720 
00721         param = os_zalloc(blen);
00722         if (param == NULL)
00723                 return -1;
00724 
00725         param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
00726         param->u.generic_elem.len = elem_len;
00727         if (drv->generic_ie) {
00728                 os_memcpy(param->u.generic_elem.data, drv->generic_ie,
00729                           drv->generic_ie_len);
00730         }
00731         if (drv->wps_ie) {
00732                 os_memcpy(&param->u.generic_elem.data[drv->generic_ie_len],
00733                           drv->wps_ie, drv->wps_ie_len);
00734         }
00735         wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
00736                     param->u.generic_elem.data, elem_len);
00737         res = hostapd_ioctl(drv, param, blen);
00738 
00739         os_free(param);
00740 
00741         return res;
00742 }
00743 
00744 
00745 static int hostap_set_generic_elem(void *priv,
00746                                    const u8 *elem, size_t elem_len)
00747 {
00748         struct hostap_driver_data *drv = priv;
00749 
00750         os_free(drv->generic_ie);
00751         drv->generic_ie = NULL;
00752         drv->generic_ie_len = 0;
00753         if (elem) {
00754                 drv->generic_ie = os_malloc(elem_len);
00755                 if (drv->generic_ie == NULL)
00756                         return -1;
00757                 os_memcpy(drv->generic_ie, elem, elem_len);
00758                 drv->generic_ie_len = elem_len;
00759         }
00760 
00761         return hostapd_ioctl_set_generic_elem(drv);
00762 }
00763 
00764 
00765 static int hostap_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
00766                                 const struct wpabuf *proberesp)
00767 {
00768         struct hostap_driver_data *drv = priv;
00769 
00770         /*
00771          * Host AP driver supports only one set of extra IEs, so we need to
00772          * use the Probe Response IEs also for Beacon frames since they include
00773          * more information.
00774          */
00775 
00776         os_free(drv->wps_ie);
00777         drv->wps_ie = NULL;
00778         drv->wps_ie_len = 0;
00779         if (proberesp) {
00780                 drv->wps_ie = os_malloc(wpabuf_len(proberesp));
00781                 if (drv->wps_ie == NULL)
00782                         return -1;
00783                 os_memcpy(drv->wps_ie, wpabuf_head(proberesp),
00784                           wpabuf_len(proberesp));
00785                 drv->wps_ie_len = wpabuf_len(proberesp);
00786         }
00787 
00788         return hostapd_ioctl_set_generic_elem(drv);
00789 }
00790 
00791 
00792 static void
00793 hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
00794                                        char *custom)
00795 {
00796         wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
00797 
00798         if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
00799                 char *pos;
00800                 u8 addr[ETH_ALEN];
00801                 pos = strstr(custom, "addr=");
00802                 if (pos == NULL) {
00803                         wpa_printf(MSG_DEBUG,
00804                                    "MLME-MICHAELMICFAILURE.indication "
00805                                    "without sender address ignored");
00806                         return;
00807                 }
00808                 pos += 5;
00809                 if (hwaddr_aton(pos, addr) == 0) {
00810                         union wpa_event_data data;
00811                         os_memset(&data, 0, sizeof(data));
00812                         data.michael_mic_failure.unicast = 1;
00813                         data.michael_mic_failure.src = addr;
00814                         wpa_supplicant_event(drv->hapd,
00815                                              EVENT_MICHAEL_MIC_FAILURE, &data);
00816                 } else {
00817                         wpa_printf(MSG_DEBUG,
00818                                    "MLME-MICHAELMICFAILURE.indication "
00819                                    "with invalid MAC address");
00820                 }
00821         }
00822 }
00823 
00824 
00825 static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
00826                                             char *data, int len)
00827 {
00828         struct iw_event iwe_buf, *iwe = &iwe_buf;
00829         char *pos, *end, *custom, *buf;
00830 
00831         pos = data;
00832         end = data + len;
00833 
00834         while (pos + IW_EV_LCP_LEN <= end) {
00835                 /* Event data may be unaligned, so make a local, aligned copy
00836                  * before processing. */
00837                 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
00838                 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
00839                            iwe->cmd, iwe->len);
00840                 if (iwe->len <= IW_EV_LCP_LEN)
00841                         return;
00842 
00843                 custom = pos + IW_EV_POINT_LEN;
00844                 if (drv->we_version > 18 &&
00845                     (iwe->cmd == IWEVMICHAELMICFAILURE ||
00846                      iwe->cmd == IWEVCUSTOM)) {
00847                         /* WE-19 removed the pointer from struct iw_point */
00848                         char *dpos = (char *) &iwe_buf.u.data.length;
00849                         int dlen = dpos - (char *) &iwe_buf;
00850                         memcpy(dpos, pos + IW_EV_LCP_LEN,
00851                                sizeof(struct iw_event) - dlen);
00852                 } else {
00853                         memcpy(&iwe_buf, pos, sizeof(struct iw_event));
00854                         custom += IW_EV_POINT_OFF;
00855                 }
00856 
00857                 switch (iwe->cmd) {
00858                 case IWEVCUSTOM:
00859                         if (custom + iwe->u.data.length > end)
00860                                 return;
00861                         buf = malloc(iwe->u.data.length + 1);
00862                         if (buf == NULL)
00863                                 return;
00864                         memcpy(buf, custom, iwe->u.data.length);
00865                         buf[iwe->u.data.length] = '\0';
00866                         hostapd_wireless_event_wireless_custom(drv, buf);
00867                         free(buf);
00868                         break;
00869                 }
00870 
00871                 pos += iwe->len;
00872         }
00873 }
00874 
00875 
00876 static void hostapd_wireless_event_rtm_newlink(void *ctx,
00877                                                struct ifinfomsg *ifi,
00878                                                u8 *buf, size_t len)
00879 {
00880         struct hostap_driver_data *drv = ctx;
00881         int attrlen, rta_len;
00882         struct rtattr *attr;
00883 
00884         /* TODO: use ifi->ifi_index to filter out wireless events from other
00885          * interfaces */
00886 
00887         attrlen = len;
00888         attr = (struct rtattr *) buf;
00889 
00890         rta_len = RTA_ALIGN(sizeof(struct rtattr));
00891         while (RTA_OK(attr, attrlen)) {
00892                 if (attr->rta_type == IFLA_WIRELESS) {
00893                         hostapd_wireless_event_wireless(
00894                                 drv, ((char *) attr) + rta_len,
00895                                 attr->rta_len - rta_len);
00896                 }
00897                 attr = RTA_NEXT(attr, attrlen);
00898         }
00899 }
00900 
00901 
00902 static int hostap_get_we_version(struct hostap_driver_data *drv)
00903 {
00904         struct iw_range *range;
00905         struct iwreq iwr;
00906         int minlen;
00907         size_t buflen;
00908 
00909         drv->we_version = 0;
00910 
00911         /*
00912          * Use larger buffer than struct iw_range in order to allow the
00913          * structure to grow in the future.
00914          */
00915         buflen = sizeof(struct iw_range) + 500;
00916         range = os_zalloc(buflen);
00917         if (range == NULL)
00918                 return -1;
00919 
00920         memset(&iwr, 0, sizeof(iwr));
00921         os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
00922         iwr.u.data.pointer = (caddr_t) range;
00923         iwr.u.data.length = buflen;
00924 
00925         minlen = ((char *) &range->enc_capa) - (char *) range +
00926                 sizeof(range->enc_capa);
00927 
00928         if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
00929                 perror("ioctl[SIOCGIWRANGE]");
00930                 free(range);
00931                 return -1;
00932         } else if (iwr.u.data.length >= minlen &&
00933                    range->we_version_compiled >= 18) {
00934                 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
00935                            "WE(source)=%d enc_capa=0x%x",
00936                            range->we_version_compiled,
00937                            range->we_version_source,
00938                            range->enc_capa);
00939                 drv->we_version = range->we_version_compiled;
00940         }
00941 
00942         free(range);
00943         return 0;
00944 }
00945 
00946 
00947 static int hostap_wireless_event_init(struct hostap_driver_data *drv)
00948 {
00949         struct netlink_config *cfg;
00950 
00951         hostap_get_we_version(drv);
00952 
00953         cfg = os_zalloc(sizeof(*cfg));
00954         if (cfg == NULL)
00955                 return -1;
00956         cfg->ctx = drv;
00957         cfg->newlink_cb = hostapd_wireless_event_rtm_newlink;
00958         drv->netlink = netlink_init(cfg);
00959         if (drv->netlink == NULL) {
00960                 os_free(cfg);
00961                 return -1;
00962         }
00963 
00964         return 0;
00965 }
00966 
00967 
00968 static void * hostap_init(struct hostapd_data *hapd,
00969                           struct wpa_init_params *params)
00970 {
00971         struct hostap_driver_data *drv;
00972 
00973         drv = os_zalloc(sizeof(struct hostap_driver_data));
00974         if (drv == NULL) {
00975                 printf("Could not allocate memory for hostapd driver data\n");
00976                 return NULL;
00977         }
00978 
00979         drv->hapd = hapd;
00980         drv->ioctl_sock = drv->sock = -1;
00981         memcpy(drv->iface, params->ifname, sizeof(drv->iface));
00982 
00983         drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
00984         if (drv->ioctl_sock < 0) {
00985                 perror("socket[PF_INET,SOCK_DGRAM]");
00986                 free(drv);
00987                 return NULL;
00988         }
00989 
00990         if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
00991                 printf("Could not enable hostapd mode for interface %s\n",
00992                        drv->iface);
00993                 close(drv->ioctl_sock);
00994                 free(drv);
00995                 return NULL;
00996         }
00997 
00998         if (hostap_init_sockets(drv, params->own_addr) ||
00999             hostap_wireless_event_init(drv)) {
01000                 close(drv->ioctl_sock);
01001                 free(drv);
01002                 return NULL;
01003         }
01004 
01005         return drv;
01006 }
01007 
01008 
01009 static void hostap_driver_deinit(void *priv)
01010 {
01011         struct hostap_driver_data *drv = priv;
01012 
01013         netlink_deinit(drv->netlink);
01014         (void) hostap_set_iface_flags(drv, 0);
01015         (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
01016         (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
01017 
01018         if (drv->ioctl_sock >= 0)
01019                 close(drv->ioctl_sock);
01020 
01021         if (drv->sock >= 0)
01022                 close(drv->sock);
01023 
01024         os_free(drv->generic_ie);
01025         os_free(drv->wps_ie);
01026 
01027         free(drv);
01028 }
01029 
01030 
01031 static int hostap_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
01032                              int reason)
01033 {
01034         struct hostap_driver_data *drv = priv;
01035         struct ieee80211_mgmt mgmt;
01036 
01037         memset(&mgmt, 0, sizeof(mgmt));
01038         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
01039                                           WLAN_FC_STYPE_DEAUTH);
01040         memcpy(mgmt.da, addr, ETH_ALEN);
01041         memcpy(mgmt.sa, own_addr, ETH_ALEN);
01042         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
01043         mgmt.u.deauth.reason_code = host_to_le16(reason);
01044         return hostap_send_mlme(drv, (u8 *) &mgmt, IEEE80211_HDRLEN +
01045                                 sizeof(mgmt.u.deauth));
01046 }
01047 
01048 
01049 static int hostap_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
01050                                int reason)
01051 {
01052         struct hostap_driver_data *drv = priv;
01053         struct ieee80211_mgmt mgmt;
01054 
01055         memset(&mgmt, 0, sizeof(mgmt));
01056         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
01057                                           WLAN_FC_STYPE_DISASSOC);
01058         memcpy(mgmt.da, addr, ETH_ALEN);
01059         memcpy(mgmt.sa, own_addr, ETH_ALEN);
01060         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
01061         mgmt.u.disassoc.reason_code = host_to_le16(reason);
01062         return  hostap_send_mlme(drv, (u8 *) &mgmt, IEEE80211_HDRLEN +
01063                                  sizeof(mgmt.u.disassoc));
01064 }
01065 
01066 
01067 static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
01068                                                             u16 *num_modes,
01069                                                             u16 *flags)
01070 {
01071         struct hostapd_hw_modes *mode;
01072         int i, clen, rlen;
01073         const short chan2freq[14] = {
01074                 2412, 2417, 2422, 2427, 2432, 2437, 2442,
01075                 2447, 2452, 2457, 2462, 2467, 2472, 2484
01076         };
01077 
01078         mode = os_zalloc(sizeof(struct hostapd_hw_modes));
01079         if (mode == NULL)
01080                 return NULL;
01081 
01082         *num_modes = 1;
01083         *flags = 0;
01084 
01085         mode->mode = HOSTAPD_MODE_IEEE80211B;
01086         mode->num_channels = 14;
01087         mode->num_rates = 4;
01088 
01089         clen = mode->num_channels * sizeof(struct hostapd_channel_data);
01090         rlen = mode->num_rates * sizeof(int);
01091 
01092         mode->channels = os_zalloc(clen);
01093         mode->rates = os_zalloc(rlen);
01094         if (mode->channels == NULL || mode->rates == NULL) {
01095                 os_free(mode->channels);
01096                 os_free(mode->rates);
01097                 os_free(mode);
01098                 return NULL;
01099         }
01100 
01101         for (i = 0; i < 14; i++) {
01102                 mode->channels[i].chan = i + 1;
01103                 mode->channels[i].freq = chan2freq[i];
01104                 /* TODO: Get allowed channel list from the driver */
01105                 if (i >= 11)
01106                         mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
01107         }
01108 
01109         mode->rates[0] = 10;
01110         mode->rates[1] = 20;
01111         mode->rates[2] = 55;
01112         mode->rates[3] = 110;
01113 
01114         return mode;
01115 }
01116 
01117 #else /* HOSTAPD */
01118 
01119 struct wpa_driver_hostap_data {
01120         void *wext; /* private data for driver_wext */
01121         void *ctx;
01122         char ifname[IFNAMSIZ + 1];
01123         int sock;
01124         int current_mode; /* infra/adhoc */
01125 };
01126 
01127 
01128 static int wpa_driver_hostap_set_auth_alg(void *priv, int auth_alg);
01129 
01130 
01131 static int hostapd_ioctl(struct wpa_driver_hostap_data *drv,
01132                          struct prism2_hostapd_param *param,
01133                          int len, int show_err)
01134 {
01135         struct iwreq iwr;
01136 
01137         os_memset(&iwr, 0, sizeof(iwr));
01138         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
01139         iwr.u.data.pointer = (caddr_t) param;
01140         iwr.u.data.length = len;
01141 
01142         if (ioctl(drv->sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
01143                 int ret = errno;
01144                 if (show_err)
01145                         perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
01146                 return ret;
01147         }
01148 
01149         return 0;
01150 }
01151 
01152 
01153 static int wpa_driver_hostap_set_wpa_ie(struct wpa_driver_hostap_data *drv,
01154                                         const u8 *wpa_ie, size_t wpa_ie_len)
01155 {
01156         struct prism2_hostapd_param *param;
01157         int res;
01158         size_t blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + wpa_ie_len;
01159         if (blen < sizeof(*param))
01160                 blen = sizeof(*param);
01161 
01162         param = os_zalloc(blen);
01163         if (param == NULL)
01164                 return -1;
01165 
01166         param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
01167         param->u.generic_elem.len = wpa_ie_len;
01168         os_memcpy(param->u.generic_elem.data, wpa_ie, wpa_ie_len);
01169         res = hostapd_ioctl(drv, param, blen, 1);
01170 
01171         os_free(param);
01172 
01173         return res;
01174 }
01175 
01176 
01177 static int prism2param(struct wpa_driver_hostap_data *drv, int param,
01178                        int value)
01179 {
01180         struct iwreq iwr;
01181         int *i, ret = 0;
01182 
01183         os_memset(&iwr, 0, sizeof(iwr));
01184         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
01185         i = (int *) iwr.u.name;
01186         *i++ = param;
01187         *i++ = value;
01188 
01189         if (ioctl(drv->sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
01190                 perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
01191                 ret = -1;
01192         }
01193         return ret;
01194 }
01195 
01196 
01197 static int wpa_driver_hostap_set_wpa(void *priv, int enabled)
01198 {
01199         struct wpa_driver_hostap_data *drv = priv;
01200         int ret = 0;
01201 
01202         wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
01203 
01204         if (!enabled && wpa_driver_hostap_set_wpa_ie(drv, NULL, 0) < 0)
01205                 ret = -1;
01206         if (prism2param(drv, PRISM2_PARAM_HOST_ROAMING, enabled ? 2 : 0) < 0)
01207                 ret = -1;
01208         if (prism2param(drv, PRISM2_PARAM_WPA, enabled) < 0)
01209                 ret = -1;
01210 
01211         return ret;
01212 }
01213 
01214 
01215 static void show_set_key_error(struct prism2_hostapd_param *param)
01216 {
01217         switch (param->u.crypt.err) {
01218         case HOSTAP_CRYPT_ERR_UNKNOWN_ALG:
01219                 wpa_printf(MSG_INFO, "Unknown algorithm '%s'.",
01220                            param->u.crypt.alg);
01221                 wpa_printf(MSG_INFO, "You may need to load kernel module to "
01222                            "register that algorithm.");
01223                 wpa_printf(MSG_INFO, "E.g., 'modprobe hostap_crypt_wep' for "
01224                            "WEP.");
01225                 break;
01226         case HOSTAP_CRYPT_ERR_UNKNOWN_ADDR:
01227                 wpa_printf(MSG_INFO, "Unknown address " MACSTR ".",
01228                            MAC2STR(param->sta_addr));
01229                 break;
01230         case HOSTAP_CRYPT_ERR_CRYPT_INIT_FAILED:
01231                 wpa_printf(MSG_INFO, "Crypt algorithm initialization failed.");
01232                 break;
01233         case HOSTAP_CRYPT_ERR_KEY_SET_FAILED:
01234                 wpa_printf(MSG_INFO, "Key setting failed.");
01235                 break;
01236         case HOSTAP_CRYPT_ERR_TX_KEY_SET_FAILED:
01237                 wpa_printf(MSG_INFO, "TX key index setting failed.");
01238                 break;
01239         case HOSTAP_CRYPT_ERR_CARD_CONF_FAILED:
01240                 wpa_printf(MSG_INFO, "Card configuration failed.");
01241                 break;
01242         }
01243 }
01244 
01245 
01246 static int wpa_driver_hostap_set_key(const char *ifname, void *priv,
01247                                      enum wpa_alg alg, const u8 *addr,
01248                                      int key_idx, int set_tx,
01249                                      const u8 *seq, size_t seq_len,
01250                                      const u8 *key, size_t key_len)
01251 {
01252         struct wpa_driver_hostap_data *drv = priv;
01253         struct prism2_hostapd_param *param;
01254         u8 *buf;
01255         size_t blen;
01256         int ret = 0;
01257         char *alg_name;
01258 
01259         switch (alg) {
01260         case WPA_ALG_NONE:
01261                 alg_name = "none";
01262                 break;
01263         case WPA_ALG_WEP:
01264                 alg_name = "WEP";
01265                 break;
01266         case WPA_ALG_TKIP:
01267                 alg_name = "TKIP";
01268                 break;
01269         case WPA_ALG_CCMP:
01270                 alg_name = "CCMP";
01271                 break;
01272         default:
01273                 return -1;
01274         }
01275 
01276         wpa_printf(MSG_DEBUG, "%s: alg=%s key_idx=%d set_tx=%d seq_len=%lu "
01277                    "key_len=%lu", __FUNCTION__, alg_name, key_idx, set_tx,
01278                    (unsigned long) seq_len, (unsigned long) key_len);
01279 
01280         if (seq_len > 8)
01281                 return -2;
01282 
01283         blen = sizeof(*param) + key_len;
01284         buf = os_zalloc(blen);
01285         if (buf == NULL)
01286                 return -1;
01287 
01288         param = (struct prism2_hostapd_param *) buf;
01289         param->cmd = PRISM2_SET_ENCRYPTION;
01290         /* TODO: In theory, STA in client mode can use five keys; four default
01291          * keys for receiving (with keyidx 0..3) and one individual key for
01292          * both transmitting and receiving (keyidx 0) _unicast_ packets. Now,
01293          * keyidx 0 is reserved for this unicast use and default keys can only
01294          * use keyidx 1..3 (i.e., default key with keyidx 0 is not supported).
01295          * This should be fine for more or less all cases, but for completeness
01296          * sake, the driver could be enhanced to support the missing key. */
01297 #if 0
01298         if (addr == NULL)
01299                 os_memset(param->sta_addr, 0xff, ETH_ALEN);
01300         else
01301                 os_memcpy(param->sta_addr, addr, ETH_ALEN);
01302 #else
01303         os_memset(param->sta_addr, 0xff, ETH_ALEN);
01304 #endif
01305         os_strlcpy((char *) param->u.crypt.alg, alg_name,
01306                    HOSTAP_CRYPT_ALG_NAME_LEN);
01307         param->u.crypt.flags = set_tx ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
01308         param->u.crypt.idx = key_idx;
01309         os_memcpy(param->u.crypt.seq, seq, seq_len);
01310         param->u.crypt.key_len = key_len;
01311         os_memcpy((u8 *) (param + 1), key, key_len);
01312 
01313         if (hostapd_ioctl(drv, param, blen, 1)) {
01314                 wpa_printf(MSG_WARNING, "Failed to set encryption.");
01315                 show_set_key_error(param);
01316                 ret = -1;
01317         }
01318         os_free(buf);
01319 
01320         return ret;
01321 }
01322 
01323 
01324 static int wpa_driver_hostap_set_countermeasures(void *priv, int enabled)
01325 {
01326         struct wpa_driver_hostap_data *drv = priv;
01327         wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
01328         return prism2param(drv, PRISM2_PARAM_TKIP_COUNTERMEASURES, enabled);
01329 }
01330 
01331 
01332 static int wpa_driver_hostap_reset(struct wpa_driver_hostap_data *drv,
01333                                    int type)
01334 {
01335         struct iwreq iwr;
01336         int *i, ret = 0;
01337 
01338         wpa_printf(MSG_DEBUG, "%s: type=%d", __FUNCTION__, type);
01339 
01340         os_memset(&iwr, 0, sizeof(iwr));
01341         os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
01342         i = (int *) iwr.u.name;
01343         *i++ = type;
01344 
01345         if (ioctl(drv->sock, PRISM2_IOCTL_RESET, &iwr) < 0) {
01346                 perror("ioctl[PRISM2_IOCTL_RESET]");
01347                 ret = -1;
01348         }
01349         return ret;
01350 }
01351 
01352 
01353 static int wpa_driver_hostap_mlme(struct wpa_driver_hostap_data *drv,
01354                                   const u8 *addr, int cmd, int reason_code)
01355 {
01356         struct prism2_hostapd_param param;
01357         int ret;
01358 
01359         /* There does not seem to be a better way of deauthenticating or
01360          * disassociating with Prism2/2.5/3 than sending the management frame
01361          * and then resetting the Port0 to make sure both the AP and the STA
01362          * end up in disconnected state. */
01363         os_memset(&param, 0, sizeof(param));
01364         param.cmd = PRISM2_HOSTAPD_MLME;
01365         os_memcpy(param.sta_addr, addr, ETH_ALEN);
01366         param.u.mlme.cmd = cmd;
01367         param.u.mlme.reason_code = reason_code;
01368         ret = hostapd_ioctl(drv, &param, sizeof(param), 1);
01369         if (ret == 0) {
01370                 os_sleep(0, 100000);
01371                 ret = wpa_driver_hostap_reset(drv, 2);
01372         }
01373         return ret;
01374 }
01375 
01376 
01377 static int wpa_driver_hostap_deauthenticate(void *priv, const u8 *addr,
01378                                             int reason_code)
01379 {
01380         struct wpa_driver_hostap_data *drv = priv;
01381         wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
01382         return wpa_driver_hostap_mlme(drv, addr, MLME_STA_DEAUTH,
01383                                       reason_code);
01384 }
01385 
01386 
01387 static int wpa_driver_hostap_disassociate(void *priv, const u8 *addr,
01388                                           int reason_code)
01389 {
01390         struct wpa_driver_hostap_data *drv = priv;
01391         wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
01392         return wpa_driver_hostap_mlme(drv, addr, MLME_STA_DISASSOC,
01393                                       reason_code);
01394 }
01395 
01396 
01397 static int
01398 wpa_driver_hostap_associate(void *priv,
01399                             struct wpa_driver_associate_params *params)
01400 {
01401         struct wpa_driver_hostap_data *drv = priv;
01402         int ret = 0;
01403         int allow_unencrypted_eapol;
01404 
01405         wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
01406 
01407         if (prism2param(drv, PRISM2_PARAM_DROP_UNENCRYPTED,
01408                         params->drop_unencrypted) < 0)
01409                 ret = -1;
01410         if (wpa_driver_hostap_set_auth_alg(drv, params->auth_alg) < 0)
01411                 ret = -1;
01412         if (params->mode != drv->current_mode) {
01413                 /* At the moment, Host AP driver requires host_roaming=2 for
01414                  * infrastructure mode and host_roaming=0 for adhoc. */
01415                 if (prism2param(drv, PRISM2_PARAM_HOST_ROAMING,
01416                                 params->mode == IEEE80211_MODE_IBSS ? 0 : 2) <
01417                     0) {
01418                         wpa_printf(MSG_DEBUG, "%s: failed to set host_roaming",
01419                                    __func__);
01420                 }
01421                 drv->current_mode = params->mode;
01422         }
01423 
01424         if (prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
01425                         params->key_mgmt_suite != KEY_MGMT_NONE) < 0)
01426                 ret = -1;
01427         if (wpa_driver_hostap_set_wpa_ie(drv, params->wpa_ie,
01428                                          params->wpa_ie_len) < 0)
01429                 ret = -1;
01430         if (wpa_driver_wext_set_mode(drv->wext, params->mode) < 0)
01431                 ret = -1;
01432         if (params->freq &&
01433             wpa_driver_wext_set_freq(drv->wext, params->freq) < 0)
01434                 ret = -1;
01435         if (wpa_driver_wext_set_ssid(drv->wext, params->ssid, params->ssid_len)
01436             < 0)
01437                 ret = -1;
01438         if (wpa_driver_wext_set_bssid(drv->wext, params->bssid) < 0)
01439                 ret = -1;
01440 
01441         /* Allow unencrypted EAPOL messages even if pairwise keys are set when
01442          * not using WPA. IEEE 802.1X specifies that these frames are not
01443          * encrypted, but WPA encrypts them when pairwise keys are in use. */
01444         if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
01445             params->key_mgmt_suite == KEY_MGMT_PSK)
01446                 allow_unencrypted_eapol = 0;
01447         else
01448                 allow_unencrypted_eapol = 1;
01449         
01450         if (prism2param(drv, PRISM2_PARAM_IEEE_802_1X,
01451                         allow_unencrypted_eapol) < 0) {
01452                 wpa_printf(MSG_DEBUG, "hostap: Failed to configure "
01453                            "ieee_802_1x param");
01454                 /* Ignore this error.. driver_hostap.c can also be used with
01455                  * other drivers that do not support this prism2_param. */
01456         }
01457 
01458         return ret;
01459 }
01460 
01461 
01462 static int wpa_driver_hostap_scan(void *priv,
01463                                   struct wpa_driver_scan_params *params)
01464 {
01465         struct wpa_driver_hostap_data *drv = priv;
01466         struct prism2_hostapd_param param;
01467         int ret;
01468         const u8 *ssid = params->ssids[0].ssid;
01469         size_t ssid_len = params->ssids[0].ssid_len;
01470 
01471         if (ssid == NULL) {
01472                 /* Use standard Linux Wireless Extensions ioctl if possible
01473                  * because some drivers using hostap code in wpa_supplicant
01474                  * might not support Host AP specific scan request (with SSID
01475                  * info). */
01476                 return wpa_driver_wext_scan(drv->wext, params);
01477         }
01478 
01479         if (ssid_len > 32)
01480                 ssid_len = 32;
01481 
01482         os_memset(&param, 0, sizeof(param));
01483         param.cmd = PRISM2_HOSTAPD_SCAN_REQ;
01484         param.u.scan_req.ssid_len = ssid_len;
01485         os_memcpy(param.u.scan_req.ssid, ssid, ssid_len);
01486         ret = hostapd_ioctl(drv, &param, sizeof(param), 1);
01487 
01488         /* Not all drivers generate "scan completed" wireless event, so try to
01489          * read results after a timeout. */
01490         eloop_cancel_timeout(wpa_driver_wext_scan_timeout, drv->wext,
01491                              drv->ctx);
01492         eloop_register_timeout(3, 0, wpa_driver_wext_scan_timeout, drv->wext,
01493                                drv->ctx);
01494 
01495         return ret;
01496 }
01497 
01498 
01499 static int wpa_driver_hostap_set_auth_alg(void *priv, int auth_alg)
01500 {
01501         struct wpa_driver_hostap_data *drv = priv;
01502         int algs = 0;
01503 
01504         if (auth_alg & WPA_AUTH_ALG_OPEN)
01505                 algs |= 1;
01506         if (auth_alg & WPA_AUTH_ALG_SHARED)
01507                 algs |= 2;
01508         if (auth_alg & WPA_AUTH_ALG_LEAP)
01509                 algs |= 4;
01510         if (algs == 0)
01511                 algs = 1; /* at least one algorithm should be set */
01512 
01513         return prism2param(drv, PRISM2_PARAM_AP_AUTH_ALGS, algs);
01514 }
01515 
01516 
01517 static int wpa_driver_hostap_get_bssid(void *priv, u8 *bssid)
01518 {
01519         struct wpa_driver_hostap_data *drv = priv;
01520         return wpa_driver_wext_get_bssid(drv->wext, bssid);
01521 }
01522 
01523 
01524 static int wpa_driver_hostap_get_ssid(void *priv, u8 *ssid)
01525 {
01526         struct wpa_driver_hostap_data *drv = priv;
01527         return wpa_driver_wext_get_ssid(drv->wext, ssid);
01528 }
01529 
01530 
01531 static struct wpa_scan_results * wpa_driver_hostap_get_scan_results(void *priv)
01532 {
01533         struct wpa_driver_hostap_data *drv = priv;
01534         return wpa_driver_wext_get_scan_results(drv->wext);
01535 }
01536 
01537 
01538 static int wpa_driver_hostap_set_operstate(void *priv, int state)
01539 {
01540         struct wpa_driver_hostap_data *drv = priv;
01541         return wpa_driver_wext_set_operstate(drv->wext, state);
01542 }
01543 
01544 
01545 static void * wpa_driver_hostap_init(void *ctx, const char *ifname)
01546 {
01547         struct wpa_driver_hostap_data *drv;
01548 
01549         drv = os_zalloc(sizeof(*drv));
01550         if (drv == NULL)
01551                 return NULL;
01552         drv->wext = wpa_driver_wext_init(ctx, ifname);
01553         if (drv->wext == NULL) {
01554                 os_free(drv);
01555                 return NULL;
01556         }
01557 
01558         drv->ctx = ctx;
01559         os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
01560         drv->sock = socket(PF_INET, SOCK_DGRAM, 0);
01561         if (drv->sock < 0) {
01562                 perror("socket");
01563                 wpa_driver_wext_deinit(drv->wext);
01564                 os_free(drv);
01565                 return NULL;
01566         }
01567 
01568         if (os_strncmp(ifname, "wlan", 4) == 0) {
01569                 /*
01570                  * Host AP driver may use both wlan# and wifi# interface in
01571                  * wireless events.
01572                  */
01573                 char ifname2[IFNAMSIZ + 1];
01574                 os_strlcpy(ifname2, ifname, sizeof(ifname2));
01575                 os_memcpy(ifname2, "wifi", 4);
01576                 wpa_driver_wext_alternative_ifindex(drv->wext, ifname2);
01577         }
01578 
01579         wpa_driver_hostap_set_wpa(drv, 1);
01580 
01581         return drv;
01582 }
01583 
01584 
01585 static void wpa_driver_hostap_deinit(void *priv)
01586 {
01587         struct wpa_driver_hostap_data *drv = priv;
01588         wpa_driver_hostap_set_wpa(drv, 0);
01589         wpa_driver_wext_deinit(drv->wext);
01590         close(drv->sock);
01591         os_free(drv);
01592 }
01593 
01594 #endif /* HOSTAPD */
01595 
01596 
01597 const struct wpa_driver_ops wpa_driver_hostap_ops = {
01598         .name = "hostap",
01599         .desc = "Host AP driver (Intersil Prism2/2.5/3)",
01600         .set_key = wpa_driver_hostap_set_key,
01601 #ifdef HOSTAPD
01602         .hapd_init = hostap_init,
01603         .hapd_deinit = hostap_driver_deinit,
01604         .set_ieee8021x = hostap_set_ieee8021x,
01605         .set_privacy = hostap_set_privacy,
01606         .get_seqnum = hostap_get_seqnum,
01607         .flush = hostap_flush,
01608         .set_generic_elem = hostap_set_generic_elem,
01609         .read_sta_data = hostap_read_sta_data,
01610         .hapd_send_eapol = hostap_send_eapol,
01611         .sta_set_flags = hostap_sta_set_flags,
01612         .sta_deauth = hostap_sta_deauth,
01613         .sta_disassoc = hostap_sta_disassoc,
01614         .sta_remove = hostap_sta_remove,
01615         .hapd_set_ssid = hostap_set_ssid,
01616         .send_mlme = hostap_send_mlme,
01617         .sta_add = hostap_sta_add,
01618         .get_inact_sec = hostap_get_inact_sec,
01619         .sta_clear_stats = hostap_sta_clear_stats,
01620         .get_hw_feature_data = hostap_get_hw_feature_data,
01621         .set_ap_wps_ie = hostap_set_ap_wps_ie,
01622 #else /* HOSTAPD */
01623         .get_bssid = wpa_driver_hostap_get_bssid,
01624         .get_ssid = wpa_driver_hostap_get_ssid,
01625         .set_countermeasures = wpa_driver_hostap_set_countermeasures,
01626         .scan2 = wpa_driver_hostap_scan,
01627         .get_scan_results2 = wpa_driver_hostap_get_scan_results,
01628         .deauthenticate = wpa_driver_hostap_deauthenticate,
01629         .disassociate = wpa_driver_hostap_disassociate,
01630         .associate = wpa_driver_hostap_associate,
01631         .init = wpa_driver_hostap_init,
01632         .deinit = wpa_driver_hostap_deinit,
01633         .set_operstate = wpa_driver_hostap_set_operstate,
01634 #endif /* HOSTAPD */
01635 };


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