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