2e225aecb8738fe0e8138181c4979049937f396e
[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         /* it probably doesn't, but if the frame includes an ERP value then
1375          * update our stored copy */
1376         if (elems.erp_info && elems.erp_info_len >= 1) {
1377                 struct ieee80211_sta_bss *bss
1378                         = ieee80211_rx_bss_get(dev, ifsta->bssid,
1379                                                local->hw.conf.channel,
1380                                                ifsta->ssid, ifsta->ssid_len);
1381                 if (bss) {
1382                         bss->erp_value = elems.erp_info[0];
1383                         bss->has_erp_value = 1;
1384                         ieee80211_rx_bss_put(dev, bss);
1385                 }
1386         }
1387
1388         printk(KERN_DEBUG "%s: associated\n", dev->name);
1389         ifsta->aid = aid;
1390         ifsta->ap_capab = capab_info;
1391
1392         kfree(ifsta->assocresp_ies);
1393         ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1394         ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1395         if (ifsta->assocresp_ies)
1396                 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1397
1398         ieee80211_set_associated(dev, ifsta, 1);
1399
1400         /* Add STA entry for the AP */
1401         sta = sta_info_get(local, ifsta->bssid);
1402         if (!sta) {
1403                 struct ieee80211_sta_bss *bss;
1404                 sta = sta_info_add(local, dev, ifsta->bssid, GFP_KERNEL);
1405                 if (!sta) {
1406                         printk(KERN_DEBUG "%s: failed to add STA entry for the"
1407                                " AP\n", dev->name);
1408                         return;
1409                 }
1410                 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
1411                                            local->hw.conf.channel,
1412                                            ifsta->ssid, ifsta->ssid_len);
1413                 if (bss) {
1414                         sta->last_rssi = bss->rssi;
1415                         sta->last_signal = bss->signal;
1416                         sta->last_noise = bss->noise;
1417                         ieee80211_rx_bss_put(dev, bss);
1418                 }
1419         }
1420
1421         sta->dev = dev;
1422         sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP;
1423
1424         rates = 0;
1425         mode = local->oper_hw_mode;
1426         for (i = 0; i < elems.supp_rates_len; i++) {
1427                 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1428                 for (j = 0; j < mode->num_rates; j++)
1429                         if (mode->rates[j].rate == rate)
1430                                 rates |= BIT(j);
1431         }
1432         for (i = 0; i < elems.ext_supp_rates_len; i++) {
1433                 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1434                 for (j = 0; j < mode->num_rates; j++)
1435                         if (mode->rates[j].rate == rate)
1436                                 rates |= BIT(j);
1437         }
1438         sta->supp_rates = rates;
1439
1440         if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1441             local->ops->conf_ht) {
1442                 struct ieee80211_ht_bss_info bss_info;
1443
1444                 ieee80211_ht_cap_ie_to_ht_info(
1445                                 (struct ieee80211_ht_cap *)
1446                                 elems.ht_cap_elem, &sta->ht_info);
1447                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1448                                 (struct ieee80211_ht_addt_info *)
1449                                 elems.ht_info_elem, &bss_info);
1450                 ieee80211_hw_config_ht(local, 1, &sta->ht_info, &bss_info);
1451         }
1452
1453         rate_control_rate_init(sta, local);
1454
1455         if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1456                 sta->flags |= WLAN_STA_WME;
1457                 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1458                                          elems.wmm_param_len);
1459         }
1460
1461
1462         sta_info_put(sta);
1463
1464         ieee80211_associated(dev, ifsta);
1465 }
1466
1467
1468 /* Caller must hold local->sta_bss_lock */
1469 static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1470                                         struct ieee80211_sta_bss *bss)
1471 {
1472         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1473         bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1474         local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1475 }
1476
1477
1478 /* Caller must hold local->sta_bss_lock */
1479 static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1480                                         struct ieee80211_sta_bss *bss)
1481 {
1482         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1483         struct ieee80211_sta_bss *b, *prev = NULL;
1484         b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1485         while (b) {
1486                 if (b == bss) {
1487                         if (!prev)
1488                                 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1489                                         bss->hnext;
1490                         else
1491                                 prev->hnext = bss->hnext;
1492                         break;
1493                 }
1494                 prev = b;
1495                 b = b->hnext;
1496         }
1497 }
1498
1499
1500 static struct ieee80211_sta_bss *
1501 ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int channel,
1502                      u8 *ssid, u8 ssid_len)
1503 {
1504         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1505         struct ieee80211_sta_bss *bss;
1506
1507         bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
1508         if (!bss)
1509                 return NULL;
1510         atomic_inc(&bss->users);
1511         atomic_inc(&bss->users);
1512         memcpy(bss->bssid, bssid, ETH_ALEN);
1513         bss->channel = channel;
1514         if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
1515                 memcpy(bss->ssid, ssid, ssid_len);
1516                 bss->ssid_len = ssid_len;
1517         }
1518
1519         spin_lock_bh(&local->sta_bss_lock);
1520         /* TODO: order by RSSI? */
1521         list_add_tail(&bss->list, &local->sta_bss_list);
1522         __ieee80211_rx_bss_hash_add(dev, bss);
1523         spin_unlock_bh(&local->sta_bss_lock);
1524         return bss;
1525 }
1526
1527
1528 static struct ieee80211_sta_bss *
1529 ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int channel,
1530                      u8 *ssid, u8 ssid_len)
1531 {
1532         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1533         struct ieee80211_sta_bss *bss;
1534
1535         spin_lock_bh(&local->sta_bss_lock);
1536         bss = local->sta_bss_hash[STA_HASH(bssid)];
1537         while (bss) {
1538                 if (!memcmp(bss->bssid, bssid, ETH_ALEN) &&
1539                     bss->channel == channel &&
1540                     bss->ssid_len == ssid_len &&
1541                     (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
1542                         atomic_inc(&bss->users);
1543                         break;
1544                 }
1545                 bss = bss->hnext;
1546         }
1547         spin_unlock_bh(&local->sta_bss_lock);
1548         return bss;
1549 }
1550
1551
1552 static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1553 {
1554         kfree(bss->wpa_ie);
1555         kfree(bss->rsn_ie);
1556         kfree(bss->wmm_ie);
1557         kfree(bss->ht_ie);
1558         kfree(bss);
1559 }
1560
1561
1562 static void ieee80211_rx_bss_put(struct net_device *dev,
1563                                  struct ieee80211_sta_bss *bss)
1564 {
1565         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1566         if (!atomic_dec_and_test(&bss->users))
1567                 return;
1568
1569         spin_lock_bh(&local->sta_bss_lock);
1570         __ieee80211_rx_bss_hash_del(dev, bss);
1571         list_del(&bss->list);
1572         spin_unlock_bh(&local->sta_bss_lock);
1573         ieee80211_rx_bss_free(bss);
1574 }
1575
1576
1577 void ieee80211_rx_bss_list_init(struct net_device *dev)
1578 {
1579         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1580         spin_lock_init(&local->sta_bss_lock);
1581         INIT_LIST_HEAD(&local->sta_bss_list);
1582 }
1583
1584
1585 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1586 {
1587         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1588         struct ieee80211_sta_bss *bss, *tmp;
1589
1590         list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1591                 ieee80211_rx_bss_put(dev, bss);
1592 }
1593
1594
1595 static void ieee80211_rx_bss_info(struct net_device *dev,
1596                                   struct ieee80211_mgmt *mgmt,
1597                                   size_t len,
1598                                   struct ieee80211_rx_status *rx_status,
1599                                   int beacon)
1600 {
1601         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1602         struct ieee802_11_elems elems;
1603         size_t baselen;
1604         int channel, clen;
1605         struct ieee80211_sta_bss *bss;
1606         struct sta_info *sta;
1607         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1608         u64 timestamp;
1609         DECLARE_MAC_BUF(mac);
1610         DECLARE_MAC_BUF(mac2);
1611
1612         if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1613                 return; /* ignore ProbeResp to foreign address */
1614
1615 #if 0
1616         printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
1617                dev->name, beacon ? "Beacon" : "Probe Response",
1618                print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
1619 #endif
1620
1621         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1622         if (baselen > len)
1623                 return;
1624
1625         timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1626
1627         if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon &&
1628             memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1629 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1630                 static unsigned long last_tsf_debug = 0;
1631                 u64 tsf;
1632                 if (local->ops->get_tsf)
1633                         tsf = local->ops->get_tsf(local_to_hw(local));
1634                 else
1635                         tsf = -1LLU;
1636                 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1637                         printk(KERN_DEBUG "RX beacon SA=%s BSSID="
1638                                "%s TSF=0x%llx BCN=0x%llx diff=%lld "
1639                                "@%lu\n",
1640                                print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->bssid),
1641                                (unsigned long long)tsf,
1642                                (unsigned long long)timestamp,
1643                                (unsigned long long)(tsf - timestamp),
1644                                jiffies);
1645                         last_tsf_debug = jiffies;
1646                 }
1647 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1648         }
1649
1650         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
1651
1652         if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1653             memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1654             (sta = sta_info_get(local, mgmt->sa))) {
1655                 struct ieee80211_hw_mode *mode;
1656                 struct ieee80211_rate *rates;
1657                 size_t num_rates;
1658                 u32 supp_rates, prev_rates;
1659                 int i, j;
1660
1661                 mode = local->sta_sw_scanning ?
1662                        local->scan_hw_mode : local->oper_hw_mode;
1663
1664                 if (local->sta_hw_scanning) {
1665                         /* search for the correct mode matches the beacon */
1666                         list_for_each_entry(mode, &local->modes_list, list)
1667                                 if (mode->mode == rx_status->phymode)
1668                                         break;
1669
1670                         if (mode == NULL)
1671                                 mode = local->oper_hw_mode;
1672                 }
1673                 rates = mode->rates;
1674                 num_rates = mode->num_rates;
1675
1676                 supp_rates = 0;
1677                 for (i = 0; i < elems.supp_rates_len +
1678                              elems.ext_supp_rates_len; i++) {
1679                         u8 rate = 0;
1680                         int own_rate;
1681                         if (i < elems.supp_rates_len)
1682                                 rate = elems.supp_rates[i];
1683                         else if (elems.ext_supp_rates)
1684                                 rate = elems.ext_supp_rates
1685                                         [i - elems.supp_rates_len];
1686                         own_rate = 5 * (rate & 0x7f);
1687                         for (j = 0; j < num_rates; j++)
1688                                 if (rates[j].rate == own_rate)
1689                                         supp_rates |= BIT(j);
1690                 }
1691
1692                 prev_rates = sta->supp_rates;
1693                 sta->supp_rates &= supp_rates;
1694                 if (sta->supp_rates == 0) {
1695                         /* No matching rates - this should not really happen.
1696                          * Make sure that at least one rate is marked
1697                          * supported to avoid issues with TX rate ctrl. */
1698                         sta->supp_rates = sdata->u.sta.supp_rates_bits;
1699                 }
1700                 if (sta->supp_rates != prev_rates) {
1701                         printk(KERN_DEBUG "%s: updated supp_rates set for "
1702                                "%s based on beacon info (0x%x & 0x%x -> "
1703                                "0x%x)\n",
1704                                dev->name, print_mac(mac, sta->addr), prev_rates,
1705                                supp_rates, sta->supp_rates);
1706                 }
1707                 sta_info_put(sta);
1708         }
1709
1710         if (!elems.ssid)
1711                 return;
1712
1713         if (elems.ds_params && elems.ds_params_len == 1)
1714                 channel = elems.ds_params[0];
1715         else
1716                 channel = rx_status->channel;
1717
1718         bss = ieee80211_rx_bss_get(dev, mgmt->bssid, channel,
1719                                    elems.ssid, elems.ssid_len);
1720         if (!bss) {
1721                 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, channel,
1722                                            elems.ssid, elems.ssid_len);
1723                 if (!bss)
1724                         return;
1725         } else {
1726 #if 0
1727                 /* TODO: order by RSSI? */
1728                 spin_lock_bh(&local->sta_bss_lock);
1729                 list_move_tail(&bss->list, &local->sta_bss_list);
1730                 spin_unlock_bh(&local->sta_bss_lock);
1731 #endif
1732         }
1733
1734         if (bss->probe_resp && beacon) {
1735                 /* Do not allow beacon to override data from Probe Response. */
1736                 ieee80211_rx_bss_put(dev, bss);
1737                 return;
1738         }
1739
1740         /* save the ERP value so that it is available at association time */
1741         if (elems.erp_info && elems.erp_info_len >= 1) {
1742                 bss->erp_value = elems.erp_info[0];
1743                 bss->has_erp_value = 1;
1744         }
1745
1746         bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1747         bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
1748
1749         bss->supp_rates_len = 0;
1750         if (elems.supp_rates) {
1751                 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1752                 if (clen > elems.supp_rates_len)
1753                         clen = elems.supp_rates_len;
1754                 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1755                        clen);
1756                 bss->supp_rates_len += clen;
1757         }
1758         if (elems.ext_supp_rates) {
1759                 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1760                 if (clen > elems.ext_supp_rates_len)
1761                         clen = elems.ext_supp_rates_len;
1762                 memcpy(&bss->supp_rates[bss->supp_rates_len],
1763                        elems.ext_supp_rates, clen);
1764                 bss->supp_rates_len += clen;
1765         }
1766
1767         if (elems.wpa &&
1768             (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1769              memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1770                 kfree(bss->wpa_ie);
1771                 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1772                 if (bss->wpa_ie) {
1773                         memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1774                         bss->wpa_ie_len = elems.wpa_len + 2;
1775                 } else
1776                         bss->wpa_ie_len = 0;
1777         } else if (!elems.wpa && bss->wpa_ie) {
1778                 kfree(bss->wpa_ie);
1779                 bss->wpa_ie = NULL;
1780                 bss->wpa_ie_len = 0;
1781         }
1782
1783         if (elems.rsn &&
1784             (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
1785              memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
1786                 kfree(bss->rsn_ie);
1787                 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
1788                 if (bss->rsn_ie) {
1789                         memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
1790                         bss->rsn_ie_len = elems.rsn_len + 2;
1791                 } else
1792                         bss->rsn_ie_len = 0;
1793         } else if (!elems.rsn && bss->rsn_ie) {
1794                 kfree(bss->rsn_ie);
1795                 bss->rsn_ie = NULL;
1796                 bss->rsn_ie_len = 0;
1797         }
1798
1799         if (elems.wmm_param &&
1800             (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
1801              memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
1802                 kfree(bss->wmm_ie);
1803                 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
1804                 if (bss->wmm_ie) {
1805                         memcpy(bss->wmm_ie, elems.wmm_param - 2,
1806                                elems.wmm_param_len + 2);
1807                         bss->wmm_ie_len = elems.wmm_param_len + 2;
1808                 } else
1809                         bss->wmm_ie_len = 0;
1810         } else if (!elems.wmm_param && bss->wmm_ie) {
1811                 kfree(bss->wmm_ie);
1812                 bss->wmm_ie = NULL;
1813                 bss->wmm_ie_len = 0;
1814         }
1815         if (elems.ht_cap_elem &&
1816             (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
1817              memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
1818                 kfree(bss->ht_ie);
1819                 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
1820                 if (bss->ht_ie) {
1821                         memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
1822                                elems.ht_cap_elem_len + 2);
1823                         bss->ht_ie_len = elems.ht_cap_elem_len + 2;
1824                 } else
1825                         bss->ht_ie_len = 0;
1826         } else if (!elems.ht_cap_elem && bss->ht_ie) {
1827                 kfree(bss->ht_ie);
1828                 bss->ht_ie = NULL;
1829                 bss->ht_ie_len = 0;
1830         }
1831
1832         bss->hw_mode = rx_status->phymode;
1833         bss->freq = rx_status->freq;
1834         if (channel != rx_status->channel &&
1835             (bss->hw_mode == MODE_IEEE80211G ||
1836              bss->hw_mode == MODE_IEEE80211B) &&
1837             channel >= 1 && channel <= 14) {
1838                 static const int freq_list[] = {
1839                         2412, 2417, 2422, 2427, 2432, 2437, 2442,
1840                         2447, 2452, 2457, 2462, 2467, 2472, 2484
1841                 };
1842                 /* IEEE 802.11g/b mode can receive packets from neighboring
1843                  * channels, so map the channel into frequency. */
1844                 bss->freq = freq_list[channel - 1];
1845         }
1846         bss->timestamp = timestamp;
1847         bss->last_update = jiffies;
1848         bss->rssi = rx_status->ssi;
1849         bss->signal = rx_status->signal;
1850         bss->noise = rx_status->noise;
1851         if (!beacon)
1852                 bss->probe_resp++;
1853         ieee80211_rx_bss_put(dev, bss);
1854 }
1855
1856
1857 static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
1858                                          struct ieee80211_mgmt *mgmt,
1859                                          size_t len,
1860                                          struct ieee80211_rx_status *rx_status)
1861 {
1862         ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
1863 }
1864
1865
1866 static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
1867                                      struct ieee80211_mgmt *mgmt,
1868                                      size_t len,
1869                                      struct ieee80211_rx_status *rx_status)
1870 {
1871         struct ieee80211_sub_if_data *sdata;
1872         struct ieee80211_if_sta *ifsta;
1873         size_t baselen;
1874         struct ieee802_11_elems elems;
1875         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1876         struct ieee80211_conf *conf = &local->hw.conf;
1877
1878         ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
1879
1880         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1881         if (sdata->type != IEEE80211_IF_TYPE_STA)
1882                 return;
1883         ifsta = &sdata->u.sta;
1884
1885         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
1886             memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1887                 return;
1888
1889         /* Process beacon from the current BSS */
1890         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1891         if (baselen > len)
1892                 return;
1893
1894         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
1895
1896         if (elems.erp_info && elems.erp_info_len >= 1)
1897                 ieee80211_handle_erp_ie(dev, elems.erp_info[0]);
1898
1899         if (elems.ht_cap_elem && elems.ht_info_elem &&
1900             elems.wmm_param && local->ops->conf_ht &&
1901             conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
1902                 struct ieee80211_ht_bss_info bss_info;
1903
1904                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1905                                 (struct ieee80211_ht_addt_info *)
1906                                 elems.ht_info_elem, &bss_info);
1907                 /* check if AP changed bss inforamation */
1908                 if ((conf->ht_bss_conf.primary_channel !=
1909                      bss_info.primary_channel) ||
1910                     (conf->ht_bss_conf.bss_cap != bss_info.bss_cap) ||
1911                     (conf->ht_bss_conf.bss_op_mode != bss_info.bss_op_mode))
1912                         ieee80211_hw_config_ht(local, 1, &conf->ht_conf,
1913                                                 &bss_info);
1914         }
1915
1916         if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1917                 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1918                                          elems.wmm_param_len);
1919         }
1920 }
1921
1922
1923 static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
1924                                         struct ieee80211_if_sta *ifsta,
1925                                         struct ieee80211_mgmt *mgmt,
1926                                         size_t len,
1927                                         struct ieee80211_rx_status *rx_status)
1928 {
1929         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1930         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1931         int tx_last_beacon;
1932         struct sk_buff *skb;
1933         struct ieee80211_mgmt *resp;
1934         u8 *pos, *end;
1935         DECLARE_MAC_BUF(mac);
1936 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1937         DECLARE_MAC_BUF(mac2);
1938         DECLARE_MAC_BUF(mac3);
1939 #endif
1940
1941         if (sdata->type != IEEE80211_IF_TYPE_IBSS ||
1942             ifsta->state != IEEE80211_IBSS_JOINED ||
1943             len < 24 + 2 || !ifsta->probe_resp)
1944                 return;
1945
1946         if (local->ops->tx_last_beacon)
1947                 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1948         else
1949                 tx_last_beacon = 1;
1950
1951 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1952         printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
1953                "%s (tx_last_beacon=%d)\n",
1954                dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
1955                print_mac(mac3, mgmt->bssid), tx_last_beacon);
1956 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1957
1958         if (!tx_last_beacon)
1959                 return;
1960
1961         if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1962             memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1963                 return;
1964
1965         end = ((u8 *) mgmt) + len;
1966         pos = mgmt->u.probe_req.variable;
1967         if (pos[0] != WLAN_EID_SSID ||
1968             pos + 2 + pos[1] > end) {
1969                 if (net_ratelimit()) {
1970                         printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1971                                "from %s\n",
1972                                dev->name, print_mac(mac, mgmt->sa));
1973                 }
1974                 return;
1975         }
1976         if (pos[1] != 0 &&
1977             (pos[1] != ifsta->ssid_len ||
1978              memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1979                 /* Ignore ProbeReq for foreign SSID */
1980                 return;
1981         }
1982
1983         /* Reply with ProbeResp */
1984         skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
1985         if (!skb)
1986                 return;
1987
1988         resp = (struct ieee80211_mgmt *) skb->data;
1989         memcpy(resp->da, mgmt->sa, ETH_ALEN);
1990 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1991         printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
1992                dev->name, print_mac(mac, resp->da));
1993 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1994         ieee80211_sta_tx(dev, skb, 0);
1995 }
1996
1997 static void ieee80211_rx_mgmt_action(struct net_device *dev,
1998                                      struct ieee80211_if_sta *ifsta,
1999                                      struct ieee80211_mgmt *mgmt,
2000                                      size_t len)
2001 {
2002         if (len < IEEE80211_MIN_ACTION_SIZE)
2003                 return;
2004
2005         switch (mgmt->u.action.category) {
2006         case WLAN_CATEGORY_BACK:
2007                 switch (mgmt->u.action.u.addba_req.action_code) {
2008                 case WLAN_ACTION_ADDBA_REQ:
2009                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2010                                    sizeof(mgmt->u.action.u.addba_req)))
2011                                 break;
2012                         ieee80211_sta_process_addba_request(dev, mgmt, len);
2013                         break;
2014                 default:
2015                         if (net_ratelimit())
2016                            printk(KERN_DEBUG "%s: received unsupported BACK\n",
2017                                         dev->name);
2018                         break;
2019                 }
2020                 break;
2021         default:
2022                 break;
2023         }
2024 }
2025
2026 void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2027                            struct ieee80211_rx_status *rx_status)
2028 {
2029         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2030         struct ieee80211_sub_if_data *sdata;
2031         struct ieee80211_if_sta *ifsta;
2032         struct ieee80211_mgmt *mgmt;
2033         u16 fc;
2034
2035         if (skb->len < 24)
2036                 goto fail;
2037
2038         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2039         ifsta = &sdata->u.sta;
2040
2041         mgmt = (struct ieee80211_mgmt *) skb->data;
2042         fc = le16_to_cpu(mgmt->frame_control);
2043
2044         switch (fc & IEEE80211_FCTL_STYPE) {
2045         case IEEE80211_STYPE_PROBE_REQ:
2046         case IEEE80211_STYPE_PROBE_RESP:
2047         case IEEE80211_STYPE_BEACON:
2048                 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2049         case IEEE80211_STYPE_AUTH:
2050         case IEEE80211_STYPE_ASSOC_RESP:
2051         case IEEE80211_STYPE_REASSOC_RESP:
2052         case IEEE80211_STYPE_DEAUTH:
2053         case IEEE80211_STYPE_DISASSOC:
2054         case IEEE80211_STYPE_ACTION:
2055                 skb_queue_tail(&ifsta->skb_queue, skb);
2056                 queue_work(local->hw.workqueue, &ifsta->work);
2057                 return;
2058         default:
2059                 printk(KERN_DEBUG "%s: received unknown management frame - "
2060                        "stype=%d\n", dev->name,
2061                        (fc & IEEE80211_FCTL_STYPE) >> 4);
2062                 break;
2063         }
2064
2065  fail:
2066         kfree_skb(skb);
2067 }
2068
2069
2070 static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2071                                          struct sk_buff *skb)
2072 {
2073         struct ieee80211_rx_status *rx_status;
2074         struct ieee80211_sub_if_data *sdata;
2075         struct ieee80211_if_sta *ifsta;
2076         struct ieee80211_mgmt *mgmt;
2077         u16 fc;
2078
2079         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2080         ifsta = &sdata->u.sta;
2081
2082         rx_status = (struct ieee80211_rx_status *) skb->cb;
2083         mgmt = (struct ieee80211_mgmt *) skb->data;
2084         fc = le16_to_cpu(mgmt->frame_control);
2085
2086         switch (fc & IEEE80211_FCTL_STYPE) {
2087         case IEEE80211_STYPE_PROBE_REQ:
2088                 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2089                                             rx_status);
2090                 break;
2091         case IEEE80211_STYPE_PROBE_RESP:
2092                 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
2093                 break;
2094         case IEEE80211_STYPE_BEACON:
2095                 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
2096                 break;
2097         case IEEE80211_STYPE_AUTH:
2098                 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
2099                 break;
2100         case IEEE80211_STYPE_ASSOC_RESP:
2101                 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0);
2102                 break;
2103         case IEEE80211_STYPE_REASSOC_RESP:
2104                 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1);
2105                 break;
2106         case IEEE80211_STYPE_DEAUTH:
2107                 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
2108                 break;
2109         case IEEE80211_STYPE_DISASSOC:
2110                 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
2111                 break;
2112         case IEEE80211_STYPE_ACTION:
2113                 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len);
2114                 break;
2115         }
2116
2117         kfree_skb(skb);
2118 }
2119
2120
2121 ieee80211_txrx_result
2122 ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
2123                       struct ieee80211_rx_status *rx_status)
2124 {
2125         struct ieee80211_mgmt *mgmt;
2126         u16 fc;
2127
2128         if (skb->len < 2)
2129                 return TXRX_DROP;
2130
2131         mgmt = (struct ieee80211_mgmt *) skb->data;
2132         fc = le16_to_cpu(mgmt->frame_control);
2133
2134         if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
2135                 return TXRX_CONTINUE;
2136
2137         if (skb->len < 24)
2138                 return TXRX_DROP;
2139
2140         if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2141                 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
2142                         ieee80211_rx_mgmt_probe_resp(dev, mgmt,
2143                                                      skb->len, rx_status);
2144                         dev_kfree_skb(skb);
2145                         return TXRX_QUEUED;
2146                 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
2147                         ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
2148                                                  rx_status);
2149                         dev_kfree_skb(skb);
2150                         return TXRX_QUEUED;
2151                 }
2152         }
2153         return TXRX_CONTINUE;
2154 }
2155
2156
2157 static int ieee80211_sta_active_ibss(struct net_device *dev)
2158 {
2159         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2160         int active = 0;
2161         struct sta_info *sta;
2162
2163         read_lock_bh(&local->sta_lock);
2164         list_for_each_entry(sta, &local->sta_list, list) {
2165                 if (sta->dev == dev &&
2166                     time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
2167                                jiffies)) {
2168                         active++;
2169                         break;
2170                 }
2171         }
2172         read_unlock_bh(&local->sta_lock);
2173
2174         return active;
2175 }
2176
2177
2178 static void ieee80211_sta_expire(struct net_device *dev)
2179 {
2180         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2181         struct sta_info *sta, *tmp;
2182         LIST_HEAD(tmp_list);
2183         DECLARE_MAC_BUF(mac);
2184
2185         write_lock_bh(&local->sta_lock);
2186         list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
2187                 if (time_after(jiffies, sta->last_rx +
2188                                IEEE80211_IBSS_INACTIVITY_LIMIT)) {
2189                         printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
2190                                dev->name, print_mac(mac, sta->addr));
2191                         __sta_info_get(sta);
2192                         sta_info_remove(sta);
2193                         list_add(&sta->list, &tmp_list);
2194                 }
2195         write_unlock_bh(&local->sta_lock);
2196
2197         list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
2198                 sta_info_free(sta);
2199                 sta_info_put(sta);
2200         }
2201 }
2202
2203
2204 static void ieee80211_sta_merge_ibss(struct net_device *dev,
2205                                      struct ieee80211_if_sta *ifsta)
2206 {
2207         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2208
2209         ieee80211_sta_expire(dev);
2210         if (ieee80211_sta_active_ibss(dev))
2211                 return;
2212
2213         printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
2214                "IBSS networks with same SSID (merge)\n", dev->name);
2215         ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
2216 }
2217
2218
2219 void ieee80211_sta_timer(unsigned long data)
2220 {
2221         struct ieee80211_sub_if_data *sdata =
2222                 (struct ieee80211_sub_if_data *) data;
2223         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2224         struct ieee80211_local *local = wdev_priv(&sdata->wdev);
2225
2226         set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2227         queue_work(local->hw.workqueue, &ifsta->work);
2228 }
2229
2230
2231 void ieee80211_sta_work(struct work_struct *work)
2232 {
2233         struct ieee80211_sub_if_data *sdata =
2234                 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2235         struct net_device *dev = sdata->dev;
2236         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2237         struct ieee80211_if_sta *ifsta;
2238         struct sk_buff *skb;
2239
2240         if (!netif_running(dev))
2241                 return;
2242
2243         if (local->sta_sw_scanning || local->sta_hw_scanning)
2244                 return;
2245
2246         if (sdata->type != IEEE80211_IF_TYPE_STA &&
2247             sdata->type != IEEE80211_IF_TYPE_IBSS) {
2248                 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
2249                        "(type=%d)\n", dev->name, sdata->type);
2250                 return;
2251         }
2252         ifsta = &sdata->u.sta;
2253
2254         while ((skb = skb_dequeue(&ifsta->skb_queue)))
2255                 ieee80211_sta_rx_queued_mgmt(dev, skb);
2256
2257         if (ifsta->state != IEEE80211_AUTHENTICATE &&
2258             ifsta->state != IEEE80211_ASSOCIATE &&
2259             test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
2260                 if (ifsta->scan_ssid_len)
2261                         ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
2262                 else
2263                         ieee80211_sta_start_scan(dev, NULL, 0);
2264                 return;
2265         }
2266
2267         if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2268                 if (ieee80211_sta_config_auth(dev, ifsta))
2269                         return;
2270                 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2271         } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2272                 return;
2273
2274         switch (ifsta->state) {
2275         case IEEE80211_DISABLED:
2276                 break;
2277         case IEEE80211_AUTHENTICATE:
2278                 ieee80211_authenticate(dev, ifsta);
2279                 break;
2280         case IEEE80211_ASSOCIATE:
2281                 ieee80211_associate(dev, ifsta);
2282                 break;
2283         case IEEE80211_ASSOCIATED:
2284                 ieee80211_associated(dev, ifsta);
2285                 break;
2286         case IEEE80211_IBSS_SEARCH:
2287                 ieee80211_sta_find_ibss(dev, ifsta);
2288                 break;
2289         case IEEE80211_IBSS_JOINED:
2290                 ieee80211_sta_merge_ibss(dev, ifsta);
2291                 break;
2292         default:
2293                 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2294                        ifsta->state);
2295                 break;
2296         }
2297
2298         if (ieee80211_privacy_mismatch(dev, ifsta)) {
2299                 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2300                        "mixed-cell disabled - disassociate\n", dev->name);
2301
2302                 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2303                 ieee80211_set_disassoc(dev, ifsta, 0);
2304         }
2305 }
2306
2307
2308 static void ieee80211_sta_reset_auth(struct net_device *dev,
2309                                      struct ieee80211_if_sta *ifsta)
2310 {
2311         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2312
2313         if (local->ops->reset_tsf) {
2314                 /* Reset own TSF to allow time synchronization work. */
2315                 local->ops->reset_tsf(local_to_hw(local));
2316         }
2317
2318         ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2319
2320
2321         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2322                 ifsta->auth_alg = WLAN_AUTH_OPEN;
2323         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2324                 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2325         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2326                 ifsta->auth_alg = WLAN_AUTH_LEAP;
2327         else
2328                 ifsta->auth_alg = WLAN_AUTH_OPEN;
2329         printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2330                ifsta->auth_alg);
2331         ifsta->auth_transaction = -1;
2332         ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2333         ifsta->auth_tries = ifsta->assoc_tries = 0;
2334         netif_carrier_off(dev);
2335 }
2336
2337
2338 void ieee80211_sta_req_auth(struct net_device *dev,
2339                             struct ieee80211_if_sta *ifsta)
2340 {
2341         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2342         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2343
2344         if (sdata->type != IEEE80211_IF_TYPE_STA)
2345                 return;
2346
2347         if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2348                                 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2349             (ifsta->flags & (IEEE80211_STA_SSID_SET |
2350                                 IEEE80211_STA_AUTO_SSID_SEL))) {
2351                 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2352                 queue_work(local->hw.workqueue, &ifsta->work);
2353         }
2354 }
2355
2356 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2357                                     const char *ssid, int ssid_len)
2358 {
2359         int tmp, hidden_ssid;
2360
2361         if (ssid_len == ifsta->ssid_len &&
2362             !memcmp(ifsta->ssid, ssid, ssid_len))
2363                 return 1;
2364
2365         if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
2366                 return 0;
2367
2368         hidden_ssid = 1;
2369         tmp = ssid_len;
2370         while (tmp--) {
2371                 if (ssid[tmp] != '\0') {
2372                         hidden_ssid = 0;
2373                         break;
2374                 }
2375         }
2376
2377         if (hidden_ssid && ifsta->ssid_len == ssid_len)
2378                 return 1;
2379
2380         if (ssid_len == 1 && ssid[0] == ' ')
2381                 return 1;
2382
2383         return 0;
2384 }
2385
2386 static int ieee80211_sta_config_auth(struct net_device *dev,
2387                                      struct ieee80211_if_sta *ifsta)
2388 {
2389         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2390         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2391         struct ieee80211_sta_bss *bss, *selected = NULL;
2392         int top_rssi = 0, freq;
2393
2394         if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2395             IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
2396                 ifsta->state = IEEE80211_AUTHENTICATE;
2397                 ieee80211_sta_reset_auth(dev, ifsta);
2398                 return 0;
2399         }
2400
2401         spin_lock_bh(&local->sta_bss_lock);
2402         freq = local->oper_channel->freq;
2403         list_for_each_entry(bss, &local->sta_bss_list, list) {
2404                 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2405                         continue;
2406
2407                 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2408                     !!sdata->default_key)
2409                         continue;
2410
2411                 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2412                     bss->freq != freq)
2413                         continue;
2414
2415                 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2416                     memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2417                         continue;
2418
2419                 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2420                     !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2421                         continue;
2422
2423                 if (!selected || top_rssi < bss->rssi) {
2424                         selected = bss;
2425                         top_rssi = bss->rssi;
2426                 }
2427         }
2428         if (selected)
2429                 atomic_inc(&selected->users);
2430         spin_unlock_bh(&local->sta_bss_lock);
2431
2432         if (selected) {
2433                 ieee80211_set_channel(local, -1, selected->freq);
2434                 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2435                         ieee80211_sta_set_ssid(dev, selected->ssid,
2436                                                selected->ssid_len);
2437                 ieee80211_sta_set_bssid(dev, selected->bssid);
2438                 ieee80211_rx_bss_put(dev, selected);
2439                 ifsta->state = IEEE80211_AUTHENTICATE;
2440                 ieee80211_sta_reset_auth(dev, ifsta);
2441                 return 0;
2442         } else {
2443                 if (ifsta->state != IEEE80211_AUTHENTICATE) {
2444                         if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
2445                                 ieee80211_sta_start_scan(dev, NULL, 0);
2446                         else
2447                                 ieee80211_sta_start_scan(dev, ifsta->ssid,
2448                                                          ifsta->ssid_len);
2449                         ifsta->state = IEEE80211_AUTHENTICATE;
2450                         set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2451                 } else
2452                         ifsta->state = IEEE80211_DISABLED;
2453         }
2454         return -1;
2455 }
2456
2457 static int ieee80211_sta_join_ibss(struct net_device *dev,
2458                                    struct ieee80211_if_sta *ifsta,
2459                                    struct ieee80211_sta_bss *bss)
2460 {
2461         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2462         int res, rates, i, j;
2463         struct sk_buff *skb;
2464         struct ieee80211_mgmt *mgmt;
2465         struct ieee80211_tx_control control;
2466         struct ieee80211_hw_mode *mode;
2467         struct rate_selection ratesel;
2468         u8 *pos;
2469         struct ieee80211_sub_if_data *sdata;
2470
2471         /* Remove possible STA entries from other IBSS networks. */
2472         sta_info_flush(local, NULL);
2473
2474         if (local->ops->reset_tsf) {
2475                 /* Reset own TSF to allow time synchronization work. */
2476                 local->ops->reset_tsf(local_to_hw(local));
2477         }
2478         memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2479         res = ieee80211_if_config(dev);
2480         if (res)
2481                 return res;
2482
2483         local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2484
2485         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2486         sdata->drop_unencrypted = bss->capability &
2487                 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2488
2489         res = ieee80211_set_channel(local, -1, bss->freq);
2490
2491         if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2492                 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2493                        "(%d MHz)\n", dev->name, local->hw.conf.channel,
2494                        local->hw.conf.freq);
2495                 return -1;
2496         }
2497
2498         /* Set beacon template based on scan results */
2499         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2500         do {
2501                 if (!skb)
2502                         break;
2503
2504                 skb_reserve(skb, local->hw.extra_tx_headroom);
2505
2506                 mgmt = (struct ieee80211_mgmt *)
2507                         skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2508                 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2509                 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2510                                                    IEEE80211_STYPE_BEACON);
2511                 memset(mgmt->da, 0xff, ETH_ALEN);
2512                 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2513                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2514                 mgmt->u.beacon.beacon_int =
2515                         cpu_to_le16(local->hw.conf.beacon_int);
2516                 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2517
2518                 pos = skb_put(skb, 2 + ifsta->ssid_len);
2519                 *pos++ = WLAN_EID_SSID;
2520                 *pos++ = ifsta->ssid_len;
2521                 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2522
2523                 rates = bss->supp_rates_len;
2524                 if (rates > 8)
2525                         rates = 8;
2526                 pos = skb_put(skb, 2 + rates);
2527                 *pos++ = WLAN_EID_SUPP_RATES;
2528                 *pos++ = rates;
2529                 memcpy(pos, bss->supp_rates, rates);
2530
2531                 pos = skb_put(skb, 2 + 1);
2532                 *pos++ = WLAN_EID_DS_PARAMS;
2533                 *pos++ = 1;
2534                 *pos++ = bss->channel;
2535
2536                 pos = skb_put(skb, 2 + 2);
2537                 *pos++ = WLAN_EID_IBSS_PARAMS;
2538                 *pos++ = 2;
2539                 /* FIX: set ATIM window based on scan results */
2540                 *pos++ = 0;
2541                 *pos++ = 0;
2542
2543                 if (bss->supp_rates_len > 8) {
2544                         rates = bss->supp_rates_len - 8;
2545                         pos = skb_put(skb, 2 + rates);
2546                         *pos++ = WLAN_EID_EXT_SUPP_RATES;
2547                         *pos++ = rates;
2548                         memcpy(pos, &bss->supp_rates[8], rates);
2549                 }
2550
2551                 memset(&control, 0, sizeof(control));
2552                 rate_control_get_rate(dev, local->oper_hw_mode, skb, &ratesel);
2553                 if (!ratesel.rate) {
2554                         printk(KERN_DEBUG "%s: Failed to determine TX rate "
2555                                "for IBSS beacon\n", dev->name);
2556                         break;
2557                 }
2558                 control.tx_rate =
2559                         ((sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE) &&
2560                         (ratesel.rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2561                         ratesel.rate->val2 : ratesel.rate->val;
2562                 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2563                 control.power_level = local->hw.conf.power_level;
2564                 control.flags |= IEEE80211_TXCTL_NO_ACK;
2565                 control.retry_limit = 1;
2566
2567                 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2568                 if (ifsta->probe_resp) {
2569                         mgmt = (struct ieee80211_mgmt *)
2570                                 ifsta->probe_resp->data;
2571                         mgmt->frame_control =
2572                                 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2573                                              IEEE80211_STYPE_PROBE_RESP);
2574                 } else {
2575                         printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2576                                "template for IBSS\n", dev->name);
2577                 }
2578
2579                 if (local->ops->beacon_update &&
2580                     local->ops->beacon_update(local_to_hw(local),
2581                                              skb, &control) == 0) {
2582                         printk(KERN_DEBUG "%s: Configured IBSS beacon "
2583                                "template based on scan results\n", dev->name);
2584                         skb = NULL;
2585                 }
2586
2587                 rates = 0;
2588                 mode = local->oper_hw_mode;
2589                 for (i = 0; i < bss->supp_rates_len; i++) {
2590                         int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2591                         for (j = 0; j < mode->num_rates; j++)
2592                                 if (mode->rates[j].rate == bitrate)
2593                                         rates |= BIT(j);
2594                 }
2595                 ifsta->supp_rates_bits = rates;
2596         } while (0);
2597
2598         if (skb) {
2599                 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2600                        "template\n", dev->name);
2601                 dev_kfree_skb(skb);
2602         }
2603
2604         ifsta->state = IEEE80211_IBSS_JOINED;
2605         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2606
2607         ieee80211_rx_bss_put(dev, bss);
2608
2609         return res;
2610 }
2611
2612
2613 static int ieee80211_sta_create_ibss(struct net_device *dev,
2614                                      struct ieee80211_if_sta *ifsta)
2615 {
2616         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2617         struct ieee80211_sta_bss *bss;
2618         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2619         struct ieee80211_hw_mode *mode;
2620         u8 bssid[ETH_ALEN], *pos;
2621         int i;
2622         DECLARE_MAC_BUF(mac);
2623
2624 #if 0
2625         /* Easier testing, use fixed BSSID. */
2626         memset(bssid, 0xfe, ETH_ALEN);
2627 #else
2628         /* Generate random, not broadcast, locally administered BSSID. Mix in
2629          * own MAC address to make sure that devices that do not have proper
2630          * random number generator get different BSSID. */
2631         get_random_bytes(bssid, ETH_ALEN);
2632         for (i = 0; i < ETH_ALEN; i++)
2633                 bssid[i] ^= dev->dev_addr[i];
2634         bssid[0] &= ~0x01;
2635         bssid[0] |= 0x02;
2636 #endif
2637
2638         printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
2639                dev->name, print_mac(mac, bssid));
2640
2641         bss = ieee80211_rx_bss_add(dev, bssid, local->hw.conf.channel,
2642                                    sdata->u.sta.ssid, sdata->u.sta.ssid_len);
2643         if (!bss)
2644                 return -ENOMEM;
2645
2646         mode = local->oper_hw_mode;
2647
2648         if (local->hw.conf.beacon_int == 0)
2649                 local->hw.conf.beacon_int = 100;
2650         bss->beacon_int = local->hw.conf.beacon_int;
2651         bss->hw_mode = local->hw.conf.phymode;
2652         bss->freq = local->hw.conf.freq;
2653         bss->last_update = jiffies;
2654         bss->capability = WLAN_CAPABILITY_IBSS;
2655         if (sdata->default_key) {
2656                 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2657         } else
2658                 sdata->drop_unencrypted = 0;
2659         bss->supp_rates_len = mode->num_rates;
2660         pos = bss->supp_rates;
2661         for (i = 0; i < mode->num_rates; i++) {
2662                 int rate = mode->rates[i].rate;
2663                 *pos++ = (u8) (rate / 5);
2664         }
2665
2666         return ieee80211_sta_join_ibss(dev, ifsta, bss);
2667 }
2668
2669
2670 static int ieee80211_sta_find_ibss(struct net_device *dev,
2671                                    struct ieee80211_if_sta *ifsta)
2672 {
2673         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2674         struct ieee80211_sta_bss *bss;
2675         int found = 0;
2676         u8 bssid[ETH_ALEN];
2677         int active_ibss;
2678         DECLARE_MAC_BUF(mac);
2679         DECLARE_MAC_BUF(mac2);
2680
2681         if (ifsta->ssid_len == 0)
2682                 return -EINVAL;
2683
2684         active_ibss = ieee80211_sta_active_ibss(dev);
2685 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2686         printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2687                dev->name, active_ibss);
2688 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2689         spin_lock_bh(&local->sta_bss_lock);
2690         list_for_each_entry(bss, &local->sta_bss_list, list) {
2691                 if (ifsta->ssid_len != bss->ssid_len ||
2692                     memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2693                     || !(bss->capability & WLAN_CAPABILITY_IBSS))
2694                         continue;
2695 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2696                 printk(KERN_DEBUG "   bssid=%s found\n",
2697                        print_mac(mac, bss->bssid));
2698 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2699                 memcpy(bssid, bss->bssid, ETH_ALEN);
2700                 found = 1;
2701                 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2702                         break;
2703         }
2704         spin_unlock_bh(&local->sta_bss_lock);
2705
2706 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2707         printk(KERN_DEBUG "   sta_find_ibss: selected %s current "
2708                "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
2709 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2710         if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
2711             (bss = ieee80211_rx_bss_get(dev, bssid, local->hw.conf.channel,
2712                                         ifsta->ssid, ifsta->ssid_len))) {
2713                 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
2714                        " based on configured SSID\n",
2715                        dev->name, print_mac(mac, bssid));
2716                 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2717         }
2718 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2719         printk(KERN_DEBUG "   did not try to join ibss\n");
2720 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2721
2722         /* Selected IBSS not found in current scan results - try to scan */
2723         if (ifsta->state == IEEE80211_IBSS_JOINED &&
2724             !ieee80211_sta_active_ibss(dev)) {
2725                 mod_timer(&ifsta->timer, jiffies +
2726                                       IEEE80211_IBSS_MERGE_INTERVAL);
2727         } else if (time_after(jiffies, local->last_scan_completed +
2728                               IEEE80211_SCAN_INTERVAL)) {
2729                 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2730                        "join\n", dev->name);
2731                 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2732                                               ifsta->ssid_len);
2733         } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2734                 int interval = IEEE80211_SCAN_INTERVAL;
2735
2736                 if (time_after(jiffies, ifsta->ibss_join_req +
2737                                IEEE80211_IBSS_JOIN_TIMEOUT)) {
2738                         if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
2739                             local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2740                                 return ieee80211_sta_create_ibss(dev, ifsta);
2741                         if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
2742                                 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2743                                        " configured channel %d (%d MHz)\n",
2744                                        dev->name, local->hw.conf.channel,
2745                                        local->hw.conf.freq);
2746                         }
2747
2748                         /* No IBSS found - decrease scan interval and continue
2749                          * scanning. */
2750                         interval = IEEE80211_SCAN_INTERVAL_SLOW;
2751                 }
2752
2753                 ifsta->state = IEEE80211_IBSS_SEARCH;
2754                 mod_timer(&ifsta->timer, jiffies + interval);
2755                 return 0;
2756         }
2757
2758         return 0;
2759 }
2760
2761
2762 int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2763 {
2764         struct ieee80211_sub_if_data *sdata;
2765         struct ieee80211_if_sta *ifsta;
2766         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2767
2768         if (len > IEEE80211_MAX_SSID_LEN)
2769                 return -EINVAL;
2770
2771         /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2772          * not defined. */
2773         if (local->ops->conf_tx) {
2774                 struct ieee80211_tx_queue_params qparam;
2775                 int i;
2776
2777                 memset(&qparam, 0, sizeof(qparam));
2778                 /* TODO: are these ok defaults for all hw_modes? */
2779                 qparam.aifs = 2;
2780                 qparam.cw_min =
2781                         local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
2782                 qparam.cw_max = 1023;
2783                 qparam.burst_time = 0;
2784                 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2785                 {
2786                         local->ops->conf_tx(local_to_hw(local),
2787                                            i + IEEE80211_TX_QUEUE_DATA0,
2788                                            &qparam);
2789                 }
2790                 /* IBSS uses different parameters for Beacon sending */
2791                 qparam.cw_min++;
2792                 qparam.cw_min *= 2;
2793                 qparam.cw_min--;
2794                 local->ops->conf_tx(local_to_hw(local),
2795                                    IEEE80211_TX_QUEUE_BEACON, &qparam);
2796         }
2797
2798         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2799         ifsta = &sdata->u.sta;
2800
2801         if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
2802                 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2803         memcpy(ifsta->ssid, ssid, len);
2804         memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
2805         ifsta->ssid_len = len;
2806
2807         if (len)
2808                 ifsta->flags |= IEEE80211_STA_SSID_SET;
2809         else
2810                 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
2811         if (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2812             !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
2813                 ifsta->ibss_join_req = jiffies;
2814                 ifsta->state = IEEE80211_IBSS_SEARCH;
2815                 return ieee80211_sta_find_ibss(dev, ifsta);
2816         }
2817         return 0;
2818 }
2819
2820
2821 int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
2822 {
2823         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2824         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2825         memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2826         *len = ifsta->ssid_len;
2827         return 0;
2828 }
2829
2830
2831 int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
2832 {
2833         struct ieee80211_sub_if_data *sdata;
2834         struct ieee80211_if_sta *ifsta;
2835         int res;
2836
2837         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2838         ifsta = &sdata->u.sta;
2839
2840         if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2841                 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2842                 res = ieee80211_if_config(dev);
2843                 if (res) {
2844                         printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2845                                "the low-level driver\n", dev->name);
2846                         return res;
2847                 }
2848         }
2849
2850         if (is_valid_ether_addr(bssid))
2851                 ifsta->flags |= IEEE80211_STA_BSSID_SET;
2852         else
2853                 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2854
2855         return 0;
2856 }
2857
2858
2859 static void ieee80211_send_nullfunc(struct ieee80211_local *local,
2860                                     struct ieee80211_sub_if_data *sdata,
2861                                     int powersave)
2862 {
2863         struct sk_buff *skb;
2864         struct ieee80211_hdr *nullfunc;
2865         u16 fc;
2866
2867         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2868         if (!skb) {
2869                 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2870                        "frame\n", sdata->dev->name);
2871                 return;
2872         }
2873         skb_reserve(skb, local->hw.extra_tx_headroom);
2874
2875         nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2876         memset(nullfunc, 0, 24);
2877         fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2878              IEEE80211_FCTL_TODS;
2879         if (powersave)
2880                 fc |= IEEE80211_FCTL_PM;
2881         nullfunc->frame_control = cpu_to_le16(fc);
2882         memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
2883         memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2884         memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
2885
2886         ieee80211_sta_tx(sdata->dev, skb, 0);
2887 }
2888
2889
2890 void ieee80211_scan_completed(struct ieee80211_hw *hw)
2891 {
2892         struct ieee80211_local *local = hw_to_local(hw);
2893         struct net_device *dev = local->scan_dev;
2894         struct ieee80211_sub_if_data *sdata;
2895         union iwreq_data wrqu;
2896
2897         local->last_scan_completed = jiffies;
2898         memset(&wrqu, 0, sizeof(wrqu));
2899         wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2900
2901         if (local->sta_hw_scanning) {
2902                 local->sta_hw_scanning = 0;
2903                 goto done;
2904         }
2905
2906         local->sta_sw_scanning = 0;
2907         if (ieee80211_hw_config(local))
2908                 printk(KERN_DEBUG "%s: failed to restore operational "
2909                        "channel after scan\n", dev->name);
2910
2911
2912         netif_tx_lock_bh(local->mdev);
2913         local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
2914         local->ops->configure_filter(local_to_hw(local),
2915                                      FIF_BCN_PRBRESP_PROMISC,
2916                                      &local->filter_flags,
2917                                      local->mdev->mc_count,
2918                                      local->mdev->mc_list);
2919
2920         netif_tx_unlock_bh(local->mdev);
2921
2922         rcu_read_lock();
2923         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2924
2925                 /* No need to wake the master device. */
2926                 if (sdata->dev == local->mdev)
2927                         continue;
2928
2929                 if (sdata->type == IEEE80211_IF_TYPE_STA) {
2930                         if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
2931                                 ieee80211_send_nullfunc(local, sdata, 0);
2932                         ieee80211_sta_timer((unsigned long)sdata);
2933                 }
2934
2935                 netif_wake_queue(sdata->dev);
2936         }
2937         rcu_read_unlock();
2938
2939 done:
2940         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2941         if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
2942                 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2943                 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
2944                     (!ifsta->state == IEEE80211_IBSS_JOINED &&
2945                     !ieee80211_sta_active_ibss(dev)))
2946                         ieee80211_sta_find_ibss(dev, ifsta);
2947         }
2948 }
2949 EXPORT_SYMBOL(ieee80211_scan_completed);
2950
2951 void ieee80211_sta_scan_work(struct work_struct *work)
2952 {
2953         struct ieee80211_local *local =
2954                 container_of(work, struct ieee80211_local, scan_work.work);
2955         struct net_device *dev = local->scan_dev;
2956         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2957         struct ieee80211_hw_mode *mode;
2958         struct ieee80211_channel *chan;
2959         int skip;
2960         unsigned long next_delay = 0;
2961
2962         if (!local->sta_sw_scanning)
2963                 return;
2964
2965         switch (local->scan_state) {
2966         case SCAN_SET_CHANNEL:
2967                 mode = local->scan_hw_mode;
2968                 if (local->scan_hw_mode->list.next == &local->modes_list &&
2969                     local->scan_channel_idx >= mode->num_channels) {
2970                         ieee80211_scan_completed(local_to_hw(local));
2971                         return;
2972                 }
2973                 skip = !(local->enabled_modes & (1 << mode->mode));
2974                 chan = &mode->channels[local->scan_channel_idx];
2975                 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
2976                     (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2977                      !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
2978                     (local->hw_modes & local->enabled_modes &
2979                      (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
2980                         skip = 1;
2981
2982                 if (!skip) {
2983 #if 0
2984                         printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
2985                                dev->name, chan->chan, chan->freq);
2986 #endif
2987
2988                         local->scan_channel = chan;
2989                         if (ieee80211_hw_config(local)) {
2990                                 printk(KERN_DEBUG "%s: failed to set channel "
2991                                        "%d (%d MHz) for scan\n", dev->name,
2992                                        chan->chan, chan->freq);
2993                                 skip = 1;
2994                         }
2995                 }
2996
2997                 local->scan_channel_idx++;
2998                 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
2999                         if (local->scan_hw_mode->list.next != &local->modes_list) {
3000                                 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
3001                                                                  struct ieee80211_hw_mode,
3002                                                                  list);
3003                                 local->scan_channel_idx = 0;
3004                         }
3005                 }
3006
3007                 if (skip)
3008                         break;
3009
3010                 next_delay = IEEE80211_PROBE_DELAY +
3011                              usecs_to_jiffies(local->hw.channel_change_time);
3012                 local->scan_state = SCAN_SEND_PROBE;
3013                 break;
3014         case SCAN_SEND_PROBE:
3015                 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
3016                         ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3017                                                  local->scan_ssid_len);
3018                         next_delay = IEEE80211_CHANNEL_TIME;
3019                 } else
3020                         next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
3021                 local->scan_state = SCAN_SET_CHANNEL;
3022                 break;
3023         }
3024
3025         if (local->sta_sw_scanning)
3026                 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3027                                    next_delay);
3028 }
3029
3030
3031 static int ieee80211_sta_start_scan(struct net_device *dev,
3032                                     u8 *ssid, size_t ssid_len)
3033 {
3034         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3035         struct ieee80211_sub_if_data *sdata;
3036
3037         if (ssid_len > IEEE80211_MAX_SSID_LEN)
3038                 return -EINVAL;
3039
3040         /* MLME-SCAN.request (page 118)  page 144 (11.1.3.1)
3041          * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3042          * BSSID: MACAddress
3043          * SSID
3044          * ScanType: ACTIVE, PASSIVE
3045          * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3046          *    a Probe frame during active scanning
3047          * ChannelList
3048          * MinChannelTime (>= ProbeDelay), in TU
3049          * MaxChannelTime: (>= MinChannelTime), in TU
3050          */
3051
3052          /* MLME-SCAN.confirm
3053           * BSSDescriptionSet
3054           * ResultCode: SUCCESS, INVALID_PARAMETERS
3055          */
3056
3057         if (local->sta_sw_scanning || local->sta_hw_scanning) {
3058                 if (local->scan_dev == dev)
3059                         return 0;
3060                 return -EBUSY;
3061         }
3062
3063         if (local->ops->hw_scan) {
3064                 int rc = local->ops->hw_scan(local_to_hw(local),
3065                                              ssid, ssid_len);
3066                 if (!rc) {
3067                         local->sta_hw_scanning = 1;
3068                         local->scan_dev = dev;
3069                 }
3070                 return rc;
3071         }
3072
3073         local->sta_sw_scanning = 1;
3074
3075         rcu_read_lock();
3076         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3077
3078                 /* Don't stop the master interface, otherwise we can't transmit
3079                  * probes! */
3080                 if (sdata->dev == local->mdev)
3081                         continue;
3082
3083                 netif_stop_queue(sdata->dev);
3084                 if (sdata->type == IEEE80211_IF_TYPE_STA &&
3085                     (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
3086                         ieee80211_send_nullfunc(local, sdata, 1);
3087         }
3088         rcu_read_unlock();
3089
3090         if (ssid) {
3091                 local->scan_ssid_len = ssid_len;
3092                 memcpy(local->scan_ssid, ssid, ssid_len);
3093         } else
3094                 local->scan_ssid_len = 0;
3095         local->scan_state = SCAN_SET_CHANNEL;
3096         local->scan_hw_mode = list_entry(local->modes_list.next,
3097                                          struct ieee80211_hw_mode,
3098                                          list);
3099         local->scan_channel_idx = 0;
3100         local->scan_dev = dev;
3101
3102         netif_tx_lock_bh(local->mdev);
3103         local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3104         local->ops->configure_filter(local_to_hw(local),
3105                                      FIF_BCN_PRBRESP_PROMISC,
3106                                      &local->filter_flags,
3107                                      local->mdev->mc_count,
3108                                      local->mdev->mc_list);
3109         netif_tx_unlock_bh(local->mdev);
3110
3111         /* TODO: start scan as soon as all nullfunc frames are ACKed */
3112         queue_delayed_work(local->hw.workqueue, &local->scan_work,
3113                            IEEE80211_CHANNEL_TIME);
3114
3115         return 0;
3116 }
3117
3118
3119 int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3120 {
3121         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3122         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3123         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3124
3125         if (sdata->type != IEEE80211_IF_TYPE_STA)
3126                 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3127
3128         if (local->sta_sw_scanning || local->sta_hw_scanning) {
3129                 if (local->scan_dev == dev)
3130                         return 0;
3131                 return -EBUSY;
3132         }
3133
3134         ifsta->scan_ssid_len = ssid_len;
3135         if (ssid_len)
3136                 memcpy(ifsta->scan_ssid, ssid, ssid_len);
3137         set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3138         queue_work(local->hw.workqueue, &ifsta->work);
3139         return 0;
3140 }
3141
3142 static char *
3143 ieee80211_sta_scan_result(struct net_device *dev,
3144                           struct ieee80211_sta_bss *bss,
3145                           char *current_ev, char *end_buf)
3146 {
3147         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3148         struct iw_event iwe;
3149
3150         if (time_after(jiffies,
3151                        bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3152                 return current_ev;
3153
3154         if (!(local->enabled_modes & (1 << bss->hw_mode)))
3155                 return current_ev;
3156
3157         memset(&iwe, 0, sizeof(iwe));
3158         iwe.cmd = SIOCGIWAP;
3159         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3160         memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3161         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3162                                           IW_EV_ADDR_LEN);
3163
3164         memset(&iwe, 0, sizeof(iwe));
3165         iwe.cmd = SIOCGIWESSID;
3166         iwe.u.data.length = bss->ssid_len;
3167         iwe.u.data.flags = 1;
3168         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3169                                           bss->ssid);
3170
3171         if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
3172                 memset(&iwe, 0, sizeof(iwe));
3173                 iwe.cmd = SIOCGIWMODE;
3174                 if (bss->capability & WLAN_CAPABILITY_ESS)
3175                         iwe.u.mode = IW_MODE_MASTER;
3176                 else
3177                         iwe.u.mode = IW_MODE_ADHOC;
3178                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3179                                                   IW_EV_UINT_LEN);
3180         }
3181
3182         memset(&iwe, 0, sizeof(iwe));
3183         iwe.cmd = SIOCGIWFREQ;
3184         iwe.u.freq.m = bss->channel;
3185         iwe.u.freq.e = 0;
3186         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3187                                           IW_EV_FREQ_LEN);
3188         iwe.u.freq.m = bss->freq * 100000;
3189         iwe.u.freq.e = 1;
3190         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3191                                           IW_EV_FREQ_LEN);
3192
3193         memset(&iwe, 0, sizeof(iwe));
3194         iwe.cmd = IWEVQUAL;
3195         iwe.u.qual.qual = bss->signal;
3196         iwe.u.qual.level = bss->rssi;
3197         iwe.u.qual.noise = bss->noise;
3198         iwe.u.qual.updated = local->wstats_flags;
3199         current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3200                                           IW_EV_QUAL_LEN);
3201
3202         memset(&iwe, 0, sizeof(iwe));
3203         iwe.cmd = SIOCGIWENCODE;
3204         if (bss->capability & WLAN_CAPABILITY_PRIVACY)
3205                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3206         else
3207                 iwe.u.data.flags = IW_ENCODE_DISABLED;
3208         iwe.u.data.length = 0;
3209         current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
3210
3211         if (bss && bss->wpa_ie) {
3212                 memset(&iwe, 0, sizeof(iwe));
3213                 iwe.cmd = IWEVGENIE;
3214                 iwe.u.data.length = bss->wpa_ie_len;
3215                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3216                                                   bss->wpa_ie);
3217         }
3218
3219         if (bss && bss->rsn_ie) {
3220                 memset(&iwe, 0, sizeof(iwe));
3221                 iwe.cmd = IWEVGENIE;
3222                 iwe.u.data.length = bss->rsn_ie_len;
3223                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3224                                                   bss->rsn_ie);
3225         }
3226
3227         if (bss && bss->supp_rates_len > 0) {
3228                 /* display all supported rates in readable format */
3229                 char *p = current_ev + IW_EV_LCP_LEN;
3230                 int i;
3231
3232                 memset(&iwe, 0, sizeof(iwe));
3233                 iwe.cmd = SIOCGIWRATE;
3234                 /* Those two flags are ignored... */
3235                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3236
3237                 for (i = 0; i < bss->supp_rates_len; i++) {
3238                         iwe.u.bitrate.value = ((bss->supp_rates[i] &
3239                                                         0x7f) * 500000);
3240                         p = iwe_stream_add_value(current_ev, p,
3241                                         end_buf, &iwe, IW_EV_PARAM_LEN);
3242                 }
3243                 current_ev = p;
3244         }
3245
3246         if (bss) {
3247                 char *buf;
3248                 buf = kmalloc(30, GFP_ATOMIC);
3249                 if (buf) {
3250                         memset(&iwe, 0, sizeof(iwe));
3251                         iwe.cmd = IWEVCUSTOM;
3252                         sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3253                         iwe.u.data.length = strlen(buf);
3254                         current_ev = iwe_stream_add_point(current_ev, end_buf,
3255                                                           &iwe, buf);
3256                         kfree(buf);
3257                 }
3258         }
3259
3260         return current_ev;
3261 }
3262
3263
3264 int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3265 {
3266         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3267         char *current_ev = buf;
3268         char *end_buf = buf + len;
3269         struct ieee80211_sta_bss *bss;
3270
3271         spin_lock_bh(&local->sta_bss_lock);
3272         list_for_each_entry(bss, &local->sta_bss_list, list) {
3273                 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3274                         spin_unlock_bh(&local->sta_bss_lock);
3275                         return -E2BIG;
3276                 }
3277                 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3278                                                        end_buf);
3279         }
3280         spin_unlock_bh(&local->sta_bss_lock);
3281         return current_ev - buf;
3282 }
3283
3284
3285 int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3286 {
3287         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3288         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3289         kfree(ifsta->extra_ie);
3290         if (len == 0) {
3291                 ifsta->extra_ie = NULL;
3292                 ifsta->extra_ie_len = 0;
3293                 return 0;
3294         }
3295         ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3296         if (!ifsta->extra_ie) {
3297                 ifsta->extra_ie_len = 0;
3298                 return -ENOMEM;
3299         }
3300         memcpy(ifsta->extra_ie, ie, len);
3301         ifsta->extra_ie_len = len;
3302         return 0;
3303 }
3304
3305
3306 struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3307                                          struct sk_buff *skb, u8 *bssid,
3308                                          u8 *addr)
3309 {
3310         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3311         struct sta_info *sta;
3312         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3313         DECLARE_MAC_BUF(mac);
3314
3315         /* TODO: Could consider removing the least recently used entry and
3316          * allow new one to be added. */
3317         if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3318                 if (net_ratelimit()) {
3319                         printk(KERN_DEBUG "%s: No room for a new IBSS STA "
3320                                "entry %s\n", dev->name, print_mac(mac, addr));
3321                 }
3322                 return NULL;
3323         }
3324
3325         printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
3326                wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
3327
3328         sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3329         if (!sta)
3330                 return NULL;
3331
3332         sta->supp_rates = sdata->u.sta.supp_rates_bits;
3333
3334         rate_control_rate_init(sta, local);
3335
3336         return sta; /* caller will call sta_info_put() */
3337 }
3338
3339
3340 int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3341 {
3342         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3343         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3344
3345         printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3346                dev->name, reason);
3347
3348         if (sdata->type != IEEE80211_IF_TYPE_STA &&
3349             sdata->type != IEEE80211_IF_TYPE_IBSS)
3350                 return -EINVAL;
3351
3352         ieee80211_send_deauth(dev, ifsta, reason);
3353         ieee80211_set_disassoc(dev, ifsta, 1);
3354         return 0;
3355 }
3356
3357
3358 int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3359 {
3360         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3361         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3362
3363         printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3364                dev->name, reason);
3365
3366         if (sdata->type != IEEE80211_IF_TYPE_STA)
3367                 return -EINVAL;
3368
3369         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
3370                 return -1;
3371
3372         ieee80211_send_disassoc(dev, ifsta, reason);
3373         ieee80211_set_disassoc(dev, ifsta, 0);
3374         return 0;
3375 }