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