blob: ccaf2389ccc1dcd68520095f3e075db50496186f [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * BSS client mode implementation
4 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
5 * Copyright 2004, Instant802 Networks, Inc.
6 * Copyright 2005, Devicescape Software, Inc.
7 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
8 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
9 * Copyright 2013-2014 Intel Mobile Communications GmbH
10 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
Olivier Deprez0e641232021-09-23 10:07:05 +020011 * Copyright (C) 2018 - 2020 Intel Corporation
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012 */
13
14#include <linux/delay.h>
David Brazdil0f672f62019-12-10 10:32:29 +000015#include <linux/fips.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016#include <linux/if_ether.h>
17#include <linux/skbuff.h>
18#include <linux/if_arp.h>
19#include <linux/etherdevice.h>
20#include <linux/moduleparam.h>
21#include <linux/rtnetlink.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#include "fils_aead.h"
33
34#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
35#define IEEE80211_AUTH_TIMEOUT_LONG (HZ / 2)
36#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
37#define IEEE80211_AUTH_TIMEOUT_SAE (HZ * 2)
38#define IEEE80211_AUTH_MAX_TRIES 3
39#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
40#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
41#define IEEE80211_ASSOC_TIMEOUT_LONG (HZ / 2)
42#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
43#define IEEE80211_ASSOC_MAX_TRIES 3
44
45static int max_nullfunc_tries = 2;
46module_param(max_nullfunc_tries, int, 0644);
47MODULE_PARM_DESC(max_nullfunc_tries,
48 "Maximum nullfunc tx tries before disconnecting (reason 4).");
49
50static int max_probe_tries = 5;
51module_param(max_probe_tries, int, 0644);
52MODULE_PARM_DESC(max_probe_tries,
53 "Maximum probe tries before disconnecting (reason 4).");
54
55/*
56 * Beacon loss timeout is calculated as N frames times the
57 * advertised beacon interval. This may need to be somewhat
58 * higher than what hardware might detect to account for
59 * delays in the host processing frames. But since we also
60 * probe on beacon miss before declaring the connection lost
61 * default to what we want.
62 */
63static int beacon_loss_count = 7;
64module_param(beacon_loss_count, int, 0644);
65MODULE_PARM_DESC(beacon_loss_count,
66 "Number of beacon intervals before we decide beacon was lost.");
67
68/*
69 * Time the connection can be idle before we probe
70 * it to see if we can still talk to the AP.
71 */
72#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
73/*
74 * Time we wait for a probe response after sending
75 * a probe request because of beacon loss or for
76 * checking the connection still works.
77 */
78static int probe_wait_ms = 500;
79module_param(probe_wait_ms, int, 0644);
80MODULE_PARM_DESC(probe_wait_ms,
81 "Maximum time(ms) to wait for probe response"
82 " before disconnecting (reason 4).");
83
84/*
85 * How many Beacon frames need to have been used in average signal strength
86 * before starting to indicate signal change events.
87 */
88#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
89
90/*
91 * We can have multiple work items (and connection probing)
92 * scheduling this timer, but we need to take care to only
93 * reschedule it when it should fire _earlier_ than it was
94 * asked for before, or if it's not pending right now. This
95 * function ensures that. Note that it then is required to
96 * run this function for all timeouts after the first one
97 * has happened -- the work that runs from this timer will
98 * do that.
99 */
100static void run_again(struct ieee80211_sub_if_data *sdata,
101 unsigned long timeout)
102{
103 sdata_assert_lock(sdata);
104
105 if (!timer_pending(&sdata->u.mgd.timer) ||
106 time_before(timeout, sdata->u.mgd.timer.expires))
107 mod_timer(&sdata->u.mgd.timer, timeout);
108}
109
110void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
111{
112 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
113 return;
114
115 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
116 return;
117
118 mod_timer(&sdata->u.mgd.bcn_mon_timer,
119 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
120}
121
122void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
123{
124 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
125
126 if (unlikely(!ifmgd->associated))
127 return;
128
129 if (ifmgd->probe_send_count)
130 ifmgd->probe_send_count = 0;
131
132 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
133 return;
134
135 mod_timer(&ifmgd->conn_mon_timer,
136 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
137}
138
139static int ecw2cw(int ecw)
140{
141 return (1 << ecw) - 1;
142}
143
144static u32
145ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
146 struct ieee80211_supported_band *sband,
147 struct ieee80211_channel *channel,
148 const struct ieee80211_ht_operation *ht_oper,
149 const struct ieee80211_vht_operation *vht_oper,
150 const struct ieee80211_he_operation *he_oper,
151 struct cfg80211_chan_def *chandef, bool tracking)
152{
153 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
154 struct cfg80211_chan_def vht_chandef;
155 struct ieee80211_sta_ht_cap sta_ht_cap;
156 u32 ht_cfreq, ret;
157
158 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
159 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
160
David Brazdil0f672f62019-12-10 10:32:29 +0000161 memset(chandef, 0, sizeof(struct cfg80211_chan_def));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000162 chandef->chan = channel;
163 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
164 chandef->center_freq1 = channel->center_freq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000165
166 if (!ht_oper || !sta_ht_cap.ht_supported) {
167 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
168 goto out;
169 }
170
171 chandef->width = NL80211_CHAN_WIDTH_20;
172
173 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
174 channel->band);
175 /* check that channel matches the right operating channel */
176 if (!tracking && channel->center_freq != ht_cfreq) {
177 /*
178 * It's possible that some APs are confused here;
179 * Netgear WNDR3700 sometimes reports 4 higher than
180 * the actual channel in association responses, but
181 * since we look at probe response/beacon data here
182 * it should be OK.
183 */
184 sdata_info(sdata,
185 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
186 channel->center_freq, ht_cfreq,
187 ht_oper->primary_chan, channel->band);
188 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
189 goto out;
190 }
191
192 /* check 40 MHz support, if we have it */
193 if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
194 ieee80211_chandef_ht_oper(ht_oper, chandef);
195 } else {
196 /* 40 MHz (and 80 MHz) must be supported for VHT */
197 ret = IEEE80211_STA_DISABLE_VHT;
198 /* also mark 40 MHz disabled */
199 ret |= IEEE80211_STA_DISABLE_40MHZ;
200 goto out;
201 }
202
203 if (!vht_oper || !sband->vht_cap.vht_supported) {
204 ret = IEEE80211_STA_DISABLE_VHT;
205 goto out;
206 }
207
208 vht_chandef = *chandef;
209 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && he_oper &&
210 (le32_to_cpu(he_oper->he_oper_params) &
211 IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
212 struct ieee80211_vht_operation he_oper_vht_cap;
213
214 /*
215 * Set only first 3 bytes (other 2 aren't used in
216 * ieee80211_chandef_vht_oper() anyway)
217 */
218 memcpy(&he_oper_vht_cap, he_oper->optional, 3);
219 he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
220
David Brazdil0f672f62019-12-10 10:32:29 +0000221 if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
222 &he_oper_vht_cap, ht_oper,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000223 &vht_chandef)) {
224 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
225 sdata_info(sdata,
226 "HE AP VHT information is invalid, disable HE\n");
227 ret = IEEE80211_STA_DISABLE_HE;
228 goto out;
229 }
David Brazdil0f672f62019-12-10 10:32:29 +0000230 } else if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_oper,
231 ht_oper, &vht_chandef)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
233 sdata_info(sdata,
234 "AP VHT information is invalid, disable VHT\n");
235 ret = IEEE80211_STA_DISABLE_VHT;
236 goto out;
237 }
238
239 if (!cfg80211_chandef_valid(&vht_chandef)) {
240 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
241 sdata_info(sdata,
242 "AP VHT information is invalid, disable VHT\n");
243 ret = IEEE80211_STA_DISABLE_VHT;
244 goto out;
245 }
246
247 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
248 ret = 0;
249 goto out;
250 }
251
252 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
253 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
254 sdata_info(sdata,
255 "AP VHT information doesn't match HT, disable VHT\n");
256 ret = IEEE80211_STA_DISABLE_VHT;
257 goto out;
258 }
259
260 *chandef = vht_chandef;
261
262 ret = 0;
263
264out:
265 /*
266 * When tracking the current AP, don't do any further checks if the
267 * new chandef is identical to the one we're currently using for the
268 * connection. This keeps us from playing ping-pong with regulatory,
269 * without it the following can happen (for example):
270 * - connect to an AP with 80 MHz, world regdom allows 80 MHz
271 * - AP advertises regdom US
272 * - CRDA loads regdom US with 80 MHz prohibited (old database)
273 * - the code below detects an unsupported channel, downgrades, and
274 * we disconnect from the AP in the caller
275 * - disconnect causes CRDA to reload world regdomain and the game
276 * starts anew.
277 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
278 *
279 * It seems possible that there are still scenarios with CSA or real
280 * bandwidth changes where a this could happen, but those cases are
281 * less common and wouldn't completely prevent using the AP.
282 */
283 if (tracking &&
284 cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
285 return ret;
286
287 /* don't print the message below for VHT mismatch if VHT is disabled */
288 if (ret & IEEE80211_STA_DISABLE_VHT)
289 vht_chandef = *chandef;
290
291 /*
292 * Ignore the DISABLED flag when we're already connected and only
293 * tracking the APs beacon for bandwidth changes - otherwise we
294 * might get disconnected here if we connect to an AP, update our
295 * regulatory information based on the AP's country IE and the
296 * information we have is wrong/outdated and disables the channel
297 * that we're actually using for the connection to the AP.
298 */
299 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
300 tracking ? 0 :
301 IEEE80211_CHAN_DISABLED)) {
302 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
303 ret = IEEE80211_STA_DISABLE_HT |
304 IEEE80211_STA_DISABLE_VHT;
305 break;
306 }
307
308 ret |= ieee80211_chandef_downgrade(chandef);
309 }
310
311 if (chandef->width != vht_chandef.width && !tracking)
312 sdata_info(sdata,
313 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
314
315 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
316 return ret;
317}
318
319static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
320 struct sta_info *sta,
321 const struct ieee80211_ht_cap *ht_cap,
322 const struct ieee80211_ht_operation *ht_oper,
323 const struct ieee80211_vht_operation *vht_oper,
324 const struct ieee80211_he_operation *he_oper,
325 const u8 *bssid, u32 *changed)
326{
327 struct ieee80211_local *local = sdata->local;
328 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
329 struct ieee80211_channel *chan = sdata->vif.bss_conf.chandef.chan;
330 struct ieee80211_supported_band *sband =
331 local->hw.wiphy->bands[chan->band];
332 struct cfg80211_chan_def chandef;
333 u16 ht_opmode;
334 u32 flags;
335 enum ieee80211_sta_rx_bandwidth new_sta_bw;
336 int ret;
337
338 /* if HT was/is disabled, don't track any bandwidth changes */
339 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
340 return 0;
341
342 /* don't check VHT if we associated as non-VHT station */
343 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
344 vht_oper = NULL;
345
346 /* don't check HE if we associated as non-HE station */
347 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE ||
348 !ieee80211_get_he_sta_cap(sband))
349 he_oper = NULL;
350
351 if (WARN_ON_ONCE(!sta))
352 return -EINVAL;
353
354 /*
355 * if bss configuration changed store the new one -
356 * this may be applicable even if channel is identical
357 */
358 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
359 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
360 *changed |= BSS_CHANGED_HT;
361 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
362 }
363
364 /* calculate new channel (type) based on HT/VHT/HE operation IEs */
365 flags = ieee80211_determine_chantype(sdata, sband, chan,
366 ht_oper, vht_oper, he_oper,
367 &chandef, true);
368
369 /*
370 * Downgrade the new channel if we associated with restricted
371 * capabilities. For example, if we associated as a 20 MHz STA
372 * to a 40 MHz AP (due to regulatory, capabilities or config
373 * reasons) then switching to a 40 MHz channel now won't do us
374 * any good -- we couldn't use it with the AP.
375 */
376 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
377 chandef.width == NL80211_CHAN_WIDTH_80P80)
378 flags |= ieee80211_chandef_downgrade(&chandef);
379 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
380 chandef.width == NL80211_CHAN_WIDTH_160)
381 flags |= ieee80211_chandef_downgrade(&chandef);
382 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
383 chandef.width > NL80211_CHAN_WIDTH_20)
384 flags |= ieee80211_chandef_downgrade(&chandef);
385
386 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
387 return 0;
388
389 sdata_info(sdata,
390 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
391 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
392 chandef.center_freq1, chandef.center_freq2);
393
394 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
395 IEEE80211_STA_DISABLE_VHT |
396 IEEE80211_STA_DISABLE_40MHZ |
397 IEEE80211_STA_DISABLE_80P80MHZ |
398 IEEE80211_STA_DISABLE_160MHZ)) ||
399 !cfg80211_chandef_valid(&chandef)) {
400 sdata_info(sdata,
401 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
402 ifmgd->bssid);
403 return -EINVAL;
404 }
405
406 switch (chandef.width) {
407 case NL80211_CHAN_WIDTH_20_NOHT:
408 case NL80211_CHAN_WIDTH_20:
409 new_sta_bw = IEEE80211_STA_RX_BW_20;
410 break;
411 case NL80211_CHAN_WIDTH_40:
412 new_sta_bw = IEEE80211_STA_RX_BW_40;
413 break;
414 case NL80211_CHAN_WIDTH_80:
415 new_sta_bw = IEEE80211_STA_RX_BW_80;
416 break;
417 case NL80211_CHAN_WIDTH_80P80:
418 case NL80211_CHAN_WIDTH_160:
419 new_sta_bw = IEEE80211_STA_RX_BW_160;
420 break;
421 default:
422 return -EINVAL;
423 }
424
425 if (new_sta_bw > sta->cur_max_bandwidth)
426 new_sta_bw = sta->cur_max_bandwidth;
427
428 if (new_sta_bw < sta->sta.bandwidth) {
429 sta->sta.bandwidth = new_sta_bw;
430 rate_control_rate_update(local, sband, sta,
431 IEEE80211_RC_BW_CHANGED);
432 }
433
434 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
435 if (ret) {
436 sdata_info(sdata,
437 "AP %pM changed bandwidth to incompatible one - disconnect\n",
438 ifmgd->bssid);
439 return ret;
440 }
441
442 if (new_sta_bw > sta->sta.bandwidth) {
443 sta->sta.bandwidth = new_sta_bw;
444 rate_control_rate_update(local, sband, sta,
445 IEEE80211_RC_BW_CHANGED);
446 }
447
448 return 0;
449}
450
451/* frame sending functions */
452
453static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
454 struct sk_buff *skb, u8 ap_ht_param,
455 struct ieee80211_supported_band *sband,
456 struct ieee80211_channel *channel,
457 enum ieee80211_smps_mode smps)
458{
459 u8 *pos;
460 u32 flags = channel->flags;
461 u16 cap;
462 struct ieee80211_sta_ht_cap ht_cap;
463
464 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
465
466 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
467 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
468
469 /* determine capability flags */
470 cap = ht_cap.cap;
471
472 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
473 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
474 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
475 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
476 cap &= ~IEEE80211_HT_CAP_SGI_40;
477 }
478 break;
479 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
480 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
481 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
482 cap &= ~IEEE80211_HT_CAP_SGI_40;
483 }
484 break;
485 }
486
487 /*
488 * If 40 MHz was disabled associate as though we weren't
489 * capable of 40 MHz -- some broken APs will never fall
490 * back to trying to transmit in 20 MHz.
491 */
492 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
493 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
494 cap &= ~IEEE80211_HT_CAP_SGI_40;
495 }
496
497 /* set SM PS mode properly */
498 cap &= ~IEEE80211_HT_CAP_SM_PS;
499 switch (smps) {
500 case IEEE80211_SMPS_AUTOMATIC:
501 case IEEE80211_SMPS_NUM_MODES:
502 WARN_ON(1);
503 /* fall through */
504 case IEEE80211_SMPS_OFF:
505 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
506 IEEE80211_HT_CAP_SM_PS_SHIFT;
507 break;
508 case IEEE80211_SMPS_STATIC:
509 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
510 IEEE80211_HT_CAP_SM_PS_SHIFT;
511 break;
512 case IEEE80211_SMPS_DYNAMIC:
513 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
514 IEEE80211_HT_CAP_SM_PS_SHIFT;
515 break;
516 }
517
518 /* reserve and fill IE */
519 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
520 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
521}
522
523/* This function determines vht capability flags for the association
524 * and builds the IE.
525 * Note - the function may set the owner of the MU-MIMO capability
526 */
527static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
528 struct sk_buff *skb,
529 struct ieee80211_supported_band *sband,
530 struct ieee80211_vht_cap *ap_vht_cap)
531{
532 struct ieee80211_local *local = sdata->local;
533 u8 *pos;
534 u32 cap;
535 struct ieee80211_sta_vht_cap vht_cap;
536 u32 mask, ap_bf_sts, our_bf_sts;
537
538 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
539
540 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
541 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
542
543 /* determine capability flags */
544 cap = vht_cap.cap;
545
546 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
547 u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
548
549 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
550 if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
551 bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
552 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
553 }
554
555 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
556 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
557 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
558 }
559
560 /*
561 * Some APs apparently get confused if our capabilities are better
562 * than theirs, so restrict what we advertise in the assoc request.
563 */
564 if (!(ap_vht_cap->vht_cap_info &
565 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
566 cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
567 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
568 else if (!(ap_vht_cap->vht_cap_info &
569 cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
570 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
571
572 /*
573 * If some other vif is using the MU-MIMO capablity we cannot associate
574 * using MU-MIMO - this will lead to contradictions in the group-id
575 * mechanism.
576 * Ownership is defined since association request, in order to avoid
577 * simultaneous associations with MU-MIMO.
578 */
579 if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
580 bool disable_mu_mimo = false;
581 struct ieee80211_sub_if_data *other;
582
583 list_for_each_entry_rcu(other, &local->interfaces, list) {
584 if (other->vif.mu_mimo_owner) {
585 disable_mu_mimo = true;
586 break;
587 }
588 }
589 if (disable_mu_mimo)
590 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
591 else
592 sdata->vif.mu_mimo_owner = true;
593 }
594
595 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
596
597 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
598 our_bf_sts = cap & mask;
599
600 if (ap_bf_sts < our_bf_sts) {
601 cap &= ~mask;
602 cap |= ap_bf_sts;
603 }
604
605 /* reserve and fill IE */
606 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
607 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
608}
609
610/* This function determines HE capability flags for the association
611 * and builds the IE.
612 */
613static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata,
614 struct sk_buff *skb,
615 struct ieee80211_supported_band *sband)
616{
617 u8 *pos;
618 const struct ieee80211_sta_he_cap *he_cap = NULL;
619 u8 he_cap_size;
620
621 he_cap = ieee80211_get_he_sta_cap(sband);
622 if (!he_cap)
623 return;
624
625 /*
626 * TODO: the 1 added is because this temporarily is under the EXTENSION
627 * IE. Get rid of it when it moves.
628 */
629 he_cap_size =
630 2 + 1 + sizeof(he_cap->he_cap_elem) +
631 ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
632 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
633 he_cap->he_cap_elem.phy_cap_info);
634 pos = skb_put(skb, he_cap_size);
635 ieee80211_ie_build_he_cap(pos, he_cap, pos + he_cap_size);
636}
637
638static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
639{
640 struct ieee80211_local *local = sdata->local;
641 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
642 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
643 struct sk_buff *skb;
644 struct ieee80211_mgmt *mgmt;
David Brazdil0f672f62019-12-10 10:32:29 +0000645 u8 *pos, qos_info, *ie_start;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000646 size_t offset = 0, noffset;
647 int i, count, rates_len, supp_rates_len, shift;
648 u16 capab;
649 struct ieee80211_supported_band *sband;
650 struct ieee80211_chanctx_conf *chanctx_conf;
651 struct ieee80211_channel *chan;
652 u32 rates = 0;
653
654 sdata_assert_lock(sdata);
655
656 rcu_read_lock();
657 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
658 if (WARN_ON(!chanctx_conf)) {
659 rcu_read_unlock();
660 return;
661 }
662 chan = chanctx_conf->def.chan;
663 rcu_read_unlock();
664 sband = local->hw.wiphy->bands[chan->band];
665 shift = ieee80211_vif_get_shift(&sdata->vif);
666
667 if (assoc_data->supp_rates_len) {
668 /*
669 * Get all rates supported by the device and the AP as
670 * some APs don't like getting a superset of their rates
671 * in the association request (e.g. D-Link DAP 1353 in
672 * b-only mode)...
673 */
674 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
675 assoc_data->supp_rates,
676 assoc_data->supp_rates_len,
677 &rates);
678 } else {
679 /*
680 * In case AP not provide any supported rates information
681 * before association, we send information element(s) with
682 * all rates that we support.
683 */
684 rates_len = 0;
685 for (i = 0; i < sband->n_bitrates; i++) {
686 rates |= BIT(i);
687 rates_len++;
688 }
689 }
690
691 skb = alloc_skb(local->hw.extra_tx_headroom +
692 sizeof(*mgmt) + /* bit too much but doesn't matter */
693 2 + assoc_data->ssid_len + /* SSID */
694 4 + rates_len + /* (extended) rates */
695 4 + /* power capability */
696 2 + 2 * sband->n_channels + /* supported channels */
697 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
698 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
699 2 + 1 + sizeof(struct ieee80211_he_cap_elem) + /* HE */
700 sizeof(struct ieee80211_he_mcs_nss_supp) +
701 IEEE80211_HE_PPE_THRES_MAX_LEN +
702 assoc_data->ie_len + /* extra IEs */
703 (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
704 9, /* WMM */
705 GFP_KERNEL);
706 if (!skb)
707 return;
708
709 skb_reserve(skb, local->hw.extra_tx_headroom);
710
711 capab = WLAN_CAPABILITY_ESS;
712
713 if (sband->band == NL80211_BAND_2GHZ) {
714 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
715 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
716 }
717
718 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
719 capab |= WLAN_CAPABILITY_PRIVACY;
720
721 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
722 ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
723 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
724
725 if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
726 capab |= WLAN_CAPABILITY_RADIO_MEASURE;
727
728 mgmt = skb_put_zero(skb, 24);
729 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
730 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
731 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
732
733 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
734 skb_put(skb, 10);
735 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
736 IEEE80211_STYPE_REASSOC_REQ);
737 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
738 mgmt->u.reassoc_req.listen_interval =
739 cpu_to_le16(local->hw.conf.listen_interval);
740 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
741 ETH_ALEN);
742 } else {
743 skb_put(skb, 4);
744 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
745 IEEE80211_STYPE_ASSOC_REQ);
746 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
747 mgmt->u.assoc_req.listen_interval =
748 cpu_to_le16(local->hw.conf.listen_interval);
749 }
750
751 /* SSID */
752 pos = skb_put(skb, 2 + assoc_data->ssid_len);
David Brazdil0f672f62019-12-10 10:32:29 +0000753 ie_start = pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000754 *pos++ = WLAN_EID_SSID;
755 *pos++ = assoc_data->ssid_len;
756 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
757
758 /* add all rates which were marked to be used above */
759 supp_rates_len = rates_len;
760 if (supp_rates_len > 8)
761 supp_rates_len = 8;
762
763 pos = skb_put(skb, supp_rates_len + 2);
764 *pos++ = WLAN_EID_SUPP_RATES;
765 *pos++ = supp_rates_len;
766
767 count = 0;
768 for (i = 0; i < sband->n_bitrates; i++) {
769 if (BIT(i) & rates) {
770 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
771 5 * (1 << shift));
772 *pos++ = (u8) rate;
773 if (++count == 8)
774 break;
775 }
776 }
777
778 if (rates_len > count) {
779 pos = skb_put(skb, rates_len - count + 2);
780 *pos++ = WLAN_EID_EXT_SUPP_RATES;
781 *pos++ = rates_len - count;
782
783 for (i++; i < sband->n_bitrates; i++) {
784 if (BIT(i) & rates) {
785 int rate;
786 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
787 5 * (1 << shift));
788 *pos++ = (u8) rate;
789 }
790 }
791 }
792
793 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
794 capab & WLAN_CAPABILITY_RADIO_MEASURE) {
795 pos = skb_put(skb, 4);
796 *pos++ = WLAN_EID_PWR_CAPABILITY;
797 *pos++ = 2;
798 *pos++ = 0; /* min tx power */
799 /* max tx power */
800 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
801 }
802
803 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
804 /* TODO: get this in reg domain format */
805 pos = skb_put(skb, 2 * sband->n_channels + 2);
806 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
807 *pos++ = 2 * sband->n_channels;
808 for (i = 0; i < sband->n_channels; i++) {
809 *pos++ = ieee80211_frequency_to_channel(
810 sband->channels[i].center_freq);
811 *pos++ = 1; /* one channel in the subband*/
812 }
813 }
814
David Brazdil0f672f62019-12-10 10:32:29 +0000815 /* Set MBSSID support for HE AP if needed */
816 if (ieee80211_hw_check(&local->hw, SUPPORTS_ONLY_HE_MULTI_BSSID) &&
817 !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && assoc_data->ie_len) {
818 struct element *elem;
819
820 /* we know it's writable, cast away the const */
821 elem = (void *)cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
822 assoc_data->ie,
823 assoc_data->ie_len);
824
825 /* We can probably assume both always true */
826 if (elem && elem->datalen >= 3)
827 elem->data[2] |= WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
828 }
829
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000830 /* if present, add any custom IEs that go before HT */
831 if (assoc_data->ie_len) {
832 static const u8 before_ht[] = {
833 WLAN_EID_SSID,
834 WLAN_EID_SUPP_RATES,
835 WLAN_EID_EXT_SUPP_RATES,
836 WLAN_EID_PWR_CAPABILITY,
837 WLAN_EID_SUPPORTED_CHANNELS,
838 WLAN_EID_RSN,
839 WLAN_EID_QOS_CAPA,
840 WLAN_EID_RRM_ENABLED_CAPABILITIES,
841 WLAN_EID_MOBILITY_DOMAIN,
842 WLAN_EID_FAST_BSS_TRANSITION, /* reassoc only */
843 WLAN_EID_RIC_DATA, /* reassoc only */
844 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
845 };
846 static const u8 after_ric[] = {
847 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
848 WLAN_EID_HT_CAPABILITY,
849 WLAN_EID_BSS_COEX_2040,
850 /* luckily this is almost always there */
851 WLAN_EID_EXT_CAPABILITY,
852 WLAN_EID_QOS_TRAFFIC_CAPA,
853 WLAN_EID_TIM_BCAST_REQ,
854 WLAN_EID_INTERWORKING,
855 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
856 WLAN_EID_VHT_CAPABILITY,
857 WLAN_EID_OPMODE_NOTIF,
858 };
859
860 noffset = ieee80211_ie_split_ric(assoc_data->ie,
861 assoc_data->ie_len,
862 before_ht,
863 ARRAY_SIZE(before_ht),
864 after_ric,
865 ARRAY_SIZE(after_ric),
866 offset);
867 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
868 offset = noffset;
869 }
870
871 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
872 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
873 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
874
875 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
876 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
877 sband, chan, sdata->smps_mode);
878
879 /* if present, add any custom IEs that go before VHT */
880 if (assoc_data->ie_len) {
881 static const u8 before_vht[] = {
882 /*
883 * no need to list the ones split off before HT
884 * or generated here
885 */
886 WLAN_EID_BSS_COEX_2040,
887 WLAN_EID_EXT_CAPABILITY,
888 WLAN_EID_QOS_TRAFFIC_CAPA,
889 WLAN_EID_TIM_BCAST_REQ,
890 WLAN_EID_INTERWORKING,
891 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
892 };
893
894 /* RIC already taken above, so no need to handle here anymore */
895 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
896 before_vht, ARRAY_SIZE(before_vht),
897 offset);
898 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
899 offset = noffset;
900 }
901
902 /* if present, add any custom IEs that go before HE */
903 if (assoc_data->ie_len) {
904 static const u8 before_he[] = {
905 /*
906 * no need to list the ones split off before VHT
907 * or generated here
908 */
909 WLAN_EID_OPMODE_NOTIF,
910 WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
911 /* 11ai elements */
912 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
913 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
914 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
915 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
916 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
917 /* TODO: add 11ah/11aj/11ak elements */
918 };
919
920 /* RIC already taken above, so no need to handle here anymore */
921 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
922 before_he, ARRAY_SIZE(before_he),
923 offset);
924 pos = skb_put(skb, noffset - offset);
925 memcpy(pos, assoc_data->ie + offset, noffset - offset);
926 offset = noffset;
927 }
928
929 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
930 ieee80211_add_vht_ie(sdata, skb, sband,
931 &assoc_data->ap_vht_cap);
932
David Brazdil0f672f62019-12-10 10:32:29 +0000933 /*
934 * If AP doesn't support HT, mark HE as disabled.
935 * If on the 5GHz band, make sure it supports VHT.
936 */
937 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT ||
938 (sband->band == NL80211_BAND_5GHZ &&
939 ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
940 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
941
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000942 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
943 ieee80211_add_he_ie(sdata, skb, sband);
944
945 /* if present, add any custom non-vendor IEs that go after HE */
946 if (assoc_data->ie_len) {
947 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
948 assoc_data->ie_len,
949 offset);
950 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
951 offset = noffset;
952 }
953
954 if (assoc_data->wmm) {
955 if (assoc_data->uapsd) {
956 qos_info = ifmgd->uapsd_queues;
957 qos_info |= (ifmgd->uapsd_max_sp_len <<
958 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
959 } else {
960 qos_info = 0;
961 }
962
963 pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
964 }
965
966 /* add any remaining custom (i.e. vendor specific here) IEs */
967 if (assoc_data->ie_len) {
968 noffset = assoc_data->ie_len;
969 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
970 }
971
972 if (assoc_data->fils_kek_len &&
973 fils_encrypt_assoc_req(skb, assoc_data) < 0) {
974 dev_kfree_skb(skb);
975 return;
976 }
977
David Brazdil0f672f62019-12-10 10:32:29 +0000978 pos = skb_tail_pointer(skb);
979 kfree(ifmgd->assoc_req_ies);
980 ifmgd->assoc_req_ies = kmemdup(ie_start, pos - ie_start, GFP_ATOMIC);
981 ifmgd->assoc_req_ies_len = pos - ie_start;
982
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000983 drv_mgd_prepare_tx(local, sdata, 0);
984
985 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
986 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
987 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
988 IEEE80211_TX_INTFL_MLME_CONN_TX;
989 ieee80211_tx_skb(sdata, skb);
990}
991
992void ieee80211_send_pspoll(struct ieee80211_local *local,
993 struct ieee80211_sub_if_data *sdata)
994{
995 struct ieee80211_pspoll *pspoll;
996 struct sk_buff *skb;
997
998 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
999 if (!skb)
1000 return;
1001
1002 pspoll = (struct ieee80211_pspoll *) skb->data;
1003 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1004
1005 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1006 ieee80211_tx_skb(sdata, skb);
1007}
1008
1009void ieee80211_send_nullfunc(struct ieee80211_local *local,
1010 struct ieee80211_sub_if_data *sdata,
1011 bool powersave)
1012{
1013 struct sk_buff *skb;
1014 struct ieee80211_hdr_3addr *nullfunc;
1015 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1016
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001017 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
1018 !ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
1019 if (!skb)
1020 return;
1021
1022 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1023 if (powersave)
1024 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1025
1026 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1027 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
1028
1029 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1030 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1031
1032 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
1033 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1034
1035 ieee80211_tx_skb(sdata, skb);
1036}
1037
1038static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1039 struct ieee80211_sub_if_data *sdata)
1040{
1041 struct sk_buff *skb;
1042 struct ieee80211_hdr *nullfunc;
1043 __le16 fc;
1044
1045 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1046 return;
1047
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001048 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
1049 if (!skb)
1050 return;
1051
1052 skb_reserve(skb, local->hw.extra_tx_headroom);
1053
1054 nullfunc = skb_put_zero(skb, 30);
1055 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1056 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1057 nullfunc->frame_control = fc;
1058 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
1059 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1060 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
1061 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1062
1063 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1064 ieee80211_tx_skb(sdata, skb);
1065}
1066
1067/* spectrum management related things */
1068static void ieee80211_chswitch_work(struct work_struct *work)
1069{
1070 struct ieee80211_sub_if_data *sdata =
1071 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
1072 struct ieee80211_local *local = sdata->local;
1073 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1074 int ret;
1075
1076 if (!ieee80211_sdata_running(sdata))
1077 return;
1078
1079 sdata_lock(sdata);
1080 mutex_lock(&local->mtx);
1081 mutex_lock(&local->chanctx_mtx);
1082
1083 if (!ifmgd->associated)
1084 goto out;
1085
1086 if (!sdata->vif.csa_active)
1087 goto out;
1088
1089 /*
1090 * using reservation isn't immediate as it may be deferred until later
1091 * with multi-vif. once reservation is complete it will re-schedule the
1092 * work with no reserved_chanctx so verify chandef to check if it
1093 * completed successfully
1094 */
1095
1096 if (sdata->reserved_chanctx) {
1097 struct ieee80211_supported_band *sband = NULL;
1098 struct sta_info *mgd_sta = NULL;
1099 enum ieee80211_sta_rx_bandwidth bw = IEEE80211_STA_RX_BW_20;
1100
1101 /*
1102 * with multi-vif csa driver may call ieee80211_csa_finish()
1103 * many times while waiting for other interfaces to use their
1104 * reservations
1105 */
1106 if (sdata->reserved_ready)
1107 goto out;
1108
1109 if (sdata->vif.bss_conf.chandef.width !=
1110 sdata->csa_chandef.width) {
1111 /*
1112 * For managed interface, we need to also update the AP
1113 * station bandwidth and align the rate scale algorithm
1114 * on the bandwidth change. Here we only consider the
1115 * bandwidth of the new channel definition (as channel
1116 * switch flow does not have the full HT/VHT/HE
1117 * information), assuming that if additional changes are
1118 * required they would be done as part of the processing
1119 * of the next beacon from the AP.
1120 */
1121 switch (sdata->csa_chandef.width) {
1122 case NL80211_CHAN_WIDTH_20_NOHT:
1123 case NL80211_CHAN_WIDTH_20:
1124 default:
1125 bw = IEEE80211_STA_RX_BW_20;
1126 break;
1127 case NL80211_CHAN_WIDTH_40:
1128 bw = IEEE80211_STA_RX_BW_40;
1129 break;
1130 case NL80211_CHAN_WIDTH_80:
1131 bw = IEEE80211_STA_RX_BW_80;
1132 break;
1133 case NL80211_CHAN_WIDTH_80P80:
1134 case NL80211_CHAN_WIDTH_160:
1135 bw = IEEE80211_STA_RX_BW_160;
1136 break;
1137 }
1138
1139 mgd_sta = sta_info_get(sdata, ifmgd->bssid);
1140 sband =
1141 local->hw.wiphy->bands[sdata->csa_chandef.chan->band];
1142 }
1143
1144 if (sdata->vif.bss_conf.chandef.width >
1145 sdata->csa_chandef.width) {
1146 mgd_sta->sta.bandwidth = bw;
1147 rate_control_rate_update(local, sband, mgd_sta,
1148 IEEE80211_RC_BW_CHANGED);
1149 }
1150
1151 ret = ieee80211_vif_use_reserved_context(sdata);
1152 if (ret) {
1153 sdata_info(sdata,
1154 "failed to use reserved channel context, disconnecting (err=%d)\n",
1155 ret);
1156 ieee80211_queue_work(&sdata->local->hw,
1157 &ifmgd->csa_connection_drop_work);
1158 goto out;
1159 }
1160
1161 if (sdata->vif.bss_conf.chandef.width <
1162 sdata->csa_chandef.width) {
1163 mgd_sta->sta.bandwidth = bw;
1164 rate_control_rate_update(local, sband, mgd_sta,
1165 IEEE80211_RC_BW_CHANGED);
1166 }
1167
1168 goto out;
1169 }
1170
1171 if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
1172 &sdata->csa_chandef)) {
1173 sdata_info(sdata,
1174 "failed to finalize channel switch, disconnecting\n");
1175 ieee80211_queue_work(&sdata->local->hw,
1176 &ifmgd->csa_connection_drop_work);
1177 goto out;
1178 }
1179
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001180 ifmgd->csa_waiting_bcn = true;
1181
1182 ieee80211_sta_reset_beacon_monitor(sdata);
1183 ieee80211_sta_reset_conn_monitor(sdata);
1184
1185out:
1186 mutex_unlock(&local->chanctx_mtx);
1187 mutex_unlock(&local->mtx);
1188 sdata_unlock(sdata);
1189}
1190
1191static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
1192{
1193 struct ieee80211_local *local = sdata->local;
1194 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1195 int ret;
1196
1197 sdata_assert_lock(sdata);
1198
1199 WARN_ON(!sdata->vif.csa_active);
1200
1201 if (sdata->csa_block_tx) {
1202 ieee80211_wake_vif_queues(local, sdata,
1203 IEEE80211_QUEUE_STOP_REASON_CSA);
1204 sdata->csa_block_tx = false;
1205 }
1206
1207 sdata->vif.csa_active = false;
1208 ifmgd->csa_waiting_bcn = false;
Olivier Deprez0e641232021-09-23 10:07:05 +02001209 /*
1210 * If the CSA IE is still present on the beacon after the switch,
1211 * we need to consider it as a new CSA (possibly to self).
1212 */
1213 ifmgd->beacon_crc_valid = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001214
1215 ret = drv_post_channel_switch(sdata);
1216 if (ret) {
1217 sdata_info(sdata,
1218 "driver post channel switch failed, disconnecting\n");
1219 ieee80211_queue_work(&local->hw,
1220 &ifmgd->csa_connection_drop_work);
1221 return;
1222 }
1223
1224 cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
1225}
1226
1227void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1228{
1229 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1230 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1231
1232 trace_api_chswitch_done(sdata, success);
1233 if (!success) {
1234 sdata_info(sdata,
1235 "driver channel switch failed, disconnecting\n");
1236 ieee80211_queue_work(&sdata->local->hw,
1237 &ifmgd->csa_connection_drop_work);
1238 } else {
1239 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
1240 }
1241}
1242EXPORT_SYMBOL(ieee80211_chswitch_done);
1243
1244static void ieee80211_chswitch_timer(struct timer_list *t)
1245{
1246 struct ieee80211_sub_if_data *sdata =
1247 from_timer(sdata, t, u.mgd.chswitch_timer);
1248
1249 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
1250}
1251
1252static void
David Brazdil0f672f62019-12-10 10:32:29 +00001253ieee80211_sta_abort_chanswitch(struct ieee80211_sub_if_data *sdata)
1254{
1255 struct ieee80211_local *local = sdata->local;
1256
1257 if (!local->ops->abort_channel_switch)
1258 return;
1259
1260 mutex_lock(&local->mtx);
1261
1262 mutex_lock(&local->chanctx_mtx);
1263 ieee80211_vif_unreserve_chanctx(sdata);
1264 mutex_unlock(&local->chanctx_mtx);
1265
1266 if (sdata->csa_block_tx)
1267 ieee80211_wake_vif_queues(local, sdata,
1268 IEEE80211_QUEUE_STOP_REASON_CSA);
1269
1270 sdata->csa_block_tx = false;
1271 sdata->vif.csa_active = false;
1272
1273 mutex_unlock(&local->mtx);
1274
1275 drv_abort_channel_switch(sdata);
1276}
1277
1278static void
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001279ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1280 u64 timestamp, u32 device_timestamp,
1281 struct ieee802_11_elems *elems,
1282 bool beacon)
1283{
1284 struct ieee80211_local *local = sdata->local;
1285 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1286 struct cfg80211_bss *cbss = ifmgd->associated;
1287 struct ieee80211_chanctx_conf *conf;
1288 struct ieee80211_chanctx *chanctx;
1289 enum nl80211_band current_band;
1290 struct ieee80211_csa_ie csa_ie;
1291 struct ieee80211_channel_switch ch_switch;
1292 int res;
1293
1294 sdata_assert_lock(sdata);
1295
1296 if (!cbss)
1297 return;
1298
1299 if (local->scanning)
1300 return;
1301
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001302 current_band = cbss->channel->band;
1303 res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
1304 ifmgd->flags,
1305 ifmgd->associated->bssid, &csa_ie);
David Brazdil0f672f62019-12-10 10:32:29 +00001306
1307 if (!res) {
1308 ch_switch.timestamp = timestamp;
1309 ch_switch.device_timestamp = device_timestamp;
Olivier Deprez0e641232021-09-23 10:07:05 +02001310 ch_switch.block_tx = csa_ie.mode;
David Brazdil0f672f62019-12-10 10:32:29 +00001311 ch_switch.chandef = csa_ie.chandef;
1312 ch_switch.count = csa_ie.count;
1313 ch_switch.delay = csa_ie.max_switch_time;
1314 }
1315
1316 if (res < 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001317 ieee80211_queue_work(&local->hw,
1318 &ifmgd->csa_connection_drop_work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001319 return;
David Brazdil0f672f62019-12-10 10:32:29 +00001320 }
1321
1322 if (beacon && sdata->vif.csa_active && !ifmgd->csa_waiting_bcn) {
1323 if (res)
1324 ieee80211_sta_abort_chanswitch(sdata);
1325 else
1326 drv_channel_switch_rx_beacon(sdata, &ch_switch);
1327 return;
1328 } else if (sdata->vif.csa_active || res) {
1329 /* disregard subsequent announcements if already processing */
1330 return;
1331 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001332
1333 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
1334 IEEE80211_CHAN_DISABLED)) {
1335 sdata_info(sdata,
1336 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1337 ifmgd->associated->bssid,
1338 csa_ie.chandef.chan->center_freq,
1339 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1340 csa_ie.chandef.center_freq2);
1341 ieee80211_queue_work(&local->hw,
1342 &ifmgd->csa_connection_drop_work);
1343 return;
1344 }
1345
1346 if (cfg80211_chandef_identical(&csa_ie.chandef,
David Brazdil0f672f62019-12-10 10:32:29 +00001347 &sdata->vif.bss_conf.chandef) &&
1348 (!csa_ie.mode || !beacon)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001349 if (ifmgd->csa_ignored_same_chan)
1350 return;
1351 sdata_info(sdata,
1352 "AP %pM tries to chanswitch to same channel, ignore\n",
1353 ifmgd->associated->bssid);
1354 ifmgd->csa_ignored_same_chan = true;
1355 return;
1356 }
1357
1358 /*
1359 * Drop all TDLS peers - either we disconnect or move to a different
1360 * channel from this point on. There's no telling what our peer will do.
1361 * The TDLS WIDER_BW scenario is also problematic, as peers might now
1362 * have an incompatible wider chandef.
1363 */
1364 ieee80211_teardown_tdls_peers(sdata);
1365
1366 mutex_lock(&local->mtx);
1367 mutex_lock(&local->chanctx_mtx);
1368 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1369 lockdep_is_held(&local->chanctx_mtx));
1370 if (!conf) {
1371 sdata_info(sdata,
1372 "no channel context assigned to vif?, disconnecting\n");
1373 goto drop_connection;
1374 }
1375
1376 chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1377
1378 if (local->use_chanctx &&
1379 !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
1380 sdata_info(sdata,
1381 "driver doesn't support chan-switch with channel contexts\n");
1382 goto drop_connection;
1383 }
1384
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001385 if (drv_pre_channel_switch(sdata, &ch_switch)) {
1386 sdata_info(sdata,
1387 "preparing for channel switch failed, disconnecting\n");
1388 goto drop_connection;
1389 }
1390
1391 res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
1392 chanctx->mode, false);
1393 if (res) {
1394 sdata_info(sdata,
1395 "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1396 res);
1397 goto drop_connection;
1398 }
1399 mutex_unlock(&local->chanctx_mtx);
1400
1401 sdata->vif.csa_active = true;
1402 sdata->csa_chandef = csa_ie.chandef;
Olivier Deprez0e641232021-09-23 10:07:05 +02001403 sdata->csa_block_tx = csa_ie.mode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001404 ifmgd->csa_ignored_same_chan = false;
1405
1406 if (sdata->csa_block_tx)
1407 ieee80211_stop_vif_queues(local, sdata,
1408 IEEE80211_QUEUE_STOP_REASON_CSA);
1409 mutex_unlock(&local->mtx);
1410
1411 cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
1412 csa_ie.count);
1413
1414 if (local->ops->channel_switch) {
1415 /* use driver's channel switch callback */
1416 drv_channel_switch(local, sdata, &ch_switch);
1417 return;
1418 }
1419
1420 /* channel switch handled in software */
1421 if (csa_ie.count <= 1)
1422 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
1423 else
1424 mod_timer(&ifmgd->chswitch_timer,
1425 TU_TO_EXP_TIME((csa_ie.count - 1) *
1426 cbss->beacon_interval));
1427 return;
1428 drop_connection:
1429 /*
1430 * This is just so that the disconnect flow will know that
1431 * we were trying to switch channel and failed. In case the
1432 * mode is 1 (we are not allowed to Tx), we will know not to
1433 * send a deauthentication frame. Those two fields will be
1434 * reset when the disconnection worker runs.
1435 */
1436 sdata->vif.csa_active = true;
Olivier Deprez0e641232021-09-23 10:07:05 +02001437 sdata->csa_block_tx = csa_ie.mode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001438
1439 ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1440 mutex_unlock(&local->chanctx_mtx);
1441 mutex_unlock(&local->mtx);
1442}
1443
1444static bool
1445ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1446 struct ieee80211_channel *channel,
1447 const u8 *country_ie, u8 country_ie_len,
1448 const u8 *pwr_constr_elem,
1449 int *chan_pwr, int *pwr_reduction)
1450{
1451 struct ieee80211_country_ie_triplet *triplet;
1452 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1453 int i, chan_increment;
1454 bool have_chan_pwr = false;
1455
1456 /* Invalid IE */
1457 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1458 return false;
1459
1460 triplet = (void *)(country_ie + 3);
1461 country_ie_len -= 3;
1462
1463 switch (channel->band) {
1464 default:
1465 WARN_ON_ONCE(1);
1466 /* fall through */
1467 case NL80211_BAND_2GHZ:
1468 case NL80211_BAND_60GHZ:
1469 chan_increment = 1;
1470 break;
1471 case NL80211_BAND_5GHZ:
1472 chan_increment = 4;
1473 break;
1474 }
1475
1476 /* find channel */
1477 while (country_ie_len >= 3) {
1478 u8 first_channel = triplet->chans.first_channel;
1479
1480 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1481 goto next;
1482
1483 for (i = 0; i < triplet->chans.num_channels; i++) {
1484 if (first_channel + i * chan_increment == chan) {
1485 have_chan_pwr = true;
1486 *chan_pwr = triplet->chans.max_power;
1487 break;
1488 }
1489 }
1490 if (have_chan_pwr)
1491 break;
1492
1493 next:
1494 triplet++;
1495 country_ie_len -= 3;
1496 }
1497
1498 if (have_chan_pwr && pwr_constr_elem)
1499 *pwr_reduction = *pwr_constr_elem;
1500 else
1501 *pwr_reduction = 0;
1502
1503 return have_chan_pwr;
1504}
1505
1506static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
1507 struct ieee80211_channel *channel,
1508 const u8 *cisco_dtpc_ie,
1509 int *pwr_level)
1510{
1511 /* From practical testing, the first data byte of the DTPC element
1512 * seems to contain the requested dBm level, and the CLI on Cisco
1513 * APs clearly state the range is -127 to 127 dBm, which indicates
1514 * a signed byte, although it seemingly never actually goes negative.
1515 * The other byte seems to always be zero.
1516 */
1517 *pwr_level = (__s8)cisco_dtpc_ie[4];
1518}
1519
1520static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1521 struct ieee80211_channel *channel,
1522 struct ieee80211_mgmt *mgmt,
1523 const u8 *country_ie, u8 country_ie_len,
1524 const u8 *pwr_constr_ie,
1525 const u8 *cisco_dtpc_ie)
1526{
1527 bool has_80211h_pwr = false, has_cisco_pwr = false;
1528 int chan_pwr = 0, pwr_reduction_80211h = 0;
1529 int pwr_level_cisco, pwr_level_80211h;
1530 int new_ap_level;
1531 __le16 capab = mgmt->u.probe_resp.capab_info;
1532
1533 if (country_ie &&
1534 (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
1535 capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
1536 has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
1537 sdata, channel, country_ie, country_ie_len,
1538 pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
1539 pwr_level_80211h =
1540 max_t(int, 0, chan_pwr - pwr_reduction_80211h);
1541 }
1542
1543 if (cisco_dtpc_ie) {
1544 ieee80211_find_cisco_dtpc(
1545 sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
1546 has_cisco_pwr = true;
1547 }
1548
1549 if (!has_80211h_pwr && !has_cisco_pwr)
1550 return 0;
1551
1552 /* If we have both 802.11h and Cisco DTPC, apply both limits
1553 * by picking the smallest of the two power levels advertised.
1554 */
1555 if (has_80211h_pwr &&
1556 (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
1557 new_ap_level = pwr_level_80211h;
1558
1559 if (sdata->ap_power_level == new_ap_level)
1560 return 0;
1561
1562 sdata_dbg(sdata,
1563 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1564 pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1565 sdata->u.mgd.bssid);
1566 } else { /* has_cisco_pwr is always true here. */
1567 new_ap_level = pwr_level_cisco;
1568
1569 if (sdata->ap_power_level == new_ap_level)
1570 return 0;
1571
1572 sdata_dbg(sdata,
1573 "Limiting TX power to %d dBm as advertised by %pM\n",
1574 pwr_level_cisco, sdata->u.mgd.bssid);
1575 }
1576
1577 sdata->ap_power_level = new_ap_level;
1578 if (__ieee80211_recalc_txpower(sdata))
1579 return BSS_CHANGED_TXPOWER;
1580 return 0;
1581}
1582
1583/* powersave */
1584static void ieee80211_enable_ps(struct ieee80211_local *local,
1585 struct ieee80211_sub_if_data *sdata)
1586{
1587 struct ieee80211_conf *conf = &local->hw.conf;
1588
1589 /*
1590 * If we are scanning right now then the parameters will
1591 * take effect when scan finishes.
1592 */
1593 if (local->scanning)
1594 return;
1595
1596 if (conf->dynamic_ps_timeout > 0 &&
1597 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
1598 mod_timer(&local->dynamic_ps_timer, jiffies +
1599 msecs_to_jiffies(conf->dynamic_ps_timeout));
1600 } else {
1601 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
1602 ieee80211_send_nullfunc(local, sdata, true);
1603
1604 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1605 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1606 return;
1607
1608 conf->flags |= IEEE80211_CONF_PS;
1609 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1610 }
1611}
1612
1613static void ieee80211_change_ps(struct ieee80211_local *local)
1614{
1615 struct ieee80211_conf *conf = &local->hw.conf;
1616
1617 if (local->ps_sdata) {
1618 ieee80211_enable_ps(local, local->ps_sdata);
1619 } else if (conf->flags & IEEE80211_CONF_PS) {
1620 conf->flags &= ~IEEE80211_CONF_PS;
1621 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1622 del_timer_sync(&local->dynamic_ps_timer);
1623 cancel_work_sync(&local->dynamic_ps_enable_work);
1624 }
1625}
1626
1627static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1628{
1629 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1630 struct sta_info *sta = NULL;
1631 bool authorized = false;
1632
1633 if (!mgd->powersave)
1634 return false;
1635
1636 if (mgd->broken_ap)
1637 return false;
1638
1639 if (!mgd->associated)
1640 return false;
1641
1642 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
1643 return false;
1644
1645 if (!mgd->have_beacon)
1646 return false;
1647
1648 rcu_read_lock();
1649 sta = sta_info_get(sdata, mgd->bssid);
1650 if (sta)
1651 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1652 rcu_read_unlock();
1653
1654 return authorized;
1655}
1656
1657/* need to hold RTNL or interface lock */
1658void ieee80211_recalc_ps(struct ieee80211_local *local)
1659{
1660 struct ieee80211_sub_if_data *sdata, *found = NULL;
1661 int count = 0;
1662 int timeout;
1663
1664 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
1665 local->ps_sdata = NULL;
1666 return;
1667 }
1668
1669 list_for_each_entry(sdata, &local->interfaces, list) {
1670 if (!ieee80211_sdata_running(sdata))
1671 continue;
1672 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1673 /* If an AP vif is found, then disable PS
1674 * by setting the count to zero thereby setting
1675 * ps_sdata to NULL.
1676 */
1677 count = 0;
1678 break;
1679 }
1680 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1681 continue;
1682 found = sdata;
1683 count++;
1684 }
1685
1686 if (count == 1 && ieee80211_powersave_allowed(found)) {
1687 u8 dtimper = found->u.mgd.dtim_period;
1688
1689 timeout = local->dynamic_ps_forced_timeout;
1690 if (timeout < 0)
1691 timeout = 100;
1692 local->hw.conf.dynamic_ps_timeout = timeout;
1693
1694 /* If the TIM IE is invalid, pretend the value is 1 */
1695 if (!dtimper)
1696 dtimper = 1;
1697
1698 local->hw.conf.ps_dtim_period = dtimper;
1699 local->ps_sdata = found;
1700 } else {
1701 local->ps_sdata = NULL;
1702 }
1703
1704 ieee80211_change_ps(local);
1705}
1706
1707void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1708{
1709 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1710
1711 if (sdata->vif.bss_conf.ps != ps_allowed) {
1712 sdata->vif.bss_conf.ps = ps_allowed;
1713 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1714 }
1715}
1716
1717void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1718{
1719 struct ieee80211_local *local =
1720 container_of(work, struct ieee80211_local,
1721 dynamic_ps_disable_work);
1722
1723 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1724 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1725 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1726 }
1727
1728 ieee80211_wake_queues_by_reason(&local->hw,
1729 IEEE80211_MAX_QUEUE_MAP,
1730 IEEE80211_QUEUE_STOP_REASON_PS,
1731 false);
1732}
1733
1734void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1735{
1736 struct ieee80211_local *local =
1737 container_of(work, struct ieee80211_local,
1738 dynamic_ps_enable_work);
1739 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
1740 struct ieee80211_if_managed *ifmgd;
1741 unsigned long flags;
1742 int q;
1743
1744 /* can only happen when PS was just disabled anyway */
1745 if (!sdata)
1746 return;
1747
1748 ifmgd = &sdata->u.mgd;
1749
1750 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1751 return;
1752
1753 if (local->hw.conf.dynamic_ps_timeout > 0) {
1754 /* don't enter PS if TX frames are pending */
1755 if (drv_tx_frames_pending(local)) {
1756 mod_timer(&local->dynamic_ps_timer, jiffies +
1757 msecs_to_jiffies(
1758 local->hw.conf.dynamic_ps_timeout));
1759 return;
1760 }
1761
1762 /*
1763 * transmission can be stopped by others which leads to
1764 * dynamic_ps_timer expiry. Postpone the ps timer if it
1765 * is not the actual idle state.
1766 */
1767 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1768 for (q = 0; q < local->hw.queues; q++) {
1769 if (local->queue_stop_reasons[q]) {
1770 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1771 flags);
1772 mod_timer(&local->dynamic_ps_timer, jiffies +
1773 msecs_to_jiffies(
1774 local->hw.conf.dynamic_ps_timeout));
1775 return;
1776 }
1777 }
1778 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1779 }
1780
1781 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1782 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1783 if (drv_tx_frames_pending(local)) {
1784 mod_timer(&local->dynamic_ps_timer, jiffies +
1785 msecs_to_jiffies(
1786 local->hw.conf.dynamic_ps_timeout));
1787 } else {
1788 ieee80211_send_nullfunc(local, sdata, true);
1789 /* Flush to get the tx status of nullfunc frame */
1790 ieee80211_flush_queues(local, sdata, false);
1791 }
1792 }
1793
1794 if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1795 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
1796 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1797 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1798 local->hw.conf.flags |= IEEE80211_CONF_PS;
1799 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1800 }
1801}
1802
1803void ieee80211_dynamic_ps_timer(struct timer_list *t)
1804{
1805 struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
1806
1807 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1808}
1809
1810void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1811{
1812 struct delayed_work *delayed_work = to_delayed_work(work);
1813 struct ieee80211_sub_if_data *sdata =
1814 container_of(delayed_work, struct ieee80211_sub_if_data,
1815 dfs_cac_timer_work);
1816 struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
1817
1818 mutex_lock(&sdata->local->mtx);
1819 if (sdata->wdev.cac_started) {
1820 ieee80211_vif_release_channel(sdata);
1821 cfg80211_cac_event(sdata->dev, &chandef,
1822 NL80211_RADAR_CAC_FINISHED,
1823 GFP_KERNEL);
1824 }
1825 mutex_unlock(&sdata->local->mtx);
1826}
1827
1828static bool
1829__ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1830{
1831 struct ieee80211_local *local = sdata->local;
1832 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1833 bool ret = false;
1834 int ac;
1835
1836 if (local->hw.queues < IEEE80211_NUM_ACS)
1837 return false;
1838
1839 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1840 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
1841 int non_acm_ac;
1842 unsigned long now = jiffies;
1843
1844 if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
1845 tx_tspec->admitted_time &&
1846 time_after(now, tx_tspec->time_slice_start + HZ)) {
1847 tx_tspec->consumed_tx_time = 0;
1848 tx_tspec->time_slice_start = now;
1849
1850 if (tx_tspec->downgraded)
1851 tx_tspec->action =
1852 TX_TSPEC_ACTION_STOP_DOWNGRADE;
1853 }
1854
1855 switch (tx_tspec->action) {
1856 case TX_TSPEC_ACTION_STOP_DOWNGRADE:
1857 /* take the original parameters */
1858 if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
1859 sdata_err(sdata,
1860 "failed to set TX queue parameters for queue %d\n",
1861 ac);
1862 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1863 tx_tspec->downgraded = false;
1864 ret = true;
1865 break;
1866 case TX_TSPEC_ACTION_DOWNGRADE:
1867 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
1868 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1869 ret = true;
1870 break;
1871 }
1872 /* downgrade next lower non-ACM AC */
1873 for (non_acm_ac = ac + 1;
1874 non_acm_ac < IEEE80211_NUM_ACS;
1875 non_acm_ac++)
1876 if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
1877 break;
1878 /* Usually the loop will result in using BK even if it
1879 * requires admission control, but such a configuration
1880 * makes no sense and we have to transmit somehow - the
1881 * AC selection does the same thing.
1882 * If we started out trying to downgrade from BK, then
1883 * the extra condition here might be needed.
1884 */
1885 if (non_acm_ac >= IEEE80211_NUM_ACS)
1886 non_acm_ac = IEEE80211_AC_BK;
1887 if (drv_conf_tx(local, sdata, ac,
1888 &sdata->tx_conf[non_acm_ac]))
1889 sdata_err(sdata,
1890 "failed to set TX queue parameters for queue %d\n",
1891 ac);
1892 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1893 ret = true;
1894 schedule_delayed_work(&ifmgd->tx_tspec_wk,
1895 tx_tspec->time_slice_start + HZ - now + 1);
1896 break;
1897 case TX_TSPEC_ACTION_NONE:
1898 /* nothing now */
1899 break;
1900 }
1901 }
1902
1903 return ret;
1904}
1905
1906void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1907{
1908 if (__ieee80211_sta_handle_tspec_ac_params(sdata))
1909 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
1910}
1911
1912static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
1913{
1914 struct ieee80211_sub_if_data *sdata;
1915
1916 sdata = container_of(work, struct ieee80211_sub_if_data,
1917 u.mgd.tx_tspec_wk.work);
1918 ieee80211_sta_handle_tspec_ac_params(sdata);
1919}
1920
1921/* MLME */
1922static bool
1923ieee80211_sta_wmm_params(struct ieee80211_local *local,
1924 struct ieee80211_sub_if_data *sdata,
1925 const u8 *wmm_param, size_t wmm_param_len,
1926 const struct ieee80211_mu_edca_param_set *mu_edca)
1927{
1928 struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
1929 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1930 size_t left;
David Brazdil0f672f62019-12-10 10:32:29 +00001931 int count, mu_edca_count, ac;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001932 const u8 *pos;
1933 u8 uapsd_queues = 0;
1934
1935 if (!local->ops->conf_tx)
1936 return false;
1937
1938 if (local->hw.queues < IEEE80211_NUM_ACS)
1939 return false;
1940
1941 if (!wmm_param)
1942 return false;
1943
1944 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
1945 return false;
1946
1947 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
1948 uapsd_queues = ifmgd->uapsd_queues;
1949
1950 count = wmm_param[6] & 0x0f;
David Brazdil0f672f62019-12-10 10:32:29 +00001951 /* -1 is the initial value of ifmgd->mu_edca_last_param_set.
1952 * if mu_edca was preset before and now it disappeared tell
1953 * the driver about it.
1954 */
1955 mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
1956 if (count == ifmgd->wmm_last_param_set &&
1957 mu_edca_count == ifmgd->mu_edca_last_param_set)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001958 return false;
1959 ifmgd->wmm_last_param_set = count;
David Brazdil0f672f62019-12-10 10:32:29 +00001960 ifmgd->mu_edca_last_param_set = mu_edca_count;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001961
1962 pos = wmm_param + 8;
1963 left = wmm_param_len - 8;
1964
1965 memset(&params, 0, sizeof(params));
1966
1967 sdata->wmm_acm = 0;
1968 for (; left >= 4; left -= 4, pos += 4) {
1969 int aci = (pos[0] >> 5) & 0x03;
1970 int acm = (pos[0] >> 4) & 0x01;
1971 bool uapsd = false;
1972
1973 switch (aci) {
1974 case 1: /* AC_BK */
1975 ac = IEEE80211_AC_BK;
1976 if (acm)
1977 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
1978 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1979 uapsd = true;
1980 params[ac].mu_edca = !!mu_edca;
1981 if (mu_edca)
1982 params[ac].mu_edca_param_rec = mu_edca->ac_bk;
1983 break;
1984 case 2: /* AC_VI */
1985 ac = IEEE80211_AC_VI;
1986 if (acm)
1987 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
1988 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1989 uapsd = true;
1990 params[ac].mu_edca = !!mu_edca;
1991 if (mu_edca)
1992 params[ac].mu_edca_param_rec = mu_edca->ac_vi;
1993 break;
1994 case 3: /* AC_VO */
1995 ac = IEEE80211_AC_VO;
1996 if (acm)
1997 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
1998 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1999 uapsd = true;
2000 params[ac].mu_edca = !!mu_edca;
2001 if (mu_edca)
2002 params[ac].mu_edca_param_rec = mu_edca->ac_vo;
2003 break;
2004 case 0: /* AC_BE */
2005 default:
2006 ac = IEEE80211_AC_BE;
2007 if (acm)
2008 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
2009 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2010 uapsd = true;
2011 params[ac].mu_edca = !!mu_edca;
2012 if (mu_edca)
2013 params[ac].mu_edca_param_rec = mu_edca->ac_be;
2014 break;
2015 }
2016
2017 params[ac].aifs = pos[0] & 0x0f;
2018
2019 if (params[ac].aifs < 2) {
2020 sdata_info(sdata,
2021 "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
2022 params[ac].aifs, aci);
2023 params[ac].aifs = 2;
2024 }
2025 params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
2026 params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
2027 params[ac].txop = get_unaligned_le16(pos + 2);
2028 params[ac].acm = acm;
2029 params[ac].uapsd = uapsd;
2030
2031 if (params[ac].cw_min == 0 ||
2032 params[ac].cw_min > params[ac].cw_max) {
2033 sdata_info(sdata,
2034 "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
2035 params[ac].cw_min, params[ac].cw_max, aci);
2036 return false;
2037 }
2038 ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
2039 }
2040
David Brazdil0f672f62019-12-10 10:32:29 +00002041 /* WMM specification requires all 4 ACIs. */
2042 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2043 if (params[ac].cw_min == 0) {
2044 sdata_info(sdata,
2045 "AP has invalid WMM params (missing AC %d), using defaults\n",
2046 ac);
2047 return false;
2048 }
2049 }
2050
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002051 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2052 mlme_dbg(sdata,
2053 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
2054 ac, params[ac].acm,
2055 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
2056 params[ac].txop, params[ac].uapsd,
2057 ifmgd->tx_tspec[ac].downgraded);
2058 sdata->tx_conf[ac] = params[ac];
2059 if (!ifmgd->tx_tspec[ac].downgraded &&
2060 drv_conf_tx(local, sdata, ac, &params[ac]))
2061 sdata_err(sdata,
2062 "failed to set TX queue parameters for AC %d\n",
2063 ac);
2064 }
2065
2066 /* enable WMM or activate new settings */
2067 sdata->vif.bss_conf.qos = true;
2068 return true;
2069}
2070
2071static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2072{
2073 lockdep_assert_held(&sdata->local->mtx);
2074
2075 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
2076 ieee80211_run_deferred_scan(sdata->local);
2077}
2078
2079static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2080{
2081 mutex_lock(&sdata->local->mtx);
2082 __ieee80211_stop_poll(sdata);
2083 mutex_unlock(&sdata->local->mtx);
2084}
2085
2086static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
2087 u16 capab, bool erp_valid, u8 erp)
2088{
2089 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2090 struct ieee80211_supported_band *sband;
2091 u32 changed = 0;
2092 bool use_protection;
2093 bool use_short_preamble;
2094 bool use_short_slot;
2095
2096 sband = ieee80211_get_sband(sdata);
2097 if (!sband)
2098 return changed;
2099
2100 if (erp_valid) {
2101 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
2102 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
2103 } else {
2104 use_protection = false;
2105 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
2106 }
2107
2108 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
2109 if (sband->band == NL80211_BAND_5GHZ)
2110 use_short_slot = true;
2111
2112 if (use_protection != bss_conf->use_cts_prot) {
2113 bss_conf->use_cts_prot = use_protection;
2114 changed |= BSS_CHANGED_ERP_CTS_PROT;
2115 }
2116
2117 if (use_short_preamble != bss_conf->use_short_preamble) {
2118 bss_conf->use_short_preamble = use_short_preamble;
2119 changed |= BSS_CHANGED_ERP_PREAMBLE;
2120 }
2121
2122 if (use_short_slot != bss_conf->use_short_slot) {
2123 bss_conf->use_short_slot = use_short_slot;
2124 changed |= BSS_CHANGED_ERP_SLOT;
2125 }
2126
2127 return changed;
2128}
2129
2130static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
2131 struct cfg80211_bss *cbss,
2132 u32 bss_info_changed)
2133{
2134 struct ieee80211_bss *bss = (void *)cbss->priv;
2135 struct ieee80211_local *local = sdata->local;
2136 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2137
2138 bss_info_changed |= BSS_CHANGED_ASSOC;
2139 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
2140 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
2141
2142 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
2143 beacon_loss_count * bss_conf->beacon_int));
2144
2145 sdata->u.mgd.associated = cbss;
2146 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
2147
2148 ieee80211_check_rate_mask(sdata);
2149
2150 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
2151
2152 if (sdata->vif.p2p ||
2153 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
2154 const struct cfg80211_bss_ies *ies;
2155
2156 rcu_read_lock();
2157 ies = rcu_dereference(cbss->ies);
2158 if (ies) {
2159 int ret;
2160
2161 ret = cfg80211_get_p2p_attr(
2162 ies->data, ies->len,
2163 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2164 (u8 *) &bss_conf->p2p_noa_attr,
2165 sizeof(bss_conf->p2p_noa_attr));
2166 if (ret >= 2) {
2167 sdata->u.mgd.p2p_noa_index =
2168 bss_conf->p2p_noa_attr.index;
2169 bss_info_changed |= BSS_CHANGED_P2P_PS;
2170 }
2171 }
2172 rcu_read_unlock();
2173 }
2174
2175 /* just to be sure */
2176 ieee80211_stop_poll(sdata);
2177
2178 ieee80211_led_assoc(local, 1);
2179
2180 if (sdata->u.mgd.have_beacon) {
2181 /*
2182 * If the AP is buggy we may get here with no DTIM period
2183 * known, so assume it's 1 which is the only safe assumption
2184 * in that case, although if the TIM IE is broken powersave
2185 * probably just won't work at all.
2186 */
2187 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
2188 bss_conf->beacon_rate = bss->beacon_rate;
2189 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
2190 } else {
2191 bss_conf->beacon_rate = NULL;
2192 bss_conf->dtim_period = 0;
2193 }
2194
2195 bss_conf->assoc = 1;
2196
2197 /* Tell the driver to monitor connection quality (if supported) */
2198 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
2199 bss_conf->cqm_rssi_thold)
2200 bss_info_changed |= BSS_CHANGED_CQM;
2201
2202 /* Enable ARP filtering */
2203 if (bss_conf->arp_addr_cnt)
2204 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
2205
2206 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
2207
2208 mutex_lock(&local->iflist_mtx);
2209 ieee80211_recalc_ps(local);
2210 mutex_unlock(&local->iflist_mtx);
2211
2212 ieee80211_recalc_smps(sdata);
2213 ieee80211_recalc_ps_vif(sdata);
2214
2215 netif_carrier_on(sdata->dev);
2216}
2217
2218static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
2219 u16 stype, u16 reason, bool tx,
2220 u8 *frame_buf)
2221{
2222 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2223 struct ieee80211_local *local = sdata->local;
2224 u32 changed = 0;
2225
2226 sdata_assert_lock(sdata);
2227
2228 if (WARN_ON_ONCE(tx && !frame_buf))
2229 return;
2230
2231 if (WARN_ON(!ifmgd->associated))
2232 return;
2233
2234 ieee80211_stop_poll(sdata);
2235
2236 ifmgd->associated = NULL;
2237 netif_carrier_off(sdata->dev);
2238
2239 /*
2240 * if we want to get out of ps before disassoc (why?) we have
2241 * to do it before sending disassoc, as otherwise the null-packet
2242 * won't be valid.
2243 */
2244 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2245 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2246 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2247 }
2248 local->ps_sdata = NULL;
2249
2250 /* disable per-vif ps */
2251 ieee80211_recalc_ps_vif(sdata);
2252
2253 /* make sure ongoing transmission finishes */
2254 synchronize_net();
2255
2256 /*
2257 * drop any frame before deauth/disassoc, this can be data or
2258 * management frame. Since we are disconnecting, we should not
2259 * insist sending these frames which can take time and delay
2260 * the disconnection and possible the roaming.
2261 */
2262 if (tx)
2263 ieee80211_flush_queues(local, sdata, true);
2264
2265 /* deauthenticate/disassociate now */
2266 if (tx || frame_buf) {
2267 /*
2268 * In multi channel scenarios guarantee that the virtual
2269 * interface is granted immediate airtime to transmit the
2270 * deauthentication frame by calling mgd_prepare_tx, if the
2271 * driver requested so.
2272 */
2273 if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
2274 !ifmgd->have_beacon)
2275 drv_mgd_prepare_tx(sdata->local, sdata, 0);
2276
David Brazdil0f672f62019-12-10 10:32:29 +00002277 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid,
2278 ifmgd->bssid, stype, reason,
2279 tx, frame_buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002280 }
2281
2282 /* flush out frame - make sure the deauth was actually sent */
2283 if (tx)
2284 ieee80211_flush_queues(local, sdata, false);
2285
2286 /* clear bssid only after building the needed mgmt frames */
2287 eth_zero_addr(ifmgd->bssid);
2288
2289 /* remove AP and TDLS peers */
2290 sta_info_flush(sdata);
2291
2292 /* finally reset all BSS / config parameters */
2293 changed |= ieee80211_reset_erp_info(sdata);
2294
2295 ieee80211_led_assoc(local, 0);
2296 changed |= BSS_CHANGED_ASSOC;
2297 sdata->vif.bss_conf.assoc = false;
2298
2299 ifmgd->p2p_noa_index = -1;
2300 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2301 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
2302
2303 /* on the next assoc, re-program HT/VHT parameters */
2304 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2305 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
2306 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2307 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
2308
2309 /* reset MU-MIMO ownership and group data */
2310 memset(sdata->vif.bss_conf.mu_group.membership, 0,
2311 sizeof(sdata->vif.bss_conf.mu_group.membership));
2312 memset(sdata->vif.bss_conf.mu_group.position, 0,
2313 sizeof(sdata->vif.bss_conf.mu_group.position));
2314 changed |= BSS_CHANGED_MU_GROUPS;
2315 sdata->vif.mu_mimo_owner = false;
2316
2317 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
2318
2319 del_timer_sync(&local->dynamic_ps_timer);
2320 cancel_work_sync(&local->dynamic_ps_enable_work);
2321
2322 /* Disable ARP filtering */
2323 if (sdata->vif.bss_conf.arp_addr_cnt)
2324 changed |= BSS_CHANGED_ARP_FILTER;
2325
2326 sdata->vif.bss_conf.qos = false;
2327 changed |= BSS_CHANGED_QOS;
2328
2329 /* The BSSID (not really interesting) and HT changed */
2330 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
2331 ieee80211_bss_info_change_notify(sdata, changed);
2332
2333 /* disassociated - set to defaults now */
2334 ieee80211_set_wmm_default(sdata, false, false);
2335
2336 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2337 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2338 del_timer_sync(&sdata->u.mgd.timer);
2339 del_timer_sync(&sdata->u.mgd.chswitch_timer);
2340
2341 sdata->vif.bss_conf.dtim_period = 0;
2342 sdata->vif.bss_conf.beacon_rate = NULL;
2343
2344 ifmgd->have_beacon = false;
2345
2346 ifmgd->flags = 0;
2347 mutex_lock(&local->mtx);
2348 ieee80211_vif_release_channel(sdata);
2349
2350 sdata->vif.csa_active = false;
2351 ifmgd->csa_waiting_bcn = false;
2352 ifmgd->csa_ignored_same_chan = false;
2353 if (sdata->csa_block_tx) {
2354 ieee80211_wake_vif_queues(local, sdata,
2355 IEEE80211_QUEUE_STOP_REASON_CSA);
2356 sdata->csa_block_tx = false;
2357 }
2358 mutex_unlock(&local->mtx);
2359
2360 /* existing TX TSPEC sessions no longer exist */
2361 memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2362 cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2363
2364 sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
2365}
2366
2367void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
2368 struct ieee80211_hdr *hdr)
2369{
2370 /*
2371 * We can postpone the mgd.timer whenever receiving unicast frames
2372 * from AP because we know that the connection is working both ways
2373 * at that time. But multicast frames (and hence also beacons) must
2374 * be ignored here, because we need to trigger the timer during
2375 * data idle periods for sending the periodic probe request to the
2376 * AP we're connected to.
2377 */
2378 if (is_multicast_ether_addr(hdr->addr1))
2379 return;
2380
2381 ieee80211_sta_reset_conn_monitor(sdata);
2382}
2383
2384static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2385{
2386 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2387 struct ieee80211_local *local = sdata->local;
2388
2389 mutex_lock(&local->mtx);
2390 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2391 goto out;
2392
2393 __ieee80211_stop_poll(sdata);
2394
2395 mutex_lock(&local->iflist_mtx);
2396 ieee80211_recalc_ps(local);
2397 mutex_unlock(&local->iflist_mtx);
2398
2399 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
2400 goto out;
2401
2402 /*
2403 * We've received a probe response, but are not sure whether
2404 * we have or will be receiving any beacons or data, so let's
2405 * schedule the timers again, just in case.
2406 */
2407 ieee80211_sta_reset_beacon_monitor(sdata);
2408
2409 mod_timer(&ifmgd->conn_mon_timer,
2410 round_jiffies_up(jiffies +
2411 IEEE80211_CONNECTION_IDLE_TIME));
2412out:
2413 mutex_unlock(&local->mtx);
2414}
2415
2416static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
2417 struct ieee80211_hdr *hdr,
2418 u16 tx_time)
2419{
2420 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2421 u16 tid = ieee80211_get_tid(hdr);
2422 int ac = ieee80211_ac_from_tid(tid);
2423 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
2424 unsigned long now = jiffies;
2425
2426 if (likely(!tx_tspec->admitted_time))
2427 return;
2428
2429 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2430 tx_tspec->consumed_tx_time = 0;
2431 tx_tspec->time_slice_start = now;
2432
2433 if (tx_tspec->downgraded) {
2434 tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2435 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2436 }
2437 }
2438
2439 if (tx_tspec->downgraded)
2440 return;
2441
2442 tx_tspec->consumed_tx_time += tx_time;
2443
2444 if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2445 tx_tspec->downgraded = true;
2446 tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2447 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2448 }
2449}
2450
2451void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2452 struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
2453{
2454 ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2455
2456 if (!ieee80211_is_data(hdr->frame_control))
2457 return;
2458
Olivier Deprez0e641232021-09-23 10:07:05 +02002459 if (ieee80211_is_any_nullfunc(hdr->frame_control) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002460 sdata->u.mgd.probe_send_count > 0) {
2461 if (ack)
2462 ieee80211_sta_reset_conn_monitor(sdata);
2463 else
2464 sdata->u.mgd.nullfunc_failed = true;
2465 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
2466 return;
2467 }
2468
2469 if (ack)
2470 ieee80211_sta_reset_conn_monitor(sdata);
2471}
2472
2473static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2474 const u8 *src, const u8 *dst,
2475 const u8 *ssid, size_t ssid_len,
2476 struct ieee80211_channel *channel)
2477{
2478 struct sk_buff *skb;
2479
2480 skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2481 ssid, ssid_len, NULL, 0,
2482 IEEE80211_PROBE_FLAG_DIRECTED);
2483 if (skb)
2484 ieee80211_tx_skb(sdata, skb);
2485}
2486
2487static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2488{
2489 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2490 const u8 *ssid;
2491 u8 *dst = ifmgd->associated->bssid;
2492 u8 unicast_limit = max(1, max_probe_tries - 3);
2493 struct sta_info *sta;
2494
2495 /*
2496 * Try sending broadcast probe requests for the last three
2497 * probe requests after the first ones failed since some
2498 * buggy APs only support broadcast probe requests.
2499 */
2500 if (ifmgd->probe_send_count >= unicast_limit)
2501 dst = NULL;
2502
2503 /*
2504 * When the hardware reports an accurate Tx ACK status, it's
2505 * better to send a nullfunc frame instead of a probe request,
2506 * as it will kick us off the AP quickly if we aren't associated
2507 * anymore. The timeout will be reset if the frame is ACKed by
2508 * the AP.
2509 */
2510 ifmgd->probe_send_count++;
2511
2512 if (dst) {
2513 mutex_lock(&sdata->local->sta_mtx);
2514 sta = sta_info_get(sdata, dst);
2515 if (!WARN_ON(!sta))
2516 ieee80211_check_fast_rx(sta);
2517 mutex_unlock(&sdata->local->sta_mtx);
2518 }
2519
2520 if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
2521 ifmgd->nullfunc_failed = false;
David Brazdil0f672f62019-12-10 10:32:29 +00002522 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
2523 ifmgd->probe_send_count--;
2524 else
2525 ieee80211_send_nullfunc(sdata->local, sdata, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002526 } else {
2527 int ssid_len;
2528
2529 rcu_read_lock();
2530 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
2531 if (WARN_ON_ONCE(ssid == NULL))
2532 ssid_len = 0;
2533 else
2534 ssid_len = ssid[1];
2535
2536 ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
2537 ssid + 2, ssid_len,
2538 ifmgd->associated->channel);
2539 rcu_read_unlock();
2540 }
2541
2542 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
2543 run_again(sdata, ifmgd->probe_timeout);
2544}
2545
2546static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2547 bool beacon)
2548{
2549 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2550 bool already = false;
2551
2552 if (!ieee80211_sdata_running(sdata))
2553 return;
2554
2555 sdata_lock(sdata);
2556
2557 if (!ifmgd->associated)
2558 goto out;
2559
2560 mutex_lock(&sdata->local->mtx);
2561
2562 if (sdata->local->tmp_channel || sdata->local->scanning) {
2563 mutex_unlock(&sdata->local->mtx);
2564 goto out;
2565 }
2566
2567 if (beacon) {
2568 mlme_dbg_ratelimited(sdata,
2569 "detected beacon loss from AP (missed %d beacons) - probing\n",
2570 beacon_loss_count);
2571
2572 ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
2573 }
2574
2575 /*
2576 * The driver/our work has already reported this event or the
2577 * connection monitoring has kicked in and we have already sent
2578 * a probe request. Or maybe the AP died and the driver keeps
2579 * reporting until we disassociate...
2580 *
2581 * In either case we have to ignore the current call to this
2582 * function (except for setting the correct probe reason bit)
2583 * because otherwise we would reset the timer every time and
2584 * never check whether we received a probe response!
2585 */
2586 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
2587 already = true;
2588
2589 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2590
2591 mutex_unlock(&sdata->local->mtx);
2592
2593 if (already)
2594 goto out;
2595
2596 mutex_lock(&sdata->local->iflist_mtx);
2597 ieee80211_recalc_ps(sdata->local);
2598 mutex_unlock(&sdata->local->iflist_mtx);
2599
2600 ifmgd->probe_send_count = 0;
2601 ieee80211_mgd_probe_ap_send(sdata);
2602 out:
2603 sdata_unlock(sdata);
2604}
2605
2606struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2607 struct ieee80211_vif *vif)
2608{
2609 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2610 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2611 struct cfg80211_bss *cbss;
2612 struct sk_buff *skb;
2613 const u8 *ssid;
2614 int ssid_len;
2615
2616 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2617 return NULL;
2618
2619 sdata_assert_lock(sdata);
2620
2621 if (ifmgd->associated)
2622 cbss = ifmgd->associated;
2623 else if (ifmgd->auth_data)
2624 cbss = ifmgd->auth_data->bss;
2625 else if (ifmgd->assoc_data)
2626 cbss = ifmgd->assoc_data->bss;
2627 else
2628 return NULL;
2629
2630 rcu_read_lock();
2631 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
David Brazdil0f672f62019-12-10 10:32:29 +00002632 if (WARN_ONCE(!ssid || ssid[1] > IEEE80211_MAX_SSID_LEN,
2633 "invalid SSID element (len=%d)", ssid ? ssid[1] : -1))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002634 ssid_len = 0;
2635 else
2636 ssid_len = ssid[1];
2637
2638 skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
2639 (u32) -1, cbss->channel,
2640 ssid + 2, ssid_len,
2641 NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
2642 rcu_read_unlock();
2643
2644 return skb;
2645}
2646EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2647
2648static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2649 const u8 *buf, size_t len, bool tx,
2650 u16 reason)
2651{
2652 struct ieee80211_event event = {
2653 .type = MLME_EVENT,
2654 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2655 .u.mlme.reason = reason,
2656 };
2657
2658 if (tx)
2659 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
2660 else
2661 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2662
2663 drv_event_callback(sdata->local, sdata, &event);
2664}
2665
2666static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2667{
2668 struct ieee80211_local *local = sdata->local;
2669 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2670 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
2671 bool tx;
2672
2673 sdata_lock(sdata);
2674 if (!ifmgd->associated) {
2675 sdata_unlock(sdata);
2676 return;
2677 }
2678
2679 tx = !sdata->csa_block_tx;
2680
2681 /* AP is probably out of range (or not reachable for another reason) so
2682 * remove the bss struct for that AP.
2683 */
2684 cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2685
2686 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2687 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2688 tx, frame_buf);
2689 mutex_lock(&local->mtx);
2690 sdata->vif.csa_active = false;
2691 ifmgd->csa_waiting_bcn = false;
2692 if (sdata->csa_block_tx) {
2693 ieee80211_wake_vif_queues(local, sdata,
2694 IEEE80211_QUEUE_STOP_REASON_CSA);
2695 sdata->csa_block_tx = false;
2696 }
2697 mutex_unlock(&local->mtx);
2698
2699 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
2700 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2701
2702 sdata_unlock(sdata);
2703}
2704
2705static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
2706{
2707 struct ieee80211_sub_if_data *sdata =
2708 container_of(work, struct ieee80211_sub_if_data,
2709 u.mgd.beacon_connection_loss_work);
2710 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2711
2712 if (ifmgd->associated)
2713 ifmgd->beacon_loss_count++;
2714
2715 if (ifmgd->connection_loss) {
2716 sdata_info(sdata, "Connection to AP %pM lost\n",
2717 ifmgd->bssid);
2718 __ieee80211_disconnect(sdata);
2719 } else {
2720 ieee80211_mgd_probe_ap(sdata, true);
2721 }
2722}
2723
2724static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2725{
2726 struct ieee80211_sub_if_data *sdata =
2727 container_of(work, struct ieee80211_sub_if_data,
2728 u.mgd.csa_connection_drop_work);
2729
2730 __ieee80211_disconnect(sdata);
2731}
2732
2733void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2734{
2735 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2736 struct ieee80211_hw *hw = &sdata->local->hw;
2737
2738 trace_api_beacon_loss(sdata);
2739
2740 sdata->u.mgd.connection_loss = false;
2741 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2742}
2743EXPORT_SYMBOL(ieee80211_beacon_loss);
2744
2745void ieee80211_connection_loss(struct ieee80211_vif *vif)
2746{
2747 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2748 struct ieee80211_hw *hw = &sdata->local->hw;
2749
2750 trace_api_connection_loss(sdata);
2751
2752 sdata->u.mgd.connection_loss = true;
2753 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2754}
2755EXPORT_SYMBOL(ieee80211_connection_loss);
2756
2757
2758static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2759 bool assoc)
2760{
2761 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2762
2763 sdata_assert_lock(sdata);
2764
2765 if (!assoc) {
2766 /*
2767 * we are not authenticated yet, the only timer that could be
2768 * running is the timeout for the authentication response which
2769 * which is not relevant anymore.
2770 */
2771 del_timer_sync(&sdata->u.mgd.timer);
2772 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2773
2774 eth_zero_addr(sdata->u.mgd.bssid);
2775 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2776 sdata->u.mgd.flags = 0;
2777 mutex_lock(&sdata->local->mtx);
2778 ieee80211_vif_release_channel(sdata);
2779 mutex_unlock(&sdata->local->mtx);
2780 }
2781
2782 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
2783 kfree(auth_data);
2784 sdata->u.mgd.auth_data = NULL;
2785}
2786
2787static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2788 bool assoc, bool abandon)
2789{
2790 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2791
2792 sdata_assert_lock(sdata);
2793
2794 if (!assoc) {
2795 /*
2796 * we are not associated yet, the only timer that could be
2797 * running is the timeout for the association response which
2798 * which is not relevant anymore.
2799 */
2800 del_timer_sync(&sdata->u.mgd.timer);
2801 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2802
2803 eth_zero_addr(sdata->u.mgd.bssid);
2804 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2805 sdata->u.mgd.flags = 0;
2806 sdata->vif.mu_mimo_owner = false;
2807
2808 mutex_lock(&sdata->local->mtx);
2809 ieee80211_vif_release_channel(sdata);
2810 mutex_unlock(&sdata->local->mtx);
2811
2812 if (abandon)
2813 cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
2814 }
2815
2816 kfree(assoc_data);
2817 sdata->u.mgd.assoc_data = NULL;
2818}
2819
2820static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2821 struct ieee80211_mgmt *mgmt, size_t len)
2822{
2823 struct ieee80211_local *local = sdata->local;
2824 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2825 u8 *pos;
2826 struct ieee802_11_elems elems;
2827 u32 tx_flags = 0;
2828
2829 pos = mgmt->u.auth.variable;
David Brazdil0f672f62019-12-10 10:32:29 +00002830 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
2831 mgmt->bssid, auth_data->bss->bssid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002832 if (!elems.challenge)
2833 return;
2834 auth_data->expected_transaction = 4;
2835 drv_mgd_prepare_tx(sdata->local, sdata, 0);
2836 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
2837 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2838 IEEE80211_TX_INTFL_MLME_CONN_TX;
2839 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
2840 elems.challenge - 2, elems.challenge_len + 2,
2841 auth_data->bss->bssid, auth_data->bss->bssid,
2842 auth_data->key, auth_data->key_len,
2843 auth_data->key_idx, tx_flags);
2844}
2845
David Brazdil0f672f62019-12-10 10:32:29 +00002846static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata,
2847 const u8 *bssid)
2848{
2849 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2850 struct sta_info *sta;
2851 bool result = true;
2852
2853 sdata_info(sdata, "authenticated\n");
2854 ifmgd->auth_data->done = true;
2855 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2856 ifmgd->auth_data->timeout_started = true;
2857 run_again(sdata, ifmgd->auth_data->timeout);
2858
2859 /* move station state to auth */
2860 mutex_lock(&sdata->local->sta_mtx);
2861 sta = sta_info_get(sdata, bssid);
2862 if (!sta) {
2863 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2864 result = false;
2865 goto out;
2866 }
2867 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2868 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2869 result = false;
2870 goto out;
2871 }
2872
2873out:
2874 mutex_unlock(&sdata->local->sta_mtx);
2875 return result;
2876}
2877
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002878static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2879 struct ieee80211_mgmt *mgmt, size_t len)
2880{
2881 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2882 u8 bssid[ETH_ALEN];
2883 u16 auth_alg, auth_transaction, status_code;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002884 struct ieee80211_event event = {
2885 .type = MLME_EVENT,
2886 .u.mlme.data = AUTH_EVENT,
2887 };
2888
2889 sdata_assert_lock(sdata);
2890
2891 if (len < 24 + 6)
2892 return;
2893
2894 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2895 return;
2896
2897 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2898
2899 if (!ether_addr_equal(bssid, mgmt->bssid))
2900 return;
2901
2902 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2903 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2904 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2905
2906 if (auth_alg != ifmgd->auth_data->algorithm ||
David Brazdil0f672f62019-12-10 10:32:29 +00002907 (auth_alg != WLAN_AUTH_SAE &&
2908 auth_transaction != ifmgd->auth_data->expected_transaction) ||
2909 (auth_alg == WLAN_AUTH_SAE &&
2910 (auth_transaction < ifmgd->auth_data->expected_transaction ||
2911 auth_transaction > 2))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002912 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2913 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2914 auth_transaction,
2915 ifmgd->auth_data->expected_transaction);
2916 return;
2917 }
2918
2919 if (status_code != WLAN_STATUS_SUCCESS) {
2920 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2921 mgmt->sa, status_code);
2922 ieee80211_destroy_auth_data(sdata, false);
2923 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2924 event.u.mlme.status = MLME_DENIED;
2925 event.u.mlme.reason = status_code;
2926 drv_event_callback(sdata->local, sdata, &event);
2927 return;
2928 }
2929
2930 switch (ifmgd->auth_data->algorithm) {
2931 case WLAN_AUTH_OPEN:
2932 case WLAN_AUTH_LEAP:
2933 case WLAN_AUTH_FT:
2934 case WLAN_AUTH_SAE:
2935 case WLAN_AUTH_FILS_SK:
2936 case WLAN_AUTH_FILS_SK_PFS:
2937 case WLAN_AUTH_FILS_PK:
2938 break;
2939 case WLAN_AUTH_SHARED_KEY:
2940 if (ifmgd->auth_data->expected_transaction != 4) {
2941 ieee80211_auth_challenge(sdata, mgmt, len);
2942 /* need another frame */
2943 return;
2944 }
2945 break;
2946 default:
2947 WARN_ONCE(1, "invalid auth alg %d",
2948 ifmgd->auth_data->algorithm);
2949 return;
2950 }
2951
2952 event.u.mlme.status = MLME_SUCCESS;
2953 drv_event_callback(sdata->local, sdata, &event);
David Brazdil0f672f62019-12-10 10:32:29 +00002954 if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
2955 (auth_transaction == 2 &&
2956 ifmgd->auth_data->expected_transaction == 2)) {
2957 if (!ieee80211_mark_sta_auth(sdata, bssid))
Olivier Deprez0e641232021-09-23 10:07:05 +02002958 return; /* ignore frame -- wait for timeout */
David Brazdil0f672f62019-12-10 10:32:29 +00002959 } else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2960 auth_transaction == 2) {
2961 sdata_info(sdata, "SAE peer confirmed\n");
2962 ifmgd->auth_data->peer_confirmed = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002963 }
2964
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002965 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002966}
2967
2968#define case_WLAN(type) \
2969 case WLAN_REASON_##type: return #type
2970
David Brazdil0f672f62019-12-10 10:32:29 +00002971const char *ieee80211_get_reason_code_string(u16 reason_code)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002972{
2973 switch (reason_code) {
2974 case_WLAN(UNSPECIFIED);
2975 case_WLAN(PREV_AUTH_NOT_VALID);
2976 case_WLAN(DEAUTH_LEAVING);
2977 case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
2978 case_WLAN(DISASSOC_AP_BUSY);
2979 case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
2980 case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
2981 case_WLAN(DISASSOC_STA_HAS_LEFT);
2982 case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
2983 case_WLAN(DISASSOC_BAD_POWER);
2984 case_WLAN(DISASSOC_BAD_SUPP_CHAN);
2985 case_WLAN(INVALID_IE);
2986 case_WLAN(MIC_FAILURE);
2987 case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
2988 case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
2989 case_WLAN(IE_DIFFERENT);
2990 case_WLAN(INVALID_GROUP_CIPHER);
2991 case_WLAN(INVALID_PAIRWISE_CIPHER);
2992 case_WLAN(INVALID_AKMP);
2993 case_WLAN(UNSUPP_RSN_VERSION);
2994 case_WLAN(INVALID_RSN_IE_CAP);
2995 case_WLAN(IEEE8021X_FAILED);
2996 case_WLAN(CIPHER_SUITE_REJECTED);
2997 case_WLAN(DISASSOC_UNSPECIFIED_QOS);
2998 case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
2999 case_WLAN(DISASSOC_LOW_ACK);
3000 case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
3001 case_WLAN(QSTA_LEAVE_QBSS);
3002 case_WLAN(QSTA_NOT_USE);
3003 case_WLAN(QSTA_REQUIRE_SETUP);
3004 case_WLAN(QSTA_TIMEOUT);
3005 case_WLAN(QSTA_CIPHER_NOT_SUPP);
3006 case_WLAN(MESH_PEER_CANCELED);
3007 case_WLAN(MESH_MAX_PEERS);
3008 case_WLAN(MESH_CONFIG);
3009 case_WLAN(MESH_CLOSE);
3010 case_WLAN(MESH_MAX_RETRIES);
3011 case_WLAN(MESH_CONFIRM_TIMEOUT);
3012 case_WLAN(MESH_INVALID_GTK);
3013 case_WLAN(MESH_INCONSISTENT_PARAM);
3014 case_WLAN(MESH_INVALID_SECURITY);
3015 case_WLAN(MESH_PATH_ERROR);
3016 case_WLAN(MESH_PATH_NOFORWARD);
3017 case_WLAN(MESH_PATH_DEST_UNREACHABLE);
3018 case_WLAN(MAC_EXISTS_IN_MBSS);
3019 case_WLAN(MESH_CHAN_REGULATORY);
3020 case_WLAN(MESH_CHAN);
3021 default: return "<unknown>";
3022 }
3023}
3024
3025static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
3026 struct ieee80211_mgmt *mgmt, size_t len)
3027{
3028 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3029 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
3030
3031 sdata_assert_lock(sdata);
3032
3033 if (len < 24 + 2)
3034 return;
3035
David Brazdil0f672f62019-12-10 10:32:29 +00003036 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3037 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3038 return;
3039 }
3040
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003041 if (ifmgd->associated &&
3042 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
3043 const u8 *bssid = ifmgd->associated->bssid;
3044
3045 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
3046 bssid, reason_code,
3047 ieee80211_get_reason_code_string(reason_code));
3048
3049 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3050
3051 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
3052 reason_code);
3053 return;
3054 }
3055
3056 if (ifmgd->assoc_data &&
3057 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
3058 const u8 *bssid = ifmgd->assoc_data->bss->bssid;
3059
3060 sdata_info(sdata,
3061 "deauthenticated from %pM while associating (Reason: %u=%s)\n",
3062 bssid, reason_code,
3063 ieee80211_get_reason_code_string(reason_code));
3064
3065 ieee80211_destroy_assoc_data(sdata, false, true);
3066
3067 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3068 return;
3069 }
3070}
3071
3072
3073static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
3074 struct ieee80211_mgmt *mgmt, size_t len)
3075{
3076 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3077 u16 reason_code;
3078
3079 sdata_assert_lock(sdata);
3080
3081 if (len < 24 + 2)
3082 return;
3083
3084 if (!ifmgd->associated ||
3085 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3086 return;
3087
3088 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
3089
David Brazdil0f672f62019-12-10 10:32:29 +00003090 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3091 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3092 return;
3093 }
3094
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003095 sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
3096 mgmt->sa, reason_code,
3097 ieee80211_get_reason_code_string(reason_code));
3098
3099 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3100
3101 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
3102}
3103
3104static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
3105 u8 *supp_rates, unsigned int supp_rates_len,
3106 u32 *rates, u32 *basic_rates,
3107 bool *have_higher_than_11mbit,
3108 int *min_rate, int *min_rate_index,
3109 int shift)
3110{
3111 int i, j;
3112
3113 for (i = 0; i < supp_rates_len; i++) {
3114 int rate = supp_rates[i] & 0x7f;
3115 bool is_basic = !!(supp_rates[i] & 0x80);
3116
3117 if ((rate * 5 * (1 << shift)) > 110)
3118 *have_higher_than_11mbit = true;
3119
3120 /*
3121 * Skip HT and VHT BSS membership selectors since they're not
3122 * rates.
3123 *
3124 * Note: Even though the membership selector and the basic
3125 * rate flag share the same bit, they are not exactly
3126 * the same.
3127 */
3128 if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
3129 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY))
3130 continue;
3131
3132 for (j = 0; j < sband->n_bitrates; j++) {
3133 struct ieee80211_rate *br;
3134 int brate;
3135
3136 br = &sband->bitrates[j];
3137
3138 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3139 if (brate == rate) {
3140 *rates |= BIT(j);
3141 if (is_basic)
3142 *basic_rates |= BIT(j);
3143 if ((rate * 5) < *min_rate) {
3144 *min_rate = rate * 5;
3145 *min_rate_index = j;
3146 }
3147 break;
3148 }
3149 }
3150 }
3151}
3152
David Brazdil0f672f62019-12-10 10:32:29 +00003153static bool ieee80211_twt_req_supported(const struct sta_info *sta,
3154 const struct ieee802_11_elems *elems)
3155{
3156 if (elems->ext_capab_len < 10)
3157 return false;
3158
3159 if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
3160 return false;
3161
3162 return sta->sta.he_cap.he_cap_elem.mac_cap_info[0] &
3163 IEEE80211_HE_MAC_CAP0_TWT_RES;
3164}
3165
3166static int ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata,
3167 struct sta_info *sta,
3168 struct ieee802_11_elems *elems)
3169{
3170 bool twt = ieee80211_twt_req_supported(sta, elems);
3171
3172 if (sdata->vif.bss_conf.twt_requester != twt) {
3173 sdata->vif.bss_conf.twt_requester = twt;
3174 return BSS_CHANGED_TWT;
3175 }
3176 return 0;
3177}
3178
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003179static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
3180 struct cfg80211_bss *cbss,
3181 struct ieee80211_mgmt *mgmt, size_t len)
3182{
3183 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3184 struct ieee80211_local *local = sdata->local;
3185 struct ieee80211_supported_band *sband;
3186 struct sta_info *sta;
3187 u8 *pos;
3188 u16 capab_info, aid;
3189 struct ieee802_11_elems elems;
3190 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3191 const struct cfg80211_bss_ies *bss_ies = NULL;
3192 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3193 u32 changed = 0;
3194 int err;
3195 bool ret;
3196
3197 /* AssocResp and ReassocResp have identical structure */
3198
3199 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3200 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3201
3202 /*
3203 * The 5 MSB of the AID field are reserved
3204 * (802.11-2016 9.4.1.8 AID field)
3205 */
3206 aid &= 0x7ff;
3207
3208 ifmgd->broken_ap = false;
3209
3210 if (aid == 0 || aid > IEEE80211_MAX_AID) {
3211 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3212 aid);
3213 aid = 0;
3214 ifmgd->broken_ap = true;
3215 }
3216
3217 pos = mgmt->u.assoc_resp.variable;
David Brazdil0f672f62019-12-10 10:32:29 +00003218 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
3219 mgmt->bssid, assoc_data->bss->bssid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003220
3221 if (!elems.supp_rates) {
3222 sdata_info(sdata, "no SuppRates element in AssocResp\n");
3223 return false;
3224 }
3225
3226 ifmgd->aid = aid;
3227 ifmgd->tdls_chan_switch_prohibited =
3228 elems.ext_capab && elems.ext_capab_len >= 5 &&
3229 (elems.ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
3230
3231 /*
3232 * Some APs are erroneously not including some information in their
3233 * (re)association response frames. Try to recover by using the data
3234 * from the beacon or probe response. This seems to afflict mobile
3235 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3236 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3237 */
3238 if ((assoc_data->wmm && !elems.wmm_param) ||
3239 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3240 (!elems.ht_cap_elem || !elems.ht_operation)) ||
3241 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3242 (!elems.vht_cap_elem || !elems.vht_operation))) {
3243 const struct cfg80211_bss_ies *ies;
3244 struct ieee802_11_elems bss_elems;
3245
3246 rcu_read_lock();
3247 ies = rcu_dereference(cbss->ies);
3248 if (ies)
3249 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3250 GFP_ATOMIC);
3251 rcu_read_unlock();
3252 if (!bss_ies)
3253 return false;
3254
3255 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
David Brazdil0f672f62019-12-10 10:32:29 +00003256 false, &bss_elems,
3257 mgmt->bssid,
3258 assoc_data->bss->bssid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003259 if (assoc_data->wmm &&
3260 !elems.wmm_param && bss_elems.wmm_param) {
3261 elems.wmm_param = bss_elems.wmm_param;
3262 sdata_info(sdata,
3263 "AP bug: WMM param missing from AssocResp\n");
3264 }
3265
3266 /*
3267 * Also check if we requested HT/VHT, otherwise the AP doesn't
3268 * have to include the IEs in the (re)association response.
3269 */
3270 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
3271 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3272 elems.ht_cap_elem = bss_elems.ht_cap_elem;
3273 sdata_info(sdata,
3274 "AP bug: HT capability missing from AssocResp\n");
3275 }
3276 if (!elems.ht_operation && bss_elems.ht_operation &&
3277 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3278 elems.ht_operation = bss_elems.ht_operation;
3279 sdata_info(sdata,
3280 "AP bug: HT operation missing from AssocResp\n");
3281 }
3282 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
3283 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3284 elems.vht_cap_elem = bss_elems.vht_cap_elem;
3285 sdata_info(sdata,
3286 "AP bug: VHT capa missing from AssocResp\n");
3287 }
3288 if (!elems.vht_operation && bss_elems.vht_operation &&
3289 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3290 elems.vht_operation = bss_elems.vht_operation;
3291 sdata_info(sdata,
3292 "AP bug: VHT operation missing from AssocResp\n");
3293 }
3294 }
3295
3296 /*
3297 * We previously checked these in the beacon/probe response, so
3298 * they should be present here. This is just a safety net.
3299 */
3300 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3301 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
3302 sdata_info(sdata,
3303 "HT AP is missing WMM params or HT capability/operation\n");
3304 ret = false;
3305 goto out;
3306 }
3307
3308 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3309 (!elems.vht_cap_elem || !elems.vht_operation)) {
3310 sdata_info(sdata,
3311 "VHT AP is missing VHT capability/operation\n");
3312 ret = false;
3313 goto out;
3314 }
3315
3316 mutex_lock(&sdata->local->sta_mtx);
3317 /*
3318 * station info was already allocated and inserted before
3319 * the association and should be available to us
3320 */
3321 sta = sta_info_get(sdata, cbss->bssid);
3322 if (WARN_ON(!sta)) {
3323 mutex_unlock(&sdata->local->sta_mtx);
3324 ret = false;
3325 goto out;
3326 }
3327
3328 sband = ieee80211_get_sband(sdata);
3329 if (!sband) {
3330 mutex_unlock(&sdata->local->sta_mtx);
3331 ret = false;
3332 goto out;
3333 }
3334
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003335 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3336 (!elems.he_cap || !elems.he_operation)) {
3337 mutex_unlock(&sdata->local->sta_mtx);
3338 sdata_info(sdata,
3339 "HE AP is missing HE capability/operation\n");
3340 ret = false;
3341 goto out;
3342 }
3343
3344 /* Set up internal HT/VHT capabilities */
3345 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
3346 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
3347 elems.ht_cap_elem, sta);
3348
3349 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
3350 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
3351 elems.vht_cap_elem, sta);
3352
3353 if (elems.he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3354 elems.he_cap) {
3355 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
3356 elems.he_cap,
3357 elems.he_cap_len,
3358 sta);
3359
3360 bss_conf->he_support = sta->sta.he_cap.has_he;
David Brazdil0f672f62019-12-10 10:32:29 +00003361 changed |= ieee80211_recalc_twt_req(sdata, sta, &elems);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003362 } else {
3363 bss_conf->he_support = false;
David Brazdil0f672f62019-12-10 10:32:29 +00003364 bss_conf->twt_requester = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003365 }
3366
3367 if (bss_conf->he_support) {
David Brazdil0f672f62019-12-10 10:32:29 +00003368 bss_conf->bss_color =
3369 le32_get_bits(elems.he_operation->he_oper_params,
3370 IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003371
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003372 bss_conf->htc_trig_based_pkt_ext =
David Brazdil0f672f62019-12-10 10:32:29 +00003373 le32_get_bits(elems.he_operation->he_oper_params,
3374 IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003375 bss_conf->frame_time_rts_th =
David Brazdil0f672f62019-12-10 10:32:29 +00003376 le32_get_bits(elems.he_operation->he_oper_params,
3377 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003378
3379 bss_conf->multi_sta_back_32bit =
3380 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3381 IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP;
3382
3383 bss_conf->ack_enabled =
3384 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3385 IEEE80211_HE_MAC_CAP2_ACK_EN;
3386
3387 bss_conf->uora_exists = !!elems.uora_element;
3388 if (elems.uora_element)
3389 bss_conf->uora_ocw_range = elems.uora_element[0];
3390
David Brazdil0f672f62019-12-10 10:32:29 +00003391 ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems.he_operation);
3392 ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems.he_spr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003393 /* TODO: OPEN: what happens if BSS color disable is set? */
3394 }
3395
David Brazdil0f672f62019-12-10 10:32:29 +00003396 if (cbss->transmitted_bss) {
3397 bss_conf->nontransmitted = true;
3398 ether_addr_copy(bss_conf->transmitter_bssid,
3399 cbss->transmitted_bss->bssid);
3400 bss_conf->bssid_indicator = cbss->max_bssid_indicator;
3401 bss_conf->bssid_index = cbss->bssid_index;
3402 }
3403
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003404 /*
3405 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3406 * in their association response, so ignore that data for our own
3407 * configuration. If it changed since the last beacon, we'll get the
3408 * next beacon and update then.
3409 */
3410
3411 /*
3412 * If an operating mode notification IE is present, override the
3413 * NSS calculation (that would be done in rate_control_rate_init())
3414 * and use the # of streams from that element.
3415 */
3416 if (elems.opmode_notif &&
3417 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
3418 u8 nss;
3419
3420 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
3421 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3422 nss += 1;
3423 sta->sta.rx_nss = nss;
3424 }
3425
3426 rate_control_rate_init(sta);
3427
3428 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
3429 set_sta_flag(sta, WLAN_STA_MFP);
3430 sta->sta.mfp = true;
3431 } else {
3432 sta->sta.mfp = false;
3433 }
3434
3435 sta->sta.wme = elems.wmm_param && local->hw.queues >= IEEE80211_NUM_ACS;
3436
3437 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
3438 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3439 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3440 if (err) {
3441 sdata_info(sdata,
3442 "failed to move station %pM to desired state\n",
3443 sta->sta.addr);
3444 WARN_ON(__sta_info_destroy(sta));
3445 mutex_unlock(&sdata->local->sta_mtx);
3446 ret = false;
3447 goto out;
3448 }
3449
3450 mutex_unlock(&sdata->local->sta_mtx);
3451
3452 /*
3453 * Always handle WMM once after association regardless
3454 * of the first value the AP uses. Setting -1 here has
3455 * that effect because the AP values is an unsigned
3456 * 4-bit value.
3457 */
3458 ifmgd->wmm_last_param_set = -1;
David Brazdil0f672f62019-12-10 10:32:29 +00003459 ifmgd->mu_edca_last_param_set = -1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003460
3461 if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
3462 ieee80211_set_wmm_default(sdata, false, false);
3463 } else if (!ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3464 elems.wmm_param_len,
3465 elems.mu_edca_param_set)) {
3466 /* still enable QoS since we might have HT/VHT */
3467 ieee80211_set_wmm_default(sdata, false, true);
3468 /* set the disable-WMM flag in this case to disable
3469 * tracking WMM parameter changes in the beacon if
3470 * the parameters weren't actually valid. Doing so
3471 * avoids changing parameters very strangely when
3472 * the AP is going back and forth between valid and
3473 * invalid parameters.
3474 */
3475 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3476 }
3477 changed |= BSS_CHANGED_QOS;
3478
3479 if (elems.max_idle_period_ie) {
3480 bss_conf->max_idle_period =
3481 le16_to_cpu(elems.max_idle_period_ie->max_idle_period);
3482 bss_conf->protected_keep_alive =
3483 !!(elems.max_idle_period_ie->idle_options &
3484 WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3485 changed |= BSS_CHANGED_KEEP_ALIVE;
3486 } else {
3487 bss_conf->max_idle_period = 0;
3488 bss_conf->protected_keep_alive = false;
3489 }
3490
3491 /* set AID and assoc capability,
3492 * ieee80211_set_associated() will tell the driver */
3493 bss_conf->aid = aid;
3494 bss_conf->assoc_capability = capab_info;
3495 ieee80211_set_associated(sdata, cbss, changed);
3496
3497 /*
3498 * If we're using 4-addr mode, let the AP know that we're
3499 * doing so, so that it can create the STA VLAN on its side
3500 */
3501 if (ifmgd->use_4addr)
3502 ieee80211_send_4addr_nullfunc(local, sdata);
3503
3504 /*
3505 * Start timer to probe the connection to the AP now.
3506 * Also start the timer that will detect beacon loss.
3507 */
3508 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
3509 ieee80211_sta_reset_beacon_monitor(sdata);
3510
3511 ret = true;
3512 out:
3513 kfree(bss_ies);
3514 return ret;
3515}
3516
3517static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3518 struct ieee80211_mgmt *mgmt,
3519 size_t len)
3520{
3521 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3522 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3523 u16 capab_info, status_code, aid;
3524 struct ieee802_11_elems elems;
3525 int ac, uapsd_queues = -1;
3526 u8 *pos;
3527 bool reassoc;
3528 struct cfg80211_bss *bss;
3529 struct ieee80211_event event = {
3530 .type = MLME_EVENT,
3531 .u.mlme.data = ASSOC_EVENT,
3532 };
3533
3534 sdata_assert_lock(sdata);
3535
3536 if (!assoc_data)
3537 return;
3538 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
3539 return;
3540
3541 /*
3542 * AssocResp and ReassocResp have identical structure, so process both
3543 * of them in this function.
3544 */
3545
3546 if (len < 24 + 6)
3547 return;
3548
3549 reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
3550 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3551 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
3552 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3553
3554 sdata_info(sdata,
3555 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
3556 reassoc ? "Rea" : "A", mgmt->sa,
3557 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
3558
3559 if (assoc_data->fils_kek_len &&
3560 fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
3561 return;
3562
3563 pos = mgmt->u.assoc_resp.variable;
David Brazdil0f672f62019-12-10 10:32:29 +00003564 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
3565 mgmt->bssid, assoc_data->bss->bssid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003566
3567 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
3568 elems.timeout_int &&
3569 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
3570 u32 tu, ms;
3571 tu = le32_to_cpu(elems.timeout_int->value);
3572 ms = tu * 1024 / 1000;
3573 sdata_info(sdata,
3574 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
3575 mgmt->sa, tu, ms);
3576 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
3577 assoc_data->timeout_started = true;
3578 if (ms > IEEE80211_ASSOC_TIMEOUT)
3579 run_again(sdata, assoc_data->timeout);
3580 return;
3581 }
3582
3583 bss = assoc_data->bss;
3584
3585 if (status_code != WLAN_STATUS_SUCCESS) {
3586 sdata_info(sdata, "%pM denied association (code=%d)\n",
3587 mgmt->sa, status_code);
3588 ieee80211_destroy_assoc_data(sdata, false, false);
3589 event.u.mlme.status = MLME_DENIED;
3590 event.u.mlme.reason = status_code;
3591 drv_event_callback(sdata->local, sdata, &event);
3592 } else {
3593 if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
3594 /* oops -- internal error -- send timeout for now */
3595 ieee80211_destroy_assoc_data(sdata, false, false);
3596 cfg80211_assoc_timeout(sdata->dev, bss);
3597 return;
3598 }
3599 event.u.mlme.status = MLME_SUCCESS;
3600 drv_event_callback(sdata->local, sdata, &event);
3601 sdata_info(sdata, "associated\n");
3602
3603 /*
3604 * destroy assoc_data afterwards, as otherwise an idle
3605 * recalc after assoc_data is NULL but before associated
3606 * is set can cause the interface to go idle
3607 */
3608 ieee80211_destroy_assoc_data(sdata, true, false);
3609
3610 /* get uapsd queues configuration */
3611 uapsd_queues = 0;
3612 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3613 if (sdata->tx_conf[ac].uapsd)
3614 uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
3615 }
3616
David Brazdil0f672f62019-12-10 10:32:29 +00003617 cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len, uapsd_queues,
3618 ifmgd->assoc_req_ies, ifmgd->assoc_req_ies_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003619}
3620
3621static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
3622 struct ieee80211_mgmt *mgmt, size_t len,
David Brazdil0f672f62019-12-10 10:32:29 +00003623 struct ieee80211_rx_status *rx_status)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003624{
3625 struct ieee80211_local *local = sdata->local;
3626 struct ieee80211_bss *bss;
3627 struct ieee80211_channel *channel;
3628
3629 sdata_assert_lock(sdata);
3630
3631 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
3632 if (!channel)
3633 return;
3634
David Brazdil0f672f62019-12-10 10:32:29 +00003635 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003636 if (bss) {
3637 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
3638 ieee80211_rx_bss_put(local, bss);
3639 }
3640}
3641
3642
3643static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
3644 struct sk_buff *skb)
3645{
3646 struct ieee80211_mgmt *mgmt = (void *)skb->data;
3647 struct ieee80211_if_managed *ifmgd;
3648 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
3649 size_t baselen, len = skb->len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003650
3651 ifmgd = &sdata->u.mgd;
3652
3653 sdata_assert_lock(sdata);
3654
3655 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
3656 return; /* ignore ProbeResp to foreign address */
3657
3658 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
3659 if (baselen > len)
3660 return;
3661
David Brazdil0f672f62019-12-10 10:32:29 +00003662 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003663
3664 if (ifmgd->associated &&
3665 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3666 ieee80211_reset_ap_probe(sdata);
3667}
3668
3669/*
3670 * This is the canonical list of information elements we care about,
3671 * the filter code also gives us all changes to the Microsoft OUI
3672 * (00:50:F2) vendor IE which is used for WMM which we need to track,
3673 * as well as the DTPC IE (part of the Cisco OUI) used for signaling
3674 * changes to requested client power.
3675 *
3676 * We implement beacon filtering in software since that means we can
3677 * avoid processing the frame here and in cfg80211, and userspace
3678 * will not be able to tell whether the hardware supports it or not.
3679 *
3680 * XXX: This list needs to be dynamic -- userspace needs to be able to
3681 * add items it requires. It also needs to be able to tell us to
3682 * look out for other vendor IEs.
3683 */
3684static const u64 care_about_ies =
3685 (1ULL << WLAN_EID_COUNTRY) |
3686 (1ULL << WLAN_EID_ERP_INFO) |
3687 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
3688 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
3689 (1ULL << WLAN_EID_HT_CAPABILITY) |
3690 (1ULL << WLAN_EID_HT_OPERATION) |
3691 (1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
3692
3693static void ieee80211_handle_beacon_sig(struct ieee80211_sub_if_data *sdata,
3694 struct ieee80211_if_managed *ifmgd,
3695 struct ieee80211_bss_conf *bss_conf,
3696 struct ieee80211_local *local,
3697 struct ieee80211_rx_status *rx_status)
3698{
3699 /* Track average RSSI from the Beacon frames of the current AP */
3700
3701 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3702 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
3703 ewma_beacon_signal_init(&ifmgd->ave_beacon_signal);
3704 ifmgd->last_cqm_event_signal = 0;
3705 ifmgd->count_beacon_signal = 1;
3706 ifmgd->last_ave_beacon_signal = 0;
3707 } else {
3708 ifmgd->count_beacon_signal++;
3709 }
3710
3711 ewma_beacon_signal_add(&ifmgd->ave_beacon_signal, -rx_status->signal);
3712
3713 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3714 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3715 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3716 int last_sig = ifmgd->last_ave_beacon_signal;
3717 struct ieee80211_event event = {
3718 .type = RSSI_EVENT,
3719 };
3720
3721 /*
3722 * if signal crosses either of the boundaries, invoke callback
3723 * with appropriate parameters
3724 */
3725 if (sig > ifmgd->rssi_max_thold &&
3726 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3727 ifmgd->last_ave_beacon_signal = sig;
3728 event.u.rssi.data = RSSI_EVENT_HIGH;
3729 drv_event_callback(local, sdata, &event);
3730 } else if (sig < ifmgd->rssi_min_thold &&
3731 (last_sig >= ifmgd->rssi_max_thold ||
3732 last_sig == 0)) {
3733 ifmgd->last_ave_beacon_signal = sig;
3734 event.u.rssi.data = RSSI_EVENT_LOW;
3735 drv_event_callback(local, sdata, &event);
3736 }
3737 }
3738
3739 if (bss_conf->cqm_rssi_thold &&
3740 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
3741 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
3742 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3743 int last_event = ifmgd->last_cqm_event_signal;
3744 int thold = bss_conf->cqm_rssi_thold;
3745 int hyst = bss_conf->cqm_rssi_hyst;
3746
3747 if (sig < thold &&
3748 (last_event == 0 || sig < last_event - hyst)) {
3749 ifmgd->last_cqm_event_signal = sig;
3750 ieee80211_cqm_rssi_notify(
3751 &sdata->vif,
3752 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3753 sig, GFP_KERNEL);
3754 } else if (sig > thold &&
3755 (last_event == 0 || sig > last_event + hyst)) {
3756 ifmgd->last_cqm_event_signal = sig;
3757 ieee80211_cqm_rssi_notify(
3758 &sdata->vif,
3759 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3760 sig, GFP_KERNEL);
3761 }
3762 }
3763
3764 if (bss_conf->cqm_rssi_low &&
3765 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3766 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3767 int last_event = ifmgd->last_cqm_event_signal;
3768 int low = bss_conf->cqm_rssi_low;
3769 int high = bss_conf->cqm_rssi_high;
3770
3771 if (sig < low &&
3772 (last_event == 0 || last_event >= low)) {
3773 ifmgd->last_cqm_event_signal = sig;
3774 ieee80211_cqm_rssi_notify(
3775 &sdata->vif,
3776 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3777 sig, GFP_KERNEL);
3778 } else if (sig > high &&
3779 (last_event == 0 || last_event <= high)) {
3780 ifmgd->last_cqm_event_signal = sig;
3781 ieee80211_cqm_rssi_notify(
3782 &sdata->vif,
3783 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3784 sig, GFP_KERNEL);
3785 }
3786 }
3787}
3788
David Brazdil0f672f62019-12-10 10:32:29 +00003789static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
3790 struct cfg80211_bss *bss)
3791{
3792 if (ether_addr_equal(tx_bssid, bss->bssid))
3793 return true;
3794 if (!bss->transmitted_bss)
3795 return false;
3796 return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
3797}
3798
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003799static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3800 struct ieee80211_mgmt *mgmt, size_t len,
3801 struct ieee80211_rx_status *rx_status)
3802{
3803 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3804 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3805 size_t baselen;
3806 struct ieee802_11_elems elems;
3807 struct ieee80211_local *local = sdata->local;
3808 struct ieee80211_chanctx_conf *chanctx_conf;
3809 struct ieee80211_channel *chan;
3810 struct sta_info *sta;
3811 u32 changed = 0;
3812 bool erp_valid;
3813 u8 erp_value = 0;
3814 u32 ncrc;
3815 u8 *bssid;
3816 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
3817
3818 sdata_assert_lock(sdata);
3819
3820 /* Process beacon from the current BSS */
3821 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
3822 if (baselen > len)
3823 return;
3824
3825 rcu_read_lock();
3826 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3827 if (!chanctx_conf) {
3828 rcu_read_unlock();
3829 return;
3830 }
3831
3832 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
3833 rcu_read_unlock();
3834 return;
3835 }
3836 chan = chanctx_conf->def.chan;
3837 rcu_read_unlock();
3838
3839 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
David Brazdil0f672f62019-12-10 10:32:29 +00003840 ieee80211_rx_our_beacon(mgmt->bssid, ifmgd->assoc_data->bss)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003841 ieee802_11_parse_elems(mgmt->u.beacon.variable,
David Brazdil0f672f62019-12-10 10:32:29 +00003842 len - baselen, false, &elems,
3843 mgmt->bssid,
3844 ifmgd->assoc_data->bss->bssid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003845
David Brazdil0f672f62019-12-10 10:32:29 +00003846 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
3847
3848 if (elems.dtim_period)
3849 ifmgd->dtim_period = elems.dtim_period;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003850 ifmgd->have_beacon = true;
3851 ifmgd->assoc_data->need_beacon = false;
3852 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
3853 sdata->vif.bss_conf.sync_tsf =
3854 le64_to_cpu(mgmt->u.beacon.timestamp);
3855 sdata->vif.bss_conf.sync_device_ts =
3856 rx_status->device_timestamp;
David Brazdil0f672f62019-12-10 10:32:29 +00003857 sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003858 }
David Brazdil0f672f62019-12-10 10:32:29 +00003859
3860 if (elems.mbssid_config_ie)
3861 bss_conf->profile_periodicity =
3862 elems.mbssid_config_ie->profile_periodicity;
3863
3864 if (elems.ext_capab_len >= 11 &&
3865 (elems.ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
3866 bss_conf->ema_ap = true;
3867
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003868 /* continue assoc process */
3869 ifmgd->assoc_data->timeout = jiffies;
3870 ifmgd->assoc_data->timeout_started = true;
3871 run_again(sdata, ifmgd->assoc_data->timeout);
3872 return;
3873 }
3874
3875 if (!ifmgd->associated ||
David Brazdil0f672f62019-12-10 10:32:29 +00003876 !ieee80211_rx_our_beacon(mgmt->bssid, ifmgd->associated))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003877 return;
3878 bssid = ifmgd->associated->bssid;
3879
3880 if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
3881 ieee80211_handle_beacon_sig(sdata, ifmgd, bss_conf,
3882 local, rx_status);
3883
3884 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
3885 mlme_dbg_ratelimited(sdata,
3886 "cancelling AP probe due to a received beacon\n");
3887 ieee80211_reset_ap_probe(sdata);
3888 }
3889
3890 /*
3891 * Push the beacon loss detection into the future since
3892 * we are processing a beacon from the AP just now.
3893 */
3894 ieee80211_sta_reset_beacon_monitor(sdata);
3895
3896 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
3897 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
3898 len - baselen, false, &elems,
David Brazdil0f672f62019-12-10 10:32:29 +00003899 care_about_ies, ncrc,
3900 mgmt->bssid, bssid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003901
3902 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
3903 ieee80211_check_tim(elems.tim, elems.tim_len, ifmgd->aid)) {
3904 if (local->hw.conf.dynamic_ps_timeout > 0) {
3905 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
3906 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
3907 ieee80211_hw_config(local,
3908 IEEE80211_CONF_CHANGE_PS);
3909 }
3910 ieee80211_send_nullfunc(local, sdata, false);
3911 } else if (!local->pspolling && sdata->u.mgd.powersave) {
3912 local->pspolling = true;
3913
3914 /*
3915 * Here is assumed that the driver will be
3916 * able to send ps-poll frame and receive a
3917 * response even though power save mode is
3918 * enabled, but some drivers might require
3919 * to disable power save here. This needs
3920 * to be investigated.
3921 */
3922 ieee80211_send_pspoll(local, sdata);
3923 }
3924 }
3925
3926 if (sdata->vif.p2p ||
3927 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
3928 struct ieee80211_p2p_noa_attr noa = {};
3929 int ret;
3930
3931 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
3932 len - baselen,
3933 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
3934 (u8 *) &noa, sizeof(noa));
3935 if (ret >= 2) {
3936 if (sdata->u.mgd.p2p_noa_index != noa.index) {
3937 /* valid noa_attr and index changed */
3938 sdata->u.mgd.p2p_noa_index = noa.index;
3939 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
3940 changed |= BSS_CHANGED_P2P_PS;
3941 /*
3942 * make sure we update all information, the CRC
3943 * mechanism doesn't look at P2P attributes.
3944 */
3945 ifmgd->beacon_crc_valid = false;
3946 }
3947 } else if (sdata->u.mgd.p2p_noa_index != -1) {
3948 /* noa_attr not found and we had valid noa_attr before */
3949 sdata->u.mgd.p2p_noa_index = -1;
3950 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
3951 changed |= BSS_CHANGED_P2P_PS;
3952 ifmgd->beacon_crc_valid = false;
3953 }
3954 }
3955
3956 if (ifmgd->csa_waiting_bcn)
3957 ieee80211_chswitch_post_beacon(sdata);
3958
3959 /*
3960 * Update beacon timing and dtim count on every beacon appearance. This
3961 * will allow the driver to use the most updated values. Do it before
3962 * comparing this one with last received beacon.
3963 * IMPORTANT: These parameters would possibly be out of sync by the time
3964 * the driver will use them. The synchronized view is currently
3965 * guaranteed only in certain callbacks.
3966 */
3967 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
3968 sdata->vif.bss_conf.sync_tsf =
3969 le64_to_cpu(mgmt->u.beacon.timestamp);
3970 sdata->vif.bss_conf.sync_device_ts =
3971 rx_status->device_timestamp;
David Brazdil0f672f62019-12-10 10:32:29 +00003972 sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003973 }
3974
3975 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
3976 return;
3977 ifmgd->beacon_crc = ncrc;
3978 ifmgd->beacon_crc_valid = true;
3979
David Brazdil0f672f62019-12-10 10:32:29 +00003980 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003981
3982 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
3983 rx_status->device_timestamp,
3984 &elems, true);
3985
3986 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
3987 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3988 elems.wmm_param_len,
3989 elems.mu_edca_param_set))
3990 changed |= BSS_CHANGED_QOS;
3991
3992 /*
3993 * If we haven't had a beacon before, tell the driver about the
3994 * DTIM period (and beacon timing if desired) now.
3995 */
3996 if (!ifmgd->have_beacon) {
3997 /* a few bogus AP send dtim_period = 0 or no TIM IE */
David Brazdil0f672f62019-12-10 10:32:29 +00003998 bss_conf->dtim_period = elems.dtim_period ?: 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003999
4000 changed |= BSS_CHANGED_BEACON_INFO;
4001 ifmgd->have_beacon = true;
4002
4003 mutex_lock(&local->iflist_mtx);
4004 ieee80211_recalc_ps(local);
4005 mutex_unlock(&local->iflist_mtx);
4006
4007 ieee80211_recalc_ps_vif(sdata);
4008 }
4009
4010 if (elems.erp_info) {
4011 erp_valid = true;
4012 erp_value = elems.erp_info[0];
4013 } else {
4014 erp_valid = false;
4015 }
4016 changed |= ieee80211_handle_bss_capability(sdata,
4017 le16_to_cpu(mgmt->u.beacon.capab_info),
4018 erp_valid, erp_value);
4019
4020 mutex_lock(&local->sta_mtx);
4021 sta = sta_info_get(sdata, bssid);
4022
David Brazdil0f672f62019-12-10 10:32:29 +00004023 changed |= ieee80211_recalc_twt_req(sdata, sta, &elems);
4024
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004025 if (ieee80211_config_bw(sdata, sta,
4026 elems.ht_cap_elem, elems.ht_operation,
4027 elems.vht_operation, elems.he_operation,
4028 bssid, &changed)) {
4029 mutex_unlock(&local->sta_mtx);
4030 sdata_info(sdata,
4031 "failed to follow AP %pM bandwidth change, disconnect\n",
4032 bssid);
4033 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4034 WLAN_REASON_DEAUTH_LEAVING,
4035 true, deauth_buf);
4036 ieee80211_report_disconnect(sdata, deauth_buf,
4037 sizeof(deauth_buf), true,
4038 WLAN_REASON_DEAUTH_LEAVING);
4039 return;
4040 }
4041
4042 if (sta && elems.opmode_notif)
4043 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
4044 rx_status->band);
4045 mutex_unlock(&local->sta_mtx);
4046
4047 changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
4048 elems.country_elem,
4049 elems.country_elem_len,
4050 elems.pwr_constr_elem,
4051 elems.cisco_dtpc_elem);
4052
4053 ieee80211_bss_info_change_notify(sdata, changed);
4054}
4055
4056void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
4057 struct sk_buff *skb)
4058{
4059 struct ieee80211_rx_status *rx_status;
4060 struct ieee80211_mgmt *mgmt;
4061 u16 fc;
4062 struct ieee802_11_elems elems;
4063 int ies_len;
4064
4065 rx_status = (struct ieee80211_rx_status *) skb->cb;
4066 mgmt = (struct ieee80211_mgmt *) skb->data;
4067 fc = le16_to_cpu(mgmt->frame_control);
4068
4069 sdata_lock(sdata);
4070
4071 switch (fc & IEEE80211_FCTL_STYPE) {
4072 case IEEE80211_STYPE_BEACON:
4073 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
4074 break;
4075 case IEEE80211_STYPE_PROBE_RESP:
4076 ieee80211_rx_mgmt_probe_resp(sdata, skb);
4077 break;
4078 case IEEE80211_STYPE_AUTH:
4079 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
4080 break;
4081 case IEEE80211_STYPE_DEAUTH:
4082 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
4083 break;
4084 case IEEE80211_STYPE_DISASSOC:
4085 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
4086 break;
4087 case IEEE80211_STYPE_ASSOC_RESP:
4088 case IEEE80211_STYPE_REASSOC_RESP:
4089 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
4090 break;
4091 case IEEE80211_STYPE_ACTION:
4092 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
4093 ies_len = skb->len -
4094 offsetof(struct ieee80211_mgmt,
4095 u.action.u.chan_switch.variable);
4096
4097 if (ies_len < 0)
4098 break;
4099
David Brazdil0f672f62019-12-10 10:32:29 +00004100 /* CSA IE cannot be overridden, no need for BSSID */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004101 ieee802_11_parse_elems(
4102 mgmt->u.action.u.chan_switch.variable,
David Brazdil0f672f62019-12-10 10:32:29 +00004103 ies_len, true, &elems, mgmt->bssid, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004104
4105 if (elems.parse_error)
4106 break;
4107
4108 ieee80211_sta_process_chanswitch(sdata,
4109 rx_status->mactime,
4110 rx_status->device_timestamp,
4111 &elems, false);
4112 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
4113 ies_len = skb->len -
4114 offsetof(struct ieee80211_mgmt,
4115 u.action.u.ext_chan_switch.variable);
4116
4117 if (ies_len < 0)
4118 break;
4119
David Brazdil0f672f62019-12-10 10:32:29 +00004120 /*
4121 * extended CSA IE can't be overridden, no need for
4122 * BSSID
4123 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004124 ieee802_11_parse_elems(
4125 mgmt->u.action.u.ext_chan_switch.variable,
David Brazdil0f672f62019-12-10 10:32:29 +00004126 ies_len, true, &elems, mgmt->bssid, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004127
4128 if (elems.parse_error)
4129 break;
4130
4131 /* for the handling code pretend this was also an IE */
4132 elems.ext_chansw_ie =
4133 &mgmt->u.action.u.ext_chan_switch.data;
4134
4135 ieee80211_sta_process_chanswitch(sdata,
4136 rx_status->mactime,
4137 rx_status->device_timestamp,
4138 &elems, false);
4139 }
4140 break;
4141 }
4142 sdata_unlock(sdata);
4143}
4144
4145static void ieee80211_sta_timer(struct timer_list *t)
4146{
4147 struct ieee80211_sub_if_data *sdata =
4148 from_timer(sdata, t, u.mgd.timer);
4149
4150 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
4151}
4152
4153static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
4154 u8 *bssid, u8 reason, bool tx)
4155{
4156 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4157
4158 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
4159 tx, frame_buf);
4160
4161 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
4162 reason);
4163}
4164
4165static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
4166{
4167 struct ieee80211_local *local = sdata->local;
4168 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4169 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
4170 u32 tx_flags = 0;
4171 u16 trans = 1;
4172 u16 status = 0;
4173 u16 prepare_tx_duration = 0;
4174
4175 sdata_assert_lock(sdata);
4176
4177 if (WARN_ON_ONCE(!auth_data))
4178 return -EINVAL;
4179
4180 auth_data->tries++;
4181
4182 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
4183 sdata_info(sdata, "authentication with %pM timed out\n",
4184 auth_data->bss->bssid);
4185
4186 /*
4187 * Most likely AP is not in the range so remove the
4188 * bss struct for that AP.
4189 */
4190 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
4191
4192 return -ETIMEDOUT;
4193 }
4194
4195 if (auth_data->algorithm == WLAN_AUTH_SAE)
4196 prepare_tx_duration =
4197 jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
4198
4199 drv_mgd_prepare_tx(local, sdata, prepare_tx_duration);
4200
4201 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
4202 auth_data->bss->bssid, auth_data->tries,
4203 IEEE80211_AUTH_MAX_TRIES);
4204
4205 auth_data->expected_transaction = 2;
4206
4207 if (auth_data->algorithm == WLAN_AUTH_SAE) {
4208 trans = auth_data->sae_trans;
4209 status = auth_data->sae_status;
4210 auth_data->expected_transaction = trans;
4211 }
4212
4213 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4214 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4215 IEEE80211_TX_INTFL_MLME_CONN_TX;
4216
4217 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
4218 auth_data->data, auth_data->data_len,
4219 auth_data->bss->bssid,
4220 auth_data->bss->bssid, NULL, 0, 0,
4221 tx_flags);
4222
4223 if (tx_flags == 0) {
4224 if (auth_data->algorithm == WLAN_AUTH_SAE)
4225 auth_data->timeout = jiffies +
4226 IEEE80211_AUTH_TIMEOUT_SAE;
4227 else
4228 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
4229 } else {
4230 auth_data->timeout =
4231 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
4232 }
4233
4234 auth_data->timeout_started = true;
4235 run_again(sdata, auth_data->timeout);
4236
4237 return 0;
4238}
4239
4240static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
4241{
4242 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4243 struct ieee80211_local *local = sdata->local;
4244
4245 sdata_assert_lock(sdata);
4246
4247 assoc_data->tries++;
4248 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
4249 sdata_info(sdata, "association with %pM timed out\n",
4250 assoc_data->bss->bssid);
4251
4252 /*
4253 * Most likely AP is not in the range so remove the
4254 * bss struct for that AP.
4255 */
4256 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
4257
4258 return -ETIMEDOUT;
4259 }
4260
4261 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
4262 assoc_data->bss->bssid, assoc_data->tries,
4263 IEEE80211_ASSOC_MAX_TRIES);
4264 ieee80211_send_assoc(sdata);
4265
4266 if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
4267 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
4268 assoc_data->timeout_started = true;
4269 run_again(sdata, assoc_data->timeout);
4270 } else {
4271 assoc_data->timeout =
4272 round_jiffies_up(jiffies +
4273 IEEE80211_ASSOC_TIMEOUT_LONG);
4274 assoc_data->timeout_started = true;
4275 run_again(sdata, assoc_data->timeout);
4276 }
4277
4278 return 0;
4279}
4280
4281void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
4282 __le16 fc, bool acked)
4283{
4284 struct ieee80211_local *local = sdata->local;
4285
4286 sdata->u.mgd.status_fc = fc;
4287 sdata->u.mgd.status_acked = acked;
4288 sdata->u.mgd.status_received = true;
4289
4290 ieee80211_queue_work(&local->hw, &sdata->work);
4291}
4292
4293void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
4294{
4295 struct ieee80211_local *local = sdata->local;
4296 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4297
4298 sdata_lock(sdata);
4299
4300 if (ifmgd->status_received) {
4301 __le16 fc = ifmgd->status_fc;
4302 bool status_acked = ifmgd->status_acked;
4303
4304 ifmgd->status_received = false;
4305 if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
4306 if (status_acked) {
4307 if (ifmgd->auth_data->algorithm ==
4308 WLAN_AUTH_SAE)
4309 ifmgd->auth_data->timeout =
4310 jiffies +
4311 IEEE80211_AUTH_TIMEOUT_SAE;
4312 else
4313 ifmgd->auth_data->timeout =
4314 jiffies +
4315 IEEE80211_AUTH_TIMEOUT_SHORT;
4316 run_again(sdata, ifmgd->auth_data->timeout);
4317 } else {
4318 ifmgd->auth_data->timeout = jiffies - 1;
4319 }
4320 ifmgd->auth_data->timeout_started = true;
4321 } else if (ifmgd->assoc_data &&
4322 (ieee80211_is_assoc_req(fc) ||
4323 ieee80211_is_reassoc_req(fc))) {
4324 if (status_acked) {
4325 ifmgd->assoc_data->timeout =
4326 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
4327 run_again(sdata, ifmgd->assoc_data->timeout);
4328 } else {
4329 ifmgd->assoc_data->timeout = jiffies - 1;
4330 }
4331 ifmgd->assoc_data->timeout_started = true;
4332 }
4333 }
4334
4335 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
4336 time_after(jiffies, ifmgd->auth_data->timeout)) {
4337 if (ifmgd->auth_data->done) {
4338 /*
4339 * ok ... we waited for assoc but userspace didn't,
4340 * so let's just kill the auth data
4341 */
4342 ieee80211_destroy_auth_data(sdata, false);
4343 } else if (ieee80211_auth(sdata)) {
4344 u8 bssid[ETH_ALEN];
4345 struct ieee80211_event event = {
4346 .type = MLME_EVENT,
4347 .u.mlme.data = AUTH_EVENT,
4348 .u.mlme.status = MLME_TIMEOUT,
4349 };
4350
4351 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
4352
4353 ieee80211_destroy_auth_data(sdata, false);
4354
4355 cfg80211_auth_timeout(sdata->dev, bssid);
4356 drv_event_callback(sdata->local, sdata, &event);
4357 }
4358 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
4359 run_again(sdata, ifmgd->auth_data->timeout);
4360
4361 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
4362 time_after(jiffies, ifmgd->assoc_data->timeout)) {
4363 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
4364 ieee80211_do_assoc(sdata)) {
4365 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
4366 struct ieee80211_event event = {
4367 .type = MLME_EVENT,
4368 .u.mlme.data = ASSOC_EVENT,
4369 .u.mlme.status = MLME_TIMEOUT,
4370 };
4371
4372 ieee80211_destroy_assoc_data(sdata, false, false);
4373 cfg80211_assoc_timeout(sdata->dev, bss);
4374 drv_event_callback(sdata->local, sdata, &event);
4375 }
4376 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
4377 run_again(sdata, ifmgd->assoc_data->timeout);
4378
4379 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
4380 ifmgd->associated) {
4381 u8 bssid[ETH_ALEN];
4382 int max_tries;
4383
4384 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4385
4386 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4387 max_tries = max_nullfunc_tries;
4388 else
4389 max_tries = max_probe_tries;
4390
4391 /* ACK received for nullfunc probing frame */
4392 if (!ifmgd->probe_send_count)
4393 ieee80211_reset_ap_probe(sdata);
4394 else if (ifmgd->nullfunc_failed) {
4395 if (ifmgd->probe_send_count < max_tries) {
4396 mlme_dbg(sdata,
4397 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
4398 bssid, ifmgd->probe_send_count,
4399 max_tries);
4400 ieee80211_mgd_probe_ap_send(sdata);
4401 } else {
4402 mlme_dbg(sdata,
4403 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
4404 bssid);
4405 ieee80211_sta_connection_lost(sdata, bssid,
4406 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4407 false);
4408 }
4409 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
4410 run_again(sdata, ifmgd->probe_timeout);
4411 else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
4412 mlme_dbg(sdata,
4413 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
4414 bssid, probe_wait_ms);
4415 ieee80211_sta_connection_lost(sdata, bssid,
4416 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
4417 } else if (ifmgd->probe_send_count < max_tries) {
4418 mlme_dbg(sdata,
4419 "No probe response from AP %pM after %dms, try %d/%i\n",
4420 bssid, probe_wait_ms,
4421 ifmgd->probe_send_count, max_tries);
4422 ieee80211_mgd_probe_ap_send(sdata);
4423 } else {
4424 /*
4425 * We actually lost the connection ... or did we?
4426 * Let's make sure!
4427 */
4428 mlme_dbg(sdata,
4429 "No probe response from AP %pM after %dms, disconnecting.\n",
4430 bssid, probe_wait_ms);
4431
4432 ieee80211_sta_connection_lost(sdata, bssid,
4433 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
4434 }
4435 }
4436
4437 sdata_unlock(sdata);
4438}
4439
4440static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
4441{
4442 struct ieee80211_sub_if_data *sdata =
4443 from_timer(sdata, t, u.mgd.bcn_mon_timer);
4444 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4445
4446 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
4447 return;
4448
4449 sdata->u.mgd.connection_loss = false;
4450 ieee80211_queue_work(&sdata->local->hw,
4451 &sdata->u.mgd.beacon_connection_loss_work);
4452}
4453
4454static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
4455{
4456 struct ieee80211_sub_if_data *sdata =
4457 from_timer(sdata, t, u.mgd.conn_mon_timer);
4458 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4459 struct ieee80211_local *local = sdata->local;
4460
4461 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
4462 return;
4463
4464 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
4465}
4466
4467static void ieee80211_sta_monitor_work(struct work_struct *work)
4468{
4469 struct ieee80211_sub_if_data *sdata =
4470 container_of(work, struct ieee80211_sub_if_data,
4471 u.mgd.monitor_work);
4472
4473 ieee80211_mgd_probe_ap(sdata, false);
4474}
4475
4476static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
4477{
4478 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
4479 __ieee80211_stop_poll(sdata);
4480
4481 /* let's probe the connection once */
4482 if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
4483 ieee80211_queue_work(&sdata->local->hw,
4484 &sdata->u.mgd.monitor_work);
4485 }
4486}
4487
4488#ifdef CONFIG_PM
4489void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
4490{
4491 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4492 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4493
4494 sdata_lock(sdata);
4495
4496 if (ifmgd->auth_data || ifmgd->assoc_data) {
4497 const u8 *bssid = ifmgd->auth_data ?
4498 ifmgd->auth_data->bss->bssid :
4499 ifmgd->assoc_data->bss->bssid;
4500
4501 /*
4502 * If we are trying to authenticate / associate while suspending,
4503 * cfg80211 won't know and won't actually abort those attempts,
4504 * thus we need to do that ourselves.
4505 */
David Brazdil0f672f62019-12-10 10:32:29 +00004506 ieee80211_send_deauth_disassoc(sdata, bssid, bssid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004507 IEEE80211_STYPE_DEAUTH,
4508 WLAN_REASON_DEAUTH_LEAVING,
4509 false, frame_buf);
4510 if (ifmgd->assoc_data)
4511 ieee80211_destroy_assoc_data(sdata, false, true);
4512 if (ifmgd->auth_data)
4513 ieee80211_destroy_auth_data(sdata, false);
4514 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4515 IEEE80211_DEAUTH_FRAME_LEN);
4516 }
4517
4518 /* This is a bit of a hack - we should find a better and more generic
4519 * solution to this. Normally when suspending, cfg80211 will in fact
4520 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
4521 * auth (not so important) or assoc (this is the problem) process.
4522 *
4523 * As a consequence, it can happen that we are in the process of both
4524 * associating and suspending, and receive an association response
4525 * after cfg80211 has checked if it needs to disconnect, but before
4526 * we actually set the flag to drop incoming frames. This will then
4527 * cause the workqueue flush to process the association response in
4528 * the suspend, resulting in a successful association just before it
4529 * tries to remove the interface from the driver, which now though
4530 * has a channel context assigned ... this results in issues.
4531 *
4532 * To work around this (for now) simply deauth here again if we're
4533 * now connected.
4534 */
4535 if (ifmgd->associated && !sdata->local->wowlan) {
4536 u8 bssid[ETH_ALEN];
4537 struct cfg80211_deauth_request req = {
4538 .reason_code = WLAN_REASON_DEAUTH_LEAVING,
4539 .bssid = bssid,
4540 };
4541
4542 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4543 ieee80211_mgd_deauth(sdata, &req);
4544 }
4545
4546 sdata_unlock(sdata);
4547}
4548
4549void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
4550{
4551 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4552
4553 sdata_lock(sdata);
4554 if (!ifmgd->associated) {
4555 sdata_unlock(sdata);
4556 return;
4557 }
4558
4559 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
4560 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
4561 mlme_dbg(sdata, "driver requested disconnect after resume\n");
4562 ieee80211_sta_connection_lost(sdata,
4563 ifmgd->associated->bssid,
4564 WLAN_REASON_UNSPECIFIED,
4565 true);
4566 sdata_unlock(sdata);
4567 return;
4568 }
4569 sdata_unlock(sdata);
4570}
4571#endif
4572
4573/* interface setup */
4574void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
4575{
4576 struct ieee80211_if_managed *ifmgd;
4577
4578 ifmgd = &sdata->u.mgd;
4579 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
4580 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
4581 INIT_WORK(&ifmgd->beacon_connection_loss_work,
4582 ieee80211_beacon_connection_loss_work);
4583 INIT_WORK(&ifmgd->csa_connection_drop_work,
4584 ieee80211_csa_connection_drop_work);
4585 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
4586 INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
4587 ieee80211_tdls_peer_del_work);
4588 timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
4589 timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
4590 timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
4591 timer_setup(&ifmgd->chswitch_timer, ieee80211_chswitch_timer, 0);
4592 INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
4593 ieee80211_sta_handle_tspec_ac_params_wk);
4594
4595 ifmgd->flags = 0;
4596 ifmgd->powersave = sdata->wdev.ps;
4597 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
4598 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
4599 ifmgd->p2p_noa_index = -1;
4600
4601 if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
4602 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
4603 else
4604 ifmgd->req_smps = IEEE80211_SMPS_OFF;
4605
4606 /* Setup TDLS data */
4607 spin_lock_init(&ifmgd->teardown_lock);
4608 ifmgd->teardown_skb = NULL;
4609 ifmgd->orig_teardown_skb = NULL;
4610}
4611
4612/* scan finished notification */
4613void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
4614{
4615 struct ieee80211_sub_if_data *sdata;
4616
4617 /* Restart STA timers */
4618 rcu_read_lock();
4619 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4620 if (ieee80211_sdata_running(sdata))
4621 ieee80211_restart_sta_timer(sdata);
4622 }
4623 rcu_read_unlock();
4624}
4625
4626static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
4627 struct cfg80211_bss *cbss)
4628{
4629 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4630 const u8 *ht_cap_ie, *vht_cap_ie;
4631 const struct ieee80211_ht_cap *ht_cap;
4632 const struct ieee80211_vht_cap *vht_cap;
4633 u8 chains = 1;
4634
4635 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
4636 return chains;
4637
4638 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
4639 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
4640 ht_cap = (void *)(ht_cap_ie + 2);
4641 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
4642 /*
4643 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
4644 * "Tx Unequal Modulation Supported" fields.
4645 */
4646 }
4647
4648 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
4649 return chains;
4650
4651 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
4652 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
4653 u8 nss;
4654 u16 tx_mcs_map;
4655
4656 vht_cap = (void *)(vht_cap_ie + 2);
4657 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
4658 for (nss = 8; nss > 0; nss--) {
4659 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
4660 IEEE80211_VHT_MCS_NOT_SUPPORTED)
4661 break;
4662 }
4663 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
4664 chains = max(chains, nss);
4665 }
4666
4667 return chains;
4668}
4669
4670static bool
4671ieee80211_verify_sta_he_mcs_support(struct ieee80211_supported_band *sband,
4672 const struct ieee80211_he_operation *he_op)
4673{
4674 const struct ieee80211_sta_he_cap *sta_he_cap =
4675 ieee80211_get_he_sta_cap(sband);
4676 u16 ap_min_req_set;
4677 int i;
4678
4679 if (!sta_he_cap || !he_op)
4680 return false;
4681
4682 ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
4683
4684 /* Need to go over for 80MHz, 160MHz and for 80+80 */
4685 for (i = 0; i < 3; i++) {
4686 const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
4687 &sta_he_cap->he_mcs_nss_supp;
4688 u16 sta_mcs_map_rx =
4689 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
4690 u16 sta_mcs_map_tx =
4691 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
4692 u8 nss;
4693 bool verified = true;
4694
4695 /*
4696 * For each band there is a maximum of 8 spatial streams
4697 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
4698 * of 2 bits per NSS (1-8), with the values defined in enum
4699 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
4700 * capabilities aren't less than the AP's minimum requirements
4701 * for this HE BSS per SS.
4702 * It is enough to find one such band that meets the reqs.
4703 */
4704 for (nss = 8; nss > 0; nss--) {
4705 u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
4706 u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
4707 u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
4708
4709 if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
4710 continue;
4711
4712 /*
4713 * Make sure the HE AP doesn't require MCSs that aren't
4714 * supported by the client
4715 */
4716 if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4717 sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4718 (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
4719 verified = false;
4720 break;
4721 }
4722 }
4723
4724 if (verified)
4725 return true;
4726 }
4727
4728 /* If here, STA doesn't meet AP's HE min requirements */
4729 return false;
4730}
4731
4732static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
4733 struct cfg80211_bss *cbss)
4734{
4735 struct ieee80211_local *local = sdata->local;
4736 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4737 const struct ieee80211_ht_cap *ht_cap = NULL;
4738 const struct ieee80211_ht_operation *ht_oper = NULL;
4739 const struct ieee80211_vht_operation *vht_oper = NULL;
4740 const struct ieee80211_he_operation *he_oper = NULL;
4741 struct ieee80211_supported_band *sband;
4742 struct cfg80211_chan_def chandef;
4743 int ret;
4744 u32 i;
4745 bool have_80mhz;
4746
4747 sband = local->hw.wiphy->bands[cbss->channel->band];
4748
4749 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
4750 IEEE80211_STA_DISABLE_80P80MHZ |
4751 IEEE80211_STA_DISABLE_160MHZ);
4752
4753 rcu_read_lock();
4754
4755 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
4756 sband->ht_cap.ht_supported) {
4757 const u8 *ht_oper_ie, *ht_cap_ie;
4758
4759 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
4760 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
4761 ht_oper = (void *)(ht_oper_ie + 2);
4762
4763 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
4764 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap))
4765 ht_cap = (void *)(ht_cap_ie + 2);
4766
4767 if (!ht_cap) {
4768 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4769 ht_oper = NULL;
4770 }
4771 }
4772
4773 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
4774 sband->vht_cap.vht_supported) {
4775 const u8 *vht_oper_ie, *vht_cap;
4776
4777 vht_oper_ie = ieee80211_bss_get_ie(cbss,
4778 WLAN_EID_VHT_OPERATION);
4779 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
4780 vht_oper = (void *)(vht_oper_ie + 2);
4781 if (vht_oper && !ht_oper) {
4782 vht_oper = NULL;
4783 sdata_info(sdata,
4784 "AP advertised VHT without HT, disabling both\n");
4785 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4786 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4787 }
4788
4789 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
4790 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
4791 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4792 vht_oper = NULL;
4793 }
4794 }
4795
David Brazdil0f672f62019-12-10 10:32:29 +00004796 if (!ieee80211_get_he_sta_cap(sband))
4797 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4798
4799 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004800 const struct cfg80211_bss_ies *ies;
4801 const u8 *he_oper_ie;
4802
4803 ies = rcu_dereference(cbss->ies);
4804 he_oper_ie = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_OPERATION,
4805 ies->data, ies->len);
4806 if (he_oper_ie &&
4807 he_oper_ie[1] == ieee80211_he_oper_size(&he_oper_ie[3]))
4808 he_oper = (void *)(he_oper_ie + 3);
4809 else
4810 he_oper = NULL;
4811
4812 if (!ieee80211_verify_sta_he_mcs_support(sband, he_oper))
4813 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4814 }
4815
4816 /* Allow VHT if at least one channel on the sband supports 80 MHz */
4817 have_80mhz = false;
4818 for (i = 0; i < sband->n_channels; i++) {
4819 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
4820 IEEE80211_CHAN_NO_80MHZ))
4821 continue;
4822
4823 have_80mhz = true;
4824 break;
4825 }
4826
4827 if (!have_80mhz)
4828 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4829
4830 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
4831 cbss->channel,
4832 ht_oper, vht_oper, he_oper,
4833 &chandef, false);
4834
4835 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
4836 local->rx_chains);
4837
4838 rcu_read_unlock();
4839
4840 /* will change later if needed */
4841 sdata->smps_mode = IEEE80211_SMPS_OFF;
4842
4843 mutex_lock(&local->mtx);
4844 /*
4845 * If this fails (possibly due to channel context sharing
4846 * on incompatible channels, e.g. 80+80 and 160 sharing the
4847 * same control channel) try to use a smaller bandwidth.
4848 */
4849 ret = ieee80211_vif_use_channel(sdata, &chandef,
4850 IEEE80211_CHANCTX_SHARED);
4851
4852 /* don't downgrade for 5 and 10 MHz channels, though. */
4853 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
4854 chandef.width == NL80211_CHAN_WIDTH_10)
4855 goto out;
4856
4857 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
4858 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
4859 ret = ieee80211_vif_use_channel(sdata, &chandef,
4860 IEEE80211_CHANCTX_SHARED);
4861 }
4862 out:
4863 mutex_unlock(&local->mtx);
4864 return ret;
4865}
4866
David Brazdil0f672f62019-12-10 10:32:29 +00004867static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
4868 u8 *dtim_count, u8 *dtim_period)
4869{
4870 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
4871 const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
4872 ies->len);
4873 const struct ieee80211_tim_ie *tim = NULL;
4874 const struct ieee80211_bssid_index *idx;
4875 bool valid = tim_ie && tim_ie[1] >= 2;
4876
4877 if (valid)
4878 tim = (void *)(tim_ie + 2);
4879
4880 if (dtim_count)
4881 *dtim_count = valid ? tim->dtim_count : 0;
4882
4883 if (dtim_period)
4884 *dtim_period = valid ? tim->dtim_period : 0;
4885
4886 /* Check if value is overridden by non-transmitted profile */
4887 if (!idx_ie || idx_ie[1] < 3)
4888 return valid;
4889
4890 idx = (void *)(idx_ie + 2);
4891
4892 if (dtim_count)
4893 *dtim_count = idx->dtim_count;
4894
4895 if (dtim_period)
4896 *dtim_period = idx->dtim_period;
4897
4898 return true;
4899}
4900
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004901static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
4902 struct cfg80211_bss *cbss, bool assoc,
4903 bool override)
4904{
4905 struct ieee80211_local *local = sdata->local;
4906 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4907 struct ieee80211_bss *bss = (void *)cbss->priv;
4908 struct sta_info *new_sta = NULL;
4909 struct ieee80211_supported_band *sband;
4910 bool have_sta = false;
4911 int err;
4912
4913 sband = local->hw.wiphy->bands[cbss->channel->band];
4914
4915 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
4916 return -EINVAL;
4917
4918 /* If a reconfig is happening, bail out */
4919 if (local->in_reconfig)
4920 return -EBUSY;
4921
4922 if (assoc) {
4923 rcu_read_lock();
4924 have_sta = sta_info_get(sdata, cbss->bssid);
4925 rcu_read_unlock();
4926 }
4927
4928 if (!have_sta) {
4929 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
4930 if (!new_sta)
4931 return -ENOMEM;
4932 }
4933
4934 /*
4935 * Set up the information for the new channel before setting the
4936 * new channel. We can't - completely race-free - change the basic
4937 * rates bitmap and the channel (sband) that it refers to, but if
4938 * we set it up before we at least avoid calling into the driver's
4939 * bss_info_changed() method with invalid information (since we do
4940 * call that from changing the channel - only for IDLE and perhaps
4941 * some others, but ...).
4942 *
4943 * So to avoid that, just set up all the new information before the
4944 * channel, but tell the driver to apply it only afterwards, since
4945 * it might need the new channel for that.
4946 */
4947 if (new_sta) {
4948 u32 rates = 0, basic_rates = 0;
4949 bool have_higher_than_11mbit;
4950 int min_rate = INT_MAX, min_rate_index = -1;
4951 const struct cfg80211_bss_ies *ies;
4952 int shift = ieee80211_vif_get_shift(&sdata->vif);
4953
4954 ieee80211_get_rates(sband, bss->supp_rates,
4955 bss->supp_rates_len,
4956 &rates, &basic_rates,
4957 &have_higher_than_11mbit,
4958 &min_rate, &min_rate_index,
4959 shift);
4960
4961 /*
4962 * This used to be a workaround for basic rates missing
4963 * in the association response frame. Now that we no
4964 * longer use the basic rates from there, it probably
4965 * doesn't happen any more, but keep the workaround so
4966 * in case some *other* APs are buggy in different ways
4967 * we can connect -- with a warning.
4968 */
4969 if (!basic_rates && min_rate_index >= 0) {
4970 sdata_info(sdata,
4971 "No basic rates, using min rate instead\n");
4972 basic_rates = BIT(min_rate_index);
4973 }
4974
David Brazdil0f672f62019-12-10 10:32:29 +00004975 if (rates)
4976 new_sta->sta.supp_rates[cbss->channel->band] = rates;
4977 else
4978 sdata_info(sdata,
4979 "No rates found, keeping mandatory only\n");
4980
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004981 sdata->vif.bss_conf.basic_rates = basic_rates;
4982
4983 /* cf. IEEE 802.11 9.2.12 */
4984 if (cbss->channel->band == NL80211_BAND_2GHZ &&
4985 have_higher_than_11mbit)
4986 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
4987 else
4988 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
4989
4990 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
4991
4992 /* set timing information */
4993 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
4994 rcu_read_lock();
4995 ies = rcu_dereference(cbss->beacon_ies);
4996 if (ies) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004997 sdata->vif.bss_conf.sync_tsf = ies->tsf;
4998 sdata->vif.bss_conf.sync_device_ts =
4999 bss->device_ts_beacon;
David Brazdil0f672f62019-12-10 10:32:29 +00005000
5001 ieee80211_get_dtim(ies,
5002 &sdata->vif.bss_conf.sync_dtim_count,
5003 NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005004 } else if (!ieee80211_hw_check(&sdata->local->hw,
5005 TIMING_BEACON_ONLY)) {
5006 ies = rcu_dereference(cbss->proberesp_ies);
5007 /* must be non-NULL since beacon IEs were NULL */
5008 sdata->vif.bss_conf.sync_tsf = ies->tsf;
5009 sdata->vif.bss_conf.sync_device_ts =
5010 bss->device_ts_presp;
5011 sdata->vif.bss_conf.sync_dtim_count = 0;
5012 } else {
5013 sdata->vif.bss_conf.sync_tsf = 0;
5014 sdata->vif.bss_conf.sync_device_ts = 0;
5015 sdata->vif.bss_conf.sync_dtim_count = 0;
5016 }
5017 rcu_read_unlock();
5018 }
5019
5020 if (new_sta || override) {
5021 err = ieee80211_prep_channel(sdata, cbss);
5022 if (err) {
5023 if (new_sta)
5024 sta_info_free(local, new_sta);
5025 return -EINVAL;
5026 }
5027 }
5028
5029 if (new_sta) {
5030 /*
5031 * tell driver about BSSID, basic rates and timing
5032 * this was set up above, before setting the channel
5033 */
5034 ieee80211_bss_info_change_notify(sdata,
5035 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
5036 BSS_CHANGED_BEACON_INT);
5037
5038 if (assoc)
5039 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
5040
5041 err = sta_info_insert(new_sta);
5042 new_sta = NULL;
5043 if (err) {
5044 sdata_info(sdata,
5045 "failed to insert STA entry for the AP (error %d)\n",
5046 err);
5047 return err;
5048 }
5049 } else
5050 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
5051
5052 /* Cancel scan to ensure that nothing interferes with connection */
5053 if (local->scanning)
5054 ieee80211_scan_cancel(local);
5055
5056 return 0;
5057}
5058
5059/* config hooks */
5060int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
5061 struct cfg80211_auth_request *req)
5062{
5063 struct ieee80211_local *local = sdata->local;
5064 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5065 struct ieee80211_mgd_auth_data *auth_data;
5066 u16 auth_alg;
5067 int err;
David Brazdil0f672f62019-12-10 10:32:29 +00005068 bool cont_auth;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005069
5070 /* prepare auth data structure */
5071
5072 switch (req->auth_type) {
5073 case NL80211_AUTHTYPE_OPEN_SYSTEM:
5074 auth_alg = WLAN_AUTH_OPEN;
5075 break;
5076 case NL80211_AUTHTYPE_SHARED_KEY:
David Brazdil0f672f62019-12-10 10:32:29 +00005077 if (fips_enabled)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005078 return -EOPNOTSUPP;
5079 auth_alg = WLAN_AUTH_SHARED_KEY;
5080 break;
5081 case NL80211_AUTHTYPE_FT:
5082 auth_alg = WLAN_AUTH_FT;
5083 break;
5084 case NL80211_AUTHTYPE_NETWORK_EAP:
5085 auth_alg = WLAN_AUTH_LEAP;
5086 break;
5087 case NL80211_AUTHTYPE_SAE:
5088 auth_alg = WLAN_AUTH_SAE;
5089 break;
5090 case NL80211_AUTHTYPE_FILS_SK:
5091 auth_alg = WLAN_AUTH_FILS_SK;
5092 break;
5093 case NL80211_AUTHTYPE_FILS_SK_PFS:
5094 auth_alg = WLAN_AUTH_FILS_SK_PFS;
5095 break;
5096 case NL80211_AUTHTYPE_FILS_PK:
5097 auth_alg = WLAN_AUTH_FILS_PK;
5098 break;
5099 default:
5100 return -EOPNOTSUPP;
5101 }
5102
David Brazdil0f672f62019-12-10 10:32:29 +00005103 if (ifmgd->assoc_data)
5104 return -EBUSY;
5105
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005106 auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
5107 req->ie_len, GFP_KERNEL);
5108 if (!auth_data)
5109 return -ENOMEM;
5110
5111 auth_data->bss = req->bss;
5112
5113 if (req->auth_data_len >= 4) {
5114 if (req->auth_type == NL80211_AUTHTYPE_SAE) {
5115 __le16 *pos = (__le16 *) req->auth_data;
5116
5117 auth_data->sae_trans = le16_to_cpu(pos[0]);
5118 auth_data->sae_status = le16_to_cpu(pos[1]);
5119 }
5120 memcpy(auth_data->data, req->auth_data + 4,
5121 req->auth_data_len - 4);
5122 auth_data->data_len += req->auth_data_len - 4;
5123 }
5124
David Brazdil0f672f62019-12-10 10:32:29 +00005125 /* Check if continuing authentication or trying to authenticate with the
5126 * same BSS that we were in the process of authenticating with and avoid
5127 * removal and re-addition of the STA entry in
5128 * ieee80211_prep_connection().
5129 */
5130 cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss;
5131
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005132 if (req->ie && req->ie_len) {
5133 memcpy(&auth_data->data[auth_data->data_len],
5134 req->ie, req->ie_len);
5135 auth_data->data_len += req->ie_len;
5136 }
5137
5138 if (req->key && req->key_len) {
5139 auth_data->key_len = req->key_len;
5140 auth_data->key_idx = req->key_idx;
5141 memcpy(auth_data->key, req->key, req->key_len);
5142 }
5143
5144 auth_data->algorithm = auth_alg;
5145
5146 /* try to authenticate/probe */
5147
David Brazdil0f672f62019-12-10 10:32:29 +00005148 if (ifmgd->auth_data) {
5149 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
5150 auth_data->peer_confirmed =
5151 ifmgd->auth_data->peer_confirmed;
5152 }
5153 ieee80211_destroy_auth_data(sdata, cont_auth);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005154 }
5155
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005156 /* prep auth_data so we don't go into idle on disassoc */
5157 ifmgd->auth_data = auth_data;
5158
David Brazdil0f672f62019-12-10 10:32:29 +00005159 /* If this is continuation of an ongoing SAE authentication exchange
5160 * (i.e., request to send SAE Confirm) and the peer has already
5161 * confirmed, mark authentication completed since we are about to send
5162 * out SAE Confirm.
5163 */
5164 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
5165 auth_data->peer_confirmed && auth_data->sae_trans == 2)
5166 ieee80211_mark_sta_auth(sdata, req->bss->bssid);
5167
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005168 if (ifmgd->associated) {
5169 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5170
5171 sdata_info(sdata,
5172 "disconnect from AP %pM for new auth to %pM\n",
5173 ifmgd->associated->bssid, req->bss->bssid);
5174 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5175 WLAN_REASON_UNSPECIFIED,
5176 false, frame_buf);
5177
5178 ieee80211_report_disconnect(sdata, frame_buf,
5179 sizeof(frame_buf), true,
5180 WLAN_REASON_UNSPECIFIED);
5181 }
5182
5183 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
5184
David Brazdil0f672f62019-12-10 10:32:29 +00005185 err = ieee80211_prep_connection(sdata, req->bss, cont_auth, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005186 if (err)
5187 goto err_clear;
5188
5189 err = ieee80211_auth(sdata);
5190 if (err) {
5191 sta_info_destroy_addr(sdata, req->bss->bssid);
5192 goto err_clear;
5193 }
5194
5195 /* hold our own reference */
5196 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
5197 return 0;
5198
5199 err_clear:
5200 eth_zero_addr(ifmgd->bssid);
5201 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
5202 ifmgd->auth_data = NULL;
5203 mutex_lock(&sdata->local->mtx);
5204 ieee80211_vif_release_channel(sdata);
5205 mutex_unlock(&sdata->local->mtx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005206 kfree(auth_data);
5207 return err;
5208}
5209
5210int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
5211 struct cfg80211_assoc_request *req)
5212{
5213 struct ieee80211_local *local = sdata->local;
5214 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5215 struct ieee80211_bss *bss = (void *)req->bss->priv;
5216 struct ieee80211_mgd_assoc_data *assoc_data;
5217 const struct cfg80211_bss_ies *beacon_ies;
5218 struct ieee80211_supported_band *sband;
5219 const u8 *ssidie, *ht_ie, *vht_ie;
5220 int i, err;
5221 bool override = false;
5222
5223 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
5224 if (!assoc_data)
5225 return -ENOMEM;
5226
5227 rcu_read_lock();
5228 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
David Brazdil0f672f62019-12-10 10:32:29 +00005229 if (!ssidie || ssidie[1] > sizeof(assoc_data->ssid)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005230 rcu_read_unlock();
5231 kfree(assoc_data);
5232 return -EINVAL;
5233 }
5234 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
5235 assoc_data->ssid_len = ssidie[1];
5236 rcu_read_unlock();
5237
5238 if (ifmgd->associated) {
5239 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5240
5241 sdata_info(sdata,
5242 "disconnect from AP %pM for new assoc to %pM\n",
5243 ifmgd->associated->bssid, req->bss->bssid);
5244 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5245 WLAN_REASON_UNSPECIFIED,
5246 false, frame_buf);
5247
5248 ieee80211_report_disconnect(sdata, frame_buf,
5249 sizeof(frame_buf), true,
5250 WLAN_REASON_UNSPECIFIED);
5251 }
5252
5253 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
5254 err = -EBUSY;
5255 goto err_free;
5256 }
5257
5258 if (ifmgd->assoc_data) {
5259 err = -EBUSY;
5260 goto err_free;
5261 }
5262
5263 if (ifmgd->auth_data) {
5264 bool match;
5265
5266 /* keep sta info, bssid if matching */
5267 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
5268 ieee80211_destroy_auth_data(sdata, match);
5269 }
5270
5271 /* prepare assoc data */
5272
5273 ifmgd->beacon_crc_valid = false;
5274
5275 assoc_data->wmm = bss->wmm_used &&
5276 (local->hw.queues >= IEEE80211_NUM_ACS);
5277
5278 /*
5279 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
5280 * We still associate in non-HT mode (11a/b/g) if any one of these
5281 * ciphers is configured as pairwise.
5282 * We can set this to true for non-11n hardware, that'll be checked
5283 * separately along with the peer capabilities.
5284 */
5285 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
5286 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5287 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
5288 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
5289 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5290 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5291 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5292 netdev_info(sdata->dev,
David Brazdil0f672f62019-12-10 10:32:29 +00005293 "disabling HT/VHT/HE due to WEP/TKIP use\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005294 }
5295 }
5296
5297 /* Also disable HT if we don't support it or the AP doesn't use WMM */
5298 sband = local->hw.wiphy->bands[req->bss->channel->band];
5299 if (!sband->ht_cap.ht_supported ||
5300 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
5301 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
5302 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5303 if (!bss->wmm_used &&
5304 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
5305 netdev_info(sdata->dev,
5306 "disabling HT as WMM/QoS is not supported by the AP\n");
5307 }
5308
5309 /* disable VHT if we don't support it or the AP doesn't use WMM */
5310 if (!sband->vht_cap.vht_supported ||
5311 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
5312 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
5313 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5314 if (!bss->wmm_used &&
5315 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
5316 netdev_info(sdata->dev,
5317 "disabling VHT as WMM/QoS is not supported by the AP\n");
5318 }
5319
5320 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
5321 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
5322 sizeof(ifmgd->ht_capa_mask));
5323
5324 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
5325 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
5326 sizeof(ifmgd->vht_capa_mask));
5327
5328 if (req->ie && req->ie_len) {
5329 memcpy(assoc_data->ie, req->ie, req->ie_len);
5330 assoc_data->ie_len = req->ie_len;
5331 }
5332
5333 if (req->fils_kek) {
5334 /* should already be checked in cfg80211 - so warn */
5335 if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
5336 err = -EINVAL;
5337 goto err_free;
5338 }
5339 memcpy(assoc_data->fils_kek, req->fils_kek,
5340 req->fils_kek_len);
5341 assoc_data->fils_kek_len = req->fils_kek_len;
5342 }
5343
5344 if (req->fils_nonces)
5345 memcpy(assoc_data->fils_nonces, req->fils_nonces,
5346 2 * FILS_NONCE_LEN);
5347
5348 assoc_data->bss = req->bss;
5349
5350 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
5351 if (ifmgd->powersave)
5352 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
5353 else
5354 sdata->smps_mode = IEEE80211_SMPS_OFF;
5355 } else
5356 sdata->smps_mode = ifmgd->req_smps;
5357
5358 assoc_data->capability = req->bss->capability;
5359 assoc_data->supp_rates = bss->supp_rates;
5360 assoc_data->supp_rates_len = bss->supp_rates_len;
5361
5362 rcu_read_lock();
5363 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
5364 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
5365 assoc_data->ap_ht_param =
5366 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
5367 else
5368 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5369 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
5370 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
5371 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
5372 sizeof(struct ieee80211_vht_cap));
5373 else
5374 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5375 rcu_read_unlock();
5376
5377 if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
5378 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
5379 "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
5380 sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
5381
5382 if (bss->wmm_used && bss->uapsd_supported &&
5383 (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
5384 assoc_data->uapsd = true;
5385 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
5386 } else {
5387 assoc_data->uapsd = false;
5388 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
5389 }
5390
5391 if (req->prev_bssid)
5392 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
5393
5394 if (req->use_mfp) {
5395 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
5396 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
5397 } else {
5398 ifmgd->mfp = IEEE80211_MFP_DISABLED;
5399 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
5400 }
5401
5402 if (req->flags & ASSOC_REQ_USE_RRM)
5403 ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
5404 else
5405 ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
5406
5407 if (req->crypto.control_port)
5408 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
5409 else
5410 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
5411
5412 sdata->control_port_protocol = req->crypto.control_port_ethertype;
5413 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
5414 sdata->control_port_over_nl80211 =
5415 req->crypto.control_port_over_nl80211;
5416 sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
5417 sdata->vif.type);
5418
5419 /* kick off associate process */
5420
5421 ifmgd->assoc_data = assoc_data;
5422 ifmgd->dtim_period = 0;
5423 ifmgd->have_beacon = false;
5424
5425 /* override HT/VHT configuration only if the AP and we support it */
5426 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
5427 struct ieee80211_sta_ht_cap sta_ht_cap;
5428
5429 if (req->flags & ASSOC_REQ_DISABLE_HT)
5430 override = true;
5431
5432 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
5433 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5434
5435 /* check for 40 MHz disable override */
5436 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
5437 sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
5438 !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
5439 override = true;
5440
5441 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
5442 req->flags & ASSOC_REQ_DISABLE_VHT)
5443 override = true;
5444 }
5445
5446 if (req->flags & ASSOC_REQ_DISABLE_HT) {
5447 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5448 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5449 }
5450
5451 if (req->flags & ASSOC_REQ_DISABLE_VHT)
5452 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5453
5454 err = ieee80211_prep_connection(sdata, req->bss, true, override);
5455 if (err)
5456 goto err_clear;
5457
5458 rcu_read_lock();
5459 beacon_ies = rcu_dereference(req->bss->beacon_ies);
5460
5461 if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
5462 !beacon_ies) {
5463 /*
5464 * Wait up to one beacon interval ...
5465 * should this be more if we miss one?
5466 */
5467 sdata_info(sdata, "waiting for beacon from %pM\n",
5468 ifmgd->bssid);
5469 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
5470 assoc_data->timeout_started = true;
5471 assoc_data->need_beacon = true;
5472 } else if (beacon_ies) {
David Brazdil0f672f62019-12-10 10:32:29 +00005473 const u8 *ie;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005474 u8 dtim_count = 0;
5475
David Brazdil0f672f62019-12-10 10:32:29 +00005476 ieee80211_get_dtim(beacon_ies, &dtim_count,
5477 &ifmgd->dtim_period);
5478
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005479 ifmgd->have_beacon = true;
5480 assoc_data->timeout = jiffies;
5481 assoc_data->timeout_started = true;
5482
5483 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
5484 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
5485 sdata->vif.bss_conf.sync_device_ts =
5486 bss->device_ts_beacon;
5487 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
5488 }
David Brazdil0f672f62019-12-10 10:32:29 +00005489
5490 ie = cfg80211_find_ext_ie(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
5491 beacon_ies->data, beacon_ies->len);
5492 if (ie && ie[1] >= 3)
5493 sdata->vif.bss_conf.profile_periodicity = ie[4];
5494
5495 ie = cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY,
5496 beacon_ies->data, beacon_ies->len);
5497 if (ie && ie[1] >= 11 &&
5498 (ie[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
5499 sdata->vif.bss_conf.ema_ap = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005500 } else {
5501 assoc_data->timeout = jiffies;
5502 assoc_data->timeout_started = true;
5503 }
5504 rcu_read_unlock();
5505
5506 run_again(sdata, assoc_data->timeout);
5507
5508 if (bss->corrupt_data) {
5509 char *corrupt_type = "data";
5510 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
5511 if (bss->corrupt_data &
5512 IEEE80211_BSS_CORRUPT_PROBE_RESP)
5513 corrupt_type = "beacon and probe response";
5514 else
5515 corrupt_type = "beacon";
5516 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
5517 corrupt_type = "probe response";
5518 sdata_info(sdata, "associating with AP with corrupt %s\n",
5519 corrupt_type);
5520 }
5521
5522 return 0;
5523 err_clear:
5524 eth_zero_addr(ifmgd->bssid);
5525 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
5526 ifmgd->assoc_data = NULL;
5527 err_free:
5528 kfree(assoc_data);
5529 return err;
5530}
5531
5532int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
5533 struct cfg80211_deauth_request *req)
5534{
5535 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5536 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5537 bool tx = !req->local_state_change;
5538
5539 if (ifmgd->auth_data &&
5540 ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
5541 sdata_info(sdata,
5542 "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
5543 req->bssid, req->reason_code,
5544 ieee80211_get_reason_code_string(req->reason_code));
5545
5546 drv_mgd_prepare_tx(sdata->local, sdata, 0);
David Brazdil0f672f62019-12-10 10:32:29 +00005547 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005548 IEEE80211_STYPE_DEAUTH,
5549 req->reason_code, tx,
5550 frame_buf);
5551 ieee80211_destroy_auth_data(sdata, false);
5552 ieee80211_report_disconnect(sdata, frame_buf,
5553 sizeof(frame_buf), true,
5554 req->reason_code);
5555
5556 return 0;
5557 }
5558
5559 if (ifmgd->assoc_data &&
5560 ether_addr_equal(ifmgd->assoc_data->bss->bssid, req->bssid)) {
5561 sdata_info(sdata,
5562 "aborting association with %pM by local choice (Reason: %u=%s)\n",
5563 req->bssid, req->reason_code,
5564 ieee80211_get_reason_code_string(req->reason_code));
5565
5566 drv_mgd_prepare_tx(sdata->local, sdata, 0);
David Brazdil0f672f62019-12-10 10:32:29 +00005567 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005568 IEEE80211_STYPE_DEAUTH,
5569 req->reason_code, tx,
5570 frame_buf);
5571 ieee80211_destroy_assoc_data(sdata, false, true);
5572 ieee80211_report_disconnect(sdata, frame_buf,
5573 sizeof(frame_buf), true,
5574 req->reason_code);
5575 return 0;
5576 }
5577
5578 if (ifmgd->associated &&
5579 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
5580 sdata_info(sdata,
5581 "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
5582 req->bssid, req->reason_code,
5583 ieee80211_get_reason_code_string(req->reason_code));
5584
5585 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5586 req->reason_code, tx, frame_buf);
5587 ieee80211_report_disconnect(sdata, frame_buf,
5588 sizeof(frame_buf), true,
5589 req->reason_code);
5590 return 0;
5591 }
5592
5593 return -ENOTCONN;
5594}
5595
5596int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
5597 struct cfg80211_disassoc_request *req)
5598{
5599 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5600 u8 bssid[ETH_ALEN];
5601 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5602
5603 /*
5604 * cfg80211 should catch this ... but it's racy since
5605 * we can receive a disassoc frame, process it, hand it
5606 * to cfg80211 while that's in a locked section already
5607 * trying to tell us that the user wants to disconnect.
5608 */
5609 if (ifmgd->associated != req->bss)
5610 return -ENOLINK;
5611
5612 sdata_info(sdata,
5613 "disassociating from %pM by local choice (Reason: %u=%s)\n",
5614 req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
5615
5616 memcpy(bssid, req->bss->bssid, ETH_ALEN);
5617 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
5618 req->reason_code, !req->local_state_change,
5619 frame_buf);
5620
5621 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
5622 req->reason_code);
5623
5624 return 0;
5625}
5626
5627void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
5628{
5629 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5630
5631 /*
5632 * Make sure some work items will not run after this,
5633 * they will not do anything but might not have been
5634 * cancelled when disconnecting.
5635 */
5636 cancel_work_sync(&ifmgd->monitor_work);
5637 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
5638 cancel_work_sync(&ifmgd->request_smps_work);
5639 cancel_work_sync(&ifmgd->csa_connection_drop_work);
5640 cancel_work_sync(&ifmgd->chswitch_work);
5641 cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
5642
5643 sdata_lock(sdata);
5644 if (ifmgd->assoc_data) {
5645 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
5646 ieee80211_destroy_assoc_data(sdata, false, false);
5647 cfg80211_assoc_timeout(sdata->dev, bss);
5648 }
5649 if (ifmgd->auth_data)
5650 ieee80211_destroy_auth_data(sdata, false);
5651 spin_lock_bh(&ifmgd->teardown_lock);
5652 if (ifmgd->teardown_skb) {
5653 kfree_skb(ifmgd->teardown_skb);
5654 ifmgd->teardown_skb = NULL;
5655 ifmgd->orig_teardown_skb = NULL;
5656 }
David Brazdil0f672f62019-12-10 10:32:29 +00005657 kfree(ifmgd->assoc_req_ies);
5658 ifmgd->assoc_req_ies = NULL;
5659 ifmgd->assoc_req_ies_len = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005660 spin_unlock_bh(&ifmgd->teardown_lock);
5661 del_timer_sync(&ifmgd->timer);
5662 sdata_unlock(sdata);
5663}
5664
5665void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
5666 enum nl80211_cqm_rssi_threshold_event rssi_event,
5667 s32 rssi_level,
5668 gfp_t gfp)
5669{
5670 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5671
5672 trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
5673
5674 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
5675}
5676EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
5677
5678void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
5679{
5680 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5681
5682 trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
5683
5684 cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
5685}
5686EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);