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