blob: a6ec20bd92633e553ad292db8c4797d61ab8eb2e [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 Shmidtcce06662013-11-04 18:44:24 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070015#include "hostapd.h"
16#include "ap_drv_ops.h"
17#include "drivers/driver.h"
18#include "dfs.h"
19
20
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070021static int dfs_get_used_n_chans(struct hostapd_iface *iface, int *seg1)
Dmitry Shmidt051af732013-10-22 13:52:46 -070022{
23 int n_chans = 1;
24
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070025 *seg1 = 0;
26
Dmitry Shmidtcce06662013-11-04 18:44:24 -080027 if (iface->conf->ieee80211n && iface->conf->secondary_channel)
Dmitry Shmidt051af732013-10-22 13:52:46 -070028 n_chans = 2;
29
Dmitry Shmidtcce06662013-11-04 18:44:24 -080030 if (iface->conf->ieee80211ac) {
31 switch (iface->conf->vht_oper_chwidth) {
Dmitry Shmidt051af732013-10-22 13:52:46 -070032 case VHT_CHANWIDTH_USE_HT:
33 break;
34 case VHT_CHANWIDTH_80MHZ:
35 n_chans = 4;
36 break;
37 case VHT_CHANWIDTH_160MHZ:
38 n_chans = 8;
39 break;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070040 case VHT_CHANWIDTH_80P80MHZ:
41 n_chans = 4;
42 *seg1 = 4;
43 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -070044 default:
45 break;
46 }
47 }
48
49 return n_chans;
50}
51
52
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080053static int dfs_channel_available(struct hostapd_channel_data *chan,
54 int skip_radar)
Dmitry Shmidt051af732013-10-22 13:52:46 -070055{
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080056 /*
57 * When radar detection happens, CSA is performed. However, there's no
58 * time for CAC, so radar channels must be skipped when finding a new
Dmitry Shmidt98660862014-03-11 17:26:21 -070059 * channel for CSA, unless they are available for immediate use.
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080060 */
Dmitry Shmidt98660862014-03-11 17:26:21 -070061 if (skip_radar && (chan->flag & HOSTAPD_CHAN_RADAR) &&
62 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) !=
63 HOSTAPD_CHAN_DFS_AVAILABLE))
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080064 return 0;
65
Dmitry Shmidt051af732013-10-22 13:52:46 -070066 if (chan->flag & HOSTAPD_CHAN_DISABLED)
67 return 0;
68 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
69 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
70 HOSTAPD_CHAN_DFS_UNAVAILABLE))
71 return 0;
72 return 1;
73}
74
75
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070076static int dfs_is_chan_allowed(struct hostapd_channel_data *chan, int n_chans)
Dmitry Shmidt051af732013-10-22 13:52:46 -070077{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070078 /*
79 * The tables contain first valid channel number based on channel width.
80 * We will also choose this first channel as the control one.
81 */
82 int allowed_40[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
83 184, 192 };
84 /*
85 * VHT80, valid channels based on center frequency:
86 * 42, 58, 106, 122, 138, 155
87 */
88 int allowed_80[] = { 36, 52, 100, 116, 132, 149 };
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080089 /*
90 * VHT160 valid channels based on center frequency:
91 * 50, 114
92 */
93 int allowed_160[] = { 36, 100 };
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070094 int *allowed = allowed_40;
95 unsigned int i, allowed_no = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -070096
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070097 switch (n_chans) {
98 case 2:
99 allowed = allowed_40;
100 allowed_no = ARRAY_SIZE(allowed_40);
101 break;
102 case 4:
103 allowed = allowed_80;
104 allowed_no = ARRAY_SIZE(allowed_80);
105 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800106 case 8:
107 allowed = allowed_160;
108 allowed_no = ARRAY_SIZE(allowed_160);
109 break;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700110 default:
111 wpa_printf(MSG_DEBUG, "Unknown width for %d channels", n_chans);
112 break;
113 }
114
115 for (i = 0; i < allowed_no; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700116 if (chan->chan == allowed[i])
117 return 1;
118 }
119
120 return 0;
121}
122
123
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800124static int dfs_chan_range_available(struct hostapd_hw_modes *mode,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800125 int first_chan_idx, int num_chans,
126 int skip_radar)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800127{
128 struct hostapd_channel_data *first_chan, *chan;
129 int i;
130
131 if (first_chan_idx + num_chans >= mode->num_channels)
132 return 0;
133
134 first_chan = &mode->channels[first_chan_idx];
135
136 for (i = 0; i < num_chans; i++) {
137 chan = &mode->channels[first_chan_idx + i];
138
139 if (first_chan->freq + i * 20 != chan->freq)
140 return 0;
141
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800142 if (!dfs_channel_available(chan, skip_radar))
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800143 return 0;
144 }
145
146 return 1;
147}
148
149
Dmitry Shmidt98660862014-03-11 17:26:21 -0700150static int is_in_chanlist(struct hostapd_iface *iface,
151 struct hostapd_channel_data *chan)
152{
153 int *entry;
154
155 if (!iface->conf->chanlist)
156 return 1;
157
158 for (entry = iface->conf->chanlist; *entry != -1; entry++) {
159 if (*entry == chan->chan)
160 return 1;
161 }
162 return 0;
163}
164
165
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800166/*
167 * The function assumes HT40+ operation.
168 * Make sure to adjust the following variables after calling this:
169 * - hapd->secondary_channel
170 * - hapd->vht_oper_centr_freq_seg0_idx
171 * - hapd->vht_oper_centr_freq_seg1_idx
172 */
173static int dfs_find_channel(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700174 struct hostapd_channel_data **ret_chan,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800175 int idx, int skip_radar)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700176{
177 struct hostapd_hw_modes *mode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800178 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700179 int i, channel_idx = 0, n_chans, n_chans1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700180
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800181 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700182 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700183
184 wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans);
185 for (i = 0; i < mode->num_channels; i++) {
186 chan = &mode->channels[i];
187
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800188 /* Skip HT40/VHT incompatible channels */
189 if (iface->conf->ieee80211n &&
190 iface->conf->secondary_channel &&
191 !dfs_is_chan_allowed(chan, n_chans))
Dmitry Shmidt051af732013-10-22 13:52:46 -0700192 continue;
193
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800194 /* Skip incompatible chandefs */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800195 if (!dfs_chan_range_available(mode, i, n_chans, skip_radar))
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800196 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700197
Dmitry Shmidt98660862014-03-11 17:26:21 -0700198 if (!is_in_chanlist(iface, chan))
199 continue;
200
Dmitry Shmidt051af732013-10-22 13:52:46 -0700201 if (ret_chan && idx == channel_idx) {
202 wpa_printf(MSG_DEBUG, "Selected ch. #%d", chan->chan);
203 *ret_chan = chan;
204 return idx;
205 }
206 wpa_printf(MSG_DEBUG, "Adding channel: %d", chan->chan);
207 channel_idx++;
208 }
209 return channel_idx;
210}
211
212
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800213static void dfs_adjust_vht_center_freq(struct hostapd_iface *iface,
214 struct hostapd_channel_data *chan,
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800215 int secondary_channel,
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800216 u8 *vht_oper_centr_freq_seg0_idx,
217 u8 *vht_oper_centr_freq_seg1_idx)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700218{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800219 if (!iface->conf->ieee80211ac)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700220 return;
221
222 if (!chan)
223 return;
224
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800225 *vht_oper_centr_freq_seg1_idx = 0;
226
227 switch (iface->conf->vht_oper_chwidth) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700228 case VHT_CHANWIDTH_USE_HT:
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800229 if (secondary_channel == 1)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800230 *vht_oper_centr_freq_seg0_idx = chan->chan + 2;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800231 else if (secondary_channel == -1)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800232 *vht_oper_centr_freq_seg0_idx = chan->chan - 2;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700233 else
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800234 *vht_oper_centr_freq_seg0_idx = chan->chan;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700235 break;
236 case VHT_CHANWIDTH_80MHZ:
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800237 *vht_oper_centr_freq_seg0_idx = chan->chan + 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700238 break;
239 case VHT_CHANWIDTH_160MHZ:
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800240 *vht_oper_centr_freq_seg0_idx = chan->chan + 14;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700241 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700242 default:
243 wpa_printf(MSG_INFO, "DFS only VHT20/40/80/160 is supported now");
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800244 *vht_oper_centr_freq_seg0_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700245 break;
246 }
247
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800248 wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d, %d",
249 *vht_oper_centr_freq_seg0_idx,
250 *vht_oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700251}
252
253
254/* Return start channel idx we will use for mode->channels[idx] */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700255static int dfs_get_start_chan_idx(struct hostapd_iface *iface, int *seg1_start)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700256{
257 struct hostapd_hw_modes *mode;
258 struct hostapd_channel_data *chan;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800259 int channel_no = iface->conf->channel;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700260 int res = -1, i;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700261 int chan_seg1 = -1;
262
263 *seg1_start = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700264
265 /* HT40- */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800266 if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700267 channel_no -= 4;
268
269 /* VHT */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800270 if (iface->conf->ieee80211ac) {
271 switch (iface->conf->vht_oper_chwidth) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700272 case VHT_CHANWIDTH_USE_HT:
273 break;
274 case VHT_CHANWIDTH_80MHZ:
275 channel_no =
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800276 iface->conf->vht_oper_centr_freq_seg0_idx - 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700277 break;
278 case VHT_CHANWIDTH_160MHZ:
279 channel_no =
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800280 iface->conf->vht_oper_centr_freq_seg0_idx - 14;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700281 break;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700282 case VHT_CHANWIDTH_80P80MHZ:
283 channel_no =
284 iface->conf->vht_oper_centr_freq_seg0_idx - 6;
285 chan_seg1 =
286 iface->conf->vht_oper_centr_freq_seg1_idx - 6;
287 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700288 default:
289 wpa_printf(MSG_INFO,
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700290 "DFS only VHT20/40/80/160/80+80 is supported now");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700291 channel_no = -1;
292 break;
293 }
294 }
295
296 /* Get idx */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800297 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700298 for (i = 0; i < mode->num_channels; i++) {
299 chan = &mode->channels[i];
300 if (chan->chan == channel_no) {
301 res = i;
302 break;
303 }
304 }
305
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700306 if (res != -1 && chan_seg1 > -1) {
307 int found = 0;
308
309 /* Get idx for seg1 */
310 mode = iface->current_mode;
311 for (i = 0; i < mode->num_channels; i++) {
312 chan = &mode->channels[i];
313 if (chan->chan == chan_seg1) {
314 *seg1_start = i;
315 found = 1;
316 break;
317 }
318 }
319 if (!found)
320 res = -1;
321 }
322
Dmitry Shmidt98660862014-03-11 17:26:21 -0700323 if (res == -1) {
324 wpa_printf(MSG_DEBUG,
325 "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d",
326 mode->num_channels, channel_no, iface->conf->channel,
327 iface->conf->ieee80211n,
328 iface->conf->secondary_channel,
329 iface->conf->vht_oper_chwidth);
330
331 for (i = 0; i < mode->num_channels; i++) {
332 wpa_printf(MSG_DEBUG, "Available channel: %d",
333 mode->channels[i].chan);
334 }
335 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700336
337 return res;
338}
339
340
341/* At least one channel have radar flag */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800342static int dfs_check_chans_radar(struct hostapd_iface *iface,
343 int start_chan_idx, int n_chans)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700344{
345 struct hostapd_channel_data *channel;
346 struct hostapd_hw_modes *mode;
347 int i, res = 0;
348
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800349 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700350
351 for (i = 0; i < n_chans; i++) {
352 channel = &mode->channels[start_chan_idx + i];
353 if (channel->flag & HOSTAPD_CHAN_RADAR)
354 res++;
355 }
356
357 return res;
358}
359
360
361/* All channels available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800362static int dfs_check_chans_available(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700363 int start_chan_idx, int n_chans)
364{
365 struct hostapd_channel_data *channel;
366 struct hostapd_hw_modes *mode;
367 int i;
368
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800369 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700370
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800371 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700372 channel = &mode->channels[start_chan_idx + i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800373
374 if (channel->flag & HOSTAPD_CHAN_DISABLED)
375 break;
376
377 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
378 continue;
379
Dmitry Shmidt051af732013-10-22 13:52:46 -0700380 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
381 HOSTAPD_CHAN_DFS_AVAILABLE)
382 break;
383 }
384
385 return i == n_chans;
386}
387
388
389/* At least one channel unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800390static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700391 int start_chan_idx,
392 int n_chans)
393{
394 struct hostapd_channel_data *channel;
395 struct hostapd_hw_modes *mode;
396 int i, res = 0;
397
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800398 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700399
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800400 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700401 channel = &mode->channels[start_chan_idx + i];
402 if (channel->flag & HOSTAPD_CHAN_DISABLED)
403 res++;
404 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
405 HOSTAPD_CHAN_DFS_UNAVAILABLE)
406 res++;
407 }
408
409 return res;
410}
411
412
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800413static struct hostapd_channel_data *
414dfs_get_valid_channel(struct hostapd_iface *iface,
415 int *secondary_channel,
416 u8 *vht_oper_centr_freq_seg0_idx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800417 u8 *vht_oper_centr_freq_seg1_idx,
418 int skip_radar)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700419{
420 struct hostapd_hw_modes *mode;
421 struct hostapd_channel_data *chan = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800422 int num_available_chandefs;
423 int chan_idx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700424 u32 _rand;
425
426 wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800427 *secondary_channel = 0;
428 *vht_oper_centr_freq_seg0_idx = 0;
429 *vht_oper_centr_freq_seg1_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700430
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800431 if (iface->current_mode == NULL)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700432 return NULL;
433
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800434 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700435 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
436 return NULL;
437
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800438 /* Get the count first */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800439 num_available_chandefs = dfs_find_channel(iface, NULL, 0, skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800440 if (num_available_chandefs == 0)
441 return NULL;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700442
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800443 os_get_random((u8 *) &_rand, sizeof(_rand));
444 chan_idx = _rand % num_available_chandefs;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800445 dfs_find_channel(iface, &chan, chan_idx, skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800446
447 /* dfs_find_channel() calculations assume HT40+ */
448 if (iface->conf->secondary_channel)
449 *secondary_channel = 1;
450 else
451 *secondary_channel = 0;
452
453 dfs_adjust_vht_center_freq(iface, chan,
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800454 *secondary_channel,
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800455 vht_oper_centr_freq_seg0_idx,
456 vht_oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700457
458 return chan;
459}
460
461
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800462static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700463{
464 struct hostapd_hw_modes *mode;
465 struct hostapd_channel_data *chan = NULL;
466 int i;
467
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800468 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700469 if (mode == NULL)
470 return 0;
471
472 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800473 for (i = 0; i < iface->current_mode->num_channels; i++) {
474 chan = &iface->current_mode->channels[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -0700475 if (chan->freq == freq) {
476 if (chan->flag & HOSTAPD_CHAN_RADAR) {
477 chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
478 chan->flag |= state;
479 return 1; /* Channel found */
480 }
481 }
482 }
483 wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
484 return 0;
485}
486
487
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800488static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700489 int chan_offset, int chan_width, int cf1,
490 int cf2, u32 state)
491{
492 int n_chans = 1, i;
493 struct hostapd_hw_modes *mode;
494 int frequency = freq;
495 int ret = 0;
496
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800497 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700498 if (mode == NULL)
499 return 0;
500
501 if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
502 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
503 return 0;
504 }
505
506 /* Seems cf1 and chan_width is enough here */
507 switch (chan_width) {
508 case CHAN_WIDTH_20_NOHT:
509 case CHAN_WIDTH_20:
510 n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800511 if (frequency == 0)
512 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700513 break;
514 case CHAN_WIDTH_40:
515 n_chans = 2;
516 frequency = cf1 - 10;
517 break;
518 case CHAN_WIDTH_80:
519 n_chans = 4;
520 frequency = cf1 - 30;
521 break;
522 case CHAN_WIDTH_160:
523 n_chans = 8;
524 frequency = cf1 - 70;
525 break;
526 default:
527 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
528 chan_width);
529 break;
530 }
531
532 wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
533 n_chans);
534 for (i = 0; i < n_chans; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800535 ret += set_dfs_state_freq(iface, frequency, state);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700536 frequency = frequency + 20;
537 }
538
539 return ret;
540}
541
542
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800543static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700544 int chan_width, int cf1, int cf2)
545{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700546 int start_chan_idx, start_chan_idx1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700547 struct hostapd_hw_modes *mode;
548 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700549 int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700550 u8 radar_chan;
551 int res = 0;
552
Dmitry Shmidt051af732013-10-22 13:52:46 -0700553 /* Our configuration */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800554 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700555 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
556 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700557
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700558 /* Check we are on DFS channel(s) */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800559 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700560 return 0;
561
Dmitry Shmidt051af732013-10-22 13:52:46 -0700562 /* Reported via radar event */
563 switch (chan_width) {
564 case CHAN_WIDTH_20_NOHT:
565 case CHAN_WIDTH_20:
566 radar_n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800567 if (frequency == 0)
568 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700569 break;
570 case CHAN_WIDTH_40:
571 radar_n_chans = 2;
572 frequency = cf1 - 10;
573 break;
574 case CHAN_WIDTH_80:
575 radar_n_chans = 4;
576 frequency = cf1 - 30;
577 break;
578 case CHAN_WIDTH_160:
579 radar_n_chans = 8;
580 frequency = cf1 - 70;
581 break;
582 default:
583 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
584 chan_width);
585 break;
586 }
587
588 ieee80211_freq_to_chan(frequency, &radar_chan);
589
590 for (i = 0; i < n_chans; i++) {
591 chan = &mode->channels[start_chan_idx + i];
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700592 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
593 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700594 for (j = 0; j < radar_n_chans; j++) {
595 wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
596 chan->chan, radar_chan + j * 4);
597 if (chan->chan == radar_chan + j * 4)
598 res++;
599 }
600 }
601
602 wpa_printf(MSG_DEBUG, "overlapped: %d", res);
603
604 return res;
605}
606
607
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700608static unsigned int dfs_get_cac_time(struct hostapd_iface *iface,
609 int start_chan_idx, int n_chans)
610{
611 struct hostapd_channel_data *channel;
612 struct hostapd_hw_modes *mode;
613 int i;
614 unsigned int cac_time_ms = 0;
615
616 mode = iface->current_mode;
617
618 for (i = 0; i < n_chans; i++) {
619 channel = &mode->channels[start_chan_idx + i];
620 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
621 continue;
622 if (channel->dfs_cac_ms > cac_time_ms)
623 cac_time_ms = channel->dfs_cac_ms;
624 }
625
626 return cac_time_ms;
627}
628
629
Dmitry Shmidt051af732013-10-22 13:52:46 -0700630/*
631 * Main DFS handler
632 * 1 - continue channel/ap setup
633 * 0 - channel/ap setup will be continued after CAC
634 * -1 - hit critical error
635 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800636int hostapd_handle_dfs(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700637{
638 struct hostapd_channel_data *channel;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700639 int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800640 int skip_radar = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700641
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800642 iface->cac_started = 0;
643
Dmitry Shmidt051af732013-10-22 13:52:46 -0700644 do {
645 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700646 start_chan_idx = dfs_get_start_chan_idx(iface,
647 &start_chan_idx1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700648 if (start_chan_idx == -1)
649 return -1;
650
651 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700652 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700653
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700654 /* Setup CAC time */
655 iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx,
656 n_chans);
657
Dmitry Shmidt051af732013-10-22 13:52:46 -0700658 /* Check if any of configured channels require DFS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800659 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700660 wpa_printf(MSG_DEBUG,
661 "DFS %d channels required radar detection",
662 res);
663 if (!res)
664 return 1;
665
666 /* Check if all channels are DFS available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800667 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700668 wpa_printf(MSG_DEBUG,
669 "DFS all channels available, (SKIP CAC): %s",
670 res ? "yes" : "no");
671 if (res)
672 return 1;
673
674 /* Check if any of configured channels is unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800675 res = dfs_check_chans_unavailable(iface, start_chan_idx,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700676 n_chans);
677 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
678 res, res ? "yes": "no");
679 if (res) {
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800680 int sec = 0;
681 u8 cf1 = 0, cf2 = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800682
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800683 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
684 skip_radar);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700685 if (!channel) {
686 wpa_printf(MSG_ERROR, "could not get valid channel");
687 return -1;
688 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800689
690 iface->freq = channel->freq;
691 iface->conf->channel = channel->chan;
692 iface->conf->secondary_channel = sec;
693 iface->conf->vht_oper_centr_freq_seg0_idx = cf1;
694 iface->conf->vht_oper_centr_freq_seg1_idx = cf2;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700695 }
696 } while (res);
697
698 /* Finally start CAC */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800699 hostapd_set_state(iface, HAPD_IFACE_DFS);
700 wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz", iface->freq);
701 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700702 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800703 iface->freq,
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800704 iface->conf->channel, iface->conf->secondary_channel,
705 iface->conf->vht_oper_chwidth,
706 iface->conf->vht_oper_centr_freq_seg0_idx,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700707 iface->conf->vht_oper_centr_freq_seg1_idx,
708 iface->dfs_cac_ms / 1000);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800709
710 res = hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
711 iface->freq,
712 iface->conf->channel,
713 iface->conf->ieee80211n,
714 iface->conf->ieee80211ac,
715 iface->conf->secondary_channel,
716 iface->conf->vht_oper_chwidth,
717 iface->conf->vht_oper_centr_freq_seg0_idx,
718 iface->conf->vht_oper_centr_freq_seg1_idx);
719
720 if (res) {
721 wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700722 return -1;
723 }
724
725 return 0;
726}
727
728
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800729int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700730 int ht_enabled, int chan_offset, int chan_width,
731 int cf1, int cf2)
732{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800733 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
734 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
735 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
736
Dmitry Shmidt051af732013-10-22 13:52:46 -0700737 if (success) {
738 /* Complete iface/ap configuration */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800739 set_dfs_state(iface, freq, ht_enabled, chan_offset,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700740 chan_width, cf1, cf2,
741 HOSTAPD_CHAN_DFS_AVAILABLE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800742 iface->cac_started = 0;
743 hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700744 }
745
746 return 0;
747}
748
749
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800750static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700751{
752 struct hostapd_channel_data *channel;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800753 int secondary_channel;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800754 u8 vht_oper_centr_freq_seg0_idx = 0;
755 u8 vht_oper_centr_freq_seg1_idx = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800756 int skip_radar = 0;
757 int err = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700758
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800759 /* Radar detected during active CAC */
760 iface->cac_started = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800761 channel = dfs_get_valid_channel(iface, &secondary_channel,
762 &vht_oper_centr_freq_seg0_idx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800763 &vht_oper_centr_freq_seg1_idx,
764 skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800765
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800766 if (!channel) {
767 wpa_printf(MSG_ERROR, "No valid channel available");
768 hostapd_setup_interface_complete(iface, err);
769 return err;
770 }
771
772 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
773 channel->chan);
774 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
775 "freq=%d chan=%d sec_chan=%d", channel->freq,
776 channel->chan, secondary_channel);
777
778 iface->freq = channel->freq;
779 iface->conf->channel = channel->chan;
780 iface->conf->secondary_channel = secondary_channel;
781 iface->conf->vht_oper_centr_freq_seg0_idx =
782 vht_oper_centr_freq_seg0_idx;
783 iface->conf->vht_oper_centr_freq_seg1_idx =
784 vht_oper_centr_freq_seg1_idx;
785 err = 0;
786
787 hostapd_setup_interface_complete(iface, err);
788 return err;
789}
790
791
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700792static int hostapd_csa_in_progress(struct hostapd_iface *iface)
793{
794 unsigned int i;
795 for (i = 0; i < iface->num_bss; i++)
796 if (iface->bss[i]->csa_in_progress)
797 return 1;
798 return 0;
799}
800
801
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800802static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
803{
804 struct hostapd_channel_data *channel;
805 int secondary_channel;
806 u8 vht_oper_centr_freq_seg0_idx;
807 u8 vht_oper_centr_freq_seg1_idx;
808 int skip_radar = 1;
809 struct csa_settings csa_settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700810 unsigned int i;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800811 int err = 1;
812
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800813 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
814 __func__, iface->cac_started ? "yes" : "no",
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700815 hostapd_csa_in_progress(iface) ? "yes" : "no");
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800816
817 /* Check if CSA in progress */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700818 if (hostapd_csa_in_progress(iface))
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800819 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800820
821 /* Check if active CAC */
822 if (iface->cac_started)
823 return hostapd_dfs_start_channel_switch_cac(iface);
824
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800825 /* Perform channel switch/CSA */
826 channel = dfs_get_valid_channel(iface, &secondary_channel,
827 &vht_oper_centr_freq_seg0_idx,
828 &vht_oper_centr_freq_seg1_idx,
829 skip_radar);
830
831 if (!channel) {
Dmitry Shmidt98660862014-03-11 17:26:21 -0700832 /*
833 * If there is no channel to switch immediately to, check if
834 * there is another channel where we can switch even if it
835 * requires to perform a CAC first.
836 */
837 skip_radar = 0;
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 if (!channel) {
843 /* FIXME: Wait for channel(s) to become available */
844 hostapd_disable_iface(iface);
845 return err;
846 }
847
848 iface->freq = channel->freq;
849 iface->conf->channel = channel->chan;
850 iface->conf->secondary_channel = secondary_channel;
851 iface->conf->vht_oper_centr_freq_seg0_idx =
852 vht_oper_centr_freq_seg0_idx;
853 iface->conf->vht_oper_centr_freq_seg1_idx =
854 vht_oper_centr_freq_seg1_idx;
855
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800856 hostapd_disable_iface(iface);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700857 hostapd_enable_iface(iface);
858 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800859 }
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 /* Setup CSA request */
868 os_memset(&csa_settings, 0, sizeof(csa_settings));
869 csa_settings.cs_count = 5;
870 csa_settings.block_tx = 1;
871 err = hostapd_set_freq_params(&csa_settings.freq_params,
872 iface->conf->hw_mode,
873 channel->freq,
874 channel->chan,
875 iface->conf->ieee80211n,
876 iface->conf->ieee80211ac,
877 secondary_channel,
878 iface->conf->vht_oper_chwidth,
879 vht_oper_centr_freq_seg0_idx,
880 vht_oper_centr_freq_seg1_idx,
881 iface->current_mode->vht_capab);
882
883 if (err) {
884 wpa_printf(MSG_ERROR, "DFS failed to calculate CSA freq params");
885 hostapd_disable_iface(iface);
886 return err;
887 }
888
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700889 for (i = 0; i < iface->num_bss; i++) {
890 err = hostapd_switch_channel(iface->bss[i], &csa_settings);
891 if (err)
892 break;
893 }
894
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800895 if (err) {
896 wpa_printf(MSG_WARNING, "DFS failed to schedule CSA (%d) - trying fallback",
897 err);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800898 iface->freq = channel->freq;
899 iface->conf->channel = channel->chan;
900 iface->conf->secondary_channel = secondary_channel;
901 iface->conf->vht_oper_centr_freq_seg0_idx =
902 vht_oper_centr_freq_seg0_idx;
903 iface->conf->vht_oper_centr_freq_seg1_idx =
904 vht_oper_centr_freq_seg1_idx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700905
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800906 hostapd_disable_iface(iface);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800907 hostapd_enable_iface(iface);
908 return 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800909 }
910
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800911 /* Channel configuration will be updated once CSA completes and
912 * ch_switch_notify event is received */
913
914 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700915 return 0;
916}
917
918
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800919int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700920 int ht_enabled, int chan_offset, int chan_width,
921 int cf1, int cf2)
922{
923 int res;
924
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800925 if (!iface->conf->ieee80211h)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700926 return 0;
927
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800928 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
929 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
930 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
931
Dmitry Shmidt051af732013-10-22 13:52:46 -0700932 /* mark radar frequency as invalid */
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -0700933 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
934 cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700935
936 /* Skip if reported radar event not overlapped our channels */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800937 res = dfs_are_channels_overlapped(iface, freq, chan_width, cf1, cf2);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700938 if (!res)
939 return 0;
940
Dmitry Shmidt051af732013-10-22 13:52:46 -0700941 /* radar detected while operating, switch the channel. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800942 res = hostapd_dfs_start_channel_switch(iface);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700943
944 return res;
945}
946
947
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800948int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700949 int ht_enabled, int chan_offset, int chan_width,
950 int cf1, int cf2)
951{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800952 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
953 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
954 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700955 /* TODO add correct implementation here */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800956 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
957 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700958 return 0;
959}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800960
961
962int hostapd_is_dfs_required(struct hostapd_iface *iface)
963{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700964 int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800965
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700966 if (!iface->conf->ieee80211h || !iface->current_mode ||
967 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
968 return 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800969
970 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700971 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800972 if (start_chan_idx == -1)
973 return -1;
974
975 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700976 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800977
978 /* Check if any of configured channels require DFS */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700979 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
980 if (res)
981 return res;
982 if (start_chan_idx1 >= 0 && n_chans1 > 0)
983 res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1);
984 return res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800985}