hostapd: initialize ht/vht/he mode on channel switch by default
[openwrt/openwrt.git] / package / network / services / hostapd / src / src / ap / ubus.c
index aae9f91a20d4456b8a149356f8590a203667452d..9918f17d6709c794d7a96d0c77d412661c39865b 100644 (file)
@@ -21,6 +21,7 @@
 #include "rrm.h"
 #include "wnm_ap.h"
 #include "taxonomy.h"
+#include "airtime_policy.h"
 
 static struct ubus_context *ctx;
 static struct blob_buf b;
@@ -297,6 +298,7 @@ hostapd_bss_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
                { "wmm", WLAN_STA_WMM },
                { "ht", WLAN_STA_HT },
                { "vht", WLAN_STA_VHT },
+               { "he", WLAN_STA_HE },
                { "wps", WLAN_STA_WPS },
                { "mfp", WLAN_STA_MFP },
        };
@@ -372,6 +374,32 @@ hostapd_bss_get_features(struct ubus_context *ctx, struct ubus_object *obj,
        return 0;
 }
 
+/* Imported from iw/util.c
+ *  https://git.kernel.org/pub/scm/linux/kernel/git/jberg/iw.git/tree/util.c?id=4b25ae3537af48dbf9d0abf94132e5ba01b32c18#n200
+ */
+int ieee80211_frequency_to_channel(int freq)
+{
+       /* see 802.11-2007 17.3.8.3.2 and Annex J */
+       if (freq == 2484)
+               return 14;
+       /* see 802.11ax D6.1 27.3.23.2 and Annex E */
+       else if (freq == 5935)
+               return 2;
+       else if (freq < 2484)
+               return (freq - 2407) / 5;
+       else if (freq >= 4910 && freq <= 4980)
+               return (freq - 4000) / 5;
+       else if (freq < 5950)
+               return (freq - 5000) / 5;
+       else if (freq <= 45000) /* DMG band lower limit */
+               /* see 802.11ax D6.1 27.3.23.2 */
+               return (freq - 5950) / 5;
+       else if (freq >= 58320 && freq <= 70200)
+               return (freq - 56160) / 2160;
+       else
+               return 0;
+}
+
 static int
 hostapd_bss_get_status(struct ubus_context *ctx, struct ubus_object *obj,
                       struct ubus_request_data *req, const char *method,
@@ -380,12 +408,23 @@ hostapd_bss_get_status(struct ubus_context *ctx, struct ubus_object *obj,
        struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
        void *airtime_table, *dfs_table;
        struct os_reltime now;
+       char ssid[SSID_MAX_LEN + 1];
        char phy_name[17];
-       char mac_buf[20];
+       size_t ssid_len = SSID_MAX_LEN;
+
+       if (hapd->conf->ssid.ssid_len < SSID_MAX_LEN)
+               ssid_len = hapd->conf->ssid.ssid_len;
 
        blob_buf_init(&b, 0);
        blobmsg_add_string(&b, "status", hostapd_state_text(hapd->iface->state));
+       blobmsg_printf(&b, "bssid", MACSTR, MAC2STR(hapd->conf->bssid));
+
+       memset(ssid, 0, SSID_MAX_LEN + 1);
+       memcpy(ssid, hapd->conf->ssid.ssid, ssid_len);
+       blobmsg_add_string(&b, "ssid", ssid);
+
        blobmsg_add_u32(&b, "freq", hapd->iface->freq);
+       blobmsg_add_u32(&b, "channel", ieee80211_frequency_to_channel(hapd->iface->freq));
 
        snprintf(phy_name, 17, "%s", hapd->iface->phy);
        blobmsg_add_string(&b, "phy", phy_name);
@@ -658,6 +697,10 @@ hostapd_config_add(struct ubus_context *ctx, struct ubus_object *obj,
        if (hostapd_add_iface(interfaces, buf))
                return UBUS_STATUS_INVALID_ARGUMENT;
 
+       blob_buf_init(&b, 0);
+       blobmsg_add_u32(&b, "pid", getpid());
+       ubus_send_reply(ctx, req, b.head);
+
        return UBUS_STATUS_OK;
 }
 
@@ -699,6 +742,7 @@ enum {
        CSA_SEC_CHANNEL_OFFSET,
        CSA_HT,
        CSA_VHT,
+       CSA_HE,
        CSA_BLOCK_TX,
        __CSA_MAX
 };
@@ -712,6 +756,7 @@ static const struct blobmsg_policy csa_policy[__CSA_MAX] = {
        [CSA_SEC_CHANNEL_OFFSET] = { "sec_channel_offset", BLOBMSG_TYPE_INT32 },
        [CSA_HT] = { "ht", BLOBMSG_TYPE_BOOL },
        [CSA_VHT] = { "vht", BLOBMSG_TYPE_BOOL },
+       [CSA_HE] = { "he", BLOBMSG_TYPE_BOOL },
        [CSA_BLOCK_TX] = { "block_tx", BLOBMSG_TYPE_BOOL },
 };
 
@@ -723,14 +768,38 @@ hostapd_switch_chan(struct ubus_context *ctx, struct ubus_object *obj,
 {
        struct blob_attr *tb[__CSA_MAX];
        struct hostapd_data *hapd = get_hapd_from_object(obj);
-       struct csa_settings css;
+       struct hostapd_config *iconf = hapd->iface->conf;
+       struct csa_settings css = {
+               .freq_params = {
+                       .ht_enabled = iconf->ieee80211n,
+                       .vht_enabled = iconf->ieee80211ac,
+                       .he_enabled = iconf->ieee80211ax,
+                       .sec_channel_offset = iconf->secondary_channel,
+               }
+       };
+       int ret = UBUS_STATUS_OK;
+       int i;
 
        blobmsg_parse(csa_policy, __CSA_MAX, tb, blob_data(msg), blob_len(msg));
 
        if (!tb[CSA_FREQ])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
-       memset(&css, 0, sizeof(css));
+       switch (iconf->vht_oper_chwidth) {
+       case CHANWIDTH_USE_HT:
+               if (iconf->secondary_channel)
+                       css.freq_params.bandwidth = 40;
+               else
+                       css.freq_params.bandwidth = 20;
+               break;
+       case CHANWIDTH_160MHZ:
+               css.freq_params.bandwidth = 160;
+               break;
+       default:
+               css.freq_params.bandwidth = 80;
+               break;
+       }
+
        css.freq_params.freq = blobmsg_get_u32(tb[CSA_FREQ]);
 
 #define SET_CSA_SETTING(name, field, type) \
@@ -746,12 +815,17 @@ hostapd_switch_chan(struct ubus_context *ctx, struct ubus_object *obj,
        SET_CSA_SETTING(CSA_SEC_CHANNEL_OFFSET, freq_params.sec_channel_offset, u32);
        SET_CSA_SETTING(CSA_HT, freq_params.ht_enabled, bool);
        SET_CSA_SETTING(CSA_VHT, freq_params.vht_enabled, bool);
+       SET_CSA_SETTING(CSA_HE, freq_params.he_enabled, bool);
        SET_CSA_SETTING(CSA_BLOCK_TX, block_tx, bool);
 
+       for (i = 0; i < hapd->iface->num_bss; i++) {
+               struct hostapd_data *bss = hapd->iface->bss[i];
 
-       if (hostapd_switch_channel(hapd, &css) != 0)
-               return UBUS_STATUS_NOT_SUPPORTED;
-       return UBUS_STATUS_OK;
+               if (hostapd_switch_channel(bss, &css) != 0)
+                       ret = UBUS_STATUS_NOT_SUPPORTED;
+       }
+
+       return ret;
 #undef SET_CSA_SETTING
 }
 #endif
@@ -1270,7 +1344,7 @@ hostapd_wnm_disassoc_imminent(struct ubus_context *ctx, struct ubus_object *obj,
        if (tb[WNM_DISASSOC_ABRIDGED] && blobmsg_get_bool(tb[WNM_DISASSOC_ABRIDGED]))
                req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
 
-       if (wnm_send_bss_tm_req(hapd, sta, req_mode, duration, 0, NULL,
+       if (wnm_send_bss_tm_req(hapd, sta, req_mode, duration, duration, NULL,
                                NULL, nr, nr_len, NULL, 0))
                return UBUS_STATUS_UNKNOWN_ERROR;
 
@@ -1278,11 +1352,68 @@ hostapd_wnm_disassoc_imminent(struct ubus_context *ctx, struct ubus_object *obj,
 }
 #endif
 
+#ifdef CONFIG_AIRTIME_POLICY
+enum {
+       UPDATE_AIRTIME_STA,
+       UPDATE_AIRTIME_WEIGHT,
+       __UPDATE_AIRTIME_MAX,
+};
+
+
+static const struct blobmsg_policy airtime_policy[__UPDATE_AIRTIME_MAX] = {
+       [UPDATE_AIRTIME_STA] = { "sta", BLOBMSG_TYPE_STRING },
+       [UPDATE_AIRTIME_WEIGHT] = { "weight", BLOBMSG_TYPE_INT32 },
+};
+
+static int
+hostapd_bss_update_airtime(struct ubus_context *ctx, struct ubus_object *obj,
+                          struct ubus_request_data *ureq, const char *method,
+                          struct blob_attr *msg)
+{
+       struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
+       struct blob_attr *tb[__UPDATE_AIRTIME_MAX];
+       struct sta_info *sta = NULL;
+       u8 addr[ETH_ALEN];
+       int weight;
+
+       blobmsg_parse(airtime_policy, __UPDATE_AIRTIME_MAX, tb, blob_data(msg), blob_len(msg));
+
+       if (!tb[UPDATE_AIRTIME_WEIGHT])
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       weight = blobmsg_get_u32(tb[UPDATE_AIRTIME_WEIGHT]);
+
+       if (!tb[UPDATE_AIRTIME_STA]) {
+               if (!weight)
+                       return UBUS_STATUS_INVALID_ARGUMENT;
+
+               hapd->conf->airtime_weight = weight;
+               return 0;
+       }
+
+       if (hwaddr_aton(blobmsg_data(tb[UPDATE_AIRTIME_STA]), addr))
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       sta = ap_get_sta(hapd, addr);
+       if (!sta)
+               return UBUS_STATUS_NOT_FOUND;
+
+       sta->dyn_airtime_weight = weight;
+       airtime_policy_new_sta(hapd, sta);
+
+       return 0;
+}
+#endif
+
+
 static const struct ubus_method bss_methods[] = {
        UBUS_METHOD_NOARG("reload", hostapd_bss_reload),
        UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
        UBUS_METHOD_NOARG("get_status", hostapd_bss_get_status),
        UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
+#ifdef CONFIG_AIRTIME_POLICY
+       UBUS_METHOD("update_airtime", hostapd_bss_update_airtime, airtime_policy),
+#endif
        UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
 #ifdef CONFIG_WPS
        UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start),
@@ -1360,6 +1491,43 @@ void hostapd_ubus_free_bss(struct hostapd_data *hapd)
        free(name);
 }
 
+static void
+hostapd_ubus_vlan_action(struct hostapd_data *hapd, struct hostapd_vlan *vlan,
+                        const char *action)
+{
+       struct vlan_description *desc = &vlan->vlan_desc;
+       void *c;
+       int i;
+
+       if (!hapd->ubus.obj.has_subscribers)
+               return;
+
+       blob_buf_init(&b, 0);
+       blobmsg_add_string(&b, "ifname", vlan->ifname);
+       blobmsg_add_string(&b, "bridge", vlan->bridge);
+       blobmsg_add_u32(&b, "vlan_id", vlan->vlan_id);
+
+       if (desc->notempty) {
+               blobmsg_add_u32(&b, "untagged", desc->untagged);
+               c = blobmsg_open_array(&b, "tagged");
+               for (i = 0; i < ARRAY_SIZE(desc->tagged) && desc->tagged[i]; i++)
+                       blobmsg_add_u32(&b, "", desc->tagged[i]);
+               blobmsg_close_array(&b, c);
+       }
+
+       ubus_notify(ctx, &hapd->ubus.obj, action, b.head, -1);
+}
+
+void hostapd_ubus_add_vlan(struct hostapd_data *hapd, struct hostapd_vlan *vlan)
+{
+       hostapd_ubus_vlan_action(hapd, vlan, "vlan_add");
+}
+
+void hostapd_ubus_remove_vlan(struct hostapd_data *hapd, struct hostapd_vlan *vlan)
+{
+       hostapd_ubus_vlan_action(hapd, vlan, "vlan_remove");
+}
+
 static const struct ubus_method daemon_methods[] = {
        UBUS_METHOD("config_add", hostapd_config_add, config_add_policy),
        UBUS_METHOD("config_remove", hostapd_config_remove, config_remove_policy),
@@ -1546,3 +1714,24 @@ void hostapd_ubus_notify_beacon_report(
 
        ubus_notify(ctx, &hapd->ubus.obj, "beacon-report", b.head, -1);
 }
+
+void hostapd_ubus_notify_radar_detected(struct hostapd_iface *iface, int frequency,
+                                       int chan_width, int cf1, int cf2)
+{
+       struct hostapd_data *hapd;
+       int i;
+
+       if (!hapd->ubus.obj.has_subscribers)
+               return;
+
+       blob_buf_init(&b, 0);
+       blobmsg_add_u16(&b, "frequency", frequency);
+       blobmsg_add_u16(&b, "width", chan_width);
+       blobmsg_add_u16(&b, "center1", cf1);
+       blobmsg_add_u16(&b, "center2", cf2);
+
+       for (i = 0; i < iface->num_bss; i++) {
+               hapd = iface->bss[i];
+               ubus_notify(ctx, &hapd->ubus.obj, "radar-detected", b.head, -1);
+       }
+}