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