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