xref: /linux/net/wireless/util.c (revision 1a15bf9708ba3bf80410065e113aa17cd6a18dcf)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Wireless utility functions
4  *
5  * Copyright 2007-2009	Johannes Berg <johannes@sipsolutions.net>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright 2017	Intel Deutschland GmbH
8  * Copyright (C) 2018-2023, 2025-2026 Intel Corporation
9  */
10 #include <linux/export.h>
11 #include <linux/bitops.h>
12 #include <linux/etherdevice.h>
13 #include <linux/slab.h>
14 #include <linux/ieee80211.h>
15 #include <net/cfg80211.h>
16 #include <net/ip.h>
17 #include <net/dsfield.h>
18 #include <linux/if_vlan.h>
19 #include <linux/mpls.h>
20 #include <linux/gcd.h>
21 #include <linux/bitfield.h>
22 #include <linux/nospec.h>
23 #include "core.h"
24 #include "rdev-ops.h"
25 
26 
27 const struct ieee80211_rate *
28 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
29 			    u32 basic_rates, int bitrate)
30 {
31 	struct ieee80211_rate *result = &sband->bitrates[0];
32 	int i;
33 
34 	for (i = 0; i < sband->n_bitrates; i++) {
35 		if (!(basic_rates & BIT(i)))
36 			continue;
37 		if (sband->bitrates[i].bitrate > bitrate)
38 			continue;
39 		result = &sband->bitrates[i];
40 	}
41 
42 	return result;
43 }
44 EXPORT_SYMBOL(ieee80211_get_response_rate);
45 
46 u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband)
47 {
48 	struct ieee80211_rate *bitrates;
49 	u32 mandatory_rates = 0;
50 	enum ieee80211_rate_flags mandatory_flag;
51 	int i;
52 
53 	if (WARN_ON(!sband))
54 		return 1;
55 
56 	if (sband->band == NL80211_BAND_2GHZ)
57 		mandatory_flag = IEEE80211_RATE_MANDATORY_B;
58 	else
59 		mandatory_flag = IEEE80211_RATE_MANDATORY_A;
60 
61 	bitrates = sband->bitrates;
62 	for (i = 0; i < sband->n_bitrates; i++)
63 		if (bitrates[i].flags & mandatory_flag)
64 			mandatory_rates |= BIT(i);
65 	return mandatory_rates;
66 }
67 EXPORT_SYMBOL(ieee80211_mandatory_rates);
68 
69 u32 ieee80211_channel_to_freq_khz(int chan, enum nl80211_band band)
70 {
71 	/* see 802.11 17.3.8.3.2 and Annex J
72 	 * there are overlapping channel numbers in 5GHz and 2GHz bands */
73 	if (chan <= 0)
74 		return 0; /* not supported */
75 	switch (band) {
76 	case NL80211_BAND_2GHZ:
77 	case NL80211_BAND_LC:
78 		if (chan == 14)
79 			return MHZ_TO_KHZ(2484);
80 		else if (chan < 14)
81 			return MHZ_TO_KHZ(2407 + chan * 5);
82 		break;
83 	case NL80211_BAND_5GHZ:
84 		if (chan >= 182 && chan <= 196)
85 			return MHZ_TO_KHZ(4000 + chan * 5);
86 		else
87 			return MHZ_TO_KHZ(5000 + chan * 5);
88 		break;
89 	case NL80211_BAND_6GHZ:
90 		/* see 802.11ax D6.1 27.3.23.2 */
91 		if (chan == 2)
92 			return MHZ_TO_KHZ(5935);
93 		if (chan <= 253)
94 			return MHZ_TO_KHZ(5950 + chan * 5);
95 		break;
96 	case NL80211_BAND_60GHZ:
97 		if (chan < 7)
98 			return MHZ_TO_KHZ(56160 + chan * 2160);
99 		break;
100 	case NL80211_BAND_S1GHZ:
101 		return 902000 + chan * 500;
102 	default:
103 		;
104 	}
105 	return 0; /* not supported */
106 }
107 EXPORT_SYMBOL(ieee80211_channel_to_freq_khz);
108 
109 int ieee80211_freq_khz_to_channel(u32 freq)
110 {
111 	/* TODO: just handle MHz for now */
112 	freq = KHZ_TO_MHZ(freq);
113 
114 	/* see 802.11 17.3.8.3.2 and Annex J */
115 	if (freq == 2484)
116 		return 14;
117 	else if (freq < 2484)
118 		return (freq - 2407) / 5;
119 	else if (freq >= 4910 && freq <= 4980)
120 		return (freq - 4000) / 5;
121 	else if (freq < 5925)
122 		return (freq - 5000) / 5;
123 	else if (freq == 5935)
124 		return 2;
125 	else if (freq <= 45000) /* DMG band lower limit */
126 		/* see 802.11ax D6.1 27.3.22.2 */
127 		return (freq - 5950) / 5;
128 	else if (freq >= 58320 && freq <= 70200)
129 		return (freq - 56160) / 2160;
130 	else
131 		return 0;
132 }
133 EXPORT_SYMBOL(ieee80211_freq_khz_to_channel);
134 
135 struct ieee80211_channel *ieee80211_get_channel_khz(struct wiphy *wiphy,
136 						    u32 freq)
137 {
138 	enum nl80211_band band;
139 	struct ieee80211_supported_band *sband;
140 	int i;
141 
142 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
143 		sband = wiphy->bands[band];
144 
145 		if (!sband)
146 			continue;
147 
148 		for (i = 0; i < sband->n_channels; i++) {
149 			struct ieee80211_channel *chan = &sband->channels[i];
150 
151 			if (ieee80211_channel_to_khz(chan) == freq)
152 				return chan;
153 		}
154 	}
155 
156 	return NULL;
157 }
158 EXPORT_SYMBOL(ieee80211_get_channel_khz);
159 
160 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
161 {
162 	int i, want;
163 
164 	switch (sband->band) {
165 	case NL80211_BAND_5GHZ:
166 	case NL80211_BAND_6GHZ:
167 		want = 3;
168 		for (i = 0; i < sband->n_bitrates; i++) {
169 			if (sband->bitrates[i].bitrate == 60 ||
170 			    sband->bitrates[i].bitrate == 120 ||
171 			    sband->bitrates[i].bitrate == 240) {
172 				sband->bitrates[i].flags |=
173 					IEEE80211_RATE_MANDATORY_A;
174 				want--;
175 			}
176 		}
177 		WARN_ON(want);
178 		break;
179 	case NL80211_BAND_2GHZ:
180 	case NL80211_BAND_LC:
181 		want = 7;
182 		for (i = 0; i < sband->n_bitrates; i++) {
183 			switch (sband->bitrates[i].bitrate) {
184 			case 10:
185 			case 20:
186 			case 55:
187 			case 110:
188 				sband->bitrates[i].flags |=
189 					IEEE80211_RATE_MANDATORY_B |
190 					IEEE80211_RATE_MANDATORY_G;
191 				want--;
192 				break;
193 			case 60:
194 			case 120:
195 			case 240:
196 				sband->bitrates[i].flags |=
197 					IEEE80211_RATE_MANDATORY_G;
198 				want--;
199 				fallthrough;
200 			default:
201 				sband->bitrates[i].flags |=
202 					IEEE80211_RATE_ERP_G;
203 				break;
204 			}
205 		}
206 		WARN_ON(want != 0 && want != 3);
207 		break;
208 	case NL80211_BAND_60GHZ:
209 		/* check for mandatory HT MCS 1..4 */
210 		WARN_ON(!sband->ht_cap.ht_supported);
211 		WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
212 		break;
213 	case NL80211_BAND_S1GHZ:
214 		/* Figure 9-589bd: 3 means unsupported, so != 3 means at least
215 		 * mandatory is ok.
216 		 */
217 		WARN_ON((sband->s1g_cap.nss_mcs[0] & 0x3) == 0x3);
218 		break;
219 	case NUM_NL80211_BANDS:
220 	default:
221 		WARN_ON(1);
222 		break;
223 	}
224 }
225 
226 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
227 {
228 	enum nl80211_band band;
229 
230 	for (band = 0; band < NUM_NL80211_BANDS; band++)
231 		if (wiphy->bands[band])
232 			set_mandatory_flags_band(wiphy->bands[band]);
233 }
234 
235 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
236 {
237 	int i;
238 	for (i = 0; i < wiphy->n_cipher_suites; i++)
239 		if (cipher == wiphy->cipher_suites[i])
240 			return true;
241 	return false;
242 }
243 
244 static bool
245 cfg80211_igtk_cipher_supported(struct cfg80211_registered_device *rdev)
246 {
247 	struct wiphy *wiphy = &rdev->wiphy;
248 	int i;
249 
250 	for (i = 0; i < wiphy->n_cipher_suites; i++) {
251 		switch (wiphy->cipher_suites[i]) {
252 		case WLAN_CIPHER_SUITE_AES_CMAC:
253 		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
254 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
255 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
256 			return true;
257 		}
258 	}
259 
260 	return false;
261 }
262 
263 bool cfg80211_valid_key_idx(struct cfg80211_registered_device *rdev,
264 			    int key_idx, bool pairwise)
265 {
266 	int max_key_idx;
267 
268 	if (pairwise)
269 		max_key_idx = 3;
270 	else if (wiphy_ext_feature_isset(&rdev->wiphy,
271 					 NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
272 		 wiphy_ext_feature_isset(&rdev->wiphy,
273 					 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
274 		max_key_idx = 7;
275 	else if (cfg80211_igtk_cipher_supported(rdev))
276 		max_key_idx = 5;
277 	else
278 		max_key_idx = 3;
279 
280 	if (key_idx < 0 || key_idx > max_key_idx)
281 		return false;
282 
283 	return true;
284 }
285 
286 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
287 				   struct wireless_dev *wdev,
288 				   struct key_params *params, int key_idx,
289 				   bool pairwise, const u8 *mac_addr)
290 {
291 	if (!cfg80211_valid_key_idx(rdev, key_idx, pairwise))
292 		return -EINVAL;
293 
294 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
295 		return -EINVAL;
296 
297 	if (pairwise && !mac_addr)
298 		return -EINVAL;
299 
300 	switch (params->cipher) {
301 	case WLAN_CIPHER_SUITE_TKIP:
302 		/* Extended Key ID can only be used with CCMP/GCMP ciphers */
303 		if ((pairwise && key_idx) ||
304 		    params->mode != NL80211_KEY_RX_TX)
305 			return -EINVAL;
306 		break;
307 	case WLAN_CIPHER_SUITE_CCMP:
308 	case WLAN_CIPHER_SUITE_CCMP_256:
309 	case WLAN_CIPHER_SUITE_GCMP:
310 	case WLAN_CIPHER_SUITE_GCMP_256:
311 		/* IEEE802.11-2016 allows only 0 and - when supporting
312 		 * Extended Key ID - 1 as index for pairwise keys.
313 		 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
314 		 * the driver supports Extended Key ID.
315 		 * @NL80211_KEY_SET_TX can't be set when installing and
316 		 * validating a key.
317 		 */
318 		if ((params->mode == NL80211_KEY_NO_TX && !pairwise) ||
319 		    params->mode == NL80211_KEY_SET_TX)
320 			return -EINVAL;
321 		if (wiphy_ext_feature_isset(&rdev->wiphy,
322 					    NL80211_EXT_FEATURE_EXT_KEY_ID)) {
323 			if (pairwise && (key_idx < 0 || key_idx > 1))
324 				return -EINVAL;
325 		} else if (pairwise && key_idx) {
326 			return -EINVAL;
327 		}
328 		break;
329 	case WLAN_CIPHER_SUITE_AES_CMAC:
330 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
331 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
332 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
333 		/* Disallow BIP (group-only) cipher as pairwise cipher */
334 		if (pairwise)
335 			return -EINVAL;
336 		if (key_idx < 4)
337 			return -EINVAL;
338 		break;
339 	case WLAN_CIPHER_SUITE_WEP40:
340 	case WLAN_CIPHER_SUITE_WEP104:
341 		if (key_idx > 3)
342 			return -EINVAL;
343 		break;
344 	default:
345 		break;
346 	}
347 
348 	/*
349 	 * Per Wi-Fi Aware v4.0 section 7.1.2, NAN Data interfaces
350 	 * shall only use CCMP-128 or GCMP-256.
351 	 */
352 	if (wdev->iftype == NL80211_IFTYPE_NAN_DATA &&
353 	    params->cipher != WLAN_CIPHER_SUITE_CCMP &&
354 	    params->cipher != WLAN_CIPHER_SUITE_GCMP_256)
355 		return -EINVAL;
356 
357 	switch (params->cipher) {
358 	case WLAN_CIPHER_SUITE_WEP40:
359 		if (params->key_len != WLAN_KEY_LEN_WEP40)
360 			return -EINVAL;
361 		break;
362 	case WLAN_CIPHER_SUITE_TKIP:
363 		if (params->key_len != WLAN_KEY_LEN_TKIP)
364 			return -EINVAL;
365 		break;
366 	case WLAN_CIPHER_SUITE_CCMP:
367 		if (params->key_len != WLAN_KEY_LEN_CCMP)
368 			return -EINVAL;
369 		break;
370 	case WLAN_CIPHER_SUITE_CCMP_256:
371 		if (params->key_len != WLAN_KEY_LEN_CCMP_256)
372 			return -EINVAL;
373 		break;
374 	case WLAN_CIPHER_SUITE_GCMP:
375 		if (params->key_len != WLAN_KEY_LEN_GCMP)
376 			return -EINVAL;
377 		break;
378 	case WLAN_CIPHER_SUITE_GCMP_256:
379 		if (params->key_len != WLAN_KEY_LEN_GCMP_256)
380 			return -EINVAL;
381 		break;
382 	case WLAN_CIPHER_SUITE_WEP104:
383 		if (params->key_len != WLAN_KEY_LEN_WEP104)
384 			return -EINVAL;
385 		break;
386 	case WLAN_CIPHER_SUITE_AES_CMAC:
387 		if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
388 			return -EINVAL;
389 		break;
390 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
391 		if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
392 			return -EINVAL;
393 		break;
394 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
395 		if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
396 			return -EINVAL;
397 		break;
398 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
399 		if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
400 			return -EINVAL;
401 		break;
402 	default:
403 		/*
404 		 * We don't know anything about this algorithm,
405 		 * allow using it -- but the driver must check
406 		 * all parameters! We still check below whether
407 		 * or not the driver supports this algorithm,
408 		 * of course.
409 		 */
410 		break;
411 	}
412 
413 	if (params->seq) {
414 		switch (params->cipher) {
415 		case WLAN_CIPHER_SUITE_WEP40:
416 		case WLAN_CIPHER_SUITE_WEP104:
417 			/* These ciphers do not use key sequence */
418 			return -EINVAL;
419 		case WLAN_CIPHER_SUITE_TKIP:
420 		case WLAN_CIPHER_SUITE_CCMP:
421 		case WLAN_CIPHER_SUITE_CCMP_256:
422 		case WLAN_CIPHER_SUITE_GCMP:
423 		case WLAN_CIPHER_SUITE_GCMP_256:
424 		case WLAN_CIPHER_SUITE_AES_CMAC:
425 		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
426 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
427 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
428 			if (params->seq_len != 6)
429 				return -EINVAL;
430 			break;
431 		}
432 	}
433 
434 	if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
435 		return -EINVAL;
436 
437 	if (params->ltf_keyseed) {
438 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
439 					     NL80211_EXT_FEATURE_SECURE_LTF) ||
440 		    !wiphy_ext_feature_isset(&rdev->wiphy,
441 					     NL80211_EXT_FEATURE_SET_KEY_LTF_SEED))
442 			return -EOPNOTSUPP;
443 
444 		/*
445 		 * LTF key seed is pairwise key material and must only be
446 		 * used with a pairwise key
447 		 */
448 		if (!pairwise)
449 			return -EINVAL;
450 	}
451 
452 	return 0;
453 }
454 
455 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
456 {
457 	unsigned int hdrlen = 24;
458 
459 	if (ieee80211_is_ext(fc)) {
460 		hdrlen = 4;
461 		goto out;
462 	}
463 
464 	if (ieee80211_is_data(fc)) {
465 		if (ieee80211_has_a4(fc))
466 			hdrlen = 30;
467 		if (ieee80211_is_data_qos(fc)) {
468 			hdrlen += IEEE80211_QOS_CTL_LEN;
469 			if (ieee80211_has_order(fc))
470 				hdrlen += IEEE80211_HT_CTL_LEN;
471 		}
472 		goto out;
473 	}
474 
475 	if (ieee80211_is_mgmt(fc)) {
476 		if (ieee80211_has_order(fc))
477 			hdrlen += IEEE80211_HT_CTL_LEN;
478 		goto out;
479 	}
480 
481 	if (ieee80211_is_ctl(fc)) {
482 		/*
483 		 * ACK and CTS are 10 bytes, all others 16. To see how
484 		 * to get this condition consider
485 		 *   subtype mask:   0b0000000011110000 (0x00F0)
486 		 *   ACK subtype:    0b0000000011010000 (0x00D0)
487 		 *   CTS subtype:    0b0000000011000000 (0x00C0)
488 		 *   bits that matter:         ^^^      (0x00E0)
489 		 *   value of those: 0b0000000011000000 (0x00C0)
490 		 */
491 		if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
492 			hdrlen = 10;
493 		else
494 			hdrlen = 16;
495 	}
496 out:
497 	return hdrlen;
498 }
499 EXPORT_SYMBOL(ieee80211_hdrlen);
500 
501 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
502 {
503 	const struct ieee80211_hdr *hdr =
504 			(const struct ieee80211_hdr *)skb->data;
505 	unsigned int hdrlen;
506 
507 	if (unlikely(skb->len < 10))
508 		return 0;
509 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
510 	if (unlikely(hdrlen > skb->len))
511 		return 0;
512 	return hdrlen;
513 }
514 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
515 
516 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
517 {
518 	int ae = flags & MESH_FLAGS_AE;
519 	/* 802.11-2012, 8.2.4.7.3 */
520 	switch (ae) {
521 	default:
522 	case 0:
523 		return 6;
524 	case MESH_FLAGS_AE_A4:
525 		return 12;
526 	case MESH_FLAGS_AE_A5_A6:
527 		return 18;
528 	}
529 }
530 
531 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
532 {
533 	return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
534 }
535 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
536 
537 bool ieee80211_get_8023_tunnel_proto(const void *hdr, __be16 *proto)
538 {
539 	const __be16 *hdr_proto = hdr + ETH_ALEN;
540 
541 	if (!(ether_addr_equal(hdr, rfc1042_header) &&
542 	      *hdr_proto != htons(ETH_P_AARP) &&
543 	      *hdr_proto != htons(ETH_P_IPX)) &&
544 	    !ether_addr_equal(hdr, bridge_tunnel_header))
545 		return false;
546 
547 	*proto = *hdr_proto;
548 
549 	return true;
550 }
551 EXPORT_SYMBOL(ieee80211_get_8023_tunnel_proto);
552 
553 int ieee80211_strip_8023_mesh_hdr(struct sk_buff *skb)
554 {
555 	const void *mesh_addr;
556 	struct {
557 		struct ethhdr eth;
558 		u8 flags;
559 	} payload;
560 	int hdrlen;
561 	int ret;
562 
563 	ret = skb_copy_bits(skb, 0, &payload, sizeof(payload));
564 	if (ret)
565 		return ret;
566 
567 	hdrlen = sizeof(payload.eth) + __ieee80211_get_mesh_hdrlen(payload.flags);
568 
569 	if (likely(pskb_may_pull(skb, hdrlen + 8) &&
570 		   ieee80211_get_8023_tunnel_proto(skb->data + hdrlen,
571 						   &payload.eth.h_proto)))
572 		hdrlen += ETH_ALEN + 2;
573 	else if (!pskb_may_pull(skb, hdrlen))
574 		return -EINVAL;
575 	else
576 		payload.eth.h_proto = htons(skb->len - hdrlen);
577 
578 	mesh_addr = skb->data + sizeof(payload.eth) + ETH_ALEN;
579 	switch (payload.flags & MESH_FLAGS_AE) {
580 	case MESH_FLAGS_AE_A4:
581 		memcpy(&payload.eth.h_source, mesh_addr, ETH_ALEN);
582 		break;
583 	case MESH_FLAGS_AE_A5_A6:
584 		memcpy(&payload.eth, mesh_addr, 2 * ETH_ALEN);
585 		break;
586 	default:
587 		break;
588 	}
589 
590 	pskb_pull(skb, hdrlen - sizeof(payload.eth));
591 	memcpy(skb->data, &payload.eth, sizeof(payload.eth));
592 
593 	return 0;
594 }
595 EXPORT_SYMBOL(ieee80211_strip_8023_mesh_hdr);
596 
597 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
598 				  const u8 *addr, enum nl80211_iftype iftype,
599 				  u8 data_offset, bool is_amsdu)
600 {
601 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
602 	struct {
603 		u8 hdr[ETH_ALEN] __aligned(2);
604 		__be16 proto;
605 	} payload;
606 	struct ethhdr tmp;
607 	u16 hdrlen;
608 
609 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
610 		return -1;
611 
612 	hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
613 	if (skb->len < hdrlen)
614 		return -1;
615 
616 	/* convert IEEE 802.11 header + possible LLC headers into Ethernet
617 	 * header
618 	 * IEEE 802.11 address fields:
619 	 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
620 	 *   0     0   DA    SA    BSSID n/a
621 	 *   0     1   DA    BSSID SA    n/a
622 	 *   1     0   BSSID SA    DA    n/a
623 	 *   1     1   RA    TA    DA    SA
624 	 */
625 	memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
626 	memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
627 
628 	switch (hdr->frame_control &
629 		cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
630 	case cpu_to_le16(IEEE80211_FCTL_TODS):
631 		if (unlikely(iftype != NL80211_IFTYPE_AP &&
632 			     iftype != NL80211_IFTYPE_AP_VLAN &&
633 			     iftype != NL80211_IFTYPE_P2P_GO))
634 			return -1;
635 		break;
636 	case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
637 		if (unlikely(iftype != NL80211_IFTYPE_MESH_POINT &&
638 			     iftype != NL80211_IFTYPE_AP_VLAN &&
639 			     iftype != NL80211_IFTYPE_STATION))
640 			return -1;
641 		break;
642 	case cpu_to_le16(IEEE80211_FCTL_FROMDS):
643 		if ((iftype != NL80211_IFTYPE_STATION &&
644 		     iftype != NL80211_IFTYPE_P2P_CLIENT &&
645 		     iftype != NL80211_IFTYPE_MESH_POINT) ||
646 		    (is_multicast_ether_addr(tmp.h_dest) &&
647 		     ether_addr_equal(tmp.h_source, addr)))
648 			return -1;
649 		break;
650 	case cpu_to_le16(0):
651 		if (iftype != NL80211_IFTYPE_ADHOC &&
652 		    iftype != NL80211_IFTYPE_STATION &&
653 		    iftype != NL80211_IFTYPE_OCB &&
654 		    iftype != NL80211_IFTYPE_NAN_DATA)
655 			return -1;
656 		break;
657 	}
658 
659 	if (likely(!is_amsdu && iftype != NL80211_IFTYPE_MESH_POINT &&
660 		   skb_copy_bits(skb, hdrlen, &payload, sizeof(payload)) == 0 &&
661 		   ieee80211_get_8023_tunnel_proto(&payload, &tmp.h_proto))) {
662 		/* remove RFC1042 or Bridge-Tunnel encapsulation */
663 		hdrlen += ETH_ALEN + 2;
664 		skb_postpull_rcsum(skb, &payload, ETH_ALEN + 2);
665 	} else {
666 		tmp.h_proto = htons(skb->len - hdrlen);
667 	}
668 
669 	pskb_pull(skb, hdrlen);
670 
671 	if (!ehdr)
672 		ehdr = skb_push(skb, sizeof(struct ethhdr));
673 	memcpy(ehdr, &tmp, sizeof(tmp));
674 
675 	return 0;
676 }
677 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
678 
679 static void
680 __frame_add_frag(struct sk_buff *skb, struct page *page,
681 		 void *ptr, int len, int size)
682 {
683 	struct skb_shared_info *sh = skb_shinfo(skb);
684 	int page_offset;
685 
686 	get_page(page);
687 	page_offset = ptr - page_address(page);
688 	skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
689 }
690 
691 static void
692 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
693 			    int offset, int len)
694 {
695 	struct skb_shared_info *sh = skb_shinfo(skb);
696 	const skb_frag_t *frag = &sh->frags[0];
697 	struct page *frag_page;
698 	void *frag_ptr;
699 	int frag_len, frag_size;
700 	int head_size = skb->len - skb->data_len;
701 	int cur_len;
702 
703 	frag_page = virt_to_head_page(skb->head);
704 	frag_ptr = skb->data;
705 	frag_size = head_size;
706 
707 	while (offset >= frag_size) {
708 		offset -= frag_size;
709 		frag_page = skb_frag_page(frag);
710 		frag_ptr = skb_frag_address(frag);
711 		frag_size = skb_frag_size(frag);
712 		frag++;
713 	}
714 
715 	frag_ptr += offset;
716 	frag_len = frag_size - offset;
717 
718 	cur_len = min(len, frag_len);
719 
720 	__frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
721 	len -= cur_len;
722 
723 	while (len > 0) {
724 		frag_len = skb_frag_size(frag);
725 		cur_len = min(len, frag_len);
726 		__frame_add_frag(frame, skb_frag_page(frag),
727 				 skb_frag_address(frag), cur_len, frag_len);
728 		len -= cur_len;
729 		frag++;
730 	}
731 }
732 
733 static struct sk_buff *
734 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
735 		       int offset, int len, bool reuse_frag,
736 		       int min_len)
737 {
738 	struct sk_buff *frame;
739 	int cur_len = len;
740 
741 	if (skb->len - offset < len)
742 		return NULL;
743 
744 	/*
745 	 * When reusing fragments, copy some data to the head to simplify
746 	 * ethernet header handling and speed up protocol header processing
747 	 * in the stack later.
748 	 */
749 	if (reuse_frag)
750 		cur_len = min_t(int, len, min_len);
751 
752 	/*
753 	 * Allocate and reserve two bytes more for payload
754 	 * alignment since sizeof(struct ethhdr) is 14.
755 	 */
756 	frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
757 	if (!frame)
758 		return NULL;
759 
760 	frame->priority = skb->priority;
761 	skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
762 	skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
763 
764 	len -= cur_len;
765 	if (!len)
766 		return frame;
767 
768 	offset += cur_len;
769 	__ieee80211_amsdu_copy_frag(skb, frame, offset, len);
770 
771 	return frame;
772 }
773 
774 static u16
775 ieee80211_amsdu_subframe_length(void *field, u8 mesh_flags, u8 hdr_type)
776 {
777 	__le16 *field_le = field;
778 	__be16 *field_be = field;
779 	u16 len;
780 
781 	if (hdr_type >= 2)
782 		len = le16_to_cpu(*field_le);
783 	else
784 		len = be16_to_cpu(*field_be);
785 	if (hdr_type)
786 		len += __ieee80211_get_mesh_hdrlen(mesh_flags);
787 
788 	return len;
789 }
790 
791 bool ieee80211_is_valid_amsdu(struct sk_buff *skb, u8 mesh_hdr)
792 {
793 	int offset = 0, subframe_len, padding;
794 
795 	for (offset = 0; offset < skb->len; offset += subframe_len + padding) {
796 		int remaining = skb->len - offset;
797 		struct {
798 		    __be16 len;
799 		    u8 mesh_flags;
800 		} hdr;
801 		u16 len;
802 
803 		if (sizeof(hdr) > remaining)
804 			return false;
805 
806 		if (skb_copy_bits(skb, offset + 2 * ETH_ALEN, &hdr, sizeof(hdr)) < 0)
807 			return false;
808 
809 		len = ieee80211_amsdu_subframe_length(&hdr.len, hdr.mesh_flags,
810 						      mesh_hdr);
811 		subframe_len = sizeof(struct ethhdr) + len;
812 		padding = (4 - subframe_len) & 0x3;
813 
814 		if (subframe_len > remaining)
815 			return false;
816 	}
817 
818 	return true;
819 }
820 EXPORT_SYMBOL(ieee80211_is_valid_amsdu);
821 
822 
823 /*
824  * Detects if an MSDU frame was maliciously converted into an A-MSDU
825  * frame by an adversary. This is done by parsing the received frame
826  * as if it were a regular MSDU, even though the A-MSDU flag is set.
827  *
828  * For non-mesh interfaces, detection involves checking whether the
829  * payload, when interpreted as an MSDU, begins with a valid RFC1042
830  * header. This is done by comparing the A-MSDU subheader's destination
831  * address to the start of the RFC1042 header.
832  *
833  * For mesh interfaces, the MSDU includes a 6-byte Mesh Control field
834  * and an optional variable-length Mesh Address Extension field before
835  * the RFC1042 header. The position of the RFC1042 header must therefore
836  * be calculated based on the mesh header length.
837  *
838  * Since this function intentionally parses an A-MSDU frame as an MSDU,
839  * it only assumes that the A-MSDU subframe header is present, and
840  * beyond this it performs its own bounds checks under the assumption
841  * that the frame is instead parsed as a non-aggregated MSDU.
842  */
843 static bool
844 is_amsdu_aggregation_attack(struct ethhdr *eth, struct sk_buff *skb,
845 			    enum nl80211_iftype iftype)
846 {
847 	int offset;
848 
849 	/* Non-mesh case can be directly compared */
850 	if (iftype != NL80211_IFTYPE_MESH_POINT)
851 		return ether_addr_equal(eth->h_dest, rfc1042_header);
852 
853 	offset = __ieee80211_get_mesh_hdrlen(eth->h_dest[0]);
854 	if (offset == 6) {
855 		/* Mesh case with empty address extension field */
856 		return ether_addr_equal(eth->h_source, rfc1042_header);
857 	} else if (offset + ETH_ALEN <= skb->len) {
858 		/* Mesh case with non-empty address extension field */
859 		u8 temp[ETH_ALEN];
860 
861 		skb_copy_bits(skb, offset, temp, ETH_ALEN);
862 		return ether_addr_equal(temp, rfc1042_header);
863 	}
864 
865 	return false;
866 }
867 
868 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
869 			      const u8 *addr, enum nl80211_iftype iftype,
870 			      const unsigned int extra_headroom,
871 			      const u8 *check_da, const u8 *check_sa,
872 			      u8 mesh_control)
873 {
874 	unsigned int hlen = ALIGN(extra_headroom, 4);
875 	struct sk_buff *frame = NULL;
876 	int offset = 0;
877 	struct {
878 		struct ethhdr eth;
879 		uint8_t flags;
880 	} hdr;
881 	bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
882 	bool reuse_skb = false;
883 	bool last = false;
884 	int copy_len = sizeof(hdr.eth);
885 
886 	if (iftype == NL80211_IFTYPE_MESH_POINT)
887 		copy_len = sizeof(hdr);
888 
889 	while (!last) {
890 		int remaining = skb->len - offset;
891 		unsigned int subframe_len;
892 		int len, mesh_len = 0;
893 		u8 padding;
894 
895 		if (copy_len > remaining)
896 			goto purge;
897 
898 		skb_copy_bits(skb, offset, &hdr, copy_len);
899 		if (iftype == NL80211_IFTYPE_MESH_POINT)
900 			mesh_len = __ieee80211_get_mesh_hdrlen(hdr.flags);
901 		len = ieee80211_amsdu_subframe_length(&hdr.eth.h_proto, hdr.flags,
902 						      mesh_control);
903 		subframe_len = sizeof(struct ethhdr) + len;
904 		padding = (4 - subframe_len) & 0x3;
905 
906 		/* the last MSDU has no padding */
907 		if (subframe_len > remaining)
908 			goto purge;
909 		/* mitigate A-MSDU aggregation injection attacks, to be
910 		 * checked when processing first subframe (offset == 0).
911 		 */
912 		if (offset == 0 && is_amsdu_aggregation_attack(&hdr.eth, skb, iftype))
913 			goto purge;
914 
915 		offset += sizeof(struct ethhdr);
916 		last = remaining <= subframe_len + padding;
917 
918 		/* FIXME: should we really accept multicast DA? */
919 		if ((check_da && !is_multicast_ether_addr(hdr.eth.h_dest) &&
920 		     !ether_addr_equal(check_da, hdr.eth.h_dest)) ||
921 		    (check_sa && !ether_addr_equal(check_sa, hdr.eth.h_source))) {
922 			offset += len + padding;
923 			continue;
924 		}
925 
926 		/* reuse skb for the last subframe */
927 		if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
928 			skb_pull(skb, offset);
929 			frame = skb;
930 			reuse_skb = true;
931 		} else {
932 			frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
933 						       reuse_frag, 32 + mesh_len);
934 			if (!frame)
935 				goto purge;
936 
937 			offset += len + padding;
938 		}
939 
940 		skb_reset_network_header(frame);
941 		frame->dev = skb->dev;
942 		frame->priority = skb->priority;
943 
944 		if (likely(iftype != NL80211_IFTYPE_MESH_POINT &&
945 			   ieee80211_get_8023_tunnel_proto(frame->data, &hdr.eth.h_proto)))
946 			skb_pull(frame, ETH_ALEN + 2);
947 
948 		memcpy(skb_push(frame, sizeof(hdr.eth)), &hdr.eth, sizeof(hdr.eth));
949 		__skb_queue_tail(list, frame);
950 	}
951 
952 	if (!reuse_skb)
953 		dev_kfree_skb(skb);
954 
955 	return;
956 
957  purge:
958 	__skb_queue_purge(list);
959 	dev_kfree_skb(skb);
960 }
961 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
962 
963 /* Given a data frame determine the 802.1p/1d tag to use. */
964 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
965 				    struct cfg80211_qos_map *qos_map)
966 {
967 	unsigned int dscp;
968 	unsigned char vlan_priority;
969 	unsigned int ret;
970 
971 	/* skb->priority values from 256->263 are magic values to
972 	 * directly indicate a specific 802.1d priority.  This is used
973 	 * to allow 802.1d priority to be passed directly in from VLAN
974 	 * tags, etc.
975 	 */
976 	if (skb->priority >= 256 && skb->priority <= 263) {
977 		ret = skb->priority - 256;
978 		goto out;
979 	}
980 
981 	if (skb_vlan_tag_present(skb)) {
982 		vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
983 			>> VLAN_PRIO_SHIFT;
984 		if (vlan_priority > 0) {
985 			ret = vlan_priority;
986 			goto out;
987 		}
988 	}
989 
990 	switch (skb->protocol) {
991 	case htons(ETH_P_IP):
992 		dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
993 		break;
994 	case htons(ETH_P_IPV6):
995 		dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
996 		break;
997 	case htons(ETH_P_MPLS_UC):
998 	case htons(ETH_P_MPLS_MC): {
999 		struct mpls_label mpls_tmp, *mpls;
1000 
1001 		mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
1002 					  sizeof(*mpls), &mpls_tmp);
1003 		if (!mpls)
1004 			return 0;
1005 
1006 		ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
1007 			>> MPLS_LS_TC_SHIFT;
1008 		goto out;
1009 	}
1010 	case htons(ETH_P_80221):
1011 		/* 802.21 is always network control traffic */
1012 		return 7;
1013 	default:
1014 		return 0;
1015 	}
1016 
1017 	if (qos_map) {
1018 		unsigned int i, tmp_dscp = dscp >> 2;
1019 
1020 		for (i = 0; i < qos_map->num_des; i++) {
1021 			if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
1022 				ret = qos_map->dscp_exception[i].up;
1023 				goto out;
1024 			}
1025 		}
1026 
1027 		for (i = 0; i < 8; i++) {
1028 			if (tmp_dscp >= qos_map->up[i].low &&
1029 			    tmp_dscp <= qos_map->up[i].high) {
1030 				ret = i;
1031 				goto out;
1032 			}
1033 		}
1034 	}
1035 
1036 	/* The default mapping as defined Section 2.3 in RFC8325: The three
1037 	 * Most Significant Bits (MSBs) of the DSCP are used as the
1038 	 * corresponding L2 markings.
1039 	 */
1040 	ret = dscp >> 5;
1041 
1042 	/* Handle specific DSCP values for which the default mapping (as
1043 	 * described above) doesn't adhere to the intended usage of the DSCP
1044 	 * value. See section 4 in RFC8325. Specifically, for the following
1045 	 * Diffserv Service Classes no update is needed:
1046 	 * - Standard: DF
1047 	 * - Low Priority Data: CS1
1048 	 * - Multimedia Conferencing: AF41, AF42, AF43
1049 	 * - Network Control Traffic: CS7
1050 	 * - Real-Time Interactive: CS4
1051 	 * - Signaling: CS5
1052 	 */
1053 	switch (dscp >> 2) {
1054 	case 10:
1055 	case 12:
1056 	case 14:
1057 		/* High throughput data: AF11, AF12, AF13 */
1058 		ret = 0;
1059 		break;
1060 	case 16:
1061 		/* Operations, Administration, and Maintenance and Provisioning:
1062 		 * CS2
1063 		 */
1064 		ret = 0;
1065 		break;
1066 	case 18:
1067 	case 20:
1068 	case 22:
1069 		/* Low latency data: AF21, AF22, AF23 */
1070 		ret = 3;
1071 		break;
1072 	case 24:
1073 		/* Broadcasting video: CS3 */
1074 		ret = 4;
1075 		break;
1076 	case 26:
1077 	case 28:
1078 	case 30:
1079 		/* Multimedia Streaming: AF31, AF32, AF33 */
1080 		ret = 4;
1081 		break;
1082 	case 44:
1083 		/* Voice Admit: VA */
1084 		ret = 6;
1085 		break;
1086 	case 46:
1087 		/* Telephony traffic: EF */
1088 		ret = 6;
1089 		break;
1090 	case 48:
1091 		/* Network Control Traffic: CS6 */
1092 		ret = 7;
1093 		break;
1094 	}
1095 out:
1096 	return array_index_nospec(ret, IEEE80211_NUM_TIDS);
1097 }
1098 EXPORT_SYMBOL(cfg80211_classify8021d);
1099 
1100 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
1101 {
1102 	const struct cfg80211_bss_ies *ies;
1103 
1104 	ies = rcu_dereference(bss->ies);
1105 	if (!ies)
1106 		return NULL;
1107 
1108 	return cfg80211_find_elem(id, ies->data, ies->len);
1109 }
1110 EXPORT_SYMBOL(ieee80211_bss_get_elem);
1111 
1112 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
1113 {
1114 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1115 	struct net_device *dev = wdev->netdev;
1116 	int i;
1117 
1118 	if (!wdev->connect_keys)
1119 		return;
1120 
1121 	for (i = 0; i < 4; i++) {
1122 		if (!wdev->connect_keys->params[i].cipher)
1123 			continue;
1124 		if (rdev_add_key(rdev, wdev, -1, i, false, NULL,
1125 				 &wdev->connect_keys->params[i])) {
1126 			netdev_err(dev, "failed to set key %d\n", i);
1127 			continue;
1128 		}
1129 		if (wdev->connect_keys->def == i &&
1130 		    rdev_set_default_key(rdev, dev, -1, i, true, true)) {
1131 			netdev_err(dev, "failed to set defkey %d\n", i);
1132 			continue;
1133 		}
1134 	}
1135 
1136 	kfree_sensitive(wdev->connect_keys);
1137 	wdev->connect_keys = NULL;
1138 }
1139 
1140 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
1141 {
1142 	struct cfg80211_event *ev;
1143 	unsigned long flags;
1144 
1145 	spin_lock_irqsave(&wdev->event_lock, flags);
1146 	while (!list_empty(&wdev->event_list)) {
1147 		ev = list_first_entry(&wdev->event_list,
1148 				      struct cfg80211_event, list);
1149 		list_del(&ev->list);
1150 		spin_unlock_irqrestore(&wdev->event_lock, flags);
1151 
1152 		switch (ev->type) {
1153 		case EVENT_CONNECT_RESULT:
1154 			__cfg80211_connect_result(
1155 				wdev->netdev,
1156 				&ev->cr,
1157 				ev->cr.status == WLAN_STATUS_SUCCESS);
1158 			break;
1159 		case EVENT_ROAMED:
1160 			__cfg80211_roamed(wdev, &ev->rm);
1161 			break;
1162 		case EVENT_DISCONNECTED:
1163 			__cfg80211_disconnected(wdev->netdev,
1164 						ev->dc.ie, ev->dc.ie_len,
1165 						ev->dc.reason,
1166 						!ev->dc.locally_generated);
1167 			break;
1168 		case EVENT_IBSS_JOINED:
1169 			__cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
1170 					       ev->ij.channel);
1171 			break;
1172 		case EVENT_STOPPED:
1173 			/*
1174 			 * for NAN interfaces cfg80211_leave must be called but
1175 			 * locking here doesn't allow this.
1176 			 */
1177 			if (WARN_ON(wdev->iftype == NL80211_IFTYPE_NAN))
1178 				break;
1179 
1180 			cfg80211_leave_locked(wiphy_to_rdev(wdev->wiphy), wdev,
1181 					      ev->link_id);
1182 			break;
1183 		case EVENT_PORT_AUTHORIZED:
1184 			__cfg80211_port_authorized(wdev, ev->pa.peer_addr,
1185 						   ev->pa.td_bitmap,
1186 						   ev->pa.td_bitmap_len);
1187 			break;
1188 		}
1189 
1190 		kfree(ev);
1191 
1192 		spin_lock_irqsave(&wdev->event_lock, flags);
1193 	}
1194 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1195 }
1196 
1197 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
1198 {
1199 	struct wireless_dev *wdev;
1200 
1201 	lockdep_assert_held(&rdev->wiphy.mtx);
1202 
1203 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
1204 		cfg80211_process_wdev_events(wdev);
1205 }
1206 
1207 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
1208 			  struct net_device *dev, enum nl80211_iftype ntype,
1209 			  struct vif_params *params)
1210 {
1211 	int err;
1212 	enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
1213 
1214 	lockdep_assert_held(&rdev->wiphy.mtx);
1215 
1216 	/* don't support changing VLANs, you just re-create them */
1217 	if (otype == NL80211_IFTYPE_AP_VLAN)
1218 		return -EOPNOTSUPP;
1219 
1220 	/*
1221 	 * for NAN interfaces cfg80211_leave must be called for leaving,
1222 	 * but locking here doesn't allow this.
1223 	 */
1224 	if (otype == NL80211_IFTYPE_NAN)
1225 		return -EOPNOTSUPP;
1226 
1227 	/* cannot change into P2P device or NAN */
1228 	if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
1229 	    ntype == NL80211_IFTYPE_NAN ||
1230 	    ntype == NL80211_IFTYPE_PD)
1231 		return -EOPNOTSUPP;
1232 
1233 	if (!rdev->ops->change_virtual_intf ||
1234 	    !(rdev->wiphy.interface_modes & (1 << ntype)))
1235 		return -EOPNOTSUPP;
1236 
1237 	if (ntype != otype) {
1238 		/* if it's part of a bridge, reject changing type to station/ibss */
1239 		if (netif_is_bridge_port(dev) &&
1240 		    (ntype == NL80211_IFTYPE_ADHOC ||
1241 		     ntype == NL80211_IFTYPE_STATION ||
1242 		     ntype == NL80211_IFTYPE_P2P_CLIENT))
1243 			return -EBUSY;
1244 
1245 		dev->ieee80211_ptr->use_4addr = false;
1246 		rdev_set_qos_map(rdev, dev, NULL);
1247 
1248 		cfg80211_leave_locked(rdev, dev->ieee80211_ptr, -1);
1249 
1250 		cfg80211_process_rdev_events(rdev);
1251 		cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
1252 
1253 		memset(&dev->ieee80211_ptr->u, 0,
1254 		       sizeof(dev->ieee80211_ptr->u));
1255 		memset(&dev->ieee80211_ptr->links, 0,
1256 		       sizeof(dev->ieee80211_ptr->links));
1257 	}
1258 
1259 	err = rdev_change_virtual_intf(rdev, dev, ntype, params);
1260 
1261 	WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1262 
1263 	if (!err && params && params->use_4addr != -1)
1264 		dev->ieee80211_ptr->use_4addr = params->use_4addr;
1265 
1266 	if (!err) {
1267 		dev->priv_flags &= ~IFF_DONT_BRIDGE;
1268 		switch (ntype) {
1269 		case NL80211_IFTYPE_STATION:
1270 			if (dev->ieee80211_ptr->use_4addr)
1271 				break;
1272 			fallthrough;
1273 		case NL80211_IFTYPE_OCB:
1274 		case NL80211_IFTYPE_P2P_CLIENT:
1275 		case NL80211_IFTYPE_ADHOC:
1276 		case NL80211_IFTYPE_NAN_DATA:
1277 			dev->priv_flags |= IFF_DONT_BRIDGE;
1278 			break;
1279 		case NL80211_IFTYPE_P2P_GO:
1280 		case NL80211_IFTYPE_AP:
1281 		case NL80211_IFTYPE_AP_VLAN:
1282 		case NL80211_IFTYPE_MESH_POINT:
1283 			/* bridging OK */
1284 			break;
1285 		case NL80211_IFTYPE_MONITOR:
1286 			/* monitor can't bridge anyway */
1287 			break;
1288 		case NL80211_IFTYPE_UNSPECIFIED:
1289 		case NUM_NL80211_IFTYPES:
1290 			/* not happening */
1291 			break;
1292 		case NL80211_IFTYPE_P2P_DEVICE:
1293 		case NL80211_IFTYPE_WDS:
1294 		case NL80211_IFTYPE_NAN:
1295 		case NL80211_IFTYPE_PD:
1296 			WARN_ON(1);
1297 			break;
1298 		}
1299 	}
1300 
1301 	if (!err && ntype != otype && netif_running(dev)) {
1302 		cfg80211_update_iface_num(rdev, ntype, 1);
1303 		cfg80211_update_iface_num(rdev, otype, -1);
1304 	}
1305 
1306 	return err;
1307 }
1308 
1309 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1310 {
1311 	int modulation, streams, bitrate;
1312 
1313 	/* the formula below does only work for MCS values smaller than 32 */
1314 	if (WARN_ON_ONCE(rate->mcs >= 32))
1315 		return 0;
1316 
1317 	modulation = rate->mcs & 7;
1318 	streams = (rate->mcs >> 3) + 1;
1319 
1320 	bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1321 
1322 	if (modulation < 4)
1323 		bitrate *= (modulation + 1);
1324 	else if (modulation == 4)
1325 		bitrate *= (modulation + 2);
1326 	else
1327 		bitrate *= (modulation + 3);
1328 
1329 	bitrate *= streams;
1330 
1331 	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1332 		bitrate = (bitrate / 9) * 10;
1333 
1334 	/* do NOT round down here */
1335 	return (bitrate + 50000) / 100000;
1336 }
1337 
1338 static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
1339 {
1340 	static const u32 __mcs2bitrate[] = {
1341 		/* control PHY */
1342 		[0] =   275,
1343 		/* SC PHY */
1344 		[1] =  3850,
1345 		[2] =  7700,
1346 		[3] =  9625,
1347 		[4] = 11550,
1348 		[5] = 12512, /* 1251.25 mbps */
1349 		[6] = 15400,
1350 		[7] = 19250,
1351 		[8] = 23100,
1352 		[9] = 25025,
1353 		[10] = 30800,
1354 		[11] = 38500,
1355 		[12] = 46200,
1356 		/* OFDM PHY */
1357 		[13] =  6930,
1358 		[14] =  8662, /* 866.25 mbps */
1359 		[15] = 13860,
1360 		[16] = 17325,
1361 		[17] = 20790,
1362 		[18] = 27720,
1363 		[19] = 34650,
1364 		[20] = 41580,
1365 		[21] = 45045,
1366 		[22] = 51975,
1367 		[23] = 62370,
1368 		[24] = 67568, /* 6756.75 mbps */
1369 		/* LP-SC PHY */
1370 		[25] =  6260,
1371 		[26] =  8340,
1372 		[27] = 11120,
1373 		[28] = 12510,
1374 		[29] = 16680,
1375 		[30] = 22240,
1376 		[31] = 25030,
1377 	};
1378 
1379 	if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1380 		return 0;
1381 
1382 	return __mcs2bitrate[rate->mcs];
1383 }
1384 
1385 static u32 cfg80211_calculate_bitrate_extended_sc_dmg(struct rate_info *rate)
1386 {
1387 	static const u32 __mcs2bitrate[] = {
1388 		[6 - 6] = 26950, /* MCS 9.1 : 2695.0 mbps */
1389 		[7 - 6] = 50050, /* MCS 12.1 */
1390 		[8 - 6] = 53900,
1391 		[9 - 6] = 57750,
1392 		[10 - 6] = 63900,
1393 		[11 - 6] = 75075,
1394 		[12 - 6] = 80850,
1395 	};
1396 
1397 	/* Extended SC MCS not defined for base MCS below 6 or above 12 */
1398 	if (WARN_ON_ONCE(rate->mcs < 6 || rate->mcs > 12))
1399 		return 0;
1400 
1401 	return __mcs2bitrate[rate->mcs - 6];
1402 }
1403 
1404 static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
1405 {
1406 	static const u32 __mcs2bitrate[] = {
1407 		/* control PHY */
1408 		[0] =   275,
1409 		/* SC PHY */
1410 		[1] =  3850,
1411 		[2] =  7700,
1412 		[3] =  9625,
1413 		[4] = 11550,
1414 		[5] = 12512, /* 1251.25 mbps */
1415 		[6] = 13475,
1416 		[7] = 15400,
1417 		[8] = 19250,
1418 		[9] = 23100,
1419 		[10] = 25025,
1420 		[11] = 26950,
1421 		[12] = 30800,
1422 		[13] = 38500,
1423 		[14] = 46200,
1424 		[15] = 50050,
1425 		[16] = 53900,
1426 		[17] = 57750,
1427 		[18] = 69300,
1428 		[19] = 75075,
1429 		[20] = 80850,
1430 	};
1431 
1432 	if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1433 		return 0;
1434 
1435 	return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
1436 }
1437 
1438 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1439 {
1440 	static const u32 base[4][12] = {
1441 		{   6500000,
1442 		   13000000,
1443 		   19500000,
1444 		   26000000,
1445 		   39000000,
1446 		   52000000,
1447 		   58500000,
1448 		   65000000,
1449 		   78000000,
1450 		/* not in the spec, but some devices use this: */
1451 		   86700000,
1452 		   97500000,
1453 		  108300000,
1454 		},
1455 		{  13500000,
1456 		   27000000,
1457 		   40500000,
1458 		   54000000,
1459 		   81000000,
1460 		  108000000,
1461 		  121500000,
1462 		  135000000,
1463 		  162000000,
1464 		  180000000,
1465 		  202500000,
1466 		  225000000,
1467 		},
1468 		{  29300000,
1469 		   58500000,
1470 		   87800000,
1471 		  117000000,
1472 		  175500000,
1473 		  234000000,
1474 		  263300000,
1475 		  292500000,
1476 		  351000000,
1477 		  390000000,
1478 		  438800000,
1479 		  487500000,
1480 		},
1481 		{  58500000,
1482 		  117000000,
1483 		  175500000,
1484 		  234000000,
1485 		  351000000,
1486 		  468000000,
1487 		  526500000,
1488 		  585000000,
1489 		  702000000,
1490 		  780000000,
1491 		  877500000,
1492 		  975000000,
1493 		},
1494 	};
1495 	u32 bitrate;
1496 	int idx;
1497 
1498 	if (rate->mcs > 11)
1499 		goto warn;
1500 
1501 	switch (rate->bw) {
1502 	case RATE_INFO_BW_160:
1503 		idx = 3;
1504 		break;
1505 	case RATE_INFO_BW_80:
1506 		idx = 2;
1507 		break;
1508 	case RATE_INFO_BW_40:
1509 		idx = 1;
1510 		break;
1511 	case RATE_INFO_BW_5:
1512 	case RATE_INFO_BW_10:
1513 	default:
1514 		goto warn;
1515 	case RATE_INFO_BW_20:
1516 		idx = 0;
1517 	}
1518 
1519 	bitrate = base[idx][rate->mcs];
1520 	bitrate *= rate->nss;
1521 
1522 	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1523 		bitrate = (bitrate / 9) * 10;
1524 
1525 	/* do NOT round down here */
1526 	return (bitrate + 50000) / 100000;
1527  warn:
1528 	WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1529 		  rate->bw, rate->mcs, rate->nss);
1530 	return 0;
1531 }
1532 
1533 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1534 {
1535 #define SCALE 6144
1536 	u32 mcs_divisors[14] = {
1537 		102399, /* 16.666666... */
1538 		 51201, /*  8.333333... */
1539 		 34134, /*  5.555555... */
1540 		 25599, /*  4.166666... */
1541 		 17067, /*  2.777777... */
1542 		 12801, /*  2.083333... */
1543 		 11377, /*  1.851725... */
1544 		 10239, /*  1.666666... */
1545 		  8532, /*  1.388888... */
1546 		  7680, /*  1.250000... */
1547 		  6828, /*  1.111111... */
1548 		  6144, /*  1.000000... */
1549 		  5690, /*  0.926106... */
1550 		  5120, /*  0.833333... */
1551 	};
1552 	u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1553 	u32 rates_996[3] =  { 480388888, 453700000, 408333333 };
1554 	u32 rates_484[3] =  { 229411111, 216666666, 195000000 };
1555 	u32 rates_242[3] =  { 114711111, 108333333,  97500000 };
1556 	u32 rates_106[3] =  {  40000000,  37777777,  34000000 };
1557 	u32 rates_52[3]  =  {  18820000,  17777777,  16000000 };
1558 	u32 rates_26[3]  =  {   9411111,   8888888,   8000000 };
1559 	u64 tmp;
1560 	u32 result;
1561 
1562 	if (WARN_ON_ONCE(rate->mcs > 13))
1563 		return 0;
1564 
1565 	if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1566 		return 0;
1567 	if (WARN_ON_ONCE(rate->he_ru_alloc >
1568 			 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1569 		return 0;
1570 	if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1571 		return 0;
1572 
1573 	if (rate->bw == RATE_INFO_BW_160 ||
1574 	    (rate->bw == RATE_INFO_BW_HE_RU &&
1575 	     rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1576 		result = rates_160M[rate->he_gi];
1577 	else if (rate->bw == RATE_INFO_BW_80 ||
1578 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1579 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1580 		result = rates_996[rate->he_gi];
1581 	else if (rate->bw == RATE_INFO_BW_40 ||
1582 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1583 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1584 		result = rates_484[rate->he_gi];
1585 	else if (rate->bw == RATE_INFO_BW_20 ||
1586 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1587 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1588 		result = rates_242[rate->he_gi];
1589 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1590 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1591 		result = rates_106[rate->he_gi];
1592 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1593 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1594 		result = rates_52[rate->he_gi];
1595 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1596 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1597 		result = rates_26[rate->he_gi];
1598 	else {
1599 		WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1600 		     rate->bw, rate->he_ru_alloc);
1601 		return 0;
1602 	}
1603 
1604 	/* now scale to the appropriate MCS */
1605 	tmp = result;
1606 	tmp *= SCALE;
1607 	do_div(tmp, mcs_divisors[rate->mcs]);
1608 
1609 	/* and take NSS, DCM into account */
1610 	tmp *= rate->nss;
1611 	do_div(tmp, 8);
1612 	if (rate->he_dcm)
1613 		do_div(tmp, 2);
1614 
1615 	result = tmp;
1616 
1617 	return result / 10000;
1618 }
1619 
1620 static u32 _cfg80211_calculate_bitrate_eht_uhr(struct rate_info *rate)
1621 {
1622 #define SCALE 6144
1623 	static const u32 mcs_divisors[] = {
1624 		[ 0] = 102399, /* 16.666666... */
1625 		[ 1] =  51201, /*  8.333333... */
1626 		[ 2] =  34134, /*  5.555555... */
1627 		[ 3] =  25599, /*  4.166666... */
1628 		[ 4] =  17067, /*  2.777777... */
1629 		[ 5] =  12801, /*  2.083333... */
1630 		[ 6] =  11377, /*  1.851725... */
1631 		[ 7] =  10239, /*  1.666666... */
1632 		[ 8] =   8532, /*  1.388888... */
1633 		[ 9] =   7680, /*  1.250000... */
1634 		[10] =   6828, /*  1.111111... */
1635 		[11] =   6144, /*  1.000000... */
1636 		[12] =   5690, /*  0.926106... */
1637 		[13] =   5120, /*  0.833333... */
1638 		[14] = 409600, /* 66.666666... */
1639 		[15] = 204800, /* 33.333333... */
1640 		[17] =  38400, /*  6.250180... */
1641 		[19] =  19200, /*  3.125090... */
1642 		[20] =  15360, /*  2.500000... */
1643 		[23] =   9600, /*  1.562545... */
1644 	};
1645 	static const u32 rates_996[3] =  { 480388888, 453700000, 408333333 };
1646 	static const u32 rates_484[3] =  { 229411111, 216666666, 195000000 };
1647 	static const u32 rates_242[3] =  { 114711111, 108333333,  97500000 };
1648 	static const u32 rates_106[3] =  {  40000000,  37777777,  34000000 };
1649 	static const u32 rates_52[3]  =  {  18820000,  17777777,  16000000 };
1650 	static const u32 rates_26[3]  =  {   9411111,   8888888,   8000000 };
1651 	u64 tmp;
1652 	u32 result;
1653 
1654 	if (WARN_ON_ONCE(rate->eht_gi > NL80211_RATE_INFO_EHT_GI_3_2))
1655 		return 0;
1656 	if (WARN_ON_ONCE(rate->eht_ru_alloc >
1657 			 NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1658 		return 0;
1659 	if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1660 		return 0;
1661 
1662 	/* Bandwidth checks for MCS 14 */
1663 	if (rate->mcs == 14) {
1664 		if ((rate->bw != RATE_INFO_BW_EHT_RU &&
1665 		     rate->bw != RATE_INFO_BW_80 &&
1666 		     rate->bw != RATE_INFO_BW_160 &&
1667 		     rate->bw != RATE_INFO_BW_320) ||
1668 		    (rate->bw == RATE_INFO_BW_EHT_RU &&
1669 		     rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_996 &&
1670 		     rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_2x996 &&
1671 		     rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) {
1672 			WARN(1, "invalid EHT BW for MCS 14: bw:%d, ru:%d\n",
1673 			     rate->bw, rate->eht_ru_alloc);
1674 			return 0;
1675 		}
1676 	}
1677 
1678 	if (rate->bw == RATE_INFO_BW_320 ||
1679 	    (rate->bw == RATE_INFO_BW_EHT_RU &&
1680 	     rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1681 		result = 4 * rates_996[rate->eht_gi];
1682 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1683 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484)
1684 		result = 3 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1685 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1686 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996)
1687 		result = 3 * rates_996[rate->eht_gi];
1688 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1689 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484)
1690 		result = 2 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1691 	else if (rate->bw == RATE_INFO_BW_160 ||
1692 		 (rate->bw == RATE_INFO_BW_EHT_RU &&
1693 		  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996))
1694 		result = 2 * rates_996[rate->eht_gi];
1695 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1696 		 rate->eht_ru_alloc ==
1697 		 NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242)
1698 		result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi]
1699 			 + rates_242[rate->eht_gi];
1700 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1701 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996P484)
1702 		result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1703 	else if (rate->bw == RATE_INFO_BW_80 ||
1704 		 (rate->bw == RATE_INFO_BW_EHT_RU &&
1705 		  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996))
1706 		result = rates_996[rate->eht_gi];
1707 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1708 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484P242)
1709 		result = rates_484[rate->eht_gi] + rates_242[rate->eht_gi];
1710 	else if (rate->bw == RATE_INFO_BW_40 ||
1711 		 (rate->bw == RATE_INFO_BW_EHT_RU &&
1712 		  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484))
1713 		result = rates_484[rate->eht_gi];
1714 	else if (rate->bw == RATE_INFO_BW_20 ||
1715 		 (rate->bw == RATE_INFO_BW_EHT_RU &&
1716 		  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_242))
1717 		result = rates_242[rate->eht_gi];
1718 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1719 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106P26)
1720 		result = rates_106[rate->eht_gi] + rates_26[rate->eht_gi];
1721 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1722 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106)
1723 		result = rates_106[rate->eht_gi];
1724 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1725 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52P26)
1726 		result = rates_52[rate->eht_gi] + rates_26[rate->eht_gi];
1727 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1728 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52)
1729 		result = rates_52[rate->eht_gi];
1730 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1731 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_26)
1732 		result = rates_26[rate->eht_gi];
1733 	else {
1734 		WARN(1, "invalid EHT or UHR MCS: bw:%d, ru:%d\n",
1735 		     rate->bw, rate->eht_ru_alloc);
1736 		return 0;
1737 	}
1738 
1739 	/* now scale to the appropriate MCS */
1740 	tmp = result;
1741 	tmp *= SCALE;
1742 	do_div(tmp, mcs_divisors[rate->mcs]);
1743 
1744 	/* and take NSS */
1745 	tmp *= rate->nss;
1746 	do_div(tmp, 8);
1747 
1748 	/* and handle interference mitigation - 0.9x */
1749 	if (rate->flags & RATE_INFO_FLAGS_UHR_IM) {
1750 		if (WARN(rate->nss != 1 || rate->mcs == 15,
1751 			 "invalid NSS or MCS for UHR IM\n"))
1752 			return 0;
1753 		tmp *= 9000;
1754 		do_div(tmp, 10000);
1755 	}
1756 
1757 	result = tmp;
1758 
1759 	return result / 10000;
1760 }
1761 
1762 static u32 cfg80211_calculate_bitrate_eht(struct rate_info *rate)
1763 {
1764 	if (WARN_ONCE(rate->mcs > 15, "bad EHT MCS %d\n", rate->mcs))
1765 		return 0;
1766 
1767 	if (WARN_ONCE(rate->flags & (RATE_INFO_FLAGS_UHR_ELR_MCS |
1768 				     RATE_INFO_FLAGS_UHR_IM),
1769 		      "bad EHT MCS flags 0x%x\n", rate->flags))
1770 		return 0;
1771 
1772 	return _cfg80211_calculate_bitrate_eht_uhr(rate);
1773 }
1774 
1775 static u32 cfg80211_calculate_bitrate_uhr(struct rate_info *rate)
1776 {
1777 	if (rate->flags & RATE_INFO_FLAGS_UHR_ELR_MCS) {
1778 		WARN_ONCE(rate->eht_gi != NL80211_RATE_INFO_EHT_GI_1_6,
1779 			  "bad UHR ELR guard interval %d\n",
1780 			  rate->eht_gi);
1781 		WARN_ONCE(rate->mcs > 1, "bad UHR ELR MCS %d\n", rate->mcs);
1782 		WARN_ONCE(rate->nss != 1, "bad UHR ELR NSS %d\n", rate->nss);
1783 		WARN_ONCE(rate->bw != RATE_INFO_BW_20,
1784 			  "bad UHR ELR bandwidth %d\n",
1785 			  rate->bw);
1786 		WARN_ONCE(rate->flags & RATE_INFO_FLAGS_UHR_IM,
1787 			  "bad UHR MCS flags 0x%x\n", rate->flags);
1788 		if (rate->mcs == 0)
1789 			return 17;
1790 		return 33;
1791 	}
1792 
1793 	switch (rate->mcs) {
1794 	case 0 ... 15:
1795 	case 17:
1796 	case 19:
1797 	case 20:
1798 	case 23:
1799 		return _cfg80211_calculate_bitrate_eht_uhr(rate);
1800 	}
1801 
1802 	WARN_ONCE(1, "bad UHR MCS %d\n", rate->mcs);
1803 	return 0;
1804 }
1805 
1806 static u32 cfg80211_calculate_bitrate_s1g(struct rate_info *rate)
1807 {
1808 	/* For 1, 2, 4, 8 and 16 MHz channels */
1809 	static const u32 base[5][11] = {
1810 		{  300000,
1811 		   600000,
1812 		   900000,
1813 		  1200000,
1814 		  1800000,
1815 		  2400000,
1816 		  2700000,
1817 		  3000000,
1818 		  3600000,
1819 		  4000000,
1820 		  /* MCS 10 supported in 1 MHz only */
1821 		  150000,
1822 		},
1823 		{  650000,
1824 		  1300000,
1825 		  1950000,
1826 		  2600000,
1827 		  3900000,
1828 		  5200000,
1829 		  5850000,
1830 		  6500000,
1831 		  7800000,
1832 		  /* MCS 9 not valid */
1833 		},
1834 		{  1350000,
1835 		   2700000,
1836 		   4050000,
1837 		   5400000,
1838 		   8100000,
1839 		  10800000,
1840 		  12150000,
1841 		  13500000,
1842 		  16200000,
1843 		  18000000,
1844 		},
1845 		{  2925000,
1846 		   5850000,
1847 		   8775000,
1848 		  11700000,
1849 		  17550000,
1850 		  23400000,
1851 		  26325000,
1852 		  29250000,
1853 		  35100000,
1854 		  39000000,
1855 		},
1856 		{  8580000,
1857 		  11700000,
1858 		  17550000,
1859 		  23400000,
1860 		  35100000,
1861 		  46800000,
1862 		  52650000,
1863 		  58500000,
1864 		  70200000,
1865 		  78000000,
1866 		},
1867 	};
1868 	u32 bitrate;
1869 	/* default is 1 MHz index */
1870 	int idx = 0;
1871 
1872 	if (rate->mcs >= 11)
1873 		goto warn;
1874 
1875 	switch (rate->bw) {
1876 	case RATE_INFO_BW_16:
1877 		idx = 4;
1878 		break;
1879 	case RATE_INFO_BW_8:
1880 		idx = 3;
1881 		break;
1882 	case RATE_INFO_BW_4:
1883 		idx = 2;
1884 		break;
1885 	case RATE_INFO_BW_2:
1886 		idx = 1;
1887 		break;
1888 	case RATE_INFO_BW_1:
1889 		idx = 0;
1890 		break;
1891 	case RATE_INFO_BW_5:
1892 	case RATE_INFO_BW_10:
1893 	case RATE_INFO_BW_20:
1894 	case RATE_INFO_BW_40:
1895 	case RATE_INFO_BW_80:
1896 	case RATE_INFO_BW_160:
1897 	default:
1898 		goto warn;
1899 	}
1900 
1901 	bitrate = base[idx][rate->mcs];
1902 	bitrate *= rate->nss;
1903 
1904 	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1905 		bitrate = (bitrate / 9) * 10;
1906 	/* do NOT round down here */
1907 	return (bitrate + 50000) / 100000;
1908 warn:
1909 	WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1910 		  rate->bw, rate->mcs, rate->nss);
1911 	return 0;
1912 }
1913 
1914 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1915 {
1916 	if (rate->flags & RATE_INFO_FLAGS_MCS)
1917 		return cfg80211_calculate_bitrate_ht(rate);
1918 	if (rate->flags & RATE_INFO_FLAGS_DMG)
1919 		return cfg80211_calculate_bitrate_dmg(rate);
1920 	if (rate->flags & RATE_INFO_FLAGS_EXTENDED_SC_DMG)
1921 		return cfg80211_calculate_bitrate_extended_sc_dmg(rate);
1922 	if (rate->flags & RATE_INFO_FLAGS_EDMG)
1923 		return cfg80211_calculate_bitrate_edmg(rate);
1924 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1925 		return cfg80211_calculate_bitrate_vht(rate);
1926 	if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1927 		return cfg80211_calculate_bitrate_he(rate);
1928 	if (rate->flags & RATE_INFO_FLAGS_EHT_MCS)
1929 		return cfg80211_calculate_bitrate_eht(rate);
1930 	if (rate->flags & RATE_INFO_FLAGS_UHR_MCS)
1931 		return cfg80211_calculate_bitrate_uhr(rate);
1932 	if (rate->flags & RATE_INFO_FLAGS_S1G_MCS)
1933 		return cfg80211_calculate_bitrate_s1g(rate);
1934 
1935 	return rate->legacy;
1936 }
1937 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1938 
1939 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1940 			  enum ieee80211_p2p_attr_id attr,
1941 			  u8 *buf, unsigned int bufsize)
1942 {
1943 	u8 *out = buf;
1944 	u16 attr_remaining = 0;
1945 	bool desired_attr = false;
1946 	u16 desired_len = 0;
1947 
1948 	while (len > 0) {
1949 		unsigned int iedatalen;
1950 		unsigned int copy;
1951 		const u8 *iedata;
1952 
1953 		if (len < 2)
1954 			return -EILSEQ;
1955 		iedatalen = ies[1];
1956 		if (iedatalen + 2 > len)
1957 			return -EILSEQ;
1958 
1959 		if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1960 			goto cont;
1961 
1962 		if (iedatalen < 4)
1963 			goto cont;
1964 
1965 		iedata = ies + 2;
1966 
1967 		/* check WFA OUI, P2P subtype */
1968 		if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1969 		    iedata[2] != 0x9a || iedata[3] != 0x09)
1970 			goto cont;
1971 
1972 		iedatalen -= 4;
1973 		iedata += 4;
1974 
1975 		/* check attribute continuation into this IE */
1976 		copy = min_t(unsigned int, attr_remaining, iedatalen);
1977 		if (copy && desired_attr) {
1978 			desired_len += copy;
1979 			if (out) {
1980 				memcpy(out, iedata, min(bufsize, copy));
1981 				out += min(bufsize, copy);
1982 				bufsize -= min(bufsize, copy);
1983 			}
1984 
1985 
1986 			if (copy == attr_remaining)
1987 				return desired_len;
1988 		}
1989 
1990 		attr_remaining -= copy;
1991 		if (attr_remaining)
1992 			goto cont;
1993 
1994 		iedatalen -= copy;
1995 		iedata += copy;
1996 
1997 		while (iedatalen > 0) {
1998 			u16 attr_len;
1999 
2000 			/* P2P attribute ID & size must fit */
2001 			if (iedatalen < 3)
2002 				return -EILSEQ;
2003 			desired_attr = iedata[0] == attr;
2004 			attr_len = get_unaligned_le16(iedata + 1);
2005 			iedatalen -= 3;
2006 			iedata += 3;
2007 
2008 			copy = min_t(unsigned int, attr_len, iedatalen);
2009 
2010 			if (desired_attr) {
2011 				desired_len += copy;
2012 				if (out) {
2013 					memcpy(out, iedata, min(bufsize, copy));
2014 					out += min(bufsize, copy);
2015 					bufsize -= min(bufsize, copy);
2016 				}
2017 
2018 				if (copy == attr_len)
2019 					return desired_len;
2020 			}
2021 
2022 			iedata += copy;
2023 			iedatalen -= copy;
2024 			attr_remaining = attr_len - copy;
2025 		}
2026 
2027  cont:
2028 		len -= ies[1] + 2;
2029 		ies += ies[1] + 2;
2030 	}
2031 
2032 	if (attr_remaining && desired_attr)
2033 		return -EILSEQ;
2034 
2035 	return -ENOENT;
2036 }
2037 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
2038 
2039 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
2040 {
2041 	int i;
2042 
2043 	/* Make sure array values are legal */
2044 	if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
2045 		return false;
2046 
2047 	i = 0;
2048 	while (i < n_ids) {
2049 		if (ids[i] == WLAN_EID_EXTENSION) {
2050 			if (id_ext && (ids[i + 1] == id))
2051 				return true;
2052 
2053 			i += 2;
2054 			continue;
2055 		}
2056 
2057 		if (ids[i] == id && !id_ext)
2058 			return true;
2059 
2060 		i++;
2061 	}
2062 	return false;
2063 }
2064 
2065 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
2066 {
2067 	/* we assume a validly formed IEs buffer */
2068 	u8 len = ies[pos + 1];
2069 
2070 	pos += 2 + len;
2071 
2072 	/* the IE itself must have 255 bytes for fragments to follow */
2073 	if (len < 255)
2074 		return pos;
2075 
2076 	while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
2077 		len = ies[pos + 1];
2078 		pos += 2 + len;
2079 	}
2080 
2081 	return pos;
2082 }
2083 
2084 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
2085 			      const u8 *ids, int n_ids,
2086 			      const u8 *after_ric, int n_after_ric,
2087 			      size_t offset)
2088 {
2089 	size_t pos = offset;
2090 
2091 	while (pos < ielen) {
2092 		u8 ext = 0;
2093 
2094 		if (ies[pos] == WLAN_EID_EXTENSION)
2095 			ext = 2;
2096 		if ((pos + ext) >= ielen)
2097 			break;
2098 
2099 		if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
2100 					  ies[pos] == WLAN_EID_EXTENSION))
2101 			break;
2102 
2103 		if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
2104 			pos = skip_ie(ies, ielen, pos);
2105 
2106 			while (pos < ielen) {
2107 				if (ies[pos] == WLAN_EID_EXTENSION)
2108 					ext = 2;
2109 				else
2110 					ext = 0;
2111 
2112 				if ((pos + ext) >= ielen)
2113 					break;
2114 
2115 				if (!ieee80211_id_in_list(after_ric,
2116 							  n_after_ric,
2117 							  ies[pos + ext],
2118 							  ext == 2))
2119 					pos = skip_ie(ies, ielen, pos);
2120 				else
2121 					break;
2122 			}
2123 		} else {
2124 			pos = skip_ie(ies, ielen, pos);
2125 		}
2126 	}
2127 
2128 	return pos;
2129 }
2130 EXPORT_SYMBOL(ieee80211_ie_split_ric);
2131 
2132 void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos, u8 frag_id)
2133 {
2134 	unsigned int elem_len;
2135 
2136 	if (!len_pos)
2137 		return;
2138 
2139 	elem_len = skb->data + skb->len - len_pos - 1;
2140 
2141 	while (elem_len > 255) {
2142 		/* this one is 255 */
2143 		*len_pos = 255;
2144 		/* remaining data gets smaller */
2145 		elem_len -= 255;
2146 		/* make space for the fragment ID/len in SKB */
2147 		skb_put(skb, 2);
2148 		/* shift back the remaining data to place fragment ID/len */
2149 		memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len);
2150 		/* place the fragment ID */
2151 		len_pos += 255 + 1;
2152 		*len_pos = frag_id;
2153 		/* and point to fragment length to update later */
2154 		len_pos++;
2155 	}
2156 
2157 	*len_pos = elem_len;
2158 }
2159 EXPORT_SYMBOL(ieee80211_fragment_element);
2160 
2161 bool ieee80211_operating_class_to_band(u8 operating_class,
2162 				       enum nl80211_band *band)
2163 {
2164 	switch (operating_class) {
2165 	case 112:
2166 	case 115 ... 127:
2167 	case 128 ... 130:
2168 		*band = NL80211_BAND_5GHZ;
2169 		return true;
2170 	case 131 ... 135:
2171 	case 137:
2172 		*band = NL80211_BAND_6GHZ;
2173 		return true;
2174 	case 81:
2175 	case 82:
2176 	case 83:
2177 	case 84:
2178 		*band = NL80211_BAND_2GHZ;
2179 		return true;
2180 	case 180:
2181 		*band = NL80211_BAND_60GHZ;
2182 		return true;
2183 	}
2184 
2185 	return false;
2186 }
2187 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
2188 
2189 bool ieee80211_operating_class_to_chandef(u8 operating_class,
2190 					  struct ieee80211_channel *chan,
2191 					  struct cfg80211_chan_def *chandef)
2192 {
2193 	u32 control_freq, offset = 0;
2194 	enum nl80211_band band;
2195 
2196 	if (!ieee80211_operating_class_to_band(operating_class, &band) ||
2197 	    !chan || band != chan->band)
2198 		return false;
2199 
2200 	control_freq = chan->center_freq;
2201 	chandef->chan = chan;
2202 
2203 	if (control_freq >= 5955)
2204 		offset = control_freq - 5955;
2205 	else if (control_freq >= 5745)
2206 		offset = control_freq - 5745;
2207 	else if (control_freq >= 5180)
2208 		offset = control_freq - 5180;
2209 	offset /= 20;
2210 
2211 	switch (operating_class) {
2212 	case 81:  /* 2 GHz band; 20 MHz; channels 1..13 */
2213 	case 82:  /* 2 GHz band; 20 MHz; channel 14 */
2214 	case 115: /* 5 GHz band; 20 MHz; channels 36,40,44,48 */
2215 	case 118: /* 5 GHz band; 20 MHz; channels 52,56,60,64 */
2216 	case 121: /* 5 GHz band; 20 MHz; channels 100..144 */
2217 	case 124: /* 5 GHz band; 20 MHz; channels 149,153,157,161 */
2218 	case 125: /* 5 GHz band; 20 MHz; channels 149..177 */
2219 	case 131: /* 6 GHz band; 20 MHz; channels 1..233*/
2220 	case 136: /* 6 GHz band; 20 MHz; channel 2 */
2221 		chandef->center_freq1 = control_freq;
2222 		chandef->width = NL80211_CHAN_WIDTH_20;
2223 		return true;
2224 	case 83:  /* 2 GHz band; 40 MHz; channels 1..9 */
2225 	case 116: /* 5 GHz band; 40 MHz; channels 36,44 */
2226 	case 119: /* 5 GHz band; 40 MHz; channels 52,60 */
2227 	case 122: /* 5 GHz band; 40 MHz; channels 100,108,116,124,132,140 */
2228 	case 126: /* 5 GHz band; 40 MHz; channels 149,157,165,173 */
2229 		chandef->center_freq1 = control_freq + 10;
2230 		chandef->width = NL80211_CHAN_WIDTH_40;
2231 		return true;
2232 	case 84:  /* 2 GHz band; 40 MHz; channels 5..13 */
2233 	case 117: /* 5 GHz band; 40 MHz; channels 40,48 */
2234 	case 120: /* 5 GHz band; 40 MHz; channels 56,64 */
2235 	case 123: /* 5 GHz band; 40 MHz; channels 104,112,120,128,136,144 */
2236 	case 127: /* 5 GHz band; 40 MHz; channels 153,161,169,177 */
2237 		chandef->center_freq1 = control_freq - 10;
2238 		chandef->width = NL80211_CHAN_WIDTH_40;
2239 		return true;
2240 	case 132: /* 6 GHz band; 40 MHz; channels 1,5,..,229*/
2241 		chandef->center_freq1 = control_freq + 10 - (offset & 1) * 20;
2242 		chandef->width = NL80211_CHAN_WIDTH_40;
2243 		return true;
2244 	case 128: /* 5 GHz band; 80 MHz; channels 36..64,100..144,149..177 */
2245 	case 133: /* 6 GHz band; 80 MHz; channels 1,5,..,229 */
2246 		chandef->center_freq1 = control_freq + 30 - (offset & 3) * 20;
2247 		chandef->width = NL80211_CHAN_WIDTH_80;
2248 		return true;
2249 	case 129: /* 5 GHz band; 160 MHz; channels 36..64,100..144,149..177 */
2250 	case 134: /* 6 GHz band; 160 MHz; channels 1,5,..,229 */
2251 		chandef->center_freq1 = control_freq + 70 - (offset & 7) * 20;
2252 		chandef->width = NL80211_CHAN_WIDTH_160;
2253 		return true;
2254 	case 130: /* 5 GHz band; 80+80 MHz; channels 36..64,100..144,149..177 */
2255 	case 135: /* 6 GHz band; 80+80 MHz; channels 1,5,..,229 */
2256 		  /* The center_freq2 of 80+80 MHz is unknown */
2257 	case 137: /* 6 GHz band; 320 MHz; channels 1,5,..,229 */
2258 		  /* 320-1 or 320-2 channelization is unknown */
2259 	default:
2260 		return false;
2261 	}
2262 }
2263 EXPORT_SYMBOL(ieee80211_operating_class_to_chandef);
2264 
2265 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
2266 					  u8 *op_class)
2267 {
2268 	u8 vht_opclass;
2269 	u32 freq = chandef->center_freq1;
2270 
2271 	if (freq >= 2412 && freq <= 2472) {
2272 		if (chandef->width > NL80211_CHAN_WIDTH_40)
2273 			return false;
2274 
2275 		/* 2.407 GHz, channels 1..13 */
2276 		if (chandef->width == NL80211_CHAN_WIDTH_40) {
2277 			if (freq > chandef->chan->center_freq)
2278 				*op_class = 83; /* HT40+ */
2279 			else
2280 				*op_class = 84; /* HT40- */
2281 		} else {
2282 			*op_class = 81;
2283 		}
2284 
2285 		return true;
2286 	}
2287 
2288 	if (freq == 2484) {
2289 		/* channel 14 is only for IEEE 802.11b */
2290 		if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
2291 			return false;
2292 
2293 		*op_class = 82; /* channel 14 */
2294 		return true;
2295 	}
2296 
2297 	switch (chandef->width) {
2298 	case NL80211_CHAN_WIDTH_80:
2299 		vht_opclass = 128;
2300 		break;
2301 	case NL80211_CHAN_WIDTH_160:
2302 		vht_opclass = 129;
2303 		break;
2304 	case NL80211_CHAN_WIDTH_80P80:
2305 		vht_opclass = 130;
2306 		break;
2307 	default:
2308 		vht_opclass = 0;
2309 		break;
2310 	}
2311 
2312 	/* 5 GHz, channels 36..48 */
2313 	if (freq >= 5180 && freq <= 5240) {
2314 		if (vht_opclass) {
2315 			*op_class = vht_opclass;
2316 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2317 			if (freq > chandef->chan->center_freq)
2318 				*op_class = 116;
2319 			else
2320 				*op_class = 117;
2321 		} else {
2322 			*op_class = 115;
2323 		}
2324 
2325 		return true;
2326 	}
2327 
2328 	/* 5 GHz, channels 52..64 */
2329 	if (freq >= 5260 && freq <= 5320) {
2330 		if (vht_opclass) {
2331 			*op_class = vht_opclass;
2332 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2333 			if (freq > chandef->chan->center_freq)
2334 				*op_class = 119;
2335 			else
2336 				*op_class = 120;
2337 		} else {
2338 			*op_class = 118;
2339 		}
2340 
2341 		return true;
2342 	}
2343 
2344 	/* 5 GHz, channels 100..144 */
2345 	if (freq >= 5500 && freq <= 5720) {
2346 		if (vht_opclass) {
2347 			*op_class = vht_opclass;
2348 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2349 			if (freq > chandef->chan->center_freq)
2350 				*op_class = 122;
2351 			else
2352 				*op_class = 123;
2353 		} else {
2354 			*op_class = 121;
2355 		}
2356 
2357 		return true;
2358 	}
2359 
2360 	/* 5 GHz, channels 149..169 */
2361 	if (freq >= 5745 && freq <= 5845) {
2362 		if (vht_opclass) {
2363 			*op_class = vht_opclass;
2364 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2365 			if (freq > chandef->chan->center_freq)
2366 				*op_class = 126;
2367 			else
2368 				*op_class = 127;
2369 		} else if (freq <= 5805) {
2370 			*op_class = 124;
2371 		} else {
2372 			*op_class = 125;
2373 		}
2374 
2375 		return true;
2376 	}
2377 
2378 	/* 56.16 GHz, channel 1..4 */
2379 	if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
2380 		if (chandef->width >= NL80211_CHAN_WIDTH_40)
2381 			return false;
2382 
2383 		*op_class = 180;
2384 		return true;
2385 	}
2386 
2387 	/* not supported yet */
2388 	return false;
2389 }
2390 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
2391 
2392 static int cfg80211_wdev_bi(struct wireless_dev *wdev)
2393 {
2394 	switch (wdev->iftype) {
2395 	case NL80211_IFTYPE_AP:
2396 	case NL80211_IFTYPE_P2P_GO:
2397 		WARN_ON(wdev->valid_links);
2398 		return wdev->links[0].ap.beacon_interval;
2399 	case NL80211_IFTYPE_MESH_POINT:
2400 		return wdev->u.mesh.beacon_interval;
2401 	case NL80211_IFTYPE_ADHOC:
2402 		return wdev->u.ibss.beacon_interval;
2403 	default:
2404 		break;
2405 	}
2406 
2407 	return 0;
2408 }
2409 
2410 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
2411 				       u32 *beacon_int_gcd,
2412 				       bool *beacon_int_different,
2413 				       int radio_idx)
2414 {
2415 	struct cfg80211_registered_device *rdev;
2416 	struct wireless_dev *wdev;
2417 
2418 	*beacon_int_gcd = 0;
2419 	*beacon_int_different = false;
2420 
2421 	rdev = wiphy_to_rdev(wiphy);
2422 	list_for_each_entry(wdev, &wiphy->wdev_list, list) {
2423 		int wdev_bi;
2424 
2425 		/* this feature isn't supported with MLO */
2426 		if (wdev->valid_links)
2427 			continue;
2428 
2429 		/* skip wdevs not active on the given wiphy radio */
2430 		if (radio_idx >= 0 &&
2431 		    !(rdev_get_radio_mask(rdev, wdev->netdev) & BIT(radio_idx)))
2432 			continue;
2433 
2434 		wdev_bi = cfg80211_wdev_bi(wdev);
2435 
2436 		if (!wdev_bi)
2437 			continue;
2438 
2439 		if (!*beacon_int_gcd) {
2440 			*beacon_int_gcd = wdev_bi;
2441 			continue;
2442 		}
2443 
2444 		if (wdev_bi == *beacon_int_gcd)
2445 			continue;
2446 
2447 		*beacon_int_different = true;
2448 		*beacon_int_gcd = gcd(*beacon_int_gcd, wdev_bi);
2449 	}
2450 
2451 	if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
2452 		if (*beacon_int_gcd)
2453 			*beacon_int_different = true;
2454 		*beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
2455 	}
2456 }
2457 
2458 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
2459 				 enum nl80211_iftype iftype, u32 beacon_int)
2460 {
2461 	/*
2462 	 * This is just a basic pre-condition check; if interface combinations
2463 	 * are possible the driver must already be checking those with a call
2464 	 * to cfg80211_check_combinations(), in which case we'll validate more
2465 	 * through the cfg80211_calculate_bi_data() call and code in
2466 	 * cfg80211_iter_combinations().
2467 	 */
2468 
2469 	if (beacon_int < 10 || beacon_int > 10000)
2470 		return -EINVAL;
2471 
2472 	return 0;
2473 }
2474 
2475 int cfg80211_iter_combinations(struct wiphy *wiphy,
2476 			       struct iface_combination_params *params,
2477 			       void (*iter)(const struct ieee80211_iface_combination *c,
2478 					    void *data),
2479 			       void *data)
2480 {
2481 	const struct wiphy_radio *radio = NULL;
2482 	const struct ieee80211_iface_combination *c, *cs;
2483 	const struct ieee80211_regdomain *regdom;
2484 	enum nl80211_dfs_regions region = 0;
2485 	int i, j, n, iftype;
2486 	int num_interfaces = 0;
2487 	u32 used_iftypes = 0;
2488 	u32 beacon_int_gcd;
2489 	bool beacon_int_different;
2490 
2491 	if (params->radio_idx >= 0)
2492 		radio = &wiphy->radio[params->radio_idx];
2493 
2494 	/*
2495 	 * This is a bit strange, since the iteration used to rely only on
2496 	 * the data given by the driver, but here it now relies on context,
2497 	 * in form of the currently operating interfaces.
2498 	 * This is OK for all current users, and saves us from having to
2499 	 * push the GCD calculations into all the drivers.
2500 	 * In the future, this should probably rely more on data that's in
2501 	 * cfg80211 already - the only thing not would appear to be any new
2502 	 * interfaces (while being brought up) and channel/radar data.
2503 	 */
2504 	cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
2505 				   &beacon_int_gcd, &beacon_int_different,
2506 				   params->radio_idx);
2507 
2508 	if (params->radar_detect) {
2509 		rcu_read_lock();
2510 		regdom = rcu_dereference(cfg80211_regdomain);
2511 		if (regdom)
2512 			region = regdom->dfs_region;
2513 		rcu_read_unlock();
2514 	}
2515 
2516 	for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2517 		num_interfaces += params->iftype_num[iftype];
2518 		if (params->iftype_num[iftype] > 0 &&
2519 		    !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2520 			used_iftypes |= BIT(iftype);
2521 	}
2522 
2523 	if (radio) {
2524 		cs = radio->iface_combinations;
2525 		n = radio->n_iface_combinations;
2526 	} else {
2527 		cs = wiphy->iface_combinations;
2528 		n = wiphy->n_iface_combinations;
2529 	}
2530 	for (i = 0; i < n; i++) {
2531 		struct ieee80211_iface_limit *limits;
2532 		u32 all_iftypes = 0;
2533 
2534 		c = &cs[i];
2535 		if (num_interfaces > c->max_interfaces)
2536 			continue;
2537 		if (params->num_different_channels > c->num_different_channels)
2538 			continue;
2539 
2540 		limits = kmemdup_array(c->limits, c->n_limits, sizeof(*limits),
2541 				       GFP_KERNEL);
2542 		if (!limits)
2543 			return -ENOMEM;
2544 
2545 		for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2546 			if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2547 				continue;
2548 			for (j = 0; j < c->n_limits; j++) {
2549 				all_iftypes |= limits[j].types;
2550 				if (!(limits[j].types & BIT(iftype)))
2551 					continue;
2552 				if (limits[j].max < params->iftype_num[iftype])
2553 					goto cont;
2554 				limits[j].max -= params->iftype_num[iftype];
2555 			}
2556 		}
2557 
2558 		if (params->radar_detect !=
2559 			(c->radar_detect_widths & params->radar_detect))
2560 			goto cont;
2561 
2562 		if (params->radar_detect && c->radar_detect_regions &&
2563 		    !(c->radar_detect_regions & BIT(region)))
2564 			goto cont;
2565 
2566 		/* Finally check that all iftypes that we're currently
2567 		 * using are actually part of this combination. If they
2568 		 * aren't then we can't use this combination and have
2569 		 * to continue to the next.
2570 		 */
2571 		if ((all_iftypes & used_iftypes) != used_iftypes)
2572 			goto cont;
2573 
2574 		if (beacon_int_gcd) {
2575 			if (c->beacon_int_min_gcd &&
2576 			    beacon_int_gcd < c->beacon_int_min_gcd)
2577 				goto cont;
2578 			if (!c->beacon_int_min_gcd && beacon_int_different)
2579 				goto cont;
2580 		}
2581 
2582 		/* This combination covered all interface types and
2583 		 * supported the requested numbers, so we're good.
2584 		 */
2585 
2586 		(*iter)(c, data);
2587  cont:
2588 		kfree(limits);
2589 	}
2590 
2591 	return 0;
2592 }
2593 EXPORT_SYMBOL(cfg80211_iter_combinations);
2594 
2595 static void
2596 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
2597 			  void *data)
2598 {
2599 	int *num = data;
2600 	(*num)++;
2601 }
2602 
2603 int cfg80211_check_combinations(struct wiphy *wiphy,
2604 				struct iface_combination_params *params)
2605 {
2606 	int err, num = 0;
2607 
2608 	err = cfg80211_iter_combinations(wiphy, params,
2609 					 cfg80211_iter_sum_ifcombs, &num);
2610 	if (err)
2611 		return err;
2612 	if (num == 0)
2613 		return -EBUSY;
2614 
2615 	return 0;
2616 }
2617 EXPORT_SYMBOL(cfg80211_check_combinations);
2618 
2619 int cfg80211_get_radio_idx_by_chan(struct wiphy *wiphy,
2620 				   const struct ieee80211_channel *chan)
2621 {
2622 	const struct wiphy_radio *radio;
2623 	int i, j;
2624 	u32 freq;
2625 
2626 	if (!chan)
2627 		return -EINVAL;
2628 
2629 	freq = ieee80211_channel_to_khz(chan);
2630 	for (i = 0; i < wiphy->n_radio; i++) {
2631 		radio = &wiphy->radio[i];
2632 		for (j = 0; j < radio->n_freq_range; j++) {
2633 			if (freq >= radio->freq_range[j].start_freq &&
2634 			    freq < radio->freq_range[j].end_freq)
2635 				return i;
2636 		}
2637 	}
2638 
2639 	return -EINVAL;
2640 }
2641 EXPORT_SYMBOL(cfg80211_get_radio_idx_by_chan);
2642 
2643 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
2644 			   const u8 *rates, unsigned int n_rates,
2645 			   u32 *mask)
2646 {
2647 	int i, j;
2648 
2649 	if (!sband)
2650 		return -EINVAL;
2651 
2652 	if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
2653 		return -EINVAL;
2654 
2655 	*mask = 0;
2656 
2657 	for (i = 0; i < n_rates; i++) {
2658 		int rate = (rates[i] & 0x7f) * 5;
2659 		bool found = false;
2660 
2661 		for (j = 0; j < sband->n_bitrates; j++) {
2662 			if (sband->bitrates[j].bitrate == rate) {
2663 				found = true;
2664 				*mask |= BIT(j);
2665 				break;
2666 			}
2667 		}
2668 		if (!found)
2669 			return -EINVAL;
2670 	}
2671 
2672 	/*
2673 	 * mask must have at least one bit set here since we
2674 	 * didn't accept a 0-length rates array nor allowed
2675 	 * entries in the array that didn't exist
2676 	 */
2677 
2678 	return 0;
2679 }
2680 
2681 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
2682 {
2683 	enum nl80211_band band;
2684 	unsigned int n_channels = 0;
2685 
2686 	for (band = 0; band < NUM_NL80211_BANDS; band++)
2687 		if (wiphy->bands[band])
2688 			n_channels += wiphy->bands[band]->n_channels;
2689 
2690 	return n_channels;
2691 }
2692 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
2693 
2694 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
2695 			 struct station_info *sinfo)
2696 {
2697 	struct cfg80211_registered_device *rdev;
2698 	struct wireless_dev *wdev;
2699 
2700 	wdev = dev->ieee80211_ptr;
2701 	if (!wdev)
2702 		return -EOPNOTSUPP;
2703 
2704 	rdev = wiphy_to_rdev(wdev->wiphy);
2705 	if (!rdev->ops->get_station)
2706 		return -EOPNOTSUPP;
2707 
2708 	memset(sinfo, 0, sizeof(*sinfo));
2709 
2710 	guard(wiphy)(&rdev->wiphy);
2711 
2712 	return rdev_get_station(rdev, wdev, mac_addr, sinfo);
2713 }
2714 EXPORT_SYMBOL(cfg80211_get_station);
2715 
2716 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
2717 {
2718 	int i;
2719 
2720 	if (!f)
2721 		return;
2722 
2723 	kfree(f->serv_spec_info);
2724 	kfree(f->srf_bf);
2725 	kfree(f->srf_macs);
2726 	for (i = 0; i < f->num_rx_filters; i++)
2727 		kfree(f->rx_filters[i].filter);
2728 
2729 	for (i = 0; i < f->num_tx_filters; i++)
2730 		kfree(f->tx_filters[i].filter);
2731 
2732 	kfree(f->rx_filters);
2733 	kfree(f->tx_filters);
2734 	kfree(f);
2735 }
2736 EXPORT_SYMBOL(cfg80211_free_nan_func);
2737 
2738 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
2739 				u32 center_freq_khz, u32 bw_khz)
2740 {
2741 	u32 start_freq_khz, end_freq_khz;
2742 
2743 	start_freq_khz = center_freq_khz - (bw_khz / 2);
2744 	end_freq_khz = center_freq_khz + (bw_khz / 2);
2745 
2746 	if (start_freq_khz >= freq_range->start_freq_khz &&
2747 	    end_freq_khz <= freq_range->end_freq_khz)
2748 		return true;
2749 
2750 	return false;
2751 }
2752 
2753 int cfg80211_link_sinfo_alloc_tid_stats(struct link_station_info *link_sinfo,
2754 					gfp_t gfp)
2755 {
2756 	link_sinfo->pertid = kzalloc_objs(*link_sinfo->pertid,
2757 					  IEEE80211_NUM_TIDS + 1, gfp);
2758 	if (!link_sinfo->pertid)
2759 		return -ENOMEM;
2760 
2761 	return 0;
2762 }
2763 EXPORT_SYMBOL(cfg80211_link_sinfo_alloc_tid_stats);
2764 
2765 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
2766 {
2767 	sinfo->pertid = kzalloc_objs(*(sinfo->pertid), IEEE80211_NUM_TIDS + 1,
2768 				     gfp);
2769 	if (!sinfo->pertid)
2770 		return -ENOMEM;
2771 
2772 	return 0;
2773 }
2774 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
2775 
2776 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
2777 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
2778 const unsigned char rfc1042_header[] __aligned(2) =
2779 	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2780 EXPORT_SYMBOL(rfc1042_header);
2781 
2782 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
2783 const unsigned char bridge_tunnel_header[] __aligned(2) =
2784 	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
2785 EXPORT_SYMBOL(bridge_tunnel_header);
2786 
2787 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
2788 struct iapp_layer2_update {
2789 	u8 da[ETH_ALEN];	/* broadcast */
2790 	u8 sa[ETH_ALEN];	/* STA addr */
2791 	__be16 len;		/* 6 */
2792 	u8 dsap;		/* 0 */
2793 	u8 ssap;		/* 0 */
2794 	u8 control;
2795 	u8 xid_info[3];
2796 } __packed;
2797 
2798 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
2799 {
2800 	struct iapp_layer2_update *msg;
2801 	struct sk_buff *skb;
2802 
2803 	/* Send Level 2 Update Frame to update forwarding tables in layer 2
2804 	 * bridge devices */
2805 
2806 	skb = dev_alloc_skb(sizeof(*msg));
2807 	if (!skb)
2808 		return;
2809 	msg = skb_put(skb, sizeof(*msg));
2810 
2811 	/* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2812 	 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2813 
2814 	eth_broadcast_addr(msg->da);
2815 	ether_addr_copy(msg->sa, addr);
2816 	msg->len = htons(6);
2817 	msg->dsap = 0;
2818 	msg->ssap = 0x01;	/* NULL LSAP, CR Bit: Response */
2819 	msg->control = 0xaf;	/* XID response lsb.1111F101.
2820 				 * F=0 (no poll command; unsolicited frame) */
2821 	msg->xid_info[0] = 0x81;	/* XID format identifier */
2822 	msg->xid_info[1] = 1;	/* LLC types/classes: Type 1 LLC */
2823 	msg->xid_info[2] = 0;	/* XID sender's receive window size (RW) */
2824 
2825 	skb->dev = dev;
2826 	skb->protocol = eth_type_trans(skb, dev);
2827 	memset(skb->cb, 0, sizeof(skb->cb));
2828 	netif_rx(skb);
2829 }
2830 EXPORT_SYMBOL(cfg80211_send_layer2_update);
2831 
2832 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2833 			      enum ieee80211_vht_chanwidth bw,
2834 			      int mcs, bool ext_nss_bw_capable,
2835 			      unsigned int max_vht_nss)
2836 {
2837 	u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
2838 	int ext_nss_bw;
2839 	int supp_width;
2840 	int i, mcs_encoding;
2841 
2842 	if (map == 0xffff)
2843 		return 0;
2844 
2845 	if (WARN_ON(mcs > 9 || max_vht_nss > 8))
2846 		return 0;
2847 	if (mcs <= 7)
2848 		mcs_encoding = 0;
2849 	else if (mcs == 8)
2850 		mcs_encoding = 1;
2851 	else
2852 		mcs_encoding = 2;
2853 
2854 	if (!max_vht_nss) {
2855 		/* find max_vht_nss for the given MCS */
2856 		for (i = 7; i >= 0; i--) {
2857 			int supp = (map >> (2 * i)) & 3;
2858 
2859 			if (supp == 3)
2860 				continue;
2861 
2862 			if (supp >= mcs_encoding) {
2863 				max_vht_nss = i + 1;
2864 				break;
2865 			}
2866 		}
2867 	}
2868 
2869 	if (!(cap->supp_mcs.tx_mcs_map &
2870 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2871 		return max_vht_nss;
2872 
2873 	ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2874 				   IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2875 	supp_width = le32_get_bits(cap->vht_cap_info,
2876 				   IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2877 
2878 	/* if not capable, treat ext_nss_bw as 0 */
2879 	if (!ext_nss_bw_capable)
2880 		ext_nss_bw = 0;
2881 
2882 	/* This is invalid */
2883 	if (supp_width == 3)
2884 		return 0;
2885 
2886 	/* This is an invalid combination so pretend nothing is supported */
2887 	if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2888 		return 0;
2889 
2890 	/*
2891 	 * Cover all the special cases according to IEEE 802.11-2016
2892 	 * Table 9-250. All other cases are either factor of 1 or not
2893 	 * valid/supported.
2894 	 */
2895 	switch (bw) {
2896 	case IEEE80211_VHT_CHANWIDTH_USE_HT:
2897 	case IEEE80211_VHT_CHANWIDTH_80MHZ:
2898 		if ((supp_width == 1 || supp_width == 2) &&
2899 		    ext_nss_bw == 3)
2900 			return 2 * max_vht_nss;
2901 		break;
2902 	case IEEE80211_VHT_CHANWIDTH_160MHZ:
2903 		if (supp_width == 0 &&
2904 		    (ext_nss_bw == 1 || ext_nss_bw == 2))
2905 			return max_vht_nss / 2;
2906 		if (supp_width == 0 &&
2907 		    ext_nss_bw == 3)
2908 			return (3 * max_vht_nss) / 4;
2909 		if (supp_width == 1 &&
2910 		    ext_nss_bw == 3)
2911 			return 2 * max_vht_nss;
2912 		break;
2913 	case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2914 		if (supp_width == 0 && ext_nss_bw == 1)
2915 			return 0; /* not possible */
2916 		if (supp_width == 0 &&
2917 		    ext_nss_bw == 2)
2918 			return max_vht_nss / 2;
2919 		if (supp_width == 0 &&
2920 		    ext_nss_bw == 3)
2921 			return (3 * max_vht_nss) / 4;
2922 		if (supp_width == 1 &&
2923 		    ext_nss_bw == 0)
2924 			return 0; /* not possible */
2925 		if (supp_width == 1 &&
2926 		    ext_nss_bw == 1)
2927 			return max_vht_nss / 2;
2928 		if (supp_width == 1 &&
2929 		    ext_nss_bw == 2)
2930 			return (3 * max_vht_nss) / 4;
2931 		break;
2932 	}
2933 
2934 	/* not covered or invalid combination received */
2935 	return max_vht_nss;
2936 }
2937 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2938 
2939 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2940 			     bool is_4addr, u8 check_swif)
2941 
2942 {
2943 	bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2944 
2945 	switch (check_swif) {
2946 	case 0:
2947 		if (is_vlan && is_4addr)
2948 			return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2949 		return wiphy->interface_modes & BIT(iftype);
2950 	case 1:
2951 		if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2952 			return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2953 		return wiphy->software_iftypes & BIT(iftype);
2954 	default:
2955 		break;
2956 	}
2957 
2958 	return false;
2959 }
2960 EXPORT_SYMBOL(cfg80211_iftype_allowed);
2961 
2962 void cfg80211_remove_link(struct wireless_dev *wdev, unsigned int link_id)
2963 {
2964 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
2965 
2966 	lockdep_assert_wiphy(wdev->wiphy);
2967 
2968 	switch (wdev->iftype) {
2969 	case NL80211_IFTYPE_AP:
2970 	case NL80211_IFTYPE_P2P_GO:
2971 		cfg80211_stop_ap(rdev, wdev->netdev, link_id, true);
2972 		break;
2973 	default:
2974 		/* per-link not relevant */
2975 		break;
2976 	}
2977 
2978 	rdev_del_intf_link(rdev, wdev, link_id);
2979 
2980 	wdev->valid_links &= ~BIT(link_id);
2981 	eth_zero_addr(wdev->links[link_id].addr);
2982 }
2983 
2984 void cfg80211_remove_links(struct wireless_dev *wdev)
2985 {
2986 	unsigned int link_id;
2987 
2988 	/*
2989 	 * links are controlled by upper layers (userspace/cfg)
2990 	 * only for AP mode, so only remove them here for AP
2991 	 */
2992 	if (wdev->iftype != NL80211_IFTYPE_AP)
2993 		return;
2994 
2995 	if (wdev->valid_links) {
2996 		for_each_valid_link(wdev, link_id)
2997 			cfg80211_remove_link(wdev, link_id);
2998 	}
2999 }
3000 
3001 int cfg80211_remove_virtual_intf(struct cfg80211_registered_device *rdev,
3002 				 struct wireless_dev *wdev)
3003 {
3004 	cfg80211_remove_links(wdev);
3005 
3006 	return rdev_del_virtual_intf(rdev, wdev);
3007 }
3008 
3009 const struct wiphy_iftype_ext_capab *
3010 cfg80211_get_iftype_ext_capa(struct wiphy *wiphy, enum nl80211_iftype type)
3011 {
3012 	int i;
3013 
3014 	for (i = 0; i < wiphy->num_iftype_ext_capab; i++) {
3015 		if (wiphy->iftype_ext_capab[i].iftype == type)
3016 			return &wiphy->iftype_ext_capab[i];
3017 	}
3018 
3019 	return NULL;
3020 }
3021 EXPORT_SYMBOL(cfg80211_get_iftype_ext_capa);
3022 
3023 bool ieee80211_radio_freq_range_valid(const struct wiphy_radio *radio,
3024 				      u32 freq, u32 width)
3025 {
3026 	const struct wiphy_radio_freq_range *r;
3027 	int i;
3028 
3029 	for (i = 0; i < radio->n_freq_range; i++) {
3030 		r = &radio->freq_range[i];
3031 		if (freq - width / 2 >= r->start_freq &&
3032 		    freq + width / 2 <= r->end_freq)
3033 			return true;
3034 	}
3035 
3036 	return false;
3037 }
3038 EXPORT_SYMBOL(ieee80211_radio_freq_range_valid);
3039 
3040 bool cfg80211_radio_chandef_valid(const struct wiphy_radio *radio,
3041 				  const struct cfg80211_chan_def *chandef)
3042 {
3043 	u32 freq, width;
3044 
3045 	freq = ieee80211_chandef_to_khz(chandef);
3046 	width = MHZ_TO_KHZ(cfg80211_chandef_get_width(chandef));
3047 	if (!ieee80211_radio_freq_range_valid(radio, freq, width))
3048 		return false;
3049 
3050 	freq = MHZ_TO_KHZ(chandef->center_freq2);
3051 	if (freq && !ieee80211_radio_freq_range_valid(radio, freq, width))
3052 		return false;
3053 
3054 	return true;
3055 }
3056 EXPORT_SYMBOL(cfg80211_radio_chandef_valid);
3057 
3058 bool cfg80211_wdev_channel_allowed(struct wireless_dev *wdev,
3059 				   struct ieee80211_channel *chan)
3060 {
3061 	struct wiphy *wiphy = wdev->wiphy;
3062 	const struct wiphy_radio *radio;
3063 	struct cfg80211_chan_def chandef;
3064 	u32 radio_mask;
3065 	int i;
3066 
3067 	radio_mask = wdev->radio_mask;
3068 	if (!wiphy->n_radio || radio_mask == BIT(wiphy->n_radio) - 1)
3069 		return true;
3070 
3071 	cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_HT20);
3072 	for (i = 0; i < wiphy->n_radio; i++) {
3073 		if (!(radio_mask & BIT(i)))
3074 			continue;
3075 
3076 		radio = &wiphy->radio[i];
3077 		if (!cfg80211_radio_chandef_valid(radio, &chandef))
3078 			continue;
3079 
3080 		return true;
3081 	}
3082 
3083 	return false;
3084 }
3085 EXPORT_SYMBOL(cfg80211_wdev_channel_allowed);
3086