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