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