mac80211: move TX status handling
[deliverable/linux.git] / net / wireless / scan.c
CommitLineData
2a519311
JB
1/*
2 * cfg80211 scan result handling
3 *
4 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
5 */
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/netdevice.h>
9#include <linux/wireless.h>
10#include <linux/nl80211.h>
11#include <linux/etherdevice.h>
12#include <net/arp.h>
13#include <net/cfg80211.h>
14#include <net/iw_handler.h>
15#include "core.h"
16#include "nl80211.h"
a9a11622 17#include "wext-compat.h"
2a519311 18
09f97e0f 19#define IEEE80211_SCAN_RESULT_EXPIRE (15 * HZ)
2a519311 20
01a0ac41 21void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak)
2a519311 22{
667503dd 23 struct cfg80211_scan_request *request;
2a519311 24 struct net_device *dev;
3d23e349 25#ifdef CONFIG_CFG80211_WEXT
2a519311
JB
26 union iwreq_data wrqu;
27#endif
28
01a0ac41
JB
29 ASSERT_RDEV_LOCK(rdev);
30
667503dd
JB
31 request = rdev->scan_req;
32
01a0ac41
JB
33 if (!request)
34 return;
35
463d0183 36 dev = request->dev;
2a519311 37
6829c878
JB
38 /*
39 * This must be before sending the other events!
40 * Otherwise, wpa_supplicant gets completely confused with
41 * wext events.
42 */
43 cfg80211_sme_scan_done(dev);
44
667503dd 45 if (request->aborted)
36e6fea8 46 nl80211_send_scan_aborted(rdev, dev);
2a519311 47 else
36e6fea8 48 nl80211_send_scan_done(rdev, dev);
2a519311 49
3d23e349 50#ifdef CONFIG_CFG80211_WEXT
667503dd 51 if (!request->aborted) {
2a519311
JB
52 memset(&wrqu, 0, sizeof(wrqu));
53
54 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
55 }
56#endif
57
58 dev_put(dev);
59
36e6fea8 60 rdev->scan_req = NULL;
01a0ac41
JB
61
62 /*
63 * OK. If this is invoked with "leak" then we can't
64 * free this ... but we've cleaned it up anyway. The
65 * driver failed to call the scan_done callback, so
66 * all bets are off, it might still be trying to use
67 * the scan request or not ... if it accesses the dev
68 * in there (it shouldn't anyway) then it may crash.
69 */
70 if (!leak)
71 kfree(request);
2a519311 72}
667503dd 73
36e6fea8
JB
74void __cfg80211_scan_done(struct work_struct *wk)
75{
76 struct cfg80211_registered_device *rdev;
77
78 rdev = container_of(wk, struct cfg80211_registered_device,
79 scan_done_wk);
80
81 cfg80211_lock_rdev(rdev);
01a0ac41 82 ___cfg80211_scan_done(rdev, false);
36e6fea8
JB
83 cfg80211_unlock_rdev(rdev);
84}
85
667503dd
JB
86void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
87{
667503dd
JB
88 WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
89
90 request->aborted = aborted;
91 schedule_work(&wiphy_to_dev(request->wiphy)->scan_done_wk);
667503dd 92}
2a519311
JB
93EXPORT_SYMBOL(cfg80211_scan_done);
94
95static void bss_release(struct kref *ref)
96{
97 struct cfg80211_internal_bss *bss;
98
99 bss = container_of(ref, struct cfg80211_internal_bss, ref);
78c1c7e1
JB
100 if (bss->pub.free_priv)
101 bss->pub.free_priv(&bss->pub);
cd1658f5
JB
102
103 if (bss->ies_allocated)
104 kfree(bss->pub.information_elements);
105
19957bb3
JB
106 BUG_ON(atomic_read(&bss->hold));
107
2a519311
JB
108 kfree(bss);
109}
110
cb3a8eec
DW
111/* must hold dev->bss_lock! */
112void cfg80211_bss_age(struct cfg80211_registered_device *dev,
113 unsigned long age_secs)
114{
115 struct cfg80211_internal_bss *bss;
116 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
117
118 list_for_each_entry(bss, &dev->bss_list, list) {
119 bss->ts -= age_jiffies;
120 }
121}
122
2a519311
JB
123/* must hold dev->bss_lock! */
124void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
125{
126 struct cfg80211_internal_bss *bss, *tmp;
127 bool expired = false;
128
129 list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
19957bb3
JB
130 if (atomic_read(&bss->hold))
131 continue;
132 if (!time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE))
2a519311
JB
133 continue;
134 list_del(&bss->list);
135 rb_erase(&bss->rbn, &dev->bss_tree);
136 kref_put(&bss->ref, bss_release);
137 expired = true;
138 }
139
140 if (expired)
141 dev->bss_generation++;
142}
143
fcc6cb0c 144static u8 *find_ie(u8 num, u8 *ies, int len)
2a519311
JB
145{
146 while (len > 2 && ies[0] != num) {
147 len -= ies[1] + 2;
148 ies += ies[1] + 2;
149 }
150 if (len < 2)
151 return NULL;
152 if (len < 2 + ies[1])
153 return NULL;
154 return ies;
155}
156
157static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
158{
159 const u8 *ie1 = find_ie(num, ies1, len1);
160 const u8 *ie2 = find_ie(num, ies2, len2);
161 int r;
162
163 if (!ie1 && !ie2)
164 return 0;
cd3468ba 165 if (!ie1 || !ie2)
2a519311
JB
166 return -1;
167
168 r = memcmp(ie1 + 2, ie2 + 2, min(ie1[1], ie2[1]));
169 if (r == 0 && ie1[1] != ie2[1])
170 return ie2[1] - ie1[1];
171 return r;
172}
173
174static bool is_bss(struct cfg80211_bss *a,
175 const u8 *bssid,
176 const u8 *ssid, size_t ssid_len)
177{
178 const u8 *ssidie;
179
79420f09 180 if (bssid && compare_ether_addr(a->bssid, bssid))
2a519311
JB
181 return false;
182
79420f09
JB
183 if (!ssid)
184 return true;
185
2a519311
JB
186 ssidie = find_ie(WLAN_EID_SSID,
187 a->information_elements,
188 a->len_information_elements);
189 if (!ssidie)
190 return false;
191 if (ssidie[1] != ssid_len)
192 return false;
193 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
194}
195
196static bool is_mesh(struct cfg80211_bss *a,
197 const u8 *meshid, size_t meshidlen,
198 const u8 *meshcfg)
199{
200 const u8 *ie;
201
202 if (!is_zero_ether_addr(a->bssid))
203 return false;
204
205 ie = find_ie(WLAN_EID_MESH_ID,
206 a->information_elements,
207 a->len_information_elements);
208 if (!ie)
209 return false;
210 if (ie[1] != meshidlen)
211 return false;
212 if (memcmp(ie + 2, meshid, meshidlen))
213 return false;
214
215 ie = find_ie(WLAN_EID_MESH_CONFIG,
216 a->information_elements,
217 a->len_information_elements);
cd3468ba
JB
218 if (!ie)
219 return false;
2a519311
JB
220 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
221 return false;
222
223 /*
224 * Ignore mesh capability (last two bytes of the IE) when
225 * comparing since that may differ between stations taking
226 * part in the same mesh.
227 */
228 return memcmp(ie + 2, meshcfg, IEEE80211_MESH_CONFIG_LEN - 2) == 0;
229}
230
231static int cmp_bss(struct cfg80211_bss *a,
232 struct cfg80211_bss *b)
233{
234 int r;
235
236 if (a->channel != b->channel)
237 return b->channel->center_freq - a->channel->center_freq;
238
239 r = memcmp(a->bssid, b->bssid, ETH_ALEN);
240 if (r)
241 return r;
242
243 if (is_zero_ether_addr(a->bssid)) {
244 r = cmp_ies(WLAN_EID_MESH_ID,
245 a->information_elements,
246 a->len_information_elements,
247 b->information_elements,
248 b->len_information_elements);
249 if (r)
250 return r;
251 return cmp_ies(WLAN_EID_MESH_CONFIG,
252 a->information_elements,
253 a->len_information_elements,
254 b->information_elements,
255 b->len_information_elements);
256 }
257
258 return cmp_ies(WLAN_EID_SSID,
259 a->information_elements,
260 a->len_information_elements,
261 b->information_elements,
262 b->len_information_elements);
263}
264
265struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
266 struct ieee80211_channel *channel,
267 const u8 *bssid,
79420f09
JB
268 const u8 *ssid, size_t ssid_len,
269 u16 capa_mask, u16 capa_val)
2a519311
JB
270{
271 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
272 struct cfg80211_internal_bss *bss, *res = NULL;
273
274 spin_lock_bh(&dev->bss_lock);
275
276 list_for_each_entry(bss, &dev->bss_list, list) {
79420f09
JB
277 if ((bss->pub.capability & capa_mask) != capa_val)
278 continue;
2a519311
JB
279 if (channel && bss->pub.channel != channel)
280 continue;
281 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
282 res = bss;
283 kref_get(&res->ref);
284 break;
285 }
286 }
287
288 spin_unlock_bh(&dev->bss_lock);
289 if (!res)
290 return NULL;
291 return &res->pub;
292}
293EXPORT_SYMBOL(cfg80211_get_bss);
294
295struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
296 struct ieee80211_channel *channel,
297 const u8 *meshid, size_t meshidlen,
298 const u8 *meshcfg)
299{
300 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
301 struct cfg80211_internal_bss *bss, *res = NULL;
302
303 spin_lock_bh(&dev->bss_lock);
304
305 list_for_each_entry(bss, &dev->bss_list, list) {
306 if (channel && bss->pub.channel != channel)
307 continue;
308 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
309 res = bss;
310 kref_get(&res->ref);
311 break;
312 }
313 }
314
315 spin_unlock_bh(&dev->bss_lock);
316 if (!res)
317 return NULL;
318 return &res->pub;
319}
320EXPORT_SYMBOL(cfg80211_get_mesh);
321
322
323static void rb_insert_bss(struct cfg80211_registered_device *dev,
324 struct cfg80211_internal_bss *bss)
325{
326 struct rb_node **p = &dev->bss_tree.rb_node;
327 struct rb_node *parent = NULL;
328 struct cfg80211_internal_bss *tbss;
329 int cmp;
330
331 while (*p) {
332 parent = *p;
333 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
334
335 cmp = cmp_bss(&bss->pub, &tbss->pub);
336
337 if (WARN_ON(!cmp)) {
338 /* will sort of leak this BSS */
339 return;
340 }
341
342 if (cmp < 0)
343 p = &(*p)->rb_left;
344 else
345 p = &(*p)->rb_right;
346 }
347
348 rb_link_node(&bss->rbn, parent, p);
349 rb_insert_color(&bss->rbn, &dev->bss_tree);
350}
351
352static struct cfg80211_internal_bss *
353rb_find_bss(struct cfg80211_registered_device *dev,
354 struct cfg80211_internal_bss *res)
355{
356 struct rb_node *n = dev->bss_tree.rb_node;
357 struct cfg80211_internal_bss *bss;
358 int r;
359
360 while (n) {
361 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
362 r = cmp_bss(&res->pub, &bss->pub);
363
364 if (r == 0)
365 return bss;
366 else if (r < 0)
367 n = n->rb_left;
368 else
369 n = n->rb_right;
370 }
371
372 return NULL;
373}
374
375static struct cfg80211_internal_bss *
376cfg80211_bss_update(struct cfg80211_registered_device *dev,
377 struct cfg80211_internal_bss *res,
378 bool overwrite)
379{
380 struct cfg80211_internal_bss *found = NULL;
381 const u8 *meshid, *meshcfg;
382
383 /*
384 * The reference to "res" is donated to this function.
385 */
386
387 if (WARN_ON(!res->pub.channel)) {
388 kref_put(&res->ref, bss_release);
389 return NULL;
390 }
391
392 res->ts = jiffies;
393
394 if (is_zero_ether_addr(res->pub.bssid)) {
395 /* must be mesh, verify */
396 meshid = find_ie(WLAN_EID_MESH_ID, res->pub.information_elements,
397 res->pub.len_information_elements);
398 meshcfg = find_ie(WLAN_EID_MESH_CONFIG,
399 res->pub.information_elements,
400 res->pub.len_information_elements);
401 if (!meshid || !meshcfg ||
402 meshcfg[1] != IEEE80211_MESH_CONFIG_LEN) {
403 /* bogus mesh */
404 kref_put(&res->ref, bss_release);
405 return NULL;
406 }
407 }
408
409 spin_lock_bh(&dev->bss_lock);
410
411 found = rb_find_bss(dev, res);
412
cd1658f5 413 if (found) {
2a519311
JB
414 found->pub.beacon_interval = res->pub.beacon_interval;
415 found->pub.tsf = res->pub.tsf;
416 found->pub.signal = res->pub.signal;
2a519311
JB
417 found->pub.capability = res->pub.capability;
418 found->ts = res->ts;
cd1658f5
JB
419
420 /* overwrite IEs */
421 if (overwrite) {
422 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
423 size_t ielen = res->pub.len_information_elements;
424
44e1b98f 425 if (!found->ies_allocated && ksize(found) >= used + ielen) {
cd1658f5
JB
426 memcpy(found->pub.information_elements,
427 res->pub.information_elements, ielen);
428 found->pub.len_information_elements = ielen;
429 } else {
430 u8 *ies = found->pub.information_elements;
431
273de92c
MB
432 if (found->ies_allocated)
433 ies = krealloc(ies, ielen, GFP_ATOMIC);
434 else
cd1658f5
JB
435 ies = kmalloc(ielen, GFP_ATOMIC);
436
437 if (ies) {
438 memcpy(ies, res->pub.information_elements, ielen);
439 found->ies_allocated = true;
440 found->pub.information_elements = ies;
c0f0aac0 441 found->pub.len_information_elements = ielen;
cd1658f5
JB
442 }
443 }
444 }
445
2a519311
JB
446 kref_put(&res->ref, bss_release);
447 } else {
448 /* this "consumes" the reference */
449 list_add_tail(&res->list, &dev->bss_list);
450 rb_insert_bss(dev, res);
451 found = res;
452 }
453
454 dev->bss_generation++;
455 spin_unlock_bh(&dev->bss_lock);
456
457 kref_get(&found->ref);
458 return found;
459}
460
06aa7afa
JK
461struct cfg80211_bss*
462cfg80211_inform_bss(struct wiphy *wiphy,
463 struct ieee80211_channel *channel,
464 const u8 *bssid,
465 u64 timestamp, u16 capability, u16 beacon_interval,
466 const u8 *ie, size_t ielen,
467 s32 signal, gfp_t gfp)
468{
469 struct cfg80211_internal_bss *res;
470 size_t privsz;
471
472 if (WARN_ON(!wiphy))
473 return NULL;
474
475 privsz = wiphy->bss_priv_size;
476
477 if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
478 (signal < 0 || signal > 100)))
479 return NULL;
480
481 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
482 if (!res)
483 return NULL;
484
485 memcpy(res->pub.bssid, bssid, ETH_ALEN);
486 res->pub.channel = channel;
487 res->pub.signal = signal;
488 res->pub.tsf = timestamp;
489 res->pub.beacon_interval = beacon_interval;
490 res->pub.capability = capability;
491 /* point to after the private area */
492 res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
493 memcpy(res->pub.information_elements, ie, ielen);
494 res->pub.len_information_elements = ielen;
495
496 kref_init(&res->ref);
497
498 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, 0);
499 if (!res)
500 return NULL;
501
502 if (res->pub.capability & WLAN_CAPABILITY_ESS)
503 regulatory_hint_found_beacon(wiphy, channel, gfp);
504
505 /* cfg80211_bss_update gives us a referenced result */
506 return &res->pub;
507}
508EXPORT_SYMBOL(cfg80211_inform_bss);
509
2a519311
JB
510struct cfg80211_bss *
511cfg80211_inform_bss_frame(struct wiphy *wiphy,
512 struct ieee80211_channel *channel,
513 struct ieee80211_mgmt *mgmt, size_t len,
77965c97 514 s32 signal, gfp_t gfp)
2a519311
JB
515{
516 struct cfg80211_internal_bss *res;
517 size_t ielen = len - offsetof(struct ieee80211_mgmt,
518 u.probe_resp.variable);
519 bool overwrite;
520 size_t privsz = wiphy->bss_priv_size;
521
77965c97 522 if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
2a519311
JB
523 (signal < 0 || signal > 100)))
524 return NULL;
525
526 if (WARN_ON(!mgmt || !wiphy ||
527 len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
528 return NULL;
529
530 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
531 if (!res)
532 return NULL;
533
534 memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
535 res->pub.channel = channel;
2a519311
JB
536 res->pub.signal = signal;
537 res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
538 res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
539 res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
540 /* point to after the private area */
541 res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
542 memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen);
543 res->pub.len_information_elements = ielen;
544
545 kref_init(&res->ref);
546
547 overwrite = ieee80211_is_probe_resp(mgmt->frame_control);
548
549 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite);
550 if (!res)
551 return NULL;
552
e38f8a7a
LR
553 if (res->pub.capability & WLAN_CAPABILITY_ESS)
554 regulatory_hint_found_beacon(wiphy, channel, gfp);
555
2a519311
JB
556 /* cfg80211_bss_update gives us a referenced result */
557 return &res->pub;
558}
559EXPORT_SYMBOL(cfg80211_inform_bss_frame);
560
561void cfg80211_put_bss(struct cfg80211_bss *pub)
562{
563 struct cfg80211_internal_bss *bss;
564
565 if (!pub)
566 return;
567
568 bss = container_of(pub, struct cfg80211_internal_bss, pub);
569 kref_put(&bss->ref, bss_release);
570}
571EXPORT_SYMBOL(cfg80211_put_bss);
572
d491af19
JB
573void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
574{
575 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
576 struct cfg80211_internal_bss *bss;
577
578 if (WARN_ON(!pub))
579 return;
580
581 bss = container_of(pub, struct cfg80211_internal_bss, pub);
582
583 spin_lock_bh(&dev->bss_lock);
584
585 list_del(&bss->list);
f5ea9120 586 dev->bss_generation++;
d491af19
JB
587 rb_erase(&bss->rbn, &dev->bss_tree);
588
589 spin_unlock_bh(&dev->bss_lock);
590
591 kref_put(&bss->ref, bss_release);
592}
593EXPORT_SYMBOL(cfg80211_unlink_bss);
594
3d23e349 595#ifdef CONFIG_CFG80211_WEXT
2a519311
JB
596int cfg80211_wext_siwscan(struct net_device *dev,
597 struct iw_request_info *info,
598 union iwreq_data *wrqu, char *extra)
599{
600 struct cfg80211_registered_device *rdev;
601 struct wiphy *wiphy;
602 struct iw_scan_req *wreq = NULL;
603 struct cfg80211_scan_request *creq;
604 int i, err, n_channels = 0;
605 enum ieee80211_band band;
606
607 if (!netif_running(dev))
608 return -ENETDOWN;
609
b2e3abdc
HS
610 if (wrqu->data.length == sizeof(struct iw_scan_req))
611 wreq = (struct iw_scan_req *)extra;
612
463d0183 613 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2a519311
JB
614
615 if (IS_ERR(rdev))
616 return PTR_ERR(rdev);
617
618 if (rdev->scan_req) {
619 err = -EBUSY;
620 goto out;
621 }
622
623 wiphy = &rdev->wiphy;
624
b2e3abdc
HS
625 /* Determine number of channels, needed to allocate creq */
626 if (wreq && wreq->num_channels)
627 n_channels = wreq->num_channels;
628 else {
629 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
630 if (wiphy->bands[band])
631 n_channels += wiphy->bands[band]->n_channels;
632 }
2a519311
JB
633
634 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
635 n_channels * sizeof(void *),
636 GFP_ATOMIC);
637 if (!creq) {
638 err = -ENOMEM;
639 goto out;
640 }
641
642 creq->wiphy = wiphy;
463d0183 643 creq->dev = dev;
5ba63533
JB
644 /* SSIDs come after channels */
645 creq->ssids = (void *)&creq->channels[n_channels];
2a519311
JB
646 creq->n_channels = n_channels;
647 creq->n_ssids = 1;
648
b2e3abdc 649 /* translate "Scan on frequencies" request */
2a519311
JB
650 i = 0;
651 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
652 int j;
584991dc 653
2a519311
JB
654 if (!wiphy->bands[band])
655 continue;
584991dc 656
2a519311 657 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
584991dc
JB
658 /* ignore disabled channels */
659 if (wiphy->bands[band]->channels[j].flags &
660 IEEE80211_CHAN_DISABLED)
661 continue;
b2e3abdc
HS
662
663 /* If we have a wireless request structure and the
664 * wireless request specifies frequencies, then search
665 * for the matching hardware channel.
666 */
667 if (wreq && wreq->num_channels) {
668 int k;
669 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
670 for (k = 0; k < wreq->num_channels; k++) {
a4e7b730 671 int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
b2e3abdc
HS
672 if (wext_freq == wiphy_freq)
673 goto wext_freq_found;
674 }
675 goto wext_freq_not_found;
676 }
677
678 wext_freq_found:
2a519311
JB
679 creq->channels[i] = &wiphy->bands[band]->channels[j];
680 i++;
b2e3abdc 681 wext_freq_not_found: ;
2a519311
JB
682 }
683 }
8862dc5f
HS
684 /* No channels found? */
685 if (!i) {
686 err = -EINVAL;
687 goto out;
688 }
2a519311 689
b2e3abdc
HS
690 /* Set real number of channels specified in creq->channels[] */
691 creq->n_channels = i;
2a519311 692
b2e3abdc
HS
693 /* translate "Scan for SSID" request */
694 if (wreq) {
2a519311
JB
695 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
696 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN)
697 return -EINVAL;
698 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
699 creq->ssids[0].ssid_len = wreq->essid_len;
700 }
701 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
702 creq->n_ssids = 0;
703 }
704
705 rdev->scan_req = creq;
706 err = rdev->ops->scan(wiphy, dev, creq);
707 if (err) {
708 rdev->scan_req = NULL;
709 kfree(creq);
463d0183 710 } else {
a538e2d5 711 nl80211_send_scan_start(rdev, dev);
463d0183
JB
712 dev_hold(dev);
713 }
2a519311 714 out:
4d0c8aea 715 cfg80211_unlock_rdev(rdev);
2a519311
JB
716 return err;
717}
ba44cb72 718EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
2a519311
JB
719
720static void ieee80211_scan_add_ies(struct iw_request_info *info,
721 struct cfg80211_bss *bss,
722 char **current_ev, char *end_buf)
723{
724 u8 *pos, *end, *next;
725 struct iw_event iwe;
726
727 if (!bss->information_elements ||
728 !bss->len_information_elements)
729 return;
730
731 /*
732 * If needed, fragment the IEs buffer (at IE boundaries) into short
733 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
734 */
735 pos = bss->information_elements;
736 end = pos + bss->len_information_elements;
737
738 while (end - pos > IW_GENERIC_IE_MAX) {
739 next = pos + 2 + pos[1];
740 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
741 next = next + 2 + next[1];
742
743 memset(&iwe, 0, sizeof(iwe));
744 iwe.cmd = IWEVGENIE;
745 iwe.u.data.length = next - pos;
746 *current_ev = iwe_stream_add_point(info, *current_ev,
747 end_buf, &iwe, pos);
748
749 pos = next;
750 }
751
752 if (end > pos) {
753 memset(&iwe, 0, sizeof(iwe));
754 iwe.cmd = IWEVGENIE;
755 iwe.u.data.length = end - pos;
756 *current_ev = iwe_stream_add_point(info, *current_ev,
757 end_buf, &iwe, pos);
758 }
759}
760
cb3a8eec
DW
761static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
762{
763 unsigned long end = jiffies;
764
765 if (end >= start)
766 return jiffies_to_msecs(end - start);
767
768 return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
769}
2a519311
JB
770
771static char *
77965c97
JB
772ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
773 struct cfg80211_internal_bss *bss, char *current_ev,
774 char *end_buf)
2a519311
JB
775{
776 struct iw_event iwe;
777 u8 *buf, *cfg, *p;
778 u8 *ie = bss->pub.information_elements;
a77b8552 779 int rem = bss->pub.len_information_elements, i, sig;
2a519311
JB
780 bool ismesh = false;
781
782 memset(&iwe, 0, sizeof(iwe));
783 iwe.cmd = SIOCGIWAP;
784 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
785 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
786 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
787 IW_EV_ADDR_LEN);
788
789 memset(&iwe, 0, sizeof(iwe));
790 iwe.cmd = SIOCGIWFREQ;
791 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
792 iwe.u.freq.e = 0;
793 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
794 IW_EV_FREQ_LEN);
795
796 memset(&iwe, 0, sizeof(iwe));
797 iwe.cmd = SIOCGIWFREQ;
798 iwe.u.freq.m = bss->pub.channel->center_freq;
799 iwe.u.freq.e = 6;
800 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
801 IW_EV_FREQ_LEN);
802
77965c97 803 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
2a519311
JB
804 memset(&iwe, 0, sizeof(iwe));
805 iwe.cmd = IWEVQUAL;
806 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
807 IW_QUAL_NOISE_INVALID |
a77b8552 808 IW_QUAL_QUAL_UPDATED;
77965c97 809 switch (wiphy->signal_type) {
2a519311 810 case CFG80211_SIGNAL_TYPE_MBM:
a77b8552
JB
811 sig = bss->pub.signal / 100;
812 iwe.u.qual.level = sig;
2a519311 813 iwe.u.qual.updated |= IW_QUAL_DBM;
a77b8552
JB
814 if (sig < -110) /* rather bad */
815 sig = -110;
816 else if (sig > -40) /* perfect */
817 sig = -40;
818 /* will give a range of 0 .. 70 */
819 iwe.u.qual.qual = sig + 110;
2a519311
JB
820 break;
821 case CFG80211_SIGNAL_TYPE_UNSPEC:
822 iwe.u.qual.level = bss->pub.signal;
a77b8552
JB
823 /* will give range 0 .. 100 */
824 iwe.u.qual.qual = bss->pub.signal;
2a519311
JB
825 break;
826 default:
827 /* not reached */
828 break;
829 }
830 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
831 &iwe, IW_EV_QUAL_LEN);
832 }
833
834 memset(&iwe, 0, sizeof(iwe));
835 iwe.cmd = SIOCGIWENCODE;
836 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
837 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
838 else
839 iwe.u.data.flags = IW_ENCODE_DISABLED;
840 iwe.u.data.length = 0;
841 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
842 &iwe, "");
843
844 while (rem >= 2) {
845 /* invalid data */
846 if (ie[1] > rem - 2)
847 break;
848
849 switch (ie[0]) {
850 case WLAN_EID_SSID:
851 memset(&iwe, 0, sizeof(iwe));
852 iwe.cmd = SIOCGIWESSID;
853 iwe.u.data.length = ie[1];
854 iwe.u.data.flags = 1;
855 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
856 &iwe, ie + 2);
857 break;
858 case WLAN_EID_MESH_ID:
859 memset(&iwe, 0, sizeof(iwe));
860 iwe.cmd = SIOCGIWESSID;
861 iwe.u.data.length = ie[1];
862 iwe.u.data.flags = 1;
863 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
864 &iwe, ie + 2);
865 break;
866 case WLAN_EID_MESH_CONFIG:
867 ismesh = true;
868 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
869 break;
870 buf = kmalloc(50, GFP_ATOMIC);
871 if (!buf)
872 break;
873 cfg = ie + 2;
874 memset(&iwe, 0, sizeof(iwe));
875 iwe.cmd = IWEVCUSTOM;
876 sprintf(buf, "Mesh network (version %d)", cfg[0]);
877 iwe.u.data.length = strlen(buf);
878 current_ev = iwe_stream_add_point(info, current_ev,
879 end_buf,
880 &iwe, buf);
881 sprintf(buf, "Path Selection Protocol ID: "
882 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
883 cfg[4]);
884 iwe.u.data.length = strlen(buf);
885 current_ev = iwe_stream_add_point(info, current_ev,
886 end_buf,
887 &iwe, buf);
888 sprintf(buf, "Path Selection Metric ID: "
889 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
890 cfg[8]);
891 iwe.u.data.length = strlen(buf);
892 current_ev = iwe_stream_add_point(info, current_ev,
893 end_buf,
894 &iwe, buf);
895 sprintf(buf, "Congestion Control Mode ID: "
896 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
897 cfg[11], cfg[12]);
898 iwe.u.data.length = strlen(buf);
899 current_ev = iwe_stream_add_point(info, current_ev,
900 end_buf,
901 &iwe, buf);
902 sprintf(buf, "Channel Precedence: "
903 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
904 cfg[15], cfg[16]);
905 iwe.u.data.length = strlen(buf);
906 current_ev = iwe_stream_add_point(info, current_ev,
907 end_buf,
908 &iwe, buf);
909 kfree(buf);
910 break;
911 case WLAN_EID_SUPP_RATES:
912 case WLAN_EID_EXT_SUPP_RATES:
913 /* display all supported rates in readable format */
914 p = current_ev + iwe_stream_lcp_len(info);
915
916 memset(&iwe, 0, sizeof(iwe));
917 iwe.cmd = SIOCGIWRATE;
918 /* Those two flags are ignored... */
919 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
920
921 for (i = 0; i < ie[1]; i++) {
922 iwe.u.bitrate.value =
923 ((ie[i + 2] & 0x7f) * 500000);
924 p = iwe_stream_add_value(info, current_ev, p,
925 end_buf, &iwe, IW_EV_PARAM_LEN);
926 }
927 current_ev = p;
928 break;
929 }
930 rem -= ie[1] + 2;
931 ie += ie[1] + 2;
932 }
933
934 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
935 || ismesh) {
936 memset(&iwe, 0, sizeof(iwe));
937 iwe.cmd = SIOCGIWMODE;
938 if (ismesh)
939 iwe.u.mode = IW_MODE_MESH;
940 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
941 iwe.u.mode = IW_MODE_MASTER;
942 else
943 iwe.u.mode = IW_MODE_ADHOC;
944 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
945 &iwe, IW_EV_UINT_LEN);
946 }
947
948 buf = kmalloc(30, GFP_ATOMIC);
949 if (buf) {
950 memset(&iwe, 0, sizeof(iwe));
951 iwe.cmd = IWEVCUSTOM;
952 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
953 iwe.u.data.length = strlen(buf);
954 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
955 &iwe, buf);
956 memset(&iwe, 0, sizeof(iwe));
957 iwe.cmd = IWEVCUSTOM;
cb3a8eec
DW
958 sprintf(buf, " Last beacon: %ums ago",
959 elapsed_jiffies_msecs(bss->ts));
2a519311
JB
960 iwe.u.data.length = strlen(buf);
961 current_ev = iwe_stream_add_point(info, current_ev,
962 end_buf, &iwe, buf);
963 kfree(buf);
964 }
965
966 ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
967
968 return current_ev;
969}
970
971
972static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
973 struct iw_request_info *info,
974 char *buf, size_t len)
975{
976 char *current_ev = buf;
977 char *end_buf = buf + len;
978 struct cfg80211_internal_bss *bss;
979
980 spin_lock_bh(&dev->bss_lock);
981 cfg80211_bss_expire(dev);
982
983 list_for_each_entry(bss, &dev->bss_list, list) {
984 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
985 spin_unlock_bh(&dev->bss_lock);
986 return -E2BIG;
987 }
77965c97
JB
988 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
989 current_ev, end_buf);
2a519311
JB
990 }
991 spin_unlock_bh(&dev->bss_lock);
992 return current_ev - buf;
993}
994
995
996int cfg80211_wext_giwscan(struct net_device *dev,
997 struct iw_request_info *info,
998 struct iw_point *data, char *extra)
999{
1000 struct cfg80211_registered_device *rdev;
1001 int res;
1002
1003 if (!netif_running(dev))
1004 return -ENETDOWN;
1005
463d0183 1006 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
2a519311
JB
1007
1008 if (IS_ERR(rdev))
1009 return PTR_ERR(rdev);
1010
1011 if (rdev->scan_req) {
1012 res = -EAGAIN;
1013 goto out;
1014 }
1015
1016 res = ieee80211_scan_results(rdev, info, extra, data->length);
1017 data->length = 0;
1018 if (res >= 0) {
1019 data->length = res;
1020 res = 0;
1021 }
1022
1023 out:
4d0c8aea 1024 cfg80211_unlock_rdev(rdev);
2a519311
JB
1025 return res;
1026}
ba44cb72 1027EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
2a519311 1028#endif
This page took 0.141178 seconds and 5 git commands to generate.