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