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