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