driver_bsd.c: Reduce code duplication (MLME)
[mech_eap.git] / src / drivers / driver_bsd.c
index aa523d6..57f4537 100644 (file)
@@ -22,6 +22,7 @@
 #include "common/ieee802_11_defs.h"
 
 #include <net/if.h>
+#include <net/if_media.h>
 
 #ifdef __NetBSD__
 #include <net/if_ether.h>
@@ -35,8 +36,8 @@
 #include <netproto/802_11/ieee80211_dragonfly.h>
 #else /* __DragonFly__ */
 #include <net80211/ieee80211.h>
-#include <net80211/ieee80211_crypto.h>
 #include <net80211/ieee80211_ioctl.h>
+#include <net80211/ieee80211_crypto.h>
 #endif /* __DragonFly__ */
 #if __FreeBSD__
 #include <net80211/ieee80211_freebsd.h>
@@ -142,6 +143,89 @@ bsd_set_ssid(int s, const char *ifname, const u8 *ssid, size_t ssid_len)
 #endif
 }
 
+static int
+bsd_get_if_media(int s, const char *ifname)
+{
+       struct ifmediareq ifmr;
+
+       os_memset(&ifmr, 0, sizeof(ifmr));
+       os_strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
+
+       if (ioctl(s, SIOCGIFMEDIA, &ifmr) < 0) {
+               wpa_printf(MSG_ERROR, "%s: SIOCGIFMEDIA %s", __func__,
+                          strerror(errno));
+               return -1;
+       }
+
+       return ifmr.ifm_current;
+}
+
+static int
+bsd_set_if_media(int s, const char *ifname, int media)
+{
+       struct ifreq ifr;
+
+       os_memset(&ifr, 0, sizeof(ifr));
+       os_strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+       ifr.ifr_media = media;
+
+       if (ioctl(s, SIOCSIFMEDIA, &ifr) < 0) {
+               wpa_printf(MSG_ERROR, "%s: SIOCSIFMEDIA %s", __func__,
+                          strerror(errno));
+               return -1;
+       }
+
+       return 0;
+}
+
+static int
+bsd_set_mediaopt(int s, const char *ifname, uint32_t mask, uint32_t mode)
+{
+       int media = bsd_get_if_media(s, ifname);
+
+       if (media < 0)
+               return -1;
+       media &= ~mask;
+       media |= mode;
+       if (bsd_set_if_media(s, ifname, media) < 0)
+               return -1;
+       return 0;
+}
+
+static int
+bsd_del_key(int s, const char *ifname, const u8 *addr, int key_idx)
+{
+       struct ieee80211req_del_key wk;
+
+       os_memset(&wk, 0, sizeof(wk));
+       if (addr == NULL) {
+               wpa_printf(MSG_DEBUG, "%s: key_idx=%d", __func__, key_idx);
+               wk.idk_keyix = key_idx;
+       } else {
+               wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__,
+                          MAC2STR(addr));
+               os_memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
+               wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */
+       }
+
+       return bsd_set80211var(s, ifname, IEEE80211_IOC_DELKEY, &wk,
+                              sizeof(wk));
+}
+
+static int
+bsd_send_mlme_param(int s, const char *ifname, const u8 op, const u16 reason,
+                   const u8 *addr)
+{
+       struct ieee80211req_mlme mlme;
+
+       os_memset(&mlme, 0, sizeof(mlme));
+       mlme.im_op = op;
+       mlme.im_reason = reason;
+       os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
+       return bsd_set80211var(s, ifname, IEEE80211_IOC_MLME, &mlme,
+                              sizeof(mlme));
+}
+
 
 #ifdef HOSTAPD
 
