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