bsd: Prepare event buffer on init process
[mech_eap.git] / src / drivers / driver_bsd.c
index 99de6c7..097e7d6 100644 (file)
@@ -3,23 +3,19 @@
  * Copyright (c) 2004, Sam Leffler <sam@errno.com>
  * Copyright (c) 2004, 2Wire, Inc
  *
- * 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
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  */
 
 #include "includes.h"
 #include <sys/ioctl.h>
+#include <sys/sysctl.h>
 
 #include "common.h"
 #include "driver.h"
 #include "eloop.h"
 #include "common/ieee802_11_defs.h"
+#include "common/wpa_common.h"
 
 #include <net/if.h>
 #include <net/if_media.h>
@@ -65,10 +61,41 @@ struct bsd_driver_data {
        int     prev_roaming;   /* roaming state to restore on deinit */
        int     prev_privacy;   /* privacy state to restore on deinit */
        int     prev_wpa;       /* wpa state to restore on deinit */
+       enum ieee80211_opmode opmode;   /* operation mode */
+       char    *event_buf;
+       size_t  event_buf_len;
 };
 
 /* Generic functions for hostapd and wpa_supplicant */
 
+static enum ieee80211_opmode
+get80211opmode(struct bsd_driver_data *drv)
+{
+       struct ifmediareq ifmr;
+
+       (void) memset(&ifmr, 0, sizeof(ifmr));
+       (void) os_strlcpy(ifmr.ifm_name, drv->ifname, sizeof(ifmr.ifm_name));
+
+       if (ioctl(drv->sock, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) {
+               if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) {
+                       if (ifmr.ifm_current & IFM_FLAG0)
+                               return IEEE80211_M_AHDEMO;
+                       else
+                               return IEEE80211_M_IBSS;
+               }
+               if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP)
+                       return IEEE80211_M_HOSTAP;
+               if (ifmr.ifm_current & IFM_IEEE80211_MONITOR)
+                       return IEEE80211_M_MONITOR;
+#ifdef IEEE80211_M_MBSS
+               if (ifmr.ifm_current & IFM_IEEE80211_MBSS)
+                       return IEEE80211_M_MBSS;
+#endif /* IEEE80211_M_MBSS */
+       }
+       return IEEE80211_M_STA;
+}
+
+
 static int
 bsd_set80211(void *priv, int op, int val, const void *arg, int arg_len)
 {
@@ -269,10 +296,15 @@ bsd_ctrl_iface(void *priv, int enable)
                return -1;
        }
 
-       if (enable)
+       if (enable) {
+               if (ifr.ifr_flags & IFF_UP)
+                       return 0;
                ifr.ifr_flags |= IFF_UP;
-       else
+       } else {
+               if (!(ifr.ifr_flags & IFF_UP))
+                       return 0;
                ifr.ifr_flags &= ~IFF_UP;
+       }
 
        if (ioctl(drv->sock, SIOCSIFFLAGS, &ifr) < 0) {
                perror("ioctl[SIOCSIFFLAGS]");
@@ -282,12 +314,23 @@ bsd_ctrl_iface(void *priv, int enable)
        return 0;
 }
 
