]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/mac80211/scan.c
mac80211: Free current bss information in few places where we don't need it any more
[mv-sheeva.git] / net / mac80211 / scan.c
1 /*
2  * Scanning implementation
3  *
4  * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
5  * Copyright 2004, Instant802 Networks, Inc.
6  * Copyright 2005, Devicescape Software, Inc.
7  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
8  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 /* TODO:
16  * order BSS list by RSSI(?) ("quality of AP")
17  * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
18  *    SSID)
19  */
20
21 #include <linux/wireless.h>
22 #include <linux/if_arp.h>
23 #include <linux/rtnetlink.h>
24 #include <net/mac80211.h>
25 #include <net/iw_handler.h>
26
27 #include "ieee80211_i.h"
28 #include "mesh.h"
29
30 #define IEEE80211_PROBE_DELAY (HZ / 33)
31 #define IEEE80211_CHANNEL_TIME (HZ / 33)
32 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
33
34 void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
35 {
36         spin_lock_init(&local->bss_lock);
37         INIT_LIST_HEAD(&local->bss_list);
38 }
39
40 void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
41 {
42         struct ieee80211_bss *bss, *tmp;
43
44         list_for_each_entry_safe(bss, tmp, &local->bss_list, list)
45                 ieee80211_rx_bss_put(local, bss);
46 }
47
48 struct ieee80211_bss *
49 ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
50                      u8 *ssid, u8 ssid_len)
51 {
52         struct ieee80211_bss *bss;
53
54         spin_lock_bh(&local->bss_lock);
55         bss = local->bss_hash[STA_HASH(bssid)];
56         while (bss) {
57                 if (!bss_mesh_cfg(bss) &&
58                     !memcmp(bss->bssid, bssid, ETH_ALEN) &&
59                     bss->freq == freq &&
60                     bss->ssid_len == ssid_len &&
61                     (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
62                         atomic_inc(&bss->users);
63                         break;
64                 }
65                 bss = bss->hnext;
66         }
67         spin_unlock_bh(&local->bss_lock);
68         return bss;
69 }
70
71 /* Caller must hold local->bss_lock */
72 static void __ieee80211_rx_bss_hash_add(struct ieee80211_local *local,
73                                         struct ieee80211_bss *bss)
74 {
75         u8 hash_idx;
76
77         if (bss_mesh_cfg(bss))
78                 hash_idx = mesh_id_hash(bss_mesh_id(bss),
79                                         bss_mesh_id_len(bss));
80         else
81                 hash_idx = STA_HASH(bss->bssid);
82
83         bss->hnext = local->bss_hash[hash_idx];
84         local->bss_hash[hash_idx] = bss;
85 }
86
87 /* Caller must hold local->bss_lock */
88 static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
89                                         struct ieee80211_bss *bss)
90 {
91         struct ieee80211_bss *b, *prev = NULL;
92         b = local->bss_hash[STA_HASH(bss->bssid)];
93         while (b) {
94                 if (b == bss) {
95                         if (!prev)
96                                 local->bss_hash[STA_HASH(bss->bssid)] =
97                                         bss->hnext;
98                         else
99                                 prev->hnext = bss->hnext;
100                         break;
101                 }
102                 prev = b;
103                 b = b->hnext;
104         }
105 }
106
107 struct ieee80211_bss *
108 ieee80211_rx_bss_add(struct ieee80211_local *local, u8 *bssid, int freq,
109                      u8 *ssid, u8 ssid_len)
110 {
111         struct ieee80211_bss *bss;
112
113         bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
114         if (!bss)
115                 return NULL;
116         atomic_set(&bss->users, 2);
117         memcpy(bss->bssid, bssid, ETH_ALEN);
118         bss->freq = freq;
119         if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
120                 memcpy(bss->ssid, ssid, ssid_len);
121                 bss->ssid_len = ssid_len;
122         }
123
124         spin_lock_bh(&local->bss_lock);
125         /* TODO: order by RSSI? */
126         list_add_tail(&bss->list, &local->bss_list);
127         __ieee80211_rx_bss_hash_add(local, bss);
128         spin_unlock_bh(&local->bss_lock);
129         return bss;
130 }
131
132 #ifdef CONFIG_MAC80211_MESH
133 static struct ieee80211_bss *
134 ieee80211_rx_mesh_bss_get(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
135                           u8 *mesh_cfg, int freq)
136 {
137         struct ieee80211_bss *bss;
138
139         spin_lock_bh(&local->bss_lock);
140         bss = local->bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
141         while (bss) {
142                 if (bss_mesh_cfg(bss) &&
143                     !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
144                     bss->freq == freq &&
145                     mesh_id_len == bss->mesh_id_len &&
146                     (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
147                                                  mesh_id_len))) {
148                         atomic_inc(&bss->users);
149                         break;
150                 }
151                 bss = bss->hnext;
152         }
153         spin_unlock_bh(&local->bss_lock);
154         return bss;
155 }
156
157 static struct ieee80211_bss *
158 ieee80211_rx_mesh_bss_add(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
159                           u8 *mesh_cfg, int mesh_config_len, int freq)
160 {
161         struct ieee80211_bss *bss;
162
163         if (mesh_config_len != IEEE80211_MESH_CONFIG_LEN)
164                 return NULL;
165
166         bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
167         if (!bss)
168                 return NULL;
169
170         bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
171         if (!bss->mesh_cfg) {
172                 kfree(bss);
173                 return NULL;
174         }
175
176         if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
177                 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
178                 if (!bss->mesh_id) {
179                         kfree(bss->mesh_cfg);
180                         kfree(bss);
181                         return NULL;
182                 }
183                 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
184         }
185
186         atomic_set(&bss->users, 2);
187         memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
188         bss->mesh_id_len = mesh_id_len;
189         bss->freq = freq;
190         spin_lock_bh(&local->bss_lock);
191         /* TODO: order by RSSI? */
192         list_add_tail(&bss->list, &local->bss_list);
193         __ieee80211_rx_bss_hash_add(local, bss);
194         spin_unlock_bh(&local->bss_lock);
195         return bss;
196 }
197 #endif
198
199 static void ieee80211_rx_bss_free(struct ieee80211_bss *bss)
200 {
201         kfree(bss->ies);
202         kfree(bss_mesh_id(bss));
203         kfree(bss_mesh_cfg(bss));
204         kfree(bss);
205 }
206
207 void ieee80211_rx_bss_put(struct ieee80211_local *local,
208                           struct ieee80211_bss *bss)
209 {
210         local_bh_disable();
211         if (!atomic_dec_and_lock(&bss->users, &local->bss_lock)) {
212                 local_bh_enable();
213                 return;
214         }
215
216         __ieee80211_rx_bss_hash_del(local, bss);
217         list_del(&bss->list);
218         spin_unlock_bh(&local->bss_lock);
219         ieee80211_rx_bss_free(bss);
220 }
221
222 struct ieee80211_bss *
223 ieee80211_bss_info_update(struct ieee80211_local *local,
224                           struct ieee80211_rx_status *rx_status,
225                           struct ieee80211_mgmt *mgmt,
226                           size_t len,
227                           struct ieee802_11_elems *elems,
228                           int freq, bool beacon)
229 {
230         struct ieee80211_bss *bss;
231         int clen;
232
233 #ifdef CONFIG_MAC80211_MESH
234         if (elems->mesh_config)
235                 bss = ieee80211_rx_mesh_bss_get(local, elems->mesh_id,
236                                 elems->mesh_id_len, elems->mesh_config, freq);
237         else
238 #endif
239                 bss = ieee80211_rx_bss_get(local, mgmt->bssid, freq,
240                                            elems->ssid, elems->ssid_len);
241         if (!bss) {
242 #ifdef CONFIG_MAC80211_MESH
243                 if (elems->mesh_config)
244                         bss = ieee80211_rx_mesh_bss_add(local, elems->mesh_id,
245                                 elems->mesh_id_len, elems->mesh_config,
246                                 elems->mesh_config_len, freq);
247                 else
248 #endif
249                         bss = ieee80211_rx_bss_add(local, mgmt->bssid, freq,
250                                                   elems->ssid, elems->ssid_len);
251                 if (!bss)
252                         return NULL;
253         } else {
254 #if 0
255                 /* TODO: order by RSSI? */
256                 spin_lock_bh(&local->bss_lock);
257                 list_move_tail(&bss->list, &local->bss_list);
258                 spin_unlock_bh(&local->bss_lock);
259 #endif
260         }
261
262         /* save the ERP value so that it is available at association time */
263         if (elems->erp_info && elems->erp_info_len >= 1) {
264                 bss->erp_value = elems->erp_info[0];
265                 bss->has_erp_value = 1;
266         }
267
268         bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
269         bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
270
271         if (elems->tim) {
272                 struct ieee80211_tim_ie *tim_ie =
273                         (struct ieee80211_tim_ie *)elems->tim;
274                 bss->dtim_period = tim_ie->dtim_period;
275         }
276
277         /* set default value for buggy APs */
278         if (!elems->tim || bss->dtim_period == 0)
279                 bss->dtim_period = 1;
280
281         bss->supp_rates_len = 0;
282         if (elems->supp_rates) {
283                 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
284                 if (clen > elems->supp_rates_len)
285                         clen = elems->supp_rates_len;
286                 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
287                        clen);
288                 bss->supp_rates_len += clen;
289         }
290         if (elems->ext_supp_rates) {
291                 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
292                 if (clen > elems->ext_supp_rates_len)
293                         clen = elems->ext_supp_rates_len;
294                 memcpy(&bss->supp_rates[bss->supp_rates_len],
295                        elems->ext_supp_rates, clen);
296                 bss->supp_rates_len += clen;
297         }
298
299         bss->band = rx_status->band;
300
301         bss->timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
302         bss->last_update = jiffies;
303         bss->signal = rx_status->signal;
304         bss->noise = rx_status->noise;
305         bss->qual = rx_status->qual;
306         bss->wmm_used = elems->wmm_param || elems->wmm_info;
307
308         if (!beacon)
309                 bss->last_probe_resp = jiffies;
310
311         /*
312          * For probe responses, or if we don't have any information yet,
313          * use the IEs from the beacon.
314          */
315         if (!bss->ies || !beacon) {
316                 if (bss->ies == NULL || bss->ies_len < elems->total_len) {
317                         kfree(bss->ies);
318                         bss->ies = kmalloc(elems->total_len, GFP_ATOMIC);
319                 }
320                 if (bss->ies) {
321                         memcpy(bss->ies, elems->ie_start, elems->total_len);
322                         bss->ies_len = elems->total_len;
323                 } else
324                         bss->ies_len = 0;
325         }
326
327         return bss;
328 }
329
330 void ieee80211_rx_bss_remove(struct ieee80211_sub_if_data *sdata, u8 *bssid,
331                              int freq, u8 *ssid, u8 ssid_len)
332 {
333         struct ieee80211_bss *bss;
334         struct ieee80211_local *local = sdata->local;
335
336         bss = ieee80211_rx_bss_get(local, bssid, freq, ssid, ssid_len);
337         if (bss) {
338                 atomic_dec(&bss->users);
339                 ieee80211_rx_bss_put(local, bss);
340         }
341 }
342
343 ieee80211_rx_result
344 ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
345                   struct ieee80211_rx_status *rx_status)
346 {
347         struct ieee80211_mgmt *mgmt;
348         struct ieee80211_bss *bss;
349         u8 *elements;
350         struct ieee80211_channel *channel;
351         size_t baselen;
352         int freq;
353         __le16 fc;
354         bool presp, beacon = false;
355         struct ieee802_11_elems elems;
356
357         if (skb->len < 2)
358                 return RX_DROP_UNUSABLE;
359
360         mgmt = (struct ieee80211_mgmt *) skb->data;
361         fc = mgmt->frame_control;
362
363         if (ieee80211_is_ctl(fc))
364                 return RX_CONTINUE;
365
366         if (skb->len < 24)
367                 return RX_DROP_MONITOR;
368
369         presp = ieee80211_is_probe_resp(fc);
370         if (presp) {
371                 /* ignore ProbeResp to foreign address */
372                 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
373                         return RX_DROP_MONITOR;
374
375                 presp = true;
376                 elements = mgmt->u.probe_resp.variable;
377                 baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
378         } else {
379                 beacon = ieee80211_is_beacon(fc);
380                 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
381                 elements = mgmt->u.beacon.variable;
382         }
383
384         if (!presp && !beacon)
385                 return RX_CONTINUE;
386
387         if (baselen > skb->len)
388                 return RX_DROP_MONITOR;
389
390         ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
391
392         if (elems.ds_params && elems.ds_params_len == 1)
393                 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
394         else
395                 freq = rx_status->freq;
396
397         channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
398
399         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
400                 return RX_DROP_MONITOR;
401
402         bss = ieee80211_bss_info_update(sdata->local, rx_status,
403                                         mgmt, skb->len, &elems,
404                                         freq, beacon);
405         if (bss)
406                 ieee80211_rx_bss_put(sdata->local, bss);
407
408         dev_kfree_skb(skb);
409         return RX_QUEUED;
410 }
411
412 void ieee80211_send_nullfunc(struct ieee80211_local *local,
413                                     struct ieee80211_sub_if_data *sdata,
414                                     int powersave)
415 {
416         struct sk_buff *skb;
417         struct ieee80211_hdr *nullfunc;
418         __le16 fc;
419
420         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
421         if (!skb) {
422                 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
423                        "frame\n", sdata->dev->name);
424                 return;
425         }
426         skb_reserve(skb, local->hw.extra_tx_headroom);
427
428         nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
429         memset(nullfunc, 0, 24);
430         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
431                          IEEE80211_FCTL_TODS);
432         if (powersave)
433                 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
434         nullfunc->frame_control = fc;
435         memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
436         memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
437         memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
438
439         ieee80211_tx_skb(sdata, skb, 0);
440 }
441
442 void ieee80211_scan_completed(struct ieee80211_hw *hw)
443 {
444         struct ieee80211_local *local = hw_to_local(hw);
445         struct ieee80211_sub_if_data *sdata;
446         union iwreq_data wrqu;
447
448         if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
449                 return;
450
451         local->last_scan_completed = jiffies;
452         memset(&wrqu, 0, sizeof(wrqu));
453
454         /*
455          * local->scan_sdata could have been NULLed by the interface
456          * down code in case we were scanning on an interface that is
457          * being taken down.
458          */
459         sdata = local->scan_sdata;
460         if (sdata)
461                 wireless_send_event(sdata->dev, SIOCGIWSCAN, &wrqu, NULL);
462
463         if (local->hw_scanning) {
464                 local->hw_scanning = false;
465                 /*
466                  * Somebody might have requested channel change during scan
467                  * that we won't have acted upon, try now. ieee80211_hw_config
468                  * will set the flag based on actual changes.
469                  */
470                 ieee80211_hw_config(local, 0);
471                 goto done;
472         }
473
474         local->sw_scanning = false;
475         ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
476
477         netif_tx_lock_bh(local->mdev);
478         netif_addr_lock(local->mdev);
479         local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
480         local->ops->configure_filter(local_to_hw(local),
481                                      FIF_BCN_PRBRESP_PROMISC,
482                                      &local->filter_flags,
483                                      local->mdev->mc_count,
484                                      local->mdev->mc_list);
485
486         netif_addr_unlock(local->mdev);
487         netif_tx_unlock_bh(local->mdev);
488
489         mutex_lock(&local->iflist_mtx);
490         list_for_each_entry(sdata, &local->interfaces, list) {
491                 if (!netif_running(sdata->dev))
492                         continue;
493
494                 /* Tell AP we're back */
495                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
496                         if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
497                                 ieee80211_send_nullfunc(local, sdata, 0);
498                                 netif_tx_wake_all_queues(sdata->dev);
499                         }
500                 } else
501                         netif_tx_wake_all_queues(sdata->dev);
502
503                 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON_ENABLED);
504         }
505         mutex_unlock(&local->iflist_mtx);
506
507  done:
508         ieee80211_mlme_notify_scan_completed(local);
509         ieee80211_mesh_notify_scan_completed(local);
510 }
511 EXPORT_SYMBOL(ieee80211_scan_completed);
512
513 void ieee80211_scan_work(struct work_struct *work)
514 {
515         struct ieee80211_local *local =
516                 container_of(work, struct ieee80211_local, scan_work.work);
517         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
518         struct ieee80211_supported_band *sband;
519         struct ieee80211_channel *chan;
520         int skip;
521         unsigned long next_delay = 0;
522
523         /*
524          * Avoid re-scheduling when the sdata is going away.
525          */
526         if (!netif_running(sdata->dev))
527                 return;
528
529         switch (local->scan_state) {
530         case SCAN_SET_CHANNEL:
531                 /*
532                  * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
533                  * after we successfully scanned the last channel of the last
534                  * band (and the last band is supported by the hw)
535                  */
536                 if (local->scan_band < IEEE80211_NUM_BANDS)
537                         sband = local->hw.wiphy->bands[local->scan_band];
538                 else
539                         sband = NULL;
540
541                 /*
542                  * If we are at an unsupported band and have more bands
543                  * left to scan, advance to the next supported one.
544                  */
545                 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
546                         local->scan_band++;
547                         sband = local->hw.wiphy->bands[local->scan_band];
548                         local->scan_channel_idx = 0;
549                 }
550
551                 /* if no more bands/channels left, complete scan */
552                 if (!sband || local->scan_channel_idx >= sband->n_channels) {
553                         ieee80211_scan_completed(local_to_hw(local));
554                         return;
555                 }
556                 skip = 0;
557                 chan = &sband->channels[local->scan_channel_idx];
558
559                 if (chan->flags & IEEE80211_CHAN_DISABLED ||
560                     (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
561                      chan->flags & IEEE80211_CHAN_NO_IBSS))
562                         skip = 1;
563
564                 if (!skip) {
565                         local->scan_channel = chan;
566                         if (ieee80211_hw_config(local,
567                                                 IEEE80211_CONF_CHANGE_CHANNEL))
568                                 skip = 1;
569                 }
570
571                 /* advance state machine to next channel/band */
572                 local->scan_channel_idx++;
573                 if (local->scan_channel_idx >= sband->n_channels) {
574                         /*
575                          * scan_band may end up == IEEE80211_NUM_BANDS, but
576                          * we'll catch that case above and complete the scan
577                          * if that is the case.
578                          */
579                         local->scan_band++;
580                         local->scan_channel_idx = 0;
581                 }
582
583                 if (skip)
584                         break;
585
586                 next_delay = IEEE80211_PROBE_DELAY +
587                              usecs_to_jiffies(local->hw.channel_change_time);
588                 local->scan_state = SCAN_SEND_PROBE;
589                 break;
590         case SCAN_SEND_PROBE:
591                 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
592                 local->scan_state = SCAN_SET_CHANNEL;
593
594                 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
595                         break;
596                 ieee80211_send_probe_req(sdata, NULL, local->scan_ssid,
597                                          local->scan_ssid_len);
598                 next_delay = IEEE80211_CHANNEL_TIME;
599                 break;
600         }
601
602         queue_delayed_work(local->hw.workqueue, &local->scan_work,
603                            next_delay);
604 }
605
606
607 int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
608                          u8 *ssid, size_t ssid_len)
609 {
610         struct ieee80211_local *local = scan_sdata->local;
611         struct ieee80211_sub_if_data *sdata;
612
613         if (ssid_len > IEEE80211_MAX_SSID_LEN)
614                 return -EINVAL;
615
616         /* MLME-SCAN.request (page 118)  page 144 (11.1.3.1)
617          * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
618          * BSSID: MACAddress
619          * SSID
620          * ScanType: ACTIVE, PASSIVE
621          * ProbeDelay: delay (in microseconds) to be used prior to transmitting
622          *    a Probe frame during active scanning
623          * ChannelList
624          * MinChannelTime (>= ProbeDelay), in TU
625          * MaxChannelTime: (>= MinChannelTime), in TU
626          */
627
628          /* MLME-SCAN.confirm
629           * BSSDescriptionSet
630           * ResultCode: SUCCESS, INVALID_PARAMETERS
631          */
632
633         if (local->sw_scanning || local->hw_scanning) {
634                 if (local->scan_sdata == scan_sdata)
635                         return 0;
636                 return -EBUSY;
637         }
638
639         if (local->ops->hw_scan) {
640                 int rc;
641
642                 local->hw_scanning = true;
643                 rc = local->ops->hw_scan(local_to_hw(local), ssid, ssid_len);
644                 if (rc) {
645                         local->hw_scanning = false;
646                         return rc;
647                 }
648                 local->scan_sdata = scan_sdata;
649                 return 0;
650         }
651
652         local->sw_scanning = true;
653
654         mutex_lock(&local->iflist_mtx);
655         list_for_each_entry(sdata, &local->interfaces, list) {
656                 if (!netif_running(sdata->dev))
657                         continue;
658
659                 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON_ENABLED);
660
661                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
662                         if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
663                                 netif_tx_stop_all_queues(sdata->dev);
664                                 ieee80211_send_nullfunc(local, sdata, 1);
665                         }
666                 } else
667                         netif_tx_stop_all_queues(sdata->dev);
668         }
669         mutex_unlock(&local->iflist_mtx);
670
671         if (ssid) {
672                 local->scan_ssid_len = ssid_len;
673                 memcpy(local->scan_ssid, ssid, ssid_len);
674         } else
675                 local->scan_ssid_len = 0;
676         local->scan_state = SCAN_SET_CHANNEL;
677         local->scan_channel_idx = 0;
678         local->scan_band = IEEE80211_BAND_2GHZ;
679         local->scan_sdata = scan_sdata;
680
681         netif_addr_lock_bh(local->mdev);
682         local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
683         local->ops->configure_filter(local_to_hw(local),
684                                      FIF_BCN_PRBRESP_PROMISC,
685                                      &local->filter_flags,
686                                      local->mdev->mc_count,
687                                      local->mdev->mc_list);
688         netif_addr_unlock_bh(local->mdev);
689
690         /* TODO: start scan as soon as all nullfunc frames are ACKed */
691         queue_delayed_work(local->hw.workqueue, &local->scan_work,
692                            IEEE80211_CHANNEL_TIME);
693
694         return 0;
695 }
696
697
698 int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
699                            u8 *ssid, size_t ssid_len)
700 {
701         struct ieee80211_local *local = sdata->local;
702         struct ieee80211_if_sta *ifsta;
703
704         if (sdata->vif.type != NL80211_IFTYPE_STATION)
705                 return ieee80211_start_scan(sdata, ssid, ssid_len);
706
707         /*
708          * STA has a state machine that might need to defer scanning
709          * while it's trying to associate/authenticate, therefore we
710          * queue it up to the state machine in that case.
711          */
712
713         if (local->sw_scanning || local->hw_scanning) {
714                 if (local->scan_sdata == sdata)
715                         return 0;
716                 return -EBUSY;
717         }
718
719         ifsta = &sdata->u.sta;
720
721         ifsta->scan_ssid_len = ssid_len;
722         if (ssid_len)
723                 memcpy(ifsta->scan_ssid, ssid, ssid_len);
724         set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
725         queue_work(local->hw.workqueue, &ifsta->work);
726
727         return 0;
728 }
729
730
731 static void ieee80211_scan_add_ies(struct iw_request_info *info,
732                                    struct ieee80211_bss *bss,
733                                    char **current_ev, char *end_buf)
734 {
735         u8 *pos, *end, *next;
736         struct iw_event iwe;
737
738         if (bss == NULL || bss->ies == NULL)
739                 return;
740
741         /*
742          * If needed, fragment the IEs buffer (at IE boundaries) into short
743          * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
744          */
745         pos = bss->ies;
746         end = pos + bss->ies_len;
747
748         while (end - pos > IW_GENERIC_IE_MAX) {
749                 next = pos + 2 + pos[1];
750                 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
751                         next = next + 2 + next[1];
752
753                 memset(&iwe, 0, sizeof(iwe));
754                 iwe.cmd = IWEVGENIE;
755                 iwe.u.data.length = next - pos;
756                 *current_ev = iwe_stream_add_point(info, *current_ev,
757                                                    end_buf, &iwe, pos);
758
759                 pos = next;
760         }
761
762         if (end > pos) {
763                 memset(&iwe, 0, sizeof(iwe));
764                 iwe.cmd = IWEVGENIE;
765                 iwe.u.data.length = end - pos;
766                 *current_ev = iwe_stream_add_point(info, *current_ev,
767                                                    end_buf, &iwe, pos);
768         }
769 }
770
771
772 static char *
773 ieee80211_scan_result(struct ieee80211_local *local,
774                       struct iw_request_info *info,
775                       struct ieee80211_bss *bss,
776                       char *current_ev, char *end_buf)
777 {
778         struct iw_event iwe;
779         char *buf;
780
781         if (time_after(jiffies,
782                        bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
783                 return current_ev;
784
785         memset(&iwe, 0, sizeof(iwe));
786         iwe.cmd = SIOCGIWAP;
787         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
788         memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
789         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
790                                           IW_EV_ADDR_LEN);
791
792         memset(&iwe, 0, sizeof(iwe));
793         iwe.cmd = SIOCGIWESSID;
794         if (bss_mesh_cfg(bss)) {
795                 iwe.u.data.length = bss_mesh_id_len(bss);
796                 iwe.u.data.flags = 1;
797                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
798                                                   &iwe, bss_mesh_id(bss));
799         } else {
800                 iwe.u.data.length = bss->ssid_len;
801                 iwe.u.data.flags = 1;
802                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
803                                                   &iwe, bss->ssid);
804         }
805
806         if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
807             || bss_mesh_cfg(bss)) {
808                 memset(&iwe, 0, sizeof(iwe));
809                 iwe.cmd = SIOCGIWMODE;
810                 if (bss_mesh_cfg(bss))
811                         iwe.u.mode = IW_MODE_MESH;
812                 else if (bss->capability & WLAN_CAPABILITY_ESS)
813                         iwe.u.mode = IW_MODE_MASTER;
814                 else
815                         iwe.u.mode = IW_MODE_ADHOC;
816                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
817                                                   &iwe, IW_EV_UINT_LEN);
818         }
819
820         memset(&iwe, 0, sizeof(iwe));
821         iwe.cmd = SIOCGIWFREQ;
822         iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
823         iwe.u.freq.e = 0;
824         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
825                                           IW_EV_FREQ_LEN);
826
827         memset(&iwe, 0, sizeof(iwe));
828         iwe.cmd = SIOCGIWFREQ;
829         iwe.u.freq.m = bss->freq;
830         iwe.u.freq.e = 6;
831         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
832                                           IW_EV_FREQ_LEN);
833         memset(&iwe, 0, sizeof(iwe));
834         iwe.cmd = IWEVQUAL;
835         iwe.u.qual.qual = bss->qual;
836         iwe.u.qual.level = bss->signal;
837         iwe.u.qual.noise = bss->noise;
838         iwe.u.qual.updated = local->wstats_flags;
839         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
840                                           IW_EV_QUAL_LEN);
841
842         memset(&iwe, 0, sizeof(iwe));
843         iwe.cmd = SIOCGIWENCODE;
844         if (bss->capability & WLAN_CAPABILITY_PRIVACY)
845                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
846         else
847                 iwe.u.data.flags = IW_ENCODE_DISABLED;
848         iwe.u.data.length = 0;
849         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
850                                           &iwe, "");
851
852         ieee80211_scan_add_ies(info, bss, &current_ev, end_buf);
853
854         if (bss->supp_rates_len > 0) {
855                 /* display all supported rates in readable format */
856                 char *p = current_ev + iwe_stream_lcp_len(info);
857                 int i;
858
859                 memset(&iwe, 0, sizeof(iwe));
860                 iwe.cmd = SIOCGIWRATE;
861                 /* Those two flags are ignored... */
862                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
863
864                 for (i = 0; i < bss->supp_rates_len; i++) {
865                         iwe.u.bitrate.value = ((bss->supp_rates[i] &
866                                                         0x7f) * 500000);
867                         p = iwe_stream_add_value(info, current_ev, p,
868                                         end_buf, &iwe, IW_EV_PARAM_LEN);
869                 }
870                 current_ev = p;
871         }
872
873         buf = kmalloc(30, GFP_ATOMIC);
874         if (buf) {
875                 memset(&iwe, 0, sizeof(iwe));
876                 iwe.cmd = IWEVCUSTOM;
877                 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
878                 iwe.u.data.length = strlen(buf);
879                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
880                                                   &iwe, buf);
881                 memset(&iwe, 0, sizeof(iwe));
882                 iwe.cmd = IWEVCUSTOM;
883                 sprintf(buf, " Last beacon: %dms ago",
884                         jiffies_to_msecs(jiffies - bss->last_update));
885                 iwe.u.data.length = strlen(buf);
886                 current_ev = iwe_stream_add_point(info, current_ev,
887                                                   end_buf, &iwe, buf);
888                 kfree(buf);
889         }
890
891         if (bss_mesh_cfg(bss)) {
892                 u8 *cfg = bss_mesh_cfg(bss);
893                 buf = kmalloc(50, GFP_ATOMIC);
894                 if (buf) {
895                         memset(&iwe, 0, sizeof(iwe));
896                         iwe.cmd = IWEVCUSTOM;
897                         sprintf(buf, "Mesh network (version %d)", cfg[0]);
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, "Path Selection Protocol ID: "
903                                 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
904                                                         cfg[4]);
905                         iwe.u.data.length = strlen(buf);
906                         current_ev = iwe_stream_add_point(info, current_ev,
907                                                           end_buf,
908                                                           &iwe, buf);
909                         sprintf(buf, "Path Selection Metric ID: "
910                                 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
911                                                         cfg[8]);
912                         iwe.u.data.length = strlen(buf);
913                         current_ev = iwe_stream_add_point(info, current_ev,
914                                                           end_buf,
915                                                           &iwe, buf);
916                         sprintf(buf, "Congestion Control Mode ID: "
917                                 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
918                                                         cfg[11], cfg[12]);
919                         iwe.u.data.length = strlen(buf);
920                         current_ev = iwe_stream_add_point(info, current_ev,
921                                                           end_buf,
922                                                           &iwe, buf);
923                         sprintf(buf, "Channel Precedence: "
924                                 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
925                                                         cfg[15], cfg[16]);
926                         iwe.u.data.length = strlen(buf);
927                         current_ev = iwe_stream_add_point(info, current_ev,
928                                                           end_buf,
929                                                           &iwe, buf);
930                         kfree(buf);
931                 }
932         }
933
934         return current_ev;
935 }
936
937
938 int ieee80211_scan_results(struct ieee80211_local *local,
939                            struct iw_request_info *info,
940                            char *buf, size_t len)
941 {
942         char *current_ev = buf;
943         char *end_buf = buf + len;
944         struct ieee80211_bss *bss;
945
946         spin_lock_bh(&local->bss_lock);
947         list_for_each_entry(bss, &local->bss_list, list) {
948                 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
949                         spin_unlock_bh(&local->bss_lock);
950                         return -E2BIG;
951                 }
952                 current_ev = ieee80211_scan_result(local, info, bss,
953                                                        current_ev, end_buf);
954         }
955         spin_unlock_bh(&local->bss_lock);
956         return current_ev - buf;
957 }