2 * mac80211 TDLS handling code
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2014, Intel Corporation
6 * Copyright 2014 Intel Mobile Communications GmbH
8 * This file is GPLv2 as found in COPYING.
11 #include <linux/ieee80211.h>
12 #include <linux/log2.h>
13 #include <net/cfg80211.h>
14 #include "ieee80211_i.h"
15 #include "driver-ops.h"
17 /* give usermode some time for retries in setting up the TDLS session */
18 #define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
20 void ieee80211_tdls_peer_del_work(struct work_struct *wk)
22 struct ieee80211_sub_if_data *sdata;
23 struct ieee80211_local *local;
25 sdata = container_of(wk, struct ieee80211_sub_if_data,
26 u.mgd.tdls_peer_del_work.work);
29 mutex_lock(&local->mtx);
30 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer)) {
31 tdls_dbg(sdata, "TDLS del peer %pM\n", sdata->u.mgd.tdls_peer);
32 sta_info_destroy_addr(sdata, sdata->u.mgd.tdls_peer);
33 eth_zero_addr(sdata->u.mgd.tdls_peer);
35 mutex_unlock(&local->mtx);
38 static void ieee80211_tdls_add_ext_capab(struct ieee80211_local *local,
41 u8 *pos = (void *)skb_put(skb, 7);
42 bool chan_switch = local->hw.wiphy->features &
43 NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
45 *pos++ = WLAN_EID_EXT_CAPABILITY;
50 *pos++ = chan_switch ? WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH : 0;
51 *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
55 ieee80211_tdls_add_subband(struct ieee80211_sub_if_data *sdata,
56 struct sk_buff *skb, u16 start, u16 end,
59 u8 subband_cnt = 0, ch_cnt = 0;
60 struct ieee80211_channel *ch;
61 struct cfg80211_chan_def chandef;
64 for (i = start; i <= end; i += spacing) {
68 ch = ieee80211_get_channel(sdata->local->hw.wiphy, i);
70 /* we will be active on the channel */
71 cfg80211_chandef_create(&chandef, ch,
73 if (cfg80211_reg_can_beacon(sdata->local->hw.wiphy,
75 sdata->wdev.iftype)) {
78 * check if the next channel is also part of
86 * we've reached the end of a range, with allowed channels
90 u8 *pos = skb_put(skb, 2);
91 *pos++ = ieee80211_frequency_to_channel(subband_start);
99 /* all channels in the requested range are allowed - add them here */
101 u8 *pos = skb_put(skb, 2);
102 *pos++ = ieee80211_frequency_to_channel(subband_start);
112 ieee80211_tdls_add_supp_channels(struct ieee80211_sub_if_data *sdata,
116 * Add possible channels for TDLS. These are channels that are allowed
120 u8 *pos = skb_put(skb, 2);
122 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
125 * 5GHz and 2GHz channels numbers can overlap. Ignore this for now, as
126 * this doesn't happen in real world scenarios.
129 /* 2GHz, with 5MHz spacing */
130 subband_cnt = ieee80211_tdls_add_subband(sdata, skb, 2412, 2472, 5);
132 /* 5GHz, with 20MHz spacing */
133 subband_cnt += ieee80211_tdls_add_subband(sdata, skb, 5000, 5825, 20);
136 *pos = 2 * subband_cnt;
139 static void ieee80211_tdls_add_oper_classes(struct ieee80211_sub_if_data *sdata,
145 if (!ieee80211_chandef_to_operating_class(&sdata->vif.bss_conf.chandef,
149 pos = skb_put(skb, 4);
150 *pos++ = WLAN_EID_SUPPORTED_REGULATORY_CLASSES;
151 *pos++ = 2; /* len */
154 *pos++ = op_class; /* give current operating class as alternate too */
157 static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb)
159 u8 *pos = (void *)skb_put(skb, 3);
161 *pos++ = WLAN_EID_BSS_COEX_2040;
162 *pos++ = 1; /* len */
164 *pos++ = WLAN_BSS_COEX_INFORMATION_REQUEST;
167 static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
170 /* The capability will be 0 when sending a failure code */
171 if (status_code != 0)
174 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_2GHZ) {
175 return WLAN_CAPABILITY_SHORT_SLOT_TIME |
176 WLAN_CAPABILITY_SHORT_PREAMBLE;
182 static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
183 struct sk_buff *skb, const u8 *peer,
186 struct ieee80211_tdls_lnkie *lnkid;
187 const u8 *init_addr, *rsp_addr;
190 init_addr = sdata->vif.addr;
194 rsp_addr = sdata->vif.addr;
197 lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
199 lnkid->ie_type = WLAN_EID_LINK_ID;
200 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
202 memcpy(lnkid->bssid, sdata->u.mgd.bssid, ETH_ALEN);
203 memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
204 memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
208 ieee80211_tdls_add_aid(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
210 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
211 u8 *pos = (void *)skb_put(skb, 4);
213 *pos++ = WLAN_EID_AID;
214 *pos++ = 2; /* len */
215 put_unaligned_le16(ifmgd->aid, pos);
218 /* translate numbering in the WMM parameter IE to the mac80211 notation */
219 static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
225 return IEEE80211_AC_BE;
227 return IEEE80211_AC_BK;
229 return IEEE80211_AC_VI;
231 return IEEE80211_AC_VO;
235 static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
242 ret |= (aci << 5) & 0x60;
246 static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
248 return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
249 ((ilog2(cw_max + 1) << 0x4) & 0xf0);
252 static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
255 struct ieee80211_wmm_param_ie *wmm;
256 struct ieee80211_tx_queue_params *txq;
259 wmm = (void *)skb_put(skb, sizeof(*wmm));
260 memset(wmm, 0, sizeof(*wmm));
262 wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
263 wmm->len = sizeof(*wmm) - 2;
265 wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
268 wmm->oui_type = 2; /* WME */
269 wmm->oui_subtype = 1; /* WME param */
270 wmm->version = 1; /* WME ver */
271 wmm->qos_info = 0; /* U-APSD not in use */
274 * Use the EDCA parameters defined for the BSS, or default if the AP
275 * doesn't support it, as mandated by 802.11-2012 section 10.22.4
277 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
278 txq = &sdata->tx_conf[ieee80211_ac_from_wmm(i)];
279 wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
281 wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
282 wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
287 ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
288 struct sk_buff *skb, const u8 *peer,
289 u8 action_code, bool initiator,
290 const u8 *extra_ies, size_t extra_ies_len)
292 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
293 struct ieee80211_local *local = sdata->local;
294 struct ieee80211_supported_band *sband;
295 struct ieee80211_sta_ht_cap ht_cap;
296 struct ieee80211_sta_vht_cap vht_cap;
297 struct sta_info *sta = NULL;
298 size_t offset = 0, noffset;
301 ieee80211_add_srates_ie(sdata, skb, false, band);
302 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
303 ieee80211_tdls_add_supp_channels(sdata, skb);
305 /* add any custom IEs that go before Extended Capabilities */
307 static const u8 before_ext_cap[] = {
310 WLAN_EID_EXT_SUPP_RATES,
311 WLAN_EID_SUPPORTED_CHANNELS,
314 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
316 ARRAY_SIZE(before_ext_cap),
318 pos = skb_put(skb, noffset - offset);
319 memcpy(pos, extra_ies + offset, noffset - offset);
323 ieee80211_tdls_add_ext_capab(local, skb);
325 /* add the QoS element if we support it */
326 if (local->hw.queues >= IEEE80211_NUM_ACS &&
327 action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
328 ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
330 /* add any custom IEs that go before HT capabilities */
332 static const u8 before_ht_cap[] = {
335 WLAN_EID_EXT_SUPP_RATES,
336 WLAN_EID_SUPPORTED_CHANNELS,
338 WLAN_EID_EXT_CAPABILITY,
340 WLAN_EID_FAST_BSS_TRANSITION,
341 WLAN_EID_TIMEOUT_INTERVAL,
342 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
344 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
346 ARRAY_SIZE(before_ht_cap),
348 pos = skb_put(skb, noffset - offset);
349 memcpy(pos, extra_ies + offset, noffset - offset);
355 /* we should have the peer STA if we're already responding */
356 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
357 sta = sta_info_get(sdata, peer);
358 if (WARN_ON_ONCE(!sta)) {
364 ieee80211_tdls_add_oper_classes(sdata, skb);
367 * with TDLS we can switch channels, and HT-caps are not necessarily
368 * the same on all bands. The specification limits the setup to a
369 * single HT-cap, so use the current band for now.
371 sband = local->hw.wiphy->bands[band];
372 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
374 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
375 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
376 ht_cap.ht_supported) {
377 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
379 /* disable SMPS in TDLS initiator */
380 ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
381 << IEEE80211_HT_CAP_SM_PS_SHIFT;
383 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
384 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
385 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
386 ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
387 /* disable SMPS in TDLS responder */
388 sta->sta.ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
389 << IEEE80211_HT_CAP_SM_PS_SHIFT;
391 /* the peer caps are already intersected with our own */
392 memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
394 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
395 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
398 if (ht_cap.ht_supported &&
399 (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
400 ieee80211_tdls_add_bss_coex_ie(skb);
402 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
404 /* add any custom IEs that go before VHT capabilities */
406 static const u8 before_vht_cap[] = {
409 WLAN_EID_EXT_SUPP_RATES,
410 WLAN_EID_SUPPORTED_CHANNELS,
412 WLAN_EID_EXT_CAPABILITY,
414 WLAN_EID_FAST_BSS_TRANSITION,
415 WLAN_EID_TIMEOUT_INTERVAL,
416 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
419 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
421 ARRAY_SIZE(before_vht_cap),
423 pos = skb_put(skb, noffset - offset);
424 memcpy(pos, extra_ies + offset, noffset - offset);
428 /* build the VHT-cap similarly to the HT-cap */
429 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
430 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
431 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
432 vht_cap.vht_supported) {
433 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
435 /* the AID is present only when VHT is implemented */
436 if (action_code == WLAN_TDLS_SETUP_REQUEST)
437 ieee80211_tdls_add_aid(sdata, skb);
439 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
440 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
441 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
442 vht_cap.vht_supported && sta->sta.vht_cap.vht_supported) {
443 /* the peer caps are already intersected with our own */
444 memcpy(&vht_cap, &sta->sta.vht_cap, sizeof(vht_cap));
446 /* the AID is present only when VHT is implemented */
447 ieee80211_tdls_add_aid(sdata, skb);
449 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
450 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
455 /* add any remaining IEs */
457 noffset = extra_ies_len;
458 pos = skb_put(skb, noffset - offset);
459 memcpy(pos, extra_ies + offset, noffset - offset);
465 ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
466 struct sk_buff *skb, const u8 *peer,
467 bool initiator, const u8 *extra_ies,
468 size_t extra_ies_len)
470 struct ieee80211_local *local = sdata->local;
471 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
472 size_t offset = 0, noffset;
473 struct sta_info *sta, *ap_sta;
474 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
479 sta = sta_info_get(sdata, peer);
480 ap_sta = sta_info_get(sdata, ifmgd->bssid);
481 if (WARN_ON_ONCE(!sta || !ap_sta)) {
486 /* add any custom IEs that go before the QoS IE */
488 static const u8 before_qos[] = {
491 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
493 ARRAY_SIZE(before_qos),
495 pos = skb_put(skb, noffset - offset);
496 memcpy(pos, extra_ies + offset, noffset - offset);
500 /* add the QoS param IE if both the peer and we support it */
501 if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
502 ieee80211_tdls_add_wmm_param_ie(sdata, skb);
504 /* add any custom IEs that go before HT operation */
506 static const u8 before_ht_op[] = {
509 WLAN_EID_FAST_BSS_TRANSITION,
510 WLAN_EID_TIMEOUT_INTERVAL,
512 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
514 ARRAY_SIZE(before_ht_op),
516 pos = skb_put(skb, noffset - offset);
517 memcpy(pos, extra_ies + offset, noffset - offset);
521 /* if HT support is only added in TDLS, we need an HT-operation IE */
522 if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
523 pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
524 /* send an empty HT operation IE */
525 ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
526 &sdata->vif.bss_conf.chandef, 0);
529 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
531 /* only include VHT-operation if not on the 2.4GHz band */
532 if (band != IEEE80211_BAND_2GHZ && sta->sta.vht_cap.vht_supported) {
533 pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
534 ieee80211_ie_build_vht_oper(pos, &sta->sta.vht_cap,
535 &sdata->vif.bss_conf.chandef);
540 /* add any remaining IEs */
542 noffset = extra_ies_len;
543 pos = skb_put(skb, noffset - offset);
544 memcpy(pos, extra_ies + offset, noffset - offset);
549 ieee80211_tdls_add_chan_switch_req_ies(struct ieee80211_sub_if_data *sdata,
550 struct sk_buff *skb, const u8 *peer,
551 bool initiator, const u8 *extra_ies,
552 size_t extra_ies_len, u8 oper_class,
553 struct cfg80211_chan_def *chandef)
555 struct ieee80211_tdls_data *tf;
556 size_t offset = 0, noffset;
559 if (WARN_ON_ONCE(!chandef))
562 tf = (void *)skb->data;
563 tf->u.chan_switch_req.target_channel =
564 ieee80211_frequency_to_channel(chandef->chan->center_freq);
565 tf->u.chan_switch_req.oper_class = oper_class;
568 static const u8 before_lnkie[] = {
569 WLAN_EID_SECONDARY_CHANNEL_OFFSET,
571 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
573 ARRAY_SIZE(before_lnkie),
575 pos = skb_put(skb, noffset - offset);
576 memcpy(pos, extra_ies + offset, noffset - offset);
580 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
582 /* add any remaining IEs */
584 noffset = extra_ies_len;
585 pos = skb_put(skb, noffset - offset);
586 memcpy(pos, extra_ies + offset, noffset - offset);
591 ieee80211_tdls_add_chan_switch_resp_ies(struct ieee80211_sub_if_data *sdata,
592 struct sk_buff *skb, const u8 *peer,
593 u16 status_code, bool initiator,
595 size_t extra_ies_len)
597 if (status_code == 0)
598 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
601 memcpy(skb_put(skb, extra_ies_len), extra_ies, extra_ies_len);
604 static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
605 struct sk_buff *skb, const u8 *peer,
606 u8 action_code, u16 status_code,
607 bool initiator, const u8 *extra_ies,
608 size_t extra_ies_len, u8 oper_class,
609 struct cfg80211_chan_def *chandef)
611 switch (action_code) {
612 case WLAN_TDLS_SETUP_REQUEST:
613 case WLAN_TDLS_SETUP_RESPONSE:
614 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
615 if (status_code == 0)
616 ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
622 case WLAN_TDLS_SETUP_CONFIRM:
623 if (status_code == 0)
624 ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
625 initiator, extra_ies,
628 case WLAN_TDLS_TEARDOWN:
629 case WLAN_TDLS_DISCOVERY_REQUEST:
631 memcpy(skb_put(skb, extra_ies_len), extra_ies,
633 if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
634 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
636 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
637 ieee80211_tdls_add_chan_switch_req_ies(sdata, skb, peer,
638 initiator, extra_ies,
640 oper_class, chandef);
642 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
643 ieee80211_tdls_add_chan_switch_resp_ies(sdata, skb, peer,
645 initiator, extra_ies,
653 ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
654 const u8 *peer, u8 action_code, u8 dialog_token,
655 u16 status_code, struct sk_buff *skb)
657 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
658 struct ieee80211_tdls_data *tf;
660 tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
662 memcpy(tf->da, peer, ETH_ALEN);
663 memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
664 tf->ether_type = cpu_to_be16(ETH_P_TDLS);
665 tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
667 /* network header is after the ethernet header */
668 skb_set_network_header(skb, ETH_HLEN);
670 switch (action_code) {
671 case WLAN_TDLS_SETUP_REQUEST:
672 tf->category = WLAN_CATEGORY_TDLS;
673 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
675 skb_put(skb, sizeof(tf->u.setup_req));
676 tf->u.setup_req.dialog_token = dialog_token;
677 tf->u.setup_req.capability =
678 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
681 case WLAN_TDLS_SETUP_RESPONSE:
682 tf->category = WLAN_CATEGORY_TDLS;
683 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
685 skb_put(skb, sizeof(tf->u.setup_resp));
686 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
687 tf->u.setup_resp.dialog_token = dialog_token;
688 tf->u.setup_resp.capability =
689 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
692 case WLAN_TDLS_SETUP_CONFIRM:
693 tf->category = WLAN_CATEGORY_TDLS;
694 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
696 skb_put(skb, sizeof(tf->u.setup_cfm));
697 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
698 tf->u.setup_cfm.dialog_token = dialog_token;
700 case WLAN_TDLS_TEARDOWN:
701 tf->category = WLAN_CATEGORY_TDLS;
702 tf->action_code = WLAN_TDLS_TEARDOWN;
704 skb_put(skb, sizeof(tf->u.teardown));
705 tf->u.teardown.reason_code = cpu_to_le16(status_code);
707 case WLAN_TDLS_DISCOVERY_REQUEST:
708 tf->category = WLAN_CATEGORY_TDLS;
709 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
711 skb_put(skb, sizeof(tf->u.discover_req));
712 tf->u.discover_req.dialog_token = dialog_token;
714 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
715 tf->category = WLAN_CATEGORY_TDLS;
716 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
718 skb_put(skb, sizeof(tf->u.chan_switch_req));
720 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
721 tf->category = WLAN_CATEGORY_TDLS;
722 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
724 skb_put(skb, sizeof(tf->u.chan_switch_resp));
725 tf->u.chan_switch_resp.status_code = cpu_to_le16(status_code);
735 ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
736 const u8 *peer, u8 action_code, u8 dialog_token,
737 u16 status_code, struct sk_buff *skb)
739 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
740 struct ieee80211_mgmt *mgmt;
742 mgmt = (void *)skb_put(skb, 24);
744 memcpy(mgmt->da, peer, ETH_ALEN);
745 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
746 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
748 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
749 IEEE80211_STYPE_ACTION);
751 switch (action_code) {
752 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
753 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
754 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
755 mgmt->u.action.u.tdls_discover_resp.action_code =
756 WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
757 mgmt->u.action.u.tdls_discover_resp.dialog_token =
759 mgmt->u.action.u.tdls_discover_resp.capability =
760 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
770 static struct sk_buff *
771 ieee80211_tdls_build_mgmt_packet_data(struct ieee80211_sub_if_data *sdata,
772 const u8 *peer, u8 action_code,
773 u8 dialog_token, u16 status_code,
774 bool initiator, const u8 *extra_ies,
775 size_t extra_ies_len, u8 oper_class,
776 struct cfg80211_chan_def *chandef)
778 struct ieee80211_local *local = sdata->local;
782 skb = netdev_alloc_skb(sdata->dev,
783 local->hw.extra_tx_headroom +
784 max(sizeof(struct ieee80211_mgmt),
785 sizeof(struct ieee80211_tdls_data)) +
786 50 + /* supported rates */
788 26 + /* max(WMM-info, WMM-param) */
789 2 + max(sizeof(struct ieee80211_ht_cap),
790 sizeof(struct ieee80211_ht_operation)) +
791 2 + max(sizeof(struct ieee80211_vht_cap),
792 sizeof(struct ieee80211_vht_operation)) +
793 50 + /* supported channels */
794 3 + /* 40/20 BSS coex */
796 4 + /* oper classes */
798 sizeof(struct ieee80211_tdls_lnkie));
802 skb_reserve(skb, local->hw.extra_tx_headroom);
804 switch (action_code) {
805 case WLAN_TDLS_SETUP_REQUEST:
806 case WLAN_TDLS_SETUP_RESPONSE:
807 case WLAN_TDLS_SETUP_CONFIRM:
808 case WLAN_TDLS_TEARDOWN:
809 case WLAN_TDLS_DISCOVERY_REQUEST:
810 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
811 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
812 ret = ieee80211_prep_tdls_encap_data(local->hw.wiphy,
814 action_code, dialog_token,
817 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
818 ret = ieee80211_prep_tdls_direct(local->hw.wiphy, sdata->dev,
820 dialog_token, status_code,
831 ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
832 initiator, extra_ies, extra_ies_len, oper_class,
842 ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
843 const u8 *peer, u8 action_code, u8 dialog_token,
844 u16 status_code, u32 peer_capability,
845 bool initiator, const u8 *extra_ies,
846 size_t extra_ies_len, u8 oper_class,
847 struct cfg80211_chan_def *chandef)
849 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
850 struct sk_buff *skb = NULL;
851 struct sta_info *sta;
856 sta = sta_info_get(sdata, peer);
858 /* infer the initiator if we can, to support old userspace */
859 switch (action_code) {
860 case WLAN_TDLS_SETUP_REQUEST:
862 set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
863 sta->sta.tdls_initiator = false;
866 case WLAN_TDLS_SETUP_CONFIRM:
867 case WLAN_TDLS_DISCOVERY_REQUEST:
870 case WLAN_TDLS_SETUP_RESPONSE:
872 * In some testing scenarios, we send a request and response.
873 * Make the last packet sent take effect for the initiator
877 clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
878 sta->sta.tdls_initiator = true;
881 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
884 case WLAN_TDLS_TEARDOWN:
885 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
886 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
887 /* any value is ok */
894 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
901 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, peer, action_code,
902 dialog_token, status_code,
903 initiator, extra_ies,
904 extra_ies_len, oper_class,
911 if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
912 ieee80211_tx_skb(sdata, skb);
917 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
918 * we should default to AC_VI.
920 switch (action_code) {
921 case WLAN_TDLS_SETUP_REQUEST:
922 case WLAN_TDLS_SETUP_RESPONSE:
923 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
927 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
933 * Set the WLAN_TDLS_TEARDOWN flag to indicate a teardown in progress.
934 * Later, if no ACK is returned from peer, we will re-send the teardown
935 * packet through the AP.
937 if ((action_code == WLAN_TDLS_TEARDOWN) &&
938 ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
939 bool try_resend; /* Should we keep skb for possible resend */
941 /* If not sending directly to peer - no point in keeping skb */
943 sta = sta_info_get(sdata, peer);
944 try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
947 spin_lock_bh(&sdata->u.mgd.teardown_lock);
948 if (try_resend && !sdata->u.mgd.teardown_skb) {
949 /* Mark it as requiring TX status callback */
950 flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
951 IEEE80211_TX_INTFL_MLME_CONN_TX;
954 * skb is copied since mac80211 will later set
955 * properties that might not be the same as the AP,
956 * such as encryption, QoS, addresses, etc.
958 * No problem if skb_copy() fails, so no need to check.
960 sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
961 sdata->u.mgd.orig_teardown_skb = skb;
963 spin_unlock_bh(&sdata->u.mgd.teardown_lock);
966 /* disable bottom halves when entering the Tx path */
968 __ieee80211_subif_start_xmit(skb, dev, flags);
979 ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
980 const u8 *peer, u8 action_code, u8 dialog_token,
981 u16 status_code, u32 peer_capability, bool initiator,
982 const u8 *extra_ies, size_t extra_ies_len)
984 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
985 struct ieee80211_local *local = sdata->local;
988 mutex_lock(&local->mtx);
990 /* we don't support concurrent TDLS peer setups */
991 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
992 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
998 * make sure we have a STA representing the peer so we drop or buffer
999 * non-TDLS-setup frames to the peer. We can't send other packets
1000 * during setup through the AP path.
1001 * Allow error packets to be sent - sometimes we don't even add a STA
1002 * before failing the setup.
1004 if (status_code == 0) {
1006 if (!sta_info_get(sdata, peer)) {
1014 ieee80211_flush_queues(local, sdata, false);
1015 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
1016 mutex_unlock(&local->mtx);
1018 /* we cannot take the mutex while preparing the setup packet */
1019 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1020 dialog_token, status_code,
1021 peer_capability, initiator,
1022 extra_ies, extra_ies_len, 0,
1025 mutex_lock(&local->mtx);
1026 eth_zero_addr(sdata->u.mgd.tdls_peer);
1027 mutex_unlock(&local->mtx);
1031 ieee80211_queue_delayed_work(&sdata->local->hw,
1032 &sdata->u.mgd.tdls_peer_del_work,
1033 TDLS_PEER_SETUP_TIMEOUT);
1037 mutex_unlock(&local->mtx);
1042 ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
1043 const u8 *peer, u8 action_code, u8 dialog_token,
1044 u16 status_code, u32 peer_capability,
1045 bool initiator, const u8 *extra_ies,
1046 size_t extra_ies_len)
1048 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1049 struct ieee80211_local *local = sdata->local;
1050 struct sta_info *sta;
1054 * No packets can be transmitted to the peer via the AP during setup -
1055 * the STA is set as a TDLS peer, but is not authorized.
1056 * During teardown, we prevent direct transmissions by stopping the
1057 * queues and flushing all direct packets.
1059 ieee80211_stop_vif_queues(local, sdata,
1060 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1061 ieee80211_flush_queues(local, sdata, false);
1063 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1064 dialog_token, status_code,
1065 peer_capability, initiator,
1066 extra_ies, extra_ies_len, 0,
1069 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
1073 * Remove the STA AUTH flag to force further traffic through the AP. If
1074 * the STA was unreachable, it was already removed.
1077 sta = sta_info_get(sdata, peer);
1079 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1082 ieee80211_wake_vif_queues(local, sdata,
1083 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1088 int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
1089 const u8 *peer, u8 action_code, u8 dialog_token,
1090 u16 status_code, u32 peer_capability,
1091 bool initiator, const u8 *extra_ies,
1092 size_t extra_ies_len)
1094 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1097 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1100 /* make sure we are in managed mode, and associated */
1101 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1102 !sdata->u.mgd.associated)
1105 switch (action_code) {
1106 case WLAN_TDLS_SETUP_REQUEST:
1107 case WLAN_TDLS_SETUP_RESPONSE:
1108 ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
1109 dialog_token, status_code,
1110 peer_capability, initiator,
1111 extra_ies, extra_ies_len);
1113 case WLAN_TDLS_TEARDOWN:
1114 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
1115 action_code, dialog_token,
1117 peer_capability, initiator,
1118 extra_ies, extra_ies_len);
1120 case WLAN_TDLS_DISCOVERY_REQUEST:
1122 * Protect the discovery so we can hear the TDLS discovery
1123 * response frame. It is transmitted directly and not buffered
1126 drv_mgd_protect_tdls_discover(sdata->local, sdata);
1128 case WLAN_TDLS_SETUP_CONFIRM:
1129 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
1130 /* no special handling */
1131 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1136 initiator, extra_ies,
1137 extra_ies_len, 0, NULL);
1144 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
1145 action_code, peer, ret);
1149 int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
1150 const u8 *peer, enum nl80211_tdls_operation oper)
1152 struct sta_info *sta;
1153 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1154 struct ieee80211_local *local = sdata->local;
1157 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1160 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1164 case NL80211_TDLS_ENABLE_LINK:
1165 case NL80211_TDLS_DISABLE_LINK:
1167 case NL80211_TDLS_TEARDOWN:
1168 case NL80211_TDLS_SETUP:
1169 case NL80211_TDLS_DISCOVERY_REQ:
1170 /* We don't support in-driver setup/teardown/discovery */
1174 mutex_lock(&local->mtx);
1175 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
1178 case NL80211_TDLS_ENABLE_LINK:
1179 if (sdata->vif.csa_active) {
1180 tdls_dbg(sdata, "TDLS: disallow link during CSA\n");
1186 sta = sta_info_get(sdata, peer);
1193 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1196 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
1197 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
1200 case NL80211_TDLS_DISABLE_LINK:
1202 * The teardown message in ieee80211_tdls_mgmt_teardown() was
1203 * created while the queues were stopped, so it might still be
1204 * pending. Before flushing the queues we need to be sure the
1205 * message is handled by the tasklet handling pending messages,
1206 * otherwise we might start destroying the station before
1207 * sending the teardown packet.
1208 * Note that this only forces the tasklet to flush pendings -
1209 * not to stop the tasklet from rescheduling itself.
1211 tasklet_kill(&local->tx_pending_tasklet);
1212 /* flush a potentially queued teardown packet */
1213 ieee80211_flush_queues(local, sdata, false);
1215 ret = sta_info_destroy_addr(sdata, peer);
1222 if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1223 cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
1224 eth_zero_addr(sdata->u.mgd.tdls_peer);
1227 mutex_unlock(&local->mtx);
1231 void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
1232 enum nl80211_tdls_operation oper,
1233 u16 reason_code, gfp_t gfp)
1235 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1237 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
1238 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
1243 cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
1245 EXPORT_SYMBOL(ieee80211_tdls_oper_request);
1248 iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
1250 struct ieee80211_ch_switch_timing *ch_sw;
1252 *buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
1253 *buf++ = sizeof(struct ieee80211_ch_switch_timing);
1255 ch_sw = (void *)buf;
1256 ch_sw->switch_time = cpu_to_le16(switch_time);
1257 ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
1260 /* find switch timing IE in SKB ready for Tx */
1261 static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
1263 struct ieee80211_tdls_data *tf;
1267 * Get the offset for the new location of the switch timing IE.
1268 * The SKB network header will now point to the "payload_type"
1269 * element of the TDLS data frame struct.
1271 tf = container_of(skb->data + skb_network_offset(skb),
1272 struct ieee80211_tdls_data, payload_type);
1273 ie_start = tf->u.chan_switch_req.variable;
1274 return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
1275 skb->len - (ie_start - skb->data));
1278 static struct sk_buff *
1279 ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
1280 struct cfg80211_chan_def *chandef,
1281 u32 *ch_sw_tm_ie_offset)
1283 struct ieee80211_sub_if_data *sdata = sta->sdata;
1284 u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
1285 2 + sizeof(struct ieee80211_ch_switch_timing)];
1286 int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
1287 u8 *pos = extra_ies;
1288 struct sk_buff *skb;
1291 * if chandef points to a wide channel add a Secondary-Channel
1292 * Offset information element
1294 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1295 struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
1298 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
1299 *pos++ = sizeof(*sec_chan_ie);
1300 sec_chan_ie = (void *)pos;
1302 ht40plus = cfg80211_get_chandef_type(chandef) ==
1303 NL80211_CHAN_HT40PLUS;
1304 sec_chan_ie->sec_chan_offs = ht40plus ?
1305 IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
1306 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1307 pos += sizeof(*sec_chan_ie);
1309 extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1312 /* just set the values to 0, this is a template */
1313 iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
1315 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1316 WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
1317 0, 0, !sta->sta.tdls_initiator,
1318 extra_ies, extra_ies_len,
1319 oper_class, chandef);
1323 skb = ieee80211_build_data_template(sdata, skb, 0);
1325 tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
1329 if (ch_sw_tm_ie_offset) {
1330 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1333 tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
1334 dev_kfree_skb_any(skb);
1338 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1342 "TDLS channel switch request template for %pM ch %d width %d\n",
1343 sta->sta.addr, chandef->chan->center_freq, chandef->width);
1348 ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
1349 const u8 *addr, u8 oper_class,
1350 struct cfg80211_chan_def *chandef)
1352 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1353 struct ieee80211_local *local = sdata->local;
1354 struct sta_info *sta;
1355 struct sk_buff *skb = NULL;
1359 mutex_lock(&local->sta_mtx);
1360 sta = sta_info_get(sdata, addr);
1363 "Invalid TDLS peer %pM for channel switch request\n",
1369 if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
1370 tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
1376 skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
1383 ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
1384 chandef, skb, ch_sw_tm_ie);
1386 set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1389 mutex_unlock(&local->sta_mtx);
1390 dev_kfree_skb_any(skb);
1395 ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
1396 struct net_device *dev,
1399 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1400 struct ieee80211_local *local = sdata->local;
1401 struct sta_info *sta;
1403 mutex_lock(&local->sta_mtx);
1404 sta = sta_info_get(sdata, addr);
1407 "Invalid TDLS peer %pM for channel switch cancel\n",
1412 if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1413 tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
1418 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1419 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1422 mutex_unlock(&local->sta_mtx);
1425 static struct sk_buff *
1426 ieee80211_tdls_ch_sw_resp_tmpl_get(struct sta_info *sta,
1427 u32 *ch_sw_tm_ie_offset)
1429 struct ieee80211_sub_if_data *sdata = sta->sdata;
1430 struct sk_buff *skb;
1431 u8 extra_ies[2 + sizeof(struct ieee80211_ch_switch_timing)];
1433 /* initial timing are always zero in the template */
1434 iee80211_tdls_add_ch_switch_timing(extra_ies, 0, 0);
1436 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1437 WLAN_TDLS_CHANNEL_SWITCH_RESPONSE,
1438 0, 0, !sta->sta.tdls_initiator,
1439 extra_ies, sizeof(extra_ies), 0, NULL);
1443 skb = ieee80211_build_data_template(sdata, skb, 0);
1446 "Failed building TDLS channel switch resp frame\n");
1450 if (ch_sw_tm_ie_offset) {
1451 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1455 "No switch timing IE in TDLS switch resp\n");
1456 dev_kfree_skb_any(skb);
1460 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1463 tdls_dbg(sdata, "TDLS get channel switch response template for %pM\n",
1469 ieee80211_process_tdls_channel_switch_resp(struct ieee80211_sub_if_data *sdata,
1470 struct sk_buff *skb)
1472 struct ieee80211_local *local = sdata->local;
1473 struct ieee802_11_elems elems;
1474 struct sta_info *sta;
1475 struct ieee80211_tdls_data *tf = (void *)skb->data;
1476 bool local_initiator;
1477 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1478 int baselen = offsetof(typeof(*tf), u.chan_switch_resp.variable);
1479 struct ieee80211_tdls_ch_sw_params params = {};
1482 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
1483 params.timestamp = rx_status->device_timestamp;
1485 if (skb->len < baselen) {
1486 tdls_dbg(sdata, "TDLS channel switch resp too short: %d\n",
1491 mutex_lock(&local->sta_mtx);
1492 sta = sta_info_get(sdata, tf->sa);
1493 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1494 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1500 params.sta = &sta->sta;
1501 params.status = le16_to_cpu(tf->u.chan_switch_resp.status_code);
1502 if (params.status != 0) {
1507 ieee802_11_parse_elems(tf->u.chan_switch_resp.variable,
1508 skb->len - baselen, false, &elems);
1509 if (elems.parse_error) {
1510 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch resp\n");
1515 if (!elems.ch_sw_timing || !elems.lnk_id) {
1516 tdls_dbg(sdata, "TDLS channel switch resp - missing IEs\n");
1521 /* validate the initiator is set correctly */
1523 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1524 if (local_initiator == sta->sta.tdls_initiator) {
1525 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1530 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1531 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1534 ieee80211_tdls_ch_sw_resp_tmpl_get(sta, ¶ms.ch_sw_tm_ie);
1535 if (!params.tmpl_skb) {
1541 drv_tdls_recv_channel_switch(sdata->local, sdata, ¶ms);
1544 "TDLS channel switch response received from %pM status %d\n",
1545 tf->sa, params.status);
1548 mutex_unlock(&local->sta_mtx);
1549 dev_kfree_skb_any(params.tmpl_skb);
1554 ieee80211_process_tdls_channel_switch_req(struct ieee80211_sub_if_data *sdata,
1555 struct sk_buff *skb)
1557 struct ieee80211_local *local = sdata->local;
1558 struct ieee802_11_elems elems;
1559 struct cfg80211_chan_def chandef;
1560 struct ieee80211_channel *chan;
1561 enum nl80211_channel_type chan_type;
1563 u8 target_channel, oper_class;
1564 bool local_initiator;
1565 struct sta_info *sta;
1566 enum ieee80211_band band;
1567 struct ieee80211_tdls_data *tf = (void *)skb->data;
1568 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1569 int baselen = offsetof(typeof(*tf), u.chan_switch_req.variable);
1570 struct ieee80211_tdls_ch_sw_params params = {};
1573 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
1574 params.timestamp = rx_status->device_timestamp;
1576 if (skb->len < baselen) {
1577 tdls_dbg(sdata, "TDLS channel switch req too short: %d\n",
1582 target_channel = tf->u.chan_switch_req.target_channel;
1583 oper_class = tf->u.chan_switch_req.oper_class;
1586 * We can't easily infer the channel band. The operating class is
1587 * ambiguous - there are multiple tables (US/Europe/JP/Global). The
1588 * solution here is to treat channels with number >14 as 5GHz ones,
1589 * and specifically check for the (oper_class, channel) combinations
1590 * where this doesn't hold. These are thankfully unique according to
1592 * We consider only the 2GHz and 5GHz bands and 20MHz+ channels as
1595 if ((oper_class == 112 || oper_class == 2 || oper_class == 3 ||
1596 oper_class == 4 || oper_class == 5 || oper_class == 6) &&
1597 target_channel < 14)
1598 band = IEEE80211_BAND_5GHZ;
1600 band = target_channel < 14 ? IEEE80211_BAND_2GHZ :
1601 IEEE80211_BAND_5GHZ;
1603 freq = ieee80211_channel_to_frequency(target_channel, band);
1605 tdls_dbg(sdata, "Invalid channel in TDLS chan switch: %d\n",
1610 chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
1613 "Unsupported channel for TDLS chan switch: %d\n",
1618 ieee802_11_parse_elems(tf->u.chan_switch_req.variable,
1619 skb->len - baselen, false, &elems);
1620 if (elems.parse_error) {
1621 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch req\n");
1625 if (!elems.ch_sw_timing || !elems.lnk_id) {
1626 tdls_dbg(sdata, "TDLS channel switch req - missing IEs\n");
1630 mutex_lock(&local->sta_mtx);
1631 sta = sta_info_get(sdata, tf->sa);
1632 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1633 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1639 params.sta = &sta->sta;
1641 /* validate the initiator is set correctly */
1643 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1644 if (local_initiator == sta->sta.tdls_initiator) {
1645 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1650 if (!sta->sta.ht_cap.ht_supported) {
1651 chan_type = NL80211_CHAN_NO_HT;
1652 } else if (!elems.sec_chan_offs) {
1653 chan_type = NL80211_CHAN_HT20;
1655 switch (elems.sec_chan_offs->sec_chan_offs) {
1656 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1657 chan_type = NL80211_CHAN_HT40PLUS;
1659 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1660 chan_type = NL80211_CHAN_HT40MINUS;
1663 chan_type = NL80211_CHAN_HT20;
1668 cfg80211_chandef_create(&chandef, chan, chan_type);
1669 params.chandef = &chandef;
1671 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1672 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1675 ieee80211_tdls_ch_sw_resp_tmpl_get(sta,
1676 ¶ms.ch_sw_tm_ie);
1677 if (!params.tmpl_skb) {
1682 drv_tdls_recv_channel_switch(sdata->local, sdata, ¶ms);
1685 "TDLS ch switch request received from %pM ch %d width %d\n",
1686 tf->sa, params.chandef->chan->center_freq,
1687 params.chandef->width);
1689 mutex_unlock(&local->sta_mtx);
1690 dev_kfree_skb_any(params.tmpl_skb);
1694 void ieee80211_process_tdls_channel_switch(struct ieee80211_sub_if_data *sdata,
1695 struct sk_buff *skb)
1697 struct ieee80211_tdls_data *tf = (void *)skb->data;
1698 struct wiphy *wiphy = sdata->local->hw.wiphy;
1700 /* make sure the driver supports it */
1701 if (!(wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
1704 /* we want to access the entire packet */
1705 if (skb_linearize(skb))
1708 * The packet/size was already validated by mac80211 Rx path, only look
1709 * at the action type.
1711 switch (tf->action_code) {
1712 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
1713 ieee80211_process_tdls_channel_switch_req(sdata, skb);
1715 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
1716 ieee80211_process_tdls_channel_switch_resp(sdata, skb);