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