cfg80211: fix whitespace in scan handling
[linux-2.6-block.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>
5a0e3ad6 7#include <linux/slab.h>
2a519311
JB
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>
262eb9b2 15#include <net/cfg80211-wext.h>
2a519311
JB
16#include <net/iw_handler.h>
17#include "core.h"
18#include "nl80211.h"
a9a11622 19#include "wext-compat.h"
e35e4d28 20#include "rdev-ops.h"
2a519311 21
f9616e0f 22#define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
2a519311 23
e8e27c66
AK
24static void bss_release(struct kref *ref)
25{
26 struct cfg80211_internal_bss *bss;
27
28 bss = container_of(ref, struct cfg80211_internal_bss, ref);
b629ea3d
JB
29
30 if (WARN_ON(atomic_read(&bss->hold)))
31 return;
32
e8e27c66
AK
33 if (bss->pub.free_priv)
34 bss->pub.free_priv(&bss->pub);
35
36 if (bss->beacon_ies_allocated)
37 kfree(bss->pub.beacon_ies);
38 if (bss->proberesp_ies_allocated)
39 kfree(bss->pub.proberesp_ies);
40
e8e27c66
AK
41 kfree(bss);
42}
43
44/* must hold dev->bss_lock! */
45static void __cfg80211_unlink_bss(struct cfg80211_registered_device *dev,
46 struct cfg80211_internal_bss *bss)
47{
48 list_del_init(&bss->list);
49 rb_erase(&bss->rbn, &dev->bss_tree);
50 kref_put(&bss->ref, bss_release);
51}
52
15d6030b
SL
53/* must hold dev->bss_lock! */
54static void __cfg80211_bss_expire(struct cfg80211_registered_device *dev,
55 unsigned long expire_time)
56{
57 struct cfg80211_internal_bss *bss, *tmp;
58 bool expired = false;
59
60 list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
61 if (atomic_read(&bss->hold))
62 continue;
63 if (!time_after(expire_time, bss->ts))
64 continue;
65
66 __cfg80211_unlink_bss(dev, bss);
67 expired = true;
68 }
69
70 if (expired)
71 dev->bss_generation++;
72}
73
01a0ac41 74void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak)
2a519311 75{
667503dd 76 struct cfg80211_scan_request *request;
fd014284 77 struct wireless_dev *wdev;
3d23e349 78#ifdef CONFIG_CFG80211_WEXT
2a519311
JB
79 union iwreq_data wrqu;
80#endif
81
01a0ac41
JB
82 ASSERT_RDEV_LOCK(rdev);
83
667503dd
JB
84 request = rdev->scan_req;
85
01a0ac41
JB
86 if (!request)
87 return;
88
fd014284 89 wdev = request->wdev;
2a519311 90
6829c878
JB
91 /*
92 * This must be before sending the other events!
93 * Otherwise, wpa_supplicant gets completely confused with
94 * wext events.
95 */
fd014284
JB
96 if (wdev->netdev)
97 cfg80211_sme_scan_done(wdev->netdev);
6829c878 98
15d6030b 99 if (request->aborted) {
fd014284 100 nl80211_send_scan_aborted(rdev, wdev);
15d6030b
SL
101 } else {
102 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
103 /* flush entries from previous scans */
104 spin_lock_bh(&rdev->bss_lock);
105 __cfg80211_bss_expire(rdev, request->scan_start);
106 spin_unlock_bh(&rdev->bss_lock);
107 }
fd014284 108 nl80211_send_scan_done(rdev, wdev);
15d6030b 109 }
2a519311 110
3d23e349 111#ifdef CONFIG_CFG80211_WEXT
fd014284 112 if (wdev->netdev && !request->aborted) {
2a519311
JB
113 memset(&wrqu, 0, sizeof(wrqu));
114
fd014284 115 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
2a519311
JB
116 }
117#endif
118
fd014284
JB
119 if (wdev->netdev)
120 dev_put(wdev->netdev);
2a519311 121
36e6fea8 122 rdev->scan_req = NULL;
01a0ac41
JB
123
124 /*
125 * OK. If this is invoked with "leak" then we can't
126 * free this ... but we've cleaned it up anyway. The
127 * driver failed to call the scan_done callback, so
128 * all bets are off, it might still be trying to use
129 * the scan request or not ... if it accesses the dev
130 * in there (it shouldn't anyway) then it may crash.
131 */
132 if (!leak)
133 kfree(request);
2a519311 134}
667503dd 135
36e6fea8
JB
136void __cfg80211_scan_done(struct work_struct *wk)
137{
138 struct cfg80211_registered_device *rdev;
139
140 rdev = container_of(wk, struct cfg80211_registered_device,
141 scan_done_wk);
142
143 cfg80211_lock_rdev(rdev);
01a0ac41 144 ___cfg80211_scan_done(rdev, false);
36e6fea8
JB
145 cfg80211_unlock_rdev(rdev);
146}
147
667503dd
JB
148void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
149{
4ee3e063 150 trace_cfg80211_scan_done(request, aborted);
667503dd
JB
151 WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
152
153 request->aborted = aborted;
e60d7443 154 queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk);
667503dd 155}
2a519311
JB
156EXPORT_SYMBOL(cfg80211_scan_done);
157
807f8a8c
LC
158void __cfg80211_sched_scan_results(struct work_struct *wk)
159{
160 struct cfg80211_registered_device *rdev;
15d6030b 161 struct cfg80211_sched_scan_request *request;
807f8a8c
LC
162
163 rdev = container_of(wk, struct cfg80211_registered_device,
164 sched_scan_results_wk);
165
15d6030b
SL
166 request = rdev->sched_scan_req;
167
c10841ca 168 mutex_lock(&rdev->sched_scan_mtx);
807f8a8c
LC
169
170 /* we don't have sched_scan_req anymore if the scan is stopping */
15d6030b
SL
171 if (request) {
172 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
173 /* flush entries from previous scans */
174 spin_lock_bh(&rdev->bss_lock);
175 __cfg80211_bss_expire(rdev, request->scan_start);
176 spin_unlock_bh(&rdev->bss_lock);
177 request->scan_start =
178 jiffies + msecs_to_jiffies(request->interval);
179 }
180 nl80211_send_sched_scan_results(rdev, request->dev);
181 }
807f8a8c 182
c10841ca 183 mutex_unlock(&rdev->sched_scan_mtx);
807f8a8c
LC
184}
185
186void cfg80211_sched_scan_results(struct wiphy *wiphy)
187{
4ee3e063 188 trace_cfg80211_sched_scan_results(wiphy);
807f8a8c
LC
189 /* ignore if we're not scanning */
190 if (wiphy_to_dev(wiphy)->sched_scan_req)
191 queue_work(cfg80211_wq,
192 &wiphy_to_dev(wiphy)->sched_scan_results_wk);
193}
194EXPORT_SYMBOL(cfg80211_sched_scan_results);
195
85a9994a 196void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
807f8a8c 197{
85a9994a 198 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
807f8a8c 199
4ee3e063
BL
200 trace_cfg80211_sched_scan_stopped(wiphy);
201
c10841ca 202 mutex_lock(&rdev->sched_scan_mtx);
807f8a8c 203 __cfg80211_stop_sched_scan(rdev, true);
c10841ca 204 mutex_unlock(&rdev->sched_scan_mtx);
807f8a8c 205}
807f8a8c
LC
206EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
207
208int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
209 bool driver_initiated)
210{
807f8a8c
LC
211 struct net_device *dev;
212
c10841ca 213 lockdep_assert_held(&rdev->sched_scan_mtx);
807f8a8c
LC
214
215 if (!rdev->sched_scan_req)
1a84ff75 216 return -ENOENT;
807f8a8c
LC
217
218 dev = rdev->sched_scan_req->dev;
219
85a9994a 220 if (!driver_initiated) {
e35e4d28 221 int err = rdev_sched_scan_stop(rdev, dev);
85a9994a
LC
222 if (err)
223 return err;
224 }
807f8a8c
LC
225
226 nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED);
227
228 kfree(rdev->sched_scan_req);
229 rdev->sched_scan_req = NULL;
230
3b4670ff 231 return 0;
807f8a8c
LC
232}
233
cb3a8eec
DW
234/* must hold dev->bss_lock! */
235void cfg80211_bss_age(struct cfg80211_registered_device *dev,
236 unsigned long age_secs)
237{
238 struct cfg80211_internal_bss *bss;
239 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
240
915de2ff 241 list_for_each_entry(bss, &dev->bss_list, list)
cb3a8eec 242 bss->ts -= age_jiffies;
cb3a8eec
DW
243}
244
2a519311
JB
245void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
246{
15d6030b 247 __cfg80211_bss_expire(dev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
2a519311
JB
248}
249
c21dbf92 250const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
2a519311 251{
c21dbf92 252 while (len > 2 && ies[0] != eid) {
2a519311
JB
253 len -= ies[1] + 2;
254 ies += ies[1] + 2;
255 }
256 if (len < 2)
257 return NULL;
258 if (len < 2 + ies[1])
259 return NULL;
260 return ies;
261}
c21dbf92 262EXPORT_SYMBOL(cfg80211_find_ie);
2a519311 263
0c28ec58
EP
264const u8 *cfg80211_find_vendor_ie(unsigned int oui, u8 oui_type,
265 const u8 *ies, int len)
266{
267 struct ieee80211_vendor_ie *ie;
268 const u8 *pos = ies, *end = ies + len;
269 int ie_oui;
270
271 while (pos < end) {
272 pos = cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC, pos,
273 end - pos);
274 if (!pos)
275 return NULL;
276
277 if (end - pos < sizeof(*ie))
278 return NULL;
279
280 ie = (struct ieee80211_vendor_ie *)pos;
281 ie_oui = ie->oui[0] << 16 | ie->oui[1] << 8 | ie->oui[2];
282 if (ie_oui == oui && ie->oui_type == oui_type)
283 return pos;
284
285 pos += 2 + ie->len;
286 }
287 return NULL;
288}
289EXPORT_SYMBOL(cfg80211_find_vendor_ie);
290
2a519311
JB
291static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
292{
c21dbf92
JB
293 const u8 *ie1 = cfg80211_find_ie(num, ies1, len1);
294 const u8 *ie2 = cfg80211_find_ie(num, ies2, len2);
2a519311 295
3b6ef633 296 /* equal if both missing */
2a519311
JB
297 if (!ie1 && !ie2)
298 return 0;
3b6ef633
JB
299 /* sort missing IE before (left of) present IE */
300 if (!ie1)
2a519311 301 return -1;
3b6ef633
JB
302 if (!ie2)
303 return 1;
2a519311 304
3b6ef633
JB
305 /* sort by length first, then by contents */
306 if (ie1[1] != ie2[1])
2a519311 307 return ie2[1] - ie1[1];
3b6ef633 308 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
2a519311
JB
309}
310
915de2ff 311static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
2a519311
JB
312 const u8 *ssid, size_t ssid_len)
313{
314 const u8 *ssidie;
315
ac422d3c 316 if (bssid && !ether_addr_equal(a->bssid, bssid))
2a519311
JB
317 return false;
318
79420f09
JB
319 if (!ssid)
320 return true;
321
c21dbf92
JB
322 ssidie = cfg80211_find_ie(WLAN_EID_SSID,
323 a->information_elements,
324 a->len_information_elements);
2a519311
JB
325 if (!ssidie)
326 return false;
327 if (ssidie[1] != ssid_len)
328 return false;
329 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
330}
331
333ba732
EP
332static bool is_mesh_bss(struct cfg80211_bss *a)
333{
334 const u8 *ie;
335
336 if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
337 return false;
338
339 ie = cfg80211_find_ie(WLAN_EID_MESH_ID,
340 a->information_elements,
341 a->len_information_elements);
342 if (!ie)
343 return false;
344
345 ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
346 a->information_elements,
347 a->len_information_elements);
348 if (!ie)
349 return false;
350
351 return true;
352}
353
2a519311
JB
354static bool is_mesh(struct cfg80211_bss *a,
355 const u8 *meshid, size_t meshidlen,
356 const u8 *meshcfg)
357{
358 const u8 *ie;
359
333ba732 360 if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
2a519311
JB
361 return false;
362
c21dbf92
JB
363 ie = cfg80211_find_ie(WLAN_EID_MESH_ID,
364 a->information_elements,
365 a->len_information_elements);
2a519311
JB
366 if (!ie)
367 return false;
368 if (ie[1] != meshidlen)
369 return false;
370 if (memcmp(ie + 2, meshid, meshidlen))
371 return false;
372
c21dbf92
JB
373 ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
374 a->information_elements,
375 a->len_information_elements);
cd3468ba
JB
376 if (!ie)
377 return false;
136cfa28 378 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
2a519311
JB
379 return false;
380
381 /*
382 * Ignore mesh capability (last two bytes of the IE) when
383 * comparing since that may differ between stations taking
384 * part in the same mesh.
385 */
136cfa28 386 return memcmp(ie + 2, meshcfg,
915de2ff 387 sizeof(struct ieee80211_meshconf_ie) - 2) == 0;
2a519311
JB
388}
389
915de2ff 390static int cmp_bss_core(struct cfg80211_bss *a, struct cfg80211_bss *b)
2a519311
JB
391{
392 int r;
393
394 if (a->channel != b->channel)
395 return b->channel->center_freq - a->channel->center_freq;
396
333ba732 397 if (is_mesh_bss(a) && is_mesh_bss(b)) {
2a519311
JB
398 r = cmp_ies(WLAN_EID_MESH_ID,
399 a->information_elements,
400 a->len_information_elements,
401 b->information_elements,
402 b->len_information_elements);
403 if (r)
404 return r;
405 return cmp_ies(WLAN_EID_MESH_CONFIG,
406 a->information_elements,
407 a->len_information_elements,
408 b->information_elements,
409 b->len_information_elements);
410 }
411
ef9456a8
EG
412 /*
413 * we can't use compare_ether_addr here since we need a < > operator.
414 * The binary return value of compare_ether_addr isn't enough
415 */
416 return memcmp(a->bssid, b->bssid, sizeof(a->bssid));
dd9dfb9f
DT
417}
418
419static int cmp_bss(struct cfg80211_bss *a,
420 struct cfg80211_bss *b)
421{
422 int r;
423
424 r = cmp_bss_core(a, b);
0a35d36d
JC
425 if (r)
426 return r;
427
2a519311
JB
428 return cmp_ies(WLAN_EID_SSID,
429 a->information_elements,
430 a->len_information_elements,
431 b->information_elements,
432 b->len_information_elements);
433}
434
915de2ff 435static int cmp_hidden_bss(struct cfg80211_bss *a, struct cfg80211_bss *b)
dd9dfb9f
DT
436{
437 const u8 *ie1;
438 const u8 *ie2;
439 int i;
440 int r;
441
442 r = cmp_bss_core(a, b);
443 if (r)
444 return r;
445
446 ie1 = cfg80211_find_ie(WLAN_EID_SSID,
915de2ff
JB
447 a->information_elements,
448 a->len_information_elements);
dd9dfb9f 449 ie2 = cfg80211_find_ie(WLAN_EID_SSID,
915de2ff
JB
450 b->information_elements,
451 b->len_information_elements);
dd9dfb9f
DT
452
453 /* Key comparator must use same algorithm in any rb-tree
454 * search function (order is important), otherwise ordering
455 * of items in the tree is broken and search gives incorrect
456 * results. This code uses same order as cmp_ies() does. */
457
458 /* sort missing IE before (left of) present IE */
459 if (!ie1)
460 return -1;
461 if (!ie2)
462 return 1;
463
464 /* zero-size SSID is used as an indication of the hidden bss */
465 if (!ie2[1])
466 return 0;
467
468 /* sort by length first, then by contents */
469 if (ie1[1] != ie2[1])
470 return ie2[1] - ie1[1];
471
472 /* zeroed SSID ie is another indication of a hidden bss */
473 for (i = 0; i < ie2[1]; i++)
474 if (ie2[i + 2])
475 return -1;
476
477 return 0;
478}
479
2a519311
JB
480struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
481 struct ieee80211_channel *channel,
482 const u8 *bssid,
79420f09
JB
483 const u8 *ssid, size_t ssid_len,
484 u16 capa_mask, u16 capa_val)
2a519311
JB
485{
486 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
487 struct cfg80211_internal_bss *bss, *res = NULL;
ccb6c136 488 unsigned long now = jiffies;
2a519311 489
4ee3e063
BL
490 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, capa_mask,
491 capa_val);
492
2a519311
JB
493 spin_lock_bh(&dev->bss_lock);
494
495 list_for_each_entry(bss, &dev->bss_list, list) {
79420f09
JB
496 if ((bss->pub.capability & capa_mask) != capa_val)
497 continue;
2a519311
JB
498 if (channel && bss->pub.channel != channel)
499 continue;
ccb6c136
JB
500 /* Don't get expired BSS structs */
501 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
502 !atomic_read(&bss->hold))
503 continue;
2a519311
JB
504 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
505 res = bss;
506 kref_get(&res->ref);
507 break;
508 }
509 }
510
511 spin_unlock_bh(&dev->bss_lock);
512 if (!res)
513 return NULL;
4ee3e063 514 trace_cfg80211_return_bss(&res->pub);
2a519311
JB
515 return &res->pub;
516}
517EXPORT_SYMBOL(cfg80211_get_bss);
518
519struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
520 struct ieee80211_channel *channel,
521 const u8 *meshid, size_t meshidlen,
522 const u8 *meshcfg)
523{
524 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
525 struct cfg80211_internal_bss *bss, *res = NULL;
526
527 spin_lock_bh(&dev->bss_lock);
528
529 list_for_each_entry(bss, &dev->bss_list, list) {
530 if (channel && bss->pub.channel != channel)
531 continue;
532 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
533 res = bss;
534 kref_get(&res->ref);
535 break;
536 }
537 }
538
539 spin_unlock_bh(&dev->bss_lock);
540 if (!res)
541 return NULL;
542 return &res->pub;
543}
544EXPORT_SYMBOL(cfg80211_get_mesh);
545
546
547static void rb_insert_bss(struct cfg80211_registered_device *dev,
548 struct cfg80211_internal_bss *bss)
549{
550 struct rb_node **p = &dev->bss_tree.rb_node;
551 struct rb_node *parent = NULL;
552 struct cfg80211_internal_bss *tbss;
553 int cmp;
554
555 while (*p) {
556 parent = *p;
557 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
558
559 cmp = cmp_bss(&bss->pub, &tbss->pub);
560
561 if (WARN_ON(!cmp)) {
562 /* will sort of leak this BSS */
563 return;
564 }
565
566 if (cmp < 0)
567 p = &(*p)->rb_left;
568 else
569 p = &(*p)->rb_right;
570 }
571
572 rb_link_node(&bss->rbn, parent, p);
573 rb_insert_color(&bss->rbn, &dev->bss_tree);
574}
575
576static struct cfg80211_internal_bss *
577rb_find_bss(struct cfg80211_registered_device *dev,
578 struct cfg80211_internal_bss *res)
579{
580 struct rb_node *n = dev->bss_tree.rb_node;
581 struct cfg80211_internal_bss *bss;
582 int r;
583
584 while (n) {
585 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
586 r = cmp_bss(&res->pub, &bss->pub);
587
588 if (r == 0)
589 return bss;
590 else if (r < 0)
591 n = n->rb_left;
592 else
593 n = n->rb_right;
594 }
595
596 return NULL;
597}
598
dd9dfb9f
DT
599static struct cfg80211_internal_bss *
600rb_find_hidden_bss(struct cfg80211_registered_device *dev,
915de2ff 601 struct cfg80211_internal_bss *res)
dd9dfb9f
DT
602{
603 struct rb_node *n = dev->bss_tree.rb_node;
604 struct cfg80211_internal_bss *bss;
605 int r;
606
607 while (n) {
608 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
609 r = cmp_hidden_bss(&res->pub, &bss->pub);
610
611 if (r == 0)
612 return bss;
613 else if (r < 0)
614 n = n->rb_left;
615 else
616 n = n->rb_right;
617 }
618
619 return NULL;
620}
621
622static void
623copy_hidden_ies(struct cfg80211_internal_bss *res,
915de2ff 624 struct cfg80211_internal_bss *hidden)
dd9dfb9f
DT
625{
626 if (unlikely(res->pub.beacon_ies))
627 return;
628 if (WARN_ON(!hidden->pub.beacon_ies))
629 return;
630
631 res->pub.beacon_ies = kmalloc(hidden->pub.len_beacon_ies, GFP_ATOMIC);
632 if (unlikely(!res->pub.beacon_ies))
633 return;
634
635 res->beacon_ies_allocated = true;
636 res->pub.len_beacon_ies = hidden->pub.len_beacon_ies;
637 memcpy(res->pub.beacon_ies, hidden->pub.beacon_ies,
915de2ff 638 res->pub.len_beacon_ies);
dd9dfb9f
DT
639}
640
2a519311
JB
641static struct cfg80211_internal_bss *
642cfg80211_bss_update(struct cfg80211_registered_device *dev,
34a6eddb 643 struct cfg80211_internal_bss *res)
2a519311
JB
644{
645 struct cfg80211_internal_bss *found = NULL;
2a519311
JB
646
647 /*
648 * The reference to "res" is donated to this function.
649 */
650
651 if (WARN_ON(!res->pub.channel)) {
652 kref_put(&res->ref, bss_release);
653 return NULL;
654 }
655
656 res->ts = jiffies;
657
2a519311
JB
658 spin_lock_bh(&dev->bss_lock);
659
660 found = rb_find_bss(dev, res);
661
cd1658f5 662 if (found) {
2a519311
JB
663 found->pub.beacon_interval = res->pub.beacon_interval;
664 found->pub.tsf = res->pub.tsf;
665 found->pub.signal = res->pub.signal;
2a519311
JB
666 found->pub.capability = res->pub.capability;
667 found->ts = res->ts;
cd1658f5 668
34a6eddb
JM
669 /* Update IEs */
670 if (res->pub.proberesp_ies) {
cd1658f5 671 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
34a6eddb
JM
672 size_t ielen = res->pub.len_proberesp_ies;
673
674 if (found->pub.proberesp_ies &&
675 !found->proberesp_ies_allocated &&
676 ksize(found) >= used + ielen) {
677 memcpy(found->pub.proberesp_ies,
678 res->pub.proberesp_ies, ielen);
679 found->pub.len_proberesp_ies = ielen;
680 } else {
681 u8 *ies = found->pub.proberesp_ies;
682
683 if (found->proberesp_ies_allocated)
684 ies = krealloc(ies, ielen, GFP_ATOMIC);
685 else
686 ies = kmalloc(ielen, GFP_ATOMIC);
687
688 if (ies) {
689 memcpy(ies, res->pub.proberesp_ies,
690 ielen);
691 found->proberesp_ies_allocated = true;
692 found->pub.proberesp_ies = ies;
693 found->pub.len_proberesp_ies = ielen;
694 }
695 }
cd1658f5 696
34a6eddb
JM
697 /* Override possible earlier Beacon frame IEs */
698 found->pub.information_elements =
699 found->pub.proberesp_ies;
700 found->pub.len_information_elements =
701 found->pub.len_proberesp_ies;
702 }
915de2ff 703
34a6eddb
JM
704 if (res->pub.beacon_ies) {
705 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
706 size_t ielen = res->pub.len_beacon_ies;
01123e23
SN
707 bool information_elements_is_beacon_ies =
708 (found->pub.information_elements ==
709 found->pub.beacon_ies);
34a6eddb
JM
710
711 if (found->pub.beacon_ies &&
712 !found->beacon_ies_allocated &&
713 ksize(found) >= used + ielen) {
714 memcpy(found->pub.beacon_ies,
715 res->pub.beacon_ies, ielen);
716 found->pub.len_beacon_ies = ielen;
cd1658f5 717 } else {
34a6eddb 718 u8 *ies = found->pub.beacon_ies;
cd1658f5 719
34a6eddb 720 if (found->beacon_ies_allocated)
273de92c
MB
721 ies = krealloc(ies, ielen, GFP_ATOMIC);
722 else
cd1658f5
JB
723 ies = kmalloc(ielen, GFP_ATOMIC);
724
725 if (ies) {
34a6eddb
JM
726 memcpy(ies, res->pub.beacon_ies,
727 ielen);
728 found->beacon_ies_allocated = true;
729 found->pub.beacon_ies = ies;
730 found->pub.len_beacon_ies = ielen;
cd1658f5
JB
731 }
732 }
01123e23
SN
733
734 /* Override IEs if they were from a beacon before */
735 if (information_elements_is_beacon_ies) {
736 found->pub.information_elements =
737 found->pub.beacon_ies;
738 found->pub.len_information_elements =
739 found->pub.len_beacon_ies;
740 }
cd1658f5
JB
741 }
742
2a519311
JB
743 kref_put(&res->ref, bss_release);
744 } else {
dd9dfb9f
DT
745 struct cfg80211_internal_bss *hidden;
746
747 /* First check if the beacon is a probe response from
748 * a hidden bss. If so, copy beacon ies (with nullified
749 * ssid) into the probe response bss entry (with real ssid).
750 * It is required basically for PSM implementation
751 * (probe responses do not contain tim ie) */
752
753 /* TODO: The code is not trying to update existing probe
754 * response bss entries when beacon ies are
755 * getting changed. */
756 hidden = rb_find_hidden_bss(dev, res);
757 if (hidden)
758 copy_hidden_ies(res, hidden);
759
2a519311
JB
760 /* this "consumes" the reference */
761 list_add_tail(&res->list, &dev->bss_list);
762 rb_insert_bss(dev, res);
763 found = res;
764 }
765
766 dev->bss_generation++;
767 spin_unlock_bh(&dev->bss_lock);
768
769 kref_get(&found->ref);
770 return found;
771}
772
0172bb75
JB
773static struct ieee80211_channel *
774cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
775 struct ieee80211_channel *channel)
776{
777 const u8 *tmp;
778 u32 freq;
779 int channel_number = -1;
780
781 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
782 if (tmp && tmp[1] == 1) {
783 channel_number = tmp[2];
784 } else {
785 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
786 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
787 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
788
789 channel_number = htop->primary_chan;
790 }
791 }
792
793 if (channel_number < 0)
794 return channel;
795
796 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
797 channel = ieee80211_get_channel(wiphy, freq);
798 if (!channel)
799 return NULL;
800 if (channel->flags & IEEE80211_CHAN_DISABLED)
801 return NULL;
802 return channel;
803}
804
06aa7afa
JK
805struct cfg80211_bss*
806cfg80211_inform_bss(struct wiphy *wiphy,
807 struct ieee80211_channel *channel,
7b8bcff2
JB
808 const u8 *bssid, u64 tsf, u16 capability,
809 u16 beacon_interval, const u8 *ie, size_t ielen,
06aa7afa
JK
810 s32 signal, gfp_t gfp)
811{
812 struct cfg80211_internal_bss *res;
813 size_t privsz;
814
815 if (WARN_ON(!wiphy))
816 return NULL;
817
818 privsz = wiphy->bss_priv_size;
819
22fe88d3 820 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
06aa7afa
JK
821 (signal < 0 || signal > 100)))
822 return NULL;
823
0172bb75
JB
824 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, channel);
825 if (!channel)
826 return NULL;
827
06aa7afa
JK
828 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
829 if (!res)
830 return NULL;
831
832 memcpy(res->pub.bssid, bssid, ETH_ALEN);
833 res->pub.channel = channel;
834 res->pub.signal = signal;
7b8bcff2 835 res->pub.tsf = tsf;
06aa7afa
JK
836 res->pub.beacon_interval = beacon_interval;
837 res->pub.capability = capability;
34a6eddb
JM
838 /*
839 * Since we do not know here whether the IEs are from a Beacon or Probe
840 * Response frame, we need to pick one of the options and only use it
841 * with the driver that does not provide the full Beacon/Probe Response
842 * frame. Use Beacon frame pointer to avoid indicating that this should
843 * override the information_elements pointer should we have received an
844 * earlier indication of Probe Response data.
845 *
846 * The initial buffer for the IEs is allocated with the BSS entry and
847 * is located after the private area.
848 */
849 res->pub.beacon_ies = (u8 *)res + sizeof(*res) + privsz;
850 memcpy(res->pub.beacon_ies, ie, ielen);
851 res->pub.len_beacon_ies = ielen;
852 res->pub.information_elements = res->pub.beacon_ies;
853 res->pub.len_information_elements = res->pub.len_beacon_ies;
06aa7afa
JK
854
855 kref_init(&res->ref);
856
34a6eddb 857 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
06aa7afa
JK
858 if (!res)
859 return NULL;
860
861 if (res->pub.capability & WLAN_CAPABILITY_ESS)
862 regulatory_hint_found_beacon(wiphy, channel, gfp);
863
4ee3e063 864 trace_cfg80211_return_bss(&res->pub);
06aa7afa
JK
865 /* cfg80211_bss_update gives us a referenced result */
866 return &res->pub;
867}
868EXPORT_SYMBOL(cfg80211_inform_bss);
869
2a519311
JB
870struct cfg80211_bss *
871cfg80211_inform_bss_frame(struct wiphy *wiphy,
872 struct ieee80211_channel *channel,
873 struct ieee80211_mgmt *mgmt, size_t len,
77965c97 874 s32 signal, gfp_t gfp)
2a519311
JB
875{
876 struct cfg80211_internal_bss *res;
877 size_t ielen = len - offsetof(struct ieee80211_mgmt,
878 u.probe_resp.variable);
bef9bacc
MK
879 size_t privsz;
880
0172bb75
JB
881 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
882 offsetof(struct ieee80211_mgmt, u.beacon.variable));
883
4ee3e063
BL
884 trace_cfg80211_inform_bss_frame(wiphy, channel, mgmt, len, signal);
885
bef9bacc
MK
886 if (WARN_ON(!mgmt))
887 return NULL;
888
889 if (WARN_ON(!wiphy))
890 return NULL;
2a519311 891
22fe88d3 892 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
768be59f 893 (signal < 0 || signal > 100)))
2a519311
JB
894 return NULL;
895
bef9bacc 896 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
2a519311
JB
897 return NULL;
898
bef9bacc
MK
899 privsz = wiphy->bss_priv_size;
900
0172bb75
JB
901 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
902 ielen, channel);
903 if (!channel)
904 return NULL;
905
2a519311
JB
906 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
907 if (!res)
908 return NULL;
909
910 memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
911 res->pub.channel = channel;
2a519311
JB
912 res->pub.signal = signal;
913 res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
914 res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
915 res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
34a6eddb
JM
916 /*
917 * The initial buffer for the IEs is allocated with the BSS entry and
918 * is located after the private area.
919 */
920 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
921 res->pub.proberesp_ies = (u8 *) res + sizeof(*res) + privsz;
922 memcpy(res->pub.proberesp_ies, mgmt->u.probe_resp.variable,
923 ielen);
924 res->pub.len_proberesp_ies = ielen;
925 res->pub.information_elements = res->pub.proberesp_ies;
926 res->pub.len_information_elements = res->pub.len_proberesp_ies;
927 } else {
928 res->pub.beacon_ies = (u8 *) res + sizeof(*res) + privsz;
929 memcpy(res->pub.beacon_ies, mgmt->u.beacon.variable, ielen);
930 res->pub.len_beacon_ies = ielen;
931 res->pub.information_elements = res->pub.beacon_ies;
932 res->pub.len_information_elements = res->pub.len_beacon_ies;
933 }
2a519311
JB
934
935 kref_init(&res->ref);
936
34a6eddb 937 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
2a519311
JB
938 if (!res)
939 return NULL;
940
e38f8a7a
LR
941 if (res->pub.capability & WLAN_CAPABILITY_ESS)
942 regulatory_hint_found_beacon(wiphy, channel, gfp);
943
4ee3e063 944 trace_cfg80211_return_bss(&res->pub);
2a519311
JB
945 /* cfg80211_bss_update gives us a referenced result */
946 return &res->pub;
947}
948EXPORT_SYMBOL(cfg80211_inform_bss_frame);
949
4c0c0b75
JB
950void cfg80211_ref_bss(struct cfg80211_bss *pub)
951{
952 struct cfg80211_internal_bss *bss;
953
954 if (!pub)
955 return;
956
957 bss = container_of(pub, struct cfg80211_internal_bss, pub);
958 kref_get(&bss->ref);
959}
960EXPORT_SYMBOL(cfg80211_ref_bss);
961
2a519311
JB
962void cfg80211_put_bss(struct cfg80211_bss *pub)
963{
964 struct cfg80211_internal_bss *bss;
965
966 if (!pub)
967 return;
968
969 bss = container_of(pub, struct cfg80211_internal_bss, pub);
970 kref_put(&bss->ref, bss_release);
971}
972EXPORT_SYMBOL(cfg80211_put_bss);
973
d491af19
JB
974void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
975{
976 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
977 struct cfg80211_internal_bss *bss;
978
979 if (WARN_ON(!pub))
980 return;
981
982 bss = container_of(pub, struct cfg80211_internal_bss, pub);
983
984 spin_lock_bh(&dev->bss_lock);
3207390a 985 if (!list_empty(&bss->list)) {
2b78ac9b 986 __cfg80211_unlink_bss(dev, bss);
3207390a 987 dev->bss_generation++;
3207390a 988 }
d491af19 989 spin_unlock_bh(&dev->bss_lock);
d491af19
JB
990}
991EXPORT_SYMBOL(cfg80211_unlink_bss);
992
3d23e349 993#ifdef CONFIG_CFG80211_WEXT
2a519311
JB
994int cfg80211_wext_siwscan(struct net_device *dev,
995 struct iw_request_info *info,
996 union iwreq_data *wrqu, char *extra)
997{
998 struct cfg80211_registered_device *rdev;
999 struct wiphy *wiphy;
1000 struct iw_scan_req *wreq = NULL;
65486c8b 1001 struct cfg80211_scan_request *creq = NULL;
2a519311
JB
1002 int i, err, n_channels = 0;
1003 enum ieee80211_band band;
1004
1005 if (!netif_running(dev))
1006 return -ENETDOWN;
1007
b2e3abdc
HS
1008 if (wrqu->data.length == sizeof(struct iw_scan_req))
1009 wreq = (struct iw_scan_req *)extra;
1010
463d0183 1011 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2a519311
JB
1012
1013 if (IS_ERR(rdev))
1014 return PTR_ERR(rdev);
1015
1016 if (rdev->scan_req) {
1017 err = -EBUSY;
1018 goto out;
1019 }
1020
1021 wiphy = &rdev->wiphy;
1022
b2e3abdc
HS
1023 /* Determine number of channels, needed to allocate creq */
1024 if (wreq && wreq->num_channels)
1025 n_channels = wreq->num_channels;
1026 else {
1027 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1028 if (wiphy->bands[band])
1029 n_channels += wiphy->bands[band]->n_channels;
1030 }
2a519311
JB
1031
1032 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1033 n_channels * sizeof(void *),
1034 GFP_ATOMIC);
1035 if (!creq) {
1036 err = -ENOMEM;
1037 goto out;
1038 }
1039
1040 creq->wiphy = wiphy;
fd014284 1041 creq->wdev = dev->ieee80211_ptr;
5ba63533
JB
1042 /* SSIDs come after channels */
1043 creq->ssids = (void *)&creq->channels[n_channels];
2a519311
JB
1044 creq->n_channels = n_channels;
1045 creq->n_ssids = 1;
15d6030b 1046 creq->scan_start = jiffies;
2a519311 1047
b2e3abdc 1048 /* translate "Scan on frequencies" request */
2a519311
JB
1049 i = 0;
1050 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1051 int j;
584991dc 1052
2a519311
JB
1053 if (!wiphy->bands[band])
1054 continue;
584991dc 1055
2a519311 1056 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
584991dc
JB
1057 /* ignore disabled channels */
1058 if (wiphy->bands[band]->channels[j].flags &
1059 IEEE80211_CHAN_DISABLED)
1060 continue;
b2e3abdc
HS
1061
1062 /* If we have a wireless request structure and the
1063 * wireless request specifies frequencies, then search
1064 * for the matching hardware channel.
1065 */
1066 if (wreq && wreq->num_channels) {
1067 int k;
1068 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1069 for (k = 0; k < wreq->num_channels; k++) {
a4e7b730 1070 int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
b2e3abdc
HS
1071 if (wext_freq == wiphy_freq)
1072 goto wext_freq_found;
1073 }
1074 goto wext_freq_not_found;
1075 }
1076
1077 wext_freq_found:
2a519311
JB
1078 creq->channels[i] = &wiphy->bands[band]->channels[j];
1079 i++;
b2e3abdc 1080 wext_freq_not_found: ;
2a519311
JB
1081 }
1082 }
8862dc5f
HS
1083 /* No channels found? */
1084 if (!i) {
1085 err = -EINVAL;
1086 goto out;
1087 }
2a519311 1088
b2e3abdc
HS
1089 /* Set real number of channels specified in creq->channels[] */
1090 creq->n_channels = i;
2a519311 1091
b2e3abdc
HS
1092 /* translate "Scan for SSID" request */
1093 if (wreq) {
2a519311 1094 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
65486c8b
JB
1095 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1096 err = -EINVAL;
1097 goto out;
1098 }
2a519311
JB
1099 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1100 creq->ssids[0].ssid_len = wreq->essid_len;
1101 }
1102 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1103 creq->n_ssids = 0;
1104 }
1105
34850ab2 1106 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
a401d2bb
JB
1107 if (wiphy->bands[i])
1108 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
34850ab2 1109
2a519311 1110 rdev->scan_req = creq;
e35e4d28 1111 err = rdev_scan(rdev, creq);
2a519311
JB
1112 if (err) {
1113 rdev->scan_req = NULL;
65486c8b 1114 /* creq will be freed below */
463d0183 1115 } else {
fd014284 1116 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
65486c8b
JB
1117 /* creq now owned by driver */
1118 creq = NULL;
463d0183
JB
1119 dev_hold(dev);
1120 }
2a519311 1121 out:
65486c8b 1122 kfree(creq);
4d0c8aea 1123 cfg80211_unlock_rdev(rdev);
2a519311
JB
1124 return err;
1125}
ba44cb72 1126EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
2a519311
JB
1127
1128static void ieee80211_scan_add_ies(struct iw_request_info *info,
1129 struct cfg80211_bss *bss,
1130 char **current_ev, char *end_buf)
1131{
1132 u8 *pos, *end, *next;
1133 struct iw_event iwe;
1134
1135 if (!bss->information_elements ||
1136 !bss->len_information_elements)
1137 return;
1138
1139 /*
1140 * If needed, fragment the IEs buffer (at IE boundaries) into short
1141 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1142 */
1143 pos = bss->information_elements;
1144 end = pos + bss->len_information_elements;
1145
1146 while (end - pos > IW_GENERIC_IE_MAX) {
1147 next = pos + 2 + pos[1];
1148 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1149 next = next + 2 + next[1];
1150
1151 memset(&iwe, 0, sizeof(iwe));
1152 iwe.cmd = IWEVGENIE;
1153 iwe.u.data.length = next - pos;
1154 *current_ev = iwe_stream_add_point(info, *current_ev,
1155 end_buf, &iwe, pos);
1156
1157 pos = next;
1158 }
1159
1160 if (end > pos) {
1161 memset(&iwe, 0, sizeof(iwe));
1162 iwe.cmd = IWEVGENIE;
1163 iwe.u.data.length = end - pos;
1164 *current_ev = iwe_stream_add_point(info, *current_ev,
1165 end_buf, &iwe, pos);
1166 }
1167}
1168
cb3a8eec
DW
1169static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
1170{
1171 unsigned long end = jiffies;
1172
1173 if (end >= start)
1174 return jiffies_to_msecs(end - start);
1175
1176 return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
1177}
2a519311
JB
1178
1179static char *
77965c97
JB
1180ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1181 struct cfg80211_internal_bss *bss, char *current_ev,
1182 char *end_buf)
2a519311
JB
1183{
1184 struct iw_event iwe;
1185 u8 *buf, *cfg, *p;
1186 u8 *ie = bss->pub.information_elements;
a77b8552 1187 int rem = bss->pub.len_information_elements, i, sig;
2a519311
JB
1188 bool ismesh = false;
1189
1190 memset(&iwe, 0, sizeof(iwe));
1191 iwe.cmd = SIOCGIWAP;
1192 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1193 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
1194 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1195 IW_EV_ADDR_LEN);
1196
1197 memset(&iwe, 0, sizeof(iwe));
1198 iwe.cmd = SIOCGIWFREQ;
1199 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
1200 iwe.u.freq.e = 0;
1201 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1202 IW_EV_FREQ_LEN);
1203
1204 memset(&iwe, 0, sizeof(iwe));
1205 iwe.cmd = SIOCGIWFREQ;
1206 iwe.u.freq.m = bss->pub.channel->center_freq;
1207 iwe.u.freq.e = 6;
1208 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1209 IW_EV_FREQ_LEN);
1210
77965c97 1211 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2a519311
JB
1212 memset(&iwe, 0, sizeof(iwe));
1213 iwe.cmd = IWEVQUAL;
1214 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
1215 IW_QUAL_NOISE_INVALID |
a77b8552 1216 IW_QUAL_QUAL_UPDATED;
77965c97 1217 switch (wiphy->signal_type) {
2a519311 1218 case CFG80211_SIGNAL_TYPE_MBM:
a77b8552
JB
1219 sig = bss->pub.signal / 100;
1220 iwe.u.qual.level = sig;
2a519311 1221 iwe.u.qual.updated |= IW_QUAL_DBM;
a77b8552
JB
1222 if (sig < -110) /* rather bad */
1223 sig = -110;
1224 else if (sig > -40) /* perfect */
1225 sig = -40;
1226 /* will give a range of 0 .. 70 */
1227 iwe.u.qual.qual = sig + 110;
2a519311
JB
1228 break;
1229 case CFG80211_SIGNAL_TYPE_UNSPEC:
1230 iwe.u.qual.level = bss->pub.signal;
a77b8552
JB
1231 /* will give range 0 .. 100 */
1232 iwe.u.qual.qual = bss->pub.signal;
2a519311
JB
1233 break;
1234 default:
1235 /* not reached */
1236 break;
1237 }
1238 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1239 &iwe, IW_EV_QUAL_LEN);
1240 }
1241
1242 memset(&iwe, 0, sizeof(iwe));
1243 iwe.cmd = SIOCGIWENCODE;
1244 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
1245 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1246 else
1247 iwe.u.data.flags = IW_ENCODE_DISABLED;
1248 iwe.u.data.length = 0;
1249 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1250 &iwe, "");
1251
1252 while (rem >= 2) {
1253 /* invalid data */
1254 if (ie[1] > rem - 2)
1255 break;
1256
1257 switch (ie[0]) {
1258 case WLAN_EID_SSID:
1259 memset(&iwe, 0, sizeof(iwe));
1260 iwe.cmd = SIOCGIWESSID;
1261 iwe.u.data.length = ie[1];
1262 iwe.u.data.flags = 1;
1263 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1264 &iwe, ie + 2);
1265 break;
1266 case WLAN_EID_MESH_ID:
1267 memset(&iwe, 0, sizeof(iwe));
1268 iwe.cmd = SIOCGIWESSID;
1269 iwe.u.data.length = ie[1];
1270 iwe.u.data.flags = 1;
1271 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1272 &iwe, ie + 2);
1273 break;
1274 case WLAN_EID_MESH_CONFIG:
1275 ismesh = true;
136cfa28 1276 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
2a519311
JB
1277 break;
1278 buf = kmalloc(50, GFP_ATOMIC);
1279 if (!buf)
1280 break;
1281 cfg = ie + 2;
1282 memset(&iwe, 0, sizeof(iwe));
1283 iwe.cmd = IWEVCUSTOM;
76aa5e70
RP
1284 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1285 "0x%02X", cfg[0]);
2a519311
JB
1286 iwe.u.data.length = strlen(buf);
1287 current_ev = iwe_stream_add_point(info, current_ev,
1288 end_buf,
1289 &iwe, buf);
76aa5e70
RP
1290 sprintf(buf, "Path Selection Metric ID: 0x%02X",
1291 cfg[1]);
2a519311
JB
1292 iwe.u.data.length = strlen(buf);
1293 current_ev = iwe_stream_add_point(info, current_ev,
1294 end_buf,
1295 &iwe, buf);
76aa5e70
RP
1296 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1297 cfg[2]);
2a519311
JB
1298 iwe.u.data.length = strlen(buf);
1299 current_ev = iwe_stream_add_point(info, current_ev,
1300 end_buf,
1301 &iwe, buf);
76aa5e70 1302 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
2a519311
JB
1303 iwe.u.data.length = strlen(buf);
1304 current_ev = iwe_stream_add_point(info, current_ev,
1305 end_buf,
1306 &iwe, buf);
76aa5e70
RP
1307 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
1308 iwe.u.data.length = strlen(buf);
1309 current_ev = iwe_stream_add_point(info, current_ev,
1310 end_buf,
1311 &iwe, buf);
1312 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1313 iwe.u.data.length = strlen(buf);
1314 current_ev = iwe_stream_add_point(info, current_ev,
1315 end_buf,
1316 &iwe, buf);
1317 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
2a519311
JB
1318 iwe.u.data.length = strlen(buf);
1319 current_ev = iwe_stream_add_point(info, current_ev,
1320 end_buf,
1321 &iwe, buf);
1322 kfree(buf);
1323 break;
1324 case WLAN_EID_SUPP_RATES:
1325 case WLAN_EID_EXT_SUPP_RATES:
1326 /* display all supported rates in readable format */
1327 p = current_ev + iwe_stream_lcp_len(info);
1328
1329 memset(&iwe, 0, sizeof(iwe));
1330 iwe.cmd = SIOCGIWRATE;
1331 /* Those two flags are ignored... */
1332 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1333
1334 for (i = 0; i < ie[1]; i++) {
1335 iwe.u.bitrate.value =
1336 ((ie[i + 2] & 0x7f) * 500000);
1337 p = iwe_stream_add_value(info, current_ev, p,
1338 end_buf, &iwe, IW_EV_PARAM_LEN);
1339 }
1340 current_ev = p;
1341 break;
1342 }
1343 rem -= ie[1] + 2;
1344 ie += ie[1] + 2;
1345 }
1346
f64f9e71
JP
1347 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1348 ismesh) {
2a519311
JB
1349 memset(&iwe, 0, sizeof(iwe));
1350 iwe.cmd = SIOCGIWMODE;
1351 if (ismesh)
1352 iwe.u.mode = IW_MODE_MESH;
1353 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
1354 iwe.u.mode = IW_MODE_MASTER;
1355 else
1356 iwe.u.mode = IW_MODE_ADHOC;
1357 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1358 &iwe, IW_EV_UINT_LEN);
1359 }
1360
1361 buf = kmalloc(30, GFP_ATOMIC);
1362 if (buf) {
1363 memset(&iwe, 0, sizeof(iwe));
1364 iwe.cmd = IWEVCUSTOM;
1365 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
1366 iwe.u.data.length = strlen(buf);
1367 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1368 &iwe, buf);
1369 memset(&iwe, 0, sizeof(iwe));
1370 iwe.cmd = IWEVCUSTOM;
cb3a8eec
DW
1371 sprintf(buf, " Last beacon: %ums ago",
1372 elapsed_jiffies_msecs(bss->ts));
2a519311
JB
1373 iwe.u.data.length = strlen(buf);
1374 current_ev = iwe_stream_add_point(info, current_ev,
1375 end_buf, &iwe, buf);
1376 kfree(buf);
1377 }
1378
1379 ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
1380
1381 return current_ev;
1382}
1383
1384
1385static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
1386 struct iw_request_info *info,
1387 char *buf, size_t len)
1388{
1389 char *current_ev = buf;
1390 char *end_buf = buf + len;
1391 struct cfg80211_internal_bss *bss;
1392
1393 spin_lock_bh(&dev->bss_lock);
1394 cfg80211_bss_expire(dev);
1395
1396 list_for_each_entry(bss, &dev->bss_list, list) {
1397 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1398 spin_unlock_bh(&dev->bss_lock);
1399 return -E2BIG;
1400 }
77965c97
JB
1401 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
1402 current_ev, end_buf);
2a519311
JB
1403 }
1404 spin_unlock_bh(&dev->bss_lock);
1405 return current_ev - buf;
1406}
1407
1408
1409int cfg80211_wext_giwscan(struct net_device *dev,
1410 struct iw_request_info *info,
1411 struct iw_point *data, char *extra)
1412{
1413 struct cfg80211_registered_device *rdev;
1414 int res;
1415
1416 if (!netif_running(dev))
1417 return -ENETDOWN;
1418
463d0183 1419 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2a519311
JB
1420
1421 if (IS_ERR(rdev))
1422 return PTR_ERR(rdev);
1423
1424 if (rdev->scan_req) {
1425 res = -EAGAIN;
1426 goto out;
1427 }
1428
1429 res = ieee80211_scan_results(rdev, info, extra, data->length);
1430 data->length = 0;
1431 if (res >= 0) {
1432 data->length = res;
1433 res = 0;
1434 }
1435
1436 out:
4d0c8aea 1437 cfg80211_unlock_rdev(rdev);
2a519311
JB
1438 return res;
1439}
ba44cb72 1440EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
2a519311 1441#endif