Fix IEEE 802.11r key derivation function to match with the standard
[wpasupplicant] / hostapd / driver_hostap.c
index 728f02d..57c6ee8 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * hostapd / Kernel driver communication with Linux Host AP driver
- * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
 
 #include "hostapd.h"
 #include "driver.h"
-#include "ieee802_1x.h"
 #include "eloop.h"
 #include "priv_netlink.h"
-#include "ieee802_11.h"
-#include "sta_info.h"
 #include "hostap_common.h"
 #include "hw_features.h"
+#include "ieee802_11_defs.h"
 
+static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
 
 struct hostap_driver_data {
        struct hostapd_data *hapd;
@@ -55,21 +54,24 @@ struct hostap_driver_data {
 
        u8 *generic_ie;
        size_t generic_ie_len;
+       u8 *wps_ie;
+       size_t wps_ie_len;
 };
 
 
 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
                         int len);
 static int hostap_set_iface_flags(void *priv, int dev_up);
+static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason);
+static int hostap_sta_deauth(void *priv, const u8 *addr, int reason);
 
-static void handle_data(struct hostapd_data *hapd, u8 *buf, size_t len,
+static void handle_data(struct hostap_driver_data *drv, u8 *buf, size_t len,
                        u16 stype)
 {
        struct ieee80211_hdr *hdr;
        u16 fc, ethertype;
        u8 *pos, *sa;
        size_t left;
-       struct sta_info *sta;
 
        if (len < sizeof(struct ieee80211_hdr))
                return;
@@ -83,20 +85,7 @@ static void handle_data(struct hostapd_data *hapd, u8 *buf, size_t len,
        }
 
        sa = hdr->addr2;
-       sta = ap_get_sta(hapd, sa);
-       if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
-               printf("Data frame from not associated STA " MACSTR "\n",
-                      MAC2STR(sa));
-               if (sta && (sta->flags & WLAN_STA_AUTH))
-                       hostapd_sta_disassoc(
-                               hapd, sa,
-                               WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
-               else
-                       hostapd_sta_deauth(
-                               hapd, sa,
-                               WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
-               return;
-       }
+       hostapd_rx_from_unknown_sta(drv->hapd, sa);
 
        pos = (u8 *) (hdr + 1);
        left = len - sizeof(*hdr);
@@ -123,7 +112,7 @@ static void handle_data(struct hostapd_data *hapd, u8 *buf, size_t len,
        left -= 2;
        switch (ethertype) {
        case ETH_P_PAE:
-               ieee802_1x_receive(hapd, sa, pos, left);
+               hostapd_eapol_receive(drv->hapd, sa, pos, left);
                break;
 
        default:
@@ -133,12 +122,11 @@ static void handle_data(struct hostapd_data *hapd, u8 *buf, size_t len,
 }
 
 
-static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
-                              int ok)
+static void handle_tx_callback(struct hostap_driver_data *drv, u8 *buf,
+                              size_t len, int ok)
 {
        struct ieee80211_hdr *hdr;
        u16 fc, type, stype;
-       struct sta_info *sta;
 
        hdr = (struct ieee80211_hdr *) buf;
        fc = le_to_host16(hdr->frame_control);
@@ -150,7 +138,7 @@ static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
        case WLAN_FC_TYPE_MGMT:
                wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
                           ok ? "ACK" : "fail");
-               ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
+               hostapd_mgmt_tx_cb(drv->hapd, buf, len, stype, ok);
                break;
        case WLAN_FC_TYPE_CTRL:
                wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
@@ -159,17 +147,7 @@ static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
        case WLAN_FC_TYPE_DATA:
                wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
                           ok ? "ACK" : "fail");
-               sta = ap_get_sta(hapd, hdr->addr1);
-               if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
-                       wpa_printf(MSG_DEBUG, "STA " MACSTR
-                                  " %s pending activity poll",
-                                  MAC2STR(sta->addr),
-                                  ok ? "ACKed" : "did not ACK");
-                       if (ok)
-                               sta->flags &= ~WLAN_STA_PENDING_POLL;
-               }
-               if (sta)
-                       ieee802_1x_tx_status(hapd, sta, buf, len, ok);
+               hostapd_tx_status(drv->hapd, hdr->addr1, buf, len, ok);
                break;
        default:
                printf("unknown TX callback frame type %d\n", type);
@@ -178,7 +156,7 @@ static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
 }
 
 
-static void handle_frame(struct hostapd_data *hapd, u8 *buf, size_t len)
+static void handle_frame(struct hostap_driver_data *drv, u8 *buf, size_t len)
 {
        struct ieee80211_hdr *hdr;
        u16 fc, extra_len, type, stype;
@@ -220,7 +198,7 @@ static void handle_frame(struct hostapd_data *hapd, u8 *buf, size_t len)
                len -= extra_len + 2;
                extra = buf + len;
        } else if (ver == 1 || ver == 2) {
-               handle_tx_callback(hapd, buf, data_len, ver == 2 ? 1 : 0);
+               handle_tx_callback(drv, buf, data_len, ver == 2 ? 1 : 0);
                return;
        } else if (ver != 0) {
                printf("unknown protocol version %d\n", ver);
@@ -231,14 +209,14 @@ static void handle_frame(struct hostapd_data *hapd, u8 *buf, size_t len)
        case WLAN_FC_TYPE_MGMT:
                if (stype != WLAN_FC_STYPE_BEACON)
                        wpa_printf(MSG_MSGDUMP, "MGMT");
-               ieee802_11_mgmt(hapd, buf, data_len, stype, NULL);
+               hostapd_mgmt_rx(drv->hapd, buf, data_len, stype, NULL);
                break;
        case WLAN_FC_TYPE_CTRL:
                wpa_printf(MSG_DEBUG, "CTRL");
                break;
        case WLAN_FC_TYPE_DATA:
                wpa_printf(MSG_DEBUG, "DATA");
-               handle_data(hapd, buf, data_len, stype);
+               handle_data(drv, buf, data_len, stype);
                break;
        default:
                wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
@@ -249,7 +227,7 @@ static void handle_frame(struct hostapd_data *hapd, u8 *buf, size_t len)
 
 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
 {
-       struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
+       struct hostap_driver_data *drv = eloop_ctx;
        int len;
        unsigned char buf[3000];
 
@@ -259,7 +237,7 @@ static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
                return;
        }
 
-       handle_frame(hapd, buf, len);
+       handle_frame(drv, buf, len);
 }
 
 
@@ -274,8 +252,7 @@ static int hostap_init_sockets(struct hostap_driver_data *drv)
                return -1;
        }
 
