Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * DFS - Dynamic Frequency Selection |
| 3 | * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4 | * Copyright (c) 2013-2017, Qualcomm Atheros, Inc. |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5 | * |
| 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 Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 14 | #include "common/hw_features_common.h" |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 15 | #include "common/wpa_ctrl.h" |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 16 | #include "hostapd.h" |
| 17 | #include "ap_drv_ops.h" |
| 18 | #include "drivers/driver.h" |
| 19 | #include "dfs.h" |
| 20 | |
| 21 | |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 22 | static int dfs_get_used_n_chans(struct hostapd_iface *iface, int *seg1) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 23 | { |
| 24 | int n_chans = 1; |
| 25 | |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 26 | *seg1 = 0; |
| 27 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 28 | if (iface->conf->ieee80211n && iface->conf->secondary_channel) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 29 | n_chans = 2; |
| 30 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 31 | if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) { |
| 32 | switch (hostapd_get_oper_chwidth(iface->conf)) { |
| 33 | case CHANWIDTH_USE_HT: |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 34 | break; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 35 | case CHANWIDTH_80MHZ: |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 36 | n_chans = 4; |
| 37 | break; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 38 | case CHANWIDTH_160MHZ: |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 39 | n_chans = 8; |
| 40 | break; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 41 | case CHANWIDTH_80P80MHZ: |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 42 | n_chans = 4; |
| 43 | *seg1 = 4; |
| 44 | break; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 45 | default: |
| 46 | break; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return n_chans; |
| 51 | } |
| 52 | |
| 53 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 54 | static int dfs_channel_available(struct hostapd_channel_data *chan, |
| 55 | int skip_radar) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 56 | { |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 57 | /* |
| 58 | * When radar detection happens, CSA is performed. However, there's no |
| 59 | * time for CAC, so radar channels must be skipped when finding a new |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 60 | * channel for CSA, unless they are available for immediate use. |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 61 | */ |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 62 | if (skip_radar && (chan->flag & HOSTAPD_CHAN_RADAR) && |
| 63 | ((chan->flag & HOSTAPD_CHAN_DFS_MASK) != |
| 64 | HOSTAPD_CHAN_DFS_AVAILABLE)) |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 65 | return 0; |
| 66 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 67 | if (chan->flag & HOSTAPD_CHAN_DISABLED) |
| 68 | return 0; |
| 69 | if ((chan->flag & HOSTAPD_CHAN_RADAR) && |
| 70 | ((chan->flag & HOSTAPD_CHAN_DFS_MASK) == |
| 71 | HOSTAPD_CHAN_DFS_UNAVAILABLE)) |
| 72 | return 0; |
| 73 | return 1; |
| 74 | } |
| 75 | |
| 76 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 77 | static int dfs_is_chan_allowed(struct hostapd_channel_data *chan, int n_chans) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 78 | { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 79 | /* |
| 80 | * The tables contain first valid channel number based on channel width. |
| 81 | * We will also choose this first channel as the control one. |
| 82 | */ |
| 83 | int allowed_40[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157, |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 84 | 165, 173, 184, 192 }; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 85 | /* |
| 86 | * VHT80, valid channels based on center frequency: |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 87 | * 42, 58, 106, 122, 138, 155, 171 |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 88 | */ |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 89 | int allowed_80[] = { 36, 52, 100, 116, 132, 149, 165 }; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 90 | /* |
| 91 | * VHT160 valid channels based on center frequency: |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 92 | * 50, 114, 163 |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 93 | */ |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 94 | int allowed_160[] = { 36, 100, 149 }; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 95 | int *allowed = allowed_40; |
| 96 | unsigned int i, allowed_no = 0; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 97 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 98 | switch (n_chans) { |
| 99 | case 2: |
| 100 | allowed = allowed_40; |
| 101 | allowed_no = ARRAY_SIZE(allowed_40); |
| 102 | break; |
| 103 | case 4: |
| 104 | allowed = allowed_80; |
| 105 | allowed_no = ARRAY_SIZE(allowed_80); |
| 106 | break; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 107 | case 8: |
| 108 | allowed = allowed_160; |
| 109 | allowed_no = ARRAY_SIZE(allowed_160); |
| 110 | break; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 111 | default: |
| 112 | wpa_printf(MSG_DEBUG, "Unknown width for %d channels", n_chans); |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | for (i = 0; i < allowed_no; i++) { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 117 | if (chan->chan == allowed[i]) |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 125 | static struct hostapd_channel_data * |
| 126 | dfs_get_chan_data(struct hostapd_hw_modes *mode, int freq, int first_chan_idx) |
| 127 | { |
| 128 | int i; |
| 129 | |
| 130 | for (i = first_chan_idx; i < mode->num_channels; i++) { |
| 131 | if (mode->channels[i].freq == freq) |
| 132 | return &mode->channels[i]; |
| 133 | } |
| 134 | |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 139 | static int dfs_chan_range_available(struct hostapd_hw_modes *mode, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 140 | int first_chan_idx, int num_chans, |
| 141 | int skip_radar) |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 142 | { |
| 143 | struct hostapd_channel_data *first_chan, *chan; |
| 144 | int i; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 145 | u32 bw = num_chan_to_bw(num_chans); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 146 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 147 | if (first_chan_idx + num_chans > mode->num_channels) { |
| 148 | wpa_printf(MSG_DEBUG, |
| 149 | "DFS: some channels in range not defined"); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 150 | return 0; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 151 | } |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 152 | |
| 153 | first_chan = &mode->channels[first_chan_idx]; |
| 154 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 155 | /* hostapd DFS implementation assumes the first channel as primary. |
| 156 | * If it's not allowed to use the first channel as primary, decline the |
| 157 | * whole channel range. */ |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 158 | if (!chan_pri_allowed(first_chan)) { |
| 159 | wpa_printf(MSG_DEBUG, "DFS: primary chanenl not allowed"); |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 160 | return 0; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 161 | } |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 162 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 163 | for (i = 0; i < num_chans; i++) { |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 164 | chan = dfs_get_chan_data(mode, first_chan->freq + i * 20, |
| 165 | first_chan_idx); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 166 | if (!chan) { |
| 167 | wpa_printf(MSG_DEBUG, "DFS: no channel data for %d", |
| 168 | first_chan->freq + i * 20); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 169 | return 0; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 170 | } |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 171 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 172 | /* HT 40 MHz secondary channel availability checked only for |
| 173 | * primary channel */ |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 174 | if (!chan_bw_allowed(chan, bw, 1, !i)) { |
| 175 | wpa_printf(MSG_DEBUG, "DFS: bw now allowed for %d", |
| 176 | first_chan->freq + i * 20); |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 177 | return 0; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 178 | } |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 179 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 180 | if (!dfs_channel_available(chan, skip_radar)) { |
| 181 | wpa_printf(MSG_DEBUG, "DFS: channel not available %d", |
| 182 | first_chan->freq + i * 20); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 183 | return 0; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 184 | } |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 191 | static int is_in_chanlist(struct hostapd_iface *iface, |
| 192 | struct hostapd_channel_data *chan) |
| 193 | { |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 194 | if (!iface->conf->acs_ch_list.num) |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 195 | return 1; |
| 196 | |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 197 | return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan); |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 201 | /* |
| 202 | * The function assumes HT40+ operation. |
| 203 | * Make sure to adjust the following variables after calling this: |
| 204 | * - hapd->secondary_channel |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 205 | * - hapd->vht/he_oper_centr_freq_seg0_idx |
| 206 | * - hapd->vht/he_oper_centr_freq_seg1_idx |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 207 | */ |
| 208 | static int dfs_find_channel(struct hostapd_iface *iface, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 209 | struct hostapd_channel_data **ret_chan, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 210 | int idx, int skip_radar) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 211 | { |
| 212 | struct hostapd_hw_modes *mode; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 213 | struct hostapd_channel_data *chan; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 214 | int i, channel_idx = 0, n_chans, n_chans1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 215 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 216 | mode = iface->current_mode; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 217 | n_chans = dfs_get_used_n_chans(iface, &n_chans1); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 218 | |
| 219 | wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans); |
| 220 | for (i = 0; i < mode->num_channels; i++) { |
| 221 | chan = &mode->channels[i]; |
| 222 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 223 | /* Skip HT40/VHT incompatible channels */ |
| 224 | if (iface->conf->ieee80211n && |
| 225 | iface->conf->secondary_channel && |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 226 | (!dfs_is_chan_allowed(chan, n_chans) || |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 227 | !(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P))) { |
| 228 | wpa_printf(MSG_DEBUG, |
| 229 | "DFS: channel %d (%d) is incompatible", |
| 230 | chan->freq, chan->chan); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 231 | continue; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 232 | } |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 233 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 234 | /* Skip incompatible chandefs */ |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 235 | if (!dfs_chan_range_available(mode, i, n_chans, skip_radar)) { |
| 236 | wpa_printf(MSG_DEBUG, |
| 237 | "DFS: range not available for %d (%d)", |
| 238 | chan->freq, chan->chan); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 239 | continue; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 240 | } |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 241 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 242 | if (!is_in_chanlist(iface, chan)) { |
| 243 | wpa_printf(MSG_DEBUG, |
| 244 | "DFS: channel %d (%d) not in chanlist", |
| 245 | chan->freq, chan->chan); |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 246 | continue; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 247 | } |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 248 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame^] | 249 | if (chan->max_tx_power < iface->conf->min_tx_power) |
| 250 | continue; |
| 251 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 252 | if (ret_chan && idx == channel_idx) { |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 253 | wpa_printf(MSG_DEBUG, "Selected channel %d (%d)", |
| 254 | chan->freq, chan->chan); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 255 | *ret_chan = chan; |
| 256 | return idx; |
| 257 | } |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 258 | wpa_printf(MSG_DEBUG, "Adding channel %d (%d)", |
| 259 | chan->freq, chan->chan); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 260 | channel_idx++; |
| 261 | } |
| 262 | return channel_idx; |
| 263 | } |
| 264 | |
| 265 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 266 | static void dfs_adjust_center_freq(struct hostapd_iface *iface, |
| 267 | struct hostapd_channel_data *chan, |
| 268 | int secondary_channel, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 269 | int sec_chan_idx_80p80, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 270 | u8 *oper_centr_freq_seg0_idx, |
| 271 | u8 *oper_centr_freq_seg1_idx) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 272 | { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 273 | if (!iface->conf->ieee80211ac && !iface->conf->ieee80211ax) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 274 | return; |
| 275 | |
| 276 | if (!chan) |
| 277 | return; |
| 278 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 279 | *oper_centr_freq_seg1_idx = 0; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 280 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 281 | switch (hostapd_get_oper_chwidth(iface->conf)) { |
| 282 | case CHANWIDTH_USE_HT: |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 283 | if (secondary_channel == 1) |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 284 | *oper_centr_freq_seg0_idx = chan->chan + 2; |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 285 | else if (secondary_channel == -1) |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 286 | *oper_centr_freq_seg0_idx = chan->chan - 2; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 287 | else |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 288 | *oper_centr_freq_seg0_idx = chan->chan; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 289 | break; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 290 | case CHANWIDTH_80MHZ: |
| 291 | *oper_centr_freq_seg0_idx = chan->chan + 6; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 292 | break; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 293 | case CHANWIDTH_160MHZ: |
| 294 | *oper_centr_freq_seg0_idx = chan->chan + 14; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 295 | break; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 296 | case CHANWIDTH_80P80MHZ: |
| 297 | *oper_centr_freq_seg0_idx = chan->chan + 6; |
| 298 | *oper_centr_freq_seg1_idx = sec_chan_idx_80p80 + 6; |
| 299 | break; |
| 300 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 301 | default: |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 302 | wpa_printf(MSG_INFO, |
| 303 | "DFS: Unsupported channel width configuration"); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 304 | *oper_centr_freq_seg0_idx = 0; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 305 | break; |
| 306 | } |
| 307 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 308 | wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d, %d", |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 309 | *oper_centr_freq_seg0_idx, |
| 310 | *oper_centr_freq_seg1_idx); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | |
| 314 | /* Return start channel idx we will use for mode->channels[idx] */ |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 315 | static int dfs_get_start_chan_idx(struct hostapd_iface *iface, int *seg1_start) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 316 | { |
| 317 | struct hostapd_hw_modes *mode; |
| 318 | struct hostapd_channel_data *chan; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 319 | int channel_no = iface->conf->channel; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 320 | int res = -1, i; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 321 | int chan_seg1 = -1; |
| 322 | |
| 323 | *seg1_start = -1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 324 | |
| 325 | /* HT40- */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 326 | if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 327 | channel_no -= 4; |
| 328 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 329 | /* VHT/HE */ |
| 330 | if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) { |
| 331 | switch (hostapd_get_oper_chwidth(iface->conf)) { |
| 332 | case CHANWIDTH_USE_HT: |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 333 | break; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 334 | case CHANWIDTH_80MHZ: |
| 335 | channel_no = hostapd_get_oper_centr_freq_seg0_idx( |
| 336 | iface->conf) - 6; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 337 | break; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 338 | case CHANWIDTH_160MHZ: |
| 339 | channel_no = hostapd_get_oper_centr_freq_seg0_idx( |
| 340 | iface->conf) - 14; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 341 | break; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 342 | case CHANWIDTH_80P80MHZ: |
| 343 | channel_no = hostapd_get_oper_centr_freq_seg0_idx( |
| 344 | iface->conf) - 6; |
| 345 | chan_seg1 = hostapd_get_oper_centr_freq_seg1_idx( |
| 346 | iface->conf) - 6; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 347 | break; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 348 | default: |
| 349 | wpa_printf(MSG_INFO, |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 350 | "DFS only VHT20/40/80/160/80+80 is supported now"); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 351 | channel_no = -1; |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /* Get idx */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 357 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 358 | for (i = 0; i < mode->num_channels; i++) { |
| 359 | chan = &mode->channels[i]; |
| 360 | if (chan->chan == channel_no) { |
| 361 | res = i; |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 366 | if (res != -1 && chan_seg1 > -1) { |
| 367 | int found = 0; |
| 368 | |
| 369 | /* Get idx for seg1 */ |
| 370 | mode = iface->current_mode; |
| 371 | for (i = 0; i < mode->num_channels; i++) { |
| 372 | chan = &mode->channels[i]; |
| 373 | if (chan->chan == chan_seg1) { |
| 374 | *seg1_start = i; |
| 375 | found = 1; |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | if (!found) |
| 380 | res = -1; |
| 381 | } |
| 382 | |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 383 | if (res == -1) { |
| 384 | wpa_printf(MSG_DEBUG, |
| 385 | "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d", |
| 386 | mode->num_channels, channel_no, iface->conf->channel, |
| 387 | iface->conf->ieee80211n, |
| 388 | iface->conf->secondary_channel, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 389 | hostapd_get_oper_chwidth(iface->conf)); |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 390 | |
| 391 | for (i = 0; i < mode->num_channels; i++) { |
| 392 | wpa_printf(MSG_DEBUG, "Available channel: %d", |
| 393 | mode->channels[i].chan); |
| 394 | } |
| 395 | } |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 396 | |
| 397 | return res; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | /* At least one channel have radar flag */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 402 | static int dfs_check_chans_radar(struct hostapd_iface *iface, |
| 403 | int start_chan_idx, int n_chans) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 404 | { |
| 405 | struct hostapd_channel_data *channel; |
| 406 | struct hostapd_hw_modes *mode; |
| 407 | int i, res = 0; |
| 408 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 409 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 410 | |
| 411 | for (i = 0; i < n_chans; i++) { |
| 412 | channel = &mode->channels[start_chan_idx + i]; |
| 413 | if (channel->flag & HOSTAPD_CHAN_RADAR) |
| 414 | res++; |
| 415 | } |
| 416 | |
| 417 | return res; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | /* All channels available */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 422 | static int dfs_check_chans_available(struct hostapd_iface *iface, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 423 | int start_chan_idx, int n_chans) |
| 424 | { |
| 425 | struct hostapd_channel_data *channel; |
| 426 | struct hostapd_hw_modes *mode; |
| 427 | int i; |
| 428 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 429 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 430 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 431 | for (i = 0; i < n_chans; i++) { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 432 | channel = &mode->channels[start_chan_idx + i]; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 433 | |
| 434 | if (channel->flag & HOSTAPD_CHAN_DISABLED) |
| 435 | break; |
| 436 | |
| 437 | if (!(channel->flag & HOSTAPD_CHAN_RADAR)) |
| 438 | continue; |
| 439 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 440 | if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) != |
| 441 | HOSTAPD_CHAN_DFS_AVAILABLE) |
| 442 | break; |
| 443 | } |
| 444 | |
| 445 | return i == n_chans; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | /* At least one channel unavailable */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 450 | static int dfs_check_chans_unavailable(struct hostapd_iface *iface, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 451 | int start_chan_idx, |
| 452 | int n_chans) |
| 453 | { |
| 454 | struct hostapd_channel_data *channel; |
| 455 | struct hostapd_hw_modes *mode; |
| 456 | int i, res = 0; |
| 457 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 458 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 459 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 460 | for (i = 0; i < n_chans; i++) { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 461 | channel = &mode->channels[start_chan_idx + i]; |
| 462 | if (channel->flag & HOSTAPD_CHAN_DISABLED) |
| 463 | res++; |
| 464 | if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) == |
| 465 | HOSTAPD_CHAN_DFS_UNAVAILABLE) |
| 466 | res++; |
| 467 | } |
| 468 | |
| 469 | return res; |
| 470 | } |
| 471 | |
| 472 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 473 | static struct hostapd_channel_data * |
| 474 | dfs_get_valid_channel(struct hostapd_iface *iface, |
| 475 | int *secondary_channel, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 476 | u8 *oper_centr_freq_seg0_idx, |
| 477 | u8 *oper_centr_freq_seg1_idx, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 478 | int skip_radar) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 479 | { |
| 480 | struct hostapd_hw_modes *mode; |
| 481 | struct hostapd_channel_data *chan = NULL; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 482 | struct hostapd_channel_data *chan2 = NULL; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 483 | int num_available_chandefs; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 484 | int chan_idx, chan_idx2; |
| 485 | int sec_chan_idx_80p80 = -1; |
| 486 | int i; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 487 | u32 _rand; |
| 488 | |
| 489 | wpa_printf(MSG_DEBUG, "DFS: Selecting random channel"); |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 490 | *secondary_channel = 0; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 491 | *oper_centr_freq_seg0_idx = 0; |
| 492 | *oper_centr_freq_seg1_idx = 0; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 493 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 494 | if (iface->current_mode == NULL) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 495 | return NULL; |
| 496 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 497 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 498 | if (mode->mode != HOSTAPD_MODE_IEEE80211A) |
| 499 | return NULL; |
| 500 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 501 | /* Get the count first */ |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 502 | num_available_chandefs = dfs_find_channel(iface, NULL, 0, skip_radar); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 503 | wpa_printf(MSG_DEBUG, "DFS: num_available_chandefs=%d", |
| 504 | num_available_chandefs); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 505 | if (num_available_chandefs == 0) |
| 506 | return NULL; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 507 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 508 | if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0) |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 509 | return NULL; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 510 | chan_idx = _rand % num_available_chandefs; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 511 | dfs_find_channel(iface, &chan, chan_idx, skip_radar); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 512 | if (!chan) { |
| 513 | wpa_printf(MSG_DEBUG, "DFS: no random channel found"); |
| 514 | return NULL; |
| 515 | } |
| 516 | wpa_printf(MSG_DEBUG, "DFS: got random channel %d (%d)", |
| 517 | chan->freq, chan->chan); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 518 | |
| 519 | /* dfs_find_channel() calculations assume HT40+ */ |
| 520 | if (iface->conf->secondary_channel) |
| 521 | *secondary_channel = 1; |
| 522 | else |
| 523 | *secondary_channel = 0; |
| 524 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 525 | /* Get secondary channel for HT80P80 */ |
| 526 | if (hostapd_get_oper_chwidth(iface->conf) == CHANWIDTH_80P80MHZ) { |
| 527 | if (num_available_chandefs <= 1) { |
| 528 | wpa_printf(MSG_ERROR, |
| 529 | "only 1 valid chan, can't support 80+80"); |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Loop all channels except channel1 to find a valid channel2 |
| 535 | * that is not adjacent to channel1. |
| 536 | */ |
| 537 | for (i = 0; i < num_available_chandefs - 1; i++) { |
| 538 | /* start from chan_idx + 1, end when chan_idx - 1 */ |
| 539 | chan_idx2 = (chan_idx + 1 + i) % num_available_chandefs; |
| 540 | dfs_find_channel(iface, &chan2, chan_idx2, skip_radar); |
| 541 | if (chan2 && abs(chan2->chan - chan->chan) > 12) { |
| 542 | /* two channels are not adjacent */ |
| 543 | sec_chan_idx_80p80 = chan2->chan; |
| 544 | wpa_printf(MSG_DEBUG, |
| 545 | "DFS: got second chan: %d (%d)", |
| 546 | chan2->freq, chan2->chan); |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /* Check if we got a valid secondary channel which is not |
| 552 | * adjacent to the first channel. |
| 553 | */ |
| 554 | if (sec_chan_idx_80p80 == -1) { |
| 555 | wpa_printf(MSG_INFO, |
| 556 | "DFS: failed to get chan2 for 80+80"); |
| 557 | return NULL; |
| 558 | } |
| 559 | } |
| 560 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 561 | dfs_adjust_center_freq(iface, chan, |
| 562 | *secondary_channel, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 563 | sec_chan_idx_80p80, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 564 | oper_centr_freq_seg0_idx, |
| 565 | oper_centr_freq_seg1_idx); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 566 | |
| 567 | return chan; |
| 568 | } |
| 569 | |
| 570 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 571 | static int set_dfs_state_freq(struct hostapd_iface *iface, int freq, u32 state) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 572 | { |
| 573 | struct hostapd_hw_modes *mode; |
| 574 | struct hostapd_channel_data *chan = NULL; |
| 575 | int i; |
| 576 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 577 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 578 | if (mode == NULL) |
| 579 | return 0; |
| 580 | |
| 581 | wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 582 | for (i = 0; i < iface->current_mode->num_channels; i++) { |
| 583 | chan = &iface->current_mode->channels[i]; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 584 | if (chan->freq == freq) { |
| 585 | if (chan->flag & HOSTAPD_CHAN_RADAR) { |
| 586 | chan->flag &= ~HOSTAPD_CHAN_DFS_MASK; |
| 587 | chan->flag |= state; |
| 588 | return 1; /* Channel found */ |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq); |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 597 | static int set_dfs_state(struct hostapd_iface *iface, int freq, int ht_enabled, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 598 | int chan_offset, int chan_width, int cf1, |
| 599 | int cf2, u32 state) |
| 600 | { |
| 601 | int n_chans = 1, i; |
| 602 | struct hostapd_hw_modes *mode; |
| 603 | int frequency = freq; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 604 | int frequency2 = 0; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 605 | int ret = 0; |
| 606 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 607 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 608 | if (mode == NULL) |
| 609 | return 0; |
| 610 | |
| 611 | if (mode->mode != HOSTAPD_MODE_IEEE80211A) { |
| 612 | wpa_printf(MSG_WARNING, "current_mode != IEEE80211A"); |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | /* Seems cf1 and chan_width is enough here */ |
| 617 | switch (chan_width) { |
| 618 | case CHAN_WIDTH_20_NOHT: |
| 619 | case CHAN_WIDTH_20: |
| 620 | n_chans = 1; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 621 | if (frequency == 0) |
| 622 | frequency = cf1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 623 | break; |
| 624 | case CHAN_WIDTH_40: |
| 625 | n_chans = 2; |
| 626 | frequency = cf1 - 10; |
| 627 | break; |
| 628 | case CHAN_WIDTH_80: |
| 629 | n_chans = 4; |
| 630 | frequency = cf1 - 30; |
| 631 | break; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 632 | case CHAN_WIDTH_80P80: |
| 633 | n_chans = 4; |
| 634 | frequency = cf1 - 30; |
| 635 | frequency2 = cf2 - 30; |
| 636 | break; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 637 | case CHAN_WIDTH_160: |
| 638 | n_chans = 8; |
| 639 | frequency = cf1 - 70; |
| 640 | break; |
| 641 | default: |
| 642 | wpa_printf(MSG_INFO, "DFS chan_width %d not supported", |
| 643 | chan_width); |
| 644 | break; |
| 645 | } |
| 646 | |
| 647 | wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency, |
| 648 | n_chans); |
| 649 | for (i = 0; i < n_chans; i++) { |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 650 | ret += set_dfs_state_freq(iface, frequency, state); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 651 | frequency = frequency + 20; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 652 | |
| 653 | if (chan_width == CHAN_WIDTH_80P80) { |
| 654 | ret += set_dfs_state_freq(iface, frequency2, state); |
| 655 | frequency2 = frequency2 + 20; |
| 656 | } |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | return ret; |
| 660 | } |
| 661 | |
| 662 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 663 | static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 664 | int chan_width, int cf1, int cf2) |
| 665 | { |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 666 | int start_chan_idx, start_chan_idx1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 667 | struct hostapd_hw_modes *mode; |
| 668 | struct hostapd_channel_data *chan; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 669 | int n_chans, n_chans1, i, j, frequency = freq, radar_n_chans = 1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 670 | u8 radar_chan; |
| 671 | int res = 0; |
| 672 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 673 | /* Our configuration */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 674 | mode = iface->current_mode; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 675 | start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1); |
| 676 | n_chans = dfs_get_used_n_chans(iface, &n_chans1); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 677 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 678 | /* Check we are on DFS channel(s) */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 679 | if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans)) |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 680 | return 0; |
| 681 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 682 | /* Reported via radar event */ |
| 683 | switch (chan_width) { |
| 684 | case CHAN_WIDTH_20_NOHT: |
| 685 | case CHAN_WIDTH_20: |
| 686 | radar_n_chans = 1; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 687 | if (frequency == 0) |
| 688 | frequency = cf1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 689 | break; |
| 690 | case CHAN_WIDTH_40: |
| 691 | radar_n_chans = 2; |
| 692 | frequency = cf1 - 10; |
| 693 | break; |
| 694 | case CHAN_WIDTH_80: |
| 695 | radar_n_chans = 4; |
| 696 | frequency = cf1 - 30; |
| 697 | break; |
| 698 | case CHAN_WIDTH_160: |
| 699 | radar_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 | ieee80211_freq_to_chan(frequency, &radar_chan); |
| 709 | |
| 710 | for (i = 0; i < n_chans; i++) { |
| 711 | chan = &mode->channels[start_chan_idx + i]; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 712 | if (!(chan->flag & HOSTAPD_CHAN_RADAR)) |
| 713 | continue; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 714 | for (j = 0; j < radar_n_chans; j++) { |
| 715 | wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d", |
| 716 | chan->chan, radar_chan + j * 4); |
| 717 | if (chan->chan == radar_chan + j * 4) |
| 718 | res++; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | wpa_printf(MSG_DEBUG, "overlapped: %d", res); |
| 723 | |
| 724 | return res; |
| 725 | } |
| 726 | |
| 727 | |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 728 | static unsigned int dfs_get_cac_time(struct hostapd_iface *iface, |
| 729 | int start_chan_idx, int n_chans) |
| 730 | { |
| 731 | struct hostapd_channel_data *channel; |
| 732 | struct hostapd_hw_modes *mode; |
| 733 | int i; |
| 734 | unsigned int cac_time_ms = 0; |
| 735 | |
| 736 | mode = iface->current_mode; |
| 737 | |
| 738 | for (i = 0; i < n_chans; i++) { |
| 739 | channel = &mode->channels[start_chan_idx + i]; |
| 740 | if (!(channel->flag & HOSTAPD_CHAN_RADAR)) |
| 741 | continue; |
| 742 | if (channel->dfs_cac_ms > cac_time_ms) |
| 743 | cac_time_ms = channel->dfs_cac_ms; |
| 744 | } |
| 745 | |
| 746 | return cac_time_ms; |
| 747 | } |
| 748 | |
| 749 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 750 | /* |
| 751 | * Main DFS handler |
| 752 | * 1 - continue channel/ap setup |
| 753 | * 0 - channel/ap setup will be continued after CAC |
| 754 | * -1 - hit critical error |
| 755 | */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 756 | int hostapd_handle_dfs(struct hostapd_iface *iface) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 757 | { |
| 758 | struct hostapd_channel_data *channel; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 759 | int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 760 | int skip_radar = 0; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 761 | |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 762 | if (is_6ghz_freq(iface->freq)) |
| 763 | return 1; |
| 764 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 765 | if (!iface->current_mode) { |
| 766 | /* |
| 767 | * This can happen with drivers that do not provide mode |
| 768 | * information and as such, cannot really use hostapd for DFS. |
| 769 | */ |
| 770 | wpa_printf(MSG_DEBUG, |
| 771 | "DFS: No current_mode information - assume no need to perform DFS operations by hostapd"); |
| 772 | return 1; |
| 773 | } |
| 774 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 775 | iface->cac_started = 0; |
| 776 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 777 | do { |
| 778 | /* Get start (first) channel for current configuration */ |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 779 | start_chan_idx = dfs_get_start_chan_idx(iface, |
| 780 | &start_chan_idx1); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 781 | if (start_chan_idx == -1) |
| 782 | return -1; |
| 783 | |
| 784 | /* Get number of used channels, depend on width */ |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 785 | n_chans = dfs_get_used_n_chans(iface, &n_chans1); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 786 | |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 787 | /* Setup CAC time */ |
| 788 | iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx, |
| 789 | n_chans); |
| 790 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 791 | /* Check if any of configured channels require DFS */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 792 | res = dfs_check_chans_radar(iface, start_chan_idx, n_chans); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 793 | wpa_printf(MSG_DEBUG, |
| 794 | "DFS %d channels required radar detection", |
| 795 | res); |
| 796 | if (!res) |
| 797 | return 1; |
| 798 | |
| 799 | /* Check if all channels are DFS available */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 800 | res = dfs_check_chans_available(iface, start_chan_idx, n_chans); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 801 | wpa_printf(MSG_DEBUG, |
| 802 | "DFS all channels available, (SKIP CAC): %s", |
| 803 | res ? "yes" : "no"); |
| 804 | if (res) |
| 805 | return 1; |
| 806 | |
| 807 | /* Check if any of configured channels is unavailable */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 808 | res = dfs_check_chans_unavailable(iface, start_chan_idx, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 809 | n_chans); |
| 810 | wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s", |
| 811 | res, res ? "yes": "no"); |
| 812 | if (res) { |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 813 | int sec = 0; |
| 814 | u8 cf1 = 0, cf2 = 0; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 815 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 816 | channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2, |
| 817 | skip_radar); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 818 | if (!channel) { |
| 819 | wpa_printf(MSG_ERROR, "could not get valid channel"); |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 820 | hostapd_set_state(iface, HAPD_IFACE_DFS); |
| 821 | return 0; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 822 | } |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 823 | |
| 824 | iface->freq = channel->freq; |
| 825 | iface->conf->channel = channel->chan; |
| 826 | iface->conf->secondary_channel = sec; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 827 | hostapd_set_oper_centr_freq_seg0_idx(iface->conf, cf1); |
| 828 | hostapd_set_oper_centr_freq_seg1_idx(iface->conf, cf2); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 829 | } |
| 830 | } while (res); |
| 831 | |
| 832 | /* Finally start CAC */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 833 | hostapd_set_state(iface, HAPD_IFACE_DFS); |
| 834 | wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz", iface->freq); |
| 835 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 836 | "freq=%d chan=%d sec_chan=%d, width=%d, seg0=%d, seg1=%d, cac_time=%ds", |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 837 | iface->freq, |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 838 | iface->conf->channel, iface->conf->secondary_channel, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 839 | hostapd_get_oper_chwidth(iface->conf), |
| 840 | hostapd_get_oper_centr_freq_seg0_idx(iface->conf), |
| 841 | hostapd_get_oper_centr_freq_seg1_idx(iface->conf), |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 842 | iface->dfs_cac_ms / 1000); |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 843 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 844 | res = hostapd_start_dfs_cac( |
| 845 | iface, iface->conf->hw_mode, iface->freq, iface->conf->channel, |
| 846 | iface->conf->ieee80211n, iface->conf->ieee80211ac, |
| 847 | iface->conf->ieee80211ax, |
| 848 | iface->conf->secondary_channel, |
| 849 | hostapd_get_oper_chwidth(iface->conf), |
| 850 | hostapd_get_oper_centr_freq_seg0_idx(iface->conf), |
| 851 | hostapd_get_oper_centr_freq_seg1_idx(iface->conf)); |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 852 | |
| 853 | if (res) { |
| 854 | wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 855 | return -1; |
| 856 | } |
| 857 | |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 862 | int hostapd_is_dfs_chan_available(struct hostapd_iface *iface) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 863 | { |
| 864 | int n_chans, n_chans1, start_chan_idx, start_chan_idx1; |
| 865 | |
| 866 | /* Get the start (first) channel for current configuration */ |
| 867 | start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1); |
| 868 | if (start_chan_idx < 0) |
| 869 | return 0; |
| 870 | |
| 871 | /* Get the number of used channels, depending on width */ |
| 872 | n_chans = dfs_get_used_n_chans(iface, &n_chans1); |
| 873 | |
| 874 | /* Check if all channels are DFS available */ |
| 875 | return dfs_check_chans_available(iface, start_chan_idx, n_chans); |
| 876 | } |
| 877 | |
| 878 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 879 | int hostapd_dfs_complete_cac(struct hostapd_iface *iface, int success, int freq, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 880 | int ht_enabled, int chan_offset, int chan_width, |
| 881 | int cf1, int cf2) |
| 882 | { |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 883 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED |
| 884 | "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d", |
| 885 | success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2); |
| 886 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 887 | if (success) { |
| 888 | /* Complete iface/ap configuration */ |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 889 | if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) { |
| 890 | /* Complete AP configuration for the first bring up. */ |
| 891 | if (iface->state != HAPD_IFACE_ENABLED) |
| 892 | hostapd_setup_interface_complete(iface, 0); |
| 893 | else |
| 894 | iface->cac_started = 0; |
| 895 | } else { |
| 896 | set_dfs_state(iface, freq, ht_enabled, chan_offset, |
| 897 | chan_width, cf1, cf2, |
| 898 | HOSTAPD_CHAN_DFS_AVAILABLE); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 899 | /* |
| 900 | * Just mark the channel available when CAC completion |
| 901 | * event is received in enabled state. CAC result could |
| 902 | * have been propagated from another radio having the |
| 903 | * same regulatory configuration. When CAC completion is |
| 904 | * received during non-HAPD_IFACE_ENABLED state, make |
| 905 | * sure the configured channel is available because this |
| 906 | * CAC completion event could have been propagated from |
| 907 | * another radio. |
| 908 | */ |
| 909 | if (iface->state != HAPD_IFACE_ENABLED && |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 910 | hostapd_is_dfs_chan_available(iface)) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 911 | hostapd_setup_interface_complete(iface, 0); |
| 912 | iface->cac_started = 0; |
| 913 | } |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 914 | } |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | return 0; |
| 918 | } |
| 919 | |
| 920 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 921 | int hostapd_dfs_pre_cac_expired(struct hostapd_iface *iface, int freq, |
| 922 | int ht_enabled, int chan_offset, int chan_width, |
| 923 | int cf1, int cf2) |
| 924 | { |
| 925 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_PRE_CAC_EXPIRED |
| 926 | "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d", |
| 927 | freq, ht_enabled, chan_offset, chan_width, cf1, cf2); |
| 928 | |
| 929 | /* Proceed only if DFS is not offloaded to the driver */ |
| 930 | if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) |
| 931 | return 0; |
| 932 | |
| 933 | set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width, |
| 934 | cf1, cf2, HOSTAPD_CHAN_DFS_USABLE); |
| 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 940 | static struct hostapd_channel_data * |
| 941 | dfs_downgrade_bandwidth(struct hostapd_iface *iface, int *secondary_channel, |
| 942 | u8 *oper_centr_freq_seg0_idx, |
| 943 | u8 *oper_centr_freq_seg1_idx, int *skip_radar) |
| 944 | { |
| 945 | struct hostapd_channel_data *channel; |
| 946 | |
| 947 | for (;;) { |
| 948 | channel = dfs_get_valid_channel(iface, secondary_channel, |
| 949 | oper_centr_freq_seg0_idx, |
| 950 | oper_centr_freq_seg1_idx, |
| 951 | *skip_radar); |
| 952 | if (channel) { |
| 953 | wpa_printf(MSG_DEBUG, "DFS: Selected channel: %d", |
| 954 | channel->chan); |
| 955 | return channel; |
| 956 | } |
| 957 | |
| 958 | if (*skip_radar) { |
| 959 | *skip_radar = 0; |
| 960 | } else { |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 961 | int oper_chwidth; |
| 962 | |
| 963 | oper_chwidth = hostapd_get_oper_chwidth(iface->conf); |
| 964 | if (oper_chwidth == CHANWIDTH_USE_HT) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 965 | break; |
| 966 | *skip_radar = 1; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 967 | hostapd_set_oper_chwidth(iface->conf, oper_chwidth - 1); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | |
| 971 | wpa_printf(MSG_INFO, |
| 972 | "%s: no DFS channels left, waiting for NOP to finish", |
| 973 | __func__); |
| 974 | return NULL; |
| 975 | } |
| 976 | |
| 977 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 978 | static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 979 | { |
| 980 | struct hostapd_channel_data *channel; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 981 | int secondary_channel; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 982 | u8 oper_centr_freq_seg0_idx = 0; |
| 983 | u8 oper_centr_freq_seg1_idx = 0; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 984 | int skip_radar = 0; |
| 985 | int err = 1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 986 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 987 | /* Radar detected during active CAC */ |
| 988 | iface->cac_started = 0; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 989 | channel = dfs_get_valid_channel(iface, &secondary_channel, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 990 | &oper_centr_freq_seg0_idx, |
| 991 | &oper_centr_freq_seg1_idx, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 992 | skip_radar); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 993 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 994 | if (!channel) { |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 995 | channel = dfs_downgrade_bandwidth(iface, &secondary_channel, |
| 996 | &oper_centr_freq_seg0_idx, |
| 997 | &oper_centr_freq_seg1_idx, |
| 998 | &skip_radar); |
| 999 | if (!channel) { |
| 1000 | wpa_printf(MSG_ERROR, "No valid channel available"); |
| 1001 | return err; |
| 1002 | } |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d", |
| 1006 | channel->chan); |
| 1007 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL |
| 1008 | "freq=%d chan=%d sec_chan=%d", channel->freq, |
| 1009 | channel->chan, secondary_channel); |
| 1010 | |
| 1011 | iface->freq = channel->freq; |
| 1012 | iface->conf->channel = channel->chan; |
| 1013 | iface->conf->secondary_channel = secondary_channel; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1014 | hostapd_set_oper_centr_freq_seg0_idx(iface->conf, |
| 1015 | oper_centr_freq_seg0_idx); |
| 1016 | hostapd_set_oper_centr_freq_seg1_idx(iface->conf, |
| 1017 | oper_centr_freq_seg1_idx); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1018 | err = 0; |
| 1019 | |
| 1020 | hostapd_setup_interface_complete(iface, err); |
| 1021 | return err; |
| 1022 | } |
| 1023 | |
| 1024 | |
| 1025 | static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface) |
| 1026 | { |
| 1027 | struct hostapd_channel_data *channel; |
| 1028 | int secondary_channel; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1029 | u8 oper_centr_freq_seg0_idx; |
| 1030 | u8 oper_centr_freq_seg1_idx; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1031 | u8 new_vht_oper_chwidth; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1032 | int skip_radar = 1; |
| 1033 | struct csa_settings csa_settings; |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 1034 | unsigned int i; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1035 | int err = 1; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1036 | struct hostapd_hw_modes *cmode = iface->current_mode; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1037 | u8 current_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf); |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1038 | int ieee80211_mode = IEEE80211_MODE_AP; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1039 | |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 1040 | wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)", |
| 1041 | __func__, iface->cac_started ? "yes" : "no", |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 1042 | hostapd_csa_in_progress(iface) ? "yes" : "no"); |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 1043 | |
| 1044 | /* Check if CSA in progress */ |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 1045 | if (hostapd_csa_in_progress(iface)) |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 1046 | return 0; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1047 | |
| 1048 | /* Check if active CAC */ |
| 1049 | if (iface->cac_started) |
| 1050 | return hostapd_dfs_start_channel_switch_cac(iface); |
| 1051 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1052 | /* |
| 1053 | * Allow selection of DFS channel in ETSI to comply with |
| 1054 | * uniform spreading. |
| 1055 | */ |
| 1056 | if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI) |
| 1057 | skip_radar = 0; |
| 1058 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1059 | /* Perform channel switch/CSA */ |
| 1060 | channel = dfs_get_valid_channel(iface, &secondary_channel, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1061 | &oper_centr_freq_seg0_idx, |
| 1062 | &oper_centr_freq_seg1_idx, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1063 | skip_radar); |
| 1064 | |
| 1065 | if (!channel) { |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 1066 | /* |
| 1067 | * If there is no channel to switch immediately to, check if |
| 1068 | * there is another channel where we can switch even if it |
| 1069 | * requires to perform a CAC first. |
| 1070 | */ |
| 1071 | skip_radar = 0; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1072 | channel = dfs_downgrade_bandwidth(iface, &secondary_channel, |
| 1073 | &oper_centr_freq_seg0_idx, |
| 1074 | &oper_centr_freq_seg1_idx, |
| 1075 | &skip_radar); |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1076 | if (!channel) { |
| 1077 | /* |
| 1078 | * Toggle interface state to enter DFS state |
| 1079 | * until NOP is finished. |
| 1080 | */ |
| 1081 | hostapd_disable_iface(iface); |
| 1082 | hostapd_enable_iface(iface); |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1086 | if (!skip_radar) { |
| 1087 | iface->freq = channel->freq; |
| 1088 | iface->conf->channel = channel->chan; |
| 1089 | iface->conf->secondary_channel = secondary_channel; |
| 1090 | hostapd_set_oper_centr_freq_seg0_idx( |
| 1091 | iface->conf, oper_centr_freq_seg0_idx); |
| 1092 | hostapd_set_oper_centr_freq_seg1_idx( |
| 1093 | iface->conf, oper_centr_freq_seg1_idx); |
| 1094 | |
| 1095 | hostapd_disable_iface(iface); |
| 1096 | hostapd_enable_iface(iface); |
| 1097 | return 0; |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 1098 | } |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d", |
| 1102 | channel->chan); |
| 1103 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL |
| 1104 | "freq=%d chan=%d sec_chan=%d", channel->freq, |
| 1105 | channel->chan, secondary_channel); |
| 1106 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1107 | new_vht_oper_chwidth = hostapd_get_oper_chwidth(iface->conf); |
| 1108 | hostapd_set_oper_chwidth(iface->conf, current_vht_oper_chwidth); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1109 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1110 | /* Setup CSA request */ |
| 1111 | os_memset(&csa_settings, 0, sizeof(csa_settings)); |
| 1112 | csa_settings.cs_count = 5; |
| 1113 | csa_settings.block_tx = 1; |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1114 | #ifdef CONFIG_MESH |
| 1115 | if (iface->mconf) |
| 1116 | ieee80211_mode = IEEE80211_MODE_MESH; |
| 1117 | #endif /* CONFIG_MESH */ |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1118 | err = hostapd_set_freq_params(&csa_settings.freq_params, |
| 1119 | iface->conf->hw_mode, |
| 1120 | channel->freq, |
| 1121 | channel->chan, |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1122 | iface->conf->enable_edmg, |
| 1123 | iface->conf->edmg_channel, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1124 | iface->conf->ieee80211n, |
| 1125 | iface->conf->ieee80211ac, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1126 | iface->conf->ieee80211ax, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1127 | secondary_channel, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1128 | new_vht_oper_chwidth, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1129 | oper_centr_freq_seg0_idx, |
| 1130 | oper_centr_freq_seg1_idx, |
| 1131 | cmode->vht_capab, |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1132 | &cmode->he_capab[ieee80211_mode]); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1133 | |
| 1134 | if (err) { |
| 1135 | wpa_printf(MSG_ERROR, "DFS failed to calculate CSA freq params"); |
| 1136 | hostapd_disable_iface(iface); |
| 1137 | return err; |
| 1138 | } |
| 1139 | |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 1140 | for (i = 0; i < iface->num_bss; i++) { |
| 1141 | err = hostapd_switch_channel(iface->bss[i], &csa_settings); |
| 1142 | if (err) |
| 1143 | break; |
| 1144 | } |
| 1145 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1146 | if (err) { |
| 1147 | wpa_printf(MSG_WARNING, "DFS failed to schedule CSA (%d) - trying fallback", |
| 1148 | err); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1149 | iface->freq = channel->freq; |
| 1150 | iface->conf->channel = channel->chan; |
| 1151 | iface->conf->secondary_channel = secondary_channel; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1152 | hostapd_set_oper_chwidth(iface->conf, new_vht_oper_chwidth); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1153 | hostapd_set_oper_centr_freq_seg0_idx(iface->conf, |
| 1154 | oper_centr_freq_seg0_idx); |
| 1155 | hostapd_set_oper_centr_freq_seg1_idx(iface->conf, |
| 1156 | oper_centr_freq_seg1_idx); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1157 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1158 | hostapd_disable_iface(iface); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1159 | hostapd_enable_iface(iface); |
| 1160 | return 0; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1163 | /* Channel configuration will be updated once CSA completes and |
| 1164 | * ch_switch_notify event is received */ |
| 1165 | |
| 1166 | wpa_printf(MSG_DEBUG, "DFS waiting channel switch event"); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1167 | return 0; |
| 1168 | } |
| 1169 | |
| 1170 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1171 | int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1172 | int ht_enabled, int chan_offset, int chan_width, |
| 1173 | int cf1, int cf2) |
| 1174 | { |
| 1175 | int res; |
| 1176 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1177 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED |
| 1178 | "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d", |
| 1179 | freq, ht_enabled, chan_offset, chan_width, cf1, cf2); |
| 1180 | |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1181 | /* Proceed only if DFS is not offloaded to the driver */ |
| 1182 | if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) |
| 1183 | return 0; |
| 1184 | |
| 1185 | if (!iface->conf->ieee80211h) |
| 1186 | return 0; |
| 1187 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1188 | /* mark radar frequency as invalid */ |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1189 | res = set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width, |
| 1190 | cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE); |
| 1191 | if (!res) |
| 1192 | return 0; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1193 | |
| 1194 | /* Skip if reported radar event not overlapped our channels */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1195 | res = dfs_are_channels_overlapped(iface, freq, chan_width, cf1, cf2); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1196 | if (!res) |
| 1197 | return 0; |
| 1198 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1199 | /* radar detected while operating, switch the channel. */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1200 | res = hostapd_dfs_start_channel_switch(iface); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1201 | |
| 1202 | return res; |
| 1203 | } |
| 1204 | |
| 1205 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1206 | int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1207 | int ht_enabled, int chan_offset, int chan_width, |
| 1208 | int cf1, int cf2) |
| 1209 | { |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1210 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED |
| 1211 | "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d", |
| 1212 | freq, ht_enabled, chan_offset, chan_width, cf1, cf2); |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1213 | |
| 1214 | /* Proceed only if DFS is not offloaded to the driver */ |
| 1215 | if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) |
| 1216 | return 0; |
| 1217 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1218 | /* TODO add correct implementation here */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1219 | set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width, |
| 1220 | cf1, cf2, HOSTAPD_CHAN_DFS_USABLE); |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 1221 | |
| 1222 | /* Handle cases where all channels were initially unavailable */ |
| 1223 | if (iface->state == HAPD_IFACE_DFS && !iface->cac_started) |
| 1224 | hostapd_handle_dfs(iface); |
| 1225 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1226 | return 0; |
| 1227 | } |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1228 | |
| 1229 | |
| 1230 | int hostapd_is_dfs_required(struct hostapd_iface *iface) |
| 1231 | { |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 1232 | int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1233 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame^] | 1234 | if ((!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) && |
| 1235 | !iface->conf->ieee80211h) || |
| 1236 | !iface->current_mode || |
Dmitry Shmidt | 61593f0 | 2014-04-21 16:27:35 -0700 | [diff] [blame] | 1237 | iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A) |
| 1238 | return 0; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1239 | |
| 1240 | /* Get start (first) channel for current configuration */ |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 1241 | start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1242 | if (start_chan_idx == -1) |
| 1243 | return -1; |
| 1244 | |
| 1245 | /* Get number of used channels, depend on width */ |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 1246 | n_chans = dfs_get_used_n_chans(iface, &n_chans1); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1247 | |
| 1248 | /* Check if any of configured channels require DFS */ |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 1249 | res = dfs_check_chans_radar(iface, start_chan_idx, n_chans); |
| 1250 | if (res) |
| 1251 | return res; |
| 1252 | if (start_chan_idx1 >= 0 && n_chans1 > 0) |
| 1253 | res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1); |
| 1254 | return res; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1255 | } |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1256 | |
| 1257 | |
| 1258 | int hostapd_dfs_start_cac(struct hostapd_iface *iface, int freq, |
| 1259 | int ht_enabled, int chan_offset, int chan_width, |
| 1260 | int cf1, int cf2) |
| 1261 | { |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1262 | /* This is called when the driver indicates that an offloaded DFS has |
| 1263 | * started CAC. */ |
| 1264 | hostapd_set_state(iface, HAPD_IFACE_DFS); |
| 1265 | /* TODO: How to check CAC time for ETSI weather channels? */ |
| 1266 | iface->dfs_cac_ms = 60000; |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1267 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START |
| 1268 | "freq=%d chan=%d chan_offset=%d width=%d seg0=%d " |
| 1269 | "seg1=%d cac_time=%ds", |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1270 | freq, (freq - 5000) / 5, chan_offset, chan_width, cf1, cf2, |
| 1271 | iface->dfs_cac_ms / 1000); |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1272 | iface->cac_started = 1; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1273 | os_get_reltime(&iface->dfs_cac_start); |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | |
| 1278 | /* |
| 1279 | * Main DFS handler for offloaded case. |
| 1280 | * 2 - continue channel/AP setup for non-DFS channel |
| 1281 | * 1 - continue channel/AP setup for DFS channel |
| 1282 | * 0 - channel/AP setup will be continued after CAC |
| 1283 | * -1 - hit critical error |
| 1284 | */ |
| 1285 | int hostapd_handle_dfs_offload(struct hostapd_iface *iface) |
| 1286 | { |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame^] | 1287 | int dfs_res; |
| 1288 | |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1289 | wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d", |
| 1290 | __func__, iface->cac_started); |
| 1291 | |
| 1292 | /* |
| 1293 | * If DFS has already been started, then we are being called from a |
| 1294 | * callback to continue AP/channel setup. Reset the CAC start flag and |
| 1295 | * return. |
| 1296 | */ |
| 1297 | if (iface->cac_started) { |
| 1298 | wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d", |
| 1299 | __func__, iface->cac_started); |
| 1300 | iface->cac_started = 0; |
| 1301 | return 1; |
| 1302 | } |
| 1303 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame^] | 1304 | dfs_res = hostapd_is_dfs_required(iface); |
| 1305 | if (dfs_res > 0) { |
| 1306 | wpa_printf(MSG_DEBUG, |
| 1307 | "%s: freq %d MHz requires DFS for %d chans", |
| 1308 | __func__, iface->freq, dfs_res); |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | wpa_printf(MSG_DEBUG, |
| 1313 | "%s: freq %d MHz does not require DFS. Continue channel/AP setup", |
| 1314 | __func__, iface->freq); |
| 1315 | return 2; |
| 1316 | } |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1317 | |
| 1318 | |
| 1319 | int hostapd_is_dfs_overlap(struct hostapd_iface *iface, enum chan_width width, |
| 1320 | int center_freq) |
| 1321 | { |
| 1322 | struct hostapd_channel_data *chan; |
| 1323 | struct hostapd_hw_modes *mode = iface->current_mode; |
| 1324 | int half_width; |
| 1325 | int res = 0; |
| 1326 | int i; |
| 1327 | |
| 1328 | if (!iface->conf->ieee80211h || !mode || |
| 1329 | mode->mode != HOSTAPD_MODE_IEEE80211A) |
| 1330 | return 0; |
| 1331 | |
| 1332 | switch (width) { |
| 1333 | case CHAN_WIDTH_20_NOHT: |
| 1334 | case CHAN_WIDTH_20: |
| 1335 | half_width = 10; |
| 1336 | break; |
| 1337 | case CHAN_WIDTH_40: |
| 1338 | half_width = 20; |
| 1339 | break; |
| 1340 | case CHAN_WIDTH_80: |
| 1341 | case CHAN_WIDTH_80P80: |
| 1342 | half_width = 40; |
| 1343 | break; |
| 1344 | case CHAN_WIDTH_160: |
| 1345 | half_width = 80; |
| 1346 | break; |
| 1347 | default: |
| 1348 | wpa_printf(MSG_WARNING, "DFS chanwidth %d not supported", |
| 1349 | width); |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
| 1353 | for (i = 0; i < mode->num_channels; i++) { |
| 1354 | chan = &mode->channels[i]; |
| 1355 | |
| 1356 | if (!(chan->flag & HOSTAPD_CHAN_RADAR)) |
| 1357 | continue; |
| 1358 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1359 | if ((chan->flag & HOSTAPD_CHAN_DFS_MASK) == |
| 1360 | HOSTAPD_CHAN_DFS_AVAILABLE) |
| 1361 | continue; |
| 1362 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1363 | if (center_freq - chan->freq < half_width && |
| 1364 | chan->freq - center_freq < half_width) |
| 1365 | res++; |
| 1366 | } |
| 1367 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1368 | wpa_printf(MSG_DEBUG, "DFS CAC required: (%d, %d): in range: %s", |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1369 | center_freq - half_width, center_freq + half_width, |
| 1370 | res ? "yes" : "no"); |
| 1371 | |
| 1372 | return res; |
| 1373 | } |