xref: /freebsd/contrib/wpa/wpa_supplicant/wmm_ac.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
15b9c547cSRui Paulo /*
25b9c547cSRui Paulo  * Wi-Fi Multimedia Admission Control (WMM-AC)
35b9c547cSRui Paulo  * Copyright(c) 2014, Intel Mobile Communication GmbH.
45b9c547cSRui Paulo  * Copyright(c) 2014, Intel Corporation. All rights reserved.
55b9c547cSRui Paulo  *
65b9c547cSRui Paulo  * This software may be distributed under the terms of the BSD license.
75b9c547cSRui Paulo  * See README for more details.
85b9c547cSRui Paulo  */
95b9c547cSRui Paulo 
105b9c547cSRui Paulo #include "includes.h"
115b9c547cSRui Paulo 
125b9c547cSRui Paulo #include "utils/common.h"
135b9c547cSRui Paulo #include "utils/list.h"
145b9c547cSRui Paulo #include "utils/eloop.h"
155b9c547cSRui Paulo #include "common/ieee802_11_common.h"
165b9c547cSRui Paulo #include "wpa_supplicant_i.h"
175b9c547cSRui Paulo #include "bss.h"
185b9c547cSRui Paulo #include "driver_i.h"
195b9c547cSRui Paulo #include "wmm_ac.h"
205b9c547cSRui Paulo 
215b9c547cSRui Paulo static void wmm_ac_addts_req_timeout(void *eloop_ctx, void *timeout_ctx);
225b9c547cSRui Paulo 
235b9c547cSRui Paulo static const enum wmm_ac up_to_ac[8] = {
245b9c547cSRui Paulo 	WMM_AC_BK,
255b9c547cSRui Paulo 	WMM_AC_BE,
265b9c547cSRui Paulo 	WMM_AC_BE,
275b9c547cSRui Paulo 	WMM_AC_BK,
285b9c547cSRui Paulo 	WMM_AC_VI,
295b9c547cSRui Paulo 	WMM_AC_VI,
305b9c547cSRui Paulo 	WMM_AC_VO,
315b9c547cSRui Paulo 	WMM_AC_VO
325b9c547cSRui Paulo };
335b9c547cSRui Paulo 
345b9c547cSRui Paulo 
wmm_ac_get_tsid(const struct wmm_tspec_element * tspec)355b9c547cSRui Paulo static inline u8 wmm_ac_get_tsid(const struct wmm_tspec_element *tspec)
365b9c547cSRui Paulo {
375b9c547cSRui Paulo 	return (tspec->ts_info[0] >> 1) & 0x0f;
385b9c547cSRui Paulo }
395b9c547cSRui Paulo 
405b9c547cSRui Paulo 
wmm_ac_get_direction(const struct wmm_tspec_element * tspec)415b9c547cSRui Paulo static u8 wmm_ac_get_direction(const struct wmm_tspec_element *tspec)
425b9c547cSRui Paulo {
435b9c547cSRui Paulo 	return (tspec->ts_info[0] >> 5) & 0x03;
445b9c547cSRui Paulo }
455b9c547cSRui Paulo 
465b9c547cSRui Paulo 
wmm_ac_get_user_priority(const struct wmm_tspec_element * tspec)475b9c547cSRui Paulo static u8 wmm_ac_get_user_priority(const struct wmm_tspec_element *tspec)
485b9c547cSRui Paulo {
495b9c547cSRui Paulo 	return (tspec->ts_info[1] >> 3) & 0x07;
505b9c547cSRui Paulo }
515b9c547cSRui Paulo 
525b9c547cSRui Paulo 
wmm_ac_direction_to_idx(u8 direction)535b9c547cSRui Paulo static u8 wmm_ac_direction_to_idx(u8 direction)
545b9c547cSRui Paulo {
555b9c547cSRui Paulo 	switch (direction) {
565b9c547cSRui Paulo 	case WMM_AC_DIR_UPLINK:
575b9c547cSRui Paulo 		return TS_DIR_IDX_UPLINK;
585b9c547cSRui Paulo 	case WMM_AC_DIR_DOWNLINK:
595b9c547cSRui Paulo 		return TS_DIR_IDX_DOWNLINK;
605b9c547cSRui Paulo 	case WMM_AC_DIR_BIDIRECTIONAL:
615b9c547cSRui Paulo 		return TS_DIR_IDX_BIDI;
625b9c547cSRui Paulo 	default:
635b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "Invalid direction: %d", direction);
645b9c547cSRui Paulo 		return WMM_AC_DIR_UPLINK;
655b9c547cSRui Paulo 	}
665b9c547cSRui Paulo }
675b9c547cSRui Paulo 
685b9c547cSRui Paulo 
wmm_ac_add_ts(struct wpa_supplicant * wpa_s,const u8 * addr,const struct wmm_tspec_element * tspec)695b9c547cSRui Paulo static int wmm_ac_add_ts(struct wpa_supplicant *wpa_s, const u8 *addr,
705b9c547cSRui Paulo 			 const struct wmm_tspec_element *tspec)
715b9c547cSRui Paulo {
725b9c547cSRui Paulo 	struct wmm_tspec_element *_tspec;
735b9c547cSRui Paulo 	int ret;
745b9c547cSRui Paulo 	u16 admitted_time = le_to_host16(tspec->medium_time);
755b9c547cSRui Paulo 	u8 up = wmm_ac_get_user_priority(tspec);
765b9c547cSRui Paulo 	u8 ac = up_to_ac[up];
775b9c547cSRui Paulo 	u8 dir = wmm_ac_get_direction(tspec);
785b9c547cSRui Paulo 	u8 tsid = wmm_ac_get_tsid(tspec);
795b9c547cSRui Paulo 	enum ts_dir_idx idx = wmm_ac_direction_to_idx(dir);
805b9c547cSRui Paulo 
815b9c547cSRui Paulo 	/* should have been verified before, but double-check here */
825b9c547cSRui Paulo 	if (wpa_s->tspecs[ac][idx]) {
835b9c547cSRui Paulo 		wpa_printf(MSG_ERROR,
845b9c547cSRui Paulo 			   "WMM AC: tspec (ac=%d, dir=%d) already exists!",
855b9c547cSRui Paulo 			   ac, dir);
865b9c547cSRui Paulo 		return -1;
875b9c547cSRui Paulo 	}
885b9c547cSRui Paulo 
895b9c547cSRui Paulo 	/* copy tspec */
9085732ac8SCy Schubert 	_tspec = os_memdup(tspec, sizeof(*_tspec));
915b9c547cSRui Paulo 	if (!_tspec)
925b9c547cSRui Paulo 		return -1;
935b9c547cSRui Paulo 
945b9c547cSRui Paulo 	if (dir != WMM_AC_DIR_DOWNLINK) {
955b9c547cSRui Paulo 		ret = wpa_drv_add_ts(wpa_s, tsid, addr, up, admitted_time);
965b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
975b9c547cSRui Paulo 			   "WMM AC: Add TS: addr=" MACSTR
985b9c547cSRui Paulo 			   " TSID=%u admitted time=%u, ret=%d",
995b9c547cSRui Paulo 			   MAC2STR(addr), tsid, admitted_time, ret);
1005b9c547cSRui Paulo 		if (ret < 0) {
1015b9c547cSRui Paulo 			os_free(_tspec);
1025b9c547cSRui Paulo 			return -1;
1035b9c547cSRui Paulo 		}
1045b9c547cSRui Paulo 	}
1055b9c547cSRui Paulo 
1065b9c547cSRui Paulo 	wpa_s->tspecs[ac][idx] = _tspec;
1075b9c547cSRui Paulo 
1085b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Traffic stream was created successfully");
1095b9c547cSRui Paulo 
1105b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_ADDED
1115b9c547cSRui Paulo 		"tsid=%d addr=" MACSTR " admitted_time=%d",
1125b9c547cSRui Paulo 		tsid, MAC2STR(addr), admitted_time);
1135b9c547cSRui Paulo 
1145b9c547cSRui Paulo 	return 0;
1155b9c547cSRui Paulo }
1165b9c547cSRui Paulo 
1175b9c547cSRui Paulo 
wmm_ac_del_ts_idx(struct wpa_supplicant * wpa_s,u8 ac,enum ts_dir_idx dir)1185b9c547cSRui Paulo static void wmm_ac_del_ts_idx(struct wpa_supplicant *wpa_s, u8 ac,
1195b9c547cSRui Paulo 			      enum ts_dir_idx dir)
1205b9c547cSRui Paulo {
1215b9c547cSRui Paulo 	struct wmm_tspec_element *tspec = wpa_s->tspecs[ac][dir];
1225b9c547cSRui Paulo 	u8 tsid;
1235b9c547cSRui Paulo 
1245b9c547cSRui Paulo 	if (!tspec)
1255b9c547cSRui Paulo 		return;
1265b9c547cSRui Paulo 
1275b9c547cSRui Paulo 	tsid = wmm_ac_get_tsid(tspec);
1285b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WMM AC: Del TS ac=%d tsid=%d", ac, tsid);
1295b9c547cSRui Paulo 
1305b9c547cSRui Paulo 	/* update the driver in case of uplink/bidi */
1315b9c547cSRui Paulo 	if (wmm_ac_get_direction(tspec) != WMM_AC_DIR_DOWNLINK)
1325b9c547cSRui Paulo 		wpa_drv_del_ts(wpa_s, tsid, wpa_s->bssid);
1335b9c547cSRui Paulo 
1345b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_REMOVED
1355b9c547cSRui Paulo 		"tsid=%d addr=" MACSTR, tsid, MAC2STR(wpa_s->bssid));
1365b9c547cSRui Paulo 
1375b9c547cSRui Paulo 	os_free(wpa_s->tspecs[ac][dir]);
1385b9c547cSRui Paulo 	wpa_s->tspecs[ac][dir] = NULL;
1395b9c547cSRui Paulo }
1405b9c547cSRui Paulo 
1415b9c547cSRui Paulo 
wmm_ac_del_req(struct wpa_supplicant * wpa_s,int failed)1425b9c547cSRui Paulo static void wmm_ac_del_req(struct wpa_supplicant *wpa_s, int failed)
1435b9c547cSRui Paulo {
1445b9c547cSRui Paulo 	struct wmm_ac_addts_request *req = wpa_s->addts_request;
1455b9c547cSRui Paulo 
1465b9c547cSRui Paulo 	if (!req)
1475b9c547cSRui Paulo 		return;
1485b9c547cSRui Paulo 
1495b9c547cSRui Paulo 	if (failed)
1505b9c547cSRui Paulo 		wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_REQ_FAILED
1515b9c547cSRui Paulo 			"tsid=%u", wmm_ac_get_tsid(&req->tspec));
1525b9c547cSRui Paulo 
1535b9c547cSRui Paulo 	eloop_cancel_timeout(wmm_ac_addts_req_timeout, wpa_s, req);
1545b9c547cSRui Paulo 	wpa_s->addts_request = NULL;
1555b9c547cSRui Paulo 	os_free(req);
1565b9c547cSRui Paulo }
1575b9c547cSRui Paulo 
1585b9c547cSRui Paulo 
wmm_ac_addts_req_timeout(void * eloop_ctx,void * timeout_ctx)1595b9c547cSRui Paulo static void wmm_ac_addts_req_timeout(void *eloop_ctx, void *timeout_ctx)
1605b9c547cSRui Paulo {
1615b9c547cSRui Paulo 	struct wpa_supplicant *wpa_s = eloop_ctx;
1625b9c547cSRui Paulo 	struct wmm_ac_addts_request *addts_req = timeout_ctx;
1635b9c547cSRui Paulo 
1645b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG,
1655b9c547cSRui Paulo 		   "Timeout getting ADDTS response (tsid=%d up=%d)",
1665b9c547cSRui Paulo 		   wmm_ac_get_tsid(&addts_req->tspec),
1675b9c547cSRui Paulo 		   wmm_ac_get_user_priority(&addts_req->tspec));
1685b9c547cSRui Paulo 
1695b9c547cSRui Paulo 	wmm_ac_del_req(wpa_s, 1);
1705b9c547cSRui Paulo }
1715b9c547cSRui Paulo 
1725b9c547cSRui Paulo 
wmm_ac_send_addts_request(struct wpa_supplicant * wpa_s,const struct wmm_ac_addts_request * req)1735b9c547cSRui Paulo static int wmm_ac_send_addts_request(struct wpa_supplicant *wpa_s,
1745b9c547cSRui Paulo 				     const struct wmm_ac_addts_request *req)
1755b9c547cSRui Paulo {
1765b9c547cSRui Paulo 	struct wpabuf *buf;
1775b9c547cSRui Paulo 	int ret;
1785b9c547cSRui Paulo 
1795b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Sending ADDTS Request to " MACSTR,
1805b9c547cSRui Paulo 		   MAC2STR(req->address));
1815b9c547cSRui Paulo 
1825b9c547cSRui Paulo 	/* category + action code + dialog token + status + sizeof(tspec) */
1835b9c547cSRui Paulo 	buf = wpabuf_alloc(4 + sizeof(req->tspec));
1845b9c547cSRui Paulo 	if (!buf) {
1855b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "WMM AC: Allocation error");
1865b9c547cSRui Paulo 		return -1;
1875b9c547cSRui Paulo 	}
1885b9c547cSRui Paulo 
1895b9c547cSRui Paulo 	wpabuf_put_u8(buf, WLAN_ACTION_WMM);
1905b9c547cSRui Paulo 	wpabuf_put_u8(buf, WMM_ACTION_CODE_ADDTS_REQ);
1915b9c547cSRui Paulo 	wpabuf_put_u8(buf, req->dialog_token);
1925b9c547cSRui Paulo 	wpabuf_put_u8(buf, 0); /* status code */
1935b9c547cSRui Paulo 	wpabuf_put_data(buf, &req->tspec, sizeof(req->tspec));
1945b9c547cSRui Paulo 
1955b9c547cSRui Paulo 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, req->address,
1965b9c547cSRui Paulo 				wpa_s->own_addr, wpa_s->bssid,
1975b9c547cSRui Paulo 				wpabuf_head(buf), wpabuf_len(buf), 0);
1985b9c547cSRui Paulo 	if (ret) {
1995b9c547cSRui Paulo 		wpa_printf(MSG_WARNING,
2005b9c547cSRui Paulo 			   "WMM AC: Failed to send ADDTS Request");
2015b9c547cSRui Paulo 	}
2025b9c547cSRui Paulo 
2035b9c547cSRui Paulo 	wpabuf_free(buf);
2045b9c547cSRui Paulo 	return ret;
2055b9c547cSRui Paulo }
2065b9c547cSRui Paulo 
2075b9c547cSRui Paulo 
wmm_ac_send_delts(struct wpa_supplicant * wpa_s,const struct wmm_tspec_element * tspec,const u8 * address)2085b9c547cSRui Paulo static int wmm_ac_send_delts(struct wpa_supplicant *wpa_s,
2095b9c547cSRui Paulo 			     const struct wmm_tspec_element *tspec,
2105b9c547cSRui Paulo 			     const u8 *address)
2115b9c547cSRui Paulo {
2125b9c547cSRui Paulo 	struct wpabuf *buf;
2135b9c547cSRui Paulo 	int ret;
2145b9c547cSRui Paulo 
2155b9c547cSRui Paulo 	/* category + action code + dialog token + status + sizeof(tspec) */
2165b9c547cSRui Paulo 	buf = wpabuf_alloc(4 + sizeof(*tspec));
2175b9c547cSRui Paulo 	if (!buf)
2185b9c547cSRui Paulo 		return -1;
2195b9c547cSRui Paulo 
2205b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Sending DELTS to " MACSTR, MAC2STR(address));
2215b9c547cSRui Paulo 
2225b9c547cSRui Paulo 	/* category + action code + dialog token + status + sizeof(tspec) */
2235b9c547cSRui Paulo 	wpabuf_put_u8(buf, WLAN_ACTION_WMM);
2245b9c547cSRui Paulo 	wpabuf_put_u8(buf, WMM_ACTION_CODE_DELTS);
2255b9c547cSRui Paulo 	wpabuf_put_u8(buf, 0); /* Dialog Token (not used) */
2265b9c547cSRui Paulo 	wpabuf_put_u8(buf, 0); /* Status Code (not used) */
2275b9c547cSRui Paulo 	wpabuf_put_data(buf, tspec, sizeof(*tspec));
2285b9c547cSRui Paulo 
2295b9c547cSRui Paulo 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, address,
2305b9c547cSRui Paulo 				  wpa_s->own_addr, wpa_s->bssid,
2315b9c547cSRui Paulo 				  wpabuf_head(buf), wpabuf_len(buf), 0);
2325b9c547cSRui Paulo 	if (ret)
2335b9c547cSRui Paulo 		wpa_printf(MSG_WARNING, "Failed to send DELTS frame");
2345b9c547cSRui Paulo 
2355b9c547cSRui Paulo 	wpabuf_free(buf);
2365b9c547cSRui Paulo 	return ret;
2375b9c547cSRui Paulo }
2385b9c547cSRui Paulo 
2395b9c547cSRui Paulo 
2405b9c547cSRui Paulo /* return the AC using the given TSPEC tid */
wmm_ac_find_tsid(struct wpa_supplicant * wpa_s,u8 tsid,enum ts_dir_idx * dir)2415b9c547cSRui Paulo static int wmm_ac_find_tsid(struct wpa_supplicant *wpa_s, u8 tsid,
2425b9c547cSRui Paulo 			    enum ts_dir_idx *dir)
2435b9c547cSRui Paulo {
2445b9c547cSRui Paulo 	int ac;
2455b9c547cSRui Paulo 	enum ts_dir_idx idx;
2465b9c547cSRui Paulo 
2475b9c547cSRui Paulo 	for (ac = 0; ac < WMM_AC_NUM; ac++) {
2485b9c547cSRui Paulo 		for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
2495b9c547cSRui Paulo 			if (wpa_s->tspecs[ac][idx] &&
2505b9c547cSRui Paulo 			    wmm_ac_get_tsid(wpa_s->tspecs[ac][idx]) == tsid) {
2515b9c547cSRui Paulo 				if (dir)
2525b9c547cSRui Paulo 					*dir = idx;
2535b9c547cSRui Paulo 				return ac;
2545b9c547cSRui Paulo 			}
2555b9c547cSRui Paulo 		}
2565b9c547cSRui Paulo 	}
2575b9c547cSRui Paulo 
2585b9c547cSRui Paulo 	return -1;
2595b9c547cSRui Paulo }
2605b9c547cSRui Paulo 
2615b9c547cSRui Paulo 
2625b9c547cSRui Paulo static struct wmm_ac_addts_request *
wmm_ac_build_addts_req(struct wpa_supplicant * wpa_s,const struct wmm_ac_ts_setup_params * params,const u8 * address)2635b9c547cSRui Paulo wmm_ac_build_addts_req(struct wpa_supplicant *wpa_s,
2645b9c547cSRui Paulo 		       const struct wmm_ac_ts_setup_params *params,
2655b9c547cSRui Paulo 		       const u8 *address)
2665b9c547cSRui Paulo {
2675b9c547cSRui Paulo 	struct wmm_ac_addts_request *addts_req;
2685b9c547cSRui Paulo 	struct wmm_tspec_element *tspec;
2695b9c547cSRui Paulo 	u8 ac = up_to_ac[params->user_priority];
2705b9c547cSRui Paulo 	u8 uapsd = wpa_s->wmm_ac_assoc_info->ac_params[ac].uapsd;
2715b9c547cSRui Paulo 
2725b9c547cSRui Paulo 	addts_req = os_zalloc(sizeof(*addts_req));
2735b9c547cSRui Paulo 	if (!addts_req)
2745b9c547cSRui Paulo 		return NULL;
2755b9c547cSRui Paulo 
2765b9c547cSRui Paulo 	tspec = &addts_req->tspec;
2775b9c547cSRui Paulo 	os_memcpy(addts_req->address, address, ETH_ALEN);
2785b9c547cSRui Paulo 
2795b9c547cSRui Paulo 	/* The dialog token cannot be zero */
2805b9c547cSRui Paulo 	if (++wpa_s->wmm_ac_last_dialog_token == 0)
2815b9c547cSRui Paulo 		wpa_s->wmm_ac_last_dialog_token++;
2825b9c547cSRui Paulo 
2835b9c547cSRui Paulo 	addts_req->dialog_token = wpa_s->wmm_ac_last_dialog_token;
2845b9c547cSRui Paulo 	tspec->eid = WLAN_EID_VENDOR_SPECIFIC;
2855b9c547cSRui Paulo 	tspec->length = sizeof(*tspec) - 2; /* reduce eid and length */
2865b9c547cSRui Paulo 	tspec->oui[0] = 0x00;
2875b9c547cSRui Paulo 	tspec->oui[1] = 0x50;
2885b9c547cSRui Paulo 	tspec->oui[2] = 0xf2;
2895b9c547cSRui Paulo 	tspec->oui_type = WMM_OUI_TYPE;
2905b9c547cSRui Paulo 	tspec->oui_subtype = WMM_OUI_SUBTYPE_TSPEC_ELEMENT;
2915b9c547cSRui Paulo 	tspec->version = WMM_VERSION;
2925b9c547cSRui Paulo 
2935b9c547cSRui Paulo 	tspec->ts_info[0] = params->tsid << 1;
2945b9c547cSRui Paulo 	tspec->ts_info[0] |= params->direction << 5;
2955b9c547cSRui Paulo 	tspec->ts_info[0] |= WMM_AC_ACCESS_POLICY_EDCA << 7;
2965b9c547cSRui Paulo 	tspec->ts_info[1] = uapsd << 2;
2975b9c547cSRui Paulo 	tspec->ts_info[1] |= params->user_priority << 3;
2985b9c547cSRui Paulo 	tspec->ts_info[2] = 0;
2995b9c547cSRui Paulo 
3005b9c547cSRui Paulo 	tspec->nominal_msdu_size = host_to_le16(params->nominal_msdu_size);
3015b9c547cSRui Paulo 	if (params->fixed_nominal_msdu)
3025b9c547cSRui Paulo 		tspec->nominal_msdu_size |=
3035b9c547cSRui Paulo 			host_to_le16(WMM_AC_FIXED_MSDU_SIZE);
3045b9c547cSRui Paulo 
3055b9c547cSRui Paulo 	tspec->mean_data_rate = host_to_le32(params->mean_data_rate);
3065b9c547cSRui Paulo 	tspec->minimum_phy_rate = host_to_le32(params->minimum_phy_rate);
3075b9c547cSRui Paulo 	tspec->surplus_bandwidth_allowance =
3085b9c547cSRui Paulo 		host_to_le16(params->surplus_bandwidth_allowance);
3095b9c547cSRui Paulo 
3105b9c547cSRui Paulo 	return addts_req;
3115b9c547cSRui Paulo }
3125b9c547cSRui Paulo 
3135b9c547cSRui Paulo 
param_in_range(const char * name,long value,long min_val,long max_val)3145b9c547cSRui Paulo static int param_in_range(const char *name, long value,
3155b9c547cSRui Paulo 			  long min_val, long max_val)
3165b9c547cSRui Paulo {
3175b9c547cSRui Paulo 	if (value < min_val || (max_val >= 0 && value > max_val)) {
3185b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
3195b9c547cSRui Paulo 			   "WMM AC: param %s (%ld) is out of range (%ld-%ld)",
3205b9c547cSRui Paulo 			   name, value, min_val, max_val);
3215b9c547cSRui Paulo 		return 0;
3225b9c547cSRui Paulo 	}
3235b9c547cSRui Paulo 
3245b9c547cSRui Paulo 	return 1;
3255b9c547cSRui Paulo }
3265b9c547cSRui Paulo 
3275b9c547cSRui Paulo 
wmm_ac_should_replace_ts(struct wpa_supplicant * wpa_s,u8 tsid,u8 ac,u8 dir)3285b9c547cSRui Paulo static int wmm_ac_should_replace_ts(struct wpa_supplicant *wpa_s,
3295b9c547cSRui Paulo 				    u8 tsid, u8 ac, u8 dir)
3305b9c547cSRui Paulo {
3315b9c547cSRui Paulo 	enum ts_dir_idx idx;
3325b9c547cSRui Paulo 	int cur_ac, existing_ts = 0, replace_ts = 0;
3335b9c547cSRui Paulo 
3345b9c547cSRui Paulo 	cur_ac = wmm_ac_find_tsid(wpa_s, tsid, &idx);
3355b9c547cSRui Paulo 	if (cur_ac >= 0) {
3365b9c547cSRui Paulo 		if (cur_ac != ac) {
3375b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
3385b9c547cSRui Paulo 				   "WMM AC: TSID %i already exists on different ac (%d)",
3395b9c547cSRui Paulo 				   tsid, cur_ac);
3405b9c547cSRui Paulo 			return -1;
3415b9c547cSRui Paulo 		}
3425b9c547cSRui Paulo 
3435b9c547cSRui Paulo 		/* same tsid - this tspec will replace the current one */
3445b9c547cSRui Paulo 		replace_ts |= BIT(idx);
3455b9c547cSRui Paulo 	}
3465b9c547cSRui Paulo 
3475b9c547cSRui Paulo 	for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
3485b9c547cSRui Paulo 		if (wpa_s->tspecs[ac][idx])
3495b9c547cSRui Paulo 			existing_ts |= BIT(idx);
3505b9c547cSRui Paulo 	}
3515b9c547cSRui Paulo 
3525b9c547cSRui Paulo 	switch (dir) {
3535b9c547cSRui Paulo 	case WMM_AC_DIR_UPLINK:
3545b9c547cSRui Paulo 		/* replace existing uplink/bidi tspecs */
3555b9c547cSRui Paulo 		replace_ts |= existing_ts & (BIT(TS_DIR_IDX_UPLINK) |
3565b9c547cSRui Paulo 					     BIT(TS_DIR_IDX_BIDI));
3575b9c547cSRui Paulo 		break;
3585b9c547cSRui Paulo 	case WMM_AC_DIR_DOWNLINK:
3595b9c547cSRui Paulo 		/* replace existing downlink/bidi tspecs */
3605b9c547cSRui Paulo 		replace_ts |= existing_ts & (BIT(TS_DIR_IDX_DOWNLINK) |
3615b9c547cSRui Paulo 					     BIT(TS_DIR_IDX_BIDI));
3625b9c547cSRui Paulo 		break;
3635b9c547cSRui Paulo 	case WMM_AC_DIR_BIDIRECTIONAL:
3645b9c547cSRui Paulo 		/* replace all existing tspecs */
3655b9c547cSRui Paulo 		replace_ts |= existing_ts;
3665b9c547cSRui Paulo 		break;
3675b9c547cSRui Paulo 	default:
3685b9c547cSRui Paulo 		return -1;
3695b9c547cSRui Paulo 	}
3705b9c547cSRui Paulo 
3715b9c547cSRui Paulo 	return replace_ts;
3725b9c547cSRui Paulo }
3735b9c547cSRui Paulo 
3745b9c547cSRui Paulo 
wmm_ac_ts_req_is_valid(struct wpa_supplicant * wpa_s,const struct wmm_ac_ts_setup_params * params)3755b9c547cSRui Paulo static int wmm_ac_ts_req_is_valid(struct wpa_supplicant *wpa_s,
3765b9c547cSRui Paulo 				  const struct wmm_ac_ts_setup_params *params)
3775b9c547cSRui Paulo {
3785b9c547cSRui Paulo 	enum wmm_ac req_ac;
3795b9c547cSRui Paulo 
3805b9c547cSRui Paulo #define PARAM_IN_RANGE(field, min_value, max_value) \
3815b9c547cSRui Paulo 	param_in_range(#field, params->field, min_value, max_value)
3825b9c547cSRui Paulo 
3835b9c547cSRui Paulo 	if (!PARAM_IN_RANGE(tsid, 0, WMM_AC_MAX_TID) ||
3845b9c547cSRui Paulo 	    !PARAM_IN_RANGE(user_priority, 0, WMM_AC_MAX_USER_PRIORITY) ||
3855b9c547cSRui Paulo 	    !PARAM_IN_RANGE(nominal_msdu_size, 1, WMM_AC_MAX_NOMINAL_MSDU) ||
3865b9c547cSRui Paulo 	    !PARAM_IN_RANGE(mean_data_rate, 1, -1) ||
3875b9c547cSRui Paulo 	    !PARAM_IN_RANGE(minimum_phy_rate, 1, -1) ||
3885b9c547cSRui Paulo 	    !PARAM_IN_RANGE(surplus_bandwidth_allowance, WMM_AC_MIN_SBA_UNITY,
3895b9c547cSRui Paulo 			    -1))
3905b9c547cSRui Paulo 		return 0;
3915b9c547cSRui Paulo #undef PARAM_IN_RANGE
3925b9c547cSRui Paulo 
3935b9c547cSRui Paulo 	if (!(params->direction == WMM_TSPEC_DIRECTION_UPLINK ||
3945b9c547cSRui Paulo 	      params->direction == WMM_TSPEC_DIRECTION_DOWNLINK ||
3955b9c547cSRui Paulo 	      params->direction == WMM_TSPEC_DIRECTION_BI_DIRECTIONAL)) {
3965b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WMM AC: invalid TS direction: %d",
3975b9c547cSRui Paulo 			   params->direction);
3985b9c547cSRui Paulo 		return 0;
3995b9c547cSRui Paulo 	}
4005b9c547cSRui Paulo 
4015b9c547cSRui Paulo 	req_ac = up_to_ac[params->user_priority];
4025b9c547cSRui Paulo 
403c1d255d3SCy Schubert 	/* Requested access category must have acm */
4045b9c547cSRui Paulo 	if (!wpa_s->wmm_ac_assoc_info->ac_params[req_ac].acm) {
4055b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WMM AC: AC %d is not ACM", req_ac);
4065b9c547cSRui Paulo 		return 0;
4075b9c547cSRui Paulo 	}
4085b9c547cSRui Paulo 
4095b9c547cSRui Paulo 	if (wmm_ac_should_replace_ts(wpa_s, params->tsid, req_ac,
4105b9c547cSRui Paulo 				     params->direction) < 0)
4115b9c547cSRui Paulo 		return 0;
4125b9c547cSRui Paulo 
4135b9c547cSRui Paulo 	return 1;
4145b9c547cSRui Paulo }
4155b9c547cSRui Paulo 
4165b9c547cSRui Paulo 
4175b9c547cSRui Paulo static struct wmm_ac_assoc_data *
wmm_ac_process_param_elem(struct wpa_supplicant * wpa_s,const u8 * ies,size_t ies_len)4185b9c547cSRui Paulo wmm_ac_process_param_elem(struct wpa_supplicant *wpa_s, const u8 *ies,
4195b9c547cSRui Paulo 			  size_t ies_len)
4205b9c547cSRui Paulo {
4215b9c547cSRui Paulo 	struct ieee802_11_elems elems;
4225b9c547cSRui Paulo 	struct wmm_parameter_element *wmm_params;
4235b9c547cSRui Paulo 	struct wmm_ac_assoc_data *assoc_data;
4245b9c547cSRui Paulo 	int i;
4255b9c547cSRui Paulo 
4265b9c547cSRui Paulo 	/* Parsing WMM Parameter Element */
4275b9c547cSRui Paulo 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
4285b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WMM AC: could not parse assoc ies");
4295b9c547cSRui Paulo 		return NULL;
4305b9c547cSRui Paulo 	}
4315b9c547cSRui Paulo 
4325b9c547cSRui Paulo 	if (!elems.wmm) {
4335b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WMM AC: No WMM IE");
4345b9c547cSRui Paulo 		return NULL;
4355b9c547cSRui Paulo 	}
4365b9c547cSRui Paulo 
4375b9c547cSRui Paulo 	if (elems.wmm_len != sizeof(*wmm_params)) {
4385b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WMM AC: Invalid WMM ie length");
4395b9c547cSRui Paulo 		return NULL;
4405b9c547cSRui Paulo 	}
4415b9c547cSRui Paulo 
4425b9c547cSRui Paulo 	wmm_params = (struct wmm_parameter_element *)(elems.wmm);
4435b9c547cSRui Paulo 
4445b9c547cSRui Paulo 	assoc_data = os_zalloc(sizeof(*assoc_data));
4455b9c547cSRui Paulo 	if (!assoc_data)
4465b9c547cSRui Paulo 		return NULL;
4475b9c547cSRui Paulo 
4485b9c547cSRui Paulo 	for (i = 0; i < WMM_AC_NUM; i++)
4495b9c547cSRui Paulo 		assoc_data->ac_params[i].acm =
4505b9c547cSRui Paulo 			!!(wmm_params->ac[i].aci_aifsn & WMM_AC_ACM);
4515b9c547cSRui Paulo 
4525b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG,
4535b9c547cSRui Paulo 		   "WMM AC: AC mandatory: AC_BE=%u AC_BK=%u AC_VI=%u AC_VO=%u",
4545b9c547cSRui Paulo 		   assoc_data->ac_params[WMM_AC_BE].acm,
4555b9c547cSRui Paulo 		   assoc_data->ac_params[WMM_AC_BK].acm,
4565b9c547cSRui Paulo 		   assoc_data->ac_params[WMM_AC_VI].acm,
4575b9c547cSRui Paulo 		   assoc_data->ac_params[WMM_AC_VO].acm);
4585b9c547cSRui Paulo 
4595b9c547cSRui Paulo 	return assoc_data;
4605b9c547cSRui Paulo }
4615b9c547cSRui Paulo 
4625b9c547cSRui Paulo 
wmm_ac_init(struct wpa_supplicant * wpa_s,const u8 * ies,size_t ies_len,const struct wmm_params * wmm_params)4635b9c547cSRui Paulo static int wmm_ac_init(struct wpa_supplicant *wpa_s, const u8 *ies,
4645b9c547cSRui Paulo 		       size_t ies_len, const struct wmm_params *wmm_params)
4655b9c547cSRui Paulo {
4665b9c547cSRui Paulo 	struct wmm_ac_assoc_data *assoc_data;
4675b9c547cSRui Paulo 	u8 ac;
4685b9c547cSRui Paulo 
4695b9c547cSRui Paulo 	if (wpa_s->wmm_ac_assoc_info) {
4705b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "WMM AC: Already initialized");
4715b9c547cSRui Paulo 		return -1;
4725b9c547cSRui Paulo 	}
4735b9c547cSRui Paulo 
4744bc52338SCy Schubert 	if (!ies || !(wmm_params->info_bitmap & WMM_PARAMS_UAPSD_QUEUES_INFO)) {
4754bc52338SCy Schubert 		/* WMM AC not in use for this connection */
4765b9c547cSRui Paulo 		return -1;
4775b9c547cSRui Paulo 	}
4785b9c547cSRui Paulo 
4795b9c547cSRui Paulo 	os_memset(wpa_s->tspecs, 0, sizeof(wpa_s->tspecs));
4805b9c547cSRui Paulo 	wpa_s->wmm_ac_last_dialog_token = 0;
4815b9c547cSRui Paulo 	wpa_s->addts_request = NULL;
4825b9c547cSRui Paulo 
4835b9c547cSRui Paulo 	assoc_data = wmm_ac_process_param_elem(wpa_s, ies, ies_len);
4845b9c547cSRui Paulo 	if (!assoc_data)
4855b9c547cSRui Paulo 		return -1;
4865b9c547cSRui Paulo 
4875b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WMM AC: U-APSD queues=0x%x",
4885b9c547cSRui Paulo 		   wmm_params->uapsd_queues);
4895b9c547cSRui Paulo 
4905b9c547cSRui Paulo 	for (ac = 0; ac < WMM_AC_NUM; ac++) {
4915b9c547cSRui Paulo 		assoc_data->ac_params[ac].uapsd =
4925b9c547cSRui Paulo 			!!(wmm_params->uapsd_queues & BIT(ac));
4935b9c547cSRui Paulo 	}
4945b9c547cSRui Paulo 
4955b9c547cSRui Paulo 	wpa_s->wmm_ac_assoc_info = assoc_data;
4965b9c547cSRui Paulo 	return 0;
4975b9c547cSRui Paulo }
4985b9c547cSRui Paulo 
4995b9c547cSRui Paulo 
wmm_ac_del_ts(struct wpa_supplicant * wpa_s,u8 ac,int dir_bitmap)5005b9c547cSRui Paulo static void wmm_ac_del_ts(struct wpa_supplicant *wpa_s, u8 ac, int dir_bitmap)
5015b9c547cSRui Paulo {
5025b9c547cSRui Paulo 	enum ts_dir_idx idx;
5035b9c547cSRui Paulo 
5045b9c547cSRui Paulo 	for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
5055b9c547cSRui Paulo 		if (!(dir_bitmap & BIT(idx)))
5065b9c547cSRui Paulo 			continue;
5075b9c547cSRui Paulo 
5085b9c547cSRui Paulo 		wmm_ac_del_ts_idx(wpa_s, ac, idx);
5095b9c547cSRui Paulo 	}
5105b9c547cSRui Paulo }
5115b9c547cSRui Paulo 
5125b9c547cSRui Paulo 
wmm_ac_deinit(struct wpa_supplicant * wpa_s)5135b9c547cSRui Paulo static void wmm_ac_deinit(struct wpa_supplicant *wpa_s)
5145b9c547cSRui Paulo {
5155b9c547cSRui Paulo 	int i;
5165b9c547cSRui Paulo 
5175b9c547cSRui Paulo 	for (i = 0; i < WMM_AC_NUM; i++)
5185b9c547cSRui Paulo 		wmm_ac_del_ts(wpa_s, i, TS_DIR_IDX_ALL);
5195b9c547cSRui Paulo 
5204bc52338SCy Schubert 	/* delete pending add_ts request */
5215b9c547cSRui Paulo 	wmm_ac_del_req(wpa_s, 1);
5225b9c547cSRui Paulo 
5235b9c547cSRui Paulo 	os_free(wpa_s->wmm_ac_assoc_info);
5245b9c547cSRui Paulo 	wpa_s->wmm_ac_assoc_info = NULL;
5255b9c547cSRui Paulo }
5265b9c547cSRui Paulo 
5275b9c547cSRui Paulo 
wmm_ac_notify_assoc(struct wpa_supplicant * wpa_s,const u8 * ies,size_t ies_len,const struct wmm_params * wmm_params)5285b9c547cSRui Paulo void wmm_ac_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *ies,
5295b9c547cSRui Paulo 			 size_t ies_len, const struct wmm_params *wmm_params)
5305b9c547cSRui Paulo {
5315b9c547cSRui Paulo 	if (wmm_ac_init(wpa_s, ies, ies_len, wmm_params))
5325b9c547cSRui Paulo 		return;
5335b9c547cSRui Paulo 
5345b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG,
5355b9c547cSRui Paulo 		   "WMM AC: Valid WMM association, WMM AC is enabled");
5365b9c547cSRui Paulo }
5375b9c547cSRui Paulo 
5385b9c547cSRui Paulo 
wmm_ac_notify_disassoc(struct wpa_supplicant * wpa_s)5395b9c547cSRui Paulo void wmm_ac_notify_disassoc(struct wpa_supplicant *wpa_s)
5405b9c547cSRui Paulo {
5415b9c547cSRui Paulo 	if (!wpa_s->wmm_ac_assoc_info)
5425b9c547cSRui Paulo 		return;
5435b9c547cSRui Paulo 
5445b9c547cSRui Paulo 	wmm_ac_deinit(wpa_s);
5455b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WMM AC: WMM AC is disabled");
5465b9c547cSRui Paulo }
5475b9c547cSRui Paulo 
5485b9c547cSRui Paulo 
wpas_wmm_ac_delts(struct wpa_supplicant * wpa_s,u8 tsid)5495b9c547cSRui Paulo int wpas_wmm_ac_delts(struct wpa_supplicant *wpa_s, u8 tsid)
5505b9c547cSRui Paulo {
5515b9c547cSRui Paulo 	struct wmm_tspec_element tspec;
5525b9c547cSRui Paulo 	int ac;
5535b9c547cSRui Paulo 	enum ts_dir_idx dir;
5545b9c547cSRui Paulo 
5555b9c547cSRui Paulo 	if (!wpa_s->wmm_ac_assoc_info) {
5565b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
5575b9c547cSRui Paulo 			   "WMM AC: Failed to delete TS, WMM AC is disabled");
5585b9c547cSRui Paulo 		return -1;
5595b9c547cSRui Paulo 	}
5605b9c547cSRui Paulo 
5615b9c547cSRui Paulo 	ac = wmm_ac_find_tsid(wpa_s, tsid, &dir);
5625b9c547cSRui Paulo 	if (ac < 0) {
5635b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WMM AC: TS does not exist");
5645b9c547cSRui Paulo 		return -1;
5655b9c547cSRui Paulo 	}
5665b9c547cSRui Paulo 
5675b9c547cSRui Paulo 	tspec = *wpa_s->tspecs[ac][dir];
5685b9c547cSRui Paulo 
5695b9c547cSRui Paulo 	wmm_ac_del_ts_idx(wpa_s, ac, dir);
5705b9c547cSRui Paulo 
5715b9c547cSRui Paulo 	wmm_ac_send_delts(wpa_s, &tspec, wpa_s->bssid);
5725b9c547cSRui Paulo 
5735b9c547cSRui Paulo 	return 0;
5745b9c547cSRui Paulo }
5755b9c547cSRui Paulo 
5765b9c547cSRui Paulo 
wpas_wmm_ac_addts(struct wpa_supplicant * wpa_s,struct wmm_ac_ts_setup_params * params)5775b9c547cSRui Paulo int wpas_wmm_ac_addts(struct wpa_supplicant *wpa_s,
5785b9c547cSRui Paulo 		      struct wmm_ac_ts_setup_params *params)
5795b9c547cSRui Paulo {
5805b9c547cSRui Paulo 	struct wmm_ac_addts_request *addts_req;
5815b9c547cSRui Paulo 
5825b9c547cSRui Paulo 	if (!wpa_s->wmm_ac_assoc_info) {
5835b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
5845b9c547cSRui Paulo 			   "WMM AC: Cannot add TS - missing assoc data");
5855b9c547cSRui Paulo 		return -1;
5865b9c547cSRui Paulo 	}
5875b9c547cSRui Paulo 
5885b9c547cSRui Paulo 	if (wpa_s->addts_request) {
5895b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
5905b9c547cSRui Paulo 			   "WMM AC: can't add TS - ADDTS request is already pending");
5915b9c547cSRui Paulo 		return -1;
5925b9c547cSRui Paulo 	}
5935b9c547cSRui Paulo 
5945b9c547cSRui Paulo 	/*
5955b9c547cSRui Paulo 	 * we can setup downlink TS even without driver support.
5965b9c547cSRui Paulo 	 * however, we need driver support for the other directions.
5975b9c547cSRui Paulo 	 */
5985b9c547cSRui Paulo 	if (params->direction != WMM_AC_DIR_DOWNLINK &&
5995b9c547cSRui Paulo 	    !wpa_s->wmm_ac_supported) {
6005b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
6015b9c547cSRui Paulo 			   "Cannot set uplink/bidi TS without driver support");
6025b9c547cSRui Paulo 		return -1;
6035b9c547cSRui Paulo 	}
6045b9c547cSRui Paulo 
6055b9c547cSRui Paulo 	if (!wmm_ac_ts_req_is_valid(wpa_s, params))
6065b9c547cSRui Paulo 		return -1;
6075b9c547cSRui Paulo 
6085b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WMM AC: TS setup request (addr=" MACSTR
6095b9c547cSRui Paulo 		   " tsid=%u user priority=%u direction=%d)",
6105b9c547cSRui Paulo 		   MAC2STR(wpa_s->bssid), params->tsid,
6115b9c547cSRui Paulo 		   params->user_priority, params->direction);
6125b9c547cSRui Paulo 
6135b9c547cSRui Paulo 	addts_req = wmm_ac_build_addts_req(wpa_s, params, wpa_s->bssid);
6145b9c547cSRui Paulo 	if (!addts_req)
6155b9c547cSRui Paulo 		return -1;
6165b9c547cSRui Paulo 
6175b9c547cSRui Paulo 	if (wmm_ac_send_addts_request(wpa_s, addts_req))
6185b9c547cSRui Paulo 		goto err;
6195b9c547cSRui Paulo 
6205b9c547cSRui Paulo 	/* save as pending and set ADDTS resp timeout to 1 second */
6215b9c547cSRui Paulo 	wpa_s->addts_request = addts_req;
6225b9c547cSRui Paulo 	eloop_register_timeout(1, 0, wmm_ac_addts_req_timeout,
6235b9c547cSRui Paulo 			       wpa_s, addts_req);
6245b9c547cSRui Paulo 	return 0;
6255b9c547cSRui Paulo err:
6265b9c547cSRui Paulo 	os_free(addts_req);
6275b9c547cSRui Paulo 	return -1;
6285b9c547cSRui Paulo }
6295b9c547cSRui Paulo 
6305b9c547cSRui Paulo 
wmm_ac_handle_delts(struct wpa_supplicant * wpa_s,const u8 * sa,const struct wmm_tspec_element * tspec)6315b9c547cSRui Paulo static void wmm_ac_handle_delts(struct wpa_supplicant *wpa_s, const u8 *sa,
6325b9c547cSRui Paulo 				const struct wmm_tspec_element *tspec)
6335b9c547cSRui Paulo {
6345b9c547cSRui Paulo 	int ac;
6355b9c547cSRui Paulo 	u8 tsid;
6365b9c547cSRui Paulo 	enum ts_dir_idx idx;
6375b9c547cSRui Paulo 
6385b9c547cSRui Paulo 	tsid = wmm_ac_get_tsid(tspec);
6395b9c547cSRui Paulo 
6405b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG,
6415b9c547cSRui Paulo 		   "WMM AC: DELTS frame has been received TSID=%u addr="
6425b9c547cSRui Paulo 		   MACSTR, tsid, MAC2STR(sa));
6435b9c547cSRui Paulo 
6445b9c547cSRui Paulo 	ac = wmm_ac_find_tsid(wpa_s, tsid, &idx);
6455b9c547cSRui Paulo 	if (ac < 0) {
6465b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
6475b9c547cSRui Paulo 			   "WMM AC: Ignoring DELTS frame - TSID does not exist");
6485b9c547cSRui Paulo 		return;
6495b9c547cSRui Paulo 	}
6505b9c547cSRui Paulo 
6515b9c547cSRui Paulo 	wmm_ac_del_ts_idx(wpa_s, ac, idx);
6525b9c547cSRui Paulo 
6535b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG,
6545b9c547cSRui Paulo 		   "TS was deleted successfully (tsid=%u address=" MACSTR ")",
6555b9c547cSRui Paulo 		   tsid, MAC2STR(sa));
6565b9c547cSRui Paulo }
6575b9c547cSRui Paulo 
6585b9c547cSRui Paulo 
wmm_ac_handle_addts_resp(struct wpa_supplicant * wpa_s,const u8 * sa,const u8 resp_dialog_token,const u8 status_code,const struct wmm_tspec_element * tspec)6595b9c547cSRui Paulo static void wmm_ac_handle_addts_resp(struct wpa_supplicant *wpa_s, const u8 *sa,
6605b9c547cSRui Paulo 		const u8 resp_dialog_token, const u8 status_code,
6615b9c547cSRui Paulo 		const struct wmm_tspec_element *tspec)
6625b9c547cSRui Paulo {
6635b9c547cSRui Paulo 	struct wmm_ac_addts_request *req = wpa_s->addts_request;
6645b9c547cSRui Paulo 	u8 ac, tsid, up, dir;
6655b9c547cSRui Paulo 	int replace_tspecs;
6665b9c547cSRui Paulo 
6675b9c547cSRui Paulo 	tsid = wmm_ac_get_tsid(tspec);
6685b9c547cSRui Paulo 	dir = wmm_ac_get_direction(tspec);
6695b9c547cSRui Paulo 	up = wmm_ac_get_user_priority(tspec);
6705b9c547cSRui Paulo 	ac = up_to_ac[up];
6715b9c547cSRui Paulo 
6725b9c547cSRui Paulo 	/* make sure we have a matching addts request */
6735b9c547cSRui Paulo 	if (!req || req->dialog_token != resp_dialog_token) {
6745b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
6755b9c547cSRui Paulo 			   "WMM AC: no req with dialog=%u, ignoring frame",
6765b9c547cSRui Paulo 			   resp_dialog_token);
6775b9c547cSRui Paulo 		return;
6785b9c547cSRui Paulo 	}
6795b9c547cSRui Paulo 
6805b9c547cSRui Paulo 	/* make sure the params are the same */
681*a90b9d01SCy Schubert 	if (!ether_addr_equal(req->address, sa) ||
6825b9c547cSRui Paulo 	    tsid != wmm_ac_get_tsid(&req->tspec) ||
6835b9c547cSRui Paulo 	    up != wmm_ac_get_user_priority(&req->tspec) ||
6845b9c547cSRui Paulo 	    dir != wmm_ac_get_direction(&req->tspec)) {
6855b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
6865b9c547cSRui Paulo 			   "WMM AC: ADDTS params do not match, ignoring frame");
6875b9c547cSRui Paulo 		return;
6885b9c547cSRui Paulo 	}
6895b9c547cSRui Paulo 
6905b9c547cSRui Paulo 	/* delete pending request */
6915b9c547cSRui Paulo 	wmm_ac_del_req(wpa_s, 0);
6925b9c547cSRui Paulo 
6935b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG,
6945b9c547cSRui Paulo 		   "ADDTS response status=%d tsid=%u up=%u direction=%u",
6955b9c547cSRui Paulo 		   status_code, tsid, up, dir);
6965b9c547cSRui Paulo 
6975b9c547cSRui Paulo 	if (status_code != WMM_ADDTS_STATUS_ADMISSION_ACCEPTED) {
6985b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "WMM AC: ADDTS request was rejected");
6995b9c547cSRui Paulo 		goto err_msg;
7005b9c547cSRui Paulo 	}
7015b9c547cSRui Paulo 
7025b9c547cSRui Paulo 	replace_tspecs = wmm_ac_should_replace_ts(wpa_s, tsid, ac, dir);
7035b9c547cSRui Paulo 	if (replace_tspecs < 0)
7045b9c547cSRui Paulo 		goto err_delts;
7055b9c547cSRui Paulo 
7065b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "ts idx replace bitmap: 0x%x", replace_tspecs);
7075b9c547cSRui Paulo 
7085b9c547cSRui Paulo 	/* when replacing tspecs - delete first */
7095b9c547cSRui Paulo 	wmm_ac_del_ts(wpa_s, ac, replace_tspecs);
7105b9c547cSRui Paulo 
7115b9c547cSRui Paulo 	/* Creating a new traffic stream */
7125b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG,
7135b9c547cSRui Paulo 		   "WMM AC: adding a new TS with TSID=%u address="MACSTR
7145b9c547cSRui Paulo 		   " medium time=%u access category=%d dir=%d ",
7155b9c547cSRui Paulo 		   tsid, MAC2STR(sa),
7165b9c547cSRui Paulo 		   le_to_host16(tspec->medium_time), ac, dir);
7175b9c547cSRui Paulo 
7185b9c547cSRui Paulo 	if (wmm_ac_add_ts(wpa_s, sa, tspec))
7195b9c547cSRui Paulo 		goto err_delts;
7205b9c547cSRui Paulo 
7215b9c547cSRui Paulo 	return;
7225b9c547cSRui Paulo 
7235b9c547cSRui Paulo err_delts:
7245b9c547cSRui Paulo 	/* ask the ap to delete the tspec */
7255b9c547cSRui Paulo 	wmm_ac_send_delts(wpa_s, tspec, sa);
7265b9c547cSRui Paulo err_msg:
7275b9c547cSRui Paulo 	wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_REQ_FAILED "tsid=%u",
7285b9c547cSRui Paulo 		tsid);
7295b9c547cSRui Paulo }
7305b9c547cSRui Paulo 
7315b9c547cSRui Paulo 
wmm_ac_rx_action(struct wpa_supplicant * wpa_s,const u8 * da,const u8 * sa,const u8 * data,size_t len)7325b9c547cSRui Paulo void wmm_ac_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7335b9c547cSRui Paulo 			const u8 *sa, const u8 *data, size_t len)
7345b9c547cSRui Paulo {
7355b9c547cSRui Paulo 	u8 action;
7365b9c547cSRui Paulo 	u8 dialog_token;
7375b9c547cSRui Paulo 	u8 status_code;
7385b9c547cSRui Paulo 	struct ieee802_11_elems elems;
7395b9c547cSRui Paulo 	struct wmm_tspec_element *tspec;
7405b9c547cSRui Paulo 
7415b9c547cSRui Paulo 	if (wpa_s->wmm_ac_assoc_info == NULL) {
7425b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
7435b9c547cSRui Paulo 			   "WMM AC: WMM AC is disabled, ignoring action frame");
7445b9c547cSRui Paulo 		return;
7455b9c547cSRui Paulo 	}
7465b9c547cSRui Paulo 
7475b9c547cSRui Paulo 	action = data[0];
7485b9c547cSRui Paulo 
7495b9c547cSRui Paulo 	if (action != WMM_ACTION_CODE_ADDTS_RESP &&
7505b9c547cSRui Paulo 	    action != WMM_ACTION_CODE_DELTS) {
7515b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
7525b9c547cSRui Paulo 			   "WMM AC: Unknown action (%d), ignoring action frame",
7535b9c547cSRui Paulo 			   action);
7545b9c547cSRui Paulo 		return;
7555b9c547cSRui Paulo 	}
7565b9c547cSRui Paulo 
7575b9c547cSRui Paulo 	/* WMM AC action frame */
758*a90b9d01SCy Schubert 	if (!ether_addr_equal(da, wpa_s->own_addr)) {
7595b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WMM AC: frame destination addr="MACSTR
7605b9c547cSRui Paulo 			   " is other than ours, ignoring frame", MAC2STR(da));
7615b9c547cSRui Paulo 		return;
7625b9c547cSRui Paulo 	}
7635b9c547cSRui Paulo 
764*a90b9d01SCy Schubert 	if (!ether_addr_equal(sa, wpa_s->bssid)) {
7655b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WMM AC: ignore frame with sa " MACSTR
7665b9c547cSRui Paulo 			   " different other than our bssid", MAC2STR(da));
7675b9c547cSRui Paulo 		return;
7685b9c547cSRui Paulo 	}
7695b9c547cSRui Paulo 
7705b9c547cSRui Paulo 	if (len < 2 + sizeof(struct wmm_tspec_element)) {
7715b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
7725b9c547cSRui Paulo 			   "WMM AC: Short ADDTS response ignored (len=%lu)",
7735b9c547cSRui Paulo 			   (unsigned long) len);
7745b9c547cSRui Paulo 		return;
7755b9c547cSRui Paulo 	}
7765b9c547cSRui Paulo 
7775b9c547cSRui Paulo 	data++;
7785b9c547cSRui Paulo 	len--;
7795b9c547cSRui Paulo 	dialog_token = data[0];
7805b9c547cSRui Paulo 	status_code = data[1];
7815b9c547cSRui Paulo 
7825b9c547cSRui Paulo 	if (ieee802_11_parse_elems(data + 2, len - 2, &elems, 1) != ParseOK) {
7835b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
7845b9c547cSRui Paulo 			   "WMM AC: Could not parse WMM AC action from " MACSTR,
7855b9c547cSRui Paulo 			   MAC2STR(sa));
7865b9c547cSRui Paulo 		return;
7875b9c547cSRui Paulo 	}
7885b9c547cSRui Paulo 
7895b9c547cSRui Paulo 	/* the struct also contains the type and value, so decrease it */
7905b9c547cSRui Paulo 	if (elems.wmm_tspec_len != sizeof(struct wmm_tspec_element) - 2) {
7915b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WMM AC: missing or wrong length TSPEC");
7925b9c547cSRui Paulo 		return;
7935b9c547cSRui Paulo 	}
7945b9c547cSRui Paulo 
7955b9c547cSRui Paulo 	tspec = (struct wmm_tspec_element *)(elems.wmm_tspec - 2);
7965b9c547cSRui Paulo 
7975b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WMM AC: RX WMM AC Action from " MACSTR,
7985b9c547cSRui Paulo 		   MAC2STR(sa));
7995b9c547cSRui Paulo 	wpa_hexdump(MSG_MSGDUMP, "WMM AC: WMM AC Action content", data, len);
8005b9c547cSRui Paulo 
8015b9c547cSRui Paulo 	switch (action) {
8025b9c547cSRui Paulo 	case WMM_ACTION_CODE_ADDTS_RESP:
8035b9c547cSRui Paulo 		wmm_ac_handle_addts_resp(wpa_s, sa, dialog_token, status_code,
8045b9c547cSRui Paulo 					 tspec);
8055b9c547cSRui Paulo 		break;
8065b9c547cSRui Paulo 	case WMM_ACTION_CODE_DELTS:
8075b9c547cSRui Paulo 		wmm_ac_handle_delts(wpa_s, sa, tspec);
8085b9c547cSRui Paulo 		break;
8095b9c547cSRui Paulo 	default:
8105b9c547cSRui Paulo 		break;
8115b9c547cSRui Paulo 	}
8125b9c547cSRui Paulo }
8135b9c547cSRui Paulo 
8145b9c547cSRui Paulo 
get_ac_str(u8 ac)8155b9c547cSRui Paulo static const char * get_ac_str(u8 ac)
8165b9c547cSRui Paulo {
8175b9c547cSRui Paulo 	switch (ac) {
8185b9c547cSRui Paulo 	case WMM_AC_BE:
8195b9c547cSRui Paulo 		return "BE";
8205b9c547cSRui Paulo 	case WMM_AC_BK:
8215b9c547cSRui Paulo 		return "BK";
8225b9c547cSRui Paulo 	case WMM_AC_VI:
8235b9c547cSRui Paulo 		return "VI";
8245b9c547cSRui Paulo 	case WMM_AC_VO:
8255b9c547cSRui Paulo 		return "VO";
8265b9c547cSRui Paulo 	default:
8275b9c547cSRui Paulo 		return "N/A";
8285b9c547cSRui Paulo 	}
8295b9c547cSRui Paulo }
8305b9c547cSRui Paulo 
8315b9c547cSRui Paulo 
get_direction_str(u8 direction)8325b9c547cSRui Paulo static const char * get_direction_str(u8 direction)
8335b9c547cSRui Paulo {
8345b9c547cSRui Paulo 	switch (direction) {
8355b9c547cSRui Paulo 	case WMM_AC_DIR_DOWNLINK:
8365b9c547cSRui Paulo 		return "Downlink";
8375b9c547cSRui Paulo 	case WMM_AC_DIR_UPLINK:
8385b9c547cSRui Paulo 		return "Uplink";
8395b9c547cSRui Paulo 	case WMM_AC_DIR_BIDIRECTIONAL:
8405b9c547cSRui Paulo 		return "Bi-directional";
8415b9c547cSRui Paulo 	default:
8425b9c547cSRui Paulo 		return "N/A";
8435b9c547cSRui Paulo 	}
8445b9c547cSRui Paulo }
8455b9c547cSRui Paulo 
8465b9c547cSRui Paulo 
wpas_wmm_ac_status(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8475b9c547cSRui Paulo int wpas_wmm_ac_status(struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
8485b9c547cSRui Paulo {
8495b9c547cSRui Paulo 	struct wmm_ac_assoc_data *assoc_info = wpa_s->wmm_ac_assoc_info;
8505b9c547cSRui Paulo 	enum ts_dir_idx idx;
8515b9c547cSRui Paulo 	int pos = 0;
8525b9c547cSRui Paulo 	u8 ac, up;
8535b9c547cSRui Paulo 
8545b9c547cSRui Paulo 	if (!assoc_info) {
8555b9c547cSRui Paulo 		return wpa_scnprintf(buf, buflen - pos,
8565b9c547cSRui Paulo 				     "Not associated to a WMM AP, WMM AC is Disabled\n");
8575b9c547cSRui Paulo 	}
8585b9c547cSRui Paulo 
8595b9c547cSRui Paulo 	pos += wpa_scnprintf(buf + pos, buflen - pos, "WMM AC is Enabled\n");
8605b9c547cSRui Paulo 
8615b9c547cSRui Paulo 	for (ac = 0; ac < WMM_AC_NUM; ac++) {
8625b9c547cSRui Paulo 		int ts_count = 0;
8635b9c547cSRui Paulo 
8645b9c547cSRui Paulo 		pos += wpa_scnprintf(buf + pos, buflen - pos,
8655b9c547cSRui Paulo 				     "%s: acm=%d uapsd=%d\n",
8665b9c547cSRui Paulo 				     get_ac_str(ac),
8675b9c547cSRui Paulo 				     assoc_info->ac_params[ac].acm,
8685b9c547cSRui Paulo 				     assoc_info->ac_params[ac].uapsd);
8695b9c547cSRui Paulo 
8705b9c547cSRui Paulo 		for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
8715b9c547cSRui Paulo 			struct wmm_tspec_element *tspec;
8725b9c547cSRui Paulo 			u8 dir, tsid;
8735b9c547cSRui Paulo 			const char *dir_str;
8745b9c547cSRui Paulo 
8755b9c547cSRui Paulo 			tspec = wpa_s->tspecs[ac][idx];
8765b9c547cSRui Paulo 			if (!tspec)
8775b9c547cSRui Paulo 				continue;
8785b9c547cSRui Paulo 
8795b9c547cSRui Paulo 			ts_count++;
8805b9c547cSRui Paulo 
8815b9c547cSRui Paulo 			dir = wmm_ac_get_direction(tspec);
8825b9c547cSRui Paulo 			dir_str = get_direction_str(dir);
8835b9c547cSRui Paulo 			tsid = wmm_ac_get_tsid(tspec);
8845b9c547cSRui Paulo 			up = wmm_ac_get_user_priority(tspec);
8855b9c547cSRui Paulo 
8865b9c547cSRui Paulo 			pos += wpa_scnprintf(buf + pos, buflen - pos,
8875b9c547cSRui Paulo 					     "\tTSID=%u UP=%u\n"
8885b9c547cSRui Paulo 					     "\tAddress = "MACSTR"\n"
8895b9c547cSRui Paulo 					     "\tWMM AC dir = %s\n"
8905b9c547cSRui Paulo 					     "\tTotal admitted time = %u\n\n",
8915b9c547cSRui Paulo 					     tsid, up,
8925b9c547cSRui Paulo 					     MAC2STR(wpa_s->bssid),
8935b9c547cSRui Paulo 					     dir_str,
8945b9c547cSRui Paulo 					     le_to_host16(tspec->medium_time));
8955b9c547cSRui Paulo 		}
8965b9c547cSRui Paulo 
8975b9c547cSRui Paulo 		if (!ts_count) {
8985b9c547cSRui Paulo 			pos += wpa_scnprintf(buf + pos, buflen - pos,
8995b9c547cSRui Paulo 					     "\t(No Traffic Stream)\n\n");
9005b9c547cSRui Paulo 		}
9015b9c547cSRui Paulo 	}
9025b9c547cSRui Paulo 
9035b9c547cSRui Paulo 	return pos;
9045b9c547cSRui Paulo }
9055b9c547cSRui Paulo 
9065b9c547cSRui Paulo 
wmm_ac_get_tspecs_count(struct wpa_supplicant * wpa_s)9075b9c547cSRui Paulo static u8 wmm_ac_get_tspecs_count(struct wpa_supplicant *wpa_s)
9085b9c547cSRui Paulo {
9095b9c547cSRui Paulo 	int ac, dir, tspecs_count = 0;
9105b9c547cSRui Paulo 
9115b9c547cSRui Paulo 	for (ac = 0; ac < WMM_AC_NUM; ac++) {
9125b9c547cSRui Paulo 		for (dir = 0; dir < TS_DIR_IDX_COUNT; dir++) {
9135b9c547cSRui Paulo 			if (wpa_s->tspecs[ac][dir])
9145b9c547cSRui Paulo 				tspecs_count++;
9155b9c547cSRui Paulo 		}
9165b9c547cSRui Paulo 	}
9175b9c547cSRui Paulo 
9185b9c547cSRui Paulo 	return tspecs_count;
9195b9c547cSRui Paulo }
9205b9c547cSRui Paulo 
9215b9c547cSRui Paulo 
wmm_ac_save_tspecs(struct wpa_supplicant * wpa_s)9225b9c547cSRui Paulo void wmm_ac_save_tspecs(struct wpa_supplicant *wpa_s)
9235b9c547cSRui Paulo {
9245b9c547cSRui Paulo 	int ac, dir, tspecs_count;
9255b9c547cSRui Paulo 
9265b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WMM AC: Save last configured tspecs");
9275b9c547cSRui Paulo 
9285b9c547cSRui Paulo 	if (!wpa_s->wmm_ac_assoc_info)
9295b9c547cSRui Paulo 		return;
9305b9c547cSRui Paulo 
9315b9c547cSRui Paulo 	tspecs_count = wmm_ac_get_tspecs_count(wpa_s);
9325b9c547cSRui Paulo 	if (!tspecs_count) {
9335b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WMM AC: No configured TSPECs");
9345b9c547cSRui Paulo 		return;
9355b9c547cSRui Paulo 	}
9365b9c547cSRui Paulo 
9375b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WMM AC: Saving tspecs");
9385b9c547cSRui Paulo 
9395b9c547cSRui Paulo 	wmm_ac_clear_saved_tspecs(wpa_s);
9405b9c547cSRui Paulo 	wpa_s->last_tspecs = os_calloc(tspecs_count,
9415b9c547cSRui Paulo 				       sizeof(*wpa_s->last_tspecs));
9425b9c547cSRui Paulo 	if (!wpa_s->last_tspecs) {
9435b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "WMM AC: Failed to save tspecs!");
9445b9c547cSRui Paulo 		return;
9455b9c547cSRui Paulo 	}
9465b9c547cSRui Paulo 
9475b9c547cSRui Paulo 	for (ac = 0; ac < WMM_AC_NUM; ac++) {
9485b9c547cSRui Paulo 		for (dir = 0; dir < TS_DIR_IDX_COUNT; dir++) {
9495b9c547cSRui Paulo 			if (!wpa_s->tspecs[ac][dir])
9505b9c547cSRui Paulo 				continue;
9515b9c547cSRui Paulo 
9525b9c547cSRui Paulo 			wpa_s->last_tspecs[wpa_s->last_tspecs_count++] =
9535b9c547cSRui Paulo 				*wpa_s->tspecs[ac][dir];
9545b9c547cSRui Paulo 		}
9555b9c547cSRui Paulo 	}
9565b9c547cSRui Paulo 
9575b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WMM AC: Successfully saved %d TSPECs",
9585b9c547cSRui Paulo 		   wpa_s->last_tspecs_count);
9595b9c547cSRui Paulo }
9605b9c547cSRui Paulo 
9615b9c547cSRui Paulo 
wmm_ac_clear_saved_tspecs(struct wpa_supplicant * wpa_s)9625b9c547cSRui Paulo void wmm_ac_clear_saved_tspecs(struct wpa_supplicant *wpa_s)
9635b9c547cSRui Paulo {
9645b9c547cSRui Paulo 	if (wpa_s->last_tspecs) {
9655b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "WMM AC: Clear saved tspecs");
9665b9c547cSRui Paulo 		os_free(wpa_s->last_tspecs);
9675b9c547cSRui Paulo 		wpa_s->last_tspecs = NULL;
9685b9c547cSRui Paulo 		wpa_s->last_tspecs_count = 0;
9695b9c547cSRui Paulo 	}
9705b9c547cSRui Paulo }
9715b9c547cSRui Paulo 
9725b9c547cSRui Paulo 
wmm_ac_restore_tspecs(struct wpa_supplicant * wpa_s)9735b9c547cSRui Paulo int wmm_ac_restore_tspecs(struct wpa_supplicant *wpa_s)
9745b9c547cSRui Paulo {
9755b9c547cSRui Paulo 	unsigned int i;
9765b9c547cSRui Paulo 
9775b9c547cSRui Paulo 	if (!wpa_s->wmm_ac_assoc_info || !wpa_s->last_tspecs_count)
9785b9c547cSRui Paulo 		return 0;
9795b9c547cSRui Paulo 
9805b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "WMM AC: Restore %u saved tspecs",
9815b9c547cSRui Paulo 		   wpa_s->last_tspecs_count);
9825b9c547cSRui Paulo 
9835b9c547cSRui Paulo 	for (i = 0; i < wpa_s->last_tspecs_count; i++)
9845b9c547cSRui Paulo 		wmm_ac_add_ts(wpa_s, wpa_s->bssid, &wpa_s->last_tspecs[i]);
9855b9c547cSRui Paulo 
9865b9c547cSRui Paulo 	return 0;
9875b9c547cSRui Paulo }
988