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