xref: /linux/net/wireless/scan.c (revision 5bdef865eb358b6f3760e25e591ae115e9eeddef)
1 /*
2  * cfg80211 scan result handling
3  *
4  * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
5  */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/netdevice.h>
9 #include <linux/wireless.h>
10 #include <linux/nl80211.h>
11 #include <linux/etherdevice.h>
12 #include <net/arp.h>
13 #include <net/cfg80211.h>
14 #include <net/iw_handler.h>
15 #include "core.h"
16 #include "nl80211.h"
17 
18 #define IEEE80211_SCAN_RESULT_EXPIRE	(10 * HZ)
19 
20 void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
21 {
22 	struct net_device *dev;
23 #ifdef CONFIG_WIRELESS_EXT
24 	union iwreq_data wrqu;
25 #endif
26 
27 	dev = dev_get_by_index(&init_net, request->ifidx);
28 	if (!dev)
29 		goto out;
30 
31 	WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
32 
33 	if (aborted)
34 		nl80211_send_scan_aborted(wiphy_to_dev(request->wiphy), dev);
35 	else
36 		nl80211_send_scan_done(wiphy_to_dev(request->wiphy), dev);
37 
38 	wiphy_to_dev(request->wiphy)->scan_req = NULL;
39 
40 #ifdef CONFIG_WIRELESS_EXT
41 	if (!aborted) {
42 		memset(&wrqu, 0, sizeof(wrqu));
43 
44 		wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
45 	}
46 #endif
47 
48 	dev_put(dev);
49 
50  out:
51 	kfree(request);
52 }
53 EXPORT_SYMBOL(cfg80211_scan_done);
54 
55 static void bss_release(struct kref *ref)
56 {
57 	struct cfg80211_internal_bss *bss;
58 
59 	bss = container_of(ref, struct cfg80211_internal_bss, ref);
60 	if (bss->pub.free_priv)
61 		bss->pub.free_priv(&bss->pub);
62 
63 	if (bss->ies_allocated)
64 		kfree(bss->pub.information_elements);
65 
66 	kfree(bss);
67 }
68 
69 /* must hold dev->bss_lock! */
70 void cfg80211_bss_age(struct cfg80211_registered_device *dev,
71                       unsigned long age_secs)
72 {
73 	struct cfg80211_internal_bss *bss;
74 	unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
75 
76 	list_for_each_entry(bss, &dev->bss_list, list) {
77 		bss->ts -= age_jiffies;
78 	}
79 }
80 
81 /* must hold dev->bss_lock! */
82 void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
83 {
84 	struct cfg80211_internal_bss *bss, *tmp;
85 	bool expired = false;
86 
87 	list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
88 		if (bss->hold ||
89 		    !time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE))
90 			continue;
91 		list_del(&bss->list);
92 		rb_erase(&bss->rbn, &dev->bss_tree);
93 		kref_put(&bss->ref, bss_release);
94 		expired = true;
95 	}
96 
97 	if (expired)
98 		dev->bss_generation++;
99 }
100 
101 static u8 *find_ie(u8 num, u8 *ies, size_t len)
102 {
103 	while (len > 2 && ies[0] != num) {
104 		len -= ies[1] + 2;
105 		ies += ies[1] + 2;
106 	}
107 	if (len < 2)
108 		return NULL;
109 	if (len < 2 + ies[1])
110 		return NULL;
111 	return ies;
112 }
113 
114 static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
115 {
116 	const u8 *ie1 = find_ie(num, ies1, len1);
117 	const u8 *ie2 = find_ie(num, ies2, len2);
118 	int r;
119 
120 	if (!ie1 && !ie2)
121 		return 0;
122 	if (!ie1)
123 		return -1;
124 
125 	r = memcmp(ie1 + 2, ie2 + 2, min(ie1[1], ie2[1]));
126 	if (r == 0 && ie1[1] != ie2[1])
127 		return ie2[1] - ie1[1];
128 	return r;
129 }
130 
131 static bool is_bss(struct cfg80211_bss *a,
132 		   const u8 *bssid,
133 		   const u8 *ssid, size_t ssid_len)
134 {
135 	const u8 *ssidie;
136 
137 	if (bssid && compare_ether_addr(a->bssid, bssid))
138 		return false;
139 
140 	if (!ssid)
141 		return true;
142 
143 	ssidie = find_ie(WLAN_EID_SSID,
144 			 a->information_elements,
145 			 a->len_information_elements);
146 	if (!ssidie)
147 		return false;
148 	if (ssidie[1] != ssid_len)
149 		return false;
150 	return memcmp(ssidie + 2, ssid, ssid_len) == 0;
151 }
152 
153 static bool is_mesh(struct cfg80211_bss *a,
154 		    const u8 *meshid, size_t meshidlen,
155 		    const u8 *meshcfg)
156 {
157 	const u8 *ie;
158 
159 	if (!is_zero_ether_addr(a->bssid))
160 		return false;
161 
162 	ie = find_ie(WLAN_EID_MESH_ID,
163 		     a->information_elements,
164 		     a->len_information_elements);
165 	if (!ie)
166 		return false;
167 	if (ie[1] != meshidlen)
168 		return false;
169 	if (memcmp(ie + 2, meshid, meshidlen))
170 		return false;
171 
172 	ie = find_ie(WLAN_EID_MESH_CONFIG,
173 		     a->information_elements,
174 		     a->len_information_elements);
175 	if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
176 		return false;
177 
178 	/*
179 	 * Ignore mesh capability (last two bytes of the IE) when
180 	 * comparing since that may differ between stations taking
181 	 * part in the same mesh.
182 	 */
183 	return memcmp(ie + 2, meshcfg, IEEE80211_MESH_CONFIG_LEN - 2) == 0;
184 }
185 
186 static int cmp_bss(struct cfg80211_bss *a,
187 		   struct cfg80211_bss *b)
188 {
189 	int r;
190 
191 	if (a->channel != b->channel)
192 		return b->channel->center_freq - a->channel->center_freq;
193 
194 	r = memcmp(a->bssid, b->bssid, ETH_ALEN);
195 	if (r)
196 		return r;
197 
198 	if (is_zero_ether_addr(a->bssid)) {
199 		r = cmp_ies(WLAN_EID_MESH_ID,
200 			    a->information_elements,
201 			    a->len_information_elements,
202 			    b->information_elements,
203 			    b->len_information_elements);
204 		if (r)
205 			return r;
206 		return cmp_ies(WLAN_EID_MESH_CONFIG,
207 			       a->information_elements,
208 			       a->len_information_elements,
209 			       b->information_elements,
210 			       b->len_information_elements);
211 	}
212 
213 	return cmp_ies(WLAN_EID_SSID,
214 		       a->information_elements,
215 		       a->len_information_elements,
216 		       b->information_elements,
217 		       b->len_information_elements);
218 }
219 
220 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
221 				      struct ieee80211_channel *channel,
222 				      const u8 *bssid,
223 				      const u8 *ssid, size_t ssid_len,
224 				      u16 capa_mask, u16 capa_val)
225 {
226 	struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
227 	struct cfg80211_internal_bss *bss, *res = NULL;
228 
229 	spin_lock_bh(&dev->bss_lock);
230 
231 	list_for_each_entry(bss, &dev->bss_list, list) {
232 		if ((bss->pub.capability & capa_mask) != capa_val)
233 			continue;
234 		if (channel && bss->pub.channel != channel)
235 			continue;
236 		if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
237 			res = bss;
238 			kref_get(&res->ref);
239 			break;
240 		}
241 	}
242 
243 	spin_unlock_bh(&dev->bss_lock);
244 	if (!res)
245 		return NULL;
246 	return &res->pub;
247 }
248 EXPORT_SYMBOL(cfg80211_get_bss);
249 
250 struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
251 				       struct ieee80211_channel *channel,
252 				       const u8 *meshid, size_t meshidlen,
253 				       const u8 *meshcfg)
254 {
255 	struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
256 	struct cfg80211_internal_bss *bss, *res = NULL;
257 
258 	spin_lock_bh(&dev->bss_lock);
259 
260 	list_for_each_entry(bss, &dev->bss_list, list) {
261 		if (channel && bss->pub.channel != channel)
262 			continue;
263 		if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
264 			res = bss;
265 			kref_get(&res->ref);
266 			break;
267 		}
268 	}
269 
270 	spin_unlock_bh(&dev->bss_lock);
271 	if (!res)
272 		return NULL;
273 	return &res->pub;
274 }
275 EXPORT_SYMBOL(cfg80211_get_mesh);
276 
277 
278 static void rb_insert_bss(struct cfg80211_registered_device *dev,
279 			  struct cfg80211_internal_bss *bss)
280 {
281 	struct rb_node **p = &dev->bss_tree.rb_node;
282 	struct rb_node *parent = NULL;
283 	struct cfg80211_internal_bss *tbss;
284 	int cmp;
285 
286 	while (*p) {
287 		parent = *p;
288 		tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
289 
290 		cmp = cmp_bss(&bss->pub, &tbss->pub);
291 
292 		if (WARN_ON(!cmp)) {
293 			/* will sort of leak this BSS */
294 			return;
295 		}
296 
297 		if (cmp < 0)
298 			p = &(*p)->rb_left;
299 		else
300 			p = &(*p)->rb_right;
301 	}
302 
303 	rb_link_node(&bss->rbn, parent, p);
304 	rb_insert_color(&bss->rbn, &dev->bss_tree);
305 }
306 
307 static struct cfg80211_internal_bss *
308 rb_find_bss(struct cfg80211_registered_device *dev,
309 	    struct cfg80211_internal_bss *res)
310 {
311 	struct rb_node *n = dev->bss_tree.rb_node;
312 	struct cfg80211_internal_bss *bss;
313 	int r;
314 
315 	while (n) {
316 		bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
317 		r = cmp_bss(&res->pub, &bss->pub);
318 
319 		if (r == 0)
320 			return bss;
321 		else if (r < 0)
322 			n = n->rb_left;
323 		else
324 			n = n->rb_right;
325 	}
326 
327 	return NULL;
328 }
329 
330 static struct cfg80211_internal_bss *
331 cfg80211_bss_update(struct cfg80211_registered_device *dev,
332 		    struct cfg80211_internal_bss *res,
333 		    bool overwrite)
334 {
335 	struct cfg80211_internal_bss *found = NULL;
336 	const u8 *meshid, *meshcfg;
337 
338 	/*
339 	 * The reference to "res" is donated to this function.
340 	 */
341 
342 	if (WARN_ON(!res->pub.channel)) {
343 		kref_put(&res->ref, bss_release);
344 		return NULL;
345 	}
346 
347 	res->ts = jiffies;
348 
349 	if (is_zero_ether_addr(res->pub.bssid)) {
350 		/* must be mesh, verify */
351 		meshid = find_ie(WLAN_EID_MESH_ID, res->pub.information_elements,
352 				 res->pub.len_information_elements);
353 		meshcfg = find_ie(WLAN_EID_MESH_CONFIG,
354 				  res->pub.information_elements,
355 				  res->pub.len_information_elements);
356 		if (!meshid || !meshcfg ||
357 		    meshcfg[1] != IEEE80211_MESH_CONFIG_LEN) {
358 			/* bogus mesh */
359 			kref_put(&res->ref, bss_release);
360 			return NULL;
361 		}
362 	}
363 
364 	spin_lock_bh(&dev->bss_lock);
365 
366 	found = rb_find_bss(dev, res);
367 
368 	if (found) {
369 		found->pub.beacon_interval = res->pub.beacon_interval;
370 		found->pub.tsf = res->pub.tsf;
371 		found->pub.signal = res->pub.signal;
372 		found->pub.capability = res->pub.capability;
373 		found->ts = res->ts;
374 
375 		/* overwrite IEs */
376 		if (overwrite) {
377 			size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
378 			size_t ielen = res->pub.len_information_elements;
379 
380 			if (!found->ies_allocated && ksize(found) >= used + ielen) {
381 				memcpy(found->pub.information_elements,
382 				       res->pub.information_elements, ielen);
383 				found->pub.len_information_elements = ielen;
384 			} else {
385 				u8 *ies = found->pub.information_elements;
386 
387 				if (found->ies_allocated)
388 					ies = krealloc(ies, ielen, GFP_ATOMIC);
389 				else
390 					ies = kmalloc(ielen, GFP_ATOMIC);
391 
392 				if (ies) {
393 					memcpy(ies, res->pub.information_elements, ielen);
394 					found->ies_allocated = true;
395 					found->pub.information_elements = ies;
396 					found->pub.len_information_elements = ielen;
397 				}
398 			}
399 		}
400 
401 		kref_put(&res->ref, bss_release);
402 	} else {
403 		/* this "consumes" the reference */
404 		list_add_tail(&res->list, &dev->bss_list);
405 		rb_insert_bss(dev, res);
406 		found = res;
407 	}
408 
409 	dev->bss_generation++;
410 	spin_unlock_bh(&dev->bss_lock);
411 
412 	kref_get(&found->ref);
413 	return found;
414 }
415 
416 struct cfg80211_bss*
417 cfg80211_inform_bss(struct wiphy *wiphy,
418 		    struct ieee80211_channel *channel,
419 		    const u8 *bssid,
420 		    u64 timestamp, u16 capability, u16 beacon_interval,
421 		    const u8 *ie, size_t ielen,
422 		    s32 signal, gfp_t gfp)
423 {
424 	struct cfg80211_internal_bss *res;
425 	size_t privsz;
426 
427 	if (WARN_ON(!wiphy))
428 		return NULL;
429 
430 	privsz = wiphy->bss_priv_size;
431 
432 	if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
433 			(signal < 0 || signal > 100)))
434 		return NULL;
435 
436 	res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
437 	if (!res)
438 		return NULL;
439 
440 	memcpy(res->pub.bssid, bssid, ETH_ALEN);
441 	res->pub.channel = channel;
442 	res->pub.signal = signal;
443 	res->pub.tsf = timestamp;
444 	res->pub.beacon_interval = beacon_interval;
445 	res->pub.capability = capability;
446 	/* point to after the private area */
447 	res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
448 	memcpy(res->pub.information_elements, ie, ielen);
449 	res->pub.len_information_elements = ielen;
450 
451 	kref_init(&res->ref);
452 
453 	res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, 0);
454 	if (!res)
455 		return NULL;
456 
457 	if (res->pub.capability & WLAN_CAPABILITY_ESS)
458 		regulatory_hint_found_beacon(wiphy, channel, gfp);
459 
460 	/* cfg80211_bss_update gives us a referenced result */
461 	return &res->pub;
462 }
463 EXPORT_SYMBOL(cfg80211_inform_bss);
464 
465 struct cfg80211_bss *
466 cfg80211_inform_bss_frame(struct wiphy *wiphy,
467 			  struct ieee80211_channel *channel,
468 			  struct ieee80211_mgmt *mgmt, size_t len,
469 			  s32 signal, gfp_t gfp)
470 {
471 	struct cfg80211_internal_bss *res;
472 	size_t ielen = len - offsetof(struct ieee80211_mgmt,
473 				      u.probe_resp.variable);
474 	bool overwrite;
475 	size_t privsz = wiphy->bss_priv_size;
476 
477 	if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
478 	            (signal < 0 || signal > 100)))
479 		return NULL;
480 
481 	if (WARN_ON(!mgmt || !wiphy ||
482 		    len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
483 		return NULL;
484 
485 	res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
486 	if (!res)
487 		return NULL;
488 
489 	memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
490 	res->pub.channel = channel;
491 	res->pub.signal = signal;
492 	res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
493 	res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
494 	res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
495 	/* point to after the private area */
496 	res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
497 	memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen);
498 	res->pub.len_information_elements = ielen;
499 
500 	kref_init(&res->ref);
501 
502 	overwrite = ieee80211_is_probe_resp(mgmt->frame_control);
503 
504 	res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite);
505 	if (!res)
506 		return NULL;
507 
508 	if (res->pub.capability & WLAN_CAPABILITY_ESS)
509 		regulatory_hint_found_beacon(wiphy, channel, gfp);
510 
511 	/* cfg80211_bss_update gives us a referenced result */
512 	return &res->pub;
513 }
514 EXPORT_SYMBOL(cfg80211_inform_bss_frame);
515 
516 void cfg80211_put_bss(struct cfg80211_bss *pub)
517 {
518 	struct cfg80211_internal_bss *bss;
519 
520 	if (!pub)
521 		return;
522 
523 	bss = container_of(pub, struct cfg80211_internal_bss, pub);
524 	kref_put(&bss->ref, bss_release);
525 }
526 EXPORT_SYMBOL(cfg80211_put_bss);
527 
528 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
529 {
530 	struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
531 	struct cfg80211_internal_bss *bss;
532 
533 	if (WARN_ON(!pub))
534 		return;
535 
536 	bss = container_of(pub, struct cfg80211_internal_bss, pub);
537 
538 	spin_lock_bh(&dev->bss_lock);
539 
540 	list_del(&bss->list);
541 	rb_erase(&bss->rbn, &dev->bss_tree);
542 
543 	spin_unlock_bh(&dev->bss_lock);
544 
545 	kref_put(&bss->ref, bss_release);
546 }
547 EXPORT_SYMBOL(cfg80211_unlink_bss);
548 
549 void cfg80211_hold_bss(struct cfg80211_bss *pub)
550 {
551 	struct cfg80211_internal_bss *bss;
552 
553 	if (!pub)
554 		return;
555 
556 	bss = container_of(pub, struct cfg80211_internal_bss, pub);
557 	bss->hold = true;
558 }
559 EXPORT_SYMBOL(cfg80211_hold_bss);
560 
561 void cfg80211_unhold_bss(struct cfg80211_bss *pub)
562 {
563 	struct cfg80211_internal_bss *bss;
564 
565 	if (!pub)
566 		return;
567 
568 	bss = container_of(pub, struct cfg80211_internal_bss, pub);
569 	bss->hold = false;
570 }
571 EXPORT_SYMBOL(cfg80211_unhold_bss);
572 
573 #ifdef CONFIG_WIRELESS_EXT
574 int cfg80211_wext_siwscan(struct net_device *dev,
575 			  struct iw_request_info *info,
576 			  union iwreq_data *wrqu, char *extra)
577 {
578 	struct cfg80211_registered_device *rdev;
579 	struct wiphy *wiphy;
580 	struct iw_scan_req *wreq = NULL;
581 	struct cfg80211_scan_request *creq;
582 	int i, err, n_channels = 0;
583 	enum ieee80211_band band;
584 
585 	if (!netif_running(dev))
586 		return -ENETDOWN;
587 
588 	rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
589 
590 	if (IS_ERR(rdev))
591 		return PTR_ERR(rdev);
592 
593 	if (rdev->scan_req) {
594 		err = -EBUSY;
595 		goto out;
596 	}
597 
598 	wiphy = &rdev->wiphy;
599 
600 	for (band = 0; band < IEEE80211_NUM_BANDS; band++)
601 		if (wiphy->bands[band])
602 			n_channels += wiphy->bands[band]->n_channels;
603 
604 	creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
605 		       n_channels * sizeof(void *),
606 		       GFP_ATOMIC);
607 	if (!creq) {
608 		err = -ENOMEM;
609 		goto out;
610 	}
611 
612 	creq->wiphy = wiphy;
613 	creq->ifidx = dev->ifindex;
614 	creq->ssids = (void *)(creq + 1);
615 	creq->channels = (void *)(creq->ssids + 1);
616 	creq->n_channels = n_channels;
617 	creq->n_ssids = 1;
618 
619 	/* all channels */
620 	i = 0;
621 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
622 		int j;
623 		if (!wiphy->bands[band])
624 			continue;
625 		for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
626 			creq->channels[i] = &wiphy->bands[band]->channels[j];
627 			i++;
628 		}
629 	}
630 
631 	/* translate scan request */
632 	if (wrqu->data.length == sizeof(struct iw_scan_req)) {
633 		wreq = (struct iw_scan_req *)extra;
634 
635 		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
636 			if (wreq->essid_len > IEEE80211_MAX_SSID_LEN)
637 				return -EINVAL;
638 			memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
639 			creq->ssids[0].ssid_len = wreq->essid_len;
640 		}
641 		if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
642 			creq->n_ssids = 0;
643 	}
644 
645 	rdev->scan_req = creq;
646 	err = rdev->ops->scan(wiphy, dev, creq);
647 	if (err) {
648 		rdev->scan_req = NULL;
649 		kfree(creq);
650 	}
651  out:
652 	cfg80211_put_dev(rdev);
653 	return err;
654 }
655 EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
656 
657 static void ieee80211_scan_add_ies(struct iw_request_info *info,
658 				   struct cfg80211_bss *bss,
659 				   char **current_ev, char *end_buf)
660 {
661 	u8 *pos, *end, *next;
662 	struct iw_event iwe;
663 
664 	if (!bss->information_elements ||
665 	    !bss->len_information_elements)
666 		return;
667 
668 	/*
669 	 * If needed, fragment the IEs buffer (at IE boundaries) into short
670 	 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
671 	 */
672 	pos = bss->information_elements;
673 	end = pos + bss->len_information_elements;
674 
675 	while (end - pos > IW_GENERIC_IE_MAX) {
676 		next = pos + 2 + pos[1];
677 		while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
678 			next = next + 2 + next[1];
679 
680 		memset(&iwe, 0, sizeof(iwe));
681 		iwe.cmd = IWEVGENIE;
682 		iwe.u.data.length = next - pos;
683 		*current_ev = iwe_stream_add_point(info, *current_ev,
684 						   end_buf, &iwe, pos);
685 
686 		pos = next;
687 	}
688 
689 	if (end > pos) {
690 		memset(&iwe, 0, sizeof(iwe));
691 		iwe.cmd = IWEVGENIE;
692 		iwe.u.data.length = end - pos;
693 		*current_ev = iwe_stream_add_point(info, *current_ev,
694 						   end_buf, &iwe, pos);
695 	}
696 }
697 
698 static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
699 {
700 	unsigned long end = jiffies;
701 
702 	if (end >= start)
703 		return jiffies_to_msecs(end - start);
704 
705 	return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
706 }
707 
708 static char *
709 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
710 	      struct cfg80211_internal_bss *bss, char *current_ev,
711 	      char *end_buf)
712 {
713 	struct iw_event iwe;
714 	u8 *buf, *cfg, *p;
715 	u8 *ie = bss->pub.information_elements;
716 	int rem = bss->pub.len_information_elements, i, sig;
717 	bool ismesh = false;
718 
719 	memset(&iwe, 0, sizeof(iwe));
720 	iwe.cmd = SIOCGIWAP;
721 	iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
722 	memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
723 	current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
724 					  IW_EV_ADDR_LEN);
725 
726 	memset(&iwe, 0, sizeof(iwe));
727 	iwe.cmd = SIOCGIWFREQ;
728 	iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
729 	iwe.u.freq.e = 0;
730 	current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
731 					  IW_EV_FREQ_LEN);
732 
733 	memset(&iwe, 0, sizeof(iwe));
734 	iwe.cmd = SIOCGIWFREQ;
735 	iwe.u.freq.m = bss->pub.channel->center_freq;
736 	iwe.u.freq.e = 6;
737 	current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
738 					  IW_EV_FREQ_LEN);
739 
740 	if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
741 		memset(&iwe, 0, sizeof(iwe));
742 		iwe.cmd = IWEVQUAL;
743 		iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
744 				     IW_QUAL_NOISE_INVALID |
745 				     IW_QUAL_QUAL_UPDATED;
746 		switch (wiphy->signal_type) {
747 		case CFG80211_SIGNAL_TYPE_MBM:
748 			sig = bss->pub.signal / 100;
749 			iwe.u.qual.level = sig;
750 			iwe.u.qual.updated |= IW_QUAL_DBM;
751 			if (sig < -110)		/* rather bad */
752 				sig = -110;
753 			else if (sig > -40)	/* perfect */
754 				sig = -40;
755 			/* will give a range of 0 .. 70 */
756 			iwe.u.qual.qual = sig + 110;
757 			break;
758 		case CFG80211_SIGNAL_TYPE_UNSPEC:
759 			iwe.u.qual.level = bss->pub.signal;
760 			/* will give range 0 .. 100 */
761 			iwe.u.qual.qual = bss->pub.signal;
762 			break;
763 		default:
764 			/* not reached */
765 			break;
766 		}
767 		current_ev = iwe_stream_add_event(info, current_ev, end_buf,
768 						  &iwe, IW_EV_QUAL_LEN);
769 	}
770 
771 	memset(&iwe, 0, sizeof(iwe));
772 	iwe.cmd = SIOCGIWENCODE;
773 	if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
774 		iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
775 	else
776 		iwe.u.data.flags = IW_ENCODE_DISABLED;
777 	iwe.u.data.length = 0;
778 	current_ev = iwe_stream_add_point(info, current_ev, end_buf,
779 					  &iwe, "");
780 
781 	while (rem >= 2) {
782 		/* invalid data */
783 		if (ie[1] > rem - 2)
784 			break;
785 
786 		switch (ie[0]) {
787 		case WLAN_EID_SSID:
788 			memset(&iwe, 0, sizeof(iwe));
789 			iwe.cmd = SIOCGIWESSID;
790 			iwe.u.data.length = ie[1];
791 			iwe.u.data.flags = 1;
792 			current_ev = iwe_stream_add_point(info, current_ev, end_buf,
793 							  &iwe, ie + 2);
794 			break;
795 		case WLAN_EID_MESH_ID:
796 			memset(&iwe, 0, sizeof(iwe));
797 			iwe.cmd = SIOCGIWESSID;
798 			iwe.u.data.length = ie[1];
799 			iwe.u.data.flags = 1;
800 			current_ev = iwe_stream_add_point(info, current_ev, end_buf,
801 							  &iwe, ie + 2);
802 			break;
803 		case WLAN_EID_MESH_CONFIG:
804 			ismesh = true;
805 			if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
806 				break;
807 			buf = kmalloc(50, GFP_ATOMIC);
808 			if (!buf)
809 				break;
810 			cfg = ie + 2;
811 			memset(&iwe, 0, sizeof(iwe));
812 			iwe.cmd = IWEVCUSTOM;
813 			sprintf(buf, "Mesh network (version %d)", cfg[0]);
814 			iwe.u.data.length = strlen(buf);
815 			current_ev = iwe_stream_add_point(info, current_ev,
816 							  end_buf,
817 							  &iwe, buf);
818 			sprintf(buf, "Path Selection Protocol ID: "
819 				"0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
820 							cfg[4]);
821 			iwe.u.data.length = strlen(buf);
822 			current_ev = iwe_stream_add_point(info, current_ev,
823 							  end_buf,
824 							  &iwe, buf);
825 			sprintf(buf, "Path Selection Metric ID: "
826 				"0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
827 							cfg[8]);
828 			iwe.u.data.length = strlen(buf);
829 			current_ev = iwe_stream_add_point(info, current_ev,
830 							  end_buf,
831 							  &iwe, buf);
832 			sprintf(buf, "Congestion Control Mode ID: "
833 				"0x%02X%02X%02X%02X", cfg[9], cfg[10],
834 							cfg[11], cfg[12]);
835 			iwe.u.data.length = strlen(buf);
836 			current_ev = iwe_stream_add_point(info, current_ev,
837 							  end_buf,
838 							  &iwe, buf);
839 			sprintf(buf, "Channel Precedence: "
840 				"0x%02X%02X%02X%02X", cfg[13], cfg[14],
841 							cfg[15], cfg[16]);
842 			iwe.u.data.length = strlen(buf);
843 			current_ev = iwe_stream_add_point(info, current_ev,
844 							  end_buf,
845 							  &iwe, buf);
846 			kfree(buf);
847 			break;
848 		case WLAN_EID_SUPP_RATES:
849 		case WLAN_EID_EXT_SUPP_RATES:
850 			/* display all supported rates in readable format */
851 			p = current_ev + iwe_stream_lcp_len(info);
852 
853 			memset(&iwe, 0, sizeof(iwe));
854 			iwe.cmd = SIOCGIWRATE;
855 			/* Those two flags are ignored... */
856 			iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
857 
858 			for (i = 0; i < ie[1]; i++) {
859 				iwe.u.bitrate.value =
860 					((ie[i + 2] & 0x7f) * 500000);
861 				p = iwe_stream_add_value(info, current_ev, p,
862 						end_buf, &iwe, IW_EV_PARAM_LEN);
863 			}
864 			current_ev = p;
865 			break;
866 		}
867 		rem -= ie[1] + 2;
868 		ie += ie[1] + 2;
869 	}
870 
871 	if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
872 	    || ismesh) {
873 		memset(&iwe, 0, sizeof(iwe));
874 		iwe.cmd = SIOCGIWMODE;
875 		if (ismesh)
876 			iwe.u.mode = IW_MODE_MESH;
877 		else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
878 			iwe.u.mode = IW_MODE_MASTER;
879 		else
880 			iwe.u.mode = IW_MODE_ADHOC;
881 		current_ev = iwe_stream_add_event(info, current_ev, end_buf,
882 						  &iwe, IW_EV_UINT_LEN);
883 	}
884 
885 	buf = kmalloc(30, GFP_ATOMIC);
886 	if (buf) {
887 		memset(&iwe, 0, sizeof(iwe));
888 		iwe.cmd = IWEVCUSTOM;
889 		sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
890 		iwe.u.data.length = strlen(buf);
891 		current_ev = iwe_stream_add_point(info, current_ev, end_buf,
892 						  &iwe, buf);
893 		memset(&iwe, 0, sizeof(iwe));
894 		iwe.cmd = IWEVCUSTOM;
895 		sprintf(buf, " Last beacon: %ums ago",
896 			elapsed_jiffies_msecs(bss->ts));
897 		iwe.u.data.length = strlen(buf);
898 		current_ev = iwe_stream_add_point(info, current_ev,
899 						  end_buf, &iwe, buf);
900 		kfree(buf);
901 	}
902 
903 	ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
904 
905 	return current_ev;
906 }
907 
908 
909 static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
910 				  struct iw_request_info *info,
911 				  char *buf, size_t len)
912 {
913 	char *current_ev = buf;
914 	char *end_buf = buf + len;
915 	struct cfg80211_internal_bss *bss;
916 
917 	spin_lock_bh(&dev->bss_lock);
918 	cfg80211_bss_expire(dev);
919 
920 	list_for_each_entry(bss, &dev->bss_list, list) {
921 		if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
922 			spin_unlock_bh(&dev->bss_lock);
923 			return -E2BIG;
924 		}
925 		current_ev = ieee80211_bss(&dev->wiphy, info, bss,
926 					   current_ev, end_buf);
927 	}
928 	spin_unlock_bh(&dev->bss_lock);
929 	return current_ev - buf;
930 }
931 
932 
933 int cfg80211_wext_giwscan(struct net_device *dev,
934 			  struct iw_request_info *info,
935 			  struct iw_point *data, char *extra)
936 {
937 	struct cfg80211_registered_device *rdev;
938 	int res;
939 
940 	if (!netif_running(dev))
941 		return -ENETDOWN;
942 
943 	rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
944 
945 	if (IS_ERR(rdev))
946 		return PTR_ERR(rdev);
947 
948 	if (rdev->scan_req) {
949 		res = -EAGAIN;
950 		goto out;
951 	}
952 
953 	res = ieee80211_scan_results(rdev, info, extra, data->length);
954 	data->length = 0;
955 	if (res >= 0) {
956 		data->length = res;
957 		res = 0;
958 	}
959 
960  out:
961 	cfg80211_put_dev(rdev);
962 	return res;
963 }
964 EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
965 #endif
966