1 /*
2  * Wi-Fi Multimedia Admission Control (WMM-AC)
3  * Copyright(c) 2014, Intel Mobile Communication GmbH.
4  * Copyright(c) 2014, Intel Corporation. All rights reserved.
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9 
10 #include "includes.h"
11 
12 #include "utils/common.h"
13 #include "utils/list.h"
14 #include "utils/eloop.h"
15 #include "common/ieee802_11_common.h"
16 #include "wpa_supplicant_i.h"
17 #include "bss.h"
18 #include "driver_i.h"
19 #include "wmm_ac.h"
20 
21 static void wmm_ac_addts_req_timeout(void *eloop_ctx, void *timeout_ctx);
22 
23 static const enum wmm_ac up_to_ac[8] = {
24 	WMM_AC_BK,
25 	WMM_AC_BE,
26 	WMM_AC_BE,
27 	WMM_AC_BK,
28 	WMM_AC_VI,
29 	WMM_AC_VI,
30 	WMM_AC_VO,
31 	WMM_AC_VO
32 };
33 
34 
wmm_ac_get_tsid(const struct wmm_tspec_element * tspec)35 static inline u8 wmm_ac_get_tsid(const struct wmm_tspec_element *tspec)
36 {
37 	return (tspec->ts_info[0] >> 1) & 0x0f;
38 }
39 
40 
wmm_ac_get_direction(const struct wmm_tspec_element * tspec)41 static u8 wmm_ac_get_direction(const struct wmm_tspec_element *tspec)
42 {
43 	return (tspec->ts_info[0] >> 5) & 0x03;
44 }
45 
46 
wmm_ac_get_user_priority(const struct wmm_tspec_element * tspec)47 static u8 wmm_ac_get_user_priority(const struct wmm_tspec_element *tspec)
48 {
49 	return (tspec->ts_info[1] >> 3) & 0x07;
50 }
51 
52 
wmm_ac_direction_to_idx(u8 direction)53 static u8 wmm_ac_direction_to_idx(u8 direction)
54 {
55 	switch (direction) {
56 	case WMM_AC_DIR_UPLINK:
57 		return TS_DIR_IDX_UPLINK;
58 	case WMM_AC_DIR_DOWNLINK:
59 		return TS_DIR_IDX_DOWNLINK;
60 	case WMM_AC_DIR_BIDIRECTIONAL:
61 		return TS_DIR_IDX_BIDI;
62 	default:
63 		wpa_printf(MSG_ERROR, "Invalid direction: %d", direction);
64 		return WMM_AC_DIR_UPLINK;
65 	}
66 }
67 
68 
wmm_ac_add_ts(struct wpa_supplicant * wpa_s,const u8 * addr,const struct wmm_tspec_element * tspec)69 static int wmm_ac_add_ts(struct wpa_supplicant *wpa_s, const u8 *addr,
70 			 const struct wmm_tspec_element *tspec)
71 {
72 	struct wmm_tspec_element *_tspec;
73 	int ret;
74 	u16 admitted_time = le_to_host16(tspec->medium_time);
75 	u8 up = wmm_ac_get_user_priority(tspec);
76 	u8 ac = up_to_ac[up];
77 	u8 dir = wmm_ac_get_direction(tspec);
78 	u8 tsid = wmm_ac_get_tsid(tspec);
79 	enum ts_dir_idx idx = wmm_ac_direction_to_idx(dir);
80 
81 	/* should have been verified before, but double-check here */
82 	if (wpa_s->tspecs[ac][idx]) {
83 		wpa_printf(MSG_ERROR,
84 			   "WMM AC: tspec (ac=%d, dir=%d) already exists!",
85 			   ac, dir);
86 		return -1;
87 	}
88 
89 	/* copy tspec */
90 	_tspec = os_memdup(tspec, sizeof(*_tspec));
91 	if (!_tspec)
92 		return -1;
93 
94 	if (dir != WMM_AC_DIR_DOWNLINK) {
95 		ret = wpa_drv_add_ts(wpa_s, tsid, addr, up, admitted_time);
96 		wpa_printf(MSG_DEBUG,
97 			   "WMM AC: Add TS: addr=" MACSTR
98 			   " TSID=%u admitted time=%u, ret=%d",
99 			   MAC2STR(addr), tsid, admitted_time, ret);
100 		if (ret < 0) {
101 			os_free(_tspec);
102 			return -1;
103 		}
104 	}
105 
106 	wpa_s->tspecs[ac][idx] = _tspec;
107 
108 	wpa_printf(MSG_DEBUG, "Traffic stream was created successfully");
109 
110 	wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_ADDED
111 		"tsid=%d addr=" MACSTR " admitted_time=%d",
112 		tsid, MAC2STR(addr), admitted_time);
113 
114 	return 0;
115 }
116 
117 
wmm_ac_del_ts_idx(struct wpa_supplicant * wpa_s,u8 ac,enum ts_dir_idx dir)118 static void wmm_ac_del_ts_idx(struct wpa_supplicant *wpa_s, u8 ac,
119 			      enum ts_dir_idx dir)
120 {
121 	struct wmm_tspec_element *tspec = wpa_s->tspecs[ac][dir];
122 	u8 tsid;
123 
124 	if (!tspec)
125 		return;
126 
127 	tsid = wmm_ac_get_tsid(tspec);
128 	wpa_printf(MSG_DEBUG, "WMM AC: Del TS ac=%d tsid=%d", ac, tsid);
129 
130 	/* update the driver in case of uplink/bidi */
131 	if (wmm_ac_get_direction(tspec) != WMM_AC_DIR_DOWNLINK)
132 		wpa_drv_del_ts(wpa_s, tsid, wpa_s->bssid);
133 
134 	wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_REMOVED
135 		"tsid=%d addr=" MACSTR, tsid, MAC2STR(wpa_s->bssid));
136 
137 	os_free(wpa_s->tspecs[ac][dir]);
138 	wpa_s->tspecs[ac][dir] = NULL;
139 }
140 
141 
wmm_ac_del_req(struct wpa_supplicant * wpa_s,int failed)142 static void wmm_ac_del_req(struct wpa_supplicant *wpa_s, int failed)
143 {
144 	struct wmm_ac_addts_request *req = wpa_s->addts_request;
145 
146 	if (!req)
147 		return;
148 
149 	if (failed)
150 		wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_REQ_FAILED
151 			"tsid=%u", wmm_ac_get_tsid(&req->tspec));
152 
153 	eloop_cancel_timeout(wmm_ac_addts_req_timeout, wpa_s, req);
154 	wpa_s->addts_request = NULL;
155 	os_free(req);
156 }
157 
158 
wmm_ac_addts_req_timeout(void * eloop_ctx,void * timeout_ctx)159 static void wmm_ac_addts_req_timeout(void *eloop_ctx, void *timeout_ctx)
160 {
161 	struct wpa_supplicant *wpa_s = eloop_ctx;
162 #ifndef __ZEPHYR__
163 	struct wmm_ac_addts_request *addts_req = timeout_ctx;
164 
165 	wpa_printf(MSG_DEBUG,
166 		   "Timeout getting ADDTS response (tsid=%d up=%d)",
167 		   wmm_ac_get_tsid(&addts_req->tspec),
168 		   wmm_ac_get_user_priority(&addts_req->tspec));
169 #endif /* __ZEPHYR__ */
170 
171 	wmm_ac_del_req(wpa_s, 1);
172 }
173 
174 
wmm_ac_send_addts_request(struct wpa_supplicant * wpa_s,const struct wmm_ac_addts_request * req)175 static int wmm_ac_send_addts_request(struct wpa_supplicant *wpa_s,
176 				     const struct wmm_ac_addts_request *req)
177 {
178 	struct wpabuf *buf;
179 	int ret;
180 
181 	wpa_printf(MSG_DEBUG, "Sending ADDTS Request to " MACSTR,
182 		   MAC2STR(req->address));
183 
184 	/* category + action code + dialog token + status + sizeof(tspec) */
185 	buf = wpabuf_alloc(4 + sizeof(req->tspec));
186 	if (!buf) {
187 		wpa_printf(MSG_ERROR, "WMM AC: Allocation error");
188 		return -1;
189 	}
190 
191 	wpabuf_put_u8(buf, WLAN_ACTION_WMM);
192 	wpabuf_put_u8(buf, WMM_ACTION_CODE_ADDTS_REQ);
193 	wpabuf_put_u8(buf, req->dialog_token);
194 	wpabuf_put_u8(buf, 0); /* status code */
195 	wpabuf_put_data(buf, &req->tspec, sizeof(req->tspec));
196 
197 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, req->address,
198 				wpa_s->own_addr, wpa_s->bssid,
199 				wpabuf_head(buf), wpabuf_len(buf), 0);
200 	if (ret) {
201 		wpa_printf(MSG_WARNING,
202 			   "WMM AC: Failed to send ADDTS Request");
203 	}
204 
205 	wpabuf_free(buf);
206 	return ret;
207 }
208 
209 
wmm_ac_send_delts(struct wpa_supplicant * wpa_s,const struct wmm_tspec_element * tspec,const u8 * address)210 static int wmm_ac_send_delts(struct wpa_supplicant *wpa_s,
211 			     const struct wmm_tspec_element *tspec,
212 			     const u8 *address)
213 {
214 	struct wpabuf *buf;
215 	int ret;
216 
217 	/* category + action code + dialog token + status + sizeof(tspec) */
218 	buf = wpabuf_alloc(4 + sizeof(*tspec));
219 	if (!buf)
220 		return -1;
221 
222 	wpa_printf(MSG_DEBUG, "Sending DELTS to " MACSTR, MAC2STR(address));
223 
224 	/* category + action code + dialog token + status + sizeof(tspec) */
225 	wpabuf_put_u8(buf, WLAN_ACTION_WMM);
226 	wpabuf_put_u8(buf, WMM_ACTION_CODE_DELTS);
227 	wpabuf_put_u8(buf, 0); /* Dialog Token (not used) */
228 	wpabuf_put_u8(buf, 0); /* Status Code (not used) */
229 	wpabuf_put_data(buf, tspec, sizeof(*tspec));
230 
231 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, address,
232 				  wpa_s->own_addr, wpa_s->bssid,
233 				  wpabuf_head(buf), wpabuf_len(buf), 0);
234 	if (ret)
235 		wpa_printf(MSG_WARNING, "Failed to send DELTS frame");
236 
237 	wpabuf_free(buf);
238 	return ret;
239 }
240 
241 
242 /* return the AC using the given TSPEC tid */
wmm_ac_find_tsid(struct wpa_supplicant * wpa_s,u8 tsid,enum ts_dir_idx * dir)243 static int wmm_ac_find_tsid(struct wpa_supplicant *wpa_s, u8 tsid,
244 			    enum ts_dir_idx *dir)
245 {
246 	int ac;
247 	enum ts_dir_idx idx;
248 
249 	for (ac = 0; ac < WMM_AC_NUM; ac++) {
250 		for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
251 			if (wpa_s->tspecs[ac][idx] &&
252 			    wmm_ac_get_tsid(wpa_s->tspecs[ac][idx]) == tsid) {
253 				if (dir)
254 					*dir = idx;
255 				return ac;
256 			}
257 		}
258 	}
259 
260 	return -1;
261 }
262 
263 
264 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)265 wmm_ac_build_addts_req(struct wpa_supplicant *wpa_s,
266 		       const struct wmm_ac_ts_setup_params *params,
267 		       const u8 *address)
268 {
269 	struct wmm_ac_addts_request *addts_req;
270 	struct wmm_tspec_element *tspec;
271 	u8 ac = up_to_ac[params->user_priority];
272 	u8 uapsd = wpa_s->wmm_ac_assoc_info->ac_params[ac].uapsd;
273 
274 	addts_req = os_zalloc(sizeof(*addts_req));
275 	if (!addts_req)
276 		return NULL;
277 
278 	tspec = &addts_req->tspec;
279 	os_memcpy(addts_req->address, address, ETH_ALEN);
280 
281 	/* The dialog token cannot be zero */
282 	if (++wpa_s->wmm_ac_last_dialog_token == 0)
283 		wpa_s->wmm_ac_last_dialog_token++;
284 
285 	addts_req->dialog_token = wpa_s->wmm_ac_last_dialog_token;
286 	tspec->eid = WLAN_EID_VENDOR_SPECIFIC;
287 	tspec->length = sizeof(*tspec) - 2; /* reduce eid and length */
288 	tspec->oui[0] = 0x00;
289 	tspec->oui[1] = 0x50;
290 	tspec->oui[2] = 0xf2;
291 	tspec->oui_type = WMM_OUI_TYPE;
292 	tspec->oui_subtype = WMM_OUI_SUBTYPE_TSPEC_ELEMENT;
293 	tspec->version = WMM_VERSION;
294 
295 	tspec->ts_info[0] = params->tsid << 1;
296 	tspec->ts_info[0] |= params->direction << 5;
297 	tspec->ts_info[0] |= WMM_AC_ACCESS_POLICY_EDCA << 7;
298 	tspec->ts_info[1] = uapsd << 2;
299 	tspec->ts_info[1] |= params->user_priority << 3;
300 	tspec->ts_info[2] = 0;
301 
302 	tspec->nominal_msdu_size = host_to_le16(params->nominal_msdu_size);
303 	if (params->fixed_nominal_msdu)
304 		tspec->nominal_msdu_size |=
305 			host_to_le16(WMM_AC_FIXED_MSDU_SIZE);
306 
307 	tspec->mean_data_rate = host_to_le32(params->mean_data_rate);
308 	tspec->minimum_phy_rate = host_to_le32(params->minimum_phy_rate);
309 	tspec->surplus_bandwidth_allowance =
310 		host_to_le16(params->surplus_bandwidth_allowance);
311 
312 	return addts_req;
313 }
314 
315 
param_in_range(const char * name,long value,long min_val,long max_val)316 static int param_in_range(const char *name, long value,
317 			  long min_val, long max_val)
318 {
319 	if (value < min_val || (max_val >= 0 && value > max_val)) {
320 		wpa_printf(MSG_DEBUG,
321 			   "WMM AC: param %s (%ld) is out of range (%ld-%ld)",
322 			   name, value, min_val, max_val);
323 		return 0;
324 	}
325 
326 	return 1;
327 }
328 
329 
wmm_ac_should_replace_ts(struct wpa_supplicant * wpa_s,u8 tsid,u8 ac,u8 dir)330 static int wmm_ac_should_replace_ts(struct wpa_supplicant *wpa_s,
331 				    u8 tsid, u8 ac, u8 dir)
332 {
333 	enum ts_dir_idx idx;
334 	int cur_ac, existing_ts = 0, replace_ts = 0;
335 
336 	cur_ac = wmm_ac_find_tsid(wpa_s, tsid, &idx);
337 	if (cur_ac >= 0) {
338 		if (cur_ac != ac) {
339 			wpa_printf(MSG_DEBUG,
340 				   "WMM AC: TSID %i already exists on different ac (%d)",
341 				   tsid, cur_ac);
342 			return -1;
343 		}
344 
345 		/* same tsid - this tspec will replace the current one */
346 		replace_ts |= BIT(idx);
347 	}
348 
349 	for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
350 		if (wpa_s->tspecs[ac][idx])
351 			existing_ts |= BIT(idx);
352 	}
353 
354 	switch (dir) {
355 	case WMM_AC_DIR_UPLINK:
356 		/* replace existing uplink/bidi tspecs */
357 		replace_ts |= existing_ts & (BIT(TS_DIR_IDX_UPLINK) |
358 					     BIT(TS_DIR_IDX_BIDI));
359 		break;
360 	case WMM_AC_DIR_DOWNLINK:
361 		/* replace existing downlink/bidi tspecs */
362 		replace_ts |= existing_ts & (BIT(TS_DIR_IDX_DOWNLINK) |
363 					     BIT(TS_DIR_IDX_BIDI));
364 		break;
365 	case WMM_AC_DIR_BIDIRECTIONAL:
366 		/* replace all existing tspecs */
367 		replace_ts |= existing_ts;
368 		break;
369 	default:
370 		return -1;
371 	}
372 
373 	return replace_ts;
374 }
375 
376 
wmm_ac_ts_req_is_valid(struct wpa_supplicant * wpa_s,const struct wmm_ac_ts_setup_params * params)377 static int wmm_ac_ts_req_is_valid(struct wpa_supplicant *wpa_s,
378 				  const struct wmm_ac_ts_setup_params *params)
379 {
380 	enum wmm_ac req_ac;
381 
382 #define PARAM_IN_RANGE(field, min_value, max_value) \
383 	param_in_range(#field, params->field, min_value, max_value)
384 
385 	if (!PARAM_IN_RANGE(tsid, 0, WMM_AC_MAX_TID) ||
386 	    !PARAM_IN_RANGE(user_priority, 0, WMM_AC_MAX_USER_PRIORITY) ||
387 	    !PARAM_IN_RANGE(nominal_msdu_size, 1, WMM_AC_MAX_NOMINAL_MSDU) ||
388 	    !PARAM_IN_RANGE(mean_data_rate, 1, -1) ||
389 	    !PARAM_IN_RANGE(minimum_phy_rate, 1, -1) ||
390 	    !PARAM_IN_RANGE(surplus_bandwidth_allowance, WMM_AC_MIN_SBA_UNITY,
391 			    -1))
392 		return 0;
393 #undef PARAM_IN_RANGE
394 
395 	if (!(params->direction == WMM_TSPEC_DIRECTION_UPLINK ||
396 	      params->direction == WMM_TSPEC_DIRECTION_DOWNLINK ||
397 	      params->direction == WMM_TSPEC_DIRECTION_BI_DIRECTIONAL)) {
398 		wpa_printf(MSG_DEBUG, "WMM AC: invalid TS direction: %d",
399 			   params->direction);
400 		return 0;
401 	}
402 
403 	req_ac = up_to_ac[params->user_priority];
404 
405 	/* Requested access category must have acm */
406 	if (!wpa_s->wmm_ac_assoc_info->ac_params[req_ac].acm) {
407 		wpa_printf(MSG_DEBUG, "WMM AC: AC %d is not ACM", req_ac);
408 		return 0;
409 	}
410 
411 	if (wmm_ac_should_replace_ts(wpa_s, params->tsid, req_ac,
412 				     params->direction) < 0)
413 		return 0;
414 
415 	return 1;
416 }
417 
418 
419 static struct wmm_ac_assoc_data *
wmm_ac_process_param_elem(struct wpa_supplicant * wpa_s,const u8 * ies,size_t ies_len)420 wmm_ac_process_param_elem(struct wpa_supplicant *wpa_s, const u8 *ies,
421 			  size_t ies_len)
422 {
423 	struct ieee802_11_elems elems;
424 	struct wmm_parameter_element *wmm_params;
425 	struct wmm_ac_assoc_data *assoc_data;
426 	int i;
427 
428 	/* Parsing WMM Parameter Element */
429 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
430 		wpa_printf(MSG_DEBUG, "WMM AC: could not parse assoc ies");
431 		return NULL;
432 	}
433 
434 	if (!elems.wmm) {
435 		wpa_printf(MSG_DEBUG, "WMM AC: No WMM IE");
436 		return NULL;
437 	}
438 
439 	if (elems.wmm_len != sizeof(*wmm_params)) {
440 		wpa_printf(MSG_DEBUG, "WMM AC: Invalid WMM ie length");
441 		return NULL;
442 	}
443 
444 	wmm_params = (struct wmm_parameter_element *)(elems.wmm);
445 
446 	assoc_data = os_zalloc(sizeof(*assoc_data));
447 	if (!assoc_data)
448 		return NULL;
449 
450 	for (i = 0; i < WMM_AC_NUM; i++)
451 		assoc_data->ac_params[i].acm =
452 			!!(wmm_params->ac[i].aci_aifsn & WMM_AC_ACM);
453 
454 	wpa_printf(MSG_DEBUG,
455 		   "WMM AC: AC mandatory: AC_BE=%u AC_BK=%u AC_VI=%u AC_VO=%u",
456 		   assoc_data->ac_params[WMM_AC_BE].acm,
457 		   assoc_data->ac_params[WMM_AC_BK].acm,
458 		   assoc_data->ac_params[WMM_AC_VI].acm,
459 		   assoc_data->ac_params[WMM_AC_VO].acm);
460 
461 	return assoc_data;
462 }
463 
464 
wmm_ac_init(struct wpa_supplicant * wpa_s,const u8 * ies,size_t ies_len,const struct wmm_params * wmm_params)465 static int wmm_ac_init(struct wpa_supplicant *wpa_s, const u8 *ies,
466 		       size_t ies_len, const struct wmm_params *wmm_params)
467 {
468 	struct wmm_ac_assoc_data *assoc_data;
469 	u8 ac;
470 
471 	if (wpa_s->wmm_ac_assoc_info) {
472 		wpa_printf(MSG_ERROR, "WMM AC: Already initialized");
473 		return -1;
474 	}
475 
476 	if (!ies || !(wmm_params->info_bitmap & WMM_PARAMS_UAPSD_QUEUES_INFO)) {
477 		/* WMM AC not in use for this connection */
478 		return -1;
479 	}
480 
481 	os_memset(wpa_s->tspecs, 0, sizeof(wpa_s->tspecs));
482 	wpa_s->wmm_ac_last_dialog_token = 0;
483 	wpa_s->addts_request = NULL;
484 
485 	assoc_data = wmm_ac_process_param_elem(wpa_s, ies, ies_len);
486 	if (!assoc_data)
487 		return -1;
488 
489 	wpa_printf(MSG_DEBUG, "WMM AC: U-APSD queues=0x%x",
490 		   wmm_params->uapsd_queues);
491 
492 	for (ac = 0; ac < WMM_AC_NUM; ac++) {
493 		assoc_data->ac_params[ac].uapsd =
494 			!!(wmm_params->uapsd_queues & BIT(ac));
495 	}
496 
497 	wpa_s->wmm_ac_assoc_info = assoc_data;
498 	return 0;
499 }
500 
501 
wmm_ac_del_ts(struct wpa_supplicant * wpa_s,u8 ac,int dir_bitmap)502 static void wmm_ac_del_ts(struct wpa_supplicant *wpa_s, u8 ac, int dir_bitmap)
503 {
504 	enum ts_dir_idx idx;
505 
506 	for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
507 		if (!(dir_bitmap & BIT(idx)))
508 			continue;
509 
510 		wmm_ac_del_ts_idx(wpa_s, ac, idx);
511 	}
512 }
513 
514 
wmm_ac_deinit(struct wpa_supplicant * wpa_s)515 static void wmm_ac_deinit(struct wpa_supplicant *wpa_s)
516 {
517 	int i;
518 
519 	for (i = 0; i < WMM_AC_NUM; i++)
520 		wmm_ac_del_ts(wpa_s, i, TS_DIR_IDX_ALL);
521 
522 	/* delete pending add_ts request */
523 	wmm_ac_del_req(wpa_s, 1);
524 
525 	os_free(wpa_s->wmm_ac_assoc_info);
526 	wpa_s->wmm_ac_assoc_info = NULL;
527 }
528 
529 
wmm_ac_notify_assoc(struct wpa_supplicant * wpa_s,const u8 * ies,size_t ies_len,const struct wmm_params * wmm_params)530 void wmm_ac_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *ies,
531 			 size_t ies_len, const struct wmm_params *wmm_params)
532 {
533 	if (wmm_ac_init(wpa_s, ies, ies_len, wmm_params))
534 		return;
535 
536 	wpa_printf(MSG_DEBUG,
537 		   "WMM AC: Valid WMM association, WMM AC is enabled");
538 }
539 
540 
wmm_ac_notify_disassoc(struct wpa_supplicant * wpa_s)541 void wmm_ac_notify_disassoc(struct wpa_supplicant *wpa_s)
542 {
543 	if (!wpa_s->wmm_ac_assoc_info)
544 		return;
545 
546 	wmm_ac_deinit(wpa_s);
547 	wpa_printf(MSG_DEBUG, "WMM AC: WMM AC is disabled");
548 }
549 
550 
wpas_wmm_ac_delts(struct wpa_supplicant * wpa_s,u8 tsid)551 int wpas_wmm_ac_delts(struct wpa_supplicant *wpa_s, u8 tsid)
552 {
553 	struct wmm_tspec_element tspec;
554 	int ac;
555 	enum ts_dir_idx dir;
556 
557 	if (!wpa_s->wmm_ac_assoc_info) {
558 		wpa_printf(MSG_DEBUG,
559 			   "WMM AC: Failed to delete TS, WMM AC is disabled");
560 		return -1;
561 	}
562 
563 	ac = wmm_ac_find_tsid(wpa_s, tsid, &dir);
564 	if (ac < 0) {
565 		wpa_printf(MSG_DEBUG, "WMM AC: TS does not exist");
566 		return -1;
567 	}
568 
569 	tspec = *wpa_s->tspecs[ac][dir];
570 
571 	wmm_ac_del_ts_idx(wpa_s, ac, dir);
572 
573 	wmm_ac_send_delts(wpa_s, &tspec, wpa_s->bssid);
574 
575 	return 0;
576 }
577 
578 
wpas_wmm_ac_addts(struct wpa_supplicant * wpa_s,struct wmm_ac_ts_setup_params * params)579 int wpas_wmm_ac_addts(struct wpa_supplicant *wpa_s,
580 		      struct wmm_ac_ts_setup_params *params)
581 {
582 	struct wmm_ac_addts_request *addts_req;
583 
584 	if (!wpa_s->wmm_ac_assoc_info) {
585 		wpa_printf(MSG_DEBUG,
586 			   "WMM AC: Cannot add TS - missing assoc data");
587 		return -1;
588 	}
589 
590 	if (wpa_s->addts_request) {
591 		wpa_printf(MSG_DEBUG,
592 			   "WMM AC: can't add TS - ADDTS request is already pending");
593 		return -1;
594 	}
595 
596 	/*
597 	 * we can setup downlink TS even without driver support.
598 	 * however, we need driver support for the other directions.
599 	 */
600 	if (params->direction != WMM_AC_DIR_DOWNLINK &&
601 	    !wpa_s->wmm_ac_supported) {
602 		wpa_printf(MSG_DEBUG,
603 			   "Cannot set uplink/bidi TS without driver support");
604 		return -1;
605 	}
606 
607 	if (!wmm_ac_ts_req_is_valid(wpa_s, params))
608 		return -1;
609 
610 	wpa_printf(MSG_DEBUG, "WMM AC: TS setup request (addr=" MACSTR
611 		   " tsid=%u user priority=%u direction=%d)",
612 		   MAC2STR(wpa_s->bssid), params->tsid,
613 		   params->user_priority, params->direction);
614 
615 	addts_req = wmm_ac_build_addts_req(wpa_s, params, wpa_s->bssid);
616 	if (!addts_req)
617 		return -1;
618 
619 	if (wmm_ac_send_addts_request(wpa_s, addts_req))
620 		goto err;
621 
622 	/* save as pending and set ADDTS resp timeout to 1 second */
623 	wpa_s->addts_request = addts_req;
624 	eloop_register_timeout(1, 0, wmm_ac_addts_req_timeout,
625 			       wpa_s, addts_req);
626 	return 0;
627 err:
628 	os_free(addts_req);
629 	return -1;
630 }
631 
632 
wmm_ac_handle_delts(struct wpa_supplicant * wpa_s,const u8 * sa,const struct wmm_tspec_element * tspec)633 static void wmm_ac_handle_delts(struct wpa_supplicant *wpa_s, const u8 *sa,
634 				const struct wmm_tspec_element *tspec)
635 {
636 	int ac;
637 	u8 tsid;
638 	enum ts_dir_idx idx;
639 
640 	tsid = wmm_ac_get_tsid(tspec);
641 
642 	wpa_printf(MSG_DEBUG,
643 		   "WMM AC: DELTS frame has been received TSID=%u addr="
644 		   MACSTR, tsid, MAC2STR(sa));
645 
646 	ac = wmm_ac_find_tsid(wpa_s, tsid, &idx);
647 	if (ac < 0) {
648 		wpa_printf(MSG_DEBUG,
649 			   "WMM AC: Ignoring DELTS frame - TSID does not exist");
650 		return;
651 	}
652 
653 	wmm_ac_del_ts_idx(wpa_s, ac, idx);
654 
655 	wpa_printf(MSG_DEBUG,
656 		   "TS was deleted successfully (tsid=%u address=" MACSTR ")",
657 		   tsid, MAC2STR(sa));
658 }
659 
660 
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)661 static void wmm_ac_handle_addts_resp(struct wpa_supplicant *wpa_s, const u8 *sa,
662 		const u8 resp_dialog_token, const u8 status_code,
663 		const struct wmm_tspec_element *tspec)
664 {
665 	struct wmm_ac_addts_request *req = wpa_s->addts_request;
666 	u8 ac, tsid, up, dir;
667 	int replace_tspecs;
668 
669 	tsid = wmm_ac_get_tsid(tspec);
670 	dir = wmm_ac_get_direction(tspec);
671 	up = wmm_ac_get_user_priority(tspec);
672 	ac = up_to_ac[up];
673 
674 	/* make sure we have a matching addts request */
675 	if (!req || req->dialog_token != resp_dialog_token) {
676 		wpa_printf(MSG_DEBUG,
677 			   "WMM AC: no req with dialog=%u, ignoring frame",
678 			   resp_dialog_token);
679 		return;
680 	}
681 
682 	/* make sure the params are the same */
683 	if (os_memcmp(req->address, sa, ETH_ALEN) != 0 ||
684 	    tsid != wmm_ac_get_tsid(&req->tspec) ||
685 	    up != wmm_ac_get_user_priority(&req->tspec) ||
686 	    dir != wmm_ac_get_direction(&req->tspec)) {
687 		wpa_printf(MSG_DEBUG,
688 			   "WMM AC: ADDTS params do not match, ignoring frame");
689 		return;
690 	}
691 
692 	/* delete pending request */
693 	wmm_ac_del_req(wpa_s, 0);
694 
695 	wpa_printf(MSG_DEBUG,
696 		   "ADDTS response status=%d tsid=%u up=%u direction=%u",
697 		   status_code, tsid, up, dir);
698 
699 	if (status_code != WMM_ADDTS_STATUS_ADMISSION_ACCEPTED) {
700 		wpa_printf(MSG_INFO, "WMM AC: ADDTS request was rejected");
701 		goto err_msg;
702 	}
703 
704 	replace_tspecs = wmm_ac_should_replace_ts(wpa_s, tsid, ac, dir);
705 	if (replace_tspecs < 0)
706 		goto err_delts;
707 
708 	wpa_printf(MSG_DEBUG, "ts idx replace bitmap: 0x%x", replace_tspecs);
709 
710 	/* when replacing tspecs - delete first */
711 	wmm_ac_del_ts(wpa_s, ac, replace_tspecs);
712 
713 	/* Creating a new traffic stream */
714 	wpa_printf(MSG_DEBUG,
715 		   "WMM AC: adding a new TS with TSID=%u address="MACSTR
716 		   " medium time=%u access category=%d dir=%d ",
717 		   tsid, MAC2STR(sa),
718 		   le_to_host16(tspec->medium_time), ac, dir);
719 
720 	if (wmm_ac_add_ts(wpa_s, sa, tspec))
721 		goto err_delts;
722 
723 	return;
724 
725 err_delts:
726 	/* ask the ap to delete the tspec */
727 	wmm_ac_send_delts(wpa_s, tspec, sa);
728 err_msg:
729 	wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_REQ_FAILED "tsid=%u",
730 		tsid);
731 }
732 
733 
wmm_ac_rx_action(struct wpa_supplicant * wpa_s,const u8 * da,const u8 * sa,const u8 * data,size_t len)734 void wmm_ac_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
735 			const u8 *sa, const u8 *data, size_t len)
736 {
737 	u8 action;
738 	u8 dialog_token;
739 	u8 status_code;
740 	struct ieee802_11_elems elems;
741 	struct wmm_tspec_element *tspec;
742 
743 	if (wpa_s->wmm_ac_assoc_info == NULL) {
744 		wpa_printf(MSG_DEBUG,
745 			   "WMM AC: WMM AC is disabled, ignoring action frame");
746 		return;
747 	}
748 
749 	action = data[0];
750 
751 	if (action != WMM_ACTION_CODE_ADDTS_RESP &&
752 	    action != WMM_ACTION_CODE_DELTS) {
753 		wpa_printf(MSG_DEBUG,
754 			   "WMM AC: Unknown action (%d), ignoring action frame",
755 			   action);
756 		return;
757 	}
758 
759 	/* WMM AC action frame */
760 	if (os_memcmp(da, wpa_s->own_addr, ETH_ALEN) != 0) {
761 		wpa_printf(MSG_DEBUG, "WMM AC: frame destination addr="MACSTR
762 			   " is other than ours, ignoring frame", MAC2STR(da));
763 		return;
764 	}
765 
766 	if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
767 		wpa_printf(MSG_DEBUG, "WMM AC: ignore frame with sa " MACSTR
768 			   " different other than our bssid", MAC2STR(da));
769 		return;
770 	}
771 
772 	if (len < 2 + sizeof(struct wmm_tspec_element)) {
773 		wpa_printf(MSG_DEBUG,
774 			   "WMM AC: Short ADDTS response ignored (len=%lu)",
775 			   (unsigned long) len);
776 		return;
777 	}
778 
779 	data++;
780 	len--;
781 	dialog_token = data[0];
782 	status_code = data[1];
783 
784 	if (ieee802_11_parse_elems(data + 2, len - 2, &elems, 1) != ParseOK) {
785 		wpa_printf(MSG_DEBUG,
786 			   "WMM AC: Could not parse WMM AC action from " MACSTR,
787 			   MAC2STR(sa));
788 		return;
789 	}
790 
791 	/* the struct also contains the type and value, so decrease it */
792 	if (elems.wmm_tspec_len != sizeof(struct wmm_tspec_element) - 2) {
793 		wpa_printf(MSG_DEBUG, "WMM AC: missing or wrong length TSPEC");
794 		return;
795 	}
796 
797 	tspec = (struct wmm_tspec_element *)(elems.wmm_tspec - 2);
798 
799 	wpa_printf(MSG_DEBUG, "WMM AC: RX WMM AC Action from " MACSTR,
800 		   MAC2STR(sa));
801 	wpa_hexdump(MSG_MSGDUMP, "WMM AC: WMM AC Action content", data, len);
802 
803 	switch (action) {
804 	case WMM_ACTION_CODE_ADDTS_RESP:
805 		wmm_ac_handle_addts_resp(wpa_s, sa, dialog_token, status_code,
806 					 tspec);
807 		break;
808 	case WMM_ACTION_CODE_DELTS:
809 		wmm_ac_handle_delts(wpa_s, sa, tspec);
810 		break;
811 	default:
812 		break;
813 	}
814 }
815 
816 
get_ac_str(u8 ac)817 static const char * get_ac_str(u8 ac)
818 {
819 	switch (ac) {
820 	case WMM_AC_BE:
821 		return "BE";
822 	case WMM_AC_BK:
823 		return "BK";
824 	case WMM_AC_VI:
825 		return "VI";
826 	case WMM_AC_VO:
827 		return "VO";
828 	default:
829 		return "N/A";
830 	}
831 }
832 
833 
get_direction_str(u8 direction)834 static const char * get_direction_str(u8 direction)
835 {
836 	switch (direction) {
837 	case WMM_AC_DIR_DOWNLINK:
838 		return "Downlink";
839 	case WMM_AC_DIR_UPLINK:
840 		return "Uplink";
841 	case WMM_AC_DIR_BIDIRECTIONAL:
842 		return "Bi-directional";
843 	default:
844 		return "N/A";
845 	}
846 }
847 
848 
wpas_wmm_ac_status(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)849 int wpas_wmm_ac_status(struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
850 {
851 	struct wmm_ac_assoc_data *assoc_info = wpa_s->wmm_ac_assoc_info;
852 	enum ts_dir_idx idx;
853 	int pos = 0;
854 	u8 ac, up;
855 
856 	if (!assoc_info) {
857 		return wpa_scnprintf(buf, buflen - pos,
858 				     "Not associated to a WMM AP, WMM AC is Disabled\n");
859 	}
860 
861 	pos += wpa_scnprintf(buf + pos, buflen - pos, "WMM AC is Enabled\n");
862 
863 	for (ac = 0; ac < WMM_AC_NUM; ac++) {
864 		int ts_count = 0;
865 
866 		pos += wpa_scnprintf(buf + pos, buflen - pos,
867 				     "%s: acm=%d uapsd=%d\n",
868 				     get_ac_str(ac),
869 				     assoc_info->ac_params[ac].acm,
870 				     assoc_info->ac_params[ac].uapsd);
871 
872 		for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
873 			struct wmm_tspec_element *tspec;
874 			u8 dir, tsid;
875 			const char *dir_str;
876 
877 			tspec = wpa_s->tspecs[ac][idx];
878 			if (!tspec)
879 				continue;
880 
881 			ts_count++;
882 
883 			dir = wmm_ac_get_direction(tspec);
884 			dir_str = get_direction_str(dir);
885 			tsid = wmm_ac_get_tsid(tspec);
886 			up = wmm_ac_get_user_priority(tspec);
887 
888 			pos += wpa_scnprintf(buf + pos, buflen - pos,
889 					     "\tTSID=%u UP=%u\n"
890 					     "\tAddress = "MACSTR"\n"
891 					     "\tWMM AC dir = %s\n"
892 					     "\tTotal admitted time = %u\n\n",
893 					     tsid, up,
894 					     MAC2STR(wpa_s->bssid),
895 					     dir_str,
896 					     le_to_host16(tspec->medium_time));
897 		}
898 
899 		if (!ts_count) {
900 			pos += wpa_scnprintf(buf + pos, buflen - pos,
901 					     "\t(No Traffic Stream)\n\n");
902 		}
903 	}
904 
905 	return pos;
906 }
907 
908 
wmm_ac_get_tspecs_count(struct wpa_supplicant * wpa_s)909 static u8 wmm_ac_get_tspecs_count(struct wpa_supplicant *wpa_s)
910 {
911 	int ac, dir, tspecs_count = 0;
912 
913 	for (ac = 0; ac < WMM_AC_NUM; ac++) {
914 		for (dir = 0; dir < TS_DIR_IDX_COUNT; dir++) {
915 			if (wpa_s->tspecs[ac][dir])
916 				tspecs_count++;
917 		}
918 	}
919 
920 	return tspecs_count;
921 }
922 
923 
wmm_ac_save_tspecs(struct wpa_supplicant * wpa_s)924 void wmm_ac_save_tspecs(struct wpa_supplicant *wpa_s)
925 {
926 	int ac, dir, tspecs_count;
927 
928 	wpa_printf(MSG_DEBUG, "WMM AC: Save last configured tspecs");
929 
930 	if (!wpa_s->wmm_ac_assoc_info)
931 		return;
932 
933 	tspecs_count = wmm_ac_get_tspecs_count(wpa_s);
934 	if (!tspecs_count) {
935 		wpa_printf(MSG_DEBUG, "WMM AC: No configured TSPECs");
936 		return;
937 	}
938 
939 	wpa_printf(MSG_DEBUG, "WMM AC: Saving tspecs");
940 
941 	wmm_ac_clear_saved_tspecs(wpa_s);
942 	wpa_s->last_tspecs = os_calloc(tspecs_count,
943 				       sizeof(*wpa_s->last_tspecs));
944 	if (!wpa_s->last_tspecs) {
945 		wpa_printf(MSG_ERROR, "WMM AC: Failed to save tspecs!");
946 		return;
947 	}
948 
949 	for (ac = 0; ac < WMM_AC_NUM; ac++) {
950 		for (dir = 0; dir < TS_DIR_IDX_COUNT; dir++) {
951 			if (!wpa_s->tspecs[ac][dir])
952 				continue;
953 
954 			wpa_s->last_tspecs[wpa_s->last_tspecs_count++] =
955 				*wpa_s->tspecs[ac][dir];
956 		}
957 	}
958 
959 	wpa_printf(MSG_DEBUG, "WMM AC: Successfully saved %d TSPECs",
960 		   wpa_s->last_tspecs_count);
961 }
962 
963 
wmm_ac_clear_saved_tspecs(struct wpa_supplicant * wpa_s)964 void wmm_ac_clear_saved_tspecs(struct wpa_supplicant *wpa_s)
965 {
966 	if (wpa_s->last_tspecs) {
967 		wpa_printf(MSG_DEBUG, "WMM AC: Clear saved tspecs");
968 		os_free(wpa_s->last_tspecs);
969 		wpa_s->last_tspecs = NULL;
970 		wpa_s->last_tspecs_count = 0;
971 	}
972 }
973 
974 
wmm_ac_restore_tspecs(struct wpa_supplicant * wpa_s)975 int wmm_ac_restore_tspecs(struct wpa_supplicant *wpa_s)
976 {
977 	unsigned int i;
978 
979 	if (!wpa_s->wmm_ac_assoc_info || !wpa_s->last_tspecs_count)
980 		return 0;
981 
982 	wpa_printf(MSG_DEBUG, "WMM AC: Restore %u saved tspecs",
983 		   wpa_s->last_tspecs_count);
984 
985 	for (i = 0; i < wpa_s->last_tspecs_count; i++)
986 		wmm_ac_add_ts(wpa_s, wpa_s->bssid, &wpa_s->last_tspecs[i]);
987 
988 	return 0;
989 }
990