hostapd: make it possible to update station airtime weights via ubus
[openwrt/openwrt.git] / package / network / services / hostapd / src / src / ap / ubus.c
index 7cc2059fc1574b5d899e6a33b4672e8d086bd7ea..85fd6f1cc77e5cc8dc961712f526df2ffb3bd5bd 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);
@@ -1288,11 +1327,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),