+#ifdef HOSTAPD
+static int
+bsd_commit(void *priv)
+{
+       return bsd_ctrl_iface(priv, 1);
+}
+#endif /* HOSTAPD */
+
 static int
 bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg,
            const unsigned char *addr, int key_idx, int set_tx, const u8 *seq,
            size_t seq_len, const u8 *key, size_t key_len)
 {
        struct ieee80211req_key wk;
+#ifdef IEEE80211_KEY_NOREPLAY
+       struct bsd_driver_data *drv = priv;
+#endif /* IEEE80211_KEY_NOREPLAY */
 
        wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%p key_idx=%d set_tx=%d "
                   "seq_len=%zu key_len=%zu", __func__, alg, addr, key_idx,
@@ -295,9 +338,7 @@ bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg,
 
        if (alg == WPA_ALG_NONE) {
 #ifndef HOSTAPD
-               if (addr == NULL ||
-                   os_memcmp(addr, "\xff\xff\xff\xff\xff\xff",
-                             IEEE80211_ADDR_LEN) == 0)
+               if (addr == NULL || is_broadcast_ether_addr(addr))
                        return bsd_del_key(priv, NULL, key_idx);
                else
 #endif /* HOSTAPD */
@@ -334,8 +375,7 @@ bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg,
                 * the address (yech).  Note also that we can only mark global
                 * keys default; doing this for a unicast key is an error.
                 */
-               if (os_memcmp(addr, "\xff\xff\xff\xff\xff\xff",
-                             IEEE80211_ADDR_LEN) == 0) {
+               if (is_broadcast_ether_addr(addr)) {
                        wk.ik_flags |= IEEE80211_KEY_GROUP;
                        wk.ik_keyix = key_idx;
                } else {
@@ -345,8 +385,31 @@ bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg,
        }
        if (wk.ik_keyix != IEEE80211_KEYIX_NONE && set_tx)
                wk.ik_flags |= IEEE80211_KEY_DEFAULT;
+#ifndef HOSTAPD
+#ifdef IEEE80211_KEY_NOREPLAY
+       /*
+        * Ignore replay failures in IBSS and AHDEMO mode.
+        */
+       if (drv->opmode == IEEE80211_M_IBSS ||
+           drv->opmode == IEEE80211_M_AHDEMO)
+               wk.ik_flags |= IEEE80211_KEY_NOREPLAY;
+#endif /* IEEE80211_KEY_NOREPLAY */
+#endif /* HOSTAPD */
        wk.ik_keylen = key_len;
-       os_memcpy(&wk.ik_keyrsc, seq, seq_len);
+       if (seq) {
+#ifdef WORDS_BIGENDIAN
+               /*
+                * wk.ik_keyrsc is in host byte order (big endian), need to
+                * swap it to match with the byte order used in WPA.
+                */
+               int i;
+               u8 *keyrsc = (u8 *) &wk.ik_keyrsc;
+               for (i = 0; i < seq_len; i++)
+                       keyrsc[WPA_KEY_RSC_LEN - i - 1] = seq[i];
+#else /* WORDS_BIGENDIAN */
+               os_memcpy(&wk.ik_keyrsc, seq, seq_len);
+#endif /* WORDS_BIGENDIAN */
+       }
        os_memcpy(wk.ik_keydata, key, key_len);
 
        return set80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk));
@@ -467,6 +530,7 @@ bsd_set_ieee8021x(void *priv, struct wpa_bss_params *params)
        return bsd_ctrl_iface(priv, 1);
 }
 
+#ifdef HOSTAPD
 static int
 bsd_set_sta_authorized(void *priv, const u8 *addr,
                       int total_flags, int flags_or, int flags_and)
@@ -486,6 +550,7 @@ bsd_set_sta_authorized(void *priv, const u8 *addr,
                                   IEEE80211_MLME_AUTHORIZE :
                                   IEEE80211_MLME_UNAUTHORIZE, 0, addr);
 }
+#endif /* HOSTAPD */
 
 static void
 bsd_new_sta(void *priv, void *ctx, u8 addr[IEEE80211_ADDR_LEN])
@@ -511,12 +576,12 @@ bsd_new_sta(void *priv, void *ctx, u8 addr[IEEE80211_ADDR_LEN])
                ielen += 2;
 
 no_ie:
-       drv_event_assoc(ctx, addr, iebuf, ielen);
+       drv_event_assoc(ctx, addr, iebuf, ielen, 0);
 }
 
 static int
 bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
-              int encrypt, const u8 *own_addr)
+              int encrypt, const u8 *own_addr, u32 flags)
 {
        struct bsd_driver_data *drv = priv;
 
@@ -527,20 +592,30 @@ bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
 }
 
 static int