@@ -362,18 +446,10 @@ static int
 bsd_set_sta_authorized(void *priv, const u8 *addr, int authorized)
 {
        struct bsd_driver_data *drv = priv;
-       struct ieee80211req_mlme mlme;
-
-       wpa_printf(MSG_DEBUG, "%s: addr=%s authorized=%d",
-                  __func__, ether_sprintf(addr), authorized);
 
-       if (authorized)
-               mlme.im_op = IEEE80211_MLME_AUTHORIZE;
-       else
-               mlme.im_op = IEEE80211_MLME_UNAUTHORIZE;
-       mlme.im_reason = 0;
-       memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
-       return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
+       return bsd_send_mlme_param(drv->ioctl_sock, drv->iface, authorized ?
+                                  IEEE80211_MLME_AUTHORIZE :
+                                  IEEE80211_MLME_UNAUTHORIZE, 0, addr);
 }
 
 static int
@@ -389,27 +465,7 @@ bsd_sta_set_flags(void *priv, const u8 *addr, int total_flags, int flags_or,
 }
 
 static int
-bsd_del_key(void *priv, const u8 *addr, int key_idx)
-{
-       struct bsd_driver_data *drv = priv;
-       struct ieee80211req_del_key wk;
-
-       wpa_printf(MSG_DEBUG, "%s: addr=%s key_idx=%d",
-                  __func__, ether_sprintf(addr), key_idx);
-
-       memset(&wk, 0, sizeof(wk));
-       if (addr != NULL) {
-               memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
-               wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */
-       } else {
-               wk.idk_keyix = key_idx;
-       }
-
-       return set80211var(drv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk));
-}
-
-static int
-bsd_set_key(const char *ifname, void *priv, wpa_alg alg,
+bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg,
            const u8 *addr, int key_idx, int set_tx, const u8 *seq,
            size_t seq_len, const u8 *key, size_t key_len)
 {
@@ -418,7 +474,7 @@ bsd_set_key(const char *ifname, void *priv, wpa_alg alg,
        u_int8_t cipher;
 
        if (alg == WPA_ALG_NONE)
-               return bsd_del_key(drv, addr, key_idx);
+               return bsd_del_key(drv->ioctl_sock, drv->iface, addr, key_idx);
 
        wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%s key_idx=%d",
                   __func__, alg, ether_sprintf(addr), key_idx);
@@ -542,15 +598,9 @@ static int
 bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, int reason_code)
 {
        struct bsd_driver_data *drv = priv;
-       struct ieee80211req_mlme mlme;
-
-       wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
-                  __func__, ether_sprintf(addr), reason_code);
 
-       mlme.im_op = IEEE80211_MLME_DEAUTH;
-       mlme.im_reason = reason_code;
-       memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
-       return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
+       return bsd_send_mlme_param(drv->ioctl_sock, drv->iface,
+                                  IEEE80211_MLME_DEAUTH, reason_code, addr);
 }
 
 static int
@@ -558,18 +608,12 @@ bsd_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
                 int reason_code)
 {
        struct bsd_driver_data *drv = priv;
-       struct ieee80211req_mlme mlme;
 
-       wpa_printf(MSG_DEBUG, "%s: addr=%s reason_code=%d",
-                  __func__, ether_sprintf(addr), reason_code);
-
-       mlme.im_op = IEEE80211_MLME_DISASSOC;
-       mlme.im_reason = reason_code;
-       memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
-       return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
+       return bsd_send_mlme_param(drv->ioctl_sock, drv->iface,
+                                  IEEE80211_MLME_DISASSOC, reason_code, addr);
 }
 
