1482 lines · 40.1 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-only/* * Scanning implementation * * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi> * Copyright 2004, Instant802 Networks, Inc. * Copyright 2005, Devicescape Software, Inc. * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz> * Copyright 2007, Michael Wu <flamingice@sourmilk.net> * Copyright 2013-2015  Intel Mobile Communications GmbH * Copyright 2016-2017  Intel Deutschland GmbH * Copyright (C) 2018-2025 Intel Corporation */ #include <linux/if_arp.h>#include <linux/etherdevice.h>#include <linux/rtnetlink.h>#include <net/sch_generic.h>#include <linux/slab.h>#include <linux/export.h>#include <linux/random.h>#include <net/mac80211.h> #include "ieee80211_i.h"#include "driver-ops.h"#include "mesh.h" #define IEEE80211_PROBE_DELAY (HZ / 33)#define IEEE80211_CHANNEL_TIME (HZ / 33)#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9) void ieee80211_rx_bss_put(struct ieee80211_local *local,			  struct ieee80211_bss *bss){	if (!bss)		return;	cfg80211_put_bss(local->hw.wiphy,			 container_of((void *)bss, struct cfg80211_bss, priv));} static bool is_uapsd_supported(struct ieee802_11_elems *elems){	u8 qos_info; 	if (elems->wmm_info && elems->wmm_info_len == 7	    && elems->wmm_info[5] == 1)		qos_info = elems->wmm_info[6];	else if (elems->wmm_param && elems->wmm_param_len == 24		 && elems->wmm_param[5] == 1)		qos_info = elems->wmm_param[6];	else		/* no valid wmm information or parameter element found */		return false; 	return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;} struct inform_bss_update_data {	struct ieee80211_rx_status *rx_status;	bool beacon;}; void ieee80211_inform_bss(struct wiphy *wiphy,			  struct cfg80211_bss *cbss,			  const struct cfg80211_bss_ies *ies,			  void *data){	struct ieee80211_local *local = wiphy_priv(wiphy);	struct inform_bss_update_data *update_data = data;	struct ieee80211_bss *bss = (void *)cbss->priv;	struct ieee80211_rx_status *rx_status;	struct ieee802_11_elems *elems;	int clen, srlen; 	/* This happens while joining an IBSS */	if (!update_data)		return; 	elems = ieee802_11_parse_elems(ies->data, ies->len,				       update_data->beacon ?					IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON :					IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_PROBE_RESP,				       NULL);	if (!elems)		return; 	rx_status = update_data->rx_status; 	if (update_data->beacon)		bss->device_ts_beacon = rx_status->device_timestamp;	else		bss->device_ts_presp = rx_status->device_timestamp; 	if (elems->parse_error) {		if (update_data->beacon)			bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON;		else			bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP;	} else {		if (update_data->beacon)			bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON;		else			bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP;	} 	/* save the ERP value so that it is available at association time */	if (elems->erp_info && (!elems->parse_error ||				!(bss->valid_data & IEEE80211_BSS_VALID_ERP))) {		bss->erp_value = elems->erp_info[0];		bss->has_erp_value = true;		if (!elems->parse_error)			bss->valid_data |= IEEE80211_BSS_VALID_ERP;	} 	/* replace old supported rates if we get new values */	if (!elems->parse_error ||	    !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) {		srlen = 0;		if (elems->supp_rates) {			clen = IEEE80211_MAX_SUPP_RATES;			if (clen > elems->supp_rates_len)				clen = elems->supp_rates_len;			memcpy(bss->supp_rates, elems->supp_rates, clen);			srlen += clen;		}		if (elems->ext_supp_rates) {			clen = IEEE80211_MAX_SUPP_RATES - srlen;			if (clen > elems->ext_supp_rates_len)				clen = elems->ext_supp_rates_len;			memcpy(bss->supp_rates + srlen, elems->ext_supp_rates,			       clen);			srlen += clen;		}		if (srlen) {			bss->supp_rates_len = srlen;			if (!elems->parse_error)				bss->valid_data |= IEEE80211_BSS_VALID_RATES;		}	} 	if (!elems->parse_error ||	    !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) {		bss->wmm_used = elems->wmm_param || elems->wmm_info;		bss->uapsd_supported = is_uapsd_supported(elems);		if (!elems->parse_error)			bss->valid_data |= IEEE80211_BSS_VALID_WMM;	} 	if (update_data->beacon) {		struct ieee80211_supported_band *sband =			local->hw.wiphy->bands[rx_status->band];		if (!(rx_status->encoding == RX_ENC_HT) &&		    !(rx_status->encoding == RX_ENC_VHT))			bss->beacon_rate =				&sband->bitrates[rx_status->rate_idx];	} 	if (elems->vht_cap_elem)		bss->vht_cap_info =			le32_to_cpu(elems->vht_cap_elem->vht_cap_info);	else		bss->vht_cap_info = 0; 	kfree(elems);} struct ieee80211_bss *ieee80211_bss_info_update(struct ieee80211_local *local,			  struct ieee80211_rx_status *rx_status,			  struct ieee80211_mgmt *mgmt, size_t len,			  struct ieee80211_channel *channel){	bool beacon = ieee80211_is_beacon(mgmt->frame_control) ||		      ieee80211_is_s1g_beacon(mgmt->frame_control);	struct cfg80211_bss *cbss;	struct inform_bss_update_data update_data = {		.rx_status = rx_status,		.beacon = beacon,	};	struct cfg80211_inform_bss bss_meta = {		.boottime_ns = rx_status->boottime_ns,		.drv_data = (void *)&update_data,	};	bool signal_valid;	struct ieee80211_sub_if_data *scan_sdata; 	if (rx_status->flag & RX_FLAG_NO_SIGNAL_VAL)		bss_meta.signal = 0; /* invalid signal indication */	else if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))		bss_meta.signal = rx_status->signal * 100;	else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC))		bss_meta.signal = (rx_status->signal * 100) / local->hw.max_signal; 	bss_meta.chan = channel; 	rcu_read_lock();	scan_sdata = rcu_dereference(local->scan_sdata);	if (scan_sdata && scan_sdata->vif.type == NL80211_IFTYPE_STATION &&	    scan_sdata->vif.cfg.assoc &&	    ieee80211_have_rx_timestamp(rx_status)) {		struct ieee80211_bss_conf *link_conf = NULL; 		/* for an MLO connection, set the TSF data only in case we have		 * an indication on which of the links the frame was received		 */		if (ieee80211_vif_is_mld(&scan_sdata->vif)) {			if (rx_status->link_valid) {				s8 link_id = rx_status->link_id; 				link_conf =					rcu_dereference(scan_sdata->vif.link_conf[link_id]);			}		} else {			link_conf = &scan_sdata->vif.bss_conf;		} 		if (link_conf) {			bss_meta.parent_tsf =				ieee80211_calculate_rx_timestamp(&local->hw,								 rx_status,								 len + FCS_LEN,								 24); 			ether_addr_copy(bss_meta.parent_bssid,					link_conf->bssid);		}	}	rcu_read_unlock(); 	cbss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta,					      mgmt, len, GFP_ATOMIC);	if (!cbss)		return NULL; 	/* In case the signal is invalid update the status */	signal_valid = channel == cbss->channel;	if (!signal_valid)		rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL; 	return (void *)cbss->priv;} static bool ieee80211_scan_accept_presp(struct ieee80211_sub_if_data *sdata,					struct ieee80211_channel *channel,					u32 scan_flags, const u8 *da){	struct ieee80211_link_data *link_sdata;	u8 link_id; 	if (!sdata)		return false; 	/* accept broadcast on 6 GHz and for OCE */	if (is_broadcast_ether_addr(da) &&	    (channel->band == NL80211_BAND_6GHZ ||	     scan_flags & NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP))		return true; 	if (scan_flags & NL80211_SCAN_FLAG_RANDOM_ADDR)		return true; 	if (ether_addr_equal(da, sdata->vif.addr))		return true; 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {		link_sdata = rcu_dereference(sdata->link[link_id]);		if (!link_sdata)			continue; 		if (ether_addr_equal(da, link_sdata->conf->addr))			return true;	} 	return false;} void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb){	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);	struct ieee80211_mgmt *mgmt = (void *)skb->data;	struct ieee80211_bss *bss;	struct ieee80211_channel *channel;	struct ieee80211_ext *ext;	size_t min_hdr_len = offsetof(struct ieee80211_mgmt,				      u.probe_resp.variable); 	if (!ieee80211_is_probe_resp(mgmt->frame_control) &&	    !ieee80211_is_beacon(mgmt->frame_control) &&	    !ieee80211_is_s1g_beacon(mgmt->frame_control))		return; 	if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {		ext = (struct ieee80211_ext *)mgmt;		min_hdr_len =			offsetof(struct ieee80211_ext, u.s1g_beacon.variable) +			ieee80211_s1g_optional_len(ext->frame_control);	} 	if (skb->len < min_hdr_len)		return; 	if (test_and_clear_bit(SCAN_BEACON_WAIT, &local->scanning)) {		/*		 * we were passive scanning because of radar/no-IR, but		 * the beacon/proberesp rx gives us an opportunity to upgrade		 * to active scan		 */		set_bit(SCAN_BEACON_DONE, &local->scanning);		wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work, 0);	} 	channel = ieee80211_get_channel_khz(local->hw.wiphy,					    ieee80211_rx_status_to_khz(rx_status)); 	if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)		return; 	if (ieee80211_is_probe_resp(mgmt->frame_control)) {		struct ieee80211_sub_if_data *sdata1, *sdata2;		struct cfg80211_scan_request *scan_req;		struct cfg80211_sched_scan_request *sched_scan_req;		u32 scan_req_flags = 0, sched_scan_req_flags = 0; 		sdata1 = rcu_dereference(local->scan_sdata);		sdata2 = rcu_dereference(local->sched_scan_sdata); 		if (likely(!sdata1 && !sdata2))			return; 		scan_req = rcu_dereference(local->scan_req);		sched_scan_req = rcu_dereference(local->sched_scan_req); 		if (scan_req)			scan_req_flags = scan_req->flags; 		if (sched_scan_req)			sched_scan_req_flags = sched_scan_req->flags; 		/* ignore ProbeResp to foreign address or non-bcast (OCE)		 * unless scanning with randomised address		 */		if (!ieee80211_scan_accept_presp(sdata1, channel,						 scan_req_flags,						 mgmt->da) &&		    !ieee80211_scan_accept_presp(sdata2, channel,						 sched_scan_req_flags,						 mgmt->da))			return;	} else {		/*		 * Non-S1G beacons are expected only with broadcast address.		 * S1G beacons only carry the SA so no DA check is required		 * nor possible.		 */		if (!ieee80211_is_s1g_beacon(mgmt->frame_control) &&		    !is_broadcast_ether_addr(mgmt->da))			return;	} 	/* Do not update the BSS table in case of only monitor interfaces */	if (local->open_count == local->monitors)		return; 	bss = ieee80211_bss_info_update(local, rx_status,					mgmt, skb->len,					channel);	if (bss)		ieee80211_rx_bss_put(local, bss);} static void ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef){	memset(chandef, 0, sizeof(*chandef)); 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;} /* return false if no more work */static bool ieee80211_prep_hw_scan(struct ieee80211_sub_if_data *sdata){	struct ieee80211_local *local = sdata->local;	struct cfg80211_scan_request *req;	struct cfg80211_chan_def chandef;	u8 bands_used = 0;	int i, ielen;	u32 *n_chans;	u32 flags = 0; 	req = rcu_dereference_protected(local->scan_req,					lockdep_is_held(&local->hw.wiphy->mtx)); 	if (test_bit(SCAN_HW_CANCELLED, &local->scanning))		return false; 	if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {		local->hw_scan_req->req.n_channels = req->n_channels; 		for (i = 0; i < req->n_channels; i++) {			local->hw_scan_req->req.channels[i] = req->channels[i];			bands_used |= BIT(req->channels[i]->band);		}	} else {		do {			if (local->hw_scan_band == NUM_NL80211_BANDS)				return false; 			n_chans = &local->hw_scan_req->req.n_channels;			*n_chans = 0; 			for (i = 0; i < req->n_channels; i++) {				if (req->channels[i]->band !=				    local->hw_scan_band)					continue;				local->hw_scan_req->req.channels[(*n_chans)++] =							req->channels[i]; 				bands_used |= BIT(req->channels[i]->band);			} 			local->hw_scan_band++;		} while (!*n_chans);	} 	ieee80211_prepare_scan_chandef(&chandef); 	if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)		flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT; 	ielen = ieee80211_build_preq_ies(sdata,					 (u8 *)local->hw_scan_req->req.ie,					 local->hw_scan_ies_bufsize,					 &local->hw_scan_req->ies,					 req->ie, req->ie_len,					 bands_used, req->rates, &chandef,					 flags);	if (ielen < 0)		return false;	local->hw_scan_req->req.ie_len = ielen;	local->hw_scan_req->req.no_cck = req->no_cck;	ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr);	ether_addr_copy(local->hw_scan_req->req.mac_addr_mask,			req->mac_addr_mask);	ether_addr_copy(local->hw_scan_req->req.bssid, req->bssid); 	return true;} static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted){	struct ieee80211_local *local = hw_to_local(hw);	bool hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);	bool was_scanning = local->scanning;	struct cfg80211_scan_request *scan_req;	struct ieee80211_sub_if_data *scan_sdata;	struct ieee80211_sub_if_data *sdata; 	lockdep_assert_wiphy(local->hw.wiphy); 	/*	 * It's ok to abort a not-yet-running scan (that	 * we have one at all will be verified by checking	 * local->scan_req next), but not to complete it	 * successfully.	 */	if (WARN_ON(!local->scanning && !aborted))		aborted = true; 	if (WARN_ON(!local->scan_req))		return; 	scan_sdata = rcu_dereference_protected(local->scan_sdata,					       lockdep_is_held(&local->hw.wiphy->mtx)); 	if (hw_scan && !aborted &&	    !ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS) &&	    ieee80211_prep_hw_scan(scan_sdata)) {		int rc; 		rc = drv_hw_scan(local,			rcu_dereference_protected(local->scan_sdata,						  lockdep_is_held(&local->hw.wiphy->mtx)),			local->hw_scan_req); 		if (rc == 0)			return; 		/* HW scan failed and is going to be reported as aborted,		 * so clear old scan info.		 */		memset(&local->scan_info, 0, sizeof(local->scan_info));		aborted = true;	} 	kfree(local->hw_scan_req);	local->hw_scan_req = NULL; 	scan_req = rcu_dereference_protected(local->scan_req,					     lockdep_is_held(&local->hw.wiphy->mtx)); 	RCU_INIT_POINTER(local->scan_req, NULL);

Lines 1–500 of 1482.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t