blob: af9dc16f5987c128244d67209b2f6abd4f46cca1 [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"
Sunil Ravi99c035e2024-07-12 01:42:03 +000017#include "beacon.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070018#include "ap_drv_ops.h"
19#include "drivers/driver.h"
20#include "dfs.h"
21
22
Sunil Ravia04bd252022-05-02 22:54:18 -070023enum dfs_channel_type {
24 DFS_ANY_CHANNEL,
25 DFS_AVAILABLE, /* non-radar or radar-available */
26 DFS_NO_CAC_YET, /* radar-not-yet-available */
27};
28
29static struct hostapd_channel_data *
30dfs_downgrade_bandwidth(struct hostapd_iface *iface, int *secondary_channel,
31 u8 *oper_centr_freq_seg0_idx,
32 u8 *oper_centr_freq_seg1_idx,
33 enum dfs_channel_type *channel_type);
34
35
36static bool dfs_use_radar_background(struct hostapd_iface *iface)
37{
Sunil Ravi2a14cf12023-11-21 00:54:38 +000038 return (iface->drv_flags2 & WPA_DRIVER_FLAGS2_RADAR_BACKGROUND) &&
Sunil Ravia04bd252022-05-02 22:54:18 -070039 iface->conf->enable_background_radar;
40}
41
42
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070043static int dfs_get_used_n_chans(struct hostapd_iface *iface, int *seg1)
Dmitry Shmidt051af732013-10-22 13:52:46 -070044{
45 int n_chans = 1;
46
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070047 *seg1 = 0;
48
Dmitry Shmidtcce06662013-11-04 18:44:24 -080049 if (iface->conf->ieee80211n && iface->conf->secondary_channel)
Dmitry Shmidt051af732013-10-22 13:52:46 -070050 n_chans = 2;
51
Hai Shalom81f62d82019-07-22 12:10:00 -070052 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
53 switch (hostapd_get_oper_chwidth(iface->conf)) {
Sunil8cd6f4d2022-06-28 18:40:46 +000054 case CONF_OPER_CHWIDTH_USE_HT:
Dmitry Shmidt051af732013-10-22 13:52:46 -070055 break;
Sunil8cd6f4d2022-06-28 18:40:46 +000056 case CONF_OPER_CHWIDTH_80MHZ:
Dmitry Shmidt051af732013-10-22 13:52:46 -070057 n_chans = 4;
58 break;
Sunil8cd6f4d2022-06-28 18:40:46 +000059 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidt051af732013-10-22 13:52:46 -070060 n_chans = 8;
61 break;
Sunil8cd6f4d2022-06-28 18:40:46 +000062 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070063 n_chans = 4;
64 *seg1 = 4;
65 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -070066 default:
67 break;
68 }
69 }
70
71 return n_chans;
72}
73
74
Sunil Ravia04bd252022-05-02 22:54:18 -070075/* dfs_channel_available: select new channel according to type parameter */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080076static int dfs_channel_available(struct hostapd_channel_data *chan,
Sunil Ravia04bd252022-05-02 22:54:18 -070077 enum dfs_channel_type type)
Dmitry Shmidt051af732013-10-22 13:52:46 -070078{
Sunil Ravia04bd252022-05-02 22:54:18 -070079 if (type == DFS_NO_CAC_YET) {
80 /* Select only radar channel where CAC has not been
81 * performed yet
82 */
83 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
84 (chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
85 HOSTAPD_CHAN_DFS_USABLE)
86 return 1;
87 return 0;
88 }
89
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080090 /*
91 * When radar detection happens, CSA is performed. However, there's no
92 * time for CAC, so radar channels must be skipped when finding a new
Dmitry Shmidt98660862014-03-11 17:26:21 -070093 * channel for CSA, unless they are available for immediate use.
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080094 */
Sunil Ravia04bd252022-05-02 22:54:18 -070095 if (type == DFS_AVAILABLE && (chan->flag & HOSTAPD_CHAN_RADAR) &&
Dmitry Shmidt98660862014-03-11 17:26:21 -070096 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) !=
97 HOSTAPD_CHAN_DFS_AVAILABLE))
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080098 return 0;
99
Dmitry Shmidt051af732013-10-22 13:52:46 -0700100 if (chan->flag & HOSTAPD_CHAN_DISABLED)
101 return 0;
102 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
103 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
104 HOSTAPD_CHAN_DFS_UNAVAILABLE))
105 return 0;
106 return 1;
107}
108
109
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700110static int dfs_is_chan_allowed(struct hostapd_channel_data *chan, int n_chans)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700111{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700112 /*
113 * The tables contain first valid channel number based on channel width.
114 * We will also choose this first channel as the control one.
115 */
116 int allowed_40[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
Hai Shalom60840252021-02-19 19:02:11 -0800117 165, 173, 184, 192 };
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700118 /*
119 * VHT80, valid channels based on center frequency:
Hai Shalom60840252021-02-19 19:02:11 -0800120 * 42, 58, 106, 122, 138, 155, 171
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700121 */
Hai Shalom60840252021-02-19 19:02:11 -0800122 int allowed_80[] = { 36, 52, 100, 116, 132, 149, 165 };
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800123 /*
124 * VHT160 valid channels based on center frequency:
Hai Shalom60840252021-02-19 19:02:11 -0800125 * 50, 114, 163
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800126 */
Hai Shalom60840252021-02-19 19:02:11 -0800127 int allowed_160[] = { 36, 100, 149 };
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700128 int *allowed = allowed_40;
129 unsigned int i, allowed_no = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700130
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700131 switch (n_chans) {
132 case 2:
133 allowed = allowed_40;
134 allowed_no = ARRAY_SIZE(allowed_40);
135 break;
136 case 4:
137 allowed = allowed_80;
138 allowed_no = ARRAY_SIZE(allowed_80);
139 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800140 case 8:
141 allowed = allowed_160;
142 allowed_no = ARRAY_SIZE(allowed_160);
143 break;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700144 default:
145 wpa_printf(MSG_DEBUG, "Unknown width for %d channels", n_chans);
146 break;
147 }
148
149 for (i = 0; i < allowed_no; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700150 if (chan->chan == allowed[i])
151 return 1;
152 }
153
154 return 0;
155}
156
157
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700158static struct hostapd_channel_data *
159dfs_get_chan_data(struct hostapd_hw_modes *mode, int freq, int first_chan_idx)
160{
161 int i;
162
163 for (i = first_chan_idx; i < mode->num_channels; i++) {
164 if (mode->channels[i].freq == freq)
165 return &mode->channels[i];
166 }
167
168 return NULL;
169}
170
171
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800172static int dfs_chan_range_available(struct hostapd_hw_modes *mode,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800173 int first_chan_idx, int num_chans,
Sunil Ravia04bd252022-05-02 22:54:18 -0700174 enum dfs_channel_type type)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800175{
176 struct hostapd_channel_data *first_chan, *chan;
177 int i;
Hai Shalom74f70d42019-02-11 14:42:39 -0800178 u32 bw = num_chan_to_bw(num_chans);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800179
Hai Shalomfdcde762020-04-02 11:19:20 -0700180 if (first_chan_idx + num_chans > mode->num_channels) {
181 wpa_printf(MSG_DEBUG,
182 "DFS: some channels in range not defined");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800183 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700184 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800185
186 first_chan = &mode->channels[first_chan_idx];
187
Hai Shalom74f70d42019-02-11 14:42:39 -0800188 /* hostapd DFS implementation assumes the first channel as primary.
189 * If it's not allowed to use the first channel as primary, decline the
190 * whole channel range. */
Hai Shalomfdcde762020-04-02 11:19:20 -0700191 if (!chan_pri_allowed(first_chan)) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000192 wpa_printf(MSG_DEBUG, "DFS: primary channel not allowed");
Hai Shalom74f70d42019-02-11 14:42:39 -0800193 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700194 }
Hai Shalom74f70d42019-02-11 14:42:39 -0800195
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800196 for (i = 0; i < num_chans; i++) {
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700197 chan = dfs_get_chan_data(mode, first_chan->freq + i * 20,
198 first_chan_idx);
Hai Shalomfdcde762020-04-02 11:19:20 -0700199 if (!chan) {
200 wpa_printf(MSG_DEBUG, "DFS: no channel data for %d",
201 first_chan->freq + i * 20);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800202 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700203 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800204
Hai Shalom74f70d42019-02-11 14:42:39 -0800205 /* HT 40 MHz secondary channel availability checked only for
206 * primary channel */
Hai Shalomfdcde762020-04-02 11:19:20 -0700207 if (!chan_bw_allowed(chan, bw, 1, !i)) {
208 wpa_printf(MSG_DEBUG, "DFS: bw now allowed for %d",
209 first_chan->freq + i * 20);
Hai Shalom74f70d42019-02-11 14:42:39 -0800210 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700211 }
Hai Shalom74f70d42019-02-11 14:42:39 -0800212
Sunil Ravia04bd252022-05-02 22:54:18 -0700213 if (!dfs_channel_available(chan, type)) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700214 wpa_printf(MSG_DEBUG, "DFS: channel not available %d",
215 first_chan->freq + i * 20);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800216 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700217 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800218 }
219
220 return 1;
221}
222
223
Dmitry Shmidt98660862014-03-11 17:26:21 -0700224static int is_in_chanlist(struct hostapd_iface *iface,
225 struct hostapd_channel_data *chan)
226{
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700227 if (!iface->conf->acs_ch_list.num)
Dmitry Shmidt98660862014-03-11 17:26:21 -0700228 return 1;
229
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700230 return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700231}
232
233
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800234/*
235 * The function assumes HT40+ operation.
236 * Make sure to adjust the following variables after calling this:
237 * - hapd->secondary_channel
Hai Shalom81f62d82019-07-22 12:10:00 -0700238 * - hapd->vht/he_oper_centr_freq_seg0_idx
239 * - hapd->vht/he_oper_centr_freq_seg1_idx
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800240 */
241static int dfs_find_channel(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700242 struct hostapd_channel_data **ret_chan,
Sunil Ravia04bd252022-05-02 22:54:18 -0700243 int idx, enum dfs_channel_type type)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700244{
245 struct hostapd_hw_modes *mode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800246 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700247 int i, channel_idx = 0, n_chans, n_chans1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700248
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800249 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700250 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700251
252 wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans);
253 for (i = 0; i < mode->num_channels; i++) {
254 chan = &mode->channels[i];
255
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800256 /* Skip HT40/VHT incompatible channels */
257 if (iface->conf->ieee80211n &&
258 iface->conf->secondary_channel &&
Hai Shalom74f70d42019-02-11 14:42:39 -0800259 (!dfs_is_chan_allowed(chan, n_chans) ||
Hai Shalomfdcde762020-04-02 11:19:20 -0700260 !(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P))) {
261 wpa_printf(MSG_DEBUG,
262 "DFS: channel %d (%d) is incompatible",
263 chan->freq, chan->chan);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700264 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -0700265 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700266
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800267 /* Skip incompatible chandefs */
Sunil Ravia04bd252022-05-02 22:54:18 -0700268 if (!dfs_chan_range_available(mode, i, n_chans, type)) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700269 wpa_printf(MSG_DEBUG,
270 "DFS: range not available for %d (%d)",
271 chan->freq, chan->chan);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800272 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -0700273 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700274
Hai Shalomfdcde762020-04-02 11:19:20 -0700275 if (!is_in_chanlist(iface, chan)) {
276 wpa_printf(MSG_DEBUG,
277 "DFS: channel %d (%d) not in chanlist",
278 chan->freq, chan->chan);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700279 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -0700280 }
Dmitry Shmidt98660862014-03-11 17:26:21 -0700281
Hai Shaloma20dcd72022-02-04 13:43:00 -0800282 if (chan->max_tx_power < iface->conf->min_tx_power)
283 continue;
284
Sunil Ravi89eba102022-09-13 21:04:37 -0700285 if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) &&
286 iface->conf->country[2] == 0x4f)
287 continue;
288
Dmitry Shmidt051af732013-10-22 13:52:46 -0700289 if (ret_chan && idx == channel_idx) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700290 wpa_printf(MSG_DEBUG, "Selected channel %d (%d)",
291 chan->freq, chan->chan);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700292 *ret_chan = chan;
293 return idx;
294 }
Hai Shalomfdcde762020-04-02 11:19:20 -0700295 wpa_printf(MSG_DEBUG, "Adding channel %d (%d)",
296 chan->freq, chan->chan);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700297 channel_idx++;
298 }
299 return channel_idx;
300}
301
302
Hai Shalom81f62d82019-07-22 12:10:00 -0700303static void dfs_adjust_center_freq(struct hostapd_iface *iface,
304 struct hostapd_channel_data *chan,
305 int secondary_channel,
Hai Shalomfdcde762020-04-02 11:19:20 -0700306 int sec_chan_idx_80p80,
Hai Shalom81f62d82019-07-22 12:10:00 -0700307 u8 *oper_centr_freq_seg0_idx,
308 u8 *oper_centr_freq_seg1_idx)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700309{
Hai Shalom81f62d82019-07-22 12:10:00 -0700310 if (!iface->conf->ieee80211ac && !iface->conf->ieee80211ax)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700311 return;
312
313 if (!chan)
314 return;
315
Hai Shalom81f62d82019-07-22 12:10:00 -0700316 *oper_centr_freq_seg1_idx = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800317
Hai Shalom81f62d82019-07-22 12:10:00 -0700318 switch (hostapd_get_oper_chwidth(iface->conf)) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000319 case CONF_OPER_CHWIDTH_USE_HT:
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800320 if (secondary_channel == 1)
Hai Shalom81f62d82019-07-22 12:10:00 -0700321 *oper_centr_freq_seg0_idx = chan->chan + 2;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800322 else if (secondary_channel == -1)
Hai Shalom81f62d82019-07-22 12:10:00 -0700323 *oper_centr_freq_seg0_idx = chan->chan - 2;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700324 else
Hai Shalom81f62d82019-07-22 12:10:00 -0700325 *oper_centr_freq_seg0_idx = chan->chan;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700326 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000327 case CONF_OPER_CHWIDTH_80MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700328 *oper_centr_freq_seg0_idx = chan->chan + 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700329 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000330 case CONF_OPER_CHWIDTH_160MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700331 *oper_centr_freq_seg0_idx = chan->chan + 14;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700332 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000333 case CONF_OPER_CHWIDTH_80P80MHZ:
Hai Shalomfdcde762020-04-02 11:19:20 -0700334 *oper_centr_freq_seg0_idx = chan->chan + 6;
335 *oper_centr_freq_seg1_idx = sec_chan_idx_80p80 + 6;
336 break;
337
Dmitry Shmidt051af732013-10-22 13:52:46 -0700338 default:
Hai Shalomfdcde762020-04-02 11:19:20 -0700339 wpa_printf(MSG_INFO,
340 "DFS: Unsupported channel width configuration");
Hai Shalom81f62d82019-07-22 12:10:00 -0700341 *oper_centr_freq_seg0_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700342 break;
343 }
344
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800345 wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d, %d",
Hai Shalom81f62d82019-07-22 12:10:00 -0700346 *oper_centr_freq_seg0_idx,
347 *oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700348}
349
350
351/* Return start channel idx we will use for mode->channels[idx] */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700352static int dfs_get_start_chan_idx(struct hostapd_iface *iface, int *seg1_start)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700353{
354 struct hostapd_hw_modes *mode;
355 struct hostapd_channel_data *chan;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800356 int channel_no = iface->conf->channel;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700357 int res = -1, i;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700358 int chan_seg1 = -1;
359
360 *seg1_start = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700361
362 /* HT40- */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800363 if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700364 channel_no -= 4;
365
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000366 /* VHT/HE/EHT */
367 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax ||
368 iface->conf->ieee80211be) {
Hai Shalom81f62d82019-07-22 12:10:00 -0700369 switch (hostapd_get_oper_chwidth(iface->conf)) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000370 case CONF_OPER_CHWIDTH_USE_HT:
Dmitry Shmidt051af732013-10-22 13:52:46 -0700371 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000372 case CONF_OPER_CHWIDTH_80MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700373 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
374 iface->conf) - 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700375 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000376 case CONF_OPER_CHWIDTH_160MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700377 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
378 iface->conf) - 14;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700379 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000380 case CONF_OPER_CHWIDTH_80P80MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700381 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
382 iface->conf) - 6;
383 chan_seg1 = hostapd_get_oper_centr_freq_seg1_idx(
384 iface->conf) - 6;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700385 break;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000386 case CONF_OPER_CHWIDTH_320MHZ:
387 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
388 iface->conf) - 30;
389 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700390 default:
391 wpa_printf(MSG_INFO,
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000392 "DFS only EHT20/40/80/160/80+80/320 is supported now");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700393 channel_no = -1;
394 break;
395 }
396 }
397
398 /* Get idx */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800399 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700400 for (i = 0; i < mode->num_channels; i++) {
401 chan = &mode->channels[i];
402 if (chan->chan == channel_no) {
403 res = i;
404 break;
405 }
406 }
407
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700408 if (res != -1 && chan_seg1 > -1) {
409 int found = 0;
410
411 /* Get idx for seg1 */
412 mode = iface->current_mode;
413 for (i = 0; i < mode->num_channels; i++) {
414 chan = &mode->channels[i];
415 if (chan->chan == chan_seg1) {
416 *seg1_start = i;
417 found = 1;
418 break;
419 }
420 }
421 if (!found)
422 res = -1;
423 }
424
Dmitry Shmidt98660862014-03-11 17:26:21 -0700425 if (res == -1) {
426 wpa_printf(MSG_DEBUG,
427 "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d",
428 mode->num_channels, channel_no, iface->conf->channel,
429 iface->conf->ieee80211n,
430 iface->conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700431 hostapd_get_oper_chwidth(iface->conf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700432
433 for (i = 0; i < mode->num_channels; i++) {
434 wpa_printf(MSG_DEBUG, "Available channel: %d",
435 mode->channels[i].chan);
436 }
437 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700438
439 return res;
440}
441
442
443/* At least one channel have radar flag */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800444static int dfs_check_chans_radar(struct hostapd_iface *iface,
445 int start_chan_idx, int n_chans)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700446{
447 struct hostapd_channel_data *channel;
448 struct hostapd_hw_modes *mode;
449 int i, res = 0;
450
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800451 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700452
453 for (i = 0; i < n_chans; i++) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000454 if (start_chan_idx + i >= mode->num_channels)
455 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700456 channel = &mode->channels[start_chan_idx + i];
457 if (channel->flag & HOSTAPD_CHAN_RADAR)
458 res++;
459 }
460
461 return res;
462}
463
464
465/* All channels available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800466static int dfs_check_chans_available(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700467 int start_chan_idx, int n_chans)
468{
469 struct hostapd_channel_data *channel;
470 struct hostapd_hw_modes *mode;
471 int i;
472
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800473 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700474
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800475 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700476 channel = &mode->channels[start_chan_idx + i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800477
478 if (channel->flag & HOSTAPD_CHAN_DISABLED)
479 break;
480
481 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
482 continue;
483
Dmitry Shmidt051af732013-10-22 13:52:46 -0700484 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
485 HOSTAPD_CHAN_DFS_AVAILABLE)
486 break;
487 }
488
489 return i == n_chans;
490}
491
492
493/* At least one channel unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800494static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700495 int start_chan_idx,
496 int n_chans)
497{
498 struct hostapd_channel_data *channel;
499 struct hostapd_hw_modes *mode;
500 int i, res = 0;
501
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800502 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700503
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800504 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700505 channel = &mode->channels[start_chan_idx + i];
506 if (channel->flag & HOSTAPD_CHAN_DISABLED)
507 res++;
508 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
509 HOSTAPD_CHAN_DFS_UNAVAILABLE)
510 res++;
511 }
512
513 return res;
514}
515
516
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800517static struct hostapd_channel_data *
518dfs_get_valid_channel(struct hostapd_iface *iface,
519 int *secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700520 u8 *oper_centr_freq_seg0_idx,
521 u8 *oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -0700522 enum dfs_channel_type type)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700523{
524 struct hostapd_hw_modes *mode;
525 struct hostapd_channel_data *chan = NULL;
Hai Shalomfdcde762020-04-02 11:19:20 -0700526 struct hostapd_channel_data *chan2 = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800527 int num_available_chandefs;
Hai Shalomfdcde762020-04-02 11:19:20 -0700528 int chan_idx, chan_idx2;
529 int sec_chan_idx_80p80 = -1;
530 int i;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700531 u32 _rand;
532
533 wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800534 *secondary_channel = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -0700535 *oper_centr_freq_seg0_idx = 0;
536 *oper_centr_freq_seg1_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700537
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800538 if (iface->current_mode == NULL)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700539 return NULL;
540
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800541 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700542 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
543 return NULL;
544
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800545 /* Get the count first */
Sunil Ravia04bd252022-05-02 22:54:18 -0700546 num_available_chandefs = dfs_find_channel(iface, NULL, 0, type);
Hai Shalomfdcde762020-04-02 11:19:20 -0700547 wpa_printf(MSG_DEBUG, "DFS: num_available_chandefs=%d",
548 num_available_chandefs);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800549 if (num_available_chandefs == 0)
550 return NULL;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700551
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800552 if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800553 return NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800554 chan_idx = _rand % num_available_chandefs;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000555 wpa_printf(MSG_DEBUG, "DFS: Picked random entry from the list: %d/%d",
556 chan_idx, num_available_chandefs);
Sunil Ravia04bd252022-05-02 22:54:18 -0700557 dfs_find_channel(iface, &chan, chan_idx, type);
Hai Shalomfdcde762020-04-02 11:19:20 -0700558 if (!chan) {
559 wpa_printf(MSG_DEBUG, "DFS: no random channel found");
560 return NULL;
561 }
562 wpa_printf(MSG_DEBUG, "DFS: got random channel %d (%d)",
563 chan->freq, chan->chan);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800564
565 /* dfs_find_channel() calculations assume HT40+ */
566 if (iface->conf->secondary_channel)
567 *secondary_channel = 1;
568 else
569 *secondary_channel = 0;
570
Hai Shalomfdcde762020-04-02 11:19:20 -0700571 /* Get secondary channel for HT80P80 */
Sunil8cd6f4d2022-06-28 18:40:46 +0000572 if (hostapd_get_oper_chwidth(iface->conf) ==
573 CONF_OPER_CHWIDTH_80P80MHZ) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700574 if (num_available_chandefs <= 1) {
575 wpa_printf(MSG_ERROR,
576 "only 1 valid chan, can't support 80+80");
577 return NULL;
578 }
579
580 /*
581 * Loop all channels except channel1 to find a valid channel2
582 * that is not adjacent to channel1.
583 */
584 for (i = 0; i < num_available_chandefs - 1; i++) {
585 /* start from chan_idx + 1, end when chan_idx - 1 */
586 chan_idx2 = (chan_idx + 1 + i) % num_available_chandefs;
Sunil Ravia04bd252022-05-02 22:54:18 -0700587 dfs_find_channel(iface, &chan2, chan_idx2, type);
Hai Shalomfdcde762020-04-02 11:19:20 -0700588 if (chan2 && abs(chan2->chan - chan->chan) > 12) {
589 /* two channels are not adjacent */
590 sec_chan_idx_80p80 = chan2->chan;
591 wpa_printf(MSG_DEBUG,
592 "DFS: got second chan: %d (%d)",
593 chan2->freq, chan2->chan);
594 break;
595 }
596 }
597
598 /* Check if we got a valid secondary channel which is not
599 * adjacent to the first channel.
600 */
601 if (sec_chan_idx_80p80 == -1) {
602 wpa_printf(MSG_INFO,
603 "DFS: failed to get chan2 for 80+80");
604 return NULL;
605 }
606 }
607
Hai Shalom81f62d82019-07-22 12:10:00 -0700608 dfs_adjust_center_freq(iface, chan,
609 *secondary_channel,
Hai Shalomfdcde762020-04-02 11:19:20 -0700610 sec_chan_idx_80p80,
Hai Shalom81f62d82019-07-22 12:10:00 -0700611 oper_centr_freq_seg0_idx,
612 oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700613
614 return chan;
615}
616
617
Sunil Ravia04bd252022-05-02 22:54:18 -0700618static int dfs_set_valid_channel(struct hostapd_iface *iface, int skip_radar)
619{
620 struct hostapd_channel_data *channel;
621 u8 cf1 = 0, cf2 = 0;
622 int sec = 0;
623
624 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
625 skip_radar ? DFS_AVAILABLE :
626 DFS_ANY_CHANNEL);
627 if (!channel) {
628 wpa_printf(MSG_ERROR, "could not get valid channel");
629 return -1;
630 }
631
632 iface->freq = channel->freq;
633 iface->conf->channel = channel->chan;
634 iface->conf->secondary_channel = sec;
635 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, cf1);
636 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, cf2);
637
638 return 0;
639}
640
641
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800642static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700643{
644 struct hostapd_hw_modes *mode;
645 struct hostapd_channel_data *chan = NULL;
646 int i;
647
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800648 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700649 if (mode == NULL)
650 return 0;
651
652 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800653 for (i = 0; i < iface->current_mode->num_channels; i++) {
654 chan = &iface->current_mode->channels[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -0700655 if (chan->freq == freq) {
656 if (chan->flag & HOSTAPD_CHAN_RADAR) {
657 chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
658 chan->flag |= state;
659 return 1; /* Channel found */
660 }
661 }
662 }
663 wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
664 return 0;
665}
666
667
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800668static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700669 int chan_offset, int chan_width, int cf1,
670 int cf2, u32 state)
671{
672 int n_chans = 1, i;
673 struct hostapd_hw_modes *mode;
674 int frequency = freq;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800675 int frequency2 = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700676 int ret = 0;
677
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800678 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700679 if (mode == NULL)
680 return 0;
681
682 if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
683 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
684 return 0;
685 }
686
687 /* Seems cf1 and chan_width is enough here */
688 switch (chan_width) {
689 case CHAN_WIDTH_20_NOHT:
690 case CHAN_WIDTH_20:
691 n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800692 if (frequency == 0)
693 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700694 break;
695 case CHAN_WIDTH_40:
696 n_chans = 2;
697 frequency = cf1 - 10;
698 break;
699 case CHAN_WIDTH_80:
700 n_chans = 4;
701 frequency = cf1 - 30;
702 break;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800703 case CHAN_WIDTH_80P80:
704 n_chans = 4;
705 frequency = cf1 - 30;
706 frequency2 = cf2 - 30;
707 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700708 case CHAN_WIDTH_160:
709 n_chans = 8;
710 frequency = cf1 - 70;
711 break;
712 default:
713 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
714 chan_width);
715 break;
716 }
717
718 wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
719 n_chans);
720 for (i = 0; i < n_chans; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800721 ret += set_dfs_state_freq(iface, frequency, state);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700722 frequency = frequency + 20;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800723
724 if (chan_width == CHAN_WIDTH_80P80) {
725 ret += set_dfs_state_freq(iface, frequency2, state);
726 frequency2 = frequency2 + 20;
727 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700728 }
729
730 return ret;
731}
732
733
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800734static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700735 int chan_width, int cf1, int cf2)
736{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700737 int start_chan_idx, start_chan_idx1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700738 struct hostapd_hw_modes *mode;
739 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700740 int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700741 u8 radar_chan;
742 int res = 0;
743
Dmitry Shmidt051af732013-10-22 13:52:46 -0700744 /* Our configuration */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800745 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700746 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
747 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700748
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700749 /* Check we are on DFS channel(s) */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800750 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700751 return 0;
752
Dmitry Shmidt051af732013-10-22 13:52:46 -0700753 /* Reported via radar event */
754 switch (chan_width) {
755 case CHAN_WIDTH_20_NOHT:
756 case CHAN_WIDTH_20:
757 radar_n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800758 if (frequency == 0)
759 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700760 break;
761 case CHAN_WIDTH_40:
762 radar_n_chans = 2;
763 frequency = cf1 - 10;
764 break;
765 case CHAN_WIDTH_80:
766 radar_n_chans = 4;
767 frequency = cf1 - 30;
768 break;
769 case CHAN_WIDTH_160:
770 radar_n_chans = 8;
771 frequency = cf1 - 70;
772 break;
773 default:
774 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
775 chan_width);
776 break;
777 }
778
779 ieee80211_freq_to_chan(frequency, &radar_chan);
780
781 for (i = 0; i < n_chans; i++) {
782 chan = &mode->channels[start_chan_idx + i];
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700783 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
784 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700785 for (j = 0; j < radar_n_chans; j++) {
786 wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
787 chan->chan, radar_chan + j * 4);
788 if (chan->chan == radar_chan + j * 4)
789 res++;
790 }
791 }
792
793 wpa_printf(MSG_DEBUG, "overlapped: %d", res);
794
795 return res;
796}
797
798
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700799static unsigned int dfs_get_cac_time(struct hostapd_iface *iface,
800 int start_chan_idx, int n_chans)
801{
802 struct hostapd_channel_data *channel;
803 struct hostapd_hw_modes *mode;
804 int i;
805 unsigned int cac_time_ms = 0;
806
807 mode = iface->current_mode;
808
809 for (i = 0; i < n_chans; i++) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000810 if (start_chan_idx + i >= mode->num_channels)
811 break;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700812 channel = &mode->channels[start_chan_idx + i];
813 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
814 continue;
815 if (channel->dfs_cac_ms > cac_time_ms)
816 cac_time_ms = channel->dfs_cac_ms;
817 }
818
819 return cac_time_ms;
820}
821
822
Dmitry Shmidt051af732013-10-22 13:52:46 -0700823/*
824 * Main DFS handler
825 * 1 - continue channel/ap setup
826 * 0 - channel/ap setup will be continued after CAC
827 * -1 - hit critical error
828 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800829int hostapd_handle_dfs(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700830{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700831 int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800832 int skip_radar = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700833
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800834 if (is_6ghz_freq(iface->freq))
835 return 1;
836
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800837 if (!iface->current_mode) {
838 /*
839 * This can happen with drivers that do not provide mode
840 * information and as such, cannot really use hostapd for DFS.
841 */
842 wpa_printf(MSG_DEBUG,
843 "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
844 return 1;
845 }
846
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800847 iface->cac_started = 0;
848
Dmitry Shmidt051af732013-10-22 13:52:46 -0700849 do {
850 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700851 start_chan_idx = dfs_get_start_chan_idx(iface,
852 &start_chan_idx1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700853 if (start_chan_idx == -1)
854 return -1;
855
856 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700857 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700858
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700859 /* Setup CAC time */
860 iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx,
861 n_chans);
862
Dmitry Shmidt051af732013-10-22 13:52:46 -0700863 /* Check if any of configured channels require DFS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800864 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700865 wpa_printf(MSG_DEBUG,
866 "DFS %d channels required radar detection",
867 res);
868 if (!res)
869 return 1;
870
871 /* Check if all channels are DFS available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800872 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700873 wpa_printf(MSG_DEBUG,
874 "DFS all channels available, (SKIP CAC): %s",
875 res ? "yes" : "no");
876 if (res)
877 return 1;
878
879 /* Check if any of configured channels is unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800880 res = dfs_check_chans_unavailable(iface, start_chan_idx,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700881 n_chans);
882 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
883 res, res ? "yes": "no");
884 if (res) {
Sunil Ravia04bd252022-05-02 22:54:18 -0700885 if (dfs_set_valid_channel(iface, skip_radar) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800886 hostapd_set_state(iface, HAPD_IFACE_DFS);
887 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700888 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700889 }
890 } while (res);
891
892 /* Finally start CAC */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800893 hostapd_set_state(iface, HAPD_IFACE_DFS);
Sunil Ravia04bd252022-05-02 22:54:18 -0700894 wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz%s", iface->freq,
895 dfs_use_radar_background(iface) ? " (background)" : "");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800896 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700897 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800898 iface->freq,
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800899 iface->conf->channel, iface->conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700900 hostapd_get_oper_chwidth(iface->conf),
901 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
902 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700903 iface->dfs_cac_ms / 1000);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800904
Hai Shalom81f62d82019-07-22 12:10:00 -0700905 res = hostapd_start_dfs_cac(
906 iface, iface->conf->hw_mode, iface->freq, iface->conf->channel,
907 iface->conf->ieee80211n, iface->conf->ieee80211ac,
Sunil Ravia04bd252022-05-02 22:54:18 -0700908 iface->conf->ieee80211ax, iface->conf->ieee80211be,
Hai Shalom81f62d82019-07-22 12:10:00 -0700909 iface->conf->secondary_channel,
910 hostapd_get_oper_chwidth(iface->conf),
911 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
Sunil Ravia04bd252022-05-02 22:54:18 -0700912 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
913 dfs_use_radar_background(iface));
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800914
915 if (res) {
916 wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700917 return -1;
918 }
919
Sunil Ravia04bd252022-05-02 22:54:18 -0700920 if (dfs_use_radar_background(iface)) {
921 /* Cache background radar parameters. */
922 iface->radar_background.channel = iface->conf->channel;
923 iface->radar_background.secondary_channel =
924 iface->conf->secondary_channel;
925 iface->radar_background.freq = iface->freq;
926 iface->radar_background.centr_freq_seg0_idx =
927 hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
928 iface->radar_background.centr_freq_seg1_idx =
929 hostapd_get_oper_centr_freq_seg1_idx(iface->conf);
930
931 /*
932 * Let's select a random channel according to the
933 * regulations and perform CAC on dedicated radar chain.
934 */
935 res = dfs_set_valid_channel(iface, 1);
936 if (res < 0)
937 return res;
938
939 iface->radar_background.temp_ch = 1;
940 return 1;
941 }
942
Dmitry Shmidt051af732013-10-22 13:52:46 -0700943 return 0;
944}
945
946
Hai Shalomfdcde762020-04-02 11:19:20 -0700947int hostapd_is_dfs_chan_available(struct hostapd_iface *iface)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700948{
949 int n_chans, n_chans1, start_chan_idx, start_chan_idx1;
950
951 /* Get the start (first) channel for current configuration */
952 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
953 if (start_chan_idx < 0)
954 return 0;
955
956 /* Get the number of used channels, depending on width */
957 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
958
959 /* Check if all channels are DFS available */
960 return dfs_check_chans_available(iface, start_chan_idx, n_chans);
961}
962
963
Sunil Ravia04bd252022-05-02 22:54:18 -0700964static int hostapd_dfs_request_channel_switch(struct hostapd_iface *iface,
965 int channel, int freq,
966 int secondary_channel,
967 u8 current_vht_oper_chwidth,
968 u8 oper_centr_freq_seg0_idx,
969 u8 oper_centr_freq_seg1_idx)
970{
971 struct hostapd_hw_modes *cmode = iface->current_mode;
972 int ieee80211_mode = IEEE80211_MODE_AP, err;
973 struct csa_settings csa_settings;
974 u8 new_vht_oper_chwidth;
975 unsigned int i;
Sunil Ravi99c035e2024-07-12 01:42:03 +0000976 unsigned int num_err = 0;
Sunil Ravia04bd252022-05-02 22:54:18 -0700977
978 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d", channel);
979 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
980 "freq=%d chan=%d sec_chan=%d", freq, channel,
981 secondary_channel);
982
983 new_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
984 hostapd_set_oper_chwidth(iface->conf, current_vht_oper_chwidth);
985
986 /* Setup CSA request */
987 os_memset(&csa_settings, 0, sizeof(csa_settings));
988 csa_settings.cs_count = 5;
989 csa_settings.block_tx = 1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000990 csa_settings.link_id = -1;
991#ifdef CONFIG_IEEE80211BE
992 if (iface->bss[0]->conf->mld_ap)
993 csa_settings.link_id = iface->bss[0]->mld_link_id;
994#endif /* CONFIG_IEEE80211BE */
Sunil Ravia04bd252022-05-02 22:54:18 -0700995#ifdef CONFIG_MESH
996 if (iface->mconf)
997 ieee80211_mode = IEEE80211_MODE_MESH;
998#endif /* CONFIG_MESH */
999 err = hostapd_set_freq_params(&csa_settings.freq_params,
1000 iface->conf->hw_mode,
1001 freq, channel,
1002 iface->conf->enable_edmg,
1003 iface->conf->edmg_channel,
1004 iface->conf->ieee80211n,
1005 iface->conf->ieee80211ac,
1006 iface->conf->ieee80211ax,
1007 iface->conf->ieee80211be,
1008 secondary_channel,
1009 new_vht_oper_chwidth,
1010 oper_centr_freq_seg0_idx,
1011 oper_centr_freq_seg1_idx,
1012 cmode->vht_capab,
1013 &cmode->he_capab[ieee80211_mode],
Sunil Ravi99c035e2024-07-12 01:42:03 +00001014 &cmode->eht_capab[ieee80211_mode],
1015 hostapd_get_punct_bitmap(iface->bss[0]));
Sunil Ravia04bd252022-05-02 22:54:18 -07001016
1017 if (err) {
1018 wpa_printf(MSG_ERROR,
1019 "DFS failed to calculate CSA freq params");
1020 hostapd_disable_iface(iface);
1021 return err;
1022 }
1023
1024 for (i = 0; i < iface->num_bss; i++) {
1025 err = hostapd_switch_channel(iface->bss[i], &csa_settings);
1026 if (err)
Sunil Ravi99c035e2024-07-12 01:42:03 +00001027 num_err++;
Sunil Ravia04bd252022-05-02 22:54:18 -07001028 }
1029
Sunil Ravi99c035e2024-07-12 01:42:03 +00001030 if (num_err == iface->num_bss) {
Sunil Ravia04bd252022-05-02 22:54:18 -07001031 wpa_printf(MSG_WARNING,
1032 "DFS failed to schedule CSA (%d) - trying fallback",
1033 err);
1034 iface->freq = freq;
1035 iface->conf->channel = channel;
1036 iface->conf->secondary_channel = secondary_channel;
1037 hostapd_set_oper_chwidth(iface->conf, new_vht_oper_chwidth);
1038 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1039 oper_centr_freq_seg0_idx);
1040 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1041 oper_centr_freq_seg1_idx);
1042
1043 hostapd_disable_iface(iface);
1044 hostapd_enable_iface(iface);
1045
1046 return 0;
1047 }
1048
1049 /* Channel configuration will be updated once CSA completes and
1050 * ch_switch_notify event is received */
1051 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
1052
1053 return 0;
1054}
1055
1056
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001057static void hostapd_dfs_update_background_chain(struct hostapd_iface *iface)
Sunil Ravia04bd252022-05-02 22:54:18 -07001058{
1059 int sec = 0;
1060 enum dfs_channel_type channel_type = DFS_NO_CAC_YET;
1061 struct hostapd_channel_data *channel;
1062 u8 oper_centr_freq_seg0_idx = 0;
1063 u8 oper_centr_freq_seg1_idx = 0;
1064
1065 /*
1066 * Allow selection of DFS channel in ETSI to comply with
1067 * uniform spreading.
1068 */
1069 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
1070 channel_type = DFS_ANY_CHANNEL;
1071
1072 channel = dfs_get_valid_channel(iface, &sec, &oper_centr_freq_seg0_idx,
1073 &oper_centr_freq_seg1_idx,
1074 channel_type);
1075 if (!channel ||
1076 channel->chan == iface->conf->channel ||
1077 channel->chan == iface->radar_background.channel)
1078 channel = dfs_downgrade_bandwidth(iface, &sec,
1079 &oper_centr_freq_seg0_idx,
1080 &oper_centr_freq_seg1_idx,
1081 &channel_type);
1082 if (!channel ||
1083 hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
1084 channel->freq, channel->chan,
1085 iface->conf->ieee80211n,
1086 iface->conf->ieee80211ac,
1087 iface->conf->ieee80211ax,
1088 iface->conf->ieee80211be,
1089 sec, hostapd_get_oper_chwidth(iface->conf),
1090 oper_centr_freq_seg0_idx,
1091 oper_centr_freq_seg1_idx, true)) {
1092 wpa_printf(MSG_ERROR, "DFS failed to start CAC offchannel");
1093 iface->radar_background.channel = -1;
1094 return;
1095 }
1096
1097 iface->radar_background.channel = channel->chan;
1098 iface->radar_background.freq = channel->freq;
1099 iface->radar_background.secondary_channel = sec;
1100 iface->radar_background.centr_freq_seg0_idx = oper_centr_freq_seg0_idx;
1101 iface->radar_background.centr_freq_seg1_idx = oper_centr_freq_seg1_idx;
1102
1103 wpa_printf(MSG_DEBUG,
1104 "%s: setting background chain to chan %d (%d MHz)",
1105 __func__, channel->chan, channel->freq);
1106}
1107
1108
1109static bool
1110hostapd_dfs_is_background_event(struct hostapd_iface *iface, int freq)
1111{
1112 return dfs_use_radar_background(iface) &&
1113 iface->radar_background.channel != -1 &&
1114 iface->radar_background.freq == freq;
1115}
1116
1117
1118static int
1119hostapd_dfs_start_channel_switch_background(struct hostapd_iface *iface)
1120{
1121 u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
1122
1123 iface->conf->channel = iface->radar_background.channel;
1124 iface->freq = iface->radar_background.freq;
1125 iface->conf->secondary_channel =
1126 iface->radar_background.secondary_channel;
1127 hostapd_set_oper_centr_freq_seg0_idx(
1128 iface->conf, iface->radar_background.centr_freq_seg0_idx);
1129 hostapd_set_oper_centr_freq_seg1_idx(
1130 iface->conf, iface->radar_background.centr_freq_seg1_idx);
1131
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001132 hostapd_dfs_update_background_chain(iface);
Sunil Ravia04bd252022-05-02 22:54:18 -07001133
1134 return hostapd_dfs_request_channel_switch(
1135 iface, iface->conf->channel, iface->freq,
1136 iface->conf->secondary_channel, current_vht_oper_chwidth,
1137 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
1138 hostapd_get_oper_centr_freq_seg1_idx(iface->conf));
1139}
1140
1141
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001142int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001143 int ht_enabled, int chan_offset, int chan_width,
1144 int cf1, int cf2)
1145{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001146 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
Sunil Ravi99c035e2024-07-12 01:42:03 +00001147 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d radar_detected=%d",
1148 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2,
1149 iface->radar_detected);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001150
Dmitry Shmidt051af732013-10-22 13:52:46 -07001151 if (success) {
1152 /* Complete iface/ap configuration */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001153 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00001154 /* Complete AP configuration for the first bring up. If
1155 * a radar was detected in this channel, interface setup
1156 * will be handled in
1157 * 1. hostapd_event_ch_switch() if switching to a
1158 * non-DFS channel
1159 * 2. on next CAC complete event if switching to another
1160 * DFS channel.
1161 */
1162 if (iface->state != HAPD_IFACE_ENABLED &&
1163 !iface->radar_detected)
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001164 hostapd_setup_interface_complete(iface, 0);
1165 else
1166 iface->cac_started = 0;
1167 } else {
1168 set_dfs_state(iface, freq, ht_enabled, chan_offset,
1169 chan_width, cf1, cf2,
1170 HOSTAPD_CHAN_DFS_AVAILABLE);
Sunil Ravia04bd252022-05-02 22:54:18 -07001171
1172 /*
1173 * Radar event from background chain for the selected
1174 * channel. Perform CSA, move the main chain to the
1175 * selected channel and configure the background chain
1176 * to a new DFS channel.
1177 */
1178 if (hostapd_dfs_is_background_event(iface, freq)) {
1179 iface->radar_background.cac_started = 0;
1180 if (!iface->radar_background.temp_ch)
1181 return 0;
1182
1183 iface->radar_background.temp_ch = 0;
1184 return hostapd_dfs_start_channel_switch_background(iface);
1185 }
1186
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001187 /*
1188 * Just mark the channel available when CAC completion
1189 * event is received in enabled state. CAC result could
1190 * have been propagated from another radio having the
1191 * same regulatory configuration. When CAC completion is
1192 * received during non-HAPD_IFACE_ENABLED state, make
1193 * sure the configured channel is available because this
1194 * CAC completion event could have been propagated from
1195 * another radio.
1196 */
1197 if (iface->state != HAPD_IFACE_ENABLED &&
Hai Shalomfdcde762020-04-02 11:19:20 -07001198 hostapd_is_dfs_chan_available(iface)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001199 hostapd_setup_interface_complete(iface, 0);
1200 iface->cac_started = 0;
1201 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001202 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001203 } else if (hostapd_dfs_is_background_event(iface, freq)) {
1204 iface->radar_background.cac_started = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001205 hostapd_dfs_update_background_chain(iface);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001206 }
1207
Sunil Ravi99c035e2024-07-12 01:42:03 +00001208 iface->radar_detected = false;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001209 return 0;
1210}
1211
1212
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001213int hostapd_dfs_pre_cac_expired(struct hostapd_iface *iface, int freq,
1214 int ht_enabled, int chan_offset, int chan_width,
1215 int cf1, int cf2)
1216{
1217 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_PRE_CAC_EXPIRED
1218 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1219 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1220
1221 /* Proceed only if DFS is not offloaded to the driver */
1222 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1223 return 0;
1224
1225 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1226 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
1227
1228 return 0;
1229}
1230
1231
Hai Shalomfdcde762020-04-02 11:19:20 -07001232static struct hostapd_channel_data *
1233dfs_downgrade_bandwidth(struct hostapd_iface *iface, int *secondary_channel,
1234 u8 *oper_centr_freq_seg0_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001235 u8 *oper_centr_freq_seg1_idx,
1236 enum dfs_channel_type *channel_type)
Hai Shalomfdcde762020-04-02 11:19:20 -07001237{
1238 struct hostapd_channel_data *channel;
1239
1240 for (;;) {
1241 channel = dfs_get_valid_channel(iface, secondary_channel,
1242 oper_centr_freq_seg0_idx,
1243 oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001244 *channel_type);
Hai Shalomfdcde762020-04-02 11:19:20 -07001245 if (channel) {
1246 wpa_printf(MSG_DEBUG, "DFS: Selected channel: %d",
1247 channel->chan);
1248 return channel;
1249 }
1250
Sunil Ravia04bd252022-05-02 22:54:18 -07001251 if (*channel_type != DFS_ANY_CHANNEL) {
1252 *channel_type = DFS_ANY_CHANNEL;
Hai Shalomfdcde762020-04-02 11:19:20 -07001253 } else {
Hai Shalom899fcc72020-10-19 14:38:18 -07001254 int oper_chwidth;
1255
1256 oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
Sunil8cd6f4d2022-06-28 18:40:46 +00001257 if (oper_chwidth == CONF_OPER_CHWIDTH_USE_HT)
Hai Shalomfdcde762020-04-02 11:19:20 -07001258 break;
Sunil Ravia04bd252022-05-02 22:54:18 -07001259 *channel_type = DFS_AVAILABLE;
Hai Shalom899fcc72020-10-19 14:38:18 -07001260 hostapd_set_oper_chwidth(iface->conf, oper_chwidth - 1);
Hai Shalomfdcde762020-04-02 11:19:20 -07001261 }
1262 }
1263
1264 wpa_printf(MSG_INFO,
1265 "%s: no DFS channels left, waiting for NOP to finish",
1266 __func__);
1267 return NULL;
1268}
1269
1270
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001271static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001272{
1273 struct hostapd_channel_data *channel;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001274 int secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001275 u8 oper_centr_freq_seg0_idx = 0;
1276 u8 oper_centr_freq_seg1_idx = 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07001277 enum dfs_channel_type channel_type = DFS_ANY_CHANNEL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001278 int err = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001279
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001280 /* Radar detected during active CAC */
1281 iface->cac_started = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001282 channel = dfs_get_valid_channel(iface, &secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07001283 &oper_centr_freq_seg0_idx,
1284 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001285 channel_type);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001286
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001287 if (!channel) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001288 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1289 &oper_centr_freq_seg0_idx,
1290 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001291 &channel_type);
Hai Shalomfdcde762020-04-02 11:19:20 -07001292 if (!channel) {
1293 wpa_printf(MSG_ERROR, "No valid channel available");
1294 return err;
1295 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001296 }
1297
1298 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
1299 channel->chan);
1300 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
1301 "freq=%d chan=%d sec_chan=%d", channel->freq,
1302 channel->chan, secondary_channel);
1303
1304 iface->freq = channel->freq;
1305 iface->conf->channel = channel->chan;
1306 iface->conf->secondary_channel = secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001307 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1308 oper_centr_freq_seg0_idx);
1309 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1310 oper_centr_freq_seg1_idx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001311 err = 0;
1312
1313 hostapd_setup_interface_complete(iface, err);
1314 return err;
1315}
1316
1317
Sunil Ravia04bd252022-05-02 22:54:18 -07001318static int
1319hostapd_dfs_background_start_channel_switch(struct hostapd_iface *iface,
1320 int freq)
1321{
1322 if (!dfs_use_radar_background(iface))
1323 return -1; /* Background radar chain not supported. */
1324
1325 wpa_printf(MSG_DEBUG,
1326 "%s called (background CAC active: %s, CSA active: %s)",
1327 __func__, iface->radar_background.cac_started ? "yes" : "no",
1328 hostapd_csa_in_progress(iface) ? "yes" : "no");
1329
1330 /* Check if CSA in progress */
1331 if (hostapd_csa_in_progress(iface))
1332 return 0;
1333
1334 if (hostapd_dfs_is_background_event(iface, freq)) {
1335 /*
1336 * Radar pattern is reported on the background chain.
1337 * Just select a new random channel according to the
1338 * regulations for monitoring.
1339 */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001340 hostapd_dfs_update_background_chain(iface);
Sunil Ravia04bd252022-05-02 22:54:18 -07001341 return 0;
1342 }
1343
1344 /*
1345 * If background radar detection is supported and the radar channel
1346 * monitored by the background chain is available switch to it without
1347 * waiting for the CAC.
1348 */
1349 if (iface->radar_background.channel == -1)
1350 return -1; /* Background radar chain not available. */
1351
1352 if (iface->radar_background.cac_started) {
1353 /*
1354 * Background channel not available yet. Perform CAC on the
1355 * main chain.
1356 */
1357 iface->radar_background.temp_ch = 1;
1358 return -1;
1359 }
1360
1361 return hostapd_dfs_start_channel_switch_background(iface);
1362}
1363
1364
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001365static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
1366{
1367 struct hostapd_channel_data *channel;
1368 int secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001369 u8 oper_centr_freq_seg0_idx;
1370 u8 oper_centr_freq_seg1_idx;
Sunil Ravia04bd252022-05-02 22:54:18 -07001371 enum dfs_channel_type channel_type = DFS_AVAILABLE;
Hai Shalom899fcc72020-10-19 14:38:18 -07001372 u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001373
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001374 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
1375 __func__, iface->cac_started ? "yes" : "no",
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001376 hostapd_csa_in_progress(iface) ? "yes" : "no");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001377
1378 /* Check if CSA in progress */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001379 if (hostapd_csa_in_progress(iface))
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001380 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001381
1382 /* Check if active CAC */
1383 if (iface->cac_started)
1384 return hostapd_dfs_start_channel_switch_cac(iface);
1385
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001386 /*
1387 * Allow selection of DFS channel in ETSI to comply with
1388 * uniform spreading.
1389 */
1390 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
Sunil Ravia04bd252022-05-02 22:54:18 -07001391 channel_type = DFS_ANY_CHANNEL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001392
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001393 /* Perform channel switch/CSA */
1394 channel = dfs_get_valid_channel(iface, &secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07001395 &oper_centr_freq_seg0_idx,
1396 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001397 channel_type);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001398
1399 if (!channel) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07001400 /*
1401 * If there is no channel to switch immediately to, check if
1402 * there is another channel where we can switch even if it
1403 * requires to perform a CAC first.
1404 */
Sunil Ravia04bd252022-05-02 22:54:18 -07001405 channel_type = DFS_ANY_CHANNEL;
Hai Shalomfdcde762020-04-02 11:19:20 -07001406 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1407 &oper_centr_freq_seg0_idx,
1408 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001409 &channel_type);
Hai Shalom60840252021-02-19 19:02:11 -08001410 if (!channel) {
1411 /*
1412 * Toggle interface state to enter DFS state
1413 * until NOP is finished.
1414 */
1415 hostapd_disable_iface(iface);
1416 hostapd_enable_iface(iface);
1417 return 0;
1418 }
1419
Sunil Ravia04bd252022-05-02 22:54:18 -07001420 if (channel_type == DFS_ANY_CHANNEL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001421 iface->freq = channel->freq;
1422 iface->conf->channel = channel->chan;
1423 iface->conf->secondary_channel = secondary_channel;
1424 hostapd_set_oper_centr_freq_seg0_idx(
1425 iface->conf, oper_centr_freq_seg0_idx);
1426 hostapd_set_oper_centr_freq_seg1_idx(
1427 iface->conf, oper_centr_freq_seg1_idx);
1428
1429 hostapd_disable_iface(iface);
1430 hostapd_enable_iface(iface);
1431 return 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001432 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001433 }
1434
Sunil Ravia04bd252022-05-02 22:54:18 -07001435 return hostapd_dfs_request_channel_switch(iface, channel->chan,
1436 channel->freq,
1437 secondary_channel,
1438 current_vht_oper_chwidth,
1439 oper_centr_freq_seg0_idx,
1440 oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001441}
1442
1443
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001444int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001445 int ht_enabled, int chan_offset, int chan_width,
1446 int cf1, int cf2)
1447{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001448 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
1449 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1450 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1451
Sunil Ravi99c035e2024-07-12 01:42:03 +00001452 iface->radar_detected = true;
1453
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001454 /* Proceed only if DFS is not offloaded to the driver */
1455 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1456 return 0;
1457
1458 if (!iface->conf->ieee80211h)
1459 return 0;
1460
Dmitry Shmidt051af732013-10-22 13:52:46 -07001461 /* mark radar frequency as invalid */
Sunil Ravia04bd252022-05-02 22:54:18 -07001462 if (!set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1463 cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE))
Hai Shalomfdcde762020-04-02 11:19:20 -07001464 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001465
Sunil Ravia04bd252022-05-02 22:54:18 -07001466 if (!hostapd_dfs_is_background_event(iface, freq)) {
1467 /* Skip if reported radar event not overlapped our channels */
1468 if (!dfs_are_channels_overlapped(iface, freq, chan_width,
1469 cf1, cf2))
1470 return 0;
1471 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001472
Sunil Ravia04bd252022-05-02 22:54:18 -07001473 if (hostapd_dfs_background_start_channel_switch(iface, freq)) {
1474 /* Radar detected while operating, switch the channel. */
1475 return hostapd_dfs_start_channel_switch(iface);
1476 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001477
Sunil Ravia04bd252022-05-02 22:54:18 -07001478 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001479}
1480
1481
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001482int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001483 int ht_enabled, int chan_offset, int chan_width,
1484 int cf1, int cf2)
1485{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001486 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
1487 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1488 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001489
1490 /* Proceed only if DFS is not offloaded to the driver */
1491 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1492 return 0;
1493
Dmitry Shmidt051af732013-10-22 13:52:46 -07001494 /* TODO add correct implementation here */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001495 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1496 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001497
Sunil Ravia04bd252022-05-02 22:54:18 -07001498 if (iface->state == HAPD_IFACE_DFS && !iface->cac_started) {
1499 /* Handle cases where all channels were initially unavailable */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001500 hostapd_handle_dfs(iface);
Sunil Ravia04bd252022-05-02 22:54:18 -07001501 } else if (dfs_use_radar_background(iface) &&
1502 iface->radar_background.channel == -1) {
1503 /* Reset radar background chain if disabled */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001504 hostapd_dfs_update_background_chain(iface);
Sunil Ravia04bd252022-05-02 22:54:18 -07001505 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001506
Dmitry Shmidt051af732013-10-22 13:52:46 -07001507 return 0;
1508}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001509
1510
1511int hostapd_is_dfs_required(struct hostapd_iface *iface)
1512{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001513 int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001514
Hai Shaloma20dcd72022-02-04 13:43:00 -08001515 if ((!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1516 !iface->conf->ieee80211h) ||
1517 !iface->current_mode ||
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001518 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
1519 return 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001520
1521 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001522 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001523 if (start_chan_idx == -1)
1524 return -1;
1525
1526 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001527 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001528
1529 /* Check if any of configured channels require DFS */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001530 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
1531 if (res)
1532 return res;
1533 if (start_chan_idx1 >= 0 && n_chans1 > 0)
1534 res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1);
1535 return res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001536}
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001537
1538
1539int hostapd_dfs_start_cac(struct hostapd_iface *iface, int freq,
1540 int ht_enabled, int chan_offset, int chan_width,
1541 int cf1, int cf2)
1542{
Sunil Ravia04bd252022-05-02 22:54:18 -07001543 if (hostapd_dfs_is_background_event(iface, freq)) {
1544 iface->radar_background.cac_started = 1;
1545 } else {
1546 /* This is called when the driver indicates that an offloaded
Sunil Ravi99c035e2024-07-12 01:42:03 +00001547 * DFS has started CAC. radar_detected might be set for previous
1548 * DFS channel. Clear it for this new CAC process. */
Sunil Ravia04bd252022-05-02 22:54:18 -07001549 hostapd_set_state(iface, HAPD_IFACE_DFS);
1550 iface->cac_started = 1;
Sunil Ravi99c035e2024-07-12 01:42:03 +00001551
1552 /* Clear radar_detected in case it is for the previous
1553 * frequency. Also remove disabled link's information in RNR
1554 * element from other links. */
1555 iface->radar_detected = false;
1556 if (iface->interfaces && iface->interfaces->count > 1)
1557 ieee802_11_set_beacons(iface);
Sunil Ravia04bd252022-05-02 22:54:18 -07001558 }
Hai Shalomc3565922019-10-28 11:58:20 -07001559 /* TODO: How to check CAC time for ETSI weather channels? */
1560 iface->dfs_cac_ms = 60000;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001561 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
1562 "freq=%d chan=%d chan_offset=%d width=%d seg0=%d "
Sunil Ravia04bd252022-05-02 22:54:18 -07001563 "seg1=%d cac_time=%ds%s",
Hai Shalomc3565922019-10-28 11:58:20 -07001564 freq, (freq - 5000) / 5, chan_offset, chan_width, cf1, cf2,
Sunil Ravia04bd252022-05-02 22:54:18 -07001565 iface->dfs_cac_ms / 1000,
1566 hostapd_dfs_is_background_event(iface, freq) ?
1567 " (background)" : "");
1568
Hai Shalomc3565922019-10-28 11:58:20 -07001569 os_get_reltime(&iface->dfs_cac_start);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001570 return 0;
1571}
1572
1573
1574/*
1575 * Main DFS handler for offloaded case.
1576 * 2 - continue channel/AP setup for non-DFS channel
1577 * 1 - continue channel/AP setup for DFS channel
1578 * 0 - channel/AP setup will be continued after CAC
1579 * -1 - hit critical error
1580 */
1581int hostapd_handle_dfs_offload(struct hostapd_iface *iface)
1582{
Hai Shaloma20dcd72022-02-04 13:43:00 -08001583 int dfs_res;
1584
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001585 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1586 __func__, iface->cac_started);
1587
1588 /*
1589 * If DFS has already been started, then we are being called from a
1590 * callback to continue AP/channel setup. Reset the CAC start flag and
1591 * return.
1592 */
1593 if (iface->cac_started) {
1594 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1595 __func__, iface->cac_started);
1596 iface->cac_started = 0;
1597 return 1;
1598 }
1599
Hai Shaloma20dcd72022-02-04 13:43:00 -08001600 dfs_res = hostapd_is_dfs_required(iface);
1601 if (dfs_res > 0) {
1602 wpa_printf(MSG_DEBUG,
1603 "%s: freq %d MHz requires DFS for %d chans",
1604 __func__, iface->freq, dfs_res);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001605 return 0;
1606 }
1607
1608 wpa_printf(MSG_DEBUG,
1609 "%s: freq %d MHz does not require DFS. Continue channel/AP setup",
1610 __func__, iface->freq);
1611 return 2;
1612}
Hai Shalomfdcde762020-04-02 11:19:20 -07001613
1614
1615int hostapd_is_dfs_overlap(struct hostapd_iface *iface, enum chan_width width,
1616 int center_freq)
1617{
1618 struct hostapd_channel_data *chan;
1619 struct hostapd_hw_modes *mode = iface->current_mode;
1620 int half_width;
1621 int res = 0;
1622 int i;
1623
1624 if (!iface->conf->ieee80211h || !mode ||
1625 mode->mode != HOSTAPD_MODE_IEEE80211A)
1626 return 0;
1627
1628 switch (width) {
1629 case CHAN_WIDTH_20_NOHT:
1630 case CHAN_WIDTH_20:
1631 half_width = 10;
1632 break;
1633 case CHAN_WIDTH_40:
1634 half_width = 20;
1635 break;
1636 case CHAN_WIDTH_80:
1637 case CHAN_WIDTH_80P80:
1638 half_width = 40;
1639 break;
1640 case CHAN_WIDTH_160:
1641 half_width = 80;
1642 break;
1643 default:
1644 wpa_printf(MSG_WARNING, "DFS chanwidth %d not supported",
1645 width);
1646 return 0;
1647 }
1648
1649 for (i = 0; i < mode->num_channels; i++) {
1650 chan = &mode->channels[i];
1651
1652 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
1653 continue;
1654
Hai Shalom60840252021-02-19 19:02:11 -08001655 if ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
1656 HOSTAPD_CHAN_DFS_AVAILABLE)
1657 continue;
1658
Hai Shalomfdcde762020-04-02 11:19:20 -07001659 if (center_freq - chan->freq < half_width &&
1660 chan->freq - center_freq < half_width)
1661 res++;
1662 }
1663
Hai Shalom60840252021-02-19 19:02:11 -08001664 wpa_printf(MSG_DEBUG, "DFS CAC required: (%d, %d): in range: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -07001665 center_freq - half_width, center_freq + half_width,
1666 res ? "yes" : "no");
1667
1668 return res;
1669}