Coverage Report

Created: 2025-10-10 06:31

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