blob: fbf6f33ae4d0558c68d71efc418db67cedfc8796 [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>
15#include <net/iw_handler.h>
16#include "core.h"
17#include "nl80211.h"
Johannes Berga9a11622009-07-27 12:01:53 +020018#include "wext-compat.h"
Johannes Berg2a519312009-02-10 21:25:55 +010019
Helmut Schaa09f97e02009-07-23 12:14:29 +020020#define IEEE80211_SCAN_RESULT_EXPIRE (15 * HZ)
Johannes Berg2a519312009-02-10 21:25:55 +010021
Johannes Berg01a0ac42009-08-20 21:36:16 +020022void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak)
Johannes Berg2a519312009-02-10 21:25:55 +010023{
Johannes Berg667503d2009-07-07 03:56:11 +020024 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +010025 struct net_device *dev;
Johannes Berg3d23e342009-09-29 23:27:28 +020026#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +010027 union iwreq_data wrqu;
28#endif
29
Johannes Berg01a0ac42009-08-20 21:36:16 +020030 ASSERT_RDEV_LOCK(rdev);
31
Johannes Berg667503d2009-07-07 03:56:11 +020032 request = rdev->scan_req;
33
Johannes Berg01a0ac42009-08-20 21:36:16 +020034 if (!request)
35 return;
36
Johannes Berg463d0182009-07-14 00:33:35 +020037 dev = request->dev;
Johannes Berg2a519312009-02-10 21:25:55 +010038
Johannes Berg6829c872009-07-02 09:13:27 +020039 /*
40 * This must be before sending the other events!
41 * Otherwise, wpa_supplicant gets completely confused with
42 * wext events.
43 */
44 cfg80211_sme_scan_done(dev);
45
Johannes Berg667503d2009-07-07 03:56:11 +020046 if (request->aborted)
Johannes Berg36e6fea2009-08-12 22:21:21 +020047 nl80211_send_scan_aborted(rdev, dev);
Johannes Berg2a519312009-02-10 21:25:55 +010048 else
Johannes Berg36e6fea2009-08-12 22:21:21 +020049 nl80211_send_scan_done(rdev, dev);
Johannes Berg2a519312009-02-10 21:25:55 +010050
Johannes Berg3d23e342009-09-29 23:27:28 +020051#ifdef CONFIG_CFG80211_WEXT
Johannes Berg667503d2009-07-07 03:56:11 +020052 if (!request->aborted) {
Johannes Berg2a519312009-02-10 21:25:55 +010053 memset(&wrqu, 0, sizeof(wrqu));
54
55 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
56 }
57#endif
58
59 dev_put(dev);
60
Johannes Berg36e6fea2009-08-12 22:21:21 +020061 rdev->scan_req = NULL;
Johannes Berg01a0ac42009-08-20 21:36:16 +020062
63 /*
64 * OK. If this is invoked with "leak" then we can't
65 * free this ... but we've cleaned it up anyway. The
66 * driver failed to call the scan_done callback, so
67 * all bets are off, it might still be trying to use
68 * the scan request or not ... if it accesses the dev
69 * in there (it shouldn't anyway) then it may crash.
70 */
71 if (!leak)
72 kfree(request);
Johannes Berg2a519312009-02-10 21:25:55 +010073}
Johannes Berg667503d2009-07-07 03:56:11 +020074
Johannes Berg36e6fea2009-08-12 22:21:21 +020075void __cfg80211_scan_done(struct work_struct *wk)
76{
77 struct cfg80211_registered_device *rdev;
78
79 rdev = container_of(wk, struct cfg80211_registered_device,
80 scan_done_wk);
81
82 cfg80211_lock_rdev(rdev);
Johannes Berg01a0ac42009-08-20 21:36:16 +020083 ___cfg80211_scan_done(rdev, false);
Johannes Berg36e6fea2009-08-12 22:21:21 +020084 cfg80211_unlock_rdev(rdev);
85}
86
Johannes Berg667503d2009-07-07 03:56:11 +020087void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
88{
Johannes Berg667503d2009-07-07 03:56:11 +020089 WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
90
91 request->aborted = aborted;
Alban Browaeyse60d7442009-11-25 15:13:00 +010092 queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk);
Johannes Berg667503d2009-07-07 03:56:11 +020093}
Johannes Berg2a519312009-02-10 21:25:55 +010094EXPORT_SYMBOL(cfg80211_scan_done);
95
96static void bss_release(struct kref *ref)
97{
98 struct cfg80211_internal_bss *bss;
99
100 bss = container_of(ref, struct cfg80211_internal_bss, ref);
Johannes Berg78c1c7e2009-02-10 21:25:57 +0100101 if (bss->pub.free_priv)
102 bss->pub.free_priv(&bss->pub);
Johannes Bergcd1658f2009-04-16 15:00:58 +0200103
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200104 if (bss->beacon_ies_allocated)
105 kfree(bss->pub.beacon_ies);
106 if (bss->proberesp_ies_allocated)
107 kfree(bss->pub.proberesp_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +0200108
Johannes Berg19957bb2009-07-02 17:20:43 +0200109 BUG_ON(atomic_read(&bss->hold));
110
Johannes Berg2a519312009-02-10 21:25:55 +0100111 kfree(bss);
112}
113
114/* must hold dev->bss_lock! */
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500115void cfg80211_bss_age(struct cfg80211_registered_device *dev,
116 unsigned long age_secs)
117{
118 struct cfg80211_internal_bss *bss;
119 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
120
121 list_for_each_entry(bss, &dev->bss_list, list) {
122 bss->ts -= age_jiffies;
123 }
124}
125
126/* must hold dev->bss_lock! */
Juuso Oikarinen2b78ac92011-03-28 14:32:32 +0300127static void __cfg80211_unlink_bss(struct cfg80211_registered_device *dev,
128 struct cfg80211_internal_bss *bss)
129{
130 list_del_init(&bss->list);
131 rb_erase(&bss->rbn, &dev->bss_tree);
132 kref_put(&bss->ref, bss_release);
133}
134
135/* must hold dev->bss_lock! */
Johannes Berg2a519312009-02-10 21:25:55 +0100136void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
137{
138 struct cfg80211_internal_bss *bss, *tmp;
139 bool expired = false;
140
141 list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200142 if (atomic_read(&bss->hold))
143 continue;
144 if (!time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE))
Johannes Berg2a519312009-02-10 21:25:55 +0100145 continue;
Juuso Oikarinen2b78ac92011-03-28 14:32:32 +0300146 __cfg80211_unlink_bss(dev, bss);
Johannes Berg2a519312009-02-10 21:25:55 +0100147 expired = true;
148 }
149
150 if (expired)
151 dev->bss_generation++;
152}
153
Johannes Bergc21dbf92010-01-26 14:15:46 +0100154const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
Johannes Berg2a519312009-02-10 21:25:55 +0100155{
Johannes Bergc21dbf92010-01-26 14:15:46 +0100156 while (len > 2 && ies[0] != eid) {
Johannes Berg2a519312009-02-10 21:25:55 +0100157 len -= ies[1] + 2;
158 ies += ies[1] + 2;
159 }
160 if (len < 2)
161 return NULL;
162 if (len < 2 + ies[1])
163 return NULL;
164 return ies;
165}
Johannes Bergc21dbf92010-01-26 14:15:46 +0100166EXPORT_SYMBOL(cfg80211_find_ie);
Johannes Berg2a519312009-02-10 21:25:55 +0100167
168static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
169{
Johannes Bergc21dbf92010-01-26 14:15:46 +0100170 const u8 *ie1 = cfg80211_find_ie(num, ies1, len1);
171 const u8 *ie2 = cfg80211_find_ie(num, ies2, len2);
Johannes Berg2a519312009-02-10 21:25:55 +0100172 int r;
173
174 if (!ie1 && !ie2)
175 return 0;
Johannes Bergcd3468b2009-07-29 22:07:44 +0200176 if (!ie1 || !ie2)
Johannes Berg2a519312009-02-10 21:25:55 +0100177 return -1;
178
179 r = memcmp(ie1 + 2, ie2 + 2, min(ie1[1], ie2[1]));
180 if (r == 0 && ie1[1] != ie2[1])
181 return ie2[1] - ie1[1];
182 return r;
183}
184
185static bool is_bss(struct cfg80211_bss *a,
186 const u8 *bssid,
187 const u8 *ssid, size_t ssid_len)
188{
189 const u8 *ssidie;
190
Johannes Berg79420f02009-02-10 21:25:59 +0100191 if (bssid && compare_ether_addr(a->bssid, bssid))
Johannes Berg2a519312009-02-10 21:25:55 +0100192 return false;
193
Johannes Berg79420f02009-02-10 21:25:59 +0100194 if (!ssid)
195 return true;
196
Johannes Bergc21dbf92010-01-26 14:15:46 +0100197 ssidie = cfg80211_find_ie(WLAN_EID_SSID,
198 a->information_elements,
199 a->len_information_elements);
Johannes Berg2a519312009-02-10 21:25:55 +0100200 if (!ssidie)
201 return false;
202 if (ssidie[1] != ssid_len)
203 return false;
204 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
205}
206
207static bool is_mesh(struct cfg80211_bss *a,
208 const u8 *meshid, size_t meshidlen,
209 const u8 *meshcfg)
210{
211 const u8 *ie;
212
213 if (!is_zero_ether_addr(a->bssid))
214 return false;
215
Johannes Bergc21dbf92010-01-26 14:15:46 +0100216 ie = cfg80211_find_ie(WLAN_EID_MESH_ID,
217 a->information_elements,
218 a->len_information_elements);
Johannes Berg2a519312009-02-10 21:25:55 +0100219 if (!ie)
220 return false;
221 if (ie[1] != meshidlen)
222 return false;
223 if (memcmp(ie + 2, meshid, meshidlen))
224 return false;
225
Johannes Bergc21dbf92010-01-26 14:15:46 +0100226 ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
227 a->information_elements,
228 a->len_information_elements);
Johannes Bergcd3468b2009-07-29 22:07:44 +0200229 if (!ie)
230 return false;
Rui Paulo136cfa22009-11-18 18:40:00 +0000231 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +0100232 return false;
233
234 /*
235 * Ignore mesh capability (last two bytes of the IE) when
236 * comparing since that may differ between stations taking
237 * part in the same mesh.
238 */
Rui Paulo136cfa22009-11-18 18:40:00 +0000239 return memcmp(ie + 2, meshcfg,
240 sizeof(struct ieee80211_meshconf_ie) - 2) == 0;
Johannes Berg2a519312009-02-10 21:25:55 +0100241}
242
243static int cmp_bss(struct cfg80211_bss *a,
244 struct cfg80211_bss *b)
245{
246 int r;
247
248 if (a->channel != b->channel)
249 return b->channel->center_freq - a->channel->center_freq;
250
251 r = memcmp(a->bssid, b->bssid, ETH_ALEN);
252 if (r)
253 return r;
254
255 if (is_zero_ether_addr(a->bssid)) {
256 r = cmp_ies(WLAN_EID_MESH_ID,
257 a->information_elements,
258 a->len_information_elements,
259 b->information_elements,
260 b->len_information_elements);
261 if (r)
262 return r;
263 return cmp_ies(WLAN_EID_MESH_CONFIG,
264 a->information_elements,
265 a->len_information_elements,
266 b->information_elements,
267 b->len_information_elements);
268 }
269
270 return cmp_ies(WLAN_EID_SSID,
271 a->information_elements,
272 a->len_information_elements,
273 b->information_elements,
274 b->len_information_elements);
275}
276
277struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
278 struct ieee80211_channel *channel,
279 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100280 const u8 *ssid, size_t ssid_len,
281 u16 capa_mask, u16 capa_val)
Johannes Berg2a519312009-02-10 21:25:55 +0100282{
283 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
284 struct cfg80211_internal_bss *bss, *res = NULL;
Johannes Bergccb6c132010-07-13 10:55:38 +0200285 unsigned long now = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100286
287 spin_lock_bh(&dev->bss_lock);
288
289 list_for_each_entry(bss, &dev->bss_list, list) {
Johannes Berg79420f02009-02-10 21:25:59 +0100290 if ((bss->pub.capability & capa_mask) != capa_val)
291 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100292 if (channel && bss->pub.channel != channel)
293 continue;
Johannes Bergccb6c132010-07-13 10:55:38 +0200294 /* Don't get expired BSS structs */
295 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
296 !atomic_read(&bss->hold))
297 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100298 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
299 res = bss;
300 kref_get(&res->ref);
301 break;
302 }
303 }
304
305 spin_unlock_bh(&dev->bss_lock);
306 if (!res)
307 return NULL;
308 return &res->pub;
309}
310EXPORT_SYMBOL(cfg80211_get_bss);
311
312struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
313 struct ieee80211_channel *channel,
314 const u8 *meshid, size_t meshidlen,
315 const u8 *meshcfg)
316{
317 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
318 struct cfg80211_internal_bss *bss, *res = NULL;
319
320 spin_lock_bh(&dev->bss_lock);
321
322 list_for_each_entry(bss, &dev->bss_list, list) {
323 if (channel && bss->pub.channel != channel)
324 continue;
325 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
326 res = bss;
327 kref_get(&res->ref);
328 break;
329 }
330 }
331
332 spin_unlock_bh(&dev->bss_lock);
333 if (!res)
334 return NULL;
335 return &res->pub;
336}
337EXPORT_SYMBOL(cfg80211_get_mesh);
338
339
340static void rb_insert_bss(struct cfg80211_registered_device *dev,
341 struct cfg80211_internal_bss *bss)
342{
343 struct rb_node **p = &dev->bss_tree.rb_node;
344 struct rb_node *parent = NULL;
345 struct cfg80211_internal_bss *tbss;
346 int cmp;
347
348 while (*p) {
349 parent = *p;
350 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
351
352 cmp = cmp_bss(&bss->pub, &tbss->pub);
353
354 if (WARN_ON(!cmp)) {
355 /* will sort of leak this BSS */
356 return;
357 }
358
359 if (cmp < 0)
360 p = &(*p)->rb_left;
361 else
362 p = &(*p)->rb_right;
363 }
364
365 rb_link_node(&bss->rbn, parent, p);
366 rb_insert_color(&bss->rbn, &dev->bss_tree);
367}
368
369static struct cfg80211_internal_bss *
370rb_find_bss(struct cfg80211_registered_device *dev,
371 struct cfg80211_internal_bss *res)
372{
373 struct rb_node *n = dev->bss_tree.rb_node;
374 struct cfg80211_internal_bss *bss;
375 int r;
376
377 while (n) {
378 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
379 r = cmp_bss(&res->pub, &bss->pub);
380
381 if (r == 0)
382 return bss;
383 else if (r < 0)
384 n = n->rb_left;
385 else
386 n = n->rb_right;
387 }
388
389 return NULL;
390}
391
392static struct cfg80211_internal_bss *
393cfg80211_bss_update(struct cfg80211_registered_device *dev,
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200394 struct cfg80211_internal_bss *res)
Johannes Berg2a519312009-02-10 21:25:55 +0100395{
396 struct cfg80211_internal_bss *found = NULL;
397 const u8 *meshid, *meshcfg;
398
399 /*
400 * The reference to "res" is donated to this function.
401 */
402
403 if (WARN_ON(!res->pub.channel)) {
404 kref_put(&res->ref, bss_release);
405 return NULL;
406 }
407
408 res->ts = jiffies;
409
410 if (is_zero_ether_addr(res->pub.bssid)) {
411 /* must be mesh, verify */
Johannes Bergc21dbf92010-01-26 14:15:46 +0100412 meshid = cfg80211_find_ie(WLAN_EID_MESH_ID,
413 res->pub.information_elements,
414 res->pub.len_information_elements);
415 meshcfg = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
416 res->pub.information_elements,
417 res->pub.len_information_elements);
Johannes Berg2a519312009-02-10 21:25:55 +0100418 if (!meshid || !meshcfg ||
Rui Paulo136cfa22009-11-18 18:40:00 +0000419 meshcfg[1] != sizeof(struct ieee80211_meshconf_ie)) {
Johannes Berg2a519312009-02-10 21:25:55 +0100420 /* bogus mesh */
421 kref_put(&res->ref, bss_release);
422 return NULL;
423 }
424 }
425
426 spin_lock_bh(&dev->bss_lock);
427
428 found = rb_find_bss(dev, res);
429
Johannes Bergcd1658f2009-04-16 15:00:58 +0200430 if (found) {
Johannes Berg2a519312009-02-10 21:25:55 +0100431 found->pub.beacon_interval = res->pub.beacon_interval;
432 found->pub.tsf = res->pub.tsf;
433 found->pub.signal = res->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +0100434 found->pub.capability = res->pub.capability;
435 found->ts = res->ts;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200436
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200437 /* Update IEs */
438 if (res->pub.proberesp_ies) {
Johannes Bergcd1658f2009-04-16 15:00:58 +0200439 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200440 size_t ielen = res->pub.len_proberesp_ies;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200441
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200442 if (found->pub.proberesp_ies &&
443 !found->proberesp_ies_allocated &&
444 ksize(found) >= used + ielen) {
445 memcpy(found->pub.proberesp_ies,
446 res->pub.proberesp_ies, ielen);
447 found->pub.len_proberesp_ies = ielen;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200448 } else {
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200449 u8 *ies = found->pub.proberesp_ies;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200450
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200451 if (found->proberesp_ies_allocated)
Michael Buesch273de922009-04-25 22:28:55 +0200452 ies = krealloc(ies, ielen, GFP_ATOMIC);
453 else
Johannes Bergcd1658f2009-04-16 15:00:58 +0200454 ies = kmalloc(ielen, GFP_ATOMIC);
455
456 if (ies) {
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200457 memcpy(ies, res->pub.proberesp_ies,
458 ielen);
459 found->proberesp_ies_allocated = true;
460 found->pub.proberesp_ies = ies;
461 found->pub.len_proberesp_ies = ielen;
462 }
463 }
464
465 /* Override possible earlier Beacon frame IEs */
466 found->pub.information_elements =
467 found->pub.proberesp_ies;
468 found->pub.len_information_elements =
469 found->pub.len_proberesp_ies;
470 }
471 if (res->pub.beacon_ies) {
472 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
473 size_t ielen = res->pub.len_beacon_ies;
Sven Neumann01123e22010-12-09 15:05:24 +0100474 bool information_elements_is_beacon_ies =
475 (found->pub.information_elements ==
476 found->pub.beacon_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200477
478 if (found->pub.beacon_ies &&
479 !found->beacon_ies_allocated &&
480 ksize(found) >= used + ielen) {
481 memcpy(found->pub.beacon_ies,
482 res->pub.beacon_ies, ielen);
483 found->pub.len_beacon_ies = ielen;
484 } else {
485 u8 *ies = found->pub.beacon_ies;
486
487 if (found->beacon_ies_allocated)
488 ies = krealloc(ies, ielen, GFP_ATOMIC);
489 else
490 ies = kmalloc(ielen, GFP_ATOMIC);
491
492 if (ies) {
493 memcpy(ies, res->pub.beacon_ies,
494 ielen);
495 found->beacon_ies_allocated = true;
496 found->pub.beacon_ies = ies;
497 found->pub.len_beacon_ies = ielen;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200498 }
499 }
Sven Neumann01123e22010-12-09 15:05:24 +0100500
501 /* Override IEs if they were from a beacon before */
502 if (information_elements_is_beacon_ies) {
503 found->pub.information_elements =
504 found->pub.beacon_ies;
505 found->pub.len_information_elements =
506 found->pub.len_beacon_ies;
507 }
Johannes Bergcd1658f2009-04-16 15:00:58 +0200508 }
509
Johannes Berg2a519312009-02-10 21:25:55 +0100510 kref_put(&res->ref, bss_release);
511 } else {
512 /* this "consumes" the reference */
513 list_add_tail(&res->list, &dev->bss_list);
514 rb_insert_bss(dev, res);
515 found = res;
516 }
517
518 dev->bss_generation++;
519 spin_unlock_bh(&dev->bss_lock);
520
521 kref_get(&found->ref);
522 return found;
523}
524
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200525struct cfg80211_bss*
526cfg80211_inform_bss(struct wiphy *wiphy,
527 struct ieee80211_channel *channel,
528 const u8 *bssid,
529 u64 timestamp, u16 capability, u16 beacon_interval,
530 const u8 *ie, size_t ielen,
531 s32 signal, gfp_t gfp)
532{
533 struct cfg80211_internal_bss *res;
534 size_t privsz;
535
536 if (WARN_ON(!wiphy))
537 return NULL;
538
539 privsz = wiphy->bss_priv_size;
540
Sujith22fe88d2010-05-13 10:34:08 +0530541 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200542 (signal < 0 || signal > 100)))
543 return NULL;
544
545 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
546 if (!res)
547 return NULL;
548
549 memcpy(res->pub.bssid, bssid, ETH_ALEN);
550 res->pub.channel = channel;
551 res->pub.signal = signal;
552 res->pub.tsf = timestamp;
553 res->pub.beacon_interval = beacon_interval;
554 res->pub.capability = capability;
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200555 /*
556 * Since we do not know here whether the IEs are from a Beacon or Probe
557 * Response frame, we need to pick one of the options and only use it
558 * with the driver that does not provide the full Beacon/Probe Response
559 * frame. Use Beacon frame pointer to avoid indicating that this should
560 * override the information_elements pointer should we have received an
561 * earlier indication of Probe Response data.
562 *
563 * The initial buffer for the IEs is allocated with the BSS entry and
564 * is located after the private area.
565 */
566 res->pub.beacon_ies = (u8 *)res + sizeof(*res) + privsz;
567 memcpy(res->pub.beacon_ies, ie, ielen);
568 res->pub.len_beacon_ies = ielen;
569 res->pub.information_elements = res->pub.beacon_ies;
570 res->pub.len_information_elements = res->pub.len_beacon_ies;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200571
572 kref_init(&res->ref);
573
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200574 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200575 if (!res)
576 return NULL;
577
578 if (res->pub.capability & WLAN_CAPABILITY_ESS)
579 regulatory_hint_found_beacon(wiphy, channel, gfp);
580
581 /* cfg80211_bss_update gives us a referenced result */
582 return &res->pub;
583}
584EXPORT_SYMBOL(cfg80211_inform_bss);
585
Johannes Berg2a519312009-02-10 21:25:55 +0100586struct cfg80211_bss *
587cfg80211_inform_bss_frame(struct wiphy *wiphy,
588 struct ieee80211_channel *channel,
589 struct ieee80211_mgmt *mgmt, size_t len,
Johannes Berg77965c92009-02-18 18:45:06 +0100590 s32 signal, gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +0100591{
592 struct cfg80211_internal_bss *res;
593 size_t ielen = len - offsetof(struct ieee80211_mgmt,
594 u.probe_resp.variable);
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100595 size_t privsz;
596
597 if (WARN_ON(!mgmt))
598 return NULL;
599
600 if (WARN_ON(!wiphy))
601 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100602
Sujith22fe88d2010-05-13 10:34:08 +0530603 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Johannes Berg2a519312009-02-10 21:25:55 +0100604 (signal < 0 || signal > 100)))
605 return NULL;
606
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100607 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
Johannes Berg2a519312009-02-10 21:25:55 +0100608 return NULL;
609
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100610 privsz = wiphy->bss_priv_size;
611
Johannes Berg2a519312009-02-10 21:25:55 +0100612 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
613 if (!res)
614 return NULL;
615
616 memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
617 res->pub.channel = channel;
Johannes Berg2a519312009-02-10 21:25:55 +0100618 res->pub.signal = signal;
619 res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
620 res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
621 res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200622 /*
623 * The initial buffer for the IEs is allocated with the BSS entry and
624 * is located after the private area.
625 */
626 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
627 res->pub.proberesp_ies = (u8 *) res + sizeof(*res) + privsz;
628 memcpy(res->pub.proberesp_ies, mgmt->u.probe_resp.variable,
629 ielen);
630 res->pub.len_proberesp_ies = ielen;
631 res->pub.information_elements = res->pub.proberesp_ies;
632 res->pub.len_information_elements = res->pub.len_proberesp_ies;
633 } else {
634 res->pub.beacon_ies = (u8 *) res + sizeof(*res) + privsz;
635 memcpy(res->pub.beacon_ies, mgmt->u.beacon.variable, ielen);
636 res->pub.len_beacon_ies = ielen;
637 res->pub.information_elements = res->pub.beacon_ies;
638 res->pub.len_information_elements = res->pub.len_beacon_ies;
639 }
Johannes Berg2a519312009-02-10 21:25:55 +0100640
641 kref_init(&res->ref);
642
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200643 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
Johannes Berg2a519312009-02-10 21:25:55 +0100644 if (!res)
645 return NULL;
646
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500647 if (res->pub.capability & WLAN_CAPABILITY_ESS)
648 regulatory_hint_found_beacon(wiphy, channel, gfp);
649
Johannes Berg2a519312009-02-10 21:25:55 +0100650 /* cfg80211_bss_update gives us a referenced result */
651 return &res->pub;
652}
653EXPORT_SYMBOL(cfg80211_inform_bss_frame);
654
655void cfg80211_put_bss(struct cfg80211_bss *pub)
656{
657 struct cfg80211_internal_bss *bss;
658
659 if (!pub)
660 return;
661
662 bss = container_of(pub, struct cfg80211_internal_bss, pub);
663 kref_put(&bss->ref, bss_release);
664}
665EXPORT_SYMBOL(cfg80211_put_bss);
666
Johannes Bergd491af12009-02-10 21:25:58 +0100667void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
668{
669 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
670 struct cfg80211_internal_bss *bss;
671
672 if (WARN_ON(!pub))
673 return;
674
675 bss = container_of(pub, struct cfg80211_internal_bss, pub);
676
677 spin_lock_bh(&dev->bss_lock);
Johannes Berg32073902010-10-06 21:18:04 +0200678 if (!list_empty(&bss->list)) {
Juuso Oikarinen2b78ac92011-03-28 14:32:32 +0300679 __cfg80211_unlink_bss(dev, bss);
Johannes Berg32073902010-10-06 21:18:04 +0200680 dev->bss_generation++;
Johannes Berg32073902010-10-06 21:18:04 +0200681 }
Johannes Bergd491af12009-02-10 21:25:58 +0100682 spin_unlock_bh(&dev->bss_lock);
Johannes Bergd491af12009-02-10 21:25:58 +0100683}
684EXPORT_SYMBOL(cfg80211_unlink_bss);
685
Johannes Berg3d23e342009-09-29 23:27:28 +0200686#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +0100687int cfg80211_wext_siwscan(struct net_device *dev,
688 struct iw_request_info *info,
689 union iwreq_data *wrqu, char *extra)
690{
691 struct cfg80211_registered_device *rdev;
692 struct wiphy *wiphy;
693 struct iw_scan_req *wreq = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +0100694 struct cfg80211_scan_request *creq = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100695 int i, err, n_channels = 0;
696 enum ieee80211_band band;
697
698 if (!netif_running(dev))
699 return -ENETDOWN;
700
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200701 if (wrqu->data.length == sizeof(struct iw_scan_req))
702 wreq = (struct iw_scan_req *)extra;
703
Johannes Berg463d0182009-07-14 00:33:35 +0200704 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +0100705
706 if (IS_ERR(rdev))
707 return PTR_ERR(rdev);
708
709 if (rdev->scan_req) {
710 err = -EBUSY;
711 goto out;
712 }
713
714 wiphy = &rdev->wiphy;
715
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200716 /* Determine number of channels, needed to allocate creq */
717 if (wreq && wreq->num_channels)
718 n_channels = wreq->num_channels;
719 else {
720 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
721 if (wiphy->bands[band])
722 n_channels += wiphy->bands[band]->n_channels;
723 }
Johannes Berg2a519312009-02-10 21:25:55 +0100724
725 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
726 n_channels * sizeof(void *),
727 GFP_ATOMIC);
728 if (!creq) {
729 err = -ENOMEM;
730 goto out;
731 }
732
733 creq->wiphy = wiphy;
Johannes Berg463d0182009-07-14 00:33:35 +0200734 creq->dev = dev;
Johannes Berg5ba63532009-08-07 17:54:07 +0200735 /* SSIDs come after channels */
736 creq->ssids = (void *)&creq->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +0100737 creq->n_channels = n_channels;
738 creq->n_ssids = 1;
739
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200740 /* translate "Scan on frequencies" request */
Johannes Berg2a519312009-02-10 21:25:55 +0100741 i = 0;
742 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
743 int j;
Johannes Berg584991d2009-11-02 13:32:03 +0100744
Johannes Berg2a519312009-02-10 21:25:55 +0100745 if (!wiphy->bands[band])
746 continue;
Johannes Berg584991d2009-11-02 13:32:03 +0100747
Johannes Berg2a519312009-02-10 21:25:55 +0100748 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +0100749 /* ignore disabled channels */
750 if (wiphy->bands[band]->channels[j].flags &
751 IEEE80211_CHAN_DISABLED)
752 continue;
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200753
754 /* If we have a wireless request structure and the
755 * wireless request specifies frequencies, then search
756 * for the matching hardware channel.
757 */
758 if (wreq && wreq->num_channels) {
759 int k;
760 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
761 for (k = 0; k < wreq->num_channels; k++) {
Holger Schuriga4e7b732009-09-11 10:13:53 +0200762 int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200763 if (wext_freq == wiphy_freq)
764 goto wext_freq_found;
765 }
766 goto wext_freq_not_found;
767 }
768
769 wext_freq_found:
Johannes Berg2a519312009-02-10 21:25:55 +0100770 creq->channels[i] = &wiphy->bands[band]->channels[j];
771 i++;
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200772 wext_freq_not_found: ;
Johannes Berg2a519312009-02-10 21:25:55 +0100773 }
774 }
Holger Schurig8862dc52009-09-11 10:13:55 +0200775 /* No channels found? */
776 if (!i) {
777 err = -EINVAL;
778 goto out;
779 }
Johannes Berg2a519312009-02-10 21:25:55 +0100780
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200781 /* Set real number of channels specified in creq->channels[] */
782 creq->n_channels = i;
Johannes Berg2a519312009-02-10 21:25:55 +0100783
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200784 /* translate "Scan for SSID" request */
785 if (wreq) {
Johannes Berg2a519312009-02-10 21:25:55 +0100786 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Johannes Berg65486c82009-12-23 15:33:35 +0100787 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
788 err = -EINVAL;
789 goto out;
790 }
Johannes Berg2a519312009-02-10 21:25:55 +0100791 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
792 creq->ssids[0].ssid_len = wreq->essid_len;
793 }
794 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
795 creq->n_ssids = 0;
796 }
797
798 rdev->scan_req = creq;
799 err = rdev->ops->scan(wiphy, dev, creq);
800 if (err) {
801 rdev->scan_req = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +0100802 /* creq will be freed below */
Johannes Berg463d0182009-07-14 00:33:35 +0200803 } else {
Johannes Berga538e2d2009-06-16 19:56:42 +0200804 nl80211_send_scan_start(rdev, dev);
Johannes Berg65486c82009-12-23 15:33:35 +0100805 /* creq now owned by driver */
806 creq = NULL;
Johannes Berg463d0182009-07-14 00:33:35 +0200807 dev_hold(dev);
808 }
Johannes Berg2a519312009-02-10 21:25:55 +0100809 out:
Johannes Berg65486c82009-12-23 15:33:35 +0100810 kfree(creq);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200811 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100812 return err;
813}
Johannes Bergba44cb72009-04-20 18:49:39 +0200814EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +0100815
816static void ieee80211_scan_add_ies(struct iw_request_info *info,
817 struct cfg80211_bss *bss,
818 char **current_ev, char *end_buf)
819{
820 u8 *pos, *end, *next;
821 struct iw_event iwe;
822
823 if (!bss->information_elements ||
824 !bss->len_information_elements)
825 return;
826
827 /*
828 * If needed, fragment the IEs buffer (at IE boundaries) into short
829 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
830 */
831 pos = bss->information_elements;
832 end = pos + bss->len_information_elements;
833
834 while (end - pos > IW_GENERIC_IE_MAX) {
835 next = pos + 2 + pos[1];
836 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
837 next = next + 2 + next[1];
838
839 memset(&iwe, 0, sizeof(iwe));
840 iwe.cmd = IWEVGENIE;
841 iwe.u.data.length = next - pos;
842 *current_ev = iwe_stream_add_point(info, *current_ev,
843 end_buf, &iwe, pos);
844
845 pos = next;
846 }
847
848 if (end > pos) {
849 memset(&iwe, 0, sizeof(iwe));
850 iwe.cmd = IWEVGENIE;
851 iwe.u.data.length = end - pos;
852 *current_ev = iwe_stream_add_point(info, *current_ev,
853 end_buf, &iwe, pos);
854 }
855}
856
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500857static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
858{
859 unsigned long end = jiffies;
860
861 if (end >= start)
862 return jiffies_to_msecs(end - start);
863
864 return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
865}
Johannes Berg2a519312009-02-10 21:25:55 +0100866
867static char *
Johannes Berg77965c92009-02-18 18:45:06 +0100868ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
869 struct cfg80211_internal_bss *bss, char *current_ev,
870 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +0100871{
872 struct iw_event iwe;
873 u8 *buf, *cfg, *p;
874 u8 *ie = bss->pub.information_elements;
Johannes Berga77b8552009-02-18 18:27:22 +0100875 int rem = bss->pub.len_information_elements, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +0100876 bool ismesh = false;
877
878 memset(&iwe, 0, sizeof(iwe));
879 iwe.cmd = SIOCGIWAP;
880 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
881 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
882 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
883 IW_EV_ADDR_LEN);
884
885 memset(&iwe, 0, sizeof(iwe));
886 iwe.cmd = SIOCGIWFREQ;
887 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
888 iwe.u.freq.e = 0;
889 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
890 IW_EV_FREQ_LEN);
891
892 memset(&iwe, 0, sizeof(iwe));
893 iwe.cmd = SIOCGIWFREQ;
894 iwe.u.freq.m = bss->pub.channel->center_freq;
895 iwe.u.freq.e = 6;
896 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
897 IW_EV_FREQ_LEN);
898
Johannes Berg77965c92009-02-18 18:45:06 +0100899 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +0100900 memset(&iwe, 0, sizeof(iwe));
901 iwe.cmd = IWEVQUAL;
902 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
903 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +0100904 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c92009-02-18 18:45:06 +0100905 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +0100906 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +0100907 sig = bss->pub.signal / 100;
908 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +0100909 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +0100910 if (sig < -110) /* rather bad */
911 sig = -110;
912 else if (sig > -40) /* perfect */
913 sig = -40;
914 /* will give a range of 0 .. 70 */
915 iwe.u.qual.qual = sig + 110;
Johannes Berg2a519312009-02-10 21:25:55 +0100916 break;
917 case CFG80211_SIGNAL_TYPE_UNSPEC:
918 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +0100919 /* will give range 0 .. 100 */
920 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +0100921 break;
922 default:
923 /* not reached */
924 break;
925 }
926 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
927 &iwe, IW_EV_QUAL_LEN);
928 }
929
930 memset(&iwe, 0, sizeof(iwe));
931 iwe.cmd = SIOCGIWENCODE;
932 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
933 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
934 else
935 iwe.u.data.flags = IW_ENCODE_DISABLED;
936 iwe.u.data.length = 0;
937 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
938 &iwe, "");
939
940 while (rem >= 2) {
941 /* invalid data */
942 if (ie[1] > rem - 2)
943 break;
944
945 switch (ie[0]) {
946 case WLAN_EID_SSID:
947 memset(&iwe, 0, sizeof(iwe));
948 iwe.cmd = SIOCGIWESSID;
949 iwe.u.data.length = ie[1];
950 iwe.u.data.flags = 1;
951 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
952 &iwe, ie + 2);
953 break;
954 case WLAN_EID_MESH_ID:
955 memset(&iwe, 0, sizeof(iwe));
956 iwe.cmd = SIOCGIWESSID;
957 iwe.u.data.length = ie[1];
958 iwe.u.data.flags = 1;
959 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
960 &iwe, ie + 2);
961 break;
962 case WLAN_EID_MESH_CONFIG:
963 ismesh = true;
Rui Paulo136cfa22009-11-18 18:40:00 +0000964 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +0100965 break;
966 buf = kmalloc(50, GFP_ATOMIC);
967 if (!buf)
968 break;
969 cfg = ie + 2;
970 memset(&iwe, 0, sizeof(iwe));
971 iwe.cmd = IWEVCUSTOM;
Rui Paulo76aa5e72009-11-18 18:22:59 +0000972 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
973 "0x%02X", cfg[0]);
Johannes Berg2a519312009-02-10 21:25:55 +0100974 iwe.u.data.length = strlen(buf);
975 current_ev = iwe_stream_add_point(info, current_ev,
976 end_buf,
977 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +0000978 sprintf(buf, "Path Selection Metric ID: 0x%02X",
979 cfg[1]);
Johannes Berg2a519312009-02-10 21:25:55 +0100980 iwe.u.data.length = strlen(buf);
981 current_ev = iwe_stream_add_point(info, current_ev,
982 end_buf,
983 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +0000984 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
985 cfg[2]);
Johannes Berg2a519312009-02-10 21:25:55 +0100986 iwe.u.data.length = strlen(buf);
987 current_ev = iwe_stream_add_point(info, current_ev,
988 end_buf,
989 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +0000990 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
Johannes Berg2a519312009-02-10 21:25:55 +0100991 iwe.u.data.length = strlen(buf);
992 current_ev = iwe_stream_add_point(info, current_ev,
993 end_buf,
994 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +0000995 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
996 iwe.u.data.length = strlen(buf);
997 current_ev = iwe_stream_add_point(info, current_ev,
998 end_buf,
999 &iwe, buf);
1000 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1001 iwe.u.data.length = strlen(buf);
1002 current_ev = iwe_stream_add_point(info, current_ev,
1003 end_buf,
1004 &iwe, buf);
1005 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
Johannes Berg2a519312009-02-10 21:25:55 +01001006 iwe.u.data.length = strlen(buf);
1007 current_ev = iwe_stream_add_point(info, current_ev,
1008 end_buf,
1009 &iwe, buf);
1010 kfree(buf);
1011 break;
1012 case WLAN_EID_SUPP_RATES:
1013 case WLAN_EID_EXT_SUPP_RATES:
1014 /* display all supported rates in readable format */
1015 p = current_ev + iwe_stream_lcp_len(info);
1016
1017 memset(&iwe, 0, sizeof(iwe));
1018 iwe.cmd = SIOCGIWRATE;
1019 /* Those two flags are ignored... */
1020 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1021
1022 for (i = 0; i < ie[1]; i++) {
1023 iwe.u.bitrate.value =
1024 ((ie[i + 2] & 0x7f) * 500000);
1025 p = iwe_stream_add_value(info, current_ev, p,
1026 end_buf, &iwe, IW_EV_PARAM_LEN);
1027 }
1028 current_ev = p;
1029 break;
1030 }
1031 rem -= ie[1] + 2;
1032 ie += ie[1] + 2;
1033 }
1034
Joe Perchesf64f9e72009-11-29 16:55:45 -08001035 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1036 ismesh) {
Johannes Berg2a519312009-02-10 21:25:55 +01001037 memset(&iwe, 0, sizeof(iwe));
1038 iwe.cmd = SIOCGIWMODE;
1039 if (ismesh)
1040 iwe.u.mode = IW_MODE_MESH;
1041 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
1042 iwe.u.mode = IW_MODE_MASTER;
1043 else
1044 iwe.u.mode = IW_MODE_ADHOC;
1045 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1046 &iwe, IW_EV_UINT_LEN);
1047 }
1048
1049 buf = kmalloc(30, GFP_ATOMIC);
1050 if (buf) {
1051 memset(&iwe, 0, sizeof(iwe));
1052 iwe.cmd = IWEVCUSTOM;
1053 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
1054 iwe.u.data.length = strlen(buf);
1055 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1056 &iwe, buf);
1057 memset(&iwe, 0, sizeof(iwe));
1058 iwe.cmd = IWEVCUSTOM;
Dan Williamscb3a8ee2009-02-11 17:14:43 -05001059 sprintf(buf, " Last beacon: %ums ago",
1060 elapsed_jiffies_msecs(bss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01001061 iwe.u.data.length = strlen(buf);
1062 current_ev = iwe_stream_add_point(info, current_ev,
1063 end_buf, &iwe, buf);
1064 kfree(buf);
1065 }
1066
1067 ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
1068
1069 return current_ev;
1070}
1071
1072
1073static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
1074 struct iw_request_info *info,
1075 char *buf, size_t len)
1076{
1077 char *current_ev = buf;
1078 char *end_buf = buf + len;
1079 struct cfg80211_internal_bss *bss;
1080
1081 spin_lock_bh(&dev->bss_lock);
1082 cfg80211_bss_expire(dev);
1083
1084 list_for_each_entry(bss, &dev->bss_list, list) {
1085 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1086 spin_unlock_bh(&dev->bss_lock);
1087 return -E2BIG;
1088 }
Johannes Berg77965c92009-02-18 18:45:06 +01001089 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
1090 current_ev, end_buf);
Johannes Berg2a519312009-02-10 21:25:55 +01001091 }
1092 spin_unlock_bh(&dev->bss_lock);
1093 return current_ev - buf;
1094}
1095
1096
1097int cfg80211_wext_giwscan(struct net_device *dev,
1098 struct iw_request_info *info,
1099 struct iw_point *data, char *extra)
1100{
1101 struct cfg80211_registered_device *rdev;
1102 int res;
1103
1104 if (!netif_running(dev))
1105 return -ENETDOWN;
1106
Johannes Berg463d0182009-07-14 00:33:35 +02001107 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01001108
1109 if (IS_ERR(rdev))
1110 return PTR_ERR(rdev);
1111
1112 if (rdev->scan_req) {
1113 res = -EAGAIN;
1114 goto out;
1115 }
1116
1117 res = ieee80211_scan_results(rdev, info, extra, data->length);
1118 data->length = 0;
1119 if (res >= 0) {
1120 data->length = res;
1121 res = 0;
1122 }
1123
1124 out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001125 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01001126 return res;
1127}
Johannes Bergba44cb72009-04-20 18:49:39 +02001128EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001129#endif