xref: /openbmc/linux/net/wireless/util.c (revision 9cb37357)
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-2022 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 			      enum nl80211_bss_scan_width scan_width)
48 {
49 	struct ieee80211_rate *bitrates;
50 	u32 mandatory_rates = 0;
51 	enum ieee80211_rate_flags mandatory_flag;
52 	int i;
53 
54 	if (WARN_ON(!sband))
55 		return 1;
56 
57 	if (sband->band == NL80211_BAND_2GHZ) {
58 		if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
59 		    scan_width == NL80211_BSS_CHAN_WIDTH_10)
60 			mandatory_flag = IEEE80211_RATE_MANDATORY_G;
61 		else
62 			mandatory_flag = IEEE80211_RATE_MANDATORY_B;
63 	} else {
64 		mandatory_flag = IEEE80211_RATE_MANDATORY_A;
65 	}
66 
67 	bitrates = sband->bitrates;
68 	for (i = 0; i < sband->n_bitrates; i++)
69 		if (bitrates[i].flags & mandatory_flag)
70 			mandatory_rates |= BIT(i);
71 	return mandatory_rates;
72 }
73 EXPORT_SYMBOL(ieee80211_mandatory_rates);
74 
75 u32 ieee80211_channel_to_freq_khz(int chan, enum nl80211_band band)
76 {
77 	/* see 802.11 17.3.8.3.2 and Annex J
78 	 * there are overlapping channel numbers in 5GHz and 2GHz bands */
79 	if (chan <= 0)
80 		return 0; /* not supported */
81 	switch (band) {
82 	case NL80211_BAND_2GHZ:
83 	case NL80211_BAND_LC:
84 		if (chan == 14)
85 			return MHZ_TO_KHZ(2484);
86 		else if (chan < 14)
87 			return MHZ_TO_KHZ(2407 + chan * 5);
88 		break;
89 	case NL80211_BAND_5GHZ:
90 		if (chan >= 182 && chan <= 196)
91 			return MHZ_TO_KHZ(4000 + chan * 5);
92 		else
93 			return MHZ_TO_KHZ(5000 + chan * 5);
94 		break;
95 	case NL80211_BAND_6GHZ:
96 		/* see 802.11ax D6.1 27.3.23.2 */
97 		if (chan == 2)
98 			return MHZ_TO_KHZ(5935);
99 		if (chan <= 233)
100 			return MHZ_TO_KHZ(5950 + chan * 5);
101 		break;
102 	case NL80211_BAND_60GHZ:
103 		if (chan < 7)
104 			return MHZ_TO_KHZ(56160 + chan * 2160);
105 		break;
106 	case NL80211_BAND_S1GHZ:
107 		return 902000 + chan * 500;
108 	default:
109 		;
110 	}
111 	return 0; /* not supported */
112 }
113 EXPORT_SYMBOL(ieee80211_channel_to_freq_khz);
114 
115 enum nl80211_chan_width
116 ieee80211_s1g_channel_width(const struct ieee80211_channel *chan)
117 {
118 	if (WARN_ON(!chan || chan->band != NL80211_BAND_S1GHZ))
119 		return NL80211_CHAN_WIDTH_20_NOHT;
120 
121 	/*S1G defines a single allowed channel width per channel.
122 	 * Extract that width here.
123 	 */
124 	if (chan->flags & IEEE80211_CHAN_1MHZ)
125 		return NL80211_CHAN_WIDTH_1;
126 	else if (chan->flags & IEEE80211_CHAN_2MHZ)
127 		return NL80211_CHAN_WIDTH_2;
128 	else if (chan->flags & IEEE80211_CHAN_4MHZ)
129 		return NL80211_CHAN_WIDTH_4;
130 	else if (chan->flags & IEEE80211_CHAN_8MHZ)
131 		return NL80211_CHAN_WIDTH_8;
132 	else if (chan->flags & IEEE80211_CHAN_16MHZ)
133 		return NL80211_CHAN_WIDTH_16;
134 
135 	pr_err("unknown channel width for channel at %dKHz?\n",
136 	       ieee80211_channel_to_khz(chan));
137 
138 	return NL80211_CHAN_WIDTH_1;
139 }
140 EXPORT_SYMBOL(ieee80211_s1g_channel_width);
141 
142 int ieee80211_freq_khz_to_channel(u32 freq)
143 {
144 	/* TODO: just handle MHz for now */
145 	freq = KHZ_TO_MHZ(freq);
146 
147 	/* see 802.11 17.3.8.3.2 and Annex J */
148 	if (freq == 2484)
149 		return 14;
150 	else if (freq < 2484)
151 		return (freq - 2407) / 5;
152 	else if (freq >= 4910 && freq <= 4980)
153 		return (freq - 4000) / 5;
154 	else if (freq < 5925)
155 		return (freq - 5000) / 5;
156 	else if (freq == 5935)
157 		return 2;
158 	else if (freq <= 45000) /* DMG band lower limit */
159 		/* see 802.11ax D6.1 27.3.22.2 */
160 		return (freq - 5950) / 5;
161 	else if (freq >= 58320 && freq <= 70200)
162 		return (freq - 56160) / 2160;
163 	else
164 		return 0;
165 }
166 EXPORT_SYMBOL(ieee80211_freq_khz_to_channel);
167 
168 struct ieee80211_channel *ieee80211_get_channel_khz(struct wiphy *wiphy,
169 						    u32 freq)
170 {
171 	enum nl80211_band band;
172 	struct ieee80211_supported_band *sband;
173 	int i;
174 
175 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
176 		sband = wiphy->bands[band];
177 
178 		if (!sband)
179 			continue;
180 
181 		for (i = 0; i < sband->n_channels; i++) {
182 			struct ieee80211_channel *chan = &sband->channels[i];
183 
184 			if (ieee80211_channel_to_khz(chan) == freq)
185 				return chan;
186 		}
187 	}
188 
189 	return NULL;
190 }
191 EXPORT_SYMBOL(ieee80211_get_channel_khz);
192 
193 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
194 {
195 	int i, want;
196 
197 	switch (sband->band) {
198 	case NL80211_BAND_5GHZ:
199 	case NL80211_BAND_6GHZ:
200 		want = 3;
201 		for (i = 0; i < sband->n_bitrates; i++) {
202 			if (sband->bitrates[i].bitrate == 60 ||
203 			    sband->bitrates[i].bitrate == 120 ||
204 			    sband->bitrates[i].bitrate == 240) {
205 				sband->bitrates[i].flags |=
206 					IEEE80211_RATE_MANDATORY_A;
207 				want--;
208 			}
209 		}
210 		WARN_ON(want);
211 		break;
212 	case NL80211_BAND_2GHZ:
213 	case NL80211_BAND_LC:
214 		want = 7;
215 		for (i = 0; i < sband->n_bitrates; i++) {
216 			switch (sband->bitrates[i].bitrate) {
217 			case 10:
218 			case 20:
219 			case 55:
220 			case 110:
221 				sband->bitrates[i].flags |=
222 					IEEE80211_RATE_MANDATORY_B |
223 					IEEE80211_RATE_MANDATORY_G;
224 				want--;
225 				break;
226 			case 60:
227 			case 120:
228 			case 240:
229 				sband->bitrates[i].flags |=
230 					IEEE80211_RATE_MANDATORY_G;
231 				want--;
232 				fallthrough;
233 			default:
234 				sband->bitrates[i].flags |=
235 					IEEE80211_RATE_ERP_G;
236 				break;
237 			}
238 		}
239 		WARN_ON(want != 0 && want != 3);
240 		break;
241 	case NL80211_BAND_60GHZ:
242 		/* check for mandatory HT MCS 1..4 */
243 		WARN_ON(!sband->ht_cap.ht_supported);
244 		WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
245 		break;
246 	case NL80211_BAND_S1GHZ:
247 		/* Figure 9-589bd: 3 means unsupported, so != 3 means at least
248 		 * mandatory is ok.
249 		 */
250 		WARN_ON((sband->s1g_cap.nss_mcs[0] & 0x3) == 0x3);
251 		break;
252 	case NUM_NL80211_BANDS:
253 	default:
254 		WARN_ON(1);
255 		break;
256 	}
257 }
258 
259 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
260 {
261 	enum nl80211_band band;
262 
263 	for (band = 0; band < NUM_NL80211_BANDS; band++)
264 		if (wiphy->bands[band])
265 			set_mandatory_flags_band(wiphy->bands[band]);
266 }
267 
268 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
269 {
270 	int i;
271 	for (i = 0; i < wiphy->n_cipher_suites; i++)
272 		if (cipher == wiphy->cipher_suites[i])
273 			return true;
274 	return false;
275 }
276 
277 static bool
278 cfg80211_igtk_cipher_supported(struct cfg80211_registered_device *rdev)
279 {
280 	struct wiphy *wiphy = &rdev->wiphy;
281 	int i;
282 
283 	for (i = 0; i < wiphy->n_cipher_suites; i++) {
284 		switch (wiphy->cipher_suites[i]) {
285 		case WLAN_CIPHER_SUITE_AES_CMAC:
286 		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
287 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
288 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
289 			return true;
290 		}
291 	}
292 
293 	return false;
294 }
295 
296 bool cfg80211_valid_key_idx(struct cfg80211_registered_device *rdev,
297 			    int key_idx, bool pairwise)
298 {
299 	int max_key_idx;
300 
301 	if (pairwise)
302 		max_key_idx = 3;
303 	else if (wiphy_ext_feature_isset(&rdev->wiphy,
304 					 NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
305 		 wiphy_ext_feature_isset(&rdev->wiphy,
306 					 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
307 		max_key_idx = 7;
308 	else if (cfg80211_igtk_cipher_supported(rdev))
309 		max_key_idx = 5;
310 	else
311 		max_key_idx = 3;
312 
313 	if (key_idx < 0 || key_idx > max_key_idx)
314 		return false;
315 
316 	return true;
317 }
318 
319 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
320 				   struct key_params *params, int key_idx,
321 				   bool pairwise, const u8 *mac_addr)
322 {
323 	if (!cfg80211_valid_key_idx(rdev, key_idx, pairwise))
324 		return -EINVAL;
325 
326 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
327 		return -EINVAL;
328 
329 	if (pairwise && !mac_addr)
330 		return -EINVAL;
331 
332 	switch (params->cipher) {
333 	case WLAN_CIPHER_SUITE_TKIP:
334 		/* Extended Key ID can only be used with CCMP/GCMP ciphers */
335 		if ((pairwise && key_idx) ||
336 		    params->mode != NL80211_KEY_RX_TX)
337 			return -EINVAL;
338 		break;
339 	case WLAN_CIPHER_SUITE_CCMP:
340 	case WLAN_CIPHER_SUITE_CCMP_256:
341 	case WLAN_CIPHER_SUITE_GCMP:
342 	case WLAN_CIPHER_SUITE_GCMP_256:
343 		/* IEEE802.11-2016 allows only 0 and - when supporting
344 		 * Extended Key ID - 1 as index for pairwise keys.
345 		 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
346 		 * the driver supports Extended Key ID.
347 		 * @NL80211_KEY_SET_TX can't be set when installing and
348 		 * validating a key.
349 		 */
350 		if ((params->mode == NL80211_KEY_NO_TX && !pairwise) ||
351 		    params->mode == NL80211_KEY_SET_TX)
352 			return -EINVAL;
353 		if (wiphy_ext_feature_isset(&rdev->wiphy,
354 					    NL80211_EXT_FEATURE_EXT_KEY_ID)) {
355 			if (pairwise && (key_idx < 0 || key_idx > 1))
356 				return -EINVAL;
357 		} else if (pairwise && key_idx) {
358 			return -EINVAL;
359 		}
360 		break;
361 	case WLAN_CIPHER_SUITE_AES_CMAC:
362 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
363 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
364 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
365 		/* Disallow BIP (group-only) cipher as pairwise cipher */
366 		if (pairwise)
367 			return -EINVAL;
368 		if (key_idx < 4)
369 			return -EINVAL;
370 		break;
371 	case WLAN_CIPHER_SUITE_WEP40:
372 	case WLAN_CIPHER_SUITE_WEP104:
373 		if (key_idx > 3)
374 			return -EINVAL;
375 		break;
376 	default:
377 		break;
378 	}
379 
380 	switch (params->cipher) {
381 	case WLAN_CIPHER_SUITE_WEP40:
382 		if (params->key_len != WLAN_KEY_LEN_WEP40)
383 			return -EINVAL;
384 		break;
385 	case WLAN_CIPHER_SUITE_TKIP:
386 		if (params->key_len != WLAN_KEY_LEN_TKIP)
387 			return -EINVAL;
388 		break;
389 	case WLAN_CIPHER_SUITE_CCMP:
390 		if (params->key_len != WLAN_KEY_LEN_CCMP)
391 			return -EINVAL;
392 		break;
393 	case WLAN_CIPHER_SUITE_CCMP_256:
394 		if (params->key_len != WLAN_KEY_LEN_CCMP_256)
395 			return -EINVAL;
396 		break;
397 	case WLAN_CIPHER_SUITE_GCMP:
398 		if (params->key_len != WLAN_KEY_LEN_GCMP)
399 			return -EINVAL;
400 		break;
401 	case WLAN_CIPHER_SUITE_GCMP_256:
402 		if (params->key_len != WLAN_KEY_LEN_GCMP_256)
403 			return -EINVAL;
404 		break;
405 	case WLAN_CIPHER_SUITE_WEP104:
406 		if (params->key_len != WLAN_KEY_LEN_WEP104)
407 			return -EINVAL;
408 		break;
409 	case WLAN_CIPHER_SUITE_AES_CMAC:
410 		if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
411 			return -EINVAL;
412 		break;
413 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
414 		if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
415 			return -EINVAL;
416 		break;
417 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
418 		if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
419 			return -EINVAL;
420 		break;
421 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
422 		if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
423 			return -EINVAL;
424 		break;
425 	default:
426 		/*
427 		 * We don't know anything about this algorithm,
428 		 * allow using it -- but the driver must check
429 		 * all parameters! We still check below whether
430 		 * or not the driver supports this algorithm,
431 		 * of course.
432 		 */
433 		break;
434 	}
435 
436 	if (params->seq) {
437 		switch (params->cipher) {
438 		case WLAN_CIPHER_SUITE_WEP40:
439 		case WLAN_CIPHER_SUITE_WEP104:
440 			/* These ciphers do not use key sequence */
441 			return -EINVAL;
442 		case WLAN_CIPHER_SUITE_TKIP:
443 		case WLAN_CIPHER_SUITE_CCMP:
444 		case WLAN_CIPHER_SUITE_CCMP_256:
445 		case WLAN_CIPHER_SUITE_GCMP:
446 		case WLAN_CIPHER_SUITE_GCMP_256:
447 		case WLAN_CIPHER_SUITE_AES_CMAC:
448 		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
449 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
450 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
451 			if (params->seq_len != 6)
452 				return -EINVAL;
453 			break;
454 		}
455 	}
456 
457 	if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
458 		return -EINVAL;
459 
460 	return 0;
461 }
462 
463 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
464 {
465 	unsigned int hdrlen = 24;
466 
467 	if (ieee80211_is_ext(fc)) {
468 		hdrlen = 4;
469 		goto out;
470 	}
471 
472 	if (ieee80211_is_data(fc)) {
473 		if (ieee80211_has_a4(fc))
474 			hdrlen = 30;
475 		if (ieee80211_is_data_qos(fc)) {
476 			hdrlen += IEEE80211_QOS_CTL_LEN;
477 			if (ieee80211_has_order(fc))
478 				hdrlen += IEEE80211_HT_CTL_LEN;
479 		}
480 		goto out;
481 	}
482 
483 	if (ieee80211_is_mgmt(fc)) {
484 		if (ieee80211_has_order(fc))
485 			hdrlen += IEEE80211_HT_CTL_LEN;
486 		goto out;
487 	}
488 
489 	if (ieee80211_is_ctl(fc)) {
490 		/*
491 		 * ACK and CTS are 10 bytes, all others 16. To see how
492 		 * to get this condition consider
493 		 *   subtype mask:   0b0000000011110000 (0x00F0)
494 		 *   ACK subtype:    0b0000000011010000 (0x00D0)
495 		 *   CTS subtype:    0b0000000011000000 (0x00C0)
496 		 *   bits that matter:         ^^^      (0x00E0)
497 		 *   value of those: 0b0000000011000000 (0x00C0)
498 		 */
499 		if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
500 			hdrlen = 10;
501 		else
502 			hdrlen = 16;
503 	}
504 out:
505 	return hdrlen;
506 }
507 EXPORT_SYMBOL(ieee80211_hdrlen);
508 
509 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
510 {
511 	const struct ieee80211_hdr *hdr =
512 			(const struct ieee80211_hdr *)skb->data;
513 	unsigned int hdrlen;
514 
515 	if (unlikely(skb->len < 10))
516 		return 0;
517 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
518 	if (unlikely(hdrlen > skb->len))
519 		return 0;
520 	return hdrlen;
521 }
522 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
523 
524 static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
525 {
526 	int ae = flags & MESH_FLAGS_AE;
527 	/* 802.11-2012, 8.2.4.7.3 */
528 	switch (ae) {
529 	default:
530 	case 0:
531 		return 6;
532 	case MESH_FLAGS_AE_A4:
533 		return 12;
534 	case MESH_FLAGS_AE_A5_A6:
535 		return 18;
536 	}
537 }
538 
539 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
540 {
541 	return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
542 }
543 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
544 
545 int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
546 				  const u8 *addr, enum nl80211_iftype iftype,
547 				  u8 data_offset, bool is_amsdu)
548 {
549 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
550 	struct {
551 		u8 hdr[ETH_ALEN] __aligned(2);
552 		__be16 proto;
553 	} payload;
554 	struct ethhdr tmp;
555 	u16 hdrlen;
556 	u8 mesh_flags = 0;
557 
558 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
559 		return -1;
560 
561 	hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
562 	if (skb->len < hdrlen)
563 		return -1;
564 
565 	/* convert IEEE 802.11 header + possible LLC headers into Ethernet
566 	 * header
567 	 * IEEE 802.11 address fields:
568 	 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
569 	 *   0     0   DA    SA    BSSID n/a
570 	 *   0     1   DA    BSSID SA    n/a
571 	 *   1     0   BSSID SA    DA    n/a
572 	 *   1     1   RA    TA    DA    SA
573 	 */
574 	memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
575 	memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
576 
577 	if (iftype == NL80211_IFTYPE_MESH_POINT &&
578 	    skb_copy_bits(skb, hdrlen, &mesh_flags, 1) < 0)
579 		return -1;
580 
581 	mesh_flags &= MESH_FLAGS_AE;
582 
583 	switch (hdr->frame_control &
584 		cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
585 	case cpu_to_le16(IEEE80211_FCTL_TODS):
586 		if (unlikely(iftype != NL80211_IFTYPE_AP &&
587 			     iftype != NL80211_IFTYPE_AP_VLAN &&
588 			     iftype != NL80211_IFTYPE_P2P_GO))
589 			return -1;
590 		break;
591 	case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
592 		if (unlikely(iftype != NL80211_IFTYPE_MESH_POINT &&
593 			     iftype != NL80211_IFTYPE_AP_VLAN &&
594 			     iftype != NL80211_IFTYPE_STATION))
595 			return -1;
596 		if (iftype == NL80211_IFTYPE_MESH_POINT) {
597 			if (mesh_flags == MESH_FLAGS_AE_A4)
598 				return -1;
599 			if (mesh_flags == MESH_FLAGS_AE_A5_A6 &&
600 			    skb_copy_bits(skb, hdrlen +
601 					  offsetof(struct ieee80211s_hdr, eaddr1),
602 					  tmp.h_dest, 2 * ETH_ALEN) < 0)
603 				return -1;
604 
605 			hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
606 		}
607 		break;
608 	case cpu_to_le16(IEEE80211_FCTL_FROMDS):
609 		if ((iftype != NL80211_IFTYPE_STATION &&
610 		     iftype != NL80211_IFTYPE_P2P_CLIENT &&
611 		     iftype != NL80211_IFTYPE_MESH_POINT) ||
612 		    (is_multicast_ether_addr(tmp.h_dest) &&
613 		     ether_addr_equal(tmp.h_source, addr)))
614 			return -1;
615 		if (iftype == NL80211_IFTYPE_MESH_POINT) {
616 			if (mesh_flags == MESH_FLAGS_AE_A5_A6)
617 				return -1;
618 			if (mesh_flags == MESH_FLAGS_AE_A4 &&
619 			    skb_copy_bits(skb, hdrlen +
620 					  offsetof(struct ieee80211s_hdr, eaddr1),
621 					  tmp.h_source, ETH_ALEN) < 0)
622 				return -1;
623 			hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
624 		}
625 		break;
626 	case cpu_to_le16(0):
627 		if (iftype != NL80211_IFTYPE_ADHOC &&
628 		    iftype != NL80211_IFTYPE_STATION &&
629 		    iftype != NL80211_IFTYPE_OCB)
630 				return -1;
631 		break;
632 	}
633 
634 	if (likely(skb_copy_bits(skb, hdrlen, &payload, sizeof(payload)) == 0 &&
635 	           ((!is_amsdu && ether_addr_equal(payload.hdr, rfc1042_header) &&
636 		     payload.proto != htons(ETH_P_AARP) &&
637 		     payload.proto != htons(ETH_P_IPX)) ||
638 		    ether_addr_equal(payload.hdr, bridge_tunnel_header)))) {
639 		/* remove RFC1042 or Bridge-Tunnel encapsulation and
640 		 * replace EtherType */
641 		hdrlen += ETH_ALEN + 2;
642 		tmp.h_proto = payload.proto;
643 		skb_postpull_rcsum(skb, &payload, ETH_ALEN + 2);
644 	} else {
645 		tmp.h_proto = htons(skb->len - hdrlen);
646 	}
647 
648 	pskb_pull(skb, hdrlen);
649 
650 	if (!ehdr)
651 		ehdr = skb_push(skb, sizeof(struct ethhdr));
652 	memcpy(ehdr, &tmp, sizeof(tmp));
653 
654 	return 0;
655 }
656 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
657 
658 static void
659 __frame_add_frag(struct sk_buff *skb, struct page *page,
660 		 void *ptr, int len, int size)
661 {
662 	struct skb_shared_info *sh = skb_shinfo(skb);
663 	int page_offset;
664 
665 	get_page(page);
666 	page_offset = ptr - page_address(page);
667 	skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
668 }
669 
670 static void
671 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
672 			    int offset, int len)
673 {
674 	struct skb_shared_info *sh = skb_shinfo(skb);
675 	const skb_frag_t *frag = &sh->frags[0];
676 	struct page *frag_page;
677 	void *frag_ptr;
678 	int frag_len, frag_size;
679 	int head_size = skb->len - skb->data_len;
680 	int cur_len;
681 
682 	frag_page = virt_to_head_page(skb->head);
683 	frag_ptr = skb->data;
684 	frag_size = head_size;
685 
686 	while (offset >= frag_size) {
687 		offset -= frag_size;
688 		frag_page = skb_frag_page(frag);
689 		frag_ptr = skb_frag_address(frag);
690 		frag_size = skb_frag_size(frag);
691 		frag++;
692 	}
693 
694 	frag_ptr += offset;
695 	frag_len = frag_size - offset;
696 
697 	cur_len = min(len, frag_len);
698 
699 	__frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
700 	len -= cur_len;
701 
702 	while (len > 0) {
703 		frag_len = skb_frag_size(frag);
704 		cur_len = min(len, frag_len);
705 		__frame_add_frag(frame, skb_frag_page(frag),
706 				 skb_frag_address(frag), cur_len, frag_len);
707 		len -= cur_len;
708 		frag++;
709 	}
710 }
711 
712 static struct sk_buff *
713 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
714 		       int offset, int len, bool reuse_frag)
715 {
716 	struct sk_buff *frame;
717 	int cur_len = len;
718 
719 	if (skb->len - offset < len)
720 		return NULL;
721 
722 	/*
723 	 * When reusing framents, copy some data to the head to simplify
724 	 * ethernet header handling and speed up protocol header processing
725 	 * in the stack later.
726 	 */
727 	if (reuse_frag)
728 		cur_len = min_t(int, len, 32);
729 
730 	/*
731 	 * Allocate and reserve two bytes more for payload
732 	 * alignment since sizeof(struct ethhdr) is 14.
733 	 */
734 	frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
735 	if (!frame)
736 		return NULL;
737 
738 	skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
739 	skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
740 
741 	len -= cur_len;
742 	if (!len)
743 		return frame;
744 
745 	offset += cur_len;
746 	__ieee80211_amsdu_copy_frag(skb, frame, offset, len);
747 
748 	return frame;
749 }
750 
751 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
752 			      const u8 *addr, enum nl80211_iftype iftype,
753 			      const unsigned int extra_headroom,
754 			      const u8 *check_da, const u8 *check_sa)
755 {
756 	unsigned int hlen = ALIGN(extra_headroom, 4);
757 	struct sk_buff *frame = NULL;
758 	u16 ethertype;
759 	u8 *payload;
760 	int offset = 0, remaining;
761 	struct ethhdr eth;
762 	bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
763 	bool reuse_skb = false;
764 	bool last = false;
765 
766 	while (!last) {
767 		unsigned int subframe_len;
768 		int len;
769 		u8 padding;
770 
771 		skb_copy_bits(skb, offset, &eth, sizeof(eth));
772 		len = ntohs(eth.h_proto);
773 		subframe_len = sizeof(struct ethhdr) + len;
774 		padding = (4 - subframe_len) & 0x3;
775 
776 		/* the last MSDU has no padding */
777 		remaining = skb->len - offset;
778 		if (subframe_len > remaining)
779 			goto purge;
780 		/* mitigate A-MSDU aggregation injection attacks */
781 		if (ether_addr_equal(eth.h_dest, rfc1042_header))
782 			goto purge;
783 
784 		offset += sizeof(struct ethhdr);
785 		last = remaining <= subframe_len + padding;
786 
787 		/* FIXME: should we really accept multicast DA? */
788 		if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
789 		     !ether_addr_equal(check_da, eth.h_dest)) ||
790 		    (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
791 			offset += len + padding;
792 			continue;
793 		}
794 
795 		/* reuse skb for the last subframe */
796 		if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
797 			skb_pull(skb, offset);
798 			frame = skb;
799 			reuse_skb = true;
800 		} else {
801 			frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
802 						       reuse_frag);
803 			if (!frame)
804 				goto purge;
805 
806 			offset += len + padding;
807 		}
808 
809 		skb_reset_network_header(frame);
810 		frame->dev = skb->dev;
811 		frame->priority = skb->priority;
812 
813 		payload = frame->data;
814 		ethertype = (payload[6] << 8) | payload[7];
815 		if (likely((ether_addr_equal(payload, rfc1042_header) &&
816 			    ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
817 			   ether_addr_equal(payload, bridge_tunnel_header))) {
818 			eth.h_proto = htons(ethertype);
819 			skb_pull(frame, ETH_ALEN + 2);
820 		}
821 
822 		memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
823 		__skb_queue_tail(list, frame);
824 	}
825 
826 	if (!reuse_skb)
827 		dev_kfree_skb(skb);
828 
829 	return;
830 
831  purge:
832 	__skb_queue_purge(list);
833 	dev_kfree_skb(skb);
834 }
835 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
836 
837 /* Given a data frame determine the 802.1p/1d tag to use. */
838 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
839 				    struct cfg80211_qos_map *qos_map)
840 {
841 	unsigned int dscp;
842 	unsigned char vlan_priority;
843 	unsigned int ret;
844 
845 	/* skb->priority values from 256->263 are magic values to
846 	 * directly indicate a specific 802.1d priority.  This is used
847 	 * to allow 802.1d priority to be passed directly in from VLAN
848 	 * tags, etc.
849 	 */
850 	if (skb->priority >= 256 && skb->priority <= 263) {
851 		ret = skb->priority - 256;
852 		goto out;
853 	}
854 
855 	if (skb_vlan_tag_present(skb)) {
856 		vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
857 			>> VLAN_PRIO_SHIFT;
858 		if (vlan_priority > 0) {
859 			ret = vlan_priority;
860 			goto out;
861 		}
862 	}
863 
864 	switch (skb->protocol) {
865 	case htons(ETH_P_IP):
866 		dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
867 		break;
868 	case htons(ETH_P_IPV6):
869 		dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
870 		break;
871 	case htons(ETH_P_MPLS_UC):
872 	case htons(ETH_P_MPLS_MC): {
873 		struct mpls_label mpls_tmp, *mpls;
874 
875 		mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
876 					  sizeof(*mpls), &mpls_tmp);
877 		if (!mpls)
878 			return 0;
879 
880 		ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
881 			>> MPLS_LS_TC_SHIFT;
882 		goto out;
883 	}
884 	case htons(ETH_P_80221):
885 		/* 802.21 is always network control traffic */
886 		return 7;
887 	default:
888 		return 0;
889 	}
890 
891 	if (qos_map) {
892 		unsigned int i, tmp_dscp = dscp >> 2;
893 
894 		for (i = 0; i < qos_map->num_des; i++) {
895 			if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
896 				ret = qos_map->dscp_exception[i].up;
897 				goto out;
898 			}
899 		}
900 
901 		for (i = 0; i < 8; i++) {
902 			if (tmp_dscp >= qos_map->up[i].low &&
903 			    tmp_dscp <= qos_map->up[i].high) {
904 				ret = i;
905 				goto out;
906 			}
907 		}
908 	}
909 
910 	ret = dscp >> 5;
911 out:
912 	return array_index_nospec(ret, IEEE80211_NUM_TIDS);
913 }
914 EXPORT_SYMBOL(cfg80211_classify8021d);
915 
916 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
917 {
918 	const struct cfg80211_bss_ies *ies;
919 
920 	ies = rcu_dereference(bss->ies);
921 	if (!ies)
922 		return NULL;
923 
924 	return cfg80211_find_elem(id, ies->data, ies->len);
925 }
926 EXPORT_SYMBOL(ieee80211_bss_get_elem);
927 
928 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
929 {
930 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
931 	struct net_device *dev = wdev->netdev;
932 	int i;
933 
934 	if (!wdev->connect_keys)
935 		return;
936 
937 	for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
938 		if (!wdev->connect_keys->params[i].cipher)
939 			continue;
940 		if (rdev_add_key(rdev, dev, -1, i, false, NULL,
941 				 &wdev->connect_keys->params[i])) {
942 			netdev_err(dev, "failed to set key %d\n", i);
943 			continue;
944 		}
945 		if (wdev->connect_keys->def == i &&
946 		    rdev_set_default_key(rdev, dev, -1, i, true, true)) {
947 			netdev_err(dev, "failed to set defkey %d\n", i);
948 			continue;
949 		}
950 	}
951 
952 	kfree_sensitive(wdev->connect_keys);
953 	wdev->connect_keys = NULL;
954 }
955 
956 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
957 {
958 	struct cfg80211_event *ev;
959 	unsigned long flags;
960 
961 	spin_lock_irqsave(&wdev->event_lock, flags);
962 	while (!list_empty(&wdev->event_list)) {
963 		ev = list_first_entry(&wdev->event_list,
964 				      struct cfg80211_event, list);
965 		list_del(&ev->list);
966 		spin_unlock_irqrestore(&wdev->event_lock, flags);
967 
968 		wdev_lock(wdev);
969 		switch (ev->type) {
970 		case EVENT_CONNECT_RESULT:
971 			__cfg80211_connect_result(
972 				wdev->netdev,
973 				&ev->cr,
974 				ev->cr.status == WLAN_STATUS_SUCCESS);
975 			break;
976 		case EVENT_ROAMED:
977 			__cfg80211_roamed(wdev, &ev->rm);
978 			break;
979 		case EVENT_DISCONNECTED:
980 			__cfg80211_disconnected(wdev->netdev,
981 						ev->dc.ie, ev->dc.ie_len,
982 						ev->dc.reason,
983 						!ev->dc.locally_generated);
984 			break;
985 		case EVENT_IBSS_JOINED:
986 			__cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
987 					       ev->ij.channel);
988 			break;
989 		case EVENT_STOPPED:
990 			__cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
991 			break;
992 		case EVENT_PORT_AUTHORIZED:
993 			__cfg80211_port_authorized(wdev, ev->pa.bssid);
994 			break;
995 		}
996 		wdev_unlock(wdev);
997 
998 		kfree(ev);
999 
1000 		spin_lock_irqsave(&wdev->event_lock, flags);
1001 	}
1002 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1003 }
1004 
1005 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
1006 {
1007 	struct wireless_dev *wdev;
1008 
1009 	lockdep_assert_held(&rdev->wiphy.mtx);
1010 
1011 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
1012 		cfg80211_process_wdev_events(wdev);
1013 }
1014 
1015 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
1016 			  struct net_device *dev, enum nl80211_iftype ntype,
1017 			  struct vif_params *params)
1018 {
1019 	int err;
1020 	enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
1021 
1022 	lockdep_assert_held(&rdev->wiphy.mtx);
1023 
1024 	/* don't support changing VLANs, you just re-create them */
1025 	if (otype == NL80211_IFTYPE_AP_VLAN)
1026 		return -EOPNOTSUPP;
1027 
1028 	/* cannot change into P2P device or NAN */
1029 	if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
1030 	    ntype == NL80211_IFTYPE_NAN)
1031 		return -EOPNOTSUPP;
1032 
1033 	if (!rdev->ops->change_virtual_intf ||
1034 	    !(rdev->wiphy.interface_modes & (1 << ntype)))
1035 		return -EOPNOTSUPP;
1036 
1037 	if (ntype != otype) {
1038 		/* if it's part of a bridge, reject changing type to station/ibss */
1039 		if (netif_is_bridge_port(dev) &&
1040 		    (ntype == NL80211_IFTYPE_ADHOC ||
1041 		     ntype == NL80211_IFTYPE_STATION ||
1042 		     ntype == NL80211_IFTYPE_P2P_CLIENT))
1043 			return -EBUSY;
1044 
1045 		dev->ieee80211_ptr->use_4addr = false;
1046 		wdev_lock(dev->ieee80211_ptr);
1047 		rdev_set_qos_map(rdev, dev, NULL);
1048 		wdev_unlock(dev->ieee80211_ptr);
1049 
1050 		switch (otype) {
1051 		case NL80211_IFTYPE_AP:
1052 		case NL80211_IFTYPE_P2P_GO:
1053 			cfg80211_stop_ap(rdev, dev, -1, true);
1054 			break;
1055 		case NL80211_IFTYPE_ADHOC:
1056 			cfg80211_leave_ibss(rdev, dev, false);
1057 			break;
1058 		case NL80211_IFTYPE_STATION:
1059 		case NL80211_IFTYPE_P2P_CLIENT:
1060 			wdev_lock(dev->ieee80211_ptr);
1061 			cfg80211_disconnect(rdev, dev,
1062 					    WLAN_REASON_DEAUTH_LEAVING, true);
1063 			wdev_unlock(dev->ieee80211_ptr);
1064 			break;
1065 		case NL80211_IFTYPE_MESH_POINT:
1066 			/* mesh should be handled? */
1067 			break;
1068 		case NL80211_IFTYPE_OCB:
1069 			cfg80211_leave_ocb(rdev, dev);
1070 			break;
1071 		default:
1072 			break;
1073 		}
1074 
1075 		cfg80211_process_rdev_events(rdev);
1076 		cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
1077 
1078 		memset(&dev->ieee80211_ptr->u, 0,
1079 		       sizeof(dev->ieee80211_ptr->u));
1080 		memset(&dev->ieee80211_ptr->links, 0,
1081 		       sizeof(dev->ieee80211_ptr->links));
1082 	}
1083 
1084 	err = rdev_change_virtual_intf(rdev, dev, ntype, params);
1085 
1086 	WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1087 
1088 	if (!err && params && params->use_4addr != -1)
1089 		dev->ieee80211_ptr->use_4addr = params->use_4addr;
1090 
1091 	if (!err) {
1092 		dev->priv_flags &= ~IFF_DONT_BRIDGE;
1093 		switch (ntype) {
1094 		case NL80211_IFTYPE_STATION:
1095 			if (dev->ieee80211_ptr->use_4addr)
1096 				break;
1097 			fallthrough;
1098 		case NL80211_IFTYPE_OCB:
1099 		case NL80211_IFTYPE_P2P_CLIENT:
1100 		case NL80211_IFTYPE_ADHOC:
1101 			dev->priv_flags |= IFF_DONT_BRIDGE;
1102 			break;
1103 		case NL80211_IFTYPE_P2P_GO:
1104 		case NL80211_IFTYPE_AP:
1105 		case NL80211_IFTYPE_AP_VLAN:
1106 		case NL80211_IFTYPE_MESH_POINT:
1107 			/* bridging OK */
1108 			break;
1109 		case NL80211_IFTYPE_MONITOR:
1110 			/* monitor can't bridge anyway */
1111 			break;
1112 		case NL80211_IFTYPE_UNSPECIFIED:
1113 		case NUM_NL80211_IFTYPES:
1114 			/* not happening */
1115 			break;
1116 		case NL80211_IFTYPE_P2P_DEVICE:
1117 		case NL80211_IFTYPE_WDS:
1118 		case NL80211_IFTYPE_NAN:
1119 			WARN_ON(1);
1120 			break;
1121 		}
1122 	}
1123 
1124 	if (!err && ntype != otype && netif_running(dev)) {
1125 		cfg80211_update_iface_num(rdev, ntype, 1);
1126 		cfg80211_update_iface_num(rdev, otype, -1);
1127 	}
1128 
1129 	return err;
1130 }
1131 
1132 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1133 {
1134 	int modulation, streams, bitrate;
1135 
1136 	/* the formula below does only work for MCS values smaller than 32 */
1137 	if (WARN_ON_ONCE(rate->mcs >= 32))
1138 		return 0;
1139 
1140 	modulation = rate->mcs & 7;
1141 	streams = (rate->mcs >> 3) + 1;
1142 
1143 	bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1144 
1145 	if (modulation < 4)
1146 		bitrate *= (modulation + 1);
1147 	else if (modulation == 4)
1148 		bitrate *= (modulation + 2);
1149 	else
1150 		bitrate *= (modulation + 3);
1151 
1152 	bitrate *= streams;
1153 
1154 	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1155 		bitrate = (bitrate / 9) * 10;
1156 
1157 	/* do NOT round down here */
1158 	return (bitrate + 50000) / 100000;
1159 }
1160 
1161 static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
1162 {
1163 	static const u32 __mcs2bitrate[] = {
1164 		/* control PHY */
1165 		[0] =   275,
1166 		/* SC PHY */
1167 		[1] =  3850,
1168 		[2] =  7700,
1169 		[3] =  9625,
1170 		[4] = 11550,
1171 		[5] = 12512, /* 1251.25 mbps */
1172 		[6] = 15400,
1173 		[7] = 19250,
1174 		[8] = 23100,
1175 		[9] = 25025,
1176 		[10] = 30800,
1177 		[11] = 38500,
1178 		[12] = 46200,
1179 		/* OFDM PHY */
1180 		[13] =  6930,
1181 		[14] =  8662, /* 866.25 mbps */
1182 		[15] = 13860,
1183 		[16] = 17325,
1184 		[17] = 20790,
1185 		[18] = 27720,
1186 		[19] = 34650,
1187 		[20] = 41580,
1188 		[21] = 45045,
1189 		[22] = 51975,
1190 		[23] = 62370,
1191 		[24] = 67568, /* 6756.75 mbps */
1192 		/* LP-SC PHY */
1193 		[25] =  6260,
1194 		[26] =  8340,
1195 		[27] = 11120,
1196 		[28] = 12510,
1197 		[29] = 16680,
1198 		[30] = 22240,
1199 		[31] = 25030,
1200 	};
1201 
1202 	if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1203 		return 0;
1204 
1205 	return __mcs2bitrate[rate->mcs];
1206 }
1207 
1208 static u32 cfg80211_calculate_bitrate_extended_sc_dmg(struct rate_info *rate)
1209 {
1210 	static const u32 __mcs2bitrate[] = {
1211 		[6 - 6] = 26950, /* MCS 9.1 : 2695.0 mbps */
1212 		[7 - 6] = 50050, /* MCS 12.1 */
1213 		[8 - 6] = 53900,
1214 		[9 - 6] = 57750,
1215 		[10 - 6] = 63900,
1216 		[11 - 6] = 75075,
1217 		[12 - 6] = 80850,
1218 	};
1219 
1220 	/* Extended SC MCS not defined for base MCS below 6 or above 12 */
1221 	if (WARN_ON_ONCE(rate->mcs < 6 || rate->mcs > 12))
1222 		return 0;
1223 
1224 	return __mcs2bitrate[rate->mcs - 6];
1225 }
1226 
1227 static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
1228 {
1229 	static const u32 __mcs2bitrate[] = {
1230 		/* control PHY */
1231 		[0] =   275,
1232 		/* SC PHY */
1233 		[1] =  3850,
1234 		[2] =  7700,
1235 		[3] =  9625,
1236 		[4] = 11550,
1237 		[5] = 12512, /* 1251.25 mbps */
1238 		[6] = 13475,
1239 		[7] = 15400,
1240 		[8] = 19250,
1241 		[9] = 23100,
1242 		[10] = 25025,
1243 		[11] = 26950,
1244 		[12] = 30800,
1245 		[13] = 38500,
1246 		[14] = 46200,
1247 		[15] = 50050,
1248 		[16] = 53900,
1249 		[17] = 57750,
1250 		[18] = 69300,
1251 		[19] = 75075,
1252 		[20] = 80850,
1253 	};
1254 
1255 	if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1256 		return 0;
1257 
1258 	return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
1259 }
1260 
1261 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1262 {
1263 	static const u32 base[4][12] = {
1264 		{   6500000,
1265 		   13000000,
1266 		   19500000,
1267 		   26000000,
1268 		   39000000,
1269 		   52000000,
1270 		   58500000,
1271 		   65000000,
1272 		   78000000,
1273 		/* not in the spec, but some devices use this: */
1274 		   86700000,
1275 		   97500000,
1276 		  108300000,
1277 		},
1278 		{  13500000,
1279 		   27000000,
1280 		   40500000,
1281 		   54000000,
1282 		   81000000,
1283 		  108000000,
1284 		  121500000,
1285 		  135000000,
1286 		  162000000,
1287 		  180000000,
1288 		  202500000,
1289 		  225000000,
1290 		},
1291 		{  29300000,
1292 		   58500000,
1293 		   87800000,
1294 		  117000000,
1295 		  175500000,
1296 		  234000000,
1297 		  263300000,
1298 		  292500000,
1299 		  351000000,
1300 		  390000000,
1301 		  438800000,
1302 		  487500000,
1303 		},
1304 		{  58500000,
1305 		  117000000,
1306 		  175500000,
1307 		  234000000,
1308 		  351000000,
1309 		  468000000,
1310 		  526500000,
1311 		  585000000,
1312 		  702000000,
1313 		  780000000,
1314 		  877500000,
1315 		  975000000,
1316 		},
1317 	};
1318 	u32 bitrate;
1319 	int idx;
1320 
1321 	if (rate->mcs > 11)
1322 		goto warn;
1323 
1324 	switch (rate->bw) {
1325 	case RATE_INFO_BW_160:
1326 		idx = 3;
1327 		break;
1328 	case RATE_INFO_BW_80:
1329 		idx = 2;
1330 		break;
1331 	case RATE_INFO_BW_40:
1332 		idx = 1;
1333 		break;
1334 	case RATE_INFO_BW_5:
1335 	case RATE_INFO_BW_10:
1336 	default:
1337 		goto warn;
1338 	case RATE_INFO_BW_20:
1339 		idx = 0;
1340 	}
1341 
1342 	bitrate = base[idx][rate->mcs];
1343 	bitrate *= rate->nss;
1344 
1345 	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1346 		bitrate = (bitrate / 9) * 10;
1347 
1348 	/* do NOT round down here */
1349 	return (bitrate + 50000) / 100000;
1350  warn:
1351 	WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1352 		  rate->bw, rate->mcs, rate->nss);
1353 	return 0;
1354 }
1355 
1356 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1357 {
1358 #define SCALE 6144
1359 	u32 mcs_divisors[14] = {
1360 		102399, /* 16.666666... */
1361 		 51201, /*  8.333333... */
1362 		 34134, /*  5.555555... */
1363 		 25599, /*  4.166666... */
1364 		 17067, /*  2.777777... */
1365 		 12801, /*  2.083333... */
1366 		 11377, /*  1.851725... */
1367 		 10239, /*  1.666666... */
1368 		  8532, /*  1.388888... */
1369 		  7680, /*  1.250000... */
1370 		  6828, /*  1.111111... */
1371 		  6144, /*  1.000000... */
1372 		  5690, /*  0.926106... */
1373 		  5120, /*  0.833333... */
1374 	};
1375 	u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1376 	u32 rates_969[3] =  { 480388888, 453700000, 408333333 };
1377 	u32 rates_484[3] =  { 229411111, 216666666, 195000000 };
1378 	u32 rates_242[3] =  { 114711111, 108333333,  97500000 };
1379 	u32 rates_106[3] =  {  40000000,  37777777,  34000000 };
1380 	u32 rates_52[3]  =  {  18820000,  17777777,  16000000 };
1381 	u32 rates_26[3]  =  {   9411111,   8888888,   8000000 };
1382 	u64 tmp;
1383 	u32 result;
1384 
1385 	if (WARN_ON_ONCE(rate->mcs > 13))
1386 		return 0;
1387 
1388 	if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1389 		return 0;
1390 	if (WARN_ON_ONCE(rate->he_ru_alloc >
1391 			 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1392 		return 0;
1393 	if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1394 		return 0;
1395 
1396 	if (rate->bw == RATE_INFO_BW_160)
1397 		result = rates_160M[rate->he_gi];
1398 	else if (rate->bw == RATE_INFO_BW_80 ||
1399 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1400 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1401 		result = rates_969[rate->he_gi];
1402 	else if (rate->bw == RATE_INFO_BW_40 ||
1403 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1404 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1405 		result = rates_484[rate->he_gi];
1406 	else if (rate->bw == RATE_INFO_BW_20 ||
1407 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1408 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1409 		result = rates_242[rate->he_gi];
1410 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1411 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1412 		result = rates_106[rate->he_gi];
1413 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1414 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1415 		result = rates_52[rate->he_gi];
1416 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1417 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1418 		result = rates_26[rate->he_gi];
1419 	else {
1420 		WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1421 		     rate->bw, rate->he_ru_alloc);
1422 		return 0;
1423 	}
1424 
1425 	/* now scale to the appropriate MCS */
1426 	tmp = result;
1427 	tmp *= SCALE;
1428 	do_div(tmp, mcs_divisors[rate->mcs]);
1429 	result = tmp;
1430 
1431 	/* and take NSS, DCM into account */
1432 	result = (result * rate->nss) / 8;
1433 	if (rate->he_dcm)
1434 		result /= 2;
1435 
1436 	return result / 10000;
1437 }
1438 
1439 static u32 cfg80211_calculate_bitrate_eht(struct rate_info *rate)
1440 {
1441 #define SCALE 6144
1442 	static const u32 mcs_divisors[16] = {
1443 		102399, /* 16.666666... */
1444 		 51201, /*  8.333333... */
1445 		 34134, /*  5.555555... */
1446 		 25599, /*  4.166666... */
1447 		 17067, /*  2.777777... */
1448 		 12801, /*  2.083333... */
1449 		 11377, /*  1.851725... */
1450 		 10239, /*  1.666666... */
1451 		  8532, /*  1.388888... */
1452 		  7680, /*  1.250000... */
1453 		  6828, /*  1.111111... */
1454 		  6144, /*  1.000000... */
1455 		  5690, /*  0.926106... */
1456 		  5120, /*  0.833333... */
1457 		409600, /* 66.666666... */
1458 		204800, /* 33.333333... */
1459 	};
1460 	static const u32 rates_996[3] =  { 480388888, 453700000, 408333333 };
1461 	static const u32 rates_484[3] =  { 229411111, 216666666, 195000000 };
1462 	static const u32 rates_242[3] =  { 114711111, 108333333,  97500000 };
1463 	static const u32 rates_106[3] =  {  40000000,  37777777,  34000000 };
1464 	static const u32 rates_52[3]  =  {  18820000,  17777777,  16000000 };
1465 	static const u32 rates_26[3]  =  {   9411111,   8888888,   8000000 };
1466 	u64 tmp;
1467 	u32 result;
1468 
1469 	if (WARN_ON_ONCE(rate->mcs > 15))
1470 		return 0;
1471 	if (WARN_ON_ONCE(rate->eht_gi > NL80211_RATE_INFO_EHT_GI_3_2))
1472 		return 0;
1473 	if (WARN_ON_ONCE(rate->eht_ru_alloc >
1474 			 NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1475 		return 0;
1476 	if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1477 		return 0;
1478 
1479 	/* Bandwidth checks for MCS 14 */
1480 	if (rate->mcs == 14) {
1481 		if ((rate->bw != RATE_INFO_BW_EHT_RU &&
1482 		     rate->bw != RATE_INFO_BW_80 &&
1483 		     rate->bw != RATE_INFO_BW_160 &&
1484 		     rate->bw != RATE_INFO_BW_320) ||
1485 		    (rate->bw == RATE_INFO_BW_EHT_RU &&
1486 		     rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_996 &&
1487 		     rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_2x996 &&
1488 		     rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) {
1489 			WARN(1, "invalid EHT BW for MCS 14: bw:%d, ru:%d\n",
1490 			     rate->bw, rate->eht_ru_alloc);
1491 			return 0;
1492 		}
1493 	}
1494 
1495 	if (rate->bw == RATE_INFO_BW_320 ||
1496 	    (rate->bw == RATE_INFO_BW_EHT_RU &&
1497 	     rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1498 		result = 4 * rates_996[rate->eht_gi];
1499 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1500 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484)
1501 		result = 3 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1502 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1503 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996)
1504 		result = 3 * rates_996[rate->eht_gi];
1505 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1506 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484)
1507 		result = 2 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1508 	else if (rate->bw == RATE_INFO_BW_160 ||
1509 		 (rate->bw == RATE_INFO_BW_EHT_RU &&
1510 		  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996))
1511 		result = 2 * rates_996[rate->eht_gi];
1512 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1513 		 rate->eht_ru_alloc ==
1514 		 NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242)
1515 		result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi]
1516 			 + rates_242[rate->eht_gi];
1517 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1518 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996P484)
1519 		result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1520 	else if (rate->bw == RATE_INFO_BW_80 ||
1521 		 (rate->bw == RATE_INFO_BW_EHT_RU &&
1522 		  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996))
1523 		result = rates_996[rate->eht_gi];
1524 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1525 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484P242)
1526 		result = rates_484[rate->eht_gi] + rates_242[rate->eht_gi];
1527 	else if (rate->bw == RATE_INFO_BW_40 ||
1528 		 (rate->bw == RATE_INFO_BW_EHT_RU &&
1529 		  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484))
1530 		result = rates_484[rate->eht_gi];
1531 	else if (rate->bw == RATE_INFO_BW_20 ||
1532 		 (rate->bw == RATE_INFO_BW_EHT_RU &&
1533 		  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_242))
1534 		result = rates_242[rate->eht_gi];
1535 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1536 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106P26)
1537 		result = rates_106[rate->eht_gi] + rates_26[rate->eht_gi];
1538 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1539 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106)
1540 		result = rates_106[rate->eht_gi];
1541 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1542 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52P26)
1543 		result = rates_52[rate->eht_gi] + rates_26[rate->eht_gi];
1544 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1545 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52)
1546 		result = rates_52[rate->eht_gi];
1547 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1548 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_26)
1549 		result = rates_26[rate->eht_gi];
1550 	else {
1551 		WARN(1, "invalid EHT MCS: bw:%d, ru:%d\n",
1552 		     rate->bw, rate->eht_ru_alloc);
1553 		return 0;
1554 	}
1555 
1556 	/* now scale to the appropriate MCS */
1557 	tmp = result;
1558 	tmp *= SCALE;
1559 	do_div(tmp, mcs_divisors[rate->mcs]);
1560 
1561 	/* and take NSS */
1562 	tmp *= rate->nss;
1563 	do_div(tmp, 8);
1564 
1565 	result = tmp;
1566 
1567 	return result / 10000;
1568 }
1569 
1570 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1571 {
1572 	if (rate->flags & RATE_INFO_FLAGS_MCS)
1573 		return cfg80211_calculate_bitrate_ht(rate);
1574 	if (rate->flags & RATE_INFO_FLAGS_DMG)
1575 		return cfg80211_calculate_bitrate_dmg(rate);
1576 	if (rate->flags & RATE_INFO_FLAGS_EXTENDED_SC_DMG)
1577 		return cfg80211_calculate_bitrate_extended_sc_dmg(rate);
1578 	if (rate->flags & RATE_INFO_FLAGS_EDMG)
1579 		return cfg80211_calculate_bitrate_edmg(rate);
1580 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1581 		return cfg80211_calculate_bitrate_vht(rate);
1582 	if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1583 		return cfg80211_calculate_bitrate_he(rate);
1584 	if (rate->flags & RATE_INFO_FLAGS_EHT_MCS)
1585 		return cfg80211_calculate_bitrate_eht(rate);
1586 
1587 	return rate->legacy;
1588 }
1589 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1590 
1591 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1592 			  enum ieee80211_p2p_attr_id attr,
1593 			  u8 *buf, unsigned int bufsize)
1594 {
1595 	u8 *out = buf;
1596 	u16 attr_remaining = 0;
1597 	bool desired_attr = false;
1598 	u16 desired_len = 0;
1599 
1600 	while (len > 0) {
1601 		unsigned int iedatalen;
1602 		unsigned int copy;
1603 		const u8 *iedata;
1604 
1605 		if (len < 2)
1606 			return -EILSEQ;
1607 		iedatalen = ies[1];
1608 		if (iedatalen + 2 > len)
1609 			return -EILSEQ;
1610 
1611 		if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1612 			goto cont;
1613 
1614 		if (iedatalen < 4)
1615 			goto cont;
1616 
1617 		iedata = ies + 2;
1618 
1619 		/* check WFA OUI, P2P subtype */
1620 		if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1621 		    iedata[2] != 0x9a || iedata[3] != 0x09)
1622 			goto cont;
1623 
1624 		iedatalen -= 4;
1625 		iedata += 4;
1626 
1627 		/* check attribute continuation into this IE */
1628 		copy = min_t(unsigned int, attr_remaining, iedatalen);
1629 		if (copy && desired_attr) {
1630 			desired_len += copy;
1631 			if (out) {
1632 				memcpy(out, iedata, min(bufsize, copy));
1633 				out += min(bufsize, copy);
1634 				bufsize -= min(bufsize, copy);
1635 			}
1636 
1637 
1638 			if (copy == attr_remaining)
1639 				return desired_len;
1640 		}
1641 
1642 		attr_remaining -= copy;
1643 		if (attr_remaining)
1644 			goto cont;
1645 
1646 		iedatalen -= copy;
1647 		iedata += copy;
1648 
1649 		while (iedatalen > 0) {
1650 			u16 attr_len;
1651 
1652 			/* P2P attribute ID & size must fit */
1653 			if (iedatalen < 3)
1654 				return -EILSEQ;
1655 			desired_attr = iedata[0] == attr;
1656 			attr_len = get_unaligned_le16(iedata + 1);
1657 			iedatalen -= 3;
1658 			iedata += 3;
1659 
1660 			copy = min_t(unsigned int, attr_len, iedatalen);
1661 
1662 			if (desired_attr) {
1663 				desired_len += copy;
1664 				if (out) {
1665 					memcpy(out, iedata, min(bufsize, copy));
1666 					out += min(bufsize, copy);
1667 					bufsize -= min(bufsize, copy);
1668 				}
1669 
1670 				if (copy == attr_len)
1671 					return desired_len;
1672 			}
1673 
1674 			iedata += copy;
1675 			iedatalen -= copy;
1676 			attr_remaining = attr_len - copy;
1677 		}
1678 
1679  cont:
1680 		len -= ies[1] + 2;
1681 		ies += ies[1] + 2;
1682 	}
1683 
1684 	if (attr_remaining && desired_attr)
1685 		return -EILSEQ;
1686 
1687 	return -ENOENT;
1688 }
1689 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1690 
1691 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1692 {
1693 	int i;
1694 
1695 	/* Make sure array values are legal */
1696 	if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1697 		return false;
1698 
1699 	i = 0;
1700 	while (i < n_ids) {
1701 		if (ids[i] == WLAN_EID_EXTENSION) {
1702 			if (id_ext && (ids[i + 1] == id))
1703 				return true;
1704 
1705 			i += 2;
1706 			continue;
1707 		}
1708 
1709 		if (ids[i] == id && !id_ext)
1710 			return true;
1711 
1712 		i++;
1713 	}
1714 	return false;
1715 }
1716 
1717 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1718 {
1719 	/* we assume a validly formed IEs buffer */
1720 	u8 len = ies[pos + 1];
1721 
1722 	pos += 2 + len;
1723 
1724 	/* the IE itself must have 255 bytes for fragments to follow */
1725 	if (len < 255)
1726 		return pos;
1727 
1728 	while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1729 		len = ies[pos + 1];
1730 		pos += 2 + len;
1731 	}
1732 
1733 	return pos;
1734 }
1735 
1736 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1737 			      const u8 *ids, int n_ids,
1738 			      const u8 *after_ric, int n_after_ric,
1739 			      size_t offset)
1740 {
1741 	size_t pos = offset;
1742 
1743 	while (pos < ielen) {
1744 		u8 ext = 0;
1745 
1746 		if (ies[pos] == WLAN_EID_EXTENSION)
1747 			ext = 2;
1748 		if ((pos + ext) >= ielen)
1749 			break;
1750 
1751 		if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1752 					  ies[pos] == WLAN_EID_EXTENSION))
1753 			break;
1754 
1755 		if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1756 			pos = skip_ie(ies, ielen, pos);
1757 
1758 			while (pos < ielen) {
1759 				if (ies[pos] == WLAN_EID_EXTENSION)
1760 					ext = 2;
1761 				else
1762 					ext = 0;
1763 
1764 				if ((pos + ext) >= ielen)
1765 					break;
1766 
1767 				if (!ieee80211_id_in_list(after_ric,
1768 							  n_after_ric,
1769 							  ies[pos + ext],
1770 							  ext == 2))
1771 					pos = skip_ie(ies, ielen, pos);
1772 				else
1773 					break;
1774 			}
1775 		} else {
1776 			pos = skip_ie(ies, ielen, pos);
1777 		}
1778 	}
1779 
1780 	return pos;
1781 }
1782 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1783 
1784 bool ieee80211_operating_class_to_band(u8 operating_class,
1785 				       enum nl80211_band *band)
1786 {
1787 	switch (operating_class) {
1788 	case 112:
1789 	case 115 ... 127:
1790 	case 128 ... 130:
1791 		*band = NL80211_BAND_5GHZ;
1792 		return true;
1793 	case 131 ... 135:
1794 		*band = NL80211_BAND_6GHZ;
1795 		return true;
1796 	case 81:
1797 	case 82:
1798 	case 83:
1799 	case 84:
1800 		*band = NL80211_BAND_2GHZ;
1801 		return true;
1802 	case 180:
1803 		*band = NL80211_BAND_60GHZ;
1804 		return true;
1805 	}
1806 
1807 	return false;
1808 }
1809 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1810 
1811 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1812 					  u8 *op_class)
1813 {
1814 	u8 vht_opclass;
1815 	u32 freq = chandef->center_freq1;
1816 
1817 	if (freq >= 2412 && freq <= 2472) {
1818 		if (chandef->width > NL80211_CHAN_WIDTH_40)
1819 			return false;
1820 
1821 		/* 2.407 GHz, channels 1..13 */
1822 		if (chandef->width == NL80211_CHAN_WIDTH_40) {
1823 			if (freq > chandef->chan->center_freq)
1824 				*op_class = 83; /* HT40+ */
1825 			else
1826 				*op_class = 84; /* HT40- */
1827 		} else {
1828 			*op_class = 81;
1829 		}
1830 
1831 		return true;
1832 	}
1833 
1834 	if (freq == 2484) {
1835 		/* channel 14 is only for IEEE 802.11b */
1836 		if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
1837 			return false;
1838 
1839 		*op_class = 82; /* channel 14 */
1840 		return true;
1841 	}
1842 
1843 	switch (chandef->width) {
1844 	case NL80211_CHAN_WIDTH_80:
1845 		vht_opclass = 128;
1846 		break;
1847 	case NL80211_CHAN_WIDTH_160:
1848 		vht_opclass = 129;
1849 		break;
1850 	case NL80211_CHAN_WIDTH_80P80:
1851 		vht_opclass = 130;
1852 		break;
1853 	case NL80211_CHAN_WIDTH_10:
1854 	case NL80211_CHAN_WIDTH_5:
1855 		return false; /* unsupported for now */
1856 	default:
1857 		vht_opclass = 0;
1858 		break;
1859 	}
1860 
1861 	/* 5 GHz, channels 36..48 */
1862 	if (freq >= 5180 && freq <= 5240) {
1863 		if (vht_opclass) {
1864 			*op_class = vht_opclass;
1865 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1866 			if (freq > chandef->chan->center_freq)
1867 				*op_class = 116;
1868 			else
1869 				*op_class = 117;
1870 		} else {
1871 			*op_class = 115;
1872 		}
1873 
1874 		return true;
1875 	}
1876 
1877 	/* 5 GHz, channels 52..64 */
1878 	if (freq >= 5260 && freq <= 5320) {
1879 		if (vht_opclass) {
1880 			*op_class = vht_opclass;
1881 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1882 			if (freq > chandef->chan->center_freq)
1883 				*op_class = 119;
1884 			else
1885 				*op_class = 120;
1886 		} else {
1887 			*op_class = 118;
1888 		}
1889 
1890 		return true;
1891 	}
1892 
1893 	/* 5 GHz, channels 100..144 */
1894 	if (freq >= 5500 && freq <= 5720) {
1895 		if (vht_opclass) {
1896 			*op_class = vht_opclass;
1897 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1898 			if (freq > chandef->chan->center_freq)
1899 				*op_class = 122;
1900 			else
1901 				*op_class = 123;
1902 		} else {
1903 			*op_class = 121;
1904 		}
1905 
1906 		return true;
1907 	}
1908 
1909 	/* 5 GHz, channels 149..169 */
1910 	if (freq >= 5745 && freq <= 5845) {
1911 		if (vht_opclass) {
1912 			*op_class = vht_opclass;
1913 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1914 			if (freq > chandef->chan->center_freq)
1915 				*op_class = 126;
1916 			else
1917 				*op_class = 127;
1918 		} else if (freq <= 5805) {
1919 			*op_class = 124;
1920 		} else {
1921 			*op_class = 125;
1922 		}
1923 
1924 		return true;
1925 	}
1926 
1927 	/* 56.16 GHz, channel 1..4 */
1928 	if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
1929 		if (chandef->width >= NL80211_CHAN_WIDTH_40)
1930 			return false;
1931 
1932 		*op_class = 180;
1933 		return true;
1934 	}
1935 
1936 	/* not supported yet */
1937 	return false;
1938 }
1939 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1940 
1941 static int cfg80211_wdev_bi(struct wireless_dev *wdev)
1942 {
1943 	switch (wdev->iftype) {
1944 	case NL80211_IFTYPE_AP:
1945 	case NL80211_IFTYPE_P2P_GO:
1946 		WARN_ON(wdev->valid_links);
1947 		return wdev->links[0].ap.beacon_interval;
1948 	case NL80211_IFTYPE_MESH_POINT:
1949 		return wdev->u.mesh.beacon_interval;
1950 	case NL80211_IFTYPE_ADHOC:
1951 		return wdev->u.ibss.beacon_interval;
1952 	default:
1953 		break;
1954 	}
1955 
1956 	return 0;
1957 }
1958 
1959 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1960 				       u32 *beacon_int_gcd,
1961 				       bool *beacon_int_different)
1962 {
1963 	struct wireless_dev *wdev;
1964 
1965 	*beacon_int_gcd = 0;
1966 	*beacon_int_different = false;
1967 
1968 	list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1969 		int wdev_bi;
1970 
1971 		/* this feature isn't supported with MLO */
1972 		if (wdev->valid_links)
1973 			continue;
1974 
1975 		wdev_bi = cfg80211_wdev_bi(wdev);
1976 
1977 		if (!wdev_bi)
1978 			continue;
1979 
1980 		if (!*beacon_int_gcd) {
1981 			*beacon_int_gcd = wdev_bi;
1982 			continue;
1983 		}
1984 
1985 		if (wdev_bi == *beacon_int_gcd)
1986 			continue;
1987 
1988 		*beacon_int_different = true;
1989 		*beacon_int_gcd = gcd(*beacon_int_gcd, wdev_bi);
1990 	}
1991 
1992 	if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1993 		if (*beacon_int_gcd)
1994 			*beacon_int_different = true;
1995 		*beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1996 	}
1997 }
1998 
1999 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
2000 				 enum nl80211_iftype iftype, u32 beacon_int)
2001 {
2002 	/*
2003 	 * This is just a basic pre-condition check; if interface combinations
2004 	 * are possible the driver must already be checking those with a call
2005 	 * to cfg80211_check_combinations(), in which case we'll validate more
2006 	 * through the cfg80211_calculate_bi_data() call and code in
2007 	 * cfg80211_iter_combinations().
2008 	 */
2009 
2010 	if (beacon_int < 10 || beacon_int > 10000)
2011 		return -EINVAL;
2012 
2013 	return 0;
2014 }
2015 
2016 int cfg80211_iter_combinations(struct wiphy *wiphy,
2017 			       struct iface_combination_params *params,
2018 			       void (*iter)(const struct ieee80211_iface_combination *c,
2019 					    void *data),
2020 			       void *data)
2021 {
2022 	const struct ieee80211_regdomain *regdom;
2023 	enum nl80211_dfs_regions region = 0;
2024 	int i, j, iftype;
2025 	int num_interfaces = 0;
2026 	u32 used_iftypes = 0;
2027 	u32 beacon_int_gcd;
2028 	bool beacon_int_different;
2029 
2030 	/*
2031 	 * This is a bit strange, since the iteration used to rely only on
2032 	 * the data given by the driver, but here it now relies on context,
2033 	 * in form of the currently operating interfaces.
2034 	 * This is OK for all current users, and saves us from having to
2035 	 * push the GCD calculations into all the drivers.
2036 	 * In the future, this should probably rely more on data that's in
2037 	 * cfg80211 already - the only thing not would appear to be any new
2038 	 * interfaces (while being brought up) and channel/radar data.
2039 	 */
2040 	cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
2041 				   &beacon_int_gcd, &beacon_int_different);
2042 
2043 	if (params->radar_detect) {
2044 		rcu_read_lock();
2045 		regdom = rcu_dereference(cfg80211_regdomain);
2046 		if (regdom)
2047 			region = regdom->dfs_region;
2048 		rcu_read_unlock();
2049 	}
2050 
2051 	for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2052 		num_interfaces += params->iftype_num[iftype];
2053 		if (params->iftype_num[iftype] > 0 &&
2054 		    !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2055 			used_iftypes |= BIT(iftype);
2056 	}
2057 
2058 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
2059 		const struct ieee80211_iface_combination *c;
2060 		struct ieee80211_iface_limit *limits;
2061 		u32 all_iftypes = 0;
2062 
2063 		c = &wiphy->iface_combinations[i];
2064 
2065 		if (num_interfaces > c->max_interfaces)
2066 			continue;
2067 		if (params->num_different_channels > c->num_different_channels)
2068 			continue;
2069 
2070 		limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
2071 				 GFP_KERNEL);
2072 		if (!limits)
2073 			return -ENOMEM;
2074 
2075 		for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2076 			if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2077 				continue;
2078 			for (j = 0; j < c->n_limits; j++) {
2079 				all_iftypes |= limits[j].types;
2080 				if (!(limits[j].types & BIT(iftype)))
2081 					continue;
2082 				if (limits[j].max < params->iftype_num[iftype])
2083 					goto cont;
2084 				limits[j].max -= params->iftype_num[iftype];
2085 			}
2086 		}
2087 
2088 		if (params->radar_detect !=
2089 			(c->radar_detect_widths & params->radar_detect))
2090 			goto cont;
2091 
2092 		if (params->radar_detect && c->radar_detect_regions &&
2093 		    !(c->radar_detect_regions & BIT(region)))
2094 			goto cont;
2095 
2096 		/* Finally check that all iftypes that we're currently
2097 		 * using are actually part of this combination. If they
2098 		 * aren't then we can't use this combination and have
2099 		 * to continue to the next.
2100 		 */
2101 		if ((all_iftypes & used_iftypes) != used_iftypes)
2102 			goto cont;
2103 
2104 		if (beacon_int_gcd) {
2105 			if (c->beacon_int_min_gcd &&
2106 			    beacon_int_gcd < c->beacon_int_min_gcd)
2107 				goto cont;
2108 			if (!c->beacon_int_min_gcd && beacon_int_different)
2109 				goto cont;
2110 		}
2111 
2112 		/* This combination covered all interface types and
2113 		 * supported the requested numbers, so we're good.
2114 		 */
2115 
2116 		(*iter)(c, data);
2117  cont:
2118 		kfree(limits);
2119 	}
2120 
2121 	return 0;
2122 }
2123 EXPORT_SYMBOL(cfg80211_iter_combinations);
2124 
2125 static void
2126 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
2127 			  void *data)
2128 {
2129 	int *num = data;
2130 	(*num)++;
2131 }
2132 
2133 int cfg80211_check_combinations(struct wiphy *wiphy,
2134 				struct iface_combination_params *params)
2135 {
2136 	int err, num = 0;
2137 
2138 	err = cfg80211_iter_combinations(wiphy, params,
2139 					 cfg80211_iter_sum_ifcombs, &num);
2140 	if (err)
2141 		return err;
2142 	if (num == 0)
2143 		return -EBUSY;
2144 
2145 	return 0;
2146 }
2147 EXPORT_SYMBOL(cfg80211_check_combinations);
2148 
2149 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
2150 			   const u8 *rates, unsigned int n_rates,
2151 			   u32 *mask)
2152 {
2153 	int i, j;
2154 
2155 	if (!sband)
2156 		return -EINVAL;
2157 
2158 	if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
2159 		return -EINVAL;
2160 
2161 	*mask = 0;
2162 
2163 	for (i = 0; i < n_rates; i++) {
2164 		int rate = (rates[i] & 0x7f) * 5;
2165 		bool found = false;
2166 
2167 		for (j = 0; j < sband->n_bitrates; j++) {
2168 			if (sband->bitrates[j].bitrate == rate) {
2169 				found = true;
2170 				*mask |= BIT(j);
2171 				break;
2172 			}
2173 		}
2174 		if (!found)
2175 			return -EINVAL;
2176 	}
2177 
2178 	/*
2179 	 * mask must have at least one bit set here since we
2180 	 * didn't accept a 0-length rates array nor allowed
2181 	 * entries in the array that didn't exist
2182 	 */
2183 
2184 	return 0;
2185 }
2186 
2187 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
2188 {
2189 	enum nl80211_band band;
2190 	unsigned int n_channels = 0;
2191 
2192 	for (band = 0; band < NUM_NL80211_BANDS; band++)
2193 		if (wiphy->bands[band])
2194 			n_channels += wiphy->bands[band]->n_channels;
2195 
2196 	return n_channels;
2197 }
2198 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
2199 
2200 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
2201 			 struct station_info *sinfo)
2202 {
2203 	struct cfg80211_registered_device *rdev;
2204 	struct wireless_dev *wdev;
2205 
2206 	wdev = dev->ieee80211_ptr;
2207 	if (!wdev)
2208 		return -EOPNOTSUPP;
2209 
2210 	rdev = wiphy_to_rdev(wdev->wiphy);
2211 	if (!rdev->ops->get_station)
2212 		return -EOPNOTSUPP;
2213 
2214 	memset(sinfo, 0, sizeof(*sinfo));
2215 
2216 	return rdev_get_station(rdev, dev, mac_addr, sinfo);
2217 }
2218 EXPORT_SYMBOL(cfg80211_get_station);
2219 
2220 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
2221 {
2222 	int i;
2223 
2224 	if (!f)
2225 		return;
2226 
2227 	kfree(f->serv_spec_info);
2228 	kfree(f->srf_bf);
2229 	kfree(f->srf_macs);
2230 	for (i = 0; i < f->num_rx_filters; i++)
2231 		kfree(f->rx_filters[i].filter);
2232 
2233 	for (i = 0; i < f->num_tx_filters; i++)
2234 		kfree(f->tx_filters[i].filter);
2235 
2236 	kfree(f->rx_filters);
2237 	kfree(f->tx_filters);
2238 	kfree(f);
2239 }
2240 EXPORT_SYMBOL(cfg80211_free_nan_func);
2241 
2242 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
2243 				u32 center_freq_khz, u32 bw_khz)
2244 {
2245 	u32 start_freq_khz, end_freq_khz;
2246 
2247 	start_freq_khz = center_freq_khz - (bw_khz / 2);
2248 	end_freq_khz = center_freq_khz + (bw_khz / 2);
2249 
2250 	if (start_freq_khz >= freq_range->start_freq_khz &&
2251 	    end_freq_khz <= freq_range->end_freq_khz)
2252 		return true;
2253 
2254 	return false;
2255 }
2256 
2257 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
2258 {
2259 	sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
2260 				sizeof(*(sinfo->pertid)),
2261 				gfp);
2262 	if (!sinfo->pertid)
2263 		return -ENOMEM;
2264 
2265 	return 0;
2266 }
2267 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
2268 
2269 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
2270 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
2271 const unsigned char rfc1042_header[] __aligned(2) =
2272 	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2273 EXPORT_SYMBOL(rfc1042_header);
2274 
2275 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
2276 const unsigned char bridge_tunnel_header[] __aligned(2) =
2277 	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
2278 EXPORT_SYMBOL(bridge_tunnel_header);
2279 
2280 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
2281 struct iapp_layer2_update {
2282 	u8 da[ETH_ALEN];	/* broadcast */
2283 	u8 sa[ETH_ALEN];	/* STA addr */
2284 	__be16 len;		/* 6 */
2285 	u8 dsap;		/* 0 */
2286 	u8 ssap;		/* 0 */
2287 	u8 control;
2288 	u8 xid_info[3];
2289 } __packed;
2290 
2291 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
2292 {
2293 	struct iapp_layer2_update *msg;
2294 	struct sk_buff *skb;
2295 
2296 	/* Send Level 2 Update Frame to update forwarding tables in layer 2
2297 	 * bridge devices */
2298 
2299 	skb = dev_alloc_skb(sizeof(*msg));
2300 	if (!skb)
2301 		return;
2302 	msg = skb_put(skb, sizeof(*msg));
2303 
2304 	/* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2305 	 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2306 
2307 	eth_broadcast_addr(msg->da);
2308 	ether_addr_copy(msg->sa, addr);
2309 	msg->len = htons(6);
2310 	msg->dsap = 0;
2311 	msg->ssap = 0x01;	/* NULL LSAP, CR Bit: Response */
2312 	msg->control = 0xaf;	/* XID response lsb.1111F101.
2313 				 * F=0 (no poll command; unsolicited frame) */
2314 	msg->xid_info[0] = 0x81;	/* XID format identifier */
2315 	msg->xid_info[1] = 1;	/* LLC types/classes: Type 1 LLC */
2316 	msg->xid_info[2] = 0;	/* XID sender's receive window size (RW) */
2317 
2318 	skb->dev = dev;
2319 	skb->protocol = eth_type_trans(skb, dev);
2320 	memset(skb->cb, 0, sizeof(skb->cb));
2321 	netif_rx(skb);
2322 }
2323 EXPORT_SYMBOL(cfg80211_send_layer2_update);
2324 
2325 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2326 			      enum ieee80211_vht_chanwidth bw,
2327 			      int mcs, bool ext_nss_bw_capable,
2328 			      unsigned int max_vht_nss)
2329 {
2330 	u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
2331 	int ext_nss_bw;
2332 	int supp_width;
2333 	int i, mcs_encoding;
2334 
2335 	if (map == 0xffff)
2336 		return 0;
2337 
2338 	if (WARN_ON(mcs > 9 || max_vht_nss > 8))
2339 		return 0;
2340 	if (mcs <= 7)
2341 		mcs_encoding = 0;
2342 	else if (mcs == 8)
2343 		mcs_encoding = 1;
2344 	else
2345 		mcs_encoding = 2;
2346 
2347 	if (!max_vht_nss) {
2348 		/* find max_vht_nss for the given MCS */
2349 		for (i = 7; i >= 0; i--) {
2350 			int supp = (map >> (2 * i)) & 3;
2351 
2352 			if (supp == 3)
2353 				continue;
2354 
2355 			if (supp >= mcs_encoding) {
2356 				max_vht_nss = i + 1;
2357 				break;
2358 			}
2359 		}
2360 	}
2361 
2362 	if (!(cap->supp_mcs.tx_mcs_map &
2363 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2364 		return max_vht_nss;
2365 
2366 	ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2367 				   IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2368 	supp_width = le32_get_bits(cap->vht_cap_info,
2369 				   IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2370 
2371 	/* if not capable, treat ext_nss_bw as 0 */
2372 	if (!ext_nss_bw_capable)
2373 		ext_nss_bw = 0;
2374 
2375 	/* This is invalid */
2376 	if (supp_width == 3)
2377 		return 0;
2378 
2379 	/* This is an invalid combination so pretend nothing is supported */
2380 	if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2381 		return 0;
2382 
2383 	/*
2384 	 * Cover all the special cases according to IEEE 802.11-2016
2385 	 * Table 9-250. All other cases are either factor of 1 or not
2386 	 * valid/supported.
2387 	 */
2388 	switch (bw) {
2389 	case IEEE80211_VHT_CHANWIDTH_USE_HT:
2390 	case IEEE80211_VHT_CHANWIDTH_80MHZ:
2391 		if ((supp_width == 1 || supp_width == 2) &&
2392 		    ext_nss_bw == 3)
2393 			return 2 * max_vht_nss;
2394 		break;
2395 	case IEEE80211_VHT_CHANWIDTH_160MHZ:
2396 		if (supp_width == 0 &&
2397 		    (ext_nss_bw == 1 || ext_nss_bw == 2))
2398 			return max_vht_nss / 2;
2399 		if (supp_width == 0 &&
2400 		    ext_nss_bw == 3)
2401 			return (3 * max_vht_nss) / 4;
2402 		if (supp_width == 1 &&
2403 		    ext_nss_bw == 3)
2404 			return 2 * max_vht_nss;
2405 		break;
2406 	case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2407 		if (supp_width == 0 && ext_nss_bw == 1)
2408 			return 0; /* not possible */
2409 		if (supp_width == 0 &&
2410 		    ext_nss_bw == 2)
2411 			return max_vht_nss / 2;
2412 		if (supp_width == 0 &&
2413 		    ext_nss_bw == 3)
2414 			return (3 * max_vht_nss) / 4;
2415 		if (supp_width == 1 &&
2416 		    ext_nss_bw == 0)
2417 			return 0; /* not possible */
2418 		if (supp_width == 1 &&
2419 		    ext_nss_bw == 1)
2420 			return max_vht_nss / 2;
2421 		if (supp_width == 1 &&
2422 		    ext_nss_bw == 2)
2423 			return (3 * max_vht_nss) / 4;
2424 		break;
2425 	}
2426 
2427 	/* not covered or invalid combination received */
2428 	return max_vht_nss;
2429 }
2430 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2431 
2432 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2433 			     bool is_4addr, u8 check_swif)
2434 
2435 {
2436 	bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2437 
2438 	switch (check_swif) {
2439 	case 0:
2440 		if (is_vlan && is_4addr)
2441 			return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2442 		return wiphy->interface_modes & BIT(iftype);
2443 	case 1:
2444 		if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2445 			return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2446 		return wiphy->software_iftypes & BIT(iftype);
2447 	default:
2448 		break;
2449 	}
2450 
2451 	return false;
2452 }
2453 EXPORT_SYMBOL(cfg80211_iftype_allowed);
2454 
2455 void cfg80211_remove_link(struct wireless_dev *wdev, unsigned int link_id)
2456 {
2457 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
2458 
2459 	ASSERT_WDEV_LOCK(wdev);
2460 
2461 	switch (wdev->iftype) {
2462 	case NL80211_IFTYPE_AP:
2463 	case NL80211_IFTYPE_P2P_GO:
2464 		__cfg80211_stop_ap(rdev, wdev->netdev, link_id, true);
2465 		break;
2466 	default:
2467 		/* per-link not relevant */
2468 		break;
2469 	}
2470 
2471 	wdev->valid_links &= ~BIT(link_id);
2472 
2473 	rdev_del_intf_link(rdev, wdev, link_id);
2474 
2475 	eth_zero_addr(wdev->links[link_id].addr);
2476 }
2477 
2478 void cfg80211_remove_links(struct wireless_dev *wdev)
2479 {
2480 	unsigned int link_id;
2481 
2482 	wdev_lock(wdev);
2483 	if (wdev->valid_links) {
2484 		for_each_valid_link(wdev, link_id)
2485 			cfg80211_remove_link(wdev, link_id);
2486 	}
2487 	wdev_unlock(wdev);
2488 }
2489 
2490 int cfg80211_remove_virtual_intf(struct cfg80211_registered_device *rdev,
2491 				 struct wireless_dev *wdev)
2492 {
2493 	cfg80211_remove_links(wdev);
2494 
2495 	return rdev_del_virtual_intf(rdev, wdev);
2496 }
2497 
2498 const struct wiphy_iftype_ext_capab *
2499 cfg80211_get_iftype_ext_capa(struct wiphy *wiphy, enum nl80211_iftype type)
2500 {
2501 	int i;
2502 
2503 	for (i = 0; i < wiphy->num_iftype_ext_capab; i++) {
2504 		if (wiphy->iftype_ext_capab[i].iftype == type)
2505 			return &wiphy->iftype_ext_capab[i];
2506 	}
2507 
2508 	return NULL;
2509 }
2510 EXPORT_SYMBOL(cfg80211_get_iftype_ext_capa);
2511