xref: /linux/net/mac80211/scan.c (revision 80e775bf08f1915870fbb0c1c7a45a3fdc291721)
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 
1500d3f14cSJohannes Berg /* TODO: figure out how to avoid that the "current BSS" expires */
165484e237SJohannes Berg 
170a51b27eSJohannes Berg #include <linux/wireless.h>
180a51b27eSJohannes Berg #include <linux/if_arp.h>
19078e1e60SJohannes Berg #include <linux/rtnetlink.h>
200a51b27eSJohannes Berg #include <net/mac80211.h>
210a51b27eSJohannes Berg #include <net/iw_handler.h>
220a51b27eSJohannes Berg 
230a51b27eSJohannes Berg #include "ieee80211_i.h"
245484e237SJohannes Berg #include "mesh.h"
250a51b27eSJohannes Berg 
260a51b27eSJohannes Berg #define IEEE80211_PROBE_DELAY (HZ / 33)
270a51b27eSJohannes Berg #define IEEE80211_CHANNEL_TIME (HZ / 33)
280a51b27eSJohannes Berg #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
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 {
3400d3f14cSJohannes Berg 	return (void *)cfg80211_get_bss(local->hw.wiphy,
3500d3f14cSJohannes Berg 					ieee80211_get_channel(local->hw.wiphy,
3600d3f14cSJohannes Berg 							      freq),
3700d3f14cSJohannes Berg 					bssid, ssid, ssid_len,
3800d3f14cSJohannes Berg 					0, 0);
395484e237SJohannes Berg }
405484e237SJohannes Berg 
4100d3f14cSJohannes Berg static void ieee80211_rx_bss_free(struct cfg80211_bss *cbss)
425484e237SJohannes Berg {
4300d3f14cSJohannes Berg 	struct ieee80211_bss *bss = (void *)cbss;
445484e237SJohannes Berg 
455484e237SJohannes Berg 	kfree(bss_mesh_id(bss));
465484e237SJohannes Berg 	kfree(bss_mesh_cfg(bss));
475484e237SJohannes Berg }
485484e237SJohannes Berg 
495484e237SJohannes Berg void ieee80211_rx_bss_put(struct ieee80211_local *local,
50c2b13452SJohannes Berg 			  struct ieee80211_bss *bss)
515484e237SJohannes Berg {
5200d3f14cSJohannes Berg 	cfg80211_put_bss((struct cfg80211_bss *)bss);
535484e237SJohannes Berg }
545484e237SJohannes Berg 
55c2b13452SJohannes Berg struct ieee80211_bss *
565484e237SJohannes Berg ieee80211_bss_info_update(struct ieee80211_local *local,
575484e237SJohannes Berg 			  struct ieee80211_rx_status *rx_status,
585484e237SJohannes Berg 			  struct ieee80211_mgmt *mgmt,
595484e237SJohannes Berg 			  size_t len,
605484e237SJohannes Berg 			  struct ieee802_11_elems *elems,
612a519311SJohannes Berg 			  struct ieee80211_channel *channel,
622a519311SJohannes Berg 			  bool beacon)
635484e237SJohannes Berg {
64c2b13452SJohannes Berg 	struct ieee80211_bss *bss;
6500d3f14cSJohannes Berg 	int clen;
662a519311SJohannes Berg 	s32 signal = 0;
672a519311SJohannes Berg 
6877965c97SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
692a519311SJohannes Berg 		signal = rx_status->signal * 100;
7077965c97SJohannes Berg 	else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
712a519311SJohannes Berg 		signal = (rx_status->signal * 100) / local->hw.max_signal;
722a519311SJohannes Berg 
7300d3f14cSJohannes Berg 	bss = (void *)cfg80211_inform_bss_frame(local->hw.wiphy, channel,
7477965c97SJohannes Berg 						mgmt, len, signal, GFP_ATOMIC);
755484e237SJohannes Berg 
765484e237SJohannes Berg 	if (!bss)
775484e237SJohannes Berg 		return NULL;
7800d3f14cSJohannes Berg 
7900d3f14cSJohannes Berg 	bss->cbss.free_priv = ieee80211_rx_bss_free;
805484e237SJohannes Berg 
815484e237SJohannes Berg 	/* save the ERP value so that it is available at association time */
825484e237SJohannes Berg 	if (elems->erp_info && elems->erp_info_len >= 1) {
835484e237SJohannes Berg 		bss->erp_value = elems->erp_info[0];
845484e237SJohannes Berg 		bss->has_erp_value = 1;
855484e237SJohannes Berg 	}
865484e237SJohannes Berg 
875484e237SJohannes Berg 	if (elems->tim) {
885484e237SJohannes Berg 		struct ieee80211_tim_ie *tim_ie =
895484e237SJohannes Berg 			(struct ieee80211_tim_ie *)elems->tim;
905484e237SJohannes Berg 		bss->dtim_period = tim_ie->dtim_period;
915484e237SJohannes Berg 	}
925484e237SJohannes Berg 
935484e237SJohannes Berg 	/* set default value for buggy APs */
945484e237SJohannes Berg 	if (!elems->tim || bss->dtim_period == 0)
955484e237SJohannes Berg 		bss->dtim_period = 1;
965484e237SJohannes Berg 
975484e237SJohannes Berg 	bss->supp_rates_len = 0;
985484e237SJohannes Berg 	if (elems->supp_rates) {
995484e237SJohannes Berg 		clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1005484e237SJohannes Berg 		if (clen > elems->supp_rates_len)
1015484e237SJohannes Berg 			clen = elems->supp_rates_len;
1025484e237SJohannes Berg 		memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
1035484e237SJohannes Berg 		       clen);
1045484e237SJohannes Berg 		bss->supp_rates_len += clen;
1055484e237SJohannes Berg 	}
1065484e237SJohannes Berg 	if (elems->ext_supp_rates) {
1075484e237SJohannes Berg 		clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1085484e237SJohannes Berg 		if (clen > elems->ext_supp_rates_len)
1095484e237SJohannes Berg 			clen = elems->ext_supp_rates_len;
1105484e237SJohannes Berg 		memcpy(&bss->supp_rates[bss->supp_rates_len],
1115484e237SJohannes Berg 		       elems->ext_supp_rates, clen);
1125484e237SJohannes Berg 		bss->supp_rates_len += clen;
1135484e237SJohannes Berg 	}
1145484e237SJohannes Berg 
1155484e237SJohannes Berg 	bss->wmm_used = elems->wmm_param || elems->wmm_info;
1165484e237SJohannes Berg 
1175484e237SJohannes Berg 	if (!beacon)
1185484e237SJohannes Berg 		bss->last_probe_resp = jiffies;
1195484e237SJohannes Berg 
1205484e237SJohannes Berg 	return bss;
1215484e237SJohannes Berg }
1220a51b27eSJohannes Berg 
1237a947080SVasanthakumar Thiagarajan void ieee80211_rx_bss_remove(struct ieee80211_sub_if_data *sdata, u8 *bssid,
1247a947080SVasanthakumar Thiagarajan 			     int freq, u8 *ssid, u8 ssid_len)
1257a947080SVasanthakumar Thiagarajan {
1267a947080SVasanthakumar Thiagarajan 	struct ieee80211_bss *bss;
1277a947080SVasanthakumar Thiagarajan 	struct ieee80211_local *local = sdata->local;
1287a947080SVasanthakumar Thiagarajan 
1297a947080SVasanthakumar Thiagarajan 	bss = ieee80211_rx_bss_get(local, bssid, freq, ssid, ssid_len);
1307a947080SVasanthakumar Thiagarajan 	if (bss) {
13100d3f14cSJohannes Berg 		cfg80211_unlink_bss(local->hw.wiphy, (void *)bss);
1327a947080SVasanthakumar Thiagarajan 		ieee80211_rx_bss_put(local, bss);
1337a947080SVasanthakumar Thiagarajan 	}
1347a947080SVasanthakumar Thiagarajan }
1357a947080SVasanthakumar Thiagarajan 
13698c8fccfSJohannes Berg ieee80211_rx_result
137c2b13452SJohannes Berg ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
13898c8fccfSJohannes Berg 		  struct ieee80211_rx_status *rx_status)
13998c8fccfSJohannes Berg {
14098c8fccfSJohannes Berg 	struct ieee80211_mgmt *mgmt;
141c2b13452SJohannes Berg 	struct ieee80211_bss *bss;
14298c8fccfSJohannes Berg 	u8 *elements;
14398c8fccfSJohannes Berg 	struct ieee80211_channel *channel;
14498c8fccfSJohannes Berg 	size_t baselen;
14598c8fccfSJohannes Berg 	int freq;
14698c8fccfSJohannes Berg 	__le16 fc;
14798c8fccfSJohannes Berg 	bool presp, beacon = false;
14898c8fccfSJohannes Berg 	struct ieee802_11_elems elems;
14998c8fccfSJohannes Berg 
15098c8fccfSJohannes Berg 	if (skb->len < 2)
15198c8fccfSJohannes Berg 		return RX_DROP_UNUSABLE;
15298c8fccfSJohannes Berg 
15398c8fccfSJohannes Berg 	mgmt = (struct ieee80211_mgmt *) skb->data;
15498c8fccfSJohannes Berg 	fc = mgmt->frame_control;
15598c8fccfSJohannes Berg 
15698c8fccfSJohannes Berg 	if (ieee80211_is_ctl(fc))
15798c8fccfSJohannes Berg 		return RX_CONTINUE;
15898c8fccfSJohannes Berg 
15998c8fccfSJohannes Berg 	if (skb->len < 24)
16098c8fccfSJohannes Berg 		return RX_DROP_MONITOR;
16198c8fccfSJohannes Berg 
16298c8fccfSJohannes Berg 	presp = ieee80211_is_probe_resp(fc);
16398c8fccfSJohannes Berg 	if (presp) {
16498c8fccfSJohannes Berg 		/* ignore ProbeResp to foreign address */
16598c8fccfSJohannes Berg 		if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
16698c8fccfSJohannes Berg 			return RX_DROP_MONITOR;
16798c8fccfSJohannes Berg 
16898c8fccfSJohannes Berg 		presp = true;
16998c8fccfSJohannes Berg 		elements = mgmt->u.probe_resp.variable;
17098c8fccfSJohannes Berg 		baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
17198c8fccfSJohannes Berg 	} else {
17298c8fccfSJohannes Berg 		beacon = ieee80211_is_beacon(fc);
17398c8fccfSJohannes Berg 		baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
17498c8fccfSJohannes Berg 		elements = mgmt->u.beacon.variable;
17598c8fccfSJohannes Berg 	}
17698c8fccfSJohannes Berg 
17798c8fccfSJohannes Berg 	if (!presp && !beacon)
17898c8fccfSJohannes Berg 		return RX_CONTINUE;
17998c8fccfSJohannes Berg 
18098c8fccfSJohannes Berg 	if (baselen > skb->len)
18198c8fccfSJohannes Berg 		return RX_DROP_MONITOR;
18298c8fccfSJohannes Berg 
18398c8fccfSJohannes Berg 	ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
18498c8fccfSJohannes Berg 
18598c8fccfSJohannes Berg 	if (elems.ds_params && elems.ds_params_len == 1)
18698c8fccfSJohannes Berg 		freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
18798c8fccfSJohannes Berg 	else
18898c8fccfSJohannes Berg 		freq = rx_status->freq;
18998c8fccfSJohannes Berg 
19098c8fccfSJohannes Berg 	channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
19198c8fccfSJohannes Berg 
19298c8fccfSJohannes Berg 	if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
19398c8fccfSJohannes Berg 		return RX_DROP_MONITOR;
19498c8fccfSJohannes Berg 
19598c8fccfSJohannes Berg 	bss = ieee80211_bss_info_update(sdata->local, rx_status,
19698c8fccfSJohannes Berg 					mgmt, skb->len, &elems,
1972a519311SJohannes Berg 					channel, beacon);
198d048e503SJouni Malinen 	if (bss)
19998c8fccfSJohannes Berg 		ieee80211_rx_bss_put(sdata->local, bss);
20098c8fccfSJohannes Berg 
20198c8fccfSJohannes Berg 	dev_kfree_skb(skb);
20298c8fccfSJohannes Berg 	return RX_QUEUED;
20398c8fccfSJohannes Berg }
20498c8fccfSJohannes Berg 
2052a519311SJohannes Berg void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
2060a51b27eSJohannes Berg {
2070a51b27eSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
2080a51b27eSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
2090a51b27eSJohannes Berg 
210c2b13452SJohannes Berg 	if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
2115bc75728SJohannes Berg 		return;
2125bc75728SJohannes Berg 
2132a519311SJohannes Berg 	if (WARN_ON(!local->scan_req))
2142a519311SJohannes Berg 		return;
2155bc75728SJohannes Berg 
2162a519311SJohannes Berg 	if (local->scan_req != &local->int_scan_req)
2172a519311SJohannes Berg 		cfg80211_scan_done(local->scan_req, aborted);
2182a519311SJohannes Berg 	local->scan_req = NULL;
2192a519311SJohannes Berg 
2202a519311SJohannes Berg 	local->last_scan_completed = jiffies;
2210a51b27eSJohannes Berg 
222c2b13452SJohannes Berg 	if (local->hw_scanning) {
223c2b13452SJohannes Berg 		local->hw_scanning = false;
224e8975581SJohannes Berg 		/*
225e8975581SJohannes Berg 		 * Somebody might have requested channel change during scan
226e8975581SJohannes Berg 		 * that we won't have acted upon, try now. ieee80211_hw_config
227e8975581SJohannes Berg 		 * will set the flag based on actual changes.
228e8975581SJohannes Berg 		 */
229e8975581SJohannes Berg 		ieee80211_hw_config(local, 0);
2300a51b27eSJohannes Berg 		goto done;
2310a51b27eSJohannes Berg 	}
2320a51b27eSJohannes Berg 
233c2b13452SJohannes Berg 	local->sw_scanning = false;
234e8975581SJohannes Berg 	ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
2350a51b27eSJohannes Berg 
2360a51b27eSJohannes Berg 	netif_tx_lock_bh(local->mdev);
2370a51b27eSJohannes Berg 	netif_addr_lock(local->mdev);
2380a51b27eSJohannes Berg 	local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
2390a51b27eSJohannes Berg 	local->ops->configure_filter(local_to_hw(local),
2400a51b27eSJohannes Berg 				     FIF_BCN_PRBRESP_PROMISC,
2410a51b27eSJohannes Berg 				     &local->filter_flags,
2420a51b27eSJohannes Berg 				     local->mdev->mc_count,
2430a51b27eSJohannes Berg 				     local->mdev->mc_list);
2440a51b27eSJohannes Berg 
2450a51b27eSJohannes Berg 	netif_addr_unlock(local->mdev);
2460a51b27eSJohannes Berg 	netif_tx_unlock_bh(local->mdev);
2470a51b27eSJohannes Berg 
248*80e775bfSMichael Buesch 	if (local->ops->sw_scan_complete)
249*80e775bfSMichael Buesch 		local->ops->sw_scan_complete(local_to_hw(local));
250*80e775bfSMichael Buesch 
251078e1e60SJohannes Berg 	mutex_lock(&local->iflist_mtx);
252078e1e60SJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
253fb9ddbf0SJohannes Berg 		if (!netif_running(sdata->dev))
254fb9ddbf0SJohannes Berg 			continue;
255fb9ddbf0SJohannes Berg 
2560a51b27eSJohannes Berg 		/* Tell AP we're back */
25705c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
25846900298SJohannes Berg 			if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED) {
2590a51b27eSJohannes Berg 				ieee80211_send_nullfunc(local, sdata, 0);
2600a51b27eSJohannes Berg 				netif_tx_wake_all_queues(sdata->dev);
2610a51b27eSJohannes Berg 			}
2620a51b27eSJohannes Berg 		} else
2630a51b27eSJohannes Berg 			netif_tx_wake_all_queues(sdata->dev);
264078e1e60SJohannes Berg 
26514b80724SJohannes Berg 		/* re-enable beaconing */
26614b80724SJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP ||
26714b80724SJohannes Berg 		    sdata->vif.type == NL80211_IFTYPE_ADHOC ||
26814b80724SJohannes Berg 		    sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
26914b80724SJohannes Berg 			ieee80211_if_config(sdata,
27014b80724SJohannes Berg 					    IEEE80211_IFCC_BEACON_ENABLED);
2710a51b27eSJohannes Berg 	}
272078e1e60SJohannes Berg 	mutex_unlock(&local->iflist_mtx);
2730a51b27eSJohannes Berg 
2740a51b27eSJohannes Berg  done:
2750a51b27eSJohannes Berg 	ieee80211_mlme_notify_scan_completed(local);
27646900298SJohannes Berg 	ieee80211_ibss_notify_scan_completed(local);
277472dbc45SJohannes Berg 	ieee80211_mesh_notify_scan_completed(local);
2780a51b27eSJohannes Berg }
2790a51b27eSJohannes Berg EXPORT_SYMBOL(ieee80211_scan_completed);
2800a51b27eSJohannes Berg 
281c2b13452SJohannes Berg void ieee80211_scan_work(struct work_struct *work)
2820a51b27eSJohannes Berg {
2830a51b27eSJohannes Berg 	struct ieee80211_local *local =
2840a51b27eSJohannes Berg 		container_of(work, struct ieee80211_local, scan_work.work);
2850a51b27eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2860a51b27eSJohannes Berg 	struct ieee80211_channel *chan;
2872a519311SJohannes Berg 	int skip, i;
2880a51b27eSJohannes Berg 	unsigned long next_delay = 0;
2890a51b27eSJohannes Berg 
2905bc75728SJohannes Berg 	/*
2915bc75728SJohannes Berg 	 * Avoid re-scheduling when the sdata is going away.
2925bc75728SJohannes Berg 	 */
2935bc75728SJohannes Berg 	if (!netif_running(sdata->dev))
2940a51b27eSJohannes Berg 		return;
2950a51b27eSJohannes Berg 
2960a51b27eSJohannes Berg 	switch (local->scan_state) {
2970a51b27eSJohannes Berg 	case SCAN_SET_CHANNEL:
2980a51b27eSJohannes Berg 		/* if no more bands/channels left, complete scan */
2992a519311SJohannes Berg 		if (local->scan_channel_idx >= local->scan_req->n_channels) {
3002a519311SJohannes Berg 			ieee80211_scan_completed(local_to_hw(local), false);
3010a51b27eSJohannes Berg 			return;
3020a51b27eSJohannes Berg 		}
3030a51b27eSJohannes Berg 		skip = 0;
3042a519311SJohannes Berg 		chan = local->scan_req->channels[local->scan_channel_idx];
3050a51b27eSJohannes Berg 
3060a51b27eSJohannes Berg 		if (chan->flags & IEEE80211_CHAN_DISABLED ||
30705c914feSJohannes Berg 		    (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
3080a51b27eSJohannes Berg 		     chan->flags & IEEE80211_CHAN_NO_IBSS))
3090a51b27eSJohannes Berg 			skip = 1;
3100a51b27eSJohannes Berg 
3110a51b27eSJohannes Berg 		if (!skip) {
3120a51b27eSJohannes Berg 			local->scan_channel = chan;
313e8975581SJohannes Berg 			if (ieee80211_hw_config(local,
314e8975581SJohannes Berg 						IEEE80211_CONF_CHANGE_CHANNEL))
3150a51b27eSJohannes Berg 				skip = 1;
3160a51b27eSJohannes Berg 		}
3170a51b27eSJohannes Berg 
3180a51b27eSJohannes Berg 		/* advance state machine to next channel/band */
3190a51b27eSJohannes Berg 		local->scan_channel_idx++;
3200a51b27eSJohannes Berg 
3210a51b27eSJohannes Berg 		if (skip)
3220a51b27eSJohannes Berg 			break;
3230a51b27eSJohannes Berg 
3240a51b27eSJohannes Berg 		next_delay = IEEE80211_PROBE_DELAY +
3250a51b27eSJohannes Berg 			     usecs_to_jiffies(local->hw.channel_change_time);
3260a51b27eSJohannes Berg 		local->scan_state = SCAN_SEND_PROBE;
3270a51b27eSJohannes Berg 		break;
3280a51b27eSJohannes Berg 	case SCAN_SEND_PROBE:
3290a51b27eSJohannes Berg 		next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3300a51b27eSJohannes Berg 		local->scan_state = SCAN_SET_CHANNEL;
3310a51b27eSJohannes Berg 
3322a519311SJohannes Berg 		if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
3332a519311SJohannes Berg 		    !local->scan_req->n_ssids)
3340a51b27eSJohannes Berg 			break;
3352a519311SJohannes Berg 		for (i = 0; i < local->scan_req->n_ssids; i++)
3362a519311SJohannes Berg 			ieee80211_send_probe_req(
3372a519311SJohannes Berg 				sdata, NULL,
3382a519311SJohannes Berg 				local->scan_req->ssids[i].ssid,
33970692ad2SJouni Malinen 				local->scan_req->ssids[i].ssid_len,
34070692ad2SJouni Malinen 				local->scan_req->ie, local->scan_req->ie_len);
3410a51b27eSJohannes Berg 		next_delay = IEEE80211_CHANNEL_TIME;
3420a51b27eSJohannes Berg 		break;
3430a51b27eSJohannes Berg 	}
3440a51b27eSJohannes Berg 
3450a51b27eSJohannes Berg 	queue_delayed_work(local->hw.workqueue, &local->scan_work,
3460a51b27eSJohannes Berg 			   next_delay);
3470a51b27eSJohannes Berg }
3480a51b27eSJohannes Berg 
3490a51b27eSJohannes Berg 
350c2b13452SJohannes Berg int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
3512a519311SJohannes Berg 			 struct cfg80211_scan_request *req)
3520a51b27eSJohannes Berg {
3530a51b27eSJohannes Berg 	struct ieee80211_local *local = scan_sdata->local;
3540a51b27eSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
3550a51b27eSJohannes Berg 
3562a519311SJohannes Berg 	if (!req)
3570a51b27eSJohannes Berg 		return -EINVAL;
3580a51b27eSJohannes Berg 
3592a519311SJohannes Berg 	if (local->scan_req && local->scan_req != req)
3602a519311SJohannes Berg 		return -EBUSY;
3612a519311SJohannes Berg 
3622a519311SJohannes Berg 	local->scan_req = req;
3632a519311SJohannes Berg 
3640a51b27eSJohannes Berg 	/* MLME-SCAN.request (page 118)  page 144 (11.1.3.1)
3650a51b27eSJohannes Berg 	 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3660a51b27eSJohannes Berg 	 * BSSID: MACAddress
3670a51b27eSJohannes Berg 	 * SSID
3680a51b27eSJohannes Berg 	 * ScanType: ACTIVE, PASSIVE
3690a51b27eSJohannes Berg 	 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3700a51b27eSJohannes Berg 	 *    a Probe frame during active scanning
3710a51b27eSJohannes Berg 	 * ChannelList
3720a51b27eSJohannes Berg 	 * MinChannelTime (>= ProbeDelay), in TU
3730a51b27eSJohannes Berg 	 * MaxChannelTime: (>= MinChannelTime), in TU
3740a51b27eSJohannes Berg 	 */
3750a51b27eSJohannes Berg 
3760a51b27eSJohannes Berg 	 /* MLME-SCAN.confirm
3770a51b27eSJohannes Berg 	  * BSSDescriptionSet
3780a51b27eSJohannes Berg 	  * ResultCode: SUCCESS, INVALID_PARAMETERS
3790a51b27eSJohannes Berg 	 */
3800a51b27eSJohannes Berg 
381c2b13452SJohannes Berg 	if (local->sw_scanning || local->hw_scanning) {
3820a51b27eSJohannes Berg 		if (local->scan_sdata == scan_sdata)
3830a51b27eSJohannes Berg 			return 0;
3840a51b27eSJohannes Berg 		return -EBUSY;
3850a51b27eSJohannes Berg 	}
3860a51b27eSJohannes Berg 
3870a51b27eSJohannes Berg 	if (local->ops->hw_scan) {
3885bc75728SJohannes Berg 		int rc;
3895bc75728SJohannes Berg 
390c2b13452SJohannes Berg 		local->hw_scanning = true;
3912a519311SJohannes Berg 		rc = local->ops->hw_scan(local_to_hw(local), req);
3925bc75728SJohannes Berg 		if (rc) {
393c2b13452SJohannes Berg 			local->hw_scanning = false;
3940a51b27eSJohannes Berg 			return rc;
3950a51b27eSJohannes Berg 		}
3965bc75728SJohannes Berg 		local->scan_sdata = scan_sdata;
3975bc75728SJohannes Berg 		return 0;
3985bc75728SJohannes Berg 	}
3990a51b27eSJohannes Berg 
400c2b13452SJohannes Berg 	local->sw_scanning = true;
401*80e775bfSMichael Buesch 	if (local->ops->sw_scan_start)
402*80e775bfSMichael Buesch 		local->ops->sw_scan_start(local_to_hw(local));
4030a51b27eSJohannes Berg 
404078e1e60SJohannes Berg 	mutex_lock(&local->iflist_mtx);
405078e1e60SJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
406fb9ddbf0SJohannes Berg 		if (!netif_running(sdata->dev))
407fb9ddbf0SJohannes Berg 			continue;
408fb9ddbf0SJohannes Berg 
40914b80724SJohannes Berg 		/* disable beaconing */
41014b80724SJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP ||
41114b80724SJohannes Berg 		    sdata->vif.type == NL80211_IFTYPE_ADHOC ||
41214b80724SJohannes Berg 		    sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
41314b80724SJohannes Berg 			ieee80211_if_config(sdata,
41414b80724SJohannes Berg 					    IEEE80211_IFCC_BEACON_ENABLED);
415078e1e60SJohannes Berg 
41605c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
41746900298SJohannes Berg 			if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED) {
4180a51b27eSJohannes Berg 				netif_tx_stop_all_queues(sdata->dev);
4190a51b27eSJohannes Berg 				ieee80211_send_nullfunc(local, sdata, 1);
4200a51b27eSJohannes Berg 			}
4210a51b27eSJohannes Berg 		} else
4220a51b27eSJohannes Berg 			netif_tx_stop_all_queues(sdata->dev);
4230a51b27eSJohannes Berg 	}
424078e1e60SJohannes Berg 	mutex_unlock(&local->iflist_mtx);
4250a51b27eSJohannes Berg 
4260a51b27eSJohannes Berg 	local->scan_state = SCAN_SET_CHANNEL;
4270a51b27eSJohannes Berg 	local->scan_channel_idx = 0;
4280a51b27eSJohannes Berg 	local->scan_sdata = scan_sdata;
4292a519311SJohannes Berg 	local->scan_req = req;
4300a51b27eSJohannes Berg 
4310a51b27eSJohannes Berg 	netif_addr_lock_bh(local->mdev);
4320a51b27eSJohannes Berg 	local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
4330a51b27eSJohannes Berg 	local->ops->configure_filter(local_to_hw(local),
4340a51b27eSJohannes Berg 				     FIF_BCN_PRBRESP_PROMISC,
4350a51b27eSJohannes Berg 				     &local->filter_flags,
4360a51b27eSJohannes Berg 				     local->mdev->mc_count,
4370a51b27eSJohannes Berg 				     local->mdev->mc_list);
4380a51b27eSJohannes Berg 	netif_addr_unlock_bh(local->mdev);
4390a51b27eSJohannes Berg 
4400a51b27eSJohannes Berg 	/* TODO: start scan as soon as all nullfunc frames are ACKed */
4410a51b27eSJohannes Berg 	queue_delayed_work(local->hw.workqueue, &local->scan_work,
4420a51b27eSJohannes Berg 			   IEEE80211_CHANNEL_TIME);
4430a51b27eSJohannes Berg 
4440a51b27eSJohannes Berg 	return 0;
4450a51b27eSJohannes Berg }
4460a51b27eSJohannes Berg 
4470a51b27eSJohannes Berg 
448c2b13452SJohannes Berg int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
4492a519311SJohannes Berg 			   struct cfg80211_scan_request *req)
4500a51b27eSJohannes Berg {
4510a51b27eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
45246900298SJohannes Berg 	struct ieee80211_if_managed *ifmgd;
4530a51b27eSJohannes Berg 
4542a519311SJohannes Berg 	if (!req)
4552a519311SJohannes Berg 		return -EINVAL;
4562a519311SJohannes Berg 
4572a519311SJohannes Berg 	if (local->scan_req && local->scan_req != req)
4582a519311SJohannes Berg 		return -EBUSY;
4592a519311SJohannes Berg 
4602a519311SJohannes Berg 	local->scan_req = req;
4612a519311SJohannes Berg 
46205c914feSJohannes Berg 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
4632a519311SJohannes Berg 		return ieee80211_start_scan(sdata, req);
4640a51b27eSJohannes Berg 
4659116dd01SJohannes Berg 	/*
4669116dd01SJohannes Berg 	 * STA has a state machine that might need to defer scanning
4679116dd01SJohannes Berg 	 * while it's trying to associate/authenticate, therefore we
4689116dd01SJohannes Berg 	 * queue it up to the state machine in that case.
4699116dd01SJohannes Berg 	 */
4709116dd01SJohannes Berg 
471c2b13452SJohannes Berg 	if (local->sw_scanning || local->hw_scanning) {
4720a51b27eSJohannes Berg 		if (local->scan_sdata == sdata)
4730a51b27eSJohannes Berg 			return 0;
4740a51b27eSJohannes Berg 		return -EBUSY;
4750a51b27eSJohannes Berg 	}
4760a51b27eSJohannes Berg 
47746900298SJohannes Berg 	ifmgd = &sdata->u.mgd;
47846900298SJohannes Berg 	set_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request);
47946900298SJohannes Berg 	queue_work(local->hw.workqueue, &ifmgd->work);
4809116dd01SJohannes Berg 
4810a51b27eSJohannes Berg 	return 0;
4820a51b27eSJohannes Berg }
483