wireless: fix all kind of warnings
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / net / wireless / scan.c
1 /*
2 * cfg80211 scan result handling
3 *
4 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2013-2014 Intel Mobile Communications GmbH
6 */
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/netdevice.h>
11 #include <linux/wireless.h>
12 #include <linux/nl80211.h>
13 #include <linux/etherdevice.h>
14 #include <net/arp.h>
15 #include <net/cfg80211.h>
16 #include <net/cfg80211-wext.h>
17 #include <net/iw_handler.h>
18 #include "core.h"
19 #include "nl80211.h"
20 #include "wext-compat.h"
21 #include "rdev-ops.h"
22
23 /**
24 * DOC: BSS tree/list structure
25 *
26 * At the top level, the BSS list is kept in both a list in each
27 * registered device (@bss_list) as well as an RB-tree for faster
28 * lookup. In the RB-tree, entries can be looked up using their
29 * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID
30 * for other BSSes.
31 *
32 * Due to the possibility of hidden SSIDs, there's a second level
33 * structure, the "hidden_list" and "hidden_beacon_bss" pointer.
34 * The hidden_list connects all BSSes belonging to a single AP
35 * that has a hidden SSID, and connects beacon and probe response
36 * entries. For a probe response entry for a hidden SSID, the
37 * hidden_beacon_bss pointer points to the BSS struct holding the
38 * beacon's information.
39 *
40 * Reference counting is done for all these references except for
41 * the hidden_list, so that a beacon BSS struct that is otherwise
42 * not referenced has one reference for being on the bss_list and
43 * one for each probe response entry that points to it using the
44 * hidden_beacon_bss pointer. When a BSS struct that has such a
45 * pointer is get/put, the refcount update is also propagated to
46 * the referenced struct, this ensure that it cannot get removed
47 * while somebody is using the probe response version.
48 *
49 * Note that the hidden_beacon_bss pointer never changes, due to
50 * the reference counting. Therefore, no locking is needed for
51 * it.
52 *
53 * Also note that the hidden_beacon_bss pointer is only relevant
54 * if the driver uses something other than the IEs, e.g. private
55 * data stored stored in the BSS struct, since the beacon IEs are
56 * also linked into the probe response struct.
57 */
58
59 /*
60 * Limit the number of BSS entries stored in mac80211. Each one is
61 * a bit over 4k at most, so this limits to roughly 4-5M of memory.
62 * If somebody wants to really attack this though, they'd likely
63 * use small beacons, and only one type of frame, limiting each of
64 * the entries to a much smaller size (in order to generate more
65 * entries in total, so overhead is bigger.)
66 */
67 static int bss_entries_limit = 1000;
68 module_param(bss_entries_limit, int, 0644);
69 MODULE_PARM_DESC(bss_entries_limit,
70 "limit to number of scan BSS entries (per wiphy, default 1000)");
71
72 #define IEEE80211_SCAN_RESULT_EXPIRE (7 * HZ)
73
74 static void bss_free(struct cfg80211_internal_bss *bss)
75 {
76 struct cfg80211_bss_ies *ies;
77
78 if (WARN_ON(atomic_read(&bss->hold)))
79 return;
80
81 ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
82 if (ies && !bss->pub.hidden_beacon_bss)
83 kfree_rcu(ies, rcu_head);
84 ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
85 if (ies)
86 kfree_rcu(ies, rcu_head);
87
88 /*
89 * This happens when the module is removed, it doesn't
90 * really matter any more save for completeness
91 */
92 if (!list_empty(&bss->hidden_list))
93 list_del(&bss->hidden_list);
94
95 kfree(bss);
96 }
97
98 static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
99 struct cfg80211_internal_bss *bss)
100 {
101 lockdep_assert_held(&rdev->bss_lock);
102
103 bss->refcount++;
104 if (bss->pub.hidden_beacon_bss) {
105 bss = container_of(bss->pub.hidden_beacon_bss,
106 struct cfg80211_internal_bss,
107 pub);
108 bss->refcount++;
109 }
110 }
111
112 static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
113 struct cfg80211_internal_bss *bss)
114 {
115 lockdep_assert_held(&rdev->bss_lock);
116
117 if (bss->pub.hidden_beacon_bss) {
118 struct cfg80211_internal_bss *hbss;
119 hbss = container_of(bss->pub.hidden_beacon_bss,
120 struct cfg80211_internal_bss,
121 pub);
122 hbss->refcount--;
123 if (hbss->refcount == 0)
124 bss_free(hbss);
125 }
126 bss->refcount--;
127 if (bss->refcount == 0)
128 bss_free(bss);
129 }
130
131 static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
132 struct cfg80211_internal_bss *bss)
133 {
134 lockdep_assert_held(&rdev->bss_lock);
135
136 if (!list_empty(&bss->hidden_list)) {
137 /*
138 * don't remove the beacon entry if it has
139 * probe responses associated with it
140 */
141 if (!bss->pub.hidden_beacon_bss)
142 return false;
143 /*
144 * if it's a probe response entry break its
145 * link to the other entries in the group
146 */
147 list_del_init(&bss->hidden_list);
148 }
149
150 list_del_init(&bss->list);
151 rb_erase(&bss->rbn, &rdev->bss_tree);
152 rdev->bss_entries--;
153 WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
154 "rdev bss entries[%d]/list[empty:%d] corruption\n",
155 rdev->bss_entries, list_empty(&rdev->bss_list));
156 bss_ref_put(rdev, bss);
157 return true;
158 }
159
160 static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
161 unsigned long expire_time)
162 {
163 struct cfg80211_internal_bss *bss, *tmp;
164 bool expired = false;
165
166 lockdep_assert_held(&rdev->bss_lock);
167
168 list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
169 if (atomic_read(&bss->hold))
170 continue;
171 if (!time_after(expire_time, bss->ts))
172 continue;
173
174 if (__cfg80211_unlink_bss(rdev, bss))
175 expired = true;
176 }
177
178 if (expired)
179 rdev->bss_generation++;
180 }
181
182 static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
183 {
184 struct cfg80211_internal_bss *bss, *oldest = NULL;
185 bool ret;
186
187 lockdep_assert_held(&rdev->bss_lock);
188
189 list_for_each_entry(bss, &rdev->bss_list, list) {
190 if (atomic_read(&bss->hold))
191 continue;
192
193 if (!list_empty(&bss->hidden_list) &&
194 !bss->pub.hidden_beacon_bss)
195 continue;
196
197 if (oldest && time_before(oldest->ts, bss->ts))
198 continue;
199 oldest = bss;
200 }
201
202 if (WARN_ON(!oldest))
203 return false;
204
205 /*
206 * The callers make sure to increase rdev->bss_generation if anything
207 * gets removed (and a new entry added), so there's no need to also do
208 * it here.
209 */
210
211 ret = __cfg80211_unlink_bss(rdev, oldest);
212 WARN_ON(!ret);
213 return ret;
214 }
215
216 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
217 bool send_message)
218 {
219 struct cfg80211_scan_request *request;
220 struct wireless_dev *wdev;
221 struct sk_buff *msg;
222 #ifdef CONFIG_CFG80211_WEXT
223 union iwreq_data wrqu;
224 #endif
225
226 ASSERT_RTNL();
227
228 if (rdev->scan_msg) {
229 nl80211_send_scan_result(rdev, rdev->scan_msg);
230 rdev->scan_msg = NULL;
231 return;
232 }
233
234 request = rdev->scan_req;
235 if (!request)
236 return;
237
238 wdev = request->wdev;
239
240 /*
241 * This must be before sending the other events!
242 * Otherwise, wpa_supplicant gets completely confused with
243 * wext events.
244 */
245 if (wdev->netdev)
246 cfg80211_sme_scan_done(wdev->netdev);
247
248 if (!request->aborted &&
249 request->flags & NL80211_SCAN_FLAG_FLUSH) {
250 /* flush entries from previous scans */
251 spin_lock_bh(&rdev->bss_lock);
252 __cfg80211_bss_expire(rdev, request->scan_start);
253 spin_unlock_bh(&rdev->bss_lock);
254 }
255
256 msg = nl80211_build_scan_msg(rdev, wdev, request->aborted);
257
258 #ifdef CONFIG_CFG80211_WEXT
259 if (wdev->netdev && !request->aborted) {
260 memset(&wrqu, 0, sizeof(wrqu));
261
262 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
263 }
264 #endif
265
266 if (wdev->netdev)
267 dev_put(wdev->netdev);
268
269 rdev->scan_req = NULL;
270 kfree(request);
271
272 if (!send_message)
273 rdev->scan_msg = msg;
274 else
275 nl80211_send_scan_result(rdev, msg);
276 }
277
278 void __cfg80211_scan_done(struct work_struct *wk)
279 {
280 struct cfg80211_registered_device *rdev;
281
282 rdev = container_of(wk, struct cfg80211_registered_device,
283 scan_done_wk);
284
285 rtnl_lock();
286 ___cfg80211_scan_done(rdev, true);
287 rtnl_unlock();
288 }
289
290 void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
291 {
292 trace_cfg80211_scan_done(request, aborted);
293 WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
294
295 request->aborted = aborted;
296 request->notified = true;
297 queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
298 }
299 EXPORT_SYMBOL(cfg80211_scan_done);
300
301 void __cfg80211_sched_scan_results(struct work_struct *wk)
302 {
303 struct cfg80211_registered_device *rdev;
304 struct cfg80211_sched_scan_request *request;
305
306 rdev = container_of(wk, struct cfg80211_registered_device,
307 sched_scan_results_wk);
308
309 rtnl_lock();
310
311 request = rtnl_dereference(rdev->sched_scan_req);
312
313 /* we don't have sched_scan_req anymore if the scan is stopping */
314 if (request) {
315 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
316 /* flush entries from previous scans */
317 spin_lock_bh(&rdev->bss_lock);
318 __cfg80211_bss_expire(rdev, request->scan_start);
319 spin_unlock_bh(&rdev->bss_lock);
320 request->scan_start = jiffies;
321 }
322 nl80211_send_sched_scan_results(rdev, request->dev);
323 }
324
325 rtnl_unlock();
326 }
327
328 void cfg80211_sched_scan_results(struct wiphy *wiphy)
329 {
330 trace_cfg80211_sched_scan_results(wiphy);
331 /* ignore if we're not scanning */
332
333 if (rcu_access_pointer(wiphy_to_rdev(wiphy)->sched_scan_req))
334 queue_work(cfg80211_wq,
335 &wiphy_to_rdev(wiphy)->sched_scan_results_wk);
336 }
337 EXPORT_SYMBOL(cfg80211_sched_scan_results);
338
339 void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy)
340 {
341 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
342
343 ASSERT_RTNL();
344
345 trace_cfg80211_sched_scan_stopped(wiphy);
346
347 __cfg80211_stop_sched_scan(rdev, true);
348 }
349 EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
350
351 void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
352 {
353 rtnl_lock();
354 cfg80211_sched_scan_stopped_rtnl(wiphy);
355 rtnl_unlock();
356 }
357 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
358
359 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
360 bool driver_initiated)
361 {
362 struct cfg80211_sched_scan_request *sched_scan_req;
363 struct net_device *dev;
364
365 ASSERT_RTNL();
366
367 if (!rdev->sched_scan_req)
368 return -ENOENT;
369
370 sched_scan_req = rtnl_dereference(rdev->sched_scan_req);
371 dev = sched_scan_req->dev;
372
373 if (!driver_initiated) {
374 int err = rdev_sched_scan_stop(rdev, dev);
375 if (err)
376 return err;
377 }
378
379 nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED);
380
381 RCU_INIT_POINTER(rdev->sched_scan_req, NULL);
382 kfree_rcu(sched_scan_req, rcu_head);
383
384 return 0;
385 }
386
387 void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
388 unsigned long age_secs)
389 {
390 struct cfg80211_internal_bss *bss;
391 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
392
393 spin_lock_bh(&rdev->bss_lock);
394 list_for_each_entry(bss, &rdev->bss_list, list)
395 bss->ts -= age_jiffies;
396 spin_unlock_bh(&rdev->bss_lock);
397 }
398
399 void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
400 {
401 __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
402 }
403
404 const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
405 {
406 while (len > 2 && ies[0] != eid) {
407 len -= ies[1] + 2;
408 ies += ies[1] + 2;
409 }
410 if (len < 2)
411 return NULL;
412 if (len < 2 + ies[1])
413 return NULL;
414 return ies;
415 }
416 EXPORT_SYMBOL(cfg80211_find_ie);
417
418 const u8 *cfg80211_find_vendor_ie(unsigned int oui, u8 oui_type,
419 const u8 *ies, int len)
420 {
421 struct ieee80211_vendor_ie *ie;
422 const u8 *pos = ies, *end = ies + len;
423 int ie_oui;
424
425 while (pos < end) {
426 pos = cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC, pos,
427 end - pos);
428 if (!pos)
429 return NULL;
430
431 ie = (struct ieee80211_vendor_ie *)pos;
432
433 /* make sure we can access ie->len */
434 BUILD_BUG_ON(offsetof(struct ieee80211_vendor_ie, len) != 1);
435
436 if (ie->len < sizeof(*ie))
437 goto cont;
438
439 ie_oui = ie->oui[0] << 16 | ie->oui[1] << 8 | ie->oui[2];
440 if (ie_oui == oui && ie->oui_type == oui_type)
441 return pos;
442 cont:
443 pos += 2 + ie->len;
444 }
445 return NULL;
446 }
447 EXPORT_SYMBOL(cfg80211_find_vendor_ie);
448
449 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
450 const u8 *ssid, size_t ssid_len)
451 {
452 const struct cfg80211_bss_ies *ies;
453 const u8 *ssidie;
454
455 if (bssid && !ether_addr_equal(a->bssid, bssid))
456 return false;
457
458 if (!ssid)
459 return true;
460
461 ies = rcu_access_pointer(a->ies);
462 if (!ies)
463 return false;
464 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
465 if (!ssidie)
466 return false;
467 if (ssidie[1] != ssid_len)
468 return false;
469 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
470 }
471
472 /**
473 * enum bss_compare_mode - BSS compare mode
474 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
475 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
476 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
477 */
478 enum bss_compare_mode {
479 BSS_CMP_REGULAR,
480 BSS_CMP_HIDE_ZLEN,
481 BSS_CMP_HIDE_NUL,
482 };
483
484 static int cmp_bss(struct cfg80211_bss *a,
485 struct cfg80211_bss *b,
486 enum bss_compare_mode mode)
487 {
488 const struct cfg80211_bss_ies *a_ies, *b_ies;
489 const u8 *ie1 = NULL;
490 const u8 *ie2 = NULL;
491 int i, r;
492 #if !(defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) \
493 || defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) \
494 || defined(CONFIG_BCM43438) || defined(CONFIG_BCM43438_MODULE) \
495 || defined(CONFIG_BCM43454) || defined(CONFIG_BCM43454_MODULE) \
496 || defined(CONFIG_BCM43455) || defined(CONFIG_BCM43455_MODULE) \
497 || defined(CONFIG_BCM4354) || defined(CONFIG_BCM4354_MODULE) \
498 || defined(CONFIG_BCM4356) || defined(CONFIG_BCM4356_MODULE) \
499 || defined(CONFIG_BCM4358) || defined(CONFIG_BCM4358_MODULE)\
500 || defined(CONFIG_BCM4359) || defined(CONFIG_BCM4359_MODULE)\
501 || defined(CONFIG_BCM4361) || defined(CONFIG_BCM4361_MODULE))
502 if (a->channel != b->channel)
503 return b->channel->center_freq - a->channel->center_freq;
504 #endif /* CONFIG_BCM43xx */
505
506 a_ies = rcu_access_pointer(a->ies);
507 if (!a_ies)
508 return -1;
509 b_ies = rcu_access_pointer(b->ies);
510 if (!b_ies)
511 return 1;
512
513 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
514 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
515 a_ies->data, a_ies->len);
516 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
517 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
518 b_ies->data, b_ies->len);
519 if (ie1 && ie2) {
520 int mesh_id_cmp;
521
522 if (ie1[1] == ie2[1])
523 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
524 else
525 mesh_id_cmp = ie2[1] - ie1[1];
526
527 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
528 a_ies->data, a_ies->len);
529 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
530 b_ies->data, b_ies->len);
531 if (ie1 && ie2) {
532 if (mesh_id_cmp)
533 return mesh_id_cmp;
534 if (ie1[1] != ie2[1])
535 return ie2[1] - ie1[1];
536 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
537 }
538 }
539
540 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
541 if (r)
542 return r;
543
544 #if (defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) \
545 || defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) \
546 || defined(CONFIG_BCM43438) || defined(CONFIG_BCM43438_MODULE) \
547 || defined(CONFIG_BCM43454) || defined(CONFIG_BCM43454_MODULE) \
548 || defined(CONFIG_BCM43455) || defined(CONFIG_BCM43455_MODULE) \
549 || defined(CONFIG_BCM4354) || defined(CONFIG_BCM4354_MODULE) \
550 || defined(CONFIG_BCM4356) || defined(CONFIG_BCM4356_MODULE) \
551 || defined(CONFIG_BCM4358) || defined(CONFIG_BCM4358_MODULE)\
552 || defined(CONFIG_BCM4359) || defined(CONFIG_BCM4359_MODULE)\
553 || defined(CONFIG_BCM4361) || defined(CONFIG_BCM4361_MODULE))
554 if (a->channel != b->channel)
555 return b->channel->center_freq - a->channel->center_freq;
556 #endif /* CONFIG_BCM43xx */
557
558 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
559 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
560
561 if (!ie1 && !ie2)
562 return 0;
563
564 /*
565 * Note that with "hide_ssid", the function returns a match if
566 * the already-present BSS ("b") is a hidden SSID beacon for
567 * the new BSS ("a").
568 */
569
570 /* sort missing IE before (left of) present IE */
571 if (!ie1)
572 return -1;
573 if (!ie2)
574 return 1;
575
576 switch (mode) {
577 case BSS_CMP_HIDE_ZLEN:
578 /*
579 * In ZLEN mode we assume the BSS entry we're
580 * looking for has a zero-length SSID. So if
581 * the one we're looking at right now has that,
582 * return 0. Otherwise, return the difference
583 * in length, but since we're looking for the
584 * 0-length it's really equivalent to returning
585 * the length of the one we're looking at.
586 *
587 * No content comparison is needed as we assume
588 * the content length is zero.
589 */
590 return ie2[1];
591 case BSS_CMP_REGULAR:
592 default:
593 /* sort by length first, then by contents */
594 if (ie1[1] != ie2[1])
595 return ie2[1] - ie1[1];
596 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
597 case BSS_CMP_HIDE_NUL:
598 if (ie1[1] != ie2[1])
599 return ie2[1] - ie1[1];
600 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
601 for (i = 0; i < ie2[1]; i++)
602 if (ie2[i + 2])
603 return -1;
604 return 0;
605 }
606 }
607
608 static bool cfg80211_bss_type_match(u16 capability,
609 enum nl80211_band band,
610 enum ieee80211_bss_type bss_type)
611 {
612 bool ret = true;
613 u16 mask, val;
614
615 if (bss_type == IEEE80211_BSS_TYPE_ANY)
616 return ret;
617
618 if (band == NL80211_BAND_60GHZ) {
619 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
620 switch (bss_type) {
621 case IEEE80211_BSS_TYPE_ESS:
622 val = WLAN_CAPABILITY_DMG_TYPE_AP;
623 break;
624 case IEEE80211_BSS_TYPE_PBSS:
625 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
626 break;
627 case IEEE80211_BSS_TYPE_IBSS:
628 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
629 break;
630 default:
631 return false;
632 }
633 } else {
634 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
635 switch (bss_type) {
636 case IEEE80211_BSS_TYPE_ESS:
637 val = WLAN_CAPABILITY_ESS;
638 break;
639 case IEEE80211_BSS_TYPE_IBSS:
640 val = WLAN_CAPABILITY_IBSS;
641 break;
642 case IEEE80211_BSS_TYPE_MBSS:
643 val = 0;
644 break;
645 default:
646 return false;
647 }
648 }
649
650 ret = ((capability & mask) == val);
651 return ret;
652 }
653
654 /* Returned bss is reference counted and must be cleaned up appropriately. */
655 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
656 struct ieee80211_channel *channel,
657 const u8 *bssid,
658 const u8 *ssid, size_t ssid_len,
659 enum ieee80211_bss_type bss_type,
660 enum ieee80211_privacy privacy)
661 {
662 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
663 struct cfg80211_internal_bss *bss, *res = NULL;
664 unsigned long now = jiffies;
665 int bss_privacy;
666
667 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
668 privacy);
669
670 spin_lock_bh(&rdev->bss_lock);
671
672 list_for_each_entry(bss, &rdev->bss_list, list) {
673 if (!cfg80211_bss_type_match(bss->pub.capability,
674 bss->pub.channel->band, bss_type))
675 continue;
676
677 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
678 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
679 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
680 continue;
681 if (channel && bss->pub.channel != channel)
682 continue;
683 if (!is_valid_ether_addr(bss->pub.bssid))
684 continue;
685 /* Don't get expired BSS structs */
686 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
687 !atomic_read(&bss->hold))
688 continue;
689 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
690 res = bss;
691 bss_ref_get(rdev, res);
692 break;
693 }
694 }
695
696 spin_unlock_bh(&rdev->bss_lock);
697 if (!res)
698 return NULL;
699 trace_cfg80211_return_bss(&res->pub);
700 return &res->pub;
701 }
702 EXPORT_SYMBOL(cfg80211_get_bss);
703
704 static void rb_insert_bss(struct cfg80211_registered_device *rdev,
705 struct cfg80211_internal_bss *bss)
706 {
707 struct rb_node **p = &rdev->bss_tree.rb_node;
708 struct rb_node *parent = NULL;
709 struct cfg80211_internal_bss *tbss;
710 int cmp;
711
712 while (*p) {
713 parent = *p;
714 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
715
716 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
717
718 if (WARN_ON(!cmp)) {
719 /* will sort of leak this BSS */
720 return;
721 }
722
723 if (cmp < 0)
724 p = &(*p)->rb_left;
725 else
726 p = &(*p)->rb_right;
727 }
728
729 rb_link_node(&bss->rbn, parent, p);
730 rb_insert_color(&bss->rbn, &rdev->bss_tree);
731 }
732
733 static struct cfg80211_internal_bss *
734 rb_find_bss(struct cfg80211_registered_device *rdev,
735 struct cfg80211_internal_bss *res,
736 enum bss_compare_mode mode)
737 {
738 struct rb_node *n = rdev->bss_tree.rb_node;
739 struct cfg80211_internal_bss *bss;
740 int r;
741
742 while (n) {
743 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
744 r = cmp_bss(&res->pub, &bss->pub, mode);
745
746 if (r == 0)
747 return bss;
748 else if (r < 0)
749 n = n->rb_left;
750 else
751 n = n->rb_right;
752 }
753
754 return NULL;
755 }
756
757 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
758 struct cfg80211_internal_bss *new)
759 {
760 const struct cfg80211_bss_ies *ies;
761 struct cfg80211_internal_bss *bss;
762 const u8 *ie;
763 int i, ssidlen;
764 u8 fold = 0;
765 u32 n_entries = 0;
766
767 ies = rcu_access_pointer(new->pub.beacon_ies);
768 if (WARN_ON(!ies))
769 return false;
770
771 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
772 if (!ie) {
773 /* nothing to do */
774 return true;
775 }
776
777 ssidlen = ie[1];
778 for (i = 0; i < ssidlen; i++)
779 fold |= ie[2 + i];
780
781 if (fold) {
782 /* not a hidden SSID */
783 return true;
784 }
785
786 /* This is the bad part ... */
787
788 list_for_each_entry(bss, &rdev->bss_list, list) {
789 /*
790 * we're iterating all the entries anyway, so take the
791 * opportunity to validate the list length accounting
792 */
793 n_entries++;
794
795 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
796 continue;
797 if (bss->pub.channel != new->pub.channel)
798 continue;
799 if (bss->pub.scan_width != new->pub.scan_width)
800 continue;
801 if (rcu_access_pointer(bss->pub.beacon_ies))
802 continue;
803 ies = rcu_access_pointer(bss->pub.ies);
804 if (!ies)
805 continue;
806 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
807 if (!ie)
808 continue;
809 if (ssidlen && ie[1] != ssidlen)
810 continue;
811 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
812 continue;
813 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
814 list_del(&bss->hidden_list);
815 /* combine them */
816 list_add(&bss->hidden_list, &new->hidden_list);
817 bss->pub.hidden_beacon_bss = &new->pub;
818 new->refcount += bss->refcount;
819 rcu_assign_pointer(bss->pub.beacon_ies,
820 new->pub.beacon_ies);
821 }
822
823 WARN_ONCE(n_entries != rdev->bss_entries,
824 "rdev bss entries[%d]/list[len:%d] corruption\n",
825 rdev->bss_entries, n_entries);
826
827 return true;
828 }
829
830 /* Returned bss is reference counted and must be cleaned up appropriately. */
831 static struct cfg80211_internal_bss *
832 cfg80211_bss_update(struct cfg80211_registered_device *rdev,
833 struct cfg80211_internal_bss *tmp,
834 bool signal_valid)
835 {
836 struct cfg80211_internal_bss *found = NULL;
837
838 if (WARN_ON(!tmp->pub.channel))
839 return NULL;
840
841 tmp->ts = jiffies;
842
843 spin_lock_bh(&rdev->bss_lock);
844
845 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
846 spin_unlock_bh(&rdev->bss_lock);
847 return NULL;
848 }
849
850 found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
851
852 if (found) {
853 /* Update IEs */
854 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
855 const struct cfg80211_bss_ies *old;
856
857 old = rcu_access_pointer(found->pub.proberesp_ies);
858
859 rcu_assign_pointer(found->pub.proberesp_ies,
860 tmp->pub.proberesp_ies);
861 /* Override possible earlier Beacon frame IEs */
862 rcu_assign_pointer(found->pub.ies,
863 tmp->pub.proberesp_ies);
864 if (old)
865 kfree_rcu((struct cfg80211_bss_ies *)old,
866 rcu_head);
867 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
868 const struct cfg80211_bss_ies *old;
869 struct cfg80211_internal_bss *bss;
870
871 if (found->pub.hidden_beacon_bss &&
872 !list_empty(&found->hidden_list)) {
873 const struct cfg80211_bss_ies *f;
874
875 /*
876 * The found BSS struct is one of the probe
877 * response members of a group, but we're
878 * receiving a beacon (beacon_ies in the tmp
879 * bss is used). This can only mean that the
880 * AP changed its beacon from not having an
881 * SSID to showing it, which is confusing so
882 * drop this information.
883 */
884
885 f = rcu_access_pointer(tmp->pub.beacon_ies);
886 kfree_rcu((struct cfg80211_bss_ies *)f,
887 rcu_head);
888 goto drop;
889 }
890
891 old = rcu_access_pointer(found->pub.beacon_ies);
892
893 rcu_assign_pointer(found->pub.beacon_ies,
894 tmp->pub.beacon_ies);
895
896 /* Override IEs if they were from a beacon before */
897 if (old == rcu_access_pointer(found->pub.ies))
898 rcu_assign_pointer(found->pub.ies,
899 tmp->pub.beacon_ies);
900
901 /* Assign beacon IEs to all sub entries */
902 list_for_each_entry(bss, &found->hidden_list,
903 hidden_list) {
904 const struct cfg80211_bss_ies *ies;
905
906 ies = rcu_access_pointer(bss->pub.beacon_ies);
907 WARN_ON(ies != old);
908
909 rcu_assign_pointer(bss->pub.beacon_ies,
910 tmp->pub.beacon_ies);
911 }
912
913 if (old)
914 kfree_rcu((struct cfg80211_bss_ies *)old,
915 rcu_head);
916 }
917
918 found->pub.beacon_interval = tmp->pub.beacon_interval;
919 /*
920 * don't update the signal if beacon was heard on
921 * adjacent channel.
922 */
923 if (signal_valid)
924 found->pub.signal = tmp->pub.signal;
925 found->pub.capability = tmp->pub.capability;
926 found->ts = tmp->ts;
927 found->ts_boottime = tmp->ts_boottime;
928 } else {
929 struct cfg80211_internal_bss *new;
930 struct cfg80211_internal_bss *hidden;
931 struct cfg80211_bss_ies *ies;
932
933 /*
934 * create a copy -- the "res" variable that is passed in
935 * is allocated on the stack since it's not needed in the
936 * more common case of an update
937 */
938 new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
939 GFP_ATOMIC);
940 if (!new) {
941 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
942 if (ies)
943 kfree_rcu(ies, rcu_head);
944 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
945 if (ies)
946 kfree_rcu(ies, rcu_head);
947 goto drop;
948 }
949 memcpy(new, tmp, sizeof(*new));
950 new->refcount = 1;
951 INIT_LIST_HEAD(&new->hidden_list);
952
953 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
954 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
955 if (!hidden)
956 hidden = rb_find_bss(rdev, tmp,
957 BSS_CMP_HIDE_NUL);
958 if (hidden) {
959 new->pub.hidden_beacon_bss = &hidden->pub;
960 list_add(&new->hidden_list,
961 &hidden->hidden_list);
962 hidden->refcount++;
963 rcu_assign_pointer(new->pub.beacon_ies,
964 hidden->pub.beacon_ies);
965 }
966 } else {
967 /*
968 * Ok so we found a beacon, and don't have an entry. If
969 * it's a beacon with hidden SSID, we might be in for an
970 * expensive search for any probe responses that should
971 * be grouped with this beacon for updates ...
972 */
973 if (!cfg80211_combine_bsses(rdev, new)) {
974 kfree(new);
975 goto drop;
976 }
977 }
978
979 if (rdev->bss_entries >= bss_entries_limit &&
980 !cfg80211_bss_expire_oldest(rdev)) {
981 kfree(new);
982 goto drop;
983 }
984
985 list_add_tail(&new->list, &rdev->bss_list);
986 rdev->bss_entries++;
987 rb_insert_bss(rdev, new);
988 found = new;
989 }
990
991 rdev->bss_generation++;
992 bss_ref_get(rdev, found);
993 spin_unlock_bh(&rdev->bss_lock);
994
995 return found;
996 drop:
997 spin_unlock_bh(&rdev->bss_lock);
998 return NULL;
999 }
1000
1001 static struct ieee80211_channel *
1002 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
1003 struct ieee80211_channel *channel)
1004 {
1005 const u8 *tmp;
1006 u32 freq;
1007 int channel_number = -1;
1008
1009 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
1010 if (tmp && tmp[1] == 1) {
1011 channel_number = tmp[2];
1012 } else {
1013 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
1014 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
1015 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
1016
1017 channel_number = htop->primary_chan;
1018 }
1019 }
1020
1021 if (channel_number < 0)
1022 return channel;
1023
1024 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
1025 channel = ieee80211_get_channel(wiphy, freq);
1026 if (!channel)
1027 return NULL;
1028 if (channel->flags & IEEE80211_CHAN_DISABLED)
1029 return NULL;
1030 return channel;
1031 }
1032
1033 /* Returned bss is reference counted and must be cleaned up appropriately. */
1034 struct cfg80211_bss *
1035 cfg80211_inform_bss_data(struct wiphy *wiphy,
1036 struct cfg80211_inform_bss *data,
1037 enum cfg80211_bss_frame_type ftype,
1038 const u8 *bssid, u64 tsf, u16 capability,
1039 u16 beacon_interval, const u8 *ie, size_t ielen,
1040 gfp_t gfp)
1041 {
1042 struct cfg80211_bss_ies *ies;
1043 struct ieee80211_channel *channel;
1044 struct cfg80211_internal_bss tmp = {}, *res;
1045 int bss_type;
1046 bool signal_valid;
1047
1048 if (WARN_ON(!wiphy))
1049 return NULL;
1050
1051 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1052 (data->signal < 0 || data->signal > 100)))
1053 return NULL;
1054
1055 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan);
1056 if (!channel)
1057 return NULL;
1058
1059 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
1060 tmp.pub.channel = channel;
1061 tmp.pub.scan_width = data->scan_width;
1062 tmp.pub.signal = data->signal;
1063 tmp.pub.beacon_interval = beacon_interval;
1064 tmp.pub.capability = capability;
1065 tmp.ts_boottime = data->boottime_ns;
1066
1067 /*
1068 * If we do not know here whether the IEs are from a Beacon or Probe
1069 * Response frame, we need to pick one of the options and only use it
1070 * with the driver that does not provide the full Beacon/Probe Response
1071 * frame. Use Beacon frame pointer to avoid indicating that this should
1072 * override the IEs pointer should we have received an earlier
1073 * indication of Probe Response data.
1074 */
1075 ies = kzalloc(sizeof(*ies) + ielen, gfp);
1076 if (!ies)
1077 return NULL;
1078 ies->len = ielen;
1079 ies->tsf = tsf;
1080 ies->from_beacon = false;
1081 memcpy(ies->data, ie, ielen);
1082
1083 switch (ftype) {
1084 case CFG80211_BSS_FTYPE_BEACON:
1085 ies->from_beacon = true;
1086 /* fall through to assign */
1087 case CFG80211_BSS_FTYPE_UNKNOWN:
1088 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1089 break;
1090 case CFG80211_BSS_FTYPE_PRESP:
1091 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1092 break;
1093 }
1094 rcu_assign_pointer(tmp.pub.ies, ies);
1095
1096 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
1097 wiphy->max_adj_channel_rssi_comp;
1098 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
1099 if (!res)
1100 return NULL;
1101
1102 if (channel->band == NL80211_BAND_60GHZ) {
1103 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1104 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1105 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1106 regulatory_hint_found_beacon(wiphy, channel, gfp);
1107 } else {
1108 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1109 regulatory_hint_found_beacon(wiphy, channel, gfp);
1110 }
1111
1112 trace_cfg80211_return_bss(&res->pub);
1113 /* cfg80211_bss_update gives us a referenced result */
1114 return &res->pub;
1115 }
1116 EXPORT_SYMBOL(cfg80211_inform_bss_data);
1117
1118 /* cfg80211_inform_bss_width_frame helper */
1119 struct cfg80211_bss *
1120 cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1121 struct cfg80211_inform_bss *data,
1122 struct ieee80211_mgmt *mgmt, size_t len,
1123 gfp_t gfp)
1124
1125 {
1126 struct cfg80211_internal_bss tmp = {}, *res;
1127 struct cfg80211_bss_ies *ies;
1128 struct ieee80211_channel *channel;
1129 bool signal_valid;
1130 size_t ielen = len - offsetof(struct ieee80211_mgmt,
1131 u.probe_resp.variable);
1132 int bss_type;
1133
1134 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
1135 offsetof(struct ieee80211_mgmt, u.beacon.variable));
1136
1137 trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
1138
1139 if (WARN_ON(!mgmt))
1140 return NULL;
1141
1142 if (WARN_ON(!wiphy))
1143 return NULL;
1144
1145 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
1146 (data->signal < 0 || data->signal > 100)))
1147 return NULL;
1148
1149 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
1150 return NULL;
1151
1152 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
1153 ielen, data->chan);
1154 if (!channel)
1155 return NULL;
1156
1157 ies = kzalloc(sizeof(*ies) + ielen, gfp);
1158 if (!ies)
1159 return NULL;
1160 ies->len = ielen;
1161 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
1162 ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
1163 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
1164
1165 if (ieee80211_is_probe_resp(mgmt->frame_control))
1166 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
1167 else
1168 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
1169 rcu_assign_pointer(tmp.pub.ies, ies);
1170
1171 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
1172 tmp.pub.channel = channel;
1173 tmp.pub.scan_width = data->scan_width;
1174 tmp.pub.signal = data->signal;
1175 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
1176 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
1177 tmp.ts_boottime = data->boottime_ns;
1178
1179 signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
1180 wiphy->max_adj_channel_rssi_comp;
1181 res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
1182 if (!res)
1183 return NULL;
1184
1185 if (channel->band == NL80211_BAND_60GHZ) {
1186 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1187 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1188 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1189 regulatory_hint_found_beacon(wiphy, channel, gfp);
1190 } else {
1191 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1192 regulatory_hint_found_beacon(wiphy, channel, gfp);
1193 }
1194
1195 trace_cfg80211_return_bss(&res->pub);
1196 /* cfg80211_bss_update gives us a referenced result */
1197 return &res->pub;
1198 }
1199 EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
1200
1201 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1202 {
1203 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1204 struct cfg80211_internal_bss *bss;
1205
1206 if (!pub)
1207 return;
1208
1209 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1210
1211 spin_lock_bh(&rdev->bss_lock);
1212 bss_ref_get(rdev, bss);
1213 spin_unlock_bh(&rdev->bss_lock);
1214 }
1215 EXPORT_SYMBOL(cfg80211_ref_bss);
1216
1217 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1218 {
1219 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1220 struct cfg80211_internal_bss *bss;
1221
1222 if (!pub)
1223 return;
1224
1225 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1226
1227 spin_lock_bh(&rdev->bss_lock);
1228 bss_ref_put(rdev, bss);
1229 spin_unlock_bh(&rdev->bss_lock);
1230 }
1231 EXPORT_SYMBOL(cfg80211_put_bss);
1232
1233 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1234 {
1235 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1236 struct cfg80211_internal_bss *bss;
1237
1238 if (WARN_ON(!pub))
1239 return;
1240
1241 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1242
1243 spin_lock_bh(&rdev->bss_lock);
1244 if (!list_empty(&bss->list)) {
1245 if (__cfg80211_unlink_bss(rdev, bss))
1246 rdev->bss_generation++;
1247 }
1248 spin_unlock_bh(&rdev->bss_lock);
1249 }
1250 EXPORT_SYMBOL(cfg80211_unlink_bss);
1251
1252 #ifdef CONFIG_CFG80211_WEXT
1253 static struct cfg80211_registered_device *
1254 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
1255 {
1256 struct cfg80211_registered_device *rdev;
1257 struct net_device *dev;
1258
1259 ASSERT_RTNL();
1260
1261 dev = dev_get_by_index(net, ifindex);
1262 if (!dev)
1263 return ERR_PTR(-ENODEV);
1264 if (dev->ieee80211_ptr)
1265 rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
1266 else
1267 rdev = ERR_PTR(-ENODEV);
1268 dev_put(dev);
1269 return rdev;
1270 }
1271
1272 int cfg80211_wext_siwscan(struct net_device *dev,
1273 struct iw_request_info *info,
1274 union iwreq_data *wrqu, char *extra)
1275 {
1276 struct cfg80211_registered_device *rdev;
1277 struct wiphy *wiphy;
1278 struct iw_scan_req *wreq = NULL;
1279 struct cfg80211_scan_request *creq = NULL;
1280 int i, err, n_channels = 0;
1281 enum nl80211_band band;
1282
1283 if (!netif_running(dev))
1284 return -ENETDOWN;
1285
1286 if (wrqu->data.length == sizeof(struct iw_scan_req))
1287 wreq = (struct iw_scan_req *)extra;
1288
1289 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
1290
1291 if (IS_ERR(rdev))
1292 return PTR_ERR(rdev);
1293
1294 if (rdev->scan_req || rdev->scan_msg) {
1295 err = -EBUSY;
1296 goto out;
1297 }
1298
1299 wiphy = &rdev->wiphy;
1300
1301 /* Determine number of channels, needed to allocate creq */
1302 if (wreq && wreq->num_channels)
1303 n_channels = wreq->num_channels;
1304 else
1305 n_channels = ieee80211_get_num_supported_channels(wiphy);
1306
1307 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1308 n_channels * sizeof(void *),
1309 GFP_ATOMIC);
1310 if (!creq) {
1311 err = -ENOMEM;
1312 goto out;
1313 }
1314
1315 creq->wiphy = wiphy;
1316 creq->wdev = dev->ieee80211_ptr;
1317 /* SSIDs come after channels */
1318 creq->ssids = (void *)&creq->channels[n_channels];
1319 creq->n_channels = n_channels;
1320 creq->n_ssids = 1;
1321 creq->scan_start = jiffies;
1322
1323 /* translate "Scan on frequencies" request */
1324 i = 0;
1325 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1326 int j;
1327
1328 if (!wiphy->bands[band])
1329 continue;
1330
1331 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
1332 /* ignore disabled channels */
1333 if (wiphy->bands[band]->channels[j].flags &
1334 IEEE80211_CHAN_DISABLED)
1335 continue;
1336
1337 /* If we have a wireless request structure and the
1338 * wireless request specifies frequencies, then search
1339 * for the matching hardware channel.
1340 */
1341 if (wreq && wreq->num_channels) {
1342 int k;
1343 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1344 for (k = 0; k < wreq->num_channels; k++) {
1345 struct iw_freq *freq =
1346 &wreq->channel_list[k];
1347 int wext_freq =
1348 cfg80211_wext_freq(freq);
1349
1350 if (wext_freq == wiphy_freq)
1351 goto wext_freq_found;
1352 }
1353 goto wext_freq_not_found;
1354 }
1355
1356 wext_freq_found:
1357 creq->channels[i] = &wiphy->bands[band]->channels[j];
1358 i++;
1359 wext_freq_not_found: ;
1360 }
1361 }
1362 /* No channels found? */
1363 if (!i) {
1364 err = -EINVAL;
1365 goto out;
1366 }
1367
1368 /* Set real number of channels specified in creq->channels[] */
1369 creq->n_channels = i;
1370
1371 /* translate "Scan for SSID" request */
1372 if (wreq) {
1373 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1374 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1375 err = -EINVAL;
1376 goto out;
1377 }
1378 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1379 creq->ssids[0].ssid_len = wreq->essid_len;
1380 }
1381 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1382 creq->n_ssids = 0;
1383 }
1384
1385 for (i = 0; i < NUM_NL80211_BANDS; i++)
1386 if (wiphy->bands[i])
1387 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
1388
1389 rdev->scan_req = creq;
1390 err = rdev_scan(rdev, creq);
1391 if (err) {
1392 rdev->scan_req = NULL;
1393 /* creq will be freed below */
1394 } else {
1395 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
1396 /* creq now owned by driver */
1397 creq = NULL;
1398 dev_hold(dev);
1399 }
1400 out:
1401 kfree(creq);
1402 return err;
1403 }
1404 EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
1405
1406 static char *ieee80211_scan_add_ies(struct iw_request_info *info,
1407 const struct cfg80211_bss_ies *ies,
1408 char *current_ev, char *end_buf)
1409 {
1410 const u8 *pos, *end, *next;
1411 struct iw_event iwe;
1412
1413 if (!ies)
1414 return current_ev;
1415
1416 /*
1417 * If needed, fragment the IEs buffer (at IE boundaries) into short
1418 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1419 */
1420 pos = ies->data;
1421 end = pos + ies->len;
1422
1423 while (end - pos > IW_GENERIC_IE_MAX) {
1424 next = pos + 2 + pos[1];
1425 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1426 next = next + 2 + next[1];
1427
1428 memset(&iwe, 0, sizeof(iwe));
1429 iwe.cmd = IWEVGENIE;
1430 iwe.u.data.length = next - pos;
1431 current_ev = iwe_stream_add_point_check(info, current_ev,
1432 end_buf, &iwe,
1433 (void *)pos);
1434 if (IS_ERR(current_ev))
1435 return current_ev;
1436 pos = next;
1437 }
1438
1439 if (end > pos) {
1440 memset(&iwe, 0, sizeof(iwe));
1441 iwe.cmd = IWEVGENIE;
1442 iwe.u.data.length = end - pos;
1443 current_ev = iwe_stream_add_point_check(info, current_ev,
1444 end_buf, &iwe,
1445 (void *)pos);
1446 if (IS_ERR(current_ev))
1447 return current_ev;
1448 }
1449
1450 return current_ev;
1451 }
1452
1453 static char *
1454 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1455 struct cfg80211_internal_bss *bss, char *current_ev,
1456 char *end_buf)
1457 {
1458 const struct cfg80211_bss_ies *ies;
1459 struct iw_event iwe;
1460 const u8 *ie;
1461 u8 buf[50];
1462 u8 *cfg, *p, *tmp;
1463 int rem, i, sig;
1464 bool ismesh = false;
1465
1466 memset(&iwe, 0, sizeof(iwe));
1467 iwe.cmd = SIOCGIWAP;
1468 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1469 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
1470 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
1471 IW_EV_ADDR_LEN);
1472 if (IS_ERR(current_ev))
1473 return current_ev;
1474
1475 memset(&iwe, 0, sizeof(iwe));
1476 iwe.cmd = SIOCGIWFREQ;
1477 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
1478 iwe.u.freq.e = 0;
1479 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
1480 IW_EV_FREQ_LEN);
1481 if (IS_ERR(current_ev))
1482 return current_ev;
1483
1484 memset(&iwe, 0, sizeof(iwe));
1485 iwe.cmd = SIOCGIWFREQ;
1486 iwe.u.freq.m = bss->pub.channel->center_freq;
1487 iwe.u.freq.e = 6;
1488 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
1489 IW_EV_FREQ_LEN);
1490 if (IS_ERR(current_ev))
1491 return current_ev;
1492
1493 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
1494 memset(&iwe, 0, sizeof(iwe));
1495 iwe.cmd = IWEVQUAL;
1496 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
1497 IW_QUAL_NOISE_INVALID |
1498 IW_QUAL_QUAL_UPDATED;
1499 switch (wiphy->signal_type) {
1500 case CFG80211_SIGNAL_TYPE_MBM:
1501 sig = bss->pub.signal / 100;
1502 iwe.u.qual.level = sig;
1503 iwe.u.qual.updated |= IW_QUAL_DBM;
1504 if (sig < -110) /* rather bad */
1505 sig = -110;
1506 else if (sig > -40) /* perfect */
1507 sig = -40;
1508 /* will give a range of 0 .. 70 */
1509 iwe.u.qual.qual = sig + 110;
1510 break;
1511 case CFG80211_SIGNAL_TYPE_UNSPEC:
1512 iwe.u.qual.level = bss->pub.signal;
1513 /* will give range 0 .. 100 */
1514 iwe.u.qual.qual = bss->pub.signal;
1515 break;
1516 default:
1517 /* not reached */
1518 break;
1519 }
1520 current_ev = iwe_stream_add_event_check(info, current_ev,
1521 end_buf, &iwe,
1522 IW_EV_QUAL_LEN);
1523 if (IS_ERR(current_ev))
1524 return current_ev;
1525 }
1526
1527 memset(&iwe, 0, sizeof(iwe));
1528 iwe.cmd = SIOCGIWENCODE;
1529 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
1530 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1531 else
1532 iwe.u.data.flags = IW_ENCODE_DISABLED;
1533 iwe.u.data.length = 0;
1534 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
1535 &iwe, "");
1536 if (IS_ERR(current_ev))
1537 return current_ev;
1538
1539 rcu_read_lock();
1540 ies = rcu_dereference(bss->pub.ies);
1541 rem = ies->len;
1542 ie = ies->data;
1543
1544 while (rem >= 2) {
1545 /* invalid data */
1546 if (ie[1] > rem - 2)
1547 break;
1548
1549 switch (ie[0]) {
1550 case WLAN_EID_SSID:
1551 memset(&iwe, 0, sizeof(iwe));
1552 iwe.cmd = SIOCGIWESSID;
1553 iwe.u.data.length = ie[1];
1554 iwe.u.data.flags = 1;
1555 current_ev = iwe_stream_add_point_check(info,
1556 current_ev,
1557 end_buf, &iwe,
1558 (u8 *)ie + 2);
1559 if (IS_ERR(current_ev))
1560 goto unlock;
1561 break;
1562 case WLAN_EID_MESH_ID:
1563 memset(&iwe, 0, sizeof(iwe));
1564 iwe.cmd = SIOCGIWESSID;
1565 iwe.u.data.length = ie[1];
1566 iwe.u.data.flags = 1;
1567 current_ev = iwe_stream_add_point_check(info,
1568 current_ev,
1569 end_buf, &iwe,
1570 (u8 *)ie + 2);
1571 if (IS_ERR(current_ev))
1572 goto unlock;
1573 break;
1574 case WLAN_EID_MESH_CONFIG:
1575 ismesh = true;
1576 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
1577 break;
1578 cfg = (u8 *)ie + 2;
1579 memset(&iwe, 0, sizeof(iwe));
1580 iwe.cmd = IWEVCUSTOM;
1581 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1582 "0x%02X", cfg[0]);
1583 iwe.u.data.length = strlen(buf);
1584 current_ev = iwe_stream_add_point_check(info,
1585 current_ev,
1586 end_buf,
1587 &iwe, buf);
1588 if (IS_ERR(current_ev))
1589 goto unlock;
1590 sprintf(buf, "Path Selection Metric ID: 0x%02X",
1591 cfg[1]);
1592 iwe.u.data.length = strlen(buf);
1593 current_ev = iwe_stream_add_point_check(info,
1594 current_ev,
1595 end_buf,
1596 &iwe, buf);
1597 if (IS_ERR(current_ev))
1598 goto unlock;
1599 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1600 cfg[2]);
1601 iwe.u.data.length = strlen(buf);
1602 current_ev = iwe_stream_add_point_check(info,
1603 current_ev,
1604 end_buf,
1605 &iwe, buf);
1606 if (IS_ERR(current_ev))
1607 goto unlock;
1608 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
1609 iwe.u.data.length = strlen(buf);
1610 current_ev = iwe_stream_add_point_check(info,
1611 current_ev,
1612 end_buf,
1613 &iwe, buf);
1614 if (IS_ERR(current_ev))
1615 goto unlock;
1616 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
1617 iwe.u.data.length = strlen(buf);
1618 current_ev = iwe_stream_add_point_check(info,
1619 current_ev,
1620 end_buf,
1621 &iwe, buf);
1622 if (IS_ERR(current_ev))
1623 goto unlock;
1624 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1625 iwe.u.data.length = strlen(buf);
1626 current_ev = iwe_stream_add_point_check(info,
1627 current_ev,
1628 end_buf,
1629 &iwe, buf);
1630 if (IS_ERR(current_ev))
1631 goto unlock;
1632 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
1633 iwe.u.data.length = strlen(buf);
1634 current_ev = iwe_stream_add_point_check(info,
1635 current_ev,
1636 end_buf,
1637 &iwe, buf);
1638 if (IS_ERR(current_ev))
1639 goto unlock;
1640 break;
1641 case WLAN_EID_SUPP_RATES:
1642 case WLAN_EID_EXT_SUPP_RATES:
1643 /* display all supported rates in readable format */
1644 p = current_ev + iwe_stream_lcp_len(info);
1645
1646 memset(&iwe, 0, sizeof(iwe));
1647 iwe.cmd = SIOCGIWRATE;
1648 /* Those two flags are ignored... */
1649 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1650
1651 for (i = 0; i < ie[1]; i++) {
1652 iwe.u.bitrate.value =
1653 ((ie[i + 2] & 0x7f) * 500000);
1654 tmp = p;
1655 p = iwe_stream_add_value(info, current_ev, p,
1656 end_buf, &iwe,
1657 IW_EV_PARAM_LEN);
1658 if (p == tmp) {
1659 current_ev = ERR_PTR(-E2BIG);
1660 goto unlock;
1661 }
1662 }
1663 current_ev = p;
1664 break;
1665 }
1666 rem -= ie[1] + 2;
1667 ie += ie[1] + 2;
1668 }
1669
1670 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1671 ismesh) {
1672 memset(&iwe, 0, sizeof(iwe));
1673 iwe.cmd = SIOCGIWMODE;
1674 if (ismesh)
1675 iwe.u.mode = IW_MODE_MESH;
1676 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
1677 iwe.u.mode = IW_MODE_MASTER;
1678 else
1679 iwe.u.mode = IW_MODE_ADHOC;
1680 current_ev = iwe_stream_add_event_check(info, current_ev,
1681 end_buf, &iwe,
1682 IW_EV_UINT_LEN);
1683 if (IS_ERR(current_ev))
1684 goto unlock;
1685 }
1686
1687 memset(&iwe, 0, sizeof(iwe));
1688 iwe.cmd = IWEVCUSTOM;
1689 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
1690 iwe.u.data.length = strlen(buf);
1691 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
1692 &iwe, buf);
1693 if (IS_ERR(current_ev))
1694 goto unlock;
1695 memset(&iwe, 0, sizeof(iwe));
1696 iwe.cmd = IWEVCUSTOM;
1697 sprintf(buf, " Last beacon: %ums ago",
1698 elapsed_jiffies_msecs(bss->ts));
1699 iwe.u.data.length = strlen(buf);
1700 current_ev = iwe_stream_add_point_check(info, current_ev,
1701 end_buf, &iwe, buf);
1702 if (IS_ERR(current_ev))
1703 goto unlock;
1704
1705 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
1706
1707 unlock:
1708 rcu_read_unlock();
1709 return current_ev;
1710 }
1711
1712
1713 static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
1714 struct iw_request_info *info,
1715 char *buf, size_t len)
1716 {
1717 char *current_ev = buf;
1718 char *end_buf = buf + len;
1719 struct cfg80211_internal_bss *bss;
1720 int err = 0;
1721
1722 spin_lock_bh(&rdev->bss_lock);
1723 cfg80211_bss_expire(rdev);
1724
1725 list_for_each_entry(bss, &rdev->bss_list, list) {
1726 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1727 err = -E2BIG;
1728 break;
1729 }
1730 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
1731 current_ev, end_buf);
1732 if (IS_ERR(current_ev)) {
1733 err = PTR_ERR(current_ev);
1734 break;
1735 }
1736 }
1737 spin_unlock_bh(&rdev->bss_lock);
1738
1739 if (err)
1740 return err;
1741 return current_ev - buf;
1742 }
1743
1744
1745 int cfg80211_wext_giwscan(struct net_device *dev,
1746 struct iw_request_info *info,
1747 struct iw_point *data, char *extra)
1748 {
1749 struct cfg80211_registered_device *rdev;
1750 int res;
1751
1752 if (!netif_running(dev))
1753 return -ENETDOWN;
1754
1755 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
1756
1757 if (IS_ERR(rdev))
1758 return PTR_ERR(rdev);
1759
1760 if (rdev->scan_req || rdev->scan_msg)
1761 return -EAGAIN;
1762
1763 res = ieee80211_scan_results(rdev, info, extra, data->length);
1764 data->length = 0;
1765 if (res >= 0) {
1766 data->length = res;
1767 res = 0;
1768 }
1769
1770 return res;
1771 }
1772 EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
1773 #endif