1 // SPDX-License-Identifier: GPL-2.0-only
3 * mac80211 TDLS handling code
5 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2014, Intel Corporation
7 * Copyright 2014 Intel Mobile Communications GmbH
8 * Copyright 2015 - 2016 Intel Deutschland GmbH
9 * Copyright (C) 2019, 2021-2023 Intel Corporation
12 #include <linux/ieee80211.h>
13 #include <linux/log2.h>
14 #include <net/cfg80211.h>
15 #include <linux/rtnetlink.h>
16 #include "ieee80211_i.h"
17 #include "driver-ops.h"
21 /* give usermode some time for retries in setting up the TDLS session */
22 #define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
24 void ieee80211_tdls_peer_del_work(struct work_struct *wk)
26 struct ieee80211_sub_if_data *sdata;
27 struct ieee80211_local *local;
29 sdata = container_of(wk, struct ieee80211_sub_if_data,
30 u.mgd.tdls_peer_del_work.work);
33 mutex_lock(&local->mtx);
34 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer)) {
35 tdls_dbg(sdata, "TDLS del peer %pM\n", sdata->u.mgd.tdls_peer);
36 sta_info_destroy_addr(sdata, sdata->u.mgd.tdls_peer);
37 eth_zero_addr(sdata->u.mgd.tdls_peer);
39 mutex_unlock(&local->mtx);
42 static void ieee80211_tdls_add_ext_capab(struct ieee80211_link_data *link,
45 struct ieee80211_sub_if_data *sdata = link->sdata;
46 struct ieee80211_local *local = sdata->local;
47 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
48 bool chan_switch = local->hw.wiphy->features &
49 NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
50 bool wider_band = ieee80211_hw_check(&local->hw, TDLS_WIDER_BW) &&
51 !ifmgd->tdls_wider_bw_prohibited;
52 bool buffer_sta = ieee80211_hw_check(&local->hw,
53 SUPPORTS_TDLS_BUFFER_STA);
54 struct ieee80211_supported_band *sband = ieee80211_get_link_sband(link);
55 bool vht = sband && sband->vht_cap.vht_supported;
56 u8 *pos = skb_put(skb, 10);
58 *pos++ = WLAN_EID_EXT_CAPABILITY;
63 *pos++ = (chan_switch ? WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH : 0) |
64 (buffer_sta ? WLAN_EXT_CAPA4_TDLS_BUFFER_STA : 0);
65 *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
68 *pos++ = (vht && wider_band) ? WLAN_EXT_CAPA8_TDLS_WIDE_BW_ENABLED : 0;
72 ieee80211_tdls_add_subband(struct ieee80211_sub_if_data *sdata,
73 struct sk_buff *skb, u16 start, u16 end,
76 u8 subband_cnt = 0, ch_cnt = 0;
77 struct ieee80211_channel *ch;
78 struct cfg80211_chan_def chandef;
80 struct wiphy *wiphy = sdata->local->hw.wiphy;
82 for (i = start; i <= end; i += spacing) {
86 ch = ieee80211_get_channel(sdata->local->hw.wiphy, i);
88 /* we will be active on the channel */
89 cfg80211_chandef_create(&chandef, ch,
91 if (cfg80211_reg_can_beacon_relax(wiphy, &chandef,
92 sdata->wdev.iftype)) {
95 * check if the next channel is also part of
103 * we've reached the end of a range, with allowed channels
107 u8 *pos = skb_put(skb, 2);
108 *pos++ = ieee80211_frequency_to_channel(subband_start);
116 /* all channels in the requested range are allowed - add them here */
118 u8 *pos = skb_put(skb, 2);
119 *pos++ = ieee80211_frequency_to_channel(subband_start);
129 ieee80211_tdls_add_supp_channels(struct ieee80211_sub_if_data *sdata,
133 * Add possible channels for TDLS. These are channels that are allowed
137 u8 *pos = skb_put(skb, 2);
139 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
142 * 5GHz and 2GHz channels numbers can overlap. Ignore this for now, as
143 * this doesn't happen in real world scenarios.
146 /* 2GHz, with 5MHz spacing */
147 subband_cnt = ieee80211_tdls_add_subband(sdata, skb, 2412, 2472, 5);
149 /* 5GHz, with 20MHz spacing */
150 subband_cnt += ieee80211_tdls_add_subband(sdata, skb, 5000, 5825, 20);
153 *pos = 2 * subband_cnt;
156 static void ieee80211_tdls_add_oper_classes(struct ieee80211_link_data *link,
162 if (!ieee80211_chandef_to_operating_class(&link->conf->chandef,
166 pos = skb_put(skb, 4);
167 *pos++ = WLAN_EID_SUPPORTED_REGULATORY_CLASSES;
168 *pos++ = 2; /* len */
171 *pos++ = op_class; /* give current operating class as alternate too */
174 static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb)
176 u8 *pos = skb_put(skb, 3);
178 *pos++ = WLAN_EID_BSS_COEX_2040;
179 *pos++ = 1; /* len */
181 *pos++ = WLAN_BSS_COEX_INFORMATION_REQUEST;
184 static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_link_data *link,
187 struct ieee80211_supported_band *sband;
189 /* The capability will be 0 when sending a failure code */
190 if (status_code != 0)
193 sband = ieee80211_get_link_sband(link);
195 if (sband && sband->band == NL80211_BAND_2GHZ) {
196 return WLAN_CAPABILITY_SHORT_SLOT_TIME |
197 WLAN_CAPABILITY_SHORT_PREAMBLE;
203 static void ieee80211_tdls_add_link_ie(struct ieee80211_link_data *link,
204 struct sk_buff *skb, const u8 *peer,
207 struct ieee80211_sub_if_data *sdata = link->sdata;
208 struct ieee80211_tdls_lnkie *lnkid;
209 const u8 *init_addr, *rsp_addr;
212 init_addr = sdata->vif.addr;
216 rsp_addr = sdata->vif.addr;
219 lnkid = skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
221 lnkid->ie_type = WLAN_EID_LINK_ID;
222 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
224 memcpy(lnkid->bssid, link->u.mgd.bssid, ETH_ALEN);
225 memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
226 memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
230 ieee80211_tdls_add_aid(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
232 u8 *pos = skb_put(skb, 4);
234 *pos++ = WLAN_EID_AID;
235 *pos++ = 2; /* len */
236 put_unaligned_le16(sdata->vif.cfg.aid, pos);
239 /* translate numbering in the WMM parameter IE to the mac80211 notation */
240 static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
247 return IEEE80211_AC_BE;
249 return IEEE80211_AC_BK;
251 return IEEE80211_AC_VI;
253 return IEEE80211_AC_VO;
257 static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
264 ret |= (aci << 5) & 0x60;
268 static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
270 return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
271 ((ilog2(cw_max + 1) << 0x4) & 0xf0);
274 static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
277 struct ieee80211_wmm_param_ie *wmm;
278 struct ieee80211_tx_queue_params *txq;
281 wmm = skb_put_zero(skb, sizeof(*wmm));
283 wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
284 wmm->len = sizeof(*wmm) - 2;
286 wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
289 wmm->oui_type = 2; /* WME */
290 wmm->oui_subtype = 1; /* WME param */
291 wmm->version = 1; /* WME ver */
292 wmm->qos_info = 0; /* U-APSD not in use */
295 * Use the EDCA parameters defined for the BSS, or default if the AP
296 * doesn't support it, as mandated by 802.11-2012 section 10.22.4
298 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
299 txq = &sdata->deflink.tx_conf[ieee80211_ac_from_wmm(i)];
300 wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
302 wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
303 wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
308 ieee80211_tdls_chandef_vht_upgrade(struct ieee80211_sub_if_data *sdata,
309 struct sta_info *sta)
311 /* IEEE802.11ac-2013 Table E-4 */
312 u16 centers_80mhz[] = { 5210, 5290, 5530, 5610, 5690, 5775 };
313 struct cfg80211_chan_def uc = sta->tdls_chandef;
314 enum nl80211_chan_width max_width =
315 ieee80211_sta_cap_chan_bw(&sta->deflink);
318 /* only support upgrading non-narrow channels up to 80Mhz */
319 if (max_width == NL80211_CHAN_WIDTH_5 ||
320 max_width == NL80211_CHAN_WIDTH_10)
323 if (max_width > NL80211_CHAN_WIDTH_80)
324 max_width = NL80211_CHAN_WIDTH_80;
326 if (uc.width >= max_width)
329 * Channel usage constrains in the IEEE802.11ac-2013 specification only
330 * allow expanding a 20MHz channel to 80MHz in a single way. In
331 * addition, there are no 40MHz allowed channels that are not part of
332 * the allowed 80MHz range in the 5GHz spectrum (the relevant one here).
334 for (i = 0; i < ARRAY_SIZE(centers_80mhz); i++)
335 if (abs(uc.chan->center_freq - centers_80mhz[i]) <= 30) {
336 uc.center_freq1 = centers_80mhz[i];
338 uc.width = NL80211_CHAN_WIDTH_80;
342 if (!uc.center_freq1)
345 /* proceed to downgrade the chandef until usable or the same as AP BW */
346 while (uc.width > max_width ||
347 (uc.width > sta->tdls_chandef.width &&
348 !cfg80211_reg_can_beacon_relax(sdata->local->hw.wiphy, &uc,
349 sdata->wdev.iftype)))
350 ieee80211_chandef_downgrade(&uc);
352 if (!cfg80211_chandef_identical(&uc, &sta->tdls_chandef)) {
353 tdls_dbg(sdata, "TDLS ch width upgraded %d -> %d\n",
354 sta->tdls_chandef.width, uc.width);
357 * the station is not yet authorized when BW upgrade is done,
358 * locking is not required
360 sta->tdls_chandef = uc;
365 ieee80211_tdls_add_setup_start_ies(struct ieee80211_link_data *link,
366 struct sk_buff *skb, const u8 *peer,
367 u8 action_code, bool initiator,
368 const u8 *extra_ies, size_t extra_ies_len)
370 struct ieee80211_sub_if_data *sdata = link->sdata;
371 struct ieee80211_supported_band *sband;
372 struct ieee80211_local *local = sdata->local;
373 struct ieee80211_sta_ht_cap ht_cap;
374 struct ieee80211_sta_vht_cap vht_cap;
375 const struct ieee80211_sta_he_cap *he_cap;
376 const struct ieee80211_sta_eht_cap *eht_cap;
377 struct sta_info *sta = NULL;
378 size_t offset = 0, noffset;
381 sband = ieee80211_get_link_sband(link);
382 if (WARN_ON_ONCE(!sband))
385 ieee80211_add_srates_ie(sdata, skb, false, sband->band);
386 ieee80211_add_ext_srates_ie(sdata, skb, false, sband->band);
387 ieee80211_tdls_add_supp_channels(sdata, skb);
389 /* add any custom IEs that go before Extended Capabilities */
391 static const u8 before_ext_cap[] = {
394 WLAN_EID_EXT_SUPP_RATES,
395 WLAN_EID_SUPPORTED_CHANNELS,
398 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
400 ARRAY_SIZE(before_ext_cap),
402 skb_put_data(skb, extra_ies + offset, noffset - offset);
406 ieee80211_tdls_add_ext_capab(link, skb);
408 /* add the QoS element if we support it */
409 if (local->hw.queues >= IEEE80211_NUM_ACS &&
410 action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
411 ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
413 /* add any custom IEs that go before HT capabilities */
415 static const u8 before_ht_cap[] = {
418 WLAN_EID_EXT_SUPP_RATES,
419 WLAN_EID_SUPPORTED_CHANNELS,
421 WLAN_EID_EXT_CAPABILITY,
423 WLAN_EID_FAST_BSS_TRANSITION,
424 WLAN_EID_TIMEOUT_INTERVAL,
425 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
427 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
429 ARRAY_SIZE(before_ht_cap),
431 skb_put_data(skb, extra_ies + offset, noffset - offset);
435 /* we should have the peer STA if we're already responding */
436 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
437 sta = sta_info_get(sdata, peer);
438 if (WARN_ON_ONCE(!sta))
441 sta->tdls_chandef = link->conf->chandef;
444 ieee80211_tdls_add_oper_classes(link, skb);
447 * with TDLS we can switch channels, and HT-caps are not necessarily
448 * the same on all bands. The specification limits the setup to a
449 * single HT-cap, so use the current band for now.
451 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
453 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
454 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
455 ht_cap.ht_supported) {
456 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
458 /* disable SMPS in TDLS initiator */
459 ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
460 << IEEE80211_HT_CAP_SM_PS_SHIFT;
462 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
463 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
464 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
465 ht_cap.ht_supported && sta->sta.deflink.ht_cap.ht_supported) {
466 /* the peer caps are already intersected with our own */
467 memcpy(&ht_cap, &sta->sta.deflink.ht_cap, sizeof(ht_cap));
469 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
470 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
473 if (ht_cap.ht_supported &&
474 (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
475 ieee80211_tdls_add_bss_coex_ie(skb);
477 ieee80211_tdls_add_link_ie(link, skb, peer, initiator);
479 /* add any custom IEs that go before VHT capabilities */
481 static const u8 before_vht_cap[] = {
484 WLAN_EID_EXT_SUPP_RATES,
485 WLAN_EID_SUPPORTED_CHANNELS,
487 WLAN_EID_EXT_CAPABILITY,
489 WLAN_EID_FAST_BSS_TRANSITION,
490 WLAN_EID_TIMEOUT_INTERVAL,
491 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
494 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
496 ARRAY_SIZE(before_vht_cap),
498 skb_put_data(skb, extra_ies + offset, noffset - offset);
502 /* add AID if VHT, HE or EHT capabilities supported */
503 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
504 he_cap = ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif);
505 eht_cap = ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif);
506 if ((vht_cap.vht_supported || he_cap || eht_cap) &&
507 (action_code == WLAN_TDLS_SETUP_REQUEST ||
508 action_code == WLAN_TDLS_SETUP_RESPONSE))
509 ieee80211_tdls_add_aid(sdata, skb);
511 /* build the VHT-cap similarly to the HT-cap */
512 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
513 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
514 vht_cap.vht_supported) {
515 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
517 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
518 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
519 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
520 vht_cap.vht_supported && sta->sta.deflink.vht_cap.vht_supported) {
521 /* the peer caps are already intersected with our own */
522 memcpy(&vht_cap, &sta->sta.deflink.vht_cap, sizeof(vht_cap));
524 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
525 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
528 * if both peers support WIDER_BW, we can expand the chandef to
529 * a wider compatible one, up to 80MHz
531 if (test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
532 ieee80211_tdls_chandef_vht_upgrade(sdata, sta);
535 /* add any custom IEs that go before HE capabilities */
537 static const u8 before_he_cap[] = {
539 WLAN_EID_EXT_FILS_REQ_PARAMS,
542 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
544 ARRAY_SIZE(before_he_cap),
546 skb_put_data(skb, extra_ies + offset, noffset - offset);
550 /* build the HE-cap from sband */
552 (action_code == WLAN_TDLS_SETUP_REQUEST ||
553 action_code == WLAN_TDLS_SETUP_RESPONSE ||
554 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES)) {
559 2 + 1 + sizeof(he_cap->he_cap_elem) +
560 ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
561 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
562 he_cap->he_cap_elem.phy_cap_info);
563 pos = skb_put(skb, cap_size);
564 pos = ieee80211_ie_build_he_cap(0, pos, he_cap, pos + cap_size);
566 /* Build HE 6Ghz capa IE from sband */
567 if (sband->band == NL80211_BAND_6GHZ) {
568 cap_size = 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa);
569 pos = skb_put(skb, cap_size);
571 ieee80211_get_he_6ghz_capa_vif(sband, &sdata->vif);
572 pos = ieee80211_write_he_6ghz_cap(pos, he_6ghz_capa,
577 /* add any custom IEs that go before EHT capabilities */
579 static const u8 before_he_cap[] = {
581 WLAN_EID_EXT_FILS_REQ_PARAMS,
585 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
587 ARRAY_SIZE(before_he_cap),
589 skb_put_data(skb, extra_ies + offset, noffset - offset);
593 /* build the EHT-cap from sband */
594 if (he_cap && eht_cap &&
595 (action_code == WLAN_TDLS_SETUP_REQUEST ||
596 action_code == WLAN_TDLS_SETUP_RESPONSE ||
597 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES)) {
601 2 + 1 + sizeof(eht_cap->eht_cap_elem) +
602 ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
603 &eht_cap->eht_cap_elem, false) +
604 ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
605 eht_cap->eht_cap_elem.phy_cap_info);
606 pos = skb_put(skb, cap_size);
607 ieee80211_ie_build_eht_cap(pos, he_cap, eht_cap, pos + cap_size, false);
610 /* add any remaining IEs */
612 noffset = extra_ies_len;
613 skb_put_data(skb, extra_ies + offset, noffset - offset);
619 ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_link_data *link,
620 struct sk_buff *skb, const u8 *peer,
621 bool initiator, const u8 *extra_ies,
622 size_t extra_ies_len)
624 struct ieee80211_sub_if_data *sdata = link->sdata;
625 struct ieee80211_local *local = sdata->local;
626 size_t offset = 0, noffset;
627 struct sta_info *sta, *ap_sta;
628 struct ieee80211_supported_band *sband;
631 sband = ieee80211_get_link_sband(link);
632 if (WARN_ON_ONCE(!sband))
635 sta = sta_info_get(sdata, peer);
636 ap_sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
638 if (WARN_ON_ONCE(!sta || !ap_sta))
641 sta->tdls_chandef = link->conf->chandef;
643 /* add any custom IEs that go before the QoS IE */
645 static const u8 before_qos[] = {
648 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
650 ARRAY_SIZE(before_qos),
652 skb_put_data(skb, extra_ies + offset, noffset - offset);
656 /* add the QoS param IE if both the peer and we support it */
657 if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
658 ieee80211_tdls_add_wmm_param_ie(sdata, skb);
660 /* add any custom IEs that go before HT operation */
662 static const u8 before_ht_op[] = {
665 WLAN_EID_FAST_BSS_TRANSITION,
666 WLAN_EID_TIMEOUT_INTERVAL,
668 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
670 ARRAY_SIZE(before_ht_op),
672 skb_put_data(skb, extra_ies + offset, noffset - offset);
677 * if HT support is only added in TDLS, we need an HT-operation IE.
678 * add the IE as required by IEEE802.11-2012 9.23.3.2.
680 if (!ap_sta->sta.deflink.ht_cap.ht_supported && sta->sta.deflink.ht_cap.ht_supported) {
681 u16 prot = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED |
682 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
683 IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
685 pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
686 ieee80211_ie_build_ht_oper(pos, &sta->sta.deflink.ht_cap,
687 &link->conf->chandef, prot,
691 ieee80211_tdls_add_link_ie(link, skb, peer, initiator);
693 /* only include VHT-operation if not on the 2.4GHz band */
694 if (sband->band != NL80211_BAND_2GHZ &&
695 sta->sta.deflink.vht_cap.vht_supported) {
697 * if both peers support WIDER_BW, we can expand the chandef to
698 * a wider compatible one, up to 80MHz
700 if (test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
701 ieee80211_tdls_chandef_vht_upgrade(sdata, sta);
703 pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
704 ieee80211_ie_build_vht_oper(pos, &sta->sta.deflink.vht_cap,
708 /* add any remaining IEs */
710 noffset = extra_ies_len;
711 skb_put_data(skb, extra_ies + offset, noffset - offset);
716 ieee80211_tdls_add_chan_switch_req_ies(struct ieee80211_link_data *link,
717 struct sk_buff *skb, const u8 *peer,
718 bool initiator, const u8 *extra_ies,
719 size_t extra_ies_len, u8 oper_class,
720 struct cfg80211_chan_def *chandef)
722 struct ieee80211_tdls_data *tf;
723 size_t offset = 0, noffset;
725 if (WARN_ON_ONCE(!chandef))
728 tf = (void *)skb->data;
729 tf->u.chan_switch_req.target_channel =
730 ieee80211_frequency_to_channel(chandef->chan->center_freq);
731 tf->u.chan_switch_req.oper_class = oper_class;
734 static const u8 before_lnkie[] = {
735 WLAN_EID_SECONDARY_CHANNEL_OFFSET,
737 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
739 ARRAY_SIZE(before_lnkie),
741 skb_put_data(skb, extra_ies + offset, noffset - offset);
745 ieee80211_tdls_add_link_ie(link, skb, peer, initiator);
747 /* add any remaining IEs */
749 noffset = extra_ies_len;
750 skb_put_data(skb, extra_ies + offset, noffset - offset);
755 ieee80211_tdls_add_chan_switch_resp_ies(struct ieee80211_link_data *link,
756 struct sk_buff *skb, const u8 *peer,
757 u16 status_code, bool initiator,
759 size_t extra_ies_len)
761 if (status_code == 0)
762 ieee80211_tdls_add_link_ie(link, skb, peer, initiator);
765 skb_put_data(skb, extra_ies, extra_ies_len);
768 static void ieee80211_tdls_add_ies(struct ieee80211_link_data *link,
769 struct sk_buff *skb, const u8 *peer,
770 u8 action_code, u16 status_code,
771 bool initiator, const u8 *extra_ies,
772 size_t extra_ies_len, u8 oper_class,
773 struct cfg80211_chan_def *chandef)
775 switch (action_code) {
776 case WLAN_TDLS_SETUP_REQUEST:
777 case WLAN_TDLS_SETUP_RESPONSE:
778 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
779 if (status_code == 0)
780 ieee80211_tdls_add_setup_start_ies(link,
787 case WLAN_TDLS_SETUP_CONFIRM:
788 if (status_code == 0)
789 ieee80211_tdls_add_setup_cfm_ies(link, skb, peer,
790 initiator, extra_ies,
793 case WLAN_TDLS_TEARDOWN:
794 case WLAN_TDLS_DISCOVERY_REQUEST:
796 skb_put_data(skb, extra_ies, extra_ies_len);
797 if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
798 ieee80211_tdls_add_link_ie(link, skb,
801 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
802 ieee80211_tdls_add_chan_switch_req_ies(link, skb, peer,
803 initiator, extra_ies,
805 oper_class, chandef);
807 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
808 ieee80211_tdls_add_chan_switch_resp_ies(link, skb, peer,
810 initiator, extra_ies,
818 ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
819 struct ieee80211_link_data *link,
820 const u8 *peer, u8 action_code, u8 dialog_token,
821 u16 status_code, struct sk_buff *skb)
823 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
824 struct ieee80211_tdls_data *tf;
826 tf = skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
828 memcpy(tf->da, peer, ETH_ALEN);
829 memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
830 tf->ether_type = cpu_to_be16(ETH_P_TDLS);
831 tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
833 /* network header is after the ethernet header */
834 skb_set_network_header(skb, ETH_HLEN);
836 switch (action_code) {
837 case WLAN_TDLS_SETUP_REQUEST:
838 tf->category = WLAN_CATEGORY_TDLS;
839 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
841 skb_put(skb, sizeof(tf->u.setup_req));
842 tf->u.setup_req.dialog_token = dialog_token;
843 tf->u.setup_req.capability =
844 cpu_to_le16(ieee80211_get_tdls_sta_capab(link,
847 case WLAN_TDLS_SETUP_RESPONSE:
848 tf->category = WLAN_CATEGORY_TDLS;
849 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
851 skb_put(skb, sizeof(tf->u.setup_resp));
852 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
853 tf->u.setup_resp.dialog_token = dialog_token;
854 tf->u.setup_resp.capability =
855 cpu_to_le16(ieee80211_get_tdls_sta_capab(link,
858 case WLAN_TDLS_SETUP_CONFIRM:
859 tf->category = WLAN_CATEGORY_TDLS;
860 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
862 skb_put(skb, sizeof(tf->u.setup_cfm));
863 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
864 tf->u.setup_cfm.dialog_token = dialog_token;
866 case WLAN_TDLS_TEARDOWN:
867 tf->category = WLAN_CATEGORY_TDLS;
868 tf->action_code = WLAN_TDLS_TEARDOWN;
870 skb_put(skb, sizeof(tf->u.teardown));
871 tf->u.teardown.reason_code = cpu_to_le16(status_code);
873 case WLAN_TDLS_DISCOVERY_REQUEST:
874 tf->category = WLAN_CATEGORY_TDLS;
875 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
877 skb_put(skb, sizeof(tf->u.discover_req));
878 tf->u.discover_req.dialog_token = dialog_token;
880 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
881 tf->category = WLAN_CATEGORY_TDLS;
882 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
884 skb_put(skb, sizeof(tf->u.chan_switch_req));
886 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
887 tf->category = WLAN_CATEGORY_TDLS;
888 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
890 skb_put(skb, sizeof(tf->u.chan_switch_resp));
891 tf->u.chan_switch_resp.status_code = cpu_to_le16(status_code);
901 ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
902 const u8 *peer, struct ieee80211_link_data *link,
903 u8 action_code, u8 dialog_token,
904 u16 status_code, struct sk_buff *skb)
906 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
907 struct ieee80211_mgmt *mgmt;
909 mgmt = skb_put_zero(skb, 24);
910 memcpy(mgmt->da, peer, ETH_ALEN);
911 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
912 memcpy(mgmt->bssid, link->u.mgd.bssid, ETH_ALEN);
913 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
914 IEEE80211_STYPE_ACTION);
916 switch (action_code) {
917 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
918 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
919 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
920 mgmt->u.action.u.tdls_discover_resp.action_code =
921 WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
922 mgmt->u.action.u.tdls_discover_resp.dialog_token =
924 mgmt->u.action.u.tdls_discover_resp.capability =
925 cpu_to_le16(ieee80211_get_tdls_sta_capab(link,
935 static struct sk_buff *
936 ieee80211_tdls_build_mgmt_packet_data(struct ieee80211_sub_if_data *sdata,
937 const u8 *peer, int link_id,
938 u8 action_code, u8 dialog_token,
939 u16 status_code, bool initiator,
940 const u8 *extra_ies, size_t extra_ies_len,
942 struct cfg80211_chan_def *chandef)
944 struct ieee80211_local *local = sdata->local;
947 struct ieee80211_link_data *link;
949 link_id = link_id >= 0 ? link_id : 0;
951 link = rcu_dereference(sdata->link[link_id]);
955 skb = netdev_alloc_skb(sdata->dev,
956 local->hw.extra_tx_headroom +
957 max(sizeof(struct ieee80211_mgmt),
958 sizeof(struct ieee80211_tdls_data)) +
959 50 + /* supported rates */
961 26 + /* max(WMM-info, WMM-param) */
962 2 + max(sizeof(struct ieee80211_ht_cap),
963 sizeof(struct ieee80211_ht_operation)) +
964 2 + max(sizeof(struct ieee80211_vht_cap),
965 sizeof(struct ieee80211_vht_operation)) +
966 2 + 1 + sizeof(struct ieee80211_he_cap_elem) +
967 sizeof(struct ieee80211_he_mcs_nss_supp) +
968 IEEE80211_HE_PPE_THRES_MAX_LEN +
969 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
970 2 + 1 + sizeof(struct ieee80211_eht_cap_elem) +
971 sizeof(struct ieee80211_eht_mcs_nss_supp) +
972 IEEE80211_EHT_PPE_THRES_MAX_LEN +
973 50 + /* supported channels */
974 3 + /* 40/20 BSS coex */
976 4 + /* oper classes */
978 sizeof(struct ieee80211_tdls_lnkie));
982 skb_reserve(skb, local->hw.extra_tx_headroom);
984 switch (action_code) {
985 case WLAN_TDLS_SETUP_REQUEST:
986 case WLAN_TDLS_SETUP_RESPONSE:
987 case WLAN_TDLS_SETUP_CONFIRM:
988 case WLAN_TDLS_TEARDOWN:
989 case WLAN_TDLS_DISCOVERY_REQUEST:
990 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
991 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
992 ret = ieee80211_prep_tdls_encap_data(local->hw.wiphy,
993 sdata->dev, link, peer,
994 action_code, dialog_token,
997 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
998 ret = ieee80211_prep_tdls_direct(local->hw.wiphy, sdata->dev,
999 peer, link, action_code,
1000 dialog_token, status_code,
1011 ieee80211_tdls_add_ies(link, skb, peer, action_code, status_code,
1012 initiator, extra_ies, extra_ies_len, oper_class,
1025 ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
1026 const u8 *peer, int link_id,
1027 u8 action_code, u8 dialog_token,
1028 u16 status_code, u32 peer_capability,
1029 bool initiator, const u8 *extra_ies,
1030 size_t extra_ies_len, u8 oper_class,
1031 struct cfg80211_chan_def *chandef)
1033 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1034 struct sk_buff *skb = NULL;
1035 struct sta_info *sta;
1040 sta = sta_info_get(sdata, peer);
1042 /* infer the initiator if we can, to support old userspace */
1043 switch (action_code) {
1044 case WLAN_TDLS_SETUP_REQUEST:
1046 set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
1047 sta->sta.tdls_initiator = false;
1050 case WLAN_TDLS_SETUP_CONFIRM:
1051 case WLAN_TDLS_DISCOVERY_REQUEST:
1054 case WLAN_TDLS_SETUP_RESPONSE:
1056 * In some testing scenarios, we send a request and response.
1057 * Make the last packet sent take effect for the initiator
1061 clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
1062 sta->sta.tdls_initiator = true;
1065 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
1068 case WLAN_TDLS_TEARDOWN:
1069 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
1070 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
1071 /* any value is ok */
1078 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
1085 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, peer,
1086 link_id, action_code,
1087 dialog_token, status_code,
1088 initiator, extra_ies,
1089 extra_ies_len, oper_class,
1096 if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
1097 ieee80211_tx_skb_tid(sdata, skb, 7, link_id);
1102 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
1103 * we should default to AC_VI.
1105 switch (action_code) {
1106 case WLAN_TDLS_SETUP_REQUEST:
1107 case WLAN_TDLS_SETUP_RESPONSE:
1108 skb->priority = 256 + 2;
1111 skb->priority = 256 + 5;
1116 * Set the WLAN_TDLS_TEARDOWN flag to indicate a teardown in progress.
1117 * Later, if no ACK is returned from peer, we will re-send the teardown
1118 * packet through the AP.
1120 if ((action_code == WLAN_TDLS_TEARDOWN) &&
1121 ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
1122 bool try_resend; /* Should we keep skb for possible resend */
1124 /* If not sending directly to peer - no point in keeping skb */
1126 sta = sta_info_get(sdata, peer);
1127 try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1130 spin_lock_bh(&sdata->u.mgd.teardown_lock);
1131 if (try_resend && !sdata->u.mgd.teardown_skb) {
1132 /* Mark it as requiring TX status callback */
1133 flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
1134 IEEE80211_TX_INTFL_MLME_CONN_TX;
1137 * skb is copied since mac80211 will later set
1138 * properties that might not be the same as the AP,
1139 * such as encryption, QoS, addresses, etc.
1141 * No problem if skb_copy() fails, so no need to check.
1143 sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
1144 sdata->u.mgd.orig_teardown_skb = skb;
1146 spin_unlock_bh(&sdata->u.mgd.teardown_lock);
1149 /* disable bottom halves when entering the Tx path */
1151 __ieee80211_subif_start_xmit(skb, dev, flags,
1152 IEEE80211_TX_CTRL_MLO_LINK_UNSPEC, NULL);
1163 ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
1164 const u8 *peer, int link_id,
1165 u8 action_code, u8 dialog_token,
1166 u16 status_code, u32 peer_capability, bool initiator,
1167 const u8 *extra_ies, size_t extra_ies_len)
1169 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1170 struct ieee80211_local *local = sdata->local;
1171 enum ieee80211_smps_mode smps_mode =
1172 sdata->deflink.u.mgd.driver_smps_mode;
1175 /* don't support setup with forced SMPS mode that's not off */
1176 if (smps_mode != IEEE80211_SMPS_AUTOMATIC &&
1177 smps_mode != IEEE80211_SMPS_OFF) {
1178 tdls_dbg(sdata, "Aborting TDLS setup due to SMPS mode %d\n",
1183 mutex_lock(&local->mtx);
1185 /* we don't support concurrent TDLS peer setups */
1186 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
1187 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1193 * make sure we have a STA representing the peer so we drop or buffer
1194 * non-TDLS-setup frames to the peer. We can't send other packets
1195 * during setup through the AP path.
1196 * Allow error packets to be sent - sometimes we don't even add a STA
1197 * before failing the setup.
1199 if (status_code == 0) {
1201 if (!sta_info_get(sdata, peer)) {
1209 ieee80211_flush_queues(local, sdata, false);
1210 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
1211 mutex_unlock(&local->mtx);
1213 /* we cannot take the mutex while preparing the setup packet */
1214 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1215 link_id, action_code,
1216 dialog_token, status_code,
1217 peer_capability, initiator,
1218 extra_ies, extra_ies_len, 0,
1221 mutex_lock(&local->mtx);
1222 eth_zero_addr(sdata->u.mgd.tdls_peer);
1223 mutex_unlock(&local->mtx);
1227 ieee80211_queue_delayed_work(&sdata->local->hw,
1228 &sdata->u.mgd.tdls_peer_del_work,
1229 TDLS_PEER_SETUP_TIMEOUT);
1233 mutex_unlock(&local->mtx);
1238 ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
1239 const u8 *peer, int link_id,
1240 u8 action_code, u8 dialog_token,
1241 u16 status_code, u32 peer_capability,
1242 bool initiator, const u8 *extra_ies,
1243 size_t extra_ies_len)
1245 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1246 struct ieee80211_local *local = sdata->local;
1247 struct sta_info *sta;
1251 * No packets can be transmitted to the peer via the AP during setup -
1252 * the STA is set as a TDLS peer, but is not authorized.
1253 * During teardown, we prevent direct transmissions by stopping the
1254 * queues and flushing all direct packets.
1256 ieee80211_stop_vif_queues(local, sdata,
1257 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1258 ieee80211_flush_queues(local, sdata, false);
1260 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1261 link_id, action_code,
1262 dialog_token, status_code,
1263 peer_capability, initiator,
1264 extra_ies, extra_ies_len, 0,
1267 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
1271 * Remove the STA AUTH flag to force further traffic through the AP. If
1272 * the STA was unreachable, it was already removed.
1275 sta = sta_info_get(sdata, peer);
1277 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1280 ieee80211_wake_vif_queues(local, sdata,
1281 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1286 int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
1287 const u8 *peer, int link_id,
1288 u8 action_code, u8 dialog_token, u16 status_code,
1289 u32 peer_capability, bool initiator,
1290 const u8 *extra_ies, size_t extra_ies_len)
1292 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1295 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1298 /* make sure we are in managed mode, and associated */
1299 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1300 !sdata->u.mgd.associated)
1303 switch (action_code) {
1304 case WLAN_TDLS_SETUP_REQUEST:
1305 case WLAN_TDLS_SETUP_RESPONSE:
1306 ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer,
1307 link_id, action_code,
1308 dialog_token, status_code,
1309 peer_capability, initiator,
1310 extra_ies, extra_ies_len);
1312 case WLAN_TDLS_TEARDOWN:
1313 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer, link_id,
1314 action_code, dialog_token,
1316 peer_capability, initiator,
1317 extra_ies, extra_ies_len);
1319 case WLAN_TDLS_DISCOVERY_REQUEST:
1321 * Protect the discovery so we can hear the TDLS discovery
1322 * response frame. It is transmitted directly and not buffered
1325 drv_mgd_protect_tdls_discover(sdata->local, sdata);
1327 case WLAN_TDLS_SETUP_CONFIRM:
1328 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
1329 /* no special handling */
1330 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1331 link_id, action_code,
1335 initiator, extra_ies,
1336 extra_ies_len, 0, NULL);
1343 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM link_id %d status %d\n",
1344 action_code, peer, link_id, ret);
1348 static void iee80211_tdls_recalc_chanctx(struct ieee80211_sub_if_data *sdata,
1349 struct sta_info *sta)
1351 struct ieee80211_local *local = sdata->local;
1352 struct ieee80211_chanctx_conf *conf;
1353 struct ieee80211_chanctx *ctx;
1354 enum nl80211_chan_width width;
1355 struct ieee80211_supported_band *sband;
1357 mutex_lock(&local->chanctx_mtx);
1358 conf = rcu_dereference_protected(sdata->vif.bss_conf.chanctx_conf,
1359 lockdep_is_held(&local->chanctx_mtx));
1361 width = conf->def.width;
1362 sband = local->hw.wiphy->bands[conf->def.chan->band];
1363 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1364 ieee80211_recalc_chanctx_chantype(local, ctx);
1366 /* if width changed and a peer is given, update its BW */
1367 if (width != conf->def.width && sta &&
1368 test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW)) {
1369 enum ieee80211_sta_rx_bandwidth bw;
1371 bw = ieee80211_chan_width_to_rx_bw(conf->def.width);
1372 bw = min(bw, ieee80211_sta_cap_rx_bw(&sta->deflink));
1373 if (bw != sta->sta.deflink.bandwidth) {
1374 sta->sta.deflink.bandwidth = bw;
1375 rate_control_rate_update(local, sband, sta, 0,
1376 IEEE80211_RC_BW_CHANGED);
1378 * if a TDLS peer BW was updated, we need to
1379 * recalc the chandef width again, to get the
1380 * correct chanctx min_def
1382 ieee80211_recalc_chanctx_chantype(local, ctx);
1387 mutex_unlock(&local->chanctx_mtx);
1390 static int iee80211_tdls_have_ht_peers(struct ieee80211_sub_if_data *sdata)
1392 struct sta_info *sta;
1393 bool result = false;
1396 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
1397 if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
1398 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
1399 !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH) ||
1400 !sta->sta.deflink.ht_cap.ht_supported)
1411 iee80211_tdls_recalc_ht_protection(struct ieee80211_sub_if_data *sdata,
1412 struct sta_info *sta)
1415 u16 protection = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED |
1416 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
1417 IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
1420 /* Nothing to do if the BSS connection uses HT */
1421 if (!(sdata->deflink.u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT))
1424 tdls_ht = (sta && sta->sta.deflink.ht_cap.ht_supported) ||
1425 iee80211_tdls_have_ht_peers(sdata);
1427 opmode = sdata->vif.bss_conf.ht_operation_mode;
1430 opmode |= protection;
1432 opmode &= ~protection;
1434 if (opmode == sdata->vif.bss_conf.ht_operation_mode)
1437 sdata->vif.bss_conf.ht_operation_mode = opmode;
1438 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
1442 int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
1443 const u8 *peer, enum nl80211_tdls_operation oper)
1445 struct sta_info *sta;
1446 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1447 struct ieee80211_local *local = sdata->local;
1450 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1453 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1457 case NL80211_TDLS_ENABLE_LINK:
1458 case NL80211_TDLS_DISABLE_LINK:
1460 case NL80211_TDLS_TEARDOWN:
1461 case NL80211_TDLS_SETUP:
1462 case NL80211_TDLS_DISCOVERY_REQ:
1463 /* We don't support in-driver setup/teardown/discovery */
1467 /* protect possible bss_conf changes and avoid concurrency in
1468 * ieee80211_bss_info_change_notify()
1471 mutex_lock(&local->mtx);
1472 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
1475 case NL80211_TDLS_ENABLE_LINK:
1476 if (sdata->vif.bss_conf.csa_active) {
1477 tdls_dbg(sdata, "TDLS: disallow link during CSA\n");
1482 mutex_lock(&local->sta_mtx);
1483 sta = sta_info_get(sdata, peer);
1485 mutex_unlock(&local->sta_mtx);
1490 iee80211_tdls_recalc_chanctx(sdata, sta);
1491 iee80211_tdls_recalc_ht_protection(sdata, sta);
1493 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1494 mutex_unlock(&local->sta_mtx);
1496 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
1497 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
1500 case NL80211_TDLS_DISABLE_LINK:
1502 * The teardown message in ieee80211_tdls_mgmt_teardown() was
1503 * created while the queues were stopped, so it might still be
1504 * pending. Before flushing the queues we need to be sure the
1505 * message is handled by the tasklet handling pending messages,
1506 * otherwise we might start destroying the station before
1507 * sending the teardown packet.
1508 * Note that this only forces the tasklet to flush pendings -
1509 * not to stop the tasklet from rescheduling itself.
1511 tasklet_kill(&local->tx_pending_tasklet);
1512 /* flush a potentially queued teardown packet */
1513 ieee80211_flush_queues(local, sdata, false);
1515 ret = sta_info_destroy_addr(sdata, peer);
1517 mutex_lock(&local->sta_mtx);
1518 iee80211_tdls_recalc_ht_protection(sdata, NULL);
1519 mutex_unlock(&local->sta_mtx);
1521 iee80211_tdls_recalc_chanctx(sdata, NULL);
1528 if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1529 cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
1530 eth_zero_addr(sdata->u.mgd.tdls_peer);
1534 wiphy_work_queue(sdata->local->hw.wiphy,
1535 &sdata->deflink.u.mgd.request_smps_work);
1537 mutex_unlock(&local->mtx);
1538 sdata_unlock(sdata);
1542 void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
1543 enum nl80211_tdls_operation oper,
1544 u16 reason_code, gfp_t gfp)
1546 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1548 if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc) {
1549 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
1554 cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
1556 EXPORT_SYMBOL(ieee80211_tdls_oper_request);
1559 iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
1561 struct ieee80211_ch_switch_timing *ch_sw;
1563 *buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
1564 *buf++ = sizeof(struct ieee80211_ch_switch_timing);
1566 ch_sw = (void *)buf;
1567 ch_sw->switch_time = cpu_to_le16(switch_time);
1568 ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
1571 /* find switch timing IE in SKB ready for Tx */
1572 static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
1574 struct ieee80211_tdls_data *tf;
1578 * Get the offset for the new location of the switch timing IE.
1579 * The SKB network header will now point to the "payload_type"
1580 * element of the TDLS data frame struct.
1582 tf = container_of(skb->data + skb_network_offset(skb),
1583 struct ieee80211_tdls_data, payload_type);
1584 ie_start = tf->u.chan_switch_req.variable;
1585 return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
1586 skb->len - (ie_start - skb->data));
1589 static struct sk_buff *
1590 ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
1591 struct cfg80211_chan_def *chandef,
1592 u32 *ch_sw_tm_ie_offset)
1594 struct ieee80211_sub_if_data *sdata = sta->sdata;
1595 u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
1596 2 + sizeof(struct ieee80211_ch_switch_timing)];
1597 int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
1598 u8 *pos = extra_ies;
1599 struct sk_buff *skb;
1600 int link_id = sta->sta.valid_links ? ffs(sta->sta.valid_links) - 1 : 0;
1603 * if chandef points to a wide channel add a Secondary-Channel
1604 * Offset information element
1606 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1607 struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
1610 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
1611 *pos++ = sizeof(*sec_chan_ie);
1612 sec_chan_ie = (void *)pos;
1614 ht40plus = cfg80211_get_chandef_type(chandef) ==
1615 NL80211_CHAN_HT40PLUS;
1616 sec_chan_ie->sec_chan_offs = ht40plus ?
1617 IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
1618 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1619 pos += sizeof(*sec_chan_ie);
1621 extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1624 /* just set the values to 0, this is a template */
1625 iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
1627 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1629 WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
1630 0, 0, !sta->sta.tdls_initiator,
1631 extra_ies, extra_ies_len,
1632 oper_class, chandef);
1636 skb = ieee80211_build_data_template(sdata, skb, 0);
1638 tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
1642 if (ch_sw_tm_ie_offset) {
1643 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1646 tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
1647 dev_kfree_skb_any(skb);
1651 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1655 "TDLS channel switch request template for %pM ch %d width %d\n",
1656 sta->sta.addr, chandef->chan->center_freq, chandef->width);
1661 ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
1662 const u8 *addr, u8 oper_class,
1663 struct cfg80211_chan_def *chandef)
1665 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1666 struct ieee80211_local *local = sdata->local;
1667 struct sta_info *sta;
1668 struct sk_buff *skb = NULL;
1672 if (chandef->chan->freq_offset)
1673 /* this may work, but is untested */
1676 mutex_lock(&local->sta_mtx);
1677 sta = sta_info_get(sdata, addr);
1680 "Invalid TDLS peer %pM for channel switch request\n",
1686 if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
1687 tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
1693 skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
1700 ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
1701 chandef, skb, ch_sw_tm_ie);
1703 set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1706 mutex_unlock(&local->sta_mtx);
1707 dev_kfree_skb_any(skb);
1712 ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
1713 struct net_device *dev,
1716 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1717 struct ieee80211_local *local = sdata->local;
1718 struct sta_info *sta;
1720 mutex_lock(&local->sta_mtx);
1721 sta = sta_info_get(sdata, addr);
1724 "Invalid TDLS peer %pM for channel switch cancel\n",
1729 if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1730 tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
1735 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1736 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1739 mutex_unlock(&local->sta_mtx);
1742 static struct sk_buff *
1743 ieee80211_tdls_ch_sw_resp_tmpl_get(struct sta_info *sta,
1744 u32 *ch_sw_tm_ie_offset)
1746 struct ieee80211_sub_if_data *sdata = sta->sdata;
1747 struct sk_buff *skb;
1748 u8 extra_ies[2 + sizeof(struct ieee80211_ch_switch_timing)];
1749 int link_id = sta->sta.valid_links ? ffs(sta->sta.valid_links) - 1 : 0;
1751 /* initial timing are always zero in the template */
1752 iee80211_tdls_add_ch_switch_timing(extra_ies, 0, 0);
1754 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1756 WLAN_TDLS_CHANNEL_SWITCH_RESPONSE,
1757 0, 0, !sta->sta.tdls_initiator,
1758 extra_ies, sizeof(extra_ies), 0, NULL);
1762 skb = ieee80211_build_data_template(sdata, skb, 0);
1765 "Failed building TDLS channel switch resp frame\n");
1769 if (ch_sw_tm_ie_offset) {
1770 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1774 "No switch timing IE in TDLS switch resp\n");
1775 dev_kfree_skb_any(skb);
1779 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1782 tdls_dbg(sdata, "TDLS get channel switch response template for %pM\n",
1788 ieee80211_process_tdls_channel_switch_resp(struct ieee80211_sub_if_data *sdata,
1789 struct sk_buff *skb)
1791 struct ieee80211_local *local = sdata->local;
1792 struct ieee802_11_elems *elems = NULL;
1793 struct sta_info *sta;
1794 struct ieee80211_tdls_data *tf = (void *)skb->data;
1795 bool local_initiator;
1796 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1797 int baselen = offsetof(typeof(*tf), u.chan_switch_resp.variable);
1798 struct ieee80211_tdls_ch_sw_params params = {};
1801 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
1802 params.timestamp = rx_status->device_timestamp;
1804 if (skb->len < baselen) {
1805 tdls_dbg(sdata, "TDLS channel switch resp too short: %d\n",
1810 mutex_lock(&local->sta_mtx);
1811 sta = sta_info_get(sdata, tf->sa);
1812 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1813 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1819 params.sta = &sta->sta;
1820 params.status = le16_to_cpu(tf->u.chan_switch_resp.status_code);
1821 if (params.status != 0) {
1826 elems = ieee802_11_parse_elems(tf->u.chan_switch_resp.variable,
1827 skb->len - baselen, false, NULL);
1833 if (elems->parse_error) {
1834 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch resp\n");
1839 if (!elems->ch_sw_timing || !elems->lnk_id) {
1840 tdls_dbg(sdata, "TDLS channel switch resp - missing IEs\n");
1845 /* validate the initiator is set correctly */
1847 !memcmp(elems->lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1848 if (local_initiator == sta->sta.tdls_initiator) {
1849 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1854 params.switch_time = le16_to_cpu(elems->ch_sw_timing->switch_time);
1855 params.switch_timeout = le16_to_cpu(elems->ch_sw_timing->switch_timeout);
1858 ieee80211_tdls_ch_sw_resp_tmpl_get(sta, ¶ms.ch_sw_tm_ie);
1859 if (!params.tmpl_skb) {
1866 drv_tdls_recv_channel_switch(sdata->local, sdata, ¶ms);
1869 "TDLS channel switch response received from %pM status %d\n",
1870 tf->sa, params.status);
1873 mutex_unlock(&local->sta_mtx);
1874 dev_kfree_skb_any(params.tmpl_skb);
1880 ieee80211_process_tdls_channel_switch_req(struct ieee80211_sub_if_data *sdata,
1881 struct sk_buff *skb)
1883 struct ieee80211_local *local = sdata->local;
1884 struct ieee802_11_elems *elems;
1885 struct cfg80211_chan_def chandef;
1886 struct ieee80211_channel *chan;
1887 enum nl80211_channel_type chan_type;
1889 u8 target_channel, oper_class;
1890 bool local_initiator;
1891 struct sta_info *sta;
1892 enum nl80211_band band;
1893 struct ieee80211_tdls_data *tf = (void *)skb->data;
1894 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1895 int baselen = offsetof(typeof(*tf), u.chan_switch_req.variable);
1896 struct ieee80211_tdls_ch_sw_params params = {};
1899 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
1900 params.timestamp = rx_status->device_timestamp;
1902 if (skb->len < baselen) {
1903 tdls_dbg(sdata, "TDLS channel switch req too short: %d\n",
1908 target_channel = tf->u.chan_switch_req.target_channel;
1909 oper_class = tf->u.chan_switch_req.oper_class;
1912 * We can't easily infer the channel band. The operating class is
1913 * ambiguous - there are multiple tables (US/Europe/JP/Global). The
1914 * solution here is to treat channels with number >14 as 5GHz ones,
1915 * and specifically check for the (oper_class, channel) combinations
1916 * where this doesn't hold. These are thankfully unique according to
1918 * We consider only the 2GHz and 5GHz bands and 20MHz+ channels as
1921 if ((oper_class == 112 || oper_class == 2 || oper_class == 3 ||
1922 oper_class == 4 || oper_class == 5 || oper_class == 6) &&
1923 target_channel < 14)
1924 band = NL80211_BAND_5GHZ;
1926 band = target_channel < 14 ? NL80211_BAND_2GHZ :
1929 freq = ieee80211_channel_to_frequency(target_channel, band);
1931 tdls_dbg(sdata, "Invalid channel in TDLS chan switch: %d\n",
1936 chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
1939 "Unsupported channel for TDLS chan switch: %d\n",
1944 elems = ieee802_11_parse_elems(tf->u.chan_switch_req.variable,
1945 skb->len - baselen, false, NULL);
1949 if (elems->parse_error) {
1950 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch req\n");
1955 if (!elems->ch_sw_timing || !elems->lnk_id) {
1956 tdls_dbg(sdata, "TDLS channel switch req - missing IEs\n");
1961 if (!elems->sec_chan_offs) {
1962 chan_type = NL80211_CHAN_HT20;
1964 switch (elems->sec_chan_offs->sec_chan_offs) {
1965 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1966 chan_type = NL80211_CHAN_HT40PLUS;
1968 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1969 chan_type = NL80211_CHAN_HT40MINUS;
1972 chan_type = NL80211_CHAN_HT20;
1977 cfg80211_chandef_create(&chandef, chan, chan_type);
1979 /* we will be active on the TDLS link */
1980 if (!cfg80211_reg_can_beacon_relax(sdata->local->hw.wiphy, &chandef,
1981 sdata->wdev.iftype)) {
1982 tdls_dbg(sdata, "TDLS chan switch to forbidden channel\n");
1987 mutex_lock(&local->sta_mtx);
1988 sta = sta_info_get(sdata, tf->sa);
1989 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1990 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1996 params.sta = &sta->sta;
1998 /* validate the initiator is set correctly */
2000 !memcmp(elems->lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
2001 if (local_initiator == sta->sta.tdls_initiator) {
2002 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
2007 /* peer should have known better */
2008 if (!sta->sta.deflink.ht_cap.ht_supported && elems->sec_chan_offs &&
2009 elems->sec_chan_offs->sec_chan_offs) {
2010 tdls_dbg(sdata, "TDLS chan switch - wide chan unsupported\n");
2015 params.chandef = &chandef;
2016 params.switch_time = le16_to_cpu(elems->ch_sw_timing->switch_time);
2017 params.switch_timeout = le16_to_cpu(elems->ch_sw_timing->switch_timeout);
2020 ieee80211_tdls_ch_sw_resp_tmpl_get(sta,
2021 ¶ms.ch_sw_tm_ie);
2022 if (!params.tmpl_skb) {
2027 drv_tdls_recv_channel_switch(sdata->local, sdata, ¶ms);
2030 "TDLS ch switch request received from %pM ch %d width %d\n",
2031 tf->sa, params.chandef->chan->center_freq,
2032 params.chandef->width);
2034 mutex_unlock(&local->sta_mtx);
2035 dev_kfree_skb_any(params.tmpl_skb);
2042 ieee80211_process_tdls_channel_switch(struct ieee80211_sub_if_data *sdata,
2043 struct sk_buff *skb)
2045 struct ieee80211_tdls_data *tf = (void *)skb->data;
2046 struct wiphy *wiphy = sdata->local->hw.wiphy;
2048 lockdep_assert_wiphy(wiphy);
2050 /* make sure the driver supports it */
2051 if (!(wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
2054 /* we want to access the entire packet */
2055 if (skb_linearize(skb))
2058 * The packet/size was already validated by mac80211 Rx path, only look
2059 * at the action type.
2061 switch (tf->action_code) {
2062 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
2063 ieee80211_process_tdls_channel_switch_req(sdata, skb);
2065 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
2066 ieee80211_process_tdls_channel_switch_resp(sdata, skb);
2074 void ieee80211_teardown_tdls_peers(struct ieee80211_sub_if_data *sdata)
2076 struct sta_info *sta;
2077 u16 reason = WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED;
2080 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
2081 if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
2082 !test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2085 ieee80211_tdls_oper_request(&sdata->vif, sta->sta.addr,
2086 NL80211_TDLS_TEARDOWN, reason,
2092 void ieee80211_tdls_handle_disconnect(struct ieee80211_sub_if_data *sdata,
2093 const u8 *peer, u16 reason)
2095 struct ieee80211_sta *sta;
2098 sta = ieee80211_find_sta(&sdata->vif, peer);
2099 if (!sta || !sta->tdls) {
2105 tdls_dbg(sdata, "disconnected from TDLS peer %pM (Reason: %u=%s)\n",
2107 ieee80211_get_reason_code_string(reason));
2109 ieee80211_tdls_oper_request(&sdata->vif, peer,
2110 NL80211_TDLS_TEARDOWN,
2111 WLAN_REASON_TDLS_TEARDOWN_UNREACHABLE,