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