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