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