2 * mac80211 TDLS handling code
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2014, Intel Corporation
7 * This file is GPLv2 as found in COPYING.
10 #include <linux/ieee80211.h>
11 #include "ieee80211_i.h"
13 /* give usermode some time for retries in setting up the TDLS session */
14 #define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
16 void ieee80211_tdls_peer_del_work(struct work_struct *wk)
18 struct ieee80211_sub_if_data *sdata;
19 struct ieee80211_local *local;
21 sdata = container_of(wk, struct ieee80211_sub_if_data,
22 tdls_peer_del_work.work);
25 mutex_lock(&local->mtx);
26 if (!is_zero_ether_addr(sdata->tdls_peer)) {
27 tdls_dbg(sdata, "TDLS del peer %pM\n", sdata->tdls_peer);
28 sta_info_destroy_addr(sdata, sdata->tdls_peer);
29 eth_zero_addr(sdata->tdls_peer);
31 mutex_unlock(&local->mtx);
34 static void ieee80211_tdls_add_ext_capab(struct sk_buff *skb)
36 u8 *pos = (void *)skb_put(skb, 7);
38 *pos++ = WLAN_EID_EXT_CAPABILITY;
44 *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
47 static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata)
49 struct ieee80211_local *local = sdata->local;
53 if (ieee80211_get_sdata_band(sdata) != IEEE80211_BAND_2GHZ)
56 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
57 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
58 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
59 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
64 static void ieee80211_tdls_add_link_ie(struct sk_buff *skb, const u8 *src_addr,
65 const u8 *peer, const u8 *bssid)
67 struct ieee80211_tdls_lnkie *lnkid;
69 lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
71 lnkid->ie_type = WLAN_EID_LINK_ID;
72 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
74 memcpy(lnkid->bssid, bssid, ETH_ALEN);
75 memcpy(lnkid->init_sta, src_addr, ETH_ALEN);
76 memcpy(lnkid->resp_sta, peer, ETH_ALEN);
80 ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
81 const u8 *peer, u8 action_code, u8 dialog_token,
82 u16 status_code, struct sk_buff *skb)
84 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
85 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
86 struct ieee80211_tdls_data *tf;
88 tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
90 memcpy(tf->da, peer, ETH_ALEN);
91 memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
92 tf->ether_type = cpu_to_be16(ETH_P_TDLS);
93 tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
95 switch (action_code) {
96 case WLAN_TDLS_SETUP_REQUEST:
97 tf->category = WLAN_CATEGORY_TDLS;
98 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
100 skb_put(skb, sizeof(tf->u.setup_req));
101 tf->u.setup_req.dialog_token = dialog_token;
102 tf->u.setup_req.capability =
103 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata));
105 ieee80211_add_srates_ie(sdata, skb, false, band);
106 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
107 ieee80211_tdls_add_ext_capab(skb);
109 case WLAN_TDLS_SETUP_RESPONSE:
110 tf->category = WLAN_CATEGORY_TDLS;
111 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
113 skb_put(skb, sizeof(tf->u.setup_resp));
114 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
115 tf->u.setup_resp.dialog_token = dialog_token;
116 tf->u.setup_resp.capability =
117 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata));
119 ieee80211_add_srates_ie(sdata, skb, false, band);
120 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
121 ieee80211_tdls_add_ext_capab(skb);
123 case WLAN_TDLS_SETUP_CONFIRM:
124 tf->category = WLAN_CATEGORY_TDLS;
125 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
127 skb_put(skb, sizeof(tf->u.setup_cfm));
128 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
129 tf->u.setup_cfm.dialog_token = dialog_token;
131 case WLAN_TDLS_TEARDOWN:
132 tf->category = WLAN_CATEGORY_TDLS;
133 tf->action_code = WLAN_TDLS_TEARDOWN;
135 skb_put(skb, sizeof(tf->u.teardown));
136 tf->u.teardown.reason_code = cpu_to_le16(status_code);
138 case WLAN_TDLS_DISCOVERY_REQUEST:
139 tf->category = WLAN_CATEGORY_TDLS;
140 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
142 skb_put(skb, sizeof(tf->u.discover_req));
143 tf->u.discover_req.dialog_token = dialog_token;
153 ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
154 const u8 *peer, u8 action_code, u8 dialog_token,
155 u16 status_code, struct sk_buff *skb)
157 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
158 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
159 struct ieee80211_mgmt *mgmt;
161 mgmt = (void *)skb_put(skb, 24);
163 memcpy(mgmt->da, peer, ETH_ALEN);
164 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
165 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
167 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
168 IEEE80211_STYPE_ACTION);
170 switch (action_code) {
171 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
172 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
173 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
174 mgmt->u.action.u.tdls_discover_resp.action_code =
175 WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
176 mgmt->u.action.u.tdls_discover_resp.dialog_token =
178 mgmt->u.action.u.tdls_discover_resp.capability =
179 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata));
181 ieee80211_add_srates_ie(sdata, skb, false, band);
182 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
183 ieee80211_tdls_add_ext_capab(skb);
193 ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
194 const u8 *peer, u8 action_code,
195 u8 dialog_token, u16 status_code,
196 u32 peer_capability, bool initiator,
197 const u8 *extra_ies, size_t extra_ies_len)
199 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
200 struct ieee80211_local *local = sdata->local;
201 struct sk_buff *skb = NULL;
203 const u8 *init_addr, *rsp_addr;
206 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
207 max(sizeof(struct ieee80211_mgmt),
208 sizeof(struct ieee80211_tdls_data)) +
209 50 + /* supported rates */
212 sizeof(struct ieee80211_tdls_lnkie));
216 skb_reserve(skb, local->hw.extra_tx_headroom);
218 switch (action_code) {
219 case WLAN_TDLS_SETUP_REQUEST:
220 case WLAN_TDLS_SETUP_RESPONSE:
221 case WLAN_TDLS_SETUP_CONFIRM:
222 case WLAN_TDLS_TEARDOWN:
223 case WLAN_TDLS_DISCOVERY_REQUEST:
224 ret = ieee80211_prep_tdls_encap_data(wiphy, dev, peer,
225 action_code, dialog_token,
229 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
230 ret = ieee80211_prep_tdls_direct(wiphy, dev, peer, action_code,
231 dialog_token, status_code,
244 memcpy(skb_put(skb, extra_ies_len), extra_ies, extra_ies_len);
246 /* sanity check for initiator */
247 switch (action_code) {
248 case WLAN_TDLS_SETUP_REQUEST:
249 case WLAN_TDLS_SETUP_CONFIRM:
250 case WLAN_TDLS_DISCOVERY_REQUEST:
256 case WLAN_TDLS_SETUP_RESPONSE:
257 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
263 case WLAN_TDLS_TEARDOWN:
264 /* any value is ok */
272 init_addr = sdata->vif.addr;
276 rsp_addr = sdata->vif.addr;
279 ieee80211_tdls_add_link_ie(skb, init_addr, rsp_addr,
283 ieee80211_tx_skb(sdata, skb);
288 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
289 * we should default to AC_VI.
291 switch (action_code) {
292 case WLAN_TDLS_SETUP_REQUEST:
293 case WLAN_TDLS_SETUP_RESPONSE:
294 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
298 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
303 /* disable bottom halves when entering the Tx path */
305 ret = ieee80211_subif_start_xmit(skb, dev);
315 int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
316 const u8 *peer, u8 action_code, u8 dialog_token,
317 u16 status_code, u32 peer_capability,
318 bool initiator, const u8 *extra_ies,
319 size_t extra_ies_len)
321 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
322 struct ieee80211_local *local = sdata->local;
325 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
328 /* make sure we are in managed mode, and associated */
329 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
330 !sdata->u.mgd.associated)
333 mutex_lock(&local->mtx);
335 /* we don't support concurrent TDLS peer setups */
336 if (!is_zero_ether_addr(sdata->tdls_peer) &&
337 !ether_addr_equal(sdata->tdls_peer, peer) &&
338 (action_code == WLAN_TDLS_SETUP_REQUEST ||
339 action_code == WLAN_TDLS_SETUP_RESPONSE)) {
344 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
345 dialog_token, status_code,
346 peer_capability, initiator,
347 extra_ies, extra_ies_len);
351 if (action_code == WLAN_TDLS_SETUP_REQUEST ||
352 action_code == WLAN_TDLS_SETUP_RESPONSE) {
353 memcpy(sdata->tdls_peer, peer, ETH_ALEN);
354 ieee80211_queue_delayed_work(&sdata->local->hw,
355 &sdata->tdls_peer_del_work,
356 TDLS_PEER_SETUP_TIMEOUT);
360 mutex_unlock(&local->mtx);
362 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
363 action_code, peer, ret);
367 int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
368 const u8 *peer, enum nl80211_tdls_operation oper)
370 struct sta_info *sta;
371 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
372 struct ieee80211_local *local = sdata->local;
375 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
378 if (sdata->vif.type != NL80211_IFTYPE_STATION)
382 case NL80211_TDLS_ENABLE_LINK:
383 case NL80211_TDLS_DISABLE_LINK:
385 case NL80211_TDLS_TEARDOWN:
386 case NL80211_TDLS_SETUP:
387 case NL80211_TDLS_DISCOVERY_REQ:
388 /* We don't support in-driver setup/teardown/discovery */
392 mutex_lock(&local->mtx);
393 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
396 case NL80211_TDLS_ENABLE_LINK:
398 sta = sta_info_get(sdata, peer);
405 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
408 WARN_ON_ONCE(is_zero_ether_addr(sdata->tdls_peer) ||
409 !ether_addr_equal(sdata->tdls_peer, peer));
412 case NL80211_TDLS_DISABLE_LINK:
413 ret = sta_info_destroy_addr(sdata, peer);
420 if (ret == 0 && ether_addr_equal(sdata->tdls_peer, peer)) {
421 cancel_delayed_work(&sdata->tdls_peer_del_work);
422 eth_zero_addr(sdata->tdls_peer);
425 mutex_unlock(&local->mtx);