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