xref: /freebsd/contrib/wpa/src/ap/wmm.c (revision 7877fdebeeb35fad1cbbafce22598b1bdf97c786)
1 /*
2  * hostapd / WMM (Wi-Fi Multimedia)
3  * Copyright 2002-2003, Instant802 Networks, Inc.
4  * Copyright 2005-2006, Devicescape Software, Inc.
5  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
6  *
7  * This software may be distributed under the terms of the BSD license.
8  * See README for more details.
9  */
10 
11 #include "utils/includes.h"
12 
13 #include "utils/common.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/ieee802_11_common.h"
16 #include "hostapd.h"
17 #include "ieee802_11.h"
18 #include "sta_info.h"
19 #include "ap_config.h"
20 #include "ap_drv_ops.h"
21 #include "wmm.h"
22 
23 #ifndef MIN
24 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
25 #endif
26 #ifndef MAX
27 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
28 #endif
29 
30 
31 static inline u8 wmm_aci_aifsn(int aifsn, int acm, int aci)
32 {
33 	u8 ret;
34 	ret = (aifsn << WMM_AC_AIFNS_SHIFT) & WMM_AC_AIFSN_MASK;
35 	if (acm)
36 		ret |= WMM_AC_ACM;
37 	ret |= (aci << WMM_AC_ACI_SHIFT) & WMM_AC_ACI_MASK;
38 	return ret;
39 }
40 
41 
42 static inline u8 wmm_ecw(int ecwmin, int ecwmax)
43 {
44 	return ((ecwmin << WMM_AC_ECWMIN_SHIFT) & WMM_AC_ECWMIN_MASK) |
45 		((ecwmax << WMM_AC_ECWMAX_SHIFT) & WMM_AC_ECWMAX_MASK);
46 }
47 
48 
49 static void
50 wmm_set_regulatory_limit(const struct hostapd_wmm_ac_params *wmm_conf,
51 			 struct hostapd_wmm_ac_params *wmm,
52 			 const struct hostapd_wmm_rule *wmm_reg)
53 {
54 	int ac;
55 
56 	for (ac = 0; ac < WMM_AC_NUM; ac++) {
57 		wmm[ac].cwmin = MAX(wmm_conf[ac].cwmin, wmm_reg[ac].min_cwmin);
58 		wmm[ac].cwmax = MAX(wmm_conf[ac].cwmax, wmm_reg[ac].min_cwmax);
59 		wmm[ac].aifs = MAX(wmm_conf[ac].aifs, wmm_reg[ac].min_aifs);
60 		wmm[ac].txop_limit =
61 			MIN(wmm_conf[ac].txop_limit, wmm_reg[ac].max_txop);
62 		wmm[ac].admission_control_mandatory =
63 			wmm_conf[ac].admission_control_mandatory;
64 	}
65 }
66 
67 
68 /*
69  * Calculate WMM regulatory limit if any.
70  */
71 static void wmm_calc_regulatory_limit(struct hostapd_data *hapd,
72 				      struct hostapd_wmm_ac_params *acp)
73 {
74 	struct hostapd_hw_modes *mode = hapd->iface->current_mode;
75 	int c;
76 
77 	os_memcpy(acp, hapd->iconf->wmm_ac_params,
78 		  sizeof(hapd->iconf->wmm_ac_params));
79 
80 	for (c = 0; mode && c < mode->num_channels; c++) {
81 		struct hostapd_channel_data *chan = &mode->channels[c];
82 
83 		if (chan->freq != hapd->iface->freq)
84 			continue;
85 
86 		if (chan->wmm_rules_valid)
87 			wmm_set_regulatory_limit(hapd->iconf->wmm_ac_params,
88 						 acp, chan->wmm_rules);
89 		break;
90 	}
91 
92 	/*
93 	 * Check if we need to update set count. Since both were initialized to
94 	 * zero we can compare the whole array in one shot.
95 	 */
96 	if (os_memcmp(acp, hapd->iface->prev_wmm,
97 		      sizeof(hapd->iconf->wmm_ac_params)) != 0) {
98 		os_memcpy(hapd->iface->prev_wmm, acp,
99 			  sizeof(hapd->iconf->wmm_ac_params));
100 		hapd->parameter_set_count++;
101 	}
102 }
103 
104 
105 /*
106  * Add WMM Parameter Element to Beacon, Probe Response, and (Re)Association
107  * Response frames.
108  */
109 u8 * hostapd_eid_wmm(struct hostapd_data *hapd, u8 *eid)
110 {
111 	u8 *pos = eid;
112 	struct wmm_parameter_element *wmm =
113 		(struct wmm_parameter_element *) (pos + 2);
114 	struct hostapd_wmm_ac_params wmmp[WMM_AC_NUM] = { 0 };
115 	int e;
116 
117 	if (!hapd->conf->wmm_enabled)
118 		return eid;
119 	wmm_calc_regulatory_limit(hapd, wmmp);
120 	eid[0] = WLAN_EID_VENDOR_SPECIFIC;
121 	wmm->oui[0] = 0x00;
122 	wmm->oui[1] = 0x50;
123 	wmm->oui[2] = 0xf2;
124 	wmm->oui_type = WMM_OUI_TYPE;
125 	wmm->oui_subtype = WMM_OUI_SUBTYPE_PARAMETER_ELEMENT;
126 	wmm->version = WMM_VERSION;
127 	wmm->qos_info = hapd->parameter_set_count & 0xf;
128 
129 	if (hapd->conf->wmm_uapsd &&
130 	    (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
131 		wmm->qos_info |= 0x80;
132 
133 	wmm->reserved = 0;
134 
135 	/* fill in a parameter set record for each AC */
136 	for (e = 0; e < 4; e++) {
137 		struct wmm_ac_parameter *ac = &wmm->ac[e];
138 		struct hostapd_wmm_ac_params *acp = &wmmp[e];
139 
140 		ac->aci_aifsn = wmm_aci_aifsn(acp->aifs,
141 					      acp->admission_control_mandatory,
142 					      e);
143 		ac->cw = wmm_ecw(acp->cwmin, acp->cwmax);
144 		ac->txop_limit = host_to_le16(acp->txop_limit);
145 	}
146 
147 	pos = (u8 *) (wmm + 1);
148 	eid[1] = pos - eid - 2; /* element length */
149 
150 	return pos;
151 }
152 
153 
154 /*
155  * This function is called when a station sends an association request with
156  * WMM info element. The function returns 1 on success or 0 on any error in WMM
157  * element. eid does not include Element ID and Length octets.
158  */
159 int hostapd_eid_wmm_valid(struct hostapd_data *hapd, const u8 *eid, size_t len)
160 {
161 	struct wmm_information_element *wmm;
162 
163 	wpa_hexdump(MSG_MSGDUMP, "WMM IE", eid, len);
164 
165 	if (len < sizeof(struct wmm_information_element)) {
166 		wpa_printf(MSG_DEBUG, "Too short WMM IE (len=%lu)",
167 			   (unsigned long) len);
168 		return 0;
169 	}
170 
171 	wmm = (struct wmm_information_element *) eid;
172 	wpa_printf(MSG_DEBUG, "Validating WMM IE: OUI %02x:%02x:%02x  "
173 		   "OUI type %d  OUI sub-type %d  version %d  QoS info 0x%x",
174 		   wmm->oui[0], wmm->oui[1], wmm->oui[2], wmm->oui_type,
175 		   wmm->oui_subtype, wmm->version, wmm->qos_info);
176 	if (wmm->oui_subtype != WMM_OUI_SUBTYPE_INFORMATION_ELEMENT ||
177 	    wmm->version != WMM_VERSION) {
178 		wpa_printf(MSG_DEBUG, "Unsupported WMM IE Subtype/Version");
179 		return 0;
180 	}
181 
182 	return 1;
183 }
184 
185 
186 static void wmm_send_action(struct hostapd_data *hapd, const u8 *addr,
187 			    const struct wmm_tspec_element *tspec,
188 			    u8 action_code, u8 dialogue_token, u8 status_code)
189 {
190 	u8 buf[256];
191 	struct ieee80211_mgmt *m = (struct ieee80211_mgmt *) buf;
192 	struct wmm_tspec_element *t = (struct wmm_tspec_element *)
193 		m->u.action.u.wmm_action.variable;
194 	int len;
195 
196 	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
197 		       HOSTAPD_LEVEL_DEBUG,
198 		       "action response - reason %d", status_code);
199 	os_memset(buf, 0, sizeof(buf));
200 	m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
201 					WLAN_FC_STYPE_ACTION);
202 	os_memcpy(m->da, addr, ETH_ALEN);
203 	os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
204 	os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
205 	m->u.action.category = WLAN_ACTION_WMM;
206 	m->u.action.u.wmm_action.action_code = action_code;
207 	m->u.action.u.wmm_action.dialog_token = dialogue_token;
208 	m->u.action.u.wmm_action.status_code = status_code;
209 	os_memcpy(t, tspec, sizeof(struct wmm_tspec_element));
210 	len = ((u8 *) (t + 1)) - buf;
211 
212 	if (hostapd_drv_send_mlme(hapd, m, len, 0) < 0)
213 		wpa_printf(MSG_INFO, "wmm_send_action: send failed");
214 }
215 
216 
217 int wmm_process_tspec(struct wmm_tspec_element *tspec)
218 {
219 	u64 medium_time;
220 	unsigned int pps, duration;
221 	unsigned int up, psb, dir, tid;
222 	u16 val, surplus;
223 
224 	up = (tspec->ts_info[1] >> 3) & 0x07;
225 	psb = (tspec->ts_info[1] >> 2) & 0x01;
226 	dir = (tspec->ts_info[0] >> 5) & 0x03;
227 	tid = (tspec->ts_info[0] >> 1) & 0x0f;
228 	wpa_printf(MSG_DEBUG, "WMM: TS Info: UP=%d PSB=%d Direction=%d TID=%d",
229 		   up, psb, dir, tid);
230 	val = le_to_host16(tspec->nominal_msdu_size);
231 	wpa_printf(MSG_DEBUG, "WMM: Nominal MSDU Size: %d%s",
232 		   val & 0x7fff, val & 0x8000 ? " (fixed)" : "");
233 	wpa_printf(MSG_DEBUG, "WMM: Mean Data Rate: %u bps",
234 		   le_to_host32(tspec->mean_data_rate));
235 	wpa_printf(MSG_DEBUG, "WMM: Minimum PHY Rate: %u bps",
236 		   le_to_host32(tspec->minimum_phy_rate));
237 	val = le_to_host16(tspec->surplus_bandwidth_allowance);
238 	wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance: %u.%04u",
239 		   val >> 13, 10000 * (val & 0x1fff) / 0x2000);
240 
241 	val = le_to_host16(tspec->nominal_msdu_size);
242 	if (val == 0) {
243 		wpa_printf(MSG_DEBUG, "WMM: Invalid Nominal MSDU Size (0)");
244 		return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
245 	}
246 	/* pps = Ceiling((Mean Data Rate / 8) / Nominal MSDU Size) */
247 	pps = ((le_to_host32(tspec->mean_data_rate) / 8) + val - 1) / val;
248 	wpa_printf(MSG_DEBUG, "WMM: Packets-per-second estimate for TSPEC: %d",
249 		   pps);
250 
251 	if (le_to_host32(tspec->minimum_phy_rate) < 1000000) {
252 		wpa_printf(MSG_DEBUG, "WMM: Too small Minimum PHY Rate");
253 		return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
254 	}
255 
256 	duration = (le_to_host16(tspec->nominal_msdu_size) & 0x7fff) * 8 /
257 		(le_to_host32(tspec->minimum_phy_rate) / 1000000) +
258 		50 /* FIX: proper SIFS + ACK duration */;
259 
260 	/* unsigned binary number with an implicit binary point after the
261 	 * leftmost 3 bits, i.e., 0x2000 = 1.0 */
262 	surplus = le_to_host16(tspec->surplus_bandwidth_allowance);
263 	if (surplus <= 0x2000) {
264 		wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance not "
265 			   "greater than unity");
266 		return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
267 	}
268 
269 	medium_time = (u64) surplus * pps * duration / 0x2000;
270 	wpa_printf(MSG_DEBUG, "WMM: Estimated medium time: %lu",
271 		   (unsigned long) medium_time);
272 
273 	/*
274 	 * TODO: store list of granted (and still active) TSPECs and check
275 	 * whether there is available medium time for this request. For now,
276 	 * just refuse requests that would by themselves take very large
277 	 * portion of the available bandwidth.
278 	 */
279 	if (medium_time > 750000) {
280 		wpa_printf(MSG_DEBUG, "WMM: Refuse TSPEC request for over "
281 			   "75%% of available bandwidth");
282 		return WMM_ADDTS_STATUS_REFUSED;
283 	}
284 
285 	/* Convert to 32 microseconds per second unit */
286 	tspec->medium_time = host_to_le16(medium_time / 32);
287 
288 	return WMM_ADDTS_STATUS_ADMISSION_ACCEPTED;
289 }
290 
291 
292 static void wmm_addts_req(struct hostapd_data *hapd,
293 			  const struct ieee80211_mgmt *mgmt,
294 			  struct wmm_tspec_element *tspec, size_t len)
295 {
296 	const u8 *end = ((const u8 *) mgmt) + len;
297 	int res;
298 
299 	if ((const u8 *) (tspec + 1) > end) {
300 		wpa_printf(MSG_DEBUG, "WMM: TSPEC overflow in ADDTS Request");
301 		return;
302 	}
303 
304 	wpa_printf(MSG_DEBUG, "WMM: ADDTS Request (Dialog Token %d) for TSPEC "
305 		   "from " MACSTR,
306 		   mgmt->u.action.u.wmm_action.dialog_token,
307 		   MAC2STR(mgmt->sa));
308 
309 	res = wmm_process_tspec(tspec);
310 	wpa_printf(MSG_DEBUG, "WMM: ADDTS processing result: %d", res);
311 
312 	wmm_send_action(hapd, mgmt->sa, tspec, WMM_ACTION_CODE_ADDTS_RESP,
313 			mgmt->u.action.u.wmm_action.dialog_token, res);
314 }
315 
316 
317 void hostapd_wmm_action(struct hostapd_data *hapd,
318 			const struct ieee80211_mgmt *mgmt, size_t len)
319 {
320 	int action_code;
321 	int left = len - IEEE80211_HDRLEN - 4;
322 	const u8 *pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 4;
323 	struct ieee802_11_elems elems;
324 	struct sta_info *sta = ap_get_sta(hapd, mgmt->sa);
325 
326 	/* check that the request comes from a valid station */
327 	if (!sta ||
328 	    (sta->flags & (WLAN_STA_ASSOC | WLAN_STA_WMM)) !=
329 	    (WLAN_STA_ASSOC | WLAN_STA_WMM)) {
330 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
331 			       HOSTAPD_LEVEL_DEBUG,
332 			       "wmm action received is not from associated wmm"
333 			       " station");
334 		/* TODO: respond with action frame refused status code */
335 		return;
336 	}
337 
338 	if (left < 0)
339 		return; /* not a valid WMM Action frame */
340 
341 	/* extract the tspec info element */
342 	if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed) {
343 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
344 			       HOSTAPD_LEVEL_DEBUG,
345 			       "hostapd_wmm_action - could not parse wmm "
346 			       "action");
347 		/* TODO: respond with action frame invalid parameters status
348 		 * code */
349 		return;
350 	}
351 
352 	if (!elems.wmm_tspec ||
353 	    elems.wmm_tspec_len != (sizeof(struct wmm_tspec_element) - 2)) {
354 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
355 			       HOSTAPD_LEVEL_DEBUG,
356 			       "hostapd_wmm_action - missing or wrong length "
357 			       "tspec");
358 		/* TODO: respond with action frame invalid parameters status
359 		 * code */
360 		return;
361 	}
362 
363 	/* TODO: check the request is for an AC with ACM set, if not, refuse
364 	 * request */
365 
366 	action_code = mgmt->u.action.u.wmm_action.action_code;
367 	switch (action_code) {
368 	case WMM_ACTION_CODE_ADDTS_REQ:
369 		wmm_addts_req(hapd, mgmt, (struct wmm_tspec_element *)
370 			      (elems.wmm_tspec - 2), len);
371 		return;
372 #if 0
373 	/* TODO: needed for client implementation */
374 	case WMM_ACTION_CODE_ADDTS_RESP:
375 		wmm_setup_request(hapd, mgmt, len);
376 		return;
377 	/* TODO: handle station teardown requests */
378 	case WMM_ACTION_CODE_DELTS:
379 		wmm_teardown(hapd, mgmt, len);
380 		return;
381 #endif
382 	}
383 
384 	hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
385 		       HOSTAPD_LEVEL_DEBUG,
386 		       "hostapd_wmm_action - unknown action code %d",
387 		       action_code);
388 }
389