~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/mac80211/mlme.c

Version: ~ [ linux-5.10-rc5 ] ~ [ linux-5.9.10 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.79 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.159 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.208 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.245 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.245 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * BSS client mode implementation
  3  * Copyright 2003-2008, Jouni Malinen <j@w1.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/if_arp.h>
 18 #include <linux/etherdevice.h>
 19 #include <linux/rtnetlink.h>
 20 #include <linux/pm_qos_params.h>
 21 #include <linux/crc32.h>
 22 #include <net/mac80211.h>
 23 #include <asm/unaligned.h>
 24 
 25 #include "ieee80211_i.h"
 26 #include "driver-ops.h"
 27 #include "rate.h"
 28 #include "led.h"
 29 
 30 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
 31 #define IEEE80211_AUTH_MAX_TRIES 3
 32 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
 33 #define IEEE80211_ASSOC_MAX_TRIES 3
 34 #define IEEE80211_MAX_PROBE_TRIES 5
 35 
 36 /*
 37  * beacon loss detection timeout
 38  * XXX: should depend on beacon interval
 39  */
 40 #define IEEE80211_BEACON_LOSS_TIME      (2 * HZ)
 41 /*
 42  * Time the connection can be idle before we probe
 43  * it to see if we can still talk to the AP.
 44  */
 45 #define IEEE80211_CONNECTION_IDLE_TIME  (30 * HZ)
 46 /*
 47  * Time we wait for a probe response after sending
 48  * a probe request because of beacon loss or for
 49  * checking the connection still works.
 50  */
 51 #define IEEE80211_PROBE_WAIT            (HZ / 2)
 52 
 53 #define TMR_RUNNING_TIMER       0
 54 #define TMR_RUNNING_CHANSW      1
 55 
 56 /*
 57  * All cfg80211 functions have to be called outside a locked
 58  * section so that they can acquire a lock themselves... This
 59  * is much simpler than queuing up things in cfg80211, but we
 60  * do need some indirection for that here.
 61  */
 62 enum rx_mgmt_action {
 63         /* no action required */
 64         RX_MGMT_NONE,
 65 
 66         /* caller must call cfg80211_send_rx_auth() */
 67         RX_MGMT_CFG80211_AUTH,
 68 
 69         /* caller must call cfg80211_send_rx_assoc() */
 70         RX_MGMT_CFG80211_ASSOC,
 71 
 72         /* caller must call cfg80211_send_deauth() */
 73         RX_MGMT_CFG80211_DEAUTH,
 74 
 75         /* caller must call cfg80211_send_disassoc() */
 76         RX_MGMT_CFG80211_DISASSOC,
 77 
 78         /* caller must call cfg80211_auth_timeout() & free work */
 79         RX_MGMT_CFG80211_AUTH_TO,
 80 
 81         /* caller must call cfg80211_assoc_timeout() & free work */
 82         RX_MGMT_CFG80211_ASSOC_TO,
 83 };
 84 
 85 /* utils */
 86 static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
 87 {
 88         WARN_ON(!mutex_is_locked(&ifmgd->mtx));
 89 }
 90 
 91 /*
 92  * We can have multiple work items (and connection probing)
 93  * scheduling this timer, but we need to take care to only
 94  * reschedule it when it should fire _earlier_ than it was
 95  * asked for before, or if it's not pending right now. This
 96  * function ensures that. Note that it then is required to
 97  * run this function for all timeouts after the first one
 98  * has happened -- the work that runs from this timer will
 99  * do that.
100  */
101 static void run_again(struct ieee80211_if_managed *ifmgd,
102                              unsigned long timeout)
103 {
104         ASSERT_MGD_MTX(ifmgd);
105 
106         if (!timer_pending(&ifmgd->timer) ||
107             time_before(timeout, ifmgd->timer.expires))
108                 mod_timer(&ifmgd->timer, timeout);
109 }
110 
111 static void mod_beacon_timer(struct ieee80211_sub_if_data *sdata)
112 {
113         if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
114                 return;
115 
116         mod_timer(&sdata->u.mgd.bcn_mon_timer,
117                   round_jiffies_up(jiffies + IEEE80211_BEACON_LOSS_TIME));
118 }
119 
120 static int ecw2cw(int ecw)
121 {
122         return (1 << ecw) - 1;
123 }
124 
125 static int ieee80211_compatible_rates(struct ieee80211_bss *bss,
126                                       struct ieee80211_supported_band *sband,
127                                       u32 *rates)
128 {
129         int i, j, count;
130         *rates = 0;
131         count = 0;
132         for (i = 0; i < bss->supp_rates_len; i++) {
133                 int rate = (bss->supp_rates[i] & 0x7F) * 5;
134 
135                 for (j = 0; j < sband->n_bitrates; j++)
136                         if (sband->bitrates[j].bitrate == rate) {
137                                 *rates |= BIT(j);
138                                 count++;
139                                 break;
140                         }
141         }
142 
143         return count;
144 }
145 
146 /*
147  * ieee80211_enable_ht should be called only after the operating band
148  * has been determined as ht configuration depends on the hw's
149  * HT abilities for a specific band.
150  */
151 static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
152                                struct ieee80211_ht_info *hti,
153                                const u8 *bssid, u16 ap_ht_cap_flags)
154 {
155         struct ieee80211_local *local = sdata->local;
156         struct ieee80211_supported_band *sband;
157         struct sta_info *sta;
158         u32 changed = 0;
159         u16 ht_opmode;
160         bool enable_ht = true, ht_changed;
161         enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
162 
163         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
164 
165         /* HT is not supported */
166         if (!sband->ht_cap.ht_supported)
167                 enable_ht = false;
168 
169         /* check that channel matches the right operating channel */
170         if (local->hw.conf.channel->center_freq !=
171             ieee80211_channel_to_frequency(hti->control_chan))
172                 enable_ht = false;
173 
174         if (enable_ht) {
175                 channel_type = NL80211_CHAN_HT20;
176 
177                 if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
178                     (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
179                     (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
180                         switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
181                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
182                                 if (!(local->hw.conf.channel->flags &
183                                     IEEE80211_CHAN_NO_HT40PLUS))
184                                         channel_type = NL80211_CHAN_HT40PLUS;
185                                 break;
186                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
187                                 if (!(local->hw.conf.channel->flags &
188                                     IEEE80211_CHAN_NO_HT40MINUS))
189                                         channel_type = NL80211_CHAN_HT40MINUS;
190                                 break;
191                         }
192                 }
193         }
194 
195         ht_changed = conf_is_ht(&local->hw.conf) != enable_ht ||
196                      channel_type != local->hw.conf.channel_type;
197 
198         local->oper_channel_type = channel_type;
199 
200         if (ht_changed) {
201                 /* channel_type change automatically detected */
202                 ieee80211_hw_config(local, 0);
203 
204                 rcu_read_lock();
205                 sta = sta_info_get(local, bssid);
206                 if (sta)
207                         rate_control_rate_update(local, sband, sta,
208                                                  IEEE80211_RC_HT_CHANGED);
209                 rcu_read_unlock();
210         }
211 
212         /* disable HT */
213         if (!enable_ht)
214                 return 0;
215 
216         ht_opmode = le16_to_cpu(hti->operation_mode);
217 
218         /* if bss configuration changed store the new one */
219         if (!sdata->ht_opmode_valid ||
220             sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
221                 changed |= BSS_CHANGED_HT;
222                 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
223                 sdata->ht_opmode_valid = true;
224         }
225 
226         return changed;
227 }
228 
229 /* frame sending functions */
230 
231 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
232                                  struct ieee80211_mgd_work *wk)
233 {
234         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
235         struct ieee80211_local *local = sdata->local;
236         struct sk_buff *skb;
237         struct ieee80211_mgmt *mgmt;
238         u8 *pos;
239         const u8 *ies, *ht_ie;
240         int i, len, count, rates_len, supp_rates_len;
241         u16 capab;
242         int wmm = 0;
243         struct ieee80211_supported_band *sband;
244         u32 rates = 0;
245 
246         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
247                             sizeof(*mgmt) + 200 + wk->ie_len +
248                             wk->ssid_len);
249         if (!skb) {
250                 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
251                        "frame\n", sdata->dev->name);
252                 return;
253         }
254         skb_reserve(skb, local->hw.extra_tx_headroom);
255 
256         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
257 
258         capab = ifmgd->capab;
259 
260         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
261                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
262                         capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
263                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
264                         capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
265         }
266 
267         if (wk->bss->cbss.capability & WLAN_CAPABILITY_PRIVACY)
268                 capab |= WLAN_CAPABILITY_PRIVACY;
269         if (wk->bss->wmm_used)
270                 wmm = 1;
271 
272         if ((wk->bss->cbss.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
273             (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
274                 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
275 
276         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
277         memset(mgmt, 0, 24);
278         memcpy(mgmt->da, wk->bss->cbss.bssid, ETH_ALEN);
279         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
280         memcpy(mgmt->bssid, wk->bss->cbss.bssid, ETH_ALEN);
281 
282         if (!is_zero_ether_addr(wk->prev_bssid)) {
283                 skb_put(skb, 10);
284                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
285                                                   IEEE80211_STYPE_REASSOC_REQ);
286                 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
287                 mgmt->u.reassoc_req.listen_interval =
288                                 cpu_to_le16(local->hw.conf.listen_interval);
289                 memcpy(mgmt->u.reassoc_req.current_ap, wk->prev_bssid,
290                        ETH_ALEN);
291         } else {
292                 skb_put(skb, 4);
293                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
294                                                   IEEE80211_STYPE_ASSOC_REQ);
295                 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
296                 mgmt->u.assoc_req.listen_interval =
297                                 cpu_to_le16(local->hw.conf.listen_interval);
298         }
299 
300         /* SSID */
301         ies = pos = skb_put(skb, 2 + wk->ssid_len);
302         *pos++ = WLAN_EID_SSID;
303         *pos++ = wk->ssid_len;
304         memcpy(pos, wk->ssid, wk->ssid_len);
305 
306         if (wk->bss->supp_rates_len) {
307                 /* get all rates supported by the device and the AP as
308                  * some APs don't like getting a superset of their rates
309                  * in the association request (e.g. D-Link DAP 1353 in
310                  * b-only mode) */
311                 rates_len = ieee80211_compatible_rates(wk->bss, sband, &rates);
312         } else {
313                 rates = ~0;
314                 rates_len = sband->n_bitrates;
315         }
316 
317         /* add all rates which were marked to be used above */
318         supp_rates_len = rates_len;
319         if (supp_rates_len > 8)
320                 supp_rates_len = 8;
321 
322         len = sband->n_bitrates;
323         pos = skb_put(skb, supp_rates_len + 2);
324         *pos++ = WLAN_EID_SUPP_RATES;
325         *pos++ = supp_rates_len;
326 
327         count = 0;
328         for (i = 0; i < sband->n_bitrates; i++) {
329                 if (BIT(i) & rates) {
330                         int rate = sband->bitrates[i].bitrate;
331                         *pos++ = (u8) (rate / 5);
332                         if (++count == 8)
333                                 break;
334                 }
335         }
336 
337         if (rates_len > count) {
338                 pos = skb_put(skb, rates_len - count + 2);
339                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
340                 *pos++ = rates_len - count;
341 
342                 for (i++; i < sband->n_bitrates; i++) {
343                         if (BIT(i) & rates) {
344                                 int rate = sband->bitrates[i].bitrate;
345                                 *pos++ = (u8) (rate / 5);
346                         }
347                 }
348         }
349 
350         if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
351                 /* 1. power capabilities */
352                 pos = skb_put(skb, 4);
353                 *pos++ = WLAN_EID_PWR_CAPABILITY;
354                 *pos++ = 2;
355                 *pos++ = 0; /* min tx power */
356                 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
357 
358                 /* 2. supported channels */
359                 /* TODO: get this in reg domain format */
360                 pos = skb_put(skb, 2 * sband->n_channels + 2);
361                 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
362                 *pos++ = 2 * sband->n_channels;
363                 for (i = 0; i < sband->n_channels; i++) {
364                         *pos++ = ieee80211_frequency_to_channel(
365                                         sband->channels[i].center_freq);
366                         *pos++ = 1; /* one channel in the subband*/
367                 }
368         }
369 
370         if (wk->ie_len && wk->ie) {
371                 pos = skb_put(skb, wk->ie_len);
372                 memcpy(pos, wk->ie, wk->ie_len);
373         }
374 
375         if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED)) {
376                 pos = skb_put(skb, 9);
377                 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
378                 *pos++ = 7; /* len */
379                 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
380                 *pos++ = 0x50;
381                 *pos++ = 0xf2;
382                 *pos++ = 2; /* WME */
383                 *pos++ = 0; /* WME info */
384                 *pos++ = 1; /* WME ver */
385                 *pos++ = 0;
386         }
387 
388         /* wmm support is a must to HT */
389         /*
390          * IEEE802.11n does not allow TKIP/WEP as pairwise
391          * ciphers in HT mode. We still associate in non-ht
392          * mode (11a/b/g) if any one of these ciphers is
393          * configured as pairwise.
394          */
395         if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
396             sband->ht_cap.ht_supported &&
397             (ht_ie = ieee80211_bss_get_ie(&wk->bss->cbss, WLAN_EID_HT_INFORMATION)) &&
398             ht_ie[1] >= sizeof(struct ieee80211_ht_info) &&
399             (!(ifmgd->flags & IEEE80211_STA_DISABLE_11N))) {
400                 struct ieee80211_ht_info *ht_info =
401                         (struct ieee80211_ht_info *)(ht_ie + 2);
402                 u16 cap = sband->ht_cap.cap;
403                 __le16 tmp;
404                 u32 flags = local->hw.conf.channel->flags;
405 
406                 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
407                 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
408                         if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
409                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
410                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
411                         }
412                         break;
413                 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
414                         if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
415                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
416                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
417                         }
418                         break;
419                 }
420 
421                 tmp = cpu_to_le16(cap);
422                 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
423                 *pos++ = WLAN_EID_HT_CAPABILITY;
424                 *pos++ = sizeof(struct ieee80211_ht_cap);
425                 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
426                 memcpy(pos, &tmp, sizeof(u16));
427                 pos += sizeof(u16);
428                 /* TODO: needs a define here for << 2 */
429                 *pos++ = sband->ht_cap.ampdu_factor |
430                          (sband->ht_cap.ampdu_density << 2);
431                 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
432         }
433 
434         ieee80211_tx_skb(sdata, skb, 0);
435 }
436 
437 
438 static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
439                                            const u8 *bssid, u16 stype, u16 reason,
440                                            void *cookie)
441 {
442         struct ieee80211_local *local = sdata->local;
443         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
444         struct sk_buff *skb;
445         struct ieee80211_mgmt *mgmt;
446 
447         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
448         if (!skb) {
449                 printk(KERN_DEBUG "%s: failed to allocate buffer for "
450                        "deauth/disassoc frame\n", sdata->dev->name);
451                 return;
452         }
453         skb_reserve(skb, local->hw.extra_tx_headroom);
454 
455         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
456         memset(mgmt, 0, 24);
457         memcpy(mgmt->da, bssid, ETH_ALEN);
458         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
459         memcpy(mgmt->bssid, bssid, ETH_ALEN);
460         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
461         skb_put(skb, 2);
462         /* u.deauth.reason_code == u.disassoc.reason_code */
463         mgmt->u.deauth.reason_code = cpu_to_le16(reason);
464 
465         if (stype == IEEE80211_STYPE_DEAUTH)
466                 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len, cookie);
467         else
468                 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len, cookie);
469         ieee80211_tx_skb(sdata, skb, ifmgd->flags & IEEE80211_STA_MFP_ENABLED);
470 }
471 
472 void ieee80211_send_pspoll(struct ieee80211_local *local,
473                            struct ieee80211_sub_if_data *sdata)
474 {
475         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
476         struct ieee80211_pspoll *pspoll;
477         struct sk_buff *skb;
478         u16 fc;
479 
480         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
481         if (!skb) {
482                 printk(KERN_DEBUG "%s: failed to allocate buffer for "
483                        "pspoll frame\n", sdata->dev->name);
484                 return;
485         }
486         skb_reserve(skb, local->hw.extra_tx_headroom);
487 
488         pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
489         memset(pspoll, 0, sizeof(*pspoll));
490         fc = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL | IEEE80211_FCTL_PM;
491         pspoll->frame_control = cpu_to_le16(fc);
492         pspoll->aid = cpu_to_le16(ifmgd->aid);
493 
494         /* aid in PS-Poll has its two MSBs each set to 1 */
495         pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
496 
497         memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
498         memcpy(pspoll->ta, sdata->dev->dev_addr, ETH_ALEN);
499 
500         ieee80211_tx_skb(sdata, skb, 0);
501 }
502 
503 void ieee80211_send_nullfunc(struct ieee80211_local *local,
504                              struct ieee80211_sub_if_data *sdata,
505                              int powersave)
506 {
507         struct sk_buff *skb;
508         struct ieee80211_hdr *nullfunc;
509         __le16 fc;
510 
511         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
512                 return;
513 
514         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
515         if (!skb) {
516                 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
517                        "frame\n", sdata->dev->name);
518                 return;
519         }
520         skb_reserve(skb, local->hw.extra_tx_headroom);
521 
522         nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
523         memset(nullfunc, 0, 24);
524         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
525                          IEEE80211_FCTL_TODS);
526         if (powersave)
527                 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
528         nullfunc->frame_control = fc;
529         memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
530         memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
531         memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
532 
533         ieee80211_tx_skb(sdata, skb, 0);
534 }
535 
536 /* spectrum management related things */
537 static void ieee80211_chswitch_work(struct work_struct *work)
538 {
539         struct ieee80211_sub_if_data *sdata =
540                 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
541         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
542 
543         if (!netif_running(sdata->dev))
544                 return;
545 
546         mutex_lock(&ifmgd->mtx);
547         if (!ifmgd->associated)
548                 goto out;
549 
550         sdata->local->oper_channel = sdata->local->csa_channel;
551         ieee80211_hw_config(sdata->local, IEEE80211_CONF_CHANGE_CHANNEL);
552 
553         /* XXX: shouldn't really modify cfg80211-owned data! */
554         ifmgd->associated->cbss.channel = sdata->local->oper_channel;
555 
556         ieee80211_wake_queues_by_reason(&sdata->local->hw,
557                                         IEEE80211_QUEUE_STOP_REASON_CSA);
558  out:
559         ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
560         mutex_unlock(&ifmgd->mtx);
561 }
562 
563 static void ieee80211_chswitch_timer(unsigned long data)
564 {
565         struct ieee80211_sub_if_data *sdata =
566                 (struct ieee80211_sub_if_data *) data;
567         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
568 
569         if (sdata->local->quiescing) {
570                 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
571                 return;
572         }
573 
574         ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
575 }
576 
577 void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
578                                       struct ieee80211_channel_sw_ie *sw_elem,
579                                       struct ieee80211_bss *bss)
580 {
581         struct ieee80211_channel *new_ch;
582         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
583         int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num);
584 
585         ASSERT_MGD_MTX(ifmgd);
586 
587         if (!ifmgd->associated)
588                 return;
589 
590         if (sdata->local->scanning)
591                 return;
592 
593         /* Disregard subsequent beacons if we are already running a timer
594            processing a CSA */
595 
596         if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
597                 return;
598 
599         new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
600         if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
601                 return;
602 
603         sdata->local->csa_channel = new_ch;
604 
605         if (sw_elem->count <= 1) {
606                 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
607         } else {
608                 ieee80211_stop_queues_by_reason(&sdata->local->hw,
609                                         IEEE80211_QUEUE_STOP_REASON_CSA);
610                 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
611                 mod_timer(&ifmgd->chswitch_timer,
612                           jiffies +
613                           msecs_to_jiffies(sw_elem->count *
614                                            bss->cbss.beacon_interval));
615         }
616 }
617 
618 static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
619                                         u16 capab_info, u8 *pwr_constr_elem,
620                                         u8 pwr_constr_elem_len)
621 {
622         struct ieee80211_conf *conf = &sdata->local->hw.conf;
623 
624         if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
625                 return;
626 
627         /* Power constraint IE length should be 1 octet */
628         if (pwr_constr_elem_len != 1)
629                 return;
630 
631         if ((*pwr_constr_elem <= conf->channel->max_power) &&
632             (*pwr_constr_elem != sdata->local->power_constr_level)) {
633                 sdata->local->power_constr_level = *pwr_constr_elem;
634                 ieee80211_hw_config(sdata->local, 0);
635         }
636 }
637 
638 /* powersave */
639 static void ieee80211_enable_ps(struct ieee80211_local *local,
640                                 struct ieee80211_sub_if_data *sdata)
641 {
642         struct ieee80211_conf *conf = &local->hw.conf;
643 
644         /*
645          * If we are scanning right now then the parameters will
646          * take effect when scan finishes.
647          */
648         if (local->scanning)
649                 return;
650 
651         if (conf->dynamic_ps_timeout > 0 &&
652             !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
653                 mod_timer(&local->dynamic_ps_timer, jiffies +
654                           msecs_to_jiffies(conf->dynamic_ps_timeout));
655         } else {
656                 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
657                         ieee80211_send_nullfunc(local, sdata, 1);
658 
659                 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
660                         conf->flags |= IEEE80211_CONF_PS;
661                         ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
662                 }
663         }
664 }
665 
666 static void ieee80211_change_ps(struct ieee80211_local *local)
667 {
668         struct ieee80211_conf *conf = &local->hw.conf;
669 
670         if (local->ps_sdata) {
671                 ieee80211_enable_ps(local, local->ps_sdata);
672         } else if (conf->flags & IEEE80211_CONF_PS) {
673                 conf->flags &= ~IEEE80211_CONF_PS;
674                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
675                 del_timer_sync(&local->dynamic_ps_timer);
676                 cancel_work_sync(&local->dynamic_ps_enable_work);
677         }
678 }
679 
680 /* need to hold RTNL or interface lock */
681 void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
682 {
683         struct ieee80211_sub_if_data *sdata, *found = NULL;
684         int count = 0;
685 
686         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
687                 local->ps_sdata = NULL;
688                 return;
689         }
690 
691         list_for_each_entry(sdata, &local->interfaces, list) {
692                 if (!netif_running(sdata->dev))
693                         continue;
694                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
695                         continue;
696                 found = sdata;
697                 count++;
698         }
699 
700         if (count == 1 && found->u.mgd.powersave &&
701             found->u.mgd.associated && list_empty(&found->u.mgd.work_list) &&
702             !(found->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
703                                     IEEE80211_STA_CONNECTION_POLL))) {
704                 s32 beaconint_us;
705 
706                 if (latency < 0)
707                         latency = pm_qos_requirement(PM_QOS_NETWORK_LATENCY);
708 
709                 beaconint_us = ieee80211_tu_to_usec(
710                                         found->vif.bss_conf.beacon_int);
711 
712                 if (beaconint_us > latency) {
713                         local->ps_sdata = NULL;
714                 } else {
715                         u8 dtimper = found->vif.bss_conf.dtim_period;
716                         int maxslp = 1;
717 
718                         if (dtimper > 1)
719                                 maxslp = min_t(int, dtimper,
720                                                     latency / beaconint_us);
721 
722                         local->hw.conf.max_sleep_period = maxslp;
723                         local->ps_sdata = found;
724                 }
725         } else {
726                 local->ps_sdata = NULL;
727         }
728 
729         ieee80211_change_ps(local);
730 }
731 
732 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
733 {
734         struct ieee80211_local *local =
735                 container_of(work, struct ieee80211_local,
736                              dynamic_ps_disable_work);
737 
738         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
739                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
740                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
741         }
742 
743         ieee80211_wake_queues_by_reason(&local->hw,
744                                         IEEE80211_QUEUE_STOP_REASON_PS);
745 }
746 
747 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
748 {
749         struct ieee80211_local *local =
750                 container_of(work, struct ieee80211_local,
751                              dynamic_ps_enable_work);
752         struct ieee80211_sub_if_data *sdata = local->ps_sdata;
753         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
754 
755         /* can only happen when PS was just disabled anyway */
756         if (!sdata)
757                 return;
758 
759         if (local->hw.conf.flags & IEEE80211_CONF_PS)
760                 return;
761 
762         if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
763             (!(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)))
764                 ieee80211_send_nullfunc(local, sdata, 1);
765 
766         if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) ||
767             (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
768                 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
769                 local->hw.conf.flags |= IEEE80211_CONF_PS;
770                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
771         }
772 }
773 
774 void ieee80211_dynamic_ps_timer(unsigned long data)
775 {
776         struct ieee80211_local *local = (void *) data;
777 
778         if (local->quiescing || local->suspended)
779                 return;
780 
781         ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
782 }
783 
784 /* MLME */
785 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
786                                      struct ieee80211_if_managed *ifmgd,
787                                      u8 *wmm_param, size_t wmm_param_len)
788 {
789         struct ieee80211_tx_queue_params params;
790         size_t left;
791         int count;
792         u8 *pos;
793 
794         if (!(ifmgd->flags & IEEE80211_STA_WMM_ENABLED))
795                 return;
796 
797         if (!wmm_param)
798                 return;
799 
800         if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
801                 return;
802         count = wmm_param[6] & 0x0f;
803         if (count == ifmgd->wmm_last_param_set)
804                 return;
805         ifmgd->wmm_last_param_set = count;
806 
807         pos = wmm_param + 8;
808         left = wmm_param_len - 8;
809 
810         memset(&params, 0, sizeof(params));
811 
812         local->wmm_acm = 0;
813         for (; left >= 4; left -= 4, pos += 4) {
814                 int aci = (pos[0] >> 5) & 0x03;
815                 int acm = (pos[0] >> 4) & 0x01;
816                 int queue;
817 
818                 switch (aci) {
819                 case 1: /* AC_BK */
820                         queue = 3;
821                         if (acm)
822                                 local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
823                         break;
824                 case 2: /* AC_VI */
825                         queue = 1;
826                         if (acm)
827                                 local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
828                         break;
829                 case 3: /* AC_VO */
830                         queue = 0;
831                         if (acm)
832                                 local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
833                         break;
834                 case 0: /* AC_BE */
835                 default:
836                         queue = 2;
837                         if (acm)
838                                 local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
839                         break;
840                 }
841 
842                 params.aifs = pos[0] & 0x0f;
843                 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
844                 params.cw_min = ecw2cw(pos[1] & 0x0f);
845                 params.txop = get_unaligned_le16(pos + 2);
846 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
847                 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
848                        "cWmin=%d cWmax=%d txop=%d\n",
849                        wiphy_name(local->hw.wiphy), queue, aci, acm,
850                        params.aifs, params.cw_min, params.cw_max, params.txop);
851 #endif
852                 if (drv_conf_tx(local, queue, &params) && local->ops->conf_tx)
853                         printk(KERN_DEBUG "%s: failed to set TX queue "
854                                "parameters for queue %d\n",
855                                wiphy_name(local->hw.wiphy), queue);
856         }
857 }
858 
859 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
860                                            u16 capab, bool erp_valid, u8 erp)
861 {
862         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
863         u32 changed = 0;
864         bool use_protection;
865         bool use_short_preamble;
866         bool use_short_slot;
867 
868         if (erp_valid) {
869                 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
870                 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
871         } else {
872                 use_protection = false;
873                 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
874         }
875 
876         use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
877 
878         if (use_protection != bss_conf->use_cts_prot) {
879                 bss_conf->use_cts_prot = use_protection;
880                 changed |= BSS_CHANGED_ERP_CTS_PROT;
881         }
882 
883         if (use_short_preamble != bss_conf->use_short_preamble) {
884                 bss_conf->use_short_preamble = use_short_preamble;
885                 changed |= BSS_CHANGED_ERP_PREAMBLE;
886         }
887 
888         if (use_short_slot != bss_conf->use_short_slot) {
889                 bss_conf->use_short_slot = use_short_slot;
890                 changed |= BSS_CHANGED_ERP_SLOT;
891         }
892 
893         return changed;
894 }
895 
896 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
897                                      struct ieee80211_mgd_work *wk,
898                                      u32 bss_info_changed)
899 {
900         struct ieee80211_local *local = sdata->local;
901         struct ieee80211_bss *bss = wk->bss;
902 
903         bss_info_changed |= BSS_CHANGED_ASSOC;
904         /* set timing information */
905         sdata->vif.bss_conf.beacon_int = bss->cbss.beacon_interval;
906         sdata->vif.bss_conf.timestamp = bss->cbss.tsf;
907         sdata->vif.bss_conf.dtim_period = bss->dtim_period;
908 
909         bss_info_changed |= BSS_CHANGED_BEACON_INT;
910         bss_info_changed |= ieee80211_handle_bss_capability(sdata,
911                 bss->cbss.capability, bss->has_erp_value, bss->erp_value);
912 
913         sdata->u.mgd.associated = bss;
914         sdata->u.mgd.old_associate_work = wk;
915         memcpy(sdata->u.mgd.bssid, bss->cbss.bssid, ETH_ALEN);
916 
917         /* just to be sure */
918         sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
919                                 IEEE80211_STA_BEACON_POLL);
920 
921         /*
922          * Always handle WMM once after association regardless
923          * of the first value the AP uses. Setting -1 here has
924          * that effect because the AP values is an unsigned
925          * 4-bit value.
926          */
927         sdata->u.mgd.wmm_last_param_set = -1;
928 
929         ieee80211_led_assoc(local, 1);
930 
931         sdata->vif.bss_conf.assoc = 1;
932         /*
933          * For now just always ask the driver to update the basic rateset
934          * when we have associated, we aren't checking whether it actually
935          * changed or not.
936          */
937         bss_info_changed |= BSS_CHANGED_BASIC_RATES;
938 
939         /* And the BSSID changed - we're associated now */
940         bss_info_changed |= BSS_CHANGED_BSSID;
941 
942         ieee80211_bss_info_change_notify(sdata, bss_info_changed);
943 
944         mutex_lock(&local->iflist_mtx);
945         ieee80211_recalc_ps(local, -1);
946         mutex_unlock(&local->iflist_mtx);
947 
948         netif_tx_start_all_queues(sdata->dev);
949         netif_carrier_on(sdata->dev);
950 }
951 
952 static enum rx_mgmt_action __must_check
953 ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
954                        struct ieee80211_mgd_work *wk)
955 {
956         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
957         struct ieee80211_local *local = sdata->local;
958 
959         wk->tries++;
960         if (wk->tries > IEEE80211_AUTH_MAX_TRIES) {
961                 printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
962                        sdata->dev->name, wk->bss->cbss.bssid);
963 
964                 /*
965                  * Most likely AP is not in the range so remove the
966                  * bss struct for that AP.
967                  */
968                 cfg80211_unlink_bss(local->hw.wiphy, &wk->bss->cbss);
969 
970                 /*
971                  * We might have a pending scan which had no chance to run yet
972                  * due to work needing to be done. Hence, queue the STAs work
973                  * again for that.
974                  */
975                 ieee80211_queue_work(&local->hw, &ifmgd->work);
976                 return RX_MGMT_CFG80211_AUTH_TO;
977         }
978 
979         printk(KERN_DEBUG "%s: direct probe to AP %pM (try %d)\n",
980                         sdata->dev->name, wk->bss->cbss.bssid,
981                         wk->tries);
982 
983         /*
984          * Direct probe is sent to broadcast address as some APs
985          * will not answer to direct packet in unassociated state.
986          */
987         ieee80211_send_probe_req(sdata, NULL, wk->ssid, wk->ssid_len, NULL, 0);
988 
989         wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
990         run_again(ifmgd, wk->timeout);
991 
992         return RX_MGMT_NONE;
993 }
994 
995 
996 static enum rx_mgmt_action __must_check
997 ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
998                        struct ieee80211_mgd_work *wk)
999 {
1000         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1001         struct ieee80211_local *local = sdata->local;
1002 
1003         wk->tries++;
1004         if (wk->tries > IEEE80211_AUTH_MAX_TRIES) {
1005                 printk(KERN_DEBUG "%s: authentication with AP %pM"
1006                        " timed out\n",
1007                        sdata->dev->name, wk->bss->cbss.bssid);
1008 
1009                 /*
1010                  * Most likely AP is not in the range so remove the
1011                  * bss struct for that AP.
1012                  */
1013                 cfg80211_unlink_bss(local->hw.wiphy, &wk->bss->cbss);
1014 
1015                 /*
1016                  * We might have a pending scan which had no chance to run yet
1017                  * due to work needing to be done. Hence, queue the STAs work
1018                  * again for that.
1019                  */
1020                 ieee80211_queue_work(&local->hw, &ifmgd->work);
1021                 return RX_MGMT_CFG80211_AUTH_TO;
1022         }
1023 
1024         printk(KERN_DEBUG "%s: authenticate with AP %pM (try %d)\n",
1025                sdata->dev->name, wk->bss->cbss.bssid, wk->tries);
1026 
1027         ieee80211_send_auth(sdata, 1, wk->auth_alg, wk->ie, wk->ie_len,
1028                             wk->bss->cbss.bssid, NULL, 0, 0);
1029         wk->auth_transaction = 2;
1030 
1031         wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
1032         run_again(ifmgd, wk->timeout);
1033 
1034         return RX_MGMT_NONE;
1035 }
1036 
1037 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
1038                                    bool deauth)
1039 {
1040         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1041         struct ieee80211_local *local = sdata->local;
1042         struct sta_info *sta;
1043         u32 changed = 0, config_changed = 0;
1044         u8 bssid[ETH_ALEN];
1045 
1046         ASSERT_MGD_MTX(ifmgd);
1047 
1048         if (WARN_ON(!ifmgd->associated))
1049                 return;
1050 
1051         memcpy(bssid, ifmgd->associated->cbss.bssid, ETH_ALEN);
1052 
1053         ifmgd->associated = NULL;
1054         memset(ifmgd->bssid, 0, ETH_ALEN);
1055 
1056         if (deauth) {
1057                 kfree(ifmgd->old_associate_work);
1058                 ifmgd->old_associate_work = NULL;
1059         } else {
1060                 struct ieee80211_mgd_work *wk = ifmgd->old_associate_work;
1061 
1062                 wk->state = IEEE80211_MGD_STATE_IDLE;
1063                 list_add(&wk->list, &ifmgd->work_list);
1064         }
1065 
1066         /*
1067          * we need to commit the associated = NULL change because the
1068          * scan code uses that to determine whether this iface should
1069          * go to/wake up from powersave or not -- and could otherwise
1070          * wake the queues erroneously.
1071          */
1072         smp_mb();
1073 
1074         /*
1075          * Thus, we can only afterwards stop the queues -- to account
1076          * for the case where another CPU is finishing a scan at this
1077          * time -- we don't want the scan code to enable queues.
1078          */
1079 
1080         netif_tx_stop_all_queues(sdata->dev);
1081         netif_carrier_off(sdata->dev);
1082 
1083         rcu_read_lock();
1084         sta = sta_info_get(local, bssid);
1085         if (sta)
1086                 ieee80211_sta_tear_down_BA_sessions(sta);
1087         rcu_read_unlock();
1088 
1089         changed |= ieee80211_reset_erp_info(sdata);
1090 
1091         ieee80211_led_assoc(local, 0);
1092         changed |= BSS_CHANGED_ASSOC;
1093         sdata->vif.bss_conf.assoc = false;
1094 
1095         ieee80211_set_wmm_default(sdata);
1096 
1097         ieee80211_recalc_idle(local);
1098 
1099         /* channel(_type) changes are handled by ieee80211_hw_config */
1100         local->oper_channel_type = NL80211_CHAN_NO_HT;
1101 
1102         /* on the next assoc, re-program HT parameters */
1103         sdata->ht_opmode_valid = false;
1104 
1105         local->power_constr_level = 0;
1106 
1107         del_timer_sync(&local->dynamic_ps_timer);
1108         cancel_work_sync(&local->dynamic_ps_enable_work);
1109 
1110         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1111                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1112                 config_changed |= IEEE80211_CONF_CHANGE_PS;
1113         }
1114 
1115         ieee80211_hw_config(local, config_changed);
1116 
1117         /* And the BSSID changed -- not very interesting here */
1118         changed |= BSS_CHANGED_BSSID;
1119         ieee80211_bss_info_change_notify(sdata, changed);
1120 
1121         rcu_read_lock();
1122 
1123         sta = sta_info_get(local, bssid);
1124         if (!sta) {
1125                 rcu_read_unlock();
1126                 return;
1127         }
1128 
1129         sta_info_unlink(&sta);
1130 
1131         rcu_read_unlock();
1132 
1133         sta_info_destroy(sta);
1134 }
1135 
1136 static enum rx_mgmt_action __must_check
1137 ieee80211_associate(struct ieee80211_sub_if_data *sdata,
1138                     struct ieee80211_mgd_work *wk)
1139 {
1140         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1141         struct ieee80211_local *local = sdata->local;
1142 
1143         wk->tries++;
1144         if (wk->tries > IEEE80211_ASSOC_MAX_TRIES) {
1145                 printk(KERN_DEBUG "%s: association with AP %pM"
1146                        " timed out\n",
1147                        sdata->dev->name, wk->bss->cbss.bssid);
1148 
1149                 /*
1150                  * Most likely AP is not in the range so remove the
1151                  * bss struct for that AP.
1152                  */
1153                 cfg80211_unlink_bss(local->hw.wiphy, &wk->bss->cbss);
1154 
1155                 /*
1156                  * We might have a pending scan which had no chance to run yet
1157                  * due to work needing to be done. Hence, queue the STAs work
1158                  * again for that.
1159                  */
1160                 ieee80211_queue_work(&local->hw, &ifmgd->work);
1161                 return RX_MGMT_CFG80211_ASSOC_TO;
1162         }
1163 
1164         printk(KERN_DEBUG "%s: associate with AP %pM (try %d)\n",
1165                sdata->dev->name, wk->bss->cbss.bssid, wk->tries);
1166         ieee80211_send_assoc(sdata, wk);
1167 
1168         wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
1169         run_again(ifmgd, wk->timeout);
1170 
1171         return RX_MGMT_NONE;
1172 }
1173 
1174 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1175                              struct ieee80211_hdr *hdr)
1176 {
1177         /*
1178          * We can postpone the mgd.timer whenever receiving unicast frames
1179          * from AP because we know that the connection is working both ways
1180          * at that time. But multicast frames (and hence also beacons) must
1181          * be ignored here, because we need to trigger the timer during
1182          * data idle periods for sending the periodic probe request to the
1183          * AP we're connected to.
1184          */
1185         if (is_multicast_ether_addr(hdr->addr1))
1186                 return;
1187 
1188         mod_timer(&sdata->u.mgd.conn_mon_timer,
1189                   round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
1190 }
1191 
1192 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1193 {
1194         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1195         const u8 *ssid;
1196 
1197         ssid = ieee80211_bss_get_ie(&ifmgd->associated->cbss, WLAN_EID_SSID);
1198         ieee80211_send_probe_req(sdata, ifmgd->associated->cbss.bssid,
1199                                  ssid + 2, ssid[1], NULL, 0);
1200 
1201         ifmgd->probe_send_count++;
1202         ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
1203         run_again(ifmgd, ifmgd->probe_timeout);
1204 }
1205 
1206 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1207                                    bool beacon)
1208 {
1209         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1210         bool already = false;
1211 
1212         if (!netif_running(sdata->dev))
1213                 return;
1214 
1215         if (sdata->local->scanning)
1216                 return;
1217 
1218         mutex_lock(&ifmgd->mtx);
1219 
1220         if (!ifmgd->associated)
1221                 goto out;
1222 
1223 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1224         if (beacon && net_ratelimit())
1225                 printk(KERN_DEBUG "%s: detected beacon loss from AP "
1226                        "- sending probe request\n", sdata->dev->name);
1227 #endif
1228 
1229         /*
1230          * The driver/our work has already reported this event or the
1231          * connection monitoring has kicked in and we have already sent
1232          * a probe request. Or maybe the AP died and the driver keeps
1233          * reporting until we disassociate...
1234          *
1235          * In either case we have to ignore the current call to this
1236          * function (except for setting the correct probe reason bit)
1237          * because otherwise we would reset the timer every time and
1238          * never check whether we received a probe response!
1239          */
1240         if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1241                             IEEE80211_STA_CONNECTION_POLL))
1242                 already = true;
1243 
1244         if (beacon)
1245                 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1246         else
1247                 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1248 
1249         if (already)
1250                 goto out;
1251 
1252         mutex_lock(&sdata->local->iflist_mtx);
1253         ieee80211_recalc_ps(sdata->local, -1);
1254         mutex_unlock(&sdata->local->iflist_mtx);
1255 
1256         ifmgd->probe_send_count = 0;
1257         ieee80211_mgd_probe_ap_send(sdata);
1258  out:
1259         mutex_unlock(&ifmgd->mtx);
1260 }
1261 
1262 void ieee80211_beacon_loss_work(struct work_struct *work)
1263 {
1264         struct ieee80211_sub_if_data *sdata =
1265                 container_of(work, struct ieee80211_sub_if_data,
1266                              u.mgd.beacon_loss_work);
1267 
1268         ieee80211_mgd_probe_ap(sdata, true);
1269 }
1270 
1271 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1272 {
1273         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1274 
1275         ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
1276 }
1277 EXPORT_SYMBOL(ieee80211_beacon_loss);
1278 
1279 static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
1280                                      struct ieee80211_mgd_work *wk)
1281 {
1282         wk->state = IEEE80211_MGD_STATE_IDLE;
1283         printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
1284 }
1285 
1286 
1287 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1288                                      struct ieee80211_mgd_work *wk,
1289                                      struct ieee80211_mgmt *mgmt,
1290                                      size_t len)
1291 {
1292         u8 *pos;
1293         struct ieee802_11_elems elems;
1294 
1295         pos = mgmt->u.auth.variable;
1296         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1297         if (!elems.challenge)
1298                 return;
1299         ieee80211_send_auth(sdata, 3, wk->auth_alg,
1300                             elems.challenge - 2, elems.challenge_len + 2,
1301                             wk->bss->cbss.bssid,
1302                             wk->key, wk->key_len, wk->key_idx);
1303         wk->auth_transaction = 4;
1304 }
1305 
1306 static enum rx_mgmt_action __must_check
1307 ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1308                        struct ieee80211_mgd_work *wk,
1309                        struct ieee80211_mgmt *mgmt, size_t len)
1310 {
1311         u16 auth_alg, auth_transaction, status_code;
1312 
1313         if (wk->state != IEEE80211_MGD_STATE_AUTH)
1314                 return RX_MGMT_NONE;
1315 
1316         if (len < 24 + 6)
1317                 return RX_MGMT_NONE;
1318 
1319         if (memcmp(wk->bss->cbss.bssid, mgmt->sa, ETH_ALEN) != 0)
1320                 return RX_MGMT_NONE;
1321 
1322         if (memcmp(wk->bss->cbss.bssid, mgmt->bssid, ETH_ALEN) != 0)
1323                 return RX_MGMT_NONE;
1324 
1325         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1326         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1327         status_code = le16_to_cpu(mgmt->u.auth.status_code);
1328 
1329         if (auth_alg != wk->auth_alg ||
1330             auth_transaction != wk->auth_transaction)
1331                 return RX_MGMT_NONE;
1332 
1333         if (status_code != WLAN_STATUS_SUCCESS) {
1334                 list_del(&wk->list);
1335                 kfree(wk);
1336                 return RX_MGMT_CFG80211_AUTH;
1337         }
1338 
1339         switch (wk->auth_alg) {
1340         case WLAN_AUTH_OPEN:
1341         case WLAN_AUTH_LEAP:
1342         case WLAN_AUTH_FT:
1343                 ieee80211_auth_completed(sdata, wk);
1344                 return RX_MGMT_CFG80211_AUTH;
1345         case WLAN_AUTH_SHARED_KEY:
1346                 if (wk->auth_transaction == 4) {
1347                         ieee80211_auth_completed(sdata, wk);
1348                         return RX_MGMT_CFG80211_AUTH;
1349                 } else
1350                         ieee80211_auth_challenge(sdata, wk, mgmt, len);
1351                 break;
1352         }
1353 
1354         return RX_MGMT_NONE;
1355 }
1356 
1357 
1358 static enum rx_mgmt_action __must_check
1359 ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1360                          struct ieee80211_mgd_work *wk,
1361                          struct ieee80211_mgmt *mgmt, size_t len)
1362 {
1363         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1364         const u8 *bssid = NULL;
1365         u16 reason_code;
1366 
1367         if (len < 24 + 2)
1368                 return RX_MGMT_NONE;
1369 
1370         ASSERT_MGD_MTX(ifmgd);
1371 
1372         if (wk)
1373                 bssid = wk->bss->cbss.bssid;
1374         else
1375                 bssid = ifmgd->associated->cbss.bssid;
1376 
1377         reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1378 
1379         printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
1380                         sdata->dev->name, bssid, reason_code);
1381 
1382         if (!wk) {
1383                 ieee80211_set_disassoc(sdata, true);
1384         } else {
1385                 list_del(&wk->list);
1386                 kfree(wk);
1387         }
1388 
1389         return RX_MGMT_CFG80211_DEAUTH;
1390 }
1391 
1392 
1393 static enum rx_mgmt_action __must_check
1394 ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1395                            struct ieee80211_mgmt *mgmt, size_t len)
1396 {
1397         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1398         u16 reason_code;
1399 
1400         if (len < 24 + 2)
1401                 return RX_MGMT_NONE;
1402 
1403         ASSERT_MGD_MTX(ifmgd);
1404 
1405         if (WARN_ON(!ifmgd->associated))
1406                 return RX_MGMT_NONE;
1407 
1408         if (WARN_ON(memcmp(ifmgd->associated->cbss.bssid, mgmt->sa, ETH_ALEN)))
1409                 return RX_MGMT_NONE;
1410 
1411         reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1412 
1413         printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
1414                         sdata->dev->name, mgmt->sa, reason_code);
1415 
1416         ieee80211_set_disassoc(sdata, false);
1417         return RX_MGMT_CFG80211_DISASSOC;
1418 }
1419 
1420 
1421 static enum rx_mgmt_action __must_check
1422 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1423                              struct ieee80211_mgd_work *wk,
1424                              struct ieee80211_mgmt *mgmt, size_t len,
1425                              bool reassoc)
1426 {
1427         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1428         struct ieee80211_local *local = sdata->local;
1429         struct ieee80211_supported_band *sband;
1430         struct sta_info *sta;
1431         u32 rates, basic_rates;
1432         u16 capab_info, status_code, aid;
1433         struct ieee802_11_elems elems;
1434         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1435         u8 *pos;
1436         u32 changed = 0;
1437         int i, j;
1438         bool have_higher_than_11mbit = false, newsta = false;
1439         u16 ap_ht_cap_flags;
1440 
1441         /*
1442          * AssocResp and ReassocResp have identical structure, so process both
1443          * of them in this function.
1444          */
1445 
1446         if (len < 24 + 6)
1447                 return RX_MGMT_NONE;
1448 
1449         if (memcmp(wk->bss->cbss.bssid, mgmt->sa, ETH_ALEN) != 0)
1450                 return RX_MGMT_NONE;
1451 
1452         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1453         status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1454         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1455 
1456         printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
1457                "status=%d aid=%d)\n",
1458                sdata->dev->name, reassoc ? "Rea" : "A", mgmt->sa,
1459                capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1460 
1461         pos = mgmt->u.assoc_resp.variable;
1462         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1463 
1464         if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
1465             elems.timeout_int && elems.timeout_int_len == 5 &&
1466             elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
1467                 u32 tu, ms;
1468                 tu = get_unaligned_le32(elems.timeout_int + 1);
1469                 ms = tu * 1024 / 1000;
1470                 printk(KERN_DEBUG "%s: AP rejected association temporarily; "
1471                        "comeback duration %u TU (%u ms)\n",
1472                        sdata->dev->name, tu, ms);
1473                 wk->timeout = jiffies + msecs_to_jiffies(ms);
1474                 if (ms > IEEE80211_ASSOC_TIMEOUT)
1475                         run_again(ifmgd, jiffies + msecs_to_jiffies(ms));
1476                 return RX_MGMT_NONE;
1477         }
1478 
1479         if (status_code != WLAN_STATUS_SUCCESS) {
1480                 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1481                        sdata->dev->name, status_code);
1482                 wk->state = IEEE80211_MGD_STATE_IDLE;
1483                 return RX_MGMT_CFG80211_ASSOC;
1484         }
1485 
1486         if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1487                 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1488                        "set\n", sdata->dev->name, aid);
1489         aid &= ~(BIT(15) | BIT(14));
1490 
1491         if (!elems.supp_rates) {
1492                 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1493                        sdata->dev->name);
1494                 return RX_MGMT_NONE;
1495         }
1496 
1497         printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
1498         ifmgd->aid = aid;
1499 
1500         rcu_read_lock();
1501 
1502         /* Add STA entry for the AP */
1503         sta = sta_info_get(local, wk->bss->cbss.bssid);
1504         if (!sta) {
1505                 newsta = true;
1506 
1507                 rcu_read_unlock();
1508 
1509                 sta = sta_info_alloc(sdata, wk->bss->cbss.bssid, GFP_KERNEL);
1510                 if (!sta) {
1511                         printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1512                                " the AP\n", sdata->dev->name);
1513                         return RX_MGMT_NONE;
1514                 }
1515 
1516                 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
1517                                    WLAN_STA_ASSOC_AP);
1518                 if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
1519                         set_sta_flags(sta, WLAN_STA_AUTHORIZED);
1520 
1521                 rcu_read_lock();
1522         }
1523 
1524         rates = 0;
1525         basic_rates = 0;
1526         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1527 
1528         for (i = 0; i < elems.supp_rates_len; i++) {
1529                 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1530                 bool is_basic = !!(elems.supp_rates[i] & 0x80);
1531 
1532                 if (rate > 110)
1533                         have_higher_than_11mbit = true;
1534 
1535                 for (j = 0; j < sband->n_bitrates; j++) {
1536                         if (sband->bitrates[j].bitrate == rate) {
1537                                 rates |= BIT(j);
1538                                 if (is_basic)
1539                                         basic_rates |= BIT(j);
1540                                 break;
1541                         }
1542                 }
1543         }
1544 
1545         for (i = 0; i < elems.ext_supp_rates_len; i++) {
1546                 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1547                 bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
1548 
1549                 if (rate > 110)
1550                         have_higher_than_11mbit = true;
1551 
1552                 for (j = 0; j < sband->n_bitrates; j++) {
1553                         if (sband->bitrates[j].bitrate == rate) {
1554                                 rates |= BIT(j);
1555                                 if (is_basic)
1556                                         basic_rates |= BIT(j);
1557                                 break;
1558                         }
1559                 }
1560         }
1561 
1562         sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
1563         sdata->vif.bss_conf.basic_rates = basic_rates;
1564 
1565         /* cf. IEEE 802.11 9.2.12 */
1566         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1567             have_higher_than_11mbit)
1568                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1569         else
1570                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1571 
1572         if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
1573                 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1574                                 elems.ht_cap_elem, &sta->sta.ht_cap);
1575 
1576         ap_ht_cap_flags = sta->sta.ht_cap.cap;
1577 
1578         rate_control_rate_init(sta);
1579 
1580         if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
1581                 set_sta_flags(sta, WLAN_STA_MFP);
1582 
1583         if (elems.wmm_param)
1584                 set_sta_flags(sta, WLAN_STA_WME);
1585 
1586         if (newsta) {
1587                 int err = sta_info_insert(sta);
1588                 if (err) {
1589                         printk(KERN_DEBUG "%s: failed to insert STA entry for"
1590                                " the AP (error %d)\n", sdata->dev->name, err);
1591                         rcu_read_unlock();
1592                         return RX_MGMT_NONE;
1593                 }
1594         }
1595 
1596         rcu_read_unlock();
1597 
1598         if (elems.wmm_param)
1599                 ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
1600                                          elems.wmm_param_len);
1601         else
1602                 ieee80211_set_wmm_default(sdata);
1603 
1604         if (elems.ht_info_elem && elems.wmm_param &&
1605             (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
1606             !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
1607                 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1608                                                wk->bss->cbss.bssid,
1609                                                ap_ht_cap_flags);
1610 
1611         /* delete work item -- must be before set_associated for PS */
1612         list_del(&wk->list);
1613 
1614         /* set AID and assoc capability,
1615          * ieee80211_set_associated() will tell the driver */
1616         bss_conf->aid = aid;
1617         bss_conf->assoc_capability = capab_info;
1618         /* this will take ownership of wk */
1619         ieee80211_set_associated(sdata, wk, changed);
1620 
1621         /*
1622          * Start timer to probe the connection to the AP now.
1623          * Also start the timer that will detect beacon loss.
1624          */
1625         ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
1626         mod_beacon_timer(sdata);
1627 
1628         return RX_MGMT_CFG80211_ASSOC;
1629 }
1630 
1631 
1632 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1633                                   struct ieee80211_mgmt *mgmt,
1634                                   size_t len,
1635                                   struct ieee80211_rx_status *rx_status,
1636                                   struct ieee802_11_elems *elems,
1637                                   bool beacon)
1638 {
1639         struct ieee80211_local *local = sdata->local;
1640         int freq;
1641         struct ieee80211_bss *bss;
1642         struct ieee80211_channel *channel;
1643 
1644         if (elems->ds_params && elems->ds_params_len == 1)
1645                 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1646         else
1647                 freq = rx_status->freq;
1648 
1649         channel = ieee80211_get_channel(local->hw.wiphy, freq);
1650 
1651         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1652                 return;
1653 
1654         bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1655                                         channel, beacon);
1656         if (bss)
1657                 ieee80211_rx_bss_put(local, bss);
1658 
1659         if (!sdata->u.mgd.associated)
1660                 return;
1661 
1662         if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
1663             (memcmp(mgmt->bssid, sdata->u.mgd.associated->cbss.bssid,
1664                                                         ETH_ALEN) == 0)) {
1665                 struct ieee80211_channel_sw_ie *sw_elem =
1666                         (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
1667                 ieee80211_sta_process_chanswitch(sdata, sw_elem, bss);
1668         }
1669 }
1670 
1671 
1672 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
1673                                          struct ieee80211_mgd_work *wk,
1674                                          struct ieee80211_mgmt *mgmt, size_t len,
1675                                          struct ieee80211_rx_status *rx_status)
1676 {
1677         struct ieee80211_if_managed *ifmgd;
1678         size_t baselen;
1679         struct ieee802_11_elems elems;
1680 
1681         ifmgd = &sdata->u.mgd;
1682 
1683         ASSERT_MGD_MTX(ifmgd);
1684 
1685         if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
1686                 return; /* ignore ProbeResp to foreign address */
1687 
1688         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1689         if (baselen > len)
1690                 return;
1691 
1692         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1693                                 &elems);
1694 
1695         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
1696 
1697         /* direct probe may be part of the association flow */
1698         if (wk && wk->state == IEEE80211_MGD_STATE_PROBE) {
1699                 printk(KERN_DEBUG "%s: direct probe responded\n",
1700                        sdata->dev->name);
1701                 wk->tries = 0;
1702                 wk->state = IEEE80211_MGD_STATE_AUTH;
1703                 WARN_ON(ieee80211_authenticate(sdata, wk) != RX_MGMT_NONE);
1704         }
1705 
1706         if (ifmgd->associated &&
1707             memcmp(mgmt->bssid, ifmgd->associated->cbss.bssid, ETH_ALEN) == 0 &&
1708             ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1709                             IEEE80211_STA_CONNECTION_POLL)) {
1710                 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1711                                   IEEE80211_STA_BEACON_POLL);
1712                 mutex_lock(&sdata->local->iflist_mtx);
1713                 ieee80211_recalc_ps(sdata->local, -1);
1714                 mutex_unlock(&sdata->local->iflist_mtx);
1715                 /*
1716                  * We've received a probe response, but are not sure whether
1717                  * we have or will be receiving any beacons or data, so let's
1718                  * schedule the timers again, just in case.
1719                  */
1720                 mod_beacon_timer(sdata);
1721                 mod_timer(&ifmgd->conn_mon_timer,
1722                           round_jiffies_up(jiffies +
1723                                            IEEE80211_CONNECTION_IDLE_TIME));
1724         }
1725 }
1726 
1727 /*
1728  * This is the canonical list of information elements we care about,
1729  * the filter code also gives us all changes to the Microsoft OUI
1730  * (00:50:F2) vendor IE which is used for WMM which we need to track.
1731  *
1732  * We implement beacon filtering in software since that means we can
1733  * avoid processing the frame here and in cfg80211, and userspace
1734  * will not be able to tell whether the hardware supports it or not.
1735  *
1736  * XXX: This list needs to be dynamic -- userspace needs to be able to
1737  *      add items it requires. It also needs to be able to tell us to
1738  *      look out for other vendor IEs.
1739  */
1740 static const u64 care_about_ies =
1741         (1ULL << WLAN_EID_COUNTRY) |
1742         (1ULL << WLAN_EID_ERP_INFO) |
1743         (1ULL << WLAN_EID_CHANNEL_SWITCH) |
1744         (1ULL << WLAN_EID_PWR_CONSTRAINT) |
1745         (1ULL << WLAN_EID_HT_CAPABILITY) |
1746         (1ULL << WLAN_EID_HT_INFORMATION);
1747 
1748 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
1749                                      struct ieee80211_mgmt *mgmt,
1750                                      size_t len,
1751                                      struct ieee80211_rx_status *rx_status)
1752 {
1753         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1754         size_t baselen;
1755         struct ieee802_11_elems elems;
1756         struct ieee80211_local *local = sdata->local;
1757         u32 changed = 0;
1758         bool erp_valid, directed_tim = false;
1759         u8 erp_value = 0;
1760         u32 ncrc;
1761         u8 *bssid;
1762 
1763         ASSERT_MGD_MTX(ifmgd);
1764 
1765         /* Process beacon from the current BSS */
1766         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1767         if (baselen > len)
1768                 return;
1769 
1770         if (rx_status->freq != local->hw.conf.channel->center_freq)
1771                 return;
1772 
1773         /*
1774          * We might have received a number of frames, among them a
1775          * disassoc frame and a beacon...
1776          */
1777         if (!ifmgd->associated)
1778                 return;
1779 
1780         bssid = ifmgd->associated->cbss.bssid;
1781 
1782         /*
1783          * And in theory even frames from a different AP we were just
1784          * associated to a split-second ago!
1785          */
1786         if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
1787                 return;
1788 
1789         if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
1790 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1791                 if (net_ratelimit()) {
1792                         printk(KERN_DEBUG "%s: cancelling probereq poll due "
1793                                "to a received beacon\n", sdata->dev->name);
1794                 }
1795 #endif
1796                 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
1797                 mutex_lock(&local->iflist_mtx);
1798                 ieee80211_recalc_ps(local, -1);
1799                 mutex_unlock(&local->iflist_mtx);
1800         }
1801 
1802         /*
1803          * Push the beacon loss detection into the future since
1804          * we are processing a beacon from the AP just now.
1805          */
1806         mod_beacon_timer(sdata);
1807 
1808         ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
1809         ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
1810                                           len - baselen, &elems,
1811                                           care_about_ies, ncrc);
1812 
1813         if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1814                 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
1815                                                    ifmgd->aid);
1816 
1817         if (ncrc != ifmgd->beacon_crc) {
1818                 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
1819                                       true);
1820 
1821                 ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
1822                                          elems.wmm_param_len);
1823         }
1824 
1825         if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
1826                 if (directed_tim) {
1827                         if (local->hw.conf.dynamic_ps_timeout > 0) {
1828                                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1829                                 ieee80211_hw_config(local,
1830                                                     IEEE80211_CONF_CHANGE_PS);
1831                                 ieee80211_send_nullfunc(local, sdata, 0);
1832                         } else {
1833                                 local->pspolling = true;
1834 
1835                                 /*
1836                                  * Here is assumed that the driver will be
1837                                  * able to send ps-poll frame and receive a
1838                                  * response even though power save mode is
1839                                  * enabled, but some drivers might require
1840                                  * to disable power save here. This needs
1841                                  * to be investigated.
1842                                  */
1843                                 ieee80211_send_pspoll(local, sdata);
1844                         }
1845                 }
1846         }
1847 
1848         if (ncrc == ifmgd->beacon_crc)
1849                 return;
1850         ifmgd->beacon_crc = ncrc;
1851 
1852         if (elems.erp_info && elems.erp_info_len >= 1) {
1853                 erp_valid = true;
1854                 erp_value = elems.erp_info[0];
1855         } else {
1856                 erp_valid = false;
1857         }
1858         changed |= ieee80211_handle_bss_capability(sdata,
1859                         le16_to_cpu(mgmt->u.beacon.capab_info),
1860                         erp_valid, erp_value);
1861 
1862 
1863         if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1864             !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
1865                 struct sta_info *sta;
1866                 struct ieee80211_supported_band *sband;
1867                 u16 ap_ht_cap_flags;
1868 
1869                 rcu_read_lock();
1870 
1871                 sta = sta_info_get(local, bssid);
1872                 if (WARN_ON(!sta)) {
1873                         rcu_read_unlock();
1874                         return;
1875                 }
1876 
1877                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1878 
1879                 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1880                                 elems.ht_cap_elem, &sta->sta.ht_cap);
1881 
1882                 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1883 
1884                 rcu_read_unlock();
1885 
1886                 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1887                                                bssid, ap_ht_cap_flags);
1888         }
1889 
1890         /* Note: country IE parsing is done for us by cfg80211 */
1891         if (elems.country_elem) {
1892                 /* TODO: IBSS also needs this */
1893                 if (elems.pwr_constr_elem)
1894                         ieee80211_handle_pwr_constr(sdata,
1895                                 le16_to_cpu(mgmt->u.probe_resp.capab_info),
1896                                 elems.pwr_constr_elem,
1897                                 elems.pwr_constr_elem_len);
1898         }
1899 
1900         ieee80211_bss_info_change_notify(sdata, changed);
1901 }
1902 
1903 ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1904                                           struct sk_buff *skb)
1905 {
1906         struct ieee80211_local *local = sdata->local;
1907         struct ieee80211_mgmt *mgmt;
1908         u16 fc;
1909 
1910         if (skb->len < 24)
1911                 return RX_DROP_MONITOR;
1912 
1913         mgmt = (struct ieee80211_mgmt *) skb->data;
1914         fc = le16_to_cpu(mgmt->frame_control);
1915 
1916         switch (fc & IEEE80211_FCTL_STYPE) {
1917         case IEEE80211_STYPE_PROBE_REQ:
1918         case IEEE80211_STYPE_PROBE_RESP:
1919         case IEEE80211_STYPE_BEACON:
1920         case IEEE80211_STYPE_AUTH:
1921         case IEEE80211_STYPE_ASSOC_RESP:
1922         case IEEE80211_STYPE_REASSOC_RESP:
1923         case IEEE80211_STYPE_DEAUTH:
1924         case IEEE80211_STYPE_DISASSOC:
1925         case IEEE80211_STYPE_ACTION:
1926                 skb_queue_tail(&sdata->u.mgd.skb_queue, skb);
1927                 ieee80211_queue_work(&local->hw, &sdata->u.mgd.work);
1928                 return RX_QUEUED;
1929         }
1930 
1931         return RX_DROP_MONITOR;
1932 }
1933 
1934 static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1935                                          struct sk_buff *skb)
1936 {
1937         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1938         struct ieee80211_rx_status *rx_status;
1939         struct ieee80211_mgmt *mgmt;
1940         struct ieee80211_mgd_work *wk;
1941         enum rx_mgmt_action rma = RX_MGMT_NONE;
1942         u16 fc;
1943 
1944         rx_status = (struct ieee80211_rx_status *) skb->cb;
1945         mgmt = (struct ieee80211_mgmt *) skb->data;
1946         fc = le16_to_cpu(mgmt->frame_control);
1947 
1948         mutex_lock(&ifmgd->mtx);
1949 
1950         if (ifmgd->associated &&
1951             memcmp(ifmgd->associated->cbss.bssid, mgmt->bssid,
1952                                                         ETH_ALEN) == 0) {
1953                 switch (fc & IEEE80211_FCTL_STYPE) {
1954                 case IEEE80211_STYPE_BEACON:
1955                         ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
1956                                                  rx_status);
1957                         break;
1958                 case IEEE80211_STYPE_PROBE_RESP:
1959                         ieee80211_rx_mgmt_probe_resp(sdata, NULL, mgmt,
1960                                                      skb->len, rx_status);
1961                         break;
1962                 case IEEE80211_STYPE_DEAUTH:
1963                         rma = ieee80211_rx_mgmt_deauth(sdata, NULL,
1964                                                        mgmt, skb->len);
1965                         break;
1966                 case IEEE80211_STYPE_DISASSOC:
1967                         rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
1968                         break;
1969                 case IEEE80211_STYPE_ACTION:
1970                         if (mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
1971                                 break;
1972 
1973                         ieee80211_sta_process_chanswitch(sdata,
1974                                         &mgmt->u.action.u.chan_switch.sw_elem,
1975                                         ifmgd->associated);
1976                         break;
1977                 }
1978                 mutex_unlock(&ifmgd->mtx);
1979 
1980                 switch (rma) {
1981                 case RX_MGMT_NONE:
1982                         /* no action */
1983                         break;
1984                 case RX_MGMT_CFG80211_DEAUTH:
1985                         cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len,
1986                                              NULL);
1987                         break;
1988                 case RX_MGMT_CFG80211_DISASSOC:
1989                         cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len,
1990                                                NULL);
1991                         break;
1992                 default:
1993                         WARN(1, "unexpected: %d", rma);
1994                 }
1995                 goto out;
1996         }
1997 
1998         list_for_each_entry(wk, &ifmgd->work_list, list) {
1999                 if (memcmp(wk->bss->cbss.bssid, mgmt->bssid, ETH_ALEN) != 0)
2000                         continue;
2001 
2002                 switch (fc & IEEE80211_FCTL_STYPE) {
2003                 case IEEE80211_STYPE_PROBE_RESP:
2004                         ieee80211_rx_mgmt_probe_resp(sdata, wk, mgmt, skb->len,
2005                                                      rx_status);
2006                         break;
2007                 case IEEE80211_STYPE_AUTH:
2008                         rma = ieee80211_rx_mgmt_auth(sdata, wk, mgmt, skb->len);
2009                         break;
2010                 case IEEE80211_STYPE_ASSOC_RESP:
2011                         rma = ieee80211_rx_mgmt_assoc_resp(sdata, wk, mgmt,
2012                                                            skb->len, false);
2013                         break;
2014                 case IEEE80211_STYPE_REASSOC_RESP:
2015                         rma = ieee80211_rx_mgmt_assoc_resp(sdata, wk, mgmt,
2016                                                            skb->len, true);
2017                         break;
2018                 case IEEE80211_STYPE_DEAUTH:
2019                         rma = ieee80211_rx_mgmt_deauth(sdata, wk, mgmt,
2020                                                        skb->len);
2021                         break;
2022                 }
2023                 /*
2024                  * We've processed this frame for that work, so it can't
2025                  * belong to another work struct.
2026                  * NB: this is also required for correctness because the
2027                  * called functions can free 'wk', and for 'rma'!
2028                  */
2029                 break;
2030         }
2031 
2032         mutex_unlock(&ifmgd->mtx);
2033 
2034         switch (rma) {
2035         case RX_MGMT_NONE:
2036                 /* no action */
2037                 break;
2038         case RX_MGMT_CFG80211_AUTH:
2039                 cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, skb->len);
2040                 break;
2041         case RX_MGMT_CFG80211_ASSOC:
2042                 cfg80211_send_rx_assoc(sdata->dev, (u8 *) mgmt, skb->len);
2043                 break;
2044         case RX_MGMT_CFG80211_DEAUTH:
2045                 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len, NULL);
2046                 break;
2047         default:
2048                 WARN(1, "unexpected: %d", rma);
2049         }
2050 
2051  out:
2052         kfree_skb(skb);
2053 }
2054 
2055 static void ieee80211_sta_timer(unsigned long data)
2056 {
2057         struct ieee80211_sub_if_data *sdata =
2058                 (struct ieee80211_sub_if_data *) data;
2059         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2060         struct ieee80211_local *local = sdata->local;
2061 
2062         if (local->quiescing) {
2063                 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2064                 return;
2065         }
2066 
2067         ieee80211_queue_work(&local->hw, &ifmgd->work);
2068 }
2069 
2070 static void ieee80211_sta_work(struct work_struct *work)
2071 {
2072         struct ieee80211_sub_if_data *sdata =
2073                 container_of(work, struct ieee80211_sub_if_data, u.mgd.work);
2074         struct ieee80211_local *local = sdata->local;
2075         struct ieee80211_if_managed *ifmgd;
2076         struct sk_buff *skb;
2077         struct ieee80211_mgd_work *wk, *tmp;
2078         LIST_HEAD(free_work);
2079         enum rx_mgmt_action rma;
2080         bool anybusy = false;
2081 
2082         if (!netif_running(sdata->dev))
2083                 return;
2084 
2085         if (local->scanning)
2086                 return;
2087 
2088         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2089                 return;
2090 
2091         /*
2092          * ieee80211_queue_work() should have picked up most cases,
2093          * here we'll pick the the rest.
2094          */
2095         if (WARN(local->suspended, "STA MLME work scheduled while "
2096                  "going to suspend\n"))
2097                 return;
2098 
2099         ifmgd = &sdata->u.mgd;
2100 
2101         /* first process frames to avoid timing out while a frame is pending */
2102         while ((skb = skb_dequeue(&ifmgd->skb_queue)))
2103                 ieee80211_sta_rx_queued_mgmt(sdata, skb);
2104 
2105         /* then process the rest of the work */
2106         mutex_lock(&ifmgd->mtx);
2107 
2108         if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2109                             IEEE80211_STA_CONNECTION_POLL) &&
2110             ifmgd->associated) {
2111                 u8 bssid[ETH_ALEN];
2112 
2113                 memcpy(bssid, ifmgd->associated->cbss.bssid, ETH_ALEN);
2114                 if (time_is_after_jiffies(ifmgd->probe_timeout))
2115                         run_again(ifmgd, ifmgd->probe_timeout);
2116 
2117                 else if (ifmgd->probe_send_count < IEEE80211_MAX_PROBE_TRIES) {
2118 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2119                         printk(KERN_DEBUG "No probe response from AP %pM"
2120                                 " after %dms, try %d\n", bssid,
2121                                 (1000 * IEEE80211_PROBE_WAIT)/HZ,
2122                                 ifmgd->probe_send_count);
2123 #endif
2124                         ieee80211_mgd_probe_ap_send(sdata);
2125                 } else {
2126                         /*
2127                          * We actually lost the connection ... or did we?
2128                          * Let's make sure!
2129                          */
2130                         ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
2131                                           IEEE80211_STA_BEACON_POLL);
2132                         printk(KERN_DEBUG "No probe response from AP %pM"
2133                                 " after %dms, disconnecting.\n",
2134                                 bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
2135                         ieee80211_set_disassoc(sdata, true);
2136                         mutex_unlock(&ifmgd->mtx);
2137                         /*
2138                          * must be outside lock due to cfg80211,
2139                          * but that's not a problem.
2140                          */
2141                         ieee80211_send_deauth_disassoc(sdata, bssid,
2142                                         IEEE80211_STYPE_DEAUTH,
2143                                         WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2144                                         NULL);
2145                         mutex_lock(&ifmgd->mtx);
2146                 }
2147         }
2148 
2149 
2150         ieee80211_recalc_idle(local);
2151 
2152         list_for_each_entry_safe(wk, tmp, &ifmgd->work_list, list) {
2153                 if (time_is_after_jiffies(wk->timeout)) {
2154                         /*
2155                          * This work item isn't supposed to be worked on
2156                          * right now, but take care to adjust the timer
2157                          * properly.
2158                          */
2159                         run_again(ifmgd, wk->timeout);
2160                         continue;
2161                 }
2162 
2163                 switch (wk->state) {
2164                 default:
2165                         WARN_ON(1);
2166                         /* fall through */
2167                 case IEEE80211_MGD_STATE_IDLE:
2168                         /* nothing */
2169                         rma = RX_MGMT_NONE;
2170                         break;
2171                 case IEEE80211_MGD_STATE_PROBE:
2172                         rma = ieee80211_direct_probe(sdata, wk);
2173                         break;
2174                 case IEEE80211_MGD_STATE_AUTH:
2175                         rma = ieee80211_authenticate(sdata, wk);
2176                         break;
2177                 case IEEE80211_MGD_STATE_ASSOC:
2178                         rma = ieee80211_associate(sdata, wk);
2179                         break;
2180                 }
2181 
2182                 switch (rma) {
2183                 case RX_MGMT_NONE:
2184                         /* no action required */
2185                         break;
2186                 case RX_MGMT_CFG80211_AUTH_TO:
2187                 case RX_MGMT_CFG80211_ASSOC_TO:
2188                         list_del(&wk->list);
2189                         list_add(&wk->list, &free_work);
2190                         wk->tries = rma; /* small abuse but only local */
2191                         break;
2192                 default:
2193                         WARN(1, "unexpected: %d", rma);
2194                 }
2195         }
2196 
2197         list_for_each_entry(wk, &ifmgd->work_list, list) {
2198                 if (wk->state != IEEE80211_MGD_STATE_IDLE) {
2199                         anybusy = true;
2200                         break;
2201                 }
2202         }
2203         if (!anybusy &&
2204             test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request))
2205                 ieee80211_queue_delayed_work(&local->hw,
2206                                              &local->scan_work,
2207                                              round_jiffies_relative(0));
2208 
2209         mutex_unlock(&ifmgd->mtx);
2210 
2211         list_for_each_entry_safe(wk, tmp, &free_work, list) {
2212                 switch (wk->tries) {
2213                 case RX_MGMT_CFG80211_AUTH_TO:
2214                         cfg80211_send_auth_timeout(sdata->dev,
2215                                                    wk->bss->cbss.bssid);
2216                         break;
2217                 case RX_MGMT_CFG80211_ASSOC_TO:
2218                         cfg80211_send_assoc_timeout(sdata->dev,
2219                                                     wk->bss->cbss.bssid);
2220                         break;
2221                 default:
2222                         WARN(1, "unexpected: %d", wk->tries);
2223                 }
2224 
2225                 list_del(&wk->list);
2226                 kfree(wk);
2227         }
2228 
2229         ieee80211_recalc_idle(local);
2230 }
2231 
2232 static void ieee80211_sta_bcn_mon_timer(unsigned long data)
2233 {
2234         struct ieee80211_sub_if_data *sdata =
2235                 (struct ieee80211_sub_if_data *) data;
2236         struct ieee80211_local *local = sdata->local;
2237 
2238         if (local->quiescing)
2239                 return;
2240 
2241         ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
2242 }
2243 
2244 static void ieee80211_sta_conn_mon_timer(unsigned long data)
2245 {
2246         struct ieee80211_sub_if_data *sdata =
2247                 (struct ieee80211_sub_if_data *) data;
2248         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2249         struct ieee80211_local *local = sdata->local;
2250 
2251         if (local->quiescing)
2252                 return;
2253 
2254         ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
2255 }
2256 
2257 static void ieee80211_sta_monitor_work(struct work_struct *work)
2258 {
2259         struct ieee80211_sub_if_data *sdata =
2260                 container_of(work, struct ieee80211_sub_if_data,
2261                              u.mgd.monitor_work);
2262 
2263         ieee80211_mgd_probe_ap(sdata, false);
2264 }
2265 
2266 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2267 {
2268         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2269                 sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
2270                                         IEEE80211_STA_CONNECTION_POLL);
2271 
2272                 /* let's probe the connection once */
2273                 ieee80211_queue_work(&sdata->local->hw,
2274                            &sdata->u.mgd.monitor_work);
2275                 /* and do all the other regular work too */
2276                 ieee80211_queue_work(&sdata->local->hw,
2277                            &sdata->u.mgd.work);
2278         }
2279 }
2280 
2281 #ifdef CONFIG_PM
2282 void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
2283 {
2284         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2285 
2286         /*
2287          * we need to use atomic bitops for the running bits
2288          * only because both timers might fire at the same
2289          * time -- the code here is properly synchronised.
2290          */
2291 
2292         cancel_work_sync(&ifmgd->work);
2293         cancel_work_sync(&ifmgd->beacon_loss_work);
2294         if (del_timer_sync(&ifmgd->timer))
2295                 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2296 
2297         cancel_work_sync(&ifmgd->chswitch_work);
2298         if (del_timer_sync(&ifmgd->chswitch_timer))
2299                 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
2300 
2301         cancel_work_sync(&ifmgd->monitor_work);
2302         /* these will just be re-established on connection */
2303         del_timer_sync(&ifmgd->conn_mon_timer);
2304         del_timer_sync(&ifmgd->bcn_mon_timer);
2305 }
2306 
2307 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
2308 {
2309         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2310 
2311         if (!ifmgd->associated)
2312                 return;
2313 
2314         if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
2315                 add_timer(&ifmgd->timer);
2316         if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
2317                 add_timer(&ifmgd->chswitch_timer);
2318 }
2319 #endif
2320 
2321 /* interface setup */
2322 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
2323 {
2324         struct ieee80211_if_managed *ifmgd;
2325 
2326         ifmgd = &sdata->u.mgd;
2327         INIT_WORK(&ifmgd->work, ieee80211_sta_work);
2328         INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
2329         INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
2330         INIT_WORK(&ifmgd->beacon_loss_work, ieee80211_beacon_loss_work);
2331         setup_timer(&ifmgd->timer, ieee80211_sta_timer,
2332                     (unsigned long) sdata);
2333         setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
2334                     (unsigned long) sdata);
2335         setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
2336                     (unsigned long) sdata);
2337         setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
2338                     (unsigned long) sdata);
2339         skb_queue_head_init(&ifmgd->skb_queue);
2340 
2341         INIT_LIST_HEAD(&ifmgd->work_list);
2342 
2343         ifmgd->capab = WLAN_CAPABILITY_ESS;
2344         ifmgd->flags = 0;
2345         if (sdata->local->hw.queues >= 4)
2346                 ifmgd->flags |= IEEE80211_STA_WMM_ENABLED;
2347 
2348         mutex_init(&ifmgd->mtx);
2349 }
2350 
2351 /* scan finished notification */
2352 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2353 {
2354         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2355 
2356         /* Restart STA timers */
2357         rcu_read_lock();
2358         list_for_each_entry_rcu(sdata, &local->interfaces, list)
2359                 ieee80211_restart_sta_timer(sdata);
2360         rcu_read_unlock();
2361 }
2362 
2363 int ieee80211_max_network_latency(struct notifier_block *nb,
2364                                   unsigned long data, void *dummy)
2365 {
2366         s32 latency_usec = (s32) data;
2367         struct ieee80211_local *local =
2368                 container_of(nb, struct ieee80211_local,
2369                              network_latency_notifier);
2370 
2371         mutex_lock(&local->iflist_mtx);
2372         ieee80211_recalc_ps(local, latency_usec);
2373         mutex_unlock(&local->iflist_mtx);
2374 
2375         return 0;
2376 }
2377 
2378 /* config hooks */
2379 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
2380                        struct cfg80211_auth_request *req)
2381 {
2382         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2383         const u8 *ssid;
2384         struct ieee80211_mgd_work *wk;
2385         u16 auth_alg;
2386 
2387         switch (req->auth_type) {
2388         case NL80211_AUTHTYPE_OPEN_SYSTEM:
2389                 auth_alg = WLAN_AUTH_OPEN;
2390                 break;
2391         case NL80211_AUTHTYPE_SHARED_KEY:
2392                 auth_alg = WLAN_AUTH_SHARED_KEY;
2393                 break;
2394         case NL80211_AUTHTYPE_FT:
2395                 auth_alg = WLAN_AUTH_FT;
2396                 break;
2397         case NL80211_AUTHTYPE_NETWORK_EAP:
2398                 auth_alg = WLAN_AUTH_LEAP;
2399                 break;
2400         default:
2401                 return -EOPNOTSUPP;
2402         }
2403 
2404         wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
2405         if (!wk)
2406                 return -ENOMEM;
2407 
2408         wk->bss = (void *)req->bss;
2409 
2410         if (req->ie && req->ie_len) {
2411                 memcpy(wk->ie, req->ie, req->ie_len);
2412                 wk->ie_len = req->ie_len;
2413         }
2414 
2415         if (req->key && req->key_len) {
2416                 wk->key_len = req->key_len;
2417                 wk->key_idx = req->key_idx;
2418                 memcpy(wk->key, req->key, req->key_len);
2419         }
2420 
2421         ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
2422         memcpy(wk->ssid, ssid + 2, ssid[1]);
2423         wk->ssid_len = ssid[1];
2424 
2425         wk->state = IEEE80211_MGD_STATE_PROBE;
2426         wk->auth_alg = auth_alg;
2427         wk->timeout = jiffies; /* run right away */
2428 
2429         /*
2430          * XXX: if still associated need to tell AP that we're going
2431          *      to sleep and then change channel etc.
2432          */
2433         sdata->local->oper_channel = req->bss->channel;
2434         ieee80211_hw_config(sdata->local, 0);
2435 
2436         mutex_lock(&ifmgd->mtx);
2437         list_add(&wk->list, &sdata->u.mgd.work_list);
2438         mutex_unlock(&ifmgd->mtx);
2439 
2440         ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.work);
2441         return 0;
2442 }
2443 
2444 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
2445                         struct cfg80211_assoc_request *req)
2446 {
2447         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2448         struct ieee80211_mgd_work *wk, *found = NULL;
2449         int i, err;
2450 
2451         mutex_lock(&ifmgd->mtx);
2452 
2453         list_for_each_entry(wk, &ifmgd->work_list, list) {
2454                 if (&wk->bss->cbss == req->bss &&
2455                     wk->state == IEEE80211_MGD_STATE_IDLE) {
2456                         found = wk;
2457                         break;
2458                 }
2459         }
2460 
2461         if (!found) {
2462                 err = -ENOLINK;
2463                 goto out;
2464         }
2465 
2466         list_del(&found->list);
2467 
2468         wk = krealloc(found, sizeof(*wk) + req->ie_len, GFP_KERNEL);
2469         if (!wk) {
2470                 list_add(&found->list, &ifmgd->work_list);
2471                 err = -ENOMEM;
2472                 goto out;
2473         }
2474 
2475         list_add(&wk->list, &ifmgd->work_list);
2476 
2477         ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
2478         ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
2479 
2480         for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
2481                 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
2482                     req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
2483                     req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
2484                         ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
2485 
2486         sdata->local->oper_channel = req->bss->channel;
2487         ieee80211_hw_config(sdata->local, 0);
2488 
2489         if (req->ie && req->ie_len) {
2490                 memcpy(wk->ie, req->ie, req->ie_len);
2491                 wk->ie_len = req->ie_len;
2492         } else
2493                 wk->ie_len = 0;
2494 
2495         if (req->prev_bssid)
2496                 memcpy(wk->prev_bssid, req->prev_bssid, ETH_ALEN);
2497 
2498         wk->state = IEEE80211_MGD_STATE_ASSOC;
2499         wk->tries = 0;
2500         wk->timeout = jiffies; /* run right away */
2501 
2502         if (req->use_mfp) {
2503                 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
2504                 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
2505         } else {
2506                 ifmgd->mfp = IEEE80211_MFP_DISABLED;
2507                 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
2508         }
2509 
2510         if (req->crypto.control_port)
2511                 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
2512         else
2513                 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
2514 
2515         ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.work);
2516 
2517         err = 0;
2518 
2519  out:
2520         mutex_unlock(&ifmgd->mtx);
2521         return err;
2522 }
2523 
2524 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
2525                          struct cfg80211_deauth_request *req,
2526                          void *cookie)
2527 {
2528         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2529         struct ieee80211_mgd_work *wk;
2530         const u8 *bssid = NULL;
2531 
2532         mutex_lock(&ifmgd->mtx);
2533 
2534         if (ifmgd->associated && &ifmgd->associated->cbss == req->bss) {
2535                 bssid = req->bss->bssid;
2536                 ieee80211_set_disassoc(sdata, true);
2537         } else list_for_each_entry(wk, &ifmgd->work_list, list) {
2538                 if (&wk->bss->cbss == req->bss) {
2539                         bssid = req->bss->bssid;
2540                         list_del(&wk->list);
2541                         kfree(wk);
2542                         break;
2543                 }
2544         }
2545 
2546         /*
2547          * cfg80211 should catch this ... but it's racy since
2548          * we can receive a deauth frame, process it, hand it
2549          * to cfg80211 while that's in a locked section already
2550          * trying to tell us that the user wants to disconnect.
2551          */
2552         if (!bssid) {
2553                 mutex_unlock(&ifmgd->mtx);
2554                 return -ENOLINK;
2555         }
2556 
2557         mutex_unlock(&ifmgd->mtx);
2558 
2559         printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
2560                sdata->dev->name, bssid, req->reason_code);
2561 
2562         ieee80211_send_deauth_disassoc(sdata, bssid,
2563                         IEEE80211_STYPE_DEAUTH, req->reason_code,
2564                         cookie);
2565 
2566         return 0;
2567 }
2568 
2569 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
2570                            struct cfg80211_disassoc_request *req,
2571                            void *cookie)
2572 {
2573         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2574 
2575         mutex_lock(&ifmgd->mtx);
2576 
2577         /*
2578          * cfg80211 should catch this ... but it's racy since
2579          * we can receive a disassoc frame, process it, hand it
2580          * to cfg80211 while that's in a locked section already
2581          * trying to tell us that the user wants to disconnect.
2582          */
2583         if (&ifmgd->associated->cbss != req->bss) {
2584                 mutex_unlock(&ifmgd->mtx);
2585                 return -ENOLINK;
2586         }
2587 
2588         printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
2589                sdata->dev->name, req->bss->bssid, req->reason_code);
2590 
2591         ieee80211_set_disassoc(sdata, false);
2592 
2593         mutex_unlock(&ifmgd->mtx);
2594 
2595         ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
2596                         IEEE80211_STYPE_DISASSOC, req->reason_code,
2597                         cookie);
2598         return 0;
2599 }
2600 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp