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, |
| 84 | 184, 192 }; |
| 85 | /* |
| 86 | * VHT80, valid channels based on center frequency: |
| 87 | * 42, 58, 106, 122, 138, 155 |
| 88 | */ |
| 89 | int allowed_80[] = { 36, 52, 100, 116, 132, 149 }; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 90 | /* |
| 91 | * VHT160 valid channels based on center frequency: |
| 92 | * 50, 114 |
| 93 | */ |
| 94 | int allowed_160[] = { 36, 100 }; |
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 | |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 147 | if (first_chan_idx + num_chans > mode->num_channels) |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 148 | return 0; |
| 149 | |
| 150 | first_chan = &mode->channels[first_chan_idx]; |
| 151 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 152 | /* hostapd DFS implementation assumes the first channel as primary. |
| 153 | * If it's not allowed to use the first channel as primary, decline the |
| 154 | * whole channel range. */ |
| 155 | if (!chan_pri_allowed(first_chan)) |
| 156 | return 0; |
| 157 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 158 | for (i = 0; i < num_chans; i++) { |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 159 | chan = dfs_get_chan_data(mode, first_chan->freq + i * 20, |
| 160 | first_chan_idx); |
| 161 | if (!chan) |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 162 | return 0; |
| 163 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 164 | /* HT 40 MHz secondary channel availability checked only for |
| 165 | * primary channel */ |
| 166 | if (!chan_bw_allowed(chan, bw, 1, !i)) |
| 167 | return 0; |
| 168 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 169 | if (!dfs_channel_available(chan, skip_radar)) |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | return 1; |
| 174 | } |
| 175 | |
| 176 | |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 177 | static int is_in_chanlist(struct hostapd_iface *iface, |
| 178 | struct hostapd_channel_data *chan) |
| 179 | { |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 180 | if (!iface->conf->acs_ch_list.num) |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 181 | return 1; |
| 182 | |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 183 | return freq_range_list_includes(&iface->conf->acs_ch_list, chan->chan); |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 187 | /* |
| 188 | * The function assumes HT40+ operation. |
| 189 | * Make sure to adjust the following variables after calling this: |
| 190 | * - hapd->secondary_channel |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 191 | * - hapd->vht/he_oper_centr_freq_seg0_idx |
| 192 | * - hapd->vht/he_oper_centr_freq_seg1_idx |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 193 | */ |
| 194 | static int dfs_find_channel(struct hostapd_iface *iface, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 195 | struct hostapd_channel_data **ret_chan, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 196 | int idx, int skip_radar) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 197 | { |
| 198 | struct hostapd_hw_modes *mode; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 199 | struct hostapd_channel_data *chan; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 200 | int i, channel_idx = 0, n_chans, n_chans1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 201 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 202 | mode = iface->current_mode; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 203 | n_chans = dfs_get_used_n_chans(iface, &n_chans1); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 204 | |
| 205 | wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans); |
| 206 | for (i = 0; i < mode->num_channels; i++) { |
| 207 | chan = &mode->channels[i]; |
| 208 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 209 | /* Skip HT40/VHT incompatible channels */ |
| 210 | if (iface->conf->ieee80211n && |
| 211 | iface->conf->secondary_channel && |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 212 | (!dfs_is_chan_allowed(chan, n_chans) || |
| 213 | !(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P))) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 214 | continue; |
| 215 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 216 | /* Skip incompatible chandefs */ |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 217 | if (!dfs_chan_range_available(mode, i, n_chans, skip_radar)) |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 218 | continue; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 219 | |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 220 | if (!is_in_chanlist(iface, chan)) |
| 221 | continue; |
| 222 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 223 | if (ret_chan && idx == channel_idx) { |
| 224 | wpa_printf(MSG_DEBUG, "Selected ch. #%d", chan->chan); |
| 225 | *ret_chan = chan; |
| 226 | return idx; |
| 227 | } |
| 228 | wpa_printf(MSG_DEBUG, "Adding channel: %d", chan->chan); |
| 229 | channel_idx++; |
| 230 | } |
| 231 | return channel_idx; |
| 232 | } |
| 233 | |
| 234 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 235 | static void dfs_adjust_center_freq(struct hostapd_iface *iface, |
| 236 | struct hostapd_channel_data *chan, |
| 237 | int secondary_channel, |
| 238 | u8 *oper_centr_freq_seg0_idx, |
| 239 | u8 *oper_centr_freq_seg1_idx) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 240 | { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 241 | if (!iface->conf->ieee80211ac && !iface->conf->ieee80211ax) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 242 | return; |
| 243 | |
| 244 | if (!chan) |
| 245 | return; |
| 246 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 247 | *oper_centr_freq_seg1_idx = 0; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 248 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 249 | switch (hostapd_get_oper_chwidth(iface->conf)) { |
| 250 | case CHANWIDTH_USE_HT: |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 251 | if (secondary_channel == 1) |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 252 | *oper_centr_freq_seg0_idx = chan->chan + 2; |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 253 | else if (secondary_channel == -1) |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 254 | *oper_centr_freq_seg0_idx = chan->chan - 2; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 255 | else |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 256 | *oper_centr_freq_seg0_idx = chan->chan; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 257 | break; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 258 | case CHANWIDTH_80MHZ: |
| 259 | *oper_centr_freq_seg0_idx = chan->chan + 6; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 260 | break; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 261 | case CHANWIDTH_160MHZ: |
| 262 | *oper_centr_freq_seg0_idx = chan->chan + 14; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 263 | break; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 264 | default: |
| 265 | wpa_printf(MSG_INFO, "DFS only VHT20/40/80/160 is supported now"); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 266 | *oper_centr_freq_seg0_idx = 0; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 267 | break; |
| 268 | } |
| 269 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 270 | wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d, %d", |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 271 | *oper_centr_freq_seg0_idx, |
| 272 | *oper_centr_freq_seg1_idx); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
| 276 | /* Return start channel idx we will use for mode->channels[idx] */ |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 277 | 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] | 278 | { |
| 279 | struct hostapd_hw_modes *mode; |
| 280 | struct hostapd_channel_data *chan; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 281 | int channel_no = iface->conf->channel; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 282 | int res = -1, i; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 283 | int chan_seg1 = -1; |
| 284 | |
| 285 | *seg1_start = -1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 286 | |
| 287 | /* HT40- */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 288 | if (iface->conf->ieee80211n && iface->conf->secondary_channel == -1) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 289 | channel_no -= 4; |
| 290 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 291 | /* VHT/HE */ |
| 292 | if (iface->conf->ieee80211ac || iface->conf->ieee80211ax) { |
| 293 | switch (hostapd_get_oper_chwidth(iface->conf)) { |
| 294 | case CHANWIDTH_USE_HT: |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 295 | break; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 296 | case CHANWIDTH_80MHZ: |
| 297 | channel_no = hostapd_get_oper_centr_freq_seg0_idx( |
| 298 | iface->conf) - 6; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 299 | break; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 300 | case CHANWIDTH_160MHZ: |
| 301 | channel_no = hostapd_get_oper_centr_freq_seg0_idx( |
| 302 | iface->conf) - 14; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 303 | break; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 304 | case CHANWIDTH_80P80MHZ: |
| 305 | channel_no = hostapd_get_oper_centr_freq_seg0_idx( |
| 306 | iface->conf) - 6; |
| 307 | chan_seg1 = hostapd_get_oper_centr_freq_seg1_idx( |
| 308 | iface->conf) - 6; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 309 | break; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 310 | default: |
| 311 | wpa_printf(MSG_INFO, |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 312 | "DFS only VHT20/40/80/160/80+80 is supported now"); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 313 | channel_no = -1; |
| 314 | break; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /* Get idx */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 319 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 320 | for (i = 0; i < mode->num_channels; i++) { |
| 321 | chan = &mode->channels[i]; |
| 322 | if (chan->chan == channel_no) { |
| 323 | res = i; |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 328 | if (res != -1 && chan_seg1 > -1) { |
| 329 | int found = 0; |
| 330 | |
| 331 | /* Get idx for seg1 */ |
| 332 | mode = iface->current_mode; |
| 333 | for (i = 0; i < mode->num_channels; i++) { |
| 334 | chan = &mode->channels[i]; |
| 335 | if (chan->chan == chan_seg1) { |
| 336 | *seg1_start = i; |
| 337 | found = 1; |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | if (!found) |
| 342 | res = -1; |
| 343 | } |
| 344 | |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 345 | if (res == -1) { |
| 346 | wpa_printf(MSG_DEBUG, |
| 347 | "DFS chan_idx seems wrong; num-ch: %d ch-no: %d conf-ch-no: %d 11n: %d sec-ch: %d vht-oper-width: %d", |
| 348 | mode->num_channels, channel_no, iface->conf->channel, |
| 349 | iface->conf->ieee80211n, |
| 350 | iface->conf->secondary_channel, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 351 | hostapd_get_oper_chwidth(iface->conf)); |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 352 | |
| 353 | for (i = 0; i < mode->num_channels; i++) { |
| 354 | wpa_printf(MSG_DEBUG, "Available channel: %d", |
| 355 | mode->channels[i].chan); |
| 356 | } |
| 357 | } |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 358 | |
| 359 | return res; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | /* At least one channel have radar flag */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 364 | static int dfs_check_chans_radar(struct hostapd_iface *iface, |
| 365 | int start_chan_idx, int n_chans) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 366 | { |
| 367 | struct hostapd_channel_data *channel; |
| 368 | struct hostapd_hw_modes *mode; |
| 369 | int i, res = 0; |
| 370 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 371 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 372 | |
| 373 | for (i = 0; i < n_chans; i++) { |
| 374 | channel = &mode->channels[start_chan_idx + i]; |
| 375 | if (channel->flag & HOSTAPD_CHAN_RADAR) |
| 376 | res++; |
| 377 | } |
| 378 | |
| 379 | return res; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | /* All channels available */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 384 | static int dfs_check_chans_available(struct hostapd_iface *iface, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 385 | int start_chan_idx, int n_chans) |
| 386 | { |
| 387 | struct hostapd_channel_data *channel; |
| 388 | struct hostapd_hw_modes *mode; |
| 389 | int i; |
| 390 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 391 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 392 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 393 | for (i = 0; i < n_chans; i++) { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 394 | channel = &mode->channels[start_chan_idx + i]; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 395 | |
| 396 | if (channel->flag & HOSTAPD_CHAN_DISABLED) |
| 397 | break; |
| 398 | |
| 399 | if (!(channel->flag & HOSTAPD_CHAN_RADAR)) |
| 400 | continue; |
| 401 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 402 | if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) != |
| 403 | HOSTAPD_CHAN_DFS_AVAILABLE) |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | return i == n_chans; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | /* At least one channel unavailable */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 412 | static int dfs_check_chans_unavailable(struct hostapd_iface *iface, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 413 | int start_chan_idx, |
| 414 | int n_chans) |
| 415 | { |
| 416 | struct hostapd_channel_data *channel; |
| 417 | struct hostapd_hw_modes *mode; |
| 418 | int i, res = 0; |
| 419 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 420 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 421 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 422 | for (i = 0; i < n_chans; i++) { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 423 | channel = &mode->channels[start_chan_idx + i]; |
| 424 | if (channel->flag & HOSTAPD_CHAN_DISABLED) |
| 425 | res++; |
| 426 | if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) == |
| 427 | HOSTAPD_CHAN_DFS_UNAVAILABLE) |
| 428 | res++; |
| 429 | } |
| 430 | |
| 431 | return res; |
| 432 | } |
| 433 | |
| 434 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 435 | static struct hostapd_channel_data * |
| 436 | dfs_get_valid_channel(struct hostapd_iface *iface, |
| 437 | int *secondary_channel, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 438 | u8 *oper_centr_freq_seg0_idx, |
| 439 | u8 *oper_centr_freq_seg1_idx, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 440 | int skip_radar) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 441 | { |
| 442 | struct hostapd_hw_modes *mode; |
| 443 | struct hostapd_channel_data *chan = NULL; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 444 | int num_available_chandefs; |
| 445 | int chan_idx; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 446 | u32 _rand; |
| 447 | |
| 448 | wpa_printf(MSG_DEBUG, "DFS: Selecting random channel"); |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 449 | *secondary_channel = 0; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 450 | *oper_centr_freq_seg0_idx = 0; |
| 451 | *oper_centr_freq_seg1_idx = 0; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 452 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 453 | if (iface->current_mode == NULL) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 454 | return NULL; |
| 455 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 456 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 457 | if (mode->mode != HOSTAPD_MODE_IEEE80211A) |
| 458 | return NULL; |
| 459 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 460 | /* Get the count first */ |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 461 | num_available_chandefs = dfs_find_channel(iface, NULL, 0, skip_radar); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 462 | if (num_available_chandefs == 0) |
| 463 | return NULL; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 464 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 465 | if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0) |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 466 | return NULL; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 467 | chan_idx = _rand % num_available_chandefs; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 468 | dfs_find_channel(iface, &chan, chan_idx, skip_radar); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 469 | |
| 470 | /* dfs_find_channel() calculations assume HT40+ */ |
| 471 | if (iface->conf->secondary_channel) |
| 472 | *secondary_channel = 1; |
| 473 | else |
| 474 | *secondary_channel = 0; |
| 475 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 476 | dfs_adjust_center_freq(iface, chan, |
| 477 | *secondary_channel, |
| 478 | oper_centr_freq_seg0_idx, |
| 479 | oper_centr_freq_seg1_idx); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 480 | |
| 481 | return chan; |
| 482 | } |
| 483 | |
| 484 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 485 | 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] | 486 | { |
| 487 | struct hostapd_hw_modes *mode; |
| 488 | struct hostapd_channel_data *chan = NULL; |
| 489 | int i; |
| 490 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 491 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 492 | if (mode == NULL) |
| 493 | return 0; |
| 494 | |
| 495 | 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] | 496 | for (i = 0; i < iface->current_mode->num_channels; i++) { |
| 497 | chan = &iface->current_mode->channels[i]; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 498 | if (chan->freq == freq) { |
| 499 | if (chan->flag & HOSTAPD_CHAN_RADAR) { |
| 500 | chan->flag &= ~HOSTAPD_CHAN_DFS_MASK; |
| 501 | chan->flag |= state; |
| 502 | return 1; /* Channel found */ |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq); |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 511 | 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] | 512 | int chan_offset, int chan_width, int cf1, |
| 513 | int cf2, u32 state) |
| 514 | { |
| 515 | int n_chans = 1, i; |
| 516 | struct hostapd_hw_modes *mode; |
| 517 | int frequency = freq; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 518 | int frequency2 = 0; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 519 | int ret = 0; |
| 520 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 521 | mode = iface->current_mode; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 522 | if (mode == NULL) |
| 523 | return 0; |
| 524 | |
| 525 | if (mode->mode != HOSTAPD_MODE_IEEE80211A) { |
| 526 | wpa_printf(MSG_WARNING, "current_mode != IEEE80211A"); |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | /* Seems cf1 and chan_width is enough here */ |
| 531 | switch (chan_width) { |
| 532 | case CHAN_WIDTH_20_NOHT: |
| 533 | case CHAN_WIDTH_20: |
| 534 | n_chans = 1; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 535 | if (frequency == 0) |
| 536 | frequency = cf1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 537 | break; |
| 538 | case CHAN_WIDTH_40: |
| 539 | n_chans = 2; |
| 540 | frequency = cf1 - 10; |
| 541 | break; |
| 542 | case CHAN_WIDTH_80: |
| 543 | n_chans = 4; |
| 544 | frequency = cf1 - 30; |
| 545 | break; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 546 | case CHAN_WIDTH_80P80: |
| 547 | n_chans = 4; |
| 548 | frequency = cf1 - 30; |
| 549 | frequency2 = cf2 - 30; |
| 550 | break; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 551 | case CHAN_WIDTH_160: |
| 552 | n_chans = 8; |
| 553 | frequency = cf1 - 70; |
| 554 | break; |
| 555 | default: |
| 556 | wpa_printf(MSG_INFO, "DFS chan_width %d not supported", |
| 557 | chan_width); |
| 558 | break; |
| 559 | } |
| 560 | |
| 561 | wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency, |
| 562 | n_chans); |
| 563 | for (i = 0; i < n_chans; i++) { |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 564 | ret += set_dfs_state_freq(iface, frequency, state); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 565 | frequency = frequency + 20; |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 566 | |
| 567 | if (chan_width == CHAN_WIDTH_80P80) { |
| 568 | ret += set_dfs_state_freq(iface, frequency2, state); |
| 569 | frequency2 = frequency2 + 20; |
| 570 | } |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | return ret; |
| 574 | } |
| 575 | |
| 576 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 577 | static int dfs_are_channels_overlapped(struct hostapd_iface *iface, int freq, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 578 | int chan_width, int cf1, int cf2) |
| 579 | { |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 580 | int start_chan_idx, start_chan_idx1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 581 | struct hostapd_hw_modes *mode; |
| 582 | struct hostapd_channel_data *chan; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 583 | 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] | 584 | u8 radar_chan; |
| 585 | int res = 0; |
| 586 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 587 | /* Our configuration */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 588 | mode = iface->current_mode; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 589 | start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1); |
| 590 | n_chans = dfs_get_used_n_chans(iface, &n_chans1); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 591 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 592 | /* Check we are on DFS channel(s) */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 593 | if (!dfs_check_chans_radar(iface, start_chan_idx, n_chans)) |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 594 | return 0; |
| 595 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 596 | /* Reported via radar event */ |
| 597 | switch (chan_width) { |
| 598 | case CHAN_WIDTH_20_NOHT: |
| 599 | case CHAN_WIDTH_20: |
| 600 | radar_n_chans = 1; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 601 | if (frequency == 0) |
| 602 | frequency = cf1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 603 | break; |
| 604 | case CHAN_WIDTH_40: |
| 605 | radar_n_chans = 2; |
| 606 | frequency = cf1 - 10; |
| 607 | break; |
| 608 | case CHAN_WIDTH_80: |
| 609 | radar_n_chans = 4; |
| 610 | frequency = cf1 - 30; |
| 611 | break; |
| 612 | case CHAN_WIDTH_160: |
| 613 | radar_n_chans = 8; |
| 614 | frequency = cf1 - 70; |
| 615 | break; |
| 616 | default: |
| 617 | wpa_printf(MSG_INFO, "DFS chan_width %d not supported", |
| 618 | chan_width); |
| 619 | break; |
| 620 | } |
| 621 | |
| 622 | ieee80211_freq_to_chan(frequency, &radar_chan); |
| 623 | |
| 624 | for (i = 0; i < n_chans; i++) { |
| 625 | chan = &mode->channels[start_chan_idx + i]; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 626 | if (!(chan->flag & HOSTAPD_CHAN_RADAR)) |
| 627 | continue; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 628 | for (j = 0; j < radar_n_chans; j++) { |
| 629 | wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d", |
| 630 | chan->chan, radar_chan + j * 4); |
| 631 | if (chan->chan == radar_chan + j * 4) |
| 632 | res++; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | wpa_printf(MSG_DEBUG, "overlapped: %d", res); |
| 637 | |
| 638 | return res; |
| 639 | } |
| 640 | |
| 641 | |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 642 | static unsigned int dfs_get_cac_time(struct hostapd_iface *iface, |
| 643 | int start_chan_idx, int n_chans) |
| 644 | { |
| 645 | struct hostapd_channel_data *channel; |
| 646 | struct hostapd_hw_modes *mode; |
| 647 | int i; |
| 648 | unsigned int cac_time_ms = 0; |
| 649 | |
| 650 | mode = iface->current_mode; |
| 651 | |
| 652 | for (i = 0; i < n_chans; i++) { |
| 653 | channel = &mode->channels[start_chan_idx + i]; |
| 654 | if (!(channel->flag & HOSTAPD_CHAN_RADAR)) |
| 655 | continue; |
| 656 | if (channel->dfs_cac_ms > cac_time_ms) |
| 657 | cac_time_ms = channel->dfs_cac_ms; |
| 658 | } |
| 659 | |
| 660 | return cac_time_ms; |
| 661 | } |
| 662 | |
| 663 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 664 | /* |
| 665 | * Main DFS handler |
| 666 | * 1 - continue channel/ap setup |
| 667 | * 0 - channel/ap setup will be continued after CAC |
| 668 | * -1 - hit critical error |
| 669 | */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 670 | int hostapd_handle_dfs(struct hostapd_iface *iface) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 671 | { |
| 672 | struct hostapd_channel_data *channel; |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 673 | int res, n_chans, n_chans1, start_chan_idx, start_chan_idx1; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 674 | int skip_radar = 0; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 675 | |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 676 | if (is_6ghz_freq(iface->freq)) |
| 677 | return 1; |
| 678 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 679 | if (!iface->current_mode) { |
| 680 | /* |
| 681 | * This can happen with drivers that do not provide mode |
| 682 | * information and as such, cannot really use hostapd for DFS. |
| 683 | */ |
| 684 | wpa_printf(MSG_DEBUG, |
| 685 | "DFS: No current_mode information - assume no need to perform DFS operations by hostapd"); |
| 686 | return 1; |
| 687 | } |
| 688 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 689 | iface->cac_started = 0; |
| 690 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 691 | do { |
| 692 | /* Get start (first) channel for current configuration */ |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 693 | start_chan_idx = dfs_get_start_chan_idx(iface, |
| 694 | &start_chan_idx1); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 695 | if (start_chan_idx == -1) |
| 696 | return -1; |
| 697 | |
| 698 | /* Get number of used channels, depend on width */ |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 699 | n_chans = dfs_get_used_n_chans(iface, &n_chans1); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 700 | |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 701 | /* Setup CAC time */ |
| 702 | iface->dfs_cac_ms = dfs_get_cac_time(iface, start_chan_idx, |
| 703 | n_chans); |
| 704 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 705 | /* Check if any of configured channels require DFS */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 706 | res = dfs_check_chans_radar(iface, start_chan_idx, n_chans); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 707 | wpa_printf(MSG_DEBUG, |
| 708 | "DFS %d channels required radar detection", |
| 709 | res); |
| 710 | if (!res) |
| 711 | return 1; |
| 712 | |
| 713 | /* Check if all channels are DFS available */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 714 | res = dfs_check_chans_available(iface, start_chan_idx, n_chans); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 715 | wpa_printf(MSG_DEBUG, |
| 716 | "DFS all channels available, (SKIP CAC): %s", |
| 717 | res ? "yes" : "no"); |
| 718 | if (res) |
| 719 | return 1; |
| 720 | |
| 721 | /* Check if any of configured channels is unavailable */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 722 | res = dfs_check_chans_unavailable(iface, start_chan_idx, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 723 | n_chans); |
| 724 | wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s", |
| 725 | res, res ? "yes": "no"); |
| 726 | if (res) { |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 727 | int sec = 0; |
| 728 | u8 cf1 = 0, cf2 = 0; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 729 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 730 | channel = dfs_get_valid_channel(iface, &sec, &cf1, &cf2, |
| 731 | skip_radar); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 732 | if (!channel) { |
| 733 | wpa_printf(MSG_ERROR, "could not get valid channel"); |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 734 | hostapd_set_state(iface, HAPD_IFACE_DFS); |
| 735 | return 0; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 736 | } |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 737 | |
| 738 | iface->freq = channel->freq; |
| 739 | iface->conf->channel = channel->chan; |
| 740 | iface->conf->secondary_channel = sec; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 741 | hostapd_set_oper_centr_freq_seg0_idx(iface->conf, cf1); |
| 742 | hostapd_set_oper_centr_freq_seg1_idx(iface->conf, cf2); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 743 | } |
| 744 | } while (res); |
| 745 | |
| 746 | /* Finally start CAC */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 747 | hostapd_set_state(iface, HAPD_IFACE_DFS); |
| 748 | wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz", iface->freq); |
| 749 | 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] | 750 | "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] | 751 | iface->freq, |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 752 | iface->conf->channel, iface->conf->secondary_channel, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 753 | hostapd_get_oper_chwidth(iface->conf), |
| 754 | hostapd_get_oper_centr_freq_seg0_idx(iface->conf), |
| 755 | hostapd_get_oper_centr_freq_seg1_idx(iface->conf), |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 756 | iface->dfs_cac_ms / 1000); |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 757 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 758 | res = hostapd_start_dfs_cac( |
| 759 | iface, iface->conf->hw_mode, iface->freq, iface->conf->channel, |
| 760 | iface->conf->ieee80211n, iface->conf->ieee80211ac, |
| 761 | iface->conf->ieee80211ax, |
| 762 | iface->conf->secondary_channel, |
| 763 | hostapd_get_oper_chwidth(iface->conf), |
| 764 | hostapd_get_oper_centr_freq_seg0_idx(iface->conf), |
| 765 | hostapd_get_oper_centr_freq_seg1_idx(iface->conf)); |
Dmitry Shmidt | a38abf9 | 2014-03-06 13:38:44 -0800 | [diff] [blame] | 766 | |
| 767 | if (res) { |
| 768 | wpa_printf(MSG_ERROR, "DFS start_dfs_cac() failed, %d", res); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 769 | return -1; |
| 770 | } |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 776 | static int hostapd_config_dfs_chan_available(struct hostapd_iface *iface) |
| 777 | { |
| 778 | int n_chans, n_chans1, start_chan_idx, start_chan_idx1; |
| 779 | |
| 780 | /* Get the start (first) channel for current configuration */ |
| 781 | start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1); |
| 782 | if (start_chan_idx < 0) |
| 783 | return 0; |
| 784 | |
| 785 | /* Get the number of used channels, depending on width */ |
| 786 | n_chans = dfs_get_used_n_chans(iface, &n_chans1); |
| 787 | |
| 788 | /* Check if all channels are DFS available */ |
| 789 | return dfs_check_chans_available(iface, start_chan_idx, n_chans); |
| 790 | } |
| 791 | |
| 792 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 793 | 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] | 794 | int ht_enabled, int chan_offset, int chan_width, |
| 795 | int cf1, int cf2) |
| 796 | { |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 797 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_COMPLETED |
| 798 | "success=%d freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d", |
| 799 | success, freq, ht_enabled, chan_offset, chan_width, cf1, cf2); |
| 800 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 801 | if (success) { |
| 802 | /* Complete iface/ap configuration */ |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 803 | if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) { |
| 804 | /* Complete AP configuration for the first bring up. */ |
| 805 | if (iface->state != HAPD_IFACE_ENABLED) |
| 806 | hostapd_setup_interface_complete(iface, 0); |
| 807 | else |
| 808 | iface->cac_started = 0; |
| 809 | } else { |
| 810 | set_dfs_state(iface, freq, ht_enabled, chan_offset, |
| 811 | chan_width, cf1, cf2, |
| 812 | HOSTAPD_CHAN_DFS_AVAILABLE); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 813 | /* |
| 814 | * Just mark the channel available when CAC completion |
| 815 | * event is received in enabled state. CAC result could |
| 816 | * have been propagated from another radio having the |
| 817 | * same regulatory configuration. When CAC completion is |
| 818 | * received during non-HAPD_IFACE_ENABLED state, make |
| 819 | * sure the configured channel is available because this |
| 820 | * CAC completion event could have been propagated from |
| 821 | * another radio. |
| 822 | */ |
| 823 | if (iface->state != HAPD_IFACE_ENABLED && |
| 824 | hostapd_config_dfs_chan_available(iface)) { |
| 825 | hostapd_setup_interface_complete(iface, 0); |
| 826 | iface->cac_started = 0; |
| 827 | } |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 828 | } |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 835 | int hostapd_dfs_pre_cac_expired(struct hostapd_iface *iface, int freq, |
| 836 | int ht_enabled, int chan_offset, int chan_width, |
| 837 | int cf1, int cf2) |
| 838 | { |
| 839 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_PRE_CAC_EXPIRED |
| 840 | "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d", |
| 841 | freq, ht_enabled, chan_offset, chan_width, cf1, cf2); |
| 842 | |
| 843 | /* Proceed only if DFS is not offloaded to the driver */ |
| 844 | if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) |
| 845 | return 0; |
| 846 | |
| 847 | set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width, |
| 848 | cf1, cf2, HOSTAPD_CHAN_DFS_USABLE); |
| 849 | |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 854 | static int hostapd_dfs_start_channel_switch_cac(struct hostapd_iface *iface) |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 855 | { |
| 856 | struct hostapd_channel_data *channel; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 857 | int secondary_channel; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 858 | u8 oper_centr_freq_seg0_idx = 0; |
| 859 | u8 oper_centr_freq_seg1_idx = 0; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 860 | int skip_radar = 0; |
| 861 | int err = 1; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 862 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 863 | /* Radar detected during active CAC */ |
| 864 | iface->cac_started = 0; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 865 | channel = dfs_get_valid_channel(iface, &secondary_channel, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 866 | &oper_centr_freq_seg0_idx, |
| 867 | &oper_centr_freq_seg1_idx, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 868 | skip_radar); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 869 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 870 | if (!channel) { |
| 871 | wpa_printf(MSG_ERROR, "No valid channel available"); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 872 | return err; |
| 873 | } |
| 874 | |
| 875 | wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d", |
| 876 | channel->chan); |
| 877 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL |
| 878 | "freq=%d chan=%d sec_chan=%d", channel->freq, |
| 879 | channel->chan, secondary_channel); |
| 880 | |
| 881 | iface->freq = channel->freq; |
| 882 | iface->conf->channel = channel->chan; |
| 883 | iface->conf->secondary_channel = secondary_channel; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 884 | hostapd_set_oper_centr_freq_seg0_idx(iface->conf, |
| 885 | oper_centr_freq_seg0_idx); |
| 886 | hostapd_set_oper_centr_freq_seg1_idx(iface->conf, |
| 887 | oper_centr_freq_seg1_idx); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 888 | err = 0; |
| 889 | |
| 890 | hostapd_setup_interface_complete(iface, err); |
| 891 | return err; |
| 892 | } |
| 893 | |
| 894 | |
| 895 | static int hostapd_dfs_start_channel_switch(struct hostapd_iface *iface) |
| 896 | { |
| 897 | struct hostapd_channel_data *channel; |
| 898 | int secondary_channel; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 899 | u8 oper_centr_freq_seg0_idx; |
| 900 | u8 oper_centr_freq_seg1_idx; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 901 | int skip_radar = 1; |
| 902 | struct csa_settings csa_settings; |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 903 | unsigned int i; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 904 | int err = 1; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 905 | struct hostapd_hw_modes *cmode = iface->current_mode; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 906 | |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 907 | wpa_printf(MSG_DEBUG, "%s called (CAC active: %s, CSA active: %s)", |
| 908 | __func__, iface->cac_started ? "yes" : "no", |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 909 | hostapd_csa_in_progress(iface) ? "yes" : "no"); |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 910 | |
| 911 | /* Check if CSA in progress */ |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 912 | if (hostapd_csa_in_progress(iface)) |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 913 | return 0; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 914 | |
| 915 | /* Check if active CAC */ |
| 916 | if (iface->cac_started) |
| 917 | return hostapd_dfs_start_channel_switch_cac(iface); |
| 918 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 919 | /* |
| 920 | * Allow selection of DFS channel in ETSI to comply with |
| 921 | * uniform spreading. |
| 922 | */ |
| 923 | if (iface->dfs_domain == HOSTAPD_DFS_REGION_ETSI) |
| 924 | skip_radar = 0; |
| 925 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 926 | /* Perform channel switch/CSA */ |
| 927 | channel = dfs_get_valid_channel(iface, &secondary_channel, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 928 | &oper_centr_freq_seg0_idx, |
| 929 | &oper_centr_freq_seg1_idx, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 930 | skip_radar); |
| 931 | |
| 932 | if (!channel) { |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 933 | /* |
| 934 | * If there is no channel to switch immediately to, check if |
| 935 | * there is another channel where we can switch even if it |
| 936 | * requires to perform a CAC first. |
| 937 | */ |
| 938 | skip_radar = 0; |
| 939 | channel = dfs_get_valid_channel(iface, &secondary_channel, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 940 | &oper_centr_freq_seg0_idx, |
| 941 | &oper_centr_freq_seg1_idx, |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 942 | skip_radar); |
| 943 | if (!channel) { |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 944 | wpa_printf(MSG_INFO, |
| 945 | "%s: no DFS channels left, waiting for NOP to finish", |
| 946 | __func__); |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 947 | return err; |
| 948 | } |
| 949 | |
| 950 | iface->freq = channel->freq; |
| 951 | iface->conf->channel = channel->chan; |
| 952 | iface->conf->secondary_channel = secondary_channel; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 953 | hostapd_set_oper_centr_freq_seg0_idx(iface->conf, |
| 954 | oper_centr_freq_seg0_idx); |
| 955 | hostapd_set_oper_centr_freq_seg1_idx(iface->conf, |
| 956 | oper_centr_freq_seg1_idx); |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 957 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 958 | hostapd_disable_iface(iface); |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 959 | hostapd_enable_iface(iface); |
| 960 | return 0; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | wpa_printf(MSG_DEBUG, "DFS will switch to a new channel %d", |
| 964 | channel->chan); |
| 965 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NEW_CHANNEL |
| 966 | "freq=%d chan=%d sec_chan=%d", channel->freq, |
| 967 | channel->chan, secondary_channel); |
| 968 | |
| 969 | /* Setup CSA request */ |
| 970 | os_memset(&csa_settings, 0, sizeof(csa_settings)); |
| 971 | csa_settings.cs_count = 5; |
| 972 | csa_settings.block_tx = 1; |
| 973 | err = hostapd_set_freq_params(&csa_settings.freq_params, |
| 974 | iface->conf->hw_mode, |
| 975 | channel->freq, |
| 976 | channel->chan, |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 977 | iface->conf->enable_edmg, |
| 978 | iface->conf->edmg_channel, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 979 | iface->conf->ieee80211n, |
| 980 | iface->conf->ieee80211ac, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 981 | iface->conf->ieee80211ax, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 982 | secondary_channel, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 983 | hostapd_get_oper_chwidth(iface->conf), |
| 984 | oper_centr_freq_seg0_idx, |
| 985 | oper_centr_freq_seg1_idx, |
| 986 | cmode->vht_capab, |
| 987 | &cmode->he_capab[IEEE80211_MODE_AP]); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 988 | |
| 989 | if (err) { |
| 990 | wpa_printf(MSG_ERROR, "DFS failed to calculate CSA freq params"); |
| 991 | hostapd_disable_iface(iface); |
| 992 | return err; |
| 993 | } |
| 994 | |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 995 | for (i = 0; i < iface->num_bss; i++) { |
| 996 | err = hostapd_switch_channel(iface->bss[i], &csa_settings); |
| 997 | if (err) |
| 998 | break; |
| 999 | } |
| 1000 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1001 | if (err) { |
| 1002 | wpa_printf(MSG_WARNING, "DFS failed to schedule CSA (%d) - trying fallback", |
| 1003 | err); |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1004 | iface->freq = channel->freq; |
| 1005 | iface->conf->channel = channel->chan; |
| 1006 | iface->conf->secondary_channel = secondary_channel; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1007 | hostapd_set_oper_centr_freq_seg0_idx(iface->conf, |
| 1008 | oper_centr_freq_seg0_idx); |
| 1009 | hostapd_set_oper_centr_freq_seg1_idx(iface->conf, |
| 1010 | oper_centr_freq_seg1_idx); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1011 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1012 | hostapd_disable_iface(iface); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1013 | hostapd_enable_iface(iface); |
| 1014 | return 0; |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1017 | /* Channel configuration will be updated once CSA completes and |
| 1018 | * ch_switch_notify event is received */ |
| 1019 | |
| 1020 | wpa_printf(MSG_DEBUG, "DFS waiting channel switch event"); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1025 | int hostapd_dfs_radar_detected(struct hostapd_iface *iface, int freq, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1026 | int ht_enabled, int chan_offset, int chan_width, |
| 1027 | int cf1, int cf2) |
| 1028 | { |
| 1029 | int res; |
| 1030 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1031 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_RADAR_DETECTED |
| 1032 | "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d", |
| 1033 | freq, ht_enabled, chan_offset, chan_width, cf1, cf2); |
| 1034 | |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1035 | /* Proceed only if DFS is not offloaded to the driver */ |
| 1036 | if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) |
| 1037 | return 0; |
| 1038 | |
| 1039 | if (!iface->conf->ieee80211h) |
| 1040 | return 0; |
| 1041 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1042 | /* mark radar frequency as invalid */ |
Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 1043 | set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width, |
| 1044 | cf1, cf2, HOSTAPD_CHAN_DFS_UNAVAILABLE); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1045 | |
| 1046 | /* Skip if reported radar event not overlapped our channels */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1047 | res = dfs_are_channels_overlapped(iface, freq, chan_width, cf1, cf2); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1048 | if (!res) |
| 1049 | return 0; |
| 1050 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1051 | /* radar detected while operating, switch the channel. */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1052 | res = hostapd_dfs_start_channel_switch(iface); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1053 | |
| 1054 | return res; |
| 1055 | } |
| 1056 | |
| 1057 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1058 | int hostapd_dfs_nop_finished(struct hostapd_iface *iface, int freq, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1059 | int ht_enabled, int chan_offset, int chan_width, |
| 1060 | int cf1, int cf2) |
| 1061 | { |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1062 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_NOP_FINISHED |
| 1063 | "freq=%d ht_enabled=%d chan_offset=%d chan_width=%d cf1=%d cf2=%d", |
| 1064 | freq, ht_enabled, chan_offset, chan_width, cf1, cf2); |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1065 | |
| 1066 | /* Proceed only if DFS is not offloaded to the driver */ |
| 1067 | if (iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) |
| 1068 | return 0; |
| 1069 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1070 | /* TODO add correct implementation here */ |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1071 | set_dfs_state(iface, freq, ht_enabled, chan_offset, chan_width, |
| 1072 | cf1, cf2, HOSTAPD_CHAN_DFS_USABLE); |
Dmitry Shmidt | 31a29cc | 2016-03-09 15:58:17 -0800 | [diff] [blame] | 1073 | |
| 1074 | /* Handle cases where all channels were initially unavailable */ |
| 1075 | if (iface->state == HAPD_IFACE_DFS && !iface->cac_started) |
| 1076 | hostapd_handle_dfs(iface); |
| 1077 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1078 | return 0; |
| 1079 | } |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1080 | |
| 1081 | |
| 1082 | int hostapd_is_dfs_required(struct hostapd_iface *iface) |
| 1083 | { |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 1084 | int n_chans, n_chans1, start_chan_idx, start_chan_idx1, res; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1085 | |
Dmitry Shmidt | 61593f0 | 2014-04-21 16:27:35 -0700 | [diff] [blame] | 1086 | if (!iface->conf->ieee80211h || !iface->current_mode || |
| 1087 | iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A) |
| 1088 | return 0; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1089 | |
| 1090 | /* Get start (first) channel for current configuration */ |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 1091 | start_chan_idx = dfs_get_start_chan_idx(iface, &start_chan_idx1); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1092 | if (start_chan_idx == -1) |
| 1093 | return -1; |
| 1094 | |
| 1095 | /* Get number of used channels, depend on width */ |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 1096 | n_chans = dfs_get_used_n_chans(iface, &n_chans1); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1097 | |
| 1098 | /* Check if any of configured channels require DFS */ |
Dmitry Shmidt | a7b06fa | 2014-10-09 12:56:52 -0700 | [diff] [blame] | 1099 | res = dfs_check_chans_radar(iface, start_chan_idx, n_chans); |
| 1100 | if (res) |
| 1101 | return res; |
| 1102 | if (start_chan_idx1 >= 0 && n_chans1 > 0) |
| 1103 | res = dfs_check_chans_radar(iface, start_chan_idx1, n_chans1); |
| 1104 | return res; |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1105 | } |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1106 | |
| 1107 | |
| 1108 | int hostapd_dfs_start_cac(struct hostapd_iface *iface, int freq, |
| 1109 | int ht_enabled, int chan_offset, int chan_width, |
| 1110 | int cf1, int cf2) |
| 1111 | { |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1112 | /* This is called when the driver indicates that an offloaded DFS has |
| 1113 | * started CAC. */ |
| 1114 | hostapd_set_state(iface, HAPD_IFACE_DFS); |
| 1115 | /* TODO: How to check CAC time for ETSI weather channels? */ |
| 1116 | iface->dfs_cac_ms = 60000; |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1117 | wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, DFS_EVENT_CAC_START |
| 1118 | "freq=%d chan=%d chan_offset=%d width=%d seg0=%d " |
| 1119 | "seg1=%d cac_time=%ds", |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1120 | freq, (freq - 5000) / 5, chan_offset, chan_width, cf1, cf2, |
| 1121 | iface->dfs_cac_ms / 1000); |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1122 | iface->cac_started = 1; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1123 | os_get_reltime(&iface->dfs_cac_start); |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | |
| 1128 | /* |
| 1129 | * Main DFS handler for offloaded case. |
| 1130 | * 2 - continue channel/AP setup for non-DFS channel |
| 1131 | * 1 - continue channel/AP setup for DFS channel |
| 1132 | * 0 - channel/AP setup will be continued after CAC |
| 1133 | * -1 - hit critical error |
| 1134 | */ |
| 1135 | int hostapd_handle_dfs_offload(struct hostapd_iface *iface) |
| 1136 | { |
| 1137 | wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d", |
| 1138 | __func__, iface->cac_started); |
| 1139 | |
| 1140 | /* |
| 1141 | * If DFS has already been started, then we are being called from a |
| 1142 | * callback to continue AP/channel setup. Reset the CAC start flag and |
| 1143 | * return. |
| 1144 | */ |
| 1145 | if (iface->cac_started) { |
| 1146 | wpa_printf(MSG_DEBUG, "%s: iface->cac_started: %d", |
| 1147 | __func__, iface->cac_started); |
| 1148 | iface->cac_started = 0; |
| 1149 | return 1; |
| 1150 | } |
| 1151 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1152 | if (ieee80211_is_dfs(iface->freq, iface->hw_features, |
| 1153 | iface->num_hw_features)) { |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1154 | wpa_printf(MSG_DEBUG, "%s: freq %d MHz requires DFS", |
| 1155 | __func__, iface->freq); |
| 1156 | return 0; |
| 1157 | } |
| 1158 | |
| 1159 | wpa_printf(MSG_DEBUG, |
| 1160 | "%s: freq %d MHz does not require DFS. Continue channel/AP setup", |
| 1161 | __func__, iface->freq); |
| 1162 | return 2; |
| 1163 | } |