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