~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/wireless/scan.c

Version: ~ [ linux-5.5 ] ~ [ linux-5.4.15 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.98 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.167 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.211 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.211 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp