blob: 146dd1ada51d1d7b8dc0152db94b7bb31f0770af [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 Ravia04bd252022-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)) {
Sunil8cd6f4d2022-06-28 18:40:46 +000053 case CONF_OPER_CHWIDTH_USE_HT:
Dmitry Shmidt051af732013-10-22 13:52:46 -070054 break;
Sunil8cd6f4d2022-06-28 18:40:46 +000055 case CONF_OPER_CHWIDTH_80MHZ:
Dmitry Shmidt051af732013-10-22 13:52:46 -070056 n_chans = 4;
57 break;
Sunil8cd6f4d2022-06-28 18:40:46 +000058 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidt051af732013-10-22 13:52:46 -070059 n_chans = 8;
60 break;
Sunil8cd6f4d2022-06-28 18:40:46 +000061 case CONF_OPER_CHWIDTH_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 Ravia04bd252022-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 Ravia04bd252022-05-02 22:54:18 -070076 enum dfs_channel_type type)
Dmitry Shmidt051af732013-10-22 13:52:46 -070077{
Sunil Ravia04bd252022-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 Ravia04bd252022-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 Ravia04bd252022-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 Ravia04bd252022-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 Ravia04bd252022-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 Ravia04bd252022-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 Shaloma20dcd72022-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)) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000314 case CONF_OPER_CHWIDTH_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;
Sunil8cd6f4d2022-06-28 18:40:46 +0000322 case CONF_OPER_CHWIDTH_80MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700323 *oper_centr_freq_seg0_idx = chan->chan + 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700324 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000325 case CONF_OPER_CHWIDTH_160MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700326 *oper_centr_freq_seg0_idx = chan->chan + 14;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700327 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000328 case CONF_OPER_CHWIDTH_80P80MHZ:
Hai Shalomfdcde762020-04-02 11:19:20 -0700329 *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)) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000364 case CONF_OPER_CHWIDTH_USE_HT:
Dmitry Shmidt051af732013-10-22 13:52:46 -0700365 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000366 case CONF_OPER_CHWIDTH_80MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700367 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
368 iface->conf) - 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700369 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000370 case CONF_OPER_CHWIDTH_160MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700371 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
372 iface->conf) - 14;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700373 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000374 case CONF_OPER_CHWIDTH_80P80MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700375 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 Ravia04bd252022-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 Ravia04bd252022-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 Ravia04bd252022-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 */
Sunil8cd6f4d2022-06-28 18:40:46 +0000558 if (hostapd_get_oper_chwidth(iface->conf) ==
559 CONF_OPER_CHWIDTH_80P80MHZ) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700560 if (num_available_chandefs <= 1) {
561 wpa_printf(MSG_ERROR,
562 "only 1 valid chan, can't support 80+80");
563 return NULL;
564 }
565
566 /*
567 * Loop all channels except channel1 to find a valid channel2
568 * that is not adjacent to channel1.
569 */
570 for (i = 0; i < num_available_chandefs - 1; i++) {
571 /* start from chan_idx + 1, end when chan_idx - 1 */
572 chan_idx2 = (chan_idx + 1 + i) % num_available_chandefs;
Sunil Ravia04bd252022-05-02 22:54:18 -0700573 dfs_find_channel(iface, &chan2, chan_idx2, type);
Hai Shalomfdcde762020-04-02 11:19:20 -0700574 if (chan2 && abs(chan2->chan - chan->chan) > 12) {
575 /* two channels are not adjacent */
576 sec_chan_idx_80p80 = chan2->chan;
577 wpa_printf(MSG_DEBUG,
578 "DFS: got second chan: %d (%d)",
579 chan2->freq, chan2->chan);
580 break;
581 }
582 }
583
584 /* Check if we got a valid secondary channel which is not
585 * adjacent to the first channel.
586 */
587 if (sec_chan_idx_80p80 == -1) {
588 wpa_printf(MSG_INFO,
589 "DFS: failed to get chan2 for 80+80");
590 return NULL;
591 }
592 }
593
Hai Shalom81f62d82019-07-22 12:10:00 -0700594 dfs_adjust_center_freq(iface, chan,
595 *secondary_channel,
Hai Shalomfdcde762020-04-02 11:19:20 -0700596 sec_chan_idx_80p80,
Hai Shalom81f62d82019-07-22 12:10:00 -0700597 oper_centr_freq_seg0_idx,
598 oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700599
600 return chan;
601}
602
603
Sunil Ravia04bd252022-05-02 22:54:18 -0700604static int dfs_set_valid_channel(struct hostapd_iface *iface, int skip_radar)
605{
606 struct hostapd_channel_data *channel;
607 u8 cf1 = 0, cf2 = 0;
608 int sec = 0;
609
610 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
611 skip_radar ? DFS_AVAILABLE :
612 DFS_ANY_CHANNEL);
613 if (!channel) {
614 wpa_printf(MSG_ERROR, "could not get valid channel");
615 return -1;
616 }
617
618 iface->freq = channel->freq;
619 iface->conf->channel = channel->chan;
620 iface->conf->secondary_channel = sec;
621 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, cf1);
622 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, cf2);
623
624 return 0;
625}
626
627
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800628static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700629{
630 struct hostapd_hw_modes *mode;
631 struct hostapd_channel_data *chan = NULL;
632 int i;
633
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800634 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700635 if (mode == NULL)
636 return 0;
637
638 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800639 for (i = 0; i < iface->current_mode->num_channels; i++) {
640 chan = &iface->current_mode->channels[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -0700641 if (chan->freq == freq) {
642 if (chan->flag & HOSTAPD_CHAN_RADAR) {
643 chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
644 chan->flag |= state;
645 return 1; /* Channel found */
646 }
647 }
648 }
649 wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
650 return 0;
651}
652
653
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800654static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700655 int chan_offset, int chan_width, int cf1,
656 int cf2, u32 state)
657{
658 int n_chans = 1, i;
659 struct hostapd_hw_modes *mode;
660 int frequency = freq;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800661 int frequency2 = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700662 int ret = 0;
663
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800664 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700665 if (mode == NULL)
666 return 0;
667
668 if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
669 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
670 return 0;
671 }
672
673 /* Seems cf1 and chan_width is enough here */
674 switch (chan_width) {
675 case CHAN_WIDTH_20_NOHT:
676 case CHAN_WIDTH_20:
677 n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800678 if (frequency == 0)
679 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700680 break;
681 case CHAN_WIDTH_40:
682 n_chans = 2;
683 frequency = cf1 - 10;
684 break;
685 case CHAN_WIDTH_80:
686 n_chans = 4;
687 frequency = cf1 - 30;
688 break;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800689 case CHAN_WIDTH_80P80:
690 n_chans = 4;
691 frequency = cf1 - 30;
692 frequency2 = cf2 - 30;
693 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700694 case CHAN_WIDTH_160:
695 n_chans = 8;
696 frequency = cf1 - 70;
697 break;
698 default:
699 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
700 chan_width);
701 break;
702 }
703
704 wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
705 n_chans);
706 for (i = 0; i < n_chans; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800707 ret += set_dfs_state_freq(iface, frequency, state);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700708 frequency = frequency + 20;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800709
710 if (chan_width == CHAN_WIDTH_80P80) {
711 ret += set_dfs_state_freq(iface, frequency2, state);
712 frequency2 = frequency2 + 20;
713 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700714 }
715
716 return ret;
717}
718
719
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800720static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700721 int chan_width, int cf1, int cf2)
722{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700723 int start_chan_idx, start_chan_idx1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700724 struct hostapd_hw_modes *mode;
725 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700726 int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700727 u8 radar_chan;
728 int res = 0;
729
Dmitry Shmidt051af732013-10-22 13:52:46 -0700730 /* Our configuration */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800731 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700732 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
733 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700734
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700735 /* Check we are on DFS channel(s) */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800736 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700737 return 0;
738
Dmitry Shmidt051af732013-10-22 13:52:46 -0700739 /* Reported via radar event */
740 switch (chan_width) {
741 case CHAN_WIDTH_20_NOHT:
742 case CHAN_WIDTH_20:
743 radar_n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800744 if (frequency == 0)
745 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700746 break;
747 case CHAN_WIDTH_40:
748 radar_n_chans = 2;
749 frequency = cf1 - 10;
750 break;
751 case CHAN_WIDTH_80:
752 radar_n_chans = 4;
753 frequency = cf1 - 30;
754 break;
755 case CHAN_WIDTH_160:
756 radar_n_chans = 8;
757 frequency = cf1 - 70;
758 break;
759 default:
760 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
761 chan_width);
762 break;
763 }
764
765 ieee80211_freq_to_chan(frequency, &radar_chan);
766
767 for (i = 0; i < n_chans; i++) {
768 chan = &mode->channels[start_chan_idx + i];
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700769 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
770 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700771 for (j = 0; j < radar_n_chans; j++) {
772 wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
773 chan->chan, radar_chan + j * 4);
774 if (chan->chan == radar_chan + j * 4)
775 res++;
776 }
777 }
778
779 wpa_printf(MSG_DEBUG, "overlapped: %d", res);
780
781 return res;
782}
783
784
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700785static unsigned int dfs_get_cac_time(struct hostapd_iface *iface,
786 int start_chan_idx, int n_chans)
787{
788 struct hostapd_channel_data *channel;
789 struct hostapd_hw_modes *mode;
790 int i;
791 unsigned int cac_time_ms = 0;
792
793 mode = iface->current_mode;
794
795 for (i = 0; i < n_chans; i++) {
796 channel = &mode->channels[start_chan_idx + i];
797 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
798 continue;
799 if (channel->dfs_cac_ms > cac_time_ms)
800 cac_time_ms = channel->dfs_cac_ms;
801 }
802
803 return cac_time_ms;
804}
805
806
Dmitry Shmidt051af732013-10-22 13:52:46 -0700807/*
808 * Main DFS handler
809 * 1 - continue channel/ap setup
810 * 0 - channel/ap setup will be continued after CAC
811 * -1 - hit critical error
812 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800813int hostapd_handle_dfs(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700814{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700815 int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800816 int skip_radar = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700817
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800818 if (is_6ghz_freq(iface->freq))
819 return 1;
820
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800821 if (!iface->current_mode) {
822 /*
823 * This can happen with drivers that do not provide mode
824 * information and as such, cannot really use hostapd for DFS.
825 */
826 wpa_printf(MSG_DEBUG,
827 "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
828 return 1;
829 }
830
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800831 iface->cac_started = 0;
832
Dmitry Shmidt051af732013-10-22 13:52:46 -0700833 do {
834 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700835 start_chan_idx = dfs_get_start_chan_idx(iface,
836 &start_chan_idx1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700837 if (start_chan_idx == -1)
838 return -1;
839
840 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700841 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700842
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700843 /* Setup CAC time */
844 iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx,
845 n_chans);
846
Dmitry Shmidt051af732013-10-22 13:52:46 -0700847 /* Check if any of configured channels require DFS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800848 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700849 wpa_printf(MSG_DEBUG,
850 "DFS %d channels required radar detection",
851 res);
852 if (!res)
853 return 1;
854
855 /* Check if all channels are DFS available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800856 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700857 wpa_printf(MSG_DEBUG,
858 "DFS all channels available, (SKIP CAC): %s",
859 res ? "yes" : "no");
860 if (res)
861 return 1;
862
863 /* Check if any of configured channels is unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800864 res = dfs_check_chans_unavailable(iface, start_chan_idx,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700865 n_chans);
866 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
867 res, res ? "yes": "no");
868 if (res) {
Sunil Ravia04bd252022-05-02 22:54:18 -0700869 if (dfs_set_valid_channel(iface, skip_radar) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800870 hostapd_set_state(iface, HAPD_IFACE_DFS);
871 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700872 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700873 }
874 } while (res);
875
876 /* Finally start CAC */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800877 hostapd_set_state(iface, HAPD_IFACE_DFS);
Sunil Ravia04bd252022-05-02 22:54:18 -0700878 wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz%s", iface->freq,
879 dfs_use_radar_background(iface) ? " (background)" : "");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800880 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700881 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800882 iface->freq,
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800883 iface->conf->channel, iface->conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700884 hostapd_get_oper_chwidth(iface->conf),
885 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
886 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700887 iface->dfs_cac_ms / 1000);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800888
Hai Shalom81f62d82019-07-22 12:10:00 -0700889 res = hostapd_start_dfs_cac(
890 iface, iface->conf->hw_mode, iface->freq, iface->conf->channel,
891 iface->conf->ieee80211n, iface->conf->ieee80211ac,
Sunil Ravia04bd252022-05-02 22:54:18 -0700892 iface->conf->ieee80211ax, iface->conf->ieee80211be,
Hai Shalom81f62d82019-07-22 12:10:00 -0700893 iface->conf->secondary_channel,
894 hostapd_get_oper_chwidth(iface->conf),
895 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
Sunil Ravia04bd252022-05-02 22:54:18 -0700896 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
897 dfs_use_radar_background(iface));
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800898
899 if (res) {
900 wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700901 return -1;
902 }
903
Sunil Ravia04bd252022-05-02 22:54:18 -0700904 if (dfs_use_radar_background(iface)) {
905 /* Cache background radar parameters. */
906 iface->radar_background.channel = iface->conf->channel;
907 iface->radar_background.secondary_channel =
908 iface->conf->secondary_channel;
909 iface->radar_background.freq = iface->freq;
910 iface->radar_background.centr_freq_seg0_idx =
911 hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
912 iface->radar_background.centr_freq_seg1_idx =
913 hostapd_get_oper_centr_freq_seg1_idx(iface->conf);
914
915 /*
916 * Let's select a random channel according to the
917 * regulations and perform CAC on dedicated radar chain.
918 */
919 res = dfs_set_valid_channel(iface, 1);
920 if (res < 0)
921 return res;
922
923 iface->radar_background.temp_ch = 1;
924 return 1;
925 }
926
Dmitry Shmidt051af732013-10-22 13:52:46 -0700927 return 0;
928}
929
930
Hai Shalomfdcde762020-04-02 11:19:20 -0700931int hostapd_is_dfs_chan_available(struct hostapd_iface *iface)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700932{
933 int n_chans, n_chans1, start_chan_idx, start_chan_idx1;
934
935 /* Get the start (first) channel for current configuration */
936 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
937 if (start_chan_idx < 0)
938 return 0;
939
940 /* Get the number of used channels, depending on width */
941 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
942
943 /* Check if all channels are DFS available */
944 return dfs_check_chans_available(iface, start_chan_idx, n_chans);
945}
946
947
Sunil Ravia04bd252022-05-02 22:54:18 -0700948static int hostapd_dfs_request_channel_switch(struct hostapd_iface *iface,
949 int channel, int freq,
950 int secondary_channel,
951 u8 current_vht_oper_chwidth,
952 u8 oper_centr_freq_seg0_idx,
953 u8 oper_centr_freq_seg1_idx)
954{
955 struct hostapd_hw_modes *cmode = iface->current_mode;
956 int ieee80211_mode = IEEE80211_MODE_AP, err;
957 struct csa_settings csa_settings;
958 u8 new_vht_oper_chwidth;
959 unsigned int i;
960
961 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d", channel);
962 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
963 "freq=%d chan=%d sec_chan=%d", freq, channel,
964 secondary_channel);
965
966 new_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
967 hostapd_set_oper_chwidth(iface->conf, current_vht_oper_chwidth);
968
969 /* Setup CSA request */
970 os_memset(&csa_settings, 0, sizeof(csa_settings));
971 csa_settings.cs_count = 5;
972 csa_settings.block_tx = 1;
973#ifdef CONFIG_MESH
974 if (iface->mconf)
975 ieee80211_mode = IEEE80211_MODE_MESH;
976#endif /* CONFIG_MESH */
977 err = hostapd_set_freq_params(&csa_settings.freq_params,
978 iface->conf->hw_mode,
979 freq, channel,
980 iface->conf->enable_edmg,
981 iface->conf->edmg_channel,
982 iface->conf->ieee80211n,
983 iface->conf->ieee80211ac,
984 iface->conf->ieee80211ax,
985 iface->conf->ieee80211be,
986 secondary_channel,
987 new_vht_oper_chwidth,
988 oper_centr_freq_seg0_idx,
989 oper_centr_freq_seg1_idx,
990 cmode->vht_capab,
991 &cmode->he_capab[ieee80211_mode],
992 &cmode->eht_capab[ieee80211_mode]);
993
994 if (err) {
995 wpa_printf(MSG_ERROR,
996 "DFS failed to calculate CSA freq params");
997 hostapd_disable_iface(iface);
998 return err;
999 }
1000
1001 for (i = 0; i < iface->num_bss; i++) {
1002 err = hostapd_switch_channel(iface->bss[i], &csa_settings);
1003 if (err)
1004 break;
1005 }
1006
1007 if (err) {
1008 wpa_printf(MSG_WARNING,
1009 "DFS failed to schedule CSA (%d) - trying fallback",
1010 err);
1011 iface->freq = freq;
1012 iface->conf->channel = channel;
1013 iface->conf->secondary_channel = secondary_channel;
1014 hostapd_set_oper_chwidth(iface->conf, new_vht_oper_chwidth);
1015 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1016 oper_centr_freq_seg0_idx);
1017 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1018 oper_centr_freq_seg1_idx);
1019
1020 hostapd_disable_iface(iface);
1021 hostapd_enable_iface(iface);
1022
1023 return 0;
1024 }
1025
1026 /* Channel configuration will be updated once CSA completes and
1027 * ch_switch_notify event is received */
1028 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
1029
1030 return 0;
1031}
1032
1033
1034static void hostpad_dfs_update_background_chain(struct hostapd_iface *iface)
1035{
1036 int sec = 0;
1037 enum dfs_channel_type channel_type = DFS_NO_CAC_YET;
1038 struct hostapd_channel_data *channel;
1039 u8 oper_centr_freq_seg0_idx = 0;
1040 u8 oper_centr_freq_seg1_idx = 0;
1041
1042 /*
1043 * Allow selection of DFS channel in ETSI to comply with
1044 * uniform spreading.
1045 */
1046 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
1047 channel_type = DFS_ANY_CHANNEL;
1048
1049 channel = dfs_get_valid_channel(iface, &sec, &oper_centr_freq_seg0_idx,
1050 &oper_centr_freq_seg1_idx,
1051 channel_type);
1052 if (!channel ||
1053 channel->chan == iface->conf->channel ||
1054 channel->chan == iface->radar_background.channel)
1055 channel = dfs_downgrade_bandwidth(iface, &sec,
1056 &oper_centr_freq_seg0_idx,
1057 &oper_centr_freq_seg1_idx,
1058 &channel_type);
1059 if (!channel ||
1060 hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
1061 channel->freq, channel->chan,
1062 iface->conf->ieee80211n,
1063 iface->conf->ieee80211ac,
1064 iface->conf->ieee80211ax,
1065 iface->conf->ieee80211be,
1066 sec, hostapd_get_oper_chwidth(iface->conf),
1067 oper_centr_freq_seg0_idx,
1068 oper_centr_freq_seg1_idx, true)) {
1069 wpa_printf(MSG_ERROR, "DFS failed to start CAC offchannel");
1070 iface->radar_background.channel = -1;
1071 return;
1072 }
1073
1074 iface->radar_background.channel = channel->chan;
1075 iface->radar_background.freq = channel->freq;
1076 iface->radar_background.secondary_channel = sec;
1077 iface->radar_background.centr_freq_seg0_idx = oper_centr_freq_seg0_idx;
1078 iface->radar_background.centr_freq_seg1_idx = oper_centr_freq_seg1_idx;
1079
1080 wpa_printf(MSG_DEBUG,
1081 "%s: setting background chain to chan %d (%d MHz)",
1082 __func__, channel->chan, channel->freq);
1083}
1084
1085
1086static bool
1087hostapd_dfs_is_background_event(struct hostapd_iface *iface, int freq)
1088{
1089 return dfs_use_radar_background(iface) &&
1090 iface->radar_background.channel != -1 &&
1091 iface->radar_background.freq == freq;
1092}
1093
1094
1095static int
1096hostapd_dfs_start_channel_switch_background(struct hostapd_iface *iface)
1097{
1098 u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
1099
1100 iface->conf->channel = iface->radar_background.channel;
1101 iface->freq = iface->radar_background.freq;
1102 iface->conf->secondary_channel =
1103 iface->radar_background.secondary_channel;
1104 hostapd_set_oper_centr_freq_seg0_idx(
1105 iface->conf, iface->radar_background.centr_freq_seg0_idx);
1106 hostapd_set_oper_centr_freq_seg1_idx(
1107 iface->conf, iface->radar_background.centr_freq_seg1_idx);
1108
1109 hostpad_dfs_update_background_chain(iface);
1110
1111 return hostapd_dfs_request_channel_switch(
1112 iface, iface->conf->channel, iface->freq,
1113 iface->conf->secondary_channel, current_vht_oper_chwidth,
1114 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
1115 hostapd_get_oper_centr_freq_seg1_idx(iface->conf));
1116}
1117
1118
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001119int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001120 int ht_enabled, int chan_offset, int chan_width,
1121 int cf1, int cf2)
1122{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001123 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
1124 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1125 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1126
Dmitry Shmidt051af732013-10-22 13:52:46 -07001127 if (success) {
1128 /* Complete iface/ap configuration */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001129 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
1130 /* Complete AP configuration for the first bring up. */
1131 if (iface->state != HAPD_IFACE_ENABLED)
1132 hostapd_setup_interface_complete(iface, 0);
1133 else
1134 iface->cac_started = 0;
1135 } else {
1136 set_dfs_state(iface, freq, ht_enabled, chan_offset,
1137 chan_width, cf1, cf2,
1138 HOSTAPD_CHAN_DFS_AVAILABLE);
Sunil Ravia04bd252022-05-02 22:54:18 -07001139
1140 /*
1141 * Radar event from background chain for the selected
1142 * channel. Perform CSA, move the main chain to the
1143 * selected channel and configure the background chain
1144 * to a new DFS channel.
1145 */
1146 if (hostapd_dfs_is_background_event(iface, freq)) {
1147 iface->radar_background.cac_started = 0;
1148 if (!iface->radar_background.temp_ch)
1149 return 0;
1150
1151 iface->radar_background.temp_ch = 0;
1152 return hostapd_dfs_start_channel_switch_background(iface);
1153 }
1154
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001155 /*
1156 * Just mark the channel available when CAC completion
1157 * event is received in enabled state. CAC result could
1158 * have been propagated from another radio having the
1159 * same regulatory configuration. When CAC completion is
1160 * received during non-HAPD_IFACE_ENABLED state, make
1161 * sure the configured channel is available because this
1162 * CAC completion event could have been propagated from
1163 * another radio.
1164 */
1165 if (iface->state != HAPD_IFACE_ENABLED &&
Hai Shalomfdcde762020-04-02 11:19:20 -07001166 hostapd_is_dfs_chan_available(iface)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001167 hostapd_setup_interface_complete(iface, 0);
1168 iface->cac_started = 0;
1169 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001170 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001171 } else if (hostapd_dfs_is_background_event(iface, freq)) {
1172 iface->radar_background.cac_started = 0;
1173 hostpad_dfs_update_background_chain(iface);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001174 }
1175
1176 return 0;
1177}
1178
1179
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001180int hostapd_dfs_pre_cac_expired(struct hostapd_iface *iface, int freq,
1181 int ht_enabled, int chan_offset, int chan_width,
1182 int cf1, int cf2)
1183{
1184 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_PRE_CAC_EXPIRED
1185 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1186 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1187
1188 /* Proceed only if DFS is not offloaded to the driver */
1189 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1190 return 0;
1191
1192 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1193 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
1194
1195 return 0;
1196}
1197
1198
Hai Shalomfdcde762020-04-02 11:19:20 -07001199static struct hostapd_channel_data *
1200dfs_downgrade_bandwidth(struct hostapd_iface *iface, int *secondary_channel,
1201 u8 *oper_centr_freq_seg0_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001202 u8 *oper_centr_freq_seg1_idx,
1203 enum dfs_channel_type *channel_type)
Hai Shalomfdcde762020-04-02 11:19:20 -07001204{
1205 struct hostapd_channel_data *channel;
1206
1207 for (;;) {
1208 channel = dfs_get_valid_channel(iface, secondary_channel,
1209 oper_centr_freq_seg0_idx,
1210 oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001211 *channel_type);
Hai Shalomfdcde762020-04-02 11:19:20 -07001212 if (channel) {
1213 wpa_printf(MSG_DEBUG, "DFS: Selected channel: %d",
1214 channel->chan);
1215 return channel;
1216 }
1217
Sunil Ravia04bd252022-05-02 22:54:18 -07001218 if (*channel_type != DFS_ANY_CHANNEL) {
1219 *channel_type = DFS_ANY_CHANNEL;
Hai Shalomfdcde762020-04-02 11:19:20 -07001220 } else {
Hai Shalom899fcc72020-10-19 14:38:18 -07001221 int oper_chwidth;
1222
1223 oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
Sunil8cd6f4d2022-06-28 18:40:46 +00001224 if (oper_chwidth == CONF_OPER_CHWIDTH_USE_HT)
Hai Shalomfdcde762020-04-02 11:19:20 -07001225 break;
Sunil Ravia04bd252022-05-02 22:54:18 -07001226 *channel_type = DFS_AVAILABLE;
Hai Shalom899fcc72020-10-19 14:38:18 -07001227 hostapd_set_oper_chwidth(iface->conf, oper_chwidth - 1);
Hai Shalomfdcde762020-04-02 11:19:20 -07001228 }
1229 }
1230
1231 wpa_printf(MSG_INFO,
1232 "%s: no DFS channels left, waiting for NOP to finish",
1233 __func__);
1234 return NULL;
1235}
1236
1237
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001238static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001239{
1240 struct hostapd_channel_data *channel;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001241 int secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001242 u8 oper_centr_freq_seg0_idx = 0;
1243 u8 oper_centr_freq_seg1_idx = 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07001244 enum dfs_channel_type channel_type = DFS_ANY_CHANNEL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001245 int err = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001246
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001247 /* Radar detected during active CAC */
1248 iface->cac_started = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001249 channel = dfs_get_valid_channel(iface, &secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07001250 &oper_centr_freq_seg0_idx,
1251 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001252 channel_type);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001253
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001254 if (!channel) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001255 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1256 &oper_centr_freq_seg0_idx,
1257 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001258 &channel_type);
Hai Shalomfdcde762020-04-02 11:19:20 -07001259 if (!channel) {
1260 wpa_printf(MSG_ERROR, "No valid channel available");
1261 return err;
1262 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001263 }
1264
1265 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
1266 channel->chan);
1267 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
1268 "freq=%d chan=%d sec_chan=%d", channel->freq,
1269 channel->chan, secondary_channel);
1270
1271 iface->freq = channel->freq;
1272 iface->conf->channel = channel->chan;
1273 iface->conf->secondary_channel = secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001274 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1275 oper_centr_freq_seg0_idx);
1276 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1277 oper_centr_freq_seg1_idx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001278 err = 0;
1279
1280 hostapd_setup_interface_complete(iface, err);
1281 return err;
1282}
1283
1284
Sunil Ravia04bd252022-05-02 22:54:18 -07001285static int
1286hostapd_dfs_background_start_channel_switch(struct hostapd_iface *iface,
1287 int freq)
1288{
1289 if (!dfs_use_radar_background(iface))
1290 return -1; /* Background radar chain not supported. */
1291
1292 wpa_printf(MSG_DEBUG,
1293 "%s called (background CAC active: %s, CSA active: %s)",
1294 __func__, iface->radar_background.cac_started ? "yes" : "no",
1295 hostapd_csa_in_progress(iface) ? "yes" : "no");
1296
1297 /* Check if CSA in progress */
1298 if (hostapd_csa_in_progress(iface))
1299 return 0;
1300
1301 if (hostapd_dfs_is_background_event(iface, freq)) {
1302 /*
1303 * Radar pattern is reported on the background chain.
1304 * Just select a new random channel according to the
1305 * regulations for monitoring.
1306 */
1307 hostpad_dfs_update_background_chain(iface);
1308 return 0;
1309 }
1310
1311 /*
1312 * If background radar detection is supported and the radar channel
1313 * monitored by the background chain is available switch to it without
1314 * waiting for the CAC.
1315 */
1316 if (iface->radar_background.channel == -1)
1317 return -1; /* Background radar chain not available. */
1318
1319 if (iface->radar_background.cac_started) {
1320 /*
1321 * Background channel not available yet. Perform CAC on the
1322 * main chain.
1323 */
1324 iface->radar_background.temp_ch = 1;
1325 return -1;
1326 }
1327
1328 return hostapd_dfs_start_channel_switch_background(iface);
1329}
1330
1331
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001332static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
1333{
1334 struct hostapd_channel_data *channel;
1335 int secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001336 u8 oper_centr_freq_seg0_idx;
1337 u8 oper_centr_freq_seg1_idx;
Sunil Ravia04bd252022-05-02 22:54:18 -07001338 enum dfs_channel_type channel_type = DFS_AVAILABLE;
Hai Shalom899fcc72020-10-19 14:38:18 -07001339 u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001340
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001341 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
1342 __func__, iface->cac_started ? "yes" : "no",
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001343 hostapd_csa_in_progress(iface) ? "yes" : "no");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001344
1345 /* Check if CSA in progress */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001346 if (hostapd_csa_in_progress(iface))
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001347 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001348
1349 /* Check if active CAC */
1350 if (iface->cac_started)
1351 return hostapd_dfs_start_channel_switch_cac(iface);
1352
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001353 /*
1354 * Allow selection of DFS channel in ETSI to comply with
1355 * uniform spreading.
1356 */
1357 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
Sunil Ravia04bd252022-05-02 22:54:18 -07001358 channel_type = DFS_ANY_CHANNEL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001359
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001360 /* Perform channel switch/CSA */
1361 channel = dfs_get_valid_channel(iface, &secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07001362 &oper_centr_freq_seg0_idx,
1363 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001364 channel_type);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001365
1366 if (!channel) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07001367 /*
1368 * If there is no channel to switch immediately to, check if
1369 * there is another channel where we can switch even if it
1370 * requires to perform a CAC first.
1371 */
Sunil Ravia04bd252022-05-02 22:54:18 -07001372 channel_type = DFS_ANY_CHANNEL;
Hai Shalomfdcde762020-04-02 11:19:20 -07001373 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1374 &oper_centr_freq_seg0_idx,
1375 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001376 &channel_type);
Hai Shalom60840252021-02-19 19:02:11 -08001377 if (!channel) {
1378 /*
1379 * Toggle interface state to enter DFS state
1380 * until NOP is finished.
1381 */
1382 hostapd_disable_iface(iface);
1383 hostapd_enable_iface(iface);
1384 return 0;
1385 }
1386
Sunil Ravia04bd252022-05-02 22:54:18 -07001387 if (channel_type == DFS_ANY_CHANNEL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001388 iface->freq = channel->freq;
1389 iface->conf->channel = channel->chan;
1390 iface->conf->secondary_channel = secondary_channel;
1391 hostapd_set_oper_centr_freq_seg0_idx(
1392 iface->conf, oper_centr_freq_seg0_idx);
1393 hostapd_set_oper_centr_freq_seg1_idx(
1394 iface->conf, oper_centr_freq_seg1_idx);
1395
1396 hostapd_disable_iface(iface);
1397 hostapd_enable_iface(iface);
1398 return 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001399 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001400 }
1401
Sunil Ravia04bd252022-05-02 22:54:18 -07001402 return hostapd_dfs_request_channel_switch(iface, channel->chan,
1403 channel->freq,
1404 secondary_channel,
1405 current_vht_oper_chwidth,
1406 oper_centr_freq_seg0_idx,
1407 oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001408}
1409
1410
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001411int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001412 int ht_enabled, int chan_offset, int chan_width,
1413 int cf1, int cf2)
1414{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001415 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
1416 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1417 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1418
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001419 /* Proceed only if DFS is not offloaded to the driver */
1420 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1421 return 0;
1422
1423 if (!iface->conf->ieee80211h)
1424 return 0;
1425
Dmitry Shmidt051af732013-10-22 13:52:46 -07001426 /* mark radar frequency as invalid */
Sunil Ravia04bd252022-05-02 22:54:18 -07001427 if (!set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1428 cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE))
Hai Shalomfdcde762020-04-02 11:19:20 -07001429 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001430
Sunil Ravia04bd252022-05-02 22:54:18 -07001431 if (!hostapd_dfs_is_background_event(iface, freq)) {
1432 /* Skip if reported radar event not overlapped our channels */
1433 if (!dfs_are_channels_overlapped(iface, freq, chan_width,
1434 cf1, cf2))
1435 return 0;
1436 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001437
Sunil Ravia04bd252022-05-02 22:54:18 -07001438 if (hostapd_dfs_background_start_channel_switch(iface, freq)) {
1439 /* Radar detected while operating, switch the channel. */
1440 return hostapd_dfs_start_channel_switch(iface);
1441 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001442
Sunil Ravia04bd252022-05-02 22:54:18 -07001443 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001444}
1445
1446
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001447int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001448 int ht_enabled, int chan_offset, int chan_width,
1449 int cf1, int cf2)
1450{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001451 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
1452 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1453 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001454
1455 /* Proceed only if DFS is not offloaded to the driver */
1456 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1457 return 0;
1458
Dmitry Shmidt051af732013-10-22 13:52:46 -07001459 /* TODO add correct implementation here */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001460 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1461 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001462
Sunil Ravia04bd252022-05-02 22:54:18 -07001463 if (iface->state == HAPD_IFACE_DFS && !iface->cac_started) {
1464 /* Handle cases where all channels were initially unavailable */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001465 hostapd_handle_dfs(iface);
Sunil Ravia04bd252022-05-02 22:54:18 -07001466 } else if (dfs_use_radar_background(iface) &&
1467 iface->radar_background.channel == -1) {
1468 /* Reset radar background chain if disabled */
1469 hostpad_dfs_update_background_chain(iface);
1470 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001471
Dmitry Shmidt051af732013-10-22 13:52:46 -07001472 return 0;
1473}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001474
1475
1476int hostapd_is_dfs_required(struct hostapd_iface *iface)
1477{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001478 int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001479
Hai Shaloma20dcd72022-02-04 13:43:00 -08001480 if ((!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1481 !iface->conf->ieee80211h) ||
1482 !iface->current_mode ||
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001483 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
1484 return 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001485
1486 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001487 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001488 if (start_chan_idx == -1)
1489 return -1;
1490
1491 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001492 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001493
1494 /* Check if any of configured channels require DFS */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001495 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
1496 if (res)
1497 return res;
1498 if (start_chan_idx1 >= 0 && n_chans1 > 0)
1499 res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1);
1500 return res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001501}
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001502
1503
1504int hostapd_dfs_start_cac(struct hostapd_iface *iface, int freq,
1505 int ht_enabled, int chan_offset, int chan_width,
1506 int cf1, int cf2)
1507{
Sunil Ravia04bd252022-05-02 22:54:18 -07001508 if (hostapd_dfs_is_background_event(iface, freq)) {
1509 iface->radar_background.cac_started = 1;
1510 } else {
1511 /* This is called when the driver indicates that an offloaded
1512 * DFS has started CAC. */
1513 hostapd_set_state(iface, HAPD_IFACE_DFS);
1514 iface->cac_started = 1;
1515 }
Hai Shalomc3565922019-10-28 11:58:20 -07001516 /* TODO: How to check CAC time for ETSI weather channels? */
1517 iface->dfs_cac_ms = 60000;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001518 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
1519 "freq=%d chan=%d chan_offset=%d width=%d seg0=%d "
Sunil Ravia04bd252022-05-02 22:54:18 -07001520 "seg1=%d cac_time=%ds%s",
Hai Shalomc3565922019-10-28 11:58:20 -07001521 freq, (freq - 5000) / 5, chan_offset, chan_width, cf1, cf2,
Sunil Ravia04bd252022-05-02 22:54:18 -07001522 iface->dfs_cac_ms / 1000,
1523 hostapd_dfs_is_background_event(iface, freq) ?
1524 " (background)" : "");
1525
Hai Shalomc3565922019-10-28 11:58:20 -07001526 os_get_reltime(&iface->dfs_cac_start);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001527 return 0;
1528}
1529
1530
1531/*
1532 * Main DFS handler for offloaded case.
1533 * 2 - continue channel/AP setup for non-DFS channel
1534 * 1 - continue channel/AP setup for DFS channel
1535 * 0 - channel/AP setup will be continued after CAC
1536 * -1 - hit critical error
1537 */
1538int hostapd_handle_dfs_offload(struct hostapd_iface *iface)
1539{
Hai Shaloma20dcd72022-02-04 13:43:00 -08001540 int dfs_res;
1541
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001542 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1543 __func__, iface->cac_started);
1544
1545 /*
1546 * If DFS has already been started, then we are being called from a
1547 * callback to continue AP/channel setup. Reset the CAC start flag and
1548 * return.
1549 */
1550 if (iface->cac_started) {
1551 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1552 __func__, iface->cac_started);
1553 iface->cac_started = 0;
1554 return 1;
1555 }
1556
Hai Shaloma20dcd72022-02-04 13:43:00 -08001557 dfs_res = hostapd_is_dfs_required(iface);
1558 if (dfs_res > 0) {
1559 wpa_printf(MSG_DEBUG,
1560 "%s: freq %d MHz requires DFS for %d chans",
1561 __func__, iface->freq, dfs_res);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001562 return 0;
1563 }
1564
1565 wpa_printf(MSG_DEBUG,
1566 "%s: freq %d MHz does not require DFS. Continue channel/AP setup",
1567 __func__, iface->freq);
1568 return 2;
1569}
Hai Shalomfdcde762020-04-02 11:19:20 -07001570
1571
1572int hostapd_is_dfs_overlap(struct hostapd_iface *iface, enum chan_width width,
1573 int center_freq)
1574{
1575 struct hostapd_channel_data *chan;
1576 struct hostapd_hw_modes *mode = iface->current_mode;
1577 int half_width;
1578 int res = 0;
1579 int i;
1580
1581 if (!iface->conf->ieee80211h || !mode ||
1582 mode->mode != HOSTAPD_MODE_IEEE80211A)
1583 return 0;
1584
1585 switch (width) {
1586 case CHAN_WIDTH_20_NOHT:
1587 case CHAN_WIDTH_20:
1588 half_width = 10;
1589 break;
1590 case CHAN_WIDTH_40:
1591 half_width = 20;
1592 break;
1593 case CHAN_WIDTH_80:
1594 case CHAN_WIDTH_80P80:
1595 half_width = 40;
1596 break;
1597 case CHAN_WIDTH_160:
1598 half_width = 80;
1599 break;
1600 default:
1601 wpa_printf(MSG_WARNING, "DFS chanwidth %d not supported",
1602 width);
1603 return 0;
1604 }
1605
1606 for (i = 0; i < mode->num_channels; i++) {
1607 chan = &mode->channels[i];
1608
1609 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
1610 continue;
1611
Hai Shalom60840252021-02-19 19:02:11 -08001612 if ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
1613 HOSTAPD_CHAN_DFS_AVAILABLE)
1614 continue;
1615
Hai Shalomfdcde762020-04-02 11:19:20 -07001616 if (center_freq - chan->freq < half_width &&
1617 chan->freq - center_freq < half_width)
1618 res++;
1619 }
1620
Hai Shalom60840252021-02-19 19:02:11 -08001621 wpa_printf(MSG_DEBUG, "DFS CAC required: (%d, %d): in range: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -07001622 center_freq - half_width, center_freq + half_width,
1623 res ? "yes" : "no");
1624
1625 return res;
1626}