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