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