-bsd_set_freq(void *priv, u16 channel)
+bsd_set_freq(void *priv, struct hostapd_freq_params *freq)
 {
        struct bsd_driver_data *drv = priv;
 #ifdef SIOCS80211CHANNEL
        struct ieee80211chanreq creq;
 #endif /* SIOCS80211CHANNEL */
        u32 mode;
-
-       if (channel < 14)
-               mode = IFM_IEEE80211_11G;
-       else if (channel == 14)
+       int channel = freq->channel;
+
+       if (channel < 14) {
+               mode =
+#ifdef CONFIG_IEEE80211N
+                       freq->ht_enabled ? IFM_IEEE80211_11NG :
+#endif /* CONFIG_IEEE80211N */
+                       IFM_IEEE80211_11G;
+       } else if (channel == 14) {
                mode = IFM_IEEE80211_11B;
-       else
-               mode = IFM_IEEE80211_11A;
+       } else {
+               mode =
+#ifdef CONFIG_IEEE80211N
+                       freq->ht_enabled ? IFM_IEEE80211_11NA :
+#endif /* CONFIG_IEEE80211N */
+                       IFM_IEEE80211_11A;
+       }
        if (bsd_set_mediaopt(drv, IFM_MMASK, mode) < 0) {
                wpa_printf(MSG_ERROR, "%s: failed to set modulation mode",
                           __func__);
@@ -550,7 +625,7 @@ bsd_set_freq(void *priv, u16 channel)
 #ifdef SIOCS80211CHANNEL
        os_memset(&creq, 0, sizeof(creq));
        os_strlcpy(creq.i_name, drv->ifname, sizeof(creq.i_name));
-       creq.i_channel = channel;
+       creq.i_channel = (u_int16_t)channel;
        return ioctl(drv->sock, SIOCS80211CHANNEL, &creq);
 #else /* SIOCS80211CHANNEL */
        return set80211param(priv, IEEE80211_IOC_CHANNEL, channel);
@@ -569,6 +644,21 @@ bsd_set_opt_ie(void *priv, const u8 *ie, size_t ie_len)
        return 0;
 }
 
+static size_t
+rtbuf_len(void)
+{
+       size_t len;
+
+       int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET, NET_RT_DUMP, 0};
+
+       if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
+               wpa_printf(MSG_WARNING, "%s failed: %s\n", __func__,
+                          strerror(errno));
+               len = 2048;
+       }
+
+       return len;
+}
 
 #ifdef HOSTAPD
 
@@ -691,7 +781,6 @@ static void
 bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
 {
        struct bsd_driver_data *drv = ctx;
-       char buf[2048];
        struct if_announcemsghdr *ifan;
        struct rt_msghdr *rtm;
        struct ieee80211_michael_event *mic;
@@ -700,17 +789,18 @@ bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
        int n;
        union wpa_event_data data;
 
-       n = read(sock, buf, sizeof(buf));
+       n = read(sock, drv->event_buf, drv->event_buf_len);
        if (n < 0) {
                if (errno != EINTR && errno != EAGAIN)
-                       perror("read(PF_ROUTE)");
+                       wpa_printf(MSG_ERROR, "%s read() failed: %s\n",
+                                  __func__, strerror(errno));
                return;
        }
 
-       rtm = (struct rt_msghdr *) buf;
+       rtm = (struct rt_msghdr *) drv->event_buf;
        if (rtm->rtm_version != RTM_VERSION) {
-               wpa_printf(MSG_DEBUG, "Routing message version %d not "
-                       "understood\n", rtm->rtm_version);
+               wpa_printf(MSG_DEBUG, "Invalid routing message version=%d",
+                          rtm->rtm_version);
                return;
        }
        ifan = (struct if_announcemsghdr *) rtm;
@@ -760,12 +850,6 @@ handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
        drv_event_eapol_rx(drv->hapd, src_addr, buf, len);
 }
 
-static int
-hostapd_bsd_set_freq(void *priv, struct hostapd_freq_params *freq)
-{
-       return bsd_set_freq(priv, freq->channel);
-}
-
 static void *
 bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params)
 {
@@ -777,6 +861,14 @@ bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params)
                goto bad;
        }
 
+       drv->event_buf_len = rtbuf_len();
+
+       drv->event_buf = os_malloc(drv->event_buf_len);
+       if (drv->event_buf == NULL) {
+               wpa_printf(MSG_ERROR, "%s: os_malloc() failed", __func__);
+               goto bad;
+       }
+
        drv->hapd = hapd;
        drv->sock = socket(PF_INET, SOCK_DGRAM, 0);
        if (drv->sock < 0) {
@@ -816,6 +908,7 @@ bad:
                l2_packet_deinit(drv->sock_xmit);
        if (drv->sock >= 0)
                close(drv->sock);
+       os_free(drv->event_buf);
        if (drv != NULL)
                os_free(drv);
        return NULL;
@@ -836,6 +929,7 @@ bsd_deinit(void *priv)
                close(drv->sock);
        if (drv->sock_xmit != NULL)
                l2_packet_deinit(drv->sock_xmit);
+       os_free(drv->event_buf);
        os_free(drv);
 }
 
@@ -936,13 +1030,6 @@ wpa_driver_bsd_deauthenticate(void *priv, const u8 *addr, int reason_code)
 }
 
 static int
