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