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