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