xref: /linux/net/mac80211/tdls.c (revision d603517771d8e08a2d8fc9e1f7682ce393d3973a)
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 
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 
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
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
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 
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 
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 
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 
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
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 */
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 
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 
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 
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
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
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
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
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
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 
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
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
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 *
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
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, NULL);
1125 	local_bh_enable();
1126 
1127 	return ret;
1128 
1129 fail:
1130 	dev_kfree_skb(skb);
1131 	return ret;
1132 }
1133 
1134 static int
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 	int ret;
1146 
1147 	/* don't support setup with forced SMPS mode that's not off */
1148 	if (smps_mode != IEEE80211_SMPS_AUTOMATIC &&
1149 	    smps_mode != IEEE80211_SMPS_OFF) {
1150 		tdls_dbg(sdata, "Aborting TDLS setup due to SMPS mode %d\n",
1151 			 smps_mode);
1152 		return -EOPNOTSUPP;
1153 	}
1154 
1155 	lockdep_assert_wiphy(local->hw.wiphy);
1156 
1157 	/* we don't support concurrent TDLS peer setups */
1158 	if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
1159 	    !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1160 		ret = -EBUSY;
1161 		goto out_unlock;
1162 	}
1163 
1164 	/*
1165 	 * make sure we have a STA representing the peer so we drop or buffer
1166 	 * non-TDLS-setup frames to the peer. We can't send other packets
1167 	 * during setup through the AP path.
1168 	 * Allow error packets to be sent - sometimes we don't even add a STA
1169 	 * before failing the setup.
1170 	 */
1171 	if (status_code == 0) {
1172 		rcu_read_lock();
1173 		if (!sta_info_get(sdata, peer)) {
1174 			rcu_read_unlock();
1175 			ret = -ENOLINK;
1176 			goto out_unlock;
1177 		}
1178 		rcu_read_unlock();
1179 	}
1180 
1181 	ieee80211_flush_queues(local, sdata, false);
1182 	memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
1183 
1184 	/* we cannot take the mutex while preparing the setup packet */
1185 	ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1186 					      link_id, action_code,
1187 					      dialog_token, status_code,
1188 					      peer_capability, initiator,
1189 					      extra_ies, extra_ies_len, 0,
1190 					      NULL);
1191 	if (ret < 0) {
1192 		eth_zero_addr(sdata->u.mgd.tdls_peer);
1193 		return ret;
1194 	}
1195 
1196 	wiphy_delayed_work_queue(sdata->local->hw.wiphy,
1197 				 &sdata->u.mgd.tdls_peer_del_work,
1198 				 TDLS_PEER_SETUP_TIMEOUT);
1199 	return 0;
1200 
1201 out_unlock:
1202 	return ret;
1203 }
1204 
1205 static int
1206 ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
1207 			     const u8 *peer, int link_id,
1208 			     u8 action_code, u8 dialog_token,
1209 			     u16 status_code, u32 peer_capability,
1210 			     bool initiator, const u8 *extra_ies,
1211 			     size_t extra_ies_len)
1212 {
1213 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1214 	struct ieee80211_local *local = sdata->local;
1215 	struct sta_info *sta;
1216 	int ret;
1217 
1218 	/*
1219 	 * No packets can be transmitted to the peer via the AP during setup -
1220 	 * the STA is set as a TDLS peer, but is not authorized.
1221 	 * During teardown, we prevent direct transmissions by stopping the
1222 	 * queues and flushing all direct packets.
1223 	 */
1224 	ieee80211_stop_vif_queues(local, sdata,
1225 				  IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1226 	ieee80211_flush_queues(local, sdata, false);
1227 
1228 	ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1229 					      link_id, action_code,
1230 					      dialog_token, status_code,
1231 					      peer_capability, initiator,
1232 					      extra_ies, extra_ies_len, 0,
1233 					      NULL);
1234 	if (ret < 0)
1235 		sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
1236 			  ret);
1237 
1238 	/*
1239 	 * Remove the STA AUTH flag to force further traffic through the AP. If
1240 	 * the STA was unreachable, it was already removed.
1241 	 */
1242 	rcu_read_lock();
1243 	sta = sta_info_get(sdata, peer);
1244 	if (sta)
1245 		clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1246 	rcu_read_unlock();
1247 
1248 	ieee80211_wake_vif_queues(local, sdata,
1249 				  IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1250 
1251 	return 0;
1252 }
1253 
1254 int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
1255 			const u8 *peer, int link_id,
1256 			u8 action_code, u8 dialog_token, u16 status_code,
1257 			u32 peer_capability, bool initiator,
1258 			const u8 *extra_ies, size_t extra_ies_len)
1259 {
1260 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1261 	int ret;
1262 
1263 	if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1264 		return -EOPNOTSUPP;
1265 
1266 	/* make sure we are in managed mode, and associated */
1267 	if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1268 	    !sdata->u.mgd.associated)
1269 		return -EINVAL;
1270 
1271 	switch (action_code) {
1272 	case WLAN_TDLS_SETUP_REQUEST:
1273 	case WLAN_TDLS_SETUP_RESPONSE:
1274 		ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer,
1275 						link_id, action_code,
1276 						dialog_token, status_code,
1277 						peer_capability, initiator,
1278 						extra_ies, extra_ies_len);
1279 		break;
1280 	case WLAN_TDLS_TEARDOWN:
1281 		ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer, link_id,
1282 						   action_code, dialog_token,
1283 						   status_code,
1284 						   peer_capability, initiator,
1285 						   extra_ies, extra_ies_len);
1286 		break;
1287 	case WLAN_TDLS_DISCOVERY_REQUEST:
1288 		/*
1289 		 * Protect the discovery so we can hear the TDLS discovery
1290 		 * response frame. It is transmitted directly and not buffered
1291 		 * by the AP.
1292 		 */
1293 		drv_mgd_protect_tdls_discover(sdata->local, sdata, link_id);
1294 		fallthrough;
1295 	case WLAN_TDLS_SETUP_CONFIRM:
1296 	case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
1297 		/* no special handling */
1298 		ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1299 						      link_id, action_code,
1300 						      dialog_token,
1301 						      status_code,
1302 						      peer_capability,
1303 						      initiator, extra_ies,
1304 						      extra_ies_len, 0, NULL);
1305 		break;
1306 	default:
1307 		ret = -EOPNOTSUPP;
1308 		break;
1309 	}
1310 
1311 	tdls_dbg(sdata, "TDLS mgmt action %d peer %pM link_id %d status %d\n",
1312 		 action_code, peer, link_id, ret);
1313 	return ret;
1314 }
1315 
1316 static void iee80211_tdls_recalc_chanctx(struct ieee80211_sub_if_data *sdata,
1317 					 struct sta_info *sta)
1318 {
1319 	struct ieee80211_local *local = sdata->local;
1320 	struct ieee80211_chanctx_conf *conf;
1321 	struct ieee80211_chanctx *ctx;
1322 	enum nl80211_chan_width width;
1323 	struct ieee80211_supported_band *sband;
1324 
1325 	lockdep_assert_wiphy(local->hw.wiphy);
1326 
1327 	conf = rcu_dereference_protected(sdata->vif.bss_conf.chanctx_conf,
1328 					 lockdep_is_held(&local->hw.wiphy->mtx));
1329 	if (conf) {
1330 		width = conf->def.width;
1331 		sband = local->hw.wiphy->bands[conf->def.chan->band];
1332 		ctx = container_of(conf, struct ieee80211_chanctx, conf);
1333 		ieee80211_recalc_chanctx_chantype(local, ctx);
1334 
1335 		/* if width changed and a peer is given, update its BW */
1336 		if (width != conf->def.width && sta &&
1337 		    test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW)) {
1338 			enum ieee80211_sta_rx_bandwidth bw;
1339 
1340 			bw = ieee80211_chan_width_to_rx_bw(conf->def.width);
1341 			bw = min(bw, ieee80211_sta_current_bw(&sta->deflink,
1342 							      &conf->def,
1343 							      IEEE80211_STA_BW_RX_FROM_STA));
1344 			if (bw != sta->sta.deflink.bandwidth) {
1345 				sta->sta.deflink.bandwidth = bw;
1346 				rate_control_rate_update(local, sband,
1347 							 &sta->deflink,
1348 							 IEEE80211_RC_BW_CHANGED);
1349 				/*
1350 				 * if a TDLS peer BW was updated, we need to
1351 				 * recalc the chandef width again, to get the
1352 				 * correct chanctx min_def
1353 				 */
1354 				ieee80211_recalc_chanctx_chantype(local, ctx);
1355 			}
1356 		}
1357 
1358 	}
1359 }
1360 
1361 static int iee80211_tdls_have_ht_peers(struct ieee80211_sub_if_data *sdata)
1362 {
1363 	struct sta_info *sta;
1364 	bool result = false;
1365 
1366 	rcu_read_lock();
1367 	list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
1368 		if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
1369 		    !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
1370 		    !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH) ||
1371 		    !sta->sta.deflink.ht_cap.ht_supported)
1372 			continue;
1373 		result = true;
1374 		break;
1375 	}
1376 	rcu_read_unlock();
1377 
1378 	return result;
1379 }
1380 
1381 static void
1382 iee80211_tdls_recalc_ht_protection(struct ieee80211_sub_if_data *sdata,
1383 				   struct sta_info *sta)
1384 {
1385 	bool tdls_ht;
1386 	u16 protection = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED |
1387 			 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
1388 			 IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
1389 	u16 opmode;
1390 
1391 	/* Nothing to do if the BSS connection uses (at least) HT */
1392 	if (sdata->deflink.u.mgd.conn.mode >= IEEE80211_CONN_MODE_HT)
1393 		return;
1394 
1395 	tdls_ht = (sta && sta->sta.deflink.ht_cap.ht_supported) ||
1396 		  iee80211_tdls_have_ht_peers(sdata);
1397 
1398 	opmode = sdata->vif.bss_conf.ht_operation_mode;
1399 
1400 	if (tdls_ht)
1401 		opmode |= protection;
1402 	else
1403 		opmode &= ~protection;
1404 
1405 	if (opmode == sdata->vif.bss_conf.ht_operation_mode)
1406 		return;
1407 
1408 	sdata->vif.bss_conf.ht_operation_mode = opmode;
1409 	ieee80211_link_info_change_notify(sdata, &sdata->deflink,
1410 					  BSS_CHANGED_HT);
1411 }
1412 
1413 int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
1414 			const u8 *peer, enum nl80211_tdls_operation oper)
1415 {
1416 	struct sta_info *sta;
1417 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1418 	struct ieee80211_local *local = sdata->local;
1419 	int ret;
1420 
1421 	lockdep_assert_wiphy(local->hw.wiphy);
1422 
1423 	if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1424 		return -EOPNOTSUPP;
1425 
1426 	if (sdata->vif.type != NL80211_IFTYPE_STATION || !sdata->vif.cfg.assoc)
1427 		return -EINVAL;
1428 
1429 	switch (oper) {
1430 	case NL80211_TDLS_ENABLE_LINK:
1431 	case NL80211_TDLS_DISABLE_LINK:
1432 		break;
1433 	case NL80211_TDLS_TEARDOWN:
1434 	case NL80211_TDLS_SETUP:
1435 	case NL80211_TDLS_DISCOVERY_REQ:
1436 		/* We don't support in-driver setup/teardown/discovery */
1437 		return -EOPNOTSUPP;
1438 	}
1439 
1440 	/* protect possible bss_conf changes and avoid concurrency in
1441 	 * ieee80211_bss_info_change_notify()
1442 	 */
1443 	tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
1444 
1445 	switch (oper) {
1446 	case NL80211_TDLS_ENABLE_LINK:
1447 		if (sdata->vif.bss_conf.csa_active) {
1448 			tdls_dbg(sdata, "TDLS: disallow link during CSA\n");
1449 			return -EBUSY;
1450 		}
1451 
1452 		sta = sta_info_get(sdata, peer);
1453 		if (!sta || !sta->sta.tdls)
1454 			return -ENOLINK;
1455 
1456 		iee80211_tdls_recalc_chanctx(sdata, sta);
1457 		iee80211_tdls_recalc_ht_protection(sdata, sta);
1458 
1459 		set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1460 
1461 		WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
1462 			     !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
1463 		break;
1464 	case NL80211_TDLS_DISABLE_LINK:
1465 		/*
1466 		 * The teardown message in ieee80211_tdls_mgmt_teardown() was
1467 		 * created while the queues were stopped, so it might still be
1468 		 * pending. Before flushing the queues we need to be sure the
1469 		 * message is handled by the tasklet handling pending messages,
1470 		 * otherwise we might start destroying the station before
1471 		 * sending the teardown packet.
1472 		 * Note that this only forces the tasklet to flush pendings -
1473 		 * not to stop the tasklet from rescheduling itself.
1474 		 */
1475 		tasklet_kill(&local->tx_pending_tasklet);
1476 		/* flush a potentially queued teardown packet */
1477 		ieee80211_flush_queues(local, sdata, false);
1478 
1479 		ret = sta_info_destroy_addr(sdata, peer);
1480 
1481 		iee80211_tdls_recalc_ht_protection(sdata, NULL);
1482 
1483 		iee80211_tdls_recalc_chanctx(sdata, NULL);
1484 		if (ret)
1485 			return ret;
1486 		break;
1487 	default:
1488 		return -EOPNOTSUPP;
1489 	}
1490 
1491 	if (ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1492 		wiphy_delayed_work_cancel(sdata->local->hw.wiphy,
1493 					  &sdata->u.mgd.tdls_peer_del_work);
1494 		eth_zero_addr(sdata->u.mgd.tdls_peer);
1495 	}
1496 
1497 	wiphy_work_queue(sdata->local->hw.wiphy,
1498 			 &sdata->deflink.u.mgd.request_smps_work);
1499 
1500 	return 0;
1501 }
1502 
1503 void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
1504 				 enum nl80211_tdls_operation oper,
1505 				 u16 reason_code, gfp_t gfp)
1506 {
1507 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1508 
1509 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc) {
1510 		sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
1511 			  oper);
1512 		return;
1513 	}
1514 
1515 	cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
1516 }
1517 EXPORT_SYMBOL(ieee80211_tdls_oper_request);
1518 
1519 static void
1520 iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
1521 {
1522 	struct ieee80211_ch_switch_timing *ch_sw;
1523 
1524 	*buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
1525 	*buf++ = sizeof(struct ieee80211_ch_switch_timing);
1526 
1527 	ch_sw = (void *)buf;
1528 	ch_sw->switch_time = cpu_to_le16(switch_time);
1529 	ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
1530 }
1531 
1532 /* find switch timing IE in SKB ready for Tx */
1533 static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
1534 {
1535 	struct ieee80211_tdls_data *tf;
1536 	const u8 *ie_start;
1537 
1538 	/*
1539 	 * Get the offset for the new location of the switch timing IE.
1540 	 * The SKB network header will now point to the "payload_type"
1541 	 * element of the TDLS data frame struct.
1542 	 */
1543 	tf = container_of(skb->data + skb_network_offset(skb),
1544 			  struct ieee80211_tdls_data, payload_type);
1545 	ie_start = tf->u.chan_switch_req.variable;
1546 	return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
1547 				skb->len - (ie_start - skb->data));
1548 }
1549 
1550 static struct sk_buff *
1551 ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
1552 			      struct cfg80211_chan_def *chandef,
1553 			      u32 *ch_sw_tm_ie_offset)
1554 {
1555 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1556 	u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
1557 		     2 + sizeof(struct ieee80211_ch_switch_timing)];
1558 	int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
1559 	u8 *pos = extra_ies;
1560 	struct sk_buff *skb;
1561 	int link_id = sta->sta.valid_links ? ffs(sta->sta.valid_links) - 1 : 0;
1562 
1563 	/*
1564 	 * if chandef points to a wide channel add a Secondary-Channel
1565 	 * Offset information element
1566 	 */
1567 	if (chandef->width == NL80211_CHAN_WIDTH_40) {
1568 		struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
1569 		bool ht40plus;
1570 
1571 		*pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
1572 		*pos++ = sizeof(*sec_chan_ie);
1573 		sec_chan_ie = (void *)pos;
1574 
1575 		ht40plus = cfg80211_get_chandef_type(chandef) ==
1576 							NL80211_CHAN_HT40PLUS;
1577 		sec_chan_ie->sec_chan_offs = ht40plus ?
1578 					     IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
1579 					     IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1580 		pos += sizeof(*sec_chan_ie);
1581 
1582 		extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1583 	}
1584 
1585 	/* just set the values to 0, this is a template */
1586 	iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
1587 
1588 	skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1589 					      link_id,
1590 					      WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
1591 					      0, 0, !sta->sta.tdls_initiator,
1592 					      extra_ies, extra_ies_len,
1593 					      oper_class, chandef);
1594 	if (!skb)
1595 		return NULL;
1596 
1597 	skb = ieee80211_build_data_template(sdata, skb, 0);
1598 	if (IS_ERR(skb)) {
1599 		tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
1600 		return NULL;
1601 	}
1602 
1603 	if (ch_sw_tm_ie_offset) {
1604 		const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1605 
1606 		if (!tm_ie) {
1607 			tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
1608 			dev_kfree_skb_any(skb);
1609 			return NULL;
1610 		}
1611 
1612 		*ch_sw_tm_ie_offset = tm_ie - skb->data;
1613 	}
1614 
1615 	tdls_dbg(sdata,
1616 		 "TDLS channel switch request template for %pM ch %d width %d\n",
1617 		 sta->sta.addr, chandef->chan->center_freq, chandef->width);
1618 	return skb;
1619 }
1620 
1621 int
1622 ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
1623 			      const u8 *addr, u8 oper_class,
1624 			      struct cfg80211_chan_def *chandef)
1625 {
1626 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1627 	struct ieee80211_local *local = sdata->local;
1628 	struct sta_info *sta;
1629 	struct sk_buff *skb = NULL;
1630 	u32 ch_sw_tm_ie;
1631 	int ret;
1632 
1633 	lockdep_assert_wiphy(local->hw.wiphy);
1634 
1635 	if (chandef->chan->freq_offset)
1636 		/* this may work, but is untested */
1637 		return -EOPNOTSUPP;
1638 
1639 	sta = sta_info_get(sdata, addr);
1640 	if (!sta) {
1641 		tdls_dbg(sdata,
1642 			 "Invalid TDLS peer %pM for channel switch request\n",
1643 			 addr);
1644 		ret = -ENOENT;
1645 		goto out;
1646 	}
1647 
1648 	if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
1649 		tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
1650 			 addr);
1651 		ret = -EOPNOTSUPP;
1652 		goto out;
1653 	}
1654 
1655 	skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
1656 					    &ch_sw_tm_ie);
1657 	if (!skb) {
1658 		ret = -ENOENT;
1659 		goto out;
1660 	}
1661 
1662 	ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
1663 				      chandef, skb, ch_sw_tm_ie);
1664 	if (!ret)
1665 		set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1666 
1667 out:
1668 	dev_kfree_skb_any(skb);
1669 	return ret;
1670 }
1671 
1672 void
1673 ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
1674 				     struct net_device *dev,
1675 				     const u8 *addr)
1676 {
1677 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1678 	struct ieee80211_local *local = sdata->local;
1679 	struct sta_info *sta;
1680 
1681 	lockdep_assert_wiphy(local->hw.wiphy);
1682 
1683 	sta = sta_info_get(sdata, addr);
1684 	if (!sta) {
1685 		tdls_dbg(sdata,
1686 			 "Invalid TDLS peer %pM for channel switch cancel\n",
1687 			 addr);
1688 		return;
1689 	}
1690 
1691 	if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1692 		tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
1693 			 addr);
1694 		return;
1695 	}
1696 
1697 	drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1698 	clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1699 }
1700 
1701 static struct sk_buff *
1702 ieee80211_tdls_ch_sw_resp_tmpl_get(struct sta_info *sta,
1703 				   u32 *ch_sw_tm_ie_offset)
1704 {
1705 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1706 	struct sk_buff *skb;
1707 	u8 extra_ies[2 + sizeof(struct ieee80211_ch_switch_timing)];
1708 	int link_id = sta->sta.valid_links ? ffs(sta->sta.valid_links) - 1 : 0;
1709 
1710 	/* initial timing are always zero in the template */
1711 	iee80211_tdls_add_ch_switch_timing(extra_ies, 0, 0);
1712 
1713 	skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1714 					link_id,
1715 					WLAN_TDLS_CHANNEL_SWITCH_RESPONSE,
1716 					0, 0, !sta->sta.tdls_initiator,
1717 					extra_ies, sizeof(extra_ies), 0, NULL);
1718 	if (!skb)
1719 		return NULL;
1720 
1721 	skb = ieee80211_build_data_template(sdata, skb, 0);
1722 	if (IS_ERR(skb)) {
1723 		tdls_dbg(sdata,
1724 			 "Failed building TDLS channel switch resp frame\n");
1725 		return NULL;
1726 	}
1727 
1728 	if (ch_sw_tm_ie_offset) {
1729 		const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1730 
1731 		if (!tm_ie) {
1732 			tdls_dbg(sdata,
1733 				 "No switch timing IE in TDLS switch resp\n");
1734 			dev_kfree_skb_any(skb);
1735 			return NULL;
1736 		}
1737 
1738 		*ch_sw_tm_ie_offset = tm_ie - skb->data;
1739 	}
1740 
1741 	tdls_dbg(sdata, "TDLS get channel switch response template for %pM\n",
1742 		 sta->sta.addr);
1743 	return skb;
1744 }
1745 
1746 static int
1747 ieee80211_process_tdls_channel_switch_resp(struct ieee80211_sub_if_data *sdata,
1748 					   struct sk_buff *skb)
1749 {
1750 	struct ieee80211_local *local = sdata->local;
1751 	struct ieee802_11_elems *elems = NULL;
1752 	struct sta_info *sta;
1753 	struct ieee80211_tdls_data *tf = (void *)skb->data;
1754 	bool local_initiator;
1755 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1756 	int baselen = offsetof(typeof(*tf), u.chan_switch_resp.variable);
1757 	struct ieee80211_tdls_ch_sw_params params = {};
1758 	int ret;
1759 
1760 	lockdep_assert_wiphy(local->hw.wiphy);
1761 
1762 	params.action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
1763 	params.timestamp = rx_status->device_timestamp;
1764 
1765 	if (skb->len < baselen) {
1766 		tdls_dbg(sdata, "TDLS channel switch resp too short: %d\n",
1767 			 skb->len);
1768 		return -EINVAL;
1769 	}
1770 
1771 	sta = sta_info_get(sdata, tf->sa);
1772 	if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1773 		tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1774 			 tf->sa);
1775 		ret = -EINVAL;
1776 		goto out;
1777 	}
1778 
1779 	params.sta = &sta->sta;
1780 	params.status = le16_to_cpu(tf->u.chan_switch_resp.status_code);
1781 	if (params.status != 0) {
1782 		ret = 0;
1783 		goto call_drv;
1784 	}
1785 
1786 	elems = ieee802_11_parse_elems(tf->u.chan_switch_resp.variable,
1787 				       skb->len - baselen,
1788 				       IEEE80211_FTYPE_MGMT |
1789 				       IEEE80211_STYPE_ACTION,
1790 				       NULL);
1791 	if (!elems) {
1792 		ret = -ENOMEM;
1793 		goto out;
1794 	}
1795 
1796 	if (elems->parse_error) {
1797 		tdls_dbg(sdata, "Invalid IEs in TDLS channel switch resp\n");
1798 		ret = -EINVAL;
1799 		goto out;
1800 	}
1801 
1802 	if (!elems->ch_sw_timing || !elems->lnk_id) {
1803 		tdls_dbg(sdata, "TDLS channel switch resp - missing IEs\n");
1804 		ret = -EINVAL;
1805 		goto out;
1806 	}
1807 
1808 	/* validate the initiator is set correctly */
1809 	local_initiator =
1810 		!memcmp(elems->lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1811 	if (local_initiator == sta->sta.tdls_initiator) {
1812 		tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1813 		ret = -EINVAL;
1814 		goto out;
1815 	}
1816 
1817 	params.switch_time = le16_to_cpu(elems->ch_sw_timing->switch_time);
1818 	params.switch_timeout = le16_to_cpu(elems->ch_sw_timing->switch_timeout);
1819 
1820 	params.tmpl_skb =
1821 		ieee80211_tdls_ch_sw_resp_tmpl_get(sta, &params.ch_sw_tm_ie);
1822 	if (!params.tmpl_skb) {
1823 		ret = -ENOENT;
1824 		goto out;
1825 	}
1826 
1827 	ret = 0;
1828 call_drv:
1829 	drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1830 
1831 	tdls_dbg(sdata,
1832 		 "TDLS channel switch response received from %pM status %d\n",
1833 		 tf->sa, params.status);
1834 
1835 out:
1836 	dev_kfree_skb_any(params.tmpl_skb);
1837 	kfree(elems);
1838 	return ret;
1839 }
1840 
1841 static int
1842 ieee80211_process_tdls_channel_switch_req(struct ieee80211_sub_if_data *sdata,
1843 					  struct sk_buff *skb)
1844 {
1845 	struct ieee80211_local *local = sdata->local;
1846 	struct ieee802_11_elems *elems;
1847 	struct cfg80211_chan_def chandef;
1848 	struct ieee80211_channel *chan;
1849 	enum nl80211_channel_type chan_type;
1850 	int freq;
1851 	u8 target_channel, oper_class;
1852 	bool local_initiator;
1853 	struct sta_info *sta;
1854 	enum nl80211_band band;
1855 	struct ieee80211_tdls_data *tf = (void *)skb->data;
1856 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1857 	int baselen = offsetof(typeof(*tf), u.chan_switch_req.variable);
1858 	struct ieee80211_tdls_ch_sw_params params = {};
1859 	int ret = 0;
1860 
1861 	lockdep_assert_wiphy(local->hw.wiphy);
1862 
1863 	params.action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
1864 	params.timestamp = rx_status->device_timestamp;
1865 
1866 	if (skb->len < baselen) {
1867 		tdls_dbg(sdata, "TDLS channel switch req too short: %d\n",
1868 			 skb->len);
1869 		return -EINVAL;
1870 	}
1871 
1872 	target_channel = tf->u.chan_switch_req.target_channel;
1873 	oper_class = tf->u.chan_switch_req.oper_class;
1874 
1875 	/*
1876 	 * We can't easily infer the channel band. The operating class is
1877 	 * ambiguous - there are multiple tables (US/Europe/JP/Global). The
1878 	 * solution here is to treat channels with number >14 as 5GHz ones,
1879 	 * and specifically check for the (oper_class, channel) combinations
1880 	 * where this doesn't hold. These are thankfully unique according to
1881 	 * IEEE802.11-2012.
1882 	 * We consider only the 2GHz and 5GHz bands and 20MHz+ channels as
1883 	 * valid here.
1884 	 */
1885 	if ((oper_class == 112 || oper_class == 2 || oper_class == 3 ||
1886 	     oper_class == 4 || oper_class == 5 || oper_class == 6) &&
1887 	     target_channel < 14)
1888 		band = NL80211_BAND_5GHZ;
1889 	else
1890 		band = target_channel < 14 ? NL80211_BAND_2GHZ :
1891 					     NL80211_BAND_5GHZ;
1892 
1893 	freq = ieee80211_channel_to_frequency(target_channel, band);
1894 	if (freq == 0) {
1895 		tdls_dbg(sdata, "Invalid channel in TDLS chan switch: %d\n",
1896 			 target_channel);
1897 		return -EINVAL;
1898 	}
1899 
1900 	chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
1901 	if (!chan) {
1902 		tdls_dbg(sdata,
1903 			 "Unsupported channel for TDLS chan switch: %d\n",
1904 			 target_channel);
1905 		return -EINVAL;
1906 	}
1907 
1908 	elems = ieee802_11_parse_elems(tf->u.chan_switch_req.variable,
1909 				       skb->len - baselen,
1910 				       IEEE80211_FTYPE_MGMT |
1911 				       IEEE80211_STYPE_ACTION,
1912 				       NULL);
1913 	if (!elems)
1914 		return -ENOMEM;
1915 
1916 	if (elems->parse_error) {
1917 		tdls_dbg(sdata, "Invalid IEs in TDLS channel switch req\n");
1918 		ret = -EINVAL;
1919 		goto free;
1920 	}
1921 
1922 	if (!elems->ch_sw_timing || !elems->lnk_id) {
1923 		tdls_dbg(sdata, "TDLS channel switch req - missing IEs\n");
1924 		ret = -EINVAL;
1925 		goto free;
1926 	}
1927 
1928 	if (!elems->sec_chan_offs) {
1929 		chan_type = NL80211_CHAN_HT20;
1930 	} else {
1931 		switch (elems->sec_chan_offs->sec_chan_offs) {
1932 		case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1933 			chan_type = NL80211_CHAN_HT40PLUS;
1934 			break;
1935 		case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1936 			chan_type = NL80211_CHAN_HT40MINUS;
1937 			break;
1938 		default:
1939 			chan_type = NL80211_CHAN_HT20;
1940 			break;
1941 		}
1942 	}
1943 
1944 	cfg80211_chandef_create(&chandef, chan, chan_type);
1945 
1946 	/* we will be active on the TDLS link */
1947 	if (!cfg80211_reg_can_beacon_relax(sdata->local->hw.wiphy, &chandef,
1948 					   sdata->wdev.iftype)) {
1949 		tdls_dbg(sdata, "TDLS chan switch to forbidden channel\n");
1950 		ret = -EINVAL;
1951 		goto free;
1952 	}
1953 
1954 	sta = sta_info_get(sdata, tf->sa);
1955 	if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1956 		tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1957 			 tf->sa);
1958 		ret = -EINVAL;
1959 		goto out;
1960 	}
1961 
1962 	params.sta = &sta->sta;
1963 
1964 	/* validate the initiator is set correctly */
1965 	local_initiator =
1966 		!memcmp(elems->lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1967 	if (local_initiator == sta->sta.tdls_initiator) {
1968 		tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1969 		ret = -EINVAL;
1970 		goto out;
1971 	}
1972 
1973 	/* peer should have known better */
1974 	if (!sta->sta.deflink.ht_cap.ht_supported && elems->sec_chan_offs &&
1975 	    elems->sec_chan_offs->sec_chan_offs) {
1976 		tdls_dbg(sdata, "TDLS chan switch - wide chan unsupported\n");
1977 		ret = -EOPNOTSUPP;
1978 		goto out;
1979 	}
1980 
1981 	params.chandef = &chandef;
1982 	params.switch_time = le16_to_cpu(elems->ch_sw_timing->switch_time);
1983 	params.switch_timeout = le16_to_cpu(elems->ch_sw_timing->switch_timeout);
1984 
1985 	params.tmpl_skb =
1986 		ieee80211_tdls_ch_sw_resp_tmpl_get(sta,
1987 						   &params.ch_sw_tm_ie);
1988 	if (!params.tmpl_skb) {
1989 		ret = -ENOENT;
1990 		goto out;
1991 	}
1992 
1993 	drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1994 
1995 	tdls_dbg(sdata,
1996 		 "TDLS ch switch request received from %pM ch %d width %d\n",
1997 		 tf->sa, params.chandef->chan->center_freq,
1998 		 params.chandef->width);
1999 out:
2000 	dev_kfree_skb_any(params.tmpl_skb);
2001 free:
2002 	kfree(elems);
2003 	return ret;
2004 }
2005 
2006 void
2007 ieee80211_process_tdls_channel_switch(struct ieee80211_sub_if_data *sdata,
2008 				      struct sk_buff *skb)
2009 {
2010 	struct ieee80211_tdls_data *tf = (void *)skb->data;
2011 	struct wiphy *wiphy = sdata->local->hw.wiphy;
2012 
2013 	lockdep_assert_wiphy(wiphy);
2014 
2015 	/* make sure the driver supports it */
2016 	if (!(wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
2017 		return;
2018 
2019 	/* we want to access the entire packet */
2020 	if (skb_linearize(skb))
2021 		return;
2022 	/*
2023 	 * The packet/size was already validated by mac80211 Rx path, only look
2024 	 * at the action type.
2025 	 */
2026 	switch (tf->action_code) {
2027 	case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
2028 		ieee80211_process_tdls_channel_switch_req(sdata, skb);
2029 		break;
2030 	case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
2031 		ieee80211_process_tdls_channel_switch_resp(sdata, skb);
2032 		break;
2033 	default:
2034 		WARN_ON_ONCE(1);
2035 		return;
2036 	}
2037 }
2038 
2039 void ieee80211_teardown_tdls_peers(struct ieee80211_link_data *link)
2040 {
2041 	struct ieee80211_sub_if_data *sdata = link->sdata;
2042 	struct sta_info *sta;
2043 	u16 reason = WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED;
2044 
2045 	rcu_read_lock();
2046 	list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
2047 		if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
2048 		    !test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2049 			continue;
2050 
2051 		if (sta->deflink.link_id != link->link_id)
2052 			continue;
2053 
2054 		ieee80211_tdls_oper_request(&sdata->vif, sta->sta.addr,
2055 					    NL80211_TDLS_TEARDOWN, reason,
2056 					    GFP_ATOMIC);
2057 	}
2058 	rcu_read_unlock();
2059 }
2060 
2061 void ieee80211_tdls_handle_disconnect(struct ieee80211_sub_if_data *sdata,
2062 				      const u8 *peer, u16 reason)
2063 {
2064 	struct ieee80211_sta *sta;
2065 
2066 	rcu_read_lock();
2067 	sta = ieee80211_find_sta(&sdata->vif, peer);
2068 	if (!sta || !sta->tdls) {
2069 		rcu_read_unlock();
2070 		return;
2071 	}
2072 	rcu_read_unlock();
2073 
2074 	tdls_dbg(sdata, "disconnected from TDLS peer %pM (Reason: %u=%s)\n",
2075 		 peer, reason,
2076 		 ieee80211_get_reason_code_string(reason));
2077 
2078 	ieee80211_tdls_oper_request(&sdata->vif, peer,
2079 				    NL80211_TDLS_TEARDOWN,
2080 				    WLAN_REASON_TDLS_TEARDOWN_UNREACHABLE,
2081 				    GFP_ATOMIC);
2082 }
2083