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