mac80211: don't try to powersave/config disabled interfaces
[safe/jmp/linux-2.6] / 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 ieee80211_rx_result
331 ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
332                   struct ieee80211_rx_status *rx_status)
333 {
334         struct ieee80211_mgmt *mgmt;
335         struct ieee80211_bss *bss;
336         u8 *elements;
337         struct ieee80211_channel *channel;
338         size_t baselen;
339         int freq;
340         __le16 fc;
341         bool presp, beacon = false;
342         struct ieee802_11_elems elems;
343
344         if (skb->len < 2)
345                 return RX_DROP_UNUSABLE;
346
347         mgmt = (struct ieee80211_mgmt *) skb->data;
348         fc = mgmt->frame_control;
349
350         if (ieee80211_is_ctl(fc))
351                 return RX_CONTINUE;
352
353         if (skb->len < 24)
354                 return RX_DROP_MONITOR;
355
356         presp = ieee80211_is_probe_resp(fc);
357         if (presp) {
358                 /* ignore ProbeResp to foreign address */
359                 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
360                         return RX_DROP_MONITOR;
361
362                 presp = true;
363                 elements = mgmt->u.probe_resp.variable;
364                 baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
365         } else {
366                 beacon = ieee80211_is_beacon(fc);
367                 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
368                 elements = mgmt->u.beacon.variable;
369         }
370
371         if (!presp && !beacon)
372                 return RX_CONTINUE;
373
374         if (baselen > skb->len)
375                 return RX_DROP_MONITOR;
376
377         ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
378
379         if (elems.ds_params && elems.ds_params_len == 1)
380                 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
381         else
382                 freq = rx_status->freq;
383
384         channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
385
386         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
387                 return RX_DROP_MONITOR;
388
389         bss = ieee80211_bss_info_update(sdata->local, rx_status,
390                                         mgmt, skb->len, &elems,
391                                         freq, beacon);
392         if (bss)
393                 ieee80211_rx_bss_put(sdata->local, bss);
394
395         dev_kfree_skb(skb);
396         return RX_QUEUED;
397 }
398
399 void ieee80211_send_nullfunc(struct ieee80211_local *local,
400                                     struct ieee80211_sub_if_data *sdata,
401                                     int powersave)
402 {
403         struct sk_buff *skb;
404         struct ieee80211_hdr *nullfunc;
405         __le16 fc;
406
407         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
408         if (!skb) {
409                 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
410                        "frame\n", sdata->dev->name);
411                 return;
412         }
413         skb_reserve(skb, local->hw.extra_tx_headroom);
414
415         nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
416         memset(nullfunc, 0, 24);
417         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
418                          IEEE80211_FCTL_TODS);
419         if (powersave)
420                 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
421         nullfunc->frame_control = fc;
422         memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
423         memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
424         memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
425
426         ieee80211_tx_skb(sdata, skb, 0);
427 }
428
429 void ieee80211_scan_completed(struct ieee80211_hw *hw)
430 {
431         struct ieee80211_local *local = hw_to_local(hw);
432         struct ieee80211_sub_if_data *sdata;
433         union iwreq_data wrqu;
434
435         if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
436                 return;
437
438         local->last_scan_completed = jiffies;
439         memset(&wrqu, 0, sizeof(wrqu));
440
441         /*
442          * local->scan_sdata could have been NULLed by the interface
443          * down code in case we were scanning on an interface that is
444          * being taken down.
445          */
446         sdata = local->scan_sdata;
447         if (sdata)
448                 wireless_send_event(sdata->dev, SIOCGIWSCAN, &wrqu, NULL);
449
450         if (local->hw_scanning) {
451                 local->hw_scanning = false;
452                 /*
453                  * Somebody might have requested channel change during scan
454                  * that we won't have acted upon, try now. ieee80211_hw_config
455                  * will set the flag based on actual changes.
456                  */
457                 ieee80211_hw_config(local, 0);
458                 goto done;
459         }
460
461         local->sw_scanning = false;
462         ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
463
464         netif_tx_lock_bh(local->mdev);
465         netif_addr_lock(local->mdev);
466         local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
467         local->ops->configure_filter(local_to_hw(local),
468                                      FIF_BCN_PRBRESP_PROMISC,
469                                      &local->filter_flags,
470                                      local->mdev->mc_count,
471                                      local->mdev->mc_list);
472
473         netif_addr_unlock(local->mdev);
474         netif_tx_unlock_bh(local->mdev);
475
476         mutex_lock(&local->iflist_mtx);
477         list_for_each_entry(sdata, &local->interfaces, list) {
478                 if (!netif_running(sdata->dev))
479                         continue;
480
481                 /* Tell AP we're back */
482                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
483                         if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
484                                 ieee80211_send_nullfunc(local, sdata, 0);
485                                 netif_tx_wake_all_queues(sdata->dev);
486                         }
487                 } else
488                         netif_tx_wake_all_queues(sdata->dev);
489
490                 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON_ENABLED);
491         }
492         mutex_unlock(&local->iflist_mtx);
493
494  done:
495         ieee80211_mlme_notify_scan_completed(local);
496         ieee80211_mesh_notify_scan_completed(local);
497 }
498 EXPORT_SYMBOL(ieee80211_scan_completed);
499
500 void ieee80211_scan_work(struct work_struct *work)
501 {
502         struct ieee80211_local *local =
503                 container_of(work, struct ieee80211_local, scan_work.work);
504         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
505         struct ieee80211_supported_band *sband;
506         struct ieee80211_channel *chan;
507         int skip;
508         unsigned long next_delay = 0;
509
510         /*
511          * Avoid re-scheduling when the sdata is going away.
512          */
513         if (!netif_running(sdata->dev))
514                 return;
515
516         switch (local->scan_state) {
517         case SCAN_SET_CHANNEL:
518                 /*
519                  * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
520                  * after we successfully scanned the last channel of the last
521                  * band (and the last band is supported by the hw)
522                  */
523                 if (local->scan_band < IEEE80211_NUM_BANDS)
524                         sband = local->hw.wiphy->bands[local->scan_band];
525                 else
526                         sband = NULL;
527
528                 /*
529                  * If we are at an unsupported band and have more bands
530                  * left to scan, advance to the next supported one.
531                  */
532                 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
533                         local->scan_band++;
534                         sband = local->hw.wiphy->bands[local->scan_band];
535                         local->scan_channel_idx = 0;
536                 }
537
538                 /* if no more bands/channels left, complete scan */
539                 if (!sband || local->scan_channel_idx >= sband->n_channels) {
540                         ieee80211_scan_completed(local_to_hw(local));
541                         return;
542                 }
543                 skip = 0;
544                 chan = &sband->channels[local->scan_channel_idx];
545
546                 if (chan->flags & IEEE80211_CHAN_DISABLED ||
547                     (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
548                      chan->flags & IEEE80211_CHAN_NO_IBSS))
549                         skip = 1;
550
551                 if (!skip) {
552                         local->scan_channel = chan;
553                         if (ieee80211_hw_config(local,
554                                                 IEEE80211_CONF_CHANGE_CHANNEL))
555                                 skip = 1;
556                 }
557
558                 /* advance state machine to next channel/band */
559                 local->scan_channel_idx++;
560                 if (local->scan_channel_idx >= sband->n_channels) {
561                         /*
562                          * scan_band may end up == IEEE80211_NUM_BANDS, but
563                          * we'll catch that case above and complete the scan
564                          * if that is the case.
565                          */
566                         local->scan_band++;
567                         local->scan_channel_idx = 0;
568                 }
569
570                 if (skip)
571                         break;
572
573                 next_delay = IEEE80211_PROBE_DELAY +
574                              usecs_to_jiffies(local->hw.channel_change_time);
575                 local->scan_state = SCAN_SEND_PROBE;
576                 break;
577         case SCAN_SEND_PROBE:
578                 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
579                 local->scan_state = SCAN_SET_CHANNEL;
580
581                 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
582                         break;
583                 ieee80211_send_probe_req(sdata, NULL, local->scan_ssid,
584                                          local->scan_ssid_len);
585                 next_delay = IEEE80211_CHANNEL_TIME;
586                 break;
587         }
588
589         queue_delayed_work(local->hw.workqueue, &local->scan_work,
590                            next_delay);
591 }
592
593
594 int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
595                          u8 *ssid, size_t ssid_len)
596 {
597         struct ieee80211_local *local = scan_sdata->local;
598         struct ieee80211_sub_if_data *sdata;
599
600         if (ssid_len > IEEE80211_MAX_SSID_LEN)
601                 return -EINVAL;
602
603         /* MLME-SCAN.request (page 118)  page 144 (11.1.3.1)
604          * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
605          * BSSID: MACAddress
606          * SSID
607          * ScanType: ACTIVE, PASSIVE
608          * ProbeDelay: delay (in microseconds) to be used prior to transmitting
609          *    a Probe frame during active scanning
610          * ChannelList
611          * MinChannelTime (>= ProbeDelay), in TU
612          * MaxChannelTime: (>= MinChannelTime), in TU
613          */
614
615          /* MLME-SCAN.confirm
616           * BSSDescriptionSet
617           * ResultCode: SUCCESS, INVALID_PARAMETERS
618          */
619
620         if (local->sw_scanning || local->hw_scanning) {
621                 if (local->scan_sdata == scan_sdata)
622                         return 0;
623                 return -EBUSY;
624         }
625
626         if (local->ops->hw_scan) {
627                 int rc;
628
629                 local->hw_scanning = true;
630                 rc = local->ops->hw_scan(local_to_hw(local), ssid, ssid_len);
631                 if (rc) {
632                         local->hw_scanning = false;
633                         return rc;
634                 }
635                 local->scan_sdata = scan_sdata;
636                 return 0;
637         }
638
639         local->sw_scanning = true;
640
641         mutex_lock(&local->iflist_mtx);
642         list_for_each_entry(sdata, &local->interfaces, list) {
643                 if (!netif_running(sdata->dev))
644                         continue;
645
646                 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON_ENABLED);
647
648                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
649                         if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
650                                 netif_tx_stop_all_queues(sdata->dev);
651                                 ieee80211_send_nullfunc(local, sdata, 1);
652                         }
653                 } else
654                         netif_tx_stop_all_queues(sdata->dev);
655         }
656         mutex_unlock(&local->iflist_mtx);
657
658         if (ssid) {
659                 local->scan_ssid_len = ssid_len;
660                 memcpy(local->scan_ssid, ssid, ssid_len);
661         } else
662                 local->scan_ssid_len = 0;
663         local->scan_state = SCAN_SET_CHANNEL;
664         local->scan_channel_idx = 0;
665         local->scan_band = IEEE80211_BAND_2GHZ;
666         local->scan_sdata = scan_sdata;
667
668         netif_addr_lock_bh(local->mdev);
669         local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
670         local->ops->configure_filter(local_to_hw(local),
671                                      FIF_BCN_PRBRESP_PROMISC,
672                                      &local->filter_flags,
673                                      local->mdev->mc_count,
674                                      local->mdev->mc_list);
675         netif_addr_unlock_bh(local->mdev);
676
677         /* TODO: start scan as soon as all nullfunc frames are ACKed */
678         queue_delayed_work(local->hw.workqueue, &local->scan_work,
679                            IEEE80211_CHANNEL_TIME);
680
681         return 0;
682 }
683
684
685 int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
686                            u8 *ssid, size_t ssid_len)
687 {
688         struct ieee80211_local *local = sdata->local;
689         struct ieee80211_if_sta *ifsta;
690
691         if (sdata->vif.type != NL80211_IFTYPE_STATION)
692                 return ieee80211_start_scan(sdata, ssid, ssid_len);
693
694         /*
695          * STA has a state machine that might need to defer scanning
696          * while it's trying to associate/authenticate, therefore we
697          * queue it up to the state machine in that case.
698          */
699
700         if (local->sw_scanning || local->hw_scanning) {
701                 if (local->scan_sdata == sdata)
702                         return 0;
703                 return -EBUSY;
704         }
705
706         ifsta = &sdata->u.sta;
707
708         ifsta->scan_ssid_len = ssid_len;
709         if (ssid_len)
710                 memcpy(ifsta->scan_ssid, ssid, ssid_len);
711         set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
712         queue_work(local->hw.workqueue, &ifsta->work);
713
714         return 0;
715 }
716
717
718 static void ieee80211_scan_add_ies(struct iw_request_info *info,
719                                    struct ieee80211_bss *bss,
720                                    char **current_ev, char *end_buf)
721 {
722         u8 *pos, *end, *next;
723         struct iw_event iwe;
724
725         if (bss == NULL || bss->ies == NULL)
726                 return;
727
728         /*
729          * If needed, fragment the IEs buffer (at IE boundaries) into short
730          * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
731          */
732         pos = bss->ies;
733         end = pos + bss->ies_len;
734
735         while (end - pos > IW_GENERIC_IE_MAX) {
736                 next = pos + 2 + pos[1];
737                 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
738                         next = next + 2 + next[1];
739
740                 memset(&iwe, 0, sizeof(iwe));
741                 iwe.cmd = IWEVGENIE;
742                 iwe.u.data.length = next - pos;
743                 *current_ev = iwe_stream_add_point(info, *current_ev,
744                                                    end_buf, &iwe, pos);
745
746                 pos = next;
747         }
748
749         if (end > pos) {
750                 memset(&iwe, 0, sizeof(iwe));
751                 iwe.cmd = IWEVGENIE;
752                 iwe.u.data.length = end - pos;
753                 *current_ev = iwe_stream_add_point(info, *current_ev,
754                                                    end_buf, &iwe, pos);
755         }
756 }
757
758
759 static char *
760 ieee80211_scan_result(struct ieee80211_local *local,
761                       struct iw_request_info *info,
762                       struct ieee80211_bss *bss,
763                       char *current_ev, char *end_buf)
764 {
765         struct iw_event iwe;
766         char *buf;
767
768         if (time_after(jiffies,
769                        bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
770                 return current_ev;
771
772         memset(&iwe, 0, sizeof(iwe));
773         iwe.cmd = SIOCGIWAP;
774         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
775         memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
776         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
777                                           IW_EV_ADDR_LEN);
778
779         memset(&iwe, 0, sizeof(iwe));
780         iwe.cmd = SIOCGIWESSID;
781         if (bss_mesh_cfg(bss)) {
782                 iwe.u.data.length = bss_mesh_id_len(bss);
783                 iwe.u.data.flags = 1;
784                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
785                                                   &iwe, bss_mesh_id(bss));
786         } else {
787                 iwe.u.data.length = bss->ssid_len;
788                 iwe.u.data.flags = 1;
789                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
790                                                   &iwe, bss->ssid);
791         }
792
793         if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
794             || bss_mesh_cfg(bss)) {
795                 memset(&iwe, 0, sizeof(iwe));
796                 iwe.cmd = SIOCGIWMODE;
797                 if (bss_mesh_cfg(bss))
798                         iwe.u.mode = IW_MODE_MESH;
799                 else if (bss->capability & WLAN_CAPABILITY_ESS)
800                         iwe.u.mode = IW_MODE_MASTER;
801                 else
802                         iwe.u.mode = IW_MODE_ADHOC;
803                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
804                                                   &iwe, IW_EV_UINT_LEN);
805         }
806
807         memset(&iwe, 0, sizeof(iwe));
808         iwe.cmd = SIOCGIWFREQ;
809         iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
810         iwe.u.freq.e = 0;
811         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
812                                           IW_EV_FREQ_LEN);
813
814         memset(&iwe, 0, sizeof(iwe));
815         iwe.cmd = SIOCGIWFREQ;
816         iwe.u.freq.m = bss->freq;
817         iwe.u.freq.e = 6;
818         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
819                                           IW_EV_FREQ_LEN);
820         memset(&iwe, 0, sizeof(iwe));
821         iwe.cmd = IWEVQUAL;
822         iwe.u.qual.qual = bss->qual;
823         iwe.u.qual.level = bss->signal;
824         iwe.u.qual.noise = bss->noise;
825         iwe.u.qual.updated = local->wstats_flags;
826         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
827                                           IW_EV_QUAL_LEN);
828
829         memset(&iwe, 0, sizeof(iwe));
830         iwe.cmd = SIOCGIWENCODE;
831         if (bss->capability & WLAN_CAPABILITY_PRIVACY)
832                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
833         else
834                 iwe.u.data.flags = IW_ENCODE_DISABLED;
835         iwe.u.data.length = 0;
836         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
837                                           &iwe, "");
838
839         ieee80211_scan_add_ies(info, bss, &current_ev, end_buf);
840
841         if (bss->supp_rates_len > 0) {
842                 /* display all supported rates in readable format */
843                 char *p = current_ev + iwe_stream_lcp_len(info);
844                 int i;
845
846                 memset(&iwe, 0, sizeof(iwe));
847                 iwe.cmd = SIOCGIWRATE;
848                 /* Those two flags are ignored... */
849                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
850
851                 for (i = 0; i < bss->supp_rates_len; i++) {
852                         iwe.u.bitrate.value = ((bss->supp_rates[i] &
853                                                         0x7f) * 500000);
854                         p = iwe_stream_add_value(info, current_ev, p,
855                                         end_buf, &iwe, IW_EV_PARAM_LEN);
856                 }
857                 current_ev = p;
858         }
859
860         buf = kmalloc(30, GFP_ATOMIC);
861         if (buf) {
862                 memset(&iwe, 0, sizeof(iwe));
863                 iwe.cmd = IWEVCUSTOM;
864                 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
865                 iwe.u.data.length = strlen(buf);
866                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
867                                                   &iwe, buf);
868                 memset(&iwe, 0, sizeof(iwe));
869                 iwe.cmd = IWEVCUSTOM;
870                 sprintf(buf, " Last beacon: %dms ago",
871                         jiffies_to_msecs(jiffies - bss->last_update));
872                 iwe.u.data.length = strlen(buf);
873                 current_ev = iwe_stream_add_point(info, current_ev,
874                                                   end_buf, &iwe, buf);
875                 kfree(buf);
876         }
877
878         if (bss_mesh_cfg(bss)) {
879                 u8 *cfg = bss_mesh_cfg(bss);
880                 buf = kmalloc(50, GFP_ATOMIC);
881                 if (buf) {
882                         memset(&iwe, 0, sizeof(iwe));
883                         iwe.cmd = IWEVCUSTOM;
884                         sprintf(buf, "Mesh network (version %d)", cfg[0]);
885                         iwe.u.data.length = strlen(buf);
886                         current_ev = iwe_stream_add_point(info, current_ev,
887                                                           end_buf,
888                                                           &iwe, buf);
889                         sprintf(buf, "Path Selection Protocol ID: "
890                                 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
891                                                         cfg[4]);
892                         iwe.u.data.length = strlen(buf);
893                         current_ev = iwe_stream_add_point(info, current_ev,
894                                                           end_buf,
895                                                           &iwe, buf);
896                         sprintf(buf, "Path Selection Metric ID: "
897                                 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
898                                                         cfg[8]);
899                         iwe.u.data.length = strlen(buf);
900                         current_ev = iwe_stream_add_point(info, current_ev,
901                                                           end_buf,
902                                                           &iwe, buf);
903                         sprintf(buf, "Congestion Control Mode ID: "
904                                 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
905                                                         cfg[11], cfg[12]);
906                         iwe.u.data.length = strlen(buf);
907                         current_ev = iwe_stream_add_point(info, current_ev,
908                                                           end_buf,
909                                                           &iwe, buf);
910                         sprintf(buf, "Channel Precedence: "
911                                 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
912                                                         cfg[15], cfg[16]);
913                         iwe.u.data.length = strlen(buf);
914                         current_ev = iwe_stream_add_point(info, current_ev,
915                                                           end_buf,
916                                                           &iwe, buf);
917                         kfree(buf);
918                 }
919         }
920
921         return current_ev;
922 }
923
924
925 int ieee80211_scan_results(struct ieee80211_local *local,
926                            struct iw_request_info *info,
927                            char *buf, size_t len)
928 {
929         char *current_ev = buf;
930         char *end_buf = buf + len;
931         struct ieee80211_bss *bss;
932
933         spin_lock_bh(&local->bss_lock);
934         list_for_each_entry(bss, &local->bss_list, list) {
935                 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
936                         spin_unlock_bh(&local->bss_lock);
937                         return -E2BIG;
938                 }
939                 current_ev = ieee80211_scan_result(local, info, bss,
940                                                        current_ev, end_buf);
941         }
942         spin_unlock_bh(&local->bss_lock);
943         return current_ev - buf;
944 }