-wpa_driver_bsd_disassociate(void *priv, const u8 *addr, int reason_code)
-{
-       return bsd_send_mlme_param(priv, IEEE80211_MLME_DISASSOC, reason_code,
-                                  addr);
-}
-
-static int
 wpa_driver_bsd_set_auth_alg(void *priv, int auth_alg)
 {
        int authmode;
@@ -972,7 +1059,6 @@ wpa_driver_bsd_associate(void *priv, struct wpa_driver_associate_params *params)
        struct bsd_driver_data *drv = priv;
        struct ieee80211req_mlme mlme;
        u32 mode;
-       u16 channel;
        int privacy;
        int ret = 0;
 
@@ -1007,18 +1093,6 @@ wpa_driver_bsd_associate(void *priv, struct wpa_driver_associate_params *params)
        }
 
        if (params->mode == IEEE80211_MODE_AP) {
-               if (params->freq >= 2412 && params->freq <= 2472)
-                       channel = (params->freq - 2407) / 5;
-               else if (params->freq == 2484)
-                       channel = 14;
-               else if ((params->freq >= 5180 && params->freq <= 5240) ||
-                        (params->freq >= 5745 && params->freq <= 5825))
-                       channel = (params->freq - 5000) / 5;
-               else
-                       channel = 0;
-               if (bsd_set_freq(drv, channel) < 0)
-                       return -1;
-
                drv->sock_xmit = l2_packet_init(drv->ifname, NULL, ETH_P_EAPOL,
                                                handle_read, drv, 0);
                if (drv->sock_xmit == NULL)
@@ -1036,9 +1110,9 @@ wpa_driver_bsd_associate(void *priv, struct wpa_driver_associate_params *params)
        if (wpa_driver_bsd_set_wpa_ie(drv, params->wpa_ie, params->wpa_ie_len) < 0)
                return -1;
 
-       privacy = !(params->pairwise_suite == CIPHER_NONE &&
-           params->group_suite == CIPHER_NONE &&
-           params->key_mgmt_suite == KEY_MGMT_NONE &&
+       privacy = !(params->pairwise_suite == WPA_CIPHER_NONE &&
+           params->group_suite == WPA_CIPHER_NONE &&
+           params->key_mgmt_suite == WPA_KEY_MGMT_NONE &&
            params->wpa_ie_len == 0);
        wpa_printf(MSG_DEBUG, "%s: set PRIVACY %u", __func__, privacy);
 
@@ -1134,7 +1208,6 @@ static void
 wpa_driver_bsd_event_receive(int sock, void *ctx, void *sock_ctx)
 {
        struct bsd_driver_data *drv = sock_ctx;
-       char buf[2048];
        struct if_announcemsghdr *ifan;
        struct if_msghdr *ifm;
        struct rt_msghdr *rtm;
@@ -1144,17 +1217,18 @@ wpa_driver_bsd_event_receive(int sock, void *ctx, void *sock_ctx)
        struct ieee80211_join_event *join;
        int n;
 
-       n = read(sock, buf, sizeof(buf));
+       n = read(sock, drv->event_buf, drv->event_buf_len);
        if (n < 0) {
                if (errno != EINTR && errno != EAGAIN)
-                       perror("read(PF_ROUTE)");
+                       wpa_printf(MSG_ERROR, "%s read() failed: %s\n",
+                                  __func__, strerror(errno));
                return;
        }
 
-       rtm = (struct rt_msghdr *) buf;
+       rtm = (struct rt_msghdr *) drv->event_buf;
        if (rtm->rtm_version != RTM_VERSION) {
-               wpa_printf(MSG_DEBUG, "Routing message version %d not "
-                       "understood\n", rtm->rtm_version);
+               wpa_printf(MSG_DEBUG, "Invalid routing message version=%d",
+                          rtm->rtm_version);
                return;
        }
        os_memset(&event, 0, sizeof(event));
@@ -1265,6 +1339,11 @@ wpa_driver_bsd_add_scan_entry(struct wpa_scan_results *res,
        result->caps = sr->isr_capinfo;
        result->qual = sr->isr_rssi;
        result->noise = sr->isr_noise;
+       /*
+        * the rssi value reported by the kernel is in 0.5dB steps relative to
+        * the reported noise floor. see ieee80211_node.h for details.
+        */
+       result->level = sr->isr_rssi / 2 + sr->isr_noise;
 
        pos = (u8 *)(result + 1);
 
@@ -1291,8 +1370,8 @@ wpa_driver_bsd_add_scan_entry(struct wpa_scan_results *res,
 
        result->ie_len = pos - (u8 *)(result + 1);
 
-       tmp = os_realloc(res->res,
-                        (res->num + 1) * sizeof(struct wpa_scan_res *));
+       tmp = os_realloc_array(res->res, res->num + 1,
+                              sizeof(struct wpa_scan_res *));
        if (tmp == NULL) {
                os_free(result);
                return;
@@ -1416,6 +1495,15 @@ wpa_driver_bsd_init(void *ctx, const char *ifname)
        drv = os_zalloc(sizeof(*drv));
        if (drv == NULL)
                return NULL;
+
+       drv->event_buf_len = rtbuf_len();
+
+       drv->event_buf = os_malloc(drv->event_buf_len);
+       if (drv->event_buf == NULL) {
+               wpa_printf(MSG_ERROR, "%s: os_malloc() failed", __func__);
+               goto fail1;
+       }
+
        /*
         * NB: We require the interface name be mappable to an index.
         *     This implies we do not support having wpa_supplicant
@@ -1431,6 +1519,12 @@ wpa_driver_bsd_init(void *ctx, const char *ifname)
        drv->sock = socket(PF_INET, SOCK_DGRAM, 0);
        if (drv->sock < 0)
                goto fail1;
+
+       os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
+       /* Down interface during setup. */
+       if (bsd_ctrl_iface(drv, 0) < 0)
+               goto fail;
+
        drv->route = socket(PF_ROUTE, SOCK_RAW, 0);
        if (drv->route < 0)
                goto fail;
@@ -1438,11 +1532,6 @@ wpa_driver_bsd_init(void *ctx, const char *ifname)
                wpa_driver_bsd_event_receive, ctx, drv);
 
        drv->ctx = ctx;
-       os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
-
-       /* Down interface during setup. */
-       if (bsd_ctrl_iface(drv, 0) < 0)
-               goto fail;
 
        if (!GETPARAM(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming)) {
                wpa_printf(MSG_DEBUG, "%s: failed to get roaming state: %s",
@@ -1463,10 +1552,13 @@ wpa_driver_bsd_init(void *ctx, const char *ifname)
        if (wpa_driver_bsd_capa(drv))
                goto fail;
 
+       drv->opmode = get80211opmode(drv);
+
        return drv;
 fail:
        close(drv->sock);
 fail1:
+       os_free(drv->event_buf);
        os_free(drv);
        return NULL;
 #undef GETPARAM
@@ -1492,6 +1584,7 @@ wpa_driver_bsd_deinit(void *priv)
                l2_packet_deinit(drv->sock_xmit);
        (void) close(drv->route);               /* ioctl socket */
        (void) close(drv->sock);                /* event socket */
+       os_free(drv->event_buf);
        os_free(drv);
 }
 
@@ -1518,7 +1611,8 @@ const struct wpa_driver_ops wpa_driver_bsd_ops = {
        .read_sta_data          = bsd_read_sta_driver_data,
        .sta_disassoc           = bsd_sta_disassoc,
        .sta_deauth             = bsd_sta_deauth,
-       .set_freq               = hostapd_bsd_set_freq,
+       .sta_set_flags          = bsd_set_sta_authorized,
+       .commit                 = bsd_commit,
 #else /* HOSTAPD */
        .init                   = wpa_driver_bsd_init,
        .deinit                 = wpa_driver_bsd_deinit,
@@ -1528,15 +1622,14 @@ const struct wpa_driver_ops wpa_driver_bsd_ops = {
        .scan2                  = wpa_driver_bsd_scan,
        .get_scan_results2      = wpa_driver_bsd_get_scan_results2,
        .deauthenticate         = wpa_driver_bsd_deauthenticate,
-       .disassociate           = wpa_driver_bsd_disassociate,
        .associate              = wpa_driver_bsd_associate,
        .get_capa               = wpa_driver_bsd_get_capa,
 #endif /* HOSTAPD */
+       .set_freq               = bsd_set_freq,
        .set_key                = bsd_set_key,
        .set_ieee8021x          = bsd_set_ieee8021x,
        .hapd_set_ssid          = bsd_set_ssid,
        .hapd_get_ssid          = bsd_get_ssid,
        .hapd_send_eapol        = bsd_send_eapol,
-       .sta_set_flags          = bsd_set_sta_authorized,
        .set_generic_elem       = bsd_set_opt_ie,
 };