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