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