mac80211: start moving scan code from mlme
[safe/jmp/linux-2.6] / net / mac80211 / scan.c
1 /*
2  * BSS client mode implementation
3  * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4  * Copyright 2004, Instant802 Networks, Inc.
5  * Copyright 2005, Devicescape Software, Inc.
6  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
7  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/wireless.h>
15 #include <linux/if_arp.h>
16 #include <net/mac80211.h>
17 #include <net/iw_handler.h>
18
19 #include "ieee80211_i.h"
20
21 #define IEEE80211_PROBE_DELAY (HZ / 33)
22 #define IEEE80211_CHANNEL_TIME (HZ / 33)
23 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
24
25
26 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
27                                     struct ieee80211_sub_if_data *sdata,
28                                     int powersave)
29 {
30         struct sk_buff *skb;
31         struct ieee80211_hdr *nullfunc;
32         __le16 fc;
33
34         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
35         if (!skb) {
36                 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
37                        "frame\n", sdata->dev->name);
38                 return;
39         }
40         skb_reserve(skb, local->hw.extra_tx_headroom);
41
42         nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
43         memset(nullfunc, 0, 24);
44         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
45                          IEEE80211_FCTL_TODS);
46         if (powersave)
47                 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
48         nullfunc->frame_control = fc;
49         memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
50         memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
51         memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
52
53         ieee80211_sta_tx(sdata, skb, 0);
54 }
55
56 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
57 {
58         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
59             ieee80211_vif_is_mesh(&sdata->vif))
60                 ieee80211_sta_timer((unsigned long)sdata);
61 }
62
63 void ieee80211_scan_completed(struct ieee80211_hw *hw)
64 {
65         struct ieee80211_local *local = hw_to_local(hw);
66         struct ieee80211_sub_if_data *sdata;
67         union iwreq_data wrqu;
68
69         local->last_scan_completed = jiffies;
70         memset(&wrqu, 0, sizeof(wrqu));
71         wireless_send_event(local->scan_sdata->dev, SIOCGIWSCAN, &wrqu, NULL);
72
73         if (local->sta_hw_scanning) {
74                 local->sta_hw_scanning = 0;
75                 if (ieee80211_hw_config(local))
76                         printk(KERN_DEBUG "%s: failed to restore operational "
77                                "channel after scan\n", wiphy_name(local->hw.wiphy));
78                 /* Restart STA timer for HW scan case */
79                 rcu_read_lock();
80                 list_for_each_entry_rcu(sdata, &local->interfaces, list)
81                         ieee80211_restart_sta_timer(sdata);
82                 rcu_read_unlock();
83
84                 goto done;
85         }
86
87         local->sta_sw_scanning = 0;
88         if (ieee80211_hw_config(local))
89                 printk(KERN_DEBUG "%s: failed to restore operational "
90                        "channel after scan\n", wiphy_name(local->hw.wiphy));
91
92
93         netif_tx_lock_bh(local->mdev);
94         netif_addr_lock(local->mdev);
95         local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
96         local->ops->configure_filter(local_to_hw(local),
97                                      FIF_BCN_PRBRESP_PROMISC,
98                                      &local->filter_flags,
99                                      local->mdev->mc_count,
100                                      local->mdev->mc_list);
101
102         netif_addr_unlock(local->mdev);
103         netif_tx_unlock_bh(local->mdev);
104
105         rcu_read_lock();
106         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
107                 /* Tell AP we're back */
108                 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
109                         if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
110                                 ieee80211_send_nullfunc(local, sdata, 0);
111                                 netif_tx_wake_all_queues(sdata->dev);
112                         }
113                 } else
114                         netif_tx_wake_all_queues(sdata->dev);
115
116                 ieee80211_restart_sta_timer(sdata);
117         }
118         rcu_read_unlock();
119
120  done:
121         ieee80211_mlme_notify_scan_completed(local);
122 }
123 EXPORT_SYMBOL(ieee80211_scan_completed);
124
125
126 void ieee80211_sta_scan_work(struct work_struct *work)
127 {
128         struct ieee80211_local *local =
129                 container_of(work, struct ieee80211_local, scan_work.work);
130         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
131         struct ieee80211_supported_band *sband;
132         struct ieee80211_channel *chan;
133         int skip;
134         unsigned long next_delay = 0;
135
136         if (!local->sta_sw_scanning)
137                 return;
138
139         switch (local->scan_state) {
140         case SCAN_SET_CHANNEL:
141                 /*
142                  * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
143                  * after we successfully scanned the last channel of the last
144                  * band (and the last band is supported by the hw)
145                  */
146                 if (local->scan_band < IEEE80211_NUM_BANDS)
147                         sband = local->hw.wiphy->bands[local->scan_band];
148                 else
149                         sband = NULL;
150
151                 /*
152                  * If we are at an unsupported band and have more bands
153                  * left to scan, advance to the next supported one.
154                  */
155                 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
156                         local->scan_band++;
157                         sband = local->hw.wiphy->bands[local->scan_band];
158                         local->scan_channel_idx = 0;
159                 }
160
161                 /* if no more bands/channels left, complete scan */
162                 if (!sband || local->scan_channel_idx >= sband->n_channels) {
163                         ieee80211_scan_completed(local_to_hw(local));
164                         return;
165                 }
166                 skip = 0;
167                 chan = &sband->channels[local->scan_channel_idx];
168
169                 if (chan->flags & IEEE80211_CHAN_DISABLED ||
170                     (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
171                      chan->flags & IEEE80211_CHAN_NO_IBSS))
172                         skip = 1;
173
174                 if (!skip) {
175                         local->scan_channel = chan;
176                         if (ieee80211_hw_config(local)) {
177                                 printk(KERN_DEBUG "%s: failed to set freq to "
178                                        "%d MHz for scan\n", wiphy_name(local->hw.wiphy),
179                                        chan->center_freq);
180                                 skip = 1;
181                         }
182                 }
183
184                 /* advance state machine to next channel/band */
185                 local->scan_channel_idx++;
186                 if (local->scan_channel_idx >= sband->n_channels) {
187                         /*
188                          * scan_band may end up == IEEE80211_NUM_BANDS, but
189                          * we'll catch that case above and complete the scan
190                          * if that is the case.
191                          */
192                         local->scan_band++;
193                         local->scan_channel_idx = 0;
194                 }
195
196                 if (skip)
197                         break;
198
199                 next_delay = IEEE80211_PROBE_DELAY +
200                              usecs_to_jiffies(local->hw.channel_change_time);
201                 local->scan_state = SCAN_SEND_PROBE;
202                 break;
203         case SCAN_SEND_PROBE:
204                 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
205                 local->scan_state = SCAN_SET_CHANNEL;
206
207                 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
208                         break;
209                 ieee80211_send_probe_req(sdata, NULL, local->scan_ssid,
210                                          local->scan_ssid_len);
211                 next_delay = IEEE80211_CHANNEL_TIME;
212                 break;
213         }
214
215         if (local->sta_sw_scanning)
216                 queue_delayed_work(local->hw.workqueue, &local->scan_work,
217                                    next_delay);
218 }
219
220
221 int ieee80211_sta_start_scan(struct ieee80211_sub_if_data *scan_sdata,
222                              u8 *ssid, size_t ssid_len)
223 {
224         struct ieee80211_local *local = scan_sdata->local;
225         struct ieee80211_sub_if_data *sdata;
226
227         if (ssid_len > IEEE80211_MAX_SSID_LEN)
228                 return -EINVAL;
229
230         /* MLME-SCAN.request (page 118)  page 144 (11.1.3.1)
231          * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
232          * BSSID: MACAddress
233          * SSID
234          * ScanType: ACTIVE, PASSIVE
235          * ProbeDelay: delay (in microseconds) to be used prior to transmitting
236          *    a Probe frame during active scanning
237          * ChannelList
238          * MinChannelTime (>= ProbeDelay), in TU
239          * MaxChannelTime: (>= MinChannelTime), in TU
240          */
241
242          /* MLME-SCAN.confirm
243           * BSSDescriptionSet
244           * ResultCode: SUCCESS, INVALID_PARAMETERS
245          */
246
247         if (local->sta_sw_scanning || local->sta_hw_scanning) {
248                 if (local->scan_sdata == scan_sdata)
249                         return 0;
250                 return -EBUSY;
251         }
252
253         if (local->ops->hw_scan) {
254                 int rc = local->ops->hw_scan(local_to_hw(local),
255                                              ssid, ssid_len);
256                 if (!rc) {
257                         local->sta_hw_scanning = 1;
258                         local->scan_sdata = scan_sdata;
259                 }
260                 return rc;
261         }
262
263         local->sta_sw_scanning = 1;
264
265         rcu_read_lock();
266         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
267                 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
268                         if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
269                                 netif_tx_stop_all_queues(sdata->dev);
270                                 ieee80211_send_nullfunc(local, sdata, 1);
271                         }
272                 } else
273                         netif_tx_stop_all_queues(sdata->dev);
274         }
275         rcu_read_unlock();
276
277         if (ssid) {
278                 local->scan_ssid_len = ssid_len;
279                 memcpy(local->scan_ssid, ssid, ssid_len);
280         } else
281                 local->scan_ssid_len = 0;
282         local->scan_state = SCAN_SET_CHANNEL;
283         local->scan_channel_idx = 0;
284         local->scan_band = IEEE80211_BAND_2GHZ;
285         local->scan_sdata = scan_sdata;
286
287         netif_addr_lock_bh(local->mdev);
288         local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
289         local->ops->configure_filter(local_to_hw(local),
290                                      FIF_BCN_PRBRESP_PROMISC,
291                                      &local->filter_flags,
292                                      local->mdev->mc_count,
293                                      local->mdev->mc_list);
294         netif_addr_unlock_bh(local->mdev);
295
296         /* TODO: start scan as soon as all nullfunc frames are ACKed */
297         queue_delayed_work(local->hw.workqueue, &local->scan_work,
298                            IEEE80211_CHANNEL_TIME);
299
300         return 0;
301 }
302
303
304 int ieee80211_sta_req_scan(struct ieee80211_sub_if_data *sdata, u8 *ssid, size_t ssid_len)
305 {
306         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
307         struct ieee80211_local *local = sdata->local;
308
309         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
310                 return ieee80211_sta_start_scan(sdata, ssid, ssid_len);
311
312         if (local->sta_sw_scanning || local->sta_hw_scanning) {
313                 if (local->scan_sdata == sdata)
314                         return 0;
315                 return -EBUSY;
316         }
317
318         ifsta->scan_ssid_len = ssid_len;
319         if (ssid_len)
320                 memcpy(ifsta->scan_ssid, ssid, ssid_len);
321         set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
322         queue_work(local->hw.workqueue, &ifsta->work);
323         return 0;
324 }
325
326
327 static void ieee80211_sta_add_scan_ies(struct iw_request_info *info,
328                                        struct ieee80211_sta_bss *bss,
329                                        char **current_ev, char *end_buf)
330 {
331         u8 *pos, *end, *next;
332         struct iw_event iwe;
333
334         if (bss == NULL || bss->ies == NULL)
335                 return;
336
337         /*
338          * If needed, fragment the IEs buffer (at IE boundaries) into short
339          * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
340          */
341         pos = bss->ies;
342         end = pos + bss->ies_len;
343
344         while (end - pos > IW_GENERIC_IE_MAX) {
345                 next = pos + 2 + pos[1];
346                 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
347                         next = next + 2 + next[1];
348
349                 memset(&iwe, 0, sizeof(iwe));
350                 iwe.cmd = IWEVGENIE;
351                 iwe.u.data.length = next - pos;
352                 *current_ev = iwe_stream_add_point(info, *current_ev,
353                                                    end_buf, &iwe, pos);
354
355                 pos = next;
356         }
357
358         if (end > pos) {
359                 memset(&iwe, 0, sizeof(iwe));
360                 iwe.cmd = IWEVGENIE;
361                 iwe.u.data.length = end - pos;
362                 *current_ev = iwe_stream_add_point(info, *current_ev,
363                                                    end_buf, &iwe, pos);
364         }
365 }
366
367
368 static char *
369 ieee80211_sta_scan_result(struct ieee80211_local *local,
370                           struct iw_request_info *info,
371                           struct ieee80211_sta_bss *bss,
372                           char *current_ev, char *end_buf)
373 {
374         struct iw_event iwe;
375         char *buf;
376
377         if (time_after(jiffies,
378                        bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
379                 return current_ev;
380
381         memset(&iwe, 0, sizeof(iwe));
382         iwe.cmd = SIOCGIWAP;
383         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
384         memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
385         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
386                                           IW_EV_ADDR_LEN);
387
388         memset(&iwe, 0, sizeof(iwe));
389         iwe.cmd = SIOCGIWESSID;
390         if (bss_mesh_cfg(bss)) {
391                 iwe.u.data.length = bss_mesh_id_len(bss);
392                 iwe.u.data.flags = 1;
393                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
394                                                   &iwe, bss_mesh_id(bss));
395         } else {
396                 iwe.u.data.length = bss->ssid_len;
397                 iwe.u.data.flags = 1;
398                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
399                                                   &iwe, bss->ssid);
400         }
401
402         if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
403             || bss_mesh_cfg(bss)) {
404                 memset(&iwe, 0, sizeof(iwe));
405                 iwe.cmd = SIOCGIWMODE;
406                 if (bss_mesh_cfg(bss))
407                         iwe.u.mode = IW_MODE_MESH;
408                 else if (bss->capability & WLAN_CAPABILITY_ESS)
409                         iwe.u.mode = IW_MODE_MASTER;
410                 else
411                         iwe.u.mode = IW_MODE_ADHOC;
412                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
413                                                   &iwe, IW_EV_UINT_LEN);
414         }
415
416         memset(&iwe, 0, sizeof(iwe));
417         iwe.cmd = SIOCGIWFREQ;
418         iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
419         iwe.u.freq.e = 0;
420         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
421                                           IW_EV_FREQ_LEN);
422
423         memset(&iwe, 0, sizeof(iwe));
424         iwe.cmd = SIOCGIWFREQ;
425         iwe.u.freq.m = bss->freq;
426         iwe.u.freq.e = 6;
427         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
428                                           IW_EV_FREQ_LEN);
429         memset(&iwe, 0, sizeof(iwe));
430         iwe.cmd = IWEVQUAL;
431         iwe.u.qual.qual = bss->qual;
432         iwe.u.qual.level = bss->signal;
433         iwe.u.qual.noise = bss->noise;
434         iwe.u.qual.updated = local->wstats_flags;
435         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
436                                           IW_EV_QUAL_LEN);
437
438         memset(&iwe, 0, sizeof(iwe));
439         iwe.cmd = SIOCGIWENCODE;
440         if (bss->capability & WLAN_CAPABILITY_PRIVACY)
441                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
442         else
443                 iwe.u.data.flags = IW_ENCODE_DISABLED;
444         iwe.u.data.length = 0;
445         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
446                                           &iwe, "");
447
448         ieee80211_sta_add_scan_ies(info, bss, &current_ev, end_buf);
449
450         if (bss->supp_rates_len > 0) {
451                 /* display all supported rates in readable format */
452                 char *p = current_ev + iwe_stream_lcp_len(info);
453                 int i;
454
455                 memset(&iwe, 0, sizeof(iwe));
456                 iwe.cmd = SIOCGIWRATE;
457                 /* Those two flags are ignored... */
458                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
459
460                 for (i = 0; i < bss->supp_rates_len; i++) {
461                         iwe.u.bitrate.value = ((bss->supp_rates[i] &
462                                                         0x7f) * 500000);
463                         p = iwe_stream_add_value(info, current_ev, p,
464                                         end_buf, &iwe, IW_EV_PARAM_LEN);
465                 }
466                 current_ev = p;
467         }
468
469         buf = kmalloc(30, GFP_ATOMIC);
470         if (buf) {
471                 memset(&iwe, 0, sizeof(iwe));
472                 iwe.cmd = IWEVCUSTOM;
473                 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
474                 iwe.u.data.length = strlen(buf);
475                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
476                                                   &iwe, buf);
477                 memset(&iwe, 0, sizeof(iwe));
478                 iwe.cmd = IWEVCUSTOM;
479                 sprintf(buf, " Last beacon: %dms ago",
480                         jiffies_to_msecs(jiffies - bss->last_update));
481                 iwe.u.data.length = strlen(buf);
482                 current_ev = iwe_stream_add_point(info, current_ev,
483                                                   end_buf, &iwe, buf);
484                 kfree(buf);
485         }
486
487         if (bss_mesh_cfg(bss)) {
488                 u8 *cfg = bss_mesh_cfg(bss);
489                 buf = kmalloc(50, GFP_ATOMIC);
490                 if (buf) {
491                         memset(&iwe, 0, sizeof(iwe));
492                         iwe.cmd = IWEVCUSTOM;
493                         sprintf(buf, "Mesh network (version %d)", cfg[0]);
494                         iwe.u.data.length = strlen(buf);
495                         current_ev = iwe_stream_add_point(info, current_ev,
496                                                           end_buf,
497                                                           &iwe, buf);
498                         sprintf(buf, "Path Selection Protocol ID: "
499                                 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
500                                                         cfg[4]);
501                         iwe.u.data.length = strlen(buf);
502                         current_ev = iwe_stream_add_point(info, current_ev,
503                                                           end_buf,
504                                                           &iwe, buf);
505                         sprintf(buf, "Path Selection Metric ID: "
506                                 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
507                                                         cfg[8]);
508                         iwe.u.data.length = strlen(buf);
509                         current_ev = iwe_stream_add_point(info, current_ev,
510                                                           end_buf,
511                                                           &iwe, buf);
512                         sprintf(buf, "Congestion Control Mode ID: "
513                                 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
514                                                         cfg[11], cfg[12]);
515                         iwe.u.data.length = strlen(buf);
516                         current_ev = iwe_stream_add_point(info, current_ev,
517                                                           end_buf,
518                                                           &iwe, buf);
519                         sprintf(buf, "Channel Precedence: "
520                                 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
521                                                         cfg[15], cfg[16]);
522                         iwe.u.data.length = strlen(buf);
523                         current_ev = iwe_stream_add_point(info, current_ev,
524                                                           end_buf,
525                                                           &iwe, buf);
526                         kfree(buf);
527                 }
528         }
529
530         return current_ev;
531 }
532
533
534 int ieee80211_sta_scan_results(struct ieee80211_local *local,
535                                struct iw_request_info *info,
536                                char *buf, size_t len)
537 {
538         char *current_ev = buf;
539         char *end_buf = buf + len;
540         struct ieee80211_sta_bss *bss;
541
542         spin_lock_bh(&local->sta_bss_lock);
543         list_for_each_entry(bss, &local->sta_bss_list, list) {
544                 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
545                         spin_unlock_bh(&local->sta_bss_lock);
546                         return -E2BIG;
547                 }
548                 current_ev = ieee80211_sta_scan_result(local, info, bss,
549                                                        current_ev, end_buf);
550         }
551         spin_unlock_bh(&local->sta_bss_lock);
552         return current_ev - buf;
553 }