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