-static int
+static void
 bsd_new_sta(struct bsd_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
 {
        struct hostapd_data *hapd = drv->hapd;
@@ -594,14 +638,13 @@ bsd_new_sta(struct bsd_driver_data *drv, u8 addr[IEEE80211_ADDR_LEN])
                ielen += 2;
 
 no_ie:
-       return hostapd_notif_assoc(hapd, addr, iebuf, ielen);
+       drv_event_assoc(hapd, addr, iebuf, ielen);
 }
 
 static void
 bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
 {
        struct bsd_driver_data *drv = ctx;
-       struct hostapd_data *hapd = drv->hapd;
        char buf[2048];
        struct if_announcemsghdr *ifan;
        struct rt_msghdr *rtm;
@@ -609,6 +652,7 @@ bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
        struct ieee80211_join_event *join;
        struct ieee80211_leave_event *leave;
        int n;
+       union wpa_event_data data;
 
        n = read(sock, buf, sizeof(buf));
        if (n < 0) {
@@ -634,7 +678,7 @@ bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
                        break;
                case RTM_IEEE80211_LEAVE:
                        leave = (struct ieee80211_leave_event *) &ifan[1];
-                       hostapd_notif_disassoc(drv->hapd, leave->iev_addr);
+                       drv_event_disassoc(drv->hapd, leave->iev_addr);
                        break;
                case RTM_IEEE80211_JOIN:
 #ifdef RTM_IEEE80211_REJOIN
@@ -652,7 +696,11 @@ bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx)
                                "Michael MIC failure wireless event: "
                                "keyix=%u src_addr=" MACSTR, mic->iev_keyix,
                                MAC2STR(mic->iev_src));
-                       hostapd_michael_mic_failure(hapd, mic->iev_src);
+                       os_memset(&data, 0, sizeof(data));
+                       data.michael_mic_failure.unicast = 1;
+                       data.michael_mic_failure.src = mic->iev_src;
+                       wpa_supplicant_event(drv->hapd,
+                                            EVENT_MICHAEL_MIC_FAILURE, &data);
                        break;
                }
                break;
@@ -692,39 +740,35 @@ bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len,
               int encrypt, const u8 *own_addr)
 {
        struct bsd_driver_data *drv = priv;
-       unsigned char buf[3000];
-       unsigned char *bp = buf;
+       unsigned char *bp;
        struct l2_ethhdr *eth;
        size_t len;
        int status;
 
        /*
-        * Prepend the Etherent header.  If the caller left us
+        * Prepend the Ethernet header.  If the caller left us
         * space at the front we could just insert it but since
         * we don't know we copy to a local buffer.  Given the frequency
         * and size of frames this probably doesn't matter.
         */
        len = data_len + sizeof(struct l2_ethhdr);
-       if (len > sizeof(buf)) {
-               bp = malloc(len);
-               if (bp == NULL) {
-                       printf("EAPOL frame discarded, cannot malloc temp "
-                              "buffer of size %u!\n", (unsigned int) len);
-                       return -1;
-               }
+       bp = os_zalloc(len);
+       if (bp == NULL) {
+               wpa_printf(MSG_ERROR, "malloc() failed for bsd_send_eapol"
+                          "(len=%lu)", (unsigned long) len);
+               return -1;
        }
        eth = (struct l2_ethhdr *) bp;
-       memcpy(eth->h_dest, addr, ETH_ALEN);
-       memcpy(eth->h_source, own_addr, ETH_ALEN);
+       os_memcpy(eth->h_dest, addr, ETH_ALEN);
+       os_memcpy(eth->h_source, own_addr, ETH_ALEN);
        eth->h_proto = htons(ETH_P_EAPOL);
-       memcpy(eth+1, data, data_len);
+       os_memcpy(eth + 1, data, data_len);
 
        wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", bp, len);
 
        status = l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, bp, len);
 
-       if (bp != buf)
-               free(bp);
+       os_free(bp);
        return status;
 }
 
@@ -732,9 +776,8 @@ static void
 handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
 {
        struct bsd_driver_data *drv = ctx;
-       hostapd_eapol_receive(drv->hapd, src_addr,
-                             buf + sizeof(struct l2_ethhdr),
-                             len - sizeof(struct l2_ethhdr));
+       drv_event_eapol_rx(drv->hapd, src_addr, buf + sizeof(struct l2_ethhdr),
+                          len - sizeof(struct l2_ethhdr));
 }
 
 static int
@@ -759,6 +802,32 @@ hostapd_bsd_set_ssid(const char *ifname, void *priv, const u8 *buf, int len)
        return bsd_set_ssid(drv->ioctl_sock, drv->iface, buf, len);
 }
 
