blob: 0cac194b267bcdee215638ac73331ec3f4e0b814 [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
Sunil Ravic0f5d412024-09-11 22:12:49 +0000256 if (!chan_in_current_hw_info(iface->current_hw_info, chan)) {
257 wpa_printf(MSG_DEBUG,
258 "DFS: channel %d (%d) is not under current hardware index",
259 chan->freq, chan->chan);
260 continue;
261 }
262
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800263 /* Skip HT40/VHT incompatible channels */
264 if (iface->conf->ieee80211n &&
265 iface->conf->secondary_channel &&
Hai Shalom74f70d42019-02-11 14:42:39 -0800266 (!dfs_is_chan_allowed(chan, n_chans) ||
Hai Shalomfdcde762020-04-02 11:19:20 -0700267 !(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P))) {
268 wpa_printf(MSG_DEBUG,
269 "DFS: channel %d (%d) is incompatible",
270 chan->freq, chan->chan);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700271 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -0700272 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700273
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800274 /* Skip incompatible chandefs */
Sunil Ravia04bd252022-05-02 22:54:18 -0700275 if (!dfs_chan_range_available(mode, i, n_chans, type)) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700276 wpa_printf(MSG_DEBUG,
277 "DFS: range not available for %d (%d)",
278 chan->freq, chan->chan);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800279 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -0700280 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700281
Hai Shalomfdcde762020-04-02 11:19:20 -0700282 if (!is_in_chanlist(iface, chan)) {
283 wpa_printf(MSG_DEBUG,
284 "DFS: channel %d (%d) not in chanlist",
285 chan->freq, chan->chan);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700286 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -0700287 }
Dmitry Shmidt98660862014-03-11 17:26:21 -0700288
Hai Shaloma20dcd72022-02-04 13:43:00 -0800289 if (chan->max_tx_power < iface->conf->min_tx_power)
290 continue;
291
Sunil Ravi89eba102022-09-13 21:04:37 -0700292 if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) &&
293 iface->conf->country[2] == 0x4f)
294 continue;
295
Dmitry Shmidt051af732013-10-22 13:52:46 -0700296 if (ret_chan && idx == channel_idx) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700297 wpa_printf(MSG_DEBUG, "Selected channel %d (%d)",
298 chan->freq, chan->chan);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700299 *ret_chan = chan;
300 return idx;
301 }
Hai Shalomfdcde762020-04-02 11:19:20 -0700302 wpa_printf(MSG_DEBUG, "Adding channel %d (%d)",
303 chan->freq, chan->chan);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700304 channel_idx++;
305 }
306 return channel_idx;
307}
308
309
Hai Shalom81f62d82019-07-22 12:10:00 -0700310static void dfs_adjust_center_freq(struct hostapd_iface *iface,
311 struct hostapd_channel_data *chan,
312 int secondary_channel,
Hai Shalomfdcde762020-04-02 11:19:20 -0700313 int sec_chan_idx_80p80,
Hai Shalom81f62d82019-07-22 12:10:00 -0700314 u8 *oper_centr_freq_seg0_idx,
315 u8 *oper_centr_freq_seg1_idx)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700316{
Hai Shalom81f62d82019-07-22 12:10:00 -0700317 if (!iface->conf->ieee80211ac && !iface->conf->ieee80211ax)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700318 return;
319
320 if (!chan)
321 return;
322
Hai Shalom81f62d82019-07-22 12:10:00 -0700323 *oper_centr_freq_seg1_idx = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800324
Hai Shalom81f62d82019-07-22 12:10:00 -0700325 switch (hostapd_get_oper_chwidth(iface->conf)) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000326 case CONF_OPER_CHWIDTH_USE_HT:
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800327 if (secondary_channel == 1)
Hai Shalom81f62d82019-07-22 12:10:00 -0700328 *oper_centr_freq_seg0_idx = chan->chan + 2;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800329 else if (secondary_channel == -1)
Hai Shalom81f62d82019-07-22 12:10:00 -0700330 *oper_centr_freq_seg0_idx = chan->chan - 2;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700331 else
Hai Shalom81f62d82019-07-22 12:10:00 -0700332 *oper_centr_freq_seg0_idx = chan->chan;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700333 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000334 case CONF_OPER_CHWIDTH_80MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700335 *oper_centr_freq_seg0_idx = chan->chan + 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700336 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000337 case CONF_OPER_CHWIDTH_160MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700338 *oper_centr_freq_seg0_idx = chan->chan + 14;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700339 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000340 case CONF_OPER_CHWIDTH_80P80MHZ:
Hai Shalomfdcde762020-04-02 11:19:20 -0700341 *oper_centr_freq_seg0_idx = chan->chan + 6;
342 *oper_centr_freq_seg1_idx = sec_chan_idx_80p80 + 6;
343 break;
344
Dmitry Shmidt051af732013-10-22 13:52:46 -0700345 default:
Hai Shalomfdcde762020-04-02 11:19:20 -0700346 wpa_printf(MSG_INFO,
347 "DFS: Unsupported channel width configuration");
Hai Shalom81f62d82019-07-22 12:10:00 -0700348 *oper_centr_freq_seg0_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700349 break;
350 }
351
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800352 wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d, %d",
Hai Shalom81f62d82019-07-22 12:10:00 -0700353 *oper_centr_freq_seg0_idx,
354 *oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700355}
356
357
358/* Return start channel idx we will use for mode->channels[idx] */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700359static int dfs_get_start_chan_idx(struct hostapd_iface *iface, int *seg1_start)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700360{
361 struct hostapd_hw_modes *mode;
362 struct hostapd_channel_data *chan;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800363 int channel_no = iface->conf->channel;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700364 int res = -1, i;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700365 int chan_seg1 = -1;
366
367 *seg1_start = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700368
369 /* HT40- */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800370 if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700371 channel_no -= 4;
372
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000373 /* VHT/HE/EHT */
374 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax ||
375 iface->conf->ieee80211be) {
Hai Shalom81f62d82019-07-22 12:10:00 -0700376 switch (hostapd_get_oper_chwidth(iface->conf)) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000377 case CONF_OPER_CHWIDTH_USE_HT:
Dmitry Shmidt051af732013-10-22 13:52:46 -0700378 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000379 case CONF_OPER_CHWIDTH_80MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700380 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
381 iface->conf) - 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700382 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000383 case CONF_OPER_CHWIDTH_160MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700384 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
385 iface->conf) - 14;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700386 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000387 case CONF_OPER_CHWIDTH_80P80MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700388 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
389 iface->conf) - 6;
390 chan_seg1 = hostapd_get_oper_centr_freq_seg1_idx(
391 iface->conf) - 6;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700392 break;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000393 case CONF_OPER_CHWIDTH_320MHZ:
394 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
395 iface->conf) - 30;
396 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700397 default:
398 wpa_printf(MSG_INFO,
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000399 "DFS only EHT20/40/80/160/80+80/320 is supported now");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700400 channel_no = -1;
401 break;
402 }
403 }
404
405 /* Get idx */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800406 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700407 for (i = 0; i < mode->num_channels; i++) {
408 chan = &mode->channels[i];
409 if (chan->chan == channel_no) {
410 res = i;
411 break;
412 }
413 }
414
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700415 if (res != -1 && chan_seg1 > -1) {
416 int found = 0;
417
418 /* Get idx for seg1 */
419 mode = iface->current_mode;
420 for (i = 0; i < mode->num_channels; i++) {
421 chan = &mode->channels[i];
422 if (chan->chan == chan_seg1) {
423 *seg1_start = i;
424 found = 1;
425 break;
426 }
427 }
428 if (!found)
429 res = -1;
430 }
431
Dmitry Shmidt98660862014-03-11 17:26:21 -0700432 if (res == -1) {
433 wpa_printf(MSG_DEBUG,
434 "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d",
435 mode->num_channels, channel_no, iface->conf->channel,
436 iface->conf->ieee80211n,
437 iface->conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700438 hostapd_get_oper_chwidth(iface->conf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700439
440 for (i = 0; i < mode->num_channels; i++) {
441 wpa_printf(MSG_DEBUG, "Available channel: %d",
442 mode->channels[i].chan);
443 }
444 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700445
446 return res;
447}
448
449
450/* At least one channel have radar flag */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800451static int dfs_check_chans_radar(struct hostapd_iface *iface,
452 int start_chan_idx, int n_chans)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700453{
454 struct hostapd_channel_data *channel;
455 struct hostapd_hw_modes *mode;
456 int i, res = 0;
457
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800458 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700459
460 for (i = 0; i < n_chans; i++) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000461 if (start_chan_idx + i >= mode->num_channels)
462 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700463 channel = &mode->channels[start_chan_idx + i];
464 if (channel->flag & HOSTAPD_CHAN_RADAR)
465 res++;
466 }
467
468 return res;
469}
470
471
472/* All channels available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800473static int dfs_check_chans_available(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700474 int start_chan_idx, int n_chans)
475{
476 struct hostapd_channel_data *channel;
477 struct hostapd_hw_modes *mode;
478 int i;
479
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800480 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700481
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800482 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700483 channel = &mode->channels[start_chan_idx + i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800484
485 if (channel->flag & HOSTAPD_CHAN_DISABLED)
486 break;
487
488 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
489 continue;
490
Dmitry Shmidt051af732013-10-22 13:52:46 -0700491 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
492 HOSTAPD_CHAN_DFS_AVAILABLE)
493 break;
494 }
495
496 return i == n_chans;
497}
498
499
500/* At least one channel unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800501static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700502 int start_chan_idx,
503 int n_chans)
504{
505 struct hostapd_channel_data *channel;
506 struct hostapd_hw_modes *mode;
507 int i, res = 0;
508
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800509 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700510
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800511 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700512 channel = &mode->channels[start_chan_idx + i];
513 if (channel->flag & HOSTAPD_CHAN_DISABLED)
514 res++;
515 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
516 HOSTAPD_CHAN_DFS_UNAVAILABLE)
517 res++;
518 }
519
520 return res;
521}
522
523
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800524static struct hostapd_channel_data *
525dfs_get_valid_channel(struct hostapd_iface *iface,
526 int *secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700527 u8 *oper_centr_freq_seg0_idx,
528 u8 *oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -0700529 enum dfs_channel_type type)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700530{
531 struct hostapd_hw_modes *mode;
532 struct hostapd_channel_data *chan = NULL;
Hai Shalomfdcde762020-04-02 11:19:20 -0700533 struct hostapd_channel_data *chan2 = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800534 int num_available_chandefs;
Hai Shalomfdcde762020-04-02 11:19:20 -0700535 int chan_idx, chan_idx2;
536 int sec_chan_idx_80p80 = -1;
537 int i;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700538 u32 _rand;
539
540 wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800541 *secondary_channel = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -0700542 *oper_centr_freq_seg0_idx = 0;
543 *oper_centr_freq_seg1_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700544
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800545 if (iface->current_mode == NULL)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700546 return NULL;
547
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800548 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700549 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
550 return NULL;
551
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800552 /* Get the count first */
Sunil Ravia04bd252022-05-02 22:54:18 -0700553 num_available_chandefs = dfs_find_channel(iface, NULL, 0, type);
Hai Shalomfdcde762020-04-02 11:19:20 -0700554 wpa_printf(MSG_DEBUG, "DFS: num_available_chandefs=%d",
555 num_available_chandefs);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800556 if (num_available_chandefs == 0)
557 return NULL;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700558
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800559 if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800560 return NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800561 chan_idx = _rand % num_available_chandefs;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000562 wpa_printf(MSG_DEBUG, "DFS: Picked random entry from the list: %d/%d",
563 chan_idx, num_available_chandefs);
Sunil Ravia04bd252022-05-02 22:54:18 -0700564 dfs_find_channel(iface, &chan, chan_idx, type);
Hai Shalomfdcde762020-04-02 11:19:20 -0700565 if (!chan) {
566 wpa_printf(MSG_DEBUG, "DFS: no random channel found");
567 return NULL;
568 }
569 wpa_printf(MSG_DEBUG, "DFS: got random channel %d (%d)",
570 chan->freq, chan->chan);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800571
572 /* dfs_find_channel() calculations assume HT40+ */
573 if (iface->conf->secondary_channel)
574 *secondary_channel = 1;
575 else
576 *secondary_channel = 0;
577
Hai Shalomfdcde762020-04-02 11:19:20 -0700578 /* Get secondary channel for HT80P80 */
Sunil8cd6f4d2022-06-28 18:40:46 +0000579 if (hostapd_get_oper_chwidth(iface->conf) ==
580 CONF_OPER_CHWIDTH_80P80MHZ) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700581 if (num_available_chandefs <= 1) {
582 wpa_printf(MSG_ERROR,
583 "only 1 valid chan, can't support 80+80");
584 return NULL;
585 }
586
587 /*
588 * Loop all channels except channel1 to find a valid channel2
589 * that is not adjacent to channel1.
590 */
591 for (i = 0; i < num_available_chandefs - 1; i++) {
592 /* start from chan_idx + 1, end when chan_idx - 1 */
593 chan_idx2 = (chan_idx + 1 + i) % num_available_chandefs;
Sunil Ravia04bd252022-05-02 22:54:18 -0700594 dfs_find_channel(iface, &chan2, chan_idx2, type);
Hai Shalomfdcde762020-04-02 11:19:20 -0700595 if (chan2 && abs(chan2->chan - chan->chan) > 12) {
596 /* two channels are not adjacent */
597 sec_chan_idx_80p80 = chan2->chan;
598 wpa_printf(MSG_DEBUG,
599 "DFS: got second chan: %d (%d)",
600 chan2->freq, chan2->chan);
601 break;
602 }
603 }
604
605 /* Check if we got a valid secondary channel which is not
606 * adjacent to the first channel.
607 */
608 if (sec_chan_idx_80p80 == -1) {
609 wpa_printf(MSG_INFO,
610 "DFS: failed to get chan2 for 80+80");
611 return NULL;
612 }
613 }
614
Hai Shalom81f62d82019-07-22 12:10:00 -0700615 dfs_adjust_center_freq(iface, chan,
616 *secondary_channel,
Hai Shalomfdcde762020-04-02 11:19:20 -0700617 sec_chan_idx_80p80,
Hai Shalom81f62d82019-07-22 12:10:00 -0700618 oper_centr_freq_seg0_idx,
619 oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700620
621 return chan;
622}
623
624
Sunil Ravia04bd252022-05-02 22:54:18 -0700625static int dfs_set_valid_channel(struct hostapd_iface *iface, int skip_radar)
626{
627 struct hostapd_channel_data *channel;
628 u8 cf1 = 0, cf2 = 0;
629 int sec = 0;
630
631 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
632 skip_radar ? DFS_AVAILABLE :
633 DFS_ANY_CHANNEL);
634 if (!channel) {
635 wpa_printf(MSG_ERROR, "could not get valid channel");
636 return -1;
637 }
638
639 iface->freq = channel->freq;
640 iface->conf->channel = channel->chan;
641 iface->conf->secondary_channel = sec;
642 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, cf1);
643 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, cf2);
644
645 return 0;
646}
647
648
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800649static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700650{
651 struct hostapd_hw_modes *mode;
652 struct hostapd_channel_data *chan = NULL;
653 int i;
654
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800655 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700656 if (mode == NULL)
657 return 0;
658
659 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800660 for (i = 0; i < iface->current_mode->num_channels; i++) {
661 chan = &iface->current_mode->channels[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -0700662 if (chan->freq == freq) {
663 if (chan->flag & HOSTAPD_CHAN_RADAR) {
664 chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
665 chan->flag |= state;
666 return 1; /* Channel found */
667 }
668 }
669 }
670 wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
671 return 0;
672}
673
674
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800675static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700676 int chan_offset, int chan_width, int cf1,
677 int cf2, u32 state)
678{
679 int n_chans = 1, i;
680 struct hostapd_hw_modes *mode;
681 int frequency = freq;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800682 int frequency2 = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700683 int ret = 0;
684
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800685 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700686 if (mode == NULL)
687 return 0;
688
689 if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
690 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
691 return 0;
692 }
693
694 /* Seems cf1 and chan_width is enough here */
695 switch (chan_width) {
696 case CHAN_WIDTH_20_NOHT:
697 case CHAN_WIDTH_20:
698 n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800699 if (frequency == 0)
700 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700701 break;
702 case CHAN_WIDTH_40:
703 n_chans = 2;
704 frequency = cf1 - 10;
705 break;
706 case CHAN_WIDTH_80:
707 n_chans = 4;
708 frequency = cf1 - 30;
709 break;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800710 case CHAN_WIDTH_80P80:
711 n_chans = 4;
712 frequency = cf1 - 30;
713 frequency2 = cf2 - 30;
714 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700715 case CHAN_WIDTH_160:
716 n_chans = 8;
717 frequency = cf1 - 70;
718 break;
719 default:
720 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
721 chan_width);
722 break;
723 }
724
725 wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
726 n_chans);
727 for (i = 0; i < n_chans; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800728 ret += set_dfs_state_freq(iface, frequency, state);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700729 frequency = frequency + 20;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800730
731 if (chan_width == CHAN_WIDTH_80P80) {
732 ret += set_dfs_state_freq(iface, frequency2, state);
733 frequency2 = frequency2 + 20;
734 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700735 }
736
737 return ret;
738}
739
740
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800741static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700742 int chan_width, int cf1, int cf2)
743{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700744 int start_chan_idx, start_chan_idx1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700745 struct hostapd_hw_modes *mode;
746 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700747 int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700748 u8 radar_chan;
749 int res = 0;
750
Dmitry Shmidt051af732013-10-22 13:52:46 -0700751 /* Our configuration */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800752 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700753 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
754 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700755
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700756 /* Check we are on DFS channel(s) */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800757 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700758 return 0;
759
Dmitry Shmidt051af732013-10-22 13:52:46 -0700760 /* Reported via radar event */
761 switch (chan_width) {
762 case CHAN_WIDTH_20_NOHT:
763 case CHAN_WIDTH_20:
764 radar_n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800765 if (frequency == 0)
766 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700767 break;
768 case CHAN_WIDTH_40:
769 radar_n_chans = 2;
770 frequency = cf1 - 10;
771 break;
772 case CHAN_WIDTH_80:
773 radar_n_chans = 4;
774 frequency = cf1 - 30;
775 break;
776 case CHAN_WIDTH_160:
777 radar_n_chans = 8;
778 frequency = cf1 - 70;
779 break;
780 default:
781 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
782 chan_width);
783 break;
784 }
785
786 ieee80211_freq_to_chan(frequency, &radar_chan);
787
788 for (i = 0; i < n_chans; i++) {
789 chan = &mode->channels[start_chan_idx + i];
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700790 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
791 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700792 for (j = 0; j < radar_n_chans; j++) {
793 wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
794 chan->chan, radar_chan + j * 4);
795 if (chan->chan == radar_chan + j * 4)
796 res++;
797 }
798 }
799
800 wpa_printf(MSG_DEBUG, "overlapped: %d", res);
801
802 return res;
803}
804
805
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700806static unsigned int dfs_get_cac_time(struct hostapd_iface *iface,
807 int start_chan_idx, int n_chans)
808{
809 struct hostapd_channel_data *channel;
810 struct hostapd_hw_modes *mode;
811 int i;
812 unsigned int cac_time_ms = 0;
813
814 mode = iface->current_mode;
815
816 for (i = 0; i < n_chans; i++) {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000817 if (start_chan_idx + i >= mode->num_channels)
818 break;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700819 channel = &mode->channels[start_chan_idx + i];
820 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
821 continue;
822 if (channel->dfs_cac_ms > cac_time_ms)
823 cac_time_ms = channel->dfs_cac_ms;
824 }
825
826 return cac_time_ms;
827}
828
829
Dmitry Shmidt051af732013-10-22 13:52:46 -0700830/*
831 * Main DFS handler
832 * 1 - continue channel/ap setup
833 * 0 - channel/ap setup will be continued after CAC
834 * -1 - hit critical error
835 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800836int hostapd_handle_dfs(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700837{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700838 int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800839 int skip_radar = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700840
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800841 if (is_6ghz_freq(iface->freq))
842 return 1;
843
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800844 if (!iface->current_mode) {
845 /*
846 * This can happen with drivers that do not provide mode
847 * information and as such, cannot really use hostapd for DFS.
848 */
849 wpa_printf(MSG_DEBUG,
850 "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
851 return 1;
852 }
853
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800854 iface->cac_started = 0;
855
Dmitry Shmidt051af732013-10-22 13:52:46 -0700856 do {
857 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700858 start_chan_idx = dfs_get_start_chan_idx(iface,
859 &start_chan_idx1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700860 if (start_chan_idx == -1)
861 return -1;
862
863 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700864 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700865
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700866 /* Setup CAC time */
867 iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx,
868 n_chans);
869
Dmitry Shmidt051af732013-10-22 13:52:46 -0700870 /* Check if any of configured channels require DFS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800871 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700872 wpa_printf(MSG_DEBUG,
873 "DFS %d channels required radar detection",
874 res);
875 if (!res)
876 return 1;
877
878 /* Check if all channels are DFS available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800879 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700880 wpa_printf(MSG_DEBUG,
881 "DFS all channels available, (SKIP CAC): %s",
882 res ? "yes" : "no");
883 if (res)
884 return 1;
885
886 /* Check if any of configured channels is unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800887 res = dfs_check_chans_unavailable(iface, start_chan_idx,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700888 n_chans);
889 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
890 res, res ? "yes": "no");
891 if (res) {
Sunil Ravia04bd252022-05-02 22:54:18 -0700892 if (dfs_set_valid_channel(iface, skip_radar) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800893 hostapd_set_state(iface, HAPD_IFACE_DFS);
894 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700895 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700896 }
897 } while (res);
898
899 /* Finally start CAC */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800900 hostapd_set_state(iface, HAPD_IFACE_DFS);
Sunil Ravia04bd252022-05-02 22:54:18 -0700901 wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz%s", iface->freq,
902 dfs_use_radar_background(iface) ? " (background)" : "");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800903 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700904 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800905 iface->freq,
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800906 iface->conf->channel, iface->conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700907 hostapd_get_oper_chwidth(iface->conf),
908 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
909 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700910 iface->dfs_cac_ms / 1000);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800911
Hai Shalom81f62d82019-07-22 12:10:00 -0700912 res = hostapd_start_dfs_cac(
913 iface, iface->conf->hw_mode, iface->freq, iface->conf->channel,
914 iface->conf->ieee80211n, iface->conf->ieee80211ac,
Sunil Ravia04bd252022-05-02 22:54:18 -0700915 iface->conf->ieee80211ax, iface->conf->ieee80211be,
Hai Shalom81f62d82019-07-22 12:10:00 -0700916 iface->conf->secondary_channel,
917 hostapd_get_oper_chwidth(iface->conf),
918 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
Sunil Ravia04bd252022-05-02 22:54:18 -0700919 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
920 dfs_use_radar_background(iface));
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800921
922 if (res) {
923 wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700924 return -1;
925 }
926
Sunil Ravia04bd252022-05-02 22:54:18 -0700927 if (dfs_use_radar_background(iface)) {
928 /* Cache background radar parameters. */
929 iface->radar_background.channel = iface->conf->channel;
930 iface->radar_background.secondary_channel =
931 iface->conf->secondary_channel;
932 iface->radar_background.freq = iface->freq;
933 iface->radar_background.centr_freq_seg0_idx =
934 hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
935 iface->radar_background.centr_freq_seg1_idx =
936 hostapd_get_oper_centr_freq_seg1_idx(iface->conf);
937
938 /*
939 * Let's select a random channel according to the
940 * regulations and perform CAC on dedicated radar chain.
941 */
942 res = dfs_set_valid_channel(iface, 1);
943 if (res < 0)
944 return res;
945
946 iface->radar_background.temp_ch = 1;
947 return 1;
948 }
949
Dmitry Shmidt051af732013-10-22 13:52:46 -0700950 return 0;
951}
952
953
Hai Shalomfdcde762020-04-02 11:19:20 -0700954int hostapd_is_dfs_chan_available(struct hostapd_iface *iface)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700955{
956 int n_chans, n_chans1, start_chan_idx, start_chan_idx1;
957
958 /* Get the start (first) channel for current configuration */
959 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
960 if (start_chan_idx < 0)
961 return 0;
962
963 /* Get the number of used channels, depending on width */
964 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
965
966 /* Check if all channels are DFS available */
967 return dfs_check_chans_available(iface, start_chan_idx, n_chans);
968}
969
970
Sunil Ravia04bd252022-05-02 22:54:18 -0700971static int hostapd_dfs_request_channel_switch(struct hostapd_iface *iface,
972 int channel, int freq,
973 int secondary_channel,
974 u8 current_vht_oper_chwidth,
975 u8 oper_centr_freq_seg0_idx,
976 u8 oper_centr_freq_seg1_idx)
977{
978 struct hostapd_hw_modes *cmode = iface->current_mode;
979 int ieee80211_mode = IEEE80211_MODE_AP, err;
980 struct csa_settings csa_settings;
981 u8 new_vht_oper_chwidth;
982 unsigned int i;
Sunil Ravi99c035e2024-07-12 01:42:03 +0000983 unsigned int num_err = 0;
Sunil Ravia04bd252022-05-02 22:54:18 -0700984
985 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d", channel);
986 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
987 "freq=%d chan=%d sec_chan=%d", freq, channel,
988 secondary_channel);
989
990 new_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
991 hostapd_set_oper_chwidth(iface->conf, current_vht_oper_chwidth);
992
993 /* Setup CSA request */
994 os_memset(&csa_settings, 0, sizeof(csa_settings));
995 csa_settings.cs_count = 5;
996 csa_settings.block_tx = 1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000997 csa_settings.link_id = -1;
998#ifdef CONFIG_IEEE80211BE
999 if (iface->bss[0]->conf->mld_ap)
1000 csa_settings.link_id = iface->bss[0]->mld_link_id;
1001#endif /* CONFIG_IEEE80211BE */
Sunil Ravia04bd252022-05-02 22:54:18 -07001002#ifdef CONFIG_MESH
1003 if (iface->mconf)
1004 ieee80211_mode = IEEE80211_MODE_MESH;
1005#endif /* CONFIG_MESH */
1006 err = hostapd_set_freq_params(&csa_settings.freq_params,
1007 iface->conf->hw_mode,
1008 freq, channel,
1009 iface->conf->enable_edmg,
1010 iface->conf->edmg_channel,
1011 iface->conf->ieee80211n,
1012 iface->conf->ieee80211ac,
1013 iface->conf->ieee80211ax,
1014 iface->conf->ieee80211be,
1015 secondary_channel,
1016 new_vht_oper_chwidth,
1017 oper_centr_freq_seg0_idx,
1018 oper_centr_freq_seg1_idx,
1019 cmode->vht_capab,
1020 &cmode->he_capab[ieee80211_mode],
Sunil Ravi99c035e2024-07-12 01:42:03 +00001021 &cmode->eht_capab[ieee80211_mode],
1022 hostapd_get_punct_bitmap(iface->bss[0]));
Sunil Ravia04bd252022-05-02 22:54:18 -07001023
1024 if (err) {
1025 wpa_printf(MSG_ERROR,
1026 "DFS failed to calculate CSA freq params");
1027 hostapd_disable_iface(iface);
1028 return err;
1029 }
1030
1031 for (i = 0; i < iface->num_bss; i++) {
1032 err = hostapd_switch_channel(iface->bss[i], &csa_settings);
1033 if (err)
Sunil Ravi99c035e2024-07-12 01:42:03 +00001034 num_err++;
Sunil Ravia04bd252022-05-02 22:54:18 -07001035 }
1036
Sunil Ravi99c035e2024-07-12 01:42:03 +00001037 if (num_err == iface->num_bss) {
Sunil Ravia04bd252022-05-02 22:54:18 -07001038 wpa_printf(MSG_WARNING,
1039 "DFS failed to schedule CSA (%d) - trying fallback",
1040 err);
1041 iface->freq = freq;
1042 iface->conf->channel = channel;
1043 iface->conf->secondary_channel = secondary_channel;
1044 hostapd_set_oper_chwidth(iface->conf, new_vht_oper_chwidth);
1045 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1046 oper_centr_freq_seg0_idx);
1047 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1048 oper_centr_freq_seg1_idx);
1049
1050 hostapd_disable_iface(iface);
1051 hostapd_enable_iface(iface);
1052
1053 return 0;
1054 }
1055
1056 /* Channel configuration will be updated once CSA completes and
1057 * ch_switch_notify event is received */
1058 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
1059
1060 return 0;
1061}
1062
1063
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001064static void hostapd_dfs_update_background_chain(struct hostapd_iface *iface)
Sunil Ravia04bd252022-05-02 22:54:18 -07001065{
1066 int sec = 0;
1067 enum dfs_channel_type channel_type = DFS_NO_CAC_YET;
1068 struct hostapd_channel_data *channel;
1069 u8 oper_centr_freq_seg0_idx = 0;
1070 u8 oper_centr_freq_seg1_idx = 0;
1071
1072 /*
1073 * Allow selection of DFS channel in ETSI to comply with
1074 * uniform spreading.
1075 */
1076 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
1077 channel_type = DFS_ANY_CHANNEL;
1078
1079 channel = dfs_get_valid_channel(iface, &sec, &oper_centr_freq_seg0_idx,
1080 &oper_centr_freq_seg1_idx,
1081 channel_type);
1082 if (!channel ||
1083 channel->chan == iface->conf->channel ||
1084 channel->chan == iface->radar_background.channel)
1085 channel = dfs_downgrade_bandwidth(iface, &sec,
1086 &oper_centr_freq_seg0_idx,
1087 &oper_centr_freq_seg1_idx,
1088 &channel_type);
1089 if (!channel ||
1090 hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
1091 channel->freq, channel->chan,
1092 iface->conf->ieee80211n,
1093 iface->conf->ieee80211ac,
1094 iface->conf->ieee80211ax,
1095 iface->conf->ieee80211be,
1096 sec, hostapd_get_oper_chwidth(iface->conf),
1097 oper_centr_freq_seg0_idx,
1098 oper_centr_freq_seg1_idx, true)) {
1099 wpa_printf(MSG_ERROR, "DFS failed to start CAC offchannel");
1100 iface->radar_background.channel = -1;
1101 return;
1102 }
1103
1104 iface->radar_background.channel = channel->chan;
1105 iface->radar_background.freq = channel->freq;
1106 iface->radar_background.secondary_channel = sec;
1107 iface->radar_background.centr_freq_seg0_idx = oper_centr_freq_seg0_idx;
1108 iface->radar_background.centr_freq_seg1_idx = oper_centr_freq_seg1_idx;
1109
1110 wpa_printf(MSG_DEBUG,
1111 "%s: setting background chain to chan %d (%d MHz)",
1112 __func__, channel->chan, channel->freq);
1113}
1114
1115
1116static bool
1117hostapd_dfs_is_background_event(struct hostapd_iface *iface, int freq)
1118{
1119 return dfs_use_radar_background(iface) &&
1120 iface->radar_background.channel != -1 &&
1121 iface->radar_background.freq == freq;
1122}
1123
1124
1125static int
1126hostapd_dfs_start_channel_switch_background(struct hostapd_iface *iface)
1127{
1128 u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
1129
1130 iface->conf->channel = iface->radar_background.channel;
1131 iface->freq = iface->radar_background.freq;
1132 iface->conf->secondary_channel =
1133 iface->radar_background.secondary_channel;
1134 hostapd_set_oper_centr_freq_seg0_idx(
1135 iface->conf, iface->radar_background.centr_freq_seg0_idx);
1136 hostapd_set_oper_centr_freq_seg1_idx(
1137 iface->conf, iface->radar_background.centr_freq_seg1_idx);
1138
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001139 hostapd_dfs_update_background_chain(iface);
Sunil Ravia04bd252022-05-02 22:54:18 -07001140
1141 return hostapd_dfs_request_channel_switch(
1142 iface, iface->conf->channel, iface->freq,
1143 iface->conf->secondary_channel, current_vht_oper_chwidth,
1144 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
1145 hostapd_get_oper_centr_freq_seg1_idx(iface->conf));
1146}
1147
1148
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001149int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001150 int ht_enabled, int chan_offset, int chan_width,
1151 int cf1, int cf2)
1152{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001153 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
Sunil Ravi99c035e2024-07-12 01:42:03 +00001154 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d radar_detected=%d",
1155 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2,
1156 iface->radar_detected);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001157
Dmitry Shmidt051af732013-10-22 13:52:46 -07001158 if (success) {
1159 /* Complete iface/ap configuration */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001160 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00001161 /* Complete AP configuration for the first bring up. If
1162 * a radar was detected in this channel, interface setup
1163 * will be handled in
1164 * 1. hostapd_event_ch_switch() if switching to a
1165 * non-DFS channel
1166 * 2. on next CAC complete event if switching to another
1167 * DFS channel.
1168 */
1169 if (iface->state != HAPD_IFACE_ENABLED &&
1170 !iface->radar_detected)
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001171 hostapd_setup_interface_complete(iface, 0);
1172 else
1173 iface->cac_started = 0;
1174 } else {
1175 set_dfs_state(iface, freq, ht_enabled, chan_offset,
1176 chan_width, cf1, cf2,
1177 HOSTAPD_CHAN_DFS_AVAILABLE);
Sunil Ravia04bd252022-05-02 22:54:18 -07001178
1179 /*
1180 * Radar event from background chain for the selected
1181 * channel. Perform CSA, move the main chain to the
1182 * selected channel and configure the background chain
1183 * to a new DFS channel.
1184 */
1185 if (hostapd_dfs_is_background_event(iface, freq)) {
1186 iface->radar_background.cac_started = 0;
1187 if (!iface->radar_background.temp_ch)
1188 return 0;
1189
1190 iface->radar_background.temp_ch = 0;
1191 return hostapd_dfs_start_channel_switch_background(iface);
1192 }
1193
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001194 /*
1195 * Just mark the channel available when CAC completion
1196 * event is received in enabled state. CAC result could
1197 * have been propagated from another radio having the
1198 * same regulatory configuration. When CAC completion is
1199 * received during non-HAPD_IFACE_ENABLED state, make
1200 * sure the configured channel is available because this
1201 * CAC completion event could have been propagated from
1202 * another radio.
1203 */
1204 if (iface->state != HAPD_IFACE_ENABLED &&
Hai Shalomfdcde762020-04-02 11:19:20 -07001205 hostapd_is_dfs_chan_available(iface)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001206 hostapd_setup_interface_complete(iface, 0);
1207 iface->cac_started = 0;
1208 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001209 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001210 } else if (hostapd_dfs_is_background_event(iface, freq)) {
1211 iface->radar_background.cac_started = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001212 hostapd_dfs_update_background_chain(iface);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001213 }
1214
Sunil Ravi99c035e2024-07-12 01:42:03 +00001215 iface->radar_detected = false;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001216 return 0;
1217}
1218
1219
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001220int hostapd_dfs_pre_cac_expired(struct hostapd_iface *iface, int freq,
1221 int ht_enabled, int chan_offset, int chan_width,
1222 int cf1, int cf2)
1223{
1224 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_PRE_CAC_EXPIRED
1225 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1226 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1227
1228 /* Proceed only if DFS is not offloaded to the driver */
1229 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1230 return 0;
1231
1232 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1233 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
1234
1235 return 0;
1236}
1237
1238
Hai Shalomfdcde762020-04-02 11:19:20 -07001239static struct hostapd_channel_data *
1240dfs_downgrade_bandwidth(struct hostapd_iface *iface, int *secondary_channel,
1241 u8 *oper_centr_freq_seg0_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001242 u8 *oper_centr_freq_seg1_idx,
1243 enum dfs_channel_type *channel_type)
Hai Shalomfdcde762020-04-02 11:19:20 -07001244{
1245 struct hostapd_channel_data *channel;
1246
1247 for (;;) {
1248 channel = dfs_get_valid_channel(iface, secondary_channel,
1249 oper_centr_freq_seg0_idx,
1250 oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001251 *channel_type);
Hai Shalomfdcde762020-04-02 11:19:20 -07001252 if (channel) {
1253 wpa_printf(MSG_DEBUG, "DFS: Selected channel: %d",
1254 channel->chan);
1255 return channel;
1256 }
1257
Sunil Ravia04bd252022-05-02 22:54:18 -07001258 if (*channel_type != DFS_ANY_CHANNEL) {
1259 *channel_type = DFS_ANY_CHANNEL;
Hai Shalomfdcde762020-04-02 11:19:20 -07001260 } else {
Hai Shalom899fcc72020-10-19 14:38:18 -07001261 int oper_chwidth;
1262
1263 oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
Sunil8cd6f4d2022-06-28 18:40:46 +00001264 if (oper_chwidth == CONF_OPER_CHWIDTH_USE_HT)
Hai Shalomfdcde762020-04-02 11:19:20 -07001265 break;
Sunil Ravia04bd252022-05-02 22:54:18 -07001266 *channel_type = DFS_AVAILABLE;
Hai Shalom899fcc72020-10-19 14:38:18 -07001267 hostapd_set_oper_chwidth(iface->conf, oper_chwidth - 1);
Hai Shalomfdcde762020-04-02 11:19:20 -07001268 }
1269 }
1270
1271 wpa_printf(MSG_INFO,
1272 "%s: no DFS channels left, waiting for NOP to finish",
1273 __func__);
1274 return NULL;
1275}
1276
1277
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001278static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001279{
1280 struct hostapd_channel_data *channel;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001281 int secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001282 u8 oper_centr_freq_seg0_idx = 0;
1283 u8 oper_centr_freq_seg1_idx = 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07001284 enum dfs_channel_type channel_type = DFS_ANY_CHANNEL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001285 int err = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001286
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001287 /* Radar detected during active CAC */
1288 iface->cac_started = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001289 channel = dfs_get_valid_channel(iface, &secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07001290 &oper_centr_freq_seg0_idx,
1291 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001292 channel_type);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001293
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001294 if (!channel) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001295 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1296 &oper_centr_freq_seg0_idx,
1297 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001298 &channel_type);
Hai Shalomfdcde762020-04-02 11:19:20 -07001299 if (!channel) {
1300 wpa_printf(MSG_ERROR, "No valid channel available");
1301 return err;
1302 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001303 }
1304
1305 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
1306 channel->chan);
1307 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
1308 "freq=%d chan=%d sec_chan=%d", channel->freq,
1309 channel->chan, secondary_channel);
1310
1311 iface->freq = channel->freq;
1312 iface->conf->channel = channel->chan;
1313 iface->conf->secondary_channel = secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001314 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1315 oper_centr_freq_seg0_idx);
1316 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1317 oper_centr_freq_seg1_idx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001318 err = 0;
1319
1320 hostapd_setup_interface_complete(iface, err);
1321 return err;
1322}
1323
1324
Sunil Ravia04bd252022-05-02 22:54:18 -07001325static int
1326hostapd_dfs_background_start_channel_switch(struct hostapd_iface *iface,
1327 int freq)
1328{
1329 if (!dfs_use_radar_background(iface))
1330 return -1; /* Background radar chain not supported. */
1331
1332 wpa_printf(MSG_DEBUG,
1333 "%s called (background CAC active: %s, CSA active: %s)",
1334 __func__, iface->radar_background.cac_started ? "yes" : "no",
1335 hostapd_csa_in_progress(iface) ? "yes" : "no");
1336
1337 /* Check if CSA in progress */
1338 if (hostapd_csa_in_progress(iface))
1339 return 0;
1340
1341 if (hostapd_dfs_is_background_event(iface, freq)) {
1342 /*
1343 * Radar pattern is reported on the background chain.
1344 * Just select a new random channel according to the
1345 * regulations for monitoring.
1346 */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001347 hostapd_dfs_update_background_chain(iface);
Sunil Ravia04bd252022-05-02 22:54:18 -07001348 return 0;
1349 }
1350
1351 /*
1352 * If background radar detection is supported and the radar channel
1353 * monitored by the background chain is available switch to it without
1354 * waiting for the CAC.
1355 */
1356 if (iface->radar_background.channel == -1)
1357 return -1; /* Background radar chain not available. */
1358
1359 if (iface->radar_background.cac_started) {
1360 /*
1361 * Background channel not available yet. Perform CAC on the
1362 * main chain.
1363 */
1364 iface->radar_background.temp_ch = 1;
1365 return -1;
1366 }
1367
1368 return hostapd_dfs_start_channel_switch_background(iface);
1369}
1370
1371
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001372static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
1373{
1374 struct hostapd_channel_data *channel;
1375 int secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001376 u8 oper_centr_freq_seg0_idx;
1377 u8 oper_centr_freq_seg1_idx;
Sunil Ravia04bd252022-05-02 22:54:18 -07001378 enum dfs_channel_type channel_type = DFS_AVAILABLE;
Hai Shalom899fcc72020-10-19 14:38:18 -07001379 u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001380
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001381 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
1382 __func__, iface->cac_started ? "yes" : "no",
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001383 hostapd_csa_in_progress(iface) ? "yes" : "no");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001384
1385 /* Check if CSA in progress */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001386 if (hostapd_csa_in_progress(iface))
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001387 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001388
1389 /* Check if active CAC */
1390 if (iface->cac_started)
1391 return hostapd_dfs_start_channel_switch_cac(iface);
1392
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001393 /*
1394 * Allow selection of DFS channel in ETSI to comply with
1395 * uniform spreading.
1396 */
1397 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
Sunil Ravia04bd252022-05-02 22:54:18 -07001398 channel_type = DFS_ANY_CHANNEL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001399
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001400 /* Perform channel switch/CSA */
1401 channel = dfs_get_valid_channel(iface, &secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07001402 &oper_centr_freq_seg0_idx,
1403 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001404 channel_type);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001405
1406 if (!channel) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07001407 /*
1408 * If there is no channel to switch immediately to, check if
1409 * there is another channel where we can switch even if it
1410 * requires to perform a CAC first.
1411 */
Sunil Ravia04bd252022-05-02 22:54:18 -07001412 channel_type = DFS_ANY_CHANNEL;
Hai Shalomfdcde762020-04-02 11:19:20 -07001413 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1414 &oper_centr_freq_seg0_idx,
1415 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001416 &channel_type);
Hai Shalom60840252021-02-19 19:02:11 -08001417 if (!channel) {
1418 /*
1419 * Toggle interface state to enter DFS state
1420 * until NOP is finished.
1421 */
1422 hostapd_disable_iface(iface);
1423 hostapd_enable_iface(iface);
1424 return 0;
1425 }
1426
Sunil Ravia04bd252022-05-02 22:54:18 -07001427 if (channel_type == DFS_ANY_CHANNEL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001428 iface->freq = channel->freq;
1429 iface->conf->channel = channel->chan;
1430 iface->conf->secondary_channel = secondary_channel;
1431 hostapd_set_oper_centr_freq_seg0_idx(
1432 iface->conf, oper_centr_freq_seg0_idx);
1433 hostapd_set_oper_centr_freq_seg1_idx(
1434 iface->conf, oper_centr_freq_seg1_idx);
1435
1436 hostapd_disable_iface(iface);
1437 hostapd_enable_iface(iface);
1438 return 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001439 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001440 }
1441
Sunil Ravia04bd252022-05-02 22:54:18 -07001442 return hostapd_dfs_request_channel_switch(iface, channel->chan,
1443 channel->freq,
1444 secondary_channel,
1445 current_vht_oper_chwidth,
1446 oper_centr_freq_seg0_idx,
1447 oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001448}
1449
1450
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001451int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001452 int ht_enabled, int chan_offset, int chan_width,
1453 int cf1, int cf2)
1454{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001455 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
1456 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1457 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1458
Sunil Ravi99c035e2024-07-12 01:42:03 +00001459 iface->radar_detected = true;
1460
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001461 /* Proceed only if DFS is not offloaded to the driver */
1462 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1463 return 0;
1464
1465 if (!iface->conf->ieee80211h)
1466 return 0;
1467
Dmitry Shmidt051af732013-10-22 13:52:46 -07001468 /* mark radar frequency as invalid */
Sunil Ravia04bd252022-05-02 22:54:18 -07001469 if (!set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1470 cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE))
Hai Shalomfdcde762020-04-02 11:19:20 -07001471 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001472
Sunil Ravia04bd252022-05-02 22:54:18 -07001473 if (!hostapd_dfs_is_background_event(iface, freq)) {
1474 /* Skip if reported radar event not overlapped our channels */
1475 if (!dfs_are_channels_overlapped(iface, freq, chan_width,
1476 cf1, cf2))
1477 return 0;
1478 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001479
Sunil Ravia04bd252022-05-02 22:54:18 -07001480 if (hostapd_dfs_background_start_channel_switch(iface, freq)) {
1481 /* Radar detected while operating, switch the channel. */
1482 return hostapd_dfs_start_channel_switch(iface);
1483 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001484
Sunil Ravia04bd252022-05-02 22:54:18 -07001485 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001486}
1487
1488
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001489int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001490 int ht_enabled, int chan_offset, int chan_width,
1491 int cf1, int cf2)
1492{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001493 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED
1494 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1495 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001496
1497 /* Proceed only if DFS is not offloaded to the driver */
1498 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1499 return 0;
1500
Dmitry Shmidt051af732013-10-22 13:52:46 -07001501 /* TODO add correct implementation here */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001502 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1503 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001504
Sunil Ravia04bd252022-05-02 22:54:18 -07001505 if (iface->state == HAPD_IFACE_DFS && !iface->cac_started) {
1506 /* Handle cases where all channels were initially unavailable */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001507 hostapd_handle_dfs(iface);
Sunil Ravia04bd252022-05-02 22:54:18 -07001508 } else if (dfs_use_radar_background(iface) &&
1509 iface->radar_background.channel == -1) {
1510 /* Reset radar background chain if disabled */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001511 hostapd_dfs_update_background_chain(iface);
Sunil Ravia04bd252022-05-02 22:54:18 -07001512 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001513
Dmitry Shmidt051af732013-10-22 13:52:46 -07001514 return 0;
1515}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001516
1517
1518int hostapd_is_dfs_required(struct hostapd_iface *iface)
1519{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001520 int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001521
Hai Shaloma20dcd72022-02-04 13:43:00 -08001522 if ((!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1523 !iface->conf->ieee80211h) ||
1524 !iface->current_mode ||
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001525 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
1526 return 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001527
1528 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001529 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001530 if (start_chan_idx == -1)
1531 return -1;
1532
1533 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001534 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001535
1536 /* Check if any of configured channels require DFS */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001537 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
1538 if (res)
1539 return res;
1540 if (start_chan_idx1 >= 0 && n_chans1 > 0)
1541 res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1);
1542 return res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001543}
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001544
1545
1546int hostapd_dfs_start_cac(struct hostapd_iface *iface, int freq,
1547 int ht_enabled, int chan_offset, int chan_width,
1548 int cf1, int cf2)
1549{
Sunil Ravia04bd252022-05-02 22:54:18 -07001550 if (hostapd_dfs_is_background_event(iface, freq)) {
1551 iface->radar_background.cac_started = 1;
1552 } else {
1553 /* This is called when the driver indicates that an offloaded
Sunil Ravi99c035e2024-07-12 01:42:03 +00001554 * DFS has started CAC. radar_detected might be set for previous
1555 * DFS channel. Clear it for this new CAC process. */
Sunil Ravia04bd252022-05-02 22:54:18 -07001556 hostapd_set_state(iface, HAPD_IFACE_DFS);
1557 iface->cac_started = 1;
Sunil Ravi99c035e2024-07-12 01:42:03 +00001558
1559 /* Clear radar_detected in case it is for the previous
1560 * frequency. Also remove disabled link's information in RNR
1561 * element from other links. */
1562 iface->radar_detected = false;
1563 if (iface->interfaces && iface->interfaces->count > 1)
1564 ieee802_11_set_beacons(iface);
Sunil Ravia04bd252022-05-02 22:54:18 -07001565 }
Hai Shalomc3565922019-10-28 11:58:20 -07001566 /* TODO: How to check CAC time for ETSI weather channels? */
1567 iface->dfs_cac_ms = 60000;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001568 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
1569 "freq=%d chan=%d chan_offset=%d width=%d seg0=%d "
Sunil Ravia04bd252022-05-02 22:54:18 -07001570 "seg1=%d cac_time=%ds%s",
Hai Shalomc3565922019-10-28 11:58:20 -07001571 freq, (freq - 5000) / 5, chan_offset, chan_width, cf1, cf2,
Sunil Ravia04bd252022-05-02 22:54:18 -07001572 iface->dfs_cac_ms / 1000,
1573 hostapd_dfs_is_background_event(iface, freq) ?
1574 " (background)" : "");
1575
Hai Shalomc3565922019-10-28 11:58:20 -07001576 os_get_reltime(&iface->dfs_cac_start);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001577 return 0;
1578}
1579
1580
1581/*
1582 * Main DFS handler for offloaded case.
1583 * 2 - continue channel/AP setup for non-DFS channel
1584 * 1 - continue channel/AP setup for DFS channel
1585 * 0 - channel/AP setup will be continued after CAC
1586 * -1 - hit critical error
1587 */
1588int hostapd_handle_dfs_offload(struct hostapd_iface *iface)
1589{
Hai Shaloma20dcd72022-02-04 13:43:00 -08001590 int dfs_res;
1591
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001592 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1593 __func__, iface->cac_started);
1594
1595 /*
1596 * If DFS has already been started, then we are being called from a
1597 * callback to continue AP/channel setup. Reset the CAC start flag and
1598 * return.
1599 */
1600 if (iface->cac_started) {
1601 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1602 __func__, iface->cac_started);
1603 iface->cac_started = 0;
1604 return 1;
1605 }
1606
Hai Shaloma20dcd72022-02-04 13:43:00 -08001607 dfs_res = hostapd_is_dfs_required(iface);
1608 if (dfs_res > 0) {
1609 wpa_printf(MSG_DEBUG,
1610 "%s: freq %d MHz requires DFS for %d chans",
1611 __func__, iface->freq, dfs_res);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001612 return 0;
1613 }
1614
1615 wpa_printf(MSG_DEBUG,
1616 "%s: freq %d MHz does not require DFS. Continue channel/AP setup",
1617 __func__, iface->freq);
1618 return 2;
1619}
Hai Shalomfdcde762020-04-02 11:19:20 -07001620
1621
1622int hostapd_is_dfs_overlap(struct hostapd_iface *iface, enum chan_width width,
1623 int center_freq)
1624{
1625 struct hostapd_channel_data *chan;
1626 struct hostapd_hw_modes *mode = iface->current_mode;
1627 int half_width;
1628 int res = 0;
1629 int i;
1630
1631 if (!iface->conf->ieee80211h || !mode ||
1632 mode->mode != HOSTAPD_MODE_IEEE80211A)
1633 return 0;
1634
1635 switch (width) {
1636 case CHAN_WIDTH_20_NOHT:
1637 case CHAN_WIDTH_20:
1638 half_width = 10;
1639 break;
1640 case CHAN_WIDTH_40:
1641 half_width = 20;
1642 break;
1643 case CHAN_WIDTH_80:
1644 case CHAN_WIDTH_80P80:
1645 half_width = 40;
1646 break;
1647 case CHAN_WIDTH_160:
1648 half_width = 80;
1649 break;
1650 default:
1651 wpa_printf(MSG_WARNING, "DFS chanwidth %d not supported",
1652 width);
1653 return 0;
1654 }
1655
1656 for (i = 0; i < mode->num_channels; i++) {
1657 chan = &mode->channels[i];
1658
1659 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
1660 continue;
1661
Hai Shalom60840252021-02-19 19:02:11 -08001662 if ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
1663 HOSTAPD_CHAN_DFS_AVAILABLE)
1664 continue;
1665
Hai Shalomfdcde762020-04-02 11:19:20 -07001666 if (center_freq - chan->freq < half_width &&
1667 chan->freq - center_freq < half_width)
1668 res++;
1669 }
1670
Hai Shalom60840252021-02-19 19:02:11 -08001671 wpa_printf(MSG_DEBUG, "DFS CAC required: (%d, %d): in range: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -07001672 center_freq - half_width, center_freq + half_width,
1673 res ? "yes" : "no");
1674
1675 return res;
1676}