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