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