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