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