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