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