+static int
+bsd_set_freq(void *priv, struct hostapd_freq_params *freq)
+{
+       struct bsd_driver_data *drv = priv;
+       struct ieee80211chanreq creq;
+       uint32_t mode;
+
+       if (freq->channel < 14)
+               mode = IFM_IEEE80211_11G;
+       else if (freq->channel == 14)
+               mode = IFM_IEEE80211_11B;
+       else
+               mode = IFM_IEEE80211_11A;
+       if (bsd_set_mediaopt(drv->ioctl_sock, drv->iface, IFM_MMASK,
+                            mode) < 0) {
+               wpa_printf(MSG_ERROR, "%s: failed to set modulation mode",
+                          __func__);
+               return -1;
+       }
+
+       os_memset(&creq, 0, sizeof(creq));
+       os_strlcpy(creq.i_name, drv->iface, sizeof(creq.i_name));
+       creq.i_channel = freq->channel;
+       return ioctl(drv->ioctl_sock, SIOCS80211CHANNEL, &creq);
+}
+
 static void *
 bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params)
 {
@@ -789,6 +858,13 @@ bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params)
        if (bsd_wireless_event_init(drv))
                goto bad;
 
+       if (bsd_set_mediaopt(drv->ioctl_sock, drv->iface, IFM_OMASK,
+                            IFM_IEEE80211_HOSTAP) < 0) {
+               wpa_printf(MSG_ERROR, "%s: failed to set operation mode",
+                          __func__);
+               goto bad;
+       }
+
        return drv;
 bad:
        if (drv->sock_xmit != NULL)
@@ -796,7 +872,7 @@ bad:
        if (drv->ioctl_sock >= 0)
                close(drv->ioctl_sock);
        if (drv != NULL)
-               free(drv);
+               os_free(drv);
        return NULL;
 }
 
@@ -812,7 +888,7 @@ bsd_deinit(void *priv)
                close(drv->ioctl_sock);
        if (drv->sock_xmit != NULL)
                l2_packet_deinit(drv->sock_xmit);
-       free(drv);
+       os_free(drv);
 }
 
 const struct wpa_driver_ops wpa_driver_bsd_ops = {
@@ -832,6 +908,7 @@ const struct wpa_driver_ops wpa_driver_bsd_ops = {
        .sta_deauth             = bsd_sta_deauth,
        .hapd_set_ssid          = hostapd_bsd_set_ssid,
        .hapd_get_ssid          = hostapd_bsd_get_ssid,
+       .set_freq               = bsd_set_freq,
 };
 
 #else /* HOSTAPD */
@@ -962,7 +1039,11 @@ static int
 wpa_driver_bsd_set_wpa_ie(struct wpa_driver_bsd_data *drv,
        const u8 *wpa_ie, size_t wpa_ie_len)
 {
+#ifdef IEEE80211_IOC_APPIE
+       return set80211var(drv, IEEE80211_IOC_APPIE, wpa_ie, wpa_ie_len);
+#else /* IEEE80211_IOC_APPIE */
        return set80211var(drv, IEEE80211_IOC_OPTIE, wpa_ie, wpa_ie_len);
+#endif /* IEEE80211_IOC_APPIE */
 }
 
 static int
@@ -993,30 +1074,7 @@ wpa_driver_bsd_set_wpa(void *priv, int enabled)
 }
 
 static int
