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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Alternatively, this software may be distributed under the terms of BSD
11 * license.
12 *
13 * See README and COPYING for more details.
14 */
15
16#include "utils/includes.h"
17
18#include "utils/common.h"
19#include "common/ieee802_11_defs.h"
20#include "drivers/driver.h"
21#include "hostapd.h"
22#include "ap_config.h"
23#include "sta_info.h"
24#include "beacon.h"
25#include "ieee802_11.h"
26
27
28u8 * hostapd_eid_ht_capabilities(struct hostapd_data *hapd, u8 *eid)
29{
30	struct ieee80211_ht_capabilities *cap;
31	u8 *pos = eid;
32
33	if (!hapd->iconf->ieee80211n || !hapd->iface->current_mode ||
34	    hapd->conf->disable_11n)
35		return eid;
36
37	*pos++ = WLAN_EID_HT_CAP;
38	*pos++ = sizeof(*cap);
39
40	cap = (struct ieee80211_ht_capabilities *) pos;
41	os_memset(cap, 0, sizeof(*cap));
42	cap->ht_capabilities_info = host_to_le16(hapd->iconf->ht_capab);
43	cap->a_mpdu_params = hapd->iface->current_mode->a_mpdu_params;
44	os_memcpy(cap->supported_mcs_set, hapd->iface->current_mode->mcs_set,
45		  16);
46
47	/* TODO: ht_extended_capabilities (now fully disabled) */
48	/* TODO: tx_bf_capability_info (now fully disabled) */
49	/* TODO: asel_capabilities (now fully disabled) */
50
51 	pos += sizeof(*cap);
52
53	return pos;
54}
55
56
57u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
58{
59	struct ieee80211_ht_operation *oper;
60	u8 *pos = eid;
61
62	if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
63		return eid;
64
65	*pos++ = WLAN_EID_HT_OPERATION;
66	*pos++ = sizeof(*oper);
67
68	oper = (struct ieee80211_ht_operation *) pos;
69	os_memset(oper, 0, sizeof(*oper));
70
71	oper->control_chan = hapd->iconf->channel;
72	oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
73	if (hapd->iconf->secondary_channel == 1)
74		oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE |
75			HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
76	if (hapd->iconf->secondary_channel == -1)
77		oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW |
78			HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
79
80	pos += sizeof(*oper);
81
82	return pos;
83}
84
85
86/*
87op_mode
88Set to 0 (HT pure) under the followign conditions
89	- all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
90	- all STAs in the BSS are 20 MHz HT in 20 MHz BSS
91Set to 1 (HT non-member protection) if there may be non-HT STAs
92	in both the primary and the secondary channel
93Set to 2 if only HT STAs are associated in BSS,
94	however and at least one 20 MHz HT STA is associated
95Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
96*/
97int hostapd_ht_operation_update(struct hostapd_iface *iface)
98{
99	u16 cur_op_mode, new_op_mode;
100	int op_mode_changes = 0;
101
102	if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
103		return 0;
104
105	wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
106		   __func__, iface->ht_op_mode);
107
108	if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
109	    && iface->num_sta_ht_no_gf) {
110		iface->ht_op_mode |=
111			HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
112		op_mode_changes++;
113	} else if ((iface->ht_op_mode &
114		    HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
115		   iface->num_sta_ht_no_gf == 0) {
116		iface->ht_op_mode &=
117			~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
118		op_mode_changes++;
119	}
120
121	if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
122	    (iface->num_sta_no_ht || iface->olbc_ht)) {
123		iface->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
124		op_mode_changes++;
125	} else if ((iface->ht_op_mode &
126		    HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
127		   (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
128		iface->ht_op_mode &=
129			~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
130		op_mode_changes++;
131	}
132
133	new_op_mode = 0;
134	if (iface->num_sta_no_ht)
135		new_op_mode = OP_MODE_MIXED;
136	else if ((iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
137		 && iface->num_sta_ht_20mhz)
138		new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
139	else if (iface->olbc_ht)
140		new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
141	else
142		new_op_mode = OP_MODE_PURE;
143
144	cur_op_mode = iface->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
145	if (cur_op_mode != new_op_mode) {
146		iface->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
147		iface->ht_op_mode |= new_op_mode;
148		op_mode_changes++;
149	}
150
151	wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
152		   __func__, iface->ht_op_mode, op_mode_changes);
153
154	return op_mode_changes;
155}
156
157
158u16 copy_sta_ht_capab(struct hostapd_data *hapd, struct sta_info *sta,
159		      const u8 *ht_capab, size_t ht_capab_len)
160{
161	/* Disable HT caps for STAs associated to no-HT BSSes. */
162	if (!ht_capab ||
163	    ht_capab_len < sizeof(struct ieee80211_ht_capabilities) ||
164	    hapd->conf->disable_11n) {
165		sta->flags &= ~WLAN_STA_HT;
166		os_free(sta->ht_capabilities);
167		sta->ht_capabilities = NULL;
168		return WLAN_STATUS_SUCCESS;
169	}
170
171	if (sta->ht_capabilities == NULL) {
172		sta->ht_capabilities =
173			os_zalloc(sizeof(struct ieee80211_ht_capabilities));
174		if (sta->ht_capabilities == NULL)
175			return WLAN_STATUS_UNSPECIFIED_FAILURE;
176	}
177
178	sta->flags |= WLAN_STA_HT;
179	os_memcpy(sta->ht_capabilities, ht_capab,
180		  sizeof(struct ieee80211_ht_capabilities));
181
182	return WLAN_STATUS_SUCCESS;
183}
184
185
186static void update_sta_ht(struct hostapd_data *hapd, struct sta_info *sta)
187{
188	u16 ht_capab;
189
190	ht_capab = le_to_host16(sta->ht_capabilities->ht_capabilities_info);
191	wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities Info: "
192		   "0x%04x", MAC2STR(sta->addr), ht_capab);
193	if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
194		if (!sta->no_ht_gf_set) {
195			sta->no_ht_gf_set = 1;
196			hapd->iface->num_sta_ht_no_gf++;
197		}
198		wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no greenfield, num "
199			   "of non-gf stations %d",
200			   __func__, MAC2STR(sta->addr),
201			   hapd->iface->num_sta_ht_no_gf);
202	}
203	if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
204		if (!sta->ht_20mhz_set) {
205			sta->ht_20mhz_set = 1;
206			hapd->iface->num_sta_ht_20mhz++;
207		}
208		wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, num of "
209			   "20MHz HT STAs %d",
210			   __func__, MAC2STR(sta->addr),
211			   hapd->iface->num_sta_ht_20mhz);
212	}
213}
214
215
216static void update_sta_no_ht(struct hostapd_data *hapd, struct sta_info *sta)
217{
218	if (!sta->no_ht_set) {
219		sta->no_ht_set = 1;
220		hapd->iface->num_sta_no_ht++;
221	}
222	if (hapd->iconf->ieee80211n) {
223		wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no HT, num of "
224			   "non-HT stations %d",
225			   __func__, MAC2STR(sta->addr),
226			   hapd->iface->num_sta_no_ht);
227	}
228}
229
230
231void update_ht_state(struct hostapd_data *hapd, struct sta_info *sta)
232{
233	if ((sta->flags & WLAN_STA_HT) && sta->ht_capabilities)
234		update_sta_ht(hapd, sta);
235	else
236		update_sta_no_ht(hapd, sta);
237
238	if (hostapd_ht_operation_update(hapd->iface) > 0)
239		ieee802_11_set_beacons(hapd->iface);
240}
241
242
243void hostapd_get_ht_capab(struct hostapd_data *hapd,
244			  struct ieee80211_ht_capabilities *ht_cap,
245			  struct ieee80211_ht_capabilities *neg_ht_cap)
246{
247	u16 cap;
248
249	if (ht_cap == NULL)
250		return;
251	os_memcpy(neg_ht_cap, ht_cap, sizeof(*neg_ht_cap));
252	cap = le_to_host16(neg_ht_cap->ht_capabilities_info);
253
254	/*
255	 * Mask out HT features we don't support, but don't overwrite
256	 * non-symmetric features like STBC and SMPS. Just because
257	 * we're not in dynamic SMPS mode the STA might still be.
258	 */
259	cap &= (hapd->iconf->ht_capab | HT_CAP_INFO_RX_STBC_MASK |
260		HT_CAP_INFO_TX_STBC | HT_CAP_INFO_SMPS_MASK);
261
262	/*
263	 * STBC needs to be handled specially
264	 * if we don't support RX STBC, mask out TX STBC in the STA's HT caps
265	 * if we don't support TX STBC, mask out RX STBC in the STA's HT caps
266	 */
267	if (!(hapd->iconf->ht_capab & HT_CAP_INFO_RX_STBC_MASK))
268		cap &= ~HT_CAP_INFO_TX_STBC;
269	if (!(hapd->iconf->ht_capab & HT_CAP_INFO_TX_STBC))
270		cap &= ~HT_CAP_INFO_RX_STBC_MASK;
271
272	neg_ht_cap->ht_capabilities_info = host_to_le16(cap);
273}
274