nl80211: Mark state disassociated on local deauth/disassoc
[wpasupplicant] / src / drivers / driver_nl80211.c
index f6fdc54..95fa6d1 100644 (file)
@@ -130,6 +130,11 @@ static int wpa_driver_nl80211_set_mode(void *priv, int mode);
 static int
 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
 
+#if defined(CONFIG_AP) || defined(HOSTAPD)
+static void nl80211_remove_monitor_interface(
+       struct wpa_driver_nl80211_data *drv);
+#endif /* CONFIG_AP || HOSTAPD */
+
 #ifdef CONFIG_AP
 static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
                                 int ifidx);
@@ -143,7 +148,6 @@ static struct i802_bss * get_bss(struct wpa_driver_nl80211_data *drv,
 static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
                                 int ifidx);
 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
-static int i802_del_beacon(struct wpa_driver_nl80211_data *drv);
 #endif /* HOSTAPD */
 
 
@@ -320,6 +324,8 @@ static int set_ifhwaddr(struct wpa_driver_nl80211_data *drv,
 #endif /* HOSTAPD */
 
 
+#ifndef HOSTAPD
+
 static int wpa_driver_nl80211_send_oper_ifla(
        struct wpa_driver_nl80211_data *drv,
        int linkmode, int operstate)
@@ -426,7 +432,6 @@ static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
 }
 
 
-#ifndef HOSTAPD
 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
                                          void *ctx, char *buf, size_t len,
                                          int del)
@@ -1262,6 +1267,26 @@ static void wpa_driver_nl80211_free_bss(struct wpa_driver_nl80211_data *drv)
 #endif /* HOSTAPD */
 
 
+#if defined(CONFIG_AP) || defined(HOSTAPD)
+static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
+{
+       struct nl_msg *msg;
+
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -ENOMEM;
+
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+                   0, NL80211_CMD_DEL_BEACON, 0);
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
+
+       return send_and_recv_msgs(drv, msg, NULL, NULL);
+ nla_put_failure:
+       return -ENOBUFS;
+}
+#endif /* CONFIG_AP || HOSTAPD */
+
+
 /**
  * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
  * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
@@ -1274,12 +1299,14 @@ static void wpa_driver_nl80211_deinit(void *priv)
        struct wpa_driver_nl80211_data *drv = priv;
 
 #if defined(CONFIG_AP) || defined(HOSTAPD)
-       if (drv->monitor_ifidx >= 0)
-               nl80211_remove_iface(drv, drv->monitor_ifidx);
+       nl80211_remove_monitor_interface(drv);
        if (drv->monitor_sock >= 0) {
                eloop_unregister_read_sock(drv->monitor_sock);
                close(drv->monitor_sock);
        }
+
+       if (drv->nlmode == NL80211_IFTYPE_AP)
+               wpa_driver_nl80211_del_beacon(drv);
 #endif /* CONFIG_AP || HOSTAPD */
 
 #ifdef HOSTAPD
@@ -1291,8 +1318,6 @@ static void wpa_driver_nl80211_deinit(void *priv)
                i802_set_freq(priv, &freq);
        }
 
-       i802_del_beacon(drv);
-
        if (drv->eapol_sock >= 0) {
                eloop_unregister_read_sock(drv->eapol_sock);
                close(drv->eapol_sock);
@@ -1678,6 +1703,8 @@ nla_put_failure:
 }
 
 
+#ifndef HOSTAPD
+
 static int wpa_driver_nl80211_set_key(void *priv, wpa_alg alg,
                                      const u8 *addr, int key_idx,
                                      int set_tx, const u8 *seq,
@@ -1726,6 +1753,7 @@ static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
 {
        struct wpa_driver_nl80211_data *drv = priv;
        wpa_printf(MSG_DEBUG, "%s", __func__);
+       drv->associated = 0;
        return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
                                       reason_code);
 }
@@ -1736,6 +1764,7 @@ static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
 {
        struct wpa_driver_nl80211_data *drv = priv;
        wpa_printf(MSG_DEBUG, "%s", __func__);
+       drv->associated = 0;
        return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
                                       reason_code);
 }
@@ -1822,6 +1851,8 @@ nla_put_failure:
        return ret;
 }
 
+#endif /* HOSTAPD */
+
 
 #if defined(CONFIG_AP) || defined(HOSTAPD)
 
@@ -2291,6 +2322,73 @@ nla_put_failure:
        return -1;
 }
 
