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