Revert "mac80211_hwsim: remove P2P_DEVICE support"
[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;
398 sband = sta->local->hw.wiphy->bands[
399 ieee80211_get_sdata_band(sta->sdata)];
400 rinfo->legacy = sband->bitrates[rate->idx].bitrate;
401 }
6b62bf32
TP
402 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
403 rinfo->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
8bc83c24
JB
404 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
405 rinfo->flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
406 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
407 rinfo->flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
6b62bf32
TP
408 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
409 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
6b62bf32
TP
410}
411
003e676a
S
412void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
413{
414 rinfo->flags = 0;
415
416 if (sta->last_rx_rate_flag & RX_FLAG_HT) {
417 rinfo->flags |= RATE_INFO_FLAGS_MCS;
418 rinfo->mcs = sta->last_rx_rate_idx;
419 } else if (sta->last_rx_rate_flag & RX_FLAG_VHT) {
420 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
421 rinfo->nss = sta->last_rx_rate_vht_nss;
422 rinfo->mcs = sta->last_rx_rate_idx;
423 } else {
424 struct ieee80211_supported_band *sband;
425
426 sband = sta->local->hw.wiphy->bands[
427 ieee80211_get_sdata_band(sta->sdata)];
428 rinfo->legacy =
429 sband->bitrates[sta->last_rx_rate_idx].bitrate;
430 }
431
432 if (sta->last_rx_rate_flag & RX_FLAG_40MHZ)
433 rinfo->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
434 if (sta->last_rx_rate_flag & RX_FLAG_SHORT_GI)
435 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
436 if (sta->last_rx_rate_flag & RX_FLAG_80MHZ)
437 rinfo->flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
438 if (sta->last_rx_rate_flag & RX_FLAG_80P80MHZ)
439 rinfo->flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
440 if (sta->last_rx_rate_flag & RX_FLAG_160MHZ)
441 rinfo->flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
442}
443
c5dd9c2b
LCC
444static void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
445{
d0709a65 446 struct ieee80211_sub_if_data *sdata = sta->sdata;
66572cfc 447 struct ieee80211_local *local = sdata->local;
ebe27c91 448 struct timespec uptime;
560d2682 449 u64 packets = 0;
ef0621e8 450 int i, ac;
c5dd9c2b 451
f5ea9120
JB
452 sinfo->generation = sdata->local->sta_generation;
453
c5dd9c2b 454 sinfo->filled = STATION_INFO_INACTIVE_TIME |
560d2682
JB
455 STATION_INFO_RX_BYTES64 |
456 STATION_INFO_TX_BYTES64 |
98c8a60a
JM
457 STATION_INFO_RX_PACKETS |
458 STATION_INFO_TX_PACKETS |
b206b4ef
BR
459 STATION_INFO_TX_RETRIES |
460 STATION_INFO_TX_FAILED |
5a5c731a 461 STATION_INFO_TX_BITRATE |
3af6334c 462 STATION_INFO_RX_BITRATE |
f4263c98 463 STATION_INFO_RX_DROP_MISC |
ebe27c91 464 STATION_INFO_BSS_PARAM |
7a724767 465 STATION_INFO_CONNECTED_TIME |
a85e1d55
PS
466 STATION_INFO_STA_FLAGS |
467 STATION_INFO_BEACON_LOSS_COUNT;
ebe27c91
MSS
468
469 do_posix_clock_monotonic_gettime(&uptime);
470 sinfo->connected_time = uptime.tv_sec - sta->last_connected;
c5dd9c2b
LCC
471
472 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
560d2682
JB
473 sinfo->tx_bytes = 0;
474 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
475 sinfo->tx_bytes += sta->tx_bytes[ac];
476 packets += sta->tx_packets[ac];
477 }
478 sinfo->tx_packets = packets;
c5dd9c2b 479 sinfo->rx_bytes = sta->rx_bytes;
98c8a60a 480 sinfo->rx_packets = sta->rx_packets;
b206b4ef
BR
481 sinfo->tx_retries = sta->tx_retry_count;
482 sinfo->tx_failed = sta->tx_retry_failed;
5a5c731a 483 sinfo->rx_dropped_misc = sta->rx_dropped;
a85e1d55 484 sinfo->beacon_loss_count = sta->beacon_loss_count;
c5dd9c2b 485
19deffbe
JL
486 if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
487 (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
541a45a1 488 sinfo->filled |= STATION_INFO_SIGNAL | STATION_INFO_SIGNAL_AVG;
66572cfc
VG
489 if (!local->ops->get_rssi ||
490 drv_get_rssi(local, sdata, &sta->sta, &sinfo->signal))
491 sinfo->signal = (s8)sta->last_signal;
541a45a1 492 sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
420e7fab 493 }
ef0621e8
FF
494 if (sta->chains) {
495 sinfo->filled |= STATION_INFO_CHAIN_SIGNAL |
496 STATION_INFO_CHAIN_SIGNAL_AVG;
497
498 sinfo->chains = sta->chains;
499 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
500 sinfo->chain_signal[i] = sta->chain_signal_last[i];
501 sinfo->chain_signal_avg[i] =
502 (s8) -ewma_read(&sta->chain_signal_avg[i]);
503 }
504 }
420e7fab 505
6b62bf32 506 sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
003e676a 507 sta_set_rate_info_rx(sta, &sinfo->rxrate);
420e7fab 508
902acc78 509 if (ieee80211_vif_is_mesh(&sdata->vif)) {
c5dd9c2b 510#ifdef CONFIG_MAC80211_MESH
c5dd9c2b
LCC
511 sinfo->filled |= STATION_INFO_LLID |
512 STATION_INFO_PLID |
3f52b7e3
MP
513 STATION_INFO_PLINK_STATE |
514 STATION_INFO_LOCAL_PM |
515 STATION_INFO_PEER_PM |
516 STATION_INFO_NONPEER_PM;
c5dd9c2b
LCC
517
518 sinfo->llid = le16_to_cpu(sta->llid);
519 sinfo->plid = le16_to_cpu(sta->plid);
520 sinfo->plink_state = sta->plink_state;
d299a1f2
JC
521 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
522 sinfo->filled |= STATION_INFO_T_OFFSET;
523 sinfo->t_offset = sta->t_offset;
524 }
3f52b7e3
MP
525 sinfo->local_pm = sta->local_pm;
526 sinfo->peer_pm = sta->peer_pm;
527 sinfo->nonpeer_pm = sta->nonpeer_pm;
c5dd9c2b 528#endif
902acc78 529 }
f4263c98
PS
530
531 sinfo->bss_param.flags = 0;
532 if (sdata->vif.bss_conf.use_cts_prot)
533 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
534 if (sdata->vif.bss_conf.use_short_preamble)
535 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
536 if (sdata->vif.bss_conf.use_short_slot)
537 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
538 sinfo->bss_param.dtim_period = sdata->local->hw.conf.ps_dtim_period;
539 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
7a724767
HS
540
541 sinfo->sta_flags.set = 0;
542 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
543 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
544 BIT(NL80211_STA_FLAG_WME) |
545 BIT(NL80211_STA_FLAG_MFP) |
e4121562 546 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
d582cffb 547 BIT(NL80211_STA_FLAG_ASSOCIATED) |
e4121562 548 BIT(NL80211_STA_FLAG_TDLS_PEER);
7a724767
HS
549 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
550 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
551 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
552 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
553 if (test_sta_flag(sta, WLAN_STA_WME))
554 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
555 if (test_sta_flag(sta, WLAN_STA_MFP))
556 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
557 if (test_sta_flag(sta, WLAN_STA_AUTH))
558 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
d582cffb
JB
559 if (test_sta_flag(sta, WLAN_STA_ASSOC))
560 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
e4121562
HS
561 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
562 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
c5dd9c2b
LCC
563}
564
b1ab7925
BG
565static const char ieee80211_gstrings_sta_stats[][ETH_GSTRING_LEN] = {
566 "rx_packets", "rx_bytes", "wep_weak_iv_count",
567 "rx_duplicates", "rx_fragments", "rx_dropped",
568 "tx_packets", "tx_bytes", "tx_fragments",
569 "tx_filtered", "tx_retry_failed", "tx_retries",
3073a7c2
BG
570 "beacon_loss", "sta_state", "txrate", "rxrate", "signal",
571 "channel", "noise", "ch_time", "ch_time_busy",
572 "ch_time_ext_busy", "ch_time_rx", "ch_time_tx"
b1ab7925
BG
573};
574#define STA_STATS_LEN ARRAY_SIZE(ieee80211_gstrings_sta_stats)
575
576static int ieee80211_get_et_sset_count(struct wiphy *wiphy,
577 struct net_device *dev,
578 int sset)
579{
e352114f
BG
580 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
581 int rv = 0;
582
b1ab7925 583 if (sset == ETH_SS_STATS)
e352114f
BG
584 rv += STA_STATS_LEN;
585
586 rv += drv_get_et_sset_count(sdata, sset);
b1ab7925 587
e352114f
BG
588 if (rv == 0)
589 return -EOPNOTSUPP;
590 return rv;
b1ab7925
BG
591}
592
593static void ieee80211_get_et_stats(struct wiphy *wiphy,
594 struct net_device *dev,
595 struct ethtool_stats *stats,
596 u64 *data)
597{
598 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
55de908a
JB
599 struct ieee80211_chanctx_conf *chanctx_conf;
600 struct ieee80211_channel *channel;
b1ab7925
BG
601 struct sta_info *sta;
602 struct ieee80211_local *local = sdata->local;
3073a7c2
BG
603 struct station_info sinfo;
604 struct survey_info survey;
605 int i, q;
606#define STA_STATS_SURVEY_LEN 7
b1ab7925
BG
607
608 memset(data, 0, sizeof(u64) * STA_STATS_LEN);
609
610#define ADD_STA_STATS(sta) \
611 do { \
612 data[i++] += sta->rx_packets; \
613 data[i++] += sta->rx_bytes; \
614 data[i++] += sta->wep_weak_iv_count; \
615 data[i++] += sta->num_duplicates; \
616 data[i++] += sta->rx_fragments; \
617 data[i++] += sta->rx_dropped; \
618 \
560d2682
JB
619 data[i++] += sinfo.tx_packets; \
620 data[i++] += sinfo.tx_bytes; \
b1ab7925
BG
621 data[i++] += sta->tx_fragments; \
622 data[i++] += sta->tx_filtered_count; \
623 data[i++] += sta->tx_retry_failed; \
624 data[i++] += sta->tx_retry_count; \
625 data[i++] += sta->beacon_loss_count; \
626 } while (0)
627
628 /* For Managed stations, find the single station based on BSSID
629 * and use that. For interface types, iterate through all available
630 * stations and add stats for any station that is assigned to this
631 * network device.
632 */
633
66572cfc 634 mutex_lock(&local->sta_mtx);
b1ab7925
BG
635
636 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
637 sta = sta_info_get_bss(sdata, sdata->u.mgd.bssid);
3073a7c2
BG
638
639 if (!(sta && !WARN_ON(sta->sdata->dev != dev)))
640 goto do_survey;
641
560d2682
JB
642 sinfo.filled = 0;
643 sta_set_sinfo(sta, &sinfo);
644
3073a7c2
BG
645 i = 0;
646 ADD_STA_STATS(sta);
647
648 data[i++] = sta->sta_state;
649
3073a7c2 650
5204267d 651 if (sinfo.filled & STATION_INFO_TX_BITRATE)
3073a7c2
BG
652 data[i] = 100000 *
653 cfg80211_calculate_bitrate(&sinfo.txrate);
654 i++;
5204267d 655 if (sinfo.filled & STATION_INFO_RX_BITRATE)
3073a7c2
BG
656 data[i] = 100000 *
657 cfg80211_calculate_bitrate(&sinfo.rxrate);
658 i++;
659
5204267d 660 if (sinfo.filled & STATION_INFO_SIGNAL_AVG)
3073a7c2
BG
661 data[i] = (u8)sinfo.signal_avg;
662 i++;
b1ab7925 663 } else {
66572cfc 664 list_for_each_entry(sta, &local->sta_list, list) {
b1ab7925
BG
665 /* Make sure this station belongs to the proper dev */
666 if (sta->sdata->dev != dev)
667 continue;
668
669 i = 0;
670 ADD_STA_STATS(sta);
b1ab7925
BG
671 }
672 }
673
3073a7c2
BG
674do_survey:
675 i = STA_STATS_LEN - STA_STATS_SURVEY_LEN;
676 /* Get survey stats for current channel */
55de908a 677 survey.filled = 0;
3073a7c2 678
55de908a
JB
679 rcu_read_lock();
680 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
681 if (chanctx_conf)
4bf88530 682 channel = chanctx_conf->def.chan;
55de908a
JB
683 else
684 channel = NULL;
685 rcu_read_unlock();
686
687 if (channel) {
688 q = 0;
689 do {
690 survey.filled = 0;
691 if (drv_get_survey(local, q, &survey) != 0) {
692 survey.filled = 0;
693 break;
694 }
695 q++;
696 } while (channel != survey.channel);
3073a7c2
BG
697 }
698
699 if (survey.filled)
700 data[i++] = survey.channel->center_freq;
701 else
702 data[i++] = 0;
703 if (survey.filled & SURVEY_INFO_NOISE_DBM)
704 data[i++] = (u8)survey.noise;
705 else
706 data[i++] = -1LL;
707 if (survey.filled & SURVEY_INFO_CHANNEL_TIME)
708 data[i++] = survey.channel_time;
709 else
710 data[i++] = -1LL;
711 if (survey.filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
712 data[i++] = survey.channel_time_busy;
713 else
714 data[i++] = -1LL;
715 if (survey.filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
716 data[i++] = survey.channel_time_ext_busy;
717 else
718 data[i++] = -1LL;
719 if (survey.filled & SURVEY_INFO_CHANNEL_TIME_RX)
720 data[i++] = survey.channel_time_rx;
721 else
722 data[i++] = -1LL;
723 if (survey.filled & SURVEY_INFO_CHANNEL_TIME_TX)
724 data[i++] = survey.channel_time_tx;
725 else
726 data[i++] = -1LL;
727
66572cfc 728 mutex_unlock(&local->sta_mtx);
e352114f 729
3073a7c2
BG
730 if (WARN_ON(i != STA_STATS_LEN))
731 return;
732
e352114f 733 drv_get_et_stats(sdata, stats, &(data[STA_STATS_LEN]));
b1ab7925
BG
734}
735
736static void ieee80211_get_et_strings(struct wiphy *wiphy,
737 struct net_device *dev,
738 u32 sset, u8 *data)
739{
e352114f
BG
740 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
741 int sz_sta_stats = 0;
742
b1ab7925 743 if (sset == ETH_SS_STATS) {
e352114f 744 sz_sta_stats = sizeof(ieee80211_gstrings_sta_stats);
bd500af2 745 memcpy(data, ieee80211_gstrings_sta_stats, sz_sta_stats);
b1ab7925 746 }
e352114f 747 drv_get_et_strings(sdata, sset, &(data[sz_sta_stats]));
b1ab7925 748}
c5dd9c2b
LCC
749
750static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
751 int idx, u8 *mac, struct station_info *sinfo)
752{
3b53fde8 753 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
66572cfc 754 struct ieee80211_local *local = sdata->local;
c5dd9c2b 755 struct sta_info *sta;
d0709a65
JB
756 int ret = -ENOENT;
757
66572cfc 758 mutex_lock(&local->sta_mtx);
c5dd9c2b 759
3b53fde8 760 sta = sta_info_get_by_idx(sdata, idx);
d0709a65
JB
761 if (sta) {
762 ret = 0;
17741cdc 763 memcpy(mac, sta->sta.addr, ETH_ALEN);
d0709a65
JB
764 sta_set_sinfo(sta, sinfo);
765 }
c5dd9c2b 766
66572cfc 767 mutex_unlock(&local->sta_mtx);
c5dd9c2b 768
d0709a65 769 return ret;
c5dd9c2b
LCC
770}
771
1289723e
HS
772static int ieee80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
773 int idx, struct survey_info *survey)
774{
775 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
776
1289723e
HS
777 return drv_get_survey(local, idx, survey);
778}
779
7bbdd2d9 780static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
2ec600d6 781 u8 *mac, struct station_info *sinfo)
7bbdd2d9 782{
abe60632 783 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
66572cfc 784 struct ieee80211_local *local = sdata->local;
7bbdd2d9 785 struct sta_info *sta;
d0709a65 786 int ret = -ENOENT;
7bbdd2d9 787
66572cfc 788 mutex_lock(&local->sta_mtx);
7bbdd2d9 789
0e5ded5a 790 sta = sta_info_get_bss(sdata, mac);
d0709a65
JB
791 if (sta) {
792 ret = 0;
793 sta_set_sinfo(sta, sinfo);
794 }
795
66572cfc 796 mutex_unlock(&local->sta_mtx);
d0709a65
JB
797
798 return ret;
7bbdd2d9
JB
799}
800
55de908a 801static int ieee80211_set_monitor_channel(struct wiphy *wiphy,
683b6d3b 802 struct cfg80211_chan_def *chandef)
3d9e6e12
JB
803{
804 struct ieee80211_local *local = wiphy_priv(wiphy);
55de908a
JB
805 struct ieee80211_sub_if_data *sdata;
806 int ret = 0;
3d9e6e12 807
4bf88530 808 if (cfg80211_chandef_identical(&local->monitor_chandef, chandef))
55de908a 809 return 0;
3d9e6e12 810
55de908a
JB
811 mutex_lock(&local->iflist_mtx);
812 if (local->use_chanctx) {
813 sdata = rcu_dereference_protected(
814 local->monitor_sdata,
815 lockdep_is_held(&local->iflist_mtx));
816 if (sdata) {
817 ieee80211_vif_release_channel(sdata);
4bf88530 818 ret = ieee80211_vif_use_channel(sdata, chandef,
55de908a
JB
819 IEEE80211_CHANCTX_EXCLUSIVE);
820 }
821 } else if (local->open_count == local->monitors) {
675a0b04 822 local->_oper_chandef = *chandef;
55de908a
JB
823 ieee80211_hw_config(local, 0);
824 }
3d9e6e12 825
4bf88530
JB
826 if (ret == 0)
827 local->monitor_chandef = *chandef;
55de908a 828 mutex_unlock(&local->iflist_mtx);
3d9e6e12 829
55de908a 830 return ret;
e8c9bd5b
JB
831}
832
02945821 833static int ieee80211_set_probe_resp(struct ieee80211_sub_if_data *sdata,
8860020e 834 const u8 *resp, size_t resp_len)
02945821 835{
aa7a0080 836 struct probe_resp *new, *old;
02945821
AN
837
838 if (!resp || !resp_len)
aba4e6ff 839 return 1;
02945821 840
f724828b 841 old = rtnl_dereference(sdata->u.ap.probe_resp);
02945821 842
aa7a0080 843 new = kzalloc(sizeof(struct probe_resp) + resp_len, GFP_KERNEL);
02945821
AN
844 if (!new)
845 return -ENOMEM;
846
aa7a0080
ES
847 new->len = resp_len;
848 memcpy(new->data, resp, resp_len);
02945821
AN
849
850 rcu_assign_pointer(sdata->u.ap.probe_resp, new);
aa7a0080
ES
851 if (old)
852 kfree_rcu(old, rcu_head);
02945821
AN
853
854 return 0;
855}
856
8860020e
JB
857static int ieee80211_assign_beacon(struct ieee80211_sub_if_data *sdata,
858 struct cfg80211_beacon_data *params)
5dfdaf58
JB
859{
860 struct beacon_data *new, *old;
861 int new_head_len, new_tail_len;
8860020e
JB
862 int size, err;
863 u32 changed = BSS_CHANGED_BEACON;
5dfdaf58 864
40b275b6 865 old = rtnl_dereference(sdata->u.ap.beacon);
5dfdaf58 866
5dfdaf58
JB
867 /* Need to have a beacon head if we don't have one yet */
868 if (!params->head && !old)
8860020e 869 return -EINVAL;
5dfdaf58
JB
870
871 /* new or old head? */
872 if (params->head)
873 new_head_len = params->head_len;
874 else
875 new_head_len = old->head_len;
876
877 /* new or old tail? */
878 if (params->tail || !old)
879 /* params->tail_len will be zero for !params->tail */
880 new_tail_len = params->tail_len;
881 else
882 new_tail_len = old->tail_len;
883
884 size = sizeof(*new) + new_head_len + new_tail_len;
885
886 new = kzalloc(size, GFP_KERNEL);
887 if (!new)
888 return -ENOMEM;
889
890 /* start filling the new info now */
891
5dfdaf58
JB
892 /*
893 * pointers go into the block we allocated,
894 * memory is | beacon_data | head | tail |
895 */
896 new->head = ((u8 *) new) + sizeof(*new);
897 new->tail = new->head + new_head_len;
898 new->head_len = new_head_len;
899 new->tail_len = new_tail_len;
900
901 /* copy in head */
902 if (params->head)
903 memcpy(new->head, params->head, new_head_len);
904 else
905 memcpy(new->head, old->head, new_head_len);
906
907 /* copy in optional tail */
908 if (params->tail)
909 memcpy(new->tail, params->tail, new_tail_len);
910 else
911 if (old)
912 memcpy(new->tail, old->tail, new_tail_len);
913
02945821
AN
914 err = ieee80211_set_probe_resp(sdata, params->probe_resp,
915 params->probe_resp_len);
8860020e
JB
916 if (err < 0)
917 return err;
918 if (err == 0)
02945821
AN
919 changed |= BSS_CHANGED_AP_PROBE_RESP;
920
8860020e
JB
921 rcu_assign_pointer(sdata->u.ap.beacon, new);
922
923 if (old)
924 kfree_rcu(old, rcu_head);
7827493b 925
8860020e 926 return changed;
5dfdaf58
JB
927}
928
8860020e
JB
929static int ieee80211_start_ap(struct wiphy *wiphy, struct net_device *dev,
930 struct cfg80211_ap_settings *params)
5dfdaf58 931{
8860020e 932 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5dfdaf58 933 struct beacon_data *old;
665c93a9 934 struct ieee80211_sub_if_data *vlan;
8860020e
JB
935 u32 changed = BSS_CHANGED_BEACON_INT |
936 BSS_CHANGED_BEACON_ENABLED |
937 BSS_CHANGED_BEACON |
339afbf4
JB
938 BSS_CHANGED_SSID |
939 BSS_CHANGED_P2P_PS;
8860020e 940 int err;
14db74bc 941
40b275b6 942 old = rtnl_dereference(sdata->u.ap.beacon);
5dfdaf58
JB
943 if (old)
944 return -EALREADY;
945
04ecd257
JB
946 /* TODO: make hostapd tell us what it wants */
947 sdata->smps_mode = IEEE80211_SMPS_OFF;
948 sdata->needed_rx_chains = sdata->local->rx_chains;
164eb02d 949 sdata->radar_required = params->radar_required;
04ecd257 950
4bf88530 951 err = ieee80211_vif_use_channel(sdata, &params->chandef,
55de908a 952 IEEE80211_CHANCTX_SHARED);
aa430da4
JB
953 if (err)
954 return err;
1f4ac5a6 955 ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
aa430da4 956
665c93a9
JB
957 /*
958 * Apply control port protocol, this allows us to
959 * not encrypt dynamic WEP control frames.
960 */
961 sdata->control_port_protocol = params->crypto.control_port_ethertype;
962 sdata->control_port_no_encrypt = params->crypto.control_port_no_encrypt;
963 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
964 vlan->control_port_protocol =
965 params->crypto.control_port_ethertype;
966 vlan->control_port_no_encrypt =
967 params->crypto.control_port_no_encrypt;
968 }
969
8860020e
JB
970 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
971 sdata->vif.bss_conf.dtim_period = params->dtim_period;
d6a83228 972 sdata->vif.bss_conf.enable_beacon = true;
8860020e
JB
973
974 sdata->vif.bss_conf.ssid_len = params->ssid_len;
975 if (params->ssid_len)
976 memcpy(sdata->vif.bss_conf.ssid, params->ssid,
977 params->ssid_len);
978 sdata->vif.bss_conf.hidden_ssid =
979 (params->hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE);
980
67baf663
JD
981 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
982 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
983 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow =
984 params->p2p_ctwindow & IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
985 if (params->p2p_opp_ps)
986 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow |=
987 IEEE80211_P2P_OPPPS_ENABLE_BIT;
339afbf4 988
8860020e
JB
989 err = ieee80211_assign_beacon(sdata, &params->beacon);
990 if (err < 0)
991 return err;
992 changed |= err;
993
1041638f
JB
994 err = drv_start_ap(sdata->local, sdata);
995 if (err) {
996 old = rtnl_dereference(sdata->u.ap.beacon);
997 if (old)
998 kfree_rcu(old, rcu_head);
999 RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
1000 return err;
1001 }
1002
8860020e
JB
1003 ieee80211_bss_info_change_notify(sdata, changed);
1004
3edaf3e6
JB
1005 netif_carrier_on(dev);
1006 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1007 netif_carrier_on(vlan->dev);
1008
665c93a9 1009 return 0;
5dfdaf58
JB
1010}
1011
8860020e
JB
1012static int ieee80211_change_beacon(struct wiphy *wiphy, struct net_device *dev,
1013 struct cfg80211_beacon_data *params)
5dfdaf58 1014{
14db74bc 1015 struct ieee80211_sub_if_data *sdata;
5dfdaf58 1016 struct beacon_data *old;
8860020e 1017 int err;
5dfdaf58 1018
14db74bc
JB
1019 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1020
40b275b6 1021 old = rtnl_dereference(sdata->u.ap.beacon);
5dfdaf58
JB
1022 if (!old)
1023 return -ENOENT;
1024
8860020e
JB
1025 err = ieee80211_assign_beacon(sdata, params);
1026 if (err < 0)
1027 return err;
1028 ieee80211_bss_info_change_notify(sdata, err);
1029 return 0;
5dfdaf58
JB
1030}
1031
8860020e 1032static int ieee80211_stop_ap(struct wiphy *wiphy, struct net_device *dev)
5dfdaf58 1033{
7b20b8e8
JB
1034 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1035 struct ieee80211_sub_if_data *vlan;
1036 struct ieee80211_local *local = sdata->local;
1037 struct beacon_data *old_beacon;
1038 struct probe_resp *old_probe_resp;
14db74bc 1039
7b20b8e8
JB
1040 old_beacon = rtnl_dereference(sdata->u.ap.beacon);
1041 if (!old_beacon)
5dfdaf58 1042 return -ENOENT;
7b20b8e8 1043 old_probe_resp = rtnl_dereference(sdata->u.ap.probe_resp);
5dfdaf58 1044
7b20b8e8 1045 /* turn off carrier for this interface and dependent VLANs */
3edaf3e6
JB
1046 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1047 netif_carrier_off(vlan->dev);
1048 netif_carrier_off(dev);
1049
7b20b8e8 1050 /* remove beacon and probe response */
a9b3cd7f 1051 RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
7b20b8e8
JB
1052 RCU_INIT_POINTER(sdata->u.ap.probe_resp, NULL);
1053 kfree_rcu(old_beacon, rcu_head);
1054 if (old_probe_resp)
1055 kfree_rcu(old_probe_resp, rcu_head);
8860020e 1056
2d4072a5 1057 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
75de9113
JB
1058 sta_info_flush_defer(vlan);
1059 sta_info_flush_defer(sdata);
8ceb5955 1060 synchronize_net();
75de9113 1061 rcu_barrier();
7b4396bd 1062 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
75de9113 1063 sta_info_flush_cleanup(vlan);
7b4396bd
JB
1064 ieee80211_free_keys(vlan);
1065 }
75de9113 1066 sta_info_flush_cleanup(sdata);
7b4396bd 1067 ieee80211_free_keys(sdata);
75de9113 1068
d6a83228 1069 sdata->vif.bss_conf.enable_beacon = false;
0eabccd9 1070 sdata->vif.bss_conf.ssid_len = 0;
d6a83228 1071 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
2d0ddec5 1072 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
8860020e 1073
1041638f
JB
1074 drv_stop_ap(sdata->local, sdata);
1075
7b20b8e8
JB
1076 /* free all potentially still buffered bcast frames */
1077 local->total_ps_buffered -= skb_queue_len(&sdata->u.ap.ps.bc_buf);
1078 skb_queue_purge(&sdata->u.ap.ps.bc_buf);
1079
1f4ac5a6 1080 ieee80211_vif_copy_chanctx_to_vlans(sdata, true);
55de908a
JB
1081 ieee80211_vif_release_channel(sdata);
1082
2d0ddec5 1083 return 0;
5dfdaf58
JB
1084}
1085
4fd6931e
JB
1086/* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1087struct iapp_layer2_update {
1088 u8 da[ETH_ALEN]; /* broadcast */
1089 u8 sa[ETH_ALEN]; /* STA addr */
1090 __be16 len; /* 6 */
1091 u8 dsap; /* 0 */
1092 u8 ssap; /* 0 */
1093 u8 control;
1094 u8 xid_info[3];
bc10502d 1095} __packed;
4fd6931e
JB
1096
1097static void ieee80211_send_layer2_update(struct sta_info *sta)
1098{
1099 struct iapp_layer2_update *msg;
1100 struct sk_buff *skb;
1101
1102 /* Send Level 2 Update Frame to update forwarding tables in layer 2
1103 * bridge devices */
1104
1105 skb = dev_alloc_skb(sizeof(*msg));
1106 if (!skb)
1107 return;
1108 msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
1109
1110 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1111 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1112
e83e6541 1113 eth_broadcast_addr(msg->da);
17741cdc 1114 memcpy(msg->sa, sta->sta.addr, ETH_ALEN);
4fd6931e
JB
1115 msg->len = htons(6);
1116 msg->dsap = 0;
1117 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
1118 msg->control = 0xaf; /* XID response lsb.1111F101.
1119 * F=0 (no poll command; unsolicited frame) */
1120 msg->xid_info[0] = 0x81; /* XID format identifier */
1121 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
1122 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
1123
d0709a65
JB
1124 skb->dev = sta->sdata->dev;
1125 skb->protocol = eth_type_trans(skb, sta->sdata->dev);
4fd6931e 1126 memset(skb->cb, 0, sizeof(skb->cb));
06ee1c26 1127 netif_rx_ni(skb);
4fd6931e
JB
1128}
1129
d582cffb
JB
1130static int sta_apply_auth_flags(struct ieee80211_local *local,
1131 struct sta_info *sta,
1132 u32 mask, u32 set)
1133{
1134 int ret;
1135
1136 if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1137 set & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1138 !test_sta_flag(sta, WLAN_STA_AUTH)) {
1139 ret = sta_info_move_state(sta, IEEE80211_STA_AUTH);
1140 if (ret)
1141 return ret;
1142 }
1143
1144 if (mask & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1145 set & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1146 !test_sta_flag(sta, WLAN_STA_ASSOC)) {
1147 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1148 if (ret)
1149 return ret;
1150 }
1151
1152 if (mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
1153 if (set & BIT(NL80211_STA_FLAG_AUTHORIZED))
1154 ret = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
1155 else if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1156 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1157 else
1158 ret = 0;
1159 if (ret)
1160 return ret;
1161 }
1162
1163 if (mask & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1164 !(set & BIT(NL80211_STA_FLAG_ASSOCIATED)) &&
1165 test_sta_flag(sta, WLAN_STA_ASSOC)) {
1166 ret = sta_info_move_state(sta, IEEE80211_STA_AUTH);
1167 if (ret)
1168 return ret;
1169 }
1170
1171 if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1172 !(set & BIT(NL80211_STA_FLAG_AUTHENTICATED)) &&
1173 test_sta_flag(sta, WLAN_STA_AUTH)) {
1174 ret = sta_info_move_state(sta, IEEE80211_STA_NONE);
1175 if (ret)
1176 return ret;
1177 }
1178
1179 return 0;
1180}
1181
d9a7ddb0
JB
1182static int sta_apply_parameters(struct ieee80211_local *local,
1183 struct sta_info *sta,
1184 struct station_parameters *params)
4fd6931e 1185{
d9a7ddb0 1186 int ret = 0;
4fd6931e
JB
1187 u32 rates;
1188 int i, j;
8318d78a 1189 struct ieee80211_supported_band *sband;
d0709a65 1190 struct ieee80211_sub_if_data *sdata = sta->sdata;
55de908a 1191 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
eccb8e8f 1192 u32 mask, set;
4fd6931e 1193
55de908a 1194 sband = local->hw.wiphy->bands[band];
ae5eb026 1195
eccb8e8f
JB
1196 mask = params->sta_flags_mask;
1197 set = params->sta_flags_set;
73651ee6 1198
d582cffb
JB
1199 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1200 /*
1201 * In mesh mode, ASSOCIATED isn't part of the nl80211
1202 * API but must follow AUTHENTICATED for driver state.
1203 */
1204 if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED))
1205 mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1206 if (set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
1207 set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
77ee7c89
JB
1208 } else if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
1209 /*
1210 * TDLS -- everything follows authorized, but
1211 * only becoming authorized is possible, not
1212 * going back
1213 */
1214 if (set & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
1215 set |= BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1216 BIT(NL80211_STA_FLAG_ASSOCIATED);
1217 mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1218 BIT(NL80211_STA_FLAG_ASSOCIATED);
1219 }
eccb8e8f 1220 }
4fd6931e 1221
d582cffb
JB
1222 ret = sta_apply_auth_flags(local, sta, mask, set);
1223 if (ret)
1224 return ret;
d9a7ddb0 1225
eccb8e8f 1226 if (mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) {
eccb8e8f 1227 if (set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
c2c98fde
JB
1228 set_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE);
1229 else
1230 clear_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE);
eccb8e8f 1231 }
4fd6931e 1232
eccb8e8f 1233 if (mask & BIT(NL80211_STA_FLAG_WME)) {
39df600a 1234 if (set & BIT(NL80211_STA_FLAG_WME)) {
c2c98fde 1235 set_sta_flag(sta, WLAN_STA_WME);
39df600a 1236 sta->sta.wme = true;
c2c98fde
JB
1237 } else {
1238 clear_sta_flag(sta, WLAN_STA_WME);
1239 sta->sta.wme = false;
39df600a 1240 }
eccb8e8f 1241 }
5394af4d 1242
eccb8e8f 1243 if (mask & BIT(NL80211_STA_FLAG_MFP)) {
eccb8e8f 1244 if (set & BIT(NL80211_STA_FLAG_MFP))
c2c98fde
JB
1245 set_sta_flag(sta, WLAN_STA_MFP);
1246 else
1247 clear_sta_flag(sta, WLAN_STA_MFP);
4fd6931e 1248 }
b39c48fa 1249
07ba55d7 1250 if (mask & BIT(NL80211_STA_FLAG_TDLS_PEER)) {
07ba55d7 1251 if (set & BIT(NL80211_STA_FLAG_TDLS_PEER))
c2c98fde
JB
1252 set_sta_flag(sta, WLAN_STA_TDLS_PEER);
1253 else
1254 clear_sta_flag(sta, WLAN_STA_TDLS_PEER);
07ba55d7 1255 }
4fd6931e 1256
3b9ce80c
JB
1257 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD) {
1258 sta->sta.uapsd_queues = params->uapsd_queues;
1259 sta->sta.max_sp = params->max_sp;
1260 }
9533b4ac 1261
51b50fbe
JB
1262 /*
1263 * cfg80211 validates this (1-2007) and allows setting the AID
1264 * only when creating a new station entry
1265 */
1266 if (params->aid)
1267 sta->sta.aid = params->aid;
1268
73651ee6 1269 /*
ba23d206
JB
1270 * Some of the following updates would be racy if called on an
1271 * existing station, via ieee80211_change_station(). However,
1272 * all such changes are rejected by cfg80211 except for updates
1273 * changing the supported rates on an existing but not yet used
1274 * TDLS peer.
73651ee6
JB
1275 */
1276
4fd6931e
JB
1277 if (params->listen_interval >= 0)
1278 sta->listen_interval = params->listen_interval;
1279
1280 if (params->supported_rates) {
1281 rates = 0;
8318d78a 1282
4fd6931e
JB
1283 for (i = 0; i < params->supported_rates_len; i++) {
1284 int rate = (params->supported_rates[i] & 0x7f) * 5;
8318d78a
JB
1285 for (j = 0; j < sband->n_bitrates; j++) {
1286 if (sband->bitrates[j].bitrate == rate)
4fd6931e
JB
1287 rates |= BIT(j);
1288 }
1289 }
55de908a 1290 sta->sta.supp_rates[band] = rates;
4fd6931e 1291 }
c5dd9c2b 1292
d9fe60de 1293 if (params->ht_capa)
ef96a842 1294 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
e1a0c6b3 1295 params->ht_capa, sta);
36aedc90 1296
f461be3e
MP
1297 if (params->vht_capa)
1298 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
4a34215e 1299 params->vht_capa, sta);
f461be3e 1300
9c3990aa 1301 if (ieee80211_vif_is_mesh(&sdata->vif)) {
4daf50f2 1302#ifdef CONFIG_MAC80211_MESH
39886b61 1303 u32 changed = 0;
77ee7c89
JB
1304
1305 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE) {
9c3990aa 1306 switch (params->plink_state) {
57cf8043 1307 case NL80211_PLINK_ESTAB:
1617bab8
MP
1308 if (sta->plink_state != NL80211_PLINK_ESTAB)
1309 changed = mesh_plink_inc_estab_count(
1310 sdata);
1311 sta->plink_state = params->plink_state;
3f52b7e3
MP
1312
1313 ieee80211_mps_sta_status_update(sta);
39886b61
TP
1314 changed |= ieee80211_mps_set_sta_local_pm(sta,
1315 sdata->u.mesh.mshcfg.power_mode);
1617bab8
MP
1316 break;
1317 case NL80211_PLINK_LISTEN:
57cf8043 1318 case NL80211_PLINK_BLOCKED:
1617bab8
MP
1319 case NL80211_PLINK_OPN_SNT:
1320 case NL80211_PLINK_OPN_RCVD:
1321 case NL80211_PLINK_CNF_RCVD:
1322 case NL80211_PLINK_HOLDING:
1323 if (sta->plink_state == NL80211_PLINK_ESTAB)
1324 changed = mesh_plink_dec_estab_count(
1325 sdata);
9c3990aa 1326 sta->plink_state = params->plink_state;
3f52b7e3
MP
1327
1328 ieee80211_mps_sta_status_update(sta);
39886b61
TP
1329 changed |=
1330 ieee80211_mps_local_status_update(sdata);
9c3990aa
JC
1331 break;
1332 default:
1333 /* nothing */
1334 break;
1335 }
77ee7c89
JB
1336 }
1337
1338 switch (params->plink_action) {
1339 case NL80211_PLINK_ACTION_NO_ACTION:
1340 /* nothing */
1341 break;
1342 case NL80211_PLINK_ACTION_OPEN:
1343 changed |= mesh_plink_open(sta);
1344 break;
1345 case NL80211_PLINK_ACTION_BLOCK:
1346 changed |= mesh_plink_block(sta);
1347 break;
1617bab8 1348 }
3f52b7e3
MP
1349
1350 if (params->local_pm)
39886b61
TP
1351 changed |=
1352 ieee80211_mps_set_sta_local_pm(sta,
1353 params->local_pm);
1354 ieee80211_bss_info_change_notify(sdata, changed);
4daf50f2 1355#endif
902acc78 1356 }
d9a7ddb0
JB
1357
1358 return 0;
4fd6931e
JB
1359}
1360
1361static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
1362 u8 *mac, struct station_parameters *params)
1363{
14db74bc 1364 struct ieee80211_local *local = wiphy_priv(wiphy);
4fd6931e
JB
1365 struct sta_info *sta;
1366 struct ieee80211_sub_if_data *sdata;
73651ee6 1367 int err;
b8d476c8 1368 int layer2_update;
4fd6931e 1369
4fd6931e
JB
1370 if (params->vlan) {
1371 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
1372
05c914fe
JB
1373 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1374 sdata->vif.type != NL80211_IFTYPE_AP)
4fd6931e
JB
1375 return -EINVAL;
1376 } else
1377 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1378
b203ca39 1379 if (ether_addr_equal(mac, sdata->vif.addr))
03e4497e
JB
1380 return -EINVAL;
1381
1382 if (is_multicast_ether_addr(mac))
1383 return -EINVAL;
1384
1385 sta = sta_info_alloc(sdata, mac, GFP_KERNEL);
73651ee6
JB
1386 if (!sta)
1387 return -ENOMEM;
4fd6931e 1388
d582cffb
JB
1389 /*
1390 * defaults -- if userspace wants something else we'll
1391 * change it accordingly in sta_apply_parameters()
1392 */
77ee7c89
JB
1393 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))) {
1394 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
1395 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
1396 }
4fd6931e 1397
d9a7ddb0
JB
1398 err = sta_apply_parameters(local, sta, params);
1399 if (err) {
1400 sta_info_free(local, sta);
1401 return err;
1402 }
4fd6931e 1403
d64cf63e 1404 /*
77ee7c89
JB
1405 * for TDLS, rate control should be initialized only when
1406 * rates are known and station is marked authorized
d64cf63e
AN
1407 */
1408 if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1409 rate_control_rate_init(sta);
4fd6931e 1410
b8d476c8
JM
1411 layer2_update = sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1412 sdata->vif.type == NL80211_IFTYPE_AP;
1413
34e89507 1414 err = sta_info_insert_rcu(sta);
73651ee6 1415 if (err) {
73651ee6
JB
1416 rcu_read_unlock();
1417 return err;
1418 }
1419
b8d476c8 1420 if (layer2_update)
73651ee6
JB
1421 ieee80211_send_layer2_update(sta);
1422
1423 rcu_read_unlock();
1424
4fd6931e
JB
1425 return 0;
1426}
1427
1428static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
1429 u8 *mac)
1430{
14db74bc 1431 struct ieee80211_sub_if_data *sdata;
4fd6931e 1432
14db74bc
JB
1433 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1434
34e89507
JB
1435 if (mac)
1436 return sta_info_destroy_addr_bss(sdata, mac);
4fd6931e 1437
b998e8bb 1438 sta_info_flush(sdata);
4fd6931e
JB
1439 return 0;
1440}
1441
1442static int ieee80211_change_station(struct wiphy *wiphy,
77ee7c89 1443 struct net_device *dev, u8 *mac,
4fd6931e
JB
1444 struct station_parameters *params)
1445{
abe60632 1446 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
14db74bc 1447 struct ieee80211_local *local = wiphy_priv(wiphy);
4fd6931e
JB
1448 struct sta_info *sta;
1449 struct ieee80211_sub_if_data *vlansdata;
77ee7c89 1450 enum cfg80211_station_type statype;
35b88623 1451 int err;
4fd6931e 1452
87be1e1e 1453 mutex_lock(&local->sta_mtx);
98dd6a57 1454
0e5ded5a 1455 sta = sta_info_get_bss(sdata, mac);
98dd6a57 1456 if (!sta) {
77ee7c89
JB
1457 err = -ENOENT;
1458 goto out_err;
98dd6a57 1459 }
4fd6931e 1460
77ee7c89
JB
1461 switch (sdata->vif.type) {
1462 case NL80211_IFTYPE_MESH_POINT:
a6dad6a2 1463 if (sdata->u.mesh.user_mpm)
eef941e6 1464 statype = CFG80211_STA_MESH_PEER_USER;
77ee7c89 1465 else
eef941e6 1466 statype = CFG80211_STA_MESH_PEER_KERNEL;
77ee7c89
JB
1467 break;
1468 case NL80211_IFTYPE_ADHOC:
1469 statype = CFG80211_STA_IBSS;
1470 break;
1471 case NL80211_IFTYPE_STATION:
1472 if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
1473 statype = CFG80211_STA_AP_STA;
1474 break;
1475 }
1476 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1477 statype = CFG80211_STA_TDLS_PEER_ACTIVE;
1478 else
1479 statype = CFG80211_STA_TDLS_PEER_SETUP;
1480 break;
1481 case NL80211_IFTYPE_AP:
1482 case NL80211_IFTYPE_AP_VLAN:
1483 statype = CFG80211_STA_AP_CLIENT;
1484 break;
1485 default:
1486 err = -EOPNOTSUPP;
1487 goto out_err;
bdd90d5e
JB
1488 }
1489
77ee7c89
JB
1490 err = cfg80211_check_station_change(wiphy, params, statype);
1491 if (err)
1492 goto out_err;
1493
d0709a65 1494 if (params->vlan && params->vlan != sta->sdata->dev) {
7e3ed02c
FF
1495 bool prev_4addr = false;
1496 bool new_4addr = false;
1497
4fd6931e
JB
1498 vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
1499
9bc383de 1500 if (params->vlan->ieee80211_ptr->use_4addr) {
3305443c 1501 if (vlansdata->u.vlan.sta) {
77ee7c89
JB
1502 err = -EBUSY;
1503 goto out_err;
3305443c 1504 }
f14543ee 1505
cf778b00 1506 rcu_assign_pointer(vlansdata->u.vlan.sta, sta);
7e3ed02c
FF
1507 new_4addr = true;
1508 }
1509
1510 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1511 sta->sdata->u.vlan.sta) {
1512 rcu_assign_pointer(sta->sdata->u.vlan.sta, NULL);
1513 prev_4addr = true;
f14543ee
FF
1514 }
1515
14db74bc 1516 sta->sdata = vlansdata;
7e3ed02c
FF
1517
1518 if (sta->sta_state == IEEE80211_STA_AUTHORIZED &&
1519 prev_4addr != new_4addr) {
1520 if (new_4addr)
1521 atomic_dec(&sta->sdata->bss->num_mcast_sta);
1522 else
1523 atomic_inc(&sta->sdata->bss->num_mcast_sta);
1524 }
1525
4fd6931e
JB
1526 ieee80211_send_layer2_update(sta);
1527 }
1528
35b88623 1529 err = sta_apply_parameters(local, sta, params);
77ee7c89
JB
1530 if (err)
1531 goto out_err;
4fd6931e 1532
77ee7c89
JB
1533 /* When peer becomes authorized, init rate control as well */
1534 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
1535 test_sta_flag(sta, WLAN_STA_AUTHORIZED))
d64cf63e
AN
1536 rate_control_rate_init(sta);
1537
87be1e1e 1538 mutex_unlock(&local->sta_mtx);
98dd6a57 1539
808118cb 1540 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
ab095877 1541 params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
808118cb 1542 ieee80211_recalc_ps(local, -1);
ab095877
EP
1543 ieee80211_recalc_ps_vif(sdata);
1544 }
77ee7c89 1545
4fd6931e 1546 return 0;
77ee7c89
JB
1547out_err:
1548 mutex_unlock(&local->sta_mtx);
1549 return err;
4fd6931e
JB
1550}
1551
c5dd9c2b
LCC
1552#ifdef CONFIG_MAC80211_MESH
1553static int ieee80211_add_mpath(struct wiphy *wiphy, struct net_device *dev,
1554 u8 *dst, u8 *next_hop)
1555{
14db74bc 1556 struct ieee80211_sub_if_data *sdata;
c5dd9c2b
LCC
1557 struct mesh_path *mpath;
1558 struct sta_info *sta;
c5dd9c2b 1559
14db74bc
JB
1560 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1561
d0709a65 1562 rcu_read_lock();
abe60632 1563 sta = sta_info_get(sdata, next_hop);
d0709a65
JB
1564 if (!sta) {
1565 rcu_read_unlock();
c5dd9c2b 1566 return -ENOENT;
d0709a65 1567 }
c5dd9c2b 1568
ae76eef0
BC
1569 mpath = mesh_path_add(sdata, dst);
1570 if (IS_ERR(mpath)) {
d0709a65 1571 rcu_read_unlock();
ae76eef0 1572 return PTR_ERR(mpath);
d0709a65 1573 }
c5dd9c2b 1574
c5dd9c2b 1575 mesh_path_fix_nexthop(mpath, sta);
d0709a65 1576
c5dd9c2b
LCC
1577 rcu_read_unlock();
1578 return 0;
1579}
1580
1581static int ieee80211_del_mpath(struct wiphy *wiphy, struct net_device *dev,
bf7cd94d 1582 u8 *dst)
c5dd9c2b 1583{
f698d856
JBG
1584 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1585
c5dd9c2b 1586 if (dst)
bf7cd94d 1587 return mesh_path_del(sdata, dst);
c5dd9c2b 1588
ece1a2e7 1589 mesh_path_flush_by_iface(sdata);
c5dd9c2b
LCC
1590 return 0;
1591}
1592
1593static int ieee80211_change_mpath(struct wiphy *wiphy,
1594 struct net_device *dev,
1595 u8 *dst, u8 *next_hop)
1596{
14db74bc 1597 struct ieee80211_sub_if_data *sdata;
c5dd9c2b
LCC
1598 struct mesh_path *mpath;
1599 struct sta_info *sta;
1600
14db74bc
JB
1601 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1602
d0709a65
JB
1603 rcu_read_lock();
1604
abe60632 1605 sta = sta_info_get(sdata, next_hop);
d0709a65
JB
1606 if (!sta) {
1607 rcu_read_unlock();
c5dd9c2b 1608 return -ENOENT;
d0709a65 1609 }
c5dd9c2b 1610
bf7cd94d 1611 mpath = mesh_path_lookup(sdata, dst);
c5dd9c2b
LCC
1612 if (!mpath) {
1613 rcu_read_unlock();
c5dd9c2b
LCC
1614 return -ENOENT;
1615 }
1616
1617 mesh_path_fix_nexthop(mpath, sta);
d0709a65 1618
c5dd9c2b
LCC
1619 rcu_read_unlock();
1620 return 0;
1621}
1622
1623static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
1624 struct mpath_info *pinfo)
1625{
a3836e02
JB
1626 struct sta_info *next_hop_sta = rcu_dereference(mpath->next_hop);
1627
1628 if (next_hop_sta)
1629 memcpy(next_hop, next_hop_sta->sta.addr, ETH_ALEN);
c5dd9c2b
LCC
1630 else
1631 memset(next_hop, 0, ETH_ALEN);
1632
7ce8c7a3
LAYS
1633 memset(pinfo, 0, sizeof(*pinfo));
1634
f5ea9120
JB
1635 pinfo->generation = mesh_paths_generation;
1636
c5dd9c2b 1637 pinfo->filled = MPATH_INFO_FRAME_QLEN |
d19b3bf6 1638 MPATH_INFO_SN |
c5dd9c2b
LCC
1639 MPATH_INFO_METRIC |
1640 MPATH_INFO_EXPTIME |
1641 MPATH_INFO_DISCOVERY_TIMEOUT |
1642 MPATH_INFO_DISCOVERY_RETRIES |
1643 MPATH_INFO_FLAGS;
1644
1645 pinfo->frame_qlen = mpath->frame_queue.qlen;
d19b3bf6 1646 pinfo->sn = mpath->sn;
c5dd9c2b
LCC
1647 pinfo->metric = mpath->metric;
1648 if (time_before(jiffies, mpath->exp_time))
1649 pinfo->exptime = jiffies_to_msecs(mpath->exp_time - jiffies);
1650 pinfo->discovery_timeout =
1651 jiffies_to_msecs(mpath->discovery_timeout);
1652 pinfo->discovery_retries = mpath->discovery_retries;
c5dd9c2b
LCC
1653 if (mpath->flags & MESH_PATH_ACTIVE)
1654 pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE;
1655 if (mpath->flags & MESH_PATH_RESOLVING)
1656 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
d19b3bf6
RP
1657 if (mpath->flags & MESH_PATH_SN_VALID)
1658 pinfo->flags |= NL80211_MPATH_FLAG_SN_VALID;
c5dd9c2b
LCC
1659 if (mpath->flags & MESH_PATH_FIXED)
1660 pinfo->flags |= NL80211_MPATH_FLAG_FIXED;
7ce8c7a3
LAYS
1661 if (mpath->flags & MESH_PATH_RESOLVED)
1662 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVED;
c5dd9c2b
LCC
1663}
1664
1665static int ieee80211_get_mpath(struct wiphy *wiphy, struct net_device *dev,
1666 u8 *dst, u8 *next_hop, struct mpath_info *pinfo)
1667
1668{
14db74bc 1669 struct ieee80211_sub_if_data *sdata;
c5dd9c2b
LCC
1670 struct mesh_path *mpath;
1671
14db74bc
JB
1672 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1673
c5dd9c2b 1674 rcu_read_lock();
bf7cd94d 1675 mpath = mesh_path_lookup(sdata, dst);
c5dd9c2b
LCC
1676 if (!mpath) {
1677 rcu_read_unlock();
1678 return -ENOENT;
1679 }
1680 memcpy(dst, mpath->dst, ETH_ALEN);
1681 mpath_set_pinfo(mpath, next_hop, pinfo);
1682 rcu_read_unlock();
1683 return 0;
1684}
1685
1686static int ieee80211_dump_mpath(struct wiphy *wiphy, struct net_device *dev,
1687 int idx, u8 *dst, u8 *next_hop,
1688 struct mpath_info *pinfo)
1689{
14db74bc 1690 struct ieee80211_sub_if_data *sdata;
c5dd9c2b
LCC
1691 struct mesh_path *mpath;
1692
14db74bc
JB
1693 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1694
c5dd9c2b 1695 rcu_read_lock();
bf7cd94d 1696 mpath = mesh_path_lookup_by_idx(sdata, idx);
c5dd9c2b
LCC
1697 if (!mpath) {
1698 rcu_read_unlock();
1699 return -ENOENT;
1700 }
1701 memcpy(dst, mpath->dst, ETH_ALEN);
1702 mpath_set_pinfo(mpath, next_hop, pinfo);
1703 rcu_read_unlock();
1704 return 0;
1705}
93da9cc1 1706
24bdd9f4 1707static int ieee80211_get_mesh_config(struct wiphy *wiphy,
93da9cc1 1708 struct net_device *dev,
1709 struct mesh_config *conf)
1710{
1711 struct ieee80211_sub_if_data *sdata;
1712 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1713
93da9cc1 1714 memcpy(conf, &(sdata->u.mesh.mshcfg), sizeof(struct mesh_config));
1715 return 0;
1716}
1717
1718static inline bool _chg_mesh_attr(enum nl80211_meshconf_params parm, u32 mask)
1719{
1720 return (mask >> (parm-1)) & 0x1;
1721}
1722
c80d545d
JC
1723static int copy_mesh_setup(struct ieee80211_if_mesh *ifmsh,
1724 const struct mesh_setup *setup)
1725{
1726 u8 *new_ie;
1727 const u8 *old_ie;
4bb62344
CYY
1728 struct ieee80211_sub_if_data *sdata = container_of(ifmsh,
1729 struct ieee80211_sub_if_data, u.mesh);
c80d545d 1730
581a8b0f 1731 /* allocate information elements */
c80d545d 1732 new_ie = NULL;
581a8b0f 1733 old_ie = ifmsh->ie;
c80d545d 1734
581a8b0f
JC
1735 if (setup->ie_len) {
1736 new_ie = kmemdup(setup->ie, setup->ie_len,
c80d545d
JC
1737 GFP_KERNEL);
1738 if (!new_ie)
1739 return -ENOMEM;
1740 }
581a8b0f
JC
1741 ifmsh->ie_len = setup->ie_len;
1742 ifmsh->ie = new_ie;
1743 kfree(old_ie);
c80d545d
JC
1744
1745 /* now copy the rest of the setup parameters */
1746 ifmsh->mesh_id_len = setup->mesh_id_len;
1747 memcpy(ifmsh->mesh_id, setup->mesh_id, ifmsh->mesh_id_len);
d299a1f2 1748 ifmsh->mesh_sp_id = setup->sync_method;
c80d545d
JC
1749 ifmsh->mesh_pp_id = setup->path_sel_proto;
1750 ifmsh->mesh_pm_id = setup->path_metric;
a6dad6a2 1751 ifmsh->user_mpm = setup->user_mpm;
0d4261ad 1752 ifmsh->mesh_auth_id = setup->auth_id;
b130e5ce
JC
1753 ifmsh->security = IEEE80211_MESH_SEC_NONE;
1754 if (setup->is_authenticated)
1755 ifmsh->security |= IEEE80211_MESH_SEC_AUTHED;
1756 if (setup->is_secure)
1757 ifmsh->security |= IEEE80211_MESH_SEC_SECURED;
c80d545d 1758
4bb62344
CYY
1759 /* mcast rate setting in Mesh Node */
1760 memcpy(sdata->vif.bss_conf.mcast_rate, setup->mcast_rate,
1761 sizeof(setup->mcast_rate));
1762
9bdbf04d
MP
1763 sdata->vif.bss_conf.beacon_int = setup->beacon_interval;
1764 sdata->vif.bss_conf.dtim_period = setup->dtim_period;
1765
c80d545d
JC
1766 return 0;
1767}
1768
24bdd9f4 1769static int ieee80211_update_mesh_config(struct wiphy *wiphy,
29cbe68c
JB
1770 struct net_device *dev, u32 mask,
1771 const struct mesh_config *nconf)
93da9cc1 1772{
1773 struct mesh_config *conf;
1774 struct ieee80211_sub_if_data *sdata;
63c5723b
RP
1775 struct ieee80211_if_mesh *ifmsh;
1776
93da9cc1 1777 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
63c5723b 1778 ifmsh = &sdata->u.mesh;
93da9cc1 1779
93da9cc1 1780 /* Set the config options which we are interested in setting */
1781 conf = &(sdata->u.mesh.mshcfg);
1782 if (_chg_mesh_attr(NL80211_MESHCONF_RETRY_TIMEOUT, mask))
1783 conf->dot11MeshRetryTimeout = nconf->dot11MeshRetryTimeout;
1784 if (_chg_mesh_attr(NL80211_MESHCONF_CONFIRM_TIMEOUT, mask))
1785 conf->dot11MeshConfirmTimeout = nconf->dot11MeshConfirmTimeout;
1786 if (_chg_mesh_attr(NL80211_MESHCONF_HOLDING_TIMEOUT, mask))
1787 conf->dot11MeshHoldingTimeout = nconf->dot11MeshHoldingTimeout;
1788 if (_chg_mesh_attr(NL80211_MESHCONF_MAX_PEER_LINKS, mask))
1789 conf->dot11MeshMaxPeerLinks = nconf->dot11MeshMaxPeerLinks;
1790 if (_chg_mesh_attr(NL80211_MESHCONF_MAX_RETRIES, mask))
1791 conf->dot11MeshMaxRetries = nconf->dot11MeshMaxRetries;
1792 if (_chg_mesh_attr(NL80211_MESHCONF_TTL, mask))
1793 conf->dot11MeshTTL = nconf->dot11MeshTTL;
45904f21 1794 if (_chg_mesh_attr(NL80211_MESHCONF_ELEMENT_TTL, mask))
58886a90 1795 conf->element_ttl = nconf->element_ttl;
146bb483
TP
1796 if (_chg_mesh_attr(NL80211_MESHCONF_AUTO_OPEN_PLINKS, mask)) {
1797 if (ifmsh->user_mpm)
1798 return -EBUSY;
93da9cc1 1799 conf->auto_open_plinks = nconf->auto_open_plinks;
146bb483 1800 }
d299a1f2
JC
1801 if (_chg_mesh_attr(NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR, mask))
1802 conf->dot11MeshNbrOffsetMaxNeighbor =
1803 nconf->dot11MeshNbrOffsetMaxNeighbor;
93da9cc1 1804 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, mask))
1805 conf->dot11MeshHWMPmaxPREQretries =
1806 nconf->dot11MeshHWMPmaxPREQretries;
1807 if (_chg_mesh_attr(NL80211_MESHCONF_PATH_REFRESH_TIME, mask))
1808 conf->path_refresh_time = nconf->path_refresh_time;
1809 if (_chg_mesh_attr(NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, mask))
1810 conf->min_discovery_timeout = nconf->min_discovery_timeout;
1811 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, mask))
1812 conf->dot11MeshHWMPactivePathTimeout =
1813 nconf->dot11MeshHWMPactivePathTimeout;
1814 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, mask))
1815 conf->dot11MeshHWMPpreqMinInterval =
1816 nconf->dot11MeshHWMPpreqMinInterval;
dca7e943
TP
1817 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL, mask))
1818 conf->dot11MeshHWMPperrMinInterval =
1819 nconf->dot11MeshHWMPperrMinInterval;
93da9cc1 1820 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
1821 mask))
1822 conf->dot11MeshHWMPnetDiameterTraversalTime =
1823 nconf->dot11MeshHWMPnetDiameterTraversalTime;
63c5723b
RP
1824 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ROOTMODE, mask)) {
1825 conf->dot11MeshHWMPRootMode = nconf->dot11MeshHWMPRootMode;
1826 ieee80211_mesh_root_setup(ifmsh);
1827 }
16dd7267 1828 if (_chg_mesh_attr(NL80211_MESHCONF_GATE_ANNOUNCEMENTS, mask)) {
c6133661
TP
1829 /* our current gate announcement implementation rides on root
1830 * announcements, so require this ifmsh to also be a root node
1831 * */
1832 if (nconf->dot11MeshGateAnnouncementProtocol &&
dbb912cd
CYY
1833 !(conf->dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)) {
1834 conf->dot11MeshHWMPRootMode = IEEE80211_PROACTIVE_RANN;
c6133661
TP
1835 ieee80211_mesh_root_setup(ifmsh);
1836 }
16dd7267
JC
1837 conf->dot11MeshGateAnnouncementProtocol =
1838 nconf->dot11MeshGateAnnouncementProtocol;
1839 }
a4f606ea 1840 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_RANN_INTERVAL, mask))
0507e159
JC
1841 conf->dot11MeshHWMPRannInterval =
1842 nconf->dot11MeshHWMPRannInterval;
94f90656
CYY
1843 if (_chg_mesh_attr(NL80211_MESHCONF_FORWARDING, mask))
1844 conf->dot11MeshForwarding = nconf->dot11MeshForwarding;
55335137
AN
1845 if (_chg_mesh_attr(NL80211_MESHCONF_RSSI_THRESHOLD, mask)) {
1846 /* our RSSI threshold implementation is supported only for
1847 * devices that report signal in dBm.
1848 */
1849 if (!(sdata->local->hw.flags & IEEE80211_HW_SIGNAL_DBM))
1850 return -ENOTSUPP;
1851 conf->rssi_threshold = nconf->rssi_threshold;
1852 }
70c33eaa
AN
1853 if (_chg_mesh_attr(NL80211_MESHCONF_HT_OPMODE, mask)) {
1854 conf->ht_opmode = nconf->ht_opmode;
1855 sdata->vif.bss_conf.ht_operation_mode = nconf->ht_opmode;
1856 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_HT);
1857 }
ac1073a6
CYY
1858 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT, mask))
1859 conf->dot11MeshHWMPactivePathToRootTimeout =
1860 nconf->dot11MeshHWMPactivePathToRootTimeout;
1861 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ROOT_INTERVAL, mask))
1862 conf->dot11MeshHWMProotInterval =
1863 nconf->dot11MeshHWMProotInterval;
728b19e5
CYY
1864 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL, mask))
1865 conf->dot11MeshHWMPconfirmationInterval =
1866 nconf->dot11MeshHWMPconfirmationInterval;
3f52b7e3
MP
1867 if (_chg_mesh_attr(NL80211_MESHCONF_POWER_MODE, mask)) {
1868 conf->power_mode = nconf->power_mode;
1869 ieee80211_mps_local_status_update(sdata);
1870 }
2b5e1967 1871 if (_chg_mesh_attr(NL80211_MESHCONF_AWAKE_WINDOW, mask))
3f52b7e3
MP
1872 conf->dot11MeshAwakeWindowDuration =
1873 nconf->dot11MeshAwakeWindowDuration;
2b5e1967 1874 ieee80211_mbss_info_change_notify(sdata, BSS_CHANGED_BEACON);
93da9cc1 1875 return 0;
1876}
1877
29cbe68c
JB
1878static int ieee80211_join_mesh(struct wiphy *wiphy, struct net_device *dev,
1879 const struct mesh_config *conf,
1880 const struct mesh_setup *setup)
1881{
1882 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1883 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
c80d545d 1884 int err;
29cbe68c 1885
c80d545d
JC
1886 memcpy(&ifmsh->mshcfg, conf, sizeof(struct mesh_config));
1887 err = copy_mesh_setup(ifmsh, setup);
1888 if (err)
1889 return err;
cc1d2806 1890
04ecd257
JB
1891 /* can mesh use other SMPS modes? */
1892 sdata->smps_mode = IEEE80211_SMPS_OFF;
1893 sdata->needed_rx_chains = sdata->local->rx_chains;
1894
4bf88530 1895 err = ieee80211_vif_use_channel(sdata, &setup->chandef,
55de908a 1896 IEEE80211_CHANCTX_SHARED);
cc1d2806
JB
1897 if (err)
1898 return err;
1899
2b5e1967 1900 return ieee80211_start_mesh(sdata);
29cbe68c
JB
1901}
1902
1903static int ieee80211_leave_mesh(struct wiphy *wiphy, struct net_device *dev)
1904{
1905 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1906
1907 ieee80211_stop_mesh(sdata);
55de908a 1908 ieee80211_vif_release_channel(sdata);
29cbe68c
JB
1909
1910 return 0;
1911}
c5dd9c2b
LCC
1912#endif
1913
9f1ba906
JM
1914static int ieee80211_change_bss(struct wiphy *wiphy,
1915 struct net_device *dev,
1916 struct bss_parameters *params)
1917{
55de908a
JB
1918 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1919 enum ieee80211_band band;
9f1ba906
JM
1920 u32 changed = 0;
1921
55de908a
JB
1922 if (!rtnl_dereference(sdata->u.ap.beacon))
1923 return -ENOENT;
1924
1925 band = ieee80211_get_sdata_band(sdata);
9f1ba906 1926
9f1ba906 1927 if (params->use_cts_prot >= 0) {
bda3933a 1928 sdata->vif.bss_conf.use_cts_prot = params->use_cts_prot;
9f1ba906
JM
1929 changed |= BSS_CHANGED_ERP_CTS_PROT;
1930 }
1931 if (params->use_short_preamble >= 0) {
bda3933a 1932 sdata->vif.bss_conf.use_short_preamble =
9f1ba906
JM
1933 params->use_short_preamble;
1934 changed |= BSS_CHANGED_ERP_PREAMBLE;
1935 }
43d35343
FF
1936
1937 if (!sdata->vif.bss_conf.use_short_slot &&
55de908a 1938 band == IEEE80211_BAND_5GHZ) {
43d35343
FF
1939 sdata->vif.bss_conf.use_short_slot = true;
1940 changed |= BSS_CHANGED_ERP_SLOT;
1941 }
1942
9f1ba906 1943 if (params->use_short_slot_time >= 0) {
bda3933a 1944 sdata->vif.bss_conf.use_short_slot =
9f1ba906
JM
1945 params->use_short_slot_time;
1946 changed |= BSS_CHANGED_ERP_SLOT;
1947 }
1948
90c97a04
JM
1949 if (params->basic_rates) {
1950 int i, j;
1951 u32 rates = 0;
55de908a 1952 struct ieee80211_supported_band *sband = wiphy->bands[band];
90c97a04
JM
1953
1954 for (i = 0; i < params->basic_rates_len; i++) {
1955 int rate = (params->basic_rates[i] & 0x7f) * 5;
1956 for (j = 0; j < sband->n_bitrates; j++) {
1957 if (sband->bitrates[j].bitrate == rate)
1958 rates |= BIT(j);
1959 }
1960 }
1961 sdata->vif.bss_conf.basic_rates = rates;
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 */
0f78231b 2381 __ieee80211_request_smps(sdata, sdata->u.mgd.req_smps);
0f78231b 2382
bc92afd9
JB
2383 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
2384 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2385
2386 ieee80211_recalc_ps(local, -1);
ab095877 2387 ieee80211_recalc_ps_vif(sdata);
bc92afd9
JB
2388
2389 return 0;
2390}
2391
a97c13c3
JO
2392static int ieee80211_set_cqm_rssi_config(struct wiphy *wiphy,
2393 struct net_device *dev,
2394 s32 rssi_thold, u32 rssi_hyst)
2395{
2396 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
a97c13c3
JO
2397 struct ieee80211_vif *vif = &sdata->vif;
2398 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
2399
a97c13c3
JO
2400 if (rssi_thold == bss_conf->cqm_rssi_thold &&
2401 rssi_hyst == bss_conf->cqm_rssi_hyst)
2402 return 0;
2403
2404 bss_conf->cqm_rssi_thold = rssi_thold;
2405 bss_conf->cqm_rssi_hyst = rssi_hyst;
2406
2407 /* tell the driver upon association, unless already associated */
ea086359
JB
2408 if (sdata->u.mgd.associated &&
2409 sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)
a97c13c3
JO
2410 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_CQM);
2411
2412 return 0;
2413}
2414
9930380f
JB
2415static int ieee80211_set_bitrate_mask(struct wiphy *wiphy,
2416 struct net_device *dev,
2417 const u8 *addr,
2418 const struct cfg80211_bitrate_mask *mask)
2419{
2420 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2421 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
bdbfd6b5 2422 int i, ret;
2c7e6bc9 2423
554a43d5
EP
2424 if (!ieee80211_sdata_running(sdata))
2425 return -ENETDOWN;
2426
bdbfd6b5
SM
2427 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) {
2428 ret = drv_set_bitrate_mask(local, sdata, mask);
2429 if (ret)
2430 return ret;
2431 }
9930380f 2432
19468413 2433 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
2ffbe6d3
FF
2434 struct ieee80211_supported_band *sband = wiphy->bands[i];
2435 int j;
2436
37eb0b16 2437 sdata->rc_rateidx_mask[i] = mask->control[i].legacy;
19468413
SW
2438 memcpy(sdata->rc_rateidx_mcs_mask[i], mask->control[i].mcs,
2439 sizeof(mask->control[i].mcs));
2ffbe6d3
FF
2440
2441 sdata->rc_has_mcs_mask[i] = false;
2442 if (!sband)
2443 continue;
2444
2445 for (j = 0; j < IEEE80211_HT_MCS_MASK_LEN; j++)
2446 if (~sdata->rc_rateidx_mcs_mask[i][j]) {
2447 sdata->rc_has_mcs_mask[i] = true;
2448 break;
2449 }
19468413 2450 }
9930380f 2451
37eb0b16 2452 return 0;
9930380f
JB
2453}
2454
2eb278e0
JB
2455static int ieee80211_start_roc_work(struct ieee80211_local *local,
2456 struct ieee80211_sub_if_data *sdata,
2457 struct ieee80211_channel *channel,
2eb278e0 2458 unsigned int duration, u64 *cookie,
d339d5ca
IP
2459 struct sk_buff *txskb,
2460 enum ieee80211_roc_type type)
2eb278e0
JB
2461{
2462 struct ieee80211_roc_work *roc, *tmp;
2463 bool queued = false;
21f83589 2464 int ret;
21f83589
JB
2465
2466 lockdep_assert_held(&local->mtx);
2467
fe57d9f5
JB
2468 if (local->use_chanctx && !local->ops->remain_on_channel)
2469 return -EOPNOTSUPP;
2470
2eb278e0
JB
2471 roc = kzalloc(sizeof(*roc), GFP_KERNEL);
2472 if (!roc)
2473 return -ENOMEM;
2474
2475 roc->chan = channel;
2eb278e0
JB
2476 roc->duration = duration;
2477 roc->req_duration = duration;
2478 roc->frame = txskb;
d339d5ca 2479 roc->type = type;
2eb278e0
JB
2480 roc->mgmt_tx_cookie = (unsigned long)txskb;
2481 roc->sdata = sdata;
2482 INIT_DELAYED_WORK(&roc->work, ieee80211_sw_roc_work);
2483 INIT_LIST_HEAD(&roc->dependents);
2484
2485 /* if there's one pending or we're scanning, queue this one */
164eb02d
SW
2486 if (!list_empty(&local->roc_list) ||
2487 local->scanning || local->radar_detect_enabled)
2eb278e0
JB
2488 goto out_check_combine;
2489
2490 /* if not HW assist, just queue & schedule work */
2491 if (!local->ops->remain_on_channel) {
2492 ieee80211_queue_delayed_work(&local->hw, &roc->work, 0);
2493 goto out_queue;
2494 }
2495
2496 /* otherwise actually kick it off here (for error handling) */
2497
2498 /*
2499 * If the duration is zero, then the driver
2500 * wouldn't actually do anything. Set it to
2501 * 10 for now.
2502 *
2503 * TODO: cancel the off-channel operation
2504 * when we get the SKB's TX status and
2505 * the wait time was zero before.
2506 */
2507 if (!duration)
2508 duration = 10;
2509
d339d5ca 2510 ret = drv_remain_on_channel(local, sdata, channel, duration, type);
21f83589 2511 if (ret) {
2eb278e0
JB
2512 kfree(roc);
2513 return ret;
21f83589
JB
2514 }
2515
2eb278e0
JB
2516 roc->started = true;
2517 goto out_queue;
2518
2519 out_check_combine:
2520 list_for_each_entry(tmp, &local->roc_list, list) {
42d97a59 2521 if (tmp->chan != channel || tmp->sdata != sdata)
2eb278e0
JB
2522 continue;
2523
2524 /*
2525 * Extend this ROC if possible:
2526 *
2527 * If it hasn't started yet, just increase the duration
2528 * and add the new one to the list of dependents.
d339d5ca
IP
2529 * If the type of the new ROC has higher priority, modify the
2530 * type of the previous one to match that of the new one.
2eb278e0
JB
2531 */
2532 if (!tmp->started) {
2533 list_add_tail(&roc->list, &tmp->dependents);
2534 tmp->duration = max(tmp->duration, roc->duration);
d339d5ca 2535 tmp->type = max(tmp->type, roc->type);
2eb278e0
JB
2536 queued = true;
2537 break;
2538 }
2539
2540 /* If it has already started, it's more difficult ... */
2541 if (local->ops->remain_on_channel) {
2542 unsigned long j = jiffies;
2543
2544 /*
2545 * In the offloaded ROC case, if it hasn't begun, add
2546 * this new one to the dependent list to be handled
d339d5ca 2547 * when the master one begins. If it has begun,
2eb278e0
JB
2548 * check that there's still a minimum time left and
2549 * if so, start this one, transmitting the frame, but
d339d5ca 2550 * add it to the list directly after this one with
2eb278e0
JB
2551 * a reduced time so we'll ask the driver to execute
2552 * it right after finishing the previous one, in the
2553 * hope that it'll also be executed right afterwards,
2554 * effectively extending the old one.
2555 * If there's no minimum time left, just add it to the
2556 * normal list.
d339d5ca
IP
2557 * TODO: the ROC type is ignored here, assuming that it
2558 * is better to immediately use the current ROC.
2eb278e0
JB
2559 */
2560 if (!tmp->hw_begun) {
2561 list_add_tail(&roc->list, &tmp->dependents);
2562 queued = true;
2563 break;
2564 }
2565
2566 if (time_before(j + IEEE80211_ROC_MIN_LEFT,
2567 tmp->hw_start_time +
2568 msecs_to_jiffies(tmp->duration))) {
2569 int new_dur;
2570
2571 ieee80211_handle_roc_started(roc);
2572
2573 new_dur = roc->duration -
2574 jiffies_to_msecs(tmp->hw_start_time +
2575 msecs_to_jiffies(
2576 tmp->duration) -
2577 j);
2578
2579 if (new_dur > 0) {
2580 /* add right after tmp */
2581 list_add(&roc->list, &tmp->list);
2582 } else {
2583 list_add_tail(&roc->list,
2584 &tmp->dependents);
2585 }
2586 queued = true;
2587 }
2588 } else if (del_timer_sync(&tmp->work.timer)) {
2589 unsigned long new_end;
2590
2591 /*
2592 * In the software ROC case, cancel the timer, if
2593 * that fails then the finish work is already
2594 * queued/pending and thus we queue the new ROC
2595 * normally, if that succeeds then we can extend
2596 * the timer duration and TX the frame (if any.)
2597 */
2598
2599 list_add_tail(&roc->list, &tmp->dependents);
2600 queued = true;
2601
2602 new_end = jiffies + msecs_to_jiffies(roc->duration);
2603
2604 /* ok, it was started & we canceled timer */
2605 if (time_after(new_end, tmp->work.timer.expires))
2606 mod_timer(&tmp->work.timer, new_end);
2607 else
2608 add_timer(&tmp->work.timer);
2609
2610 ieee80211_handle_roc_started(roc);
2611 }
2612 break;
2613 }
2614
2615 out_queue:
2616 if (!queued)
2617 list_add_tail(&roc->list, &local->roc_list);
2618
2619 /*
50febf6a 2620 * cookie is either the roc cookie (for normal roc)
2eb278e0
JB
2621 * or the SKB (for mgmt TX)
2622 */
50febf6a
JB
2623 if (!txskb) {
2624 /* local->mtx protects this */
2625 local->roc_cookie_counter++;
2626 roc->cookie = local->roc_cookie_counter;
2627 /* wow, you wrapped 64 bits ... more likely a bug */
2628 if (WARN_ON(roc->cookie == 0)) {
2629 roc->cookie = 1;
2630 local->roc_cookie_counter++;
2631 }
2632 *cookie = roc->cookie;
2633 } else {
2eb278e0 2634 *cookie = (unsigned long)txskb;
50febf6a 2635 }
2eb278e0
JB
2636
2637 return 0;
21f83589
JB
2638}
2639
b8bc4b0a 2640static int ieee80211_remain_on_channel(struct wiphy *wiphy,
71bbc994 2641 struct wireless_dev *wdev,
b8bc4b0a 2642 struct ieee80211_channel *chan,
b8bc4b0a
JB
2643 unsigned int duration,
2644 u64 *cookie)
2645{
71bbc994 2646 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
21f83589 2647 struct ieee80211_local *local = sdata->local;
2eb278e0 2648 int ret;
21f83589 2649
2eb278e0 2650 mutex_lock(&local->mtx);
42d97a59 2651 ret = ieee80211_start_roc_work(local, sdata, chan,
d339d5ca
IP
2652 duration, cookie, NULL,
2653 IEEE80211_ROC_TYPE_NORMAL);
2eb278e0 2654 mutex_unlock(&local->mtx);
b8bc4b0a 2655
2eb278e0 2656 return ret;
b8bc4b0a
JB
2657}
2658
2eb278e0
JB
2659static int ieee80211_cancel_roc(struct ieee80211_local *local,
2660 u64 cookie, bool mgmt_tx)
21f83589 2661{
2eb278e0 2662 struct ieee80211_roc_work *roc, *tmp, *found = NULL;
21f83589
JB
2663 int ret;
2664
2eb278e0
JB
2665 mutex_lock(&local->mtx);
2666 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
e979e33c
JB
2667 struct ieee80211_roc_work *dep, *tmp2;
2668
2669 list_for_each_entry_safe(dep, tmp2, &roc->dependents, list) {
50febf6a 2670 if (!mgmt_tx && dep->cookie != cookie)
e979e33c
JB
2671 continue;
2672 else if (mgmt_tx && dep->mgmt_tx_cookie != cookie)
2673 continue;
2674 /* found dependent item -- just remove it */
2675 list_del(&dep->list);
2676 mutex_unlock(&local->mtx);
2677
3fbd45ca 2678 ieee80211_roc_notify_destroy(dep, true);
e979e33c
JB
2679 return 0;
2680 }
2681
50febf6a 2682 if (!mgmt_tx && roc->cookie != cookie)
2eb278e0
JB
2683 continue;
2684 else if (mgmt_tx && roc->mgmt_tx_cookie != cookie)
2685 continue;
21f83589 2686
2eb278e0
JB
2687 found = roc;
2688 break;
2689 }
21f83589 2690
2eb278e0
JB
2691 if (!found) {
2692 mutex_unlock(&local->mtx);
2693 return -ENOENT;
2694 }
21f83589 2695
e979e33c
JB
2696 /*
2697 * We found the item to cancel, so do that. Note that it
2698 * may have dependents, which we also cancel (and send
2699 * the expired signal for.) Not doing so would be quite
2700 * tricky here, but we may need to fix it later.
2701 */
2702
2eb278e0
JB
2703 if (local->ops->remain_on_channel) {
2704 if (found->started) {
2705 ret = drv_cancel_remain_on_channel(local);
2706 if (WARN_ON_ONCE(ret)) {
2707 mutex_unlock(&local->mtx);
2708 return ret;
2709 }
2710 }
21f83589 2711
2eb278e0 2712 list_del(&found->list);
21f83589 2713
0f6b3f59
JB
2714 if (found->started)
2715 ieee80211_start_next_roc(local);
2eb278e0 2716 mutex_unlock(&local->mtx);
21f83589 2717
3fbd45ca 2718 ieee80211_roc_notify_destroy(found, true);
2eb278e0
JB
2719 } else {
2720 /* work may be pending so use it all the time */
2721 found->abort = true;
2722 ieee80211_queue_delayed_work(&local->hw, &found->work, 0);
21f83589 2723
21f83589
JB
2724 mutex_unlock(&local->mtx);
2725
2eb278e0
JB
2726 /* work will clean up etc */
2727 flush_delayed_work(&found->work);
3fbd45ca
JB
2728 WARN_ON(!found->to_be_freed);
2729 kfree(found);
21f83589 2730 }
b8bc4b0a 2731
2eb278e0 2732 return 0;
b8bc4b0a
JB
2733}
2734
2eb278e0 2735static int ieee80211_cancel_remain_on_channel(struct wiphy *wiphy,
71bbc994 2736 struct wireless_dev *wdev,
2eb278e0 2737 u64 cookie)
f30221e4 2738{
71bbc994 2739 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2eb278e0 2740 struct ieee80211_local *local = sdata->local;
f30221e4 2741
2eb278e0 2742 return ieee80211_cancel_roc(local, cookie, false);
f30221e4
JB
2743}
2744
164eb02d
SW
2745static int ieee80211_start_radar_detection(struct wiphy *wiphy,
2746 struct net_device *dev,
2747 struct cfg80211_chan_def *chandef)
2748{
2749 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2750 struct ieee80211_local *local = sdata->local;
2751 unsigned long timeout;
2752 int err;
2753
2754 if (!list_empty(&local->roc_list) || local->scanning)
2755 return -EBUSY;
2756
2757 /* whatever, but channel contexts should not complain about that one */
2758 sdata->smps_mode = IEEE80211_SMPS_OFF;
2759 sdata->needed_rx_chains = local->rx_chains;
2760 sdata->radar_required = true;
2761
2762 mutex_lock(&local->iflist_mtx);
2763 err = ieee80211_vif_use_channel(sdata, chandef,
2764 IEEE80211_CHANCTX_SHARED);
2765 mutex_unlock(&local->iflist_mtx);
2766 if (err)
2767 return err;
2768
2769 timeout = msecs_to_jiffies(IEEE80211_DFS_MIN_CAC_TIME_MS);
2770 ieee80211_queue_delayed_work(&sdata->local->hw,
2771 &sdata->dfs_cac_timer_work, timeout);
2772
2773 return 0;
2774}
2775
71bbc994 2776static int ieee80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
f7ca38df 2777 struct ieee80211_channel *chan, bool offchan,
42d97a59
JB
2778 unsigned int wait, const u8 *buf, size_t len,
2779 bool no_cck, bool dont_wait_for_ack, u64 *cookie)
026331c4 2780{
71bbc994 2781 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
9d38d85d
JB
2782 struct ieee80211_local *local = sdata->local;
2783 struct sk_buff *skb;
2784 struct sta_info *sta;
2785 const struct ieee80211_mgmt *mgmt = (void *)buf;
2eb278e0 2786 bool need_offchan = false;
e247bd90 2787 u32 flags;
2eb278e0 2788 int ret;
f7ca38df 2789
e247bd90
JB
2790 if (dont_wait_for_ack)
2791 flags = IEEE80211_TX_CTL_NO_ACK;
2792 else
2793 flags = IEEE80211_TX_INTFL_NL80211_FRAME_TX |
2794 IEEE80211_TX_CTL_REQ_TX_STATUS;
2795
aad14ceb
RM
2796 if (no_cck)
2797 flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
2798
9d38d85d
JB
2799 switch (sdata->vif.type) {
2800 case NL80211_IFTYPE_ADHOC:
2eb278e0
JB
2801 if (!sdata->vif.bss_conf.ibss_joined)
2802 need_offchan = true;
2803 /* fall through */
2804#ifdef CONFIG_MAC80211_MESH
2805 case NL80211_IFTYPE_MESH_POINT:
2806 if (ieee80211_vif_is_mesh(&sdata->vif) &&
2807 !sdata->u.mesh.mesh_id_len)
2808 need_offchan = true;
2809 /* fall through */
2810#endif
663fcafd
JB
2811 case NL80211_IFTYPE_AP:
2812 case NL80211_IFTYPE_AP_VLAN:
2813 case NL80211_IFTYPE_P2P_GO:
2eb278e0
JB
2814 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2815 !ieee80211_vif_is_mesh(&sdata->vif) &&
2816 !rcu_access_pointer(sdata->bss->beacon))
2817 need_offchan = true;
663fcafd
JB
2818 if (!ieee80211_is_action(mgmt->frame_control) ||
2819 mgmt->u.action.category == WLAN_CATEGORY_PUBLIC)
9d38d85d
JB
2820 break;
2821 rcu_read_lock();
2822 sta = sta_info_get(sdata, mgmt->da);
2823 rcu_read_unlock();
2824 if (!sta)
2825 return -ENOLINK;
2826 break;
2827 case NL80211_IFTYPE_STATION:
663fcafd 2828 case NL80211_IFTYPE_P2P_CLIENT:
2eb278e0
JB
2829 if (!sdata->u.mgd.associated)
2830 need_offchan = true;
9d38d85d 2831 break;
f142c6b9
JB
2832 case NL80211_IFTYPE_P2P_DEVICE:
2833 need_offchan = true;
2834 break;
9d38d85d
JB
2835 default:
2836 return -EOPNOTSUPP;
2837 }
2838
2eb278e0
JB
2839 mutex_lock(&local->mtx);
2840
2841 /* Check if the operating channel is the requested channel */
2842 if (!need_offchan) {
55de908a
JB
2843 struct ieee80211_chanctx_conf *chanctx_conf;
2844
2845 rcu_read_lock();
2846 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2847
42d97a59 2848 if (chanctx_conf)
4bf88530 2849 need_offchan = chan != chanctx_conf->def.chan;
42d97a59 2850 else
2eb278e0 2851 need_offchan = true;
55de908a 2852 rcu_read_unlock();
2eb278e0
JB
2853 }
2854
2855 if (need_offchan && !offchan) {
2856 ret = -EBUSY;
2857 goto out_unlock;
2858 }
2859
9d38d85d 2860 skb = dev_alloc_skb(local->hw.extra_tx_headroom + len);
2eb278e0
JB
2861 if (!skb) {
2862 ret = -ENOMEM;
2863 goto out_unlock;
2864 }
9d38d85d
JB
2865 skb_reserve(skb, local->hw.extra_tx_headroom);
2866
2867 memcpy(skb_put(skb, len), buf, len);
2868
2869 IEEE80211_SKB_CB(skb)->flags = flags;
2870
2871 skb->dev = sdata->dev;
9d38d85d 2872
2eb278e0 2873 if (!need_offchan) {
7f9f78ab 2874 *cookie = (unsigned long) skb;
f30221e4 2875 ieee80211_tx_skb(sdata, skb);
2eb278e0
JB
2876 ret = 0;
2877 goto out_unlock;
f30221e4
JB
2878 }
2879
6c17b77b
SF
2880 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_TX_OFFCHAN |
2881 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
2eb278e0
JB
2882 if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
2883 IEEE80211_SKB_CB(skb)->hw_queue =
2884 local->hw.offchannel_tx_hw_queue;
f30221e4 2885
2eb278e0 2886 /* This will handle all kinds of coalescing and immediate TX */
42d97a59 2887 ret = ieee80211_start_roc_work(local, sdata, chan,
d339d5ca
IP
2888 wait, cookie, skb,
2889 IEEE80211_ROC_TYPE_MGMT_TX);
2eb278e0
JB
2890 if (ret)
2891 kfree_skb(skb);
2892 out_unlock:
2893 mutex_unlock(&local->mtx);
2894 return ret;
026331c4
JM
2895}
2896
f30221e4 2897static int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
71bbc994 2898 struct wireless_dev *wdev,
f30221e4
JB
2899 u64 cookie)
2900{
71bbc994 2901 struct ieee80211_local *local = wiphy_priv(wiphy);
f30221e4 2902
2eb278e0 2903 return ieee80211_cancel_roc(local, cookie, true);
f30221e4
JB
2904}
2905
7be5086d 2906static void ieee80211_mgmt_frame_register(struct wiphy *wiphy,
71bbc994 2907 struct wireless_dev *wdev,
7be5086d
JB
2908 u16 frame_type, bool reg)
2909{
2910 struct ieee80211_local *local = wiphy_priv(wiphy);
71bbc994 2911 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
7be5086d 2912
6abe0563
WH
2913 switch (frame_type) {
2914 case IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_AUTH:
2915 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
2916 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
7be5086d 2917
6abe0563
WH
2918 if (reg)
2919 ifibss->auth_frame_registrations++;
2920 else
2921 ifibss->auth_frame_registrations--;
2922 }
2923 break;
2924 case IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_PROBE_REQ:
2925 if (reg)
2926 local->probe_req_reg++;
2927 else
2928 local->probe_req_reg--;
7be5086d 2929
35f5149e
FF
2930 if (!local->open_count)
2931 break;
2932
6abe0563
WH
2933 ieee80211_queue_work(&local->hw, &local->reconfig_filter);
2934 break;
2935 default:
2936 break;
2937 }
7be5086d
JB
2938}
2939
15d96753
BR
2940static int ieee80211_set_antenna(struct wiphy *wiphy, u32 tx_ant, u32 rx_ant)
2941{
2942 struct ieee80211_local *local = wiphy_priv(wiphy);
2943
2944 if (local->started)
2945 return -EOPNOTSUPP;
2946
2947 return drv_set_antenna(local, tx_ant, rx_ant);
2948}
2949
2950static int ieee80211_get_antenna(struct wiphy *wiphy, u32 *tx_ant, u32 *rx_ant)
2951{
2952 struct ieee80211_local *local = wiphy_priv(wiphy);
2953
2954 return drv_get_antenna(local, tx_ant, rx_ant);
2955}
2956
38c09159
JL
2957static int ieee80211_set_ringparam(struct wiphy *wiphy, u32 tx, u32 rx)
2958{
2959 struct ieee80211_local *local = wiphy_priv(wiphy);
2960
2961 return drv_set_ringparam(local, tx, rx);
2962}
2963
2964static void ieee80211_get_ringparam(struct wiphy *wiphy,
2965 u32 *tx, u32 *tx_max, u32 *rx, u32 *rx_max)
2966{
2967 struct ieee80211_local *local = wiphy_priv(wiphy);
2968
2969 drv_get_ringparam(local, tx, tx_max, rx, rx_max);
2970}
2971
c68f4b89
JB
2972static int ieee80211_set_rekey_data(struct wiphy *wiphy,
2973 struct net_device *dev,
2974 struct cfg80211_gtk_rekey_data *data)
2975{
2976 struct ieee80211_local *local = wiphy_priv(wiphy);
2977 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2978
2979 if (!local->ops->set_rekey_data)
2980 return -EOPNOTSUPP;
2981
2982 drv_set_rekey_data(local, sdata, data);
2983
2984 return 0;
2985}
2986
dfe018bf
AN
2987static void ieee80211_tdls_add_ext_capab(struct sk_buff *skb)
2988{
2989 u8 *pos = (void *)skb_put(skb, 7);
2990
2991 *pos++ = WLAN_EID_EXT_CAPABILITY;
2992 *pos++ = 5; /* len */
2993 *pos++ = 0x0;
2994 *pos++ = 0x0;
2995 *pos++ = 0x0;
2996 *pos++ = 0x0;
2997 *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
2998}
2999
3000static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata)
3001{
3002 struct ieee80211_local *local = sdata->local;
3003 u16 capab;
3004
3005 capab = 0;
55de908a 3006 if (ieee80211_get_sdata_band(sdata) != IEEE80211_BAND_2GHZ)
dfe018bf
AN
3007 return capab;
3008
3009 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
3010 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
3011 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
3012 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
3013
3014 return capab;
3015}
3016
3017static void ieee80211_tdls_add_link_ie(struct sk_buff *skb, u8 *src_addr,
3018 u8 *peer, u8 *bssid)
3019{
3020 struct ieee80211_tdls_lnkie *lnkid;
3021
3022 lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
3023
3024 lnkid->ie_type = WLAN_EID_LINK_ID;
3025 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
3026
3027 memcpy(lnkid->bssid, bssid, ETH_ALEN);
3028 memcpy(lnkid->init_sta, src_addr, ETH_ALEN);
3029 memcpy(lnkid->resp_sta, peer, ETH_ALEN);
3030}
3031
3032static int
3033ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
3034 u8 *peer, u8 action_code, u8 dialog_token,
3035 u16 status_code, struct sk_buff *skb)
3036{
3037 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
55de908a 3038 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
dfe018bf
AN
3039 struct ieee80211_tdls_data *tf;
3040
3041 tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
3042
3043 memcpy(tf->da, peer, ETH_ALEN);
3044 memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
3045 tf->ether_type = cpu_to_be16(ETH_P_TDLS);
3046 tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
3047
3048 switch (action_code) {
3049 case WLAN_TDLS_SETUP_REQUEST:
3050 tf->category = WLAN_CATEGORY_TDLS;
3051 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
3052
3053 skb_put(skb, sizeof(tf->u.setup_req));
3054 tf->u.setup_req.dialog_token = dialog_token;
3055 tf->u.setup_req.capability =
3056 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata));
3057
55de908a
JB
3058 ieee80211_add_srates_ie(sdata, skb, false, band);
3059 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
dfe018bf
AN
3060 ieee80211_tdls_add_ext_capab(skb);
3061 break;
3062 case WLAN_TDLS_SETUP_RESPONSE:
3063 tf->category = WLAN_CATEGORY_TDLS;
3064 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
3065
3066 skb_put(skb, sizeof(tf->u.setup_resp));
3067 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
3068 tf->u.setup_resp.dialog_token = dialog_token;
3069 tf->u.setup_resp.capability =
3070 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata));
3071
55de908a
JB
3072 ieee80211_add_srates_ie(sdata, skb, false, band);
3073 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
dfe018bf
AN
3074 ieee80211_tdls_add_ext_capab(skb);
3075 break;
3076 case WLAN_TDLS_SETUP_CONFIRM:
3077 tf->category = WLAN_CATEGORY_TDLS;
3078 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
3079
3080 skb_put(skb, sizeof(tf->u.setup_cfm));
3081 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
3082 tf->u.setup_cfm.dialog_token = dialog_token;
3083 break;
3084 case WLAN_TDLS_TEARDOWN:
3085 tf->category = WLAN_CATEGORY_TDLS;
3086 tf->action_code = WLAN_TDLS_TEARDOWN;
3087
3088 skb_put(skb, sizeof(tf->u.teardown));
3089 tf->u.teardown.reason_code = cpu_to_le16(status_code);
3090 break;
3091 case WLAN_TDLS_DISCOVERY_REQUEST:
3092 tf->category = WLAN_CATEGORY_TDLS;
3093 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
3094
3095 skb_put(skb, sizeof(tf->u.discover_req));
3096 tf->u.discover_req.dialog_token = dialog_token;
3097 break;
3098 default:
3099 return -EINVAL;
3100 }
3101
3102 return 0;
3103}
3104
3105static int
3106ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
3107 u8 *peer, u8 action_code, u8 dialog_token,
3108 u16 status_code, struct sk_buff *skb)
3109{
3110 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
55de908a 3111 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
dfe018bf
AN
3112 struct ieee80211_mgmt *mgmt;
3113
3114 mgmt = (void *)skb_put(skb, 24);
3115 memset(mgmt, 0, 24);
3116 memcpy(mgmt->da, peer, ETH_ALEN);
3117 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
3118 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
3119
3120 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3121 IEEE80211_STYPE_ACTION);
3122
3123 switch (action_code) {
3124 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
3125 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
3126 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
3127 mgmt->u.action.u.tdls_discover_resp.action_code =
3128 WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
3129 mgmt->u.action.u.tdls_discover_resp.dialog_token =
3130 dialog_token;
3131 mgmt->u.action.u.tdls_discover_resp.capability =
3132 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata));
3133
55de908a
JB
3134 ieee80211_add_srates_ie(sdata, skb, false, band);
3135 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
dfe018bf
AN
3136 ieee80211_tdls_add_ext_capab(skb);
3137 break;
3138 default:
3139 return -EINVAL;
3140 }
3141
3142 return 0;
3143}
3144
3145static int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
3146 u8 *peer, u8 action_code, u8 dialog_token,
3147 u16 status_code, const u8 *extra_ies,
3148 size_t extra_ies_len)
3149{
3150 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3151 struct ieee80211_local *local = sdata->local;
dfe018bf
AN
3152 struct sk_buff *skb = NULL;
3153 bool send_direct;
3154 int ret;
3155
3156 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
3157 return -ENOTSUPP;
3158
3159 /* make sure we are in managed mode, and associated */
3160 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
3161 !sdata->u.mgd.associated)
3162 return -EINVAL;
3163
bdcbd8e0
JB
3164 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM\n",
3165 action_code, peer);
dfe018bf
AN
3166
3167 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
3168 max(sizeof(struct ieee80211_mgmt),
3169 sizeof(struct ieee80211_tdls_data)) +
3170 50 + /* supported rates */
3171 7 + /* ext capab */
3172 extra_ies_len +
3173 sizeof(struct ieee80211_tdls_lnkie));
3174 if (!skb)
3175 return -ENOMEM;
3176
dfe018bf
AN
3177 skb_reserve(skb, local->hw.extra_tx_headroom);
3178
3179 switch (action_code) {
3180 case WLAN_TDLS_SETUP_REQUEST:
3181 case WLAN_TDLS_SETUP_RESPONSE:
3182 case WLAN_TDLS_SETUP_CONFIRM:
3183 case WLAN_TDLS_TEARDOWN:
3184 case WLAN_TDLS_DISCOVERY_REQUEST:
3185 ret = ieee80211_prep_tdls_encap_data(wiphy, dev, peer,
3186 action_code, dialog_token,
3187 status_code, skb);
3188 send_direct = false;
3189 break;
3190 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
3191 ret = ieee80211_prep_tdls_direct(wiphy, dev, peer, action_code,
3192 dialog_token, status_code,
3193 skb);
3194 send_direct = true;
3195 break;
3196 default:
3197 ret = -ENOTSUPP;
3198 break;
3199 }
3200
3201 if (ret < 0)
3202 goto fail;
3203
3204 if (extra_ies_len)
3205 memcpy(skb_put(skb, extra_ies_len), extra_ies, extra_ies_len);
3206
3207 /* the TDLS link IE is always added last */
3208 switch (action_code) {
3209 case WLAN_TDLS_SETUP_REQUEST:
3210 case WLAN_TDLS_SETUP_CONFIRM:
3211 case WLAN_TDLS_TEARDOWN:
3212 case WLAN_TDLS_DISCOVERY_REQUEST:
3213 /* we are the initiator */
3214 ieee80211_tdls_add_link_ie(skb, sdata->vif.addr, peer,
3215 sdata->u.mgd.bssid);
3216 break;
3217 case WLAN_TDLS_SETUP_RESPONSE:
3218 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
3219 /* we are the responder */
3220 ieee80211_tdls_add_link_ie(skb, peer, sdata->vif.addr,
3221 sdata->u.mgd.bssid);
3222 break;
3223 default:
3224 ret = -ENOTSUPP;
3225 goto fail;
3226 }
3227
3228 if (send_direct) {
3229 ieee80211_tx_skb(sdata, skb);
3230 return 0;
3231 }
3232
3233 /*
3234 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
3235 * we should default to AC_VI.
3236 */
3237 switch (action_code) {
3238 case WLAN_TDLS_SETUP_REQUEST:
3239 case WLAN_TDLS_SETUP_RESPONSE:
3240 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
3241 skb->priority = 2;
3242 break;
3243 default:
3244 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
3245 skb->priority = 5;
3246 break;
3247 }
3248
3249 /* disable bottom halves when entering the Tx path */
3250 local_bh_disable();
3251 ret = ieee80211_subif_start_xmit(skb, dev);
3252 local_bh_enable();
3253
3254 return ret;
3255
3256fail:
3257 dev_kfree_skb(skb);
3258 return ret;
3259}
3260
3261static int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
3262 u8 *peer, enum nl80211_tdls_operation oper)
3263{
941c93cd 3264 struct sta_info *sta;
dfe018bf
AN
3265 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3266
3267 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
3268 return -ENOTSUPP;
3269
3270 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3271 return -EINVAL;
3272
bdcbd8e0 3273 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
dfe018bf
AN
3274
3275 switch (oper) {
3276 case NL80211_TDLS_ENABLE_LINK:
941c93cd
AN
3277 rcu_read_lock();
3278 sta = sta_info_get(sdata, peer);
3279 if (!sta) {
3280 rcu_read_unlock();
3281 return -ENOLINK;
3282 }
3283
c2c98fde 3284 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
941c93cd 3285 rcu_read_unlock();
dfe018bf
AN
3286 break;
3287 case NL80211_TDLS_DISABLE_LINK:
3288 return sta_info_destroy_addr(sdata, peer);
3289 case NL80211_TDLS_TEARDOWN:
3290 case NL80211_TDLS_SETUP:
3291 case NL80211_TDLS_DISCOVERY_REQ:
3292 /* We don't support in-driver setup/teardown/discovery */
3293 return -ENOTSUPP;
3294 default:
3295 return -ENOTSUPP;
3296 }
3297
3298 return 0;
3299}
3300
06500736
JB
3301static int ieee80211_probe_client(struct wiphy *wiphy, struct net_device *dev,
3302 const u8 *peer, u64 *cookie)
3303{
3304 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3305 struct ieee80211_local *local = sdata->local;
3306 struct ieee80211_qos_hdr *nullfunc;
3307 struct sk_buff *skb;
3308 int size = sizeof(*nullfunc);
3309 __le16 fc;
3310 bool qos;
3311 struct ieee80211_tx_info *info;
3312 struct sta_info *sta;
55de908a
JB
3313 struct ieee80211_chanctx_conf *chanctx_conf;
3314 enum ieee80211_band band;
06500736
JB
3315
3316 rcu_read_lock();
55de908a
JB
3317 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3318 if (WARN_ON(!chanctx_conf)) {
3319 rcu_read_unlock();
3320 return -EINVAL;
3321 }
4bf88530 3322 band = chanctx_conf->def.chan->band;
06500736 3323 sta = sta_info_get(sdata, peer);
b4487c2d 3324 if (sta) {
06500736 3325 qos = test_sta_flag(sta, WLAN_STA_WME);
b4487c2d
JB
3326 } else {
3327 rcu_read_unlock();
06500736 3328 return -ENOLINK;
b4487c2d 3329 }
06500736
JB
3330
3331 if (qos) {
3332 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
3333 IEEE80211_STYPE_QOS_NULLFUNC |
3334 IEEE80211_FCTL_FROMDS);
3335 } else {
3336 size -= 2;
3337 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
3338 IEEE80211_STYPE_NULLFUNC |
3339 IEEE80211_FCTL_FROMDS);
3340 }
3341
3342 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
55de908a
JB
3343 if (!skb) {
3344 rcu_read_unlock();
06500736 3345 return -ENOMEM;
55de908a 3346 }
06500736
JB
3347
3348 skb->dev = dev;
3349
3350 skb_reserve(skb, local->hw.extra_tx_headroom);
3351
3352 nullfunc = (void *) skb_put(skb, size);
3353 nullfunc->frame_control = fc;
3354 nullfunc->duration_id = 0;
3355 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
3356 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
3357 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
3358 nullfunc->seq_ctrl = 0;
3359
3360 info = IEEE80211_SKB_CB(skb);
3361
3362 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
3363 IEEE80211_TX_INTFL_NL80211_FRAME_TX;
3364
3365 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
3366 skb->priority = 7;
3367 if (qos)
3368 nullfunc->qos_ctrl = cpu_to_le16(7);
3369
3370 local_bh_disable();
55de908a 3371 ieee80211_xmit(sdata, skb, band);
06500736 3372 local_bh_enable();
55de908a 3373 rcu_read_unlock();
06500736
JB
3374
3375 *cookie = (unsigned long) skb;
3376 return 0;
3377}
3378
683b6d3b
JB
3379static int ieee80211_cfg_get_channel(struct wiphy *wiphy,
3380 struct wireless_dev *wdev,
3381 struct cfg80211_chan_def *chandef)
5b7ccaf3 3382{
55de908a 3383 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
cb601ffa 3384 struct ieee80211_local *local = wiphy_priv(wiphy);
55de908a 3385 struct ieee80211_chanctx_conf *chanctx_conf;
683b6d3b 3386 int ret = -ENODATA;
55de908a
JB
3387
3388 rcu_read_lock();
feda3027
JB
3389 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3390 if (chanctx_conf) {
3391 *chandef = chanctx_conf->def;
3392 ret = 0;
3393 } else if (local->open_count > 0 &&
3394 local->open_count == local->monitors &&
3395 sdata->vif.type == NL80211_IFTYPE_MONITOR) {
3396 if (local->use_chanctx)
3397 *chandef = local->monitor_chandef;
3398 else
675a0b04 3399 *chandef = local->_oper_chandef;
683b6d3b 3400 ret = 0;
55de908a
JB
3401 }
3402 rcu_read_unlock();
5b7ccaf3 3403
683b6d3b 3404 return ret;
5b7ccaf3
JB
3405}
3406
6d52563f
JB
3407#ifdef CONFIG_PM
3408static void ieee80211_set_wakeup(struct wiphy *wiphy, bool enabled)
3409{
3410 drv_set_wakeup(wiphy_priv(wiphy), enabled);
3411}
3412#endif
3413
f0706e82
JB
3414struct cfg80211_ops mac80211_config_ops = {
3415 .add_virtual_intf = ieee80211_add_iface,
3416 .del_virtual_intf = ieee80211_del_iface,
42613db7 3417 .change_virtual_intf = ieee80211_change_iface,
f142c6b9
JB
3418 .start_p2p_device = ieee80211_start_p2p_device,
3419 .stop_p2p_device = ieee80211_stop_p2p_device,
e8cbb4cb
JB
3420 .add_key = ieee80211_add_key,
3421 .del_key = ieee80211_del_key,
62da92fb 3422 .get_key = ieee80211_get_key,
e8cbb4cb 3423 .set_default_key = ieee80211_config_default_key,
3cfcf6ac 3424 .set_default_mgmt_key = ieee80211_config_default_mgmt_key,
8860020e
JB
3425 .start_ap = ieee80211_start_ap,
3426 .change_beacon = ieee80211_change_beacon,
3427 .stop_ap = ieee80211_stop_ap,
4fd6931e
JB
3428 .add_station = ieee80211_add_station,
3429 .del_station = ieee80211_del_station,
3430 .change_station = ieee80211_change_station,
7bbdd2d9 3431 .get_station = ieee80211_get_station,
c5dd9c2b 3432 .dump_station = ieee80211_dump_station,
1289723e 3433 .dump_survey = ieee80211_dump_survey,
c5dd9c2b
LCC
3434#ifdef CONFIG_MAC80211_MESH
3435 .add_mpath = ieee80211_add_mpath,
3436 .del_mpath = ieee80211_del_mpath,
3437 .change_mpath = ieee80211_change_mpath,
3438 .get_mpath = ieee80211_get_mpath,
3439 .dump_mpath = ieee80211_dump_mpath,
24bdd9f4
JC
3440 .update_mesh_config = ieee80211_update_mesh_config,
3441 .get_mesh_config = ieee80211_get_mesh_config,
29cbe68c
JB
3442 .join_mesh = ieee80211_join_mesh,
3443 .leave_mesh = ieee80211_leave_mesh,
c5dd9c2b 3444#endif
9f1ba906 3445 .change_bss = ieee80211_change_bss,
31888487 3446 .set_txq_params = ieee80211_set_txq_params,
e8c9bd5b 3447 .set_monitor_channel = ieee80211_set_monitor_channel,
665af4fc
BC
3448 .suspend = ieee80211_suspend,
3449 .resume = ieee80211_resume,
2a519311 3450 .scan = ieee80211_scan,
79f460ca
LC
3451 .sched_scan_start = ieee80211_sched_scan_start,
3452 .sched_scan_stop = ieee80211_sched_scan_stop,
636a5d36
JM
3453 .auth = ieee80211_auth,
3454 .assoc = ieee80211_assoc,
3455 .deauth = ieee80211_deauth,
3456 .disassoc = ieee80211_disassoc,
af8cdcd8
JB
3457 .join_ibss = ieee80211_join_ibss,
3458 .leave_ibss = ieee80211_leave_ibss,
391e53e3 3459 .set_mcast_rate = ieee80211_set_mcast_rate,
b9a5f8ca 3460 .set_wiphy_params = ieee80211_set_wiphy_params,
7643a2c3
JB
3461 .set_tx_power = ieee80211_set_tx_power,
3462 .get_tx_power = ieee80211_get_tx_power,
ab737a4f 3463 .set_wds_peer = ieee80211_set_wds_peer,
1f87f7d3 3464 .rfkill_poll = ieee80211_rfkill_poll,
aff89a9b 3465 CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
71063f0e 3466 CFG80211_TESTMODE_DUMP(ieee80211_testmode_dump)
bc92afd9 3467 .set_power_mgmt = ieee80211_set_power_mgmt,
9930380f 3468 .set_bitrate_mask = ieee80211_set_bitrate_mask,
b8bc4b0a
JB
3469 .remain_on_channel = ieee80211_remain_on_channel,
3470 .cancel_remain_on_channel = ieee80211_cancel_remain_on_channel,
2e161f78 3471 .mgmt_tx = ieee80211_mgmt_tx,
f30221e4 3472 .mgmt_tx_cancel_wait = ieee80211_mgmt_tx_cancel_wait,
a97c13c3 3473 .set_cqm_rssi_config = ieee80211_set_cqm_rssi_config,
7be5086d 3474 .mgmt_frame_register = ieee80211_mgmt_frame_register,
15d96753
BR
3475 .set_antenna = ieee80211_set_antenna,
3476 .get_antenna = ieee80211_get_antenna,
38c09159
JL
3477 .set_ringparam = ieee80211_set_ringparam,
3478 .get_ringparam = ieee80211_get_ringparam,
c68f4b89 3479 .set_rekey_data = ieee80211_set_rekey_data,
dfe018bf
AN
3480 .tdls_oper = ieee80211_tdls_oper,
3481 .tdls_mgmt = ieee80211_tdls_mgmt,
06500736 3482 .probe_client = ieee80211_probe_client,
b53be792 3483 .set_noack_map = ieee80211_set_noack_map,
6d52563f
JB
3484#ifdef CONFIG_PM
3485 .set_wakeup = ieee80211_set_wakeup,
3486#endif
b1ab7925
BG
3487 .get_et_sset_count = ieee80211_get_et_sset_count,
3488 .get_et_stats = ieee80211_get_et_stats,
3489 .get_et_strings = ieee80211_get_et_strings,
5b7ccaf3 3490 .get_channel = ieee80211_cfg_get_channel,
164eb02d 3491 .start_radar_detection = ieee80211_start_radar_detection,
f0706e82 3492};
This page took 0.799999 seconds and 5 git commands to generate.