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