+
+static int wpa_driver_nl80211_sta_add(const char *ifname, void *priv,
+                                     struct hostapd_sta_add_params *params)
+{
+       struct wpa_driver_nl80211_data *drv = priv;
+       struct nl_msg *msg;
+       int ret = -ENOBUFS;
+
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -ENOMEM;
+
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+                   0, NL80211_CMD_NEW_STATION, 0);
+
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(ifname));
+       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
+       NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
+       NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
+               params->supp_rates);
+       NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
+                   params->listen_interval);
+
+#ifdef CONFIG_IEEE80211N
+       if (params->ht_capabilities) {
+               NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
+                       params->ht_capabilities->length,
+                       &params->ht_capabilities->data);
+       }
+#endif /* CONFIG_IEEE80211N */
+
+       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
+       if (ret)
+               wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
+                          "result: %d (%s)", ret, strerror(-ret));
+       if (ret == -EEXIST)
+               ret = 0;
+ nla_put_failure:
+       return ret;
+}
+
+
+static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
+{
+       struct wpa_driver_nl80211_data *drv = priv;
+       struct nl_msg *msg;
+       int ret;
+
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -ENOMEM;
+
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+                   0, NL80211_CMD_DEL_STATION, 0);
+
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
+                   if_nametoindex(drv->ifname));
+       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
+
+       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
+       if (ret == -ENOENT)
+               return 0;
+       return ret;
+ nla_put_failure:
+       return -ENOBUFS;
+}
+
 #endif /* CONFIG_AP || HOSTAPD */
 
 
@@ -2717,6 +2815,16 @@ static int add_monitor_filter(int s)
 }
 
 
+static void nl80211_remove_monitor_interface(
+       struct wpa_driver_nl80211_data *drv)
+{
+       if (drv->monitor_ifidx >= 0) {
+               nl80211_remove_iface(drv, drv->monitor_ifidx);
+               drv->monitor_ifidx = -1;
+       }
+}
+
+
 static int
 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
 {
@@ -2773,7 +2881,7 @@ nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
 
        return 0;
  error:
-       nl80211_remove_iface(drv, drv->monitor_ifidx);
+       nl80211_remove_monitor_interface(drv);
        return -1;
 }
 
@@ -2784,14 +2892,9 @@ nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
                                 struct wpa_driver_associate_params *params)
 {
-       if (drv->monitor_ifidx < 0 &&
-           nl80211_create_monitor_interface(drv))
-               return -1;
-
        if (wpa_driver_nl80211_set_mode(drv, params->mode) ||
            wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) {
-               nl80211_remove_iface(drv, drv->monitor_ifidx);
-               drv->monitor_ifidx = -1;
+               nl80211_remove_monitor_interface(drv);
                return -1;
        }
 
@@ -2803,6 +2906,7 @@ static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
 #endif /* CONFIG_AP */
 
 
+#ifndef HOSTAPD
 static int wpa_driver_nl80211_associate(
        void *priv, struct wpa_driver_associate_params *params)
 {
@@ -2871,6 +2975,7 @@ nla_put_failure:
        nlmsg_free(msg);
        return ret;
 }
+#endif /* HOSTAPD */
 
 
 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
@@ -2920,11 +3025,14 @@ static int wpa_driver_nl80211_set_mode(void *priv, int mode)
 
        if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) {
                drv->nlmode = nlmode;
-               return 0;
+               ret = 0;
+               goto done;
        }
 
-       if (nlmode == drv->nlmode)
-               return 0; /* Already in the requested mode */
+       if (nlmode == drv->nlmode) {
+               ret = 0;
+               goto done; /* Already in the requested mode */
+       }
 
        /* mac80211 doesn't allow mode changes while the device is up, so
         * take the device down, try to set the mode again, and bring the
@@ -2940,10 +3048,25 @@ static int wpa_driver_nl80211_set_mode(void *priv, int mode)
        if (!ret)
                drv->nlmode = nlmode;
 
+done:
+#if defined(CONFIG_AP) || defined(HOSTAPD)
+       if (!ret && nlmode == NL80211_IFTYPE_AP) {
+               /* Setup additional AP mode functionality if needed */
+               if (drv->monitor_ifidx < 0 &&
+                   nl80211_create_monitor_interface(drv))
+                       return -1;
+       } else if (!ret && nlmode != NL80211_IFTYPE_AP) {
+               /* Remove additional AP mode functionality */
+               nl80211_remove_monitor_interface(drv);
+       }
+#endif /* CONFIG_AP || HOSTAPD */
+
        return ret;
 }
 
 
+#ifndef HOSTAPD
+
 static int wpa_driver_nl80211_get_capa(void *priv,
                                       struct wpa_driver_capa *capa)
 {
@@ -2966,6 +3089,8 @@ static int wpa_driver_nl80211_set_operstate(void *priv, int state)
                drv, -1, state ? IF_OPER_UP : IF_OPER_DORMANT);
 }
 
+#endif /* HOSTAPD */
+
 
 #ifdef HOSTAPD
 
@@ -3392,74 +3517,6 @@ static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
 }
 
 
