Linux 3.12.28
[linux/fpc-iii.git] / net / mac80211 / cfg.c
blobc6d417a3885f4f93f3c7a2e24365242c87db304f
1 /*
2 * mac80211 configuration hooks for cfg80211
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
6 * This file is GPLv2 as found in COPYING.
7 */
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"
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)
29 struct ieee80211_local *local = wiphy_priv(wiphy);
30 struct wireless_dev *wdev;
31 struct ieee80211_sub_if_data *sdata;
32 int err;
34 err = ieee80211_if_add(local, name, &wdev, type, params);
35 if (err)
36 return ERR_PTR(err);
38 if (type == NL80211_IFTYPE_MONITOR && flags) {
39 sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
40 sdata->u.mntr_flags = *flags;
43 return wdev;
46 static int ieee80211_del_iface(struct wiphy *wiphy, struct wireless_dev *wdev)
48 ieee80211_if_remove(IEEE80211_WDEV_TO_SUB_IF(wdev));
50 return 0;
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)
58 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
59 int ret;
61 ret = ieee80211_if_change_type(sdata, type);
62 if (ret)
63 return ret;
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;
72 if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags) {
73 struct ieee80211_local *local = sdata->local;
75 if (ieee80211_sdata_running(sdata)) {
76 u32 mask = MONITOR_FLAG_COOK_FRAMES |
77 MONITOR_FLAG_ACTIVE;
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
88 if ((*flags & mask) != (sdata->u.mntr_flags & mask))
89 return -EBUSY;
91 ieee80211_adjust_monitor_flags(sdata, -1);
92 sdata->u.mntr_flags = *flags;
93 ieee80211_adjust_monitor_flags(sdata, 1);
95 ieee80211_configure_filter(local);
96 } else {
98 * Because the interface is down, ieee80211_do_stop
99 * and ieee80211_do_open take care of "everything"
100 * mentioned in the comment above.
102 sdata->u.mntr_flags = *flags;
106 return 0;
109 static int ieee80211_start_p2p_device(struct wiphy *wiphy,
110 struct wireless_dev *wdev)
112 return ieee80211_do_open(wdev, true);
115 static void ieee80211_stop_p2p_device(struct wiphy *wiphy,
116 struct wireless_dev *wdev)
118 ieee80211_sdata_stop(IEEE80211_WDEV_TO_SUB_IF(wdev));
121 static int ieee80211_set_noack_map(struct wiphy *wiphy,
122 struct net_device *dev,
123 u16 noack_map)
125 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
127 sdata->noack_map = noack_map;
128 return 0;
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)
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;
140 if (!ieee80211_sdata_running(sdata))
141 return -ENETDOWN;
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;
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);
160 if (pairwise)
161 key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
163 mutex_lock(&sdata->local->sta_mtx);
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);
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.
177 * TODO: accept the key if we have a station entry and
178 * add it to the device after the station.
180 if (!sta || !test_sta_flag(sta, WLAN_STA_ASSOC)) {
181 ieee80211_key_free_unused(key);
182 err = -ENOENT;
183 goto out_unlock;
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;
219 err = ieee80211_key_link(key, sdata, sta);
221 out_unlock:
222 mutex_unlock(&sdata->local->sta_mtx);
224 return err;
227 static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
228 u8 key_idx, bool pairwise, const u8 *mac_addr)
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;
236 mutex_lock(&local->sta_mtx);
237 mutex_lock(&local->key_mtx);
239 if (mac_addr) {
240 ret = -ENOENT;
242 sta = sta_info_get_bss(sdata, mac_addr);
243 if (!sta)
244 goto out_unlock;
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]);
253 if (!key) {
254 ret = -ENOENT;
255 goto out_unlock;
258 ieee80211_key_free(key, true);
260 ret = 0;
261 out_unlock:
262 mutex_unlock(&local->key_mtx);
263 mutex_unlock(&local->sta_mtx);
265 return ret;
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))
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;
284 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
286 rcu_read_lock();
288 if (mac_addr) {
289 sta = sta_info_get_bss(sdata, mac_addr);
290 if (!sta)
291 goto out;
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]);
300 if (!key)
301 goto out;
303 memset(&params, 0, sizeof(params));
305 params.cipher = key->conf.cipher;
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;
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);
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;
350 params.key = key->conf.key;
351 params.key_len = key->conf.keylen;
353 callback(cookie, &params);
354 err = 0;
356 out:
357 rcu_read_unlock();
358 return err;
361 static int ieee80211_config_default_key(struct wiphy *wiphy,
362 struct net_device *dev,
363 u8 key_idx, bool uni,
364 bool multi)
366 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
368 ieee80211_set_default_key(sdata, key_idx, uni, multi);
370 return 0;
373 static int ieee80211_config_default_mgmt_key(struct wiphy *wiphy,
374 struct net_device *dev,
375 u8 key_idx)
377 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
379 ieee80211_set_default_mgmt_key(sdata, key_idx);
381 return 0;
384 void sta_set_rate_info_tx(struct sta_info *sta,
385 const struct ieee80211_tx_rate *rate,
386 struct rate_info *rinfo)
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 int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
399 u16 brate;
401 sband = sta->local->hw.wiphy->bands[
402 ieee80211_get_sdata_band(sta->sdata)];
403 brate = sband->bitrates[rate->idx].bitrate;
404 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
406 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
407 rinfo->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
408 if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
409 rinfo->flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
410 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
411 rinfo->flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
412 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
413 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
416 void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
418 rinfo->flags = 0;
420 if (sta->last_rx_rate_flag & RX_FLAG_HT) {
421 rinfo->flags |= RATE_INFO_FLAGS_MCS;
422 rinfo->mcs = sta->last_rx_rate_idx;
423 } else if (sta->last_rx_rate_flag & RX_FLAG_VHT) {
424 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
425 rinfo->nss = sta->last_rx_rate_vht_nss;
426 rinfo->mcs = sta->last_rx_rate_idx;
427 } else {
428 struct ieee80211_supported_band *sband;
429 int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
430 u16 brate;
432 sband = sta->local->hw.wiphy->bands[
433 ieee80211_get_sdata_band(sta->sdata)];
434 brate = sband->bitrates[sta->last_rx_rate_idx].bitrate;
435 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
438 if (sta->last_rx_rate_flag & RX_FLAG_40MHZ)
439 rinfo->flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
440 if (sta->last_rx_rate_flag & RX_FLAG_SHORT_GI)
441 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
442 if (sta->last_rx_rate_flag & RX_FLAG_80MHZ)
443 rinfo->flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
444 if (sta->last_rx_rate_flag & RX_FLAG_80P80MHZ)
445 rinfo->flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
446 if (sta->last_rx_rate_flag & RX_FLAG_160MHZ)
447 rinfo->flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
450 static void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
452 struct ieee80211_sub_if_data *sdata = sta->sdata;
453 struct ieee80211_local *local = sdata->local;
454 struct timespec uptime;
455 u64 packets = 0;
456 int i, ac;
458 sinfo->generation = sdata->local->sta_generation;
460 sinfo->filled = STATION_INFO_INACTIVE_TIME |
461 STATION_INFO_RX_BYTES64 |
462 STATION_INFO_TX_BYTES64 |
463 STATION_INFO_RX_PACKETS |
464 STATION_INFO_TX_PACKETS |
465 STATION_INFO_TX_RETRIES |
466 STATION_INFO_TX_FAILED |
467 STATION_INFO_TX_BITRATE |
468 STATION_INFO_RX_BITRATE |
469 STATION_INFO_RX_DROP_MISC |
470 STATION_INFO_BSS_PARAM |
471 STATION_INFO_CONNECTED_TIME |
472 STATION_INFO_STA_FLAGS |
473 STATION_INFO_BEACON_LOSS_COUNT;
475 do_posix_clock_monotonic_gettime(&uptime);
476 sinfo->connected_time = uptime.tv_sec - sta->last_connected;
478 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
479 sinfo->tx_bytes = 0;
480 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
481 sinfo->tx_bytes += sta->tx_bytes[ac];
482 packets += sta->tx_packets[ac];
484 sinfo->tx_packets = packets;
485 sinfo->rx_bytes = sta->rx_bytes;
486 sinfo->rx_packets = sta->rx_packets;
487 sinfo->tx_retries = sta->tx_retry_count;
488 sinfo->tx_failed = sta->tx_retry_failed;
489 sinfo->rx_dropped_misc = sta->rx_dropped;
490 sinfo->beacon_loss_count = sta->beacon_loss_count;
492 if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
493 (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
494 sinfo->filled |= STATION_INFO_SIGNAL | STATION_INFO_SIGNAL_AVG;
495 if (!local->ops->get_rssi ||
496 drv_get_rssi(local, sdata, &sta->sta, &sinfo->signal))
497 sinfo->signal = (s8)sta->last_signal;
498 sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
500 if (sta->chains) {
501 sinfo->filled |= STATION_INFO_CHAIN_SIGNAL |
502 STATION_INFO_CHAIN_SIGNAL_AVG;
504 sinfo->chains = sta->chains;
505 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
506 sinfo->chain_signal[i] = sta->chain_signal_last[i];
507 sinfo->chain_signal_avg[i] =
508 (s8) -ewma_read(&sta->chain_signal_avg[i]);
512 sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
513 sta_set_rate_info_rx(sta, &sinfo->rxrate);
515 if (ieee80211_vif_is_mesh(&sdata->vif)) {
516 #ifdef CONFIG_MAC80211_MESH
517 sinfo->filled |= STATION_INFO_LLID |
518 STATION_INFO_PLID |
519 STATION_INFO_PLINK_STATE |
520 STATION_INFO_LOCAL_PM |
521 STATION_INFO_PEER_PM |
522 STATION_INFO_NONPEER_PM;
524 sinfo->llid = le16_to_cpu(sta->llid);
525 sinfo->plid = le16_to_cpu(sta->plid);
526 sinfo->plink_state = sta->plink_state;
527 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
528 sinfo->filled |= STATION_INFO_T_OFFSET;
529 sinfo->t_offset = sta->t_offset;
531 sinfo->local_pm = sta->local_pm;
532 sinfo->peer_pm = sta->peer_pm;
533 sinfo->nonpeer_pm = sta->nonpeer_pm;
534 #endif
537 sinfo->bss_param.flags = 0;
538 if (sdata->vif.bss_conf.use_cts_prot)
539 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
540 if (sdata->vif.bss_conf.use_short_preamble)
541 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
542 if (sdata->vif.bss_conf.use_short_slot)
543 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
544 sinfo->bss_param.dtim_period = sdata->local->hw.conf.ps_dtim_period;
545 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
547 sinfo->sta_flags.set = 0;
548 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
549 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
550 BIT(NL80211_STA_FLAG_WME) |
551 BIT(NL80211_STA_FLAG_MFP) |
552 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
553 BIT(NL80211_STA_FLAG_ASSOCIATED) |
554 BIT(NL80211_STA_FLAG_TDLS_PEER);
555 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
556 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
557 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
558 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
559 if (test_sta_flag(sta, WLAN_STA_WME))
560 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
561 if (test_sta_flag(sta, WLAN_STA_MFP))
562 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
563 if (test_sta_flag(sta, WLAN_STA_AUTH))
564 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
565 if (test_sta_flag(sta, WLAN_STA_ASSOC))
566 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
567 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
568 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
571 static const char ieee80211_gstrings_sta_stats[][ETH_GSTRING_LEN] = {
572 "rx_packets", "rx_bytes", "wep_weak_iv_count",
573 "rx_duplicates", "rx_fragments", "rx_dropped",
574 "tx_packets", "tx_bytes", "tx_fragments",
575 "tx_filtered", "tx_retry_failed", "tx_retries",
576 "beacon_loss", "sta_state", "txrate", "rxrate", "signal",
577 "channel", "noise", "ch_time", "ch_time_busy",
578 "ch_time_ext_busy", "ch_time_rx", "ch_time_tx"
580 #define STA_STATS_LEN ARRAY_SIZE(ieee80211_gstrings_sta_stats)
582 static int ieee80211_get_et_sset_count(struct wiphy *wiphy,
583 struct net_device *dev,
584 int sset)
586 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
587 int rv = 0;
589 if (sset == ETH_SS_STATS)
590 rv += STA_STATS_LEN;
592 rv += drv_get_et_sset_count(sdata, sset);
594 if (rv == 0)
595 return -EOPNOTSUPP;
596 return rv;
599 static void ieee80211_get_et_stats(struct wiphy *wiphy,
600 struct net_device *dev,
601 struct ethtool_stats *stats,
602 u64 *data)
604 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
605 struct ieee80211_chanctx_conf *chanctx_conf;
606 struct ieee80211_channel *channel;
607 struct sta_info *sta;
608 struct ieee80211_local *local = sdata->local;
609 struct station_info sinfo;
610 struct survey_info survey;
611 int i, q;
612 #define STA_STATS_SURVEY_LEN 7
614 memset(data, 0, sizeof(u64) * STA_STATS_LEN);
616 #define ADD_STA_STATS(sta) \
617 do { \
618 data[i++] += sta->rx_packets; \
619 data[i++] += sta->rx_bytes; \
620 data[i++] += sta->wep_weak_iv_count; \
621 data[i++] += sta->num_duplicates; \
622 data[i++] += sta->rx_fragments; \
623 data[i++] += sta->rx_dropped; \
625 data[i++] += sinfo.tx_packets; \
626 data[i++] += sinfo.tx_bytes; \
627 data[i++] += sta->tx_fragments; \
628 data[i++] += sta->tx_filtered_count; \
629 data[i++] += sta->tx_retry_failed; \
630 data[i++] += sta->tx_retry_count; \
631 data[i++] += sta->beacon_loss_count; \
632 } while (0)
634 /* For Managed stations, find the single station based on BSSID
635 * and use that. For interface types, iterate through all available
636 * stations and add stats for any station that is assigned to this
637 * network device.
640 mutex_lock(&local->sta_mtx);
642 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
643 sta = sta_info_get_bss(sdata, sdata->u.mgd.bssid);
645 if (!(sta && !WARN_ON(sta->sdata->dev != dev)))
646 goto do_survey;
648 sinfo.filled = 0;
649 sta_set_sinfo(sta, &sinfo);
651 i = 0;
652 ADD_STA_STATS(sta);
654 data[i++] = sta->sta_state;
657 if (sinfo.filled & STATION_INFO_TX_BITRATE)
658 data[i] = 100000 *
659 cfg80211_calculate_bitrate(&sinfo.txrate);
660 i++;
661 if (sinfo.filled & STATION_INFO_RX_BITRATE)
662 data[i] = 100000 *
663 cfg80211_calculate_bitrate(&sinfo.rxrate);
664 i++;
666 if (sinfo.filled & STATION_INFO_SIGNAL_AVG)
667 data[i] = (u8)sinfo.signal_avg;
668 i++;
669 } else {
670 list_for_each_entry(sta, &local->sta_list, list) {
671 /* Make sure this station belongs to the proper dev */
672 if (sta->sdata->dev != dev)
673 continue;
675 sinfo.filled = 0;
676 sta_set_sinfo(sta, &sinfo);
677 i = 0;
678 ADD_STA_STATS(sta);
682 do_survey:
683 i = STA_STATS_LEN - STA_STATS_SURVEY_LEN;
684 /* Get survey stats for current channel */
685 survey.filled = 0;
687 rcu_read_lock();
688 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
689 if (chanctx_conf)
690 channel = chanctx_conf->def.chan;
691 else
692 channel = NULL;
693 rcu_read_unlock();
695 if (channel) {
696 q = 0;
697 do {
698 survey.filled = 0;
699 if (drv_get_survey(local, q, &survey) != 0) {
700 survey.filled = 0;
701 break;
703 q++;
704 } while (channel != survey.channel);
707 if (survey.filled)
708 data[i++] = survey.channel->center_freq;
709 else
710 data[i++] = 0;
711 if (survey.filled & SURVEY_INFO_NOISE_DBM)
712 data[i++] = (u8)survey.noise;
713 else
714 data[i++] = -1LL;
715 if (survey.filled & SURVEY_INFO_CHANNEL_TIME)
716 data[i++] = survey.channel_time;
717 else
718 data[i++] = -1LL;
719 if (survey.filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
720 data[i++] = survey.channel_time_busy;
721 else
722 data[i++] = -1LL;
723 if (survey.filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
724 data[i++] = survey.channel_time_ext_busy;
725 else
726 data[i++] = -1LL;
727 if (survey.filled & SURVEY_INFO_CHANNEL_TIME_RX)
728 data[i++] = survey.channel_time_rx;
729 else
730 data[i++] = -1LL;
731 if (survey.filled & SURVEY_INFO_CHANNEL_TIME_TX)
732 data[i++] = survey.channel_time_tx;
733 else
734 data[i++] = -1LL;
736 mutex_unlock(&local->sta_mtx);
738 if (WARN_ON(i != STA_STATS_LEN))
739 return;
741 drv_get_et_stats(sdata, stats, &(data[STA_STATS_LEN]));
744 static void ieee80211_get_et_strings(struct wiphy *wiphy,
745 struct net_device *dev,
746 u32 sset, u8 *data)
748 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
749 int sz_sta_stats = 0;
751 if (sset == ETH_SS_STATS) {
752 sz_sta_stats = sizeof(ieee80211_gstrings_sta_stats);
753 memcpy(data, ieee80211_gstrings_sta_stats, sz_sta_stats);
755 drv_get_et_strings(sdata, sset, &(data[sz_sta_stats]));
758 static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
759 int idx, u8 *mac, struct station_info *sinfo)
761 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
762 struct ieee80211_local *local = sdata->local;
763 struct sta_info *sta;
764 int ret = -ENOENT;
766 mutex_lock(&local->sta_mtx);
768 sta = sta_info_get_by_idx(sdata, idx);
769 if (sta) {
770 ret = 0;
771 memcpy(mac, sta->sta.addr, ETH_ALEN);
772 sta_set_sinfo(sta, sinfo);
775 mutex_unlock(&local->sta_mtx);
777 return ret;
780 static int ieee80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
781 int idx, struct survey_info *survey)
783 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
785 return drv_get_survey(local, idx, survey);
788 static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
789 u8 *mac, struct station_info *sinfo)
791 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
792 struct ieee80211_local *local = sdata->local;
793 struct sta_info *sta;
794 int ret = -ENOENT;
796 mutex_lock(&local->sta_mtx);
798 sta = sta_info_get_bss(sdata, mac);
799 if (sta) {
800 ret = 0;
801 sta_set_sinfo(sta, sinfo);
804 mutex_unlock(&local->sta_mtx);
806 return ret;
809 static int ieee80211_set_monitor_channel(struct wiphy *wiphy,
810 struct cfg80211_chan_def *chandef)
812 struct ieee80211_local *local = wiphy_priv(wiphy);
813 struct ieee80211_sub_if_data *sdata;
814 int ret = 0;
816 if (cfg80211_chandef_identical(&local->monitor_chandef, chandef))
817 return 0;
819 mutex_lock(&local->iflist_mtx);
820 if (local->use_chanctx) {
821 sdata = rcu_dereference_protected(
822 local->monitor_sdata,
823 lockdep_is_held(&local->iflist_mtx));
824 if (sdata) {
825 ieee80211_vif_release_channel(sdata);
826 ret = ieee80211_vif_use_channel(sdata, chandef,
827 IEEE80211_CHANCTX_EXCLUSIVE);
829 } else if (local->open_count == local->monitors) {
830 local->_oper_chandef = *chandef;
831 ieee80211_hw_config(local, 0);
834 if (ret == 0)
835 local->monitor_chandef = *chandef;
836 mutex_unlock(&local->iflist_mtx);
838 return ret;
841 static int ieee80211_set_probe_resp(struct ieee80211_sub_if_data *sdata,
842 const u8 *resp, size_t resp_len)
844 struct probe_resp *new, *old;
846 if (!resp || !resp_len)
847 return 1;
849 old = rtnl_dereference(sdata->u.ap.probe_resp);
851 new = kzalloc(sizeof(struct probe_resp) + resp_len, GFP_KERNEL);
852 if (!new)
853 return -ENOMEM;
855 new->len = resp_len;
856 memcpy(new->data, resp, resp_len);
858 rcu_assign_pointer(sdata->u.ap.probe_resp, new);
859 if (old)
860 kfree_rcu(old, rcu_head);
862 return 0;
865 int ieee80211_assign_beacon(struct ieee80211_sub_if_data *sdata,
866 struct cfg80211_beacon_data *params)
868 struct beacon_data *new, *old;
869 int new_head_len, new_tail_len;
870 int size, err;
871 u32 changed = BSS_CHANGED_BEACON;
873 old = rtnl_dereference(sdata->u.ap.beacon);
875 /* Need to have a beacon head if we don't have one yet */
876 if (!params->head && !old)
877 return -EINVAL;
879 /* new or old head? */
880 if (params->head)
881 new_head_len = params->head_len;
882 else
883 new_head_len = old->head_len;
885 /* new or old tail? */
886 if (params->tail || !old)
887 /* params->tail_len will be zero for !params->tail */
888 new_tail_len = params->tail_len;
889 else
890 new_tail_len = old->tail_len;
892 size = sizeof(*new) + new_head_len + new_tail_len;
894 new = kzalloc(size, GFP_KERNEL);
895 if (!new)
896 return -ENOMEM;
898 /* start filling the new info now */
901 * pointers go into the block we allocated,
902 * memory is | beacon_data | head | tail |
904 new->head = ((u8 *) new) + sizeof(*new);
905 new->tail = new->head + new_head_len;
906 new->head_len = new_head_len;
907 new->tail_len = new_tail_len;
909 /* copy in head */
910 if (params->head)
911 memcpy(new->head, params->head, new_head_len);
912 else
913 memcpy(new->head, old->head, new_head_len);
915 /* copy in optional tail */
916 if (params->tail)
917 memcpy(new->tail, params->tail, new_tail_len);
918 else
919 if (old)
920 memcpy(new->tail, old->tail, new_tail_len);
922 err = ieee80211_set_probe_resp(sdata, params->probe_resp,
923 params->probe_resp_len);
924 if (err < 0)
925 return err;
926 if (err == 0)
927 changed |= BSS_CHANGED_AP_PROBE_RESP;
929 rcu_assign_pointer(sdata->u.ap.beacon, new);
931 if (old)
932 kfree_rcu(old, rcu_head);
934 return changed;
937 static int ieee80211_start_ap(struct wiphy *wiphy, struct net_device *dev,
938 struct cfg80211_ap_settings *params)
940 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
941 struct beacon_data *old;
942 struct ieee80211_sub_if_data *vlan;
943 u32 changed = BSS_CHANGED_BEACON_INT |
944 BSS_CHANGED_BEACON_ENABLED |
945 BSS_CHANGED_BEACON |
946 BSS_CHANGED_SSID |
947 BSS_CHANGED_P2P_PS;
948 int err;
950 old = rtnl_dereference(sdata->u.ap.beacon);
951 if (old)
952 return -EALREADY;
954 /* TODO: make hostapd tell us what it wants */
955 sdata->smps_mode = IEEE80211_SMPS_OFF;
956 sdata->needed_rx_chains = sdata->local->rx_chains;
957 sdata->radar_required = params->radar_required;
959 err = ieee80211_vif_use_channel(sdata, &params->chandef,
960 IEEE80211_CHANCTX_SHARED);
961 if (err)
962 return err;
963 ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
966 * Apply control port protocol, this allows us to
967 * not encrypt dynamic WEP control frames.
969 sdata->control_port_protocol = params->crypto.control_port_ethertype;
970 sdata->control_port_no_encrypt = params->crypto.control_port_no_encrypt;
971 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
972 vlan->control_port_protocol =
973 params->crypto.control_port_ethertype;
974 vlan->control_port_no_encrypt =
975 params->crypto.control_port_no_encrypt;
978 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
979 sdata->vif.bss_conf.dtim_period = params->dtim_period;
980 sdata->vif.bss_conf.enable_beacon = true;
982 sdata->vif.bss_conf.ssid_len = params->ssid_len;
983 if (params->ssid_len)
984 memcpy(sdata->vif.bss_conf.ssid, params->ssid,
985 params->ssid_len);
986 sdata->vif.bss_conf.hidden_ssid =
987 (params->hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE);
989 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
990 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
991 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow =
992 params->p2p_ctwindow & IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
993 if (params->p2p_opp_ps)
994 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow |=
995 IEEE80211_P2P_OPPPS_ENABLE_BIT;
997 err = ieee80211_assign_beacon(sdata, &params->beacon);
998 if (err < 0) {
999 ieee80211_vif_release_channel(sdata);
1000 return err;
1002 changed |= err;
1004 err = drv_start_ap(sdata->local, sdata);
1005 if (err) {
1006 old = rtnl_dereference(sdata->u.ap.beacon);
1007 if (old)
1008 kfree_rcu(old, rcu_head);
1009 RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
1010 ieee80211_vif_release_channel(sdata);
1011 return err;
1014 ieee80211_bss_info_change_notify(sdata, changed);
1016 netif_carrier_on(dev);
1017 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1018 netif_carrier_on(vlan->dev);
1020 return 0;
1023 static int ieee80211_change_beacon(struct wiphy *wiphy, struct net_device *dev,
1024 struct cfg80211_beacon_data *params)
1026 struct ieee80211_sub_if_data *sdata;
1027 struct beacon_data *old;
1028 int err;
1030 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1032 /* don't allow changing the beacon while CSA is in place - offset
1033 * of channel switch counter may change
1035 if (sdata->vif.csa_active)
1036 return -EBUSY;
1038 old = rtnl_dereference(sdata->u.ap.beacon);
1039 if (!old)
1040 return -ENOENT;
1042 err = ieee80211_assign_beacon(sdata, params);
1043 if (err < 0)
1044 return err;
1045 ieee80211_bss_info_change_notify(sdata, err);
1046 return 0;
1049 static int ieee80211_stop_ap(struct wiphy *wiphy, struct net_device *dev)
1051 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1052 struct ieee80211_sub_if_data *vlan;
1053 struct ieee80211_local *local = sdata->local;
1054 struct beacon_data *old_beacon;
1055 struct probe_resp *old_probe_resp;
1057 old_beacon = rtnl_dereference(sdata->u.ap.beacon);
1058 if (!old_beacon)
1059 return -ENOENT;
1060 old_probe_resp = rtnl_dereference(sdata->u.ap.probe_resp);
1062 /* abort any running channel switch */
1063 sdata->vif.csa_active = false;
1064 cancel_work_sync(&sdata->csa_finalize_work);
1066 /* turn off carrier for this interface and dependent VLANs */
1067 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1068 netif_carrier_off(vlan->dev);
1069 netif_carrier_off(dev);
1071 /* remove beacon and probe response */
1072 RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
1073 RCU_INIT_POINTER(sdata->u.ap.probe_resp, NULL);
1074 kfree_rcu(old_beacon, rcu_head);
1075 if (old_probe_resp)
1076 kfree_rcu(old_probe_resp, rcu_head);
1078 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1079 sta_info_flush_defer(vlan);
1080 sta_info_flush_defer(sdata);
1081 synchronize_net();
1082 rcu_barrier();
1083 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
1084 sta_info_flush_cleanup(vlan);
1085 ieee80211_free_keys(vlan);
1087 sta_info_flush_cleanup(sdata);
1088 ieee80211_free_keys(sdata);
1090 sdata->vif.bss_conf.enable_beacon = false;
1091 sdata->vif.bss_conf.ssid_len = 0;
1092 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
1093 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
1095 if (sdata->wdev.cac_started) {
1096 cancel_delayed_work_sync(&sdata->dfs_cac_timer_work);
1097 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_ABORTED,
1098 GFP_KERNEL);
1101 drv_stop_ap(sdata->local, sdata);
1103 /* free all potentially still buffered bcast frames */
1104 local->total_ps_buffered -= skb_queue_len(&sdata->u.ap.ps.bc_buf);
1105 skb_queue_purge(&sdata->u.ap.ps.bc_buf);
1107 ieee80211_vif_copy_chanctx_to_vlans(sdata, true);
1108 ieee80211_vif_release_channel(sdata);
1110 return 0;
1113 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1114 struct iapp_layer2_update {
1115 u8 da[ETH_ALEN]; /* broadcast */
1116 u8 sa[ETH_ALEN]; /* STA addr */
1117 __be16 len; /* 6 */
1118 u8 dsap; /* 0 */
1119 u8 ssap; /* 0 */
1120 u8 control;
1121 u8 xid_info[3];
1122 } __packed;
1124 static void ieee80211_send_layer2_update(struct sta_info *sta)
1126 struct iapp_layer2_update *msg;
1127 struct sk_buff *skb;
1129 /* Send Level 2 Update Frame to update forwarding tables in layer 2
1130 * bridge devices */
1132 skb = dev_alloc_skb(sizeof(*msg));
1133 if (!skb)
1134 return;
1135 msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
1137 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1138 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1140 eth_broadcast_addr(msg->da);
1141 memcpy(msg->sa, sta->sta.addr, ETH_ALEN);
1142 msg->len = htons(6);
1143 msg->dsap = 0;
1144 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
1145 msg->control = 0xaf; /* XID response lsb.1111F101.
1146 * F=0 (no poll command; unsolicited frame) */
1147 msg->xid_info[0] = 0x81; /* XID format identifier */
1148 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
1149 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
1151 skb->dev = sta->sdata->dev;
1152 skb->protocol = eth_type_trans(skb, sta->sdata->dev);
1153 memset(skb->cb, 0, sizeof(skb->cb));
1154 netif_rx_ni(skb);
1157 static int sta_apply_auth_flags(struct ieee80211_local *local,
1158 struct sta_info *sta,
1159 u32 mask, u32 set)
1161 int ret;
1163 if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1164 set & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1165 !test_sta_flag(sta, WLAN_STA_AUTH)) {
1166 ret = sta_info_move_state(sta, IEEE80211_STA_AUTH);
1167 if (ret)
1168 return ret;
1171 if (mask & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1172 set & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1173 !test_sta_flag(sta, WLAN_STA_ASSOC)) {
1174 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1175 if (ret)
1176 return ret;
1179 if (mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
1180 if (set & BIT(NL80211_STA_FLAG_AUTHORIZED))
1181 ret = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
1182 else if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1183 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1184 else
1185 ret = 0;
1186 if (ret)
1187 return ret;
1190 if (mask & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
1191 !(set & BIT(NL80211_STA_FLAG_ASSOCIATED)) &&
1192 test_sta_flag(sta, WLAN_STA_ASSOC)) {
1193 ret = sta_info_move_state(sta, IEEE80211_STA_AUTH);
1194 if (ret)
1195 return ret;
1198 if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
1199 !(set & BIT(NL80211_STA_FLAG_AUTHENTICATED)) &&
1200 test_sta_flag(sta, WLAN_STA_AUTH)) {
1201 ret = sta_info_move_state(sta, IEEE80211_STA_NONE);
1202 if (ret)
1203 return ret;
1206 return 0;
1209 static int sta_apply_parameters(struct ieee80211_local *local,
1210 struct sta_info *sta,
1211 struct station_parameters *params)
1213 int ret = 0;
1214 struct ieee80211_supported_band *sband;
1215 struct ieee80211_sub_if_data *sdata = sta->sdata;
1216 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
1217 u32 mask, set;
1219 sband = local->hw.wiphy->bands[band];
1221 mask = params->sta_flags_mask;
1222 set = params->sta_flags_set;
1224 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1226 * In mesh mode, ASSOCIATED isn't part of the nl80211
1227 * API but must follow AUTHENTICATED for driver state.
1229 if (mask & BIT(NL80211_STA_FLAG_AUTHENTICATED))
1230 mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1231 if (set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
1232 set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1233 } else if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
1235 * TDLS -- everything follows authorized, but
1236 * only becoming authorized is possible, not
1237 * going back
1239 if (set & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
1240 set |= BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1241 BIT(NL80211_STA_FLAG_ASSOCIATED);
1242 mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1243 BIT(NL80211_STA_FLAG_ASSOCIATED);
1247 ret = sta_apply_auth_flags(local, sta, mask, set);
1248 if (ret)
1249 return ret;
1251 if (mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) {
1252 if (set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
1253 set_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE);
1254 else
1255 clear_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE);
1258 if (mask & BIT(NL80211_STA_FLAG_WME)) {
1259 if (set & BIT(NL80211_STA_FLAG_WME)) {
1260 set_sta_flag(sta, WLAN_STA_WME);
1261 sta->sta.wme = true;
1262 } else {
1263 clear_sta_flag(sta, WLAN_STA_WME);
1264 sta->sta.wme = false;
1268 if (mask & BIT(NL80211_STA_FLAG_MFP)) {
1269 if (set & BIT(NL80211_STA_FLAG_MFP))
1270 set_sta_flag(sta, WLAN_STA_MFP);
1271 else
1272 clear_sta_flag(sta, WLAN_STA_MFP);
1275 if (mask & BIT(NL80211_STA_FLAG_TDLS_PEER)) {
1276 if (set & BIT(NL80211_STA_FLAG_TDLS_PEER))
1277 set_sta_flag(sta, WLAN_STA_TDLS_PEER);
1278 else
1279 clear_sta_flag(sta, WLAN_STA_TDLS_PEER);
1282 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD) {
1283 sta->sta.uapsd_queues = params->uapsd_queues;
1284 sta->sta.max_sp = params->max_sp;
1288 * cfg80211 validates this (1-2007) and allows setting the AID
1289 * only when creating a new station entry
1291 if (params->aid)
1292 sta->sta.aid = params->aid;
1295 * Some of the following updates would be racy if called on an
1296 * existing station, via ieee80211_change_station(). However,
1297 * all such changes are rejected by cfg80211 except for updates
1298 * changing the supported rates on an existing but not yet used
1299 * TDLS peer.
1302 if (params->listen_interval >= 0)
1303 sta->listen_interval = params->listen_interval;
1305 if (params->supported_rates) {
1306 ieee80211_parse_bitrates(&sdata->vif.bss_conf.chandef,
1307 sband, params->supported_rates,
1308 params->supported_rates_len,
1309 &sta->sta.supp_rates[band]);
1312 if (params->ht_capa)
1313 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
1314 params->ht_capa, sta);
1316 if (params->vht_capa)
1317 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
1318 params->vht_capa, sta);
1320 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1321 #ifdef CONFIG_MAC80211_MESH
1322 u32 changed = 0;
1324 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE) {
1325 switch (params->plink_state) {
1326 case NL80211_PLINK_ESTAB:
1327 if (sta->plink_state != NL80211_PLINK_ESTAB)
1328 changed = mesh_plink_inc_estab_count(
1329 sdata);
1330 sta->plink_state = params->plink_state;
1332 ieee80211_mps_sta_status_update(sta);
1333 changed |= ieee80211_mps_set_sta_local_pm(sta,
1334 sdata->u.mesh.mshcfg.power_mode);
1335 break;
1336 case NL80211_PLINK_LISTEN:
1337 case NL80211_PLINK_BLOCKED:
1338 case NL80211_PLINK_OPN_SNT:
1339 case NL80211_PLINK_OPN_RCVD:
1340 case NL80211_PLINK_CNF_RCVD:
1341 case NL80211_PLINK_HOLDING:
1342 if (sta->plink_state == NL80211_PLINK_ESTAB)
1343 changed = mesh_plink_dec_estab_count(
1344 sdata);
1345 sta->plink_state = params->plink_state;
1347 ieee80211_mps_sta_status_update(sta);
1348 changed |=
1349 ieee80211_mps_local_status_update(sdata);
1350 break;
1351 default:
1352 /* nothing */
1353 break;
1357 switch (params->plink_action) {
1358 case NL80211_PLINK_ACTION_NO_ACTION:
1359 /* nothing */
1360 break;
1361 case NL80211_PLINK_ACTION_OPEN:
1362 changed |= mesh_plink_open(sta);
1363 break;
1364 case NL80211_PLINK_ACTION_BLOCK:
1365 changed |= mesh_plink_block(sta);
1366 break;
1369 if (params->local_pm)
1370 changed |=
1371 ieee80211_mps_set_sta_local_pm(sta,
1372 params->local_pm);
1373 ieee80211_bss_info_change_notify(sdata, changed);
1374 #endif
1377 return 0;
1380 static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
1381 u8 *mac, struct station_parameters *params)
1383 struct ieee80211_local *local = wiphy_priv(wiphy);
1384 struct sta_info *sta;
1385 struct ieee80211_sub_if_data *sdata;
1386 int err;
1387 int layer2_update;
1389 if (params->vlan) {
1390 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
1392 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1393 sdata->vif.type != NL80211_IFTYPE_AP)
1394 return -EINVAL;
1395 } else
1396 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1398 if (ether_addr_equal(mac, sdata->vif.addr))
1399 return -EINVAL;
1401 if (is_multicast_ether_addr(mac))
1402 return -EINVAL;
1404 sta = sta_info_alloc(sdata, mac, GFP_KERNEL);
1405 if (!sta)
1406 return -ENOMEM;
1409 * defaults -- if userspace wants something else we'll
1410 * change it accordingly in sta_apply_parameters()
1412 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))) {
1413 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
1414 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
1417 err = sta_apply_parameters(local, sta, params);
1418 if (err) {
1419 sta_info_free(local, sta);
1420 return err;
1424 * for TDLS, rate control should be initialized only when
1425 * rates are known and station is marked authorized
1427 if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1428 rate_control_rate_init(sta);
1430 layer2_update = sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1431 sdata->vif.type == NL80211_IFTYPE_AP;
1433 err = sta_info_insert_rcu(sta);
1434 if (err) {
1435 rcu_read_unlock();
1436 return err;
1439 if (layer2_update)
1440 ieee80211_send_layer2_update(sta);
1442 rcu_read_unlock();
1444 return 0;
1447 static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
1448 u8 *mac)
1450 struct ieee80211_sub_if_data *sdata;
1452 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1454 if (mac)
1455 return sta_info_destroy_addr_bss(sdata, mac);
1457 sta_info_flush(sdata);
1458 return 0;
1461 static int ieee80211_change_station(struct wiphy *wiphy,
1462 struct net_device *dev, u8 *mac,
1463 struct station_parameters *params)
1465 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1466 struct ieee80211_local *local = wiphy_priv(wiphy);
1467 struct sta_info *sta;
1468 struct ieee80211_sub_if_data *vlansdata;
1469 enum cfg80211_station_type statype;
1470 int err;
1472 mutex_lock(&local->sta_mtx);
1474 sta = sta_info_get_bss(sdata, mac);
1475 if (!sta) {
1476 err = -ENOENT;
1477 goto out_err;
1480 switch (sdata->vif.type) {
1481 case NL80211_IFTYPE_MESH_POINT:
1482 if (sdata->u.mesh.user_mpm)
1483 statype = CFG80211_STA_MESH_PEER_USER;
1484 else
1485 statype = CFG80211_STA_MESH_PEER_KERNEL;
1486 break;
1487 case NL80211_IFTYPE_ADHOC:
1488 statype = CFG80211_STA_IBSS;
1489 break;
1490 case NL80211_IFTYPE_STATION:
1491 if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
1492 statype = CFG80211_STA_AP_STA;
1493 break;
1495 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1496 statype = CFG80211_STA_TDLS_PEER_ACTIVE;
1497 else
1498 statype = CFG80211_STA_TDLS_PEER_SETUP;
1499 break;
1500 case NL80211_IFTYPE_AP:
1501 case NL80211_IFTYPE_AP_VLAN:
1502 statype = CFG80211_STA_AP_CLIENT;
1503 break;
1504 default:
1505 err = -EOPNOTSUPP;
1506 goto out_err;
1509 err = cfg80211_check_station_change(wiphy, params, statype);
1510 if (err)
1511 goto out_err;
1513 if (params->vlan && params->vlan != sta->sdata->dev) {
1514 bool prev_4addr = false;
1515 bool new_4addr = false;
1517 vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
1519 if (params->vlan->ieee80211_ptr->use_4addr) {
1520 if (vlansdata->u.vlan.sta) {
1521 err = -EBUSY;
1522 goto out_err;
1525 rcu_assign_pointer(vlansdata->u.vlan.sta, sta);
1526 new_4addr = true;
1529 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1530 sta->sdata->u.vlan.sta) {
1531 rcu_assign_pointer(sta->sdata->u.vlan.sta, NULL);
1532 prev_4addr = true;
1535 sta->sdata = vlansdata;
1537 if (sta->sta_state == IEEE80211_STA_AUTHORIZED &&
1538 prev_4addr != new_4addr) {
1539 if (new_4addr)
1540 atomic_dec(&sta->sdata->bss->num_mcast_sta);
1541 else
1542 atomic_inc(&sta->sdata->bss->num_mcast_sta);
1545 ieee80211_send_layer2_update(sta);
1548 err = sta_apply_parameters(local, sta, params);
1549 if (err)
1550 goto out_err;
1552 /* When peer becomes authorized, init rate control as well */
1553 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
1554 test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1555 rate_control_rate_init(sta);
1557 mutex_unlock(&local->sta_mtx);
1559 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1560 params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
1561 ieee80211_recalc_ps(local, -1);
1562 ieee80211_recalc_ps_vif(sdata);
1565 return 0;
1566 out_err:
1567 mutex_unlock(&local->sta_mtx);
1568 return err;
1571 #ifdef CONFIG_MAC80211_MESH
1572 static int ieee80211_add_mpath(struct wiphy *wiphy, struct net_device *dev,
1573 u8 *dst, u8 *next_hop)
1575 struct ieee80211_sub_if_data *sdata;
1576 struct mesh_path *mpath;
1577 struct sta_info *sta;
1579 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1581 rcu_read_lock();
1582 sta = sta_info_get(sdata, next_hop);
1583 if (!sta) {
1584 rcu_read_unlock();
1585 return -ENOENT;
1588 mpath = mesh_path_add(sdata, dst);
1589 if (IS_ERR(mpath)) {
1590 rcu_read_unlock();
1591 return PTR_ERR(mpath);
1594 mesh_path_fix_nexthop(mpath, sta);
1596 rcu_read_unlock();
1597 return 0;
1600 static int ieee80211_del_mpath(struct wiphy *wiphy, struct net_device *dev,
1601 u8 *dst)
1603 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1605 if (dst)
1606 return mesh_path_del(sdata, dst);
1608 mesh_path_flush_by_iface(sdata);
1609 return 0;
1612 static int ieee80211_change_mpath(struct wiphy *wiphy,
1613 struct net_device *dev,
1614 u8 *dst, u8 *next_hop)
1616 struct ieee80211_sub_if_data *sdata;
1617 struct mesh_path *mpath;
1618 struct sta_info *sta;
1620 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1622 rcu_read_lock();
1624 sta = sta_info_get(sdata, next_hop);
1625 if (!sta) {
1626 rcu_read_unlock();
1627 return -ENOENT;
1630 mpath = mesh_path_lookup(sdata, dst);
1631 if (!mpath) {
1632 rcu_read_unlock();
1633 return -ENOENT;
1636 mesh_path_fix_nexthop(mpath, sta);
1638 rcu_read_unlock();
1639 return 0;
1642 static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
1643 struct mpath_info *pinfo)
1645 struct sta_info *next_hop_sta = rcu_dereference(mpath->next_hop);
1647 if (next_hop_sta)
1648 memcpy(next_hop, next_hop_sta->sta.addr, ETH_ALEN);
1649 else
1650 memset(next_hop, 0, ETH_ALEN);
1652 memset(pinfo, 0, sizeof(*pinfo));
1654 pinfo->generation = mesh_paths_generation;
1656 pinfo->filled = MPATH_INFO_FRAME_QLEN |
1657 MPATH_INFO_SN |
1658 MPATH_INFO_METRIC |
1659 MPATH_INFO_EXPTIME |
1660 MPATH_INFO_DISCOVERY_TIMEOUT |
1661 MPATH_INFO_DISCOVERY_RETRIES |
1662 MPATH_INFO_FLAGS;
1664 pinfo->frame_qlen = mpath->frame_queue.qlen;
1665 pinfo->sn = mpath->sn;
1666 pinfo->metric = mpath->metric;
1667 if (time_before(jiffies, mpath->exp_time))
1668 pinfo->exptime = jiffies_to_msecs(mpath->exp_time - jiffies);
1669 pinfo->discovery_timeout =
1670 jiffies_to_msecs(mpath->discovery_timeout);
1671 pinfo->discovery_retries = mpath->discovery_retries;
1672 if (mpath->flags & MESH_PATH_ACTIVE)
1673 pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE;
1674 if (mpath->flags & MESH_PATH_RESOLVING)
1675 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
1676 if (mpath->flags & MESH_PATH_SN_VALID)
1677 pinfo->flags |= NL80211_MPATH_FLAG_SN_VALID;
1678 if (mpath->flags & MESH_PATH_FIXED)
1679 pinfo->flags |= NL80211_MPATH_FLAG_FIXED;
1680 if (mpath->flags & MESH_PATH_RESOLVED)
1681 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVED;
1684 static int ieee80211_get_mpath(struct wiphy *wiphy, struct net_device *dev,
1685 u8 *dst, u8 *next_hop, struct mpath_info *pinfo)
1688 struct ieee80211_sub_if_data *sdata;
1689 struct mesh_path *mpath;
1691 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1693 rcu_read_lock();
1694 mpath = mesh_path_lookup(sdata, dst);
1695 if (!mpath) {
1696 rcu_read_unlock();
1697 return -ENOENT;
1699 memcpy(dst, mpath->dst, ETH_ALEN);
1700 mpath_set_pinfo(mpath, next_hop, pinfo);
1701 rcu_read_unlock();
1702 return 0;
1705 static int ieee80211_dump_mpath(struct wiphy *wiphy, struct net_device *dev,
1706 int idx, u8 *dst, u8 *next_hop,
1707 struct mpath_info *pinfo)
1709 struct ieee80211_sub_if_data *sdata;
1710 struct mesh_path *mpath;
1712 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1714 rcu_read_lock();
1715 mpath = mesh_path_lookup_by_idx(sdata, idx);
1716 if (!mpath) {
1717 rcu_read_unlock();
1718 return -ENOENT;
1720 memcpy(dst, mpath->dst, ETH_ALEN);
1721 mpath_set_pinfo(mpath, next_hop, pinfo);
1722 rcu_read_unlock();
1723 return 0;
1726 static int ieee80211_get_mesh_config(struct wiphy *wiphy,
1727 struct net_device *dev,
1728 struct mesh_config *conf)
1730 struct ieee80211_sub_if_data *sdata;
1731 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1733 memcpy(conf, &(sdata->u.mesh.mshcfg), sizeof(struct mesh_config));
1734 return 0;
1737 static inline bool _chg_mesh_attr(enum nl80211_meshconf_params parm, u32 mask)
1739 return (mask >> (parm-1)) & 0x1;
1742 static int copy_mesh_setup(struct ieee80211_if_mesh *ifmsh,
1743 const struct mesh_setup *setup)
1745 u8 *new_ie;
1746 const u8 *old_ie;
1747 struct ieee80211_sub_if_data *sdata = container_of(ifmsh,
1748 struct ieee80211_sub_if_data, u.mesh);
1750 /* allocate information elements */
1751 new_ie = NULL;
1752 old_ie = ifmsh->ie;
1754 if (setup->ie_len) {
1755 new_ie = kmemdup(setup->ie, setup->ie_len,
1756 GFP_KERNEL);
1757 if (!new_ie)
1758 return -ENOMEM;
1760 ifmsh->ie_len = setup->ie_len;
1761 ifmsh->ie = new_ie;
1762 kfree(old_ie);
1764 /* now copy the rest of the setup parameters */
1765 ifmsh->mesh_id_len = setup->mesh_id_len;
1766 memcpy(ifmsh->mesh_id, setup->mesh_id, ifmsh->mesh_id_len);
1767 ifmsh->mesh_sp_id = setup->sync_method;
1768 ifmsh->mesh_pp_id = setup->path_sel_proto;
1769 ifmsh->mesh_pm_id = setup->path_metric;
1770 ifmsh->user_mpm = setup->user_mpm;
1771 ifmsh->mesh_auth_id = setup->auth_id;
1772 ifmsh->security = IEEE80211_MESH_SEC_NONE;
1773 if (setup->is_authenticated)
1774 ifmsh->security |= IEEE80211_MESH_SEC_AUTHED;
1775 if (setup->is_secure)
1776 ifmsh->security |= IEEE80211_MESH_SEC_SECURED;
1778 /* mcast rate setting in Mesh Node */
1779 memcpy(sdata->vif.bss_conf.mcast_rate, setup->mcast_rate,
1780 sizeof(setup->mcast_rate));
1781 sdata->vif.bss_conf.basic_rates = setup->basic_rates;
1783 sdata->vif.bss_conf.beacon_int = setup->beacon_interval;
1784 sdata->vif.bss_conf.dtim_period = setup->dtim_period;
1786 return 0;
1789 static int ieee80211_update_mesh_config(struct wiphy *wiphy,
1790 struct net_device *dev, u32 mask,
1791 const struct mesh_config *nconf)
1793 struct mesh_config *conf;
1794 struct ieee80211_sub_if_data *sdata;
1795 struct ieee80211_if_mesh *ifmsh;
1797 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1798 ifmsh = &sdata->u.mesh;
1800 /* Set the config options which we are interested in setting */
1801 conf = &(sdata->u.mesh.mshcfg);
1802 if (_chg_mesh_attr(NL80211_MESHCONF_RETRY_TIMEOUT, mask))
1803 conf->dot11MeshRetryTimeout = nconf->dot11MeshRetryTimeout;
1804 if (_chg_mesh_attr(NL80211_MESHCONF_CONFIRM_TIMEOUT, mask))
1805 conf->dot11MeshConfirmTimeout = nconf->dot11MeshConfirmTimeout;
1806 if (_chg_mesh_attr(NL80211_MESHCONF_HOLDING_TIMEOUT, mask))
1807 conf->dot11MeshHoldingTimeout = nconf->dot11MeshHoldingTimeout;
1808 if (_chg_mesh_attr(NL80211_MESHCONF_MAX_PEER_LINKS, mask))
1809 conf->dot11MeshMaxPeerLinks = nconf->dot11MeshMaxPeerLinks;
1810 if (_chg_mesh_attr(NL80211_MESHCONF_MAX_RETRIES, mask))
1811 conf->dot11MeshMaxRetries = nconf->dot11MeshMaxRetries;
1812 if (_chg_mesh_attr(NL80211_MESHCONF_TTL, mask))
1813 conf->dot11MeshTTL = nconf->dot11MeshTTL;
1814 if (_chg_mesh_attr(NL80211_MESHCONF_ELEMENT_TTL, mask))
1815 conf->element_ttl = nconf->element_ttl;
1816 if (_chg_mesh_attr(NL80211_MESHCONF_AUTO_OPEN_PLINKS, mask)) {
1817 if (ifmsh->user_mpm)
1818 return -EBUSY;
1819 conf->auto_open_plinks = nconf->auto_open_plinks;
1821 if (_chg_mesh_attr(NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR, mask))
1822 conf->dot11MeshNbrOffsetMaxNeighbor =
1823 nconf->dot11MeshNbrOffsetMaxNeighbor;
1824 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, mask))
1825 conf->dot11MeshHWMPmaxPREQretries =
1826 nconf->dot11MeshHWMPmaxPREQretries;
1827 if (_chg_mesh_attr(NL80211_MESHCONF_PATH_REFRESH_TIME, mask))
1828 conf->path_refresh_time = nconf->path_refresh_time;
1829 if (_chg_mesh_attr(NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, mask))
1830 conf->min_discovery_timeout = nconf->min_discovery_timeout;
1831 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, mask))
1832 conf->dot11MeshHWMPactivePathTimeout =
1833 nconf->dot11MeshHWMPactivePathTimeout;
1834 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, mask))
1835 conf->dot11MeshHWMPpreqMinInterval =
1836 nconf->dot11MeshHWMPpreqMinInterval;
1837 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL, mask))
1838 conf->dot11MeshHWMPperrMinInterval =
1839 nconf->dot11MeshHWMPperrMinInterval;
1840 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
1841 mask))
1842 conf->dot11MeshHWMPnetDiameterTraversalTime =
1843 nconf->dot11MeshHWMPnetDiameterTraversalTime;
1844 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ROOTMODE, mask)) {
1845 conf->dot11MeshHWMPRootMode = nconf->dot11MeshHWMPRootMode;
1846 ieee80211_mesh_root_setup(ifmsh);
1848 if (_chg_mesh_attr(NL80211_MESHCONF_GATE_ANNOUNCEMENTS, mask)) {
1849 /* our current gate announcement implementation rides on root
1850 * announcements, so require this ifmsh to also be a root node
1851 * */
1852 if (nconf->dot11MeshGateAnnouncementProtocol &&
1853 !(conf->dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)) {
1854 conf->dot11MeshHWMPRootMode = IEEE80211_PROACTIVE_RANN;
1855 ieee80211_mesh_root_setup(ifmsh);
1857 conf->dot11MeshGateAnnouncementProtocol =
1858 nconf->dot11MeshGateAnnouncementProtocol;
1860 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_RANN_INTERVAL, mask))
1861 conf->dot11MeshHWMPRannInterval =
1862 nconf->dot11MeshHWMPRannInterval;
1863 if (_chg_mesh_attr(NL80211_MESHCONF_FORWARDING, mask))
1864 conf->dot11MeshForwarding = nconf->dot11MeshForwarding;
1865 if (_chg_mesh_attr(NL80211_MESHCONF_RSSI_THRESHOLD, mask)) {
1866 /* our RSSI threshold implementation is supported only for
1867 * devices that report signal in dBm.
1869 if (!(sdata->local->hw.flags & IEEE80211_HW_SIGNAL_DBM))
1870 return -ENOTSUPP;
1871 conf->rssi_threshold = nconf->rssi_threshold;
1873 if (_chg_mesh_attr(NL80211_MESHCONF_HT_OPMODE, mask)) {
1874 conf->ht_opmode = nconf->ht_opmode;
1875 sdata->vif.bss_conf.ht_operation_mode = nconf->ht_opmode;
1876 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_HT);
1878 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT, mask))
1879 conf->dot11MeshHWMPactivePathToRootTimeout =
1880 nconf->dot11MeshHWMPactivePathToRootTimeout;
1881 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ROOT_INTERVAL, mask))
1882 conf->dot11MeshHWMProotInterval =
1883 nconf->dot11MeshHWMProotInterval;
1884 if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL, mask))
1885 conf->dot11MeshHWMPconfirmationInterval =
1886 nconf->dot11MeshHWMPconfirmationInterval;
1887 if (_chg_mesh_attr(NL80211_MESHCONF_POWER_MODE, mask)) {
1888 conf->power_mode = nconf->power_mode;
1889 ieee80211_mps_local_status_update(sdata);
1891 if (_chg_mesh_attr(NL80211_MESHCONF_AWAKE_WINDOW, mask))
1892 conf->dot11MeshAwakeWindowDuration =
1893 nconf->dot11MeshAwakeWindowDuration;
1894 if (_chg_mesh_attr(NL80211_MESHCONF_PLINK_TIMEOUT, mask))
1895 conf->plink_timeout = nconf->plink_timeout;
1896 ieee80211_mbss_info_change_notify(sdata, BSS_CHANGED_BEACON);
1897 return 0;
1900 static int ieee80211_join_mesh(struct wiphy *wiphy, struct net_device *dev,
1901 const struct mesh_config *conf,
1902 const struct mesh_setup *setup)
1904 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1905 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1906 int err;
1908 memcpy(&ifmsh->mshcfg, conf, sizeof(struct mesh_config));
1909 err = copy_mesh_setup(ifmsh, setup);
1910 if (err)
1911 return err;
1913 /* can mesh use other SMPS modes? */
1914 sdata->smps_mode = IEEE80211_SMPS_OFF;
1915 sdata->needed_rx_chains = sdata->local->rx_chains;
1917 err = ieee80211_vif_use_channel(sdata, &setup->chandef,
1918 IEEE80211_CHANCTX_SHARED);
1919 if (err)
1920 return err;
1922 return ieee80211_start_mesh(sdata);
1925 static int ieee80211_leave_mesh(struct wiphy *wiphy, struct net_device *dev)
1927 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1929 ieee80211_stop_mesh(sdata);
1930 ieee80211_vif_release_channel(sdata);
1932 return 0;
1934 #endif
1936 static int ieee80211_change_bss(struct wiphy *wiphy,
1937 struct net_device *dev,
1938 struct bss_parameters *params)
1940 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1941 enum ieee80211_band band;
1942 u32 changed = 0;
1944 if (!rtnl_dereference(sdata->u.ap.beacon))
1945 return -ENOENT;
1947 band = ieee80211_get_sdata_band(sdata);
1949 if (params->use_cts_prot >= 0) {
1950 sdata->vif.bss_conf.use_cts_prot = params->use_cts_prot;
1951 changed |= BSS_CHANGED_ERP_CTS_PROT;
1953 if (params->use_short_preamble >= 0) {
1954 sdata->vif.bss_conf.use_short_preamble =
1955 params->use_short_preamble;
1956 changed |= BSS_CHANGED_ERP_PREAMBLE;
1959 if (!sdata->vif.bss_conf.use_short_slot &&
1960 band == IEEE80211_BAND_5GHZ) {
1961 sdata->vif.bss_conf.use_short_slot = true;
1962 changed |= BSS_CHANGED_ERP_SLOT;
1965 if (params->use_short_slot_time >= 0) {
1966 sdata->vif.bss_conf.use_short_slot =
1967 params->use_short_slot_time;
1968 changed |= BSS_CHANGED_ERP_SLOT;
1971 if (params->basic_rates) {
1972 ieee80211_parse_bitrates(&sdata->vif.bss_conf.chandef,
1973 wiphy->bands[band],
1974 params->basic_rates,
1975 params->basic_rates_len,
1976 &sdata->vif.bss_conf.basic_rates);
1977 changed |= BSS_CHANGED_BASIC_RATES;
1980 if (params->ap_isolate >= 0) {
1981 if (params->ap_isolate)
1982 sdata->flags |= IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
1983 else
1984 sdata->flags &= ~IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
1987 if (params->ht_opmode >= 0) {
1988 sdata->vif.bss_conf.ht_operation_mode =
1989 (u16) params->ht_opmode;
1990 changed |= BSS_CHANGED_HT;
1993 if (params->p2p_ctwindow >= 0) {
1994 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow &=
1995 ~IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
1996 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow |=
1997 params->p2p_ctwindow & IEEE80211_P2P_OPPPS_CTWINDOW_MASK;
1998 changed |= BSS_CHANGED_P2P_PS;
2001 if (params->p2p_opp_ps > 0) {
2002 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow |=
2003 IEEE80211_P2P_OPPPS_ENABLE_BIT;
2004 changed |= BSS_CHANGED_P2P_PS;
2005 } else if (params->p2p_opp_ps == 0) {
2006 sdata->vif.bss_conf.p2p_noa_attr.oppps_ctwindow &=
2007 ~IEEE80211_P2P_OPPPS_ENABLE_BIT;
2008 changed |= BSS_CHANGED_P2P_PS;
2011 ieee80211_bss_info_change_notify(sdata, changed);
2013 return 0;
2016 static int ieee80211_set_txq_params(struct wiphy *wiphy,
2017 struct net_device *dev,
2018 struct ieee80211_txq_params *params)
2020 struct ieee80211_local *local = wiphy_priv(wiphy);
2021 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2022 struct ieee80211_tx_queue_params p;
2024 if (!local->ops->conf_tx)
2025 return -EOPNOTSUPP;
2027 if (local->hw.queues < IEEE80211_NUM_ACS)
2028 return -EOPNOTSUPP;
2030 memset(&p, 0, sizeof(p));
2031 p.aifs = params->aifs;
2032 p.cw_max = params->cwmax;
2033 p.cw_min = params->cwmin;
2034 p.txop = params->txop;
2037 * Setting tx queue params disables u-apsd because it's only
2038 * called in master mode.
2040 p.uapsd = false;
2042 sdata->tx_conf[params->ac] = p;
2043 if (drv_conf_tx(local, sdata, params->ac, &p)) {
2044 wiphy_debug(local->hw.wiphy,
2045 "failed to set TX queue parameters for AC %d\n",
2046 params->ac);
2047 return -EINVAL;
2050 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
2052 return 0;
2055 #ifdef CONFIG_PM
2056 static int ieee80211_suspend(struct wiphy *wiphy,
2057 struct cfg80211_wowlan *wowlan)
2059 return __ieee80211_suspend(wiphy_priv(wiphy), wowlan);
2062 static int ieee80211_resume(struct wiphy *wiphy)
2064 return __ieee80211_resume(wiphy_priv(wiphy));
2066 #else
2067 #define ieee80211_suspend NULL
2068 #define ieee80211_resume NULL
2069 #endif
2071 static int ieee80211_scan(struct wiphy *wiphy,
2072 struct cfg80211_scan_request *req)
2074 struct ieee80211_sub_if_data *sdata;
2076 sdata = IEEE80211_WDEV_TO_SUB_IF(req->wdev);
2078 switch (ieee80211_vif_type_p2p(&sdata->vif)) {
2079 case NL80211_IFTYPE_STATION:
2080 case NL80211_IFTYPE_ADHOC:
2081 case NL80211_IFTYPE_MESH_POINT:
2082 case NL80211_IFTYPE_P2P_CLIENT:
2083 case NL80211_IFTYPE_P2P_DEVICE:
2084 break;
2085 case NL80211_IFTYPE_P2P_GO:
2086 if (sdata->local->ops->hw_scan)
2087 break;
2089 * FIXME: implement NoA while scanning in software,
2090 * for now fall through to allow scanning only when
2091 * beaconing hasn't been configured yet
2093 case NL80211_IFTYPE_AP:
2095 * If the scan has been forced (and the driver supports
2096 * forcing), don't care about being beaconing already.
2097 * This will create problems to the attached stations (e.g. all
2098 * the frames sent while scanning on other channel will be
2099 * lost)
2101 if (sdata->u.ap.beacon &&
2102 (!(wiphy->features & NL80211_FEATURE_AP_SCAN) ||
2103 !(req->flags & NL80211_SCAN_FLAG_AP)))
2104 return -EOPNOTSUPP;
2105 break;
2106 default:
2107 return -EOPNOTSUPP;
2110 return ieee80211_request_scan(sdata, req);
2113 static int
2114 ieee80211_sched_scan_start(struct wiphy *wiphy,
2115 struct net_device *dev,
2116 struct cfg80211_sched_scan_request *req)
2118 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2120 if (!sdata->local->ops->sched_scan_start)
2121 return -EOPNOTSUPP;
2123 return ieee80211_request_sched_scan_start(sdata, req);
2126 static int
2127 ieee80211_sched_scan_stop(struct wiphy *wiphy, struct net_device *dev)
2129 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2131 if (!sdata->local->ops->sched_scan_stop)
2132 return -EOPNOTSUPP;
2134 return ieee80211_request_sched_scan_stop(sdata);
2137 static int ieee80211_auth(struct wiphy *wiphy, struct net_device *dev,
2138 struct cfg80211_auth_request *req)
2140 return ieee80211_mgd_auth(IEEE80211_DEV_TO_SUB_IF(dev), req);
2143 static int ieee80211_assoc(struct wiphy *wiphy, struct net_device *dev,
2144 struct cfg80211_assoc_request *req)
2146 return ieee80211_mgd_assoc(IEEE80211_DEV_TO_SUB_IF(dev), req);
2149 static int ieee80211_deauth(struct wiphy *wiphy, struct net_device *dev,
2150 struct cfg80211_deauth_request *req)
2152 return ieee80211_mgd_deauth(IEEE80211_DEV_TO_SUB_IF(dev), req);
2155 static int ieee80211_disassoc(struct wiphy *wiphy, struct net_device *dev,
2156 struct cfg80211_disassoc_request *req)
2158 return ieee80211_mgd_disassoc(IEEE80211_DEV_TO_SUB_IF(dev), req);
2161 static int ieee80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2162 struct cfg80211_ibss_params *params)
2164 return ieee80211_ibss_join(IEEE80211_DEV_TO_SUB_IF(dev), params);
2167 static int ieee80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2169 return ieee80211_ibss_leave(IEEE80211_DEV_TO_SUB_IF(dev));
2172 static int ieee80211_set_mcast_rate(struct wiphy *wiphy, struct net_device *dev,
2173 int rate[IEEE80211_NUM_BANDS])
2175 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2177 memcpy(sdata->vif.bss_conf.mcast_rate, rate,
2178 sizeof(int) * IEEE80211_NUM_BANDS);
2180 return 0;
2183 static int ieee80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
2185 struct ieee80211_local *local = wiphy_priv(wiphy);
2186 int err;
2188 if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
2189 err = drv_set_frag_threshold(local, wiphy->frag_threshold);
2191 if (err)
2192 return err;
2195 if (changed & WIPHY_PARAM_COVERAGE_CLASS) {
2196 err = drv_set_coverage_class(local, wiphy->coverage_class);
2198 if (err)
2199 return err;
2202 if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
2203 err = drv_set_rts_threshold(local, wiphy->rts_threshold);
2205 if (err)
2206 return err;
2209 if (changed & WIPHY_PARAM_RETRY_SHORT) {
2210 if (wiphy->retry_short > IEEE80211_MAX_TX_RETRY)
2211 return -EINVAL;
2212 local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
2214 if (changed & WIPHY_PARAM_RETRY_LONG) {
2215 if (wiphy->retry_long > IEEE80211_MAX_TX_RETRY)
2216 return -EINVAL;
2217 local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
2219 if (changed &
2220 (WIPHY_PARAM_RETRY_SHORT | WIPHY_PARAM_RETRY_LONG))
2221 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_RETRY_LIMITS);
2223 return 0;
2226 static int ieee80211_set_tx_power(struct wiphy *wiphy,
2227 struct wireless_dev *wdev,
2228 enum nl80211_tx_power_setting type, int mbm)
2230 struct ieee80211_local *local = wiphy_priv(wiphy);
2231 struct ieee80211_sub_if_data *sdata;
2233 if (wdev) {
2234 sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2236 switch (type) {
2237 case NL80211_TX_POWER_AUTOMATIC:
2238 sdata->user_power_level = IEEE80211_UNSET_POWER_LEVEL;
2239 break;
2240 case NL80211_TX_POWER_LIMITED:
2241 case NL80211_TX_POWER_FIXED:
2242 if (mbm < 0 || (mbm % 100))
2243 return -EOPNOTSUPP;
2244 sdata->user_power_level = MBM_TO_DBM(mbm);
2245 break;
2248 ieee80211_recalc_txpower(sdata);
2250 return 0;
2253 switch (type) {
2254 case NL80211_TX_POWER_AUTOMATIC:
2255 local->user_power_level = IEEE80211_UNSET_POWER_LEVEL;
2256 break;
2257 case NL80211_TX_POWER_LIMITED:
2258 case NL80211_TX_POWER_FIXED:
2259 if (mbm < 0 || (mbm % 100))
2260 return -EOPNOTSUPP;
2261 local->user_power_level = MBM_TO_DBM(mbm);
2262 break;
2265 mutex_lock(&local->iflist_mtx);
2266 list_for_each_entry(sdata, &local->interfaces, list)
2267 sdata->user_power_level = local->user_power_level;
2268 list_for_each_entry(sdata, &local->interfaces, list)
2269 ieee80211_recalc_txpower(sdata);
2270 mutex_unlock(&local->iflist_mtx);
2272 return 0;
2275 static int ieee80211_get_tx_power(struct wiphy *wiphy,
2276 struct wireless_dev *wdev,
2277 int *dbm)
2279 struct ieee80211_local *local = wiphy_priv(wiphy);
2280 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2282 if (!local->use_chanctx)
2283 *dbm = local->hw.conf.power_level;
2284 else
2285 *dbm = sdata->vif.bss_conf.txpower;
2287 return 0;
2290 static int ieee80211_set_wds_peer(struct wiphy *wiphy, struct net_device *dev,
2291 const u8 *addr)
2293 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2295 memcpy(&sdata->u.wds.remote_addr, addr, ETH_ALEN);
2297 return 0;
2300 static void ieee80211_rfkill_poll(struct wiphy *wiphy)
2302 struct ieee80211_local *local = wiphy_priv(wiphy);
2304 drv_rfkill_poll(local);
2307 #ifdef CONFIG_NL80211_TESTMODE
2308 static int ieee80211_testmode_cmd(struct wiphy *wiphy,
2309 struct wireless_dev *wdev,
2310 void *data, int len)
2312 struct ieee80211_local *local = wiphy_priv(wiphy);
2313 struct ieee80211_vif *vif = NULL;
2315 if (!local->ops->testmode_cmd)
2316 return -EOPNOTSUPP;
2318 if (wdev) {
2319 struct ieee80211_sub_if_data *sdata;
2321 sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2322 if (sdata->flags & IEEE80211_SDATA_IN_DRIVER)
2323 vif = &sdata->vif;
2326 return local->ops->testmode_cmd(&local->hw, vif, data, len);
2329 static int ieee80211_testmode_dump(struct wiphy *wiphy,
2330 struct sk_buff *skb,
2331 struct netlink_callback *cb,
2332 void *data, int len)
2334 struct ieee80211_local *local = wiphy_priv(wiphy);
2336 if (!local->ops->testmode_dump)
2337 return -EOPNOTSUPP;
2339 return local->ops->testmode_dump(&local->hw, skb, cb, data, len);
2341 #endif
2343 int __ieee80211_request_smps(struct ieee80211_sub_if_data *sdata,
2344 enum ieee80211_smps_mode smps_mode)
2346 const u8 *ap;
2347 enum ieee80211_smps_mode old_req;
2348 int err;
2350 lockdep_assert_held(&sdata->wdev.mtx);
2352 old_req = sdata->u.mgd.req_smps;
2353 sdata->u.mgd.req_smps = smps_mode;
2355 if (old_req == smps_mode &&
2356 smps_mode != IEEE80211_SMPS_AUTOMATIC)
2357 return 0;
2360 * If not associated, or current association is not an HT
2361 * association, there's no need to do anything, just store
2362 * the new value until we associate.
2364 if (!sdata->u.mgd.associated ||
2365 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
2366 return 0;
2368 ap = sdata->u.mgd.associated->bssid;
2370 if (smps_mode == IEEE80211_SMPS_AUTOMATIC) {
2371 if (sdata->u.mgd.powersave)
2372 smps_mode = IEEE80211_SMPS_DYNAMIC;
2373 else
2374 smps_mode = IEEE80211_SMPS_OFF;
2377 /* send SM PS frame to AP */
2378 err = ieee80211_send_smps_action(sdata, smps_mode,
2379 ap, ap);
2380 if (err)
2381 sdata->u.mgd.req_smps = old_req;
2383 return err;
2386 static int ieee80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
2387 bool enabled, int timeout)
2389 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2390 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2392 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2393 return -EOPNOTSUPP;
2395 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
2396 return -EOPNOTSUPP;
2398 if (enabled == sdata->u.mgd.powersave &&
2399 timeout == local->dynamic_ps_forced_timeout)
2400 return 0;
2402 sdata->u.mgd.powersave = enabled;
2403 local->dynamic_ps_forced_timeout = timeout;
2405 /* no change, but if automatic follow powersave */
2406 sdata_lock(sdata);
2407 __ieee80211_request_smps(sdata, sdata->u.mgd.req_smps);
2408 sdata_unlock(sdata);
2410 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
2411 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2413 ieee80211_recalc_ps(local, -1);
2414 ieee80211_recalc_ps_vif(sdata);
2416 return 0;
2419 static int ieee80211_set_cqm_rssi_config(struct wiphy *wiphy,
2420 struct net_device *dev,
2421 s32 rssi_thold, u32 rssi_hyst)
2423 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2424 struct ieee80211_vif *vif = &sdata->vif;
2425 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
2427 if (rssi_thold == bss_conf->cqm_rssi_thold &&
2428 rssi_hyst == bss_conf->cqm_rssi_hyst)
2429 return 0;
2431 bss_conf->cqm_rssi_thold = rssi_thold;
2432 bss_conf->cqm_rssi_hyst = rssi_hyst;
2434 /* tell the driver upon association, unless already associated */
2435 if (sdata->u.mgd.associated &&
2436 sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)
2437 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_CQM);
2439 return 0;
2442 static int ieee80211_set_bitrate_mask(struct wiphy *wiphy,
2443 struct net_device *dev,
2444 const u8 *addr,
2445 const struct cfg80211_bitrate_mask *mask)
2447 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2448 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2449 int i, ret;
2451 if (!ieee80211_sdata_running(sdata))
2452 return -ENETDOWN;
2454 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) {
2455 ret = drv_set_bitrate_mask(local, sdata, mask);
2456 if (ret)
2457 return ret;
2460 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
2461 struct ieee80211_supported_band *sband = wiphy->bands[i];
2462 int j;
2464 sdata->rc_rateidx_mask[i] = mask->control[i].legacy;
2465 memcpy(sdata->rc_rateidx_mcs_mask[i], mask->control[i].mcs,
2466 sizeof(mask->control[i].mcs));
2468 sdata->rc_has_mcs_mask[i] = false;
2469 if (!sband)
2470 continue;
2472 for (j = 0; j < IEEE80211_HT_MCS_MASK_LEN; j++)
2473 if (~sdata->rc_rateidx_mcs_mask[i][j]) {
2474 sdata->rc_has_mcs_mask[i] = true;
2475 break;
2479 return 0;
2482 static int ieee80211_start_roc_work(struct ieee80211_local *local,
2483 struct ieee80211_sub_if_data *sdata,
2484 struct ieee80211_channel *channel,
2485 unsigned int duration, u64 *cookie,
2486 struct sk_buff *txskb,
2487 enum ieee80211_roc_type type)
2489 struct ieee80211_roc_work *roc, *tmp;
2490 bool queued = false;
2491 int ret;
2493 lockdep_assert_held(&local->mtx);
2495 if (local->use_chanctx && !local->ops->remain_on_channel)
2496 return -EOPNOTSUPP;
2498 roc = kzalloc(sizeof(*roc), GFP_KERNEL);
2499 if (!roc)
2500 return -ENOMEM;
2502 roc->chan = channel;
2503 roc->duration = duration;
2504 roc->req_duration = duration;
2505 roc->frame = txskb;
2506 roc->type = type;
2507 roc->mgmt_tx_cookie = (unsigned long)txskb;
2508 roc->sdata = sdata;
2509 INIT_DELAYED_WORK(&roc->work, ieee80211_sw_roc_work);
2510 INIT_LIST_HEAD(&roc->dependents);
2513 * cookie is either the roc cookie (for normal roc)
2514 * or the SKB (for mgmt TX)
2516 if (!txskb) {
2517 /* local->mtx protects this */
2518 local->roc_cookie_counter++;
2519 roc->cookie = local->roc_cookie_counter;
2520 /* wow, you wrapped 64 bits ... more likely a bug */
2521 if (WARN_ON(roc->cookie == 0)) {
2522 roc->cookie = 1;
2523 local->roc_cookie_counter++;
2525 *cookie = roc->cookie;
2526 } else {
2527 *cookie = (unsigned long)txskb;
2530 /* if there's one pending or we're scanning, queue this one */
2531 if (!list_empty(&local->roc_list) ||
2532 local->scanning || local->radar_detect_enabled)
2533 goto out_check_combine;
2535 /* if not HW assist, just queue & schedule work */
2536 if (!local->ops->remain_on_channel) {
2537 ieee80211_queue_delayed_work(&local->hw, &roc->work, 0);
2538 goto out_queue;
2541 /* otherwise actually kick it off here (for error handling) */
2544 * If the duration is zero, then the driver
2545 * wouldn't actually do anything. Set it to
2546 * 10 for now.
2548 * TODO: cancel the off-channel operation
2549 * when we get the SKB's TX status and
2550 * the wait time was zero before.
2552 if (!duration)
2553 duration = 10;
2555 ret = drv_remain_on_channel(local, sdata, channel, duration, type);
2556 if (ret) {
2557 kfree(roc);
2558 return ret;
2561 roc->started = true;
2562 goto out_queue;
2564 out_check_combine:
2565 list_for_each_entry(tmp, &local->roc_list, list) {
2566 if (tmp->chan != channel || tmp->sdata != sdata)
2567 continue;
2570 * Extend this ROC if possible:
2572 * If it hasn't started yet, just increase the duration
2573 * and add the new one to the list of dependents.
2574 * If the type of the new ROC has higher priority, modify the
2575 * type of the previous one to match that of the new one.
2577 if (!tmp->started) {
2578 list_add_tail(&roc->list, &tmp->dependents);
2579 tmp->duration = max(tmp->duration, roc->duration);
2580 tmp->type = max(tmp->type, roc->type);
2581 queued = true;
2582 break;
2585 /* If it has already started, it's more difficult ... */
2586 if (local->ops->remain_on_channel) {
2587 unsigned long j = jiffies;
2590 * In the offloaded ROC case, if it hasn't begun, add
2591 * this new one to the dependent list to be handled
2592 * when the master one begins. If it has begun,
2593 * check that there's still a minimum time left and
2594 * if so, start this one, transmitting the frame, but
2595 * add it to the list directly after this one with
2596 * a reduced time so we'll ask the driver to execute
2597 * it right after finishing the previous one, in the
2598 * hope that it'll also be executed right afterwards,
2599 * effectively extending the old one.
2600 * If there's no minimum time left, just add it to the
2601 * normal list.
2602 * TODO: the ROC type is ignored here, assuming that it
2603 * is better to immediately use the current ROC.
2605 if (!tmp->hw_begun) {
2606 list_add_tail(&roc->list, &tmp->dependents);
2607 queued = true;
2608 break;
2611 if (time_before(j + IEEE80211_ROC_MIN_LEFT,
2612 tmp->hw_start_time +
2613 msecs_to_jiffies(tmp->duration))) {
2614 int new_dur;
2616 ieee80211_handle_roc_started(roc);
2618 new_dur = roc->duration -
2619 jiffies_to_msecs(tmp->hw_start_time +
2620 msecs_to_jiffies(
2621 tmp->duration) -
2624 if (new_dur > 0) {
2625 /* add right after tmp */
2626 list_add(&roc->list, &tmp->list);
2627 } else {
2628 list_add_tail(&roc->list,
2629 &tmp->dependents);
2631 queued = true;
2633 } else if (del_timer_sync(&tmp->work.timer)) {
2634 unsigned long new_end;
2637 * In the software ROC case, cancel the timer, if
2638 * that fails then the finish work is already
2639 * queued/pending and thus we queue the new ROC
2640 * normally, if that succeeds then we can extend
2641 * the timer duration and TX the frame (if any.)
2644 list_add_tail(&roc->list, &tmp->dependents);
2645 queued = true;
2647 new_end = jiffies + msecs_to_jiffies(roc->duration);
2649 /* ok, it was started & we canceled timer */
2650 if (time_after(new_end, tmp->work.timer.expires))
2651 mod_timer(&tmp->work.timer, new_end);
2652 else
2653 add_timer(&tmp->work.timer);
2655 ieee80211_handle_roc_started(roc);
2657 break;
2660 out_queue:
2661 if (!queued)
2662 list_add_tail(&roc->list, &local->roc_list);
2664 return 0;
2667 static int ieee80211_remain_on_channel(struct wiphy *wiphy,
2668 struct wireless_dev *wdev,
2669 struct ieee80211_channel *chan,
2670 unsigned int duration,
2671 u64 *cookie)
2673 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2674 struct ieee80211_local *local = sdata->local;
2675 int ret;
2677 mutex_lock(&local->mtx);
2678 ret = ieee80211_start_roc_work(local, sdata, chan,
2679 duration, cookie, NULL,
2680 IEEE80211_ROC_TYPE_NORMAL);
2681 mutex_unlock(&local->mtx);
2683 return ret;
2686 static int ieee80211_cancel_roc(struct ieee80211_local *local,
2687 u64 cookie, bool mgmt_tx)
2689 struct ieee80211_roc_work *roc, *tmp, *found = NULL;
2690 int ret;
2692 mutex_lock(&local->mtx);
2693 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
2694 struct ieee80211_roc_work *dep, *tmp2;
2696 list_for_each_entry_safe(dep, tmp2, &roc->dependents, list) {
2697 if (!mgmt_tx && dep->cookie != cookie)
2698 continue;
2699 else if (mgmt_tx && dep->mgmt_tx_cookie != cookie)
2700 continue;
2701 /* found dependent item -- just remove it */
2702 list_del(&dep->list);
2703 mutex_unlock(&local->mtx);
2705 ieee80211_roc_notify_destroy(dep, true);
2706 return 0;
2709 if (!mgmt_tx && roc->cookie != cookie)
2710 continue;
2711 else if (mgmt_tx && roc->mgmt_tx_cookie != cookie)
2712 continue;
2714 found = roc;
2715 break;
2718 if (!found) {
2719 mutex_unlock(&local->mtx);
2720 return -ENOENT;
2724 * We found the item to cancel, so do that. Note that it
2725 * may have dependents, which we also cancel (and send
2726 * the expired signal for.) Not doing so would be quite
2727 * tricky here, but we may need to fix it later.
2730 if (local->ops->remain_on_channel) {
2731 if (found->started) {
2732 ret = drv_cancel_remain_on_channel(local);
2733 if (WARN_ON_ONCE(ret)) {
2734 mutex_unlock(&local->mtx);
2735 return ret;
2739 list_del(&found->list);
2741 if (found->started)
2742 ieee80211_start_next_roc(local);
2743 mutex_unlock(&local->mtx);
2745 ieee80211_roc_notify_destroy(found, true);
2746 } else {
2747 /* work may be pending so use it all the time */
2748 found->abort = true;
2749 ieee80211_queue_delayed_work(&local->hw, &found->work, 0);
2751 mutex_unlock(&local->mtx);
2753 /* work will clean up etc */
2754 flush_delayed_work(&found->work);
2755 WARN_ON(!found->to_be_freed);
2756 kfree(found);
2759 return 0;
2762 static int ieee80211_cancel_remain_on_channel(struct wiphy *wiphy,
2763 struct wireless_dev *wdev,
2764 u64 cookie)
2766 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2767 struct ieee80211_local *local = sdata->local;
2769 return ieee80211_cancel_roc(local, cookie, false);
2772 static int ieee80211_start_radar_detection(struct wiphy *wiphy,
2773 struct net_device *dev,
2774 struct cfg80211_chan_def *chandef)
2776 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2777 struct ieee80211_local *local = sdata->local;
2778 unsigned long timeout;
2779 int err;
2781 if (!list_empty(&local->roc_list) || local->scanning)
2782 return -EBUSY;
2784 /* whatever, but channel contexts should not complain about that one */
2785 sdata->smps_mode = IEEE80211_SMPS_OFF;
2786 sdata->needed_rx_chains = local->rx_chains;
2787 sdata->radar_required = true;
2789 mutex_lock(&local->iflist_mtx);
2790 err = ieee80211_vif_use_channel(sdata, chandef,
2791 IEEE80211_CHANCTX_SHARED);
2792 mutex_unlock(&local->iflist_mtx);
2793 if (err)
2794 return err;
2796 timeout = msecs_to_jiffies(IEEE80211_DFS_MIN_CAC_TIME_MS);
2797 ieee80211_queue_delayed_work(&sdata->local->hw,
2798 &sdata->dfs_cac_timer_work, timeout);
2800 return 0;
2803 static struct cfg80211_beacon_data *
2804 cfg80211_beacon_dup(struct cfg80211_beacon_data *beacon)
2806 struct cfg80211_beacon_data *new_beacon;
2807 u8 *pos;
2808 int len;
2810 len = beacon->head_len + beacon->tail_len + beacon->beacon_ies_len +
2811 beacon->proberesp_ies_len + beacon->assocresp_ies_len +
2812 beacon->probe_resp_len;
2814 new_beacon = kzalloc(sizeof(*new_beacon) + len, GFP_KERNEL);
2815 if (!new_beacon)
2816 return NULL;
2818 pos = (u8 *)(new_beacon + 1);
2819 if (beacon->head_len) {
2820 new_beacon->head_len = beacon->head_len;
2821 new_beacon->head = pos;
2822 memcpy(pos, beacon->head, beacon->head_len);
2823 pos += beacon->head_len;
2825 if (beacon->tail_len) {
2826 new_beacon->tail_len = beacon->tail_len;
2827 new_beacon->tail = pos;
2828 memcpy(pos, beacon->tail, beacon->tail_len);
2829 pos += beacon->tail_len;
2831 if (beacon->beacon_ies_len) {
2832 new_beacon->beacon_ies_len = beacon->beacon_ies_len;
2833 new_beacon->beacon_ies = pos;
2834 memcpy(pos, beacon->beacon_ies, beacon->beacon_ies_len);
2835 pos += beacon->beacon_ies_len;
2837 if (beacon->proberesp_ies_len) {
2838 new_beacon->proberesp_ies_len = beacon->proberesp_ies_len;
2839 new_beacon->proberesp_ies = pos;
2840 memcpy(pos, beacon->proberesp_ies, beacon->proberesp_ies_len);
2841 pos += beacon->proberesp_ies_len;
2843 if (beacon->assocresp_ies_len) {
2844 new_beacon->assocresp_ies_len = beacon->assocresp_ies_len;
2845 new_beacon->assocresp_ies = pos;
2846 memcpy(pos, beacon->assocresp_ies, beacon->assocresp_ies_len);
2847 pos += beacon->assocresp_ies_len;
2849 if (beacon->probe_resp_len) {
2850 new_beacon->probe_resp_len = beacon->probe_resp_len;
2851 beacon->probe_resp = pos;
2852 memcpy(pos, beacon->probe_resp, beacon->probe_resp_len);
2853 pos += beacon->probe_resp_len;
2856 return new_beacon;
2859 void ieee80211_csa_finalize_work(struct work_struct *work)
2861 struct ieee80211_sub_if_data *sdata =
2862 container_of(work, struct ieee80211_sub_if_data,
2863 csa_finalize_work);
2864 struct ieee80211_local *local = sdata->local;
2865 int err, changed;
2867 if (!ieee80211_sdata_running(sdata))
2868 return;
2870 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
2871 return;
2873 sdata->radar_required = sdata->csa_radar_required;
2874 err = ieee80211_vif_change_channel(sdata, &local->csa_chandef,
2875 &changed);
2876 if (WARN_ON(err < 0))
2877 return;
2879 err = ieee80211_assign_beacon(sdata, sdata->u.ap.next_beacon);
2880 if (err < 0)
2881 return;
2883 changed |= err;
2884 kfree(sdata->u.ap.next_beacon);
2885 sdata->u.ap.next_beacon = NULL;
2886 sdata->vif.csa_active = false;
2888 ieee80211_wake_queues_by_reason(&sdata->local->hw,
2889 IEEE80211_MAX_QUEUE_MAP,
2890 IEEE80211_QUEUE_STOP_REASON_CSA);
2892 ieee80211_bss_info_change_notify(sdata, changed);
2894 cfg80211_ch_switch_notify(sdata->dev, &local->csa_chandef);
2897 static int ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
2898 struct cfg80211_csa_settings *params)
2900 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2901 struct ieee80211_local *local = sdata->local;
2902 struct ieee80211_chanctx_conf *chanctx_conf;
2903 struct ieee80211_chanctx *chanctx;
2904 int err, num_chanctx;
2906 if (!list_empty(&local->roc_list) || local->scanning)
2907 return -EBUSY;
2909 if (sdata->wdev.cac_started)
2910 return -EBUSY;
2912 if (cfg80211_chandef_identical(&params->chandef,
2913 &sdata->vif.bss_conf.chandef))
2914 return -EINVAL;
2916 rcu_read_lock();
2917 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2918 if (!chanctx_conf) {
2919 rcu_read_unlock();
2920 return -EBUSY;
2923 /* don't handle for multi-VIF cases */
2924 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
2925 if (chanctx->refcount > 1) {
2926 rcu_read_unlock();
2927 return -EBUSY;
2929 num_chanctx = 0;
2930 list_for_each_entry_rcu(chanctx, &local->chanctx_list, list)
2931 num_chanctx++;
2932 rcu_read_unlock();
2934 if (num_chanctx > 1)
2935 return -EBUSY;
2937 /* don't allow another channel switch if one is already active. */
2938 if (sdata->vif.csa_active)
2939 return -EBUSY;
2941 /* only handle AP for now. */
2942 switch (sdata->vif.type) {
2943 case NL80211_IFTYPE_AP:
2944 break;
2945 default:
2946 return -EOPNOTSUPP;
2949 sdata->u.ap.next_beacon = cfg80211_beacon_dup(&params->beacon_after);
2950 if (!sdata->u.ap.next_beacon)
2951 return -ENOMEM;
2953 sdata->csa_counter_offset_beacon = params->counter_offset_beacon;
2954 sdata->csa_counter_offset_presp = params->counter_offset_presp;
2955 sdata->csa_radar_required = params->radar_required;
2957 if (params->block_tx)
2958 ieee80211_stop_queues_by_reason(&local->hw,
2959 IEEE80211_MAX_QUEUE_MAP,
2960 IEEE80211_QUEUE_STOP_REASON_CSA);
2962 err = ieee80211_assign_beacon(sdata, &params->beacon_csa);
2963 if (err < 0)
2964 return err;
2966 local->csa_chandef = params->chandef;
2967 sdata->vif.csa_active = true;
2969 ieee80211_bss_info_change_notify(sdata, err);
2970 drv_channel_switch_beacon(sdata, &params->chandef);
2972 return 0;
2975 static int ieee80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
2976 struct ieee80211_channel *chan, bool offchan,
2977 unsigned int wait, const u8 *buf, size_t len,
2978 bool no_cck, bool dont_wait_for_ack, u64 *cookie)
2980 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
2981 struct ieee80211_local *local = sdata->local;
2982 struct sk_buff *skb;
2983 struct sta_info *sta;
2984 const struct ieee80211_mgmt *mgmt = (void *)buf;
2985 bool need_offchan = false;
2986 u32 flags;
2987 int ret;
2989 if (dont_wait_for_ack)
2990 flags = IEEE80211_TX_CTL_NO_ACK;
2991 else
2992 flags = IEEE80211_TX_INTFL_NL80211_FRAME_TX |
2993 IEEE80211_TX_CTL_REQ_TX_STATUS;
2995 if (no_cck)
2996 flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
2998 switch (sdata->vif.type) {
2999 case NL80211_IFTYPE_ADHOC:
3000 if (!sdata->vif.bss_conf.ibss_joined)
3001 need_offchan = true;
3002 /* fall through */
3003 #ifdef CONFIG_MAC80211_MESH
3004 case NL80211_IFTYPE_MESH_POINT:
3005 if (ieee80211_vif_is_mesh(&sdata->vif) &&
3006 !sdata->u.mesh.mesh_id_len)
3007 need_offchan = true;
3008 /* fall through */
3009 #endif
3010 case NL80211_IFTYPE_AP:
3011 case NL80211_IFTYPE_AP_VLAN:
3012 case NL80211_IFTYPE_P2P_GO:
3013 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3014 !ieee80211_vif_is_mesh(&sdata->vif) &&
3015 !rcu_access_pointer(sdata->bss->beacon))
3016 need_offchan = true;
3017 if (!ieee80211_is_action(mgmt->frame_control) ||
3018 mgmt->u.action.category == WLAN_CATEGORY_PUBLIC ||
3019 mgmt->u.action.category == WLAN_CATEGORY_SELF_PROTECTED)
3020 break;
3021 rcu_read_lock();
3022 sta = sta_info_get(sdata, mgmt->da);
3023 rcu_read_unlock();
3024 if (!sta)
3025 return -ENOLINK;
3026 break;
3027 case NL80211_IFTYPE_STATION:
3028 case NL80211_IFTYPE_P2P_CLIENT:
3029 if (!sdata->u.mgd.associated)
3030 need_offchan = true;
3031 break;
3032 case NL80211_IFTYPE_P2P_DEVICE:
3033 need_offchan = true;
3034 break;
3035 default:
3036 return -EOPNOTSUPP;
3039 /* configurations requiring offchan cannot work if no channel has been
3040 * specified
3042 if (need_offchan && !chan)
3043 return -EINVAL;
3045 mutex_lock(&local->mtx);
3047 /* Check if the operating channel is the requested channel */
3048 if (!need_offchan) {
3049 struct ieee80211_chanctx_conf *chanctx_conf;
3051 rcu_read_lock();
3052 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3054 if (chanctx_conf) {
3055 need_offchan = chan && (chan != chanctx_conf->def.chan);
3056 } else if (!chan) {
3057 ret = -EINVAL;
3058 rcu_read_unlock();
3059 goto out_unlock;
3060 } else {
3061 need_offchan = true;
3063 rcu_read_unlock();
3066 if (need_offchan && !offchan) {
3067 ret = -EBUSY;
3068 goto out_unlock;
3071 skb = dev_alloc_skb(local->hw.extra_tx_headroom + len);
3072 if (!skb) {
3073 ret = -ENOMEM;
3074 goto out_unlock;
3076 skb_reserve(skb, local->hw.extra_tx_headroom);
3078 memcpy(skb_put(skb, len), buf, len);
3080 IEEE80211_SKB_CB(skb)->flags = flags;
3082 skb->dev = sdata->dev;
3084 if (!need_offchan) {
3085 *cookie = (unsigned long) skb;
3086 ieee80211_tx_skb(sdata, skb);
3087 ret = 0;
3088 goto out_unlock;
3091 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_TX_OFFCHAN |
3092 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
3093 if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
3094 IEEE80211_SKB_CB(skb)->hw_queue =
3095 local->hw.offchannel_tx_hw_queue;
3097 /* This will handle all kinds of coalescing and immediate TX */
3098 ret = ieee80211_start_roc_work(local, sdata, chan,
3099 wait, cookie, skb,
3100 IEEE80211_ROC_TYPE_MGMT_TX);
3101 if (ret)
3102 kfree_skb(skb);
3103 out_unlock:
3104 mutex_unlock(&local->mtx);
3105 return ret;
3108 static int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
3109 struct wireless_dev *wdev,
3110 u64 cookie)
3112 struct ieee80211_local *local = wiphy_priv(wiphy);
3114 return ieee80211_cancel_roc(local, cookie, true);
3117 static void ieee80211_mgmt_frame_register(struct wiphy *wiphy,
3118 struct wireless_dev *wdev,
3119 u16 frame_type, bool reg)
3121 struct ieee80211_local *local = wiphy_priv(wiphy);
3123 switch (frame_type) {
3124 case IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_PROBE_REQ:
3125 if (reg)
3126 local->probe_req_reg++;
3127 else
3128 local->probe_req_reg--;
3130 if (!local->open_count)
3131 break;
3133 ieee80211_queue_work(&local->hw, &local->reconfig_filter);
3134 break;
3135 default:
3136 break;
3140 static int ieee80211_set_antenna(struct wiphy *wiphy, u32 tx_ant, u32 rx_ant)
3142 struct ieee80211_local *local = wiphy_priv(wiphy);
3144 if (local->started)
3145 return -EOPNOTSUPP;
3147 return drv_set_antenna(local, tx_ant, rx_ant);
3150 static int ieee80211_get_antenna(struct wiphy *wiphy, u32 *tx_ant, u32 *rx_ant)
3152 struct ieee80211_local *local = wiphy_priv(wiphy);
3154 return drv_get_antenna(local, tx_ant, rx_ant);
3157 static int ieee80211_set_ringparam(struct wiphy *wiphy, u32 tx, u32 rx)
3159 struct ieee80211_local *local = wiphy_priv(wiphy);
3161 return drv_set_ringparam(local, tx, rx);
3164 static void ieee80211_get_ringparam(struct wiphy *wiphy,
3165 u32 *tx, u32 *tx_max, u32 *rx, u32 *rx_max)
3167 struct ieee80211_local *local = wiphy_priv(wiphy);
3169 drv_get_ringparam(local, tx, tx_max, rx, rx_max);
3172 static int ieee80211_set_rekey_data(struct wiphy *wiphy,
3173 struct net_device *dev,
3174 struct cfg80211_gtk_rekey_data *data)
3176 struct ieee80211_local *local = wiphy_priv(wiphy);
3177 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3179 if (!local->ops->set_rekey_data)
3180 return -EOPNOTSUPP;
3182 drv_set_rekey_data(local, sdata, data);
3184 return 0;
3187 static void ieee80211_tdls_add_ext_capab(struct sk_buff *skb)
3189 u8 *pos = (void *)skb_put(skb, 7);
3191 *pos++ = WLAN_EID_EXT_CAPABILITY;
3192 *pos++ = 5; /* len */
3193 *pos++ = 0x0;
3194 *pos++ = 0x0;
3195 *pos++ = 0x0;
3196 *pos++ = 0x0;
3197 *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
3200 static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata)
3202 struct ieee80211_local *local = sdata->local;
3203 u16 capab;
3205 capab = 0;
3206 if (ieee80211_get_sdata_band(sdata) != IEEE80211_BAND_2GHZ)
3207 return capab;
3209 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
3210 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
3211 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
3212 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
3214 return capab;
3217 static void ieee80211_tdls_add_link_ie(struct sk_buff *skb, u8 *src_addr,
3218 u8 *peer, u8 *bssid)
3220 struct ieee80211_tdls_lnkie *lnkid;
3222 lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
3224 lnkid->ie_type = WLAN_EID_LINK_ID;
3225 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
3227 memcpy(lnkid->bssid, bssid, ETH_ALEN);
3228 memcpy(lnkid->init_sta, src_addr, ETH_ALEN);
3229 memcpy(lnkid->resp_sta, peer, ETH_ALEN);
3232 static int
3233 ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
3234 u8 *peer, u8 action_code, u8 dialog_token,
3235 u16 status_code, struct sk_buff *skb)
3237 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3238 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
3239 struct ieee80211_tdls_data *tf;
3241 tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
3243 memcpy(tf->da, peer, ETH_ALEN);
3244 memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
3245 tf->ether_type = cpu_to_be16(ETH_P_TDLS);
3246 tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
3248 switch (action_code) {
3249 case WLAN_TDLS_SETUP_REQUEST:
3250 tf->category = WLAN_CATEGORY_TDLS;
3251 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
3253 skb_put(skb, sizeof(tf->u.setup_req));
3254 tf->u.setup_req.dialog_token = dialog_token;
3255 tf->u.setup_req.capability =
3256 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata));
3258 ieee80211_add_srates_ie(sdata, skb, false, band);
3259 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
3260 ieee80211_tdls_add_ext_capab(skb);
3261 break;
3262 case WLAN_TDLS_SETUP_RESPONSE:
3263 tf->category = WLAN_CATEGORY_TDLS;
3264 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
3266 skb_put(skb, sizeof(tf->u.setup_resp));
3267 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
3268 tf->u.setup_resp.dialog_token = dialog_token;
3269 tf->u.setup_resp.capability =
3270 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata));
3272 ieee80211_add_srates_ie(sdata, skb, false, band);
3273 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
3274 ieee80211_tdls_add_ext_capab(skb);
3275 break;
3276 case WLAN_TDLS_SETUP_CONFIRM:
3277 tf->category = WLAN_CATEGORY_TDLS;
3278 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
3280 skb_put(skb, sizeof(tf->u.setup_cfm));
3281 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
3282 tf->u.setup_cfm.dialog_token = dialog_token;
3283 break;
3284 case WLAN_TDLS_TEARDOWN:
3285 tf->category = WLAN_CATEGORY_TDLS;
3286 tf->action_code = WLAN_TDLS_TEARDOWN;
3288 skb_put(skb, sizeof(tf->u.teardown));
3289 tf->u.teardown.reason_code = cpu_to_le16(status_code);
3290 break;
3291 case WLAN_TDLS_DISCOVERY_REQUEST:
3292 tf->category = WLAN_CATEGORY_TDLS;
3293 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
3295 skb_put(skb, sizeof(tf->u.discover_req));
3296 tf->u.discover_req.dialog_token = dialog_token;
3297 break;
3298 default:
3299 return -EINVAL;
3302 return 0;
3305 static int
3306 ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
3307 u8 *peer, u8 action_code, u8 dialog_token,
3308 u16 status_code, struct sk_buff *skb)
3310 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3311 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
3312 struct ieee80211_mgmt *mgmt;
3314 mgmt = (void *)skb_put(skb, 24);
3315 memset(mgmt, 0, 24);
3316 memcpy(mgmt->da, peer, ETH_ALEN);
3317 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
3318 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
3320 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3321 IEEE80211_STYPE_ACTION);
3323 switch (action_code) {
3324 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
3325 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
3326 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
3327 mgmt->u.action.u.tdls_discover_resp.action_code =
3328 WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
3329 mgmt->u.action.u.tdls_discover_resp.dialog_token =
3330 dialog_token;
3331 mgmt->u.action.u.tdls_discover_resp.capability =
3332 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata));
3334 ieee80211_add_srates_ie(sdata, skb, false, band);
3335 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
3336 ieee80211_tdls_add_ext_capab(skb);
3337 break;
3338 default:
3339 return -EINVAL;
3342 return 0;
3345 static int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
3346 u8 *peer, u8 action_code, u8 dialog_token,
3347 u16 status_code, const u8 *extra_ies,
3348 size_t extra_ies_len)
3350 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3351 struct ieee80211_local *local = sdata->local;
3352 struct sk_buff *skb = NULL;
3353 bool send_direct;
3354 int ret;
3356 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
3357 return -ENOTSUPP;
3359 /* make sure we are in managed mode, and associated */
3360 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
3361 !sdata->u.mgd.associated)
3362 return -EINVAL;
3364 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM\n",
3365 action_code, peer);
3367 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
3368 max(sizeof(struct ieee80211_mgmt),
3369 sizeof(struct ieee80211_tdls_data)) +
3370 50 + /* supported rates */
3371 7 + /* ext capab */
3372 extra_ies_len +
3373 sizeof(struct ieee80211_tdls_lnkie));
3374 if (!skb)
3375 return -ENOMEM;
3377 skb_reserve(skb, local->hw.extra_tx_headroom);
3379 switch (action_code) {
3380 case WLAN_TDLS_SETUP_REQUEST:
3381 case WLAN_TDLS_SETUP_RESPONSE:
3382 case WLAN_TDLS_SETUP_CONFIRM:
3383 case WLAN_TDLS_TEARDOWN:
3384 case WLAN_TDLS_DISCOVERY_REQUEST:
3385 ret = ieee80211_prep_tdls_encap_data(wiphy, dev, peer,
3386 action_code, dialog_token,
3387 status_code, skb);
3388 send_direct = false;
3389 break;
3390 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
3391 ret = ieee80211_prep_tdls_direct(wiphy, dev, peer, action_code,
3392 dialog_token, status_code,
3393 skb);
3394 send_direct = true;
3395 break;
3396 default:
3397 ret = -ENOTSUPP;
3398 break;
3401 if (ret < 0)
3402 goto fail;
3404 if (extra_ies_len)
3405 memcpy(skb_put(skb, extra_ies_len), extra_ies, extra_ies_len);
3407 /* the TDLS link IE is always added last */
3408 switch (action_code) {
3409 case WLAN_TDLS_SETUP_REQUEST:
3410 case WLAN_TDLS_SETUP_CONFIRM:
3411 case WLAN_TDLS_TEARDOWN:
3412 case WLAN_TDLS_DISCOVERY_REQUEST:
3413 /* we are the initiator */
3414 ieee80211_tdls_add_link_ie(skb, sdata->vif.addr, peer,
3415 sdata->u.mgd.bssid);
3416 break;
3417 case WLAN_TDLS_SETUP_RESPONSE:
3418 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
3419 /* we are the responder */
3420 ieee80211_tdls_add_link_ie(skb, peer, sdata->vif.addr,
3421 sdata->u.mgd.bssid);
3422 break;
3423 default:
3424 ret = -ENOTSUPP;
3425 goto fail;
3428 if (send_direct) {
3429 ieee80211_tx_skb(sdata, skb);
3430 return 0;
3434 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
3435 * we should default to AC_VI.
3437 switch (action_code) {
3438 case WLAN_TDLS_SETUP_REQUEST:
3439 case WLAN_TDLS_SETUP_RESPONSE:
3440 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
3441 skb->priority = 2;
3442 break;
3443 default:
3444 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
3445 skb->priority = 5;
3446 break;
3449 /* disable bottom halves when entering the Tx path */
3450 local_bh_disable();
3451 ret = ieee80211_subif_start_xmit(skb, dev);
3452 local_bh_enable();
3454 return ret;
3456 fail:
3457 dev_kfree_skb(skb);
3458 return ret;
3461 static int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
3462 u8 *peer, enum nl80211_tdls_operation oper)
3464 struct sta_info *sta;
3465 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3467 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
3468 return -ENOTSUPP;
3470 if (sdata->vif.type != NL80211_IFTYPE_STATION)
3471 return -EINVAL;
3473 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
3475 switch (oper) {
3476 case NL80211_TDLS_ENABLE_LINK:
3477 rcu_read_lock();
3478 sta = sta_info_get(sdata, peer);
3479 if (!sta) {
3480 rcu_read_unlock();
3481 return -ENOLINK;
3484 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
3485 rcu_read_unlock();
3486 break;
3487 case NL80211_TDLS_DISABLE_LINK:
3488 return sta_info_destroy_addr(sdata, peer);
3489 case NL80211_TDLS_TEARDOWN:
3490 case NL80211_TDLS_SETUP:
3491 case NL80211_TDLS_DISCOVERY_REQ:
3492 /* We don't support in-driver setup/teardown/discovery */
3493 return -ENOTSUPP;
3494 default:
3495 return -ENOTSUPP;
3498 return 0;
3501 static int ieee80211_probe_client(struct wiphy *wiphy, struct net_device *dev,
3502 const u8 *peer, u64 *cookie)
3504 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3505 struct ieee80211_local *local = sdata->local;
3506 struct ieee80211_qos_hdr *nullfunc;
3507 struct sk_buff *skb;
3508 int size = sizeof(*nullfunc);
3509 __le16 fc;
3510 bool qos;
3511 struct ieee80211_tx_info *info;
3512 struct sta_info *sta;
3513 struct ieee80211_chanctx_conf *chanctx_conf;
3514 enum ieee80211_band band;
3516 rcu_read_lock();
3517 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3518 if (WARN_ON(!chanctx_conf)) {
3519 rcu_read_unlock();
3520 return -EINVAL;
3522 band = chanctx_conf->def.chan->band;
3523 sta = sta_info_get_bss(sdata, peer);
3524 if (sta) {
3525 qos = test_sta_flag(sta, WLAN_STA_WME);
3526 } else {
3527 rcu_read_unlock();
3528 return -ENOLINK;
3531 if (qos) {
3532 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
3533 IEEE80211_STYPE_QOS_NULLFUNC |
3534 IEEE80211_FCTL_FROMDS);
3535 } else {
3536 size -= 2;
3537 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
3538 IEEE80211_STYPE_NULLFUNC |
3539 IEEE80211_FCTL_FROMDS);
3542 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
3543 if (!skb) {
3544 rcu_read_unlock();
3545 return -ENOMEM;
3548 skb->dev = dev;
3550 skb_reserve(skb, local->hw.extra_tx_headroom);
3552 nullfunc = (void *) skb_put(skb, size);
3553 nullfunc->frame_control = fc;
3554 nullfunc->duration_id = 0;
3555 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
3556 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
3557 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
3558 nullfunc->seq_ctrl = 0;
3560 info = IEEE80211_SKB_CB(skb);
3562 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
3563 IEEE80211_TX_INTFL_NL80211_FRAME_TX;
3565 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
3566 skb->priority = 7;
3567 if (qos)
3568 nullfunc->qos_ctrl = cpu_to_le16(7);
3570 local_bh_disable();
3571 ieee80211_xmit(sdata, skb, band);
3572 local_bh_enable();
3573 rcu_read_unlock();
3575 *cookie = (unsigned long) skb;
3576 return 0;
3579 static int ieee80211_cfg_get_channel(struct wiphy *wiphy,
3580 struct wireless_dev *wdev,
3581 struct cfg80211_chan_def *chandef)
3583 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
3584 struct ieee80211_local *local = wiphy_priv(wiphy);
3585 struct ieee80211_chanctx_conf *chanctx_conf;
3586 int ret = -ENODATA;
3588 rcu_read_lock();
3589 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3590 if (chanctx_conf) {
3591 *chandef = chanctx_conf->def;
3592 ret = 0;
3593 } else if (local->open_count > 0 &&
3594 local->open_count == local->monitors &&
3595 sdata->vif.type == NL80211_IFTYPE_MONITOR) {
3596 if (local->use_chanctx)
3597 *chandef = local->monitor_chandef;
3598 else
3599 *chandef = local->_oper_chandef;
3600 ret = 0;
3602 rcu_read_unlock();
3604 return ret;
3607 #ifdef CONFIG_PM
3608 static void ieee80211_set_wakeup(struct wiphy *wiphy, bool enabled)
3610 drv_set_wakeup(wiphy_priv(wiphy), enabled);
3612 #endif
3614 struct cfg80211_ops mac80211_config_ops = {
3615 .add_virtual_intf = ieee80211_add_iface,
3616 .del_virtual_intf = ieee80211_del_iface,
3617 .change_virtual_intf = ieee80211_change_iface,
3618 .start_p2p_device = ieee80211_start_p2p_device,
3619 .stop_p2p_device = ieee80211_stop_p2p_device,
3620 .add_key = ieee80211_add_key,
3621 .del_key = ieee80211_del_key,
3622 .get_key = ieee80211_get_key,
3623 .set_default_key = ieee80211_config_default_key,
3624 .set_default_mgmt_key = ieee80211_config_default_mgmt_key,
3625 .start_ap = ieee80211_start_ap,
3626 .change_beacon = ieee80211_change_beacon,
3627 .stop_ap = ieee80211_stop_ap,
3628 .add_station = ieee80211_add_station,
3629 .del_station = ieee80211_del_station,
3630 .change_station = ieee80211_change_station,
3631 .get_station = ieee80211_get_station,
3632 .dump_station = ieee80211_dump_station,
3633 .dump_survey = ieee80211_dump_survey,
3634 #ifdef CONFIG_MAC80211_MESH
3635 .add_mpath = ieee80211_add_mpath,
3636 .del_mpath = ieee80211_del_mpath,
3637 .change_mpath = ieee80211_change_mpath,
3638 .get_mpath = ieee80211_get_mpath,
3639 .dump_mpath = ieee80211_dump_mpath,
3640 .update_mesh_config = ieee80211_update_mesh_config,
3641 .get_mesh_config = ieee80211_get_mesh_config,
3642 .join_mesh = ieee80211_join_mesh,
3643 .leave_mesh = ieee80211_leave_mesh,
3644 #endif
3645 .change_bss = ieee80211_change_bss,
3646 .set_txq_params = ieee80211_set_txq_params,
3647 .set_monitor_channel = ieee80211_set_monitor_channel,
3648 .suspend = ieee80211_suspend,
3649 .resume = ieee80211_resume,
3650 .scan = ieee80211_scan,
3651 .sched_scan_start = ieee80211_sched_scan_start,
3652 .sched_scan_stop = ieee80211_sched_scan_stop,
3653 .auth = ieee80211_auth,
3654 .assoc = ieee80211_assoc,
3655 .deauth = ieee80211_deauth,
3656 .disassoc = ieee80211_disassoc,
3657 .join_ibss = ieee80211_join_ibss,
3658 .leave_ibss = ieee80211_leave_ibss,
3659 .set_mcast_rate = ieee80211_set_mcast_rate,
3660 .set_wiphy_params = ieee80211_set_wiphy_params,
3661 .set_tx_power = ieee80211_set_tx_power,
3662 .get_tx_power = ieee80211_get_tx_power,
3663 .set_wds_peer = ieee80211_set_wds_peer,
3664 .rfkill_poll = ieee80211_rfkill_poll,
3665 CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
3666 CFG80211_TESTMODE_DUMP(ieee80211_testmode_dump)
3667 .set_power_mgmt = ieee80211_set_power_mgmt,
3668 .set_bitrate_mask = ieee80211_set_bitrate_mask,
3669 .remain_on_channel = ieee80211_remain_on_channel,
3670 .cancel_remain_on_channel = ieee80211_cancel_remain_on_channel,
3671 .mgmt_tx = ieee80211_mgmt_tx,
3672 .mgmt_tx_cancel_wait = ieee80211_mgmt_tx_cancel_wait,
3673 .set_cqm_rssi_config = ieee80211_set_cqm_rssi_config,
3674 .mgmt_frame_register = ieee80211_mgmt_frame_register,
3675 .set_antenna = ieee80211_set_antenna,
3676 .get_antenna = ieee80211_get_antenna,
3677 .set_ringparam = ieee80211_set_ringparam,
3678 .get_ringparam = ieee80211_get_ringparam,
3679 .set_rekey_data = ieee80211_set_rekey_data,
3680 .tdls_oper = ieee80211_tdls_oper,
3681 .tdls_mgmt = ieee80211_tdls_mgmt,
3682 .probe_client = ieee80211_probe_client,
3683 .set_noack_map = ieee80211_set_noack_map,
3684 #ifdef CONFIG_PM
3685 .set_wakeup = ieee80211_set_wakeup,
3686 #endif
3687 .get_et_sset_count = ieee80211_get_et_sset_count,
3688 .get_et_stats = ieee80211_get_et_stats,
3689 .get_et_strings = ieee80211_get_et_strings,
3690 .get_channel = ieee80211_cfg_get_channel,
3691 .start_radar_detection = ieee80211_start_radar_detection,
3692 .channel_switch = ieee80211_channel_switch,