blob: c4c00fc4f848aa909ec23b173b97277fd2492910 [file] [log] [blame]
Dmitry Shmidt051af732013-10-22 13:52:46 -07001/*
2 * DFS - Dynamic Frequency Selection
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004 * Copyright (c) 2013-2017, Qualcomm Atheros, Inc.
Dmitry Shmidt051af732013-10-22 13:52:46 -07005 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10#include "utils/includes.h"
11
12#include "utils/common.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidt7f656022015-02-25 14:36:37 -080014#include "common/hw_features_common.h"
Dmitry Shmidtcce06662013-11-04 18:44:24 -080015#include "common/wpa_ctrl.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070016#include "hostapd.h"
17#include "ap_drv_ops.h"
18#include "drivers/driver.h"
19#include "dfs.h"
20
21
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070022static int dfs_get_used_n_chans(struct hostapd_iface *iface, int *seg1)
Dmitry Shmidt051af732013-10-22 13:52:46 -070023{
24 int n_chans = 1;
25
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070026 *seg1 = 0;
27
Dmitry Shmidtcce06662013-11-04 18:44:24 -080028 if (iface->conf->ieee80211n && iface->conf->secondary_channel)
Dmitry Shmidt051af732013-10-22 13:52:46 -070029 n_chans = 2;
30
Hai Shalom81f62d82019-07-22 12:10:00 -070031 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
32 switch (hostapd_get_oper_chwidth(iface->conf)) {
33 case CHANWIDTH_USE_HT:
Dmitry Shmidt051af732013-10-22 13:52:46 -070034 break;
Hai Shalom81f62d82019-07-22 12:10:00 -070035 case CHANWIDTH_80MHZ:
Dmitry Shmidt051af732013-10-22 13:52:46 -070036 n_chans = 4;
37 break;
Hai Shalom81f62d82019-07-22 12:10:00 -070038 case CHANWIDTH_160MHZ:
Dmitry Shmidt051af732013-10-22 13:52:46 -070039 n_chans = 8;
40 break;
Hai Shalom81f62d82019-07-22 12:10:00 -070041 case CHANWIDTH_80P80MHZ:
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070042 n_chans = 4;
43 *seg1 = 4;
44 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -070045 default:
46 break;
47 }
48 }
49
50 return n_chans;
51}
52
53
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080054static int dfs_channel_available(struct hostapd_channel_data *chan,
55 int skip_radar)
Dmitry Shmidt051af732013-10-22 13:52:46 -070056{
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080057 /*
58 * When radar detection happens, CSA is performed. However, there's no
59 * time for CAC, so radar channels must be skipped when finding a new
Dmitry Shmidt98660862014-03-11 17:26:21 -070060 * channel for CSA, unless they are available for immediate use.
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080061 */
Dmitry Shmidt98660862014-03-11 17:26:21 -070062 if (skip_radar && (chan->flag & HOSTAPD_CHAN_RADAR) &&
63 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) !=
64 HOSTAPD_CHAN_DFS_AVAILABLE))
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080065 return 0;
66
Dmitry Shmidt051af732013-10-22 13:52:46 -070067 if (chan->flag & HOSTAPD_CHAN_DISABLED)
68 return 0;
69 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
70 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
71 HOSTAPD_CHAN_DFS_UNAVAILABLE))
72 return 0;
73 return 1;
74}
75
76
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070077static int dfs_is_chan_allowed(struct hostapd_channel_data *chan, int n_chans)
Dmitry Shmidt051af732013-10-22 13:52:46 -070078{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070079 /*
80 * The tables contain first valid channel number based on channel width.
81 * We will also choose this first channel as the control one.
82 */
83 int allowed_40[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
84 184, 192 };
85 /*
86 * VHT80, valid channels based on center frequency:
87 * 42, 58, 106, 122, 138, 155
88 */
89 int allowed_80[] = { 36, 52, 100, 116, 132, 149 };
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080090 /*
91 * VHT160 valid channels based on center frequency:
92 * 50, 114
93 */
94 int allowed_160[] = { 36, 100 };
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070095 int *allowed = allowed_40;
96 unsigned int i, allowed_no = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -070097
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070098 switch (n_chans) {
99 case 2:
100 allowed = allowed_40;
101 allowed_no = ARRAY_SIZE(allowed_40);
102 break;
103 case 4:
104 allowed = allowed_80;
105 allowed_no = ARRAY_SIZE(allowed_80);
106 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800107 case 8:
108 allowed = allowed_160;
109 allowed_no = ARRAY_SIZE(allowed_160);
110 break;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700111 default:
112 wpa_printf(MSG_DEBUG, "Unknown width for %d channels", n_chans);
113 break;
114 }
115
116 for (i = 0; i < allowed_no; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700117 if (chan->chan == allowed[i])
118 return 1;
119 }
120
121 return 0;
122}
123
124
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700125static struct hostapd_channel_data *
126dfs_get_chan_data(struct hostapd_hw_modes *mode, int freq, int first_chan_idx)
127{
128 int i;
129
130 for (i = first_chan_idx; i < mode->num_channels; i++) {
131 if (mode->channels[i].freq == freq)
132 return &mode->channels[i];
133 }
134
135 return NULL;
136}
137
138
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800139static int dfs_chan_range_available(struct hostapd_hw_modes *mode,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800140 int first_chan_idx, int num_chans,
141 int skip_radar)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800142{
143 struct hostapd_channel_data *first_chan, *chan;
144 int i;
Hai Shalom74f70d42019-02-11 14:42:39 -0800145 u32 bw = num_chan_to_bw(num_chans);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800146
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700147 if (first_chan_idx + num_chans > mode->num_channels)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800148 return 0;
149
150 first_chan = &mode->channels[first_chan_idx];
151
Hai Shalom74f70d42019-02-11 14:42:39 -0800152 /* hostapd DFS implementation assumes the first channel as primary.
153 * If it's not allowed to use the first channel as primary, decline the
154 * whole channel range. */
155 if (!chan_pri_allowed(first_chan))
156 return 0;
157
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800158 for (i = 0; i < num_chans; i++) {
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700159 chan = dfs_get_chan_data(mode, first_chan->freq + i * 20,
160 first_chan_idx);
161 if (!chan)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800162 return 0;
163
Hai Shalom74f70d42019-02-11 14:42:39 -0800164 /* HT 40 MHz secondary channel availability checked only for
165 * primary channel */
166 if (!chan_bw_allowed(chan, bw, 1, !i))
167 return 0;
168
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800169 if (!dfs_channel_available(chan, skip_radar))
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800170 return 0;
171 }
172
173 return 1;
174}
175
176
Dmitry Shmidt98660862014-03-11 17:26:21 -0700177static int is_in_chanlist(struct hostapd_iface *iface,
178 struct hostapd_channel_data *chan)
179{
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700180 if (!iface->conf->acs_ch_list.num)
Dmitry Shmidt98660862014-03-11 17:26:21 -0700181 return 1;
182
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700183 return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700184}
185
186
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800187/*
188 * The function assumes HT40+ operation.
189 * Make sure to adjust the following variables after calling this:
190 * - hapd->secondary_channel
Hai Shalom81f62d82019-07-22 12:10:00 -0700191 * - hapd->vht/he_oper_centr_freq_seg0_idx
192 * - hapd->vht/he_oper_centr_freq_seg1_idx
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800193 */
194static int dfs_find_channel(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700195 struct hostapd_channel_data **ret_chan,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800196 int idx, int skip_radar)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700197{
198 struct hostapd_hw_modes *mode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800199 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700200 int i, channel_idx = 0, n_chans, n_chans1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700201
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800202 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700203 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700204
205 wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans);
206 for (i = 0; i < mode->num_channels; i++) {
207 chan = &mode->channels[i];
208
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800209 /* Skip HT40/VHT incompatible channels */
210 if (iface->conf->ieee80211n &&
211 iface->conf->secondary_channel &&
Hai Shalom74f70d42019-02-11 14:42:39 -0800212 (!dfs_is_chan_allowed(chan, n_chans) ||
213 !(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P)))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700214 continue;
215
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800216 /* Skip incompatible chandefs */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800217 if (!dfs_chan_range_available(mode, i, n_chans, skip_radar))
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800218 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700219
Dmitry Shmidt98660862014-03-11 17:26:21 -0700220 if (!is_in_chanlist(iface, chan))
221 continue;
222
Dmitry Shmidt051af732013-10-22 13:52:46 -0700223 if (ret_chan && idx == channel_idx) {
224 wpa_printf(MSG_DEBUG, "Selected ch. #%d", chan->chan);
225 *ret_chan = chan;
226 return idx;
227 }
228 wpa_printf(MSG_DEBUG, "Adding channel: %d", chan->chan);
229 channel_idx++;
230 }
231 return channel_idx;
232}
233
234
Hai Shalom81f62d82019-07-22 12:10:00 -0700235static void dfs_adjust_center_freq(struct hostapd_iface *iface,
236 struct hostapd_channel_data *chan,
237 int secondary_channel,
238 u8 *oper_centr_freq_seg0_idx,
239 u8 *oper_centr_freq_seg1_idx)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700240{
Hai Shalom81f62d82019-07-22 12:10:00 -0700241 if (!iface->conf->ieee80211ac && !iface->conf->ieee80211ax)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700242 return;
243
244 if (!chan)
245 return;
246
Hai Shalom81f62d82019-07-22 12:10:00 -0700247 *oper_centr_freq_seg1_idx = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800248
Hai Shalom81f62d82019-07-22 12:10:00 -0700249 switch (hostapd_get_oper_chwidth(iface->conf)) {
250 case CHANWIDTH_USE_HT:
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800251 if (secondary_channel == 1)
Hai Shalom81f62d82019-07-22 12:10:00 -0700252 *oper_centr_freq_seg0_idx = chan->chan + 2;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800253 else if (secondary_channel == -1)
Hai Shalom81f62d82019-07-22 12:10:00 -0700254 *oper_centr_freq_seg0_idx = chan->chan - 2;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700255 else
Hai Shalom81f62d82019-07-22 12:10:00 -0700256 *oper_centr_freq_seg0_idx = chan->chan;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700257 break;
Hai Shalom81f62d82019-07-22 12:10:00 -0700258 case CHANWIDTH_80MHZ:
259 *oper_centr_freq_seg0_idx = chan->chan + 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700260 break;
Hai Shalom81f62d82019-07-22 12:10:00 -0700261 case CHANWIDTH_160MHZ:
262 *oper_centr_freq_seg0_idx = chan->chan + 14;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700263 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700264 default:
265 wpa_printf(MSG_INFO, "DFS only VHT20/40/80/160 is supported now");
Hai Shalom81f62d82019-07-22 12:10:00 -0700266 *oper_centr_freq_seg0_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700267 break;
268 }
269
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800270 wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d, %d",
Hai Shalom81f62d82019-07-22 12:10:00 -0700271 *oper_centr_freq_seg0_idx,
272 *oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700273}
274
275
276/* Return start channel idx we will use for mode->channels[idx] */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700277static int dfs_get_start_chan_idx(struct hostapd_iface *iface, int *seg1_start)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700278{
279 struct hostapd_hw_modes *mode;
280 struct hostapd_channel_data *chan;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800281 int channel_no = iface->conf->channel;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700282 int res = -1, i;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700283 int chan_seg1 = -1;
284
285 *seg1_start = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700286
287 /* HT40- */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800288 if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700289 channel_no -= 4;
290
Hai Shalom81f62d82019-07-22 12:10:00 -0700291 /* VHT/HE */
292 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
293 switch (hostapd_get_oper_chwidth(iface->conf)) {
294 case CHANWIDTH_USE_HT:
Dmitry Shmidt051af732013-10-22 13:52:46 -0700295 break;
Hai Shalom81f62d82019-07-22 12:10:00 -0700296 case CHANWIDTH_80MHZ:
297 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
298 iface->conf) - 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700299 break;
Hai Shalom81f62d82019-07-22 12:10:00 -0700300 case CHANWIDTH_160MHZ:
301 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
302 iface->conf) - 14;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700303 break;
Hai Shalom81f62d82019-07-22 12:10:00 -0700304 case CHANWIDTH_80P80MHZ:
305 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
306 iface->conf) - 6;
307 chan_seg1 = hostapd_get_oper_centr_freq_seg1_idx(
308 iface->conf) - 6;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700309 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700310 default:
311 wpa_printf(MSG_INFO,
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700312 "DFS only VHT20/40/80/160/80+80 is supported now");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700313 channel_no = -1;
314 break;
315 }
316 }
317
318 /* Get idx */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800319 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700320 for (i = 0; i < mode->num_channels; i++) {
321 chan = &mode->channels[i];
322 if (chan->chan == channel_no) {
323 res = i;
324 break;
325 }
326 }
327
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700328 if (res != -1 && chan_seg1 > -1) {
329 int found = 0;
330
331 /* Get idx for seg1 */
332 mode = iface->current_mode;
333 for (i = 0; i < mode->num_channels; i++) {
334 chan = &mode->channels[i];
335 if (chan->chan == chan_seg1) {
336 *seg1_start = i;
337 found = 1;
338 break;
339 }
340 }
341 if (!found)
342 res = -1;
343 }
344
Dmitry Shmidt98660862014-03-11 17:26:21 -0700345 if (res == -1) {
346 wpa_printf(MSG_DEBUG,
347 "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d",
348 mode->num_channels, channel_no, iface->conf->channel,
349 iface->conf->ieee80211n,
350 iface->conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700351 hostapd_get_oper_chwidth(iface->conf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700352
353 for (i = 0; i < mode->num_channels; i++) {
354 wpa_printf(MSG_DEBUG, "Available channel: %d",
355 mode->channels[i].chan);
356 }
357 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700358
359 return res;
360}
361
362
363/* At least one channel have radar flag */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800364static int dfs_check_chans_radar(struct hostapd_iface *iface,
365 int start_chan_idx, int n_chans)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700366{
367 struct hostapd_channel_data *channel;
368 struct hostapd_hw_modes *mode;
369 int i, res = 0;
370
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800371 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700372
373 for (i = 0; i < n_chans; i++) {
374 channel = &mode->channels[start_chan_idx + i];
375 if (channel->flag & HOSTAPD_CHAN_RADAR)
376 res++;
377 }
378
379 return res;
380}
381
382
383/* All channels available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800384static int dfs_check_chans_available(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700385 int start_chan_idx, int n_chans)
386{
387 struct hostapd_channel_data *channel;
388 struct hostapd_hw_modes *mode;
389 int i;
390
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800391 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700392
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800393 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700394 channel = &mode->channels[start_chan_idx + i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800395
396 if (channel->flag & HOSTAPD_CHAN_DISABLED)
397 break;
398
399 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
400 continue;
401
Dmitry Shmidt051af732013-10-22 13:52:46 -0700402 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
403 HOSTAPD_CHAN_DFS_AVAILABLE)
404 break;
405 }
406
407 return i == n_chans;
408}
409
410
411/* At least one channel unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800412static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700413 int start_chan_idx,
414 int n_chans)
415{
416 struct hostapd_channel_data *channel;
417 struct hostapd_hw_modes *mode;
418 int i, res = 0;
419
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800420 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700421
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800422 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700423 channel = &mode->channels[start_chan_idx + i];
424 if (channel->flag & HOSTAPD_CHAN_DISABLED)
425 res++;
426 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
427 HOSTAPD_CHAN_DFS_UNAVAILABLE)
428 res++;
429 }
430
431 return res;
432}
433
434
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800435static struct hostapd_channel_data *
436dfs_get_valid_channel(struct hostapd_iface *iface,
437 int *secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700438 u8 *oper_centr_freq_seg0_idx,
439 u8 *oper_centr_freq_seg1_idx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800440 int skip_radar)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700441{
442 struct hostapd_hw_modes *mode;
443 struct hostapd_channel_data *chan = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800444 int num_available_chandefs;
445 int chan_idx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700446 u32 _rand;
447
448 wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800449 *secondary_channel = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -0700450 *oper_centr_freq_seg0_idx = 0;
451 *oper_centr_freq_seg1_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700452
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800453 if (iface->current_mode == NULL)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700454 return NULL;
455
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800456 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700457 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
458 return NULL;
459
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800460 /* Get the count first */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800461 num_available_chandefs = dfs_find_channel(iface, NULL, 0, skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800462 if (num_available_chandefs == 0)
463 return NULL;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700464
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800465 if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800466 return NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800467 chan_idx = _rand % num_available_chandefs;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800468 dfs_find_channel(iface, &chan, chan_idx, skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800469
470 /* dfs_find_channel() calculations assume HT40+ */
471 if (iface->conf->secondary_channel)
472 *secondary_channel = 1;
473 else
474 *secondary_channel = 0;
475
Hai Shalom81f62d82019-07-22 12:10:00 -0700476 dfs_adjust_center_freq(iface, chan,
477 *secondary_channel,
478 oper_centr_freq_seg0_idx,
479 oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700480
481 return chan;
482}
483
484
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800485static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700486{
487 struct hostapd_hw_modes *mode;
488 struct hostapd_channel_data *chan = NULL;
489 int i;
490
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800491 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700492 if (mode == NULL)
493 return 0;
494
495 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800496 for (i = 0; i < iface->current_mode->num_channels; i++) {
497 chan = &iface->current_mode->channels[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -0700498 if (chan->freq == freq) {
499 if (chan->flag & HOSTAPD_CHAN_RADAR) {
500 chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
501 chan->flag |= state;
502 return 1; /* Channel found */
503 }
504 }
505 }
506 wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
507 return 0;
508}
509
510
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800511static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700512 int chan_offset, int chan_width, int cf1,
513 int cf2, u32 state)
514{
515 int n_chans = 1, i;
516 struct hostapd_hw_modes *mode;
517 int frequency = freq;
518 int ret = 0;
519
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800520 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700521 if (mode == NULL)
522 return 0;
523
524 if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
525 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
526 return 0;
527 }
528
529 /* Seems cf1 and chan_width is enough here */
530 switch (chan_width) {
531 case CHAN_WIDTH_20_NOHT:
532 case CHAN_WIDTH_20:
533 n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800534 if (frequency == 0)
535 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700536 break;
537 case CHAN_WIDTH_40:
538 n_chans = 2;
539 frequency = cf1 - 10;
540 break;
541 case CHAN_WIDTH_80:
542 n_chans = 4;
543 frequency = cf1 - 30;
544 break;
545 case CHAN_WIDTH_160:
546 n_chans = 8;
547 frequency = cf1 - 70;
548 break;
549 default:
550 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
551 chan_width);
552 break;
553 }
554
555 wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
556 n_chans);
557 for (i = 0; i < n_chans; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800558 ret += set_dfs_state_freq(iface, frequency, state);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700559 frequency = frequency + 20;
560 }
561
562 return ret;
563}
564
565
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800566static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700567 int chan_width, int cf1, int cf2)
568{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700569 int start_chan_idx, start_chan_idx1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700570 struct hostapd_hw_modes *mode;
571 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700572 int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700573 u8 radar_chan;
574 int res = 0;
575
Dmitry Shmidt051af732013-10-22 13:52:46 -0700576 /* Our configuration */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800577 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700578 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
579 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700580
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700581 /* Check we are on DFS channel(s) */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800582 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700583 return 0;
584
Dmitry Shmidt051af732013-10-22 13:52:46 -0700585 /* Reported via radar event */
586 switch (chan_width) {
587 case CHAN_WIDTH_20_NOHT:
588 case CHAN_WIDTH_20:
589 radar_n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800590 if (frequency == 0)
591 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700592 break;
593 case CHAN_WIDTH_40:
594 radar_n_chans = 2;
595 frequency = cf1 - 10;
596 break;
597 case CHAN_WIDTH_80:
598 radar_n_chans = 4;
599 frequency = cf1 - 30;
600 break;
601 case CHAN_WIDTH_160:
602 radar_n_chans = 8;
603 frequency = cf1 - 70;
604 break;
605 default:
606 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
607 chan_width);
608 break;
609 }
610
611 ieee80211_freq_to_chan(frequency, &radar_chan);
612
613 for (i = 0; i < n_chans; i++) {
614 chan = &mode->channels[start_chan_idx + i];
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700615 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
616 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700617 for (j = 0; j < radar_n_chans; j++) {
618 wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
619 chan->chan, radar_chan + j * 4);
620 if (chan->chan == radar_chan + j * 4)
621 res++;
622 }
623 }
624
625 wpa_printf(MSG_DEBUG, "overlapped: %d", res);
626
627 return res;
628}
629
630
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700631static unsigned int dfs_get_cac_time(struct hostapd_iface *iface,
632 int start_chan_idx, int n_chans)
633{
634 struct hostapd_channel_data *channel;
635 struct hostapd_hw_modes *mode;
636 int i;
637 unsigned int cac_time_ms = 0;
638
639 mode = iface->current_mode;
640
641 for (i = 0; i < n_chans; i++) {
642 channel = &mode->channels[start_chan_idx + i];
643 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
644 continue;
645 if (channel->dfs_cac_ms > cac_time_ms)
646 cac_time_ms = channel->dfs_cac_ms;
647 }
648
649 return cac_time_ms;
650}
651
652
Dmitry Shmidt051af732013-10-22 13:52:46 -0700653/*
654 * Main DFS handler
655 * 1 - continue channel/ap setup
656 * 0 - channel/ap setup will be continued after CAC
657 * -1 - hit critical error
658 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800659int hostapd_handle_dfs(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700660{
661 struct hostapd_channel_data *channel;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700662 int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800663 int skip_radar = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700664
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800665 if (!iface->current_mode) {
666 /*
667 * This can happen with drivers that do not provide mode
668 * information and as such, cannot really use hostapd for DFS.
669 */
670 wpa_printf(MSG_DEBUG,
671 "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
672 return 1;
673 }
674
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800675 iface->cac_started = 0;
676
Dmitry Shmidt051af732013-10-22 13:52:46 -0700677 do {
678 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700679 start_chan_idx = dfs_get_start_chan_idx(iface,
680 &start_chan_idx1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700681 if (start_chan_idx == -1)
682 return -1;
683
684 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700685 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700686
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700687 /* Setup CAC time */
688 iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx,
689 n_chans);
690
Dmitry Shmidt051af732013-10-22 13:52:46 -0700691 /* Check if any of configured channels require DFS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800692 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700693 wpa_printf(MSG_DEBUG,
694 "DFS %d channels required radar detection",
695 res);
696 if (!res)
697 return 1;
698
699 /* Check if all channels are DFS available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800700 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700701 wpa_printf(MSG_DEBUG,
702 "DFS all channels available, (SKIP CAC): %s",
703 res ? "yes" : "no");
704 if (res)
705 return 1;
706
707 /* Check if any of configured channels is unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800708 res = dfs_check_chans_unavailable(iface, start_chan_idx,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700709 n_chans);
710 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
711 res, res ? "yes": "no");
712 if (res) {
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800713 int sec = 0;
714 u8 cf1 = 0, cf2 = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800715
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800716 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
717 skip_radar);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700718 if (!channel) {
719 wpa_printf(MSG_ERROR, "could not get valid channel");
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800720 hostapd_set_state(iface, HAPD_IFACE_DFS);
721 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700722 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800723
724 iface->freq = channel->freq;
725 iface->conf->channel = channel->chan;
726 iface->conf->secondary_channel = sec;
Hai Shalom81f62d82019-07-22 12:10:00 -0700727 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, cf1);
728 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, cf2);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700729 }
730 } while (res);
731
732 /* Finally start CAC */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800733 hostapd_set_state(iface, HAPD_IFACE_DFS);
734 wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz", iface->freq);
735 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700736 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800737 iface->freq,
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800738 iface->conf->channel, iface->conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700739 hostapd_get_oper_chwidth(iface->conf),
740 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
741 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700742 iface->dfs_cac_ms / 1000);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800743
Hai Shalom81f62d82019-07-22 12:10:00 -0700744 res = hostapd_start_dfs_cac(
745 iface, iface->conf->hw_mode, iface->freq, iface->conf->channel,
746 iface->conf->ieee80211n, iface->conf->ieee80211ac,
747 iface->conf->ieee80211ax,
748 iface->conf->secondary_channel,
749 hostapd_get_oper_chwidth(iface->conf),
750 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
751 hostapd_get_oper_centr_freq_seg1_idx(iface->conf));
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800752
753 if (res) {
754 wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700755 return -1;
756 }
757
758 return 0;
759}
760
761
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700762static int hostapd_config_dfs_chan_available(struct hostapd_iface *iface)
763{
764 int n_chans, n_chans1, start_chan_idx, start_chan_idx1;
765
766 /* Get the start (first) channel for current configuration */
767 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
768 if (start_chan_idx < 0)
769 return 0;
770
771 /* Get the number of used channels, depending on width */
772 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
773
774 /* Check if all channels are DFS available */
775 return dfs_check_chans_available(iface, start_chan_idx, n_chans);
776}
777
778
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800779int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700780 int ht_enabled, int chan_offset, int chan_width,
781 int cf1, int cf2)
782{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800783 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
784 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
785 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
786
Dmitry Shmidt051af732013-10-22 13:52:46 -0700787 if (success) {
788 /* Complete iface/ap configuration */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800789 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
790 /* Complete AP configuration for the first bring up. */
791 if (iface->state != HAPD_IFACE_ENABLED)
792 hostapd_setup_interface_complete(iface, 0);
793 else
794 iface->cac_started = 0;
795 } else {
796 set_dfs_state(iface, freq, ht_enabled, chan_offset,
797 chan_width, cf1, cf2,
798 HOSTAPD_CHAN_DFS_AVAILABLE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700799 /*
800 * Just mark the channel available when CAC completion
801 * event is received in enabled state. CAC result could
802 * have been propagated from another radio having the
803 * same regulatory configuration. When CAC completion is
804 * received during non-HAPD_IFACE_ENABLED state, make
805 * sure the configured channel is available because this
806 * CAC completion event could have been propagated from
807 * another radio.
808 */
809 if (iface->state != HAPD_IFACE_ENABLED &&
810 hostapd_config_dfs_chan_available(iface)) {
811 hostapd_setup_interface_complete(iface, 0);
812 iface->cac_started = 0;
813 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800814 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700815 }
816
817 return 0;
818}
819
820
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700821int hostapd_dfs_pre_cac_expired(struct hostapd_iface *iface, int freq,
822 int ht_enabled, int chan_offset, int chan_width,
823 int cf1, int cf2)
824{
825 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_PRE_CAC_EXPIRED
826 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
827 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
828
829 /* Proceed only if DFS is not offloaded to the driver */
830 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
831 return 0;
832
833 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
834 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
835
836 return 0;
837}
838
839
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800840static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700841{
842 struct hostapd_channel_data *channel;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800843 int secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -0700844 u8 oper_centr_freq_seg0_idx = 0;
845 u8 oper_centr_freq_seg1_idx = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800846 int skip_radar = 0;
847 int err = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700848
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800849 /* Radar detected during active CAC */
850 iface->cac_started = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800851 channel = dfs_get_valid_channel(iface, &secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700852 &oper_centr_freq_seg0_idx,
853 &oper_centr_freq_seg1_idx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800854 skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800855
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800856 if (!channel) {
857 wpa_printf(MSG_ERROR, "No valid channel available");
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800858 return err;
859 }
860
861 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
862 channel->chan);
863 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
864 "freq=%d chan=%d sec_chan=%d", channel->freq,
865 channel->chan, secondary_channel);
866
867 iface->freq = channel->freq;
868 iface->conf->channel = channel->chan;
869 iface->conf->secondary_channel = secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -0700870 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
871 oper_centr_freq_seg0_idx);
872 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
873 oper_centr_freq_seg1_idx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800874 err = 0;
875
876 hostapd_setup_interface_complete(iface, err);
877 return err;
878}
879
880
881static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
882{
883 struct hostapd_channel_data *channel;
884 int secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -0700885 u8 oper_centr_freq_seg0_idx;
886 u8 oper_centr_freq_seg1_idx;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800887 int skip_radar = 1;
888 struct csa_settings csa_settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700889 unsigned int i;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800890 int err = 1;
Hai Shalom81f62d82019-07-22 12:10:00 -0700891 struct hostapd_hw_modes *cmode = iface->current_mode;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800892
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800893 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
894 __func__, iface->cac_started ? "yes" : "no",
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700895 hostapd_csa_in_progress(iface) ? "yes" : "no");
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800896
897 /* Check if CSA in progress */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700898 if (hostapd_csa_in_progress(iface))
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800899 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800900
901 /* Check if active CAC */
902 if (iface->cac_started)
903 return hostapd_dfs_start_channel_switch_cac(iface);
904
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700905 /*
906 * Allow selection of DFS channel in ETSI to comply with
907 * uniform spreading.
908 */
909 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
910 skip_radar = 0;
911
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800912 /* Perform channel switch/CSA */
913 channel = dfs_get_valid_channel(iface, &secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700914 &oper_centr_freq_seg0_idx,
915 &oper_centr_freq_seg1_idx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800916 skip_radar);
917
918 if (!channel) {
Dmitry Shmidt98660862014-03-11 17:26:21 -0700919 /*
920 * If there is no channel to switch immediately to, check if
921 * there is another channel where we can switch even if it
922 * requires to perform a CAC first.
923 */
924 skip_radar = 0;
925 channel = dfs_get_valid_channel(iface, &secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700926 &oper_centr_freq_seg0_idx,
927 &oper_centr_freq_seg1_idx,
Dmitry Shmidt98660862014-03-11 17:26:21 -0700928 skip_radar);
929 if (!channel) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800930 wpa_printf(MSG_INFO,
931 "%s: no DFS channels left, waiting for NOP to finish",
932 __func__);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700933 return err;
934 }
935
936 iface->freq = channel->freq;
937 iface->conf->channel = channel->chan;
938 iface->conf->secondary_channel = secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -0700939 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
940 oper_centr_freq_seg0_idx);
941 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
942 oper_centr_freq_seg1_idx);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700943
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800944 hostapd_disable_iface(iface);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700945 hostapd_enable_iface(iface);
946 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800947 }
948
949 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
950 channel->chan);
951 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
952 "freq=%d chan=%d sec_chan=%d", channel->freq,
953 channel->chan, secondary_channel);
954
955 /* Setup CSA request */
956 os_memset(&csa_settings, 0, sizeof(csa_settings));
957 csa_settings.cs_count = 5;
958 csa_settings.block_tx = 1;
959 err = hostapd_set_freq_params(&csa_settings.freq_params,
960 iface->conf->hw_mode,
961 channel->freq,
962 channel->chan,
Hai Shalomc3565922019-10-28 11:58:20 -0700963 iface->conf->enable_edmg,
964 iface->conf->edmg_channel,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800965 iface->conf->ieee80211n,
966 iface->conf->ieee80211ac,
Hai Shalom81f62d82019-07-22 12:10:00 -0700967 iface->conf->ieee80211ax,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800968 secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700969 hostapd_get_oper_chwidth(iface->conf),
970 oper_centr_freq_seg0_idx,
971 oper_centr_freq_seg1_idx,
972 cmode->vht_capab,
973 &cmode->he_capab[IEEE80211_MODE_AP]);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800974
975 if (err) {
976 wpa_printf(MSG_ERROR, "DFS failed to calculate CSA freq params");
977 hostapd_disable_iface(iface);
978 return err;
979 }
980
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700981 for (i = 0; i < iface->num_bss; i++) {
982 err = hostapd_switch_channel(iface->bss[i], &csa_settings);
983 if (err)
984 break;
985 }
986
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800987 if (err) {
988 wpa_printf(MSG_WARNING, "DFS failed to schedule CSA (%d) - trying fallback",
989 err);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800990 iface->freq = channel->freq;
991 iface->conf->channel = channel->chan;
992 iface->conf->secondary_channel = secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -0700993 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
994 oper_centr_freq_seg0_idx);
995 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
996 oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700997
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800998 hostapd_disable_iface(iface);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800999 hostapd_enable_iface(iface);
1000 return 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001001 }
1002
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001003 /* Channel configuration will be updated once CSA completes and
1004 * ch_switch_notify event is received */
1005
1006 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
Dmitry Shmidt051af732013-10-22 13:52:46 -07001007 return 0;
1008}
1009
1010
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001011int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001012 int ht_enabled, int chan_offset, int chan_width,
1013 int cf1, int cf2)
1014{
1015 int res;
1016
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001017 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
1018 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1019 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1020
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001021 /* Proceed only if DFS is not offloaded to the driver */
1022 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1023 return 0;
1024
1025 if (!iface->conf->ieee80211h)
1026 return 0;
1027
Dmitry Shmidt051af732013-10-22 13:52:46 -07001028 /* mark radar frequency as invalid */
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001029 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1030 cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001031
1032 /* Skip if reported radar event not overlapped our channels */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001033 res = dfs_are_channels_overlapped(iface, freq, chan_width, cf1, cf2);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001034 if (!res)
1035 return 0;
1036
Dmitry Shmidt051af732013-10-22 13:52:46 -07001037 /* radar detected while operating, switch the channel. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001038 res = hostapd_dfs_start_channel_switch(iface);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001039
1040 return res;
1041}
1042
1043
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001044int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001045 int ht_enabled, int chan_offset, int chan_width,
1046 int cf1, int cf2)
1047{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001048 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
1049 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1050 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001051
1052 /* Proceed only if DFS is not offloaded to the driver */
1053 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1054 return 0;
1055
Dmitry Shmidt051af732013-10-22 13:52:46 -07001056 /* TODO add correct implementation here */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001057 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1058 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001059
1060 /* Handle cases where all channels were initially unavailable */
1061 if (iface->state == HAPD_IFACE_DFS && !iface->cac_started)
1062 hostapd_handle_dfs(iface);
1063
Dmitry Shmidt051af732013-10-22 13:52:46 -07001064 return 0;
1065}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001066
1067
1068int hostapd_is_dfs_required(struct hostapd_iface *iface)
1069{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001070 int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001071
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001072 if (!iface->conf->ieee80211h || !iface->current_mode ||
1073 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
1074 return 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001075
1076 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001077 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001078 if (start_chan_idx == -1)
1079 return -1;
1080
1081 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001082 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001083
1084 /* Check if any of configured channels require DFS */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001085 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
1086 if (res)
1087 return res;
1088 if (start_chan_idx1 >= 0 && n_chans1 > 0)
1089 res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1);
1090 return res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001091}
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001092
1093
1094int hostapd_dfs_start_cac(struct hostapd_iface *iface, int freq,
1095 int ht_enabled, int chan_offset, int chan_width,
1096 int cf1, int cf2)
1097{
Hai Shalomc3565922019-10-28 11:58:20 -07001098 /* This is called when the driver indicates that an offloaded DFS has
1099 * started CAC. */
1100 hostapd_set_state(iface, HAPD_IFACE_DFS);
1101 /* TODO: How to check CAC time for ETSI weather channels? */
1102 iface->dfs_cac_ms = 60000;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001103 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
1104 "freq=%d chan=%d chan_offset=%d width=%d seg0=%d "
1105 "seg1=%d cac_time=%ds",
Hai Shalomc3565922019-10-28 11:58:20 -07001106 freq, (freq - 5000) / 5, chan_offset, chan_width, cf1, cf2,
1107 iface->dfs_cac_ms / 1000);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001108 iface->cac_started = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07001109 os_get_reltime(&iface->dfs_cac_start);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001110 return 0;
1111}
1112
1113
1114/*
1115 * Main DFS handler for offloaded case.
1116 * 2 - continue channel/AP setup for non-DFS channel
1117 * 1 - continue channel/AP setup for DFS channel
1118 * 0 - channel/AP setup will be continued after CAC
1119 * -1 - hit critical error
1120 */
1121int hostapd_handle_dfs_offload(struct hostapd_iface *iface)
1122{
1123 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1124 __func__, iface->cac_started);
1125
1126 /*
1127 * If DFS has already been started, then we are being called from a
1128 * callback to continue AP/channel setup. Reset the CAC start flag and
1129 * return.
1130 */
1131 if (iface->cac_started) {
1132 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1133 __func__, iface->cac_started);
1134 iface->cac_started = 0;
1135 return 1;
1136 }
1137
Roshan Pius3a1667e2018-07-03 15:17:14 -07001138 if (ieee80211_is_dfs(iface->freq, iface->hw_features,
1139 iface->num_hw_features)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001140 wpa_printf(MSG_DEBUG, "%s: freq %d MHz requires DFS",
1141 __func__, iface->freq);
1142 return 0;
1143 }
1144
1145 wpa_printf(MSG_DEBUG,
1146 "%s: freq %d MHz does not require DFS. Continue channel/AP setup",
1147 __func__, iface->freq);
1148 return 2;
1149}