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