• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * cfg80211 scan result handling
4  *
5  * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright 2016	Intel Deutschland GmbH
8  * Copyright (C) 2018-2019 Intel Corporation
9  */
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/netdevice.h>
14 #include <linux/wireless.h>
15 #include <linux/nl80211.h>
16 #include <linux/etherdevice.h>
17 #include <net/arp.h>
18 #include <net/cfg80211.h>
19 #include <net/cfg80211-wext.h>
20 #include <net/iw_handler.h>
21 #include "core.h"
22 #include "nl80211.h"
23 #include "wext-compat.h"
24 #include "rdev-ops.h"
25 
26 /**
27  * DOC: BSS tree/list structure
28  *
29  * At the top level, the BSS list is kept in both a list in each
30  * registered device (@bss_list) as well as an RB-tree for faster
31  * lookup. In the RB-tree, entries can be looked up using their
32  * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID
33  * for other BSSes.
34  *
35  * Due to the possibility of hidden SSIDs, there's a second level
36  * structure, the "hidden_list" and "hidden_beacon_bss" pointer.
37  * The hidden_list connects all BSSes belonging to a single AP
38  * that has a hidden SSID, and connects beacon and probe response
39  * entries. For a probe response entry for a hidden SSID, the
40  * hidden_beacon_bss pointer points to the BSS struct holding the
41  * beacon's information.
42  *
43  * Reference counting is done for all these references except for
44  * the hidden_list, so that a beacon BSS struct that is otherwise
45  * not referenced has one reference for being on the bss_list and
46  * one for each probe response entry that points to it using the
47  * hidden_beacon_bss pointer. When a BSS struct that has such a
48  * pointer is get/put, the refcount update is also propagated to
49  * the referenced struct, this ensure that it cannot get removed
50  * while somebody is using the probe response version.
51  *
52  * Note that the hidden_beacon_bss pointer never changes, due to
53  * the reference counting. Therefore, no locking is needed for
54  * it.
55  *
56  * Also note that the hidden_beacon_bss pointer is only relevant
57  * if the driver uses something other than the IEs, e.g. private
58  * data stored stored in the BSS struct, since the beacon IEs are
59  * also linked into the probe response struct.
60  */
61 
62 /*
63  * Limit the number of BSS entries stored in mac80211. Each one is
64  * a bit over 4k at most, so this limits to roughly 4-5M of memory.
65  * If somebody wants to really attack this though, they'd likely
66  * use small beacons, and only one type of frame, limiting each of
67  * the entries to a much smaller size (in order to generate more
68  * entries in total, so overhead is bigger.)
69  */
70 static int bss_entries_limit = 1000;
71 module_param(bss_entries_limit, int, 0644);
72 MODULE_PARM_DESC(bss_entries_limit,
73                  "limit to number of scan BSS entries (per wiphy, default 1000)");
74 
75 #define IEEE80211_SCAN_RESULT_EXPIRE	(30 * HZ)
76 
bss_free(struct cfg80211_internal_bss * bss)77 static void bss_free(struct cfg80211_internal_bss *bss)
78 {
79 	struct cfg80211_bss_ies *ies;
80 
81 	if (WARN_ON(atomic_read(&bss->hold)))
82 		return;
83 
84 	ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
85 	if (ies && !bss->pub.hidden_beacon_bss)
86 		kfree_rcu(ies, rcu_head);
87 	ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
88 	if (ies)
89 		kfree_rcu(ies, rcu_head);
90 
91 	/*
92 	 * This happens when the module is removed, it doesn't
93 	 * really matter any more save for completeness
94 	 */
95 	if (!list_empty(&bss->hidden_list))
96 		list_del(&bss->hidden_list);
97 
98 	kfree(bss);
99 }
100 
bss_ref_get(struct cfg80211_registered_device * rdev,struct cfg80211_internal_bss * bss)101 static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
102 			       struct cfg80211_internal_bss *bss)
103 {
104 	lockdep_assert_held(&rdev->bss_lock);
105 
106 	bss->refcount++;
107 
108 	if (bss->pub.hidden_beacon_bss)
109 		bss_from_pub(bss->pub.hidden_beacon_bss)->refcount++;
110 
111 	if (bss->pub.transmitted_bss)
112 		bss_from_pub(bss->pub.transmitted_bss)->refcount++;
113 }
114 
bss_ref_put(struct cfg80211_registered_device * rdev,struct cfg80211_internal_bss * bss)115 static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
116 			       struct cfg80211_internal_bss *bss)
117 {
118 	lockdep_assert_held(&rdev->bss_lock);
119 
120 	if (bss->pub.hidden_beacon_bss) {
121 		struct cfg80211_internal_bss *hbss;
122 		hbss = container_of(bss->pub.hidden_beacon_bss,
123 				    struct cfg80211_internal_bss,
124 				    pub);
125 		hbss->refcount--;
126 		if (hbss->refcount == 0)
127 			bss_free(hbss);
128 	}
129 
130 	if (bss->pub.transmitted_bss) {
131 		struct cfg80211_internal_bss *tbss;
132 
133 		tbss = container_of(bss->pub.transmitted_bss,
134 				    struct cfg80211_internal_bss,
135 				    pub);
136 		tbss->refcount--;
137 		if (tbss->refcount == 0)
138 			bss_free(tbss);
139 	}
140 
141 	bss->refcount--;
142 	if (bss->refcount == 0)
143 		bss_free(bss);
144 }
145 
__cfg80211_unlink_bss(struct cfg80211_registered_device * rdev,struct cfg80211_internal_bss * bss)146 static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
147 				  struct cfg80211_internal_bss *bss)
148 {
149 	lockdep_assert_held(&rdev->bss_lock);
150 
151 	if (!list_empty(&bss->hidden_list)) {
152 		/*
153 		 * don't remove the beacon entry if it has
154 		 * probe responses associated with it
155 		 */
156 		if (!bss->pub.hidden_beacon_bss)
157 			return false;
158 		/*
159 		 * if it's a probe response entry break its
160 		 * link to the other entries in the group
161 		 */
162 		list_del_init(&bss->hidden_list);
163 	}
164 
165 	list_del_init(&bss->list);
166 	list_del_init(&bss->pub.nontrans_list);
167 	rb_erase(&bss->rbn, &rdev->bss_tree);
168 	rdev->bss_entries--;
169 	WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
170 		  "rdev bss entries[%d]/list[empty:%d] corruption\n",
171 		  rdev->bss_entries, list_empty(&rdev->bss_list));
172 	bss_ref_put(rdev, bss);
173 	return true;
174 }
175 
cfg80211_is_element_inherited(const struct element * elem,const struct element * non_inherit_elem)176 bool cfg80211_is_element_inherited(const struct element *elem,
177 				   const struct element *non_inherit_elem)
178 {
179 	u8 id_len, ext_id_len, i, loop_len, id;
180 	const u8 *list;
181 
182 	if (elem->id == WLAN_EID_MULTIPLE_BSSID)
183 		return false;
184 
185 	if (!non_inherit_elem || non_inherit_elem->datalen < 2)
186 		return true;
187 
188 	/*
189 	 * non inheritance element format is:
190 	 * ext ID (56) | IDs list len | list | extension IDs list len | list
191 	 * Both lists are optional. Both lengths are mandatory.
192 	 * This means valid length is:
193 	 * elem_len = 1 (extension ID) + 2 (list len fields) + list lengths
194 	 */
195 	id_len = non_inherit_elem->data[1];
196 	if (non_inherit_elem->datalen < 3 + id_len)
197 		return true;
198 
199 	ext_id_len = non_inherit_elem->data[2 + id_len];
200 	if (non_inherit_elem->datalen < 3 + id_len + ext_id_len)
201 		return true;
202 
203 	if (elem->id == WLAN_EID_EXTENSION) {
204 		if (!ext_id_len)
205 			return true;
206 		loop_len = ext_id_len;
207 		list = &non_inherit_elem->data[3 + id_len];
208 		id = elem->data[0];
209 	} else {
210 		if (!id_len)
211 			return true;
212 		loop_len = id_len;
213 		list = &non_inherit_elem->data[2];
214 		id = elem->id;
215 	}
216 
217 	for (i = 0; i < loop_len; i++) {
218 		if (list[i] == id)
219 			return false;
220 	}
221 
222 	return true;
223 }
224 EXPORT_SYMBOL(cfg80211_is_element_inherited);
225 
cfg80211_copy_elem_with_frags(const struct element * elem,const u8 * ie,size_t ie_len,u8 ** pos,u8 * buf,size_t buf_len)226 static size_t cfg80211_copy_elem_with_frags(const struct element *elem,
227 					    const u8 *ie, size_t ie_len,
228 					    u8 **pos, u8 *buf, size_t buf_len)
229 {
230 	if (WARN_ON((u8 *)elem < ie || elem->data > ie + ie_len ||
231 		    elem->data + elem->datalen > ie + ie_len))
232 		return 0;
233 
234 	if (elem->datalen + 2 > buf + buf_len - *pos)
235 		return 0;
236 
237 	memcpy(*pos, elem, elem->datalen + 2);
238 	*pos += elem->datalen + 2;
239 
240 	/* Finish if it is not fragmented  */
241 	if (elem->datalen != 255)
242 		return *pos - buf;
243 
244 	ie_len = ie + ie_len - elem->data - elem->datalen;
245 	ie = (const u8 *)elem->data + elem->datalen;
246 
247 	for_each_element(elem, ie, ie_len) {
248 		if (elem->id != WLAN_EID_FRAGMENT)
249 			break;
250 
251 		if (elem->datalen + 2 > buf + buf_len - *pos)
252 			return 0;
253 
254 		memcpy(*pos, elem, elem->datalen + 2);
255 		*pos += elem->datalen + 2;
256 
257 		if (elem->datalen != 255)
258 			break;
259 	}
260 
261 	return *pos - buf;
262 }
263 
cfg80211_gen_new_ie(const u8 * ie,size_t ielen,const u8 * subie,size_t subie_len,u8 * new_ie,size_t new_ie_len)264 static size_t cfg80211_gen_new_ie(const u8 *ie, size_t ielen,
265 				  const u8 *subie, size_t subie_len,
266 				  u8 *new_ie, size_t new_ie_len)
267 {
268 	const struct element *non_inherit_elem, *parent, *sub;
269 	u8 *pos = new_ie;
270 	u8 id, ext_id;
271 	unsigned int match_len;
272 
273 	non_inherit_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
274 						  subie, subie_len);
275 
276 	/* We copy the elements one by one from the parent to the generated
277 	 * elements.
278 	 * If they are not inherited (included in subie or in the non
279 	 * inheritance element), then we copy all occurrences the first time
280 	 * we see this element type.
281 	 */
282 	for_each_element(parent, ie, ielen) {
283 		if (parent->id == WLAN_EID_FRAGMENT)
284 			continue;
285 
286 		if (parent->id == WLAN_EID_EXTENSION) {
287 			if (parent->datalen < 1)
288 				continue;
289 
290 			id = WLAN_EID_EXTENSION;
291 			ext_id = parent->data[0];
292 			match_len = 1;
293 		} else {
294 			id = parent->id;
295 			match_len = 0;
296 		}
297 
298 		/* Find first occurrence in subie */
299 		sub = cfg80211_find_elem_match(id, subie, subie_len,
300 					       &ext_id, match_len, 0);
301 
302 		/* Copy from parent if not in subie and inherited */
303 		if (!sub &&
304 		    cfg80211_is_element_inherited(parent, non_inherit_elem)) {
305 			if (!cfg80211_copy_elem_with_frags(parent,
306 							   ie, ielen,
307 							   &pos, new_ie,
308 							   new_ie_len))
309 				return 0;
310 
311 			continue;
312 		}
313 
314 		/* Already copied if an earlier element had the same type */
315 		if (cfg80211_find_elem_match(id, ie, (u8 *)parent - ie,
316 					     &ext_id, match_len, 0))
317 			continue;
318 
319 		/* Not inheriting, copy all similar elements from subie */
320 		while (sub) {
321 			if (!cfg80211_copy_elem_with_frags(sub,
322 							   subie, subie_len,
323 							   &pos, new_ie,
324 							   new_ie_len))
325 				return 0;
326 
327 			sub = cfg80211_find_elem_match(id,
328 						       sub->data + sub->datalen,
329 						       subie_len + subie -
330 						       (sub->data +
331 							sub->datalen),
332 						       &ext_id, match_len, 0);
333 		}
334 	}
335 
336 	/* The above misses elements that are included in subie but not in the
337 	 * parent, so do a pass over subie and append those.
338 	 * Skip the non-tx BSSID caps and non-inheritance element.
339 	 */
340 	for_each_element(sub, subie, subie_len) {
341 		if (sub->id == WLAN_EID_NON_TX_BSSID_CAP)
342 			continue;
343 
344 		if (sub->id == WLAN_EID_FRAGMENT)
345 			continue;
346 
347 		if (sub->id == WLAN_EID_EXTENSION) {
348 			if (sub->datalen < 1)
349 				continue;
350 
351 			id = WLAN_EID_EXTENSION;
352 			ext_id = sub->data[0];
353 			match_len = 1;
354 
355 			if (ext_id == WLAN_EID_EXT_NON_INHERITANCE)
356 				continue;
357 		} else {
358 			id = sub->id;
359 			match_len = 0;
360 		}
361 
362 		/* Processed if one was included in the parent */
363 		if (cfg80211_find_elem_match(id, ie, ielen,
364 					     &ext_id, match_len, 0))
365 			continue;
366 
367 		if (!cfg80211_copy_elem_with_frags(sub, subie, subie_len,
368 						   &pos, new_ie, new_ie_len))
369 			return 0;
370 	}
371 
372 	return pos - new_ie;
373 }
374 
is_bss(struct cfg80211_bss * a,const u8 * bssid,const u8 * ssid,size_t ssid_len)375 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
376 		   const u8 *ssid, size_t ssid_len)
377 {
378 	const struct cfg80211_bss_ies *ies;
379 	const u8 *ssidie;
380 
381 	if (bssid && !ether_addr_equal(a->bssid, bssid))
382 		return false;
383 
384 	if (!ssid)
385 		return true;
386 
387 	ies = rcu_access_pointer(a->ies);
388 	if (!ies)
389 		return false;
390 	ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
391 	if (!ssidie)
392 		return false;
393 	if (ssidie[1] != ssid_len)
394 		return false;
395 	return memcmp(ssidie + 2, ssid, ssid_len) == 0;
396 }
397 
398 static int
cfg80211_add_nontrans_list(struct cfg80211_bss * trans_bss,struct cfg80211_bss * nontrans_bss)399 cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
400 			   struct cfg80211_bss *nontrans_bss)
401 {
402 	const u8 *ssid;
403 	size_t ssid_len;
404 	struct cfg80211_bss *bss = NULL;
405 
406 	rcu_read_lock();
407 	ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
408 	if (!ssid) {
409 		rcu_read_unlock();
410 		return -EINVAL;
411 	}
412 	ssid_len = ssid[1];
413 	ssid = ssid + 2;
414 
415 	/* check if nontrans_bss is in the list */
416 	list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
417 		if (is_bss(bss, nontrans_bss->bssid, ssid, ssid_len)) {
418 			rcu_read_unlock();
419 			return 0;
420 		}
421 	}
422 
423 	rcu_read_unlock();
424 
425 	/*
426 	 * This is a bit weird - it's not on the list, but already on another
427 	 * one! The only way that could happen is if there's some BSSID/SSID
428 	 * shared by multiple APs in their multi-BSSID profiles, potentially
429 	 * with hidden SSID mixed in ... ignore it.
430 	 */
431 	if (!list_empty(&nontrans_bss->nontrans_list))
432 		return -EINVAL;
433 
434 	/* add to the list */
435 	list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
436 	return 0;
437 }
438 
__cfg80211_bss_expire(struct cfg80211_registered_device * rdev,unsigned long expire_time)439 static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
440 				  unsigned long expire_time)
441 {
442 	struct cfg80211_internal_bss *bss, *tmp;
443 	bool expired = false;
444 
445 	lockdep_assert_held(&rdev->bss_lock);
446 
447 	list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
448 		if (atomic_read(&bss->hold))
449 			continue;
450 		if (!time_after(expire_time, bss->ts))
451 			continue;
452 
453 		if (__cfg80211_unlink_bss(rdev, bss))
454 			expired = true;
455 	}
456 
457 	if (expired)
458 		rdev->bss_generation++;
459 }
460 
cfg80211_bss_expire_oldest(struct cfg80211_registered_device * rdev)461 static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
462 {
463 	struct cfg80211_internal_bss *bss, *oldest = NULL;
464 	bool ret;
465 
466 	lockdep_assert_held(&rdev->bss_lock);
467 
468 	list_for_each_entry(bss, &rdev->bss_list, list) {
469 		if (atomic_read(&bss->hold))
470 			continue;
471 
472 		if (!list_empty(&bss->hidden_list) &&
473 		    !bss->pub.hidden_beacon_bss)
474 			continue;
475 
476 		if (oldest && time_before(oldest->ts, bss->ts))
477 			continue;
478 		oldest = bss;
479 	}
480 
481 	if (WARN_ON(!oldest))
482 		return false;
483 
484 	/*
485 	 * The callers make sure to increase rdev->bss_generation if anything
486 	 * gets removed (and a new entry added), so there's no need to also do
487 	 * it here.
488 	 */
489 
490 	ret = __cfg80211_unlink_bss(rdev, oldest);
491 	WARN_ON(!ret);
492 	return ret;
493 }
494 
___cfg80211_scan_done(struct cfg80211_registered_device * rdev,bool send_message)495 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
496 			   bool send_message)
497 {
498 	struct cfg80211_scan_request *request;
499 	struct wireless_dev *wdev;
500 	struct sk_buff *msg;
501 #ifdef CONFIG_CFG80211_WEXT
502 	union iwreq_data wrqu;
503 #endif
504 
505 	ASSERT_RTNL();
506 
507 	if (rdev->scan_msg) {
508 		nl80211_send_scan_msg(rdev, rdev->scan_msg);
509 		rdev->scan_msg = NULL;
510 		return;
511 	}
512 
513 	request = rdev->scan_req;
514 	if (!request)
515 		return;
516 
517 	wdev = request->wdev;
518 
519 	/*
520 	 * This must be before sending the other events!
521 	 * Otherwise, wpa_supplicant gets completely confused with
522 	 * wext events.
523 	 */
524 	if (wdev->netdev)
525 		cfg80211_sme_scan_done(wdev->netdev);
526 
527 	if (!request->info.aborted &&
528 	    request->flags & NL80211_SCAN_FLAG_FLUSH) {
529 		/* flush entries from previous scans */
530 		spin_lock_bh(&rdev->bss_lock);
531 		__cfg80211_bss_expire(rdev, request->scan_start);
532 		spin_unlock_bh(&rdev->bss_lock);
533 	}
534 
535 	msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
536 
537 #ifdef CONFIG_CFG80211_WEXT
538 	if (wdev->netdev && !request->info.aborted) {
539 		memset(&wrqu, 0, sizeof(wrqu));
540 
541 		wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
542 	}
543 #endif
544 
545 	if (wdev->netdev)
546 		dev_put(wdev->netdev);
547 
548 	rdev->scan_req = NULL;
549 	kfree(request);
550 
551 	if (!send_message)
552 		rdev->scan_msg = msg;
553 	else
554 		nl80211_send_scan_msg(rdev, msg);
555 }
556 
__cfg80211_scan_done(struct work_struct * wk)557 void __cfg80211_scan_done(struct work_struct *wk)
558 {
559 	struct cfg80211_registered_device *rdev;
560 
561 	rdev = container_of(wk, struct cfg80211_registered_device,
562 			    scan_done_wk);
563 
564 	rtnl_lock();
565 	___cfg80211_scan_done(rdev, true);
566 	rtnl_unlock();
567 }
568 
cfg80211_scan_done(struct cfg80211_scan_request * request,struct cfg80211_scan_info * info)569 void cfg80211_scan_done(struct cfg80211_scan_request *request,
570 			struct cfg80211_scan_info *info)
571 {
572 	trace_cfg80211_scan_done(request, info);
573 	WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
574 
575 	request->info = *info;
576 	request->notified = true;
577 	queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
578 }
579 EXPORT_SYMBOL(cfg80211_scan_done);
580 
cfg80211_add_sched_scan_req(struct cfg80211_registered_device * rdev,struct cfg80211_sched_scan_request * req)581 void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
582 				 struct cfg80211_sched_scan_request *req)
583 {
584 	ASSERT_RTNL();
585 
586 	list_add_rcu(&req->list, &rdev->sched_scan_req_list);
587 }
588 
cfg80211_del_sched_scan_req(struct cfg80211_registered_device * rdev,struct cfg80211_sched_scan_request * req)589 static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
590 					struct cfg80211_sched_scan_request *req)
591 {
592 	ASSERT_RTNL();
593 
594 	list_del_rcu(&req->list);
595 	kfree_rcu(req, rcu_head);
596 }
597 
598 static struct cfg80211_sched_scan_request *
cfg80211_find_sched_scan_req(struct cfg80211_registered_device * rdev,u64 reqid)599 cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
600 {
601 	struct cfg80211_sched_scan_request *pos;
602 
603 	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
604 
605 	list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list) {
606 		if (pos->reqid == reqid)
607 			return pos;
608 	}
609 	return NULL;
610 }
611 
612 /*
613  * Determines if a scheduled scan request can be handled. When a legacy
614  * scheduled scan is running no other scheduled scan is allowed regardless
615  * whether the request is for legacy or multi-support scan. When a multi-support
616  * scheduled scan is running a request for legacy scan is not allowed. In this
617  * case a request for multi-support scan can be handled if resources are
618  * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
619  */
cfg80211_sched_scan_req_possible(struct cfg80211_registered_device * rdev,bool want_multi)620 int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
621 				     bool want_multi)
622 {
623 	struct cfg80211_sched_scan_request *pos;
624 	int i = 0;
625 
626 	list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
627 		/* request id zero means legacy in progress */
628 		if (!i && !pos->reqid)
629 			return -EINPROGRESS;
630 		i++;
631 	}
632 
633 	if (i) {
634 		/* no legacy allowed when multi request(s) are active */
635 		if (!want_multi)
636 			return -EINPROGRESS;
637 
638 		/* resource limit reached */
639 		if (i == rdev->wiphy.max_sched_scan_reqs)
640 			return -ENOSPC;
641 	}
642 	return 0;
643 }
644 
cfg80211_sched_scan_results_wk(struct work_struct * work)645 void cfg80211_sched_scan_results_wk(struct work_struct *work)
646 {
647 	struct cfg80211_registered_device *rdev;
648 	struct cfg80211_sched_scan_request *req, *tmp;
649 
650 	rdev = container_of(work, struct cfg80211_registered_device,
651 			   sched_scan_res_wk);
652 
653 	rtnl_lock();
654 	list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
655 		if (req->report_results) {
656 			req->report_results = false;
657 			if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
658 				/* flush entries from previous scans */
659 				spin_lock_bh(&rdev->bss_lock);
660 				__cfg80211_bss_expire(rdev, req->scan_start);
661 				spin_unlock_bh(&rdev->bss_lock);
662 				req->scan_start = jiffies;
663 			}
664 			nl80211_send_sched_scan(req,
665 						NL80211_CMD_SCHED_SCAN_RESULTS);
666 		}
667 	}
668 	rtnl_unlock();
669 }
670 
cfg80211_sched_scan_results(struct wiphy * wiphy,u64 reqid)671 void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
672 {
673 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
674 	struct cfg80211_sched_scan_request *request;
675 
676 	trace_cfg80211_sched_scan_results(wiphy, reqid);
677 	/* ignore if we're not scanning */
678 
679 	rcu_read_lock();
680 	request = cfg80211_find_sched_scan_req(rdev, reqid);
681 	if (request) {
682 		request->report_results = true;
683 		queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
684 	}
685 	rcu_read_unlock();
686 }
687 EXPORT_SYMBOL(cfg80211_sched_scan_results);
688 
cfg80211_sched_scan_stopped_rtnl(struct wiphy * wiphy,u64 reqid)689 void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
690 {
691 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
692 
693 	ASSERT_RTNL();
694 
695 	trace_cfg80211_sched_scan_stopped(wiphy, reqid);
696 
697 	__cfg80211_stop_sched_scan(rdev, reqid, true);
698 }
699 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
700 
cfg80211_sched_scan_stopped(struct wiphy * wiphy,u64 reqid)701 void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
702 {
703 	rtnl_lock();
704 	cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
705 	rtnl_unlock();
706 }
707 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
708 
cfg80211_stop_sched_scan_req(struct cfg80211_registered_device * rdev,struct cfg80211_sched_scan_request * req,bool driver_initiated)709 int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
710 				 struct cfg80211_sched_scan_request *req,
711 				 bool driver_initiated)
712 {
713 	ASSERT_RTNL();
714 
715 	if (!driver_initiated) {
716 		int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
717 		if (err)
718 			return err;
719 	}
720 
721 	nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
722 
723 	cfg80211_del_sched_scan_req(rdev, req);
724 
725 	return 0;
726 }
727 
__cfg80211_stop_sched_scan(struct cfg80211_registered_device * rdev,u64 reqid,bool driver_initiated)728 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
729 			       u64 reqid, bool driver_initiated)
730 {
731 	struct cfg80211_sched_scan_request *sched_scan_req;
732 
733 	ASSERT_RTNL();
734 
735 	sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
736 	if (!sched_scan_req)
737 		return -ENOENT;
738 
739 	return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
740 					    driver_initiated);
741 }
742 
cfg80211_bss_age(struct cfg80211_registered_device * rdev,unsigned long age_secs)743 void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
744                       unsigned long age_secs)
745 {
746 	struct cfg80211_internal_bss *bss;
747 	unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
748 
749 	spin_lock_bh(&rdev->bss_lock);
750 	list_for_each_entry(bss, &rdev->bss_list, list)
751 		bss->ts -= age_jiffies;
752 	spin_unlock_bh(&rdev->bss_lock);
753 }
754 
cfg80211_bss_expire(struct cfg80211_registered_device * rdev)755 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
756 {
757 	__cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
758 }
759 
760 const struct element *
cfg80211_find_elem_match(u8 eid,const u8 * ies,unsigned int len,const u8 * match,unsigned int match_len,unsigned int match_offset)761 cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
762 			 const u8 *match, unsigned int match_len,
763 			 unsigned int match_offset)
764 {
765 	const struct element *elem;
766 
767 	for_each_element_id(elem, eid, ies, len) {
768 		if (elem->datalen >= match_offset + match_len &&
769 		    !memcmp(elem->data + match_offset, match, match_len))
770 			return elem;
771 	}
772 
773 	return NULL;
774 }
775 EXPORT_SYMBOL(cfg80211_find_elem_match);
776 
cfg80211_find_vendor_elem(unsigned int oui,int oui_type,const u8 * ies,unsigned int len)777 const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
778 						const u8 *ies,
779 						unsigned int len)
780 {
781 	const struct element *elem;
782 	u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
783 	int match_len = (oui_type < 0) ? 3 : sizeof(match);
784 
785 	if (WARN_ON(oui_type > 0xff))
786 		return NULL;
787 
788 	elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
789 					match, match_len, 0);
790 
791 	if (!elem || elem->datalen < 4)
792 		return NULL;
793 
794 	return elem;
795 }
796 EXPORT_SYMBOL(cfg80211_find_vendor_elem);
797 
798 /**
799  * enum bss_compare_mode - BSS compare mode
800  * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
801  * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
802  * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
803  */
804 enum bss_compare_mode {
805 	BSS_CMP_REGULAR,
806 	BSS_CMP_HIDE_ZLEN,
807 	BSS_CMP_HIDE_NUL,
808 };
809 
cmp_bss(struct cfg80211_bss * a,struct cfg80211_bss * b,enum bss_compare_mode mode)810 static int cmp_bss(struct cfg80211_bss *a,
811 		   struct cfg80211_bss *b,
812 		   enum bss_compare_mode mode)
813 {
814 	const struct cfg80211_bss_ies *a_ies, *b_ies;
815 	const u8 *ie1 = NULL;
816 	const u8 *ie2 = NULL;
817 	int i, r;
818 
819 	if (a->channel != b->channel)
820 		return b->channel->center_freq - a->channel->center_freq;
821 
822 	a_ies = rcu_access_pointer(a->ies);
823 	if (!a_ies)
824 		return -1;
825 	b_ies = rcu_access_pointer(b->ies);
826 	if (!b_ies)
827 		return 1;
828 
829 	if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
830 		ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
831 				       a_ies->data, a_ies->len);
832 	if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
833 		ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
834 				       b_ies->data, b_ies->len);
835 	if (ie1 && ie2) {
836 		int mesh_id_cmp;
837 
838 		if (ie1[1] == ie2[1])
839 			mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
840 		else
841 			mesh_id_cmp = ie2[1] - ie1[1];
842 
843 		ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
844 				       a_ies->data, a_ies->len);
845 		ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
846 				       b_ies->data, b_ies->len);
847 		if (ie1 && ie2) {
848 			if (mesh_id_cmp)
849 				return mesh_id_cmp;
850 			if (ie1[1] != ie2[1])
851 				return ie2[1] - ie1[1];
852 			return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
853 		}
854 	}
855 
856 	r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
857 	if (r)
858 		return r;
859 
860 	ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
861 	ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
862 
863 	if (!ie1 && !ie2)
864 		return 0;
865 
866 	/*
867 	 * Note that with "hide_ssid", the function returns a match if
868 	 * the already-present BSS ("b") is a hidden SSID beacon for
869 	 * the new BSS ("a").
870 	 */
871 
872 	/* sort missing IE before (left of) present IE */
873 	if (!ie1)
874 		return -1;
875 	if (!ie2)
876 		return 1;
877 
878 	switch (mode) {
879 	case BSS_CMP_HIDE_ZLEN:
880 		/*
881 		 * In ZLEN mode we assume the BSS entry we're
882 		 * looking for has a zero-length SSID. So if
883 		 * the one we're looking at right now has that,
884 		 * return 0. Otherwise, return the difference
885 		 * in length, but since we're looking for the
886 		 * 0-length it's really equivalent to returning
887 		 * the length of the one we're looking at.
888 		 *
889 		 * No content comparison is needed as we assume
890 		 * the content length is zero.
891 		 */
892 		return ie2[1];
893 	case BSS_CMP_REGULAR:
894 	default:
895 		/* sort by length first, then by contents */
896 		if (ie1[1] != ie2[1])
897 			return ie2[1] - ie1[1];
898 		return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
899 	case BSS_CMP_HIDE_NUL:
900 		if (ie1[1] != ie2[1])
901 			return ie2[1] - ie1[1];
902 		/* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
903 		for (i = 0; i < ie2[1]; i++)
904 			if (ie2[i + 2])
905 				return -1;
906 		return 0;
907 	}
908 }
909 
cfg80211_bss_type_match(u16 capability,enum nl80211_band band,enum ieee80211_bss_type bss_type)910 static bool cfg80211_bss_type_match(u16 capability,
911 				    enum nl80211_band band,
912 				    enum ieee80211_bss_type bss_type)
913 {
914 	bool ret = true;
915 	u16 mask, val;
916 
917 	if (bss_type == IEEE80211_BSS_TYPE_ANY)
918 		return ret;
919 
920 	if (band == NL80211_BAND_60GHZ) {
921 		mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
922 		switch (bss_type) {
923 		case IEEE80211_BSS_TYPE_ESS:
924 			val = WLAN_CAPABILITY_DMG_TYPE_AP;
925 			break;
926 		case IEEE80211_BSS_TYPE_PBSS:
927 			val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
928 			break;
929 		case IEEE80211_BSS_TYPE_IBSS:
930 			val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
931 			break;
932 		default:
933 			return false;
934 		}
935 	} else {
936 		mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
937 		switch (bss_type) {
938 		case IEEE80211_BSS_TYPE_ESS:
939 			val = WLAN_CAPABILITY_ESS;
940 			break;
941 		case IEEE80211_BSS_TYPE_IBSS:
942 			val = WLAN_CAPABILITY_IBSS;
943 			break;
944 		case IEEE80211_BSS_TYPE_MBSS:
945 			val = 0;
946 			break;
947 		default:
948 			return false;
949 		}
950 	}
951 
952 	ret = ((capability & mask) == val);
953 	return ret;
954 }
955 
956 /* Returned bss is reference counted and must be cleaned up appropriately. */
cfg80211_get_bss(struct wiphy * wiphy,struct ieee80211_channel * channel,const u8 * bssid,const u8 * ssid,size_t ssid_len,enum ieee80211_bss_type bss_type,enum ieee80211_privacy privacy)957 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
958 				      struct ieee80211_channel *channel,
959 				      const u8 *bssid,
960 				      const u8 *ssid, size_t ssid_len,
961 				      enum ieee80211_bss_type bss_type,
962 				      enum ieee80211_privacy privacy)
963 {
964 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
965 	struct cfg80211_internal_bss *bss, *res = NULL;
966 	unsigned long now = jiffies;
967 	int bss_privacy;
968 
969 	trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
970 			       privacy);
971 
972 	spin_lock_bh(&rdev->bss_lock);
973 
974 	list_for_each_entry(bss, &rdev->bss_list, list) {
975 		if (!cfg80211_bss_type_match(bss->pub.capability,
976 					     bss->pub.channel->band, bss_type))
977 			continue;
978 
979 		bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
980 		if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
981 		    (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
982 			continue;
983 		if (channel && bss->pub.channel != channel)
984 			continue;
985 		if (!is_valid_ether_addr(bss->pub.bssid))
986 			continue;
987 		/* Don't get expired BSS structs */
988 		if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
989 		    !atomic_read(&bss->hold))
990 			continue;
991 		if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
992 			res = bss;
993 			bss_ref_get(rdev, res);
994 			break;
995 		}
996 	}
997 
998 	spin_unlock_bh(&rdev->bss_lock);
999 	if (!res)
1000 		return NULL;
1001 	trace_cfg80211_return_bss(&res->pub);
1002 	return &res->pub;
1003 }
1004 EXPORT_SYMBOL(cfg80211_get_bss);
1005 
rb_insert_bss(struct cfg80211_registered_device * rdev,struct cfg80211_internal_bss * bss)1006 static void rb_insert_bss(struct cfg80211_registered_device *rdev,
1007 			  struct cfg80211_internal_bss *bss)
1008 {
1009 	struct rb_node **p = &rdev->bss_tree.rb_node;
1010 	struct rb_node *parent = NULL;
1011 	struct cfg80211_internal_bss *tbss;
1012 	int cmp;
1013 
1014 	while (*p) {
1015 		parent = *p;
1016 		tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
1017 
1018 		cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
1019 
1020 		if (WARN_ON(!cmp)) {
1021 			/* will sort of leak this BSS */
1022 			return;
1023 		}
1024 
1025 		if (cmp < 0)
1026 			p = &(*p)->rb_left;
1027 		else
1028 			p = &(*p)->rb_right;
1029 	}
1030 
1031 	rb_link_node(&bss->rbn, parent, p);
1032 	rb_insert_color(&bss->rbn, &rdev->bss_tree);
1033 }
1034 
1035 static struct cfg80211_internal_bss *
rb_find_bss(struct cfg80211_registered_device * rdev,struct cfg80211_internal_bss * res,enum bss_compare_mode mode)1036 rb_find_bss(struct cfg80211_registered_device *rdev,
1037 	    struct cfg80211_internal_bss *res,
1038 	    enum bss_compare_mode mode)
1039 {
1040 	struct rb_node *n = rdev->bss_tree.rb_node;
1041 	struct cfg80211_internal_bss *bss;
1042 	int r;
1043 
1044 	while (n) {
1045 		bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
1046 		r = cmp_bss(&res->pub, &bss->pub, mode);
1047 
1048 		if (r == 0)
1049 			return bss;
1050 		else if (r < 0)
1051 			n = n->rb_left;
1052 		else
1053 			n = n->rb_right;
1054 	}
1055 
1056 	return NULL;
1057 }
1058 
cfg80211_combine_bsses(struct cfg80211_registered_device * rdev,struct cfg80211_internal_bss * new)1059 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
1060 				   struct cfg80211_internal_bss *new)
1061 {
1062 	const struct cfg80211_bss_ies *ies;
1063 	struct cfg80211_internal_bss *bss;
1064 	const u8 *ie;
1065 	int i, ssidlen;
1066 	u8 fold = 0;
1067 	u32 n_entries = 0;
1068 
1069 	ies = rcu_access_pointer(new->pub.beacon_ies);
1070 	if (WARN_ON(!ies))
1071 		return false;
1072 
1073 	ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1074 	if (!ie) {
1075 		/* nothing to do */
1076 		return true;
1077 	}
1078 
1079 	ssidlen = ie[1];
1080 	for (i = 0; i < ssidlen; i++)
1081 		fold |= ie[2 + i];
1082 
1083 	if (fold) {
1084 		/* not a hidden SSID */
1085 		return true;
1086 	}
1087 
1088 	/* This is the bad part ... */
1089 
1090 	list_for_each_entry(bss, &rdev->bss_list, list) {
1091 		/*
1092 		 * we're iterating all the entries anyway, so take the
1093 		 * opportunity to validate the list length accounting
1094 		 */
1095 		n_entries++;
1096 
1097 		if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
1098 			continue;
1099 		if (bss->pub.channel != new->pub.channel)
1100 			continue;
1101 		if (bss->pub.scan_width != new->pub.scan_width)
1102 			continue;
1103 		if (rcu_access_pointer(bss->pub.beacon_ies))
1104 			continue;
1105 		ies = rcu_access_pointer(bss->pub.ies);
1106 		if (!ies)
1107 			continue;
1108 		ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
1109 		if (!ie)
1110 			continue;
1111 		if (ssidlen && ie[1] != ssidlen)
1112 			continue;
1113 		if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
1114 			continue;
1115 		if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
1116 			list_del(&bss->hidden_list);
1117 		/* combine them */
1118 		list_add(&bss->hidden_list, &new->hidden_list);
1119 		bss->pub.hidden_beacon_bss = &new->pub;
1120 		new->refcount += bss->refcount;
1121 		rcu_assign_pointer(bss->pub.beacon_ies,
1122 				   new->pub.beacon_ies);
1123 	}
1124 
1125 	WARN_ONCE(n_entries != rdev->bss_entries,
1126 		  "rdev bss entries[%d]/list[len:%d] corruption\n",
1127 		  rdev->bss_entries, n_entries);
1128 
1129 	return true;
1130 }
1131 
1132 struct cfg80211_non_tx_bss {
1133 	struct cfg80211_bss *tx_bss;
1134 	u8 max_bssid_indicator;
1135 	u8 bssid_index;
1136 };
1137 
cfg80211_update_hidden_bsses(struct cfg80211_internal_bss * known,const struct cfg80211_bss_ies * new_ies,const struct cfg80211_bss_ies * old_ies)1138 static void cfg80211_update_hidden_bsses(struct cfg80211_internal_bss *known,
1139 					 const struct cfg80211_bss_ies *new_ies,
1140 					 const struct cfg80211_bss_ies *old_ies)
1141 {
1142 	struct cfg80211_internal_bss *bss;
1143 
1144 	/* Assign beacon IEs to all sub entries */
1145 	list_for_each_entry(bss, &known->hidden_list, hidden_list) {
1146 		const struct cfg80211_bss_ies *ies;
1147 
1148 		ies = rcu_access_pointer(bss->pub.beacon_ies);
1149 		WARN_ON(ies != old_ies);
1150 
1151 		rcu_assign_pointer(bss->pub.beacon_ies, new_ies);
1152 	}
1153 }
1154 
1155 static bool
cfg80211_update_known_bss(struct cfg80211_registered_device * rdev,struct cfg80211_internal_bss * known,struct cfg80211_internal_bss * new,bool signal_valid)1156 cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
1157 			  struct cfg80211_internal_bss *known,
1158 			  struct cfg80211_internal_bss *new,
1159 			  bool signal_valid)
1160 {
1161 	lockdep_assert_held(&rdev->bss_lock);
1162 
1163 	/* Update IEs */
1164 	if (rcu_access_pointer(new->pub.proberesp_ies)) {
1165 		const struct cfg80211_bss_ies *old;
1166 
1167 		old = rcu_access_pointer(known->pub.proberesp_ies);
1168 
1169 		rcu_assign_pointer(known->pub.proberesp_ies,
1170 				   new->pub.proberesp_ies);
1171 		/* Override possible earlier Beacon frame IEs */
1172 		rcu_assign_pointer(known->pub.ies,
1173 				   new->pub.proberesp_ies);
1174 		if (old)
1175 			kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1176 	} else if (rcu_access_pointer(new->pub.beacon_ies)) {
1177 		const struct cfg80211_bss_ies *old;
1178 
1179 		if (known->pub.hidden_beacon_bss &&
1180 		    !list_empty(&known->hidden_list)) {
1181 			const struct cfg80211_bss_ies *f;
1182 
1183 			/* The known BSS struct is one of the probe
1184 			 * response members of a group, but we're
1185 			 * receiving a beacon (beacon_ies in the new
1186 			 * bss is used). This can only mean that the
1187 			 * AP changed its beacon from not having an
1188 			 * SSID to showing it, which is confusing so
1189 			 * drop this information.
1190 			 */
1191 
1192 			f = rcu_access_pointer(new->pub.beacon_ies);
1193 			kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
1194 			return false;
1195 		}
1196 
1197 		old = rcu_access_pointer(known->pub.beacon_ies);
1198 
1199 		rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
1200 
1201 		/* Override IEs if they were from a beacon before */
1202 		if (old == rcu_access_pointer(known->pub.ies))
1203 			rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
1204 
1205 		cfg80211_update_hidden_bsses(known,
1206 					     rcu_access_pointer(new->pub.beacon_ies),
1207 					     old);
1208 
1209 		if (old)
1210 			kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1211 	}
1212 
1213 	known->pub.beacon_interval = new->pub.beacon_interval;
1214 
1215 	/* don't update the signal if beacon was heard on
1216 	 * adjacent channel.
1217 	 */
1218 	if (signal_valid)
1219 		known->pub.signal = new->pub.signal;
1220 	known->pub.capability = new->pub.capability;
1221 	known->ts = new->ts;
1222 	known->ts_boottime = new->ts_boottime;
1223 	known->parent_tsf = new->parent_tsf;
1224 	known->pub.chains = new->pub.chains;
1225 	memcpy(known->pub.chain_signal, new->pub.chain_signal,
1226 	       IEEE80211_MAX_CHAINS);
1227 	ether_addr_copy(known->parent_bssid, new->parent_bssid);
1228 	known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
1229 	known->pub.bssid_index = new->pub.bssid_index;
1230 
1231 	return true;
1232 }
1233 
1234 /* Returned bss is reference counted and must be cleaned up appropriately. */
1235 struct cfg80211_internal_bss *
cfg80211_bss_update(struct cfg80211_registered_device * rdev,struct cfg80211_internal_bss * tmp,bool signal_valid,unsigned long ts)1236 cfg80211_bss_update(struct cfg80211_registered_device *rdev,
1237 		    struct cfg80211_internal_bss *tmp,
1238 		    bool signal_valid, unsigned long ts)
1239 {
1240 	struct cfg80211_internal_bss *found = NULL;
1241 
1242 	if (WARN_ON(!tmp->pub.channel))
1243 		return NULL;
1244 
1245 	tmp->ts = ts;
1246 
1247 	spin_lock_bh(&rdev->bss_lock);
1248 
1249 	if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
1250 		spin_unlock_bh(&rdev->bss_lock);
1251 		return NULL;
1252 	}
1253 
1254 	found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
1255 
1256 	if (found) {
1257 		if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
1258 			goto drop;
1259 	} else {
1260 		struct cfg80211_internal_bss *new;
1261 		struct cfg80211_internal_bss *hidden;
1262 		struct cfg80211_bss_ies *ies;
1263 
1264 		/*
1265 		 * create a copy -- the "res" variable that is passed in
1266 		 * is allocated on the stack since it's not needed in the
1267 		 * more common case of an update
1268 		 */
1269 		new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
1270 			      GFP_ATOMIC);
1271 		if (!new) {
1272 			ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
1273 			if (ies)
1274 				kfree_rcu(ies, rcu_head);
1275 			ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
1276 			if (ies)
1277 				kfree_rcu(ies, rcu_head);
1278 			goto drop;
1279 		}
1280 		memcpy(new, tmp, sizeof(*new));
1281 		new->refcount = 1;
1282 		INIT_LIST_HEAD(&new->hidden_list);
1283 		INIT_LIST_HEAD(&new->pub.nontrans_list);
1284 		/* we'll set this later if it was non-NULL */
1285 		new->pub.transmitted_bss = NULL;
1286 
1287 		if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
1288 			hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
1289 			if (!hidden)
1290 				hidden = rb_find_bss(rdev, tmp,
1291 						     BSS_CMP_HIDE_NUL);
1292 			if (hidden) {
1293 				new->pub.hidden_beacon_bss = &hidden->pub;
1294 				list_add(&new->hidden_list,
1295 					 &hidden->hidden_list);
1296 				hidden->refcount++;
1297 
1298 				ies = (void *)rcu_access_pointer(new->pub.beacon_ies);
1299 				rcu_assign_pointer(new->pub.beacon_ies,
1300 						   hidden->pub.beacon_ies);
1301 				if (ies)
1302 					kfree_rcu(ies, rcu_head);
1303 			}
1304 		} else {
1305 			/*
1306 			 * Ok so we found a beacon, and don't have an entry. If
1307 			 * it's a beacon with hidden SSID, we might be in for an
1308 			 * expensive search for any probe responses that should
1309 			 * be grouped with this beacon for updates ...
1310 			 */
1311 			if (!cfg80211_combine_bsses(rdev, new)) {
1312 				bss_ref_put(rdev, new);
1313 				goto drop;
1314 			}
1315 		}
1316 
1317 		if (rdev->bss_entries >= bss_entries_limit &&
1318 		    !cfg80211_bss_expire_oldest(rdev)) {
1319 			bss_ref_put(rdev, new);
1320 			goto drop;
1321 		}
1322 
1323 		/* This must be before the call to bss_ref_get */
1324 		if (tmp->pub.transmitted_bss) {
1325 			struct cfg80211_internal_bss *pbss =
1326 				container_of(tmp->pub.transmitted_bss,
1327 					     struct cfg80211_internal_bss,
1328 					     pub);
1329 
1330 			new->pub.transmitted_bss = tmp->pub.transmitted_bss;
1331 			bss_ref_get(rdev, pbss);
1332 		}
1333 
1334 		list_add_tail(&new->list, &rdev->bss_list);
1335 		rdev->bss_entries++;
1336 		rb_insert_bss(rdev, new);
1337 		found = new;
1338 	}
1339 
1340 	rdev->bss_generation++;
1341 	bss_ref_get(rdev, found);
1342 	spin_unlock_bh(&rdev->bss_lock);
1343 
1344 	return found;
1345  drop:
1346 	spin_unlock_bh(&rdev->bss_lock);
1347 	return NULL;
1348 }
1349 
1350 /*
1351  * Update RX channel information based on the available frame payload
1352  * information. This is mainly for the 2.4 GHz band where frames can be received
1353  * from neighboring channels and the Beacon frames use the DSSS Parameter Set
1354  * element to indicate the current (transmitting) channel, but this might also
1355  * be needed on other bands if RX frequency does not match with the actual
1356  * operating channel of a BSS.
1357  */
1358 static struct ieee80211_channel *
cfg80211_get_bss_channel(struct wiphy * wiphy,const u8 * ie,size_t ielen,struct ieee80211_channel * channel,enum nl80211_bss_scan_width scan_width)1359 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
1360 			 struct ieee80211_channel *channel,
1361 			 enum nl80211_bss_scan_width scan_width)
1362 {
1363 	const u8 *tmp;
1364 	u32 freq;
1365 	int channel_number = -1;
1366 	struct ieee80211_channel *alt_channel;
1367 
1368 	tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
1369 	if (tmp && tmp[1] == 1) {
1370 		channel_number = tmp[2];
1371 	} else {
1372 		tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
1373 		if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
1374 			struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
1375 
1376 			channel_number = htop->primary_chan;
1377 		}
1378 	}
1379 
1380 	if (channel_number < 0) {
1381 		/* No channel information in frame payload */
1382 		return channel;
1383 	}
1384 
1385 	freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
1386 	alt_channel = ieee80211_get_channel_khz(wiphy, freq);
1387 	if (!alt_channel) {
1388 		if (channel->band == NL80211_BAND_2GHZ) {
1389 			/*
1390 			 * Better not allow unexpected channels when that could
1391 			 * be going beyond the 1-11 range (e.g., discovering
1392 			 * BSS on channel 12 when radio is configured for
1393 			 * channel 11.
1394 			 */
1395 			return NULL;
1396 		}
1397 
1398 		/* No match for the payload channel number - ignore it */
1399 		return channel;
1400 	}
1401 
1402 	if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
1403 	    scan_width == NL80211_BSS_CHAN_WIDTH_5) {
1404 		/*
1405 		 * Ignore channel number in 5 and 10 MHz channels where there
1406 		 * may not be an n:1 or 1:n mapping between frequencies and
1407 		 * channel numbers.
1408 		 */
1409 		return channel;
1410 	}
1411 
1412 	/*
1413 	 * Use the channel determined through the payload channel number
1414 	 * instead of the RX channel reported by the driver.
1415 	 */
1416 	if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
1417 		return NULL;
1418 	return alt_channel;
1419 }
1420 
1421 /* Returned bss is reference counted and must be cleaned up appropriately. */
1422 static struct cfg80211_bss *
cfg80211_inform_single_bss_data(struct wiphy * wiphy,struct cfg80211_inform_bss * data,enum cfg80211_bss_frame_type ftype,const u8 * bssid,u64 tsf,u16 capability,u16 beacon_interval,const u8 * ie,size_t ielen,struct cfg80211_non_tx_bss * non_tx_data,gfp_t gfp)1423 cfg80211_inform_single_bss_data(struct wiphy *wiphy,
1424 				struct cfg80211_inform_bss *data,
1425 				enum cfg80211_bss_frame_type ftype,
1426 				const u8 *bssid, u64 tsf, u16 capability,
1427 				u16 beacon_interval, const u8 *ie, size_t ielen,
1428 				struct cfg80211_non_tx_bss *non_tx_data,
1429 				gfp_t gfp)
1430 {
1431 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1432 	struct cfg80211_bss_ies *ies;
1433 	struct ieee80211_channel *channel;
1434 	struct cfg80211_internal_bss tmp = {}, *res;
1435 	int bss_type;
1436 	bool signal_valid;
1437 	unsigned long ts;
1438 
1439 	if (WARN_ON(!wiphy))
1440 		return NULL;
1441 
1442 	if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1443 		    (data->signal < 0 || data->signal > 100)))
1444 		return NULL;
1445 
1446 	channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
1447 					   data->scan_width);
1448 	if (!channel)
1449 		return NULL;
1450 
1451 	memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1452 	tmp.pub.channel = channel;
1453 	tmp.pub.scan_width = data->scan_width;
1454 	tmp.pub.signal = data->signal;
1455 	tmp.pub.beacon_interval = beacon_interval;
1456 	tmp.pub.capability = capability;
1457 	tmp.ts_boottime = data->boottime_ns;
1458 	if (non_tx_data) {
1459 		tmp.pub.transmitted_bss = non_tx_data->tx_bss;
1460 		ts = bss_from_pub(non_tx_data->tx_bss)->ts;
1461 		tmp.pub.bssid_index = non_tx_data->bssid_index;
1462 		tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
1463 	} else {
1464 		ts = jiffies;
1465 	}
1466 
1467 	/*
1468 	 * If we do not know here whether the IEs are from a Beacon or Probe
1469 	 * Response frame, we need to pick one of the options and only use it
1470 	 * with the driver that does not provide the full Beacon/Probe Response
1471 	 * frame. Use Beacon frame pointer to avoid indicating that this should
1472 	 * override the IEs pointer should we have received an earlier
1473 	 * indication of Probe Response data.
1474 	 */
1475 	ies = kzalloc(sizeof(*ies) + ielen, gfp);
1476 	if (!ies)
1477 		return NULL;
1478 	ies->len = ielen;
1479 	ies->tsf = tsf;
1480 	ies->from_beacon = false;
1481 	memcpy(ies->data, ie, ielen);
1482 
1483 	switch (ftype) {
1484 	case CFG80211_BSS_FTYPE_BEACON:
1485 		ies->from_beacon = true;
1486 		/* fall through */
1487 	case CFG80211_BSS_FTYPE_UNKNOWN:
1488 		rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1489 		break;
1490 	case CFG80211_BSS_FTYPE_PRESP:
1491 		rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1492 		break;
1493 	}
1494 	rcu_assign_pointer(tmp.pub.ies, ies);
1495 
1496 	signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
1497 		wiphy->max_adj_channel_rssi_comp;
1498 	res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid, ts);
1499 	if (!res)
1500 		return NULL;
1501 
1502 	if (channel->band == NL80211_BAND_60GHZ) {
1503 		bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1504 		if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1505 		    bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1506 			regulatory_hint_found_beacon(wiphy, channel, gfp);
1507 	} else {
1508 		if (res->pub.capability & WLAN_CAPABILITY_ESS)
1509 			regulatory_hint_found_beacon(wiphy, channel, gfp);
1510 	}
1511 
1512 	if (non_tx_data) {
1513 		/* this is a nontransmitting bss, we need to add it to
1514 		 * transmitting bss' list if it is not there
1515 		 */
1516 		spin_lock_bh(&rdev->bss_lock);
1517 		if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
1518 					       &res->pub)) {
1519 			if (__cfg80211_unlink_bss(rdev, res)) {
1520 				rdev->bss_generation++;
1521 				res = NULL;
1522 			}
1523 		}
1524 		spin_unlock_bh(&rdev->bss_lock);
1525 
1526 		if (!res)
1527 			return NULL;
1528 	}
1529 
1530 	trace_cfg80211_return_bss(&res->pub);
1531 	/* cfg80211_bss_update gives us a referenced result */
1532 	return &res->pub;
1533 }
1534 
1535 static const struct element
cfg80211_get_profile_continuation(const u8 * ie,size_t ielen,const struct element * mbssid_elem,const struct element * sub_elem)1536 *cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
1537 				   const struct element *mbssid_elem,
1538 				   const struct element *sub_elem)
1539 {
1540 	const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
1541 	const struct element *next_mbssid;
1542 	const struct element *next_sub;
1543 
1544 	next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
1545 					 mbssid_end,
1546 					 ielen - (mbssid_end - ie));
1547 
1548 	/*
1549 	 * If is is not the last subelement in current MBSSID IE or there isn't
1550 	 * a next MBSSID IE - profile is complete.
1551 	*/
1552 	if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
1553 	    !next_mbssid)
1554 		return NULL;
1555 
1556 	/* For any length error, just return NULL */
1557 
1558 	if (next_mbssid->datalen < 4)
1559 		return NULL;
1560 
1561 	next_sub = (void *)&next_mbssid->data[1];
1562 
1563 	if (next_mbssid->data + next_mbssid->datalen <
1564 	    next_sub->data + next_sub->datalen)
1565 		return NULL;
1566 
1567 	if (next_sub->id != 0 || next_sub->datalen < 2)
1568 		return NULL;
1569 
1570 	/*
1571 	 * Check if the first element in the next sub element is a start
1572 	 * of a new profile
1573 	 */
1574 	return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
1575 	       NULL : next_mbssid;
1576 }
1577 
cfg80211_merge_profile(const u8 * ie,size_t ielen,const struct element * mbssid_elem,const struct element * sub_elem,u8 * merged_ie,size_t max_copy_len)1578 size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
1579 			      const struct element *mbssid_elem,
1580 			      const struct element *sub_elem,
1581 			      u8 *merged_ie, size_t max_copy_len)
1582 {
1583 	size_t copied_len = sub_elem->datalen;
1584 	const struct element *next_mbssid;
1585 
1586 	if (sub_elem->datalen > max_copy_len)
1587 		return 0;
1588 
1589 	memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
1590 
1591 	while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
1592 								mbssid_elem,
1593 								sub_elem))) {
1594 		const struct element *next_sub = (void *)&next_mbssid->data[1];
1595 
1596 		if (copied_len + next_sub->datalen > max_copy_len)
1597 			break;
1598 		memcpy(merged_ie + copied_len, next_sub->data,
1599 		       next_sub->datalen);
1600 		copied_len += next_sub->datalen;
1601 	}
1602 
1603 	return copied_len;
1604 }
1605 EXPORT_SYMBOL(cfg80211_merge_profile);
1606 
cfg80211_parse_mbssid_data(struct wiphy * wiphy,struct cfg80211_inform_bss * data,enum cfg80211_bss_frame_type ftype,const u8 * bssid,u64 tsf,u16 beacon_interval,const u8 * ie,size_t ielen,struct cfg80211_non_tx_bss * non_tx_data,gfp_t gfp)1607 static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
1608 				       struct cfg80211_inform_bss *data,
1609 				       enum cfg80211_bss_frame_type ftype,
1610 				       const u8 *bssid, u64 tsf,
1611 				       u16 beacon_interval, const u8 *ie,
1612 				       size_t ielen,
1613 				       struct cfg80211_non_tx_bss *non_tx_data,
1614 				       gfp_t gfp)
1615 {
1616 	const u8 *mbssid_index_ie;
1617 	const struct element *elem, *sub;
1618 	size_t new_ie_len;
1619 	u8 new_bssid[ETH_ALEN];
1620 	u8 *new_ie, *profile;
1621 	u64 seen_indices = 0;
1622 	u16 capability;
1623 	struct cfg80211_bss *bss;
1624 
1625 	if (!non_tx_data)
1626 		return;
1627 	if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1628 		return;
1629 	if (!wiphy->support_mbssid)
1630 		return;
1631 	if (wiphy->support_only_he_mbssid &&
1632 	    !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1633 		return;
1634 
1635 	new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
1636 	if (!new_ie)
1637 		return;
1638 
1639 	profile = kmalloc(ielen, gfp);
1640 	if (!profile)
1641 		goto out;
1642 
1643 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
1644 		if (elem->datalen < 4)
1645 			continue;
1646 		if (elem->data[0] < 1 || (int)elem->data[0] > 8)
1647 			continue;
1648 		for_each_element(sub, elem->data + 1, elem->datalen - 1) {
1649 			u8 profile_len;
1650 
1651 			if (sub->id != 0 || sub->datalen < 4) {
1652 				/* not a valid BSS profile */
1653 				continue;
1654 			}
1655 
1656 			if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1657 			    sub->data[1] != 2) {
1658 				/* The first element within the Nontransmitted
1659 				 * BSSID Profile is not the Nontransmitted
1660 				 * BSSID Capability element.
1661 				 */
1662 				continue;
1663 			}
1664 
1665 			memset(profile, 0, ielen);
1666 			profile_len = cfg80211_merge_profile(ie, ielen,
1667 							     elem,
1668 							     sub,
1669 							     profile,
1670 							     ielen);
1671 
1672 			/* found a Nontransmitted BSSID Profile */
1673 			mbssid_index_ie = cfg80211_find_ie
1674 				(WLAN_EID_MULTI_BSSID_IDX,
1675 				 profile, profile_len);
1676 			if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
1677 			    mbssid_index_ie[2] == 0 ||
1678 			    mbssid_index_ie[2] > 46) {
1679 				/* No valid Multiple BSSID-Index element */
1680 				continue;
1681 			}
1682 
1683 			if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
1684 				/* We don't support legacy split of a profile */
1685 				net_dbg_ratelimited("Partial info for BSSID index %d\n",
1686 						    mbssid_index_ie[2]);
1687 
1688 			seen_indices |= BIT_ULL(mbssid_index_ie[2]);
1689 
1690 			non_tx_data->bssid_index = mbssid_index_ie[2];
1691 			non_tx_data->max_bssid_indicator = elem->data[0];
1692 
1693 			cfg80211_gen_new_bssid(bssid,
1694 					       non_tx_data->max_bssid_indicator,
1695 					       non_tx_data->bssid_index,
1696 					       new_bssid);
1697 			memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
1698 			new_ie_len = cfg80211_gen_new_ie(ie, ielen,
1699 							 profile,
1700 							 profile_len, new_ie,
1701 							 IEEE80211_MAX_DATA_LEN);
1702 			if (!new_ie_len)
1703 				continue;
1704 
1705 			capability = get_unaligned_le16(profile + 2);
1706 			bss = cfg80211_inform_single_bss_data(wiphy, data,
1707 							      ftype,
1708 							      new_bssid, tsf,
1709 							      capability,
1710 							      beacon_interval,
1711 							      new_ie,
1712 							      new_ie_len,
1713 							      non_tx_data,
1714 							      gfp);
1715 			if (!bss)
1716 				break;
1717 			cfg80211_put_bss(wiphy, bss);
1718 		}
1719 	}
1720 
1721 out:
1722 	kfree(new_ie);
1723 	kfree(profile);
1724 }
1725 
1726 struct cfg80211_bss *
cfg80211_inform_bss_data(struct wiphy * wiphy,struct cfg80211_inform_bss * data,enum cfg80211_bss_frame_type ftype,const u8 * bssid,u64 tsf,u16 capability,u16 beacon_interval,const u8 * ie,size_t ielen,gfp_t gfp)1727 cfg80211_inform_bss_data(struct wiphy *wiphy,
1728 			 struct cfg80211_inform_bss *data,
1729 			 enum cfg80211_bss_frame_type ftype,
1730 			 const u8 *bssid, u64 tsf, u16 capability,
1731 			 u16 beacon_interval, const u8 *ie, size_t ielen,
1732 			 gfp_t gfp)
1733 {
1734 	struct cfg80211_bss *res;
1735 	struct cfg80211_non_tx_bss non_tx_data;
1736 
1737 	res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
1738 					      capability, beacon_interval, ie,
1739 					      ielen, NULL, gfp);
1740 	if (!res)
1741 		return NULL;
1742 	non_tx_data.tx_bss = res;
1743 	cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
1744 				   beacon_interval, ie, ielen, &non_tx_data,
1745 				   gfp);
1746 	return res;
1747 }
1748 EXPORT_SYMBOL(cfg80211_inform_bss_data);
1749 
1750 static void
cfg80211_parse_mbssid_frame_data(struct wiphy * wiphy,struct cfg80211_inform_bss * data,struct ieee80211_mgmt * mgmt,size_t len,struct cfg80211_non_tx_bss * non_tx_data,gfp_t gfp)1751 cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
1752 				 struct cfg80211_inform_bss *data,
1753 				 struct ieee80211_mgmt *mgmt, size_t len,
1754 				 struct cfg80211_non_tx_bss *non_tx_data,
1755 				 gfp_t gfp)
1756 {
1757 	enum cfg80211_bss_frame_type ftype;
1758 	const u8 *ie = mgmt->u.probe_resp.variable;
1759 	size_t ielen = len - offsetof(struct ieee80211_mgmt,
1760 				      u.probe_resp.variable);
1761 
1762 	ftype = ieee80211_is_beacon(mgmt->frame_control) ?
1763 		CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
1764 
1765 	cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
1766 				   le64_to_cpu(mgmt->u.probe_resp.timestamp),
1767 				   le16_to_cpu(mgmt->u.probe_resp.beacon_int),
1768 				   ie, ielen, non_tx_data, gfp);
1769 }
1770 
1771 static void
cfg80211_update_notlisted_nontrans(struct wiphy * wiphy,struct cfg80211_bss * nontrans_bss,struct ieee80211_mgmt * mgmt,size_t len)1772 cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
1773 				   struct cfg80211_bss *nontrans_bss,
1774 				   struct ieee80211_mgmt *mgmt, size_t len)
1775 {
1776 	u8 *ie, *new_ie, *pos;
1777 	const u8 *nontrans_ssid, *trans_ssid, *mbssid;
1778 	size_t ielen = len - offsetof(struct ieee80211_mgmt,
1779 				      u.probe_resp.variable);
1780 	size_t new_ie_len;
1781 	struct cfg80211_bss_ies *new_ies;
1782 	const struct cfg80211_bss_ies *old;
1783 	size_t cpy_len;
1784 
1785 	lockdep_assert_held(&wiphy_to_rdev(wiphy)->bss_lock);
1786 
1787 	ie = mgmt->u.probe_resp.variable;
1788 
1789 	new_ie_len = ielen;
1790 	trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
1791 	if (!trans_ssid)
1792 		return;
1793 	new_ie_len -= trans_ssid[1];
1794 	mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
1795 	/*
1796 	 * It's not valid to have the MBSSID element before SSID
1797 	 * ignore if that happens - the code below assumes it is
1798 	 * after (while copying things inbetween).
1799 	 */
1800 	if (!mbssid || mbssid < trans_ssid)
1801 		return;
1802 	new_ie_len -= mbssid[1];
1803 
1804 	nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
1805 	if (!nontrans_ssid)
1806 		return;
1807 
1808 	new_ie_len += nontrans_ssid[1];
1809 
1810 	/* generate new ie for nontrans BSS
1811 	 * 1. replace SSID with nontrans BSS' SSID
1812 	 * 2. skip MBSSID IE
1813 	 */
1814 	new_ie = kzalloc(new_ie_len, GFP_ATOMIC);
1815 	if (!new_ie)
1816 		return;
1817 
1818 	new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, GFP_ATOMIC);
1819 	if (!new_ies)
1820 		goto out_free;
1821 
1822 	pos = new_ie;
1823 
1824 	/* copy the nontransmitted SSID */
1825 	cpy_len = nontrans_ssid[1] + 2;
1826 	memcpy(pos, nontrans_ssid, cpy_len);
1827 	pos += cpy_len;
1828 	/* copy the IEs between SSID and MBSSID */
1829 	cpy_len = trans_ssid[1] + 2;
1830 	memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
1831 	pos += (mbssid - (trans_ssid + cpy_len));
1832 	/* copy the IEs after MBSSID */
1833 	cpy_len = mbssid[1] + 2;
1834 	memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
1835 
1836 	/* update ie */
1837 	new_ies->len = new_ie_len;
1838 	new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1839 	new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1840 	memcpy(new_ies->data, new_ie, new_ie_len);
1841 	if (ieee80211_is_probe_resp(mgmt->frame_control)) {
1842 		old = rcu_access_pointer(nontrans_bss->proberesp_ies);
1843 		rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
1844 		rcu_assign_pointer(nontrans_bss->ies, new_ies);
1845 		if (old)
1846 			kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1847 	} else {
1848 		old = rcu_access_pointer(nontrans_bss->beacon_ies);
1849 		rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
1850 		cfg80211_update_hidden_bsses(bss_from_pub(nontrans_bss),
1851 					     new_ies, old);
1852 		rcu_assign_pointer(nontrans_bss->ies, new_ies);
1853 		if (old)
1854 			kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
1855 	}
1856 
1857 out_free:
1858 	kfree(new_ie);
1859 }
1860 
1861 /* cfg80211_inform_bss_width_frame helper */
1862 static struct cfg80211_bss *
cfg80211_inform_single_bss_frame_data(struct wiphy * wiphy,struct cfg80211_inform_bss * data,struct ieee80211_mgmt * mgmt,size_t len,gfp_t gfp)1863 cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
1864 				      struct cfg80211_inform_bss *data,
1865 				      struct ieee80211_mgmt *mgmt, size_t len,
1866 				      gfp_t gfp)
1867 {
1868 	struct cfg80211_internal_bss tmp = {}, *res;
1869 	struct cfg80211_bss_ies *ies;
1870 	struct ieee80211_channel *channel;
1871 	bool signal_valid;
1872 	size_t ielen = len - offsetof(struct ieee80211_mgmt,
1873 				      u.probe_resp.variable);
1874 	int bss_type;
1875 
1876 	BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
1877 			offsetof(struct ieee80211_mgmt, u.beacon.variable));
1878 
1879 	trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
1880 
1881 	if (WARN_ON(!mgmt))
1882 		return NULL;
1883 
1884 	if (WARN_ON(!wiphy))
1885 		return NULL;
1886 
1887 	if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1888 		    (data->signal < 0 || data->signal > 100)))
1889 		return NULL;
1890 
1891 	if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
1892 		return NULL;
1893 
1894 	channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
1895 					   ielen, data->chan, data->scan_width);
1896 	if (!channel)
1897 		return NULL;
1898 
1899 	ies = kzalloc(sizeof(*ies) + ielen, gfp);
1900 	if (!ies)
1901 		return NULL;
1902 	ies->len = ielen;
1903 	ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1904 	ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1905 	memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
1906 
1907 	if (ieee80211_is_probe_resp(mgmt->frame_control))
1908 		rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1909 	else
1910 		rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1911 	rcu_assign_pointer(tmp.pub.ies, ies);
1912 
1913 	memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
1914 	tmp.pub.channel = channel;
1915 	tmp.pub.scan_width = data->scan_width;
1916 	tmp.pub.signal = data->signal;
1917 	tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
1918 	tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
1919 	tmp.ts_boottime = data->boottime_ns;
1920 	tmp.parent_tsf = data->parent_tsf;
1921 	tmp.pub.chains = data->chains;
1922 	memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
1923 	ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
1924 
1925 	signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
1926 		wiphy->max_adj_channel_rssi_comp;
1927 	res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid,
1928 				  jiffies);
1929 	if (!res)
1930 		return NULL;
1931 
1932 	if (channel->band == NL80211_BAND_60GHZ) {
1933 		bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1934 		if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1935 		    bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1936 			regulatory_hint_found_beacon(wiphy, channel, gfp);
1937 	} else {
1938 		if (res->pub.capability & WLAN_CAPABILITY_ESS)
1939 			regulatory_hint_found_beacon(wiphy, channel, gfp);
1940 	}
1941 
1942 	trace_cfg80211_return_bss(&res->pub);
1943 	/* cfg80211_bss_update gives us a referenced result */
1944 	return &res->pub;
1945 }
1946 
1947 struct cfg80211_bss *
cfg80211_inform_bss_frame_data(struct wiphy * wiphy,struct cfg80211_inform_bss * data,struct ieee80211_mgmt * mgmt,size_t len,gfp_t gfp)1948 cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1949 			       struct cfg80211_inform_bss *data,
1950 			       struct ieee80211_mgmt *mgmt, size_t len,
1951 			       gfp_t gfp)
1952 {
1953 	struct cfg80211_bss *res, *tmp_bss;
1954 	const u8 *ie = mgmt->u.probe_resp.variable;
1955 	const struct cfg80211_bss_ies *ies1, *ies2;
1956 	size_t ielen = len - offsetof(struct ieee80211_mgmt,
1957 				      u.probe_resp.variable);
1958 	struct cfg80211_non_tx_bss non_tx_data;
1959 
1960 	res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
1961 						    len, gfp);
1962 	if (!res || !wiphy->support_mbssid ||
1963 	    !cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
1964 		return res;
1965 	if (wiphy->support_only_he_mbssid &&
1966 	    !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
1967 		return res;
1968 
1969 	non_tx_data.tx_bss = res;
1970 	/* process each non-transmitting bss */
1971 	cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
1972 					 &non_tx_data, gfp);
1973 
1974 	spin_lock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
1975 
1976 	/* check if the res has other nontransmitting bss which is not
1977 	 * in MBSSID IE
1978 	 */
1979 	ies1 = rcu_access_pointer(res->ies);
1980 
1981 	/* go through nontrans_list, if the timestamp of the BSS is
1982 	 * earlier than the timestamp of the transmitting BSS then
1983 	 * update it
1984 	 */
1985 	list_for_each_entry(tmp_bss, &res->nontrans_list,
1986 			    nontrans_list) {
1987 		ies2 = rcu_access_pointer(tmp_bss->ies);
1988 		if (ies2->tsf < ies1->tsf)
1989 			cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
1990 							   mgmt, len);
1991 	}
1992 	spin_unlock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
1993 
1994 	return res;
1995 }
1996 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
1997 
cfg80211_ref_bss(struct wiphy * wiphy,struct cfg80211_bss * pub)1998 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1999 {
2000 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2001 	struct cfg80211_internal_bss *bss;
2002 
2003 	if (!pub)
2004 		return;
2005 
2006 	bss = container_of(pub, struct cfg80211_internal_bss, pub);
2007 
2008 	spin_lock_bh(&rdev->bss_lock);
2009 	bss_ref_get(rdev, bss);
2010 	spin_unlock_bh(&rdev->bss_lock);
2011 }
2012 EXPORT_SYMBOL(cfg80211_ref_bss);
2013 
cfg80211_put_bss(struct wiphy * wiphy,struct cfg80211_bss * pub)2014 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2015 {
2016 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2017 	struct cfg80211_internal_bss *bss;
2018 
2019 	if (!pub)
2020 		return;
2021 
2022 	bss = container_of(pub, struct cfg80211_internal_bss, pub);
2023 
2024 	spin_lock_bh(&rdev->bss_lock);
2025 	bss_ref_put(rdev, bss);
2026 	spin_unlock_bh(&rdev->bss_lock);
2027 }
2028 EXPORT_SYMBOL(cfg80211_put_bss);
2029 
cfg80211_unlink_bss(struct wiphy * wiphy,struct cfg80211_bss * pub)2030 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2031 {
2032 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2033 	struct cfg80211_internal_bss *bss, *tmp1;
2034 	struct cfg80211_bss *nontrans_bss, *tmp;
2035 
2036 	if (WARN_ON(!pub))
2037 		return;
2038 
2039 	bss = container_of(pub, struct cfg80211_internal_bss, pub);
2040 
2041 	spin_lock_bh(&rdev->bss_lock);
2042 	if (list_empty(&bss->list))
2043 		goto out;
2044 
2045 	list_for_each_entry_safe(nontrans_bss, tmp,
2046 				 &pub->nontrans_list,
2047 				 nontrans_list) {
2048 		tmp1 = container_of(nontrans_bss,
2049 				    struct cfg80211_internal_bss, pub);
2050 		if (__cfg80211_unlink_bss(rdev, tmp1))
2051 			rdev->bss_generation++;
2052 	}
2053 
2054 	if (__cfg80211_unlink_bss(rdev, bss))
2055 		rdev->bss_generation++;
2056 out:
2057 	spin_unlock_bh(&rdev->bss_lock);
2058 }
2059 EXPORT_SYMBOL(cfg80211_unlink_bss);
2060 
cfg80211_bss_iter(struct wiphy * wiphy,struct cfg80211_chan_def * chandef,void (* iter)(struct wiphy * wiphy,struct cfg80211_bss * bss,void * data),void * iter_data)2061 void cfg80211_bss_iter(struct wiphy *wiphy,
2062 		       struct cfg80211_chan_def *chandef,
2063 		       void (*iter)(struct wiphy *wiphy,
2064 				    struct cfg80211_bss *bss,
2065 				    void *data),
2066 		       void *iter_data)
2067 {
2068 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2069 	struct cfg80211_internal_bss *bss;
2070 
2071 	spin_lock_bh(&rdev->bss_lock);
2072 
2073 	list_for_each_entry(bss, &rdev->bss_list, list) {
2074 		if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel))
2075 			iter(wiphy, &bss->pub, iter_data);
2076 	}
2077 
2078 	spin_unlock_bh(&rdev->bss_lock);
2079 }
2080 EXPORT_SYMBOL(cfg80211_bss_iter);
2081 
cfg80211_update_assoc_bss_entry(struct wireless_dev * wdev,struct ieee80211_channel * chan)2082 void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
2083 				     struct ieee80211_channel *chan)
2084 {
2085 	struct wiphy *wiphy = wdev->wiphy;
2086 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2087 	struct cfg80211_internal_bss *cbss = wdev->current_bss;
2088 	struct cfg80211_internal_bss *new = NULL;
2089 	struct cfg80211_internal_bss *bss;
2090 	struct cfg80211_bss *nontrans_bss;
2091 	struct cfg80211_bss *tmp;
2092 
2093 	spin_lock_bh(&rdev->bss_lock);
2094 
2095 	/*
2096 	 * Some APs use CSA also for bandwidth changes, i.e., without actually
2097 	 * changing the control channel, so no need to update in such a case.
2098 	 */
2099 	if (cbss->pub.channel == chan)
2100 		goto done;
2101 
2102 	/* use transmitting bss */
2103 	if (cbss->pub.transmitted_bss)
2104 		cbss = container_of(cbss->pub.transmitted_bss,
2105 				    struct cfg80211_internal_bss,
2106 				    pub);
2107 
2108 	cbss->pub.channel = chan;
2109 
2110 	list_for_each_entry(bss, &rdev->bss_list, list) {
2111 		if (!cfg80211_bss_type_match(bss->pub.capability,
2112 					     bss->pub.channel->band,
2113 					     wdev->conn_bss_type))
2114 			continue;
2115 
2116 		if (bss == cbss)
2117 			continue;
2118 
2119 		if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
2120 			new = bss;
2121 			break;
2122 		}
2123 	}
2124 
2125 	if (new) {
2126 		/* to save time, update IEs for transmitting bss only */
2127 		if (cfg80211_update_known_bss(rdev, cbss, new, false)) {
2128 			new->pub.proberesp_ies = NULL;
2129 			new->pub.beacon_ies = NULL;
2130 		}
2131 
2132 		list_for_each_entry_safe(nontrans_bss, tmp,
2133 					 &new->pub.nontrans_list,
2134 					 nontrans_list) {
2135 			bss = container_of(nontrans_bss,
2136 					   struct cfg80211_internal_bss, pub);
2137 			if (__cfg80211_unlink_bss(rdev, bss))
2138 				rdev->bss_generation++;
2139 		}
2140 
2141 		WARN_ON(atomic_read(&new->hold));
2142 		if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
2143 			rdev->bss_generation++;
2144 	}
2145 
2146 	rb_erase(&cbss->rbn, &rdev->bss_tree);
2147 	rb_insert_bss(rdev, cbss);
2148 	rdev->bss_generation++;
2149 
2150 	list_for_each_entry_safe(nontrans_bss, tmp,
2151 				 &cbss->pub.nontrans_list,
2152 				 nontrans_list) {
2153 		bss = container_of(nontrans_bss,
2154 				   struct cfg80211_internal_bss, pub);
2155 		bss->pub.channel = chan;
2156 		rb_erase(&bss->rbn, &rdev->bss_tree);
2157 		rb_insert_bss(rdev, bss);
2158 		rdev->bss_generation++;
2159 	}
2160 
2161 done:
2162 	spin_unlock_bh(&rdev->bss_lock);
2163 }
2164 
2165 #ifdef CONFIG_CFG80211_WEXT
2166 static struct cfg80211_registered_device *
cfg80211_get_dev_from_ifindex(struct net * net,int ifindex)2167 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
2168 {
2169 	struct cfg80211_registered_device *rdev;
2170 	struct net_device *dev;
2171 
2172 	ASSERT_RTNL();
2173 
2174 	dev = dev_get_by_index(net, ifindex);
2175 	if (!dev)
2176 		return ERR_PTR(-ENODEV);
2177 	if (dev->ieee80211_ptr)
2178 		rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
2179 	else
2180 		rdev = ERR_PTR(-ENODEV);
2181 	dev_put(dev);
2182 	return rdev;
2183 }
2184 
cfg80211_wext_siwscan(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)2185 int cfg80211_wext_siwscan(struct net_device *dev,
2186 			  struct iw_request_info *info,
2187 			  union iwreq_data *wrqu, char *extra)
2188 {
2189 	struct cfg80211_registered_device *rdev;
2190 	struct wiphy *wiphy;
2191 	struct iw_scan_req *wreq = NULL;
2192 	struct cfg80211_scan_request *creq = NULL;
2193 	int i, err, n_channels = 0;
2194 	enum nl80211_band band;
2195 
2196 	if (!netif_running(dev))
2197 		return -ENETDOWN;
2198 
2199 	if (wrqu->data.length == sizeof(struct iw_scan_req))
2200 		wreq = (struct iw_scan_req *)extra;
2201 
2202 	rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2203 
2204 	if (IS_ERR(rdev))
2205 		return PTR_ERR(rdev);
2206 
2207 	if (rdev->scan_req || rdev->scan_msg) {
2208 		err = -EBUSY;
2209 		goto out;
2210 	}
2211 
2212 	wiphy = &rdev->wiphy;
2213 
2214 	/* Determine number of channels, needed to allocate creq */
2215 	if (wreq && wreq->num_channels)
2216 		n_channels = wreq->num_channels;
2217 	else
2218 		n_channels = ieee80211_get_num_supported_channels(wiphy);
2219 
2220 	creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
2221 		       n_channels * sizeof(void *),
2222 		       GFP_ATOMIC);
2223 	if (!creq) {
2224 		err = -ENOMEM;
2225 		goto out;
2226 	}
2227 
2228 	creq->wiphy = wiphy;
2229 	creq->wdev = dev->ieee80211_ptr;
2230 	/* SSIDs come after channels */
2231 	creq->ssids = (void *)&creq->channels[n_channels];
2232 	creq->n_channels = n_channels;
2233 	creq->n_ssids = 1;
2234 	creq->scan_start = jiffies;
2235 
2236 	/* translate "Scan on frequencies" request */
2237 	i = 0;
2238 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
2239 		int j;
2240 
2241 		if (!wiphy->bands[band])
2242 			continue;
2243 
2244 		for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2245 			/* ignore disabled channels */
2246 			if (wiphy->bands[band]->channels[j].flags &
2247 						IEEE80211_CHAN_DISABLED)
2248 				continue;
2249 
2250 			/* If we have a wireless request structure and the
2251 			 * wireless request specifies frequencies, then search
2252 			 * for the matching hardware channel.
2253 			 */
2254 			if (wreq && wreq->num_channels) {
2255 				int k;
2256 				int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
2257 				for (k = 0; k < wreq->num_channels; k++) {
2258 					struct iw_freq *freq =
2259 						&wreq->channel_list[k];
2260 					int wext_freq =
2261 						cfg80211_wext_freq(freq);
2262 
2263 					if (wext_freq == wiphy_freq)
2264 						goto wext_freq_found;
2265 				}
2266 				goto wext_freq_not_found;
2267 			}
2268 
2269 		wext_freq_found:
2270 			creq->channels[i] = &wiphy->bands[band]->channels[j];
2271 			i++;
2272 		wext_freq_not_found: ;
2273 		}
2274 	}
2275 	/* No channels found? */
2276 	if (!i) {
2277 		err = -EINVAL;
2278 		goto out;
2279 	}
2280 
2281 	/* Set real number of channels specified in creq->channels[] */
2282 	creq->n_channels = i;
2283 
2284 	/* translate "Scan for SSID" request */
2285 	if (wreq) {
2286 		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
2287 			if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
2288 				err = -EINVAL;
2289 				goto out;
2290 			}
2291 			memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
2292 			creq->ssids[0].ssid_len = wreq->essid_len;
2293 		}
2294 		if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
2295 			creq->n_ssids = 0;
2296 	}
2297 
2298 	for (i = 0; i < NUM_NL80211_BANDS; i++)
2299 		if (wiphy->bands[i])
2300 			creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
2301 
2302 	eth_broadcast_addr(creq->bssid);
2303 
2304 	rdev->scan_req = creq;
2305 	err = rdev_scan(rdev, creq);
2306 	if (err) {
2307 		rdev->scan_req = NULL;
2308 		/* creq will be freed below */
2309 	} else {
2310 		nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
2311 		/* creq now owned by driver */
2312 		creq = NULL;
2313 		dev_hold(dev);
2314 	}
2315  out:
2316 	kfree(creq);
2317 	return err;
2318 }
2319 EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
2320 
ieee80211_scan_add_ies(struct iw_request_info * info,const struct cfg80211_bss_ies * ies,char * current_ev,char * end_buf)2321 static char *ieee80211_scan_add_ies(struct iw_request_info *info,
2322 				    const struct cfg80211_bss_ies *ies,
2323 				    char *current_ev, char *end_buf)
2324 {
2325 	const u8 *pos, *end, *next;
2326 	struct iw_event iwe;
2327 
2328 	if (!ies)
2329 		return current_ev;
2330 
2331 	/*
2332 	 * If needed, fragment the IEs buffer (at IE boundaries) into short
2333 	 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
2334 	 */
2335 	pos = ies->data;
2336 	end = pos + ies->len;
2337 
2338 	while (end - pos > IW_GENERIC_IE_MAX) {
2339 		next = pos + 2 + pos[1];
2340 		while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
2341 			next = next + 2 + next[1];
2342 
2343 		memset(&iwe, 0, sizeof(iwe));
2344 		iwe.cmd = IWEVGENIE;
2345 		iwe.u.data.length = next - pos;
2346 		current_ev = iwe_stream_add_point_check(info, current_ev,
2347 							end_buf, &iwe,
2348 							(void *)pos);
2349 		if (IS_ERR(current_ev))
2350 			return current_ev;
2351 		pos = next;
2352 	}
2353 
2354 	if (end > pos) {
2355 		memset(&iwe, 0, sizeof(iwe));
2356 		iwe.cmd = IWEVGENIE;
2357 		iwe.u.data.length = end - pos;
2358 		current_ev = iwe_stream_add_point_check(info, current_ev,
2359 							end_buf, &iwe,
2360 							(void *)pos);
2361 		if (IS_ERR(current_ev))
2362 			return current_ev;
2363 	}
2364 
2365 	return current_ev;
2366 }
2367 
2368 static char *
ieee80211_bss(struct wiphy * wiphy,struct iw_request_info * info,struct cfg80211_internal_bss * bss,char * current_ev,char * end_buf)2369 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
2370 	      struct cfg80211_internal_bss *bss, char *current_ev,
2371 	      char *end_buf)
2372 {
2373 	const struct cfg80211_bss_ies *ies;
2374 	struct iw_event iwe;
2375 	const u8 *ie;
2376 	u8 buf[50];
2377 	u8 *cfg, *p, *tmp;
2378 	int rem, i, sig;
2379 	bool ismesh = false;
2380 
2381 	memset(&iwe, 0, sizeof(iwe));
2382 	iwe.cmd = SIOCGIWAP;
2383 	iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2384 	memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
2385 	current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2386 						IW_EV_ADDR_LEN);
2387 	if (IS_ERR(current_ev))
2388 		return current_ev;
2389 
2390 	memset(&iwe, 0, sizeof(iwe));
2391 	iwe.cmd = SIOCGIWFREQ;
2392 	iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
2393 	iwe.u.freq.e = 0;
2394 	current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2395 						IW_EV_FREQ_LEN);
2396 	if (IS_ERR(current_ev))
2397 		return current_ev;
2398 
2399 	memset(&iwe, 0, sizeof(iwe));
2400 	iwe.cmd = SIOCGIWFREQ;
2401 	iwe.u.freq.m = bss->pub.channel->center_freq;
2402 	iwe.u.freq.e = 6;
2403 	current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
2404 						IW_EV_FREQ_LEN);
2405 	if (IS_ERR(current_ev))
2406 		return current_ev;
2407 
2408 	if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2409 		memset(&iwe, 0, sizeof(iwe));
2410 		iwe.cmd = IWEVQUAL;
2411 		iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
2412 				     IW_QUAL_NOISE_INVALID |
2413 				     IW_QUAL_QUAL_UPDATED;
2414 		switch (wiphy->signal_type) {
2415 		case CFG80211_SIGNAL_TYPE_MBM:
2416 			sig = bss->pub.signal / 100;
2417 			iwe.u.qual.level = sig;
2418 			iwe.u.qual.updated |= IW_QUAL_DBM;
2419 			if (sig < -110)		/* rather bad */
2420 				sig = -110;
2421 			else if (sig > -40)	/* perfect */
2422 				sig = -40;
2423 			/* will give a range of 0 .. 70 */
2424 			iwe.u.qual.qual = sig + 110;
2425 			break;
2426 		case CFG80211_SIGNAL_TYPE_UNSPEC:
2427 			iwe.u.qual.level = bss->pub.signal;
2428 			/* will give range 0 .. 100 */
2429 			iwe.u.qual.qual = bss->pub.signal;
2430 			break;
2431 		default:
2432 			/* not reached */
2433 			break;
2434 		}
2435 		current_ev = iwe_stream_add_event_check(info, current_ev,
2436 							end_buf, &iwe,
2437 							IW_EV_QUAL_LEN);
2438 		if (IS_ERR(current_ev))
2439 			return current_ev;
2440 	}
2441 
2442 	memset(&iwe, 0, sizeof(iwe));
2443 	iwe.cmd = SIOCGIWENCODE;
2444 	if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
2445 		iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2446 	else
2447 		iwe.u.data.flags = IW_ENCODE_DISABLED;
2448 	iwe.u.data.length = 0;
2449 	current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2450 						&iwe, "");
2451 	if (IS_ERR(current_ev))
2452 		return current_ev;
2453 
2454 	rcu_read_lock();
2455 	ies = rcu_dereference(bss->pub.ies);
2456 	rem = ies->len;
2457 	ie = ies->data;
2458 
2459 	while (rem >= 2) {
2460 		/* invalid data */
2461 		if (ie[1] > rem - 2)
2462 			break;
2463 
2464 		switch (ie[0]) {
2465 		case WLAN_EID_SSID:
2466 			memset(&iwe, 0, sizeof(iwe));
2467 			iwe.cmd = SIOCGIWESSID;
2468 			iwe.u.data.length = ie[1];
2469 			iwe.u.data.flags = 1;
2470 			current_ev = iwe_stream_add_point_check(info,
2471 								current_ev,
2472 								end_buf, &iwe,
2473 								(u8 *)ie + 2);
2474 			if (IS_ERR(current_ev))
2475 				goto unlock;
2476 			break;
2477 		case WLAN_EID_MESH_ID:
2478 			memset(&iwe, 0, sizeof(iwe));
2479 			iwe.cmd = SIOCGIWESSID;
2480 			iwe.u.data.length = ie[1];
2481 			iwe.u.data.flags = 1;
2482 			current_ev = iwe_stream_add_point_check(info,
2483 								current_ev,
2484 								end_buf, &iwe,
2485 								(u8 *)ie + 2);
2486 			if (IS_ERR(current_ev))
2487 				goto unlock;
2488 			break;
2489 		case WLAN_EID_MESH_CONFIG:
2490 			ismesh = true;
2491 			if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
2492 				break;
2493 			cfg = (u8 *)ie + 2;
2494 			memset(&iwe, 0, sizeof(iwe));
2495 			iwe.cmd = IWEVCUSTOM;
2496 			sprintf(buf, "Mesh Network Path Selection Protocol ID: "
2497 				"0x%02X", cfg[0]);
2498 			iwe.u.data.length = strlen(buf);
2499 			current_ev = iwe_stream_add_point_check(info,
2500 								current_ev,
2501 								end_buf,
2502 								&iwe, buf);
2503 			if (IS_ERR(current_ev))
2504 				goto unlock;
2505 			sprintf(buf, "Path Selection Metric ID: 0x%02X",
2506 				cfg[1]);
2507 			iwe.u.data.length = strlen(buf);
2508 			current_ev = iwe_stream_add_point_check(info,
2509 								current_ev,
2510 								end_buf,
2511 								&iwe, buf);
2512 			if (IS_ERR(current_ev))
2513 				goto unlock;
2514 			sprintf(buf, "Congestion Control Mode ID: 0x%02X",
2515 				cfg[2]);
2516 			iwe.u.data.length = strlen(buf);
2517 			current_ev = iwe_stream_add_point_check(info,
2518 								current_ev,
2519 								end_buf,
2520 								&iwe, buf);
2521 			if (IS_ERR(current_ev))
2522 				goto unlock;
2523 			sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
2524 			iwe.u.data.length = strlen(buf);
2525 			current_ev = iwe_stream_add_point_check(info,
2526 								current_ev,
2527 								end_buf,
2528 								&iwe, buf);
2529 			if (IS_ERR(current_ev))
2530 				goto unlock;
2531 			sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
2532 			iwe.u.data.length = strlen(buf);
2533 			current_ev = iwe_stream_add_point_check(info,
2534 								current_ev,
2535 								end_buf,
2536 								&iwe, buf);
2537 			if (IS_ERR(current_ev))
2538 				goto unlock;
2539 			sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
2540 			iwe.u.data.length = strlen(buf);
2541 			current_ev = iwe_stream_add_point_check(info,
2542 								current_ev,
2543 								end_buf,
2544 								&iwe, buf);
2545 			if (IS_ERR(current_ev))
2546 				goto unlock;
2547 			sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
2548 			iwe.u.data.length = strlen(buf);
2549 			current_ev = iwe_stream_add_point_check(info,
2550 								current_ev,
2551 								end_buf,
2552 								&iwe, buf);
2553 			if (IS_ERR(current_ev))
2554 				goto unlock;
2555 			break;
2556 		case WLAN_EID_SUPP_RATES:
2557 		case WLAN_EID_EXT_SUPP_RATES:
2558 			/* display all supported rates in readable format */
2559 			p = current_ev + iwe_stream_lcp_len(info);
2560 
2561 			memset(&iwe, 0, sizeof(iwe));
2562 			iwe.cmd = SIOCGIWRATE;
2563 			/* Those two flags are ignored... */
2564 			iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2565 
2566 			for (i = 0; i < ie[1]; i++) {
2567 				iwe.u.bitrate.value =
2568 					((ie[i + 2] & 0x7f) * 500000);
2569 				tmp = p;
2570 				p = iwe_stream_add_value(info, current_ev, p,
2571 							 end_buf, &iwe,
2572 							 IW_EV_PARAM_LEN);
2573 				if (p == tmp) {
2574 					current_ev = ERR_PTR(-E2BIG);
2575 					goto unlock;
2576 				}
2577 			}
2578 			current_ev = p;
2579 			break;
2580 		}
2581 		rem -= ie[1] + 2;
2582 		ie += ie[1] + 2;
2583 	}
2584 
2585 	if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
2586 	    ismesh) {
2587 		memset(&iwe, 0, sizeof(iwe));
2588 		iwe.cmd = SIOCGIWMODE;
2589 		if (ismesh)
2590 			iwe.u.mode = IW_MODE_MESH;
2591 		else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
2592 			iwe.u.mode = IW_MODE_MASTER;
2593 		else
2594 			iwe.u.mode = IW_MODE_ADHOC;
2595 		current_ev = iwe_stream_add_event_check(info, current_ev,
2596 							end_buf, &iwe,
2597 							IW_EV_UINT_LEN);
2598 		if (IS_ERR(current_ev))
2599 			goto unlock;
2600 	}
2601 
2602 	memset(&iwe, 0, sizeof(iwe));
2603 	iwe.cmd = IWEVCUSTOM;
2604 	sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
2605 	iwe.u.data.length = strlen(buf);
2606 	current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
2607 						&iwe, buf);
2608 	if (IS_ERR(current_ev))
2609 		goto unlock;
2610 	memset(&iwe, 0, sizeof(iwe));
2611 	iwe.cmd = IWEVCUSTOM;
2612 	sprintf(buf, " Last beacon: %ums ago",
2613 		elapsed_jiffies_msecs(bss->ts));
2614 	iwe.u.data.length = strlen(buf);
2615 	current_ev = iwe_stream_add_point_check(info, current_ev,
2616 						end_buf, &iwe, buf);
2617 	if (IS_ERR(current_ev))
2618 		goto unlock;
2619 
2620 	current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
2621 
2622  unlock:
2623 	rcu_read_unlock();
2624 	return current_ev;
2625 }
2626 
2627 
ieee80211_scan_results(struct cfg80211_registered_device * rdev,struct iw_request_info * info,char * buf,size_t len)2628 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
2629 				  struct iw_request_info *info,
2630 				  char *buf, size_t len)
2631 {
2632 	char *current_ev = buf;
2633 	char *end_buf = buf + len;
2634 	struct cfg80211_internal_bss *bss;
2635 	int err = 0;
2636 
2637 	spin_lock_bh(&rdev->bss_lock);
2638 	cfg80211_bss_expire(rdev);
2639 
2640 	list_for_each_entry(bss, &rdev->bss_list, list) {
2641 		if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
2642 			err = -E2BIG;
2643 			break;
2644 		}
2645 		current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
2646 					   current_ev, end_buf);
2647 		if (IS_ERR(current_ev)) {
2648 			err = PTR_ERR(current_ev);
2649 			break;
2650 		}
2651 	}
2652 	spin_unlock_bh(&rdev->bss_lock);
2653 
2654 	if (err)
2655 		return err;
2656 	return current_ev - buf;
2657 }
2658 
2659 
cfg80211_wext_giwscan(struct net_device * dev,struct iw_request_info * info,struct iw_point * data,char * extra)2660 int cfg80211_wext_giwscan(struct net_device *dev,
2661 			  struct iw_request_info *info,
2662 			  struct iw_point *data, char *extra)
2663 {
2664 	struct cfg80211_registered_device *rdev;
2665 	int res;
2666 
2667 	if (!netif_running(dev))
2668 		return -ENETDOWN;
2669 
2670 	rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2671 
2672 	if (IS_ERR(rdev))
2673 		return PTR_ERR(rdev);
2674 
2675 	if (rdev->scan_req || rdev->scan_msg)
2676 		return -EAGAIN;
2677 
2678 	res = ieee80211_scan_results(rdev, info, extra, data->length);
2679 	data->length = 0;
2680 	if (res >= 0) {
2681 		data->length = res;
2682 		res = 0;
2683 	}
2684 
2685 	return res;
2686 }
2687 EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
2688 #endif
2689