1 /*
2 * Common hostapd/wpa_supplicant HW features
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2015, Qualcomm Atheros, Inc.
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 "common.h"
13 #include "defs.h"
14 #include "ieee802_11_defs.h"
15 #include "ieee802_11_common.h"
16 #include "hw_features_common.h"
17
18
hw_get_channel_chan(struct hostapd_hw_modes * mode,int chan,int * freq)19 struct hostapd_channel_data * hw_get_channel_chan(struct hostapd_hw_modes *mode,
20 int chan, int *freq)
21 {
22 int i;
23
24 if (freq)
25 *freq = 0;
26
27 if (!mode)
28 return NULL;
29
30 for (i = 0; i < mode->num_channels; i++) {
31 struct hostapd_channel_data *ch = &mode->channels[i];
32 if (ch->chan == chan) {
33 if (freq)
34 *freq = ch->freq;
35 return ch;
36 }
37 }
38
39 return NULL;
40 }
41
42
43 struct hostapd_channel_data *
hw_mode_get_channel(struct hostapd_hw_modes * mode,int freq,int * chan)44 hw_mode_get_channel(struct hostapd_hw_modes *mode, int freq, int *chan)
45 {
46 int i;
47
48 for (i = 0; i < mode->num_channels; i++) {
49 struct hostapd_channel_data *ch = &mode->channels[i];
50
51 if (ch->freq == freq) {
52 if (chan)
53 *chan = ch->chan;
54 return ch;
55 }
56 }
57
58 return NULL;
59 }
60
61
62 struct hostapd_channel_data *
hw_get_channel_freq(enum hostapd_hw_mode mode,int freq,int * chan,struct hostapd_hw_modes * hw_features,int num_hw_features)63 hw_get_channel_freq(enum hostapd_hw_mode mode, int freq, int *chan,
64 struct hostapd_hw_modes *hw_features, int num_hw_features)
65 {
66 struct hostapd_channel_data *chan_data;
67 int i;
68
69 if (chan)
70 *chan = 0;
71
72 if (!hw_features)
73 return NULL;
74
75 for (i = 0; i < num_hw_features; i++) {
76 struct hostapd_hw_modes *curr_mode = &hw_features[i];
77
78 if (curr_mode->mode != mode)
79 continue;
80
81 chan_data = hw_mode_get_channel(curr_mode, freq, chan);
82 if (chan_data)
83 return chan_data;
84 }
85
86 return NULL;
87 }
88
89
hw_get_freq(struct hostapd_hw_modes * mode,int chan)90 int hw_get_freq(struct hostapd_hw_modes *mode, int chan)
91 {
92 int freq;
93
94 hw_get_channel_chan(mode, chan, &freq);
95
96 return freq;
97 }
98
99
hw_get_chan(enum hostapd_hw_mode mode,int freq,struct hostapd_hw_modes * hw_features,int num_hw_features)100 int hw_get_chan(enum hostapd_hw_mode mode, int freq,
101 struct hostapd_hw_modes *hw_features, int num_hw_features)
102 {
103 int chan;
104
105 hw_get_channel_freq(mode, freq, &chan, hw_features, num_hw_features);
106
107 return chan;
108 }
109
110
allowed_ht40_channel_pair(enum hostapd_hw_mode mode,struct hostapd_channel_data * p_chan,struct hostapd_channel_data * s_chan)111 int allowed_ht40_channel_pair(enum hostapd_hw_mode mode,
112 struct hostapd_channel_data *p_chan,
113 struct hostapd_channel_data *s_chan)
114 {
115 int ok, first;
116 int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 140,
117 149, 157, 165, 173, 184, 192 };
118 size_t k;
119 int ht40_plus, pri_chan, sec_chan;
120
121 if (!p_chan || !s_chan)
122 return 0;
123 pri_chan = p_chan->chan;
124 sec_chan = s_chan->chan;
125
126 ht40_plus = pri_chan < sec_chan;
127
128 if (pri_chan == sec_chan || !sec_chan) {
129 if (chan_pri_allowed(p_chan))
130 return 1; /* HT40 not used */
131
132 wpa_printf(MSG_ERROR, "Channel %d is not allowed as primary",
133 pri_chan);
134 return 0;
135 }
136
137 wpa_printf(MSG_DEBUG,
138 "HT40: control channel: %d (%d MHz), secondary channel: %d (%d MHz)",
139 pri_chan, p_chan->freq, sec_chan, s_chan->freq);
140
141 /* Verify that HT40 secondary channel is an allowed 20 MHz
142 * channel */
143 if ((s_chan->flag & HOSTAPD_CHAN_DISABLED) ||
144 (ht40_plus && !(p_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P)) ||
145 (!ht40_plus && !(p_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40M))) {
146 wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
147 sec_chan);
148 return 0;
149 }
150
151 /*
152 * Verify that HT40 primary,secondary channel pair is allowed per
153 * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
154 * 2.4 GHz rules allow all cases where the secondary channel fits into
155 * the list of allowed channels (already checked above).
156 */
157 if (mode != HOSTAPD_MODE_IEEE80211A)
158 return 1;
159
160 first = pri_chan < sec_chan ? pri_chan : sec_chan;
161
162 ok = 0;
163 for (k = 0; k < ARRAY_SIZE(allowed); k++) {
164 if (first == allowed[k]) {
165 ok = 1;
166 break;
167 }
168 }
169 if (!ok) {
170 wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
171 pri_chan, sec_chan);
172 return 0;
173 }
174
175 return 1;
176 }
177
178
get_pri_sec_chan(struct wpa_scan_res * bss,int * pri_chan,int * sec_chan)179 void get_pri_sec_chan(struct wpa_scan_res *bss, int *pri_chan, int *sec_chan)
180 {
181 struct ieee80211_ht_operation *oper;
182 struct ieee802_11_elems elems;
183
184 *pri_chan = *sec_chan = 0;
185
186 if (ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0) !=
187 ParseFailed && elems.ht_operation) {
188 oper = (struct ieee80211_ht_operation *) elems.ht_operation;
189 *pri_chan = oper->primary_chan;
190 if (oper->ht_param & HT_INFO_HT_PARAM_STA_CHNL_WIDTH) {
191 int sec = oper->ht_param &
192 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
193 if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
194 *sec_chan = *pri_chan + 4;
195 else if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
196 *sec_chan = *pri_chan - 4;
197 }
198 }
199 }
200
201
check_40mhz_5g(struct wpa_scan_results * scan_res,struct hostapd_channel_data * pri_chan,struct hostapd_channel_data * sec_chan)202 int check_40mhz_5g(struct wpa_scan_results *scan_res,
203 struct hostapd_channel_data *pri_chan,
204 struct hostapd_channel_data *sec_chan)
205 {
206 int pri_bss, sec_bss;
207 int bss_pri_chan, bss_sec_chan;
208 size_t i;
209 int match;
210
211 if (!scan_res || !pri_chan || !sec_chan ||
212 pri_chan->freq == sec_chan->freq)
213 return 0;
214
215 /*
216 * Switch PRI/SEC channels if Beacons were detected on selected SEC
217 * channel, but not on selected PRI channel.
218 */
219 pri_bss = sec_bss = 0;
220 for (i = 0; i < scan_res->num; i++) {
221 struct wpa_scan_res *bss = scan_res->res[i];
222 if (bss->freq == pri_chan->freq)
223 pri_bss++;
224 else if (bss->freq == sec_chan->freq)
225 sec_bss++;
226 }
227 if (sec_bss && !pri_bss) {
228 wpa_printf(MSG_INFO,
229 "Switch own primary and secondary channel to get secondary channel with no Beacons from other BSSes");
230 return 2;
231 }
232
233 /*
234 * Match PRI/SEC channel with any existing HT40 BSS on the same
235 * channels that we are about to use (if already mixed order in
236 * existing BSSes, use own preference).
237 */
238 match = 0;
239 for (i = 0; i < scan_res->num; i++) {
240 struct wpa_scan_res *bss = scan_res->res[i];
241 get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
242 if (pri_chan->chan == bss_pri_chan &&
243 sec_chan->chan == bss_sec_chan) {
244 match = 1;
245 break;
246 }
247 }
248 if (!match) {
249 for (i = 0; i < scan_res->num; i++) {
250 struct wpa_scan_res *bss = scan_res->res[i];
251 get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
252 if (pri_chan->chan == bss_sec_chan &&
253 sec_chan->chan == bss_pri_chan) {
254 wpa_printf(MSG_INFO, "Switch own primary and "
255 "secondary channel due to BSS "
256 "overlap with " MACSTR,
257 MAC2STR(bss->bssid));
258 return 2;
259 }
260 }
261 }
262
263 return 1;
264 }
265
266
check_20mhz_bss(struct wpa_scan_res * bss,int pri_freq,int start,int end)267 static int check_20mhz_bss(struct wpa_scan_res *bss, int pri_freq, int start,
268 int end)
269 {
270 struct ieee802_11_elems elems;
271 struct ieee80211_ht_operation *oper;
272
273 if (bss->freq < start || bss->freq > end || bss->freq == pri_freq)
274 return 0;
275
276 if (ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0) ==
277 ParseFailed)
278 return 0;
279
280 if (!elems.ht_capabilities) {
281 wpa_printf(MSG_DEBUG, "Found overlapping legacy BSS: "
282 MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
283 return 1;
284 }
285
286 if (elems.ht_operation) {
287 oper = (struct ieee80211_ht_operation *) elems.ht_operation;
288 if (oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)
289 return 0;
290
291 wpa_printf(MSG_DEBUG, "Found overlapping 20 MHz HT BSS: "
292 MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
293 return 1;
294 }
295 return 0;
296 }
297
298
check_40mhz_2g4(struct hostapd_hw_modes * mode,struct wpa_scan_results * scan_res,int pri_chan,int sec_chan)299 int check_40mhz_2g4(struct hostapd_hw_modes *mode,
300 struct wpa_scan_results *scan_res, int pri_chan,
301 int sec_chan)
302 {
303 int pri_freq, sec_freq;
304 int affected_start, affected_end;
305 size_t i;
306
307 if (!mode || !scan_res || !pri_chan || !sec_chan ||
308 pri_chan == sec_chan)
309 return 0;
310
311 pri_freq = hw_get_freq(mode, pri_chan);
312 sec_freq = hw_get_freq(mode, sec_chan);
313
314 affected_start = (pri_freq + sec_freq) / 2 - 25;
315 affected_end = (pri_freq + sec_freq) / 2 + 25;
316 wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
317 affected_start, affected_end);
318 for (i = 0; i < scan_res->num; i++) {
319 struct wpa_scan_res *bss = scan_res->res[i];
320 int pri = bss->freq;
321 int sec = pri;
322 struct ieee802_11_elems elems;
323
324 /* Check for overlapping 20 MHz BSS */
325 if (check_20mhz_bss(bss, pri_freq, affected_start,
326 affected_end)) {
327 wpa_printf(MSG_DEBUG,
328 "Overlapping 20 MHz BSS is found");
329 return 0;
330 }
331
332 get_pri_sec_chan(bss, &pri_chan, &sec_chan);
333
334 if (sec_chan) {
335 if (sec_chan < pri_chan)
336 sec = pri - 20;
337 else
338 sec = pri + 20;
339 }
340
341 if ((pri < affected_start || pri > affected_end) &&
342 (sec < affected_start || sec > affected_end))
343 continue; /* not within affected channel range */
344
345 wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
346 " freq=%d pri=%d sec=%d",
347 MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
348
349 if (sec_chan) {
350 if (pri_freq != pri || sec_freq != sec) {
351 wpa_printf(MSG_DEBUG,
352 "40 MHz pri/sec mismatch with BSS "
353 MACSTR
354 " <%d,%d> (chan=%d%c) vs. <%d,%d>",
355 MAC2STR(bss->bssid),
356 pri, sec, pri_chan,
357 sec > pri ? '+' : '-',
358 pri_freq, sec_freq);
359 return 0;
360 }
361 }
362
363 if (ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len,
364 &elems, 0) != ParseFailed &&
365 elems.ht_capabilities) {
366 struct ieee80211_ht_capabilities *ht_cap =
367 (struct ieee80211_ht_capabilities *)
368 elems.ht_capabilities;
369
370 if (le_to_host16(ht_cap->ht_capabilities_info) &
371 HT_CAP_INFO_40MHZ_INTOLERANT) {
372 wpa_printf(MSG_DEBUG,
373 "40 MHz Intolerant is set on channel %d in BSS "
374 MACSTR, pri, MAC2STR(bss->bssid));
375 return 0;
376 }
377 }
378 }
379
380 return 1;
381 }
382
383
punct_update_legacy_bw_80(u8 bitmap,u8 pri_chan,u8 * seg0)384 static void punct_update_legacy_bw_80(u8 bitmap, u8 pri_chan, u8 *seg0)
385 {
386 u8 first_chan = *seg0 - 6, sec_chan;
387
388 switch (bitmap) {
389 case 0x6:
390 case 0x9:
391 *seg0 = 0;
392 return;
393 case 0x8:
394 case 0x4:
395 case 0x2:
396 case 0x1:
397 case 0xC:
398 case 0x3:
399 if (pri_chan < *seg0)
400 *seg0 -= 4;
401 else
402 *seg0 += 4;
403 break;
404 }
405
406 if (pri_chan < *seg0)
407 sec_chan = pri_chan + 4;
408 else
409 sec_chan = pri_chan - 4;
410
411 if (bitmap & BIT((sec_chan - first_chan) / 4))
412 *seg0 = 0;
413 }
414
415
punct_update_legacy_bw_160(u8 bitmap,u8 pri,enum oper_chan_width * width,u8 * seg0)416 static void punct_update_legacy_bw_160(u8 bitmap, u8 pri,
417 enum oper_chan_width *width, u8 *seg0)
418 {
419 if (pri < *seg0) {
420 *seg0 -= 8;
421 if (bitmap & 0x0F) {
422 *width = 0;
423 punct_update_legacy_bw_80(bitmap & 0xF, pri, seg0);
424 }
425 } else {
426 *seg0 += 8;
427 if (bitmap & 0xF0) {
428 *width = 0;
429 punct_update_legacy_bw_80((bitmap & 0xF0) >> 4, pri,
430 seg0);
431 }
432 }
433 }
434
435
punct_update_legacy_bw_320(u16 bitmap,u8 pri,enum oper_chan_width * width,u8 * seg0)436 static void punct_update_legacy_bw_320(u16 bitmap, u8 pri,
437 enum oper_chan_width *width, u8 *seg0)
438 {
439 if (pri < *seg0) {
440 *seg0 -= 16;
441 if (bitmap & 0x00FF) {
442 *width = 1;
443 punct_update_legacy_bw_160(bitmap & 0xFF, pri, width,
444 seg0);
445 }
446 } else {
447 *seg0 += 16;
448 if (bitmap & 0xFF00) {
449 *width = 1;
450 punct_update_legacy_bw_160((bitmap & 0xFF00) >> 8,
451 pri, width, seg0);
452 }
453 }
454 }
455
456
punct_update_legacy_bw(u16 bitmap,u8 pri,enum oper_chan_width * width,u8 * seg0,u8 * seg1)457 void punct_update_legacy_bw(u16 bitmap, u8 pri, enum oper_chan_width *width,
458 u8 *seg0, u8 *seg1)
459 {
460 if (*width == CONF_OPER_CHWIDTH_80MHZ && (bitmap & 0xF)) {
461 *width = CONF_OPER_CHWIDTH_USE_HT;
462 punct_update_legacy_bw_80(bitmap & 0xF, pri, seg0);
463 }
464
465 if (*width == CONF_OPER_CHWIDTH_160MHZ && (bitmap & 0xFF)) {
466 *width = CONF_OPER_CHWIDTH_80MHZ;
467 *seg1 = 0;
468 punct_update_legacy_bw_160(bitmap & 0xFF, pri, width, seg0);
469 }
470
471 if (*width == CONF_OPER_CHWIDTH_320MHZ && (bitmap & 0xFFFF)) {
472 *width = CONF_OPER_CHWIDTH_160MHZ;
473 punct_update_legacy_bw_320(bitmap & 0xFFFF, pri, width, seg0);
474 }
475 }
476
477
hostapd_set_freq_params(struct hostapd_freq_params * data,enum hostapd_hw_mode mode,int freq,int channel,int enable_edmg,u8 edmg_channel,int ht_enabled,int vht_enabled,int he_enabled,bool eht_enabled,int sec_channel_offset,enum oper_chan_width oper_chwidth,int center_segment0,int center_segment1,u32 vht_caps,struct he_capabilities * he_cap,struct eht_capabilities * eht_cap,u16 punct_bitmap)478 int hostapd_set_freq_params(struct hostapd_freq_params *data,
479 enum hostapd_hw_mode mode,
480 int freq, int channel, int enable_edmg,
481 u8 edmg_channel, int ht_enabled,
482 int vht_enabled, int he_enabled,
483 bool eht_enabled, int sec_channel_offset,
484 enum oper_chan_width oper_chwidth,
485 int center_segment0,
486 int center_segment1, u32 vht_caps,
487 struct he_capabilities *he_cap,
488 struct eht_capabilities *eht_cap,
489 u16 punct_bitmap)
490 {
491 enum oper_chan_width oper_chwidth_legacy;
492 u8 seg0_legacy, seg1_legacy;
493
494 if (!he_cap || !he_cap->he_supported)
495 he_enabled = 0;
496 if (!eht_cap || !eht_cap->eht_supported)
497 eht_enabled = 0;
498 os_memset(data, 0, sizeof(*data));
499 data->mode = mode;
500 data->freq = freq;
501 data->channel = channel;
502 data->ht_enabled = ht_enabled;
503 data->vht_enabled = vht_enabled;
504 data->he_enabled = he_enabled;
505 data->eht_enabled = eht_enabled;
506 data->sec_channel_offset = sec_channel_offset;
507 data->center_freq1 = freq + sec_channel_offset * 10;
508 data->center_freq2 = 0;
509 data->punct_bitmap = punct_bitmap;
510 if (oper_chwidth == CONF_OPER_CHWIDTH_80MHZ)
511 data->bandwidth = 80;
512 else if (oper_chwidth == CONF_OPER_CHWIDTH_160MHZ ||
513 oper_chwidth == CONF_OPER_CHWIDTH_80P80MHZ)
514 data->bandwidth = 160;
515 else if (oper_chwidth == CONF_OPER_CHWIDTH_320MHZ)
516 data->bandwidth = 320;
517 else if (sec_channel_offset)
518 data->bandwidth = 40;
519 else
520 data->bandwidth = 20;
521
522
523 hostapd_encode_edmg_chan(enable_edmg, edmg_channel, channel,
524 &data->edmg);
525
526 if (is_6ghz_freq(freq)) {
527 if (!data->he_enabled && !data->eht_enabled) {
528 wpa_printf(MSG_ERROR,
529 "Can't set 6 GHz mode - HE or EHT aren't enabled");
530 return -1;
531 }
532
533 if (center_idx_to_bw_6ghz(channel) < 0) {
534 wpa_printf(MSG_ERROR,
535 "Invalid control channel for 6 GHz band");
536 return -1;
537 }
538
539 if (!center_segment0) {
540 if (center_segment1) {
541 wpa_printf(MSG_ERROR,
542 "Segment 0 center frequency isn't set");
543 return -1;
544 }
545 if (!sec_channel_offset)
546 data->center_freq1 = data->freq;
547 } else {
548 int freq1, freq2 = 0;
549 int bw = center_idx_to_bw_6ghz(center_segment0);
550 int opclass;
551
552 if (bw < 0) {
553 wpa_printf(MSG_ERROR,
554 "Invalid center frequency index for 6 GHz");
555 return -1;
556 }
557
558 /* The 6 GHz channel 2 uses a different operating class
559 */
560 opclass = center_segment0 == 2 ? 136 : 131;
561 freq1 = ieee80211_chan_to_freq(NULL, opclass,
562 center_segment0);
563 if (freq1 < 0) {
564 wpa_printf(MSG_ERROR,
565 "Invalid segment 0 center frequency for 6 GHz");
566 return -1;
567 }
568
569 if (center_segment1) {
570 if (center_idx_to_bw_6ghz(center_segment1) != 2 ||
571 bw != 2) {
572 wpa_printf(MSG_ERROR,
573 "6 GHz 80+80 MHz configuration doesn't use valid 80 MHz channels");
574 return -1;
575 }
576
577 freq2 = ieee80211_chan_to_freq(NULL, 131,
578 center_segment1);
579 if (freq2 < 0) {
580 wpa_printf(MSG_ERROR,
581 "Invalid segment 1 center frequency for UHB");
582 return -1;
583 }
584 }
585
586 data->bandwidth = (1 << (u8) bw) * 20;
587 data->center_freq1 = freq1;
588 data->center_freq2 = freq2;
589 }
590 data->ht_enabled = 0;
591 data->vht_enabled = 0;
592
593 return 0;
594 }
595
596 if (data->eht_enabled) switch (oper_chwidth) {
597 case CONF_OPER_CHWIDTH_320MHZ:
598 if (eht_cap &&
599 !(eht_cap->phy_cap[EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_IDX] &
600 EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_MASK)) {
601 wpa_printf(MSG_ERROR,
602 "320 MHz channel width is not supported in 5 or 6 GHz");
603 return -1;
604 }
605 break;
606 default:
607 break;
608 }
609
610 if (data->he_enabled || data->eht_enabled) switch (oper_chwidth) {
611 case CONF_OPER_CHWIDTH_USE_HT:
612 if (sec_channel_offset == 0)
613 break;
614
615 if (mode == HOSTAPD_MODE_IEEE80211G) {
616 if (he_cap &&
617 !(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
618 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G)) {
619 wpa_printf(MSG_ERROR,
620 "40 MHz channel width is not supported in 2.4 GHz");
621 return -1;
622 }
623 break;
624 }
625 /* fall through */
626 case CONF_OPER_CHWIDTH_80MHZ:
627 if (mode == HOSTAPD_MODE_IEEE80211A) {
628 if (he_cap &&
629 !(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
630 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
631 wpa_printf(MSG_ERROR,
632 "40/80 MHz channel width is not supported in 5/6 GHz");
633 return -1;
634 }
635 }
636 break;
637 case CONF_OPER_CHWIDTH_80P80MHZ:
638 if (he_cap &&
639 !(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
640 HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G)) {
641 wpa_printf(MSG_ERROR,
642 "80+80 MHz channel width is not supported in 5/6 GHz");
643 return -1;
644 }
645 break;
646 case CONF_OPER_CHWIDTH_160MHZ:
647 if (he_cap &&
648 !(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
649 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G)) {
650 wpa_printf(MSG_ERROR,
651 "160 MHz channel width is not supported in 5 / 6GHz");
652 return -1;
653 }
654 break;
655 default:
656 break;
657 } else if (data->vht_enabled) switch (oper_chwidth) {
658 case CONF_OPER_CHWIDTH_USE_HT:
659 break;
660 case CONF_OPER_CHWIDTH_80P80MHZ:
661 if (!(vht_caps & VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)) {
662 wpa_printf(MSG_ERROR,
663 "80+80 channel width is not supported!");
664 return -1;
665 }
666 /* fall through */
667 case CONF_OPER_CHWIDTH_80MHZ:
668 break;
669 case CONF_OPER_CHWIDTH_160MHZ:
670 if (!(vht_caps & (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
671 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
672 wpa_printf(MSG_ERROR,
673 "160 MHz channel width is not supported!");
674 return -1;
675 }
676 break;
677 default:
678 break;
679 }
680
681 oper_chwidth_legacy = oper_chwidth;
682 seg0_legacy = center_segment0;
683 seg1_legacy = center_segment1;
684 if (punct_bitmap)
685 punct_update_legacy_bw(punct_bitmap, channel,
686 &oper_chwidth_legacy,
687 &seg0_legacy, &seg1_legacy);
688
689 if (data->eht_enabled || data->he_enabled ||
690 data->vht_enabled) switch (oper_chwidth) {
691 case CONF_OPER_CHWIDTH_USE_HT:
692 if (center_segment1 ||
693 (center_segment0 != 0 &&
694 5000 + center_segment0 * 5 != data->center_freq1 &&
695 2407 + center_segment0 * 5 != data->center_freq1)) {
696 wpa_printf(MSG_ERROR,
697 "20/40 MHz: center segment 0 (=%d) and center freq 1 (=%d) not in sync",
698 center_segment0, data->center_freq1);
699 return -1;
700 }
701 break;
702 case CONF_OPER_CHWIDTH_80P80MHZ:
703 if (center_segment1 == center_segment0 + 4 ||
704 center_segment1 == center_segment0 - 4) {
705 wpa_printf(MSG_ERROR,
706 "80+80 MHz: center segment 1 only 20 MHz apart");
707 return -1;
708 }
709 data->center_freq2 = 5000 + center_segment1 * 5;
710 /* fall through */
711 case CONF_OPER_CHWIDTH_80MHZ:
712 data->bandwidth = 80;
713 if (!sec_channel_offset &&
714 oper_chwidth_legacy != CONF_OPER_CHWIDTH_USE_HT) {
715 wpa_printf(MSG_ERROR,
716 "80/80+80 MHz: no second channel offset");
717 return -1;
718 }
719 if (oper_chwidth == CONF_OPER_CHWIDTH_80MHZ &&
720 center_segment1) {
721 wpa_printf(MSG_ERROR,
722 "80 MHz: center segment 1 configured");
723 return -1;
724 }
725 if (oper_chwidth == CONF_OPER_CHWIDTH_80P80MHZ &&
726 !center_segment1) {
727 wpa_printf(MSG_ERROR,
728 "80+80 MHz: center segment 1 not configured");
729 return -1;
730 }
731 if (!center_segment0) {
732 if (channel <= 48)
733 center_segment0 = 42;
734 else if (channel <= 64)
735 center_segment0 = 58;
736 else if (channel <= 112)
737 center_segment0 = 106;
738 else if (channel <= 128)
739 center_segment0 = 122;
740 else if (channel <= 144)
741 center_segment0 = 138;
742 else if (channel <= 161)
743 center_segment0 = 155;
744 else if (channel <= 177)
745 center_segment0 = 171;
746 data->center_freq1 = 5000 + center_segment0 * 5;
747 } else {
748 /*
749 * Note: HT/VHT config and params are coupled. Check if
750 * HT40 channel band is in VHT80 Pri channel band
751 * configuration.
752 */
753 if (center_segment0 == channel + 6 ||
754 center_segment0 == channel + 2 ||
755 center_segment0 == channel - 2 ||
756 center_segment0 == channel - 6)
757 data->center_freq1 = 5000 + center_segment0 * 5;
758 else {
759 wpa_printf(MSG_ERROR,
760 "Wrong coupling between HT and VHT/HE channel setting");
761 return -1;
762 }
763 }
764 break;
765 case CONF_OPER_CHWIDTH_160MHZ:
766 data->bandwidth = 160;
767 if (center_segment1) {
768 wpa_printf(MSG_ERROR,
769 "160 MHz: center segment 1 should not be set");
770 return -1;
771 }
772 if (!sec_channel_offset &&
773 oper_chwidth_legacy != CONF_OPER_CHWIDTH_USE_HT) {
774 wpa_printf(MSG_ERROR,
775 "160 MHz: second channel offset not set");
776 return -1;
777 }
778 /*
779 * Note: HT/VHT config and params are coupled. Check if
780 * HT40 channel band is in VHT160 channel band configuration.
781 */
782 if (center_segment0 == channel + 14 ||
783 center_segment0 == channel + 10 ||
784 center_segment0 == channel + 6 ||
785 center_segment0 == channel + 2 ||
786 center_segment0 == channel - 2 ||
787 center_segment0 == channel - 6 ||
788 center_segment0 == channel - 10 ||
789 center_segment0 == channel - 14)
790 data->center_freq1 = 5000 + center_segment0 * 5;
791 else {
792 wpa_printf(MSG_ERROR,
793 "160 MHz: HT40 channel band is not in 160 MHz band");
794 return -1;
795 }
796 break;
797 case CONF_OPER_CHWIDTH_320MHZ:
798 data->bandwidth = 320;
799 if (!data->eht_enabled || !is_6ghz_freq(freq)) {
800 wpa_printf(MSG_ERROR,
801 "320 MHz: EHT not enabled or not a 6 GHz channel");
802 return -1;
803 }
804 if (center_segment1) {
805 wpa_printf(MSG_ERROR,
806 "320 MHz: center segment 1 should not be set");
807 return -1;
808 }
809 if (center_segment0 == channel + 30 ||
810 center_segment0 == channel + 26 ||
811 center_segment0 == channel + 22 ||
812 center_segment0 == channel + 18 ||
813 center_segment0 == channel + 14 ||
814 center_segment0 == channel + 10 ||
815 center_segment0 == channel + 6 ||
816 center_segment0 == channel + 2 ||
817 center_segment0 == channel - 2 ||
818 center_segment0 == channel - 6 ||
819 center_segment0 == channel - 10 ||
820 center_segment0 == channel - 14 ||
821 center_segment0 == channel - 18 ||
822 center_segment0 == channel - 22 ||
823 center_segment0 == channel - 26 ||
824 center_segment0 == channel - 30)
825 data->center_freq1 = 5000 + center_segment0 * 5;
826 else {
827 wpa_printf(MSG_ERROR,
828 "320 MHz: wrong center segment 0");
829 return -1;
830 }
831 break;
832 default:
833 break;
834 }
835
836 return 0;
837 }
838
839
set_disable_ht40(struct ieee80211_ht_capabilities * htcaps,int disabled)840 void set_disable_ht40(struct ieee80211_ht_capabilities *htcaps,
841 int disabled)
842 {
843 /* Masking these out disables HT40 */
844 le16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
845 HT_CAP_INFO_SHORT_GI40MHZ);
846
847 if (disabled)
848 htcaps->ht_capabilities_info &= ~msk;
849 else
850 htcaps->ht_capabilities_info |= msk;
851 }
852
853
854 #ifdef CONFIG_IEEE80211AC
855
_ieee80211ac_cap_check(u32 hw,u32 conf,u32 cap,const char * name)856 static int _ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap,
857 const char *name)
858 {
859 u32 req_cap = conf & cap;
860
861 /*
862 * Make sure we support all requested capabilities.
863 * NOTE: We assume that 'cap' represents a capability mask,
864 * not a discrete value.
865 */
866 if ((hw & req_cap) != req_cap) {
867 wpa_printf(MSG_ERROR,
868 "Driver does not support configured VHT capability [%s]",
869 name);
870 return 0;
871 }
872 return 1;
873 }
874
875
ieee80211ac_cap_check_max(u32 hw,u32 conf,u32 mask,unsigned int shift,const char * name)876 static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 mask,
877 unsigned int shift,
878 const char *name)
879 {
880 u32 hw_max = hw & mask;
881 u32 conf_val = conf & mask;
882
883 if (conf_val > hw_max) {
884 wpa_printf(MSG_ERROR,
885 "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
886 name, conf_val >> shift, hw_max >> shift);
887 return 0;
888 }
889 return 1;
890 }
891
892
ieee80211ac_cap_check(u32 hw,u32 conf)893 int ieee80211ac_cap_check(u32 hw, u32 conf)
894 {
895 #define VHT_CAP_CHECK(cap) \
896 do { \
897 if (!_ieee80211ac_cap_check(hw, conf, cap, #cap)) \
898 return 0; \
899 } while (0)
900
901 #define VHT_CAP_CHECK_MAX(cap) \
902 do { \
903 if (!ieee80211ac_cap_check_max(hw, conf, cap, cap ## _SHIFT, \
904 #cap)) \
905 return 0; \
906 } while (0)
907
908 VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
909 VHT_CAP_CHECK_MAX(VHT_CAP_SUPP_CHAN_WIDTH_MASK);
910 VHT_CAP_CHECK(VHT_CAP_RXLDPC);
911 VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
912 VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
913 VHT_CAP_CHECK(VHT_CAP_TXSTBC);
914 VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
915 VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
916 VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
917 VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
918 VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
919 VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
920 VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
921 VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
922 VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
923 VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX);
924 VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
925 VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
926 VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
927 VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
928
929 #undef VHT_CAP_CHECK
930 #undef VHT_CAP_CHECK_MAX
931
932 return 1;
933 }
934
935 #endif /* CONFIG_IEEE80211AC */
936
937
num_chan_to_bw(int num_chans)938 u32 num_chan_to_bw(int num_chans)
939 {
940 switch (num_chans) {
941 case 2:
942 case 4:
943 case 8:
944 case 16:
945 return num_chans * 20;
946 default:
947 return 20;
948 }
949 }
950
951
952 /* check if BW is applicable for channel */
chan_bw_allowed(const struct hostapd_channel_data * chan,u32 bw,int ht40_plus,int pri)953 int chan_bw_allowed(const struct hostapd_channel_data *chan, u32 bw,
954 int ht40_plus, int pri)
955 {
956 u32 bw_mask;
957
958 switch (bw) {
959 case 20:
960 bw_mask = HOSTAPD_CHAN_WIDTH_20;
961 break;
962 case 40:
963 /* HT 40 MHz support declared only for primary channel,
964 * just skip 40 MHz secondary checking */
965 if (pri && ht40_plus)
966 bw_mask = HOSTAPD_CHAN_WIDTH_40P;
967 else if (pri && !ht40_plus)
968 bw_mask = HOSTAPD_CHAN_WIDTH_40M;
969 else
970 bw_mask = 0;
971 break;
972 case 80:
973 bw_mask = HOSTAPD_CHAN_WIDTH_80;
974 break;
975 case 160:
976 bw_mask = HOSTAPD_CHAN_WIDTH_160;
977 break;
978 case 320:
979 bw_mask = HOSTAPD_CHAN_WIDTH_320;
980 break;
981 default:
982 bw_mask = 0;
983 break;
984 }
985
986 return (chan->allowed_bw & bw_mask) == bw_mask;
987 }
988
989
990 /* check if channel is allowed to be used as primary */
chan_pri_allowed(const struct hostapd_channel_data * chan)991 int chan_pri_allowed(const struct hostapd_channel_data *chan)
992 {
993 return !(chan->flag & HOSTAPD_CHAN_DISABLED) &&
994 (chan->allowed_bw & HOSTAPD_CHAN_WIDTH_20);
995 }
996
997
998 /* IEEE Std 802.11be-2024, Table 36-30 - Definition of the Punctured Channel
999 * Information field in the U-SIG for an EHT MU PPDU using non-OFDMA
1000 * transmissions */
1001 static const u16 punct_bitmap_80[] = { 0xF, 0xE, 0xD, 0xB, 0x7 };
1002 static const u16 punct_bitmap_160[] = {
1003 0xFF, 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF,
1004 0x7F, 0xFC, 0xF3, 0xCF, 0x3F
1005 };
1006 static const u16 punct_bitmap_320[] = {
1007 0xFFFF, 0xFFFC, 0xFFF3, 0xFFCF, 0xFF3F, 0xFCFF, 0xF3FF, 0xCFFF,
1008 0x3FFF, 0xFFF0, 0xFF0F, 0xF0FF, 0x0FFF, 0xFFC0, 0xFF30, 0xFCF0,
1009 0xF3F0, 0xCFF0, 0x3FF0, 0x0FFC, 0x0FF3, 0x0FCF, 0x0F3F, 0x0CFF,
1010 0x03FF
1011 };
1012
1013
is_punct_bitmap_valid(u16 bw,u16 pri_ch_bit_pos,u16 punct_bitmap)1014 bool is_punct_bitmap_valid(u16 bw, u16 pri_ch_bit_pos, u16 punct_bitmap)
1015 {
1016 u8 i, count;
1017 u16 bitmap;
1018 const u16 *valid_bitmaps;
1019
1020 if (!punct_bitmap) /* All channels active */
1021 return true;
1022
1023 bitmap = ~punct_bitmap;
1024
1025 switch (bw) {
1026 case 80:
1027 bitmap &= 0xF;
1028 valid_bitmaps = punct_bitmap_80;
1029 count = ARRAY_SIZE(punct_bitmap_80);
1030 break;
1031
1032 case 160:
1033 bitmap &= 0xFF;
1034 valid_bitmaps = punct_bitmap_160;
1035 count = ARRAY_SIZE(punct_bitmap_160);
1036 break;
1037
1038 case 320:
1039 bitmap &= 0xFFFF;
1040 valid_bitmaps = punct_bitmap_320;
1041 count = ARRAY_SIZE(punct_bitmap_320);
1042 break;
1043
1044 default:
1045 return false;
1046 }
1047
1048 if (!bitmap) /* No channel active */
1049 return false;
1050
1051 if (!(bitmap & BIT(pri_ch_bit_pos))) {
1052 wpa_printf(MSG_DEBUG, "Primary channel cannot be punctured");
1053 return false;
1054 }
1055
1056 for (i = 0; i < count; i++) {
1057 if (valid_bitmaps[i] == bitmap)
1058 return true;
1059 }
1060
1061 return false;
1062 }
1063
1064
chan_in_current_hw_info(struct hostapd_multi_hw_info * current_hw_info,struct hostapd_channel_data * chan)1065 bool chan_in_current_hw_info(struct hostapd_multi_hw_info *current_hw_info,
1066 struct hostapd_channel_data *chan)
1067 {
1068 /* Assuming that if current_hw_info is not set full
1069 * iface->current_mode->channels[] can be used to scan for channels,
1070 * hence we return true.
1071 */
1072 if (!current_hw_info)
1073 return true;
1074
1075 return current_hw_info->start_freq <= chan->freq &&
1076 current_hw_info->end_freq >= chan->freq;
1077 }
1078