Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / src / drivers / driver_bsd.c
index 7700a85..bab1f03 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,6 +61,9 @@ 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 */
@@ -265,17 +264,24 @@ bsd_ctrl_iface(void *priv, int enable)
        os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name));
 
        if (ioctl(drv->sock, SIOCGIFFLAGS, &ifr) < 0) {
-               perror("ioctl[SIOCGIFFLAGS]");
+               wpa_printf(MSG_ERROR, "ioctl[SIOCGIFFLAGS]: %s",
+                          strerror(errno));
                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]");
+               wpa_printf(MSG_ERROR, "ioctl[SIOCSIFFLAGS]: %s",
+                          strerror(errno));
                return -1;
        }
 
@@ -288,6 +294,9 @@ bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg,
            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 +304,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 +341,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 +351,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));
@@ -377,22 +406,24 @@ bsd_configure_wpa(void *priv, struct wpa_bss_params *params)
                v = IEEE80211_CIPHER_NONE;
                break;
        default:
-               printf("Unknown group key cipher %u\n",
-                       params->wpa_group);
+               wpa_printf(MSG_INFO, "Unknown group key cipher %u",
+                          params->wpa_group);
                return -1;
        }
        wpa_printf(MSG_DEBUG, "%s: group key cipher=%s (%u)",
                   __func__, ciphernames[v], v);
        if (set80211param(priv, IEEE80211_IOC_MCASTCIPHER, v)) {
-               printf("Unable to set group key cipher to %u (%s)\n",
-                       v, ciphernames[v]);
+               wpa_printf(MSG_INFO,
+                          "Unable to set group key cipher to %u (%s)",
+                          v, ciphernames[v]);
                return -1;
        }
        if (v == IEEE80211_CIPHER_WEP) {
                /* key length is done only for specific ciphers */
                v = (params->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5);
                if (set80211param(priv, IEEE80211_IOC_MCASTKEYLEN, v)) {
-                       printf("Unable to set group key length to %u\n", v);
+                       wpa_printf(MSG_INFO,
+                                  "Unable to set group key length to %u", v);
                        return -1;
                }
        }
@@ -406,7 +437,8 @@ bsd_configure_wpa(void *priv, struct wpa_bss_params *params)
                v |= 1<<IEEE80211_CIPHER_NONE;
        wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v);
        if (set80211param(priv, IEEE80211_IOC_UCASTCIPHERS, v)) {
-               printf("Unable to set pairwise key ciphers to 0x%x\n", v);
+               wpa_printf(MSG_INFO,
+                          "Unable to set pairwise key ciphers to 0x%x", v);
                return -1;
        }
 
@@ -414,8 +446,9 @@ bsd_configure_wpa(void *priv, struct wpa_bss_params *params)
                   __func__, params->wpa_key_mgmt);
        if (set80211param(priv, IEEE80211_IOC_KEYMGTALGS,
                          params->wpa_key_mgmt)) {
-               printf("Unable to set key management algorithms to 0x%x\n",
-                       params->wpa_key_mgmt);
+               wpa_printf(MSG_INFO,
+                          "Unable to set key management algorithms to 0x%x",
+                          params->wpa_key_mgmt);
                return -1;
        }
 
@@ -425,14 +458,15 @@ bsd_configure_wpa(void *priv, struct wpa_bss_params *params)
        wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x",
                   __func__, params->rsn_preauth);
        if (set80211param(priv, IEEE80211_IOC_RSNCAPS, v)) {
-               printf("Unable to set RSN capabilities to 0x%x\n", v);
+               wpa_printf(MSG_INFO, "Unable to set RSN capabilities to 0x%x",
+                          v);
                return -1;
        }
 #endif /* IEEE80211_IOC_APPIE */
 
        wpa_printf(MSG_DEBUG, "%s: enable WPA= 0x%x", __func__, params->wpa);
        if (set80211param(priv, IEEE80211_IOC_WPA, params->wpa)) {
-               printf("Unable to set WPA to %u\n", params->wpa);
+               wpa_printf(MSG_INFO, "Unable to set WPA to %u", params->wpa);
                return -1;
        }
        return 0;
@@ -467,26 +501,6 @@ bsd_set_ieee8021x(void *priv, struct wpa_bss_params *params)
        return bsd_ctrl_iface(priv, 1);
 }
 
-static int
-bsd_set_sta_authorized(void *priv, const u8 *addr,
-                      int total_flags, int flags_or, int flags_and)
-{
-       int authorized = -1;
-
-       /* For now, only support setting Authorized flag */
-       if (flags_or & WPA_STA_AUTHORIZED)
-               authorized = 1;
-       if (!(flags_and & WPA_STA_AUTHORIZED))
-               authorized = 0;
-
-       if (authorized < 0)
-               return 0;
-
-       return bsd_send_mlme_param(priv, authorized ?
-                                  IEEE80211_MLME_AUTHORIZE :
-                                  IEEE80211_MLME_UNAUTHORIZE, 0, addr);
-}
-
 static void
 bsd_new_sta(void *priv, void *ctx, u8 addr[IEEE80211_ADDR_LEN])
 {
@@ -500,7 +514,8 @@ bsd_new_sta(void *priv, void *ctx, u8 addr[IEEE80211_ADDR_LEN])
        memset(&ie, 0, sizeof(ie));
        memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN);
        if (get80211var(priv, IEEE80211_IOC_WPAIE, &ie, sizeof(ie)) < 0) {
-               printf("Failed to get WPA/RSN information element.\n");
+               wpa_printf(MSG_INFO,
+                          "Failed to get WPA/RSN information element");
                goto no_ie;
        }
        iebuf = ie.wpa_ie;
