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