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