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