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