1/*
2 * hostapd / IEEE 802.11n HT
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2007-2008, Intel Corporation
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10#include "utils/includes.h"
11
12#include "utils/common.h"
13#include "utils/eloop.h"
14#include "common/ieee802_11_defs.h"
15#include "hostapd.h"
16#include "ap_config.h"
17#include "sta_info.h"
18#include "beacon.h"
19#include "ieee802_11.h"
20#include "hw_features.h"
21#include "ap_drv_ops.h"
22
23
24u8 * hostapd_eid_ht_capabilities(struct hostapd_data *hapd, u8 *eid)
25{
26	struct ieee80211_ht_capabilities *cap;
27	u8 *pos = eid;
28
29	if (!hapd->iconf->ieee80211n || !hapd->iface->current_mode ||
30	    hapd->conf->disable_11n)
31		return eid;
32
33	*pos++ = WLAN_EID_HT_CAP;
34	*pos++ = sizeof(*cap);
35
36	cap = (struct ieee80211_ht_capabilities *) pos;
37	os_memset(cap, 0, sizeof(*cap));
38	cap->ht_capabilities_info = host_to_le16(hapd->iconf->ht_capab);
39	cap->a_mpdu_params = hapd->iface->current_mode->a_mpdu_params;
40	os_memcpy(cap->supported_mcs_set, hapd->iface->current_mode->mcs_set,
41		  16);
42
43	/* TODO: ht_extended_capabilities (now fully disabled) */
44	/* TODO: tx_bf_capability_info (now fully disabled) */
45	/* TODO: asel_capabilities (now fully disabled) */
46
47 	pos += sizeof(*cap);
48
49	if (hapd->iconf->obss_interval) {
50		struct ieee80211_obss_scan_parameters *scan_params;
51
52		*pos++ = WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS;
53		*pos++ = sizeof(*scan_params);
54
55		scan_params = (struct ieee80211_obss_scan_parameters *) pos;
56		os_memset(scan_params, 0, sizeof(*scan_params));
57		scan_params->width_trigger_scan_interval =
58			host_to_le16(hapd->iconf->obss_interval);
59
60		/* Fill in default values for remaining parameters
61		 * (IEEE Std 802.11-2012, 8.4.2.61 and MIB defval) */
62		scan_params->scan_passive_dwell =
63			host_to_le16(20);
64		scan_params->scan_active_dwell =
65			host_to_le16(10);
66		scan_params->scan_passive_total_per_channel =
67			host_to_le16(200);
68		scan_params->scan_active_total_per_channel =
69			host_to_le16(20);
70		scan_params->channel_transition_delay_factor =
71			host_to_le16(5);
72		scan_params->scan_activity_threshold =
73			host_to_le16(25);
74
75		pos += sizeof(*scan_params);
76	}
77
78	return pos;
79}
80
81
82u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
83{
84	struct ieee80211_ht_operation *oper;
85	u8 *pos = eid;
86
87	if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
88		return eid;
89
90	*pos++ = WLAN_EID_HT_OPERATION;
91	*pos++ = sizeof(*oper);
92
93	oper = (struct ieee80211_ht_operation *) pos;
94	os_memset(oper, 0, sizeof(*oper));
95
96	oper->primary_chan = hapd->iconf->channel;
97	oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
98	if (hapd->iconf->secondary_channel == 1)
99		oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE |
100			HT_INFO_HT_PARAM_STA_CHNL_WIDTH;
101	if (hapd->iconf->secondary_channel == -1)
102		oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW |
103			HT_INFO_HT_PARAM_STA_CHNL_WIDTH;
104
105	pos += sizeof(*oper);
106
107	return pos;
108}
109
110
111u8 * hostapd_eid_secondary_channel(struct hostapd_data *hapd, u8 *eid)
112{
113	u8 sec_ch;
114
115	if (!hapd->cs_freq_params.channel ||
116	    !hapd->cs_freq_params.sec_channel_offset)
117		return eid;
118
119	if (hapd->cs_freq_params.sec_channel_offset == -1)
120		sec_ch = HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW;
121	else if (hapd->cs_freq_params.sec_channel_offset == 1)
122		sec_ch = HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE;
123	else
124		return eid;
125
126	*eid++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
127	*eid++ = 1;
128	*eid++ = sec_ch;
129
130	return eid;
131}
132
133
134/*
135op_mode
136Set to 0 (HT pure) under the followign conditions
137	- all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
138	- all STAs in the BSS are 20 MHz HT in 20 MHz BSS
139Set to 1 (HT non-member protection) if there may be non-HT STAs
140	in both the primary and the secondary channel
141Set to 2 if only HT STAs are associated in BSS,
142	however and at least one 20 MHz HT STA is associated
143Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
144*/
145int hostapd_ht_operation_update(struct hostapd_iface *iface)
146{
147	u16 cur_op_mode, new_op_mode;
148	int op_mode_changes = 0;
149
150	if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
151		return 0;
152
153	wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
154		   __func__, iface->ht_op_mode);
155
156	if (!(iface->ht_op_mode & HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT)
157	    && iface->num_sta_ht_no_gf) {
158		iface->ht_op_mode |= HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT;
159		op_mode_changes++;
160	} else if ((iface->ht_op_mode &
161		    HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT) &&
162		   iface->num_sta_ht_no_gf == 0) {
163		iface->ht_op_mode &= ~HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT;
164		op_mode_changes++;
165	}
166
167	if (!(iface->ht_op_mode & HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT) &&
168	    (iface->num_sta_no_ht || iface->olbc_ht)) {
169		iface->ht_op_mode |= HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT;
170		op_mode_changes++;
171	} else if ((iface->ht_op_mode &
172		    HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT) &&
173		   (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
174		iface->ht_op_mode &= ~HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT;
175		op_mode_changes++;
176	}
177
178	if (iface->num_sta_no_ht)
179		new_op_mode = HT_PROT_NON_HT_MIXED;
180	else if (iface->conf->secondary_channel && iface->num_sta_ht_20mhz)
181		new_op_mode = HT_PROT_20MHZ_PROTECTION;
182	else if (iface->olbc_ht)
183		new_op_mode = HT_PROT_NONMEMBER_PROTECTION;
184	else
185		new_op_mode = HT_PROT_NO_PROTECTION;
186
187	cur_op_mode = iface->ht_op_mode & HT_OPER_OP_MODE_HT_PROT_MASK;
188	if (cur_op_mode != new_op_mode) {
189		iface->ht_op_mode &= ~HT_OPER_OP_MODE_HT_PROT_MASK;
190		iface->ht_op_mode |= new_op_mode;
191		op_mode_changes++;
192	}
193
194	wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
195		   __func__, iface->ht_op_mode, op_mode_changes);
196
197	return op_mode_changes;
198}
199
200
201static int is_40_allowed(struct hostapd_iface *iface, int channel)
202{
203	int pri_freq, sec_freq;
204	int affected_start, affected_end;
205	int pri = 2407 + 5 * channel;
206
207	if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
208		return 1;
209
210	pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
211
212	if (iface->conf->secondary_channel > 0)
213		sec_freq = pri_freq + 20;
214	else
215		sec_freq = pri_freq - 20;
216
217	affected_start = (pri_freq + sec_freq) / 2 - 25;
218	affected_end = (pri_freq + sec_freq) / 2 + 25;
219	if ((pri < affected_start || pri > affected_end))
220		return 1; /* not within affected channel range */
221
222	wpa_printf(MSG_ERROR, "40 MHz affected channel range: [%d,%d] MHz",
223		   affected_start, affected_end);
224	wpa_printf(MSG_ERROR, "Neighboring BSS: freq=%d", pri);
225	return 0;
226}
227
228
229void hostapd_2040_coex_action(struct hostapd_data *hapd,
230			      const struct ieee80211_mgmt *mgmt, size_t len)
231{
232	struct hostapd_iface *iface = hapd->iface;
233	struct ieee80211_2040_bss_coex_ie *bc_ie;
234	struct ieee80211_2040_intol_chan_report *ic_report;
235	int is_ht40_allowed = 1;
236	int i;
237	const u8 *start = (const u8 *) mgmt;
238	const u8 *data = start + IEEE80211_HDRLEN + 2;
239	struct sta_info *sta;
240
241	wpa_printf(MSG_DEBUG,
242		   "HT: Received 20/40 BSS Coexistence Management frame from "
243		   MACSTR, MAC2STR(mgmt->sa));
244
245	hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
246		       HOSTAPD_LEVEL_DEBUG, "hostapd_public_action - action=%d",
247		       mgmt->u.action.u.public_action.action);
248
249	if (!(iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
250		wpa_printf(MSG_DEBUG,
251			   "Ignore 20/40 BSS Coexistence Management frame since 40 MHz capability is not enabled");
252		return;
253	}
254
255	if (len < IEEE80211_HDRLEN + 2 + sizeof(*bc_ie)) {
256		wpa_printf(MSG_DEBUG,
257			   "Ignore too short 20/40 BSS Coexistence Management frame");
258		return;
259	}
260
261	/* 20/40 BSS Coexistence element */
262	bc_ie = (struct ieee80211_2040_bss_coex_ie *) data;
263	if (bc_ie->element_id != WLAN_EID_20_40_BSS_COEXISTENCE ||
264	    bc_ie->length < 1) {
265		wpa_printf(MSG_DEBUG, "Unexpected IE (%u,%u) in coex report",
266			   bc_ie->element_id, bc_ie->length);
267		return;
268	}
269	if (len < IEEE80211_HDRLEN + 2 + 2 + bc_ie->length) {
270		wpa_printf(MSG_DEBUG,
271			   "Truncated 20/40 BSS Coexistence element");
272		return;
273	}
274	data += 2 + bc_ie->length;
275
276	wpa_printf(MSG_DEBUG,
277		   "20/40 BSS Coexistence Information field: 0x%x (%s%s%s%s%s%s)",
278		   bc_ie->coex_param,
279		   (bc_ie->coex_param & BIT(0)) ? "[InfoReq]" : "",
280		   (bc_ie->coex_param & BIT(1)) ? "[40MHzIntolerant]" : "",
281		   (bc_ie->coex_param & BIT(2)) ? "[20MHzBSSWidthReq]" : "",
282		   (bc_ie->coex_param & BIT(3)) ? "[OBSSScanExemptionReq]" : "",
283		   (bc_ie->coex_param & BIT(4)) ?
284		   "[OBSSScanExemptionGrant]" : "",
285		   (bc_ie->coex_param & (BIT(5) | BIT(6) | BIT(7))) ?
286		   "[Reserved]" : "");
287
288	if (bc_ie->coex_param & WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ) {
289		/* Intra-BSS communication prohibiting 20/40 MHz BSS operation
290		 */
291		sta = ap_get_sta(hapd, mgmt->sa);
292		if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
293			wpa_printf(MSG_DEBUG,
294				   "Ignore intra-BSS 20/40 BSS Coexistence Management frame from not-associated STA");
295			return;
296		}
297
298		hostapd_logger(hapd, mgmt->sa,
299			       HOSTAPD_MODULE_IEEE80211,
300			       HOSTAPD_LEVEL_DEBUG,
301			       "20 MHz BSS width request bit is set in BSS coexistence information field");
302		is_ht40_allowed = 0;
303	}
304
305	if (bc_ie->coex_param & WLAN_20_40_BSS_COEX_40MHZ_INTOL) {
306		/* Inter-BSS communication prohibiting 20/40 MHz BSS operation
307		 */
308		hostapd_logger(hapd, mgmt->sa,
309			       HOSTAPD_MODULE_IEEE80211,
310			       HOSTAPD_LEVEL_DEBUG,
311			       "40 MHz intolerant bit is set in BSS coexistence information field");
312		is_ht40_allowed = 0;
313	}
314
315	/* 20/40 BSS Intolerant Channel Report element (zero or more times) */
316	while (start + len - data >= 3 &&
317	       data[0] == WLAN_EID_20_40_BSS_INTOLERANT && data[1] >= 1) {
318		u8 ielen = data[1];
319
320		if (ielen > start + len - data - 2) {
321			wpa_printf(MSG_DEBUG,
322				   "Truncated 20/40 BSS Intolerant Channel Report element");
323			return;
324		}
325		ic_report = (struct ieee80211_2040_intol_chan_report *) data;
326		wpa_printf(MSG_DEBUG,
327			   "20/40 BSS Intolerant Channel Report: Operating Class %u",
328			   ic_report->op_class);
329
330		/* Go through the channel report to find any BSS there in the
331		 * affected channel range */
332		for (i = 0; i < ielen - 1; i++) {
333			u8 chan = ic_report->variable[i];
334
335			if (chan == iface->conf->channel)
336				continue; /* matching own primary channel */
337			if (is_40_allowed(iface, chan))
338				continue; /* not within affected channels */
339			hostapd_logger(hapd, mgmt->sa,
340				       HOSTAPD_MODULE_IEEE80211,
341				       HOSTAPD_LEVEL_DEBUG,
342				       "20_40_INTOLERANT channel %d reported",
343				       chan);
344			is_ht40_allowed = 0;
345		}
346
347		data += 2 + ielen;
348	}
349	wpa_printf(MSG_DEBUG, "is_ht40_allowed=%d num_sta_ht40_intolerant=%d",
350		   is_ht40_allowed, iface->num_sta_ht40_intolerant);
351
352	if (!is_ht40_allowed &&
353	    (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX)) {
354		if (iface->conf->secondary_channel) {
355			hostapd_logger(hapd, mgmt->sa,
356				       HOSTAPD_MODULE_IEEE80211,
357				       HOSTAPD_LEVEL_INFO,
358				       "Switching to 20 MHz operation");
359			iface->conf->secondary_channel = 0;
360			ieee802_11_set_beacons(iface);
361		}
362		if (!iface->num_sta_ht40_intolerant &&
363		    iface->conf->obss_interval) {
364			unsigned int delay_time;
365			delay_time = OVERLAPPING_BSS_TRANS_DELAY_FACTOR *
366				iface->conf->obss_interval;
367			eloop_cancel_timeout(ap_ht2040_timeout, hapd->iface,
368					     NULL);
369			eloop_register_timeout(delay_time, 0, ap_ht2040_timeout,
370					       hapd->iface, NULL);
371			wpa_printf(MSG_DEBUG,
372				   "Reschedule HT 20/40 timeout to occur in %u seconds",
373				   delay_time);
374		}
375	}
376}
377
378
379u16 copy_sta_ht_capab(struct hostapd_data *hapd, struct sta_info *sta,
380		      const u8 *ht_capab)
381{
382	/*
383	 * Disable HT caps for STAs associated to no-HT BSSes, or for stations
384	 * that did not specify a valid WMM IE in the (Re)Association Request
385	 * frame.
386	 */
387	if (!ht_capab || !(sta->flags & WLAN_STA_WMM) ||
388	    !hapd->iconf->ieee80211n || hapd->conf->disable_11n) {
389		sta->flags &= ~WLAN_STA_HT;
390		os_free(sta->ht_capabilities);
391		sta->ht_capabilities = NULL;
392		return WLAN_STATUS_SUCCESS;
393	}
394
395	if (sta->ht_capabilities == NULL) {
396		sta->ht_capabilities =
397			os_zalloc(sizeof(struct ieee80211_ht_capabilities));
398		if (sta->ht_capabilities == NULL)
399			return WLAN_STATUS_UNSPECIFIED_FAILURE;
400	}
401
402	sta->flags |= WLAN_STA_HT;
403	os_memcpy(sta->ht_capabilities, ht_capab,
404		  sizeof(struct ieee80211_ht_capabilities));
405
406	return WLAN_STATUS_SUCCESS;
407}
408
409
410void ht40_intolerant_add(struct hostapd_iface *iface, struct sta_info *sta)
411{
412	if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
413		return;
414
415	wpa_printf(MSG_INFO, "HT: Forty MHz Intolerant is set by STA " MACSTR
416		   " in Association Request", MAC2STR(sta->addr));
417
418	if (sta->ht40_intolerant_set)
419		return;
420
421	sta->ht40_intolerant_set = 1;
422	iface->num_sta_ht40_intolerant++;
423	eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
424
425	if (iface->conf->secondary_channel &&
426	    (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX)) {
427		iface->conf->secondary_channel = 0;
428		ieee802_11_set_beacons(iface);
429	}
430}
431
432
433void ht40_intolerant_remove(struct hostapd_iface *iface, struct sta_info *sta)
434{
435	if (!sta->ht40_intolerant_set)
436		return;
437
438	sta->ht40_intolerant_set = 0;
439	iface->num_sta_ht40_intolerant--;
440
441	if (iface->num_sta_ht40_intolerant == 0 &&
442	    (iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
443	    (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX)) {
444		unsigned int delay_time = OVERLAPPING_BSS_TRANS_DELAY_FACTOR *
445			iface->conf->obss_interval;
446		wpa_printf(MSG_DEBUG,
447			   "HT: Start 20->40 MHz transition timer (%d seconds)",
448			   delay_time);
449		eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
450		eloop_register_timeout(delay_time, 0, ap_ht2040_timeout,
451				       iface, NULL);
452	}
453}
454
455
456static void update_sta_ht(struct hostapd_data *hapd, struct sta_info *sta)
457{
458	u16 ht_capab;
459
460	ht_capab = le_to_host16(sta->ht_capabilities->ht_capabilities_info);
461	wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities Info: "
462		   "0x%04x", MAC2STR(sta->addr), ht_capab);
463	if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
464		if (!sta->no_ht_gf_set) {
465			sta->no_ht_gf_set = 1;
466			hapd->iface->num_sta_ht_no_gf++;
467		}
468		wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no greenfield, num "
469			   "of non-gf stations %d",
470			   __func__, MAC2STR(sta->addr),
471			   hapd->iface->num_sta_ht_no_gf);
472	}
473	if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
474		if (!sta->ht_20mhz_set) {
475			sta->ht_20mhz_set = 1;
476			hapd->iface->num_sta_ht_20mhz++;
477		}
478		wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, num of "
479			   "20MHz HT STAs %d",
480			   __func__, MAC2STR(sta->addr),
481			   hapd->iface->num_sta_ht_20mhz);
482	}
483
484	if (ht_capab & HT_CAP_INFO_40MHZ_INTOLERANT)
485		ht40_intolerant_add(hapd->iface, sta);
486}
487
488
489static void update_sta_no_ht(struct hostapd_data *hapd, struct sta_info *sta)
490{
491	if (!sta->no_ht_set) {
492		sta->no_ht_set = 1;
493		hapd->iface->num_sta_no_ht++;
494	}
495	if (hapd->iconf->ieee80211n) {
496		wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no HT, num of "
497			   "non-HT stations %d",
498			   __func__, MAC2STR(sta->addr),
499			   hapd->iface->num_sta_no_ht);
500	}
501}
502
503
504void update_ht_state(struct hostapd_data *hapd, struct sta_info *sta)
505{
506	if ((sta->flags & WLAN_STA_HT) && sta->ht_capabilities)
507		update_sta_ht(hapd, sta);
508	else
509		update_sta_no_ht(hapd, sta);
510
511	if (hostapd_ht_operation_update(hapd->iface) > 0)
512		ieee802_11_set_beacons(hapd->iface);
513}
514
515
516void hostapd_get_ht_capab(struct hostapd_data *hapd,
517			  struct ieee80211_ht_capabilities *ht_cap,
518			  struct ieee80211_ht_capabilities *neg_ht_cap)
519{
520	u16 cap;
521
522	if (ht_cap == NULL)
523		return;
524	os_memcpy(neg_ht_cap, ht_cap, sizeof(*neg_ht_cap));
525	cap = le_to_host16(neg_ht_cap->ht_capabilities_info);
526
527	/*
528	 * Mask out HT features we don't support, but don't overwrite
529	 * non-symmetric features like STBC and SMPS. Just because
530	 * we're not in dynamic SMPS mode the STA might still be.
531	 */
532	cap &= (hapd->iconf->ht_capab | HT_CAP_INFO_RX_STBC_MASK |
533		HT_CAP_INFO_TX_STBC | HT_CAP_INFO_SMPS_MASK);
534
535	/*
536	 * STBC needs to be handled specially
537	 * if we don't support RX STBC, mask out TX STBC in the STA's HT caps
538	 * if we don't support TX STBC, mask out RX STBC in the STA's HT caps
539	 */
540	if (!(hapd->iconf->ht_capab & HT_CAP_INFO_RX_STBC_MASK))
541		cap &= ~HT_CAP_INFO_TX_STBC;
542	if (!(hapd->iconf->ht_capab & HT_CAP_INFO_TX_STBC))
543		cap &= ~HT_CAP_INFO_RX_STBC_MASK;
544
545	neg_ht_cap->ht_capabilities_info = host_to_le16(cap);
546}
547
548
549void ap_ht2040_timeout(void *eloop_data, void *user_data)
550{
551	struct hostapd_iface *iface = eloop_data;
552
553	wpa_printf(MSG_INFO, "Switching to 40 MHz operation");
554
555	iface->conf->secondary_channel = iface->secondary_ch;
556	ieee802_11_set_beacons(iface);
557}
558