net: add moduleparam.h for users of module_param/MODULE_PARM_DESC
[deliverable/linux.git] / net / mac80211 / mlme.c
CommitLineData
f0706e82
JB
1/*
2 * BSS client mode implementation
5394af4d 3 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
f0706e82
JB
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
5b323edb 14#include <linux/delay.h>
f0706e82
JB
15#include <linux/if_ether.h>
16#include <linux/skbuff.h>
f0706e82 17#include <linux/if_arp.h>
f0706e82 18#include <linux/etherdevice.h>
d9b93842 19#include <linux/moduleparam.h>
d0709a65 20#include <linux/rtnetlink.h>
e8db0be1 21#include <linux/pm_qos.h>
d91f36db 22#include <linux/crc32.h>
5a0e3ad6 23#include <linux/slab.h>
f0706e82 24#include <net/mac80211.h>
472dbc45 25#include <asm/unaligned.h>
60f8b39c 26
f0706e82 27#include "ieee80211_i.h"
24487981 28#include "driver-ops.h"
2c8dccc7
JB
29#include "rate.h"
30#include "led.h"
f0706e82 31
180205bd
BG
32static int max_nullfunc_tries = 2;
33module_param(max_nullfunc_tries, int, 0644);
34MODULE_PARM_DESC(max_nullfunc_tries,
35 "Maximum nullfunc tx tries before disconnecting (reason 4).");
36
37static int max_probe_tries = 5;
38module_param(max_probe_tries, int, 0644);
39MODULE_PARM_DESC(max_probe_tries,
40 "Maximum probe tries before disconnecting (reason 4).");
b291ba11
JB
41
42/*
7ccc8bd7
FF
43 * Beacon loss timeout is calculated as N frames times the
44 * advertised beacon interval. This may need to be somewhat
45 * higher than what hardware might detect to account for
46 * delays in the host processing frames. But since we also
47 * probe on beacon miss before declaring the connection lost
48 * default to what we want.
b291ba11 49 */
7ccc8bd7
FF
50#define IEEE80211_BEACON_LOSS_COUNT 7
51
b291ba11
JB
52/*
53 * Time the connection can be idle before we probe
54 * it to see if we can still talk to the AP.
55 */
d1c5091f 56#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
b291ba11
JB
57/*
58 * Time we wait for a probe response after sending
59 * a probe request because of beacon loss or for
60 * checking the connection still works.
61 */
180205bd
BG
62static int probe_wait_ms = 500;
63module_param(probe_wait_ms, int, 0644);
64MODULE_PARM_DESC(probe_wait_ms,
65 "Maximum time(ms) to wait for probe response"
66 " before disconnecting (reason 4).");
f0706e82 67
17e4ec14
JM
68/*
69 * Weight given to the latest Beacon frame when calculating average signal
70 * strength for Beacon frames received in the current BSS. This must be
71 * between 1 and 15.
72 */
73#define IEEE80211_SIGNAL_AVE_WEIGHT 3
74
391a200a
JM
75/*
76 * How many Beacon frames need to have been used in average signal strength
77 * before starting to indicate signal change events.
78 */
79#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
80
5bb644a0
JB
81#define TMR_RUNNING_TIMER 0
82#define TMR_RUNNING_CHANSW 1
83
77fdaa12
JB
84/*
85 * All cfg80211 functions have to be called outside a locked
86 * section so that they can acquire a lock themselves... This
87 * is much simpler than queuing up things in cfg80211, but we
88 * do need some indirection for that here.
89 */
90enum rx_mgmt_action {
91 /* no action required */
92 RX_MGMT_NONE,
93
77fdaa12
JB
94 /* caller must call cfg80211_send_deauth() */
95 RX_MGMT_CFG80211_DEAUTH,
96
97 /* caller must call cfg80211_send_disassoc() */
98 RX_MGMT_CFG80211_DISASSOC,
77fdaa12
JB
99};
100
5484e237 101/* utils */
77fdaa12
JB
102static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
103{
46a5ebaf 104 lockdep_assert_held(&ifmgd->mtx);
77fdaa12
JB
105}
106
ca386f31
JB
107/*
108 * We can have multiple work items (and connection probing)
109 * scheduling this timer, but we need to take care to only
110 * reschedule it when it should fire _earlier_ than it was
111 * asked for before, or if it's not pending right now. This
112 * function ensures that. Note that it then is required to
113 * run this function for all timeouts after the first one
114 * has happened -- the work that runs from this timer will
115 * do that.
116 */
117static void run_again(struct ieee80211_if_managed *ifmgd,
118 unsigned long timeout)
119{
120 ASSERT_MGD_MTX(ifmgd);
121
122 if (!timer_pending(&ifmgd->timer) ||
123 time_before(timeout, ifmgd->timer.expires))
124 mod_timer(&ifmgd->timer, timeout);
125}
126
d3a910a8 127void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
b291ba11
JB
128{
129 if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
130 return;
131
132 mod_timer(&sdata->u.mgd.bcn_mon_timer,
7ccc8bd7 133 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
b291ba11
JB
134}
135
be099e82
LR
136void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
137{
0c699c3a
LR
138 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
139
8628172f
SG
140 if (unlikely(!sdata->u.mgd.associated))
141 return;
142
be099e82
LR
143 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
144 return;
145
146 mod_timer(&sdata->u.mgd.conn_mon_timer,
147 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
0c699c3a
LR
148
149 ifmgd->probe_send_count = 0;
be099e82
LR
150}
151
5484e237 152static int ecw2cw(int ecw)
60f8b39c 153{
5484e237 154 return (1 << ecw) - 1;
60f8b39c
JB
155}
156
d5522e03
JB
157/*
158 * ieee80211_enable_ht should be called only after the operating band
159 * has been determined as ht configuration depends on the hw's
160 * HT abilities for a specific band.
161 */
162static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
163 struct ieee80211_ht_info *hti,
7cc44ed4
RM
164 const u8 *bssid, u16 ap_ht_cap_flags,
165 bool beacon_htcap_ie)
d5522e03
JB
166{
167 struct ieee80211_local *local = sdata->local;
168 struct ieee80211_supported_band *sband;
d5522e03
JB
169 struct sta_info *sta;
170 u32 changed = 0;
172710bf 171 int hti_cfreq;
9ed6bcce 172 u16 ht_opmode;
0aaffa9b
JB
173 bool enable_ht = true;
174 enum nl80211_channel_type prev_chantype;
d5522e03
JB
175 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
176
177 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
178
0aaffa9b
JB
179 prev_chantype = sdata->vif.bss_conf.channel_type;
180
d5522e03
JB
181 /* HT is not supported */
182 if (!sband->ht_cap.ht_supported)
183 enable_ht = false;
184
172710bf
BG
185 if (enable_ht) {
186 hti_cfreq = ieee80211_channel_to_frequency(hti->control_chan,
187 sband->band);
188 /* check that channel matches the right operating channel */
189 if (local->hw.conf.channel->center_freq != hti_cfreq) {
190 /* Some APs mess this up, evidently.
191 * Netgear WNDR3700 sometimes reports 4 higher than
192 * the actual channel, for instance.
193 */
194 printk(KERN_DEBUG
195 "%s: Wrong control channel in association"
196 " response: configured center-freq: %d"
197 " hti-cfreq: %d hti->control_chan: %d"
198 " band: %d. Disabling HT.\n",
199 sdata->name,
200 local->hw.conf.channel->center_freq,
201 hti_cfreq, hti->control_chan,
202 sband->band);
203 enable_ht = false;
204 }
205 }
d5522e03
JB
206
207 if (enable_ht) {
208 channel_type = NL80211_CHAN_HT20;
209
210 if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
211 (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
212 (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
213 switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
214 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
768777ea
LR
215 if (!(local->hw.conf.channel->flags &
216 IEEE80211_CHAN_NO_HT40PLUS))
217 channel_type = NL80211_CHAN_HT40PLUS;
d5522e03
JB
218 break;
219 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
768777ea
LR
220 if (!(local->hw.conf.channel->flags &
221 IEEE80211_CHAN_NO_HT40MINUS))
222 channel_type = NL80211_CHAN_HT40MINUS;
d5522e03
JB
223 break;
224 }
225 }
226 }
227
fe6f212c
RC
228 if (local->tmp_channel)
229 local->tmp_channel_type = channel_type;
d5522e03 230
0aaffa9b
JB
231 if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
232 /* can only fail due to HT40+/- mismatch */
233 channel_type = NL80211_CHAN_HT20;
234 WARN_ON(!ieee80211_set_channel_type(local, sdata, channel_type));
235 }
d5522e03 236
7cc44ed4
RM
237 if (beacon_htcap_ie && (prev_chantype != channel_type)) {
238 /*
239 * Whenever the AP announces the HT mode change that can be
240 * 40MHz intolerant or etc., it would be safer to stop tx
241 * queues before doing hw config to avoid buffer overflow.
242 */
243 ieee80211_stop_queues_by_reason(&sdata->local->hw,
244 IEEE80211_QUEUE_STOP_REASON_CHTYPE_CHANGE);
245
246 /* flush out all packets */
247 synchronize_net();
248
249 drv_flush(local, false);
250 }
251
0aaffa9b
JB
252 /* channel_type change automatically detected */
253 ieee80211_hw_config(local, 0);
d5522e03 254
0aaffa9b 255 if (prev_chantype != channel_type) {
d5522e03 256 rcu_read_lock();
abe60632 257 sta = sta_info_get(sdata, bssid);
d5522e03
JB
258 if (sta)
259 rate_control_rate_update(local, sband, sta,
4fa00437 260 IEEE80211_RC_HT_CHANGED,
0aaffa9b 261 channel_type);
d5522e03 262 rcu_read_unlock();
7cc44ed4
RM
263
264 if (beacon_htcap_ie)
265 ieee80211_wake_queues_by_reason(&sdata->local->hw,
266 IEEE80211_QUEUE_STOP_REASON_CHTYPE_CHANGE);
0aaffa9b 267 }
d5522e03 268
9ed6bcce 269 ht_opmode = le16_to_cpu(hti->operation_mode);
d5522e03
JB
270
271 /* if bss configuration changed store the new one */
0aaffa9b
JB
272 if (sdata->ht_opmode_valid != enable_ht ||
273 sdata->vif.bss_conf.ht_operation_mode != ht_opmode ||
274 prev_chantype != channel_type) {
d5522e03 275 changed |= BSS_CHANGED_HT;
9ed6bcce 276 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
0aaffa9b 277 sdata->ht_opmode_valid = enable_ht;
d5522e03
JB
278 }
279
280 return changed;
281}
282
9c6bd790
JB
283/* frame sending functions */
284
ef422bc0 285static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
667503dd 286 const u8 *bssid, u16 stype, u16 reason,
d5cdfacb 287 void *cookie, bool send_frame)
9ac19a90
JB
288{
289 struct ieee80211_local *local = sdata->local;
46900298 290 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9ac19a90
JB
291 struct sk_buff *skb;
292 struct ieee80211_mgmt *mgmt;
9aed3cc1 293
65fc73ac 294 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
d15b8459 295 if (!skb)
9ac19a90 296 return;
d15b8459 297
9ac19a90
JB
298 skb_reserve(skb, local->hw.extra_tx_headroom);
299
300 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
301 memset(mgmt, 0, 24);
77fdaa12 302 memcpy(mgmt->da, bssid, ETH_ALEN);
47846c9b 303 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
77fdaa12 304 memcpy(mgmt->bssid, bssid, ETH_ALEN);
ef422bc0 305 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
9ac19a90 306 skb_put(skb, 2);
ef422bc0 307 /* u.deauth.reason_code == u.disassoc.reason_code */
9ac19a90
JB
308 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
309
53b46b84 310 if (stype == IEEE80211_STYPE_DEAUTH)
ce470613
HS
311 if (cookie)
312 __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
313 else
314 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
53b46b84 315 else
ce470613
HS
316 if (cookie)
317 __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
318 else
319 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
62ae67be
JB
320 if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
321 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
d5cdfacb
JM
322
323 if (send_frame)
324 ieee80211_tx_skb(sdata, skb);
325 else
326 kfree_skb(skb);
9ac19a90
JB
327}
328
572e0012
KV
329void ieee80211_send_pspoll(struct ieee80211_local *local,
330 struct ieee80211_sub_if_data *sdata)
331{
572e0012
KV
332 struct ieee80211_pspoll *pspoll;
333 struct sk_buff *skb;
572e0012 334
d8cd189e
KV
335 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
336 if (!skb)
572e0012 337 return;
572e0012 338
d8cd189e
KV
339 pspoll = (struct ieee80211_pspoll *) skb->data;
340 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
572e0012 341
62ae67be
JB
342 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
343 ieee80211_tx_skb(sdata, skb);
572e0012
KV
344}
345
965bedad
JB
346void ieee80211_send_nullfunc(struct ieee80211_local *local,
347 struct ieee80211_sub_if_data *sdata,
348 int powersave)
349{
350 struct sk_buff *skb;
d8cd189e 351 struct ieee80211_hdr_3addr *nullfunc;
b6f35301 352 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
965bedad 353
d8cd189e
KV
354 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
355 if (!skb)
965bedad 356 return;
965bedad 357
d8cd189e 358 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
965bedad 359 if (powersave)
d8cd189e 360 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
965bedad 361
62ae67be 362 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
b6f35301
RM
363 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
364 IEEE80211_STA_CONNECTION_POLL))
365 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
366
62ae67be 367 ieee80211_tx_skb(sdata, skb);
965bedad
JB
368}
369
d524215f
FF
370static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
371 struct ieee80211_sub_if_data *sdata)
372{
373 struct sk_buff *skb;
374 struct ieee80211_hdr *nullfunc;
375 __le16 fc;
376
377 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
378 return;
379
380 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
d15b8459 381 if (!skb)
d524215f 382 return;
d15b8459 383
d524215f
FF
384 skb_reserve(skb, local->hw.extra_tx_headroom);
385
386 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
387 memset(nullfunc, 0, 30);
388 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
389 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
390 nullfunc->frame_control = fc;
391 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
392 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
393 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
394 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
395
396 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
397 ieee80211_tx_skb(sdata, skb);
398}
399
cc32abd4
JB
400/* spectrum management related things */
401static void ieee80211_chswitch_work(struct work_struct *work)
402{
403 struct ieee80211_sub_if_data *sdata =
404 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
cc32abd4
JB
405 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
406
9607e6b6 407 if (!ieee80211_sdata_running(sdata))
cc32abd4
JB
408 return;
409
77fdaa12
JB
410 mutex_lock(&ifmgd->mtx);
411 if (!ifmgd->associated)
412 goto out;
cc32abd4
JB
413
414 sdata->local->oper_channel = sdata->local->csa_channel;
5ce6e438
JB
415 if (!sdata->local->ops->channel_switch) {
416 /* call "hw_config" only if doing sw channel switch */
417 ieee80211_hw_config(sdata->local,
418 IEEE80211_CONF_CHANGE_CHANNEL);
1ea57b1f
SL
419 } else {
420 /* update the device channel directly */
421 sdata->local->hw.conf.channel = sdata->local->oper_channel;
5ce6e438 422 }
77fdaa12 423
cc32abd4 424 /* XXX: shouldn't really modify cfg80211-owned data! */
0c1ad2ca 425 ifmgd->associated->channel = sdata->local->oper_channel;
cc32abd4 426
cc32abd4
JB
427 ieee80211_wake_queues_by_reason(&sdata->local->hw,
428 IEEE80211_QUEUE_STOP_REASON_CSA);
77fdaa12
JB
429 out:
430 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
431 mutex_unlock(&ifmgd->mtx);
cc32abd4
JB
432}
433
5ce6e438
JB
434void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
435{
436 struct ieee80211_sub_if_data *sdata;
437 struct ieee80211_if_managed *ifmgd;
438
439 sdata = vif_to_sdata(vif);
440 ifmgd = &sdata->u.mgd;
441
442 trace_api_chswitch_done(sdata, success);
443 if (!success) {
444 /*
445 * If the channel switch was not successful, stay
446 * around on the old channel. We currently lack
447 * good handling of this situation, possibly we
448 * should just drop the association.
449 */
450 sdata->local->csa_channel = sdata->local->oper_channel;
451 }
452
453 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
454}
455EXPORT_SYMBOL(ieee80211_chswitch_done);
456
cc32abd4
JB
457static void ieee80211_chswitch_timer(unsigned long data)
458{
459 struct ieee80211_sub_if_data *sdata =
460 (struct ieee80211_sub_if_data *) data;
461 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
462
5bb644a0
JB
463 if (sdata->local->quiescing) {
464 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
465 return;
466 }
467
42935eca 468 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
cc32abd4
JB
469}
470
471void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
472 struct ieee80211_channel_sw_ie *sw_elem,
5ce6e438
JB
473 struct ieee80211_bss *bss,
474 u64 timestamp)
cc32abd4 475{
0c1ad2ca
JB
476 struct cfg80211_bss *cbss =
477 container_of((void *)bss, struct cfg80211_bss, priv);
cc32abd4
JB
478 struct ieee80211_channel *new_ch;
479 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
59eb21a6
BR
480 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
481 cbss->channel->band);
cc32abd4 482
77fdaa12
JB
483 ASSERT_MGD_MTX(ifmgd);
484
485 if (!ifmgd->associated)
cc32abd4
JB
486 return;
487
fbe9c429 488 if (sdata->local->scanning)
cc32abd4
JB
489 return;
490
491 /* Disregard subsequent beacons if we are already running a timer
492 processing a CSA */
493
494 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
495 return;
496
497 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
498 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
499 return;
500
501 sdata->local->csa_channel = new_ch;
502
5ce6e438
JB
503 if (sdata->local->ops->channel_switch) {
504 /* use driver's channel switch callback */
505 struct ieee80211_channel_switch ch_switch;
506 memset(&ch_switch, 0, sizeof(ch_switch));
507 ch_switch.timestamp = timestamp;
508 if (sw_elem->mode) {
509 ch_switch.block_tx = true;
510 ieee80211_stop_queues_by_reason(&sdata->local->hw,
511 IEEE80211_QUEUE_STOP_REASON_CSA);
512 }
513 ch_switch.channel = new_ch;
514 ch_switch.count = sw_elem->count;
515 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
516 drv_channel_switch(sdata->local, &ch_switch);
517 return;
518 }
519
520 /* channel switch handled in software */
cc32abd4 521 if (sw_elem->count <= 1) {
42935eca 522 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
cc32abd4 523 } else {
9feaddc7
WYG
524 if (sw_elem->mode)
525 ieee80211_stop_queues_by_reason(&sdata->local->hw,
cc32abd4
JB
526 IEEE80211_QUEUE_STOP_REASON_CSA);
527 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
528 mod_timer(&ifmgd->chswitch_timer,
529 jiffies +
530 msecs_to_jiffies(sw_elem->count *
0c1ad2ca 531 cbss->beacon_interval));
cc32abd4
JB
532 }
533}
534
535static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
536 u16 capab_info, u8 *pwr_constr_elem,
537 u8 pwr_constr_elem_len)
538{
539 struct ieee80211_conf *conf = &sdata->local->hw.conf;
540
541 if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
542 return;
543
544 /* Power constraint IE length should be 1 octet */
545 if (pwr_constr_elem_len != 1)
546 return;
547
548 if ((*pwr_constr_elem <= conf->channel->max_power) &&
549 (*pwr_constr_elem != sdata->local->power_constr_level)) {
550 sdata->local->power_constr_level = *pwr_constr_elem;
551 ieee80211_hw_config(sdata->local, 0);
552 }
553}
554
f90754c1
JO
555void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
556{
557 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
558 struct ieee80211_local *local = sdata->local;
559 struct ieee80211_conf *conf = &local->hw.conf;
560
561 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
562 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
563 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
564
565 local->disable_dynamic_ps = false;
566 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
567}
568EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
569
570void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
571{
572 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
573 struct ieee80211_local *local = sdata->local;
574 struct ieee80211_conf *conf = &local->hw.conf;
575
576 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
577 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
578 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
579
580 local->disable_dynamic_ps = true;
581 conf->dynamic_ps_timeout = 0;
582 del_timer_sync(&local->dynamic_ps_timer);
583 ieee80211_queue_work(&local->hw,
584 &local->dynamic_ps_enable_work);
585}
586EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
587
965bedad
JB
588/* powersave */
589static void ieee80211_enable_ps(struct ieee80211_local *local,
590 struct ieee80211_sub_if_data *sdata)
591{
592 struct ieee80211_conf *conf = &local->hw.conf;
593
d5edaedc
JB
594 /*
595 * If we are scanning right now then the parameters will
596 * take effect when scan finishes.
597 */
fbe9c429 598 if (local->scanning)
d5edaedc
JB
599 return;
600
965bedad
JB
601 if (conf->dynamic_ps_timeout > 0 &&
602 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
603 mod_timer(&local->dynamic_ps_timer, jiffies +
604 msecs_to_jiffies(conf->dynamic_ps_timeout));
605 } else {
606 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
607 ieee80211_send_nullfunc(local, sdata, 1);
375177bf 608
2a13052f
JO
609 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
610 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
611 return;
612
613 conf->flags |= IEEE80211_CONF_PS;
614 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
965bedad
JB
615 }
616}
617
618static void ieee80211_change_ps(struct ieee80211_local *local)
619{
620 struct ieee80211_conf *conf = &local->hw.conf;
621
622 if (local->ps_sdata) {
965bedad
JB
623 ieee80211_enable_ps(local, local->ps_sdata);
624 } else if (conf->flags & IEEE80211_CONF_PS) {
625 conf->flags &= ~IEEE80211_CONF_PS;
626 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
627 del_timer_sync(&local->dynamic_ps_timer);
628 cancel_work_sync(&local->dynamic_ps_enable_work);
629 }
630}
631
808118cb
JY
632static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
633{
634 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
635 struct sta_info *sta = NULL;
c2c98fde 636 bool authorized = false;
808118cb
JY
637
638 if (!mgd->powersave)
639 return false;
640
641 if (!mgd->associated)
642 return false;
643
644 if (!mgd->associated->beacon_ies)
645 return false;
646
647 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
648 IEEE80211_STA_CONNECTION_POLL))
649 return false;
650
651 rcu_read_lock();
652 sta = sta_info_get(sdata, mgd->bssid);
653 if (sta)
c2c98fde 654 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
808118cb
JY
655 rcu_read_unlock();
656
c2c98fde 657 return authorized;
808118cb
JY
658}
659
965bedad 660/* need to hold RTNL or interface lock */
10f644a4 661void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
965bedad
JB
662{
663 struct ieee80211_sub_if_data *sdata, *found = NULL;
664 int count = 0;
195e294d 665 int timeout;
965bedad
JB
666
667 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
668 local->ps_sdata = NULL;
669 return;
670 }
671
af6b6374
JB
672 if (!list_empty(&local->work_list)) {
673 local->ps_sdata = NULL;
674 goto change;
675 }
676
965bedad 677 list_for_each_entry(sdata, &local->interfaces, list) {
9607e6b6 678 if (!ieee80211_sdata_running(sdata))
965bedad 679 continue;
8c7914de
RM
680 if (sdata->vif.type == NL80211_IFTYPE_AP) {
681 /* If an AP vif is found, then disable PS
682 * by setting the count to zero thereby setting
683 * ps_sdata to NULL.
684 */
685 count = 0;
686 break;
687 }
965bedad
JB
688 if (sdata->vif.type != NL80211_IFTYPE_STATION)
689 continue;
690 found = sdata;
691 count++;
692 }
693
808118cb 694 if (count == 1 && ieee80211_powersave_allowed(found)) {
f90754c1 695 struct ieee80211_conf *conf = &local->hw.conf;
10f644a4
JB
696 s32 beaconint_us;
697
698 if (latency < 0)
ed77134b 699 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
10f644a4
JB
700
701 beaconint_us = ieee80211_tu_to_usec(
702 found->vif.bss_conf.beacon_int);
703
ff616381 704 timeout = local->dynamic_ps_forced_timeout;
195e294d
JO
705 if (timeout < 0) {
706 /*
ff616381
JO
707 * Go to full PSM if the user configures a very low
708 * latency requirement.
0ab82b04
EP
709 * The 2000 second value is there for compatibility
710 * until the PM_QOS_NETWORK_LATENCY is configured
711 * with real values.
195e294d 712 */
0ab82b04
EP
713 if (latency > (1900 * USEC_PER_MSEC) &&
714 latency != (2000 * USEC_PER_SEC))
195e294d 715 timeout = 0;
ff616381
JO
716 else
717 timeout = 100;
195e294d 718 }
f90754c1
JO
719 local->dynamic_ps_user_timeout = timeout;
720 if (!local->disable_dynamic_ps)
721 conf->dynamic_ps_timeout =
722 local->dynamic_ps_user_timeout;
195e294d 723
04fe2037 724 if (beaconint_us > latency) {
10f644a4 725 local->ps_sdata = NULL;
04fe2037 726 } else {
56007a02 727 struct ieee80211_bss *bss;
04fe2037 728 int maxslp = 1;
56007a02 729 u8 dtimper;
04fe2037 730
56007a02
JB
731 bss = (void *)found->u.mgd.associated->priv;
732 dtimper = bss->dtim_period;
733
734 /* If the TIM IE is invalid, pretend the value is 1 */
735 if (!dtimper)
736 dtimper = 1;
737 else if (dtimper > 1)
04fe2037
JB
738 maxslp = min_t(int, dtimper,
739 latency / beaconint_us);
740
9ccebe61 741 local->hw.conf.max_sleep_period = maxslp;
56007a02 742 local->hw.conf.ps_dtim_period = dtimper;
10f644a4 743 local->ps_sdata = found;
04fe2037 744 }
10f644a4 745 } else {
965bedad 746 local->ps_sdata = NULL;
10f644a4 747 }
965bedad 748
af6b6374 749 change:
965bedad
JB
750 ieee80211_change_ps(local);
751}
752
753void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
754{
755 struct ieee80211_local *local =
756 container_of(work, struct ieee80211_local,
757 dynamic_ps_disable_work);
758
759 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
760 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
761 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
762 }
763
764 ieee80211_wake_queues_by_reason(&local->hw,
765 IEEE80211_QUEUE_STOP_REASON_PS);
766}
767
768void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
769{
770 struct ieee80211_local *local =
771 container_of(work, struct ieee80211_local,
772 dynamic_ps_enable_work);
773 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
5e34069c 774 struct ieee80211_if_managed *ifmgd;
1ddc2867
RM
775 unsigned long flags;
776 int q;
965bedad
JB
777
778 /* can only happen when PS was just disabled anyway */
779 if (!sdata)
780 return;
781
5e34069c
CL
782 ifmgd = &sdata->u.mgd;
783
965bedad
JB
784 if (local->hw.conf.flags & IEEE80211_CONF_PS)
785 return;
786
77b7023a
AN
787 if (!local->disable_dynamic_ps &&
788 local->hw.conf.dynamic_ps_timeout > 0) {
789 /* don't enter PS if TX frames are pending */
790 if (drv_tx_frames_pending(local)) {
1ddc2867
RM
791 mod_timer(&local->dynamic_ps_timer, jiffies +
792 msecs_to_jiffies(
793 local->hw.conf.dynamic_ps_timeout));
794 return;
795 }
77b7023a
AN
796
797 /*
798 * transmission can be stopped by others which leads to
799 * dynamic_ps_timer expiry. Postpone the ps timer if it
800 * is not the actual idle state.
801 */
802 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
803 for (q = 0; q < local->hw.queues; q++) {
804 if (local->queue_stop_reasons[q]) {
805 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
806 flags);
807 mod_timer(&local->dynamic_ps_timer, jiffies +
808 msecs_to_jiffies(
809 local->hw.conf.dynamic_ps_timeout));
810 return;
811 }
812 }
813 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1ddc2867 814 }
1ddc2867 815
375177bf 816 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
f3e85b9e
VN
817 (!(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED))) {
818 netif_tx_stop_all_queues(sdata->dev);
965bedad 819
e8306f98
VN
820 if (drv_tx_frames_pending(local))
821 mod_timer(&local->dynamic_ps_timer, jiffies +
822 msecs_to_jiffies(
823 local->hw.conf.dynamic_ps_timeout));
824 else {
825 ieee80211_send_nullfunc(local, sdata, 1);
826 /* Flush to get the tx status of nullfunc frame */
827 drv_flush(local, false);
828 }
f3e85b9e
VN
829 }
830
2a13052f
JO
831 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
832 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
375177bf
VN
833 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
834 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
835 local->hw.conf.flags |= IEEE80211_CONF_PS;
836 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
837 }
f3e85b9e 838
77b7023a
AN
839 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
840 netif_tx_wake_all_queues(sdata->dev);
965bedad
JB
841}
842
843void ieee80211_dynamic_ps_timer(unsigned long data)
844{
845 struct ieee80211_local *local = (void *) data;
846
78f1a8b7 847 if (local->quiescing || local->suspended)
5bb644a0
JB
848 return;
849
42935eca 850 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
965bedad
JB
851}
852
60f8b39c 853/* MLME */
f698d856 854static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
4ced3f74 855 struct ieee80211_sub_if_data *sdata,
f0706e82
JB
856 u8 *wmm_param, size_t wmm_param_len)
857{
f0706e82 858 struct ieee80211_tx_queue_params params;
4ced3f74 859 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
f0706e82
JB
860 size_t left;
861 int count;
ab13315a 862 u8 *pos, uapsd_queues = 0;
f0706e82 863
e1b3ec1a
SG
864 if (!local->ops->conf_tx)
865 return;
866
af6b6374 867 if (local->hw.queues < 4)
3434fbd3
JB
868 return;
869
870 if (!wmm_param)
871 return;
872
f0706e82
JB
873 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
874 return;
ab13315a
KV
875
876 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
50ae0cf1 877 uapsd_queues = local->uapsd_queues;
ab13315a 878
f0706e82 879 count = wmm_param[6] & 0x0f;
46900298 880 if (count == ifmgd->wmm_last_param_set)
f0706e82 881 return;
46900298 882 ifmgd->wmm_last_param_set = count;
f0706e82
JB
883
884 pos = wmm_param + 8;
885 left = wmm_param_len - 8;
886
887 memset(&params, 0, sizeof(params));
888
f0706e82
JB
889 local->wmm_acm = 0;
890 for (; left >= 4; left -= 4, pos += 4) {
891 int aci = (pos[0] >> 5) & 0x03;
892 int acm = (pos[0] >> 4) & 0x01;
ab13315a 893 bool uapsd = false;
f0706e82
JB
894 int queue;
895
896 switch (aci) {
0eeb59fe 897 case 1: /* AC_BK */
e100bb64 898 queue = 3;
988c0f72 899 if (acm)
0eeb59fe 900 local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
ab13315a
KV
901 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
902 uapsd = true;
f0706e82 903 break;
0eeb59fe 904 case 2: /* AC_VI */
e100bb64 905 queue = 1;
988c0f72 906 if (acm)
0eeb59fe 907 local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
ab13315a
KV
908 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
909 uapsd = true;
f0706e82 910 break;
0eeb59fe 911 case 3: /* AC_VO */
e100bb64 912 queue = 0;
988c0f72 913 if (acm)
0eeb59fe 914 local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
ab13315a
KV
915 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
916 uapsd = true;
f0706e82 917 break;
0eeb59fe 918 case 0: /* AC_BE */
f0706e82 919 default:
e100bb64 920 queue = 2;
988c0f72 921 if (acm)
0eeb59fe 922 local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
ab13315a
KV
923 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
924 uapsd = true;
f0706e82
JB
925 break;
926 }
927
928 params.aifs = pos[0] & 0x0f;
929 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
930 params.cw_min = ecw2cw(pos[1] & 0x0f);
f434b2d1 931 params.txop = get_unaligned_le16(pos + 2);
ab13315a
KV
932 params.uapsd = uapsd;
933
f4ea83dd 934#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0fb9a9ec
JP
935 wiphy_debug(local->hw.wiphy,
936 "WMM queue=%d aci=%d acm=%d aifs=%d "
937 "cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
938 queue, aci, acm,
939 params.aifs, params.cw_min, params.cw_max,
940 params.txop, params.uapsd);
3330d7be 941#endif
f6f3def3
EP
942 sdata->tx_conf[queue] = params;
943 if (drv_conf_tx(local, sdata, queue, &params))
0fb9a9ec
JP
944 wiphy_debug(local->hw.wiphy,
945 "failed to set TX queue parameters for queue %d\n",
946 queue);
f0706e82 947 }
e1b3ec1a
SG
948
949 /* enable WMM or activate new settings */
4ced3f74
JB
950 sdata->vif.bss_conf.qos = true;
951 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
f0706e82
JB
952}
953
7a5158ef
JB
954static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
955 u16 capab, bool erp_valid, u8 erp)
5628221c 956{
bda3933a 957 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
471b3efd 958 u32 changed = 0;
7a5158ef
JB
959 bool use_protection;
960 bool use_short_preamble;
961 bool use_short_slot;
962
963 if (erp_valid) {
964 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
965 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
966 } else {
967 use_protection = false;
968 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
969 }
970
971 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
43d35343
FF
972 if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
973 use_short_slot = true;
5628221c 974
471b3efd 975 if (use_protection != bss_conf->use_cts_prot) {
471b3efd
JB
976 bss_conf->use_cts_prot = use_protection;
977 changed |= BSS_CHANGED_ERP_CTS_PROT;
5628221c 978 }
7e9ed188 979
d43c7b37 980 if (use_short_preamble != bss_conf->use_short_preamble) {
d43c7b37 981 bss_conf->use_short_preamble = use_short_preamble;
471b3efd 982 changed |= BSS_CHANGED_ERP_PREAMBLE;
7e9ed188 983 }
d9430a32 984
7a5158ef 985 if (use_short_slot != bss_conf->use_short_slot) {
7a5158ef
JB
986 bss_conf->use_short_slot = use_short_slot;
987 changed |= BSS_CHANGED_ERP_SLOT;
50c4afb9
JL
988 }
989
990 return changed;
991}
992
f698d856 993static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
0c1ad2ca 994 struct cfg80211_bss *cbss,
ae5eb026 995 u32 bss_info_changed)
f0706e82 996{
0c1ad2ca 997 struct ieee80211_bss *bss = (void *)cbss->priv;
471b3efd 998 struct ieee80211_local *local = sdata->local;
68542962 999 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
d6f2da5b 1000
ae5eb026 1001 bss_info_changed |= BSS_CHANGED_ASSOC;
77fdaa12 1002 /* set timing information */
68542962
JO
1003 bss_conf->beacon_int = cbss->beacon_interval;
1004 bss_conf->timestamp = cbss->tsf;
5628221c 1005
77fdaa12
JB
1006 bss_info_changed |= BSS_CHANGED_BEACON_INT;
1007 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
0c1ad2ca 1008 cbss->capability, bss->has_erp_value, bss->erp_value);
21c0cbe7 1009
7ccc8bd7
FF
1010 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1011 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1012
0c1ad2ca
JB
1013 sdata->u.mgd.associated = cbss;
1014 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
f0706e82 1015
17e4ec14
JM
1016 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1017
b291ba11
JB
1018 /* just to be sure */
1019 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1020 IEEE80211_STA_BEACON_POLL);
1021
9ac19a90 1022 ieee80211_led_assoc(local, 1);
9306102e 1023
e5b900d2
JB
1024 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
1025 bss_conf->dtim_period = bss->dtim_period;
1026 else
1027 bss_conf->dtim_period = 0;
1028
68542962 1029 bss_conf->assoc = 1;
96dd22ac
JB
1030 /*
1031 * For now just always ask the driver to update the basic rateset
1032 * when we have associated, we aren't checking whether it actually
1033 * changed or not.
1034 */
ae5eb026 1035 bss_info_changed |= BSS_CHANGED_BASIC_RATES;
9cef8737
JB
1036
1037 /* And the BSSID changed - we're associated now */
1038 bss_info_changed |= BSS_CHANGED_BSSID;
1039
a97c13c3
JO
1040 /* Tell the driver to monitor connection quality (if supported) */
1041 if ((local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI) &&
68542962 1042 bss_conf->cqm_rssi_thold)
a97c13c3
JO
1043 bss_info_changed |= BSS_CHANGED_CQM;
1044
68542962
JO
1045 /* Enable ARP filtering */
1046 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
1047 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
1048 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1049 }
1050
ae5eb026 1051 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
f0706e82 1052
056508dc
JB
1053 mutex_lock(&local->iflist_mtx);
1054 ieee80211_recalc_ps(local, -1);
025e6be2 1055 ieee80211_recalc_smps(local);
056508dc 1056 mutex_unlock(&local->iflist_mtx);
e0cb686f 1057
8a5b33f5 1058 netif_tx_start_all_queues(sdata->dev);
9ac19a90 1059 netif_carrier_on(sdata->dev);
f0706e82
JB
1060}
1061
e69e95db 1062static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
53f73c09 1063 bool remove_sta, bool tx)
aa458d17 1064{
46900298 1065 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
aa458d17
TW
1066 struct ieee80211_local *local = sdata->local;
1067 struct sta_info *sta;
e0cb686f 1068 u32 changed = 0, config_changed = 0;
b291ba11 1069 u8 bssid[ETH_ALEN];
aa458d17 1070
77fdaa12
JB
1071 ASSERT_MGD_MTX(ifmgd);
1072
b291ba11
JB
1073 if (WARN_ON(!ifmgd->associated))
1074 return;
1075
0c1ad2ca 1076 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
b291ba11 1077
77fdaa12
JB
1078 ifmgd->associated = NULL;
1079 memset(ifmgd->bssid, 0, ETH_ALEN);
1080
1081 /*
1082 * we need to commit the associated = NULL change because the
1083 * scan code uses that to determine whether this iface should
1084 * go to/wake up from powersave or not -- and could otherwise
1085 * wake the queues erroneously.
1086 */
1087 smp_mb();
1088
1089 /*
1090 * Thus, we can only afterwards stop the queues -- to account
1091 * for the case where another CPU is finishing a scan at this
1092 * time -- we don't want the scan code to enable queues.
1093 */
aa458d17 1094
8a5b33f5 1095 netif_tx_stop_all_queues(sdata->dev);
aa458d17
TW
1096 netif_carrier_off(sdata->dev);
1097
2a419056 1098 mutex_lock(&local->sta_mtx);
abe60632 1099 sta = sta_info_get(sdata, bssid);
4cad6c7c 1100 if (sta) {
c2c98fde 1101 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
53f73c09 1102 ieee80211_sta_tear_down_BA_sessions(sta, tx);
4cad6c7c 1103 }
2a419056 1104 mutex_unlock(&local->sta_mtx);
aa458d17 1105
f5e5bf25
TW
1106 changed |= ieee80211_reset_erp_info(sdata);
1107
f5e5bf25 1108 ieee80211_led_assoc(local, 0);
ae5eb026
JB
1109 changed |= BSS_CHANGED_ASSOC;
1110 sdata->vif.bss_conf.assoc = false;
f5e5bf25 1111
aa837e1d
JB
1112 ieee80211_set_wmm_default(sdata);
1113
4797938c 1114 /* channel(_type) changes are handled by ieee80211_hw_config */
0aaffa9b 1115 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
e0cb686f 1116
413ad50a
JB
1117 /* on the next assoc, re-program HT parameters */
1118 sdata->ht_opmode_valid = false;
1119
a8302de9
VT
1120 local->power_constr_level = 0;
1121
520eb820
KV
1122 del_timer_sync(&local->dynamic_ps_timer);
1123 cancel_work_sync(&local->dynamic_ps_enable_work);
1124
e0cb686f
KV
1125 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1126 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1127 config_changed |= IEEE80211_CONF_CHANGE_PS;
1128 }
a331400b 1129 local->ps_sdata = NULL;
ae5eb026 1130
e0cb686f 1131 ieee80211_hw_config(local, config_changed);
9cef8737 1132
68542962
JO
1133 /* Disable ARP filtering */
1134 if (sdata->vif.bss_conf.arp_filter_enabled) {
1135 sdata->vif.bss_conf.arp_filter_enabled = false;
1136 changed |= BSS_CHANGED_ARP_FILTER;
1137 }
1138
0aaffa9b
JB
1139 /* The BSSID (not really interesting) and HT changed */
1140 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
ae5eb026 1141 ieee80211_bss_info_change_notify(sdata, changed);
8e268e47 1142
07ba55d7 1143 /* remove AP and TDLS peers */
e69e95db 1144 if (remove_sta)
07ba55d7 1145 sta_info_flush(local, sdata);
b9dcf712
JB
1146
1147 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1148 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1149 del_timer_sync(&sdata->u.mgd.timer);
1150 del_timer_sync(&sdata->u.mgd.chswitch_timer);
aa458d17 1151}
f0706e82 1152
3cf335d5
KV
1153void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1154 struct ieee80211_hdr *hdr)
1155{
1156 /*
1157 * We can postpone the mgd.timer whenever receiving unicast frames
1158 * from AP because we know that the connection is working both ways
1159 * at that time. But multicast frames (and hence also beacons) must
1160 * be ignored here, because we need to trigger the timer during
b291ba11
JB
1161 * data idle periods for sending the periodic probe request to the
1162 * AP we're connected to.
3cf335d5 1163 */
b291ba11
JB
1164 if (is_multicast_ether_addr(hdr->addr1))
1165 return;
1166
be099e82 1167 ieee80211_sta_reset_conn_monitor(sdata);
3cf335d5 1168}
f0706e82 1169
4e5ff376
FF
1170static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1171{
1172 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1173
1174 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1175 IEEE80211_STA_CONNECTION_POLL)))
1176 return;
1177
1178 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1179 IEEE80211_STA_BEACON_POLL);
1180 mutex_lock(&sdata->local->iflist_mtx);
1181 ieee80211_recalc_ps(sdata->local, -1);
1182 mutex_unlock(&sdata->local->iflist_mtx);
1183
1184 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1185 return;
1186
1187 /*
1188 * We've received a probe response, but are not sure whether
1189 * we have or will be receiving any beacons or data, so let's
1190 * schedule the timers again, just in case.
1191 */
1192 ieee80211_sta_reset_beacon_monitor(sdata);
1193
1194 mod_timer(&ifmgd->conn_mon_timer,
1195 round_jiffies_up(jiffies +
1196 IEEE80211_CONNECTION_IDLE_TIME));
1197}
1198
1199void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
04ac3c0e 1200 struct ieee80211_hdr *hdr, bool ack)
4e5ff376 1201{
75706d0e 1202 if (!ieee80211_is_data(hdr->frame_control))
4e5ff376
FF
1203 return;
1204
04ac3c0e
FF
1205 if (ack)
1206 ieee80211_sta_reset_conn_monitor(sdata);
4e5ff376
FF
1207
1208 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1209 sdata->u.mgd.probe_send_count > 0) {
04ac3c0e
FF
1210 if (ack)
1211 sdata->u.mgd.probe_send_count = 0;
1212 else
1213 sdata->u.mgd.nullfunc_failed = true;
4e5ff376
FF
1214 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1215 }
1216}
1217
a43abf29
ML
1218static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1219{
1220 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1221 const u8 *ssid;
f01a067d 1222 u8 *dst = ifmgd->associated->bssid;
180205bd 1223 u8 unicast_limit = max(1, max_probe_tries - 3);
f01a067d
LR
1224
1225 /*
1226 * Try sending broadcast probe requests for the last three
1227 * probe requests after the first ones failed since some
1228 * buggy APs only support broadcast probe requests.
1229 */
1230 if (ifmgd->probe_send_count >= unicast_limit)
1231 dst = NULL;
a43abf29 1232
4e5ff376
FF
1233 /*
1234 * When the hardware reports an accurate Tx ACK status, it's
1235 * better to send a nullfunc frame instead of a probe request,
1236 * as it will kick us off the AP quickly if we aren't associated
1237 * anymore. The timeout will be reset if the frame is ACKed by
1238 * the AP.
1239 */
04ac3c0e
FF
1240 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1241 ifmgd->nullfunc_failed = false;
4e5ff376 1242 ieee80211_send_nullfunc(sdata->local, sdata, 0);
04ac3c0e 1243 } else {
4e5ff376 1244 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
a806c558 1245 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid[1], NULL, 0,
aad14ceb 1246 (u32) -1, true, false);
4e5ff376 1247 }
a43abf29
ML
1248
1249 ifmgd->probe_send_count++;
180205bd 1250 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
a43abf29
ML
1251 run_again(ifmgd, ifmgd->probe_timeout);
1252}
1253
b291ba11
JB
1254static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1255 bool beacon)
04de8381 1256{
04de8381 1257 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
b291ba11 1258 bool already = false;
34bfc411 1259
9607e6b6 1260 if (!ieee80211_sdata_running(sdata))
0e2b6286
JB
1261 return;
1262
91a3bd76
LR
1263 if (sdata->local->scanning)
1264 return;
1265
b8bc4b0a
JB
1266 if (sdata->local->tmp_channel)
1267 return;
1268
77fdaa12
JB
1269 mutex_lock(&ifmgd->mtx);
1270
1271 if (!ifmgd->associated)
1272 goto out;
1273
a860402d 1274#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
b291ba11
JB
1275 if (beacon && net_ratelimit())
1276 printk(KERN_DEBUG "%s: detected beacon loss from AP "
47846c9b 1277 "- sending probe request\n", sdata->name);
a860402d 1278#endif
04de8381 1279
b291ba11
JB
1280 /*
1281 * The driver/our work has already reported this event or the
1282 * connection monitoring has kicked in and we have already sent
1283 * a probe request. Or maybe the AP died and the driver keeps
1284 * reporting until we disassociate...
1285 *
1286 * In either case we have to ignore the current call to this
1287 * function (except for setting the correct probe reason bit)
1288 * because otherwise we would reset the timer every time and
1289 * never check whether we received a probe response!
1290 */
1291 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1292 IEEE80211_STA_CONNECTION_POLL))
1293 already = true;
1294
1295 if (beacon)
1296 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1297 else
1298 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1299
1300 if (already)
1301 goto out;
1302
4e751843
JB
1303 mutex_lock(&sdata->local->iflist_mtx);
1304 ieee80211_recalc_ps(sdata->local, -1);
1305 mutex_unlock(&sdata->local->iflist_mtx);
1306
a43abf29
ML
1307 ifmgd->probe_send_count = 0;
1308 ieee80211_mgd_probe_ap_send(sdata);
77fdaa12
JB
1309 out:
1310 mutex_unlock(&ifmgd->mtx);
04de8381
KV
1311}
1312
a619a4c0
JO
1313struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1314 struct ieee80211_vif *vif)
1315{
1316 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1317 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1318 struct sk_buff *skb;
1319 const u8 *ssid;
1320
1321 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1322 return NULL;
1323
1324 ASSERT_MGD_MTX(ifmgd);
1325
1326 if (!ifmgd->associated)
1327 return NULL;
1328
1329 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1330 skb = ieee80211_build_probe_req(sdata, ifmgd->associated->bssid,
85a237fe
JB
1331 (u32) -1, ssid + 2, ssid[1],
1332 NULL, 0, true);
a619a4c0
JO
1333
1334 return skb;
1335}
1336EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1337
1e4dcd01
JO
1338static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
1339{
1340 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1341 struct ieee80211_local *local = sdata->local;
1342 u8 bssid[ETH_ALEN];
1343
1344 mutex_lock(&ifmgd->mtx);
1345 if (!ifmgd->associated) {
1346 mutex_unlock(&ifmgd->mtx);
1347 return;
1348 }
1349
1350 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
1351
180205bd
BG
1352 printk(KERN_DEBUG "%s: Connection to AP %pM lost.\n",
1353 sdata->name, bssid);
1e4dcd01 1354
53f73c09 1355 ieee80211_set_disassoc(sdata, true, true);
1e4dcd01 1356 mutex_unlock(&ifmgd->mtx);
7da7cc1d
JB
1357
1358 mutex_lock(&local->mtx);
1359 ieee80211_recalc_idle(local);
1360 mutex_unlock(&local->mtx);
1e4dcd01
JO
1361 /*
1362 * must be outside lock due to cfg80211,
1363 * but that's not a problem.
1364 */
1365 ieee80211_send_deauth_disassoc(sdata, bssid,
1366 IEEE80211_STYPE_DEAUTH,
1367 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
d5cdfacb 1368 NULL, true);
1e4dcd01
JO
1369}
1370
1371void ieee80211_beacon_connection_loss_work(struct work_struct *work)
b291ba11
JB
1372{
1373 struct ieee80211_sub_if_data *sdata =
1374 container_of(work, struct ieee80211_sub_if_data,
1e4dcd01 1375 u.mgd.beacon_connection_loss_work);
b291ba11 1376
1e4dcd01
JO
1377 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1378 __ieee80211_connection_loss(sdata);
1379 else
1380 ieee80211_mgd_probe_ap(sdata, true);
b291ba11
JB
1381}
1382
04de8381
KV
1383void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1384{
1385 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1e4dcd01 1386 struct ieee80211_hw *hw = &sdata->local->hw;
04de8381 1387
b5878a2d
JB
1388 trace_api_beacon_loss(sdata);
1389
1e4dcd01
JO
1390 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1391 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
04de8381
KV
1392}
1393EXPORT_SYMBOL(ieee80211_beacon_loss);
1394
1e4dcd01
JO
1395void ieee80211_connection_loss(struct ieee80211_vif *vif)
1396{
1397 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1398 struct ieee80211_hw *hw = &sdata->local->hw;
1399
b5878a2d
JB
1400 trace_api_connection_loss(sdata);
1401
1e4dcd01
JO
1402 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1403 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1404}
1405EXPORT_SYMBOL(ieee80211_connection_loss);
1406
1407
77fdaa12
JB
1408static enum rx_mgmt_action __must_check
1409ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
77fdaa12 1410 struct ieee80211_mgmt *mgmt, size_t len)
f0706e82 1411{
46900298 1412 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
77fdaa12 1413 const u8 *bssid = NULL;
f0706e82
JB
1414 u16 reason_code;
1415
f4ea83dd 1416 if (len < 24 + 2)
77fdaa12 1417 return RX_MGMT_NONE;
f0706e82 1418
77fdaa12
JB
1419 ASSERT_MGD_MTX(ifmgd);
1420
0c1ad2ca 1421 bssid = ifmgd->associated->bssid;
f0706e82
JB
1422
1423 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1424
77fdaa12 1425 printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
47846c9b 1426 sdata->name, bssid, reason_code);
77fdaa12 1427
53f73c09 1428 ieee80211_set_disassoc(sdata, true, false);
7da7cc1d 1429 mutex_lock(&sdata->local->mtx);
63f170e0 1430 ieee80211_recalc_idle(sdata->local);
7da7cc1d 1431 mutex_unlock(&sdata->local->mtx);
f0706e82 1432
77fdaa12 1433 return RX_MGMT_CFG80211_DEAUTH;
f0706e82
JB
1434}
1435
1436
77fdaa12
JB
1437static enum rx_mgmt_action __must_check
1438ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1439 struct ieee80211_mgmt *mgmt, size_t len)
f0706e82 1440{
46900298 1441 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
f0706e82
JB
1442 u16 reason_code;
1443
f4ea83dd 1444 if (len < 24 + 2)
77fdaa12 1445 return RX_MGMT_NONE;
f0706e82 1446
77fdaa12
JB
1447 ASSERT_MGD_MTX(ifmgd);
1448
1449 if (WARN_ON(!ifmgd->associated))
1450 return RX_MGMT_NONE;
1451
0c1ad2ca 1452 if (WARN_ON(memcmp(ifmgd->associated->bssid, mgmt->sa, ETH_ALEN)))
77fdaa12 1453 return RX_MGMT_NONE;
f0706e82
JB
1454
1455 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1456
0ff71613 1457 printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
47846c9b 1458 sdata->name, mgmt->sa, reason_code);
f0706e82 1459
53f73c09 1460 ieee80211_set_disassoc(sdata, true, false);
7da7cc1d 1461 mutex_lock(&sdata->local->mtx);
bc83b681 1462 ieee80211_recalc_idle(sdata->local);
7da7cc1d 1463 mutex_unlock(&sdata->local->mtx);
77fdaa12 1464 return RX_MGMT_CFG80211_DISASSOC;
f0706e82
JB
1465}
1466
1467
af6b6374
JB
1468static bool ieee80211_assoc_success(struct ieee80211_work *wk,
1469 struct ieee80211_mgmt *mgmt, size_t len)
f0706e82 1470{
af6b6374 1471 struct ieee80211_sub_if_data *sdata = wk->sdata;
46900298 1472 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
471b3efd 1473 struct ieee80211_local *local = sdata->local;
8318d78a 1474 struct ieee80211_supported_band *sband;
f0706e82 1475 struct sta_info *sta;
0c1ad2ca 1476 struct cfg80211_bss *cbss = wk->assoc.bss;
af6b6374 1477 u8 *pos;
881d948c 1478 u32 rates, basic_rates;
af6b6374 1479 u16 capab_info, aid;
f0706e82 1480 struct ieee802_11_elems elems;
bda3933a 1481 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
ae5eb026 1482 u32 changed = 0;
5d1ec85f
JB
1483 int i, j, err;
1484 bool have_higher_than_11mbit = false;
ae5eb026 1485 u16 ap_ht_cap_flags;
f0706e82 1486
af6b6374 1487 /* AssocResp and ReassocResp have identical structure */
f0706e82 1488
f0706e82 1489 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
af6b6374 1490 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
f0706e82 1491
1dd84aa2
JB
1492 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1493 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
47846c9b 1494 "set\n", sdata->name, aid);
1dd84aa2
JB
1495 aid &= ~(BIT(15) | BIT(14));
1496
af6b6374
JB
1497 pos = mgmt->u.assoc_resp.variable;
1498 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1499
f0706e82
JB
1500 if (!elems.supp_rates) {
1501 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
47846c9b 1502 sdata->name);
af6b6374 1503 return false;
f0706e82
JB
1504 }
1505
46900298 1506 ifmgd->aid = aid;
f0706e82 1507
2a33bee2
GE
1508 mutex_lock(&sdata->local->sta_mtx);
1509 /*
1510 * station info was already allocated and inserted before
1511 * the association and should be available to us
1512 */
1513 sta = sta_info_get_rx(sdata, cbss->bssid);
1514 if (WARN_ON(!sta)) {
1515 mutex_unlock(&sdata->local->sta_mtx);
af6b6374 1516 return false;
77fdaa12 1517 }
05e5e883 1518
c2c98fde
JB
1519 set_sta_flag(sta, WLAN_STA_AUTH);
1520 set_sta_flag(sta, WLAN_STA_ASSOC);
1521 set_sta_flag(sta, WLAN_STA_ASSOC_AP);
5d1ec85f 1522 if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
c2c98fde 1523 set_sta_flag(sta, WLAN_STA_AUTHORIZED);
5d1ec85f 1524
60f8b39c
JB
1525 rates = 0;
1526 basic_rates = 0;
e7480bbb 1527 sband = local->hw.wiphy->bands[wk->chan->band];
f709fc69 1528
60f8b39c
JB
1529 for (i = 0; i < elems.supp_rates_len; i++) {
1530 int rate = (elems.supp_rates[i] & 0x7f) * 5;
d61272cb 1531 bool is_basic = !!(elems.supp_rates[i] & 0x80);
f709fc69 1532
60f8b39c
JB
1533 if (rate > 110)
1534 have_higher_than_11mbit = true;
1535
1536 for (j = 0; j < sband->n_bitrates; j++) {
d61272cb 1537 if (sband->bitrates[j].bitrate == rate) {
60f8b39c 1538 rates |= BIT(j);
d61272cb
TW
1539 if (is_basic)
1540 basic_rates |= BIT(j);
1541 break;
1542 }
f709fc69 1543 }
f709fc69
LCC
1544 }
1545
60f8b39c
JB
1546 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1547 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
7e0986c1 1548 bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
f0706e82 1549
60f8b39c
JB
1550 if (rate > 110)
1551 have_higher_than_11mbit = true;
f0706e82 1552
60f8b39c 1553 for (j = 0; j < sband->n_bitrates; j++) {
d61272cb 1554 if (sband->bitrates[j].bitrate == rate) {
60f8b39c 1555 rates |= BIT(j);
d61272cb
TW
1556 if (is_basic)
1557 basic_rates |= BIT(j);
1558 break;
1559 }
60f8b39c 1560 }
1ebebea8 1561 }
f0706e82 1562
e7480bbb 1563 sta->sta.supp_rates[wk->chan->band] = rates;
bda3933a 1564 sdata->vif.bss_conf.basic_rates = basic_rates;
60f8b39c
JB
1565
1566 /* cf. IEEE 802.11 9.2.12 */
e7480bbb 1567 if (wk->chan->band == IEEE80211_BAND_2GHZ &&
60f8b39c
JB
1568 have_higher_than_11mbit)
1569 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1570 else
1571 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
f0706e82 1572
ab1faead 1573 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
ae5eb026 1574 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
d9fe60de 1575 elems.ht_cap_elem, &sta->sta.ht_cap);
ae5eb026
JB
1576
1577 ap_ht_cap_flags = sta->sta.ht_cap.cap;
f0706e82 1578
4b7679a5 1579 rate_control_rate_init(sta);
f0706e82 1580
46900298 1581 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
c2c98fde 1582 set_sta_flag(sta, WLAN_STA_MFP);
5394af4d 1583
ddf4ac53 1584 if (elems.wmm_param)
c2c98fde 1585 set_sta_flag(sta, WLAN_STA_WME);
ddf4ac53 1586
2a33bee2
GE
1587 /* sta_info_reinsert will also unlock the mutex lock */
1588 err = sta_info_reinsert(sta);
5d1ec85f
JB
1589 sta = NULL;
1590 if (err) {
1591 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1592 " the AP (error %d)\n", sdata->name, err);
90be561b 1593 return false;
ddf4ac53
JB
1594 }
1595
f2176d72
JO
1596 /*
1597 * Always handle WMM once after association regardless
1598 * of the first value the AP uses. Setting -1 here has
1599 * that effect because the AP values is an unsigned
1600 * 4-bit value.
1601 */
1602 ifmgd->wmm_last_param_set = -1;
1603
ddf4ac53 1604 if (elems.wmm_param)
4ced3f74 1605 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
60f8b39c 1606 elems.wmm_param_len);
aa837e1d
JB
1607 else
1608 ieee80211_set_wmm_default(sdata);
f0706e82 1609
e4da8c37
JB
1610 local->oper_channel = wk->chan;
1611
ae5eb026 1612 if (elems.ht_info_elem && elems.wmm_param &&
af6b6374 1613 (sdata->local->hw.queues >= 4) &&
ab1faead 1614 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
ae5eb026 1615 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
7cc44ed4
RM
1616 cbss->bssid, ap_ht_cap_flags,
1617 false);
ae5eb026 1618
60f8b39c
JB
1619 /* set AID and assoc capability,
1620 * ieee80211_set_associated() will tell the driver */
1621 bss_conf->aid = aid;
1622 bss_conf->assoc_capability = capab_info;
0c1ad2ca 1623 ieee80211_set_associated(sdata, cbss, changed);
f0706e82 1624
d524215f
FF
1625 /*
1626 * If we're using 4-addr mode, let the AP know that we're
1627 * doing so, so that it can create the STA VLAN on its side
1628 */
1629 if (ifmgd->use_4addr)
1630 ieee80211_send_4addr_nullfunc(local, sdata);
1631
15b7b062 1632 /*
b291ba11
JB
1633 * Start timer to probe the connection to the AP now.
1634 * Also start the timer that will detect beacon loss.
15b7b062 1635 */
b291ba11 1636 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
d3a910a8 1637 ieee80211_sta_reset_beacon_monitor(sdata);
15b7b062 1638
af6b6374 1639 return true;
f0706e82
JB
1640}
1641
1642
98c8fccf
JB
1643static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1644 struct ieee80211_mgmt *mgmt,
1645 size_t len,
1646 struct ieee80211_rx_status *rx_status,
1647 struct ieee802_11_elems *elems,
1648 bool beacon)
1649{
1650 struct ieee80211_local *local = sdata->local;
1651 int freq;
c2b13452 1652 struct ieee80211_bss *bss;
98c8fccf 1653 struct ieee80211_channel *channel;
56007a02
JB
1654 bool need_ps = false;
1655
1656 if (sdata->u.mgd.associated) {
1657 bss = (void *)sdata->u.mgd.associated->priv;
1658 /* not previously set so we may need to recalc */
1659 need_ps = !bss->dtim_period;
1660 }
98c8fccf
JB
1661
1662 if (elems->ds_params && elems->ds_params_len == 1)
59eb21a6
BR
1663 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
1664 rx_status->band);
98c8fccf
JB
1665 else
1666 freq = rx_status->freq;
1667
1668 channel = ieee80211_get_channel(local->hw.wiphy, freq);
1669
1670 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1671 return;
1672
98c8fccf 1673 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
2a519311 1674 channel, beacon);
77fdaa12
JB
1675 if (bss)
1676 ieee80211_rx_bss_put(local, bss);
1677
1678 if (!sdata->u.mgd.associated)
98c8fccf
JB
1679 return;
1680
56007a02
JB
1681 if (need_ps) {
1682 mutex_lock(&local->iflist_mtx);
1683 ieee80211_recalc_ps(local, -1);
1684 mutex_unlock(&local->iflist_mtx);
1685 }
1686
c481ec97 1687 if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
0c1ad2ca 1688 (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
77fdaa12 1689 ETH_ALEN) == 0)) {
c481ec97
S
1690 struct ieee80211_channel_sw_ie *sw_elem =
1691 (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
5ce6e438
JB
1692 ieee80211_sta_process_chanswitch(sdata, sw_elem,
1693 bss, rx_status->mactime);
c481ec97 1694 }
f0706e82
JB
1695}
1696
1697
f698d856 1698static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
af6b6374 1699 struct sk_buff *skb)
f0706e82 1700{
af6b6374 1701 struct ieee80211_mgmt *mgmt = (void *)skb->data;
15b7b062 1702 struct ieee80211_if_managed *ifmgd;
af6b6374
JB
1703 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
1704 size_t baselen, len = skb->len;
ae6a44e3
EK
1705 struct ieee802_11_elems elems;
1706
15b7b062
KV
1707 ifmgd = &sdata->u.mgd;
1708
77fdaa12
JB
1709 ASSERT_MGD_MTX(ifmgd);
1710
47846c9b 1711 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
8e7cdbb6
TW
1712 return; /* ignore ProbeResp to foreign address */
1713
ae6a44e3
EK
1714 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1715 if (baselen > len)
1716 return;
1717
1718 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1719 &elems);
1720
98c8fccf 1721 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
9859b81e 1722
77fdaa12 1723 if (ifmgd->associated &&
4e5ff376
FF
1724 memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN) == 0)
1725 ieee80211_reset_ap_probe(sdata);
f0706e82
JB
1726}
1727
d91f36db
JB
1728/*
1729 * This is the canonical list of information elements we care about,
1730 * the filter code also gives us all changes to the Microsoft OUI
1731 * (00:50:F2) vendor IE which is used for WMM which we need to track.
1732 *
1733 * We implement beacon filtering in software since that means we can
1734 * avoid processing the frame here and in cfg80211, and userspace
1735 * will not be able to tell whether the hardware supports it or not.
1736 *
1737 * XXX: This list needs to be dynamic -- userspace needs to be able to
1738 * add items it requires. It also needs to be able to tell us to
1739 * look out for other vendor IEs.
1740 */
1741static const u64 care_about_ies =
1d4df3a5
JB
1742 (1ULL << WLAN_EID_COUNTRY) |
1743 (1ULL << WLAN_EID_ERP_INFO) |
1744 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
1745 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
1746 (1ULL << WLAN_EID_HT_CAPABILITY) |
1747 (1ULL << WLAN_EID_HT_INFORMATION);
d91f36db 1748
f698d856 1749static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
f0706e82
JB
1750 struct ieee80211_mgmt *mgmt,
1751 size_t len,
1752 struct ieee80211_rx_status *rx_status)
1753{
d91f36db 1754 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
17e4ec14 1755 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
f0706e82
JB
1756 size_t baselen;
1757 struct ieee802_11_elems elems;
f698d856 1758 struct ieee80211_local *local = sdata->local;
471b3efd 1759 u32 changed = 0;
d91f36db 1760 bool erp_valid, directed_tim = false;
7a5158ef 1761 u8 erp_value = 0;
d91f36db 1762 u32 ncrc;
77fdaa12
JB
1763 u8 *bssid;
1764
1765 ASSERT_MGD_MTX(ifmgd);
f0706e82 1766
ae6a44e3
EK
1767 /* Process beacon from the current BSS */
1768 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1769 if (baselen > len)
1770 return;
1771
d91f36db 1772 if (rx_status->freq != local->hw.conf.channel->center_freq)
f0706e82 1773 return;
f0706e82 1774
b291ba11
JB
1775 /*
1776 * We might have received a number of frames, among them a
1777 * disassoc frame and a beacon...
1778 */
1779 if (!ifmgd->associated)
77fdaa12
JB
1780 return;
1781
0c1ad2ca 1782 bssid = ifmgd->associated->bssid;
77fdaa12 1783
b291ba11
JB
1784 /*
1785 * And in theory even frames from a different AP we were just
1786 * associated to a split-second ago!
1787 */
1788 if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
f0706e82
JB
1789 return;
1790
17e4ec14
JM
1791 /* Track average RSSI from the Beacon frames of the current AP */
1792 ifmgd->last_beacon_signal = rx_status->signal;
1793 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
1794 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
3ba06c6f 1795 ifmgd->ave_beacon_signal = rx_status->signal * 16;
17e4ec14 1796 ifmgd->last_cqm_event_signal = 0;
391a200a 1797 ifmgd->count_beacon_signal = 1;
615f7b9b 1798 ifmgd->last_ave_beacon_signal = 0;
17e4ec14
JM
1799 } else {
1800 ifmgd->ave_beacon_signal =
1801 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
1802 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
1803 ifmgd->ave_beacon_signal) / 16;
391a200a 1804 ifmgd->count_beacon_signal++;
17e4ec14 1805 }
615f7b9b
MV
1806
1807 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
1808 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
1809 int sig = ifmgd->ave_beacon_signal;
1810 int last_sig = ifmgd->last_ave_beacon_signal;
1811
1812 /*
1813 * if signal crosses either of the boundaries, invoke callback
1814 * with appropriate parameters
1815 */
1816 if (sig > ifmgd->rssi_max_thold &&
1817 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
1818 ifmgd->last_ave_beacon_signal = sig;
1819 drv_rssi_callback(local, RSSI_EVENT_HIGH);
1820 } else if (sig < ifmgd->rssi_min_thold &&
1821 (last_sig >= ifmgd->rssi_max_thold ||
1822 last_sig == 0)) {
1823 ifmgd->last_ave_beacon_signal = sig;
1824 drv_rssi_callback(local, RSSI_EVENT_LOW);
1825 }
1826 }
1827
17e4ec14 1828 if (bss_conf->cqm_rssi_thold &&
391a200a 1829 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
17e4ec14
JM
1830 !(local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI)) {
1831 int sig = ifmgd->ave_beacon_signal / 16;
1832 int last_event = ifmgd->last_cqm_event_signal;
1833 int thold = bss_conf->cqm_rssi_thold;
1834 int hyst = bss_conf->cqm_rssi_hyst;
1835 if (sig < thold &&
1836 (last_event == 0 || sig < last_event - hyst)) {
1837 ifmgd->last_cqm_event_signal = sig;
1838 ieee80211_cqm_rssi_notify(
1839 &sdata->vif,
1840 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
1841 GFP_KERNEL);
1842 } else if (sig > thold &&
1843 (last_event == 0 || sig > last_event + hyst)) {
1844 ifmgd->last_cqm_event_signal = sig;
1845 ieee80211_cqm_rssi_notify(
1846 &sdata->vif,
1847 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
1848 GFP_KERNEL);
1849 }
1850 }
1851
b291ba11 1852 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
92778180
JM
1853#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1854 if (net_ratelimit()) {
1855 printk(KERN_DEBUG "%s: cancelling probereq poll due "
47846c9b 1856 "to a received beacon\n", sdata->name);
92778180
JM
1857 }
1858#endif
b291ba11 1859 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
4e751843
JB
1860 mutex_lock(&local->iflist_mtx);
1861 ieee80211_recalc_ps(local, -1);
1862 mutex_unlock(&local->iflist_mtx);
92778180
JM
1863 }
1864
b291ba11
JB
1865 /*
1866 * Push the beacon loss detection into the future since
1867 * we are processing a beacon from the AP just now.
1868 */
d3a910a8 1869 ieee80211_sta_reset_beacon_monitor(sdata);
b291ba11 1870
d91f36db
JB
1871 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
1872 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
1873 len - baselen, &elems,
1874 care_about_ies, ncrc);
1875
1876 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
e7ec86f5
JB
1877 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
1878 ifmgd->aid);
d91f36db 1879
d8ec4433 1880 if (ncrc != ifmgd->beacon_crc || !ifmgd->beacon_crc_valid) {
30196673
JM
1881 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
1882 true);
d91f36db 1883
4ced3f74 1884 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
30196673
JM
1885 elems.wmm_param_len);
1886 }
fe3fa827 1887
25c9c875 1888 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
1fb3606b 1889 if (directed_tim) {
572e0012
KV
1890 if (local->hw.conf.dynamic_ps_timeout > 0) {
1891 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1892 ieee80211_hw_config(local,
1893 IEEE80211_CONF_CHANGE_PS);
1894 ieee80211_send_nullfunc(local, sdata, 0);
1895 } else {
1896 local->pspolling = true;
1897
1898 /*
1899 * Here is assumed that the driver will be
1900 * able to send ps-poll frame and receive a
1901 * response even though power save mode is
1902 * enabled, but some drivers might require
1903 * to disable power save here. This needs
1904 * to be investigated.
1905 */
1906 ieee80211_send_pspoll(local, sdata);
1907 }
a97b77b9
VN
1908 }
1909 }
7a5158ef 1910
d8ec4433 1911 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
30196673
JM
1912 return;
1913 ifmgd->beacon_crc = ncrc;
d8ec4433 1914 ifmgd->beacon_crc_valid = true;
30196673 1915
7a5158ef
JB
1916 if (elems.erp_info && elems.erp_info_len >= 1) {
1917 erp_valid = true;
1918 erp_value = elems.erp_info[0];
1919 } else {
1920 erp_valid = false;
50c4afb9 1921 }
7a5158ef
JB
1922 changed |= ieee80211_handle_bss_capability(sdata,
1923 le16_to_cpu(mgmt->u.beacon.capab_info),
1924 erp_valid, erp_value);
f0706e82 1925
d3c990fb 1926
53d6f81c 1927 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
ab1faead 1928 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
ae5eb026
JB
1929 struct sta_info *sta;
1930 struct ieee80211_supported_band *sband;
1931 u16 ap_ht_cap_flags;
1932
1933 rcu_read_lock();
1934
abe60632 1935 sta = sta_info_get(sdata, bssid);
77fdaa12 1936 if (WARN_ON(!sta)) {
ae5eb026
JB
1937 rcu_read_unlock();
1938 return;
1939 }
1940
1941 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1942
1943 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1944 elems.ht_cap_elem, &sta->sta.ht_cap);
1945
1946 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1947
1948 rcu_read_unlock();
1949
1950 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
7cc44ed4 1951 bssid, ap_ht_cap_flags, true);
d3c990fb
RR
1952 }
1953
8b19e6ca 1954 /* Note: country IE parsing is done for us by cfg80211 */
3f2355cb 1955 if (elems.country_elem) {
a8302de9
VT
1956 /* TODO: IBSS also needs this */
1957 if (elems.pwr_constr_elem)
1958 ieee80211_handle_pwr_constr(sdata,
1959 le16_to_cpu(mgmt->u.probe_resp.capab_info),
1960 elems.pwr_constr_elem,
1961 elems.pwr_constr_elem_len);
3f2355cb
LR
1962 }
1963
471b3efd 1964 ieee80211_bss_info_change_notify(sdata, changed);
f0706e82
JB
1965}
1966
1fa57d01
JB
1967void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1968 struct sk_buff *skb)
f0706e82 1969{
77fdaa12 1970 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
f0706e82 1971 struct ieee80211_rx_status *rx_status;
f0706e82 1972 struct ieee80211_mgmt *mgmt;
77fdaa12 1973 enum rx_mgmt_action rma = RX_MGMT_NONE;
f0706e82
JB
1974 u16 fc;
1975
f0706e82
JB
1976 rx_status = (struct ieee80211_rx_status *) skb->cb;
1977 mgmt = (struct ieee80211_mgmt *) skb->data;
1978 fc = le16_to_cpu(mgmt->frame_control);
1979
77fdaa12
JB
1980 mutex_lock(&ifmgd->mtx);
1981
1982 if (ifmgd->associated &&
0c1ad2ca 1983 memcmp(ifmgd->associated->bssid, mgmt->bssid, ETH_ALEN) == 0) {
77fdaa12
JB
1984 switch (fc & IEEE80211_FCTL_STYPE) {
1985 case IEEE80211_STYPE_BEACON:
1986 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
1987 rx_status);
1988 break;
1989 case IEEE80211_STYPE_PROBE_RESP:
af6b6374 1990 ieee80211_rx_mgmt_probe_resp(sdata, skb);
77fdaa12
JB
1991 break;
1992 case IEEE80211_STYPE_DEAUTH:
63f170e0 1993 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
77fdaa12
JB
1994 break;
1995 case IEEE80211_STYPE_DISASSOC:
1996 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
1997 break;
1998 case IEEE80211_STYPE_ACTION:
8b9a4e6e 1999 switch (mgmt->u.action.category) {
8b9a4e6e
JB
2000 case WLAN_CATEGORY_SPECTRUM_MGMT:
2001 ieee80211_sta_process_chanswitch(sdata,
2002 &mgmt->u.action.u.chan_switch.sw_elem,
2003 (void *)ifmgd->associated->priv,
2004 rx_status->mactime);
2005 break;
2006 }
77fdaa12
JB
2007 }
2008 mutex_unlock(&ifmgd->mtx);
2009
2010 switch (rma) {
2011 case RX_MGMT_NONE:
2012 /* no action */
2013 break;
2014 case RX_MGMT_CFG80211_DEAUTH:
ce470613 2015 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
77fdaa12
JB
2016 break;
2017 case RX_MGMT_CFG80211_DISASSOC:
ce470613 2018 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
77fdaa12
JB
2019 break;
2020 default:
2021 WARN(1, "unexpected: %d", rma);
2022 }
36b3a628 2023 return;
77fdaa12
JB
2024 }
2025
77fdaa12
JB
2026 mutex_unlock(&ifmgd->mtx);
2027
63f170e0 2028 if (skb->len >= 24 + 2 /* mgmt + deauth reason */ &&
b054b747
JB
2029 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_DEAUTH) {
2030 struct ieee80211_local *local = sdata->local;
2031 struct ieee80211_work *wk;
2032
a1699b75 2033 mutex_lock(&local->mtx);
b054b747
JB
2034 list_for_each_entry(wk, &local->work_list, list) {
2035 if (wk->sdata != sdata)
2036 continue;
2037
e5b900d2
JB
2038 if (wk->type != IEEE80211_WORK_ASSOC &&
2039 wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
b054b747
JB
2040 continue;
2041
2042 if (memcmp(mgmt->bssid, wk->filter_ta, ETH_ALEN))
2043 continue;
2044 if (memcmp(mgmt->sa, wk->filter_ta, ETH_ALEN))
2045 continue;
2046
2047 /*
2048 * Printing the message only here means we can't
2049 * spuriously print it, but it also means that it
2050 * won't be printed when the frame comes in before
2051 * we even tried to associate or in similar cases.
2052 *
2053 * Ultimately, I suspect cfg80211 should print the
2054 * messages instead.
2055 */
2056 printk(KERN_DEBUG
2057 "%s: deauthenticated from %pM (Reason: %u)\n",
2058 sdata->name, mgmt->bssid,
2059 le16_to_cpu(mgmt->u.deauth.reason_code));
2060
2061 list_del_rcu(&wk->list);
2062 free_work(wk);
2063 break;
2064 }
a1699b75 2065 mutex_unlock(&local->mtx);
f0706e82 2066
ce470613 2067 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
b054b747 2068 }
f0706e82
JB
2069}
2070
9c6bd790 2071static void ieee80211_sta_timer(unsigned long data)
f0706e82 2072{
60f8b39c
JB
2073 struct ieee80211_sub_if_data *sdata =
2074 (struct ieee80211_sub_if_data *) data;
46900298 2075 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
60f8b39c 2076 struct ieee80211_local *local = sdata->local;
f0706e82 2077
5bb644a0
JB
2078 if (local->quiescing) {
2079 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2080 return;
2081 }
2082
64592c8f 2083 ieee80211_queue_work(&local->hw, &sdata->work);
f0706e82
JB
2084}
2085
04ac3c0e 2086static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
95acac61 2087 u8 *bssid, u8 reason)
04ac3c0e
FF
2088{
2089 struct ieee80211_local *local = sdata->local;
2090 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2091
2092 ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
2093 IEEE80211_STA_BEACON_POLL);
2094
2095 ieee80211_set_disassoc(sdata, true, true);
2096 mutex_unlock(&ifmgd->mtx);
2097 mutex_lock(&local->mtx);
2098 ieee80211_recalc_idle(local);
2099 mutex_unlock(&local->mtx);
2100 /*
2101 * must be outside lock due to cfg80211,
2102 * but that's not a problem.
2103 */
2104 ieee80211_send_deauth_disassoc(sdata, bssid,
95acac61 2105 IEEE80211_STYPE_DEAUTH, reason,
04ac3c0e
FF
2106 NULL, true);
2107 mutex_lock(&ifmgd->mtx);
2108}
2109
1fa57d01 2110void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
9c6bd790 2111{
9c6bd790 2112 struct ieee80211_local *local = sdata->local;
1fa57d01 2113 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9c6bd790 2114
77fdaa12
JB
2115 /* then process the rest of the work */
2116 mutex_lock(&ifmgd->mtx);
2117
b291ba11
JB
2118 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2119 IEEE80211_STA_CONNECTION_POLL) &&
2120 ifmgd->associated) {
a43abf29 2121 u8 bssid[ETH_ALEN];
72a8a3ed 2122 int max_tries;
a43abf29 2123
0c1ad2ca 2124 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4e5ff376 2125
72a8a3ed 2126 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
180205bd 2127 max_tries = max_nullfunc_tries;
72a8a3ed 2128 else
180205bd 2129 max_tries = max_probe_tries;
72a8a3ed 2130
4e5ff376
FF
2131 /* ACK received for nullfunc probing frame */
2132 if (!ifmgd->probe_send_count)
2133 ieee80211_reset_ap_probe(sdata);
04ac3c0e
FF
2134 else if (ifmgd->nullfunc_failed) {
2135 if (ifmgd->probe_send_count < max_tries) {
2136#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2137 wiphy_debug(local->hw.wiphy,
2138 "%s: No ack for nullfunc frame to"
bfc31df3 2139 " AP %pM, try %d/%i\n",
04ac3c0e 2140 sdata->name, bssid,
bfc31df3 2141 ifmgd->probe_send_count, max_tries);
04ac3c0e
FF
2142#endif
2143 ieee80211_mgd_probe_ap_send(sdata);
2144 } else {
2145#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2146 wiphy_debug(local->hw.wiphy,
2147 "%s: No ack for nullfunc frame to"
2148 " AP %pM, disconnecting.\n",
c658e5db 2149 sdata->name, bssid);
04ac3c0e 2150#endif
95acac61
JB
2151 ieee80211_sta_connection_lost(sdata, bssid,
2152 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
04ac3c0e
FF
2153 }
2154 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
b291ba11 2155 run_again(ifmgd, ifmgd->probe_timeout);
04ac3c0e
FF
2156 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
2157#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2158 wiphy_debug(local->hw.wiphy,
2159 "%s: Failed to send nullfunc to AP %pM"
2160 " after %dms, disconnecting.\n",
2161 sdata->name,
180205bd 2162 bssid, probe_wait_ms);
04ac3c0e 2163#endif
95acac61
JB
2164 ieee80211_sta_connection_lost(sdata, bssid,
2165 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
04ac3c0e 2166 } else if (ifmgd->probe_send_count < max_tries) {
a43abf29 2167#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
b38afa87
BG
2168 wiphy_debug(local->hw.wiphy,
2169 "%s: No probe response from AP %pM"
bfc31df3 2170 " after %dms, try %d/%i\n",
b38afa87 2171 sdata->name,
180205bd 2172 bssid, probe_wait_ms,
bfc31df3 2173 ifmgd->probe_send_count, max_tries);
a43abf29
ML
2174#endif
2175 ieee80211_mgd_probe_ap_send(sdata);
2176 } else {
b291ba11
JB
2177 /*
2178 * We actually lost the connection ... or did we?
2179 * Let's make sure!
2180 */
b38afa87
BG
2181 wiphy_debug(local->hw.wiphy,
2182 "%s: No probe response from AP %pM"
2183 " after %dms, disconnecting.\n",
2184 sdata->name,
180205bd 2185 bssid, probe_wait_ms);
04ac3c0e 2186
95acac61
JB
2187 ieee80211_sta_connection_lost(sdata, bssid,
2188 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
b291ba11
JB
2189 }
2190 }
2191
77fdaa12 2192 mutex_unlock(&ifmgd->mtx);
f0706e82
JB
2193}
2194
b291ba11
JB
2195static void ieee80211_sta_bcn_mon_timer(unsigned long data)
2196{
2197 struct ieee80211_sub_if_data *sdata =
2198 (struct ieee80211_sub_if_data *) data;
2199 struct ieee80211_local *local = sdata->local;
2200
2201 if (local->quiescing)
2202 return;
2203
1e4dcd01
JO
2204 ieee80211_queue_work(&sdata->local->hw,
2205 &sdata->u.mgd.beacon_connection_loss_work);
b291ba11
JB
2206}
2207
2208static void ieee80211_sta_conn_mon_timer(unsigned long data)
2209{
2210 struct ieee80211_sub_if_data *sdata =
2211 (struct ieee80211_sub_if_data *) data;
2212 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2213 struct ieee80211_local *local = sdata->local;
2214
2215 if (local->quiescing)
2216 return;
2217
42935eca 2218 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
b291ba11
JB
2219}
2220
2221static void ieee80211_sta_monitor_work(struct work_struct *work)
2222{
2223 struct ieee80211_sub_if_data *sdata =
2224 container_of(work, struct ieee80211_sub_if_data,
2225 u.mgd.monitor_work);
2226
2227 ieee80211_mgd_probe_ap(sdata, false);
2228}
2229
9c6bd790
JB
2230static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2231{
ad935687 2232 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
b291ba11
JB
2233 sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
2234 IEEE80211_STA_CONNECTION_POLL);
ad935687 2235
b291ba11 2236 /* let's probe the connection once */
42935eca 2237 ieee80211_queue_work(&sdata->local->hw,
b291ba11
JB
2238 &sdata->u.mgd.monitor_work);
2239 /* and do all the other regular work too */
64592c8f 2240 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
ad935687 2241 }
9c6bd790 2242}
f0706e82 2243
5bb644a0
JB
2244#ifdef CONFIG_PM
2245void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
2246{
2247 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2248
2249 /*
2250 * we need to use atomic bitops for the running bits
2251 * only because both timers might fire at the same
2252 * time -- the code here is properly synchronised.
2253 */
2254
d1f5b7a3
JB
2255 cancel_work_sync(&ifmgd->request_smps_work);
2256
1e4dcd01 2257 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
5bb644a0
JB
2258 if (del_timer_sync(&ifmgd->timer))
2259 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2260
2261 cancel_work_sync(&ifmgd->chswitch_work);
2262 if (del_timer_sync(&ifmgd->chswitch_timer))
2263 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
b291ba11
JB
2264
2265 cancel_work_sync(&ifmgd->monitor_work);
2266 /* these will just be re-established on connection */
2267 del_timer_sync(&ifmgd->conn_mon_timer);
2268 del_timer_sync(&ifmgd->bcn_mon_timer);
5bb644a0
JB
2269}
2270
2271void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
2272{
2273 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2274
676b58c2
RM
2275 if (!ifmgd->associated)
2276 return;
2277
95acac61
JB
2278 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
2279 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
2280 mutex_lock(&ifmgd->mtx);
2281 if (ifmgd->associated) {
2282#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2283 wiphy_debug(sdata->local->hw.wiphy,
2284 "%s: driver requested disconnect after resume.\n",
2285 sdata->name);
2286#endif
2287 ieee80211_sta_connection_lost(sdata,
2288 ifmgd->associated->bssid,
2289 WLAN_REASON_UNSPECIFIED);
2290 mutex_unlock(&ifmgd->mtx);
2291 return;
2292 }
2293 mutex_unlock(&ifmgd->mtx);
2294 }
2295
5bb644a0
JB
2296 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
2297 add_timer(&ifmgd->timer);
2298 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
2299 add_timer(&ifmgd->chswitch_timer);
c8a7972c 2300 ieee80211_sta_reset_beacon_monitor(sdata);
46090979 2301 ieee80211_restart_sta_timer(sdata);
a6af1d84 2302 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.monitor_work);
5bb644a0
JB
2303}
2304#endif
2305
9c6bd790
JB
2306/* interface setup */
2307void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
f0706e82 2308{
46900298 2309 struct ieee80211_if_managed *ifmgd;
988c0f72 2310
46900298 2311 ifmgd = &sdata->u.mgd;
b291ba11 2312 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
46900298 2313 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
1e4dcd01
JO
2314 INIT_WORK(&ifmgd->beacon_connection_loss_work,
2315 ieee80211_beacon_connection_loss_work);
d1f5b7a3 2316 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
46900298 2317 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
c481ec97 2318 (unsigned long) sdata);
b291ba11
JB
2319 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
2320 (unsigned long) sdata);
2321 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
2322 (unsigned long) sdata);
46900298 2323 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
9c6bd790 2324 (unsigned long) sdata);
9c6bd790 2325
ab1faead 2326 ifmgd->flags = 0;
bbbdff9e 2327
77fdaa12 2328 mutex_init(&ifmgd->mtx);
0f78231b
JB
2329
2330 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
2331 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
2332 else
2333 ifmgd->req_smps = IEEE80211_SMPS_OFF;
f0706e82
JB
2334}
2335
77fdaa12
JB
2336/* scan finished notification */
2337void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
60f8b39c 2338{
77fdaa12 2339 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
60f8b39c 2340
77fdaa12
JB
2341 /* Restart STA timers */
2342 rcu_read_lock();
2343 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2344 ieee80211_restart_sta_timer(sdata);
2345 rcu_read_unlock();
2346}
60f8b39c 2347
77fdaa12
JB
2348int ieee80211_max_network_latency(struct notifier_block *nb,
2349 unsigned long data, void *dummy)
2350{
2351 s32 latency_usec = (s32) data;
2352 struct ieee80211_local *local =
2353 container_of(nb, struct ieee80211_local,
2354 network_latency_notifier);
1fa6f4af 2355
77fdaa12
JB
2356 mutex_lock(&local->iflist_mtx);
2357 ieee80211_recalc_ps(local, latency_usec);
2358 mutex_unlock(&local->iflist_mtx);
dfe67012 2359
77fdaa12 2360 return 0;
9c6bd790
JB
2361}
2362
77fdaa12 2363/* config hooks */
af6b6374
JB
2364static enum work_done_result
2365ieee80211_probe_auth_done(struct ieee80211_work *wk,
2366 struct sk_buff *skb)
2367{
b2abb6e2
JB
2368 struct ieee80211_local *local = wk->sdata->local;
2369
af6b6374
JB
2370 if (!skb) {
2371 cfg80211_send_auth_timeout(wk->sdata->dev, wk->filter_ta);
b2abb6e2 2372 goto destroy;
af6b6374
JB
2373 }
2374
2375 if (wk->type == IEEE80211_WORK_AUTH) {
2376 cfg80211_send_rx_auth(wk->sdata->dev, skb->data, skb->len);
b2abb6e2 2377 goto destroy;
af6b6374
JB
2378 }
2379
2380 mutex_lock(&wk->sdata->u.mgd.mtx);
2381 ieee80211_rx_mgmt_probe_resp(wk->sdata, skb);
2382 mutex_unlock(&wk->sdata->u.mgd.mtx);
2383
2384 wk->type = IEEE80211_WORK_AUTH;
2385 wk->probe_auth.tries = 0;
2386 return WORK_DONE_REQUEUE;
b2abb6e2
JB
2387 destroy:
2388 if (wk->probe_auth.synced)
2389 drv_finish_tx_sync(local, wk->sdata, wk->filter_ta,
2390 IEEE80211_TX_SYNC_AUTH);
2391
2392 return WORK_DONE_DESTROY;
af6b6374
JB
2393}
2394
77fdaa12
JB
2395int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
2396 struct cfg80211_auth_request *req)
9c6bd790 2397{
77fdaa12 2398 const u8 *ssid;
f679f65d 2399 struct ieee80211_work *wk;
77fdaa12 2400 u16 auth_alg;
9c6bd790 2401
d5cdfacb
JM
2402 if (req->local_state_change)
2403 return 0; /* no need to update mac80211 state */
2404
77fdaa12
JB
2405 switch (req->auth_type) {
2406 case NL80211_AUTHTYPE_OPEN_SYSTEM:
2407 auth_alg = WLAN_AUTH_OPEN;
2408 break;
2409 case NL80211_AUTHTYPE_SHARED_KEY:
9dca9c49
JB
2410 if (IS_ERR(sdata->local->wep_tx_tfm))
2411 return -EOPNOTSUPP;
77fdaa12
JB
2412 auth_alg = WLAN_AUTH_SHARED_KEY;
2413 break;
2414 case NL80211_AUTHTYPE_FT:
2415 auth_alg = WLAN_AUTH_FT;
2416 break;
2417 case NL80211_AUTHTYPE_NETWORK_EAP:
2418 auth_alg = WLAN_AUTH_LEAP;
2419 break;
2420 default:
2421 return -EOPNOTSUPP;
60f8b39c 2422 }
77fdaa12
JB
2423
2424 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
2425 if (!wk)
9c6bd790 2426 return -ENOMEM;
77fdaa12 2427
5e124bd5 2428 memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
77fdaa12
JB
2429
2430 if (req->ie && req->ie_len) {
2431 memcpy(wk->ie, req->ie, req->ie_len);
2432 wk->ie_len = req->ie_len;
9c6bd790 2433 }
77fdaa12 2434
fffd0934 2435 if (req->key && req->key_len) {
af6b6374
JB
2436 wk->probe_auth.key_len = req->key_len;
2437 wk->probe_auth.key_idx = req->key_idx;
2438 memcpy(wk->probe_auth.key, req->key, req->key_len);
fffd0934
JB
2439 }
2440
77fdaa12 2441 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
af6b6374
JB
2442 memcpy(wk->probe_auth.ssid, ssid + 2, ssid[1]);
2443 wk->probe_auth.ssid_len = ssid[1];
f679f65d 2444
af6b6374
JB
2445 wk->probe_auth.algorithm = auth_alg;
2446 wk->probe_auth.privacy = req->bss->capability & WLAN_CAPABILITY_PRIVACY;
77fdaa12 2447
070bb547
JB
2448 /* if we already have a probe, don't probe again */
2449 if (req->bss->proberesp_ies)
2450 wk->type = IEEE80211_WORK_AUTH;
2451 else
2452 wk->type = IEEE80211_WORK_DIRECT_PROBE;
f679f65d 2453 wk->chan = req->bss->channel;
4d51e149 2454 wk->chan_type = NL80211_CHAN_NO_HT;
af6b6374
JB
2455 wk->sdata = sdata;
2456 wk->done = ieee80211_probe_auth_done;
77fdaa12 2457
af6b6374 2458 ieee80211_add_work(wk);
9c6bd790 2459 return 0;
60f8b39c
JB
2460}
2461
2a33bee2
GE
2462/* create and insert a dummy station entry */
2463static int ieee80211_pre_assoc(struct ieee80211_sub_if_data *sdata,
2464 u8 *bssid) {
2465 struct sta_info *sta;
2466 int err;
2467
2468 sta = sta_info_alloc(sdata, bssid, GFP_KERNEL);
d15b8459 2469 if (!sta)
2a33bee2 2470 return -ENOMEM;
2a33bee2
GE
2471
2472 sta->dummy = true;
2473
2474 err = sta_info_insert(sta);
2475 sta = NULL;
2476 if (err) {
2477 printk(KERN_DEBUG "%s: failed to insert Dummy STA entry for"
2478 " the AP (error %d)\n", sdata->name, err);
2479 return err;
2480 }
2481
2482 return 0;
2483}
2484
af6b6374
JB
2485static enum work_done_result ieee80211_assoc_done(struct ieee80211_work *wk,
2486 struct sk_buff *skb)
2487{
b2abb6e2 2488 struct ieee80211_local *local = wk->sdata->local;
af6b6374 2489 struct ieee80211_mgmt *mgmt;
e5b900d2
JB
2490 struct ieee80211_rx_status *rx_status;
2491 struct ieee802_11_elems elems;
2a33bee2 2492 struct cfg80211_bss *cbss = wk->assoc.bss;
af6b6374
JB
2493 u16 status;
2494
2495 if (!skb) {
2a33bee2 2496 sta_info_destroy_addr(wk->sdata, cbss->bssid);
af6b6374 2497 cfg80211_send_assoc_timeout(wk->sdata->dev, wk->filter_ta);
b2abb6e2 2498 goto destroy;
af6b6374
JB
2499 }
2500
e5b900d2
JB
2501 if (wk->type == IEEE80211_WORK_ASSOC_BEACON_WAIT) {
2502 mutex_lock(&wk->sdata->u.mgd.mtx);
2503 rx_status = (void *) skb->cb;
2504 ieee802_11_parse_elems(skb->data + 24 + 12, skb->len - 24 - 12, &elems);
2505 ieee80211_rx_bss_info(wk->sdata, (void *)skb->data, skb->len, rx_status,
2506 &elems, true);
2507 mutex_unlock(&wk->sdata->u.mgd.mtx);
2508
2509 wk->type = IEEE80211_WORK_ASSOC;
2510 /* not really done yet */
2511 return WORK_DONE_REQUEUE;
2512 }
2513
af6b6374
JB
2514 mgmt = (void *)skb->data;
2515 status = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2516
2517 if (status == WLAN_STATUS_SUCCESS) {
b2abb6e2
JB
2518 if (wk->assoc.synced)
2519 drv_finish_tx_sync(local, wk->sdata, wk->filter_ta,
2520 IEEE80211_TX_SYNC_ASSOC);
2521
af6b6374
JB
2522 mutex_lock(&wk->sdata->u.mgd.mtx);
2523 if (!ieee80211_assoc_success(wk, mgmt, skb->len)) {
2524 mutex_unlock(&wk->sdata->u.mgd.mtx);
2525 /* oops -- internal error -- send timeout for now */
2a33bee2 2526 sta_info_destroy_addr(wk->sdata, cbss->bssid);
af6b6374
JB
2527 cfg80211_send_assoc_timeout(wk->sdata->dev,
2528 wk->filter_ta);
2529 return WORK_DONE_DESTROY;
2530 }
68542962
JO
2531
2532 mutex_unlock(&wk->sdata->u.mgd.mtx);
2a33bee2
GE
2533 } else {
2534 /* assoc failed - destroy the dummy station entry */
2535 sta_info_destroy_addr(wk->sdata, cbss->bssid);
af6b6374
JB
2536 }
2537
2538 cfg80211_send_rx_assoc(wk->sdata->dev, skb->data, skb->len);
b2abb6e2
JB
2539 destroy:
2540 if (wk->assoc.synced)
2541 drv_finish_tx_sync(local, wk->sdata, wk->filter_ta,
2542 IEEE80211_TX_SYNC_ASSOC);
2543
af6b6374
JB
2544 return WORK_DONE_DESTROY;
2545}
2546
77fdaa12
JB
2547int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
2548 struct cfg80211_assoc_request *req)
f0706e82 2549{
77fdaa12 2550 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
0c1ad2ca 2551 struct ieee80211_bss *bss = (void *)req->bss->priv;
f679f65d 2552 struct ieee80211_work *wk;
63f170e0 2553 const u8 *ssid;
2a33bee2 2554 int i, err;
f0706e82 2555
77fdaa12 2556 mutex_lock(&ifmgd->mtx);
af6b6374 2557 if (ifmgd->associated) {
9c87ba67
JM
2558 if (!req->prev_bssid ||
2559 memcmp(req->prev_bssid, ifmgd->associated->bssid,
2560 ETH_ALEN)) {
2561 /*
2562 * We are already associated and the request was not a
2563 * reassociation request from the current BSS, so
2564 * reject it.
2565 */
2566 mutex_unlock(&ifmgd->mtx);
2567 return -EALREADY;
2568 }
2569
2570 /* Trying to reassociate - clear previous association state */
53f73c09 2571 ieee80211_set_disassoc(sdata, true, false);
af6b6374
JB
2572 }
2573 mutex_unlock(&ifmgd->mtx);
77fdaa12 2574
63f170e0 2575 wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
af6b6374
JB
2576 if (!wk)
2577 return -ENOMEM;
77fdaa12 2578
2a33bee2
GE
2579 /*
2580 * create a dummy station info entry in order
2581 * to start accepting incoming EAPOL packets from the station
2582 */
2583 err = ieee80211_pre_assoc(sdata, req->bss->bssid);
2584 if (err) {
2585 kfree(wk);
2586 return err;
2587 }
2588
77fdaa12 2589 ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
375177bf 2590 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
77fdaa12 2591
d8ec4433
JO
2592 ifmgd->beacon_crc_valid = false;
2593
77fdaa12
JB
2594 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
2595 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
2596 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
2597 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
2598 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
2599
77fdaa12
JB
2600
2601 if (req->ie && req->ie_len) {
2602 memcpy(wk->ie, req->ie, req->ie_len);
2603 wk->ie_len = req->ie_len;
2604 } else
2605 wk->ie_len = 0;
2606
0c1ad2ca 2607 wk->assoc.bss = req->bss;
f679f65d 2608
af6b6374 2609 memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
f679f65d 2610
af6b6374
JB
2611 /* new association always uses requested smps mode */
2612 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
2613 if (ifmgd->powersave)
2614 ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
2615 else
2616 ifmgd->ap_smps = IEEE80211_SMPS_OFF;
2617 } else
2618 ifmgd->ap_smps = ifmgd->req_smps;
2619
2620 wk->assoc.smps = ifmgd->ap_smps;
77c8144a
JB
2621 /*
2622 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
2623 * We still associate in non-HT mode (11a/b/g) if any one of these
2624 * ciphers is configured as pairwise.
2625 * We can set this to true for non-11n hardware, that'll be checked
2626 * separately along with the peer capabilities.
2627 */
af6b6374 2628 wk->assoc.use_11n = !(ifmgd->flags & IEEE80211_STA_DISABLE_11N);
f679f65d 2629 wk->assoc.capability = req->bss->capability;
0c1ad2ca
JB
2630 wk->assoc.wmm_used = bss->wmm_used;
2631 wk->assoc.supp_rates = bss->supp_rates;
2632 wk->assoc.supp_rates_len = bss->supp_rates_len;
f679f65d
JB
2633 wk->assoc.ht_information_ie =
2634 ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
63f170e0 2635
ab13315a
KV
2636 if (bss->wmm_used && bss->uapsd_supported &&
2637 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
2638 wk->assoc.uapsd_used = true;
2639 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
2640 } else {
2641 wk->assoc.uapsd_used = false;
2642 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
2643 }
2644
63f170e0 2645 ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
f679f65d
JB
2646 memcpy(wk->assoc.ssid, ssid + 2, ssid[1]);
2647 wk->assoc.ssid_len = ssid[1];
63f170e0 2648
77fdaa12 2649 if (req->prev_bssid)
f679f65d 2650 memcpy(wk->assoc.prev_bssid, req->prev_bssid, ETH_ALEN);
77fdaa12 2651
f679f65d 2652 wk->chan = req->bss->channel;
4d51e149 2653 wk->chan_type = NL80211_CHAN_NO_HT;
af6b6374
JB
2654 wk->sdata = sdata;
2655 wk->done = ieee80211_assoc_done;
e5b900d2
JB
2656 if (!bss->dtim_period &&
2657 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
2658 wk->type = IEEE80211_WORK_ASSOC_BEACON_WAIT;
2659 else
2660 wk->type = IEEE80211_WORK_ASSOC;
77fdaa12
JB
2661
2662 if (req->use_mfp) {
2663 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
2664 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
2665 } else {
2666 ifmgd->mfp = IEEE80211_MFP_DISABLED;
2667 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
2668 }
2669
2670 if (req->crypto.control_port)
2671 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
2672 else
2673 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
2674
a621fa4d
JB
2675 sdata->control_port_protocol = req->crypto.control_port_ethertype;
2676 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
2677
af6b6374
JB
2678 ieee80211_add_work(wk);
2679 return 0;
f0706e82
JB
2680}
2681
77fdaa12 2682int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
667503dd
JB
2683 struct cfg80211_deauth_request *req,
2684 void *cookie)
f0706e82 2685{
af6b6374 2686 struct ieee80211_local *local = sdata->local;
46900298 2687 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
f679f65d 2688 struct ieee80211_work *wk;
05e48e8e
JM
2689 u8 bssid[ETH_ALEN];
2690 bool assoc_bss = false;
f0706e82 2691
77fdaa12
JB
2692 mutex_lock(&ifmgd->mtx);
2693
05e48e8e 2694 memcpy(bssid, req->bss->bssid, ETH_ALEN);
0c1ad2ca 2695 if (ifmgd->associated == req->bss) {
53f73c09 2696 ieee80211_set_disassoc(sdata, false, true);
af6b6374 2697 mutex_unlock(&ifmgd->mtx);
05e48e8e 2698 assoc_bss = true;
af6b6374
JB
2699 } else {
2700 bool not_auth_yet = false;
f0706e82 2701
a58ce43f 2702 mutex_unlock(&ifmgd->mtx);
a58ce43f 2703
a1699b75 2704 mutex_lock(&local->mtx);
af6b6374 2705 list_for_each_entry(wk, &local->work_list, list) {
29165e4c 2706 if (wk->sdata != sdata)
af6b6374 2707 continue;
29165e4c
JB
2708
2709 if (wk->type != IEEE80211_WORK_DIRECT_PROBE &&
79733a86 2710 wk->type != IEEE80211_WORK_AUTH &&
e5b900d2
JB
2711 wk->type != IEEE80211_WORK_ASSOC &&
2712 wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
29165e4c
JB
2713 continue;
2714
af6b6374
JB
2715 if (memcmp(req->bss->bssid, wk->filter_ta, ETH_ALEN))
2716 continue;
29165e4c
JB
2717
2718 not_auth_yet = wk->type == IEEE80211_WORK_DIRECT_PROBE;
2719 list_del_rcu(&wk->list);
af6b6374
JB
2720 free_work(wk);
2721 break;
2722 }
a1699b75 2723 mutex_unlock(&local->mtx);
af6b6374
JB
2724
2725 /*
2726 * If somebody requests authentication and we haven't
2727 * sent out an auth frame yet there's no need to send
2728 * out a deauth frame either. If the state was PROBE,
2729 * then this is the case. If it's AUTH we have sent a
2730 * frame, and if it's IDLE we have completed the auth
2731 * process already.
2732 */
2733 if (not_auth_yet) {
2734 __cfg80211_auth_canceled(sdata->dev, bssid);
2735 return 0;
2736 }
2737 }
77fdaa12 2738
0ff71613 2739 printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
47846c9b 2740 sdata->name, bssid, req->reason_code);
0ff71613 2741
d5cdfacb
JM
2742 ieee80211_send_deauth_disassoc(sdata, bssid, IEEE80211_STYPE_DEAUTH,
2743 req->reason_code, cookie,
2744 !req->local_state_change);
05e48e8e 2745 if (assoc_bss)
07ba55d7 2746 sta_info_flush(sdata->local, sdata);
f0706e82 2747
7da7cc1d 2748 mutex_lock(&sdata->local->mtx);
bc83b681 2749 ieee80211_recalc_idle(sdata->local);
7da7cc1d 2750 mutex_unlock(&sdata->local->mtx);
bc83b681 2751
f0706e82
JB
2752 return 0;
2753}
84363e6e 2754
77fdaa12 2755int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
667503dd
JB
2756 struct cfg80211_disassoc_request *req,
2757 void *cookie)
9c6bd790 2758{
77fdaa12 2759 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
e69e95db 2760 u8 bssid[ETH_ALEN];
9c6bd790 2761
77fdaa12 2762 mutex_lock(&ifmgd->mtx);
10f644a4 2763
8d8b261a
JB
2764 /*
2765 * cfg80211 should catch this ... but it's racy since
2766 * we can receive a disassoc frame, process it, hand it
2767 * to cfg80211 while that's in a locked section already
2768 * trying to tell us that the user wants to disconnect.
2769 */
0c1ad2ca 2770 if (ifmgd->associated != req->bss) {
77fdaa12
JB
2771 mutex_unlock(&ifmgd->mtx);
2772 return -ENOLINK;
2773 }
2774
0ff71613 2775 printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
47846c9b 2776 sdata->name, req->bss->bssid, req->reason_code);
0ff71613 2777
e69e95db 2778 memcpy(bssid, req->bss->bssid, ETH_ALEN);
53f73c09 2779 ieee80211_set_disassoc(sdata, false, true);
77fdaa12
JB
2780
2781 mutex_unlock(&ifmgd->mtx);
10f644a4 2782
77fdaa12 2783 ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
667503dd 2784 IEEE80211_STYPE_DISASSOC, req->reason_code,
d5cdfacb 2785 cookie, !req->local_state_change);
07ba55d7 2786 sta_info_flush(sdata->local, sdata);
bc83b681 2787
7da7cc1d 2788 mutex_lock(&sdata->local->mtx);
bc83b681 2789 ieee80211_recalc_idle(sdata->local);
7da7cc1d 2790 mutex_unlock(&sdata->local->mtx);
bc83b681 2791
10f644a4
JB
2792 return 0;
2793}
026331c4 2794
a97c13c3
JO
2795void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
2796 enum nl80211_cqm_rssi_threshold_event rssi_event,
2797 gfp_t gfp)
2798{
2799 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2800
b5878a2d
JB
2801 trace_api_cqm_rssi_notify(sdata, rssi_event);
2802
a97c13c3
JO
2803 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
2804}
2805EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
1d34d108
EP
2806
2807unsigned char ieee80211_get_operstate(struct ieee80211_vif *vif)
2808{
2809 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2810 return sdata->dev->operstate;
2811}
2812EXPORT_SYMBOL(ieee80211_get_operstate);
This page took 0.688835 seconds and 5 git commands to generate.