-static int i802_sta_add(const char *ifname, void *priv,
-                       struct hostapd_sta_add_params *params)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       struct nl_msg *msg;
-       int ret = -ENOBUFS;
-
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -ENOMEM;
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_NEW_STATION, 0);
-
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
-                   if_nametoindex(drv->ifname));
-       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
-       NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
-       NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
-               params->supp_rates);
-       NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
-                   params->listen_interval);
-
-#ifdef CONFIG_IEEE80211N
-       if (params->ht_capabilities) {
-               NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
-                       params->ht_capabilities->length,
-                       &params->ht_capabilities->data);
-       }
-#endif /* CONFIG_IEEE80211N */
-
-       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
-       if (ret)
-               wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
-                          "result: %d (%s)", ret, strerror(-ret));
-       if (ret == -EEXIST)
-               ret = 0;
- nla_put_failure:
-       return ret;
-}
-
-
-static int i802_sta_remove(void *priv, const u8 *addr)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       struct nl_msg *msg;
-       int ret;
-
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -ENOMEM;
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_DEL_STATION, 0);
-
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
-                   if_nametoindex(drv->ifname));
-       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
-
-       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
-       if (ret == -ENOENT)
-               return 0;
-       return ret;
- nla_put_failure:
-       return -ENOBUFS;
-}
-
-
 static int i802_sta_set_flags(void *priv, const u8 *addr,
                              int total_flags, int flags_or, int flags_and)
 {
@@ -3560,13 +3617,13 @@ static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
        bss = os_zalloc(sizeof(*bss));
        if (bss == NULL)
                return -1;
-       bss->ifindex = if_nametoindex(ifname);
 
        ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
        if (ifidx < 0) {
                os_free(bss);
                return -1;
        }
+       bss->ifindex = ifidx;
        if (hostapd_set_iface_flags(priv, ifname, 1)) {
                nl80211_remove_iface(priv, ifidx);
                os_free(bss);
@@ -3610,24 +3667,6 @@ static int i802_set_beacon(const char *iface, void *priv,
 }
 
 
-static int i802_del_beacon(struct wpa_driver_nl80211_data *drv)
-{
-       struct nl_msg *msg;
-
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -ENOMEM;
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_DEL_BEACON, 0);
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
-
-       return send_and_recv_msgs(drv, msg, NULL, NULL);
- nla_put_failure:
-       return -ENOBUFS;
-}
-
-
 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
 {
        struct wpa_driver_nl80211_data *drv = priv;
@@ -3842,15 +3881,13 @@ static void *i802_init(struct hostapd_data *hapd,
        /* start listening for EAPOL on the default AP interface */
        add_ifidx(drv, drv->ifindex);
 
-       if (hostapd_set_iface_flags(drv, drv->ifname, 0))
-               goto failed;
-
-       if (params->bssid && set_ifhwaddr(drv, drv->ifname, params->bssid))
-               goto failed;
+       if (params->bssid) {
+               if (hostapd_set_iface_flags(drv, drv->ifname, 0))
+                       goto failed;
 
-       /* Initialise a monitor interface */
-       if (nl80211_create_monitor_interface(drv))
-               goto failed;
+               if (set_ifhwaddr(drv, drv->ifname, params->bssid))
+                       goto failed;
+       }
 
        if (nl80211_set_mode(drv, drv->ifindex, NL80211_IFTYPE_AP)) {
                wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
@@ -3879,8 +3916,7 @@ static void *i802_init(struct hostapd_data *hapd,
        return drv;
 
 failed:
-       if (drv->monitor_ifidx >= 0)
-               nl80211_remove_iface(drv, drv->monitor_ifidx);
+       nl80211_remove_monitor_interface(drv);
        if (drv->ioctl_sock >= 0)
                close(drv->ioctl_sock);
 
@@ -3905,11 +3941,14 @@ static void i802_deinit(void *priv)
 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .name = "nl80211",
        .desc = "Linux nl80211/cfg80211",
+#ifndef HOSTAPD
        .get_bssid = wpa_driver_nl80211_get_bssid,
        .get_ssid = wpa_driver_nl80211_get_ssid,
        .set_key = wpa_driver_nl80211_set_key,
+#endif /* HOSTAPD */
        .scan2 = wpa_driver_nl80211_scan,
        .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
+#ifndef HOSTAPD
        .deauthenticate = wpa_driver_nl80211_deauthenticate,
        .disassociate = wpa_driver_nl80211_disassociate,
        .authenticate = wpa_driver_nl80211_authenticate,
@@ -3918,6 +3957,7 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .deinit = wpa_driver_nl80211_deinit,
        .get_capa = wpa_driver_nl80211_get_capa,
        .set_operstate = wpa_driver_nl80211_set_operstate,
+#endif /* HOSTAPD */
        .set_country = wpa_driver_nl80211_set_country,
        .set_mode = wpa_driver_nl80211_set_mode,
 #ifdef CONFIG_AP
@@ -3927,6 +3967,8 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .send_mlme = wpa_driver_nl80211_send_mlme,
        .set_beacon_int = wpa_driver_nl80211_set_beacon_int,
        .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
+       .sta_add = wpa_driver_nl80211_sta_add,
+       .sta_remove = wpa_driver_nl80211_sta_remove,
 #endif /* CONFIG_AP || HOSTAPD */
 #ifdef HOSTAPD
        .hapd_init = i802_init,
@@ -3939,8 +3981,6 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .sta_set_flags = i802_sta_set_flags,
        .sta_deauth = i802_sta_deauth,
        .sta_disassoc = i802_sta_disassoc,
-       .sta_remove = i802_sta_remove,
-       .sta_add = i802_sta_add,
        .get_inact_sec = i802_get_inact_sec,
        .sta_clear_stats = i802_sta_clear_stats,
        .set_freq = i802_set_freq,