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