Passive Client Taxonomy
[mech_eap.git] / src / ap / sta_info.c
index 8bba73c..d9aa8e3 100644 (file)
 #include "ap_drv_ops.h"
 #include "gas_serv.h"
 #include "wnm_ap.h"
+#include "mbo_ap.h"
 #include "ndisc_snoop.h"
 #include "sta_info.h"
+#include "vlan.h"
 
 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
                                       struct sta_info *sta);
@@ -169,8 +171,10 @@ void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
        ap_sta_ip6addr_del(hapd, sta);
 
        if (!hapd->iface->driver_ap_teardown &&
-           !(sta->flags & WLAN_STA_PREAUTH))
+           !(sta->flags & WLAN_STA_PREAUTH)) {
                hostapd_drv_sta_remove(hapd, sta->addr);
+               sta->added_unassoc = 0;
+       }
 
        ap_sta_hash_del(hapd, sta);
        ap_sta_list_del(hapd, sta);
@@ -218,6 +222,13 @@ void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
                hapd->iface->num_sta_ht_20mhz--;
        }
 
+#ifdef CONFIG_TAXONOMY
+       wpabuf_free(sta->probe_ie_taxonomy);
+       sta->probe_ie_taxonomy = NULL;
+       wpabuf_free(sta->assoc_ie_taxonomy);
+       sta->assoc_ie_taxonomy = NULL;
+#endif /* CONFIG_TAXONOMY */
+
 #ifdef CONFIG_IEEE80211N
        ht40_intolerant_remove(hapd->iface, sta);
 #endif /* CONFIG_IEEE80211N */
@@ -238,7 +249,7 @@ void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
 
 #ifdef CONFIG_MESH
        if (hapd->mesh_sta_free_cb)
-               hapd->mesh_sta_free_cb(sta);
+               hapd->mesh_sta_free_cb(hapd, sta);
 #endif /* CONFIG_MESH */
 
        if (set_beacon)
@@ -266,14 +277,18 @@ void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
         * vlan_remove_dynamic() can check that no stations are left on the
         * AP_VLAN netdev.
         */
+       if (sta->vlan_id)
+               vlan_remove_dynamic(hapd, sta->vlan_id);
        if (sta->vlan_id_bound) {
                /*
                 * Need to remove the STA entry before potentially removing the
                 * VLAN.
                 */
                if (hapd->iface->driver_ap_teardown &&
-                   !(sta->flags & WLAN_STA_PREAUTH))
+                   !(sta->flags & WLAN_STA_PREAUTH)) {
                        hostapd_drv_sta_remove(hapd, sta->addr);
+                       sta->added_unassoc = 0;
+               }
                vlan_remove_dynamic(hapd, sta->vlan_id_bound);
        }
 #endif /* CONFIG_NO_VLAN */
@@ -319,6 +334,9 @@ void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
        os_free(sta->sae);
 #endif /* CONFIG_SAE */
 
+       mbo_ap_sta_free(sta);
+       os_free(sta->supp_op_classes);
+
        os_free(sta);
 }
 
@@ -625,7 +643,10 @@ struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
                return NULL;
        }
        sta->acct_interim_interval = hapd->conf->acct_interim_interval;
-       accounting_sta_get_id(hapd, sta);
+       if (accounting_sta_get_id(hapd, sta) < 0) {
+               os_free(sta);
+               return NULL;
+       }
 
        if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
                wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
@@ -667,6 +688,7 @@ static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
                           hapd->conf->iface, MAC2STR(sta->addr));
                return -1;
        }
+       sta->added_unassoc = 0;
        return 0;
 }
 
