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