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