mac80211: handle failed scan requests in STA mode
[linux-2.6.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: figure out how to avoid that the "current BSS" expires */
16
17 #include <linux/wireless.h>
18 #include <linux/if_arp.h>
19 #include <linux/rtnetlink.h>
20 #include <net/mac80211.h>
21 #include <net/iw_handler.h>
22
23 #include "ieee80211_i.h"
24 #include "mesh.h"
25
26 #define IEEE80211_PROBE_DELAY (HZ / 33)
27 #define IEEE80211_CHANNEL_TIME (HZ / 33)
28 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
29
30 struct ieee80211_bss *
31 ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
32                      u8 *ssid, u8 ssid_len)
33 {
34         return (void *)cfg80211_get_bss(local->hw.wiphy,
35                                         ieee80211_get_channel(local->hw.wiphy,
36                                                               freq),
37                                         bssid, ssid, ssid_len,
38                                         0, 0);
39 }
40
41 static void ieee80211_rx_bss_free(struct cfg80211_bss *cbss)
42 {
43         struct ieee80211_bss *bss = (void *)cbss;
44
45         kfree(bss_mesh_id(bss));
46         kfree(bss_mesh_cfg(bss));
47 }
48
49 void ieee80211_rx_bss_put(struct ieee80211_local *local,
50                           struct ieee80211_bss *bss)
51 {
52         cfg80211_put_bss((struct cfg80211_bss *)bss);
53 }
54
55 struct ieee80211_bss *
56 ieee80211_bss_info_update(struct ieee80211_local *local,
57                           struct ieee80211_rx_status *rx_status,
58                           struct ieee80211_mgmt *mgmt,
59                           size_t len,
60                           struct ieee802_11_elems *elems,
61                           struct ieee80211_channel *channel,
62                           bool beacon)
63 {
64         struct ieee80211_bss *bss;
65         int clen;
66         s32 signal = 0;
67
68         if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
69                 signal = rx_status->signal * 100;
70         else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
71                 signal = (rx_status->signal * 100) / local->hw.max_signal;
72
73         bss = (void *)cfg80211_inform_bss_frame(local->hw.wiphy, channel,
74                                                 mgmt, len, signal, GFP_ATOMIC);
75
76         if (!bss)
77                 return NULL;
78
79         bss->cbss.free_priv = ieee80211_rx_bss_free;
80
81         /* save the ERP value so that it is available at association time */
82         if (elems->erp_info && elems->erp_info_len >= 1) {
83                 bss->erp_value = elems->erp_info[0];
84                 bss->has_erp_value = 1;
85         }
86
87         if (elems->tim) {
88                 struct ieee80211_tim_ie *tim_ie =
89                         (struct ieee80211_tim_ie *)elems->tim;
90                 bss->dtim_period = tim_ie->dtim_period;
91         }
92
93         /* set default value for buggy APs */
94         if (!elems->tim || bss->dtim_period == 0)
95                 bss->dtim_period = 1;
96
97         bss->supp_rates_len = 0;
98         if (elems->supp_rates) {
99                 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
100                 if (clen > elems->supp_rates_len)
101                         clen = elems->supp_rates_len;
102                 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
103                        clen);
104                 bss->supp_rates_len += clen;
105         }
106         if (elems->ext_supp_rates) {
107                 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
108                 if (clen > elems->ext_supp_rates_len)
109                         clen = elems->ext_supp_rates_len;
110                 memcpy(&bss->supp_rates[bss->supp_rates_len],
111                        elems->ext_supp_rates, clen);
112                 bss->supp_rates_len += clen;
113         }
114
115         bss->wmm_used = elems->wmm_param || elems->wmm_info;
116
117         if (!beacon)
118                 bss->last_probe_resp = jiffies;
119
120         return bss;
121 }
122
123 void ieee80211_rx_bss_remove(struct ieee80211_sub_if_data *sdata, u8 *bssid,
124                              int freq, u8 *ssid, u8 ssid_len)
125 {
126         struct ieee80211_bss *bss;
127         struct ieee80211_local *local = sdata->local;
128
129         bss = ieee80211_rx_bss_get(local, bssid, freq, ssid, ssid_len);
130         if (bss) {
131                 cfg80211_unlink_bss(local->hw.wiphy, (void *)bss);
132                 ieee80211_rx_bss_put(local, bss);
133         }
134 }
135
136 ieee80211_rx_result
137 ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
138                   struct ieee80211_rx_status *rx_status)
139 {
140         struct ieee80211_mgmt *mgmt;
141         struct ieee80211_bss *bss;
142         u8 *elements;
143         struct ieee80211_channel *channel;
144         size_t baselen;
145         int freq;
146         __le16 fc;
147         bool presp, beacon = false;
148         struct ieee802_11_elems elems;
149
150         if (skb->len < 2)
151                 return RX_DROP_UNUSABLE;
152
153         mgmt = (struct ieee80211_mgmt *) skb->data;
154         fc = mgmt->frame_control;
155
156         if (ieee80211_is_ctl(fc))
157                 return RX_CONTINUE;
158
159         if (skb->len < 24)
160                 return RX_DROP_MONITOR;
161
162         presp = ieee80211_is_probe_resp(fc);
163         if (presp) {
164                 /* ignore ProbeResp to foreign address */
165                 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
166                         return RX_DROP_MONITOR;
167
168                 presp = true;
169                 elements = mgmt->u.probe_resp.variable;
170                 baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
171         } else {
172                 beacon = ieee80211_is_beacon(fc);
173                 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
174                 elements = mgmt->u.beacon.variable;
175         }
176
177         if (!presp && !beacon)
178                 return RX_CONTINUE;
179
180         if (baselen > skb->len)
181                 return RX_DROP_MONITOR;
182
183         ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
184
185         if (elems.ds_params && elems.ds_params_len == 1)
186                 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
187         else
188                 freq = rx_status->freq;
189
190         channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
191
192         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
193                 return RX_DROP_MONITOR;
194
195         bss = ieee80211_bss_info_update(sdata->local, rx_status,
196                                         mgmt, skb->len, &elems,
197                                         channel, beacon);
198         if (bss)
199                 ieee80211_rx_bss_put(sdata->local, bss);
200
201         dev_kfree_skb(skb);
202         return RX_QUEUED;
203 }
204
205 void ieee80211_scan_failed(struct ieee80211_local *local)
206 {
207         if (WARN_ON(!local->scan_req))
208                 return;
209
210         /* notify cfg80211 about the failed scan */
211         if (local->scan_req != &local->int_scan_req)
212                 cfg80211_scan_done(local->scan_req, true);
213
214         local->scan_req = NULL;
215 }
216
217 void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
218 {
219         struct ieee80211_local *local = hw_to_local(hw);
220         struct ieee80211_sub_if_data *sdata;
221
222         if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
223                 return;
224
225         if (WARN_ON(!local->scan_req))
226                 return;
227
228         if (local->scan_req != &local->int_scan_req)
229                 cfg80211_scan_done(local->scan_req, aborted);
230         local->scan_req = NULL;
231
232         local->last_scan_completed = jiffies;
233
234         if (local->hw_scanning) {
235                 local->hw_scanning = false;
236                 /*
237                  * Somebody might have requested channel change during scan
238                  * that we won't have acted upon, try now. ieee80211_hw_config
239                  * will set the flag based on actual changes.
240                  */
241                 ieee80211_hw_config(local, 0);
242                 goto done;
243         }
244
245         local->sw_scanning = false;
246         ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
247
248         netif_tx_lock_bh(local->mdev);
249         netif_addr_lock(local->mdev);
250         local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
251         local->ops->configure_filter(local_to_hw(local),
252                                      FIF_BCN_PRBRESP_PROMISC,
253                                      &local->filter_flags,
254                                      local->mdev->mc_count,
255                                      local->mdev->mc_list);
256
257         netif_addr_unlock(local->mdev);
258         netif_tx_unlock_bh(local->mdev);
259
260         if (local->ops->sw_scan_complete)
261                 local->ops->sw_scan_complete(local_to_hw(local));
262
263         mutex_lock(&local->iflist_mtx);
264         list_for_each_entry(sdata, &local->interfaces, list) {
265                 if (!netif_running(sdata->dev))
266                         continue;
267
268                 /* Tell AP we're back */
269                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
270                         if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED) {
271                                 ieee80211_send_nullfunc(local, sdata, 0);
272                                 netif_tx_wake_all_queues(sdata->dev);
273                         }
274                 } else
275                         netif_tx_wake_all_queues(sdata->dev);
276
277                 /* re-enable beaconing */
278                 if (sdata->vif.type == NL80211_IFTYPE_AP ||
279                     sdata->vif.type == NL80211_IFTYPE_ADHOC ||
280                     sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
281                         ieee80211_if_config(sdata,
282                                             IEEE80211_IFCC_BEACON_ENABLED);
283         }
284         mutex_unlock(&local->iflist_mtx);
285
286  done:
287         ieee80211_mlme_notify_scan_completed(local);
288         ieee80211_ibss_notify_scan_completed(local);
289         ieee80211_mesh_notify_scan_completed(local);
290 }
291 EXPORT_SYMBOL(ieee80211_scan_completed);
292
293 void ieee80211_scan_work(struct work_struct *work)
294 {
295         struct ieee80211_local *local =
296                 container_of(work, struct ieee80211_local, scan_work.work);
297         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
298         struct ieee80211_channel *chan;
299         int skip, i;
300         unsigned long next_delay = 0;
301
302         /*
303          * Avoid re-scheduling when the sdata is going away.
304          */
305         if (!netif_running(sdata->dev))
306                 return;
307
308         switch (local->scan_state) {
309         case SCAN_SET_CHANNEL:
310                 /* if no more bands/channels left, complete scan */
311                 if (local->scan_channel_idx >= local->scan_req->n_channels) {
312                         ieee80211_scan_completed(local_to_hw(local), false);
313                         return;
314                 }
315                 skip = 0;
316                 chan = local->scan_req->channels[local->scan_channel_idx];
317
318                 if (chan->flags & IEEE80211_CHAN_DISABLED ||
319                     (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
320                      chan->flags & IEEE80211_CHAN_NO_IBSS))
321                         skip = 1;
322
323                 if (!skip) {
324                         local->scan_channel = chan;
325                         if (ieee80211_hw_config(local,
326                                                 IEEE80211_CONF_CHANGE_CHANNEL))
327                                 skip = 1;
328                 }
329
330                 /* advance state machine to next channel/band */
331                 local->scan_channel_idx++;
332
333                 if (skip)
334                         break;
335
336                 next_delay = IEEE80211_PROBE_DELAY +
337                              usecs_to_jiffies(local->hw.channel_change_time);
338                 local->scan_state = SCAN_SEND_PROBE;
339                 break;
340         case SCAN_SEND_PROBE:
341                 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
342                 local->scan_state = SCAN_SET_CHANNEL;
343
344                 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
345                     !local->scan_req->n_ssids)
346                         break;
347                 for (i = 0; i < local->scan_req->n_ssids; i++)
348                         ieee80211_send_probe_req(
349                                 sdata, NULL,
350                                 local->scan_req->ssids[i].ssid,
351                                 local->scan_req->ssids[i].ssid_len,
352                                 local->scan_req->ie, local->scan_req->ie_len);
353                 next_delay = IEEE80211_CHANNEL_TIME;
354                 break;
355         }
356
357         queue_delayed_work(local->hw.workqueue, &local->scan_work,
358                            next_delay);
359 }
360
361
362 int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
363                          struct cfg80211_scan_request *req)
364 {
365         struct ieee80211_local *local = scan_sdata->local;
366         struct ieee80211_sub_if_data *sdata;
367
368         if (!req)
369                 return -EINVAL;
370
371         if (local->scan_req && local->scan_req != req)
372                 return -EBUSY;
373
374         local->scan_req = req;
375
376         /* MLME-SCAN.request (page 118)  page 144 (11.1.3.1)
377          * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
378          * BSSID: MACAddress
379          * SSID
380          * ScanType: ACTIVE, PASSIVE
381          * ProbeDelay: delay (in microseconds) to be used prior to transmitting
382          *    a Probe frame during active scanning
383          * ChannelList
384          * MinChannelTime (>= ProbeDelay), in TU
385          * MaxChannelTime: (>= MinChannelTime), in TU
386          */
387
388          /* MLME-SCAN.confirm
389           * BSSDescriptionSet
390           * ResultCode: SUCCESS, INVALID_PARAMETERS
391          */
392
393         if (local->sw_scanning || local->hw_scanning) {
394                 if (local->scan_sdata == scan_sdata)
395                         return 0;
396                 return -EBUSY;
397         }
398
399         if (local->ops->hw_scan) {
400                 int rc;
401
402                 local->hw_scanning = true;
403                 rc = local->ops->hw_scan(local_to_hw(local), req);
404                 if (rc) {
405                         local->hw_scanning = false;
406                         return rc;
407                 }
408                 local->scan_sdata = scan_sdata;
409                 return 0;
410         }
411
412         local->sw_scanning = true;
413         if (local->ops->sw_scan_start)
414                 local->ops->sw_scan_start(local_to_hw(local));
415
416         mutex_lock(&local->iflist_mtx);
417         list_for_each_entry(sdata, &local->interfaces, list) {
418                 if (!netif_running(sdata->dev))
419                         continue;
420
421                 /* disable beaconing */
422                 if (sdata->vif.type == NL80211_IFTYPE_AP ||
423                     sdata->vif.type == NL80211_IFTYPE_ADHOC ||
424                     sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
425                         ieee80211_if_config(sdata,
426                                             IEEE80211_IFCC_BEACON_ENABLED);
427
428                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
429                         if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED) {
430                                 netif_tx_stop_all_queues(sdata->dev);
431                                 ieee80211_send_nullfunc(local, sdata, 1);
432                         }
433                 } else
434                         netif_tx_stop_all_queues(sdata->dev);
435         }
436         mutex_unlock(&local->iflist_mtx);
437
438         local->scan_state = SCAN_SET_CHANNEL;
439         local->scan_channel_idx = 0;
440         local->scan_sdata = scan_sdata;
441         local->scan_req = req;
442
443         netif_addr_lock_bh(local->mdev);
444         local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
445         local->ops->configure_filter(local_to_hw(local),
446                                      FIF_BCN_PRBRESP_PROMISC,
447                                      &local->filter_flags,
448                                      local->mdev->mc_count,
449                                      local->mdev->mc_list);
450         netif_addr_unlock_bh(local->mdev);
451
452         /* TODO: start scan as soon as all nullfunc frames are ACKed */
453         queue_delayed_work(local->hw.workqueue, &local->scan_work,
454                            IEEE80211_CHANNEL_TIME);
455
456         return 0;
457 }
458
459
460 int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
461                            struct cfg80211_scan_request *req)
462 {
463         struct ieee80211_local *local = sdata->local;
464         struct ieee80211_if_managed *ifmgd;
465
466         if (!req)
467                 return -EINVAL;
468
469         if (local->scan_req && local->scan_req != req)
470                 return -EBUSY;
471
472         local->scan_req = req;
473
474         if (sdata->vif.type != NL80211_IFTYPE_STATION)
475                 return ieee80211_start_scan(sdata, req);
476
477         /*
478          * STA has a state machine that might need to defer scanning
479          * while it's trying to associate/authenticate, therefore we
480          * queue it up to the state machine in that case.
481          */
482
483         if (local->sw_scanning || local->hw_scanning) {
484                 if (local->scan_sdata == sdata)
485                         return 0;
486                 return -EBUSY;
487         }
488
489         ifmgd = &sdata->u.mgd;
490         set_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request);
491         queue_work(local->hw.workqueue, &ifmgd->work);
492
493         return 0;
494 }