Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 1 | /* |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 2 | * Scanning implementation |
| 3 | * |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 4 | * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi> |
| 5 | * Copyright 2004, Instant802 Networks, Inc. |
| 6 | * Copyright 2005, Devicescape Software, Inc. |
| 7 | * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> |
| 8 | * Copyright 2007, Michael Wu <flamingice@sourmilk.net> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
Johannes Berg | 00d3f14 | 2009-02-10 21:26:00 +0100 | [diff] [blame] | 15 | /* TODO: figure out how to avoid that the "current BSS" expires */ |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 16 | |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 17 | #include <linux/wireless.h> |
| 18 | #include <linux/if_arp.h> |
Johannes Berg | 078e1e6 | 2009-01-22 18:07:31 +0100 | [diff] [blame] | 19 | #include <linux/rtnetlink.h> |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 20 | #include <net/mac80211.h> |
| 21 | #include <net/iw_handler.h> |
| 22 | |
| 23 | #include "ieee80211_i.h" |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 24 | #include "mesh.h" |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 25 | |
| 26 | #define IEEE80211_PROBE_DELAY (HZ / 33) |
| 27 | #define IEEE80211_CHANNEL_TIME (HZ / 33) |
| 28 | #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5) |
| 29 | |
Johannes Berg | c2b1345 | 2008-09-11 00:01:55 +0200 | [diff] [blame] | 30 | struct ieee80211_bss * |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 31 | ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq, |
| 32 | u8 *ssid, u8 ssid_len) |
| 33 | { |
Johannes Berg | 00d3f14 | 2009-02-10 21:26:00 +0100 | [diff] [blame] | 34 | return (void *)cfg80211_get_bss(local->hw.wiphy, |
| 35 | ieee80211_get_channel(local->hw.wiphy, |
| 36 | freq), |
| 37 | bssid, ssid, ssid_len, |
| 38 | 0, 0); |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Johannes Berg | 00d3f14 | 2009-02-10 21:26:00 +0100 | [diff] [blame] | 41 | static void ieee80211_rx_bss_free(struct cfg80211_bss *cbss) |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 42 | { |
Johannes Berg | 00d3f14 | 2009-02-10 21:26:00 +0100 | [diff] [blame] | 43 | struct ieee80211_bss *bss = (void *)cbss; |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 44 | |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 45 | kfree(bss_mesh_id(bss)); |
| 46 | kfree(bss_mesh_cfg(bss)); |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void ieee80211_rx_bss_put(struct ieee80211_local *local, |
Johannes Berg | c2b1345 | 2008-09-11 00:01:55 +0200 | [diff] [blame] | 50 | struct ieee80211_bss *bss) |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 51 | { |
Johannes Berg | 00d3f14 | 2009-02-10 21:26:00 +0100 | [diff] [blame] | 52 | cfg80211_put_bss((struct cfg80211_bss *)bss); |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Johannes Berg | c2b1345 | 2008-09-11 00:01:55 +0200 | [diff] [blame] | 55 | struct ieee80211_bss * |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 56 | ieee80211_bss_info_update(struct ieee80211_local *local, |
| 57 | struct ieee80211_rx_status *rx_status, |
| 58 | struct ieee80211_mgmt *mgmt, |
| 59 | size_t len, |
| 60 | struct ieee802_11_elems *elems, |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 61 | struct ieee80211_channel *channel, |
| 62 | bool beacon) |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 63 | { |
Johannes Berg | c2b1345 | 2008-09-11 00:01:55 +0200 | [diff] [blame] | 64 | struct ieee80211_bss *bss; |
Johannes Berg | 00d3f14 | 2009-02-10 21:26:00 +0100 | [diff] [blame] | 65 | int clen; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 66 | s32 signal = 0; |
| 67 | |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame] | 68 | if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 69 | signal = rx_status->signal * 100; |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame] | 70 | else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 71 | signal = (rx_status->signal * 100) / local->hw.max_signal; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 72 | |
Johannes Berg | 00d3f14 | 2009-02-10 21:26:00 +0100 | [diff] [blame] | 73 | bss = (void *)cfg80211_inform_bss_frame(local->hw.wiphy, channel, |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame] | 74 | mgmt, len, signal, GFP_ATOMIC); |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 75 | |
Johannes Berg | 00d3f14 | 2009-02-10 21:26:00 +0100 | [diff] [blame] | 76 | if (!bss) |
| 77 | return NULL; |
| 78 | |
| 79 | bss->cbss.free_priv = ieee80211_rx_bss_free; |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 80 | |
| 81 | /* save the ERP value so that it is available at association time */ |
| 82 | if (elems->erp_info && elems->erp_info_len >= 1) { |
| 83 | bss->erp_value = elems->erp_info[0]; |
| 84 | bss->has_erp_value = 1; |
| 85 | } |
| 86 | |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 87 | if (elems->tim) { |
| 88 | struct ieee80211_tim_ie *tim_ie = |
| 89 | (struct ieee80211_tim_ie *)elems->tim; |
| 90 | bss->dtim_period = tim_ie->dtim_period; |
| 91 | } |
| 92 | |
| 93 | /* set default value for buggy APs */ |
| 94 | if (!elems->tim || bss->dtim_period == 0) |
| 95 | bss->dtim_period = 1; |
| 96 | |
| 97 | bss->supp_rates_len = 0; |
| 98 | if (elems->supp_rates) { |
| 99 | clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len; |
| 100 | if (clen > elems->supp_rates_len) |
| 101 | clen = elems->supp_rates_len; |
| 102 | memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates, |
| 103 | clen); |
| 104 | bss->supp_rates_len += clen; |
| 105 | } |
| 106 | if (elems->ext_supp_rates) { |
| 107 | clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len; |
| 108 | if (clen > elems->ext_supp_rates_len) |
| 109 | clen = elems->ext_supp_rates_len; |
| 110 | memcpy(&bss->supp_rates[bss->supp_rates_len], |
| 111 | elems->ext_supp_rates, clen); |
| 112 | bss->supp_rates_len += clen; |
| 113 | } |
| 114 | |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 115 | bss->wmm_used = elems->wmm_param || elems->wmm_info; |
| 116 | |
| 117 | if (!beacon) |
| 118 | bss->last_probe_resp = jiffies; |
| 119 | |
Johannes Berg | 5484e23 | 2008-09-08 17:44:27 +0200 | [diff] [blame] | 120 | return bss; |
| 121 | } |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 122 | |
Vasanthakumar Thiagarajan | 7a94708 | 2009-02-04 18:28:48 +0530 | [diff] [blame] | 123 | void ieee80211_rx_bss_remove(struct ieee80211_sub_if_data *sdata, u8 *bssid, |
| 124 | int freq, u8 *ssid, u8 ssid_len) |
| 125 | { |
| 126 | struct ieee80211_bss *bss; |
| 127 | struct ieee80211_local *local = sdata->local; |
| 128 | |
| 129 | bss = ieee80211_rx_bss_get(local, bssid, freq, ssid, ssid_len); |
| 130 | if (bss) { |
Johannes Berg | 00d3f14 | 2009-02-10 21:26:00 +0100 | [diff] [blame] | 131 | cfg80211_unlink_bss(local->hw.wiphy, (void *)bss); |
Vasanthakumar Thiagarajan | 7a94708 | 2009-02-04 18:28:48 +0530 | [diff] [blame] | 132 | ieee80211_rx_bss_put(local, bss); |
| 133 | } |
| 134 | } |
| 135 | |
Johannes Berg | 98c8fcc | 2008-09-08 17:44:26 +0200 | [diff] [blame] | 136 | ieee80211_rx_result |
Johannes Berg | c2b1345 | 2008-09-11 00:01:55 +0200 | [diff] [blame] | 137 | ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb, |
| 138 | struct ieee80211_rx_status *rx_status) |
Johannes Berg | 98c8fcc | 2008-09-08 17:44:26 +0200 | [diff] [blame] | 139 | { |
| 140 | struct ieee80211_mgmt *mgmt; |
Johannes Berg | c2b1345 | 2008-09-11 00:01:55 +0200 | [diff] [blame] | 141 | struct ieee80211_bss *bss; |
Johannes Berg | 98c8fcc | 2008-09-08 17:44:26 +0200 | [diff] [blame] | 142 | u8 *elements; |
| 143 | struct ieee80211_channel *channel; |
| 144 | size_t baselen; |
| 145 | int freq; |
| 146 | __le16 fc; |
| 147 | bool presp, beacon = false; |
| 148 | struct ieee802_11_elems elems; |
| 149 | |
| 150 | if (skb->len < 2) |
| 151 | return RX_DROP_UNUSABLE; |
| 152 | |
| 153 | mgmt = (struct ieee80211_mgmt *) skb->data; |
| 154 | fc = mgmt->frame_control; |
| 155 | |
| 156 | if (ieee80211_is_ctl(fc)) |
| 157 | return RX_CONTINUE; |
| 158 | |
| 159 | if (skb->len < 24) |
| 160 | return RX_DROP_MONITOR; |
| 161 | |
| 162 | presp = ieee80211_is_probe_resp(fc); |
| 163 | if (presp) { |
| 164 | /* ignore ProbeResp to foreign address */ |
| 165 | if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN)) |
| 166 | return RX_DROP_MONITOR; |
| 167 | |
| 168 | presp = true; |
| 169 | elements = mgmt->u.probe_resp.variable; |
| 170 | baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable); |
| 171 | } else { |
| 172 | beacon = ieee80211_is_beacon(fc); |
| 173 | baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable); |
| 174 | elements = mgmt->u.beacon.variable; |
| 175 | } |
| 176 | |
| 177 | if (!presp && !beacon) |
| 178 | return RX_CONTINUE; |
| 179 | |
| 180 | if (baselen > skb->len) |
| 181 | return RX_DROP_MONITOR; |
| 182 | |
| 183 | ieee802_11_parse_elems(elements, skb->len - baselen, &elems); |
| 184 | |
| 185 | if (elems.ds_params && elems.ds_params_len == 1) |
| 186 | freq = ieee80211_channel_to_frequency(elems.ds_params[0]); |
| 187 | else |
| 188 | freq = rx_status->freq; |
| 189 | |
| 190 | channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq); |
| 191 | |
| 192 | if (!channel || channel->flags & IEEE80211_CHAN_DISABLED) |
| 193 | return RX_DROP_MONITOR; |
| 194 | |
| 195 | bss = ieee80211_bss_info_update(sdata->local, rx_status, |
| 196 | mgmt, skb->len, &elems, |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 197 | channel, beacon); |
Jouni Malinen | d048e50 | 2008-10-11 03:29:55 +0300 | [diff] [blame] | 198 | if (bss) |
| 199 | ieee80211_rx_bss_put(sdata->local, bss); |
Johannes Berg | 98c8fcc | 2008-09-08 17:44:26 +0200 | [diff] [blame] | 200 | |
| 201 | dev_kfree_skb(skb); |
| 202 | return RX_QUEUED; |
| 203 | } |
| 204 | |
Kalle Valo | 9050bdd | 2009-03-22 21:57:21 +0200 | [diff] [blame] | 205 | /* |
| 206 | * inform AP that we will go to sleep so that it will buffer the frames |
| 207 | * while we scan |
| 208 | */ |
| 209 | static void ieee80211_scan_ps_enable(struct ieee80211_sub_if_data *sdata) |
| 210 | { |
| 211 | struct ieee80211_local *local = sdata->local; |
| 212 | bool ps = false; |
| 213 | |
| 214 | /* FIXME: what to do when local->pspolling is true? */ |
| 215 | |
| 216 | del_timer_sync(&local->dynamic_ps_timer); |
| 217 | cancel_work_sync(&local->dynamic_ps_enable_work); |
| 218 | |
| 219 | if (local->hw.conf.flags & IEEE80211_CONF_PS) { |
| 220 | ps = true; |
| 221 | local->hw.conf.flags &= ~IEEE80211_CONF_PS; |
| 222 | ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS); |
| 223 | } |
| 224 | |
| 225 | if (!ps || !(local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) |
| 226 | /* |
| 227 | * If power save was enabled, no need to send a nullfunc |
| 228 | * frame because AP knows that we are sleeping. But if the |
| 229 | * hardware is creating the nullfunc frame for power save |
| 230 | * status (ie. IEEE80211_HW_PS_NULLFUNC_STACK is not |
| 231 | * enabled) and power save was enabled, the firmware just |
| 232 | * sent a null frame with power save disabled. So we need |
| 233 | * to send a new nullfunc frame to inform the AP that we |
| 234 | * are again sleeping. |
| 235 | */ |
| 236 | ieee80211_send_nullfunc(local, sdata, 1); |
| 237 | } |
| 238 | |
| 239 | /* inform AP that we are awake again, unless power save is enabled */ |
| 240 | static void ieee80211_scan_ps_disable(struct ieee80211_sub_if_data *sdata) |
| 241 | { |
| 242 | struct ieee80211_local *local = sdata->local; |
| 243 | |
Johannes Berg | 965beda | 2009-04-16 13:17:24 +0200 | [diff] [blame] | 244 | if (!local->ps_sdata) |
Kalle Valo | 9050bdd | 2009-03-22 21:57:21 +0200 | [diff] [blame] | 245 | ieee80211_send_nullfunc(local, sdata, 0); |
| 246 | else { |
| 247 | /* |
| 248 | * In !IEEE80211_HW_PS_NULLFUNC_STACK case the hardware |
| 249 | * will send a nullfunc frame with the powersave bit set |
| 250 | * even though the AP already knows that we are sleeping. |
| 251 | * This could be avoided by sending a null frame with power |
| 252 | * save bit disabled before enabling the power save, but |
| 253 | * this doesn't gain anything. |
| 254 | * |
| 255 | * When IEEE80211_HW_PS_NULLFUNC_STACK is enabled, no need |
| 256 | * to send a nullfunc frame because AP already knows that |
| 257 | * we are sleeping, let's just enable power save mode in |
| 258 | * hardware. |
| 259 | */ |
| 260 | local->hw.conf.flags |= IEEE80211_CONF_PS; |
| 261 | ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS); |
| 262 | } |
| 263 | } |
| 264 | |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 265 | static void ieee80211_restore_scan_ies(struct ieee80211_local *local) |
| 266 | { |
| 267 | kfree(local->scan_req->ie); |
| 268 | local->scan_req->ie = local->orig_ies; |
| 269 | local->scan_req->ie_len = local->orig_ies_len; |
| 270 | } |
| 271 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 272 | void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted) |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 273 | { |
| 274 | struct ieee80211_local *local = hw_to_local(hw); |
| 275 | struct ieee80211_sub_if_data *sdata; |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 276 | bool was_hw_scan; |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 277 | |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 278 | mutex_lock(&local->scan_mtx); |
| 279 | |
| 280 | if (WARN_ON(!local->hw_scanning && !local->sw_scanning)) { |
| 281 | mutex_unlock(&local->scan_mtx); |
Johannes Berg | 5bc7572 | 2008-09-11 00:01:51 +0200 | [diff] [blame] | 282 | return; |
Johannes Berg | de95a54 | 2009-04-01 11:58:36 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 285 | if (WARN_ON(!local->scan_req)) { |
| 286 | mutex_unlock(&local->scan_mtx); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | if (local->hw_scanning) |
| 291 | ieee80211_restore_scan_ies(local); |
| 292 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 293 | if (local->scan_req != &local->int_scan_req) |
| 294 | cfg80211_scan_done(local->scan_req, aborted); |
| 295 | local->scan_req = NULL; |
| 296 | |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 297 | was_hw_scan = local->hw_scanning; |
| 298 | local->hw_scanning = false; |
| 299 | local->sw_scanning = false; |
| 300 | |
| 301 | /* we only have to protect scan_req and hw/sw scan */ |
| 302 | mutex_unlock(&local->scan_mtx); |
| 303 | |
| 304 | if (was_hw_scan) { |
Johannes Berg | e897558 | 2008-10-09 12:18:51 +0200 | [diff] [blame] | 305 | /* |
| 306 | * Somebody might have requested channel change during scan |
| 307 | * that we won't have acted upon, try now. ieee80211_hw_config |
| 308 | * will set the flag based on actual changes. |
| 309 | */ |
| 310 | ieee80211_hw_config(local, 0); |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 311 | goto done; |
| 312 | } |
| 313 | |
Johannes Berg | e897558 | 2008-10-09 12:18:51 +0200 | [diff] [blame] | 314 | ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 315 | |
| 316 | netif_tx_lock_bh(local->mdev); |
| 317 | netif_addr_lock(local->mdev); |
| 318 | local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC; |
| 319 | local->ops->configure_filter(local_to_hw(local), |
| 320 | FIF_BCN_PRBRESP_PROMISC, |
| 321 | &local->filter_flags, |
| 322 | local->mdev->mc_count, |
| 323 | local->mdev->mc_list); |
| 324 | |
| 325 | netif_addr_unlock(local->mdev); |
| 326 | netif_tx_unlock_bh(local->mdev); |
| 327 | |
Michael Buesch | 80e775b | 2009-02-20 15:37:03 +0100 | [diff] [blame] | 328 | if (local->ops->sw_scan_complete) |
| 329 | local->ops->sw_scan_complete(local_to_hw(local)); |
| 330 | |
Johannes Berg | 078e1e6 | 2009-01-22 18:07:31 +0100 | [diff] [blame] | 331 | mutex_lock(&local->iflist_mtx); |
| 332 | list_for_each_entry(sdata, &local->interfaces, list) { |
Johannes Berg | fb9ddbf | 2009-01-26 19:11:57 +0100 | [diff] [blame] | 333 | if (!netif_running(sdata->dev)) |
| 334 | continue; |
| 335 | |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 336 | /* Tell AP we're back */ |
Johannes Berg | 05c914f | 2008-09-11 00:01:58 +0200 | [diff] [blame] | 337 | if (sdata->vif.type == NL80211_IFTYPE_STATION) { |
Johannes Berg | 4690029 | 2009-02-15 12:44:28 +0100 | [diff] [blame] | 338 | if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED) { |
Kalle Valo | 9050bdd | 2009-03-22 21:57:21 +0200 | [diff] [blame] | 339 | ieee80211_scan_ps_disable(sdata); |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 340 | netif_tx_wake_all_queues(sdata->dev); |
| 341 | } |
| 342 | } else |
| 343 | netif_tx_wake_all_queues(sdata->dev); |
Johannes Berg | 078e1e6 | 2009-01-22 18:07:31 +0100 | [diff] [blame] | 344 | |
Johannes Berg | 14b8072 | 2009-02-10 21:25:42 +0100 | [diff] [blame] | 345 | /* re-enable beaconing */ |
| 346 | if (sdata->vif.type == NL80211_IFTYPE_AP || |
| 347 | sdata->vif.type == NL80211_IFTYPE_ADHOC || |
| 348 | sdata->vif.type == NL80211_IFTYPE_MESH_POINT) |
| 349 | ieee80211_if_config(sdata, |
| 350 | IEEE80211_IFCC_BEACON_ENABLED); |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 351 | } |
Johannes Berg | 078e1e6 | 2009-01-22 18:07:31 +0100 | [diff] [blame] | 352 | mutex_unlock(&local->iflist_mtx); |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 353 | |
| 354 | done: |
| 355 | ieee80211_mlme_notify_scan_completed(local); |
Johannes Berg | 4690029 | 2009-02-15 12:44:28 +0100 | [diff] [blame] | 356 | ieee80211_ibss_notify_scan_completed(local); |
Johannes Berg | 472dbc4 | 2008-09-11 00:01:49 +0200 | [diff] [blame] | 357 | ieee80211_mesh_notify_scan_completed(local); |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 358 | } |
| 359 | EXPORT_SYMBOL(ieee80211_scan_completed); |
| 360 | |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 361 | static int ieee80211_start_sw_scan(struct ieee80211_local *local) |
| 362 | { |
| 363 | struct ieee80211_sub_if_data *sdata; |
| 364 | |
| 365 | /* |
| 366 | * Hardware/driver doesn't support hw_scan, so use software |
| 367 | * scanning instead. First send a nullfunc frame with power save |
| 368 | * bit on so that AP will buffer the frames for us while we are not |
| 369 | * listening, then send probe requests to each channel and wait for |
| 370 | * the responses. After all channels are scanned, tune back to the |
| 371 | * original channel and send a nullfunc frame with power save bit |
| 372 | * off to trigger the AP to send us all the buffered frames. |
| 373 | * |
| 374 | * Note that while local->sw_scanning is true everything else but |
| 375 | * nullfunc frames and probe requests will be dropped in |
| 376 | * ieee80211_tx_h_check_assoc(). |
| 377 | */ |
| 378 | if (local->ops->sw_scan_start) |
| 379 | local->ops->sw_scan_start(local_to_hw(local)); |
| 380 | |
| 381 | mutex_lock(&local->iflist_mtx); |
| 382 | list_for_each_entry(sdata, &local->interfaces, list) { |
| 383 | if (!netif_running(sdata->dev)) |
| 384 | continue; |
| 385 | |
| 386 | /* disable beaconing */ |
| 387 | if (sdata->vif.type == NL80211_IFTYPE_AP || |
| 388 | sdata->vif.type == NL80211_IFTYPE_ADHOC || |
| 389 | sdata->vif.type == NL80211_IFTYPE_MESH_POINT) |
| 390 | ieee80211_if_config(sdata, |
| 391 | IEEE80211_IFCC_BEACON_ENABLED); |
| 392 | |
| 393 | if (sdata->vif.type == NL80211_IFTYPE_STATION) { |
| 394 | if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED) { |
| 395 | netif_tx_stop_all_queues(sdata->dev); |
| 396 | ieee80211_scan_ps_enable(sdata); |
| 397 | } |
| 398 | } else |
| 399 | netif_tx_stop_all_queues(sdata->dev); |
| 400 | } |
| 401 | mutex_unlock(&local->iflist_mtx); |
| 402 | |
| 403 | local->scan_state = SCAN_SET_CHANNEL; |
| 404 | local->scan_channel_idx = 0; |
| 405 | |
| 406 | netif_addr_lock_bh(local->mdev); |
| 407 | local->filter_flags |= FIF_BCN_PRBRESP_PROMISC; |
| 408 | local->ops->configure_filter(local_to_hw(local), |
| 409 | FIF_BCN_PRBRESP_PROMISC, |
| 410 | &local->filter_flags, |
| 411 | local->mdev->mc_count, |
| 412 | local->mdev->mc_list); |
| 413 | netif_addr_unlock_bh(local->mdev); |
| 414 | |
| 415 | /* TODO: start scan as soon as all nullfunc frames are ACKed */ |
| 416 | queue_delayed_work(local->hw.workqueue, &local->scan_work, |
| 417 | IEEE80211_CHANNEL_TIME); |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata, |
| 424 | struct cfg80211_scan_request *req) |
| 425 | { |
| 426 | struct ieee80211_local *local = sdata->local; |
| 427 | struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; |
| 428 | int rc; |
| 429 | |
| 430 | if (local->scan_req) |
| 431 | return -EBUSY; |
| 432 | |
| 433 | if (local->ops->hw_scan) { |
| 434 | u8 *ies; |
| 435 | int ielen; |
| 436 | |
| 437 | ies = kmalloc(2 + IEEE80211_MAX_SSID_LEN + |
| 438 | local->scan_ies_len + req->ie_len, GFP_KERNEL); |
| 439 | if (!ies) |
| 440 | return -ENOMEM; |
| 441 | |
| 442 | ielen = ieee80211_build_preq_ies(local, ies, |
| 443 | req->ie, req->ie_len); |
| 444 | local->orig_ies = req->ie; |
| 445 | local->orig_ies_len = req->ie_len; |
| 446 | req->ie = ies; |
| 447 | req->ie_len = ielen; |
| 448 | } |
| 449 | |
| 450 | local->scan_req = req; |
| 451 | local->scan_sdata = sdata; |
| 452 | |
| 453 | if (req != &local->int_scan_req && |
| 454 | sdata->vif.type == NL80211_IFTYPE_STATION && |
| 455 | (ifmgd->state == IEEE80211_STA_MLME_DIRECT_PROBE || |
| 456 | ifmgd->state == IEEE80211_STA_MLME_AUTHENTICATE || |
| 457 | ifmgd->state == IEEE80211_STA_MLME_ASSOCIATE)) { |
| 458 | /* actually wait for the assoc to finish/time out */ |
| 459 | set_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request); |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | if (local->ops->hw_scan) |
| 464 | local->hw_scanning = true; |
| 465 | else |
| 466 | local->sw_scanning = true; |
| 467 | /* |
| 468 | * Kicking off the scan need not be protected, |
| 469 | * only the scan variable stuff, since now |
| 470 | * local->scan_req is assigned and other callers |
| 471 | * will abort their scan attempts. |
| 472 | * |
| 473 | * This avoids getting a scan_mtx -> iflist_mtx |
| 474 | * dependency, so that the scan completed calls |
| 475 | * have more locking freedom. |
| 476 | */ |
| 477 | mutex_unlock(&local->scan_mtx); |
| 478 | |
| 479 | if (local->ops->hw_scan) |
| 480 | rc = local->ops->hw_scan(local_to_hw(local), |
| 481 | local->scan_req); |
| 482 | else |
| 483 | rc = ieee80211_start_sw_scan(local); |
| 484 | |
| 485 | mutex_lock(&local->scan_mtx); |
| 486 | |
| 487 | if (rc) { |
| 488 | if (local->ops->hw_scan) { |
| 489 | local->hw_scanning = false; |
| 490 | ieee80211_restore_scan_ies(local); |
| 491 | } else |
| 492 | local->sw_scanning = false; |
| 493 | |
| 494 | local->scan_req = NULL; |
| 495 | local->scan_sdata = NULL; |
| 496 | } |
| 497 | |
| 498 | return rc; |
| 499 | } |
| 500 | |
Johannes Berg | c2b1345 | 2008-09-11 00:01:55 +0200 | [diff] [blame] | 501 | void ieee80211_scan_work(struct work_struct *work) |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 502 | { |
| 503 | struct ieee80211_local *local = |
| 504 | container_of(work, struct ieee80211_local, scan_work.work); |
| 505 | struct ieee80211_sub_if_data *sdata = local->scan_sdata; |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 506 | struct ieee80211_channel *chan; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 507 | int skip, i; |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 508 | unsigned long next_delay = 0; |
| 509 | |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 510 | mutex_lock(&local->scan_mtx); |
| 511 | if (!sdata || !local->scan_req) { |
| 512 | mutex_unlock(&local->scan_mtx); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | if (local->scan_req && !(local->sw_scanning || local->hw_scanning)) { |
| 517 | struct cfg80211_scan_request *req = local->scan_req; |
| 518 | int rc; |
| 519 | |
| 520 | local->scan_req = NULL; |
| 521 | |
| 522 | rc = __ieee80211_start_scan(sdata, req); |
| 523 | mutex_unlock(&local->scan_mtx); |
| 524 | |
| 525 | if (rc) |
| 526 | ieee80211_scan_completed(&local->hw, true); |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | mutex_unlock(&local->scan_mtx); |
| 531 | |
Johannes Berg | 5bc7572 | 2008-09-11 00:01:51 +0200 | [diff] [blame] | 532 | /* |
| 533 | * Avoid re-scheduling when the sdata is going away. |
| 534 | */ |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 535 | if (!netif_running(sdata->dev)) { |
| 536 | ieee80211_scan_completed(&local->hw, true); |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 537 | return; |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 538 | } |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 539 | |
| 540 | switch (local->scan_state) { |
| 541 | case SCAN_SET_CHANNEL: |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 542 | /* if no more bands/channels left, complete scan */ |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 543 | if (local->scan_channel_idx >= local->scan_req->n_channels) { |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 544 | ieee80211_scan_completed(&local->hw, false); |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 545 | return; |
| 546 | } |
| 547 | skip = 0; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 548 | chan = local->scan_req->channels[local->scan_channel_idx]; |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 549 | |
| 550 | if (chan->flags & IEEE80211_CHAN_DISABLED || |
Johannes Berg | 05c914f | 2008-09-11 00:01:58 +0200 | [diff] [blame] | 551 | (sdata->vif.type == NL80211_IFTYPE_ADHOC && |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 552 | chan->flags & IEEE80211_CHAN_NO_IBSS)) |
| 553 | skip = 1; |
| 554 | |
| 555 | if (!skip) { |
| 556 | local->scan_channel = chan; |
Johannes Berg | e897558 | 2008-10-09 12:18:51 +0200 | [diff] [blame] | 557 | if (ieee80211_hw_config(local, |
| 558 | IEEE80211_CONF_CHANGE_CHANNEL)) |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 559 | skip = 1; |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /* advance state machine to next channel/band */ |
| 563 | local->scan_channel_idx++; |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 564 | |
| 565 | if (skip) |
| 566 | break; |
| 567 | |
| 568 | next_delay = IEEE80211_PROBE_DELAY + |
| 569 | usecs_to_jiffies(local->hw.channel_change_time); |
| 570 | local->scan_state = SCAN_SEND_PROBE; |
| 571 | break; |
| 572 | case SCAN_SEND_PROBE: |
| 573 | next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; |
| 574 | local->scan_state = SCAN_SET_CHANNEL; |
| 575 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 576 | if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN || |
| 577 | !local->scan_req->n_ssids) |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 578 | break; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 579 | for (i = 0; i < local->scan_req->n_ssids; i++) |
| 580 | ieee80211_send_probe_req( |
| 581 | sdata, NULL, |
| 582 | local->scan_req->ssids[i].ssid, |
Jouni Malinen | 70692ad | 2009-02-16 19:39:13 +0200 | [diff] [blame] | 583 | local->scan_req->ssids[i].ssid_len, |
| 584 | local->scan_req->ie, local->scan_req->ie_len); |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 585 | next_delay = IEEE80211_CHANNEL_TIME; |
| 586 | break; |
| 587 | } |
| 588 | |
Johannes Berg | 5bc7572 | 2008-09-11 00:01:51 +0200 | [diff] [blame] | 589 | queue_delayed_work(local->hw.workqueue, &local->scan_work, |
| 590 | next_delay); |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 591 | } |
| 592 | |
Johannes Berg | c2b1345 | 2008-09-11 00:01:55 +0200 | [diff] [blame] | 593 | int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata, |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 594 | struct cfg80211_scan_request *req) |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 595 | { |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 596 | int res; |
| 597 | |
| 598 | mutex_lock(&sdata->local->scan_mtx); |
| 599 | res = __ieee80211_start_scan(sdata, req); |
| 600 | mutex_unlock(&sdata->local->scan_mtx); |
| 601 | |
| 602 | return res; |
| 603 | } |
| 604 | |
| 605 | int ieee80211_request_internal_scan(struct ieee80211_sub_if_data *sdata, |
| 606 | const u8 *ssid, u8 ssid_len) |
| 607 | { |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 608 | struct ieee80211_local *local = sdata->local; |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 609 | int ret = -EBUSY; |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 610 | |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 611 | mutex_lock(&local->scan_mtx); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 612 | |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 613 | /* busy scanning */ |
| 614 | if (local->scan_req) |
| 615 | goto unlock; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 616 | |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 617 | memcpy(local->int_scan_req.ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN); |
| 618 | local->int_scan_req.ssids[0].ssid_len = ssid_len; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 619 | |
Johannes Berg | f3b8525 | 2009-04-23 16:01:47 +0200 | [diff] [blame^] | 620 | ret = __ieee80211_start_scan(sdata, &sdata->local->int_scan_req); |
| 621 | unlock: |
| 622 | mutex_unlock(&local->scan_mtx); |
| 623 | return ret; |
Johannes Berg | 0a51b27 | 2008-09-08 17:44:25 +0200 | [diff] [blame] | 624 | } |