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