xref: /linux/net/mac80211/scan.c (revision 70692ad2923a379e0a10f9ec2ad93fbbe084cc46)
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 	enum cfg80211_signal_type sigtype = CFG80211_SIGNAL_TYPE_NONE;
672a519311SJohannes Berg 	s32 signal = 0;
682a519311SJohannes Berg 
692a519311SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
702a519311SJohannes Berg 		sigtype = CFG80211_SIGNAL_TYPE_MBM;
712a519311SJohannes Berg 		signal = rx_status->signal * 100;
722a519311SJohannes Berg 	} else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC) {
732a519311SJohannes Berg 		sigtype = CFG80211_SIGNAL_TYPE_UNSPEC;
742a519311SJohannes Berg 		signal = (rx_status->signal * 100) / local->hw.max_signal;
752a519311SJohannes Berg 	}
762a519311SJohannes Berg 
7700d3f14cSJohannes Berg 	bss = (void *)cfg80211_inform_bss_frame(local->hw.wiphy, channel,
782a519311SJohannes Berg 						mgmt, len, signal, sigtype,
7900d3f14cSJohannes Berg 						GFP_ATOMIC);
805484e237SJohannes Berg 
815484e237SJohannes Berg 	if (!bss)
825484e237SJohannes Berg 		return NULL;
8300d3f14cSJohannes Berg 
8400d3f14cSJohannes Berg 	bss->cbss.free_priv = ieee80211_rx_bss_free;
855484e237SJohannes Berg 
865484e237SJohannes Berg 	/* save the ERP value so that it is available at association time */
875484e237SJohannes Berg 	if (elems->erp_info && elems->erp_info_len >= 1) {
885484e237SJohannes Berg 		bss->erp_value = elems->erp_info[0];
895484e237SJohannes Berg 		bss->has_erp_value = 1;
905484e237SJohannes Berg 	}
915484e237SJohannes Berg 
925484e237SJohannes Berg 	if (elems->tim) {
935484e237SJohannes Berg 		struct ieee80211_tim_ie *tim_ie =
945484e237SJohannes Berg 			(struct ieee80211_tim_ie *)elems->tim;
955484e237SJohannes Berg 		bss->dtim_period = tim_ie->dtim_period;
965484e237SJohannes Berg 	}
975484e237SJohannes Berg 
985484e237SJohannes Berg 	/* set default value for buggy APs */
995484e237SJohannes Berg 	if (!elems->tim || bss->dtim_period == 0)
1005484e237SJohannes Berg 		bss->dtim_period = 1;
1015484e237SJohannes Berg 
1025484e237SJohannes Berg 	bss->supp_rates_len = 0;
1035484e237SJohannes Berg 	if (elems->supp_rates) {
1045484e237SJohannes Berg 		clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1055484e237SJohannes Berg 		if (clen > elems->supp_rates_len)
1065484e237SJohannes Berg 			clen = elems->supp_rates_len;
1075484e237SJohannes Berg 		memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
1085484e237SJohannes Berg 		       clen);
1095484e237SJohannes Berg 		bss->supp_rates_len += clen;
1105484e237SJohannes Berg 	}
1115484e237SJohannes Berg 	if (elems->ext_supp_rates) {
1125484e237SJohannes Berg 		clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1135484e237SJohannes Berg 		if (clen > elems->ext_supp_rates_len)
1145484e237SJohannes Berg 			clen = elems->ext_supp_rates_len;
1155484e237SJohannes Berg 		memcpy(&bss->supp_rates[bss->supp_rates_len],
1165484e237SJohannes Berg 		       elems->ext_supp_rates, clen);
1175484e237SJohannes Berg 		bss->supp_rates_len += clen;
1185484e237SJohannes Berg 	}
1195484e237SJohannes Berg 
1205484e237SJohannes Berg 	bss->wmm_used = elems->wmm_param || elems->wmm_info;
1215484e237SJohannes Berg 
1225484e237SJohannes Berg 	if (!beacon)
1235484e237SJohannes Berg 		bss->last_probe_resp = jiffies;
1245484e237SJohannes Berg 
1255484e237SJohannes Berg 	return bss;
1265484e237SJohannes Berg }
1270a51b27eSJohannes Berg 
1287a947080SVasanthakumar Thiagarajan void ieee80211_rx_bss_remove(struct ieee80211_sub_if_data *sdata, u8 *bssid,
1297a947080SVasanthakumar Thiagarajan 			     int freq, u8 *ssid, u8 ssid_len)
1307a947080SVasanthakumar Thiagarajan {
1317a947080SVasanthakumar Thiagarajan 	struct ieee80211_bss *bss;
1327a947080SVasanthakumar Thiagarajan 	struct ieee80211_local *local = sdata->local;
1337a947080SVasanthakumar Thiagarajan 
1347a947080SVasanthakumar Thiagarajan 	bss = ieee80211_rx_bss_get(local, bssid, freq, ssid, ssid_len);
1357a947080SVasanthakumar Thiagarajan 	if (bss) {
13600d3f14cSJohannes Berg 		cfg80211_unlink_bss(local->hw.wiphy, (void *)bss);
1377a947080SVasanthakumar Thiagarajan 		ieee80211_rx_bss_put(local, bss);
1387a947080SVasanthakumar Thiagarajan 	}
1397a947080SVasanthakumar Thiagarajan }
1407a947080SVasanthakumar Thiagarajan 
14198c8fccfSJohannes Berg ieee80211_rx_result
142c2b13452SJohannes Berg ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
14398c8fccfSJohannes Berg 		  struct ieee80211_rx_status *rx_status)
14498c8fccfSJohannes Berg {
14598c8fccfSJohannes Berg 	struct ieee80211_mgmt *mgmt;
146c2b13452SJohannes Berg 	struct ieee80211_bss *bss;
14798c8fccfSJohannes Berg 	u8 *elements;
14898c8fccfSJohannes Berg 	struct ieee80211_channel *channel;
14998c8fccfSJohannes Berg 	size_t baselen;
15098c8fccfSJohannes Berg 	int freq;
15198c8fccfSJohannes Berg 	__le16 fc;
15298c8fccfSJohannes Berg 	bool presp, beacon = false;
15398c8fccfSJohannes Berg 	struct ieee802_11_elems elems;
15498c8fccfSJohannes Berg 
15598c8fccfSJohannes Berg 	if (skb->len < 2)
15698c8fccfSJohannes Berg 		return RX_DROP_UNUSABLE;
15798c8fccfSJohannes Berg 
15898c8fccfSJohannes Berg 	mgmt = (struct ieee80211_mgmt *) skb->data;
15998c8fccfSJohannes Berg 	fc = mgmt->frame_control;
16098c8fccfSJohannes Berg 
16198c8fccfSJohannes Berg 	if (ieee80211_is_ctl(fc))
16298c8fccfSJohannes Berg 		return RX_CONTINUE;
16398c8fccfSJohannes Berg 
16498c8fccfSJohannes Berg 	if (skb->len < 24)
16598c8fccfSJohannes Berg 		return RX_DROP_MONITOR;
16698c8fccfSJohannes Berg 
16798c8fccfSJohannes Berg 	presp = ieee80211_is_probe_resp(fc);
16898c8fccfSJohannes Berg 	if (presp) {
16998c8fccfSJohannes Berg 		/* ignore ProbeResp to foreign address */
17098c8fccfSJohannes Berg 		if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
17198c8fccfSJohannes Berg 			return RX_DROP_MONITOR;
17298c8fccfSJohannes Berg 
17398c8fccfSJohannes Berg 		presp = true;
17498c8fccfSJohannes Berg 		elements = mgmt->u.probe_resp.variable;
17598c8fccfSJohannes Berg 		baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
17698c8fccfSJohannes Berg 	} else {
17798c8fccfSJohannes Berg 		beacon = ieee80211_is_beacon(fc);
17898c8fccfSJohannes Berg 		baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
17998c8fccfSJohannes Berg 		elements = mgmt->u.beacon.variable;
18098c8fccfSJohannes Berg 	}
18198c8fccfSJohannes Berg 
18298c8fccfSJohannes Berg 	if (!presp && !beacon)
18398c8fccfSJohannes Berg 		return RX_CONTINUE;
18498c8fccfSJohannes Berg 
18598c8fccfSJohannes Berg 	if (baselen > skb->len)
18698c8fccfSJohannes Berg 		return RX_DROP_MONITOR;
18798c8fccfSJohannes Berg 
18898c8fccfSJohannes Berg 	ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
18998c8fccfSJohannes Berg 
19098c8fccfSJohannes Berg 	if (elems.ds_params && elems.ds_params_len == 1)
19198c8fccfSJohannes Berg 		freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
19298c8fccfSJohannes Berg 	else
19398c8fccfSJohannes Berg 		freq = rx_status->freq;
19498c8fccfSJohannes Berg 
19598c8fccfSJohannes Berg 	channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
19698c8fccfSJohannes Berg 
19798c8fccfSJohannes Berg 	if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
19898c8fccfSJohannes Berg 		return RX_DROP_MONITOR;
19998c8fccfSJohannes Berg 
20098c8fccfSJohannes Berg 	bss = ieee80211_bss_info_update(sdata->local, rx_status,
20198c8fccfSJohannes Berg 					mgmt, skb->len, &elems,
2022a519311SJohannes Berg 					channel, beacon);
203d048e503SJouni Malinen 	if (bss)
20498c8fccfSJohannes Berg 		ieee80211_rx_bss_put(sdata->local, bss);
20598c8fccfSJohannes Berg 
20698c8fccfSJohannes Berg 	dev_kfree_skb(skb);
20798c8fccfSJohannes Berg 	return RX_QUEUED;
20898c8fccfSJohannes Berg }
20998c8fccfSJohannes Berg 
2102a519311SJohannes Berg void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
2110a51b27eSJohannes Berg {
2120a51b27eSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
2130a51b27eSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
2140a51b27eSJohannes Berg 
215c2b13452SJohannes Berg 	if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
2165bc75728SJohannes Berg 		return;
2175bc75728SJohannes Berg 
2182a519311SJohannes Berg 	if (WARN_ON(!local->scan_req))
2192a519311SJohannes Berg 		return;
2205bc75728SJohannes Berg 
2212a519311SJohannes Berg 	if (local->scan_req != &local->int_scan_req)
2222a519311SJohannes Berg 		cfg80211_scan_done(local->scan_req, aborted);
2232a519311SJohannes Berg 	local->scan_req = NULL;
2242a519311SJohannes Berg 
2252a519311SJohannes Berg 	local->last_scan_completed = jiffies;
2260a51b27eSJohannes Berg 
227c2b13452SJohannes Berg 	if (local->hw_scanning) {
228c2b13452SJohannes Berg 		local->hw_scanning = false;
229e8975581SJohannes Berg 		/*
230e8975581SJohannes Berg 		 * Somebody might have requested channel change during scan
231e8975581SJohannes Berg 		 * that we won't have acted upon, try now. ieee80211_hw_config
232e8975581SJohannes Berg 		 * will set the flag based on actual changes.
233e8975581SJohannes Berg 		 */
234e8975581SJohannes Berg 		ieee80211_hw_config(local, 0);
2350a51b27eSJohannes Berg 		goto done;
2360a51b27eSJohannes Berg 	}
2370a51b27eSJohannes Berg 
238c2b13452SJohannes Berg 	local->sw_scanning = false;
239e8975581SJohannes Berg 	ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
2400a51b27eSJohannes Berg 
2410a51b27eSJohannes Berg 	netif_tx_lock_bh(local->mdev);
2420a51b27eSJohannes Berg 	netif_addr_lock(local->mdev);
2430a51b27eSJohannes Berg 	local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
2440a51b27eSJohannes Berg 	local->ops->configure_filter(local_to_hw(local),
2450a51b27eSJohannes Berg 				     FIF_BCN_PRBRESP_PROMISC,
2460a51b27eSJohannes Berg 				     &local->filter_flags,
2470a51b27eSJohannes Berg 				     local->mdev->mc_count,
2480a51b27eSJohannes Berg 				     local->mdev->mc_list);
2490a51b27eSJohannes Berg 
2500a51b27eSJohannes Berg 	netif_addr_unlock(local->mdev);
2510a51b27eSJohannes Berg 	netif_tx_unlock_bh(local->mdev);
2520a51b27eSJohannes Berg 
253078e1e60SJohannes Berg 	mutex_lock(&local->iflist_mtx);
254078e1e60SJohannes Berg 	list_for_each_entry(sdata, &local->interfaces, list) {
255fb9ddbf0SJohannes Berg 		if (!netif_running(sdata->dev))
256fb9ddbf0SJohannes Berg 			continue;
257fb9ddbf0SJohannes Berg 
2580a51b27eSJohannes Berg 		/* Tell AP we're back */
25905c914feSJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
26046900298SJohannes Berg 			if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED) {
2610a51b27eSJohannes Berg 				ieee80211_send_nullfunc(local, sdata, 0);
2620a51b27eSJohannes Berg 				netif_tx_wake_all_queues(sdata->dev);
2630a51b27eSJohannes Berg 			}
2640a51b27eSJohannes Berg 		} else
2650a51b27eSJohannes Berg 			netif_tx_wake_all_queues(sdata->dev);
266078e1e60SJohannes Berg 
26714b80724SJohannes Berg 		/* re-enable beaconing */
26814b80724SJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP ||
26914b80724SJohannes Berg 		    sdata->vif.type == NL80211_IFTYPE_ADHOC ||
27014b80724SJohannes Berg 		    sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
27114b80724SJohannes Berg 			ieee80211_if_config(sdata,
27214b80724SJohannes Berg 					    IEEE80211_IFCC_BEACON_ENABLED);
2730a51b27eSJohannes Berg 	}
274078e1e60SJohannes Berg 	mutex_unlock(&local->iflist_mtx);
2750a51b27eSJohannes Berg 
2760a51b27eSJohannes Berg  done:
2770a51b27eSJohannes Berg 	ieee80211_mlme_notify_scan_completed(local);
27846900298SJohannes Berg 	ieee80211_ibss_notify_scan_completed(local);
279472dbc45SJohannes Berg 	ieee80211_mesh_notify_scan_completed(local);
2800a51b27eSJohannes Berg }
2810a51b27eSJohannes Berg EXPORT_SYMBOL(ieee80211_scan_completed);
2820a51b27eSJohannes Berg 
283c2b13452SJohannes Berg void ieee80211_scan_work(struct work_struct *work)
2840a51b27eSJohannes Berg {
2850a51b27eSJohannes Berg 	struct ieee80211_local *local =
2860a51b27eSJohannes Berg 		container_of(work, struct ieee80211_local, scan_work.work);
2870a51b27eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2880a51b27eSJohannes Berg 	struct ieee80211_channel *chan;
2892a519311SJohannes Berg 	int skip, i;
2900a51b27eSJohannes Berg 	unsigned long next_delay = 0;
2910a51b27eSJohannes Berg 
2925bc75728SJohannes Berg 	/*
2935bc75728SJohannes Berg 	 * Avoid re-scheduling when the sdata is going away.
2945bc75728SJohannes Berg 	 */
2955bc75728SJohannes Berg 	if (!netif_running(sdata->dev))
2960a51b27eSJohannes Berg 		return;
2970a51b27eSJohannes Berg 
2980a51b27eSJohannes Berg 	switch (local->scan_state) {
2990a51b27eSJohannes Berg 	case SCAN_SET_CHANNEL:
3000a51b27eSJohannes Berg 		/* if no more bands/channels left, complete scan */
3012a519311SJohannes Berg 		if (local->scan_channel_idx >= local->scan_req->n_channels) {
3022a519311SJohannes Berg 			ieee80211_scan_completed(local_to_hw(local), false);
3030a51b27eSJohannes Berg 			return;
3040a51b27eSJohannes Berg 		}
3050a51b27eSJohannes Berg 		skip = 0;
3062a519311SJohannes Berg 		chan = local->scan_req->channels[local->scan_channel_idx];
3070a51b27eSJohannes Berg 
3080a51b27eSJohannes Berg 		if (chan->flags & IEEE80211_CHAN_DISABLED ||
30905c914feSJohannes Berg 		    (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
3100a51b27eSJohannes Berg 		     chan->flags & IEEE80211_CHAN_NO_IBSS))
3110a51b27eSJohannes Berg 			skip = 1;
3120a51b27eSJohannes Berg 
3130a51b27eSJohannes Berg 		if (!skip) {
3140a51b27eSJohannes Berg 			local->scan_channel = chan;
315e8975581SJohannes Berg 			if (ieee80211_hw_config(local,
316e8975581SJohannes Berg 						IEEE80211_CONF_CHANGE_CHANNEL))
3170a51b27eSJohannes Berg 				skip = 1;
3180a51b27eSJohannes Berg 		}
3190a51b27eSJohannes Berg 
3200a51b27eSJohannes Berg 		/* advance state machine to next channel/band */
3210a51b27eSJohannes Berg 		local->scan_channel_idx++;
3220a51b27eSJohannes Berg 
3230a51b27eSJohannes Berg 		if (skip)
3240a51b27eSJohannes Berg 			break;
3250a51b27eSJohannes Berg 
3260a51b27eSJohannes Berg 		next_delay = IEEE80211_PROBE_DELAY +
3270a51b27eSJohannes Berg 			     usecs_to_jiffies(local->hw.channel_change_time);
3280a51b27eSJohannes Berg 		local->scan_state = SCAN_SEND_PROBE;
3290a51b27eSJohannes Berg 		break;
3300a51b27eSJohannes Berg 	case SCAN_SEND_PROBE:
3310a51b27eSJohannes Berg 		next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3320a51b27eSJohannes Berg 		local->scan_state = SCAN_SET_CHANNEL;
3330a51b27eSJohannes Berg 
3342a519311SJohannes Berg 		if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
3352a519311SJohannes Berg 		    !local->scan_req->n_ssids)
3360a51b27eSJohannes Berg 			break;
3372a519311SJohannes Berg 		for (i = 0; i < local->scan_req->n_ssids; i++)
3382a519311SJohannes Berg 			ieee80211_send_probe_req(
3392a519311SJohannes Berg 				sdata, NULL,
3402a519311SJohannes Berg 				local->scan_req->ssids[i].ssid,
341*70692ad2SJouni Malinen 				local->scan_req->ssids[i].ssid_len,
342*70692ad2SJouni Malinen 				local->scan_req->ie, local->scan_req->ie_len);
3430a51b27eSJohannes Berg 		next_delay = IEEE80211_CHANNEL_TIME;
3440a51b27eSJohannes Berg 		break;
3450a51b27eSJohannes Berg 	}
3460a51b27eSJohannes Berg 
3470a51b27eSJohannes Berg 	queue_delayed_work(local->hw.workqueue, &local->scan_work,
3480a51b27eSJohannes Berg 			   next_delay);
3490a51b27eSJohannes Berg }
3500a51b27eSJohannes Berg 
3510a51b27eSJohannes Berg 
352c2b13452SJohannes Berg int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
3532a519311SJohannes Berg 			 struct cfg80211_scan_request *req)
3540a51b27eSJohannes Berg {
3550a51b27eSJohannes Berg 	struct ieee80211_local *local = scan_sdata->local;
3560a51b27eSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
3570a51b27eSJohannes Berg 
3582a519311SJohannes Berg 	if (!req)
3590a51b27eSJohannes Berg 		return -EINVAL;
3600a51b27eSJohannes Berg 
3612a519311SJohannes Berg 	if (local->scan_req && local->scan_req != req)
3622a519311SJohannes Berg 		return -EBUSY;
3632a519311SJohannes Berg 
3642a519311SJohannes Berg 	local->scan_req = req;
3652a519311SJohannes Berg 
3660a51b27eSJohannes Berg 	/* MLME-SCAN.request (page 118)  page 144 (11.1.3.1)
3670a51b27eSJohannes Berg 	 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3680a51b27eSJohannes Berg 	 * BSSID: MACAddress
3690a51b27eSJohannes Berg 	 * SSID
3700a51b27eSJohannes Berg 	 * ScanType: ACTIVE, PASSIVE
3710a51b27eSJohannes Berg 	 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3720a51b27eSJohannes Berg 	 *    a Probe frame during active scanning
3730a51b27eSJohannes Berg 	 * ChannelList
3740a51b27eSJohannes Berg 	 * MinChannelTime (>= ProbeDelay), in TU
3750a51b27eSJohannes Berg 	 * MaxChannelTime: (>= MinChannelTime), in TU
3760a51b27eSJohannes Berg 	 */
3770a51b27eSJohannes Berg 
3780a51b27eSJohannes Berg 	 /* MLME-SCAN.confirm
3790a51b27eSJohannes Berg 	  * BSSDescriptionSet
3800a51b27eSJohannes Berg 	  * ResultCode: SUCCESS, INVALID_PARAMETERS
3810a51b27eSJohannes Berg 	 */
3820a51b27eSJohannes Berg 
383c2b13452SJohannes Berg 	if (local->sw_scanning || local->hw_scanning) {
3840a51b27eSJohannes Berg 		if (local->scan_sdata == scan_sdata)
3850a51b27eSJohannes Berg 			return 0;
3860a51b27eSJohannes Berg 		return -EBUSY;
3870a51b27eSJohannes Berg 	}
3880a51b27eSJohannes Berg 
3890a51b27eSJohannes Berg 	if (local->ops->hw_scan) {
3905bc75728SJohannes Berg 		int rc;
3915bc75728SJohannes Berg 
392c2b13452SJohannes Berg 		local->hw_scanning = true;
3932a519311SJohannes Berg 		rc = local->ops->hw_scan(local_to_hw(local), req);
3945bc75728SJohannes Berg 		if (rc) {
395c2b13452SJohannes Berg 			local->hw_scanning = false;
3960a51b27eSJohannes Berg 			return rc;
3970a51b27eSJohannes Berg 		}
3985bc75728SJohannes Berg 		local->scan_sdata = scan_sdata;
3995bc75728SJohannes Berg 		return 0;
4005bc75728SJohannes Berg 	}
4010a51b27eSJohannes Berg 
402c2b13452SJohannes Berg 	local->sw_scanning = true;
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