blob: fc8d7adf71df98c72a908166beb8a41faf9cbf2f [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>
4 * Copyright (c) 2013, Qualcomm Atheros, Inc.
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10#include "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 Shmidtcce06662013-11-04 18:44:24 -0800125static int dfs_chan_range_available(struct hostapd_hw_modes *mode,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800126 int first_chan_idx, int num_chans,
127 int skip_radar)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800128{
129 struct hostapd_channel_data *first_chan, *chan;
130 int i;
131
132 if (first_chan_idx + num_chans >= mode->num_channels)
133 return 0;
134
135 first_chan = &mode->channels[first_chan_idx];
136
137 for (i = 0; i < num_chans; i++) {
138 chan = &mode->channels[first_chan_idx + i];
139
140 if (first_chan->freq + i * 20 != chan->freq)
141 return 0;
142
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800143 if (!dfs_channel_available(chan, skip_radar))
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800144 return 0;
145 }
146
147 return 1;
148}
149
150
Dmitry Shmidt98660862014-03-11 17:26:21 -0700151static int is_in_chanlist(struct hostapd_iface *iface,
152 struct hostapd_channel_data *chan)
153{
154 int *entry;
155
156 if (!iface->conf->chanlist)
157 return 1;
158
159 for (entry = iface->conf->chanlist; *entry != -1; entry++) {
160 if (*entry == chan->chan)
161 return 1;
162 }
163 return 0;
164}
165
166
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800167/*
168 * The function assumes HT40+ operation.
169 * Make sure to adjust the following variables after calling this:
170 * - hapd->secondary_channel
171 * - hapd->vht_oper_centr_freq_seg0_idx
172 * - hapd->vht_oper_centr_freq_seg1_idx
173 */
174static int dfs_find_channel(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700175 struct hostapd_channel_data **ret_chan,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800176 int idx, int skip_radar)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700177{
178 struct hostapd_hw_modes *mode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800179 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700180 int i, channel_idx = 0, n_chans, n_chans1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700181
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800182 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700183 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700184
185 wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans);
186 for (i = 0; i < mode->num_channels; i++) {
187 chan = &mode->channels[i];
188
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800189 /* Skip HT40/VHT incompatible channels */
190 if (iface->conf->ieee80211n &&
191 iface->conf->secondary_channel &&
192 !dfs_is_chan_allowed(chan, n_chans))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700193 continue;
194
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800195 /* Skip incompatible chandefs */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800196 if (!dfs_chan_range_available(mode, i, n_chans, skip_radar))
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800197 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700198
Dmitry Shmidt98660862014-03-11 17:26:21 -0700199 if (!is_in_chanlist(iface, chan))
200 continue;
201
Dmitry Shmidt051af732013-10-22 13:52:46 -0700202 if (ret_chan && idx == channel_idx) {
203 wpa_printf(MSG_DEBUG, "Selected ch. #%d", chan->chan);
204 *ret_chan = chan;
205 return idx;
206 }
207 wpa_printf(MSG_DEBUG, "Adding channel: %d", chan->chan);
208 channel_idx++;
209 }
210 return channel_idx;
211}
212
213
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800214static void dfs_adjust_vht_center_freq(struct hostapd_iface *iface,
215 struct hostapd_channel_data *chan,
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800216 int secondary_channel,
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800217 u8 *vht_oper_centr_freq_seg0_idx,
218 u8 *vht_oper_centr_freq_seg1_idx)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700219{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800220 if (!iface->conf->ieee80211ac)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700221 return;
222
223 if (!chan)
224 return;
225
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800226 *vht_oper_centr_freq_seg1_idx = 0;
227
228 switch (iface->conf->vht_oper_chwidth) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700229 case VHT_CHANWIDTH_USE_HT:
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800230 if (secondary_channel == 1)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800231 *vht_oper_centr_freq_seg0_idx = chan->chan + 2;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800232 else if (secondary_channel == -1)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800233 *vht_oper_centr_freq_seg0_idx = chan->chan - 2;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700234 else
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800235 *vht_oper_centr_freq_seg0_idx = chan->chan;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700236 break;
237 case VHT_CHANWIDTH_80MHZ:
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800238 *vht_oper_centr_freq_seg0_idx = chan->chan + 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700239 break;
240 case VHT_CHANWIDTH_160MHZ:
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800241 *vht_oper_centr_freq_seg0_idx = chan->chan + 14;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700242 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700243 default:
244 wpa_printf(MSG_INFO, "DFS only VHT20/40/80/160 is supported now");
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800245 *vht_oper_centr_freq_seg0_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700246 break;
247 }
248
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800249 wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d, %d",
250 *vht_oper_centr_freq_seg0_idx,
251 *vht_oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700252}
253
254
255/* Return start channel idx we will use for mode->channels[idx] */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700256static int dfs_get_start_chan_idx(struct hostapd_iface *iface, int *seg1_start)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700257{
258 struct hostapd_hw_modes *mode;
259 struct hostapd_channel_data *chan;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800260 int channel_no = iface->conf->channel;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700261 int res = -1, i;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700262 int chan_seg1 = -1;
263
264 *seg1_start = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700265
266 /* HT40- */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800267 if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700268 channel_no -= 4;
269
270 /* VHT */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800271 if (iface->conf->ieee80211ac) {
272 switch (iface->conf->vht_oper_chwidth) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700273 case VHT_CHANWIDTH_USE_HT:
274 break;
275 case VHT_CHANWIDTH_80MHZ:
276 channel_no =
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800277 iface->conf->vht_oper_centr_freq_seg0_idx - 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700278 break;
279 case VHT_CHANWIDTH_160MHZ:
280 channel_no =
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800281 iface->conf->vht_oper_centr_freq_seg0_idx - 14;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700282 break;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700283 case VHT_CHANWIDTH_80P80MHZ:
284 channel_no =
285 iface->conf->vht_oper_centr_freq_seg0_idx - 6;
286 chan_seg1 =
287 iface->conf->vht_oper_centr_freq_seg1_idx - 6;
288 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700289 default:
290 wpa_printf(MSG_INFO,
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700291 "DFS only VHT20/40/80/160/80+80 is supported now");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700292 channel_no = -1;
293 break;
294 }
295 }
296
297 /* Get idx */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800298 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700299 for (i = 0; i < mode->num_channels; i++) {
300 chan = &mode->channels[i];
301 if (chan->chan == channel_no) {
302 res = i;
303 break;
304 }
305 }
306
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700307 if (res != -1 && chan_seg1 > -1) {
308 int found = 0;
309
310 /* Get idx for seg1 */
311 mode = iface->current_mode;
312 for (i = 0; i < mode->num_channels; i++) {
313 chan = &mode->channels[i];
314 if (chan->chan == chan_seg1) {
315 *seg1_start = i;
316 found = 1;
317 break;
318 }
319 }
320 if (!found)
321 res = -1;
322 }
323
Dmitry Shmidt98660862014-03-11 17:26:21 -0700324 if (res == -1) {
325 wpa_printf(MSG_DEBUG,
326 "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d",
327 mode->num_channels, channel_no, iface->conf->channel,
328 iface->conf->ieee80211n,
329 iface->conf->secondary_channel,
330 iface->conf->vht_oper_chwidth);
331
332 for (i = 0; i < mode->num_channels; i++) {
333 wpa_printf(MSG_DEBUG, "Available channel: %d",
334 mode->channels[i].chan);
335 }
336 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700337
338 return res;
339}
340
341
342/* At least one channel have radar flag */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800343static int dfs_check_chans_radar(struct hostapd_iface *iface,
344 int start_chan_idx, int n_chans)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700345{
346 struct hostapd_channel_data *channel;
347 struct hostapd_hw_modes *mode;
348 int i, res = 0;
349
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800350 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700351
352 for (i = 0; i < n_chans; i++) {
353 channel = &mode->channels[start_chan_idx + i];
354 if (channel->flag & HOSTAPD_CHAN_RADAR)
355 res++;
356 }
357
358 return res;
359}
360
361
362/* All channels available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800363static int dfs_check_chans_available(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700364 int start_chan_idx, int n_chans)
365{
366 struct hostapd_channel_data *channel;
367 struct hostapd_hw_modes *mode;
368 int i;
369
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800370 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700371
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800372 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700373 channel = &mode->channels[start_chan_idx + i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800374
375 if (channel->flag & HOSTAPD_CHAN_DISABLED)
376 break;
377
378 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
379 continue;
380
Dmitry Shmidt051af732013-10-22 13:52:46 -0700381 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
382 HOSTAPD_CHAN_DFS_AVAILABLE)
383 break;
384 }
385
386 return i == n_chans;
387}
388
389
390/* At least one channel unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800391static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700392 int start_chan_idx,
393 int n_chans)
394{
395 struct hostapd_channel_data *channel;
396 struct hostapd_hw_modes *mode;
397 int i, res = 0;
398
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800399 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700400
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800401 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700402 channel = &mode->channels[start_chan_idx + i];
403 if (channel->flag & HOSTAPD_CHAN_DISABLED)
404 res++;
405 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
406 HOSTAPD_CHAN_DFS_UNAVAILABLE)
407 res++;
408 }
409
410 return res;
411}
412
413
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800414static struct hostapd_channel_data *
415dfs_get_valid_channel(struct hostapd_iface *iface,
416 int *secondary_channel,
417 u8 *vht_oper_centr_freq_seg0_idx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800418 u8 *vht_oper_centr_freq_seg1_idx,
419 int skip_radar)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700420{
421 struct hostapd_hw_modes *mode;
422 struct hostapd_channel_data *chan = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800423 int num_available_chandefs;
424 int chan_idx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700425 u32 _rand;
426
427 wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800428 *secondary_channel = 0;
429 *vht_oper_centr_freq_seg0_idx = 0;
430 *vht_oper_centr_freq_seg1_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700431
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800432 if (iface->current_mode == NULL)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700433 return NULL;
434
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800435 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700436 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
437 return NULL;
438
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800439 /* Get the count first */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800440 num_available_chandefs = dfs_find_channel(iface, NULL, 0, skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800441 if (num_available_chandefs == 0)
442 return NULL;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700443
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800444 if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
445 _rand = os_random();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800446 chan_idx = _rand % num_available_chandefs;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800447 dfs_find_channel(iface, &chan, chan_idx, skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800448
449 /* dfs_find_channel() calculations assume HT40+ */
450 if (iface->conf->secondary_channel)
451 *secondary_channel = 1;
452 else
453 *secondary_channel = 0;
454
455 dfs_adjust_vht_center_freq(iface, chan,
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800456 *secondary_channel,
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800457 vht_oper_centr_freq_seg0_idx,
458 vht_oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700459
460 return chan;
461}
462
463
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800464static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700465{
466 struct hostapd_hw_modes *mode;
467 struct hostapd_channel_data *chan = NULL;
468 int i;
469
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800470 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700471 if (mode == NULL)
472 return 0;
473
474 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800475 for (i = 0; i < iface->current_mode->num_channels; i++) {
476 chan = &iface->current_mode->channels[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -0700477 if (chan->freq == freq) {
478 if (chan->flag & HOSTAPD_CHAN_RADAR) {
479 chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
480 chan->flag |= state;
481 return 1; /* Channel found */
482 }
483 }
484 }
485 wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
486 return 0;
487}
488
489
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800490static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700491 int chan_offset, int chan_width, int cf1,
492 int cf2, u32 state)
493{
494 int n_chans = 1, i;
495 struct hostapd_hw_modes *mode;
496 int frequency = freq;
497 int ret = 0;
498
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800499 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700500 if (mode == NULL)
501 return 0;
502
503 if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
504 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
505 return 0;
506 }
507
508 /* Seems cf1 and chan_width is enough here */
509 switch (chan_width) {
510 case CHAN_WIDTH_20_NOHT:
511 case CHAN_WIDTH_20:
512 n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800513 if (frequency == 0)
514 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700515 break;
516 case CHAN_WIDTH_40:
517 n_chans = 2;
518 frequency = cf1 - 10;
519 break;
520 case CHAN_WIDTH_80:
521 n_chans = 4;
522 frequency = cf1 - 30;
523 break;
524 case CHAN_WIDTH_160:
525 n_chans = 8;
526 frequency = cf1 - 70;
527 break;
528 default:
529 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
530 chan_width);
531 break;
532 }
533
534 wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
535 n_chans);
536 for (i = 0; i < n_chans; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800537 ret += set_dfs_state_freq(iface, frequency, state);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700538 frequency = frequency + 20;
539 }
540
541 return ret;
542}
543
544
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800545static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700546 int chan_width, int cf1, int cf2)
547{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700548 int start_chan_idx, start_chan_idx1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700549 struct hostapd_hw_modes *mode;
550 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700551 int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700552 u8 radar_chan;
553 int res = 0;
554
Dmitry Shmidt051af732013-10-22 13:52:46 -0700555 /* Our configuration */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800556 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700557 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
558 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700559
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700560 /* Check we are on DFS channel(s) */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800561 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700562 return 0;
563
Dmitry Shmidt051af732013-10-22 13:52:46 -0700564 /* Reported via radar event */
565 switch (chan_width) {
566 case CHAN_WIDTH_20_NOHT:
567 case CHAN_WIDTH_20:
568 radar_n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800569 if (frequency == 0)
570 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700571 break;
572 case CHAN_WIDTH_40:
573 radar_n_chans = 2;
574 frequency = cf1 - 10;
575 break;
576 case CHAN_WIDTH_80:
577 radar_n_chans = 4;
578 frequency = cf1 - 30;
579 break;
580 case CHAN_WIDTH_160:
581 radar_n_chans = 8;
582 frequency = cf1 - 70;
583 break;
584 default:
585 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
586 chan_width);
587 break;
588 }
589
590 ieee80211_freq_to_chan(frequency, &radar_chan);
591
592 for (i = 0; i < n_chans; i++) {
593 chan = &mode->channels[start_chan_idx + i];
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700594 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
595 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700596 for (j = 0; j < radar_n_chans; j++) {
597 wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
598 chan->chan, radar_chan + j * 4);
599 if (chan->chan == radar_chan + j * 4)
600 res++;
601 }
602 }
603
604 wpa_printf(MSG_DEBUG, "overlapped: %d", res);
605
606 return res;
607}
608
609
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700610static unsigned int dfs_get_cac_time(struct hostapd_iface *iface,
611 int start_chan_idx, int n_chans)
612{
613 struct hostapd_channel_data *channel;
614 struct hostapd_hw_modes *mode;
615 int i;
616 unsigned int cac_time_ms = 0;
617
618 mode = iface->current_mode;
619
620 for (i = 0; i < n_chans; i++) {
621 channel = &mode->channels[start_chan_idx + i];
622 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
623 continue;
624 if (channel->dfs_cac_ms > cac_time_ms)
625 cac_time_ms = channel->dfs_cac_ms;
626 }
627
628 return cac_time_ms;
629}
630
631
Dmitry Shmidt051af732013-10-22 13:52:46 -0700632/*
633 * Main DFS handler
634 * 1 - continue channel/ap setup
635 * 0 - channel/ap setup will be continued after CAC
636 * -1 - hit critical error
637 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800638int hostapd_handle_dfs(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700639{
640 struct hostapd_channel_data *channel;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700641 int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800642 int skip_radar = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700643
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800644 if (!iface->current_mode) {
645 /*
646 * This can happen with drivers that do not provide mode
647 * information and as such, cannot really use hostapd for DFS.
648 */
649 wpa_printf(MSG_DEBUG,
650 "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
651 return 1;
652 }
653
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800654 iface->cac_started = 0;
655
Dmitry Shmidt051af732013-10-22 13:52:46 -0700656 do {
657 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700658 start_chan_idx = dfs_get_start_chan_idx(iface,
659 &start_chan_idx1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700660 if (start_chan_idx == -1)
661 return -1;
662
663 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700664 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700665
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700666 /* Setup CAC time */
667 iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx,
668 n_chans);
669
Dmitry Shmidt051af732013-10-22 13:52:46 -0700670 /* Check if any of configured channels require DFS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800671 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700672 wpa_printf(MSG_DEBUG,
673 "DFS %d channels required radar detection",
674 res);
675 if (!res)
676 return 1;
677
678 /* Check if all channels are DFS available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800679 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700680 wpa_printf(MSG_DEBUG,
681 "DFS all channels available, (SKIP CAC): %s",
682 res ? "yes" : "no");
683 if (res)
684 return 1;
685
686 /* Check if any of configured channels is unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800687 res = dfs_check_chans_unavailable(iface, start_chan_idx,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700688 n_chans);
689 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
690 res, res ? "yes": "no");
691 if (res) {
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800692 int sec = 0;
693 u8 cf1 = 0, cf2 = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800694
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800695 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
696 skip_radar);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700697 if (!channel) {
698 wpa_printf(MSG_ERROR, "could not get valid channel");
699 return -1;
700 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800701
702 iface->freq = channel->freq;
703 iface->conf->channel = channel->chan;
704 iface->conf->secondary_channel = sec;
705 iface->conf->vht_oper_centr_freq_seg0_idx = cf1;
706 iface->conf->vht_oper_centr_freq_seg1_idx = cf2;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700707 }
708 } while (res);
709
710 /* Finally start CAC */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800711 hostapd_set_state(iface, HAPD_IFACE_DFS);
712 wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz", iface->freq);
713 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700714 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800715 iface->freq,
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800716 iface->conf->channel, iface->conf->secondary_channel,
717 iface->conf->vht_oper_chwidth,
718 iface->conf->vht_oper_centr_freq_seg0_idx,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700719 iface->conf->vht_oper_centr_freq_seg1_idx,
720 iface->dfs_cac_ms / 1000);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800721
722 res = hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
723 iface->freq,
724 iface->conf->channel,
725 iface->conf->ieee80211n,
726 iface->conf->ieee80211ac,
727 iface->conf->secondary_channel,
728 iface->conf->vht_oper_chwidth,
729 iface->conf->vht_oper_centr_freq_seg0_idx,
730 iface->conf->vht_oper_centr_freq_seg1_idx);
731
732 if (res) {
733 wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700734 return -1;
735 }
736
737 return 0;
738}
739
740
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800741int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700742 int ht_enabled, int chan_offset, int chan_width,
743 int cf1, int cf2)
744{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800745 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
746 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
747 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
748
Dmitry Shmidt051af732013-10-22 13:52:46 -0700749 if (success) {
750 /* Complete iface/ap configuration */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800751 set_dfs_state(iface, freq, ht_enabled, chan_offset,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700752 chan_width, cf1, cf2,
753 HOSTAPD_CHAN_DFS_AVAILABLE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800754 iface->cac_started = 0;
755 hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700756 }
757
758 return 0;
759}
760
761
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800762static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700763{
764 struct hostapd_channel_data *channel;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800765 int secondary_channel;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800766 u8 vht_oper_centr_freq_seg0_idx = 0;
767 u8 vht_oper_centr_freq_seg1_idx = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800768 int skip_radar = 0;
769 int err = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700770
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800771 /* Radar detected during active CAC */
772 iface->cac_started = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800773 channel = dfs_get_valid_channel(iface, &secondary_channel,
774 &vht_oper_centr_freq_seg0_idx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800775 &vht_oper_centr_freq_seg1_idx,
776 skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800777
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800778 if (!channel) {
779 wpa_printf(MSG_ERROR, "No valid channel available");
780 hostapd_setup_interface_complete(iface, err);
781 return err;
782 }
783
784 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
785 channel->chan);
786 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
787 "freq=%d chan=%d sec_chan=%d", channel->freq,
788 channel->chan, secondary_channel);
789
790 iface->freq = channel->freq;
791 iface->conf->channel = channel->chan;
792 iface->conf->secondary_channel = secondary_channel;
793 iface->conf->vht_oper_centr_freq_seg0_idx =
794 vht_oper_centr_freq_seg0_idx;
795 iface->conf->vht_oper_centr_freq_seg1_idx =
796 vht_oper_centr_freq_seg1_idx;
797 err = 0;
798
799 hostapd_setup_interface_complete(iface, err);
800 return err;
801}
802
803
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700804static int hostapd_csa_in_progress(struct hostapd_iface *iface)
805{
806 unsigned int i;
807 for (i = 0; i < iface->num_bss; i++)
808 if (iface->bss[i]->csa_in_progress)
809 return 1;
810 return 0;
811}
812
813
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800814static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
815{
816 struct hostapd_channel_data *channel;
817 int secondary_channel;
818 u8 vht_oper_centr_freq_seg0_idx;
819 u8 vht_oper_centr_freq_seg1_idx;
820 int skip_radar = 1;
821 struct csa_settings csa_settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700822 unsigned int i;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800823 int err = 1;
824
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800825 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
826 __func__, iface->cac_started ? "yes" : "no",
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700827 hostapd_csa_in_progress(iface) ? "yes" : "no");
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800828
829 /* Check if CSA in progress */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700830 if (hostapd_csa_in_progress(iface))
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800831 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800832
833 /* Check if active CAC */
834 if (iface->cac_started)
835 return hostapd_dfs_start_channel_switch_cac(iface);
836
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800837 /* Perform channel switch/CSA */
838 channel = dfs_get_valid_channel(iface, &secondary_channel,
839 &vht_oper_centr_freq_seg0_idx,
840 &vht_oper_centr_freq_seg1_idx,
841 skip_radar);
842
843 if (!channel) {
Dmitry Shmidt98660862014-03-11 17:26:21 -0700844 /*
845 * If there is no channel to switch immediately to, check if
846 * there is another channel where we can switch even if it
847 * requires to perform a CAC first.
848 */
849 skip_radar = 0;
850 channel = dfs_get_valid_channel(iface, &secondary_channel,
851 &vht_oper_centr_freq_seg0_idx,
852 &vht_oper_centr_freq_seg1_idx,
853 skip_radar);
854 if (!channel) {
855 /* FIXME: Wait for channel(s) to become available */
856 hostapd_disable_iface(iface);
857 return err;
858 }
859
860 iface->freq = channel->freq;
861 iface->conf->channel = channel->chan;
862 iface->conf->secondary_channel = secondary_channel;
863 iface->conf->vht_oper_centr_freq_seg0_idx =
864 vht_oper_centr_freq_seg0_idx;
865 iface->conf->vht_oper_centr_freq_seg1_idx =
866 vht_oper_centr_freq_seg1_idx;
867
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800868 hostapd_disable_iface(iface);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700869 hostapd_enable_iface(iface);
870 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800871 }
872
873 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
874 channel->chan);
875 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
876 "freq=%d chan=%d sec_chan=%d", channel->freq,
877 channel->chan, secondary_channel);
878
879 /* Setup CSA request */
880 os_memset(&csa_settings, 0, sizeof(csa_settings));
881 csa_settings.cs_count = 5;
882 csa_settings.block_tx = 1;
883 err = hostapd_set_freq_params(&csa_settings.freq_params,
884 iface->conf->hw_mode,
885 channel->freq,
886 channel->chan,
887 iface->conf->ieee80211n,
888 iface->conf->ieee80211ac,
889 secondary_channel,
890 iface->conf->vht_oper_chwidth,
891 vht_oper_centr_freq_seg0_idx,
892 vht_oper_centr_freq_seg1_idx,
893 iface->current_mode->vht_capab);
894
895 if (err) {
896 wpa_printf(MSG_ERROR, "DFS failed to calculate CSA freq params");
897 hostapd_disable_iface(iface);
898 return err;
899 }
900
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700901 for (i = 0; i < iface->num_bss; i++) {
902 err = hostapd_switch_channel(iface->bss[i], &csa_settings);
903 if (err)
904 break;
905 }
906
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800907 if (err) {
908 wpa_printf(MSG_WARNING, "DFS failed to schedule CSA (%d) - trying fallback",
909 err);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800910 iface->freq = channel->freq;
911 iface->conf->channel = channel->chan;
912 iface->conf->secondary_channel = secondary_channel;
913 iface->conf->vht_oper_centr_freq_seg0_idx =
914 vht_oper_centr_freq_seg0_idx;
915 iface->conf->vht_oper_centr_freq_seg1_idx =
916 vht_oper_centr_freq_seg1_idx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700917
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800918 hostapd_disable_iface(iface);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800919 hostapd_enable_iface(iface);
920 return 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800921 }
922
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800923 /* Channel configuration will be updated once CSA completes and
924 * ch_switch_notify event is received */
925
926 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700927 return 0;
928}
929
930
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800931int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700932 int ht_enabled, int chan_offset, int chan_width,
933 int cf1, int cf2)
934{
935 int res;
936
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800937 if (!iface->conf->ieee80211h)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700938 return 0;
939
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800940 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
941 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
942 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
943
Dmitry Shmidt051af732013-10-22 13:52:46 -0700944 /* mark radar frequency as invalid */
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -0700945 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
946 cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700947
948 /* Skip if reported radar event not overlapped our channels */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800949 res = dfs_are_channels_overlapped(iface, freq, chan_width, cf1, cf2);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700950 if (!res)
951 return 0;
952
Dmitry Shmidt051af732013-10-22 13:52:46 -0700953 /* radar detected while operating, switch the channel. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800954 res = hostapd_dfs_start_channel_switch(iface);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700955
956 return res;
957}
958
959
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800960int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700961 int ht_enabled, int chan_offset, int chan_width,
962 int cf1, int cf2)
963{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800964 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
965 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
966 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700967 /* TODO add correct implementation here */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800968 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
969 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700970 return 0;
971}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800972
973
974int hostapd_is_dfs_required(struct hostapd_iface *iface)
975{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700976 int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800977
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700978 if (!iface->conf->ieee80211h || !iface->current_mode ||
979 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
980 return 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800981
982 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700983 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800984 if (start_chan_idx == -1)
985 return -1;
986
987 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700988 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800989
990 /* Check if any of configured channels require DFS */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700991 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
992 if (res)
993 return res;
994 if (start_chan_idx1 >= 0 && n_chans1 > 0)
995 res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1);
996 return res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800997}