tipc: correct spelling errors for tipc_topsrv_queue_evt() comments
[muen/linux.git] / net / mac80211 / mlme.c
1 /*
2  * BSS client mode implementation
3  * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
4  * Copyright 2004, Instant802 Networks, Inc.
5  * Copyright 2005, Devicescape Software, Inc.
6  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
7  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8  * Copyright 2013-2014  Intel Mobile Communications GmbH
9  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
10  * Copyright (C) 2018        Intel Corporation
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  */
16
17 #include <linux/delay.h>
18 #include <linux/if_ether.h>
19 #include <linux/skbuff.h>
20 #include <linux/if_arp.h>
21 #include <linux/etherdevice.h>
22 #include <linux/moduleparam.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/crc32.h>
25 #include <linux/slab.h>
26 #include <linux/export.h>
27 #include <net/mac80211.h>
28 #include <asm/unaligned.h>
29
30 #include "ieee80211_i.h"
31 #include "driver-ops.h"
32 #include "rate.h"
33 #include "led.h"
34 #include "fils_aead.h"
35
36 #define IEEE80211_AUTH_TIMEOUT          (HZ / 5)
37 #define IEEE80211_AUTH_TIMEOUT_LONG     (HZ / 2)
38 #define IEEE80211_AUTH_TIMEOUT_SHORT    (HZ / 10)
39 #define IEEE80211_AUTH_TIMEOUT_SAE      (HZ * 2)
40 #define IEEE80211_AUTH_MAX_TRIES        3
41 #define IEEE80211_AUTH_WAIT_ASSOC       (HZ * 5)
42 #define IEEE80211_ASSOC_TIMEOUT         (HZ / 5)
43 #define IEEE80211_ASSOC_TIMEOUT_LONG    (HZ / 2)
44 #define IEEE80211_ASSOC_TIMEOUT_SHORT   (HZ / 10)
45 #define IEEE80211_ASSOC_MAX_TRIES       3
46
47 static int max_nullfunc_tries = 2;
48 module_param(max_nullfunc_tries, int, 0644);
49 MODULE_PARM_DESC(max_nullfunc_tries,
50                  "Maximum nullfunc tx tries before disconnecting (reason 4).");
51
52 static int max_probe_tries = 5;
53 module_param(max_probe_tries, int, 0644);
54 MODULE_PARM_DESC(max_probe_tries,
55                  "Maximum probe tries before disconnecting (reason 4).");
56
57 /*
58  * Beacon loss timeout is calculated as N frames times the
59  * advertised beacon interval.  This may need to be somewhat
60  * higher than what hardware might detect to account for
61  * delays in the host processing frames. But since we also
62  * probe on beacon miss before declaring the connection lost
63  * default to what we want.
64  */
65 static int beacon_loss_count = 7;
66 module_param(beacon_loss_count, int, 0644);
67 MODULE_PARM_DESC(beacon_loss_count,
68                  "Number of beacon intervals before we decide beacon was lost.");
69
70 /*
71  * Time the connection can be idle before we probe
72  * it to see if we can still talk to the AP.
73  */
74 #define IEEE80211_CONNECTION_IDLE_TIME  (30 * HZ)
75 /*
76  * Time we wait for a probe response after sending
77  * a probe request because of beacon loss or for
78  * checking the connection still works.
79  */
80 static int probe_wait_ms = 500;
81 module_param(probe_wait_ms, int, 0644);
82 MODULE_PARM_DESC(probe_wait_ms,
83                  "Maximum time(ms) to wait for probe response"
84                  " before disconnecting (reason 4).");
85
86 /*
87  * How many Beacon frames need to have been used in average signal strength
88  * before starting to indicate signal change events.
89  */
90 #define IEEE80211_SIGNAL_AVE_MIN_COUNT  4
91
92 /*
93  * We can have multiple work items (and connection probing)
94  * scheduling this timer, but we need to take care to only
95  * reschedule it when it should fire _earlier_ than it was
96  * asked for before, or if it's not pending right now. This
97  * function ensures that. Note that it then is required to
98  * run this function for all timeouts after the first one
99  * has happened -- the work that runs from this timer will
100  * do that.
101  */
102 static void run_again(struct ieee80211_sub_if_data *sdata,
103                       unsigned long timeout)
104 {
105         sdata_assert_lock(sdata);
106
107         if (!timer_pending(&sdata->u.mgd.timer) ||
108             time_before(timeout, sdata->u.mgd.timer.expires))
109                 mod_timer(&sdata->u.mgd.timer, timeout);
110 }
111
112 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
113 {
114         if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
115                 return;
116
117         if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
118                 return;
119
120         mod_timer(&sdata->u.mgd.bcn_mon_timer,
121                   round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
122 }
123
124 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
125 {
126         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
127
128         if (unlikely(!ifmgd->associated))
129                 return;
130
131         if (ifmgd->probe_send_count)
132                 ifmgd->probe_send_count = 0;
133
134         if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
135                 return;
136
137         mod_timer(&ifmgd->conn_mon_timer,
138                   round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
139 }
140
141 static int ecw2cw(int ecw)
142 {
143         return (1 << ecw) - 1;
144 }
145
146 static u32
147 ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
148                              struct ieee80211_supported_band *sband,
149                              struct ieee80211_channel *channel,
150                              const struct ieee80211_ht_operation *ht_oper,
151                              const struct ieee80211_vht_operation *vht_oper,
152                              const struct ieee80211_he_operation *he_oper,
153                              struct cfg80211_chan_def *chandef, bool tracking)
154 {
155         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
156         struct cfg80211_chan_def vht_chandef;
157         struct ieee80211_sta_ht_cap sta_ht_cap;
158         u32 ht_cfreq, ret;
159
160         memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
161         ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
162
163         chandef->chan = channel;
164         chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
165         chandef->center_freq1 = channel->center_freq;
166         chandef->center_freq2 = 0;
167
168         if (!ht_oper || !sta_ht_cap.ht_supported) {
169                 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
170                 goto out;
171         }
172
173         chandef->width = NL80211_CHAN_WIDTH_20;
174
175         ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
176                                                   channel->band);
177         /* check that channel matches the right operating channel */
178         if (!tracking && channel->center_freq != ht_cfreq) {
179                 /*
180                  * It's possible that some APs are confused here;
181                  * Netgear WNDR3700 sometimes reports 4 higher than
182                  * the actual channel in association responses, but
183                  * since we look at probe response/beacon data here
184                  * it should be OK.
185                  */
186                 sdata_info(sdata,
187                            "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
188                            channel->center_freq, ht_cfreq,
189                            ht_oper->primary_chan, channel->band);
190                 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
191                 goto out;
192         }
193
194         /* check 40 MHz support, if we have it */
195         if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
196                 ieee80211_chandef_ht_oper(ht_oper, chandef);
197         } else {
198                 /* 40 MHz (and 80 MHz) must be supported for VHT */
199                 ret = IEEE80211_STA_DISABLE_VHT;
200                 /* also mark 40 MHz disabled */
201                 ret |= IEEE80211_STA_DISABLE_40MHZ;
202                 goto out;
203         }
204
205         if (!vht_oper || !sband->vht_cap.vht_supported) {
206                 ret = IEEE80211_STA_DISABLE_VHT;
207                 goto out;
208         }
209
210         vht_chandef = *chandef;
211         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && he_oper &&
212             (le32_to_cpu(he_oper->he_oper_params) &
213              IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
214                 struct ieee80211_vht_operation he_oper_vht_cap;
215
216                 /*
217                  * Set only first 3 bytes (other 2 aren't used in
218                  * ieee80211_chandef_vht_oper() anyway)
219                  */
220                 memcpy(&he_oper_vht_cap, he_oper->optional, 3);
221                 he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
222
223                 if (!ieee80211_chandef_vht_oper(&he_oper_vht_cap,
224                                                 &vht_chandef)) {
225                         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
226                                 sdata_info(sdata,
227                                            "HE AP VHT information is invalid, disable HE\n");
228                         ret = IEEE80211_STA_DISABLE_HE;
229                         goto out;
230                 }
231         } else if (!ieee80211_chandef_vht_oper(vht_oper, &vht_chandef)) {
232                 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
264 out:
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
319 static 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
453 static 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  */
527 static 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  */
613 static 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
638 static 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;
645         u8 *pos, qos_info;
646         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);
753         *pos++ = WLAN_EID_SSID;
754         *pos++ = assoc_data->ssid_len;
755         memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
756
757         /* add all rates which were marked to be used above */
758         supp_rates_len = rates_len;
759         if (supp_rates_len > 8)
760                 supp_rates_len = 8;
761
762         pos = skb_put(skb, supp_rates_len + 2);
763         *pos++ = WLAN_EID_SUPP_RATES;
764         *pos++ = supp_rates_len;
765
766         count = 0;
767         for (i = 0; i < sband->n_bitrates; i++) {
768                 if (BIT(i) & rates) {
769                         int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
770                                                 5 * (1 << shift));
771                         *pos++ = (u8) rate;
772                         if (++count == 8)
773                                 break;
774                 }
775         }
776
777         if (rates_len > count) {
778                 pos = skb_put(skb, rates_len - count + 2);
779                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
780                 *pos++ = rates_len - count;
781
782                 for (i++; i < sband->n_bitrates; i++) {
783                         if (BIT(i) & rates) {
784                                 int rate;
785                                 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
786                                                     5 * (1 << shift));
787                                 *pos++ = (u8) rate;
788                         }
789                 }
790         }
791
792         if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
793             capab & WLAN_CAPABILITY_RADIO_MEASURE) {
794                 pos = skb_put(skb, 4);
795                 *pos++ = WLAN_EID_PWR_CAPABILITY;
796                 *pos++ = 2;
797                 *pos++ = 0; /* min tx power */
798                  /* max tx power */
799                 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
800         }
801
802         if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
803                 /* TODO: get this in reg domain format */
804                 pos = skb_put(skb, 2 * sband->n_channels + 2);
805                 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
806                 *pos++ = 2 * sband->n_channels;
807                 for (i = 0; i < sband->n_channels; i++) {
808                         *pos++ = ieee80211_frequency_to_channel(
809                                         sband->channels[i].center_freq);
810                         *pos++ = 1; /* one channel in the subband*/
811                 }
812         }
813
814         /* if present, add any custom IEs that go before HT */
815         if (assoc_data->ie_len) {
816                 static const u8 before_ht[] = {
817                         WLAN_EID_SSID,
818                         WLAN_EID_SUPP_RATES,
819                         WLAN_EID_EXT_SUPP_RATES,
820                         WLAN_EID_PWR_CAPABILITY,
821                         WLAN_EID_SUPPORTED_CHANNELS,
822                         WLAN_EID_RSN,
823                         WLAN_EID_QOS_CAPA,
824                         WLAN_EID_RRM_ENABLED_CAPABILITIES,
825                         WLAN_EID_MOBILITY_DOMAIN,
826                         WLAN_EID_FAST_BSS_TRANSITION,   /* reassoc only */
827                         WLAN_EID_RIC_DATA,              /* reassoc only */
828                         WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
829                 };
830                 static const u8 after_ric[] = {
831                         WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
832                         WLAN_EID_HT_CAPABILITY,
833                         WLAN_EID_BSS_COEX_2040,
834                         /* luckily this is almost always there */
835                         WLAN_EID_EXT_CAPABILITY,
836                         WLAN_EID_QOS_TRAFFIC_CAPA,
837                         WLAN_EID_TIM_BCAST_REQ,
838                         WLAN_EID_INTERWORKING,
839                         /* 60 GHz (Multi-band, DMG, MMS) can't happen */
840                         WLAN_EID_VHT_CAPABILITY,
841                         WLAN_EID_OPMODE_NOTIF,
842                 };
843
844                 noffset = ieee80211_ie_split_ric(assoc_data->ie,
845                                                  assoc_data->ie_len,
846                                                  before_ht,
847                                                  ARRAY_SIZE(before_ht),
848                                                  after_ric,
849                                                  ARRAY_SIZE(after_ric),
850                                                  offset);
851                 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
852                 offset = noffset;
853         }
854
855         if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
856                          !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
857                 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
858
859         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
860                 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
861                                     sband, chan, sdata->smps_mode);
862
863         /* if present, add any custom IEs that go before VHT */
864         if (assoc_data->ie_len) {
865                 static const u8 before_vht[] = {
866                         /*
867                          * no need to list the ones split off before HT
868                          * or generated here
869                          */
870                         WLAN_EID_BSS_COEX_2040,
871                         WLAN_EID_EXT_CAPABILITY,
872                         WLAN_EID_QOS_TRAFFIC_CAPA,
873                         WLAN_EID_TIM_BCAST_REQ,
874                         WLAN_EID_INTERWORKING,
875                         /* 60 GHz (Multi-band, DMG, MMS) can't happen */
876                 };
877
878                 /* RIC already taken above, so no need to handle here anymore */
879                 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
880                                              before_vht, ARRAY_SIZE(before_vht),
881                                              offset);
882                 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
883                 offset = noffset;
884         }
885
886         /* if present, add any custom IEs that go before HE */
887         if (assoc_data->ie_len) {
888                 static const u8 before_he[] = {
889                         /*
890                          * no need to list the ones split off before VHT
891                          * or generated here
892                          */
893                         WLAN_EID_OPMODE_NOTIF,
894                         WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
895                         /* 11ai elements */
896                         WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
897                         WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
898                         WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
899                         WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
900                         WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
901                         /* TODO: add 11ah/11aj/11ak elements */
902                 };
903
904                 /* RIC already taken above, so no need to handle here anymore */
905                 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
906                                              before_he, ARRAY_SIZE(before_he),
907                                              offset);
908                 pos = skb_put(skb, noffset - offset);
909                 memcpy(pos, assoc_data->ie + offset, noffset - offset);
910                 offset = noffset;
911         }
912
913         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
914                 ieee80211_add_vht_ie(sdata, skb, sband,
915                                      &assoc_data->ap_vht_cap);
916
917         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
918                 ieee80211_add_he_ie(sdata, skb, sband);
919
920         /* if present, add any custom non-vendor IEs that go after HE */
921         if (assoc_data->ie_len) {
922                 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
923                                                     assoc_data->ie_len,
924                                                     offset);
925                 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
926                 offset = noffset;
927         }
928
929         if (assoc_data->wmm) {
930                 if (assoc_data->uapsd) {
931                         qos_info = ifmgd->uapsd_queues;
932                         qos_info |= (ifmgd->uapsd_max_sp_len <<
933                                      IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
934                 } else {
935                         qos_info = 0;
936                 }
937
938                 pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
939         }
940
941         /* add any remaining custom (i.e. vendor specific here) IEs */
942         if (assoc_data->ie_len) {
943                 noffset = assoc_data->ie_len;
944                 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
945         }
946
947         if (assoc_data->fils_kek_len &&
948             fils_encrypt_assoc_req(skb, assoc_data) < 0) {
949                 dev_kfree_skb(skb);
950                 return;
951         }
952
953         drv_mgd_prepare_tx(local, sdata, 0);
954
955         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
956         if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
957                 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
958                                                 IEEE80211_TX_INTFL_MLME_CONN_TX;
959         ieee80211_tx_skb(sdata, skb);
960 }
961
962 void ieee80211_send_pspoll(struct ieee80211_local *local,
963                            struct ieee80211_sub_if_data *sdata)
964 {
965         struct ieee80211_pspoll *pspoll;
966         struct sk_buff *skb;
967
968         skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
969         if (!skb)
970                 return;
971
972         pspoll = (struct ieee80211_pspoll *) skb->data;
973         pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
974
975         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
976         ieee80211_tx_skb(sdata, skb);
977 }
978
979 void ieee80211_send_nullfunc(struct ieee80211_local *local,
980                              struct ieee80211_sub_if_data *sdata,
981                              bool powersave)
982 {
983         struct sk_buff *skb;
984         struct ieee80211_hdr_3addr *nullfunc;
985         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
986
987         /* Don't send NDPs when STA is connected HE */
988         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
989             !(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
990                 return;
991
992         skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
993                 !ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
994         if (!skb)
995                 return;
996
997         nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
998         if (powersave)
999                 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1000
1001         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1002                                         IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
1003
1004         if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1005                 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1006
1007         if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
1008                 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1009
1010         ieee80211_tx_skb(sdata, skb);
1011 }
1012
1013 static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1014                                           struct ieee80211_sub_if_data *sdata)
1015 {
1016         struct sk_buff *skb;
1017         struct ieee80211_hdr *nullfunc;
1018         __le16 fc;
1019
1020         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1021                 return;
1022
1023         /* Don't send NDPs when connected HE */
1024         if (!(sdata->u.mgd.flags & IEEE80211_STA_DISABLE_HE))
1025                 return;
1026
1027         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
1028         if (!skb)
1029                 return;
1030
1031         skb_reserve(skb, local->hw.extra_tx_headroom);
1032
1033         nullfunc = skb_put_zero(skb, 30);
1034         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1035                          IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1036         nullfunc->frame_control = fc;
1037         memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
1038         memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1039         memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
1040         memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1041
1042         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1043         ieee80211_tx_skb(sdata, skb);
1044 }
1045
1046 /* spectrum management related things */
1047 static void ieee80211_chswitch_work(struct work_struct *work)
1048 {
1049         struct ieee80211_sub_if_data *sdata =
1050                 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
1051         struct ieee80211_local *local = sdata->local;
1052         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1053         int ret;
1054
1055         if (!ieee80211_sdata_running(sdata))
1056                 return;
1057
1058         sdata_lock(sdata);
1059         mutex_lock(&local->mtx);
1060         mutex_lock(&local->chanctx_mtx);
1061
1062         if (!ifmgd->associated)
1063                 goto out;
1064
1065         if (!sdata->vif.csa_active)
1066                 goto out;
1067
1068         /*
1069          * using reservation isn't immediate as it may be deferred until later
1070          * with multi-vif. once reservation is complete it will re-schedule the
1071          * work with no reserved_chanctx so verify chandef to check if it
1072          * completed successfully
1073          */
1074
1075         if (sdata->reserved_chanctx) {
1076                 /*
1077                  * with multi-vif csa driver may call ieee80211_csa_finish()
1078                  * many times while waiting for other interfaces to use their
1079                  * reservations
1080                  */
1081                 if (sdata->reserved_ready)
1082                         goto out;
1083
1084                 ret = ieee80211_vif_use_reserved_context(sdata);
1085                 if (ret) {
1086                         sdata_info(sdata,
1087                                    "failed to use reserved channel context, disconnecting (err=%d)\n",
1088                                    ret);
1089                         ieee80211_queue_work(&sdata->local->hw,
1090                                              &ifmgd->csa_connection_drop_work);
1091                         goto out;
1092                 }
1093
1094                 goto out;
1095         }
1096
1097         if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
1098                                         &sdata->csa_chandef)) {
1099                 sdata_info(sdata,
1100                            "failed to finalize channel switch, disconnecting\n");
1101                 ieee80211_queue_work(&sdata->local->hw,
1102                                      &ifmgd->csa_connection_drop_work);
1103                 goto out;
1104         }
1105
1106         /* XXX: shouldn't really modify cfg80211-owned data! */
1107         ifmgd->associated->channel = sdata->csa_chandef.chan;
1108
1109         ifmgd->csa_waiting_bcn = true;
1110
1111         ieee80211_sta_reset_beacon_monitor(sdata);
1112         ieee80211_sta_reset_conn_monitor(sdata);
1113
1114 out:
1115         mutex_unlock(&local->chanctx_mtx);
1116         mutex_unlock(&local->mtx);
1117         sdata_unlock(sdata);
1118 }
1119
1120 static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
1121 {
1122         struct ieee80211_local *local = sdata->local;
1123         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1124         int ret;
1125
1126         sdata_assert_lock(sdata);
1127
1128         WARN_ON(!sdata->vif.csa_active);
1129
1130         if (sdata->csa_block_tx) {
1131                 ieee80211_wake_vif_queues(local, sdata,
1132                                           IEEE80211_QUEUE_STOP_REASON_CSA);
1133                 sdata->csa_block_tx = false;
1134         }
1135
1136         sdata->vif.csa_active = false;
1137         ifmgd->csa_waiting_bcn = false;
1138
1139         ret = drv_post_channel_switch(sdata);
1140         if (ret) {
1141                 sdata_info(sdata,
1142                            "driver post channel switch failed, disconnecting\n");
1143                 ieee80211_queue_work(&local->hw,
1144                                      &ifmgd->csa_connection_drop_work);
1145                 return;
1146         }
1147
1148         cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
1149 }
1150
1151 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1152 {
1153         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1154         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1155
1156         trace_api_chswitch_done(sdata, success);
1157         if (!success) {
1158                 sdata_info(sdata,
1159                            "driver channel switch failed, disconnecting\n");
1160                 ieee80211_queue_work(&sdata->local->hw,
1161                                      &ifmgd->csa_connection_drop_work);
1162         } else {
1163                 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
1164         }
1165 }
1166 EXPORT_SYMBOL(ieee80211_chswitch_done);
1167
1168 static void ieee80211_chswitch_timer(struct timer_list *t)
1169 {
1170         struct ieee80211_sub_if_data *sdata =
1171                 from_timer(sdata, t, u.mgd.chswitch_timer);
1172
1173         ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
1174 }
1175
1176 static void
1177 ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1178                                  u64 timestamp, u32 device_timestamp,
1179                                  struct ieee802_11_elems *elems,
1180                                  bool beacon)
1181 {
1182         struct ieee80211_local *local = sdata->local;
1183         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1184         struct cfg80211_bss *cbss = ifmgd->associated;
1185         struct ieee80211_chanctx_conf *conf;
1186         struct ieee80211_chanctx *chanctx;
1187         enum nl80211_band current_band;
1188         struct ieee80211_csa_ie csa_ie;
1189         struct ieee80211_channel_switch ch_switch;
1190         int res;
1191
1192         sdata_assert_lock(sdata);
1193
1194         if (!cbss)
1195                 return;
1196
1197         if (local->scanning)
1198                 return;
1199
1200         /* disregard subsequent announcements if we are already processing */
1201         if (sdata->vif.csa_active)
1202                 return;
1203
1204         current_band = cbss->channel->band;
1205         res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
1206                                            ifmgd->flags,
1207                                            ifmgd->associated->bssid, &csa_ie);
1208         if (res < 0)
1209                 ieee80211_queue_work(&local->hw,
1210                                      &ifmgd->csa_connection_drop_work);
1211         if (res)
1212                 return;
1213
1214         if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
1215                                      IEEE80211_CHAN_DISABLED)) {
1216                 sdata_info(sdata,
1217                            "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1218                            ifmgd->associated->bssid,
1219                            csa_ie.chandef.chan->center_freq,
1220                            csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1221                            csa_ie.chandef.center_freq2);
1222                 ieee80211_queue_work(&local->hw,
1223                                      &ifmgd->csa_connection_drop_work);
1224                 return;
1225         }
1226
1227         if (cfg80211_chandef_identical(&csa_ie.chandef,
1228                                        &sdata->vif.bss_conf.chandef)) {
1229                 if (ifmgd->csa_ignored_same_chan)
1230                         return;
1231                 sdata_info(sdata,
1232                            "AP %pM tries to chanswitch to same channel, ignore\n",
1233                            ifmgd->associated->bssid);
1234                 ifmgd->csa_ignored_same_chan = true;
1235                 return;
1236         }
1237
1238         /*
1239          * Drop all TDLS peers - either we disconnect or move to a different
1240          * channel from this point on. There's no telling what our peer will do.
1241          * The TDLS WIDER_BW scenario is also problematic, as peers might now
1242          * have an incompatible wider chandef.
1243          */
1244         ieee80211_teardown_tdls_peers(sdata);
1245
1246         mutex_lock(&local->mtx);
1247         mutex_lock(&local->chanctx_mtx);
1248         conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1249                                          lockdep_is_held(&local->chanctx_mtx));
1250         if (!conf) {
1251                 sdata_info(sdata,
1252                            "no channel context assigned to vif?, disconnecting\n");
1253                 goto drop_connection;
1254         }
1255
1256         chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1257
1258         if (local->use_chanctx &&
1259             !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
1260                 sdata_info(sdata,
1261                            "driver doesn't support chan-switch with channel contexts\n");
1262                 goto drop_connection;
1263         }
1264
1265         ch_switch.timestamp = timestamp;
1266         ch_switch.device_timestamp = device_timestamp;
1267         ch_switch.block_tx = csa_ie.mode;
1268         ch_switch.chandef = csa_ie.chandef;
1269         ch_switch.count = csa_ie.count;
1270
1271         if (drv_pre_channel_switch(sdata, &ch_switch)) {
1272                 sdata_info(sdata,
1273                            "preparing for channel switch failed, disconnecting\n");
1274                 goto drop_connection;
1275         }
1276
1277         res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
1278                                             chanctx->mode, false);
1279         if (res) {
1280                 sdata_info(sdata,
1281                            "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1282                            res);
1283                 goto drop_connection;
1284         }
1285         mutex_unlock(&local->chanctx_mtx);
1286
1287         sdata->vif.csa_active = true;
1288         sdata->csa_chandef = csa_ie.chandef;
1289         sdata->csa_block_tx = csa_ie.mode;
1290         ifmgd->csa_ignored_same_chan = false;
1291
1292         if (sdata->csa_block_tx)
1293                 ieee80211_stop_vif_queues(local, sdata,
1294                                           IEEE80211_QUEUE_STOP_REASON_CSA);
1295         mutex_unlock(&local->mtx);
1296
1297         cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
1298                                           csa_ie.count);
1299
1300         if (local->ops->channel_switch) {
1301                 /* use driver's channel switch callback */
1302                 drv_channel_switch(local, sdata, &ch_switch);
1303                 return;
1304         }
1305
1306         /* channel switch handled in software */
1307         if (csa_ie.count <= 1)
1308                 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
1309         else
1310                 mod_timer(&ifmgd->chswitch_timer,
1311                           TU_TO_EXP_TIME((csa_ie.count - 1) *
1312                                          cbss->beacon_interval));
1313         return;
1314  drop_connection:
1315         ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1316         mutex_unlock(&local->chanctx_mtx);
1317         mutex_unlock(&local->mtx);
1318 }
1319
1320 static bool
1321 ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1322                                  struct ieee80211_channel *channel,
1323                                  const u8 *country_ie, u8 country_ie_len,
1324                                  const u8 *pwr_constr_elem,
1325                                  int *chan_pwr, int *pwr_reduction)
1326 {
1327         struct ieee80211_country_ie_triplet *triplet;
1328         int chan = ieee80211_frequency_to_channel(channel->center_freq);
1329         int i, chan_increment;
1330         bool have_chan_pwr = false;
1331
1332         /* Invalid IE */
1333         if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1334                 return false;
1335
1336         triplet = (void *)(country_ie + 3);
1337         country_ie_len -= 3;
1338
1339         switch (channel->band) {
1340         default:
1341                 WARN_ON_ONCE(1);
1342                 /* fall through */
1343         case NL80211_BAND_2GHZ:
1344         case NL80211_BAND_60GHZ:
1345                 chan_increment = 1;
1346                 break;
1347         case NL80211_BAND_5GHZ:
1348                 chan_increment = 4;
1349                 break;
1350         }
1351
1352         /* find channel */
1353         while (country_ie_len >= 3) {
1354                 u8 first_channel = triplet->chans.first_channel;
1355
1356                 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1357                         goto next;
1358
1359                 for (i = 0; i < triplet->chans.num_channels; i++) {
1360                         if (first_channel + i * chan_increment == chan) {
1361                                 have_chan_pwr = true;
1362                                 *chan_pwr = triplet->chans.max_power;
1363                                 break;
1364                         }
1365                 }
1366                 if (have_chan_pwr)
1367                         break;
1368
1369  next:
1370                 triplet++;
1371                 country_ie_len -= 3;
1372         }
1373
1374         if (have_chan_pwr && pwr_constr_elem)
1375                 *pwr_reduction = *pwr_constr_elem;
1376         else
1377                 *pwr_reduction = 0;
1378
1379         return have_chan_pwr;
1380 }
1381
1382 static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
1383                                       struct ieee80211_channel *channel,
1384                                       const u8 *cisco_dtpc_ie,
1385                                       int *pwr_level)
1386 {
1387         /* From practical testing, the first data byte of the DTPC element
1388          * seems to contain the requested dBm level, and the CLI on Cisco
1389          * APs clearly state the range is -127 to 127 dBm, which indicates
1390          * a signed byte, although it seemingly never actually goes negative.
1391          * The other byte seems to always be zero.
1392          */
1393         *pwr_level = (__s8)cisco_dtpc_ie[4];
1394 }
1395
1396 static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1397                                        struct ieee80211_channel *channel,
1398                                        struct ieee80211_mgmt *mgmt,
1399                                        const u8 *country_ie, u8 country_ie_len,
1400                                        const u8 *pwr_constr_ie,
1401                                        const u8 *cisco_dtpc_ie)
1402 {
1403         bool has_80211h_pwr = false, has_cisco_pwr = false;
1404         int chan_pwr = 0, pwr_reduction_80211h = 0;
1405         int pwr_level_cisco, pwr_level_80211h;
1406         int new_ap_level;
1407         __le16 capab = mgmt->u.probe_resp.capab_info;
1408
1409         if (country_ie &&
1410             (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
1411              capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
1412                 has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
1413                         sdata, channel, country_ie, country_ie_len,
1414                         pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
1415                 pwr_level_80211h =
1416                         max_t(int, 0, chan_pwr - pwr_reduction_80211h);
1417         }
1418
1419         if (cisco_dtpc_ie) {
1420                 ieee80211_find_cisco_dtpc(
1421                         sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
1422                 has_cisco_pwr = true;
1423         }
1424
1425         if (!has_80211h_pwr && !has_cisco_pwr)
1426                 return 0;
1427
1428         /* If we have both 802.11h and Cisco DTPC, apply both limits
1429          * by picking the smallest of the two power levels advertised.
1430          */
1431         if (has_80211h_pwr &&
1432             (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
1433                 new_ap_level = pwr_level_80211h;
1434
1435                 if (sdata->ap_power_level == new_ap_level)
1436                         return 0;
1437
1438                 sdata_dbg(sdata,
1439                           "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1440                           pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1441                           sdata->u.mgd.bssid);
1442         } else {  /* has_cisco_pwr is always true here. */
1443                 new_ap_level = pwr_level_cisco;
1444
1445                 if (sdata->ap_power_level == new_ap_level)
1446                         return 0;
1447
1448                 sdata_dbg(sdata,
1449                           "Limiting TX power to %d dBm as advertised by %pM\n",
1450                           pwr_level_cisco, sdata->u.mgd.bssid);
1451         }
1452
1453         sdata->ap_power_level = new_ap_level;
1454         if (__ieee80211_recalc_txpower(sdata))
1455                 return BSS_CHANGED_TXPOWER;
1456         return 0;
1457 }
1458
1459 /* powersave */
1460 static void ieee80211_enable_ps(struct ieee80211_local *local,
1461                                 struct ieee80211_sub_if_data *sdata)
1462 {
1463         struct ieee80211_conf *conf = &local->hw.conf;
1464
1465         /*
1466          * If we are scanning right now then the parameters will
1467          * take effect when scan finishes.
1468          */
1469         if (local->scanning)
1470                 return;
1471
1472         if (conf->dynamic_ps_timeout > 0 &&
1473             !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
1474                 mod_timer(&local->dynamic_ps_timer, jiffies +
1475                           msecs_to_jiffies(conf->dynamic_ps_timeout));
1476         } else {
1477                 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
1478                         ieee80211_send_nullfunc(local, sdata, true);
1479
1480                 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1481                     ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1482                         return;
1483
1484                 conf->flags |= IEEE80211_CONF_PS;
1485                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1486         }
1487 }
1488
1489 static void ieee80211_change_ps(struct ieee80211_local *local)
1490 {
1491         struct ieee80211_conf *conf = &local->hw.conf;
1492
1493         if (local->ps_sdata) {
1494                 ieee80211_enable_ps(local, local->ps_sdata);
1495         } else if (conf->flags & IEEE80211_CONF_PS) {
1496                 conf->flags &= ~IEEE80211_CONF_PS;
1497                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1498                 del_timer_sync(&local->dynamic_ps_timer);
1499                 cancel_work_sync(&local->dynamic_ps_enable_work);
1500         }
1501 }
1502
1503 static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1504 {
1505         struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1506         struct sta_info *sta = NULL;
1507         bool authorized = false;
1508
1509         if (!mgd->powersave)
1510                 return false;
1511
1512         if (mgd->broken_ap)
1513                 return false;
1514
1515         if (!mgd->associated)
1516                 return false;
1517
1518         if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
1519                 return false;
1520
1521         if (!mgd->have_beacon)
1522                 return false;
1523
1524         rcu_read_lock();
1525         sta = sta_info_get(sdata, mgd->bssid);
1526         if (sta)
1527                 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1528         rcu_read_unlock();
1529
1530         return authorized;
1531 }
1532
1533 /* need to hold RTNL or interface lock */
1534 void ieee80211_recalc_ps(struct ieee80211_local *local)
1535 {
1536         struct ieee80211_sub_if_data *sdata, *found = NULL;
1537         int count = 0;
1538         int timeout;
1539
1540         if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
1541                 local->ps_sdata = NULL;
1542                 return;
1543         }
1544
1545         list_for_each_entry(sdata, &local->interfaces, list) {
1546                 if (!ieee80211_sdata_running(sdata))
1547                         continue;
1548                 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1549                         /* If an AP vif is found, then disable PS
1550                          * by setting the count to zero thereby setting
1551                          * ps_sdata to NULL.
1552                          */
1553                         count = 0;
1554                         break;
1555                 }
1556                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1557                         continue;
1558                 found = sdata;
1559                 count++;
1560         }
1561
1562         if (count == 1 && ieee80211_powersave_allowed(found)) {
1563                 u8 dtimper = found->u.mgd.dtim_period;
1564
1565                 timeout = local->dynamic_ps_forced_timeout;
1566                 if (timeout < 0)
1567                         timeout = 100;
1568                 local->hw.conf.dynamic_ps_timeout = timeout;
1569
1570                 /* If the TIM IE is invalid, pretend the value is 1 */
1571                 if (!dtimper)
1572                         dtimper = 1;
1573
1574                 local->hw.conf.ps_dtim_period = dtimper;
1575                 local->ps_sdata = found;
1576         } else {
1577                 local->ps_sdata = NULL;
1578         }
1579
1580         ieee80211_change_ps(local);
1581 }
1582
1583 void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1584 {
1585         bool ps_allowed = ieee80211_powersave_allowed(sdata);
1586
1587         if (sdata->vif.bss_conf.ps != ps_allowed) {
1588                 sdata->vif.bss_conf.ps = ps_allowed;
1589                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1590         }
1591 }
1592
1593 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1594 {
1595         struct ieee80211_local *local =
1596                 container_of(work, struct ieee80211_local,
1597                              dynamic_ps_disable_work);
1598
1599         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1600                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1601                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1602         }
1603
1604         ieee80211_wake_queues_by_reason(&local->hw,
1605                                         IEEE80211_MAX_QUEUE_MAP,
1606                                         IEEE80211_QUEUE_STOP_REASON_PS,
1607                                         false);
1608 }
1609
1610 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1611 {
1612         struct ieee80211_local *local =
1613                 container_of(work, struct ieee80211_local,
1614                              dynamic_ps_enable_work);
1615         struct ieee80211_sub_if_data *sdata = local->ps_sdata;
1616         struct ieee80211_if_managed *ifmgd;
1617         unsigned long flags;
1618         int q;
1619
1620         /* can only happen when PS was just disabled anyway */
1621         if (!sdata)
1622                 return;
1623
1624         ifmgd = &sdata->u.mgd;
1625
1626         if (local->hw.conf.flags & IEEE80211_CONF_PS)
1627                 return;
1628
1629         if (local->hw.conf.dynamic_ps_timeout > 0) {
1630                 /* don't enter PS if TX frames are pending */
1631                 if (drv_tx_frames_pending(local)) {
1632                         mod_timer(&local->dynamic_ps_timer, jiffies +
1633                                   msecs_to_jiffies(
1634                                   local->hw.conf.dynamic_ps_timeout));
1635                         return;
1636                 }
1637
1638                 /*
1639                  * transmission can be stopped by others which leads to
1640                  * dynamic_ps_timer expiry. Postpone the ps timer if it
1641                  * is not the actual idle state.
1642                  */
1643                 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1644                 for (q = 0; q < local->hw.queues; q++) {
1645                         if (local->queue_stop_reasons[q]) {
1646                                 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1647                                                        flags);
1648                                 mod_timer(&local->dynamic_ps_timer, jiffies +
1649                                           msecs_to_jiffies(
1650                                           local->hw.conf.dynamic_ps_timeout));
1651                                 return;
1652                         }
1653                 }
1654                 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1655         }
1656
1657         if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1658             !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1659                 if (drv_tx_frames_pending(local)) {
1660                         mod_timer(&local->dynamic_ps_timer, jiffies +
1661                                   msecs_to_jiffies(
1662                                   local->hw.conf.dynamic_ps_timeout));
1663                 } else {
1664                         ieee80211_send_nullfunc(local, sdata, true);
1665                         /* Flush to get the tx status of nullfunc frame */
1666                         ieee80211_flush_queues(local, sdata, false);
1667                 }
1668         }
1669
1670         if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1671               ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
1672             (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1673                 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1674                 local->hw.conf.flags |= IEEE80211_CONF_PS;
1675                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1676         }
1677 }
1678
1679 void ieee80211_dynamic_ps_timer(struct timer_list *t)
1680 {
1681         struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
1682
1683         ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1684 }
1685
1686 void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1687 {
1688         struct delayed_work *delayed_work = to_delayed_work(work);
1689         struct ieee80211_sub_if_data *sdata =
1690                 container_of(delayed_work, struct ieee80211_sub_if_data,
1691                              dfs_cac_timer_work);
1692         struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
1693
1694         mutex_lock(&sdata->local->mtx);
1695         if (sdata->wdev.cac_started) {
1696                 ieee80211_vif_release_channel(sdata);
1697                 cfg80211_cac_event(sdata->dev, &chandef,
1698                                    NL80211_RADAR_CAC_FINISHED,
1699                                    GFP_KERNEL);
1700         }
1701         mutex_unlock(&sdata->local->mtx);
1702 }
1703
1704 static bool
1705 __ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1706 {
1707         struct ieee80211_local *local = sdata->local;
1708         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1709         bool ret = false;
1710         int ac;
1711
1712         if (local->hw.queues < IEEE80211_NUM_ACS)
1713                 return false;
1714
1715         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1716                 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
1717                 int non_acm_ac;
1718                 unsigned long now = jiffies;
1719
1720                 if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
1721                     tx_tspec->admitted_time &&
1722                     time_after(now, tx_tspec->time_slice_start + HZ)) {
1723                         tx_tspec->consumed_tx_time = 0;
1724                         tx_tspec->time_slice_start = now;
1725
1726                         if (tx_tspec->downgraded)
1727                                 tx_tspec->action =
1728                                         TX_TSPEC_ACTION_STOP_DOWNGRADE;
1729                 }
1730
1731                 switch (tx_tspec->action) {
1732                 case TX_TSPEC_ACTION_STOP_DOWNGRADE:
1733                         /* take the original parameters */
1734                         if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
1735                                 sdata_err(sdata,
1736                                           "failed to set TX queue parameters for queue %d\n",
1737                                           ac);
1738                         tx_tspec->action = TX_TSPEC_ACTION_NONE;
1739                         tx_tspec->downgraded = false;
1740                         ret = true;
1741                         break;
1742                 case TX_TSPEC_ACTION_DOWNGRADE:
1743                         if (time_after(now, tx_tspec->time_slice_start + HZ)) {
1744                                 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1745                                 ret = true;
1746                                 break;
1747                         }
1748                         /* downgrade next lower non-ACM AC */
1749                         for (non_acm_ac = ac + 1;
1750                              non_acm_ac < IEEE80211_NUM_ACS;
1751                              non_acm_ac++)
1752                                 if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
1753                                         break;
1754                         /* Usually the loop will result in using BK even if it
1755                          * requires admission control, but such a configuration
1756                          * makes no sense and we have to transmit somehow - the
1757                          * AC selection does the same thing.
1758                          * If we started out trying to downgrade from BK, then
1759                          * the extra condition here might be needed.
1760                          */
1761                         if (non_acm_ac >= IEEE80211_NUM_ACS)
1762                                 non_acm_ac = IEEE80211_AC_BK;
1763                         if (drv_conf_tx(local, sdata, ac,
1764                                         &sdata->tx_conf[non_acm_ac]))
1765                                 sdata_err(sdata,
1766                                           "failed to set TX queue parameters for queue %d\n",
1767                                           ac);
1768                         tx_tspec->action = TX_TSPEC_ACTION_NONE;
1769                         ret = true;
1770                         schedule_delayed_work(&ifmgd->tx_tspec_wk,
1771                                 tx_tspec->time_slice_start + HZ - now + 1);
1772                         break;
1773                 case TX_TSPEC_ACTION_NONE:
1774                         /* nothing now */
1775                         break;
1776                 }
1777         }
1778
1779         return ret;
1780 }
1781
1782 void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1783 {
1784         if (__ieee80211_sta_handle_tspec_ac_params(sdata))
1785                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
1786 }
1787
1788 static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
1789 {
1790         struct ieee80211_sub_if_data *sdata;
1791
1792         sdata = container_of(work, struct ieee80211_sub_if_data,
1793                              u.mgd.tx_tspec_wk.work);
1794         ieee80211_sta_handle_tspec_ac_params(sdata);
1795 }
1796
1797 /* MLME */
1798 static bool
1799 ieee80211_sta_wmm_params(struct ieee80211_local *local,
1800                          struct ieee80211_sub_if_data *sdata,
1801                          const u8 *wmm_param, size_t wmm_param_len,
1802                          const struct ieee80211_mu_edca_param_set *mu_edca)
1803 {
1804         struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
1805         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1806         size_t left;
1807         int count, ac;
1808         const u8 *pos;
1809         u8 uapsd_queues = 0;
1810
1811         if (!local->ops->conf_tx)
1812                 return false;
1813
1814         if (local->hw.queues < IEEE80211_NUM_ACS)
1815                 return false;
1816
1817         if (!wmm_param)
1818                 return false;
1819
1820         if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
1821                 return false;
1822
1823         if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
1824                 uapsd_queues = ifmgd->uapsd_queues;
1825
1826         count = wmm_param[6] & 0x0f;
1827         if (count == ifmgd->wmm_last_param_set)
1828                 return false;
1829         ifmgd->wmm_last_param_set = count;
1830
1831         pos = wmm_param + 8;
1832         left = wmm_param_len - 8;
1833
1834         memset(&params, 0, sizeof(params));
1835
1836         sdata->wmm_acm = 0;
1837         for (; left >= 4; left -= 4, pos += 4) {
1838                 int aci = (pos[0] >> 5) & 0x03;
1839                 int acm = (pos[0] >> 4) & 0x01;
1840                 bool uapsd = false;
1841
1842                 switch (aci) {
1843                 case 1: /* AC_BK */
1844                         ac = IEEE80211_AC_BK;
1845                         if (acm)
1846                                 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
1847                         if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1848                                 uapsd = true;
1849                         params[ac].mu_edca = !!mu_edca;
1850                         if (mu_edca)
1851                                 params[ac].mu_edca_param_rec = mu_edca->ac_bk;
1852                         break;
1853                 case 2: /* AC_VI */
1854                         ac = IEEE80211_AC_VI;
1855                         if (acm)
1856                                 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
1857                         if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1858                                 uapsd = true;
1859                         params[ac].mu_edca = !!mu_edca;
1860                         if (mu_edca)
1861                                 params[ac].mu_edca_param_rec = mu_edca->ac_vi;
1862                         break;
1863                 case 3: /* AC_VO */
1864                         ac = IEEE80211_AC_VO;
1865                         if (acm)
1866                                 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
1867                         if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1868                                 uapsd = true;
1869                         params[ac].mu_edca = !!mu_edca;
1870                         if (mu_edca)
1871                                 params[ac].mu_edca_param_rec = mu_edca->ac_vo;
1872                         break;
1873                 case 0: /* AC_BE */
1874                 default:
1875                         ac = IEEE80211_AC_BE;
1876                         if (acm)
1877                                 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
1878                         if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1879                                 uapsd = true;
1880                         params[ac].mu_edca = !!mu_edca;
1881                         if (mu_edca)
1882                                 params[ac].mu_edca_param_rec = mu_edca->ac_be;
1883                         break;
1884                 }
1885
1886                 params[ac].aifs = pos[0] & 0x0f;
1887
1888                 if (params[ac].aifs < 2) {
1889                         sdata_info(sdata,
1890                                    "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
1891                                    params[ac].aifs, aci);
1892                         params[ac].aifs = 2;
1893                 }
1894                 params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1895                 params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
1896                 params[ac].txop = get_unaligned_le16(pos + 2);
1897                 params[ac].acm = acm;
1898                 params[ac].uapsd = uapsd;
1899
1900                 if (params[ac].cw_min == 0 ||
1901                     params[ac].cw_min > params[ac].cw_max) {
1902                         sdata_info(sdata,
1903                                    "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
1904                                    params[ac].cw_min, params[ac].cw_max, aci);
1905                         return false;
1906                 }
1907                 ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
1908         }
1909
1910         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1911                 mlme_dbg(sdata,
1912                          "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
1913                          ac, params[ac].acm,
1914                          params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
1915                          params[ac].txop, params[ac].uapsd,
1916                          ifmgd->tx_tspec[ac].downgraded);
1917                 sdata->tx_conf[ac] = params[ac];
1918                 if (!ifmgd->tx_tspec[ac].downgraded &&
1919                     drv_conf_tx(local, sdata, ac, &params[ac]))
1920                         sdata_err(sdata,
1921                                   "failed to set TX queue parameters for AC %d\n",
1922                                   ac);
1923         }
1924
1925         /* enable WMM or activate new settings */
1926         sdata->vif.bss_conf.qos = true;
1927         return true;
1928 }
1929
1930 static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1931 {
1932         lockdep_assert_held(&sdata->local->mtx);
1933
1934         sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
1935         ieee80211_run_deferred_scan(sdata->local);
1936 }
1937
1938 static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1939 {
1940         mutex_lock(&sdata->local->mtx);
1941         __ieee80211_stop_poll(sdata);
1942         mutex_unlock(&sdata->local->mtx);
1943 }
1944
1945 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1946                                            u16 capab, bool erp_valid, u8 erp)
1947 {
1948         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1949         struct ieee80211_supported_band *sband;
1950         u32 changed = 0;
1951         bool use_protection;
1952         bool use_short_preamble;
1953         bool use_short_slot;
1954
1955         sband = ieee80211_get_sband(sdata);
1956         if (!sband)
1957                 return changed;
1958
1959         if (erp_valid) {
1960                 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1961                 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1962         } else {
1963                 use_protection = false;
1964                 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1965         }
1966
1967         use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
1968         if (sband->band == NL80211_BAND_5GHZ)
1969                 use_short_slot = true;
1970
1971         if (use_protection != bss_conf->use_cts_prot) {
1972                 bss_conf->use_cts_prot = use_protection;
1973                 changed |= BSS_CHANGED_ERP_CTS_PROT;
1974         }
1975
1976         if (use_short_preamble != bss_conf->use_short_preamble) {
1977                 bss_conf->use_short_preamble = use_short_preamble;
1978                 changed |= BSS_CHANGED_ERP_PREAMBLE;
1979         }
1980
1981         if (use_short_slot != bss_conf->use_short_slot) {
1982                 bss_conf->use_short_slot = use_short_slot;
1983                 changed |= BSS_CHANGED_ERP_SLOT;
1984         }
1985
1986         return changed;
1987 }
1988
1989 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
1990                                      struct cfg80211_bss *cbss,
1991                                      u32 bss_info_changed)
1992 {
1993         struct ieee80211_bss *bss = (void *)cbss->priv;
1994         struct ieee80211_local *local = sdata->local;
1995         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1996
1997         bss_info_changed |= BSS_CHANGED_ASSOC;
1998         bss_info_changed |= ieee80211_handle_bss_capability(sdata,
1999                 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
2000
2001         sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
2002                 beacon_loss_count * bss_conf->beacon_int));
2003
2004         sdata->u.mgd.associated = cbss;
2005         memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
2006
2007         ieee80211_check_rate_mask(sdata);
2008
2009         sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
2010
2011         if (sdata->vif.p2p ||
2012             sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
2013                 const struct cfg80211_bss_ies *ies;
2014
2015                 rcu_read_lock();
2016                 ies = rcu_dereference(cbss->ies);
2017                 if (ies) {
2018                         int ret;
2019
2020                         ret = cfg80211_get_p2p_attr(
2021                                         ies->data, ies->len,
2022                                         IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2023                                         (u8 *) &bss_conf->p2p_noa_attr,
2024                                         sizeof(bss_conf->p2p_noa_attr));
2025                         if (ret >= 2) {
2026                                 sdata->u.mgd.p2p_noa_index =
2027                                         bss_conf->p2p_noa_attr.index;
2028                                 bss_info_changed |= BSS_CHANGED_P2P_PS;
2029                         }
2030                 }
2031                 rcu_read_unlock();
2032         }
2033
2034         /* just to be sure */
2035         ieee80211_stop_poll(sdata);
2036
2037         ieee80211_led_assoc(local, 1);
2038
2039         if (sdata->u.mgd.have_beacon) {
2040                 /*
2041                  * If the AP is buggy we may get here with no DTIM period
2042                  * known, so assume it's 1 which is the only safe assumption
2043                  * in that case, although if the TIM IE is broken powersave
2044                  * probably just won't work at all.
2045                  */
2046                 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
2047                 bss_conf->beacon_rate = bss->beacon_rate;
2048                 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
2049         } else {
2050                 bss_conf->beacon_rate = NULL;
2051                 bss_conf->dtim_period = 0;
2052         }
2053
2054         bss_conf->assoc = 1;
2055
2056         /* Tell the driver to monitor connection quality (if supported) */
2057         if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
2058             bss_conf->cqm_rssi_thold)
2059                 bss_info_changed |= BSS_CHANGED_CQM;
2060
2061         /* Enable ARP filtering */
2062         if (bss_conf->arp_addr_cnt)
2063                 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
2064
2065         ieee80211_bss_info_change_notify(sdata, bss_info_changed);
2066
2067         mutex_lock(&local->iflist_mtx);
2068         ieee80211_recalc_ps(local);
2069         mutex_unlock(&local->iflist_mtx);
2070
2071         ieee80211_recalc_smps(sdata);
2072         ieee80211_recalc_ps_vif(sdata);
2073
2074         netif_carrier_on(sdata->dev);
2075 }
2076
2077 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
2078                                    u16 stype, u16 reason, bool tx,
2079                                    u8 *frame_buf)
2080 {
2081         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2082         struct ieee80211_local *local = sdata->local;
2083         u32 changed = 0;
2084
2085         sdata_assert_lock(sdata);
2086
2087         if (WARN_ON_ONCE(tx && !frame_buf))
2088                 return;
2089
2090         if (WARN_ON(!ifmgd->associated))
2091                 return;
2092
2093         ieee80211_stop_poll(sdata);
2094
2095         ifmgd->associated = NULL;
2096         netif_carrier_off(sdata->dev);
2097
2098         /*
2099          * if we want to get out of ps before disassoc (why?) we have
2100          * to do it before sending disassoc, as otherwise the null-packet
2101          * won't be valid.
2102          */
2103         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2104                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2105                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2106         }
2107         local->ps_sdata = NULL;
2108
2109         /* disable per-vif ps */
2110         ieee80211_recalc_ps_vif(sdata);
2111
2112         /* make sure ongoing transmission finishes */
2113         synchronize_net();
2114
2115         /*
2116          * drop any frame before deauth/disassoc, this can be data or
2117          * management frame. Since we are disconnecting, we should not
2118          * insist sending these frames which can take time and delay
2119          * the disconnection and possible the roaming.
2120          */
2121         if (tx)
2122                 ieee80211_flush_queues(local, sdata, true);
2123
2124         /* deauthenticate/disassociate now */
2125         if (tx || frame_buf) {
2126                 /*
2127                  * In multi channel scenarios guarantee that the virtual
2128                  * interface is granted immediate airtime to transmit the
2129                  * deauthentication frame by calling mgd_prepare_tx, if the
2130                  * driver requested so.
2131                  */
2132                 if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
2133                     !ifmgd->have_beacon)
2134                         drv_mgd_prepare_tx(sdata->local, sdata, 0);
2135
2136                 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
2137                                                reason, tx, frame_buf);
2138         }
2139
2140         /* flush out frame - make sure the deauth was actually sent */
2141         if (tx)
2142                 ieee80211_flush_queues(local, sdata, false);
2143
2144         /* clear bssid only after building the needed mgmt frames */
2145         eth_zero_addr(ifmgd->bssid);
2146
2147         /* remove AP and TDLS peers */
2148         sta_info_flush(sdata);
2149
2150         /* finally reset all BSS / config parameters */
2151         changed |= ieee80211_reset_erp_info(sdata);
2152
2153         ieee80211_led_assoc(local, 0);
2154         changed |= BSS_CHANGED_ASSOC;
2155         sdata->vif.bss_conf.assoc = false;
2156
2157         ifmgd->p2p_noa_index = -1;
2158         memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2159                sizeof(sdata->vif.bss_conf.p2p_noa_attr));
2160
2161         /* on the next assoc, re-program HT/VHT parameters */
2162         memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2163         memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
2164         memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2165         memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
2166
2167         /* reset MU-MIMO ownership and group data */
2168         memset(sdata->vif.bss_conf.mu_group.membership, 0,
2169                sizeof(sdata->vif.bss_conf.mu_group.membership));
2170         memset(sdata->vif.bss_conf.mu_group.position, 0,
2171                sizeof(sdata->vif.bss_conf.mu_group.position));
2172         changed |= BSS_CHANGED_MU_GROUPS;
2173         sdata->vif.mu_mimo_owner = false;
2174
2175         sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
2176
2177         del_timer_sync(&local->dynamic_ps_timer);
2178         cancel_work_sync(&local->dynamic_ps_enable_work);
2179
2180         /* Disable ARP filtering */
2181         if (sdata->vif.bss_conf.arp_addr_cnt)
2182                 changed |= BSS_CHANGED_ARP_FILTER;
2183
2184         sdata->vif.bss_conf.qos = false;
2185         changed |= BSS_CHANGED_QOS;
2186
2187         /* The BSSID (not really interesting) and HT changed */
2188         changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
2189         ieee80211_bss_info_change_notify(sdata, changed);
2190
2191         /* disassociated - set to defaults now */
2192         ieee80211_set_wmm_default(sdata, false, false);
2193
2194         del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2195         del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2196         del_timer_sync(&sdata->u.mgd.timer);
2197         del_timer_sync(&sdata->u.mgd.chswitch_timer);
2198
2199         sdata->vif.bss_conf.dtim_period = 0;
2200         sdata->vif.bss_conf.beacon_rate = NULL;
2201
2202         ifmgd->have_beacon = false;
2203
2204         ifmgd->flags = 0;
2205         mutex_lock(&local->mtx);
2206         ieee80211_vif_release_channel(sdata);
2207
2208         sdata->vif.csa_active = false;
2209         ifmgd->csa_waiting_bcn = false;
2210         ifmgd->csa_ignored_same_chan = false;
2211         if (sdata->csa_block_tx) {
2212                 ieee80211_wake_vif_queues(local, sdata,
2213                                           IEEE80211_QUEUE_STOP_REASON_CSA);
2214                 sdata->csa_block_tx = false;
2215         }
2216         mutex_unlock(&local->mtx);
2217
2218         /* existing TX TSPEC sessions no longer exist */
2219         memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2220         cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2221
2222         sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
2223 }
2224
2225 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
2226                              struct ieee80211_hdr *hdr)
2227 {
2228         /*
2229          * We can postpone the mgd.timer whenever receiving unicast frames
2230          * from AP because we know that the connection is working both ways
2231          * at that time. But multicast frames (and hence also beacons) must
2232          * be ignored here, because we need to trigger the timer during
2233          * data idle periods for sending the periodic probe request to the
2234          * AP we're connected to.
2235          */
2236         if (is_multicast_ether_addr(hdr->addr1))
2237                 return;
2238
2239         ieee80211_sta_reset_conn_monitor(sdata);
2240 }
2241
2242 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2243 {
2244         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2245         struct ieee80211_local *local = sdata->local;
2246
2247         mutex_lock(&local->mtx);
2248         if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2249                 goto out;
2250
2251         __ieee80211_stop_poll(sdata);
2252
2253         mutex_lock(&local->iflist_mtx);
2254         ieee80211_recalc_ps(local);
2255         mutex_unlock(&local->iflist_mtx);
2256
2257         if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
2258                 goto out;
2259
2260         /*
2261          * We've received a probe response, but are not sure whether
2262          * we have or will be receiving any beacons or data, so let's
2263          * schedule the timers again, just in case.
2264          */
2265         ieee80211_sta_reset_beacon_monitor(sdata);
2266
2267         mod_timer(&ifmgd->conn_mon_timer,
2268                   round_jiffies_up(jiffies +
2269                                    IEEE80211_CONNECTION_IDLE_TIME));
2270 out:
2271         mutex_unlock(&local->mtx);
2272 }
2273
2274 static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
2275                                            struct ieee80211_hdr *hdr,
2276                                            u16 tx_time)
2277 {
2278         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2279         u16 tid = ieee80211_get_tid(hdr);
2280         int ac = ieee80211_ac_from_tid(tid);
2281         struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
2282         unsigned long now = jiffies;
2283
2284         if (likely(!tx_tspec->admitted_time))
2285                 return;
2286
2287         if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2288                 tx_tspec->consumed_tx_time = 0;
2289                 tx_tspec->time_slice_start = now;
2290
2291                 if (tx_tspec->downgraded) {
2292                         tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2293                         schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2294                 }
2295         }
2296
2297         if (tx_tspec->downgraded)
2298                 return;
2299
2300         tx_tspec->consumed_tx_time += tx_time;
2301
2302         if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2303                 tx_tspec->downgraded = true;
2304                 tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2305                 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2306         }
2307 }
2308
2309 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2310                              struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
2311 {
2312         ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2313
2314         if (!ieee80211_is_data(hdr->frame_control))
2315             return;
2316
2317         if (ieee80211_is_nullfunc(hdr->frame_control) &&
2318             sdata->u.mgd.probe_send_count > 0) {
2319                 if (ack)
2320                         ieee80211_sta_reset_conn_monitor(sdata);
2321                 else
2322                         sdata->u.mgd.nullfunc_failed = true;
2323                 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
2324                 return;
2325         }
2326
2327         if (ack)
2328                 ieee80211_sta_reset_conn_monitor(sdata);
2329 }
2330
2331 static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2332                                           const u8 *src, const u8 *dst,
2333                                           const u8 *ssid, size_t ssid_len,
2334                                           struct ieee80211_channel *channel)
2335 {
2336         struct sk_buff *skb;
2337
2338         skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2339                                         ssid, ssid_len, NULL, 0,
2340                                         IEEE80211_PROBE_FLAG_DIRECTED);
2341         if (skb)
2342                 ieee80211_tx_skb(sdata, skb);
2343 }
2344
2345 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2346 {
2347         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2348         const u8 *ssid;
2349         u8 *dst = ifmgd->associated->bssid;
2350         u8 unicast_limit = max(1, max_probe_tries - 3);
2351         struct sta_info *sta;
2352
2353         /*
2354          * Try sending broadcast probe requests for the last three
2355          * probe requests after the first ones failed since some
2356          * buggy APs only support broadcast probe requests.
2357          */
2358         if (ifmgd->probe_send_count >= unicast_limit)
2359                 dst = NULL;
2360
2361         /*
2362          * When the hardware reports an accurate Tx ACK status, it's
2363          * better to send a nullfunc frame instead of a probe request,
2364          * as it will kick us off the AP quickly if we aren't associated
2365          * anymore. The timeout will be reset if the frame is ACKed by
2366          * the AP.
2367          */
2368         ifmgd->probe_send_count++;
2369
2370         if (dst) {
2371                 mutex_lock(&sdata->local->sta_mtx);
2372                 sta = sta_info_get(sdata, dst);
2373                 if (!WARN_ON(!sta))
2374                         ieee80211_check_fast_rx(sta);
2375                 mutex_unlock(&sdata->local->sta_mtx);
2376         }
2377
2378         if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
2379                 ifmgd->nullfunc_failed = false;
2380                 ieee80211_send_nullfunc(sdata->local, sdata, false);
2381         } else {
2382                 int ssid_len;
2383
2384                 rcu_read_lock();
2385                 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
2386                 if (WARN_ON_ONCE(ssid == NULL))
2387                         ssid_len = 0;
2388                 else
2389                         ssid_len = ssid[1];
2390
2391                 ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
2392                                               ssid + 2, ssid_len,
2393                                               ifmgd->associated->channel);
2394                 rcu_read_unlock();
2395         }
2396
2397         ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
2398         run_again(sdata, ifmgd->probe_timeout);
2399 }
2400
2401 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2402                                    bool beacon)
2403 {
2404         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2405         bool already = false;
2406
2407         if (!ieee80211_sdata_running(sdata))
2408                 return;
2409
2410         sdata_lock(sdata);
2411
2412         if (!ifmgd->associated)
2413                 goto out;
2414
2415         mutex_lock(&sdata->local->mtx);
2416
2417         if (sdata->local->tmp_channel || sdata->local->scanning) {
2418                 mutex_unlock(&sdata->local->mtx);
2419                 goto out;
2420         }
2421
2422         if (beacon) {
2423                 mlme_dbg_ratelimited(sdata,
2424                                      "detected beacon loss from AP (missed %d beacons) - probing\n",
2425                                      beacon_loss_count);
2426
2427                 ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
2428         }
2429
2430         /*
2431          * The driver/our work has already reported this event or the
2432          * connection monitoring has kicked in and we have already sent
2433          * a probe request. Or maybe the AP died and the driver keeps
2434          * reporting until we disassociate...
2435          *
2436          * In either case we have to ignore the current call to this
2437          * function (except for setting the correct probe reason bit)
2438          * because otherwise we would reset the timer every time and
2439          * never check whether we received a probe response!
2440          */
2441         if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
2442                 already = true;
2443
2444         ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2445
2446         mutex_unlock(&sdata->local->mtx);
2447
2448         if (already)
2449                 goto out;
2450
2451         mutex_lock(&sdata->local->iflist_mtx);
2452         ieee80211_recalc_ps(sdata->local);
2453         mutex_unlock(&sdata->local->iflist_mtx);
2454
2455         ifmgd->probe_send_count = 0;
2456         ieee80211_mgd_probe_ap_send(sdata);
2457  out:
2458         sdata_unlock(sdata);
2459 }
2460
2461 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2462                                           struct ieee80211_vif *vif)
2463 {
2464         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2465         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2466         struct cfg80211_bss *cbss;
2467         struct sk_buff *skb;
2468         const u8 *ssid;
2469         int ssid_len;
2470
2471         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2472                 return NULL;
2473
2474         sdata_assert_lock(sdata);
2475
2476         if (ifmgd->associated)
2477                 cbss = ifmgd->associated;
2478         else if (ifmgd->auth_data)
2479                 cbss = ifmgd->auth_data->bss;
2480         else if (ifmgd->assoc_data)
2481                 cbss = ifmgd->assoc_data->bss;
2482         else
2483                 return NULL;
2484
2485         rcu_read_lock();
2486         ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
2487         if (WARN_ON_ONCE(ssid == NULL))
2488                 ssid_len = 0;
2489         else
2490                 ssid_len = ssid[1];
2491
2492         skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
2493                                         (u32) -1, cbss->channel,
2494                                         ssid + 2, ssid_len,
2495                                         NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
2496         rcu_read_unlock();
2497
2498         return skb;
2499 }
2500 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2501
2502 static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2503                                         const u8 *buf, size_t len, bool tx,
2504                                         u16 reason)
2505 {
2506         struct ieee80211_event event = {
2507                 .type = MLME_EVENT,
2508                 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2509                 .u.mlme.reason = reason,
2510         };
2511
2512         if (tx)
2513                 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
2514         else
2515                 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2516
2517         drv_event_callback(sdata->local, sdata, &event);
2518 }
2519
2520 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2521 {
2522         struct ieee80211_local *local = sdata->local;
2523         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2524         u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
2525
2526         sdata_lock(sdata);
2527         if (!ifmgd->associated) {
2528                 sdata_unlock(sdata);
2529                 return;
2530         }
2531
2532         /* AP is probably out of range (or not reachable for another reason) so
2533          * remove the bss struct for that AP.
2534          */
2535         cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2536
2537         ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2538                                WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2539                                true, frame_buf);
2540         mutex_lock(&local->mtx);
2541         sdata->vif.csa_active = false;
2542         ifmgd->csa_waiting_bcn = false;
2543         if (sdata->csa_block_tx) {
2544                 ieee80211_wake_vif_queues(local, sdata,
2545                                           IEEE80211_QUEUE_STOP_REASON_CSA);
2546                 sdata->csa_block_tx = false;
2547         }
2548         mutex_unlock(&local->mtx);
2549
2550         ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
2551                                     WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2552
2553         sdata_unlock(sdata);
2554 }
2555
2556 static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
2557 {
2558         struct ieee80211_sub_if_data *sdata =
2559                 container_of(work, struct ieee80211_sub_if_data,
2560                              u.mgd.beacon_connection_loss_work);
2561         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2562
2563         if (ifmgd->associated)
2564                 ifmgd->beacon_loss_count++;
2565
2566         if (ifmgd->connection_loss) {
2567                 sdata_info(sdata, "Connection to AP %pM lost\n",
2568                            ifmgd->bssid);
2569                 __ieee80211_disconnect(sdata);
2570         } else {
2571                 ieee80211_mgd_probe_ap(sdata, true);
2572         }
2573 }
2574
2575 static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2576 {
2577         struct ieee80211_sub_if_data *sdata =
2578                 container_of(work, struct ieee80211_sub_if_data,
2579                              u.mgd.csa_connection_drop_work);
2580
2581         __ieee80211_disconnect(sdata);
2582 }
2583
2584 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2585 {
2586         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2587         struct ieee80211_hw *hw = &sdata->local->hw;
2588
2589         trace_api_beacon_loss(sdata);
2590
2591         sdata->u.mgd.connection_loss = false;
2592         ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2593 }
2594 EXPORT_SYMBOL(ieee80211_beacon_loss);
2595
2596 void ieee80211_connection_loss(struct ieee80211_vif *vif)
2597 {
2598         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2599         struct ieee80211_hw *hw = &sdata->local->hw;
2600
2601         trace_api_connection_loss(sdata);
2602
2603         sdata->u.mgd.connection_loss = true;
2604         ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2605 }
2606 EXPORT_SYMBOL(ieee80211_connection_loss);
2607
2608
2609 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2610                                         bool assoc)
2611 {
2612         struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2613
2614         sdata_assert_lock(sdata);
2615
2616         if (!assoc) {
2617                 /*
2618                  * we are not authenticated yet, the only timer that could be
2619                  * running is the timeout for the authentication response which
2620                  * which is not relevant anymore.
2621                  */
2622                 del_timer_sync(&sdata->u.mgd.timer);
2623                 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2624
2625                 eth_zero_addr(sdata->u.mgd.bssid);
2626                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2627                 sdata->u.mgd.flags = 0;
2628                 mutex_lock(&sdata->local->mtx);
2629                 ieee80211_vif_release_channel(sdata);
2630                 mutex_unlock(&sdata->local->mtx);
2631         }
2632
2633         cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
2634         kfree(auth_data);
2635         sdata->u.mgd.auth_data = NULL;
2636 }
2637
2638 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2639                                          bool assoc, bool abandon)
2640 {
2641         struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2642
2643         sdata_assert_lock(sdata);
2644
2645         if (!assoc) {
2646                 /*
2647                  * we are not associated yet, the only timer that could be
2648                  * running is the timeout for the association response which
2649                  * which is not relevant anymore.
2650                  */
2651                 del_timer_sync(&sdata->u.mgd.timer);
2652                 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2653
2654                 eth_zero_addr(sdata->u.mgd.bssid);
2655                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2656                 sdata->u.mgd.flags = 0;
2657                 sdata->vif.mu_mimo_owner = false;
2658
2659                 mutex_lock(&sdata->local->mtx);
2660                 ieee80211_vif_release_channel(sdata);
2661                 mutex_unlock(&sdata->local->mtx);
2662
2663                 if (abandon)
2664                         cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
2665         }
2666
2667         kfree(assoc_data);
2668         sdata->u.mgd.assoc_data = NULL;
2669 }
2670
2671 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2672                                      struct ieee80211_mgmt *mgmt, size_t len)
2673 {
2674         struct ieee80211_local *local = sdata->local;
2675         struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2676         u8 *pos;
2677         struct ieee802_11_elems elems;
2678         u32 tx_flags = 0;
2679
2680         pos = mgmt->u.auth.variable;
2681         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2682         if (!elems.challenge)
2683                 return;
2684         auth_data->expected_transaction = 4;
2685         drv_mgd_prepare_tx(sdata->local, sdata, 0);
2686         if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
2687                 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2688                            IEEE80211_TX_INTFL_MLME_CONN_TX;
2689         ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
2690                             elems.challenge - 2, elems.challenge_len + 2,
2691                             auth_data->bss->bssid, auth_data->bss->bssid,
2692                             auth_data->key, auth_data->key_len,
2693                             auth_data->key_idx, tx_flags);
2694 }
2695
2696 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2697                                    struct ieee80211_mgmt *mgmt, size_t len)
2698 {
2699         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2700         u8 bssid[ETH_ALEN];
2701         u16 auth_alg, auth_transaction, status_code;
2702         struct sta_info *sta;
2703         struct ieee80211_event event = {
2704                 .type = MLME_EVENT,
2705                 .u.mlme.data = AUTH_EVENT,
2706         };
2707
2708         sdata_assert_lock(sdata);
2709
2710         if (len < 24 + 6)
2711                 return;
2712
2713         if (!ifmgd->auth_data || ifmgd->auth_data->done)
2714                 return;
2715
2716         memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2717
2718         if (!ether_addr_equal(bssid, mgmt->bssid))
2719                 return;
2720
2721         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2722         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2723         status_code = le16_to_cpu(mgmt->u.auth.status_code);
2724
2725         if (auth_alg != ifmgd->auth_data->algorithm ||
2726             auth_transaction != ifmgd->auth_data->expected_transaction) {
2727                 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2728                            mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2729                            auth_transaction,
2730                            ifmgd->auth_data->expected_transaction);
2731                 return;
2732         }
2733
2734         if (status_code != WLAN_STATUS_SUCCESS) {
2735                 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2736                            mgmt->sa, status_code);
2737                 ieee80211_destroy_auth_data(sdata, false);
2738                 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2739                 event.u.mlme.status = MLME_DENIED;
2740                 event.u.mlme.reason = status_code;
2741                 drv_event_callback(sdata->local, sdata, &event);
2742                 return;
2743         }
2744
2745         switch (ifmgd->auth_data->algorithm) {
2746         case WLAN_AUTH_OPEN:
2747         case WLAN_AUTH_LEAP:
2748         case WLAN_AUTH_FT:
2749         case WLAN_AUTH_SAE:
2750         case WLAN_AUTH_FILS_SK:
2751         case WLAN_AUTH_FILS_SK_PFS:
2752         case WLAN_AUTH_FILS_PK:
2753                 break;
2754         case WLAN_AUTH_SHARED_KEY:
2755                 if (ifmgd->auth_data->expected_transaction != 4) {
2756                         ieee80211_auth_challenge(sdata, mgmt, len);
2757                         /* need another frame */
2758                         return;
2759                 }
2760                 break;
2761         default:
2762                 WARN_ONCE(1, "invalid auth alg %d",
2763                           ifmgd->auth_data->algorithm);
2764                 return;
2765         }
2766
2767         event.u.mlme.status = MLME_SUCCESS;
2768         drv_event_callback(sdata->local, sdata, &event);
2769         sdata_info(sdata, "authenticated\n");
2770         ifmgd->auth_data->done = true;
2771         ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2772         ifmgd->auth_data->timeout_started = true;
2773         run_again(sdata, ifmgd->auth_data->timeout);
2774
2775         if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2776             ifmgd->auth_data->expected_transaction != 2) {
2777                 /*
2778                  * Report auth frame to user space for processing since another
2779                  * round of Authentication frames is still needed.
2780                  */
2781                 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2782                 return;
2783         }
2784
2785         /* move station state to auth */
2786         mutex_lock(&sdata->local->sta_mtx);
2787         sta = sta_info_get(sdata, bssid);
2788         if (!sta) {
2789                 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2790                 goto out_err;
2791         }
2792         if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2793                 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2794                 goto out_err;
2795         }
2796         mutex_unlock(&sdata->local->sta_mtx);
2797
2798         cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2799         return;
2800  out_err:
2801         mutex_unlock(&sdata->local->sta_mtx);
2802         /* ignore frame -- wait for timeout */
2803 }
2804
2805 #define case_WLAN(type) \
2806         case WLAN_REASON_##type: return #type
2807
2808 static const char *ieee80211_get_reason_code_string(u16 reason_code)
2809 {
2810         switch (reason_code) {
2811         case_WLAN(UNSPECIFIED);
2812         case_WLAN(PREV_AUTH_NOT_VALID);
2813         case_WLAN(DEAUTH_LEAVING);
2814         case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
2815         case_WLAN(DISASSOC_AP_BUSY);
2816         case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
2817         case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
2818         case_WLAN(DISASSOC_STA_HAS_LEFT);
2819         case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
2820         case_WLAN(DISASSOC_BAD_POWER);
2821         case_WLAN(DISASSOC_BAD_SUPP_CHAN);
2822         case_WLAN(INVALID_IE);
2823         case_WLAN(MIC_FAILURE);
2824         case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
2825         case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
2826         case_WLAN(IE_DIFFERENT);
2827         case_WLAN(INVALID_GROUP_CIPHER);
2828         case_WLAN(INVALID_PAIRWISE_CIPHER);
2829         case_WLAN(INVALID_AKMP);
2830         case_WLAN(UNSUPP_RSN_VERSION);
2831         case_WLAN(INVALID_RSN_IE_CAP);
2832         case_WLAN(IEEE8021X_FAILED);
2833         case_WLAN(CIPHER_SUITE_REJECTED);
2834         case_WLAN(DISASSOC_UNSPECIFIED_QOS);
2835         case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
2836         case_WLAN(DISASSOC_LOW_ACK);
2837         case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
2838         case_WLAN(QSTA_LEAVE_QBSS);
2839         case_WLAN(QSTA_NOT_USE);
2840         case_WLAN(QSTA_REQUIRE_SETUP);
2841         case_WLAN(QSTA_TIMEOUT);
2842         case_WLAN(QSTA_CIPHER_NOT_SUPP);
2843         case_WLAN(MESH_PEER_CANCELED);
2844         case_WLAN(MESH_MAX_PEERS);
2845         case_WLAN(MESH_CONFIG);
2846         case_WLAN(MESH_CLOSE);
2847         case_WLAN(MESH_MAX_RETRIES);
2848         case_WLAN(MESH_CONFIRM_TIMEOUT);
2849         case_WLAN(MESH_INVALID_GTK);
2850         case_WLAN(MESH_INCONSISTENT_PARAM);
2851         case_WLAN(MESH_INVALID_SECURITY);
2852         case_WLAN(MESH_PATH_ERROR);
2853         case_WLAN(MESH_PATH_NOFORWARD);
2854         case_WLAN(MESH_PATH_DEST_UNREACHABLE);
2855         case_WLAN(MAC_EXISTS_IN_MBSS);
2856         case_WLAN(MESH_CHAN_REGULATORY);
2857         case_WLAN(MESH_CHAN);
2858         default: return "<unknown>";
2859         }
2860 }
2861
2862 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2863                                      struct ieee80211_mgmt *mgmt, size_t len)
2864 {
2865         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2866         u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2867
2868         sdata_assert_lock(sdata);
2869
2870         if (len < 24 + 2)
2871                 return;
2872
2873         if (ifmgd->associated &&
2874             ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
2875                 const u8 *bssid = ifmgd->associated->bssid;
2876
2877                 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
2878                            bssid, reason_code,
2879                            ieee80211_get_reason_code_string(reason_code));
2880
2881                 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2882
2883                 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
2884                                             reason_code);
2885                 return;
2886         }
2887
2888         if (ifmgd->assoc_data &&
2889             ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
2890                 const u8 *bssid = ifmgd->assoc_data->bss->bssid;
2891
2892                 sdata_info(sdata,
2893                            "deauthenticated from %pM while associating (Reason: %u=%s)\n",
2894                            bssid, reason_code,
2895                            ieee80211_get_reason_code_string(reason_code));
2896
2897                 ieee80211_destroy_assoc_data(sdata, false, true);
2898
2899                 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2900                 return;
2901         }
2902 }
2903
2904
2905 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2906                                        struct ieee80211_mgmt *mgmt, size_t len)
2907 {
2908         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2909         u16 reason_code;
2910
2911         sdata_assert_lock(sdata);
2912
2913         if (len < 24 + 2)
2914                 return;
2915
2916         if (!ifmgd->associated ||
2917             !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2918                 return;
2919
2920         reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2921
2922         sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
2923                    mgmt->sa, reason_code,
2924                    ieee80211_get_reason_code_string(reason_code));
2925
2926         ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2927
2928         ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
2929 }
2930
2931 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2932                                 u8 *supp_rates, unsigned int supp_rates_len,
2933                                 u32 *rates, u32 *basic_rates,
2934                                 bool *have_higher_than_11mbit,
2935                                 int *min_rate, int *min_rate_index,
2936                                 int shift)
2937 {
2938         int i, j;
2939
2940         for (i = 0; i < supp_rates_len; i++) {
2941                 int rate = supp_rates[i] & 0x7f;
2942                 bool is_basic = !!(supp_rates[i] & 0x80);
2943
2944                 if ((rate * 5 * (1 << shift)) > 110)
2945                         *have_higher_than_11mbit = true;
2946
2947                 /*
2948                  * Skip HT and VHT BSS membership selectors since they're not
2949                  * rates.
2950                  *
2951                  * Note: Even though the membership selector and the basic
2952                  *       rate flag share the same bit, they are not exactly
2953                  *       the same.
2954                  */
2955                 if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
2956                     supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY))
2957                         continue;
2958
2959                 for (j = 0; j < sband->n_bitrates; j++) {
2960                         struct ieee80211_rate *br;
2961                         int brate;
2962
2963                         br = &sband->bitrates[j];
2964
2965                         brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
2966                         if (brate == rate) {
2967                                 *rates |= BIT(j);
2968                                 if (is_basic)
2969                                         *basic_rates |= BIT(j);
2970                                 if ((rate * 5) < *min_rate) {
2971                                         *min_rate = rate * 5;
2972                                         *min_rate_index = j;
2973                                 }
2974                                 break;
2975                         }
2976                 }
2977         }
2978 }
2979
2980 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2981                                     struct cfg80211_bss *cbss,
2982                                     struct ieee80211_mgmt *mgmt, size_t len)
2983 {
2984         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2985         struct ieee80211_local *local = sdata->local;
2986         struct ieee80211_supported_band *sband;
2987         struct sta_info *sta;
2988         u8 *pos;
2989         u16 capab_info, aid;
2990         struct ieee802_11_elems elems;
2991         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2992         const struct cfg80211_bss_ies *bss_ies = NULL;
2993         struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2994         u32 changed = 0;
2995         int err;
2996         bool ret;
2997
2998         /* AssocResp and ReassocResp have identical structure */
2999
3000         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3001         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3002
3003         /*
3004          * The 5 MSB of the AID field are reserved
3005          * (802.11-2016 9.4.1.8 AID field)
3006          */
3007         aid &= 0x7ff;
3008
3009         ifmgd->broken_ap = false;
3010
3011         if (aid == 0 || aid > IEEE80211_MAX_AID) {
3012                 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3013                            aid);
3014                 aid = 0;
3015                 ifmgd->broken_ap = true;
3016         }
3017
3018         pos = mgmt->u.assoc_resp.variable;
3019         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
3020
3021         if (!elems.supp_rates) {
3022                 sdata_info(sdata, "no SuppRates element in AssocResp\n");
3023                 return false;
3024         }
3025
3026         ifmgd->aid = aid;
3027         ifmgd->tdls_chan_switch_prohibited =
3028                 elems.ext_capab && elems.ext_capab_len >= 5 &&
3029                 (elems.ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
3030
3031         /*
3032          * Some APs are erroneously not including some information in their
3033          * (re)association response frames. Try to recover by using the data
3034          * from the beacon or probe response. This seems to afflict mobile
3035          * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3036          * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3037          */
3038         if ((assoc_data->wmm && !elems.wmm_param) ||
3039             (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3040              (!elems.ht_cap_elem || !elems.ht_operation)) ||
3041             (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3042              (!elems.vht_cap_elem || !elems.vht_operation))) {
3043                 const struct cfg80211_bss_ies *ies;
3044                 struct ieee802_11_elems bss_elems;
3045
3046                 rcu_read_lock();
3047                 ies = rcu_dereference(cbss->ies);
3048                 if (ies)
3049                         bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3050                                           GFP_ATOMIC);
3051                 rcu_read_unlock();
3052                 if (!bss_ies)
3053                         return false;
3054
3055                 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
3056                                        false, &bss_elems);
3057                 if (assoc_data->wmm &&
3058                     !elems.wmm_param && bss_elems.wmm_param) {
3059                         elems.wmm_param = bss_elems.wmm_param;
3060                         sdata_info(sdata,
3061                                    "AP bug: WMM param missing from AssocResp\n");
3062                 }
3063
3064                 /*
3065                  * Also check if we requested HT/VHT, otherwise the AP doesn't
3066                  * have to include the IEs in the (re)association response.
3067                  */
3068                 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
3069                     !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3070                         elems.ht_cap_elem = bss_elems.ht_cap_elem;
3071                         sdata_info(sdata,
3072                                    "AP bug: HT capability missing from AssocResp\n");
3073                 }
3074                 if (!elems.ht_operation && bss_elems.ht_operation &&
3075                     !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3076                         elems.ht_operation = bss_elems.ht_operation;
3077                         sdata_info(sdata,
3078                                    "AP bug: HT operation missing from AssocResp\n");
3079                 }
3080                 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
3081                     !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3082                         elems.vht_cap_elem = bss_elems.vht_cap_elem;
3083                         sdata_info(sdata,
3084                                    "AP bug: VHT capa missing from AssocResp\n");
3085                 }
3086                 if (!elems.vht_operation && bss_elems.vht_operation &&
3087                     !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3088                         elems.vht_operation = bss_elems.vht_operation;
3089                         sdata_info(sdata,
3090                                    "AP bug: VHT operation missing from AssocResp\n");
3091                 }
3092         }
3093
3094         /*
3095          * We previously checked these in the beacon/probe response, so
3096          * they should be present here. This is just a safety net.
3097          */
3098         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3099             (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
3100                 sdata_info(sdata,
3101                            "HT AP is missing WMM params or HT capability/operation\n");
3102                 ret = false;
3103                 goto out;
3104         }
3105
3106         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3107             (!elems.vht_cap_elem || !elems.vht_operation)) {
3108                 sdata_info(sdata,
3109                            "VHT AP is missing VHT capability/operation\n");
3110                 ret = false;
3111                 goto out;
3112         }
3113
3114         mutex_lock(&sdata->local->sta_mtx);
3115         /*
3116          * station info was already allocated and inserted before
3117          * the association and should be available to us
3118          */
3119         sta = sta_info_get(sdata, cbss->bssid);
3120         if (WARN_ON(!sta)) {
3121                 mutex_unlock(&sdata->local->sta_mtx);
3122                 ret = false;
3123                 goto out;
3124         }
3125
3126         sband = ieee80211_get_sband(sdata);
3127         if (!sband) {
3128                 mutex_unlock(&sdata->local->sta_mtx);
3129                 ret = false;
3130                 goto out;
3131         }
3132
3133         /*
3134          * If AP doesn't support HT, or it doesn't have HE mandatory IEs, mark
3135          * HE as disabled. If on the 5GHz band, make sure it supports VHT.
3136          */
3137         if (ifmgd->flags & IEEE80211_STA_DISABLE_HT ||
3138             (sband->band == NL80211_BAND_5GHZ &&
3139              ifmgd->flags & IEEE80211_STA_DISABLE_VHT) ||
3140             (!elems.he_cap && !elems.he_operation))
3141                 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
3142
3143         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3144             (!elems.he_cap || !elems.he_operation)) {
3145                 mutex_unlock(&sdata->local->sta_mtx);
3146                 sdata_info(sdata,
3147                            "HE AP is missing HE capability/operation\n");
3148                 ret = false;
3149                 goto out;
3150         }
3151
3152         /* Set up internal HT/VHT capabilities */
3153         if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
3154                 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
3155                                                   elems.ht_cap_elem, sta);
3156
3157         if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
3158                 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
3159                                                     elems.vht_cap_elem, sta);
3160
3161         if (elems.he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3162             elems.he_cap) {
3163                 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
3164                                                   elems.he_cap,
3165                                                   elems.he_cap_len,
3166                                                   sta);
3167
3168                 bss_conf->he_support = sta->sta.he_cap.has_he;
3169         } else {
3170                 bss_conf->he_support = false;
3171         }
3172
3173         if (bss_conf->he_support) {
3174                 u32 he_oper_params =
3175                         le32_to_cpu(elems.he_operation->he_oper_params);
3176
3177                 bss_conf->bss_color = he_oper_params &
3178                                       IEEE80211_HE_OPERATION_BSS_COLOR_MASK;
3179                 bss_conf->htc_trig_based_pkt_ext =
3180                         (he_oper_params &
3181                          IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK) <<
3182                         IEEE80211_HE_OPERATION_DFLT_PE_DURATION_OFFSET;
3183                 bss_conf->frame_time_rts_th =
3184                         (he_oper_params &
3185                          IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK) <<
3186                         IEEE80211_HE_OPERATION_RTS_THRESHOLD_OFFSET;
3187
3188                 bss_conf->multi_sta_back_32bit =
3189                         sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3190                         IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP;
3191
3192                 bss_conf->ack_enabled =
3193                         sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3194                         IEEE80211_HE_MAC_CAP2_ACK_EN;
3195
3196                 bss_conf->uora_exists = !!elems.uora_element;
3197                 if (elems.uora_element)
3198                         bss_conf->uora_ocw_range = elems.uora_element[0];
3199
3200                 /* TODO: OPEN: what happens if BSS color disable is set? */
3201         }
3202
3203         /*
3204          * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3205          * in their association response, so ignore that data for our own
3206          * configuration. If it changed since the last beacon, we'll get the
3207          * next beacon and update then.
3208          */
3209
3210         /*
3211          * If an operating mode notification IE is present, override the
3212          * NSS calculation (that would be done in rate_control_rate_init())
3213          * and use the # of streams from that element.
3214          */
3215         if (elems.opmode_notif &&
3216             !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
3217                 u8 nss;
3218
3219                 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
3220                 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3221                 nss += 1;
3222                 sta->sta.rx_nss = nss;
3223         }
3224
3225         rate_control_rate_init(sta);
3226
3227         if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
3228                 set_sta_flag(sta, WLAN_STA_MFP);
3229                 sta->sta.mfp = true;
3230         } else {
3231                 sta->sta.mfp = false;
3232         }
3233
3234         sta->sta.wme = elems.wmm_param && local->hw.queues >= IEEE80211_NUM_ACS;
3235
3236         err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
3237         if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3238                 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3239         if (err) {
3240                 sdata_info(sdata,
3241                            "failed to move station %pM to desired state\n",
3242                            sta->sta.addr);
3243                 WARN_ON(__sta_info_destroy(sta));
3244                 mutex_unlock(&sdata->local->sta_mtx);
3245                 ret = false;
3246                 goto out;
3247         }
3248
3249         mutex_unlock(&sdata->local->sta_mtx);
3250
3251         /*
3252          * Always handle WMM once after association regardless
3253          * of the first value the AP uses. Setting -1 here has
3254          * that effect because the AP values is an unsigned
3255          * 4-bit value.
3256          */
3257         ifmgd->wmm_last_param_set = -1;
3258
3259         if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
3260                 ieee80211_set_wmm_default(sdata, false, false);
3261         } else if (!ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3262                                              elems.wmm_param_len,
3263                                              elems.mu_edca_param_set)) {
3264                 /* still enable QoS since we might have HT/VHT */
3265                 ieee80211_set_wmm_default(sdata, false, true);
3266                 /* set the disable-WMM flag in this case to disable
3267                  * tracking WMM parameter changes in the beacon if
3268                  * the parameters weren't actually valid. Doing so
3269                  * avoids changing parameters very strangely when
3270                  * the AP is going back and forth between valid and
3271                  * invalid parameters.
3272                  */
3273                 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3274         }
3275         changed |= BSS_CHANGED_QOS;
3276
3277         if (elems.max_idle_period_ie) {
3278                 bss_conf->max_idle_period =
3279                         le16_to_cpu(elems.max_idle_period_ie->max_idle_period);
3280                 bss_conf->protected_keep_alive =
3281                         !!(elems.max_idle_period_ie->idle_options &
3282                            WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3283                 changed |= BSS_CHANGED_KEEP_ALIVE;
3284         } else {
3285                 bss_conf->max_idle_period = 0;
3286                 bss_conf->protected_keep_alive = false;