hostapd: Use stations nsts capability in (Re)Association Response frame
[mech_eap.git] / src / ap / ieee802_11_vht.c
1 /*
2  * hostapd / IEEE 802.11ac VHT
3  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of BSD license
7  *
8  * See README and COPYING 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 "hostapd.h"
16 #include "ap_config.h"
17 #include "sta_info.h"
18 #include "beacon.h"
19 #include "ieee802_11.h"
20 #include "dfs.h"
21
22
23 u8 * hostapd_eid_vht_capabilities(struct hostapd_data *hapd, u8 *eid, u32 nsts)
24 {
25         struct ieee80211_vht_capabilities *cap;
26         struct hostapd_hw_modes *mode = hapd->iface->current_mode;
27         u8 *pos = eid;
28
29         if (!mode)
30                 return eid;
31
32         if (mode->mode == HOSTAPD_MODE_IEEE80211G && hapd->conf->vendor_vht &&
33             mode->vht_capab == 0 && hapd->iface->hw_features) {
34                 int i;
35
36                 for (i = 0; i < hapd->iface->num_hw_features; i++) {
37                         if (hapd->iface->hw_features[i].mode ==
38                             HOSTAPD_MODE_IEEE80211A) {
39                                 mode = &hapd->iface->hw_features[i];
40                                 break;
41                         }
42                 }
43         }
44
45         *pos++ = WLAN_EID_VHT_CAP;
46         *pos++ = sizeof(*cap);
47
48         cap = (struct ieee80211_vht_capabilities *) pos;
49         os_memset(cap, 0, sizeof(*cap));
50         cap->vht_capabilities_info = host_to_le32(
51                 hapd->iface->conf->vht_capab);
52
53         if (nsts != 0) {
54                 u32 hapd_nsts;
55
56                 hapd_nsts = le_to_host32(cap->vht_capabilities_info);
57                 hapd_nsts = (hapd_nsts >> VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
58                 cap->vht_capabilities_info &=
59                         ~(host_to_le32(hapd_nsts <<
60                                        VHT_CAP_BEAMFORMEE_STS_OFFSET));
61                 cap->vht_capabilities_info |=
62                         host_to_le32(nsts << VHT_CAP_BEAMFORMEE_STS_OFFSET);
63         }
64
65         /* Supported MCS set comes from hw */
66         os_memcpy(&cap->vht_supported_mcs_set, mode->vht_mcs_set, 8);
67
68         pos += sizeof(*cap);
69
70         return pos;
71 }
72
73
74 u8 * hostapd_eid_vht_operation(struct hostapd_data *hapd, u8 *eid)
75 {
76         struct ieee80211_vht_operation *oper;
77         u8 *pos = eid;
78
79         *pos++ = WLAN_EID_VHT_OPERATION;
80         *pos++ = sizeof(*oper);
81
82         oper = (struct ieee80211_vht_operation *) pos;
83         os_memset(oper, 0, sizeof(*oper));
84
85         /*
86          * center freq = 5 GHz + (5 * index)
87          * So index 42 gives center freq 5.210 GHz
88          * which is channel 42 in 5G band
89          */
90         oper->vht_op_info_chan_center_freq_seg0_idx =
91                 hapd->iconf->vht_oper_centr_freq_seg0_idx;
92         oper->vht_op_info_chan_center_freq_seg1_idx =
93                 hapd->iconf->vht_oper_centr_freq_seg1_idx;
94
95         oper->vht_op_info_chwidth = hapd->iconf->vht_oper_chwidth;
96         if (hapd->iconf->vht_oper_chwidth == 2) {
97                 /*
98                  * Convert 160 MHz channel width to new style as interop
99                  * workaround.
100                  */
101                 oper->vht_op_info_chwidth = 1;
102                 oper->vht_op_info_chan_center_freq_seg1_idx =
103                         oper->vht_op_info_chan_center_freq_seg0_idx;
104                 if (hapd->iconf->channel <
105                     hapd->iconf->vht_oper_centr_freq_seg0_idx)
106                         oper->vht_op_info_chan_center_freq_seg0_idx -= 8;
107                 else
108                         oper->vht_op_info_chan_center_freq_seg0_idx += 8;
109         } else if (hapd->iconf->vht_oper_chwidth == 3) {
110                 /*
111                  * Convert 80+80 MHz channel width to new style as interop
112                  * workaround.
113                  */
114                 oper->vht_op_info_chwidth = 1;
115         }
116
117         /* VHT Basic MCS set comes from hw */
118         /* Hard code 1 stream, MCS0-7 is a min Basic VHT MCS rates */
119         oper->vht_basic_mcs_set = host_to_le16(0xfffc);
120         pos += sizeof(*oper);
121
122         return pos;
123 }
124
125
126 static int check_valid_vht_mcs(struct hostapd_hw_modes *mode,
127                                const u8 *sta_vht_capab)
128 {
129         const struct ieee80211_vht_capabilities *vht_cap;
130         struct ieee80211_vht_capabilities ap_vht_cap;
131         u16 sta_rx_mcs_set, ap_tx_mcs_set;
132         int i;
133
134         if (!mode)
135                 return 1;
136
137         /*
138          * Disable VHT caps for STAs for which there is not even a single
139          * allowed MCS in any supported number of streams, i.e., STA is
140          * advertising 3 (not supported) as VHT MCS rates for all supported
141          * stream cases.
142          */
143         os_memcpy(&ap_vht_cap.vht_supported_mcs_set, mode->vht_mcs_set,
144                   sizeof(ap_vht_cap.vht_supported_mcs_set));
145         vht_cap = (const struct ieee80211_vht_capabilities *) sta_vht_capab;
146
147         /* AP Tx MCS map vs. STA Rx MCS map */
148         sta_rx_mcs_set = le_to_host16(vht_cap->vht_supported_mcs_set.rx_map);
149         ap_tx_mcs_set = le_to_host16(ap_vht_cap.vht_supported_mcs_set.tx_map);
150
151         for (i = 0; i < VHT_RX_NSS_MAX_STREAMS; i++) {
152                 if ((ap_tx_mcs_set & (0x3 << (i * 2))) == 3)
153                         continue;
154
155                 if ((sta_rx_mcs_set & (0x3 << (i * 2))) == 3)
156                         continue;
157
158                 return 1;
159         }
160
161         wpa_printf(MSG_DEBUG,
162                    "No matching VHT MCS found between AP TX and STA RX");
163         return 0;
164 }
165
166
167 u8 * hostapd_eid_wb_chsw_wrapper(struct hostapd_data *hapd, u8 *eid)
168 {
169         u8 bw, chan1, chan2 = 0;
170         int freq1;
171
172         if (!hapd->cs_freq_params.channel ||
173             !hapd->cs_freq_params.vht_enabled)
174                 return eid;
175
176         /* bandwidth: 0: 40, 1: 80, 2: 160, 3: 80+80 */
177         switch (hapd->cs_freq_params.bandwidth) {
178         case 40:
179                 bw = 0;
180                 break;
181         case 80:
182                 /* check if it's 80+80 */
183                 if (!hapd->cs_freq_params.center_freq2)
184                         bw = 1;
185                 else
186                         bw = 3;
187                 break;
188         case 160:
189                 bw = 2;
190                 break;
191         default:
192                 /* not valid VHT bandwidth or not in CSA */
193                 return eid;
194         }
195
196         freq1 = hapd->cs_freq_params.center_freq1 ?
197                 hapd->cs_freq_params.center_freq1 :
198                 hapd->cs_freq_params.freq;
199         if (ieee80211_freq_to_chan(freq1, &chan1) !=
200             HOSTAPD_MODE_IEEE80211A)
201                 return eid;
202
203         if (hapd->cs_freq_params.center_freq2 &&
204             ieee80211_freq_to_chan(hapd->cs_freq_params.center_freq2,
205                                    &chan2) != HOSTAPD_MODE_IEEE80211A)
206                 return eid;
207
208         *eid++ = WLAN_EID_VHT_CHANNEL_SWITCH_WRAPPER;
209         *eid++ = 5; /* Length of Channel Switch Wrapper */
210         *eid++ = WLAN_EID_VHT_WIDE_BW_CHSWITCH;
211         *eid++ = 3; /* Length of Wide Bandwidth Channel Switch element */
212         *eid++ = bw; /* New Channel Width */
213         *eid++ = chan1; /* New Channel Center Frequency Segment 0 */
214         *eid++ = chan2; /* New Channel Center Frequency Segment 1 */
215
216         return eid;
217 }
218
219
220 u8 * hostapd_eid_txpower_envelope(struct hostapd_data *hapd, u8 *eid)
221 {
222         struct hostapd_iface *iface = hapd->iface;
223         struct hostapd_config *iconf = iface->conf;
224         struct hostapd_hw_modes *mode = iface->current_mode;
225         struct hostapd_channel_data *chan;
226         int dfs, i;
227         u8 channel, tx_pwr_count, local_pwr_constraint;
228         int max_tx_power;
229         u8 tx_pwr;
230
231         if (!mode)
232                 return eid;
233
234         if (ieee80211_freq_to_chan(iface->freq, &channel) == NUM_HOSTAPD_MODES)
235                 return eid;
236
237         for (i = 0; i < mode->num_channels; i++) {
238                 if (mode->channels[i].freq == iface->freq)
239                         break;
240         }
241         if (i == mode->num_channels)
242                 return eid;
243
244         switch (iface->conf->vht_oper_chwidth) {
245         case VHT_CHANWIDTH_USE_HT:
246                 if (iconf->secondary_channel == 0) {
247                         /* Max Transmit Power count = 0 (20 MHz) */
248                         tx_pwr_count = 0;
249                 } else {
250                         /* Max Transmit Power count = 1 (20, 40 MHz) */
251                         tx_pwr_count = 1;
252                 }
253                 break;
254         case VHT_CHANWIDTH_80MHZ:
255                 /* Max Transmit Power count = 2 (20, 40, and 80 MHz) */
256                 tx_pwr_count = 2;
257                 break;
258         case VHT_CHANWIDTH_80P80MHZ:
259         case VHT_CHANWIDTH_160MHZ:
260                 /* Max Transmit Power count = 3 (20, 40, 80, 160/80+80 MHz) */
261                 tx_pwr_count = 3;
262                 break;
263         default:
264                 return eid;
265         }
266
267         /*
268          * Below local_pwr_constraint logic is referred from
269          * hostapd_eid_pwr_constraint.
270          *
271          * Check if DFS is required by regulatory.
272          */
273         dfs = hostapd_is_dfs_required(hapd->iface);
274         if (dfs < 0)
275                 dfs = 0;
276
277         /*
278          * In order to meet regulations when TPC is not implemented using
279          * a transmit power that is below the legal maximum (including any
280          * mitigation factor) should help. In this case, indicate 3 dB below
281          * maximum allowed transmit power.
282          */
283         if (hapd->iconf->local_pwr_constraint == -1)
284                 local_pwr_constraint = (dfs == 0) ? 0 : 3;
285         else
286                 local_pwr_constraint = hapd->iconf->local_pwr_constraint;
287
288         /*
289          * A STA that is not an AP shall use a transmit power less than or
290          * equal to the local maximum transmit power level for the channel.
291          * The local maximum transmit power can be calculated from the formula:
292          * local max TX pwr = max TX pwr - local pwr constraint
293          * Where max TX pwr is maximum transmit power level specified for
294          * channel in Country element and local pwr constraint is specified
295          * for channel in this Power Constraint element.
296          */
297         chan = &mode->channels[i];
298         max_tx_power = chan->max_tx_power - local_pwr_constraint;
299
300         /*
301          * Local Maximum Transmit power is encoded as two's complement
302          * with a 0.5 dB step.
303          */
304         max_tx_power *= 2; /* in 0.5 dB steps */
305         if (max_tx_power > 127) {
306                 /* 63.5 has special meaning of 63.5 dBm or higher */
307                 max_tx_power = 127;
308         }
309         if (max_tx_power < -128)
310                 max_tx_power = -128;
311         if (max_tx_power < 0)
312                 tx_pwr = 0x80 + max_tx_power + 128;
313         else
314                 tx_pwr = max_tx_power;
315
316         *eid++ = WLAN_EID_VHT_TRANSMIT_POWER_ENVELOPE;
317         *eid++ = 2 + tx_pwr_count;
318
319         /*
320          * Max Transmit Power count and
321          * Max Transmit Power units = 0 (EIRP)
322          */
323         *eid++ = tx_pwr_count;
324
325         for (i = 0; i <= tx_pwr_count; i++)
326                 *eid++ = tx_pwr;
327
328         return eid;
329 }
330
331
332 u16 copy_sta_vht_capab(struct hostapd_data *hapd, struct sta_info *sta,
333                        const u8 *vht_capab)
334 {
335         /* Disable VHT caps for STAs associated to no-VHT BSSes. */
336         if (!vht_capab ||
337             hapd->conf->disable_11ac ||
338             !check_valid_vht_mcs(hapd->iface->current_mode, vht_capab)) {
339                 sta->flags &= ~WLAN_STA_VHT;
340                 os_free(sta->vht_capabilities);
341                 sta->vht_capabilities = NULL;
342                 return WLAN_STATUS_SUCCESS;
343         }
344
345         if (sta->vht_capabilities == NULL) {
346                 sta->vht_capabilities =
347                         os_zalloc(sizeof(struct ieee80211_vht_capabilities));
348                 if (sta->vht_capabilities == NULL)
349                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
350         }
351
352         sta->flags |= WLAN_STA_VHT;
353         os_memcpy(sta->vht_capabilities, vht_capab,
354                   sizeof(struct ieee80211_vht_capabilities));
355
356         return WLAN_STATUS_SUCCESS;
357 }
358
359
360 u16 copy_sta_vendor_vht(struct hostapd_data *hapd, struct sta_info *sta,
361                         const u8 *ie, size_t len)
362 {
363         const u8 *vht_capab;
364         unsigned int vht_capab_len;
365
366         if (!ie || len < 5 + 2 + sizeof(struct ieee80211_vht_capabilities) ||
367             hapd->conf->disable_11ac)
368                 goto no_capab;
369
370         /* The VHT Capabilities element embedded in vendor VHT */
371         vht_capab = ie + 5;
372         if (vht_capab[0] != WLAN_EID_VHT_CAP)
373                 goto no_capab;
374         vht_capab_len = vht_capab[1];
375         if (vht_capab_len < sizeof(struct ieee80211_vht_capabilities) ||
376             (int) vht_capab_len > ie + len - vht_capab - 2)
377                 goto no_capab;
378         vht_capab += 2;
379
380         if (sta->vht_capabilities == NULL) {
381                 sta->vht_capabilities =
382                         os_zalloc(sizeof(struct ieee80211_vht_capabilities));
383                 if (sta->vht_capabilities == NULL)
384                         return WLAN_STATUS_UNSPECIFIED_FAILURE;
385         }
386
387         sta->flags |= WLAN_STA_VHT | WLAN_STA_VENDOR_VHT;
388         os_memcpy(sta->vht_capabilities, vht_capab,
389                   sizeof(struct ieee80211_vht_capabilities));
390         return WLAN_STATUS_SUCCESS;
391
392 no_capab:
393         sta->flags &= ~WLAN_STA_VENDOR_VHT;
394         return WLAN_STATUS_SUCCESS;
395 }
396
397
398 u8 * hostapd_eid_vendor_vht(struct hostapd_data *hapd, u8 *eid)
399 {
400         u8 *pos = eid;
401
402         if (!hapd->iface->current_mode)
403                 return eid;
404
405         *pos++ = WLAN_EID_VENDOR_SPECIFIC;
406         *pos++ = (5 +           /* The Vendor OUI, type and subtype */
407                   2 + sizeof(struct ieee80211_vht_capabilities) +
408                   2 + sizeof(struct ieee80211_vht_operation));
409
410         WPA_PUT_BE32(pos, (OUI_BROADCOM << 8) | VENDOR_VHT_TYPE);
411         pos += 4;
412         *pos++ = VENDOR_VHT_SUBTYPE;
413         pos = hostapd_eid_vht_capabilities(hapd, pos, 0);
414         pos = hostapd_eid_vht_operation(hapd, pos);
415
416         return pos;
417 }
418
419
420 u16 set_sta_vht_opmode(struct hostapd_data *hapd, struct sta_info *sta,
421                        const u8 *vht_oper_notif)
422 {
423         if (!vht_oper_notif) {
424                 sta->flags &= ~WLAN_STA_VHT_OPMODE_ENABLED;
425                 return WLAN_STATUS_SUCCESS;
426         }
427
428         sta->flags |= WLAN_STA_VHT_OPMODE_ENABLED;
429         sta->vht_opmode = *vht_oper_notif;
430         return WLAN_STATUS_SUCCESS;
431 }
432
433
434 void hostapd_get_vht_capab(struct hostapd_data *hapd,
435                            struct ieee80211_vht_capabilities *vht_cap,
436                            struct ieee80211_vht_capabilities *neg_vht_cap)
437 {
438         u32 cap, own_cap, sym_caps;
439
440         if (vht_cap == NULL)
441                 return;
442         os_memcpy(neg_vht_cap, vht_cap, sizeof(*neg_vht_cap));
443
444         cap = le_to_host32(neg_vht_cap->vht_capabilities_info);
445         own_cap = hapd->iconf->vht_capab;
446
447         /* mask out symmetric VHT capabilities we don't support */
448         sym_caps = VHT_CAP_SHORT_GI_80 | VHT_CAP_SHORT_GI_160;
449         cap &= ~sym_caps | (own_cap & sym_caps);
450
451         /* mask out beamformer/beamformee caps if not supported */
452         if (!(own_cap & VHT_CAP_SU_BEAMFORMER_CAPABLE))
453                 cap &= ~(VHT_CAP_SU_BEAMFORMEE_CAPABLE |
454                          VHT_CAP_BEAMFORMEE_STS_MAX);
455
456         if (!(own_cap & VHT_CAP_SU_BEAMFORMEE_CAPABLE))
457                 cap &= ~(VHT_CAP_SU_BEAMFORMER_CAPABLE |
458                          VHT_CAP_SOUNDING_DIMENSION_MAX);
459
460         if (!(own_cap & VHT_CAP_MU_BEAMFORMER_CAPABLE))
461                 cap &= ~VHT_CAP_MU_BEAMFORMEE_CAPABLE;
462
463         if (!(own_cap & VHT_CAP_MU_BEAMFORMEE_CAPABLE))
464                 cap &= ~VHT_CAP_MU_BEAMFORMER_CAPABLE;
465
466         /* mask channel widths we don't support */
467         switch (own_cap & VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
468         case VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
469                 break;
470         case VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
471                 if (cap & VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) {
472                         cap &= ~VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
473                         cap |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
474                 }
475                 break;
476         default:
477                 cap &= ~VHT_CAP_SUPP_CHAN_WIDTH_MASK;
478                 break;
479         }
480
481         if (!(cap & VHT_CAP_SUPP_CHAN_WIDTH_MASK))
482                 cap &= ~VHT_CAP_SHORT_GI_160;
483
484         /*
485          * if we don't support RX STBC, mask out TX STBC in the STA's HT caps
486          * if we don't support TX STBC, mask out RX STBC in the STA's HT caps
487          */
488         if (!(own_cap & VHT_CAP_RXSTBC_MASK))
489                 cap &= ~VHT_CAP_TXSTBC;
490         if (!(own_cap & VHT_CAP_TXSTBC))
491                 cap &= ~VHT_CAP_RXSTBC_MASK;
492
493         neg_vht_cap->vht_capabilities_info = host_to_le32(cap);
494 }