xref: /linux/net/wireless/scan.c (revision 492c826b9facefa84995f4dea917e301b5ee0884)
1 /*
2  * cfg80211 scan result handling
3  *
4  * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
5  */
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 #include <linux/module.h>
9 #include <linux/netdevice.h>
10 #include <linux/wireless.h>
11 #include <linux/nl80211.h>
12 #include <linux/etherdevice.h>
13 #include <net/arp.h>
14 #include <net/cfg80211.h>
15 #include <net/iw_handler.h>
16 #include "core.h"
17 #include "nl80211.h"
18 #include "wext-compat.h"
19 
20 #define IEEE80211_SCAN_RESULT_EXPIRE	(15 * HZ)
21 
22 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak)
23 {
24 	struct cfg80211_scan_request *request;
25 	struct net_device *dev;
26 #ifdef CONFIG_CFG80211_WEXT
27 	union iwreq_data wrqu;
28 #endif
29 
30 	ASSERT_RDEV_LOCK(rdev);
31 
32 	request = rdev->scan_req;
33 
34 	if (!request)
35 		return;
36 
37 	dev = request->dev;
38 
39 	/*
40 	 * This must be before sending the other events!
41 	 * Otherwise, wpa_supplicant gets completely confused with
42 	 * wext events.
43 	 */
44 	cfg80211_sme_scan_done(dev);
45 
46 	if (request->aborted)
47 		nl80211_send_scan_aborted(rdev, dev);
48 	else
49 		nl80211_send_scan_done(rdev, dev);
50 
51 #ifdef CONFIG_CFG80211_WEXT
52 	if (!request->aborted) {
53 		memset(&wrqu, 0, sizeof(wrqu));
54 
55 		wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
56 	}
57 #endif
58 
59 	dev_put(dev);
60 
61 	rdev->scan_req = NULL;
62 
63 	/*
64 	 * OK. If this is invoked with "leak" then we can't
65 	 * free this ... but we've cleaned it up anyway. The
66 	 * driver failed to call the scan_done callback, so
67 	 * all bets are off, it might still be trying to use
68 	 * the scan request or not ... if it accesses the dev
69 	 * in there (it shouldn't anyway) then it may crash.
70 	 */
71 	if (!leak)
72 		kfree(request);
73 }
74 
75 void __cfg80211_scan_done(struct work_struct *wk)
76 {
77 	struct cfg80211_registered_device *rdev;
78 
79 	rdev = container_of(wk, struct cfg80211_registered_device,
80 			    scan_done_wk);
81 
82 	cfg80211_lock_rdev(rdev);
83 	___cfg80211_scan_done(rdev, false);
84 	cfg80211_unlock_rdev(rdev);
85 }
86 
87 void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
88 {
89 	WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
90 
91 	request->aborted = aborted;
92 	queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk);
93 }
94 EXPORT_SYMBOL(cfg80211_scan_done);
95 
96 void __cfg80211_sched_scan_results(struct work_struct *wk)
97 {
98 	struct cfg80211_registered_device *rdev;
99 
100 	rdev = container_of(wk, struct cfg80211_registered_device,
101 			    sched_scan_results_wk);
102 
103 	cfg80211_lock_rdev(rdev);
104 
105 	/* we don't have sched_scan_req anymore if the scan is stopping */
106 	if (rdev->sched_scan_req)
107 		nl80211_send_sched_scan_results(rdev,
108 						rdev->sched_scan_req->dev);
109 
110 	cfg80211_unlock_rdev(rdev);
111 }
112 
113 void cfg80211_sched_scan_results(struct wiphy *wiphy)
114 {
115 	/* ignore if we're not scanning */
116 	if (wiphy_to_dev(wiphy)->sched_scan_req)
117 		queue_work(cfg80211_wq,
118 			   &wiphy_to_dev(wiphy)->sched_scan_results_wk);
119 }
120 EXPORT_SYMBOL(cfg80211_sched_scan_results);
121 
122 void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
123 {
124 	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
125 
126 	cfg80211_lock_rdev(rdev);
127 	__cfg80211_stop_sched_scan(rdev, true);
128 	cfg80211_unlock_rdev(rdev);
129 }
130 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
131 
132 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
133 			       bool driver_initiated)
134 {
135 	int err;
136 	struct net_device *dev;
137 
138 	ASSERT_RDEV_LOCK(rdev);
139 
140 	if (!rdev->sched_scan_req)
141 		return 0;
142 
143 	dev = rdev->sched_scan_req->dev;
144 
145 	if (!driver_initiated) {
146 		err = rdev->ops->sched_scan_stop(&rdev->wiphy, dev);
147 		if (err)
148 			return err;
149 	}
150 
151 	nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED);
152 
153 	kfree(rdev->sched_scan_req);
154 	rdev->sched_scan_req = NULL;
155 
156 	return err;
157 }
158 
159 static void bss_release(struct kref *ref)
160 {
161 	struct cfg80211_internal_bss *bss;
162 
163 	bss = container_of(ref, struct cfg80211_internal_bss, ref);
164 	if (bss->pub.free_priv)
165 		bss->pub.free_priv(&bss->pub);
166 
167 	if (bss->beacon_ies_allocated)
168 		kfree(bss->pub.beacon_ies);
169 	if (bss->proberesp_ies_allocated)
170 		kfree(bss->pub.proberesp_ies);
171 
172 	BUG_ON(atomic_read(&bss->hold));
173 
174 	kfree(bss);
175 }
176 
177 /* must hold dev->bss_lock! */
178 void cfg80211_bss_age(struct cfg80211_registered_device *dev,
179                       unsigned long age_secs)
180 {
181 	struct cfg80211_internal_bss *bss;
182 	unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
183 
184 	list_for_each_entry(bss, &dev->bss_list, list) {
185 		bss->ts -= age_jiffies;
186 	}
187 }
188 
189 /* must hold dev->bss_lock! */
190 static void __cfg80211_unlink_bss(struct cfg80211_registered_device *dev,
191 				  struct cfg80211_internal_bss *bss)
192 {
193 	list_del_init(&bss->list);
194 	rb_erase(&bss->rbn, &dev->bss_tree);
195 	kref_put(&bss->ref, bss_release);
196 }
197 
198 /* must hold dev->bss_lock! */
199 void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
200 {
201 	struct cfg80211_internal_bss *bss, *tmp;
202 	bool expired = false;
203 
204 	list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
205 		if (atomic_read(&bss->hold))
206 			continue;
207 		if (!time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE))
208 			continue;
209 		__cfg80211_unlink_bss(dev, bss);
210 		expired = true;
211 	}
212 
213 	if (expired)
214 		dev->bss_generation++;
215 }
216 
217 const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
218 {
219 	while (len > 2 && ies[0] != eid) {
220 		len -= ies[1] + 2;
221 		ies += ies[1] + 2;
222 	}
223 	if (len < 2)
224 		return NULL;
225 	if (len < 2 + ies[1])
226 		return NULL;
227 	return ies;
228 }
229 EXPORT_SYMBOL(cfg80211_find_ie);
230 
231 static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
232 {
233 	const u8 *ie1 = cfg80211_find_ie(num, ies1, len1);
234 	const u8 *ie2 = cfg80211_find_ie(num, ies2, len2);
235 	int r;
236 
237 	if (!ie1 && !ie2)
238 		return 0;
239 	if (!ie1 || !ie2)
240 		return -1;
241 
242 	r = memcmp(ie1 + 2, ie2 + 2, min(ie1[1], ie2[1]));
243 	if (r == 0 && ie1[1] != ie2[1])
244 		return ie2[1] - ie1[1];
245 	return r;
246 }
247 
248 static bool is_bss(struct cfg80211_bss *a,
249 		   const u8 *bssid,
250 		   const u8 *ssid, size_t ssid_len)
251 {
252 	const u8 *ssidie;
253 
254 	if (bssid && compare_ether_addr(a->bssid, bssid))
255 		return false;
256 
257 	if (!ssid)
258 		return true;
259 
260 	ssidie = cfg80211_find_ie(WLAN_EID_SSID,
261 				  a->information_elements,
262 				  a->len_information_elements);
263 	if (!ssidie)
264 		return false;
265 	if (ssidie[1] != ssid_len)
266 		return false;
267 	return memcmp(ssidie + 2, ssid, ssid_len) == 0;
268 }
269 
270 static bool is_mesh(struct cfg80211_bss *a,
271 		    const u8 *meshid, size_t meshidlen,
272 		    const u8 *meshcfg)
273 {
274 	const u8 *ie;
275 
276 	if (!WLAN_CAPABILITY_IS_MBSS(a->capability))
277 		return false;
278 
279 	ie = cfg80211_find_ie(WLAN_EID_MESH_ID,
280 			      a->information_elements,
281 			      a->len_information_elements);
282 	if (!ie)
283 		return false;
284 	if (ie[1] != meshidlen)
285 		return false;
286 	if (memcmp(ie + 2, meshid, meshidlen))
287 		return false;
288 
289 	ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
290 			      a->information_elements,
291 			      a->len_information_elements);
292 	if (!ie)
293 		return false;
294 	if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
295 		return false;
296 
297 	/*
298 	 * Ignore mesh capability (last two bytes of the IE) when
299 	 * comparing since that may differ between stations taking
300 	 * part in the same mesh.
301 	 */
302 	return memcmp(ie + 2, meshcfg,
303 	    sizeof(struct ieee80211_meshconf_ie) - 2) == 0;
304 }
305 
306 static int cmp_bss(struct cfg80211_bss *a,
307 		   struct cfg80211_bss *b)
308 {
309 	int r;
310 
311 	if (a->channel != b->channel)
312 		return b->channel->center_freq - a->channel->center_freq;
313 
314 	if (WLAN_CAPABILITY_IS_MBSS(a->capability | b->capability)) {
315 		r = cmp_ies(WLAN_EID_MESH_ID,
316 			    a->information_elements,
317 			    a->len_information_elements,
318 			    b->information_elements,
319 			    b->len_information_elements);
320 		if (r)
321 			return r;
322 		return cmp_ies(WLAN_EID_MESH_CONFIG,
323 			       a->information_elements,
324 			       a->len_information_elements,
325 			       b->information_elements,
326 			       b->len_information_elements);
327 	}
328 
329 	r = memcmp(a->bssid, b->bssid, ETH_ALEN);
330 	if (r)
331 		return r;
332 
333 	return cmp_ies(WLAN_EID_SSID,
334 		       a->information_elements,
335 		       a->len_information_elements,
336 		       b->information_elements,
337 		       b->len_information_elements);
338 }
339 
340 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
341 				      struct ieee80211_channel *channel,
342 				      const u8 *bssid,
343 				      const u8 *ssid, size_t ssid_len,
344 				      u16 capa_mask, u16 capa_val)
345 {
346 	struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
347 	struct cfg80211_internal_bss *bss, *res = NULL;
348 	unsigned long now = jiffies;
349 
350 	spin_lock_bh(&dev->bss_lock);
351 
352 	list_for_each_entry(bss, &dev->bss_list, list) {
353 		if ((bss->pub.capability & capa_mask) != capa_val)
354 			continue;
355 		if (channel && bss->pub.channel != channel)
356 			continue;
357 		/* Don't get expired BSS structs */
358 		if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
359 		    !atomic_read(&bss->hold))
360 			continue;
361 		if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
362 			res = bss;
363 			kref_get(&res->ref);
364 			break;
365 		}
366 	}
367 
368 	spin_unlock_bh(&dev->bss_lock);
369 	if (!res)
370 		return NULL;
371 	return &res->pub;
372 }
373 EXPORT_SYMBOL(cfg80211_get_bss);
374 
375 struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
376 				       struct ieee80211_channel *channel,
377 				       const u8 *meshid, size_t meshidlen,
378 				       const u8 *meshcfg)
379 {
380 	struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
381 	struct cfg80211_internal_bss *bss, *res = NULL;
382 
383 	spin_lock_bh(&dev->bss_lock);
384 
385 	list_for_each_entry(bss, &dev->bss_list, list) {
386 		if (channel && bss->pub.channel != channel)
387 			continue;
388 		if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
389 			res = bss;
390 			kref_get(&res->ref);
391 			break;
392 		}
393 	}
394 
395 	spin_unlock_bh(&dev->bss_lock);
396 	if (!res)
397 		return NULL;
398 	return &res->pub;
399 }
400 EXPORT_SYMBOL(cfg80211_get_mesh);
401 
402 
403 static void rb_insert_bss(struct cfg80211_registered_device *dev,
404 			  struct cfg80211_internal_bss *bss)
405 {
406 	struct rb_node **p = &dev->bss_tree.rb_node;
407 	struct rb_node *parent = NULL;
408 	struct cfg80211_internal_bss *tbss;
409 	int cmp;
410 
411 	while (*p) {
412 		parent = *p;
413 		tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
414 
415 		cmp = cmp_bss(&bss->pub, &tbss->pub);
416 
417 		if (WARN_ON(!cmp)) {
418 			/* will sort of leak this BSS */
419 			return;
420 		}
421 
422 		if (cmp < 0)
423 			p = &(*p)->rb_left;
424 		else
425 			p = &(*p)->rb_right;
426 	}
427 
428 	rb_link_node(&bss->rbn, parent, p);
429 	rb_insert_color(&bss->rbn, &dev->bss_tree);
430 }
431 
432 static struct cfg80211_internal_bss *
433 rb_find_bss(struct cfg80211_registered_device *dev,
434 	    struct cfg80211_internal_bss *res)
435 {
436 	struct rb_node *n = dev->bss_tree.rb_node;
437 	struct cfg80211_internal_bss *bss;
438 	int r;
439 
440 	while (n) {
441 		bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
442 		r = cmp_bss(&res->pub, &bss->pub);
443 
444 		if (r == 0)
445 			return bss;
446 		else if (r < 0)
447 			n = n->rb_left;
448 		else
449 			n = n->rb_right;
450 	}
451 
452 	return NULL;
453 }
454 
455 static struct cfg80211_internal_bss *
456 cfg80211_bss_update(struct cfg80211_registered_device *dev,
457 		    struct cfg80211_internal_bss *res)
458 {
459 	struct cfg80211_internal_bss *found = NULL;
460 	const u8 *meshid, *meshcfg;
461 
462 	/*
463 	 * The reference to "res" is donated to this function.
464 	 */
465 
466 	if (WARN_ON(!res->pub.channel)) {
467 		kref_put(&res->ref, bss_release);
468 		return NULL;
469 	}
470 
471 	res->ts = jiffies;
472 
473 	if (WLAN_CAPABILITY_IS_MBSS(res->pub.capability)) {
474 		/* must be mesh, verify */
475 		meshid = cfg80211_find_ie(WLAN_EID_MESH_ID,
476 					  res->pub.information_elements,
477 					  res->pub.len_information_elements);
478 		meshcfg = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
479 					   res->pub.information_elements,
480 					   res->pub.len_information_elements);
481 		if (!meshid || !meshcfg ||
482 		    meshcfg[1] != sizeof(struct ieee80211_meshconf_ie)) {
483 			/* bogus mesh */
484 			kref_put(&res->ref, bss_release);
485 			return NULL;
486 		}
487 	}
488 
489 	spin_lock_bh(&dev->bss_lock);
490 
491 	found = rb_find_bss(dev, res);
492 
493 	if (found) {
494 		found->pub.beacon_interval = res->pub.beacon_interval;
495 		found->pub.tsf = res->pub.tsf;
496 		found->pub.signal = res->pub.signal;
497 		found->pub.capability = res->pub.capability;
498 		found->ts = res->ts;
499 
500 		/* Update IEs */
501 		if (res->pub.proberesp_ies) {
502 			size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
503 			size_t ielen = res->pub.len_proberesp_ies;
504 
505 			if (found->pub.proberesp_ies &&
506 			    !found->proberesp_ies_allocated &&
507 			    ksize(found) >= used + ielen) {
508 				memcpy(found->pub.proberesp_ies,
509 				       res->pub.proberesp_ies, ielen);
510 				found->pub.len_proberesp_ies = ielen;
511 			} else {
512 				u8 *ies = found->pub.proberesp_ies;
513 
514 				if (found->proberesp_ies_allocated)
515 					ies = krealloc(ies, ielen, GFP_ATOMIC);
516 				else
517 					ies = kmalloc(ielen, GFP_ATOMIC);
518 
519 				if (ies) {
520 					memcpy(ies, res->pub.proberesp_ies,
521 					       ielen);
522 					found->proberesp_ies_allocated = true;
523 					found->pub.proberesp_ies = ies;
524 					found->pub.len_proberesp_ies = ielen;
525 				}
526 			}
527 
528 			/* Override possible earlier Beacon frame IEs */
529 			found->pub.information_elements =
530 				found->pub.proberesp_ies;
531 			found->pub.len_information_elements =
532 				found->pub.len_proberesp_ies;
533 		}
534 		if (res->pub.beacon_ies) {
535 			size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
536 			size_t ielen = res->pub.len_beacon_ies;
537 			bool information_elements_is_beacon_ies =
538 				(found->pub.information_elements ==
539 				 found->pub.beacon_ies);
540 
541 			if (found->pub.beacon_ies &&
542 			    !found->beacon_ies_allocated &&
543 			    ksize(found) >= used + ielen) {
544 				memcpy(found->pub.beacon_ies,
545 				       res->pub.beacon_ies, ielen);
546 				found->pub.len_beacon_ies = ielen;
547 			} else {
548 				u8 *ies = found->pub.beacon_ies;
549 
550 				if (found->beacon_ies_allocated)
551 					ies = krealloc(ies, ielen, GFP_ATOMIC);
552 				else
553 					ies = kmalloc(ielen, GFP_ATOMIC);
554 
555 				if (ies) {
556 					memcpy(ies, res->pub.beacon_ies,
557 					       ielen);
558 					found->beacon_ies_allocated = true;
559 					found->pub.beacon_ies = ies;
560 					found->pub.len_beacon_ies = ielen;
561 				}
562 			}
563 
564 			/* Override IEs if they were from a beacon before */
565 			if (information_elements_is_beacon_ies) {
566 				found->pub.information_elements =
567 					found->pub.beacon_ies;
568 				found->pub.len_information_elements =
569 					found->pub.len_beacon_ies;
570 			}
571 		}
572 
573 		kref_put(&res->ref, bss_release);
574 	} else {
575 		/* this "consumes" the reference */
576 		list_add_tail(&res->list, &dev->bss_list);
577 		rb_insert_bss(dev, res);
578 		found = res;
579 	}
580 
581 	dev->bss_generation++;
582 	spin_unlock_bh(&dev->bss_lock);
583 
584 	kref_get(&found->ref);
585 	return found;
586 }
587 
588 struct cfg80211_bss*
589 cfg80211_inform_bss(struct wiphy *wiphy,
590 		    struct ieee80211_channel *channel,
591 		    const u8 *bssid,
592 		    u64 timestamp, u16 capability, u16 beacon_interval,
593 		    const u8 *ie, size_t ielen,
594 		    s32 signal, gfp_t gfp)
595 {
596 	struct cfg80211_internal_bss *res;
597 	size_t privsz;
598 
599 	if (WARN_ON(!wiphy))
600 		return NULL;
601 
602 	privsz = wiphy->bss_priv_size;
603 
604 	if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
605 			(signal < 0 || signal > 100)))
606 		return NULL;
607 
608 	res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
609 	if (!res)
610 		return NULL;
611 
612 	memcpy(res->pub.bssid, bssid, ETH_ALEN);
613 	res->pub.channel = channel;
614 	res->pub.signal = signal;
615 	res->pub.tsf = timestamp;
616 	res->pub.beacon_interval = beacon_interval;
617 	res->pub.capability = capability;
618 	/*
619 	 * Since we do not know here whether the IEs are from a Beacon or Probe
620 	 * Response frame, we need to pick one of the options and only use it
621 	 * with the driver that does not provide the full Beacon/Probe Response
622 	 * frame. Use Beacon frame pointer to avoid indicating that this should
623 	 * override the information_elements pointer should we have received an
624 	 * earlier indication of Probe Response data.
625 	 *
626 	 * The initial buffer for the IEs is allocated with the BSS entry and
627 	 * is located after the private area.
628 	 */
629 	res->pub.beacon_ies = (u8 *)res + sizeof(*res) + privsz;
630 	memcpy(res->pub.beacon_ies, ie, ielen);
631 	res->pub.len_beacon_ies = ielen;
632 	res->pub.information_elements = res->pub.beacon_ies;
633 	res->pub.len_information_elements = res->pub.len_beacon_ies;
634 
635 	kref_init(&res->ref);
636 
637 	res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
638 	if (!res)
639 		return NULL;
640 
641 	if (res->pub.capability & WLAN_CAPABILITY_ESS)
642 		regulatory_hint_found_beacon(wiphy, channel, gfp);
643 
644 	/* cfg80211_bss_update gives us a referenced result */
645 	return &res->pub;
646 }
647 EXPORT_SYMBOL(cfg80211_inform_bss);
648 
649 struct cfg80211_bss *
650 cfg80211_inform_bss_frame(struct wiphy *wiphy,
651 			  struct ieee80211_channel *channel,
652 			  struct ieee80211_mgmt *mgmt, size_t len,
653 			  s32 signal, gfp_t gfp)
654 {
655 	struct cfg80211_internal_bss *res;
656 	size_t ielen = len - offsetof(struct ieee80211_mgmt,
657 				      u.probe_resp.variable);
658 	size_t privsz;
659 
660 	if (WARN_ON(!mgmt))
661 		return NULL;
662 
663 	if (WARN_ON(!wiphy))
664 		return NULL;
665 
666 	if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
667 	            (signal < 0 || signal > 100)))
668 		return NULL;
669 
670 	if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
671 		return NULL;
672 
673 	privsz = wiphy->bss_priv_size;
674 
675 	res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
676 	if (!res)
677 		return NULL;
678 
679 	memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
680 	res->pub.channel = channel;
681 	res->pub.signal = signal;
682 	res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
683 	res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
684 	res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
685 	/*
686 	 * The initial buffer for the IEs is allocated with the BSS entry and
687 	 * is located after the private area.
688 	 */
689 	if (ieee80211_is_probe_resp(mgmt->frame_control)) {
690 		res->pub.proberesp_ies = (u8 *) res + sizeof(*res) + privsz;
691 		memcpy(res->pub.proberesp_ies, mgmt->u.probe_resp.variable,
692 		       ielen);
693 		res->pub.len_proberesp_ies = ielen;
694 		res->pub.information_elements = res->pub.proberesp_ies;
695 		res->pub.len_information_elements = res->pub.len_proberesp_ies;
696 	} else {
697 		res->pub.beacon_ies = (u8 *) res + sizeof(*res) + privsz;
698 		memcpy(res->pub.beacon_ies, mgmt->u.beacon.variable, ielen);
699 		res->pub.len_beacon_ies = ielen;
700 		res->pub.information_elements = res->pub.beacon_ies;
701 		res->pub.len_information_elements = res->pub.len_beacon_ies;
702 	}
703 
704 	kref_init(&res->ref);
705 
706 	res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
707 	if (!res)
708 		return NULL;
709 
710 	if (res->pub.capability & WLAN_CAPABILITY_ESS)
711 		regulatory_hint_found_beacon(wiphy, channel, gfp);
712 
713 	/* cfg80211_bss_update gives us a referenced result */
714 	return &res->pub;
715 }
716 EXPORT_SYMBOL(cfg80211_inform_bss_frame);
717 
718 void cfg80211_put_bss(struct cfg80211_bss *pub)
719 {
720 	struct cfg80211_internal_bss *bss;
721 
722 	if (!pub)
723 		return;
724 
725 	bss = container_of(pub, struct cfg80211_internal_bss, pub);
726 	kref_put(&bss->ref, bss_release);
727 }
728 EXPORT_SYMBOL(cfg80211_put_bss);
729 
730 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
731 {
732 	struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
733 	struct cfg80211_internal_bss *bss;
734 
735 	if (WARN_ON(!pub))
736 		return;
737 
738 	bss = container_of(pub, struct cfg80211_internal_bss, pub);
739 
740 	spin_lock_bh(&dev->bss_lock);
741 	if (!list_empty(&bss->list)) {
742 		__cfg80211_unlink_bss(dev, bss);
743 		dev->bss_generation++;
744 	}
745 	spin_unlock_bh(&dev->bss_lock);
746 }
747 EXPORT_SYMBOL(cfg80211_unlink_bss);
748 
749 #ifdef CONFIG_CFG80211_WEXT
750 int cfg80211_wext_siwscan(struct net_device *dev,
751 			  struct iw_request_info *info,
752 			  union iwreq_data *wrqu, char *extra)
753 {
754 	struct cfg80211_registered_device *rdev;
755 	struct wiphy *wiphy;
756 	struct iw_scan_req *wreq = NULL;
757 	struct cfg80211_scan_request *creq = NULL;
758 	int i, err, n_channels = 0;
759 	enum ieee80211_band band;
760 
761 	if (!netif_running(dev))
762 		return -ENETDOWN;
763 
764 	if (wrqu->data.length == sizeof(struct iw_scan_req))
765 		wreq = (struct iw_scan_req *)extra;
766 
767 	rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
768 
769 	if (IS_ERR(rdev))
770 		return PTR_ERR(rdev);
771 
772 	if (rdev->scan_req) {
773 		err = -EBUSY;
774 		goto out;
775 	}
776 
777 	wiphy = &rdev->wiphy;
778 
779 	/* Determine number of channels, needed to allocate creq */
780 	if (wreq && wreq->num_channels)
781 		n_channels = wreq->num_channels;
782 	else {
783 		for (band = 0; band < IEEE80211_NUM_BANDS; band++)
784 			if (wiphy->bands[band])
785 				n_channels += wiphy->bands[band]->n_channels;
786 	}
787 
788 	creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
789 		       n_channels * sizeof(void *),
790 		       GFP_ATOMIC);
791 	if (!creq) {
792 		err = -ENOMEM;
793 		goto out;
794 	}
795 
796 	creq->wiphy = wiphy;
797 	creq->dev = dev;
798 	/* SSIDs come after channels */
799 	creq->ssids = (void *)&creq->channels[n_channels];
800 	creq->n_channels = n_channels;
801 	creq->n_ssids = 1;
802 
803 	/* translate "Scan on frequencies" request */
804 	i = 0;
805 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
806 		int j;
807 
808 		if (!wiphy->bands[band])
809 			continue;
810 
811 		for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
812 			/* ignore disabled channels */
813 			if (wiphy->bands[band]->channels[j].flags &
814 						IEEE80211_CHAN_DISABLED)
815 				continue;
816 
817 			/* If we have a wireless request structure and the
818 			 * wireless request specifies frequencies, then search
819 			 * for the matching hardware channel.
820 			 */
821 			if (wreq && wreq->num_channels) {
822 				int k;
823 				int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
824 				for (k = 0; k < wreq->num_channels; k++) {
825 					int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
826 					if (wext_freq == wiphy_freq)
827 						goto wext_freq_found;
828 				}
829 				goto wext_freq_not_found;
830 			}
831 
832 		wext_freq_found:
833 			creq->channels[i] = &wiphy->bands[band]->channels[j];
834 			i++;
835 		wext_freq_not_found: ;
836 		}
837 	}
838 	/* No channels found? */
839 	if (!i) {
840 		err = -EINVAL;
841 		goto out;
842 	}
843 
844 	/* Set real number of channels specified in creq->channels[] */
845 	creq->n_channels = i;
846 
847 	/* translate "Scan for SSID" request */
848 	if (wreq) {
849 		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
850 			if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
851 				err = -EINVAL;
852 				goto out;
853 			}
854 			memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
855 			creq->ssids[0].ssid_len = wreq->essid_len;
856 		}
857 		if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
858 			creq->n_ssids = 0;
859 	}
860 
861 	rdev->scan_req = creq;
862 	err = rdev->ops->scan(wiphy, dev, creq);
863 	if (err) {
864 		rdev->scan_req = NULL;
865 		/* creq will be freed below */
866 	} else {
867 		nl80211_send_scan_start(rdev, dev);
868 		/* creq now owned by driver */
869 		creq = NULL;
870 		dev_hold(dev);
871 	}
872  out:
873 	kfree(creq);
874 	cfg80211_unlock_rdev(rdev);
875 	return err;
876 }
877 EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
878 
879 static void ieee80211_scan_add_ies(struct iw_request_info *info,
880 				   struct cfg80211_bss *bss,
881 				   char **current_ev, char *end_buf)
882 {
883 	u8 *pos, *end, *next;
884 	struct iw_event iwe;
885 
886 	if (!bss->information_elements ||
887 	    !bss->len_information_elements)
888 		return;
889 
890 	/*
891 	 * If needed, fragment the IEs buffer (at IE boundaries) into short
892 	 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
893 	 */
894 	pos = bss->information_elements;
895 	end = pos + bss->len_information_elements;
896 
897 	while (end - pos > IW_GENERIC_IE_MAX) {
898 		next = pos + 2 + pos[1];
899 		while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
900 			next = next + 2 + next[1];
901 
902 		memset(&iwe, 0, sizeof(iwe));
903 		iwe.cmd = IWEVGENIE;
904 		iwe.u.data.length = next - pos;
905 		*current_ev = iwe_stream_add_point(info, *current_ev,
906 						   end_buf, &iwe, pos);
907 
908 		pos = next;
909 	}
910 
911 	if (end > pos) {
912 		memset(&iwe, 0, sizeof(iwe));
913 		iwe.cmd = IWEVGENIE;
914 		iwe.u.data.length = end - pos;
915 		*current_ev = iwe_stream_add_point(info, *current_ev,
916 						   end_buf, &iwe, pos);
917 	}
918 }
919 
920 static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
921 {
922 	unsigned long end = jiffies;
923 
924 	if (end >= start)
925 		return jiffies_to_msecs(end - start);
926 
927 	return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
928 }
929 
930 static char *
931 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
932 	      struct cfg80211_internal_bss *bss, char *current_ev,
933 	      char *end_buf)
934 {
935 	struct iw_event iwe;
936 	u8 *buf, *cfg, *p;
937 	u8 *ie = bss->pub.information_elements;
938 	int rem = bss->pub.len_information_elements, i, sig;
939 	bool ismesh = false;
940 
941 	memset(&iwe, 0, sizeof(iwe));
942 	iwe.cmd = SIOCGIWAP;
943 	iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
944 	memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
945 	current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
946 					  IW_EV_ADDR_LEN);
947 
948 	memset(&iwe, 0, sizeof(iwe));
949 	iwe.cmd = SIOCGIWFREQ;
950 	iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
951 	iwe.u.freq.e = 0;
952 	current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
953 					  IW_EV_FREQ_LEN);
954 
955 	memset(&iwe, 0, sizeof(iwe));
956 	iwe.cmd = SIOCGIWFREQ;
957 	iwe.u.freq.m = bss->pub.channel->center_freq;
958 	iwe.u.freq.e = 6;
959 	current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
960 					  IW_EV_FREQ_LEN);
961 
962 	if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
963 		memset(&iwe, 0, sizeof(iwe));
964 		iwe.cmd = IWEVQUAL;
965 		iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
966 				     IW_QUAL_NOISE_INVALID |
967 				     IW_QUAL_QUAL_UPDATED;
968 		switch (wiphy->signal_type) {
969 		case CFG80211_SIGNAL_TYPE_MBM:
970 			sig = bss->pub.signal / 100;
971 			iwe.u.qual.level = sig;
972 			iwe.u.qual.updated |= IW_QUAL_DBM;
973 			if (sig < -110)		/* rather bad */
974 				sig = -110;
975 			else if (sig > -40)	/* perfect */
976 				sig = -40;
977 			/* will give a range of 0 .. 70 */
978 			iwe.u.qual.qual = sig + 110;
979 			break;
980 		case CFG80211_SIGNAL_TYPE_UNSPEC:
981 			iwe.u.qual.level = bss->pub.signal;
982 			/* will give range 0 .. 100 */
983 			iwe.u.qual.qual = bss->pub.signal;
984 			break;
985 		default:
986 			/* not reached */
987 			break;
988 		}
989 		current_ev = iwe_stream_add_event(info, current_ev, end_buf,
990 						  &iwe, IW_EV_QUAL_LEN);
991 	}
992 
993 	memset(&iwe, 0, sizeof(iwe));
994 	iwe.cmd = SIOCGIWENCODE;
995 	if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
996 		iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
997 	else
998 		iwe.u.data.flags = IW_ENCODE_DISABLED;
999 	iwe.u.data.length = 0;
1000 	current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1001 					  &iwe, "");
1002 
1003 	while (rem >= 2) {
1004 		/* invalid data */
1005 		if (ie[1] > rem - 2)
1006 			break;
1007 
1008 		switch (ie[0]) {
1009 		case WLAN_EID_SSID:
1010 			memset(&iwe, 0, sizeof(iwe));
1011 			iwe.cmd = SIOCGIWESSID;
1012 			iwe.u.data.length = ie[1];
1013 			iwe.u.data.flags = 1;
1014 			current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1015 							  &iwe, ie + 2);
1016 			break;
1017 		case WLAN_EID_MESH_ID:
1018 			memset(&iwe, 0, sizeof(iwe));
1019 			iwe.cmd = SIOCGIWESSID;
1020 			iwe.u.data.length = ie[1];
1021 			iwe.u.data.flags = 1;
1022 			current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1023 							  &iwe, ie + 2);
1024 			break;
1025 		case WLAN_EID_MESH_CONFIG:
1026 			ismesh = true;
1027 			if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
1028 				break;
1029 			buf = kmalloc(50, GFP_ATOMIC);
1030 			if (!buf)
1031 				break;
1032 			cfg = ie + 2;
1033 			memset(&iwe, 0, sizeof(iwe));
1034 			iwe.cmd = IWEVCUSTOM;
1035 			sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1036 				"0x%02X", cfg[0]);
1037 			iwe.u.data.length = strlen(buf);
1038 			current_ev = iwe_stream_add_point(info, current_ev,
1039 							  end_buf,
1040 							  &iwe, buf);
1041 			sprintf(buf, "Path Selection Metric ID: 0x%02X",
1042 				cfg[1]);
1043 			iwe.u.data.length = strlen(buf);
1044 			current_ev = iwe_stream_add_point(info, current_ev,
1045 							  end_buf,
1046 							  &iwe, buf);
1047 			sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1048 				cfg[2]);
1049 			iwe.u.data.length = strlen(buf);
1050 			current_ev = iwe_stream_add_point(info, current_ev,
1051 							  end_buf,
1052 							  &iwe, buf);
1053 			sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
1054 			iwe.u.data.length = strlen(buf);
1055 			current_ev = iwe_stream_add_point(info, current_ev,
1056 							  end_buf,
1057 							  &iwe, buf);
1058 			sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
1059 			iwe.u.data.length = strlen(buf);
1060 			current_ev = iwe_stream_add_point(info, current_ev,
1061 							  end_buf,
1062 							  &iwe, buf);
1063 			sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1064 			iwe.u.data.length = strlen(buf);
1065 			current_ev = iwe_stream_add_point(info, current_ev,
1066 							  end_buf,
1067 							  &iwe, buf);
1068 			sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
1069 			iwe.u.data.length = strlen(buf);
1070 			current_ev = iwe_stream_add_point(info, current_ev,
1071 							  end_buf,
1072 							  &iwe, buf);
1073 			kfree(buf);
1074 			break;
1075 		case WLAN_EID_SUPP_RATES:
1076 		case WLAN_EID_EXT_SUPP_RATES:
1077 			/* display all supported rates in readable format */
1078 			p = current_ev + iwe_stream_lcp_len(info);
1079 
1080 			memset(&iwe, 0, sizeof(iwe));
1081 			iwe.cmd = SIOCGIWRATE;
1082 			/* Those two flags are ignored... */
1083 			iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1084 
1085 			for (i = 0; i < ie[1]; i++) {
1086 				iwe.u.bitrate.value =
1087 					((ie[i + 2] & 0x7f) * 500000);
1088 				p = iwe_stream_add_value(info, current_ev, p,
1089 						end_buf, &iwe, IW_EV_PARAM_LEN);
1090 			}
1091 			current_ev = p;
1092 			break;
1093 		}
1094 		rem -= ie[1] + 2;
1095 		ie += ie[1] + 2;
1096 	}
1097 
1098 	if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1099 	    ismesh) {
1100 		memset(&iwe, 0, sizeof(iwe));
1101 		iwe.cmd = SIOCGIWMODE;
1102 		if (ismesh)
1103 			iwe.u.mode = IW_MODE_MESH;
1104 		else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
1105 			iwe.u.mode = IW_MODE_MASTER;
1106 		else
1107 			iwe.u.mode = IW_MODE_ADHOC;
1108 		current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1109 						  &iwe, IW_EV_UINT_LEN);
1110 	}
1111 
1112 	buf = kmalloc(30, GFP_ATOMIC);
1113 	if (buf) {
1114 		memset(&iwe, 0, sizeof(iwe));
1115 		iwe.cmd = IWEVCUSTOM;
1116 		sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
1117 		iwe.u.data.length = strlen(buf);
1118 		current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1119 						  &iwe, buf);
1120 		memset(&iwe, 0, sizeof(iwe));
1121 		iwe.cmd = IWEVCUSTOM;
1122 		sprintf(buf, " Last beacon: %ums ago",
1123 			elapsed_jiffies_msecs(bss->ts));
1124 		iwe.u.data.length = strlen(buf);
1125 		current_ev = iwe_stream_add_point(info, current_ev,
1126 						  end_buf, &iwe, buf);
1127 		kfree(buf);
1128 	}
1129 
1130 	ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
1131 
1132 	return current_ev;
1133 }
1134 
1135 
1136 static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
1137 				  struct iw_request_info *info,
1138 				  char *buf, size_t len)
1139 {
1140 	char *current_ev = buf;
1141 	char *end_buf = buf + len;
1142 	struct cfg80211_internal_bss *bss;
1143 
1144 	spin_lock_bh(&dev->bss_lock);
1145 	cfg80211_bss_expire(dev);
1146 
1147 	list_for_each_entry(bss, &dev->bss_list, list) {
1148 		if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1149 			spin_unlock_bh(&dev->bss_lock);
1150 			return -E2BIG;
1151 		}
1152 		current_ev = ieee80211_bss(&dev->wiphy, info, bss,
1153 					   current_ev, end_buf);
1154 	}
1155 	spin_unlock_bh(&dev->bss_lock);
1156 	return current_ev - buf;
1157 }
1158 
1159 
1160 int cfg80211_wext_giwscan(struct net_device *dev,
1161 			  struct iw_request_info *info,
1162 			  struct iw_point *data, char *extra)
1163 {
1164 	struct cfg80211_registered_device *rdev;
1165 	int res;
1166 
1167 	if (!netif_running(dev))
1168 		return -ENETDOWN;
1169 
1170 	rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
1171 
1172 	if (IS_ERR(rdev))
1173 		return PTR_ERR(rdev);
1174 
1175 	if (rdev->scan_req) {
1176 		res = -EAGAIN;
1177 		goto out;
1178 	}
1179 
1180 	res = ieee80211_scan_results(rdev, info, extra, data->length);
1181 	data->length = 0;
1182 	if (res >= 0) {
1183 		data->length = res;
1184 		res = 0;
1185 	}
1186 
1187  out:
1188 	cfg80211_unlock_rdev(rdev);
1189 	return res;
1190 }
1191 EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
1192 #endif
1193