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 u32 flags = IEEE80211_CHAN_DISABLED |
73 cfg80211_chandef_create(&chandef, ch,
75 if (cfg80211_chandef_usable(sdata->local->hw.wiphy,
83 u8 *pos = skb_put(skb, 2);
84 *pos++ = ieee80211_frequency_to_channel(subband_start);
96 ieee80211_tdls_add_supp_channels(struct ieee80211_sub_if_data *sdata,
100 * Add possible channels for TDLS. These are channels that are allowed
104 u8 *pos = skb_put(skb, 2);
106 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
109 * 5GHz and 2GHz channels numbers can overlap. Ignore this for now, as
110 * this doesn't happen in real world scenarios.
113 /* 2GHz, with 5MHz spacing */
114 subband_cnt = ieee80211_tdls_add_subband(sdata, skb, 2412, 2472, 5);
116 /* 5GHz, with 20MHz spacing */
117 subband_cnt += ieee80211_tdls_add_subband(sdata, skb, 5000, 5825, 20);
120 *pos = 2 * subband_cnt;
123 static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb)
125 u8 *pos = (void *)skb_put(skb, 3);
127 *pos++ = WLAN_EID_BSS_COEX_2040;
128 *pos++ = 1; /* len */
130 *pos++ = WLAN_BSS_COEX_INFORMATION_REQUEST;
133 static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
136 struct ieee80211_local *local = sdata->local;
139 /* The capability will be 0 when sending a failure code */
140 if (status_code != 0)
144 if (ieee80211_get_sdata_band(sdata) != IEEE80211_BAND_2GHZ)
147 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
148 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
149 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
150 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
155 static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
156 struct sk_buff *skb, const u8 *peer,
159 struct ieee80211_tdls_lnkie *lnkid;
160 const u8 *init_addr, *rsp_addr;
163 init_addr = sdata->vif.addr;
167 rsp_addr = sdata->vif.addr;
170 lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
172 lnkid->ie_type = WLAN_EID_LINK_ID;
173 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
175 memcpy(lnkid->bssid, sdata->u.mgd.bssid, ETH_ALEN);
176 memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
177 memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
180 /* translate numbering in the WMM parameter IE to the mac80211 notation */
181 static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
187 return IEEE80211_AC_BE;
189 return IEEE80211_AC_BK;
191 return IEEE80211_AC_VI;
193 return IEEE80211_AC_VO;
197 static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
204 ret |= (aci << 5) & 0x60;
208 static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
210 return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
211 ((ilog2(cw_max + 1) << 0x4) & 0xf0);
214 static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
217 struct ieee80211_wmm_param_ie *wmm;
218 struct ieee80211_tx_queue_params *txq;
221 wmm = (void *)skb_put(skb, sizeof(*wmm));
222 memset(wmm, 0, sizeof(*wmm));
224 wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
225 wmm->len = sizeof(*wmm) - 2;
227 wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
230 wmm->oui_type = 2; /* WME */
231 wmm->oui_subtype = 1; /* WME param */
232 wmm->version = 1; /* WME ver */
233 wmm->qos_info = 0; /* U-APSD not in use */
236 * Use the EDCA parameters defined for the BSS, or default if the AP
237 * doesn't support it, as mandated by 802.11-2012 section 10.22.4
239 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
240 txq = &sdata->tx_conf[ieee80211_ac_from_wmm(i)];
241 wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
243 wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
244 wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
249 ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
250 struct sk_buff *skb, const u8 *peer,
251 u8 action_code, bool initiator,
252 const u8 *extra_ies, size_t extra_ies_len)
254 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
255 struct ieee80211_local *local = sdata->local;
256 struct ieee80211_supported_band *sband;
257 struct ieee80211_sta_ht_cap ht_cap;
258 struct sta_info *sta = NULL;
259 size_t offset = 0, noffset;
264 /* we should have the peer STA if we're already responding */
265 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
266 sta = sta_info_get(sdata, peer);
267 if (WARN_ON_ONCE(!sta)) {
273 ieee80211_add_srates_ie(sdata, skb, false, band);
274 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
275 ieee80211_tdls_add_supp_channels(sdata, skb);
277 /* add any custom IEs that go before Extended Capabilities */
279 static const u8 before_ext_cap[] = {
282 WLAN_EID_EXT_SUPP_RATES,
283 WLAN_EID_SUPPORTED_CHANNELS,
286 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
288 ARRAY_SIZE(before_ext_cap),
290 pos = skb_put(skb, noffset - offset);
291 memcpy(pos, extra_ies + offset, noffset - offset);
295 ieee80211_tdls_add_ext_capab(local, skb);
297 /* add the QoS element if we support it */
298 if (local->hw.queues >= IEEE80211_NUM_ACS &&
299 action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
300 ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
302 /* add any custom IEs that go before HT capabilities */
304 static const u8 before_ht_cap[] = {
307 WLAN_EID_EXT_SUPP_RATES,
308 WLAN_EID_SUPPORTED_CHANNELS,
310 WLAN_EID_EXT_CAPABILITY,
312 WLAN_EID_FAST_BSS_TRANSITION,
313 WLAN_EID_TIMEOUT_INTERVAL,
314 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
316 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
318 ARRAY_SIZE(before_ht_cap),
320 pos = skb_put(skb, noffset - offset);
321 memcpy(pos, extra_ies + offset, noffset - offset);
326 * with TDLS we can switch channels, and HT-caps are not necessarily
327 * the same on all bands. The specification limits the setup to a
328 * single HT-cap, so use the current band for now.
330 sband = local->hw.wiphy->bands[band];
331 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
332 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
333 action_code == WLAN_TDLS_SETUP_RESPONSE) &&
334 ht_cap.ht_supported && (!sta || sta->sta.ht_cap.ht_supported)) {
335 if (action_code == WLAN_TDLS_SETUP_REQUEST) {
336 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
338 /* disable SMPS in TDLS initiator */
339 ht_cap.cap |= (WLAN_HT_CAP_SM_PS_DISABLED
340 << IEEE80211_HT_CAP_SM_PS_SHIFT);
342 /* disable SMPS in TDLS responder */
343 sta->sta.ht_cap.cap |=
344 (WLAN_HT_CAP_SM_PS_DISABLED
345 << IEEE80211_HT_CAP_SM_PS_SHIFT);
347 /* the peer caps are already intersected with our own */
348 memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
351 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
352 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
357 if (ht_cap.ht_supported &&
358 (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
359 ieee80211_tdls_add_bss_coex_ie(skb);
361 /* add any remaining IEs */
363 noffset = extra_ies_len;
364 pos = skb_put(skb, noffset - offset);
365 memcpy(pos, extra_ies + offset, noffset - offset);
368 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
372 ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
373 struct sk_buff *skb, const u8 *peer,
374 bool initiator, const u8 *extra_ies,
375 size_t extra_ies_len)
377 struct ieee80211_local *local = sdata->local;
378 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
379 size_t offset = 0, noffset;
380 struct sta_info *sta, *ap_sta;
385 sta = sta_info_get(sdata, peer);
386 ap_sta = sta_info_get(sdata, ifmgd->bssid);
387 if (WARN_ON_ONCE(!sta || !ap_sta)) {
392 /* add any custom IEs that go before the QoS IE */
394 static const u8 before_qos[] = {
397 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
399 ARRAY_SIZE(before_qos),
401 pos = skb_put(skb, noffset - offset);
402 memcpy(pos, extra_ies + offset, noffset - offset);
406 /* add the QoS param IE if both the peer and we support it */
407 if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
408 ieee80211_tdls_add_wmm_param_ie(sdata, skb);
410 /* add any custom IEs that go before HT operation */
412 static const u8 before_ht_op[] = {
415 WLAN_EID_FAST_BSS_TRANSITION,
416 WLAN_EID_TIMEOUT_INTERVAL,
418 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
420 ARRAY_SIZE(before_ht_op),
422 pos = skb_put(skb, noffset - offset);
423 memcpy(pos, extra_ies + offset, noffset - offset);
427 /* if HT support is only added in TDLS, we need an HT-operation IE */
428 if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
429 struct ieee80211_chanctx_conf *chanctx_conf =
430 rcu_dereference(sdata->vif.chanctx_conf);
431 if (!WARN_ON(!chanctx_conf)) {
432 pos = skb_put(skb, 2 +
433 sizeof(struct ieee80211_ht_operation));
434 /* send an empty HT operation IE */
435 ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
436 &chanctx_conf->def, 0);
442 /* add any remaining IEs */
444 noffset = extra_ies_len;
445 pos = skb_put(skb, noffset - offset);
446 memcpy(pos, extra_ies + offset, noffset - offset);
449 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
453 ieee80211_tdls_add_chan_switch_req_ies(struct ieee80211_sub_if_data *sdata,
454 struct sk_buff *skb, const u8 *peer,
455 bool initiator, const u8 *extra_ies,
456 size_t extra_ies_len, u8 oper_class,
457 struct cfg80211_chan_def *chandef)
459 struct ieee80211_tdls_data *tf;
460 size_t offset = 0, noffset;
463 if (WARN_ON_ONCE(!chandef))
466 tf = (void *)skb->data;
467 tf->u.chan_switch_req.target_channel =
468 ieee80211_frequency_to_channel(chandef->chan->center_freq);
469 tf->u.chan_switch_req.oper_class = oper_class;
472 static const u8 before_lnkie[] = {
473 WLAN_EID_SECONDARY_CHANNEL_OFFSET,
475 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
477 ARRAY_SIZE(before_lnkie),
479 pos = skb_put(skb, noffset - offset);
480 memcpy(pos, extra_ies + offset, noffset - offset);
484 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
486 /* add any remaining IEs */
488 noffset = extra_ies_len;
489 pos = skb_put(skb, noffset - offset);
490 memcpy(pos, extra_ies + offset, noffset - offset);
494 static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
495 struct sk_buff *skb, const u8 *peer,
496 u8 action_code, u16 status_code,
497 bool initiator, const u8 *extra_ies,
498 size_t extra_ies_len, u8 oper_class,
499 struct cfg80211_chan_def *chandef)
501 switch (action_code) {
502 case WLAN_TDLS_SETUP_REQUEST:
503 case WLAN_TDLS_SETUP_RESPONSE:
504 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
505 if (status_code == 0)
506 ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
512 case WLAN_TDLS_SETUP_CONFIRM:
513 if (status_code == 0)
514 ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
515 initiator, extra_ies,
518 case WLAN_TDLS_TEARDOWN:
519 case WLAN_TDLS_DISCOVERY_REQUEST:
521 memcpy(skb_put(skb, extra_ies_len), extra_ies,
523 if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
524 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
526 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
527 ieee80211_tdls_add_chan_switch_req_ies(sdata, skb, peer,
528 initiator, extra_ies,
530 oper_class, chandef);
537 ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
538 const u8 *peer, u8 action_code, u8 dialog_token,
539 u16 status_code, struct sk_buff *skb)
541 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
542 struct ieee80211_tdls_data *tf;
544 tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
546 memcpy(tf->da, peer, ETH_ALEN);
547 memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
548 tf->ether_type = cpu_to_be16(ETH_P_TDLS);
549 tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
551 /* network header is after the ethernet header */
552 skb_set_network_header(skb, ETH_HLEN);
554 switch (action_code) {
555 case WLAN_TDLS_SETUP_REQUEST:
556 tf->category = WLAN_CATEGORY_TDLS;
557 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
559 skb_put(skb, sizeof(tf->u.setup_req));
560 tf->u.setup_req.dialog_token = dialog_token;
561 tf->u.setup_req.capability =
562 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
565 case WLAN_TDLS_SETUP_RESPONSE:
566 tf->category = WLAN_CATEGORY_TDLS;
567 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
569 skb_put(skb, sizeof(tf->u.setup_resp));
570 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
571 tf->u.setup_resp.dialog_token = dialog_token;
572 tf->u.setup_resp.capability =
573 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
576 case WLAN_TDLS_SETUP_CONFIRM:
577 tf->category = WLAN_CATEGORY_TDLS;
578 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
580 skb_put(skb, sizeof(tf->u.setup_cfm));
581 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
582 tf->u.setup_cfm.dialog_token = dialog_token;
584 case WLAN_TDLS_TEARDOWN:
585 tf->category = WLAN_CATEGORY_TDLS;
586 tf->action_code = WLAN_TDLS_TEARDOWN;
588 skb_put(skb, sizeof(tf->u.teardown));
589 tf->u.teardown.reason_code = cpu_to_le16(status_code);
591 case WLAN_TDLS_DISCOVERY_REQUEST:
592 tf->category = WLAN_CATEGORY_TDLS;
593 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
595 skb_put(skb, sizeof(tf->u.discover_req));
596 tf->u.discover_req.dialog_token = dialog_token;
598 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
599 tf->category = WLAN_CATEGORY_TDLS;
600 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
602 skb_put(skb, sizeof(tf->u.chan_switch_req));
612 ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
613 const u8 *peer, u8 action_code, u8 dialog_token,
614 u16 status_code, struct sk_buff *skb)
616 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
617 struct ieee80211_mgmt *mgmt;
619 mgmt = (void *)skb_put(skb, 24);
621 memcpy(mgmt->da, peer, ETH_ALEN);
622 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
623 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
625 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
626 IEEE80211_STYPE_ACTION);
628 switch (action_code) {
629 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
630 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
631 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
632 mgmt->u.action.u.tdls_discover_resp.action_code =
633 WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
634 mgmt->u.action.u.tdls_discover_resp.dialog_token =
636 mgmt->u.action.u.tdls_discover_resp.capability =
637 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
647 static struct sk_buff *
648 ieee80211_tdls_build_mgmt_packet_data(struct ieee80211_sub_if_data *sdata,
649 const u8 *peer, u8 action_code,
650 u8 dialog_token, u16 status_code,
651 bool initiator, const u8 *extra_ies,
652 size_t extra_ies_len, u8 oper_class,
653 struct cfg80211_chan_def *chandef)
655 struct ieee80211_local *local = sdata->local;
659 skb = netdev_alloc_skb(sdata->dev,
660 local->hw.extra_tx_headroom +
661 max(sizeof(struct ieee80211_mgmt),
662 sizeof(struct ieee80211_tdls_data)) +
663 50 + /* supported rates */
665 26 + /* max(WMM-info, WMM-param) */
666 2 + max(sizeof(struct ieee80211_ht_cap),
667 sizeof(struct ieee80211_ht_operation)) +
668 50 + /* supported channels */
669 3 + /* 40/20 BSS coex */
671 sizeof(struct ieee80211_tdls_lnkie));
675 skb_reserve(skb, local->hw.extra_tx_headroom);
677 switch (action_code) {
678 case WLAN_TDLS_SETUP_REQUEST:
679 case WLAN_TDLS_SETUP_RESPONSE:
680 case WLAN_TDLS_SETUP_CONFIRM:
681 case WLAN_TDLS_TEARDOWN:
682 case WLAN_TDLS_DISCOVERY_REQUEST:
683 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
684 ret = ieee80211_prep_tdls_encap_data(local->hw.wiphy,
686 action_code, dialog_token,
689 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
690 ret = ieee80211_prep_tdls_direct(local->hw.wiphy, sdata->dev,
692 dialog_token, status_code,
703 ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
704 initiator, extra_ies, extra_ies_len, oper_class,
714 ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
715 const u8 *peer, u8 action_code, u8 dialog_token,
716 u16 status_code, u32 peer_capability,
717 bool initiator, const u8 *extra_ies,
718 size_t extra_ies_len, u8 oper_class,
719 struct cfg80211_chan_def *chandef)
721 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
722 struct sk_buff *skb = NULL;
723 struct sta_info *sta;
728 sta = sta_info_get(sdata, peer);
730 /* infer the initiator if we can, to support old userspace */
731 switch (action_code) {
732 case WLAN_TDLS_SETUP_REQUEST:
734 set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
735 sta->sta.tdls_initiator = false;
738 case WLAN_TDLS_SETUP_CONFIRM:
739 case WLAN_TDLS_DISCOVERY_REQUEST:
742 case WLAN_TDLS_SETUP_RESPONSE:
744 * In some testing scenarios, we send a request and response.
745 * Make the last packet sent take effect for the initiator
749 clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
750 sta->sta.tdls_initiator = true;
753 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
756 case WLAN_TDLS_TEARDOWN:
757 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
758 /* any value is ok */
765 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
772 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, peer, action_code,
773 dialog_token, status_code,
774 initiator, extra_ies,
775 extra_ies_len, oper_class,
782 if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
783 ieee80211_tx_skb(sdata, skb);
788 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
789 * we should default to AC_VI.
791 switch (action_code) {
792 case WLAN_TDLS_SETUP_REQUEST:
793 case WLAN_TDLS_SETUP_RESPONSE:
794 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
798 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
804 * Set the WLAN_TDLS_TEARDOWN flag to indicate a teardown in progress.
805 * Later, if no ACK is returned from peer, we will re-send the teardown
806 * packet through the AP.
808 if ((action_code == WLAN_TDLS_TEARDOWN) &&
809 (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
810 struct sta_info *sta = NULL;
811 bool try_resend; /* Should we keep skb for possible resend */
813 /* If not sending directly to peer - no point in keeping skb */
815 sta = sta_info_get(sdata, peer);
816 try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
819 spin_lock_bh(&sdata->u.mgd.teardown_lock);
820 if (try_resend && !sdata->u.mgd.teardown_skb) {
821 /* Mark it as requiring TX status callback */
822 flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
823 IEEE80211_TX_INTFL_MLME_CONN_TX;
826 * skb is copied since mac80211 will later set
827 * properties that might not be the same as the AP,
828 * such as encryption, QoS, addresses, etc.
830 * No problem if skb_copy() fails, so no need to check.
832 sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
833 sdata->u.mgd.orig_teardown_skb = skb;
835 spin_unlock_bh(&sdata->u.mgd.teardown_lock);
838 /* disable bottom halves when entering the Tx path */
840 __ieee80211_subif_start_xmit(skb, dev, flags);
851 ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
852 const u8 *peer, u8 action_code, u8 dialog_token,
853 u16 status_code, u32 peer_capability, bool initiator,
854 const u8 *extra_ies, size_t extra_ies_len)
856 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
857 struct ieee80211_local *local = sdata->local;
860 mutex_lock(&local->mtx);
862 /* we don't support concurrent TDLS peer setups */
863 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
864 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
870 * make sure we have a STA representing the peer so we drop or buffer
871 * non-TDLS-setup frames to the peer. We can't send other packets
872 * during setup through the AP path.
873 * Allow error packets to be sent - sometimes we don't even add a STA
874 * before failing the setup.
876 if (status_code == 0) {
878 if (!sta_info_get(sdata, peer)) {
886 ieee80211_flush_queues(local, sdata);
888 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
889 dialog_token, status_code,
890 peer_capability, initiator,
891 extra_ies, extra_ies_len, 0,
896 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
897 ieee80211_queue_delayed_work(&sdata->local->hw,
898 &sdata->u.mgd.tdls_peer_del_work,
899 TDLS_PEER_SETUP_TIMEOUT);
902 mutex_unlock(&local->mtx);
907 ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
908 const u8 *peer, u8 action_code, u8 dialog_token,
909 u16 status_code, u32 peer_capability,
910 bool initiator, const u8 *extra_ies,
911 size_t extra_ies_len)
913 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
914 struct ieee80211_local *local = sdata->local;
915 struct sta_info *sta;
919 * No packets can be transmitted to the peer via the AP during setup -
920 * the STA is set as a TDLS peer, but is not authorized.
921 * During teardown, we prevent direct transmissions by stopping the
922 * queues and flushing all direct packets.
924 ieee80211_stop_vif_queues(local, sdata,
925 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
926 ieee80211_flush_queues(local, sdata);
928 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
929 dialog_token, status_code,
930 peer_capability, initiator,
931 extra_ies, extra_ies_len, 0,
934 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
938 * Remove the STA AUTH flag to force further traffic through the AP. If
939 * the STA was unreachable, it was already removed.
942 sta = sta_info_get(sdata, peer);
944 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
947 ieee80211_wake_vif_queues(local, sdata,
948 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
953 int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
954 const u8 *peer, u8 action_code, u8 dialog_token,
955 u16 status_code, u32 peer_capability,
956 bool initiator, const u8 *extra_ies,
957 size_t extra_ies_len)
959 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
962 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
965 /* make sure we are in managed mode, and associated */
966 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
967 !sdata->u.mgd.associated)
970 switch (action_code) {
971 case WLAN_TDLS_SETUP_REQUEST:
972 case WLAN_TDLS_SETUP_RESPONSE:
973 ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
974 dialog_token, status_code,
975 peer_capability, initiator,
976 extra_ies, extra_ies_len);
978 case WLAN_TDLS_TEARDOWN:
979 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
980 action_code, dialog_token,
982 peer_capability, initiator,
983 extra_ies, extra_ies_len);
985 case WLAN_TDLS_DISCOVERY_REQUEST:
987 * Protect the discovery so we can hear the TDLS discovery
988 * response frame. It is transmitted directly and not buffered
991 drv_mgd_protect_tdls_discover(sdata->local, sdata);
993 case WLAN_TDLS_SETUP_CONFIRM:
994 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
995 /* no special handling */
996 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1001 initiator, extra_ies,
1002 extra_ies_len, 0, NULL);
1009 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
1010 action_code, peer, ret);
1014 int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
1015 const u8 *peer, enum nl80211_tdls_operation oper)
1017 struct sta_info *sta;
1018 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1019 struct ieee80211_local *local = sdata->local;
1022 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1025 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1029 case NL80211_TDLS_ENABLE_LINK:
1030 case NL80211_TDLS_DISABLE_LINK:
1032 case NL80211_TDLS_TEARDOWN:
1033 case NL80211_TDLS_SETUP:
1034 case NL80211_TDLS_DISCOVERY_REQ:
1035 /* We don't support in-driver setup/teardown/discovery */
1039 mutex_lock(&local->mtx);
1040 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
1043 case NL80211_TDLS_ENABLE_LINK:
1045 sta = sta_info_get(sdata, peer);
1052 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1055 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
1056 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
1059 case NL80211_TDLS_DISABLE_LINK:
1061 * The teardown message in ieee80211_tdls_mgmt_teardown() was
1062 * created while the queues were stopped, so it might still be
1063 * pending. Before flushing the queues we need to be sure the
1064 * message is handled by the tasklet handling pending messages,
1065 * otherwise we might start destroying the station before
1066 * sending the teardown packet.
1067 * Note that this only forces the tasklet to flush pendings -
1068 * not to stop the tasklet from rescheduling itself.
1070 tasklet_kill(&local->tx_pending_tasklet);
1071 /* flush a potentially queued teardown packet */
1072 ieee80211_flush_queues(local, sdata);
1074 ret = sta_info_destroy_addr(sdata, peer);
1081 if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1082 cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
1083 eth_zero_addr(sdata->u.mgd.tdls_peer);
1086 mutex_unlock(&local->mtx);
1090 void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
1091 enum nl80211_tdls_operation oper,
1092 u16 reason_code, gfp_t gfp)
1094 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1096 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
1097 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
1102 cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
1104 EXPORT_SYMBOL(ieee80211_tdls_oper_request);
1107 iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
1109 struct ieee80211_ch_switch_timing *ch_sw;
1111 *buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
1112 *buf++ = sizeof(struct ieee80211_ch_switch_timing);
1114 ch_sw = (void *)buf;
1115 ch_sw->switch_time = cpu_to_le16(switch_time);
1116 ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
1119 /* find switch timing IE in SKB ready for Tx */
1120 static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
1122 struct ieee80211_tdls_data *tf;
1126 * Get the offset for the new location of the switch timing IE.
1127 * The SKB network header will now point to the "payload_type"
1128 * element of the TDLS data frame struct.
1130 tf = container_of(skb->data + skb_network_offset(skb),
1131 struct ieee80211_tdls_data, payload_type);
1132 ie_start = tf->u.chan_switch_req.variable;
1133 return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
1134 skb->len - (ie_start - skb->data));
1137 static struct sk_buff *
1138 ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
1139 struct cfg80211_chan_def *chandef,
1140 u32 *ch_sw_tm_ie_offset)
1142 struct ieee80211_sub_if_data *sdata = sta->sdata;
1143 u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
1144 2 + sizeof(struct ieee80211_ch_switch_timing)];
1145 int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
1146 u8 *pos = extra_ies;
1147 struct sk_buff *skb;
1150 * if chandef points to a wide channel add a Secondary-Channel
1151 * Offset information element
1153 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1154 struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
1157 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
1158 *pos++ = sizeof(*sec_chan_ie);
1159 sec_chan_ie = (void *)pos;
1161 ht40plus = cfg80211_get_chandef_type(chandef) ==
1162 NL80211_CHAN_HT40PLUS;
1163 sec_chan_ie->sec_chan_offs = ht40plus ?
1164 IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
1165 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1166 pos += sizeof(*sec_chan_ie);
1168 extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1171 /* just set the values to 0, this is a template */
1172 iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
1174 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1175 WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
1176 0, 0, !sta->sta.tdls_initiator,
1177 extra_ies, extra_ies_len,
1178 oper_class, chandef);
1182 skb = ieee80211_build_data_template(sdata, skb, 0);
1184 tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
1188 if (ch_sw_tm_ie_offset) {
1189 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1192 tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
1193 dev_kfree_skb_any(skb);
1197 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1201 "TDLS channel switch request template for %pM ch %d width %d\n",
1202 sta->sta.addr, chandef->chan->center_freq, chandef->width);
1207 ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
1208 const u8 *addr, u8 oper_class,
1209 struct cfg80211_chan_def *chandef)
1211 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1212 struct ieee80211_local *local = sdata->local;
1213 struct sta_info *sta;
1214 struct sk_buff *skb = NULL;
1218 mutex_lock(&local->sta_mtx);
1219 sta = sta_info_get(sdata, addr);
1222 "Invalid TDLS peer %pM for channel switch request\n",
1228 if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
1229 tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
1235 skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
1242 ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
1243 chandef, skb, ch_sw_tm_ie);
1245 set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1248 mutex_unlock(&local->sta_mtx);
1249 dev_kfree_skb_any(skb);
1254 ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
1255 struct net_device *dev,
1258 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1259 struct ieee80211_local *local = sdata->local;
1260 struct sta_info *sta;
1262 mutex_lock(&local->sta_mtx);
1263 sta = sta_info_get(sdata, addr);
1266 "Invalid TDLS peer %pM for channel switch cancel\n",
1271 if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1272 tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
1277 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1278 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1281 mutex_unlock(&local->sta_mtx);