xref: /openbmc/linux/net/wireless/util.c (revision 9dd7c463)
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-2020 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 + 8)
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);
579 
580 	mesh_flags &= MESH_FLAGS_AE;
581 
582 	switch (hdr->frame_control &
583 		cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
584 	case cpu_to_le16(IEEE80211_FCTL_TODS):
585 		if (unlikely(iftype != NL80211_IFTYPE_AP &&
586 			     iftype != NL80211_IFTYPE_AP_VLAN &&
587 			     iftype != NL80211_IFTYPE_P2P_GO))
588 			return -1;
589 		break;
590 	case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
591 		if (unlikely(iftype != NL80211_IFTYPE_MESH_POINT &&
592 			     iftype != NL80211_IFTYPE_AP_VLAN &&
593 			     iftype != NL80211_IFTYPE_STATION))
594 			return -1;
595 		if (iftype == NL80211_IFTYPE_MESH_POINT) {
596 			if (mesh_flags == MESH_FLAGS_AE_A4)
597 				return -1;
598 			if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
599 				skb_copy_bits(skb, hdrlen +
600 					offsetof(struct ieee80211s_hdr, eaddr1),
601 					tmp.h_dest, 2 * ETH_ALEN);
602 			}
603 			hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
604 		}
605 		break;
606 	case cpu_to_le16(IEEE80211_FCTL_FROMDS):
607 		if ((iftype != NL80211_IFTYPE_STATION &&
608 		     iftype != NL80211_IFTYPE_P2P_CLIENT &&
609 		     iftype != NL80211_IFTYPE_MESH_POINT) ||
610 		    (is_multicast_ether_addr(tmp.h_dest) &&
611 		     ether_addr_equal(tmp.h_source, addr)))
612 			return -1;
613 		if (iftype == NL80211_IFTYPE_MESH_POINT) {
614 			if (mesh_flags == MESH_FLAGS_AE_A5_A6)
615 				return -1;
616 			if (mesh_flags == MESH_FLAGS_AE_A4)
617 				skb_copy_bits(skb, hdrlen +
618 					offsetof(struct ieee80211s_hdr, eaddr1),
619 					tmp.h_source, ETH_ALEN);
620 			hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
621 		}
622 		break;
623 	case cpu_to_le16(0):
624 		if (iftype != NL80211_IFTYPE_ADHOC &&
625 		    iftype != NL80211_IFTYPE_STATION &&
626 		    iftype != NL80211_IFTYPE_OCB)
627 				return -1;
628 		break;
629 	}
630 
631 	skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
632 	tmp.h_proto = payload.proto;
633 
634 	if (likely((!is_amsdu && ether_addr_equal(payload.hdr, rfc1042_header) &&
635 		    tmp.h_proto != htons(ETH_P_AARP) &&
636 		    tmp.h_proto != htons(ETH_P_IPX)) ||
637 		   ether_addr_equal(payload.hdr, bridge_tunnel_header)))
638 		/* remove RFC1042 or Bridge-Tunnel encapsulation and
639 		 * replace EtherType */
640 		hdrlen += ETH_ALEN + 2;
641 	else
642 		tmp.h_proto = htons(skb->len - hdrlen);
643 
644 	pskb_pull(skb, hdrlen);
645 
646 	if (!ehdr)
647 		ehdr = skb_push(skb, sizeof(struct ethhdr));
648 	memcpy(ehdr, &tmp, sizeof(tmp));
649 
650 	return 0;
651 }
652 EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
653 
654 static void
655 __frame_add_frag(struct sk_buff *skb, struct page *page,
656 		 void *ptr, int len, int size)
657 {
658 	struct skb_shared_info *sh = skb_shinfo(skb);
659 	int page_offset;
660 
661 	get_page(page);
662 	page_offset = ptr - page_address(page);
663 	skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
664 }
665 
666 static void
667 __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
668 			    int offset, int len)
669 {
670 	struct skb_shared_info *sh = skb_shinfo(skb);
671 	const skb_frag_t *frag = &sh->frags[0];
672 	struct page *frag_page;
673 	void *frag_ptr;
674 	int frag_len, frag_size;
675 	int head_size = skb->len - skb->data_len;
676 	int cur_len;
677 
678 	frag_page = virt_to_head_page(skb->head);
679 	frag_ptr = skb->data;
680 	frag_size = head_size;
681 
682 	while (offset >= frag_size) {
683 		offset -= frag_size;
684 		frag_page = skb_frag_page(frag);
685 		frag_ptr = skb_frag_address(frag);
686 		frag_size = skb_frag_size(frag);
687 		frag++;
688 	}
689 
690 	frag_ptr += offset;
691 	frag_len = frag_size - offset;
692 
693 	cur_len = min(len, frag_len);
694 
695 	__frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
696 	len -= cur_len;
697 
698 	while (len > 0) {
699 		frag_len = skb_frag_size(frag);
700 		cur_len = min(len, frag_len);
701 		__frame_add_frag(frame, skb_frag_page(frag),
702 				 skb_frag_address(frag), cur_len, frag_len);
703 		len -= cur_len;
704 		frag++;
705 	}
706 }
707 
708 static struct sk_buff *
709 __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
710 		       int offset, int len, bool reuse_frag)
711 {
712 	struct sk_buff *frame;
713 	int cur_len = len;
714 
715 	if (skb->len - offset < len)
716 		return NULL;
717 
718 	/*
719 	 * When reusing framents, copy some data to the head to simplify
720 	 * ethernet header handling and speed up protocol header processing
721 	 * in the stack later.
722 	 */
723 	if (reuse_frag)
724 		cur_len = min_t(int, len, 32);
725 
726 	/*
727 	 * Allocate and reserve two bytes more for payload
728 	 * alignment since sizeof(struct ethhdr) is 14.
729 	 */
730 	frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
731 	if (!frame)
732 		return NULL;
733 
734 	skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
735 	skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
736 
737 	len -= cur_len;
738 	if (!len)
739 		return frame;
740 
741 	offset += cur_len;
742 	__ieee80211_amsdu_copy_frag(skb, frame, offset, len);
743 
744 	return frame;
745 }
746 
747 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
748 			      const u8 *addr, enum nl80211_iftype iftype,
749 			      const unsigned int extra_headroom,
750 			      const u8 *check_da, const u8 *check_sa)
751 {
752 	unsigned int hlen = ALIGN(extra_headroom, 4);
753 	struct sk_buff *frame = NULL;
754 	u16 ethertype;
755 	u8 *payload;
756 	int offset = 0, remaining;
757 	struct ethhdr eth;
758 	bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
759 	bool reuse_skb = false;
760 	bool last = false;
761 
762 	while (!last) {
763 		unsigned int subframe_len;
764 		int len;
765 		u8 padding;
766 
767 		skb_copy_bits(skb, offset, &eth, sizeof(eth));
768 		len = ntohs(eth.h_proto);
769 		subframe_len = sizeof(struct ethhdr) + len;
770 		padding = (4 - subframe_len) & 0x3;
771 
772 		/* the last MSDU has no padding */
773 		remaining = skb->len - offset;
774 		if (subframe_len > remaining)
775 			goto purge;
776 		/* mitigate A-MSDU aggregation injection attacks */
777 		if (ether_addr_equal(eth.h_dest, rfc1042_header))
778 			goto purge;
779 
780 		offset += sizeof(struct ethhdr);
781 		last = remaining <= subframe_len + padding;
782 
783 		/* FIXME: should we really accept multicast DA? */
784 		if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
785 		     !ether_addr_equal(check_da, eth.h_dest)) ||
786 		    (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
787 			offset += len + padding;
788 			continue;
789 		}
790 
791 		/* reuse skb for the last subframe */
792 		if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
793 			skb_pull(skb, offset);
794 			frame = skb;
795 			reuse_skb = true;
796 		} else {
797 			frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
798 						       reuse_frag);
799 			if (!frame)
800 				goto purge;
801 
802 			offset += len + padding;
803 		}
804 
805 		skb_reset_network_header(frame);
806 		frame->dev = skb->dev;
807 		frame->priority = skb->priority;
808 
809 		payload = frame->data;
810 		ethertype = (payload[6] << 8) | payload[7];
811 		if (likely((ether_addr_equal(payload, rfc1042_header) &&
812 			    ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
813 			   ether_addr_equal(payload, bridge_tunnel_header))) {
814 			eth.h_proto = htons(ethertype);
815 			skb_pull(frame, ETH_ALEN + 2);
816 		}
817 
818 		memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
819 		__skb_queue_tail(list, frame);
820 	}
821 
822 	if (!reuse_skb)
823 		dev_kfree_skb(skb);
824 
825 	return;
826 
827  purge:
828 	__skb_queue_purge(list);
829 	dev_kfree_skb(skb);
830 }
831 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
832 
833 /* Given a data frame determine the 802.1p/1d tag to use. */
834 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
835 				    struct cfg80211_qos_map *qos_map)
836 {
837 	unsigned int dscp;
838 	unsigned char vlan_priority;
839 	unsigned int ret;
840 
841 	/* skb->priority values from 256->263 are magic values to
842 	 * directly indicate a specific 802.1d priority.  This is used
843 	 * to allow 802.1d priority to be passed directly in from VLAN
844 	 * tags, etc.
845 	 */
846 	if (skb->priority >= 256 && skb->priority <= 263) {
847 		ret = skb->priority - 256;
848 		goto out;
849 	}
850 
851 	if (skb_vlan_tag_present(skb)) {
852 		vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
853 			>> VLAN_PRIO_SHIFT;
854 		if (vlan_priority > 0) {
855 			ret = vlan_priority;
856 			goto out;
857 		}
858 	}
859 
860 	switch (skb->protocol) {
861 	case htons(ETH_P_IP):
862 		dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
863 		break;
864 	case htons(ETH_P_IPV6):
865 		dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
866 		break;
867 	case htons(ETH_P_MPLS_UC):
868 	case htons(ETH_P_MPLS_MC): {
869 		struct mpls_label mpls_tmp, *mpls;
870 
871 		mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
872 					  sizeof(*mpls), &mpls_tmp);
873 		if (!mpls)
874 			return 0;
875 
876 		ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
877 			>> MPLS_LS_TC_SHIFT;
878 		goto out;
879 	}
880 	case htons(ETH_P_80221):
881 		/* 802.21 is always network control traffic */
882 		return 7;
883 	default:
884 		return 0;
885 	}
886 
887 	if (qos_map) {
888 		unsigned int i, tmp_dscp = dscp >> 2;
889 
890 		for (i = 0; i < qos_map->num_des; i++) {
891 			if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
892 				ret = qos_map->dscp_exception[i].up;
893 				goto out;
894 			}
895 		}
896 
897 		for (i = 0; i < 8; i++) {
898 			if (tmp_dscp >= qos_map->up[i].low &&
899 			    tmp_dscp <= qos_map->up[i].high) {
900 				ret = i;
901 				goto out;
902 			}
903 		}
904 	}
905 
906 	ret = dscp >> 5;
907 out:
908 	return array_index_nospec(ret, IEEE80211_NUM_TIDS);
909 }
910 EXPORT_SYMBOL(cfg80211_classify8021d);
911 
912 const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
913 {
914 	const struct cfg80211_bss_ies *ies;
915 
916 	ies = rcu_dereference(bss->ies);
917 	if (!ies)
918 		return NULL;
919 
920 	return cfg80211_find_elem(id, ies->data, ies->len);
921 }
922 EXPORT_SYMBOL(ieee80211_bss_get_elem);
923 
924 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
925 {
926 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
927 	struct net_device *dev = wdev->netdev;
928 	int i;
929 
930 	if (!wdev->connect_keys)
931 		return;
932 
933 	for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
934 		if (!wdev->connect_keys->params[i].cipher)
935 			continue;
936 		if (rdev_add_key(rdev, dev, i, false, NULL,
937 				 &wdev->connect_keys->params[i])) {
938 			netdev_err(dev, "failed to set key %d\n", i);
939 			continue;
940 		}
941 		if (wdev->connect_keys->def == i &&
942 		    rdev_set_default_key(rdev, dev, i, true, true)) {
943 			netdev_err(dev, "failed to set defkey %d\n", i);
944 			continue;
945 		}
946 	}
947 
948 	kfree_sensitive(wdev->connect_keys);
949 	wdev->connect_keys = NULL;
950 }
951 
952 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
953 {
954 	struct cfg80211_event *ev;
955 	unsigned long flags;
956 
957 	spin_lock_irqsave(&wdev->event_lock, flags);
958 	while (!list_empty(&wdev->event_list)) {
959 		ev = list_first_entry(&wdev->event_list,
960 				      struct cfg80211_event, list);
961 		list_del(&ev->list);
962 		spin_unlock_irqrestore(&wdev->event_lock, flags);
963 
964 		wdev_lock(wdev);
965 		switch (ev->type) {
966 		case EVENT_CONNECT_RESULT:
967 			__cfg80211_connect_result(
968 				wdev->netdev,
969 				&ev->cr,
970 				ev->cr.status == WLAN_STATUS_SUCCESS);
971 			break;
972 		case EVENT_ROAMED:
973 			__cfg80211_roamed(wdev, &ev->rm);
974 			break;
975 		case EVENT_DISCONNECTED:
976 			__cfg80211_disconnected(wdev->netdev,
977 						ev->dc.ie, ev->dc.ie_len,
978 						ev->dc.reason,
979 						!ev->dc.locally_generated);
980 			break;
981 		case EVENT_IBSS_JOINED:
982 			__cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
983 					       ev->ij.channel);
984 			break;
985 		case EVENT_STOPPED:
986 			__cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
987 			break;
988 		case EVENT_PORT_AUTHORIZED:
989 			__cfg80211_port_authorized(wdev, ev->pa.bssid);
990 			break;
991 		}
992 		wdev_unlock(wdev);
993 
994 		kfree(ev);
995 
996 		spin_lock_irqsave(&wdev->event_lock, flags);
997 	}
998 	spin_unlock_irqrestore(&wdev->event_lock, flags);
999 }
1000 
1001 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
1002 {
1003 	struct wireless_dev *wdev;
1004 
1005 	lockdep_assert_held(&rdev->wiphy.mtx);
1006 
1007 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
1008 		cfg80211_process_wdev_events(wdev);
1009 }
1010 
1011 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
1012 			  struct net_device *dev, enum nl80211_iftype ntype,
1013 			  struct vif_params *params)
1014 {
1015 	int err;
1016 	enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
1017 
1018 	lockdep_assert_held(&rdev->wiphy.mtx);
1019 
1020 	/* don't support changing VLANs, you just re-create them */
1021 	if (otype == NL80211_IFTYPE_AP_VLAN)
1022 		return -EOPNOTSUPP;
1023 
1024 	/* cannot change into P2P device or NAN */
1025 	if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
1026 	    ntype == NL80211_IFTYPE_NAN)
1027 		return -EOPNOTSUPP;
1028 
1029 	if (!rdev->ops->change_virtual_intf ||
1030 	    !(rdev->wiphy.interface_modes & (1 << ntype)))
1031 		return -EOPNOTSUPP;
1032 
1033 	if (ntype != otype) {
1034 		/* if it's part of a bridge, reject changing type to station/ibss */
1035 		if (netif_is_bridge_port(dev) &&
1036 		    (ntype == NL80211_IFTYPE_ADHOC ||
1037 		     ntype == NL80211_IFTYPE_STATION ||
1038 		     ntype == NL80211_IFTYPE_P2P_CLIENT))
1039 			return -EBUSY;
1040 
1041 		dev->ieee80211_ptr->use_4addr = false;
1042 		dev->ieee80211_ptr->mesh_id_up_len = 0;
1043 		wdev_lock(dev->ieee80211_ptr);
1044 		rdev_set_qos_map(rdev, dev, NULL);
1045 		wdev_unlock(dev->ieee80211_ptr);
1046 
1047 		switch (otype) {
1048 		case NL80211_IFTYPE_AP:
1049 		case NL80211_IFTYPE_P2P_GO:
1050 			cfg80211_stop_ap(rdev, dev, true);
1051 			break;
1052 		case NL80211_IFTYPE_ADHOC:
1053 			cfg80211_leave_ibss(rdev, dev, false);
1054 			break;
1055 		case NL80211_IFTYPE_STATION:
1056 		case NL80211_IFTYPE_P2P_CLIENT:
1057 			wdev_lock(dev->ieee80211_ptr);
1058 			cfg80211_disconnect(rdev, dev,
1059 					    WLAN_REASON_DEAUTH_LEAVING, true);
1060 			wdev_unlock(dev->ieee80211_ptr);
1061 			break;
1062 		case NL80211_IFTYPE_MESH_POINT:
1063 			/* mesh should be handled? */
1064 			break;
1065 		case NL80211_IFTYPE_OCB:
1066 			cfg80211_leave_ocb(rdev, dev);
1067 			break;
1068 		default:
1069 			break;
1070 		}
1071 
1072 		cfg80211_process_rdev_events(rdev);
1073 		cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
1074 	}
1075 
1076 	err = rdev_change_virtual_intf(rdev, dev, ntype, params);
1077 
1078 	WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1079 
1080 	if (!err && params && params->use_4addr != -1)
1081 		dev->ieee80211_ptr->use_4addr = params->use_4addr;
1082 
1083 	if (!err) {
1084 		dev->priv_flags &= ~IFF_DONT_BRIDGE;
1085 		switch (ntype) {
1086 		case NL80211_IFTYPE_STATION:
1087 			if (dev->ieee80211_ptr->use_4addr)
1088 				break;
1089 			fallthrough;
1090 		case NL80211_IFTYPE_OCB:
1091 		case NL80211_IFTYPE_P2P_CLIENT:
1092 		case NL80211_IFTYPE_ADHOC:
1093 			dev->priv_flags |= IFF_DONT_BRIDGE;
1094 			break;
1095 		case NL80211_IFTYPE_P2P_GO:
1096 		case NL80211_IFTYPE_AP:
1097 		case NL80211_IFTYPE_AP_VLAN:
1098 		case NL80211_IFTYPE_MESH_POINT:
1099 			/* bridging OK */
1100 			break;
1101 		case NL80211_IFTYPE_MONITOR:
1102 			/* monitor can't bridge anyway */
1103 			break;
1104 		case NL80211_IFTYPE_UNSPECIFIED:
1105 		case NUM_NL80211_IFTYPES:
1106 			/* not happening */
1107 			break;
1108 		case NL80211_IFTYPE_P2P_DEVICE:
1109 		case NL80211_IFTYPE_WDS:
1110 		case NL80211_IFTYPE_NAN:
1111 			WARN_ON(1);
1112 			break;
1113 		}
1114 	}
1115 
1116 	if (!err && ntype != otype && netif_running(dev)) {
1117 		cfg80211_update_iface_num(rdev, ntype, 1);
1118 		cfg80211_update_iface_num(rdev, otype, -1);
1119 	}
1120 
1121 	return err;
1122 }
1123 
1124 static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1125 {
1126 	int modulation, streams, bitrate;
1127 
1128 	/* the formula below does only work for MCS values smaller than 32 */
1129 	if (WARN_ON_ONCE(rate->mcs >= 32))
1130 		return 0;
1131 
1132 	modulation = rate->mcs & 7;
1133 	streams = (rate->mcs >> 3) + 1;
1134 
1135 	bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1136 
1137 	if (modulation < 4)
1138 		bitrate *= (modulation + 1);
1139 	else if (modulation == 4)
1140 		bitrate *= (modulation + 2);
1141 	else
1142 		bitrate *= (modulation + 3);
1143 
1144 	bitrate *= streams;
1145 
1146 	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1147 		bitrate = (bitrate / 9) * 10;
1148 
1149 	/* do NOT round down here */
1150 	return (bitrate + 50000) / 100000;
1151 }
1152 
1153 static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
1154 {
1155 	static const u32 __mcs2bitrate[] = {
1156 		/* control PHY */
1157 		[0] =   275,
1158 		/* SC PHY */
1159 		[1] =  3850,
1160 		[2] =  7700,
1161 		[3] =  9625,
1162 		[4] = 11550,
1163 		[5] = 12512, /* 1251.25 mbps */
1164 		[6] = 15400,
1165 		[7] = 19250,
1166 		[8] = 23100,
1167 		[9] = 25025,
1168 		[10] = 30800,
1169 		[11] = 38500,
1170 		[12] = 46200,
1171 		/* OFDM PHY */
1172 		[13] =  6930,
1173 		[14] =  8662, /* 866.25 mbps */
1174 		[15] = 13860,
1175 		[16] = 17325,
1176 		[17] = 20790,
1177 		[18] = 27720,
1178 		[19] = 34650,
1179 		[20] = 41580,
1180 		[21] = 45045,
1181 		[22] = 51975,
1182 		[23] = 62370,
1183 		[24] = 67568, /* 6756.75 mbps */
1184 		/* LP-SC PHY */
1185 		[25] =  6260,
1186 		[26] =  8340,
1187 		[27] = 11120,
1188 		[28] = 12510,
1189 		[29] = 16680,
1190 		[30] = 22240,
1191 		[31] = 25030,
1192 	};
1193 
1194 	if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1195 		return 0;
1196 
1197 	return __mcs2bitrate[rate->mcs];
1198 }
1199 
1200 static u32 cfg80211_calculate_bitrate_extended_sc_dmg(struct rate_info *rate)
1201 {
1202 	static const u32 __mcs2bitrate[] = {
1203 		[6 - 6] = 26950, /* MCS 9.1 : 2695.0 mbps */
1204 		[7 - 6] = 50050, /* MCS 12.1 */
1205 		[8 - 6] = 53900,
1206 		[9 - 6] = 57750,
1207 		[10 - 6] = 63900,
1208 		[11 - 6] = 75075,
1209 		[12 - 6] = 80850,
1210 	};
1211 
1212 	/* Extended SC MCS not defined for base MCS below 6 or above 12 */
1213 	if (WARN_ON_ONCE(rate->mcs < 6 || rate->mcs > 12))
1214 		return 0;
1215 
1216 	return __mcs2bitrate[rate->mcs - 6];
1217 }
1218 
1219 static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
1220 {
1221 	static const u32 __mcs2bitrate[] = {
1222 		/* control PHY */
1223 		[0] =   275,
1224 		/* SC PHY */
1225 		[1] =  3850,
1226 		[2] =  7700,
1227 		[3] =  9625,
1228 		[4] = 11550,
1229 		[5] = 12512, /* 1251.25 mbps */
1230 		[6] = 13475,
1231 		[7] = 15400,
1232 		[8] = 19250,
1233 		[9] = 23100,
1234 		[10] = 25025,
1235 		[11] = 26950,
1236 		[12] = 30800,
1237 		[13] = 38500,
1238 		[14] = 46200,
1239 		[15] = 50050,
1240 		[16] = 53900,
1241 		[17] = 57750,
1242 		[18] = 69300,
1243 		[19] = 75075,
1244 		[20] = 80850,
1245 	};
1246 
1247 	if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1248 		return 0;
1249 
1250 	return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
1251 }
1252 
1253 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1254 {
1255 	static const u32 base[4][12] = {
1256 		{   6500000,
1257 		   13000000,
1258 		   19500000,
1259 		   26000000,
1260 		   39000000,
1261 		   52000000,
1262 		   58500000,
1263 		   65000000,
1264 		   78000000,
1265 		/* not in the spec, but some devices use this: */
1266 		   86700000,
1267 		   97500000,
1268 		  108300000,
1269 		},
1270 		{  13500000,
1271 		   27000000,
1272 		   40500000,
1273 		   54000000,
1274 		   81000000,
1275 		  108000000,
1276 		  121500000,
1277 		  135000000,
1278 		  162000000,
1279 		  180000000,
1280 		  202500000,
1281 		  225000000,
1282 		},
1283 		{  29300000,
1284 		   58500000,
1285 		   87800000,
1286 		  117000000,
1287 		  175500000,
1288 		  234000000,
1289 		  263300000,
1290 		  292500000,
1291 		  351000000,
1292 		  390000000,
1293 		  438800000,
1294 		  487500000,
1295 		},
1296 		{  58500000,
1297 		  117000000,
1298 		  175500000,
1299 		  234000000,
1300 		  351000000,
1301 		  468000000,
1302 		  526500000,
1303 		  585000000,
1304 		  702000000,
1305 		  780000000,
1306 		  877500000,
1307 		  975000000,
1308 		},
1309 	};
1310 	u32 bitrate;
1311 	int idx;
1312 
1313 	if (rate->mcs > 11)
1314 		goto warn;
1315 
1316 	switch (rate->bw) {
1317 	case RATE_INFO_BW_160:
1318 		idx = 3;
1319 		break;
1320 	case RATE_INFO_BW_80:
1321 		idx = 2;
1322 		break;
1323 	case RATE_INFO_BW_40:
1324 		idx = 1;
1325 		break;
1326 	case RATE_INFO_BW_5:
1327 	case RATE_INFO_BW_10:
1328 	default:
1329 		goto warn;
1330 	case RATE_INFO_BW_20:
1331 		idx = 0;
1332 	}
1333 
1334 	bitrate = base[idx][rate->mcs];
1335 	bitrate *= rate->nss;
1336 
1337 	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1338 		bitrate = (bitrate / 9) * 10;
1339 
1340 	/* do NOT round down here */
1341 	return (bitrate + 50000) / 100000;
1342  warn:
1343 	WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1344 		  rate->bw, rate->mcs, rate->nss);
1345 	return 0;
1346 }
1347 
1348 static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1349 {
1350 #define SCALE 6144
1351 	u32 mcs_divisors[14] = {
1352 		102399, /* 16.666666... */
1353 		 51201, /*  8.333333... */
1354 		 34134, /*  5.555555... */
1355 		 25599, /*  4.166666... */
1356 		 17067, /*  2.777777... */
1357 		 12801, /*  2.083333... */
1358 		 11769, /*  1.851851... */
1359 		 10239, /*  1.666666... */
1360 		  8532, /*  1.388888... */
1361 		  7680, /*  1.250000... */
1362 		  6828, /*  1.111111... */
1363 		  6144, /*  1.000000... */
1364 		  5690, /*  0.926106... */
1365 		  5120, /*  0.833333... */
1366 	};
1367 	u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1368 	u32 rates_969[3] =  { 480388888, 453700000, 408333333 };
1369 	u32 rates_484[3] =  { 229411111, 216666666, 195000000 };
1370 	u32 rates_242[3] =  { 114711111, 108333333,  97500000 };
1371 	u32 rates_106[3] =  {  40000000,  37777777,  34000000 };
1372 	u32 rates_52[3]  =  {  18820000,  17777777,  16000000 };
1373 	u32 rates_26[3]  =  {   9411111,   8888888,   8000000 };
1374 	u64 tmp;
1375 	u32 result;
1376 
1377 	if (WARN_ON_ONCE(rate->mcs > 13))
1378 		return 0;
1379 
1380 	if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1381 		return 0;
1382 	if (WARN_ON_ONCE(rate->he_ru_alloc >
1383 			 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1384 		return 0;
1385 	if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1386 		return 0;
1387 
1388 	if (rate->bw == RATE_INFO_BW_160)
1389 		result = rates_160M[rate->he_gi];
1390 	else if (rate->bw == RATE_INFO_BW_80 ||
1391 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1392 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1393 		result = rates_969[rate->he_gi];
1394 	else if (rate->bw == RATE_INFO_BW_40 ||
1395 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1396 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1397 		result = rates_484[rate->he_gi];
1398 	else if (rate->bw == RATE_INFO_BW_20 ||
1399 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1400 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1401 		result = rates_242[rate->he_gi];
1402 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1403 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1404 		result = rates_106[rate->he_gi];
1405 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1406 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1407 		result = rates_52[rate->he_gi];
1408 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1409 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1410 		result = rates_26[rate->he_gi];
1411 	else {
1412 		WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1413 		     rate->bw, rate->he_ru_alloc);
1414 		return 0;
1415 	}
1416 
1417 	/* now scale to the appropriate MCS */
1418 	tmp = result;
1419 	tmp *= SCALE;
1420 	do_div(tmp, mcs_divisors[rate->mcs]);
1421 	result = tmp;
1422 
1423 	/* and take NSS, DCM into account */
1424 	result = (result * rate->nss) / 8;
1425 	if (rate->he_dcm)
1426 		result /= 2;
1427 
1428 	return result / 10000;
1429 }
1430 
1431 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1432 {
1433 	if (rate->flags & RATE_INFO_FLAGS_MCS)
1434 		return cfg80211_calculate_bitrate_ht(rate);
1435 	if (rate->flags & RATE_INFO_FLAGS_DMG)
1436 		return cfg80211_calculate_bitrate_dmg(rate);
1437 	if (rate->flags & RATE_INFO_FLAGS_EXTENDED_SC_DMG)
1438 		return cfg80211_calculate_bitrate_extended_sc_dmg(rate);
1439 	if (rate->flags & RATE_INFO_FLAGS_EDMG)
1440 		return cfg80211_calculate_bitrate_edmg(rate);
1441 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1442 		return cfg80211_calculate_bitrate_vht(rate);
1443 	if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1444 		return cfg80211_calculate_bitrate_he(rate);
1445 
1446 	return rate->legacy;
1447 }
1448 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1449 
1450 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1451 			  enum ieee80211_p2p_attr_id attr,
1452 			  u8 *buf, unsigned int bufsize)
1453 {
1454 	u8 *out = buf;
1455 	u16 attr_remaining = 0;
1456 	bool desired_attr = false;
1457 	u16 desired_len = 0;
1458 
1459 	while (len > 0) {
1460 		unsigned int iedatalen;
1461 		unsigned int copy;
1462 		const u8 *iedata;
1463 
1464 		if (len < 2)
1465 			return -EILSEQ;
1466 		iedatalen = ies[1];
1467 		if (iedatalen + 2 > len)
1468 			return -EILSEQ;
1469 
1470 		if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1471 			goto cont;
1472 
1473 		if (iedatalen < 4)
1474 			goto cont;
1475 
1476 		iedata = ies + 2;
1477 
1478 		/* check WFA OUI, P2P subtype */
1479 		if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1480 		    iedata[2] != 0x9a || iedata[3] != 0x09)
1481 			goto cont;
1482 
1483 		iedatalen -= 4;
1484 		iedata += 4;
1485 
1486 		/* check attribute continuation into this IE */
1487 		copy = min_t(unsigned int, attr_remaining, iedatalen);
1488 		if (copy && desired_attr) {
1489 			desired_len += copy;
1490 			if (out) {
1491 				memcpy(out, iedata, min(bufsize, copy));
1492 				out += min(bufsize, copy);
1493 				bufsize -= min(bufsize, copy);
1494 			}
1495 
1496 
1497 			if (copy == attr_remaining)
1498 				return desired_len;
1499 		}
1500 
1501 		attr_remaining -= copy;
1502 		if (attr_remaining)
1503 			goto cont;
1504 
1505 		iedatalen -= copy;
1506 		iedata += copy;
1507 
1508 		while (iedatalen > 0) {
1509 			u16 attr_len;
1510 
1511 			/* P2P attribute ID & size must fit */
1512 			if (iedatalen < 3)
1513 				return -EILSEQ;
1514 			desired_attr = iedata[0] == attr;
1515 			attr_len = get_unaligned_le16(iedata + 1);
1516 			iedatalen -= 3;
1517 			iedata += 3;
1518 
1519 			copy = min_t(unsigned int, attr_len, iedatalen);
1520 
1521 			if (desired_attr) {
1522 				desired_len += copy;
1523 				if (out) {
1524 					memcpy(out, iedata, min(bufsize, copy));
1525 					out += min(bufsize, copy);
1526 					bufsize -= min(bufsize, copy);
1527 				}
1528 
1529 				if (copy == attr_len)
1530 					return desired_len;
1531 			}
1532 
1533 			iedata += copy;
1534 			iedatalen -= copy;
1535 			attr_remaining = attr_len - copy;
1536 		}
1537 
1538  cont:
1539 		len -= ies[1] + 2;
1540 		ies += ies[1] + 2;
1541 	}
1542 
1543 	if (attr_remaining && desired_attr)
1544 		return -EILSEQ;
1545 
1546 	return -ENOENT;
1547 }
1548 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1549 
1550 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1551 {
1552 	int i;
1553 
1554 	/* Make sure array values are legal */
1555 	if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1556 		return false;
1557 
1558 	i = 0;
1559 	while (i < n_ids) {
1560 		if (ids[i] == WLAN_EID_EXTENSION) {
1561 			if (id_ext && (ids[i + 1] == id))
1562 				return true;
1563 
1564 			i += 2;
1565 			continue;
1566 		}
1567 
1568 		if (ids[i] == id && !id_ext)
1569 			return true;
1570 
1571 		i++;
1572 	}
1573 	return false;
1574 }
1575 
1576 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1577 {
1578 	/* we assume a validly formed IEs buffer */
1579 	u8 len = ies[pos + 1];
1580 
1581 	pos += 2 + len;
1582 
1583 	/* the IE itself must have 255 bytes for fragments to follow */
1584 	if (len < 255)
1585 		return pos;
1586 
1587 	while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1588 		len = ies[pos + 1];
1589 		pos += 2 + len;
1590 	}
1591 
1592 	return pos;
1593 }
1594 
1595 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1596 			      const u8 *ids, int n_ids,
1597 			      const u8 *after_ric, int n_after_ric,
1598 			      size_t offset)
1599 {
1600 	size_t pos = offset;
1601 
1602 	while (pos < ielen) {
1603 		u8 ext = 0;
1604 
1605 		if (ies[pos] == WLAN_EID_EXTENSION)
1606 			ext = 2;
1607 		if ((pos + ext) >= ielen)
1608 			break;
1609 
1610 		if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1611 					  ies[pos] == WLAN_EID_EXTENSION))
1612 			break;
1613 
1614 		if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1615 			pos = skip_ie(ies, ielen, pos);
1616 
1617 			while (pos < ielen) {
1618 				if (ies[pos] == WLAN_EID_EXTENSION)
1619 					ext = 2;
1620 				else
1621 					ext = 0;
1622 
1623 				if ((pos + ext) >= ielen)
1624 					break;
1625 
1626 				if (!ieee80211_id_in_list(after_ric,
1627 							  n_after_ric,
1628 							  ies[pos + ext],
1629 							  ext == 2))
1630 					pos = skip_ie(ies, ielen, pos);
1631 				else
1632 					break;
1633 			}
1634 		} else {
1635 			pos = skip_ie(ies, ielen, pos);
1636 		}
1637 	}
1638 
1639 	return pos;
1640 }
1641 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1642 
1643 bool ieee80211_operating_class_to_band(u8 operating_class,
1644 				       enum nl80211_band *band)
1645 {
1646 	switch (operating_class) {
1647 	case 112:
1648 	case 115 ... 127:
1649 	case 128 ... 130:
1650 		*band = NL80211_BAND_5GHZ;
1651 		return true;
1652 	case 131 ... 135:
1653 		*band = NL80211_BAND_6GHZ;
1654 		return true;
1655 	case 81:
1656 	case 82:
1657 	case 83:
1658 	case 84:
1659 		*band = NL80211_BAND_2GHZ;
1660 		return true;
1661 	case 180:
1662 		*band = NL80211_BAND_60GHZ;
1663 		return true;
1664 	}
1665 
1666 	return false;
1667 }
1668 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1669 
1670 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1671 					  u8 *op_class)
1672 {
1673 	u8 vht_opclass;
1674 	u32 freq = chandef->center_freq1;
1675 
1676 	if (freq >= 2412 && freq <= 2472) {
1677 		if (chandef->width > NL80211_CHAN_WIDTH_40)
1678 			return false;
1679 
1680 		/* 2.407 GHz, channels 1..13 */
1681 		if (chandef->width == NL80211_CHAN_WIDTH_40) {
1682 			if (freq > chandef->chan->center_freq)
1683 				*op_class = 83; /* HT40+ */
1684 			else
1685 				*op_class = 84; /* HT40- */
1686 		} else {
1687 			*op_class = 81;
1688 		}
1689 
1690 		return true;
1691 	}
1692 
1693 	if (freq == 2484) {
1694 		/* channel 14 is only for IEEE 802.11b */
1695 		if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
1696 			return false;
1697 
1698 		*op_class = 82; /* channel 14 */
1699 		return true;
1700 	}
1701 
1702 	switch (chandef->width) {
1703 	case NL80211_CHAN_WIDTH_80:
1704 		vht_opclass = 128;
1705 		break;
1706 	case NL80211_CHAN_WIDTH_160:
1707 		vht_opclass = 129;
1708 		break;
1709 	case NL80211_CHAN_WIDTH_80P80:
1710 		vht_opclass = 130;
1711 		break;
1712 	case NL80211_CHAN_WIDTH_10:
1713 	case NL80211_CHAN_WIDTH_5:
1714 		return false; /* unsupported for now */
1715 	default:
1716 		vht_opclass = 0;
1717 		break;
1718 	}
1719 
1720 	/* 5 GHz, channels 36..48 */
1721 	if (freq >= 5180 && freq <= 5240) {
1722 		if (vht_opclass) {
1723 			*op_class = vht_opclass;
1724 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1725 			if (freq > chandef->chan->center_freq)
1726 				*op_class = 116;
1727 			else
1728 				*op_class = 117;
1729 		} else {
1730 			*op_class = 115;
1731 		}
1732 
1733 		return true;
1734 	}
1735 
1736 	/* 5 GHz, channels 52..64 */
1737 	if (freq >= 5260 && freq <= 5320) {
1738 		if (vht_opclass) {
1739 			*op_class = vht_opclass;
1740 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1741 			if (freq > chandef->chan->center_freq)
1742 				*op_class = 119;
1743 			else
1744 				*op_class = 120;
1745 		} else {
1746 			*op_class = 118;
1747 		}
1748 
1749 		return true;
1750 	}
1751 
1752 	/* 5 GHz, channels 100..144 */
1753 	if (freq >= 5500 && freq <= 5720) {
1754 		if (vht_opclass) {
1755 			*op_class = vht_opclass;
1756 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1757 			if (freq > chandef->chan->center_freq)
1758 				*op_class = 122;
1759 			else
1760 				*op_class = 123;
1761 		} else {
1762 			*op_class = 121;
1763 		}
1764 
1765 		return true;
1766 	}
1767 
1768 	/* 5 GHz, channels 149..169 */
1769 	if (freq >= 5745 && freq <= 5845) {
1770 		if (vht_opclass) {
1771 			*op_class = vht_opclass;
1772 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1773 			if (freq > chandef->chan->center_freq)
1774 				*op_class = 126;
1775 			else
1776 				*op_class = 127;
1777 		} else if (freq <= 5805) {
1778 			*op_class = 124;
1779 		} else {
1780 			*op_class = 125;
1781 		}
1782 
1783 		return true;
1784 	}
1785 
1786 	/* 56.16 GHz, channel 1..4 */
1787 	if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
1788 		if (chandef->width >= NL80211_CHAN_WIDTH_40)
1789 			return false;
1790 
1791 		*op_class = 180;
1792 		return true;
1793 	}
1794 
1795 	/* not supported yet */
1796 	return false;
1797 }
1798 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1799 
1800 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1801 				       u32 *beacon_int_gcd,
1802 				       bool *beacon_int_different)
1803 {
1804 	struct wireless_dev *wdev;
1805 
1806 	*beacon_int_gcd = 0;
1807 	*beacon_int_different = false;
1808 
1809 	list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1810 		if (!wdev->beacon_interval)
1811 			continue;
1812 
1813 		if (!*beacon_int_gcd) {
1814 			*beacon_int_gcd = wdev->beacon_interval;
1815 			continue;
1816 		}
1817 
1818 		if (wdev->beacon_interval == *beacon_int_gcd)
1819 			continue;
1820 
1821 		*beacon_int_different = true;
1822 		*beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1823 	}
1824 
1825 	if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1826 		if (*beacon_int_gcd)
1827 			*beacon_int_different = true;
1828 		*beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1829 	}
1830 }
1831 
1832 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1833 				 enum nl80211_iftype iftype, u32 beacon_int)
1834 {
1835 	/*
1836 	 * This is just a basic pre-condition check; if interface combinations
1837 	 * are possible the driver must already be checking those with a call
1838 	 * to cfg80211_check_combinations(), in which case we'll validate more
1839 	 * through the cfg80211_calculate_bi_data() call and code in
1840 	 * cfg80211_iter_combinations().
1841 	 */
1842 
1843 	if (beacon_int < 10 || beacon_int > 10000)
1844 		return -EINVAL;
1845 
1846 	return 0;
1847 }
1848 
1849 int cfg80211_iter_combinations(struct wiphy *wiphy,
1850 			       struct iface_combination_params *params,
1851 			       void (*iter)(const struct ieee80211_iface_combination *c,
1852 					    void *data),
1853 			       void *data)
1854 {
1855 	const struct ieee80211_regdomain *regdom;
1856 	enum nl80211_dfs_regions region = 0;
1857 	int i, j, iftype;
1858 	int num_interfaces = 0;
1859 	u32 used_iftypes = 0;
1860 	u32 beacon_int_gcd;
1861 	bool beacon_int_different;
1862 
1863 	/*
1864 	 * This is a bit strange, since the iteration used to rely only on
1865 	 * the data given by the driver, but here it now relies on context,
1866 	 * in form of the currently operating interfaces.
1867 	 * This is OK for all current users, and saves us from having to
1868 	 * push the GCD calculations into all the drivers.
1869 	 * In the future, this should probably rely more on data that's in
1870 	 * cfg80211 already - the only thing not would appear to be any new
1871 	 * interfaces (while being brought up) and channel/radar data.
1872 	 */
1873 	cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1874 				   &beacon_int_gcd, &beacon_int_different);
1875 
1876 	if (params->radar_detect) {
1877 		rcu_read_lock();
1878 		regdom = rcu_dereference(cfg80211_regdomain);
1879 		if (regdom)
1880 			region = regdom->dfs_region;
1881 		rcu_read_unlock();
1882 	}
1883 
1884 	for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1885 		num_interfaces += params->iftype_num[iftype];
1886 		if (params->iftype_num[iftype] > 0 &&
1887 		    !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1888 			used_iftypes |= BIT(iftype);
1889 	}
1890 
1891 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1892 		const struct ieee80211_iface_combination *c;
1893 		struct ieee80211_iface_limit *limits;
1894 		u32 all_iftypes = 0;
1895 
1896 		c = &wiphy->iface_combinations[i];
1897 
1898 		if (num_interfaces > c->max_interfaces)
1899 			continue;
1900 		if (params->num_different_channels > c->num_different_channels)
1901 			continue;
1902 
1903 		limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1904 				 GFP_KERNEL);
1905 		if (!limits)
1906 			return -ENOMEM;
1907 
1908 		for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1909 			if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
1910 				continue;
1911 			for (j = 0; j < c->n_limits; j++) {
1912 				all_iftypes |= limits[j].types;
1913 				if (!(limits[j].types & BIT(iftype)))
1914 					continue;
1915 				if (limits[j].max < params->iftype_num[iftype])
1916 					goto cont;
1917 				limits[j].max -= params->iftype_num[iftype];
1918 			}
1919 		}
1920 
1921 		if (params->radar_detect !=
1922 			(c->radar_detect_widths & params->radar_detect))
1923 			goto cont;
1924 
1925 		if (params->radar_detect && c->radar_detect_regions &&
1926 		    !(c->radar_detect_regions & BIT(region)))
1927 			goto cont;
1928 
1929 		/* Finally check that all iftypes that we're currently
1930 		 * using are actually part of this combination. If they
1931 		 * aren't then we can't use this combination and have
1932 		 * to continue to the next.
1933 		 */
1934 		if ((all_iftypes & used_iftypes) != used_iftypes)
1935 			goto cont;
1936 
1937 		if (beacon_int_gcd) {
1938 			if (c->beacon_int_min_gcd &&
1939 			    beacon_int_gcd < c->beacon_int_min_gcd)
1940 				goto cont;
1941 			if (!c->beacon_int_min_gcd && beacon_int_different)
1942 				goto cont;
1943 		}
1944 
1945 		/* This combination covered all interface types and
1946 		 * supported the requested numbers, so we're good.
1947 		 */
1948 
1949 		(*iter)(c, data);
1950  cont:
1951 		kfree(limits);
1952 	}
1953 
1954 	return 0;
1955 }
1956 EXPORT_SYMBOL(cfg80211_iter_combinations);
1957 
1958 static void
1959 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1960 			  void *data)
1961 {
1962 	int *num = data;
1963 	(*num)++;
1964 }
1965 
1966 int cfg80211_check_combinations(struct wiphy *wiphy,
1967 				struct iface_combination_params *params)
1968 {
1969 	int err, num = 0;
1970 
1971 	err = cfg80211_iter_combinations(wiphy, params,
1972 					 cfg80211_iter_sum_ifcombs, &num);
1973 	if (err)
1974 		return err;
1975 	if (num == 0)
1976 		return -EBUSY;
1977 
1978 	return 0;
1979 }
1980 EXPORT_SYMBOL(cfg80211_check_combinations);
1981 
1982 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1983 			   const u8 *rates, unsigned int n_rates,
1984 			   u32 *mask)
1985 {
1986 	int i, j;
1987 
1988 	if (!sband)
1989 		return -EINVAL;
1990 
1991 	if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1992 		return -EINVAL;
1993 
1994 	*mask = 0;
1995 
1996 	for (i = 0; i < n_rates; i++) {
1997 		int rate = (rates[i] & 0x7f) * 5;
1998 		bool found = false;
1999 
2000 		for (j = 0; j < sband->n_bitrates; j++) {
2001 			if (sband->bitrates[j].bitrate == rate) {
2002 				found = true;
2003 				*mask |= BIT(j);
2004 				break;
2005 			}
2006 		}
2007 		if (!found)
2008 			return -EINVAL;
2009 	}
2010 
2011 	/*
2012 	 * mask must have at least one bit set here since we
2013 	 * didn't accept a 0-length rates array nor allowed
2014 	 * entries in the array that didn't exist
2015 	 */
2016 
2017 	return 0;
2018 }
2019 
2020 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
2021 {
2022 	enum nl80211_band band;
2023 	unsigned int n_channels = 0;
2024 
2025 	for (band = 0; band < NUM_NL80211_BANDS; band++)
2026 		if (wiphy->bands[band])
2027 			n_channels += wiphy->bands[band]->n_channels;
2028 
2029 	return n_channels;
2030 }
2031 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
2032 
2033 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
2034 			 struct station_info *sinfo)
2035 {
2036 	struct cfg80211_registered_device *rdev;
2037 	struct wireless_dev *wdev;
2038 
2039 	wdev = dev->ieee80211_ptr;
2040 	if (!wdev)
2041 		return -EOPNOTSUPP;
2042 
2043 	rdev = wiphy_to_rdev(wdev->wiphy);
2044 	if (!rdev->ops->get_station)
2045 		return -EOPNOTSUPP;
2046 
2047 	memset(sinfo, 0, sizeof(*sinfo));
2048 
2049 	return rdev_get_station(rdev, dev, mac_addr, sinfo);
2050 }
2051 EXPORT_SYMBOL(cfg80211_get_station);
2052 
2053 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
2054 {
2055 	int i;
2056 
2057 	if (!f)
2058 		return;
2059 
2060 	kfree(f->serv_spec_info);
2061 	kfree(f->srf_bf);
2062 	kfree(f->srf_macs);
2063 	for (i = 0; i < f->num_rx_filters; i++)
2064 		kfree(f->rx_filters[i].filter);
2065 
2066 	for (i = 0; i < f->num_tx_filters; i++)
2067 		kfree(f->tx_filters[i].filter);
2068 
2069 	kfree(f->rx_filters);
2070 	kfree(f->tx_filters);
2071 	kfree(f);
2072 }
2073 EXPORT_SYMBOL(cfg80211_free_nan_func);
2074 
2075 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
2076 				u32 center_freq_khz, u32 bw_khz)
2077 {
2078 	u32 start_freq_khz, end_freq_khz;
2079 
2080 	start_freq_khz = center_freq_khz - (bw_khz / 2);
2081 	end_freq_khz = center_freq_khz + (bw_khz / 2);
2082 
2083 	if (start_freq_khz >= freq_range->start_freq_khz &&
2084 	    end_freq_khz <= freq_range->end_freq_khz)
2085 		return true;
2086 
2087 	return false;
2088 }
2089 
2090 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
2091 {
2092 	sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
2093 				sizeof(*(sinfo->pertid)),
2094 				gfp);
2095 	if (!sinfo->pertid)
2096 		return -ENOMEM;
2097 
2098 	return 0;
2099 }
2100 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
2101 
2102 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
2103 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
2104 const unsigned char rfc1042_header[] __aligned(2) =
2105 	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2106 EXPORT_SYMBOL(rfc1042_header);
2107 
2108 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
2109 const unsigned char bridge_tunnel_header[] __aligned(2) =
2110 	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
2111 EXPORT_SYMBOL(bridge_tunnel_header);
2112 
2113 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
2114 struct iapp_layer2_update {
2115 	u8 da[ETH_ALEN];	/* broadcast */
2116 	u8 sa[ETH_ALEN];	/* STA addr */
2117 	__be16 len;		/* 6 */
2118 	u8 dsap;		/* 0 */
2119 	u8 ssap;		/* 0 */
2120 	u8 control;
2121 	u8 xid_info[3];
2122 } __packed;
2123 
2124 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
2125 {
2126 	struct iapp_layer2_update *msg;
2127 	struct sk_buff *skb;
2128 
2129 	/* Send Level 2 Update Frame to update forwarding tables in layer 2
2130 	 * bridge devices */
2131 
2132 	skb = dev_alloc_skb(sizeof(*msg));
2133 	if (!skb)
2134 		return;
2135 	msg = skb_put(skb, sizeof(*msg));
2136 
2137 	/* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2138 	 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2139 
2140 	eth_broadcast_addr(msg->da);
2141 	ether_addr_copy(msg->sa, addr);
2142 	msg->len = htons(6);
2143 	msg->dsap = 0;
2144 	msg->ssap = 0x01;	/* NULL LSAP, CR Bit: Response */
2145 	msg->control = 0xaf;	/* XID response lsb.1111F101.
2146 				 * F=0 (no poll command; unsolicited frame) */
2147 	msg->xid_info[0] = 0x81;	/* XID format identifier */
2148 	msg->xid_info[1] = 1;	/* LLC types/classes: Type 1 LLC */
2149 	msg->xid_info[2] = 0;	/* XID sender's receive window size (RW) */
2150 
2151 	skb->dev = dev;
2152 	skb->protocol = eth_type_trans(skb, dev);
2153 	memset(skb->cb, 0, sizeof(skb->cb));
2154 	netif_rx_ni(skb);
2155 }
2156 EXPORT_SYMBOL(cfg80211_send_layer2_update);
2157 
2158 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2159 			      enum ieee80211_vht_chanwidth bw,
2160 			      int mcs, bool ext_nss_bw_capable,
2161 			      unsigned int max_vht_nss)
2162 {
2163 	u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
2164 	int ext_nss_bw;
2165 	int supp_width;
2166 	int i, mcs_encoding;
2167 
2168 	if (map == 0xffff)
2169 		return 0;
2170 
2171 	if (WARN_ON(mcs > 9 || max_vht_nss > 8))
2172 		return 0;
2173 	if (mcs <= 7)
2174 		mcs_encoding = 0;
2175 	else if (mcs == 8)
2176 		mcs_encoding = 1;
2177 	else
2178 		mcs_encoding = 2;
2179 
2180 	if (!max_vht_nss) {
2181 		/* find max_vht_nss for the given MCS */
2182 		for (i = 7; i >= 0; i--) {
2183 			int supp = (map >> (2 * i)) & 3;
2184 
2185 			if (supp == 3)
2186 				continue;
2187 
2188 			if (supp >= mcs_encoding) {
2189 				max_vht_nss = i + 1;
2190 				break;
2191 			}
2192 		}
2193 	}
2194 
2195 	if (!(cap->supp_mcs.tx_mcs_map &
2196 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2197 		return max_vht_nss;
2198 
2199 	ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2200 				   IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2201 	supp_width = le32_get_bits(cap->vht_cap_info,
2202 				   IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2203 
2204 	/* if not capable, treat ext_nss_bw as 0 */
2205 	if (!ext_nss_bw_capable)
2206 		ext_nss_bw = 0;
2207 
2208 	/* This is invalid */
2209 	if (supp_width == 3)
2210 		return 0;
2211 
2212 	/* This is an invalid combination so pretend nothing is supported */
2213 	if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2214 		return 0;
2215 
2216 	/*
2217 	 * Cover all the special cases according to IEEE 802.11-2016
2218 	 * Table 9-250. All other cases are either factor of 1 or not
2219 	 * valid/supported.
2220 	 */
2221 	switch (bw) {
2222 	case IEEE80211_VHT_CHANWIDTH_USE_HT:
2223 	case IEEE80211_VHT_CHANWIDTH_80MHZ:
2224 		if ((supp_width == 1 || supp_width == 2) &&
2225 		    ext_nss_bw == 3)
2226 			return 2 * max_vht_nss;
2227 		break;
2228 	case IEEE80211_VHT_CHANWIDTH_160MHZ:
2229 		if (supp_width == 0 &&
2230 		    (ext_nss_bw == 1 || ext_nss_bw == 2))
2231 			return max_vht_nss / 2;
2232 		if (supp_width == 0 &&
2233 		    ext_nss_bw == 3)
2234 			return (3 * max_vht_nss) / 4;
2235 		if (supp_width == 1 &&
2236 		    ext_nss_bw == 3)
2237 			return 2 * max_vht_nss;
2238 		break;
2239 	case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2240 		if (supp_width == 0 && ext_nss_bw == 1)
2241 			return 0; /* not possible */
2242 		if (supp_width == 0 &&
2243 		    ext_nss_bw == 2)
2244 			return max_vht_nss / 2;
2245 		if (supp_width == 0 &&
2246 		    ext_nss_bw == 3)
2247 			return (3 * max_vht_nss) / 4;
2248 		if (supp_width == 1 &&
2249 		    ext_nss_bw == 0)
2250 			return 0; /* not possible */
2251 		if (supp_width == 1 &&
2252 		    ext_nss_bw == 1)
2253 			return max_vht_nss / 2;
2254 		if (supp_width == 1 &&
2255 		    ext_nss_bw == 2)
2256 			return (3 * max_vht_nss) / 4;
2257 		break;
2258 	}
2259 
2260 	/* not covered or invalid combination received */
2261 	return max_vht_nss;
2262 }
2263 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2264 
2265 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2266 			     bool is_4addr, u8 check_swif)
2267 
2268 {
2269 	bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2270 
2271 	switch (check_swif) {
2272 	case 0:
2273 		if (is_vlan && is_4addr)
2274 			return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2275 		return wiphy->interface_modes & BIT(iftype);
2276 	case 1:
2277 		if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2278 			return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2279 		return wiphy->software_iftypes & BIT(iftype);
2280 	default:
2281 		break;
2282 	}
2283 
2284 	return false;
2285 }
2286 EXPORT_SYMBOL(cfg80211_iftype_allowed);
2287