2625 lines · 68.3 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * cfg80211 scan result handling * * Copyright 2008 Johannes Berg <johannes@sipsolutions.net> * Copyright 2013-2014  Intel Mobile Communications GmbH * Copyright 2016	Intel Deutschland GmbH * Copyright (C) 2018-2019 Intel Corporation */#include <linux/kernel.h>#include <linux/slab.h>#include <linux/module.h>#include <linux/netdevice.h>#include <linux/wireless.h>#include <linux/nl80211.h>#include <linux/etherdevice.h>#include <net/arp.h>#include <net/cfg80211.h>#include <net/cfg80211-wext.h>#include <net/iw_handler.h>#include "core.h"#include "nl80211.h"#include "wext-compat.h"#include "rdev-ops.h" /** * DOC: BSS tree/list structure * * At the top level, the BSS list is kept in both a list in each * registered device (@bss_list) as well as an RB-tree for faster * lookup. In the RB-tree, entries can be looked up using their * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID * for other BSSes. * * Due to the possibility of hidden SSIDs, there's a second level * structure, the "hidden_list" and "hidden_beacon_bss" pointer. * The hidden_list connects all BSSes belonging to a single AP * that has a hidden SSID, and connects beacon and probe response * entries. For a probe response entry for a hidden SSID, the * hidden_beacon_bss pointer points to the BSS struct holding the * beacon's information. * * Reference counting is done for all these references except for * the hidden_list, so that a beacon BSS struct that is otherwise * not referenced has one reference for being on the bss_list and * one for each probe response entry that points to it using the * hidden_beacon_bss pointer. When a BSS struct that has such a * pointer is get/put, the refcount update is also propagated to * the referenced struct, this ensure that it cannot get removed * while somebody is using the probe response version. * * Note that the hidden_beacon_bss pointer never changes, due to * the reference counting. Therefore, no locking is needed for * it. * * Also note that the hidden_beacon_bss pointer is only relevant * if the driver uses something other than the IEs, e.g. private * data stored stored in the BSS struct, since the beacon IEs are * also linked into the probe response struct. */ /* * Limit the number of BSS entries stored in mac80211. Each one is * a bit over 4k at most, so this limits to roughly 4-5M of memory. * If somebody wants to really attack this though, they'd likely * use small beacons, and only one type of frame, limiting each of * the entries to a much smaller size (in order to generate more * entries in total, so overhead is bigger.) */static int bss_entries_limit = 1000;module_param(bss_entries_limit, int, 0644);MODULE_PARM_DESC(bss_entries_limit,                 "limit to number of scan BSS entries (per wiphy, default 1000)"); #define IEEE80211_SCAN_RESULT_EXPIRE	(30 * HZ) static void bss_free(struct cfg80211_internal_bss *bss){	struct cfg80211_bss_ies *ies; 	if (WARN_ON(atomic_read(&bss->hold)))		return; 	ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);	if (ies && !bss->pub.hidden_beacon_bss)		kfree_rcu(ies, rcu_head);	ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);	if (ies)		kfree_rcu(ies, rcu_head); 	/*	 * This happens when the module is removed, it doesn't	 * really matter any more save for completeness	 */	if (!list_empty(&bss->hidden_list))		list_del(&bss->hidden_list); 	kfree(bss);} static inline void bss_ref_get(struct cfg80211_registered_device *rdev,			       struct cfg80211_internal_bss *bss){	lockdep_assert_held(&rdev->bss_lock); 	bss->refcount++;	if (bss->pub.hidden_beacon_bss) {		bss = container_of(bss->pub.hidden_beacon_bss,				   struct cfg80211_internal_bss,				   pub);		bss->refcount++;	}	if (bss->pub.transmitted_bss) {		bss = container_of(bss->pub.transmitted_bss,				   struct cfg80211_internal_bss,				   pub);		bss->refcount++;	}} static inline void bss_ref_put(struct cfg80211_registered_device *rdev,			       struct cfg80211_internal_bss *bss){	lockdep_assert_held(&rdev->bss_lock); 	if (bss->pub.hidden_beacon_bss) {		struct cfg80211_internal_bss *hbss;		hbss = container_of(bss->pub.hidden_beacon_bss,				    struct cfg80211_internal_bss,				    pub);		hbss->refcount--;		if (hbss->refcount == 0)			bss_free(hbss);	} 	if (bss->pub.transmitted_bss) {		struct cfg80211_internal_bss *tbss; 		tbss = container_of(bss->pub.transmitted_bss,				    struct cfg80211_internal_bss,				    pub);		tbss->refcount--;		if (tbss->refcount == 0)			bss_free(tbss);	} 	bss->refcount--;	if (bss->refcount == 0)		bss_free(bss);} static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,				  struct cfg80211_internal_bss *bss){	lockdep_assert_held(&rdev->bss_lock); 	if (!list_empty(&bss->hidden_list)) {		/*		 * don't remove the beacon entry if it has		 * probe responses associated with it		 */		if (!bss->pub.hidden_beacon_bss)			return false;		/*		 * if it's a probe response entry break its		 * link to the other entries in the group		 */		list_del_init(&bss->hidden_list);	} 	list_del_init(&bss->list);	list_del_init(&bss->pub.nontrans_list);	rb_erase(&bss->rbn, &rdev->bss_tree);	rdev->bss_entries--;	WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),		  "rdev bss entries[%d]/list[empty:%d] corruption\n",		  rdev->bss_entries, list_empty(&rdev->bss_list));	bss_ref_put(rdev, bss);	return true;} bool cfg80211_is_element_inherited(const struct element *elem,				   const struct element *non_inherit_elem){	u8 id_len, ext_id_len, i, loop_len, id;	const u8 *list; 	if (elem->id == WLAN_EID_MULTIPLE_BSSID)		return false; 	if (!non_inherit_elem || non_inherit_elem->datalen < 2)		return true; 	/*	 * non inheritance element format is:	 * ext ID (56) | IDs list len | list | extension IDs list len | list	 * Both lists are optional. Both lengths are mandatory.	 * This means valid length is:	 * elem_len = 1 (extension ID) + 2 (list len fields) + list lengths	 */	id_len = non_inherit_elem->data[1];	if (non_inherit_elem->datalen < 3 + id_len)		return true; 	ext_id_len = non_inherit_elem->data[2 + id_len];	if (non_inherit_elem->datalen < 3 + id_len + ext_id_len)		return true; 	if (elem->id == WLAN_EID_EXTENSION) {		if (!ext_id_len)			return true;		loop_len = ext_id_len;		list = &non_inherit_elem->data[3 + id_len];		id = elem->data[0];	} else {		if (!id_len)			return true;		loop_len = id_len;		list = &non_inherit_elem->data[2];		id = elem->id;	} 	for (i = 0; i < loop_len; i++) {		if (list[i] == id)			return false;	} 	return true;}EXPORT_SYMBOL(cfg80211_is_element_inherited); static size_t cfg80211_gen_new_ie(const u8 *ie, size_t ielen,				  const u8 *subelement, size_t subie_len,				  u8 *new_ie, gfp_t gfp){	u8 *pos, *tmp;	const u8 *tmp_old, *tmp_new;	const struct element *non_inherit_elem;	u8 *sub_copy; 	/* copy subelement as we need to change its content to	 * mark an ie after it is processed.	 */	sub_copy = kmemdup(subelement, subie_len, gfp);	if (!sub_copy)		return 0; 	pos = &new_ie[0]; 	/* set new ssid */	tmp_new = cfg80211_find_ie(WLAN_EID_SSID, sub_copy, subie_len);	if (tmp_new) {		memcpy(pos, tmp_new, tmp_new[1] + 2);		pos += (tmp_new[1] + 2);	} 	/* get non inheritance list if exists */	non_inherit_elem =		cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,				       sub_copy, subie_len); 	/* go through IEs in ie (skip SSID) and subelement,	 * merge them into new_ie	 */	tmp_old = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);	tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + 2 : ie; 	while (tmp_old + tmp_old[1] + 2 - ie <= ielen) {		if (tmp_old[0] == 0) {			tmp_old++;			continue;		} 		if (tmp_old[0] == WLAN_EID_EXTENSION)			tmp = (u8 *)cfg80211_find_ext_ie(tmp_old[2], sub_copy,							 subie_len);		else			tmp = (u8 *)cfg80211_find_ie(tmp_old[0], sub_copy,						     subie_len); 		if (!tmp) {			const struct element *old_elem = (void *)tmp_old; 			/* ie in old ie but not in subelement */			if (cfg80211_is_element_inherited(old_elem,							  non_inherit_elem)) {				memcpy(pos, tmp_old, tmp_old[1] + 2);				pos += tmp_old[1] + 2;			}		} else {			/* ie in transmitting ie also in subelement,			 * copy from subelement and flag the ie in subelement			 * as copied (by setting eid field to WLAN_EID_SSID,			 * which is skipped anyway).			 * For vendor ie, compare OUI + type + subType to			 * determine if they are the same ie.			 */			if (tmp_old[0] == WLAN_EID_VENDOR_SPECIFIC) {				if (!memcmp(tmp_old + 2, tmp + 2, 5)) {					/* same vendor ie, copy from					 * subelement					 */					memcpy(pos, tmp, tmp[1] + 2);					pos += tmp[1] + 2;					tmp[0] = WLAN_EID_SSID;				} else {					memcpy(pos, tmp_old, tmp_old[1] + 2);					pos += tmp_old[1] + 2;				}			} else {				/* copy ie from subelement into new ie */				memcpy(pos, tmp, tmp[1] + 2);				pos += tmp[1] + 2;				tmp[0] = WLAN_EID_SSID;			}		} 		if (tmp_old + tmp_old[1] + 2 - ie == ielen)			break; 		tmp_old += tmp_old[1] + 2;	} 	/* go through subelement again to check if there is any ie not	 * copied to new ie, skip ssid, capability, bssid-index ie	 */	tmp_new = sub_copy;	while (tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) {		if (!(tmp_new[0] == WLAN_EID_NON_TX_BSSID_CAP ||		      tmp_new[0] == WLAN_EID_SSID)) {			memcpy(pos, tmp_new, tmp_new[1] + 2);			pos += tmp_new[1] + 2;		}		if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)			break;		tmp_new += tmp_new[1] + 2;	} 	kfree(sub_copy);	return pos - new_ie;} static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,		   const u8 *ssid, size_t ssid_len){	const struct cfg80211_bss_ies *ies;	const u8 *ssidie; 	if (bssid && !ether_addr_equal(a->bssid, bssid))		return false; 	if (!ssid)		return true; 	ies = rcu_access_pointer(a->ies);	if (!ies)		return false;	ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);	if (!ssidie)		return false;	if (ssidie[1] != ssid_len)		return false;	return memcmp(ssidie + 2, ssid, ssid_len) == 0;} static intcfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,			   struct cfg80211_bss *nontrans_bss){	const u8 *ssid;	size_t ssid_len;	struct cfg80211_bss *bss = NULL; 	rcu_read_lock();	ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);	if (!ssid) {		rcu_read_unlock();		return -EINVAL;	}	ssid_len = ssid[1];	ssid = ssid + 2;	rcu_read_unlock(); 	/* check if nontrans_bss is in the list */	list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {		if (is_bss(bss, nontrans_bss->bssid, ssid, ssid_len))			return 0;	} 	/* add to the list */	list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);	return 0;} static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,				  unsigned long expire_time){	struct cfg80211_internal_bss *bss, *tmp;	bool expired = false; 	lockdep_assert_held(&rdev->bss_lock); 	list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {		if (atomic_read(&bss->hold))			continue;		if (!time_after(expire_time, bss->ts))			continue; 		if (__cfg80211_unlink_bss(rdev, bss))			expired = true;	} 	if (expired)		rdev->bss_generation++;} static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev){	struct cfg80211_internal_bss *bss, *oldest = NULL;	bool ret; 	lockdep_assert_held(&rdev->bss_lock); 	list_for_each_entry(bss, &rdev->bss_list, list) {		if (atomic_read(&bss->hold))			continue; 		if (!list_empty(&bss->hidden_list) &&		    !bss->pub.hidden_beacon_bss)			continue; 		if (oldest && time_before(oldest->ts, bss->ts))			continue;		oldest = bss;	} 	if (WARN_ON(!oldest))		return false; 	/*	 * The callers make sure to increase rdev->bss_generation if anything	 * gets removed (and a new entry added), so there's no need to also do	 * it here.	 */ 	ret = __cfg80211_unlink_bss(rdev, oldest);	WARN_ON(!ret);	return ret;} void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,			   bool send_message){	struct cfg80211_scan_request *request;	struct wireless_dev *wdev;	struct sk_buff *msg;#ifdef CONFIG_CFG80211_WEXT	union iwreq_data wrqu;#endif 	ASSERT_RTNL(); 	if (rdev->scan_msg) {		nl80211_send_scan_msg(rdev, rdev->scan_msg);		rdev->scan_msg = NULL;		return;	} 	request = rdev->scan_req;	if (!request)		return; 	wdev = request->wdev; 	/*	 * This must be before sending the other events!	 * Otherwise, wpa_supplicant gets completely confused with	 * wext events.	 */	if (wdev->netdev)		cfg80211_sme_scan_done(wdev->netdev); 	if (!request->info.aborted &&	    request->flags & NL80211_SCAN_FLAG_FLUSH) {		/* flush entries from previous scans */		spin_lock_bh(&rdev->bss_lock);		__cfg80211_bss_expire(rdev, request->scan_start);		spin_unlock_bh(&rdev->bss_lock);	} 	msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted); #ifdef CONFIG_CFG80211_WEXT	if (wdev->netdev && !request->info.aborted) {		memset(&wrqu, 0, sizeof(wrqu)); 		wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);	}#endif 

Lines 1–500 of 2625.

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