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