mac80211: move some HT code out of mlme.c
[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 #include <linux/delay.h>
15 #include <linux/if_ether.h>
16 #include <linux/skbuff.h>
17 #include <linux/netdevice.h>
18 #include <linux/if_arp.h>
19 #include <linux/wireless.h>
20 #include <linux/random.h>
21 #include <linux/etherdevice.h>
22 #include <linux/rtnetlink.h>
23 #include <net/iw_handler.h>
24 #include <net/mac80211.h>
25
26 #include "ieee80211_i.h"
27 #include "rate.h"
28 #include "led.h"
29 #include "mesh.h"
30
31 #define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
32 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
33 #define IEEE80211_AUTH_MAX_TRIES 3
34 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
35 #define IEEE80211_ASSOC_MAX_TRIES 3
36 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
37 #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
38 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
39 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
40 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
41 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
42 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
43
44 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
45 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
46 #define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
47
48 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
49
50
51 /* utils */
52 static int ecw2cw(int ecw)
53 {
54         return (1 << ecw) - 1;
55 }
56
57 static u8 *ieee80211_bss_get_ie(struct ieee80211_sta_bss *bss, u8 ie)
58 {
59         u8 *end, *pos;
60
61         pos = bss->ies;
62         if (pos == NULL)
63                 return NULL;
64         end = pos + bss->ies_len;
65
66         while (pos + 1 < end) {
67                 if (pos + 2 + pos[1] > end)
68                         break;
69                 if (pos[0] == ie)
70                         return pos;
71                 pos += 2 + pos[1];
72         }
73
74         return NULL;
75 }
76
77 /* frame sending functions */
78 void ieee80211_sta_tx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
79                       int encrypt)
80 {
81         skb->dev = sdata->local->mdev;
82         skb_set_mac_header(skb, 0);
83         skb_set_network_header(skb, 0);
84         skb_set_transport_header(skb, 0);
85
86         skb->iif = sdata->dev->ifindex;
87         skb->do_not_encrypt = !encrypt;
88
89         dev_queue_xmit(skb);
90 }
91
92 static void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
93                                 struct ieee80211_if_sta *ifsta,
94                                 int transaction, u8 *extra, size_t extra_len,
95                                 int encrypt)
96 {
97         struct ieee80211_local *local = sdata->local;
98         struct sk_buff *skb;
99         struct ieee80211_mgmt *mgmt;
100
101         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
102                             sizeof(*mgmt) + 6 + extra_len);
103         if (!skb) {
104                 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
105                        "frame\n", sdata->dev->name);
106                 return;
107         }
108         skb_reserve(skb, local->hw.extra_tx_headroom);
109
110         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
111         memset(mgmt, 0, 24 + 6);
112         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
113                                           IEEE80211_STYPE_AUTH);
114         if (encrypt)
115                 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
116         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
117         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
118         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
119         mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
120         mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
121         ifsta->auth_transaction = transaction + 1;
122         mgmt->u.auth.status_code = cpu_to_le16(0);
123         if (extra)
124                 memcpy(skb_put(skb, extra_len), extra, extra_len);
125
126         ieee80211_sta_tx(sdata, skb, encrypt);
127 }
128
129 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
130                               u8 *ssid, size_t ssid_len)
131 {
132         struct ieee80211_local *local = sdata->local;
133         struct ieee80211_supported_band *sband;
134         struct sk_buff *skb;
135         struct ieee80211_mgmt *mgmt;
136         u8 *pos, *supp_rates, *esupp_rates = NULL;
137         int i;
138
139         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
140         if (!skb) {
141                 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
142                        "request\n", sdata->dev->name);
143                 return;
144         }
145         skb_reserve(skb, local->hw.extra_tx_headroom);
146
147         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
148         memset(mgmt, 0, 24);
149         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
150                                           IEEE80211_STYPE_PROBE_REQ);
151         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
152         if (dst) {
153                 memcpy(mgmt->da, dst, ETH_ALEN);
154                 memcpy(mgmt->bssid, dst, ETH_ALEN);
155         } else {
156                 memset(mgmt->da, 0xff, ETH_ALEN);
157                 memset(mgmt->bssid, 0xff, ETH_ALEN);
158         }
159         pos = skb_put(skb, 2 + ssid_len);
160         *pos++ = WLAN_EID_SSID;
161         *pos++ = ssid_len;
162         memcpy(pos, ssid, ssid_len);
163
164         supp_rates = skb_put(skb, 2);
165         supp_rates[0] = WLAN_EID_SUPP_RATES;
166         supp_rates[1] = 0;
167         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
168
169         for (i = 0; i < sband->n_bitrates; i++) {
170                 struct ieee80211_rate *rate = &sband->bitrates[i];
171                 if (esupp_rates) {
172                         pos = skb_put(skb, 1);
173                         esupp_rates[1]++;
174                 } else if (supp_rates[1] == 8) {
175                         esupp_rates = skb_put(skb, 3);
176                         esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
177                         esupp_rates[1] = 1;
178                         pos = &esupp_rates[2];
179                 } else {
180                         pos = skb_put(skb, 1);
181                         supp_rates[1]++;
182                 }
183                 *pos = rate->bitrate / 5;
184         }
185
186         ieee80211_sta_tx(sdata, skb, 0);
187 }
188
189 /* MLME */
190 static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
191                                          struct ieee80211_sta_bss *bss,
192                                          int ibss)
193 {
194         struct ieee80211_local *local = sdata->local;
195         int i, have_higher_than_11mbit = 0;
196
197
198         /* cf. IEEE 802.11 9.2.12 */
199         for (i = 0; i < bss->supp_rates_len; i++)
200                 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
201                         have_higher_than_11mbit = 1;
202
203         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
204             have_higher_than_11mbit)
205                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
206         else
207                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
208
209
210         if (local->ops->conf_tx) {
211                 struct ieee80211_tx_queue_params qparam;
212
213                 memset(&qparam, 0, sizeof(qparam));
214
215                 qparam.aifs = 2;
216
217                 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
218                     !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
219                         qparam.cw_min = 31;
220                 else
221                         qparam.cw_min = 15;
222
223                 qparam.cw_max = 1023;
224                 qparam.txop = 0;
225
226                 for (i = 0; i < local_to_hw(local)->queues; i++)
227                         local->ops->conf_tx(local_to_hw(local), i, &qparam);
228         }
229 }
230
231 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
232                                      struct ieee80211_if_sta *ifsta,
233                                      u8 *wmm_param, size_t wmm_param_len)
234 {
235         struct ieee80211_tx_queue_params params;
236         size_t left;
237         int count;
238         u8 *pos;
239
240         if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
241                 return;
242
243         if (!wmm_param)
244                 return;
245
246         if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
247                 return;
248         count = wmm_param[6] & 0x0f;
249         if (count == ifsta->wmm_last_param_set)
250                 return;
251         ifsta->wmm_last_param_set = count;
252
253         pos = wmm_param + 8;
254         left = wmm_param_len - 8;
255
256         memset(&params, 0, sizeof(params));
257
258         if (!local->ops->conf_tx)
259                 return;
260
261         local->wmm_acm = 0;
262         for (; left >= 4; left -= 4, pos += 4) {
263                 int aci = (pos[0] >> 5) & 0x03;
264                 int acm = (pos[0] >> 4) & 0x01;
265                 int queue;
266
267                 switch (aci) {
268                 case 1:
269                         queue = 3;
270                         if (acm)
271                                 local->wmm_acm |= BIT(0) | BIT(3);
272                         break;
273                 case 2:
274                         queue = 1;
275                         if (acm)
276                                 local->wmm_acm |= BIT(4) | BIT(5);
277                         break;
278                 case 3:
279                         queue = 0;
280                         if (acm)
281                                 local->wmm_acm |= BIT(6) | BIT(7);
282                         break;
283                 case 0:
284                 default:
285                         queue = 2;
286                         if (acm)
287                                 local->wmm_acm |= BIT(1) | BIT(2);
288                         break;
289                 }
290
291                 params.aifs = pos[0] & 0x0f;
292                 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
293                 params.cw_min = ecw2cw(pos[1] & 0x0f);
294                 params.txop = get_unaligned_le16(pos + 2);
295 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
296                 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
297                        "cWmin=%d cWmax=%d txop=%d\n",
298                        local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
299                        params.cw_max, params.txop);
300 #endif
301                 /* TODO: handle ACM (block TX, fallback to next lowest allowed
302                  * AC for now) */
303                 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
304                         printk(KERN_DEBUG "%s: failed to set TX queue "
305                                "parameters for queue %d\n", local->mdev->name, queue);
306                 }
307         }
308 }
309
310 static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
311                                            bool use_protection,
312                                            bool use_short_preamble)
313 {
314         struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
315 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
316         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
317         DECLARE_MAC_BUF(mac);
318 #endif
319         u32 changed = 0;
320
321         if (use_protection != bss_conf->use_cts_prot) {
322 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
323                 if (net_ratelimit()) {
324                         printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
325                                "%s)\n",
326                                sdata->dev->name,
327                                use_protection ? "enabled" : "disabled",
328                                print_mac(mac, ifsta->bssid));
329                 }
330 #endif
331                 bss_conf->use_cts_prot = use_protection;
332                 changed |= BSS_CHANGED_ERP_CTS_PROT;
333         }
334
335         if (use_short_preamble != bss_conf->use_short_preamble) {
336 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
337                 if (net_ratelimit()) {
338                         printk(KERN_DEBUG "%s: switched to %s barker preamble"
339                                " (BSSID=%s)\n",
340                                sdata->dev->name,
341                                use_short_preamble ? "short" : "long",
342                                print_mac(mac, ifsta->bssid));
343                 }
344 #endif
345                 bss_conf->use_short_preamble = use_short_preamble;
346                 changed |= BSS_CHANGED_ERP_PREAMBLE;
347         }
348
349         return changed;
350 }
351
352 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
353                                    u8 erp_value)
354 {
355         bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
356         bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
357
358         return ieee80211_handle_protect_preamb(sdata,
359                         use_protection, use_short_preamble);
360 }
361
362 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
363                                            struct ieee80211_sta_bss *bss)
364 {
365         u32 changed = 0;
366
367         if (bss->has_erp_value)
368                 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
369         else {
370                 u16 capab = bss->capability;
371                 changed |= ieee80211_handle_protect_preamb(sdata, false,
372                                 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
373         }
374
375         return changed;
376 }
377
378 static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata,
379                                         struct ieee80211_if_sta *ifsta)
380 {
381         union iwreq_data wrqu;
382         memset(&wrqu, 0, sizeof(wrqu));
383         if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
384                 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
385         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
386         wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
387 }
388
389 static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
390                                          struct ieee80211_if_sta *ifsta)
391 {
392         union iwreq_data wrqu;
393
394         if (ifsta->assocreq_ies) {
395                 memset(&wrqu, 0, sizeof(wrqu));
396                 wrqu.data.length = ifsta->assocreq_ies_len;
397                 wireless_send_event(sdata->dev, IWEVASSOCREQIE, &wrqu,
398                                     ifsta->assocreq_ies);
399         }
400         if (ifsta->assocresp_ies) {
401                 memset(&wrqu, 0, sizeof(wrqu));
402                 wrqu.data.length = ifsta->assocresp_ies_len;
403                 wireless_send_event(sdata->dev, IWEVASSOCRESPIE, &wrqu,
404                                     ifsta->assocresp_ies);
405         }
406 }
407
408
409 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
410                                      struct ieee80211_if_sta *ifsta)
411 {
412         struct ieee80211_local *local = sdata->local;
413         struct ieee80211_conf *conf = &local_to_hw(local)->conf;
414         u32 changed = BSS_CHANGED_ASSOC;
415
416         struct ieee80211_sta_bss *bss;
417
418         ifsta->flags |= IEEE80211_STA_ASSOCIATED;
419
420         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
421                 return;
422
423         bss = ieee80211_rx_bss_get(local, ifsta->bssid,
424                                    conf->channel->center_freq,
425                                    ifsta->ssid, ifsta->ssid_len);
426         if (bss) {
427                 /* set timing information */
428                 sdata->bss_conf.beacon_int = bss->beacon_int;
429                 sdata->bss_conf.timestamp = bss->timestamp;
430                 sdata->bss_conf.dtim_period = bss->dtim_period;
431
432                 changed |= ieee80211_handle_bss_capability(sdata, bss);
433
434                 ieee80211_rx_bss_put(local, bss);
435         }
436
437         if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
438                 changed |= BSS_CHANGED_HT;
439                 sdata->bss_conf.assoc_ht = 1;
440                 sdata->bss_conf.ht_conf = &conf->ht_conf;
441                 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
442         }
443
444         ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
445         memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
446         ieee80211_sta_send_associnfo(sdata, ifsta);
447
448         ifsta->last_probe = jiffies;
449         ieee80211_led_assoc(local, 1);
450
451         sdata->bss_conf.assoc = 1;
452         ieee80211_bss_info_change_notify(sdata, changed);
453
454         netif_tx_start_all_queues(sdata->dev);
455         netif_carrier_on(sdata->dev);
456
457         ieee80211_sta_send_apinfo(sdata, ifsta);
458 }
459
460 static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
461                                    struct ieee80211_if_sta *ifsta)
462 {
463         DECLARE_MAC_BUF(mac);
464
465         ifsta->direct_probe_tries++;
466         if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
467                 printk(KERN_DEBUG "%s: direct probe to AP %s timed out\n",
468                        sdata->dev->name, print_mac(mac, ifsta->bssid));
469                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
470                 return;
471         }
472
473         printk(KERN_DEBUG "%s: direct probe to AP %s try %d\n",
474                         sdata->dev->name, print_mac(mac, ifsta->bssid),
475                         ifsta->direct_probe_tries);
476
477         ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
478
479         set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
480
481         /* Direct probe is sent to broadcast address as some APs
482          * will not answer to direct packet in unassociated state.
483          */
484         ieee80211_send_probe_req(sdata, NULL,
485                                  ifsta->ssid, ifsta->ssid_len);
486
487         mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
488 }
489
490
491 static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
492                                    struct ieee80211_if_sta *ifsta)
493 {
494         DECLARE_MAC_BUF(mac);
495
496         ifsta->auth_tries++;
497         if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
498                 printk(KERN_DEBUG "%s: authentication with AP %s"
499                        " timed out\n",
500                        sdata->dev->name, print_mac(mac, ifsta->bssid));
501                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
502                 return;
503         }
504
505         ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
506         printk(KERN_DEBUG "%s: authenticate with AP %s\n",
507                sdata->dev->name, print_mac(mac, ifsta->bssid));
508
509         ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
510
511         mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
512 }
513
514 static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
515                                       struct ieee80211_supported_band *sband,
516                                       u64 *rates)
517 {
518         int i, j, count;
519         *rates = 0;
520         count = 0;
521         for (i = 0; i < bss->supp_rates_len; i++) {
522                 int rate = (bss->supp_rates[i] & 0x7F) * 5;
523
524                 for (j = 0; j < sband->n_bitrates; j++)
525                         if (sband->bitrates[j].bitrate == rate) {
526                                 *rates |= BIT(j);
527                                 count++;
528                                 break;
529                         }
530         }
531
532         return count;
533 }
534
535 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
536                                  struct ieee80211_if_sta *ifsta)
537 {
538         struct ieee80211_local *local = sdata->local;
539         struct sk_buff *skb;
540         struct ieee80211_mgmt *mgmt;
541         u8 *pos, *ies, *ht_add_ie;
542         int i, len, count, rates_len, supp_rates_len;
543         u16 capab;
544         struct ieee80211_sta_bss *bss;
545         int wmm = 0;
546         struct ieee80211_supported_band *sband;
547         u64 rates = 0;
548
549         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
550                             sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
551                             ifsta->ssid_len);
552         if (!skb) {
553                 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
554                        "frame\n", sdata->dev->name);
555                 return;
556         }
557         skb_reserve(skb, local->hw.extra_tx_headroom);
558
559         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
560
561         capab = ifsta->capab;
562
563         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
564                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
565                         capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
566                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
567                         capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
568         }
569
570         bss = ieee80211_rx_bss_get(local, ifsta->bssid,
571                                    local->hw.conf.channel->center_freq,
572                                    ifsta->ssid, ifsta->ssid_len);
573         if (bss) {
574                 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
575                         capab |= WLAN_CAPABILITY_PRIVACY;
576                 if (bss->wmm_used)
577                         wmm = 1;
578
579                 /* get all rates supported by the device and the AP as
580                  * some APs don't like getting a superset of their rates
581                  * in the association request (e.g. D-Link DAP 1353 in
582                  * b-only mode) */
583                 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
584
585                 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
586                     (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
587                         capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
588
589                 ieee80211_rx_bss_put(local, bss);
590         } else {
591                 rates = ~0;
592                 rates_len = sband->n_bitrates;
593         }
594
595         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
596         memset(mgmt, 0, 24);
597         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
598         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
599         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
600
601         if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
602                 skb_put(skb, 10);
603                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
604                                                   IEEE80211_STYPE_REASSOC_REQ);
605                 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
606                 mgmt->u.reassoc_req.listen_interval =
607                                 cpu_to_le16(local->hw.conf.listen_interval);
608                 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
609                        ETH_ALEN);
610         } else {
611                 skb_put(skb, 4);
612                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
613                                                   IEEE80211_STYPE_ASSOC_REQ);
614                 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
615                 mgmt->u.reassoc_req.listen_interval =
616                                 cpu_to_le16(local->hw.conf.listen_interval);
617         }
618
619         /* SSID */
620         ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
621         *pos++ = WLAN_EID_SSID;
622         *pos++ = ifsta->ssid_len;
623         memcpy(pos, ifsta->ssid, ifsta->ssid_len);
624
625         /* add all rates which were marked to be used above */
626         supp_rates_len = rates_len;
627         if (supp_rates_len > 8)
628                 supp_rates_len = 8;
629
630         len = sband->n_bitrates;
631         pos = skb_put(skb, supp_rates_len + 2);
632         *pos++ = WLAN_EID_SUPP_RATES;
633         *pos++ = supp_rates_len;
634
635         count = 0;
636         for (i = 0; i < sband->n_bitrates; i++) {
637                 if (BIT(i) & rates) {
638                         int rate = sband->bitrates[i].bitrate;
639                         *pos++ = (u8) (rate / 5);
640                         if (++count == 8)
641                                 break;
642                 }
643         }
644
645         if (rates_len > count) {
646                 pos = skb_put(skb, rates_len - count + 2);
647                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
648                 *pos++ = rates_len - count;
649
650                 for (i++; i < sband->n_bitrates; i++) {
651                         if (BIT(i) & rates) {
652                                 int rate = sband->bitrates[i].bitrate;
653                                 *pos++ = (u8) (rate / 5);
654                         }
655                 }
656         }
657
658         if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
659                 /* 1. power capabilities */
660                 pos = skb_put(skb, 4);
661                 *pos++ = WLAN_EID_PWR_CAPABILITY;
662                 *pos++ = 2;
663                 *pos++ = 0; /* min tx power */
664                 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
665
666                 /* 2. supported channels */
667                 /* TODO: get this in reg domain format */
668                 pos = skb_put(skb, 2 * sband->n_channels + 2);
669                 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
670                 *pos++ = 2 * sband->n_channels;
671                 for (i = 0; i < sband->n_channels; i++) {
672                         *pos++ = ieee80211_frequency_to_channel(
673                                         sband->channels[i].center_freq);
674                         *pos++ = 1; /* one channel in the subband*/
675                 }
676         }
677
678         if (ifsta->extra_ie) {
679                 pos = skb_put(skb, ifsta->extra_ie_len);
680                 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
681         }
682
683         if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
684                 pos = skb_put(skb, 9);
685                 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
686                 *pos++ = 7; /* len */
687                 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
688                 *pos++ = 0x50;
689                 *pos++ = 0xf2;
690                 *pos++ = 2; /* WME */
691                 *pos++ = 0; /* WME info */
692                 *pos++ = 1; /* WME ver */
693                 *pos++ = 0;
694         }
695
696         /* wmm support is a must to HT */
697         if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
698             sband->ht_info.ht_supported &&
699             (ht_add_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_EXTRA_INFO))) {
700                 struct ieee80211_ht_addt_info *ht_add_info =
701                         (struct ieee80211_ht_addt_info *)ht_add_ie;
702                 u16 cap = sband->ht_info.cap;
703                 __le16 tmp;
704                 u32 flags = local->hw.conf.channel->flags;
705
706                 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
707                 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
708                         if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
709                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
710                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
711                         }
712                         break;
713                 case IEEE80211_HT_IE_CHA_SEC_BELOW:
714                         if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
715                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
716                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
717                         }
718                         break;
719                 }
720
721                 tmp = cpu_to_le16(cap);
722                 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
723                 *pos++ = WLAN_EID_HT_CAPABILITY;
724                 *pos++ = sizeof(struct ieee80211_ht_cap);
725                 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
726                 memcpy(pos, &tmp, sizeof(u16));
727                 pos += sizeof(u16);
728                 /* TODO: needs a define here for << 2 */
729                 *pos++ = sband->ht_info.ampdu_factor |
730                          (sband->ht_info.ampdu_density << 2);
731                 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
732         }
733
734         kfree(ifsta->assocreq_ies);
735         ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
736         ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
737         if (ifsta->assocreq_ies)
738                 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
739
740         ieee80211_sta_tx(sdata, skb, 0);
741 }
742
743
744 static void ieee80211_send_deauth(struct ieee80211_sub_if_data *sdata,
745                                   struct ieee80211_if_sta *ifsta, u16 reason)
746 {
747         struct ieee80211_local *local = sdata->local;
748         struct sk_buff *skb;
749         struct ieee80211_mgmt *mgmt;
750
751         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
752         if (!skb) {
753                 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
754                        "frame\n", sdata->dev->name);
755                 return;
756         }
757         skb_reserve(skb, local->hw.extra_tx_headroom);
758
759         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
760         memset(mgmt, 0, 24);
761         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
762         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
763         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
764         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
765                                           IEEE80211_STYPE_DEAUTH);
766         skb_put(skb, 2);
767         mgmt->u.deauth.reason_code = cpu_to_le16(reason);
768
769         ieee80211_sta_tx(sdata, skb, 0);
770 }
771
772 static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
773 {
774         if (!sdata || !sdata->default_key ||
775             sdata->default_key->conf.alg != ALG_WEP)
776                 return 0;
777         return 1;
778 }
779
780 static void ieee80211_send_disassoc(struct ieee80211_sub_if_data *sdata,
781                                     struct ieee80211_if_sta *ifsta, u16 reason)
782 {
783         struct ieee80211_local *local = sdata->local;
784         struct sk_buff *skb;
785         struct ieee80211_mgmt *mgmt;
786
787         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
788         if (!skb) {
789                 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
790                        "frame\n", sdata->dev->name);
791                 return;
792         }
793         skb_reserve(skb, local->hw.extra_tx_headroom);
794
795         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
796         memset(mgmt, 0, 24);
797         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
798         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
799         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
800         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
801                                           IEEE80211_STYPE_DISASSOC);
802         skb_put(skb, 2);
803         mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
804
805         ieee80211_sta_tx(sdata, skb, 0);
806 }
807
808 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
809                                    struct ieee80211_if_sta *ifsta, bool deauth,
810                                    bool self_disconnected, u16 reason)
811 {
812         struct ieee80211_local *local = sdata->local;
813         struct sta_info *sta;
814         u32 changed = BSS_CHANGED_ASSOC;
815
816         rcu_read_lock();
817
818         sta = sta_info_get(local, ifsta->bssid);
819         if (!sta) {
820                 rcu_read_unlock();
821                 return;
822         }
823
824         if (deauth) {
825                 ifsta->direct_probe_tries = 0;
826                 ifsta->auth_tries = 0;
827         }
828         ifsta->assoc_scan_tries = 0;
829         ifsta->assoc_tries = 0;
830
831         netif_tx_stop_all_queues(sdata->dev);
832         netif_carrier_off(sdata->dev);
833
834         ieee80211_sta_tear_down_BA_sessions(sdata, sta->addr);
835
836         if (self_disconnected) {
837                 if (deauth)
838                         ieee80211_send_deauth(sdata, ifsta, reason);
839                 else
840                         ieee80211_send_disassoc(sdata, ifsta, reason);
841         }
842
843         ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
844         changed |= ieee80211_reset_erp_info(sdata);
845
846         if (sdata->bss_conf.assoc_ht)
847                 changed |= BSS_CHANGED_HT;
848
849         sdata->bss_conf.assoc_ht = 0;
850         sdata->bss_conf.ht_conf = NULL;
851         sdata->bss_conf.ht_bss_conf = NULL;
852
853         ieee80211_led_assoc(local, 0);
854         sdata->bss_conf.assoc = 0;
855
856         ieee80211_sta_send_apinfo(sdata, ifsta);
857
858         if (self_disconnected)
859                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
860
861         sta_info_unlink(&sta);
862
863         rcu_read_unlock();
864
865         sta_info_destroy(sta);
866 }
867
868 static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
869                                       struct ieee80211_if_sta *ifsta)
870 {
871         struct ieee80211_local *local = sdata->local;
872         struct ieee80211_sta_bss *bss;
873         int bss_privacy;
874         int wep_privacy;
875         int privacy_invoked;
876
877         if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
878                 return 0;
879
880         bss = ieee80211_rx_bss_get(local, ifsta->bssid,
881                                    local->hw.conf.channel->center_freq,
882                                    ifsta->ssid, ifsta->ssid_len);
883         if (!bss)
884                 return 0;
885
886         bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
887         wep_privacy = !!ieee80211_sta_wep_configured(sdata);
888         privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
889
890         ieee80211_rx_bss_put(local, bss);
891
892         if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
893                 return 0;
894
895         return 1;
896 }
897
898 static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
899                                 struct ieee80211_if_sta *ifsta)
900 {
901         DECLARE_MAC_BUF(mac);
902
903         ifsta->assoc_tries++;
904         if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
905                 printk(KERN_DEBUG "%s: association with AP %s"
906                        " timed out\n",
907                        sdata->dev->name, print_mac(mac, ifsta->bssid));
908                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
909                 return;
910         }
911
912         ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
913         printk(KERN_DEBUG "%s: associate with AP %s\n",
914                sdata->dev->name, print_mac(mac, ifsta->bssid));
915         if (ieee80211_privacy_mismatch(sdata, ifsta)) {
916                 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
917                        "mixed-cell disabled - abort association\n", sdata->dev->name);
918                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
919                 return;
920         }
921
922         ieee80211_send_assoc(sdata, ifsta);
923
924         mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
925 }
926
927
928 static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
929                                  struct ieee80211_if_sta *ifsta)
930 {
931         struct ieee80211_local *local = sdata->local;
932         struct sta_info *sta;
933         int disassoc;
934         DECLARE_MAC_BUF(mac);
935
936         /* TODO: start monitoring current AP signal quality and number of
937          * missed beacons. Scan other channels every now and then and search
938          * for better APs. */
939         /* TODO: remove expired BSSes */
940
941         ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
942
943         rcu_read_lock();
944
945         sta = sta_info_get(local, ifsta->bssid);
946         if (!sta) {
947                 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
948                        sdata->dev->name, print_mac(mac, ifsta->bssid));
949                 disassoc = 1;
950         } else {
951                 disassoc = 0;
952                 if (time_after(jiffies,
953                                sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
954                         if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
955                                 printk(KERN_DEBUG "%s: No ProbeResp from "
956                                        "current AP %s - assume out of "
957                                        "range\n",
958                                        sdata->dev->name, print_mac(mac, ifsta->bssid));
959                                 disassoc = 1;
960                         } else
961                                 ieee80211_send_probe_req(sdata, ifsta->bssid,
962                                                          local->scan_ssid,
963                                                          local->scan_ssid_len);
964                         ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
965                 } else {
966                         ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
967                         if (time_after(jiffies, ifsta->last_probe +
968                                        IEEE80211_PROBE_INTERVAL)) {
969                                 ifsta->last_probe = jiffies;
970                                 ieee80211_send_probe_req(sdata, ifsta->bssid,
971                                                          ifsta->ssid,
972                                                          ifsta->ssid_len);
973                         }
974                 }
975         }
976
977         rcu_read_unlock();
978
979         if (disassoc)
980                 ieee80211_set_disassoc(sdata, ifsta, true, true,
981                                         WLAN_REASON_PREV_AUTH_NOT_VALID);
982         else
983                 mod_timer(&ifsta->timer, jiffies +
984                                       IEEE80211_MONITORING_INTERVAL);
985 }
986
987
988 static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
989                                      struct ieee80211_if_sta *ifsta)
990 {
991         printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
992         ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
993         ieee80211_associate(sdata, ifsta);
994 }
995
996
997 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
998                                      struct ieee80211_if_sta *ifsta,
999                                      struct ieee80211_mgmt *mgmt,
1000                                      size_t len)
1001 {
1002         u8 *pos;
1003         struct ieee802_11_elems elems;
1004
1005         pos = mgmt->u.auth.variable;
1006         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1007         if (!elems.challenge)
1008                 return;
1009         ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
1010                             elems.challenge_len + 2, 1);
1011 }
1012
1013 static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
1014                                         u8 dialog_token, u16 status, u16 policy,
1015                                         u16 buf_size, u16 timeout)
1016 {
1017         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1018         struct ieee80211_local *local = sdata->local;
1019         struct sk_buff *skb;
1020         struct ieee80211_mgmt *mgmt;
1021         u16 capab;
1022
1023         skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1024
1025         if (!skb) {
1026                 printk(KERN_DEBUG "%s: failed to allocate buffer "
1027                        "for addba resp frame\n", sdata->dev->name);
1028                 return;
1029         }
1030
1031         skb_reserve(skb, local->hw.extra_tx_headroom);
1032         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1033         memset(mgmt, 0, 24);
1034         memcpy(mgmt->da, da, ETH_ALEN);
1035         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1036         if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1037                 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
1038         else
1039                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1040         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1041                                           IEEE80211_STYPE_ACTION);
1042
1043         skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1044         mgmt->u.action.category = WLAN_CATEGORY_BACK;
1045         mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1046         mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1047
1048         capab = (u16)(policy << 1);     /* bit 1 aggregation policy */
1049         capab |= (u16)(tid << 2);       /* bit 5:2 TID number */
1050         capab |= (u16)(buf_size << 6);  /* bit 15:6 max size of aggregation */
1051
1052         mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1053         mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1054         mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1055
1056         ieee80211_sta_tx(sdata, skb, 0);
1057
1058         return;
1059 }
1060
1061 /*
1062  * After accepting the AddBA Request we activated a timer,
1063  * resetting it after each frame that arrives from the originator.
1064  * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1065  */
1066 static void sta_rx_agg_session_timer_expired(unsigned long data)
1067 {
1068         /* not an elegant detour, but there is no choice as the timer passes
1069          * only one argument, and various sta_info are needed here, so init
1070          * flow in sta_info_create gives the TID as data, while the timer_to_id
1071          * array gives the sta through container_of */
1072         u8 *ptid = (u8 *)data;
1073         u8 *timer_to_id = ptid - *ptid;
1074         struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1075                                          timer_to_tid[0]);
1076
1077 #ifdef CONFIG_MAC80211_HT_DEBUG
1078         printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1079 #endif
1080         ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->addr,
1081                                          (u16)*ptid, WLAN_BACK_TIMER,
1082                                          WLAN_REASON_QSTA_TIMEOUT);
1083 }
1084
1085 static void ieee80211_sta_process_addba_request(struct ieee80211_local *local,
1086                                                 struct ieee80211_mgmt *mgmt,
1087                                                 size_t len)
1088 {
1089         struct ieee80211_hw *hw = &local->hw;
1090         struct ieee80211_conf *conf = &hw->conf;
1091         struct sta_info *sta;
1092         struct tid_ampdu_rx *tid_agg_rx;
1093         u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1094         u8 dialog_token;
1095         int ret = -EOPNOTSUPP;
1096         DECLARE_MAC_BUF(mac);
1097
1098         rcu_read_lock();
1099
1100         sta = sta_info_get(local, mgmt->sa);
1101         if (!sta) {
1102                 rcu_read_unlock();
1103                 return;
1104         }
1105
1106         /* extract session parameters from addba request frame */
1107         dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1108         timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1109         start_seq_num =
1110                 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1111
1112         capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1113         ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1114         tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1115         buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1116
1117         status = WLAN_STATUS_REQUEST_DECLINED;
1118
1119         /* sanity check for incoming parameters:
1120          * check if configuration can support the BA policy
1121          * and if buffer size does not exceeds max value */
1122         if (((ba_policy != 1)
1123                 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1124                 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1125                 status = WLAN_STATUS_INVALID_QOS_PARAM;
1126 #ifdef CONFIG_MAC80211_HT_DEBUG
1127                 if (net_ratelimit())
1128                         printk(KERN_DEBUG "AddBA Req with bad params from "
1129                                 "%s on tid %u. policy %d, buffer size %d\n",
1130                                 print_mac(mac, mgmt->sa), tid, ba_policy,
1131                                 buf_size);
1132 #endif /* CONFIG_MAC80211_HT_DEBUG */
1133                 goto end_no_lock;
1134         }
1135         /* determine default buffer size */
1136         if (buf_size == 0) {
1137                 struct ieee80211_supported_band *sband;
1138
1139                 sband = local->hw.wiphy->bands[conf->channel->band];
1140                 buf_size = IEEE80211_MIN_AMPDU_BUF;
1141                 buf_size = buf_size << sband->ht_info.ampdu_factor;
1142         }
1143
1144
1145         /* examine state machine */
1146         spin_lock_bh(&sta->lock);
1147
1148         if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
1149 #ifdef CONFIG_MAC80211_HT_DEBUG
1150                 if (net_ratelimit())
1151                         printk(KERN_DEBUG "unexpected AddBA Req from "
1152                                 "%s on tid %u\n",
1153                                 print_mac(mac, mgmt->sa), tid);
1154 #endif /* CONFIG_MAC80211_HT_DEBUG */
1155                 goto end;
1156         }
1157
1158         /* prepare A-MPDU MLME for Rx aggregation */
1159         sta->ampdu_mlme.tid_rx[tid] =
1160                         kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1161         if (!sta->ampdu_mlme.tid_rx[tid]) {
1162 #ifdef CONFIG_MAC80211_HT_DEBUG
1163                 if (net_ratelimit())
1164                         printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1165                                         tid);
1166 #endif
1167                 goto end;
1168         }
1169         /* rx timer */
1170         sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1171                                 sta_rx_agg_session_timer_expired;
1172         sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1173                                 (unsigned long)&sta->timer_to_tid[tid];
1174         init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1175
1176         tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1177
1178         /* prepare reordering buffer */
1179         tid_agg_rx->reorder_buf =
1180                 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
1181         if (!tid_agg_rx->reorder_buf) {
1182 #ifdef CONFIG_MAC80211_HT_DEBUG
1183                 if (net_ratelimit())
1184                         printk(KERN_ERR "can not allocate reordering buffer "
1185                                "to tid %d\n", tid);
1186 #endif
1187                 kfree(sta->ampdu_mlme.tid_rx[tid]);
1188                 goto end;
1189         }
1190         memset(tid_agg_rx->reorder_buf, 0,
1191                 buf_size * sizeof(struct sk_buff *));
1192
1193         if (local->ops->ampdu_action)
1194                 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1195                                                sta->addr, tid, &start_seq_num);
1196 #ifdef CONFIG_MAC80211_HT_DEBUG
1197         printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
1198 #endif /* CONFIG_MAC80211_HT_DEBUG */
1199
1200         if (ret) {
1201                 kfree(tid_agg_rx->reorder_buf);
1202                 kfree(tid_agg_rx);
1203                 sta->ampdu_mlme.tid_rx[tid] = NULL;
1204                 goto end;
1205         }
1206
1207         /* change state and send addba resp */
1208         sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
1209         tid_agg_rx->dialog_token = dialog_token;
1210         tid_agg_rx->ssn = start_seq_num;
1211         tid_agg_rx->head_seq_num = start_seq_num;
1212         tid_agg_rx->buf_size = buf_size;
1213         tid_agg_rx->timeout = timeout;
1214         tid_agg_rx->stored_mpdu_num = 0;
1215         status = WLAN_STATUS_SUCCESS;
1216 end:
1217         spin_unlock_bh(&sta->lock);
1218
1219 end_no_lock:
1220         ieee80211_send_addba_resp(sta->sdata, sta->addr, tid,
1221                                   dialog_token, status, 1, buf_size, timeout);
1222         rcu_read_unlock();
1223 }
1224
1225 static void ieee80211_sta_process_addba_resp(struct ieee80211_local *local,
1226                                              struct ieee80211_mgmt *mgmt,
1227                                              size_t len)
1228 {
1229         struct ieee80211_hw *hw = &local->hw;
1230         struct sta_info *sta;
1231         u16 capab;
1232         u16 tid;
1233         u8 *state;
1234
1235         rcu_read_lock();
1236
1237         sta = sta_info_get(local, mgmt->sa);
1238         if (!sta) {
1239                 rcu_read_unlock();
1240                 return;
1241         }
1242
1243         capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1244         tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1245
1246         state = &sta->ampdu_mlme.tid_state_tx[tid];
1247
1248         spin_lock_bh(&sta->lock);
1249
1250         if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1251                 spin_unlock_bh(&sta->lock);
1252                 goto addba_resp_exit;
1253         }
1254
1255         if (mgmt->u.action.u.addba_resp.dialog_token !=
1256                 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1257                 spin_unlock_bh(&sta->lock);
1258 #ifdef CONFIG_MAC80211_HT_DEBUG
1259                 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1260 #endif /* CONFIG_MAC80211_HT_DEBUG */
1261                 goto addba_resp_exit;
1262         }
1263
1264         del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1265 #ifdef CONFIG_MAC80211_HT_DEBUG
1266         printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1267 #endif /* CONFIG_MAC80211_HT_DEBUG */
1268         if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1269                         == WLAN_STATUS_SUCCESS) {
1270                 *state |= HT_ADDBA_RECEIVED_MSK;
1271                 sta->ampdu_mlme.addba_req_num[tid] = 0;
1272
1273                 if (*state == HT_AGG_STATE_OPERATIONAL)
1274                         ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1275
1276                 spin_unlock_bh(&sta->lock);
1277         } else {
1278                 sta->ampdu_mlme.addba_req_num[tid]++;
1279                 /* this will allow the state check in stop_BA_session */
1280                 *state = HT_AGG_STATE_OPERATIONAL;
1281                 spin_unlock_bh(&sta->lock);
1282                 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1283                                              WLAN_BACK_INITIATOR);
1284         }
1285
1286 addba_resp_exit:
1287         rcu_read_unlock();
1288 }
1289
1290 static void ieee80211_sta_process_delba(struct ieee80211_sub_if_data *sdata,
1291                         struct ieee80211_mgmt *mgmt, size_t len)
1292 {
1293         struct ieee80211_local *local = sdata->local;
1294         struct sta_info *sta;
1295         u16 tid, params;
1296         u16 initiator;
1297         DECLARE_MAC_BUF(mac);
1298
1299         rcu_read_lock();
1300
1301         sta = sta_info_get(local, mgmt->sa);
1302         if (!sta) {
1303                 rcu_read_unlock();
1304                 return;
1305         }
1306
1307         params = le16_to_cpu(mgmt->u.action.u.delba.params);
1308         tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1309         initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1310
1311 #ifdef CONFIG_MAC80211_HT_DEBUG
1312         if (net_ratelimit())
1313                 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1314                         print_mac(mac, mgmt->sa),
1315                         initiator ? "initiator" : "recipient", tid,
1316                         mgmt->u.action.u.delba.reason_code);
1317 #endif /* CONFIG_MAC80211_HT_DEBUG */
1318
1319         if (initiator == WLAN_BACK_INITIATOR)
1320                 ieee80211_sta_stop_rx_ba_session(sdata, sta->addr, tid,
1321                                                  WLAN_BACK_INITIATOR, 0);
1322         else { /* WLAN_BACK_RECIPIENT */
1323                 spin_lock_bh(&sta->lock);
1324                 sta->ampdu_mlme.tid_state_tx[tid] =
1325                                 HT_AGG_STATE_OPERATIONAL;
1326                 spin_unlock_bh(&sta->lock);
1327                 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1328                                              WLAN_BACK_RECIPIENT);
1329         }
1330         rcu_read_unlock();
1331 }
1332
1333 static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata,
1334                                         struct ieee80211_msrment_ie *request_ie,
1335                                         const u8 *da, const u8 *bssid,
1336                                         u8 dialog_token)
1337 {
1338         struct ieee80211_local *local = sdata->local;
1339         struct sk_buff *skb;
1340         struct ieee80211_mgmt *msr_report;
1341
1342         skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
1343                                 sizeof(struct ieee80211_msrment_ie));
1344
1345         if (!skb) {
1346                 printk(KERN_ERR "%s: failed to allocate buffer for "
1347                                 "measurement report frame\n", sdata->dev->name);
1348                 return;
1349         }
1350
1351         skb_reserve(skb, local->hw.extra_tx_headroom);
1352         msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
1353         memset(msr_report, 0, 24);
1354         memcpy(msr_report->da, da, ETH_ALEN);
1355         memcpy(msr_report->sa, sdata->dev->dev_addr, ETH_ALEN);
1356         memcpy(msr_report->bssid, bssid, ETH_ALEN);
1357         msr_report->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1358                                                 IEEE80211_STYPE_ACTION);
1359
1360         skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
1361         msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
1362         msr_report->u.action.u.measurement.action_code =
1363                                 WLAN_ACTION_SPCT_MSR_RPRT;
1364         msr_report->u.action.u.measurement.dialog_token = dialog_token;
1365
1366         msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
1367         msr_report->u.action.u.measurement.length =
1368                         sizeof(struct ieee80211_msrment_ie);
1369
1370         memset(&msr_report->u.action.u.measurement.msr_elem, 0,
1371                 sizeof(struct ieee80211_msrment_ie));
1372         msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
1373         msr_report->u.action.u.measurement.msr_elem.mode |=
1374                         IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
1375         msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
1376
1377         ieee80211_sta_tx(sdata, skb, 0);
1378 }
1379
1380 static void ieee80211_sta_process_measurement_req(struct ieee80211_sub_if_data *sdata,
1381                                                 struct ieee80211_mgmt *mgmt,
1382                                                 size_t len)
1383 {
1384         /*
1385          * Ignoring measurement request is spec violation.
1386          * Mandatory measurements must be reported optional
1387          * measurements might be refused or reported incapable
1388          * For now just refuse
1389          * TODO: Answer basic measurement as unmeasured
1390          */
1391         ieee80211_send_refuse_measurement_request(sdata,
1392                         &mgmt->u.action.u.measurement.msr_elem,
1393                         mgmt->sa, mgmt->bssid,
1394                         mgmt->u.action.u.measurement.dialog_token);
1395 }
1396
1397
1398 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1399                                    struct ieee80211_if_sta *ifsta,
1400                                    struct ieee80211_mgmt *mgmt,
1401                                    size_t len)
1402 {
1403         u16 auth_alg, auth_transaction, status_code;
1404         DECLARE_MAC_BUF(mac);
1405
1406         if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
1407             sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
1408                 return;
1409
1410         if (len < 24 + 6)
1411                 return;
1412
1413         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1414             memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
1415                 return;
1416
1417         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1418             memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1419                 return;
1420
1421         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1422         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1423         status_code = le16_to_cpu(mgmt->u.auth.status_code);
1424
1425         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1426                 /*
1427                  * IEEE 802.11 standard does not require authentication in IBSS
1428                  * networks and most implementations do not seem to use it.
1429                  * However, try to reply to authentication attempts if someone
1430                  * has actually implemented this.
1431                  */
1432                 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
1433                         return;
1434                 ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
1435         }
1436
1437         if (auth_alg != ifsta->auth_alg ||
1438             auth_transaction != ifsta->auth_transaction)
1439                 return;
1440
1441         if (status_code != WLAN_STATUS_SUCCESS) {
1442                 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1443                         u8 algs[3];
1444                         const int num_algs = ARRAY_SIZE(algs);
1445                         int i, pos;
1446                         algs[0] = algs[1] = algs[2] = 0xff;
1447                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1448                                 algs[0] = WLAN_AUTH_OPEN;
1449                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1450                                 algs[1] = WLAN_AUTH_SHARED_KEY;
1451                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1452                                 algs[2] = WLAN_AUTH_LEAP;
1453                         if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1454                                 pos = 0;
1455                         else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1456                                 pos = 1;
1457                         else
1458                                 pos = 2;
1459                         for (i = 0; i < num_algs; i++) {
1460                                 pos++;
1461                                 if (pos >= num_algs)
1462                                         pos = 0;
1463                                 if (algs[pos] == ifsta->auth_alg ||
1464                                     algs[pos] == 0xff)
1465                                         continue;
1466                                 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1467                                     !ieee80211_sta_wep_configured(sdata))
1468                                         continue;
1469                                 ifsta->auth_alg = algs[pos];
1470                                 break;
1471                         }
1472                 }
1473                 return;
1474         }
1475
1476         switch (ifsta->auth_alg) {
1477         case WLAN_AUTH_OPEN:
1478         case WLAN_AUTH_LEAP:
1479                 ieee80211_auth_completed(sdata, ifsta);
1480                 break;
1481         case WLAN_AUTH_SHARED_KEY:
1482                 if (ifsta->auth_transaction == 4)
1483                         ieee80211_auth_completed(sdata, ifsta);
1484                 else
1485                         ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
1486                 break;
1487         }
1488 }
1489
1490
1491 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1492                                      struct ieee80211_if_sta *ifsta,
1493                                      struct ieee80211_mgmt *mgmt,
1494                                      size_t len)
1495 {
1496         u16 reason_code;
1497         DECLARE_MAC_BUF(mac);
1498
1499         if (len < 24 + 2)
1500                 return;
1501
1502         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1503                 return;
1504
1505         reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1506
1507         if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
1508                 printk(KERN_DEBUG "%s: deauthenticated\n", sdata->dev->name);
1509
1510         if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
1511             ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
1512             ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1513                 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
1514                 mod_timer(&ifsta->timer, jiffies +
1515                                       IEEE80211_RETRY_AUTH_INTERVAL);
1516         }
1517
1518         ieee80211_set_disassoc(sdata, ifsta, true, false, 0);
1519         ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1520 }
1521
1522
1523 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1524                                        struct ieee80211_if_sta *ifsta,
1525                                        struct ieee80211_mgmt *mgmt,
1526                                        size_t len)
1527 {
1528         u16 reason_code;
1529         DECLARE_MAC_BUF(mac);
1530
1531         if (len < 24 + 2)
1532                 return;
1533
1534         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1535                 return;
1536
1537         reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1538
1539         if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1540                 printk(KERN_DEBUG "%s: disassociated\n", sdata->dev->name);
1541
1542         if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1543                 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
1544                 mod_timer(&ifsta->timer, jiffies +
1545                                       IEEE80211_RETRY_AUTH_INTERVAL);
1546         }
1547
1548         ieee80211_set_disassoc(sdata, ifsta, false, false, 0);
1549 }
1550
1551
1552 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1553                                          struct ieee80211_if_sta *ifsta,
1554                                          struct ieee80211_mgmt *mgmt,
1555                                          size_t len,
1556                                          int reassoc)
1557 {
1558         struct ieee80211_local *local = sdata->local;
1559         struct ieee80211_supported_band *sband;
1560         struct sta_info *sta;
1561         u64 rates, basic_rates;
1562         u16 capab_info, status_code, aid;
1563         struct ieee802_11_elems elems;
1564         struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
1565         u8 *pos;
1566         int i, j;
1567         DECLARE_MAC_BUF(mac);
1568         bool have_higher_than_11mbit = false;
1569
1570         /* AssocResp and ReassocResp have identical structure, so process both
1571          * of them in this function. */
1572
1573         if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
1574                 return;
1575
1576         if (len < 24 + 6)
1577                 return;
1578
1579         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
1580                 return;
1581
1582         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1583         status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1584         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1585
1586         printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1587                "status=%d aid=%d)\n",
1588                sdata->dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1589                capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1590
1591         if (status_code != WLAN_STATUS_SUCCESS) {
1592                 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1593                        sdata->dev->name, status_code);
1594                 /* if this was a reassociation, ensure we try a "full"
1595                  * association next time. This works around some broken APs
1596                  * which do not correctly reject reassociation requests. */
1597                 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1598                 return;
1599         }
1600
1601         if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1602                 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1603                        "set\n", sdata->dev->name, aid);
1604         aid &= ~(BIT(15) | BIT(14));
1605
1606         pos = mgmt->u.assoc_resp.variable;
1607         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1608
1609         if (!elems.supp_rates) {
1610                 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1611                        sdata->dev->name);
1612                 return;
1613         }
1614
1615         printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
1616         ifsta->aid = aid;
1617         ifsta->ap_capab = capab_info;
1618
1619         kfree(ifsta->assocresp_ies);
1620         ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1621         ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1622         if (ifsta->assocresp_ies)
1623                 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1624
1625         rcu_read_lock();
1626
1627         /* Add STA entry for the AP */
1628         sta = sta_info_get(local, ifsta->bssid);
1629         if (!sta) {
1630                 struct ieee80211_sta_bss *bss;
1631                 int err;
1632
1633                 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1634                 if (!sta) {
1635                         printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1636                                " the AP\n", sdata->dev->name);
1637                         rcu_read_unlock();
1638                         return;
1639                 }
1640                 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
1641                                            local->hw.conf.channel->center_freq,
1642                                            ifsta->ssid, ifsta->ssid_len);
1643                 if (bss) {
1644                         sta->last_signal = bss->signal;
1645                         sta->last_qual = bss->qual;
1646                         sta->last_noise = bss->noise;
1647                         ieee80211_rx_bss_put(local, bss);
1648                 }
1649
1650                 err = sta_info_insert(sta);
1651                 if (err) {
1652                         printk(KERN_DEBUG "%s: failed to insert STA entry for"
1653                                " the AP (error %d)\n", sdata->dev->name, err);
1654                         rcu_read_unlock();
1655                         return;
1656                 }
1657                 /* update new sta with its last rx activity */
1658                 sta->last_rx = jiffies;
1659         }
1660
1661         /*
1662          * FIXME: Do we really need to update the sta_info's information here?
1663          *        We already know about the AP (we found it in our list) so it
1664          *        should already be filled with the right info, no?
1665          *        As is stands, all this is racy because typically we assume
1666          *        the information that is filled in here (except flags) doesn't
1667          *        change while a STA structure is alive. As such, it should move
1668          *        to between the sta_info_alloc() and sta_info_insert() above.
1669          */
1670
1671         set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1672                            WLAN_STA_AUTHORIZED);
1673
1674         rates = 0;
1675         basic_rates = 0;
1676         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1677
1678         for (i = 0; i < elems.supp_rates_len; i++) {
1679                 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1680
1681                 if (rate > 110)
1682                         have_higher_than_11mbit = true;
1683
1684                 for (j = 0; j < sband->n_bitrates; j++) {
1685                         if (sband->bitrates[j].bitrate == rate)
1686                                 rates |= BIT(j);
1687                         if (elems.supp_rates[i] & 0x80)
1688                                 basic_rates |= BIT(j);
1689                 }
1690         }
1691
1692         for (i = 0; i < elems.ext_supp_rates_len; i++) {
1693                 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1694
1695                 if (rate > 110)
1696                         have_higher_than_11mbit = true;
1697
1698                 for (j = 0; j < sband->n_bitrates; j++) {
1699                         if (sband->bitrates[j].bitrate == rate)
1700                                 rates |= BIT(j);
1701                         if (elems.ext_supp_rates[i] & 0x80)
1702                                 basic_rates |= BIT(j);
1703                 }
1704         }
1705
1706         sta->supp_rates[local->hw.conf.channel->band] = rates;
1707         sdata->basic_rates = basic_rates;
1708
1709         /* cf. IEEE 802.11 9.2.12 */
1710         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1711             have_higher_than_11mbit)
1712                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1713         else
1714                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1715
1716         if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1717             (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1718                 struct ieee80211_ht_bss_info bss_info;
1719                 ieee80211_ht_cap_ie_to_ht_info(
1720                                 (struct ieee80211_ht_cap *)
1721                                 elems.ht_cap_elem, &sta->ht_info);
1722                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1723                                 (struct ieee80211_ht_addt_info *)
1724                                 elems.ht_info_elem, &bss_info);
1725                 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
1726         }
1727
1728         rate_control_rate_init(sta, local);
1729
1730         if (elems.wmm_param) {
1731                 set_sta_flags(sta, WLAN_STA_WME);
1732                 rcu_read_unlock();
1733                 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
1734                                          elems.wmm_param_len);
1735         } else
1736                 rcu_read_unlock();
1737
1738         /* set AID and assoc capability,
1739          * ieee80211_set_associated() will tell the driver */
1740         bss_conf->aid = aid;
1741         bss_conf->assoc_capability = capab_info;
1742         ieee80211_set_associated(sdata, ifsta);
1743
1744         ieee80211_associated(sdata, ifsta);
1745 }
1746
1747
1748 static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
1749                                    struct ieee80211_if_sta *ifsta,
1750                                    struct ieee80211_sta_bss *bss)
1751 {
1752         struct ieee80211_local *local = sdata->local;
1753         int res, rates, i, j;
1754         struct sk_buff *skb;
1755         struct ieee80211_mgmt *mgmt;
1756         u8 *pos;
1757         struct ieee80211_supported_band *sband;
1758         union iwreq_data wrqu;
1759
1760         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1761
1762         /* Remove possible STA entries from other IBSS networks. */
1763         sta_info_flush_delayed(sdata);
1764
1765         if (local->ops->reset_tsf) {
1766                 /* Reset own TSF to allow time synchronization work. */
1767                 local->ops->reset_tsf(local_to_hw(local));
1768         }
1769         memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
1770         res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
1771         if (res)
1772                 return res;
1773
1774         local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
1775
1776         sdata->drop_unencrypted = bss->capability &
1777                 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
1778
1779         res = ieee80211_set_freq(sdata, bss->freq);
1780
1781         if (res)
1782                 return res;
1783
1784         /* Build IBSS probe response */
1785         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
1786         if (skb) {
1787                 skb_reserve(skb, local->hw.extra_tx_headroom);
1788
1789                 mgmt = (struct ieee80211_mgmt *)
1790                         skb_put(skb, 24 + sizeof(mgmt->u.beacon));
1791                 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
1792                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1793                                                   IEEE80211_STYPE_PROBE_RESP);
1794                 memset(mgmt->da, 0xff, ETH_ALEN);
1795                 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1796                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1797                 mgmt->u.beacon.beacon_int =
1798                         cpu_to_le16(local->hw.conf.beacon_int);
1799                 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
1800                 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
1801
1802                 pos = skb_put(skb, 2 + ifsta->ssid_len);
1803                 *pos++ = WLAN_EID_SSID;
1804                 *pos++ = ifsta->ssid_len;
1805                 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
1806
1807                 rates = bss->supp_rates_len;
1808                 if (rates > 8)
1809                         rates = 8;
1810                 pos = skb_put(skb, 2 + rates);
1811                 *pos++ = WLAN_EID_SUPP_RATES;
1812                 *pos++ = rates;
1813                 memcpy(pos, bss->supp_rates, rates);
1814
1815                 if (bss->band == IEEE80211_BAND_2GHZ) {
1816                         pos = skb_put(skb, 2 + 1);
1817                         *pos++ = WLAN_EID_DS_PARAMS;
1818                         *pos++ = 1;
1819                         *pos++ = ieee80211_frequency_to_channel(bss->freq);
1820                 }
1821
1822                 pos = skb_put(skb, 2 + 2);
1823                 *pos++ = WLAN_EID_IBSS_PARAMS;
1824                 *pos++ = 2;
1825                 /* FIX: set ATIM window based on scan results */
1826                 *pos++ = 0;
1827                 *pos++ = 0;
1828
1829                 if (bss->supp_rates_len > 8) {
1830                         rates = bss->supp_rates_len - 8;
1831                         pos = skb_put(skb, 2 + rates);
1832                         *pos++ = WLAN_EID_EXT_SUPP_RATES;
1833                         *pos++ = rates;
1834                         memcpy(pos, &bss->supp_rates[8], rates);
1835                 }
1836
1837                 ifsta->probe_resp = skb;
1838
1839                 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
1840         }
1841
1842         rates = 0;
1843         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1844         for (i = 0; i < bss->supp_rates_len; i++) {
1845                 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
1846                 for (j = 0; j < sband->n_bitrates; j++)
1847                         if (sband->bitrates[j].bitrate == bitrate)
1848                                 rates |= BIT(j);
1849         }
1850         ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
1851
1852         ieee80211_sta_def_wmm_params(sdata, bss, 1);
1853
1854         ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
1855         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1856
1857         memset(&wrqu, 0, sizeof(wrqu));
1858         memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
1859         wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
1860
1861         return res;
1862 }
1863
1864 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
1865                             struct ieee802_11_elems *elems,
1866                             enum ieee80211_band band)
1867 {
1868         struct ieee80211_supported_band *sband;
1869         struct ieee80211_rate *bitrates;
1870         size_t num_rates;
1871         u64 supp_rates;
1872         int i, j;
1873         sband = local->hw.wiphy->bands[band];
1874
1875         if (!sband) {
1876                 WARN_ON(1);
1877                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1878         }
1879
1880         bitrates = sband->bitrates;
1881         num_rates = sband->n_bitrates;
1882         supp_rates = 0;
1883         for (i = 0; i < elems->supp_rates_len +
1884                      elems->ext_supp_rates_len; i++) {
1885                 u8 rate = 0;
1886                 int own_rate;
1887                 if (i < elems->supp_rates_len)
1888                         rate = elems->supp_rates[i];
1889                 else if (elems->ext_supp_rates)
1890                         rate = elems->ext_supp_rates
1891                                 [i - elems->supp_rates_len];
1892                 own_rate = 5 * (rate & 0x7f);
1893                 for (j = 0; j < num_rates; j++)
1894                         if (bitrates[j].bitrate == own_rate)
1895                                 supp_rates |= BIT(j);
1896         }
1897         return supp_rates;
1898 }
1899
1900 static u64 ieee80211_sta_get_mandatory_rates(struct ieee80211_local *local,
1901                                         enum ieee80211_band band)
1902 {
1903         struct ieee80211_supported_band *sband;
1904         struct ieee80211_rate *bitrates;
1905         u64 mandatory_rates;
1906         enum ieee80211_rate_flags mandatory_flag;
1907         int i;
1908
1909         sband = local->hw.wiphy->bands[band];
1910         if (!sband) {
1911                 WARN_ON(1);
1912                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1913         }
1914
1915         if (band == IEEE80211_BAND_2GHZ)
1916                 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
1917         else
1918                 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
1919
1920         bitrates = sband->bitrates;
1921         mandatory_rates = 0;
1922         for (i = 0; i < sband->n_bitrates; i++)
1923                 if (bitrates[i].flags & mandatory_flag)
1924                         mandatory_rates |= BIT(i);
1925         return mandatory_rates;
1926 }
1927
1928 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1929                                   struct ieee80211_mgmt *mgmt,
1930                                   size_t len,
1931                                   struct ieee80211_rx_status *rx_status,
1932                                   struct ieee802_11_elems *elems,
1933                                   bool beacon)
1934 {
1935         struct ieee80211_local *local = sdata->local;
1936         int freq;
1937         struct ieee80211_sta_bss *bss;
1938         struct sta_info *sta;
1939         struct ieee80211_channel *channel;
1940         u64 beacon_timestamp, rx_timestamp;
1941         u64 supp_rates = 0;
1942         enum ieee80211_band band = rx_status->band;
1943         DECLARE_MAC_BUF(mac);
1944         DECLARE_MAC_BUF(mac2);
1945
1946         if (elems->ds_params && elems->ds_params_len == 1)
1947                 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1948         else
1949                 freq = rx_status->freq;
1950
1951         channel = ieee80211_get_channel(local->hw.wiphy, freq);
1952
1953         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1954                 return;
1955
1956         if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
1957             elems->mesh_config && mesh_matches_local(elems, sdata)) {
1958                 supp_rates = ieee80211_sta_get_rates(local, elems, band);
1959
1960                 mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
1961                                       mesh_peer_accepts_plinks(elems));
1962         }
1963
1964         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
1965             memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1966                 supp_rates = ieee80211_sta_get_rates(local, elems, band);
1967
1968                 rcu_read_lock();
1969
1970                 sta = sta_info_get(local, mgmt->sa);
1971                 if (sta) {
1972                         u64 prev_rates;
1973
1974                         prev_rates = sta->supp_rates[band];
1975                         /* make sure mandatory rates are always added */
1976                         sta->supp_rates[band] = supp_rates |
1977                                 ieee80211_sta_get_mandatory_rates(local, band);
1978
1979 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1980                         if (sta->supp_rates[band] != prev_rates)
1981                                 printk(KERN_DEBUG "%s: updated supp_rates set "
1982                                     "for %s based on beacon info (0x%llx | "
1983                                     "0x%llx -> 0x%llx)\n",
1984                                     sdata->dev->name, print_mac(mac, sta->addr),
1985                                     (unsigned long long) prev_rates,
1986                                     (unsigned long long) supp_rates,
1987                                     (unsigned long long) sta->supp_rates[band]);
1988 #endif
1989                 } else {
1990                         ieee80211_ibss_add_sta(sdata, NULL, mgmt->bssid,
1991                                                mgmt->sa, supp_rates);
1992                 }
1993
1994                 rcu_read_unlock();
1995         }
1996
1997         bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1998                                         freq, beacon);
1999         if (!bss)
2000                 return;
2001
2002         /* was just updated in ieee80211_bss_info_update */
2003         beacon_timestamp = bss->timestamp;
2004
2005         /*
2006          * In STA mode, the remaining parameters should not be overridden
2007          * by beacons because they're not necessarily accurate there.
2008          */
2009         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2010             bss->last_probe_resp && beacon) {
2011                 ieee80211_rx_bss_put(local, bss);
2012                 return;
2013         }
2014
2015         /* check if we need to merge IBSS */
2016         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2017             bss->capability & WLAN_CAPABILITY_IBSS &&
2018             bss->freq == local->oper_channel->center_freq &&
2019             elems->ssid_len == sdata->u.sta.ssid_len &&
2020             memcmp(elems->ssid, sdata->u.sta.ssid,
2021                                 sdata->u.sta.ssid_len) == 0) {
2022                 if (rx_status->flag & RX_FLAG_TSFT) {
2023                         /* in order for correct IBSS merging we need mactime
2024                          *
2025                          * since mactime is defined as the time the first data
2026                          * symbol of the frame hits the PHY, and the timestamp
2027                          * of the beacon is defined as "the time that the data
2028                          * symbol containing the first bit of the timestamp is
2029                          * transmitted to the PHY plus the transmitting STA’s
2030                          * delays through its local PHY from the MAC-PHY
2031                          * interface to its interface with the WM"
2032                          * (802.11 11.1.2) - equals the time this bit arrives at
2033                          * the receiver - we have to take into account the
2034                          * offset between the two.
2035                          * e.g: at 1 MBit that means mactime is 192 usec earlier
2036                          * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2037                          */
2038                         int rate = local->hw.wiphy->bands[band]->
2039                                         bitrates[rx_status->rate_idx].bitrate;
2040                         rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2041                 } else if (local && local->ops && local->ops->get_tsf)
2042                         /* second best option: get current TSF */
2043                         rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2044                 else
2045                         /* can't merge without knowing the TSF */
2046                         rx_timestamp = -1LLU;
2047 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2048                 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2049                        "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2050                        print_mac(mac, mgmt->sa),
2051                        print_mac(mac2, mgmt->bssid),
2052                        (unsigned long long)rx_timestamp,
2053                        (unsigned long long)beacon_timestamp,
2054                        (unsigned long long)(rx_timestamp - beacon_timestamp),
2055                        jiffies);
2056 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2057                 if (beacon_timestamp > rx_timestamp) {
2058 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2059                         printk(KERN_DEBUG "%s: beacon TSF higher than "
2060                                "local TSF - IBSS merge with BSSID %s\n",
2061                                sdata->dev->name, print_mac(mac, mgmt->bssid));
2062 #endif
2063                         ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
2064                         ieee80211_ibss_add_sta(sdata, NULL,
2065                                                mgmt->bssid, mgmt->sa,
2066                                                supp_rates);
2067                 }
2068         }
2069
2070         ieee80211_rx_bss_put(local, bss);
2071 }
2072
2073
2074 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
2075                                          struct ieee80211_mgmt *mgmt,
2076                                          size_t len,
2077                                          struct ieee80211_rx_status *rx_status)
2078 {
2079         size_t baselen;
2080         struct ieee802_11_elems elems;
2081         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2082
2083         if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
2084                 return; /* ignore ProbeResp to foreign address */
2085
2086         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2087         if (baselen > len)
2088                 return;
2089
2090         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2091                                 &elems);
2092
2093         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
2094
2095         /* direct probe may be part of the association flow */
2096         if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
2097                                                         &ifsta->request)) {
2098                 printk(KERN_DEBUG "%s direct probe responded\n",
2099                        sdata->dev->name);
2100                 ieee80211_authenticate(sdata, ifsta);
2101         }
2102 }
2103
2104
2105 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2106                                      struct ieee80211_mgmt *mgmt,
2107                                      size_t len,
2108                                      struct ieee80211_rx_status *rx_status)
2109 {
2110         struct ieee80211_if_sta *ifsta;
2111         size_t baselen;
2112         struct ieee802_11_elems elems;
2113         struct ieee80211_local *local = sdata->local;
2114         struct ieee80211_conf *conf = &local->hw.conf;
2115         u32 changed = 0;
2116
2117         /* Process beacon from the current BSS */
2118         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2119         if (baselen > len)
2120                 return;
2121
2122         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2123
2124         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
2125
2126         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2127                 return;
2128         ifsta = &sdata->u.sta;
2129
2130         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
2131             memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2132                 return;
2133
2134         ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
2135                                  elems.wmm_param_len);
2136
2137         if (elems.erp_info && elems.erp_info_len >= 1)
2138                 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2139         else {
2140                 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2141                 changed |= ieee80211_handle_protect_preamb(sdata, false,
2142                                 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2143         }
2144
2145         if (elems.ht_cap_elem && elems.ht_info_elem &&
2146             elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2147                 struct ieee80211_ht_bss_info bss_info;
2148
2149                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2150                                 (struct ieee80211_ht_addt_info *)
2151                                 elems.ht_info_elem, &bss_info);
2152                 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2153                                                &bss_info);
2154         }
2155
2156         ieee80211_bss_info_change_notify(sdata, changed);
2157 }
2158
2159
2160 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
2161                                         struct ieee80211_if_sta *ifsta,
2162                                         struct ieee80211_mgmt *mgmt,
2163                                         size_t len,
2164                                         struct ieee80211_rx_status *rx_status)
2165 {
2166         struct ieee80211_local *local = sdata->local;
2167         int tx_last_beacon;
2168         struct sk_buff *skb;
2169         struct ieee80211_mgmt *resp;
2170         u8 *pos, *end;
2171         DECLARE_MAC_BUF(mac);
2172 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2173         DECLARE_MAC_BUF(mac2);
2174         DECLARE_MAC_BUF(mac3);
2175 #endif
2176
2177         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2178             ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
2179             len < 24 + 2 || !ifsta->probe_resp)
2180                 return;
2181
2182         if (local->ops->tx_last_beacon)
2183                 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2184         else
2185                 tx_last_beacon = 1;
2186
2187 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2188         printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2189                "%s (tx_last_beacon=%d)\n",
2190                sdata->dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2191                print_mac(mac3, mgmt->bssid), tx_last_beacon);
2192 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2193
2194         if (!tx_last_beacon)
2195                 return;
2196
2197         if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2198             memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2199                 return;
2200
2201         end = ((u8 *) mgmt) + len;
2202         pos = mgmt->u.probe_req.variable;
2203         if (pos[0] != WLAN_EID_SSID ||
2204             pos + 2 + pos[1] > end) {
2205 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2206                 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2207                        "from %s\n",
2208                        sdata->dev->name, print_mac(mac, mgmt->sa));
2209 #endif
2210                 return;
2211         }
2212         if (pos[1] != 0 &&
2213             (pos[1] != ifsta->ssid_len ||
2214              memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2215                 /* Ignore ProbeReq for foreign SSID */
2216                 return;
2217         }
2218
2219         /* Reply with ProbeResp */
2220         skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
2221         if (!skb)
2222                 return;
2223
2224         resp = (struct ieee80211_mgmt *) skb->data;
2225         memcpy(resp->da, mgmt->sa, ETH_ALEN);
2226 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2227         printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2228                sdata->dev->name, print_mac(mac, resp->da));
2229 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2230         ieee80211_sta_tx(sdata, skb, 0);
2231 }
2232
2233 static void ieee80211_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
2234                                      struct ieee80211_if_sta *ifsta,
2235                                      struct ieee80211_mgmt *mgmt,
2236                                      size_t len,
2237                                      struct ieee80211_rx_status *rx_status)
2238 {
2239         struct ieee80211_local *local = sdata->local;
2240
2241         /* all categories we currently handle have action_code */
2242         if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2243                 return;
2244
2245         switch (mgmt->u.action.category) {
2246         case WLAN_CATEGORY_SPECTRUM_MGMT:
2247                 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
2248                         break;
2249                 switch (mgmt->u.action.u.measurement.action_code) {
2250                 case WLAN_ACTION_SPCT_MSR_REQ:
2251                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2252                                    sizeof(mgmt->u.action.u.measurement)))
2253                                 break;
2254                         ieee80211_sta_process_measurement_req(sdata, mgmt, len);
2255                         break;
2256                 }
2257                 break;
2258         case WLAN_CATEGORY_BACK:
2259                 switch (mgmt->u.action.u.addba_req.action_code) {
2260                 case WLAN_ACTION_ADDBA_REQ:
2261                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2262                                    sizeof(mgmt->u.action.u.addba_req)))
2263                                 break;
2264                         ieee80211_sta_process_addba_request(local, mgmt, len);
2265                         break;
2266                 case WLAN_ACTION_ADDBA_RESP:
2267                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2268                                    sizeof(mgmt->u.action.u.addba_resp)))
2269                                 break;
2270                         ieee80211_sta_process_addba_resp(local, mgmt, len);
2271                         break;
2272                 case WLAN_ACTION_DELBA:
2273                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2274                                    sizeof(mgmt->u.action.u.delba)))
2275                                 break;
2276                         ieee80211_sta_process_delba(sdata, mgmt, len);
2277                         break;
2278                 }
2279                 break;
2280         case PLINK_CATEGORY:
2281                 if (ieee80211_vif_is_mesh(&sdata->vif))
2282                         mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
2283                 break;
2284         case MESH_PATH_SEL_CATEGORY:
2285                 if (ieee80211_vif_is_mesh(&sdata->vif))
2286                         mesh_rx_path_sel_frame(sdata, mgmt, len);
2287                 break;
2288         }
2289 }
2290
2291 void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
2292                            struct ieee80211_rx_status *rx_status)
2293 {
2294         struct ieee80211_local *local = sdata->local;
2295         struct ieee80211_if_sta *ifsta;
2296         struct ieee80211_mgmt *mgmt;
2297         u16 fc;
2298
2299         if (skb->len < 24)
2300                 goto fail;
2301
2302         ifsta = &sdata->u.sta;
2303
2304         mgmt = (struct ieee80211_mgmt *) skb->data;
2305         fc = le16_to_cpu(mgmt->frame_control);
2306
2307         switch (fc & IEEE80211_FCTL_STYPE) {
2308         case IEEE80211_STYPE_PROBE_REQ:
2309         case IEEE80211_STYPE_PROBE_RESP:
2310         case IEEE80211_STYPE_BEACON:
2311         case IEEE80211_STYPE_ACTION:
2312                 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2313         case IEEE80211_STYPE_AUTH:
2314         case IEEE80211_STYPE_ASSOC_RESP:
2315         case IEEE80211_STYPE_REASSOC_RESP:
2316         case IEEE80211_STYPE_DEAUTH:
2317         case IEEE80211_STYPE_DISASSOC:
2318                 skb_queue_tail(&ifsta->skb_queue, skb);
2319                 queue_work(local->hw.workqueue, &ifsta->work);
2320                 return;
2321         }
2322
2323  fail:
2324         kfree_skb(skb);
2325 }
2326
2327 static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2328                                          struct sk_buff *skb)
2329 {
2330         struct ieee80211_rx_status *rx_status;
2331         struct ieee80211_if_sta *ifsta;
2332         struct ieee80211_mgmt *mgmt;
2333         u16 fc;
2334
2335         ifsta = &sdata->u.sta;
2336
2337         rx_status = (struct ieee80211_rx_status *) skb->cb;
2338         mgmt = (struct ieee80211_mgmt *) skb->data;
2339         fc = le16_to_cpu(mgmt->frame_control);
2340
2341         switch (fc & IEEE80211_FCTL_STYPE) {
2342         case IEEE80211_STYPE_PROBE_REQ:
2343                 ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len,
2344                                             rx_status);
2345                 break;
2346         case IEEE80211_STYPE_PROBE_RESP:
2347                 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
2348                 break;
2349         case IEEE80211_STYPE_BEACON:
2350                 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2351                 break;
2352         case IEEE80211_STYPE_AUTH:
2353                 ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
2354                 break;
2355         case IEEE80211_STYPE_ASSOC_RESP:
2356                 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
2357                 break;
2358         case IEEE80211_STYPE_REASSOC_RESP:
2359                 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
2360                 break;
2361         case IEEE80211_STYPE_DEAUTH:
2362                 ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
2363                 break;
2364         case IEEE80211_STYPE_DISASSOC:
2365                 ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
2366                 break;
2367         case IEEE80211_STYPE_ACTION:
2368                 ieee80211_rx_mgmt_action(sdata, ifsta, mgmt, skb->len, rx_status);
2369                 break;
2370         }
2371
2372         kfree_skb(skb);
2373 }
2374
2375
2376 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
2377 {
2378         struct ieee80211_local *local = sdata->local;
2379         int active = 0;
2380         struct sta_info *sta;
2381
2382         rcu_read_lock();
2383
2384         list_for_each_entry_rcu(sta, &local->sta_list, list) {
2385                 if (sta->sdata == sdata &&
2386                     time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
2387                                jiffies)) {
2388                         active++;
2389                         break;
2390                 }
2391         }
2392
2393         rcu_read_unlock();
2394
2395         return active;
2396 }
2397
2398
2399 static void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, unsigned long exp_time)
2400 {
2401         struct ieee80211_local *local = sdata->local;
2402         struct sta_info *sta, *tmp;
2403         LIST_HEAD(tmp_list);
2404         DECLARE_MAC_BUF(mac);
2405         unsigned long flags;
2406
2407         spin_lock_irqsave(&local->sta_lock, flags);
2408         list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
2409                 if (time_after(jiffies, sta->last_rx + exp_time)) {
2410 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2411                         printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
2412                                sdata->dev->name, print_mac(mac, sta->addr));
2413 #endif
2414                         __sta_info_unlink(&sta);
2415                         if (sta)
2416                                 list_add(&sta->list, &tmp_list);
2417                 }
2418         spin_unlock_irqrestore(&local->sta_lock, flags);
2419
2420         list_for_each_entry_safe(sta, tmp, &tmp_list, list)
2421                 sta_info_destroy(sta);
2422 }
2423
2424
2425 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
2426                                      struct ieee80211_if_sta *ifsta)
2427 {
2428         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2429
2430         ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
2431         if (ieee80211_sta_active_ibss(sdata))
2432                 return;
2433
2434         printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
2435                "IBSS networks with same SSID (merge)\n", sdata->dev->name);
2436         ieee80211_sta_req_scan(sdata, ifsta->ssid, ifsta->ssid_len);
2437 }
2438
2439
2440 #ifdef CONFIG_MAC80211_MESH
2441 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
2442                            struct ieee80211_if_sta *ifsta)
2443 {
2444         bool free_plinks;
2445
2446         ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
2447         mesh_path_expire(sdata);
2448
2449         free_plinks = mesh_plink_availables(sdata);
2450         if (free_plinks != sdata->u.sta.accepting_plinks)
2451                 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
2452
2453         mod_timer(&ifsta->timer, jiffies +
2454                         IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
2455 }
2456
2457
2458 void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
2459 {
2460         struct ieee80211_if_sta *ifsta;
2461         ifsta = &sdata->u.sta;
2462         ifsta->state = IEEE80211_STA_MLME_MESH_UP;
2463         ieee80211_sta_timer((unsigned long)sdata);
2464         ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
2465 }
2466 #endif
2467
2468
2469 void ieee80211_sta_timer(unsigned long data)
2470 {
2471         struct ieee80211_sub_if_data *sdata =
2472                 (struct ieee80211_sub_if_data *) data;
2473         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2474         struct ieee80211_local *local = sdata->local;
2475
2476         set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2477         queue_work(local->hw.workqueue, &ifsta->work);
2478 }
2479
2480 static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
2481                                      struct ieee80211_if_sta *ifsta)
2482 {
2483         struct ieee80211_local *local = sdata->local;
2484
2485         if (local->ops->reset_tsf) {
2486                 /* Reset own TSF to allow time synchronization work. */
2487                 local->ops->reset_tsf(local_to_hw(local));
2488         }
2489
2490         ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2491
2492
2493         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2494                 ifsta->auth_alg = WLAN_AUTH_OPEN;
2495         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2496                 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2497         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2498                 ifsta->auth_alg = WLAN_AUTH_LEAP;
2499         else
2500                 ifsta->auth_alg = WLAN_AUTH_OPEN;
2501         ifsta->auth_transaction = -1;
2502         ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2503         ifsta->assoc_scan_tries = 0;
2504         ifsta->direct_probe_tries = 0;
2505         ifsta->auth_tries = 0;
2506         ifsta->assoc_tries = 0;
2507         netif_tx_stop_all_queues(sdata->dev);
2508         netif_carrier_off(sdata->dev);
2509 }
2510
2511
2512 void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
2513                             struct ieee80211_if_sta *ifsta)
2514 {
2515         struct ieee80211_local *local = sdata->local;
2516
2517         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2518                 return;
2519
2520         if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2521                              IEEE80211_STA_AUTO_BSSID_SEL)) &&
2522             (ifsta->flags & (IEEE80211_STA_SSID_SET |
2523                              IEEE80211_STA_AUTO_SSID_SEL))) {
2524
2525                 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED)
2526                         ieee80211_set_disassoc(sdata, ifsta, true, true,
2527                                                WLAN_REASON_DEAUTH_LEAVING);
2528
2529                 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2530                 queue_work(local->hw.workqueue, &ifsta->work);
2531         }
2532 }
2533
2534 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2535                                     const char *ssid, int ssid_len)
2536 {
2537         int tmp, hidden_ssid;
2538
2539         if (ssid_len == ifsta->ssid_len &&
2540             !memcmp(ifsta->ssid, ssid, ssid_len))
2541                 return 1;
2542
2543         if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
2544                 return 0;
2545
2546         hidden_ssid = 1;
2547         tmp = ssid_len;
2548         while (tmp--) {
2549                 if (ssid[tmp] != '\0') {
2550                         hidden_ssid = 0;
2551                         break;
2552                 }
2553         }
2554
2555         if (hidden_ssid && ifsta->ssid_len == ssid_len)
2556                 return 1;
2557
2558         if (ssid_len == 1 && ssid[0] == ' ')
2559                 return 1;
2560
2561         return 0;
2562 }
2563
2564 static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
2565                                      struct ieee80211_if_sta *ifsta)
2566 {
2567         struct ieee80211_local *local = sdata->local;
2568         struct ieee80211_sta_bss *bss;
2569         struct ieee80211_supported_band *sband;
2570         u8 bssid[ETH_ALEN], *pos;
2571         int i;
2572         int ret;
2573         DECLARE_MAC_BUF(mac);
2574
2575 #if 0
2576         /* Easier testing, use fixed BSSID. */
2577         memset(bssid, 0xfe, ETH_ALEN);
2578 #else
2579         /* Generate random, not broadcast, locally administered BSSID. Mix in
2580          * own MAC address to make sure that devices that do not have proper
2581          * random number generator get different BSSID. */
2582         get_random_bytes(bssid, ETH_ALEN);
2583         for (i = 0; i < ETH_ALEN; i++)
2584                 bssid[i] ^= sdata->dev->dev_addr[i];
2585         bssid[0] &= ~0x01;
2586         bssid[0] |= 0x02;
2587 #endif
2588
2589         printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
2590                sdata->dev->name, print_mac(mac, bssid));
2591
2592         bss = ieee80211_rx_bss_add(local, bssid,
2593                                    local->hw.conf.channel->center_freq,
2594                                    sdata->u.sta.ssid, sdata->u.sta.ssid_len);
2595         if (!bss)
2596                 return -ENOMEM;
2597
2598         bss->band = local->hw.conf.channel->band;
2599         sband = local->hw.wiphy->bands[bss->band];
2600
2601         if (local->hw.conf.beacon_int == 0)
2602                 local->hw.conf.beacon_int = 100;
2603         bss->beacon_int = local->hw.conf.beacon_int;
2604         bss->last_update = jiffies;
2605         bss->capability = WLAN_CAPABILITY_IBSS;
2606
2607         if (sdata->default_key)
2608                 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2609         else
2610                 sdata->drop_unencrypted = 0;
2611
2612         bss->supp_rates_len = sband->n_bitrates;
2613         pos = bss->supp_rates;
2614         for (i = 0; i < sband->n_bitrates; i++) {
2615                 int rate = sband->bitrates[i].bitrate;
2616                 *pos++ = (u8) (rate / 5);
2617         }
2618
2619         ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
2620         ieee80211_rx_bss_put(local, bss);
2621         return ret;
2622 }
2623
2624
2625 static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
2626                                    struct ieee80211_if_sta *ifsta)
2627 {
2628         struct ieee80211_local *local = sdata->local;
2629         struct ieee80211_sta_bss *bss;
2630         int found = 0;
2631         u8 bssid[ETH_ALEN];
2632         int active_ibss;
2633         DECLARE_MAC_BUF(mac);
2634         DECLARE_MAC_BUF(mac2);
2635
2636         if (ifsta->ssid_len == 0)
2637                 return -EINVAL;
2638
2639         active_ibss = ieee80211_sta_active_ibss(sdata);
2640 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2641         printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2642                sdata->dev->name, active_ibss);
2643 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2644         spin_lock_bh(&local->sta_bss_lock);
2645         list_for_each_entry(bss, &local->sta_bss_list, list) {
2646                 if (ifsta->ssid_len != bss->ssid_len ||
2647                     memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2648                     || !(bss->capability & WLAN_CAPABILITY_IBSS))
2649                         continue;
2650 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2651                 printk(KERN_DEBUG "   bssid=%s found\n",
2652                        print_mac(mac, bss->bssid));
2653 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2654                 memcpy(bssid, bss->bssid, ETH_ALEN);
2655                 found = 1;
2656                 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2657                         break;
2658         }
2659         spin_unlock_bh(&local->sta_bss_lock);
2660
2661 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2662         if (found)
2663                 printk(KERN_DEBUG "   sta_find_ibss: selected %s current "
2664                        "%s\n", print_mac(mac, bssid),
2665                        print_mac(mac2, ifsta->bssid));
2666 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2667
2668         if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2669                 int ret;
2670                 int search_freq;
2671
2672                 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
2673                         search_freq = bss->freq;
2674                 else
2675                         search_freq = local->hw.conf.channel->center_freq;
2676
2677                 bss = ieee80211_rx_bss_get(local, bssid, search_freq,
2678                                            ifsta->ssid, ifsta->ssid_len);
2679                 if (!bss)
2680                         goto dont_join;
2681
2682                 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
2683                        " based on configured SSID\n",
2684                        sdata->dev->name, print_mac(mac, bssid));
2685                 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
2686                 ieee80211_rx_bss_put(local, bss);
2687                 return ret;
2688         }
2689
2690 dont_join:
2691 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2692         printk(KERN_DEBUG "   did not try to join ibss\n");
2693 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2694
2695         /* Selected IBSS not found in current scan results - try to scan */
2696         if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
2697             !ieee80211_sta_active_ibss(sdata)) {
2698                 mod_timer(&ifsta->timer, jiffies +
2699                                       IEEE80211_IBSS_MERGE_INTERVAL);
2700         } else if (time_after(jiffies, local->last_scan_completed +
2701                               IEEE80211_SCAN_INTERVAL)) {
2702                 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2703                        "join\n", sdata->dev->name);
2704                 return ieee80211_sta_req_scan(sdata, ifsta->ssid,
2705                                               ifsta->ssid_len);
2706         } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
2707                 int interval = IEEE80211_SCAN_INTERVAL;
2708
2709                 if (time_after(jiffies, ifsta->ibss_join_req +
2710                                IEEE80211_IBSS_JOIN_TIMEOUT)) {
2711                         if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
2712                             (!(local->oper_channel->flags &
2713                                         IEEE80211_CHAN_NO_IBSS)))
2714                                 return ieee80211_sta_create_ibss(sdata, ifsta);
2715                         if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
2716                                 printk(KERN_DEBUG "%s: IBSS not allowed on"
2717                                        " %d MHz\n", sdata->dev->name,
2718                                        local->hw.conf.channel->center_freq);
2719                         }
2720
2721                         /* No IBSS found - decrease scan interval and continue
2722                          * scanning. */
2723                         interval = IEEE80211_SCAN_INTERVAL_SLOW;
2724                 }
2725
2726                 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
2727                 mod_timer(&ifsta->timer, jiffies + interval);
2728                 return 0;
2729         }
2730
2731         return 0;
2732 }
2733
2734
2735 int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
2736 {
2737         struct ieee80211_if_sta *ifsta;
2738         int res;
2739
2740         if (len > IEEE80211_MAX_SSID_LEN)
2741                 return -EINVAL;
2742
2743         ifsta = &sdata->u.sta;
2744
2745         if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
2746                 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
2747                 memcpy(ifsta->ssid, ssid, len);
2748                 ifsta->ssid_len = len;
2749                 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2750
2751                 res = 0;
2752                 /*
2753                  * Hack! MLME code needs to be cleaned up to have different
2754                  * entry points for configuration and internal selection change
2755                  */
2756                 if (netif_running(sdata->dev))
2757                         res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
2758                 if (res) {
2759                         printk(KERN_DEBUG "%s: Failed to config new SSID to "
2760                                "the low-level driver\n", sdata->dev->name);
2761                         return res;
2762                 }
2763         }
2764
2765         if (len)
2766                 ifsta->flags |= IEEE80211_STA_SSID_SET;
2767         else
2768                 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
2769
2770         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
2771             !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
2772                 ifsta->ibss_join_req = jiffies;
2773                 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
2774                 return ieee80211_sta_find_ibss(sdata, ifsta);
2775         }
2776
2777         return 0;
2778 }
2779
2780
2781 int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
2782 {
2783         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2784         memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2785         *len = ifsta->ssid_len;
2786         return 0;
2787 }
2788
2789
2790 int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
2791 {
2792         struct ieee80211_if_sta *ifsta;
2793         int res;
2794
2795         ifsta = &sdata->u.sta;
2796
2797         if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2798                 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2799                 res = 0;
2800                 /*
2801                  * Hack! See also ieee80211_sta_set_ssid.
2802                  */
2803                 if (netif_running(sdata->dev))
2804                         res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
2805                 if (res) {
2806                         printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2807                                "the low-level driver\n", sdata->dev->name);
2808                         return res;
2809                 }
2810         }
2811
2812         if (is_valid_ether_addr(bssid))
2813                 ifsta->flags |= IEEE80211_STA_BSSID_SET;
2814         else
2815                 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2816
2817         return 0;
2818 }
2819
2820
2821 int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
2822 {
2823         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2824
2825         kfree(ifsta->extra_ie);
2826         if (len == 0) {
2827                 ifsta->extra_ie = NULL;
2828                 ifsta->extra_ie_len = 0;
2829                 return 0;
2830         }
2831         ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
2832         if (!ifsta->extra_ie) {
2833                 ifsta->extra_ie_len = 0;
2834                 return -ENOMEM;
2835         }
2836         memcpy(ifsta->extra_ie, ie, len);
2837         ifsta->extra_ie_len = len;
2838         return 0;
2839 }
2840
2841
2842 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
2843                                         struct sk_buff *skb, u8 *bssid,
2844                                         u8 *addr, u64 supp_rates)
2845 {
2846         struct ieee80211_local *local = sdata->local;
2847         struct sta_info *sta;
2848         DECLARE_MAC_BUF(mac);
2849         int band = local->hw.conf.channel->band;
2850
2851         /* TODO: Could consider removing the least recently used entry and
2852          * allow new one to be added. */
2853         if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
2854                 if (net_ratelimit()) {
2855                         printk(KERN_DEBUG "%s: No room for a new IBSS STA "
2856                                "entry %s\n", sdata->dev->name, print_mac(mac, addr));
2857                 }
2858                 return NULL;
2859         }
2860
2861         if (compare_ether_addr(bssid, sdata->u.sta.bssid))
2862                 return NULL;
2863
2864 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2865         printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
2866                wiphy_name(local->hw.wiphy), print_mac(mac, addr), sdata->dev->name);
2867 #endif
2868
2869         sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
2870         if (!sta)
2871                 return NULL;
2872
2873         set_sta_flags(sta, WLAN_STA_AUTHORIZED);
2874
2875         /* make sure mandatory rates are always added */
2876         sta->supp_rates[band] = supp_rates |
2877                         ieee80211_sta_get_mandatory_rates(local, band);
2878
2879         rate_control_rate_init(sta, local);
2880
2881         if (sta_info_insert(sta))
2882                 return NULL;
2883
2884         return sta;
2885 }
2886
2887
2888 static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
2889                                      struct ieee80211_if_sta *ifsta)
2890 {
2891         struct ieee80211_local *local = sdata->local;
2892         struct ieee80211_sta_bss *bss, *selected = NULL;
2893         int top_rssi = 0, freq;
2894
2895         spin_lock_bh(&local->sta_bss_lock);
2896         freq = local->oper_channel->center_freq;
2897         list_for_each_entry(bss, &local->sta_bss_list, list) {
2898                 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2899                         continue;
2900
2901                 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2902                         IEEE80211_STA_AUTO_BSSID_SEL |
2903                         IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
2904                     (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2905                      !!sdata->default_key))
2906                         continue;
2907
2908                 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2909                     bss->freq != freq)
2910                         continue;
2911
2912                 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2913                     memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2914                         continue;
2915
2916                 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2917                     !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2918                         continue;
2919
2920                 if (!selected || top_rssi < bss->signal) {
2921                         selected = bss;
2922                         top_rssi = bss->signal;
2923                 }
2924         }
2925         if (selected)
2926                 atomic_inc(&selected->users);
2927         spin_unlock_bh(&local->sta_bss_lock);
2928
2929         if (selected) {
2930                 ieee80211_set_freq(sdata, selected->freq);
2931                 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2932                         ieee80211_sta_set_ssid(sdata, selected->ssid,
2933                                                selected->ssid_len);
2934                 ieee80211_sta_set_bssid(sdata, selected->bssid);
2935                 ieee80211_sta_def_wmm_params(sdata, selected, 0);
2936
2937                 /* Send out direct probe if no probe resp was received or
2938                  * the one we have is outdated
2939                  */
2940                 if (!selected->last_probe_resp ||
2941                     time_after(jiffies, selected->last_probe_resp
2942                                         + IEEE80211_SCAN_RESULT_EXPIRE))
2943                         ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
2944                 else
2945                         ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2946
2947                 ieee80211_rx_bss_put(local, selected);
2948                 ieee80211_sta_reset_auth(sdata, ifsta);
2949                 return 0;
2950         } else {
2951                 if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
2952                         ifsta->assoc_scan_tries++;
2953                         if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
2954                                 ieee80211_sta_start_scan(sdata, NULL, 0);
2955                         else
2956                                 ieee80211_sta_start_scan(sdata, ifsta->ssid,
2957                                                          ifsta->ssid_len);
2958                         ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2959                         set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2960                 } else
2961                         ifsta->state = IEEE80211_STA_MLME_DISABLED;
2962         }
2963         return -1;
2964 }
2965
2966
2967 int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
2968 {
2969         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2970
2971         printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
2972                sdata->dev->name, reason);
2973
2974         if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2975             sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
2976                 return -EINVAL;
2977
2978         ieee80211_set_disassoc(sdata, ifsta, true, true, reason);
2979         return 0;
2980 }
2981
2982
2983 int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
2984 {
2985         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2986
2987         printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
2988                sdata->dev->name, reason);
2989
2990         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2991                 return -EINVAL;
2992
2993         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
2994                 return -1;
2995
2996         ieee80211_set_disassoc(sdata, ifsta, false, true, reason);
2997         return 0;
2998 }
2999
3000 void ieee80211_notify_mac(struct ieee80211_hw *hw,
3001                           enum ieee80211_notification_types  notif_type)
3002 {
3003         struct ieee80211_local *local = hw_to_local(hw);
3004         struct ieee80211_sub_if_data *sdata;
3005
3006         switch (notif_type) {
3007         case IEEE80211_NOTIFY_RE_ASSOC:
3008                 rcu_read_lock();
3009                 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3010                         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3011                                 continue;
3012
3013                         ieee80211_sta_req_auth(sdata, &sdata->u.sta);
3014                 }
3015                 rcu_read_unlock();
3016                 break;
3017         }
3018 }
3019 EXPORT_SYMBOL(ieee80211_notify_mac);
3020
3021 void ieee80211_sta_work(struct work_struct *work)
3022 {
3023         struct ieee80211_sub_if_data *sdata =
3024                 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3025         struct ieee80211_local *local = sdata->local;
3026         struct ieee80211_if_sta *ifsta;
3027         struct sk_buff *skb;
3028
3029         if (!netif_running(sdata->dev))
3030                 return;
3031
3032         if (local->sta_sw_scanning || local->sta_hw_scanning)
3033                 return;
3034
3035         if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3036                     sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3037                     sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
3038                 return;
3039         ifsta = &sdata->u.sta;
3040
3041         while ((skb = skb_dequeue(&ifsta->skb_queue)))
3042                 ieee80211_sta_rx_queued_mgmt(sdata, skb);
3043
3044 #ifdef CONFIG_MAC80211_MESH
3045         if (ifsta->preq_queue_len &&
3046             time_after(jiffies,
3047                        ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
3048                 mesh_path_start_discovery(sdata);
3049 #endif
3050
3051         if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
3052             ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
3053             ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
3054             test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
3055                 if (ifsta->scan_ssid_len)
3056                         ieee80211_sta_start_scan(sdata, ifsta->scan_ssid, ifsta->scan_ssid_len);
3057                 else
3058                         ieee80211_sta_start_scan(sdata, NULL, 0);
3059                 return;
3060         }
3061
3062         if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3063                 if (ieee80211_sta_config_auth(sdata, ifsta))
3064                         return;
3065                 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3066         } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3067                 return;
3068
3069         switch (ifsta->state) {
3070         case IEEE80211_STA_MLME_DISABLED:
3071                 break;
3072         case IEEE80211_STA_MLME_DIRECT_PROBE:
3073                 ieee80211_direct_probe(sdata, ifsta);
3074                 break;
3075         case IEEE80211_STA_MLME_AUTHENTICATE:
3076                 ieee80211_authenticate(sdata, ifsta);
3077                 break;
3078         case IEEE80211_STA_MLME_ASSOCIATE:
3079                 ieee80211_associate(sdata, ifsta);
3080                 break;
3081         case IEEE80211_STA_MLME_ASSOCIATED:
3082                 ieee80211_associated(sdata, ifsta);
3083                 break;
3084         case IEEE80211_STA_MLME_IBSS_SEARCH:
3085                 ieee80211_sta_find_ibss(sdata, ifsta);
3086                 break;
3087         case IEEE80211_STA_MLME_IBSS_JOINED:
3088                 ieee80211_sta_merge_ibss(sdata, ifsta);
3089                 break;
3090 #ifdef CONFIG_MAC80211_MESH
3091         case IEEE80211_STA_MLME_MESH_UP:
3092                 ieee80211_mesh_housekeeping(sdata, ifsta);
3093                 break;
3094 #endif
3095         default:
3096                 WARN_ON(1);
3097                 break;
3098         }
3099
3100         if (ieee80211_privacy_mismatch(sdata, ifsta)) {
3101                 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3102                        "mixed-cell disabled - disassociate\n", sdata->dev->name);
3103
3104                 ieee80211_set_disassoc(sdata, ifsta, false, true,
3105                                         WLAN_REASON_UNSPECIFIED);
3106         }
3107 }
3108
3109 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3110 {
3111         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
3112         struct ieee80211_if_sta *ifsta;
3113
3114         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3115                 ifsta = &sdata->u.sta;
3116                 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3117                     (!(ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED) &&
3118                     !ieee80211_sta_active_ibss(sdata)))
3119                         ieee80211_sta_find_ibss(sdata, ifsta);
3120         }
3121 }