blob: 0db5ef6913d152930673a8102fdffcb837db17f0 [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 Shmidt6c0da2b2015-01-05 13:08:17 -0800443 if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
444 _rand = os_random();
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800445 chan_idx = _rand % num_available_chandefs;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800446 dfs_find_channel(iface, &chan, chan_idx, skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800447
448 /* dfs_find_channel() calculations assume HT40+ */
449 if (iface->conf->secondary_channel)
450 *secondary_channel = 1;
451 else
452 *secondary_channel = 0;
453
454 dfs_adjust_vht_center_freq(iface, chan,
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800455 *secondary_channel,
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800456 vht_oper_centr_freq_seg0_idx,
457 vht_oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700458
459 return chan;
460}
461
462
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800463static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700464{
465 struct hostapd_hw_modes *mode;
466 struct hostapd_channel_data *chan = NULL;
467 int i;
468
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800469 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700470 if (mode == NULL)
471 return 0;
472
473 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800474 for (i = 0; i < iface->current_mode->num_channels; i++) {
475 chan = &iface->current_mode->channels[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -0700476 if (chan->freq == freq) {
477 if (chan->flag & HOSTAPD_CHAN_RADAR) {
478 chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
479 chan->flag |= state;
480 return 1; /* Channel found */
481 }
482 }
483 }
484 wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
485 return 0;
486}
487
488
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800489static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700490 int chan_offset, int chan_width, int cf1,
491 int cf2, u32 state)
492{
493 int n_chans = 1, i;
494 struct hostapd_hw_modes *mode;
495 int frequency = freq;
496 int ret = 0;
497
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800498 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700499 if (mode == NULL)
500 return 0;
501
502 if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
503 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
504 return 0;
505 }
506
507 /* Seems cf1 and chan_width is enough here */
508 switch (chan_width) {
509 case CHAN_WIDTH_20_NOHT:
510 case CHAN_WIDTH_20:
511 n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800512 if (frequency == 0)
513 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700514 break;
515 case CHAN_WIDTH_40:
516 n_chans = 2;
517 frequency = cf1 - 10;
518 break;
519 case CHAN_WIDTH_80:
520 n_chans = 4;
521 frequency = cf1 - 30;
522 break;
523 case CHAN_WIDTH_160:
524 n_chans = 8;
525 frequency = cf1 - 70;
526 break;
527 default:
528 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
529 chan_width);
530 break;
531 }
532
533 wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
534 n_chans);
535 for (i = 0; i < n_chans; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800536 ret += set_dfs_state_freq(iface, frequency, state);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700537 frequency = frequency + 20;
538 }
539
540 return ret;
541}
542
543
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800544static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700545 int chan_width, int cf1, int cf2)
546{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700547 int start_chan_idx, start_chan_idx1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700548 struct hostapd_hw_modes *mode;
549 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700550 int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700551 u8 radar_chan;
552 int res = 0;
553
Dmitry Shmidt051af732013-10-22 13:52:46 -0700554 /* Our configuration */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800555 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700556 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
557 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700558
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700559 /* Check we are on DFS channel(s) */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800560 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700561 return 0;
562
Dmitry Shmidt051af732013-10-22 13:52:46 -0700563 /* Reported via radar event */
564 switch (chan_width) {
565 case CHAN_WIDTH_20_NOHT:
566 case CHAN_WIDTH_20:
567 radar_n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800568 if (frequency == 0)
569 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700570 break;
571 case CHAN_WIDTH_40:
572 radar_n_chans = 2;
573 frequency = cf1 - 10;
574 break;
575 case CHAN_WIDTH_80:
576 radar_n_chans = 4;
577 frequency = cf1 - 30;
578 break;
579 case CHAN_WIDTH_160:
580 radar_n_chans = 8;
581 frequency = cf1 - 70;
582 break;
583 default:
584 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
585 chan_width);
586 break;
587 }
588
589 ieee80211_freq_to_chan(frequency, &radar_chan);
590
591 for (i = 0; i < n_chans; i++) {
592 chan = &mode->channels[start_chan_idx + i];
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700593 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
594 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700595 for (j = 0; j < radar_n_chans; j++) {
596 wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
597 chan->chan, radar_chan + j * 4);
598 if (chan->chan == radar_chan + j * 4)
599 res++;
600 }
601 }
602
603 wpa_printf(MSG_DEBUG, "overlapped: %d", res);
604
605 return res;
606}
607
608
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700609static unsigned int dfs_get_cac_time(struct hostapd_iface *iface,
610 int start_chan_idx, int n_chans)
611{
612 struct hostapd_channel_data *channel;
613 struct hostapd_hw_modes *mode;
614 int i;
615 unsigned int cac_time_ms = 0;
616
617 mode = iface->current_mode;
618
619 for (i = 0; i < n_chans; i++) {
620 channel = &mode->channels[start_chan_idx + i];
621 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
622 continue;
623 if (channel->dfs_cac_ms > cac_time_ms)
624 cac_time_ms = channel->dfs_cac_ms;
625 }
626
627 return cac_time_ms;
628}
629
630
Dmitry Shmidt051af732013-10-22 13:52:46 -0700631/*
632 * Main DFS handler
633 * 1 - continue channel/ap setup
634 * 0 - channel/ap setup will be continued after CAC
635 * -1 - hit critical error
636 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800637int hostapd_handle_dfs(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700638{
639 struct hostapd_channel_data *channel;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700640 int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800641 int skip_radar = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700642
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800643 if (!iface->current_mode) {
644 /*
645 * This can happen with drivers that do not provide mode
646 * information and as such, cannot really use hostapd for DFS.
647 */
648 wpa_printf(MSG_DEBUG,
649 "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
650 return 1;
651 }
652
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800653 iface->cac_started = 0;
654
Dmitry Shmidt051af732013-10-22 13:52:46 -0700655 do {
656 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700657 start_chan_idx = dfs_get_start_chan_idx(iface,
658 &start_chan_idx1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700659 if (start_chan_idx == -1)
660 return -1;
661
662 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700663 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700664
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700665 /* Setup CAC time */
666 iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx,
667 n_chans);
668
Dmitry Shmidt051af732013-10-22 13:52:46 -0700669 /* Check if any of configured channels require DFS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800670 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700671 wpa_printf(MSG_DEBUG,
672 "DFS %d channels required radar detection",
673 res);
674 if (!res)
675 return 1;
676
677 /* Check if all channels are DFS available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800678 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700679 wpa_printf(MSG_DEBUG,
680 "DFS all channels available, (SKIP CAC): %s",
681 res ? "yes" : "no");
682 if (res)
683 return 1;
684
685 /* Check if any of configured channels is unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800686 res = dfs_check_chans_unavailable(iface, start_chan_idx,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700687 n_chans);
688 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
689 res, res ? "yes": "no");
690 if (res) {
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800691 int sec = 0;
692 u8 cf1 = 0, cf2 = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800693
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800694 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
695 skip_radar);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700696 if (!channel) {
697 wpa_printf(MSG_ERROR, "could not get valid channel");
698 return -1;
699 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800700
701 iface->freq = channel->freq;
702 iface->conf->channel = channel->chan;
703 iface->conf->secondary_channel = sec;
704 iface->conf->vht_oper_centr_freq_seg0_idx = cf1;
705 iface->conf->vht_oper_centr_freq_seg1_idx = cf2;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700706 }
707 } while (res);
708
709 /* Finally start CAC */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800710 hostapd_set_state(iface, HAPD_IFACE_DFS);
711 wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz", iface->freq);
712 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700713 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800714 iface->freq,
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800715 iface->conf->channel, iface->conf->secondary_channel,
716 iface->conf->vht_oper_chwidth,
717 iface->conf->vht_oper_centr_freq_seg0_idx,
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700718 iface->conf->vht_oper_centr_freq_seg1_idx,
719 iface->dfs_cac_ms / 1000);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800720
721 res = hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
722 iface->freq,
723 iface->conf->channel,
724 iface->conf->ieee80211n,
725 iface->conf->ieee80211ac,
726 iface->conf->secondary_channel,
727 iface->conf->vht_oper_chwidth,
728 iface->conf->vht_oper_centr_freq_seg0_idx,
729 iface->conf->vht_oper_centr_freq_seg1_idx);
730
731 if (res) {
732 wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700733 return -1;
734 }
735
736 return 0;
737}
738
739
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800740int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700741 int ht_enabled, int chan_offset, int chan_width,
742 int cf1, int cf2)
743{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800744 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
745 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
746 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
747
Dmitry Shmidt051af732013-10-22 13:52:46 -0700748 if (success) {
749 /* Complete iface/ap configuration */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800750 set_dfs_state(iface, freq, ht_enabled, chan_offset,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700751 chan_width, cf1, cf2,
752 HOSTAPD_CHAN_DFS_AVAILABLE);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800753 iface->cac_started = 0;
754 hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700755 }
756
757 return 0;
758}
759
760
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800761static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700762{
763 struct hostapd_channel_data *channel;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800764 int secondary_channel;
Dmitry Shmidt96be6222014-02-13 10:16:51 -0800765 u8 vht_oper_centr_freq_seg0_idx = 0;
766 u8 vht_oper_centr_freq_seg1_idx = 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800767 int skip_radar = 0;
768 int err = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700769
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800770 /* Radar detected during active CAC */
771 iface->cac_started = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800772 channel = dfs_get_valid_channel(iface, &secondary_channel,
773 &vht_oper_centr_freq_seg0_idx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800774 &vht_oper_centr_freq_seg1_idx,
775 skip_radar);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800776
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800777 if (!channel) {
778 wpa_printf(MSG_ERROR, "No valid channel available");
779 hostapd_setup_interface_complete(iface, err);
780 return err;
781 }
782
783 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
784 channel->chan);
785 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
786 "freq=%d chan=%d sec_chan=%d", channel->freq,
787 channel->chan, secondary_channel);
788
789 iface->freq = channel->freq;
790 iface->conf->channel = channel->chan;
791 iface->conf->secondary_channel = secondary_channel;
792 iface->conf->vht_oper_centr_freq_seg0_idx =
793 vht_oper_centr_freq_seg0_idx;
794 iface->conf->vht_oper_centr_freq_seg1_idx =
795 vht_oper_centr_freq_seg1_idx;
796 err = 0;
797
798 hostapd_setup_interface_complete(iface, err);
799 return err;
800}
801
802
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700803static int hostapd_csa_in_progress(struct hostapd_iface *iface)
804{
805 unsigned int i;
806 for (i = 0; i < iface->num_bss; i++)
807 if (iface->bss[i]->csa_in_progress)
808 return 1;
809 return 0;
810}
811
812
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800813static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
814{
815 struct hostapd_channel_data *channel;
816 int secondary_channel;
817 u8 vht_oper_centr_freq_seg0_idx;
818 u8 vht_oper_centr_freq_seg1_idx;
819 int skip_radar = 1;
820 struct csa_settings csa_settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700821 unsigned int i;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800822 int err = 1;
823
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800824 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
825 __func__, iface->cac_started ? "yes" : "no",
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700826 hostapd_csa_in_progress(iface) ? "yes" : "no");
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800827
828 /* Check if CSA in progress */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700829 if (hostapd_csa_in_progress(iface))
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800830 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800831
832 /* Check if active CAC */
833 if (iface->cac_started)
834 return hostapd_dfs_start_channel_switch_cac(iface);
835
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800836 /* Perform channel switch/CSA */
837 channel = dfs_get_valid_channel(iface, &secondary_channel,
838 &vht_oper_centr_freq_seg0_idx,
839 &vht_oper_centr_freq_seg1_idx,
840 skip_radar);
841
842 if (!channel) {
Dmitry Shmidt98660862014-03-11 17:26:21 -0700843 /*
844 * If there is no channel to switch immediately to, check if
845 * there is another channel where we can switch even if it
846 * requires to perform a CAC first.
847 */
848 skip_radar = 0;
849 channel = dfs_get_valid_channel(iface, &secondary_channel,
850 &vht_oper_centr_freq_seg0_idx,
851 &vht_oper_centr_freq_seg1_idx,
852 skip_radar);
853 if (!channel) {
854 /* FIXME: Wait for channel(s) to become available */
855 hostapd_disable_iface(iface);
856 return err;
857 }
858
859 iface->freq = channel->freq;
860 iface->conf->channel = channel->chan;
861 iface->conf->secondary_channel = secondary_channel;
862 iface->conf->vht_oper_centr_freq_seg0_idx =
863 vht_oper_centr_freq_seg0_idx;
864 iface->conf->vht_oper_centr_freq_seg1_idx =
865 vht_oper_centr_freq_seg1_idx;
866
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800867 hostapd_disable_iface(iface);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700868 hostapd_enable_iface(iface);
869 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800870 }
871
872 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
873 channel->chan);
874 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
875 "freq=%d chan=%d sec_chan=%d", channel->freq,
876 channel->chan, secondary_channel);
877
878 /* Setup CSA request */
879 os_memset(&csa_settings, 0, sizeof(csa_settings));
880 csa_settings.cs_count = 5;
881 csa_settings.block_tx = 1;
882 err = hostapd_set_freq_params(&csa_settings.freq_params,
883 iface->conf->hw_mode,
884 channel->freq,
885 channel->chan,
886 iface->conf->ieee80211n,
887 iface->conf->ieee80211ac,
888 secondary_channel,
889 iface->conf->vht_oper_chwidth,
890 vht_oper_centr_freq_seg0_idx,
891 vht_oper_centr_freq_seg1_idx,
892 iface->current_mode->vht_capab);
893
894 if (err) {
895 wpa_printf(MSG_ERROR, "DFS failed to calculate CSA freq params");
896 hostapd_disable_iface(iface);
897 return err;
898 }
899
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700900 for (i = 0; i < iface->num_bss; i++) {
901 err = hostapd_switch_channel(iface->bss[i], &csa_settings);
902 if (err)
903 break;
904 }
905
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800906 if (err) {
907 wpa_printf(MSG_WARNING, "DFS failed to schedule CSA (%d) - trying fallback",
908 err);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800909 iface->freq = channel->freq;
910 iface->conf->channel = channel->chan;
911 iface->conf->secondary_channel = secondary_channel;
912 iface->conf->vht_oper_centr_freq_seg0_idx =
913 vht_oper_centr_freq_seg0_idx;
914 iface->conf->vht_oper_centr_freq_seg1_idx =
915 vht_oper_centr_freq_seg1_idx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700916
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800917 hostapd_disable_iface(iface);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800918 hostapd_enable_iface(iface);
919 return 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800920 }
921
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800922 /* Channel configuration will be updated once CSA completes and
923 * ch_switch_notify event is received */
924
925 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700926 return 0;
927}
928
929
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800930int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700931 int ht_enabled, int chan_offset, int chan_width,
932 int cf1, int cf2)
933{
934 int res;
935
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800936 if (!iface->conf->ieee80211h)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700937 return 0;
938
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800939 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
940 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
941 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
942
Dmitry Shmidt051af732013-10-22 13:52:46 -0700943 /* mark radar frequency as invalid */
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -0700944 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
945 cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700946
947 /* Skip if reported radar event not overlapped our channels */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800948 res = dfs_are_channels_overlapped(iface, freq, chan_width, cf1, cf2);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700949 if (!res)
950 return 0;
951
Dmitry Shmidt051af732013-10-22 13:52:46 -0700952 /* radar detected while operating, switch the channel. */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800953 res = hostapd_dfs_start_channel_switch(iface);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700954
955 return res;
956}
957
958
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800959int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700960 int ht_enabled, int chan_offset, int chan_width,
961 int cf1, int cf2)
962{
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800963 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
964 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
965 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700966 /* TODO add correct implementation here */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800967 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
968 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700969 return 0;
970}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800971
972
973int hostapd_is_dfs_required(struct hostapd_iface *iface)
974{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700975 int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800976
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700977 if (!iface->conf->ieee80211h || !iface->current_mode ||
978 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
979 return 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800980
981 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700982 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800983 if (start_chan_idx == -1)
984 return -1;
985
986 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700987 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800988
989 /* Check if any of configured channels require DFS */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700990 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
991 if (res)
992 return res;
993 if (start_chan_idx1 >= 0 && n_chans1 > 0)
994 res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1);
995 return res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800996}