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