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