xref: /openbmc/linux/net/wireless/util.c (revision 0ccc6395)
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_969[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 		result = rates_160M[rate->he_gi];
1485 	else if (rate->bw == RATE_INFO_BW_80 ||
1486 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1487 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1488 		result = rates_969[rate->he_gi];
1489 	else if (rate->bw == RATE_INFO_BW_40 ||
1490 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1491 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1492 		result = rates_484[rate->he_gi];
1493 	else if (rate->bw == RATE_INFO_BW_20 ||
1494 		 (rate->bw == RATE_INFO_BW_HE_RU &&
1495 		  rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1496 		result = rates_242[rate->he_gi];
1497 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1498 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1499 		result = rates_106[rate->he_gi];
1500 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1501 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1502 		result = rates_52[rate->he_gi];
1503 	else if (rate->bw == RATE_INFO_BW_HE_RU &&
1504 		 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1505 		result = rates_26[rate->he_gi];
1506 	else {
1507 		WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1508 		     rate->bw, rate->he_ru_alloc);
1509 		return 0;
1510 	}
1511 
1512 	/* now scale to the appropriate MCS */
1513 	tmp = result;
1514 	tmp *= SCALE;
1515 	do_div(tmp, mcs_divisors[rate->mcs]);
1516 	result = tmp;
1517 
1518 	/* and take NSS, DCM into account */
1519 	result = (result * rate->nss) / 8;
1520 	if (rate->he_dcm)
1521 		result /= 2;
1522 
1523 	return result / 10000;
1524 }
1525 
cfg80211_calculate_bitrate_eht(struct rate_info * rate)1526 static u32 cfg80211_calculate_bitrate_eht(struct rate_info *rate)
1527 {
1528 #define SCALE 6144
1529 	static const u32 mcs_divisors[16] = {
1530 		102399, /* 16.666666... */
1531 		 51201, /*  8.333333... */
1532 		 34134, /*  5.555555... */
1533 		 25599, /*  4.166666... */
1534 		 17067, /*  2.777777... */
1535 		 12801, /*  2.083333... */
1536 		 11377, /*  1.851725... */
1537 		 10239, /*  1.666666... */
1538 		  8532, /*  1.388888... */
1539 		  7680, /*  1.250000... */
1540 		  6828, /*  1.111111... */
1541 		  6144, /*  1.000000... */
1542 		  5690, /*  0.926106... */
1543 		  5120, /*  0.833333... */
1544 		409600, /* 66.666666... */
1545 		204800, /* 33.333333... */
1546 	};
1547 	static const u32 rates_996[3] =  { 480388888, 453700000, 408333333 };
1548 	static const u32 rates_484[3] =  { 229411111, 216666666, 195000000 };
1549 	static const u32 rates_242[3] =  { 114711111, 108333333,  97500000 };
1550 	static const u32 rates_106[3] =  {  40000000,  37777777,  34000000 };
1551 	static const u32 rates_52[3]  =  {  18820000,  17777777,  16000000 };
1552 	static const u32 rates_26[3]  =  {   9411111,   8888888,   8000000 };
1553 	u64 tmp;
1554 	u32 result;
1555 
1556 	if (WARN_ON_ONCE(rate->mcs > 15))
1557 		return 0;
1558 	if (WARN_ON_ONCE(rate->eht_gi > NL80211_RATE_INFO_EHT_GI_3_2))
1559 		return 0;
1560 	if (WARN_ON_ONCE(rate->eht_ru_alloc >
1561 			 NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1562 		return 0;
1563 	if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1564 		return 0;
1565 
1566 	/* Bandwidth checks for MCS 14 */
1567 	if (rate->mcs == 14) {
1568 		if ((rate->bw != RATE_INFO_BW_EHT_RU &&
1569 		     rate->bw != RATE_INFO_BW_80 &&
1570 		     rate->bw != RATE_INFO_BW_160 &&
1571 		     rate->bw != RATE_INFO_BW_320) ||
1572 		    (rate->bw == RATE_INFO_BW_EHT_RU &&
1573 		     rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_996 &&
1574 		     rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_2x996 &&
1575 		     rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) {
1576 			WARN(1, "invalid EHT BW for MCS 14: bw:%d, ru:%d\n",
1577 			     rate->bw, rate->eht_ru_alloc);
1578 			return 0;
1579 		}
1580 	}
1581 
1582 	if (rate->bw == RATE_INFO_BW_320 ||
1583 	    (rate->bw == RATE_INFO_BW_EHT_RU &&
1584 	     rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
1585 		result = 4 * rates_996[rate->eht_gi];
1586 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1587 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484)
1588 		result = 3 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1589 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1590 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996)
1591 		result = 3 * rates_996[rate->eht_gi];
1592 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1593 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484)
1594 		result = 2 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1595 	else if (rate->bw == RATE_INFO_BW_160 ||
1596 		 (rate->bw == RATE_INFO_BW_EHT_RU &&
1597 		  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996))
1598 		result = 2 * rates_996[rate->eht_gi];
1599 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1600 		 rate->eht_ru_alloc ==
1601 		 NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242)
1602 		result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi]
1603 			 + rates_242[rate->eht_gi];
1604 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1605 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996P484)
1606 		result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
1607 	else if (rate->bw == RATE_INFO_BW_80 ||
1608 		 (rate->bw == RATE_INFO_BW_EHT_RU &&
1609 		  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996))
1610 		result = rates_996[rate->eht_gi];
1611 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1612 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484P242)
1613 		result = rates_484[rate->eht_gi] + rates_242[rate->eht_gi];
1614 	else if (rate->bw == RATE_INFO_BW_40 ||
1615 		 (rate->bw == RATE_INFO_BW_EHT_RU &&
1616 		  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484))
1617 		result = rates_484[rate->eht_gi];
1618 	else if (rate->bw == RATE_INFO_BW_20 ||
1619 		 (rate->bw == RATE_INFO_BW_EHT_RU &&
1620 		  rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_242))
1621 		result = rates_242[rate->eht_gi];
1622 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1623 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106P26)
1624 		result = rates_106[rate->eht_gi] + rates_26[rate->eht_gi];
1625 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1626 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106)
1627 		result = rates_106[rate->eht_gi];
1628 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1629 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52P26)
1630 		result = rates_52[rate->eht_gi] + rates_26[rate->eht_gi];
1631 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1632 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52)
1633 		result = rates_52[rate->eht_gi];
1634 	else if (rate->bw == RATE_INFO_BW_EHT_RU &&
1635 		 rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_26)
1636 		result = rates_26[rate->eht_gi];
1637 	else {
1638 		WARN(1, "invalid EHT MCS: bw:%d, ru:%d\n",
1639 		     rate->bw, rate->eht_ru_alloc);
1640 		return 0;
1641 	}
1642 
1643 	/* now scale to the appropriate MCS */
1644 	tmp = result;
1645 	tmp *= SCALE;
1646 	do_div(tmp, mcs_divisors[rate->mcs]);
1647 
1648 	/* and take NSS */
1649 	tmp *= rate->nss;
1650 	do_div(tmp, 8);
1651 
1652 	result = tmp;
1653 
1654 	return result / 10000;
1655 }
1656 
cfg80211_calculate_bitrate_s1g(struct rate_info * rate)1657 static u32 cfg80211_calculate_bitrate_s1g(struct rate_info *rate)
1658 {
1659 	/* For 1, 2, 4, 8 and 16 MHz channels */
1660 	static const u32 base[5][11] = {
1661 		{  300000,
1662 		   600000,
1663 		   900000,
1664 		  1200000,
1665 		  1800000,
1666 		  2400000,
1667 		  2700000,
1668 		  3000000,
1669 		  3600000,
1670 		  4000000,
1671 		  /* MCS 10 supported in 1 MHz only */
1672 		  150000,
1673 		},
1674 		{  650000,
1675 		  1300000,
1676 		  1950000,
1677 		  2600000,
1678 		  3900000,
1679 		  5200000,
1680 		  5850000,
1681 		  6500000,
1682 		  7800000,
1683 		  /* MCS 9 not valid */
1684 		},
1685 		{  1350000,
1686 		   2700000,
1687 		   4050000,
1688 		   5400000,
1689 		   8100000,
1690 		  10800000,
1691 		  12150000,
1692 		  13500000,
1693 		  16200000,
1694 		  18000000,
1695 		},
1696 		{  2925000,
1697 		   5850000,
1698 		   8775000,
1699 		  11700000,
1700 		  17550000,
1701 		  23400000,
1702 		  26325000,
1703 		  29250000,
1704 		  35100000,
1705 		  39000000,
1706 		},
1707 		{  8580000,
1708 		  11700000,
1709 		  17550000,
1710 		  23400000,
1711 		  35100000,
1712 		  46800000,
1713 		  52650000,
1714 		  58500000,
1715 		  70200000,
1716 		  78000000,
1717 		},
1718 	};
1719 	u32 bitrate;
1720 	/* default is 1 MHz index */
1721 	int idx = 0;
1722 
1723 	if (rate->mcs >= 11)
1724 		goto warn;
1725 
1726 	switch (rate->bw) {
1727 	case RATE_INFO_BW_16:
1728 		idx = 4;
1729 		break;
1730 	case RATE_INFO_BW_8:
1731 		idx = 3;
1732 		break;
1733 	case RATE_INFO_BW_4:
1734 		idx = 2;
1735 		break;
1736 	case RATE_INFO_BW_2:
1737 		idx = 1;
1738 		break;
1739 	case RATE_INFO_BW_1:
1740 		idx = 0;
1741 		break;
1742 	case RATE_INFO_BW_5:
1743 	case RATE_INFO_BW_10:
1744 	case RATE_INFO_BW_20:
1745 	case RATE_INFO_BW_40:
1746 	case RATE_INFO_BW_80:
1747 	case RATE_INFO_BW_160:
1748 	default:
1749 		goto warn;
1750 	}
1751 
1752 	bitrate = base[idx][rate->mcs];
1753 	bitrate *= rate->nss;
1754 
1755 	if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1756 		bitrate = (bitrate / 9) * 10;
1757 	/* do NOT round down here */
1758 	return (bitrate + 50000) / 100000;
1759 warn:
1760 	WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1761 		  rate->bw, rate->mcs, rate->nss);
1762 	return 0;
1763 }
1764 
cfg80211_calculate_bitrate(struct rate_info * rate)1765 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1766 {
1767 	if (rate->flags & RATE_INFO_FLAGS_MCS)
1768 		return cfg80211_calculate_bitrate_ht(rate);
1769 	if (rate->flags & RATE_INFO_FLAGS_DMG)
1770 		return cfg80211_calculate_bitrate_dmg(rate);
1771 	if (rate->flags & RATE_INFO_FLAGS_EXTENDED_SC_DMG)
1772 		return cfg80211_calculate_bitrate_extended_sc_dmg(rate);
1773 	if (rate->flags & RATE_INFO_FLAGS_EDMG)
1774 		return cfg80211_calculate_bitrate_edmg(rate);
1775 	if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1776 		return cfg80211_calculate_bitrate_vht(rate);
1777 	if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1778 		return cfg80211_calculate_bitrate_he(rate);
1779 	if (rate->flags & RATE_INFO_FLAGS_EHT_MCS)
1780 		return cfg80211_calculate_bitrate_eht(rate);
1781 	if (rate->flags & RATE_INFO_FLAGS_S1G_MCS)
1782 		return cfg80211_calculate_bitrate_s1g(rate);
1783 
1784 	return rate->legacy;
1785 }
1786 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1787 
cfg80211_get_p2p_attr(const u8 * ies,unsigned int len,enum ieee80211_p2p_attr_id attr,u8 * buf,unsigned int bufsize)1788 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1789 			  enum ieee80211_p2p_attr_id attr,
1790 			  u8 *buf, unsigned int bufsize)
1791 {
1792 	u8 *out = buf;
1793 	u16 attr_remaining = 0;
1794 	bool desired_attr = false;
1795 	u16 desired_len = 0;
1796 
1797 	while (len > 0) {
1798 		unsigned int iedatalen;
1799 		unsigned int copy;
1800 		const u8 *iedata;
1801 
1802 		if (len < 2)
1803 			return -EILSEQ;
1804 		iedatalen = ies[1];
1805 		if (iedatalen + 2 > len)
1806 			return -EILSEQ;
1807 
1808 		if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1809 			goto cont;
1810 
1811 		if (iedatalen < 4)
1812 			goto cont;
1813 
1814 		iedata = ies + 2;
1815 
1816 		/* check WFA OUI, P2P subtype */
1817 		if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1818 		    iedata[2] != 0x9a || iedata[3] != 0x09)
1819 			goto cont;
1820 
1821 		iedatalen -= 4;
1822 		iedata += 4;
1823 
1824 		/* check attribute continuation into this IE */
1825 		copy = min_t(unsigned int, attr_remaining, iedatalen);
1826 		if (copy && desired_attr) {
1827 			desired_len += copy;
1828 			if (out) {
1829 				memcpy(out, iedata, min(bufsize, copy));
1830 				out += min(bufsize, copy);
1831 				bufsize -= min(bufsize, copy);
1832 			}
1833 
1834 
1835 			if (copy == attr_remaining)
1836 				return desired_len;
1837 		}
1838 
1839 		attr_remaining -= copy;
1840 		if (attr_remaining)
1841 			goto cont;
1842 
1843 		iedatalen -= copy;
1844 		iedata += copy;
1845 
1846 		while (iedatalen > 0) {
1847 			u16 attr_len;
1848 
1849 			/* P2P attribute ID & size must fit */
1850 			if (iedatalen < 3)
1851 				return -EILSEQ;
1852 			desired_attr = iedata[0] == attr;
1853 			attr_len = get_unaligned_le16(iedata + 1);
1854 			iedatalen -= 3;
1855 			iedata += 3;
1856 
1857 			copy = min_t(unsigned int, attr_len, iedatalen);
1858 
1859 			if (desired_attr) {
1860 				desired_len += copy;
1861 				if (out) {
1862 					memcpy(out, iedata, min(bufsize, copy));
1863 					out += min(bufsize, copy);
1864 					bufsize -= min(bufsize, copy);
1865 				}
1866 
1867 				if (copy == attr_len)
1868 					return desired_len;
1869 			}
1870 
1871 			iedata += copy;
1872 			iedatalen -= copy;
1873 			attr_remaining = attr_len - copy;
1874 		}
1875 
1876  cont:
1877 		len -= ies[1] + 2;
1878 		ies += ies[1] + 2;
1879 	}
1880 
1881 	if (attr_remaining && desired_attr)
1882 		return -EILSEQ;
1883 
1884 	return -ENOENT;
1885 }
1886 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1887 
ieee80211_id_in_list(const u8 * ids,int n_ids,u8 id,bool id_ext)1888 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
1889 {
1890 	int i;
1891 
1892 	/* Make sure array values are legal */
1893 	if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1894 		return false;
1895 
1896 	i = 0;
1897 	while (i < n_ids) {
1898 		if (ids[i] == WLAN_EID_EXTENSION) {
1899 			if (id_ext && (ids[i + 1] == id))
1900 				return true;
1901 
1902 			i += 2;
1903 			continue;
1904 		}
1905 
1906 		if (ids[i] == id && !id_ext)
1907 			return true;
1908 
1909 		i++;
1910 	}
1911 	return false;
1912 }
1913 
skip_ie(const u8 * ies,size_t ielen,size_t pos)1914 static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1915 {
1916 	/* we assume a validly formed IEs buffer */
1917 	u8 len = ies[pos + 1];
1918 
1919 	pos += 2 + len;
1920 
1921 	/* the IE itself must have 255 bytes for fragments to follow */
1922 	if (len < 255)
1923 		return pos;
1924 
1925 	while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1926 		len = ies[pos + 1];
1927 		pos += 2 + len;
1928 	}
1929 
1930 	return pos;
1931 }
1932 
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)1933 size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1934 			      const u8 *ids, int n_ids,
1935 			      const u8 *after_ric, int n_after_ric,
1936 			      size_t offset)
1937 {
1938 	size_t pos = offset;
1939 
1940 	while (pos < ielen) {
1941 		u8 ext = 0;
1942 
1943 		if (ies[pos] == WLAN_EID_EXTENSION)
1944 			ext = 2;
1945 		if ((pos + ext) >= ielen)
1946 			break;
1947 
1948 		if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1949 					  ies[pos] == WLAN_EID_EXTENSION))
1950 			break;
1951 
1952 		if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1953 			pos = skip_ie(ies, ielen, pos);
1954 
1955 			while (pos < ielen) {
1956 				if (ies[pos] == WLAN_EID_EXTENSION)
1957 					ext = 2;
1958 				else
1959 					ext = 0;
1960 
1961 				if ((pos + ext) >= ielen)
1962 					break;
1963 
1964 				if (!ieee80211_id_in_list(after_ric,
1965 							  n_after_ric,
1966 							  ies[pos + ext],
1967 							  ext == 2))
1968 					pos = skip_ie(ies, ielen, pos);
1969 				else
1970 					break;
1971 			}
1972 		} else {
1973 			pos = skip_ie(ies, ielen, pos);
1974 		}
1975 	}
1976 
1977 	return pos;
1978 }
1979 EXPORT_SYMBOL(ieee80211_ie_split_ric);
1980 
ieee80211_operating_class_to_band(u8 operating_class,enum nl80211_band * band)1981 bool ieee80211_operating_class_to_band(u8 operating_class,
1982 				       enum nl80211_band *band)
1983 {
1984 	switch (operating_class) {
1985 	case 112:
1986 	case 115 ... 127:
1987 	case 128 ... 130:
1988 		*band = NL80211_BAND_5GHZ;
1989 		return true;
1990 	case 131 ... 135:
1991 		*band = NL80211_BAND_6GHZ;
1992 		return true;
1993 	case 81:
1994 	case 82:
1995 	case 83:
1996 	case 84:
1997 		*band = NL80211_BAND_2GHZ;
1998 		return true;
1999 	case 180:
2000 		*band = NL80211_BAND_60GHZ;
2001 		return true;
2002 	}
2003 
2004 	return false;
2005 }
2006 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
2007 
ieee80211_chandef_to_operating_class(struct cfg80211_chan_def * chandef,u8 * op_class)2008 bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
2009 					  u8 *op_class)
2010 {
2011 	u8 vht_opclass;
2012 	u32 freq = chandef->center_freq1;
2013 
2014 	if (freq >= 2412 && freq <= 2472) {
2015 		if (chandef->width > NL80211_CHAN_WIDTH_40)
2016 			return false;
2017 
2018 		/* 2.407 GHz, channels 1..13 */
2019 		if (chandef->width == NL80211_CHAN_WIDTH_40) {
2020 			if (freq > chandef->chan->center_freq)
2021 				*op_class = 83; /* HT40+ */
2022 			else
2023 				*op_class = 84; /* HT40- */
2024 		} else {
2025 			*op_class = 81;
2026 		}
2027 
2028 		return true;
2029 	}
2030 
2031 	if (freq == 2484) {
2032 		/* channel 14 is only for IEEE 802.11b */
2033 		if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
2034 			return false;
2035 
2036 		*op_class = 82; /* channel 14 */
2037 		return true;
2038 	}
2039 
2040 	switch (chandef->width) {
2041 	case NL80211_CHAN_WIDTH_80:
2042 		vht_opclass = 128;
2043 		break;
2044 	case NL80211_CHAN_WIDTH_160:
2045 		vht_opclass = 129;
2046 		break;
2047 	case NL80211_CHAN_WIDTH_80P80:
2048 		vht_opclass = 130;
2049 		break;
2050 	case NL80211_CHAN_WIDTH_10:
2051 	case NL80211_CHAN_WIDTH_5:
2052 		return false; /* unsupported for now */
2053 	default:
2054 		vht_opclass = 0;
2055 		break;
2056 	}
2057 
2058 	/* 5 GHz, channels 36..48 */
2059 	if (freq >= 5180 && freq <= 5240) {
2060 		if (vht_opclass) {
2061 			*op_class = vht_opclass;
2062 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2063 			if (freq > chandef->chan->center_freq)
2064 				*op_class = 116;
2065 			else
2066 				*op_class = 117;
2067 		} else {
2068 			*op_class = 115;
2069 		}
2070 
2071 		return true;
2072 	}
2073 
2074 	/* 5 GHz, channels 52..64 */
2075 	if (freq >= 5260 && freq <= 5320) {
2076 		if (vht_opclass) {
2077 			*op_class = vht_opclass;
2078 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2079 			if (freq > chandef->chan->center_freq)
2080 				*op_class = 119;
2081 			else
2082 				*op_class = 120;
2083 		} else {
2084 			*op_class = 118;
2085 		}
2086 
2087 		return true;
2088 	}
2089 
2090 	/* 5 GHz, channels 100..144 */
2091 	if (freq >= 5500 && freq <= 5720) {
2092 		if (vht_opclass) {
2093 			*op_class = vht_opclass;
2094 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2095 			if (freq > chandef->chan->center_freq)
2096 				*op_class = 122;
2097 			else
2098 				*op_class = 123;
2099 		} else {
2100 			*op_class = 121;
2101 		}
2102 
2103 		return true;
2104 	}
2105 
2106 	/* 5 GHz, channels 149..169 */
2107 	if (freq >= 5745 && freq <= 5845) {
2108 		if (vht_opclass) {
2109 			*op_class = vht_opclass;
2110 		} else if (chandef->width == NL80211_CHAN_WIDTH_40) {
2111 			if (freq > chandef->chan->center_freq)
2112 				*op_class = 126;
2113 			else
2114 				*op_class = 127;
2115 		} else if (freq <= 5805) {
2116 			*op_class = 124;
2117 		} else {
2118 			*op_class = 125;
2119 		}
2120 
2121 		return true;
2122 	}
2123 
2124 	/* 56.16 GHz, channel 1..4 */
2125 	if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
2126 		if (chandef->width >= NL80211_CHAN_WIDTH_40)
2127 			return false;
2128 
2129 		*op_class = 180;
2130 		return true;
2131 	}
2132 
2133 	/* not supported yet */
2134 	return false;
2135 }
2136 EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
2137 
cfg80211_wdev_bi(struct wireless_dev * wdev)2138 static int cfg80211_wdev_bi(struct wireless_dev *wdev)
2139 {
2140 	switch (wdev->iftype) {
2141 	case NL80211_IFTYPE_AP:
2142 	case NL80211_IFTYPE_P2P_GO:
2143 		WARN_ON(wdev->valid_links);
2144 		return wdev->links[0].ap.beacon_interval;
2145 	case NL80211_IFTYPE_MESH_POINT:
2146 		return wdev->u.mesh.beacon_interval;
2147 	case NL80211_IFTYPE_ADHOC:
2148 		return wdev->u.ibss.beacon_interval;
2149 	default:
2150 		break;
2151 	}
2152 
2153 	return 0;
2154 }
2155 
cfg80211_calculate_bi_data(struct wiphy * wiphy,u32 new_beacon_int,u32 * beacon_int_gcd,bool * beacon_int_different)2156 static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
2157 				       u32 *beacon_int_gcd,
2158 				       bool *beacon_int_different)
2159 {
2160 	struct wireless_dev *wdev;
2161 
2162 	*beacon_int_gcd = 0;
2163 	*beacon_int_different = false;
2164 
2165 	list_for_each_entry(wdev, &wiphy->wdev_list, list) {
2166 		int wdev_bi;
2167 
2168 		/* this feature isn't supported with MLO */
2169 		if (wdev->valid_links)
2170 			continue;
2171 
2172 		wdev_bi = cfg80211_wdev_bi(wdev);
2173 
2174 		if (!wdev_bi)
2175 			continue;
2176 
2177 		if (!*beacon_int_gcd) {
2178 			*beacon_int_gcd = wdev_bi;
2179 			continue;
2180 		}
2181 
2182 		if (wdev_bi == *beacon_int_gcd)
2183 			continue;
2184 
2185 		*beacon_int_different = true;
2186 		*beacon_int_gcd = gcd(*beacon_int_gcd, wdev_bi);
2187 	}
2188 
2189 	if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
2190 		if (*beacon_int_gcd)
2191 			*beacon_int_different = true;
2192 		*beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
2193 	}
2194 }
2195 
cfg80211_validate_beacon_int(struct cfg80211_registered_device * rdev,enum nl80211_iftype iftype,u32 beacon_int)2196 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
2197 				 enum nl80211_iftype iftype, u32 beacon_int)
2198 {
2199 	/*
2200 	 * This is just a basic pre-condition check; if interface combinations
2201 	 * are possible the driver must already be checking those with a call
2202 	 * to cfg80211_check_combinations(), in which case we'll validate more
2203 	 * through the cfg80211_calculate_bi_data() call and code in
2204 	 * cfg80211_iter_combinations().
2205 	 */
2206 
2207 	if (beacon_int < 10 || beacon_int > 10000)
2208 		return -EINVAL;
2209 
2210 	return 0;
2211 }
2212 
cfg80211_iter_combinations(struct wiphy * wiphy,struct iface_combination_params * params,void (* iter)(const struct ieee80211_iface_combination * c,void * data),void * data)2213 int cfg80211_iter_combinations(struct wiphy *wiphy,
2214 			       struct iface_combination_params *params,
2215 			       void (*iter)(const struct ieee80211_iface_combination *c,
2216 					    void *data),
2217 			       void *data)
2218 {
2219 	const struct ieee80211_regdomain *regdom;
2220 	enum nl80211_dfs_regions region = 0;
2221 	int i, j, iftype;
2222 	int num_interfaces = 0;
2223 	u32 used_iftypes = 0;
2224 	u32 beacon_int_gcd;
2225 	bool beacon_int_different;
2226 
2227 	/*
2228 	 * This is a bit strange, since the iteration used to rely only on
2229 	 * the data given by the driver, but here it now relies on context,
2230 	 * in form of the currently operating interfaces.
2231 	 * This is OK for all current users, and saves us from having to
2232 	 * push the GCD calculations into all the drivers.
2233 	 * In the future, this should probably rely more on data that's in
2234 	 * cfg80211 already - the only thing not would appear to be any new
2235 	 * interfaces (while being brought up) and channel/radar data.
2236 	 */
2237 	cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
2238 				   &beacon_int_gcd, &beacon_int_different);
2239 
2240 	if (params->radar_detect) {
2241 		rcu_read_lock();
2242 		regdom = rcu_dereference(cfg80211_regdomain);
2243 		if (regdom)
2244 			region = regdom->dfs_region;
2245 		rcu_read_unlock();
2246 	}
2247 
2248 	for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2249 		num_interfaces += params->iftype_num[iftype];
2250 		if (params->iftype_num[iftype] > 0 &&
2251 		    !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2252 			used_iftypes |= BIT(iftype);
2253 	}
2254 
2255 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
2256 		const struct ieee80211_iface_combination *c;
2257 		struct ieee80211_iface_limit *limits;
2258 		u32 all_iftypes = 0;
2259 
2260 		c = &wiphy->iface_combinations[i];
2261 
2262 		if (num_interfaces > c->max_interfaces)
2263 			continue;
2264 		if (params->num_different_channels > c->num_different_channels)
2265 			continue;
2266 
2267 		limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
2268 				 GFP_KERNEL);
2269 		if (!limits)
2270 			return -ENOMEM;
2271 
2272 		for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
2273 			if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
2274 				continue;
2275 			for (j = 0; j < c->n_limits; j++) {
2276 				all_iftypes |= limits[j].types;
2277 				if (!(limits[j].types & BIT(iftype)))
2278 					continue;
2279 				if (limits[j].max < params->iftype_num[iftype])
2280 					goto cont;
2281 				limits[j].max -= params->iftype_num[iftype];
2282 			}
2283 		}
2284 
2285 		if (params->radar_detect !=
2286 			(c->radar_detect_widths & params->radar_detect))
2287 			goto cont;
2288 
2289 		if (params->radar_detect && c->radar_detect_regions &&
2290 		    !(c->radar_detect_regions & BIT(region)))
2291 			goto cont;
2292 
2293 		/* Finally check that all iftypes that we're currently
2294 		 * using are actually part of this combination. If they
2295 		 * aren't then we can't use this combination and have
2296 		 * to continue to the next.
2297 		 */
2298 		if ((all_iftypes & used_iftypes) != used_iftypes)
2299 			goto cont;
2300 
2301 		if (beacon_int_gcd) {
2302 			if (c->beacon_int_min_gcd &&
2303 			    beacon_int_gcd < c->beacon_int_min_gcd)
2304 				goto cont;
2305 			if (!c->beacon_int_min_gcd && beacon_int_different)
2306 				goto cont;
2307 		}
2308 
2309 		/* This combination covered all interface types and
2310 		 * supported the requested numbers, so we're good.
2311 		 */
2312 
2313 		(*iter)(c, data);
2314  cont:
2315 		kfree(limits);
2316 	}
2317 
2318 	return 0;
2319 }
2320 EXPORT_SYMBOL(cfg80211_iter_combinations);
2321 
2322 static void
cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination * c,void * data)2323 cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
2324 			  void *data)
2325 {
2326 	int *num = data;
2327 	(*num)++;
2328 }
2329 
cfg80211_check_combinations(struct wiphy * wiphy,struct iface_combination_params * params)2330 int cfg80211_check_combinations(struct wiphy *wiphy,
2331 				struct iface_combination_params *params)
2332 {
2333 	int err, num = 0;
2334 
2335 	err = cfg80211_iter_combinations(wiphy, params,
2336 					 cfg80211_iter_sum_ifcombs, &num);
2337 	if (err)
2338 		return err;
2339 	if (num == 0)
2340 		return -EBUSY;
2341 
2342 	return 0;
2343 }
2344 EXPORT_SYMBOL(cfg80211_check_combinations);
2345 
ieee80211_get_ratemask(struct ieee80211_supported_band * sband,const u8 * rates,unsigned int n_rates,u32 * mask)2346 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
2347 			   const u8 *rates, unsigned int n_rates,
2348 			   u32 *mask)
2349 {
2350 	int i, j;
2351 
2352 	if (!sband)
2353 		return -EINVAL;
2354 
2355 	if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
2356 		return -EINVAL;
2357 
2358 	*mask = 0;
2359 
2360 	for (i = 0; i < n_rates; i++) {
2361 		int rate = (rates[i] & 0x7f) * 5;
2362 		bool found = false;
2363 
2364 		for (j = 0; j < sband->n_bitrates; j++) {
2365 			if (sband->bitrates[j].bitrate == rate) {
2366 				found = true;
2367 				*mask |= BIT(j);
2368 				break;
2369 			}
2370 		}
2371 		if (!found)
2372 			return -EINVAL;
2373 	}
2374 
2375 	/*
2376 	 * mask must have at least one bit set here since we
2377 	 * didn't accept a 0-length rates array nor allowed
2378 	 * entries in the array that didn't exist
2379 	 */
2380 
2381 	return 0;
2382 }
2383 
ieee80211_get_num_supported_channels(struct wiphy * wiphy)2384 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
2385 {
2386 	enum nl80211_band band;
2387 	unsigned int n_channels = 0;
2388 
2389 	for (band = 0; band < NUM_NL80211_BANDS; band++)
2390 		if (wiphy->bands[band])
2391 			n_channels += wiphy->bands[band]->n_channels;
2392 
2393 	return n_channels;
2394 }
2395 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
2396 
cfg80211_get_station(struct net_device * dev,const u8 * mac_addr,struct station_info * sinfo)2397 int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
2398 			 struct station_info *sinfo)
2399 {
2400 	struct cfg80211_registered_device *rdev;
2401 	struct wireless_dev *wdev;
2402 	int ret;
2403 
2404 	wdev = dev->ieee80211_ptr;
2405 	if (!wdev)
2406 		return -EOPNOTSUPP;
2407 
2408 	rdev = wiphy_to_rdev(wdev->wiphy);
2409 	if (!rdev->ops->get_station)
2410 		return -EOPNOTSUPP;
2411 
2412 	memset(sinfo, 0, sizeof(*sinfo));
2413 
2414 	wiphy_lock(&rdev->wiphy);
2415 	ret = rdev_get_station(rdev, dev, mac_addr, sinfo);
2416 	wiphy_unlock(&rdev->wiphy);
2417 
2418 	return ret;
2419 }
2420 EXPORT_SYMBOL(cfg80211_get_station);
2421 
cfg80211_free_nan_func(struct cfg80211_nan_func * f)2422 void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
2423 {
2424 	int i;
2425 
2426 	if (!f)
2427 		return;
2428 
2429 	kfree(f->serv_spec_info);
2430 	kfree(f->srf_bf);
2431 	kfree(f->srf_macs);
2432 	for (i = 0; i < f->num_rx_filters; i++)
2433 		kfree(f->rx_filters[i].filter);
2434 
2435 	for (i = 0; i < f->num_tx_filters; i++)
2436 		kfree(f->tx_filters[i].filter);
2437 
2438 	kfree(f->rx_filters);
2439 	kfree(f->tx_filters);
2440 	kfree(f);
2441 }
2442 EXPORT_SYMBOL(cfg80211_free_nan_func);
2443 
cfg80211_does_bw_fit_range(const struct ieee80211_freq_range * freq_range,u32 center_freq_khz,u32 bw_khz)2444 bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
2445 				u32 center_freq_khz, u32 bw_khz)
2446 {
2447 	u32 start_freq_khz, end_freq_khz;
2448 
2449 	start_freq_khz = center_freq_khz - (bw_khz / 2);
2450 	end_freq_khz = center_freq_khz + (bw_khz / 2);
2451 
2452 	if (start_freq_khz >= freq_range->start_freq_khz &&
2453 	    end_freq_khz <= freq_range->end_freq_khz)
2454 		return true;
2455 
2456 	return false;
2457 }
2458 
cfg80211_sinfo_alloc_tid_stats(struct station_info * sinfo,gfp_t gfp)2459 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
2460 {
2461 	sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
2462 				sizeof(*(sinfo->pertid)),
2463 				gfp);
2464 	if (!sinfo->pertid)
2465 		return -ENOMEM;
2466 
2467 	return 0;
2468 }
2469 EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
2470 
2471 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
2472 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
2473 const unsigned char rfc1042_header[] __aligned(2) =
2474 	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2475 EXPORT_SYMBOL(rfc1042_header);
2476 
2477 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
2478 const unsigned char bridge_tunnel_header[] __aligned(2) =
2479 	{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
2480 EXPORT_SYMBOL(bridge_tunnel_header);
2481 
2482 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
2483 struct iapp_layer2_update {
2484 	u8 da[ETH_ALEN];	/* broadcast */
2485 	u8 sa[ETH_ALEN];	/* STA addr */
2486 	__be16 len;		/* 6 */
2487 	u8 dsap;		/* 0 */
2488 	u8 ssap;		/* 0 */
2489 	u8 control;
2490 	u8 xid_info[3];
2491 } __packed;
2492 
cfg80211_send_layer2_update(struct net_device * dev,const u8 * addr)2493 void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
2494 {
2495 	struct iapp_layer2_update *msg;
2496 	struct sk_buff *skb;
2497 
2498 	/* Send Level 2 Update Frame to update forwarding tables in layer 2
2499 	 * bridge devices */
2500 
2501 	skb = dev_alloc_skb(sizeof(*msg));
2502 	if (!skb)
2503 		return;
2504 	msg = skb_put(skb, sizeof(*msg));
2505 
2506 	/* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2507 	 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2508 
2509 	eth_broadcast_addr(msg->da);
2510 	ether_addr_copy(msg->sa, addr);
2511 	msg->len = htons(6);
2512 	msg->dsap = 0;
2513 	msg->ssap = 0x01;	/* NULL LSAP, CR Bit: Response */
2514 	msg->control = 0xaf;	/* XID response lsb.1111F101.
2515 				 * F=0 (no poll command; unsolicited frame) */
2516 	msg->xid_info[0] = 0x81;	/* XID format identifier */
2517 	msg->xid_info[1] = 1;	/* LLC types/classes: Type 1 LLC */
2518 	msg->xid_info[2] = 0;	/* XID sender's receive window size (RW) */
2519 
2520 	skb->dev = dev;
2521 	skb->protocol = eth_type_trans(skb, dev);
2522 	memset(skb->cb, 0, sizeof(skb->cb));
2523 	netif_rx(skb);
2524 }
2525 EXPORT_SYMBOL(cfg80211_send_layer2_update);
2526 
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)2527 int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2528 			      enum ieee80211_vht_chanwidth bw,
2529 			      int mcs, bool ext_nss_bw_capable,
2530 			      unsigned int max_vht_nss)
2531 {
2532 	u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
2533 	int ext_nss_bw;
2534 	int supp_width;
2535 	int i, mcs_encoding;
2536 
2537 	if (map == 0xffff)
2538 		return 0;
2539 
2540 	if (WARN_ON(mcs > 9 || max_vht_nss > 8))
2541 		return 0;
2542 	if (mcs <= 7)
2543 		mcs_encoding = 0;
2544 	else if (mcs == 8)
2545 		mcs_encoding = 1;
2546 	else
2547 		mcs_encoding = 2;
2548 
2549 	if (!max_vht_nss) {
2550 		/* find max_vht_nss for the given MCS */
2551 		for (i = 7; i >= 0; i--) {
2552 			int supp = (map >> (2 * i)) & 3;
2553 
2554 			if (supp == 3)
2555 				continue;
2556 
2557 			if (supp >= mcs_encoding) {
2558 				max_vht_nss = i + 1;
2559 				break;
2560 			}
2561 		}
2562 	}
2563 
2564 	if (!(cap->supp_mcs.tx_mcs_map &
2565 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2566 		return max_vht_nss;
2567 
2568 	ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2569 				   IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2570 	supp_width = le32_get_bits(cap->vht_cap_info,
2571 				   IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2572 
2573 	/* if not capable, treat ext_nss_bw as 0 */
2574 	if (!ext_nss_bw_capable)
2575 		ext_nss_bw = 0;
2576 
2577 	/* This is invalid */
2578 	if (supp_width == 3)
2579 		return 0;
2580 
2581 	/* This is an invalid combination so pretend nothing is supported */
2582 	if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2583 		return 0;
2584 
2585 	/*
2586 	 * Cover all the special cases according to IEEE 802.11-2016
2587 	 * Table 9-250. All other cases are either factor of 1 or not
2588 	 * valid/supported.
2589 	 */
2590 	switch (bw) {
2591 	case IEEE80211_VHT_CHANWIDTH_USE_HT:
2592 	case IEEE80211_VHT_CHANWIDTH_80MHZ:
2593 		if ((supp_width == 1 || supp_width == 2) &&
2594 		    ext_nss_bw == 3)
2595 			return 2 * max_vht_nss;
2596 		break;
2597 	case IEEE80211_VHT_CHANWIDTH_160MHZ:
2598 		if (supp_width == 0 &&
2599 		    (ext_nss_bw == 1 || ext_nss_bw == 2))
2600 			return max_vht_nss / 2;
2601 		if (supp_width == 0 &&
2602 		    ext_nss_bw == 3)
2603 			return (3 * max_vht_nss) / 4;
2604 		if (supp_width == 1 &&
2605 		    ext_nss_bw == 3)
2606 			return 2 * max_vht_nss;
2607 		break;
2608 	case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2609 		if (supp_width == 0 && ext_nss_bw == 1)
2610 			return 0; /* not possible */
2611 		if (supp_width == 0 &&
2612 		    ext_nss_bw == 2)
2613 			return max_vht_nss / 2;
2614 		if (supp_width == 0 &&
2615 		    ext_nss_bw == 3)
2616 			return (3 * max_vht_nss) / 4;
2617 		if (supp_width == 1 &&
2618 		    ext_nss_bw == 0)
2619 			return 0; /* not possible */
2620 		if (supp_width == 1 &&
2621 		    ext_nss_bw == 1)
2622 			return max_vht_nss / 2;
2623 		if (supp_width == 1 &&
2624 		    ext_nss_bw == 2)
2625 			return (3 * max_vht_nss) / 4;
2626 		break;
2627 	}
2628 
2629 	/* not covered or invalid combination received */
2630 	return max_vht_nss;
2631 }
2632 EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
2633 
cfg80211_iftype_allowed(struct wiphy * wiphy,enum nl80211_iftype iftype,bool is_4addr,u8 check_swif)2634 bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2635 			     bool is_4addr, u8 check_swif)
2636 
2637 {
2638 	bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2639 
2640 	switch (check_swif) {
2641 	case 0:
2642 		if (is_vlan && is_4addr)
2643 			return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2644 		return wiphy->interface_modes & BIT(iftype);
2645 	case 1:
2646 		if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2647 			return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2648 		return wiphy->software_iftypes & BIT(iftype);
2649 	default:
2650 		break;
2651 	}
2652 
2653 	return false;
2654 }
2655 EXPORT_SYMBOL(cfg80211_iftype_allowed);
2656 
cfg80211_remove_link(struct wireless_dev * wdev,unsigned int link_id)2657 void cfg80211_remove_link(struct wireless_dev *wdev, unsigned int link_id)
2658 {
2659 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
2660 
2661 	ASSERT_WDEV_LOCK(wdev);
2662 
2663 	switch (wdev->iftype) {
2664 	case NL80211_IFTYPE_AP:
2665 	case NL80211_IFTYPE_P2P_GO:
2666 		__cfg80211_stop_ap(rdev, wdev->netdev, link_id, true);
2667 		break;
2668 	default:
2669 		/* per-link not relevant */
2670 		break;
2671 	}
2672 
2673 	wdev->valid_links &= ~BIT(link_id);
2674 
2675 	rdev_del_intf_link(rdev, wdev, link_id);
2676 
2677 	eth_zero_addr(wdev->links[link_id].addr);
2678 }
2679 
cfg80211_remove_links(struct wireless_dev * wdev)2680 void cfg80211_remove_links(struct wireless_dev *wdev)
2681 {
2682 	unsigned int link_id;
2683 
2684 	/*
2685 	 * links are controlled by upper layers (userspace/cfg)
2686 	 * only for AP mode, so only remove them here for AP
2687 	 */
2688 	if (wdev->iftype != NL80211_IFTYPE_AP)
2689 		return;
2690 
2691 	wdev_lock(wdev);
2692 	if (wdev->valid_links) {
2693 		for_each_valid_link(wdev, link_id)
2694 			cfg80211_remove_link(wdev, link_id);
2695 	}
2696 	wdev_unlock(wdev);
2697 }
2698 
cfg80211_remove_virtual_intf(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev)2699 int cfg80211_remove_virtual_intf(struct cfg80211_registered_device *rdev,
2700 				 struct wireless_dev *wdev)
2701 {
2702 	cfg80211_remove_links(wdev);
2703 
2704 	return rdev_del_virtual_intf(rdev, wdev);
2705 }
2706 
2707 const struct wiphy_iftype_ext_capab *
cfg80211_get_iftype_ext_capa(struct wiphy * wiphy,enum nl80211_iftype type)2708 cfg80211_get_iftype_ext_capa(struct wiphy *wiphy, enum nl80211_iftype type)
2709 {
2710 	int i;
2711 
2712 	for (i = 0; i < wiphy->num_iftype_ext_capab; i++) {
2713 		if (wiphy->iftype_ext_capab[i].iftype == type)
2714 			return &wiphy->iftype_ext_capab[i];
2715 	}
2716 
2717 	return NULL;
2718 }
2719 EXPORT_SYMBOL(cfg80211_get_iftype_ext_capa);
2720