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