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