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