-       if (eloop_register_read_sock(drv->sock, handle_read, drv->hapd, NULL))
-       {
+       if (eloop_register_read_sock(drv->sock, handle_read, drv, NULL)) {
                printf("Could not register read socket\n");
                return -1;
        }
@@ -770,7 +747,7 @@ static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
        int res;
        size_t blen, elem_len;
 
-       elem_len = drv->generic_ie_len;
+       elem_len = drv->generic_ie_len + drv->wps_ie_len;
        blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
        if (blen < sizeof(*param))
                blen = sizeof(*param);
@@ -785,6 +762,10 @@ static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
                os_memcpy(param->u.generic_elem.data, drv->generic_ie,
                          drv->generic_ie_len);
        }
+       if (drv->wps_ie) {
+               os_memcpy(&param->u.generic_elem.data[drv->generic_ie_len],
+                         drv->wps_ie, drv->wps_ie_len);
+       }
        wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
                    param->u.generic_elem.data, elem_len);
        res = hostapd_ioctl(drv, param, blen);
@@ -815,6 +796,36 @@ static int hostap_set_generic_elem(const char *ifname, void *priv,
 }
 
 
+static int hostap_set_wps_beacon_ie(const char *ifname, void *priv,
+                                   const u8 *ie, size_t len)
+{
+       /* Host AP driver supports only one set of extra IEs, so we need to
+        * use the ProbeResp IEs also for Beacon frames since they include more
+        * information. */
+       return 0;
+}
+
+
+static int hostap_set_wps_probe_resp_ie(const char *ifname, void *priv,
+                                       const u8 *ie, size_t len)
+{
+       struct hostap_driver_data *drv = priv;
+
+       os_free(drv->wps_ie);
+       drv->wps_ie = NULL;
+       drv->wps_ie_len = 0;
+       if (ie) {
+               drv->wps_ie = os_malloc(len);
+               if (drv->wps_ie == NULL)
+                       return -1;
+               os_memcpy(drv->wps_ie, ie, len);
+               drv->wps_ie_len = len;
+       }
+
+       return hostapd_ioctl_set_generic_elem(drv);
+}
+
+
 static void
 hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
                                       char *custom)
@@ -833,7 +844,7 @@ hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
                }
                pos += 5;
                if (hwaddr_aton(pos, addr) == 0) {
-                       ieee80211_michael_mic_failure(drv->hapd, addr, 1);
+                       hostapd_michael_mic_failure(drv->hapd, addr);
                } else {
                        wpa_printf(MSG_DEBUG,
                                   "MLME-MICHAELMICFAILURE.indication "
@@ -1095,15 +1106,6 @@ static void * hostap_init(struct hostapd_data *hapd)
                return NULL;
        }
 
-       if (hapd->conf->assoc_ap &&
-           hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 1)) {
-               printf("Could not enable hostapd STA mode for interface %s\n",
-                      drv->iface);
-               close(drv->ioctl_sock);
-               free(drv);
-               return NULL;
-       }
-
        if (hostap_init_sockets(drv)) {
                close(drv->ioctl_sock);
                free(drv);
@@ -1129,6 +1131,7 @@ static void hostap_driver_deinit(void *priv)
                close(drv->sock);
 
        os_free(drv->generic_ie);
+       os_free(drv->wps_ie);
 
        free(drv);
 }
@@ -1203,6 +1206,9 @@ static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
        for (i = 0; i < 14; i++) {
                mode->channels[i].chan = i + 1;
                mode->channels[i].freq = chan2freq[i];
+               /* TODO: Get allowed channel list from the driver */
+               if (i >= 11)
+                       mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
        }
 
        mode->rates[0].rate = 10;
@@ -1243,4 +1249,6 @@ const struct wpa_driver_ops wpa_driver_hostap_ops = {
        .get_inact_sec = hostap_get_inact_sec,
        .sta_clear_stats = hostap_sta_clear_stats,
        .get_hw_feature_data = hostap_get_hw_feature_data,
+       .set_wps_beacon_ie = hostap_set_wps_beacon_ie,
+       .set_wps_probe_resp_ie = hostap_set_wps_probe_resp_ie,
 };