mac80211: remove useless non-NULL tests from scan results code
[linux-2.6.git] / net / mac80211 / mlme.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 /* TODO:
15  * order BSS list by RSSI(?) ("quality of AP")
16  * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17  *    SSID)
18  */
19 #include <linux/delay.h>
20 #include <linux/if_ether.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_arp.h>
24 #include <linux/wireless.h>
25 #include <linux/random.h>
26 #include <linux/etherdevice.h>
27 #include <linux/rtnetlink.h>
28 #include <net/iw_handler.h>
29 #include <net/mac80211.h>
30
31 #include "ieee80211_i.h"
32 #include "rate.h"
33 #include "led.h"
34 #include "mesh.h"
35
36 #define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
37 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38 #define IEEE80211_AUTH_MAX_TRIES 3
39 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40 #define IEEE80211_ASSOC_MAX_TRIES 3
41 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
42 #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
43 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
44 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
46 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
47 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
48
49 #define IEEE80211_PROBE_DELAY (HZ / 33)
50 #define IEEE80211_CHANNEL_TIME (HZ / 33)
51 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
52 #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
53 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
54 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
55 #define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
56
57 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
58
59
60 /* mgmt header + 1 byte category code */
61 #define IEEE80211_MIN_ACTION_SIZE (24 + 1)
62
63 #define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
64 #define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
65 #define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
66 #define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
67 #define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
68
69 /* next values represent the buffer size for A-MPDU frame.
70  * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
71 #define IEEE80211_MIN_AMPDU_BUF 0x8
72 #define IEEE80211_MAX_AMPDU_BUF 0x40
73
74 /* BSS handling */
75 static struct ieee80211_sta_bss *
76 ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
77                      u8 *ssid, u8 ssid_len)
78 {
79         struct ieee80211_sta_bss *bss;
80
81         spin_lock_bh(&local->sta_bss_lock);
82         bss = local->sta_bss_hash[STA_HASH(bssid)];
83         while (bss) {
84                 if (!bss_mesh_cfg(bss) &&
85                     !memcmp(bss->bssid, bssid, ETH_ALEN) &&
86                     bss->freq == freq &&
87                     bss->ssid_len == ssid_len &&
88                     (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
89                         atomic_inc(&bss->users);
90                         break;
91                 }
92                 bss = bss->hnext;
93         }
94         spin_unlock_bh(&local->sta_bss_lock);
95         return bss;
96 }
97
98 /* Caller must hold local->sta_bss_lock */
99 static void __ieee80211_rx_bss_hash_add(struct ieee80211_local *local,
100                                         struct ieee80211_sta_bss *bss)
101 {
102         u8 hash_idx;
103
104         if (bss_mesh_cfg(bss))
105                 hash_idx = mesh_id_hash(bss_mesh_id(bss),
106                                         bss_mesh_id_len(bss));
107         else
108                 hash_idx = STA_HASH(bss->bssid);
109
110         bss->hnext = local->sta_bss_hash[hash_idx];
111         local->sta_bss_hash[hash_idx] = bss;
112 }
113
114 /* Caller must hold local->sta_bss_lock */
115 static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
116                                         struct ieee80211_sta_bss *bss)
117 {
118         struct ieee80211_sta_bss *b, *prev = NULL;
119         b = local->sta_bss_hash[STA_HASH(bss->bssid)];
120         while (b) {
121                 if (b == bss) {
122                         if (!prev)
123                                 local->sta_bss_hash[STA_HASH(bss->bssid)] =
124                                         bss->hnext;
125                         else
126                                 prev->hnext = bss->hnext;
127                         break;
128                 }
129                 prev = b;
130                 b = b->hnext;
131         }
132 }
133
134 static struct ieee80211_sta_bss *
135 ieee80211_rx_bss_add(struct ieee80211_sub_if_data *sdata, u8 *bssid, int freq,
136                      u8 *ssid, u8 ssid_len)
137 {
138         struct ieee80211_local *local = sdata->local;
139         struct ieee80211_sta_bss *bss;
140
141         bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
142         if (!bss)
143                 return NULL;
144         atomic_inc(&bss->users);
145         atomic_inc(&bss->users);
146         memcpy(bss->bssid, bssid, ETH_ALEN);
147         bss->freq = freq;
148         if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
149                 memcpy(bss->ssid, ssid, ssid_len);
150                 bss->ssid_len = ssid_len;
151         }
152
153         spin_lock_bh(&local->sta_bss_lock);
154         /* TODO: order by RSSI? */
155         list_add_tail(&bss->list, &local->sta_bss_list);
156         __ieee80211_rx_bss_hash_add(local, bss);
157         spin_unlock_bh(&local->sta_bss_lock);
158         return bss;
159 }
160
161 #ifdef CONFIG_MAC80211_MESH
162 static struct ieee80211_sta_bss *
163 ieee80211_rx_mesh_bss_get(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
164                           u8 *mesh_cfg, int freq)
165 {
166         struct ieee80211_sta_bss *bss;
167
168         spin_lock_bh(&local->sta_bss_lock);
169         bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
170         while (bss) {
171                 if (bss_mesh_cfg(bss) &&
172                     !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
173                     bss->freq == freq &&
174                     mesh_id_len == bss->mesh_id_len &&
175                     (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
176                                                  mesh_id_len))) {
177                         atomic_inc(&bss->users);
178                         break;
179                 }
180                 bss = bss->hnext;
181         }
182         spin_unlock_bh(&local->sta_bss_lock);
183         return bss;
184 }
185
186 static struct ieee80211_sta_bss *
187 ieee80211_rx_mesh_bss_add(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
188                           u8 *mesh_cfg, int mesh_config_len, int freq)
189 {
190         struct ieee80211_sta_bss *bss;
191
192         if (mesh_config_len != MESH_CFG_LEN)
193                 return NULL;
194
195         bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
196         if (!bss)
197                 return NULL;
198
199         bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
200         if (!bss->mesh_cfg) {
201                 kfree(bss);
202                 return NULL;
203         }
204
205         if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
206                 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
207                 if (!bss->mesh_id) {
208                         kfree(bss->mesh_cfg);
209                         kfree(bss);
210                         return NULL;
211                 }
212                 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
213         }
214
215         atomic_inc(&bss->users);
216         atomic_inc(&bss->users);
217         memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
218         bss->mesh_id_len = mesh_id_len;
219         bss->freq = freq;
220         spin_lock_bh(&local->sta_bss_lock);
221         /* TODO: order by RSSI? */
222         list_add_tail(&bss->list, &local->sta_bss_list);
223         __ieee80211_rx_bss_hash_add(local, bss);
224         spin_unlock_bh(&local->sta_bss_lock);
225         return bss;
226 }
227 #endif
228
229 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
230 {
231         kfree(bss->ies);
232         kfree(bss_mesh_id(bss));
233         kfree(bss_mesh_cfg(bss));
234         kfree(bss);
235 }
236
237 static void ieee80211_rx_bss_put(struct ieee80211_local *local,
238                                  struct ieee80211_sta_bss *bss)
239 {
240         local_bh_disable();
241         if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
242                 local_bh_enable();
243                 return;
244         }
245
246         __ieee80211_rx_bss_hash_del(local, bss);
247         list_del(&bss->list);
248         spin_unlock_bh(&local->sta_bss_lock);
249         ieee80211_rx_bss_free(bss);
250 }
251
252 void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
253 {
254         spin_lock_init(&local->sta_bss_lock);
255         INIT_LIST_HEAD(&local->sta_bss_list);
256 }
257
258 void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
259 {
260         struct ieee80211_sta_bss *bss, *tmp;
261
262         list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
263                 ieee80211_rx_bss_put(local, bss);
264 }
265
266 static u8 *ieee80211_bss_get_ie(struct ieee80211_sta_bss *bss, u8 ie)
267 {
268         u8 *end, *pos;
269
270         pos = bss->ies;
271         if (pos == NULL)
272                 return NULL;
273         end = pos + bss->ies_len;
274
275         while (pos + 1 < end) {
276                 if (pos + 2 + pos[1] > end)
277                         break;
278                 if (pos[0] == ie)
279                         return pos;
280                 pos += 2 + pos[1];
281         }
282
283         return NULL;
284 }
285
286 /* utils */
287 static int ecw2cw(int ecw)
288 {
289         return (1 << ecw) - 1;
290 }
291
292 /* frame sending functions */
293 void ieee80211_sta_tx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
294                       int encrypt)
295 {
296         skb->dev = sdata->local->mdev;
297         skb_set_mac_header(skb, 0);
298         skb_set_network_header(skb, 0);
299         skb_set_transport_header(skb, 0);
300
301         skb->iif = sdata->dev->ifindex;
302         skb->do_not_encrypt = !encrypt;
303
304         dev_queue_xmit(skb);
305 }
306
307 static void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
308                                 struct ieee80211_if_sta *ifsta,
309                                 int transaction, u8 *extra, size_t extra_len,
310                                 int encrypt)
311 {
312         struct ieee80211_local *local = sdata->local;
313         struct sk_buff *skb;
314         struct ieee80211_mgmt *mgmt;
315
316         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
317                             sizeof(*mgmt) + 6 + extra_len);
318         if (!skb) {
319                 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
320                        "frame\n", sdata->dev->name);
321                 return;
322         }
323         skb_reserve(skb, local->hw.extra_tx_headroom);
324
325         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
326         memset(mgmt, 0, 24 + 6);
327         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
328                                           IEEE80211_STYPE_AUTH);
329         if (encrypt)
330                 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
331         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
332         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
333         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
334         mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
335         mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
336         ifsta->auth_transaction = transaction + 1;
337         mgmt->u.auth.status_code = cpu_to_le16(0);
338         if (extra)
339                 memcpy(skb_put(skb, extra_len), extra, extra_len);
340
341         ieee80211_sta_tx(sdata, skb, encrypt);
342 }
343
344 static void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
345                                      u8 *ssid, size_t ssid_len)
346 {
347         struct ieee80211_local *local = sdata->local;
348         struct ieee80211_supported_band *sband;
349         struct sk_buff *skb;
350         struct ieee80211_mgmt *mgmt;
351         u8 *pos, *supp_rates, *esupp_rates = NULL;
352         int i;
353
354         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
355         if (!skb) {
356                 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
357                        "request\n", sdata->dev->name);
358                 return;
359         }
360         skb_reserve(skb, local->hw.extra_tx_headroom);
361
362         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
363         memset(mgmt, 0, 24);
364         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
365                                           IEEE80211_STYPE_PROBE_REQ);
366         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
367         if (dst) {
368                 memcpy(mgmt->da, dst, ETH_ALEN);
369                 memcpy(mgmt->bssid, dst, ETH_ALEN);
370         } else {
371                 memset(mgmt->da, 0xff, ETH_ALEN);
372                 memset(mgmt->bssid, 0xff, ETH_ALEN);
373         }
374         pos = skb_put(skb, 2 + ssid_len);
375         *pos++ = WLAN_EID_SSID;
376         *pos++ = ssid_len;
377         memcpy(pos, ssid, ssid_len);
378
379         supp_rates = skb_put(skb, 2);
380         supp_rates[0] = WLAN_EID_SUPP_RATES;
381         supp_rates[1] = 0;
382         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
383
384         for (i = 0; i < sband->n_bitrates; i++) {
385                 struct ieee80211_rate *rate = &sband->bitrates[i];
386                 if (esupp_rates) {
387                         pos = skb_put(skb, 1);
388                         esupp_rates[1]++;
389                 } else if (supp_rates[1] == 8) {
390                         esupp_rates = skb_put(skb, 3);
391                         esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
392                         esupp_rates[1] = 1;
393                         pos = &esupp_rates[2];
394                 } else {
395                         pos = skb_put(skb, 1);
396                         supp_rates[1]++;
397                 }
398                 *pos = rate->bitrate / 5;
399         }
400
401         ieee80211_sta_tx(sdata, skb, 0);
402 }
403
404 /* MLME */
405 static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
406                                          struct ieee80211_sta_bss *bss,
407                                          int ibss)
408 {
409         struct ieee80211_local *local = sdata->local;
410         int i, have_higher_than_11mbit = 0;
411
412
413         /* cf. IEEE 802.11 9.2.12 */
414         for (i = 0; i < bss->supp_rates_len; i++)
415                 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
416                         have_higher_than_11mbit = 1;
417
418         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
419             have_higher_than_11mbit)
420                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
421         else
422                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
423
424
425         if (local->ops->conf_tx) {
426                 struct ieee80211_tx_queue_params qparam;
427
428                 memset(&qparam, 0, sizeof(qparam));
429
430                 qparam.aifs = 2;
431
432                 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
433                     !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
434                         qparam.cw_min = 31;
435                 else
436                         qparam.cw_min = 15;
437
438                 qparam.cw_max = 1023;
439                 qparam.txop = 0;
440
441                 for (i = 0; i < local_to_hw(local)->queues; i++)
442                         local->ops->conf_tx(local_to_hw(local), i, &qparam);
443         }
444 }
445
446 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
447                                      struct ieee80211_if_sta *ifsta,
448                                      u8 *wmm_param, size_t wmm_param_len)
449 {
450         struct ieee80211_tx_queue_params params;
451         size_t left;
452         int count;
453         u8 *pos;
454
455         if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
456                 return;
457
458         if (!wmm_param)
459                 return;
460
461         if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
462                 return;
463         count = wmm_param[6] & 0x0f;
464         if (count == ifsta->wmm_last_param_set)
465                 return;
466         ifsta->wmm_last_param_set = count;
467
468         pos = wmm_param + 8;
469         left = wmm_param_len - 8;
470
471         memset(&params, 0, sizeof(params));
472
473         if (!local->ops->conf_tx)
474                 return;
475
476         local->wmm_acm = 0;
477         for (; left >= 4; left -= 4, pos += 4) {
478                 int aci = (pos[0] >> 5) & 0x03;
479                 int acm = (pos[0] >> 4) & 0x01;
480                 int queue;
481
482                 switch (aci) {
483                 case 1:
484                         queue = 3;
485                         if (acm)
486                                 local->wmm_acm |= BIT(0) | BIT(3);
487                         break;
488                 case 2:
489                         queue = 1;
490                         if (acm)
491                                 local->wmm_acm |= BIT(4) | BIT(5);
492                         break;
493                 case 3:
494                         queue = 0;
495                         if (acm)
496                                 local->wmm_acm |= BIT(6) | BIT(7);
497                         break;
498                 case 0:
499                 default:
500                         queue = 2;
501                         if (acm)
502                                 local->wmm_acm |= BIT(1) | BIT(2);
503                         break;
504                 }
505
506                 params.aifs = pos[0] & 0x0f;
507                 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
508                 params.cw_min = ecw2cw(pos[1] & 0x0f);
509                 params.txop = get_unaligned_le16(pos + 2);
510 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
511                 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
512                        "cWmin=%d cWmax=%d txop=%d\n",
513                        local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
514                        params.cw_max, params.txop);
515 #endif
516                 /* TODO: handle ACM (block TX, fallback to next lowest allowed
517                  * AC for now) */
518                 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
519                         printk(KERN_DEBUG "%s: failed to set TX queue "
520                                "parameters for queue %d\n", local->mdev->name, queue);
521                 }
522         }
523 }
524
525 static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
526                                            bool use_protection,
527                                            bool use_short_preamble)
528 {
529         struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
530 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
531         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
532         DECLARE_MAC_BUF(mac);
533 #endif
534         u32 changed = 0;
535
536         if (use_protection != bss_conf->use_cts_prot) {
537 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
538                 if (net_ratelimit()) {
539                         printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
540                                "%s)\n",
541                                sdata->dev->name,
542                                use_protection ? "enabled" : "disabled",
543                                print_mac(mac, ifsta->bssid));
544                 }
545 #endif
546                 bss_conf->use_cts_prot = use_protection;
547                 changed |= BSS_CHANGED_ERP_CTS_PROT;
548         }
549
550         if (use_short_preamble != bss_conf->use_short_preamble) {
551 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
552                 if (net_ratelimit()) {
553                         printk(KERN_DEBUG "%s: switched to %s barker preamble"
554                                " (BSSID=%s)\n",
555                                sdata->dev->name,
556                                use_short_preamble ? "short" : "long",
557                                print_mac(mac, ifsta->bssid));
558                 }
559 #endif
560                 bss_conf->use_short_preamble = use_short_preamble;
561                 changed |= BSS_CHANGED_ERP_PREAMBLE;
562         }
563
564         return changed;
565 }
566
567 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
568                                    u8 erp_value)
569 {
570         bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
571         bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
572
573         return ieee80211_handle_protect_preamb(sdata,
574                         use_protection, use_short_preamble);
575 }
576
577 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
578                                            struct ieee80211_sta_bss *bss)
579 {
580         u32 changed = 0;
581
582         if (bss->has_erp_value)
583                 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
584         else {
585                 u16 capab = bss->capability;
586                 changed |= ieee80211_handle_protect_preamb(sdata, false,
587                                 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
588         }
589
590         return changed;
591 }
592
593 int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
594                                    struct ieee80211_ht_info *ht_info)
595 {
596
597         if (ht_info == NULL)
598                 return -EINVAL;
599
600         memset(ht_info, 0, sizeof(*ht_info));
601
602         if (ht_cap_ie) {
603                 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
604
605                 ht_info->ht_supported = 1;
606                 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
607                 ht_info->ampdu_factor =
608                         ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
609                 ht_info->ampdu_density =
610                         (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
611                 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
612         } else
613                 ht_info->ht_supported = 0;
614
615         return 0;
616 }
617
618 int ieee80211_ht_addt_info_ie_to_ht_bss_info(
619                         struct ieee80211_ht_addt_info *ht_add_info_ie,
620                         struct ieee80211_ht_bss_info *bss_info)
621 {
622         if (bss_info == NULL)
623                 return -EINVAL;
624
625         memset(bss_info, 0, sizeof(*bss_info));
626
627         if (ht_add_info_ie) {
628                 u16 op_mode;
629                 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
630
631                 bss_info->primary_channel = ht_add_info_ie->control_chan;
632                 bss_info->bss_cap = ht_add_info_ie->ht_param;
633                 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
634         }
635
636         return 0;
637 }
638
639 static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata,
640                                         struct ieee80211_if_sta *ifsta)
641 {
642         union iwreq_data wrqu;
643         memset(&wrqu, 0, sizeof(wrqu));
644         if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
645                 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
646         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
647         wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
648 }
649
650 static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
651                                          struct ieee80211_if_sta *ifsta)
652 {
653         union iwreq_data wrqu;
654
655         if (ifsta->assocreq_ies) {
656                 memset(&wrqu, 0, sizeof(wrqu));
657                 wrqu.data.length = ifsta->assocreq_ies_len;
658                 wireless_send_event(sdata->dev, IWEVASSOCREQIE, &wrqu,
659                                     ifsta->assocreq_ies);
660         }
661         if (ifsta->assocresp_ies) {
662                 memset(&wrqu, 0, sizeof(wrqu));
663                 wrqu.data.length = ifsta->assocresp_ies_len;
664                 wireless_send_event(sdata->dev, IWEVASSOCRESPIE, &wrqu,
665                                     ifsta->assocresp_ies);
666         }
667 }
668
669
670 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
671                                      struct ieee80211_if_sta *ifsta)
672 {
673         struct ieee80211_local *local = sdata->local;
674         struct ieee80211_conf *conf = &local_to_hw(local)->conf;
675         u32 changed = BSS_CHANGED_ASSOC;
676
677         struct ieee80211_sta_bss *bss;
678
679         ifsta->flags |= IEEE80211_STA_ASSOCIATED;
680
681         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
682                 return;
683
684         bss = ieee80211_rx_bss_get(local, ifsta->bssid,
685                                    conf->channel->center_freq,
686                                    ifsta->ssid, ifsta->ssid_len);
687         if (bss) {
688                 /* set timing information */
689                 sdata->bss_conf.beacon_int = bss->beacon_int;
690                 sdata->bss_conf.timestamp = bss->timestamp;
691                 sdata->bss_conf.dtim_period = bss->dtim_period;
692
693                 changed |= ieee80211_handle_bss_capability(sdata, bss);
694
695                 ieee80211_rx_bss_put(local, bss);
696         }
697
698         if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
699                 changed |= BSS_CHANGED_HT;
700                 sdata->bss_conf.assoc_ht = 1;
701                 sdata->bss_conf.ht_conf = &conf->ht_conf;
702                 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
703         }
704
705         ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
706         memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
707         ieee80211_sta_send_associnfo(sdata, ifsta);
708
709         ifsta->last_probe = jiffies;
710         ieee80211_led_assoc(local, 1);
711
712         sdata->bss_conf.assoc = 1;
713         ieee80211_bss_info_change_notify(sdata, changed);
714
715         netif_tx_start_all_queues(sdata->dev);
716         netif_carrier_on(sdata->dev);
717
718         ieee80211_sta_send_apinfo(sdata, ifsta);
719 }
720
721 static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
722                                    struct ieee80211_if_sta *ifsta)
723 {
724         DECLARE_MAC_BUF(mac);
725
726         ifsta->direct_probe_tries++;
727         if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
728                 printk(KERN_DEBUG "%s: direct probe to AP %s timed out\n",
729                        sdata->dev->name, print_mac(mac, ifsta->bssid));
730                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
731                 return;
732         }
733
734         printk(KERN_DEBUG "%s: direct probe to AP %s try %d\n",
735                         sdata->dev->name, print_mac(mac, ifsta->bssid),
736                         ifsta->direct_probe_tries);
737
738         ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
739
740         set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
741
742         /* Direct probe is sent to broadcast address as some APs
743          * will not answer to direct packet in unassociated state.
744          */
745         ieee80211_send_probe_req(sdata, NULL,
746                                  ifsta->ssid, ifsta->ssid_len);
747
748         mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
749 }
750
751
752 static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
753                                    struct ieee80211_if_sta *ifsta)
754 {
755         DECLARE_MAC_BUF(mac);
756
757         ifsta->auth_tries++;
758         if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
759                 printk(KERN_DEBUG "%s: authentication with AP %s"
760                        " timed out\n",
761                        sdata->dev->name, print_mac(mac, ifsta->bssid));
762                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
763                 return;
764         }
765
766         ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
767         printk(KERN_DEBUG "%s: authenticate with AP %s\n",
768                sdata->dev->name, print_mac(mac, ifsta->bssid));
769
770         ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
771
772         mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
773 }
774
775 static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
776                                       struct ieee80211_supported_band *sband,
777                                       u64 *rates)
778 {
779         int i, j, count;
780         *rates = 0;
781         count = 0;
782         for (i = 0; i < bss->supp_rates_len; i++) {
783                 int rate = (bss->supp_rates[i] & 0x7F) * 5;
784
785                 for (j = 0; j < sband->n_bitrates; j++)
786                         if (sband->bitrates[j].bitrate == rate) {
787                                 *rates |= BIT(j);
788                                 count++;
789                                 break;
790                         }
791         }
792
793         return count;
794 }
795
796 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
797                                  struct ieee80211_if_sta *ifsta)
798 {
799         struct ieee80211_local *local = sdata->local;
800         struct sk_buff *skb;
801         struct ieee80211_mgmt *mgmt;
802         u8 *pos, *ies, *ht_add_ie;
803         int i, len, count, rates_len, supp_rates_len;
804         u16 capab;
805         struct ieee80211_sta_bss *bss;
806         int wmm = 0;
807         struct ieee80211_supported_band *sband;
808         u64 rates = 0;
809
810         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
811                             sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
812                             ifsta->ssid_len);
813         if (!skb) {
814                 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
815                        "frame\n", sdata->dev->name);
816                 return;
817         }
818         skb_reserve(skb, local->hw.extra_tx_headroom);
819
820         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
821
822         capab = ifsta->capab;
823
824         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
825                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
826                         capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
827                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
828                         capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
829         }
830
831         bss = ieee80211_rx_bss_get(local, ifsta->bssid,
832                                    local->hw.conf.channel->center_freq,
833                                    ifsta->ssid, ifsta->ssid_len);
834         if (bss) {
835                 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
836                         capab |= WLAN_CAPABILITY_PRIVACY;
837                 if (bss->wmm_used)
838                         wmm = 1;
839
840                 /* get all rates supported by the device and the AP as
841                  * some APs don't like getting a superset of their rates
842                  * in the association request (e.g. D-Link DAP 1353 in
843                  * b-only mode) */
844                 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
845
846                 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
847                     (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
848                         capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
849
850                 ieee80211_rx_bss_put(local, bss);
851         } else {
852                 rates = ~0;
853                 rates_len = sband->n_bitrates;
854         }
855
856         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
857         memset(mgmt, 0, 24);
858         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
859         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
860         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
861
862         if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
863                 skb_put(skb, 10);
864                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
865                                                   IEEE80211_STYPE_REASSOC_REQ);
866                 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
867                 mgmt->u.reassoc_req.listen_interval =
868                                 cpu_to_le16(local->hw.conf.listen_interval);
869                 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
870                        ETH_ALEN);
871         } else {
872                 skb_put(skb, 4);
873                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
874                                                   IEEE80211_STYPE_ASSOC_REQ);
875                 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
876                 mgmt->u.reassoc_req.listen_interval =
877                                 cpu_to_le16(local->hw.conf.listen_interval);
878         }
879
880         /* SSID */
881         ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
882         *pos++ = WLAN_EID_SSID;
883         *pos++ = ifsta->ssid_len;
884         memcpy(pos, ifsta->ssid, ifsta->ssid_len);
885
886         /* add all rates which were marked to be used above */
887         supp_rates_len = rates_len;
888         if (supp_rates_len > 8)
889                 supp_rates_len = 8;
890
891         len = sband->n_bitrates;
892         pos = skb_put(skb, supp_rates_len + 2);
893         *pos++ = WLAN_EID_SUPP_RATES;
894         *pos++ = supp_rates_len;
895
896         count = 0;
897         for (i = 0; i < sband->n_bitrates; i++) {
898                 if (BIT(i) & rates) {
899                         int rate = sband->bitrates[i].bitrate;
900                         *pos++ = (u8) (rate / 5);
901                         if (++count == 8)
902                                 break;
903                 }
904         }
905
906         if (rates_len > count) {
907                 pos = skb_put(skb, rates_len - count + 2);
908                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
909                 *pos++ = rates_len - count;
910
911                 for (i++; i < sband->n_bitrates; i++) {
912                         if (BIT(i) & rates) {
913                                 int rate = sband->bitrates[i].bitrate;
914                                 *pos++ = (u8) (rate / 5);
915                         }
916                 }
917         }
918
919         if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
920                 /* 1. power capabilities */
921                 pos = skb_put(skb, 4);
922                 *pos++ = WLAN_EID_PWR_CAPABILITY;
923                 *pos++ = 2;
924                 *pos++ = 0; /* min tx power */
925                 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
926
927                 /* 2. supported channels */
928                 /* TODO: get this in reg domain format */
929                 pos = skb_put(skb, 2 * sband->n_channels + 2);
930                 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
931                 *pos++ = 2 * sband->n_channels;
932                 for (i = 0; i < sband->n_channels; i++) {
933                         *pos++ = ieee80211_frequency_to_channel(
934                                         sband->channels[i].center_freq);
935                         *pos++ = 1; /* one channel in the subband*/
936                 }
937         }
938
939         if (ifsta->extra_ie) {
940                 pos = skb_put(skb, ifsta->extra_ie_len);
941                 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
942         }
943
944         if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
945                 pos = skb_put(skb, 9);
946                 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
947                 *pos++ = 7; /* len */
948                 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
949                 *pos++ = 0x50;
950                 *pos++ = 0xf2;
951                 *pos++ = 2; /* WME */
952                 *pos++ = 0; /* WME info */
953                 *pos++ = 1; /* WME ver */
954                 *pos++ = 0;
955         }
956
957         /* wmm support is a must to HT */
958         if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
959             sband->ht_info.ht_supported &&
960             (ht_add_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_EXTRA_INFO))) {
961                 struct ieee80211_ht_addt_info *ht_add_info =
962                         (struct ieee80211_ht_addt_info *)ht_add_ie;
963                 u16 cap = sband->ht_info.cap;
964                 __le16 tmp;
965                 u32 flags = local->hw.conf.channel->flags;
966
967                 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
968                 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
969                         if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
970                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
971                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
972                         }
973                         break;
974                 case IEEE80211_HT_IE_CHA_SEC_BELOW:
975                         if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
976                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
977                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
978                         }
979                         break;
980                 }
981
982                 tmp = cpu_to_le16(cap);
983                 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
984                 *pos++ = WLAN_EID_HT_CAPABILITY;
985                 *pos++ = sizeof(struct ieee80211_ht_cap);
986                 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
987                 memcpy(pos, &tmp, sizeof(u16));
988                 pos += sizeof(u16);
989                 /* TODO: needs a define here for << 2 */
990                 *pos++ = sband->ht_info.ampdu_factor |
991                          (sband->ht_info.ampdu_density << 2);
992                 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
993         }
994
995         kfree(ifsta->assocreq_ies);
996         ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
997         ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
998         if (ifsta->assocreq_ies)
999                 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
1000
1001         ieee80211_sta_tx(sdata, skb, 0);
1002 }
1003
1004
1005 static void ieee80211_send_deauth(struct ieee80211_sub_if_data *sdata,
1006                                   struct ieee80211_if_sta *ifsta, u16 reason)
1007 {
1008         struct ieee80211_local *local = sdata->local;
1009         struct sk_buff *skb;
1010         struct ieee80211_mgmt *mgmt;
1011
1012         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
1013         if (!skb) {
1014                 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
1015                        "frame\n", sdata->dev->name);
1016                 return;
1017         }
1018         skb_reserve(skb, local->hw.extra_tx_headroom);
1019
1020         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1021         memset(mgmt, 0, 24);
1022         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
1023         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1024         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1025         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1026                                           IEEE80211_STYPE_DEAUTH);
1027         skb_put(skb, 2);
1028         mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1029
1030         ieee80211_sta_tx(sdata, skb, 0);
1031 }
1032
1033 static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
1034 {
1035         if (!sdata || !sdata->default_key ||
1036             sdata->default_key->conf.alg != ALG_WEP)
1037                 return 0;
1038         return 1;
1039 }
1040
1041 static void ieee80211_send_disassoc(struct ieee80211_sub_if_data *sdata,
1042                                     struct ieee80211_if_sta *ifsta, u16 reason)
1043 {
1044         struct ieee80211_local *local = sdata->local;
1045         struct sk_buff *skb;
1046         struct ieee80211_mgmt *mgmt;
1047
1048         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
1049         if (!skb) {
1050                 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
1051                        "frame\n", sdata->dev->name);
1052                 return;
1053         }
1054         skb_reserve(skb, local->hw.extra_tx_headroom);
1055
1056         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1057         memset(mgmt, 0, 24);
1058         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
1059         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1060         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1061         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1062                                           IEEE80211_STYPE_DISASSOC);
1063         skb_put(skb, 2);
1064         mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
1065
1066         ieee80211_sta_tx(sdata, skb, 0);
1067 }
1068
1069 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
1070                                    struct ieee80211_if_sta *ifsta, bool deauth,
1071                                    bool self_disconnected, u16 reason)
1072 {
1073         struct ieee80211_local *local = sdata->local;
1074         struct sta_info *sta;
1075         u32 changed = BSS_CHANGED_ASSOC;
1076
1077         rcu_read_lock();
1078
1079         sta = sta_info_get(local, ifsta->bssid);
1080         if (!sta) {
1081                 rcu_read_unlock();
1082                 return;
1083         }
1084
1085         if (deauth) {
1086                 ifsta->direct_probe_tries = 0;
1087                 ifsta->auth_tries = 0;
1088         }
1089         ifsta->assoc_scan_tries = 0;
1090         ifsta->assoc_tries = 0;
1091
1092         netif_tx_stop_all_queues(sdata->dev);
1093         netif_carrier_off(sdata->dev);
1094
1095         ieee80211_sta_tear_down_BA_sessions(sdata, sta->addr);
1096
1097         if (self_disconnected) {
1098                 if (deauth)
1099                         ieee80211_send_deauth(sdata, ifsta, reason);
1100                 else
1101                         ieee80211_send_disassoc(sdata, ifsta, reason);
1102         }
1103
1104         ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
1105         changed |= ieee80211_reset_erp_info(sdata);
1106
1107         if (sdata->bss_conf.assoc_ht)
1108                 changed |= BSS_CHANGED_HT;
1109
1110         sdata->bss_conf.assoc_ht = 0;
1111         sdata->bss_conf.ht_conf = NULL;
1112         sdata->bss_conf.ht_bss_conf = NULL;
1113
1114         ieee80211_led_assoc(local, 0);
1115         sdata->bss_conf.assoc = 0;
1116
1117         ieee80211_sta_send_apinfo(sdata, ifsta);
1118
1119         if (self_disconnected)
1120                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
1121
1122         sta_info_unlink(&sta);
1123
1124         rcu_read_unlock();
1125
1126         sta_info_destroy(sta);
1127 }
1128
1129 static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
1130                                       struct ieee80211_if_sta *ifsta)
1131 {
1132         struct ieee80211_local *local = sdata->local;
1133         struct ieee80211_sta_bss *bss;
1134         int bss_privacy;
1135         int wep_privacy;
1136         int privacy_invoked;
1137
1138         if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
1139                 return 0;
1140
1141         bss = ieee80211_rx_bss_get(local, ifsta->bssid,
1142                                    local->hw.conf.channel->center_freq,
1143                                    ifsta->ssid, ifsta->ssid_len);
1144         if (!bss)
1145                 return 0;
1146
1147         bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
1148         wep_privacy = !!ieee80211_sta_wep_configured(sdata);
1149         privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
1150
1151         ieee80211_rx_bss_put(local, bss);
1152
1153         if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
1154                 return 0;
1155
1156         return 1;
1157 }
1158
1159 static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
1160                                 struct ieee80211_if_sta *ifsta)
1161 {
1162         DECLARE_MAC_BUF(mac);
1163
1164         ifsta->assoc_tries++;
1165         if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
1166                 printk(KERN_DEBUG "%s: association with AP %s"
1167                        " timed out\n",
1168                        sdata->dev->name, print_mac(mac, ifsta->bssid));
1169                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
1170                 return;
1171         }
1172
1173         ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
1174         printk(KERN_DEBUG "%s: associate with AP %s\n",
1175                sdata->dev->name, print_mac(mac, ifsta->bssid));
1176         if (ieee80211_privacy_mismatch(sdata, ifsta)) {
1177                 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
1178                        "mixed-cell disabled - abort association\n", sdata->dev->name);
1179                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
1180                 return;
1181         }
1182
1183         ieee80211_send_assoc(sdata, ifsta);
1184
1185         mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
1186 }
1187
1188
1189 static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
1190                                  struct ieee80211_if_sta *ifsta)
1191 {
1192         struct ieee80211_local *local = sdata->local;
1193         struct sta_info *sta;
1194         int disassoc;
1195         DECLARE_MAC_BUF(mac);
1196
1197         /* TODO: start monitoring current AP signal quality and number of
1198          * missed beacons. Scan other channels every now and then and search
1199          * for better APs. */
1200         /* TODO: remove expired BSSes */
1201
1202         ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
1203
1204         rcu_read_lock();
1205
1206         sta = sta_info_get(local, ifsta->bssid);
1207         if (!sta) {
1208                 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
1209                        sdata->dev->name, print_mac(mac, ifsta->bssid));
1210                 disassoc = 1;
1211         } else {
1212                 disassoc = 0;
1213                 if (time_after(jiffies,
1214                                sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
1215                         if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
1216                                 printk(KERN_DEBUG "%s: No ProbeResp from "
1217                                        "current AP %s - assume out of "
1218                                        "range\n",
1219                                        sdata->dev->name, print_mac(mac, ifsta->bssid));
1220                                 disassoc = 1;
1221                         } else
1222                                 ieee80211_send_probe_req(sdata, ifsta->bssid,
1223                                                          local->scan_ssid,
1224                                                          local->scan_ssid_len);
1225                         ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
1226                 } else {
1227                         ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
1228                         if (time_after(jiffies, ifsta->last_probe +
1229                                        IEEE80211_PROBE_INTERVAL)) {
1230                                 ifsta->last_probe = jiffies;
1231                                 ieee80211_send_probe_req(sdata, ifsta->bssid,
1232                                                          ifsta->ssid,
1233                                                          ifsta->ssid_len);
1234                         }
1235                 }
1236         }
1237
1238         rcu_read_unlock();
1239
1240         if (disassoc)
1241                 ieee80211_set_disassoc(sdata, ifsta, true, true,
1242                                         WLAN_REASON_PREV_AUTH_NOT_VALID);
1243         else
1244                 mod_timer(&ifsta->timer, jiffies +
1245                                       IEEE80211_MONITORING_INTERVAL);
1246 }
1247
1248
1249 static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
1250                                      struct ieee80211_if_sta *ifsta)
1251 {
1252         printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
1253         ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
1254         ieee80211_associate(sdata, ifsta);
1255 }
1256
1257
1258 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1259                                      struct ieee80211_if_sta *ifsta,
1260                                      struct ieee80211_mgmt *mgmt,
1261                                      size_t len)
1262 {
1263         u8 *pos;
1264         struct ieee802_11_elems elems;
1265
1266         pos = mgmt->u.auth.variable;
1267         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1268         if (!elems.challenge)
1269                 return;
1270         ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
1271                             elems.challenge_len + 2, 1);
1272 }
1273
1274 static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
1275                                         u8 dialog_token, u16 status, u16 policy,
1276                                         u16 buf_size, u16 timeout)
1277 {
1278         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1279         struct ieee80211_local *local = sdata->local;
1280         struct sk_buff *skb;
1281         struct ieee80211_mgmt *mgmt;
1282         u16 capab;
1283
1284         skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1285
1286         if (!skb) {
1287                 printk(KERN_DEBUG "%s: failed to allocate buffer "
1288                        "for addba resp frame\n", sdata->dev->name);
1289                 return;
1290         }
1291
1292         skb_reserve(skb, local->hw.extra_tx_headroom);
1293         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1294         memset(mgmt, 0, 24);
1295         memcpy(mgmt->da, da, ETH_ALEN);
1296         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1297         if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1298                 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
1299         else
1300                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1301         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1302                                           IEEE80211_STYPE_ACTION);
1303
1304         skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1305         mgmt->u.action.category = WLAN_CATEGORY_BACK;
1306         mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1307         mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1308
1309         capab = (u16)(policy << 1);     /* bit 1 aggregation policy */
1310         capab |= (u16)(tid << 2);       /* bit 5:2 TID number */
1311         capab |= (u16)(buf_size << 6);  /* bit 15:6 max size of aggregation */
1312
1313         mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1314         mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1315         mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1316
1317         ieee80211_sta_tx(sdata, skb, 0);
1318
1319         return;
1320 }
1321
1322 void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata, const u8 *da,
1323                                 u16 tid, u8 dialog_token, u16 start_seq_num,
1324                                 u16 agg_size, u16 timeout)
1325 {
1326         struct ieee80211_local *local = sdata->local;
1327         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1328         struct sk_buff *skb;
1329         struct ieee80211_mgmt *mgmt;
1330         u16 capab;
1331
1332         skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1333
1334         if (!skb) {
1335                 printk(KERN_ERR "%s: failed to allocate buffer "
1336                                 "for addba request frame\n", sdata->dev->name);
1337                 return;
1338         }
1339         skb_reserve(skb, local->hw.extra_tx_headroom);
1340         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1341         memset(mgmt, 0, 24);
1342         memcpy(mgmt->da, da, ETH_ALEN);
1343         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1344         if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1345                 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
1346         else
1347                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1348
1349         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1350                                           IEEE80211_STYPE_ACTION);
1351
1352         skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1353
1354         mgmt->u.action.category = WLAN_CATEGORY_BACK;
1355         mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1356
1357         mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1358         capab = (u16)(1 << 1);          /* bit 1 aggregation policy */
1359         capab |= (u16)(tid << 2);       /* bit 5:2 TID number */
1360         capab |= (u16)(agg_size << 6);  /* bit 15:6 max size of aggergation */
1361
1362         mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1363
1364         mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1365         mgmt->u.action.u.addba_req.start_seq_num =
1366                                         cpu_to_le16(start_seq_num << 4);
1367
1368         ieee80211_sta_tx(sdata, skb, 0);
1369 }
1370
1371 /*
1372  * After accepting the AddBA Request we activated a timer,
1373  * resetting it after each frame that arrives from the originator.
1374  * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1375  */
1376 static void sta_rx_agg_session_timer_expired(unsigned long data)
1377 {
1378         /* not an elegant detour, but there is no choice as the timer passes
1379          * only one argument, and various sta_info are needed here, so init
1380          * flow in sta_info_create gives the TID as data, while the timer_to_id
1381          * array gives the sta through container_of */
1382         u8 *ptid = (u8 *)data;
1383         u8 *timer_to_id = ptid - *ptid;
1384         struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1385                                          timer_to_tid[0]);
1386
1387 #ifdef CONFIG_MAC80211_HT_DEBUG
1388         printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1389 #endif
1390         ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->addr,
1391                                          (u16)*ptid, WLAN_BACK_TIMER,
1392                                          WLAN_REASON_QSTA_TIMEOUT);
1393 }
1394
1395 static void ieee80211_sta_process_addba_request(struct ieee80211_local *local,
1396                                                 struct ieee80211_mgmt *mgmt,
1397                                                 size_t len)
1398 {
1399         struct ieee80211_hw *hw = &local->hw;
1400         struct ieee80211_conf *conf = &hw->conf;
1401         struct sta_info *sta;
1402         struct tid_ampdu_rx *tid_agg_rx;
1403         u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1404         u8 dialog_token;
1405         int ret = -EOPNOTSUPP;
1406         DECLARE_MAC_BUF(mac);
1407
1408         rcu_read_lock();
1409
1410         sta = sta_info_get(local, mgmt->sa);
1411         if (!sta) {
1412                 rcu_read_unlock();
1413                 return;
1414         }
1415
1416         /* extract session parameters from addba request frame */
1417         dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1418         timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1419         start_seq_num =
1420                 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1421
1422         capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1423         ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1424         tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1425         buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1426
1427         status = WLAN_STATUS_REQUEST_DECLINED;
1428
1429         /* sanity check for incoming parameters:
1430          * check if configuration can support the BA policy
1431          * and if buffer size does not exceeds max value */
1432         if (((ba_policy != 1)
1433                 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1434                 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1435                 status = WLAN_STATUS_INVALID_QOS_PARAM;
1436 #ifdef CONFIG_MAC80211_HT_DEBUG
1437                 if (net_ratelimit())
1438                         printk(KERN_DEBUG "AddBA Req with bad params from "
1439                                 "%s on tid %u. policy %d, buffer size %d\n",
1440                                 print_mac(mac, mgmt->sa), tid, ba_policy,
1441                                 buf_size);
1442 #endif /* CONFIG_MAC80211_HT_DEBUG */
1443                 goto end_no_lock;
1444         }
1445         /* determine default buffer size */
1446         if (buf_size == 0) {
1447                 struct ieee80211_supported_band *sband;
1448
1449                 sband = local->hw.wiphy->bands[conf->channel->band];
1450                 buf_size = IEEE80211_MIN_AMPDU_BUF;
1451                 buf_size = buf_size << sband->ht_info.ampdu_factor;
1452         }
1453
1454
1455         /* examine state machine */
1456         spin_lock_bh(&sta->lock);
1457
1458         if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
1459 #ifdef CONFIG_MAC80211_HT_DEBUG
1460                 if (net_ratelimit())
1461                         printk(KERN_DEBUG "unexpected AddBA Req from "
1462                                 "%s on tid %u\n",
1463                                 print_mac(mac, mgmt->sa), tid);
1464 #endif /* CONFIG_MAC80211_HT_DEBUG */
1465                 goto end;
1466         }
1467
1468         /* prepare A-MPDU MLME for Rx aggregation */
1469         sta->ampdu_mlme.tid_rx[tid] =
1470                         kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1471         if (!sta->ampdu_mlme.tid_rx[tid]) {
1472 #ifdef CONFIG_MAC80211_HT_DEBUG
1473                 if (net_ratelimit())
1474                         printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1475                                         tid);
1476 #endif
1477                 goto end;
1478         }
1479         /* rx timer */
1480         sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1481                                 sta_rx_agg_session_timer_expired;
1482         sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1483                                 (unsigned long)&sta->timer_to_tid[tid];
1484         init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1485
1486         tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1487
1488         /* prepare reordering buffer */
1489         tid_agg_rx->reorder_buf =
1490                 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
1491         if (!tid_agg_rx->reorder_buf) {
1492 #ifdef CONFIG_MAC80211_HT_DEBUG
1493                 if (net_ratelimit())
1494                         printk(KERN_ERR "can not allocate reordering buffer "
1495                                "to tid %d\n", tid);
1496 #endif
1497                 kfree(sta->ampdu_mlme.tid_rx[tid]);
1498                 goto end;
1499         }
1500         memset(tid_agg_rx->reorder_buf, 0,
1501                 buf_size * sizeof(struct sk_buff *));
1502
1503         if (local->ops->ampdu_action)
1504                 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1505                                                sta->addr, tid, &start_seq_num);
1506 #ifdef CONFIG_MAC80211_HT_DEBUG
1507         printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
1508 #endif /* CONFIG_MAC80211_HT_DEBUG */
1509
1510         if (ret) {
1511                 kfree(tid_agg_rx->reorder_buf);
1512                 kfree(tid_agg_rx);
1513                 sta->ampdu_mlme.tid_rx[tid] = NULL;
1514                 goto end;
1515         }
1516
1517         /* change state and send addba resp */
1518         sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
1519         tid_agg_rx->dialog_token = dialog_token;
1520         tid_agg_rx->ssn = start_seq_num;
1521         tid_agg_rx->head_seq_num = start_seq_num;
1522         tid_agg_rx->buf_size = buf_size;
1523         tid_agg_rx->timeout = timeout;
1524         tid_agg_rx->stored_mpdu_num = 0;
1525         status = WLAN_STATUS_SUCCESS;
1526 end:
1527         spin_unlock_bh(&sta->lock);
1528
1529 end_no_lock:
1530         ieee80211_send_addba_resp(sta->sdata, sta->addr, tid,
1531                                   dialog_token, status, 1, buf_size, timeout);
1532         rcu_read_unlock();
1533 }
1534
1535 static void ieee80211_sta_process_addba_resp(struct ieee80211_local *local,
1536                                              struct ieee80211_mgmt *mgmt,
1537                                              size_t len)
1538 {
1539         struct ieee80211_hw *hw = &local->hw;
1540         struct sta_info *sta;
1541         u16 capab;
1542         u16 tid;
1543         u8 *state;
1544
1545         rcu_read_lock();
1546
1547         sta = sta_info_get(local, mgmt->sa);
1548         if (!sta) {
1549                 rcu_read_unlock();
1550                 return;
1551         }
1552
1553         capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1554         tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1555
1556         state = &sta->ampdu_mlme.tid_state_tx[tid];
1557
1558         spin_lock_bh(&sta->lock);
1559
1560         if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1561                 spin_unlock_bh(&sta->lock);
1562                 goto addba_resp_exit;
1563         }
1564
1565         if (mgmt->u.action.u.addba_resp.dialog_token !=
1566                 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1567                 spin_unlock_bh(&sta->lock);
1568 #ifdef CONFIG_MAC80211_HT_DEBUG
1569                 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1570 #endif /* CONFIG_MAC80211_HT_DEBUG */
1571                 goto addba_resp_exit;
1572         }
1573
1574         del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1575 #ifdef CONFIG_MAC80211_HT_DEBUG
1576         printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1577 #endif /* CONFIG_MAC80211_HT_DEBUG */
1578         if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1579                         == WLAN_STATUS_SUCCESS) {
1580                 *state |= HT_ADDBA_RECEIVED_MSK;
1581                 sta->ampdu_mlme.addba_req_num[tid] = 0;
1582
1583                 if (*state == HT_AGG_STATE_OPERATIONAL)
1584                         ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1585
1586                 spin_unlock_bh(&sta->lock);
1587         } else {
1588                 sta->ampdu_mlme.addba_req_num[tid]++;
1589                 /* this will allow the state check in stop_BA_session */
1590                 *state = HT_AGG_STATE_OPERATIONAL;
1591                 spin_unlock_bh(&sta->lock);
1592                 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1593                                              WLAN_BACK_INITIATOR);
1594         }
1595
1596 addba_resp_exit:
1597         rcu_read_unlock();
1598 }
1599
1600 void ieee80211_send_delba(struct ieee80211_sub_if_data *sdata, const u8 *da, u16 tid,
1601                           u16 initiator, u16 reason_code)
1602 {
1603         struct ieee80211_local *local = sdata->local;
1604         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1605         struct sk_buff *skb;
1606         struct ieee80211_mgmt *mgmt;
1607         u16 params;
1608
1609         skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1610
1611         if (!skb) {
1612                 printk(KERN_ERR "%s: failed to allocate buffer "
1613                                         "for delba frame\n", sdata->dev->name);
1614                 return;
1615         }
1616
1617         skb_reserve(skb, local->hw.extra_tx_headroom);
1618         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1619         memset(mgmt, 0, 24);
1620         memcpy(mgmt->da, da, ETH_ALEN);
1621         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1622         if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1623                 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
1624         else
1625                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1626         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1627                                           IEEE80211_STYPE_ACTION);
1628
1629         skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1630
1631         mgmt->u.action.category = WLAN_CATEGORY_BACK;
1632         mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1633         params = (u16)(initiator << 11);        /* bit 11 initiator */
1634         params |= (u16)(tid << 12);             /* bit 15:12 TID number */
1635
1636         mgmt->u.action.u.delba.params = cpu_to_le16(params);
1637         mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1638
1639         ieee80211_sta_tx(sdata, skb, 0);
1640 }
1641
1642 void ieee80211_send_bar(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid, u16 ssn)
1643 {
1644         struct ieee80211_local *local = sdata->local;
1645         struct sk_buff *skb;
1646         struct ieee80211_bar *bar;
1647         u16 bar_control = 0;
1648
1649         skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
1650         if (!skb) {
1651                 printk(KERN_ERR "%s: failed to allocate buffer for "
1652                         "bar frame\n", sdata->dev->name);
1653                 return;
1654         }
1655         skb_reserve(skb, local->hw.extra_tx_headroom);
1656         bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
1657         memset(bar, 0, sizeof(*bar));
1658         bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
1659                                          IEEE80211_STYPE_BACK_REQ);
1660         memcpy(bar->ra, ra, ETH_ALEN);
1661         memcpy(bar->ta, sdata->dev->dev_addr, ETH_ALEN);
1662         bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
1663         bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
1664         bar_control |= (u16)(tid << 12);
1665         bar->control = cpu_to_le16(bar_control);
1666         bar->start_seq_num = cpu_to_le16(ssn);
1667
1668         ieee80211_sta_tx(sdata, skb, 0);
1669 }
1670
1671 void ieee80211_sta_stop_rx_ba_session(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid,
1672                                         u16 initiator, u16 reason)
1673 {
1674         struct ieee80211_local *local = sdata->local;
1675         struct ieee80211_hw *hw = &local->hw;
1676         struct sta_info *sta;
1677         int ret, i;
1678         DECLARE_MAC_BUF(mac);
1679
1680         rcu_read_lock();
1681
1682         sta = sta_info_get(local, ra);
1683         if (!sta) {
1684                 rcu_read_unlock();
1685                 return;
1686         }
1687
1688         /* check if TID is in operational state */
1689         spin_lock_bh(&sta->lock);
1690         if (sta->ampdu_mlme.tid_state_rx[tid]
1691                                 != HT_AGG_STATE_OPERATIONAL) {
1692                 spin_unlock_bh(&sta->lock);
1693                 rcu_read_unlock();
1694                 return;
1695         }
1696         sta->ampdu_mlme.tid_state_rx[tid] =
1697                 HT_AGG_STATE_REQ_STOP_BA_MSK |
1698                 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1699         spin_unlock_bh(&sta->lock);
1700
1701         /* stop HW Rx aggregation. ampdu_action existence
1702          * already verified in session init so we add the BUG_ON */
1703         BUG_ON(!local->ops->ampdu_action);
1704
1705 #ifdef CONFIG_MAC80211_HT_DEBUG
1706         printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1707                                 print_mac(mac, ra), tid);
1708 #endif /* CONFIG_MAC80211_HT_DEBUG */
1709
1710         ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
1711                                         ra, tid, NULL);
1712         if (ret)
1713                 printk(KERN_DEBUG "HW problem - can not stop rx "
1714                                 "aggregation for tid %d\n", tid);
1715
1716         /* shutdown timer has not expired */
1717         if (initiator != WLAN_BACK_TIMER)
1718                 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1719
1720         /* check if this is a self generated aggregation halt */
1721         if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1722                 ieee80211_send_delba(sdata, ra, tid, 0, reason);
1723
1724         /* free the reordering buffer */
1725         for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1726                 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
1727                         /* release the reordered frames */
1728                         dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1729                         sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1730                         sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
1731                 }
1732         }
1733         /* free resources */
1734         kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1735         kfree(sta->ampdu_mlme.tid_rx[tid]);
1736         sta->ampdu_mlme.tid_rx[tid] = NULL;
1737         sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
1738
1739         rcu_read_unlock();
1740 }
1741
1742
1743 static void ieee80211_sta_process_delba(struct ieee80211_sub_if_data *sdata,
1744                         struct ieee80211_mgmt *mgmt, size_t len)
1745 {
1746         struct ieee80211_local *local = sdata->local;
1747         struct sta_info *sta;
1748         u16 tid, params;
1749         u16 initiator;
1750         DECLARE_MAC_BUF(mac);
1751
1752         rcu_read_lock();
1753
1754         sta = sta_info_get(local, mgmt->sa);
1755         if (!sta) {
1756                 rcu_read_unlock();
1757                 return;
1758         }
1759
1760         params = le16_to_cpu(mgmt->u.action.u.delba.params);
1761         tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1762         initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1763
1764 #ifdef CONFIG_MAC80211_HT_DEBUG
1765         if (net_ratelimit())
1766                 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1767                         print_mac(mac, mgmt->sa),
1768                         initiator ? "initiator" : "recipient", tid,
1769                         mgmt->u.action.u.delba.reason_code);
1770 #endif /* CONFIG_MAC80211_HT_DEBUG */
1771
1772         if (initiator == WLAN_BACK_INITIATOR)
1773                 ieee80211_sta_stop_rx_ba_session(sdata, sta->addr, tid,
1774                                                  WLAN_BACK_INITIATOR, 0);
1775         else { /* WLAN_BACK_RECIPIENT */
1776                 spin_lock_bh(&sta->lock);
1777                 sta->ampdu_mlme.tid_state_tx[tid] =
1778                                 HT_AGG_STATE_OPERATIONAL;
1779                 spin_unlock_bh(&sta->lock);
1780                 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1781                                              WLAN_BACK_RECIPIENT);
1782         }
1783         rcu_read_unlock();
1784 }
1785
1786 /*
1787  * After sending add Block Ack request we activated a timer until
1788  * add Block Ack response will arrive from the recipient.
1789  * If this timer expires sta_addba_resp_timer_expired will be executed.
1790  */
1791 void sta_addba_resp_timer_expired(unsigned long data)
1792 {
1793         /* not an elegant detour, but there is no choice as the timer passes
1794          * only one argument, and both sta_info and TID are needed, so init
1795          * flow in sta_info_create gives the TID as data, while the timer_to_id
1796          * array gives the sta through container_of */
1797         u16 tid = *(u8 *)data;
1798         struct sta_info *temp_sta = container_of((void *)data,
1799                 struct sta_info, timer_to_tid[tid]);
1800
1801         struct ieee80211_local *local = temp_sta->local;
1802         struct ieee80211_hw *hw = &local->hw;
1803         struct sta_info *sta;
1804         u8 *state;
1805
1806         rcu_read_lock();
1807
1808         sta = sta_info_get(local, temp_sta->addr);
1809         if (!sta) {
1810                 rcu_read_unlock();
1811                 return;
1812         }
1813
1814         state = &sta->ampdu_mlme.tid_state_tx[tid];
1815         /* check if the TID waits for addBA response */
1816         spin_lock_bh(&sta->lock);
1817         if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1818                 spin_unlock_bh(&sta->lock);
1819                 *state = HT_AGG_STATE_IDLE;
1820 #ifdef CONFIG_MAC80211_HT_DEBUG
1821                 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1822                                 "expecting addBA response there", tid);
1823 #endif
1824                 goto timer_expired_exit;
1825         }
1826
1827 #ifdef CONFIG_MAC80211_HT_DEBUG
1828         printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1829 #endif
1830
1831         /* go through the state check in stop_BA_session */
1832         *state = HT_AGG_STATE_OPERATIONAL;
1833         spin_unlock_bh(&sta->lock);
1834         ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1835                                      WLAN_BACK_INITIATOR);
1836
1837 timer_expired_exit:
1838         rcu_read_unlock();
1839 }
1840
1841 void ieee80211_sta_tear_down_BA_sessions(struct ieee80211_sub_if_data *sdata, u8 *addr)
1842 {
1843         struct ieee80211_local *local = sdata->local;
1844         int i;
1845
1846         for (i = 0; i <  STA_TID_NUM; i++) {
1847                 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1848                                              WLAN_BACK_INITIATOR);
1849                 ieee80211_sta_stop_rx_ba_session(sdata, addr, i,
1850                                                  WLAN_BACK_RECIPIENT,
1851                                                  WLAN_REASON_QSTA_LEAVE_QBSS);
1852         }
1853 }
1854
1855 static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata,
1856                                         struct ieee80211_msrment_ie *request_ie,
1857                                         const u8 *da, const u8 *bssid,
1858                                         u8 dialog_token)
1859 {
1860         struct ieee80211_local *local = sdata->local;
1861         struct sk_buff *skb;
1862         struct ieee80211_mgmt *msr_report;
1863
1864         skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
1865                                 sizeof(struct ieee80211_msrment_ie));
1866
1867         if (!skb) {
1868                 printk(KERN_ERR "%s: failed to allocate buffer for "
1869                                 "measurement report frame\n", sdata->dev->name);
1870                 return;
1871         }
1872
1873         skb_reserve(skb, local->hw.extra_tx_headroom);
1874         msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
1875         memset(msr_report, 0, 24);
1876         memcpy(msr_report->da, da, ETH_ALEN);
1877         memcpy(msr_report->sa, sdata->dev->dev_addr, ETH_ALEN);
1878         memcpy(msr_report->bssid, bssid, ETH_ALEN);
1879         msr_report->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1880                                                 IEEE80211_STYPE_ACTION);
1881
1882         skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
1883         msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
1884         msr_report->u.action.u.measurement.action_code =
1885                                 WLAN_ACTION_SPCT_MSR_RPRT;
1886         msr_report->u.action.u.measurement.dialog_token = dialog_token;
1887
1888         msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
1889         msr_report->u.action.u.measurement.length =
1890                         sizeof(struct ieee80211_msrment_ie);
1891
1892         memset(&msr_report->u.action.u.measurement.msr_elem, 0,
1893                 sizeof(struct ieee80211_msrment_ie));
1894         msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
1895         msr_report->u.action.u.measurement.msr_elem.mode |=
1896                         IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
1897         msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
1898
1899         ieee80211_sta_tx(sdata, skb, 0);
1900 }
1901
1902 static void ieee80211_sta_process_measurement_req(struct ieee80211_sub_if_data *sdata,
1903                                                 struct ieee80211_mgmt *mgmt,
1904                                                 size_t len)
1905 {
1906         /*
1907          * Ignoring measurement request is spec violation.
1908          * Mandatory measurements must be reported optional
1909          * measurements might be refused or reported incapable
1910          * For now just refuse
1911          * TODO: Answer basic measurement as unmeasured
1912          */
1913         ieee80211_send_refuse_measurement_request(sdata,
1914                         &mgmt->u.action.u.measurement.msr_elem,
1915                         mgmt->sa, mgmt->bssid,
1916                         mgmt->u.action.u.measurement.dialog_token);
1917 }
1918
1919
1920 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1921                                    struct ieee80211_if_sta *ifsta,
1922                                    struct ieee80211_mgmt *mgmt,
1923                                    size_t len)
1924 {
1925         u16 auth_alg, auth_transaction, status_code;
1926         DECLARE_MAC_BUF(mac);
1927
1928         if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
1929             sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
1930                 return;
1931
1932         if (len < 24 + 6)
1933                 return;
1934
1935         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1936             memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
1937                 return;
1938
1939         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1940             memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1941                 return;
1942
1943         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1944         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1945         status_code = le16_to_cpu(mgmt->u.auth.status_code);
1946
1947         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1948                 /*
1949                  * IEEE 802.11 standard does not require authentication in IBSS
1950                  * networks and most implementations do not seem to use it.
1951                  * However, try to reply to authentication attempts if someone
1952                  * has actually implemented this.
1953                  */
1954                 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
1955                         return;
1956                 ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
1957         }
1958
1959         if (auth_alg != ifsta->auth_alg ||
1960             auth_transaction != ifsta->auth_transaction)
1961                 return;
1962
1963         if (status_code != WLAN_STATUS_SUCCESS) {
1964                 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1965                         u8 algs[3];
1966                         const int num_algs = ARRAY_SIZE(algs);
1967                         int i, pos;
1968                         algs[0] = algs[1] = algs[2] = 0xff;
1969                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1970                                 algs[0] = WLAN_AUTH_OPEN;
1971                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1972                                 algs[1] = WLAN_AUTH_SHARED_KEY;
1973                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1974                                 algs[2] = WLAN_AUTH_LEAP;
1975                         if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1976                                 pos = 0;
1977                         else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1978                                 pos = 1;
1979                         else
1980                                 pos = 2;
1981                         for (i = 0; i < num_algs; i++) {
1982                                 pos++;
1983                                 if (pos >= num_algs)
1984                                         pos = 0;
1985                                 if (algs[pos] == ifsta->auth_alg ||
1986                                     algs[pos] == 0xff)
1987                                         continue;
1988                                 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1989                                     !ieee80211_sta_wep_configured(sdata))
1990                                         continue;
1991                                 ifsta->auth_alg = algs[pos];
1992                                 break;
1993                         }
1994                 }
1995                 return;
1996         }
1997
1998         switch (ifsta->auth_alg) {
1999         case WLAN_AUTH_OPEN:
2000         case WLAN_AUTH_LEAP:
2001                 ieee80211_auth_completed(sdata, ifsta);
2002                 break;
2003         case WLAN_AUTH_SHARED_KEY:
2004                 if (ifsta->auth_transaction == 4)
2005                         ieee80211_auth_completed(sdata, ifsta);
2006                 else
2007                         ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
2008                 break;
2009         }
2010 }
2011
2012
2013 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2014                                      struct ieee80211_if_sta *ifsta,
2015                                      struct ieee80211_mgmt *mgmt,
2016                                      size_t len)
2017 {
2018         u16 reason_code;
2019         DECLARE_MAC_BUF(mac);
2020
2021         if (len < 24 + 2)
2022                 return;
2023
2024         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
2025                 return;
2026
2027         reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2028
2029         if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
2030                 printk(KERN_DEBUG "%s: deauthenticated\n", sdata->dev->name);
2031
2032         if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
2033             ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
2034             ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
2035                 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
2036                 mod_timer(&ifsta->timer, jiffies +
2037                                       IEEE80211_RETRY_AUTH_INTERVAL);
2038         }
2039
2040         ieee80211_set_disassoc(sdata, ifsta, true, false, 0);
2041         ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
2042 }
2043
2044
2045 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2046                                        struct ieee80211_if_sta *ifsta,
2047                                        struct ieee80211_mgmt *mgmt,
2048                                        size_t len)
2049 {
2050         u16 reason_code;
2051         DECLARE_MAC_BUF(mac);
2052
2053         if (len < 24 + 2)
2054                 return;
2055
2056         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
2057                 return;
2058
2059         reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2060
2061         if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
2062                 printk(KERN_DEBUG "%s: disassociated\n", sdata->dev->name);
2063
2064         if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
2065                 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
2066                 mod_timer(&ifsta->timer, jiffies +
2067                                       IEEE80211_RETRY_AUTH_INTERVAL);
2068         }
2069
2070         ieee80211_set_disassoc(sdata, ifsta, false, false, 0);
2071 }
2072
2073
2074 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2075                                          struct ieee80211_if_sta *ifsta,
2076                                          struct ieee80211_mgmt *mgmt,
2077                                          size_t len,
2078                                          int reassoc)
2079 {
2080         struct ieee80211_local *local = sdata->local;
2081         struct ieee80211_supported_band *sband;
2082         struct sta_info *sta;
2083         u64 rates, basic_rates;
2084         u16 capab_info, status_code, aid;
2085         struct ieee802_11_elems elems;
2086         struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
2087         u8 *pos;
2088         int i, j;
2089         DECLARE_MAC_BUF(mac);
2090         bool have_higher_than_11mbit = false;
2091
2092         /* AssocResp and ReassocResp have identical structure, so process both
2093          * of them in this function. */
2094
2095         if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
2096                 return;
2097
2098         if (len < 24 + 6)
2099                 return;
2100
2101         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
2102                 return;
2103
2104         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2105         status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2106         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2107
2108         printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
2109                "status=%d aid=%d)\n",
2110                sdata->dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
2111                capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
2112
2113         if (status_code != WLAN_STATUS_SUCCESS) {
2114                 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
2115                        sdata->dev->name, status_code);
2116                 /* if this was a reassociation, ensure we try a "full"
2117                  * association next time. This works around some broken APs
2118                  * which do not correctly reject reassociation requests. */
2119                 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2120                 return;
2121         }
2122
2123         if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2124                 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
2125                        "set\n", sdata->dev->name, aid);
2126         aid &= ~(BIT(15) | BIT(14));
2127
2128         pos = mgmt->u.assoc_resp.variable;
2129         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2130
2131         if (!elems.supp_rates) {
2132                 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
2133                        sdata->dev->name);
2134                 return;
2135         }
2136
2137         printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
2138         ifsta->aid = aid;
2139         ifsta->ap_capab = capab_info;
2140
2141         kfree(ifsta->assocresp_ies);
2142         ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
2143         ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
2144         if (ifsta->assocresp_ies)
2145                 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
2146
2147         rcu_read_lock();
2148
2149         /* Add STA entry for the AP */
2150         sta = sta_info_get(local, ifsta->bssid);
2151         if (!sta) {
2152                 struct ieee80211_sta_bss *bss;
2153                 int err;
2154
2155                 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2156                 if (!sta) {
2157                         printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2158                                " the AP\n", sdata->dev->name);
2159                         rcu_read_unlock();
2160                         return;
2161                 }
2162                 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
2163                                            local->hw.conf.channel->center_freq,
2164                                            ifsta->ssid, ifsta->ssid_len);
2165                 if (bss) {
2166                         sta->last_signal = bss->signal;
2167                         sta->last_qual = bss->qual;
2168                         sta->last_noise = bss->noise;
2169                         ieee80211_rx_bss_put(local, bss);
2170                 }
2171
2172                 err = sta_info_insert(sta);
2173                 if (err) {
2174                         printk(KERN_DEBUG "%s: failed to insert STA entry for"
2175                                " the AP (error %d)\n", sdata->dev->name, err);
2176                         rcu_read_unlock();
2177                         return;
2178                 }
2179                 /* update new sta with its last rx activity */
2180                 sta->last_rx = jiffies;
2181         }
2182
2183         /*
2184          * FIXME: Do we really need to update the sta_info's information here?
2185          *        We already know about the AP (we found it in our list) so it
2186          *        should already be filled with the right info, no?
2187          *        As is stands, all this is racy because typically we assume
2188          *        the information that is filled in here (except flags) doesn't
2189          *        change while a STA structure is alive. As such, it should move
2190          *        to between the sta_info_alloc() and sta_info_insert() above.
2191          */
2192
2193         set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2194                            WLAN_STA_AUTHORIZED);
2195
2196         rates = 0;
2197         basic_rates = 0;
2198         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2199
2200         for (i = 0; i < elems.supp_rates_len; i++) {
2201                 int rate = (elems.supp_rates[i] & 0x7f) * 5;
2202
2203                 if (rate > 110)
2204                         have_higher_than_11mbit = true;
2205
2206                 for (j = 0; j < sband->n_bitrates; j++) {
2207                         if (sband->bitrates[j].bitrate == rate)
2208                                 rates |= BIT(j);
2209                         if (elems.supp_rates[i] & 0x80)
2210                                 basic_rates |= BIT(j);
2211                 }
2212         }
2213
2214         for (i = 0; i < elems.ext_supp_rates_len; i++) {
2215                 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
2216
2217                 if (rate > 110)
2218                         have_higher_than_11mbit = true;
2219
2220                 for (j = 0; j < sband->n_bitrates; j++) {
2221                         if (sband->bitrates[j].bitrate == rate)
2222                                 rates |= BIT(j);
2223                         if (elems.ext_supp_rates[i] & 0x80)
2224                                 basic_rates |= BIT(j);
2225                 }
2226         }
2227
2228         sta->supp_rates[local->hw.conf.channel->band] = rates;
2229         sdata->basic_rates = basic_rates;
2230
2231         /* cf. IEEE 802.11 9.2.12 */
2232         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2233             have_higher_than_11mbit)
2234                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2235         else
2236                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
2237
2238         if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2239             (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2240                 struct ieee80211_ht_bss_info bss_info;
2241                 ieee80211_ht_cap_ie_to_ht_info(
2242                                 (struct ieee80211_ht_cap *)
2243                                 elems.ht_cap_elem, &sta->ht_info);
2244                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2245                                 (struct ieee80211_ht_addt_info *)
2246                                 elems.ht_info_elem, &bss_info);
2247                 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
2248         }
2249
2250         rate_control_rate_init(sta, local);
2251
2252         if (elems.wmm_param) {
2253                 set_sta_flags(sta, WLAN_STA_WME);
2254                 rcu_read_unlock();
2255                 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
2256                                          elems.wmm_param_len);
2257         } else
2258                 rcu_read_unlock();
2259
2260         /* set AID and assoc capability,
2261          * ieee80211_set_associated() will tell the driver */
2262         bss_conf->aid = aid;
2263         bss_conf->assoc_capability = capab_info;
2264         ieee80211_set_associated(sdata, ifsta);
2265
2266         ieee80211_associated(sdata, ifsta);
2267 }
2268
2269
2270 static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
2271                                    struct ieee80211_if_sta *ifsta,
2272                                    struct ieee80211_sta_bss *bss)
2273 {
2274         struct ieee80211_local *local = sdata->local;
2275         int res, rates, i, j;
2276         struct sk_buff *skb;
2277         struct ieee80211_mgmt *mgmt;
2278         u8 *pos;
2279         struct ieee80211_supported_band *sband;
2280         union iwreq_data wrqu;
2281
2282         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2283
2284         /* Remove possible STA entries from other IBSS networks. */
2285         sta_info_flush_delayed(sdata);
2286
2287         if (local->ops->reset_tsf) {
2288                 /* Reset own TSF to allow time synchronization work. */
2289                 local->ops->reset_tsf(local_to_hw(local));
2290         }
2291         memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2292         res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
2293         if (res)
2294                 return res;
2295
2296         local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2297
2298         sdata->drop_unencrypted = bss->capability &
2299                 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2300
2301         res = ieee80211_set_freq(sdata, bss->freq);
2302
2303         if (res)
2304                 return res;
2305
2306         /* Build IBSS probe response */
2307         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2308         if (skb) {
2309                 skb_reserve(skb, local->hw.extra_tx_headroom);
2310
2311                 mgmt = (struct ieee80211_mgmt *)
2312                         skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2313                 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2314                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2315                                                   IEEE80211_STYPE_PROBE_RESP);
2316                 memset(mgmt->da, 0xff, ETH_ALEN);
2317                 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
2318                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2319                 mgmt->u.beacon.beacon_int =
2320                         cpu_to_le16(local->hw.conf.beacon_int);
2321                 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
2322                 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2323
2324                 pos = skb_put(skb, 2 + ifsta->ssid_len);
2325                 *pos++ = WLAN_EID_SSID;
2326                 *pos++ = ifsta->ssid_len;
2327                 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2328
2329                 rates = bss->supp_rates_len;
2330                 if (rates > 8)
2331                         rates = 8;
2332                 pos = skb_put(skb, 2 + rates);
2333                 *pos++ = WLAN_EID_SUPP_RATES;
2334                 *pos++ = rates;
2335                 memcpy(pos, bss->supp_rates, rates);
2336
2337                 if (bss->band == IEEE80211_BAND_2GHZ) {
2338                         pos = skb_put(skb, 2 + 1);
2339                         *pos++ = WLAN_EID_DS_PARAMS;
2340                         *pos++ = 1;
2341                         *pos++ = ieee80211_frequency_to_channel(bss->freq);
2342                 }
2343
2344                 pos = skb_put(skb, 2 + 2);
2345                 *pos++ = WLAN_EID_IBSS_PARAMS;
2346                 *pos++ = 2;
2347                 /* FIX: set ATIM window based on scan results */
2348                 *pos++ = 0;
2349                 *pos++ = 0;
2350
2351                 if (bss->supp_rates_len > 8) {
2352                         rates = bss->supp_rates_len - 8;
2353                         pos = skb_put(skb, 2 + rates);
2354                         *pos++ = WLAN_EID_EXT_SUPP_RATES;
2355                         *pos++ = rates;
2356                         memcpy(pos, &bss->supp_rates[8], rates);
2357                 }
2358
2359                 ifsta->probe_resp = skb;
2360
2361                 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
2362         }
2363
2364         rates = 0;
2365         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2366         for (i = 0; i < bss->supp_rates_len; i++) {
2367                 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2368                 for (j = 0; j < sband->n_bitrates; j++)
2369                         if (sband->bitrates[j].bitrate == bitrate)
2370                                 rates |= BIT(j);
2371         }
2372         ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2373
2374         ieee80211_sta_def_wmm_params(sdata, bss, 1);
2375
2376         ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
2377         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2378
2379         memset(&wrqu, 0, sizeof(wrqu));
2380         memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2381         wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
2382
2383         return res;
2384 }
2385
2386 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2387                             struct ieee802_11_elems *elems,
2388                             enum ieee80211_band band)
2389 {
2390         struct ieee80211_supported_band *sband;
2391         struct ieee80211_rate *bitrates;
2392         size_t num_rates;
2393         u64 supp_rates;
2394         int i, j;
2395         sband = local->hw.wiphy->bands[band];
2396
2397         if (!sband) {
2398                 WARN_ON(1);
2399                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2400         }
2401
2402         bitrates = sband->bitrates;
2403         num_rates = sband->n_bitrates;
2404         supp_rates = 0;
2405         for (i = 0; i < elems->supp_rates_len +
2406                      elems->ext_supp_rates_len; i++) {
2407                 u8 rate = 0;
2408                 int own_rate;
2409                 if (i < elems->supp_rates_len)
2410                         rate = elems->supp_rates[i];
2411                 else if (elems->ext_supp_rates)
2412                         rate = elems->ext_supp_rates
2413                                 [i - elems->supp_rates_len];
2414                 own_rate = 5 * (rate & 0x7f);
2415                 for (j = 0; j < num_rates; j++)
2416                         if (bitrates[j].bitrate == own_rate)
2417                                 supp_rates |= BIT(j);
2418         }
2419         return supp_rates;
2420 }
2421
2422 static u64 ieee80211_sta_get_mandatory_rates(struct ieee80211_local *local,
2423                                         enum ieee80211_band band)
2424 {
2425         struct ieee80211_supported_band *sband;
2426         struct ieee80211_rate *bitrates;
2427         u64 mandatory_rates;
2428         enum ieee80211_rate_flags mandatory_flag;
2429         int i;
2430
2431         sband = local->hw.wiphy->bands[band];
2432         if (!sband) {
2433                 WARN_ON(1);
2434                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2435         }
2436
2437         if (band == IEEE80211_BAND_2GHZ)
2438                 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
2439         else
2440                 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
2441
2442         bitrates = sband->bitrates;
2443         mandatory_rates = 0;
2444         for (i = 0; i < sband->n_bitrates; i++)
2445                 if (bitrates[i].flags & mandatory_flag)
2446                         mandatory_rates |= BIT(i);
2447         return mandatory_rates;
2448 }
2449
2450 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
2451                                   struct ieee80211_mgmt *mgmt,
2452                                   size_t len,
2453                                   struct ieee80211_rx_status *rx_status,
2454                                   struct ieee802_11_elems *elems)
2455 {
2456         struct ieee80211_local *local = sdata->local;
2457         int freq, clen;
2458         struct ieee80211_sta_bss *bss;
2459         struct sta_info *sta;
2460         struct ieee80211_channel *channel;
2461         u64 beacon_timestamp, rx_timestamp;
2462         u64 supp_rates = 0;
2463         bool beacon = ieee80211_is_beacon(mgmt->frame_control);
2464         enum ieee80211_band band = rx_status->band;
2465         DECLARE_MAC_BUF(mac);
2466         DECLARE_MAC_BUF(mac2);
2467
2468         if (elems->ds_params && elems->ds_params_len == 1)
2469                 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
2470         else
2471                 freq = rx_status->freq;
2472
2473         channel = ieee80211_get_channel(local->hw.wiphy, freq);
2474
2475         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2476                 return;
2477
2478         if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
2479             elems->mesh_config && mesh_matches_local(elems, sdata)) {
2480                 supp_rates = ieee80211_sta_get_rates(local, elems, band);
2481
2482                 mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
2483                                       mesh_peer_accepts_plinks(elems));
2484         }
2485
2486         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
2487             memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
2488                 supp_rates = ieee80211_sta_get_rates(local, elems, band);
2489
2490                 rcu_read_lock();
2491
2492                 sta = sta_info_get(local, mgmt->sa);
2493                 if (sta) {
2494                         u64 prev_rates;
2495
2496                         prev_rates = sta->supp_rates[band];
2497                         /* make sure mandatory rates are always added */
2498                         sta->supp_rates[band] = supp_rates |
2499                                 ieee80211_sta_get_mandatory_rates(local, band);
2500
2501 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2502                         if (sta->supp_rates[band] != prev_rates)
2503                                 printk(KERN_DEBUG "%s: updated supp_rates set "
2504                                     "for %s based on beacon info (0x%llx | "
2505                                     "0x%llx -> 0x%llx)\n",
2506                                     sdata->dev->name, print_mac(mac, sta->addr),
2507                                     (unsigned long long) prev_rates,
2508                                     (unsigned long long) supp_rates,
2509                                     (unsigned long long) sta->supp_rates[band]);
2510 #endif
2511                 } else {
2512                         ieee80211_ibss_add_sta(sdata, NULL, mgmt->bssid,
2513                                                mgmt->sa, supp_rates);
2514                 }
2515
2516                 rcu_read_unlock();
2517         }
2518
2519 #ifdef CONFIG_MAC80211_MESH
2520         if (elems->mesh_config)
2521                 bss = ieee80211_rx_mesh_bss_get(local, elems->mesh_id,
2522                                 elems->mesh_id_len, elems->mesh_config, freq);
2523         else
2524 #endif
2525                 bss = ieee80211_rx_bss_get(local, mgmt->bssid, freq,
2526                                            elems->ssid, elems->ssid_len);
2527         if (!bss) {
2528 #ifdef CONFIG_MAC80211_MESH
2529                 if (elems->mesh_config)
2530                         bss = ieee80211_rx_mesh_bss_add(local, elems->mesh_id,
2531                                 elems->mesh_id_len, elems->mesh_config,
2532                                 elems->mesh_config_len, freq);
2533                 else
2534 #endif
2535                         bss = ieee80211_rx_bss_add(sdata, mgmt->bssid, freq,
2536                                                   elems->ssid, elems->ssid_len);
2537                 if (!bss)
2538                         return;
2539         } else {
2540 #if 0
2541                 /* TODO: order by RSSI? */
2542                 spin_lock_bh(&local->sta_bss_lock);
2543                 list_move_tail(&bss->list, &local->sta_bss_list);
2544                 spin_unlock_bh(&local->sta_bss_lock);
2545 #endif
2546         }
2547
2548         /* save the ERP value so that it is available at association time */
2549         if (elems->erp_info && elems->erp_info_len >= 1) {
2550                 bss->erp_value = elems->erp_info[0];
2551                 bss->has_erp_value = 1;
2552         }
2553
2554         bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2555         bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
2556
2557         if (elems->tim) {
2558                 struct ieee80211_tim_ie *tim_ie =
2559                         (struct ieee80211_tim_ie *)elems->tim;
2560                 bss->dtim_period = tim_ie->dtim_period;
2561         }
2562
2563         /* set default value for buggy APs */
2564         if (!elems->tim || bss->dtim_period == 0)
2565                 bss->dtim_period = 1;
2566
2567         bss->supp_rates_len = 0;
2568         if (elems->supp_rates) {
2569                 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2570                 if (clen > elems->supp_rates_len)
2571                         clen = elems->supp_rates_len;
2572                 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
2573                        clen);
2574                 bss->supp_rates_len += clen;
2575         }
2576         if (elems->ext_supp_rates) {
2577                 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2578                 if (clen > elems->ext_supp_rates_len)
2579                         clen = elems->ext_supp_rates_len;
2580                 memcpy(&bss->supp_rates[bss->supp_rates_len],
2581                        elems->ext_supp_rates, clen);
2582                 bss->supp_rates_len += clen;
2583         }
2584
2585         bss->band = band;
2586
2587         beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2588
2589         bss->timestamp = beacon_timestamp;
2590         bss->last_update = jiffies;
2591         bss->signal = rx_status->signal;
2592         bss->noise = rx_status->noise;
2593         bss->qual = rx_status->qual;
2594         if (!beacon)
2595                 bss->last_probe_resp = jiffies;
2596         /*
2597          * In STA mode, the remaining parameters should not be overridden
2598          * by beacons because they're not necessarily accurate there.
2599          */
2600         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2601             bss->last_probe_resp && beacon) {
2602                 ieee80211_rx_bss_put(local, bss);
2603                 return;
2604         }
2605
2606         if (bss->ies == NULL || bss->ies_len < elems->total_len) {
2607                 kfree(bss->ies);
2608                 bss->ies = kmalloc(elems->total_len, GFP_ATOMIC);
2609         }
2610         if (bss->ies) {
2611                 memcpy(bss->ies, elems->ie_start, elems->total_len);
2612                 bss->ies_len = elems->total_len;
2613         } else
2614                 bss->ies_len = 0;
2615
2616         bss->wmm_used = elems->wmm_param || elems->wmm_info;
2617
2618         /* check if we need to merge IBSS */
2619         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2620             !local->sta_sw_scanning && !local->sta_hw_scanning &&
2621             bss->capability & WLAN_CAPABILITY_IBSS &&
2622             bss->freq == local->oper_channel->center_freq &&
2623             elems->ssid_len == sdata->u.sta.ssid_len &&
2624             memcmp(elems->ssid, sdata->u.sta.ssid,
2625                                 sdata->u.sta.ssid_len) == 0) {
2626                 if (rx_status->flag & RX_FLAG_TSFT) {
2627                         /* in order for correct IBSS merging we need mactime
2628                          *
2629                          * since mactime is defined as the time the first data
2630                          * symbol of the frame hits the PHY, and the timestamp
2631                          * of the beacon is defined as "the time that the data
2632                          * symbol containing the first bit of the timestamp is
2633                          * transmitted to the PHY plus the transmitting STA’s
2634                          * delays through its local PHY from the MAC-PHY
2635                          * interface to its interface with the WM"
2636                          * (802.11 11.1.2) - equals the time this bit arrives at
2637                          * the receiver - we have to take into account the
2638                          * offset between the two.
2639                          * e.g: at 1 MBit that means mactime is 192 usec earlier
2640                          * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2641                          */
2642                         int rate = local->hw.wiphy->bands[band]->
2643                                         bitrates[rx_status->rate_idx].bitrate;
2644                         rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2645                 } else if (local && local->ops && local->ops->get_tsf)
2646                         /* second best option: get current TSF */
2647                         rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2648                 else
2649                         /* can't merge without knowing the TSF */
2650                         rx_timestamp = -1LLU;
2651 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2652                 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2653                        "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2654                        print_mac(mac, mgmt->sa),
2655                        print_mac(mac2, mgmt->bssid),
2656                        (unsigned long long)rx_timestamp,
2657                        (unsigned long long)beacon_timestamp,
2658                        (unsigned long long)(rx_timestamp - beacon_timestamp),
2659                        jiffies);
2660 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2661                 if (beacon_timestamp > rx_timestamp) {
2662 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2663                         printk(KERN_DEBUG "%s: beacon TSF higher than "
2664                                "local TSF - IBSS merge with BSSID %s\n",
2665                                sdata->dev->name, print_mac(mac, mgmt->bssid));
2666 #endif
2667                         ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
2668                         ieee80211_ibss_add_sta(sdata, NULL,
2669                                                mgmt->bssid, mgmt->sa,
2670                                                supp_rates);
2671                 }
2672         }
2673
2674         ieee80211_rx_bss_put(local, bss);
2675 }
2676
2677
2678 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
2679                                          struct ieee80211_mgmt *mgmt,
2680                                          size_t len,
2681                                          struct ieee80211_rx_status *rx_status)
2682 {
2683         size_t baselen;
2684         struct ieee802_11_elems elems;
2685         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2686
2687         if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
2688                 return; /* ignore ProbeResp to foreign address */
2689
2690         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2691         if (baselen > len)
2692                 return;
2693
2694         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2695                                 &elems);
2696
2697         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2698
2699         /* direct probe may be part of the association flow */
2700         if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
2701                                                         &ifsta->request)) {
2702                 printk(KERN_DEBUG "%s direct probe responded\n",
2703                        sdata->dev->name);
2704                 ieee80211_authenticate(sdata, ifsta);
2705         }
2706 }
2707
2708
2709 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2710                                      struct ieee80211_mgmt *mgmt,
2711                                      size_t len,
2712                                      struct ieee80211_rx_status *rx_status)
2713 {
2714         struct ieee80211_if_sta *ifsta;
2715         size_t baselen;
2716         struct ieee802_11_elems elems;
2717         struct ieee80211_local *local = sdata->local;
2718         struct ieee80211_conf *conf = &local->hw.conf;
2719         u32 changed = 0;
2720
2721         /* Process beacon from the current BSS */
2722         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2723         if (baselen > len)
2724                 return;
2725
2726         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2727
2728         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2729
2730         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2731                 return;
2732         ifsta = &sdata->u.sta;
2733
2734         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
2735             memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2736                 return;
2737
2738         /* Do not send changes to driver if we are scanning. This removes
2739          * requirement that a driver's bss_info_changed/conf_tx functions
2740          * need to be atomic.
2741          * This is really ugly code, we should rewrite scanning and make
2742          * all this more understandable for humans.
2743          */
2744         if (local->sta_sw_scanning || local->sta_hw_scanning)
2745                 return;
2746
2747         ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
2748                                  elems.wmm_param_len);
2749
2750         if (elems.erp_info && elems.erp_info_len >= 1)
2751                 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2752         else {
2753                 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2754                 changed |= ieee80211_handle_protect_preamb(sdata, false,
2755                                 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2756         }
2757
2758         if (elems.ht_cap_elem && elems.ht_info_elem &&
2759             elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2760                 struct ieee80211_ht_bss_info bss_info;
2761
2762                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2763                                 (struct ieee80211_ht_addt_info *)
2764                                 elems.ht_info_elem, &bss_info);
2765                 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2766                                                &bss_info);
2767         }
2768
2769         ieee80211_bss_info_change_notify(sdata, changed);
2770 }
2771
2772
2773 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
2774                                         struct ieee80211_if_sta *ifsta,
2775                                         struct ieee80211_mgmt *mgmt,
2776                                         size_t len,
2777                                         struct ieee80211_rx_status *rx_status)
2778 {
2779         struct ieee80211_local *local = sdata->local;
2780         int tx_last_beacon;
2781         struct sk_buff *skb;
2782         struct ieee80211_mgmt *resp;
2783         u8 *pos, *end;
2784         DECLARE_MAC_BUF(mac);
2785 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2786         DECLARE_MAC_BUF(mac2);
2787         DECLARE_MAC_BUF(mac3);
2788 #endif
2789
2790         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2791             ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
2792             len < 24 + 2 || !ifsta->probe_resp)
2793                 return;
2794
2795         if (local->ops->tx_last_beacon)
2796                 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2797         else
2798                 tx_last_beacon = 1;
2799
2800 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2801         printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2802                "%s (tx_last_beacon=%d)\n",
2803                sdata->dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2804                print_mac(mac3, mgmt->bssid), tx_last_beacon);
2805 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2806
2807         if (!tx_last_beacon)
2808                 return;
2809
2810         if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2811             memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2812                 return;
2813
2814         end = ((u8 *) mgmt) + len;
2815         pos = mgmt->u.probe_req.variable;
2816         if (pos[0] != WLAN_EID_SSID ||
2817             pos + 2 + pos[1] > end) {
2818 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2819                 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2820                        "from %s\n",
2821                        sdata->dev->name, print_mac(mac, mgmt->sa));
2822 #endif
2823                 return;
2824         }
2825         if (pos[1] != 0 &&
2826             (pos[1] != ifsta->ssid_len ||
2827              memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2828                 /* Ignore ProbeReq for foreign SSID */
2829                 return;
2830         }
2831
2832         /* Reply with ProbeResp */
2833         skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
2834         if (!skb)
2835                 return;
2836
2837         resp = (struct ieee80211_mgmt *) skb->data;
2838         memcpy(resp->da, mgmt->sa, ETH_ALEN);
2839 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2840         printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2841                sdata->dev->name, print_mac(mac, resp->da));
2842 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2843         ieee80211_sta_tx(sdata, skb, 0);
2844 }
2845
2846 static void ieee80211_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
2847                                      struct ieee80211_if_sta *ifsta,
2848                                      struct ieee80211_mgmt *mgmt,
2849                                      size_t len,
2850                                      struct ieee80211_rx_status *rx_status)
2851 {
2852         struct ieee80211_local *local = sdata->local;
2853
2854         /* all categories we currently handle have action_code */
2855         if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2856                 return;
2857
2858         switch (mgmt->u.action.category) {
2859         case WLAN_CATEGORY_SPECTRUM_MGMT:
2860                 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
2861                         break;
2862                 switch (mgmt->u.action.u.measurement.action_code) {
2863                 case WLAN_ACTION_SPCT_MSR_REQ:
2864                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2865                                    sizeof(mgmt->u.action.u.measurement)))
2866                                 break;
2867                         ieee80211_sta_process_measurement_req(sdata, mgmt, len);
2868                         break;
2869                 }
2870                 break;
2871         case WLAN_CATEGORY_BACK:
2872                 switch (mgmt->u.action.u.addba_req.action_code) {
2873                 case WLAN_ACTION_ADDBA_REQ:
2874                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2875                                    sizeof(mgmt->u.action.u.addba_req)))
2876                                 break;
2877                         ieee80211_sta_process_addba_request(local, mgmt, len);
2878                         break;
2879                 case WLAN_ACTION_ADDBA_RESP:
2880                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2881                                    sizeof(mgmt->u.action.u.addba_resp)))
2882                                 break;
2883                         ieee80211_sta_process_addba_resp(local, mgmt, len);
2884                         break;
2885                 case WLAN_ACTION_DELBA:
2886                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2887                                    sizeof(mgmt->u.action.u.delba)))
2888                                 break;
2889                         ieee80211_sta_process_delba(sdata, mgmt, len);
2890                         break;
2891                 }
2892                 break;
2893         case PLINK_CATEGORY:
2894                 if (ieee80211_vif_is_mesh(&sdata->vif))
2895                         mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
2896                 break;
2897         case MESH_PATH_SEL_CATEGORY:
2898                 if (ieee80211_vif_is_mesh(&sdata->vif))
2899                         mesh_rx_path_sel_frame(sdata, mgmt, len);
2900                 break;
2901         }
2902 }
2903
2904 void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
2905                            struct ieee80211_rx_status *rx_status)
2906 {
2907         struct ieee80211_local *local = sdata->local;
2908         struct ieee80211_if_sta *ifsta;
2909         struct ieee80211_mgmt *mgmt;
2910         u16 fc;
2911
2912         if (skb->len < 24)
2913                 goto fail;
2914
2915         ifsta = &sdata->u.sta;
2916
2917         mgmt = (struct ieee80211_mgmt *) skb->data;
2918         fc = le16_to_cpu(mgmt->frame_control);
2919
2920         switch (fc & IEEE80211_FCTL_STYPE) {
2921         case IEEE80211_STYPE_PROBE_REQ:
2922         case IEEE80211_STYPE_PROBE_RESP:
2923         case IEEE80211_STYPE_BEACON:
2924         case IEEE80211_STYPE_ACTION:
2925                 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2926         case IEEE80211_STYPE_AUTH:
2927         case IEEE80211_STYPE_ASSOC_RESP:
2928         case IEEE80211_STYPE_REASSOC_RESP:
2929         case IEEE80211_STYPE_DEAUTH:
2930         case IEEE80211_STYPE_DISASSOC:
2931                 skb_queue_tail(&ifsta->skb_queue, skb);
2932                 queue_work(local->hw.workqueue, &ifsta->work);
2933                 return;
2934         }
2935
2936  fail:
2937         kfree_skb(skb);
2938 }
2939
2940 static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2941                                          struct sk_buff *skb)
2942 {
2943         struct ieee80211_rx_status *rx_status;
2944         struct ieee80211_if_sta *ifsta;
2945         struct ieee80211_mgmt *mgmt;
2946         u16 fc;
2947
2948         ifsta = &sdata->u.sta;
2949
2950         rx_status = (struct ieee80211_rx_status *) skb->cb;
2951         mgmt = (struct ieee80211_mgmt *) skb->data;
2952         fc = le16_to_cpu(mgmt->frame_control);
2953
2954         switch (fc & IEEE80211_FCTL_STYPE) {
2955         case IEEE80211_STYPE_PROBE_REQ:
2956                 ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len,
2957                                             rx_status);
2958                 break;
2959         case IEEE80211_STYPE_PROBE_RESP:
2960                 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
2961                 break;
2962         case IEEE80211_STYPE_BEACON:
2963                 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2964                 break;
2965         case IEEE80211_STYPE_AUTH:
2966                 ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
2967                 break;
2968         case IEEE80211_STYPE_ASSOC_RESP:
2969                 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
2970                 break;
2971         case IEEE80211_STYPE_REASSOC_RESP:
2972                 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
2973                 break;
2974         case IEEE80211_STYPE_DEAUTH:
2975                 ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
2976                 break;
2977         case IEEE80211_STYPE_DISASSOC:
2978                 ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
2979                 break;
2980         case IEEE80211_STYPE_ACTION:
2981                 ieee80211_rx_mgmt_action(sdata, ifsta, mgmt, skb->len, rx_status);
2982                 break;
2983         }
2984
2985         kfree_skb(skb);
2986 }
2987
2988
2989 ieee80211_rx_result
2990 ieee80211_sta_rx_scan(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
2991                       struct ieee80211_rx_status *rx_status)
2992 {
2993         struct ieee80211_mgmt *mgmt;
2994         __le16 fc;
2995
2996         if (skb->len < 2)
2997                 return RX_DROP_UNUSABLE;
2998
2999         mgmt = (struct ieee80211_mgmt *) skb->data;
3000         fc = mgmt->frame_control;
3001
3002         if (ieee80211_is_ctl(fc))
3003                 return RX_CONTINUE;
3004
3005         if (skb->len < 24)
3006                 return RX_DROP_MONITOR;
3007
3008         if (ieee80211_is_probe_resp(fc)) {
3009                 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
3010                 dev_kfree_skb(skb);
3011                 return RX_QUEUED;
3012         }
3013
3014         if (ieee80211_is_beacon(fc)) {
3015                 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
3016                 dev_kfree_skb(skb);
3017                 return RX_QUEUED;
3018         }
3019
3020         return RX_CONTINUE;
3021 }
3022
3023
3024 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
3025 {
3026         struct ieee80211_local *local = sdata->local;
3027         int active = 0;
3028         struct sta_info *sta;
3029
3030         rcu_read_lock();
3031
3032         list_for_each_entry_rcu(sta, &local->sta_list, list) {
3033                 if (sta->sdata == sdata &&
3034                     time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3035                                jiffies)) {
3036                         active++;
3037                         break;
3038                 }
3039         }
3040
3041         rcu_read_unlock();
3042
3043         return active;
3044 }
3045
3046
3047 static void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, unsigned long exp_time)
3048 {
3049         struct ieee80211_local *local = sdata->local;
3050         struct sta_info *sta, *tmp;
3051         LIST_HEAD(tmp_list);
3052         DECLARE_MAC_BUF(mac);
3053         unsigned long flags;
3054
3055         spin_lock_irqsave(&local->sta_lock, flags);
3056         list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
3057                 if (time_after(jiffies, sta->last_rx + exp_time)) {
3058 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3059                         printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3060                                sdata->dev->name, print_mac(mac, sta->addr));
3061 #endif
3062                         __sta_info_unlink(&sta);
3063                         if (sta)
3064                                 list_add(&sta->list, &tmp_list);
3065                 }
3066         spin_unlock_irqrestore(&local->sta_lock, flags);
3067
3068         list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3069                 sta_info_destroy(sta);
3070 }
3071
3072
3073 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
3074                                      struct ieee80211_if_sta *ifsta)
3075 {
3076         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3077
3078         ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
3079         if (ieee80211_sta_active_ibss(sdata))
3080                 return;
3081
3082         printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3083                "IBSS networks with same SSID (merge)\n", sdata->dev->name);
3084         ieee80211_sta_req_scan(sdata, ifsta->ssid, ifsta->ssid_len);
3085 }
3086
3087
3088 #ifdef CONFIG_MAC80211_MESH
3089 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
3090                            struct ieee80211_if_sta *ifsta)
3091 {
3092         bool free_plinks;
3093
3094         ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3095         mesh_path_expire(sdata);
3096
3097         free_plinks = mesh_plink_availables(sdata);
3098         if (free_plinks != sdata->u.sta.accepting_plinks)
3099                 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
3100
3101         mod_timer(&ifsta->timer, jiffies +
3102                         IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3103 }
3104
3105
3106 void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
3107 {
3108         struct ieee80211_if_sta *ifsta;
3109         ifsta = &sdata->u.sta;
3110         ifsta->state = IEEE80211_STA_MLME_MESH_UP;
3111         ieee80211_sta_timer((unsigned long)sdata);
3112         ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
3113 }
3114 #endif
3115
3116
3117 void ieee80211_sta_timer(unsigned long data)
3118 {
3119         struct ieee80211_sub_if_data *sdata =
3120                 (struct ieee80211_sub_if_data *) data;
3121         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3122         struct ieee80211_local *local = sdata->local;
3123
3124         set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3125         queue_work(local->hw.workqueue, &ifsta->work);
3126 }
3127
3128 static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
3129                                      struct ieee80211_if_sta *ifsta)
3130 {
3131         struct ieee80211_local *local = sdata->local;
3132
3133         if (local->ops->reset_tsf) {
3134                 /* Reset own TSF to allow time synchronization work. */
3135                 local->ops->reset_tsf(local_to_hw(local));
3136         }
3137
3138         ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3139
3140
3141         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3142                 ifsta->auth_alg = WLAN_AUTH_OPEN;
3143         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3144                 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3145         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3146                 ifsta->auth_alg = WLAN_AUTH_LEAP;
3147         else
3148                 ifsta->auth_alg = WLAN_AUTH_OPEN;
3149         ifsta->auth_transaction = -1;
3150         ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3151         ifsta->assoc_scan_tries = 0;
3152         ifsta->direct_probe_tries = 0;
3153         ifsta->auth_tries = 0;
3154         ifsta->assoc_tries = 0;
3155         netif_tx_stop_all_queues(sdata->dev);
3156         netif_carrier_off(sdata->dev);
3157 }
3158
3159
3160 void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
3161                             struct ieee80211_if_sta *ifsta)
3162 {
3163         struct ieee80211_local *local = sdata->local;
3164
3165         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3166                 return;
3167
3168         if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3169                              IEEE80211_STA_AUTO_BSSID_SEL)) &&
3170             (ifsta->flags & (IEEE80211_STA_SSID_SET |
3171                              IEEE80211_STA_AUTO_SSID_SEL))) {
3172
3173                 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED)
3174                         ieee80211_set_disassoc(sdata, ifsta, true, true,
3175                                                WLAN_REASON_DEAUTH_LEAVING);
3176
3177                 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3178                 queue_work(local->hw.workqueue, &ifsta->work);
3179         }
3180 }
3181
3182 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3183                                     const char *ssid, int ssid_len)
3184 {
3185         int tmp, hidden_ssid;
3186
3187         if (ssid_len == ifsta->ssid_len &&
3188             !memcmp(ifsta->ssid, ssid, ssid_len))
3189                 return 1;
3190
3191         if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
3192                 return 0;
3193
3194         hidden_ssid = 1;
3195         tmp = ssid_len;
3196         while (tmp--) {
3197                 if (ssid[tmp] != '\0') {
3198                         hidden_ssid = 0;
3199                         break;
3200                 }
3201         }
3202
3203         if (hidden_ssid && ifsta->ssid_len == ssid_len)
3204                 return 1;
3205
3206         if (ssid_len == 1 && ssid[0] == ' ')
3207                 return 1;
3208
3209         return 0;
3210 }
3211
3212 static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
3213                                      struct ieee80211_if_sta *ifsta)
3214 {
3215         struct ieee80211_local *local = sdata->local;
3216         struct ieee80211_sta_bss *bss;
3217         struct ieee80211_supported_band *sband;
3218         u8 bssid[ETH_ALEN], *pos;
3219         int i;
3220         int ret;
3221         DECLARE_MAC_BUF(mac);
3222
3223 #if 0
3224         /* Easier testing, use fixed BSSID. */
3225         memset(bssid, 0xfe, ETH_ALEN);
3226 #else
3227         /* Generate random, not broadcast, locally administered BSSID. Mix in
3228          * own MAC address to make sure that devices that do not have proper
3229          * random number generator get different BSSID. */
3230         get_random_bytes(bssid, ETH_ALEN);
3231         for (i = 0; i < ETH_ALEN; i++)
3232                 bssid[i] ^= sdata->dev->dev_addr[i];
3233         bssid[0] &= ~0x01;
3234         bssid[0] |= 0x02;
3235 #endif
3236
3237         printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3238                sdata->dev->name, print_mac(mac, bssid));
3239
3240         bss = ieee80211_rx_bss_add(sdata, bssid,
3241                                    local->hw.conf.channel->center_freq,
3242                                    sdata->u.sta.ssid, sdata->u.sta.ssid_len);
3243         if (!bss)
3244                 return -ENOMEM;
3245
3246         bss->band = local->hw.conf.channel->band;
3247         sband = local->hw.wiphy->bands[bss->band];
3248
3249         if (local->hw.conf.beacon_int == 0)
3250                 local->hw.conf.beacon_int = 100;
3251         bss->beacon_int = local->hw.conf.beacon_int;
3252         bss->last_update = jiffies;
3253         bss->capability = WLAN_CAPABILITY_IBSS;
3254
3255         if (sdata->default_key)
3256                 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3257         else
3258                 sdata->drop_unencrypted = 0;
3259
3260         bss->supp_rates_len = sband->n_bitrates;
3261         pos = bss->supp_rates;
3262         for (i = 0; i < sband->n_bitrates; i++) {
3263                 int rate = sband->bitrates[i].bitrate;
3264                 *pos++ = (u8) (rate / 5);
3265         }
3266
3267         ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
3268         ieee80211_rx_bss_put(local, bss);
3269         return ret;
3270 }
3271
3272
3273 static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
3274                                    struct ieee80211_if_sta *ifsta)
3275 {
3276         struct ieee80211_local *local = sdata->local;
3277         struct ieee80211_sta_bss *bss;
3278         int found = 0;
3279         u8 bssid[ETH_ALEN];
3280         int active_ibss;
3281         DECLARE_MAC_BUF(mac);
3282         DECLARE_MAC_BUF(mac2);
3283
3284         if (ifsta->ssid_len == 0)
3285                 return -EINVAL;
3286
3287         active_ibss = ieee80211_sta_active_ibss(sdata);
3288 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3289         printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3290                sdata->dev->name, active_ibss);
3291 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3292         spin_lock_bh(&local->sta_bss_lock);
3293         list_for_each_entry(bss, &local->sta_bss_list, list) {
3294                 if (ifsta->ssid_len != bss->ssid_len ||
3295                     memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3296                     || !(bss->capability & WLAN_CAPABILITY_IBSS))
3297                         continue;
3298 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3299                 printk(KERN_DEBUG "   bssid=%s found\n",
3300                        print_mac(mac, bss->bssid));
3301 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3302                 memcpy(bssid, bss->bssid, ETH_ALEN);
3303                 found = 1;
3304                 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3305                         break;
3306         }
3307         spin_unlock_bh(&local->sta_bss_lock);
3308
3309 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3310         if (found)
3311                 printk(KERN_DEBUG "   sta_find_ibss: selected %s current "
3312                        "%s\n", print_mac(mac, bssid),
3313                        print_mac(mac2, ifsta->bssid));
3314 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3315
3316         if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3317                 int ret;
3318                 int search_freq;
3319
3320                 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
3321                         search_freq = bss->freq;
3322                 else
3323                         search_freq = local->hw.conf.channel->center_freq;
3324
3325                 bss = ieee80211_rx_bss_get(local, bssid, search_freq,
3326                                            ifsta->ssid, ifsta->ssid_len);
3327                 if (!bss)
3328                         goto dont_join;
3329
3330                 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
3331                        " based on configured SSID\n",
3332                        sdata->dev->name, print_mac(mac, bssid));
3333                 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
3334                 ieee80211_rx_bss_put(local, bss);
3335                 return ret;
3336         }
3337
3338 dont_join:
3339 #ifdef CONFIG_MAC80211_IBSS_DEBUG
3340         printk(KERN_DEBUG "   did not try to join ibss\n");
3341 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
3342
3343         /* Selected IBSS not found in current scan results - try to scan */
3344         if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
3345             !ieee80211_sta_active_ibss(sdata)) {
3346                 mod_timer(&ifsta->timer, jiffies +
3347                                       IEEE80211_IBSS_MERGE_INTERVAL);
3348         } else if (time_after(jiffies, local->last_scan_completed +
3349                               IEEE80211_SCAN_INTERVAL)) {
3350                 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3351                        "join\n", sdata->dev->name);
3352                 return ieee80211_sta_req_scan(sdata, ifsta->ssid,
3353                                               ifsta->ssid_len);
3354         } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
3355                 int interval = IEEE80211_SCAN_INTERVAL;
3356
3357                 if (time_after(jiffies, ifsta->ibss_join_req +
3358                                IEEE80211_IBSS_JOIN_TIMEOUT)) {
3359                         if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
3360                             (!(local->oper_channel->flags &
3361                                         IEEE80211_CHAN_NO_IBSS)))
3362                                 return ieee80211_sta_create_ibss(sdata, ifsta);
3363                         if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
3364                                 printk(KERN_DEBUG "%s: IBSS not allowed on"
3365                                        " %d MHz\n", sdata->dev->name,
3366                                        local->hw.conf.channel->center_freq);
3367                         }
3368
3369                         /* No IBSS found - decrease scan interval and continue
3370                          * scanning. */
3371                         interval = IEEE80211_SCAN_INTERVAL_SLOW;
3372                 }
3373
3374                 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
3375                 mod_timer(&ifsta->timer, jiffies + interval);
3376                 return 0;
3377         }
3378
3379         return 0;
3380 }
3381
3382
3383 int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
3384 {
3385         struct ieee80211_if_sta *ifsta;
3386         int res;
3387
3388         if (len > IEEE80211_MAX_SSID_LEN)
3389                 return -EINVAL;
3390
3391         ifsta = &sdata->u.sta;
3392
3393         if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
3394                 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
3395                 memcpy(ifsta->ssid, ssid, len);
3396                 ifsta->ssid_len = len;
3397                 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
3398
3399                 res = 0;
3400                 /*
3401                  * Hack! MLME code needs to be cleaned up to have different
3402                  * entry points for configuration and internal selection change
3403                  */
3404                 if (netif_running(sdata->dev))
3405                         res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
3406                 if (res) {
3407                         printk(KERN_DEBUG "%s: Failed to config new SSID to "
3408                                "the low-level driver\n", sdata->dev->name);
3409                         return res;
3410                 }
3411         }
3412
3413         if (len)
3414                 ifsta->flags |= IEEE80211_STA_SSID_SET;
3415         else
3416                 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
3417
3418         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3419             !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
3420                 ifsta->ibss_join_req = jiffies;
3421                 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
3422                 return ieee80211_sta_find_ibss(sdata, ifsta);
3423         }
3424
3425         return 0;
3426 }
3427
3428
3429 int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
3430 {
3431         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3432         memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3433         *len = ifsta->ssid_len;
3434         return 0;
3435 }
3436
3437
3438 int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
3439 {
3440         struct ieee80211_if_sta *ifsta;
3441         int res;
3442
3443         ifsta = &sdata->u.sta;
3444
3445         if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3446                 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3447                 res = 0;
3448                 /*
3449                  * Hack! See also ieee80211_sta_set_ssid.
3450                  */
3451                 if (netif_running(sdata->dev))
3452                         res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
3453                 if (res) {
3454                         printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3455                                "the low-level driver\n", sdata->dev->name);
3456                         return res;
3457                 }
3458         }
3459
3460         if (is_valid_ether_addr(bssid))
3461                 ifsta->flags |= IEEE80211_STA_BSSID_SET;
3462         else
3463                 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3464
3465         return 0;
3466 }
3467
3468
3469 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3470                                     struct ieee80211_sub_if_data *sdata,
3471                                     int powersave)
3472 {
3473         struct sk_buff *skb;
3474         struct ieee80211_hdr *nullfunc;
3475         __le16 fc;
3476
3477         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3478         if (!skb) {
3479                 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3480                        "frame\n", sdata->dev->name);
3481                 return;
3482         }
3483         skb_reserve(skb, local->hw.extra_tx_headroom);
3484
3485         nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3486         memset(nullfunc, 0, 24);
3487         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3488                          IEEE80211_FCTL_TODS);
3489         if (powersave)
3490                 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
3491         nullfunc->frame_control = fc;
3492         memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3493         memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3494         memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3495
3496         ieee80211_sta_tx(sdata, skb, 0);
3497 }
3498
3499
3500 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3501 {
3502         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3503             ieee80211_vif_is_mesh(&sdata->vif))
3504                 ieee80211_sta_timer((unsigned long)sdata);
3505 }
3506
3507 void ieee80211_scan_completed(struct ieee80211_hw *hw)
3508 {
3509         struct ieee80211_local *local = hw_to_local(hw);
3510         struct ieee80211_sub_if_data *sdata;
3511         union iwreq_data wrqu;
3512
3513         local->last_scan_completed = jiffies;
3514         memset(&wrqu, 0, sizeof(wrqu));
3515         wireless_send_event(local->scan_sdata->dev, SIOCGIWSCAN, &wrqu, NULL);
3516
3517         if (local->sta_hw_scanning) {
3518                 local->sta_hw_scanning = 0;
3519                 if (ieee80211_hw_config(local))
3520                         printk(KERN_DEBUG "%s: failed to restore operational "
3521                                "channel after scan\n", wiphy_name(local->hw.wiphy));
3522                 /* Restart STA timer for HW scan case */
3523                 rcu_read_lock();
3524                 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3525                         ieee80211_restart_sta_timer(sdata);
3526                 rcu_read_unlock();
3527
3528                 goto done;
3529         }
3530
3531         local->sta_sw_scanning = 0;
3532         if (ieee80211_hw_config(local))
3533                 printk(KERN_DEBUG "%s: failed to restore operational "
3534                        "channel after scan\n", wiphy_name(local->hw.wiphy));
3535
3536
3537         netif_tx_lock_bh(local->mdev);
3538         netif_addr_lock(local->mdev);
3539         local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3540         local->ops->configure_filter(local_to_hw(local),
3541                                      FIF_BCN_PRBRESP_PROMISC,
3542                                      &local->filter_flags,
3543                                      local->mdev->mc_count,
3544                                      local->mdev->mc_list);
3545
3546         netif_addr_unlock(local->mdev);
3547         netif_tx_unlock_bh(local->mdev);
3548
3549         rcu_read_lock();
3550         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3551                 /* Tell AP we're back */
3552                 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
3553                         if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
3554                                 ieee80211_send_nullfunc(local, sdata, 0);
3555                                 netif_tx_wake_all_queues(sdata->dev);
3556                         }
3557                 } else
3558                         netif_tx_wake_all_queues(sdata->dev);
3559
3560                 ieee80211_restart_sta_timer(sdata);
3561         }
3562         rcu_read_unlock();
3563
3564  done:
3565         sdata = local->scan_sdata;
3566         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3567                 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3568                 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3569                     (!(ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED) &&
3570                     !ieee80211_sta_active_ibss(sdata)))
3571                         ieee80211_sta_find_ibss(sdata, ifsta);
3572         }
3573 }
3574 EXPORT_SYMBOL(ieee80211_scan_completed);
3575
3576 void ieee80211_sta_scan_work(struct work_struct *work)
3577 {
3578         struct ieee80211_local *local =
3579                 container_of(work, struct ieee80211_local, scan_work.work);
3580         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
3581         struct ieee80211_supported_band *sband;
3582         struct ieee80211_channel *chan;
3583         int skip;
3584         unsigned long next_delay = 0;
3585
3586         if (!local->sta_sw_scanning)
3587                 return;
3588
3589         switch (local->scan_state) {
3590         case SCAN_SET_CHANNEL:
3591                 /*
3592                  * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3593                  * after we successfully scanned the last channel of the last
3594                  * band (and the last band is supported by the hw)
3595                  */
3596                 if (local->scan_band < IEEE80211_NUM_BANDS)
3597                         sband = local->hw.wiphy->bands[local->scan_band];
3598                 else
3599                         sband = NULL;
3600
3601                 /*
3602                  * If we are at an unsupported band and have more bands
3603                  * left to scan, advance to the next supported one.
3604                  */
3605                 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
3606                         local->scan_band++;
3607                         sband = local->hw.wiphy->bands[local->scan_band];
3608                         local->scan_channel_idx = 0;
3609                 }
3610
3611                 /* if no more bands/channels left, complete scan */
3612                 if (!sband || local->scan_channel_idx >= sband->n_channels) {
3613                         ieee80211_scan_completed(local_to_hw(local));
3614                         return;
3615                 }
3616                 skip = 0;
3617                 chan = &sband->channels[local->scan_channel_idx];
3618
3619                 if (chan->flags & IEEE80211_CHAN_DISABLED ||
3620                     (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3621                      chan->flags & IEEE80211_CHAN_NO_IBSS))
3622                         skip = 1;
3623
3624                 if (!skip) {
3625                         local->scan_channel = chan;
3626                         if (ieee80211_hw_config(local)) {
3627                                 printk(KERN_DEBUG "%s: failed to set freq to "
3628                                        "%d MHz for scan\n", wiphy_name(local->hw.wiphy),
3629                                        chan->center_freq);
3630                                 skip = 1;
3631                         }
3632                 }
3633
3634                 /* advance state machine to next channel/band */
3635                 local->scan_channel_idx++;
3636                 if (local->scan_channel_idx >= sband->n_channels) {
3637                         /*
3638                          * scan_band may end up == IEEE80211_NUM_BANDS, but
3639                          * we'll catch that case above and complete the scan
3640                          * if that is the case.
3641                          */
3642                         local->scan_band++;
3643                         local->scan_channel_idx = 0;
3644                 }
3645
3646                 if (skip)
3647                         break;
3648
3649                 next_delay = IEEE80211_PROBE_DELAY +
3650                              usecs_to_jiffies(local->hw.channel_change_time);
3651                 local->scan_state = SCAN_SEND_PROBE;
3652                 break;
3653         case SCAN_SEND_PROBE:
3654                 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3655                 local->scan_state = SCAN_SET_CHANNEL;
3656
3657                 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3658                         break;
3659                 ieee80211_send_probe_req(sdata, NULL, local->scan_ssid,
3660                                          local->scan_ssid_len);
3661                 next_delay = IEEE80211_CHANNEL_TIME;
3662                 break;
3663         }
3664
3665         if (local->sta_sw_scanning)
3666                 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3667                                    next_delay);
3668 }
3669
3670
3671 static int ieee80211_sta_start_scan(struct ieee80211_sub_if_data *scan_sdata,
3672                                     u8 *ssid, size_t ssid_len)
3673 {
3674         struct ieee80211_local *local = scan_sdata->local;
3675         struct ieee80211_sub_if_data *sdata;
3676
3677         if (ssid_len > IEEE80211_MAX_SSID_LEN)
3678                 return -EINVAL;
3679
3680         /* MLME-SCAN.request (page 118)  page 144 (11.1.3.1)
3681          * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3682          * BSSID: MACAddress
3683          * SSID
3684          * ScanType: ACTIVE, PASSIVE
3685          * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3686          *    a Probe frame during active scanning
3687          * ChannelList
3688          * MinChannelTime (>= ProbeDelay), in TU
3689          * MaxChannelTime: (>= MinChannelTime), in TU
3690          */
3691
3692          /* MLME-SCAN.confirm
3693           * BSSDescriptionSet
3694           * ResultCode: SUCCESS, INVALID_PARAMETERS
3695          */
3696
3697         if (local->sta_sw_scanning || local->sta_hw_scanning) {
3698                 if (local->scan_sdata == scan_sdata)
3699                         return 0;
3700                 return -EBUSY;
3701         }
3702
3703         if (local->ops->hw_scan) {
3704                 int rc = local->ops->hw_scan(local_to_hw(local),
3705                                              ssid, ssid_len);
3706                 if (!rc) {
3707                         local->sta_hw_scanning = 1;
3708                         local->scan_sdata = scan_sdata;
3709                 }
3710                 return rc;
3711         }
3712
3713         local->sta_sw_scanning = 1;
3714
3715         rcu_read_lock();
3716         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3717                 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
3718                         if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
3719                                 netif_tx_stop_all_queues(sdata->dev);
3720                                 ieee80211_send_nullfunc(local, sdata, 1);
3721                         }
3722                 } else
3723                         netif_tx_stop_all_queues(sdata->dev);
3724         }
3725         rcu_read_unlock();
3726
3727         if (ssid) {
3728                 local->scan_ssid_len = ssid_len;
3729                 memcpy(local->scan_ssid, ssid, ssid_len);
3730         } else
3731                 local->scan_ssid_len = 0;
3732         local->scan_state = SCAN_SET_CHANNEL;
3733         local->scan_channel_idx = 0;
3734         local->scan_band = IEEE80211_BAND_2GHZ;
3735         local->scan_sdata = scan_sdata;
3736
3737         netif_addr_lock_bh(local->mdev);
3738         local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3739         local->ops->configure_filter(local_to_hw(local),
3740                                      FIF_BCN_PRBRESP_PROMISC,
3741                                      &local->filter_flags,
3742                                      local->mdev->mc_count,
3743                                      local->mdev->mc_list);
3744         netif_addr_unlock_bh(local->mdev);
3745
3746         /* TODO: start scan as soon as all nullfunc frames are ACKed */
3747         queue_delayed_work(local->hw.workqueue, &local->scan_work,
3748                            IEEE80211_CHANNEL_TIME);
3749
3750         return 0;
3751 }
3752
3753
3754 int ieee80211_sta_req_scan(struct ieee80211_sub_if_data *sdata, u8 *ssid, size_t ssid_len)
3755 {
3756         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3757         struct ieee80211_local *local = sdata->local;
3758
3759         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3760                 return ieee80211_sta_start_scan(sdata, ssid, ssid_len);
3761
3762         if (local->sta_sw_scanning || local->sta_hw_scanning) {
3763                 if (local->scan_sdata == sdata)
3764                         return 0;
3765                 return -EBUSY;
3766         }
3767
3768         ifsta->scan_ssid_len = ssid_len;
3769         if (ssid_len)
3770                 memcpy(ifsta->scan_ssid, ssid, ssid_len);
3771         set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3772         queue_work(local->hw.workqueue, &ifsta->work);
3773         return 0;
3774 }
3775
3776
3777 static void ieee80211_sta_add_scan_ies(struct iw_request_info *info,
3778                                        struct ieee80211_sta_bss *bss,
3779                                        char **current_ev, char *end_buf)
3780 {
3781         u8 *pos, *end, *next;
3782         struct iw_event iwe;
3783
3784         if (bss == NULL || bss->ies == NULL)
3785                 return;
3786
3787         /*
3788          * If needed, fragment the IEs buffer (at IE boundaries) into short
3789          * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
3790          */
3791         pos = bss->ies;
3792         end = pos + bss->ies_len;
3793
3794         while (end - pos > IW_GENERIC_IE_MAX) {
3795                 next = pos + 2 + pos[1];
3796                 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
3797                         next = next + 2 + next[1];
3798
3799                 memset(&iwe, 0, sizeof(iwe));
3800                 iwe.cmd = IWEVGENIE;
3801                 iwe.u.data.length = next - pos;
3802                 *current_ev = iwe_stream_add_point(info, *current_ev,
3803                                                    end_buf, &iwe, pos);
3804
3805                 pos = next;
3806         }
3807
3808         if (end > pos) {
3809                 memset(&iwe, 0, sizeof(iwe));
3810                 iwe.cmd = IWEVGENIE;
3811                 iwe.u.data.length = end - pos;
3812                 *current_ev = iwe_stream_add_point(info, *current_ev,
3813                                                    end_buf, &iwe, pos);
3814         }
3815 }
3816
3817
3818 static char *
3819 ieee80211_sta_scan_result(struct ieee80211_local *local,
3820                           struct iw_request_info *info,
3821                           struct ieee80211_sta_bss *bss,
3822                           char *current_ev, char *end_buf)
3823 {
3824         struct iw_event iwe;
3825         char *buf;
3826
3827         if (time_after(jiffies,
3828                        bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3829                 return current_ev;
3830
3831         memset(&iwe, 0, sizeof(iwe));
3832         iwe.cmd = SIOCGIWAP;
3833         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3834         memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3835         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
3836                                           IW_EV_ADDR_LEN);
3837
3838         memset(&iwe, 0, sizeof(iwe));
3839         iwe.cmd = SIOCGIWESSID;
3840         if (bss_mesh_cfg(bss)) {
3841                 iwe.u.data.length = bss_mesh_id_len(bss);
3842                 iwe.u.data.flags = 1;
3843                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
3844                                                   &iwe, bss_mesh_id(bss));
3845         } else {
3846                 iwe.u.data.length = bss->ssid_len;
3847                 iwe.u.data.flags = 1;
3848                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
3849                                                   &iwe, bss->ssid);
3850         }
3851
3852         if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
3853             || bss_mesh_cfg(bss)) {
3854                 memset(&iwe, 0, sizeof(iwe));
3855                 iwe.cmd = SIOCGIWMODE;
3856                 if (bss_mesh_cfg(bss))
3857                         iwe.u.mode = IW_MODE_MESH;
3858                 else if (bss->capability & WLAN_CAPABILITY_ESS)
3859                         iwe.u.mode = IW_MODE_MASTER;
3860                 else
3861                         iwe.u.mode = IW_MODE_ADHOC;
3862                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
3863                                                   &iwe, IW_EV_UINT_LEN);
3864         }
3865
3866         memset(&iwe, 0, sizeof(iwe));
3867         iwe.cmd = SIOCGIWFREQ;
3868         iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
3869         iwe.u.freq.e = 0;
3870         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
3871                                           IW_EV_FREQ_LEN);
3872
3873         memset(&iwe, 0, sizeof(iwe));
3874         iwe.cmd = SIOCGIWFREQ;
3875         iwe.u.freq.m = bss->freq;
3876         iwe.u.freq.e = 6;
3877         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
3878                                           IW_EV_FREQ_LEN);
3879         memset(&iwe, 0, sizeof(iwe));
3880         iwe.cmd = IWEVQUAL;
3881         iwe.u.qual.qual = bss->qual;
3882         iwe.u.qual.level = bss->signal;
3883         iwe.u.qual.noise = bss->noise;
3884         iwe.u.qual.updated = local->wstats_flags;
3885         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
3886                                           IW_EV_QUAL_LEN);
3887
3888         memset(&iwe, 0, sizeof(iwe));
3889         iwe.cmd = SIOCGIWENCODE;
3890         if (bss->capability & WLAN_CAPABILITY_PRIVACY)
3891                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3892         else
3893                 iwe.u.data.flags = IW_ENCODE_DISABLED;
3894         iwe.u.data.length = 0;
3895         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
3896                                           &iwe, "");
3897
3898         ieee80211_sta_add_scan_ies(info, bss, &current_ev, end_buf);
3899
3900         if (bss->supp_rates_len > 0) {
3901                 /* display all supported rates in readable format */
3902                 char *p = current_ev + iwe_stream_lcp_len(info);
3903                 int i;
3904
3905                 memset(&iwe, 0, sizeof(iwe));
3906                 iwe.cmd = SIOCGIWRATE;
3907                 /* Those two flags are ignored... */
3908                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3909
3910                 for (i = 0; i < bss->supp_rates_len; i++) {
3911                         iwe.u.bitrate.value = ((bss->supp_rates[i] &
3912                                                         0x7f) * 500000);
3913                         p = iwe_stream_add_value(info, current_ev, p,
3914                                         end_buf, &iwe, IW_EV_PARAM_LEN);
3915                 }
3916                 current_ev = p;
3917         }
3918
3919         buf = kmalloc(30, GFP_ATOMIC);
3920         if (buf) {
3921                 memset(&iwe, 0, sizeof(iwe));
3922                 iwe.cmd = IWEVCUSTOM;
3923                 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3924                 iwe.u.data.length = strlen(buf);
3925                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
3926                                                   &iwe, buf);
3927                 memset(&iwe, 0, sizeof(iwe));
3928                 iwe.cmd = IWEVCUSTOM;
3929                 sprintf(buf, " Last beacon: %dms ago",
3930                         jiffies_to_msecs(jiffies - bss->last_update));
3931                 iwe.u.data.length = strlen(buf);
3932                 current_ev = iwe_stream_add_point(info, current_ev,
3933                                                   end_buf, &iwe, buf);
3934                 kfree(buf);
3935         }
3936
3937         if (bss_mesh_cfg(bss)) {
3938                 u8 *cfg = bss_mesh_cfg(bss);
3939                 buf = kmalloc(50, GFP_ATOMIC);
3940                 if (buf) {
3941                         memset(&iwe, 0, sizeof(iwe));
3942                         iwe.cmd = IWEVCUSTOM;
3943                         sprintf(buf, "Mesh network (version %d)", cfg[0]);
3944                         iwe.u.data.length = strlen(buf);
3945                         current_ev = iwe_stream_add_point(info, current_ev,
3946                                                           end_buf,
3947                                                           &iwe, buf);
3948                         sprintf(buf, "Path Selection Protocol ID: "
3949                                 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
3950                                                         cfg[4]);
3951                         iwe.u.data.length = strlen(buf);
3952                         current_ev = iwe_stream_add_point(info, current_ev,
3953                                                           end_buf,
3954                                                           &iwe, buf);
3955                         sprintf(buf, "Path Selection Metric ID: "
3956                                 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
3957                                                         cfg[8]);
3958                         iwe.u.data.length = strlen(buf);
3959                         current_ev = iwe_stream_add_point(info, current_ev,
3960                                                           end_buf,
3961                                                           &iwe, buf);
3962                         sprintf(buf, "Congestion Control Mode ID: "
3963                                 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
3964                                                         cfg[11], cfg[12]);
3965                         iwe.u.data.length = strlen(buf);
3966                         current_ev = iwe_stream_add_point(info, current_ev,
3967                                                           end_buf,
3968                                                           &iwe, buf);
3969                         sprintf(buf, "Channel Precedence: "
3970                                 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
3971                                                         cfg[15], cfg[16]);
3972                         iwe.u.data.length = strlen(buf);
3973                         current_ev = iwe_stream_add_point(info, current_ev,
3974                                                           end_buf,
3975                                                           &iwe, buf);
3976                         kfree(buf);
3977                 }
3978         }
3979
3980         return current_ev;
3981 }
3982
3983
3984 int ieee80211_sta_scan_results(struct ieee80211_local *local,
3985                                struct iw_request_info *info,
3986                                char *buf, size_t len)
3987 {
3988         char *current_ev = buf;
3989         char *end_buf = buf + len;
3990         struct ieee80211_sta_bss *bss;
3991
3992         spin_lock_bh(&local->sta_bss_lock);
3993         list_for_each_entry(bss, &local->sta_bss_list, list) {
3994                 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3995                         spin_unlock_bh(&local->sta_bss_lock);
3996                         return -E2BIG;
3997                 }
3998                 current_ev = ieee80211_sta_scan_result(local, info, bss,
3999                                                        current_ev, end_buf);
4000         }
4001         spin_unlock_bh(&local->sta_bss_lock);
4002         return current_ev - buf;
4003 }
4004
4005
4006 int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
4007 {
4008         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4009
4010         kfree(ifsta->extra_ie);
4011         if (len == 0) {
4012                 ifsta->extra_ie = NULL;
4013                 ifsta->extra_ie_len = 0;
4014                 return 0;
4015         }
4016         ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4017         if (!ifsta->extra_ie) {
4018                 ifsta->extra_ie_len = 0;
4019                 return -ENOMEM;
4020         }
4021         memcpy(ifsta->extra_ie, ie, len);
4022         ifsta->extra_ie_len = len;
4023         return 0;
4024 }
4025
4026
4027 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
4028                                         struct sk_buff *skb, u8 *bssid,
4029                                         u8 *addr, u64 supp_rates)
4030 {
4031         struct ieee80211_local *local = sdata->local;
4032         struct sta_info *sta;
4033         DECLARE_MAC_BUF(mac);
4034         int band = local->hw.conf.channel->band;
4035
4036         /* TODO: Could consider removing the least recently used entry and
4037          * allow new one to be added. */
4038         if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4039                 if (net_ratelimit()) {
4040                         printk(KERN_DEBUG "%s: No room for a new IBSS STA "
4041                                "entry %s\n", sdata->dev->name, print_mac(mac, addr));
4042                 }
4043                 return NULL;
4044         }
4045
4046         if (compare_ether_addr(bssid, sdata->u.sta.bssid))
4047                 return NULL;
4048
4049 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4050         printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
4051                wiphy_name(local->hw.wiphy), print_mac(mac, addr), sdata->dev->name);
4052 #endif
4053
4054         sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4055         if (!sta)
4056                 return NULL;
4057
4058         set_sta_flags(sta, WLAN_STA_AUTHORIZED);
4059
4060         /* make sure mandatory rates are always added */
4061         sta->supp_rates[band] = supp_rates |
4062                         ieee80211_sta_get_mandatory_rates(local, band);
4063
4064         rate_control_rate_init(sta, local);
4065
4066         if (sta_info_insert(sta))
4067                 return NULL;
4068
4069         return sta;
4070 }
4071
4072
4073 static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
4074                                      struct ieee80211_if_sta *ifsta)
4075 {
4076         struct ieee80211_local *local = sdata->local;
4077         struct ieee80211_sta_bss *bss, *selected = NULL;
4078         int top_rssi = 0, freq;
4079
4080         spin_lock_bh(&local->sta_bss_lock);
4081         freq = local->oper_channel->center_freq;
4082         list_for_each_entry(bss, &local->sta_bss_list, list) {
4083                 if (!(bss->capability & WLAN_CAPABILITY_ESS))
4084                         continue;
4085
4086                 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
4087                         IEEE80211_STA_AUTO_BSSID_SEL |
4088                         IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
4089                     (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
4090                      !!sdata->default_key))
4091                         continue;
4092
4093                 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
4094                     bss->freq != freq)
4095                         continue;
4096
4097                 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
4098                     memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
4099                         continue;
4100
4101                 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
4102                     !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
4103                         continue;
4104
4105                 if (!selected || top_rssi < bss->signal) {
4106                         selected = bss;
4107                         top_rssi = bss->signal;
4108                 }
4109         }
4110         if (selected)
4111                 atomic_inc(&selected->users);
4112         spin_unlock_bh(&local->sta_bss_lock);
4113
4114         if (selected) {
4115                 ieee80211_set_freq(sdata, selected->freq);
4116                 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
4117                         ieee80211_sta_set_ssid(sdata, selected->ssid,
4118                                                selected->ssid_len);
4119                 ieee80211_sta_set_bssid(sdata, selected->bssid);
4120                 ieee80211_sta_def_wmm_params(sdata, selected, 0);
4121
4122                 /* Send out direct probe if no probe resp was received or
4123                  * the one we have is outdated
4124                  */
4125                 if (!selected->last_probe_resp ||
4126                     time_after(jiffies, selected->last_probe_resp
4127                                         + IEEE80211_SCAN_RESULT_EXPIRE))
4128                         ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
4129                 else
4130                         ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
4131
4132                 ieee80211_rx_bss_put(local, selected);
4133                 ieee80211_sta_reset_auth(sdata, ifsta);
4134                 return 0;
4135         } else {
4136                 if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
4137                         ifsta->assoc_scan_tries++;
4138                         if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
4139                                 ieee80211_sta_start_scan(sdata, NULL, 0);
4140                         else
4141                                 ieee80211_sta_start_scan(sdata, ifsta->ssid,
4142                                                          ifsta->ssid_len);
4143                         ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
4144                         set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
4145                 } else
4146                         ifsta->state = IEEE80211_STA_MLME_DISABLED;
4147         }
4148         return -1;
4149 }
4150
4151
4152 int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
4153 {
4154         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4155
4156         printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
4157                sdata->dev->name, reason);
4158
4159         if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4160             sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
4161                 return -EINVAL;
4162
4163         ieee80211_set_disassoc(sdata, ifsta, true, true, reason);
4164         return 0;
4165 }
4166
4167
4168 int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
4169 {
4170         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4171
4172         printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
4173                sdata->dev->name, reason);
4174
4175         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4176                 return -EINVAL;
4177
4178         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
4179                 return -1;
4180
4181         ieee80211_set_disassoc(sdata, ifsta, false, true, reason);
4182         return 0;
4183 }
4184
4185 void ieee80211_notify_mac(struct ieee80211_hw *hw,
4186                           enum ieee80211_notification_types  notif_type)
4187 {
4188         struct ieee80211_local *local = hw_to_local(hw);
4189         struct ieee80211_sub_if_data *sdata;
4190
4191         switch (notif_type) {
4192         case IEEE80211_NOTIFY_RE_ASSOC:
4193                 rcu_read_lock();
4194                 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4195                         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4196                                 continue;
4197
4198                         ieee80211_sta_req_auth(sdata, &sdata->u.sta);
4199                 }
4200                 rcu_read_unlock();
4201                 break;
4202         }
4203 }
4204 EXPORT_SYMBOL(ieee80211_notify_mac);
4205
4206 void ieee80211_sta_work(struct work_struct *work)
4207 {
4208         struct ieee80211_sub_if_data *sdata =
4209                 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
4210         struct ieee80211_local *local = sdata->local;
4211         struct ieee80211_if_sta *ifsta;
4212         struct sk_buff *skb;
4213
4214         if (!netif_running(sdata->dev))
4215                 return;
4216
4217         if (local->sta_sw_scanning || local->sta_hw_scanning)
4218                 return;
4219
4220         if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4221                     sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
4222                     sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
4223                 return;
4224         ifsta = &sdata->u.sta;
4225
4226         while ((skb = skb_dequeue(&ifsta->skb_queue)))
4227                 ieee80211_sta_rx_queued_mgmt(sdata, skb);
4228
4229 #ifdef CONFIG_MAC80211_MESH
4230         if (ifsta->preq_queue_len &&
4231             time_after(jiffies,
4232                        ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
4233                 mesh_path_start_discovery(sdata);
4234 #endif
4235
4236         if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
4237             ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
4238             ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
4239             test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
4240                 if (ifsta->scan_ssid_len)
4241                         ieee80211_sta_start_scan(sdata, ifsta->scan_ssid, ifsta->scan_ssid_len);
4242                 else
4243                         ieee80211_sta_start_scan(sdata, NULL, 0);
4244                 return;
4245         }
4246
4247         if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
4248                 if (ieee80211_sta_config_auth(sdata, ifsta))
4249                         return;
4250                 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
4251         } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
4252                 return;
4253
4254         switch (ifsta->state) {
4255         case IEEE80211_STA_MLME_DISABLED:
4256                 break;
4257         case IEEE80211_STA_MLME_DIRECT_PROBE:
4258                 ieee80211_direct_probe(sdata, ifsta);
4259                 break;
4260         case IEEE80211_STA_MLME_AUTHENTICATE:
4261                 ieee80211_authenticate(sdata, ifsta);
4262                 break;
4263         case IEEE80211_STA_MLME_ASSOCIATE:
4264                 ieee80211_associate(sdata, ifsta);
4265                 break;
4266         case IEEE80211_STA_MLME_ASSOCIATED:
4267                 ieee80211_associated(sdata, ifsta);
4268                 break;
4269         case IEEE80211_STA_MLME_IBSS_SEARCH:
4270                 ieee80211_sta_find_ibss(sdata, ifsta);
4271                 break;
4272         case IEEE80211_STA_MLME_IBSS_JOINED:
4273                 ieee80211_sta_merge_ibss(sdata, ifsta);
4274                 break;
4275 #ifdef CONFIG_MAC80211_MESH
4276         case IEEE80211_STA_MLME_MESH_UP:
4277                 ieee80211_mesh_housekeeping(sdata, ifsta);
4278                 break;
4279 #endif
4280         default:
4281                 WARN_ON(1);
4282                 break;
4283         }
4284
4285         if (ieee80211_privacy_mismatch(sdata, ifsta)) {
4286                 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
4287                        "mixed-cell disabled - disassociate\n", sdata->dev->name);
4288
4289                 ieee80211_set_disassoc(sdata, ifsta, false, true,
4290                                         WLAN_REASON_UNSPECIFIED);
4291         }
4292 }