]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/mac80211/mlme.c
mac80211: initialise queue QoS parameters at hw start
[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_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
449                                         u8 dialog_token, u16 status, u16 policy,
450                                         u16 buf_size, u16 timeout)
451 {
452         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
453         struct ieee80211_local *local = sdata->local;
454         struct sk_buff *skb;
455         struct ieee80211_mgmt *mgmt;
456         u16 capab;
457
458         skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
459
460         if (!skb) {
461                 printk(KERN_DEBUG "%s: failed to allocate buffer "
462                        "for addba resp frame\n", sdata->dev->name);
463                 return;
464         }
465
466         skb_reserve(skb, local->hw.extra_tx_headroom);
467         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
468         memset(mgmt, 0, 24);
469         memcpy(mgmt->da, da, ETH_ALEN);
470         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
471         if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
472                 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
473         else
474                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
475         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
476                                           IEEE80211_STYPE_ACTION);
477
478         skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
479         mgmt->u.action.category = WLAN_CATEGORY_BACK;
480         mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
481         mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
482
483         capab = (u16)(policy << 1);     /* bit 1 aggregation policy */
484         capab |= (u16)(tid << 2);       /* bit 5:2 TID number */
485         capab |= (u16)(buf_size << 6);  /* bit 15:6 max size of aggregation */
486
487         mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
488         mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
489         mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
490
491         ieee80211_sta_tx(sdata, skb, 0);
492 }
493
494 static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata,
495                                         struct ieee80211_msrment_ie *request_ie,
496                                         const u8 *da, const u8 *bssid,
497                                         u8 dialog_token)
498 {
499         struct ieee80211_local *local = sdata->local;
500         struct sk_buff *skb;
501         struct ieee80211_mgmt *msr_report;
502
503         skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
504                                 sizeof(struct ieee80211_msrment_ie));
505
506         if (!skb) {
507                 printk(KERN_ERR "%s: failed to allocate buffer for "
508                                 "measurement report frame\n", sdata->dev->name);
509                 return;
510         }
511
512         skb_reserve(skb, local->hw.extra_tx_headroom);
513         msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
514         memset(msr_report, 0, 24);
515         memcpy(msr_report->da, da, ETH_ALEN);
516         memcpy(msr_report->sa, sdata->dev->dev_addr, ETH_ALEN);
517         memcpy(msr_report->bssid, bssid, ETH_ALEN);
518         msr_report->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
519                                                 IEEE80211_STYPE_ACTION);
520
521         skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
522         msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
523         msr_report->u.action.u.measurement.action_code =
524                                 WLAN_ACTION_SPCT_MSR_RPRT;
525         msr_report->u.action.u.measurement.dialog_token = dialog_token;
526
527         msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
528         msr_report->u.action.u.measurement.length =
529                         sizeof(struct ieee80211_msrment_ie);
530
531         memset(&msr_report->u.action.u.measurement.msr_elem, 0,
532                 sizeof(struct ieee80211_msrment_ie));
533         msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
534         msr_report->u.action.u.measurement.msr_elem.mode |=
535                         IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
536         msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
537
538         ieee80211_sta_tx(sdata, skb, 0);
539 }
540
541 /* MLME */
542 static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
543                                          struct ieee80211_sta_bss *bss)
544 {
545         struct ieee80211_local *local = sdata->local;
546         int i, have_higher_than_11mbit = 0;
547
548         /* cf. IEEE 802.11 9.2.12 */
549         for (i = 0; i < bss->supp_rates_len; i++)
550                 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
551                         have_higher_than_11mbit = 1;
552
553         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
554             have_higher_than_11mbit)
555                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
556         else
557                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
558
559         ieee80211_set_wmm_default(sdata);
560 }
561
562 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
563                                      struct ieee80211_if_sta *ifsta,
564                                      u8 *wmm_param, size_t wmm_param_len)
565 {
566         struct ieee80211_tx_queue_params params;
567         size_t left;
568         int count;
569         u8 *pos;
570
571         if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
572                 return;
573
574         if (!wmm_param)
575                 return;
576
577         if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
578                 return;
579         count = wmm_param[6] & 0x0f;
580         if (count == ifsta->wmm_last_param_set)
581                 return;
582         ifsta->wmm_last_param_set = count;
583
584         pos = wmm_param + 8;
585         left = wmm_param_len - 8;
586
587         memset(&params, 0, sizeof(params));
588
589         if (!local->ops->conf_tx)
590                 return;
591
592         local->wmm_acm = 0;
593         for (; left >= 4; left -= 4, pos += 4) {
594                 int aci = (pos[0] >> 5) & 0x03;
595                 int acm = (pos[0] >> 4) & 0x01;
596                 int queue;
597
598                 switch (aci) {
599                 case 1:
600                         queue = 3;
601                         if (acm)
602                                 local->wmm_acm |= BIT(0) | BIT(3);
603                         break;
604                 case 2:
605                         queue = 1;
606                         if (acm)
607                                 local->wmm_acm |= BIT(4) | BIT(5);
608                         break;
609                 case 3:
610                         queue = 0;
611                         if (acm)
612                                 local->wmm_acm |= BIT(6) | BIT(7);
613                         break;
614                 case 0:
615                 default:
616                         queue = 2;
617                         if (acm)
618                                 local->wmm_acm |= BIT(1) | BIT(2);
619                         break;
620                 }
621
622                 params.aifs = pos[0] & 0x0f;
623                 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
624                 params.cw_min = ecw2cw(pos[1] & 0x0f);
625                 params.txop = get_unaligned_le16(pos + 2);
626 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
627                 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
628                        "cWmin=%d cWmax=%d txop=%d\n",
629                        local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
630                        params.cw_max, params.txop);
631 #endif
632                 /* TODO: handle ACM (block TX, fallback to next lowest allowed
633                  * AC for now) */
634                 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
635                         printk(KERN_DEBUG "%s: failed to set TX queue "
636                                "parameters for queue %d\n", local->mdev->name, queue);
637                 }
638         }
639 }
640
641 static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
642                                            bool use_protection,
643                                            bool use_short_preamble)
644 {
645         struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
646 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
647         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
648         DECLARE_MAC_BUF(mac);
649 #endif
650         u32 changed = 0;
651
652         if (use_protection != bss_conf->use_cts_prot) {
653 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
654                 if (net_ratelimit()) {
655                         printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
656                                "%s)\n",
657                                sdata->dev->name,
658                                use_protection ? "enabled" : "disabled",
659                                print_mac(mac, ifsta->bssid));
660                 }
661 #endif
662                 bss_conf->use_cts_prot = use_protection;
663                 changed |= BSS_CHANGED_ERP_CTS_PROT;
664         }
665
666         if (use_short_preamble != bss_conf->use_short_preamble) {
667 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
668                 if (net_ratelimit()) {
669                         printk(KERN_DEBUG "%s: switched to %s barker preamble"
670                                " (BSSID=%s)\n",
671                                sdata->dev->name,
672                                use_short_preamble ? "short" : "long",
673                                print_mac(mac, ifsta->bssid));
674                 }
675 #endif
676                 bss_conf->use_short_preamble = use_short_preamble;
677                 changed |= BSS_CHANGED_ERP_PREAMBLE;
678         }
679
680         return changed;
681 }
682
683 static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
684                                    u8 erp_value)
685 {
686         bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
687         bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
688
689         return ieee80211_handle_protect_preamb(sdata,
690                         use_protection, use_short_preamble);
691 }
692
693 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
694                                            struct ieee80211_sta_bss *bss)
695 {
696         u32 changed = 0;
697
698         if (bss->has_erp_value)
699                 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
700         else {
701                 u16 capab = bss->capability;
702                 changed |= ieee80211_handle_protect_preamb(sdata, false,
703                                 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
704         }
705
706         return changed;
707 }
708
709 static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata,
710                                         struct ieee80211_if_sta *ifsta)
711 {
712         union iwreq_data wrqu;
713         memset(&wrqu, 0, sizeof(wrqu));
714         if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
715                 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
716         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
717         wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
718 }
719
720 static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
721                                          struct ieee80211_if_sta *ifsta)
722 {
723         union iwreq_data wrqu;
724
725         if (ifsta->assocreq_ies) {
726                 memset(&wrqu, 0, sizeof(wrqu));
727                 wrqu.data.length = ifsta->assocreq_ies_len;
728                 wireless_send_event(sdata->dev, IWEVASSOCREQIE, &wrqu,
729                                     ifsta->assocreq_ies);
730         }
731         if (ifsta->assocresp_ies) {
732                 memset(&wrqu, 0, sizeof(wrqu));
733                 wrqu.data.length = ifsta->assocresp_ies_len;
734                 wireless_send_event(sdata->dev, IWEVASSOCRESPIE, &wrqu,
735                                     ifsta->assocresp_ies);
736         }
737 }
738
739
740 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
741                                      struct ieee80211_if_sta *ifsta)
742 {
743         struct ieee80211_local *local = sdata->local;
744         struct ieee80211_conf *conf = &local_to_hw(local)->conf;
745         u32 changed = BSS_CHANGED_ASSOC;
746
747         struct ieee80211_sta_bss *bss;
748
749         ifsta->flags |= IEEE80211_STA_ASSOCIATED;
750
751         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
752                 return;
753
754         bss = ieee80211_rx_bss_get(local, ifsta->bssid,
755                                    conf->channel->center_freq,
756                                    ifsta->ssid, ifsta->ssid_len);
757         if (bss) {
758                 /* set timing information */
759                 sdata->bss_conf.beacon_int = bss->beacon_int;
760                 sdata->bss_conf.timestamp = bss->timestamp;
761                 sdata->bss_conf.dtim_period = bss->dtim_period;
762
763                 changed |= ieee80211_handle_bss_capability(sdata, bss);
764
765                 ieee80211_rx_bss_put(local, bss);
766         }
767
768         if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
769                 changed |= BSS_CHANGED_HT;
770                 sdata->bss_conf.assoc_ht = 1;
771                 sdata->bss_conf.ht_conf = &conf->ht_conf;
772                 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
773         }
774
775         ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
776         memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
777         ieee80211_sta_send_associnfo(sdata, ifsta);
778
779         ifsta->last_probe = jiffies;
780         ieee80211_led_assoc(local, 1);
781
782         sdata->bss_conf.assoc = 1;
783         ieee80211_bss_info_change_notify(sdata, changed);
784
785         netif_tx_start_all_queues(sdata->dev);
786         netif_carrier_on(sdata->dev);
787
788         ieee80211_sta_send_apinfo(sdata, ifsta);
789 }
790
791 static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
792                                    struct ieee80211_if_sta *ifsta)
793 {
794         DECLARE_MAC_BUF(mac);
795
796         ifsta->direct_probe_tries++;
797         if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
798                 printk(KERN_DEBUG "%s: direct probe to AP %s timed out\n",
799                        sdata->dev->name, print_mac(mac, ifsta->bssid));
800                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
801                 return;
802         }
803
804         printk(KERN_DEBUG "%s: direct probe to AP %s try %d\n",
805                         sdata->dev->name, print_mac(mac, ifsta->bssid),
806                         ifsta->direct_probe_tries);
807
808         ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
809
810         set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
811
812         /* Direct probe is sent to broadcast address as some APs
813          * will not answer to direct packet in unassociated state.
814          */
815         ieee80211_send_probe_req(sdata, NULL,
816                                  ifsta->ssid, ifsta->ssid_len);
817
818         mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
819 }
820
821
822 static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
823                                    struct ieee80211_if_sta *ifsta)
824 {
825         DECLARE_MAC_BUF(mac);
826
827         ifsta->auth_tries++;
828         if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
829                 printk(KERN_DEBUG "%s: authentication with AP %s"
830                        " timed out\n",
831                        sdata->dev->name, print_mac(mac, ifsta->bssid));
832                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
833                 return;
834         }
835
836         ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
837         printk(KERN_DEBUG "%s: authenticate with AP %s\n",
838                sdata->dev->name, print_mac(mac, ifsta->bssid));
839
840         ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
841
842         mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
843 }
844
845 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
846                                    struct ieee80211_if_sta *ifsta, bool deauth,
847                                    bool self_disconnected, u16 reason)
848 {
849         struct ieee80211_local *local = sdata->local;
850         struct sta_info *sta;
851         u32 changed = BSS_CHANGED_ASSOC;
852
853         rcu_read_lock();
854
855         sta = sta_info_get(local, ifsta->bssid);
856         if (!sta) {
857                 rcu_read_unlock();
858                 return;
859         }
860
861         if (deauth) {
862                 ifsta->direct_probe_tries = 0;
863                 ifsta->auth_tries = 0;
864         }
865         ifsta->assoc_scan_tries = 0;
866         ifsta->assoc_tries = 0;
867
868         netif_tx_stop_all_queues(sdata->dev);
869         netif_carrier_off(sdata->dev);
870
871         ieee80211_sta_tear_down_BA_sessions(sdata, sta->addr);
872
873         if (self_disconnected) {
874                 if (deauth)
875                         ieee80211_send_deauth_disassoc(sdata,
876                                 IEEE80211_STYPE_DEAUTH, reason);
877                 else
878                         ieee80211_send_deauth_disassoc(sdata,
879                                 IEEE80211_STYPE_DISASSOC, reason);
880         }
881
882         ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
883         changed |= ieee80211_reset_erp_info(sdata);
884
885         if (sdata->bss_conf.assoc_ht)
886                 changed |= BSS_CHANGED_HT;
887
888         sdata->bss_conf.assoc_ht = 0;
889         sdata->bss_conf.ht_conf = NULL;
890         sdata->bss_conf.ht_bss_conf = NULL;
891
892         ieee80211_led_assoc(local, 0);
893         sdata->bss_conf.assoc = 0;
894
895         ieee80211_sta_send_apinfo(sdata, ifsta);
896
897         if (self_disconnected)
898                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
899
900         sta_info_unlink(&sta);
901
902         rcu_read_unlock();
903
904         sta_info_destroy(sta);
905 }
906
907 static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
908 {
909         if (!sdata || !sdata->default_key ||
910             sdata->default_key->conf.alg != ALG_WEP)
911                 return 0;
912         return 1;
913 }
914
915 static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
916                                       struct ieee80211_if_sta *ifsta)
917 {
918         struct ieee80211_local *local = sdata->local;
919         struct ieee80211_sta_bss *bss;
920         int bss_privacy;
921         int wep_privacy;
922         int privacy_invoked;
923
924         if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
925                 return 0;
926
927         bss = ieee80211_rx_bss_get(local, ifsta->bssid,
928                                    local->hw.conf.channel->center_freq,
929                                    ifsta->ssid, ifsta->ssid_len);
930         if (!bss)
931                 return 0;
932
933         bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
934         wep_privacy = !!ieee80211_sta_wep_configured(sdata);
935         privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
936
937         ieee80211_rx_bss_put(local, bss);
938
939         if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
940                 return 0;
941
942         return 1;
943 }
944
945 static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
946                                 struct ieee80211_if_sta *ifsta)
947 {
948         DECLARE_MAC_BUF(mac);
949
950         ifsta->assoc_tries++;
951         if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
952                 printk(KERN_DEBUG "%s: association with AP %s"
953                        " timed out\n",
954                        sdata->dev->name, print_mac(mac, ifsta->bssid));
955                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
956                 return;
957         }
958
959         ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
960         printk(KERN_DEBUG "%s: associate with AP %s\n",
961                sdata->dev->name, print_mac(mac, ifsta->bssid));
962         if (ieee80211_privacy_mismatch(sdata, ifsta)) {
963                 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
964                        "mixed-cell disabled - abort association\n", sdata->dev->name);
965                 ifsta->state = IEEE80211_STA_MLME_DISABLED;
966                 return;
967         }
968
969         ieee80211_send_assoc(sdata, ifsta);
970
971         mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
972 }
973
974
975 static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
976                                  struct ieee80211_if_sta *ifsta)
977 {
978         struct ieee80211_local *local = sdata->local;
979         struct sta_info *sta;
980         int disassoc;
981         DECLARE_MAC_BUF(mac);
982
983         /* TODO: start monitoring current AP signal quality and number of
984          * missed beacons. Scan other channels every now and then and search
985          * for better APs. */
986         /* TODO: remove expired BSSes */
987
988         ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
989
990         rcu_read_lock();
991
992         sta = sta_info_get(local, ifsta->bssid);
993         if (!sta) {
994                 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
995                        sdata->dev->name, print_mac(mac, ifsta->bssid));
996                 disassoc = 1;
997         } else {
998                 disassoc = 0;
999                 if (time_after(jiffies,
1000                                sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
1001                         if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
1002                                 printk(KERN_DEBUG "%s: No ProbeResp from "
1003                                        "current AP %s - assume out of "
1004                                        "range\n",
1005                                        sdata->dev->name, print_mac(mac, ifsta->bssid));
1006                                 disassoc = 1;
1007                         } else
1008                                 ieee80211_send_probe_req(sdata, ifsta->bssid,
1009                                                          local->scan_ssid,
1010                                                          local->scan_ssid_len);
1011                         ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
1012                 } else {
1013                         ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
1014                         if (time_after(jiffies, ifsta->last_probe +
1015                                        IEEE80211_PROBE_INTERVAL)) {
1016                                 ifsta->last_probe = jiffies;
1017                                 ieee80211_send_probe_req(sdata, ifsta->bssid,
1018                                                          ifsta->ssid,
1019                                                          ifsta->ssid_len);
1020                         }
1021                 }
1022         }
1023
1024         rcu_read_unlock();
1025
1026         if (disassoc)
1027                 ieee80211_set_disassoc(sdata, ifsta, true, true,
1028                                         WLAN_REASON_PREV_AUTH_NOT_VALID);
1029         else
1030                 mod_timer(&ifsta->timer, jiffies +
1031                                       IEEE80211_MONITORING_INTERVAL);
1032 }
1033
1034
1035 static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
1036                                      struct ieee80211_if_sta *ifsta)
1037 {
1038         printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
1039         ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
1040         ieee80211_associate(sdata, ifsta);
1041 }
1042
1043
1044 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1045                                      struct ieee80211_if_sta *ifsta,
1046                                      struct ieee80211_mgmt *mgmt,
1047                                      size_t len)
1048 {
1049         u8 *pos;
1050         struct ieee802_11_elems elems;
1051
1052         pos = mgmt->u.auth.variable;
1053         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1054         if (!elems.challenge)
1055                 return;
1056         ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
1057                             elems.challenge_len + 2, 1);
1058 }
1059
1060 /*
1061  * After accepting the AddBA Request we activated a timer,
1062  * resetting it after each frame that arrives from the originator.
1063  * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1064  */
1065 static void sta_rx_agg_session_timer_expired(unsigned long data)
1066 {
1067         /* not an elegant detour, but there is no choice as the timer passes
1068          * only one argument, and various sta_info are needed here, so init
1069          * flow in sta_info_create gives the TID as data, while the timer_to_id
1070          * array gives the sta through container_of */
1071         u8 *ptid = (u8 *)data;
1072         u8 *timer_to_id = ptid - *ptid;
1073         struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1074                                          timer_to_tid[0]);
1075
1076 #ifdef CONFIG_MAC80211_HT_DEBUG
1077         printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1078 #endif
1079         ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->addr,
1080                                          (u16)*ptid, WLAN_BACK_TIMER,
1081                                          WLAN_REASON_QSTA_TIMEOUT);
1082 }
1083
1084 static void ieee80211_sta_process_addba_request(struct ieee80211_local *local,
1085                                                 struct ieee80211_mgmt *mgmt,
1086                                                 size_t len)
1087 {
1088         struct ieee80211_hw *hw = &local->hw;
1089         struct ieee80211_conf *conf = &hw->conf;
1090         struct sta_info *sta;
1091         struct tid_ampdu_rx *tid_agg_rx;
1092         u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
1093         u8 dialog_token;
1094         int ret = -EOPNOTSUPP;
1095         DECLARE_MAC_BUF(mac);
1096
1097         rcu_read_lock();
1098
1099         sta = sta_info_get(local, mgmt->sa);
1100         if (!sta) {
1101                 rcu_read_unlock();
1102                 return;
1103         }
1104
1105         /* extract session parameters from addba request frame */
1106         dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1107         timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
1108         start_seq_num =
1109                 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
1110
1111         capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1112         ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1113         tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1114         buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1115
1116         status = WLAN_STATUS_REQUEST_DECLINED;
1117
1118         /* sanity check for incoming parameters:
1119          * check if configuration can support the BA policy
1120          * and if buffer size does not exceeds max value */
1121         if (((ba_policy != 1)
1122                 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1123                 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1124                 status = WLAN_STATUS_INVALID_QOS_PARAM;
1125 #ifdef CONFIG_MAC80211_HT_DEBUG
1126                 if (net_ratelimit())
1127                         printk(KERN_DEBUG "AddBA Req with bad params from "
1128                                 "%s on tid %u. policy %d, buffer size %d\n",
1129                                 print_mac(mac, mgmt->sa), tid, ba_policy,
1130                                 buf_size);
1131 #endif /* CONFIG_MAC80211_HT_DEBUG */
1132                 goto end_no_lock;
1133         }
1134         /* determine default buffer size */
1135         if (buf_size == 0) {
1136                 struct ieee80211_supported_band *sband;
1137
1138                 sband = local->hw.wiphy->bands[conf->channel->band];
1139                 buf_size = IEEE80211_MIN_AMPDU_BUF;
1140                 buf_size = buf_size << sband->ht_info.ampdu_factor;
1141         }
1142
1143
1144         /* examine state machine */
1145         spin_lock_bh(&sta->lock);
1146
1147         if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
1148 #ifdef CONFIG_MAC80211_HT_DEBUG
1149                 if (net_ratelimit())
1150                         printk(KERN_DEBUG "unexpected AddBA Req from "
1151                                 "%s on tid %u\n",
1152                                 print_mac(mac, mgmt->sa), tid);
1153 #endif /* CONFIG_MAC80211_HT_DEBUG */
1154                 goto end;
1155         }
1156
1157         /* prepare A-MPDU MLME for Rx aggregation */
1158         sta->ampdu_mlme.tid_rx[tid] =
1159                         kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1160         if (!sta->ampdu_mlme.tid_rx[tid]) {
1161 #ifdef CONFIG_MAC80211_HT_DEBUG
1162                 if (net_ratelimit())
1163                         printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1164                                         tid);
1165 #endif
1166                 goto end;
1167         }
1168         /* rx timer */
1169         sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1170                                 sta_rx_agg_session_timer_expired;
1171         sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1172                                 (unsigned long)&sta->timer_to_tid[tid];
1173         init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1174
1175         tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1176
1177         /* prepare reordering buffer */
1178         tid_agg_rx->reorder_buf =
1179                 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
1180         if (!tid_agg_rx->reorder_buf) {
1181 #ifdef CONFIG_MAC80211_HT_DEBUG
1182                 if (net_ratelimit())
1183                         printk(KERN_ERR "can not allocate reordering buffer "
1184                                "to tid %d\n", tid);
1185 #endif
1186                 kfree(sta->ampdu_mlme.tid_rx[tid]);
1187                 goto end;
1188         }
1189         memset(tid_agg_rx->reorder_buf, 0,
1190                 buf_size * sizeof(struct sk_buff *));
1191
1192         if (local->ops->ampdu_action)
1193                 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
1194                                                sta->addr, tid, &start_seq_num);
1195 #ifdef CONFIG_MAC80211_HT_DEBUG
1196         printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
1197 #endif /* CONFIG_MAC80211_HT_DEBUG */
1198
1199         if (ret) {
1200                 kfree(tid_agg_rx->reorder_buf);
1201                 kfree(tid_agg_rx);
1202                 sta->ampdu_mlme.tid_rx[tid] = NULL;
1203                 goto end;
1204         }
1205
1206         /* change state and send addba resp */
1207         sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
1208         tid_agg_rx->dialog_token = dialog_token;
1209         tid_agg_rx->ssn = start_seq_num;
1210         tid_agg_rx->head_seq_num = start_seq_num;
1211         tid_agg_rx->buf_size = buf_size;
1212         tid_agg_rx->timeout = timeout;
1213         tid_agg_rx->stored_mpdu_num = 0;
1214         status = WLAN_STATUS_SUCCESS;
1215 end:
1216         spin_unlock_bh(&sta->lock);
1217
1218 end_no_lock:
1219         ieee80211_send_addba_resp(sta->sdata, sta->addr, tid,
1220                                   dialog_token, status, 1, buf_size, timeout);
1221         rcu_read_unlock();
1222 }
1223
1224 static void ieee80211_sta_process_addba_resp(struct ieee80211_local *local,
1225                                              struct ieee80211_mgmt *mgmt,
1226                                              size_t len)
1227 {
1228         struct ieee80211_hw *hw = &local->hw;
1229         struct sta_info *sta;
1230         u16 capab;
1231         u16 tid;
1232         u8 *state;
1233
1234         rcu_read_lock();
1235
1236         sta = sta_info_get(local, mgmt->sa);
1237         if (!sta) {
1238                 rcu_read_unlock();
1239                 return;
1240         }
1241
1242         capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1243         tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1244
1245         state = &sta->ampdu_mlme.tid_state_tx[tid];
1246
1247         spin_lock_bh(&sta->lock);
1248
1249         if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1250                 spin_unlock_bh(&sta->lock);
1251                 goto addba_resp_exit;
1252         }
1253
1254         if (mgmt->u.action.u.addba_resp.dialog_token !=
1255                 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1256                 spin_unlock_bh(&sta->lock);
1257 #ifdef CONFIG_MAC80211_HT_DEBUG
1258                 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1259 #endif /* CONFIG_MAC80211_HT_DEBUG */
1260                 goto addba_resp_exit;
1261         }
1262
1263         del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1264 #ifdef CONFIG_MAC80211_HT_DEBUG
1265         printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1266 #endif /* CONFIG_MAC80211_HT_DEBUG */
1267         if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1268                         == WLAN_STATUS_SUCCESS) {
1269                 *state |= HT_ADDBA_RECEIVED_MSK;
1270                 sta->ampdu_mlme.addba_req_num[tid] = 0;
1271
1272                 if (*state == HT_AGG_STATE_OPERATIONAL)
1273                         ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1274
1275                 spin_unlock_bh(&sta->lock);
1276         } else {
1277                 sta->ampdu_mlme.addba_req_num[tid]++;
1278                 /* this will allow the state check in stop_BA_session */
1279                 *state = HT_AGG_STATE_OPERATIONAL;
1280                 spin_unlock_bh(&sta->lock);
1281                 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1282                                              WLAN_BACK_INITIATOR);
1283         }
1284
1285 addba_resp_exit:
1286         rcu_read_unlock();
1287 }
1288
1289 static void ieee80211_sta_process_delba(struct ieee80211_sub_if_data *sdata,
1290                         struct ieee80211_mgmt *mgmt, size_t len)
1291 {
1292         struct ieee80211_local *local = sdata->local;
1293         struct sta_info *sta;
1294         u16 tid, params;
1295         u16 initiator;
1296         DECLARE_MAC_BUF(mac);
1297
1298         rcu_read_lock();
1299
1300         sta = sta_info_get(local, mgmt->sa);
1301         if (!sta) {
1302                 rcu_read_unlock();
1303                 return;
1304         }
1305
1306         params = le16_to_cpu(mgmt->u.action.u.delba.params);
1307         tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1308         initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1309
1310 #ifdef CONFIG_MAC80211_HT_DEBUG
1311         if (net_ratelimit())
1312                 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1313                         print_mac(mac, mgmt->sa),
1314                         initiator ? "initiator" : "recipient", tid,
1315                         mgmt->u.action.u.delba.reason_code);
1316 #endif /* CONFIG_MAC80211_HT_DEBUG */
1317
1318         if (initiator == WLAN_BACK_INITIATOR)
1319                 ieee80211_sta_stop_rx_ba_session(sdata, sta->addr, tid,
1320                                                  WLAN_BACK_INITIATOR, 0);
1321         else { /* WLAN_BACK_RECIPIENT */
1322                 spin_lock_bh(&sta->lock);
1323                 sta->ampdu_mlme.tid_state_tx[tid] =
1324                                 HT_AGG_STATE_OPERATIONAL;
1325                 spin_unlock_bh(&sta->lock);
1326                 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1327                                              WLAN_BACK_RECIPIENT);
1328         }
1329         rcu_read_unlock();
1330 }
1331
1332 static void ieee80211_sta_process_measurement_req(struct ieee80211_sub_if_data *sdata,
1333                                                 struct ieee80211_mgmt *mgmt,
1334                                                 size_t len)
1335 {
1336         /*
1337          * Ignoring measurement request is spec violation.
1338          * Mandatory measurements must be reported optional
1339          * measurements might be refused or reported incapable
1340          * For now just refuse
1341          * TODO: Answer basic measurement as unmeasured
1342          */
1343         ieee80211_send_refuse_measurement_request(sdata,
1344                         &mgmt->u.action.u.measurement.msr_elem,
1345                         mgmt->sa, mgmt->bssid,
1346                         mgmt->u.action.u.measurement.dialog_token);
1347 }
1348
1349
1350 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1351                                    struct ieee80211_if_sta *ifsta,
1352                                    struct ieee80211_mgmt *mgmt,
1353                                    size_t len)
1354 {
1355         u16 auth_alg, auth_transaction, status_code;
1356         DECLARE_MAC_BUF(mac);
1357
1358         if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
1359             sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
1360                 return;
1361
1362         if (len < 24 + 6)
1363                 return;
1364
1365         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1366             memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
1367                 return;
1368
1369         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1370             memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1371                 return;
1372
1373         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1374         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1375         status_code = le16_to_cpu(mgmt->u.auth.status_code);
1376
1377         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
1378                 /*
1379                  * IEEE 802.11 standard does not require authentication in IBSS
1380                  * networks and most implementations do not seem to use it.
1381                  * However, try to reply to authentication attempts if someone
1382                  * has actually implemented this.
1383                  */
1384                 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
1385                         return;
1386                 ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
1387         }
1388
1389         if (auth_alg != ifsta->auth_alg ||
1390             auth_transaction != ifsta->auth_transaction)
1391                 return;
1392
1393         if (status_code != WLAN_STATUS_SUCCESS) {
1394                 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1395                         u8 algs[3];
1396                         const int num_algs = ARRAY_SIZE(algs);
1397                         int i, pos;
1398                         algs[0] = algs[1] = algs[2] = 0xff;
1399                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1400                                 algs[0] = WLAN_AUTH_OPEN;
1401                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1402                                 algs[1] = WLAN_AUTH_SHARED_KEY;
1403                         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1404                                 algs[2] = WLAN_AUTH_LEAP;
1405                         if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1406                                 pos = 0;
1407                         else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1408                                 pos = 1;
1409                         else
1410                                 pos = 2;
1411                         for (i = 0; i < num_algs; i++) {
1412                                 pos++;
1413                                 if (pos >= num_algs)
1414                                         pos = 0;
1415                                 if (algs[pos] == ifsta->auth_alg ||
1416                                     algs[pos] == 0xff)
1417                                         continue;
1418                                 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1419                                     !ieee80211_sta_wep_configured(sdata))
1420                                         continue;
1421                                 ifsta->auth_alg = algs[pos];
1422                                 break;
1423                         }
1424                 }
1425                 return;
1426         }
1427
1428         switch (ifsta->auth_alg) {
1429         case WLAN_AUTH_OPEN:
1430         case WLAN_AUTH_LEAP:
1431                 ieee80211_auth_completed(sdata, ifsta);
1432                 break;
1433         case WLAN_AUTH_SHARED_KEY:
1434                 if (ifsta->auth_transaction == 4)
1435                         ieee80211_auth_completed(sdata, ifsta);
1436                 else
1437                         ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
1438                 break;
1439         }
1440 }
1441
1442
1443 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1444                                      struct ieee80211_if_sta *ifsta,
1445                                      struct ieee80211_mgmt *mgmt,
1446                                      size_t len)
1447 {
1448         u16 reason_code;
1449         DECLARE_MAC_BUF(mac);
1450
1451         if (len < 24 + 2)
1452                 return;
1453
1454         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1455                 return;
1456
1457         reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1458
1459         if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
1460                 printk(KERN_DEBUG "%s: deauthenticated\n", sdata->dev->name);
1461
1462         if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
1463             ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
1464             ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1465                 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
1466                 mod_timer(&ifsta->timer, jiffies +
1467                                       IEEE80211_RETRY_AUTH_INTERVAL);
1468         }
1469
1470         ieee80211_set_disassoc(sdata, ifsta, true, false, 0);
1471         ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
1472 }
1473
1474
1475 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1476                                        struct ieee80211_if_sta *ifsta,
1477                                        struct ieee80211_mgmt *mgmt,
1478                                        size_t len)
1479 {
1480         u16 reason_code;
1481         DECLARE_MAC_BUF(mac);
1482
1483         if (len < 24 + 2)
1484                 return;
1485
1486         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
1487                 return;
1488
1489         reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1490
1491         if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
1492                 printk(KERN_DEBUG "%s: disassociated\n", sdata->dev->name);
1493
1494         if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1495                 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
1496                 mod_timer(&ifsta->timer, jiffies +
1497                                       IEEE80211_RETRY_AUTH_INTERVAL);
1498         }
1499
1500         ieee80211_set_disassoc(sdata, ifsta, false, false, 0);
1501 }
1502
1503
1504 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1505                                          struct ieee80211_if_sta *ifsta,
1506                                          struct ieee80211_mgmt *mgmt,
1507                                          size_t len,
1508                                          int reassoc)
1509 {
1510         struct ieee80211_local *local = sdata->local;
1511         struct ieee80211_supported_band *sband;
1512         struct sta_info *sta;
1513         u64 rates, basic_rates;
1514         u16 capab_info, status_code, aid;
1515         struct ieee802_11_elems elems;
1516         struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
1517         u8 *pos;
1518         int i, j;
1519         DECLARE_MAC_BUF(mac);
1520         bool have_higher_than_11mbit = false;
1521
1522         /* AssocResp and ReassocResp have identical structure, so process both
1523          * of them in this function. */
1524
1525         if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
1526                 return;
1527
1528         if (len < 24 + 6)
1529                 return;
1530
1531         if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
1532                 return;
1533
1534         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1535         status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1536         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1537
1538         printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
1539                "status=%d aid=%d)\n",
1540                sdata->dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
1541                capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1542
1543         if (status_code != WLAN_STATUS_SUCCESS) {
1544                 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1545                        sdata->dev->name, status_code);
1546                 /* if this was a reassociation, ensure we try a "full"
1547                  * association next time. This works around some broken APs
1548                  * which do not correctly reject reassociation requests. */
1549                 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1550                 return;
1551         }
1552
1553         if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1554                 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1555                        "set\n", sdata->dev->name, aid);
1556         aid &= ~(BIT(15) | BIT(14));
1557
1558         pos = mgmt->u.assoc_resp.variable;
1559         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1560
1561         if (!elems.supp_rates) {
1562                 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1563                        sdata->dev->name);
1564                 return;
1565         }
1566
1567         printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
1568         ifsta->aid = aid;
1569         ifsta->ap_capab = capab_info;
1570
1571         kfree(ifsta->assocresp_ies);
1572         ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1573         ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
1574         if (ifsta->assocresp_ies)
1575                 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1576
1577         rcu_read_lock();
1578
1579         /* Add STA entry for the AP */
1580         sta = sta_info_get(local, ifsta->bssid);
1581         if (!sta) {
1582                 struct ieee80211_sta_bss *bss;
1583                 int err;
1584
1585                 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1586                 if (!sta) {
1587                         printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1588                                " the AP\n", sdata->dev->name);
1589                         rcu_read_unlock();
1590                         return;
1591                 }
1592                 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
1593                                            local->hw.conf.channel->center_freq,
1594                                            ifsta->ssid, ifsta->ssid_len);
1595                 if (bss) {
1596                         sta->last_signal = bss->signal;
1597                         sta->last_qual = bss->qual;
1598                         sta->last_noise = bss->noise;
1599                         ieee80211_rx_bss_put(local, bss);
1600                 }
1601
1602                 err = sta_info_insert(sta);
1603                 if (err) {
1604                         printk(KERN_DEBUG "%s: failed to insert STA entry for"
1605                                " the AP (error %d)\n", sdata->dev->name, err);
1606                         rcu_read_unlock();
1607                         return;
1608                 }
1609                 /* update new sta with its last rx activity */
1610                 sta->last_rx = jiffies;
1611         }
1612
1613         /*
1614          * FIXME: Do we really need to update the sta_info's information here?
1615          *        We already know about the AP (we found it in our list) so it
1616          *        should already be filled with the right info, no?
1617          *        As is stands, all this is racy because typically we assume
1618          *        the information that is filled in here (except flags) doesn't
1619          *        change while a STA structure is alive. As such, it should move
1620          *        to between the sta_info_alloc() and sta_info_insert() above.
1621          */
1622
1623         set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1624                            WLAN_STA_AUTHORIZED);
1625
1626         rates = 0;
1627         basic_rates = 0;
1628         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1629
1630         for (i = 0; i < elems.supp_rates_len; i++) {
1631                 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1632
1633                 if (rate > 110)
1634                         have_higher_than_11mbit = true;
1635
1636                 for (j = 0; j < sband->n_bitrates; j++) {
1637                         if (sband->bitrates[j].bitrate == rate)
1638                                 rates |= BIT(j);
1639                         if (elems.supp_rates[i] & 0x80)
1640                                 basic_rates |= BIT(j);
1641                 }
1642         }
1643
1644         for (i = 0; i < elems.ext_supp_rates_len; i++) {
1645                 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1646
1647                 if (rate > 110)
1648                         have_higher_than_11mbit = true;
1649
1650                 for (j = 0; j < sband->n_bitrates; j++) {
1651                         if (sband->bitrates[j].bitrate == rate)
1652                                 rates |= BIT(j);
1653                         if (elems.ext_supp_rates[i] & 0x80)
1654                                 basic_rates |= BIT(j);
1655                 }
1656         }
1657
1658         sta->supp_rates[local->hw.conf.channel->band] = rates;
1659         sdata->basic_rates = basic_rates;
1660
1661         /* cf. IEEE 802.11 9.2.12 */
1662         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1663             have_higher_than_11mbit)
1664                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1665         else
1666                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1667
1668         if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1669             (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
1670                 struct ieee80211_ht_bss_info bss_info;
1671                 ieee80211_ht_cap_ie_to_ht_info(
1672                                 (struct ieee80211_ht_cap *)
1673                                 elems.ht_cap_elem, &sta->ht_info);
1674                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1675                                 (struct ieee80211_ht_addt_info *)
1676                                 elems.ht_info_elem, &bss_info);
1677                 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
1678         }
1679
1680         rate_control_rate_init(sta, local);
1681
1682         if (elems.wmm_param) {
1683                 set_sta_flags(sta, WLAN_STA_WME);
1684                 rcu_read_unlock();
1685                 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
1686                                          elems.wmm_param_len);
1687         } else
1688                 rcu_read_unlock();
1689
1690         /* set AID and assoc capability,
1691          * ieee80211_set_associated() will tell the driver */
1692         bss_conf->aid = aid;
1693         bss_conf->assoc_capability = capab_info;
1694         ieee80211_set_associated(sdata, ifsta);
1695
1696         ieee80211_associated(sdata, ifsta);
1697 }
1698
1699
1700 static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
1701                                    struct ieee80211_if_sta *ifsta,
1702                                    struct ieee80211_sta_bss *bss)
1703 {
1704         struct ieee80211_local *local = sdata->local;
1705         int res, rates, i, j;
1706         struct sk_buff *skb;
1707         struct ieee80211_mgmt *mgmt;
1708         u8 *pos;
1709         struct ieee80211_supported_band *sband;
1710         union iwreq_data wrqu;
1711
1712         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1713
1714         /* Remove possible STA entries from other IBSS networks. */
1715         sta_info_flush_delayed(sdata);
1716
1717         if (local->ops->reset_tsf) {
1718                 /* Reset own TSF to allow time synchronization work. */
1719                 local->ops->reset_tsf(local_to_hw(local));
1720         }
1721         memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
1722         res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
1723         if (res)
1724                 return res;
1725
1726         local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
1727
1728         sdata->drop_unencrypted = bss->capability &
1729                 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
1730
1731         res = ieee80211_set_freq(sdata, bss->freq);
1732
1733         if (res)
1734                 return res;
1735
1736         /* Build IBSS probe response */
1737         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
1738         if (skb) {
1739                 skb_reserve(skb, local->hw.extra_tx_headroom);
1740
1741                 mgmt = (struct ieee80211_mgmt *)
1742                         skb_put(skb, 24 + sizeof(mgmt->u.beacon));
1743                 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
1744                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1745                                                   IEEE80211_STYPE_PROBE_RESP);
1746                 memset(mgmt->da, 0xff, ETH_ALEN);
1747                 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1748                 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1749                 mgmt->u.beacon.beacon_int =
1750                         cpu_to_le16(local->hw.conf.beacon_int);
1751                 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
1752                 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
1753
1754                 pos = skb_put(skb, 2 + ifsta->ssid_len);
1755                 *pos++ = WLAN_EID_SSID;
1756                 *pos++ = ifsta->ssid_len;
1757                 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
1758
1759                 rates = bss->supp_rates_len;
1760                 if (rates > 8)
1761                         rates = 8;
1762                 pos = skb_put(skb, 2 + rates);
1763                 *pos++ = WLAN_EID_SUPP_RATES;
1764                 *pos++ = rates;
1765                 memcpy(pos, bss->supp_rates, rates);
1766
1767                 if (bss->band == IEEE80211_BAND_2GHZ) {
1768                         pos = skb_put(skb, 2 + 1);
1769                         *pos++ = WLAN_EID_DS_PARAMS;
1770                         *pos++ = 1;
1771                         *pos++ = ieee80211_frequency_to_channel(bss->freq);
1772                 }
1773
1774                 pos = skb_put(skb, 2 + 2);
1775                 *pos++ = WLAN_EID_IBSS_PARAMS;
1776                 *pos++ = 2;
1777                 /* FIX: set ATIM window based on scan results */
1778                 *pos++ = 0;
1779                 *pos++ = 0;
1780
1781                 if (bss->supp_rates_len > 8) {
1782                         rates = bss->supp_rates_len - 8;
1783                         pos = skb_put(skb, 2 + rates);
1784                         *pos++ = WLAN_EID_EXT_SUPP_RATES;
1785                         *pos++ = rates;
1786                         memcpy(pos, &bss->supp_rates[8], rates);
1787                 }
1788
1789                 ifsta->probe_resp = skb;
1790
1791                 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
1792         }
1793
1794         rates = 0;
1795         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1796         for (i = 0; i < bss->supp_rates_len; i++) {
1797                 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
1798                 for (j = 0; j < sband->n_bitrates; j++)
1799                         if (sband->bitrates[j].bitrate == bitrate)
1800                                 rates |= BIT(j);
1801         }
1802         ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
1803
1804         ieee80211_sta_def_wmm_params(sdata, bss);
1805
1806         ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
1807         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1808
1809         memset(&wrqu, 0, sizeof(wrqu));
1810         memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
1811         wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
1812
1813         return res;
1814 }
1815
1816 u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
1817                             struct ieee802_11_elems *elems,
1818                             enum ieee80211_band band)
1819 {
1820         struct ieee80211_supported_band *sband;
1821         struct ieee80211_rate *bitrates;
1822         size_t num_rates;
1823         u64 supp_rates;
1824         int i, j;
1825         sband = local->hw.wiphy->bands[band];
1826
1827         if (!sband) {
1828                 WARN_ON(1);
1829                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1830         }
1831
1832         bitrates = sband->bitrates;
1833         num_rates = sband->n_bitrates;
1834         supp_rates = 0;
1835         for (i = 0; i < elems->supp_rates_len +
1836                      elems->ext_supp_rates_len; i++) {
1837                 u8 rate = 0;
1838                 int own_rate;
1839                 if (i < elems->supp_rates_len)
1840                         rate = elems->supp_rates[i];
1841                 else if (elems->ext_supp_rates)
1842                         rate = elems->ext_supp_rates
1843                                 [i - elems->supp_rates_len];
1844                 own_rate = 5 * (rate & 0x7f);
1845                 for (j = 0; j < num_rates; j++)
1846                         if (bitrates[j].bitrate == own_rate)
1847                                 supp_rates |= BIT(j);
1848         }
1849         return supp_rates;
1850 }
1851
1852 static u64 ieee80211_sta_get_mandatory_rates(struct ieee80211_local *local,
1853                                         enum ieee80211_band band)
1854 {
1855         struct ieee80211_supported_band *sband;
1856         struct ieee80211_rate *bitrates;
1857         u64 mandatory_rates;
1858         enum ieee80211_rate_flags mandatory_flag;
1859         int i;
1860
1861         sband = local->hw.wiphy->bands[band];
1862         if (!sband) {
1863                 WARN_ON(1);
1864                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1865         }
1866
1867         if (band == IEEE80211_BAND_2GHZ)
1868                 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
1869         else
1870                 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
1871
1872         bitrates = sband->bitrates;
1873         mandatory_rates = 0;
1874         for (i = 0; i < sband->n_bitrates; i++)
1875                 if (bitrates[i].flags & mandatory_flag)
1876                         mandatory_rates |= BIT(i);
1877         return mandatory_rates;
1878 }
1879
1880 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1881                                   struct ieee80211_mgmt *mgmt,
1882                                   size_t len,
1883                                   struct ieee80211_rx_status *rx_status,
1884                                   struct ieee802_11_elems *elems,
1885                                   bool beacon)
1886 {
1887         struct ieee80211_local *local = sdata->local;
1888         int freq;
1889         struct ieee80211_sta_bss *bss;
1890         struct sta_info *sta;
1891         struct ieee80211_channel *channel;
1892         u64 beacon_timestamp, rx_timestamp;
1893         u64 supp_rates = 0;
1894         enum ieee80211_band band = rx_status->band;
1895         DECLARE_MAC_BUF(mac);
1896         DECLARE_MAC_BUF(mac2);
1897
1898         if (elems->ds_params && elems->ds_params_len == 1)
1899                 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1900         else
1901                 freq = rx_status->freq;
1902
1903         channel = ieee80211_get_channel(local->hw.wiphy, freq);
1904
1905         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1906                 return;
1907
1908         if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
1909             elems->mesh_config && mesh_matches_local(elems, sdata)) {
1910                 supp_rates = ieee80211_sta_get_rates(local, elems, band);
1911
1912                 mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
1913                                       mesh_peer_accepts_plinks(elems));
1914         }
1915
1916         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
1917             memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1918                 supp_rates = ieee80211_sta_get_rates(local, elems, band);
1919
1920                 rcu_read_lock();
1921
1922                 sta = sta_info_get(local, mgmt->sa);
1923                 if (sta) {
1924                         u64 prev_rates;
1925
1926                         prev_rates = sta->supp_rates[band];
1927                         /* make sure mandatory rates are always added */
1928                         sta->supp_rates[band] = supp_rates |
1929                                 ieee80211_sta_get_mandatory_rates(local, band);
1930
1931 #ifdef CONFIG_MAC80211_IBSS_DEBUG
1932                         if (sta->supp_rates[band] != prev_rates)
1933                                 printk(KERN_DEBUG "%s: updated supp_rates set "
1934                                     "for %s based on beacon info (0x%llx | "
1935                                     "0x%llx -> 0x%llx)\n",
1936                                     sdata->dev->name, print_mac(mac, sta->addr),
1937                                     (unsigned long long) prev_rates,
1938                                     (unsigned long long) supp_rates,
1939                                     (unsigned long long) sta->supp_rates[band]);
1940 #endif
1941                 } else {
1942                         ieee80211_ibss_add_sta(sdata, NULL, mgmt->bssid,
1943                                                mgmt->sa, supp_rates);
1944                 }
1945
1946                 rcu_read_unlock();
1947         }
1948
1949         bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1950                                         freq, beacon);
1951         if (!bss)
1952                 return;
1953
1954         /* was just updated in ieee80211_bss_info_update */
1955         beacon_timestamp = bss->timestamp;
1956
1957         /*
1958          * In STA mode, the remaining parameters should not be overridden
1959          * by beacons because they're not necessarily accurate there.
1960          */
1961         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
1962             bss->last_probe_resp && beacon) {
1963                 ieee80211_rx_bss_put(local, bss);
1964                 return;
1965         }
1966
1967         /* check if we need to merge IBSS */
1968         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
1969             bss->capability & WLAN_CAPABILITY_IBSS &&
1970             bss->freq == local->oper_channel->center_freq &&
1971             elems->ssid_len == sdata->u.sta.ssid_len &&
1972             memcmp(elems->ssid, sdata->u.sta.ssid,
1973                                 sdata->u.sta.ssid_len) == 0) {
1974                 if (rx_status->flag & RX_FLAG_TSFT) {
1975                         /* in order for correct IBSS merging we need mactime
1976                          *
1977                          * since mactime is defined as the time the first data
1978                          * symbol of the frame hits the PHY, and the timestamp
1979                          * of the beacon is defined as "the time that the data
1980                          * symbol containing the first bit of the timestamp is
1981                          * transmitted to the PHY plus the transmitting STA’s
1982                          * delays through its local PHY from the MAC-PHY
1983                          * interface to its interface with the WM"
1984                          * (802.11 11.1.2) - equals the time this bit arrives at
1985                          * the receiver - we have to take into account the
1986                          * offset between the two.
1987                          * e.g: at 1 MBit that means mactime is 192 usec earlier
1988                          * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
1989                          */
1990                         int rate = local->hw.wiphy->bands[band]->
1991                                         bitrates[rx_status->rate_idx].bitrate;
1992                         rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
1993                 } else if (local && local->ops && local->ops->get_tsf)
1994                         /* second best option: get current TSF */
1995                         rx_timestamp = local->ops->get_tsf(local_to_hw(local));
1996                 else
1997                         /* can't merge without knowing the TSF */
1998                         rx_timestamp = -1LLU;
1999 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2000                 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2001                        "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2002                        print_mac(mac, mgmt->sa),
2003                        print_mac(mac2, mgmt->bssid),
2004                        (unsigned long long)rx_timestamp,
2005                        (unsigned long long)beacon_timestamp,
2006                        (unsigned long long)(rx_timestamp - beacon_timestamp),
2007                        jiffies);
2008 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2009                 if (beacon_timestamp > rx_timestamp) {
2010 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2011                         printk(KERN_DEBUG "%s: beacon TSF higher than "
2012                                "local TSF - IBSS merge with BSSID %s\n",
2013                                sdata->dev->name, print_mac(mac, mgmt->bssid));
2014 #endif
2015                         ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
2016                         ieee80211_ibss_add_sta(sdata, NULL,
2017                                                mgmt->bssid, mgmt->sa,
2018                                                supp_rates);
2019                 }
2020         }
2021
2022         ieee80211_rx_bss_put(local, bss);
2023 }
2024
2025
2026 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
2027                                          struct ieee80211_mgmt *mgmt,
2028                                          size_t len,
2029                                          struct ieee80211_rx_status *rx_status)
2030 {
2031         size_t baselen;
2032         struct ieee802_11_elems elems;
2033         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2034
2035         if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
2036                 return; /* ignore ProbeResp to foreign address */
2037
2038         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2039         if (baselen > len)
2040                 return;
2041
2042         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2043                                 &elems);
2044
2045         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
2046
2047         /* direct probe may be part of the association flow */
2048         if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
2049                                                         &ifsta->request)) {
2050                 printk(KERN_DEBUG "%s direct probe responded\n",
2051                        sdata->dev->name);
2052                 ieee80211_authenticate(sdata, ifsta);
2053         }
2054 }
2055
2056
2057 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2058                                      struct ieee80211_mgmt *mgmt,
2059                                      size_t len,
2060                                      struct ieee80211_rx_status *rx_status)
2061 {
2062         struct ieee80211_if_sta *ifsta;
2063         size_t baselen;
2064         struct ieee802_11_elems elems;
2065         struct ieee80211_local *local = sdata->local;
2066         struct ieee80211_conf *conf = &local->hw.conf;
2067         u32 changed = 0;
2068
2069         /* Process beacon from the current BSS */
2070         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2071         if (baselen > len)
2072                 return;
2073
2074         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2075
2076         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
2077
2078         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2079                 return;
2080         ifsta = &sdata->u.sta;
2081
2082         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
2083             memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2084                 return;
2085
2086         ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
2087                                  elems.wmm_param_len);
2088
2089         if (elems.erp_info && elems.erp_info_len >= 1)
2090                 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
2091         else {
2092                 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2093                 changed |= ieee80211_handle_protect_preamb(sdata, false,
2094                                 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2095         }
2096
2097         if (elems.ht_cap_elem && elems.ht_info_elem &&
2098             elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2099                 struct ieee80211_ht_bss_info bss_info;
2100
2101                 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2102                                 (struct ieee80211_ht_addt_info *)
2103                                 elems.ht_info_elem, &bss_info);
2104                 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2105                                                &bss_info);
2106         }
2107
2108         ieee80211_bss_info_change_notify(sdata, changed);
2109 }
2110
2111
2112 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
2113                                         struct ieee80211_if_sta *ifsta,
2114                                         struct ieee80211_mgmt *mgmt,
2115                                         size_t len,
2116                                         struct ieee80211_rx_status *rx_status)
2117 {
2118         struct ieee80211_local *local = sdata->local;
2119         int tx_last_beacon;
2120         struct sk_buff *skb;
2121         struct ieee80211_mgmt *resp;
2122         u8 *pos, *end;
2123         DECLARE_MAC_BUF(mac);
2124 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2125         DECLARE_MAC_BUF(mac2);
2126         DECLARE_MAC_BUF(mac3);
2127 #endif
2128
2129         if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
2130             ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
2131             len < 24 + 2 || !ifsta->probe_resp)
2132                 return;
2133
2134         if (local->ops->tx_last_beacon)
2135                 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2136         else
2137                 tx_last_beacon = 1;
2138
2139 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2140         printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2141                "%s (tx_last_beacon=%d)\n",
2142                sdata->dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2143                print_mac(mac3, mgmt->bssid), tx_last_beacon);
2144 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2145
2146         if (!tx_last_beacon)
2147                 return;
2148
2149         if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2150             memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2151                 return;
2152
2153         end = ((u8 *) mgmt) + len;
2154         pos = mgmt->u.probe_req.variable;
2155         if (pos[0] != WLAN_EID_SSID ||
2156             pos + 2 + pos[1] > end) {
2157 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2158                 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2159                        "from %s\n",
2160                        sdata->dev->name, print_mac(mac, mgmt->sa));
2161 #endif
2162                 return;
2163         }
2164         if (pos[1] != 0 &&
2165             (pos[1] != ifsta->ssid_len ||
2166              memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2167                 /* Ignore ProbeReq for foreign SSID */
2168                 return;
2169         }
2170
2171         /* Reply with ProbeResp */
2172         skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
2173         if (!skb)
2174                 return;
2175
2176         resp = (struct ieee80211_mgmt *) skb->data;
2177         memcpy(resp->da, mgmt->sa, ETH_ALEN);
2178 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2179         printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2180                sdata->dev->name, print_mac(mac, resp->da));
2181 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2182         ieee80211_sta_tx(sdata, skb, 0);
2183 }
2184
2185 static void ieee80211_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
2186                                      struct ieee80211_if_sta *ifsta,
2187                                      struct ieee80211_mgmt *mgmt,
2188                                      size_t len,
2189                                      struct ieee80211_rx_status *rx_status)
2190 {
2191         struct ieee80211_local *local = sdata->local;
2192
2193         /* all categories we currently handle have action_code */
2194         if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2195                 return;
2196
2197         switch (mgmt->u.action.category) {
2198         case WLAN_CATEGORY_SPECTRUM_MGMT:
2199                 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
2200                         break;
2201                 switch (mgmt->u.action.u.measurement.action_code) {
2202                 case WLAN_ACTION_SPCT_MSR_REQ:
2203                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2204                                    sizeof(mgmt->u.action.u.measurement)))
2205                                 break;
2206                         ieee80211_sta_process_measurement_req(sdata, mgmt, len);
2207                         break;
2208                 }
2209                 break;
2210         case WLAN_CATEGORY_BACK:
2211                 switch (mgmt->u.action.u.addba_req.action_code) {
2212                 case WLAN_ACTION_ADDBA_REQ:
2213                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2214                                    sizeof(mgmt->u.action.u.addba_req)))
2215                                 break;
2216                         ieee80211_sta_process_addba_request(local, mgmt, len);
2217                         break;
2218                 case WLAN_ACTION_ADDBA_RESP:
2219                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2220                                    sizeof(mgmt->u.action.u.addba_resp)))
2221                                 break;
2222                         ieee80211_sta_process_addba_resp(local, mgmt, len);
2223                         break;
2224                 case WLAN_ACTION_DELBA:
2225                         if (len < (IEEE80211_MIN_ACTION_SIZE +
2226                                    sizeof(mgmt->u.action.u.delba)))
2227                                 break;
2228                         ieee80211_sta_process_delba(sdata, mgmt, len);
2229                         break;
2230                 }
2231                 break;
2232         case PLINK_CATEGORY:
2233                 if (ieee80211_vif_is_mesh(&sdata->vif))
2234                         mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
2235                 break;
2236         case MESH_PATH_SEL_CATEGORY:
2237                 if (ieee80211_vif_is_mesh(&sdata->vif))
2238                         mesh_rx_path_sel_frame(sdata, mgmt, len);
2239                 break;
2240         }
2241 }
2242
2243 void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
2244                            struct ieee80211_rx_status *rx_status)
2245 {
2246         struct ieee80211_local *local = sdata->local;
2247         struct ieee80211_if_sta *ifsta;
2248         struct ieee80211_mgmt *mgmt;
2249         u16 fc;
2250
2251         if (skb->len < 24)
2252                 goto fail;
2253
2254         ifsta = &sdata->u.sta;
2255
2256         mgmt = (struct ieee80211_mgmt *) skb->data;
2257         fc = le16_to_cpu(mgmt->frame_control);
2258
2259         switch (fc & IEEE80211_FCTL_STYPE) {
2260         case IEEE80211_STYPE_PROBE_REQ:
2261         case IEEE80211_STYPE_PROBE_RESP:
2262         case IEEE80211_STYPE_BEACON:
2263         case IEEE80211_STYPE_ACTION:
2264                 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2265         case IEEE80211_STYPE_AUTH:
2266         case IEEE80211_STYPE_ASSOC_RESP:
2267         case IEEE80211_STYPE_REASSOC_RESP:
2268         case IEEE80211_STYPE_DEAUTH:
2269         case IEEE80211_STYPE_DISASSOC:
2270                 skb_queue_tail(&ifsta->skb_queue, skb);
2271                 queue_work(local->hw.workqueue, &ifsta->work);
2272                 return;
2273         }
2274
2275  fail:
2276         kfree_skb(skb);
2277 }
2278
2279 static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2280                                          struct sk_buff *skb)
2281 {
2282         struct ieee80211_rx_status *rx_status;
2283         struct ieee80211_if_sta *ifsta;
2284         struct ieee80211_mgmt *mgmt;
2285         u16 fc;
2286
2287         ifsta = &sdata->u.sta;
2288
2289         rx_status = (struct ieee80211_rx_status *) skb->cb;
2290         mgmt = (struct ieee80211_mgmt *) skb->data;
2291         fc = le16_to_cpu(mgmt->frame_control);
2292
2293         switch (fc & IEEE80211_FCTL_STYPE) {
2294         case IEEE80211_STYPE_PROBE_REQ:
2295                 ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len,
2296                                             rx_status);
2297                 break;
2298         case IEEE80211_STYPE_PROBE_RESP:
2299                 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
2300                 break;
2301         case IEEE80211_STYPE_BEACON:
2302                 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2303                 break;
2304         case IEEE80211_STYPE_AUTH:
2305                 ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
2306                 break;
2307         case IEEE80211_STYPE_ASSOC_RESP:
2308                 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
2309                 break;
2310         case IEEE80211_STYPE_REASSOC_RESP:
2311                 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
2312                 break;
2313         case IEEE80211_STYPE_DEAUTH:
2314                 ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
2315                 break;
2316         case IEEE80211_STYPE_DISASSOC:
2317                 ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
2318                 break;
2319         case IEEE80211_STYPE_ACTION:
2320                 ieee80211_rx_mgmt_action(sdata, ifsta, mgmt, skb->len, rx_status);
2321                 break;
2322         }
2323
2324         kfree_skb(skb);
2325 }
2326
2327
2328 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
2329 {
2330         struct ieee80211_local *local = sdata->local;
2331         int active = 0;
2332         struct sta_info *sta;
2333
2334         rcu_read_lock();
2335
2336         list_for_each_entry_rcu(sta, &local->sta_list, list) {
2337                 if (sta->sdata == sdata &&
2338                     time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
2339                                jiffies)) {
2340                         active++;
2341                         break;
2342                 }
2343         }
2344
2345         rcu_read_unlock();
2346
2347         return active;
2348 }
2349
2350
2351 static void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, unsigned long exp_time)
2352 {
2353         struct ieee80211_local *local = sdata->local;
2354         struct sta_info *sta, *tmp;
2355         LIST_HEAD(tmp_list);
2356         DECLARE_MAC_BUF(mac);
2357         unsigned long flags;
2358
2359         spin_lock_irqsave(&local->sta_lock, flags);
2360         list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
2361                 if (time_after(jiffies, sta->last_rx + exp_time)) {
2362 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2363                         printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
2364                                sdata->dev->name, print_mac(mac, sta->addr));
2365 #endif
2366                         __sta_info_unlink(&sta);
2367                         if (sta)
2368                                 list_add(&sta->list, &tmp_list);
2369                 }
2370         spin_unlock_irqrestore(&local->sta_lock, flags);
2371
2372         list_for_each_entry_safe(sta, tmp, &tmp_list, list)
2373                 sta_info_destroy(sta);
2374 }
2375
2376
2377 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
2378                                      struct ieee80211_if_sta *ifsta)
2379 {
2380         mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2381
2382         ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
2383         if (ieee80211_sta_active_ibss(sdata))
2384                 return;
2385
2386         printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
2387                "IBSS networks with same SSID (merge)\n", sdata->dev->name);
2388         ieee80211_sta_req_scan(sdata, ifsta->ssid, ifsta->ssid_len);
2389 }
2390
2391
2392 #ifdef CONFIG_MAC80211_MESH
2393 static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
2394                            struct ieee80211_if_sta *ifsta)
2395 {
2396         bool free_plinks;
2397
2398         ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
2399         mesh_path_expire(sdata);
2400
2401         free_plinks = mesh_plink_availables(sdata);
2402         if (free_plinks != sdata->u.sta.accepting_plinks)
2403                 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
2404
2405         mod_timer(&ifsta->timer, jiffies +
2406                         IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
2407 }
2408
2409
2410 void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
2411 {
2412         struct ieee80211_if_sta *ifsta;
2413         ifsta = &sdata->u.sta;
2414         ifsta->state = IEEE80211_STA_MLME_MESH_UP;
2415         ieee80211_sta_timer((unsigned long)sdata);
2416         ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
2417 }
2418 #endif
2419
2420
2421 void ieee80211_sta_timer(unsigned long data)
2422 {
2423         struct ieee80211_sub_if_data *sdata =
2424                 (struct ieee80211_sub_if_data *) data;
2425         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2426         struct ieee80211_local *local = sdata->local;
2427
2428         set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2429         queue_work(local->hw.workqueue, &ifsta->work);
2430 }
2431
2432 static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
2433                                      struct ieee80211_if_sta *ifsta)
2434 {
2435         struct ieee80211_local *local = sdata->local;
2436
2437         if (local->ops->reset_tsf) {
2438                 /* Reset own TSF to allow time synchronization work. */
2439                 local->ops->reset_tsf(local_to_hw(local));
2440         }
2441
2442         ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2443
2444
2445         if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2446                 ifsta->auth_alg = WLAN_AUTH_OPEN;
2447         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2448                 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2449         else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2450                 ifsta->auth_alg = WLAN_AUTH_LEAP;
2451         else
2452                 ifsta->auth_alg = WLAN_AUTH_OPEN;
2453         ifsta->auth_transaction = -1;
2454         ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2455         ifsta->assoc_scan_tries = 0;
2456         ifsta->direct_probe_tries = 0;
2457         ifsta->auth_tries = 0;
2458         ifsta->assoc_tries = 0;
2459         netif_tx_stop_all_queues(sdata->dev);
2460         netif_carrier_off(sdata->dev);
2461 }
2462
2463
2464 void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
2465                             struct ieee80211_if_sta *ifsta)
2466 {
2467         struct ieee80211_local *local = sdata->local;
2468
2469         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2470                 return;
2471
2472         if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2473                              IEEE80211_STA_AUTO_BSSID_SEL)) &&
2474             (ifsta->flags & (IEEE80211_STA_SSID_SET |
2475                              IEEE80211_STA_AUTO_SSID_SEL))) {
2476
2477                 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED)
2478                         ieee80211_set_disassoc(sdata, ifsta, true, true,
2479                                                WLAN_REASON_DEAUTH_LEAVING);
2480
2481                 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2482                 queue_work(local->hw.workqueue, &ifsta->work);
2483         }
2484 }
2485
2486 static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2487                                     const char *ssid, int ssid_len)
2488 {
2489         int tmp, hidden_ssid;
2490
2491         if (ssid_len == ifsta->ssid_len &&
2492             !memcmp(ifsta->ssid, ssid, ssid_len))
2493                 return 1;
2494
2495         if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
2496                 return 0;
2497
2498         hidden_ssid = 1;
2499         tmp = ssid_len;
2500         while (tmp--) {
2501                 if (ssid[tmp] != '\0') {
2502                         hidden_ssid = 0;
2503                         break;
2504                 }
2505         }
2506
2507         if (hidden_ssid && ifsta->ssid_len == ssid_len)
2508                 return 1;
2509
2510         if (ssid_len == 1 && ssid[0] == ' ')
2511                 return 1;
2512
2513         return 0;
2514 }
2515
2516 static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
2517                                      struct ieee80211_if_sta *ifsta)
2518 {
2519         struct ieee80211_local *local = sdata->local;
2520         struct ieee80211_sta_bss *bss;
2521         struct ieee80211_supported_band *sband;
2522         u8 bssid[ETH_ALEN], *pos;
2523         int i;
2524         int ret;
2525         DECLARE_MAC_BUF(mac);
2526
2527 #if 0
2528         /* Easier testing, use fixed BSSID. */
2529         memset(bssid, 0xfe, ETH_ALEN);
2530 #else
2531         /* Generate random, not broadcast, locally administered BSSID. Mix in
2532          * own MAC address to make sure that devices that do not have proper
2533          * random number generator get different BSSID. */
2534         get_random_bytes(bssid, ETH_ALEN);
2535         for (i = 0; i < ETH_ALEN; i++)
2536                 bssid[i] ^= sdata->dev->dev_addr[i];
2537         bssid[0] &= ~0x01;
2538         bssid[0] |= 0x02;
2539 #endif
2540
2541         printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
2542                sdata->dev->name, print_mac(mac, bssid));
2543
2544         bss = ieee80211_rx_bss_add(local, bssid,
2545                                    local->hw.conf.channel->center_freq,
2546                                    sdata->u.sta.ssid, sdata->u.sta.ssid_len);
2547         if (!bss)
2548                 return -ENOMEM;
2549
2550         bss->band = local->hw.conf.channel->band;
2551         sband = local->hw.wiphy->bands[bss->band];
2552
2553         if (local->hw.conf.beacon_int == 0)
2554                 local->hw.conf.beacon_int = 100;
2555         bss->beacon_int = local->hw.conf.beacon_int;
2556         bss->last_update = jiffies;
2557         bss->capability = WLAN_CAPABILITY_IBSS;
2558
2559         if (sdata->default_key)
2560                 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2561         else
2562                 sdata->drop_unencrypted = 0;
2563
2564         bss->supp_rates_len = sband->n_bitrates;
2565         pos = bss->supp_rates;
2566         for (i = 0; i < sband->n_bitrates; i++) {
2567                 int rate = sband->bitrates[i].bitrate;
2568                 *pos++ = (u8) (rate / 5);
2569         }
2570
2571         ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
2572         ieee80211_rx_bss_put(local, bss);
2573         return ret;
2574 }
2575
2576
2577 static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
2578                                    struct ieee80211_if_sta *ifsta)
2579 {
2580         struct ieee80211_local *local = sdata->local;
2581         struct ieee80211_sta_bss *bss;
2582         int found = 0;
2583         u8 bssid[ETH_ALEN];
2584         int active_ibss;
2585         DECLARE_MAC_BUF(mac);
2586         DECLARE_MAC_BUF(mac2);
2587
2588         if (ifsta->ssid_len == 0)
2589                 return -EINVAL;
2590
2591         active_ibss = ieee80211_sta_active_ibss(sdata);
2592 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2593         printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2594                sdata->dev->name, active_ibss);
2595 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2596         spin_lock_bh(&local->sta_bss_lock);
2597         list_for_each_entry(bss, &local->sta_bss_list, list) {
2598                 if (ifsta->ssid_len != bss->ssid_len ||
2599                     memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2600                     || !(bss->capability & WLAN_CAPABILITY_IBSS))
2601                         continue;
2602 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2603                 printk(KERN_DEBUG "   bssid=%s found\n",
2604                        print_mac(mac, bss->bssid));
2605 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2606                 memcpy(bssid, bss->bssid, ETH_ALEN);
2607                 found = 1;
2608                 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2609                         break;
2610         }
2611         spin_unlock_bh(&local->sta_bss_lock);
2612
2613 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2614         if (found)
2615                 printk(KERN_DEBUG "   sta_find_ibss: selected %s current "
2616                        "%s\n", print_mac(mac, bssid),
2617                        print_mac(mac2, ifsta->bssid));
2618 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2619
2620         if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2621                 int ret;
2622                 int search_freq;
2623
2624                 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
2625                         search_freq = bss->freq;
2626                 else
2627                         search_freq = local->hw.conf.channel->center_freq;
2628
2629                 bss = ieee80211_rx_bss_get(local, bssid, search_freq,
2630                                            ifsta->ssid, ifsta->ssid_len);
2631                 if (!bss)
2632                         goto dont_join;
2633
2634                 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
2635                        " based on configured SSID\n",
2636                        sdata->dev->name, print_mac(mac, bssid));
2637                 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
2638                 ieee80211_rx_bss_put(local, bss);
2639                 return ret;
2640         }
2641
2642 dont_join:
2643 #ifdef CONFIG_MAC80211_IBSS_DEBUG
2644         printk(KERN_DEBUG "   did not try to join ibss\n");
2645 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
2646
2647         /* Selected IBSS not found in current scan results - try to scan */
2648         if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
2649             !ieee80211_sta_active_ibss(sdata)) {
2650                 mod_timer(&ifsta->timer, jiffies +
2651                                       IEEE80211_IBSS_MERGE_INTERVAL);
2652         } else if (time_after(jiffies, local->last_scan_completed +
2653                               IEEE80211_SCAN_INTERVAL)) {
2654                 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2655                        "join\n", sdata->dev->name);
2656                 return ieee80211_sta_req_scan(sdata, ifsta->ssid,
2657                                               ifsta->ssid_len);
2658         } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
2659                 int interval = IEEE80211_SCAN_INTERVAL;
2660
2661                 if (time_after(jiffies, ifsta->ibss_join_req +
2662                                IEEE80211_IBSS_JOIN_TIMEOUT)) {
2663                         if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
2664                             (!(local->oper_channel->flags &
2665                                         IEEE80211_CHAN_NO_IBSS)))
2666                                 return ieee80211_sta_create_ibss(sdata, ifsta);
2667                         if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
2668                                 printk(KERN_DEBUG "%s: IBSS not allowed on"
2669                                        " %d MHz\n", sdata->dev->name,
2670                                        local->hw.conf.channel->center_freq);
2671                         }
2672
2673                         /* No IBSS found - decrease scan interval and continue
2674                          * scanning. */
2675                         interval = IEEE80211_SCAN_INTERVAL_SLOW;
2676                 }
2677
2678                 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
2679                 mod_timer(&ifsta->timer, jiffies + interval);
2680                 return 0;
2681         }
2682
2683         return 0;
2684 }
2685
2686
2687 int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
2688 {
2689         struct ieee80211_if_sta *ifsta;
2690         int res;
2691
2692         if (len > IEEE80211_MAX_SSID_LEN)
2693                 return -EINVAL;
2694
2695         ifsta = &sdata->u.sta;
2696
2697         if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
2698                 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
2699                 memcpy(ifsta->ssid, ssid, len);
2700                 ifsta->ssid_len = len;
2701                 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2702
2703                 res = 0;
2704                 /*
2705                  * Hack! MLME code needs to be cleaned up to have different
2706                  * entry points for configuration and internal selection change
2707                  */
2708                 if (netif_running(sdata->dev))
2709                         res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
2710                 if (res) {
2711                         printk(KERN_DEBUG "%s: Failed to config new SSID to "
2712                                "the low-level driver\n", sdata->dev->name);
2713                         return res;
2714                 }
2715         }
2716
2717         if (len)
2718                 ifsta->flags |= IEEE80211_STA_SSID_SET;
2719         else
2720                 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
2721
2722         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
2723             !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
2724                 ifsta->ibss_join_req = jiffies;
2725                 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
2726                 return ieee80211_sta_find_ibss(sdata, ifsta);
2727         }
2728
2729         return 0;
2730 }
2731
2732
2733 int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
2734 {
2735         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2736         memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2737         *len = ifsta->ssid_len;
2738         return 0;
2739 }
2740
2741
2742 int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
2743 {
2744         struct ieee80211_if_sta *ifsta;
2745         int res;
2746
2747         ifsta = &sdata->u.sta;
2748
2749         if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2750                 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2751                 res = 0;
2752                 /*
2753                  * Hack! See also ieee80211_sta_set_ssid.
2754                  */
2755                 if (netif_running(sdata->dev))
2756                         res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
2757                 if (res) {
2758                         printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2759                                "the low-level driver\n", sdata->dev->name);
2760                         return res;
2761                 }
2762         }
2763
2764         if (is_valid_ether_addr(bssid))
2765                 ifsta->flags |= IEEE80211_STA_BSSID_SET;
2766         else
2767                 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2768
2769         return 0;
2770 }
2771
2772
2773 int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
2774 {
2775         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2776
2777         kfree(ifsta->extra_ie);
2778         if (len == 0) {
2779                 ifsta->extra_ie = NULL;
2780                 ifsta->extra_ie_len = 0;
2781                 return 0;
2782         }
2783         ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
2784         if (!ifsta->extra_ie) {
2785                 ifsta->extra_ie_len = 0;
2786                 return -ENOMEM;
2787         }
2788         memcpy(ifsta->extra_ie, ie, len);
2789         ifsta->extra_ie_len = len;
2790         return 0;
2791 }
2792
2793
2794 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
2795                                         struct sk_buff *skb, u8 *bssid,
2796                                         u8 *addr, u64 supp_rates)
2797 {
2798         struct ieee80211_local *local = sdata->local;
2799         struct sta_info *sta;
2800         DECLARE_MAC_BUF(mac);
2801         int band = local->hw.conf.channel->band;
2802
2803         /* TODO: Could consider removing the least recently used entry and
2804          * allow new one to be added. */
2805         if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
2806                 if (net_ratelimit()) {
2807                         printk(KERN_DEBUG "%s: No room for a new IBSS STA "
2808                                "entry %s\n", sdata->dev->name, print_mac(mac, addr));
2809                 }
2810                 return NULL;
2811         }
2812
2813         if (compare_ether_addr(bssid, sdata->u.sta.bssid))
2814                 return NULL;
2815
2816 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2817         printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
2818                wiphy_name(local->hw.wiphy), print_mac(mac, addr), sdata->dev->name);
2819 #endif
2820
2821         sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
2822         if (!sta)
2823                 return NULL;
2824
2825         set_sta_flags(sta, WLAN_STA_AUTHORIZED);
2826
2827         /* make sure mandatory rates are always added */
2828         sta->supp_rates[band] = supp_rates |
2829                         ieee80211_sta_get_mandatory_rates(local, band);
2830
2831         rate_control_rate_init(sta, local);
2832
2833         if (sta_info_insert(sta))
2834                 return NULL;
2835
2836         return sta;
2837 }
2838
2839
2840 static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
2841                                      struct ieee80211_if_sta *ifsta)
2842 {
2843         struct ieee80211_local *local = sdata->local;
2844         struct ieee80211_sta_bss *bss, *selected = NULL;
2845         int top_rssi = 0, freq;
2846
2847         spin_lock_bh(&local->sta_bss_lock);
2848         freq = local->oper_channel->center_freq;
2849         list_for_each_entry(bss, &local->sta_bss_list, list) {
2850                 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2851                         continue;
2852
2853                 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2854                         IEEE80211_STA_AUTO_BSSID_SEL |
2855                         IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
2856                     (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2857                      !!sdata->default_key))
2858                         continue;
2859
2860                 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2861                     bss->freq != freq)
2862                         continue;
2863
2864                 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
2865                     memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2866                         continue;
2867
2868                 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
2869                     !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2870                         continue;
2871
2872                 if (!selected || top_rssi < bss->signal) {
2873                         selected = bss;
2874                         top_rssi = bss->signal;
2875                 }
2876         }
2877         if (selected)
2878                 atomic_inc(&selected->users);
2879         spin_unlock_bh(&local->sta_bss_lock);
2880
2881         if (selected) {
2882                 ieee80211_set_freq(sdata, selected->freq);
2883                 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
2884                         ieee80211_sta_set_ssid(sdata, selected->ssid,
2885                                                selected->ssid_len);
2886                 ieee80211_sta_set_bssid(sdata, selected->bssid);
2887                 ieee80211_sta_def_wmm_params(sdata, selected);
2888
2889                 /* Send out direct probe if no probe resp was received or
2890                  * the one we have is outdated
2891                  */
2892                 if (!selected->last_probe_resp ||
2893                     time_after(jiffies, selected->last_probe_resp
2894                                         + IEEE80211_SCAN_RESULT_EXPIRE))
2895                         ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
2896                 else
2897                         ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2898
2899                 ieee80211_rx_bss_put(local, selected);
2900                 ieee80211_sta_reset_auth(sdata, ifsta);
2901                 return 0;
2902         } else {
2903                 if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
2904                         ifsta->assoc_scan_tries++;
2905                         if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
2906                                 ieee80211_sta_start_scan(sdata, NULL, 0);
2907                         else
2908                                 ieee80211_sta_start_scan(sdata, ifsta->ssid,
2909                                                          ifsta->ssid_len);
2910                         ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
2911                         set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2912                 } else
2913                         ifsta->state = IEEE80211_STA_MLME_DISABLED;
2914         }
2915         return -1;
2916 }
2917
2918
2919 int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
2920 {
2921         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2922
2923         printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
2924                sdata->dev->name, reason);
2925
2926         if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2927             sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
2928                 return -EINVAL;
2929
2930         ieee80211_set_disassoc(sdata, ifsta, true, true, reason);
2931         return 0;
2932 }
2933
2934
2935 int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
2936 {
2937         struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2938
2939         printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
2940                sdata->dev->name, reason);
2941
2942         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2943                 return -EINVAL;
2944
2945         if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
2946                 return -1;
2947
2948         ieee80211_set_disassoc(sdata, ifsta, false, true, reason);
2949         return 0;
2950 }
2951
2952 void ieee80211_notify_mac(struct ieee80211_hw *hw,
2953                           enum ieee80211_notification_types  notif_type)
2954 {
2955         struct ieee80211_local *local = hw_to_local(hw);
2956         struct ieee80211_sub_if_data *sdata;
2957
2958         switch (notif_type) {
2959         case IEEE80211_NOTIFY_RE_ASSOC:
2960                 rcu_read_lock();
2961                 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2962                         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
2963                                 continue;
2964
2965                         ieee80211_sta_req_auth(sdata, &sdata->u.sta);
2966                 }
2967                 rcu_read_unlock();
2968                 break;
2969         }
2970 }
2971 EXPORT_SYMBOL(ieee80211_notify_mac);
2972
2973 void ieee80211_sta_work(struct work_struct *work)
2974 {
2975         struct ieee80211_sub_if_data *sdata =
2976                 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2977         struct ieee80211_local *local = sdata->local;
2978         struct ieee80211_if_sta *ifsta;
2979         struct sk_buff *skb;
2980
2981         if (!netif_running(sdata->dev))
2982                 return;
2983
2984         if (local->sta_sw_scanning || local->sta_hw_scanning)
2985                 return;
2986
2987         if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2988                     sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2989                     sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
2990                 return;
2991         ifsta = &sdata->u.sta;
2992
2993         while ((skb = skb_dequeue(&ifsta->skb_queue)))
2994                 ieee80211_sta_rx_queued_mgmt(sdata, skb);
2995
2996 #ifdef CONFIG_MAC80211_MESH
2997         if (ifsta->preq_queue_len &&
2998             time_after(jiffies,
2999                        ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
3000                 mesh_path_start_discovery(sdata);
3001 #endif
3002
3003         if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
3004             ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
3005             ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
3006             test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
3007                 ieee80211_sta_start_scan(sdata, ifsta->scan_ssid, ifsta->scan_ssid_len);
3008                 return;
3009         }
3010
3011         if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3012                 if (ieee80211_sta_config_auth(sdata, ifsta))
3013                         return;
3014                 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3015         } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3016                 return;
3017
3018         switch (ifsta->state) {
3019         case IEEE80211_STA_MLME_DISABLED:
3020                 break;
3021         case IEEE80211_STA_MLME_DIRECT_PROBE:
3022                 ieee80211_direct_probe(sdata, ifsta);
3023                 break;
3024         case IEEE80211_STA_MLME_AUTHENTICATE:
3025                 ieee80211_authenticate(sdata, ifsta);
3026                 break;
3027         case IEEE80211_STA_MLME_ASSOCIATE:
3028                 ieee80211_associate(sdata, ifsta);
3029                 break;
3030         case IEEE80211_STA_MLME_ASSOCIATED:
3031                 ieee80211_associated(sdata, ifsta);
3032                 break;
3033         case IEEE80211_STA_MLME_IBSS_SEARCH:
3034                 ieee80211_sta_find_ibss(sdata, ifsta);
3035                 break;
3036         case IEEE80211_STA_MLME_IBSS_JOINED:
3037                 ieee80211_sta_merge_ibss(sdata, ifsta);
3038                 break;
3039 #ifdef CONFIG_MAC80211_MESH
3040         case IEEE80211_STA_MLME_MESH_UP:
3041                 ieee80211_mesh_housekeeping(sdata, ifsta);
3042                 break;
3043 #endif
3044         default:
3045                 WARN_ON(1);
3046                 break;
3047         }
3048
3049         if (ieee80211_privacy_mismatch(sdata, ifsta)) {
3050                 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3051                        "mixed-cell disabled - disassociate\n", sdata->dev->name);
3052
3053                 ieee80211_set_disassoc(sdata, ifsta, false, true,
3054                                         WLAN_REASON_UNSPECIFIED);
3055         }
3056 }
3057
3058 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3059 {
3060         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
3061         struct ieee80211_if_sta *ifsta;
3062
3063         if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
3064                 ifsta = &sdata->u.sta;
3065                 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
3066                     (!(ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED) &&
3067                     !ieee80211_sta_active_ibss(sdata)))
3068                         ieee80211_sta_find_ibss(sdata, ifsta);
3069         }
3070 }