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