blob: e46dd7ede3b82d5fd333f9023cdcbfb3d2dd7c27 [file] [log] [blame]
Dmitry Shmidt051af732013-10-22 13:52:46 -07001/*
2 * DFS - Dynamic Frequency Selection
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004 * Copyright (c) 2013-2017, Qualcomm Atheros, Inc.
Dmitry Shmidt051af732013-10-22 13:52:46 -07005 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10#include "utils/includes.h"
11
12#include "utils/common.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidt7f656022015-02-25 14:36:37 -080014#include "common/hw_features_common.h"
Dmitry Shmidtcce06662013-11-04 18:44:24 -080015#include "common/wpa_ctrl.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070016#include "hostapd.h"
17#include "ap_drv_ops.h"
18#include "drivers/driver.h"
19#include "dfs.h"
20
21
Sunil Ravi59fa4b42022-05-02 22:54:18 -070022enum dfs_channel_type {
23 DFS_ANY_CHANNEL,
24 DFS_AVAILABLE, /* non-radar or radar-available */
25 DFS_NO_CAC_YET, /* radar-not-yet-available */
26};
27
28static struct hostapd_channel_data *
29dfs_downgrade_bandwidth(struct hostapd_iface *iface, int *secondary_channel,
30 u8 *oper_centr_freq_seg0_idx,
31 u8 *oper_centr_freq_seg1_idx,
32 enum dfs_channel_type *channel_type);
33
34
35static bool dfs_use_radar_background(struct hostapd_iface *iface)
36{
37 return (iface->drv_flags2 & WPA_DRIVER_RADAR_BACKGROUND) &&
38 iface->conf->enable_background_radar;
39}
40
41
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070042static int dfs_get_used_n_chans(struct hostapd_iface *iface, int *seg1)
Dmitry Shmidt051af732013-10-22 13:52:46 -070043{
44 int n_chans = 1;
45
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070046 *seg1 = 0;
47
Dmitry Shmidtcce06662013-11-04 18:44:24 -080048 if (iface->conf->ieee80211n && iface->conf->secondary_channel)
Dmitry Shmidt051af732013-10-22 13:52:46 -070049 n_chans = 2;
50
Hai Shalom81f62d82019-07-22 12:10:00 -070051 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
52 switch (hostapd_get_oper_chwidth(iface->conf)) {
53 case CHANWIDTH_USE_HT:
Dmitry Shmidt051af732013-10-22 13:52:46 -070054 break;
Hai Shalom81f62d82019-07-22 12:10:00 -070055 case CHANWIDTH_80MHZ:
Dmitry Shmidt051af732013-10-22 13:52:46 -070056 n_chans = 4;
57 break;
Hai Shalom81f62d82019-07-22 12:10:00 -070058 case CHANWIDTH_160MHZ:
Dmitry Shmidt051af732013-10-22 13:52:46 -070059 n_chans = 8;
60 break;
Hai Shalom81f62d82019-07-22 12:10:00 -070061 case CHANWIDTH_80P80MHZ:
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070062 n_chans = 4;
63 *seg1 = 4;
64 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -070065 default:
66 break;
67 }
68 }
69
70 return n_chans;
71}
72
73
Sunil Ravi59fa4b42022-05-02 22:54:18 -070074/* dfs_channel_available: select new channel according to type parameter */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080075static int dfs_channel_available(struct hostapd_channel_data *chan,
Sunil Ravi59fa4b42022-05-02 22:54:18 -070076 enum dfs_channel_type type)
Dmitry Shmidt051af732013-10-22 13:52:46 -070077{
Sunil Ravi59fa4b42022-05-02 22:54:18 -070078 if (type == DFS_NO_CAC_YET) {
79 /* Select only radar channel where CAC has not been
80 * performed yet
81 */
82 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
83 (chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
84 HOSTAPD_CHAN_DFS_USABLE)
85 return 1;
86 return 0;
87 }
88
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080089 /*
90 * When radar detection happens, CSA is performed. However, there's no
91 * time for CAC, so radar channels must be skipped when finding a new
Dmitry Shmidt98660862014-03-11 17:26:21 -070092 * channel for CSA, unless they are available for immediate use.
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080093 */
Sunil Ravi59fa4b42022-05-02 22:54:18 -070094 if (type == DFS_AVAILABLE && (chan->flag & HOSTAPD_CHAN_RADAR) &&
Dmitry Shmidt98660862014-03-11 17:26:21 -070095 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) !=
96 HOSTAPD_CHAN_DFS_AVAILABLE))
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080097 return 0;
98
Dmitry Shmidt051af732013-10-22 13:52:46 -070099 if (chan->flag & HOSTAPD_CHAN_DISABLED)
100 return 0;
101 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
102 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
103 HOSTAPD_CHAN_DFS_UNAVAILABLE))
104 return 0;
105 return 1;
106}
107
108
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700109static int dfs_is_chan_allowed(struct hostapd_channel_data *chan, int n_chans)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700110{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700111 /*
112 * The tables contain first valid channel number based on channel width.
113 * We will also choose this first channel as the control one.
114 */
115 int allowed_40[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
Hai Shalom60840252021-02-19 19:02:11 -0800116 165, 173, 184, 192 };
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700117 /*
118 * VHT80, valid channels based on center frequency:
Hai Shalom60840252021-02-19 19:02:11 -0800119 * 42, 58, 106, 122, 138, 155, 171
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700120 */
Hai Shalom60840252021-02-19 19:02:11 -0800121 int allowed_80[] = { 36, 52, 100, 116, 132, 149, 165 };
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800122 /*
123 * VHT160 valid channels based on center frequency:
Hai Shalom60840252021-02-19 19:02:11 -0800124 * 50, 114, 163
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800125 */
Hai Shalom60840252021-02-19 19:02:11 -0800126 int allowed_160[] = { 36, 100, 149 };
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700127 int *allowed = allowed_40;
128 unsigned int i, allowed_no = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700129
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700130 switch (n_chans) {
131 case 2:
132 allowed = allowed_40;
133 allowed_no = ARRAY_SIZE(allowed_40);
134 break;
135 case 4:
136 allowed = allowed_80;
137 allowed_no = ARRAY_SIZE(allowed_80);
138 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800139 case 8:
140 allowed = allowed_160;
141 allowed_no = ARRAY_SIZE(allowed_160);
142 break;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700143 default:
144 wpa_printf(MSG_DEBUG, "Unknown width for %d channels", n_chans);
145 break;
146 }
147
148 for (i = 0; i < allowed_no; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700149 if (chan->chan == allowed[i])
150 return 1;
151 }
152
153 return 0;
154}
155
156
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700157static struct hostapd_channel_data *
158dfs_get_chan_data(struct hostapd_hw_modes *mode, int freq, int first_chan_idx)
159{
160 int i;
161
162 for (i = first_chan_idx; i < mode->num_channels; i++) {
163 if (mode->channels[i].freq == freq)
164 return &mode->channels[i];
165 }
166
167 return NULL;
168}
169
170
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800171static int dfs_chan_range_available(struct hostapd_hw_modes *mode,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800172 int first_chan_idx, int num_chans,
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700173 enum dfs_channel_type type)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800174{
175 struct hostapd_channel_data *first_chan, *chan;
176 int i;
Hai Shalom74f70d42019-02-11 14:42:39 -0800177 u32 bw = num_chan_to_bw(num_chans);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800178
Hai Shalomfdcde762020-04-02 11:19:20 -0700179 if (first_chan_idx + num_chans > mode->num_channels) {
180 wpa_printf(MSG_DEBUG,
181 "DFS: some channels in range not defined");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800182 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700183 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800184
185 first_chan = &mode->channels[first_chan_idx];
186
Hai Shalom74f70d42019-02-11 14:42:39 -0800187 /* hostapd DFS implementation assumes the first channel as primary.
188 * If it's not allowed to use the first channel as primary, decline the
189 * whole channel range. */
Hai Shalomfdcde762020-04-02 11:19:20 -0700190 if (!chan_pri_allowed(first_chan)) {
191 wpa_printf(MSG_DEBUG, "DFS: primary chanenl not allowed");
Hai Shalom74f70d42019-02-11 14:42:39 -0800192 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700193 }
Hai Shalom74f70d42019-02-11 14:42:39 -0800194
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800195 for (i = 0; i < num_chans; i++) {
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700196 chan = dfs_get_chan_data(mode, first_chan->freq + i * 20,
197 first_chan_idx);
Hai Shalomfdcde762020-04-02 11:19:20 -0700198 if (!chan) {
199 wpa_printf(MSG_DEBUG, "DFS: no channel data for %d",
200 first_chan->freq + i * 20);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800201 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700202 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800203
Hai Shalom74f70d42019-02-11 14:42:39 -0800204 /* HT 40 MHz secondary channel availability checked only for
205 * primary channel */
Hai Shalomfdcde762020-04-02 11:19:20 -0700206 if (!chan_bw_allowed(chan, bw, 1, !i)) {
207 wpa_printf(MSG_DEBUG, "DFS: bw now allowed for %d",
208 first_chan->freq + i * 20);
Hai Shalom74f70d42019-02-11 14:42:39 -0800209 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700210 }
Hai Shalom74f70d42019-02-11 14:42:39 -0800211
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700212 if (!dfs_channel_available(chan, type)) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700213 wpa_printf(MSG_DEBUG, "DFS: channel not available %d",
214 first_chan->freq + i * 20);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800215 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700216 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800217 }
218
219 return 1;
220}
221
222
Dmitry Shmidt98660862014-03-11 17:26:21 -0700223static int is_in_chanlist(struct hostapd_iface *iface,
224 struct hostapd_channel_data *chan)
225{
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700226 if (!iface->conf->acs_ch_list.num)
Dmitry Shmidt98660862014-03-11 17:26:21 -0700227 return 1;
228
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700229 return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700230}
231
232
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800233/*
234 * The function assumes HT40+ operation.
235 * Make sure to adjust the following variables after calling this:
236 * - hapd->secondary_channel
Hai Shalom81f62d82019-07-22 12:10:00 -0700237 * - hapd->vht/he_oper_centr_freq_seg0_idx
238 * - hapd->vht/he_oper_centr_freq_seg1_idx
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800239 */
240static int dfs_find_channel(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700241 struct hostapd_channel_data **ret_chan,
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700242 int idx, enum dfs_channel_type type)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700243{
244 struct hostapd_hw_modes *mode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800245 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700246 int i, channel_idx = 0, n_chans, n_chans1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700247
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800248 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700249 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700250
251 wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans);
252 for (i = 0; i < mode->num_channels; i++) {
253 chan = &mode->channels[i];
254
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800255 /* Skip HT40/VHT incompatible channels */
256 if (iface->conf->ieee80211n &&
257 iface->conf->secondary_channel &&
Hai Shalom74f70d42019-02-11 14:42:39 -0800258 (!dfs_is_chan_allowed(chan, n_chans) ||
Hai Shalomfdcde762020-04-02 11:19:20 -0700259 !(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P))) {
260 wpa_printf(MSG_DEBUG,
261 "DFS: channel %d (%d) is incompatible",
262 chan->freq, chan->chan);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700263 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -0700264 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700265
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800266 /* Skip incompatible chandefs */
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700267 if (!dfs_chan_range_available(mode, i, n_chans, type)) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700268 wpa_printf(MSG_DEBUG,
269 "DFS: range not available for %d (%d)",
270 chan->freq, chan->chan);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800271 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -0700272 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700273
Hai Shalomfdcde762020-04-02 11:19:20 -0700274 if (!is_in_chanlist(iface, chan)) {
275 wpa_printf(MSG_DEBUG,
276 "DFS: channel %d (%d) not in chanlist",
277 chan->freq, chan->chan);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700278 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -0700279 }
Dmitry Shmidt98660862014-03-11 17:26:21 -0700280
Hai Shalomc1a21442022-02-04 13:43:00 -0800281 if (chan->max_tx_power < iface->conf->min_tx_power)
282 continue;
283
Dmitry Shmidt051af732013-10-22 13:52:46 -0700284 if (ret_chan && idx == channel_idx) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700285 wpa_printf(MSG_DEBUG, "Selected channel %d (%d)",
286 chan->freq, chan->chan);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700287 *ret_chan = chan;
288 return idx;
289 }
Hai Shalomfdcde762020-04-02 11:19:20 -0700290 wpa_printf(MSG_DEBUG, "Adding channel %d (%d)",
291 chan->freq, chan->chan);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700292 channel_idx++;
293 }
294 return channel_idx;
295}
296
297
Hai Shalom81f62d82019-07-22 12:10:00 -0700298static void dfs_adjust_center_freq(struct hostapd_iface *iface,
299 struct hostapd_channel_data *chan,
300 int secondary_channel,
Hai Shalomfdcde762020-04-02 11:19:20 -0700301 int sec_chan_idx_80p80,
Hai Shalom81f62d82019-07-22 12:10:00 -0700302 u8 *oper_centr_freq_seg0_idx,
303 u8 *oper_centr_freq_seg1_idx)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700304{
Hai Shalom81f62d82019-07-22 12:10:00 -0700305 if (!iface->conf->ieee80211ac && !iface->conf->ieee80211ax)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700306 return;
307
308 if (!chan)
309 return;
310
Hai Shalom81f62d82019-07-22 12:10:00 -0700311 *oper_centr_freq_seg1_idx = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800312
Hai Shalom81f62d82019-07-22 12:10:00 -0700313 switch (hostapd_get_oper_chwidth(iface->conf)) {
314 case CHANWIDTH_USE_HT:
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800315 if (secondary_channel == 1)
Hai Shalom81f62d82019-07-22 12:10:00 -0700316 *oper_centr_freq_seg0_idx = chan->chan + 2;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800317 else if (secondary_channel == -1)
Hai Shalom81f62d82019-07-22 12:10:00 -0700318 *oper_centr_freq_seg0_idx = chan->chan - 2;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700319 else
Hai Shalom81f62d82019-07-22 12:10:00 -0700320 *oper_centr_freq_seg0_idx = chan->chan;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700321 break;
Hai Shalom81f62d82019-07-22 12:10:00 -0700322 case CHANWIDTH_80MHZ:
323 *oper_centr_freq_seg0_idx = chan->chan + 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700324 break;
Hai Shalom81f62d82019-07-22 12:10:00 -0700325 case CHANWIDTH_160MHZ:
326 *oper_centr_freq_seg0_idx = chan->chan + 14;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700327 break;
Hai Shalomfdcde762020-04-02 11:19:20 -0700328 case CHANWIDTH_80P80MHZ:
329 *oper_centr_freq_seg0_idx = chan->chan + 6;
330 *oper_centr_freq_seg1_idx = sec_chan_idx_80p80 + 6;
331 break;
332
Dmitry Shmidt051af732013-10-22 13:52:46 -0700333 default:
Hai Shalomfdcde762020-04-02 11:19:20 -0700334 wpa_printf(MSG_INFO,
335 "DFS: Unsupported channel width configuration");
Hai Shalom81f62d82019-07-22 12:10:00 -0700336 *oper_centr_freq_seg0_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700337 break;
338 }
339
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800340 wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d, %d",
Hai Shalom81f62d82019-07-22 12:10:00 -0700341 *oper_centr_freq_seg0_idx,
342 *oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700343}
344
345
346/* Return start channel idx we will use for mode->channels[idx] */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700347static int dfs_get_start_chan_idx(struct hostapd_iface *iface, int *seg1_start)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700348{
349 struct hostapd_hw_modes *mode;
350 struct hostapd_channel_data *chan;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800351 int channel_no = iface->conf->channel;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700352 int res = -1, i;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700353 int chan_seg1 = -1;
354
355 *seg1_start = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700356
357 /* HT40- */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800358 if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700359 channel_no -= 4;
360
Hai Shalom81f62d82019-07-22 12:10:00 -0700361 /* VHT/HE */
362 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
363 switch (hostapd_get_oper_chwidth(iface->conf)) {
364 case CHANWIDTH_USE_HT:
Dmitry Shmidt051af732013-10-22 13:52:46 -0700365 break;
Hai Shalom81f62d82019-07-22 12:10:00 -0700366 case CHANWIDTH_80MHZ:
367 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
368 iface->conf) - 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700369 break;
Hai Shalom81f62d82019-07-22 12:10:00 -0700370 case CHANWIDTH_160MHZ:
371 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
372 iface->conf) - 14;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700373 break;
Hai Shalom81f62d82019-07-22 12:10:00 -0700374 case CHANWIDTH_80P80MHZ:
375 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
376 iface->conf) - 6;
377 chan_seg1 = hostapd_get_oper_centr_freq_seg1_idx(
378 iface->conf) - 6;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700379 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700380 default:
381 wpa_printf(MSG_INFO,
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700382 "DFS only VHT20/40/80/160/80+80 is supported now");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700383 channel_no = -1;
384 break;
385 }
386 }
387
388 /* Get idx */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800389 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700390 for (i = 0; i < mode->num_channels; i++) {
391 chan = &mode->channels[i];
392 if (chan->chan == channel_no) {
393 res = i;
394 break;
395 }
396 }
397
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700398 if (res != -1 && chan_seg1 > -1) {
399 int found = 0;
400
401 /* Get idx for seg1 */
402 mode = iface->current_mode;
403 for (i = 0; i < mode->num_channels; i++) {
404 chan = &mode->channels[i];
405 if (chan->chan == chan_seg1) {
406 *seg1_start = i;
407 found = 1;
408 break;
409 }
410 }
411 if (!found)
412 res = -1;
413 }
414
Dmitry Shmidt98660862014-03-11 17:26:21 -0700415 if (res == -1) {
416 wpa_printf(MSG_DEBUG,
417 "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d",
418 mode->num_channels, channel_no, iface->conf->channel,
419 iface->conf->ieee80211n,
420 iface->conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700421 hostapd_get_oper_chwidth(iface->conf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700422
423 for (i = 0; i < mode->num_channels; i++) {
424 wpa_printf(MSG_DEBUG, "Available channel: %d",
425 mode->channels[i].chan);
426 }
427 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700428
429 return res;
430}
431
432
433/* At least one channel have radar flag */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800434static int dfs_check_chans_radar(struct hostapd_iface *iface,
435 int start_chan_idx, int n_chans)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700436{
437 struct hostapd_channel_data *channel;
438 struct hostapd_hw_modes *mode;
439 int i, res = 0;
440
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800441 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700442
443 for (i = 0; i < n_chans; i++) {
444 channel = &mode->channels[start_chan_idx + i];
445 if (channel->flag & HOSTAPD_CHAN_RADAR)
446 res++;
447 }
448
449 return res;
450}
451
452
453/* All channels available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800454static int dfs_check_chans_available(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700455 int start_chan_idx, int n_chans)
456{
457 struct hostapd_channel_data *channel;
458 struct hostapd_hw_modes *mode;
459 int i;
460
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800461 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700462
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800463 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700464 channel = &mode->channels[start_chan_idx + i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800465
466 if (channel->flag & HOSTAPD_CHAN_DISABLED)
467 break;
468
469 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
470 continue;
471
Dmitry Shmidt051af732013-10-22 13:52:46 -0700472 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
473 HOSTAPD_CHAN_DFS_AVAILABLE)
474 break;
475 }
476
477 return i == n_chans;
478}
479
480
481/* At least one channel unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800482static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700483 int start_chan_idx,
484 int n_chans)
485{
486 struct hostapd_channel_data *channel;
487 struct hostapd_hw_modes *mode;
488 int i, res = 0;
489
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800490 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700491
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800492 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700493 channel = &mode->channels[start_chan_idx + i];
494 if (channel->flag & HOSTAPD_CHAN_DISABLED)
495 res++;
496 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
497 HOSTAPD_CHAN_DFS_UNAVAILABLE)
498 res++;
499 }
500
501 return res;
502}
503
504
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800505static struct hostapd_channel_data *
506dfs_get_valid_channel(struct hostapd_iface *iface,
507 int *secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700508 u8 *oper_centr_freq_seg0_idx,
509 u8 *oper_centr_freq_seg1_idx,
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700510 enum dfs_channel_type type)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700511{
512 struct hostapd_hw_modes *mode;
513 struct hostapd_channel_data *chan = NULL;
Hai Shalomfdcde762020-04-02 11:19:20 -0700514 struct hostapd_channel_data *chan2 = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800515 int num_available_chandefs;
Hai Shalomfdcde762020-04-02 11:19:20 -0700516 int chan_idx, chan_idx2;
517 int sec_chan_idx_80p80 = -1;
518 int i;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700519 u32 _rand;
520
521 wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800522 *secondary_channel = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -0700523 *oper_centr_freq_seg0_idx = 0;
524 *oper_centr_freq_seg1_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700525
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800526 if (iface->current_mode == NULL)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700527 return NULL;
528
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800529 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700530 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
531 return NULL;
532
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800533 /* Get the count first */
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700534 num_available_chandefs = dfs_find_channel(iface, NULL, 0, type);
Hai Shalomfdcde762020-04-02 11:19:20 -0700535 wpa_printf(MSG_DEBUG, "DFS: num_available_chandefs=%d",
536 num_available_chandefs);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800537 if (num_available_chandefs == 0)
538 return NULL;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700539
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800540 if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800541 return NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800542 chan_idx = _rand % num_available_chandefs;
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700543 dfs_find_channel(iface, &chan, chan_idx, type);
Hai Shalomfdcde762020-04-02 11:19:20 -0700544 if (!chan) {
545 wpa_printf(MSG_DEBUG, "DFS: no random channel found");
546 return NULL;
547 }
548 wpa_printf(MSG_DEBUG, "DFS: got random channel %d (%d)",
549 chan->freq, chan->chan);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800550
551 /* dfs_find_channel() calculations assume HT40+ */
552 if (iface->conf->secondary_channel)
553 *secondary_channel = 1;
554 else
555 *secondary_channel = 0;
556
Hai Shalomfdcde762020-04-02 11:19:20 -0700557 /* Get secondary channel for HT80P80 */
558 if (hostapd_get_oper_chwidth(iface->conf) == CHANWIDTH_80P80MHZ) {
559 if (num_available_chandefs <= 1) {
560 wpa_printf(MSG_ERROR,
561 "only 1 valid chan, can't support 80+80");
562 return NULL;
563 }
564
565 /*
566 * Loop all channels except channel1 to find a valid channel2
567 * that is not adjacent to channel1.
568 */
569 for (i = 0; i < num_available_chandefs - 1; i++) {
570 /* start from chan_idx + 1, end when chan_idx - 1 */
571 chan_idx2 = (chan_idx + 1 + i) % num_available_chandefs;
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700572 dfs_find_channel(iface, &chan2, chan_idx2, type);
Hai Shalomfdcde762020-04-02 11:19:20 -0700573 if (chan2 && abs(chan2->chan - chan->chan) > 12) {
574 /* two channels are not adjacent */
575 sec_chan_idx_80p80 = chan2->chan;
576 wpa_printf(MSG_DEBUG,
577 "DFS: got second chan: %d (%d)",
578 chan2->freq, chan2->chan);
579 break;
580 }
581 }
582
583 /* Check if we got a valid secondary channel which is not
584 * adjacent to the first channel.
585 */
586 if (sec_chan_idx_80p80 == -1) {
587 wpa_printf(MSG_INFO,
588 "DFS: failed to get chan2 for 80+80");
589 return NULL;
590 }
591 }
592
Hai Shalom81f62d82019-07-22 12:10:00 -0700593 dfs_adjust_center_freq(iface, chan,
594 *secondary_channel,
Hai Shalomfdcde762020-04-02 11:19:20 -0700595 sec_chan_idx_80p80,
Hai Shalom81f62d82019-07-22 12:10:00 -0700596 oper_centr_freq_seg0_idx,
597 oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700598
599 return chan;
600}
601
602
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700603static int dfs_set_valid_channel(struct hostapd_iface *iface, int skip_radar)
604{
605 struct hostapd_channel_data *channel;
606 u8 cf1 = 0, cf2 = 0;
607 int sec = 0;
608
609 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
610 skip_radar ? DFS_AVAILABLE :
611 DFS_ANY_CHANNEL);
612 if (!channel) {
613 wpa_printf(MSG_ERROR, "could not get valid channel");
614 return -1;
615 }
616
617 iface->freq = channel->freq;
618 iface->conf->channel = channel->chan;
619 iface->conf->secondary_channel = sec;
620 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, cf1);
621 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, cf2);
622
623 return 0;
624}
625
626
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800627static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700628{
629 struct hostapd_hw_modes *mode;
630 struct hostapd_channel_data *chan = NULL;
631 int i;
632
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800633 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700634 if (mode == NULL)
635 return 0;
636
637 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800638 for (i = 0; i < iface->current_mode->num_channels; i++) {
639 chan = &iface->current_mode->channels[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -0700640 if (chan->freq == freq) {
641 if (chan->flag & HOSTAPD_CHAN_RADAR) {
642 chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
643 chan->flag |= state;
644 return 1; /* Channel found */
645 }
646 }
647 }
648 wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
649 return 0;
650}
651
652
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800653static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700654 int chan_offset, int chan_width, int cf1,
655 int cf2, u32 state)
656{
657 int n_chans = 1, i;
658 struct hostapd_hw_modes *mode;
659 int frequency = freq;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800660 int frequency2 = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700661 int ret = 0;
662
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800663 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700664 if (mode == NULL)
665 return 0;
666
667 if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
668 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
669 return 0;
670 }
671
672 /* Seems cf1 and chan_width is enough here */
673 switch (chan_width) {
674 case CHAN_WIDTH_20_NOHT:
675 case CHAN_WIDTH_20:
676 n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800677 if (frequency == 0)
678 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700679 break;
680 case CHAN_WIDTH_40:
681 n_chans = 2;
682 frequency = cf1 - 10;
683 break;
684 case CHAN_WIDTH_80:
685 n_chans = 4;
686 frequency = cf1 - 30;
687 break;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800688 case CHAN_WIDTH_80P80:
689 n_chans = 4;
690 frequency = cf1 - 30;
691 frequency2 = cf2 - 30;
692 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700693 case CHAN_WIDTH_160:
694 n_chans = 8;
695 frequency = cf1 - 70;
696 break;
697 default:
698 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
699 chan_width);
700 break;
701 }
702
703 wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
704 n_chans);
705 for (i = 0; i < n_chans; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800706 ret += set_dfs_state_freq(iface, frequency, state);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700707 frequency = frequency + 20;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800708
709 if (chan_width == CHAN_WIDTH_80P80) {
710 ret += set_dfs_state_freq(iface, frequency2, state);
711 frequency2 = frequency2 + 20;
712 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700713 }
714
715 return ret;
716}
717
718
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800719static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700720 int chan_width, int cf1, int cf2)
721{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700722 int start_chan_idx, start_chan_idx1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700723 struct hostapd_hw_modes *mode;
724 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700725 int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700726 u8 radar_chan;
727 int res = 0;
728
Dmitry Shmidt051af732013-10-22 13:52:46 -0700729 /* Our configuration */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800730 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700731 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
732 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700733
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700734 /* Check we are on DFS channel(s) */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800735 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700736 return 0;
737
Dmitry Shmidt051af732013-10-22 13:52:46 -0700738 /* Reported via radar event */
739 switch (chan_width) {
740 case CHAN_WIDTH_20_NOHT:
741 case CHAN_WIDTH_20:
742 radar_n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800743 if (frequency == 0)
744 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700745 break;
746 case CHAN_WIDTH_40:
747 radar_n_chans = 2;
748 frequency = cf1 - 10;
749 break;
750 case CHAN_WIDTH_80:
751 radar_n_chans = 4;
752 frequency = cf1 - 30;
753 break;
754 case CHAN_WIDTH_160:
755 radar_n_chans = 8;
756 frequency = cf1 - 70;
757 break;
758 default:
759 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
760 chan_width);
761 break;
762 }
763
764 ieee80211_freq_to_chan(frequency, &radar_chan);
765
766 for (i = 0; i < n_chans; i++) {
767 chan = &mode->channels[start_chan_idx + i];
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700768 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
769 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700770 for (j = 0; j < radar_n_chans; j++) {
771 wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
772 chan->chan, radar_chan + j * 4);
773 if (chan->chan == radar_chan + j * 4)
774 res++;
775 }
776 }
777
778 wpa_printf(MSG_DEBUG, "overlapped: %d", res);
779
780 return res;
781}
782
783
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700784static unsigned int dfs_get_cac_time(struct hostapd_iface *iface,
785 int start_chan_idx, int n_chans)
786{
787 struct hostapd_channel_data *channel;
788 struct hostapd_hw_modes *mode;
789 int i;
790 unsigned int cac_time_ms = 0;
791
792 mode = iface->current_mode;
793
794 for (i = 0; i < n_chans; i++) {
795 channel = &mode->channels[start_chan_idx + i];
796 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
797 continue;
798 if (channel->dfs_cac_ms > cac_time_ms)
799 cac_time_ms = channel->dfs_cac_ms;
800 }
801
802 return cac_time_ms;
803}
804
805
Dmitry Shmidt051af732013-10-22 13:52:46 -0700806/*
807 * Main DFS handler
808 * 1 - continue channel/ap setup
809 * 0 - channel/ap setup will be continued after CAC
810 * -1 - hit critical error
811 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800812int hostapd_handle_dfs(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700813{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700814 int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800815 int skip_radar = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700816
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800817 if (is_6ghz_freq(iface->freq))
818 return 1;
819
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800820 if (!iface->current_mode) {
821 /*
822 * This can happen with drivers that do not provide mode
823 * information and as such, cannot really use hostapd for DFS.
824 */
825 wpa_printf(MSG_DEBUG,
826 "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
827 return 1;
828 }
829
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800830 iface->cac_started = 0;
831
Dmitry Shmidt051af732013-10-22 13:52:46 -0700832 do {
833 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700834 start_chan_idx = dfs_get_start_chan_idx(iface,
835 &start_chan_idx1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700836 if (start_chan_idx == -1)
837 return -1;
838
839 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700840 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700841
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700842 /* Setup CAC time */
843 iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx,
844 n_chans);
845
Dmitry Shmidt051af732013-10-22 13:52:46 -0700846 /* Check if any of configured channels require DFS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800847 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700848 wpa_printf(MSG_DEBUG,
849 "DFS %d channels required radar detection",
850 res);
851 if (!res)
852 return 1;
853
854 /* Check if all channels are DFS available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800855 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700856 wpa_printf(MSG_DEBUG,
857 "DFS all channels available, (SKIP CAC): %s",
858 res ? "yes" : "no");
859 if (res)
860 return 1;
861
862 /* Check if any of configured channels is unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800863 res = dfs_check_chans_unavailable(iface, start_chan_idx,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700864 n_chans);
865 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
866 res, res ? "yes": "no");
867 if (res) {
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700868 if (dfs_set_valid_channel(iface, skip_radar) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800869 hostapd_set_state(iface, HAPD_IFACE_DFS);
870 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700871 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700872 }
873 } while (res);
874
875 /* Finally start CAC */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800876 hostapd_set_state(iface, HAPD_IFACE_DFS);
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700877 wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz%s", iface->freq,
878 dfs_use_radar_background(iface) ? " (background)" : "");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800879 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700880 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800881 iface->freq,
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800882 iface->conf->channel, iface->conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700883 hostapd_get_oper_chwidth(iface->conf),
884 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
885 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700886 iface->dfs_cac_ms / 1000);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800887
Hai Shalom81f62d82019-07-22 12:10:00 -0700888 res = hostapd_start_dfs_cac(
889 iface, iface->conf->hw_mode, iface->freq, iface->conf->channel,
890 iface->conf->ieee80211n, iface->conf->ieee80211ac,
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700891 iface->conf->ieee80211ax, iface->conf->ieee80211be,
Hai Shalom81f62d82019-07-22 12:10:00 -0700892 iface->conf->secondary_channel,
893 hostapd_get_oper_chwidth(iface->conf),
894 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700895 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
896 dfs_use_radar_background(iface));
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800897
898 if (res) {
899 wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700900 return -1;
901 }
902
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700903 if (dfs_use_radar_background(iface)) {
904 /* Cache background radar parameters. */
905 iface->radar_background.channel = iface->conf->channel;
906 iface->radar_background.secondary_channel =
907 iface->conf->secondary_channel;
908 iface->radar_background.freq = iface->freq;
909 iface->radar_background.centr_freq_seg0_idx =
910 hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
911 iface->radar_background.centr_freq_seg1_idx =
912 hostapd_get_oper_centr_freq_seg1_idx(iface->conf);
913
914 /*
915 * Let's select a random channel according to the
916 * regulations and perform CAC on dedicated radar chain.
917 */
918 res = dfs_set_valid_channel(iface, 1);
919 if (res < 0)
920 return res;
921
922 iface->radar_background.temp_ch = 1;
923 return 1;
924 }
925
Dmitry Shmidt051af732013-10-22 13:52:46 -0700926 return 0;
927}
928
929
Hai Shalomfdcde762020-04-02 11:19:20 -0700930int hostapd_is_dfs_chan_available(struct hostapd_iface *iface)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700931{
932 int n_chans, n_chans1, start_chan_idx, start_chan_idx1;
933
934 /* Get the start (first) channel for current configuration */
935 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
936 if (start_chan_idx < 0)
937 return 0;
938
939 /* Get the number of used channels, depending on width */
940 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
941
942 /* Check if all channels are DFS available */
943 return dfs_check_chans_available(iface, start_chan_idx, n_chans);
944}
945
946
Sunil Ravi59fa4b42022-05-02 22:54:18 -0700947static int hostapd_dfs_request_channel_switch(struct hostapd_iface *iface,
948 int channel, int freq,
949 int secondary_channel,
950 u8 current_vht_oper_chwidth,
951 u8 oper_centr_freq_seg0_idx,
952 u8 oper_centr_freq_seg1_idx)
953{
954 struct hostapd_hw_modes *cmode = iface->current_mode;
955 int ieee80211_mode = IEEE80211_MODE_AP, err;
956 struct csa_settings csa_settings;
957 u8 new_vht_oper_chwidth;
958 unsigned int i;
959
960 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d", channel);
961 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
962 "freq=%d chan=%d sec_chan=%d", freq, channel,
963 secondary_channel);
964
965 new_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
966 hostapd_set_oper_chwidth(iface->conf, current_vht_oper_chwidth);
967
968 /* Setup CSA request */
969 os_memset(&csa_settings, 0, sizeof(csa_settings));
970 csa_settings.cs_count = 5;
971 csa_settings.block_tx = 1;
972#ifdef CONFIG_MESH
973 if (iface->mconf)
974 ieee80211_mode = IEEE80211_MODE_MESH;
975#endif /* CONFIG_MESH */
976 err = hostapd_set_freq_params(&csa_settings.freq_params,
977 iface->conf->hw_mode,
978 freq, channel,
979 iface->conf->enable_edmg,
980 iface->conf->edmg_channel,
981 iface->conf->ieee80211n,
982 iface->conf->ieee80211ac,
983 iface->conf->ieee80211ax,
984 iface->conf->ieee80211be,
985 secondary_channel,
986 new_vht_oper_chwidth,
987 oper_centr_freq_seg0_idx,
988 oper_centr_freq_seg1_idx,
989 cmode->vht_capab,
990 &cmode->he_capab[ieee80211_mode],
991 &cmode->eht_capab[ieee80211_mode]);
992
993 if (err) {
994 wpa_printf(MSG_ERROR,
995 "DFS failed to calculate CSA freq params");
996 hostapd_disable_iface(iface);
997 return err;
998 }
999
1000 for (i = 0; i < iface->num_bss; i++) {
1001 err = hostapd_switch_channel(iface->bss[i], &csa_settings);
1002 if (err)
1003 break;
1004 }
1005
1006 if (err) {
1007 wpa_printf(MSG_WARNING,
1008 "DFS failed to schedule CSA (%d) - trying fallback",
1009 err);
1010 iface->freq = freq;
1011 iface->conf->channel = channel;
1012 iface->conf->secondary_channel = secondary_channel;
1013 hostapd_set_oper_chwidth(iface->conf, new_vht_oper_chwidth);
1014 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1015 oper_centr_freq_seg0_idx);
1016 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1017 oper_centr_freq_seg1_idx);
1018
1019 hostapd_disable_iface(iface);
1020 hostapd_enable_iface(iface);
1021
1022 return 0;
1023 }
1024
1025 /* Channel configuration will be updated once CSA completes and
1026 * ch_switch_notify event is received */
1027 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
1028
1029 return 0;
1030}
1031
1032
1033static void hostpad_dfs_update_background_chain(struct hostapd_iface *iface)
1034{
1035 int sec = 0;
1036 enum dfs_channel_type channel_type = DFS_NO_CAC_YET;
1037 struct hostapd_channel_data *channel;
1038 u8 oper_centr_freq_seg0_idx = 0;
1039 u8 oper_centr_freq_seg1_idx = 0;
1040
1041 /*
1042 * Allow selection of DFS channel in ETSI to comply with
1043 * uniform spreading.
1044 */
1045 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
1046 channel_type = DFS_ANY_CHANNEL;
1047
1048 channel = dfs_get_valid_channel(iface, &sec, &oper_centr_freq_seg0_idx,
1049 &oper_centr_freq_seg1_idx,
1050 channel_type);
1051 if (!channel ||
1052 channel->chan == iface->conf->channel ||
1053 channel->chan == iface->radar_background.channel)
1054 channel = dfs_downgrade_bandwidth(iface, &sec,
1055 &oper_centr_freq_seg0_idx,
1056 &oper_centr_freq_seg1_idx,
1057 &channel_type);
1058 if (!channel ||
1059 hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
1060 channel->freq, channel->chan,
1061 iface->conf->ieee80211n,
1062 iface->conf->ieee80211ac,
1063 iface->conf->ieee80211ax,
1064 iface->conf->ieee80211be,
1065 sec, hostapd_get_oper_chwidth(iface->conf),
1066 oper_centr_freq_seg0_idx,
1067 oper_centr_freq_seg1_idx, true)) {
1068 wpa_printf(MSG_ERROR, "DFS failed to start CAC offchannel");
1069 iface->radar_background.channel = -1;
1070 return;
1071 }
1072
1073 iface->radar_background.channel = channel->chan;
1074 iface->radar_background.freq = channel->freq;
1075 iface->radar_background.secondary_channel = sec;
1076 iface->radar_background.centr_freq_seg0_idx = oper_centr_freq_seg0_idx;
1077 iface->radar_background.centr_freq_seg1_idx = oper_centr_freq_seg1_idx;
1078
1079 wpa_printf(MSG_DEBUG,
1080 "%s: setting background chain to chan %d (%d MHz)",
1081 __func__, channel->chan, channel->freq);
1082}
1083
1084
1085static bool
1086hostapd_dfs_is_background_event(struct hostapd_iface *iface, int freq)
1087{
1088 return dfs_use_radar_background(iface) &&
1089 iface->radar_background.channel != -1 &&
1090 iface->radar_background.freq == freq;
1091}
1092
1093
1094static int
1095hostapd_dfs_start_channel_switch_background(struct hostapd_iface *iface)
1096{
1097 u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
1098
1099 iface->conf->channel = iface->radar_background.channel;
1100 iface->freq = iface->radar_background.freq;
1101 iface->conf->secondary_channel =
1102 iface->radar_background.secondary_channel;
1103 hostapd_set_oper_centr_freq_seg0_idx(
1104 iface->conf, iface->radar_background.centr_freq_seg0_idx);
1105 hostapd_set_oper_centr_freq_seg1_idx(
1106 iface->conf, iface->radar_background.centr_freq_seg1_idx);
1107
1108 hostpad_dfs_update_background_chain(iface);
1109
1110 return hostapd_dfs_request_channel_switch(
1111 iface, iface->conf->channel, iface->freq,
1112 iface->conf->secondary_channel, current_vht_oper_chwidth,
1113 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
1114 hostapd_get_oper_centr_freq_seg1_idx(iface->conf));
1115}
1116
1117
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001118int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001119 int ht_enabled, int chan_offset, int chan_width,
1120 int cf1, int cf2)
1121{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001122 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
1123 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1124 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1125
Dmitry Shmidt051af732013-10-22 13:52:46 -07001126 if (success) {
1127 /* Complete iface/ap configuration */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001128 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
1129 /* Complete AP configuration for the first bring up. */
1130 if (iface->state != HAPD_IFACE_ENABLED)
1131 hostapd_setup_interface_complete(iface, 0);
1132 else
1133 iface->cac_started = 0;
1134 } else {
1135 set_dfs_state(iface, freq, ht_enabled, chan_offset,
1136 chan_width, cf1, cf2,
1137 HOSTAPD_CHAN_DFS_AVAILABLE);
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001138
1139 /*
1140 * Radar event from background chain for the selected
1141 * channel. Perform CSA, move the main chain to the
1142 * selected channel and configure the background chain
1143 * to a new DFS channel.
1144 */
1145 if (hostapd_dfs_is_background_event(iface, freq)) {
1146 iface->radar_background.cac_started = 0;
1147 if (!iface->radar_background.temp_ch)
1148 return 0;
1149
1150 iface->radar_background.temp_ch = 0;
1151 return hostapd_dfs_start_channel_switch_background(iface);
1152 }
1153
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001154 /*
1155 * Just mark the channel available when CAC completion
1156 * event is received in enabled state. CAC result could
1157 * have been propagated from another radio having the
1158 * same regulatory configuration. When CAC completion is
1159 * received during non-HAPD_IFACE_ENABLED state, make
1160 * sure the configured channel is available because this
1161 * CAC completion event could have been propagated from
1162 * another radio.
1163 */
1164 if (iface->state != HAPD_IFACE_ENABLED &&
Hai Shalomfdcde762020-04-02 11:19:20 -07001165 hostapd_is_dfs_chan_available(iface)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001166 hostapd_setup_interface_complete(iface, 0);
1167 iface->cac_started = 0;
1168 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001169 }
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001170 } else if (hostapd_dfs_is_background_event(iface, freq)) {
1171 iface->radar_background.cac_started = 0;
1172 hostpad_dfs_update_background_chain(iface);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001173 }
1174
1175 return 0;
1176}
1177
1178
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001179int hostapd_dfs_pre_cac_expired(struct hostapd_iface *iface, int freq,
1180 int ht_enabled, int chan_offset, int chan_width,
1181 int cf1, int cf2)
1182{
1183 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_PRE_CAC_EXPIRED
1184 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1185 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1186
1187 /* Proceed only if DFS is not offloaded to the driver */
1188 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1189 return 0;
1190
1191 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1192 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
1193
1194 return 0;
1195}
1196
1197
Hai Shalomfdcde762020-04-02 11:19:20 -07001198static struct hostapd_channel_data *
1199dfs_downgrade_bandwidth(struct hostapd_iface *iface, int *secondary_channel,
1200 u8 *oper_centr_freq_seg0_idx,
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001201 u8 *oper_centr_freq_seg1_idx,
1202 enum dfs_channel_type *channel_type)
Hai Shalomfdcde762020-04-02 11:19:20 -07001203{
1204 struct hostapd_channel_data *channel;
1205
1206 for (;;) {
1207 channel = dfs_get_valid_channel(iface, secondary_channel,
1208 oper_centr_freq_seg0_idx,
1209 oper_centr_freq_seg1_idx,
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001210 *channel_type);
Hai Shalomfdcde762020-04-02 11:19:20 -07001211 if (channel) {
1212 wpa_printf(MSG_DEBUG, "DFS: Selected channel: %d",
1213 channel->chan);
1214 return channel;
1215 }
1216
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001217 if (*channel_type != DFS_ANY_CHANNEL) {
1218 *channel_type = DFS_ANY_CHANNEL;
Hai Shalomfdcde762020-04-02 11:19:20 -07001219 } else {
Hai Shalom899fcc72020-10-19 14:38:18 -07001220 int oper_chwidth;
1221
1222 oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
1223 if (oper_chwidth == CHANWIDTH_USE_HT)
Hai Shalomfdcde762020-04-02 11:19:20 -07001224 break;
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001225 *channel_type = DFS_AVAILABLE;
Hai Shalom899fcc72020-10-19 14:38:18 -07001226 hostapd_set_oper_chwidth(iface->conf, oper_chwidth - 1);
Hai Shalomfdcde762020-04-02 11:19:20 -07001227 }
1228 }
1229
1230 wpa_printf(MSG_INFO,
1231 "%s: no DFS channels left, waiting for NOP to finish",
1232 __func__);
1233 return NULL;
1234}
1235
1236
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001237static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001238{
1239 struct hostapd_channel_data *channel;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001240 int secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001241 u8 oper_centr_freq_seg0_idx = 0;
1242 u8 oper_centr_freq_seg1_idx = 0;
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001243 enum dfs_channel_type channel_type = DFS_ANY_CHANNEL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001244 int err = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001245
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001246 /* Radar detected during active CAC */
1247 iface->cac_started = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001248 channel = dfs_get_valid_channel(iface, &secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07001249 &oper_centr_freq_seg0_idx,
1250 &oper_centr_freq_seg1_idx,
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001251 channel_type);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001252
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001253 if (!channel) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001254 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1255 &oper_centr_freq_seg0_idx,
1256 &oper_centr_freq_seg1_idx,
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001257 &channel_type);
Hai Shalomfdcde762020-04-02 11:19:20 -07001258 if (!channel) {
1259 wpa_printf(MSG_ERROR, "No valid channel available");
1260 return err;
1261 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001262 }
1263
1264 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
1265 channel->chan);
1266 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
1267 "freq=%d chan=%d sec_chan=%d", channel->freq,
1268 channel->chan, secondary_channel);
1269
1270 iface->freq = channel->freq;
1271 iface->conf->channel = channel->chan;
1272 iface->conf->secondary_channel = secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001273 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1274 oper_centr_freq_seg0_idx);
1275 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1276 oper_centr_freq_seg1_idx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001277 err = 0;
1278
1279 hostapd_setup_interface_complete(iface, err);
1280 return err;
1281}
1282
1283
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001284static int
1285hostapd_dfs_background_start_channel_switch(struct hostapd_iface *iface,
1286 int freq)
1287{
1288 if (!dfs_use_radar_background(iface))
1289 return -1; /* Background radar chain not supported. */
1290
1291 wpa_printf(MSG_DEBUG,
1292 "%s called (background CAC active: %s, CSA active: %s)",
1293 __func__, iface->radar_background.cac_started ? "yes" : "no",
1294 hostapd_csa_in_progress(iface) ? "yes" : "no");
1295
1296 /* Check if CSA in progress */
1297 if (hostapd_csa_in_progress(iface))
1298 return 0;
1299
1300 if (hostapd_dfs_is_background_event(iface, freq)) {
1301 /*
1302 * Radar pattern is reported on the background chain.
1303 * Just select a new random channel according to the
1304 * regulations for monitoring.
1305 */
1306 hostpad_dfs_update_background_chain(iface);
1307 return 0;
1308 }
1309
1310 /*
1311 * If background radar detection is supported and the radar channel
1312 * monitored by the background chain is available switch to it without
1313 * waiting for the CAC.
1314 */
1315 if (iface->radar_background.channel == -1)
1316 return -1; /* Background radar chain not available. */
1317
1318 if (iface->radar_background.cac_started) {
1319 /*
1320 * Background channel not available yet. Perform CAC on the
1321 * main chain.
1322 */
1323 iface->radar_background.temp_ch = 1;
1324 return -1;
1325 }
1326
1327 return hostapd_dfs_start_channel_switch_background(iface);
1328}
1329
1330
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001331static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
1332{
1333 struct hostapd_channel_data *channel;
1334 int secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001335 u8 oper_centr_freq_seg0_idx;
1336 u8 oper_centr_freq_seg1_idx;
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001337 enum dfs_channel_type channel_type = DFS_AVAILABLE;
Hai Shalom899fcc72020-10-19 14:38:18 -07001338 u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001339
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001340 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
1341 __func__, iface->cac_started ? "yes" : "no",
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001342 hostapd_csa_in_progress(iface) ? "yes" : "no");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001343
1344 /* Check if CSA in progress */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001345 if (hostapd_csa_in_progress(iface))
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001346 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001347
1348 /* Check if active CAC */
1349 if (iface->cac_started)
1350 return hostapd_dfs_start_channel_switch_cac(iface);
1351
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001352 /*
1353 * Allow selection of DFS channel in ETSI to comply with
1354 * uniform spreading.
1355 */
1356 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001357 channel_type = DFS_ANY_CHANNEL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001358
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001359 /* Perform channel switch/CSA */
1360 channel = dfs_get_valid_channel(iface, &secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07001361 &oper_centr_freq_seg0_idx,
1362 &oper_centr_freq_seg1_idx,
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001363 channel_type);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001364
1365 if (!channel) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07001366 /*
1367 * If there is no channel to switch immediately to, check if
1368 * there is another channel where we can switch even if it
1369 * requires to perform a CAC first.
1370 */
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001371 channel_type = DFS_ANY_CHANNEL;
Hai Shalomfdcde762020-04-02 11:19:20 -07001372 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1373 &oper_centr_freq_seg0_idx,
1374 &oper_centr_freq_seg1_idx,
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001375 &channel_type);
Hai Shalom60840252021-02-19 19:02:11 -08001376 if (!channel) {
1377 /*
1378 * Toggle interface state to enter DFS state
1379 * until NOP is finished.
1380 */
1381 hostapd_disable_iface(iface);
1382 hostapd_enable_iface(iface);
1383 return 0;
1384 }
1385
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001386 if (channel_type == DFS_ANY_CHANNEL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001387 iface->freq = channel->freq;
1388 iface->conf->channel = channel->chan;
1389 iface->conf->secondary_channel = secondary_channel;
1390 hostapd_set_oper_centr_freq_seg0_idx(
1391 iface->conf, oper_centr_freq_seg0_idx);
1392 hostapd_set_oper_centr_freq_seg1_idx(
1393 iface->conf, oper_centr_freq_seg1_idx);
1394
1395 hostapd_disable_iface(iface);
1396 hostapd_enable_iface(iface);
1397 return 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001398 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001399 }
1400
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001401 return hostapd_dfs_request_channel_switch(iface, channel->chan,
1402 channel->freq,
1403 secondary_channel,
1404 current_vht_oper_chwidth,
1405 oper_centr_freq_seg0_idx,
1406 oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001407}
1408
1409
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001410int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001411 int ht_enabled, int chan_offset, int chan_width,
1412 int cf1, int cf2)
1413{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001414 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
1415 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1416 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1417
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001418 /* Proceed only if DFS is not offloaded to the driver */
1419 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1420 return 0;
1421
1422 if (!iface->conf->ieee80211h)
1423 return 0;
1424
Dmitry Shmidt051af732013-10-22 13:52:46 -07001425 /* mark radar frequency as invalid */
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001426 if (!set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1427 cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE))
Hai Shalomfdcde762020-04-02 11:19:20 -07001428 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001429
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001430 if (!hostapd_dfs_is_background_event(iface, freq)) {
1431 /* Skip if reported radar event not overlapped our channels */
1432 if (!dfs_are_channels_overlapped(iface, freq, chan_width,
1433 cf1, cf2))
1434 return 0;
1435 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001436
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001437 if (hostapd_dfs_background_start_channel_switch(iface, freq)) {
1438 /* Radar detected while operating, switch the channel. */
1439 return hostapd_dfs_start_channel_switch(iface);
1440 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001441
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001442 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001443}
1444
1445
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001446int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001447 int ht_enabled, int chan_offset, int chan_width,
1448 int cf1, int cf2)
1449{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001450 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
1451 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1452 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001453
1454 /* Proceed only if DFS is not offloaded to the driver */
1455 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1456 return 0;
1457
Dmitry Shmidt051af732013-10-22 13:52:46 -07001458 /* TODO add correct implementation here */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001459 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1460 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001461
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001462 if (iface->state == HAPD_IFACE_DFS && !iface->cac_started) {
1463 /* Handle cases where all channels were initially unavailable */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001464 hostapd_handle_dfs(iface);
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001465 } else if (dfs_use_radar_background(iface) &&
1466 iface->radar_background.channel == -1) {
1467 /* Reset radar background chain if disabled */
1468 hostpad_dfs_update_background_chain(iface);
1469 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001470
Dmitry Shmidt051af732013-10-22 13:52:46 -07001471 return 0;
1472}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001473
1474
1475int hostapd_is_dfs_required(struct hostapd_iface *iface)
1476{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001477 int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001478
Hai Shalomc1a21442022-02-04 13:43:00 -08001479 if ((!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1480 !iface->conf->ieee80211h) ||
1481 !iface->current_mode ||
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001482 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
1483 return 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001484
1485 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001486 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001487 if (start_chan_idx == -1)
1488 return -1;
1489
1490 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001491 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001492
1493 /* Check if any of configured channels require DFS */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001494 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
1495 if (res)
1496 return res;
1497 if (start_chan_idx1 >= 0 && n_chans1 > 0)
1498 res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1);
1499 return res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001500}
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001501
1502
1503int hostapd_dfs_start_cac(struct hostapd_iface *iface, int freq,
1504 int ht_enabled, int chan_offset, int chan_width,
1505 int cf1, int cf2)
1506{
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001507 if (hostapd_dfs_is_background_event(iface, freq)) {
1508 iface->radar_background.cac_started = 1;
1509 } else {
1510 /* This is called when the driver indicates that an offloaded
1511 * DFS has started CAC. */
1512 hostapd_set_state(iface, HAPD_IFACE_DFS);
1513 iface->cac_started = 1;
1514 }
Hai Shalomc3565922019-10-28 11:58:20 -07001515 /* TODO: How to check CAC time for ETSI weather channels? */
1516 iface->dfs_cac_ms = 60000;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001517 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
1518 "freq=%d chan=%d chan_offset=%d width=%d seg0=%d "
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001519 "seg1=%d cac_time=%ds%s",
Hai Shalomc3565922019-10-28 11:58:20 -07001520 freq, (freq - 5000) / 5, chan_offset, chan_width, cf1, cf2,
Sunil Ravi59fa4b42022-05-02 22:54:18 -07001521 iface->dfs_cac_ms / 1000,
1522 hostapd_dfs_is_background_event(iface, freq) ?
1523 " (background)" : "");
1524
Hai Shalomc3565922019-10-28 11:58:20 -07001525 os_get_reltime(&iface->dfs_cac_start);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001526 return 0;
1527}
1528
1529
1530/*
1531 * Main DFS handler for offloaded case.
1532 * 2 - continue channel/AP setup for non-DFS channel
1533 * 1 - continue channel/AP setup for DFS channel
1534 * 0 - channel/AP setup will be continued after CAC
1535 * -1 - hit critical error
1536 */
1537int hostapd_handle_dfs_offload(struct hostapd_iface *iface)
1538{
Hai Shalomc1a21442022-02-04 13:43:00 -08001539 int dfs_res;
1540
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001541 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1542 __func__, iface->cac_started);
1543
1544 /*
1545 * If DFS has already been started, then we are being called from a
1546 * callback to continue AP/channel setup. Reset the CAC start flag and
1547 * return.
1548 */
1549 if (iface->cac_started) {
1550 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1551 __func__, iface->cac_started);
1552 iface->cac_started = 0;
1553 return 1;
1554 }
1555
Hai Shalomc1a21442022-02-04 13:43:00 -08001556 dfs_res = hostapd_is_dfs_required(iface);
1557 if (dfs_res > 0) {
1558 wpa_printf(MSG_DEBUG,
1559 "%s: freq %d MHz requires DFS for %d chans",
1560 __func__, iface->freq, dfs_res);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001561 return 0;
1562 }
1563
1564 wpa_printf(MSG_DEBUG,
1565 "%s: freq %d MHz does not require DFS. Continue channel/AP setup",
1566 __func__, iface->freq);
1567 return 2;
1568}
Hai Shalomfdcde762020-04-02 11:19:20 -07001569
1570
1571int hostapd_is_dfs_overlap(struct hostapd_iface *iface, enum chan_width width,
1572 int center_freq)
1573{
1574 struct hostapd_channel_data *chan;
1575 struct hostapd_hw_modes *mode = iface->current_mode;
1576 int half_width;
1577 int res = 0;
1578 int i;
1579
1580 if (!iface->conf->ieee80211h || !mode ||
1581 mode->mode != HOSTAPD_MODE_IEEE80211A)
1582 return 0;
1583
1584 switch (width) {
1585 case CHAN_WIDTH_20_NOHT:
1586 case CHAN_WIDTH_20:
1587 half_width = 10;
1588 break;
1589 case CHAN_WIDTH_40:
1590 half_width = 20;
1591 break;
1592 case CHAN_WIDTH_80:
1593 case CHAN_WIDTH_80P80:
1594 half_width = 40;
1595 break;
1596 case CHAN_WIDTH_160:
1597 half_width = 80;
1598 break;
1599 default:
1600 wpa_printf(MSG_WARNING, "DFS chanwidth %d not supported",
1601 width);
1602 return 0;
1603 }
1604
1605 for (i = 0; i < mode->num_channels; i++) {
1606 chan = &mode->channels[i];
1607
1608 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
1609 continue;
1610
Hai Shalom60840252021-02-19 19:02:11 -08001611 if ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
1612 HOSTAPD_CHAN_DFS_AVAILABLE)
1613 continue;
1614
Hai Shalomfdcde762020-04-02 11:19:20 -07001615 if (center_freq - chan->freq < half_width &&
1616 chan->freq - center_freq < half_width)
1617 res++;
1618 }
1619
Hai Shalom60840252021-02-19 19:02:11 -08001620 wpa_printf(MSG_DEBUG, "DFS CAC required: (%d, %d): in range: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -07001621 center_freq - half_width, center_freq + half_width,
1622 res ? "yes" : "no");
1623
1624 return res;
1625}