mac80211: remove unnecessary BUG_ON()
[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
f9d15d16
JB
164void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
165 bool send_message)
2a519311 166{
667503dd 167 struct cfg80211_scan_request *request;
fd014284 168 struct wireless_dev *wdev;
f9d15d16 169 struct sk_buff *msg;
3d23e349 170#ifdef CONFIG_CFG80211_WEXT
2a519311
JB
171 union iwreq_data wrqu;
172#endif
173
5fe231e8 174 ASSERT_RTNL();
01a0ac41 175
f9d15d16
JB
176 if (rdev->scan_msg) {
177 nl80211_send_scan_result(rdev, rdev->scan_msg);
178 rdev->scan_msg = NULL;
179 return;
180 }
667503dd 181
f9d15d16 182 request = rdev->scan_req;
01a0ac41
JB
183 if (!request)
184 return;
185
fd014284 186 wdev = request->wdev;
2a519311 187
6829c878
JB
188 /*
189 * This must be before sending the other events!
190 * Otherwise, wpa_supplicant gets completely confused with
191 * wext events.
192 */
fd014284
JB
193 if (wdev->netdev)
194 cfg80211_sme_scan_done(wdev->netdev);
6829c878 195
f9d15d16
JB
196 if (!request->aborted &&
197 request->flags & NL80211_SCAN_FLAG_FLUSH) {
198 /* flush entries from previous scans */
199 spin_lock_bh(&rdev->bss_lock);
200 __cfg80211_bss_expire(rdev, request->scan_start);
201 spin_unlock_bh(&rdev->bss_lock);
15d6030b 202 }
2a519311 203
f9d15d16
JB
204 msg = nl80211_build_scan_msg(rdev, wdev, request->aborted);
205
3d23e349 206#ifdef CONFIG_CFG80211_WEXT
fd014284 207 if (wdev->netdev && !request->aborted) {
2a519311
JB
208 memset(&wrqu, 0, sizeof(wrqu));
209
fd014284 210 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
2a519311
JB
211 }
212#endif
213
fd014284
JB
214 if (wdev->netdev)
215 dev_put(wdev->netdev);
2a519311 216
36e6fea8 217 rdev->scan_req = NULL;
4a58e7c3 218 kfree(request);
f9d15d16
JB
219
220 if (!send_message)
221 rdev->scan_msg = msg;
222 else
223 nl80211_send_scan_result(rdev, msg);
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();
f9d15d16 234 ___cfg80211_scan_done(rdev, true);
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
5fe231e8 257 rtnl_lock();
807f8a8c 258
79845c66
JB
259 request = rdev->sched_scan_req;
260
807f8a8c 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
3af6341c
JB
468 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
469 if (r)
470 return r;
471
9caf0364
JB
472 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
473 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
dd9dfb9f 474
5622f5bb
JB
475 if (!ie1 && !ie2)
476 return 0;
477
f94f8b16 478 /*
5622f5bb
JB
479 * Note that with "hide_ssid", the function returns a match if
480 * the already-present BSS ("b") is a hidden SSID beacon for
481 * the new BSS ("a").
f94f8b16 482 */
dd9dfb9f
DT
483
484 /* sort missing IE before (left of) present IE */
485 if (!ie1)
486 return -1;
487 if (!ie2)
488 return 1;
489
4593c4cb
JB
490 switch (mode) {
491 case BSS_CMP_HIDE_ZLEN:
492 /*
493 * In ZLEN mode we assume the BSS entry we're
494 * looking for has a zero-length SSID. So if
495 * the one we're looking at right now has that,
496 * return 0. Otherwise, return the difference
497 * in length, but since we're looking for the
498 * 0-length it's really equivalent to returning
499 * the length of the one we're looking at.
500 *
501 * No content comparison is needed as we assume
502 * the content length is zero.
503 */
504 return ie2[1];
505 case BSS_CMP_REGULAR:
506 default:
507 /* sort by length first, then by contents */
508 if (ie1[1] != ie2[1])
509 return ie2[1] - ie1[1];
5622f5bb 510 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
4593c4cb
JB
511 case BSS_CMP_HIDE_NUL:
512 if (ie1[1] != ie2[1])
513 return ie2[1] - ie1[1];
514 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
515 for (i = 0; i < ie2[1]; i++)
516 if (ie2[i + 2])
517 return -1;
518 return 0;
519 }
dd9dfb9f
DT
520}
521
0e3a39b5 522/* Returned bss is reference counted and must be cleaned up appropriately. */
2a519311
JB
523struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
524 struct ieee80211_channel *channel,
525 const u8 *bssid,
79420f09
JB
526 const u8 *ssid, size_t ssid_len,
527 u16 capa_mask, u16 capa_val)
2a519311
JB
528{
529 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
530 struct cfg80211_internal_bss *bss, *res = NULL;
ccb6c136 531 unsigned long now = jiffies;
2a519311 532
4ee3e063
BL
533 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, capa_mask,
534 capa_val);
535
2a519311
JB
536 spin_lock_bh(&dev->bss_lock);
537
538 list_for_each_entry(bss, &dev->bss_list, list) {
79420f09
JB
539 if ((bss->pub.capability & capa_mask) != capa_val)
540 continue;
2a519311
JB
541 if (channel && bss->pub.channel != channel)
542 continue;
c14a7400
JB
543 if (!is_valid_ether_addr(bss->pub.bssid))
544 continue;
ccb6c136
JB
545 /* Don't get expired BSS structs */
546 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
547 !atomic_read(&bss->hold))
548 continue;
2a519311
JB
549 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
550 res = bss;
776b3580 551 bss_ref_get(dev, res);
2a519311
JB
552 break;
553 }
554 }
555
556 spin_unlock_bh(&dev->bss_lock);
557 if (!res)
558 return NULL;
4ee3e063 559 trace_cfg80211_return_bss(&res->pub);
2a519311
JB
560 return &res->pub;
561}
562EXPORT_SYMBOL(cfg80211_get_bss);
563
2a519311
JB
564static void rb_insert_bss(struct cfg80211_registered_device *dev,
565 struct cfg80211_internal_bss *bss)
566{
567 struct rb_node **p = &dev->bss_tree.rb_node;
568 struct rb_node *parent = NULL;
569 struct cfg80211_internal_bss *tbss;
570 int cmp;
571
572 while (*p) {
573 parent = *p;
574 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
575
4593c4cb 576 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
2a519311
JB
577
578 if (WARN_ON(!cmp)) {
579 /* will sort of leak this BSS */
580 return;
581 }
582
583 if (cmp < 0)
584 p = &(*p)->rb_left;
585 else
586 p = &(*p)->rb_right;
587 }
588
589 rb_link_node(&bss->rbn, parent, p);
590 rb_insert_color(&bss->rbn, &dev->bss_tree);
591}
592
593static struct cfg80211_internal_bss *
594rb_find_bss(struct cfg80211_registered_device *dev,
5622f5bb 595 struct cfg80211_internal_bss *res,
4593c4cb 596 enum bss_compare_mode mode)
dd9dfb9f
DT
597{
598 struct rb_node *n = dev->bss_tree.rb_node;
599 struct cfg80211_internal_bss *bss;
600 int r;
601
602 while (n) {
603 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
4593c4cb 604 r = cmp_bss(&res->pub, &bss->pub, mode);
dd9dfb9f
DT
605
606 if (r == 0)
607 return bss;
608 else if (r < 0)
609 n = n->rb_left;
610 else
611 n = n->rb_right;
612 }
613
614 return NULL;
615}
616
776b3580
JB
617static bool cfg80211_combine_bsses(struct cfg80211_registered_device *dev,
618 struct cfg80211_internal_bss *new)
dd9dfb9f 619{
9caf0364 620 const struct cfg80211_bss_ies *ies;
776b3580
JB
621 struct cfg80211_internal_bss *bss;
622 const u8 *ie;
623 int i, ssidlen;
624 u8 fold = 0;
9caf0364 625
776b3580 626 ies = rcu_access_pointer(new->pub.beacon_ies);
9caf0364 627 if (WARN_ON(!ies))
776b3580 628 return false;
dd9dfb9f 629
776b3580
JB
630 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
631 if (!ie) {
632 /* nothing to do */
633 return true;
634 }
635
636 ssidlen = ie[1];
637 for (i = 0; i < ssidlen; i++)
638 fold |= ie[2 + i];
639
640 if (fold) {
641 /* not a hidden SSID */
642 return true;
643 }
644
645 /* This is the bad part ... */
646
647 list_for_each_entry(bss, &dev->bss_list, list) {
648 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
649 continue;
650 if (bss->pub.channel != new->pub.channel)
651 continue;
dcd6eac1
SW
652 if (bss->pub.scan_width != new->pub.scan_width)
653 continue;
776b3580
JB
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;
776b3580
JB
664 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
665 continue;
666 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
667 list_del(&bss->hidden_list);
668 /* combine them */
669 list_add(&bss->hidden_list, &new->hidden_list);
670 bss->pub.hidden_beacon_bss = &new->pub;
671 new->refcount += bss->refcount;
672 rcu_assign_pointer(bss->pub.beacon_ies,
673 new->pub.beacon_ies);
674 }
675
676 return true;
dd9dfb9f
DT
677}
678
0e3a39b5 679/* Returned bss is reference counted and must be cleaned up appropriately. */
2a519311
JB
680static struct cfg80211_internal_bss *
681cfg80211_bss_update(struct cfg80211_registered_device *dev,
3afc2167
EG
682 struct cfg80211_internal_bss *tmp,
683 bool signal_valid)
2a519311
JB
684{
685 struct cfg80211_internal_bss *found = NULL;
2a519311 686
9caf0364 687 if (WARN_ON(!tmp->pub.channel))
2a519311 688 return NULL;
2a519311 689
9caf0364 690 tmp->ts = jiffies;
2a519311 691
2a519311
JB
692 spin_lock_bh(&dev->bss_lock);
693
9caf0364
JB
694 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
695 spin_unlock_bh(&dev->bss_lock);
696 return NULL;
697 }
698
4593c4cb 699 found = rb_find_bss(dev, tmp, BSS_CMP_REGULAR);
2a519311 700
cd1658f5 701 if (found) {
34a6eddb 702 /* Update IEs */
9caf0364
JB
703 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
704 const struct cfg80211_bss_ies *old;
705
706 old = rcu_access_pointer(found->pub.proberesp_ies);
cd1658f5 707
9caf0364
JB
708 rcu_assign_pointer(found->pub.proberesp_ies,
709 tmp->pub.proberesp_ies);
34a6eddb 710 /* Override possible earlier Beacon frame IEs */
9caf0364
JB
711 rcu_assign_pointer(found->pub.ies,
712 tmp->pub.proberesp_ies);
713 if (old)
714 kfree_rcu((struct cfg80211_bss_ies *)old,
715 rcu_head);
716 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
9537f227 717 const struct cfg80211_bss_ies *old;
776b3580
JB
718 struct cfg80211_internal_bss *bss;
719
720 if (found->pub.hidden_beacon_bss &&
721 !list_empty(&found->hidden_list)) {
1345ee6a
JB
722 const struct cfg80211_bss_ies *f;
723
776b3580
JB
724 /*
725 * The found BSS struct is one of the probe
726 * response members of a group, but we're
727 * receiving a beacon (beacon_ies in the tmp
728 * bss is used). This can only mean that the
729 * AP changed its beacon from not having an
730 * SSID to showing it, which is confusing so
731 * drop this information.
732 */
1345ee6a
JB
733
734 f = rcu_access_pointer(tmp->pub.beacon_ies);
735 kfree_rcu((struct cfg80211_bss_ies *)f,
736 rcu_head);
776b3580
JB
737 goto drop;
738 }
915de2ff 739
9caf0364 740 old = rcu_access_pointer(found->pub.beacon_ies);
9caf0364
JB
741
742 rcu_assign_pointer(found->pub.beacon_ies,
743 tmp->pub.beacon_ies);
01123e23
SN
744
745 /* Override IEs if they were from a beacon before */
9537f227 746 if (old == rcu_access_pointer(found->pub.ies))
9caf0364
JB
747 rcu_assign_pointer(found->pub.ies,
748 tmp->pub.beacon_ies);
cd1658f5 749
776b3580
JB
750 /* Assign beacon IEs to all sub entries */
751 list_for_each_entry(bss, &found->hidden_list,
752 hidden_list) {
753 const struct cfg80211_bss_ies *ies;
754
755 ies = rcu_access_pointer(bss->pub.beacon_ies);
756 WARN_ON(ies != old);
757
758 rcu_assign_pointer(bss->pub.beacon_ies,
759 tmp->pub.beacon_ies);
760 }
761
9caf0364
JB
762 if (old)
763 kfree_rcu((struct cfg80211_bss_ies *)old,
764 rcu_head);
765 }
1345ee6a
JB
766
767 found->pub.beacon_interval = tmp->pub.beacon_interval;
3afc2167
EG
768 /*
769 * don't update the signal if beacon was heard on
770 * adjacent channel.
771 */
772 if (signal_valid)
773 found->pub.signal = tmp->pub.signal;
1345ee6a
JB
774 found->pub.capability = tmp->pub.capability;
775 found->ts = tmp->ts;
2a519311 776 } else {
9caf0364 777 struct cfg80211_internal_bss *new;
dd9dfb9f 778 struct cfg80211_internal_bss *hidden;
9caf0364 779 struct cfg80211_bss_ies *ies;
dd9dfb9f 780
9caf0364
JB
781 /*
782 * create a copy -- the "res" variable that is passed in
783 * is allocated on the stack since it's not needed in the
784 * more common case of an update
785 */
786 new = kzalloc(sizeof(*new) + dev->wiphy.bss_priv_size,
787 GFP_ATOMIC);
788 if (!new) {
789 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
790 if (ies)
791 kfree_rcu(ies, rcu_head);
792 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
793 if (ies)
794 kfree_rcu(ies, rcu_head);
776b3580 795 goto drop;
9caf0364
JB
796 }
797 memcpy(new, tmp, sizeof(*new));
776b3580
JB
798 new->refcount = 1;
799 INIT_LIST_HEAD(&new->hidden_list);
800
801 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
802 hidden = rb_find_bss(dev, tmp, BSS_CMP_HIDE_ZLEN);
803 if (!hidden)
804 hidden = rb_find_bss(dev, tmp,
805 BSS_CMP_HIDE_NUL);
806 if (hidden) {
807 new->pub.hidden_beacon_bss = &hidden->pub;
808 list_add(&new->hidden_list,
809 &hidden->hidden_list);
810 hidden->refcount++;
811 rcu_assign_pointer(new->pub.beacon_ies,
812 hidden->pub.beacon_ies);
813 }
814 } else {
815 /*
816 * Ok so we found a beacon, and don't have an entry. If
817 * it's a beacon with hidden SSID, we might be in for an
818 * expensive search for any probe responses that should
819 * be grouped with this beacon for updates ...
820 */
821 if (!cfg80211_combine_bsses(dev, new)) {
822 kfree(new);
823 goto drop;
824 }
825 }
826
9caf0364
JB
827 list_add_tail(&new->list, &dev->bss_list);
828 rb_insert_bss(dev, new);
829 found = new;
2a519311
JB
830 }
831
832 dev->bss_generation++;
776b3580 833 bss_ref_get(dev, found);
2a519311
JB
834 spin_unlock_bh(&dev->bss_lock);
835
2a519311 836 return found;
776b3580
JB
837 drop:
838 spin_unlock_bh(&dev->bss_lock);
839 return NULL;
2a519311
JB
840}
841
0172bb75
JB
842static struct ieee80211_channel *
843cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
844 struct ieee80211_channel *channel)
845{
846 const u8 *tmp;
847 u32 freq;
848 int channel_number = -1;
849
850 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
851 if (tmp && tmp[1] == 1) {
852 channel_number = tmp[2];
853 } else {
854 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
855 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
856 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
857
858 channel_number = htop->primary_chan;
859 }
860 }
861
862 if (channel_number < 0)
863 return channel;
864
865 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
866 channel = ieee80211_get_channel(wiphy, freq);
867 if (!channel)
868 return NULL;
869 if (channel->flags & IEEE80211_CHAN_DISABLED)
870 return NULL;
871 return channel;
872}
873
0e3a39b5 874/* Returned bss is reference counted and must be cleaned up appropriately. */
06aa7afa 875struct cfg80211_bss*
dcd6eac1 876cfg80211_inform_bss_width(struct wiphy *wiphy,
3afc2167 877 struct ieee80211_channel *rx_channel,
dcd6eac1
SW
878 enum nl80211_bss_scan_width scan_width,
879 const u8 *bssid, u64 tsf, u16 capability,
880 u16 beacon_interval, const u8 *ie, size_t ielen,
881 s32 signal, gfp_t gfp)
06aa7afa 882{
9caf0364 883 struct cfg80211_bss_ies *ies;
3afc2167 884 struct ieee80211_channel *channel;
9caf0364 885 struct cfg80211_internal_bss tmp = {}, *res;
06aa7afa
JK
886
887 if (WARN_ON(!wiphy))
888 return NULL;
889
22fe88d3 890 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
06aa7afa
JK
891 (signal < 0 || signal > 100)))
892 return NULL;
893
3afc2167 894 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, rx_channel);
0172bb75
JB
895 if (!channel)
896 return NULL;
897
9caf0364
JB
898 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
899 tmp.pub.channel = channel;
dcd6eac1 900 tmp.pub.scan_width = scan_width;
9caf0364 901 tmp.pub.signal = signal;
9caf0364
JB
902 tmp.pub.beacon_interval = beacon_interval;
903 tmp.pub.capability = capability;
34a6eddb
JM
904 /*
905 * Since we do not know here whether the IEs are from a Beacon or Probe
906 * Response frame, we need to pick one of the options and only use it
907 * with the driver that does not provide the full Beacon/Probe Response
908 * frame. Use Beacon frame pointer to avoid indicating that this should
50521aa8 909 * override the IEs pointer should we have received an earlier
9caf0364 910 * indication of Probe Response data.
34a6eddb 911 */
9caf0364
JB
912 ies = kmalloc(sizeof(*ies) + ielen, gfp);
913 if (!ies)
914 return NULL;
915 ies->len = ielen;
8cef2c9d 916 ies->tsf = tsf;
9caf0364 917 memcpy(ies->data, ie, ielen);
06aa7afa 918
9caf0364
JB
919 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
920 rcu_assign_pointer(tmp.pub.ies, ies);
06aa7afa 921
3afc2167
EG
922 res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp,
923 rx_channel == channel);
06aa7afa
JK
924 if (!res)
925 return NULL;
926
927 if (res->pub.capability & WLAN_CAPABILITY_ESS)
928 regulatory_hint_found_beacon(wiphy, channel, gfp);
929
4ee3e063 930 trace_cfg80211_return_bss(&res->pub);
06aa7afa
JK
931 /* cfg80211_bss_update gives us a referenced result */
932 return &res->pub;
933}
dcd6eac1 934EXPORT_SYMBOL(cfg80211_inform_bss_width);
06aa7afa 935
0e3a39b5 936/* Returned bss is reference counted and must be cleaned up appropriately. */
2a519311 937struct cfg80211_bss *
dcd6eac1 938cfg80211_inform_bss_width_frame(struct wiphy *wiphy,
3afc2167 939 struct ieee80211_channel *rx_channel,
dcd6eac1
SW
940 enum nl80211_bss_scan_width scan_width,
941 struct ieee80211_mgmt *mgmt, size_t len,
942 s32 signal, gfp_t gfp)
2a519311 943{
9caf0364
JB
944 struct cfg80211_internal_bss tmp = {}, *res;
945 struct cfg80211_bss_ies *ies;
3afc2167 946 struct ieee80211_channel *channel;
2a519311
JB
947 size_t ielen = len - offsetof(struct ieee80211_mgmt,
948 u.probe_resp.variable);
bef9bacc 949
0172bb75
JB
950 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
951 offsetof(struct ieee80211_mgmt, u.beacon.variable));
952
3afc2167 953 trace_cfg80211_inform_bss_width_frame(wiphy, rx_channel, scan_width, mgmt,
dcd6eac1 954 len, signal);
4ee3e063 955
bef9bacc
MK
956 if (WARN_ON(!mgmt))
957 return NULL;
958
959 if (WARN_ON(!wiphy))
960 return NULL;
2a519311 961
22fe88d3 962 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
768be59f 963 (signal < 0 || signal > 100)))
2a519311
JB
964 return NULL;
965
bef9bacc 966 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
2a519311
JB
967 return NULL;
968
0172bb75 969 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
3afc2167 970 ielen, rx_channel);
0172bb75
JB
971 if (!channel)
972 return NULL;
973
9caf0364
JB
974 ies = kmalloc(sizeof(*ies) + ielen, gfp);
975 if (!ies)
2a519311 976 return NULL;
9caf0364 977 ies->len = ielen;
8cef2c9d 978 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
9caf0364 979 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
2a519311 980
9caf0364
JB
981 if (ieee80211_is_probe_resp(mgmt->frame_control))
982 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
983 else
984 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
985 rcu_assign_pointer(tmp.pub.ies, ies);
986
987 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
988 tmp.pub.channel = channel;
dcd6eac1 989 tmp.pub.scan_width = scan_width;
9caf0364 990 tmp.pub.signal = signal;
9caf0364
JB
991 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
992 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
993
3afc2167
EG
994 res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp,
995 rx_channel == channel);
2a519311
JB
996 if (!res)
997 return NULL;
998
e38f8a7a
LR
999 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1000 regulatory_hint_found_beacon(wiphy, channel, gfp);
1001
4ee3e063 1002 trace_cfg80211_return_bss(&res->pub);
2a519311
JB
1003 /* cfg80211_bss_update gives us a referenced result */
1004 return &res->pub;
1005}
dcd6eac1 1006EXPORT_SYMBOL(cfg80211_inform_bss_width_frame);
2a519311 1007
5b112d3d 1008void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
4c0c0b75 1009{
776b3580 1010 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
4c0c0b75
JB
1011 struct cfg80211_internal_bss *bss;
1012
1013 if (!pub)
1014 return;
1015
1016 bss = container_of(pub, struct cfg80211_internal_bss, pub);
776b3580
JB
1017
1018 spin_lock_bh(&dev->bss_lock);
1019 bss_ref_get(dev, bss);
1020 spin_unlock_bh(&dev->bss_lock);
4c0c0b75
JB
1021}
1022EXPORT_SYMBOL(cfg80211_ref_bss);
1023
5b112d3d 1024void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
2a519311 1025{
776b3580 1026 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
2a519311
JB
1027 struct cfg80211_internal_bss *bss;
1028
1029 if (!pub)
1030 return;
1031
1032 bss = container_of(pub, struct cfg80211_internal_bss, pub);
776b3580
JB
1033
1034 spin_lock_bh(&dev->bss_lock);
1035 bss_ref_put(dev, bss);
1036 spin_unlock_bh(&dev->bss_lock);
2a519311
JB
1037}
1038EXPORT_SYMBOL(cfg80211_put_bss);
1039
d491af19
JB
1040void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1041{
1042 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
1043 struct cfg80211_internal_bss *bss;
1044
1045 if (WARN_ON(!pub))
1046 return;
1047
1048 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1049
1050 spin_lock_bh(&dev->bss_lock);
3207390a 1051 if (!list_empty(&bss->list)) {
776b3580
JB
1052 if (__cfg80211_unlink_bss(dev, bss))
1053 dev->bss_generation++;
3207390a 1054 }
d491af19 1055 spin_unlock_bh(&dev->bss_lock);
d491af19
JB
1056}
1057EXPORT_SYMBOL(cfg80211_unlink_bss);
1058
3d23e349 1059#ifdef CONFIG_CFG80211_WEXT
9f419f38
JB
1060static struct cfg80211_registered_device *
1061cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
1062{
5fe231e8 1063 struct cfg80211_registered_device *rdev;
9f419f38
JB
1064 struct net_device *dev;
1065
5fe231e8
JB
1066 ASSERT_RTNL();
1067
9f419f38
JB
1068 dev = dev_get_by_index(net, ifindex);
1069 if (!dev)
5fe231e8
JB
1070 return ERR_PTR(-ENODEV);
1071 if (dev->ieee80211_ptr)
9f419f38 1072 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
5fe231e8 1073 else
9f419f38
JB
1074 rdev = ERR_PTR(-ENODEV);
1075 dev_put(dev);
9f419f38
JB
1076 return rdev;
1077}
1078
2a519311
JB
1079int cfg80211_wext_siwscan(struct net_device *dev,
1080 struct iw_request_info *info,
1081 union iwreq_data *wrqu, char *extra)
1082{
1083 struct cfg80211_registered_device *rdev;
1084 struct wiphy *wiphy;
1085 struct iw_scan_req *wreq = NULL;
65486c8b 1086 struct cfg80211_scan_request *creq = NULL;
2a519311
JB
1087 int i, err, n_channels = 0;
1088 enum ieee80211_band band;
1089
1090 if (!netif_running(dev))
1091 return -ENETDOWN;
1092
b2e3abdc
HS
1093 if (wrqu->data.length == sizeof(struct iw_scan_req))
1094 wreq = (struct iw_scan_req *)extra;
1095
463d0183 1096 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2a519311
JB
1097
1098 if (IS_ERR(rdev))
1099 return PTR_ERR(rdev);
1100
f9d15d16 1101 if (rdev->scan_req || rdev->scan_msg) {
2a519311
JB
1102 err = -EBUSY;
1103 goto out;
1104 }
1105
1106 wiphy = &rdev->wiphy;
1107
b2e3abdc
HS
1108 /* Determine number of channels, needed to allocate creq */
1109 if (wreq && wreq->num_channels)
1110 n_channels = wreq->num_channels;
bdfbec2d
IP
1111 else
1112 n_channels = ieee80211_get_num_supported_channels(wiphy);
2a519311
JB
1113
1114 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1115 n_channels * sizeof(void *),
1116 GFP_ATOMIC);
1117 if (!creq) {
1118 err = -ENOMEM;
1119 goto out;
1120 }
1121
1122 creq->wiphy = wiphy;
fd014284 1123 creq->wdev = dev->ieee80211_ptr;
5ba63533
JB
1124 /* SSIDs come after channels */
1125 creq->ssids = (void *)&creq->channels[n_channels];
2a519311
JB
1126 creq->n_channels = n_channels;
1127 creq->n_ssids = 1;
15d6030b 1128 creq->scan_start = jiffies;
2a519311 1129
b2e3abdc 1130 /* translate "Scan on frequencies" request */
2a519311
JB
1131 i = 0;
1132 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1133 int j;
584991dc 1134
2a519311
JB
1135 if (!wiphy->bands[band])
1136 continue;
584991dc 1137
2a519311 1138 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
584991dc
JB
1139 /* ignore disabled channels */
1140 if (wiphy->bands[band]->channels[j].flags &
1141 IEEE80211_CHAN_DISABLED)
1142 continue;
b2e3abdc
HS
1143
1144 /* If we have a wireless request structure and the
1145 * wireless request specifies frequencies, then search
1146 * for the matching hardware channel.
1147 */
1148 if (wreq && wreq->num_channels) {
1149 int k;
1150 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1151 for (k = 0; k < wreq->num_channels; k++) {
96998e3a
ZG
1152 struct iw_freq *freq =
1153 &wreq->channel_list[k];
1154 int wext_freq =
1155 cfg80211_wext_freq(freq);
1156
b2e3abdc
HS
1157 if (wext_freq == wiphy_freq)
1158 goto wext_freq_found;
1159 }
1160 goto wext_freq_not_found;
1161 }
1162
1163 wext_freq_found:
2a519311
JB
1164 creq->channels[i] = &wiphy->bands[band]->channels[j];
1165 i++;
b2e3abdc 1166 wext_freq_not_found: ;
2a519311
JB
1167 }
1168 }
8862dc5f
HS
1169 /* No channels found? */
1170 if (!i) {
1171 err = -EINVAL;
1172 goto out;
1173 }
2a519311 1174
b2e3abdc
HS
1175 /* Set real number of channels specified in creq->channels[] */
1176 creq->n_channels = i;
2a519311 1177
b2e3abdc
HS
1178 /* translate "Scan for SSID" request */
1179 if (wreq) {
2a519311 1180 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
65486c8b
JB
1181 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1182 err = -EINVAL;
1183 goto out;
1184 }
2a519311
JB
1185 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1186 creq->ssids[0].ssid_len = wreq->essid_len;
1187 }
1188 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1189 creq->n_ssids = 0;
1190 }
1191
34850ab2 1192 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
a401d2bb
JB
1193 if (wiphy->bands[i])
1194 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
34850ab2 1195
2a519311 1196 rdev->scan_req = creq;
e35e4d28 1197 err = rdev_scan(rdev, creq);
2a519311
JB
1198 if (err) {
1199 rdev->scan_req = NULL;
65486c8b 1200 /* creq will be freed below */
463d0183 1201 } else {
fd014284 1202 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
65486c8b
JB
1203 /* creq now owned by driver */
1204 creq = NULL;
463d0183
JB
1205 dev_hold(dev);
1206 }
2a519311 1207 out:
65486c8b 1208 kfree(creq);
2a519311
JB
1209 return err;
1210}
ba44cb72 1211EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
2a519311
JB
1212
1213static void ieee80211_scan_add_ies(struct iw_request_info *info,
9caf0364 1214 const struct cfg80211_bss_ies *ies,
2a519311
JB
1215 char **current_ev, char *end_buf)
1216{
9caf0364 1217 const u8 *pos, *end, *next;
2a519311
JB
1218 struct iw_event iwe;
1219
9caf0364 1220 if (!ies)
2a519311
JB
1221 return;
1222
1223 /*
1224 * If needed, fragment the IEs buffer (at IE boundaries) into short
1225 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1226 */
9caf0364
JB
1227 pos = ies->data;
1228 end = pos + ies->len;
2a519311
JB
1229
1230 while (end - pos > IW_GENERIC_IE_MAX) {
1231 next = pos + 2 + pos[1];
1232 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1233 next = next + 2 + next[1];
1234
1235 memset(&iwe, 0, sizeof(iwe));
1236 iwe.cmd = IWEVGENIE;
1237 iwe.u.data.length = next - pos;
1238 *current_ev = iwe_stream_add_point(info, *current_ev,
9caf0364
JB
1239 end_buf, &iwe,
1240 (void *)pos);
2a519311
JB
1241
1242 pos = next;
1243 }
1244
1245 if (end > pos) {
1246 memset(&iwe, 0, sizeof(iwe));
1247 iwe.cmd = IWEVGENIE;
1248 iwe.u.data.length = end - pos;
1249 *current_ev = iwe_stream_add_point(info, *current_ev,
9caf0364
JB
1250 end_buf, &iwe,
1251 (void *)pos);
2a519311
JB
1252 }
1253}
1254
2a519311 1255static char *
77965c97
JB
1256ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1257 struct cfg80211_internal_bss *bss, char *current_ev,
1258 char *end_buf)
2a519311 1259{
9caf0364 1260 const struct cfg80211_bss_ies *ies;
2a519311 1261 struct iw_event iwe;
9caf0364 1262 const u8 *ie;
2a519311 1263 u8 *buf, *cfg, *p;
9caf0364 1264 int rem, i, sig;
2a519311
JB
1265 bool ismesh = false;
1266
1267 memset(&iwe, 0, sizeof(iwe));
1268 iwe.cmd = SIOCGIWAP;
1269 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1270 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
1271 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1272 IW_EV_ADDR_LEN);
1273
1274 memset(&iwe, 0, sizeof(iwe));
1275 iwe.cmd = SIOCGIWFREQ;
1276 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
1277 iwe.u.freq.e = 0;
1278 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1279 IW_EV_FREQ_LEN);
1280
1281 memset(&iwe, 0, sizeof(iwe));
1282 iwe.cmd = SIOCGIWFREQ;
1283 iwe.u.freq.m = bss->pub.channel->center_freq;
1284 iwe.u.freq.e = 6;
1285 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1286 IW_EV_FREQ_LEN);
1287
77965c97 1288 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2a519311
JB
1289 memset(&iwe, 0, sizeof(iwe));
1290 iwe.cmd = IWEVQUAL;
1291 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
1292 IW_QUAL_NOISE_INVALID |
a77b8552 1293 IW_QUAL_QUAL_UPDATED;
77965c97 1294 switch (wiphy->signal_type) {
2a519311 1295 case CFG80211_SIGNAL_TYPE_MBM:
a77b8552
JB
1296 sig = bss->pub.signal / 100;
1297 iwe.u.qual.level = sig;
2a519311 1298 iwe.u.qual.updated |= IW_QUAL_DBM;
a77b8552
JB
1299 if (sig < -110) /* rather bad */
1300 sig = -110;
1301 else if (sig > -40) /* perfect */
1302 sig = -40;
1303 /* will give a range of 0 .. 70 */
1304 iwe.u.qual.qual = sig + 110;
2a519311
JB
1305 break;
1306 case CFG80211_SIGNAL_TYPE_UNSPEC:
1307 iwe.u.qual.level = bss->pub.signal;
a77b8552
JB
1308 /* will give range 0 .. 100 */
1309 iwe.u.qual.qual = bss->pub.signal;
2a519311
JB
1310 break;
1311 default:
1312 /* not reached */
1313 break;
1314 }
1315 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1316 &iwe, IW_EV_QUAL_LEN);
1317 }
1318
1319 memset(&iwe, 0, sizeof(iwe));
1320 iwe.cmd = SIOCGIWENCODE;
1321 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
1322 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1323 else
1324 iwe.u.data.flags = IW_ENCODE_DISABLED;
1325 iwe.u.data.length = 0;
1326 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1327 &iwe, "");
1328
9caf0364
JB
1329 rcu_read_lock();
1330 ies = rcu_dereference(bss->pub.ies);
83c7aa1a
JB
1331 rem = ies->len;
1332 ie = ies->data;
9caf0364 1333
83c7aa1a 1334 while (rem >= 2) {
2a519311
JB
1335 /* invalid data */
1336 if (ie[1] > rem - 2)
1337 break;
1338
1339 switch (ie[0]) {
1340 case WLAN_EID_SSID:
1341 memset(&iwe, 0, sizeof(iwe));
1342 iwe.cmd = SIOCGIWESSID;
1343 iwe.u.data.length = ie[1];
1344 iwe.u.data.flags = 1;
1345 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
9caf0364 1346 &iwe, (u8 *)ie + 2);
2a519311
JB
1347 break;
1348 case WLAN_EID_MESH_ID:
1349 memset(&iwe, 0, sizeof(iwe));
1350 iwe.cmd = SIOCGIWESSID;
1351 iwe.u.data.length = ie[1];
1352 iwe.u.data.flags = 1;
1353 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
9caf0364 1354 &iwe, (u8 *)ie + 2);
2a519311
JB
1355 break;
1356 case WLAN_EID_MESH_CONFIG:
1357 ismesh = true;
136cfa28 1358 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
2a519311
JB
1359 break;
1360 buf = kmalloc(50, GFP_ATOMIC);
1361 if (!buf)
1362 break;
9caf0364 1363 cfg = (u8 *)ie + 2;
2a519311
JB
1364 memset(&iwe, 0, sizeof(iwe));
1365 iwe.cmd = IWEVCUSTOM;
76aa5e70
RP
1366 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1367 "0x%02X", cfg[0]);
2a519311
JB
1368 iwe.u.data.length = strlen(buf);
1369 current_ev = iwe_stream_add_point(info, current_ev,
1370 end_buf,
1371 &iwe, buf);
76aa5e70
RP
1372 sprintf(buf, "Path Selection Metric ID: 0x%02X",
1373 cfg[1]);
2a519311
JB
1374 iwe.u.data.length = strlen(buf);
1375 current_ev = iwe_stream_add_point(info, current_ev,
1376 end_buf,
1377 &iwe, buf);
76aa5e70
RP
1378 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1379 cfg[2]);
2a519311
JB
1380 iwe.u.data.length = strlen(buf);
1381 current_ev = iwe_stream_add_point(info, current_ev,
1382 end_buf,
1383 &iwe, buf);
76aa5e70 1384 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
2a519311
JB
1385 iwe.u.data.length = strlen(buf);
1386 current_ev = iwe_stream_add_point(info, current_ev,
1387 end_buf,
1388 &iwe, buf);
76aa5e70
RP
1389 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
1390 iwe.u.data.length = strlen(buf);
1391 current_ev = iwe_stream_add_point(info, current_ev,
1392 end_buf,
1393 &iwe, buf);
1394 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1395 iwe.u.data.length = strlen(buf);
1396 current_ev = iwe_stream_add_point(info, current_ev,
1397 end_buf,
1398 &iwe, buf);
1399 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
2a519311
JB
1400 iwe.u.data.length = strlen(buf);
1401 current_ev = iwe_stream_add_point(info, current_ev,
1402 end_buf,
1403 &iwe, buf);
1404 kfree(buf);
1405 break;
1406 case WLAN_EID_SUPP_RATES:
1407 case WLAN_EID_EXT_SUPP_RATES:
1408 /* display all supported rates in readable format */
1409 p = current_ev + iwe_stream_lcp_len(info);
1410
1411 memset(&iwe, 0, sizeof(iwe));
1412 iwe.cmd = SIOCGIWRATE;
1413 /* Those two flags are ignored... */
1414 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1415
1416 for (i = 0; i < ie[1]; i++) {
1417 iwe.u.bitrate.value =
1418 ((ie[i + 2] & 0x7f) * 500000);
1419 p = iwe_stream_add_value(info, current_ev, p,
1420 end_buf, &iwe, IW_EV_PARAM_LEN);
1421 }
1422 current_ev = p;
1423 break;
1424 }
1425 rem -= ie[1] + 2;
1426 ie += ie[1] + 2;
1427 }
1428
f64f9e71
JP
1429 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1430 ismesh) {
2a519311
JB
1431 memset(&iwe, 0, sizeof(iwe));
1432 iwe.cmd = SIOCGIWMODE;
1433 if (ismesh)
1434 iwe.u.mode = IW_MODE_MESH;
1435 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
1436 iwe.u.mode = IW_MODE_MASTER;
1437 else
1438 iwe.u.mode = IW_MODE_ADHOC;
1439 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1440 &iwe, IW_EV_UINT_LEN);
1441 }
1442
c49dc900 1443 buf = kmalloc(31, GFP_ATOMIC);
2a519311
JB
1444 if (buf) {
1445 memset(&iwe, 0, sizeof(iwe));
1446 iwe.cmd = IWEVCUSTOM;
8cef2c9d 1447 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
2a519311
JB
1448 iwe.u.data.length = strlen(buf);
1449 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1450 &iwe, buf);
1451 memset(&iwe, 0, sizeof(iwe));
1452 iwe.cmd = IWEVCUSTOM;
cb3a8eec
DW
1453 sprintf(buf, " Last beacon: %ums ago",
1454 elapsed_jiffies_msecs(bss->ts));
2a519311
JB
1455 iwe.u.data.length = strlen(buf);
1456 current_ev = iwe_stream_add_point(info, current_ev,
1457 end_buf, &iwe, buf);
1458 kfree(buf);
1459 }
1460
9caf0364
JB
1461 ieee80211_scan_add_ies(info, ies, &current_ev, end_buf);
1462 rcu_read_unlock();
2a519311
JB
1463
1464 return current_ev;
1465}
1466
1467
1468static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
1469 struct iw_request_info *info,
1470 char *buf, size_t len)
1471{
1472 char *current_ev = buf;
1473 char *end_buf = buf + len;
1474 struct cfg80211_internal_bss *bss;
1475
1476 spin_lock_bh(&dev->bss_lock);
1477 cfg80211_bss_expire(dev);
1478
1479 list_for_each_entry(bss, &dev->bss_list, list) {
1480 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1481 spin_unlock_bh(&dev->bss_lock);
1482 return -E2BIG;
1483 }
77965c97
JB
1484 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
1485 current_ev, end_buf);
2a519311
JB
1486 }
1487 spin_unlock_bh(&dev->bss_lock);
1488 return current_ev - buf;
1489}
1490
1491
1492int cfg80211_wext_giwscan(struct net_device *dev,
1493 struct iw_request_info *info,
1494 struct iw_point *data, char *extra)
1495{
1496 struct cfg80211_registered_device *rdev;
1497 int res;
1498
1499 if (!netif_running(dev))
1500 return -ENETDOWN;
1501
463d0183 1502 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2a519311
JB
1503
1504 if (IS_ERR(rdev))
1505 return PTR_ERR(rdev);
1506
f9d15d16 1507 if (rdev->scan_req || rdev->scan_msg)
5fe231e8 1508 return -EAGAIN;
2a519311
JB
1509
1510 res = ieee80211_scan_results(rdev, info, extra, data->length);
1511 data->length = 0;
1512 if (res >= 0) {
1513 data->length = res;
1514 res = 0;
1515 }
1516
2a519311
JB
1517 return res;
1518}
ba44cb72 1519EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
2a519311 1520#endif