xref: /linux/net/mac80211/scan.c (revision a806c558e01747b499201d2667818f03d79ef1e3)
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>
17df13cce5SHelmut Schaa #include <linux/pm_qos_params.h>
18df13cce5SHelmut Schaa #include <net/sch_generic.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
200a51b27eSJohannes Berg #include <net/mac80211.h>
210a51b27eSJohannes Berg 
220a51b27eSJohannes Berg #include "ieee80211_i.h"
2324487981SJohannes Berg #include "driver-ops.h"
245484e237SJohannes Berg #include "mesh.h"
250a51b27eSJohannes Berg 
260a51b27eSJohannes Berg #define IEEE80211_PROBE_DELAY (HZ / 33)
270a51b27eSJohannes Berg #define IEEE80211_CHANNEL_TIME (HZ / 33)
2896f7e739SHelmut Schaa #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 8)
290a51b27eSJohannes Berg 
30c2b13452SJohannes Berg struct ieee80211_bss *
315484e237SJohannes Berg ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
325484e237SJohannes Berg 		     u8 *ssid, u8 ssid_len)
335484e237SJohannes Berg {
340c1ad2caSJohannes Berg 	struct cfg80211_bss *cbss;
350c1ad2caSJohannes Berg 
360c1ad2caSJohannes Berg 	cbss = cfg80211_get_bss(local->hw.wiphy,
370c1ad2caSJohannes Berg 				ieee80211_get_channel(local->hw.wiphy, freq),
380c1ad2caSJohannes Berg 				bssid, ssid, ssid_len, 0, 0);
390c1ad2caSJohannes Berg 	if (!cbss)
400c1ad2caSJohannes Berg 		return NULL;
410c1ad2caSJohannes Berg 	return (void *)cbss->priv;
425484e237SJohannes Berg }
435484e237SJohannes Berg 
4400d3f14cSJohannes Berg static void ieee80211_rx_bss_free(struct cfg80211_bss *cbss)
455484e237SJohannes Berg {
460c1ad2caSJohannes Berg 	struct ieee80211_bss *bss = (void *)cbss->priv;
475484e237SJohannes Berg 
485484e237SJohannes Berg 	kfree(bss_mesh_id(bss));
495484e237SJohannes Berg 	kfree(bss_mesh_cfg(bss));
505484e237SJohannes Berg }
515484e237SJohannes Berg 
525484e237SJohannes Berg void ieee80211_rx_bss_put(struct ieee80211_local *local,
53c2b13452SJohannes Berg 			  struct ieee80211_bss *bss)
545484e237SJohannes Berg {
550c1ad2caSJohannes Berg 	if (!bss)
560c1ad2caSJohannes Berg 		return;
570c1ad2caSJohannes Berg 	cfg80211_put_bss(container_of((void *)bss, struct cfg80211_bss, priv));
585484e237SJohannes Berg }
595484e237SJohannes Berg 
60ab13315aSKalle Valo static bool is_uapsd_supported(struct ieee802_11_elems *elems)
61ab13315aSKalle Valo {
62ab13315aSKalle Valo 	u8 qos_info;
63ab13315aSKalle Valo 
64ab13315aSKalle Valo 	if (elems->wmm_info && elems->wmm_info_len == 7
65ab13315aSKalle Valo 	    && elems->wmm_info[5] == 1)
66ab13315aSKalle Valo 		qos_info = elems->wmm_info[6];
67ab13315aSKalle Valo 	else if (elems->wmm_param && elems->wmm_param_len == 24
68ab13315aSKalle Valo 		 && elems->wmm_param[5] == 1)
69ab13315aSKalle Valo 		qos_info = elems->wmm_param[6];
70ab13315aSKalle Valo 	else
71ab13315aSKalle Valo 		/* no valid wmm information or parameter element found */
72ab13315aSKalle Valo 		return false;
73ab13315aSKalle Valo 
74ab13315aSKalle Valo 	return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
75ab13315aSKalle Valo }
76ab13315aSKalle Valo 
77c2b13452SJohannes Berg struct ieee80211_bss *
785484e237SJohannes Berg ieee80211_bss_info_update(struct ieee80211_local *local,
795484e237SJohannes Berg 			  struct ieee80211_rx_status *rx_status,
805484e237SJohannes Berg 			  struct ieee80211_mgmt *mgmt,
815484e237SJohannes Berg 			  size_t len,
825484e237SJohannes Berg 			  struct ieee802_11_elems *elems,
832a519311SJohannes Berg 			  struct ieee80211_channel *channel,
842a519311SJohannes Berg 			  bool beacon)
855484e237SJohannes Berg {
860c1ad2caSJohannes Berg 	struct cfg80211_bss *cbss;
87c2b13452SJohannes Berg 	struct ieee80211_bss *bss;
88f0b058b6SStanislaw Gruszka 	int clen, srlen;
892a519311SJohannes Berg 	s32 signal = 0;
902a519311SJohannes Berg 
9177965c97SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
922a519311SJohannes Berg 		signal = rx_status->signal * 100;
9377965c97SJohannes Berg 	else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
942a519311SJohannes Berg 		signal = (rx_status->signal * 100) / local->hw.max_signal;
952a519311SJohannes Berg 
960c1ad2caSJohannes Berg 	cbss = cfg80211_inform_bss_frame(local->hw.wiphy, channel,
9777965c97SJohannes Berg 					 mgmt, len, signal, GFP_ATOMIC);
985484e237SJohannes Berg 
990c1ad2caSJohannes Berg 	if (!cbss)
1005484e237SJohannes Berg 		return NULL;
10100d3f14cSJohannes Berg 
1020c1ad2caSJohannes Berg 	cbss->free_priv = ieee80211_rx_bss_free;
1030c1ad2caSJohannes Berg 	bss = (void *)cbss->priv;
1045484e237SJohannes Berg 
1055484e237SJohannes Berg 	/* save the ERP value so that it is available at association time */
1065484e237SJohannes Berg 	if (elems->erp_info && elems->erp_info_len >= 1) {
1075484e237SJohannes Berg 		bss->erp_value = elems->erp_info[0];
1085484e237SJohannes Berg 		bss->has_erp_value = 1;
1095484e237SJohannes Berg 	}
1105484e237SJohannes Berg 
1115484e237SJohannes Berg 	if (elems->tim) {
1125484e237SJohannes Berg 		struct ieee80211_tim_ie *tim_ie =
1135484e237SJohannes Berg 			(struct ieee80211_tim_ie *)elems->tim;
1145484e237SJohannes Berg 		bss->dtim_period = tim_ie->dtim_period;
1155484e237SJohannes Berg 	}
1165484e237SJohannes Berg 
117e5b900d2SJohannes Berg 	/* If the beacon had no TIM IE, or it was invalid, use 1 */
118e5b900d2SJohannes Berg 	if (beacon && !bss->dtim_period)
119e5b900d2SJohannes Berg 		bss->dtim_period = 1;
120e5b900d2SJohannes Berg 
121f0b058b6SStanislaw Gruszka 	/* replace old supported rates if we get new values */
122f0b058b6SStanislaw Gruszka 	srlen = 0;
1235484e237SJohannes Berg 	if (elems->supp_rates) {
124f0b058b6SStanislaw Gruszka 		clen = IEEE80211_MAX_SUPP_RATES;
1255484e237SJohannes Berg 		if (clen > elems->supp_rates_len)
1265484e237SJohannes Berg 			clen = elems->supp_rates_len;
127f0b058b6SStanislaw Gruszka 		memcpy(bss->supp_rates, elems->supp_rates, clen);
128f0b058b6SStanislaw Gruszka 		srlen += clen;
1295484e237SJohannes Berg 	}
1305484e237SJohannes Berg 	if (elems->ext_supp_rates) {
131f0b058b6SStanislaw Gruszka 		clen = IEEE80211_MAX_SUPP_RATES - srlen;
1325484e237SJohannes Berg 		if (clen > elems->ext_supp_rates_len)
1335484e237SJohannes Berg 			clen = elems->ext_supp_rates_len;
134f0b058b6SStanislaw Gruszka 		memcpy(bss->supp_rates + srlen, elems->ext_supp_rates, clen);
135f0b058b6SStanislaw Gruszka 		srlen += clen;
1365484e237SJohannes Berg 	}
137f0b058b6SStanislaw Gruszka 	if (srlen)
138f0b058b6SStanislaw Gruszka 		bss->supp_rates_len = srlen;
1395484e237SJohannes Berg 
1405484e237SJohannes Berg 	bss->wmm_used = elems->wmm_param || elems->wmm_info;
141ab13315aSKalle Valo 	bss->uapsd_supported = is_uapsd_supported(elems);
1425484e237SJohannes Berg 
1435484e237SJohannes Berg 	if (!beacon)
1445484e237SJohannes Berg 		bss->last_probe_resp = jiffies;
1455484e237SJohannes Berg 
1465484e237SJohannes Berg 	return bss;
1475484e237SJohannes Berg }
1480a51b27eSJohannes Berg 
14998c8fccfSJohannes Berg ieee80211_rx_result
150f1d58c25SJohannes Berg ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
15198c8fccfSJohannes Berg {
152f1d58c25SJohannes Berg 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
15398c8fccfSJohannes Berg 	struct ieee80211_mgmt *mgmt;
154c2b13452SJohannes Berg 	struct ieee80211_bss *bss;
15598c8fccfSJohannes Berg 	u8 *elements;
15698c8fccfSJohannes Berg 	struct ieee80211_channel *channel;
15798c8fccfSJohannes Berg 	size_t baselen;
15898c8fccfSJohannes Berg 	int freq;
15998c8fccfSJohannes Berg 	__le16 fc;
16098c8fccfSJohannes Berg 	bool presp, beacon = false;
16198c8fccfSJohannes Berg 	struct ieee802_11_elems elems;
16298c8fccfSJohannes Berg 
16398c8fccfSJohannes Berg 	if (skb->len < 2)
16498c8fccfSJohannes Berg 		return RX_DROP_UNUSABLE;
16598c8fccfSJohannes Berg 
16698c8fccfSJohannes Berg 	mgmt = (struct ieee80211_mgmt *) skb->data;
16798c8fccfSJohannes Berg 	fc = mgmt->frame_control;
16898c8fccfSJohannes Berg 
16998c8fccfSJohannes Berg 	if (ieee80211_is_ctl(fc))
17098c8fccfSJohannes Berg 		return RX_CONTINUE;
17198c8fccfSJohannes Berg 
17298c8fccfSJohannes Berg 	if (skb->len < 24)
173306fe938SLuciano Coelho 		return RX_CONTINUE;
17498c8fccfSJohannes Berg 
17598c8fccfSJohannes Berg 	presp = ieee80211_is_probe_resp(fc);
17698c8fccfSJohannes Berg 	if (presp) {
17798c8fccfSJohannes Berg 		/* ignore ProbeResp to foreign address */
17847846c9bSJohannes Berg 		if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
17998c8fccfSJohannes Berg 			return RX_DROP_MONITOR;
18098c8fccfSJohannes Berg 
18198c8fccfSJohannes Berg 		presp = true;
18298c8fccfSJohannes Berg 		elements = mgmt->u.probe_resp.variable;
18398c8fccfSJohannes Berg 		baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
18498c8fccfSJohannes Berg 	} else {
18598c8fccfSJohannes Berg 		beacon = ieee80211_is_beacon(fc);
18698c8fccfSJohannes Berg 		baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
18798c8fccfSJohannes Berg 		elements = mgmt->u.beacon.variable;
18898c8fccfSJohannes Berg 	}
18998c8fccfSJohannes Berg 
19098c8fccfSJohannes Berg 	if (!presp && !beacon)
19198c8fccfSJohannes Berg 		return RX_CONTINUE;
19298c8fccfSJohannes Berg 
19398c8fccfSJohannes Berg 	if (baselen > skb->len)
19498c8fccfSJohannes Berg 		return RX_DROP_MONITOR;
19598c8fccfSJohannes Berg 
19698c8fccfSJohannes Berg 	ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
19798c8fccfSJohannes Berg 
19898c8fccfSJohannes Berg 	if (elems.ds_params && elems.ds_params_len == 1)
19959eb21a6SBruno Randolf 		freq = ieee80211_channel_to_frequency(elems.ds_params[0],
20059eb21a6SBruno Randolf 						      rx_status->band);
20198c8fccfSJohannes Berg 	else
20298c8fccfSJohannes Berg 		freq = rx_status->freq;
20398c8fccfSJohannes Berg 
20498c8fccfSJohannes Berg 	channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
20598c8fccfSJohannes Berg 
20698c8fccfSJohannes Berg 	if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
20798c8fccfSJohannes Berg 		return RX_DROP_MONITOR;
20898c8fccfSJohannes Berg 
20998c8fccfSJohannes Berg 	bss = ieee80211_bss_info_update(sdata->local, rx_status,
21098c8fccfSJohannes Berg 					mgmt, skb->len, &elems,
2112a519311SJohannes Berg 					channel, beacon);
212d048e503SJouni Malinen 	if (bss)
21398c8fccfSJohannes Berg 		ieee80211_rx_bss_put(sdata->local, bss);
21498c8fccfSJohannes Berg 
215b23b025fSBen Greear 	/* If we are on-operating-channel, and this packet is for the
216b23b025fSBen Greear 	 * current channel, pass the pkt on up the stack so that
217b23b025fSBen Greear 	 * the rest of the stack can make use of it.
218b23b025fSBen Greear 	 */
219b23b025fSBen Greear 	if (ieee80211_cfg_on_oper_channel(sdata->local)
220b23b025fSBen Greear 	    && (channel == sdata->local->oper_channel))
221b23b025fSBen Greear 		return RX_CONTINUE;
222b23b025fSBen Greear 
22398c8fccfSJohannes Berg 	dev_kfree_skb(skb);
22498c8fccfSJohannes Berg 	return RX_QUEUED;
22598c8fccfSJohannes Berg }
22698c8fccfSJohannes Berg 
2274d36ec58SJohannes Berg /* return false if no more work */
2284d36ec58SJohannes Berg static bool ieee80211_prep_hw_scan(struct ieee80211_local *local)
2294d36ec58SJohannes Berg {
2304d36ec58SJohannes Berg 	struct cfg80211_scan_request *req = local->scan_req;
2318ee31080SJohannes Berg 	struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2324d36ec58SJohannes Berg 	enum ieee80211_band band;
2334d36ec58SJohannes Berg 	int i, ielen, n_chans;
2344d36ec58SJohannes Berg 
2354d36ec58SJohannes Berg 	do {
2364d36ec58SJohannes Berg 		if (local->hw_scan_band == IEEE80211_NUM_BANDS)
2374d36ec58SJohannes Berg 			return false;
2384d36ec58SJohannes Berg 
2394d36ec58SJohannes Berg 		band = local->hw_scan_band;
2404d36ec58SJohannes Berg 		n_chans = 0;
2414d36ec58SJohannes Berg 		for (i = 0; i < req->n_channels; i++) {
2424d36ec58SJohannes Berg 			if (req->channels[i]->band == band) {
2434d36ec58SJohannes Berg 				local->hw_scan_req->channels[n_chans] =
2444d36ec58SJohannes Berg 							req->channels[i];
2454d36ec58SJohannes Berg 				n_chans++;
2464d36ec58SJohannes Berg 			}
2474d36ec58SJohannes Berg 		}
2484d36ec58SJohannes Berg 
2494d36ec58SJohannes Berg 		local->hw_scan_band++;
2504d36ec58SJohannes Berg 	} while (!n_chans);
2514d36ec58SJohannes Berg 
2524d36ec58SJohannes Berg 	local->hw_scan_req->n_channels = n_chans;
2534d36ec58SJohannes Berg 
2544d36ec58SJohannes Berg 	ielen = ieee80211_build_preq_ies(local, (u8 *)local->hw_scan_req->ie,
2558ee31080SJohannes Berg 					 req->ie, req->ie_len, band,
2568ee31080SJohannes Berg 					 sdata->rc_rateidx_mask[band], 0);
2574d36ec58SJohannes Berg 	local->hw_scan_req->ie_len = ielen;
2584d36ec58SJohannes Berg 
2594d36ec58SJohannes Berg 	return true;
2604d36ec58SJohannes Berg }
2614d36ec58SJohannes Berg 
262d07bfd8bSJohannes Berg static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted,
263e229f844SStanislaw Gruszka 				       bool was_hw_scan)
264f3b85252SJohannes Berg {
265f3b85252SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
266d07bfd8bSJohannes Berg 	bool on_oper_chan;
267d07bfd8bSJohannes Berg 	bool enable_beacons = false;
268f3b85252SJohannes Berg 
269e229f844SStanislaw Gruszka 	lockdep_assert_held(&local->mtx);
270f3b85252SJohannes Berg 
2716d3560d4SJohannes Berg 	/*
2726d3560d4SJohannes Berg 	 * It's ok to abort a not-yet-running scan (that
2736d3560d4SJohannes Berg 	 * we have one at all will be verified by checking
2746d3560d4SJohannes Berg 	 * local->scan_req next), but not to complete it
2756d3560d4SJohannes Berg 	 * successfully.
2766d3560d4SJohannes Berg 	 */
2776d3560d4SJohannes Berg 	if (WARN_ON(!local->scanning && !aborted))
2786d3560d4SJohannes Berg 		aborted = true;
279f3b85252SJohannes Berg 
280e229f844SStanislaw Gruszka 	if (WARN_ON(!local->scan_req))
281d07bfd8bSJohannes Berg 		return;
282f3b85252SJohannes Berg 
2834d36ec58SJohannes Berg 	if (was_hw_scan && !aborted && ieee80211_prep_hw_scan(local)) {
2846eb11a9aSStanislaw Gruszka 		int rc = drv_hw_scan(local, local->scan_sdata, local->hw_scan_req);
2856eb11a9aSStanislaw Gruszka 		if (rc == 0)
286d07bfd8bSJohannes Berg 			return;
2874d36ec58SJohannes Berg 	}
2884d36ec58SJohannes Berg 
2894d36ec58SJohannes Berg 	kfree(local->hw_scan_req);
2904d36ec58SJohannes Berg 	local->hw_scan_req = NULL;
291f3b85252SJohannes Berg 
2925ba63533SJohannes Berg 	if (local->scan_req != local->int_scan_req)
2932a519311SJohannes Berg 		cfg80211_scan_done(local->scan_req, aborted);
2942a519311SJohannes Berg 	local->scan_req = NULL;
29515db0b7fSJohannes Berg 	local->scan_sdata = NULL;
2962a519311SJohannes Berg 
297fbe9c429SHelmut Schaa 	local->scanning = 0;
29858905ca5SJohannes Berg 	local->scan_channel = NULL;
299f3b85252SJohannes Berg 
300b23b025fSBen Greear 	on_oper_chan = ieee80211_cfg_on_oper_channel(local);
301b23b025fSBen Greear 
302d07bfd8bSJohannes Berg 	if (was_hw_scan || !on_oper_chan)
303b23b025fSBen Greear 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
304d07bfd8bSJohannes Berg 	else
30559bdf3b0SBen Greear 		/* Set power back to normal operating levels. */
30659bdf3b0SBen Greear 		ieee80211_hw_config(local, 0);
307b23b025fSBen Greear 
308b23b025fSBen Greear 	if (!was_hw_scan) {
309b23b025fSBen Greear 		bool on_oper_chan2;
310b23b025fSBen Greear 		ieee80211_configure_filter(local);
311b23b025fSBen Greear 		drv_sw_scan_complete(local);
312b23b025fSBen Greear 		on_oper_chan2 = ieee80211_cfg_on_oper_channel(local);
313b23b025fSBen Greear 		/* We should always be on-channel at this point. */
314b23b025fSBen Greear 		WARN_ON(!on_oper_chan2);
315b23b025fSBen Greear 		if (on_oper_chan2 && (on_oper_chan != on_oper_chan2))
316b23b025fSBen Greear 			enable_beacons = true;
317b23b025fSBen Greear 
318b23b025fSBen Greear 		ieee80211_offchannel_return(local, enable_beacons, true);
319b23b025fSBen Greear 	}
320b23b025fSBen Greear 
3215cff20e6SJohannes Berg 	ieee80211_recalc_idle(local);
322e229f844SStanislaw Gruszka 
3230a51b27eSJohannes Berg 	ieee80211_mlme_notify_scan_completed(local);
32446900298SJohannes Berg 	ieee80211_ibss_notify_scan_completed(local);
325472dbc45SJohannes Berg 	ieee80211_mesh_notify_scan_completed(local);
32681ac3462SJohannes Berg 	ieee80211_queue_work(&local->hw, &local->work_work);
3270a51b27eSJohannes Berg }
3288789d459SJohannes Berg 
3298789d459SJohannes Berg void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
3308789d459SJohannes Berg {
3318789d459SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
3328789d459SJohannes Berg 
3338789d459SJohannes Berg 	trace_api_scan_completed(local, aborted);
3348789d459SJohannes Berg 
3358789d459SJohannes Berg 	set_bit(SCAN_COMPLETED, &local->scanning);
3368789d459SJohannes Berg 	if (aborted)
3378789d459SJohannes Berg 		set_bit(SCAN_ABORTED, &local->scanning);
3388789d459SJohannes Berg 	ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
3398789d459SJohannes Berg }
3400a51b27eSJohannes Berg EXPORT_SYMBOL(ieee80211_scan_completed);
3410a51b27eSJohannes Berg 
342f3b85252SJohannes Berg static int ieee80211_start_sw_scan(struct ieee80211_local *local)
343f3b85252SJohannes Berg {
344f3b85252SJohannes Berg 	/*
345f3b85252SJohannes Berg 	 * Hardware/driver doesn't support hw_scan, so use software
346f3b85252SJohannes Berg 	 * scanning instead. First send a nullfunc frame with power save
347f3b85252SJohannes Berg 	 * bit on so that AP will buffer the frames for us while we are not
348f3b85252SJohannes Berg 	 * listening, then send probe requests to each channel and wait for
349f3b85252SJohannes Berg 	 * the responses. After all channels are scanned, tune back to the
350f3b85252SJohannes Berg 	 * original channel and send a nullfunc frame with power save bit
351f3b85252SJohannes Berg 	 * off to trigger the AP to send us all the buffered frames.
352f3b85252SJohannes Berg 	 *
353f3b85252SJohannes Berg 	 * Note that while local->sw_scanning is true everything else but
354f3b85252SJohannes Berg 	 * nullfunc frames and probe requests will be dropped in
355f3b85252SJohannes Berg 	 * ieee80211_tx_h_check_assoc().
356f3b85252SJohannes Berg 	 */
35724487981SJohannes Berg 	drv_sw_scan_start(local);
358f3b85252SJohannes Berg 
359df13cce5SHelmut Schaa 	local->leave_oper_channel_time = 0;
360977923b0SHelmut Schaa 	local->next_scan_state = SCAN_DECISION;
361f3b85252SJohannes Berg 	local->scan_channel_idx = 0;
362f3b85252SJohannes Berg 
363b23b025fSBen Greear 	/* We always want to use off-channel PS, even if we
364b23b025fSBen Greear 	 * are not really leaving oper-channel.  Don't
365b23b025fSBen Greear 	 * tell the AP though, as long as we are on-channel.
366b23b025fSBen Greear 	 */
367b23b025fSBen Greear 	ieee80211_offchannel_enable_all_ps(local, false);
368a80f7c0bSJohannes Berg 
3693ac64beeSJohannes Berg 	ieee80211_configure_filter(local);
370f3b85252SJohannes Berg 
37159bdf3b0SBen Greear 	/* We need to set power level at maximum rate for scanning. */
37259bdf3b0SBen Greear 	ieee80211_hw_config(local, 0);
37359bdf3b0SBen Greear 
37442935ecaSLuis R. Rodriguez 	ieee80211_queue_delayed_work(&local->hw,
37542935ecaSLuis R. Rodriguez 				     &local->scan_work,
376f3b85252SJohannes Berg 				     IEEE80211_CHANNEL_TIME);
377f3b85252SJohannes Berg 
378f3b85252SJohannes Berg 	return 0;
379f3b85252SJohannes Berg }
380f3b85252SJohannes Berg 
381f3b85252SJohannes Berg 
382f3b85252SJohannes Berg static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
383f3b85252SJohannes Berg 				  struct cfg80211_scan_request *req)
384f3b85252SJohannes Berg {
385f3b85252SJohannes Berg 	struct ieee80211_local *local = sdata->local;
386f3b85252SJohannes Berg 	int rc;
387f3b85252SJohannes Berg 
388e229f844SStanislaw Gruszka 	lockdep_assert_held(&local->mtx);
389e229f844SStanislaw Gruszka 
390f3b85252SJohannes Berg 	if (local->scan_req)
391f3b85252SJohannes Berg 		return -EBUSY;
392f3b85252SJohannes Berg 
3936e7e6213SJohn W. Linville 	if (!list_empty(&local->work_list)) {
3946e7e6213SJohn W. Linville 		/* wait for the work to finish/time out */
395c0ce77b8SJohannes Berg 		local->scan_req = req;
396c0ce77b8SJohannes Berg 		local->scan_sdata = sdata;
397c0ce77b8SJohannes Berg 		return 0;
398c0ce77b8SJohannes Berg 	}
399c0ce77b8SJohannes Berg 
400f3b85252SJohannes Berg 	if (local->ops->hw_scan) {
401f3b85252SJohannes Berg 		u8 *ies;
402f3b85252SJohannes Berg 
4034d36ec58SJohannes Berg 		local->hw_scan_req = kmalloc(
4044d36ec58SJohannes Berg 				sizeof(*local->hw_scan_req) +
4054d36ec58SJohannes Berg 				req->n_channels * sizeof(req->channels[0]) +
4064d36ec58SJohannes Berg 				2 + IEEE80211_MAX_SSID_LEN + local->scan_ies_len +
4074d36ec58SJohannes Berg 				req->ie_len, GFP_KERNEL);
4084d36ec58SJohannes Berg 		if (!local->hw_scan_req)
409f3b85252SJohannes Berg 			return -ENOMEM;
410f3b85252SJohannes Berg 
4114d36ec58SJohannes Berg 		local->hw_scan_req->ssids = req->ssids;
4124d36ec58SJohannes Berg 		local->hw_scan_req->n_ssids = req->n_ssids;
4134d36ec58SJohannes Berg 		ies = (u8 *)local->hw_scan_req +
4144d36ec58SJohannes Berg 			sizeof(*local->hw_scan_req) +
4154d36ec58SJohannes Berg 			req->n_channels * sizeof(req->channels[0]);
4164d36ec58SJohannes Berg 		local->hw_scan_req->ie = ies;
4174d36ec58SJohannes Berg 
4184d36ec58SJohannes Berg 		local->hw_scan_band = 0;
4196e7e6213SJohn W. Linville 
4206e7e6213SJohn W. Linville 		/*
4216e7e6213SJohn W. Linville 		 * After allocating local->hw_scan_req, we must
4226e7e6213SJohn W. Linville 		 * go through until ieee80211_prep_hw_scan(), so
4236e7e6213SJohn W. Linville 		 * anything that might be changed here and leave
4246e7e6213SJohn W. Linville 		 * this function early must not go after this
4256e7e6213SJohn W. Linville 		 * allocation.
4266e7e6213SJohn W. Linville 		 */
427f3b85252SJohannes Berg 	}
428f3b85252SJohannes Berg 
429f3b85252SJohannes Berg 	local->scan_req = req;
430f3b85252SJohannes Berg 	local->scan_sdata = sdata;
431f3b85252SJohannes Berg 
432f3b85252SJohannes Berg 	if (local->ops->hw_scan)
433fbe9c429SHelmut Schaa 		__set_bit(SCAN_HW_SCANNING, &local->scanning);
434f3b85252SJohannes Berg 	else
435fbe9c429SHelmut Schaa 		__set_bit(SCAN_SW_SCANNING, &local->scanning);
4366e7e6213SJohn W. Linville 
4375cff20e6SJohannes Berg 	ieee80211_recalc_idle(local);
438f3b85252SJohannes Berg 
4394d36ec58SJohannes Berg 	if (local->ops->hw_scan) {
4404d36ec58SJohannes Berg 		WARN_ON(!ieee80211_prep_hw_scan(local));
441a060bbfeSJohannes Berg 		rc = drv_hw_scan(local, sdata, local->hw_scan_req);
4424d36ec58SJohannes Berg 	} else
443f3b85252SJohannes Berg 		rc = ieee80211_start_sw_scan(local);
444f3b85252SJohannes Berg 
445f3b85252SJohannes Berg 	if (rc) {
4464d36ec58SJohannes Berg 		kfree(local->hw_scan_req);
4474d36ec58SJohannes Berg 		local->hw_scan_req = NULL;
448fbe9c429SHelmut Schaa 		local->scanning = 0;
449f3b85252SJohannes Berg 
4505cff20e6SJohannes Berg 		ieee80211_recalc_idle(local);
4515cff20e6SJohannes Berg 
452f3b85252SJohannes Berg 		local->scan_req = NULL;
453f3b85252SJohannes Berg 		local->scan_sdata = NULL;
454f3b85252SJohannes Berg 	}
455f3b85252SJohannes Berg 
456f3b85252SJohannes Berg 	return rc;
457f3b85252SJohannes Berg }
458f3b85252SJohannes Berg 
459df13cce5SHelmut Schaa static unsigned long
460df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
461df13cce5SHelmut Schaa {
462df13cce5SHelmut Schaa 	/*
463df13cce5SHelmut Schaa 	 * TODO: channel switching also consumes quite some time,
464df13cce5SHelmut Schaa 	 * add that delay as well to get a better estimation
465df13cce5SHelmut Schaa 	 */
466df13cce5SHelmut Schaa 	if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
467df13cce5SHelmut Schaa 		return IEEE80211_PASSIVE_CHANNEL_TIME;
468df13cce5SHelmut Schaa 	return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
469df13cce5SHelmut Schaa }
470df13cce5SHelmut Schaa 
471e229f844SStanislaw Gruszka static void ieee80211_scan_state_decision(struct ieee80211_local *local,
4722fb3f028SHelmut Schaa 					  unsigned long *next_delay)
4732fb3f028SHelmut Schaa {
474142b9f50SHelmut Schaa 	bool associated = false;
475df13cce5SHelmut Schaa 	bool tx_empty = true;
476df13cce5SHelmut Schaa 	bool bad_latency;
477df13cce5SHelmut Schaa 	bool listen_int_exceeded;
478df13cce5SHelmut Schaa 	unsigned long min_beacon_int = 0;
479142b9f50SHelmut Schaa 	struct ieee80211_sub_if_data *sdata;
480df13cce5SHelmut Schaa 	struct ieee80211_channel *next_chan;
481142b9f50SHelmut Schaa 
482df13cce5SHelmut Schaa 	/*
483df13cce5SHelmut Schaa 	 * check if at least one STA interface is associated,
484df13cce5SHelmut Schaa 	 * check if at least one STA interface has pending tx frames
485df13cce5SHelmut Schaa 	 * and grab the lowest used beacon interval
486df13cce5SHelmut Schaa 	 */
487142b9f50SHelmut Schaa 	mutex_lock(&local->iflist_mtx);
488142b9f50SHelmut Schaa 	list_for_each_entry(sdata, &local->interfaces, list) {
4899607e6b6SJohannes Berg 		if (!ieee80211_sdata_running(sdata))
490142b9f50SHelmut Schaa 			continue;
491142b9f50SHelmut Schaa 
492142b9f50SHelmut Schaa 		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
493142b9f50SHelmut Schaa 			if (sdata->u.mgd.associated) {
494142b9f50SHelmut Schaa 				associated = true;
495df13cce5SHelmut Schaa 
496df13cce5SHelmut Schaa 				if (sdata->vif.bss_conf.beacon_int <
497df13cce5SHelmut Schaa 				    min_beacon_int || min_beacon_int == 0)
498df13cce5SHelmut Schaa 					min_beacon_int =
499df13cce5SHelmut Schaa 						sdata->vif.bss_conf.beacon_int;
500df13cce5SHelmut Schaa 
501df13cce5SHelmut Schaa 				if (!qdisc_all_tx_empty(sdata->dev)) {
502df13cce5SHelmut Schaa 					tx_empty = false;
503142b9f50SHelmut Schaa 					break;
504142b9f50SHelmut Schaa 				}
505142b9f50SHelmut Schaa 			}
506142b9f50SHelmut Schaa 		}
507df13cce5SHelmut Schaa 	}
508142b9f50SHelmut Schaa 	mutex_unlock(&local->iflist_mtx);
509142b9f50SHelmut Schaa 
510b23b025fSBen Greear 	next_chan = local->scan_req->channels[local->scan_channel_idx];
511b23b025fSBen Greear 
512b23b025fSBen Greear 	if (ieee80211_cfg_on_oper_channel(local)) {
513b23b025fSBen Greear 		/* We're currently on operating channel. */
5144f2e9d91SBen Greear 		if (next_chan == local->oper_channel)
515b23b025fSBen Greear 			/* We don't need to move off of operating channel. */
516b23b025fSBen Greear 			local->next_scan_state = SCAN_SET_CHANNEL;
517b23b025fSBen Greear 		else
518b23b025fSBen Greear 			/*
519b23b025fSBen Greear 			 * We do need to leave operating channel, as next
520b23b025fSBen Greear 			 * scan is somewhere else.
521b23b025fSBen Greear 			 */
522b23b025fSBen Greear 			local->next_scan_state = SCAN_LEAVE_OPER_CHANNEL;
523b23b025fSBen Greear 	} else {
524142b9f50SHelmut Schaa 		/*
525142b9f50SHelmut Schaa 		 * we're currently scanning a different channel, let's
526df13cce5SHelmut Schaa 		 * see if we can scan another channel without interfering
527df13cce5SHelmut Schaa 		 * with the current traffic situation.
528df13cce5SHelmut Schaa 		 *
529df13cce5SHelmut Schaa 		 * Since we don't know if the AP has pending frames for us
530df13cce5SHelmut Schaa 		 * we can only check for our tx queues and use the current
531df13cce5SHelmut Schaa 		 * pm_qos requirements for rx. Hence, if no tx traffic occurs
532df13cce5SHelmut Schaa 		 * at all we will scan as many channels in a row as the pm_qos
533df13cce5SHelmut Schaa 		 * latency allows us to. Additionally we also check for the
534df13cce5SHelmut Schaa 		 * currently negotiated listen interval to prevent losing
535df13cce5SHelmut Schaa 		 * frames unnecessarily.
536df13cce5SHelmut Schaa 		 *
537df13cce5SHelmut Schaa 		 * Otherwise switch back to the operating channel.
538142b9f50SHelmut Schaa 		 */
539df13cce5SHelmut Schaa 
540df13cce5SHelmut Schaa 		bad_latency = time_after(jiffies +
541df13cce5SHelmut Schaa 				ieee80211_scan_get_channel_time(next_chan),
542df13cce5SHelmut Schaa 				local->leave_oper_channel_time +
543f8965467SLinus Torvalds 				usecs_to_jiffies(pm_qos_request(PM_QOS_NETWORK_LATENCY)));
544df13cce5SHelmut Schaa 
545df13cce5SHelmut Schaa 		listen_int_exceeded = time_after(jiffies +
546df13cce5SHelmut Schaa 				ieee80211_scan_get_channel_time(next_chan),
547df13cce5SHelmut Schaa 				local->leave_oper_channel_time +
548df13cce5SHelmut Schaa 				usecs_to_jiffies(min_beacon_int * 1024) *
549df13cce5SHelmut Schaa 				local->hw.conf.listen_interval);
550df13cce5SHelmut Schaa 
551df13cce5SHelmut Schaa 		if (associated && ( !tx_empty || bad_latency ||
552df13cce5SHelmut Schaa 		    listen_int_exceeded))
553977923b0SHelmut Schaa 			local->next_scan_state = SCAN_ENTER_OPER_CHANNEL;
554142b9f50SHelmut Schaa 		else
555977923b0SHelmut Schaa 			local->next_scan_state = SCAN_SET_CHANNEL;
556142b9f50SHelmut Schaa 	}
557142b9f50SHelmut Schaa 
558142b9f50SHelmut Schaa 	*next_delay = 0;
5592fb3f028SHelmut Schaa }
5602fb3f028SHelmut Schaa 
561142b9f50SHelmut Schaa static void ieee80211_scan_state_leave_oper_channel(struct ieee80211_local *local,
562142b9f50SHelmut Schaa 						    unsigned long *next_delay)
563142b9f50SHelmut Schaa {
564b23b025fSBen Greear 	/* PS will already be in off-channel mode,
565b23b025fSBen Greear 	 * we do that once at the beginning of scanning.
566b23b025fSBen Greear 	 */
567b23b025fSBen Greear 	ieee80211_offchannel_stop_vifs(local, false);
568142b9f50SHelmut Schaa 
569a80f7c0bSJohannes Berg 	/*
570a80f7c0bSJohannes Berg 	 * What if the nullfunc frames didn't arrive?
571a80f7c0bSJohannes Berg 	 */
572a80f7c0bSJohannes Berg 	drv_flush(local, false);
573a80f7c0bSJohannes Berg 	if (local->ops->flush)
574a80f7c0bSJohannes Berg 		*next_delay = 0;
575a80f7c0bSJohannes Berg 	else
576142b9f50SHelmut Schaa 		*next_delay = HZ / 10;
577a80f7c0bSJohannes Berg 
578df13cce5SHelmut Schaa 	/* remember when we left the operating channel */
579df13cce5SHelmut Schaa 	local->leave_oper_channel_time = jiffies;
580df13cce5SHelmut Schaa 
581a80f7c0bSJohannes Berg 	/* advance to the next channel to be scanned */
582977923b0SHelmut Schaa 	local->next_scan_state = SCAN_SET_CHANNEL;
583142b9f50SHelmut Schaa }
584142b9f50SHelmut Schaa 
585142b9f50SHelmut Schaa static void ieee80211_scan_state_enter_oper_channel(struct ieee80211_local *local,
586142b9f50SHelmut Schaa 						    unsigned long *next_delay)
587142b9f50SHelmut Schaa {
588142b9f50SHelmut Schaa 	/* switch back to the operating channel */
589142b9f50SHelmut Schaa 	local->scan_channel = NULL;
590b23b025fSBen Greear 	if (!ieee80211_cfg_on_oper_channel(local))
591142b9f50SHelmut Schaa 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
592142b9f50SHelmut Schaa 
593142b9f50SHelmut Schaa 	/*
594b23b025fSBen Greear 	 * Re-enable vifs and beaconing.  Leave PS
595b23b025fSBen Greear 	 * in off-channel state..will put that back
596b23b025fSBen Greear 	 * on-channel at the end of scanning.
597142b9f50SHelmut Schaa 	 */
598b23b025fSBen Greear 	ieee80211_offchannel_return(local, true, false);
599142b9f50SHelmut Schaa 
600142b9f50SHelmut Schaa 	*next_delay = HZ / 5;
601977923b0SHelmut Schaa 	local->next_scan_state = SCAN_DECISION;
602142b9f50SHelmut Schaa }
603142b9f50SHelmut Schaa 
6042fb3f028SHelmut Schaa static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
6057d3be3ccSHelmut Schaa 					     unsigned long *next_delay)
6067d3be3ccSHelmut Schaa {
6077d3be3ccSHelmut Schaa 	int skip;
6087d3be3ccSHelmut Schaa 	struct ieee80211_channel *chan;
6097d3be3ccSHelmut Schaa 
6107d3be3ccSHelmut Schaa 	skip = 0;
6117d3be3ccSHelmut Schaa 	chan = local->scan_req->channels[local->scan_channel_idx];
6127d3be3ccSHelmut Schaa 
6137d3be3ccSHelmut Schaa 	local->scan_channel = chan;
614b23b025fSBen Greear 
615b23b025fSBen Greear 	/* Only call hw-config if we really need to change channels. */
6164f2e9d91SBen Greear 	if (chan != local->hw.conf.channel)
617584991dcSJohannes Berg 		if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
6187d3be3ccSHelmut Schaa 			skip = 1;
6197d3be3ccSHelmut Schaa 
6207d3be3ccSHelmut Schaa 	/* advance state machine to next channel/band */
6217d3be3ccSHelmut Schaa 	local->scan_channel_idx++;
6227d3be3ccSHelmut Schaa 
6230ee9c13cSHelmut Schaa 	if (skip) {
6240ee9c13cSHelmut Schaa 		/* if we skip this channel return to the decision state */
6250ee9c13cSHelmut Schaa 		local->next_scan_state = SCAN_DECISION;
6262fb3f028SHelmut Schaa 		return;
6270ee9c13cSHelmut Schaa 	}
6287d3be3ccSHelmut Schaa 
6297d3be3ccSHelmut Schaa 	/*
6307d3be3ccSHelmut Schaa 	 * Probe delay is used to update the NAV, cf. 11.1.3.2.2
6317d3be3ccSHelmut Schaa 	 * (which unfortunately doesn't say _why_ step a) is done,
6327d3be3ccSHelmut Schaa 	 * but it waits for the probe delay or until a frame is
6337d3be3ccSHelmut Schaa 	 * received - and the received frame would update the NAV).
6347d3be3ccSHelmut Schaa 	 * For now, we do not support waiting until a frame is
6357d3be3ccSHelmut Schaa 	 * received.
6367d3be3ccSHelmut Schaa 	 *
6377d3be3ccSHelmut Schaa 	 * In any case, it is not necessary for a passive scan.
6387d3be3ccSHelmut Schaa 	 */
6397d3be3ccSHelmut Schaa 	if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
6407d3be3ccSHelmut Schaa 	    !local->scan_req->n_ssids) {
6417d3be3ccSHelmut Schaa 		*next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
642977923b0SHelmut Schaa 		local->next_scan_state = SCAN_DECISION;
6432fb3f028SHelmut Schaa 		return;
6447d3be3ccSHelmut Schaa 	}
6457d3be3ccSHelmut Schaa 
6462fb3f028SHelmut Schaa 	/* active scan, send probes */
6477d3be3ccSHelmut Schaa 	*next_delay = IEEE80211_PROBE_DELAY;
648977923b0SHelmut Schaa 	local->next_scan_state = SCAN_SEND_PROBE;
6497d3be3ccSHelmut Schaa }
6507d3be3ccSHelmut Schaa 
6517d3be3ccSHelmut Schaa static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
6527d3be3ccSHelmut Schaa 					    unsigned long *next_delay)
6537d3be3ccSHelmut Schaa {
6547d3be3ccSHelmut Schaa 	int i;
6557d3be3ccSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = local->scan_sdata;
6567d3be3ccSHelmut Schaa 
6577d3be3ccSHelmut Schaa 	for (i = 0; i < local->scan_req->n_ssids; i++)
6587d3be3ccSHelmut Schaa 		ieee80211_send_probe_req(
6597d3be3ccSHelmut Schaa 			sdata, NULL,
6607d3be3ccSHelmut Schaa 			local->scan_req->ssids[i].ssid,
6617d3be3ccSHelmut Schaa 			local->scan_req->ssids[i].ssid_len,
662*a806c558SPaul Stewart 			local->scan_req->ie, local->scan_req->ie_len,
663*a806c558SPaul Stewart 			false);
6647d3be3ccSHelmut Schaa 
6657d3be3ccSHelmut Schaa 	/*
6667d3be3ccSHelmut Schaa 	 * After sending probe requests, wait for probe responses
6677d3be3ccSHelmut Schaa 	 * on the channel.
6687d3be3ccSHelmut Schaa 	 */
6697d3be3ccSHelmut Schaa 	*next_delay = IEEE80211_CHANNEL_TIME;
670977923b0SHelmut Schaa 	local->next_scan_state = SCAN_DECISION;
6717d3be3ccSHelmut Schaa }
6727d3be3ccSHelmut Schaa 
673c2b13452SJohannes Berg void ieee80211_scan_work(struct work_struct *work)
6740a51b27eSJohannes Berg {
6750a51b27eSJohannes Berg 	struct ieee80211_local *local =
6760a51b27eSJohannes Berg 		container_of(work, struct ieee80211_local, scan_work.work);
677d07bfd8bSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
6780a51b27eSJohannes Berg 	unsigned long next_delay = 0;
679d07bfd8bSJohannes Berg 	bool aborted, hw_scan;
6808789d459SJohannes Berg 
681259b62e3SStanislaw Gruszka 	mutex_lock(&local->mtx);
682259b62e3SStanislaw Gruszka 
683d07bfd8bSJohannes Berg 	sdata = local->scan_sdata;
684d07bfd8bSJohannes Berg 
685259b62e3SStanislaw Gruszka 	if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
6868789d459SJohannes Berg 		aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
687259b62e3SStanislaw Gruszka 		goto out_complete;
6888789d459SJohannes Berg 	}
6898789d459SJohannes Berg 
690259b62e3SStanislaw Gruszka 	if (!sdata || !local->scan_req)
691259b62e3SStanislaw Gruszka 		goto out;
692f3b85252SJohannes Berg 
693fbe9c429SHelmut Schaa 	if (local->scan_req && !local->scanning) {
694f3b85252SJohannes Berg 		struct cfg80211_scan_request *req = local->scan_req;
695f3b85252SJohannes Berg 		int rc;
696f3b85252SJohannes Berg 
697f3b85252SJohannes Berg 		local->scan_req = NULL;
69815db0b7fSJohannes Berg 		local->scan_sdata = NULL;
699f3b85252SJohannes Berg 
700f3b85252SJohannes Berg 		rc = __ieee80211_start_scan(sdata, req);
701259b62e3SStanislaw Gruszka 		if (rc) {
7023aed49efSStanislaw Gruszka 			/* need to complete scan in cfg80211 */
7033aed49efSStanislaw Gruszka 			local->scan_req = req;
704259b62e3SStanislaw Gruszka 			aborted = true;
705259b62e3SStanislaw Gruszka 			goto out_complete;
706259b62e3SStanislaw Gruszka 		} else
707259b62e3SStanislaw Gruszka 			goto out;
708f3b85252SJohannes Berg 	}
709f3b85252SJohannes Berg 
7105bc75728SJohannes Berg 	/*
7115bc75728SJohannes Berg 	 * Avoid re-scheduling when the sdata is going away.
7125bc75728SJohannes Berg 	 */
7139607e6b6SJohannes Berg 	if (!ieee80211_sdata_running(sdata)) {
714259b62e3SStanislaw Gruszka 		aborted = true;
715259b62e3SStanislaw Gruszka 		goto out_complete;
716f3b85252SJohannes Berg 	}
7170a51b27eSJohannes Berg 
718f502d09bSHelmut Schaa 	/*
719f502d09bSHelmut Schaa 	 * as long as no delay is required advance immediately
720f502d09bSHelmut Schaa 	 * without scheduling a new work
721f502d09bSHelmut Schaa 	 */
722f502d09bSHelmut Schaa 	do {
723c29acf20SRajkumar Manoharan 		if (!ieee80211_sdata_running(sdata)) {
724c29acf20SRajkumar Manoharan 			aborted = true;
725c29acf20SRajkumar Manoharan 			goto out_complete;
726c29acf20SRajkumar Manoharan 		}
727c29acf20SRajkumar Manoharan 
728977923b0SHelmut Schaa 		switch (local->next_scan_state) {
7292fb3f028SHelmut Schaa 		case SCAN_DECISION:
730e229f844SStanislaw Gruszka 			/* if no more bands/channels left, complete scan */
731e229f844SStanislaw Gruszka 			if (local->scan_channel_idx >= local->scan_req->n_channels) {
732e229f844SStanislaw Gruszka 				aborted = false;
733e229f844SStanislaw Gruszka 				goto out_complete;
734e229f844SStanislaw Gruszka 			}
735e229f844SStanislaw Gruszka 			ieee80211_scan_state_decision(local, &next_delay);
7360a51b27eSJohannes Berg 			break;
7372fb3f028SHelmut Schaa 		case SCAN_SET_CHANNEL:
7382fb3f028SHelmut Schaa 			ieee80211_scan_state_set_channel(local, &next_delay);
7392fb3f028SHelmut Schaa 			break;
7400a51b27eSJohannes Berg 		case SCAN_SEND_PROBE:
7417d3be3ccSHelmut Schaa 			ieee80211_scan_state_send_probe(local, &next_delay);
7420a51b27eSJohannes Berg 			break;
743142b9f50SHelmut Schaa 		case SCAN_LEAVE_OPER_CHANNEL:
744142b9f50SHelmut Schaa 			ieee80211_scan_state_leave_oper_channel(local, &next_delay);
745142b9f50SHelmut Schaa 			break;
746142b9f50SHelmut Schaa 		case SCAN_ENTER_OPER_CHANNEL:
747142b9f50SHelmut Schaa 			ieee80211_scan_state_enter_oper_channel(local, &next_delay);
748142b9f50SHelmut Schaa 			break;
7490a51b27eSJohannes Berg 		}
750f502d09bSHelmut Schaa 	} while (next_delay == 0);
7510a51b27eSJohannes Berg 
75242935ecaSLuis R. Rodriguez 	ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
753d07bfd8bSJohannes Berg 	goto out;
754259b62e3SStanislaw Gruszka 
755259b62e3SStanislaw Gruszka out_complete:
756e229f844SStanislaw Gruszka 	hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
757d07bfd8bSJohannes Berg 	__ieee80211_scan_completed(&local->hw, aborted, hw_scan);
758259b62e3SStanislaw Gruszka out:
759259b62e3SStanislaw Gruszka 	mutex_unlock(&local->mtx);
7600a51b27eSJohannes Berg }
7610a51b27eSJohannes Berg 
762c2b13452SJohannes Berg int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
7632a519311SJohannes Berg 			   struct cfg80211_scan_request *req)
7640a51b27eSJohannes Berg {
765f3b85252SJohannes Berg 	int res;
7660a51b27eSJohannes Berg 
767a1699b75SJohannes Berg 	mutex_lock(&sdata->local->mtx);
768f3b85252SJohannes Berg 	res = __ieee80211_start_scan(sdata, req);
769a1699b75SJohannes Berg 	mutex_unlock(&sdata->local->mtx);
7702a519311SJohannes Berg 
771f3b85252SJohannes Berg 	return res;
7720a51b27eSJohannes Berg }
7730a51b27eSJohannes Berg 
774f3b85252SJohannes Berg int ieee80211_request_internal_scan(struct ieee80211_sub_if_data *sdata,
775be4a4b6aSJohannes Berg 				    const u8 *ssid, u8 ssid_len,
776be4a4b6aSJohannes Berg 				    struct ieee80211_channel *chan)
777f3b85252SJohannes Berg {
778f3b85252SJohannes Berg 	struct ieee80211_local *local = sdata->local;
779f3b85252SJohannes Berg 	int ret = -EBUSY;
780fb63bc41SGertjan van Wingerde 	enum ieee80211_band band;
7819116dd01SJohannes Berg 
782a1699b75SJohannes Berg 	mutex_lock(&local->mtx);
783f3b85252SJohannes Berg 
784f3b85252SJohannes Berg 	/* busy scanning */
785f3b85252SJohannes Berg 	if (local->scan_req)
786f3b85252SJohannes Berg 		goto unlock;
787f3b85252SJohannes Berg 
788be4a4b6aSJohannes Berg 	/* fill internal scan request */
789be4a4b6aSJohannes Berg 	if (!chan) {
790be4a4b6aSJohannes Berg 		int i, nchan = 0;
791be4a4b6aSJohannes Berg 
792be4a4b6aSJohannes Berg 		for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
793be4a4b6aSJohannes Berg 			if (!local->hw.wiphy->bands[band])
794be4a4b6aSJohannes Berg 				continue;
795be4a4b6aSJohannes Berg 			for (i = 0;
796be4a4b6aSJohannes Berg 			     i < local->hw.wiphy->bands[band]->n_channels;
797be4a4b6aSJohannes Berg 			     i++) {
798be4a4b6aSJohannes Berg 				local->int_scan_req->channels[nchan] =
799be4a4b6aSJohannes Berg 				    &local->hw.wiphy->bands[band]->channels[i];
800be4a4b6aSJohannes Berg 				nchan++;
801be4a4b6aSJohannes Berg 			}
802be4a4b6aSJohannes Berg 		}
803be4a4b6aSJohannes Berg 
804be4a4b6aSJohannes Berg 		local->int_scan_req->n_channels = nchan;
805be4a4b6aSJohannes Berg 	} else {
806be4a4b6aSJohannes Berg 		local->int_scan_req->channels[0] = chan;
807be4a4b6aSJohannes Berg 		local->int_scan_req->n_channels = 1;
808be4a4b6aSJohannes Berg 	}
809be4a4b6aSJohannes Berg 
810be4a4b6aSJohannes Berg 	local->int_scan_req->ssids = &local->scan_ssid;
811be4a4b6aSJohannes Berg 	local->int_scan_req->n_ssids = 1;
8125ba63533SJohannes Berg 	memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
8135ba63533SJohannes Berg 	local->int_scan_req->ssids[0].ssid_len = ssid_len;
814f3b85252SJohannes Berg 
8155ba63533SJohannes Berg 	ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
816f3b85252SJohannes Berg  unlock:
817a1699b75SJohannes Berg 	mutex_unlock(&local->mtx);
818f3b85252SJohannes Berg 	return ret;
8190a51b27eSJohannes Berg }
8205bb644a0SJohannes Berg 
8214136c422SStanislaw Gruszka /*
8224136c422SStanislaw Gruszka  * Only call this function when a scan can't be queued -- under RTNL.
8234136c422SStanislaw Gruszka  */
8245bb644a0SJohannes Berg void ieee80211_scan_cancel(struct ieee80211_local *local)
8255bb644a0SJohannes Berg {
8265bb644a0SJohannes Berg 	/*
827b856439bSEliad Peller 	 * We are canceling software scan, or deferred scan that was not
8284136c422SStanislaw Gruszka 	 * yet really started (see __ieee80211_start_scan ).
8294136c422SStanislaw Gruszka 	 *
8304136c422SStanislaw Gruszka 	 * Regarding hardware scan:
8314136c422SStanislaw Gruszka 	 * - we can not call  __ieee80211_scan_completed() as when
8324136c422SStanislaw Gruszka 	 *   SCAN_HW_SCANNING bit is set this function change
8334136c422SStanislaw Gruszka 	 *   local->hw_scan_req to operate on 5G band, what race with
8344136c422SStanislaw Gruszka 	 *   driver which can use local->hw_scan_req
8354136c422SStanislaw Gruszka 	 *
8364136c422SStanislaw Gruszka 	 * - we can not cancel scan_work since driver can schedule it
8374136c422SStanislaw Gruszka 	 *   by ieee80211_scan_completed(..., true) to finish scan
8384136c422SStanislaw Gruszka 	 *
839b856439bSEliad Peller 	 * Hence we only call the cancel_hw_scan() callback, but the low-level
840b856439bSEliad Peller 	 * driver is still responsible for calling ieee80211_scan_completed()
841b856439bSEliad Peller 	 * after the scan was completed/aborted.
8425bb644a0SJohannes Berg 	 */
8434136c422SStanislaw Gruszka 
844a1699b75SJohannes Berg 	mutex_lock(&local->mtx);
845b856439bSEliad Peller 	if (!local->scan_req)
846b856439bSEliad Peller 		goto out;
847b856439bSEliad Peller 
848b856439bSEliad Peller 	if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
849b856439bSEliad Peller 		if (local->ops->cancel_hw_scan)
850b856439bSEliad Peller 			drv_cancel_hw_scan(local, local->scan_sdata);
851b856439bSEliad Peller 		goto out;
852b856439bSEliad Peller 	}
853b856439bSEliad Peller 
854d07bfd8bSJohannes Berg 	/*
855d07bfd8bSJohannes Berg 	 * If the work is currently running, it must be blocked on
856d07bfd8bSJohannes Berg 	 * the mutex, but we'll set scan_sdata = NULL and it'll
857d07bfd8bSJohannes Berg 	 * simply exit once it acquires the mutex.
858d07bfd8bSJohannes Berg 	 */
859d07bfd8bSJohannes Berg 	cancel_delayed_work(&local->scan_work);
860d07bfd8bSJohannes Berg 	/* and clean up */
861d07bfd8bSJohannes Berg 	__ieee80211_scan_completed(&local->hw, true, false);
862b856439bSEliad Peller out:
863d07bfd8bSJohannes Berg 	mutex_unlock(&local->mtx);
8645bb644a0SJohannes Berg }
86579f460caSLuciano Coelho 
86679f460caSLuciano Coelho int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
86779f460caSLuciano Coelho 				       struct cfg80211_sched_scan_request *req)
86879f460caSLuciano Coelho {
86979f460caSLuciano Coelho 	struct ieee80211_local *local = sdata->local;
87079f460caSLuciano Coelho 	int ret, i;
87179f460caSLuciano Coelho 
87279f460caSLuciano Coelho 	mutex_lock(&sdata->local->mtx);
87379f460caSLuciano Coelho 
87479f460caSLuciano Coelho 	if (local->sched_scanning) {
87579f460caSLuciano Coelho 		ret = -EBUSY;
87679f460caSLuciano Coelho 		goto out;
87779f460caSLuciano Coelho 	}
87879f460caSLuciano Coelho 
87979f460caSLuciano Coelho 	if (!local->ops->sched_scan_start) {
88079f460caSLuciano Coelho 		ret = -ENOTSUPP;
88179f460caSLuciano Coelho 		goto out;
88279f460caSLuciano Coelho 	}
88379f460caSLuciano Coelho 
88479f460caSLuciano Coelho 	for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
88579f460caSLuciano Coelho 		local->sched_scan_ies.ie[i] = kzalloc(2 +
88679f460caSLuciano Coelho 						      IEEE80211_MAX_SSID_LEN +
88779f460caSLuciano Coelho 						      local->scan_ies_len,
88879f460caSLuciano Coelho 						      GFP_KERNEL);
88979f460caSLuciano Coelho 		if (!local->sched_scan_ies.ie[i]) {
89079f460caSLuciano Coelho 			ret = -ENOMEM;
89179f460caSLuciano Coelho 			goto out_free;
89279f460caSLuciano Coelho 		}
89379f460caSLuciano Coelho 
89479f460caSLuciano Coelho 		local->sched_scan_ies.len[i] =
89579f460caSLuciano Coelho 			ieee80211_build_preq_ies(local,
89679f460caSLuciano Coelho 						 local->sched_scan_ies.ie[i],
89779f460caSLuciano Coelho 						 req->ie, req->ie_len, i,
89879f460caSLuciano Coelho 						 (u32) -1, 0);
89979f460caSLuciano Coelho 	}
90079f460caSLuciano Coelho 
90179f460caSLuciano Coelho 	ret = drv_sched_scan_start(local, sdata, req,
90279f460caSLuciano Coelho 				   &local->sched_scan_ies);
90379f460caSLuciano Coelho 	if (ret == 0) {
90479f460caSLuciano Coelho 		local->sched_scanning = true;
90579f460caSLuciano Coelho 		goto out;
90679f460caSLuciano Coelho 	}
90779f460caSLuciano Coelho 
90879f460caSLuciano Coelho out_free:
90979f460caSLuciano Coelho 	while (i > 0)
91079f460caSLuciano Coelho 		kfree(local->sched_scan_ies.ie[--i]);
91179f460caSLuciano Coelho out:
91279f460caSLuciano Coelho 	mutex_unlock(&sdata->local->mtx);
91379f460caSLuciano Coelho 	return ret;
91479f460caSLuciano Coelho }
91579f460caSLuciano Coelho 
91685a9994aSLuciano Coelho int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata)
91779f460caSLuciano Coelho {
91879f460caSLuciano Coelho 	struct ieee80211_local *local = sdata->local;
91979f460caSLuciano Coelho 	int ret = 0, i;
92079f460caSLuciano Coelho 
92179f460caSLuciano Coelho 	mutex_lock(&sdata->local->mtx);
92279f460caSLuciano Coelho 
92379f460caSLuciano Coelho 	if (!local->ops->sched_scan_stop) {
92479f460caSLuciano Coelho 		ret = -ENOTSUPP;
92579f460caSLuciano Coelho 		goto out;
92679f460caSLuciano Coelho 	}
92779f460caSLuciano Coelho 
92879f460caSLuciano Coelho 	if (local->sched_scanning) {
92979f460caSLuciano Coelho 		for (i = 0; i < IEEE80211_NUM_BANDS; i++)
93079f460caSLuciano Coelho 			kfree(local->sched_scan_ies.ie[i]);
93179f460caSLuciano Coelho 
93279f460caSLuciano Coelho 		drv_sched_scan_stop(local, sdata);
93379f460caSLuciano Coelho 		local->sched_scanning = false;
93479f460caSLuciano Coelho 	}
93579f460caSLuciano Coelho out:
93679f460caSLuciano Coelho 	mutex_unlock(&sdata->local->mtx);
93779f460caSLuciano Coelho 
93879f460caSLuciano Coelho 	return ret;
93979f460caSLuciano Coelho }
94079f460caSLuciano Coelho 
94179f460caSLuciano Coelho void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
94279f460caSLuciano Coelho {
94379f460caSLuciano Coelho 	struct ieee80211_local *local = hw_to_local(hw);
94479f460caSLuciano Coelho 
94579f460caSLuciano Coelho 	trace_api_sched_scan_results(local);
94679f460caSLuciano Coelho 
94779f460caSLuciano Coelho 	cfg80211_sched_scan_results(hw->wiphy);
94879f460caSLuciano Coelho }
94979f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_results);
95079f460caSLuciano Coelho 
95185a9994aSLuciano Coelho void ieee80211_sched_scan_stopped_work(struct work_struct *work)
95285a9994aSLuciano Coelho {
95385a9994aSLuciano Coelho 	struct ieee80211_local *local =
95485a9994aSLuciano Coelho 		container_of(work, struct ieee80211_local,
95585a9994aSLuciano Coelho 			     sched_scan_stopped_work);
95685a9994aSLuciano Coelho 	int i;
95785a9994aSLuciano Coelho 
95885a9994aSLuciano Coelho 	mutex_lock(&local->mtx);
95985a9994aSLuciano Coelho 
96085a9994aSLuciano Coelho 	if (!local->sched_scanning) {
96185a9994aSLuciano Coelho 		mutex_unlock(&local->mtx);
96285a9994aSLuciano Coelho 		return;
96385a9994aSLuciano Coelho 	}
96485a9994aSLuciano Coelho 
96585a9994aSLuciano Coelho 	for (i = 0; i < IEEE80211_NUM_BANDS; i++)
96685a9994aSLuciano Coelho 		kfree(local->sched_scan_ies.ie[i]);
96785a9994aSLuciano Coelho 
96885a9994aSLuciano Coelho 	local->sched_scanning = false;
96985a9994aSLuciano Coelho 
97085a9994aSLuciano Coelho 	mutex_unlock(&local->mtx);
97185a9994aSLuciano Coelho 
97285a9994aSLuciano Coelho 	cfg80211_sched_scan_stopped(local->hw.wiphy);
97385a9994aSLuciano Coelho }
97485a9994aSLuciano Coelho 
97579f460caSLuciano Coelho void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
97679f460caSLuciano Coelho {
97779f460caSLuciano Coelho 	struct ieee80211_local *local = hw_to_local(hw);
97879f460caSLuciano Coelho 
97979f460caSLuciano Coelho 	trace_api_sched_scan_stopped(local);
98079f460caSLuciano Coelho 
98185a9994aSLuciano Coelho 	ieee80211_queue_work(&local->hw, &local->sched_scan_stopped_work);
98279f460caSLuciano Coelho }
98379f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_stopped);
984