]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/mac80211/mlme.c
mac80211: Add debug print on unexpect authentication state
[mirror_ubuntu-artful-kernel.git] / net / mac80211 / mlme.c
1 /*
2 * BSS client mode implementation
3 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14 #include <linux/delay.h>
15 #include <linux/if_ether.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/etherdevice.h>
19 #include <linux/moduleparam.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/pm_qos.h>
22 #include <linux/crc32.h>
23 #include <linux/slab.h>
24 #include <linux/export.h>
25 #include <net/mac80211.h>
26 #include <asm/unaligned.h>
27
28 #include "ieee80211_i.h"
29 #include "driver-ops.h"
30 #include "rate.h"
31 #include "led.h"
32
33 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
34 #define IEEE80211_AUTH_MAX_TRIES 3
35 #define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
36 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
37 #define IEEE80211_ASSOC_MAX_TRIES 3
38
39 static int max_nullfunc_tries = 2;
40 module_param(max_nullfunc_tries, int, 0644);
41 MODULE_PARM_DESC(max_nullfunc_tries,
42 "Maximum nullfunc tx tries before disconnecting (reason 4).");
43
44 static int max_probe_tries = 5;
45 module_param(max_probe_tries, int, 0644);
46 MODULE_PARM_DESC(max_probe_tries,
47 "Maximum probe tries before disconnecting (reason 4).");
48
49 /*
50 * Beacon loss timeout is calculated as N frames times the
51 * advertised beacon interval. This may need to be somewhat
52 * higher than what hardware might detect to account for
53 * delays in the host processing frames. But since we also
54 * probe on beacon miss before declaring the connection lost
55 * default to what we want.
56 */
57 #define IEEE80211_BEACON_LOSS_COUNT 7
58
59 /*
60 * Time the connection can be idle before we probe
61 * it to see if we can still talk to the AP.
62 */
63 #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
64 /*
65 * Time we wait for a probe response after sending
66 * a probe request because of beacon loss or for
67 * checking the connection still works.
68 */
69 static int probe_wait_ms = 500;
70 module_param(probe_wait_ms, int, 0644);
71 MODULE_PARM_DESC(probe_wait_ms,
72 "Maximum time(ms) to wait for probe response"
73 " before disconnecting (reason 4).");
74
75 /*
76 * Weight given to the latest Beacon frame when calculating average signal
77 * strength for Beacon frames received in the current BSS. This must be
78 * between 1 and 15.
79 */
80 #define IEEE80211_SIGNAL_AVE_WEIGHT 3
81
82 /*
83 * How many Beacon frames need to have been used in average signal strength
84 * before starting to indicate signal change events.
85 */
86 #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
87
88 #define TMR_RUNNING_TIMER 0
89 #define TMR_RUNNING_CHANSW 1
90
91 /*
92 * All cfg80211 functions have to be called outside a locked
93 * section so that they can acquire a lock themselves... This
94 * is much simpler than queuing up things in cfg80211, but we
95 * do need some indirection for that here.
96 */
97 enum rx_mgmt_action {
98 /* no action required */
99 RX_MGMT_NONE,
100
101 /* caller must call cfg80211_send_deauth() */
102 RX_MGMT_CFG80211_DEAUTH,
103
104 /* caller must call cfg80211_send_disassoc() */
105 RX_MGMT_CFG80211_DISASSOC,
106
107 /* caller must call cfg80211_send_rx_auth() */
108 RX_MGMT_CFG80211_RX_AUTH,
109
110 /* caller must call cfg80211_send_rx_assoc() */
111 RX_MGMT_CFG80211_RX_ASSOC,
112
113 /* caller must call cfg80211_send_assoc_timeout() */
114 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
115 };
116
117 /* utils */
118 static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
119 {
120 lockdep_assert_held(&ifmgd->mtx);
121 }
122
123 /*
124 * We can have multiple work items (and connection probing)
125 * scheduling this timer, but we need to take care to only
126 * reschedule it when it should fire _earlier_ than it was
127 * asked for before, or if it's not pending right now. This
128 * function ensures that. Note that it then is required to
129 * run this function for all timeouts after the first one
130 * has happened -- the work that runs from this timer will
131 * do that.
132 */
133 static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
134 {
135 ASSERT_MGD_MTX(ifmgd);
136
137 if (!timer_pending(&ifmgd->timer) ||
138 time_before(timeout, ifmgd->timer.expires))
139 mod_timer(&ifmgd->timer, timeout);
140 }
141
142 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
143 {
144 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
145 return;
146
147 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
148 return;
149
150 mod_timer(&sdata->u.mgd.bcn_mon_timer,
151 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
152 }
153
154 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
155 {
156 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
157
158 if (unlikely(!sdata->u.mgd.associated))
159 return;
160
161 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
162 return;
163
164 mod_timer(&sdata->u.mgd.conn_mon_timer,
165 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
166
167 ifmgd->probe_send_count = 0;
168 }
169
170 static int ecw2cw(int ecw)
171 {
172 return (1 << ecw) - 1;
173 }
174
175 static u32 ieee80211_config_ht_tx(struct ieee80211_sub_if_data *sdata,
176 struct ieee80211_ht_operation *ht_oper,
177 const u8 *bssid, bool reconfig)
178 {
179 struct ieee80211_local *local = sdata->local;
180 struct ieee80211_supported_band *sband;
181 struct ieee80211_chanctx_conf *chanctx_conf;
182 struct ieee80211_channel *chan;
183 struct sta_info *sta;
184 u32 changed = 0;
185 u16 ht_opmode;
186 bool disable_40 = false;
187
188 rcu_read_lock();
189 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
190 if (WARN_ON(!chanctx_conf)) {
191 rcu_read_unlock();
192 return 0;
193 }
194 chan = chanctx_conf->channel;
195 rcu_read_unlock();
196 sband = local->hw.wiphy->bands[chan->band];
197
198 switch (sdata->vif.bss_conf.channel_type) {
199 case NL80211_CHAN_HT40PLUS:
200 if (chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
201 disable_40 = true;
202 break;
203 case NL80211_CHAN_HT40MINUS:
204 if (chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
205 disable_40 = true;
206 break;
207 default:
208 break;
209 }
210
211 /* This can change during the lifetime of the BSS */
212 if (!(ht_oper->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
213 disable_40 = true;
214
215 mutex_lock(&local->sta_mtx);
216 sta = sta_info_get(sdata, bssid);
217
218 WARN_ON_ONCE(!sta);
219
220 if (sta && !sta->supports_40mhz)
221 disable_40 = true;
222
223 if (sta && (!reconfig ||
224 (disable_40 != !(sta->sta.ht_cap.cap &
225 IEEE80211_HT_CAP_SUP_WIDTH_20_40)))) {
226
227 if (disable_40)
228 sta->sta.ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
229 else
230 sta->sta.ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
231
232 rate_control_rate_update(local, sband, sta,
233 IEEE80211_RC_BW_CHANGED);
234 }
235 mutex_unlock(&local->sta_mtx);
236
237 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
238
239 /* if bss configuration changed store the new one */
240 if (!reconfig || (sdata->vif.bss_conf.ht_operation_mode != ht_opmode)) {
241 changed |= BSS_CHANGED_HT;
242 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
243 }
244
245 return changed;
246 }
247
248 /* frame sending functions */
249
250 static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
251 struct ieee80211_supported_band *sband,
252 u32 *rates)
253 {
254 int i, j, count;
255 *rates = 0;
256 count = 0;
257 for (i = 0; i < supp_rates_len; i++) {
258 int rate = (supp_rates[i] & 0x7F) * 5;
259
260 for (j = 0; j < sband->n_bitrates; j++)
261 if (sband->bitrates[j].bitrate == rate) {
262 *rates |= BIT(j);
263 count++;
264 break;
265 }
266 }
267
268 return count;
269 }
270
271 static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
272 struct sk_buff *skb, u8 ap_ht_param,
273 struct ieee80211_supported_band *sband,
274 struct ieee80211_channel *channel,
275 enum ieee80211_smps_mode smps)
276 {
277 u8 *pos;
278 u32 flags = channel->flags;
279 u16 cap;
280 struct ieee80211_sta_ht_cap ht_cap;
281
282 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
283
284 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
285 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
286
287 /* determine capability flags */
288 cap = ht_cap.cap;
289
290 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
291 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
292 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
293 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
294 cap &= ~IEEE80211_HT_CAP_SGI_40;
295 }
296 break;
297 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
298 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
299 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
300 cap &= ~IEEE80211_HT_CAP_SGI_40;
301 }
302 break;
303 }
304
305 /*
306 * If 40 MHz was disabled associate as though we weren't
307 * capable of 40 MHz -- some broken APs will never fall
308 * back to trying to transmit in 20 MHz.
309 */
310 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
311 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
312 cap &= ~IEEE80211_HT_CAP_SGI_40;
313 }
314
315 /* set SM PS mode properly */
316 cap &= ~IEEE80211_HT_CAP_SM_PS;
317 switch (smps) {
318 case IEEE80211_SMPS_AUTOMATIC:
319 case IEEE80211_SMPS_NUM_MODES:
320 WARN_ON(1);
321 case IEEE80211_SMPS_OFF:
322 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
323 IEEE80211_HT_CAP_SM_PS_SHIFT;
324 break;
325 case IEEE80211_SMPS_STATIC:
326 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
327 IEEE80211_HT_CAP_SM_PS_SHIFT;
328 break;
329 case IEEE80211_SMPS_DYNAMIC:
330 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
331 IEEE80211_HT_CAP_SM_PS_SHIFT;
332 break;
333 }
334
335 /* reserve and fill IE */
336 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
337 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
338 }
339
340 static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
341 struct sk_buff *skb,
342 struct ieee80211_supported_band *sband)
343 {
344 u8 *pos;
345 u32 cap;
346 struct ieee80211_sta_vht_cap vht_cap;
347
348 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
349
350 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
351
352 /* determine capability flags */
353 cap = vht_cap.cap;
354
355 /* reserve and fill IE */
356 pos = skb_put(skb, sizeof(struct ieee80211_vht_capabilities) + 2);
357 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
358 }
359
360 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
361 {
362 struct ieee80211_local *local = sdata->local;
363 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
364 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
365 struct sk_buff *skb;
366 struct ieee80211_mgmt *mgmt;
367 u8 *pos, qos_info;
368 size_t offset = 0, noffset;
369 int i, count, rates_len, supp_rates_len;
370 u16 capab;
371 struct ieee80211_supported_band *sband;
372 struct ieee80211_chanctx_conf *chanctx_conf;
373 struct ieee80211_channel *chan;
374 u32 rates = 0;
375
376 lockdep_assert_held(&ifmgd->mtx);
377
378 rcu_read_lock();
379 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
380 if (WARN_ON(!chanctx_conf)) {
381 rcu_read_unlock();
382 return;
383 }
384 chan = chanctx_conf->channel;
385 rcu_read_unlock();
386 sband = local->hw.wiphy->bands[chan->band];
387
388 if (assoc_data->supp_rates_len) {
389 /*
390 * Get all rates supported by the device and the AP as
391 * some APs don't like getting a superset of their rates
392 * in the association request (e.g. D-Link DAP 1353 in
393 * b-only mode)...
394 */
395 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
396 assoc_data->supp_rates_len,
397 sband, &rates);
398 } else {
399 /*
400 * In case AP not provide any supported rates information
401 * before association, we send information element(s) with
402 * all rates that we support.
403 */
404 rates = ~0;
405 rates_len = sband->n_bitrates;
406 }
407
408 skb = alloc_skb(local->hw.extra_tx_headroom +
409 sizeof(*mgmt) + /* bit too much but doesn't matter */
410 2 + assoc_data->ssid_len + /* SSID */
411 4 + rates_len + /* (extended) rates */
412 4 + /* power capability */
413 2 + 2 * sband->n_channels + /* supported channels */
414 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
415 2 + sizeof(struct ieee80211_vht_capabilities) + /* VHT */
416 assoc_data->ie_len + /* extra IEs */
417 9, /* WMM */
418 GFP_KERNEL);
419 if (!skb)
420 return;
421
422 skb_reserve(skb, local->hw.extra_tx_headroom);
423
424 capab = WLAN_CAPABILITY_ESS;
425
426 if (sband->band == IEEE80211_BAND_2GHZ) {
427 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
428 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
429 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
430 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
431 }
432
433 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
434 capab |= WLAN_CAPABILITY_PRIVACY;
435
436 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
437 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
438 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
439
440 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
441 memset(mgmt, 0, 24);
442 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
443 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
444 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
445
446 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
447 skb_put(skb, 10);
448 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
449 IEEE80211_STYPE_REASSOC_REQ);
450 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
451 mgmt->u.reassoc_req.listen_interval =
452 cpu_to_le16(local->hw.conf.listen_interval);
453 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
454 ETH_ALEN);
455 } else {
456 skb_put(skb, 4);
457 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
458 IEEE80211_STYPE_ASSOC_REQ);
459 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
460 mgmt->u.assoc_req.listen_interval =
461 cpu_to_le16(local->hw.conf.listen_interval);
462 }
463
464 /* SSID */
465 pos = skb_put(skb, 2 + assoc_data->ssid_len);
466 *pos++ = WLAN_EID_SSID;
467 *pos++ = assoc_data->ssid_len;
468 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
469
470 /* add all rates which were marked to be used above */
471 supp_rates_len = rates_len;
472 if (supp_rates_len > 8)
473 supp_rates_len = 8;
474
475 pos = skb_put(skb, supp_rates_len + 2);
476 *pos++ = WLAN_EID_SUPP_RATES;
477 *pos++ = supp_rates_len;
478
479 count = 0;
480 for (i = 0; i < sband->n_bitrates; i++) {
481 if (BIT(i) & rates) {
482 int rate = sband->bitrates[i].bitrate;
483 *pos++ = (u8) (rate / 5);
484 if (++count == 8)
485 break;
486 }
487 }
488
489 if (rates_len > count) {
490 pos = skb_put(skb, rates_len - count + 2);
491 *pos++ = WLAN_EID_EXT_SUPP_RATES;
492 *pos++ = rates_len - count;
493
494 for (i++; i < sband->n_bitrates; i++) {
495 if (BIT(i) & rates) {
496 int rate = sband->bitrates[i].bitrate;
497 *pos++ = (u8) (rate / 5);
498 }
499 }
500 }
501
502 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
503 /* 1. power capabilities */
504 pos = skb_put(skb, 4);
505 *pos++ = WLAN_EID_PWR_CAPABILITY;
506 *pos++ = 2;
507 *pos++ = 0; /* min tx power */
508 *pos++ = chan->max_power; /* max tx power */
509
510 /* 2. supported channels */
511 /* TODO: get this in reg domain format */
512 pos = skb_put(skb, 2 * sband->n_channels + 2);
513 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
514 *pos++ = 2 * sband->n_channels;
515 for (i = 0; i < sband->n_channels; i++) {
516 *pos++ = ieee80211_frequency_to_channel(
517 sband->channels[i].center_freq);
518 *pos++ = 1; /* one channel in the subband*/
519 }
520 }
521
522 /* if present, add any custom IEs that go before HT */
523 if (assoc_data->ie_len && assoc_data->ie) {
524 static const u8 before_ht[] = {
525 WLAN_EID_SSID,
526 WLAN_EID_SUPP_RATES,
527 WLAN_EID_EXT_SUPP_RATES,
528 WLAN_EID_PWR_CAPABILITY,
529 WLAN_EID_SUPPORTED_CHANNELS,
530 WLAN_EID_RSN,
531 WLAN_EID_QOS_CAPA,
532 WLAN_EID_RRM_ENABLED_CAPABILITIES,
533 WLAN_EID_MOBILITY_DOMAIN,
534 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
535 };
536 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
537 before_ht, ARRAY_SIZE(before_ht),
538 offset);
539 pos = skb_put(skb, noffset - offset);
540 memcpy(pos, assoc_data->ie + offset, noffset - offset);
541 offset = noffset;
542 }
543
544 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
545 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
546 sband, chan, sdata->smps_mode);
547
548 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
549 ieee80211_add_vht_ie(sdata, skb, sband);
550
551 /* if present, add any custom non-vendor IEs that go after HT */
552 if (assoc_data->ie_len && assoc_data->ie) {
553 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
554 assoc_data->ie_len,
555 offset);
556 pos = skb_put(skb, noffset - offset);
557 memcpy(pos, assoc_data->ie + offset, noffset - offset);
558 offset = noffset;
559 }
560
561 if (assoc_data->wmm) {
562 if (assoc_data->uapsd) {
563 qos_info = ifmgd->uapsd_queues;
564 qos_info |= (ifmgd->uapsd_max_sp_len <<
565 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
566 } else {
567 qos_info = 0;
568 }
569
570 pos = skb_put(skb, 9);
571 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
572 *pos++ = 7; /* len */
573 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
574 *pos++ = 0x50;
575 *pos++ = 0xf2;
576 *pos++ = 2; /* WME */
577 *pos++ = 0; /* WME info */
578 *pos++ = 1; /* WME ver */
579 *pos++ = qos_info;
580 }
581
582 /* add any remaining custom (i.e. vendor specific here) IEs */
583 if (assoc_data->ie_len && assoc_data->ie) {
584 noffset = assoc_data->ie_len;
585 pos = skb_put(skb, noffset - offset);
586 memcpy(pos, assoc_data->ie + offset, noffset - offset);
587 }
588
589 drv_mgd_prepare_tx(local, sdata);
590
591 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
592 ieee80211_tx_skb(sdata, skb);
593 }
594
595 void ieee80211_send_pspoll(struct ieee80211_local *local,
596 struct ieee80211_sub_if_data *sdata)
597 {
598 struct ieee80211_pspoll *pspoll;
599 struct sk_buff *skb;
600
601 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
602 if (!skb)
603 return;
604
605 pspoll = (struct ieee80211_pspoll *) skb->data;
606 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
607
608 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
609 ieee80211_tx_skb(sdata, skb);
610 }
611
612 void ieee80211_send_nullfunc(struct ieee80211_local *local,
613 struct ieee80211_sub_if_data *sdata,
614 int powersave)
615 {
616 struct sk_buff *skb;
617 struct ieee80211_hdr_3addr *nullfunc;
618 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
619
620 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
621 if (!skb)
622 return;
623
624 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
625 if (powersave)
626 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
627
628 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
629 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
630 IEEE80211_STA_CONNECTION_POLL))
631 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
632
633 ieee80211_tx_skb(sdata, skb);
634 }
635
636 static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
637 struct ieee80211_sub_if_data *sdata)
638 {
639 struct sk_buff *skb;
640 struct ieee80211_hdr *nullfunc;
641 __le16 fc;
642
643 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
644 return;
645
646 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
647 if (!skb)
648 return;
649
650 skb_reserve(skb, local->hw.extra_tx_headroom);
651
652 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
653 memset(nullfunc, 0, 30);
654 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
655 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
656 nullfunc->frame_control = fc;
657 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
658 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
659 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
660 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
661
662 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
663 ieee80211_tx_skb(sdata, skb);
664 }
665
666 /* spectrum management related things */
667 static void ieee80211_chswitch_work(struct work_struct *work)
668 {
669 struct ieee80211_sub_if_data *sdata =
670 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
671 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
672
673 if (!ieee80211_sdata_running(sdata))
674 return;
675
676 mutex_lock(&ifmgd->mtx);
677 if (!ifmgd->associated)
678 goto out;
679
680 sdata->local->_oper_channel = sdata->local->csa_channel;
681 if (!sdata->local->ops->channel_switch) {
682 /* call "hw_config" only if doing sw channel switch */
683 ieee80211_hw_config(sdata->local,
684 IEEE80211_CONF_CHANGE_CHANNEL);
685 } else {
686 /* update the device channel directly */
687 sdata->local->hw.conf.channel = sdata->local->_oper_channel;
688 }
689
690 /* XXX: shouldn't really modify cfg80211-owned data! */
691 ifmgd->associated->channel = sdata->local->_oper_channel;
692
693 /* XXX: wait for a beacon first? */
694 ieee80211_wake_queues_by_reason(&sdata->local->hw,
695 IEEE80211_QUEUE_STOP_REASON_CSA);
696 out:
697 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
698 mutex_unlock(&ifmgd->mtx);
699 }
700
701 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
702 {
703 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
704 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
705
706 trace_api_chswitch_done(sdata, success);
707 if (!success) {
708 sdata_info(sdata,
709 "driver channel switch failed, disconnecting\n");
710 ieee80211_queue_work(&sdata->local->hw,
711 &ifmgd->csa_connection_drop_work);
712 } else {
713 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
714 }
715 }
716 EXPORT_SYMBOL(ieee80211_chswitch_done);
717
718 static void ieee80211_chswitch_timer(unsigned long data)
719 {
720 struct ieee80211_sub_if_data *sdata =
721 (struct ieee80211_sub_if_data *) data;
722 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
723
724 if (sdata->local->quiescing) {
725 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
726 return;
727 }
728
729 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
730 }
731
732 void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
733 struct ieee80211_channel_sw_ie *sw_elem,
734 struct ieee80211_bss *bss,
735 u64 timestamp)
736 {
737 struct cfg80211_bss *cbss =
738 container_of((void *)bss, struct cfg80211_bss, priv);
739 struct ieee80211_channel *new_ch;
740 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
741 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
742 cbss->channel->band);
743 struct ieee80211_chanctx *chanctx;
744
745 ASSERT_MGD_MTX(ifmgd);
746
747 if (!ifmgd->associated)
748 return;
749
750 if (sdata->local->scanning)
751 return;
752
753 /* Disregard subsequent beacons if we are already running a timer
754 processing a CSA */
755
756 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
757 return;
758
759 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
760 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
761 sdata_info(sdata,
762 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
763 ifmgd->associated->bssid, new_freq);
764 ieee80211_queue_work(&sdata->local->hw,
765 &ifmgd->csa_connection_drop_work);
766 return;
767 }
768
769 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
770
771 if (sdata->local->use_chanctx) {
772 sdata_info(sdata,
773 "not handling channel switch with channel contexts\n");
774 ieee80211_queue_work(&sdata->local->hw,
775 &ifmgd->csa_connection_drop_work);
776 }
777
778 mutex_lock(&sdata->local->chanctx_mtx);
779 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
780 mutex_unlock(&sdata->local->chanctx_mtx);
781 return;
782 }
783 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
784 struct ieee80211_chanctx, conf);
785 if (chanctx->refcount > 1) {
786 sdata_info(sdata,
787 "channel switch with multiple interfaces on the same channel, disconnecting\n");
788 ieee80211_queue_work(&sdata->local->hw,
789 &ifmgd->csa_connection_drop_work);
790 mutex_unlock(&sdata->local->chanctx_mtx);
791 return;
792 }
793 mutex_unlock(&sdata->local->chanctx_mtx);
794
795 sdata->local->csa_channel = new_ch;
796
797 if (sw_elem->mode)
798 ieee80211_stop_queues_by_reason(&sdata->local->hw,
799 IEEE80211_QUEUE_STOP_REASON_CSA);
800
801 if (sdata->local->ops->channel_switch) {
802 /* use driver's channel switch callback */
803 struct ieee80211_channel_switch ch_switch = {
804 .timestamp = timestamp,
805 .block_tx = sw_elem->mode,
806 .channel = new_ch,
807 .count = sw_elem->count,
808 };
809
810 drv_channel_switch(sdata->local, &ch_switch);
811 return;
812 }
813
814 /* channel switch handled in software */
815 if (sw_elem->count <= 1)
816 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
817 else
818 mod_timer(&ifmgd->chswitch_timer,
819 TU_TO_EXP_TIME(sw_elem->count *
820 cbss->beacon_interval));
821 }
822
823 static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
824 struct ieee80211_channel *channel,
825 const u8 *country_ie, u8 country_ie_len,
826 const u8 *pwr_constr_elem)
827 {
828 struct ieee80211_country_ie_triplet *triplet;
829 int chan = ieee80211_frequency_to_channel(channel->center_freq);
830 int i, chan_pwr, chan_increment, new_ap_level;
831 bool have_chan_pwr = false;
832
833 /* Invalid IE */
834 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
835 return;
836
837 triplet = (void *)(country_ie + 3);
838 country_ie_len -= 3;
839
840 switch (channel->band) {
841 default:
842 WARN_ON_ONCE(1);
843 /* fall through */
844 case IEEE80211_BAND_2GHZ:
845 case IEEE80211_BAND_60GHZ:
846 chan_increment = 1;
847 break;
848 case IEEE80211_BAND_5GHZ:
849 chan_increment = 4;
850 break;
851 }
852
853 /* find channel */
854 while (country_ie_len >= 3) {
855 u8 first_channel = triplet->chans.first_channel;
856
857 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
858 goto next;
859
860 for (i = 0; i < triplet->chans.num_channels; i++) {
861 if (first_channel + i * chan_increment == chan) {
862 have_chan_pwr = true;
863 chan_pwr = triplet->chans.max_power;
864 break;
865 }
866 }
867 if (have_chan_pwr)
868 break;
869
870 next:
871 triplet++;
872 country_ie_len -= 3;
873 }
874
875 if (!have_chan_pwr)
876 return;
877
878 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
879
880 if (sdata->local->ap_power_level == new_ap_level)
881 return;
882
883 sdata_info(sdata,
884 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
885 new_ap_level, chan_pwr, *pwr_constr_elem,
886 sdata->u.mgd.bssid);
887 sdata->local->ap_power_level = new_ap_level;
888 ieee80211_hw_config(sdata->local, 0);
889 }
890
891 void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
892 {
893 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
894 struct ieee80211_local *local = sdata->local;
895 struct ieee80211_conf *conf = &local->hw.conf;
896
897 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
898 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
899 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
900
901 local->disable_dynamic_ps = false;
902 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
903 }
904 EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
905
906 void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
907 {
908 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
909 struct ieee80211_local *local = sdata->local;
910 struct ieee80211_conf *conf = &local->hw.conf;
911
912 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
913 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
914 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
915
916 local->disable_dynamic_ps = true;
917 conf->dynamic_ps_timeout = 0;
918 del_timer_sync(&local->dynamic_ps_timer);
919 ieee80211_queue_work(&local->hw,
920 &local->dynamic_ps_enable_work);
921 }
922 EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
923
924 /* powersave */
925 static void ieee80211_enable_ps(struct ieee80211_local *local,
926 struct ieee80211_sub_if_data *sdata)
927 {
928 struct ieee80211_conf *conf = &local->hw.conf;
929
930 /*
931 * If we are scanning right now then the parameters will
932 * take effect when scan finishes.
933 */
934 if (local->scanning)
935 return;
936
937 if (conf->dynamic_ps_timeout > 0 &&
938 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
939 mod_timer(&local->dynamic_ps_timer, jiffies +
940 msecs_to_jiffies(conf->dynamic_ps_timeout));
941 } else {
942 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
943 ieee80211_send_nullfunc(local, sdata, 1);
944
945 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
946 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
947 return;
948
949 conf->flags |= IEEE80211_CONF_PS;
950 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
951 }
952 }
953
954 static void ieee80211_change_ps(struct ieee80211_local *local)
955 {
956 struct ieee80211_conf *conf = &local->hw.conf;
957
958 if (local->ps_sdata) {
959 ieee80211_enable_ps(local, local->ps_sdata);
960 } else if (conf->flags & IEEE80211_CONF_PS) {
961 conf->flags &= ~IEEE80211_CONF_PS;
962 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
963 del_timer_sync(&local->dynamic_ps_timer);
964 cancel_work_sync(&local->dynamic_ps_enable_work);
965 }
966 }
967
968 static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
969 {
970 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
971 struct sta_info *sta = NULL;
972 bool authorized = false;
973
974 if (!mgd->powersave)
975 return false;
976
977 if (mgd->broken_ap)
978 return false;
979
980 if (!mgd->associated)
981 return false;
982
983 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
984 IEEE80211_STA_CONNECTION_POLL))
985 return false;
986
987 rcu_read_lock();
988 sta = sta_info_get(sdata, mgd->bssid);
989 if (sta)
990 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
991 rcu_read_unlock();
992
993 return authorized;
994 }
995
996 /* need to hold RTNL or interface lock */
997 void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
998 {
999 struct ieee80211_sub_if_data *sdata, *found = NULL;
1000 int count = 0;
1001 int timeout;
1002
1003 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1004 local->ps_sdata = NULL;
1005 return;
1006 }
1007
1008 list_for_each_entry(sdata, &local->interfaces, list) {
1009 if (!ieee80211_sdata_running(sdata))
1010 continue;
1011 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1012 /* If an AP vif is found, then disable PS
1013 * by setting the count to zero thereby setting
1014 * ps_sdata to NULL.
1015 */
1016 count = 0;
1017 break;
1018 }
1019 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1020 continue;
1021 found = sdata;
1022 count++;
1023 }
1024
1025 if (count == 1 && ieee80211_powersave_allowed(found)) {
1026 struct ieee80211_conf *conf = &local->hw.conf;
1027 s32 beaconint_us;
1028
1029 if (latency < 0)
1030 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
1031
1032 beaconint_us = ieee80211_tu_to_usec(
1033 found->vif.bss_conf.beacon_int);
1034
1035 timeout = local->dynamic_ps_forced_timeout;
1036 if (timeout < 0) {
1037 /*
1038 * Go to full PSM if the user configures a very low
1039 * latency requirement.
1040 * The 2000 second value is there for compatibility
1041 * until the PM_QOS_NETWORK_LATENCY is configured
1042 * with real values.
1043 */
1044 if (latency > (1900 * USEC_PER_MSEC) &&
1045 latency != (2000 * USEC_PER_SEC))
1046 timeout = 0;
1047 else
1048 timeout = 100;
1049 }
1050 local->dynamic_ps_user_timeout = timeout;
1051 if (!local->disable_dynamic_ps)
1052 conf->dynamic_ps_timeout =
1053 local->dynamic_ps_user_timeout;
1054
1055 if (beaconint_us > latency) {
1056 local->ps_sdata = NULL;
1057 } else {
1058 struct ieee80211_bss *bss;
1059 int maxslp = 1;
1060 u8 dtimper;
1061
1062 bss = (void *)found->u.mgd.associated->priv;
1063 dtimper = bss->dtim_period;
1064
1065 /* If the TIM IE is invalid, pretend the value is 1 */
1066 if (!dtimper)
1067 dtimper = 1;
1068 else if (dtimper > 1)
1069 maxslp = min_t(int, dtimper,
1070 latency / beaconint_us);
1071
1072 local->hw.conf.max_sleep_period = maxslp;
1073 local->hw.conf.ps_dtim_period = dtimper;
1074 local->ps_sdata = found;
1075 }
1076 } else {
1077 local->ps_sdata = NULL;
1078 }
1079
1080 ieee80211_change_ps(local);
1081 }
1082
1083 void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1084 {
1085 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1086
1087 if (sdata->vif.bss_conf.ps != ps_allowed) {
1088 sdata->vif.bss_conf.ps = ps_allowed;
1089 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1090 }
1091 }
1092
1093 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1094 {
1095 struct ieee80211_local *local =
1096 container_of(work, struct ieee80211_local,
1097 dynamic_ps_disable_work);
1098
1099 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1100 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1101 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1102 }
1103
1104 ieee80211_wake_queues_by_reason(&local->hw,
1105 IEEE80211_QUEUE_STOP_REASON_PS);
1106 }
1107
1108 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1109 {
1110 struct ieee80211_local *local =
1111 container_of(work, struct ieee80211_local,
1112 dynamic_ps_enable_work);
1113 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
1114 struct ieee80211_if_managed *ifmgd;
1115 unsigned long flags;
1116 int q;
1117
1118 /* can only happen when PS was just disabled anyway */
1119 if (!sdata)
1120 return;
1121
1122 ifmgd = &sdata->u.mgd;
1123
1124 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1125 return;
1126
1127 if (!local->disable_dynamic_ps &&
1128 local->hw.conf.dynamic_ps_timeout > 0) {
1129 /* don't enter PS if TX frames are pending */
1130 if (drv_tx_frames_pending(local)) {
1131 mod_timer(&local->dynamic_ps_timer, jiffies +
1132 msecs_to_jiffies(
1133 local->hw.conf.dynamic_ps_timeout));
1134 return;
1135 }
1136
1137 /*
1138 * transmission can be stopped by others which leads to
1139 * dynamic_ps_timer expiry. Postpone the ps timer if it
1140 * is not the actual idle state.
1141 */
1142 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1143 for (q = 0; q < local->hw.queues; q++) {
1144 if (local->queue_stop_reasons[q]) {
1145 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1146 flags);
1147 mod_timer(&local->dynamic_ps_timer, jiffies +
1148 msecs_to_jiffies(
1149 local->hw.conf.dynamic_ps_timeout));
1150 return;
1151 }
1152 }
1153 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1154 }
1155
1156 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1157 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1158 netif_tx_stop_all_queues(sdata->dev);
1159
1160 if (drv_tx_frames_pending(local))
1161 mod_timer(&local->dynamic_ps_timer, jiffies +
1162 msecs_to_jiffies(
1163 local->hw.conf.dynamic_ps_timeout));
1164 else {
1165 ieee80211_send_nullfunc(local, sdata, 1);
1166 /* Flush to get the tx status of nullfunc frame */
1167 drv_flush(local, false);
1168 }
1169 }
1170
1171 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1172 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
1173 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1174 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1175 local->hw.conf.flags |= IEEE80211_CONF_PS;
1176 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1177 }
1178
1179 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1180 netif_tx_wake_all_queues(sdata->dev);
1181 }
1182
1183 void ieee80211_dynamic_ps_timer(unsigned long data)
1184 {
1185 struct ieee80211_local *local = (void *) data;
1186
1187 if (local->quiescing || local->suspended)
1188 return;
1189
1190 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1191 }
1192
1193 /* MLME */
1194 static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
1195 struct ieee80211_sub_if_data *sdata,
1196 u8 *wmm_param, size_t wmm_param_len)
1197 {
1198 struct ieee80211_tx_queue_params params;
1199 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1200 size_t left;
1201 int count;
1202 u8 *pos, uapsd_queues = 0;
1203
1204 if (!local->ops->conf_tx)
1205 return false;
1206
1207 if (local->hw.queues < IEEE80211_NUM_ACS)
1208 return false;
1209
1210 if (!wmm_param)
1211 return false;
1212
1213 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
1214 return false;
1215
1216 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
1217 uapsd_queues = ifmgd->uapsd_queues;
1218
1219 count = wmm_param[6] & 0x0f;
1220 if (count == ifmgd->wmm_last_param_set)
1221 return false;
1222 ifmgd->wmm_last_param_set = count;
1223
1224 pos = wmm_param + 8;
1225 left = wmm_param_len - 8;
1226
1227 memset(&params, 0, sizeof(params));
1228
1229 sdata->wmm_acm = 0;
1230 for (; left >= 4; left -= 4, pos += 4) {
1231 int aci = (pos[0] >> 5) & 0x03;
1232 int acm = (pos[0] >> 4) & 0x01;
1233 bool uapsd = false;
1234 int queue;
1235
1236 switch (aci) {
1237 case 1: /* AC_BK */
1238 queue = 3;
1239 if (acm)
1240 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
1241 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1242 uapsd = true;
1243 break;
1244 case 2: /* AC_VI */
1245 queue = 1;
1246 if (acm)
1247 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
1248 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1249 uapsd = true;
1250 break;
1251 case 3: /* AC_VO */
1252 queue = 0;
1253 if (acm)
1254 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
1255 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1256 uapsd = true;
1257 break;
1258 case 0: /* AC_BE */
1259 default:
1260 queue = 2;
1261 if (acm)
1262 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
1263 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1264 uapsd = true;
1265 break;
1266 }
1267
1268 params.aifs = pos[0] & 0x0f;
1269 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1270 params.cw_min = ecw2cw(pos[1] & 0x0f);
1271 params.txop = get_unaligned_le16(pos + 2);
1272 params.uapsd = uapsd;
1273
1274 mlme_dbg(sdata,
1275 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1276 queue, aci, acm,
1277 params.aifs, params.cw_min, params.cw_max,
1278 params.txop, params.uapsd);
1279 sdata->tx_conf[queue] = params;
1280 if (drv_conf_tx(local, sdata, queue, &params))
1281 sdata_err(sdata,
1282 "failed to set TX queue parameters for queue %d\n",
1283 queue);
1284 }
1285
1286 /* enable WMM or activate new settings */
1287 sdata->vif.bss_conf.qos = true;
1288 return true;
1289 }
1290
1291 static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1292 {
1293 lockdep_assert_held(&sdata->local->mtx);
1294
1295 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1296 IEEE80211_STA_BEACON_POLL);
1297 ieee80211_run_deferred_scan(sdata->local);
1298 }
1299
1300 static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1301 {
1302 mutex_lock(&sdata->local->mtx);
1303 __ieee80211_stop_poll(sdata);
1304 mutex_unlock(&sdata->local->mtx);
1305 }
1306
1307 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1308 u16 capab, bool erp_valid, u8 erp)
1309 {
1310 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1311 u32 changed = 0;
1312 bool use_protection;
1313 bool use_short_preamble;
1314 bool use_short_slot;
1315
1316 if (erp_valid) {
1317 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1318 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1319 } else {
1320 use_protection = false;
1321 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1322 }
1323
1324 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
1325 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
1326 use_short_slot = true;
1327
1328 if (use_protection != bss_conf->use_cts_prot) {
1329 bss_conf->use_cts_prot = use_protection;
1330 changed |= BSS_CHANGED_ERP_CTS_PROT;
1331 }
1332
1333 if (use_short_preamble != bss_conf->use_short_preamble) {
1334 bss_conf->use_short_preamble = use_short_preamble;
1335 changed |= BSS_CHANGED_ERP_PREAMBLE;
1336 }
1337
1338 if (use_short_slot != bss_conf->use_short_slot) {
1339 bss_conf->use_short_slot = use_short_slot;
1340 changed |= BSS_CHANGED_ERP_SLOT;
1341 }
1342
1343 return changed;
1344 }
1345
1346 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
1347 struct cfg80211_bss *cbss,
1348 u32 bss_info_changed)
1349 {
1350 struct ieee80211_bss *bss = (void *)cbss->priv;
1351 struct ieee80211_local *local = sdata->local;
1352 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1353
1354 bss_info_changed |= BSS_CHANGED_ASSOC;
1355 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
1356 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
1357
1358 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1359 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1360
1361 sdata->u.mgd.associated = cbss;
1362 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
1363
1364 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1365
1366 /* just to be sure */
1367 ieee80211_stop_poll(sdata);
1368
1369 ieee80211_led_assoc(local, 1);
1370
1371 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
1372 bss_conf->dtim_period = bss->dtim_period;
1373 else
1374 bss_conf->dtim_period = 0;
1375
1376 bss_conf->assoc = 1;
1377
1378 /* Tell the driver to monitor connection quality (if supported) */
1379 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
1380 bss_conf->cqm_rssi_thold)
1381 bss_info_changed |= BSS_CHANGED_CQM;
1382
1383 /* Enable ARP filtering */
1384 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
1385 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
1386 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1387 }
1388
1389 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
1390
1391 mutex_lock(&local->iflist_mtx);
1392 ieee80211_recalc_ps(local, -1);
1393 mutex_unlock(&local->iflist_mtx);
1394
1395 ieee80211_recalc_smps(sdata);
1396 ieee80211_recalc_ps_vif(sdata);
1397
1398 netif_tx_start_all_queues(sdata->dev);
1399 netif_carrier_on(sdata->dev);
1400 }
1401
1402 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
1403 u16 stype, u16 reason, bool tx,
1404 u8 *frame_buf)
1405 {
1406 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1407 struct ieee80211_local *local = sdata->local;
1408 struct sta_info *sta;
1409 u32 changed = 0;
1410
1411 ASSERT_MGD_MTX(ifmgd);
1412
1413 if (WARN_ON_ONCE(tx && !frame_buf))
1414 return;
1415
1416 if (WARN_ON(!ifmgd->associated))
1417 return;
1418
1419 ieee80211_stop_poll(sdata);
1420
1421 ifmgd->associated = NULL;
1422
1423 /*
1424 * we need to commit the associated = NULL change because the
1425 * scan code uses that to determine whether this iface should
1426 * go to/wake up from powersave or not -- and could otherwise
1427 * wake the queues erroneously.
1428 */
1429 smp_mb();
1430
1431 /*
1432 * Thus, we can only afterwards stop the queues -- to account
1433 * for the case where another CPU is finishing a scan at this
1434 * time -- we don't want the scan code to enable queues.
1435 */
1436
1437 netif_tx_stop_all_queues(sdata->dev);
1438 netif_carrier_off(sdata->dev);
1439
1440 mutex_lock(&local->sta_mtx);
1441 sta = sta_info_get(sdata, ifmgd->bssid);
1442 if (sta) {
1443 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1444 ieee80211_sta_tear_down_BA_sessions(sta, false);
1445 }
1446 mutex_unlock(&local->sta_mtx);
1447
1448 /*
1449 * if we want to get out of ps before disassoc (why?) we have
1450 * to do it before sending disassoc, as otherwise the null-packet
1451 * won't be valid.
1452 */
1453 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1454 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1455 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1456 }
1457 local->ps_sdata = NULL;
1458
1459 /* disable per-vif ps */
1460 ieee80211_recalc_ps_vif(sdata);
1461
1462 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1463 if (tx)
1464 drv_flush(local, false);
1465
1466 /* deauthenticate/disassociate now */
1467 if (tx || frame_buf)
1468 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1469 reason, tx, frame_buf);
1470
1471 /* flush out frame */
1472 if (tx)
1473 drv_flush(local, false);
1474
1475 /* clear bssid only after building the needed mgmt frames */
1476 memset(ifmgd->bssid, 0, ETH_ALEN);
1477
1478 /* remove AP and TDLS peers */
1479 sta_info_flush(local, sdata);
1480
1481 /* finally reset all BSS / config parameters */
1482 changed |= ieee80211_reset_erp_info(sdata);
1483
1484 ieee80211_led_assoc(local, 0);
1485 changed |= BSS_CHANGED_ASSOC;
1486 sdata->vif.bss_conf.assoc = false;
1487
1488 /* on the next assoc, re-program HT parameters */
1489 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1490 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
1491
1492 local->ap_power_level = 0;
1493
1494 del_timer_sync(&local->dynamic_ps_timer);
1495 cancel_work_sync(&local->dynamic_ps_enable_work);
1496
1497 /* Disable ARP filtering */
1498 if (sdata->vif.bss_conf.arp_filter_enabled) {
1499 sdata->vif.bss_conf.arp_filter_enabled = false;
1500 changed |= BSS_CHANGED_ARP_FILTER;
1501 }
1502
1503 sdata->vif.bss_conf.qos = false;
1504 changed |= BSS_CHANGED_QOS;
1505
1506 /* The BSSID (not really interesting) and HT changed */
1507 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
1508 ieee80211_bss_info_change_notify(sdata, changed);
1509
1510 ieee80211_vif_release_channel(sdata);
1511
1512 /* disassociated - set to defaults now */
1513 ieee80211_set_wmm_default(sdata, false);
1514
1515 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1516 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1517 del_timer_sync(&sdata->u.mgd.timer);
1518 del_timer_sync(&sdata->u.mgd.chswitch_timer);
1519
1520 sdata->u.mgd.timers_running = 0;
1521 }
1522
1523 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1524 struct ieee80211_hdr *hdr)
1525 {
1526 /*
1527 * We can postpone the mgd.timer whenever receiving unicast frames
1528 * from AP because we know that the connection is working both ways
1529 * at that time. But multicast frames (and hence also beacons) must
1530 * be ignored here, because we need to trigger the timer during
1531 * data idle periods for sending the periodic probe request to the
1532 * AP we're connected to.
1533 */
1534 if (is_multicast_ether_addr(hdr->addr1))
1535 return;
1536
1537 ieee80211_sta_reset_conn_monitor(sdata);
1538 }
1539
1540 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1541 {
1542 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1543 struct ieee80211_local *local = sdata->local;
1544
1545 mutex_lock(&local->mtx);
1546 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1547 IEEE80211_STA_CONNECTION_POLL))) {
1548 mutex_unlock(&local->mtx);
1549 return;
1550 }
1551
1552 __ieee80211_stop_poll(sdata);
1553
1554 mutex_lock(&local->iflist_mtx);
1555 ieee80211_recalc_ps(local, -1);
1556 mutex_unlock(&local->iflist_mtx);
1557
1558 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1559 goto out;
1560
1561 /*
1562 * We've received a probe response, but are not sure whether
1563 * we have or will be receiving any beacons or data, so let's
1564 * schedule the timers again, just in case.
1565 */
1566 ieee80211_sta_reset_beacon_monitor(sdata);
1567
1568 mod_timer(&ifmgd->conn_mon_timer,
1569 round_jiffies_up(jiffies +
1570 IEEE80211_CONNECTION_IDLE_TIME));
1571 out:
1572 mutex_unlock(&local->mtx);
1573 }
1574
1575 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
1576 struct ieee80211_hdr *hdr, bool ack)
1577 {
1578 if (!ieee80211_is_data(hdr->frame_control))
1579 return;
1580
1581 if (ack)
1582 ieee80211_sta_reset_conn_monitor(sdata);
1583
1584 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1585 sdata->u.mgd.probe_send_count > 0) {
1586 if (ack)
1587 sdata->u.mgd.probe_send_count = 0;
1588 else
1589 sdata->u.mgd.nullfunc_failed = true;
1590 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1591 }
1592 }
1593
1594 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1595 {
1596 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1597 const u8 *ssid;
1598 u8 *dst = ifmgd->associated->bssid;
1599 u8 unicast_limit = max(1, max_probe_tries - 3);
1600
1601 /*
1602 * Try sending broadcast probe requests for the last three
1603 * probe requests after the first ones failed since some
1604 * buggy APs only support broadcast probe requests.
1605 */
1606 if (ifmgd->probe_send_count >= unicast_limit)
1607 dst = NULL;
1608
1609 /*
1610 * When the hardware reports an accurate Tx ACK status, it's
1611 * better to send a nullfunc frame instead of a probe request,
1612 * as it will kick us off the AP quickly if we aren't associated
1613 * anymore. The timeout will be reset if the frame is ACKed by
1614 * the AP.
1615 */
1616 ifmgd->probe_send_count++;
1617
1618 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1619 ifmgd->nullfunc_failed = false;
1620 ieee80211_send_nullfunc(sdata->local, sdata, 0);
1621 } else {
1622 int ssid_len;
1623
1624 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1625 if (WARN_ON_ONCE(ssid == NULL))
1626 ssid_len = 0;
1627 else
1628 ssid_len = ssid[1];
1629
1630 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
1631 0, (u32) -1, true, false,
1632 ifmgd->associated->channel, false);
1633 }
1634
1635 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
1636 run_again(ifmgd, ifmgd->probe_timeout);
1637 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1638 drv_flush(sdata->local, false);
1639 }
1640
1641 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1642 bool beacon)
1643 {
1644 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1645 bool already = false;
1646
1647 if (!ieee80211_sdata_running(sdata))
1648 return;
1649
1650 mutex_lock(&ifmgd->mtx);
1651
1652 if (!ifmgd->associated)
1653 goto out;
1654
1655 mutex_lock(&sdata->local->mtx);
1656
1657 if (sdata->local->tmp_channel || sdata->local->scanning) {
1658 mutex_unlock(&sdata->local->mtx);
1659 goto out;
1660 }
1661
1662 if (beacon)
1663 mlme_dbg_ratelimited(sdata,
1664 "detected beacon loss from AP - sending probe request\n");
1665
1666 ieee80211_cqm_rssi_notify(&sdata->vif,
1667 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
1668
1669 /*
1670 * The driver/our work has already reported this event or the
1671 * connection monitoring has kicked in and we have already sent
1672 * a probe request. Or maybe the AP died and the driver keeps
1673 * reporting until we disassociate...
1674 *
1675 * In either case we have to ignore the current call to this
1676 * function (except for setting the correct probe reason bit)
1677 * because otherwise we would reset the timer every time and
1678 * never check whether we received a probe response!
1679 */
1680 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1681 IEEE80211_STA_CONNECTION_POLL))
1682 already = true;
1683
1684 if (beacon)
1685 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1686 else
1687 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1688
1689 mutex_unlock(&sdata->local->mtx);
1690
1691 if (already)
1692 goto out;
1693
1694 mutex_lock(&sdata->local->iflist_mtx);
1695 ieee80211_recalc_ps(sdata->local, -1);
1696 mutex_unlock(&sdata->local->iflist_mtx);
1697
1698 ifmgd->probe_send_count = 0;
1699 ieee80211_mgd_probe_ap_send(sdata);
1700 out:
1701 mutex_unlock(&ifmgd->mtx);
1702 }
1703
1704 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1705 struct ieee80211_vif *vif)
1706 {
1707 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1708 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1709 struct cfg80211_bss *cbss;
1710 struct sk_buff *skb;
1711 const u8 *ssid;
1712 int ssid_len;
1713
1714 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1715 return NULL;
1716
1717 ASSERT_MGD_MTX(ifmgd);
1718
1719 if (ifmgd->associated)
1720 cbss = ifmgd->associated;
1721 else if (ifmgd->auth_data)
1722 cbss = ifmgd->auth_data->bss;
1723 else if (ifmgd->assoc_data)
1724 cbss = ifmgd->assoc_data->bss;
1725 else
1726 return NULL;
1727
1728 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
1729 if (WARN_ON_ONCE(ssid == NULL))
1730 ssid_len = 0;
1731 else
1732 ssid_len = ssid[1];
1733
1734 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
1735 (u32) -1, cbss->channel,
1736 ssid + 2, ssid_len,
1737 NULL, 0, true);
1738
1739 return skb;
1740 }
1741 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1742
1743 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata,
1744 bool transmit_frame)
1745 {
1746 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1747 struct ieee80211_local *local = sdata->local;
1748 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
1749
1750 mutex_lock(&ifmgd->mtx);
1751 if (!ifmgd->associated) {
1752 mutex_unlock(&ifmgd->mtx);
1753 return;
1754 }
1755
1756 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1757 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
1758 transmit_frame, frame_buf);
1759 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
1760 mutex_unlock(&ifmgd->mtx);
1761
1762 /*
1763 * must be outside lock due to cfg80211,
1764 * but that's not a problem.
1765 */
1766 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
1767
1768 mutex_lock(&local->mtx);
1769 ieee80211_recalc_idle(local);
1770 mutex_unlock(&local->mtx);
1771 }
1772
1773 static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
1774 {
1775 struct ieee80211_sub_if_data *sdata =
1776 container_of(work, struct ieee80211_sub_if_data,
1777 u.mgd.beacon_connection_loss_work);
1778 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1779 struct sta_info *sta;
1780
1781 if (ifmgd->associated) {
1782 rcu_read_lock();
1783 sta = sta_info_get(sdata, ifmgd->bssid);
1784 if (sta)
1785 sta->beacon_loss_count++;
1786 rcu_read_unlock();
1787 }
1788
1789 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR) {
1790 sdata_info(sdata, "Connection to AP %pM lost\n",
1791 ifmgd->bssid);
1792 __ieee80211_disconnect(sdata, false);
1793 } else {
1794 ieee80211_mgd_probe_ap(sdata, true);
1795 }
1796 }
1797
1798 static void ieee80211_csa_connection_drop_work(struct work_struct *work)
1799 {
1800 struct ieee80211_sub_if_data *sdata =
1801 container_of(work, struct ieee80211_sub_if_data,
1802 u.mgd.csa_connection_drop_work);
1803
1804 ieee80211_wake_queues_by_reason(&sdata->local->hw,
1805 IEEE80211_QUEUE_STOP_REASON_CSA);
1806 __ieee80211_disconnect(sdata, true);
1807 }
1808
1809 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1810 {
1811 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1812 struct ieee80211_hw *hw = &sdata->local->hw;
1813
1814 trace_api_beacon_loss(sdata);
1815
1816 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1817 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1818 }
1819 EXPORT_SYMBOL(ieee80211_beacon_loss);
1820
1821 void ieee80211_connection_loss(struct ieee80211_vif *vif)
1822 {
1823 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1824 struct ieee80211_hw *hw = &sdata->local->hw;
1825
1826 trace_api_connection_loss(sdata);
1827
1828 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1829 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1830 }
1831 EXPORT_SYMBOL(ieee80211_connection_loss);
1832
1833
1834 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
1835 bool assoc)
1836 {
1837 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1838
1839 lockdep_assert_held(&sdata->u.mgd.mtx);
1840
1841 if (!assoc) {
1842 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
1843
1844 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1845 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
1846 ieee80211_vif_release_channel(sdata);
1847 }
1848
1849 cfg80211_put_bss(auth_data->bss);
1850 kfree(auth_data);
1851 sdata->u.mgd.auth_data = NULL;
1852 }
1853
1854 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1855 struct ieee80211_mgmt *mgmt, size_t len)
1856 {
1857 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1858 u8 *pos;
1859 struct ieee802_11_elems elems;
1860
1861 pos = mgmt->u.auth.variable;
1862 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1863 if (!elems.challenge)
1864 return;
1865 auth_data->expected_transaction = 4;
1866 drv_mgd_prepare_tx(sdata->local, sdata);
1867 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
1868 elems.challenge - 2, elems.challenge_len + 2,
1869 auth_data->bss->bssid, auth_data->bss->bssid,
1870 auth_data->key, auth_data->key_len,
1871 auth_data->key_idx);
1872 }
1873
1874 static enum rx_mgmt_action __must_check
1875 ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1876 struct ieee80211_mgmt *mgmt, size_t len)
1877 {
1878 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1879 u8 bssid[ETH_ALEN];
1880 u16 auth_alg, auth_transaction, status_code;
1881 struct sta_info *sta;
1882
1883 lockdep_assert_held(&ifmgd->mtx);
1884
1885 if (len < 24 + 6)
1886 return RX_MGMT_NONE;
1887
1888 if (!ifmgd->auth_data || ifmgd->auth_data->done)
1889 return RX_MGMT_NONE;
1890
1891 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
1892
1893 if (!ether_addr_equal(bssid, mgmt->bssid))
1894 return RX_MGMT_NONE;
1895
1896 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1897 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1898 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1899
1900 if (auth_alg != ifmgd->auth_data->algorithm ||
1901 auth_transaction != ifmgd->auth_data->expected_transaction) {
1902 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
1903 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
1904 auth_transaction,
1905 ifmgd->auth_data->expected_transaction);
1906 return RX_MGMT_NONE;
1907 }
1908
1909 if (status_code != WLAN_STATUS_SUCCESS) {
1910 sdata_info(sdata, "%pM denied authentication (status %d)\n",
1911 mgmt->sa, status_code);
1912 ieee80211_destroy_auth_data(sdata, false);
1913 return RX_MGMT_CFG80211_RX_AUTH;
1914 }
1915
1916 switch (ifmgd->auth_data->algorithm) {
1917 case WLAN_AUTH_OPEN:
1918 case WLAN_AUTH_LEAP:
1919 case WLAN_AUTH_FT:
1920 break;
1921 case WLAN_AUTH_SHARED_KEY:
1922 if (ifmgd->auth_data->expected_transaction != 4) {
1923 ieee80211_auth_challenge(sdata, mgmt, len);
1924 /* need another frame */
1925 return RX_MGMT_NONE;
1926 }
1927 break;
1928 default:
1929 WARN_ONCE(1, "invalid auth alg %d",
1930 ifmgd->auth_data->algorithm);
1931 return RX_MGMT_NONE;
1932 }
1933
1934 sdata_info(sdata, "authenticated\n");
1935 ifmgd->auth_data->done = true;
1936 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
1937 run_again(ifmgd, ifmgd->auth_data->timeout);
1938
1939 /* move station state to auth */
1940 mutex_lock(&sdata->local->sta_mtx);
1941 sta = sta_info_get(sdata, bssid);
1942 if (!sta) {
1943 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
1944 goto out_err;
1945 }
1946 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
1947 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
1948 goto out_err;
1949 }
1950 mutex_unlock(&sdata->local->sta_mtx);
1951
1952 return RX_MGMT_CFG80211_RX_AUTH;
1953 out_err:
1954 mutex_unlock(&sdata->local->sta_mtx);
1955 /* ignore frame -- wait for timeout */
1956 return RX_MGMT_NONE;
1957 }
1958
1959
1960 static enum rx_mgmt_action __must_check
1961 ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1962 struct ieee80211_mgmt *mgmt, size_t len)
1963 {
1964 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1965 const u8 *bssid = NULL;
1966 u16 reason_code;
1967
1968 lockdep_assert_held(&ifmgd->mtx);
1969
1970 if (len < 24 + 2)
1971 return RX_MGMT_NONE;
1972
1973 if (!ifmgd->associated ||
1974 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
1975 return RX_MGMT_NONE;
1976
1977 bssid = ifmgd->associated->bssid;
1978
1979 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1980
1981 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
1982 bssid, reason_code);
1983
1984 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
1985
1986 mutex_lock(&sdata->local->mtx);
1987 ieee80211_recalc_idle(sdata->local);
1988 mutex_unlock(&sdata->local->mtx);
1989
1990 return RX_MGMT_CFG80211_DEAUTH;
1991 }
1992
1993
1994 static enum rx_mgmt_action __must_check
1995 ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1996 struct ieee80211_mgmt *mgmt, size_t len)
1997 {
1998 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1999 u16 reason_code;
2000
2001 lockdep_assert_held(&ifmgd->mtx);
2002
2003 if (len < 24 + 2)
2004 return RX_MGMT_NONE;
2005
2006 if (!ifmgd->associated ||
2007 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2008 return RX_MGMT_NONE;
2009
2010 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2011
2012 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2013 mgmt->sa, reason_code);
2014
2015 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2016
2017 mutex_lock(&sdata->local->mtx);
2018 ieee80211_recalc_idle(sdata->local);
2019 mutex_unlock(&sdata->local->mtx);
2020
2021 return RX_MGMT_CFG80211_DISASSOC;
2022 }
2023
2024 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2025 u8 *supp_rates, unsigned int supp_rates_len,
2026 u32 *rates, u32 *basic_rates,
2027 bool *have_higher_than_11mbit,
2028 int *min_rate, int *min_rate_index)
2029 {
2030 int i, j;
2031
2032 for (i = 0; i < supp_rates_len; i++) {
2033 int rate = (supp_rates[i] & 0x7f) * 5;
2034 bool is_basic = !!(supp_rates[i] & 0x80);
2035
2036 if (rate > 110)
2037 *have_higher_than_11mbit = true;
2038
2039 /*
2040 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2041 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2042 *
2043 * Note: Even through the membership selector and the basic
2044 * rate flag share the same bit, they are not exactly
2045 * the same.
2046 */
2047 if (!!(supp_rates[i] & 0x80) &&
2048 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2049 continue;
2050
2051 for (j = 0; j < sband->n_bitrates; j++) {
2052 if (sband->bitrates[j].bitrate == rate) {
2053 *rates |= BIT(j);
2054 if (is_basic)
2055 *basic_rates |= BIT(j);
2056 if (rate < *min_rate) {
2057 *min_rate = rate;
2058 *min_rate_index = j;
2059 }
2060 break;
2061 }
2062 }
2063 }
2064 }
2065
2066 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2067 bool assoc)
2068 {
2069 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2070
2071 lockdep_assert_held(&sdata->u.mgd.mtx);
2072
2073 if (!assoc) {
2074 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2075
2076 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2077 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2078 ieee80211_vif_release_channel(sdata);
2079 }
2080
2081 kfree(assoc_data);
2082 sdata->u.mgd.assoc_data = NULL;
2083 }
2084
2085 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2086 struct cfg80211_bss *cbss,
2087 struct ieee80211_mgmt *mgmt, size_t len)
2088 {
2089 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2090 struct ieee80211_local *local = sdata->local;
2091 struct ieee80211_supported_band *sband;
2092 struct sta_info *sta;
2093 u8 *pos;
2094 u16 capab_info, aid;
2095 struct ieee802_11_elems elems;
2096 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2097 u32 changed = 0;
2098 int err;
2099
2100 /* AssocResp and ReassocResp have identical structure */
2101
2102 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2103 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2104
2105 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2106 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2107 aid);
2108 aid &= ~(BIT(15) | BIT(14));
2109
2110 ifmgd->broken_ap = false;
2111
2112 if (aid == 0 || aid > IEEE80211_MAX_AID) {
2113 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2114 aid);
2115 aid = 0;
2116 ifmgd->broken_ap = true;
2117 }
2118
2119 pos = mgmt->u.assoc_resp.variable;
2120 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2121
2122 if (!elems.supp_rates) {
2123 sdata_info(sdata, "no SuppRates element in AssocResp\n");
2124 return false;
2125 }
2126
2127 ifmgd->aid = aid;
2128
2129 mutex_lock(&sdata->local->sta_mtx);
2130 /*
2131 * station info was already allocated and inserted before
2132 * the association and should be available to us
2133 */
2134 sta = sta_info_get(sdata, cbss->bssid);
2135 if (WARN_ON(!sta)) {
2136 mutex_unlock(&sdata->local->sta_mtx);
2137 return false;
2138 }
2139
2140 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
2141
2142 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
2143 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
2144 elems.ht_cap_elem, &sta->sta.ht_cap);
2145
2146 sta->supports_40mhz =
2147 sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2148
2149 rate_control_rate_init(sta);
2150
2151 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
2152 set_sta_flag(sta, WLAN_STA_MFP);
2153
2154 if (elems.wmm_param)
2155 set_sta_flag(sta, WLAN_STA_WME);
2156
2157 err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
2158 if (!err)
2159 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2160 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2161 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2162 if (err) {
2163 sdata_info(sdata,
2164 "failed to move station %pM to desired state\n",
2165 sta->sta.addr);
2166 WARN_ON(__sta_info_destroy(sta));
2167 mutex_unlock(&sdata->local->sta_mtx);
2168 return false;
2169 }
2170
2171 mutex_unlock(&sdata->local->sta_mtx);
2172
2173 /*
2174 * Always handle WMM once after association regardless
2175 * of the first value the AP uses. Setting -1 here has
2176 * that effect because the AP values is an unsigned
2177 * 4-bit value.
2178 */
2179 ifmgd->wmm_last_param_set = -1;
2180
2181 if (elems.wmm_param)
2182 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2183 elems.wmm_param_len);
2184 else
2185 ieee80211_set_wmm_default(sdata, false);
2186 changed |= BSS_CHANGED_QOS;
2187
2188 if (elems.ht_operation && elems.wmm_param &&
2189 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
2190 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2191 cbss->bssid, false);
2192
2193 /* set AID and assoc capability,
2194 * ieee80211_set_associated() will tell the driver */
2195 bss_conf->aid = aid;
2196 bss_conf->assoc_capability = capab_info;
2197 ieee80211_set_associated(sdata, cbss, changed);
2198
2199 /*
2200 * If we're using 4-addr mode, let the AP know that we're
2201 * doing so, so that it can create the STA VLAN on its side
2202 */
2203 if (ifmgd->use_4addr)
2204 ieee80211_send_4addr_nullfunc(local, sdata);
2205
2206 /*
2207 * Start timer to probe the connection to the AP now.
2208 * Also start the timer that will detect beacon loss.
2209 */
2210 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
2211 ieee80211_sta_reset_beacon_monitor(sdata);
2212
2213 return true;
2214 }
2215
2216 static enum rx_mgmt_action __must_check
2217 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2218 struct ieee80211_mgmt *mgmt, size_t len,
2219 struct cfg80211_bss **bss)
2220 {
2221 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2222 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2223 u16 capab_info, status_code, aid;
2224 struct ieee802_11_elems elems;
2225 u8 *pos;
2226 bool reassoc;
2227
2228 lockdep_assert_held(&ifmgd->mtx);
2229
2230 if (!assoc_data)
2231 return RX_MGMT_NONE;
2232 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
2233 return RX_MGMT_NONE;
2234
2235 /*
2236 * AssocResp and ReassocResp have identical structure, so process both
2237 * of them in this function.
2238 */
2239
2240 if (len < 24 + 6)
2241 return RX_MGMT_NONE;
2242
2243 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2244 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2245 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2246 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2247
2248 sdata_info(sdata,
2249 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2250 reassoc ? "Rea" : "A", mgmt->sa,
2251 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
2252
2253 pos = mgmt->u.assoc_resp.variable;
2254 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2255
2256 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2257 elems.timeout_int && elems.timeout_int_len == 5 &&
2258 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2259 u32 tu, ms;
2260 tu = get_unaligned_le32(elems.timeout_int + 1);
2261 ms = tu * 1024 / 1000;
2262 sdata_info(sdata,
2263 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2264 mgmt->sa, tu, ms);
2265 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2266 if (ms > IEEE80211_ASSOC_TIMEOUT)
2267 run_again(ifmgd, assoc_data->timeout);
2268 return RX_MGMT_NONE;
2269 }
2270
2271 *bss = assoc_data->bss;
2272
2273 if (status_code != WLAN_STATUS_SUCCESS) {
2274 sdata_info(sdata, "%pM denied association (code=%d)\n",
2275 mgmt->sa, status_code);
2276 ieee80211_destroy_assoc_data(sdata, false);
2277 } else {
2278 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2279 /* oops -- internal error -- send timeout for now */
2280 ieee80211_destroy_assoc_data(sdata, false);
2281 cfg80211_put_bss(*bss);
2282 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2283 }
2284 sdata_info(sdata, "associated\n");
2285
2286 /*
2287 * destroy assoc_data afterwards, as otherwise an idle
2288 * recalc after assoc_data is NULL but before associated
2289 * is set can cause the interface to go idle
2290 */
2291 ieee80211_destroy_assoc_data(sdata, true);
2292 }
2293
2294 return RX_MGMT_CFG80211_RX_ASSOC;
2295 }
2296 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
2297 struct ieee80211_mgmt *mgmt,
2298 size_t len,
2299 struct ieee80211_rx_status *rx_status,
2300 struct ieee802_11_elems *elems,
2301 bool beacon)
2302 {
2303 struct ieee80211_local *local = sdata->local;
2304 int freq;
2305 struct ieee80211_bss *bss;
2306 struct ieee80211_channel *channel;
2307 bool need_ps = false;
2308
2309 if (sdata->u.mgd.associated &&
2310 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) {
2311 bss = (void *)sdata->u.mgd.associated->priv;
2312 /* not previously set so we may need to recalc */
2313 need_ps = !bss->dtim_period;
2314 }
2315
2316 if (elems->ds_params && elems->ds_params_len == 1)
2317 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2318 rx_status->band);
2319 else
2320 freq = rx_status->freq;
2321
2322 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2323
2324 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2325 return;
2326
2327 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
2328 channel, beacon);
2329 if (bss)
2330 ieee80211_rx_bss_put(local, bss);
2331
2332 if (!sdata->u.mgd.associated)
2333 return;
2334
2335 if (need_ps) {
2336 mutex_lock(&local->iflist_mtx);
2337 ieee80211_recalc_ps(local, -1);
2338 mutex_unlock(&local->iflist_mtx);
2339 }
2340
2341 if (elems->ch_switch_ie &&
2342 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2343 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
2344 bss, rx_status->mactime);
2345 }
2346
2347
2348 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
2349 struct sk_buff *skb)
2350 {
2351 struct ieee80211_mgmt *mgmt = (void *)skb->data;
2352 struct ieee80211_if_managed *ifmgd;
2353 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2354 size_t baselen, len = skb->len;
2355 struct ieee802_11_elems elems;
2356
2357 ifmgd = &sdata->u.mgd;
2358
2359 ASSERT_MGD_MTX(ifmgd);
2360
2361 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
2362 return; /* ignore ProbeResp to foreign address */
2363
2364 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2365 if (baselen > len)
2366 return;
2367
2368 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2369 &elems);
2370
2371 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
2372
2373 if (ifmgd->associated &&
2374 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2375 ieee80211_reset_ap_probe(sdata);
2376
2377 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
2378 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
2379 /* got probe response, continue with auth */
2380 sdata_info(sdata, "direct probe responded\n");
2381 ifmgd->auth_data->tries = 0;
2382 ifmgd->auth_data->timeout = jiffies;
2383 run_again(ifmgd, ifmgd->auth_data->timeout);
2384 }
2385 }
2386
2387 /*
2388 * This is the canonical list of information elements we care about,
2389 * the filter code also gives us all changes to the Microsoft OUI
2390 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2391 *
2392 * We implement beacon filtering in software since that means we can
2393 * avoid processing the frame here and in cfg80211, and userspace
2394 * will not be able to tell whether the hardware supports it or not.
2395 *
2396 * XXX: This list needs to be dynamic -- userspace needs to be able to
2397 * add items it requires. It also needs to be able to tell us to
2398 * look out for other vendor IEs.
2399 */
2400 static const u64 care_about_ies =
2401 (1ULL << WLAN_EID_COUNTRY) |
2402 (1ULL << WLAN_EID_ERP_INFO) |
2403 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2404 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2405 (1ULL << WLAN_EID_HT_CAPABILITY) |
2406 (1ULL << WLAN_EID_HT_OPERATION);
2407
2408 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2409 struct ieee80211_mgmt *mgmt,
2410 size_t len,
2411 struct ieee80211_rx_status *rx_status)
2412 {
2413 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2414 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2415 size_t baselen;
2416 struct ieee802_11_elems elems;
2417 struct ieee80211_local *local = sdata->local;
2418 struct ieee80211_chanctx_conf *chanctx_conf;
2419 struct ieee80211_channel *chan;
2420 u32 changed = 0;
2421 bool erp_valid, directed_tim = false;
2422 u8 erp_value = 0;
2423 u32 ncrc;
2424 u8 *bssid;
2425
2426 lockdep_assert_held(&ifmgd->mtx);
2427
2428 /* Process beacon from the current BSS */
2429 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2430 if (baselen > len)
2431 return;
2432
2433 rcu_read_lock();
2434 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2435 if (!chanctx_conf) {
2436 rcu_read_unlock();
2437 return;
2438 }
2439
2440 if (rx_status->freq != chanctx_conf->channel->center_freq) {
2441 rcu_read_unlock();
2442 return;
2443 }
2444 chan = chanctx_conf->channel;
2445 rcu_read_unlock();
2446
2447 if (ifmgd->assoc_data && !ifmgd->assoc_data->have_beacon &&
2448 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
2449 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2450 len - baselen, &elems);
2451
2452 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2453 false);
2454 ifmgd->assoc_data->have_beacon = true;
2455 ifmgd->assoc_data->sent_assoc = false;
2456 /* continue assoc process */
2457 ifmgd->assoc_data->timeout = jiffies;
2458 run_again(ifmgd, ifmgd->assoc_data->timeout);
2459 return;
2460 }
2461
2462 if (!ifmgd->associated ||
2463 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2464 return;
2465 bssid = ifmgd->associated->bssid;
2466
2467 /* Track average RSSI from the Beacon frames of the current AP */
2468 ifmgd->last_beacon_signal = rx_status->signal;
2469 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2470 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
2471 ifmgd->ave_beacon_signal = rx_status->signal * 16;
2472 ifmgd->last_cqm_event_signal = 0;
2473 ifmgd->count_beacon_signal = 1;
2474 ifmgd->last_ave_beacon_signal = 0;
2475 } else {
2476 ifmgd->ave_beacon_signal =
2477 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2478 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2479 ifmgd->ave_beacon_signal) / 16;
2480 ifmgd->count_beacon_signal++;
2481 }
2482
2483 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2484 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2485 int sig = ifmgd->ave_beacon_signal;
2486 int last_sig = ifmgd->last_ave_beacon_signal;
2487
2488 /*
2489 * if signal crosses either of the boundaries, invoke callback
2490 * with appropriate parameters
2491 */
2492 if (sig > ifmgd->rssi_max_thold &&
2493 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2494 ifmgd->last_ave_beacon_signal = sig;
2495 drv_rssi_callback(local, RSSI_EVENT_HIGH);
2496 } else if (sig < ifmgd->rssi_min_thold &&
2497 (last_sig >= ifmgd->rssi_max_thold ||
2498 last_sig == 0)) {
2499 ifmgd->last_ave_beacon_signal = sig;
2500 drv_rssi_callback(local, RSSI_EVENT_LOW);
2501 }
2502 }
2503
2504 if (bss_conf->cqm_rssi_thold &&
2505 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
2506 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
2507 int sig = ifmgd->ave_beacon_signal / 16;
2508 int last_event = ifmgd->last_cqm_event_signal;
2509 int thold = bss_conf->cqm_rssi_thold;
2510 int hyst = bss_conf->cqm_rssi_hyst;
2511 if (sig < thold &&
2512 (last_event == 0 || sig < last_event - hyst)) {
2513 ifmgd->last_cqm_event_signal = sig;
2514 ieee80211_cqm_rssi_notify(
2515 &sdata->vif,
2516 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2517 GFP_KERNEL);
2518 } else if (sig > thold &&
2519 (last_event == 0 || sig > last_event + hyst)) {
2520 ifmgd->last_cqm_event_signal = sig;
2521 ieee80211_cqm_rssi_notify(
2522 &sdata->vif,
2523 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2524 GFP_KERNEL);
2525 }
2526 }
2527
2528 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
2529 mlme_dbg_ratelimited(sdata,
2530 "cancelling probereq poll due to a received beacon\n");
2531 mutex_lock(&local->mtx);
2532 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
2533 ieee80211_run_deferred_scan(local);
2534 mutex_unlock(&local->mtx);
2535
2536 mutex_lock(&local->iflist_mtx);
2537 ieee80211_recalc_ps(local, -1);
2538 mutex_unlock(&local->iflist_mtx);
2539 }
2540
2541 /*
2542 * Push the beacon loss detection into the future since
2543 * we are processing a beacon from the AP just now.
2544 */
2545 ieee80211_sta_reset_beacon_monitor(sdata);
2546
2547 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2548 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2549 len - baselen, &elems,
2550 care_about_ies, ncrc);
2551
2552 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
2553 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
2554 ifmgd->aid);
2555
2556 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
2557 if (directed_tim) {
2558 if (local->hw.conf.dynamic_ps_timeout > 0) {
2559 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2560 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2561 ieee80211_hw_config(local,
2562 IEEE80211_CONF_CHANGE_PS);
2563 }
2564 ieee80211_send_nullfunc(local, sdata, 0);
2565 } else if (!local->pspolling && sdata->u.mgd.powersave) {
2566 local->pspolling = true;
2567
2568 /*
2569 * Here is assumed that the driver will be
2570 * able to send ps-poll frame and receive a
2571 * response even though power save mode is
2572 * enabled, but some drivers might require
2573 * to disable power save here. This needs
2574 * to be investigated.
2575 */
2576 ieee80211_send_pspoll(local, sdata);
2577 }
2578 }
2579 }
2580
2581 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
2582 return;
2583 ifmgd->beacon_crc = ncrc;
2584 ifmgd->beacon_crc_valid = true;
2585
2586 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2587 true);
2588
2589 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2590 elems.wmm_param_len))
2591 changed |= BSS_CHANGED_QOS;
2592
2593 if (elems.erp_info && elems.erp_info_len >= 1) {
2594 erp_valid = true;
2595 erp_value = elems.erp_info[0];
2596 } else {
2597 erp_valid = false;
2598 }
2599 changed |= ieee80211_handle_bss_capability(sdata,
2600 le16_to_cpu(mgmt->u.beacon.capab_info),
2601 erp_valid, erp_value);
2602
2603
2604 if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
2605 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
2606 struct ieee80211_supported_band *sband;
2607
2608 sband = local->hw.wiphy->bands[chan->band];
2609
2610 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2611 bssid, true);
2612 }
2613
2614 if (elems.country_elem && elems.pwr_constr_elem &&
2615 mgmt->u.probe_resp.capab_info &
2616 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
2617 ieee80211_handle_pwr_constr(sdata, chan,
2618 elems.country_elem,
2619 elems.country_elem_len,
2620 elems.pwr_constr_elem);
2621
2622 ieee80211_bss_info_change_notify(sdata, changed);
2623 }
2624
2625 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2626 struct sk_buff *skb)
2627 {
2628 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2629 struct ieee80211_rx_status *rx_status;
2630 struct ieee80211_mgmt *mgmt;
2631 struct cfg80211_bss *bss = NULL;
2632 enum rx_mgmt_action rma = RX_MGMT_NONE;
2633 u16 fc;
2634
2635 rx_status = (struct ieee80211_rx_status *) skb->cb;
2636 mgmt = (struct ieee80211_mgmt *) skb->data;
2637 fc = le16_to_cpu(mgmt->frame_control);
2638
2639 mutex_lock(&ifmgd->mtx);
2640
2641 switch (fc & IEEE80211_FCTL_STYPE) {
2642 case IEEE80211_STYPE_BEACON:
2643 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2644 break;
2645 case IEEE80211_STYPE_PROBE_RESP:
2646 ieee80211_rx_mgmt_probe_resp(sdata, skb);
2647 break;
2648 case IEEE80211_STYPE_AUTH:
2649 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
2650 break;
2651 case IEEE80211_STYPE_DEAUTH:
2652 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
2653 break;
2654 case IEEE80211_STYPE_DISASSOC:
2655 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2656 break;
2657 case IEEE80211_STYPE_ASSOC_RESP:
2658 case IEEE80211_STYPE_REASSOC_RESP:
2659 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
2660 break;
2661 case IEEE80211_STYPE_ACTION:
2662 switch (mgmt->u.action.category) {
2663 case WLAN_CATEGORY_SPECTRUM_MGMT:
2664 ieee80211_sta_process_chanswitch(sdata,
2665 &mgmt->u.action.u.chan_switch.sw_elem,
2666 (void *)ifmgd->associated->priv,
2667 rx_status->mactime);
2668 break;
2669 }
2670 }
2671 mutex_unlock(&ifmgd->mtx);
2672
2673 switch (rma) {
2674 case RX_MGMT_NONE:
2675 /* no action */
2676 break;
2677 case RX_MGMT_CFG80211_DEAUTH:
2678 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
2679 break;
2680 case RX_MGMT_CFG80211_DISASSOC:
2681 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
2682 break;
2683 case RX_MGMT_CFG80211_RX_AUTH:
2684 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
2685 break;
2686 case RX_MGMT_CFG80211_RX_ASSOC:
2687 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
2688 break;
2689 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
2690 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
2691 break;
2692 default:
2693 WARN(1, "unexpected: %d", rma);
2694 }
2695 }
2696
2697 static void ieee80211_sta_timer(unsigned long data)
2698 {
2699 struct ieee80211_sub_if_data *sdata =
2700 (struct ieee80211_sub_if_data *) data;
2701 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2702 struct ieee80211_local *local = sdata->local;
2703
2704 if (local->quiescing) {
2705 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2706 return;
2707 }
2708
2709 ieee80211_queue_work(&local->hw, &sdata->work);
2710 }
2711
2712 static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
2713 u8 *bssid, u8 reason)
2714 {
2715 struct ieee80211_local *local = sdata->local;
2716 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2717 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
2718
2719 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
2720 false, frame_buf);
2721 mutex_unlock(&ifmgd->mtx);
2722
2723 /*
2724 * must be outside lock due to cfg80211,
2725 * but that's not a problem.
2726 */
2727 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
2728
2729 mutex_lock(&local->mtx);
2730 ieee80211_recalc_idle(local);
2731 mutex_unlock(&local->mtx);
2732
2733 mutex_lock(&ifmgd->mtx);
2734 }
2735
2736 static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
2737 {
2738 struct ieee80211_local *local = sdata->local;
2739 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2740 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
2741
2742 lockdep_assert_held(&ifmgd->mtx);
2743
2744 if (WARN_ON_ONCE(!auth_data))
2745 return -EINVAL;
2746
2747 auth_data->tries++;
2748
2749 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
2750 sdata_info(sdata, "authentication with %pM timed out\n",
2751 auth_data->bss->bssid);
2752
2753 /*
2754 * Most likely AP is not in the range so remove the
2755 * bss struct for that AP.
2756 */
2757 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
2758
2759 return -ETIMEDOUT;
2760 }
2761
2762 drv_mgd_prepare_tx(local, sdata);
2763
2764 if (auth_data->bss->proberesp_ies) {
2765 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
2766 auth_data->bss->bssid, auth_data->tries,
2767 IEEE80211_AUTH_MAX_TRIES);
2768
2769 auth_data->expected_transaction = 2;
2770 ieee80211_send_auth(sdata, 1, auth_data->algorithm, 0,
2771 auth_data->ie, auth_data->ie_len,
2772 auth_data->bss->bssid,
2773 auth_data->bss->bssid, NULL, 0, 0);
2774 } else {
2775 const u8 *ssidie;
2776
2777 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
2778 auth_data->bss->bssid, auth_data->tries,
2779 IEEE80211_AUTH_MAX_TRIES);
2780
2781 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
2782 if (!ssidie)
2783 return -EINVAL;
2784 /*
2785 * Direct probe is sent to broadcast address as some APs
2786 * will not answer to direct packet in unassociated state.
2787 */
2788 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
2789 NULL, 0, (u32) -1, true, false,
2790 auth_data->bss->channel, false);
2791 }
2792
2793 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
2794 run_again(ifmgd, auth_data->timeout);
2795
2796 return 0;
2797 }
2798
2799 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
2800 {
2801 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2802 struct ieee80211_local *local = sdata->local;
2803
2804 lockdep_assert_held(&sdata->u.mgd.mtx);
2805
2806 assoc_data->tries++;
2807 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
2808 sdata_info(sdata, "association with %pM timed out\n",
2809 assoc_data->bss->bssid);
2810
2811 /*
2812 * Most likely AP is not in the range so remove the
2813 * bss struct for that AP.
2814 */
2815 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
2816
2817 return -ETIMEDOUT;
2818 }
2819
2820 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
2821 assoc_data->bss->bssid, assoc_data->tries,
2822 IEEE80211_ASSOC_MAX_TRIES);
2823 ieee80211_send_assoc(sdata);
2824
2825 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
2826 run_again(&sdata->u.mgd, assoc_data->timeout);
2827
2828 return 0;
2829 }
2830
2831 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
2832 {
2833 struct ieee80211_local *local = sdata->local;
2834 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2835
2836 mutex_lock(&ifmgd->mtx);
2837
2838 if (ifmgd->auth_data &&
2839 time_after(jiffies, ifmgd->auth_data->timeout)) {
2840 if (ifmgd->auth_data->done) {
2841 /*
2842 * ok ... we waited for assoc but userspace didn't,
2843 * so let's just kill the auth data
2844 */
2845 ieee80211_destroy_auth_data(sdata, false);
2846 } else if (ieee80211_probe_auth(sdata)) {
2847 u8 bssid[ETH_ALEN];
2848
2849 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2850
2851 ieee80211_destroy_auth_data(sdata, false);
2852
2853 mutex_unlock(&ifmgd->mtx);
2854 cfg80211_send_auth_timeout(sdata->dev, bssid);
2855 mutex_lock(&ifmgd->mtx);
2856 }
2857 } else if (ifmgd->auth_data)
2858 run_again(ifmgd, ifmgd->auth_data->timeout);
2859
2860 if (ifmgd->assoc_data &&
2861 time_after(jiffies, ifmgd->assoc_data->timeout)) {
2862 if (!ifmgd->assoc_data->have_beacon ||
2863 ieee80211_do_assoc(sdata)) {
2864 u8 bssid[ETH_ALEN];
2865
2866 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
2867
2868 ieee80211_destroy_assoc_data(sdata, false);
2869
2870 mutex_unlock(&ifmgd->mtx);
2871 cfg80211_send_assoc_timeout(sdata->dev, bssid);
2872 mutex_lock(&ifmgd->mtx);
2873 }
2874 } else if (ifmgd->assoc_data)
2875 run_again(ifmgd, ifmgd->assoc_data->timeout);
2876
2877 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2878 IEEE80211_STA_CONNECTION_POLL) &&
2879 ifmgd->associated) {
2880 u8 bssid[ETH_ALEN];
2881 int max_tries;
2882
2883 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
2884
2885 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2886 max_tries = max_nullfunc_tries;
2887 else
2888 max_tries = max_probe_tries;
2889
2890 /* ACK received for nullfunc probing frame */
2891 if (!ifmgd->probe_send_count)
2892 ieee80211_reset_ap_probe(sdata);
2893 else if (ifmgd->nullfunc_failed) {
2894 if (ifmgd->probe_send_count < max_tries) {
2895 mlme_dbg(sdata,
2896 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
2897 bssid, ifmgd->probe_send_count,
2898 max_tries);
2899 ieee80211_mgd_probe_ap_send(sdata);
2900 } else {
2901 mlme_dbg(sdata,
2902 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
2903 bssid);
2904 ieee80211_sta_connection_lost(sdata, bssid,
2905 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2906 }
2907 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
2908 run_again(ifmgd, ifmgd->probe_timeout);
2909 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
2910 mlme_dbg(sdata,
2911 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
2912 bssid, probe_wait_ms);
2913 ieee80211_sta_connection_lost(sdata, bssid,
2914 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2915 } else if (ifmgd->probe_send_count < max_tries) {
2916 mlme_dbg(sdata,
2917 "No probe response from AP %pM after %dms, try %d/%i\n",
2918 bssid, probe_wait_ms,
2919 ifmgd->probe_send_count, max_tries);
2920 ieee80211_mgd_probe_ap_send(sdata);
2921 } else {
2922 /*
2923 * We actually lost the connection ... or did we?
2924 * Let's make sure!
2925 */
2926 wiphy_debug(local->hw.wiphy,
2927 "%s: No probe response from AP %pM"
2928 " after %dms, disconnecting.\n",
2929 sdata->name,
2930 bssid, probe_wait_ms);
2931
2932 ieee80211_sta_connection_lost(sdata, bssid,
2933 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2934 }
2935 }
2936
2937 mutex_unlock(&ifmgd->mtx);
2938
2939 mutex_lock(&local->mtx);
2940 ieee80211_recalc_idle(local);
2941 mutex_unlock(&local->mtx);
2942 }
2943
2944 static void ieee80211_sta_bcn_mon_timer(unsigned long data)
2945 {
2946 struct ieee80211_sub_if_data *sdata =
2947 (struct ieee80211_sub_if_data *) data;
2948 struct ieee80211_local *local = sdata->local;
2949
2950 if (local->quiescing)
2951 return;
2952
2953 ieee80211_queue_work(&sdata->local->hw,
2954 &sdata->u.mgd.beacon_connection_loss_work);
2955 }
2956
2957 static void ieee80211_sta_conn_mon_timer(unsigned long data)
2958 {
2959 struct ieee80211_sub_if_data *sdata =
2960 (struct ieee80211_sub_if_data *) data;
2961 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2962 struct ieee80211_local *local = sdata->local;
2963
2964 if (local->quiescing)
2965 return;
2966
2967 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
2968 }
2969
2970 static void ieee80211_sta_monitor_work(struct work_struct *work)
2971 {
2972 struct ieee80211_sub_if_data *sdata =
2973 container_of(work, struct ieee80211_sub_if_data,
2974 u.mgd.monitor_work);
2975
2976 ieee80211_mgd_probe_ap(sdata, false);
2977 }
2978
2979 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2980 {
2981 u32 flags;
2982
2983 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2984 __ieee80211_stop_poll(sdata);
2985
2986 /* let's probe the connection once */
2987 flags = sdata->local->hw.flags;
2988 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
2989 ieee80211_queue_work(&sdata->local->hw,
2990 &sdata->u.mgd.monitor_work);
2991 /* and do all the other regular work too */
2992 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
2993 }
2994 }
2995
2996 #ifdef CONFIG_PM
2997 void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
2998 {
2999 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3000
3001 /*
3002 * we need to use atomic bitops for the running bits
3003 * only because both timers might fire at the same
3004 * time -- the code here is properly synchronised.
3005 */
3006
3007 cancel_work_sync(&ifmgd->request_smps_work);
3008
3009 cancel_work_sync(&ifmgd->monitor_work);
3010 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
3011 cancel_work_sync(&ifmgd->csa_connection_drop_work);
3012 if (del_timer_sync(&ifmgd->timer))
3013 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3014
3015 cancel_work_sync(&ifmgd->chswitch_work);
3016 if (del_timer_sync(&ifmgd->chswitch_timer))
3017 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
3018
3019 /* these will just be re-established on connection */
3020 del_timer_sync(&ifmgd->conn_mon_timer);
3021 del_timer_sync(&ifmgd->bcn_mon_timer);
3022 }
3023
3024 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3025 {
3026 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3027
3028 if (!ifmgd->associated)
3029 return;
3030
3031 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3032 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3033 mutex_lock(&ifmgd->mtx);
3034 if (ifmgd->associated) {
3035 mlme_dbg(sdata,
3036 "driver requested disconnect after resume\n");
3037 ieee80211_sta_connection_lost(sdata,
3038 ifmgd->associated->bssid,
3039 WLAN_REASON_UNSPECIFIED);
3040 mutex_unlock(&ifmgd->mtx);
3041 return;
3042 }
3043 mutex_unlock(&ifmgd->mtx);
3044 }
3045
3046 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3047 add_timer(&ifmgd->timer);
3048 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3049 add_timer(&ifmgd->chswitch_timer);
3050 ieee80211_sta_reset_beacon_monitor(sdata);
3051
3052 mutex_lock(&sdata->local->mtx);
3053 ieee80211_restart_sta_timer(sdata);
3054 mutex_unlock(&sdata->local->mtx);
3055 }
3056 #endif
3057
3058 /* interface setup */
3059 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3060 {
3061 struct ieee80211_if_managed *ifmgd;
3062
3063 ifmgd = &sdata->u.mgd;
3064 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
3065 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
3066 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3067 ieee80211_beacon_connection_loss_work);
3068 INIT_WORK(&ifmgd->csa_connection_drop_work,
3069 ieee80211_csa_connection_drop_work);
3070 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
3071 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
3072 (unsigned long) sdata);
3073 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3074 (unsigned long) sdata);
3075 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3076 (unsigned long) sdata);
3077 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
3078 (unsigned long) sdata);
3079
3080 ifmgd->flags = 0;
3081 ifmgd->powersave = sdata->wdev.ps;
3082 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3083 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
3084
3085 mutex_init(&ifmgd->mtx);
3086
3087 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3088 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3089 else
3090 ifmgd->req_smps = IEEE80211_SMPS_OFF;
3091 }
3092
3093 /* scan finished notification */
3094 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3095 {
3096 struct ieee80211_sub_if_data *sdata;
3097
3098 /* Restart STA timers */
3099 rcu_read_lock();
3100 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3101 ieee80211_restart_sta_timer(sdata);
3102 rcu_read_unlock();
3103 }
3104
3105 int ieee80211_max_network_latency(struct notifier_block *nb,
3106 unsigned long data, void *dummy)
3107 {
3108 s32 latency_usec = (s32) data;
3109 struct ieee80211_local *local =
3110 container_of(nb, struct ieee80211_local,
3111 network_latency_notifier);
3112
3113 mutex_lock(&local->iflist_mtx);
3114 ieee80211_recalc_ps(local, latency_usec);
3115 mutex_unlock(&local->iflist_mtx);
3116
3117 return 0;
3118 }
3119
3120 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3121 struct cfg80211_bss *cbss)
3122 {
3123 struct ieee80211_local *local = sdata->local;
3124 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3125 int ht_cfreq;
3126 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
3127 const u8 *ht_oper_ie;
3128 const struct ieee80211_ht_operation *ht_oper = NULL;
3129 struct ieee80211_supported_band *sband;
3130
3131 sband = local->hw.wiphy->bands[cbss->channel->band];
3132
3133 ifmgd->flags &= ~IEEE80211_STA_DISABLE_40MHZ;
3134
3135 if (sband->ht_cap.ht_supported) {
3136 ht_oper_ie = cfg80211_find_ie(WLAN_EID_HT_OPERATION,
3137 cbss->information_elements,
3138 cbss->len_information_elements);
3139 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3140 ht_oper = (void *)(ht_oper_ie + 2);
3141 }
3142
3143 if (ht_oper) {
3144 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
3145 cbss->channel->band);
3146 /* check that channel matches the right operating channel */
3147 if (cbss->channel->center_freq != ht_cfreq) {
3148 /*
3149 * It's possible that some APs are confused here;
3150 * Netgear WNDR3700 sometimes reports 4 higher than
3151 * the actual channel in association responses, but
3152 * since we look at probe response/beacon data here
3153 * it should be OK.
3154 */
3155 sdata_info(sdata,
3156 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
3157 cbss->channel->center_freq,
3158 ht_cfreq, ht_oper->primary_chan,
3159 cbss->channel->band);
3160 ht_oper = NULL;
3161 }
3162 }
3163
3164 if (ht_oper) {
3165 const u8 *ht_cap_ie;
3166 const struct ieee80211_ht_cap *ht_cap;
3167 u8 chains = 1;
3168
3169 channel_type = NL80211_CHAN_HT20;
3170
3171 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
3172 switch (ht_oper->ht_param &
3173 IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3174 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3175 channel_type = NL80211_CHAN_HT40PLUS;
3176 break;
3177 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3178 channel_type = NL80211_CHAN_HT40MINUS;
3179 break;
3180 }
3181 }
3182
3183 ht_cap_ie = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY,
3184 cbss->information_elements,
3185 cbss->len_information_elements);
3186 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3187 ht_cap = (void *)(ht_cap_ie + 2);
3188 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3189 }
3190 sdata->needed_rx_chains = min(chains, local->rx_chains);
3191 } else {
3192 sdata->needed_rx_chains = 1;
3193 }
3194
3195 /* will change later if needed */
3196 sdata->smps_mode = IEEE80211_SMPS_OFF;
3197
3198 ieee80211_vif_release_channel(sdata);
3199 return ieee80211_vif_use_channel(sdata, cbss->channel, channel_type,
3200 IEEE80211_CHANCTX_SHARED);
3201 }
3202
3203 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3204 struct cfg80211_bss *cbss, bool assoc)
3205 {
3206 struct ieee80211_local *local = sdata->local;
3207 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3208 struct ieee80211_bss *bss = (void *)cbss->priv;
3209 struct sta_info *new_sta = NULL;
3210 bool have_sta = false;
3211 int err;
3212
3213 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3214 return -EINVAL;
3215
3216 if (assoc) {
3217 rcu_read_lock();
3218 have_sta = sta_info_get(sdata, cbss->bssid);
3219 rcu_read_unlock();
3220 }
3221
3222 if (!have_sta) {
3223 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3224 if (!new_sta)
3225 return -ENOMEM;
3226 }
3227
3228 mutex_lock(&local->mtx);
3229 ieee80211_recalc_idle(sdata->local);
3230 mutex_unlock(&local->mtx);
3231
3232 if (new_sta) {
3233 u32 rates = 0, basic_rates = 0;
3234 bool have_higher_than_11mbit;
3235 int min_rate = INT_MAX, min_rate_index = -1;
3236 struct ieee80211_supported_band *sband;
3237
3238 sband = local->hw.wiphy->bands[cbss->channel->band];
3239
3240 err = ieee80211_prep_channel(sdata, cbss);
3241 if (err) {
3242 sta_info_free(local, new_sta);
3243 return err;
3244 }
3245
3246 ieee80211_get_rates(sband, bss->supp_rates,
3247 bss->supp_rates_len,
3248 &rates, &basic_rates,
3249 &have_higher_than_11mbit,
3250 &min_rate, &min_rate_index);
3251
3252 /*
3253 * This used to be a workaround for basic rates missing
3254 * in the association response frame. Now that we no
3255 * longer use the basic rates from there, it probably
3256 * doesn't happen any more, but keep the workaround so
3257 * in case some *other* APs are buggy in different ways
3258 * we can connect -- with a warning.
3259 */
3260 if (!basic_rates && min_rate_index >= 0) {
3261 sdata_info(sdata,
3262 "No basic rates, using min rate instead\n");
3263 basic_rates = BIT(min_rate_index);
3264 }
3265
3266 new_sta->sta.supp_rates[cbss->channel->band] = rates;
3267 sdata->vif.bss_conf.basic_rates = basic_rates;
3268
3269 /* cf. IEEE 802.11 9.2.12 */
3270 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
3271 have_higher_than_11mbit)
3272 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3273 else
3274 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3275
3276 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3277
3278 /* set timing information */
3279 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
3280 sdata->vif.bss_conf.sync_tsf = cbss->tsf;
3281 sdata->vif.bss_conf.sync_device_ts = bss->device_ts;
3282
3283 /* tell driver about BSSID, basic rates and timing */
3284 ieee80211_bss_info_change_notify(sdata,
3285 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3286 BSS_CHANGED_BEACON_INT);
3287
3288 if (assoc)
3289 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
3290
3291 err = sta_info_insert(new_sta);
3292 new_sta = NULL;
3293 if (err) {
3294 sdata_info(sdata,
3295 "failed to insert STA entry for the AP (error %d)\n",
3296 err);
3297 return err;
3298 }
3299 } else
3300 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
3301
3302 return 0;
3303 }
3304
3305 /* config hooks */
3306 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3307 struct cfg80211_auth_request *req)
3308 {
3309 struct ieee80211_local *local = sdata->local;
3310 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3311 struct ieee80211_mgd_auth_data *auth_data;
3312 u16 auth_alg;
3313 int err;
3314
3315 /* prepare auth data structure */
3316
3317 switch (req->auth_type) {
3318 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3319 auth_alg = WLAN_AUTH_OPEN;
3320 break;
3321 case NL80211_AUTHTYPE_SHARED_KEY:
3322 if (IS_ERR(local->wep_tx_tfm))
3323 return -EOPNOTSUPP;
3324 auth_alg = WLAN_AUTH_SHARED_KEY;
3325 break;
3326 case NL80211_AUTHTYPE_FT:
3327 auth_alg = WLAN_AUTH_FT;
3328 break;
3329 case NL80211_AUTHTYPE_NETWORK_EAP:
3330 auth_alg = WLAN_AUTH_LEAP;
3331 break;
3332 default:
3333 return -EOPNOTSUPP;
3334 }
3335
3336 auth_data = kzalloc(sizeof(*auth_data) + req->ie_len, GFP_KERNEL);
3337 if (!auth_data)
3338 return -ENOMEM;
3339
3340 auth_data->bss = req->bss;
3341
3342 if (req->ie && req->ie_len) {
3343 memcpy(auth_data->ie, req->ie, req->ie_len);
3344 auth_data->ie_len = req->ie_len;
3345 }
3346
3347 if (req->key && req->key_len) {
3348 auth_data->key_len = req->key_len;
3349 auth_data->key_idx = req->key_idx;
3350 memcpy(auth_data->key, req->key, req->key_len);
3351 }
3352
3353 auth_data->algorithm = auth_alg;
3354
3355 /* try to authenticate/probe */
3356
3357 mutex_lock(&ifmgd->mtx);
3358
3359 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3360 ifmgd->assoc_data) {
3361 err = -EBUSY;
3362 goto err_free;
3363 }
3364
3365 if (ifmgd->auth_data)
3366 ieee80211_destroy_auth_data(sdata, false);
3367
3368 /* prep auth_data so we don't go into idle on disassoc */
3369 ifmgd->auth_data = auth_data;
3370
3371 if (ifmgd->associated)
3372 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3373
3374 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
3375
3376 err = ieee80211_prep_connection(sdata, req->bss, false);
3377 if (err)
3378 goto err_clear;
3379
3380 err = ieee80211_probe_auth(sdata);
3381 if (err) {
3382 sta_info_destroy_addr(sdata, req->bss->bssid);
3383 goto err_clear;
3384 }
3385
3386 /* hold our own reference */
3387 cfg80211_ref_bss(auth_data->bss);
3388 err = 0;
3389 goto out_unlock;
3390
3391 err_clear:
3392 memset(ifmgd->bssid, 0, ETH_ALEN);
3393 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
3394 ifmgd->auth_data = NULL;
3395 err_free:
3396 kfree(auth_data);
3397 out_unlock:
3398 mutex_unlock(&ifmgd->mtx);
3399
3400 return err;
3401 }
3402
3403 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3404 struct cfg80211_assoc_request *req)
3405 {
3406 struct ieee80211_local *local = sdata->local;
3407 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3408 struct ieee80211_bss *bss = (void *)req->bss->priv;
3409 struct ieee80211_mgd_assoc_data *assoc_data;
3410 struct ieee80211_supported_band *sband;
3411 const u8 *ssidie, *ht_ie;
3412 int i, err;
3413
3414 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3415 if (!ssidie)
3416 return -EINVAL;
3417
3418 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3419 if (!assoc_data)
3420 return -ENOMEM;
3421
3422 mutex_lock(&ifmgd->mtx);
3423
3424 if (ifmgd->associated)
3425 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3426
3427 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3428 err = -EBUSY;
3429 goto err_free;
3430 }
3431
3432 if (ifmgd->assoc_data) {
3433 err = -EBUSY;
3434 goto err_free;
3435 }
3436
3437 if (ifmgd->auth_data) {
3438 bool match;
3439
3440 /* keep sta info, bssid if matching */
3441 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
3442 ieee80211_destroy_auth_data(sdata, match);
3443 }
3444
3445 /* prepare assoc data */
3446
3447 /*
3448 * keep only the 40 MHz disable bit set as it might have
3449 * been set during authentication already, all other bits
3450 * should be reset for a new connection
3451 */
3452 ifmgd->flags &= IEEE80211_STA_DISABLE_40MHZ;
3453
3454 ifmgd->beacon_crc_valid = false;
3455
3456 /*
3457 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3458 * We still associate in non-HT mode (11a/b/g) if any one of these
3459 * ciphers is configured as pairwise.
3460 * We can set this to true for non-11n hardware, that'll be checked
3461 * separately along with the peer capabilities.
3462 */
3463 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
3464 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3465 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
3466 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
3467 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
3468 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3469 netdev_info(sdata->dev,
3470 "disabling HT/VHT due to WEP/TKIP use\n");
3471 }
3472 }
3473
3474 if (req->flags & ASSOC_REQ_DISABLE_HT) {
3475 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
3476 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3477 }
3478
3479 /* Also disable HT if we don't support it or the AP doesn't use WMM */
3480 sband = local->hw.wiphy->bands[req->bss->channel->band];
3481 if (!sband->ht_cap.ht_supported ||
3482 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3483 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
3484 if (!bss->wmm_used)
3485 netdev_info(sdata->dev,
3486 "disabling HT as WMM/QoS is not supported by the AP\n");
3487 }
3488
3489 /* disable VHT if we don't support it or the AP doesn't use WMM */
3490 if (!sband->vht_cap.vht_supported ||
3491 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3492 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3493 if (!bss->wmm_used)
3494 netdev_info(sdata->dev,
3495 "disabling VHT as WMM/QoS is not supported by the AP\n");
3496 }
3497
3498 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
3499 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
3500 sizeof(ifmgd->ht_capa_mask));
3501
3502 if (req->ie && req->ie_len) {
3503 memcpy(assoc_data->ie, req->ie, req->ie_len);
3504 assoc_data->ie_len = req->ie_len;
3505 }
3506
3507 assoc_data->bss = req->bss;
3508
3509 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
3510 if (ifmgd->powersave)
3511 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
3512 else
3513 sdata->smps_mode = IEEE80211_SMPS_OFF;
3514 } else
3515 sdata->smps_mode = ifmgd->req_smps;
3516
3517 assoc_data->capability = req->bss->capability;
3518 assoc_data->wmm = bss->wmm_used &&
3519 (local->hw.queues >= IEEE80211_NUM_ACS);
3520 assoc_data->supp_rates = bss->supp_rates;
3521 assoc_data->supp_rates_len = bss->supp_rates_len;
3522
3523 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
3524 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
3525 assoc_data->ap_ht_param =
3526 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
3527 else
3528 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
3529
3530 if (bss->wmm_used && bss->uapsd_supported &&
3531 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
3532 assoc_data->uapsd = true;
3533 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
3534 } else {
3535 assoc_data->uapsd = false;
3536 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
3537 }
3538
3539 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3540 assoc_data->ssid_len = ssidie[1];
3541
3542 if (req->prev_bssid)
3543 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
3544
3545 if (req->use_mfp) {
3546 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
3547 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
3548 } else {
3549 ifmgd->mfp = IEEE80211_MFP_DISABLED;
3550 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
3551 }
3552
3553 if (req->crypto.control_port)
3554 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
3555 else
3556 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
3557
3558 sdata->control_port_protocol = req->crypto.control_port_ethertype;
3559 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
3560
3561 /* kick off associate process */
3562
3563 ifmgd->assoc_data = assoc_data;
3564
3565 err = ieee80211_prep_connection(sdata, req->bss, true);
3566 if (err)
3567 goto err_clear;
3568
3569 if (!bss->dtim_period &&
3570 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD) {
3571 /*
3572 * Wait up to one beacon interval ...
3573 * should this be more if we miss one?
3574 */
3575 sdata_info(sdata, "waiting for beacon from %pM\n",
3576 ifmgd->bssid);
3577 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
3578 } else {
3579 assoc_data->have_beacon = true;
3580 assoc_data->sent_assoc = false;
3581 assoc_data->timeout = jiffies;
3582 }
3583 run_again(ifmgd, assoc_data->timeout);
3584
3585 if (bss->corrupt_data) {
3586 char *corrupt_type = "data";
3587 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
3588 if (bss->corrupt_data &
3589 IEEE80211_BSS_CORRUPT_PROBE_RESP)
3590 corrupt_type = "beacon and probe response";
3591 else
3592 corrupt_type = "beacon";
3593 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
3594 corrupt_type = "probe response";
3595 sdata_info(sdata, "associating with AP with corrupt %s\n",
3596 corrupt_type);
3597 }
3598
3599 err = 0;
3600 goto out;
3601 err_clear:
3602 memset(ifmgd->bssid, 0, ETH_ALEN);
3603 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
3604 ifmgd->assoc_data = NULL;
3605 err_free:
3606 kfree(assoc_data);
3607 out:
3608 mutex_unlock(&ifmgd->mtx);
3609
3610 return err;
3611 }
3612
3613 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
3614 struct cfg80211_deauth_request *req)
3615 {
3616 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3617 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3618
3619 mutex_lock(&ifmgd->mtx);
3620
3621 if (ifmgd->auth_data) {
3622 ieee80211_destroy_auth_data(sdata, false);
3623 mutex_unlock(&ifmgd->mtx);
3624 return 0;
3625 }
3626
3627 sdata_info(sdata,
3628 "deauthenticating from %pM by local choice (reason=%d)\n",
3629 req->bssid, req->reason_code);
3630
3631 if (ifmgd->associated &&
3632 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
3633 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3634 req->reason_code, true, frame_buf);
3635 } else {
3636 drv_mgd_prepare_tx(sdata->local, sdata);
3637 ieee80211_send_deauth_disassoc(sdata, req->bssid,
3638 IEEE80211_STYPE_DEAUTH,
3639 req->reason_code, true,
3640 frame_buf);
3641 }
3642
3643 mutex_unlock(&ifmgd->mtx);
3644
3645 __cfg80211_send_deauth(sdata->dev, frame_buf,
3646 IEEE80211_DEAUTH_FRAME_LEN);
3647
3648 mutex_lock(&sdata->local->mtx);
3649 ieee80211_recalc_idle(sdata->local);
3650 mutex_unlock(&sdata->local->mtx);
3651
3652 return 0;
3653 }
3654
3655 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
3656 struct cfg80211_disassoc_request *req)
3657 {
3658 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3659 u8 bssid[ETH_ALEN];
3660 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3661
3662 mutex_lock(&ifmgd->mtx);
3663
3664 /*
3665 * cfg80211 should catch this ... but it's racy since
3666 * we can receive a disassoc frame, process it, hand it
3667 * to cfg80211 while that's in a locked section already
3668 * trying to tell us that the user wants to disconnect.
3669 */
3670 if (ifmgd->associated != req->bss) {
3671 mutex_unlock(&ifmgd->mtx);
3672 return -ENOLINK;
3673 }
3674
3675 sdata_info(sdata,
3676 "disassociating from %pM by local choice (reason=%d)\n",
3677 req->bss->bssid, req->reason_code);
3678
3679 memcpy(bssid, req->bss->bssid, ETH_ALEN);
3680 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
3681 req->reason_code, !req->local_state_change,
3682 frame_buf);
3683 mutex_unlock(&ifmgd->mtx);
3684
3685 __cfg80211_send_disassoc(sdata->dev, frame_buf,
3686 IEEE80211_DEAUTH_FRAME_LEN);
3687
3688 mutex_lock(&sdata->local->mtx);
3689 ieee80211_recalc_idle(sdata->local);
3690 mutex_unlock(&sdata->local->mtx);
3691
3692 return 0;
3693 }
3694
3695 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
3696 {
3697 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3698
3699 mutex_lock(&ifmgd->mtx);
3700 if (ifmgd->assoc_data)
3701 ieee80211_destroy_assoc_data(sdata, false);
3702 if (ifmgd->auth_data)
3703 ieee80211_destroy_auth_data(sdata, false);
3704 del_timer_sync(&ifmgd->timer);
3705 mutex_unlock(&ifmgd->mtx);
3706 }
3707
3708 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
3709 enum nl80211_cqm_rssi_threshold_event rssi_event,
3710 gfp_t gfp)
3711 {
3712 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3713
3714 trace_api_cqm_rssi_notify(sdata, rssi_event);
3715
3716 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
3717 }
3718 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);