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