blob: 358f9a4512ade758cbcf6be88698595bd0120787 [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
Johannes Bergd98ad832014-09-03 15:24:57 +03006 * Copyright 2014 Intel Mobile Communications GmbH
Arik Nemtsov95224fe2014-05-01 10:17:28 +03007 *
8 * This file is GPLv2 as found in COPYING.
9 */
10
11#include <linux/ieee80211.h>
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +030012#include <linux/log2.h>
Arik Nemtsovc887f0d32014-06-11 17:18:25 +030013#include <net/cfg80211.h>
Arik Nemtsov95224fe2014-05-01 10:17:28 +030014#include "ieee80211_i.h"
Arik Nemtsovee10f2c2014-06-11 17:18:27 +030015#include "driver-ops.h"
Arik Nemtsov95224fe2014-05-01 10:17:28 +030016
Arik Nemtsov17e6a592014-06-11 17:18:20 +030017/* give usermode some time for retries in setting up the TDLS session */
18#define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
19
20void ieee80211_tdls_peer_del_work(struct work_struct *wk)
21{
22 struct ieee80211_sub_if_data *sdata;
23 struct ieee80211_local *local;
24
25 sdata = container_of(wk, struct ieee80211_sub_if_data,
Arik Nemtsov81dd2b82014-07-17 17:14:25 +030026 u.mgd.tdls_peer_del_work.work);
Arik Nemtsov17e6a592014-06-11 17:18:20 +030027 local = sdata->local;
28
29 mutex_lock(&local->mtx);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +030030 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);
Arik Nemtsov17e6a592014-06-11 17:18:20 +030034 }
35 mutex_unlock(&local->mtx);
36}
37
Arik Nemtsov78632a12014-11-09 18:50:14 +020038static void ieee80211_tdls_add_ext_capab(struct ieee80211_local *local,
39 struct sk_buff *skb)
Arik Nemtsov95224fe2014-05-01 10:17:28 +030040{
41 u8 *pos = (void *)skb_put(skb, 7);
Arik Nemtsov78632a12014-11-09 18:50:14 +020042 bool chan_switch = local->hw.wiphy->features &
43 NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
Arik Nemtsov95224fe2014-05-01 10:17:28 +030044
45 *pos++ = WLAN_EID_EXT_CAPABILITY;
46 *pos++ = 5; /* len */
47 *pos++ = 0x0;
48 *pos++ = 0x0;
49 *pos++ = 0x0;
Arik Nemtsov78632a12014-11-09 18:50:14 +020050 *pos++ = chan_switch ? WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH : 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +030051 *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
52}
53
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020054static u8
55ieee80211_tdls_add_subband(struct ieee80211_sub_if_data *sdata,
56 struct sk_buff *skb, u16 start, u16 end,
57 u16 spacing)
58{
59 u8 subband_cnt = 0, ch_cnt = 0;
60 struct ieee80211_channel *ch;
61 struct cfg80211_chan_def chandef;
62 int i, subband_start;
63
64 for (i = start; i <= end; i += spacing) {
65 if (!ch_cnt)
66 subband_start = i;
67
68 ch = ieee80211_get_channel(sdata->local->hw.wiphy, i);
69 if (ch) {
70 /* we will be active on the channel */
71 u32 flags = IEEE80211_CHAN_DISABLED |
72 IEEE80211_CHAN_NO_IR;
73 cfg80211_chandef_create(&chandef, ch,
74 NL80211_CHAN_HT20);
75 if (cfg80211_chandef_usable(sdata->local->hw.wiphy,
76 &chandef, flags)) {
77 ch_cnt++;
78 continue;
79 }
80 }
81
82 if (ch_cnt) {
83 u8 *pos = skb_put(skb, 2);
84 *pos++ = ieee80211_frequency_to_channel(subband_start);
85 *pos++ = ch_cnt;
86
87 subband_cnt++;
88 ch_cnt = 0;
89 }
90 }
91
92 return subband_cnt;
93}
94
95static void
96ieee80211_tdls_add_supp_channels(struct ieee80211_sub_if_data *sdata,
97 struct sk_buff *skb)
98{
99 /*
100 * Add possible channels for TDLS. These are channels that are allowed
101 * to be active.
102 */
103 u8 subband_cnt;
104 u8 *pos = skb_put(skb, 2);
105
106 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
107
108 /*
109 * 5GHz and 2GHz channels numbers can overlap. Ignore this for now, as
110 * this doesn't happen in real world scenarios.
111 */
112
113 /* 2GHz, with 5MHz spacing */
114 subband_cnt = ieee80211_tdls_add_subband(sdata, skb, 2412, 2472, 5);
115
116 /* 5GHz, with 20MHz spacing */
117 subband_cnt += ieee80211_tdls_add_subband(sdata, skb, 5000, 5825, 20);
118
119 /* length */
120 *pos = 2 * subband_cnt;
121}
122
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200123static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb)
124{
125 u8 *pos = (void *)skb_put(skb, 3);
126
127 *pos++ = WLAN_EID_BSS_COEX_2040;
128 *pos++ = 1; /* len */
129
130 *pos++ = WLAN_BSS_COEX_INFORMATION_REQUEST;
131}
132
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300133static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
134 u16 status_code)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300135{
136 struct ieee80211_local *local = sdata->local;
137 u16 capab;
138
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300139 /* The capability will be 0 when sending a failure code */
140 if (status_code != 0)
141 return 0;
142
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300143 capab = 0;
144 if (ieee80211_get_sdata_band(sdata) != IEEE80211_BAND_2GHZ)
145 return capab;
146
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;
151
152 return capab;
153}
154
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300155static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
156 struct sk_buff *skb, const u8 *peer,
157 bool initiator)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300158{
159 struct ieee80211_tdls_lnkie *lnkid;
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300160 const u8 *init_addr, *rsp_addr;
161
162 if (initiator) {
163 init_addr = sdata->vif.addr;
164 rsp_addr = peer;
165 } else {
166 init_addr = peer;
167 rsp_addr = sdata->vif.addr;
168 }
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300169
170 lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
171
172 lnkid->ie_type = WLAN_EID_LINK_ID;
173 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
174
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300175 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);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300178}
179
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300180/* translate numbering in the WMM parameter IE to the mac80211 notation */
181static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
182{
183 switch (ac) {
184 default:
185 WARN_ON_ONCE(1);
186 case 0:
187 return IEEE80211_AC_BE;
188 case 1:
189 return IEEE80211_AC_BK;
190 case 2:
191 return IEEE80211_AC_VI;
192 case 3:
193 return IEEE80211_AC_VO;
194 }
195}
196
197static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
198{
199 u8 ret;
200
201 ret = aifsn & 0x0f;
202 if (acm)
203 ret |= 0x10;
204 ret |= (aci << 5) & 0x60;
205 return ret;
206}
207
208static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
209{
210 return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
211 ((ilog2(cw_max + 1) << 0x4) & 0xf0);
212}
213
214static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
215 struct sk_buff *skb)
216{
217 struct ieee80211_wmm_param_ie *wmm;
218 struct ieee80211_tx_queue_params *txq;
219 int i;
220
221 wmm = (void *)skb_put(skb, sizeof(*wmm));
222 memset(wmm, 0, sizeof(*wmm));
223
224 wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
225 wmm->len = sizeof(*wmm) - 2;
226
227 wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
228 wmm->oui[1] = 0x50;
229 wmm->oui[2] = 0xf2;
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 */
234
235 /*
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
238 */
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,
242 txq->acm, i);
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);
245 }
246}
247
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300248static void
249ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
250 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300251 u8 action_code, bool initiator,
252 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300253{
254 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300255 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300256 struct ieee80211_supported_band *sband;
257 struct ieee80211_sta_ht_cap ht_cap;
258 struct sta_info *sta = NULL;
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300259 size_t offset = 0, noffset;
260 u8 *pos;
261
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300262 rcu_read_lock();
263
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)) {
268 rcu_read_unlock();
269 return;
270 }
271 }
272
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300273 ieee80211_add_srates_ie(sdata, skb, false, band);
274 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200275 ieee80211_tdls_add_supp_channels(sdata, skb);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300276
277 /* add any custom IEs that go before Extended Capabilities */
278 if (extra_ies_len) {
279 static const u8 before_ext_cap[] = {
280 WLAN_EID_SUPP_RATES,
281 WLAN_EID_COUNTRY,
282 WLAN_EID_EXT_SUPP_RATES,
283 WLAN_EID_SUPPORTED_CHANNELS,
284 WLAN_EID_RSN,
285 };
286 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
287 before_ext_cap,
288 ARRAY_SIZE(before_ext_cap),
289 offset);
290 pos = skb_put(skb, noffset - offset);
291 memcpy(pos, extra_ies + offset, noffset - offset);
292 offset = noffset;
293 }
294
Arik Nemtsov78632a12014-11-09 18:50:14 +0200295 ieee80211_tdls_add_ext_capab(local, skb);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300296
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300297 /* 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 */
301
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300302 /* add any custom IEs that go before HT capabilities */
303 if (extra_ies_len) {
304 static const u8 before_ht_cap[] = {
305 WLAN_EID_SUPP_RATES,
306 WLAN_EID_COUNTRY,
307 WLAN_EID_EXT_SUPP_RATES,
308 WLAN_EID_SUPPORTED_CHANNELS,
309 WLAN_EID_RSN,
310 WLAN_EID_EXT_CAPABILITY,
311 WLAN_EID_QOS_CAPA,
312 WLAN_EID_FAST_BSS_TRANSITION,
313 WLAN_EID_TIMEOUT_INTERVAL,
314 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
315 };
316 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
317 before_ht_cap,
318 ARRAY_SIZE(before_ht_cap),
319 offset);
320 pos = skb_put(skb, noffset - offset);
321 memcpy(pos, extra_ies + offset, noffset - offset);
322 offset = noffset;
323 }
324
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300325 /*
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.
329 */
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);
337
338 /* disable SMPS in TDLS initiator */
339 ht_cap.cap |= (WLAN_HT_CAP_SM_PS_DISABLED
340 << IEEE80211_HT_CAP_SM_PS_SHIFT);
341 } else {
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);
346
347 /* the peer caps are already intersected with our own */
348 memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
349 }
350
351 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
352 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
353 }
354
355 rcu_read_unlock();
356
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200357 if (ht_cap.ht_supported &&
358 (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
359 ieee80211_tdls_add_bss_coex_ie(skb);
360
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300361 /* add any remaining IEs */
362 if (extra_ies_len) {
363 noffset = extra_ies_len;
364 pos = skb_put(skb, noffset - offset);
365 memcpy(pos, extra_ies + offset, noffset - offset);
366 }
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300367
368 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300369}
370
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300371static void
372ieee80211_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)
376{
377 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300378 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300379 size_t offset = 0, noffset;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300380 struct sta_info *sta, *ap_sta;
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300381 u8 *pos;
382
383 rcu_read_lock();
384
385 sta = sta_info_get(sdata, peer);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300386 ap_sta = sta_info_get(sdata, ifmgd->bssid);
387 if (WARN_ON_ONCE(!sta || !ap_sta)) {
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300388 rcu_read_unlock();
389 return;
390 }
391
392 /* add any custom IEs that go before the QoS IE */
393 if (extra_ies_len) {
394 static const u8 before_qos[] = {
395 WLAN_EID_RSN,
396 };
397 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
398 before_qos,
399 ARRAY_SIZE(before_qos),
400 offset);
401 pos = skb_put(skb, noffset - offset);
402 memcpy(pos, extra_ies + offset, noffset - offset);
403 offset = noffset;
404 }
405
406 /* add the QoS param IE if both the peer and we support it */
Johannes Berga74a8c82014-07-22 14:50:47 +0200407 if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300408 ieee80211_tdls_add_wmm_param_ie(sdata, skb);
409
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300410 /* add any custom IEs that go before HT operation */
411 if (extra_ies_len) {
412 static const u8 before_ht_op[] = {
413 WLAN_EID_RSN,
414 WLAN_EID_QOS_CAPA,
415 WLAN_EID_FAST_BSS_TRANSITION,
416 WLAN_EID_TIMEOUT_INTERVAL,
417 };
418 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
419 before_ht_op,
420 ARRAY_SIZE(before_ht_op),
421 offset);
422 pos = skb_put(skb, noffset - offset);
423 memcpy(pos, extra_ies + offset, noffset - offset);
424 offset = noffset;
425 }
426
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);
437 }
438 }
439
440 rcu_read_unlock();
441
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300442 /* add any remaining IEs */
443 if (extra_ies_len) {
444 noffset = extra_ies_len;
445 pos = skb_put(skb, noffset - offset);
446 memcpy(pos, extra_ies + offset, noffset - offset);
447 }
448
449 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300450}
451
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200452static void
453ieee80211_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)
458{
459 struct ieee80211_tdls_data *tf;
460 size_t offset = 0, noffset;
461 u8 *pos;
462
463 if (WARN_ON_ONCE(!chandef))
464 return;
465
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;
470
471 if (extra_ies_len) {
472 static const u8 before_lnkie[] = {
473 WLAN_EID_SECONDARY_CHANNEL_OFFSET,
474 };
475 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
476 before_lnkie,
477 ARRAY_SIZE(before_lnkie),
478 offset);
479 pos = skb_put(skb, noffset - offset);
480 memcpy(pos, extra_ies + offset, noffset - offset);
481 offset = noffset;
482 }
483
484 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
485
486 /* add any remaining IEs */
487 if (extra_ies_len) {
488 noffset = extra_ies_len;
489 pos = skb_put(skb, noffset - offset);
490 memcpy(pos, extra_ies + offset, noffset - offset);
491 }
492}
493
Arik Nemtsov46792a22014-07-17 17:14:19 +0300494static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
495 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300496 u8 action_code, u16 status_code,
497 bool initiator, const u8 *extra_ies,
Arik Nemtsovc2733902014-11-09 18:50:16 +0200498 size_t extra_ies_len, u8 oper_class,
499 struct cfg80211_chan_def *chandef)
Arik Nemtsov46792a22014-07-17 17:14:19 +0300500{
Arik Nemtsov46792a22014-07-17 17:14:19 +0300501 switch (action_code) {
502 case WLAN_TDLS_SETUP_REQUEST:
503 case WLAN_TDLS_SETUP_RESPONSE:
504 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300505 if (status_code == 0)
506 ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
507 action_code,
508 initiator,
509 extra_ies,
510 extra_ies_len);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300511 break;
512 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300513 if (status_code == 0)
514 ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
515 initiator, extra_ies,
516 extra_ies_len);
517 break;
Arik Nemtsov46792a22014-07-17 17:14:19 +0300518 case WLAN_TDLS_TEARDOWN:
519 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300520 if (extra_ies_len)
521 memcpy(skb_put(skb, extra_ies_len), extra_ies,
522 extra_ies_len);
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300523 if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
524 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300525 break;
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200526 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
527 ieee80211_tdls_add_chan_switch_req_ies(sdata, skb, peer,
528 initiator, extra_ies,
529 extra_ies_len,
530 oper_class, chandef);
531 break;
Arik Nemtsov46792a22014-07-17 17:14:19 +0300532 }
533
Arik Nemtsov46792a22014-07-17 17:14:19 +0300534}
535
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300536static int
537ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200538 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300539 u16 status_code, struct sk_buff *skb)
540{
541 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300542 struct ieee80211_tdls_data *tf;
543
544 tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
545
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;
550
Arik Nemtsov59cd85c2014-09-09 17:11:02 +0300551 /* network header is after the ethernet header */
552 skb_set_network_header(skb, ETH_HLEN);
553
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300554 switch (action_code) {
555 case WLAN_TDLS_SETUP_REQUEST:
556 tf->category = WLAN_CATEGORY_TDLS;
557 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
558
559 skb_put(skb, sizeof(tf->u.setup_req));
560 tf->u.setup_req.dialog_token = dialog_token;
561 tf->u.setup_req.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300562 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
563 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300564 break;
565 case WLAN_TDLS_SETUP_RESPONSE:
566 tf->category = WLAN_CATEGORY_TDLS;
567 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
568
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 =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300573 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
574 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300575 break;
576 case WLAN_TDLS_SETUP_CONFIRM:
577 tf->category = WLAN_CATEGORY_TDLS;
578 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
579
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;
583 break;
584 case WLAN_TDLS_TEARDOWN:
585 tf->category = WLAN_CATEGORY_TDLS;
586 tf->action_code = WLAN_TDLS_TEARDOWN;
587
588 skb_put(skb, sizeof(tf->u.teardown));
589 tf->u.teardown.reason_code = cpu_to_le16(status_code);
590 break;
591 case WLAN_TDLS_DISCOVERY_REQUEST:
592 tf->category = WLAN_CATEGORY_TDLS;
593 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
594
595 skb_put(skb, sizeof(tf->u.discover_req));
596 tf->u.discover_req.dialog_token = dialog_token;
597 break;
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200598 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
599 tf->category = WLAN_CATEGORY_TDLS;
600 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
601
602 skb_put(skb, sizeof(tf->u.chan_switch_req));
603 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300604 default:
605 return -EINVAL;
606 }
607
608 return 0;
609}
610
611static int
612ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200613 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300614 u16 status_code, struct sk_buff *skb)
615{
616 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300617 struct ieee80211_mgmt *mgmt;
618
619 mgmt = (void *)skb_put(skb, 24);
620 memset(mgmt, 0, 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);
624
625 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
626 IEEE80211_STYPE_ACTION);
627
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 =
635 dialog_token;
636 mgmt->u.action.u.tdls_discover_resp.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300637 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
638 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300639 break;
640 default:
641 return -EINVAL;
642 }
643
644 return 0;
645}
646
Arik Nemtsovc2733902014-11-09 18:50:16 +0200647static struct sk_buff *
648ieee80211_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)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300654{
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300655 struct ieee80211_local *local = sdata->local;
Arik Nemtsovc2733902014-11-09 18:50:16 +0200656 struct sk_buff *skb;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300657 int ret;
658
Arik Nemtsovc2733902014-11-09 18:50:16 +0200659 skb = netdev_alloc_skb(sdata->dev,
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200660 local->hw.extra_tx_headroom +
661 max(sizeof(struct ieee80211_mgmt),
662 sizeof(struct ieee80211_tdls_data)) +
663 50 + /* supported rates */
664 7 + /* ext capab */
665 26 + /* max(WMM-info, WMM-param) */
666 2 + max(sizeof(struct ieee80211_ht_cap),
667 sizeof(struct ieee80211_ht_operation)) +
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200668 50 + /* supported channels */
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200669 3 + /* 40/20 BSS coex */
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200670 extra_ies_len +
671 sizeof(struct ieee80211_tdls_lnkie));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300672 if (!skb)
Arik Nemtsovc2733902014-11-09 18:50:16 +0200673 return NULL;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300674
675 skb_reserve(skb, local->hw.extra_tx_headroom);
676
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:
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200683 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
Arik Nemtsovc2733902014-11-09 18:50:16 +0200684 ret = ieee80211_prep_tdls_encap_data(local->hw.wiphy,
685 sdata->dev, peer,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300686 action_code, dialog_token,
687 status_code, skb);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300688 break;
689 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsovc2733902014-11-09 18:50:16 +0200690 ret = ieee80211_prep_tdls_direct(local->hw.wiphy, sdata->dev,
691 peer, action_code,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300692 dialog_token, status_code,
693 skb);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300694 break;
695 default:
696 ret = -ENOTSUPP;
697 break;
698 }
699
700 if (ret < 0)
701 goto fail;
702
Arik Nemtsovc2733902014-11-09 18:50:16 +0200703 ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
704 initiator, extra_ies, extra_ies_len, oper_class,
705 chandef);
706 return skb;
707
708fail:
709 dev_kfree_skb(skb);
710 return NULL;
711}
712
713static int
714ieee80211_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)
720{
721 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
722 struct sk_buff *skb = NULL;
723 struct sta_info *sta;
724 u32 flags = 0;
725 int ret = 0;
726
Arik Nemtsov626911c2014-07-17 17:14:17 +0300727 rcu_read_lock();
728 sta = sta_info_get(sdata, peer);
729
730 /* infer the initiator if we can, to support old userspace */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300731 switch (action_code) {
732 case WLAN_TDLS_SETUP_REQUEST:
Arik Nemtsov8b941482014-10-22 12:32:48 +0300733 if (sta) {
Arik Nemtsov626911c2014-07-17 17:14:17 +0300734 set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
Arik Nemtsov8b941482014-10-22 12:32:48 +0300735 sta->sta.tdls_initiator = false;
736 }
Arik Nemtsov626911c2014-07-17 17:14:17 +0300737 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300738 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300739 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300740 initiator = true;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300741 break;
742 case WLAN_TDLS_SETUP_RESPONSE:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300743 /*
744 * In some testing scenarios, we send a request and response.
745 * Make the last packet sent take effect for the initiator
746 * value.
747 */
Arik Nemtsov8b941482014-10-22 12:32:48 +0300748 if (sta) {
Arik Nemtsov626911c2014-07-17 17:14:17 +0300749 clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
Arik Nemtsov8b941482014-10-22 12:32:48 +0300750 sta->sta.tdls_initiator = true;
751 }
Arik Nemtsov626911c2014-07-17 17:14:17 +0300752 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300753 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300754 initiator = false;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300755 break;
756 case WLAN_TDLS_TEARDOWN:
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200757 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300758 /* any value is ok */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300759 break;
760 default:
761 ret = -ENOTSUPP;
Arik Nemtsov626911c2014-07-17 17:14:17 +0300762 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300763 }
764
Arik Nemtsov46792a22014-07-17 17:14:19 +0300765 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
766 initiator = true;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300767
Arik Nemtsov626911c2014-07-17 17:14:17 +0300768 rcu_read_unlock();
769 if (ret < 0)
770 goto fail;
771
Arik Nemtsovc2733902014-11-09 18:50:16 +0200772 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,
776 chandef);
777 if (!skb) {
778 ret = -EINVAL;
779 goto fail;
780 }
781
782 if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300783 ieee80211_tx_skb(sdata, skb);
784 return 0;
785 }
786
787 /*
788 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
789 * we should default to AC_VI.
790 */
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);
795 skb->priority = 2;
796 break;
797 default:
798 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
799 skb->priority = 5;
800 break;
801 }
802
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200803 /*
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.
807 */
808 if ((action_code == WLAN_TDLS_TEARDOWN) &&
Arik Nemtsovc2733902014-11-09 18:50:16 +0200809 (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200810 struct sta_info *sta = NULL;
811 bool try_resend; /* Should we keep skb for possible resend */
812
813 /* If not sending directly to peer - no point in keeping skb */
814 rcu_read_lock();
815 sta = sta_info_get(sdata, peer);
816 try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
817 rcu_read_unlock();
818
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;
824
825 /*
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.
829 *
830 * No problem if skb_copy() fails, so no need to check.
831 */
832 sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
833 sdata->u.mgd.orig_teardown_skb = skb;
834 }
835 spin_unlock_bh(&sdata->u.mgd.teardown_lock);
836 }
837
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300838 /* disable bottom halves when entering the Tx path */
839 local_bh_disable();
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200840 __ieee80211_subif_start_xmit(skb, dev, flags);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300841 local_bh_enable();
842
843 return ret;
844
845fail:
846 dev_kfree_skb(skb);
847 return ret;
848}
849
Arik Nemtsov191dd462014-06-11 17:18:23 +0300850static int
851ieee80211_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)
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300855{
856 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
857 struct ieee80211_local *local = sdata->local;
858 int ret;
859
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300860 mutex_lock(&local->mtx);
861
862 /* we don't support concurrent TDLS peer setups */
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300863 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
864 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300865 ret = -EBUSY;
866 goto exit;
867 }
868
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300869 /*
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
Arik Nemtsov6ae32e52014-07-17 17:14:18 +0300872 * 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.
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300875 */
Arik Nemtsov6ae32e52014-07-17 17:14:18 +0300876 if (status_code == 0) {
877 rcu_read_lock();
878 if (!sta_info_get(sdata, peer)) {
879 rcu_read_unlock();
880 ret = -ENOLINK;
881 goto exit;
882 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300883 rcu_read_unlock();
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300884 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300885
Arik Nemtsovdb67d662014-06-11 17:18:24 +0300886 ieee80211_flush_queues(local, sdata);
887
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300888 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
889 dialog_token, status_code,
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300890 peer_capability, initiator,
Arik Nemtsovc2733902014-11-09 18:50:16 +0200891 extra_ies, extra_ies_len, 0,
892 NULL);
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300893 if (ret < 0)
894 goto exit;
895
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300896 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
Arik Nemtsov191dd462014-06-11 17:18:23 +0300897 ieee80211_queue_delayed_work(&sdata->local->hw,
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300898 &sdata->u.mgd.tdls_peer_del_work,
Arik Nemtsov191dd462014-06-11 17:18:23 +0300899 TDLS_PEER_SETUP_TIMEOUT);
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300900
901exit:
902 mutex_unlock(&local->mtx);
Arik Nemtsov191dd462014-06-11 17:18:23 +0300903 return ret;
904}
905
Arik Nemtsovdb67d662014-06-11 17:18:24 +0300906static int
907ieee80211_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)
912{
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;
916 int ret;
917
918 /*
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.
923 */
924 ieee80211_stop_vif_queues(local, sdata,
925 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
926 ieee80211_flush_queues(local, sdata);
927
928 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
929 dialog_token, status_code,
930 peer_capability, initiator,
Arik Nemtsovc2733902014-11-09 18:50:16 +0200931 extra_ies, extra_ies_len, 0,
932 NULL);
Arik Nemtsovdb67d662014-06-11 17:18:24 +0300933 if (ret < 0)
934 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
935 ret);
936
937 /*
938 * Remove the STA AUTH flag to force further traffic through the AP. If
939 * the STA was unreachable, it was already removed.
940 */
941 rcu_read_lock();
942 sta = sta_info_get(sdata, peer);
943 if (sta)
944 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
945 rcu_read_unlock();
946
947 ieee80211_wake_vif_queues(local, sdata,
948 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
949
950 return 0;
951}
952
Arik Nemtsov191dd462014-06-11 17:18:23 +0300953int 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)
958{
959 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
960 int ret;
961
962 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
963 return -ENOTSUPP;
964
965 /* make sure we are in managed mode, and associated */
966 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
967 !sdata->u.mgd.associated)
968 return -EINVAL;
969
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);
977 break;
978 case WLAN_TDLS_TEARDOWN:
Arik Nemtsovdb67d662014-06-11 17:18:24 +0300979 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
980 action_code, dialog_token,
981 status_code,
982 peer_capability, initiator,
983 extra_ies, extra_ies_len);
984 break;
Arik Nemtsov191dd462014-06-11 17:18:23 +0300985 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovee10f2c2014-06-11 17:18:27 +0300986 /*
987 * Protect the discovery so we can hear the TDLS discovery
988 * response frame. It is transmitted directly and not buffered
989 * by the AP.
990 */
991 drv_mgd_protect_tdls_discover(sdata->local, sdata);
992 /* fall-through */
993 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov191dd462014-06-11 17:18:23 +0300994 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
995 /* no special handling */
996 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
997 action_code,
998 dialog_token,
999 status_code,
1000 peer_capability,
1001 initiator, extra_ies,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001002 extra_ies_len, 0, NULL);
Arik Nemtsov191dd462014-06-11 17:18:23 +03001003 break;
1004 default:
1005 ret = -EOPNOTSUPP;
1006 break;
1007 }
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001008
1009 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
1010 action_code, peer, ret);
1011 return ret;
1012}
1013
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001014int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +02001015 const u8 *peer, enum nl80211_tdls_operation oper)
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001016{
1017 struct sta_info *sta;
1018 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001019 struct ieee80211_local *local = sdata->local;
1020 int ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001021
1022 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1023 return -ENOTSUPP;
1024
1025 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1026 return -EINVAL;
1027
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001028 switch (oper) {
1029 case NL80211_TDLS_ENABLE_LINK:
1030 case NL80211_TDLS_DISABLE_LINK:
1031 break;
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 */
1036 return -ENOTSUPP;
1037 }
1038
1039 mutex_lock(&local->mtx);
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001040 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
1041
1042 switch (oper) {
1043 case NL80211_TDLS_ENABLE_LINK:
1044 rcu_read_lock();
1045 sta = sta_info_get(sdata, peer);
1046 if (!sta) {
1047 rcu_read_unlock();
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001048 ret = -ENOLINK;
1049 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001050 }
1051
1052 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1053 rcu_read_unlock();
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001054
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001055 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
1056 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001057 ret = 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001058 break;
1059 case NL80211_TDLS_DISABLE_LINK:
Liad Kaufmanbb3f8482014-07-17 17:14:31 +03001060 /*
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.
1069 */
1070 tasklet_kill(&local->tx_pending_tasklet);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001071 /* flush a potentially queued teardown packet */
1072 ieee80211_flush_queues(local, sdata);
1073
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001074 ret = sta_info_destroy_addr(sdata, peer);
1075 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001076 default:
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001077 ret = -ENOTSUPP;
1078 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001079 }
1080
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001081 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);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001084 }
1085
1086 mutex_unlock(&local->mtx);
1087 return ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001088}
Arik Nemtsovc887f0d32014-06-11 17:18:25 +03001089
1090void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
1091 enum nl80211_tdls_operation oper,
1092 u16 reason_code, gfp_t gfp)
1093{
1094 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1095
1096 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
1097 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
1098 oper);
1099 return;
1100 }
1101
1102 cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
1103}
1104EXPORT_SYMBOL(ieee80211_tdls_oper_request);
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +02001105
1106static void
1107iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
1108{
1109 struct ieee80211_ch_switch_timing *ch_sw;
1110
1111 *buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
1112 *buf++ = sizeof(struct ieee80211_ch_switch_timing);
1113
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);
1117}
1118
1119/* find switch timing IE in SKB ready for Tx */
1120static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
1121{
1122 struct ieee80211_tdls_data *tf;
1123 const u8 *ie_start;
1124
1125 /*
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.
1129 */
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));
1135}
1136
1137static struct sk_buff *
1138ieee80211_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)
1141{
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;
1148
1149 /*
1150 * if chandef points to a wide channel add a Secondary-Channel
1151 * Offset information element
1152 */
1153 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1154 struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
1155 bool ht40plus;
1156
1157 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
1158 *pos++ = sizeof(*sec_chan_ie);
1159 sec_chan_ie = (void *)pos;
1160
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);
1167
1168 extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1169 }
1170
1171 /* just set the values to 0, this is a template */
1172 iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
1173
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);
1179 if (!skb)
1180 return NULL;
1181
1182 skb = ieee80211_build_data_template(sdata, skb, 0);
1183 if (IS_ERR(skb)) {
1184 tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
1185 return NULL;
1186 }
1187
1188 if (ch_sw_tm_ie_offset) {
1189 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1190
1191 if (!tm_ie) {
1192 tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
1193 dev_kfree_skb_any(skb);
1194 return NULL;
1195 }
1196
1197 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1198 }
1199
1200 tdls_dbg(sdata,
1201 "TDLS channel switch request template for %pM ch %d width %d\n",
1202 sta->sta.addr, chandef->chan->center_freq, chandef->width);
1203 return skb;
1204}
1205
1206int
1207ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
1208 const u8 *addr, u8 oper_class,
1209 struct cfg80211_chan_def *chandef)
1210{
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;
1215 u32 ch_sw_tm_ie;
1216 int ret;
1217
1218 mutex_lock(&local->sta_mtx);
1219 sta = sta_info_get(sdata, addr);
1220 if (!sta) {
1221 tdls_dbg(sdata,
1222 "Invalid TDLS peer %pM for channel switch request\n",
1223 addr);
1224 ret = -ENOENT;
1225 goto out;
1226 }
1227
1228 if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
1229 tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
1230 addr);
1231 ret = -ENOTSUPP;
1232 goto out;
1233 }
1234
1235 skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
1236 &ch_sw_tm_ie);
1237 if (!skb) {
1238 ret = -ENOENT;
1239 goto out;
1240 }
1241
1242 ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
1243 chandef, skb, ch_sw_tm_ie);
1244 if (!ret)
1245 set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1246
1247out:
1248 mutex_unlock(&local->sta_mtx);
1249 dev_kfree_skb_any(skb);
1250 return ret;
1251}
1252
1253void
1254ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
1255 struct net_device *dev,
1256 const u8 *addr)
1257{
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;
1261
1262 mutex_lock(&local->sta_mtx);
1263 sta = sta_info_get(sdata, addr);
1264 if (!sta) {
1265 tdls_dbg(sdata,
1266 "Invalid TDLS peer %pM for channel switch cancel\n",
1267 addr);
1268 goto out;
1269 }
1270
1271 if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1272 tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
1273 addr);
1274 goto out;
1275 }
1276
1277 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1278 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1279
1280out:
1281 mutex_unlock(&local->sta_mtx);
1282}