blob: 45f1618c8e239c2db21692cfa5358f460918b76d [file] [log] [blame]
Johannes Berg2a519312009-02-10 21:25:55 +01001/*
2 * cfg80211 scan result handling
3 *
4 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
5 */
6#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Johannes Berg2a519312009-02-10 21:25:55 +01008#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 Berg262eb9b22011-07-13 10:39:09 +020015#include <net/cfg80211-wext.h>
Johannes Berg2a519312009-02-10 21:25:55 +010016#include <net/iw_handler.h>
17#include "core.h"
18#include "nl80211.h"
Johannes Berga9a11622009-07-27 12:01:53 +020019#include "wext-compat.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030020#include "rdev-ops.h"
Johannes Berg2a519312009-02-10 21:25:55 +010021
Rajkumar Manoharanf9616e02012-04-13 16:38:40 +053022#define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
Johannes Berg2a519312009-02-10 21:25:55 +010023
Amitkumar Karware8e27c62012-10-11 21:03:33 -070024static void bss_release(struct kref *ref)
25{
Johannes Berg9caf0362012-11-29 01:25:20 +010026 struct cfg80211_bss_ies *ies;
Amitkumar Karware8e27c62012-10-11 21:03:33 -070027 struct cfg80211_internal_bss *bss;
28
29 bss = container_of(ref, struct cfg80211_internal_bss, ref);
Johannes Bergb629ea32012-11-28 22:14:56 +010030
31 if (WARN_ON(atomic_read(&bss->hold)))
32 return;
33
Amitkumar Karware8e27c62012-10-11 21:03:33 -070034 if (bss->pub.free_priv)
35 bss->pub.free_priv(&bss->pub);
36
Johannes Berg9caf0362012-11-29 01:25:20 +010037 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 Karware8e27c62012-10-11 21:03:33 -070043
Amitkumar Karware8e27c62012-10-11 21:03:33 -070044 kfree(bss);
45}
46
47/* must hold dev->bss_lock! */
48static 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 Leffler15d60302012-10-11 21:03:34 -070056/* must hold dev->bss_lock! */
57static 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 Berg01a0ac42009-08-20 21:36:16 +020077void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak)
Johannes Berg2a519312009-02-10 21:25:55 +010078{
Johannes Berg667503dd2009-07-07 03:56:11 +020079 struct cfg80211_scan_request *request;
Johannes Bergfd014282012-06-18 19:17:03 +020080 struct wireless_dev *wdev;
Johannes Berg3d23e342009-09-29 23:27:28 +020081#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +010082 union iwreq_data wrqu;
83#endif
84
Johannes Berg01a0ac42009-08-20 21:36:16 +020085 ASSERT_RDEV_LOCK(rdev);
86
Johannes Berg667503dd2009-07-07 03:56:11 +020087 request = rdev->scan_req;
88
Johannes Berg01a0ac42009-08-20 21:36:16 +020089 if (!request)
90 return;
91
Johannes Bergfd014282012-06-18 19:17:03 +020092 wdev = request->wdev;
Johannes Berg2a519312009-02-10 21:25:55 +010093
Johannes Berg6829c872009-07-02 09:13:27 +020094 /*
95 * This must be before sending the other events!
96 * Otherwise, wpa_supplicant gets completely confused with
97 * wext events.
98 */
Johannes Bergfd014282012-06-18 19:17:03 +020099 if (wdev->netdev)
100 cfg80211_sme_scan_done(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200101
Sam Leffler15d60302012-10-11 21:03:34 -0700102 if (request->aborted) {
Johannes Bergfd014282012-06-18 19:17:03 +0200103 nl80211_send_scan_aborted(rdev, wdev);
Sam Leffler15d60302012-10-11 21:03:34 -0700104 } 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 Bergfd014282012-06-18 19:17:03 +0200111 nl80211_send_scan_done(rdev, wdev);
Sam Leffler15d60302012-10-11 21:03:34 -0700112 }
Johannes Berg2a519312009-02-10 21:25:55 +0100113
Johannes Berg3d23e342009-09-29 23:27:28 +0200114#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfd014282012-06-18 19:17:03 +0200115 if (wdev->netdev && !request->aborted) {
Johannes Berg2a519312009-02-10 21:25:55 +0100116 memset(&wrqu, 0, sizeof(wrqu));
117
Johannes Bergfd014282012-06-18 19:17:03 +0200118 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100119 }
120#endif
121
Johannes Bergfd014282012-06-18 19:17:03 +0200122 if (wdev->netdev)
123 dev_put(wdev->netdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100124
Johannes Berg36e6fea2009-08-12 22:21:21 +0200125 rdev->scan_req = NULL;
Johannes Berg01a0ac42009-08-20 21:36:16 +0200126
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 Berg2a519312009-02-10 21:25:55 +0100137}
Johannes Berg667503dd2009-07-07 03:56:11 +0200138
Johannes Berg36e6fea2009-08-12 22:21:21 +0200139void __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 Berg01a0ac42009-08-20 21:36:16 +0200147 ___cfg80211_scan_done(rdev, false);
Johannes Berg36e6fea2009-08-12 22:21:21 +0200148 cfg80211_unlock_rdev(rdev);
149}
150
Johannes Berg667503dd2009-07-07 03:56:11 +0200151void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
152{
Beni Lev4ee3e062012-08-27 12:49:39 +0300153 trace_cfg80211_scan_done(request, aborted);
Johannes Berg667503dd2009-07-07 03:56:11 +0200154 WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
155
156 request->aborted = aborted;
Alban Browaeyse60d7442009-11-25 15:13:00 +0100157 queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk);
Johannes Berg667503dd2009-07-07 03:56:11 +0200158}
Johannes Berg2a519312009-02-10 21:25:55 +0100159EXPORT_SYMBOL(cfg80211_scan_done);
160
Luciano Coelho807f8a82011-05-11 17:09:35 +0300161void __cfg80211_sched_scan_results(struct work_struct *wk)
162{
163 struct cfg80211_registered_device *rdev;
Sam Leffler15d60302012-10-11 21:03:34 -0700164 struct cfg80211_sched_scan_request *request;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300165
166 rdev = container_of(wk, struct cfg80211_registered_device,
167 sched_scan_results_wk);
168
Sam Leffler15d60302012-10-11 21:03:34 -0700169 request = rdev->sched_scan_req;
170
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300171 mutex_lock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300172
173 /* we don't have sched_scan_req anymore if the scan is stopping */
Sam Leffler15d60302012-10-11 21:03:34 -0700174 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 Coelho807f8a82011-05-11 17:09:35 +0300185
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300186 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300187}
188
189void cfg80211_sched_scan_results(struct wiphy *wiphy)
190{
Beni Lev4ee3e062012-08-27 12:49:39 +0300191 trace_cfg80211_sched_scan_results(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300192 /* 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}
197EXPORT_SYMBOL(cfg80211_sched_scan_results);
198
Luciano Coelho85a99942011-05-12 16:28:29 +0300199void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300200{
Luciano Coelho85a99942011-05-12 16:28:29 +0300201 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300202
Beni Lev4ee3e062012-08-27 12:49:39 +0300203 trace_cfg80211_sched_scan_stopped(wiphy);
204
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300205 mutex_lock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300206 __cfg80211_stop_sched_scan(rdev, true);
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300207 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300208}
Luciano Coelho807f8a82011-05-11 17:09:35 +0300209EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
210
211int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
212 bool driver_initiated)
213{
Luciano Coelho807f8a82011-05-11 17:09:35 +0300214 struct net_device *dev;
215
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300216 lockdep_assert_held(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300217
218 if (!rdev->sched_scan_req)
Luciano Coelho1a84ff72011-07-08 11:16:16 +0300219 return -ENOENT;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300220
221 dev = rdev->sched_scan_req->dev;
222
Luciano Coelho85a99942011-05-12 16:28:29 +0300223 if (!driver_initiated) {
Hila Gonene35e4d22012-06-27 17:19:42 +0300224 int err = rdev_sched_scan_stop(rdev, dev);
Luciano Coelho85a99942011-05-12 16:28:29 +0300225 if (err)
226 return err;
227 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300228
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 Juhl3b4670f2011-06-29 22:49:33 +0200234 return 0;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300235}
236
Johannes Berg2a519312009-02-10 21:25:55 +0100237/* must hold dev->bss_lock! */
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500238void 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 Berg915de2f2012-11-28 22:39:37 +0100244 list_for_each_entry(bss, &dev->bss_list, list)
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500245 bss->ts -= age_jiffies;
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500246}
247
Johannes Berg2a519312009-02-10 21:25:55 +0100248void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
249{
Sam Leffler15d60302012-10-11 21:03:34 -0700250 __cfg80211_bss_expire(dev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
Johannes Berg2a519312009-02-10 21:25:55 +0100251}
252
Johannes Bergc21dbf92010-01-26 14:15:46 +0100253const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
Johannes Berg2a519312009-02-10 21:25:55 +0100254{
Johannes Bergc21dbf92010-01-26 14:15:46 +0100255 while (len > 2 && ies[0] != eid) {
Johannes Berg2a519312009-02-10 21:25:55 +0100256 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 Bergc21dbf92010-01-26 14:15:46 +0100265EXPORT_SYMBOL(cfg80211_find_ie);
Johannes Berg2a519312009-02-10 21:25:55 +0100266
Eliad Peller0c28ec52011-09-15 11:53:01 +0300267const 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}
292EXPORT_SYMBOL(cfg80211_find_vendor_ie);
293
Johannes Berg9caf0362012-11-29 01:25:20 +0100294static int cmp_ies(u8 num, const u8 *ies1, int len1, const u8 *ies2, int len2)
Johannes Berg2a519312009-02-10 21:25:55 +0100295{
Johannes Bergc21dbf92010-01-26 14:15:46 +0100296 const u8 *ie1 = cfg80211_find_ie(num, ies1, len1);
297 const u8 *ie2 = cfg80211_find_ie(num, ies2, len2);
Johannes Berg2a519312009-02-10 21:25:55 +0100298
Johannes Berg3b6ef632011-11-04 11:01:46 +0100299 /* equal if both missing */
Johannes Berg2a519312009-02-10 21:25:55 +0100300 if (!ie1 && !ie2)
301 return 0;
Johannes Berg3b6ef632011-11-04 11:01:46 +0100302 /* sort missing IE before (left of) present IE */
303 if (!ie1)
Johannes Berg2a519312009-02-10 21:25:55 +0100304 return -1;
Johannes Berg3b6ef632011-11-04 11:01:46 +0100305 if (!ie2)
306 return 1;
Johannes Berg2a519312009-02-10 21:25:55 +0100307
Johannes Berg3b6ef632011-11-04 11:01:46 +0100308 /* sort by length first, then by contents */
309 if (ie1[1] != ie2[1])
Johannes Berg2a519312009-02-10 21:25:55 +0100310 return ie2[1] - ie1[1];
Johannes Berg3b6ef632011-11-04 11:01:46 +0100311 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
Johannes Berg2a519312009-02-10 21:25:55 +0100312}
313
Johannes Berg915de2f2012-11-28 22:39:37 +0100314static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
Johannes Berg2a519312009-02-10 21:25:55 +0100315 const u8 *ssid, size_t ssid_len)
316{
Johannes Berg9caf0362012-11-29 01:25:20 +0100317 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +0100318 const u8 *ssidie;
319
Joe Perchesac422d32012-05-08 18:56:55 +0000320 if (bssid && !ether_addr_equal(a->bssid, bssid))
Johannes Berg2a519312009-02-10 21:25:55 +0100321 return false;
322
Johannes Berg79420f02009-02-10 21:25:59 +0100323 if (!ssid)
324 return true;
325
Johannes Berg9caf0362012-11-29 01:25:20 +0100326 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 Berg2a519312009-02-10 21:25:55 +0100330 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 Peller333ba732011-05-29 15:53:20 +0300337static bool is_mesh_bss(struct cfg80211_bss *a)
338{
Johannes Berg9caf0362012-11-29 01:25:20 +0100339 const struct cfg80211_bss_ies *ies;
Eliad Peller333ba732011-05-29 15:53:20 +0300340 const u8 *ie;
341
342 if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
343 return false;
344
Johannes Berg9caf0362012-11-29 01:25:20 +0100345 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 Peller333ba732011-05-29 15:53:20 +0300350 if (!ie)
351 return false;
352
Johannes Berg9caf0362012-11-29 01:25:20 +0100353 ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG, ies->data, ies->len);
Eliad Peller333ba732011-05-29 15:53:20 +0300354 if (!ie)
355 return false;
356
357 return true;
358}
359
Johannes Berg2a519312009-02-10 21:25:55 +0100360static bool is_mesh(struct cfg80211_bss *a,
361 const u8 *meshid, size_t meshidlen,
362 const u8 *meshcfg)
363{
Johannes Berg9caf0362012-11-29 01:25:20 +0100364 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +0100365 const u8 *ie;
366
Eliad Peller333ba732011-05-29 15:53:20 +0300367 if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
Johannes Berg2a519312009-02-10 21:25:55 +0100368 return false;
369
Johannes Berg9caf0362012-11-29 01:25:20 +0100370 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 Berg2a519312009-02-10 21:25:55 +0100375 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 Berg9caf0362012-11-29 01:25:20 +0100382 ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG, ies->data, ies->len);
Johannes Bergcd3468b2009-07-29 22:07:44 +0200383 if (!ie)
384 return false;
Rui Paulo136cfa22009-11-18 18:40:00 +0000385 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +0100386 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 Paulo136cfa22009-11-18 18:40:00 +0000393 return memcmp(ie + 2, meshcfg,
Johannes Berg915de2f2012-11-28 22:39:37 +0100394 sizeof(struct ieee80211_meshconf_ie) - 2) == 0;
Johannes Berg2a519312009-02-10 21:25:55 +0100395}
396
Johannes Berg915de2f2012-11-28 22:39:37 +0100397static int cmp_bss_core(struct cfg80211_bss *a, struct cfg80211_bss *b)
Johannes Berg2a519312009-02-10 21:25:55 +0100398{
Johannes Berg9caf0362012-11-29 01:25:20 +0100399 const struct cfg80211_bss_ies *a_ies, *b_ies;
Johannes Berg2a519312009-02-10 21:25:55 +0100400 int r;
401
402 if (a->channel != b->channel)
403 return b->channel->center_freq - a->channel->center_freq;
404
Eliad Peller333ba732011-05-29 15:53:20 +0300405 if (is_mesh_bss(a) && is_mesh_bss(b)) {
Johannes Berg9caf0362012-11-29 01:25:20 +0100406 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 Berg2a519312009-02-10 21:25:55 +0100413 r = cmp_ies(WLAN_EID_MESH_ID,
Johannes Berg9caf0362012-11-29 01:25:20 +0100414 a_ies->data, a_ies->len,
415 b_ies->data, b_ies->len);
Johannes Berg2a519312009-02-10 21:25:55 +0100416 if (r)
417 return r;
418 return cmp_ies(WLAN_EID_MESH_CONFIG,
Johannes Berg9caf0362012-11-29 01:25:20 +0100419 a_ies->data, a_ies->len,
420 b_ies->data, b_ies->len);
Johannes Berg2a519312009-02-10 21:25:55 +0100421 }
422
Emmanuel Grumbachef9456a2012-04-30 10:23:36 +0300423 /*
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 Tarnyagindd9dfb92011-11-04 17:12:07 +0100428}
429
430static int cmp_bss(struct cfg80211_bss *a,
431 struct cfg80211_bss *b)
432{
Johannes Berg9caf0362012-11-29 01:25:20 +0100433 const struct cfg80211_bss_ies *a_ies, *b_ies;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100434 int r;
435
436 r = cmp_bss_core(a, b);
Javier Cardona0a35d362011-05-04 10:24:56 -0700437 if (r)
438 return r;
439
Johannes Berg9caf0362012-11-29 01:25:20 +0100440 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 Berg2a519312009-02-10 21:25:55 +0100447 return cmp_ies(WLAN_EID_SSID,
Johannes Berg9caf0362012-11-29 01:25:20 +0100448 a_ies->data, a_ies->len,
449 b_ies->data, b_ies->len);
Johannes Berg2a519312009-02-10 21:25:55 +0100450}
451
Johannes Berg915de2f2012-11-28 22:39:37 +0100452static int cmp_hidden_bss(struct cfg80211_bss *a, struct cfg80211_bss *b)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100453{
Johannes Berg9caf0362012-11-29 01:25:20 +0100454 const struct cfg80211_bss_ies *a_ies, *b_ies;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100455 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 Berg9caf0362012-11-29 01:25:20 +0100464 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 Tarnyagindd9dfb92011-11-04 17:12:07 +0100473
Johannes Bergf94f8b12012-11-28 22:42:34 +0100474 /*
475 * Key comparator must use same algorithm in any rb-tree
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100476 * search function (order is important), otherwise ordering
477 * of items in the tree is broken and search gives incorrect
Johannes Bergf94f8b12012-11-28 22:42:34 +0100478 * 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 Tarnyagindd9dfb92011-11-04 17:12:07 +0100484
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 Bergf94f8b12012-11-28 22:42:34 +0100499 /*
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 Tarnyagindd9dfb92011-11-04 17:12:07 +0100504 for (i = 0; i < ie2[1]; i++)
505 if (ie2[i + 2])
Johannes Bergf94f8b12012-11-28 22:42:34 +0100506 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100507
508 return 0;
509}
510
Johannes Berg2a519312009-02-10 21:25:55 +0100511struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
512 struct ieee80211_channel *channel,
513 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100514 const u8 *ssid, size_t ssid_len,
515 u16 capa_mask, u16 capa_val)
Johannes Berg2a519312009-02-10 21:25:55 +0100516{
517 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
518 struct cfg80211_internal_bss *bss, *res = NULL;
Johannes Bergccb6c132010-07-13 10:55:38 +0200519 unsigned long now = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100520
Beni Lev4ee3e062012-08-27 12:49:39 +0300521 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, capa_mask,
522 capa_val);
523
Johannes Berg2a519312009-02-10 21:25:55 +0100524 spin_lock_bh(&dev->bss_lock);
525
526 list_for_each_entry(bss, &dev->bss_list, list) {
Johannes Berg79420f02009-02-10 21:25:59 +0100527 if ((bss->pub.capability & capa_mask) != capa_val)
528 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100529 if (channel && bss->pub.channel != channel)
530 continue;
Johannes Bergccb6c132010-07-13 10:55:38 +0200531 /* 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 Berg2a519312009-02-10 21:25:55 +0100535 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 Lev4ee3e062012-08-27 12:49:39 +0300545 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +0100546 return &res->pub;
547}
548EXPORT_SYMBOL(cfg80211_get_bss);
549
550struct 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}
575EXPORT_SYMBOL(cfg80211_get_mesh);
576
577
578static 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
607static struct cfg80211_internal_bss *
608rb_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
630static struct cfg80211_internal_bss *
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100631rb_find_hidden_bss(struct cfg80211_registered_device *dev,
Johannes Berg915de2f2012-11-28 22:39:37 +0100632 struct cfg80211_internal_bss *res)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100633{
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
653static void
654copy_hidden_ies(struct cfg80211_internal_bss *res,
Johannes Berg915de2f2012-11-28 22:39:37 +0100655 struct cfg80211_internal_bss *hidden)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100656{
Johannes Berg9caf0362012-11-29 01:25:20 +0100657 const struct cfg80211_bss_ies *ies;
658
659 if (rcu_access_pointer(res->pub.beacon_ies))
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100660 return;
661
Johannes Berg9caf0362012-11-29 01:25:20 +0100662 ies = rcu_access_pointer(hidden->pub.beacon_ies);
663 if (WARN_ON(!ies))
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100664 return;
665
Johannes Berg9caf0362012-11-29 01:25:20 +0100666 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 Tarnyagindd9dfb92011-11-04 17:12:07 +0100670}
671
672static struct cfg80211_internal_bss *
Johannes Berg2a519312009-02-10 21:25:55 +0100673cfg80211_bss_update(struct cfg80211_registered_device *dev,
Johannes Berg9caf0362012-11-29 01:25:20 +0100674 struct cfg80211_internal_bss *tmp)
Johannes Berg2a519312009-02-10 21:25:55 +0100675{
676 struct cfg80211_internal_bss *found = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100677
Johannes Berg9caf0362012-11-29 01:25:20 +0100678 if (WARN_ON(!tmp->pub.channel))
Johannes Berg2a519312009-02-10 21:25:55 +0100679 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100680
Johannes Berg9caf0362012-11-29 01:25:20 +0100681 tmp->ts = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100682
Johannes Berg2a519312009-02-10 21:25:55 +0100683 spin_lock_bh(&dev->bss_lock);
684
Johannes Berg9caf0362012-11-29 01:25:20 +0100685 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 Berg2a519312009-02-10 21:25:55 +0100691
Johannes Bergcd1658f2009-04-16 15:00:58 +0200692 if (found) {
Johannes Berg9caf0362012-11-29 01:25:20 +0100693 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 Bergcd1658f2009-04-16 15:00:58 +0200698
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200699 /* Update IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +0100700 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
701 const struct cfg80211_bss_ies *old;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200702
Johannes Berg9caf0362012-11-29 01:25:20 +0100703 old = rcu_access_pointer(found->pub.proberesp_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +0200704
Johannes Berg9caf0362012-11-29 01:25:20 +0100705 rcu_assign_pointer(found->pub.proberesp_ies,
706 tmp->pub.proberesp_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200707 /* Override possible earlier Beacon frame IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +0100708 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 Berg915de2f2012-11-28 22:39:37 +0100715
Johannes Berg9caf0362012-11-29 01:25:20 +0100716 old = rcu_access_pointer(found->pub.beacon_ies);
717 ies = rcu_access_pointer(found->pub.ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200718
Johannes Berg9caf0362012-11-29 01:25:20 +0100719 rcu_assign_pointer(found->pub.beacon_ies,
720 tmp->pub.beacon_ies);
Sven Neumann01123e22010-12-09 15:05:24 +0100721
722 /* Override IEs if they were from a beacon before */
Johannes Berg9caf0362012-11-29 01:25:20 +0100723 if (old == ies)
724 rcu_assign_pointer(found->pub.ies,
725 tmp->pub.beacon_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +0200726
Johannes Berg9caf0362012-11-29 01:25:20 +0100727 if (old)
728 kfree_rcu((struct cfg80211_bss_ies *)old,
729 rcu_head);
730 }
Johannes Berg2a519312009-02-10 21:25:55 +0100731 } else {
Johannes Berg9caf0362012-11-29 01:25:20 +0100732 struct cfg80211_internal_bss *new;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100733 struct cfg80211_internal_bss *hidden;
Johannes Berg9caf0362012-11-29 01:25:20 +0100734 struct cfg80211_bss_ies *ies;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100735
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 Berg9caf0362012-11-29 01:25:20 +0100745 hidden = rb_find_hidden_bss(dev, tmp);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100746 if (hidden)
Johannes Berg9caf0362012-11-29 01:25:20 +0100747 copy_hidden_ies(tmp, hidden);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100748
Johannes Berg9caf0362012-11-29 01:25:20 +0100749 /*
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 Berg2a519312009-02-10 21:25:55 +0100771 }
772
773 dev->bss_generation++;
774 spin_unlock_bh(&dev->bss_lock);
775
776 kref_get(&found->ref);
777 return found;
778}
779
Johannes Berg0172bb72012-11-23 14:23:30 +0100780static struct ieee80211_channel *
781cfg80211_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 Kivilinna06aa7af2009-03-26 23:40:09 +0200812struct cfg80211_bss*
813cfg80211_inform_bss(struct wiphy *wiphy,
814 struct ieee80211_channel *channel,
Johannes Berg7b8bcff2012-03-13 13:57:04 +0100815 const u8 *bssid, u64 tsf, u16 capability,
816 u16 beacon_interval, const u8 *ie, size_t ielen,
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200817 s32 signal, gfp_t gfp)
818{
Johannes Berg9caf0362012-11-29 01:25:20 +0100819 struct cfg80211_bss_ies *ies;
820 struct cfg80211_internal_bss tmp = {}, *res;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200821
822 if (WARN_ON(!wiphy))
823 return NULL;
824
Sujith22fe88d2010-05-13 10:34:08 +0530825 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200826 (signal < 0 || signal > 100)))
827 return NULL;
828
Johannes Berg0172bb72012-11-23 14:23:30 +0100829 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, channel);
830 if (!channel)
831 return NULL;
832
Johannes Berg9caf0362012-11-29 01:25:20 +0100833 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 Malinen34a6edd2010-01-06 16:19:24 +0200839 /*
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 Berg9caf0362012-11-29 01:25:20 +0100844 * override the iies pointer should we have received an earlier
845 * indication of Probe Response data.
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200846 *
847 * The initial buffer for the IEs is allocated with the BSS entry and
848 * is located after the private area.
849 */
Johannes Berg9caf0362012-11-29 01:25:20 +0100850 ies = kmalloc(sizeof(*ies) + ielen, gfp);
851 if (!ies)
852 return NULL;
853 ies->len = ielen;
854 memcpy(ies->data, ie, ielen);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200855
Johannes Berg9caf0362012-11-29 01:25:20 +0100856 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
857 rcu_assign_pointer(tmp.pub.ies, ies);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200858
Johannes Berg9caf0362012-11-29 01:25:20 +0100859 res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200860 if (!res)
861 return NULL;
862
863 if (res->pub.capability & WLAN_CAPABILITY_ESS)
864 regulatory_hint_found_beacon(wiphy, channel, gfp);
865
Beni Lev4ee3e062012-08-27 12:49:39 +0300866 trace_cfg80211_return_bss(&res->pub);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200867 /* cfg80211_bss_update gives us a referenced result */
868 return &res->pub;
869}
870EXPORT_SYMBOL(cfg80211_inform_bss);
871
Johannes Berg2a519312009-02-10 21:25:55 +0100872struct cfg80211_bss *
873cfg80211_inform_bss_frame(struct wiphy *wiphy,
874 struct ieee80211_channel *channel,
875 struct ieee80211_mgmt *mgmt, size_t len,
Johannes Berg77965c92009-02-18 18:45:06 +0100876 s32 signal, gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +0100877{
Johannes Berg9caf0362012-11-29 01:25:20 +0100878 struct cfg80211_internal_bss tmp = {}, *res;
879 struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +0100880 size_t ielen = len - offsetof(struct ieee80211_mgmt,
881 u.probe_resp.variable);
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100882
Johannes Berg0172bb72012-11-23 14:23:30 +0100883 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
884 offsetof(struct ieee80211_mgmt, u.beacon.variable));
885
Beni Lev4ee3e062012-08-27 12:49:39 +0300886 trace_cfg80211_inform_bss_frame(wiphy, channel, mgmt, len, signal);
887
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100888 if (WARN_ON(!mgmt))
889 return NULL;
890
891 if (WARN_ON(!wiphy))
892 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100893
Sujith22fe88d2010-05-13 10:34:08 +0530894 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Hila Gonen768be592012-08-26 11:00:28 +0300895 (signal < 0 || signal > 100)))
Johannes Berg2a519312009-02-10 21:25:55 +0100896 return NULL;
897
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100898 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
Johannes Berg2a519312009-02-10 21:25:55 +0100899 return NULL;
900
Johannes Berg0172bb72012-11-23 14:23:30 +0100901 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
902 ielen, channel);
903 if (!channel)
904 return NULL;
905
Johannes Berg9caf0362012-11-29 01:25:20 +0100906 ies = kmalloc(sizeof(*ies) + ielen, gfp);
907 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +0100908 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +0100909 ies->len = ielen;
910 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
Johannes Berg2a519312009-02-10 21:25:55 +0100911
Johannes Berg9caf0362012-11-29 01:25:20 +0100912 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 Berg2a519312009-02-10 21:25:55 +0100924
Johannes Berg9caf0362012-11-29 01:25:20 +0100925 res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
Johannes Berg2a519312009-02-10 21:25:55 +0100926 if (!res)
927 return NULL;
928
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500929 if (res->pub.capability & WLAN_CAPABILITY_ESS)
930 regulatory_hint_found_beacon(wiphy, channel, gfp);
931
Beni Lev4ee3e062012-08-27 12:49:39 +0300932 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +0100933 /* cfg80211_bss_update gives us a referenced result */
934 return &res->pub;
935}
936EXPORT_SYMBOL(cfg80211_inform_bss_frame);
937
Johannes Berg4c0c0b72012-01-20 13:55:26 +0100938void 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}
948EXPORT_SYMBOL(cfg80211_ref_bss);
949
Johannes Berg2a519312009-02-10 21:25:55 +0100950void 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}
960EXPORT_SYMBOL(cfg80211_put_bss);
961
Johannes Bergd491af12009-02-10 21:25:58 +0100962void 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 Berg32073902010-10-06 21:18:04 +0200973 if (!list_empty(&bss->list)) {
Juuso Oikarinen2b78ac92011-03-28 14:32:32 +0300974 __cfg80211_unlink_bss(dev, bss);
Johannes Berg32073902010-10-06 21:18:04 +0200975 dev->bss_generation++;
Johannes Berg32073902010-10-06 21:18:04 +0200976 }
Johannes Bergd491af12009-02-10 21:25:58 +0100977 spin_unlock_bh(&dev->bss_lock);
Johannes Bergd491af12009-02-10 21:25:58 +0100978}
979EXPORT_SYMBOL(cfg80211_unlink_bss);
980
Johannes Berg3d23e342009-09-29 23:27:28 +0200981#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +0100982int 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 Berg65486c82009-12-23 15:33:35 +0100989 struct cfg80211_scan_request *creq = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100990 int i, err, n_channels = 0;
991 enum ieee80211_band band;
992
993 if (!netif_running(dev))
994 return -ENETDOWN;
995
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200996 if (wrqu->data.length == sizeof(struct iw_scan_req))
997 wreq = (struct iw_scan_req *)extra;
998
Johannes Berg463d0182009-07-14 00:33:35 +0200999 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01001000
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 Schurigb2e3abd2009-09-09 13:09:54 +02001011 /* 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 Berg2a519312009-02-10 21:25:55 +01001019
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 Bergfd014282012-06-18 19:17:03 +02001029 creq->wdev = dev->ieee80211_ptr;
Johannes Berg5ba63532009-08-07 17:54:07 +02001030 /* SSIDs come after channels */
1031 creq->ssids = (void *)&creq->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01001032 creq->n_channels = n_channels;
1033 creq->n_ssids = 1;
Sam Leffler15d60302012-10-11 21:03:34 -07001034 creq->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01001035
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001036 /* translate "Scan on frequencies" request */
Johannes Berg2a519312009-02-10 21:25:55 +01001037 i = 0;
1038 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1039 int j;
Johannes Berg584991d2009-11-02 13:32:03 +01001040
Johannes Berg2a519312009-02-10 21:25:55 +01001041 if (!wiphy->bands[band])
1042 continue;
Johannes Berg584991d2009-11-02 13:32:03 +01001043
Johannes Berg2a519312009-02-10 21:25:55 +01001044 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01001045 /* ignore disabled channels */
1046 if (wiphy->bands[band]->channels[j].flags &
1047 IEEE80211_CHAN_DISABLED)
1048 continue;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001049
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 Schuriga4e7b732009-09-11 10:13:53 +02001058 int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001059 if (wext_freq == wiphy_freq)
1060 goto wext_freq_found;
1061 }
1062 goto wext_freq_not_found;
1063 }
1064
1065 wext_freq_found:
Johannes Berg2a519312009-02-10 21:25:55 +01001066 creq->channels[i] = &wiphy->bands[band]->channels[j];
1067 i++;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001068 wext_freq_not_found: ;
Johannes Berg2a519312009-02-10 21:25:55 +01001069 }
1070 }
Holger Schurig8862dc52009-09-11 10:13:55 +02001071 /* No channels found? */
1072 if (!i) {
1073 err = -EINVAL;
1074 goto out;
1075 }
Johannes Berg2a519312009-02-10 21:25:55 +01001076
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001077 /* Set real number of channels specified in creq->channels[] */
1078 creq->n_channels = i;
Johannes Berg2a519312009-02-10 21:25:55 +01001079
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001080 /* translate "Scan for SSID" request */
1081 if (wreq) {
Johannes Berg2a519312009-02-10 21:25:55 +01001082 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Johannes Berg65486c82009-12-23 15:33:35 +01001083 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1084 err = -EINVAL;
1085 goto out;
1086 }
Johannes Berg2a519312009-02-10 21:25:55 +01001087 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 Berg34850ab2011-07-18 18:08:35 +02001094 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02001095 if (wiphy->bands[i])
1096 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02001097
Johannes Berg2a519312009-02-10 21:25:55 +01001098 rdev->scan_req = creq;
Hila Gonene35e4d22012-06-27 17:19:42 +03001099 err = rdev_scan(rdev, creq);
Johannes Berg2a519312009-02-10 21:25:55 +01001100 if (err) {
1101 rdev->scan_req = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01001102 /* creq will be freed below */
Johannes Berg463d0182009-07-14 00:33:35 +02001103 } else {
Johannes Bergfd014282012-06-18 19:17:03 +02001104 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
Johannes Berg65486c82009-12-23 15:33:35 +01001105 /* creq now owned by driver */
1106 creq = NULL;
Johannes Berg463d0182009-07-14 00:33:35 +02001107 dev_hold(dev);
1108 }
Johannes Berg2a519312009-02-10 21:25:55 +01001109 out:
Johannes Berg65486c82009-12-23 15:33:35 +01001110 kfree(creq);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001111 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01001112 return err;
1113}
Johannes Bergba44cb72009-04-20 18:49:39 +02001114EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001115
1116static void ieee80211_scan_add_ies(struct iw_request_info *info,
Johannes Berg9caf0362012-11-29 01:25:20 +01001117 const struct cfg80211_bss_ies *ies,
Johannes Berg2a519312009-02-10 21:25:55 +01001118 char **current_ev, char *end_buf)
1119{
Johannes Berg9caf0362012-11-29 01:25:20 +01001120 const u8 *pos, *end, *next;
Johannes Berg2a519312009-02-10 21:25:55 +01001121 struct iw_event iwe;
1122
Johannes Berg9caf0362012-11-29 01:25:20 +01001123 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +01001124 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 Berg9caf0362012-11-29 01:25:20 +01001130 pos = ies->data;
1131 end = pos + ies->len;
Johannes Berg2a519312009-02-10 21:25:55 +01001132
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 Berg9caf0362012-11-29 01:25:20 +01001142 end_buf, &iwe,
1143 (void *)pos);
Johannes Berg2a519312009-02-10 21:25:55 +01001144
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 Berg9caf0362012-11-29 01:25:20 +01001153 end_buf, &iwe,
1154 (void *)pos);
Johannes Berg2a519312009-02-10 21:25:55 +01001155 }
1156}
1157
Dan Williamscb3a8ee2009-02-11 17:14:43 -05001158static 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 Berg2a519312009-02-10 21:25:55 +01001167
1168static char *
Johannes Berg77965c92009-02-18 18:45:06 +01001169ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1170 struct cfg80211_internal_bss *bss, char *current_ev,
1171 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01001172{
Johannes Berg9caf0362012-11-29 01:25:20 +01001173 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01001174 struct iw_event iwe;
Johannes Berg9caf0362012-11-29 01:25:20 +01001175 const u8 *ie;
Johannes Berg2a519312009-02-10 21:25:55 +01001176 u8 *buf, *cfg, *p;
Johannes Berg9caf0362012-11-29 01:25:20 +01001177 int rem, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +01001178 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 Berg77965c92009-02-18 18:45:06 +01001201 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +01001202 memset(&iwe, 0, sizeof(iwe));
1203 iwe.cmd = IWEVQUAL;
1204 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
1205 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +01001206 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c92009-02-18 18:45:06 +01001207 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01001208 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +01001209 sig = bss->pub.signal / 100;
1210 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +01001211 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +01001212 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 Berg2a519312009-02-10 21:25:55 +01001218 break;
1219 case CFG80211_SIGNAL_TYPE_UNSPEC:
1220 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +01001221 /* will give range 0 .. 100 */
1222 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +01001223 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 Berg9caf0362012-11-29 01:25:20 +01001242 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 Berg2a519312009-02-10 21:25:55 +01001253 /* 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 Berg9caf0362012-11-29 01:25:20 +01001264 &iwe, (u8 *)ie + 2);
Johannes Berg2a519312009-02-10 21:25:55 +01001265 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 Berg9caf0362012-11-29 01:25:20 +01001272 &iwe, (u8 *)ie + 2);
Johannes Berg2a519312009-02-10 21:25:55 +01001273 break;
1274 case WLAN_EID_MESH_CONFIG:
1275 ismesh = true;
Rui Paulo136cfa22009-11-18 18:40:00 +00001276 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +01001277 break;
1278 buf = kmalloc(50, GFP_ATOMIC);
1279 if (!buf)
1280 break;
Johannes Berg9caf0362012-11-29 01:25:20 +01001281 cfg = (u8 *)ie + 2;
Johannes Berg2a519312009-02-10 21:25:55 +01001282 memset(&iwe, 0, sizeof(iwe));
1283 iwe.cmd = IWEVCUSTOM;
Rui Paulo76aa5e72009-11-18 18:22:59 +00001284 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1285 "0x%02X", cfg[0]);
Johannes Berg2a519312009-02-10 21:25:55 +01001286 iwe.u.data.length = strlen(buf);
1287 current_ev = iwe_stream_add_point(info, current_ev,
1288 end_buf,
1289 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001290 sprintf(buf, "Path Selection Metric ID: 0x%02X",
1291 cfg[1]);
Johannes Berg2a519312009-02-10 21:25:55 +01001292 iwe.u.data.length = strlen(buf);
1293 current_ev = iwe_stream_add_point(info, current_ev,
1294 end_buf,
1295 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001296 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1297 cfg[2]);
Johannes Berg2a519312009-02-10 21:25:55 +01001298 iwe.u.data.length = strlen(buf);
1299 current_ev = iwe_stream_add_point(info, current_ev,
1300 end_buf,
1301 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001302 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
Johannes Berg2a519312009-02-10 21:25:55 +01001303 iwe.u.data.length = strlen(buf);
1304 current_ev = iwe_stream_add_point(info, current_ev,
1305 end_buf,
1306 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001307 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 Berg2a519312009-02-10 21:25:55 +01001318 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 Perchesf64f9e72009-11-29 16:55:45 -08001347 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1348 ismesh) {
Johannes Berg2a519312009-02-10 21:25:55 +01001349 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 Carpenterc49dc902013-01-24 09:40:00 +03001361 buf = kmalloc(31, GFP_ATOMIC);
Johannes Berg2a519312009-02-10 21:25:55 +01001362 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 Williamscb3a8ee2009-02-11 17:14:43 -05001371 sprintf(buf, " Last beacon: %ums ago",
1372 elapsed_jiffies_msecs(bss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01001373 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 Berg9caf0362012-11-29 01:25:20 +01001379 ieee80211_scan_add_ies(info, ies, &current_ev, end_buf);
1380 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01001381
1382 return current_ev;
1383}
1384
1385
1386static 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 Berg77965c92009-02-18 18:45:06 +01001402 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
1403 current_ev, end_buf);
Johannes Berg2a519312009-02-10 21:25:55 +01001404 }
1405 spin_unlock_bh(&dev->bss_lock);
1406 return current_ev - buf;
1407}
1408
1409
1410int 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 Berg463d0182009-07-14 00:33:35 +02001420 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01001421
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 Berg4d0c8ae2009-07-07 03:56:09 +02001438 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01001439 return res;
1440}
Johannes Bergba44cb72009-04-20 18:49:39 +02001441EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001442#endif