blob: 7f31f283d39d7dbe412af22b80c89f0f1b06b70c [file] [log] [blame]
Dmitry Shmidt051af732013-10-22 13:52:46 -07001/*
2 * DFS - Dynamic Frequency Selection
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004 * Copyright (c) 2013-2017, Qualcomm Atheros, Inc.
Dmitry Shmidt051af732013-10-22 13:52:46 -07005 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10#include "utils/includes.h"
11
12#include "utils/common.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidt7f656022015-02-25 14:36:37 -080014#include "common/hw_features_common.h"
Dmitry Shmidtcce06662013-11-04 18:44:24 -080015#include "common/wpa_ctrl.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070016#include "hostapd.h"
17#include "ap_drv_ops.h"
18#include "drivers/driver.h"
19#include "dfs.h"
20
21
Sunil Ravia04bd252022-05-02 22:54:18 -070022enum dfs_channel_type {
23 DFS_ANY_CHANNEL,
24 DFS_AVAILABLE, /* non-radar or radar-available */
25 DFS_NO_CAC_YET, /* radar-not-yet-available */
26};
27
28static struct hostapd_channel_data *
29dfs_downgrade_bandwidth(struct hostapd_iface *iface, int *secondary_channel,
30 u8 *oper_centr_freq_seg0_idx,
31 u8 *oper_centr_freq_seg1_idx,
32 enum dfs_channel_type *channel_type);
33
34
35static bool dfs_use_radar_background(struct hostapd_iface *iface)
36{
37 return (iface->drv_flags2 & WPA_DRIVER_RADAR_BACKGROUND) &&
38 iface->conf->enable_background_radar;
39}
40
41
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070042static int dfs_get_used_n_chans(struct hostapd_iface *iface, int *seg1)
Dmitry Shmidt051af732013-10-22 13:52:46 -070043{
44 int n_chans = 1;
45
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070046 *seg1 = 0;
47
Dmitry Shmidtcce06662013-11-04 18:44:24 -080048 if (iface->conf->ieee80211n && iface->conf->secondary_channel)
Dmitry Shmidt051af732013-10-22 13:52:46 -070049 n_chans = 2;
50
Hai Shalom81f62d82019-07-22 12:10:00 -070051 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
52 switch (hostapd_get_oper_chwidth(iface->conf)) {
Sunil8cd6f4d2022-06-28 18:40:46 +000053 case CONF_OPER_CHWIDTH_USE_HT:
Dmitry Shmidt051af732013-10-22 13:52:46 -070054 break;
Sunil8cd6f4d2022-06-28 18:40:46 +000055 case CONF_OPER_CHWIDTH_80MHZ:
Dmitry Shmidt051af732013-10-22 13:52:46 -070056 n_chans = 4;
57 break;
Sunil8cd6f4d2022-06-28 18:40:46 +000058 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidt051af732013-10-22 13:52:46 -070059 n_chans = 8;
60 break;
Sunil8cd6f4d2022-06-28 18:40:46 +000061 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -070062 n_chans = 4;
63 *seg1 = 4;
64 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -070065 default:
66 break;
67 }
68 }
69
70 return n_chans;
71}
72
73
Sunil Ravia04bd252022-05-02 22:54:18 -070074/* dfs_channel_available: select new channel according to type parameter */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080075static int dfs_channel_available(struct hostapd_channel_data *chan,
Sunil Ravia04bd252022-05-02 22:54:18 -070076 enum dfs_channel_type type)
Dmitry Shmidt051af732013-10-22 13:52:46 -070077{
Sunil Ravia04bd252022-05-02 22:54:18 -070078 if (type == DFS_NO_CAC_YET) {
79 /* Select only radar channel where CAC has not been
80 * performed yet
81 */
82 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
83 (chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
84 HOSTAPD_CHAN_DFS_USABLE)
85 return 1;
86 return 0;
87 }
88
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080089 /*
90 * When radar detection happens, CSA is performed. However, there's no
91 * time for CAC, so radar channels must be skipped when finding a new
Dmitry Shmidt98660862014-03-11 17:26:21 -070092 * channel for CSA, unless they are available for immediate use.
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080093 */
Sunil Ravia04bd252022-05-02 22:54:18 -070094 if (type == DFS_AVAILABLE && (chan->flag & HOSTAPD_CHAN_RADAR) &&
Dmitry Shmidt98660862014-03-11 17:26:21 -070095 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) !=
96 HOSTAPD_CHAN_DFS_AVAILABLE))
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080097 return 0;
98
Dmitry Shmidt051af732013-10-22 13:52:46 -070099 if (chan->flag & HOSTAPD_CHAN_DISABLED)
100 return 0;
101 if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
102 ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
103 HOSTAPD_CHAN_DFS_UNAVAILABLE))
104 return 0;
105 return 1;
106}
107
108
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700109static int dfs_is_chan_allowed(struct hostapd_channel_data *chan, int n_chans)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700110{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700111 /*
112 * The tables contain first valid channel number based on channel width.
113 * We will also choose this first channel as the control one.
114 */
115 int allowed_40[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
Hai Shalom60840252021-02-19 19:02:11 -0800116 165, 173, 184, 192 };
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700117 /*
118 * VHT80, valid channels based on center frequency:
Hai Shalom60840252021-02-19 19:02:11 -0800119 * 42, 58, 106, 122, 138, 155, 171
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700120 */
Hai Shalom60840252021-02-19 19:02:11 -0800121 int allowed_80[] = { 36, 52, 100, 116, 132, 149, 165 };
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800122 /*
123 * VHT160 valid channels based on center frequency:
Hai Shalom60840252021-02-19 19:02:11 -0800124 * 50, 114, 163
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800125 */
Hai Shalom60840252021-02-19 19:02:11 -0800126 int allowed_160[] = { 36, 100, 149 };
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700127 int *allowed = allowed_40;
128 unsigned int i, allowed_no = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700129
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700130 switch (n_chans) {
131 case 2:
132 allowed = allowed_40;
133 allowed_no = ARRAY_SIZE(allowed_40);
134 break;
135 case 4:
136 allowed = allowed_80;
137 allowed_no = ARRAY_SIZE(allowed_80);
138 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800139 case 8:
140 allowed = allowed_160;
141 allowed_no = ARRAY_SIZE(allowed_160);
142 break;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700143 default:
144 wpa_printf(MSG_DEBUG, "Unknown width for %d channels", n_chans);
145 break;
146 }
147
148 for (i = 0; i < allowed_no; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700149 if (chan->chan == allowed[i])
150 return 1;
151 }
152
153 return 0;
154}
155
156
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700157static struct hostapd_channel_data *
158dfs_get_chan_data(struct hostapd_hw_modes *mode, int freq, int first_chan_idx)
159{
160 int i;
161
162 for (i = first_chan_idx; i < mode->num_channels; i++) {
163 if (mode->channels[i].freq == freq)
164 return &mode->channels[i];
165 }
166
167 return NULL;
168}
169
170
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800171static int dfs_chan_range_available(struct hostapd_hw_modes *mode,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800172 int first_chan_idx, int num_chans,
Sunil Ravia04bd252022-05-02 22:54:18 -0700173 enum dfs_channel_type type)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800174{
175 struct hostapd_channel_data *first_chan, *chan;
176 int i;
Hai Shalom74f70d42019-02-11 14:42:39 -0800177 u32 bw = num_chan_to_bw(num_chans);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800178
Hai Shalomfdcde762020-04-02 11:19:20 -0700179 if (first_chan_idx + num_chans > mode->num_channels) {
180 wpa_printf(MSG_DEBUG,
181 "DFS: some channels in range not defined");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800182 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700183 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800184
185 first_chan = &mode->channels[first_chan_idx];
186
Hai Shalom74f70d42019-02-11 14:42:39 -0800187 /* hostapd DFS implementation assumes the first channel as primary.
188 * If it's not allowed to use the first channel as primary, decline the
189 * whole channel range. */
Hai Shalomfdcde762020-04-02 11:19:20 -0700190 if (!chan_pri_allowed(first_chan)) {
191 wpa_printf(MSG_DEBUG, "DFS: primary chanenl not allowed");
Hai Shalom74f70d42019-02-11 14:42:39 -0800192 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700193 }
Hai Shalom74f70d42019-02-11 14:42:39 -0800194
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800195 for (i = 0; i < num_chans; i++) {
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700196 chan = dfs_get_chan_data(mode, first_chan->freq + i * 20,
197 first_chan_idx);
Hai Shalomfdcde762020-04-02 11:19:20 -0700198 if (!chan) {
199 wpa_printf(MSG_DEBUG, "DFS: no channel data for %d",
200 first_chan->freq + i * 20);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800201 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700202 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800203
Hai Shalom74f70d42019-02-11 14:42:39 -0800204 /* HT 40 MHz secondary channel availability checked only for
205 * primary channel */
Hai Shalomfdcde762020-04-02 11:19:20 -0700206 if (!chan_bw_allowed(chan, bw, 1, !i)) {
207 wpa_printf(MSG_DEBUG, "DFS: bw now allowed for %d",
208 first_chan->freq + i * 20);
Hai Shalom74f70d42019-02-11 14:42:39 -0800209 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700210 }
Hai Shalom74f70d42019-02-11 14:42:39 -0800211
Sunil Ravia04bd252022-05-02 22:54:18 -0700212 if (!dfs_channel_available(chan, type)) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700213 wpa_printf(MSG_DEBUG, "DFS: channel not available %d",
214 first_chan->freq + i * 20);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800215 return 0;
Hai Shalomfdcde762020-04-02 11:19:20 -0700216 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800217 }
218
219 return 1;
220}
221
222
Dmitry Shmidt98660862014-03-11 17:26:21 -0700223static int is_in_chanlist(struct hostapd_iface *iface,
224 struct hostapd_channel_data *chan)
225{
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700226 if (!iface->conf->acs_ch_list.num)
Dmitry Shmidt98660862014-03-11 17:26:21 -0700227 return 1;
228
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700229 return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700230}
231
232
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800233/*
234 * The function assumes HT40+ operation.
235 * Make sure to adjust the following variables after calling this:
236 * - hapd->secondary_channel
Hai Shalom81f62d82019-07-22 12:10:00 -0700237 * - hapd->vht/he_oper_centr_freq_seg0_idx
238 * - hapd->vht/he_oper_centr_freq_seg1_idx
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800239 */
240static int dfs_find_channel(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700241 struct hostapd_channel_data **ret_chan,
Sunil Ravia04bd252022-05-02 22:54:18 -0700242 int idx, enum dfs_channel_type type)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700243{
244 struct hostapd_hw_modes *mode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800245 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700246 int i, channel_idx = 0, n_chans, n_chans1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700247
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800248 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700249 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700250
251 wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans);
252 for (i = 0; i < mode->num_channels; i++) {
253 chan = &mode->channels[i];
254
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800255 /* Skip HT40/VHT incompatible channels */
256 if (iface->conf->ieee80211n &&
257 iface->conf->secondary_channel &&
Hai Shalom74f70d42019-02-11 14:42:39 -0800258 (!dfs_is_chan_allowed(chan, n_chans) ||
Hai Shalomfdcde762020-04-02 11:19:20 -0700259 !(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P))) {
260 wpa_printf(MSG_DEBUG,
261 "DFS: channel %d (%d) is incompatible",
262 chan->freq, chan->chan);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700263 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -0700264 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700265
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800266 /* Skip incompatible chandefs */
Sunil Ravia04bd252022-05-02 22:54:18 -0700267 if (!dfs_chan_range_available(mode, i, n_chans, type)) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700268 wpa_printf(MSG_DEBUG,
269 "DFS: range not available for %d (%d)",
270 chan->freq, chan->chan);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800271 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -0700272 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700273
Hai Shalomfdcde762020-04-02 11:19:20 -0700274 if (!is_in_chanlist(iface, chan)) {
275 wpa_printf(MSG_DEBUG,
276 "DFS: channel %d (%d) not in chanlist",
277 chan->freq, chan->chan);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700278 continue;
Hai Shalomfdcde762020-04-02 11:19:20 -0700279 }
Dmitry Shmidt98660862014-03-11 17:26:21 -0700280
Hai Shaloma20dcd72022-02-04 13:43:00 -0800281 if (chan->max_tx_power < iface->conf->min_tx_power)
282 continue;
283
Sunil Ravi89eba102022-09-13 21:04:37 -0700284 if ((chan->flag & HOSTAPD_CHAN_INDOOR_ONLY) &&
285 iface->conf->country[2] == 0x4f)
286 continue;
287
Dmitry Shmidt051af732013-10-22 13:52:46 -0700288 if (ret_chan && idx == channel_idx) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700289 wpa_printf(MSG_DEBUG, "Selected channel %d (%d)",
290 chan->freq, chan->chan);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700291 *ret_chan = chan;
292 return idx;
293 }
Hai Shalomfdcde762020-04-02 11:19:20 -0700294 wpa_printf(MSG_DEBUG, "Adding channel %d (%d)",
295 chan->freq, chan->chan);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700296 channel_idx++;
297 }
298 return channel_idx;
299}
300
301
Hai Shalom81f62d82019-07-22 12:10:00 -0700302static void dfs_adjust_center_freq(struct hostapd_iface *iface,
303 struct hostapd_channel_data *chan,
304 int secondary_channel,
Hai Shalomfdcde762020-04-02 11:19:20 -0700305 int sec_chan_idx_80p80,
Hai Shalom81f62d82019-07-22 12:10:00 -0700306 u8 *oper_centr_freq_seg0_idx,
307 u8 *oper_centr_freq_seg1_idx)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700308{
Hai Shalom81f62d82019-07-22 12:10:00 -0700309 if (!iface->conf->ieee80211ac && !iface->conf->ieee80211ax)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700310 return;
311
312 if (!chan)
313 return;
314
Hai Shalom81f62d82019-07-22 12:10:00 -0700315 *oper_centr_freq_seg1_idx = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800316
Hai Shalom81f62d82019-07-22 12:10:00 -0700317 switch (hostapd_get_oper_chwidth(iface->conf)) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000318 case CONF_OPER_CHWIDTH_USE_HT:
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800319 if (secondary_channel == 1)
Hai Shalom81f62d82019-07-22 12:10:00 -0700320 *oper_centr_freq_seg0_idx = chan->chan + 2;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800321 else if (secondary_channel == -1)
Hai Shalom81f62d82019-07-22 12:10:00 -0700322 *oper_centr_freq_seg0_idx = chan->chan - 2;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700323 else
Hai Shalom81f62d82019-07-22 12:10:00 -0700324 *oper_centr_freq_seg0_idx = chan->chan;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700325 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000326 case CONF_OPER_CHWIDTH_80MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700327 *oper_centr_freq_seg0_idx = chan->chan + 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700328 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000329 case CONF_OPER_CHWIDTH_160MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700330 *oper_centr_freq_seg0_idx = chan->chan + 14;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700331 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000332 case CONF_OPER_CHWIDTH_80P80MHZ:
Hai Shalomfdcde762020-04-02 11:19:20 -0700333 *oper_centr_freq_seg0_idx = chan->chan + 6;
334 *oper_centr_freq_seg1_idx = sec_chan_idx_80p80 + 6;
335 break;
336
Dmitry Shmidt051af732013-10-22 13:52:46 -0700337 default:
Hai Shalomfdcde762020-04-02 11:19:20 -0700338 wpa_printf(MSG_INFO,
339 "DFS: Unsupported channel width configuration");
Hai Shalom81f62d82019-07-22 12:10:00 -0700340 *oper_centr_freq_seg0_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700341 break;
342 }
343
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800344 wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d, %d",
Hai Shalom81f62d82019-07-22 12:10:00 -0700345 *oper_centr_freq_seg0_idx,
346 *oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700347}
348
349
350/* Return start channel idx we will use for mode->channels[idx] */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700351static int dfs_get_start_chan_idx(struct hostapd_iface *iface, int *seg1_start)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700352{
353 struct hostapd_hw_modes *mode;
354 struct hostapd_channel_data *chan;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800355 int channel_no = iface->conf->channel;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700356 int res = -1, i;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700357 int chan_seg1 = -1;
358
359 *seg1_start = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700360
361 /* HT40- */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800362 if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700363 channel_no -= 4;
364
Hai Shalom81f62d82019-07-22 12:10:00 -0700365 /* VHT/HE */
366 if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) {
367 switch (hostapd_get_oper_chwidth(iface->conf)) {
Sunil8cd6f4d2022-06-28 18:40:46 +0000368 case CONF_OPER_CHWIDTH_USE_HT:
Dmitry Shmidt051af732013-10-22 13:52:46 -0700369 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000370 case CONF_OPER_CHWIDTH_80MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700371 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
372 iface->conf) - 6;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700373 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000374 case CONF_OPER_CHWIDTH_160MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700375 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
376 iface->conf) - 14;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700377 break;
Sunil8cd6f4d2022-06-28 18:40:46 +0000378 case CONF_OPER_CHWIDTH_80P80MHZ:
Hai Shalom81f62d82019-07-22 12:10:00 -0700379 channel_no = hostapd_get_oper_centr_freq_seg0_idx(
380 iface->conf) - 6;
381 chan_seg1 = hostapd_get_oper_centr_freq_seg1_idx(
382 iface->conf) - 6;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700383 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700384 default:
385 wpa_printf(MSG_INFO,
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700386 "DFS only VHT20/40/80/160/80+80 is supported now");
Dmitry Shmidt051af732013-10-22 13:52:46 -0700387 channel_no = -1;
388 break;
389 }
390 }
391
392 /* Get idx */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800393 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700394 for (i = 0; i < mode->num_channels; i++) {
395 chan = &mode->channels[i];
396 if (chan->chan == channel_no) {
397 res = i;
398 break;
399 }
400 }
401
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700402 if (res != -1 && chan_seg1 > -1) {
403 int found = 0;
404
405 /* Get idx for seg1 */
406 mode = iface->current_mode;
407 for (i = 0; i < mode->num_channels; i++) {
408 chan = &mode->channels[i];
409 if (chan->chan == chan_seg1) {
410 *seg1_start = i;
411 found = 1;
412 break;
413 }
414 }
415 if (!found)
416 res = -1;
417 }
418
Dmitry Shmidt98660862014-03-11 17:26:21 -0700419 if (res == -1) {
420 wpa_printf(MSG_DEBUG,
421 "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d",
422 mode->num_channels, channel_no, iface->conf->channel,
423 iface->conf->ieee80211n,
424 iface->conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700425 hostapd_get_oper_chwidth(iface->conf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700426
427 for (i = 0; i < mode->num_channels; i++) {
428 wpa_printf(MSG_DEBUG, "Available channel: %d",
429 mode->channels[i].chan);
430 }
431 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700432
433 return res;
434}
435
436
437/* At least one channel have radar flag */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800438static int dfs_check_chans_radar(struct hostapd_iface *iface,
439 int start_chan_idx, int n_chans)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700440{
441 struct hostapd_channel_data *channel;
442 struct hostapd_hw_modes *mode;
443 int i, res = 0;
444
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800445 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700446
447 for (i = 0; i < n_chans; i++) {
448 channel = &mode->channels[start_chan_idx + i];
449 if (channel->flag & HOSTAPD_CHAN_RADAR)
450 res++;
451 }
452
453 return res;
454}
455
456
457/* All channels available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800458static int dfs_check_chans_available(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700459 int start_chan_idx, int n_chans)
460{
461 struct hostapd_channel_data *channel;
462 struct hostapd_hw_modes *mode;
463 int i;
464
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800465 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700466
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800467 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700468 channel = &mode->channels[start_chan_idx + i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800469
470 if (channel->flag & HOSTAPD_CHAN_DISABLED)
471 break;
472
473 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
474 continue;
475
Dmitry Shmidt051af732013-10-22 13:52:46 -0700476 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
477 HOSTAPD_CHAN_DFS_AVAILABLE)
478 break;
479 }
480
481 return i == n_chans;
482}
483
484
485/* At least one channel unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800486static int dfs_check_chans_unavailable(struct hostapd_iface *iface,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700487 int start_chan_idx,
488 int n_chans)
489{
490 struct hostapd_channel_data *channel;
491 struct hostapd_hw_modes *mode;
492 int i, res = 0;
493
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800494 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700495
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800496 for (i = 0; i < n_chans; i++) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700497 channel = &mode->channels[start_chan_idx + i];
498 if (channel->flag & HOSTAPD_CHAN_DISABLED)
499 res++;
500 if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
501 HOSTAPD_CHAN_DFS_UNAVAILABLE)
502 res++;
503 }
504
505 return res;
506}
507
508
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800509static struct hostapd_channel_data *
510dfs_get_valid_channel(struct hostapd_iface *iface,
511 int *secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700512 u8 *oper_centr_freq_seg0_idx,
513 u8 *oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -0700514 enum dfs_channel_type type)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700515{
516 struct hostapd_hw_modes *mode;
517 struct hostapd_channel_data *chan = NULL;
Hai Shalomfdcde762020-04-02 11:19:20 -0700518 struct hostapd_channel_data *chan2 = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800519 int num_available_chandefs;
Hai Shalomfdcde762020-04-02 11:19:20 -0700520 int chan_idx, chan_idx2;
521 int sec_chan_idx_80p80 = -1;
522 int i;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700523 u32 _rand;
524
525 wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800526 *secondary_channel = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -0700527 *oper_centr_freq_seg0_idx = 0;
528 *oper_centr_freq_seg1_idx = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700529
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800530 if (iface->current_mode == NULL)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700531 return NULL;
532
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800533 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700534 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
535 return NULL;
536
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800537 /* Get the count first */
Sunil Ravia04bd252022-05-02 22:54:18 -0700538 num_available_chandefs = dfs_find_channel(iface, NULL, 0, type);
Hai Shalomfdcde762020-04-02 11:19:20 -0700539 wpa_printf(MSG_DEBUG, "DFS: num_available_chandefs=%d",
540 num_available_chandefs);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800541 if (num_available_chandefs == 0)
542 return NULL;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700543
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800544 if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800545 return NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800546 chan_idx = _rand % num_available_chandefs;
Sunil Ravia04bd252022-05-02 22:54:18 -0700547 dfs_find_channel(iface, &chan, chan_idx, type);
Hai Shalomfdcde762020-04-02 11:19:20 -0700548 if (!chan) {
549 wpa_printf(MSG_DEBUG, "DFS: no random channel found");
550 return NULL;
551 }
552 wpa_printf(MSG_DEBUG, "DFS: got random channel %d (%d)",
553 chan->freq, chan->chan);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800554
555 /* dfs_find_channel() calculations assume HT40+ */
556 if (iface->conf->secondary_channel)
557 *secondary_channel = 1;
558 else
559 *secondary_channel = 0;
560
Hai Shalomfdcde762020-04-02 11:19:20 -0700561 /* Get secondary channel for HT80P80 */
Sunil8cd6f4d2022-06-28 18:40:46 +0000562 if (hostapd_get_oper_chwidth(iface->conf) ==
563 CONF_OPER_CHWIDTH_80P80MHZ) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700564 if (num_available_chandefs <= 1) {
565 wpa_printf(MSG_ERROR,
566 "only 1 valid chan, can't support 80+80");
567 return NULL;
568 }
569
570 /*
571 * Loop all channels except channel1 to find a valid channel2
572 * that is not adjacent to channel1.
573 */
574 for (i = 0; i < num_available_chandefs - 1; i++) {
575 /* start from chan_idx + 1, end when chan_idx - 1 */
576 chan_idx2 = (chan_idx + 1 + i) % num_available_chandefs;
Sunil Ravia04bd252022-05-02 22:54:18 -0700577 dfs_find_channel(iface, &chan2, chan_idx2, type);
Hai Shalomfdcde762020-04-02 11:19:20 -0700578 if (chan2 && abs(chan2->chan - chan->chan) > 12) {
579 /* two channels are not adjacent */
580 sec_chan_idx_80p80 = chan2->chan;
581 wpa_printf(MSG_DEBUG,
582 "DFS: got second chan: %d (%d)",
583 chan2->freq, chan2->chan);
584 break;
585 }
586 }
587
588 /* Check if we got a valid secondary channel which is not
589 * adjacent to the first channel.
590 */
591 if (sec_chan_idx_80p80 == -1) {
592 wpa_printf(MSG_INFO,
593 "DFS: failed to get chan2 for 80+80");
594 return NULL;
595 }
596 }
597
Hai Shalom81f62d82019-07-22 12:10:00 -0700598 dfs_adjust_center_freq(iface, chan,
599 *secondary_channel,
Hai Shalomfdcde762020-04-02 11:19:20 -0700600 sec_chan_idx_80p80,
Hai Shalom81f62d82019-07-22 12:10:00 -0700601 oper_centr_freq_seg0_idx,
602 oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700603
604 return chan;
605}
606
607
Sunil Ravia04bd252022-05-02 22:54:18 -0700608static int dfs_set_valid_channel(struct hostapd_iface *iface, int skip_radar)
609{
610 struct hostapd_channel_data *channel;
611 u8 cf1 = 0, cf2 = 0;
612 int sec = 0;
613
614 channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2,
615 skip_radar ? DFS_AVAILABLE :
616 DFS_ANY_CHANNEL);
617 if (!channel) {
618 wpa_printf(MSG_ERROR, "could not get valid channel");
619 return -1;
620 }
621
622 iface->freq = channel->freq;
623 iface->conf->channel = channel->chan;
624 iface->conf->secondary_channel = sec;
625 hostapd_set_oper_centr_freq_seg0_idx(iface->conf, cf1);
626 hostapd_set_oper_centr_freq_seg1_idx(iface->conf, cf2);
627
628 return 0;
629}
630
631
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800632static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700633{
634 struct hostapd_hw_modes *mode;
635 struct hostapd_channel_data *chan = NULL;
636 int i;
637
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800638 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700639 if (mode == NULL)
640 return 0;
641
642 wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800643 for (i = 0; i < iface->current_mode->num_channels; i++) {
644 chan = &iface->current_mode->channels[i];
Dmitry Shmidt051af732013-10-22 13:52:46 -0700645 if (chan->freq == freq) {
646 if (chan->flag & HOSTAPD_CHAN_RADAR) {
647 chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
648 chan->flag |= state;
649 return 1; /* Channel found */
650 }
651 }
652 }
653 wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
654 return 0;
655}
656
657
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800658static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700659 int chan_offset, int chan_width, int cf1,
660 int cf2, u32 state)
661{
662 int n_chans = 1, i;
663 struct hostapd_hw_modes *mode;
664 int frequency = freq;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800665 int frequency2 = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700666 int ret = 0;
667
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800668 mode = iface->current_mode;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700669 if (mode == NULL)
670 return 0;
671
672 if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
673 wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
674 return 0;
675 }
676
677 /* Seems cf1 and chan_width is enough here */
678 switch (chan_width) {
679 case CHAN_WIDTH_20_NOHT:
680 case CHAN_WIDTH_20:
681 n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800682 if (frequency == 0)
683 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700684 break;
685 case CHAN_WIDTH_40:
686 n_chans = 2;
687 frequency = cf1 - 10;
688 break;
689 case CHAN_WIDTH_80:
690 n_chans = 4;
691 frequency = cf1 - 30;
692 break;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800693 case CHAN_WIDTH_80P80:
694 n_chans = 4;
695 frequency = cf1 - 30;
696 frequency2 = cf2 - 30;
697 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700698 case CHAN_WIDTH_160:
699 n_chans = 8;
700 frequency = cf1 - 70;
701 break;
702 default:
703 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
704 chan_width);
705 break;
706 }
707
708 wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
709 n_chans);
710 for (i = 0; i < n_chans; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800711 ret += set_dfs_state_freq(iface, frequency, state);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700712 frequency = frequency + 20;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800713
714 if (chan_width == CHAN_WIDTH_80P80) {
715 ret += set_dfs_state_freq(iface, frequency2, state);
716 frequency2 = frequency2 + 20;
717 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700718 }
719
720 return ret;
721}
722
723
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800724static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700725 int chan_width, int cf1, int cf2)
726{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700727 int start_chan_idx, start_chan_idx1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700728 struct hostapd_hw_modes *mode;
729 struct hostapd_channel_data *chan;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700730 int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700731 u8 radar_chan;
732 int res = 0;
733
Dmitry Shmidt051af732013-10-22 13:52:46 -0700734 /* Our configuration */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800735 mode = iface->current_mode;
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700736 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
737 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700738
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700739 /* Check we are on DFS channel(s) */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800740 if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700741 return 0;
742
Dmitry Shmidt051af732013-10-22 13:52:46 -0700743 /* Reported via radar event */
744 switch (chan_width) {
745 case CHAN_WIDTH_20_NOHT:
746 case CHAN_WIDTH_20:
747 radar_n_chans = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800748 if (frequency == 0)
749 frequency = cf1;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700750 break;
751 case CHAN_WIDTH_40:
752 radar_n_chans = 2;
753 frequency = cf1 - 10;
754 break;
755 case CHAN_WIDTH_80:
756 radar_n_chans = 4;
757 frequency = cf1 - 30;
758 break;
759 case CHAN_WIDTH_160:
760 radar_n_chans = 8;
761 frequency = cf1 - 70;
762 break;
763 default:
764 wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
765 chan_width);
766 break;
767 }
768
769 ieee80211_freq_to_chan(frequency, &radar_chan);
770
771 for (i = 0; i < n_chans; i++) {
772 chan = &mode->channels[start_chan_idx + i];
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700773 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
774 continue;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700775 for (j = 0; j < radar_n_chans; j++) {
776 wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
777 chan->chan, radar_chan + j * 4);
778 if (chan->chan == radar_chan + j * 4)
779 res++;
780 }
781 }
782
783 wpa_printf(MSG_DEBUG, "overlapped: %d", res);
784
785 return res;
786}
787
788
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700789static unsigned int dfs_get_cac_time(struct hostapd_iface *iface,
790 int start_chan_idx, int n_chans)
791{
792 struct hostapd_channel_data *channel;
793 struct hostapd_hw_modes *mode;
794 int i;
795 unsigned int cac_time_ms = 0;
796
797 mode = iface->current_mode;
798
799 for (i = 0; i < n_chans; i++) {
800 channel = &mode->channels[start_chan_idx + i];
801 if (!(channel->flag & HOSTAPD_CHAN_RADAR))
802 continue;
803 if (channel->dfs_cac_ms > cac_time_ms)
804 cac_time_ms = channel->dfs_cac_ms;
805 }
806
807 return cac_time_ms;
808}
809
810
Dmitry Shmidt051af732013-10-22 13:52:46 -0700811/*
812 * Main DFS handler
813 * 1 - continue channel/ap setup
814 * 0 - channel/ap setup will be continued after CAC
815 * -1 - hit critical error
816 */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800817int hostapd_handle_dfs(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700818{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700819 int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800820 int skip_radar = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700821
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800822 if (is_6ghz_freq(iface->freq))
823 return 1;
824
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800825 if (!iface->current_mode) {
826 /*
827 * This can happen with drivers that do not provide mode
828 * information and as such, cannot really use hostapd for DFS.
829 */
830 wpa_printf(MSG_DEBUG,
831 "DFS: No current_mode information - assume no need to perform DFS operations by hostapd");
832 return 1;
833 }
834
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800835 iface->cac_started = 0;
836
Dmitry Shmidt051af732013-10-22 13:52:46 -0700837 do {
838 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700839 start_chan_idx = dfs_get_start_chan_idx(iface,
840 &start_chan_idx1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700841 if (start_chan_idx == -1)
842 return -1;
843
844 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700845 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700846
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700847 /* Setup CAC time */
848 iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx,
849 n_chans);
850
Dmitry Shmidt051af732013-10-22 13:52:46 -0700851 /* Check if any of configured channels require DFS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800852 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700853 wpa_printf(MSG_DEBUG,
854 "DFS %d channels required radar detection",
855 res);
856 if (!res)
857 return 1;
858
859 /* Check if all channels are DFS available */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800860 res = dfs_check_chans_available(iface, start_chan_idx, n_chans);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700861 wpa_printf(MSG_DEBUG,
862 "DFS all channels available, (SKIP CAC): %s",
863 res ? "yes" : "no");
864 if (res)
865 return 1;
866
867 /* Check if any of configured channels is unavailable */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800868 res = dfs_check_chans_unavailable(iface, start_chan_idx,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700869 n_chans);
870 wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
871 res, res ? "yes": "no");
872 if (res) {
Sunil Ravia04bd252022-05-02 22:54:18 -0700873 if (dfs_set_valid_channel(iface, skip_radar) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800874 hostapd_set_state(iface, HAPD_IFACE_DFS);
875 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700876 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700877 }
878 } while (res);
879
880 /* Finally start CAC */
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800881 hostapd_set_state(iface, HAPD_IFACE_DFS);
Sunil Ravia04bd252022-05-02 22:54:18 -0700882 wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz%s", iface->freq,
883 dfs_use_radar_background(iface) ? " (background)" : "");
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800884 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700885 "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds",
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800886 iface->freq,
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800887 iface->conf->channel, iface->conf->secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -0700888 hostapd_get_oper_chwidth(iface->conf),
889 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
890 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700891 iface->dfs_cac_ms / 1000);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800892
Hai Shalom81f62d82019-07-22 12:10:00 -0700893 res = hostapd_start_dfs_cac(
894 iface, iface->conf->hw_mode, iface->freq, iface->conf->channel,
895 iface->conf->ieee80211n, iface->conf->ieee80211ac,
Sunil Ravia04bd252022-05-02 22:54:18 -0700896 iface->conf->ieee80211ax, iface->conf->ieee80211be,
Hai Shalom81f62d82019-07-22 12:10:00 -0700897 iface->conf->secondary_channel,
898 hostapd_get_oper_chwidth(iface->conf),
899 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
Sunil Ravia04bd252022-05-02 22:54:18 -0700900 hostapd_get_oper_centr_freq_seg1_idx(iface->conf),
901 dfs_use_radar_background(iface));
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800902
903 if (res) {
904 wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700905 return -1;
906 }
907
Sunil Ravia04bd252022-05-02 22:54:18 -0700908 if (dfs_use_radar_background(iface)) {
909 /* Cache background radar parameters. */
910 iface->radar_background.channel = iface->conf->channel;
911 iface->radar_background.secondary_channel =
912 iface->conf->secondary_channel;
913 iface->radar_background.freq = iface->freq;
914 iface->radar_background.centr_freq_seg0_idx =
915 hostapd_get_oper_centr_freq_seg0_idx(iface->conf);
916 iface->radar_background.centr_freq_seg1_idx =
917 hostapd_get_oper_centr_freq_seg1_idx(iface->conf);
918
919 /*
920 * Let's select a random channel according to the
921 * regulations and perform CAC on dedicated radar chain.
922 */
923 res = dfs_set_valid_channel(iface, 1);
924 if (res < 0)
925 return res;
926
927 iface->radar_background.temp_ch = 1;
928 return 1;
929 }
930
Dmitry Shmidt051af732013-10-22 13:52:46 -0700931 return 0;
932}
933
934
Hai Shalomfdcde762020-04-02 11:19:20 -0700935int hostapd_is_dfs_chan_available(struct hostapd_iface *iface)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700936{
937 int n_chans, n_chans1, start_chan_idx, start_chan_idx1;
938
939 /* Get the start (first) channel for current configuration */
940 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
941 if (start_chan_idx < 0)
942 return 0;
943
944 /* Get the number of used channels, depending on width */
945 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
946
947 /* Check if all channels are DFS available */
948 return dfs_check_chans_available(iface, start_chan_idx, n_chans);
949}
950
951
Sunil Ravia04bd252022-05-02 22:54:18 -0700952static int hostapd_dfs_request_channel_switch(struct hostapd_iface *iface,
953 int channel, int freq,
954 int secondary_channel,
955 u8 current_vht_oper_chwidth,
956 u8 oper_centr_freq_seg0_idx,
957 u8 oper_centr_freq_seg1_idx)
958{
959 struct hostapd_hw_modes *cmode = iface->current_mode;
960 int ieee80211_mode = IEEE80211_MODE_AP, err;
961 struct csa_settings csa_settings;
962 u8 new_vht_oper_chwidth;
963 unsigned int i;
964
965 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d", channel);
966 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
967 "freq=%d chan=%d sec_chan=%d", freq, channel,
968 secondary_channel);
969
970 new_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
971 hostapd_set_oper_chwidth(iface->conf, current_vht_oper_chwidth);
972
973 /* Setup CSA request */
974 os_memset(&csa_settings, 0, sizeof(csa_settings));
975 csa_settings.cs_count = 5;
976 csa_settings.block_tx = 1;
977#ifdef CONFIG_MESH
978 if (iface->mconf)
979 ieee80211_mode = IEEE80211_MODE_MESH;
980#endif /* CONFIG_MESH */
981 err = hostapd_set_freq_params(&csa_settings.freq_params,
982 iface->conf->hw_mode,
983 freq, channel,
984 iface->conf->enable_edmg,
985 iface->conf->edmg_channel,
986 iface->conf->ieee80211n,
987 iface->conf->ieee80211ac,
988 iface->conf->ieee80211ax,
989 iface->conf->ieee80211be,
990 secondary_channel,
991 new_vht_oper_chwidth,
992 oper_centr_freq_seg0_idx,
993 oper_centr_freq_seg1_idx,
994 cmode->vht_capab,
995 &cmode->he_capab[ieee80211_mode],
996 &cmode->eht_capab[ieee80211_mode]);
997
998 if (err) {
999 wpa_printf(MSG_ERROR,
1000 "DFS failed to calculate CSA freq params");
1001 hostapd_disable_iface(iface);
1002 return err;
1003 }
1004
1005 for (i = 0; i < iface->num_bss; i++) {
1006 err = hostapd_switch_channel(iface->bss[i], &csa_settings);
1007 if (err)
1008 break;
1009 }
1010
1011 if (err) {
1012 wpa_printf(MSG_WARNING,
1013 "DFS failed to schedule CSA (%d) - trying fallback",
1014 err);
1015 iface->freq = freq;
1016 iface->conf->channel = channel;
1017 iface->conf->secondary_channel = secondary_channel;
1018 hostapd_set_oper_chwidth(iface->conf, new_vht_oper_chwidth);
1019 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1020 oper_centr_freq_seg0_idx);
1021 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1022 oper_centr_freq_seg1_idx);
1023
1024 hostapd_disable_iface(iface);
1025 hostapd_enable_iface(iface);
1026
1027 return 0;
1028 }
1029
1030 /* Channel configuration will be updated once CSA completes and
1031 * ch_switch_notify event is received */
1032 wpa_printf(MSG_DEBUG, "DFS waiting channel switch event");
1033
1034 return 0;
1035}
1036
1037
1038static void hostpad_dfs_update_background_chain(struct hostapd_iface *iface)
1039{
1040 int sec = 0;
1041 enum dfs_channel_type channel_type = DFS_NO_CAC_YET;
1042 struct hostapd_channel_data *channel;
1043 u8 oper_centr_freq_seg0_idx = 0;
1044 u8 oper_centr_freq_seg1_idx = 0;
1045
1046 /*
1047 * Allow selection of DFS channel in ETSI to comply with
1048 * uniform spreading.
1049 */
1050 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
1051 channel_type = DFS_ANY_CHANNEL;
1052
1053 channel = dfs_get_valid_channel(iface, &sec, &oper_centr_freq_seg0_idx,
1054 &oper_centr_freq_seg1_idx,
1055 channel_type);
1056 if (!channel ||
1057 channel->chan == iface->conf->channel ||
1058 channel->chan == iface->radar_background.channel)
1059 channel = dfs_downgrade_bandwidth(iface, &sec,
1060 &oper_centr_freq_seg0_idx,
1061 &oper_centr_freq_seg1_idx,
1062 &channel_type);
1063 if (!channel ||
1064 hostapd_start_dfs_cac(iface, iface->conf->hw_mode,
1065 channel->freq, channel->chan,
1066 iface->conf->ieee80211n,
1067 iface->conf->ieee80211ac,
1068 iface->conf->ieee80211ax,
1069 iface->conf->ieee80211be,
1070 sec, hostapd_get_oper_chwidth(iface->conf),
1071 oper_centr_freq_seg0_idx,
1072 oper_centr_freq_seg1_idx, true)) {
1073 wpa_printf(MSG_ERROR, "DFS failed to start CAC offchannel");
1074 iface->radar_background.channel = -1;
1075 return;
1076 }
1077
1078 iface->radar_background.channel = channel->chan;
1079 iface->radar_background.freq = channel->freq;
1080 iface->radar_background.secondary_channel = sec;
1081 iface->radar_background.centr_freq_seg0_idx = oper_centr_freq_seg0_idx;
1082 iface->radar_background.centr_freq_seg1_idx = oper_centr_freq_seg1_idx;
1083
1084 wpa_printf(MSG_DEBUG,
1085 "%s: setting background chain to chan %d (%d MHz)",
1086 __func__, channel->chan, channel->freq);
1087}
1088
1089
1090static bool
1091hostapd_dfs_is_background_event(struct hostapd_iface *iface, int freq)
1092{
1093 return dfs_use_radar_background(iface) &&
1094 iface->radar_background.channel != -1 &&
1095 iface->radar_background.freq == freq;
1096}
1097
1098
1099static int
1100hostapd_dfs_start_channel_switch_background(struct hostapd_iface *iface)
1101{
1102 u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
1103
1104 iface->conf->channel = iface->radar_background.channel;
1105 iface->freq = iface->radar_background.freq;
1106 iface->conf->secondary_channel =
1107 iface->radar_background.secondary_channel;
1108 hostapd_set_oper_centr_freq_seg0_idx(
1109 iface->conf, iface->radar_background.centr_freq_seg0_idx);
1110 hostapd_set_oper_centr_freq_seg1_idx(
1111 iface->conf, iface->radar_background.centr_freq_seg1_idx);
1112
1113 hostpad_dfs_update_background_chain(iface);
1114
1115 return hostapd_dfs_request_channel_switch(
1116 iface, iface->conf->channel, iface->freq,
1117 iface->conf->secondary_channel, current_vht_oper_chwidth,
1118 hostapd_get_oper_centr_freq_seg0_idx(iface->conf),
1119 hostapd_get_oper_centr_freq_seg1_idx(iface->conf));
1120}
1121
1122
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001123int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001124 int ht_enabled, int chan_offset, int chan_width,
1125 int cf1, int cf2)
1126{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001127 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED
1128 "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1129 success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1130
Dmitry Shmidt051af732013-10-22 13:52:46 -07001131 if (success) {
1132 /* Complete iface/ap configuration */
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001133 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
1134 /* Complete AP configuration for the first bring up. */
1135 if (iface->state != HAPD_IFACE_ENABLED)
1136 hostapd_setup_interface_complete(iface, 0);
1137 else
1138 iface->cac_started = 0;
1139 } else {
1140 set_dfs_state(iface, freq, ht_enabled, chan_offset,
1141 chan_width, cf1, cf2,
1142 HOSTAPD_CHAN_DFS_AVAILABLE);
Sunil Ravia04bd252022-05-02 22:54:18 -07001143
1144 /*
1145 * Radar event from background chain for the selected
1146 * channel. Perform CSA, move the main chain to the
1147 * selected channel and configure the background chain
1148 * to a new DFS channel.
1149 */
1150 if (hostapd_dfs_is_background_event(iface, freq)) {
1151 iface->radar_background.cac_started = 0;
1152 if (!iface->radar_background.temp_ch)
1153 return 0;
1154
1155 iface->radar_background.temp_ch = 0;
1156 return hostapd_dfs_start_channel_switch_background(iface);
1157 }
1158
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001159 /*
1160 * Just mark the channel available when CAC completion
1161 * event is received in enabled state. CAC result could
1162 * have been propagated from another radio having the
1163 * same regulatory configuration. When CAC completion is
1164 * received during non-HAPD_IFACE_ENABLED state, make
1165 * sure the configured channel is available because this
1166 * CAC completion event could have been propagated from
1167 * another radio.
1168 */
1169 if (iface->state != HAPD_IFACE_ENABLED &&
Hai Shalomfdcde762020-04-02 11:19:20 -07001170 hostapd_is_dfs_chan_available(iface)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001171 hostapd_setup_interface_complete(iface, 0);
1172 iface->cac_started = 0;
1173 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001174 }
Sunil Ravia04bd252022-05-02 22:54:18 -07001175 } else if (hostapd_dfs_is_background_event(iface, freq)) {
1176 iface->radar_background.cac_started = 0;
1177 hostpad_dfs_update_background_chain(iface);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001178 }
1179
1180 return 0;
1181}
1182
1183
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001184int hostapd_dfs_pre_cac_expired(struct hostapd_iface *iface, int freq,
1185 int ht_enabled, int chan_offset, int chan_width,
1186 int cf1, int cf2)
1187{
1188 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_PRE_CAC_EXPIRED
1189 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1190 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1191
1192 /* Proceed only if DFS is not offloaded to the driver */
1193 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1194 return 0;
1195
1196 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1197 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
1198
1199 return 0;
1200}
1201
1202
Hai Shalomfdcde762020-04-02 11:19:20 -07001203static struct hostapd_channel_data *
1204dfs_downgrade_bandwidth(struct hostapd_iface *iface, int *secondary_channel,
1205 u8 *oper_centr_freq_seg0_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001206 u8 *oper_centr_freq_seg1_idx,
1207 enum dfs_channel_type *channel_type)
Hai Shalomfdcde762020-04-02 11:19:20 -07001208{
1209 struct hostapd_channel_data *channel;
1210
1211 for (;;) {
1212 channel = dfs_get_valid_channel(iface, secondary_channel,
1213 oper_centr_freq_seg0_idx,
1214 oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001215 *channel_type);
Hai Shalomfdcde762020-04-02 11:19:20 -07001216 if (channel) {
1217 wpa_printf(MSG_DEBUG, "DFS: Selected channel: %d",
1218 channel->chan);
1219 return channel;
1220 }
1221
Sunil Ravia04bd252022-05-02 22:54:18 -07001222 if (*channel_type != DFS_ANY_CHANNEL) {
1223 *channel_type = DFS_ANY_CHANNEL;
Hai Shalomfdcde762020-04-02 11:19:20 -07001224 } else {
Hai Shalom899fcc72020-10-19 14:38:18 -07001225 int oper_chwidth;
1226
1227 oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
Sunil8cd6f4d2022-06-28 18:40:46 +00001228 if (oper_chwidth == CONF_OPER_CHWIDTH_USE_HT)
Hai Shalomfdcde762020-04-02 11:19:20 -07001229 break;
Sunil Ravia04bd252022-05-02 22:54:18 -07001230 *channel_type = DFS_AVAILABLE;
Hai Shalom899fcc72020-10-19 14:38:18 -07001231 hostapd_set_oper_chwidth(iface->conf, oper_chwidth - 1);
Hai Shalomfdcde762020-04-02 11:19:20 -07001232 }
1233 }
1234
1235 wpa_printf(MSG_INFO,
1236 "%s: no DFS channels left, waiting for NOP to finish",
1237 __func__);
1238 return NULL;
1239}
1240
1241
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001242static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface)
Dmitry Shmidt051af732013-10-22 13:52:46 -07001243{
1244 struct hostapd_channel_data *channel;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001245 int secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001246 u8 oper_centr_freq_seg0_idx = 0;
1247 u8 oper_centr_freq_seg1_idx = 0;
Sunil Ravia04bd252022-05-02 22:54:18 -07001248 enum dfs_channel_type channel_type = DFS_ANY_CHANNEL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001249 int err = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001250
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001251 /* Radar detected during active CAC */
1252 iface->cac_started = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001253 channel = dfs_get_valid_channel(iface, &secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07001254 &oper_centr_freq_seg0_idx,
1255 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001256 channel_type);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001257
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001258 if (!channel) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001259 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1260 &oper_centr_freq_seg0_idx,
1261 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001262 &channel_type);
Hai Shalomfdcde762020-04-02 11:19:20 -07001263 if (!channel) {
1264 wpa_printf(MSG_ERROR, "No valid channel available");
1265 return err;
1266 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001267 }
1268
1269 wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d",
1270 channel->chan);
1271 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL
1272 "freq=%d chan=%d sec_chan=%d", channel->freq,
1273 channel->chan, secondary_channel);
1274
1275 iface->freq = channel->freq;
1276 iface->conf->channel = channel->chan;
1277 iface->conf->secondary_channel = secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001278 hostapd_set_oper_centr_freq_seg0_idx(iface->conf,
1279 oper_centr_freq_seg0_idx);
1280 hostapd_set_oper_centr_freq_seg1_idx(iface->conf,
1281 oper_centr_freq_seg1_idx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001282 err = 0;
1283
1284 hostapd_setup_interface_complete(iface, err);
1285 return err;
1286}
1287
1288
Sunil Ravia04bd252022-05-02 22:54:18 -07001289static int
1290hostapd_dfs_background_start_channel_switch(struct hostapd_iface *iface,
1291 int freq)
1292{
1293 if (!dfs_use_radar_background(iface))
1294 return -1; /* Background radar chain not supported. */
1295
1296 wpa_printf(MSG_DEBUG,
1297 "%s called (background CAC active: %s, CSA active: %s)",
1298 __func__, iface->radar_background.cac_started ? "yes" : "no",
1299 hostapd_csa_in_progress(iface) ? "yes" : "no");
1300
1301 /* Check if CSA in progress */
1302 if (hostapd_csa_in_progress(iface))
1303 return 0;
1304
1305 if (hostapd_dfs_is_background_event(iface, freq)) {
1306 /*
1307 * Radar pattern is reported on the background chain.
1308 * Just select a new random channel according to the
1309 * regulations for monitoring.
1310 */
1311 hostpad_dfs_update_background_chain(iface);
1312 return 0;
1313 }
1314
1315 /*
1316 * If background radar detection is supported and the radar channel
1317 * monitored by the background chain is available switch to it without
1318 * waiting for the CAC.
1319 */
1320 if (iface->radar_background.channel == -1)
1321 return -1; /* Background radar chain not available. */
1322
1323 if (iface->radar_background.cac_started) {
1324 /*
1325 * Background channel not available yet. Perform CAC on the
1326 * main chain.
1327 */
1328 iface->radar_background.temp_ch = 1;
1329 return -1;
1330 }
1331
1332 return hostapd_dfs_start_channel_switch_background(iface);
1333}
1334
1335
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001336static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface)
1337{
1338 struct hostapd_channel_data *channel;
1339 int secondary_channel;
Hai Shalom81f62d82019-07-22 12:10:00 -07001340 u8 oper_centr_freq_seg0_idx;
1341 u8 oper_centr_freq_seg1_idx;
Sunil Ravia04bd252022-05-02 22:54:18 -07001342 enum dfs_channel_type channel_type = DFS_AVAILABLE;
Hai Shalom899fcc72020-10-19 14:38:18 -07001343 u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001344
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001345 wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)",
1346 __func__, iface->cac_started ? "yes" : "no",
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001347 hostapd_csa_in_progress(iface) ? "yes" : "no");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001348
1349 /* Check if CSA in progress */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001350 if (hostapd_csa_in_progress(iface))
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001351 return 0;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001352
1353 /* Check if active CAC */
1354 if (iface->cac_started)
1355 return hostapd_dfs_start_channel_switch_cac(iface);
1356
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001357 /*
1358 * Allow selection of DFS channel in ETSI to comply with
1359 * uniform spreading.
1360 */
1361 if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI)
Sunil Ravia04bd252022-05-02 22:54:18 -07001362 channel_type = DFS_ANY_CHANNEL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001363
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001364 /* Perform channel switch/CSA */
1365 channel = dfs_get_valid_channel(iface, &secondary_channel,
Hai Shalom81f62d82019-07-22 12:10:00 -07001366 &oper_centr_freq_seg0_idx,
1367 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001368 channel_type);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001369
1370 if (!channel) {
Dmitry Shmidt98660862014-03-11 17:26:21 -07001371 /*
1372 * If there is no channel to switch immediately to, check if
1373 * there is another channel where we can switch even if it
1374 * requires to perform a CAC first.
1375 */
Sunil Ravia04bd252022-05-02 22:54:18 -07001376 channel_type = DFS_ANY_CHANNEL;
Hai Shalomfdcde762020-04-02 11:19:20 -07001377 channel = dfs_downgrade_bandwidth(iface, &secondary_channel,
1378 &oper_centr_freq_seg0_idx,
1379 &oper_centr_freq_seg1_idx,
Sunil Ravia04bd252022-05-02 22:54:18 -07001380 &channel_type);
Hai Shalom60840252021-02-19 19:02:11 -08001381 if (!channel) {
1382 /*
1383 * Toggle interface state to enter DFS state
1384 * until NOP is finished.
1385 */
1386 hostapd_disable_iface(iface);
1387 hostapd_enable_iface(iface);
1388 return 0;
1389 }
1390
Sunil Ravia04bd252022-05-02 22:54:18 -07001391 if (channel_type == DFS_ANY_CHANNEL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001392 iface->freq = channel->freq;
1393 iface->conf->channel = channel->chan;
1394 iface->conf->secondary_channel = secondary_channel;
1395 hostapd_set_oper_centr_freq_seg0_idx(
1396 iface->conf, oper_centr_freq_seg0_idx);
1397 hostapd_set_oper_centr_freq_seg1_idx(
1398 iface->conf, oper_centr_freq_seg1_idx);
1399
1400 hostapd_disable_iface(iface);
1401 hostapd_enable_iface(iface);
1402 return 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001403 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001404 }
1405
Sunil Ravia04bd252022-05-02 22:54:18 -07001406 return hostapd_dfs_request_channel_switch(iface, channel->chan,
1407 channel->freq,
1408 secondary_channel,
1409 current_vht_oper_chwidth,
1410 oper_centr_freq_seg0_idx,
1411 oper_centr_freq_seg1_idx);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001412}
1413
1414
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001415int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001416 int ht_enabled, int chan_offset, int chan_width,
1417 int cf1, int cf2)
1418{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001419 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED
1420 "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d",
1421 freq, ht_enabled, chan_offset, chan_width, cf1, cf2);
1422
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001423 /* Proceed only if DFS is not offloaded to the driver */
1424 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1425 return 0;
1426
1427 if (!iface->conf->ieee80211h)
1428 return 0;
1429
Dmitry Shmidt051af732013-10-22 13:52:46 -07001430 /* mark radar frequency as invalid */
Sunil Ravia04bd252022-05-02 22:54:18 -07001431 if (!set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1432 cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE))
Hai Shalomfdcde762020-04-02 11:19:20 -07001433 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001434
Sunil Ravia04bd252022-05-02 22:54:18 -07001435 if (!hostapd_dfs_is_background_event(iface, freq)) {
1436 /* Skip if reported radar event not overlapped our channels */
1437 if (!dfs_are_channels_overlapped(iface, freq, chan_width,
1438 cf1, cf2))
1439 return 0;
1440 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001441
Sunil Ravia04bd252022-05-02 22:54:18 -07001442 if (hostapd_dfs_background_start_channel_switch(iface, freq)) {
1443 /* Radar detected while operating, switch the channel. */
1444 return hostapd_dfs_start_channel_switch(iface);
1445 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001446
Sunil Ravia04bd252022-05-02 22:54:18 -07001447 return 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001448}
1449
1450
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001451int hostapd_dfs_nop_finished(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_NOP_FINISHED
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);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001458
1459 /* Proceed only if DFS is not offloaded to the driver */
1460 if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
1461 return 0;
1462
Dmitry Shmidt051af732013-10-22 13:52:46 -07001463 /* TODO add correct implementation here */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001464 set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width,
1465 cf1, cf2, HOSTAPD_CHAN_DFS_USABLE);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001466
Sunil Ravia04bd252022-05-02 22:54:18 -07001467 if (iface->state == HAPD_IFACE_DFS && !iface->cac_started) {
1468 /* Handle cases where all channels were initially unavailable */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001469 hostapd_handle_dfs(iface);
Sunil Ravia04bd252022-05-02 22:54:18 -07001470 } else if (dfs_use_radar_background(iface) &&
1471 iface->radar_background.channel == -1) {
1472 /* Reset radar background chain if disabled */
1473 hostpad_dfs_update_background_chain(iface);
1474 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001475
Dmitry Shmidt051af732013-10-22 13:52:46 -07001476 return 0;
1477}
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001478
1479
1480int hostapd_is_dfs_required(struct hostapd_iface *iface)
1481{
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001482 int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001483
Hai Shaloma20dcd72022-02-04 13:43:00 -08001484 if ((!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1485 !iface->conf->ieee80211h) ||
1486 !iface->current_mode ||
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001487 iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
1488 return 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001489
1490 /* Get start (first) channel for current configuration */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001491 start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001492 if (start_chan_idx == -1)
1493 return -1;
1494
1495 /* Get number of used channels, depend on width */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001496 n_chans = dfs_get_used_n_chans(iface, &n_chans1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001497
1498 /* Check if any of configured channels require DFS */
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -07001499 res = dfs_check_chans_radar(iface, start_chan_idx, n_chans);
1500 if (res)
1501 return res;
1502 if (start_chan_idx1 >= 0 && n_chans1 > 0)
1503 res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1);
1504 return res;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001505}
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001506
1507
1508int hostapd_dfs_start_cac(struct hostapd_iface *iface, int freq,
1509 int ht_enabled, int chan_offset, int chan_width,
1510 int cf1, int cf2)
1511{
Sunil Ravia04bd252022-05-02 22:54:18 -07001512 if (hostapd_dfs_is_background_event(iface, freq)) {
1513 iface->radar_background.cac_started = 1;
1514 } else {
1515 /* This is called when the driver indicates that an offloaded
1516 * DFS has started CAC. */
1517 hostapd_set_state(iface, HAPD_IFACE_DFS);
1518 iface->cac_started = 1;
1519 }
Hai Shalomc3565922019-10-28 11:58:20 -07001520 /* TODO: How to check CAC time for ETSI weather channels? */
1521 iface->dfs_cac_ms = 60000;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001522 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START
1523 "freq=%d chan=%d chan_offset=%d width=%d seg0=%d "
Sunil Ravia04bd252022-05-02 22:54:18 -07001524 "seg1=%d cac_time=%ds%s",
Hai Shalomc3565922019-10-28 11:58:20 -07001525 freq, (freq - 5000) / 5, chan_offset, chan_width, cf1, cf2,
Sunil Ravia04bd252022-05-02 22:54:18 -07001526 iface->dfs_cac_ms / 1000,
1527 hostapd_dfs_is_background_event(iface, freq) ?
1528 " (background)" : "");
1529
Hai Shalomc3565922019-10-28 11:58:20 -07001530 os_get_reltime(&iface->dfs_cac_start);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001531 return 0;
1532}
1533
1534
1535/*
1536 * Main DFS handler for offloaded case.
1537 * 2 - continue channel/AP setup for non-DFS channel
1538 * 1 - continue channel/AP setup for DFS channel
1539 * 0 - channel/AP setup will be continued after CAC
1540 * -1 - hit critical error
1541 */
1542int hostapd_handle_dfs_offload(struct hostapd_iface *iface)
1543{
Hai Shaloma20dcd72022-02-04 13:43:00 -08001544 int dfs_res;
1545
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001546 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1547 __func__, iface->cac_started);
1548
1549 /*
1550 * If DFS has already been started, then we are being called from a
1551 * callback to continue AP/channel setup. Reset the CAC start flag and
1552 * return.
1553 */
1554 if (iface->cac_started) {
1555 wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d",
1556 __func__, iface->cac_started);
1557 iface->cac_started = 0;
1558 return 1;
1559 }
1560
Hai Shaloma20dcd72022-02-04 13:43:00 -08001561 dfs_res = hostapd_is_dfs_required(iface);
1562 if (dfs_res > 0) {
1563 wpa_printf(MSG_DEBUG,
1564 "%s: freq %d MHz requires DFS for %d chans",
1565 __func__, iface->freq, dfs_res);
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001566 return 0;
1567 }
1568
1569 wpa_printf(MSG_DEBUG,
1570 "%s: freq %d MHz does not require DFS. Continue channel/AP setup",
1571 __func__, iface->freq);
1572 return 2;
1573}
Hai Shalomfdcde762020-04-02 11:19:20 -07001574
1575
1576int hostapd_is_dfs_overlap(struct hostapd_iface *iface, enum chan_width width,
1577 int center_freq)
1578{
1579 struct hostapd_channel_data *chan;
1580 struct hostapd_hw_modes *mode = iface->current_mode;
1581 int half_width;
1582 int res = 0;
1583 int i;
1584
1585 if (!iface->conf->ieee80211h || !mode ||
1586 mode->mode != HOSTAPD_MODE_IEEE80211A)
1587 return 0;
1588
1589 switch (width) {
1590 case CHAN_WIDTH_20_NOHT:
1591 case CHAN_WIDTH_20:
1592 half_width = 10;
1593 break;
1594 case CHAN_WIDTH_40:
1595 half_width = 20;
1596 break;
1597 case CHAN_WIDTH_80:
1598 case CHAN_WIDTH_80P80:
1599 half_width = 40;
1600 break;
1601 case CHAN_WIDTH_160:
1602 half_width = 80;
1603 break;
1604 default:
1605 wpa_printf(MSG_WARNING, "DFS chanwidth %d not supported",
1606 width);
1607 return 0;
1608 }
1609
1610 for (i = 0; i < mode->num_channels; i++) {
1611 chan = &mode->channels[i];
1612
1613 if (!(chan->flag & HOSTAPD_CHAN_RADAR))
1614 continue;
1615
Hai Shalom60840252021-02-19 19:02:11 -08001616 if ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
1617 HOSTAPD_CHAN_DFS_AVAILABLE)
1618 continue;
1619
Hai Shalomfdcde762020-04-02 11:19:20 -07001620 if (center_freq - chan->freq < half_width &&
1621 chan->freq - center_freq < half_width)
1622 res++;
1623 }
1624
Hai Shalom60840252021-02-19 19:02:11 -08001625 wpa_printf(MSG_DEBUG, "DFS CAC required: (%d, %d): in range: %s",
Hai Shalomfdcde762020-04-02 11:19:20 -07001626 center_freq - half_width, center_freq + half_width,
1627 res ? "yes" : "no");
1628
1629 return res;
1630}