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