blob: f2cb3b6c1871e6b67f418b04f091af5609b117a8 [file] [log] [blame]
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001/*
2 * mac80211 TDLS handling code
3 *
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2014, Intel Corporation
6 *
7 * This file is GPLv2 as found in COPYING.
8 */
9
10#include <linux/ieee80211.h>
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +030011#include <linux/log2.h>
Arik Nemtsovc887f0d32014-06-11 17:18:25 +030012#include <net/cfg80211.h>
Arik Nemtsov95224fe2014-05-01 10:17:28 +030013#include "ieee80211_i.h"
Arik Nemtsovee10f2c2014-06-11 17:18:27 +030014#include "driver-ops.h"
Arik Nemtsov95224fe2014-05-01 10:17:28 +030015
Arik Nemtsov17e6a592014-06-11 17:18:20 +030016/* give usermode some time for retries in setting up the TDLS session */
17#define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
18
19void ieee80211_tdls_peer_del_work(struct work_struct *wk)
20{
21 struct ieee80211_sub_if_data *sdata;
22 struct ieee80211_local *local;
23
24 sdata = container_of(wk, struct ieee80211_sub_if_data,
Arik Nemtsov81dd2b82014-07-17 17:14:25 +030025 u.mgd.tdls_peer_del_work.work);
Arik Nemtsov17e6a592014-06-11 17:18:20 +030026 local = sdata->local;
27
28 mutex_lock(&local->mtx);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +030029 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer)) {
30 tdls_dbg(sdata, "TDLS del peer %pM\n", sdata->u.mgd.tdls_peer);
31 sta_info_destroy_addr(sdata, sdata->u.mgd.tdls_peer);
32 eth_zero_addr(sdata->u.mgd.tdls_peer);
Arik Nemtsov17e6a592014-06-11 17:18:20 +030033 }
34 mutex_unlock(&local->mtx);
35}
36
Arik Nemtsov95224fe2014-05-01 10:17:28 +030037static void ieee80211_tdls_add_ext_capab(struct sk_buff *skb)
38{
39 u8 *pos = (void *)skb_put(skb, 7);
40
41 *pos++ = WLAN_EID_EXT_CAPABILITY;
42 *pos++ = 5; /* len */
43 *pos++ = 0x0;
44 *pos++ = 0x0;
45 *pos++ = 0x0;
46 *pos++ = 0x0;
47 *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
48}
49
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +030050static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
51 u16 status_code)
Arik Nemtsov95224fe2014-05-01 10:17:28 +030052{
53 struct ieee80211_local *local = sdata->local;
54 u16 capab;
55
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +030056 /* The capability will be 0 when sending a failure code */
57 if (status_code != 0)
58 return 0;
59
Arik Nemtsov95224fe2014-05-01 10:17:28 +030060 capab = 0;
61 if (ieee80211_get_sdata_band(sdata) != IEEE80211_BAND_2GHZ)
62 return capab;
63
64 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
65 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
66 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
67 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
68
69 return capab;
70}
71
Arik Nemtsov1606ef42014-07-17 17:14:21 +030072static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
73 struct sk_buff *skb, const u8 *peer,
74 bool initiator)
Arik Nemtsov95224fe2014-05-01 10:17:28 +030075{
76 struct ieee80211_tdls_lnkie *lnkid;
Arik Nemtsov1606ef42014-07-17 17:14:21 +030077 const u8 *init_addr, *rsp_addr;
78
79 if (initiator) {
80 init_addr = sdata->vif.addr;
81 rsp_addr = peer;
82 } else {
83 init_addr = peer;
84 rsp_addr = sdata->vif.addr;
85 }
Arik Nemtsov95224fe2014-05-01 10:17:28 +030086
87 lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
88
89 lnkid->ie_type = WLAN_EID_LINK_ID;
90 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
91
Arik Nemtsov1606ef42014-07-17 17:14:21 +030092 memcpy(lnkid->bssid, sdata->u.mgd.bssid, ETH_ALEN);
93 memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
94 memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
Arik Nemtsov95224fe2014-05-01 10:17:28 +030095}
96
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +030097/* translate numbering in the WMM parameter IE to the mac80211 notation */
98static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
99{
100 switch (ac) {
101 default:
102 WARN_ON_ONCE(1);
103 case 0:
104 return IEEE80211_AC_BE;
105 case 1:
106 return IEEE80211_AC_BK;
107 case 2:
108 return IEEE80211_AC_VI;
109 case 3:
110 return IEEE80211_AC_VO;
111 }
112}
113
114static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
115{
116 u8 ret;
117
118 ret = aifsn & 0x0f;
119 if (acm)
120 ret |= 0x10;
121 ret |= (aci << 5) & 0x60;
122 return ret;
123}
124
125static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
126{
127 return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
128 ((ilog2(cw_max + 1) << 0x4) & 0xf0);
129}
130
131static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
132 struct sk_buff *skb)
133{
134 struct ieee80211_wmm_param_ie *wmm;
135 struct ieee80211_tx_queue_params *txq;
136 int i;
137
138 wmm = (void *)skb_put(skb, sizeof(*wmm));
139 memset(wmm, 0, sizeof(*wmm));
140
141 wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
142 wmm->len = sizeof(*wmm) - 2;
143
144 wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
145 wmm->oui[1] = 0x50;
146 wmm->oui[2] = 0xf2;
147 wmm->oui_type = 2; /* WME */
148 wmm->oui_subtype = 1; /* WME param */
149 wmm->version = 1; /* WME ver */
150 wmm->qos_info = 0; /* U-APSD not in use */
151
152 /*
153 * Use the EDCA parameters defined for the BSS, or default if the AP
154 * doesn't support it, as mandated by 802.11-2012 section 10.22.4
155 */
156 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
157 txq = &sdata->tx_conf[ieee80211_ac_from_wmm(i)];
158 wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
159 txq->acm, i);
160 wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
161 wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
162 }
163}
164
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300165static void
166ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
167 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300168 u8 action_code, bool initiator,
169 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300170{
171 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300172 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300173 struct ieee80211_supported_band *sband;
174 struct ieee80211_sta_ht_cap ht_cap;
175 struct sta_info *sta = NULL;
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300176 size_t offset = 0, noffset;
177 u8 *pos;
178
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300179 rcu_read_lock();
180
181 /* we should have the peer STA if we're already responding */
182 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
183 sta = sta_info_get(sdata, peer);
184 if (WARN_ON_ONCE(!sta)) {
185 rcu_read_unlock();
186 return;
187 }
188 }
189
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300190 ieee80211_add_srates_ie(sdata, skb, false, band);
191 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
192
193 /* add any custom IEs that go before Extended Capabilities */
194 if (extra_ies_len) {
195 static const u8 before_ext_cap[] = {
196 WLAN_EID_SUPP_RATES,
197 WLAN_EID_COUNTRY,
198 WLAN_EID_EXT_SUPP_RATES,
199 WLAN_EID_SUPPORTED_CHANNELS,
200 WLAN_EID_RSN,
201 };
202 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
203 before_ext_cap,
204 ARRAY_SIZE(before_ext_cap),
205 offset);
206 pos = skb_put(skb, noffset - offset);
207 memcpy(pos, extra_ies + offset, noffset - offset);
208 offset = noffset;
209 }
210
211 ieee80211_tdls_add_ext_capab(skb);
212
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300213 /* add the QoS element if we support it */
214 if (local->hw.queues >= IEEE80211_NUM_ACS &&
215 action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
216 ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
217
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300218 /* add any custom IEs that go before HT capabilities */
219 if (extra_ies_len) {
220 static const u8 before_ht_cap[] = {
221 WLAN_EID_SUPP_RATES,
222 WLAN_EID_COUNTRY,
223 WLAN_EID_EXT_SUPP_RATES,
224 WLAN_EID_SUPPORTED_CHANNELS,
225 WLAN_EID_RSN,
226 WLAN_EID_EXT_CAPABILITY,
227 WLAN_EID_QOS_CAPA,
228 WLAN_EID_FAST_BSS_TRANSITION,
229 WLAN_EID_TIMEOUT_INTERVAL,
230 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
231 };
232 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
233 before_ht_cap,
234 ARRAY_SIZE(before_ht_cap),
235 offset);
236 pos = skb_put(skb, noffset - offset);
237 memcpy(pos, extra_ies + offset, noffset - offset);
238 offset = noffset;
239 }
240
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300241 /*
242 * with TDLS we can switch channels, and HT-caps are not necessarily
243 * the same on all bands. The specification limits the setup to a
244 * single HT-cap, so use the current band for now.
245 */
246 sband = local->hw.wiphy->bands[band];
247 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
248 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
249 action_code == WLAN_TDLS_SETUP_RESPONSE) &&
250 ht_cap.ht_supported && (!sta || sta->sta.ht_cap.ht_supported)) {
251 if (action_code == WLAN_TDLS_SETUP_REQUEST) {
252 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
253
254 /* disable SMPS in TDLS initiator */
255 ht_cap.cap |= (WLAN_HT_CAP_SM_PS_DISABLED
256 << IEEE80211_HT_CAP_SM_PS_SHIFT);
257 } else {
258 /* disable SMPS in TDLS responder */
259 sta->sta.ht_cap.cap |=
260 (WLAN_HT_CAP_SM_PS_DISABLED
261 << IEEE80211_HT_CAP_SM_PS_SHIFT);
262
263 /* the peer caps are already intersected with our own */
264 memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
265 }
266
267 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
268 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
269 }
270
271 rcu_read_unlock();
272
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300273 /* add any remaining IEs */
274 if (extra_ies_len) {
275 noffset = extra_ies_len;
276 pos = skb_put(skb, noffset - offset);
277 memcpy(pos, extra_ies + offset, noffset - offset);
278 }
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300279
280 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300281}
282
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300283static void
284ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
285 struct sk_buff *skb, const u8 *peer,
286 bool initiator, const u8 *extra_ies,
287 size_t extra_ies_len)
288{
289 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300290 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300291 size_t offset = 0, noffset;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300292 struct sta_info *sta, *ap_sta;
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300293 u8 *pos;
294
295 rcu_read_lock();
296
297 sta = sta_info_get(sdata, peer);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300298 ap_sta = sta_info_get(sdata, ifmgd->bssid);
299 if (WARN_ON_ONCE(!sta || !ap_sta)) {
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300300 rcu_read_unlock();
301 return;
302 }
303
304 /* add any custom IEs that go before the QoS IE */
305 if (extra_ies_len) {
306 static const u8 before_qos[] = {
307 WLAN_EID_RSN,
308 };
309 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
310 before_qos,
311 ARRAY_SIZE(before_qos),
312 offset);
313 pos = skb_put(skb, noffset - offset);
314 memcpy(pos, extra_ies + offset, noffset - offset);
315 offset = noffset;
316 }
317
318 /* add the QoS param IE if both the peer and we support it */
Johannes Berga74a8c82014-07-22 14:50:47 +0200319 if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300320 ieee80211_tdls_add_wmm_param_ie(sdata, skb);
321
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300322 /* add any custom IEs that go before HT operation */
323 if (extra_ies_len) {
324 static const u8 before_ht_op[] = {
325 WLAN_EID_RSN,
326 WLAN_EID_QOS_CAPA,
327 WLAN_EID_FAST_BSS_TRANSITION,
328 WLAN_EID_TIMEOUT_INTERVAL,
329 };
330 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
331 before_ht_op,
332 ARRAY_SIZE(before_ht_op),
333 offset);
334 pos = skb_put(skb, noffset - offset);
335 memcpy(pos, extra_ies + offset, noffset - offset);
336 offset = noffset;
337 }
338
339 /* if HT support is only added in TDLS, we need an HT-operation IE */
340 if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
341 struct ieee80211_chanctx_conf *chanctx_conf =
342 rcu_dereference(sdata->vif.chanctx_conf);
343 if (!WARN_ON(!chanctx_conf)) {
344 pos = skb_put(skb, 2 +
345 sizeof(struct ieee80211_ht_operation));
346 /* send an empty HT operation IE */
347 ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
348 &chanctx_conf->def, 0);
349 }
350 }
351
352 rcu_read_unlock();
353
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300354 /* add any remaining IEs */
355 if (extra_ies_len) {
356 noffset = extra_ies_len;
357 pos = skb_put(skb, noffset - offset);
358 memcpy(pos, extra_ies + offset, noffset - offset);
359 }
360
361 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300362}
363
Arik Nemtsov46792a22014-07-17 17:14:19 +0300364static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
365 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300366 u8 action_code, u16 status_code,
367 bool initiator, const u8 *extra_ies,
368 size_t extra_ies_len)
Arik Nemtsov46792a22014-07-17 17:14:19 +0300369{
Arik Nemtsov46792a22014-07-17 17:14:19 +0300370 switch (action_code) {
371 case WLAN_TDLS_SETUP_REQUEST:
372 case WLAN_TDLS_SETUP_RESPONSE:
373 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300374 if (status_code == 0)
375 ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
376 action_code,
377 initiator,
378 extra_ies,
379 extra_ies_len);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300380 break;
381 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300382 if (status_code == 0)
383 ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
384 initiator, extra_ies,
385 extra_ies_len);
386 break;
Arik Nemtsov46792a22014-07-17 17:14:19 +0300387 case WLAN_TDLS_TEARDOWN:
388 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300389 if (extra_ies_len)
390 memcpy(skb_put(skb, extra_ies_len), extra_ies,
391 extra_ies_len);
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300392 if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
393 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300394 break;
395 }
396
Arik Nemtsov46792a22014-07-17 17:14:19 +0300397}
398
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300399static int
400ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200401 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300402 u16 status_code, struct sk_buff *skb)
403{
404 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300405 struct ieee80211_tdls_data *tf;
406
407 tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
408
409 memcpy(tf->da, peer, ETH_ALEN);
410 memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
411 tf->ether_type = cpu_to_be16(ETH_P_TDLS);
412 tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
413
414 switch (action_code) {
415 case WLAN_TDLS_SETUP_REQUEST:
416 tf->category = WLAN_CATEGORY_TDLS;
417 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
418
419 skb_put(skb, sizeof(tf->u.setup_req));
420 tf->u.setup_req.dialog_token = dialog_token;
421 tf->u.setup_req.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300422 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
423 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300424 break;
425 case WLAN_TDLS_SETUP_RESPONSE:
426 tf->category = WLAN_CATEGORY_TDLS;
427 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
428
429 skb_put(skb, sizeof(tf->u.setup_resp));
430 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
431 tf->u.setup_resp.dialog_token = dialog_token;
432 tf->u.setup_resp.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300433 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
434 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300435 break;
436 case WLAN_TDLS_SETUP_CONFIRM:
437 tf->category = WLAN_CATEGORY_TDLS;
438 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
439
440 skb_put(skb, sizeof(tf->u.setup_cfm));
441 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
442 tf->u.setup_cfm.dialog_token = dialog_token;
443 break;
444 case WLAN_TDLS_TEARDOWN:
445 tf->category = WLAN_CATEGORY_TDLS;
446 tf->action_code = WLAN_TDLS_TEARDOWN;
447
448 skb_put(skb, sizeof(tf->u.teardown));
449 tf->u.teardown.reason_code = cpu_to_le16(status_code);
450 break;
451 case WLAN_TDLS_DISCOVERY_REQUEST:
452 tf->category = WLAN_CATEGORY_TDLS;
453 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
454
455 skb_put(skb, sizeof(tf->u.discover_req));
456 tf->u.discover_req.dialog_token = dialog_token;
457 break;
458 default:
459 return -EINVAL;
460 }
461
462 return 0;
463}
464
465static int
466ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200467 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300468 u16 status_code, struct sk_buff *skb)
469{
470 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300471 struct ieee80211_mgmt *mgmt;
472
473 mgmt = (void *)skb_put(skb, 24);
474 memset(mgmt, 0, 24);
475 memcpy(mgmt->da, peer, ETH_ALEN);
476 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
477 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
478
479 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
480 IEEE80211_STYPE_ACTION);
481
482 switch (action_code) {
483 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
484 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
485 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
486 mgmt->u.action.u.tdls_discover_resp.action_code =
487 WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
488 mgmt->u.action.u.tdls_discover_resp.dialog_token =
489 dialog_token;
490 mgmt->u.action.u.tdls_discover_resp.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300491 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
492 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300493 break;
494 default:
495 return -EINVAL;
496 }
497
498 return 0;
499}
500
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300501static int
502ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
503 const u8 *peer, u8 action_code,
504 u8 dialog_token, u16 status_code,
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300505 u32 peer_capability, bool initiator,
506 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300507{
508 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
509 struct ieee80211_local *local = sdata->local;
510 struct sk_buff *skb = NULL;
511 bool send_direct;
Arik Nemtsov626911c2014-07-17 17:14:17 +0300512 struct sta_info *sta;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300513 int ret;
514
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300515 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
516 max(sizeof(struct ieee80211_mgmt),
517 sizeof(struct ieee80211_tdls_data)) +
518 50 + /* supported rates */
519 7 + /* ext capab */
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300520 26 + /* max(WMM-info, WMM-param) */
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300521 2 + max(sizeof(struct ieee80211_ht_cap),
522 sizeof(struct ieee80211_ht_operation)) +
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300523 extra_ies_len +
524 sizeof(struct ieee80211_tdls_lnkie));
525 if (!skb)
526 return -ENOMEM;
527
528 skb_reserve(skb, local->hw.extra_tx_headroom);
529
530 switch (action_code) {
531 case WLAN_TDLS_SETUP_REQUEST:
532 case WLAN_TDLS_SETUP_RESPONSE:
533 case WLAN_TDLS_SETUP_CONFIRM:
534 case WLAN_TDLS_TEARDOWN:
535 case WLAN_TDLS_DISCOVERY_REQUEST:
536 ret = ieee80211_prep_tdls_encap_data(wiphy, dev, peer,
537 action_code, dialog_token,
538 status_code, skb);
539 send_direct = false;
540 break;
541 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
542 ret = ieee80211_prep_tdls_direct(wiphy, dev, peer, action_code,
543 dialog_token, status_code,
544 skb);
545 send_direct = true;
546 break;
547 default:
548 ret = -ENOTSUPP;
549 break;
550 }
551
552 if (ret < 0)
553 goto fail;
554
Arik Nemtsov626911c2014-07-17 17:14:17 +0300555 rcu_read_lock();
556 sta = sta_info_get(sdata, peer);
557
558 /* infer the initiator if we can, to support old userspace */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300559 switch (action_code) {
560 case WLAN_TDLS_SETUP_REQUEST:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300561 if (sta)
562 set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
563 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300564 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300565 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300566 initiator = true;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300567 break;
568 case WLAN_TDLS_SETUP_RESPONSE:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300569 /*
570 * In some testing scenarios, we send a request and response.
571 * Make the last packet sent take effect for the initiator
572 * value.
573 */
574 if (sta)
575 clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
576 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300577 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300578 initiator = false;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300579 break;
580 case WLAN_TDLS_TEARDOWN:
581 /* any value is ok */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300582 break;
583 default:
584 ret = -ENOTSUPP;
Arik Nemtsov626911c2014-07-17 17:14:17 +0300585 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300586 }
587
Arik Nemtsov46792a22014-07-17 17:14:19 +0300588 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
589 initiator = true;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300590
Arik Nemtsov626911c2014-07-17 17:14:17 +0300591 rcu_read_unlock();
592 if (ret < 0)
593 goto fail;
594
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300595 ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
596 initiator, extra_ies, extra_ies_len);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300597 if (send_direct) {
598 ieee80211_tx_skb(sdata, skb);
599 return 0;
600 }
601
602 /*
603 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
604 * we should default to AC_VI.
605 */
606 switch (action_code) {
607 case WLAN_TDLS_SETUP_REQUEST:
608 case WLAN_TDLS_SETUP_RESPONSE:
609 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
610 skb->priority = 2;
611 break;
612 default:
613 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
614 skb->priority = 5;
615 break;
616 }
617
618 /* disable bottom halves when entering the Tx path */
619 local_bh_disable();
620 ret = ieee80211_subif_start_xmit(skb, dev);
621 local_bh_enable();
622
623 return ret;
624
625fail:
626 dev_kfree_skb(skb);
627 return ret;
628}
629
Arik Nemtsov191dd462014-06-11 17:18:23 +0300630static int
631ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
632 const u8 *peer, u8 action_code, u8 dialog_token,
633 u16 status_code, u32 peer_capability, bool initiator,
634 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300635{
636 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
637 struct ieee80211_local *local = sdata->local;
638 int ret;
639
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300640 mutex_lock(&local->mtx);
641
642 /* we don't support concurrent TDLS peer setups */
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300643 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
644 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300645 ret = -EBUSY;
646 goto exit;
647 }
648
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300649 /*
650 * make sure we have a STA representing the peer so we drop or buffer
651 * non-TDLS-setup frames to the peer. We can't send other packets
Arik Nemtsov6ae32e52014-07-17 17:14:18 +0300652 * during setup through the AP path.
653 * Allow error packets to be sent - sometimes we don't even add a STA
654 * before failing the setup.
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300655 */
Arik Nemtsov6ae32e52014-07-17 17:14:18 +0300656 if (status_code == 0) {
657 rcu_read_lock();
658 if (!sta_info_get(sdata, peer)) {
659 rcu_read_unlock();
660 ret = -ENOLINK;
661 goto exit;
662 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300663 rcu_read_unlock();
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300664 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300665
Arik Nemtsovdb67d662014-06-11 17:18:24 +0300666 ieee80211_flush_queues(local, sdata);
667
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300668 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
669 dialog_token, status_code,
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300670 peer_capability, initiator,
671 extra_ies, extra_ies_len);
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300672 if (ret < 0)
673 goto exit;
674
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300675 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
Arik Nemtsov191dd462014-06-11 17:18:23 +0300676 ieee80211_queue_delayed_work(&sdata->local->hw,
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300677 &sdata->u.mgd.tdls_peer_del_work,
Arik Nemtsov191dd462014-06-11 17:18:23 +0300678 TDLS_PEER_SETUP_TIMEOUT);
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300679
680exit:
681 mutex_unlock(&local->mtx);
Arik Nemtsov191dd462014-06-11 17:18:23 +0300682 return ret;
683}
684
Arik Nemtsovdb67d662014-06-11 17:18:24 +0300685static int
686ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
687 const u8 *peer, u8 action_code, u8 dialog_token,
688 u16 status_code, u32 peer_capability,
689 bool initiator, const u8 *extra_ies,
690 size_t extra_ies_len)
691{
692 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
693 struct ieee80211_local *local = sdata->local;
694 struct sta_info *sta;
695 int ret;
696
697 /*
698 * No packets can be transmitted to the peer via the AP during setup -
699 * the STA is set as a TDLS peer, but is not authorized.
700 * During teardown, we prevent direct transmissions by stopping the
701 * queues and flushing all direct packets.
702 */
703 ieee80211_stop_vif_queues(local, sdata,
704 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
705 ieee80211_flush_queues(local, sdata);
706
707 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
708 dialog_token, status_code,
709 peer_capability, initiator,
710 extra_ies, extra_ies_len);
711 if (ret < 0)
712 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
713 ret);
714
715 /*
716 * Remove the STA AUTH flag to force further traffic through the AP. If
717 * the STA was unreachable, it was already removed.
718 */
719 rcu_read_lock();
720 sta = sta_info_get(sdata, peer);
721 if (sta)
722 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
723 rcu_read_unlock();
724
725 ieee80211_wake_vif_queues(local, sdata,
726 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
727
728 return 0;
729}
730
Arik Nemtsov191dd462014-06-11 17:18:23 +0300731int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
732 const u8 *peer, u8 action_code, u8 dialog_token,
733 u16 status_code, u32 peer_capability,
734 bool initiator, const u8 *extra_ies,
735 size_t extra_ies_len)
736{
737 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
738 int ret;
739
740 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
741 return -ENOTSUPP;
742
743 /* make sure we are in managed mode, and associated */
744 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
745 !sdata->u.mgd.associated)
746 return -EINVAL;
747
748 switch (action_code) {
749 case WLAN_TDLS_SETUP_REQUEST:
750 case WLAN_TDLS_SETUP_RESPONSE:
751 ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
752 dialog_token, status_code,
753 peer_capability, initiator,
754 extra_ies, extra_ies_len);
755 break;
756 case WLAN_TDLS_TEARDOWN:
Arik Nemtsovdb67d662014-06-11 17:18:24 +0300757 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
758 action_code, dialog_token,
759 status_code,
760 peer_capability, initiator,
761 extra_ies, extra_ies_len);
762 break;
Arik Nemtsov191dd462014-06-11 17:18:23 +0300763 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovee10f2c2014-06-11 17:18:27 +0300764 /*
765 * Protect the discovery so we can hear the TDLS discovery
766 * response frame. It is transmitted directly and not buffered
767 * by the AP.
768 */
769 drv_mgd_protect_tdls_discover(sdata->local, sdata);
770 /* fall-through */
771 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov191dd462014-06-11 17:18:23 +0300772 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
773 /* no special handling */
774 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
775 action_code,
776 dialog_token,
777 status_code,
778 peer_capability,
779 initiator, extra_ies,
780 extra_ies_len);
781 break;
782 default:
783 ret = -EOPNOTSUPP;
784 break;
785 }
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300786
787 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
788 action_code, peer, ret);
789 return ret;
790}
791
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300792int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200793 const u8 *peer, enum nl80211_tdls_operation oper)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300794{
795 struct sta_info *sta;
796 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300797 struct ieee80211_local *local = sdata->local;
798 int ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300799
800 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
801 return -ENOTSUPP;
802
803 if (sdata->vif.type != NL80211_IFTYPE_STATION)
804 return -EINVAL;
805
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300806 switch (oper) {
807 case NL80211_TDLS_ENABLE_LINK:
808 case NL80211_TDLS_DISABLE_LINK:
809 break;
810 case NL80211_TDLS_TEARDOWN:
811 case NL80211_TDLS_SETUP:
812 case NL80211_TDLS_DISCOVERY_REQ:
813 /* We don't support in-driver setup/teardown/discovery */
814 return -ENOTSUPP;
815 }
816
817 mutex_lock(&local->mtx);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300818 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
819
820 switch (oper) {
821 case NL80211_TDLS_ENABLE_LINK:
822 rcu_read_lock();
823 sta = sta_info_get(sdata, peer);
824 if (!sta) {
825 rcu_read_unlock();
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300826 ret = -ENOLINK;
827 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300828 }
829
830 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
831 rcu_read_unlock();
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300832
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300833 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
834 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300835 ret = 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300836 break;
837 case NL80211_TDLS_DISABLE_LINK:
Liad Kaufmanbb3f8482014-07-17 17:14:31 +0300838 /*
839 * The teardown message in ieee80211_tdls_mgmt_teardown() was
840 * created while the queues were stopped, so it might still be
841 * pending. Before flushing the queues we need to be sure the
842 * message is handled by the tasklet handling pending messages,
843 * otherwise we might start destroying the station before
844 * sending the teardown packet.
845 * Note that this only forces the tasklet to flush pendings -
846 * not to stop the tasklet from rescheduling itself.
847 */
848 tasklet_kill(&local->tx_pending_tasklet);
Arik Nemtsovdb67d662014-06-11 17:18:24 +0300849 /* flush a potentially queued teardown packet */
850 ieee80211_flush_queues(local, sdata);
851
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300852 ret = sta_info_destroy_addr(sdata, peer);
853 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300854 default:
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300855 ret = -ENOTSUPP;
856 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300857 }
858
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300859 if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
860 cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
861 eth_zero_addr(sdata->u.mgd.tdls_peer);
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300862 }
863
864 mutex_unlock(&local->mtx);
865 return ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300866}
Arik Nemtsovc887f0d32014-06-11 17:18:25 +0300867
868void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
869 enum nl80211_tdls_operation oper,
870 u16 reason_code, gfp_t gfp)
871{
872 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
873
874 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
875 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
876 oper);
877 return;
878 }
879
880 cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
881}
882EXPORT_SYMBOL(ieee80211_tdls_oper_request);