-wpa_driver_bsd_del_key(struct wpa_driver_bsd_data *drv, int key_idx,
-                      const unsigned char *addr)
-{
-       struct ieee80211req_del_key wk;
-
-       os_memset(&wk, 0, sizeof(wk));
-       if (addr != NULL &&
-           bcmp(addr, "\xff\xff\xff\xff\xff\xff", IEEE80211_ADDR_LEN) != 0) {
-               struct ether_addr ea;
-
-               os_memcpy(&ea, addr, IEEE80211_ADDR_LEN);
-               wpa_printf(MSG_DEBUG, "%s: addr=%s keyidx=%d",
-                       __func__, ether_ntoa(&ea), key_idx);
-               os_memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
-               wk.idk_keyix = (uint8_t) IEEE80211_KEYIX_NONE;
-       } else {
-               wpa_printf(MSG_DEBUG, "%s: keyidx=%d", __func__, key_idx);
-               wk.idk_keyix = key_idx;
-       }
-       return set80211var(drv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk));
-}
-
-static int
-wpa_driver_bsd_set_key(const char *ifname, void *priv, wpa_alg alg,
+wpa_driver_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)
@@ -1028,7 +1086,10 @@ wpa_driver_bsd_set_key(const char *ifname, void *priv, wpa_alg alg,
        u_int8_t cipher;
 
        if (alg == WPA_ALG_NONE)
-               return wpa_driver_bsd_del_key(drv, key_idx, addr);
+               return bsd_del_key(drv->sock, drv->ifname,
+                                  os_memcmp(addr, "\xff\xff\xff\xff\xff\xff",
+                                  IEEE80211_ADDR_LEN) == 0 ? NULL : addr,
+                                  key_idx);
 
        switch (alg) {
        case WPA_ALG_WEP:
@@ -1115,28 +1176,18 @@ static int
 wpa_driver_bsd_deauthenticate(void *priv, const u8 *addr, int reason_code)
 {
        struct wpa_driver_bsd_data *drv = priv;
-       struct ieee80211req_mlme mlme;
 
-       wpa_printf(MSG_DEBUG, "%s", __func__);
-       os_memset(&mlme, 0, sizeof(mlme));
-       mlme.im_op = IEEE80211_MLME_DEAUTH;
-       mlme.im_reason = reason_code;
-       os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
-       return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
+       return bsd_send_mlme_param(drv->sock, drv->ifname,
+                                  IEEE80211_MLME_DEAUTH, reason_code, addr);
 }
 
 static int
 wpa_driver_bsd_disassociate(void *priv, const u8 *addr, int reason_code)
 {
        struct wpa_driver_bsd_data *drv = priv;
-       struct ieee80211req_mlme mlme;
 
-       wpa_printf(MSG_DEBUG, "%s", __func__);
-       os_memset(&mlme, 0, sizeof(mlme));
-       mlme.im_op = IEEE80211_MLME_DISASSOC;
-       mlme.im_reason = reason_code;
-       os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
-       return set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme));
+       return bsd_send_mlme_param(drv->sock, drv->ifname,
+                                  IEEE80211_MLME_DISASSOC, reason_code, addr);
 }
 
 static int
@@ -1145,10 +1196,10 @@ wpa_driver_bsd_set_auth_alg(void *priv, int auth_alg)
        struct wpa_driver_bsd_data *drv = priv;
        int authmode;
 
-       if ((auth_alg & AUTH_ALG_OPEN_SYSTEM) &&
-           (auth_alg & AUTH_ALG_SHARED_KEY))
+       if ((auth_alg & WPA_AUTH_ALG_OPEN) &&
+           (auth_alg & WPA_AUTH_ALG_SHARED))
                authmode = IEEE80211_AUTH_AUTO;
-       else if (auth_alg & AUTH_ALG_SHARED_KEY)
+       else if (auth_alg & WPA_AUTH_ALG_SHARED)
                authmode = IEEE80211_AUTH_SHARED;
        else
                authmode = IEEE80211_AUTH_OPEN;
@@ -1462,6 +1513,13 @@ wpa_driver_bsd_init(void *ctx, const char *ifname)
                        __func__, strerror(errno));
                goto fail;
        }
+
+       if (bsd_set_mediaopt(drv->sock, drv->ifname, IFM_OMASK,
+                            0 /* STA */) < 0) {
+               wpa_printf(MSG_ERROR, "%s: failed to set operation mode",
+                          __func__);
+               goto fail;
+       }
        if (set80211param(drv, IEEE80211_IOC_ROAMING, IEEE80211_ROAMING_MANUAL) < 0) {
                wpa_printf(MSG_DEBUG, "%s: failed to set wpa_supplicant-based "
                           "roaming: %s", __func__, strerror(errno));