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