@@ -799,6 +821,128 @@ int ap_sta_wps_cancel(struct hostapd_data *hapd,
 #endif /* CONFIG_WPS */
 
 
+static int ap_sta_get_free_vlan_id(struct hostapd_data *hapd)
+{
+       struct hostapd_vlan *vlan;
+       int vlan_id = MAX_VLAN_ID + 2;
+
+retry:
+       for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
+               if (vlan->vlan_id == vlan_id) {
+                       vlan_id++;
+                       goto retry;
+               }
+       }
+       return vlan_id;
+}
+
+
+int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
+                   struct vlan_description *vlan_desc)
+{
+       struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
+       int old_vlan_id, vlan_id = 0, ret = 0;
+
+       if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
+               vlan_desc = NULL;
+
+       /* Check if there is something to do */
+       if (hapd->conf->ssid.per_sta_vif && !sta->vlan_id) {
+               /* This sta is lacking its own vif */
+       } else if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED &&
+                  !hapd->conf->ssid.per_sta_vif && sta->vlan_id) {
+               /* sta->vlan_id needs to be reset */
+       } else if (!vlan_compare(vlan_desc, sta->vlan_desc)) {
+               return 0; /* nothing to change */
+       }
+
+       /* Now the real VLAN changed or the STA just needs its own vif */
+       if (hapd->conf->ssid.per_sta_vif) {
+               /* Assign a new vif, always */
+               /* find a free vlan_id sufficiently big */
+               vlan_id = ap_sta_get_free_vlan_id(hapd);
+               /* Get wildcard VLAN */
+               for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
+                       if (vlan->vlan_id == VLAN_ID_WILDCARD)
+                               break;
+               }
+               if (!vlan) {
+                       hostapd_logger(hapd, sta->addr,
+                                      HOSTAPD_MODULE_IEEE80211,
+                                      HOSTAPD_LEVEL_DEBUG,
+                                      "per_sta_vif missing wildcard");
+                       vlan_id = 0;
+                       ret = -1;
+                       goto done;
+               }
+       } else if (vlan_desc && vlan_desc->notempty) {
+               for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
+                       if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
+                               break;
+                       if (vlan->vlan_id == VLAN_ID_WILDCARD)
+                               wildcard_vlan = vlan;
+               }
+               if (vlan) {
+                       vlan_id = vlan->vlan_id;
+               } else if (wildcard_vlan) {
+                       vlan = wildcard_vlan;
+                       vlan_id = vlan_desc->untagged;
+                       if (vlan_desc->tagged[0]) {
+                               /* Tagged VLAN configuration */
+                               vlan_id = ap_sta_get_free_vlan_id(hapd);
+                       }
+               } else {
+                       hostapd_logger(hapd, sta->addr,
+                                      HOSTAPD_MODULE_IEEE80211,
+                                      HOSTAPD_LEVEL_DEBUG,
+                                      "missing vlan and wildcard for vlan=%d%s",
+                                      vlan_desc->untagged,
+                                      vlan_desc->tagged[0] ? "+" : "");
+                       vlan_id = 0;
+                       ret = -1;
+                       goto done;
+               }
+       }
+
+       if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
+               vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
+               if (vlan == NULL) {
+                       hostapd_logger(hapd, sta->addr,
+                                      HOSTAPD_MODULE_IEEE80211,
+                                      HOSTAPD_LEVEL_DEBUG,
+                                      "could not add dynamic VLAN interface for vlan=%d%s",
+                                      vlan_desc ? vlan_desc->untagged : -1,
+                                      (vlan_desc && vlan_desc->tagged[0]) ?
+                                      "+" : "");
+                       vlan_id = 0;
+                       ret = -1;
+                       goto done;
+               }
+
+               hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
+                              HOSTAPD_LEVEL_DEBUG,
+                              "added new dynamic VLAN interface '%s'",
+                              vlan->ifname);
+       } else if (vlan && vlan->dynamic_vlan > 0) {
+               vlan->dynamic_vlan++;
+               hostapd_logger(hapd, sta->addr,
+                              HOSTAPD_MODULE_IEEE80211,
+                              HOSTAPD_LEVEL_DEBUG,
+                              "updated existing dynamic VLAN interface '%s'",
+                              vlan->ifname);
+       }
+done:
+       old_vlan_id = sta->vlan_id;
+       sta->vlan_id = vlan_id;
+       sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
+
+       if (vlan_id != old_vlan_id && old_vlan_id)
+               vlan_remove_dynamic(hapd, old_vlan_id);
+
+       return ret;
+}
+
+
 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
 {
 #ifndef CONFIG_NO_VLAN
@@ -811,20 +955,11 @@ int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
        if (hapd->conf->ssid.vlan[0])
                iface = hapd->conf->ssid.vlan;
 
-       if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
-               sta->vlan_id = 0;
-       else if (sta->vlan_id > 0) {
-               struct hostapd_vlan *wildcard_vlan = NULL;
-               vlan = hapd->conf->vlan;
-               while (vlan) {
+       if (sta->vlan_id > 0) {
+               for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
                        if (vlan->vlan_id == sta->vlan_id)
                                break;
-                       if (vlan->vlan_id == VLAN_ID_WILDCARD)
-                               wildcard_vlan = vlan;
-                       vlan = vlan->next;
                }
-               if (!vlan)
-                       vlan = wildcard_vlan;
                if (vlan)
                        iface = vlan->ifname;
        }
@@ -844,24 +979,7 @@ int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
                               sta->vlan_id);
                ret = -1;
                goto done;
-       } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
-               vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
-               if (vlan == NULL) {
-                       hostapd_logger(hapd, sta->addr,
-                                      HOSTAPD_MODULE_IEEE80211,
-                                      HOSTAPD_LEVEL_DEBUG, "could not add "
-                                      "dynamic VLAN interface for vlan_id=%d",
-                                      sta->vlan_id);
-                       ret = -1;
-                       goto done;
-               }
-
-               iface = vlan->ifname;
-               hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
-                              HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
-                              "interface '%s'", iface);
-       } else if (vlan && vlan->vlan_id == sta->vlan_id &&
-                  vlan->dynamic_vlan > 0) {
+       } else if (vlan && vlan->dynamic_vlan > 0) {
                vlan->dynamic_vlan++;
                hostapd_logger(hapd, sta->addr,
                               HOSTAPD_MODULE_IEEE80211,