blob: 5a0d7814b79bec3a68850e9a86149e0a57f27b92 [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
Dmitry Shmidtcce06662013-11-04 18:44:24 -080031 if (iface->conf->ieee80211ac) {
32 switch (iface->conf->vht_oper_chwidth) {
Dmitry Shmidt051af732013-10-22 13:52:46 -070033 case VHT_CHANWIDTH_USE_HT:
34 break;
35 case VHT_CHANWIDTH_80MHZ:
36 n_chans = 4;
37 break;
38 case VHT_CHANWIDTH_160MHZ:
39 n_chans = 8;
40 break;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070041 case VHT_CHANWIDTH_80P80MHZ:
42 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;
145
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700146 if (first_chan_idx + num_chans > mode->num_channels)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800147 return 0;
148
149 first_chan = &mode->channels[first_chan_idx];
150
151 for (i = 0; i < num_chans; i++) {
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700152 chan = dfs_get_chan_data(mode, first_chan->freq + i * 20,
153 first_chan_idx);
154 if (!chan)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800155 return 0;
156
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800157 if (!dfs_channel_available(chan, skip_radar))
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800158 return 0;
159 }
160
161 return 1;
162}
163
164
Dmitry Shmidt98660862014-03-11 17:26:21 -0700165static int is_in_chanlist(struct hostapd_iface *iface,
166 struct hostapd_channel_data *chan)
167{
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700168 if (!iface->conf->acs_ch_list.num)
Dmitry Shmidt98660862014-03-11 17:26:21 -0700169 return 1;
170
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700171 return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700172}
173
174
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800175/*
176 * The function assumes HT40+ operation.
177 * Make sure to adjust the following variables after calling this:
178 * - hapd->secondary_channel
179 * - hapd->vht_oper_centr_freq_seg0_idx
180 * - hapd->vht_oper_centr_freq_seg1_idx
181 */
182static int dfs_find_channel(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700183 struct hostapd_channel_data **ret_chan,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800184 int idx, int skip_radar)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700185{
186 struct hostapd_hw_modes *mode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800187 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700188 int i, channel_idx = 0, n_chans, n_chans1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700189
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800190 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700191 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700192
193 wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans);
194 for (i = 0; i < mode->num_channels; i++) {
195 chan = &mode->channels[i];
196
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800197 /* Skip HT40/VHT incompatible channels */
198 if (iface->conf->ieee80211n &&
199 iface->conf->secondary_channel &&
200 !dfs_is_chan_allowed(chan, n_chans))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700201 continue;
202
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800203 /* Skip incompatible chandefs */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800204 if (!dfs_chan_range_available(mode, i, n_chans, skip_radar))
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800205 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700206
Dmitry Shmidt98660862014-03-11 17:26:21 -0700207 if (!is_in_chanlist(iface, chan))
208 continue;
209
Dmitry Shmidt051af732013-10-22 13:52:46 -0700210 if (ret_chan && idx == channel_idx) {
211 wpa_printf(MSG_DEBUG, "Selected ch. #%d", chan->chan);
212 *ret_chan = chan;
213 return idx;
214 }
215 wpa_printf(MSG_DEBUG, "Adding channel: %d", chan->chan);
216 channel_idx++;
217 }
218 return channel_idx;
219}
220
221
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800222static void dfs_adjust_vht_center_freq(struct hostapd_iface *iface,
223 struct hostapd_channel_data *chan,
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800224 int secondary_channel,
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800225 u8 *vht_oper_centr_freq_seg0_idx,
226 u8 *vht_oper_centr_freq_seg1_idx)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700227{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800228 if (!iface->conf->ieee80211ac)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700229 return;
230
231 if (!chan)
232 return;
233
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800234 *vht_oper_centr_freq_seg1_idx = 0;
235
236 switch (iface->conf->vht_oper_chwidth) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700237 case VHT_CHANWIDTH_USE_HT:
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800238 if (secondary_channel == 1)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800239 *vht_oper_centr_freq_seg0_idx = chan->chan + 2;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800240 else if (secondary_channel == -1)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800241 *vht_oper_centr_freq_seg0_idx = chan->chan - 2;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700242 else
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800243 *vht_oper_centr_freq_seg0_idx = chan->chan;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700244 break;
245 case VHT_CHANWIDTH_80MHZ:
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800246 *vht_oper_centr_freq_seg0_idx = chan->chan + 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700247 break;
248 case VHT_CHANWIDTH_160MHZ:
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800249 *vht_oper_centr_freq_seg0_idx = chan->chan + 14;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700250 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700251 default:
252 wpa_printf(MSG_INFO, "DFS only VHT20/40/80/160 is supported now");
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800253 *vht_oper_centr_freq_seg0_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700254 break;
255 }
256
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800257 wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d, %d",
258 *vht_oper_centr_freq_seg0_idx,
259 *vht_oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700260}
261
262
263/* Return start channel idx we will use for mode->channels[idx] */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700264static int dfs_get_start_chan_idx(struct hostapd_iface *iface, int *seg1_start)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700265{
266 struct hostapd_hw_modes *mode;
267 struct hostapd_channel_data *chan;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800268 int channel_no = iface->conf->channel;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700269 int res = -1, i;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700270 int chan_seg1 = -1;
271
272 *seg1_start = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700273
274 /* HT40- */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800275 if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700276 channel_no -= 4;
277
278 /* VHT */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800279 if (iface->conf->ieee80211ac) {
280 switch (iface->conf->vht_oper_chwidth) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700281 case VHT_CHANWIDTH_USE_HT:
282 break;
283 case VHT_CHANWIDTH_80MHZ:
284 channel_no =
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800285 iface->conf->vht_oper_centr_freq_seg0_idx - 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700286 break;
287 case VHT_CHANWIDTH_160MHZ:
288 channel_no =
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800289 iface->conf->vht_oper_centr_freq_seg0_idx - 14;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700290 break;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700291 case VHT_CHANWIDTH_80P80MHZ:
292 channel_no =
293 iface->conf->vht_oper_centr_freq_seg0_idx - 6;
294 chan_seg1 =
295 iface->conf->vht_oper_centr_freq_seg1_idx - 6;
296 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700297 default:
298 wpa_printf(MSG_INFO,
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700299 "DFS only VHT20/40/80/160/80+80 is supported now");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700300 channel_no = -1;
301 break;
302 }
303 }
304
305 /* Get idx */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800306 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700307 for (i = 0; i < mode->num_channels; i++) {
308 chan = &mode->channels[i];
309 if (chan->chan == channel_no) {
310 res = i;
311 break;
312 }
313 }
314
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700315 if (res != -1 && chan_seg1 > -1) {
316 int found = 0;
317
318 /* Get idx for seg1 */
319 mode = iface->current_mode;
320 for (i = 0; i < mode->num_channels; i++) {
321 chan = &mode->channels[i];
322 if (chan->chan == chan_seg1) {
323 *seg1_start = i;
324 found = 1;
325 break;
326 }
327 }
328 if (!found)
329 res = -1;
330 }
331
Dmitry Shmidt98660862014-03-11 17:26:21 -0700332 if (res == -1) {
333 wpa_printf(MSG_DEBUG,
334 "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d",
335 mode->num_channels, channel_no, iface->conf->channel,
336 iface->conf->ieee80211n,
337 iface->conf->secondary_channel,
338 iface->conf->vht_oper_chwidth);
339
340 for (i = 0; i < mode->num_channels; i++) {
341 wpa_printf(MSG_DEBUG, "Available channel: %d",
342 mode->channels[i].chan);
343 }
344 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700345
346 return res;
347}
348
349
350/* At least one channel have radar flag */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800351static int dfs_check_chans_radar(struct hostapd_iface *iface,
352 int start_chan_idx, int n_chans)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700353{
354 struct hostapd_channel_data *channel;
355 struct hostapd_hw_modes *mode;
356 int i, res = 0;
357
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800358 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700359
360 for (i = 0; i < n_chans; i++) {
361 channel = &mode->channels[start_chan_idx + i];
362 if (channel->flag & HOSTAPD_CHAN_RADAR)
363 res++;
364 }
365
366 return res;
367}
368
369
370/* All channels available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800371static int dfs_check_chans_available(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700372 int start_chan_idx, int n_chans)
373{
374 struct hostapd_channel_data *channel;
375 struct hostapd_hw_modes *mode;
376 int i;
377
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800378 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700379
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800380 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700381 channel = &mode->channels[start_chan_idx + i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800382
383 if (channel->flag & HOSTAPD_CHAN_DISABLED)
384 break;
385
386 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
387 continue;
388
Dmitry Shmidt051af732013-10-22 13:52:46 -0700389 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
390 HOSTAPD_CHAN_DFS_AVAILABLE)
391 break;
392 }
393
394 return i == n_chans;
395}
396
397
398/* At least one channel unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800399static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700400 int start_chan_idx,
401 int n_chans)
402{
403 struct hostapd_channel_data *channel;
404 struct hostapd_hw_modes *mode;
405 int i, res = 0;
406
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800407 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700408
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800409 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700410 channel = &mode->channels[start_chan_idx + i];
411 if (channel->flag & HOSTAPD_CHAN_DISABLED)
412 res++;
413 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
414 HOSTAPD_CHAN_DFS_UNAVAILABLE)
415 res++;
416 }
417
418 return res;
419}
420
421
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800422static struct hostapd_channel_data *
423dfs_get_valid_channel(struct hostapd_iface *iface,
424 int *secondary_channel,
425 u8 *vht_oper_centr_freq_seg0_idx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800426 u8 *vht_oper_centr_freq_seg1_idx,
427 int skip_radar)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700428{
429 struct hostapd_hw_modes *mode;
430 struct hostapd_channel_data *chan = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800431 int num_available_chandefs;
432 int chan_idx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700433 u32 _rand;
434
435 wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800436 *secondary_channel = 0;
437 *vht_oper_centr_freq_seg0_idx = 0;
438 *vht_oper_centr_freq_seg1_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700439
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800440 if (iface->current_mode == NULL)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700441 return NULL;
442
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800443 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700444 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
445 return NULL;
446
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800447 /* Get the count first */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800448 num_available_chandefs = dfs_find_channel(iface, NULL, 0, skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800449 if (num_available_chandefs == 0)
450 return NULL;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700451
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800452 if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800453 return NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800454 chan_idx = _rand % num_available_chandefs;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800455 dfs_find_channel(iface, &chan, chan_idx, skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800456
457 /* dfs_find_channel() calculations assume HT40+ */
458 if (iface->conf->secondary_channel)
459 *secondary_channel = 1;
460 else
461 *secondary_channel = 0;
462
463 dfs_adjust_vht_center_freq(iface, chan,
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800464 *secondary_channel,
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800465 vht_oper_centr_freq_seg0_idx,
466 vht_oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700467
468 return chan;
469}
470
471
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800472static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700473{
474 struct hostapd_hw_modes *mode;
475 struct hostapd_channel_data *chan = NULL;
476 int i;
477
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800478 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700479 if (mode == NULL)
480 return 0;
481
482 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800483 for (i = 0; i < iface->current_mode->num_channels; i++) {
484 chan = &iface->current_mode->channels[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -0700485 if (chan->freq == freq) {
486 if (chan->flag & HOSTAPD_CHAN_RADAR) {
487 chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
488 chan->flag |= state;
489 return 1; /* Channel found */
490 }
491 }
492 }
493 wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
494 return 0;
495}
496
497
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800498static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700499 int chan_offset, int chan_width, int cf1,
500 int cf2, u32 state)
501{
502 int n_chans = 1, i;
503 struct hostapd_hw_modes *mode;
504 int frequency = freq;
505 int ret = 0;
506
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800507 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700508 if (mode == NULL)
509 return 0;
510
511 if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
512 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
513 return 0;
514 }
515
516 /* Seems cf1 and chan_width is enough here */
517 switch (chan_width) {
518 case CHAN_WIDTH_20_NOHT:
519 case CHAN_WIDTH_20:
520 n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800521 if (frequency == 0)
522 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700523 break;
524 case CHAN_WIDTH_40:
525 n_chans = 2;
526 frequency = cf1 - 10;
527 break;
528 case CHAN_WIDTH_80:
529 n_chans = 4;
530 frequency = cf1 - 30;
531 break;
532 case CHAN_WIDTH_160:
533 n_chans = 8;
534 frequency = cf1 - 70;
535 break;
536 default:
537 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
538 chan_width);
539 break;
540 }
541
542 wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
543 n_chans);
544 for (i = 0; i < n_chans; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800545 ret += set_dfs_state_freq(iface, frequency, state);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700546 frequency = frequency + 20;
547 }
548
549 return ret;
550}
551
552
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800553static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700554 int chan_width, int cf1, int cf2)
555{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700556 int start_chan_idx, start_chan_idx1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700557 struct hostapd_hw_modes *mode;
558 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700559 int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700560 u8 radar_chan;
561 int res = 0;
562
Dmitry Shmidt051af732013-10-22 13:52:46 -0700563 /* Our configuration */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800564 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700565 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
566 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700567
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700568 /* Check we are on DFS channel(s) */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800569 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700570 return 0;
571
Dmitry Shmidt051af732013-10-22 13:52:46 -0700572 /* Reported via radar event */
573 switch (chan_width) {
574 case CHAN_WIDTH_20_NOHT:
575 case CHAN_WIDTH_20:
576 radar_n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800577 if (frequency == 0)
578 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700579 break;
580 case CHAN_WIDTH_40:
581 radar_n_chans = 2;
582 frequency = cf1 - 10;
583 break;
584 case CHAN_WIDTH_80:
585 radar_n_chans = 4;
586 frequency = cf1 - 30;
587 break;
588 case CHAN_WIDTH_160:
589 radar_n_chans = 8;
590 frequency = cf1 - 70;
591 break;
592 default:
593 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
594 chan_width);
595 break;
596 }
597
598 ieee80211_freq_to_chan(frequency, &radar_chan);
599
600 for (i = 0; i < n_chans; i++) {
601 chan = &mode->channels[start_chan_idx + i];
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700602 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
603 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700604 for (j = 0; j < radar_n_chans; j++) {
605 wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
606 chan->chan, radar_chan + j * 4);
607 if (chan->chan == radar_chan + j * 4)
608 res++;
609 }
610 }
611
612 wpa_printf(MSG_DEBUG, "overlapped: %d", res);
613
614 return res;
615}
616
617
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700618static unsigned int dfs_get_cac_time(struct hostapd_iface *iface,
619 int start_chan_idx, int n_chans)
620{
621 struct hostapd_channel_data *channel;
622 struct hostapd_hw_modes *mode;
623 int i;
624 unsigned int cac_time_ms = 0;
625
626 mode = iface->current_mode;
627
628 for (i = 0; i < n_chans; i++) {
629 channel = &mode->channels[start_chan_idx + i];
630 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
631 continue;
632 if (channel->dfs_cac_ms > cac_time_ms)
633 cac_time_ms = channel->dfs_cac_ms;
634 }
635
636 return cac_time_ms;
637}
638
639
Dmitry Shmidt051af732013-10-22 13:52:46 -0700640/*
641 * Main DFS handler
642 * 1 - continue channel/ap setup
643 * 0 - channel/ap setup will be continued after CAC
644 * -1 - hit critical error
645 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800646int hostapd_handle_dfs(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700647{
648 struct hostapd_channel_data *channel;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700649 int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800650 int skip_radar = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700651
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800652 if (!iface->current_mode) {
653 /*
654 * This can happen with drivers that do not provide mode
655 * information and as such, cannot really use hostapd for DFS.
656 */
657 wpa_printf(MSG_DEBUG,
658 "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
659 return 1;
660 }
661
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800662 iface->cac_started = 0;
663
Dmitry Shmidt051af732013-10-22 13:52:46 -0700664 do {
665 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700666 start_chan_idx = dfs_get_start_chan_idx(iface,
667 &start_chan_idx1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700668 if (start_chan_idx == -1)
669 return -1;
670
671 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700672 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700673
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700674 /* Setup CAC time */
675 iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx,
676 n_chans);
677
Dmitry Shmidt051af732013-10-22 13:52:46 -0700678 /* Check if any of configured channels require DFS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800679 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700680 wpa_printf(MSG_DEBUG,
681 "DFS %d channels required radar detection",
682 res);
683 if (!res)
684 return 1;
685
686 /* Check if all channels are DFS available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800687 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700688 wpa_printf(MSG_DEBUG,
689 "DFS all channels available, (SKIP CAC): %s",
690 res ? "yes" : "no");
691 if (res)
692 return 1;
693
694 /* Check if any of configured channels is unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800695 res = dfs_check_chans_unavailable(iface, start_chan_idx,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700696 n_chans);
697 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
698 res, res ? "yes": "no");
699 if (res) {
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800700 int sec = 0;
701 u8 cf1 = 0, cf2 = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800702
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800703 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
704 skip_radar);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700705 if (!channel) {
706 wpa_printf(MSG_ERROR, "could not get valid channel");
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800707 hostapd_set_state(iface, HAPD_IFACE_DFS);
708 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700709 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800710
711 iface->freq = channel->freq;
712 iface->conf->channel = channel->chan;
713 iface->conf->secondary_channel = sec;
714 iface->conf->vht_oper_centr_freq_seg0_idx = cf1;
715 iface->conf->vht_oper_centr_freq_seg1_idx = cf2;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700716 }
717 } while (res);
718
719 /* Finally start CAC */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800720 hostapd_set_state(iface, HAPD_IFACE_DFS);
721 wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz", iface->freq);
722 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700723 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800724 iface->freq,
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800725 iface->conf->channel, iface->conf->secondary_channel,
726 iface->conf->vht_oper_chwidth,
727 iface->conf->vht_oper_centr_freq_seg0_idx,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700728 iface->conf->vht_oper_centr_freq_seg1_idx,
729 iface->dfs_cac_ms / 1000);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800730
731 res = hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
732 iface->freq,
733 iface->conf->channel,
734 iface->conf->ieee80211n,
735 iface->conf->ieee80211ac,
736 iface->conf->secondary_channel,
737 iface->conf->vht_oper_chwidth,
738 iface->conf->vht_oper_centr_freq_seg0_idx,
739 iface->conf->vht_oper_centr_freq_seg1_idx);
740
741 if (res) {
742 wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700743 return -1;
744 }
745
746 return 0;
747}
748
749
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700750static int hostapd_config_dfs_chan_available(struct hostapd_iface *iface)
751{
752 int n_chans, n_chans1, start_chan_idx, start_chan_idx1;
753
754 /* Get the start (first) channel for current configuration */
755 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
756 if (start_chan_idx < 0)
757 return 0;
758
759 /* Get the number of used channels, depending on width */
760 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
761
762 /* Check if all channels are DFS available */
763 return dfs_check_chans_available(iface, start_chan_idx, n_chans);
764}
765
766
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800767int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700768 int ht_enabled, int chan_offset, int chan_width,
769 int cf1, int cf2)
770{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800771 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
772 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
773 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
774
Dmitry Shmidt051af732013-10-22 13:52:46 -0700775 if (success) {
776 /* Complete iface/ap configuration */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800777 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
778 /* Complete AP configuration for the first bring up. */
779 if (iface->state != HAPD_IFACE_ENABLED)
780 hostapd_setup_interface_complete(iface, 0);
781 else
782 iface->cac_started = 0;
783 } else {
784 set_dfs_state(iface, freq, ht_enabled, chan_offset,
785 chan_width, cf1, cf2,
786 HOSTAPD_CHAN_DFS_AVAILABLE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700787 /*
788 * Just mark the channel available when CAC completion
789 * event is received in enabled state. CAC result could
790 * have been propagated from another radio having the
791 * same regulatory configuration. When CAC completion is
792 * received during non-HAPD_IFACE_ENABLED state, make
793 * sure the configured channel is available because this
794 * CAC completion event could have been propagated from
795 * another radio.
796 */
797 if (iface->state != HAPD_IFACE_ENABLED &&
798 hostapd_config_dfs_chan_available(iface)) {
799 hostapd_setup_interface_complete(iface, 0);
800 iface->cac_started = 0;
801 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800802 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700803 }
804
805 return 0;
806}
807
808
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700809int hostapd_dfs_pre_cac_expired(struct hostapd_iface *iface, int freq,
810 int ht_enabled, int chan_offset, int chan_width,
811 int cf1, int cf2)
812{
813 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_PRE_CAC_EXPIRED
814 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
815 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
816
817 /* Proceed only if DFS is not offloaded to the driver */
818 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
819 return 0;
820
821 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
822 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
823
824 return 0;
825}
826
827
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800828static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700829{
830 struct hostapd_channel_data *channel;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800831 int secondary_channel;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800832 u8 vht_oper_centr_freq_seg0_idx = 0;
833 u8 vht_oper_centr_freq_seg1_idx = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800834 int skip_radar = 0;
835 int err = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700836
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800837 /* Radar detected during active CAC */
838 iface->cac_started = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800839 channel = dfs_get_valid_channel(iface, &secondary_channel,
840 &vht_oper_centr_freq_seg0_idx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800841 &vht_oper_centr_freq_seg1_idx,
842 skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800843
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800844 if (!channel) {
845 wpa_printf(MSG_ERROR, "No valid channel available");
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800846 return err;
847 }
848
849 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
850 channel->chan);
851 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
852 "freq=%d chan=%d sec_chan=%d", channel->freq,
853 channel->chan, secondary_channel);
854
855 iface->freq = channel->freq;
856 iface->conf->channel = channel->chan;
857 iface->conf->secondary_channel = secondary_channel;
858 iface->conf->vht_oper_centr_freq_seg0_idx =
859 vht_oper_centr_freq_seg0_idx;
860 iface->conf->vht_oper_centr_freq_seg1_idx =
861 vht_oper_centr_freq_seg1_idx;
862 err = 0;
863
864 hostapd_setup_interface_complete(iface, err);
865 return err;
866}
867
868
869static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
870{
871 struct hostapd_channel_data *channel;
872 int secondary_channel;
873 u8 vht_oper_centr_freq_seg0_idx;
874 u8 vht_oper_centr_freq_seg1_idx;
875 int skip_radar = 1;
876 struct csa_settings csa_settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700877 unsigned int i;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800878 int err = 1;
879
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800880 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
881 __func__, iface->cac_started ? "yes" : "no",
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700882 hostapd_csa_in_progress(iface) ? "yes" : "no");
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800883
884 /* Check if CSA in progress */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700885 if (hostapd_csa_in_progress(iface))
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800886 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800887
888 /* Check if active CAC */
889 if (iface->cac_started)
890 return hostapd_dfs_start_channel_switch_cac(iface);
891
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700892 /*
893 * Allow selection of DFS channel in ETSI to comply with
894 * uniform spreading.
895 */
896 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
897 skip_radar = 0;
898
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800899 /* Perform channel switch/CSA */
900 channel = dfs_get_valid_channel(iface, &secondary_channel,
901 &vht_oper_centr_freq_seg0_idx,
902 &vht_oper_centr_freq_seg1_idx,
903 skip_radar);
904
905 if (!channel) {
Dmitry Shmidt98660862014-03-11 17:26:21 -0700906 /*
907 * If there is no channel to switch immediately to, check if
908 * there is another channel where we can switch even if it
909 * requires to perform a CAC first.
910 */
911 skip_radar = 0;
912 channel = dfs_get_valid_channel(iface, &secondary_channel,
913 &vht_oper_centr_freq_seg0_idx,
914 &vht_oper_centr_freq_seg1_idx,
915 skip_radar);
916 if (!channel) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800917 wpa_printf(MSG_INFO,
918 "%s: no DFS channels left, waiting for NOP to finish",
919 __func__);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700920 return err;
921 }
922
923 iface->freq = channel->freq;
924 iface->conf->channel = channel->chan;
925 iface->conf->secondary_channel = secondary_channel;
926 iface->conf->vht_oper_centr_freq_seg0_idx =
927 vht_oper_centr_freq_seg0_idx;
928 iface->conf->vht_oper_centr_freq_seg1_idx =
929 vht_oper_centr_freq_seg1_idx;
930
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800931 hostapd_disable_iface(iface);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700932 hostapd_enable_iface(iface);
933 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800934 }
935
936 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
937 channel->chan);
938 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
939 "freq=%d chan=%d sec_chan=%d", channel->freq,
940 channel->chan, secondary_channel);
941
942 /* Setup CSA request */
943 os_memset(&csa_settings, 0, sizeof(csa_settings));
944 csa_settings.cs_count = 5;
945 csa_settings.block_tx = 1;
946 err = hostapd_set_freq_params(&csa_settings.freq_params,
947 iface->conf->hw_mode,
948 channel->freq,
949 channel->chan,
950 iface->conf->ieee80211n,
951 iface->conf->ieee80211ac,
952 secondary_channel,
953 iface->conf->vht_oper_chwidth,
954 vht_oper_centr_freq_seg0_idx,
955 vht_oper_centr_freq_seg1_idx,
956 iface->current_mode->vht_capab);
957
958 if (err) {
959 wpa_printf(MSG_ERROR, "DFS failed to calculate CSA freq params");
960 hostapd_disable_iface(iface);
961 return err;
962 }
963
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700964 for (i = 0; i < iface->num_bss; i++) {
965 err = hostapd_switch_channel(iface->bss[i], &csa_settings);
966 if (err)
967 break;
968 }
969
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800970 if (err) {
971 wpa_printf(MSG_WARNING, "DFS failed to schedule CSA (%d) - trying fallback",
972 err);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800973 iface->freq = channel->freq;
974 iface->conf->channel = channel->chan;
975 iface->conf->secondary_channel = secondary_channel;
976 iface->conf->vht_oper_centr_freq_seg0_idx =
977 vht_oper_centr_freq_seg0_idx;
978 iface->conf->vht_oper_centr_freq_seg1_idx =
979 vht_oper_centr_freq_seg1_idx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700980
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800981 hostapd_disable_iface(iface);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800982 hostapd_enable_iface(iface);
983 return 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800984 }
985
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800986 /* Channel configuration will be updated once CSA completes and
987 * ch_switch_notify event is received */
988
989 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700990 return 0;
991}
992
993
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800994int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700995 int ht_enabled, int chan_offset, int chan_width,
996 int cf1, int cf2)
997{
998 int res;
999
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001000 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
1001 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1002 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1003
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001004 /* Proceed only if DFS is not offloaded to the driver */
1005 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1006 return 0;
1007
1008 if (!iface->conf->ieee80211h)
1009 return 0;
1010
Dmitry Shmidt051af732013-10-22 13:52:46 -07001011 /* mark radar frequency as invalid */
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001012 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1013 cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001014
1015 /* Skip if reported radar event not overlapped our channels */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001016 res = dfs_are_channels_overlapped(iface, freq, chan_width, cf1, cf2);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001017 if (!res)
1018 return 0;
1019
Dmitry Shmidt051af732013-10-22 13:52:46 -07001020 /* radar detected while operating, switch the channel. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001021 res = hostapd_dfs_start_channel_switch(iface);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001022
1023 return res;
1024}
1025
1026
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001027int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001028 int ht_enabled, int chan_offset, int chan_width,
1029 int cf1, int cf2)
1030{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001031 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
1032 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1033 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001034
1035 /* Proceed only if DFS is not offloaded to the driver */
1036 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1037 return 0;
1038
Dmitry Shmidt051af732013-10-22 13:52:46 -07001039 /* TODO add correct implementation here */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001040 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1041 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001042
1043 /* Handle cases where all channels were initially unavailable */
1044 if (iface->state == HAPD_IFACE_DFS && !iface->cac_started)
1045 hostapd_handle_dfs(iface);
1046
Dmitry Shmidt051af732013-10-22 13:52:46 -07001047 return 0;
1048}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001049
1050
1051int hostapd_is_dfs_required(struct hostapd_iface *iface)
1052{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001053 int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001054
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001055 if (!iface->conf->ieee80211h || !iface->current_mode ||
1056 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
1057 return 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001058
1059 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001060 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001061 if (start_chan_idx == -1)
1062 return -1;
1063
1064 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001065 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001066
1067 /* Check if any of configured channels require DFS */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001068 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
1069 if (res)
1070 return res;
1071 if (start_chan_idx1 >= 0 && n_chans1 > 0)
1072 res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1);
1073 return res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001074}
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001075
1076
1077int hostapd_dfs_start_cac(struct hostapd_iface *iface, int freq,
1078 int ht_enabled, int chan_offset, int chan_width,
1079 int cf1, int cf2)
1080{
1081 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
1082 "freq=%d chan=%d chan_offset=%d width=%d seg0=%d "
1083 "seg1=%d cac_time=%ds",
1084 freq, (freq - 5000) / 5, chan_offset, chan_width, cf1, cf2, 60);
1085 iface->cac_started = 1;
1086 return 0;
1087}
1088
1089
1090/*
1091 * Main DFS handler for offloaded case.
1092 * 2 - continue channel/AP setup for non-DFS channel
1093 * 1 - continue channel/AP setup for DFS channel
1094 * 0 - channel/AP setup will be continued after CAC
1095 * -1 - hit critical error
1096 */
1097int hostapd_handle_dfs_offload(struct hostapd_iface *iface)
1098{
1099 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1100 __func__, iface->cac_started);
1101
1102 /*
1103 * If DFS has already been started, then we are being called from a
1104 * callback to continue AP/channel setup. Reset the CAC start flag and
1105 * return.
1106 */
1107 if (iface->cac_started) {
1108 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1109 __func__, iface->cac_started);
1110 iface->cac_started = 0;
1111 return 1;
1112 }
1113
1114 if (ieee80211_is_dfs(iface->freq)) {
1115 wpa_printf(MSG_DEBUG, "%s: freq %d MHz requires DFS",
1116 __func__, iface->freq);
1117 return 0;
1118 }
1119
1120 wpa_printf(MSG_DEBUG,
1121 "%s: freq %d MHz does not require DFS. Continue channel/AP setup",
1122 __func__, iface->freq);
1123 return 2;
1124}