@@ -511,12 +526,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;
 
@@ -536,12 +551,21 @@ bsd_set_freq(void *priv, struct hostapd_freq_params *freq)
        u32 mode;
        int channel = freq->channel;
 
-       if (channel < 14)
-               mode = IFM_IEEE80211_11G;
-       else if (channel == 14)
+       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__);
@@ -570,6 +594,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", __func__,
+                          strerror(errno));
+               len = 2048;
+       }
+
+       return len;
+}
 
 #ifdef HOSTAPD
 
@@ -621,7 +660,7 @@ bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx,
        wk.ik_keyix = idx;
 
        if (get80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) {
-               printf("Failed to get encryption.\n");
+               wpa_printf(MSG_INFO, "Failed to get encryption");
                return -1;
        }
 
@@ -692,7 +731,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;
@@ -701,17 +739,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",
+                                  __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;
@@ -768,14 +807,23 @@ bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params)
 
        drv = os_zalloc(sizeof(struct bsd_driver_data));
        if (drv == NULL) {
-               printf("Could not allocate memory for bsd driver data\n");
+               wpa_printf(MSG_ERROR, "Could not allocate memory for bsd driver data");
+               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 bad;
        }
 
        drv->hapd = hapd;
        drv->sock = socket(PF_INET, SOCK_DGRAM, 0);
        if (drv->sock < 0) {
-               perror("socket[PF_INET,SOCK_DGRAM]");
+               wpa_printf(MSG_ERROR, "socket[PF_INET,SOCK_DGRAM]: %s",
+                          strerror(errno));
                goto bad;
        }
        os_strlcpy(drv->ifname, params->ifname, sizeof(drv->ifname));
@@ -793,7 +841,8 @@ bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params)
 
        drv->route = socket(PF_ROUTE, SOCK_RAW, 0);
        if (drv->route < 0) {
-               perror("socket(PF_ROUTE,SOCK_RAW)");
+               wpa_printf(MSG_ERROR, "socket(PF_ROUTE,SOCK_RAW): %s",
+                          strerror(errno));
                goto bad;
        }
        eloop_register_read_sock(drv->route, bsd_wireless_event_receive, drv,
@@ -811,8 +860,8 @@ bad:
                l2_packet_deinit(drv->sock_xmit);
        if (drv->sock >= 0)
                close(drv->sock);
-       if (drv != NULL)
-               os_free(drv);
+       os_free(drv->event_buf);
+       os_free(drv);
        return NULL;
 }
 
@@ -831,9 +880,38 @@ 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);
 }
 
+
+static int
+bsd_commit(void *priv)
+{
+       return bsd_ctrl_iface(priv, 1);
+}
+
+
+static int
+bsd_set_sta_authorized(void *priv, const u8 *addr,
+                      unsigned int total_flags, unsigned int flags_or,
+                      unsigned int flags_and)
+{
+       int authorized = -1;
+
+       /* For now, only support setting Authorized flag */
+       if (flags_or & WPA_STA_AUTHORIZED)
+               authorized = 1;
+       if (!(flags_and & WPA_STA_AUTHORIZED))
+               authorized = 0;
+
+       if (authorized < 0)
+               return 0;
+
+       return bsd_send_mlme_param(priv, authorized ?
+                                  IEEE80211_MLME_AUTHORIZE :
+                                  IEEE80211_MLME_UNAUTHORIZE, 0, addr);
+}
 #else /* HOSTAPD */
 
 static int
@@ -931,13 +1009,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;
@@ -1018,9 +1089,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);
 
@@ -1116,7 +1187,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;
@@ -1126,17 +1196,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",
+                                  __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));
@@ -1247,6 +1318,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);
 
@@ -1268,13 +1344,18 @@ wpa_driver_bsd_add_scan_entry(struct wpa_scan_results *res,
        *pos++ = 1;
        *pos++ = sr->isr_erp;
 
+#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+       os_memcpy(pos, (u8 *)(sr + 1) + sr->isr_ssid_len + sr->isr_meshid_len,
+                 sr->isr_ie_len);
+#else
        os_memcpy(pos, (u8 *)(sr + 1) + sr->isr_ssid_len, sr->isr_ie_len);
+#endif
        pos += sr->isr_ie_len;
 
        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;
@@ -1388,6 +1469,33 @@ static int wpa_driver_bsd_capa(struct bsd_driver_data *drv)
        return 0;
 }
 
+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 void *
 wpa_driver_bsd_init(void *ctx, const char *ifname)
 {
@@ -1398,6 +1506,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
@@ -1413,6 +1530,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;
@@ -1420,11 +1543,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",
@@ -1445,10 +1563,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
@@ -1474,6 +1595,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);
 }
 
@@ -1500,6 +1622,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,
+       .sta_set_flags          = bsd_set_sta_authorized,
+       .commit                 = bsd_commit,
 #else /* HOSTAPD */
        .init                   = wpa_driver_bsd_init,
        .deinit                 = wpa_driver_bsd_deinit,
@@ -1509,7 +1633,6 @@ 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 */
@@ -1519,6 +1642,5 @@ const struct wpa_driver_ops wpa_driver_bsd_ops = {
        .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,
 };