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