10a51b27eSJohannes Berg /* 25484e237SJohannes Berg * Scanning implementation 35484e237SJohannes Berg * 40a51b27eSJohannes Berg * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi> 50a51b27eSJohannes Berg * Copyright 2004, Instant802 Networks, Inc. 60a51b27eSJohannes Berg * Copyright 2005, Devicescape Software, Inc. 70a51b27eSJohannes Berg * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 80a51b27eSJohannes Berg * Copyright 2007, Michael Wu <flamingice@sourmilk.net> 90a51b27eSJohannes Berg * 100a51b27eSJohannes Berg * This program is free software; you can redistribute it and/or modify 110a51b27eSJohannes Berg * it under the terms of the GNU General Public License version 2 as 120a51b27eSJohannes Berg * published by the Free Software Foundation. 130a51b27eSJohannes Berg */ 140a51b27eSJohannes Berg 150a51b27eSJohannes Berg #include <linux/if_arp.h> 16078e1e60SJohannes Berg #include <linux/rtnetlink.h> 17e8db0be1SJean Pihet #include <linux/pm_qos.h> 18df13cce5SHelmut Schaa #include <net/sch_generic.h> 195a0e3ad6STejun Heo #include <linux/slab.h> 20bc3b2d7fSPaul Gortmaker #include <linux/export.h> 210a51b27eSJohannes Berg #include <net/mac80211.h> 220a51b27eSJohannes Berg 230a51b27eSJohannes Berg #include "ieee80211_i.h" 2424487981SJohannes Berg #include "driver-ops.h" 255484e237SJohannes Berg #include "mesh.h" 260a51b27eSJohannes Berg 270a51b27eSJohannes Berg #define IEEE80211_PROBE_DELAY (HZ / 33) 280a51b27eSJohannes Berg #define IEEE80211_CHANNEL_TIME (HZ / 33) 2996f7e739SHelmut Schaa #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 8) 300a51b27eSJohannes Berg 31c2b13452SJohannes Berg struct ieee80211_bss * 325484e237SJohannes Berg ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq, 335484e237SJohannes Berg u8 *ssid, u8 ssid_len) 345484e237SJohannes Berg { 350c1ad2caSJohannes Berg struct cfg80211_bss *cbss; 360c1ad2caSJohannes Berg 370c1ad2caSJohannes Berg cbss = cfg80211_get_bss(local->hw.wiphy, 380c1ad2caSJohannes Berg ieee80211_get_channel(local->hw.wiphy, freq), 390c1ad2caSJohannes Berg bssid, ssid, ssid_len, 0, 0); 400c1ad2caSJohannes Berg if (!cbss) 410c1ad2caSJohannes Berg return NULL; 420c1ad2caSJohannes Berg return (void *)cbss->priv; 435484e237SJohannes Berg } 445484e237SJohannes Berg 4500d3f14cSJohannes Berg static void ieee80211_rx_bss_free(struct cfg80211_bss *cbss) 465484e237SJohannes Berg { 470c1ad2caSJohannes Berg struct ieee80211_bss *bss = (void *)cbss->priv; 485484e237SJohannes Berg 495484e237SJohannes Berg kfree(bss_mesh_id(bss)); 505484e237SJohannes Berg kfree(bss_mesh_cfg(bss)); 515484e237SJohannes Berg } 525484e237SJohannes Berg 535484e237SJohannes Berg void ieee80211_rx_bss_put(struct ieee80211_local *local, 54c2b13452SJohannes Berg struct ieee80211_bss *bss) 555484e237SJohannes Berg { 560c1ad2caSJohannes Berg if (!bss) 570c1ad2caSJohannes Berg return; 580c1ad2caSJohannes Berg cfg80211_put_bss(container_of((void *)bss, struct cfg80211_bss, priv)); 595484e237SJohannes Berg } 605484e237SJohannes Berg 61ab13315aSKalle Valo static bool is_uapsd_supported(struct ieee802_11_elems *elems) 62ab13315aSKalle Valo { 63ab13315aSKalle Valo u8 qos_info; 64ab13315aSKalle Valo 65ab13315aSKalle Valo if (elems->wmm_info && elems->wmm_info_len == 7 66ab13315aSKalle Valo && elems->wmm_info[5] == 1) 67ab13315aSKalle Valo qos_info = elems->wmm_info[6]; 68ab13315aSKalle Valo else if (elems->wmm_param && elems->wmm_param_len == 24 69ab13315aSKalle Valo && elems->wmm_param[5] == 1) 70ab13315aSKalle Valo qos_info = elems->wmm_param[6]; 71ab13315aSKalle Valo else 72ab13315aSKalle Valo /* no valid wmm information or parameter element found */ 73ab13315aSKalle Valo return false; 74ab13315aSKalle Valo 75ab13315aSKalle Valo return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD; 76ab13315aSKalle Valo } 77ab13315aSKalle Valo 78c2b13452SJohannes Berg struct ieee80211_bss * 795484e237SJohannes Berg ieee80211_bss_info_update(struct ieee80211_local *local, 805484e237SJohannes Berg struct ieee80211_rx_status *rx_status, 815484e237SJohannes Berg struct ieee80211_mgmt *mgmt, 825484e237SJohannes Berg size_t len, 835484e237SJohannes Berg struct ieee802_11_elems *elems, 842a519311SJohannes Berg struct ieee80211_channel *channel, 852a519311SJohannes Berg bool beacon) 865484e237SJohannes Berg { 870c1ad2caSJohannes Berg struct cfg80211_bss *cbss; 88c2b13452SJohannes Berg struct ieee80211_bss *bss; 89f0b058b6SStanislaw Gruszka int clen, srlen; 902a519311SJohannes Berg s32 signal = 0; 912a519311SJohannes Berg 9277965c97SJohannes Berg if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) 932a519311SJohannes Berg signal = rx_status->signal * 100; 9477965c97SJohannes Berg else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC) 952a519311SJohannes Berg signal = (rx_status->signal * 100) / local->hw.max_signal; 962a519311SJohannes Berg 970c1ad2caSJohannes Berg cbss = cfg80211_inform_bss_frame(local->hw.wiphy, channel, 9877965c97SJohannes Berg mgmt, len, signal, GFP_ATOMIC); 995484e237SJohannes Berg 1000c1ad2caSJohannes Berg if (!cbss) 1015484e237SJohannes Berg return NULL; 10200d3f14cSJohannes Berg 1030c1ad2caSJohannes Berg cbss->free_priv = ieee80211_rx_bss_free; 1040c1ad2caSJohannes Berg bss = (void *)cbss->priv; 1055484e237SJohannes Berg 1065484e237SJohannes Berg /* save the ERP value so that it is available at association time */ 1075484e237SJohannes Berg if (elems->erp_info && elems->erp_info_len >= 1) { 1085484e237SJohannes Berg bss->erp_value = elems->erp_info[0]; 1095484e237SJohannes Berg bss->has_erp_value = 1; 1105484e237SJohannes Berg } 1115484e237SJohannes Berg 1125484e237SJohannes Berg if (elems->tim) { 1135484e237SJohannes Berg struct ieee80211_tim_ie *tim_ie = 1145484e237SJohannes Berg (struct ieee80211_tim_ie *)elems->tim; 1155484e237SJohannes Berg bss->dtim_period = tim_ie->dtim_period; 1165484e237SJohannes Berg } 1175484e237SJohannes Berg 118e5b900d2SJohannes Berg /* If the beacon had no TIM IE, or it was invalid, use 1 */ 119e5b900d2SJohannes Berg if (beacon && !bss->dtim_period) 120e5b900d2SJohannes Berg bss->dtim_period = 1; 121e5b900d2SJohannes Berg 122f0b058b6SStanislaw Gruszka /* replace old supported rates if we get new values */ 123f0b058b6SStanislaw Gruszka srlen = 0; 1245484e237SJohannes Berg if (elems->supp_rates) { 125f0b058b6SStanislaw Gruszka clen = IEEE80211_MAX_SUPP_RATES; 1265484e237SJohannes Berg if (clen > elems->supp_rates_len) 1275484e237SJohannes Berg clen = elems->supp_rates_len; 128f0b058b6SStanislaw Gruszka memcpy(bss->supp_rates, elems->supp_rates, clen); 129f0b058b6SStanislaw Gruszka srlen += clen; 1305484e237SJohannes Berg } 1315484e237SJohannes Berg if (elems->ext_supp_rates) { 132f0b058b6SStanislaw Gruszka clen = IEEE80211_MAX_SUPP_RATES - srlen; 1335484e237SJohannes Berg if (clen > elems->ext_supp_rates_len) 1345484e237SJohannes Berg clen = elems->ext_supp_rates_len; 135f0b058b6SStanislaw Gruszka memcpy(bss->supp_rates + srlen, elems->ext_supp_rates, clen); 136f0b058b6SStanislaw Gruszka srlen += clen; 1375484e237SJohannes Berg } 138f0b058b6SStanislaw Gruszka if (srlen) 139f0b058b6SStanislaw Gruszka bss->supp_rates_len = srlen; 1405484e237SJohannes Berg 1415484e237SJohannes Berg bss->wmm_used = elems->wmm_param || elems->wmm_info; 142ab13315aSKalle Valo bss->uapsd_supported = is_uapsd_supported(elems); 1435484e237SJohannes Berg 1445484e237SJohannes Berg if (!beacon) 1455484e237SJohannes Berg bss->last_probe_resp = jiffies; 1465484e237SJohannes Berg 1475484e237SJohannes Berg return bss; 1485484e237SJohannes Berg } 1490a51b27eSJohannes Berg 15098c8fccfSJohannes Berg ieee80211_rx_result 151f1d58c25SJohannes Berg ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb) 15298c8fccfSJohannes Berg { 153f1d58c25SJohannes Berg struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb); 15498c8fccfSJohannes Berg struct ieee80211_mgmt *mgmt; 155c2b13452SJohannes Berg struct ieee80211_bss *bss; 15698c8fccfSJohannes Berg u8 *elements; 15798c8fccfSJohannes Berg struct ieee80211_channel *channel; 15898c8fccfSJohannes Berg size_t baselen; 15998c8fccfSJohannes Berg int freq; 16098c8fccfSJohannes Berg __le16 fc; 16198c8fccfSJohannes Berg bool presp, beacon = false; 16298c8fccfSJohannes Berg struct ieee802_11_elems elems; 16398c8fccfSJohannes Berg 16498c8fccfSJohannes Berg if (skb->len < 2) 16598c8fccfSJohannes Berg return RX_DROP_UNUSABLE; 16698c8fccfSJohannes Berg 16798c8fccfSJohannes Berg mgmt = (struct ieee80211_mgmt *) skb->data; 16898c8fccfSJohannes Berg fc = mgmt->frame_control; 16998c8fccfSJohannes Berg 17098c8fccfSJohannes Berg if (ieee80211_is_ctl(fc)) 17198c8fccfSJohannes Berg return RX_CONTINUE; 17298c8fccfSJohannes Berg 17398c8fccfSJohannes Berg if (skb->len < 24) 174306fe938SLuciano Coelho return RX_CONTINUE; 17598c8fccfSJohannes Berg 17698c8fccfSJohannes Berg presp = ieee80211_is_probe_resp(fc); 17798c8fccfSJohannes Berg if (presp) { 17898c8fccfSJohannes Berg /* ignore ProbeResp to foreign address */ 17947846c9bSJohannes Berg if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN)) 18098c8fccfSJohannes Berg return RX_DROP_MONITOR; 18198c8fccfSJohannes Berg 18298c8fccfSJohannes Berg presp = true; 18398c8fccfSJohannes Berg elements = mgmt->u.probe_resp.variable; 18498c8fccfSJohannes Berg baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable); 18598c8fccfSJohannes Berg } else { 18698c8fccfSJohannes Berg beacon = ieee80211_is_beacon(fc); 18798c8fccfSJohannes Berg baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable); 18898c8fccfSJohannes Berg elements = mgmt->u.beacon.variable; 18998c8fccfSJohannes Berg } 19098c8fccfSJohannes Berg 19198c8fccfSJohannes Berg if (!presp && !beacon) 19298c8fccfSJohannes Berg return RX_CONTINUE; 19398c8fccfSJohannes Berg 19498c8fccfSJohannes Berg if (baselen > skb->len) 19598c8fccfSJohannes Berg return RX_DROP_MONITOR; 19698c8fccfSJohannes Berg 19798c8fccfSJohannes Berg ieee802_11_parse_elems(elements, skb->len - baselen, &elems); 19898c8fccfSJohannes Berg 19998c8fccfSJohannes Berg if (elems.ds_params && elems.ds_params_len == 1) 20059eb21a6SBruno Randolf freq = ieee80211_channel_to_frequency(elems.ds_params[0], 20159eb21a6SBruno Randolf rx_status->band); 20298c8fccfSJohannes Berg else 20398c8fccfSJohannes Berg freq = rx_status->freq; 20498c8fccfSJohannes Berg 20598c8fccfSJohannes Berg channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq); 20698c8fccfSJohannes Berg 20798c8fccfSJohannes Berg if (!channel || channel->flags & IEEE80211_CHAN_DISABLED) 20898c8fccfSJohannes Berg return RX_DROP_MONITOR; 20998c8fccfSJohannes Berg 21098c8fccfSJohannes Berg bss = ieee80211_bss_info_update(sdata->local, rx_status, 21198c8fccfSJohannes Berg mgmt, skb->len, &elems, 2122a519311SJohannes Berg channel, beacon); 213d048e503SJouni Malinen if (bss) 21498c8fccfSJohannes Berg ieee80211_rx_bss_put(sdata->local, bss); 21598c8fccfSJohannes Berg 21607ef03eeSJohannes Berg if (channel == sdata->local->oper_channel) 217b23b025fSBen Greear return RX_CONTINUE; 218b23b025fSBen Greear 21998c8fccfSJohannes Berg dev_kfree_skb(skb); 22098c8fccfSJohannes Berg return RX_QUEUED; 22198c8fccfSJohannes Berg } 22298c8fccfSJohannes Berg 2234d36ec58SJohannes Berg /* return false if no more work */ 2244d36ec58SJohannes Berg static bool ieee80211_prep_hw_scan(struct ieee80211_local *local) 2254d36ec58SJohannes Berg { 2264d36ec58SJohannes Berg struct cfg80211_scan_request *req = local->scan_req; 2274d36ec58SJohannes Berg enum ieee80211_band band; 2284d36ec58SJohannes Berg int i, ielen, n_chans; 2294d36ec58SJohannes Berg 2304d36ec58SJohannes Berg do { 2314d36ec58SJohannes Berg if (local->hw_scan_band == IEEE80211_NUM_BANDS) 2324d36ec58SJohannes Berg return false; 2334d36ec58SJohannes Berg 2344d36ec58SJohannes Berg band = local->hw_scan_band; 2354d36ec58SJohannes Berg n_chans = 0; 2364d36ec58SJohannes Berg for (i = 0; i < req->n_channels; i++) { 2374d36ec58SJohannes Berg if (req->channels[i]->band == band) { 2384d36ec58SJohannes Berg local->hw_scan_req->channels[n_chans] = 2394d36ec58SJohannes Berg req->channels[i]; 2404d36ec58SJohannes Berg n_chans++; 2414d36ec58SJohannes Berg } 2424d36ec58SJohannes Berg } 2434d36ec58SJohannes Berg 2444d36ec58SJohannes Berg local->hw_scan_band++; 2454d36ec58SJohannes Berg } while (!n_chans); 2464d36ec58SJohannes Berg 2474d36ec58SJohannes Berg local->hw_scan_req->n_channels = n_chans; 2484d36ec58SJohannes Berg 2494d36ec58SJohannes Berg ielen = ieee80211_build_preq_ies(local, (u8 *)local->hw_scan_req->ie, 2508ee31080SJohannes Berg req->ie, req->ie_len, band, 25185a237feSJohannes Berg req->rates[band], 0); 2524d36ec58SJohannes Berg local->hw_scan_req->ie_len = ielen; 253dcd83976SJohannes Berg local->hw_scan_req->no_cck = req->no_cck; 2544d36ec58SJohannes Berg 2554d36ec58SJohannes Berg return true; 2564d36ec58SJohannes Berg } 2574d36ec58SJohannes Berg 258d07bfd8bSJohannes Berg static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted, 259e229f844SStanislaw Gruszka bool was_hw_scan) 260f3b85252SJohannes Berg { 261f3b85252SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 262f3b85252SJohannes Berg 263e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 264f3b85252SJohannes Berg 2656d3560d4SJohannes Berg /* 2666d3560d4SJohannes Berg * It's ok to abort a not-yet-running scan (that 2676d3560d4SJohannes Berg * we have one at all will be verified by checking 2686d3560d4SJohannes Berg * local->scan_req next), but not to complete it 2696d3560d4SJohannes Berg * successfully. 2706d3560d4SJohannes Berg */ 2716d3560d4SJohannes Berg if (WARN_ON(!local->scanning && !aborted)) 2726d3560d4SJohannes Berg aborted = true; 273f3b85252SJohannes Berg 274e229f844SStanislaw Gruszka if (WARN_ON(!local->scan_req)) 275d07bfd8bSJohannes Berg return; 276f3b85252SJohannes Berg 2774d36ec58SJohannes Berg if (was_hw_scan && !aborted && ieee80211_prep_hw_scan(local)) { 2786eb11a9aSStanislaw Gruszka int rc = drv_hw_scan(local, local->scan_sdata, local->hw_scan_req); 2796eb11a9aSStanislaw Gruszka if (rc == 0) 280d07bfd8bSJohannes Berg return; 2814d36ec58SJohannes Berg } 2824d36ec58SJohannes Berg 2834d36ec58SJohannes Berg kfree(local->hw_scan_req); 2844d36ec58SJohannes Berg local->hw_scan_req = NULL; 285f3b85252SJohannes Berg 2865ba63533SJohannes Berg if (local->scan_req != local->int_scan_req) 2872a519311SJohannes Berg cfg80211_scan_done(local->scan_req, aborted); 2882a519311SJohannes Berg local->scan_req = NULL; 28915db0b7fSJohannes Berg local->scan_sdata = NULL; 2902a519311SJohannes Berg 291fbe9c429SHelmut Schaa local->scanning = 0; 29258905ca5SJohannes Berg local->scan_channel = NULL; 293f3b85252SJohannes Berg 29459bdf3b0SBen Greear /* Set power back to normal operating levels. */ 29559bdf3b0SBen Greear ieee80211_hw_config(local, 0); 296b23b025fSBen Greear 297b23b025fSBen Greear if (!was_hw_scan) { 298b23b025fSBen Greear ieee80211_configure_filter(local); 299b23b025fSBen Greear drv_sw_scan_complete(local); 300*e76aadc5SJohannes Berg ieee80211_offchannel_return(local, true); 301b23b025fSBen Greear } 302b23b025fSBen Greear 3035cff20e6SJohannes Berg ieee80211_recalc_idle(local); 304e229f844SStanislaw Gruszka 3050a51b27eSJohannes Berg ieee80211_mlme_notify_scan_completed(local); 30646900298SJohannes Berg ieee80211_ibss_notify_scan_completed(local); 307472dbc45SJohannes Berg ieee80211_mesh_notify_scan_completed(local); 30881ac3462SJohannes Berg ieee80211_queue_work(&local->hw, &local->work_work); 3090a51b27eSJohannes Berg } 3108789d459SJohannes Berg 3118789d459SJohannes Berg void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted) 3128789d459SJohannes Berg { 3138789d459SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 3148789d459SJohannes Berg 3158789d459SJohannes Berg trace_api_scan_completed(local, aborted); 3168789d459SJohannes Berg 3178789d459SJohannes Berg set_bit(SCAN_COMPLETED, &local->scanning); 3188789d459SJohannes Berg if (aborted) 3198789d459SJohannes Berg set_bit(SCAN_ABORTED, &local->scanning); 3208789d459SJohannes Berg ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0); 3218789d459SJohannes Berg } 3220a51b27eSJohannes Berg EXPORT_SYMBOL(ieee80211_scan_completed); 3230a51b27eSJohannes Berg 324f3b85252SJohannes Berg static int ieee80211_start_sw_scan(struct ieee80211_local *local) 325f3b85252SJohannes Berg { 326f3b85252SJohannes Berg /* 327f3b85252SJohannes Berg * Hardware/driver doesn't support hw_scan, so use software 328f3b85252SJohannes Berg * scanning instead. First send a nullfunc frame with power save 329f3b85252SJohannes Berg * bit on so that AP will buffer the frames for us while we are not 330f3b85252SJohannes Berg * listening, then send probe requests to each channel and wait for 331f3b85252SJohannes Berg * the responses. After all channels are scanned, tune back to the 332f3b85252SJohannes Berg * original channel and send a nullfunc frame with power save bit 333f3b85252SJohannes Berg * off to trigger the AP to send us all the buffered frames. 334f3b85252SJohannes Berg * 335f3b85252SJohannes Berg * Note that while local->sw_scanning is true everything else but 336f3b85252SJohannes Berg * nullfunc frames and probe requests will be dropped in 337f3b85252SJohannes Berg * ieee80211_tx_h_check_assoc(). 338f3b85252SJohannes Berg */ 33924487981SJohannes Berg drv_sw_scan_start(local); 340f3b85252SJohannes Berg 341df13cce5SHelmut Schaa local->leave_oper_channel_time = 0; 342977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 343f3b85252SJohannes Berg local->scan_channel_idx = 0; 344f3b85252SJohannes Berg 34507ef03eeSJohannes Berg ieee80211_offchannel_stop_vifs(local, true); 346a80f7c0bSJohannes Berg 3473ac64beeSJohannes Berg ieee80211_configure_filter(local); 348f3b85252SJohannes Berg 34959bdf3b0SBen Greear /* We need to set power level at maximum rate for scanning. */ 35059bdf3b0SBen Greear ieee80211_hw_config(local, 0); 35159bdf3b0SBen Greear 35242935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, 35307ef03eeSJohannes Berg &local->scan_work, 0); 354f3b85252SJohannes Berg 355f3b85252SJohannes Berg return 0; 356f3b85252SJohannes Berg } 357f3b85252SJohannes Berg 358f3b85252SJohannes Berg 359f3b85252SJohannes Berg static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata, 360f3b85252SJohannes Berg struct cfg80211_scan_request *req) 361f3b85252SJohannes Berg { 362f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 363f3b85252SJohannes Berg int rc; 364f3b85252SJohannes Berg 365e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 366e229f844SStanislaw Gruszka 367f3b85252SJohannes Berg if (local->scan_req) 368f3b85252SJohannes Berg return -EBUSY; 369f3b85252SJohannes Berg 3706e7e6213SJohn W. Linville if (!list_empty(&local->work_list)) { 3716e7e6213SJohn W. Linville /* wait for the work to finish/time out */ 372c0ce77b8SJohannes Berg local->scan_req = req; 373c0ce77b8SJohannes Berg local->scan_sdata = sdata; 374c0ce77b8SJohannes Berg return 0; 375c0ce77b8SJohannes Berg } 376c0ce77b8SJohannes Berg 377f3b85252SJohannes Berg if (local->ops->hw_scan) { 378f3b85252SJohannes Berg u8 *ies; 379f3b85252SJohannes Berg 3804d36ec58SJohannes Berg local->hw_scan_req = kmalloc( 3814d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 3824d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]) + 3834d36ec58SJohannes Berg 2 + IEEE80211_MAX_SSID_LEN + local->scan_ies_len + 3844d36ec58SJohannes Berg req->ie_len, GFP_KERNEL); 3854d36ec58SJohannes Berg if (!local->hw_scan_req) 386f3b85252SJohannes Berg return -ENOMEM; 387f3b85252SJohannes Berg 3884d36ec58SJohannes Berg local->hw_scan_req->ssids = req->ssids; 3894d36ec58SJohannes Berg local->hw_scan_req->n_ssids = req->n_ssids; 3904d36ec58SJohannes Berg ies = (u8 *)local->hw_scan_req + 3914d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 3924d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]); 3934d36ec58SJohannes Berg local->hw_scan_req->ie = ies; 3944d36ec58SJohannes Berg 3954d36ec58SJohannes Berg local->hw_scan_band = 0; 3966e7e6213SJohn W. Linville 3976e7e6213SJohn W. Linville /* 3986e7e6213SJohn W. Linville * After allocating local->hw_scan_req, we must 3996e7e6213SJohn W. Linville * go through until ieee80211_prep_hw_scan(), so 4006e7e6213SJohn W. Linville * anything that might be changed here and leave 4016e7e6213SJohn W. Linville * this function early must not go after this 4026e7e6213SJohn W. Linville * allocation. 4036e7e6213SJohn W. Linville */ 404f3b85252SJohannes Berg } 405f3b85252SJohannes Berg 406f3b85252SJohannes Berg local->scan_req = req; 407f3b85252SJohannes Berg local->scan_sdata = sdata; 408f3b85252SJohannes Berg 409f3b85252SJohannes Berg if (local->ops->hw_scan) 410fbe9c429SHelmut Schaa __set_bit(SCAN_HW_SCANNING, &local->scanning); 411f3b85252SJohannes Berg else 412fbe9c429SHelmut Schaa __set_bit(SCAN_SW_SCANNING, &local->scanning); 4136e7e6213SJohn W. Linville 4145cff20e6SJohannes Berg ieee80211_recalc_idle(local); 415f3b85252SJohannes Berg 4164d36ec58SJohannes Berg if (local->ops->hw_scan) { 4174d36ec58SJohannes Berg WARN_ON(!ieee80211_prep_hw_scan(local)); 418a060bbfeSJohannes Berg rc = drv_hw_scan(local, sdata, local->hw_scan_req); 4194d36ec58SJohannes Berg } else 420f3b85252SJohannes Berg rc = ieee80211_start_sw_scan(local); 421f3b85252SJohannes Berg 422f3b85252SJohannes Berg if (rc) { 4234d36ec58SJohannes Berg kfree(local->hw_scan_req); 4244d36ec58SJohannes Berg local->hw_scan_req = NULL; 425fbe9c429SHelmut Schaa local->scanning = 0; 426f3b85252SJohannes Berg 4275cff20e6SJohannes Berg ieee80211_recalc_idle(local); 4285cff20e6SJohannes Berg 429f3b85252SJohannes Berg local->scan_req = NULL; 430f3b85252SJohannes Berg local->scan_sdata = NULL; 431f3b85252SJohannes Berg } 432f3b85252SJohannes Berg 433f3b85252SJohannes Berg return rc; 434f3b85252SJohannes Berg } 435f3b85252SJohannes Berg 436df13cce5SHelmut Schaa static unsigned long 437df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(struct ieee80211_channel *chan) 438df13cce5SHelmut Schaa { 439df13cce5SHelmut Schaa /* 440df13cce5SHelmut Schaa * TODO: channel switching also consumes quite some time, 441df13cce5SHelmut Schaa * add that delay as well to get a better estimation 442df13cce5SHelmut Schaa */ 443df13cce5SHelmut Schaa if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) 444df13cce5SHelmut Schaa return IEEE80211_PASSIVE_CHANNEL_TIME; 445df13cce5SHelmut Schaa return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME; 446df13cce5SHelmut Schaa } 447df13cce5SHelmut Schaa 448e229f844SStanislaw Gruszka static void ieee80211_scan_state_decision(struct ieee80211_local *local, 4492fb3f028SHelmut Schaa unsigned long *next_delay) 4502fb3f028SHelmut Schaa { 451142b9f50SHelmut Schaa bool associated = false; 452df13cce5SHelmut Schaa bool tx_empty = true; 453df13cce5SHelmut Schaa bool bad_latency; 454df13cce5SHelmut Schaa bool listen_int_exceeded; 455df13cce5SHelmut Schaa unsigned long min_beacon_int = 0; 456142b9f50SHelmut Schaa struct ieee80211_sub_if_data *sdata; 457df13cce5SHelmut Schaa struct ieee80211_channel *next_chan; 458142b9f50SHelmut Schaa 459df13cce5SHelmut Schaa /* 460df13cce5SHelmut Schaa * check if at least one STA interface is associated, 461df13cce5SHelmut Schaa * check if at least one STA interface has pending tx frames 462df13cce5SHelmut Schaa * and grab the lowest used beacon interval 463df13cce5SHelmut Schaa */ 464142b9f50SHelmut Schaa mutex_lock(&local->iflist_mtx); 465142b9f50SHelmut Schaa list_for_each_entry(sdata, &local->interfaces, list) { 4669607e6b6SJohannes Berg if (!ieee80211_sdata_running(sdata)) 467142b9f50SHelmut Schaa continue; 468142b9f50SHelmut Schaa 469142b9f50SHelmut Schaa if (sdata->vif.type == NL80211_IFTYPE_STATION) { 470142b9f50SHelmut Schaa if (sdata->u.mgd.associated) { 471142b9f50SHelmut Schaa associated = true; 472df13cce5SHelmut Schaa 473df13cce5SHelmut Schaa if (sdata->vif.bss_conf.beacon_int < 474df13cce5SHelmut Schaa min_beacon_int || min_beacon_int == 0) 475df13cce5SHelmut Schaa min_beacon_int = 476df13cce5SHelmut Schaa sdata->vif.bss_conf.beacon_int; 477df13cce5SHelmut Schaa 478df13cce5SHelmut Schaa if (!qdisc_all_tx_empty(sdata->dev)) { 479df13cce5SHelmut Schaa tx_empty = false; 480142b9f50SHelmut Schaa break; 481142b9f50SHelmut Schaa } 482142b9f50SHelmut Schaa } 483142b9f50SHelmut Schaa } 484df13cce5SHelmut Schaa } 485142b9f50SHelmut Schaa mutex_unlock(&local->iflist_mtx); 486142b9f50SHelmut Schaa 487b23b025fSBen Greear next_chan = local->scan_req->channels[local->scan_channel_idx]; 488b23b025fSBen Greear 489142b9f50SHelmut Schaa /* 490142b9f50SHelmut Schaa * we're currently scanning a different channel, let's 491df13cce5SHelmut Schaa * see if we can scan another channel without interfering 492df13cce5SHelmut Schaa * with the current traffic situation. 493df13cce5SHelmut Schaa * 494df13cce5SHelmut Schaa * Since we don't know if the AP has pending frames for us 495df13cce5SHelmut Schaa * we can only check for our tx queues and use the current 496df13cce5SHelmut Schaa * pm_qos requirements for rx. Hence, if no tx traffic occurs 497df13cce5SHelmut Schaa * at all we will scan as many channels in a row as the pm_qos 498df13cce5SHelmut Schaa * latency allows us to. Additionally we also check for the 499df13cce5SHelmut Schaa * currently negotiated listen interval to prevent losing 500df13cce5SHelmut Schaa * frames unnecessarily. 501df13cce5SHelmut Schaa * 502df13cce5SHelmut Schaa * Otherwise switch back to the operating channel. 503142b9f50SHelmut Schaa */ 504df13cce5SHelmut Schaa 505df13cce5SHelmut Schaa bad_latency = time_after(jiffies + 506df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(next_chan), 507df13cce5SHelmut Schaa local->leave_oper_channel_time + 508f8965467SLinus Torvalds usecs_to_jiffies(pm_qos_request(PM_QOS_NETWORK_LATENCY))); 509df13cce5SHelmut Schaa 510df13cce5SHelmut Schaa listen_int_exceeded = time_after(jiffies + 511df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(next_chan), 512df13cce5SHelmut Schaa local->leave_oper_channel_time + 513df13cce5SHelmut Schaa usecs_to_jiffies(min_beacon_int * 1024) * 514df13cce5SHelmut Schaa local->hw.conf.listen_interval); 515df13cce5SHelmut Schaa 51607ef03eeSJohannes Berg if (associated && (!tx_empty || bad_latency || listen_int_exceeded)) 51707ef03eeSJohannes Berg local->next_scan_state = SCAN_SUSPEND; 518142b9f50SHelmut Schaa else 519977923b0SHelmut Schaa local->next_scan_state = SCAN_SET_CHANNEL; 520142b9f50SHelmut Schaa 521142b9f50SHelmut Schaa *next_delay = 0; 5222fb3f028SHelmut Schaa } 5232fb3f028SHelmut Schaa 5242fb3f028SHelmut Schaa static void ieee80211_scan_state_set_channel(struct ieee80211_local *local, 5257d3be3ccSHelmut Schaa unsigned long *next_delay) 5267d3be3ccSHelmut Schaa { 5277d3be3ccSHelmut Schaa int skip; 5287d3be3ccSHelmut Schaa struct ieee80211_channel *chan; 5297d3be3ccSHelmut Schaa 5307d3be3ccSHelmut Schaa skip = 0; 5317d3be3ccSHelmut Schaa chan = local->scan_req->channels[local->scan_channel_idx]; 5327d3be3ccSHelmut Schaa 5337d3be3ccSHelmut Schaa local->scan_channel = chan; 534b23b025fSBen Greear 535584991dcSJohannes Berg if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL)) 5367d3be3ccSHelmut Schaa skip = 1; 5377d3be3ccSHelmut Schaa 5387d3be3ccSHelmut Schaa /* advance state machine to next channel/band */ 5397d3be3ccSHelmut Schaa local->scan_channel_idx++; 5407d3be3ccSHelmut Schaa 5410ee9c13cSHelmut Schaa if (skip) { 5420ee9c13cSHelmut Schaa /* if we skip this channel return to the decision state */ 5430ee9c13cSHelmut Schaa local->next_scan_state = SCAN_DECISION; 5442fb3f028SHelmut Schaa return; 5450ee9c13cSHelmut Schaa } 5467d3be3ccSHelmut Schaa 5477d3be3ccSHelmut Schaa /* 5487d3be3ccSHelmut Schaa * Probe delay is used to update the NAV, cf. 11.1.3.2.2 5497d3be3ccSHelmut Schaa * (which unfortunately doesn't say _why_ step a) is done, 5507d3be3ccSHelmut Schaa * but it waits for the probe delay or until a frame is 5517d3be3ccSHelmut Schaa * received - and the received frame would update the NAV). 5527d3be3ccSHelmut Schaa * For now, we do not support waiting until a frame is 5537d3be3ccSHelmut Schaa * received. 5547d3be3ccSHelmut Schaa * 5557d3be3ccSHelmut Schaa * In any case, it is not necessary for a passive scan. 5567d3be3ccSHelmut Schaa */ 5577d3be3ccSHelmut Schaa if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN || 5587d3be3ccSHelmut Schaa !local->scan_req->n_ssids) { 5597d3be3ccSHelmut Schaa *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 560977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 5612fb3f028SHelmut Schaa return; 5627d3be3ccSHelmut Schaa } 5637d3be3ccSHelmut Schaa 5642fb3f028SHelmut Schaa /* active scan, send probes */ 5657d3be3ccSHelmut Schaa *next_delay = IEEE80211_PROBE_DELAY; 566977923b0SHelmut Schaa local->next_scan_state = SCAN_SEND_PROBE; 5677d3be3ccSHelmut Schaa } 5687d3be3ccSHelmut Schaa 5697d3be3ccSHelmut Schaa static void ieee80211_scan_state_send_probe(struct ieee80211_local *local, 5707d3be3ccSHelmut Schaa unsigned long *next_delay) 5717d3be3ccSHelmut Schaa { 5727d3be3ccSHelmut Schaa int i; 5737d3be3ccSHelmut Schaa struct ieee80211_sub_if_data *sdata = local->scan_sdata; 57485a237feSJohannes Berg enum ieee80211_band band = local->hw.conf.channel->band; 5757d3be3ccSHelmut Schaa 5767d3be3ccSHelmut Schaa for (i = 0; i < local->scan_req->n_ssids; i++) 5777d3be3ccSHelmut Schaa ieee80211_send_probe_req( 5787d3be3ccSHelmut Schaa sdata, NULL, 5797d3be3ccSHelmut Schaa local->scan_req->ssids[i].ssid, 5807d3be3ccSHelmut Schaa local->scan_req->ssids[i].ssid_len, 581a806c558SPaul Stewart local->scan_req->ie, local->scan_req->ie_len, 582aad14cebSRajkumar Manoharan local->scan_req->rates[band], false, 583aad14cebSRajkumar Manoharan local->scan_req->no_cck); 5847d3be3ccSHelmut Schaa 5857d3be3ccSHelmut Schaa /* 5867d3be3ccSHelmut Schaa * After sending probe requests, wait for probe responses 5877d3be3ccSHelmut Schaa * on the channel. 5887d3be3ccSHelmut Schaa */ 5897d3be3ccSHelmut Schaa *next_delay = IEEE80211_CHANNEL_TIME; 590977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 5917d3be3ccSHelmut Schaa } 5927d3be3ccSHelmut Schaa 59307ef03eeSJohannes Berg static void ieee80211_scan_state_suspend(struct ieee80211_local *local, 59407ef03eeSJohannes Berg unsigned long *next_delay) 59507ef03eeSJohannes Berg { 59607ef03eeSJohannes Berg /* switch back to the operating channel */ 59707ef03eeSJohannes Berg local->scan_channel = NULL; 59807ef03eeSJohannes Berg ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); 59907ef03eeSJohannes Berg 60007ef03eeSJohannes Berg /* 60107ef03eeSJohannes Berg * Re-enable vifs and beaconing. Leave PS 60207ef03eeSJohannes Berg * in off-channel state..will put that back 60307ef03eeSJohannes Berg * on-channel at the end of scanning. 60407ef03eeSJohannes Berg */ 605*e76aadc5SJohannes Berg ieee80211_offchannel_return(local, false); 60607ef03eeSJohannes Berg 60707ef03eeSJohannes Berg *next_delay = HZ / 5; 60807ef03eeSJohannes Berg /* afterwards, resume scan & go to next channel */ 60907ef03eeSJohannes Berg local->next_scan_state = SCAN_RESUME; 61007ef03eeSJohannes Berg } 61107ef03eeSJohannes Berg 61207ef03eeSJohannes Berg static void ieee80211_scan_state_resume(struct ieee80211_local *local, 61307ef03eeSJohannes Berg unsigned long *next_delay) 61407ef03eeSJohannes Berg { 61507ef03eeSJohannes Berg /* PS already is in off-channel mode */ 61607ef03eeSJohannes Berg ieee80211_offchannel_stop_vifs(local, false); 61707ef03eeSJohannes Berg 61807ef03eeSJohannes Berg if (local->ops->flush) { 61907ef03eeSJohannes Berg drv_flush(local, false); 62007ef03eeSJohannes Berg *next_delay = 0; 62107ef03eeSJohannes Berg } else 62207ef03eeSJohannes Berg *next_delay = HZ / 10; 62307ef03eeSJohannes Berg 62407ef03eeSJohannes Berg /* remember when we left the operating channel */ 62507ef03eeSJohannes Berg local->leave_oper_channel_time = jiffies; 62607ef03eeSJohannes Berg 62707ef03eeSJohannes Berg /* advance to the next channel to be scanned */ 62807ef03eeSJohannes Berg local->next_scan_state = SCAN_DECISION; 62907ef03eeSJohannes Berg } 63007ef03eeSJohannes Berg 631c2b13452SJohannes Berg void ieee80211_scan_work(struct work_struct *work) 6320a51b27eSJohannes Berg { 6330a51b27eSJohannes Berg struct ieee80211_local *local = 6340a51b27eSJohannes Berg container_of(work, struct ieee80211_local, scan_work.work); 635d07bfd8bSJohannes Berg struct ieee80211_sub_if_data *sdata; 6360a51b27eSJohannes Berg unsigned long next_delay = 0; 637d07bfd8bSJohannes Berg bool aborted, hw_scan; 6388789d459SJohannes Berg 639259b62e3SStanislaw Gruszka mutex_lock(&local->mtx); 640259b62e3SStanislaw Gruszka 641d07bfd8bSJohannes Berg sdata = local->scan_sdata; 642d07bfd8bSJohannes Berg 643259b62e3SStanislaw Gruszka if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) { 6448789d459SJohannes Berg aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 645259b62e3SStanislaw Gruszka goto out_complete; 6468789d459SJohannes Berg } 6478789d459SJohannes Berg 648259b62e3SStanislaw Gruszka if (!sdata || !local->scan_req) 649259b62e3SStanislaw Gruszka goto out; 650f3b85252SJohannes Berg 651fbe9c429SHelmut Schaa if (local->scan_req && !local->scanning) { 652f3b85252SJohannes Berg struct cfg80211_scan_request *req = local->scan_req; 653f3b85252SJohannes Berg int rc; 654f3b85252SJohannes Berg 655f3b85252SJohannes Berg local->scan_req = NULL; 65615db0b7fSJohannes Berg local->scan_sdata = NULL; 657f3b85252SJohannes Berg 658f3b85252SJohannes Berg rc = __ieee80211_start_scan(sdata, req); 659259b62e3SStanislaw Gruszka if (rc) { 6603aed49efSStanislaw Gruszka /* need to complete scan in cfg80211 */ 6613aed49efSStanislaw Gruszka local->scan_req = req; 662259b62e3SStanislaw Gruszka aborted = true; 663259b62e3SStanislaw Gruszka goto out_complete; 664259b62e3SStanislaw Gruszka } else 665259b62e3SStanislaw Gruszka goto out; 666f3b85252SJohannes Berg } 667f3b85252SJohannes Berg 6685bc75728SJohannes Berg /* 6695bc75728SJohannes Berg * Avoid re-scheduling when the sdata is going away. 6705bc75728SJohannes Berg */ 6719607e6b6SJohannes Berg if (!ieee80211_sdata_running(sdata)) { 672259b62e3SStanislaw Gruszka aborted = true; 673259b62e3SStanislaw Gruszka goto out_complete; 674f3b85252SJohannes Berg } 6750a51b27eSJohannes Berg 676f502d09bSHelmut Schaa /* 677f502d09bSHelmut Schaa * as long as no delay is required advance immediately 678f502d09bSHelmut Schaa * without scheduling a new work 679f502d09bSHelmut Schaa */ 680f502d09bSHelmut Schaa do { 681c29acf20SRajkumar Manoharan if (!ieee80211_sdata_running(sdata)) { 682c29acf20SRajkumar Manoharan aborted = true; 683c29acf20SRajkumar Manoharan goto out_complete; 684c29acf20SRajkumar Manoharan } 685c29acf20SRajkumar Manoharan 686977923b0SHelmut Schaa switch (local->next_scan_state) { 6872fb3f028SHelmut Schaa case SCAN_DECISION: 688e229f844SStanislaw Gruszka /* if no more bands/channels left, complete scan */ 689e229f844SStanislaw Gruszka if (local->scan_channel_idx >= local->scan_req->n_channels) { 690e229f844SStanislaw Gruszka aborted = false; 691e229f844SStanislaw Gruszka goto out_complete; 692e229f844SStanislaw Gruszka } 693e229f844SStanislaw Gruszka ieee80211_scan_state_decision(local, &next_delay); 6940a51b27eSJohannes Berg break; 6952fb3f028SHelmut Schaa case SCAN_SET_CHANNEL: 6962fb3f028SHelmut Schaa ieee80211_scan_state_set_channel(local, &next_delay); 6972fb3f028SHelmut Schaa break; 6980a51b27eSJohannes Berg case SCAN_SEND_PROBE: 6997d3be3ccSHelmut Schaa ieee80211_scan_state_send_probe(local, &next_delay); 7000a51b27eSJohannes Berg break; 70107ef03eeSJohannes Berg case SCAN_SUSPEND: 70207ef03eeSJohannes Berg ieee80211_scan_state_suspend(local, &next_delay); 703142b9f50SHelmut Schaa break; 70407ef03eeSJohannes Berg case SCAN_RESUME: 70507ef03eeSJohannes Berg ieee80211_scan_state_resume(local, &next_delay); 706142b9f50SHelmut Schaa break; 7070a51b27eSJohannes Berg } 708f502d09bSHelmut Schaa } while (next_delay == 0); 7090a51b27eSJohannes Berg 71042935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay); 711d07bfd8bSJohannes Berg goto out; 712259b62e3SStanislaw Gruszka 713259b62e3SStanislaw Gruszka out_complete: 714e229f844SStanislaw Gruszka hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning); 715d07bfd8bSJohannes Berg __ieee80211_scan_completed(&local->hw, aborted, hw_scan); 716259b62e3SStanislaw Gruszka out: 717259b62e3SStanislaw Gruszka mutex_unlock(&local->mtx); 7180a51b27eSJohannes Berg } 7190a51b27eSJohannes Berg 720c2b13452SJohannes Berg int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata, 7212a519311SJohannes Berg struct cfg80211_scan_request *req) 7220a51b27eSJohannes Berg { 723f3b85252SJohannes Berg int res; 7240a51b27eSJohannes Berg 725a1699b75SJohannes Berg mutex_lock(&sdata->local->mtx); 726f3b85252SJohannes Berg res = __ieee80211_start_scan(sdata, req); 727a1699b75SJohannes Berg mutex_unlock(&sdata->local->mtx); 7282a519311SJohannes Berg 729f3b85252SJohannes Berg return res; 7300a51b27eSJohannes Berg } 7310a51b27eSJohannes Berg 732f3b85252SJohannes Berg int ieee80211_request_internal_scan(struct ieee80211_sub_if_data *sdata, 733be4a4b6aSJohannes Berg const u8 *ssid, u8 ssid_len, 734be4a4b6aSJohannes Berg struct ieee80211_channel *chan) 735f3b85252SJohannes Berg { 736f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 737f3b85252SJohannes Berg int ret = -EBUSY; 738fb63bc41SGertjan van Wingerde enum ieee80211_band band; 7399116dd01SJohannes Berg 740a1699b75SJohannes Berg mutex_lock(&local->mtx); 741f3b85252SJohannes Berg 742f3b85252SJohannes Berg /* busy scanning */ 743f3b85252SJohannes Berg if (local->scan_req) 744f3b85252SJohannes Berg goto unlock; 745f3b85252SJohannes Berg 746be4a4b6aSJohannes Berg /* fill internal scan request */ 747be4a4b6aSJohannes Berg if (!chan) { 748be4a4b6aSJohannes Berg int i, nchan = 0; 749be4a4b6aSJohannes Berg 750be4a4b6aSJohannes Berg for (band = 0; band < IEEE80211_NUM_BANDS; band++) { 751be4a4b6aSJohannes Berg if (!local->hw.wiphy->bands[band]) 752be4a4b6aSJohannes Berg continue; 753be4a4b6aSJohannes Berg for (i = 0; 754be4a4b6aSJohannes Berg i < local->hw.wiphy->bands[band]->n_channels; 755be4a4b6aSJohannes Berg i++) { 756be4a4b6aSJohannes Berg local->int_scan_req->channels[nchan] = 757be4a4b6aSJohannes Berg &local->hw.wiphy->bands[band]->channels[i]; 758be4a4b6aSJohannes Berg nchan++; 759be4a4b6aSJohannes Berg } 760be4a4b6aSJohannes Berg } 761be4a4b6aSJohannes Berg 762be4a4b6aSJohannes Berg local->int_scan_req->n_channels = nchan; 763be4a4b6aSJohannes Berg } else { 764be4a4b6aSJohannes Berg local->int_scan_req->channels[0] = chan; 765be4a4b6aSJohannes Berg local->int_scan_req->n_channels = 1; 766be4a4b6aSJohannes Berg } 767be4a4b6aSJohannes Berg 768be4a4b6aSJohannes Berg local->int_scan_req->ssids = &local->scan_ssid; 769be4a4b6aSJohannes Berg local->int_scan_req->n_ssids = 1; 7705ba63533SJohannes Berg memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN); 7715ba63533SJohannes Berg local->int_scan_req->ssids[0].ssid_len = ssid_len; 772f3b85252SJohannes Berg 7735ba63533SJohannes Berg ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req); 774f3b85252SJohannes Berg unlock: 775a1699b75SJohannes Berg mutex_unlock(&local->mtx); 776f3b85252SJohannes Berg return ret; 7770a51b27eSJohannes Berg } 7785bb644a0SJohannes Berg 7794136c422SStanislaw Gruszka /* 7804136c422SStanislaw Gruszka * Only call this function when a scan can't be queued -- under RTNL. 7814136c422SStanislaw Gruszka */ 7825bb644a0SJohannes Berg void ieee80211_scan_cancel(struct ieee80211_local *local) 7835bb644a0SJohannes Berg { 7845bb644a0SJohannes Berg /* 785b856439bSEliad Peller * We are canceling software scan, or deferred scan that was not 7864136c422SStanislaw Gruszka * yet really started (see __ieee80211_start_scan ). 7874136c422SStanislaw Gruszka * 7884136c422SStanislaw Gruszka * Regarding hardware scan: 7894136c422SStanislaw Gruszka * - we can not call __ieee80211_scan_completed() as when 7904136c422SStanislaw Gruszka * SCAN_HW_SCANNING bit is set this function change 7914136c422SStanislaw Gruszka * local->hw_scan_req to operate on 5G band, what race with 7924136c422SStanislaw Gruszka * driver which can use local->hw_scan_req 7934136c422SStanislaw Gruszka * 7944136c422SStanislaw Gruszka * - we can not cancel scan_work since driver can schedule it 7954136c422SStanislaw Gruszka * by ieee80211_scan_completed(..., true) to finish scan 7964136c422SStanislaw Gruszka * 797b856439bSEliad Peller * Hence we only call the cancel_hw_scan() callback, but the low-level 798b856439bSEliad Peller * driver is still responsible for calling ieee80211_scan_completed() 799b856439bSEliad Peller * after the scan was completed/aborted. 8005bb644a0SJohannes Berg */ 8014136c422SStanislaw Gruszka 802a1699b75SJohannes Berg mutex_lock(&local->mtx); 803b856439bSEliad Peller if (!local->scan_req) 804b856439bSEliad Peller goto out; 805b856439bSEliad Peller 806b856439bSEliad Peller if (test_bit(SCAN_HW_SCANNING, &local->scanning)) { 807b856439bSEliad Peller if (local->ops->cancel_hw_scan) 808b856439bSEliad Peller drv_cancel_hw_scan(local, local->scan_sdata); 809b856439bSEliad Peller goto out; 810b856439bSEliad Peller } 811b856439bSEliad Peller 812d07bfd8bSJohannes Berg /* 813d07bfd8bSJohannes Berg * If the work is currently running, it must be blocked on 814d07bfd8bSJohannes Berg * the mutex, but we'll set scan_sdata = NULL and it'll 815d07bfd8bSJohannes Berg * simply exit once it acquires the mutex. 816d07bfd8bSJohannes Berg */ 817d07bfd8bSJohannes Berg cancel_delayed_work(&local->scan_work); 818d07bfd8bSJohannes Berg /* and clean up */ 819d07bfd8bSJohannes Berg __ieee80211_scan_completed(&local->hw, true, false); 820b856439bSEliad Peller out: 821d07bfd8bSJohannes Berg mutex_unlock(&local->mtx); 8225bb644a0SJohannes Berg } 82379f460caSLuciano Coelho 82479f460caSLuciano Coelho int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 82579f460caSLuciano Coelho struct cfg80211_sched_scan_request *req) 82679f460caSLuciano Coelho { 82779f460caSLuciano Coelho struct ieee80211_local *local = sdata->local; 82879f460caSLuciano Coelho int ret, i; 82979f460caSLuciano Coelho 83079f460caSLuciano Coelho mutex_lock(&sdata->local->mtx); 83179f460caSLuciano Coelho 83279f460caSLuciano Coelho if (local->sched_scanning) { 83379f460caSLuciano Coelho ret = -EBUSY; 83479f460caSLuciano Coelho goto out; 83579f460caSLuciano Coelho } 83679f460caSLuciano Coelho 83779f460caSLuciano Coelho if (!local->ops->sched_scan_start) { 83879f460caSLuciano Coelho ret = -ENOTSUPP; 83979f460caSLuciano Coelho goto out; 84079f460caSLuciano Coelho } 84179f460caSLuciano Coelho 84279f460caSLuciano Coelho for (i = 0; i < IEEE80211_NUM_BANDS; i++) { 84379f460caSLuciano Coelho local->sched_scan_ies.ie[i] = kzalloc(2 + 84479f460caSLuciano Coelho IEEE80211_MAX_SSID_LEN + 8451186980dSLuciano Coelho local->scan_ies_len + 8461186980dSLuciano Coelho req->ie_len, 84779f460caSLuciano Coelho GFP_KERNEL); 84879f460caSLuciano Coelho if (!local->sched_scan_ies.ie[i]) { 84979f460caSLuciano Coelho ret = -ENOMEM; 85079f460caSLuciano Coelho goto out_free; 85179f460caSLuciano Coelho } 85279f460caSLuciano Coelho 85379f460caSLuciano Coelho local->sched_scan_ies.len[i] = 85479f460caSLuciano Coelho ieee80211_build_preq_ies(local, 85579f460caSLuciano Coelho local->sched_scan_ies.ie[i], 85679f460caSLuciano Coelho req->ie, req->ie_len, i, 85779f460caSLuciano Coelho (u32) -1, 0); 85879f460caSLuciano Coelho } 85979f460caSLuciano Coelho 86079f460caSLuciano Coelho ret = drv_sched_scan_start(local, sdata, req, 86179f460caSLuciano Coelho &local->sched_scan_ies); 86279f460caSLuciano Coelho if (ret == 0) { 86379f460caSLuciano Coelho local->sched_scanning = true; 86479f460caSLuciano Coelho goto out; 86579f460caSLuciano Coelho } 86679f460caSLuciano Coelho 86779f460caSLuciano Coelho out_free: 86879f460caSLuciano Coelho while (i > 0) 86979f460caSLuciano Coelho kfree(local->sched_scan_ies.ie[--i]); 87079f460caSLuciano Coelho out: 87179f460caSLuciano Coelho mutex_unlock(&sdata->local->mtx); 87279f460caSLuciano Coelho return ret; 87379f460caSLuciano Coelho } 87479f460caSLuciano Coelho 87585a9994aSLuciano Coelho int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata) 87679f460caSLuciano Coelho { 87779f460caSLuciano Coelho struct ieee80211_local *local = sdata->local; 87879f460caSLuciano Coelho int ret = 0, i; 87979f460caSLuciano Coelho 88079f460caSLuciano Coelho mutex_lock(&sdata->local->mtx); 88179f460caSLuciano Coelho 88279f460caSLuciano Coelho if (!local->ops->sched_scan_stop) { 88379f460caSLuciano Coelho ret = -ENOTSUPP; 88479f460caSLuciano Coelho goto out; 88579f460caSLuciano Coelho } 88679f460caSLuciano Coelho 88779f460caSLuciano Coelho if (local->sched_scanning) { 88879f460caSLuciano Coelho for (i = 0; i < IEEE80211_NUM_BANDS; i++) 88979f460caSLuciano Coelho kfree(local->sched_scan_ies.ie[i]); 89079f460caSLuciano Coelho 89179f460caSLuciano Coelho drv_sched_scan_stop(local, sdata); 89279f460caSLuciano Coelho local->sched_scanning = false; 89379f460caSLuciano Coelho } 89479f460caSLuciano Coelho out: 89579f460caSLuciano Coelho mutex_unlock(&sdata->local->mtx); 89679f460caSLuciano Coelho 89779f460caSLuciano Coelho return ret; 89879f460caSLuciano Coelho } 89979f460caSLuciano Coelho 90079f460caSLuciano Coelho void ieee80211_sched_scan_results(struct ieee80211_hw *hw) 90179f460caSLuciano Coelho { 90279f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 90379f460caSLuciano Coelho 90479f460caSLuciano Coelho trace_api_sched_scan_results(local); 90579f460caSLuciano Coelho 90679f460caSLuciano Coelho cfg80211_sched_scan_results(hw->wiphy); 90779f460caSLuciano Coelho } 90879f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_results); 90979f460caSLuciano Coelho 91085a9994aSLuciano Coelho void ieee80211_sched_scan_stopped_work(struct work_struct *work) 91185a9994aSLuciano Coelho { 91285a9994aSLuciano Coelho struct ieee80211_local *local = 91385a9994aSLuciano Coelho container_of(work, struct ieee80211_local, 91485a9994aSLuciano Coelho sched_scan_stopped_work); 91585a9994aSLuciano Coelho int i; 91685a9994aSLuciano Coelho 91785a9994aSLuciano Coelho mutex_lock(&local->mtx); 91885a9994aSLuciano Coelho 91985a9994aSLuciano Coelho if (!local->sched_scanning) { 92085a9994aSLuciano Coelho mutex_unlock(&local->mtx); 92185a9994aSLuciano Coelho return; 92285a9994aSLuciano Coelho } 92385a9994aSLuciano Coelho 92485a9994aSLuciano Coelho for (i = 0; i < IEEE80211_NUM_BANDS; i++) 92585a9994aSLuciano Coelho kfree(local->sched_scan_ies.ie[i]); 92685a9994aSLuciano Coelho 92785a9994aSLuciano Coelho local->sched_scanning = false; 92885a9994aSLuciano Coelho 92985a9994aSLuciano Coelho mutex_unlock(&local->mtx); 93085a9994aSLuciano Coelho 93185a9994aSLuciano Coelho cfg80211_sched_scan_stopped(local->hw.wiphy); 93285a9994aSLuciano Coelho } 93385a9994aSLuciano Coelho 93479f460caSLuciano Coelho void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw) 93579f460caSLuciano Coelho { 93679f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 93779f460caSLuciano Coelho 93879f460caSLuciano Coelho trace_api_sched_scan_stopped(local); 93979f460caSLuciano Coelho 94085a9994aSLuciano Coelho ieee80211_queue_work(&local->hw, &local->sched_scan_stopped_work); 94179f460caSLuciano Coelho } 94279f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_stopped); 943