mac80211: move spectrum management code out
[safe/jmp/linux-2.6] / net / mac80211 / mlme.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 #include <linux/delay.h>
15 #include <linux/if_ether.h>
16 #include <linux/skbuff.h>
17 #include <linux/netdevice.h>
18 #include <linux/if_arp.h>
19 #include <linux/wireless.h>
20 #include <linux/random.h>
21 #include <linux/etherdevice.h>
22 #include <linux/rtnetlink.h>
23 #include <net/iw_handler.h>
24 #include <net/mac80211.h>
25
26 #include "ieee80211_i.h"
27 #include "rate.h"
28 #include "led.h"
29 #include "mesh.h"
30
31 #define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
32 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
33 #define IEEE80211_AUTH_MAX_TRIES 3
34 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
35 #define IEEE80211_ASSOC_MAX_TRIES 3
36 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
37 #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
38 #define IEEE80211_PROBE_INTERVAL (60 * HZ)
39 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
40 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
41 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
42 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
43
44 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
45 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
46 #define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
47
48 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
49
50
51 /* utils */
52 static int ecw2cw(int ecw)
53 {
54         return (1 << ecw) - 1;
55 }
56
57 static u8 *ieee80211_bss_get_ie(struct ieee80211_sta_bss *bss, u8 ie)
58 {
59         u8 *end, *pos;
60
61         pos = bss->ies;
62         if (pos == NULL)
63                 return NULL;
64         end = pos + bss->ies_len;
65
66         while (pos + 1 < end) {
67                 if (pos + 2 + pos[1] > end)
68                         break;
69                 if (pos[0] == ie)
70                         return pos;
71                 pos += 2 + pos[1];
72         }
73
74         return NULL;
75 }
76
77 static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
78                                       struct ieee80211_supported_band *sband,
79                                       u64 *rates)
80 {
81         int i, j, count;
82         *rates = 0;
83         count = 0;
84         for (i = 0; i < bss->supp_rates_len; i++) {
85                 int rate = (bss->supp_rates[i] & 0x7F) * 5;
86
87                 for (j = 0; j < sband->n_bitrates; j++)
88                         if (sband->bitrates[j].bitrate == rate) {
89                                 *rates |= BIT(j);
90                                 count++;
91                                 break;
92                         }
93         }
94
95         return count;
96 }
97
98 /* frame sending functions */
99 void ieee80211_sta_tx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
100                       int encrypt)
101 {
102         skb->dev = sdata->local->mdev;
103         skb_set_mac_header(skb, 0);
104         skb_set_network_header(skb, 0);
105         skb_set_transport_header(skb, 0);
106
107         skb->iif = sdata->dev->ifindex;
108         skb->do_not_encrypt = !encrypt;
109
110         dev_queue_xmit(skb);
111 }
112
113 static void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
114                                 struct ieee80211_if_sta *ifsta,
115                                 int transaction, u8 *extra, size_t extra_len,
116                                 int encrypt)
117 {
118         struct ieee80211_local *local = sdata->local;
119         struct sk_buff *skb;
120         struct ieee80211_mgmt *mgmt;
121
122         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
123                             sizeof(*mgmt) + 6 + extra_len);
124         if (!skb) {
125                 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
126                        "frame\n", sdata->dev->name);
127                 return;
128         }
129         skb_reserve(skb, local->hw.extra_tx_headroom);
130
131         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
132         memset(mgmt, 0, 24 + 6);
133         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
134                                           IEEE80211_STYPE_AUTH);
135         if (encrypt)
136                 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
137         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
138         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
139         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
140         mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
141         mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
142         ifsta->auth_transaction = transaction + 1;
143         mgmt->u.auth.status_code = cpu_to_le16(0);
144         if (extra)
145                 memcpy(skb_put(skb, extra_len), extra, extra_len);
146
147         ieee80211_sta_tx(sdata, skb, encrypt);
148 }
149
150 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
151                               u8 *ssid, size_t ssid_len)
152 {
153         struct ieee80211_local *local = sdata->local;
154         struct ieee80211_supported_band *sband;
155         struct sk_buff *skb;
156         struct ieee80211_mgmt *mgmt;
157         u8 *pos, *supp_rates, *esupp_rates = NULL;
158         int i;
159
160         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
161         if (!skb) {
162                 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
163                        "request\n", sdata->dev->name);
164                 return;
165         }
166         skb_reserve(skb, local->hw.extra_tx_headroom);
167
168         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
169         memset(mgmt, 0, 24);
170         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
171                                           IEEE80211_STYPE_PROBE_REQ);
172         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
173         if (dst) {
174                 memcpy(mgmt->da, dst, ETH_ALEN);
175                 memcpy(mgmt->bssid, dst, ETH_ALEN);
176         } else {
177                 memset(mgmt->da, 0xff, ETH_ALEN);
178                 memset(mgmt->bssid, 0xff, ETH_ALEN);
179         }
180         pos = skb_put(skb, 2 + ssid_len);
181         *pos++ = WLAN_EID_SSID;
182         *pos++ = ssid_len;
183         memcpy(pos, ssid, ssid_len);
184
185         supp_rates = skb_put(skb, 2);
186         supp_rates[0] = WLAN_EID_SUPP_RATES;
187         supp_rates[1] = 0;
188         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
189
190         for (i = 0; i < sband->n_bitrates; i++) {
191                 struct ieee80211_rate *rate = &sband->bitrates[i];
192                 if (esupp_rates) {
193                         pos = skb_put(skb, 1);
194                         esupp_rates[1]++;
195                 } else if (supp_rates[1] == 8) {
196                         esupp_rates = skb_put(skb, 3);
197                         esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
198                         esupp_rates[1] = 1;
199                         pos = &esupp_rates[2];
200                 } else {
201                         pos = skb_put(skb, 1);
202                         supp_rates[1]++;
203                 }
204                 *pos = rate->bitrate / 5;
205         }
206
207         ieee80211_sta_tx(sdata, skb, 0);
208 }
209
210 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
211                                  struct ieee80211_if_sta *ifsta)
212 {
213         struct ieee80211_local *local = sdata->local;
214         struct sk_buff *skb;
215         struct ieee80211_mgmt *mgmt;
216         u8 *pos, *ies, *ht_add_ie;
217         int i, len, count, rates_len, supp_rates_len;
218         u16 capab;
219         struct ieee80211_sta_bss *bss;
220         int wmm = 0;
221         struct ieee80211_supported_band *sband;
222         u64 rates = 0;
223
224         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
225                             sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
226                             ifsta->ssid_len);
227         if (!skb) {
228                 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
229                        "frame\n", sdata->dev->name);
230                 return;
231         }
232         skb_reserve(skb, local->hw.extra_tx_headroom);
233
234         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
235
236         capab = ifsta->capab;
237
238         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
239                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
240                         capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
241                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
242                         capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
243         }
244
245         bss = ieee80211_rx_bss_get(local, ifsta->bssid,
246                                    local->hw.conf.channel->center_freq,
247                                    ifsta->ssid, ifsta->ssid_len);
248         if (bss) {
249                 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
250                         capab |= WLAN_CAPABILITY_PRIVACY;
251                 if (bss->wmm_used)
252                         wmm = 1;
253
254                 /* get all rates supported by the device and the AP as
255                  * some APs don't like getting a superset of their rates
256                  * in the association request (e.g. D-Link DAP 1353 in
257                  * b-only mode) */
258                 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
259
260                 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
261                     (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
262                         capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
263
264                 ieee80211_rx_bss_put(local, bss);
265         } else {
266                 rates = ~0;
267                 rates_len = sband->n_bitrates;
268         }
269
270         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
271         memset(mgmt, 0, 24);
272         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
273         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
274         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
275
276         if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
277                 skb_put(skb, 10);
278                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
279                                                   IEEE80211_STYPE_REASSOC_REQ);
280                 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
281                 mgmt->u.reassoc_req.listen_interval =
282                                 cpu_to_le16(local->hw.conf.listen_interval);
283                 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
284                        ETH_ALEN);
285         } else {
286                 skb_put(skb, 4);
287                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
288                                                   IEEE80211_STYPE_ASSOC_REQ);
289                 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
290                 mgmt->u.reassoc_req.listen_interval =
291                                 cpu_to_le16(local->hw.conf.listen_interval);
292         }
293
294         /* SSID */
295         ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
296         *pos++ = WLAN_EID_SSID;
297         *pos++ = ifsta->ssid_len;
298         memcpy(pos, ifsta->ssid, ifsta->ssid_len);
299
300         /* add all rates which were marked to be used above */
301         supp_rates_len = rates_len;
302         if (supp_rates_len > 8)
303                 supp_rates_len = 8;
304
305         len = sband->n_bitrates;
306         pos = skb_put(skb, supp_rates_len + 2);
307         *pos++ = WLAN_EID_SUPP_RATES;
308         *pos++ = supp_rates_len;
309
310         count = 0;
311         for (i = 0; i < sband->n_bitrates; i++) {
312                 if (BIT(i) & rates) {
313                         int rate = sband->bitrates[i].bitrate;
314                         *pos++ = (u8) (rate / 5);
315                         if (++count == 8)
316                                 break;
317                 }
318         }
319
320         if (rates_len > count) {
321                 pos = skb_put(skb, rates_len - count + 2);
322                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
323                 *pos++ = rates_len - count;
324
325                 for (i++; i < sband->n_bitrates; i++) {
326                         if (BIT(i) & rates) {
327                                 int rate = sband->bitrates[i].bitrate;
328                                 *pos++ = (u8) (rate / 5);
329                         }
330                 }
331         }
332
333         if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
334                 /* 1. power capabilities */
335                 pos = skb_put(skb, 4);
336                 *pos++ = WLAN_EID_PWR_CAPABILITY;
337                 *pos++ = 2;
338                 *pos++ = 0; /* min tx power */
339                 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
340
341                 /* 2. supported channels */
342                 /* TODO: get this in reg domain format */
343                 pos = skb_put(skb, 2 * sband->n_channels + 2);
344                 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
345                 *pos++ = 2 * sband->n_channels;
346                 for (i = 0; i < sband->n_channels; i++) {
347                         *pos++ = ieee80211_frequency_to_channel(
348                                         sband->channels[i].center_freq);
349                         *pos++ = 1; /* one channel in the subband*/
350                 }
351         }
352
353         if (ifsta->extra_ie) {
354                 pos = skb_put(skb, ifsta->extra_ie_len);
355                 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
356         }
357
358         if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
359                 pos = skb_put(skb, 9);
360                 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
361                 *pos++ = 7; /* len */
362                 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
363                 *pos++ = 0x50;
364                 *pos++ = 0xf2;
365                 *pos++ = 2; /* WME */
366                 *pos++ = 0; /* WME info */
367                 *pos++ = 1; /* WME ver */
368                 *pos++ = 0;
369         }
370
371         /* wmm support is a must to HT */
372         if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
373             sband->ht_info.ht_supported &&
374             (ht_add_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_EXTRA_INFO))) {
375                 struct ieee80211_ht_addt_info *ht_add_info =
376                         (struct ieee80211_ht_addt_info *)ht_add_ie;
377                 u16 cap = sband->ht_info.cap;
378                 __le16 tmp;
379                 u32 flags = local->hw.conf.channel->flags;
380
381                 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
382                 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
383                         if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
384                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
385                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
386                         }
387                         break;
388                 case IEEE80211_HT_IE_CHA_SEC_BELOW:
389                         if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
390                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
391                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
392                         }
393                         break;
394                 }
395
396                 tmp = cpu_to_le16(cap);
397                 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
398                 *pos++ = WLAN_EID_HT_CAPABILITY;
399                 *pos++ = sizeof(struct ieee80211_ht_cap);
400                 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
401                 memcpy(pos, &tmp, sizeof(u16));
402                 pos += sizeof(u16);
403                 /* TODO: needs a define here for << 2 */
404                 *pos++ = sband->ht_info.ampdu_factor |
405                          (sband->ht_info.ampdu_density << 2);
406                 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
407         }
408
409         kfree(ifsta->assocreq_ies);
410         ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
411         ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
412         if (ifsta->assocreq_ies)
413                 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
414
415         ieee80211_sta_tx(sdata, skb, 0);
416 }
417
418
419 static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
420                                            u16 stype, u16 reason)
421 {
422         struct ieee80211_local *local = sdata->local;
423         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
424         struct sk_buff *skb;
425         struct ieee80211_mgmt *mgmt;
426
427         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
428         if (!skb) {
429                 printk(KERN_DEBUG "%s: failed to allocate buffer for "
430                        "deauth/disassoc frame\n", sdata->dev->name);
431                 return;
432         }
433         skb_reserve(skb, local->hw.extra_tx_headroom);
434
435         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
436         memset(mgmt, 0, 24);
437         memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
438         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
439         memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
440         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
441         skb_put(skb, 2);
442         /* u.deauth.reason_code == u.disassoc.reason_code */
443         mgmt->u.deauth.reason_code = cpu_to_le16(reason);
444
445         ieee80211_sta_tx(sdata, skb, 0);
446 }
447
448 /* MLME */
449 static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
450                                          struct ieee80211_sta_bss *bss)
451 {
452         struct ieee80211_local *local = sdata->local;
453         int i, have_higher_than_11mbit = 0;
454
455         /* cf. IEEE 802.11 9.2.12 */
456         for (i = 0; i < bss->supp_rates_len; i++)
457                 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
458                         have_higher_than_11mbit = 1;
459
460         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
461             have_higher_than_11mbit)
462                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
463         else
464                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
465
466         ieee80211_set_wmm_default(sdata);
467 }
468
469 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
470                                      struct ieee80211_if_sta *ifsta,
471                                      u8 *wmm_param, size_t wmm_param_len)
472 {
473         struct ieee80211_tx_queue_params params;
474         size_t left;
475         int count;
476         u8 *pos;
477
478         if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
479                 return;
480
481         if (!wmm_param)
482                 return;
483
484         if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
485                 return;
486         count = wmm_param[6] & 0x0f;
487         if (count == ifsta->wmm_last_param_set)
488                 return;
489         ifsta->wmm_last_param_set = count;
490
491         pos = wmm_param + 8;
492         left = wmm_param_len - 8;
493
494         memset(&params, 0, sizeof(params));
495
496         if (!local->ops->conf_tx)
497                 return;
498
499         local->wmm_acm = 0;
500         for (; left >= 4; left -= 4, pos += 4) {
501                 int aci = (pos[0] >> 5) & 0x03;
502                 int acm = (pos[0] >> 4) & 0x01;
503                 int queue;
504
505                 switch (aci) {
506                 case 1:
507                         queue = 3;
508                         if (acm)
509                                 local->wmm_acm |= BIT(0) | BIT(3);
510                         break;
511                 case 2:
512                         queue = 1;
513                         if (acm)
514                                 local->wmm_acm |= BIT(4) | BIT(5);
515                         break;
516                 case 3:
517                         queue = 0;
518                         if (acm)
519                                 local->wmm_acm |= BIT(6) | BIT(7);
520                         break;
521                 case 0:
522                 default:
523                         queue = 2;
524                         if (acm)
525                                 local->wmm_acm |= BIT(1) | BIT(2);
526                         break;
527                 }
528
529                 params.aifs = pos[0] & 0x0f;
530                 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
531                 params.cw_min = ecw2cw(pos[1] & 0x0f);
532                 params.txop = get_unaligned_le16(pos + 2);
533 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
534                 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
535                        "cWmin=%d cWmax=%d txop=%d\n",
536                        local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
537                        params.cw_max, params.txop);
538 #endif
539                 /* TODO: handle ACM (block TX, fallback to next lowest allowed
540                  * AC for now) */
541                 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
542                         printk(KERN_DEBUG "%s: failed to set TX queue "
543                                "parameters for queue %d\n", local->mdev->name, queue);
544                 }
545         }
546 }
547
548 static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
549                                            bool use_protection,
550                                            bool use_short_preamble)
551 {
552         struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
553 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
554         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
555         DECLARE_MAC_BUF(mac);
556 #endif
557         u32 changed = 0;
558
559         if (use_protection != bss_conf->use_cts_prot) {
560 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
561                 if (net_ratelimit()) {
562                         printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
563                                "%s)\n",
564                                sdata->dev->name,
565                                use_protection ? "enabled" : "disabled",
566                                print_mac(mac, ifsta->bssid));
567                 }
568 #endif
569                 bss_conf->use_cts_prot = use_protection;
570                 changed |= BSS_CHANGED_ERP_CTS_PROT;
571         }
572
573         if (use_short_preamble != bss_conf->use_short_preamble) {
574 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
575                 if (net_ratelimit()) {
576                         printk(KERN_DEBUG "%s: switched to %s barker preamble"
577                                " (BSSID=%s)\n",
578                                sdata->dev->name,
579                                use_short_preamble ? "short" : "long",
580                                print_mac(mac, ifsta->bssid));
581                 }
582 #endif
583                 bss_conf->use_short_preamble = use_short_preamble;
584                 changed |= BSS_CHANGED_ERP_PREAMBLE;
585         }
586
587         return changed;
588 }
589
590 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
591                                    u8 erp_value)
592 {
593         bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
594         bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
595
596         return ieee80211_handle_protect_preamb(sdata,
597                         use_protection, use_short_preamble);
598 }
599
600 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
601                                            struct ieee80211_sta_bss *bss)
602 {
603         u32 changed = 0;
604
605         if (bss->has_erp_value)
606                 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
607         else {
608                 u16 capab = bss->capability;
609                 changed |= ieee80211_handle_protect_preamb(sdata, false,
610                                 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
611         }
612
613         return changed;
614 }
615
616 static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata,
617                                         struct ieee80211_if_sta *ifsta)
618 {
619         union iwreq_data wrqu;
620         memset(&wrqu, 0, sizeof(wrqu));
621         if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
622                 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
623         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
624         wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
625 }
626
627 static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
628                                          struct ieee80211_if_sta *ifsta)
629 {
630         union iwreq_data wrqu;
631
632         if (ifsta->assocreq_ies) {
633                 memset(&wrqu, 0, sizeof(wrqu));
634                 wrqu.data.length = ifsta->assocreq_ies_len;
635                 wireless_send_event(sdata->dev, IWEVASSOCREQIE, &wrqu,
636                                     ifsta->assocreq_ies);
637         }
638         if (ifsta->assocresp_ies) {
639                 memset(&wrqu, 0, sizeof(wrqu));
640                 wrqu.data.length = ifsta->assocresp_ies_len;
641                 wireless_send_event(sdata->dev, IWEVASSOCRESPIE, &wrqu,
642                                     ifsta->assocresp_ies);
643         }
644 }
645
646
647 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
648                                      struct ieee80211_if_sta *ifsta)
649 {
650         struct ieee80211_local *local = sdata->local;
651         struct ieee80211_conf *conf = &local_to_hw(local)->conf;
652         u32 changed = BSS_CHANGED_ASSOC;
653
654         struct ieee80211_sta_bss *bss;
655
656         ifsta->flags |= IEEE80211_STA_ASSOCIATED;
657
658         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
659                 return;
660
661         bss = ieee80211_rx_bss_get(local, ifsta->bssid,
662                                    conf->channel->center_freq,
663                                    ifsta->ssid, ifsta->ssid_len);
664         if (bss) {
665                 /* set timing information */
666                 sdata->bss_conf.beacon_int = bss->beacon_int;
667                 sdata->bss_conf.timestamp = bss->timestamp;
668                 sdata->bss_conf.dtim_period = bss->dtim_period;
669
670                 changed |= ieee80211_handle_bss_capability(sdata, bss);
671
672                 ieee80211_rx_bss_put(local, bss);
673         }
674
675         if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
676                 changed |= BSS_CHANGED_HT;
677                 sdata->bss_conf.assoc_ht = 1;
678                 sdata->bss_conf.ht_conf = &conf->ht_conf;
679                 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
680         }
681
682         ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
683         memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
684         ieee80211_sta_send_associnfo(sdata, ifsta);
685
686         ifsta->last_probe = jiffies;
687         ieee80211_led_assoc(local, 1);
688
689         sdata->bss_conf.assoc = 1;
690         ieee80211_bss_info_change_notify(sdata, changed);
691
692         netif_tx_start_all_queues(sdata->dev);
693         netif_carrier_on(sdata->dev);
694
695         ieee80211_sta_send_apinfo(sdata, ifsta);
696 }
697
698 static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
699                                    struct ieee80211_if_sta *ifsta)
700 {
701         DECLARE_MAC_BUF(mac);
702
703         ifsta->direct_probe_tries++;
704         if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
705                 printk(KERN_DEBUG "%s: direct probe to AP %s timed out\n",
706                        sdata->dev->name, print_mac(mac, ifsta->bssid));
707                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
708                 return;
709         }
710
711         printk(KERN_DEBUG "%s: direct probe to AP %s try %d\n",
712                         sdata->dev->name, print_mac(mac, ifsta->bssid),
713                         ifsta->direct_probe_tries);
714
715         ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
716
717         set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
718
719         /* Direct probe is sent to broadcast address as some APs
720          * will not answer to direct packet in unassociated state.
721          */
722         ieee80211_send_probe_req(sdata, NULL,
723                                  ifsta->ssid, ifsta->ssid_len);
724
725         mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
726 }
727
728
729 static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
730                                    struct ieee80211_if_sta *ifsta)
731 {
732         DECLARE_MAC_BUF(mac);
733
734         ifsta->auth_tries++;
735         if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
736                 printk(KERN_DEBUG "%s: authentication with AP %s"
737                        " timed out\n",
738                        sdata->dev->name, print_mac(mac, ifsta->bssid));
739                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
740                 return;
741         }
742
743         ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
744         printk(KERN_DEBUG "%s: authenticate with AP %s\n",
745                sdata->dev->name, print_mac(mac, ifsta->bssid));
746
747         ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
748
749         mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
750 }
751
752 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
753                                    struct ieee80211_if_sta *ifsta, bool deauth,
754                                    bool self_disconnected, u16 reason)
755 {
756         struct ieee80211_local *local = sdata->local;
757         struct sta_info *sta;
758         u32 changed = BSS_CHANGED_ASSOC;
759
760         rcu_read_lock();
761
762         sta = sta_info_get(local, ifsta->bssid);
763         if (!sta) {
764                 rcu_read_unlock();
765                 return;
766         }
767
768         if (deauth) {
769                 ifsta->direct_probe_tries = 0;
770                 ifsta->auth_tries = 0;
771         }
772         ifsta->assoc_scan_tries = 0;
773         ifsta->assoc_tries = 0;
774
775         netif_tx_stop_all_queues(sdata->dev);
776         netif_carrier_off(sdata->dev);
777
778         ieee80211_sta_tear_down_BA_sessions(sdata, sta->addr);
779
780         if (self_disconnected) {
781                 if (deauth)
782                         ieee80211_send_deauth_disassoc(sdata,
783                                 IEEE80211_STYPE_DEAUTH, reason);
784                 else
785                         ieee80211_send_deauth_disassoc(sdata,
786                                 IEEE80211_STYPE_DISASSOC, reason);
787         }
788
789         ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
790         changed |= ieee80211_reset_erp_info(sdata);
791
792         if (sdata->bss_conf.assoc_ht)
793                 changed |= BSS_CHANGED_HT;
794
795         sdata->bss_conf.assoc_ht = 0;
796         sdata->bss_conf.ht_conf = NULL;
797         sdata->bss_conf.ht_bss_conf = NULL;
798
799         ieee80211_led_assoc(local, 0);
800         sdata->bss_conf.assoc = 0;
801
802         ieee80211_sta_send_apinfo(sdata, ifsta);
803
804         if (self_disconnected)
805                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
806
807         sta_info_unlink(&sta);
808
809         rcu_read_unlock();
810
811         sta_info_destroy(sta);
812 }
813
814 static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
815 {
816         if (!sdata || !sdata->default_key ||
817             sdata->default_key->conf.alg != ALG_WEP)
818                 return 0;
819         return 1;
820 }
821
822 static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
823                                       struct ieee80211_if_sta *ifsta)
824 {
825         struct ieee80211_local *local = sdata->local;
826         struct ieee80211_sta_bss *bss;
827         int bss_privacy;
828         int wep_privacy;
829         int privacy_invoked;
830
831         if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
832                 return 0;
833
834         bss = ieee80211_rx_bss_get(local, ifsta->bssid,
835                                    local->hw.conf.channel->center_freq,
836                                    ifsta->ssid, ifsta->ssid_len);
837         if (!bss)
838                 return 0;
839
840         bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
841         wep_privacy = !!ieee80211_sta_wep_configured(sdata);
842         privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
843
844         ieee80211_rx_bss_put(local, bss);
845
846         if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
847                 return 0;
848
849         return 1;
850 }
851
852 static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
853                                 struct ieee80211_if_sta *ifsta)
854 {
855         DECLARE_MAC_BUF(mac);
856
857         ifsta->assoc_tries++;
858         if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
859                 printk(KERN_DEBUG "%s: association with AP %s"
860                        " timed out\n",
861                        sdata->dev->name, print_mac(mac, ifsta->bssid));
862                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
863                 return;
864         }
865
866         ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
867         printk(KERN_DEBUG "%s: associate with AP %s\n",
868                sdata->dev->name, print_mac(mac, ifsta->bssid));
869         if (ieee80211_privacy_mismatch(sdata, ifsta)) {
870                 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
871                        "mixed-cell disabled - abort association\n", sdata->dev->name);
872                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
873                 return;
874         }
875
876         ieee80211_send_assoc(sdata, ifsta);
877
878         mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
879 }
880
881
882 static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
883                                  struct ieee80211_if_sta *ifsta)
884 {
885         struct ieee80211_local *local = sdata->local;
886         struct sta_info *sta;
887         int disassoc;
888         DECLARE_MAC_BUF(mac);
889
890         /* TODO: start monitoring current AP signal quality and number of
891          * missed beacons. Scan other channels every now and then and search
892          * for better APs. */
893         /* TODO: remove expired BSSes */
894
895         ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
896
897         rcu_read_lock();
898
899         sta = sta_info_get(local, ifsta->bssid);
900         if (!sta) {
901                 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
902                        sdata->dev->name, print_mac(mac, ifsta->bssid));
903                 disassoc = 1;
904         } else {
905                 disassoc = 0;
906                 if (time_after(jiffies,
907                                sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
908                         if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
909                                 printk(KERN_DEBUG "%s: No ProbeResp from "
910                                        "current AP %s - assume out of "
911                                        "range\n",
912                                        sdata->dev->name, print_mac(mac, ifsta->bssid));
913                                 disassoc = 1;
914                         } else
915                                 ieee80211_send_probe_req(sdata, ifsta->bssid,
916                                                          local->scan_ssid,
917                                                          local->scan_ssid_len);
918                         ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
919                 } else {
920                         ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
921                         if (time_after(jiffies, ifsta->last_probe +
922                                        IEEE80211_PROBE_INTERVAL)) {
923                                 ifsta->last_probe = jiffies;
924                                 ieee80211_send_probe_req(sdata, ifsta->bssid,
925                                                          ifsta->ssid,
926                                                          ifsta->ssid_len);
927                         }
928                 }
929         }
930
931         rcu_read_unlock();
932
933         if (disassoc)
934                 ieee80211_set_disassoc(sdata, ifsta, true, true,
935                                         WLAN_REASON_PREV_AUTH_NOT_VALID);
936         else
937                 mod_timer(&ifsta->timer, jiffies +
938                                       IEEE80211_MONITORING_INTERVAL);
939 }
940
941
942 static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
943                                      struct ieee80211_if_sta *ifsta)
944 {
945         printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
946         ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
947         ieee80211_associate(sdata, ifsta);
948 }
949
950
951 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
952                                      struct ieee80211_if_sta *ifsta,
953                                      struct ieee80211_mgmt *mgmt,
954                                      size_t len)
955 {
956         u8 *pos;
957         struct ieee802_11_elems elems;
958
959         pos = mgmt->u.auth.variable;
960         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
961         if (!elems.challenge)
962                 return;
963         ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
964                             elems.challenge_len + 2, 1);
965 }
966
967 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
968                                    struct ieee80211_if_sta *ifsta,
969                                    struct ieee80211_mgmt *mgmt,
970                                    size_t len)
971 {
972         u16 auth_alg, auth_transaction, status_code;
973         DECLARE_MAC_BUF(mac);
974
975         if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
976             sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
977                 return;
978
979         if (len < 24 + 6)
980                 return;
981
982         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
983             memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
984                 return;
985
986         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
987             memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
988                 return;
989
990         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
991         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
992         status_code = le16_to_cpu(mgmt->u.auth.status_code);
993
994         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
995                 /*
996                  * IEEE 802.11 standard does not require authentication in IBSS
997                  * networks and most implementations do not seem to use it.
998                  * However, try to reply to authentication attempts if someone
999                  * has actually implemented this.
1000                  */
1001                 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
1002                         return;
1003                 ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
1004         }
1005
1006         if (auth_alg != ifsta->auth_alg ||
1007             auth_transaction != ifsta->auth_transaction)
1008                 return;
1009
1010         if (status_code != WLAN_STATUS_SUCCESS) {
1011                 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1012                         u8 algs[3];
1013                         const int num_algs = ARRAY_SIZE(algs);
1014                         int i, pos;
1015                         algs[0] = algs[1] = algs[2] = 0xff;
1016                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1017                                 algs[0] = WLAN_AUTH_OPEN;
1018                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1019                                 algs[1] = WLAN_AUTH_SHARED_KEY;
1020                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1021                                 algs[2] = WLAN_AUTH_LEAP;
1022                         if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1023                                 pos = 0;
1024                         else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1025                                 pos = 1;
1026                         else
1027                                 pos = 2;
1028                         for (i = 0; i < num_algs; i++) {
1029                                 pos++;
1030                                 if (pos >= num_algs)
1031                                         pos = 0;
1032                                 if (algs[pos] == ifsta->auth_alg ||
1033                                     algs[pos] == 0xff)
1034                                         continue;
1035                                 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1036                                     !ieee80211_sta_wep_configured(sdata))
1037                                         continue;
1038                                 ifsta->auth_alg = algs[pos];
1039                                 break;
1040                         }
1041                 }
1042                 return;
1043         }
1044
1045         switch (ifsta->auth_alg) {
1046         case WLAN_AUTH_OPEN:
1047         case WLAN_AUTH_LEAP:
1048                 ieee80211_auth_completed(sdata, ifsta);
1049                 break;
1050         case WLAN_AUTH_SHARED_KEY:
1051                 if (ifsta->auth_transaction == 4)
1052                         ieee80211_auth_completed(sdata, ifsta);
1053                 else
1054                         ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
1055                 break;
1056         }
1057 }
1058
1059
1060 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1061                                      struct ieee80211_if_sta *ifsta,
1062                                      struct ieee80211_mgmt *mgmt,
1063                                      size_t len)
1064 {
1065         u16 reason_code;
1066         DECLARE_MAC_BUF(mac);
1067
1068         if (len < 24 + 2)
1069                 return;
1070
1071         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1072                 return;
1073
1074         reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1075
1076         if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
1077                 printk(KERN_DEBUG "%s: deauthenticated\n", sdata->dev->name);
1078
1079         if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
1080             ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
1081             ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1082                 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
1083                 mod_timer(&ifsta->timer, jiffies +
1084                                       IEEE80211_RETRY_AUTH_INTERVAL);
1085         }
1086
1087         ieee80211_set_disassoc(sdata, ifsta, true, false, 0);
1088         ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1089 }
1090
1091
1092 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1093                                        struct ieee80211_if_sta *ifsta,
1094                                        struct ieee80211_mgmt *mgmt,
1095                                        size_t len)
1096 {
1097         u16 reason_code;
1098         DECLARE_MAC_BUF(mac);
1099
1100         if (len < 24 + 2)
1101                 return;
1102
1103         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1104                 return;
1105
1106         reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1107
1108         if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1109                 printk(KERN_DEBUG "%s: disassociated\n", sdata->dev->name);
1110
1111         if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1112                 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
1113                 mod_timer(&ifsta->timer, jiffies +
1114                                       IEEE80211_RETRY_AUTH_INTERVAL);
1115         }
1116
1117         ieee80211_set_disassoc(sdata, ifsta, false, false, 0);
1118 }
1119
1120
1121 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1122                                          struct ieee80211_if_sta *ifsta,
1123                                          struct ieee80211_mgmt *mgmt,
1124                                          size_t len,
1125                                          int reassoc)
1126 {
1127         struct ieee80211_local *local = sdata->local;
1128         struct ieee80211_supported_band *sband;
1129         struct sta_info *sta;
1130         u64 rates, basic_rates;
1131         u16 capab_info, status_code, aid;
1132         struct ieee802_11_elems elems;
1133         struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
1134         u8 *pos;
1135         int i, j;
1136         DECLARE_MAC_BUF(mac);
1137         bool have_higher_than_11mbit = false;
1138
1139         /* AssocResp and ReassocResp have identical structure, so process both
1140          * of them in this function. */
1141
1142         if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
1143                 return;
1144
1145         if (len < 24 + 6)
1146                 return;
1147
1148         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
1149                 return;
1150
1151         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1152         status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1153         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1154
1155         printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1156                "status=%d aid=%d)\n",
1157                sdata->dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1158                capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1159
1160         if (status_code != WLAN_STATUS_SUCCESS) {
1161                 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1162                        sdata->dev->name, status_code);
1163                 /* if this was a reassociation, ensure we try a "full"
1164                  * association next time. This works around some broken APs
1165                  * which do not correctly reject reassociation requests. */
1166                 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1167                 return;
1168         }
1169
1170         if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1171                 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1172                        "set\n", sdata->dev->name, aid);
1173         aid &= ~(BIT(15) | BIT(14));
1174
1175         pos = mgmt->u.assoc_resp.variable;
1176         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1177
1178         if (!elems.supp_rates) {
1179                 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1180                        sdata->dev->name);
1181                 return;
1182         }
1183
1184         printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
1185         ifsta->aid = aid;
1186         ifsta->ap_capab = capab_info;
1187
1188         kfree(ifsta->assocresp_ies);
1189         ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1190         ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1191         if (ifsta->assocresp_ies)
1192                 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1193
1194         rcu_read_lock();
1195
1196         /* Add STA entry for the AP */
1197         sta = sta_info_get(local, ifsta->bssid);
1198         if (!sta) {
1199                 struct ieee80211_sta_bss *bss;
1200                 int err;
1201
1202                 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1203                 if (!sta) {
1204                         printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1205                                " the AP\n", sdata->dev->name);
1206                         rcu_read_unlock();
1207                         return;
1208                 }
1209                 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
1210                                            local->hw.conf.channel->center_freq,
1211                                            ifsta->ssid, ifsta->ssid_len);
1212                 if (bss) {
1213                         sta->last_signal = bss->signal;
1214                         sta->last_qual = bss->qual;
1215                         sta->last_noise = bss->noise;
1216                         ieee80211_rx_bss_put(local, bss);
1217                 }
1218
1219                 err = sta_info_insert(sta);
1220                 if (err) {
1221                         printk(KERN_DEBUG "%s: failed to insert STA entry for"
1222                                " the AP (error %d)\n", sdata->dev->name, err);
1223                         rcu_read_unlock();
1224                         return;
1225                 }
1226                 /* update new sta with its last rx activity */
1227                 sta->last_rx = jiffies;
1228         }
1229
1230         /*
1231          * FIXME: Do we really need to update the sta_info's information here?
1232          *        We already know about the AP (we found it in our list) so it
1233          *        should already be filled with the right info, no?
1234          *        As is stands, all this is racy because typically we assume
1235          *        the information that is filled in here (except flags) doesn't
1236          *        change while a STA structure is alive. As such, it should move
1237          *        to between the sta_info_alloc() and sta_info_insert() above.
1238          */
1239
1240         set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1241                            WLAN_STA_AUTHORIZED);
1242
1243         rates = 0;
1244         basic_rates = 0;
1245         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1246
1247         for (i = 0; i < elems.supp_rates_len; i++) {
1248                 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1249
1250                 if (rate > 110)
1251                         have_higher_than_11mbit = true;
1252
1253                 for (j = 0; j < sband->n_bitrates; j++) {
1254                         if (sband->bitrates[j].bitrate == rate)
1255                                 rates |= BIT(j);
1256                         if (elems.supp_rates[i] & 0x80)
1257                                 basic_rates |= BIT(j);
1258                 }
1259         }
1260
1261         for (i = 0; i < elems.ext_supp_rates_len; i++) {
1262                 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1263
1264                 if (rate > 110)
1265                         have_higher_than_11mbit = true;
1266
1267                 for (j = 0; j < sband->n_bitrates; j++) {
1268                         if (sband->bitrates[j].bitrate == rate)
1269                                 rates |= BIT(j);
1270                         if (elems.ext_supp_rates[i] & 0x80)
1271                                 basic_rates |= BIT(j);
1272                 }
1273         }
1274
1275         sta->supp_rates[local->hw.conf.channel->band] = rates;
1276         sdata->basic_rates = basic_rates;
1277
1278         /* cf. IEEE 802.11 9.2.12 */
1279         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1280             have_higher_than_11mbit)
1281                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1282         else
1283                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1284
1285         if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1286             (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1287                 struct ieee80211_ht_bss_info bss_info;
1288                 ieee80211_ht_cap_ie_to_ht_info(
1289                                 (struct ieee80211_ht_cap *)
1290                                 elems.ht_cap_elem, &sta->ht_info);
1291                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1292                                 (struct ieee80211_ht_addt_info *)
1293                                 elems.ht_info_elem, &bss_info);
1294                 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
1295         }
1296
1297         rate_control_rate_init(sta, local);
1298
1299         if (elems.wmm_param) {
1300                 set_sta_flags(sta, WLAN_STA_WME);
1301                 rcu_read_unlock();
1302                 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
1303                                          elems.wmm_param_len);
1304         } else
1305                 rcu_read_unlock();
1306
1307         /* set AID and assoc capability,
1308          * ieee80211_set_associated() will tell the driver */
1309         bss_conf->aid = aid;
1310         bss_conf->assoc_capability = capab_info;
1311         ieee80211_set_associated(sdata, ifsta);
1312
1313         ieee80211_associated(sdata, ifsta);
1314 }
1315
1316
1317 static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
1318                                    struct ieee80211_if_sta *ifsta,
1319                                    struct ieee80211_sta_bss *bss)
1320 {
1321         struct ieee80211_local *local = sdata->local;
1322         int res, rates, i, j;
1323         struct sk_buff *skb;
1324         struct ieee80211_mgmt *mgmt;
1325         u8 *pos;
1326         struct ieee80211_supported_band *sband;
1327         union iwreq_data wrqu;
1328
1329         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1330
1331         /* Remove possible STA entries from other IBSS networks. */
1332         sta_info_flush_delayed(sdata);
1333
1334         if (local->ops->reset_tsf) {
1335                 /* Reset own TSF to allow time synchronization work. */
1336                 local->ops->reset_tsf(local_to_hw(local));
1337         }
1338         memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
1339         res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
1340         if (res)
1341                 return res;
1342
1343         local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
1344
1345         sdata->drop_unencrypted = bss->capability &
1346                 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
1347
1348         res = ieee80211_set_freq(sdata, bss->freq);
1349
1350         if (res)
1351                 return res;
1352
1353         /* Build IBSS probe response */
1354         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
1355         if (skb) {
1356                 skb_reserve(skb, local->hw.extra_tx_headroom);
1357
1358                 mgmt = (struct ieee80211_mgmt *)
1359                         skb_put(skb, 24 + sizeof(mgmt->u.beacon));
1360                 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
1361                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1362                                                   IEEE80211_STYPE_PROBE_RESP);
1363                 memset(mgmt->da, 0xff, ETH_ALEN);
1364                 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1365                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1366                 mgmt->u.beacon.beacon_int =
1367                         cpu_to_le16(local->hw.conf.beacon_int);
1368                 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
1369                 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
1370
1371                 pos = skb_put(skb, 2 + ifsta->ssid_len);
1372                 *pos++ = WLAN_EID_SSID;
1373                 *pos++ = ifsta->ssid_len;
1374                 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
1375
1376                 rates = bss->supp_rates_len;
1377                 if (rates > 8)
1378                         rates = 8;
1379                 pos = skb_put(skb, 2 + rates);
1380                 *pos++ = WLAN_EID_SUPP_RATES;
1381                 *pos++ = rates;
1382                 memcpy(pos, bss->supp_rates, rates);
1383
1384                 if (bss->band == IEEE80211_BAND_2GHZ) {
1385                         pos = skb_put(skb, 2 + 1);
1386                         *pos++ = WLAN_EID_DS_PARAMS;
1387                         *pos++ = 1;
1388                         *pos++ = ieee80211_frequency_to_channel(bss->freq);
1389                 }
1390
1391                 pos = skb_put(skb, 2 + 2);
1392                 *pos++ = WLAN_EID_IBSS_PARAMS;
1393                 *pos++ = 2;
1394                 /* FIX: set ATIM window based on scan results */
1395                 *pos++ = 0;
1396                 *pos++ = 0;
1397
1398                 if (bss->supp_rates_len > 8) {
1399                         rates = bss->supp_rates_len - 8;
1400                         pos = skb_put(skb, 2 + rates);
1401                         *pos++ = WLAN_EID_EXT_SUPP_RATES;
1402                         *pos++ = rates;
1403                         memcpy(pos, &bss->supp_rates[8], rates);
1404                 }
1405
1406                 ifsta->probe_resp = skb;
1407
1408                 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
1409         }
1410
1411         rates = 0;
1412         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1413         for (i = 0; i < bss->supp_rates_len; i++) {
1414                 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
1415                 for (j = 0; j < sband->n_bitrates; j++)
1416                         if (sband->bitrates[j].bitrate == bitrate)
1417                                 rates |= BIT(j);
1418         }
1419         ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
1420
1421         ieee80211_sta_def_wmm_params(sdata, bss);
1422
1423         ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
1424         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1425
1426         memset(&wrqu, 0, sizeof(wrqu));
1427         memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
1428         wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
1429
1430         return res;
1431 }
1432
1433 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
1434                             struct ieee802_11_elems *elems,
1435                             enum ieee80211_band band)
1436 {
1437         struct ieee80211_supported_band *sband;
1438         struct ieee80211_rate *bitrates;
1439         size_t num_rates;
1440         u64 supp_rates;
1441         int i, j;
1442         sband = local->hw.wiphy->bands[band];
1443
1444         if (!sband) {
1445                 WARN_ON(1);
1446                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1447         }
1448
1449         bitrates = sband->bitrates;
1450         num_rates = sband->n_bitrates;
1451         supp_rates = 0;
1452         for (i = 0; i < elems->supp_rates_len +
1453                      elems->ext_supp_rates_len; i++) {
1454                 u8 rate = 0;
1455                 int own_rate;
1456                 if (i < elems->supp_rates_len)
1457                         rate = elems->supp_rates[i];
1458                 else if (elems->ext_supp_rates)
1459                         rate = elems->ext_supp_rates
1460                                 [i - elems->supp_rates_len];
1461                 own_rate = 5 * (rate & 0x7f);
1462                 for (j = 0; j < num_rates; j++)
1463                         if (bitrates[j].bitrate == own_rate)
1464                                 supp_rates |= BIT(j);
1465         }
1466         return supp_rates;
1467 }
1468
1469 static u64 ieee80211_sta_get_mandatory_rates(struct ieee80211_local *local,
1470                                         enum ieee80211_band band)
1471 {
1472         struct ieee80211_supported_band *sband;
1473         struct ieee80211_rate *bitrates;
1474         u64 mandatory_rates;
1475         enum ieee80211_rate_flags mandatory_flag;
1476         int i;
1477
1478         sband = local->hw.wiphy->bands[band];
1479         if (!sband) {
1480                 WARN_ON(1);
1481                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1482         }
1483
1484         if (band == IEEE80211_BAND_2GHZ)
1485                 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
1486         else
1487                 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
1488
1489         bitrates = sband->bitrates;
1490         mandatory_rates = 0;
1491         for (i = 0; i < sband->n_bitrates; i++)
1492                 if (bitrates[i].flags & mandatory_flag)
1493                         mandatory_rates |= BIT(i);
1494         return mandatory_rates;
1495 }
1496
1497 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1498                                   struct ieee80211_mgmt *mgmt,
1499                                   size_t len,
1500                                   struct ieee80211_rx_status *rx_status,
1501                                   struct ieee802_11_elems *elems,
1502                                   bool beacon)
1503 {
1504         struct ieee80211_local *local = sdata->local;
1505         int freq;
1506         struct ieee80211_sta_bss *bss;
1507         struct sta_info *sta;
1508         struct ieee80211_channel *channel;
1509         u64 beacon_timestamp, rx_timestamp;
1510         u64 supp_rates = 0;
1511         enum ieee80211_band band = rx_status->band;
1512         DECLARE_MAC_BUF(mac);
1513         DECLARE_MAC_BUF(mac2);
1514
1515         if (elems->ds_params && elems->ds_params_len == 1)
1516                 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1517         else
1518                 freq = rx_status->freq;
1519
1520         channel = ieee80211_get_channel(local->hw.wiphy, freq);
1521
1522         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1523                 return;
1524
1525         if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
1526             elems->mesh_config && mesh_matches_local(elems, sdata)) {
1527                 supp_rates = ieee80211_sta_get_rates(local, elems, band);
1528
1529                 mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
1530                                       mesh_peer_accepts_plinks(elems));
1531         }
1532
1533         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
1534             memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1535                 supp_rates = ieee80211_sta_get_rates(local, elems, band);
1536
1537                 rcu_read_lock();
1538
1539                 sta = sta_info_get(local, mgmt->sa);
1540                 if (sta) {
1541                         u64 prev_rates;
1542
1543                         prev_rates = sta->supp_rates[band];
1544                         /* make sure mandatory rates are always added */
1545                         sta->supp_rates[band] = supp_rates |
1546                                 ieee80211_sta_get_mandatory_rates(local, band);
1547
1548 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1549                         if (sta->supp_rates[band] != prev_rates)
1550                                 printk(KERN_DEBUG "%s: updated supp_rates set "
1551                                     "for %s based on beacon info (0x%llx | "
1552                                     "0x%llx -> 0x%llx)\n",
1553                                     sdata->dev->name, print_mac(mac, sta->addr),
1554                                     (unsigned long long) prev_rates,
1555                                     (unsigned long long) supp_rates,
1556                                     (unsigned long long) sta->supp_rates[band]);
1557 #endif
1558                 } else {
1559                         ieee80211_ibss_add_sta(sdata, NULL, mgmt->bssid,
1560                                                mgmt->sa, supp_rates);
1561                 }
1562
1563                 rcu_read_unlock();
1564         }
1565
1566         bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1567                                         freq, beacon);
1568         if (!bss)
1569                 return;
1570
1571         /* was just updated in ieee80211_bss_info_update */
1572         beacon_timestamp = bss->timestamp;
1573
1574         /*
1575          * In STA mode, the remaining parameters should not be overridden
1576          * by beacons because they're not necessarily accurate there.
1577          */
1578         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1579             bss->last_probe_resp && beacon) {
1580                 ieee80211_rx_bss_put(local, bss);
1581                 return;
1582         }
1583
1584         /* check if we need to merge IBSS */
1585         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
1586             bss->capability & WLAN_CAPABILITY_IBSS &&
1587             bss->freq == local->oper_channel->center_freq &&
1588             elems->ssid_len == sdata->u.sta.ssid_len &&
1589             memcmp(elems->ssid, sdata->u.sta.ssid,
1590                                 sdata->u.sta.ssid_len) == 0) {
1591                 if (rx_status->flag & RX_FLAG_TSFT) {
1592                         /* in order for correct IBSS merging we need mactime
1593                          *
1594                          * since mactime is defined as the time the first data
1595                          * symbol of the frame hits the PHY, and the timestamp
1596                          * of the beacon is defined as "the time that the data
1597                          * symbol containing the first bit of the timestamp is
1598                          * transmitted to the PHY plus the transmitting STA’s
1599                          * delays through its local PHY from the MAC-PHY
1600                          * interface to its interface with the WM"
1601                          * (802.11 11.1.2) - equals the time this bit arrives at
1602                          * the receiver - we have to take into account the
1603                          * offset between the two.
1604                          * e.g: at 1 MBit that means mactime is 192 usec earlier
1605                          * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
1606                          */
1607                         int rate = local->hw.wiphy->bands[band]->
1608                                         bitrates[rx_status->rate_idx].bitrate;
1609                         rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
1610                 } else if (local && local->ops && local->ops->get_tsf)
1611                         /* second best option: get current TSF */
1612                         rx_timestamp = local->ops->get_tsf(local_to_hw(local));
1613                 else
1614                         /* can't merge without knowing the TSF */
1615                         rx_timestamp = -1LLU;
1616 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1617                 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
1618                        "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
1619                        print_mac(mac, mgmt->sa),
1620                        print_mac(mac2, mgmt->bssid),
1621                        (unsigned long long)rx_timestamp,
1622                        (unsigned long long)beacon_timestamp,
1623                        (unsigned long long)(rx_timestamp - beacon_timestamp),
1624                        jiffies);
1625 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1626                 if (beacon_timestamp > rx_timestamp) {
1627 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1628                         printk(KERN_DEBUG "%s: beacon TSF higher than "
1629                                "local TSF - IBSS merge with BSSID %s\n",
1630                                sdata->dev->name, print_mac(mac, mgmt->bssid));
1631 #endif
1632                         ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
1633                         ieee80211_ibss_add_sta(sdata, NULL,
1634                                                mgmt->bssid, mgmt->sa,
1635                                                supp_rates);
1636                 }
1637         }
1638
1639         ieee80211_rx_bss_put(local, bss);
1640 }
1641
1642
1643 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
1644                                          struct ieee80211_mgmt *mgmt,
1645                                          size_t len,
1646                                          struct ieee80211_rx_status *rx_status)
1647 {
1648         size_t baselen;
1649         struct ieee802_11_elems elems;
1650         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1651
1652         if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
1653                 return; /* ignore ProbeResp to foreign address */
1654
1655         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1656         if (baselen > len)
1657                 return;
1658
1659         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1660                                 &elems);
1661
1662         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
1663
1664         /* direct probe may be part of the association flow */
1665         if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
1666                                                         &ifsta->request)) {
1667                 printk(KERN_DEBUG "%s direct probe responded\n",
1668                        sdata->dev->name);
1669                 ieee80211_authenticate(sdata, ifsta);
1670         }
1671 }
1672
1673
1674 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
1675                                      struct ieee80211_mgmt *mgmt,
1676                                      size_t len,
1677                                      struct ieee80211_rx_status *rx_status)
1678 {
1679         struct ieee80211_if_sta *ifsta;
1680         size_t baselen;
1681         struct ieee802_11_elems elems;
1682         struct ieee80211_local *local = sdata->local;
1683         struct ieee80211_conf *conf = &local->hw.conf;
1684         u32 changed = 0;
1685
1686         /* Process beacon from the current BSS */
1687         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1688         if (baselen > len)
1689                 return;
1690
1691         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
1692
1693         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
1694
1695         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
1696                 return;
1697         ifsta = &sdata->u.sta;
1698
1699         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
1700             memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1701                 return;
1702
1703         ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
1704                                  elems.wmm_param_len);
1705
1706         if (elems.erp_info && elems.erp_info_len >= 1)
1707                 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
1708         else {
1709                 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
1710                 changed |= ieee80211_handle_protect_preamb(sdata, false,
1711                                 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
1712         }
1713
1714         if (elems.ht_cap_elem && elems.ht_info_elem &&
1715             elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
1716                 struct ieee80211_ht_bss_info bss_info;
1717
1718                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1719                                 (struct ieee80211_ht_addt_info *)
1720                                 elems.ht_info_elem, &bss_info);
1721                 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
1722                                                &bss_info);
1723         }
1724
1725         ieee80211_bss_info_change_notify(sdata, changed);
1726 }
1727
1728
1729 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
1730                                         struct ieee80211_if_sta *ifsta,
1731                                         struct ieee80211_mgmt *mgmt,
1732                                         size_t len,
1733                                         struct ieee80211_rx_status *rx_status)
1734 {
1735         struct ieee80211_local *local = sdata->local;
1736         int tx_last_beacon;
1737         struct sk_buff *skb;
1738         struct ieee80211_mgmt *resp;
1739         u8 *pos, *end;
1740         DECLARE_MAC_BUF(mac);
1741 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1742         DECLARE_MAC_BUF(mac2);
1743         DECLARE_MAC_BUF(mac3);
1744 #endif
1745
1746         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
1747             ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
1748             len < 24 + 2 || !ifsta->probe_resp)
1749                 return;
1750
1751         if (local->ops->tx_last_beacon)
1752                 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1753         else
1754                 tx_last_beacon = 1;
1755
1756 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1757         printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
1758                "%s (tx_last_beacon=%d)\n",
1759                sdata->dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
1760                print_mac(mac3, mgmt->bssid), tx_last_beacon);
1761 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1762
1763         if (!tx_last_beacon)
1764                 return;
1765
1766         if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1767             memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1768                 return;
1769
1770         end = ((u8 *) mgmt) + len;
1771         pos = mgmt->u.probe_req.variable;
1772         if (pos[0] != WLAN_EID_SSID ||
1773             pos + 2 + pos[1] > end) {
1774 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1775                 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1776                        "from %s\n",
1777                        sdata->dev->name, print_mac(mac, mgmt->sa));
1778 #endif
1779                 return;
1780         }
1781         if (pos[1] != 0 &&
1782             (pos[1] != ifsta->ssid_len ||
1783              memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1784                 /* Ignore ProbeReq for foreign SSID */
1785                 return;
1786         }
1787
1788         /* Reply with ProbeResp */
1789         skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
1790         if (!skb)
1791                 return;
1792
1793         resp = (struct ieee80211_mgmt *) skb->data;
1794         memcpy(resp->da, mgmt->sa, ETH_ALEN);
1795 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1796         printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
1797                sdata->dev->name, print_mac(mac, resp->da));
1798 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
1799         ieee80211_sta_tx(sdata, skb, 0);
1800 }
1801
1802 static void ieee80211_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
1803                                      struct ieee80211_if_sta *ifsta,
1804                                      struct ieee80211_mgmt *mgmt,
1805                                      size_t len,
1806                                      struct ieee80211_rx_status *rx_status)
1807 {
1808         /* currently we only handle mesh interface action frames here */
1809         if (!ieee80211_vif_is_mesh(&sdata->vif))
1810                 return;
1811
1812         switch (mgmt->u.action.category) {
1813         case PLINK_CATEGORY:
1814                 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
1815                 break;
1816         case MESH_PATH_SEL_CATEGORY:
1817                 mesh_rx_path_sel_frame(sdata, mgmt, len);
1818                 break;
1819         }
1820 }
1821
1822 void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
1823                            struct ieee80211_rx_status *rx_status)
1824 {
1825         struct ieee80211_local *local = sdata->local;
1826         struct ieee80211_if_sta *ifsta;
1827         struct ieee80211_mgmt *mgmt;
1828         u16 fc;
1829
1830         if (skb->len < 24)
1831                 goto fail;
1832
1833         ifsta = &sdata->u.sta;
1834
1835         mgmt = (struct ieee80211_mgmt *) skb->data;
1836         fc = le16_to_cpu(mgmt->frame_control);
1837
1838         switch (fc & IEEE80211_FCTL_STYPE) {
1839         case IEEE80211_STYPE_PROBE_REQ:
1840         case IEEE80211_STYPE_PROBE_RESP:
1841         case IEEE80211_STYPE_BEACON:
1842         case IEEE80211_STYPE_ACTION:
1843                 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1844         case IEEE80211_STYPE_AUTH:
1845         case IEEE80211_STYPE_ASSOC_RESP:
1846         case IEEE80211_STYPE_REASSOC_RESP:
1847         case IEEE80211_STYPE_DEAUTH:
1848         case IEEE80211_STYPE_DISASSOC:
1849                 skb_queue_tail(&ifsta->skb_queue, skb);
1850                 queue_work(local->hw.workqueue, &ifsta->work);
1851                 return;
1852         }
1853
1854  fail:
1855         kfree_skb(skb);
1856 }
1857
1858 static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1859                                          struct sk_buff *skb)
1860 {
1861         struct ieee80211_rx_status *rx_status;
1862         struct ieee80211_if_sta *ifsta;
1863         struct ieee80211_mgmt *mgmt;
1864         u16 fc;
1865
1866         ifsta = &sdata->u.sta;
1867
1868         rx_status = (struct ieee80211_rx_status *) skb->cb;
1869         mgmt = (struct ieee80211_mgmt *) skb->data;
1870         fc = le16_to_cpu(mgmt->frame_control);
1871
1872         switch (fc & IEEE80211_FCTL_STYPE) {
1873         case IEEE80211_STYPE_PROBE_REQ:
1874                 ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len,
1875                                             rx_status);
1876                 break;
1877         case IEEE80211_STYPE_PROBE_RESP:
1878                 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
1879                 break;
1880         case IEEE80211_STYPE_BEACON:
1881                 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
1882                 break;
1883         case IEEE80211_STYPE_AUTH:
1884                 ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
1885                 break;
1886         case IEEE80211_STYPE_ASSOC_RESP:
1887                 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
1888                 break;
1889         case IEEE80211_STYPE_REASSOC_RESP:
1890                 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
1891                 break;
1892         case IEEE80211_STYPE_DEAUTH:
1893                 ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
1894                 break;
1895         case IEEE80211_STYPE_DISASSOC:
1896                 ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
1897                 break;
1898         case IEEE80211_STYPE_ACTION:
1899                 ieee80211_rx_mgmt_action(sdata, ifsta, mgmt, skb->len, rx_status);
1900                 break;
1901         }
1902
1903         kfree_skb(skb);
1904 }
1905
1906
1907 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
1908 {
1909         struct ieee80211_local *local = sdata->local;
1910         int active = 0;
1911         struct sta_info *sta;
1912
1913         rcu_read_lock();
1914
1915         list_for_each_entry_rcu(sta, &local->sta_list, list) {
1916                 if (sta->sdata == sdata &&
1917                     time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1918                                jiffies)) {
1919                         active++;
1920                         break;
1921                 }
1922         }
1923
1924         rcu_read_unlock();
1925
1926         return active;
1927 }
1928
1929
1930 static void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, unsigned long exp_time)
1931 {
1932         struct ieee80211_local *local = sdata->local;
1933         struct sta_info *sta, *tmp;
1934         LIST_HEAD(tmp_list);
1935         DECLARE_MAC_BUF(mac);
1936         unsigned long flags;
1937
1938         spin_lock_irqsave(&local->sta_lock, flags);
1939         list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1940                 if (time_after(jiffies, sta->last_rx + exp_time)) {
1941 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1942                         printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
1943                                sdata->dev->name, print_mac(mac, sta->addr));
1944 #endif
1945                         __sta_info_unlink(&sta);
1946                         if (sta)
1947                                 list_add(&sta->list, &tmp_list);
1948                 }
1949         spin_unlock_irqrestore(&local->sta_lock, flags);
1950
1951         list_for_each_entry_safe(sta, tmp, &tmp_list, list)
1952                 sta_info_destroy(sta);
1953 }
1954
1955
1956 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
1957                                      struct ieee80211_if_sta *ifsta)
1958 {
1959         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1960
1961         ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
1962         if (ieee80211_sta_active_ibss(sdata))
1963                 return;
1964
1965         printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1966                "IBSS networks with same SSID (merge)\n", sdata->dev->name);
1967         ieee80211_sta_req_scan(sdata, ifsta->ssid, ifsta->ssid_len);
1968 }
1969
1970
1971 #ifdef CONFIG_MAC80211_MESH
1972 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
1973                            struct ieee80211_if_sta *ifsta)
1974 {
1975         bool free_plinks;
1976
1977         ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
1978         mesh_path_expire(sdata);
1979
1980         free_plinks = mesh_plink_availables(sdata);
1981         if (free_plinks != sdata->u.sta.accepting_plinks)
1982                 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
1983
1984         mod_timer(&ifsta->timer, jiffies +
1985                         IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
1986 }
1987
1988
1989 void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
1990 {
1991         struct ieee80211_if_sta *ifsta;
1992         ifsta = &sdata->u.sta;
1993         ifsta->state = IEEE80211_STA_MLME_MESH_UP;
1994         ieee80211_sta_timer((unsigned long)sdata);
1995         ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
1996 }
1997 #endif
1998
1999
2000 void ieee80211_sta_timer(unsigned long data)
2001 {
2002         struct ieee80211_sub_if_data *sdata =
2003                 (struct ieee80211_sub_if_data *) data;
2004         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2005         struct ieee80211_local *local = sdata->local;
2006
2007         set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2008         queue_work(local->hw.workqueue, &ifsta->work);
2009 }
2010
2011 static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
2012                                      struct ieee80211_if_sta *ifsta)
2013 {
2014         struct ieee80211_local *local = sdata->local;
2015
2016         if (local->ops->reset_tsf) {
2017                 /* Reset own TSF to allow time synchronization work. */
2018                 local->ops->reset_tsf(local_to_hw(local));
2019         }
2020
2021         ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2022
2023
2024         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2025                 ifsta->auth_alg = WLAN_AUTH_OPEN;
2026         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2027                 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2028         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2029                 ifsta->auth_alg = WLAN_AUTH_LEAP;
2030         else
2031                 ifsta->auth_alg = WLAN_AUTH_OPEN;
2032         ifsta->auth_transaction = -1;
2033         ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2034         ifsta->assoc_scan_tries = 0;
2035         ifsta->direct_probe_tries = 0;
2036         ifsta->auth_tries = 0;
2037         ifsta->assoc_tries = 0;
2038         netif_tx_stop_all_queues(sdata->dev);
2039         netif_carrier_off(sdata->dev);
2040 }
2041
2042
2043 void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
2044                             struct ieee80211_if_sta *ifsta)
2045 {
2046         struct ieee80211_local *local = sdata->local;
2047
2048         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2049                 return;
2050
2051         if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2052                              IEEE80211_STA_AUTO_BSSID_SEL)) &&
2053             (ifsta->flags & (IEEE80211_STA_SSID_SET |
2054                              IEEE80211_STA_AUTO_SSID_SEL))) {
2055
2056                 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED)
2057                         ieee80211_set_disassoc(sdata, ifsta, true, true,
2058                                                WLAN_REASON_DEAUTH_LEAVING);
2059
2060                 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2061                 queue_work(local->hw.workqueue, &ifsta->work);
2062         }
2063 }
2064
2065 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2066                                     const char *ssid, int ssid_len)
2067 {
2068         int tmp, hidden_ssid;
2069
2070         if (ssid_len == ifsta->ssid_len &&
2071             !memcmp(ifsta->ssid, ssid, ssid_len))
2072                 return 1;
2073
2074         if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
2075                 return 0;
2076
2077         hidden_ssid = 1;
2078         tmp = ssid_len;
2079         while (tmp--) {
2080                 if (ssid[tmp] != '\0') {
2081                         hidden_ssid = 0;
2082                         break;
2083                 }
2084         }
2085
2086         if (hidden_ssid && ifsta->ssid_len == ssid_len)
2087                 return 1;
2088
2089         if (ssid_len == 1 && ssid[0] == ' ')
2090                 return 1;
2091
2092         return 0;
2093 }
2094
2095 static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
2096                                      struct ieee80211_if_sta *ifsta)
2097 {
2098         struct ieee80211_local *local = sdata->local;
2099         struct ieee80211_sta_bss *bss;
2100         struct ieee80211_supported_band *sband;
2101         u8 bssid[ETH_ALEN], *pos;
2102         int i;
2103         int ret;
2104         DECLARE_MAC_BUF(mac);
2105
2106 #if 0
2107         /* Easier testing, use fixed BSSID. */
2108         memset(bssid, 0xfe, ETH_ALEN);
2109 #else
2110         /* Generate random, not broadcast, locally administered BSSID. Mix in
2111          * own MAC address to make sure that devices that do not have proper
2112          * random number generator get different BSSID. */
2113         get_random_bytes(bssid, ETH_ALEN);
2114         for (i = 0; i < ETH_ALEN; i++)
2115                 bssid[i] ^= sdata->dev->dev_addr[i];
2116         bssid[0] &= ~0x01;
2117         bssid[0] |= 0x02;
2118 #endif
2119
2120         printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
2121                sdata->dev->name, print_mac(mac, bssid));
2122
2123         bss = ieee80211_rx_bss_add(local, bssid,
2124                                    local->hw.conf.channel->center_freq,
2125                                    sdata->u.sta.ssid, sdata->u.sta.ssid_len);
2126         if (!bss)
2127                 return -ENOMEM;
2128
2129         bss->band = local->hw.conf.channel->band;
2130         sband = local->hw.wiphy->bands[bss->band];
2131
2132         if (local->hw.conf.beacon_int == 0)
2133                 local->hw.conf.beacon_int = 100;
2134         bss->beacon_int = local->hw.conf.beacon_int;
2135         bss->last_update = jiffies;
2136         bss->capability = WLAN_CAPABILITY_IBSS;
2137
2138         if (sdata->default_key)
2139                 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2140         else
2141                 sdata->drop_unencrypted = 0;
2142
2143         bss->supp_rates_len = sband->n_bitrates;
2144         pos = bss->supp_rates;
2145         for (i = 0; i < sband->n_bitrates; i++) {
2146                 int rate = sband->bitrates[i].bitrate;
2147                 *pos++ = (u8) (rate / 5);
2148         }
2149
2150         ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
2151         ieee80211_rx_bss_put(local, bss);
2152         return ret;
2153 }
2154
2155
2156 static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
2157                                    struct ieee80211_if_sta *ifsta)
2158 {
2159         struct ieee80211_local *local = sdata->local;
2160         struct ieee80211_sta_bss *bss;
2161         int found = 0;
2162         u8 bssid[ETH_ALEN];
2163         int active_ibss;
2164         DECLARE_MAC_BUF(mac);
2165         DECLARE_MAC_BUF(mac2);
2166
2167         if (ifsta->ssid_len == 0)
2168                 return -EINVAL;
2169
2170         active_ibss = ieee80211_sta_active_ibss(sdata);
2171 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2172         printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2173                sdata->dev->name, active_ibss);
2174 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2175         spin_lock_bh(&local->sta_bss_lock);
2176         list_for_each_entry(bss, &local->sta_bss_list, list) {
2177                 if (ifsta->ssid_len != bss->ssid_len ||
2178                     memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2179                     || !(bss->capability & WLAN_CAPABILITY_IBSS))
2180                         continue;
2181 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2182                 printk(KERN_DEBUG "   bssid=%s found\n",
2183                        print_mac(mac, bss->bssid));
2184 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2185                 memcpy(bssid, bss->bssid, ETH_ALEN);
2186                 found = 1;
2187                 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2188                         break;
2189         }
2190         spin_unlock_bh(&local->sta_bss_lock);
2191
2192 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2193         if (found)
2194                 printk(KERN_DEBUG "   sta_find_ibss: selected %s current "
2195                        "%s\n", print_mac(mac, bssid),
2196                        print_mac(mac2, ifsta->bssid));
2197 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2198
2199         if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2200                 int ret;
2201                 int search_freq;
2202
2203                 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
2204                         search_freq = bss->freq;
2205                 else
2206                         search_freq = local->hw.conf.channel->center_freq;
2207
2208                 bss = ieee80211_rx_bss_get(local, bssid, search_freq,
2209                                            ifsta->ssid, ifsta->ssid_len);
2210                 if (!bss)
2211                         goto dont_join;
2212
2213                 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
2214                        " based on configured SSID\n",
2215                        sdata->dev->name, print_mac(mac, bssid));
2216                 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
2217                 ieee80211_rx_bss_put(local, bss);
2218                 return ret;
2219         }
2220
2221 dont_join:
2222 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2223         printk(KERN_DEBUG "   did not try to join ibss\n");
2224 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2225
2226         /* Selected IBSS not found in current scan results - try to scan */
2227         if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
2228             !ieee80211_sta_active_ibss(sdata)) {
2229                 mod_timer(&ifsta->timer, jiffies +
2230                                       IEEE80211_IBSS_MERGE_INTERVAL);
2231         } else if (time_after(jiffies, local->last_scan_completed +
2232                               IEEE80211_SCAN_INTERVAL)) {
2233                 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2234                        "join\n", sdata->dev->name);
2235                 return ieee80211_sta_req_scan(sdata, ifsta->ssid,
2236                                               ifsta->ssid_len);
2237         } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
2238                 int interval = IEEE80211_SCAN_INTERVAL;
2239
2240                 if (time_after(jiffies, ifsta->ibss_join_req +
2241                                IEEE80211_IBSS_JOIN_TIMEOUT)) {
2242                         if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
2243                             (!(local->oper_channel->flags &
2244                                         IEEE80211_CHAN_NO_IBSS)))
2245                                 return ieee80211_sta_create_ibss(sdata, ifsta);
2246                         if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
2247                                 printk(KERN_DEBUG "%s: IBSS not allowed on"
2248                                        " %d MHz\n", sdata->dev->name,
2249                                        local->hw.conf.channel->center_freq);
2250                         }
2251
2252                         /* No IBSS found - decrease scan interval and continue
2253                          * scanning. */
2254                         interval = IEEE80211_SCAN_INTERVAL_SLOW;
2255                 }
2256
2257                 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
2258                 mod_timer(&ifsta->timer, jiffies + interval);
2259                 return 0;
2260         }
2261
2262         return 0;
2263 }
2264
2265
2266 int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
2267 {
2268         struct ieee80211_if_sta *ifsta;
2269         int res;
2270
2271         if (len > IEEE80211_MAX_SSID_LEN)
2272                 return -EINVAL;
2273
2274         ifsta = &sdata->u.sta;
2275
2276         if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
2277                 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
2278                 memcpy(ifsta->ssid, ssid, len);
2279                 ifsta->ssid_len = len;
2280                 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2281
2282                 res = 0;
2283                 /*
2284                  * Hack! MLME code needs to be cleaned up to have different
2285                  * entry points for configuration and internal selection change
2286                  */
2287                 if (netif_running(sdata->dev))
2288                         res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
2289                 if (res) {
2290                         printk(KERN_DEBUG "%s: Failed to config new SSID to "
2291                                "the low-level driver\n", sdata->dev->name);
2292                         return res;
2293                 }
2294         }
2295
2296         if (len)
2297                 ifsta->flags |= IEEE80211_STA_SSID_SET;
2298         else
2299                 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
2300
2301         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
2302             !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
2303                 ifsta->ibss_join_req = jiffies;
2304                 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
2305                 return ieee80211_sta_find_ibss(sdata, ifsta);
2306         }
2307
2308         return 0;
2309 }
2310
2311
2312 int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
2313 {
2314         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2315         memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2316         *len = ifsta->ssid_len;
2317         return 0;
2318 }
2319
2320
2321 int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
2322 {
2323         struct ieee80211_if_sta *ifsta;
2324         int res;
2325
2326         ifsta = &sdata->u.sta;
2327
2328         if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2329                 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2330                 res = 0;
2331                 /*
2332                  * Hack! See also ieee80211_sta_set_ssid.
2333                  */
2334                 if (netif_running(sdata->dev))
2335                         res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
2336                 if (res) {
2337                         printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2338                                "the low-level driver\n", sdata->dev->name);
2339                         return res;
2340                 }
2341         }
2342
2343         if (is_valid_ether_addr(bssid))
2344                 ifsta->flags |= IEEE80211_STA_BSSID_SET;
2345         else
2346                 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2347
2348         return 0;
2349 }
2350
2351
2352 int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
2353 {
2354         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2355
2356         kfree(ifsta->extra_ie);
2357         if (len == 0) {
2358                 ifsta->extra_ie = NULL;
2359                 ifsta->extra_ie_len = 0;
2360                 return 0;
2361         }
2362         ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
2363         if (!ifsta->extra_ie) {
2364                 ifsta->extra_ie_len = 0;
2365                 return -ENOMEM;
2366         }
2367         memcpy(ifsta->extra_ie, ie, len);
2368         ifsta->extra_ie_len = len;
2369         return 0;
2370 }
2371
2372
2373 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
2374                                         struct sk_buff *skb, u8 *bssid,
2375                                         u8 *addr, u64 supp_rates)
2376 {
2377         struct ieee80211_local *local = sdata->local;
2378         struct sta_info *sta;
2379         DECLARE_MAC_BUF(mac);
2380         int band = local->hw.conf.channel->band;
2381
2382         /* TODO: Could consider removing the least recently used entry and
2383          * allow new one to be added. */
2384         if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
2385                 if (net_ratelimit()) {
2386                         printk(KERN_DEBUG "%s: No room for a new IBSS STA "
2387                                "entry %s\n", sdata->dev->name, print_mac(mac, addr));
2388                 }
2389                 return NULL;
2390         }
2391
2392         if (compare_ether_addr(bssid, sdata->u.sta.bssid))
2393                 return NULL;
2394
2395 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2396         printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
2397                wiphy_name(local->hw.wiphy), print_mac(mac, addr), sdata->dev->name);
2398 #endif
2399
2400         sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
2401         if (!sta)
2402                 return NULL;
2403
2404         set_sta_flags(sta, WLAN_STA_AUTHORIZED);
2405
2406         /* make sure mandatory rates are always added */
2407         sta->supp_rates[band] = supp_rates |
2408                         ieee80211_sta_get_mandatory_rates(local, band);
2409
2410         rate_control_rate_init(sta, local);
2411
2412         if (sta_info_insert(sta))
2413                 return NULL;
2414
2415         return sta;
2416 }
2417
2418
2419 static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
2420                                      struct ieee80211_if_sta *ifsta)
2421 {
2422         struct ieee80211_local *local = sdata->local;
2423         struct ieee80211_sta_bss *bss, *selected = NULL;
2424         int top_rssi = 0, freq;
2425
2426         spin_lock_bh(&local->sta_bss_lock);
2427         freq = local->oper_channel->center_freq;
2428         list_for_each_entry(bss, &local->sta_bss_list, list) {
2429                 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2430                         continue;
2431
2432                 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2433                         IEEE80211_STA_AUTO_BSSID_SEL |
2434                         IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
2435                     (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2436                      !!sdata->default_key))
2437                         continue;
2438
2439                 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2440                     bss->freq != freq)
2441                         continue;
2442
2443                 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2444                     memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2445                         continue;
2446
2447                 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2448                     !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2449                         continue;
2450
2451                 if (!selected || top_rssi < bss->signal) {
2452                         selected = bss;
2453                         top_rssi = bss->signal;
2454                 }
2455         }
2456         if (selected)
2457                 atomic_inc(&selected->users);
2458         spin_unlock_bh(&local->sta_bss_lock);
2459
2460         if (selected) {
2461                 ieee80211_set_freq(sdata, selected->freq);
2462                 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2463                         ieee80211_sta_set_ssid(sdata, selected->ssid,
2464                                                selected->ssid_len);
2465                 ieee80211_sta_set_bssid(sdata, selected->bssid);
2466                 ieee80211_sta_def_wmm_params(sdata, selected);
2467
2468                 /* Send out direct probe if no probe resp was received or
2469                  * the one we have is outdated
2470                  */
2471                 if (!selected->last_probe_resp ||
2472                     time_after(jiffies, selected->last_probe_resp
2473                                         + IEEE80211_SCAN_RESULT_EXPIRE))
2474                         ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
2475                 else
2476                         ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2477
2478                 ieee80211_rx_bss_put(local, selected);
2479                 ieee80211_sta_reset_auth(sdata, ifsta);
2480                 return 0;
2481         } else {
2482                 if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
2483                         ifsta->assoc_scan_tries++;
2484                         if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
2485                                 ieee80211_sta_start_scan(sdata, NULL, 0);
2486                         else
2487                                 ieee80211_sta_start_scan(sdata, ifsta->ssid,
2488                                                          ifsta->ssid_len);
2489                         ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2490                         set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2491                 } else
2492                         ifsta->state = IEEE80211_STA_MLME_DISABLED;
2493         }
2494         return -1;
2495 }
2496
2497
2498 int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
2499 {
2500         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2501
2502         printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
2503                sdata->dev->name, reason);
2504
2505         if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2506             sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
2507                 return -EINVAL;
2508
2509         ieee80211_set_disassoc(sdata, ifsta, true, true, reason);
2510         return 0;
2511 }
2512
2513
2514 int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
2515 {
2516         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2517
2518         printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
2519                sdata->dev->name, reason);
2520
2521         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2522                 return -EINVAL;
2523
2524         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
2525                 return -1;
2526
2527         ieee80211_set_disassoc(sdata, ifsta, false, true, reason);
2528         return 0;
2529 }
2530
2531 void ieee80211_notify_mac(struct ieee80211_hw *hw,
2532                           enum ieee80211_notification_types  notif_type)
2533 {
2534         struct ieee80211_local *local = hw_to_local(hw);
2535         struct ieee80211_sub_if_data *sdata;
2536
2537         switch (notif_type) {
2538         case IEEE80211_NOTIFY_RE_ASSOC:
2539                 rcu_read_lock();
2540                 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2541                         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2542                                 continue;
2543
2544                         ieee80211_sta_req_auth(sdata, &sdata->u.sta);
2545                 }
2546                 rcu_read_unlock();
2547                 break;
2548         }
2549 }
2550 EXPORT_SYMBOL(ieee80211_notify_mac);
2551
2552 void ieee80211_sta_work(struct work_struct *work)
2553 {
2554         struct ieee80211_sub_if_data *sdata =
2555                 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2556         struct ieee80211_local *local = sdata->local;
2557         struct ieee80211_if_sta *ifsta;
2558         struct sk_buff *skb;
2559
2560         if (!netif_running(sdata->dev))
2561                 return;
2562
2563         if (local->sta_sw_scanning || local->sta_hw_scanning)
2564                 return;
2565
2566         if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2567                     sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2568                     sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
2569                 return;
2570         ifsta = &sdata->u.sta;
2571
2572         while ((skb = skb_dequeue(&ifsta->skb_queue)))
2573                 ieee80211_sta_rx_queued_mgmt(sdata, skb);
2574
2575 #ifdef CONFIG_MAC80211_MESH
2576         if (ifsta->preq_queue_len &&
2577             time_after(jiffies,
2578                        ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
2579                 mesh_path_start_discovery(sdata);
2580 #endif
2581
2582         if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
2583             ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
2584             ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
2585             test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
2586                 ieee80211_sta_start_scan(sdata, ifsta->scan_ssid, ifsta->scan_ssid_len);
2587                 return;
2588         }
2589
2590         if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2591                 if (ieee80211_sta_config_auth(sdata, ifsta))
2592                         return;
2593                 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2594         } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2595                 return;
2596
2597         switch (ifsta->state) {
2598         case IEEE80211_STA_MLME_DISABLED:
2599                 break;
2600         case IEEE80211_STA_MLME_DIRECT_PROBE:
2601                 ieee80211_direct_probe(sdata, ifsta);
2602                 break;
2603         case IEEE80211_STA_MLME_AUTHENTICATE:
2604                 ieee80211_authenticate(sdata, ifsta);
2605                 break;
2606         case IEEE80211_STA_MLME_ASSOCIATE:
2607                 ieee80211_associate(sdata, ifsta);
2608                 break;
2609         case IEEE80211_STA_MLME_ASSOCIATED:
2610                 ieee80211_associated(sdata, ifsta);
2611                 break;
2612         case IEEE80211_STA_MLME_IBSS_SEARCH:
2613                 ieee80211_sta_find_ibss(sdata, ifsta);
2614                 break;
2615         case IEEE80211_STA_MLME_IBSS_JOINED:
2616                 ieee80211_sta_merge_ibss(sdata, ifsta);
2617                 break;
2618 #ifdef CONFIG_MAC80211_MESH
2619         case IEEE80211_STA_MLME_MESH_UP:
2620                 ieee80211_mesh_housekeeping(sdata, ifsta);
2621                 break;
2622 #endif
2623         default:
2624                 WARN_ON(1);
2625                 break;
2626         }
2627
2628         if (ieee80211_privacy_mismatch(sdata, ifsta)) {
2629                 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2630                        "mixed-cell disabled - disassociate\n", sdata->dev->name);
2631
2632                 ieee80211_set_disassoc(sdata, ifsta, false, true,
2633                                         WLAN_REASON_UNSPECIFIED);
2634         }
2635 }
2636
2637 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2638 {
2639         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2640         struct ieee80211_if_sta *ifsta;
2641
2642         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
2643                 ifsta = &sdata->u.sta;
2644                 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
2645                     (!(ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED) &&
2646                     !ieee80211_sta_active_ibss(sdata)))
2647                         ieee80211_sta_find_ibss(sdata, ifsta);
2648         }
2649 }