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