mac80211: fix sparse warning caused by __ieee80211_channel_switch()
[pandora-kernel.git] / net / mac80211 / cfg.c
1 /*
2  * mac80211 configuration hooks for cfg80211
3  *
4  * Copyright 2006-2010  Johannes Berg <johannes@sipsolutions.net>
5  *
6  * This file is GPLv2 as found in COPYING.
7  */
8
9 #include <linux/ieee80211.h>
10 #include <linux/nl80211.h>
11 #include <linux/rtnetlink.h>
12 #include <linux/slab.h>
13 #include <net/net_namespace.h>
14 #include <linux/rcupdate.h>
15 #include <linux/if_ether.h>
16 #include <net/cfg80211.h>
17 #include "ieee80211_i.h"
18 #include "driver-ops.h"
19 #include "cfg.h"
20 #include "rate.h"
21 #include "mesh.h"
22
23 static struct wireless_dev *ieee80211_add_iface(struct wiphy *wiphy,
24                                                 const char *name,
25                                                 enum nl80211_iftype type,
26                                                 u32 *flags,
27                                                 struct vif_params *params)
28 {
29         struct ieee80211_local *local = wiphy_priv(wiphy);
30         struct wireless_dev *wdev;
31         struct ieee80211_sub_if_data *sdata;
32         int err;
33
34         err = ieee80211_if_add(local, name, &wdev, type, params);
35         if (err)
36                 return ERR_PTR(err);
37
38         if (type == NL80211_IFTYPE_MONITOR && flags) {
39                 sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
40                 sdata->u.mntr_flags = *flags;
41         }
42
43         return wdev;
44 }
45
46 static int ieee80211_del_iface(struct wiphy *wiphy, struct wireless_dev *wdev)
47 {
48         ieee80211_if_remove(IEEE80211_WDEV_TO_SUB_IF(wdev));
49
50         return 0;
51 }
52
53 static int ieee80211_change_iface(struct wiphy *wiphy,
54                                   struct net_device *dev,
55                                   enum nl80211_iftype type, u32 *flags,
56                                   struct vif_params *params)
57 {
58         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
59         int ret;
60
61         ret = ieee80211_if_change_type(sdata, type);
62         if (ret)
63                 return ret;
64
65         if (type == NL80211_IFTYPE_AP_VLAN &&
66             params && params->use_4addr == 0)
67                 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
68         else if (type == NL80211_IFTYPE_STATION &&
69                  params && params->use_4addr >= 0)
70                 sdata->u.mgd.use_4addr = params->use_4addr;
71
72         if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags) {
73                 struct ieee80211_local *local = sdata->local;
74
75                 if (ieee80211_sdata_running(sdata)) {
76                         u32 mask = MONITOR_FLAG_COOK_FRAMES |
77                                    MONITOR_FLAG_ACTIVE;
78
79                         /*
80                          * Prohibit MONITOR_FLAG_COOK_FRAMES and
81                          * MONITOR_FLAG_ACTIVE to be changed while the
82                          * interface is up.
83                          * Else we would need to add a lot of cruft
84                          * to update everything:
85                          *      cooked_mntrs, monitor and all fif_* counters
86                          *      reconfigure hardware
87                          */
88                         if ((*flags & mask) != (sdata->u.mntr_flags & mask))
89                                 return -EBUSY;
90
91                         ieee80211_adjust_monitor_flags(sdata, -1);
92                         sdata->u.mntr_flags = *flags;
93                         ieee80211_adjust_monitor_flags(sdata, 1);
94
95                         ieee80211_configure_filter(local);
96                 } else {
97                         /*
98                          * Because the interface is down, ieee80211_do_stop
99                          * and ieee80211_do_open take care of "everything"
100                          * mentioned in the comment above.
101                          */
102                         sdata->u.mntr_flags = *flags;
103                 }
104         }
105
106         return 0;
107 }
108
109 static int ieee80211_start_p2p_device(struct wiphy *wiphy,
110                                       struct wireless_dev *wdev)
111 {
112         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
113         int ret;
114
115         mutex_lock(&sdata->local->chanctx_mtx);
116         ret = ieee80211_check_combinations(sdata, NULL, 0, 0);
117         mutex_unlock(&sdata->local->chanctx_mtx);
118         if (ret < 0)
119                 return ret;
120
121         return ieee80211_do_open(wdev, true);
122 }
123
124 static void ieee80211_stop_p2p_device(struct wiphy *wiphy,
125                                       struct wireless_dev *wdev)
126 {
127         ieee80211_sdata_stop(IEEE80211_WDEV_TO_SUB_IF(wdev));
128 }
129
130 static int ieee80211_set_noack_map(struct wiphy *wiphy,
131                                   struct net_device *dev,
132                                   u16 noack_map)
133 {
134         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
135
136         sdata->noack_map = noack_map;
137         return 0;
138 }
139
140 static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
141                              u8 key_idx, bool pairwise, const u8 *mac_addr,
142                              struct key_params *params)
143 {
144         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
145         struct ieee80211_local *local = sdata->local;
146         struct sta_info *sta = NULL;
147         const struct ieee80211_cipher_scheme *cs = NULL;
148         struct ieee80211_key *key;
149         int err;
150
151         if (!ieee80211_sdata_running(sdata))
152                 return -ENETDOWN;
153
154         /* reject WEP and TKIP keys if WEP failed to initialize */
155         switch (params->cipher) {
156         case WLAN_CIPHER_SUITE_WEP40:
157         case WLAN_CIPHER_SUITE_TKIP:
158         case WLAN_CIPHER_SUITE_WEP104:
159                 if (IS_ERR(local->wep_tx_tfm))
160                         return -EINVAL;
161                 break;
162         case WLAN_CIPHER_SUITE_CCMP:
163         case WLAN_CIPHER_SUITE_AES_CMAC:
164         case WLAN_CIPHER_SUITE_GCMP:
165                 break;
166         default:
167                 cs = ieee80211_cs_get(local, params->cipher, sdata->vif.type);
168                 break;
169         }
170
171         key = ieee80211_key_alloc(params->cipher, key_idx, params->key_len,
172                                   params->key, params->seq_len, params->seq,
173                                   cs);
174         if (IS_ERR(key))
175                 return PTR_ERR(key);
176
177         if (pairwise)
178                 key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
179
180         mutex_lock(&local->sta_mtx);
181
182         if (mac_addr) {
183                 if (ieee80211_vif_is_mesh(&sdata->vif))
184                         sta = sta_info_get(sdata, mac_addr);
185                 else
186                         sta = sta_info_get_bss(sdata, mac_addr);
187                 /*
188                  * The ASSOC test makes sure the driver is ready to
189                  * receive the key. When wpa_supplicant has roamed
190                  * using FT, it attempts to set the key before
191                  * association has completed, this rejects that attempt
192                  * so it will set the key again after assocation.
193                  *
194                  * TODO: accept the key if we have a station entry and
195                  *       add it to the device after the station.
196                  */
197                 if (!sta || !test_sta_flag(sta, WLAN_STA_ASSOC)) {
198                         ieee80211_key_free_unused(key);
199                         err = -ENOENT;
200                         goto out_unlock;
201                 }
202         }
203
204         switch (sdata->vif.type) {
205         case NL80211_IFTYPE_STATION:
206                 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
207                         key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
208                 break;
209         case NL80211_IFTYPE_AP:
210         case NL80211_IFTYPE_AP_VLAN:
211                 /* Keys without a station are used for TX only */
212                 if (key->sta && test_sta_flag(key->sta, WLAN_STA_MFP))
213                         key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
214                 break;
215         case NL80211_IFTYPE_ADHOC:
216                 /* no MFP (yet) */
217                 break;
218         case NL80211_IFTYPE_MESH_POINT:
219 #ifdef CONFIG_MAC80211_MESH
220                 if (sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE)
221                         key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
222                 break;
223 #endif
224         case NL80211_IFTYPE_WDS:
225         case NL80211_IFTYPE_MONITOR:
226         case NL80211_IFTYPE_P2P_DEVICE:
227         case NL80211_IFTYPE_UNSPECIFIED:
228         case NUM_NL80211_IFTYPES:
229         case NL80211_IFTYPE_P2P_CLIENT:
230         case NL80211_IFTYPE_P2P_GO:
231                 /* shouldn't happen */
232                 WARN_ON_ONCE(1);
233                 break;
234         }
235
236         if (sta)
237                 sta->cipher_scheme = cs;
238
239         err = ieee80211_key_link(key, sdata, sta);
240
241  out_unlock:
242         mutex_unlock(&local->sta_mtx);
243
244         return err;
245 }
246
247 static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
248                              u8 key_idx, bool pairwise, const u8 *mac_addr)
249 {
250         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
251         struct ieee80211_local *local = sdata->local;
252         struct sta_info *sta;
253         struct ieee80211_key *key = NULL;
254         int ret;
255
256         mutex_lock(&local->sta_mtx);
257         mutex_lock(&local->key_mtx);
258
259         if (mac_addr) {
260                 ret = -ENOENT;
261
262                 sta = sta_info_get_bss(sdata, mac_addr);
263                 if (!sta)
264                         goto out_unlock;
265
266                 if (pairwise)
267                         key = key_mtx_dereference(local, sta->ptk[key_idx]);
268                 else
269                         key = key_mtx_dereference(local, sta->gtk[key_idx]);
270         } else
271                 key = key_mtx_dereference(local, sdata->keys[key_idx]);
272
273         if (!key) {
274                 ret = -ENOENT;
275                 goto out_unlock;
276         }
277
278         ieee80211_key_free(key, true);
279
280         ret = 0;
281  out_unlock:
282         mutex_unlock(&local->key_mtx);
283         mutex_unlock(&local->sta_mtx);
284
285         return ret;
286 }
287
288 static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
289                              u8 key_idx, bool pairwise, const u8 *mac_addr,
290                              void *cookie,
291                              void (*callback)(void *cookie,
292                                               struct key_params *params))
293 {
294         struct ieee80211_sub_if_data *sdata;
295         struct sta_info *sta = NULL;
296         u8 seq[6] = {0};
297         struct key_params params;
298         struct ieee80211_key *key = NULL;
299         u64 pn64;
300         u32 iv32;
301         u16 iv16;
302         int err = -ENOENT;
303
304         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
305
306         rcu_read_lock();
307
308         if (mac_addr) {
309                 sta = sta_info_get_bss(sdata, mac_addr);
310                 if (!sta)
311                         goto out;
312
313                 if (pairwise && key_idx < NUM_DEFAULT_KEYS)
314                         key = rcu_dereference(sta->ptk[key_idx]);
315                 else if (!pairwise &&
316                          key_idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
317                         key = rcu_dereference(sta->gtk[key_idx]);
318         } else
319                 key = rcu_dereference(sdata->keys[key_idx]);
320
321         if (!key)
322                 goto out;
323
324         memset(&params, 0, sizeof(params));
325
326         params.cipher = key->conf.cipher;
327
328         switch (key->conf.cipher) {
329         case WLAN_CIPHER_SUITE_TKIP:
330                 iv32 = key->u.tkip.tx.iv32;
331                 iv16 = key->u.tkip.tx.iv16;
332
333                 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
334                         drv_get_tkip_seq(sdata->local,
335                                          key->conf.hw_key_idx,
336                                          &iv32, &iv16);
337
338                 seq[0] = iv16 & 0xff;
339                 seq[1] = (iv16 >> 8) & 0xff;
340                 seq[2] = iv32 & 0xff;
341                 seq[3] = (iv32 >> 8) & 0xff;
342                 seq[4] = (iv32 >> 16) & 0xff;
343                 seq[5] = (iv32 >> 24) & 0xff;
344                 params.seq = seq;
345                 params.seq_len = 6;
346                 break;
347         case WLAN_CIPHER_SUITE_CCMP:
348                 pn64 = atomic64_read(&key->u.ccmp.tx_pn);
349                 seq[0] = pn64;
350                 seq[1] = pn64 >> 8;
351                 seq[2] = pn64 >> 16;
352                 seq[3] = pn64 >> 24;
353                 seq[4] = pn64 >> 32;
354                 seq[5] = pn64 >> 40;
355                 params.seq = seq;
356                 params.seq_len = 6;
357                 break;
358         case WLAN_CIPHER_SUITE_AES_CMAC:
359                 pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
360                 seq[0] = pn64;
361                 seq[1] = pn64 >> 8;
362                 seq[2] = pn64 >> 16;
363                 seq[3] = pn64 >> 24;
364                 seq[4] = pn64 >> 32;
365                 seq[5] = pn64 >> 40;
366                 params.seq = seq;
367                 params.seq_len = 6;
368                 break;
369         }
370
371         params.key = key->conf.key;
372         params.key_len = key->conf.keylen;
373
374         callback(cookie, &params);
375         err = 0;
376
377  out:
378         rcu_read_unlock();
379         return err;
380 }
381
382 static int ieee80211_config_default_key(struct wiphy *wiphy,
383                                         struct net_device *dev,
384                                         u8 key_idx, bool uni,
385                                         bool multi)
386 {
387         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
388
389         ieee80211_set_default_key(sdata, key_idx, uni, multi);
390
391         return 0;
392 }
393
394 static int ieee80211_config_default_mgmt_key(struct wiphy *wiphy,
395                                              struct net_device *dev,
396                                              u8 key_idx)
397 {
398         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
399
400         ieee80211_set_default_mgmt_key(sdata, key_idx);
401
402         return 0;
403 }
404
405 void sta_set_rate_info_tx(struct sta_info *sta,
406                           const struct ieee80211_tx_rate *rate,
407                           struct rate_info *rinfo)
408 {
409         rinfo->flags = 0;
410         if (rate->flags & IEEE80211_TX_RC_MCS) {
411                 rinfo->flags |= RATE_INFO_FLAGS_MCS;
412                 rinfo->mcs = rate->idx;
413         } else if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
414                 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
415                 rinfo->mcs = ieee80211_rate_get_vht_mcs(rate);
416                 rinfo->nss = ieee80211_rate_get_vht_nss(rate);
417         } else {
418                 struct ieee80211_supported_band *sband;
419                 int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
420                 u16 brate;
421
422                 sband = sta->local->hw.wiphy->bands[
423                                 ieee80211_get_sdata_band(sta->sdata)];
424                 brate = sband->bitrates[rate->idx].bitrate;
425                 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
426         }
427         if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
428                 rinfo->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
429         if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
430                 rinfo->flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
431         if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
432                 rinfo->flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
433         if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
434                 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
435 }
436
437 void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
438 {
439         rinfo->flags = 0;
440
441         if (sta->last_rx_rate_flag & RX_FLAG_HT) {
442                 rinfo->flags |= RATE_INFO_FLAGS_MCS;
443                 rinfo->mcs = sta->last_rx_rate_idx;
444         } else if (sta->last_rx_rate_flag & RX_FLAG_VHT) {
445                 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
446                 rinfo->nss = sta->last_rx_rate_vht_nss;
447                 rinfo->mcs = sta->last_rx_rate_idx;
448         } else {
449                 struct ieee80211_supported_band *sband;
450                 int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
451                 u16 brate;
452
453                 sband = sta->local->hw.wiphy->bands[
454                                 ieee80211_get_sdata_band(sta->sdata)];
455                 brate = sband->bitrates[sta->last_rx_rate_idx].bitrate;
456                 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
457         }
458
459         if (sta->last_rx_rate_flag & RX_FLAG_40MHZ)
460                 rinfo->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
461         if (sta->last_rx_rate_flag & RX_FLAG_SHORT_GI)
462                 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
463         if (sta->last_rx_rate_vht_flag & RX_VHT_FLAG_80MHZ)
464                 rinfo->flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
465         if (sta->last_rx_rate_vht_flag & RX_VHT_FLAG_80P80MHZ)
466                 rinfo->flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
467         if (sta->last_rx_rate_vht_flag & RX_VHT_FLAG_160MHZ)
468                 rinfo->flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
469 }
470
471 static void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
472 {
473         struct ieee80211_sub_if_data *sdata = sta->sdata;
474         struct ieee80211_local *local = sdata->local;
475         struct timespec uptime;
476         u64 packets = 0;
477         int i, ac;
478
479         sinfo->generation = sdata->local->sta_generation;
480
481         sinfo->filled = STATION_INFO_INACTIVE_TIME |
482                         STATION_INFO_RX_BYTES64 |
483                         STATION_INFO_TX_BYTES64 |
484                         STATION_INFO_RX_PACKETS |
485                         STATION_INFO_TX_PACKETS |
486                         STATION_INFO_TX_RETRIES |
487                         STATION_INFO_TX_FAILED |
488                         STATION_INFO_TX_BITRATE |
489                         STATION_INFO_RX_BITRATE |
490                         STATION_INFO_RX_DROP_MISC |
491                         STATION_INFO_BSS_PARAM |
492                         STATION_INFO_CONNECTED_TIME |
493                         STATION_INFO_STA_FLAGS |
494                         STATION_INFO_BEACON_LOSS_COUNT;
495
496         do_posix_clock_monotonic_gettime(&uptime);
497         sinfo->connected_time = uptime.tv_sec - sta->last_connected;
498
499         sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
500         sinfo->tx_bytes = 0;
501         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
502                 sinfo->tx_bytes += sta->tx_bytes[ac];
503                 packets += sta->tx_packets[ac];
504         }
505         sinfo->tx_packets = packets;
506         sinfo->rx_bytes = sta->rx_bytes;
507         sinfo->rx_packets = sta->rx_packets;
508         sinfo->tx_retries = sta->tx_retry_count;
509         sinfo->tx_failed = sta->tx_retry_failed;
510         sinfo->rx_dropped_misc = sta->rx_dropped;
511         sinfo->beacon_loss_count = sta->beacon_loss_count;
512
513         if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
514             (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
515                 sinfo->filled |= STATION_INFO_SIGNAL | STATION_INFO_SIGNAL_AVG;
516                 if (!local->ops->get_rssi ||
517                     drv_get_rssi(local, sdata, &sta->sta, &sinfo->signal))
518                         sinfo->signal = (s8)sta->last_signal;
519                 sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
520         }
521         if (sta->chains) {
522                 sinfo->filled |= STATION_INFO_CHAIN_SIGNAL |
523                                  STATION_INFO_CHAIN_SIGNAL_AVG;
524
525                 sinfo->chains = sta->chains;
526                 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
527                         sinfo->chain_signal[i] = sta->chain_signal_last[i];
528                         sinfo->chain_signal_avg[i] =
529                                 (s8) -ewma_read(&sta->chain_signal_avg[i]);
530                 }
531         }
532
533         sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
534         sta_set_rate_info_rx(sta, &sinfo->rxrate);
535
536         if (ieee80211_vif_is_mesh(&sdata->vif)) {
537 #ifdef CONFIG_MAC80211_MESH
538                 sinfo->filled |= STATION_INFO_LLID |
539                                  STATION_INFO_PLID |
540                                  STATION_INFO_PLINK_STATE |
541                                  STATION_INFO_LOCAL_PM |
542                                  STATION_INFO_PEER_PM |
543                                  STATION_INFO_NONPEER_PM;
544
545                 sinfo->llid = sta->llid;
546                 sinfo->plid = sta->plid;
547                 sinfo->plink_state = sta->plink_state;
548                 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
549                         sinfo->filled |= STATION_INFO_T_OFFSET;
550                         sinfo->t_offset = sta->t_offset;
551                 }
552                 sinfo->local_pm = sta->local_pm;
553                 sinfo->peer_pm = sta->peer_pm;
554                 sinfo->nonpeer_pm = sta->nonpeer_pm;
555 #endif
556         }
557
558         sinfo->bss_param.flags = 0;
559         if (sdata->vif.bss_conf.use_cts_prot)
560                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
561         if (sdata->vif.bss_conf.use_short_preamble)
562                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
563         if (sdata->vif.bss_conf.use_short_slot)
564                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
565         sinfo->bss_param.dtim_period = sdata->local->hw.conf.ps_dtim_period;
566         sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
567
568         sinfo->sta_flags.set = 0;
569         sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
570                                 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
571                                 BIT(NL80211_STA_FLAG_WME) |
572                                 BIT(NL80211_STA_FLAG_MFP) |
573                                 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
574                                 BIT(NL80211_STA_FLAG_ASSOCIATED) |
575                                 BIT(NL80211_STA_FLAG_TDLS_PEER);
576         if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
577                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
578         if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
579                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
580         if (test_sta_flag(sta, WLAN_STA_WME))
581                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
582         if (test_sta_flag(sta, WLAN_STA_MFP))
583                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
584         if (test_sta_flag(sta, WLAN_STA_AUTH))
585                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
586         if (test_sta_flag(sta, WLAN_STA_ASSOC))
587                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
588         if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
589                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
590 }
591
592 static const char ieee80211_gstrings_sta_stats[][ETH_GSTRING_LEN] = {
593         "rx_packets", "rx_bytes", "wep_weak_iv_count",
594         "rx_duplicates", "rx_fragments", "rx_dropped",
595         "tx_packets", "tx_bytes", "tx_fragments",
596         "tx_filtered", "tx_retry_failed", "tx_retries",
597         "beacon_loss", "sta_state", "txrate", "rxrate", "signal",
598         "channel", "noise", "ch_time", "ch_time_busy",
599         "ch_time_ext_busy", "ch_time_rx", "ch_time_tx"
600 };
601 #define STA_STATS_LEN   ARRAY_SIZE(ieee80211_gstrings_sta_stats)
602
603 static int ieee80211_get_et_sset_count(struct wiphy *wiphy,
604                                        struct net_device *dev,
605                                        int sset)
606 {
607         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
608         int rv = 0;
609
610         if (sset == ETH_SS_STATS)
611                 rv += STA_STATS_LEN;
612
613         rv += drv_get_et_sset_count(sdata, sset);
614
615         if (rv == 0)
616                 return -EOPNOTSUPP;
617         return rv;
618 }
619
620 static void ieee80211_get_et_stats(struct wiphy *wiphy,
621                                    struct net_device *dev,
622                                    struct ethtool_stats *stats,
623                                    u64 *data)
624 {
625         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
626         struct ieee80211_chanctx_conf *chanctx_conf;
627         struct ieee80211_channel *channel;
628         struct sta_info *sta;
629         struct ieee80211_local *local = sdata->local;
630         struct station_info sinfo;
631         struct survey_info survey;
632         int i, q;
633 #define STA_STATS_SURVEY_LEN 7
634
635         memset(data, 0, sizeof(u64) * STA_STATS_LEN);
636
637 #define ADD_STA_STATS(sta)                              \
638         do {                                            \
639                 data[i++] += sta->rx_packets;           \
640                 data[i++] += sta->rx_bytes;             \
641                 data[i++] += sta->wep_weak_iv_count;    \
642                 data[i++] += sta->num_duplicates;       \
643                 data[i++] += sta->rx_fragments;         \
644                 data[i++] += sta->rx_dropped;           \
645                                                         \
646                 data[i++] += sinfo.tx_packets;          \
647                 data[i++] += sinfo.tx_bytes;            \
648                 data[i++] += sta->tx_fragments;         \
649                 data[i++] += sta->tx_filtered_count;    \
650                 data[i++] += sta->tx_retry_failed;      \
651                 data[i++] += sta->tx_retry_count;       \
652                 data[i++] += sta->beacon_loss_count;    \
653         } while (0)
654
655         /* For Managed stations, find the single station based on BSSID
656          * and use that.  For interface types, iterate through all available
657          * stations and add stats for any station that is assigned to this
658          * network device.
659          */
660
661         mutex_lock(&local->sta_mtx);
662
663         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
664                 sta = sta_info_get_bss(sdata, sdata->u.mgd.bssid);
665
666                 if (!(sta && !WARN_ON(sta->sdata->dev != dev)))
667                         goto do_survey;
668
669                 sinfo.filled = 0;
670                 sta_set_sinfo(sta, &sinfo);
671
672                 i = 0;
673                 ADD_STA_STATS(sta);
674
675                 data[i++] = sta->sta_state;
676
677
678                 if (sinfo.filled & STATION_INFO_TX_BITRATE)
679                         data[i] = 100000 *
680                                 cfg80211_calculate_bitrate(&sinfo.txrate);
681                 i++;
682                 if (sinfo.filled & STATION_INFO_RX_BITRATE)
683                         data[i] = 100000 *
684                                 cfg80211_calculate_bitrate(&sinfo.rxrate);
685                 i++;
686
687                 if (sinfo.filled & STATION_INFO_SIGNAL_AVG)
688                         data[i] = (u8)sinfo.signal_avg;
689                 i++;
690         } else {
691                 list_for_each_entry(sta, &local->sta_list, list) {
692                         /* Make sure this station belongs to the proper dev */
693                         if (sta->sdata->dev != dev)
694                                 continue;
695
696                         sinfo.filled = 0;
697                         sta_set_sinfo(sta, &sinfo);
698                         i = 0;
699                         ADD_STA_STATS(sta);
700                 }
701         }
702
703 do_survey:
704         i = STA_STATS_LEN - STA_STATS_SURVEY_LEN;
705         /* Get survey stats for current channel */
706         survey.filled = 0;
707
708         rcu_read_lock();
709         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
710         if (chanctx_conf)
711                 channel = chanctx_conf->def.chan;
712         else
713                 channel = NULL;
714         rcu_read_unlock();
715
716         if (channel) {
717                 q = 0;
718                 do {
719                         survey.filled = 0;
720                         if (drv_get_survey(local, q, &survey) != 0) {
721                                 survey.filled = 0;
722                                 break;
723                         }
724                         q++;
725                 } while (channel != survey.channel);
726         }
727
728         if (survey.filled)
729                 data[i++] = survey.channel->center_freq;
730         else
731                 data[i++] = 0;
732         if (survey.filled & SURVEY_INFO_NOISE_DBM)
733                 data[i++] = (u8)survey.noise;
734         else
735                 data[i++] = -1LL;
736         if (survey.filled & SURVEY_INFO_CHANNEL_TIME)
737                 data[i++] = survey.channel_time;
738         else
739                 data[i++] = -1LL;
740         if (survey.filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
741                 data[i++] = survey.channel_time_busy;
742         else
743                 data[i++] = -1LL;
744         if (survey.filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
745                 data[i++] = survey.channel_time_ext_busy;
746         else
747                 data[i++] = -1LL;
748         if (survey.filled & SURVEY_INFO_CHANNEL_TIME_RX)
749                 data[i++] = survey.channel_time_rx;
750         else
751                 data[i++] = -1LL;
752         if (survey.filled & SURVEY_INFO_CHANNEL_TIME_TX)
753                 data[i++] = survey.channel_time_tx;
754         else
755                 data[i++] = -1LL;
756
757         mutex_unlock(&local->sta_mtx);
758
759         if (WARN_ON(i != STA_STATS_LEN))
760                 return;
761
762         drv_get_et_stats(sdata, stats, &(data[STA_STATS_LEN]));
763 }
764
765 static void ieee80211_get_et_strings(struct wiphy *wiphy,
766                                      struct net_device *dev,
767                                      u32 sset, u8 *data)
768 {
769         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
770         int sz_sta_stats = 0;
771
772         if (sset == ETH_SS_STATS) {
773                 sz_sta_stats = sizeof(ieee80211_gstrings_sta_stats);
774                 memcpy(data, ieee80211_gstrings_sta_stats, sz_sta_stats);
775         }
776         drv_get_et_strings(sdata, sset, &(data[sz_sta_stats]));
777 }
778
779 static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
780                                  int idx, u8 *mac, struct station_info *sinfo)
781 {
782         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
783         struct ieee80211_local *local = sdata->local;
784         struct sta_info *sta;
785         int ret = -ENOENT;
786
787         mutex_lock(&local->sta_mtx);
788
789         sta = sta_info_get_by_idx(sdata, idx);
790         if (sta) {
791                 ret = 0;
792                 memcpy(mac, sta->sta.addr, ETH_ALEN);
793                 sta_set_sinfo(sta, sinfo);
794         }
795
796         mutex_unlock(&local->sta_mtx);
797
798         return ret;
799 }
800
801 static int ieee80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
802                                  int idx, struct survey_info *survey)
803 {
804         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
805
806         return drv_get_survey(local, idx, survey);
807 }
808
809 static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
810                                  u8 *mac, struct station_info *sinfo)
811 {
812         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
813         struct ieee80211_local *local = sdata->local;
814         struct sta_info *sta;
815         int ret = -ENOENT;
816
817         mutex_lock(&local->sta_mtx);
818
819         sta = sta_info_get_bss(sdata, mac);
820         if (sta) {
821                 ret = 0;
822                 sta_set_sinfo(sta, sinfo);
823         }
824
825         mutex_unlock(&local->sta_mtx);
826
827         return ret;
828 }
829
830 static int ieee80211_set_monitor_channel(struct wiphy *wiphy,
831                                          struct cfg80211_chan_def *chandef)
832 {
833         struct ieee80211_local *local = wiphy_priv(wiphy);
834         struct ieee80211_sub_if_data *sdata;
835         int ret = 0;
836
837         if (cfg80211_chandef_identical(&local->monitor_chandef, chandef))
838                 return 0;
839
840         mutex_lock(&local->mtx);
841         mutex_lock(&local->iflist_mtx);
842         if (local->use_chanctx) {
843                 sdata = rcu_dereference_protected(
844                                 local->monitor_sdata,
845                                 lockdep_is_held(&local->iflist_mtx));
846                 if (sdata) {
847                         ieee80211_vif_release_channel(sdata);
848                         ret = ieee80211_vif_use_channel(sdata, chandef,
849                                         IEEE80211_CHANCTX_EXCLUSIVE);
850                 }
851         } else if (local->open_count == local->monitors) {
852                 local->_oper_chandef = *chandef;
853                 ieee80211_hw_config(local, 0);
854         }
855
856         if (ret == 0)
857                 local->monitor_chandef = *chandef;
858         mutex_unlock(&local->iflist_mtx);
859         mutex_unlock(&local->mtx);
860
861         return ret;
862 }
863
864 static int ieee80211_set_probe_resp(struct ieee80211_sub_if_data *sdata,
865                                     const u8 *resp, size_t resp_len)
866 {
867         struct probe_resp *new, *old;
868
869         if (!resp || !resp_len)
870                 return 1;
871
872         old = sdata_dereference(sdata->u.ap.probe_resp, sdata);
873
874         new = kzalloc(sizeof(struct probe_resp) + resp_len, GFP_KERNEL);
875         if (!new)
876                 return -ENOMEM;
877
878         new->len = resp_len;
879         memcpy(new->data, resp, resp_len);
880
881         rcu_assign_pointer(sdata->u.ap.probe_resp, new);
882         if (old)
883                 kfree_rcu(old, rcu_head);
884
885         return 0;
886 }
887
888 static int ieee80211_assign_beacon(struct ieee80211_sub_if_data *sdata,
889                                    struct cfg80211_beacon_data *params)
890 {
891         struct beacon_data *new, *old;
892         int new_head_len, new_tail_len;
893         int size, err;
894         u32 changed = BSS_CHANGED_BEACON;
895
896         old = sdata_dereference(sdata->u.ap.beacon, sdata);
897
898
899         /* Need to have a beacon head if we don't have one yet */
900         if (!params->head && !old)
901                 return -EINVAL;
902
903         /* new or old head? */
904         if (params->head)
905                 new_head_len = params->head_len;
906         else
907                 new_head_len = old->head_len;
908
909         /* new or old tail? */
910         if (params->tail || !old)
911                 /* params->tail_len will be zero for !params->tail */
912                 new_tail_len = params->tail_len;
913         else
914                 new_tail_len = old->tail_len;
915
916         size = sizeof(*new) + new_head_len + new_tail_len;
917
918         new = kzalloc(size, GFP_KERNEL);
919         if (!new)
920                 return -ENOMEM;
921
922         /* start filling the new info now */
923
924         /*
925          * pointers go into the block we allocated,
926          * memory is | beacon_data | head | tail |
927          */
928         new->head = ((u8 *) new) + sizeof(*new);
929         new->tail = new->head + new_head_len;
930         new->head_len = new_head_len;
931         new->tail_len = new_tail_len;
932
933         /* copy in head */
934         if (params->head)
935                 memcpy(new->head, params->head, new_head_len);
936         else
937                 memcpy(new->head, old->head, new_head_len);
938
939         /* copy in optional tail */
940         if (params->tail)
941                 memcpy(new->tail, params->tail, new_tail_len);
942         else
943                 if (old)
944                         memcpy(new->tail, old->tail, new_tail_len);
945
946         err = ieee80211_set_probe_resp(sdata, params->probe_resp,
947                                        params->probe_resp_len);
948         if (err < 0)
949                 return err;
950         if (err == 0)
951                 changed |= BSS_CHANGED_AP_PROBE_RESP;
952
953         rcu_assign_pointer(sdata->u.ap.beacon, new);
954
955         if (old)
956                 kfree_rcu(old, rcu_head);
957
958         return changed;
959 }
960
961 static int ieee80211_start_ap(struct wiphy *wiphy, struct net_device *dev,
962                               struct cfg80211_ap_settings *params)
963 {
964         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
965         struct ieee80211_local *local = sdata->local;
966         struct beacon_data *old;
967         struct ieee80211_sub_if_data *vlan;
968         u32 changed = BSS_CHANGED_BEACON_INT |
969                       BSS_CHANGED_BEACON_ENABLED |
970                       BSS_CHANGED_BEACON |
971                       BSS_CHANGED_SSID |
972                       BSS_CHANGED_P2P_PS;
973         int err;
974
975         old = sdata_dereference(sdata->u.ap.beacon, sdata);
976         if (old)
977                 return -EALREADY;
978
979         /* TODO: make hostapd tell us what it wants */
980         sdata->smps_mode = IEEE80211_SMPS_OFF;
981         sdata->needed_rx_chains = sdata->local->rx_chains;
982
983         mutex_lock(&local->mtx);
984         err = ieee80211_vif_use_channel(sdata, &params->chandef,
985                                         IEEE80211_CHANCTX_SHARED);
986         if (!err)
987                 ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
988         mutex_unlock(&local->mtx);
989         if (err)
990                 return err;
991
992         /*
993          * Apply control port protocol, this allows us to
994          * not encrypt dynamic WEP control frames.
995          */
996         sdata->control_port_protocol = params->crypto.control_port_ethertype;
997         sdata->control_port_no_encrypt = params->crypto.control_port_no_encrypt;
998         sdata->encrypt_headroom = ieee80211_cs_headroom(sdata->local,
999                                                         &params->crypto,
1000                                                         sdata->vif.type);
1001
1002         list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
1003                 vlan->control_port_protocol =
1004                         params->crypto.control_port_ethertype;
1005                 vlan->control_port_no_encrypt =
1006                         params->crypto.control_port_no_encrypt;
1007                 vlan->encrypt_headroom =
1008                         ieee80211_cs_headroom(sdata->local,
1009                                               &params->crypto,
1010                                               vlan->vif.type);
1011         }
1012
1013         sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1014         sdata->vif.bss_conf.dtim_period = params->dtim_period;
1015         sdata->vif.bss_conf.enable_beacon = true;
1016
1017         sdata->vif.bss_conf.ssid_len = params->ssid_len;
1018         if (params->ssid_len)
1019                 memcpy(sdata->vif.bss_conf.ssid, params->ssid,
1020                        params->ssid_len);
1021         sdata->vif.bss_conf.hidden_ssid =
1022                 (params->hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE);
1023
1024         memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
1025                sizeof(sdata->vif.bss_conf.p2p_noa_attr));
1026         sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow =
1027                 params->p2p_ctwindow & IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
1028         if (params->p2p_opp_ps)
1029                 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow |=
1030                                         IEEE80211_P2P_OPPPS_ENABLE_BIT;
1031
1032         err = ieee80211_assign_beacon(sdata, &params->beacon);
1033         if (err < 0) {
1034                 ieee80211_vif_release_channel(sdata);
1035                 return err;
1036         }
1037         changed |= err;
1038
1039         err = drv_start_ap(sdata->local, sdata);
1040         if (err) {
1041                 old = sdata_dereference(sdata->u.ap.beacon, sdata);
1042
1043                 if (old)
1044                         kfree_rcu(old, rcu_head);
1045                 RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
1046                 ieee80211_vif_release_channel(sdata);
1047                 return err;
1048         }
1049
1050         ieee80211_recalc_dtim(local, sdata);
1051         ieee80211_bss_info_change_notify(sdata, changed);
1052
1053         netif_carrier_on(dev);
1054         list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1055                 netif_carrier_on(vlan->dev);
1056
1057         return 0;
1058 }
1059
1060 static int ieee80211_change_beacon(struct wiphy *wiphy, struct net_device *dev,
1061                                    struct cfg80211_beacon_data *params)
1062 {
1063         struct ieee80211_sub_if_data *sdata;
1064         struct beacon_data *old;
1065         int err;
1066
1067         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1068         sdata_assert_lock(sdata);
1069
1070         /* don't allow changing the beacon while CSA is in place - offset
1071          * of channel switch counter may change
1072          */
1073         if (sdata->vif.csa_active)
1074                 return -EBUSY;
1075
1076         old = sdata_dereference(sdata->u.ap.beacon, sdata);
1077         if (!old)
1078                 return -ENOENT;
1079
1080         err = ieee80211_assign_beacon(sdata, params);
1081         if (err < 0)
1082                 return err;
1083         ieee80211_bss_info_change_notify(sdata, err);
1084         return 0;
1085 }
1086
1087 bool ieee80211_csa_needs_block_tx(struct ieee80211_local *local)
1088 {
1089         struct ieee80211_sub_if_data *sdata;
1090
1091         lockdep_assert_held(&local->mtx);
1092
1093         rcu_read_lock();
1094         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
1095                 if (!ieee80211_sdata_running(sdata))
1096                         continue;
1097
1098                 if (!sdata->vif.csa_active)
1099                         continue;
1100
1101                 if (!sdata->csa_block_tx)
1102                         continue;
1103
1104                 rcu_read_unlock();
1105                 return true;
1106         }
1107         rcu_read_unlock();
1108
1109         return false;
1110 }
1111
1112 static int ieee80211_stop_ap(struct wiphy *wiphy, struct net_device *dev)
1113 {
1114         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1115         struct ieee80211_sub_if_data *vlan;
1116         struct ieee80211_local *local = sdata->local;
1117         struct beacon_data *old_beacon;
1118         struct probe_resp *old_probe_resp;
1119         struct cfg80211_chan_def chandef;
1120
1121         sdata_assert_lock(sdata);
1122
1123         old_beacon = sdata_dereference(sdata->u.ap.beacon, sdata);
1124         if (!old_beacon)
1125                 return -ENOENT;
1126         old_probe_resp = sdata_dereference(sdata->u.ap.probe_resp, sdata);
1127
1128         /* abort any running channel switch */
1129         mutex_lock(&local->mtx);
1130         sdata->vif.csa_active = false;
1131         if (!ieee80211_csa_needs_block_tx(local))
1132                 ieee80211_wake_queues_by_reason(&local->hw,
1133                                         IEEE80211_MAX_QUEUE_MAP,
1134                                         IEEE80211_QUEUE_STOP_REASON_CSA);
1135         mutex_unlock(&local->mtx);
1136
1137         kfree(sdata->u.ap.next_beacon);
1138         sdata->u.ap.next_beacon = NULL;
1139
1140         /* turn off carrier for this interface and dependent VLANs */
1141         list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1142                 netif_carrier_off(vlan->dev);
1143         netif_carrier_off(dev);
1144
1145         /* remove beacon and probe response */
1146         RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
1147         RCU_INIT_POINTER(sdata->u.ap.probe_resp, NULL);
1148         kfree_rcu(old_beacon, rcu_head);
1149         if (old_probe_resp)
1150                 kfree_rcu(old_probe_resp, rcu_head);
1151         sdata->u.ap.driver_smps_mode = IEEE80211_SMPS_OFF;
1152
1153         __sta_info_flush(sdata, true);
1154         ieee80211_free_keys(sdata, true);
1155
1156         sdata->vif.bss_conf.enable_beacon = false;
1157         sdata->vif.bss_conf.ssid_len = 0;
1158         clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
1159         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
1160
1161         if (sdata->wdev.cac_started) {
1162                 chandef = sdata->vif.bss_conf.chandef;
1163                 cancel_delayed_work_sync(&sdata->dfs_cac_timer_work);
1164                 cfg80211_cac_event(sdata->dev, &chandef,
1165                                    NL80211_RADAR_CAC_ABORTED,
1166                                    GFP_KERNEL);
1167         }
1168
1169         drv_stop_ap(sdata->local, sdata);
1170
1171         /* free all potentially still buffered bcast frames */
1172         local->total_ps_buffered -= skb_queue_len(&sdata->u.ap.ps.bc_buf);
1173         skb_queue_purge(&sdata->u.ap.ps.bc_buf);
1174
1175         mutex_lock(&local->mtx);
1176         ieee80211_vif_copy_chanctx_to_vlans(sdata, true);
1177         ieee80211_vif_release_channel(sdata);
1178         mutex_unlock(&local->mtx);
1179
1180         return 0;
1181 }
1182
1183 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1184 struct iapp_layer2_update {
1185         u8 da[ETH_ALEN];        /* broadcast */
1186         u8 sa[ETH_ALEN];        /* STA addr */
1187         __be16 len;             /* 6 */
1188         u8 dsap;                /* 0 */
1189         u8 ssap;                /* 0 */
1190         u8 control;
1191         u8 xid_info[3];
1192 } __packed;
1193
1194 static void ieee80211_send_layer2_update(struct sta_info *sta)
1195 {
1196         struct iapp_layer2_update *msg;
1197         struct sk_buff *skb;
1198
1199         /* Send Level 2 Update Frame to update forwarding tables in layer 2
1200          * bridge devices */
1201
1202         skb = dev_alloc_skb(sizeof(*msg));
1203         if (!skb)
1204                 return;
1205         msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
1206
1207         /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1208          * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1209
1210         eth_broadcast_addr(msg->da);
1211         memcpy(msg->sa, sta->sta.addr, ETH_ALEN);
1212         msg->len = htons(6);
1213         msg->dsap = 0;
1214         msg->ssap = 0x01;       /* NULL LSAP, CR Bit: Response */
1215         msg->control = 0xaf;    /* XID response lsb.1111F101.
1216                                  * F=0 (no poll command; unsolicited frame) */
1217         msg->xid_info[0] = 0x81;        /* XID format identifier */
1218         msg->xid_info[1] = 1;   /* LLC types/classes: Type 1 LLC */
1219         msg->xid_info[2] = 0;   /* XID sender's receive window size (RW) */
1220
1221         skb->dev = sta->sdata->dev;
1222         skb->protocol = eth_type_trans(skb, sta->sdata->dev);
1223         memset(skb->cb, 0, sizeof(skb->cb));
1224         netif_rx_ni(skb);
1225 }
1226
1227 static int sta_apply_auth_flags(struct ieee80211_local *local,
1228                                 struct sta_info *sta,
1229                                 u32 mask, u32 set)
1230 {
1231         int ret;
1232
1233         if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1234             set & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1235             !test_sta_flag(sta, WLAN_STA_AUTH)) {
1236                 ret = sta_info_move_state(sta, IEEE80211_STA_AUTH);
1237                 if (ret)
1238                         return ret;
1239         }
1240
1241         if (mask & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1242             set & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1243             !test_sta_flag(sta, WLAN_STA_ASSOC)) {
1244                 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1245                 if (ret)
1246                         return ret;
1247         }
1248
1249         if (mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
1250                 if (set & BIT(NL80211_STA_FLAG_AUTHORIZED))
1251                         ret = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
1252                 else if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1253                         ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1254                 else
1255                         ret = 0;
1256                 if (ret)
1257                         return ret;
1258         }
1259
1260         if (mask & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1261             !(set & BIT(NL80211_STA_FLAG_ASSOCIATED)) &&
1262             test_sta_flag(sta, WLAN_STA_ASSOC)) {
1263                 ret = sta_info_move_state(sta, IEEE80211_STA_AUTH);
1264                 if (ret)
1265                         return ret;
1266         }
1267
1268         if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1269             !(set & BIT(NL80211_STA_FLAG_AUTHENTICATED)) &&
1270             test_sta_flag(sta, WLAN_STA_AUTH)) {
1271                 ret = sta_info_move_state(sta, IEEE80211_STA_NONE);
1272                 if (ret)
1273                         return ret;
1274         }
1275
1276         return 0;
1277 }
1278
1279 static int sta_apply_parameters(struct ieee80211_local *local,
1280                                 struct sta_info *sta,
1281                                 struct station_parameters *params)
1282 {
1283         int ret = 0;
1284         struct ieee80211_supported_band *sband;
1285         struct ieee80211_sub_if_data *sdata = sta->sdata;
1286         enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
1287         u32 mask, set;
1288
1289         sband = local->hw.wiphy->bands[band];
1290
1291         mask = params->sta_flags_mask;
1292         set = params->sta_flags_set;
1293
1294         if (ieee80211_vif_is_mesh(&sdata->vif)) {
1295                 /*
1296                  * In mesh mode, ASSOCIATED isn't part of the nl80211
1297                  * API but must follow AUTHENTICATED for driver state.
1298                  */
1299                 if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED))
1300                         mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1301                 if (set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
1302                         set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1303         } else if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
1304                 /*
1305                  * TDLS -- everything follows authorized, but
1306                  * only becoming authorized is possible, not
1307                  * going back
1308                  */
1309                 if (set & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
1310                         set |= BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1311                                BIT(NL80211_STA_FLAG_ASSOCIATED);
1312                         mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1313                                 BIT(NL80211_STA_FLAG_ASSOCIATED);
1314                 }
1315         }
1316
1317         ret = sta_apply_auth_flags(local, sta, mask, set);
1318         if (ret)
1319                 return ret;
1320
1321         if (mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) {
1322                 if (set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
1323                         set_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE);
1324                 else
1325                         clear_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE);
1326         }
1327
1328         if (mask & BIT(NL80211_STA_FLAG_WME)) {
1329                 if (set & BIT(NL80211_STA_FLAG_WME)) {
1330                         set_sta_flag(sta, WLAN_STA_WME);
1331                         sta->sta.wme = true;
1332                 } else {
1333                         clear_sta_flag(sta, WLAN_STA_WME);
1334                         sta->sta.wme = false;
1335                 }
1336         }
1337
1338         if (mask & BIT(NL80211_STA_FLAG_MFP)) {
1339                 if (set & BIT(NL80211_STA_FLAG_MFP))
1340                         set_sta_flag(sta, WLAN_STA_MFP);
1341                 else
1342                         clear_sta_flag(sta, WLAN_STA_MFP);
1343         }
1344
1345         if (mask & BIT(NL80211_STA_FLAG_TDLS_PEER)) {
1346                 if (set & BIT(NL80211_STA_FLAG_TDLS_PEER))
1347                         set_sta_flag(sta, WLAN_STA_TDLS_PEER);
1348                 else
1349                         clear_sta_flag(sta, WLAN_STA_TDLS_PEER);
1350         }
1351
1352         if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD) {
1353                 sta->sta.uapsd_queues = params->uapsd_queues;
1354                 sta->sta.max_sp = params->max_sp;
1355         }
1356
1357         /*
1358          * cfg80211 validates this (1-2007) and allows setting the AID
1359          * only when creating a new station entry
1360          */
1361         if (params->aid)
1362                 sta->sta.aid = params->aid;
1363
1364         /*
1365          * Some of the following updates would be racy if called on an
1366          * existing station, via ieee80211_change_station(). However,
1367          * all such changes are rejected by cfg80211 except for updates
1368          * changing the supported rates on an existing but not yet used
1369          * TDLS peer.
1370          */
1371
1372         if (params->listen_interval >= 0)
1373                 sta->listen_interval = params->listen_interval;
1374
1375         if (params->supported_rates) {
1376                 ieee80211_parse_bitrates(&sdata->vif.bss_conf.chandef,
1377                                          sband, params->supported_rates,
1378                                          params->supported_rates_len,
1379                                          &sta->sta.supp_rates[band]);
1380         }
1381
1382         if (params->ht_capa)
1383                 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
1384                                                   params->ht_capa, sta);
1385
1386         if (params->vht_capa)
1387                 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
1388                                                     params->vht_capa, sta);
1389
1390         if (params->opmode_notif_used) {
1391                 /* returned value is only needed for rc update, but the
1392                  * rc isn't initialized here yet, so ignore it
1393                  */
1394                 __ieee80211_vht_handle_opmode(sdata, sta,
1395                                               params->opmode_notif,
1396                                               band, false);
1397         }
1398
1399         if (ieee80211_vif_is_mesh(&sdata->vif)) {
1400 #ifdef CONFIG_MAC80211_MESH
1401                 u32 changed = 0;
1402
1403                 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE) {
1404                         switch (params->plink_state) {
1405                         case NL80211_PLINK_ESTAB:
1406                                 if (sta->plink_state != NL80211_PLINK_ESTAB)
1407                                         changed = mesh_plink_inc_estab_count(
1408                                                         sdata);
1409                                 sta->plink_state = params->plink_state;
1410
1411                                 ieee80211_mps_sta_status_update(sta);
1412                                 changed |= ieee80211_mps_set_sta_local_pm(sta,
1413                                               sdata->u.mesh.mshcfg.power_mode);
1414                                 break;
1415                         case NL80211_PLINK_LISTEN:
1416                         case NL80211_PLINK_BLOCKED:
1417                         case NL80211_PLINK_OPN_SNT:
1418                         case NL80211_PLINK_OPN_RCVD:
1419                         case NL80211_PLINK_CNF_RCVD:
1420                         case NL80211_PLINK_HOLDING:
1421                                 if (sta->plink_state == NL80211_PLINK_ESTAB)
1422                                         changed = mesh_plink_dec_estab_count(
1423                                                         sdata);
1424                                 sta->plink_state = params->plink_state;
1425
1426                                 ieee80211_mps_sta_status_update(sta);
1427                                 changed |= ieee80211_mps_set_sta_local_pm(sta,
1428                                                 NL80211_MESH_POWER_UNKNOWN);
1429                                 break;
1430                         default:
1431                                 /*  nothing  */
1432                                 break;
1433                         }
1434                 }
1435
1436                 switch (params->plink_action) {
1437                 case NL80211_PLINK_ACTION_NO_ACTION:
1438                         /* nothing */
1439                         break;
1440                 case NL80211_PLINK_ACTION_OPEN:
1441                         changed |= mesh_plink_open(sta);
1442                         break;
1443                 case NL80211_PLINK_ACTION_BLOCK:
1444                         changed |= mesh_plink_block(sta);
1445                         break;
1446                 }
1447
1448                 if (params->local_pm)
1449                         changed |=
1450                               ieee80211_mps_set_sta_local_pm(sta,
1451                                                              params->local_pm);
1452                 ieee80211_mbss_info_change_notify(sdata, changed);
1453 #endif
1454         }
1455
1456         return 0;
1457 }
1458
1459 static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
1460                                  u8 *mac, struct station_parameters *params)
1461 {
1462         struct ieee80211_local *local = wiphy_priv(wiphy);
1463         struct sta_info *sta;
1464         struct ieee80211_sub_if_data *sdata;
1465         int err;
1466         int layer2_update;
1467
1468         if (params->vlan) {
1469                 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
1470
1471                 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1472                     sdata->vif.type != NL80211_IFTYPE_AP)
1473                         return -EINVAL;
1474         } else
1475                 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1476
1477         if (ether_addr_equal(mac, sdata->vif.addr))
1478                 return -EINVAL;
1479
1480         if (is_multicast_ether_addr(mac))
1481                 return -EINVAL;
1482
1483         sta = sta_info_alloc(sdata, mac, GFP_KERNEL);
1484         if (!sta)
1485                 return -ENOMEM;
1486
1487         /*
1488          * defaults -- if userspace wants something else we'll
1489          * change it accordingly in sta_apply_parameters()
1490          */
1491         if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))) {
1492                 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
1493                 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
1494         } else {
1495                 sta->sta.tdls = true;
1496         }
1497
1498         err = sta_apply_parameters(local, sta, params);
1499         if (err) {
1500                 sta_info_free(local, sta);
1501                 return err;
1502         }
1503
1504         /*
1505          * for TDLS, rate control should be initialized only when
1506          * rates are known and station is marked authorized
1507          */
1508         if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1509                 rate_control_rate_init(sta);
1510
1511         layer2_update = sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1512                 sdata->vif.type == NL80211_IFTYPE_AP;
1513
1514         err = sta_info_insert_rcu(sta);
1515         if (err) {
1516                 rcu_read_unlock();
1517                 return err;
1518         }
1519
1520         if (layer2_update)
1521                 ieee80211_send_layer2_update(sta);
1522
1523         rcu_read_unlock();
1524
1525         return 0;
1526 }
1527
1528 static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
1529                                  u8 *mac)
1530 {
1531         struct ieee80211_sub_if_data *sdata;
1532
1533         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1534
1535         if (mac)
1536                 return sta_info_destroy_addr_bss(sdata, mac);
1537
1538         sta_info_flush(sdata);
1539         return 0;
1540 }
1541
1542 static int ieee80211_change_station(struct wiphy *wiphy,
1543                                     struct net_device *dev, u8 *mac,
1544                                     struct station_parameters *params)
1545 {
1546         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1547         struct ieee80211_local *local = wiphy_priv(wiphy);
1548         struct sta_info *sta;
1549         struct ieee80211_sub_if_data *vlansdata;
1550         enum cfg80211_station_type statype;
1551         int err;
1552
1553         mutex_lock(&local->sta_mtx);
1554
1555         sta = sta_info_get_bss(sdata, mac);
1556         if (!sta) {
1557                 err = -ENOENT;
1558                 goto out_err;
1559         }
1560
1561         switch (sdata->vif.type) {
1562         case NL80211_IFTYPE_MESH_POINT:
1563                 if (sdata->u.mesh.user_mpm)
1564                         statype = CFG80211_STA_MESH_PEER_USER;
1565                 else
1566                         statype = CFG80211_STA_MESH_PEER_KERNEL;
1567                 break;
1568         case NL80211_IFTYPE_ADHOC:
1569                 statype = CFG80211_STA_IBSS;
1570                 break;
1571         case NL80211_IFTYPE_STATION:
1572                 if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
1573                         statype = CFG80211_STA_AP_STA;
1574                         break;
1575                 }
1576                 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1577                         statype = CFG80211_STA_TDLS_PEER_ACTIVE;
1578                 else
1579                         statype = CFG80211_STA_TDLS_PEER_SETUP;
1580                 break;
1581         case NL80211_IFTYPE_AP:
1582         case NL80211_IFTYPE_AP_VLAN:
1583                 statype = CFG80211_STA_AP_CLIENT;
1584                 break;
1585         default:
1586                 err = -EOPNOTSUPP;
1587                 goto out_err;
1588         }
1589
1590         err = cfg80211_check_station_change(wiphy, params, statype);
1591         if (err)
1592                 goto out_err;
1593
1594         if (params->vlan && params->vlan != sta->sdata->dev) {
1595                 bool prev_4addr = false;
1596                 bool new_4addr = false;
1597
1598                 vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
1599
1600                 if (params->vlan->ieee80211_ptr->use_4addr) {
1601                         if (vlansdata->u.vlan.sta) {
1602                                 err = -EBUSY;
1603                                 goto out_err;
1604                         }
1605
1606                         rcu_assign_pointer(vlansdata->u.vlan.sta, sta);
1607                         new_4addr = true;
1608                 }
1609
1610                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1611                     sta->sdata->u.vlan.sta) {
1612                         RCU_INIT_POINTER(sta->sdata->u.vlan.sta, NULL);
1613                         prev_4addr = true;
1614                 }
1615
1616                 sta->sdata = vlansdata;
1617
1618                 if (sta->sta_state == IEEE80211_STA_AUTHORIZED &&
1619                     prev_4addr != new_4addr) {
1620                         if (new_4addr)
1621                                 atomic_dec(&sta->sdata->bss->num_mcast_sta);
1622                         else
1623                                 atomic_inc(&sta->sdata->bss->num_mcast_sta);
1624                 }
1625
1626                 ieee80211_send_layer2_update(sta);
1627         }
1628
1629         err = sta_apply_parameters(local, sta, params);
1630         if (err)
1631                 goto out_err;
1632
1633         /* When peer becomes authorized, init rate control as well */
1634         if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
1635             test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1636                 rate_control_rate_init(sta);
1637
1638         mutex_unlock(&local->sta_mtx);
1639
1640         if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1641              sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1642             sta->known_smps_mode != sta->sdata->bss->req_smps &&
1643             test_sta_flag(sta, WLAN_STA_AUTHORIZED) &&
1644             sta_info_tx_streams(sta) != 1) {
1645                 ht_dbg(sta->sdata,
1646                        "%pM just authorized and MIMO capable - update SMPS\n",
1647                        sta->sta.addr);
1648                 ieee80211_send_smps_action(sta->sdata,
1649                         sta->sdata->bss->req_smps,
1650                         sta->sta.addr,
1651                         sta->sdata->vif.bss_conf.bssid);
1652         }
1653
1654         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1655             params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
1656                 ieee80211_recalc_ps(local, -1);
1657                 ieee80211_recalc_ps_vif(sdata);
1658         }
1659
1660         return 0;
1661 out_err:
1662         mutex_unlock(&local->sta_mtx);
1663         return err;
1664 }
1665
1666 #ifdef CONFIG_MAC80211_MESH
1667 static int ieee80211_add_mpath(struct wiphy *wiphy, struct net_device *dev,
1668                                  u8 *dst, u8 *next_hop)
1669 {
1670         struct ieee80211_sub_if_data *sdata;
1671         struct mesh_path *mpath;
1672         struct sta_info *sta;
1673
1674         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1675
1676         rcu_read_lock();
1677         sta = sta_info_get(sdata, next_hop);
1678         if (!sta) {
1679                 rcu_read_unlock();
1680                 return -ENOENT;
1681         }
1682
1683         mpath = mesh_path_add(sdata, dst);
1684         if (IS_ERR(mpath)) {
1685                 rcu_read_unlock();
1686                 return PTR_ERR(mpath);
1687         }
1688
1689         mesh_path_fix_nexthop(mpath, sta);
1690
1691         rcu_read_unlock();
1692         return 0;
1693 }
1694
1695 static int ieee80211_del_mpath(struct wiphy *wiphy, struct net_device *dev,
1696                                u8 *dst)
1697 {
1698         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1699
1700         if (dst)
1701                 return mesh_path_del(sdata, dst);
1702
1703         mesh_path_flush_by_iface(sdata);
1704         return 0;
1705 }
1706
1707 static int ieee80211_change_mpath(struct wiphy *wiphy,
1708                                     struct net_device *dev,
1709                                     u8 *dst, u8 *next_hop)
1710 {
1711         struct ieee80211_sub_if_data *sdata;
1712         struct mesh_path *mpath;
1713         struct sta_info *sta;
1714
1715         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1716
1717         rcu_read_lock();
1718
1719         sta = sta_info_get(sdata, next_hop);
1720         if (!sta) {
1721                 rcu_read_unlock();
1722                 return -ENOENT;
1723         }
1724
1725         mpath = mesh_path_lookup(sdata, dst);
1726         if (!mpath) {
1727                 rcu_read_unlock();
1728                 return -ENOENT;
1729         }
1730
1731         mesh_path_fix_nexthop(mpath, sta);
1732
1733         rcu_read_unlock();
1734         return 0;
1735 }
1736
1737 static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
1738                             struct mpath_info *pinfo)
1739 {
1740         struct sta_info *next_hop_sta = rcu_dereference(mpath->next_hop);
1741
1742         if (next_hop_sta)
1743                 memcpy(next_hop, next_hop_sta->sta.addr, ETH_ALEN);
1744         else
1745                 memset(next_hop, 0, ETH_ALEN);
1746
1747         memset(pinfo, 0, sizeof(*pinfo));
1748
1749         pinfo->generation = mesh_paths_generation;
1750
1751         pinfo->filled = MPATH_INFO_FRAME_QLEN |
1752                         MPATH_INFO_SN |
1753                         MPATH_INFO_METRIC |
1754                         MPATH_INFO_EXPTIME |
1755                         MPATH_INFO_DISCOVERY_TIMEOUT |
1756                         MPATH_INFO_DISCOVERY_RETRIES |
1757                         MPATH_INFO_FLAGS;
1758
1759         pinfo->frame_qlen = mpath->frame_queue.qlen;
1760         pinfo->sn = mpath->sn;
1761         pinfo->metric = mpath->metric;
1762         if (time_before(jiffies, mpath->exp_time))
1763                 pinfo->exptime = jiffies_to_msecs(mpath->exp_time - jiffies);
1764         pinfo->discovery_timeout =
1765                         jiffies_to_msecs(mpath->discovery_timeout);
1766         pinfo->discovery_retries = mpath->discovery_retries;
1767         if (mpath->flags & MESH_PATH_ACTIVE)
1768                 pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE;
1769         if (mpath->flags & MESH_PATH_RESOLVING)
1770                 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
1771         if (mpath->flags & MESH_PATH_SN_VALID)
1772                 pinfo->flags |= NL80211_MPATH_FLAG_SN_VALID;
1773         if (mpath->flags & MESH_PATH_FIXED)
1774                 pinfo->flags |= NL80211_MPATH_FLAG_FIXED;
1775         if (mpath->flags & MESH_PATH_RESOLVED)
1776                 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVED;
1777 }
1778
1779 static int ieee80211_get_mpath(struct wiphy *wiphy, struct net_device *dev,
1780                                u8 *dst, u8 *next_hop, struct mpath_info *pinfo)
1781
1782 {
1783         struct ieee80211_sub_if_data *sdata;
1784         struct mesh_path *mpath;
1785
1786         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1787
1788         rcu_read_lock();
1789         mpath = mesh_path_lookup(sdata, dst);
1790         if (!mpath) {
1791                 rcu_read_unlock();
1792                 return -ENOENT;
1793         }
1794         memcpy(dst, mpath->dst, ETH_ALEN);
1795         mpath_set_pinfo(mpath, next_hop, pinfo);
1796         rcu_read_unlock();
1797         return 0;
1798 }
1799
1800 static int ieee80211_dump_mpath(struct wiphy *wiphy, struct net_device *dev,
1801                                  int idx, u8 *dst, u8 *next_hop,
1802                                  struct mpath_info *pinfo)
1803 {
1804         struct ieee80211_sub_if_data *sdata;
1805         struct mesh_path *mpath;
1806
1807         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1808
1809         rcu_read_lock();
1810         mpath = mesh_path_lookup_by_idx(sdata, idx);
1811         if (!mpath) {
1812                 rcu_read_unlock();
1813                 return -ENOENT;
1814         }
1815         memcpy(dst, mpath->dst, ETH_ALEN);
1816         mpath_set_pinfo(mpath, next_hop, pinfo);
1817         rcu_read_unlock();
1818         return 0;
1819 }
1820
1821 static int ieee80211_get_mesh_config(struct wiphy *wiphy,
1822                                 struct net_device *dev,
1823                                 struct mesh_config *conf)
1824 {
1825         struct ieee80211_sub_if_data *sdata;
1826         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1827
1828         memcpy(conf, &(sdata->u.mesh.mshcfg), sizeof(struct mesh_config));
1829         return 0;
1830 }
1831
1832 static inline bool _chg_mesh_attr(enum nl80211_meshconf_params parm, u32 mask)
1833 {
1834         return (mask >> (parm-1)) & 0x1;
1835 }
1836
1837 static int copy_mesh_setup(struct ieee80211_if_mesh *ifmsh,
1838                 const struct mesh_setup *setup)
1839 {
1840         u8 *new_ie;
1841         const u8 *old_ie;
1842         struct ieee80211_sub_if_data *sdata = container_of(ifmsh,
1843                                         struct ieee80211_sub_if_data, u.mesh);
1844
1845         /* allocate information elements */
1846         new_ie = NULL;
1847         old_ie = ifmsh->ie;
1848
1849         if (setup->ie_len) {
1850                 new_ie = kmemdup(setup->ie, setup->ie_len,
1851                                 GFP_KERNEL);
1852                 if (!new_ie)
1853                         return -ENOMEM;
1854         }
1855         ifmsh->ie_len = setup->ie_len;
1856         ifmsh->ie = new_ie;
1857         kfree(old_ie);
1858
1859         /* now copy the rest of the setup parameters */
1860         ifmsh->mesh_id_len = setup->mesh_id_len;
1861         memcpy(ifmsh->mesh_id, setup->mesh_id, ifmsh->mesh_id_len);
1862         ifmsh->mesh_sp_id = setup->sync_method;
1863         ifmsh->mesh_pp_id = setup->path_sel_proto;
1864         ifmsh->mesh_pm_id = setup->path_metric;
1865         ifmsh->user_mpm = setup->user_mpm;
1866         ifmsh->mesh_auth_id = setup->auth_id;
1867         ifmsh->security = IEEE80211_MESH_SEC_NONE;
1868         if (setup->is_authenticated)
1869                 ifmsh->security |= IEEE80211_MESH_SEC_AUTHED;
1870         if (setup->is_secure)
1871                 ifmsh->security |= IEEE80211_MESH_SEC_SECURED;
1872
1873         /* mcast rate setting in Mesh Node */
1874         memcpy(sdata->vif.bss_conf.mcast_rate, setup->mcast_rate,
1875                                                 sizeof(setup->mcast_rate));
1876         sdata->vif.bss_conf.basic_rates = setup->basic_rates;
1877
1878         sdata->vif.bss_conf.beacon_int = setup->beacon_interval;
1879         sdata->vif.bss_conf.dtim_period = setup->dtim_period;
1880
1881         return 0;
1882 }
1883
1884 static int ieee80211_update_mesh_config(struct wiphy *wiphy,
1885                                         struct net_device *dev, u32 mask,
1886                                         const struct mesh_config *nconf)
1887 {
1888         struct mesh_config *conf;
1889         struct ieee80211_sub_if_data *sdata;
1890         struct ieee80211_if_mesh *ifmsh;
1891
1892         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1893         ifmsh = &sdata->u.mesh;
1894
1895         /* Set the config options which we are interested in setting */
1896         conf = &(sdata->u.mesh.mshcfg);
1897         if (_chg_mesh_attr(NL80211_MESHCONF_RETRY_TIMEOUT, mask))
1898                 conf->dot11MeshRetryTimeout = nconf->dot11MeshRetryTimeout;
1899         if (_chg_mesh_attr(NL80211_MESHCONF_CONFIRM_TIMEOUT, mask))
1900                 conf->dot11MeshConfirmTimeout = nconf->dot11MeshConfirmTimeout;
1901         if (_chg_mesh_attr(NL80211_MESHCONF_HOLDING_TIMEOUT, mask))
1902                 conf->dot11MeshHoldingTimeout = nconf->dot11MeshHoldingTimeout;
1903         if (_chg_mesh_attr(NL80211_MESHCONF_MAX_PEER_LINKS, mask))
1904                 conf->dot11MeshMaxPeerLinks = nconf->dot11MeshMaxPeerLinks;
1905         if (_chg_mesh_attr(NL80211_MESHCONF_MAX_RETRIES, mask))
1906                 conf->dot11MeshMaxRetries = nconf->dot11MeshMaxRetries;
1907         if (_chg_mesh_attr(NL80211_MESHCONF_TTL, mask))
1908                 conf->dot11MeshTTL = nconf->dot11MeshTTL;
1909         if (_chg_mesh_attr(NL80211_MESHCONF_ELEMENT_TTL, mask))
1910                 conf->element_ttl = nconf->element_ttl;
1911         if (_chg_mesh_attr(NL80211_MESHCONF_AUTO_OPEN_PLINKS, mask)) {
1912                 if (ifmsh->user_mpm)
1913                         return -EBUSY;
1914                 conf->auto_open_plinks = nconf->auto_open_plinks;
1915         }
1916         if (_chg_mesh_attr(NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR, mask))
1917                 conf->dot11MeshNbrOffsetMaxNeighbor =
1918                         nconf->dot11MeshNbrOffsetMaxNeighbor;
1919         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, mask))
1920                 conf->dot11MeshHWMPmaxPREQretries =
1921                         nconf->dot11MeshHWMPmaxPREQretries;
1922         if (_chg_mesh_attr(NL80211_MESHCONF_PATH_REFRESH_TIME, mask))
1923                 conf->path_refresh_time = nconf->path_refresh_time;
1924         if (_chg_mesh_attr(NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, mask))
1925                 conf->min_discovery_timeout = nconf->min_discovery_timeout;
1926         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, mask))
1927                 conf->dot11MeshHWMPactivePathTimeout =
1928                         nconf->dot11MeshHWMPactivePathTimeout;
1929         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, mask))
1930                 conf->dot11MeshHWMPpreqMinInterval =
1931                         nconf->dot11MeshHWMPpreqMinInterval;
1932         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL, mask))
1933                 conf->dot11MeshHWMPperrMinInterval =
1934                         nconf->dot11MeshHWMPperrMinInterval;
1935         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
1936                            mask))
1937                 conf->dot11MeshHWMPnetDiameterTraversalTime =
1938                         nconf->dot11MeshHWMPnetDiameterTraversalTime;
1939         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ROOTMODE, mask)) {
1940                 conf->dot11MeshHWMPRootMode = nconf->dot11MeshHWMPRootMode;
1941                 ieee80211_mesh_root_setup(ifmsh);
1942         }
1943         if (_chg_mesh_attr(NL80211_MESHCONF_GATE_ANNOUNCEMENTS, mask)) {
1944                 /* our current gate announcement implementation rides on root
1945                  * announcements, so require this ifmsh to also be a root node
1946                  * */
1947                 if (nconf->dot11MeshGateAnnouncementProtocol &&
1948                     !(conf->dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)) {
1949                         conf->dot11MeshHWMPRootMode = IEEE80211_PROACTIVE_RANN;
1950                         ieee80211_mesh_root_setup(ifmsh);
1951                 }
1952                 conf->dot11MeshGateAnnouncementProtocol =
1953                         nconf->dot11MeshGateAnnouncementProtocol;
1954         }
1955         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_RANN_INTERVAL, mask))
1956                 conf->dot11MeshHWMPRannInterval =
1957                         nconf->dot11MeshHWMPRannInterval;
1958         if (_chg_mesh_attr(NL80211_MESHCONF_FORWARDING, mask))
1959                 conf->dot11MeshForwarding = nconf->dot11MeshForwarding;
1960         if (_chg_mesh_attr(NL80211_MESHCONF_RSSI_THRESHOLD, mask)) {
1961                 /* our RSSI threshold implementation is supported only for
1962                  * devices that report signal in dBm.
1963                  */
1964                 if (!(sdata->local->hw.flags & IEEE80211_HW_SIGNAL_DBM))
1965                         return -ENOTSUPP;
1966                 conf->rssi_threshold = nconf->rssi_threshold;
1967         }
1968         if (_chg_mesh_attr(NL80211_MESHCONF_HT_OPMODE, mask)) {
1969                 conf->ht_opmode = nconf->ht_opmode;
1970                 sdata->vif.bss_conf.ht_operation_mode = nconf->ht_opmode;
1971                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_HT);
1972         }
1973         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT, mask))
1974                 conf->dot11MeshHWMPactivePathToRootTimeout =
1975                         nconf->dot11MeshHWMPactivePathToRootTimeout;
1976         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ROOT_INTERVAL, mask))
1977                 conf->dot11MeshHWMProotInterval =
1978                         nconf->dot11MeshHWMProotInterval;
1979         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL, mask))
1980                 conf->dot11MeshHWMPconfirmationInterval =
1981                         nconf->dot11MeshHWMPconfirmationInterval;
1982         if (_chg_mesh_attr(NL80211_MESHCONF_POWER_MODE, mask)) {
1983                 conf->power_mode = nconf->power_mode;
1984                 ieee80211_mps_local_status_update(sdata);
1985         }
1986         if (_chg_mesh_attr(NL80211_MESHCONF_AWAKE_WINDOW, mask))
1987                 conf->dot11MeshAwakeWindowDuration =
1988                         nconf->dot11MeshAwakeWindowDuration;
1989         if (_chg_mesh_attr(NL80211_MESHCONF_PLINK_TIMEOUT, mask))
1990                 conf->plink_timeout = nconf->plink_timeout;
1991         ieee80211_mbss_info_change_notify(sdata, BSS_CHANGED_BEACON);
1992         return 0;
1993 }
1994
1995 static int ieee80211_join_mesh(struct wiphy *wiphy, struct net_device *dev,
1996                                const struct mesh_config *conf,
1997                                const struct mesh_setup *setup)
1998 {
1999         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2000         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2001         int err;
2002
2003         memcpy(&ifmsh->mshcfg, conf, sizeof(struct mesh_config));
2004         err = copy_mesh_setup(ifmsh, setup);
2005         if (err)
2006                 return err;
2007
2008         /* can mesh use other SMPS modes? */
2009         sdata->smps_mode = IEEE80211_SMPS_OFF;
2010         sdata->needed_rx_chains = sdata->local->rx_chains;
2011
2012         mutex_lock(&sdata->local->mtx);
2013         err = ieee80211_vif_use_channel(sdata, &setup->chandef,
2014                                         IEEE80211_CHANCTX_SHARED);
2015         mutex_unlock(&sdata->local->mtx);
2016         if (err)
2017                 return err;
2018
2019         return ieee80211_start_mesh(sdata);
2020 }
2021
2022 static int ieee80211_leave_mesh(struct wiphy *wiphy, struct net_device *dev)
2023 {
2024         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2025
2026         ieee80211_stop_mesh(sdata);
2027         mutex_lock(&sdata->local->mtx);
2028         ieee80211_vif_release_channel(sdata);
2029         mutex_unlock(&sdata->local->mtx);
2030
2031         return 0;
2032 }
2033 #endif
2034
2035 static int ieee80211_change_bss(struct wiphy *wiphy,
2036                                 struct net_device *dev,
2037                                 struct bss_parameters *params)
2038 {
2039         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2040         enum ieee80211_band band;
2041         u32 changed = 0;
2042
2043         if (!sdata_dereference(sdata->u.ap.beacon, sdata))
2044                 return -ENOENT;
2045
2046         band = ieee80211_get_sdata_band(sdata);
2047
2048         if (params->use_cts_prot >= 0) {
2049                 sdata->vif.bss_conf.use_cts_prot = params->use_cts_prot;
2050                 changed |= BSS_CHANGED_ERP_CTS_PROT;
2051         }
2052         if (params->use_short_preamble >= 0) {
2053                 sdata->vif.bss_conf.use_short_preamble =
2054                         params->use_short_preamble;
2055                 changed |= BSS_CHANGED_ERP_PREAMBLE;
2056         }
2057
2058         if (!sdata->vif.bss_conf.use_short_slot &&
2059             band == IEEE80211_BAND_5GHZ) {
2060                 sdata->vif.bss_conf.use_short_slot = true;
2061                 changed |= BSS_CHANGED_ERP_SLOT;
2062         }
2063
2064         if (params->use_short_slot_time >= 0) {
2065                 sdata->vif.bss_conf.use_short_slot =
2066                         params->use_short_slot_time;
2067                 changed |= BSS_CHANGED_ERP_SLOT;
2068         }
2069
2070         if (params->basic_rates) {
2071                 ieee80211_parse_bitrates(&sdata->vif.bss_conf.chandef,
2072                                          wiphy->bands[band],
2073                                          params->basic_rates,
2074                                          params->basic_rates_len,
2075                                          &sdata->vif.bss_conf.basic_rates);
2076                 changed |= BSS_CHANGED_BASIC_RATES;
2077         }
2078
2079         if (params->ap_isolate >= 0) {
2080                 if (params->ap_isolate)
2081                         sdata->flags |= IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
2082                 else
2083                         sdata->flags &= ~IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
2084         }
2085
2086         if (params->ht_opmode >= 0) {
2087                 sdata->vif.bss_conf.ht_operation_mode =
2088                         (u16) params->ht_opmode;
2089                 changed |= BSS_CHANGED_HT;
2090         }
2091
2092         if (params->p2p_ctwindow >= 0) {
2093                 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow &=
2094                                         ~IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
2095                 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow |=
2096                         params->p2p_ctwindow & IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
2097                 changed |= BSS_CHANGED_P2P_PS;
2098         }
2099
2100         if (params->p2p_opp_ps > 0) {
2101                 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow |=
2102                                         IEEE80211_P2P_OPPPS_ENABLE_BIT;
2103                 changed |= BSS_CHANGED_P2P_PS;
2104         } else if (params->p2p_opp_ps == 0) {
2105                 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow &=
2106                                         ~IEEE80211_P2P_OPPPS_ENABLE_BIT;
2107                 changed |= BSS_CHANGED_P2P_PS;
2108         }
2109
2110         ieee80211_bss_info_change_notify(sdata, changed);
2111
2112         return 0;
2113 }
2114
2115 static int ieee80211_set_txq_params(struct wiphy *wiphy,
2116                                     struct net_device *dev,
2117                                     struct ieee80211_txq_params *params)
2118 {
2119         struct ieee80211_local *local = wiphy_priv(wiphy);
2120         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2121         struct ieee80211_tx_queue_params p;
2122
2123         if (!local->ops->conf_tx)
2124                 return -EOPNOTSUPP;
2125
2126         if (local->hw.queues < IEEE80211_NUM_ACS)
2127                 return -EOPNOTSUPP;
2128
2129         memset(&p, 0, sizeof(p));
2130         p.aifs = params->aifs;
2131         p.cw_max = params->cwmax;
2132         p.cw_min = params->cwmin;
2133         p.txop = params->txop;
2134
2135         /*
2136          * Setting tx queue params disables u-apsd because it's only
2137          * called in master mode.
2138          */
2139         p.uapsd = false;
2140
2141         sdata->tx_conf[params->ac] = p;
2142         if (drv_conf_tx(local, sdata, params->ac, &p)) {
2143                 wiphy_debug(local->hw.wiphy,
2144                             "failed to set TX queue parameters for AC %d\n",
2145                             params->ac);
2146                 return -EINVAL;
2147         }
2148
2149         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
2150
2151         return 0;
2152 }
2153
2154 #ifdef CONFIG_PM
2155 static int ieee80211_suspend(struct wiphy *wiphy,
2156                              struct cfg80211_wowlan *wowlan)
2157 {
2158         return __ieee80211_suspend(wiphy_priv(wiphy), wowlan);
2159 }
2160
2161 static int ieee80211_resume(struct wiphy *wiphy)
2162 {
2163         return __ieee80211_resume(wiphy_priv(wiphy));
2164 }
2165 #else
2166 #define ieee80211_suspend NULL
2167 #define ieee80211_resume NULL
2168 #endif
2169
2170 static int ieee80211_scan(struct wiphy *wiphy,
2171                           struct cfg80211_scan_request *req)
2172 {
2173         struct ieee80211_sub_if_data *sdata;
2174
2175         sdata = IEEE80211_WDEV_TO_SUB_IF(req->wdev);
2176
2177         switch (ieee80211_vif_type_p2p(&sdata->vif)) {
2178         case NL80211_IFTYPE_STATION:
2179         case NL80211_IFTYPE_ADHOC:
2180         case NL80211_IFTYPE_MESH_POINT:
2181         case NL80211_IFTYPE_P2P_CLIENT:
2182         case NL80211_IFTYPE_P2P_DEVICE:
2183                 break;
2184         case NL80211_IFTYPE_P2P_GO:
2185                 if (sdata->local->ops->hw_scan)
2186                         break;
2187                 /*
2188                  * FIXME: implement NoA while scanning in software,
2189                  * for now fall through to allow scanning only when
2190                  * beaconing hasn't been configured yet
2191                  */
2192         case NL80211_IFTYPE_AP:
2193                 /*
2194                  * If the scan has been forced (and the driver supports
2195                  * forcing), don't care about being beaconing already.
2196                  * This will create problems to the attached stations (e.g. all
2197                  * the  frames sent while scanning on other channel will be
2198                  * lost)
2199                  */
2200                 if (sdata->u.ap.beacon &&
2201                     (!(wiphy->features & NL80211_FEATURE_AP_SCAN) ||
2202                      !(req->flags & NL80211_SCAN_FLAG_AP)))
2203                         return -EOPNOTSUPP;
2204                 break;
2205         default:
2206                 return -EOPNOTSUPP;
2207         }
2208
2209         return ieee80211_request_scan(sdata, req);
2210 }
2211
2212 static int
2213 ieee80211_sched_scan_start(struct wiphy *wiphy,
2214                            struct net_device *dev,
2215                            struct cfg80211_sched_scan_request *req)
2216 {
2217         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2218
2219         if (!sdata->local->ops->sched_scan_start)
2220                 return -EOPNOTSUPP;
2221
2222         return ieee80211_request_sched_scan_start(sdata, req);
2223 }
2224
2225 static int
2226 ieee80211_sched_scan_stop(struct wiphy *wiphy, struct net_device *dev)
2227 {
2228         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2229
2230         if (!sdata->local->ops->sched_scan_stop)
2231                 return -EOPNOTSUPP;
2232
2233         return ieee80211_request_sched_scan_stop(sdata);
2234 }
2235
2236 static int ieee80211_auth(struct wiphy *wiphy, struct net_device *dev,
2237                           struct cfg80211_auth_request *req)
2238 {
2239         return ieee80211_mgd_auth(IEEE80211_DEV_TO_SUB_IF(dev), req);
2240 }
2241
2242 static int ieee80211_assoc(struct wiphy *wiphy, struct net_device *dev,
2243                            struct cfg80211_assoc_request *req)
2244 {
2245         return ieee80211_mgd_assoc(IEEE80211_DEV_TO_SUB_IF(dev), req);
2246 }
2247
2248 static int ieee80211_deauth(struct wiphy *wiphy, struct net_device *dev,
2249                             struct cfg80211_deauth_request *req)
2250 {
2251         return ieee80211_mgd_deauth(IEEE80211_DEV_TO_SUB_IF(dev), req);
2252 }
2253
2254 static int ieee80211_disassoc(struct wiphy *wiphy, struct net_device *dev,
2255                               struct cfg80211_disassoc_request *req)
2256 {
2257         return ieee80211_mgd_disassoc(IEEE80211_DEV_TO_SUB_IF(dev), req);
2258 }
2259
2260 static int ieee80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2261                                struct cfg80211_ibss_params *params)
2262 {
2263         return ieee80211_ibss_join(IEEE80211_DEV_TO_SUB_IF(dev), params);
2264 }
2265
2266 static int ieee80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2267 {
2268         return ieee80211_ibss_leave(IEEE80211_DEV_TO_SUB_IF(dev));
2269 }
2270
2271 static int ieee80211_set_mcast_rate(struct wiphy *wiphy, struct net_device *dev,
2272                                     int rate[IEEE80211_NUM_BANDS])
2273 {
2274         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2275
2276         memcpy(sdata->vif.bss_conf.mcast_rate, rate,
2277                sizeof(int) * IEEE80211_NUM_BANDS);
2278
2279         return 0;
2280 }
2281
2282 static int ieee80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
2283 {
2284         struct ieee80211_local *local = wiphy_priv(wiphy);
2285         int err;
2286
2287         if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
2288                 err = drv_set_frag_threshold(local, wiphy->frag_threshold);
2289
2290                 if (err)
2291                         return err;
2292         }
2293
2294         if (changed & WIPHY_PARAM_COVERAGE_CLASS) {
2295                 err = drv_set_coverage_class(local, wiphy->coverage_class);
2296
2297                 if (err)
2298                         return err;
2299         }
2300
2301         if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
2302                 err = drv_set_rts_threshold(local, wiphy->rts_threshold);
2303
2304                 if (err)
2305                         return err;
2306         }
2307
2308         if (changed & WIPHY_PARAM_RETRY_SHORT) {
2309                 if (wiphy->retry_short > IEEE80211_MAX_TX_RETRY)
2310                         return -EINVAL;
2311                 local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
2312         }
2313         if (changed & WIPHY_PARAM_RETRY_LONG) {
2314                 if (wiphy->retry_long > IEEE80211_MAX_TX_RETRY)
2315                         return -EINVAL;
2316                 local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
2317         }
2318         if (changed &
2319             (WIPHY_PARAM_RETRY_SHORT | WIPHY_PARAM_RETRY_LONG))
2320                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_RETRY_LIMITS);
2321
2322         return 0;
2323 }
2324
2325 static int ieee80211_set_tx_power(struct wiphy *wiphy,
2326                                   struct wireless_dev *wdev,
2327                                   enum nl80211_tx_power_setting type, int mbm)
2328 {
2329         struct ieee80211_local *local = wiphy_priv(wiphy);
2330         struct ieee80211_sub_if_data *sdata;
2331
2332         if (wdev) {
2333                 sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2334
2335                 switch (type) {
2336                 case NL80211_TX_POWER_AUTOMATIC:
2337                         sdata->user_power_level = IEEE80211_UNSET_POWER_LEVEL;
2338                         break;
2339                 case NL80211_TX_POWER_LIMITED:
2340                 case NL80211_TX_POWER_FIXED:
2341                         if (mbm < 0 || (mbm % 100))
2342                                 return -EOPNOTSUPP;
2343                         sdata->user_power_level = MBM_TO_DBM(mbm);
2344                         break;
2345                 }
2346
2347                 ieee80211_recalc_txpower(sdata);
2348
2349                 return 0;
2350         }
2351
2352         switch (type) {
2353         case NL80211_TX_POWER_AUTOMATIC:
2354                 local->user_power_level = IEEE80211_UNSET_POWER_LEVEL;
2355                 break;
2356         case NL80211_TX_POWER_LIMITED:
2357         case NL80211_TX_POWER_FIXED:
2358                 if (mbm < 0 || (mbm % 100))
2359                         return -EOPNOTSUPP;
2360                 local->user_power_level = MBM_TO_DBM(mbm);
2361                 break;
2362         }
2363
2364         mutex_lock(&local->iflist_mtx);
2365         list_for_each_entry(sdata, &local->interfaces, list)
2366                 sdata->user_power_level = local->user_power_level;
2367         list_for_each_entry(sdata, &local->interfaces, list)
2368                 ieee80211_recalc_txpower(sdata);
2369         mutex_unlock(&local->iflist_mtx);
2370
2371         return 0;
2372 }
2373
2374 static int ieee80211_get_tx_power(struct wiphy *wiphy,
2375                                   struct wireless_dev *wdev,
2376                                   int *dbm)
2377 {
2378         struct ieee80211_local *local = wiphy_priv(wiphy);
2379         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2380
2381         if (!local->use_chanctx)
2382                 *dbm = local->hw.conf.power_level;
2383         else
2384                 *dbm = sdata->vif.bss_conf.txpower;
2385
2386         return 0;
2387 }
2388
2389 static int ieee80211_set_wds_peer(struct wiphy *wiphy, struct net_device *dev,
2390                                   const u8 *addr)
2391 {
2392         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2393
2394         memcpy(&sdata->u.wds.remote_addr, addr, ETH_ALEN);
2395
2396         return 0;
2397 }
2398
2399 static void ieee80211_rfkill_poll(struct wiphy *wiphy)
2400 {
2401         struct ieee80211_local *local = wiphy_priv(wiphy);
2402
2403         drv_rfkill_poll(local);
2404 }
2405
2406 #ifdef CONFIG_NL80211_TESTMODE
2407 static int ieee80211_testmode_cmd(struct wiphy *wiphy,
2408                                   struct wireless_dev *wdev,
2409                                   void *data, int len)
2410 {
2411         struct ieee80211_local *local = wiphy_priv(wiphy);
2412         struct ieee80211_vif *vif = NULL;
2413
2414         if (!local->ops->testmode_cmd)
2415                 return -EOPNOTSUPP;
2416
2417         if (wdev) {
2418                 struct ieee80211_sub_if_data *sdata;
2419
2420                 sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2421                 if (sdata->flags & IEEE80211_SDATA_IN_DRIVER)
2422                         vif = &sdata->vif;
2423         }
2424
2425         return local->ops->testmode_cmd(&local->hw, vif, data, len);
2426 }
2427
2428 static int ieee80211_testmode_dump(struct wiphy *wiphy,
2429                                    struct sk_buff *skb,
2430                                    struct netlink_callback *cb,
2431                                    void *data, int len)
2432 {
2433         struct ieee80211_local *local = wiphy_priv(wiphy);
2434
2435         if (!local->ops->testmode_dump)
2436                 return -EOPNOTSUPP;
2437
2438         return local->ops->testmode_dump(&local->hw, skb, cb, data, len);
2439 }
2440 #endif
2441
2442 int __ieee80211_request_smps_ap(struct ieee80211_sub_if_data *sdata,
2443                                 enum ieee80211_smps_mode smps_mode)
2444 {
2445         struct sta_info *sta;
2446         enum ieee80211_smps_mode old_req;
2447         int i;
2448
2449         if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_AP))
2450                 return -EINVAL;
2451
2452         if (sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
2453                 return 0;
2454
2455         old_req = sdata->u.ap.req_smps;
2456         sdata->u.ap.req_smps = smps_mode;
2457
2458         /* AUTOMATIC doesn't mean much for AP - don't allow it */
2459         if (old_req == smps_mode ||
2460             smps_mode == IEEE80211_SMPS_AUTOMATIC)
2461                 return 0;
2462
2463          /* If no associated stations, there's no need to do anything */
2464         if (!atomic_read(&sdata->u.ap.num_mcast_sta)) {
2465                 sdata->smps_mode = smps_mode;
2466                 ieee80211_queue_work(&sdata->local->hw, &sdata->recalc_smps);
2467                 return 0;
2468         }
2469
2470         ht_dbg(sdata,
2471                "SMSP %d requested in AP mode, sending Action frame to %d stations\n",
2472                smps_mode, atomic_read(&sdata->u.ap.num_mcast_sta));
2473
2474         mutex_lock(&sdata->local->sta_mtx);
2475         for (i = 0; i < STA_HASH_SIZE; i++) {
2476                 for (sta = rcu_dereference_protected(sdata->local->sta_hash[i],
2477                                 lockdep_is_held(&sdata->local->sta_mtx));
2478                      sta;
2479                      sta = rcu_dereference_protected(sta->hnext,
2480                                 lockdep_is_held(&sdata->local->sta_mtx))) {
2481                         /*
2482                          * Only stations associated to our AP and
2483                          * associated VLANs
2484                          */
2485                         if (sta->sdata->bss != &sdata->u.ap)
2486                                 continue;
2487
2488                         /* This station doesn't support MIMO - skip it */
2489                         if (sta_info_tx_streams(sta) == 1)
2490                                 continue;
2491
2492                         /*
2493                          * Don't wake up a STA just to send the action frame
2494                          * unless we are getting more restrictive.
2495                          */
2496                         if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
2497                             !ieee80211_smps_is_restrictive(sta->known_smps_mode,
2498                                                            smps_mode)) {
2499                                 ht_dbg(sdata,
2500                                        "Won't send SMPS to sleeping STA %pM\n",
2501                                        sta->sta.addr);
2502                                 continue;
2503                         }
2504
2505                         /*
2506                          * If the STA is not authorized, wait until it gets
2507                          * authorized and the action frame will be sent then.
2508                          */
2509                         if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2510                                 continue;
2511
2512                         ht_dbg(sdata, "Sending SMPS to %pM\n", sta->sta.addr);
2513                         ieee80211_send_smps_action(sdata, smps_mode,
2514                                                    sta->sta.addr,
2515                                                    sdata->vif.bss_conf.bssid);
2516                 }
2517         }
2518         mutex_unlock(&sdata->local->sta_mtx);
2519
2520         sdata->smps_mode = smps_mode;
2521         ieee80211_queue_work(&sdata->local->hw, &sdata->recalc_smps);
2522
2523         return 0;
2524 }
2525
2526 int __ieee80211_request_smps_mgd(struct ieee80211_sub_if_data *sdata,
2527                                  enum ieee80211_smps_mode smps_mode)
2528 {
2529         const u8 *ap;
2530         enum ieee80211_smps_mode old_req;
2531         int err;
2532
2533         lockdep_assert_held(&sdata->wdev.mtx);
2534
2535         if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION))
2536                 return -EINVAL;
2537
2538         old_req = sdata->u.mgd.req_smps;
2539         sdata->u.mgd.req_smps = smps_mode;
2540
2541         if (old_req == smps_mode &&
2542             smps_mode != IEEE80211_SMPS_AUTOMATIC)
2543                 return 0;
2544
2545         /*
2546          * If not associated, or current association is not an HT
2547          * association, there's no need to do anything, just store
2548          * the new value until we associate.
2549          */
2550         if (!sdata->u.mgd.associated ||
2551             sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
2552                 return 0;
2553
2554         ap = sdata->u.mgd.associated->bssid;
2555
2556         if (smps_mode == IEEE80211_SMPS_AUTOMATIC) {
2557                 if (sdata->u.mgd.powersave)
2558                         smps_mode = IEEE80211_SMPS_DYNAMIC;
2559                 else
2560                         smps_mode = IEEE80211_SMPS_OFF;
2561         }
2562
2563         /* send SM PS frame to AP */
2564         err = ieee80211_send_smps_action(sdata, smps_mode,
2565                                          ap, ap);
2566         if (err)
2567                 sdata->u.mgd.req_smps = old_req;
2568
2569         return err;
2570 }
2571
2572 static int ieee80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
2573                                     bool enabled, int timeout)
2574 {
2575         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2576         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2577
2578         if (sdata->vif.type != NL80211_IFTYPE_STATION)
2579                 return -EOPNOTSUPP;
2580
2581         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
2582                 return -EOPNOTSUPP;
2583
2584         if (enabled == sdata->u.mgd.powersave &&
2585             timeout == local->dynamic_ps_forced_timeout)
2586                 return 0;
2587
2588         sdata->u.mgd.powersave = enabled;
2589         local->dynamic_ps_forced_timeout = timeout;
2590
2591         /* no change, but if automatic follow powersave */
2592         sdata_lock(sdata);
2593         __ieee80211_request_smps_mgd(sdata, sdata->u.mgd.req_smps);
2594         sdata_unlock(sdata);
2595
2596         if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
2597                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2598
2599         ieee80211_recalc_ps(local, -1);
2600         ieee80211_recalc_ps_vif(sdata);
2601
2602         return 0;
2603 }
2604
2605 static int ieee80211_set_cqm_rssi_config(struct wiphy *wiphy,
2606                                          struct net_device *dev,
2607                                          s32 rssi_thold, u32 rssi_hyst)
2608 {
2609         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2610         struct ieee80211_vif *vif = &sdata->vif;
2611         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
2612
2613         if (rssi_thold == bss_conf->cqm_rssi_thold &&
2614             rssi_hyst == bss_conf->cqm_rssi_hyst)
2615                 return 0;
2616
2617         bss_conf->cqm_rssi_thold = rssi_thold;
2618         bss_conf->cqm_rssi_hyst = rssi_hyst;
2619
2620         /* tell the driver upon association, unless already associated */
2621         if (sdata->u.mgd.associated &&
2622             sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)
2623                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_CQM);
2624
2625         return 0;
2626 }
2627
2628 static int ieee80211_set_bitrate_mask(struct wiphy *wiphy,
2629                                       struct net_device *dev,
2630                                       const u8 *addr,
2631                                       const struct cfg80211_bitrate_mask *mask)
2632 {
2633         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2634         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2635         int i, ret;
2636
2637         if (!ieee80211_sdata_running(sdata))
2638                 return -ENETDOWN;
2639
2640         if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) {
2641                 ret = drv_set_bitrate_mask(local, sdata, mask);
2642                 if (ret)
2643                         return ret;
2644         }
2645
2646         for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
2647                 struct ieee80211_supported_band *sband = wiphy->bands[i];
2648                 int j;
2649
2650                 sdata->rc_rateidx_mask[i] = mask->control[i].legacy;
2651                 memcpy(sdata->rc_rateidx_mcs_mask[i], mask->control[i].ht_mcs,
2652                        sizeof(mask->control[i].ht_mcs));
2653
2654                 sdata->rc_has_mcs_mask[i] = false;
2655                 if (!sband)
2656                         continue;
2657
2658                 for (j = 0; j < IEEE80211_HT_MCS_MASK_LEN; j++)
2659                         if (~sdata->rc_rateidx_mcs_mask[i][j]) {
2660                                 sdata->rc_has_mcs_mask[i] = true;
2661                                 break;
2662                         }
2663         }
2664
2665         return 0;
2666 }
2667
2668 static int ieee80211_start_roc_work(struct ieee80211_local *local,
2669                                     struct ieee80211_sub_if_data *sdata,
2670                                     struct ieee80211_channel *channel,
2671                                     unsigned int duration, u64 *cookie,
2672                                     struct sk_buff *txskb,
2673                                     enum ieee80211_roc_type type)
2674 {
2675         struct ieee80211_roc_work *roc, *tmp;
2676         bool queued = false;
2677         int ret;
2678
2679         lockdep_assert_held(&local->mtx);
2680
2681         if (local->use_chanctx && !local->ops->remain_on_channel)
2682                 return -EOPNOTSUPP;
2683
2684         roc = kzalloc(sizeof(*roc), GFP_KERNEL);
2685         if (!roc)
2686                 return -ENOMEM;
2687
2688         /*
2689          * If the duration is zero, then the driver
2690          * wouldn't actually do anything. Set it to
2691          * 10 for now.
2692          *
2693          * TODO: cancel the off-channel operation
2694          *       when we get the SKB's TX status and
2695          *       the wait time was zero before.
2696          */
2697         if (!duration)
2698                 duration = 10;
2699
2700         roc->chan = channel;
2701         roc->duration = duration;
2702         roc->req_duration = duration;
2703         roc->frame = txskb;
2704         roc->type = type;
2705         roc->mgmt_tx_cookie = (unsigned long)txskb;
2706         roc->sdata = sdata;
2707         INIT_DELAYED_WORK(&roc->work, ieee80211_sw_roc_work);
2708         INIT_LIST_HEAD(&roc->dependents);
2709
2710         /*
2711          * cookie is either the roc cookie (for normal roc)
2712          * or the SKB (for mgmt TX)
2713          */
2714         if (!txskb) {
2715                 /* local->mtx protects this */
2716                 local->roc_cookie_counter++;
2717                 roc->cookie = local->roc_cookie_counter;
2718                 /* wow, you wrapped 64 bits ... more likely a bug */
2719                 if (WARN_ON(roc->cookie == 0)) {
2720                         roc->cookie = 1;
2721                         local->roc_cookie_counter++;
2722                 }
2723                 *cookie = roc->cookie;
2724         } else {
2725                 *cookie = (unsigned long)txskb;
2726         }
2727
2728         /* if there's one pending or we're scanning, queue this one */
2729         if (!list_empty(&local->roc_list) ||
2730             local->scanning || local->radar_detect_enabled)
2731                 goto out_check_combine;
2732
2733         /* if not HW assist, just queue & schedule work */
2734         if (!local->ops->remain_on_channel) {
2735                 ieee80211_queue_delayed_work(&local->hw, &roc->work, 0);
2736                 goto out_queue;
2737         }
2738
2739         /* otherwise actually kick it off here (for error handling) */
2740
2741         ret = drv_remain_on_channel(local, sdata, channel, duration, type);
2742         if (ret) {
2743                 kfree(roc);
2744                 return ret;
2745         }
2746
2747         roc->started = true;
2748         goto out_queue;
2749
2750  out_check_combine:
2751         list_for_each_entry(tmp, &local->roc_list, list) {
2752                 if (tmp->chan != channel || tmp->sdata != sdata)
2753                         continue;
2754
2755                 /*
2756                  * Extend this ROC if possible:
2757                  *
2758                  * If it hasn't started yet, just increase the duration
2759                  * and add the new one to the list of dependents.
2760                  * If the type of the new ROC has higher priority, modify the
2761                  * type of the previous one to match that of the new one.
2762                  */
2763                 if (!tmp->started) {
2764                         list_add_tail(&roc->list, &tmp->dependents);
2765                         tmp->duration = max(tmp->duration, roc->duration);
2766                         tmp->type = max(tmp->type, roc->type);
2767                         queued = true;
2768                         break;
2769                 }
2770
2771                 /* If it has already started, it's more difficult ... */
2772                 if (local->ops->remain_on_channel) {
2773                         unsigned long j = jiffies;
2774
2775                         /*
2776                          * In the offloaded ROC case, if it hasn't begun, add
2777                          * this new one to the dependent list to be handled
2778                          * when the master one begins. If it has begun,
2779                          * check that there's still a minimum time left and
2780                          * if so, start this one, transmitting the frame, but
2781                          * add it to the list directly after this one with
2782                          * a reduced time so we'll ask the driver to execute
2783                          * it right after finishing the previous one, in the
2784                          * hope that it'll also be executed right afterwards,
2785                          * effectively extending the old one.
2786                          * If there's no minimum time left, just add it to the
2787                          * normal list.
2788                          * TODO: the ROC type is ignored here, assuming that it
2789                          * is better to immediately use the current ROC.
2790                          */
2791                         if (!tmp->hw_begun) {
2792                                 list_add_tail(&roc->list, &tmp->dependents);
2793                                 queued = true;
2794                                 break;
2795                         }
2796
2797                         if (time_before(j + IEEE80211_ROC_MIN_LEFT,
2798                                         tmp->hw_start_time +
2799                                         msecs_to_jiffies(tmp->duration))) {
2800                                 int new_dur;
2801
2802                                 ieee80211_handle_roc_started(roc);
2803
2804                                 new_dur = roc->duration -
2805                                           jiffies_to_msecs(tmp->hw_start_time +
2806                                                            msecs_to_jiffies(
2807                                                                 tmp->duration) -
2808                                                            j);
2809
2810                                 if (new_dur > 0) {
2811                                         /* add right after tmp */
2812                                         list_add(&roc->list, &tmp->list);
2813                                 } else {
2814                                         list_add_tail(&roc->list,
2815                                                       &tmp->dependents);
2816                                 }
2817                                 queued = true;
2818                         }
2819                 } else if (del_timer_sync(&tmp->work.timer)) {
2820                         unsigned long new_end;
2821
2822                         /*
2823                          * In the software ROC case, cancel the timer, if
2824                          * that fails then the finish work is already
2825                          * queued/pending and thus we queue the new ROC
2826                          * normally, if that succeeds then we can extend
2827                          * the timer duration and TX the frame (if any.)
2828                          */
2829
2830                         list_add_tail(&roc->list, &tmp->dependents);
2831                         queued = true;
2832
2833                         new_end = jiffies + msecs_to_jiffies(roc->duration);
2834
2835                         /* ok, it was started & we canceled timer */
2836                         if (time_after(new_end, tmp->work.timer.expires))
2837                                 mod_timer(&tmp->work.timer, new_end);
2838                         else
2839                                 add_timer(&tmp->work.timer);
2840
2841                         ieee80211_handle_roc_started(roc);
2842                 }
2843                 break;
2844         }
2845
2846  out_queue:
2847         if (!queued)
2848                 list_add_tail(&roc->list, &local->roc_list);
2849
2850         return 0;
2851 }
2852
2853 static int ieee80211_remain_on_channel(struct wiphy *wiphy,
2854                                        struct wireless_dev *wdev,
2855                                        struct ieee80211_channel *chan,
2856                                        unsigned int duration,
2857                                        u64 *cookie)
2858 {
2859         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2860         struct ieee80211_local *local = sdata->local;
2861         int ret;
2862
2863         mutex_lock(&local->mtx);
2864         ret = ieee80211_start_roc_work(local, sdata, chan,
2865                                        duration, cookie, NULL,
2866                                        IEEE80211_ROC_TYPE_NORMAL);
2867         mutex_unlock(&local->mtx);
2868
2869         return ret;
2870 }
2871
2872 static int ieee80211_cancel_roc(struct ieee80211_local *local,
2873                                 u64 cookie, bool mgmt_tx)
2874 {
2875         struct ieee80211_roc_work *roc, *tmp, *found = NULL;
2876         int ret;
2877
2878         mutex_lock(&local->mtx);
2879         list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
2880                 struct ieee80211_roc_work *dep, *tmp2;
2881
2882                 list_for_each_entry_safe(dep, tmp2, &roc->dependents, list) {
2883                         if (!mgmt_tx && dep->cookie != cookie)
2884                                 continue;
2885                         else if (mgmt_tx && dep->mgmt_tx_cookie != cookie)
2886                                 continue;
2887                         /* found dependent item -- just remove it */
2888                         list_del(&dep->list);
2889                         mutex_unlock(&local->mtx);
2890
2891                         ieee80211_roc_notify_destroy(dep, true);
2892                         return 0;
2893                 }
2894
2895                 if (!mgmt_tx && roc->cookie != cookie)
2896                         continue;
2897                 else if (mgmt_tx && roc->mgmt_tx_cookie != cookie)
2898                         continue;
2899
2900                 found = roc;
2901                 break;
2902         }
2903
2904         if (!found) {
2905                 mutex_unlock(&local->mtx);
2906                 return -ENOENT;
2907         }
2908
2909         /*
2910          * We found the item to cancel, so do that. Note that it
2911          * may have dependents, which we also cancel (and send
2912          * the expired signal for.) Not doing so would be quite
2913          * tricky here, but we may need to fix it later.
2914          */
2915
2916         if (local->ops->remain_on_channel) {
2917                 if (found->started) {
2918                         ret = drv_cancel_remain_on_channel(local);
2919                         if (WARN_ON_ONCE(ret)) {
2920                                 mutex_unlock(&local->mtx);
2921                                 return ret;
2922                         }
2923                 }
2924
2925                 list_del(&found->list);
2926
2927                 if (found->started)
2928                         ieee80211_start_next_roc(local);
2929                 mutex_unlock(&local->mtx);
2930
2931                 ieee80211_roc_notify_destroy(found, true);
2932         } else {
2933                 /* work may be pending so use it all the time */
2934                 found->abort = true;
2935                 ieee80211_queue_delayed_work(&local->hw, &found->work, 0);
2936
2937                 mutex_unlock(&local->mtx);
2938
2939                 /* work will clean up etc */
2940                 flush_delayed_work(&found->work);
2941                 WARN_ON(!found->to_be_freed);
2942                 kfree(found);
2943         }
2944
2945         return 0;
2946 }
2947
2948 static int ieee80211_cancel_remain_on_channel(struct wiphy *wiphy,
2949                                               struct wireless_dev *wdev,
2950                                               u64 cookie)
2951 {
2952         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2953         struct ieee80211_local *local = sdata->local;
2954
2955         return ieee80211_cancel_roc(local, cookie, false);
2956 }
2957
2958 static int ieee80211_start_radar_detection(struct wiphy *wiphy,
2959                                            struct net_device *dev,
2960                                            struct cfg80211_chan_def *chandef,
2961                                            u32 cac_time_ms)
2962 {
2963         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2964         struct ieee80211_local *local = sdata->local;
2965         int err;
2966
2967         mutex_lock(&local->mtx);
2968         if (!list_empty(&local->roc_list) || local->scanning) {
2969                 err = -EBUSY;
2970                 goto out_unlock;
2971         }
2972
2973         /* whatever, but channel contexts should not complain about that one */
2974         sdata->smps_mode = IEEE80211_SMPS_OFF;
2975         sdata->needed_rx_chains = local->rx_chains;
2976
2977         err = ieee80211_vif_use_channel(sdata, chandef,
2978                                         IEEE80211_CHANCTX_SHARED);
2979         if (err)
2980                 goto out_unlock;
2981
2982         ieee80211_queue_delayed_work(&sdata->local->hw,
2983                                      &sdata->dfs_cac_timer_work,
2984                                      msecs_to_jiffies(cac_time_ms));
2985
2986  out_unlock:
2987         mutex_unlock(&local->mtx);
2988         return err;
2989 }
2990
2991 static struct cfg80211_beacon_data *
2992 cfg80211_beacon_dup(struct cfg80211_beacon_data *beacon)
2993 {
2994         struct cfg80211_beacon_data *new_beacon;
2995         u8 *pos;
2996         int len;
2997
2998         len = beacon->head_len + beacon->tail_len + beacon->beacon_ies_len +
2999               beacon->proberesp_ies_len + beacon->assocresp_ies_len +
3000               beacon->probe_resp_len;
3001
3002         new_beacon = kzalloc(sizeof(*new_beacon) + len, GFP_KERNEL);
3003         if (!new_beacon)
3004                 return NULL;
3005
3006         pos = (u8 *)(new_beacon + 1);
3007         if (beacon->head_len) {
3008                 new_beacon->head_len = beacon->head_len;
3009                 new_beacon->head = pos;
3010                 memcpy(pos, beacon->head, beacon->head_len);
3011                 pos += beacon->head_len;
3012         }
3013         if (beacon->tail_len) {
3014                 new_beacon->tail_len = beacon->tail_len;
3015                 new_beacon->tail = pos;
3016                 memcpy(pos, beacon->tail, beacon->tail_len);
3017                 pos += beacon->tail_len;
3018         }
3019         if (beacon->beacon_ies_len) {
3020                 new_beacon->beacon_ies_len = beacon->beacon_ies_len;
3021                 new_beacon->beacon_ies = pos;
3022                 memcpy(pos, beacon->beacon_ies, beacon->beacon_ies_len);
3023                 pos += beacon->beacon_ies_len;
3024         }
3025         if (beacon->proberesp_ies_len) {
3026                 new_beacon->proberesp_ies_len = beacon->proberesp_ies_len;
3027                 new_beacon->proberesp_ies = pos;
3028                 memcpy(pos, beacon->proberesp_ies, beacon->proberesp_ies_len);
3029                 pos += beacon->proberesp_ies_len;
3030         }
3031         if (beacon->assocresp_ies_len) {
3032                 new_beacon->assocresp_ies_len = beacon->assocresp_ies_len;
3033                 new_beacon->assocresp_ies = pos;
3034                 memcpy(pos, beacon->assocresp_ies, beacon->assocresp_ies_len);
3035                 pos += beacon->assocresp_ies_len;
3036         }
3037         if (beacon->probe_resp_len) {
3038                 new_beacon->probe_resp_len = beacon->probe_resp_len;
3039                 beacon->probe_resp = pos;
3040                 memcpy(pos, beacon->probe_resp, beacon->probe_resp_len);
3041                 pos += beacon->probe_resp_len;
3042         }
3043
3044         return new_beacon;
3045 }
3046
3047 void ieee80211_csa_finish(struct ieee80211_vif *vif)
3048 {
3049         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3050
3051         ieee80211_queue_work(&sdata->local->hw,
3052                              &sdata->csa_finalize_work);
3053 }
3054 EXPORT_SYMBOL(ieee80211_csa_finish);
3055
3056 static int ieee80211_set_after_csa_beacon(struct ieee80211_sub_if_data *sdata,
3057                                           u32 *changed)
3058 {
3059         int err;
3060
3061         switch (sdata->vif.type) {
3062         case NL80211_IFTYPE_AP:
3063                 err = ieee80211_assign_beacon(sdata, sdata->u.ap.next_beacon);
3064                 kfree(sdata->u.ap.next_beacon);
3065                 sdata->u.ap.next_beacon = NULL;
3066
3067                 if (err < 0)
3068                         return err;
3069                 *changed |= err;
3070                 break;
3071         case NL80211_IFTYPE_ADHOC:
3072                 err = ieee80211_ibss_finish_csa(sdata);
3073                 if (err < 0)
3074                         return err;
3075                 *changed |= err;
3076                 break;
3077 #ifdef CONFIG_MAC80211_MESH
3078         case NL80211_IFTYPE_MESH_POINT:
3079                 err = ieee80211_mesh_finish_csa(sdata);
3080                 if (err < 0)
3081                         return err;
3082                 *changed |= err;
3083                 break;
3084 #endif
3085         default:
3086                 WARN_ON(1);
3087                 return -EINVAL;
3088         }
3089
3090         return 0;
3091 }
3092
3093 static int __ieee80211_csa_finalize(struct ieee80211_sub_if_data *sdata)
3094 {
3095         struct ieee80211_local *local = sdata->local;
3096         u32 changed = 0;
3097         int err;
3098
3099         sdata_assert_lock(sdata);
3100         lockdep_assert_held(&local->mtx);
3101
3102         sdata->radar_required = sdata->csa_radar_required;
3103         err = ieee80211_vif_change_channel(sdata, &changed);
3104         if (err < 0)
3105                 return err;
3106
3107         if (!local->use_chanctx) {
3108                 local->_oper_chandef = sdata->csa_chandef;
3109                 ieee80211_hw_config(local, 0);
3110         }
3111
3112         sdata->vif.csa_active = false;
3113
3114         err = ieee80211_set_after_csa_beacon(sdata, &changed);
3115         if (err)
3116                 return err;
3117
3118         ieee80211_bss_info_change_notify(sdata, changed);
3119         cfg80211_ch_switch_notify(sdata->dev, &sdata->csa_chandef);
3120
3121         if (!ieee80211_csa_needs_block_tx(local))
3122                 ieee80211_wake_queues_by_reason(&local->hw,
3123                                         IEEE80211_MAX_QUEUE_MAP,
3124                                         IEEE80211_QUEUE_STOP_REASON_CSA);
3125
3126         return 0;
3127 }
3128
3129 static void ieee80211_csa_finalize(struct ieee80211_sub_if_data *sdata)
3130 {
3131         if (__ieee80211_csa_finalize(sdata)) {
3132                 sdata_info(sdata, "failed to finalize CSA, disconnecting\n");
3133                 cfg80211_stop_iface(sdata->local->hw.wiphy, &sdata->wdev,
3134                                     GFP_KERNEL);
3135         }
3136 }
3137
3138 void ieee80211_csa_finalize_work(struct work_struct *work)
3139 {
3140         struct ieee80211_sub_if_data *sdata =
3141                 container_of(work, struct ieee80211_sub_if_data,
3142                              csa_finalize_work);
3143         struct ieee80211_local *local = sdata->local;
3144
3145         sdata_lock(sdata);
3146         mutex_lock(&local->mtx);
3147
3148         /* AP might have been stopped while waiting for the lock. */
3149         if (!sdata->vif.csa_active)
3150                 goto unlock;
3151
3152         if (!ieee80211_sdata_running(sdata))
3153                 goto unlock;
3154
3155         ieee80211_csa_finalize(sdata);
3156
3157 unlock:
3158         mutex_unlock(&local->mtx);
3159         sdata_unlock(sdata);
3160 }
3161
3162 static int ieee80211_set_csa_beacon(struct ieee80211_sub_if_data *sdata,
3163                                     struct cfg80211_csa_settings *params,
3164                                     u32 *changed)
3165 {
3166         int err;
3167
3168         switch (sdata->vif.type) {
3169         case NL80211_IFTYPE_AP:
3170                 sdata->u.ap.next_beacon =
3171                         cfg80211_beacon_dup(&params->beacon_after);
3172                 if (!sdata->u.ap.next_beacon)
3173                         return -ENOMEM;
3174
3175                 /*
3176                  * With a count of 0, we don't have to wait for any
3177                  * TBTT before switching, so complete the CSA
3178                  * immediately.  In theory, with a count == 1 we
3179                  * should delay the switch until just before the next
3180                  * TBTT, but that would complicate things so we switch
3181                  * immediately too.  If we would delay the switch
3182                  * until the next TBTT, we would have to set the probe
3183                  * response here.
3184                  *
3185                  * TODO: A channel switch with count <= 1 without
3186                  * sending a CSA action frame is kind of useless,
3187                  * because the clients won't know we're changing
3188                  * channels.  The action frame must be implemented
3189                  * either here or in the userspace.
3190                  */
3191                 if (params->count <= 1)
3192                         break;
3193
3194                 sdata->csa_counter_offset_beacon =
3195                         params->counter_offset_beacon;
3196                 sdata->csa_counter_offset_presp = params->counter_offset_presp;
3197                 err = ieee80211_assign_beacon(sdata, &params->beacon_csa);
3198                 if (err < 0) {
3199                         kfree(sdata->u.ap.next_beacon);
3200                         return err;
3201                 }
3202                 *changed |= err;
3203
3204                 break;
3205         case NL80211_IFTYPE_ADHOC:
3206                 if (!sdata->vif.bss_conf.ibss_joined)
3207                         return -EINVAL;
3208
3209                 if (params->chandef.width != sdata->u.ibss.chandef.width)
3210                         return -EINVAL;
3211
3212                 switch (params->chandef.width) {
3213                 case NL80211_CHAN_WIDTH_40:
3214                         if (cfg80211_get_chandef_type(&params->chandef) !=
3215                             cfg80211_get_chandef_type(&sdata->u.ibss.chandef))
3216                                 return -EINVAL;
3217                 case NL80211_CHAN_WIDTH_5:
3218                 case NL80211_CHAN_WIDTH_10:
3219                 case NL80211_CHAN_WIDTH_20_NOHT:
3220                 case NL80211_CHAN_WIDTH_20:
3221                         break;
3222                 default:
3223                         return -EINVAL;
3224                 }
3225
3226                 /* changes into another band are not supported */
3227                 if (sdata->u.ibss.chandef.chan->band !=
3228                     params->chandef.chan->band)
3229                         return -EINVAL;
3230
3231                 /* see comments in the NL80211_IFTYPE_AP block */
3232                 if (params->count > 1) {
3233                         err = ieee80211_ibss_csa_beacon(sdata, params);
3234                         if (err < 0)
3235                                 return err;
3236                         *changed |= err;
3237                 }
3238
3239                 ieee80211_send_action_csa(sdata, params);
3240
3241                 break;
3242 #ifdef CONFIG_MAC80211_MESH
3243         case NL80211_IFTYPE_MESH_POINT: {
3244                 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3245
3246                 if (params->chandef.width != sdata->vif.bss_conf.chandef.width)
3247                         return -EINVAL;
3248
3249                 /* changes into another band are not supported */
3250                 if (sdata->vif.bss_conf.chandef.chan->band !=
3251                     params->chandef.chan->band)
3252                         return -EINVAL;
3253
3254                 if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_NONE) {
3255                         ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_INIT;
3256                         if (!ifmsh->pre_value)
3257                                 ifmsh->pre_value = 1;
3258                         else
3259                                 ifmsh->pre_value++;
3260                 }
3261
3262                 /* see comments in the NL80211_IFTYPE_AP block */
3263                 if (params->count > 1) {
3264                         err = ieee80211_mesh_csa_beacon(sdata, params);
3265                         if (err < 0) {
3266                                 ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
3267                                 return err;
3268                         }
3269                         *changed |= err;
3270                 }
3271
3272                 if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_INIT)
3273                         ieee80211_send_action_csa(sdata, params);
3274
3275                 break;
3276                 }
3277 #endif
3278         default:
3279                 return -EOPNOTSUPP;
3280         }
3281
3282         return 0;
3283 }
3284
3285 static int
3286 __ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
3287                            struct cfg80211_csa_settings *params)
3288 {
3289         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3290         struct ieee80211_local *local = sdata->local;
3291         struct ieee80211_chanctx_conf *conf;
3292         struct ieee80211_chanctx *chanctx;
3293         int err, num_chanctx, changed = 0;
3294
3295         sdata_assert_lock(sdata);
3296         lockdep_assert_held(&local->mtx);
3297
3298         if (!list_empty(&local->roc_list) || local->scanning)
3299                 return -EBUSY;
3300
3301         if (sdata->wdev.cac_started)
3302                 return -EBUSY;
3303
3304         if (cfg80211_chandef_identical(&params->chandef,
3305                                        &sdata->vif.bss_conf.chandef))
3306                 return -EINVAL;
3307
3308         mutex_lock(&local->chanctx_mtx);
3309         conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
3310                                          lockdep_is_held(&local->chanctx_mtx));
3311         if (!conf) {
3312                 mutex_unlock(&local->chanctx_mtx);
3313                 return -EBUSY;
3314         }
3315
3316         /* don't handle for multi-VIF cases */
3317         chanctx = container_of(conf, struct ieee80211_chanctx, conf);
3318         if (ieee80211_chanctx_refcount(local, chanctx) > 1) {
3319                 mutex_unlock(&local->chanctx_mtx);
3320                 return -EBUSY;
3321         }
3322         num_chanctx = 0;
3323         list_for_each_entry_rcu(chanctx, &local->chanctx_list, list)
3324                 num_chanctx++;
3325         mutex_unlock(&local->chanctx_mtx);
3326
3327         if (num_chanctx > 1)
3328                 return -EBUSY;
3329
3330         /* don't allow another channel switch if one is already active. */
3331         if (sdata->vif.csa_active)
3332                 return -EBUSY;
3333
3334         err = ieee80211_set_csa_beacon(sdata, params, &changed);
3335         if (err)
3336                 return err;
3337
3338         sdata->csa_radar_required = params->radar_required;
3339         sdata->csa_chandef = params->chandef;
3340         sdata->csa_block_tx = params->block_tx;
3341         sdata->vif.csa_active = true;
3342
3343         if (sdata->csa_block_tx)
3344                 ieee80211_stop_queues_by_reason(&local->hw,
3345                                         IEEE80211_MAX_QUEUE_MAP,
3346                                         IEEE80211_QUEUE_STOP_REASON_CSA);
3347
3348         if (changed) {
3349                 ieee80211_bss_info_change_notify(sdata, changed);
3350                 drv_channel_switch_beacon(sdata, &params->chandef);
3351         } else {
3352                 /* if the beacon didn't change, we can finalize immediately */
3353                 ieee80211_csa_finalize(sdata);
3354         }
3355
3356         return 0;
3357 }
3358
3359 int ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
3360                              struct cfg80211_csa_settings *params)
3361 {
3362         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3363         struct ieee80211_local *local = sdata->local;
3364         int err;
3365
3366         mutex_lock(&local->mtx);
3367         err = __ieee80211_channel_switch(wiphy, dev, params);
3368         mutex_unlock(&local->mtx);
3369
3370         return err;
3371 }
3372
3373 static int ieee80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
3374                              struct cfg80211_mgmt_tx_params *params,
3375                              u64 *cookie)
3376 {
3377         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
3378         struct ieee80211_local *local = sdata->local;
3379         struct sk_buff *skb;
3380         struct sta_info *sta;
3381         const struct ieee80211_mgmt *mgmt = (void *)params->buf;
3382         bool need_offchan = false;
3383         u32 flags;
3384         int ret;
3385
3386         if (params->dont_wait_for_ack)
3387                 flags = IEEE80211_TX_CTL_NO_ACK;
3388         else
3389                 flags = IEEE80211_TX_INTFL_NL80211_FRAME_TX |
3390                         IEEE80211_TX_CTL_REQ_TX_STATUS;
3391
3392         if (params->no_cck)
3393                 flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
3394
3395         switch (sdata->vif.type) {
3396         case NL80211_IFTYPE_ADHOC:
3397                 if (!sdata->vif.bss_conf.ibss_joined)
3398                         need_offchan = true;
3399                 /* fall through */
3400 #ifdef CONFIG_MAC80211_MESH
3401         case NL80211_IFTYPE_MESH_POINT:
3402                 if (ieee80211_vif_is_mesh(&sdata->vif) &&
3403                     !sdata->u.mesh.mesh_id_len)
3404                         need_offchan = true;
3405                 /* fall through */
3406 #endif
3407         case NL80211_IFTYPE_AP:
3408         case NL80211_IFTYPE_AP_VLAN:
3409         case NL80211_IFTYPE_P2P_GO:
3410                 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3411                     !ieee80211_vif_is_mesh(&sdata->vif) &&
3412                     !rcu_access_pointer(sdata->bss->beacon))
3413                         need_offchan = true;
3414                 if (!ieee80211_is_action(mgmt->frame_control) ||
3415                     mgmt->u.action.category == WLAN_CATEGORY_PUBLIC ||
3416                     mgmt->u.action.category == WLAN_CATEGORY_SELF_PROTECTED ||
3417                     mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT)
3418                         break;
3419                 rcu_read_lock();
3420                 sta = sta_info_get(sdata, mgmt->da);
3421                 rcu_read_unlock();
3422                 if (!sta)
3423                         return -ENOLINK;
3424                 break;
3425         case NL80211_IFTYPE_STATION:
3426         case NL80211_IFTYPE_P2P_CLIENT:
3427                 if (!sdata->u.mgd.associated)
3428                         need_offchan = true;
3429                 break;
3430         case NL80211_IFTYPE_P2P_DEVICE:
3431                 need_offchan = true;
3432                 break;
3433         default:
3434                 return -EOPNOTSUPP;
3435         }
3436
3437         /* configurations requiring offchan cannot work if no channel has been
3438          * specified
3439          */
3440         if (need_offchan && !params->chan)
3441                 return -EINVAL;
3442
3443         mutex_lock(&local->mtx);
3444
3445         /* Check if the operating channel is the requested channel */
3446         if (!need_offchan) {
3447                 struct ieee80211_chanctx_conf *chanctx_conf;
3448
3449                 rcu_read_lock();
3450                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3451
3452                 if (chanctx_conf) {
3453                         need_offchan = params->chan &&
3454                                        (params->chan !=
3455                                         chanctx_conf->def.chan);
3456                 } else if (!params->chan) {
3457                         ret = -EINVAL;
3458                         rcu_read_unlock();
3459                         goto out_unlock;
3460                 } else {
3461                         need_offchan = true;
3462                 }
3463                 rcu_read_unlock();
3464         }
3465
3466         if (need_offchan && !params->offchan) {
3467                 ret = -EBUSY;
3468                 goto out_unlock;
3469         }
3470
3471         skb = dev_alloc_skb(local->hw.extra_tx_headroom + params->len);
3472         if (!skb) {
3473                 ret = -ENOMEM;
3474                 goto out_unlock;
3475         }
3476         skb_reserve(skb, local->hw.extra_tx_headroom);
3477
3478         memcpy(skb_put(skb, params->len), params->buf, params->len);
3479
3480         IEEE80211_SKB_CB(skb)->flags = flags;
3481
3482         skb->dev = sdata->dev;
3483
3484         if (!need_offchan) {
3485                 *cookie = (unsigned long) skb;
3486                 ieee80211_tx_skb(sdata, skb);
3487                 ret = 0;
3488                 goto out_unlock;
3489         }
3490
3491         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_TX_OFFCHAN |
3492                                         IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
3493         if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
3494                 IEEE80211_SKB_CB(skb)->hw_queue =
3495                         local->hw.offchannel_tx_hw_queue;
3496
3497         /* This will handle all kinds of coalescing and immediate TX */
3498         ret = ieee80211_start_roc_work(local, sdata, params->chan,
3499                                        params->wait, cookie, skb,
3500                                        IEEE80211_ROC_TYPE_MGMT_TX);
3501         if (ret)
3502                 kfree_skb(skb);
3503  out_unlock:
3504         mutex_unlock(&local->mtx);
3505         return ret;
3506 }
3507
3508 static int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
3509                                          struct wireless_dev *wdev,
3510                                          u64 cookie)
3511 {
3512         struct ieee80211_local *local = wiphy_priv(wiphy);
3513
3514         return ieee80211_cancel_roc(local, cookie, true);
3515 }
3516
3517 static void ieee80211_mgmt_frame_register(struct wiphy *wiphy,
3518                                           struct wireless_dev *wdev,
3519                                           u16 frame_type, bool reg)
3520 {
3521         struct ieee80211_local *local = wiphy_priv(wiphy);
3522
3523         switch (frame_type) {
3524         case IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_PROBE_REQ:
3525                 if (reg)
3526                         local->probe_req_reg++;
3527                 else
3528                         local->probe_req_reg--;
3529
3530                 if (!local->open_count)
3531                         break;
3532
3533                 ieee80211_queue_work(&local->hw, &local->reconfig_filter);
3534                 break;
3535         default:
3536                 break;
3537         }
3538 }
3539
3540 static int ieee80211_set_antenna(struct wiphy *wiphy, u32 tx_ant, u32 rx_ant)
3541 {
3542         struct ieee80211_local *local = wiphy_priv(wiphy);
3543
3544         if (local->started)
3545                 return -EOPNOTSUPP;
3546
3547         return drv_set_antenna(local, tx_ant, rx_ant);
3548 }
3549
3550 static int ieee80211_get_antenna(struct wiphy *wiphy, u32 *tx_ant, u32 *rx_ant)
3551 {
3552         struct ieee80211_local *local = wiphy_priv(wiphy);
3553
3554         return drv_get_antenna(local, tx_ant, rx_ant);
3555 }
3556
3557 static int ieee80211_set_ringparam(struct wiphy *wiphy, u32 tx, u32 rx)
3558 {
3559         struct ieee80211_local *local = wiphy_priv(wiphy);
3560
3561         return drv_set_ringparam(local, tx, rx);
3562 }
3563
3564 static void ieee80211_get_ringparam(struct wiphy *wiphy,
3565                                     u32 *tx, u32 *tx_max, u32 *rx, u32 *rx_max)
3566 {
3567         struct ieee80211_local *local = wiphy_priv(wiphy);
3568
3569         drv_get_ringparam(local, tx, tx_max, rx, rx_max);
3570 }
3571
3572 static int ieee80211_set_rekey_data(struct wiphy *wiphy,
3573                                     struct net_device *dev,
3574                                     struct cfg80211_gtk_rekey_data *data)
3575 {
3576         struct ieee80211_local *local = wiphy_priv(wiphy);
3577         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3578
3579         if (!local->ops->set_rekey_data)
3580                 return -EOPNOTSUPP;
3581
3582         drv_set_rekey_data(local, sdata, data);
3583
3584         return 0;
3585 }
3586
3587 static int ieee80211_probe_client(struct wiphy *wiphy, struct net_device *dev,
3588                                   const u8 *peer, u64 *cookie)
3589 {
3590         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3591         struct ieee80211_local *local = sdata->local;
3592         struct ieee80211_qos_hdr *nullfunc;
3593         struct sk_buff *skb;
3594         int size = sizeof(*nullfunc);
3595         __le16 fc;
3596         bool qos;
3597         struct ieee80211_tx_info *info;
3598         struct sta_info *sta;
3599         struct ieee80211_chanctx_conf *chanctx_conf;
3600         enum ieee80211_band band;
3601
3602         rcu_read_lock();
3603         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3604         if (WARN_ON(!chanctx_conf)) {
3605                 rcu_read_unlock();
3606                 return -EINVAL;
3607         }
3608         band = chanctx_conf->def.chan->band;
3609         sta = sta_info_get_bss(sdata, peer);
3610         if (sta) {
3611                 qos = test_sta_flag(sta, WLAN_STA_WME);
3612         } else {
3613                 rcu_read_unlock();
3614                 return -ENOLINK;
3615         }
3616
3617         if (qos) {
3618                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
3619                                  IEEE80211_STYPE_QOS_NULLFUNC |
3620                                  IEEE80211_FCTL_FROMDS);
3621         } else {
3622                 size -= 2;
3623                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
3624                                  IEEE80211_STYPE_NULLFUNC |
3625                                  IEEE80211_FCTL_FROMDS);
3626         }
3627
3628         skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
3629         if (!skb) {
3630                 rcu_read_unlock();
3631                 return -ENOMEM;
3632         }
3633
3634         skb->dev = dev;
3635
3636         skb_reserve(skb, local->hw.extra_tx_headroom);
3637
3638         nullfunc = (void *) skb_put(skb, size);
3639         nullfunc->frame_control = fc;
3640         nullfunc->duration_id = 0;
3641         memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
3642         memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
3643         memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
3644         nullfunc->seq_ctrl = 0;
3645
3646         info = IEEE80211_SKB_CB(skb);
3647
3648         info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
3649                        IEEE80211_TX_INTFL_NL80211_FRAME_TX;
3650
3651         skb_set_queue_mapping(skb, IEEE80211_AC_VO);
3652         skb->priority = 7;
3653         if (qos)
3654                 nullfunc->qos_ctrl = cpu_to_le16(7);
3655
3656         local_bh_disable();
3657         ieee80211_xmit(sdata, skb, band);
3658         local_bh_enable();
3659         rcu_read_unlock();
3660
3661         *cookie = (unsigned long) skb;
3662         return 0;
3663 }
3664
3665 static int ieee80211_cfg_get_channel(struct wiphy *wiphy,
3666                                      struct wireless_dev *wdev,
3667                                      struct cfg80211_chan_def *chandef)
3668 {
3669         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
3670         struct ieee80211_local *local = wiphy_priv(wiphy);
3671         struct ieee80211_chanctx_conf *chanctx_conf;
3672         int ret = -ENODATA;
3673
3674         rcu_read_lock();
3675         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3676         if (chanctx_conf) {
3677                 *chandef = chanctx_conf->def;
3678                 ret = 0;
3679         } else if (local->open_count > 0 &&
3680                    local->open_count == local->monitors &&
3681                    sdata->vif.type == NL80211_IFTYPE_MONITOR) {
3682                 if (local->use_chanctx)
3683                         *chandef = local->monitor_chandef;
3684                 else
3685                         *chandef = local->_oper_chandef;
3686                 ret = 0;
3687         }
3688         rcu_read_unlock();
3689
3690         return ret;
3691 }
3692
3693 #ifdef CONFIG_PM
3694 static void ieee80211_set_wakeup(struct wiphy *wiphy, bool enabled)
3695 {
3696         drv_set_wakeup(wiphy_priv(wiphy), enabled);
3697 }
3698 #endif
3699
3700 static int ieee80211_set_qos_map(struct wiphy *wiphy,
3701                                  struct net_device *dev,
3702                                  struct cfg80211_qos_map *qos_map)
3703 {
3704         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3705         struct mac80211_qos_map *new_qos_map, *old_qos_map;
3706
3707         if (qos_map) {
3708                 new_qos_map = kzalloc(sizeof(*new_qos_map), GFP_KERNEL);
3709                 if (!new_qos_map)
3710                         return -ENOMEM;
3711                 memcpy(&new_qos_map->qos_map, qos_map, sizeof(*qos_map));
3712         } else {
3713                 /* A NULL qos_map was passed to disable QoS mapping */
3714                 new_qos_map = NULL;
3715         }
3716
3717         old_qos_map = sdata_dereference(sdata->qos_map, sdata);
3718         rcu_assign_pointer(sdata->qos_map, new_qos_map);
3719         if (old_qos_map)
3720                 kfree_rcu(old_qos_map, rcu_head);
3721
3722         return 0;
3723 }
3724
3725 static int ieee80211_set_ap_chanwidth(struct wiphy *wiphy,
3726                                       struct net_device *dev,
3727                                       struct cfg80211_chan_def *chandef)
3728 {
3729         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3730         int ret;
3731         u32 changed = 0;
3732
3733         ret = ieee80211_vif_change_bandwidth(sdata, chandef, &changed);
3734         if (ret == 0)
3735                 ieee80211_bss_info_change_notify(sdata, changed);
3736
3737         return ret;
3738 }
3739
3740 const struct cfg80211_ops mac80211_config_ops = {
3741         .add_virtual_intf = ieee80211_add_iface,
3742         .del_virtual_intf = ieee80211_del_iface,
3743         .change_virtual_intf = ieee80211_change_iface,
3744         .start_p2p_device = ieee80211_start_p2p_device,
3745         .stop_p2p_device = ieee80211_stop_p2p_device,
3746         .add_key = ieee80211_add_key,
3747         .del_key = ieee80211_del_key,
3748         .get_key = ieee80211_get_key,
3749         .set_default_key = ieee80211_config_default_key,
3750         .set_default_mgmt_key = ieee80211_config_default_mgmt_key,
3751         .start_ap = ieee80211_start_ap,
3752         .change_beacon = ieee80211_change_beacon,
3753         .stop_ap = ieee80211_stop_ap,
3754         .add_station = ieee80211_add_station,
3755         .del_station = ieee80211_del_station,
3756         .change_station = ieee80211_change_station,
3757         .get_station = ieee80211_get_station,
3758         .dump_station = ieee80211_dump_station,
3759         .dump_survey = ieee80211_dump_survey,
3760 #ifdef CONFIG_MAC80211_MESH
3761         .add_mpath = ieee80211_add_mpath,
3762         .del_mpath = ieee80211_del_mpath,
3763         .change_mpath = ieee80211_change_mpath,
3764         .get_mpath = ieee80211_get_mpath,
3765         .dump_mpath = ieee80211_dump_mpath,
3766         .update_mesh_config = ieee80211_update_mesh_config,
3767         .get_mesh_config = ieee80211_get_mesh_config,
3768         .join_mesh = ieee80211_join_mesh,
3769         .leave_mesh = ieee80211_leave_mesh,
3770 #endif
3771         .change_bss = ieee80211_change_bss,
3772         .set_txq_params = ieee80211_set_txq_params,
3773         .set_monitor_channel = ieee80211_set_monitor_channel,
3774         .suspend = ieee80211_suspend,
3775         .resume = ieee80211_resume,
3776         .scan = ieee80211_scan,
3777         .sched_scan_start = ieee80211_sched_scan_start,
3778         .sched_scan_stop = ieee80211_sched_scan_stop,
3779         .auth = ieee80211_auth,
3780         .assoc = ieee80211_assoc,
3781         .deauth = ieee80211_deauth,
3782         .disassoc = ieee80211_disassoc,
3783         .join_ibss = ieee80211_join_ibss,
3784         .leave_ibss = ieee80211_leave_ibss,
3785         .set_mcast_rate = ieee80211_set_mcast_rate,
3786         .set_wiphy_params = ieee80211_set_wiphy_params,
3787         .set_tx_power = ieee80211_set_tx_power,
3788         .get_tx_power = ieee80211_get_tx_power,
3789         .set_wds_peer = ieee80211_set_wds_peer,
3790         .rfkill_poll = ieee80211_rfkill_poll,
3791         CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
3792         CFG80211_TESTMODE_DUMP(ieee80211_testmode_dump)
3793         .set_power_mgmt = ieee80211_set_power_mgmt,
3794         .set_bitrate_mask = ieee80211_set_bitrate_mask,
3795         .remain_on_channel = ieee80211_remain_on_channel,
3796         .cancel_remain_on_channel = ieee80211_cancel_remain_on_channel,
3797         .mgmt_tx = ieee80211_mgmt_tx,
3798         .mgmt_tx_cancel_wait = ieee80211_mgmt_tx_cancel_wait,
3799         .set_cqm_rssi_config = ieee80211_set_cqm_rssi_config,
3800         .mgmt_frame_register = ieee80211_mgmt_frame_register,
3801         .set_antenna = ieee80211_set_antenna,
3802         .get_antenna = ieee80211_get_antenna,
3803         .set_ringparam = ieee80211_set_ringparam,
3804         .get_ringparam = ieee80211_get_ringparam,
3805         .set_rekey_data = ieee80211_set_rekey_data,
3806         .tdls_oper = ieee80211_tdls_oper,
3807         .tdls_mgmt = ieee80211_tdls_mgmt,
3808         .probe_client = ieee80211_probe_client,
3809         .set_noack_map = ieee80211_set_noack_map,
3810 #ifdef CONFIG_PM
3811         .set_wakeup = ieee80211_set_wakeup,
3812 #endif
3813         .get_et_sset_count = ieee80211_get_et_sset_count,
3814         .get_et_stats = ieee80211_get_et_stats,
3815         .get_et_strings = ieee80211_get_et_strings,
3816         .get_channel = ieee80211_cfg_get_channel,
3817         .start_radar_detection = ieee80211_start_radar_detection,
3818         .channel_switch = ieee80211_channel_switch,
3819         .set_qos_map = ieee80211_set_qos_map,
3820         .set_ap_chanwidth = ieee80211_set_ap_chanwidth,
3821 };