]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/mac80211/tdls.c
Merge branch 'mac80211' into mac80211-next
[mirror_ubuntu-bionic-kernel.git] / net / mac80211 / tdls.c
CommitLineData
95224fe8
AN
1/*
2 * mac80211 TDLS handling code
3 *
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2014, Intel Corporation
d98ad83e 6 * Copyright 2014 Intel Mobile Communications GmbH
d51c2ea3 7 * Copyright 2015 Intel Deutschland GmbH
95224fe8
AN
8 *
9 * This file is GPLv2 as found in COPYING.
10 */
11
12#include <linux/ieee80211.h>
6f7eaa47 13#include <linux/log2.h>
c887f0d3 14#include <net/cfg80211.h>
95224fe8 15#include "ieee80211_i.h"
ee10f2c7 16#include "driver-ops.h"
95224fe8 17
17e6a59a
AN
18/* give usermode some time for retries in setting up the TDLS session */
19#define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
20
21void ieee80211_tdls_peer_del_work(struct work_struct *wk)
22{
23 struct ieee80211_sub_if_data *sdata;
24 struct ieee80211_local *local;
25
26 sdata = container_of(wk, struct ieee80211_sub_if_data,
81dd2b88 27 u.mgd.tdls_peer_del_work.work);
17e6a59a
AN
28 local = sdata->local;
29
30 mutex_lock(&local->mtx);
81dd2b88
AN
31 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer)) {
32 tdls_dbg(sdata, "TDLS del peer %pM\n", sdata->u.mgd.tdls_peer);
33 sta_info_destroy_addr(sdata, sdata->u.mgd.tdls_peer);
34 eth_zero_addr(sdata->u.mgd.tdls_peer);
17e6a59a
AN
35 }
36 mutex_unlock(&local->mtx);
37}
38
b98fb44f 39static void ieee80211_tdls_add_ext_capab(struct ieee80211_sub_if_data *sdata,
78632a17 40 struct sk_buff *skb)
95224fe8 41{
b98fb44f 42 struct ieee80211_local *local = sdata->local;
78632a17
AN
43 bool chan_switch = local->hw.wiphy->features &
44 NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
b98fb44f
AN
45 bool wider_band = ieee80211_hw_check(&local->hw, TDLS_WIDER_BW);
46 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
47 struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
48 bool vht = sband && sband->vht_cap.vht_supported;
49 u8 *pos = (void *)skb_put(skb, 10);
95224fe8
AN
50
51 *pos++ = WLAN_EID_EXT_CAPABILITY;
b98fb44f 52 *pos++ = 8; /* len */
95224fe8
AN
53 *pos++ = 0x0;
54 *pos++ = 0x0;
55 *pos++ = 0x0;
78632a17 56 *pos++ = chan_switch ? WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH : 0;
95224fe8 57 *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
b98fb44f
AN
58 *pos++ = 0;
59 *pos++ = 0;
60 *pos++ = (vht && wider_band) ? WLAN_EXT_CAPA8_TDLS_WIDE_BW_ENABLED : 0;
95224fe8
AN
61}
62
f0d29cb9
AN
63static u8
64ieee80211_tdls_add_subband(struct ieee80211_sub_if_data *sdata,
65 struct sk_buff *skb, u16 start, u16 end,
66 u16 spacing)
67{
68 u8 subband_cnt = 0, ch_cnt = 0;
69 struct ieee80211_channel *ch;
70 struct cfg80211_chan_def chandef;
71 int i, subband_start;
923b352f 72 struct wiphy *wiphy = sdata->local->hw.wiphy;
f0d29cb9
AN
73
74 for (i = start; i <= end; i += spacing) {
75 if (!ch_cnt)
76 subband_start = i;
77
78 ch = ieee80211_get_channel(sdata->local->hw.wiphy, i);
79 if (ch) {
80 /* we will be active on the channel */
f0d29cb9 81 cfg80211_chandef_create(&chandef, ch,
50075892 82 NL80211_CHAN_NO_HT);
923b352f
AN
83 if (cfg80211_reg_can_beacon_relax(wiphy, &chandef,
84 sdata->wdev.iftype)) {
f0d29cb9 85 ch_cnt++;
50075892
AN
86 /*
87 * check if the next channel is also part of
88 * this allowed range
89 */
f0d29cb9
AN
90 continue;
91 }
92 }
93
50075892
AN
94 /*
95 * we've reached the end of a range, with allowed channels
96 * found
97 */
f0d29cb9
AN
98 if (ch_cnt) {
99 u8 *pos = skb_put(skb, 2);
100 *pos++ = ieee80211_frequency_to_channel(subband_start);
101 *pos++ = ch_cnt;
102
103 subband_cnt++;
104 ch_cnt = 0;
105 }
106 }
107
50075892
AN
108 /* all channels in the requested range are allowed - add them here */
109 if (ch_cnt) {
110 u8 *pos = skb_put(skb, 2);
111 *pos++ = ieee80211_frequency_to_channel(subband_start);
112 *pos++ = ch_cnt;
113
114 subband_cnt++;
115 }
116
f0d29cb9
AN
117 return subband_cnt;
118}
119
120static void
121ieee80211_tdls_add_supp_channels(struct ieee80211_sub_if_data *sdata,
122 struct sk_buff *skb)
123{
124 /*
125 * Add possible channels for TDLS. These are channels that are allowed
126 * to be active.
127 */
128 u8 subband_cnt;
129 u8 *pos = skb_put(skb, 2);
130
131 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
132
133 /*
134 * 5GHz and 2GHz channels numbers can overlap. Ignore this for now, as
135 * this doesn't happen in real world scenarios.
136 */
137
138 /* 2GHz, with 5MHz spacing */
139 subband_cnt = ieee80211_tdls_add_subband(sdata, skb, 2412, 2472, 5);
140
141 /* 5GHz, with 20MHz spacing */
142 subband_cnt += ieee80211_tdls_add_subband(sdata, skb, 5000, 5825, 20);
143
144 /* length */
145 *pos = 2 * subband_cnt;
146}
147
a38700dd
AN
148static void ieee80211_tdls_add_oper_classes(struct ieee80211_sub_if_data *sdata,
149 struct sk_buff *skb)
150{
151 u8 *pos;
152 u8 op_class;
153
154 if (!ieee80211_chandef_to_operating_class(&sdata->vif.bss_conf.chandef,
155 &op_class))
156 return;
157
158 pos = skb_put(skb, 4);
159 *pos++ = WLAN_EID_SUPPORTED_REGULATORY_CLASSES;
160 *pos++ = 2; /* len */
161
162 *pos++ = op_class;
163 *pos++ = op_class; /* give current operating class as alternate too */
164}
165
2cedd879
AN
166static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb)
167{
168 u8 *pos = (void *)skb_put(skb, 3);
169
170 *pos++ = WLAN_EID_BSS_COEX_2040;
171 *pos++ = 1; /* len */
172
173 *pos++ = WLAN_BSS_COEX_INFORMATION_REQUEST;
174}
175
dd8c0b03
AN
176static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
177 u16 status_code)
95224fe8 178{
dd8c0b03
AN
179 /* The capability will be 0 when sending a failure code */
180 if (status_code != 0)
181 return 0;
182
ea1b2b45
JB
183 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_2GHZ) {
184 return WLAN_CAPABILITY_SHORT_SLOT_TIME |
185 WLAN_CAPABILITY_SHORT_PREAMBLE;
186 }
95224fe8 187
ea1b2b45 188 return 0;
95224fe8
AN
189}
190
1606ef4a
AN
191static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
192 struct sk_buff *skb, const u8 *peer,
193 bool initiator)
95224fe8
AN
194{
195 struct ieee80211_tdls_lnkie *lnkid;
1606ef4a
AN
196 const u8 *init_addr, *rsp_addr;
197
198 if (initiator) {
199 init_addr = sdata->vif.addr;
200 rsp_addr = peer;
201 } else {
202 init_addr = peer;
203 rsp_addr = sdata->vif.addr;
204 }
95224fe8
AN
205
206 lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
207
208 lnkid->ie_type = WLAN_EID_LINK_ID;
209 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
210
1606ef4a
AN
211 memcpy(lnkid->bssid, sdata->u.mgd.bssid, ETH_ALEN);
212 memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
213 memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
95224fe8
AN
214}
215
fb28ec0c
AN
216static void
217ieee80211_tdls_add_aid(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
218{
219 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
220 u8 *pos = (void *)skb_put(skb, 4);
221
222 *pos++ = WLAN_EID_AID;
223 *pos++ = 2; /* len */
224 put_unaligned_le16(ifmgd->aid, pos);
225}
226
6f7eaa47
AN
227/* translate numbering in the WMM parameter IE to the mac80211 notation */
228static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
229{
230 switch (ac) {
231 default:
232 WARN_ON_ONCE(1);
233 case 0:
234 return IEEE80211_AC_BE;
235 case 1:
236 return IEEE80211_AC_BK;
237 case 2:
238 return IEEE80211_AC_VI;
239 case 3:
240 return IEEE80211_AC_VO;
241 }
242}
243
244static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
245{
246 u8 ret;
247
248 ret = aifsn & 0x0f;
249 if (acm)
250 ret |= 0x10;
251 ret |= (aci << 5) & 0x60;
252 return ret;
253}
254
255static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
256{
257 return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
258 ((ilog2(cw_max + 1) << 0x4) & 0xf0);
259}
260
261static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
262 struct sk_buff *skb)
263{
264 struct ieee80211_wmm_param_ie *wmm;
265 struct ieee80211_tx_queue_params *txq;
266 int i;
267
268 wmm = (void *)skb_put(skb, sizeof(*wmm));
269 memset(wmm, 0, sizeof(*wmm));
270
271 wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
272 wmm->len = sizeof(*wmm) - 2;
273
274 wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
275 wmm->oui[1] = 0x50;
276 wmm->oui[2] = 0xf2;
277 wmm->oui_type = 2; /* WME */
278 wmm->oui_subtype = 1; /* WME param */
279 wmm->version = 1; /* WME ver */
280 wmm->qos_info = 0; /* U-APSD not in use */
281
282 /*
283 * Use the EDCA parameters defined for the BSS, or default if the AP
284 * doesn't support it, as mandated by 802.11-2012 section 10.22.4
285 */
286 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
287 txq = &sdata->tx_conf[ieee80211_ac_from_wmm(i)];
288 wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
289 txq->acm, i);
290 wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
291 wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
292 }
293}
294
0fabfaaf
AN
295static void
296ieee80211_tdls_chandef_vht_upgrade(struct ieee80211_sub_if_data *sdata,
297 struct sta_info *sta)
298{
299 /* IEEE802.11ac-2013 Table E-4 */
300 u16 centers_80mhz[] = { 5210, 5290, 5530, 5610, 5690, 5775 };
301 struct cfg80211_chan_def uc = sta->tdls_chandef;
302 enum nl80211_chan_width max_width = ieee80211_get_sta_bw(&sta->sta);
303 int i;
304
305 /* only support upgrading non-narrow channels up to 80Mhz */
306 if (max_width == NL80211_CHAN_WIDTH_5 ||
307 max_width == NL80211_CHAN_WIDTH_10)
308 return;
309
310 if (max_width > NL80211_CHAN_WIDTH_80)
311 max_width = NL80211_CHAN_WIDTH_80;
312
313 if (uc.width == max_width)
314 return;
315 /*
316 * Channel usage constrains in the IEEE802.11ac-2013 specification only
317 * allow expanding a 20MHz channel to 80MHz in a single way. In
318 * addition, there are no 40MHz allowed channels that are not part of
319 * the allowed 80MHz range in the 5GHz spectrum (the relevant one here).
320 */
321 for (i = 0; i < ARRAY_SIZE(centers_80mhz); i++)
322 if (abs(uc.chan->center_freq - centers_80mhz[i]) <= 30) {
323 uc.center_freq1 = centers_80mhz[i];
324 uc.width = NL80211_CHAN_WIDTH_80;
325 break;
326 }
327
328 if (!uc.center_freq1)
329 return;
330
331 /* proceed to downgrade the chandef until usable or the same */
332 while (uc.width > max_width &&
333 !cfg80211_reg_can_beacon(sdata->local->hw.wiphy,
334 &uc, sdata->wdev.iftype))
335 ieee80211_chandef_downgrade(&uc);
336
337 if (!cfg80211_chandef_identical(&uc, &sta->tdls_chandef)) {
338 tdls_dbg(sdata, "TDLS ch width upgraded %d -> %d\n",
339 sta->tdls_chandef.width, uc.width);
340
341 /*
342 * the station is not yet authorized when BW upgrade is done,
343 * locking is not required
344 */
345 sta->tdls_chandef = uc;
346 }
347}
348
f09a87d2
AN
349static void
350ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
351 struct sk_buff *skb, const u8 *peer,
1606ef4a
AN
352 u8 action_code, bool initiator,
353 const u8 *extra_ies, size_t extra_ies_len)
f09a87d2
AN
354{
355 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
40b861a0 356 struct ieee80211_local *local = sdata->local;
13cc8a4a
AN
357 struct ieee80211_supported_band *sband;
358 struct ieee80211_sta_ht_cap ht_cap;
fb28ec0c 359 struct ieee80211_sta_vht_cap vht_cap;
13cc8a4a 360 struct sta_info *sta = NULL;
f09a87d2
AN
361 size_t offset = 0, noffset;
362 u8 *pos;
363
364 ieee80211_add_srates_ie(sdata, skb, false, band);
365 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
f0d29cb9 366 ieee80211_tdls_add_supp_channels(sdata, skb);
f09a87d2
AN
367
368 /* add any custom IEs that go before Extended Capabilities */
369 if (extra_ies_len) {
370 static const u8 before_ext_cap[] = {
371 WLAN_EID_SUPP_RATES,
372 WLAN_EID_COUNTRY,
373 WLAN_EID_EXT_SUPP_RATES,
374 WLAN_EID_SUPPORTED_CHANNELS,
375 WLAN_EID_RSN,
376 };
377 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
378 before_ext_cap,
379 ARRAY_SIZE(before_ext_cap),
380 offset);
381 pos = skb_put(skb, noffset - offset);
382 memcpy(pos, extra_ies + offset, noffset - offset);
383 offset = noffset;
384 }
385
b98fb44f 386 ieee80211_tdls_add_ext_capab(sdata, skb);
f09a87d2 387
40b861a0
AN
388 /* add the QoS element if we support it */
389 if (local->hw.queues >= IEEE80211_NUM_ACS &&
390 action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
391 ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
392
f09a87d2
AN
393 /* add any custom IEs that go before HT capabilities */
394 if (extra_ies_len) {
395 static const u8 before_ht_cap[] = {
396 WLAN_EID_SUPP_RATES,
397 WLAN_EID_COUNTRY,
398 WLAN_EID_EXT_SUPP_RATES,
399 WLAN_EID_SUPPORTED_CHANNELS,
400 WLAN_EID_RSN,
401 WLAN_EID_EXT_CAPABILITY,
402 WLAN_EID_QOS_CAPA,
403 WLAN_EID_FAST_BSS_TRANSITION,
404 WLAN_EID_TIMEOUT_INTERVAL,
405 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
406 };
407 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
408 before_ht_cap,
409 ARRAY_SIZE(before_ht_cap),
410 offset);
411 pos = skb_put(skb, noffset - offset);
412 memcpy(pos, extra_ies + offset, noffset - offset);
413 offset = noffset;
414 }
415
0fabfaaf 416 mutex_lock(&local->sta_mtx);
ae2e9fba
AN
417
418 /* we should have the peer STA if we're already responding */
419 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
420 sta = sta_info_get(sdata, peer);
421 if (WARN_ON_ONCE(!sta)) {
0fabfaaf 422 mutex_unlock(&local->sta_mtx);
ae2e9fba
AN
423 return;
424 }
0fabfaaf
AN
425
426 sta->tdls_chandef = sdata->vif.bss_conf.chandef;
ae2e9fba
AN
427 }
428
a38700dd
AN
429 ieee80211_tdls_add_oper_classes(sdata, skb);
430
13cc8a4a
AN
431 /*
432 * with TDLS we can switch channels, and HT-caps are not necessarily
433 * the same on all bands. The specification limits the setup to a
434 * single HT-cap, so use the current band for now.
435 */
436 sband = local->hw.wiphy->bands[band];
437 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
c5309ba7 438
070e176a
AN
439 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
440 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
441 ht_cap.ht_supported) {
c5309ba7
JB
442 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
443
444 /* disable SMPS in TDLS initiator */
445 ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
446 << IEEE80211_HT_CAP_SM_PS_SHIFT;
447
448 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
449 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
450 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
451 ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
c5309ba7
JB
452 /* the peer caps are already intersected with our own */
453 memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
13cc8a4a
AN
454
455 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
456 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
457 }
458
2cedd879
AN
459 if (ht_cap.ht_supported &&
460 (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
461 ieee80211_tdls_add_bss_coex_ie(skb);
462
fb28ec0c
AN
463 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
464
465 /* add any custom IEs that go before VHT capabilities */
466 if (extra_ies_len) {
467 static const u8 before_vht_cap[] = {
468 WLAN_EID_SUPP_RATES,
469 WLAN_EID_COUNTRY,
470 WLAN_EID_EXT_SUPP_RATES,
471 WLAN_EID_SUPPORTED_CHANNELS,
472 WLAN_EID_RSN,
473 WLAN_EID_EXT_CAPABILITY,
474 WLAN_EID_QOS_CAPA,
475 WLAN_EID_FAST_BSS_TRANSITION,
476 WLAN_EID_TIMEOUT_INTERVAL,
477 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
478 WLAN_EID_MULTI_BAND,
479 };
480 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
481 before_vht_cap,
482 ARRAY_SIZE(before_vht_cap),
483 offset);
484 pos = skb_put(skb, noffset - offset);
485 memcpy(pos, extra_ies + offset, noffset - offset);
486 offset = noffset;
487 }
488
489 /* build the VHT-cap similarly to the HT-cap */
490 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
070e176a
AN
491 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
492 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
493 vht_cap.vht_supported) {
fb28ec0c
AN
494 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
495
496 /* the AID is present only when VHT is implemented */
070e176a
AN
497 if (action_code == WLAN_TDLS_SETUP_REQUEST)
498 ieee80211_tdls_add_aid(sdata, skb);
fb28ec0c
AN
499
500 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
501 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
502 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
503 vht_cap.vht_supported && sta->sta.vht_cap.vht_supported) {
504 /* the peer caps are already intersected with our own */
505 memcpy(&vht_cap, &sta->sta.vht_cap, sizeof(vht_cap));
506
507 /* the AID is present only when VHT is implemented */
508 ieee80211_tdls_add_aid(sdata, skb);
509
510 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
511 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
0fabfaaf
AN
512
513 /*
514 * if both peers support WIDER_BW, we can expand the chandef to
515 * a wider compatible one, up to 80MHz
516 */
517 if (test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
518 ieee80211_tdls_chandef_vht_upgrade(sdata, sta);
fb28ec0c
AN
519 }
520
0fabfaaf 521 mutex_unlock(&local->sta_mtx);
fb28ec0c 522
f09a87d2
AN
523 /* add any remaining IEs */
524 if (extra_ies_len) {
525 noffset = extra_ies_len;
526 pos = skb_put(skb, noffset - offset);
527 memcpy(pos, extra_ies + offset, noffset - offset);
528 }
1606ef4a 529
f09a87d2
AN
530}
531
6f7eaa47
AN
532static void
533ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
534 struct sk_buff *skb, const u8 *peer,
535 bool initiator, const u8 *extra_ies,
536 size_t extra_ies_len)
537{
538 struct ieee80211_local *local = sdata->local;
13cc8a4a 539 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6f7eaa47 540 size_t offset = 0, noffset;
13cc8a4a 541 struct sta_info *sta, *ap_sta;
fb28ec0c 542 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
6f7eaa47
AN
543 u8 *pos;
544
0fabfaaf 545 mutex_lock(&local->sta_mtx);
6f7eaa47
AN
546
547 sta = sta_info_get(sdata, peer);
13cc8a4a
AN
548 ap_sta = sta_info_get(sdata, ifmgd->bssid);
549 if (WARN_ON_ONCE(!sta || !ap_sta)) {
0fabfaaf 550 mutex_unlock(&local->sta_mtx);
6f7eaa47
AN
551 return;
552 }
553
0fabfaaf
AN
554 sta->tdls_chandef = sdata->vif.bss_conf.chandef;
555
6f7eaa47
AN
556 /* add any custom IEs that go before the QoS IE */
557 if (extra_ies_len) {
558 static const u8 before_qos[] = {
559 WLAN_EID_RSN,
560 };
561 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
562 before_qos,
563 ARRAY_SIZE(before_qos),
564 offset);
565 pos = skb_put(skb, noffset - offset);
566 memcpy(pos, extra_ies + offset, noffset - offset);
567 offset = noffset;
568 }
569
570 /* add the QoS param IE if both the peer and we support it */
a74a8c84 571 if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
6f7eaa47
AN
572 ieee80211_tdls_add_wmm_param_ie(sdata, skb);
573
13cc8a4a
AN
574 /* add any custom IEs that go before HT operation */
575 if (extra_ies_len) {
576 static const u8 before_ht_op[] = {
577 WLAN_EID_RSN,
578 WLAN_EID_QOS_CAPA,
579 WLAN_EID_FAST_BSS_TRANSITION,
580 WLAN_EID_TIMEOUT_INTERVAL,
581 };
582 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
583 before_ht_op,
584 ARRAY_SIZE(before_ht_op),
585 offset);
586 pos = skb_put(skb, noffset - offset);
587 memcpy(pos, extra_ies + offset, noffset - offset);
588 offset = noffset;
589 }
590
591 /* if HT support is only added in TDLS, we need an HT-operation IE */
592 if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
890b7878
AN
593 pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
594 /* send an empty HT operation IE */
595 ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
596 &sdata->vif.bss_conf.chandef, 0);
13cc8a4a
AN
597 }
598
fb28ec0c
AN
599 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
600
601 /* only include VHT-operation if not on the 2.4GHz band */
890b7878 602 if (band != IEEE80211_BAND_2GHZ && sta->sta.vht_cap.vht_supported) {
0fabfaaf
AN
603 /*
604 * if both peers support WIDER_BW, we can expand the chandef to
605 * a wider compatible one, up to 80MHz
606 */
607 if (test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
608 ieee80211_tdls_chandef_vht_upgrade(sdata, sta);
609
890b7878
AN
610 pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
611 ieee80211_ie_build_vht_oper(pos, &sta->sta.vht_cap,
0fabfaaf 612 &sta->tdls_chandef);
fb28ec0c
AN
613 }
614
0fabfaaf 615 mutex_unlock(&local->sta_mtx);
13cc8a4a 616
6f7eaa47
AN
617 /* add any remaining IEs */
618 if (extra_ies_len) {
619 noffset = extra_ies_len;
620 pos = skb_put(skb, noffset - offset);
621 memcpy(pos, extra_ies + offset, noffset - offset);
622 }
6f7eaa47
AN
623}
624
a7a6bdd0
AN
625static void
626ieee80211_tdls_add_chan_switch_req_ies(struct ieee80211_sub_if_data *sdata,
627 struct sk_buff *skb, const u8 *peer,
628 bool initiator, const u8 *extra_ies,
629 size_t extra_ies_len, u8 oper_class,
630 struct cfg80211_chan_def *chandef)
631{
632 struct ieee80211_tdls_data *tf;
633 size_t offset = 0, noffset;
634 u8 *pos;
635
636 if (WARN_ON_ONCE(!chandef))
637 return;
638
639 tf = (void *)skb->data;
640 tf->u.chan_switch_req.target_channel =
641 ieee80211_frequency_to_channel(chandef->chan->center_freq);
642 tf->u.chan_switch_req.oper_class = oper_class;
643
644 if (extra_ies_len) {
645 static const u8 before_lnkie[] = {
646 WLAN_EID_SECONDARY_CHANNEL_OFFSET,
647 };
648 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
649 before_lnkie,
650 ARRAY_SIZE(before_lnkie),
651 offset);
652 pos = skb_put(skb, noffset - offset);
653 memcpy(pos, extra_ies + offset, noffset - offset);
654 offset = noffset;
655 }
656
657 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
658
659 /* add any remaining IEs */
660 if (extra_ies_len) {
661 noffset = extra_ies_len;
662 pos = skb_put(skb, noffset - offset);
663 memcpy(pos, extra_ies + offset, noffset - offset);
664 }
665}
666
8a4d32f3
AN
667static void
668ieee80211_tdls_add_chan_switch_resp_ies(struct ieee80211_sub_if_data *sdata,
669 struct sk_buff *skb, const u8 *peer,
670 u16 status_code, bool initiator,
671 const u8 *extra_ies,
672 size_t extra_ies_len)
673{
674 if (status_code == 0)
675 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
676
677 if (extra_ies_len)
678 memcpy(skb_put(skb, extra_ies_len), extra_ies, extra_ies_len);
679}
680
46792a2d
AN
681static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
682 struct sk_buff *skb, const u8 *peer,
1606ef4a
AN
683 u8 action_code, u16 status_code,
684 bool initiator, const u8 *extra_ies,
c2733905
AN
685 size_t extra_ies_len, u8 oper_class,
686 struct cfg80211_chan_def *chandef)
46792a2d 687{
46792a2d
AN
688 switch (action_code) {
689 case WLAN_TDLS_SETUP_REQUEST:
690 case WLAN_TDLS_SETUP_RESPONSE:
691 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
1606ef4a
AN
692 if (status_code == 0)
693 ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
694 action_code,
695 initiator,
696 extra_ies,
697 extra_ies_len);
46792a2d
AN
698 break;
699 case WLAN_TDLS_SETUP_CONFIRM:
6f7eaa47
AN
700 if (status_code == 0)
701 ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
702 initiator, extra_ies,
703 extra_ies_len);
704 break;
46792a2d
AN
705 case WLAN_TDLS_TEARDOWN:
706 case WLAN_TDLS_DISCOVERY_REQUEST:
f09a87d2
AN
707 if (extra_ies_len)
708 memcpy(skb_put(skb, extra_ies_len), extra_ies,
709 extra_ies_len);
1606ef4a
AN
710 if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
711 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
46792a2d 712 break;
a7a6bdd0
AN
713 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
714 ieee80211_tdls_add_chan_switch_req_ies(sdata, skb, peer,
715 initiator, extra_ies,
716 extra_ies_len,
717 oper_class, chandef);
718 break;
8a4d32f3
AN
719 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
720 ieee80211_tdls_add_chan_switch_resp_ies(sdata, skb, peer,
721 status_code,
722 initiator, extra_ies,
723 extra_ies_len);
724 break;
46792a2d
AN
725 }
726
46792a2d
AN
727}
728
95224fe8
AN
729static int
730ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
3b3a0162 731 const u8 *peer, u8 action_code, u8 dialog_token,
95224fe8
AN
732 u16 status_code, struct sk_buff *skb)
733{
734 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
95224fe8
AN
735 struct ieee80211_tdls_data *tf;
736
737 tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
738
739 memcpy(tf->da, peer, ETH_ALEN);
740 memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
741 tf->ether_type = cpu_to_be16(ETH_P_TDLS);
742 tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
743
59cd85cb
AN
744 /* network header is after the ethernet header */
745 skb_set_network_header(skb, ETH_HLEN);
746
95224fe8
AN
747 switch (action_code) {
748 case WLAN_TDLS_SETUP_REQUEST:
749 tf->category = WLAN_CATEGORY_TDLS;
750 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
751
752 skb_put(skb, sizeof(tf->u.setup_req));
753 tf->u.setup_req.dialog_token = dialog_token;
754 tf->u.setup_req.capability =
dd8c0b03
AN
755 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
756 status_code));
95224fe8
AN
757 break;
758 case WLAN_TDLS_SETUP_RESPONSE:
759 tf->category = WLAN_CATEGORY_TDLS;
760 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
761
762 skb_put(skb, sizeof(tf->u.setup_resp));
763 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
764 tf->u.setup_resp.dialog_token = dialog_token;
765 tf->u.setup_resp.capability =
dd8c0b03
AN
766 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
767 status_code));
95224fe8
AN
768 break;
769 case WLAN_TDLS_SETUP_CONFIRM:
770 tf->category = WLAN_CATEGORY_TDLS;
771 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
772
773 skb_put(skb, sizeof(tf->u.setup_cfm));
774 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
775 tf->u.setup_cfm.dialog_token = dialog_token;
776 break;
777 case WLAN_TDLS_TEARDOWN:
778 tf->category = WLAN_CATEGORY_TDLS;
779 tf->action_code = WLAN_TDLS_TEARDOWN;
780
781 skb_put(skb, sizeof(tf->u.teardown));
782 tf->u.teardown.reason_code = cpu_to_le16(status_code);
783 break;
784 case WLAN_TDLS_DISCOVERY_REQUEST:
785 tf->category = WLAN_CATEGORY_TDLS;
786 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
787
788 skb_put(skb, sizeof(tf->u.discover_req));
789 tf->u.discover_req.dialog_token = dialog_token;
790 break;
a7a6bdd0
AN
791 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
792 tf->category = WLAN_CATEGORY_TDLS;
793 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
794
795 skb_put(skb, sizeof(tf->u.chan_switch_req));
796 break;
8a4d32f3
AN
797 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
798 tf->category = WLAN_CATEGORY_TDLS;
799 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
800
801 skb_put(skb, sizeof(tf->u.chan_switch_resp));
802 tf->u.chan_switch_resp.status_code = cpu_to_le16(status_code);
803 break;
95224fe8
AN
804 default:
805 return -EINVAL;
806 }
807
808 return 0;
809}
810
811static int
812ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
3b3a0162 813 const u8 *peer, u8 action_code, u8 dialog_token,
95224fe8
AN
814 u16 status_code, struct sk_buff *skb)
815{
816 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
95224fe8
AN
817 struct ieee80211_mgmt *mgmt;
818
819 mgmt = (void *)skb_put(skb, 24);
820 memset(mgmt, 0, 24);
821 memcpy(mgmt->da, peer, ETH_ALEN);
822 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
823 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
824
825 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
826 IEEE80211_STYPE_ACTION);
827
828 switch (action_code) {
829 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
830 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
831 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
832 mgmt->u.action.u.tdls_discover_resp.action_code =
833 WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
834 mgmt->u.action.u.tdls_discover_resp.dialog_token =
835 dialog_token;
836 mgmt->u.action.u.tdls_discover_resp.capability =
dd8c0b03
AN
837 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
838 status_code));
95224fe8
AN
839 break;
840 default:
841 return -EINVAL;
842 }
843
844 return 0;
845}
846
c2733905
AN
847static struct sk_buff *
848ieee80211_tdls_build_mgmt_packet_data(struct ieee80211_sub_if_data *sdata,
849 const u8 *peer, u8 action_code,
850 u8 dialog_token, u16 status_code,
851 bool initiator, const u8 *extra_ies,
852 size_t extra_ies_len, u8 oper_class,
853 struct cfg80211_chan_def *chandef)
95224fe8 854{
95224fe8 855 struct ieee80211_local *local = sdata->local;
c2733905 856 struct sk_buff *skb;
95224fe8
AN
857 int ret;
858
c2733905 859 skb = netdev_alloc_skb(sdata->dev,
1277b4a9
LK
860 local->hw.extra_tx_headroom +
861 max(sizeof(struct ieee80211_mgmt),
862 sizeof(struct ieee80211_tdls_data)) +
863 50 + /* supported rates */
b98fb44f 864 10 + /* ext capab */
1277b4a9
LK
865 26 + /* max(WMM-info, WMM-param) */
866 2 + max(sizeof(struct ieee80211_ht_cap),
867 sizeof(struct ieee80211_ht_operation)) +
fb28ec0c
AN
868 2 + max(sizeof(struct ieee80211_vht_cap),
869 sizeof(struct ieee80211_vht_operation)) +
f0d29cb9 870 50 + /* supported channels */
2cedd879 871 3 + /* 40/20 BSS coex */
fb28ec0c 872 4 + /* AID */
a38700dd 873 4 + /* oper classes */
1277b4a9
LK
874 extra_ies_len +
875 sizeof(struct ieee80211_tdls_lnkie));
95224fe8 876 if (!skb)
c2733905 877 return NULL;
95224fe8
AN
878
879 skb_reserve(skb, local->hw.extra_tx_headroom);
880
881 switch (action_code) {
882 case WLAN_TDLS_SETUP_REQUEST:
883 case WLAN_TDLS_SETUP_RESPONSE:
884 case WLAN_TDLS_SETUP_CONFIRM:
885 case WLAN_TDLS_TEARDOWN:
886 case WLAN_TDLS_DISCOVERY_REQUEST:
a7a6bdd0 887 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
8a4d32f3 888 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
c2733905
AN
889 ret = ieee80211_prep_tdls_encap_data(local->hw.wiphy,
890 sdata->dev, peer,
95224fe8
AN
891 action_code, dialog_token,
892 status_code, skb);
95224fe8
AN
893 break;
894 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
c2733905
AN
895 ret = ieee80211_prep_tdls_direct(local->hw.wiphy, sdata->dev,
896 peer, action_code,
95224fe8
AN
897 dialog_token, status_code,
898 skb);
95224fe8
AN
899 break;
900 default:
901 ret = -ENOTSUPP;
902 break;
903 }
904
905 if (ret < 0)
906 goto fail;
907
c2733905
AN
908 ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
909 initiator, extra_ies, extra_ies_len, oper_class,
910 chandef);
911 return skb;
912
913fail:
914 dev_kfree_skb(skb);
915 return NULL;
916}
917
918static int
919ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
920 const u8 *peer, u8 action_code, u8 dialog_token,
921 u16 status_code, u32 peer_capability,
922 bool initiator, const u8 *extra_ies,
923 size_t extra_ies_len, u8 oper_class,
924 struct cfg80211_chan_def *chandef)
925{
926 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
927 struct sk_buff *skb = NULL;
928 struct sta_info *sta;
929 u32 flags = 0;
930 int ret = 0;
931
626911cc
AN
932 rcu_read_lock();
933 sta = sta_info_get(sdata, peer);
934
935 /* infer the initiator if we can, to support old userspace */
95224fe8
AN
936 switch (action_code) {
937 case WLAN_TDLS_SETUP_REQUEST:
8b94148c 938 if (sta) {
626911cc 939 set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
8b94148c
AN
940 sta->sta.tdls_initiator = false;
941 }
626911cc 942 /* fall-through */
95224fe8 943 case WLAN_TDLS_SETUP_CONFIRM:
95224fe8 944 case WLAN_TDLS_DISCOVERY_REQUEST:
626911cc 945 initiator = true;
95224fe8
AN
946 break;
947 case WLAN_TDLS_SETUP_RESPONSE:
626911cc
AN
948 /*
949 * In some testing scenarios, we send a request and response.
950 * Make the last packet sent take effect for the initiator
951 * value.
952 */
8b94148c 953 if (sta) {
626911cc 954 clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
8b94148c
AN
955 sta->sta.tdls_initiator = true;
956 }
626911cc 957 /* fall-through */
95224fe8 958 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
626911cc 959 initiator = false;
2fb6b9b8
AN
960 break;
961 case WLAN_TDLS_TEARDOWN:
a7a6bdd0 962 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
8a4d32f3 963 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
2fb6b9b8 964 /* any value is ok */
95224fe8
AN
965 break;
966 default:
967 ret = -ENOTSUPP;
626911cc 968 break;
95224fe8
AN
969 }
970
46792a2d
AN
971 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
972 initiator = true;
2fb6b9b8 973
626911cc
AN
974 rcu_read_unlock();
975 if (ret < 0)
976 goto fail;
977
c2733905
AN
978 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, peer, action_code,
979 dialog_token, status_code,
980 initiator, extra_ies,
981 extra_ies_len, oper_class,
982 chandef);
983 if (!skb) {
984 ret = -EINVAL;
985 goto fail;
986 }
987
988 if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
95224fe8
AN
989 ieee80211_tx_skb(sdata, skb);
990 return 0;
991 }
992
993 /*
994 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
995 * we should default to AC_VI.
996 */
997 switch (action_code) {
998 case WLAN_TDLS_SETUP_REQUEST:
999 case WLAN_TDLS_SETUP_RESPONSE:
1000 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
1001 skb->priority = 2;
1002 break;
1003 default:
1004 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
1005 skb->priority = 5;
1006 break;
1007 }
1008
1277b4a9
LK
1009 /*
1010 * Set the WLAN_TDLS_TEARDOWN flag to indicate a teardown in progress.
1011 * Later, if no ACK is returned from peer, we will re-send the teardown
1012 * packet through the AP.
1013 */
1014 if ((action_code == WLAN_TDLS_TEARDOWN) &&
30686bf7 1015 ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
1277b4a9
LK
1016 bool try_resend; /* Should we keep skb for possible resend */
1017
1018 /* If not sending directly to peer - no point in keeping skb */
1019 rcu_read_lock();
1020 sta = sta_info_get(sdata, peer);
1021 try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1022 rcu_read_unlock();
1023
1024 spin_lock_bh(&sdata->u.mgd.teardown_lock);
1025 if (try_resend && !sdata->u.mgd.teardown_skb) {
1026 /* Mark it as requiring TX status callback */
1027 flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
1028 IEEE80211_TX_INTFL_MLME_CONN_TX;
1029
1030 /*
1031 * skb is copied since mac80211 will later set
1032 * properties that might not be the same as the AP,
1033 * such as encryption, QoS, addresses, etc.
1034 *
1035 * No problem if skb_copy() fails, so no need to check.
1036 */
1037 sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
1038 sdata->u.mgd.orig_teardown_skb = skb;
1039 }
1040 spin_unlock_bh(&sdata->u.mgd.teardown_lock);
1041 }
1042
95224fe8
AN
1043 /* disable bottom halves when entering the Tx path */
1044 local_bh_disable();
1277b4a9 1045 __ieee80211_subif_start_xmit(skb, dev, flags);
95224fe8
AN
1046 local_bh_enable();
1047
1048 return ret;
1049
1050fail:
1051 dev_kfree_skb(skb);
1052 return ret;
1053}
1054
191dd469
AN
1055static int
1056ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
1057 const u8 *peer, u8 action_code, u8 dialog_token,
1058 u16 status_code, u32 peer_capability, bool initiator,
1059 const u8 *extra_ies, size_t extra_ies_len)
17e6a59a
AN
1060{
1061 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1062 struct ieee80211_local *local = sdata->local;
d51c2ea3 1063 enum ieee80211_smps_mode smps_mode = sdata->u.mgd.driver_smps_mode;
17e6a59a
AN
1064 int ret;
1065
d51c2ea3
AN
1066 /* don't support setup with forced SMPS mode that's not off */
1067 if (smps_mode != IEEE80211_SMPS_AUTOMATIC &&
1068 smps_mode != IEEE80211_SMPS_OFF) {
1069 tdls_dbg(sdata, "Aborting TDLS setup due to SMPS mode %d\n",
1070 smps_mode);
1071 return -ENOTSUPP;
1072 }
1073
17e6a59a
AN
1074 mutex_lock(&local->mtx);
1075
1076 /* we don't support concurrent TDLS peer setups */
81dd2b88
AN
1077 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
1078 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
17e6a59a 1079 ret = -EBUSY;
ae2e9fba 1080 goto out_unlock;
17e6a59a
AN
1081 }
1082
7adc3e46
AN
1083 /*
1084 * make sure we have a STA representing the peer so we drop or buffer
1085 * non-TDLS-setup frames to the peer. We can't send other packets
6ae32e5d
AN
1086 * during setup through the AP path.
1087 * Allow error packets to be sent - sometimes we don't even add a STA
1088 * before failing the setup.
7adc3e46 1089 */
6ae32e5d
AN
1090 if (status_code == 0) {
1091 rcu_read_lock();
1092 if (!sta_info_get(sdata, peer)) {
1093 rcu_read_unlock();
1094 ret = -ENOLINK;
ae2e9fba 1095 goto out_unlock;
6ae32e5d 1096 }
7adc3e46 1097 rcu_read_unlock();
7adc3e46 1098 }
7adc3e46 1099
3b24f4c6 1100 ieee80211_flush_queues(local, sdata, false);
ae2e9fba
AN
1101 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
1102 mutex_unlock(&local->mtx);
db67d661 1103
ae2e9fba 1104 /* we cannot take the mutex while preparing the setup packet */
17e6a59a
AN
1105 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1106 dialog_token, status_code,
2fb6b9b8 1107 peer_capability, initiator,
c2733905
AN
1108 extra_ies, extra_ies_len, 0,
1109 NULL);
ae2e9fba
AN
1110 if (ret < 0) {
1111 mutex_lock(&local->mtx);
1112 eth_zero_addr(sdata->u.mgd.tdls_peer);
1113 mutex_unlock(&local->mtx);
1114 return ret;
1115 }
17e6a59a 1116
191dd469 1117 ieee80211_queue_delayed_work(&sdata->local->hw,
81dd2b88 1118 &sdata->u.mgd.tdls_peer_del_work,
191dd469 1119 TDLS_PEER_SETUP_TIMEOUT);
ae2e9fba 1120 return 0;
17e6a59a 1121
ae2e9fba 1122out_unlock:
17e6a59a 1123 mutex_unlock(&local->mtx);
191dd469
AN
1124 return ret;
1125}
1126
db67d661
AN
1127static int
1128ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
1129 const u8 *peer, u8 action_code, u8 dialog_token,
1130 u16 status_code, u32 peer_capability,
1131 bool initiator, const u8 *extra_ies,
1132 size_t extra_ies_len)
1133{
1134 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1135 struct ieee80211_local *local = sdata->local;
1136 struct sta_info *sta;
1137 int ret;
1138
1139 /*
1140 * No packets can be transmitted to the peer via the AP during setup -
1141 * the STA is set as a TDLS peer, but is not authorized.
1142 * During teardown, we prevent direct transmissions by stopping the
1143 * queues and flushing all direct packets.
1144 */
1145 ieee80211_stop_vif_queues(local, sdata,
1146 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
3b24f4c6 1147 ieee80211_flush_queues(local, sdata, false);
db67d661
AN
1148
1149 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1150 dialog_token, status_code,
1151 peer_capability, initiator,
c2733905
AN
1152 extra_ies, extra_ies_len, 0,
1153 NULL);
db67d661
AN
1154 if (ret < 0)
1155 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
1156 ret);
1157
1158 /*
1159 * Remove the STA AUTH flag to force further traffic through the AP. If
1160 * the STA was unreachable, it was already removed.
1161 */
1162 rcu_read_lock();
1163 sta = sta_info_get(sdata, peer);
1164 if (sta)
1165 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1166 rcu_read_unlock();
1167
1168 ieee80211_wake_vif_queues(local, sdata,
1169 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1170
1171 return 0;
1172}
1173
191dd469
AN
1174int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
1175 const u8 *peer, u8 action_code, u8 dialog_token,
1176 u16 status_code, u32 peer_capability,
1177 bool initiator, const u8 *extra_ies,
1178 size_t extra_ies_len)
1179{
1180 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1181 int ret;
1182
1183 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1184 return -ENOTSUPP;
1185
1186 /* make sure we are in managed mode, and associated */
1187 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1188 !sdata->u.mgd.associated)
1189 return -EINVAL;
1190
1191 switch (action_code) {
1192 case WLAN_TDLS_SETUP_REQUEST:
1193 case WLAN_TDLS_SETUP_RESPONSE:
1194 ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
1195 dialog_token, status_code,
1196 peer_capability, initiator,
1197 extra_ies, extra_ies_len);
1198 break;
1199 case WLAN_TDLS_TEARDOWN:
db67d661
AN
1200 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
1201 action_code, dialog_token,
1202 status_code,
1203 peer_capability, initiator,
1204 extra_ies, extra_ies_len);
1205 break;
191dd469 1206 case WLAN_TDLS_DISCOVERY_REQUEST:
ee10f2c7
AN
1207 /*
1208 * Protect the discovery so we can hear the TDLS discovery
1209 * response frame. It is transmitted directly and not buffered
1210 * by the AP.
1211 */
1212 drv_mgd_protect_tdls_discover(sdata->local, sdata);
1213 /* fall-through */
1214 case WLAN_TDLS_SETUP_CONFIRM:
191dd469
AN
1215 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
1216 /* no special handling */
1217 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1218 action_code,
1219 dialog_token,
1220 status_code,
1221 peer_capability,
1222 initiator, extra_ies,
c2733905 1223 extra_ies_len, 0, NULL);
191dd469
AN
1224 break;
1225 default:
1226 ret = -EOPNOTSUPP;
1227 break;
1228 }
17e6a59a
AN
1229
1230 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
1231 action_code, peer, ret);
1232 return ret;
1233}
1234
0fabfaaf
AN
1235static void iee80211_tdls_recalc_chanctx(struct ieee80211_sub_if_data *sdata)
1236{
1237 struct ieee80211_local *local = sdata->local;
1238 struct ieee80211_chanctx_conf *conf;
1239 struct ieee80211_chanctx *ctx;
1240
1241 mutex_lock(&local->chanctx_mtx);
1242 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1243 lockdep_is_held(&local->chanctx_mtx));
1244 if (conf) {
1245 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1246 ieee80211_recalc_chanctx_chantype(local, ctx);
1247 }
1248 mutex_unlock(&local->chanctx_mtx);
1249}
1250
95224fe8 1251int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
3b3a0162 1252 const u8 *peer, enum nl80211_tdls_operation oper)
95224fe8
AN
1253{
1254 struct sta_info *sta;
1255 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
17e6a59a
AN
1256 struct ieee80211_local *local = sdata->local;
1257 int ret;
95224fe8
AN
1258
1259 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1260 return -ENOTSUPP;
1261
1262 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1263 return -EINVAL;
1264
17e6a59a
AN
1265 switch (oper) {
1266 case NL80211_TDLS_ENABLE_LINK:
1267 case NL80211_TDLS_DISABLE_LINK:
1268 break;
1269 case NL80211_TDLS_TEARDOWN:
1270 case NL80211_TDLS_SETUP:
1271 case NL80211_TDLS_DISCOVERY_REQ:
1272 /* We don't support in-driver setup/teardown/discovery */
1273 return -ENOTSUPP;
1274 }
1275
1276 mutex_lock(&local->mtx);
95224fe8
AN
1277 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
1278
1279 switch (oper) {
1280 case NL80211_TDLS_ENABLE_LINK:
c5a71688
AN
1281 if (sdata->vif.csa_active) {
1282 tdls_dbg(sdata, "TDLS: disallow link during CSA\n");
1283 ret = -EBUSY;
1284 break;
1285 }
1286
0fabfaaf
AN
1287 iee80211_tdls_recalc_chanctx(sdata);
1288
95224fe8
AN
1289 rcu_read_lock();
1290 sta = sta_info_get(sdata, peer);
1291 if (!sta) {
1292 rcu_read_unlock();
17e6a59a
AN
1293 ret = -ENOLINK;
1294 break;
95224fe8
AN
1295 }
1296
1297 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1298 rcu_read_unlock();
17e6a59a 1299
81dd2b88
AN
1300 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
1301 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
17e6a59a 1302 ret = 0;
95224fe8
AN
1303 break;
1304 case NL80211_TDLS_DISABLE_LINK:
bb3f8486
LK
1305 /*
1306 * The teardown message in ieee80211_tdls_mgmt_teardown() was
1307 * created while the queues were stopped, so it might still be
1308 * pending. Before flushing the queues we need to be sure the
1309 * message is handled by the tasklet handling pending messages,
1310 * otherwise we might start destroying the station before
1311 * sending the teardown packet.
1312 * Note that this only forces the tasklet to flush pendings -
1313 * not to stop the tasklet from rescheduling itself.
1314 */
1315 tasklet_kill(&local->tx_pending_tasklet);
db67d661 1316 /* flush a potentially queued teardown packet */
3b24f4c6 1317 ieee80211_flush_queues(local, sdata, false);
db67d661 1318
17e6a59a 1319 ret = sta_info_destroy_addr(sdata, peer);
0fabfaaf 1320 iee80211_tdls_recalc_chanctx(sdata);
17e6a59a 1321 break;
95224fe8 1322 default:
17e6a59a
AN
1323 ret = -ENOTSUPP;
1324 break;
95224fe8
AN
1325 }
1326
81dd2b88
AN
1327 if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1328 cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
1329 eth_zero_addr(sdata->u.mgd.tdls_peer);
17e6a59a
AN
1330 }
1331
d51c2ea3
AN
1332 if (ret == 0)
1333 ieee80211_queue_work(&sdata->local->hw,
1334 &sdata->u.mgd.request_smps_work);
1335
17e6a59a
AN
1336 mutex_unlock(&local->mtx);
1337 return ret;
95224fe8 1338}
c887f0d3
AN
1339
1340void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
1341 enum nl80211_tdls_operation oper,
1342 u16 reason_code, gfp_t gfp)
1343{
1344 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1345
1346 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
1347 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
1348 oper);
1349 return;
1350 }
1351
1352 cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
1353}
1354EXPORT_SYMBOL(ieee80211_tdls_oper_request);
a7a6bdd0
AN
1355
1356static void
1357iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
1358{
1359 struct ieee80211_ch_switch_timing *ch_sw;
1360
1361 *buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
1362 *buf++ = sizeof(struct ieee80211_ch_switch_timing);
1363
1364 ch_sw = (void *)buf;
1365 ch_sw->switch_time = cpu_to_le16(switch_time);
1366 ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
1367}
1368
1369/* find switch timing IE in SKB ready for Tx */
1370static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
1371{
1372 struct ieee80211_tdls_data *tf;
1373 const u8 *ie_start;
1374
1375 /*
1376 * Get the offset for the new location of the switch timing IE.
1377 * The SKB network header will now point to the "payload_type"
1378 * element of the TDLS data frame struct.
1379 */
1380 tf = container_of(skb->data + skb_network_offset(skb),
1381 struct ieee80211_tdls_data, payload_type);
1382 ie_start = tf->u.chan_switch_req.variable;
1383 return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
1384 skb->len - (ie_start - skb->data));
1385}
1386
1387static struct sk_buff *
1388ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
1389 struct cfg80211_chan_def *chandef,
1390 u32 *ch_sw_tm_ie_offset)
1391{
1392 struct ieee80211_sub_if_data *sdata = sta->sdata;
1393 u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
1394 2 + sizeof(struct ieee80211_ch_switch_timing)];
1395 int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
1396 u8 *pos = extra_ies;
1397 struct sk_buff *skb;
1398
1399 /*
1400 * if chandef points to a wide channel add a Secondary-Channel
1401 * Offset information element
1402 */
1403 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1404 struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
1405 bool ht40plus;
1406
1407 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
1408 *pos++ = sizeof(*sec_chan_ie);
1409 sec_chan_ie = (void *)pos;
1410
1411 ht40plus = cfg80211_get_chandef_type(chandef) ==
1412 NL80211_CHAN_HT40PLUS;
1413 sec_chan_ie->sec_chan_offs = ht40plus ?
1414 IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
1415 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1416 pos += sizeof(*sec_chan_ie);
1417
1418 extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1419 }
1420
1421 /* just set the values to 0, this is a template */
1422 iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
1423
1424 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1425 WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
1426 0, 0, !sta->sta.tdls_initiator,
1427 extra_ies, extra_ies_len,
1428 oper_class, chandef);
1429 if (!skb)
1430 return NULL;
1431
1432 skb = ieee80211_build_data_template(sdata, skb, 0);
1433 if (IS_ERR(skb)) {
1434 tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
1435 return NULL;
1436 }
1437
1438 if (ch_sw_tm_ie_offset) {
1439 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1440
1441 if (!tm_ie) {
1442 tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
1443 dev_kfree_skb_any(skb);
1444 return NULL;
1445 }
1446
1447 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1448 }
1449
1450 tdls_dbg(sdata,
1451 "TDLS channel switch request template for %pM ch %d width %d\n",
1452 sta->sta.addr, chandef->chan->center_freq, chandef->width);
1453 return skb;
1454}
1455
1456int
1457ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
1458 const u8 *addr, u8 oper_class,
1459 struct cfg80211_chan_def *chandef)
1460{
1461 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1462 struct ieee80211_local *local = sdata->local;
1463 struct sta_info *sta;
1464 struct sk_buff *skb = NULL;
1465 u32 ch_sw_tm_ie;
1466 int ret;
1467
1468 mutex_lock(&local->sta_mtx);
1469 sta = sta_info_get(sdata, addr);
1470 if (!sta) {
1471 tdls_dbg(sdata,
1472 "Invalid TDLS peer %pM for channel switch request\n",
1473 addr);
1474 ret = -ENOENT;
1475 goto out;
1476 }
1477
1478 if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
1479 tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
1480 addr);
1481 ret = -ENOTSUPP;
1482 goto out;
1483 }
1484
1485 skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
1486 &ch_sw_tm_ie);
1487 if (!skb) {
1488 ret = -ENOENT;
1489 goto out;
1490 }
1491
1492 ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
1493 chandef, skb, ch_sw_tm_ie);
1494 if (!ret)
1495 set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1496
1497out:
1498 mutex_unlock(&local->sta_mtx);
1499 dev_kfree_skb_any(skb);
1500 return ret;
1501}
1502
1503void
1504ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
1505 struct net_device *dev,
1506 const u8 *addr)
1507{
1508 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1509 struct ieee80211_local *local = sdata->local;
1510 struct sta_info *sta;
1511
1512 mutex_lock(&local->sta_mtx);
1513 sta = sta_info_get(sdata, addr);
1514 if (!sta) {
1515 tdls_dbg(sdata,
1516 "Invalid TDLS peer %pM for channel switch cancel\n",
1517 addr);
1518 goto out;
1519 }
1520
1521 if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1522 tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
1523 addr);
1524 goto out;
1525 }
1526
1527 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1528 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1529
1530out:
1531 mutex_unlock(&local->sta_mtx);
1532}
8a4d32f3
AN
1533
1534static struct sk_buff *
1535ieee80211_tdls_ch_sw_resp_tmpl_get(struct sta_info *sta,
1536 u32 *ch_sw_tm_ie_offset)
1537{
1538 struct ieee80211_sub_if_data *sdata = sta->sdata;
1539 struct sk_buff *skb;
1540 u8 extra_ies[2 + sizeof(struct ieee80211_ch_switch_timing)];
1541
1542 /* initial timing are always zero in the template */
1543 iee80211_tdls_add_ch_switch_timing(extra_ies, 0, 0);
1544
1545 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1546 WLAN_TDLS_CHANNEL_SWITCH_RESPONSE,
1547 0, 0, !sta->sta.tdls_initiator,
1548 extra_ies, sizeof(extra_ies), 0, NULL);
1549 if (!skb)
1550 return NULL;
1551
1552 skb = ieee80211_build_data_template(sdata, skb, 0);
1553 if (IS_ERR(skb)) {
1554 tdls_dbg(sdata,
1555 "Failed building TDLS channel switch resp frame\n");
1556 return NULL;
1557 }
1558
1559 if (ch_sw_tm_ie_offset) {
1560 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1561
1562 if (!tm_ie) {
1563 tdls_dbg(sdata,
1564 "No switch timing IE in TDLS switch resp\n");
1565 dev_kfree_skb_any(skb);
1566 return NULL;
1567 }
1568
1569 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1570 }
1571
1572 tdls_dbg(sdata, "TDLS get channel switch response template for %pM\n",
1573 sta->sta.addr);
1574 return skb;
1575}
1576
1577static int
1578ieee80211_process_tdls_channel_switch_resp(struct ieee80211_sub_if_data *sdata,
1579 struct sk_buff *skb)
1580{
1581 struct ieee80211_local *local = sdata->local;
1582 struct ieee802_11_elems elems;
1583 struct sta_info *sta;
1584 struct ieee80211_tdls_data *tf = (void *)skb->data;
1585 bool local_initiator;
1586 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1587 int baselen = offsetof(typeof(*tf), u.chan_switch_resp.variable);
1588 struct ieee80211_tdls_ch_sw_params params = {};
1589 int ret;
1590
1591 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
1592 params.timestamp = rx_status->device_timestamp;
1593
1594 if (skb->len < baselen) {
1595 tdls_dbg(sdata, "TDLS channel switch resp too short: %d\n",
1596 skb->len);
1597 return -EINVAL;
1598 }
1599
1600 mutex_lock(&local->sta_mtx);
1601 sta = sta_info_get(sdata, tf->sa);
1602 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1603 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1604 tf->sa);
1605 ret = -EINVAL;
1606 goto out;
1607 }
1608
1609 params.sta = &sta->sta;
1610 params.status = le16_to_cpu(tf->u.chan_switch_resp.status_code);
1611 if (params.status != 0) {
1612 ret = 0;
1613 goto call_drv;
1614 }
1615
1616 ieee802_11_parse_elems(tf->u.chan_switch_resp.variable,
1617 skb->len - baselen, false, &elems);
1618 if (elems.parse_error) {
1619 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch resp\n");
1620 ret = -EINVAL;
1621 goto out;
1622 }
1623
1624 if (!elems.ch_sw_timing || !elems.lnk_id) {
1625 tdls_dbg(sdata, "TDLS channel switch resp - missing IEs\n");
1626 ret = -EINVAL;
1627 goto out;
1628 }
1629
1630 /* validate the initiator is set correctly */
1631 local_initiator =
1632 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1633 if (local_initiator == sta->sta.tdls_initiator) {
1634 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1635 ret = -EINVAL;
1636 goto out;
1637 }
1638
1639 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1640 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1641
1642 params.tmpl_skb =
1643 ieee80211_tdls_ch_sw_resp_tmpl_get(sta, &params.ch_sw_tm_ie);
1644 if (!params.tmpl_skb) {
1645 ret = -ENOENT;
1646 goto out;
1647 }
1648
1649call_drv:
1650 drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1651
1652 tdls_dbg(sdata,
1653 "TDLS channel switch response received from %pM status %d\n",
1654 tf->sa, params.status);
1655
1656out:
1657 mutex_unlock(&local->sta_mtx);
1658 dev_kfree_skb_any(params.tmpl_skb);
1659 return ret;
1660}
1661
1662static int
1663ieee80211_process_tdls_channel_switch_req(struct ieee80211_sub_if_data *sdata,
1664 struct sk_buff *skb)
1665{
1666 struct ieee80211_local *local = sdata->local;
1667 struct ieee802_11_elems elems;
1668 struct cfg80211_chan_def chandef;
1669 struct ieee80211_channel *chan;
1670 enum nl80211_channel_type chan_type;
1671 int freq;
1672 u8 target_channel, oper_class;
1673 bool local_initiator;
1674 struct sta_info *sta;
1675 enum ieee80211_band band;
1676 struct ieee80211_tdls_data *tf = (void *)skb->data;
1677 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1678 int baselen = offsetof(typeof(*tf), u.chan_switch_req.variable);
1679 struct ieee80211_tdls_ch_sw_params params = {};
1680 int ret = 0;
1681
1682 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
1683 params.timestamp = rx_status->device_timestamp;
1684
1685 if (skb->len < baselen) {
1686 tdls_dbg(sdata, "TDLS channel switch req too short: %d\n",
1687 skb->len);
1688 return -EINVAL;
1689 }
1690
1691 target_channel = tf->u.chan_switch_req.target_channel;
1692 oper_class = tf->u.chan_switch_req.oper_class;
1693
1694 /*
1695 * We can't easily infer the channel band. The operating class is
1696 * ambiguous - there are multiple tables (US/Europe/JP/Global). The
1697 * solution here is to treat channels with number >14 as 5GHz ones,
1698 * and specifically check for the (oper_class, channel) combinations
1699 * where this doesn't hold. These are thankfully unique according to
1700 * IEEE802.11-2012.
1701 * We consider only the 2GHz and 5GHz bands and 20MHz+ channels as
1702 * valid here.
1703 */
1704 if ((oper_class == 112 || oper_class == 2 || oper_class == 3 ||
1705 oper_class == 4 || oper_class == 5 || oper_class == 6) &&
1706 target_channel < 14)
1707 band = IEEE80211_BAND_5GHZ;
1708 else
1709 band = target_channel < 14 ? IEEE80211_BAND_2GHZ :
1710 IEEE80211_BAND_5GHZ;
1711
1712 freq = ieee80211_channel_to_frequency(target_channel, band);
1713 if (freq == 0) {
1714 tdls_dbg(sdata, "Invalid channel in TDLS chan switch: %d\n",
1715 target_channel);
1716 return -EINVAL;
1717 }
1718
1719 chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
1720 if (!chan) {
1721 tdls_dbg(sdata,
1722 "Unsupported channel for TDLS chan switch: %d\n",
1723 target_channel);
1724 return -EINVAL;
1725 }
1726
1727 ieee802_11_parse_elems(tf->u.chan_switch_req.variable,
1728 skb->len - baselen, false, &elems);
1729 if (elems.parse_error) {
1730 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch req\n");
1731 return -EINVAL;
1732 }
1733
1734 if (!elems.ch_sw_timing || !elems.lnk_id) {
1735 tdls_dbg(sdata, "TDLS channel switch req - missing IEs\n");
1736 return -EINVAL;
1737 }
1738
1739 mutex_lock(&local->sta_mtx);
1740 sta = sta_info_get(sdata, tf->sa);
1741 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1742 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1743 tf->sa);
1744 ret = -EINVAL;
1745 goto out;
1746 }
1747
1748 params.sta = &sta->sta;
1749
1750 /* validate the initiator is set correctly */
1751 local_initiator =
1752 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1753 if (local_initiator == sta->sta.tdls_initiator) {
1754 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1755 ret = -EINVAL;
1756 goto out;
1757 }
1758
1759 if (!sta->sta.ht_cap.ht_supported) {
1760 chan_type = NL80211_CHAN_NO_HT;
1761 } else if (!elems.sec_chan_offs) {
1762 chan_type = NL80211_CHAN_HT20;
1763 } else {
1764 switch (elems.sec_chan_offs->sec_chan_offs) {
1765 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1766 chan_type = NL80211_CHAN_HT40PLUS;
1767 break;
1768 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1769 chan_type = NL80211_CHAN_HT40MINUS;
1770 break;
1771 default:
1772 chan_type = NL80211_CHAN_HT20;
1773 break;
1774 }
1775 }
1776
1777 cfg80211_chandef_create(&chandef, chan, chan_type);
1778 params.chandef = &chandef;
1779
1780 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1781 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1782
1783 params.tmpl_skb =
1784 ieee80211_tdls_ch_sw_resp_tmpl_get(sta,
1785 &params.ch_sw_tm_ie);
1786 if (!params.tmpl_skb) {
1787 ret = -ENOENT;
1788 goto out;
1789 }
1790
1791 drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1792
1793 tdls_dbg(sdata,
1794 "TDLS ch switch request received from %pM ch %d width %d\n",
1795 tf->sa, params.chandef->chan->center_freq,
1796 params.chandef->width);
1797out:
1798 mutex_unlock(&local->sta_mtx);
1799 dev_kfree_skb_any(params.tmpl_skb);
1800 return ret;
1801}
1802
1803void ieee80211_process_tdls_channel_switch(struct ieee80211_sub_if_data *sdata,
1804 struct sk_buff *skb)
1805{
1806 struct ieee80211_tdls_data *tf = (void *)skb->data;
1807 struct wiphy *wiphy = sdata->local->hw.wiphy;
1808
1809 /* make sure the driver supports it */
1810 if (!(wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
1811 return;
1812
1813 /* we want to access the entire packet */
1814 if (skb_linearize(skb))
1815 return;
1816 /*
1817 * The packet/size was already validated by mac80211 Rx path, only look
1818 * at the action type.
1819 */
1820 switch (tf->action_code) {
1821 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
1822 ieee80211_process_tdls_channel_switch_req(sdata, skb);
1823 break;
1824 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
1825 ieee80211_process_tdls_channel_switch_resp(sdata, skb);
1826 break;
1827 default:
1828 WARN_ON_ONCE(1);
1829 return;
1830 }
1831}
d51c2ea3
AN
1832
1833void ieee80211_teardown_tdls_peers(struct ieee80211_sub_if_data *sdata)
1834{
1835 struct sta_info *sta;
1836 u16 reason = WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED;
1837
1838 rcu_read_lock();
1839 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
1840 if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
1841 !test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1842 continue;
1843
1844 ieee80211_tdls_oper_request(&sdata->vif, sta->sta.addr,
1845 NL80211_TDLS_TEARDOWN, reason,
1846 GFP_ATOMIC);
1847 }
1848 rcu_read_unlock();
1849}