mac80211: Initialize vif pointer
[linux-2.6] / net / mac80211 / ieee80211_sta.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 <net/iw_handler.h>
28 #include <asm/types.h>
29
30 #include <net/mac80211.h>
31 #include "ieee80211_i.h"
32 #include "ieee80211_rate.h"
33 #include "ieee80211_led.h"
34
35 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
36 #define IEEE80211_AUTH_MAX_TRIES 3
37 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
38 #define IEEE80211_ASSOC_MAX_TRIES 3
39 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
40 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
41 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
42 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
43 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
44 #define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
45
46 #define IEEE80211_PROBE_DELAY (HZ / 33)
47 #define IEEE80211_CHANNEL_TIME (HZ / 33)
48 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
49 #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
50 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
51 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
52
53 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
54
55
56 #define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
57
58 #define ERP_INFO_USE_PROTECTION BIT(1)
59
60 /* mgmt header + 1 byte action code */
61 #define IEEE80211_MIN_ACTION_SIZE (24 + 1)
62
63 #define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
64 #define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
65 #define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
66 #define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
67 #define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
68
69 /* next values represent the buffer size for A-MPDU frame.
70  * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
71 #define IEEE80211_MIN_AMPDU_BUF 0x8
72 #define IEEE80211_MAX_AMPDU_BUF 0x40
73
74 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
75                                      u8 *ssid, size_t ssid_len);
76 static struct ieee80211_sta_bss *
77 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int channel,
78                      u8 *ssid, u8 ssid_len);
79 static void ieee80211_rx_bss_put(struct net_device *dev,
80                                  struct ieee80211_sta_bss *bss);
81 static int ieee80211_sta_find_ibss(struct net_device *dev,
82                                    struct ieee80211_if_sta *ifsta);
83 static int ieee80211_sta_wep_configured(struct net_device *dev);
84 static int ieee80211_sta_start_scan(struct net_device *dev,
85                                     u8 *ssid, size_t ssid_len);
86 static int ieee80211_sta_config_auth(struct net_device *dev,
87                                      struct ieee80211_if_sta *ifsta);
88
89
90 /* Parsed Information Elements */
91 struct ieee802_11_elems {
92         /* pointers to IEs */
93         u8 *ssid;
94         u8 *supp_rates;
95         u8 *fh_params;
96         u8 *ds_params;
97         u8 *cf_params;
98         u8 *tim;
99         u8 *ibss_params;
100         u8 *challenge;
101         u8 *wpa;
102         u8 *rsn;
103         u8 *erp_info;
104         u8 *ext_supp_rates;
105         u8 *wmm_info;
106         u8 *wmm_param;
107         u8 *ht_cap_elem;
108         u8 *ht_info_elem;
109         /* length of them, respectively */
110         u8 ssid_len;
111         u8 supp_rates_len;
112         u8 fh_params_len;
113         u8 ds_params_len;
114         u8 cf_params_len;
115         u8 tim_len;
116         u8 ibss_params_len;
117         u8 challenge_len;
118         u8 wpa_len;
119         u8 rsn_len;
120         u8 erp_info_len;
121         u8 ext_supp_rates_len;
122         u8 wmm_info_len;
123         u8 wmm_param_len;
124         u8 ht_cap_elem_len;
125         u8 ht_info_elem_len;
126 };
127
128 static void ieee802_11_parse_elems(u8 *start, size_t len,
129                                    struct ieee802_11_elems *elems)
130 {
131         size_t left = len;
132         u8 *pos = start;
133
134         memset(elems, 0, sizeof(*elems));
135
136         while (left >= 2) {
137                 u8 id, elen;
138
139                 id = *pos++;
140                 elen = *pos++;
141                 left -= 2;
142
143                 if (elen > left)
144                         return;
145
146                 switch (id) {
147                 case WLAN_EID_SSID:
148                         elems->ssid = pos;
149                         elems->ssid_len = elen;
150                         break;
151                 case WLAN_EID_SUPP_RATES:
152                         elems->supp_rates = pos;
153                         elems->supp_rates_len = elen;
154                         break;
155                 case WLAN_EID_FH_PARAMS:
156                         elems->fh_params = pos;
157                         elems->fh_params_len = elen;
158                         break;
159                 case WLAN_EID_DS_PARAMS:
160                         elems->ds_params = pos;
161                         elems->ds_params_len = elen;
162                         break;
163                 case WLAN_EID_CF_PARAMS:
164                         elems->cf_params = pos;
165                         elems->cf_params_len = elen;
166                         break;
167                 case WLAN_EID_TIM:
168                         elems->tim = pos;
169                         elems->tim_len = elen;
170                         break;
171                 case WLAN_EID_IBSS_PARAMS:
172                         elems->ibss_params = pos;
173                         elems->ibss_params_len = elen;
174                         break;
175                 case WLAN_EID_CHALLENGE:
176                         elems->challenge = pos;
177                         elems->challenge_len = elen;
178                         break;
179                 case WLAN_EID_WPA:
180                         if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
181                             pos[2] == 0xf2) {
182                                 /* Microsoft OUI (00:50:F2) */
183                                 if (pos[3] == 1) {
184                                         /* OUI Type 1 - WPA IE */
185                                         elems->wpa = pos;
186                                         elems->wpa_len = elen;
187                                 } else if (elen >= 5 && pos[3] == 2) {
188                                         if (pos[4] == 0) {
189                                                 elems->wmm_info = pos;
190                                                 elems->wmm_info_len = elen;
191                                         } else if (pos[4] == 1) {
192                                                 elems->wmm_param = pos;
193                                                 elems->wmm_param_len = elen;
194                                         }
195                                 }
196                         }
197                         break;
198                 case WLAN_EID_RSN:
199                         elems->rsn = pos;
200                         elems->rsn_len = elen;
201                         break;
202                 case WLAN_EID_ERP_INFO:
203                         elems->erp_info = pos;
204                         elems->erp_info_len = elen;
205                         break;
206                 case WLAN_EID_EXT_SUPP_RATES:
207                         elems->ext_supp_rates = pos;
208                         elems->ext_supp_rates_len = elen;
209                         break;
210                 case WLAN_EID_HT_CAPABILITY:
211                         elems->ht_cap_elem = pos;
212                         elems->ht_cap_elem_len = elen;
213                         break;
214                 case WLAN_EID_HT_EXTRA_INFO:
215                         elems->ht_info_elem = pos;
216                         elems->ht_info_elem_len = elen;
217                         break;
218                 default:
219                         break;
220                 }
221
222                 left -= elen;
223                 pos += elen;
224         }
225 }
226
227
228 static int ecw2cw(int ecw)
229 {
230         int cw = 1;
231         while (ecw > 0) {
232                 cw <<= 1;
233                 ecw--;
234         }
235         return cw - 1;
236 }
237
238 static void ieee80211_sta_wmm_params(struct net_device *dev,
239                                      struct ieee80211_if_sta *ifsta,
240                                      u8 *wmm_param, size_t wmm_param_len)
241 {
242         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
243         struct ieee80211_tx_queue_params params;
244         size_t left;
245         int count;
246         u8 *pos;
247
248         if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
249                 return;
250         count = wmm_param[6] & 0x0f;
251         if (count == ifsta->wmm_last_param_set)
252                 return;
253         ifsta->wmm_last_param_set = count;
254
255         pos = wmm_param + 8;
256         left = wmm_param_len - 8;
257
258         memset(&params, 0, sizeof(params));
259
260         if (!local->ops->conf_tx)
261                 return;
262
263         local->wmm_acm = 0;
264         for (; left >= 4; left -= 4, pos += 4) {
265                 int aci = (pos[0] >> 5) & 0x03;
266                 int acm = (pos[0] >> 4) & 0x01;
267                 int queue;
268
269                 switch (aci) {
270                 case 1:
271                         queue = IEEE80211_TX_QUEUE_DATA3;
272                         if (acm) {
273                                 local->wmm_acm |= BIT(0) | BIT(3);
274                         }
275                         break;
276                 case 2:
277                         queue = IEEE80211_TX_QUEUE_DATA1;
278                         if (acm) {
279                                 local->wmm_acm |= BIT(4) | BIT(5);
280                         }
281                         break;
282                 case 3:
283                         queue = IEEE80211_TX_QUEUE_DATA0;
284                         if (acm) {
285                                 local->wmm_acm |= BIT(6) | BIT(7);
286                         }
287                         break;
288                 case 0:
289                 default:
290                         queue = IEEE80211_TX_QUEUE_DATA2;
291                         if (acm) {
292                                 local->wmm_acm |= BIT(1) | BIT(2);
293                         }
294                         break;
295                 }
296
297                 params.aifs = pos[0] & 0x0f;
298                 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
299                 params.cw_min = ecw2cw(pos[1] & 0x0f);
300                 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
301                 params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
302                 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
303                        "cWmin=%d cWmax=%d burst=%d\n",
304                        dev->name, queue, aci, acm, params.aifs, params.cw_min,
305                        params.cw_max, params.burst_time);
306                 /* TODO: handle ACM (block TX, fallback to next lowest allowed
307                  * AC for now) */
308                 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
309                         printk(KERN_DEBUG "%s: failed to set TX queue "
310                                "parameters for queue %d\n", dev->name, queue);
311                 }
312         }
313 }
314
315
316 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
317                                    u8 erp_value)
318 {
319         struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
320         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
321         bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
322         bool preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
323         DECLARE_MAC_BUF(mac);
324         u32 changed = 0;
325
326         if (use_protection != bss_conf->use_cts_prot) {
327                 if (net_ratelimit()) {
328                         printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
329                                "%s)\n",
330                                sdata->dev->name,
331                                use_protection ? "enabled" : "disabled",
332                                print_mac(mac, ifsta->bssid));
333                 }
334                 bss_conf->use_cts_prot = use_protection;
335                 changed |= BSS_CHANGED_ERP_CTS_PROT;
336         }
337
338         if (preamble_mode != bss_conf->use_short_preamble) {
339                 if (net_ratelimit()) {
340                         printk(KERN_DEBUG "%s: switched to %s barker preamble"
341                                " (BSSID=%s)\n",
342                                sdata->dev->name,
343                                (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
344                                         "short" : "long",
345                                print_mac(mac, ifsta->bssid));
346                 }
347                 bss_conf->use_short_preamble = preamble_mode;
348                 changed |= BSS_CHANGED_ERP_PREAMBLE;
349         }
350
351         return changed;
352 }
353
354 int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
355                                    struct ieee80211_ht_info *ht_info)
356 {
357
358         if (ht_info == NULL)
359                 return -EINVAL;
360
361         memset(ht_info, 0, sizeof(*ht_info));
362
363         if (ht_cap_ie) {
364                 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
365
366                 ht_info->ht_supported = 1;
367                 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
368                 ht_info->ampdu_factor =
369                         ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
370                 ht_info->ampdu_density =
371                         (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
372                 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
373         } else
374                 ht_info->ht_supported = 0;
375
376         return 0;
377 }
378
379 int ieee80211_ht_addt_info_ie_to_ht_bss_info(
380                         struct ieee80211_ht_addt_info *ht_add_info_ie,
381                         struct ieee80211_ht_bss_info *bss_info)
382 {
383         if (bss_info == NULL)
384                 return -EINVAL;
385
386         memset(bss_info, 0, sizeof(*bss_info));
387
388         if (ht_add_info_ie) {
389                 u16 op_mode;
390                 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
391
392                 bss_info->primary_channel = ht_add_info_ie->control_chan;
393                 bss_info->bss_cap = ht_add_info_ie->ht_param;
394                 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
395         }
396
397         return 0;
398 }
399
400 static void ieee80211_sta_send_associnfo(struct net_device *dev,
401                                          struct ieee80211_if_sta *ifsta)
402 {
403         char *buf;
404         size_t len;
405         int i;
406         union iwreq_data wrqu;
407
408         if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
409                 return;
410
411         buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
412                                 ifsta->assocresp_ies_len), GFP_KERNEL);
413         if (!buf)
414                 return;
415
416         len = sprintf(buf, "ASSOCINFO(");
417         if (ifsta->assocreq_ies) {
418                 len += sprintf(buf + len, "ReqIEs=");
419                 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
420                         len += sprintf(buf + len, "%02x",
421                                        ifsta->assocreq_ies[i]);
422                 }
423         }
424         if (ifsta->assocresp_ies) {
425                 if (ifsta->assocreq_ies)
426                         len += sprintf(buf + len, " ");
427                 len += sprintf(buf + len, "RespIEs=");
428                 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
429                         len += sprintf(buf + len, "%02x",
430                                        ifsta->assocresp_ies[i]);
431                 }
432         }
433         len += sprintf(buf + len, ")");
434
435         if (len > IW_CUSTOM_MAX) {
436                 len = sprintf(buf, "ASSOCRESPIE=");
437                 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
438                         len += sprintf(buf + len, "%02x",
439                                        ifsta->assocresp_ies[i]);
440                 }
441         }
442
443         memset(&wrqu, 0, sizeof(wrqu));
444         wrqu.data.length = len;
445         wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
446
447         kfree(buf);
448 }
449
450
451 static void ieee80211_set_associated(struct net_device *dev,
452                                      struct ieee80211_if_sta *ifsta,
453                                      bool assoc)
454 {
455         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
456         struct ieee80211_local *local = sdata->local;
457         union iwreq_data wrqu;
458         u32 changed = BSS_CHANGED_ASSOC;
459
460         if (assoc) {
461                 struct ieee80211_sta_bss *bss;
462
463                 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
464
465                 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
466                         return;
467
468                 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
469                                            local->hw.conf.channel,
470                                            ifsta->ssid, ifsta->ssid_len);
471                 if (bss) {
472                         if (bss->has_erp_value)
473                                 changed |= ieee80211_handle_erp_ie(
474                                                 sdata, bss->erp_value);
475                         ieee80211_rx_bss_put(dev, bss);
476                 }
477
478                 netif_carrier_on(dev);
479                 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
480                 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
481                 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
482                 ieee80211_sta_send_associnfo(dev, ifsta);
483         } else {
484                 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
485
486                 netif_carrier_off(dev);
487                 ieee80211_reset_erp_info(dev);
488                 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
489         }
490         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
491         wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
492         ifsta->last_probe = jiffies;
493         ieee80211_led_assoc(local, assoc);
494
495         ieee80211_bss_info_change_notify(sdata, changed);
496 }
497
498 static void ieee80211_set_disassoc(struct net_device *dev,
499                                    struct ieee80211_if_sta *ifsta, int deauth)
500 {
501         if (deauth)
502                 ifsta->auth_tries = 0;
503         ifsta->assoc_tries = 0;
504         ieee80211_set_associated(dev, ifsta, 0);
505 }
506
507 static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
508                              int encrypt)
509 {
510         struct ieee80211_sub_if_data *sdata;
511         struct ieee80211_tx_packet_data *pkt_data;
512
513         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
514         skb->dev = sdata->local->mdev;
515         skb_set_mac_header(skb, 0);
516         skb_set_network_header(skb, 0);
517         skb_set_transport_header(skb, 0);
518
519         pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
520         memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
521         pkt_data->ifindex = sdata->dev->ifindex;
522         if (!encrypt)
523                 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
524
525         dev_queue_xmit(skb);
526 }
527
528
529 static void ieee80211_send_auth(struct net_device *dev,
530                                 struct ieee80211_if_sta *ifsta,
531                                 int transaction, u8 *extra, size_t extra_len,
532                                 int encrypt)
533 {
534         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
535         struct sk_buff *skb;
536         struct ieee80211_mgmt *mgmt;
537
538         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
539                             sizeof(*mgmt) + 6 + extra_len);
540         if (!skb) {
541                 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
542                        "frame\n", dev->name);
543                 return;
544         }
545         skb_reserve(skb, local->hw.extra_tx_headroom);
546
547         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
548         memset(mgmt, 0, 24 + 6);
549         mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
550                                            IEEE80211_STYPE_AUTH);
551         if (encrypt)
552                 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
553         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
554         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
555         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
556         mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
557         mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
558         ifsta->auth_transaction = transaction + 1;
559         mgmt->u.auth.status_code = cpu_to_le16(0);
560         if (extra)
561                 memcpy(skb_put(skb, extra_len), extra, extra_len);
562
563         ieee80211_sta_tx(dev, skb, encrypt);
564 }
565
566
567 static void ieee80211_authenticate(struct net_device *dev,
568                                    struct ieee80211_if_sta *ifsta)
569 {
570         DECLARE_MAC_BUF(mac);
571
572         ifsta->auth_tries++;
573         if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
574                 printk(KERN_DEBUG "%s: authentication with AP %s"
575                        " timed out\n",
576                        dev->name, print_mac(mac, ifsta->bssid));
577                 ifsta->state = IEEE80211_DISABLED;
578                 return;
579         }
580
581         ifsta->state = IEEE80211_AUTHENTICATE;
582         printk(KERN_DEBUG "%s: authenticate with AP %s\n",
583                dev->name, print_mac(mac, ifsta->bssid));
584
585         ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
586
587         mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
588 }
589
590
591 static void ieee80211_send_assoc(struct net_device *dev,
592                                  struct ieee80211_if_sta *ifsta)
593 {
594         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
595         struct ieee80211_hw_mode *mode;
596         struct sk_buff *skb;
597         struct ieee80211_mgmt *mgmt;
598         u8 *pos, *ies;
599         int i, len;
600         u16 capab;
601         struct ieee80211_sta_bss *bss;
602         int wmm = 0;
603
604         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
605                             sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
606                             ifsta->ssid_len);
607         if (!skb) {
608                 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
609                        "frame\n", dev->name);
610                 return;
611         }
612         skb_reserve(skb, local->hw.extra_tx_headroom);
613
614         mode = local->oper_hw_mode;
615         capab = ifsta->capab;
616         if (mode->mode == MODE_IEEE80211G) {
617                 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
618                         WLAN_CAPABILITY_SHORT_PREAMBLE;
619         }
620         bss = ieee80211_rx_bss_get(dev, ifsta->bssid, local->hw.conf.channel,
621                                    ifsta->ssid, ifsta->ssid_len);
622         if (bss) {
623                 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
624                         capab |= WLAN_CAPABILITY_PRIVACY;
625                 if (bss->wmm_ie) {
626                         wmm = 1;
627                 }
628                 ieee80211_rx_bss_put(dev, bss);
629         }
630
631         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
632         memset(mgmt, 0, 24);
633         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
634         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
635         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
636
637         if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
638                 skb_put(skb, 10);
639                 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
640                                                    IEEE80211_STYPE_REASSOC_REQ);
641                 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
642                 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
643                 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
644                        ETH_ALEN);
645         } else {
646                 skb_put(skb, 4);
647                 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
648                                                    IEEE80211_STYPE_ASSOC_REQ);
649                 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
650                 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
651         }
652
653         /* SSID */
654         ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
655         *pos++ = WLAN_EID_SSID;
656         *pos++ = ifsta->ssid_len;
657         memcpy(pos, ifsta->ssid, ifsta->ssid_len);
658
659         len = mode->num_rates;
660         if (len > 8)
661                 len = 8;
662         pos = skb_put(skb, len + 2);
663         *pos++ = WLAN_EID_SUPP_RATES;
664         *pos++ = len;
665         for (i = 0; i < len; i++) {
666                 int rate = mode->rates[i].rate;
667                 *pos++ = (u8) (rate / 5);
668         }
669
670         if (mode->num_rates > len) {
671                 pos = skb_put(skb, mode->num_rates - len + 2);
672                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
673                 *pos++ = mode->num_rates - len;
674                 for (i = len; i < mode->num_rates; i++) {
675                         int rate = mode->rates[i].rate;
676                         *pos++ = (u8) (rate / 5);
677                 }
678         }
679
680         if (ifsta->extra_ie) {
681                 pos = skb_put(skb, ifsta->extra_ie_len);
682                 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
683         }
684
685         if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
686                 pos = skb_put(skb, 9);
687                 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
688                 *pos++ = 7; /* len */
689                 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
690                 *pos++ = 0x50;
691                 *pos++ = 0xf2;
692                 *pos++ = 2; /* WME */
693                 *pos++ = 0; /* WME info */
694                 *pos++ = 1; /* WME ver */
695                 *pos++ = 0;
696         }
697         /* wmm support is a must to HT */
698         if (wmm && mode->ht_info.ht_supported) {
699                 __le16 tmp = cpu_to_le16(mode->ht_info.cap);
700                 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
701                 *pos++ = WLAN_EID_HT_CAPABILITY;
702                 *pos++ = sizeof(struct ieee80211_ht_cap);
703                 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
704                 memcpy(pos, &tmp, sizeof(u16));
705                 pos += sizeof(u16);
706                 *pos++ = (mode->ht_info.ampdu_factor |
707                                 (mode->ht_info.ampdu_density << 2));
708                 memcpy(pos, mode->ht_info.supp_mcs_set, 16);
709         }
710
711         kfree(ifsta->assocreq_ies);
712         ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
713         ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
714         if (ifsta->assocreq_ies)
715                 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
716
717         ieee80211_sta_tx(dev, skb, 0);
718 }
719
720
721 static void ieee80211_send_deauth(struct net_device *dev,
722                                   struct ieee80211_if_sta *ifsta, u16 reason)
723 {
724         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
725         struct sk_buff *skb;
726         struct ieee80211_mgmt *mgmt;
727
728         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
729         if (!skb) {
730                 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
731                        "frame\n", dev->name);
732                 return;
733         }
734         skb_reserve(skb, local->hw.extra_tx_headroom);
735
736         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
737         memset(mgmt, 0, 24);
738         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
739         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
740         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
741         mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
742                                            IEEE80211_STYPE_DEAUTH);
743         skb_put(skb, 2);
744         mgmt->u.deauth.reason_code = cpu_to_le16(reason);
745
746         ieee80211_sta_tx(dev, skb, 0);
747 }
748
749
750 static void ieee80211_send_disassoc(struct net_device *dev,
751                                     struct ieee80211_if_sta *ifsta, u16 reason)
752 {
753         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
754         struct sk_buff *skb;
755         struct ieee80211_mgmt *mgmt;
756
757         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
758         if (!skb) {
759                 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
760                        "frame\n", dev->name);
761                 return;
762         }
763         skb_reserve(skb, local->hw.extra_tx_headroom);
764
765         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
766         memset(mgmt, 0, 24);
767         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
768         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
769         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
770         mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
771                                            IEEE80211_STYPE_DISASSOC);
772         skb_put(skb, 2);
773         mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
774
775         ieee80211_sta_tx(dev, skb, 0);
776 }
777
778
779 static int ieee80211_privacy_mismatch(struct net_device *dev,
780                                       struct ieee80211_if_sta *ifsta)
781 {
782         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
783         struct ieee80211_sta_bss *bss;
784         int bss_privacy;
785         int wep_privacy;
786         int privacy_invoked;
787
788         if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
789                 return 0;
790
791         bss = ieee80211_rx_bss_get(dev, ifsta->bssid, local->hw.conf.channel,
792                                    ifsta->ssid, ifsta->ssid_len);
793         if (!bss)
794                 return 0;
795
796         bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
797         wep_privacy = !!ieee80211_sta_wep_configured(dev);
798         privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
799
800         ieee80211_rx_bss_put(dev, bss);
801
802         if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
803                 return 0;
804
805         return 1;
806 }
807
808
809 static void ieee80211_associate(struct net_device *dev,
810                                 struct ieee80211_if_sta *ifsta)
811 {
812         DECLARE_MAC_BUF(mac);
813
814         ifsta->assoc_tries++;
815         if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
816                 printk(KERN_DEBUG "%s: association with AP %s"
817                        " timed out\n",
818                        dev->name, print_mac(mac, ifsta->bssid));
819                 ifsta->state = IEEE80211_DISABLED;
820                 return;
821         }
822
823         ifsta->state = IEEE80211_ASSOCIATE;
824         printk(KERN_DEBUG "%s: associate with AP %s\n",
825                dev->name, print_mac(mac, ifsta->bssid));
826         if (ieee80211_privacy_mismatch(dev, ifsta)) {
827                 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
828                        "mixed-cell disabled - abort association\n", dev->name);
829                 ifsta->state = IEEE80211_DISABLED;
830                 return;
831         }
832
833         ieee80211_send_assoc(dev, ifsta);
834
835         mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
836 }
837
838
839 static void ieee80211_associated(struct net_device *dev,
840                                  struct ieee80211_if_sta *ifsta)
841 {
842         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
843         struct sta_info *sta;
844         int disassoc;
845         DECLARE_MAC_BUF(mac);
846
847         /* TODO: start monitoring current AP signal quality and number of
848          * missed beacons. Scan other channels every now and then and search
849          * for better APs. */
850         /* TODO: remove expired BSSes */
851
852         ifsta->state = IEEE80211_ASSOCIATED;
853
854         sta = sta_info_get(local, ifsta->bssid);
855         if (!sta) {
856                 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
857                        dev->name, print_mac(mac, ifsta->bssid));
858                 disassoc = 1;
859         } else {
860                 disassoc = 0;
861                 if (time_after(jiffies,
862                                sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
863                         if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
864                                 printk(KERN_DEBUG "%s: No ProbeResp from "
865                                        "current AP %s - assume out of "
866                                        "range\n",
867                                        dev->name, print_mac(mac, ifsta->bssid));
868                                 disassoc = 1;
869                                 sta_info_free(sta);
870                         } else
871                                 ieee80211_send_probe_req(dev, ifsta->bssid,
872                                                          local->scan_ssid,
873                                                          local->scan_ssid_len);
874                         ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
875                 } else {
876                         ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
877                         if (time_after(jiffies, ifsta->last_probe +
878                                        IEEE80211_PROBE_INTERVAL)) {
879                                 ifsta->last_probe = jiffies;
880                                 ieee80211_send_probe_req(dev, ifsta->bssid,
881                                                          ifsta->ssid,
882                                                          ifsta->ssid_len);
883                         }
884                 }
885                 sta_info_put(sta);
886         }
887         if (disassoc) {
888                 ifsta->state = IEEE80211_DISABLED;
889                 ieee80211_set_associated(dev, ifsta, 0);
890         } else {
891                 mod_timer(&ifsta->timer, jiffies +
892                                       IEEE80211_MONITORING_INTERVAL);
893         }
894 }
895
896
897 static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
898                                      u8 *ssid, size_t ssid_len)
899 {
900         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
901         struct ieee80211_hw_mode *mode;
902         struct sk_buff *skb;
903         struct ieee80211_mgmt *mgmt;
904         u8 *pos, *supp_rates, *esupp_rates = NULL;
905         int i;
906
907         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
908         if (!skb) {
909                 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
910                        "request\n", dev->name);
911                 return;
912         }
913         skb_reserve(skb, local->hw.extra_tx_headroom);
914
915         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
916         memset(mgmt, 0, 24);
917         mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
918                                            IEEE80211_STYPE_PROBE_REQ);
919         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
920         if (dst) {
921                 memcpy(mgmt->da, dst, ETH_ALEN);
922                 memcpy(mgmt->bssid, dst, ETH_ALEN);
923         } else {
924                 memset(mgmt->da, 0xff, ETH_ALEN);
925                 memset(mgmt->bssid, 0xff, ETH_ALEN);
926         }
927         pos = skb_put(skb, 2 + ssid_len);
928         *pos++ = WLAN_EID_SSID;
929         *pos++ = ssid_len;
930         memcpy(pos, ssid, ssid_len);
931
932         supp_rates = skb_put(skb, 2);
933         supp_rates[0] = WLAN_EID_SUPP_RATES;
934         supp_rates[1] = 0;
935         mode = local->oper_hw_mode;
936         for (i = 0; i < mode->num_rates; i++) {
937                 struct ieee80211_rate *rate = &mode->rates[i];
938                 if (!(rate->flags & IEEE80211_RATE_SUPPORTED))
939                         continue;
940                 if (esupp_rates) {
941                         pos = skb_put(skb, 1);
942                         esupp_rates[1]++;
943                 } else if (supp_rates[1] == 8) {
944                         esupp_rates = skb_put(skb, 3);
945                         esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
946                         esupp_rates[1] = 1;
947                         pos = &esupp_rates[2];
948                 } else {
949                         pos = skb_put(skb, 1);
950                         supp_rates[1]++;
951                 }
952                 *pos = rate->rate / 5;
953         }
954
955         ieee80211_sta_tx(dev, skb, 0);
956 }
957
958
959 static int ieee80211_sta_wep_configured(struct net_device *dev)
960 {
961         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
962         if (!sdata || !sdata->default_key ||
963             sdata->default_key->conf.alg != ALG_WEP)
964                 return 0;
965         return 1;
966 }
967
968
969 static void ieee80211_auth_completed(struct net_device *dev,
970                                      struct ieee80211_if_sta *ifsta)
971 {
972         printk(KERN_DEBUG "%s: authenticated\n", dev->name);
973         ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
974         ieee80211_associate(dev, ifsta);
975 }
976
977
978 static void ieee80211_auth_challenge(struct net_device *dev,
979                                      struct ieee80211_if_sta *ifsta,
980                                      struct ieee80211_mgmt *mgmt,
981                                      size_t len)
982 {
983         u8 *pos;
984         struct ieee802_11_elems elems;
985
986         printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
987         pos = mgmt->u.auth.variable;
988         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
989         if (!elems.challenge) {
990                 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
991                        "frame\n", dev->name);
992                 return;
993         }
994         ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
995                             elems.challenge_len + 2, 1);
996 }
997
998 static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
999                                         u8 dialog_token, u16 status, u16 policy,
1000                                         u16 buf_size, u16 timeout)
1001 {
1002         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1003         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1004         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1005         struct sk_buff *skb;
1006         struct ieee80211_mgmt *mgmt;
1007         u16 capab;
1008
1009         skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1010                                         sizeof(mgmt->u.action.u.addba_resp));
1011         if (!skb) {
1012                 printk(KERN_DEBUG "%s: failed to allocate buffer "
1013                        "for addba resp frame\n", dev->name);
1014                 return;
1015         }
1016
1017         skb_reserve(skb, local->hw.extra_tx_headroom);
1018         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1019         memset(mgmt, 0, 24);
1020         memcpy(mgmt->da, da, ETH_ALEN);
1021         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1022         if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1023                 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1024         else
1025                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1026         mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1027                                            IEEE80211_STYPE_ACTION);
1028
1029         skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1030         mgmt->u.action.category = WLAN_CATEGORY_BACK;
1031         mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1032         mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1033
1034         capab = (u16)(policy << 1);     /* bit 1 aggregation policy */
1035         capab |= (u16)(tid << 2);       /* bit 5:2 TID number */
1036         capab |= (u16)(buf_size << 6);  /* bit 15:6 max size of aggregation */
1037
1038         mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1039         mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1040         mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1041
1042         ieee80211_sta_tx(dev, skb, 0);
1043
1044         return;
1045 }
1046
1047 static void ieee80211_sta_process_addba_request(struct net_device *dev,
1048                                                 struct ieee80211_mgmt *mgmt,
1049                                                 size_t len)
1050 {
1051         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1052         struct ieee80211_hw *hw = &local->hw;
1053         struct ieee80211_conf *conf = &hw->conf;
1054         struct sta_info *sta;
1055         struct tid_ampdu_rx *tid_agg_rx;
1056         u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1057         u8 dialog_token;
1058         int ret = -EOPNOTSUPP;
1059         DECLARE_MAC_BUF(mac);
1060
1061         sta = sta_info_get(local, mgmt->sa);
1062         if (!sta)
1063                 return;
1064
1065         /* extract session parameters from addba request frame */
1066         dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1067         timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1068         start_seq_num =
1069                 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1070
1071         capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1072         ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1073         tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1074         buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1075
1076         status = WLAN_STATUS_REQUEST_DECLINED;
1077
1078         /* sanity check for incoming parameters:
1079          * check if configuration can support the BA policy
1080          * and if buffer size does not exceeds max value */
1081         if (((ba_policy != 1)
1082                 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1083                 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1084                 status = WLAN_STATUS_INVALID_QOS_PARAM;
1085 #ifdef CONFIG_MAC80211_HT_DEBUG
1086                 if (net_ratelimit())
1087                         printk(KERN_DEBUG "Block Ack Req with bad params from "
1088                                 "%s on tid %u. policy %d, buffer size %d\n",
1089                                 print_mac(mac, mgmt->sa), tid, ba_policy,
1090                                 buf_size);
1091 #endif /* CONFIG_MAC80211_HT_DEBUG */
1092                 goto end_no_lock;
1093         }
1094         /* determine default buffer size */
1095         if (buf_size == 0) {
1096                 struct ieee80211_hw_mode *mode = conf->mode;
1097                 buf_size = IEEE80211_MIN_AMPDU_BUF;
1098                 buf_size = buf_size << mode->ht_info.ampdu_factor;
1099         }
1100
1101         tid_agg_rx = &sta->ampdu_mlme.tid_rx[tid];
1102
1103         /* examine state machine */
1104         spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1105
1106         if (tid_agg_rx->state != HT_AGG_STATE_IDLE) {
1107 #ifdef CONFIG_MAC80211_HT_DEBUG
1108                 if (net_ratelimit())
1109                         printk(KERN_DEBUG "unexpected Block Ack Req from "
1110                                 "%s on tid %u\n",
1111                                 print_mac(mac, mgmt->sa), tid);
1112 #endif /* CONFIG_MAC80211_HT_DEBUG */
1113                 goto end;
1114         }
1115
1116         /* prepare reordering buffer */
1117         tid_agg_rx->reorder_buf =
1118                 kmalloc(buf_size * sizeof(struct sk_buf *), GFP_ATOMIC);
1119         if ((!tid_agg_rx->reorder_buf) && net_ratelimit()) {
1120                 printk(KERN_ERR "can not allocate reordering buffer "
1121                                                 "to tid %d\n", tid);
1122                 goto end;
1123         }
1124         memset(tid_agg_rx->reorder_buf, 0,
1125                 buf_size * sizeof(struct sk_buf *));
1126
1127         if (local->ops->ampdu_action)
1128                 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1129                                                sta->addr, tid, start_seq_num);
1130 #ifdef CONFIG_MAC80211_HT_DEBUG
1131         printk(KERN_DEBUG "Rx A-MPDU on tid %d result %d", tid, ret);
1132 #endif /* CONFIG_MAC80211_HT_DEBUG */
1133
1134         if (ret) {
1135                 kfree(tid_agg_rx->reorder_buf);
1136                 goto end;
1137         }
1138
1139         /* change state and send addba resp */
1140         tid_agg_rx->state = HT_AGG_STATE_OPERATIONAL;
1141         tid_agg_rx->dialog_token = dialog_token;
1142         tid_agg_rx->ssn = start_seq_num;
1143         tid_agg_rx->head_seq_num = start_seq_num;
1144         tid_agg_rx->buf_size = buf_size;
1145         tid_agg_rx->timeout = timeout;
1146         tid_agg_rx->stored_mpdu_num = 0;
1147         status = WLAN_STATUS_SUCCESS;
1148 end:
1149         spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1150
1151 end_no_lock:
1152         ieee80211_send_addba_resp(sta->dev, sta->addr, tid, dialog_token,
1153                                 status, 1, buf_size, timeout);
1154         sta_info_put(sta);
1155 }
1156
1157 void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1158                                 u16 initiator, u16 reason_code)
1159 {
1160         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1161         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1162         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1163         struct sk_buff *skb;
1164         struct ieee80211_mgmt *mgmt;
1165         u16 params;
1166
1167         skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1168                                         sizeof(mgmt->u.action.u.delba));
1169
1170         if (!skb) {
1171                 printk(KERN_ERR "%s: failed to allocate buffer "
1172                                         "for delba frame\n", dev->name);
1173                 return;
1174         }
1175
1176         skb_reserve(skb, local->hw.extra_tx_headroom);
1177         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1178         memset(mgmt, 0, 24);
1179         memcpy(mgmt->da, da, ETH_ALEN);
1180         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1181         if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1182                 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1183         else
1184                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1185         mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1186                                         IEEE80211_STYPE_ACTION);
1187
1188         skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1189
1190         mgmt->u.action.category = WLAN_CATEGORY_BACK;
1191         mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1192         params = (u16)(initiator << 11);        /* bit 11 initiator */
1193         params |= (u16)(tid << 12);             /* bit 15:12 TID number */
1194
1195         mgmt->u.action.u.delba.params = cpu_to_le16(params);
1196         mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1197
1198         ieee80211_sta_tx(dev, skb, 0);
1199 }
1200
1201 void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1202                                         u16 initiator, u16 reason)
1203 {
1204         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1205         struct ieee80211_hw *hw = &local->hw;
1206         struct sta_info *sta;
1207         int ret, i;
1208
1209         sta = sta_info_get(local, ra);
1210         if (!sta)
1211                 return;
1212
1213         /* check if TID is in operational state */
1214         spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1215         if (sta->ampdu_mlme.tid_rx[tid].state
1216                                 != HT_AGG_STATE_OPERATIONAL) {
1217                 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1218                 if (net_ratelimit())
1219                         printk(KERN_DEBUG "rx BA session requested to stop on "
1220                                 "inactive tid %d\n", tid);
1221                 sta_info_put(sta);
1222                 return;
1223         }
1224         sta->ampdu_mlme.tid_rx[tid].state =
1225                 HT_AGG_STATE_REQ_STOP_BA_MSK |
1226                 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1227                 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1228
1229         /* stop HW Rx aggregation. ampdu_action existence
1230          * already verified in session init so we add the BUG_ON */
1231         BUG_ON(!local->ops->ampdu_action);
1232
1233         ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
1234                                         ra, tid, EINVAL);
1235         if (ret)
1236                 printk(KERN_DEBUG "HW problem - can not stop rx "
1237                                 "aggergation for tid %d\n", tid);
1238
1239         /* shutdown timer has not expired */
1240         if (initiator != WLAN_BACK_TIMER)
1241                 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid].
1242                                         session_timer);
1243
1244         /* check if this is a self generated aggregation halt */
1245         if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1246                 ieee80211_send_delba(dev, ra, tid, 0, reason);
1247
1248         /* free the reordering buffer */
1249         for (i = 0; i < sta->ampdu_mlme.tid_rx[tid].buf_size; i++) {
1250                 if (sta->ampdu_mlme.tid_rx[tid].reorder_buf[i]) {
1251                         /* release the reordered frames */
1252                         dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid].reorder_buf[i]);
1253                         sta->ampdu_mlme.tid_rx[tid].stored_mpdu_num--;
1254                         sta->ampdu_mlme.tid_rx[tid].reorder_buf[i] = NULL;
1255                 }
1256         }
1257         kfree(sta->ampdu_mlme.tid_rx[tid].reorder_buf);
1258
1259         sta->ampdu_mlme.tid_rx[tid].state = HT_AGG_STATE_IDLE;
1260         sta_info_put(sta);
1261 }
1262
1263 static void ieee80211_sta_process_delba(struct net_device *dev,
1264                         struct ieee80211_mgmt *mgmt, size_t len)
1265 {
1266         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1267         struct sta_info *sta;
1268         u16 tid, params;
1269         u16 initiator;
1270         DECLARE_MAC_BUF(mac);
1271
1272         sta = sta_info_get(local, mgmt->sa);
1273         if (!sta)
1274                 return;
1275
1276         params = le16_to_cpu(mgmt->u.action.u.delba.params);
1277         tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1278         initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1279
1280 #ifdef CONFIG_MAC80211_HT_DEBUG
1281         if (net_ratelimit())
1282                 printk(KERN_DEBUG "delba from %s on tid %d reason code %d\n",
1283                         print_mac(mac, mgmt->sa), tid,
1284                         mgmt->u.action.u.delba.reason_code);
1285 #endif /* CONFIG_MAC80211_HT_DEBUG */
1286
1287         if (initiator == WLAN_BACK_INITIATOR)
1288                 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1289                                                  WLAN_BACK_INITIATOR, 0);
1290         sta_info_put(sta);
1291 }
1292
1293 /*
1294  * After receiving Block Ack Request (BAR) we activated a
1295  * timer after each frame arrives from the originator.
1296  * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1297  */
1298 void sta_rx_agg_session_timer_expired(unsigned long data)
1299 {
1300         /* not an elegant detour, but there is no choice as the timer passes
1301          * only one argument, and verious sta_info are needed here, so init
1302          * flow in sta_info_add gives the TID as data, while the timer_to_id
1303          * array gives the sta through container_of */
1304         u8 *ptid = (u8 *)data;
1305         u8 *timer_to_id = ptid - *ptid;
1306         struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1307                                          timer_to_tid[0]);
1308
1309         printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1310         ieee80211_sta_stop_rx_ba_session(sta->dev, sta->addr, (u16)*ptid,
1311                                          WLAN_BACK_TIMER,
1312                                          WLAN_REASON_QSTA_TIMEOUT);
1313 }
1314
1315
1316 static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1317                                    struct ieee80211_if_sta *ifsta,
1318                                    struct ieee80211_mgmt *mgmt,
1319                                    size_t len)
1320 {
1321         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1322         u16 auth_alg, auth_transaction, status_code;
1323         DECLARE_MAC_BUF(mac);
1324
1325         if (ifsta->state != IEEE80211_AUTHENTICATE &&
1326             sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
1327                 printk(KERN_DEBUG "%s: authentication frame received from "
1328                        "%s, but not in authenticate state - ignored\n",
1329                        dev->name, print_mac(mac, mgmt->sa));
1330                 return;
1331         }
1332
1333         if (len < 24 + 6) {
1334                 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
1335                        "received from %s - ignored\n",
1336                        dev->name, len, print_mac(mac, mgmt->sa));
1337                 return;
1338         }
1339
1340         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1341             memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1342                 printk(KERN_DEBUG "%s: authentication frame received from "
1343                        "unknown AP (SA=%s BSSID=%s) - "
1344                        "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1345                        print_mac(mac, mgmt->bssid));
1346                 return;
1347         }
1348
1349         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1350             memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1351                 printk(KERN_DEBUG "%s: authentication frame received from "
1352                        "unknown BSSID (SA=%s BSSID=%s) - "
1353                        "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1354                        print_mac(mac, mgmt->bssid));
1355                 return;
1356         }
1357
1358         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1359         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1360         status_code = le16_to_cpu(mgmt->u.auth.status_code);
1361
1362         printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
1363                "transaction=%d status=%d)\n",
1364                dev->name, print_mac(mac, mgmt->sa), auth_alg,
1365                auth_transaction, status_code);
1366
1367         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1368                 /* IEEE 802.11 standard does not require authentication in IBSS
1369                  * networks and most implementations do not seem to use it.
1370                  * However, try to reply to authentication attempts if someone
1371                  * has actually implemented this.
1372                  * TODO: Could implement shared key authentication. */
1373                 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1374                         printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1375                                "frame (alg=%d transaction=%d)\n",
1376                                dev->name, auth_alg, auth_transaction);
1377                         return;
1378                 }
1379                 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1380         }
1381
1382         if (auth_alg != ifsta->auth_alg ||
1383             auth_transaction != ifsta->auth_transaction) {
1384                 printk(KERN_DEBUG "%s: unexpected authentication frame "
1385                        "(alg=%d transaction=%d)\n",
1386                        dev->name, auth_alg, auth_transaction);
1387                 return;
1388         }
1389
1390         if (status_code != WLAN_STATUS_SUCCESS) {
1391                 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1392                        "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1393                 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1394                         u8 algs[3];
1395                         const int num_algs = ARRAY_SIZE(algs);
1396                         int i, pos;
1397                         algs[0] = algs[1] = algs[2] = 0xff;
1398                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1399                                 algs[0] = WLAN_AUTH_OPEN;
1400                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1401                                 algs[1] = WLAN_AUTH_SHARED_KEY;
1402                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1403                                 algs[2] = WLAN_AUTH_LEAP;
1404                         if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1405                                 pos = 0;
1406                         else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1407                                 pos = 1;
1408                         else
1409                                 pos = 2;
1410                         for (i = 0; i < num_algs; i++) {
1411                                 pos++;
1412                                 if (pos >= num_algs)
1413                                         pos = 0;
1414                                 if (algs[pos] == ifsta->auth_alg ||
1415                                     algs[pos] == 0xff)
1416                                         continue;
1417                                 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1418                                     !ieee80211_sta_wep_configured(dev))
1419                                         continue;
1420                                 ifsta->auth_alg = algs[pos];
1421                                 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1422                                        "next try\n",
1423                                        dev->name, ifsta->auth_alg);
1424                                 break;
1425                         }
1426                 }
1427                 return;
1428         }
1429
1430         switch (ifsta->auth_alg) {
1431         case WLAN_AUTH_OPEN:
1432         case WLAN_AUTH_LEAP:
1433                 ieee80211_auth_completed(dev, ifsta);
1434                 break;
1435         case WLAN_AUTH_SHARED_KEY:
1436                 if (ifsta->auth_transaction == 4)
1437                         ieee80211_auth_completed(dev, ifsta);
1438                 else
1439                         ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1440                 break;
1441         }
1442 }
1443
1444
1445 static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1446                                      struct ieee80211_if_sta *ifsta,
1447                                      struct ieee80211_mgmt *mgmt,
1448                                      size_t len)
1449 {
1450         u16 reason_code;
1451         DECLARE_MAC_BUF(mac);
1452
1453         if (len < 24 + 2) {
1454                 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1455                        "received from %s - ignored\n",
1456                        dev->name, len, print_mac(mac, mgmt->sa));
1457                 return;
1458         }
1459
1460         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1461                 printk(KERN_DEBUG "%s: deauthentication frame received from "
1462                        "unknown AP (SA=%s BSSID=%s) - "
1463                        "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1464                        print_mac(mac, mgmt->bssid));
1465                 return;
1466         }
1467
1468         reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1469
1470         printk(KERN_DEBUG "%s: RX deauthentication from %s"
1471                " (reason=%d)\n",
1472                dev->name, print_mac(mac, mgmt->sa), reason_code);
1473
1474         if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
1475                 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1476         }
1477
1478         if (ifsta->state == IEEE80211_AUTHENTICATE ||
1479             ifsta->state == IEEE80211_ASSOCIATE ||
1480             ifsta->state == IEEE80211_ASSOCIATED) {
1481                 ifsta->state = IEEE80211_AUTHENTICATE;
1482                 mod_timer(&ifsta->timer, jiffies +
1483                                       IEEE80211_RETRY_AUTH_INTERVAL);
1484         }
1485
1486         ieee80211_set_disassoc(dev, ifsta, 1);
1487         ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1488 }
1489
1490
1491 static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1492                                        struct ieee80211_if_sta *ifsta,
1493                                        struct ieee80211_mgmt *mgmt,
1494                                        size_t len)
1495 {
1496         u16 reason_code;
1497         DECLARE_MAC_BUF(mac);
1498
1499         if (len < 24 + 2) {
1500                 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1501                        "received from %s - ignored\n",
1502                        dev->name, len, print_mac(mac, mgmt->sa));
1503                 return;
1504         }
1505
1506         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1507                 printk(KERN_DEBUG "%s: disassociation frame received from "
1508                        "unknown AP (SA=%s BSSID=%s) - "
1509                        "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1510                        print_mac(mac, mgmt->bssid));
1511                 return;
1512         }
1513
1514         reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1515
1516         printk(KERN_DEBUG "%s: RX disassociation from %s"
1517                " (reason=%d)\n",
1518                dev->name, print_mac(mac, mgmt->sa), reason_code);
1519
1520         if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1521                 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1522
1523         if (ifsta->state == IEEE80211_ASSOCIATED) {
1524                 ifsta->state = IEEE80211_ASSOCIATE;
1525                 mod_timer(&ifsta->timer, jiffies +
1526                                       IEEE80211_RETRY_AUTH_INTERVAL);
1527         }
1528
1529         ieee80211_set_disassoc(dev, ifsta, 0);
1530 }
1531
1532
1533 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1534                                          struct ieee80211_if_sta *ifsta,
1535                                          struct ieee80211_mgmt *mgmt,
1536                                          size_t len,
1537                                          int reassoc)
1538 {
1539         struct ieee80211_local *local = sdata->local;
1540         struct net_device *dev = sdata->dev;
1541         struct ieee80211_hw_mode *mode;
1542         struct sta_info *sta;
1543         u32 rates;
1544         u16 capab_info, status_code, aid;
1545         struct ieee802_11_elems elems;
1546         struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
1547         u8 *pos;
1548         int i, j;
1549         DECLARE_MAC_BUF(mac);
1550
1551         /* AssocResp and ReassocResp have identical structure, so process both
1552          * of them in this function. */
1553
1554         if (ifsta->state != IEEE80211_ASSOCIATE) {
1555                 printk(KERN_DEBUG "%s: association frame received from "
1556                        "%s, but not in associate state - ignored\n",
1557                        dev->name, print_mac(mac, mgmt->sa));
1558                 return;
1559         }
1560
1561         if (len < 24 + 6) {
1562                 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1563                        "received from %s - ignored\n",
1564                        dev->name, len, print_mac(mac, mgmt->sa));
1565                 return;
1566         }
1567
1568         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1569                 printk(KERN_DEBUG "%s: association frame received from "
1570                        "unknown AP (SA=%s BSSID=%s) - "
1571                        "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1572                        print_mac(mac, mgmt->bssid));
1573                 return;
1574         }
1575
1576         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1577         status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1578         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1579
1580         printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1581                "status=%d aid=%d)\n",
1582                dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1583                capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1584
1585         if (status_code != WLAN_STATUS_SUCCESS) {
1586                 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1587                        dev->name, status_code);
1588                 /* if this was a reassociation, ensure we try a "full"
1589                  * association next time. This works around some broken APs
1590                  * which do not correctly reject reassociation requests. */
1591                 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1592                 return;
1593         }
1594
1595         if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1596                 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1597                        "set\n", dev->name, aid);
1598         aid &= ~(BIT(15) | BIT(14));
1599
1600         pos = mgmt->u.assoc_resp.variable;
1601         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1602
1603         if (!elems.supp_rates) {
1604                 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1605                        dev->name);
1606                 return;
1607         }
1608
1609         printk(KERN_DEBUG "%s: associated\n", dev->name);
1610         ifsta->aid = aid;
1611         ifsta->ap_capab = capab_info;
1612
1613         kfree(ifsta->assocresp_ies);
1614         ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1615         ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1616         if (ifsta->assocresp_ies)
1617                 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1618
1619         /* set AID, ieee80211_set_associated() will tell the driver */
1620         bss_conf->aid = aid;
1621         ieee80211_set_associated(dev, ifsta, 1);
1622
1623         /* Add STA entry for the AP */
1624         sta = sta_info_get(local, ifsta->bssid);
1625         if (!sta) {
1626                 struct ieee80211_sta_bss *bss;
1627                 sta = sta_info_add(local, dev, ifsta->bssid, GFP_KERNEL);
1628                 if (!sta) {
1629                         printk(KERN_DEBUG "%s: failed to add STA entry for the"
1630                                " AP\n", dev->name);
1631                         return;
1632                 }
1633                 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
1634                                            local->hw.conf.channel,
1635                                            ifsta->ssid, ifsta->ssid_len);
1636                 if (bss) {
1637                         sta->last_rssi = bss->rssi;
1638                         sta->last_signal = bss->signal;
1639                         sta->last_noise = bss->noise;
1640                         ieee80211_rx_bss_put(dev, bss);
1641                 }
1642         }
1643
1644         sta->dev = dev;
1645         sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP;
1646
1647         rates = 0;
1648         mode = local->oper_hw_mode;
1649         for (i = 0; i < elems.supp_rates_len; i++) {
1650                 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1651                 for (j = 0; j < mode->num_rates; j++)
1652                         if (mode->rates[j].rate == rate)
1653                                 rates |= BIT(j);
1654         }
1655         for (i = 0; i < elems.ext_supp_rates_len; i++) {
1656                 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1657                 for (j = 0; j < mode->num_rates; j++)
1658                         if (mode->rates[j].rate == rate)
1659                                 rates |= BIT(j);
1660         }
1661         sta->supp_rates = rates;
1662
1663         if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1664             local->ops->conf_ht) {
1665                 struct ieee80211_ht_bss_info bss_info;
1666
1667                 ieee80211_ht_cap_ie_to_ht_info(
1668                                 (struct ieee80211_ht_cap *)
1669                                 elems.ht_cap_elem, &sta->ht_info);
1670                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1671                                 (struct ieee80211_ht_addt_info *)
1672                                 elems.ht_info_elem, &bss_info);
1673                 ieee80211_hw_config_ht(local, 1, &sta->ht_info, &bss_info);
1674         }
1675
1676         rate_control_rate_init(sta, local);
1677
1678         if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1679                 sta->flags |= WLAN_STA_WME;
1680                 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1681                                          elems.wmm_param_len);
1682         }
1683
1684
1685         sta_info_put(sta);
1686
1687         ieee80211_associated(dev, ifsta);
1688 }
1689
1690
1691 /* Caller must hold local->sta_bss_lock */
1692 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1693                                         struct ieee80211_sta_bss *bss)
1694 {
1695         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1696         bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1697         local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1698 }
1699
1700
1701 /* Caller must hold local->sta_bss_lock */
1702 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1703                                         struct ieee80211_sta_bss *bss)
1704 {
1705         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1706         struct ieee80211_sta_bss *b, *prev = NULL;
1707         b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1708         while (b) {
1709                 if (b == bss) {
1710                         if (!prev)
1711                                 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1712                                         bss->hnext;
1713                         else
1714                                 prev->hnext = bss->hnext;
1715                         break;
1716                 }
1717                 prev = b;
1718                 b = b->hnext;
1719         }
1720 }
1721
1722
1723 static struct ieee80211_sta_bss *
1724 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int channel,
1725                      u8 *ssid, u8 ssid_len)
1726 {
1727         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1728         struct ieee80211_sta_bss *bss;
1729
1730         bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
1731         if (!bss)
1732                 return NULL;
1733         atomic_inc(&bss->users);
1734         atomic_inc(&bss->users);
1735         memcpy(bss->bssid, bssid, ETH_ALEN);
1736         bss->channel = channel;
1737         if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
1738                 memcpy(bss->ssid, ssid, ssid_len);
1739                 bss->ssid_len = ssid_len;
1740         }
1741
1742         spin_lock_bh(&local->sta_bss_lock);
1743         /* TODO: order by RSSI? */
1744         list_add_tail(&bss->list, &local->sta_bss_list);
1745         __ieee80211_rx_bss_hash_add(dev, bss);
1746         spin_unlock_bh(&local->sta_bss_lock);
1747         return bss;
1748 }
1749
1750
1751 static struct ieee80211_sta_bss *
1752 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int channel,
1753                      u8 *ssid, u8 ssid_len)
1754 {
1755         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1756         struct ieee80211_sta_bss *bss;
1757
1758         spin_lock_bh(&local->sta_bss_lock);
1759         bss = local->sta_bss_hash[STA_HASH(bssid)];
1760         while (bss) {
1761                 if (!memcmp(bss->bssid, bssid, ETH_ALEN) &&
1762                     bss->channel == channel &&
1763                     bss->ssid_len == ssid_len &&
1764                     (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
1765                         atomic_inc(&bss->users);
1766                         break;
1767                 }
1768                 bss = bss->hnext;
1769         }
1770         spin_unlock_bh(&local->sta_bss_lock);
1771         return bss;
1772 }
1773
1774
1775 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1776 {
1777         kfree(bss->wpa_ie);
1778         kfree(bss->rsn_ie);
1779         kfree(bss->wmm_ie);
1780         kfree(bss->ht_ie);
1781         kfree(bss);
1782 }
1783
1784
1785 static void ieee80211_rx_bss_put(struct net_device *dev,
1786                                  struct ieee80211_sta_bss *bss)
1787 {
1788         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1789         if (!atomic_dec_and_test(&bss->users))
1790                 return;
1791
1792         spin_lock_bh(&local->sta_bss_lock);
1793         __ieee80211_rx_bss_hash_del(dev, bss);
1794         list_del(&bss->list);
1795         spin_unlock_bh(&local->sta_bss_lock);
1796         ieee80211_rx_bss_free(bss);
1797 }
1798
1799
1800 void ieee80211_rx_bss_list_init(struct net_device *dev)
1801 {
1802         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1803         spin_lock_init(&local->sta_bss_lock);
1804         INIT_LIST_HEAD(&local->sta_bss_list);
1805 }
1806
1807
1808 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1809 {
1810         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1811         struct ieee80211_sta_bss *bss, *tmp;
1812
1813         list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1814                 ieee80211_rx_bss_put(dev, bss);
1815 }
1816
1817
1818 static void ieee80211_rx_bss_info(struct net_device *dev,
1819                                   struct ieee80211_mgmt *mgmt,
1820                                   size_t len,
1821                                   struct ieee80211_rx_status *rx_status,
1822                                   int beacon)
1823 {
1824         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1825         struct ieee802_11_elems elems;
1826         size_t baselen;
1827         int channel, clen;
1828         struct ieee80211_sta_bss *bss;
1829         struct sta_info *sta;
1830         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1831         u64 timestamp;
1832         DECLARE_MAC_BUF(mac);
1833         DECLARE_MAC_BUF(mac2);
1834
1835         if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1836                 return; /* ignore ProbeResp to foreign address */
1837
1838 #if 0
1839         printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
1840                dev->name, beacon ? "Beacon" : "Probe Response",
1841                print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
1842 #endif
1843
1844         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1845         if (baselen > len)
1846                 return;
1847
1848         timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1849
1850         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
1851             memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1852 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1853                 static unsigned long last_tsf_debug = 0;
1854                 u64 tsf;
1855                 if (local->ops->get_tsf)
1856                         tsf = local->ops->get_tsf(local_to_hw(local));
1857                 else
1858                         tsf = -1LLU;
1859                 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1860                         printk(KERN_DEBUG "RX beacon SA=%s BSSID="
1861                                "%s TSF=0x%llx BCN=0x%llx diff=%lld "
1862                                "@%lu\n",
1863                                print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->bssid),
1864                                (unsigned long long)tsf,
1865                                (unsigned long long)timestamp,
1866                                (unsigned long long)(tsf - timestamp),
1867                                jiffies);
1868                         last_tsf_debug = jiffies;
1869                 }
1870 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1871         }
1872
1873         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
1874
1875         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1876             memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1877             (sta = sta_info_get(local, mgmt->sa))) {
1878                 struct ieee80211_hw_mode *mode;
1879                 struct ieee80211_rate *rates;
1880                 size_t num_rates;
1881                 u32 supp_rates, prev_rates;
1882                 int i, j;
1883
1884                 mode = local->sta_sw_scanning ?
1885                        local->scan_hw_mode : local->oper_hw_mode;
1886
1887                 if (local->sta_hw_scanning) {
1888                         /* search for the correct mode matches the beacon */
1889                         list_for_each_entry(mode, &local->modes_list, list)
1890                                 if (mode->mode == rx_status->phymode)
1891                                         break;
1892
1893                         if (mode == NULL)
1894                                 mode = local->oper_hw_mode;
1895                 }
1896                 rates = mode->rates;
1897                 num_rates = mode->num_rates;
1898
1899                 supp_rates = 0;
1900                 for (i = 0; i < elems.supp_rates_len +
1901                              elems.ext_supp_rates_len; i++) {
1902                         u8 rate = 0;
1903                         int own_rate;
1904                         if (i < elems.supp_rates_len)
1905                                 rate = elems.supp_rates[i];
1906                         else if (elems.ext_supp_rates)
1907                                 rate = elems.ext_supp_rates
1908                                         [i - elems.supp_rates_len];
1909                         own_rate = 5 * (rate & 0x7f);
1910                         for (j = 0; j < num_rates; j++)
1911                                 if (rates[j].rate == own_rate)
1912                                         supp_rates |= BIT(j);
1913                 }
1914
1915                 prev_rates = sta->supp_rates;
1916                 sta->supp_rates &= supp_rates;
1917                 if (sta->supp_rates == 0) {
1918                         /* No matching rates - this should not really happen.
1919                          * Make sure that at least one rate is marked
1920                          * supported to avoid issues with TX rate ctrl. */
1921                         sta->supp_rates = sdata->u.sta.supp_rates_bits;
1922                 }
1923                 if (sta->supp_rates != prev_rates) {
1924                         printk(KERN_DEBUG "%s: updated supp_rates set for "
1925                                "%s based on beacon info (0x%x & 0x%x -> "
1926                                "0x%x)\n",
1927                                dev->name, print_mac(mac, sta->addr), prev_rates,
1928                                supp_rates, sta->supp_rates);
1929                 }
1930                 sta_info_put(sta);
1931         }
1932
1933         if (!elems.ssid)
1934                 return;
1935
1936         if (elems.ds_params && elems.ds_params_len == 1)
1937                 channel = elems.ds_params[0];
1938         else
1939                 channel = rx_status->channel;
1940
1941         bss = ieee80211_rx_bss_get(dev, mgmt->bssid, channel,
1942                                    elems.ssid, elems.ssid_len);
1943         if (!bss) {
1944                 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, channel,
1945                                            elems.ssid, elems.ssid_len);
1946                 if (!bss)
1947                         return;
1948         } else {
1949 #if 0
1950                 /* TODO: order by RSSI? */
1951                 spin_lock_bh(&local->sta_bss_lock);
1952                 list_move_tail(&bss->list, &local->sta_bss_list);
1953                 spin_unlock_bh(&local->sta_bss_lock);
1954 #endif
1955         }
1956
1957         if (bss->probe_resp && beacon) {
1958                 /* Do not allow beacon to override data from Probe Response. */
1959                 ieee80211_rx_bss_put(dev, bss);
1960                 return;
1961         }
1962
1963         /* save the ERP value so that it is available at association time */
1964         if (elems.erp_info && elems.erp_info_len >= 1) {
1965                 bss->erp_value = elems.erp_info[0];
1966                 bss->has_erp_value = 1;
1967         }
1968
1969         bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1970         bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
1971
1972         bss->supp_rates_len = 0;
1973         if (elems.supp_rates) {
1974                 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1975                 if (clen > elems.supp_rates_len)
1976                         clen = elems.supp_rates_len;
1977                 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1978                        clen);
1979                 bss->supp_rates_len += clen;
1980         }
1981         if (elems.ext_supp_rates) {
1982                 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1983                 if (clen > elems.ext_supp_rates_len)
1984                         clen = elems.ext_supp_rates_len;
1985                 memcpy(&bss->supp_rates[bss->supp_rates_len],
1986                        elems.ext_supp_rates, clen);
1987                 bss->supp_rates_len += clen;
1988         }
1989
1990         if (elems.wpa &&
1991             (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1992              memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1993                 kfree(bss->wpa_ie);
1994                 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1995                 if (bss->wpa_ie) {
1996                         memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1997                         bss->wpa_ie_len = elems.wpa_len + 2;
1998                 } else
1999                         bss->wpa_ie_len = 0;
2000         } else if (!elems.wpa && bss->wpa_ie) {
2001                 kfree(bss->wpa_ie);
2002                 bss->wpa_ie = NULL;
2003                 bss->wpa_ie_len = 0;
2004         }
2005
2006         if (elems.rsn &&
2007             (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2008              memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2009                 kfree(bss->rsn_ie);
2010                 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2011                 if (bss->rsn_ie) {
2012                         memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2013                         bss->rsn_ie_len = elems.rsn_len + 2;
2014                 } else
2015                         bss->rsn_ie_len = 0;
2016         } else if (!elems.rsn && bss->rsn_ie) {
2017                 kfree(bss->rsn_ie);
2018                 bss->rsn_ie = NULL;
2019                 bss->rsn_ie_len = 0;
2020         }
2021
2022         if (elems.wmm_param &&
2023             (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2024              memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2025                 kfree(bss->wmm_ie);
2026                 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2027                 if (bss->wmm_ie) {
2028                         memcpy(bss->wmm_ie, elems.wmm_param - 2,
2029                                elems.wmm_param_len + 2);
2030                         bss->wmm_ie_len = elems.wmm_param_len + 2;
2031                 } else
2032                         bss->wmm_ie_len = 0;
2033         } else if (!elems.wmm_param && bss->wmm_ie) {
2034                 kfree(bss->wmm_ie);
2035                 bss->wmm_ie = NULL;
2036                 bss->wmm_ie_len = 0;
2037         }
2038         if (elems.ht_cap_elem &&
2039             (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2040              memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2041                 kfree(bss->ht_ie);
2042                 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2043                 if (bss->ht_ie) {
2044                         memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2045                                elems.ht_cap_elem_len + 2);
2046                         bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2047                 } else
2048                         bss->ht_ie_len = 0;
2049         } else if (!elems.ht_cap_elem && bss->ht_ie) {
2050                 kfree(bss->ht_ie);
2051                 bss->ht_ie = NULL;
2052                 bss->ht_ie_len = 0;
2053         }
2054
2055         bss->hw_mode = rx_status->phymode;
2056         bss->freq = rx_status->freq;
2057         if (channel != rx_status->channel &&
2058             (bss->hw_mode == MODE_IEEE80211G ||
2059              bss->hw_mode == MODE_IEEE80211B) &&
2060             channel >= 1 && channel <= 14) {
2061                 static const int freq_list[] = {
2062                         2412, 2417, 2422, 2427, 2432, 2437, 2442,
2063                         2447, 2452, 2457, 2462, 2467, 2472, 2484
2064                 };
2065                 /* IEEE 802.11g/b mode can receive packets from neighboring
2066                  * channels, so map the channel into frequency. */
2067                 bss->freq = freq_list[channel - 1];
2068         }
2069         bss->timestamp = timestamp;
2070         bss->last_update = jiffies;
2071         bss->rssi = rx_status->ssi;
2072         bss->signal = rx_status->signal;
2073         bss->noise = rx_status->noise;
2074         if (!beacon)
2075                 bss->probe_resp++;
2076         ieee80211_rx_bss_put(dev, bss);
2077 }
2078
2079
2080 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2081                                          struct ieee80211_mgmt *mgmt,
2082                                          size_t len,
2083                                          struct ieee80211_rx_status *rx_status)
2084 {
2085         ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2086 }
2087
2088
2089 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2090                                      struct ieee80211_mgmt *mgmt,
2091                                      size_t len,
2092                                      struct ieee80211_rx_status *rx_status)
2093 {
2094         struct ieee80211_sub_if_data *sdata;
2095         struct ieee80211_if_sta *ifsta;
2096         size_t baselen;
2097         struct ieee802_11_elems elems;
2098         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2099         struct ieee80211_conf *conf = &local->hw.conf;
2100         u32 changed = 0;
2101
2102         ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2103
2104         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2105         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2106                 return;
2107         ifsta = &sdata->u.sta;
2108
2109         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
2110             memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2111                 return;
2112
2113         /* Process beacon from the current BSS */
2114         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2115         if (baselen > len)
2116                 return;
2117
2118         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2119
2120         if (elems.erp_info && elems.erp_info_len >= 1)
2121                 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2122
2123         if (elems.ht_cap_elem && elems.ht_info_elem &&
2124             elems.wmm_param && local->ops->conf_ht &&
2125             conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2126                 struct ieee80211_ht_bss_info bss_info;
2127
2128                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2129                                 (struct ieee80211_ht_addt_info *)
2130                                 elems.ht_info_elem, &bss_info);
2131                 /* check if AP changed bss inforamation */
2132                 if ((conf->ht_bss_conf.primary_channel !=
2133                      bss_info.primary_channel) ||
2134                     (conf->ht_bss_conf.bss_cap != bss_info.bss_cap) ||
2135                     (conf->ht_bss_conf.bss_op_mode != bss_info.bss_op_mode))
2136                         ieee80211_hw_config_ht(local, 1, &conf->ht_conf,
2137                                                 &bss_info);
2138         }
2139
2140         if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2141                 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2142                                          elems.wmm_param_len);
2143         }
2144
2145         ieee80211_bss_info_change_notify(sdata, changed);
2146 }
2147
2148
2149 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2150                                         struct ieee80211_if_sta *ifsta,
2151                                         struct ieee80211_mgmt *mgmt,
2152                                         size_t len,
2153                                         struct ieee80211_rx_status *rx_status)
2154 {
2155         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2156         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2157         int tx_last_beacon;
2158         struct sk_buff *skb;
2159         struct ieee80211_mgmt *resp;
2160         u8 *pos, *end;
2161         DECLARE_MAC_BUF(mac);
2162 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2163         DECLARE_MAC_BUF(mac2);
2164         DECLARE_MAC_BUF(mac3);
2165 #endif
2166
2167         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2168             ifsta->state != IEEE80211_IBSS_JOINED ||
2169             len < 24 + 2 || !ifsta->probe_resp)
2170                 return;
2171
2172         if (local->ops->tx_last_beacon)
2173                 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2174         else
2175                 tx_last_beacon = 1;
2176
2177 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2178         printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2179                "%s (tx_last_beacon=%d)\n",
2180                dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2181                print_mac(mac3, mgmt->bssid), tx_last_beacon);
2182 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2183
2184         if (!tx_last_beacon)
2185                 return;
2186
2187         if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2188             memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2189                 return;
2190
2191         end = ((u8 *) mgmt) + len;
2192         pos = mgmt->u.probe_req.variable;
2193         if (pos[0] != WLAN_EID_SSID ||
2194             pos + 2 + pos[1] > end) {
2195                 if (net_ratelimit()) {
2196                         printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2197                                "from %s\n",
2198                                dev->name, print_mac(mac, mgmt->sa));
2199                 }
2200                 return;
2201         }
2202         if (pos[1] != 0 &&
2203             (pos[1] != ifsta->ssid_len ||
2204              memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2205                 /* Ignore ProbeReq for foreign SSID */
2206                 return;
2207         }
2208
2209         /* Reply with ProbeResp */
2210         skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
2211         if (!skb)
2212                 return;
2213
2214         resp = (struct ieee80211_mgmt *) skb->data;
2215         memcpy(resp->da, mgmt->sa, ETH_ALEN);
2216 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2217         printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2218                dev->name, print_mac(mac, resp->da));
2219 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2220         ieee80211_sta_tx(dev, skb, 0);
2221 }
2222
2223 static void ieee80211_rx_mgmt_action(struct net_device *dev,
2224                                      struct ieee80211_if_sta *ifsta,
2225                                      struct ieee80211_mgmt *mgmt,
2226                                      size_t len)
2227 {
2228         if (len < IEEE80211_MIN_ACTION_SIZE)
2229                 return;
2230
2231         switch (mgmt->u.action.category) {
2232         case WLAN_CATEGORY_BACK:
2233                 switch (mgmt->u.action.u.addba_req.action_code) {
2234                 case WLAN_ACTION_ADDBA_REQ:
2235                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2236                                    sizeof(mgmt->u.action.u.addba_req)))
2237                                 break;
2238                         ieee80211_sta_process_addba_request(dev, mgmt, len);
2239                         break;
2240                 case WLAN_ACTION_DELBA:
2241                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2242                                    sizeof(mgmt->u.action.u.delba)))
2243                                 break;
2244                         ieee80211_sta_process_delba(dev, mgmt, len);
2245                         break;
2246                 default:
2247                         if (net_ratelimit())
2248                            printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
2249                                         dev->name);
2250                         break;
2251                 }
2252                 break;
2253         default:
2254                 break;
2255         }
2256 }
2257
2258 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2259                            struct ieee80211_rx_status *rx_status)
2260 {
2261         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2262         struct ieee80211_sub_if_data *sdata;
2263         struct ieee80211_if_sta *ifsta;
2264         struct ieee80211_mgmt *mgmt;
2265         u16 fc;
2266
2267         if (skb->len < 24)
2268                 goto fail;
2269
2270         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2271         ifsta = &sdata->u.sta;
2272
2273         mgmt = (struct ieee80211_mgmt *) skb->data;
2274         fc = le16_to_cpu(mgmt->frame_control);
2275
2276         switch (fc & IEEE80211_FCTL_STYPE) {
2277         case IEEE80211_STYPE_PROBE_REQ:
2278         case IEEE80211_STYPE_PROBE_RESP:
2279         case IEEE80211_STYPE_BEACON:
2280                 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2281         case IEEE80211_STYPE_AUTH:
2282         case IEEE80211_STYPE_ASSOC_RESP:
2283         case IEEE80211_STYPE_REASSOC_RESP:
2284         case IEEE80211_STYPE_DEAUTH:
2285         case IEEE80211_STYPE_DISASSOC:
2286         case IEEE80211_STYPE_ACTION:
2287                 skb_queue_tail(&ifsta->skb_queue, skb);
2288                 queue_work(local->hw.workqueue, &ifsta->work);
2289                 return;
2290         default:
2291                 printk(KERN_DEBUG "%s: received unknown management frame - "
2292                        "stype=%d\n", dev->name,
2293                        (fc & IEEE80211_FCTL_STYPE) >> 4);
2294                 break;
2295         }
2296
2297  fail:
2298         kfree_skb(skb);
2299 }
2300
2301
2302 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2303                                          struct sk_buff *skb)
2304 {
2305         struct ieee80211_rx_status *rx_status;
2306         struct ieee80211_sub_if_data *sdata;
2307         struct ieee80211_if_sta *ifsta;
2308         struct ieee80211_mgmt *mgmt;
2309         u16 fc;
2310
2311         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2312         ifsta = &sdata->u.sta;
2313
2314         rx_status = (struct ieee80211_rx_status *) skb->cb;
2315         mgmt = (struct ieee80211_mgmt *) skb->data;
2316         fc = le16_to_cpu(mgmt->frame_control);
2317
2318         switch (fc & IEEE80211_FCTL_STYPE) {
2319         case IEEE80211_STYPE_PROBE_REQ:
2320                 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2321                                             rx_status);
2322                 break;
2323         case IEEE80211_STYPE_PROBE_RESP:
2324                 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
2325                 break;
2326         case IEEE80211_STYPE_BEACON:
2327                 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
2328                 break;
2329         case IEEE80211_STYPE_AUTH:
2330                 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
2331                 break;
2332         case IEEE80211_STYPE_ASSOC_RESP:
2333                 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
2334                 break;
2335         case IEEE80211_STYPE_REASSOC_RESP:
2336                 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
2337                 break;
2338         case IEEE80211_STYPE_DEAUTH:
2339                 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
2340                 break;
2341         case IEEE80211_STYPE_DISASSOC:
2342                 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
2343                 break;
2344         case IEEE80211_STYPE_ACTION:
2345                 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len);
2346                 break;
2347         }
2348
2349         kfree_skb(skb);
2350 }
2351
2352
2353 ieee80211_txrx_result
2354 ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
2355                       struct ieee80211_rx_status *rx_status)
2356 {
2357         struct ieee80211_mgmt *mgmt;
2358         u16 fc;
2359
2360         if (skb->len < 2)
2361                 return TXRX_DROP;
2362
2363         mgmt = (struct ieee80211_mgmt *) skb->data;
2364         fc = le16_to_cpu(mgmt->frame_control);
2365
2366         if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
2367                 return TXRX_CONTINUE;
2368
2369         if (skb->len < 24)
2370                 return TXRX_DROP;
2371
2372         if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2373                 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
2374                         ieee80211_rx_mgmt_probe_resp(dev, mgmt,
2375                                                      skb->len, rx_status);
2376                         dev_kfree_skb(skb);
2377                         return TXRX_QUEUED;
2378                 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
2379                         ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
2380                                                  rx_status);
2381                         dev_kfree_skb(skb);
2382                         return TXRX_QUEUED;
2383                 }
2384         }
2385         return TXRX_CONTINUE;
2386 }
2387
2388
2389 static int ieee80211_sta_active_ibss(struct net_device *dev)
2390 {
2391         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2392         int active = 0;
2393         struct sta_info *sta;
2394
2395         read_lock_bh(&local->sta_lock);
2396         list_for_each_entry(sta, &local->sta_list, list) {
2397                 if (sta->dev == dev &&
2398                     time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
2399                                jiffies)) {
2400                         active++;
2401                         break;
2402                 }
2403         }
2404         read_unlock_bh(&local->sta_lock);
2405
2406         return active;
2407 }
2408
2409
2410 static void ieee80211_sta_expire(struct net_device *dev)
2411 {
2412         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2413         struct sta_info *sta, *tmp;
2414         LIST_HEAD(tmp_list);
2415         DECLARE_MAC_BUF(mac);
2416
2417         write_lock_bh(&local->sta_lock);
2418         list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
2419                 if (time_after(jiffies, sta->last_rx +
2420                                IEEE80211_IBSS_INACTIVITY_LIMIT)) {
2421                         printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
2422                                dev->name, print_mac(mac, sta->addr));
2423                         __sta_info_get(sta);
2424                         sta_info_remove(sta);
2425                         list_add(&sta->list, &tmp_list);
2426                 }
2427         write_unlock_bh(&local->sta_lock);
2428
2429         list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
2430                 sta_info_free(sta);
2431                 sta_info_put(sta);
2432         }
2433 }
2434
2435
2436 static void ieee80211_sta_merge_ibss(struct net_device *dev,
2437                                      struct ieee80211_if_sta *ifsta)
2438 {
2439         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2440
2441         ieee80211_sta_expire(dev);
2442         if (ieee80211_sta_active_ibss(dev))
2443                 return;
2444
2445         printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
2446                "IBSS networks with same SSID (merge)\n", dev->name);
2447         ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
2448 }
2449
2450
2451 void ieee80211_sta_timer(unsigned long data)
2452 {
2453         struct ieee80211_sub_if_data *sdata =
2454                 (struct ieee80211_sub_if_data *) data;
2455         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2456         struct ieee80211_local *local = wdev_priv(&sdata->wdev);
2457
2458         set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2459         queue_work(local->hw.workqueue, &ifsta->work);
2460 }
2461
2462
2463 void ieee80211_sta_work(struct work_struct *work)
2464 {
2465         struct ieee80211_sub_if_data *sdata =
2466                 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2467         struct net_device *dev = sdata->dev;
2468         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2469         struct ieee80211_if_sta *ifsta;
2470         struct sk_buff *skb;
2471
2472         if (!netif_running(dev))
2473                 return;
2474
2475         if (local->sta_sw_scanning || local->sta_hw_scanning)
2476                 return;
2477
2478         if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2479             sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
2480                 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
2481                        "(type=%d)\n", dev->name, sdata->vif.type);
2482                 return;
2483         }
2484         ifsta = &sdata->u.sta;
2485
2486         while ((skb = skb_dequeue(&ifsta->skb_queue)))
2487                 ieee80211_sta_rx_queued_mgmt(dev, skb);
2488
2489         if (ifsta->state != IEEE80211_AUTHENTICATE &&
2490             ifsta->state != IEEE80211_ASSOCIATE &&
2491             test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
2492                 if (ifsta->scan_ssid_len)
2493                         ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
2494                 else
2495                         ieee80211_sta_start_scan(dev, NULL, 0);
2496                 return;
2497         }
2498
2499         if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2500                 if (ieee80211_sta_config_auth(dev, ifsta))
2501                         return;
2502                 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2503         } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2504                 return;
2505
2506         switch (ifsta->state) {
2507         case IEEE80211_DISABLED:
2508                 break;
2509         case IEEE80211_AUTHENTICATE:
2510                 ieee80211_authenticate(dev, ifsta);
2511                 break;
2512         case IEEE80211_ASSOCIATE:
2513                 ieee80211_associate(dev, ifsta);
2514                 break;
2515         case IEEE80211_ASSOCIATED:
2516                 ieee80211_associated(dev, ifsta);
2517                 break;
2518         case IEEE80211_IBSS_SEARCH:
2519                 ieee80211_sta_find_ibss(dev, ifsta);
2520                 break;
2521         case IEEE80211_IBSS_JOINED:
2522                 ieee80211_sta_merge_ibss(dev, ifsta);
2523                 break;
2524         default:
2525                 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2526                        ifsta->state);
2527                 break;
2528         }
2529
2530         if (ieee80211_privacy_mismatch(dev, ifsta)) {
2531                 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2532                        "mixed-cell disabled - disassociate\n", dev->name);
2533
2534                 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2535                 ieee80211_set_disassoc(dev, ifsta, 0);
2536         }
2537 }
2538
2539
2540 static void ieee80211_sta_reset_auth(struct net_device *dev,
2541                                      struct ieee80211_if_sta *ifsta)
2542 {
2543         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2544
2545         if (local->ops->reset_tsf) {
2546                 /* Reset own TSF to allow time synchronization work. */
2547                 local->ops->reset_tsf(local_to_hw(local));
2548         }
2549
2550         ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2551
2552
2553         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2554                 ifsta->auth_alg = WLAN_AUTH_OPEN;
2555         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2556                 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2557         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2558                 ifsta->auth_alg = WLAN_AUTH_LEAP;
2559         else
2560                 ifsta->auth_alg = WLAN_AUTH_OPEN;
2561         printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2562                ifsta->auth_alg);
2563         ifsta->auth_transaction = -1;
2564         ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2565         ifsta->auth_tries = ifsta->assoc_tries = 0;
2566         netif_carrier_off(dev);
2567 }
2568
2569
2570 void ieee80211_sta_req_auth(struct net_device *dev,
2571                             struct ieee80211_if_sta *ifsta)
2572 {
2573         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2574         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2575
2576         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2577                 return;
2578
2579         if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2580                                 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2581             (ifsta->flags & (IEEE80211_STA_SSID_SET |
2582                                 IEEE80211_STA_AUTO_SSID_SEL))) {
2583                 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2584                 queue_work(local->hw.workqueue, &ifsta->work);
2585         }
2586 }
2587
2588 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2589                                     const char *ssid, int ssid_len)
2590 {
2591         int tmp, hidden_ssid;
2592
2593         if (ssid_len == ifsta->ssid_len &&
2594             !memcmp(ifsta->ssid, ssid, ssid_len))
2595                 return 1;
2596
2597         if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
2598                 return 0;
2599
2600         hidden_ssid = 1;
2601         tmp = ssid_len;
2602         while (tmp--) {
2603                 if (ssid[tmp] != '\0') {
2604                         hidden_ssid = 0;
2605                         break;
2606                 }
2607         }
2608
2609         if (hidden_ssid && ifsta->ssid_len == ssid_len)
2610                 return 1;
2611
2612         if (ssid_len == 1 && ssid[0] == ' ')
2613                 return 1;
2614
2615         return 0;
2616 }
2617
2618 static int ieee80211_sta_config_auth(struct net_device *dev,
2619                                      struct ieee80211_if_sta *ifsta)
2620 {
2621         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2622         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2623         struct ieee80211_sta_bss *bss, *selected = NULL;
2624         int top_rssi = 0, freq;
2625
2626         if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2627             IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
2628                 ifsta->state = IEEE80211_AUTHENTICATE;
2629                 ieee80211_sta_reset_auth(dev, ifsta);
2630                 return 0;
2631         }
2632
2633         spin_lock_bh(&local->sta_bss_lock);
2634         freq = local->oper_channel->freq;
2635         list_for_each_entry(bss, &local->sta_bss_list, list) {
2636                 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2637                         continue;
2638
2639                 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2640                     !!sdata->default_key)
2641                         continue;
2642
2643                 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2644                     bss->freq != freq)
2645                         continue;
2646
2647                 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2648                     memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2649                         continue;
2650
2651                 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2652                     !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2653                         continue;
2654
2655                 if (!selected || top_rssi < bss->rssi) {
2656                         selected = bss;
2657                         top_rssi = bss->rssi;
2658                 }
2659         }
2660         if (selected)
2661                 atomic_inc(&selected->users);
2662         spin_unlock_bh(&local->sta_bss_lock);
2663
2664         if (selected) {
2665                 ieee80211_set_channel(local, -1, selected->freq);
2666                 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2667                         ieee80211_sta_set_ssid(dev, selected->ssid,
2668                                                selected->ssid_len);
2669                 ieee80211_sta_set_bssid(dev, selected->bssid);
2670                 ieee80211_rx_bss_put(dev, selected);
2671                 ifsta->state = IEEE80211_AUTHENTICATE;
2672                 ieee80211_sta_reset_auth(dev, ifsta);
2673                 return 0;
2674         } else {
2675                 if (ifsta->state != IEEE80211_AUTHENTICATE) {
2676                         if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
2677                                 ieee80211_sta_start_scan(dev, NULL, 0);
2678                         else
2679                                 ieee80211_sta_start_scan(dev, ifsta->ssid,
2680                                                          ifsta->ssid_len);
2681                         ifsta->state = IEEE80211_AUTHENTICATE;
2682                         set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2683                 } else
2684                         ifsta->state = IEEE80211_DISABLED;
2685         }
2686         return -1;
2687 }
2688
2689 static int ieee80211_sta_join_ibss(struct net_device *dev,
2690                                    struct ieee80211_if_sta *ifsta,
2691                                    struct ieee80211_sta_bss *bss)
2692 {
2693         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2694         int res, rates, i, j;
2695         struct sk_buff *skb;
2696         struct ieee80211_mgmt *mgmt;
2697         struct ieee80211_tx_control control;
2698         struct ieee80211_hw_mode *mode;
2699         struct rate_selection ratesel;
2700         u8 *pos;
2701         struct ieee80211_sub_if_data *sdata;
2702
2703         /* Remove possible STA entries from other IBSS networks. */
2704         sta_info_flush(local, NULL);
2705
2706         if (local->ops->reset_tsf) {
2707                 /* Reset own TSF to allow time synchronization work. */
2708                 local->ops->reset_tsf(local_to_hw(local));
2709         }
2710         memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2711         res = ieee80211_if_config(dev);
2712         if (res)
2713                 return res;
2714
2715         local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2716
2717         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2718         sdata->drop_unencrypted = bss->capability &
2719                 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2720
2721         res = ieee80211_set_channel(local, -1, bss->freq);
2722
2723         if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2724                 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2725                        "(%d MHz)\n", dev->name, local->hw.conf.channel,
2726                        local->hw.conf.freq);
2727                 return -1;
2728         }
2729
2730         /* Set beacon template based on scan results */
2731         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2732         do {
2733                 if (!skb)
2734                         break;
2735
2736                 skb_reserve(skb, local->hw.extra_tx_headroom);
2737
2738                 mgmt = (struct ieee80211_mgmt *)
2739                         skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2740                 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2741                 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2742                                                    IEEE80211_STYPE_BEACON);
2743                 memset(mgmt->da, 0xff, ETH_ALEN);
2744                 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2745                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2746                 mgmt->u.beacon.beacon_int =
2747                         cpu_to_le16(local->hw.conf.beacon_int);
2748                 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2749
2750                 pos = skb_put(skb, 2 + ifsta->ssid_len);
2751                 *pos++ = WLAN_EID_SSID;
2752                 *pos++ = ifsta->ssid_len;
2753                 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2754
2755                 rates = bss->supp_rates_len;
2756                 if (rates > 8)
2757                         rates = 8;
2758                 pos = skb_put(skb, 2 + rates);
2759                 *pos++ = WLAN_EID_SUPP_RATES;
2760                 *pos++ = rates;
2761                 memcpy(pos, bss->supp_rates, rates);
2762
2763                 pos = skb_put(skb, 2 + 1);
2764                 *pos++ = WLAN_EID_DS_PARAMS;
2765                 *pos++ = 1;
2766                 *pos++ = bss->channel;
2767
2768                 pos = skb_put(skb, 2 + 2);
2769                 *pos++ = WLAN_EID_IBSS_PARAMS;
2770                 *pos++ = 2;
2771                 /* FIX: set ATIM window based on scan results */
2772                 *pos++ = 0;
2773                 *pos++ = 0;
2774
2775                 if (bss->supp_rates_len > 8) {
2776                         rates = bss->supp_rates_len - 8;
2777                         pos = skb_put(skb, 2 + rates);
2778                         *pos++ = WLAN_EID_EXT_SUPP_RATES;
2779                         *pos++ = rates;
2780                         memcpy(pos, &bss->supp_rates[8], rates);
2781                 }
2782
2783                 memset(&control, 0, sizeof(control));
2784                 rate_control_get_rate(dev, local->oper_hw_mode, skb, &ratesel);
2785                 if (!ratesel.rate) {
2786                         printk(KERN_DEBUG "%s: Failed to determine TX rate "
2787                                "for IBSS beacon\n", dev->name);
2788                         break;
2789                 }
2790                 control.vif = &sdata->vif;
2791                 control.tx_rate =
2792                         (sdata->bss_conf.use_short_preamble &&
2793                         (ratesel.rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2794                         ratesel.rate->val2 : ratesel.rate->val;
2795                 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2796                 control.power_level = local->hw.conf.power_level;
2797                 control.flags |= IEEE80211_TXCTL_NO_ACK;
2798                 control.retry_limit = 1;
2799
2800                 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2801                 if (ifsta->probe_resp) {
2802                         mgmt = (struct ieee80211_mgmt *)
2803                                 ifsta->probe_resp->data;
2804                         mgmt->frame_control =
2805                                 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2806                                              IEEE80211_STYPE_PROBE_RESP);
2807                 } else {
2808                         printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2809                                "template for IBSS\n", dev->name);
2810                 }
2811
2812                 if (local->ops->beacon_update &&
2813                     local->ops->beacon_update(local_to_hw(local),
2814                                              skb, &control) == 0) {
2815                         printk(KERN_DEBUG "%s: Configured IBSS beacon "
2816                                "template based on scan results\n", dev->name);
2817                         skb = NULL;
2818                 }
2819
2820                 rates = 0;
2821                 mode = local->oper_hw_mode;
2822                 for (i = 0; i < bss->supp_rates_len; i++) {
2823                         int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2824                         for (j = 0; j < mode->num_rates; j++)
2825                                 if (mode->rates[j].rate == bitrate)
2826                                         rates |= BIT(j);
2827                 }
2828                 ifsta->supp_rates_bits = rates;
2829         } while (0);
2830
2831         if (skb) {
2832                 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2833                        "template\n", dev->name);
2834                 dev_kfree_skb(skb);
2835         }
2836
2837         ifsta->state = IEEE80211_IBSS_JOINED;
2838         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2839
2840         ieee80211_rx_bss_put(dev, bss);
2841
2842         return res;
2843 }
2844
2845
2846 static int ieee80211_sta_create_ibss(struct net_device *dev,
2847                                      struct ieee80211_if_sta *ifsta)
2848 {
2849         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2850         struct ieee80211_sta_bss *bss;
2851         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2852         struct ieee80211_hw_mode *mode;
2853         u8 bssid[ETH_ALEN], *pos;
2854         int i;
2855         DECLARE_MAC_BUF(mac);
2856
2857 #if 0
2858         /* Easier testing, use fixed BSSID. */
2859         memset(bssid, 0xfe, ETH_ALEN);
2860 #else
2861         /* Generate random, not broadcast, locally administered BSSID. Mix in
2862          * own MAC address to make sure that devices that do not have proper
2863          * random number generator get different BSSID. */
2864         get_random_bytes(bssid, ETH_ALEN);
2865         for (i = 0; i < ETH_ALEN; i++)
2866                 bssid[i] ^= dev->dev_addr[i];
2867         bssid[0] &= ~0x01;
2868         bssid[0] |= 0x02;
2869 #endif
2870
2871         printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
2872                dev->name, print_mac(mac, bssid));
2873
2874         bss = ieee80211_rx_bss_add(dev, bssid, local->hw.conf.channel,
2875                                    sdata->u.sta.ssid, sdata->u.sta.ssid_len);
2876         if (!bss)
2877                 return -ENOMEM;
2878
2879         mode = local->oper_hw_mode;
2880
2881         if (local->hw.conf.beacon_int == 0)
2882                 local->hw.conf.beacon_int = 100;
2883         bss->beacon_int = local->hw.conf.beacon_int;
2884         bss->hw_mode = local->hw.conf.phymode;
2885         bss->freq = local->hw.conf.freq;
2886         bss->last_update = jiffies;
2887         bss->capability = WLAN_CAPABILITY_IBSS;
2888         if (sdata->default_key) {
2889                 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2890         } else
2891                 sdata->drop_unencrypted = 0;
2892         bss->supp_rates_len = mode->num_rates;
2893         pos = bss->supp_rates;
2894         for (i = 0; i < mode->num_rates; i++) {
2895                 int rate = mode->rates[i].rate;
2896                 *pos++ = (u8) (rate / 5);
2897         }
2898
2899         return ieee80211_sta_join_ibss(dev, ifsta, bss);
2900 }
2901
2902
2903 static int ieee80211_sta_find_ibss(struct net_device *dev,
2904                                    struct ieee80211_if_sta *ifsta)
2905 {
2906         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2907         struct ieee80211_sta_bss *bss;
2908         int found = 0;
2909         u8 bssid[ETH_ALEN];
2910         int active_ibss;
2911         DECLARE_MAC_BUF(mac);
2912         DECLARE_MAC_BUF(mac2);
2913
2914         if (ifsta->ssid_len == 0)
2915                 return -EINVAL;
2916
2917         active_ibss = ieee80211_sta_active_ibss(dev);
2918 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2919         printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2920                dev->name, active_ibss);
2921 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2922         spin_lock_bh(&local->sta_bss_lock);
2923         list_for_each_entry(bss, &local->sta_bss_list, list) {
2924                 if (ifsta->ssid_len != bss->ssid_len ||
2925                     memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2926                     || !(bss->capability & WLAN_CAPABILITY_IBSS))
2927                         continue;
2928 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2929                 printk(KERN_DEBUG "   bssid=%s found\n",
2930                        print_mac(mac, bss->bssid));
2931 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2932                 memcpy(bssid, bss->bssid, ETH_ALEN);
2933                 found = 1;
2934                 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2935                         break;
2936         }
2937         spin_unlock_bh(&local->sta_bss_lock);
2938
2939 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2940         printk(KERN_DEBUG "   sta_find_ibss: selected %s current "
2941                "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
2942 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2943         if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
2944             (bss = ieee80211_rx_bss_get(dev, bssid, local->hw.conf.channel,
2945                                         ifsta->ssid, ifsta->ssid_len))) {
2946                 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
2947                        " based on configured SSID\n",
2948                        dev->name, print_mac(mac, bssid));
2949                 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2950         }
2951 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2952         printk(KERN_DEBUG "   did not try to join ibss\n");
2953 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2954
2955         /* Selected IBSS not found in current scan results - try to scan */
2956         if (ifsta->state == IEEE80211_IBSS_JOINED &&
2957             !ieee80211_sta_active_ibss(dev)) {
2958                 mod_timer(&ifsta->timer, jiffies +
2959                                       IEEE80211_IBSS_MERGE_INTERVAL);
2960         } else if (time_after(jiffies, local->last_scan_completed +
2961                               IEEE80211_SCAN_INTERVAL)) {
2962                 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2963                        "join\n", dev->name);
2964                 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2965                                               ifsta->ssid_len);
2966         } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2967                 int interval = IEEE80211_SCAN_INTERVAL;
2968
2969                 if (time_after(jiffies, ifsta->ibss_join_req +
2970                                IEEE80211_IBSS_JOIN_TIMEOUT)) {
2971                         if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
2972                             local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2973                                 return ieee80211_sta_create_ibss(dev, ifsta);
2974                         if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
2975                                 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2976                                        " configured channel %d (%d MHz)\n",
2977                                        dev->name, local->hw.conf.channel,
2978                                        local->hw.conf.freq);
2979                         }
2980
2981                         /* No IBSS found - decrease scan interval and continue
2982                          * scanning. */
2983                         interval = IEEE80211_SCAN_INTERVAL_SLOW;
2984                 }
2985
2986                 ifsta->state = IEEE80211_IBSS_SEARCH;
2987                 mod_timer(&ifsta->timer, jiffies + interval);
2988                 return 0;
2989         }
2990
2991         return 0;
2992 }
2993
2994
2995 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2996 {
2997         struct ieee80211_sub_if_data *sdata;
2998         struct ieee80211_if_sta *ifsta;
2999         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3000
3001         if (len > IEEE80211_MAX_SSID_LEN)
3002                 return -EINVAL;
3003
3004         /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
3005          * not defined. */
3006         if (local->ops->conf_tx) {
3007                 struct ieee80211_tx_queue_params qparam;
3008                 int i;
3009
3010                 memset(&qparam, 0, sizeof(qparam));
3011                 /* TODO: are these ok defaults for all hw_modes? */
3012                 qparam.aifs = 2;
3013                 qparam.cw_min =
3014                         local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
3015                 qparam.cw_max = 1023;
3016                 qparam.burst_time = 0;
3017                 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
3018                 {
3019                         local->ops->conf_tx(local_to_hw(local),
3020                                            i + IEEE80211_TX_QUEUE_DATA0,
3021                                            &qparam);
3022                 }
3023                 /* IBSS uses different parameters for Beacon sending */
3024                 qparam.cw_min++;
3025                 qparam.cw_min *= 2;
3026                 qparam.cw_min--;
3027                 local->ops->conf_tx(local_to_hw(local),
3028                                    IEEE80211_TX_QUEUE_BEACON, &qparam);
3029         }
3030
3031         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3032         ifsta = &sdata->u.sta;
3033
3034         if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
3035                 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
3036         memcpy(ifsta->ssid, ssid, len);
3037         memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3038         ifsta->ssid_len = len;
3039
3040         if (len)
3041                 ifsta->flags |= IEEE80211_STA_SSID_SET;
3042         else
3043                 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
3044         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3045             !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
3046                 ifsta->ibss_join_req = jiffies;
3047                 ifsta->state = IEEE80211_IBSS_SEARCH;
3048                 return ieee80211_sta_find_ibss(dev, ifsta);
3049         }
3050         return 0;
3051 }
3052
3053
3054 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3055 {
3056         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3057         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3058         memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3059         *len = ifsta->ssid_len;
3060         return 0;
3061 }
3062
3063
3064 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3065 {
3066         struct ieee80211_sub_if_data *sdata;
3067         struct ieee80211_if_sta *ifsta;
3068         int res;
3069
3070         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3071         ifsta = &sdata->u.sta;
3072
3073         if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3074                 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3075                 res = ieee80211_if_config(dev);
3076                 if (res) {
3077                         printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3078                                "the low-level driver\n", dev->name);
3079                         return res;
3080                 }
3081         }
3082
3083         if (is_valid_ether_addr(bssid))
3084                 ifsta->flags |= IEEE80211_STA_BSSID_SET;
3085         else
3086                 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3087
3088         return 0;
3089 }
3090
3091
3092 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3093                                     struct ieee80211_sub_if_data *sdata,
3094                                     int powersave)
3095 {
3096         struct sk_buff *skb;
3097         struct ieee80211_hdr *nullfunc;
3098         u16 fc;
3099
3100         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3101         if (!skb) {
3102                 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3103                        "frame\n", sdata->dev->name);
3104                 return;
3105         }
3106         skb_reserve(skb, local->hw.extra_tx_headroom);
3107
3108         nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3109         memset(nullfunc, 0, 24);
3110         fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3111              IEEE80211_FCTL_TODS;
3112         if (powersave)
3113                 fc |= IEEE80211_FCTL_PM;
3114         nullfunc->frame_control = cpu_to_le16(fc);
3115         memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3116         memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3117         memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3118
3119         ieee80211_sta_tx(sdata->dev, skb, 0);
3120 }
3121
3122
3123 void ieee80211_scan_completed(struct ieee80211_hw *hw)
3124 {
3125         struct ieee80211_local *local = hw_to_local(hw);
3126         struct net_device *dev = local->scan_dev;
3127         struct ieee80211_sub_if_data *sdata;
3128         union iwreq_data wrqu;
3129
3130         local->last_scan_completed = jiffies;
3131         memset(&wrqu, 0, sizeof(wrqu));
3132         wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
3133
3134         if (local->sta_hw_scanning) {
3135                 local->sta_hw_scanning = 0;
3136                 goto done;
3137         }
3138
3139         local->sta_sw_scanning = 0;
3140         if (ieee80211_hw_config(local))
3141                 printk(KERN_DEBUG "%s: failed to restore operational "
3142                        "channel after scan\n", dev->name);
3143
3144
3145         netif_tx_lock_bh(local->mdev);
3146         local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3147         local->ops->configure_filter(local_to_hw(local),
3148                                      FIF_BCN_PRBRESP_PROMISC,
3149                                      &local->filter_flags,
3150                                      local->mdev->mc_count,
3151                                      local->mdev->mc_list);
3152
3153         netif_tx_unlock_bh(local->mdev);
3154
3155         rcu_read_lock();
3156         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3157
3158                 /* No need to wake the master device. */
3159                 if (sdata->dev == local->mdev)
3160                         continue;
3161
3162                 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
3163                         if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3164                                 ieee80211_send_nullfunc(local, sdata, 0);
3165                         ieee80211_sta_timer((unsigned long)sdata);
3166                 }
3167
3168                 netif_wake_queue(sdata->dev);
3169         }
3170         rcu_read_unlock();
3171
3172 done:
3173         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3174         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3175                 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3176                 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3177                     (!ifsta->state == IEEE80211_IBSS_JOINED &&
3178                     !ieee80211_sta_active_ibss(dev)))
3179                         ieee80211_sta_find_ibss(dev, ifsta);
3180         }
3181 }
3182 EXPORT_SYMBOL(ieee80211_scan_completed);
3183
3184 void ieee80211_sta_scan_work(struct work_struct *work)
3185 {
3186         struct ieee80211_local *local =
3187                 container_of(work, struct ieee80211_local, scan_work.work);
3188         struct net_device *dev = local->scan_dev;
3189         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3190         struct ieee80211_hw_mode *mode;
3191         struct ieee80211_channel *chan;
3192         int skip;
3193         unsigned long next_delay = 0;
3194
3195         if (!local->sta_sw_scanning)
3196                 return;
3197
3198         switch (local->scan_state) {
3199         case SCAN_SET_CHANNEL:
3200                 mode = local->scan_hw_mode;
3201                 if (local->scan_hw_mode->list.next == &local->modes_list &&
3202                     local->scan_channel_idx >= mode->num_channels) {
3203                         ieee80211_scan_completed(local_to_hw(local));
3204                         return;
3205                 }
3206                 skip = !(local->enabled_modes & (1 << mode->mode));
3207                 chan = &mode->channels[local->scan_channel_idx];
3208                 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
3209                     (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
3210                      !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
3211                     (local->hw_modes & local->enabled_modes &
3212                      (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
3213                         skip = 1;
3214
3215                 if (!skip) {
3216 #if 0
3217                         printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
3218                                dev->name, chan->chan, chan->freq);
3219 #endif
3220
3221                         local->scan_channel = chan;
3222                         if (ieee80211_hw_config(local)) {
3223                                 printk(KERN_DEBUG "%s: failed to set channel "
3224                                        "%d (%d MHz) for scan\n", dev->name,
3225                                        chan->chan, chan->freq);
3226                                 skip = 1;
3227                         }
3228                 }
3229
3230                 local->scan_channel_idx++;
3231                 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
3232                         if (local->scan_hw_mode->list.next != &local->modes_list) {
3233                                 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
3234                                                                  struct ieee80211_hw_mode,
3235                                                                  list);
3236                                 local->scan_channel_idx = 0;
3237                         }
3238                 }
3239
3240                 if (skip)
3241                         break;
3242
3243                 next_delay = IEEE80211_PROBE_DELAY +
3244                              usecs_to_jiffies(local->hw.channel_change_time);
3245                 local->scan_state = SCAN_SEND_PROBE;
3246                 break;
3247         case SCAN_SEND_PROBE:
3248                 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
3249                         ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3250                                                  local->scan_ssid_len);
3251                         next_delay = IEEE80211_CHANNEL_TIME;
3252                 } else
3253                         next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3254                 local->scan_state = SCAN_SET_CHANNEL;
3255                 break;
3256         }
3257
3258         if (local->sta_sw_scanning)
3259                 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3260                                    next_delay);
3261 }
3262
3263
3264 static int ieee80211_sta_start_scan(struct net_device *dev,
3265                                     u8 *ssid, size_t ssid_len)
3266 {
3267         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3268         struct ieee80211_sub_if_data *sdata;
3269
3270         if (ssid_len > IEEE80211_MAX_SSID_LEN)
3271                 return -EINVAL;
3272
3273         /* MLME-SCAN.request (page 118)  page 144 (11.1.3.1)
3274          * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3275          * BSSID: MACAddress
3276          * SSID
3277          * ScanType: ACTIVE, PASSIVE
3278          * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3279          *    a Probe frame during active scanning
3280          * ChannelList
3281          * MinChannelTime (>= ProbeDelay), in TU
3282          * MaxChannelTime: (>= MinChannelTime), in TU
3283          */
3284
3285          /* MLME-SCAN.confirm
3286           * BSSDescriptionSet
3287           * ResultCode: SUCCESS, INVALID_PARAMETERS
3288          */
3289
3290         if (local->sta_sw_scanning || local->sta_hw_scanning) {
3291                 if (local->scan_dev == dev)
3292                         return 0;
3293                 return -EBUSY;
3294         }
3295
3296         if (local->ops->hw_scan) {
3297                 int rc = local->ops->hw_scan(local_to_hw(local),
3298                                              ssid, ssid_len);
3299                 if (!rc) {
3300                         local->sta_hw_scanning = 1;
3301                         local->scan_dev = dev;
3302                 }
3303                 return rc;
3304         }
3305
3306         local->sta_sw_scanning = 1;
3307
3308         rcu_read_lock();
3309         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3310
3311                 /* Don't stop the master interface, otherwise we can't transmit
3312                  * probes! */
3313                 if (sdata->dev == local->mdev)
3314                         continue;
3315
3316                 netif_stop_queue(sdata->dev);
3317                 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3318                     (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
3319                         ieee80211_send_nullfunc(local, sdata, 1);
3320         }
3321         rcu_read_unlock();
3322
3323         if (ssid) {
3324                 local->scan_ssid_len = ssid_len;
3325                 memcpy(local->scan_ssid, ssid, ssid_len);
3326         } else
3327                 local->scan_ssid_len = 0;
3328         local->scan_state = SCAN_SET_CHANNEL;
3329         local->scan_hw_mode = list_entry(local->modes_list.next,
3330                                          struct ieee80211_hw_mode,
3331                                          list);
3332         local->scan_channel_idx = 0;
3333         local->scan_dev = dev;
3334
3335         netif_tx_lock_bh(local->mdev);
3336         local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3337         local->ops->configure_filter(local_to_hw(local),
3338                                      FIF_BCN_PRBRESP_PROMISC,
3339                                      &local->filter_flags,
3340                                      local->mdev->mc_count,
3341                                      local->mdev->mc_list);
3342         netif_tx_unlock_bh(local->mdev);
3343
3344         /* TODO: start scan as soon as all nullfunc frames are ACKed */
3345         queue_delayed_work(local->hw.workqueue, &local->scan_work,
3346                            IEEE80211_CHANNEL_TIME);
3347
3348         return 0;
3349 }
3350
3351
3352 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3353 {
3354         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3355         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3356         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3357
3358         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3359                 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3360
3361         if (local->sta_sw_scanning || local->sta_hw_scanning) {
3362                 if (local->scan_dev == dev)
3363                         return 0;
3364                 return -EBUSY;
3365         }
3366
3367         ifsta->scan_ssid_len = ssid_len;
3368         if (ssid_len)
3369                 memcpy(ifsta->scan_ssid, ssid, ssid_len);
3370         set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3371         queue_work(local->hw.workqueue, &ifsta->work);
3372         return 0;
3373 }
3374
3375 static char *
3376 ieee80211_sta_scan_result(struct net_device *dev,
3377                           struct ieee80211_sta_bss *bss,
3378                           char *current_ev, char *end_buf)
3379 {
3380         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3381         struct iw_event iwe;
3382
3383         if (time_after(jiffies,
3384                        bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3385                 return current_ev;
3386
3387         if (!(local->enabled_modes & (1 << bss->hw_mode)))
3388                 return current_ev;
3389
3390         memset(&iwe, 0, sizeof(iwe));
3391         iwe.cmd = SIOCGIWAP;
3392         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3393         memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3394         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3395                                           IW_EV_ADDR_LEN);
3396
3397         memset(&iwe, 0, sizeof(iwe));
3398         iwe.cmd = SIOCGIWESSID;
3399         iwe.u.data.length = bss->ssid_len;
3400         iwe.u.data.flags = 1;
3401         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3402                                           bss->ssid);
3403
3404         if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
3405                 memset(&iwe, 0, sizeof(iwe));
3406                 iwe.cmd = SIOCGIWMODE;
3407                 if (bss->capability & WLAN_CAPABILITY_ESS)
3408                         iwe.u.mode = IW_MODE_MASTER;
3409                 else
3410                         iwe.u.mode = IW_MODE_ADHOC;
3411                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3412                                                   IW_EV_UINT_LEN);
3413         }
3414
3415         memset(&iwe, 0, sizeof(iwe));
3416         iwe.cmd = SIOCGIWFREQ;
3417         iwe.u.freq.m = bss->channel;
3418         iwe.u.freq.e = 0;
3419         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3420                                           IW_EV_FREQ_LEN);
3421         iwe.u.freq.m = bss->freq * 100000;
3422         iwe.u.freq.e = 1;
3423         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3424                                           IW_EV_FREQ_LEN);
3425
3426         memset(&iwe, 0, sizeof(iwe));
3427         iwe.cmd = IWEVQUAL;
3428         iwe.u.qual.qual = bss->signal;
3429         iwe.u.qual.level = bss->rssi;
3430         iwe.u.qual.noise = bss->noise;
3431         iwe.u.qual.updated = local->wstats_flags;
3432         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3433                                           IW_EV_QUAL_LEN);
3434
3435         memset(&iwe, 0, sizeof(iwe));
3436         iwe.cmd = SIOCGIWENCODE;
3437         if (bss->capability & WLAN_CAPABILITY_PRIVACY)
3438                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3439         else
3440                 iwe.u.data.flags = IW_ENCODE_DISABLED;
3441         iwe.u.data.length = 0;
3442         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
3443
3444         if (bss && bss->wpa_ie) {
3445                 memset(&iwe, 0, sizeof(iwe));
3446                 iwe.cmd = IWEVGENIE;
3447                 iwe.u.data.length = bss->wpa_ie_len;
3448                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3449                                                   bss->wpa_ie);
3450         }
3451
3452         if (bss && bss->rsn_ie) {
3453                 memset(&iwe, 0, sizeof(iwe));
3454                 iwe.cmd = IWEVGENIE;
3455                 iwe.u.data.length = bss->rsn_ie_len;
3456                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3457                                                   bss->rsn_ie);
3458         }
3459
3460         if (bss && bss->supp_rates_len > 0) {
3461                 /* display all supported rates in readable format */
3462                 char *p = current_ev + IW_EV_LCP_LEN;
3463                 int i;
3464
3465                 memset(&iwe, 0, sizeof(iwe));
3466                 iwe.cmd = SIOCGIWRATE;
3467                 /* Those two flags are ignored... */
3468                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3469
3470                 for (i = 0; i < bss->supp_rates_len; i++) {
3471                         iwe.u.bitrate.value = ((bss->supp_rates[i] &
3472                                                         0x7f) * 500000);
3473                         p = iwe_stream_add_value(current_ev, p,
3474                                         end_buf, &iwe, IW_EV_PARAM_LEN);
3475                 }
3476                 current_ev = p;
3477         }
3478
3479         if (bss) {
3480                 char *buf;
3481                 buf = kmalloc(30, GFP_ATOMIC);
3482                 if (buf) {
3483                         memset(&iwe, 0, sizeof(iwe));
3484                         iwe.cmd = IWEVCUSTOM;
3485                         sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3486                         iwe.u.data.length = strlen(buf);
3487                         current_ev = iwe_stream_add_point(current_ev, end_buf,
3488                                                           &iwe, buf);
3489                         kfree(buf);
3490                 }
3491         }
3492
3493         return current_ev;
3494 }
3495
3496
3497 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3498 {
3499         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3500         char *current_ev = buf;
3501         char *end_buf = buf + len;
3502         struct ieee80211_sta_bss *bss;
3503
3504         spin_lock_bh(&local->sta_bss_lock);
3505         list_for_each_entry(bss, &local->sta_bss_list, list) {
3506                 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3507                         spin_unlock_bh(&local->sta_bss_lock);
3508                         return -E2BIG;
3509                 }
3510                 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3511                                                        end_buf);
3512         }
3513         spin_unlock_bh(&local->sta_bss_lock);
3514         return current_ev - buf;
3515 }
3516
3517
3518 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3519 {
3520         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3521         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3522         kfree(ifsta->extra_ie);
3523         if (len == 0) {
3524                 ifsta->extra_ie = NULL;
3525                 ifsta->extra_ie_len = 0;
3526                 return 0;
3527         }
3528         ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3529         if (!ifsta->extra_ie) {
3530                 ifsta->extra_ie_len = 0;
3531                 return -ENOMEM;
3532         }
3533         memcpy(ifsta->extra_ie, ie, len);
3534         ifsta->extra_ie_len = len;
3535         return 0;
3536 }
3537
3538
3539 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3540                                          struct sk_buff *skb, u8 *bssid,
3541                                          u8 *addr)
3542 {
3543         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3544         struct sta_info *sta;
3545         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3546         DECLARE_MAC_BUF(mac);
3547
3548         /* TODO: Could consider removing the least recently used entry and
3549          * allow new one to be added. */
3550         if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3551                 if (net_ratelimit()) {
3552                         printk(KERN_DEBUG "%s: No room for a new IBSS STA "
3553                                "entry %s\n", dev->name, print_mac(mac, addr));
3554                 }
3555                 return NULL;
3556         }
3557
3558         printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
3559                wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
3560
3561         sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3562         if (!sta)
3563                 return NULL;
3564
3565         sta->supp_rates = sdata->u.sta.supp_rates_bits;
3566
3567         rate_control_rate_init(sta, local);
3568
3569         return sta; /* caller will call sta_info_put() */
3570 }
3571
3572
3573 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3574 {
3575         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3576         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3577
3578         printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3579                dev->name, reason);
3580
3581         if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3582             sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
3583                 return -EINVAL;
3584
3585         ieee80211_send_deauth(dev, ifsta, reason);
3586         ieee80211_set_disassoc(dev, ifsta, 1);
3587         return 0;
3588 }
3589
3590
3591 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3592 {
3593         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3594         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3595
3596         printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3597                dev->name, reason);
3598
3599         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
3600                 return -EINVAL;
3601
3602         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
3603                 return -1;
3604
3605         ieee80211_send_disassoc(dev, ifsta, reason);
3606         ieee80211_set_disassoc(dev, ifsta, 0);
3607         return 0;
3608 }