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