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