blob: ac2bb1a80f2b4db537ed778a177e068b43d21de2 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Johannes Berg8318d782008-01-24 19:38:38 +01002/*
3 * Wireless utility functions
4 *
Johannes Bergd3236552009-04-20 14:31:42 +02005 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg2740f0c2014-09-03 15:24:58 +03006 * Copyright 2013-2014 Intel Mobile Communications GmbH
Luca Coelhoc4cbaf72018-06-09 09:14:42 +03007 * Copyright 2017 Intel Deutschland GmbH
Johannes Berg9166cc42020-03-26 15:09:32 +02008 * Copyright (C) 2018-2020 Intel Corporation
Johannes Berg8318d782008-01-24 19:38:38 +01009 */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040010#include <linux/export.h>
Johannes Bergd3236552009-04-20 14:31:42 +020011#include <linux/bitops.h>
Zhu Yie31a16d2009-05-21 21:47:03 +080012#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Johannes Bergb0aa75f2018-08-31 11:31:16 +030014#include <linux/ieee80211.h>
Johannes Bergd3236552009-04-20 14:31:42 +020015#include <net/cfg80211.h>
Zhu Yie31a16d2009-05-21 21:47:03 +080016#include <net/ip.h>
Dave Tähtb1565792011-12-22 12:55:08 -080017#include <net/dsfield.h>
cedric Vonckenc6ca5e22013-08-26 14:04:52 +020018#include <linux/if_vlan.h>
Simon Wunderlich960d97f2014-03-03 17:23:12 +010019#include <linux/mpls.h>
Johannes Berg4c8dea62016-10-21 14:25:13 +020020#include <linux/gcd.h>
Johannes Bergb0aa75f2018-08-31 11:31:16 +030021#include <linux/bitfield.h>
Johannes Berg1fc9b722019-02-06 13:17:14 +020022#include <linux/nospec.h>
Johannes Berg8318d782008-01-24 19:38:38 +010023#include "core.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030024#include "rdev-ops.h"
25
Johannes Berg8318d782008-01-24 19:38:38 +010026
Johannes Bergbd815252008-10-29 20:00:45 +010027struct ieee80211_rate *
28ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
Johannes Berg881d9482009-01-21 15:13:48 +010029 u32 basic_rates, int bitrate)
Johannes Bergbd815252008-10-29 20:00:45 +010030{
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
Simon Wunderlich74608ac2013-07-08 16:55:54 +020046u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
47 enum nl80211_bss_scan_width scan_width)
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070048{
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
Johannes Berg57fbcce2016-04-12 15:56:15 +020057 if (sband->band == NL80211_BAND_2GHZ) {
Simon Wunderlich74608ac2013-07-08 16:55:54 +020058 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 {
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070064 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
Simon Wunderlich74608ac2013-07-08 16:55:54 +020065 }
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070066
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
Thomas Pedersen934f4c72020-04-01 18:18:03 -070075u32 ieee80211_channel_to_freq_khz(int chan, enum nl80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +010076{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090077 /* see 802.11 17.3.8.3.2 and Annex J
78 * there are overlapping channel numbers in 5GHz and 2GHz bands */
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030079 if (chan <= 0)
80 return 0; /* not supported */
81 switch (band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +020082 case NL80211_BAND_2GHZ:
Bruno Randolf59eb21a2011-01-17 13:37:28 +090083 if (chan == 14)
Thomas Pedersen934f4c72020-04-01 18:18:03 -070084 return MHZ_TO_KHZ(2484);
Bruno Randolf59eb21a2011-01-17 13:37:28 +090085 else if (chan < 14)
Thomas Pedersen934f4c72020-04-01 18:18:03 -070086 return MHZ_TO_KHZ(2407 + chan * 5);
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030087 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +020088 case NL80211_BAND_5GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030089 if (chan >= 182 && chan <= 196)
Thomas Pedersen934f4c72020-04-01 18:18:03 -070090 return MHZ_TO_KHZ(4000 + chan * 5);
Bruno Randolf59eb21a2011-01-17 13:37:28 +090091 else
Thomas Pedersen934f4c72020-04-01 18:18:03 -070092 return MHZ_TO_KHZ(5000 + chan * 5);
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030093 break;
Arend van Sprielfa1f1082019-08-02 13:31:00 +020094 case NL80211_BAND_6GHZ:
Arend Van Sprield1a16462020-05-29 11:41:43 +020095 /* see 802.11ax D6.1 27.3.23.2 */
96 if (chan == 2)
97 return MHZ_TO_KHZ(5935);
Arend van Sprielfa1f1082019-08-02 13:31:00 +020098 if (chan <= 253)
Arend Van Sprield1a16462020-05-29 11:41:43 +020099 return MHZ_TO_KHZ(5950 + chan * 5);
Arend van Sprielfa1f1082019-08-02 13:31:00 +0200100 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200101 case NL80211_BAND_60GHZ:
Alexei Avshalom Lazar9cf0a0b2018-08-13 15:33:00 +0300102 if (chan < 7)
Thomas Pedersen934f4c72020-04-01 18:18:03 -0700103 return MHZ_TO_KHZ(56160 + chan * 2160);
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300104 break;
Thomas Pedersendf78a0c2020-06-01 23:22:47 -0700105 case NL80211_BAND_S1GHZ:
106 return 902000 + chan * 500;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300107 default:
108 ;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900109 }
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300110 return 0; /* not supported */
Johannes Berg8318d782008-01-24 19:38:38 +0100111}
Thomas Pedersen934f4c72020-04-01 18:18:03 -0700112EXPORT_SYMBOL(ieee80211_channel_to_freq_khz);
Johannes Berg8318d782008-01-24 19:38:38 +0100113
Thomas Pedersen11b34732020-09-08 12:03:06 -0700114enum nl80211_chan_width
115ieee80211_s1g_channel_width(const struct ieee80211_channel *chan)
116{
117 if (WARN_ON(!chan || chan->band != NL80211_BAND_S1GHZ))
118 return NL80211_CHAN_WIDTH_20_NOHT;
119
120 /*S1G defines a single allowed channel width per channel.
121 * Extract that width here.
122 */
123 if (chan->flags & IEEE80211_CHAN_1MHZ)
124 return NL80211_CHAN_WIDTH_1;
125 else if (chan->flags & IEEE80211_CHAN_2MHZ)
126 return NL80211_CHAN_WIDTH_2;
127 else if (chan->flags & IEEE80211_CHAN_4MHZ)
128 return NL80211_CHAN_WIDTH_4;
129 else if (chan->flags & IEEE80211_CHAN_8MHZ)
130 return NL80211_CHAN_WIDTH_8;
131 else if (chan->flags & IEEE80211_CHAN_16MHZ)
132 return NL80211_CHAN_WIDTH_16;
133
134 pr_err("unknown channel width for channel at %dKHz?\n",
135 ieee80211_channel_to_khz(chan));
136
137 return NL80211_CHAN_WIDTH_1;
138}
139EXPORT_SYMBOL(ieee80211_s1g_channel_width);
140
Thomas Pedersen934f4c72020-04-01 18:18:03 -0700141int ieee80211_freq_khz_to_channel(u32 freq)
Johannes Berg8318d782008-01-24 19:38:38 +0100142{
Thomas Pedersen934f4c72020-04-01 18:18:03 -0700143 /* TODO: just handle MHz for now */
144 freq = KHZ_TO_MHZ(freq);
145
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900146 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +0100147 if (freq == 2484)
148 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900149 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +0100150 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900151 else if (freq >= 4910 && freq <= 4980)
152 return (freq - 4000) / 5;
Amar Singhal2d9b5552020-06-19 13:52:01 -0700153 else if (freq < 5925)
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900154 return (freq - 5000) / 5;
Amar Singhal2d9b5552020-06-19 13:52:01 -0700155 else if (freq == 5935)
156 return 2;
Arend van Sprielfa1f1082019-08-02 13:31:00 +0200157 else if (freq <= 45000) /* DMG band lower limit */
Amar Singhal2d9b5552020-06-19 13:52:01 -0700158 /* see 802.11ax D6.1 27.3.22.2 */
159 return (freq - 5950) / 5;
Alexei Avshalom Lazar9cf0a0b2018-08-13 15:33:00 +0300160 else if (freq >= 58320 && freq <= 70200)
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300161 return (freq - 56160) / 2160;
162 else
163 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100164}
Thomas Pedersen934f4c72020-04-01 18:18:03 -0700165EXPORT_SYMBOL(ieee80211_freq_khz_to_channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100166
Thomas Pedersen934f4c72020-04-01 18:18:03 -0700167struct ieee80211_channel *ieee80211_get_channel_khz(struct wiphy *wiphy,
168 u32 freq)
Johannes Berg906c7302008-03-16 18:34:33 +0100169{
Johannes Berg57fbcce2016-04-12 15:56:15 +0200170 enum nl80211_band band;
Johannes Berg906c7302008-03-16 18:34:33 +0100171 struct ieee80211_supported_band *sband;
172 int i;
173
Johannes Berg57fbcce2016-04-12 15:56:15 +0200174 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Berg906c7302008-03-16 18:34:33 +0100175 sband = wiphy->bands[band];
176
177 if (!sband)
178 continue;
179
180 for (i = 0; i < sband->n_channels; i++) {
Thomas Pedersen934f4c72020-04-01 18:18:03 -0700181 struct ieee80211_channel *chan = &sband->channels[i];
182
183 if (ieee80211_channel_to_khz(chan) == freq)
184 return chan;
Johannes Berg906c7302008-03-16 18:34:33 +0100185 }
186 }
187
188 return NULL;
189}
Thomas Pedersen934f4c72020-04-01 18:18:03 -0700190EXPORT_SYMBOL(ieee80211_get_channel_khz);
Johannes Berg906c7302008-03-16 18:34:33 +0100191
Arend Van Spriel343884c2017-01-04 10:53:37 +0000192static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +0100193{
194 int i, want;
195
Arend Van Spriel343884c2017-01-04 10:53:37 +0000196 switch (sband->band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200197 case NL80211_BAND_5GHZ:
Arend van Spriel62524a52019-08-02 13:31:05 +0200198 case NL80211_BAND_6GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100199 want = 3;
200 for (i = 0; i < sband->n_bitrates; i++) {
201 if (sband->bitrates[i].bitrate == 60 ||
202 sband->bitrates[i].bitrate == 120 ||
203 sband->bitrates[i].bitrate == 240) {
204 sband->bitrates[i].flags |=
205 IEEE80211_RATE_MANDATORY_A;
206 want--;
207 }
208 }
209 WARN_ON(want);
210 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200211 case NL80211_BAND_2GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100212 want = 7;
213 for (i = 0; i < sband->n_bitrates; i++) {
Richard Schütz1bd773c2017-09-07 17:47:43 +0200214 switch (sband->bitrates[i].bitrate) {
215 case 10:
216 case 20:
217 case 55:
218 case 110:
Johannes Berg8318d782008-01-24 19:38:38 +0100219 sband->bitrates[i].flags |=
220 IEEE80211_RATE_MANDATORY_B |
221 IEEE80211_RATE_MANDATORY_G;
222 want--;
Richard Schütz1bd773c2017-09-07 17:47:43 +0200223 break;
224 case 60:
225 case 120:
226 case 240:
Johannes Berg8318d782008-01-24 19:38:38 +0100227 sband->bitrates[i].flags |=
228 IEEE80211_RATE_MANDATORY_G;
229 want--;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500230 fallthrough;
Richard Schütz1bd773c2017-09-07 17:47:43 +0200231 default:
Johannes Berg8318d782008-01-24 19:38:38 +0100232 sband->bitrates[i].flags |=
233 IEEE80211_RATE_ERP_G;
Richard Schütz1bd773c2017-09-07 17:47:43 +0200234 break;
235 }
Johannes Berg8318d782008-01-24 19:38:38 +0100236 }
Richard Schütz1bd773c2017-09-07 17:47:43 +0200237 WARN_ON(want != 0 && want != 3);
Johannes Berg8318d782008-01-24 19:38:38 +0100238 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200239 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300240 /* check for mandatory HT MCS 1..4 */
241 WARN_ON(!sband->ht_cap.ht_supported);
242 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
243 break;
Thomas Pedersendf78a0c2020-06-01 23:22:47 -0700244 case NL80211_BAND_S1GHZ:
245 /* Figure 9-589bd: 3 means unsupported, so != 3 means at least
246 * mandatory is ok.
247 */
248 WARN_ON((sband->s1g_cap.nss_mcs[0] & 0x3) == 0x3);
249 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200250 case NUM_NL80211_BANDS:
Arend Van Spriel343884c2017-01-04 10:53:37 +0000251 default:
Johannes Berg8318d782008-01-24 19:38:38 +0100252 WARN_ON(1);
253 break;
254 }
255}
256
257void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
258{
Johannes Berg57fbcce2016-04-12 15:56:15 +0200259 enum nl80211_band band;
Johannes Berg8318d782008-01-24 19:38:38 +0100260
Johannes Berg57fbcce2016-04-12 15:56:15 +0200261 for (band = 0; band < NUM_NL80211_BANDS; band++)
Johannes Berg8318d782008-01-24 19:38:38 +0100262 if (wiphy->bands[band])
Arend Van Spriel343884c2017-01-04 10:53:37 +0000263 set_mandatory_flags_band(wiphy->bands[band]);
Johannes Berg8318d782008-01-24 19:38:38 +0100264}
Johannes Berg08645122009-05-11 13:54:58 +0200265
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300266bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
267{
268 int i;
269 for (i = 0; i < wiphy->n_cipher_suites; i++)
270 if (cipher == wiphy->cipher_suites[i])
271 return true;
272 return false;
273}
274
Johannes Bergfffd0932009-07-08 14:22:54 +0200275int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
276 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200277 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200278{
Jouni Malinen56be3932020-02-22 15:25:43 +0200279 int max_key_idx = 5;
280
281 if (wiphy_ext_feature_isset(&rdev->wiphy,
Johannes Berg0e479012020-05-28 21:34:24 +0200282 NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
283 wiphy_ext_feature_isset(&rdev->wiphy,
284 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
Jouni Malinen56be3932020-02-22 15:25:43 +0200285 max_key_idx = 7;
286 if (key_idx < 0 || key_idx > max_key_idx)
Johannes Berg08645122009-05-11 13:54:58 +0200287 return -EINVAL;
288
Johannes Berge31b8212010-10-05 19:39:30 +0200289 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
290 return -EINVAL;
291
292 if (pairwise && !mac_addr)
293 return -EINVAL;
294
Jouni Malinen37720562015-01-24 19:52:04 +0200295 switch (params->cipher) {
296 case WLAN_CIPHER_SUITE_TKIP:
Alexander Wetzelb67fd722019-08-05 14:34:00 +0200297 /* Extended Key ID can only be used with CCMP/GCMP ciphers */
298 if ((pairwise && key_idx) ||
299 params->mode != NL80211_KEY_RX_TX)
300 return -EINVAL;
301 break;
Jouni Malinen37720562015-01-24 19:52:04 +0200302 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200303 case WLAN_CIPHER_SUITE_CCMP_256:
304 case WLAN_CIPHER_SUITE_GCMP:
305 case WLAN_CIPHER_SUITE_GCMP_256:
Alexander Wetzelb67fd722019-08-05 14:34:00 +0200306 /* IEEE802.11-2016 allows only 0 and - when supporting
307 * Extended Key ID - 1 as index for pairwise keys.
Alexander Wetzel6cdd3972019-03-19 21:34:07 +0100308 * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
309 * the driver supports Extended Key ID.
310 * @NL80211_KEY_SET_TX can't be set when installing and
311 * validating a key.
Jouni Malinen37720562015-01-24 19:52:04 +0200312 */
Alexander Wetzelb67fd722019-08-05 14:34:00 +0200313 if ((params->mode == NL80211_KEY_NO_TX && !pairwise) ||
314 params->mode == NL80211_KEY_SET_TX)
315 return -EINVAL;
316 if (wiphy_ext_feature_isset(&rdev->wiphy,
317 NL80211_EXT_FEATURE_EXT_KEY_ID)) {
318 if (pairwise && (key_idx < 0 || key_idx > 1))
Alexander Wetzel6cdd3972019-03-19 21:34:07 +0100319 return -EINVAL;
Alexander Wetzelb67fd722019-08-05 14:34:00 +0200320 } else if (pairwise && key_idx) {
Jouni Malinen37720562015-01-24 19:52:04 +0200321 return -EINVAL;
Alexander Wetzel6cdd3972019-03-19 21:34:07 +0100322 }
Jouni Malinen37720562015-01-24 19:52:04 +0200323 break;
324 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200325 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
326 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
327 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen37720562015-01-24 19:52:04 +0200328 /* Disallow BIP (group-only) cipher as pairwise cipher */
329 if (pairwise)
330 return -EINVAL;
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200331 if (key_idx < 4)
332 return -EINVAL;
Jouni Malinen37720562015-01-24 19:52:04 +0200333 break;
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200334 case WLAN_CIPHER_SUITE_WEP40:
335 case WLAN_CIPHER_SUITE_WEP104:
336 if (key_idx > 3)
337 return -EINVAL;
Jouni Malinen37720562015-01-24 19:52:04 +0200338 default:
339 break;
340 }
Johannes Berg08645122009-05-11 13:54:58 +0200341
Johannes Berg08645122009-05-11 13:54:58 +0200342 switch (params->cipher) {
343 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200344 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200345 return -EINVAL;
346 break;
347 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200348 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200349 return -EINVAL;
350 break;
351 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200352 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200353 return -EINVAL;
354 break;
Jouni Malinencfcf1682015-01-24 19:52:05 +0200355 case WLAN_CIPHER_SUITE_CCMP_256:
356 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
357 return -EINVAL;
358 break;
359 case WLAN_CIPHER_SUITE_GCMP:
360 if (params->key_len != WLAN_KEY_LEN_GCMP)
361 return -EINVAL;
362 break;
363 case WLAN_CIPHER_SUITE_GCMP_256:
364 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
365 return -EINVAL;
366 break;
Johannes Berg08645122009-05-11 13:54:58 +0200367 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200368 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200369 return -EINVAL;
370 break;
371 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200372 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200373 return -EINVAL;
374 break;
Jouni Malinencfcf1682015-01-24 19:52:05 +0200375 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
376 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
377 return -EINVAL;
378 break;
379 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
380 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
381 return -EINVAL;
382 break;
383 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
384 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
385 return -EINVAL;
386 break;
Johannes Berg08645122009-05-11 13:54:58 +0200387 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300388 /*
389 * We don't know anything about this algorithm,
390 * allow using it -- but the driver must check
391 * all parameters! We still check below whether
392 * or not the driver supports this algorithm,
393 * of course.
394 */
395 break;
Johannes Berg08645122009-05-11 13:54:58 +0200396 }
397
Jouni Malinen9f26a952009-05-15 12:38:32 +0300398 if (params->seq) {
399 switch (params->cipher) {
400 case WLAN_CIPHER_SUITE_WEP40:
401 case WLAN_CIPHER_SUITE_WEP104:
402 /* These ciphers do not use key sequence */
403 return -EINVAL;
404 case WLAN_CIPHER_SUITE_TKIP:
405 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200406 case WLAN_CIPHER_SUITE_CCMP_256:
407 case WLAN_CIPHER_SUITE_GCMP:
408 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinen9f26a952009-05-15 12:38:32 +0300409 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200410 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
411 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
412 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen9f26a952009-05-15 12:38:32 +0300413 if (params->seq_len != 6)
414 return -EINVAL;
415 break;
416 }
417 }
418
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300419 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200420 return -EINVAL;
421
Johannes Berg08645122009-05-11 13:54:58 +0200422 return 0;
423}
Zhu Yie31a16d2009-05-21 21:47:03 +0800424
Johannes Berg633adf12010-08-12 14:49:58 +0200425unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800426{
427 unsigned int hdrlen = 24;
428
Thomas Pedersen1d47f112020-09-08 12:03:05 -0700429 if (ieee80211_is_ext(fc)) {
430 hdrlen = 4;
431 goto out;
432 }
433
Zhu Yie31a16d2009-05-21 21:47:03 +0800434 if (ieee80211_is_data(fc)) {
435 if (ieee80211_has_a4(fc))
436 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200437 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800438 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200439 if (ieee80211_has_order(fc))
440 hdrlen += IEEE80211_HT_CTL_LEN;
441 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800442 goto out;
443 }
444
Fred Choufb142f42015-01-20 10:17:27 +0800445 if (ieee80211_is_mgmt(fc)) {
446 if (ieee80211_has_order(fc))
447 hdrlen += IEEE80211_HT_CTL_LEN;
448 goto out;
449 }
450
Zhu Yie31a16d2009-05-21 21:47:03 +0800451 if (ieee80211_is_ctl(fc)) {
452 /*
453 * ACK and CTS are 10 bytes, all others 16. To see how
454 * to get this condition consider
455 * subtype mask: 0b0000000011110000 (0x00F0)
456 * ACK subtype: 0b0000000011010000 (0x00D0)
457 * CTS subtype: 0b0000000011000000 (0x00C0)
458 * bits that matter: ^^^ (0x00E0)
459 * value of those: 0b0000000011000000 (0x00C0)
460 */
461 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
462 hdrlen = 10;
463 else
464 hdrlen = 16;
465 }
466out:
467 return hdrlen;
468}
469EXPORT_SYMBOL(ieee80211_hdrlen);
470
471unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
472{
473 const struct ieee80211_hdr *hdr =
474 (const struct ieee80211_hdr *)skb->data;
475 unsigned int hdrlen;
476
477 if (unlikely(skb->len < 10))
478 return 0;
479 hdrlen = ieee80211_hdrlen(hdr->frame_control);
480 if (unlikely(hdrlen > skb->len))
481 return 0;
482 return hdrlen;
483}
484EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
485
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100486static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
Zhu Yie31a16d2009-05-21 21:47:03 +0800487{
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100488 int ae = flags & MESH_FLAGS_AE;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200489 /* 802.11-2012, 8.2.4.7.3 */
Zhu Yie31a16d2009-05-21 21:47:03 +0800490 switch (ae) {
Johannes Berg7dd111e2012-10-25 21:51:59 +0200491 default:
Zhu Yie31a16d2009-05-21 21:47:03 +0800492 case 0:
493 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700494 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800495 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700496 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800497 return 18;
Zhu Yie31a16d2009-05-21 21:47:03 +0800498 }
499}
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100500
501unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
502{
503 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
504}
Johannes Berg9b395bc2012-10-26 00:36:40 +0200505EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800506
Johannes Berg7f6990c2016-10-05 15:29:49 +0200507int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
Felix Fietkau24bba072018-02-27 13:03:07 +0100508 const u8 *addr, enum nl80211_iftype iftype,
509 u8 data_offset)
Zhu Yie31a16d2009-05-21 21:47:03 +0800510{
511 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100512 struct {
513 u8 hdr[ETH_ALEN] __aligned(2);
514 __be16 proto;
515 } payload;
516 struct ethhdr tmp;
517 u16 hdrlen;
518 u8 mesh_flags = 0;
Zhu Yie31a16d2009-05-21 21:47:03 +0800519
520 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
521 return -1;
522
Felix Fietkau24bba072018-02-27 13:03:07 +0100523 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100524 if (skb->len < hdrlen + 8)
525 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800526
527 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
528 * header
529 * IEEE 802.11 address fields:
530 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
531 * 0 0 DA SA BSSID n/a
532 * 0 1 DA BSSID SA n/a
533 * 1 0 BSSID SA DA n/a
534 * 1 1 RA TA DA SA
535 */
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100536 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
537 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
538
539 if (iftype == NL80211_IFTYPE_MESH_POINT)
540 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
Zhu Yie31a16d2009-05-21 21:47:03 +0800541
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700542 mesh_flags &= MESH_FLAGS_AE;
543
Zhu Yie31a16d2009-05-21 21:47:03 +0800544 switch (hdr->frame_control &
545 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
546 case cpu_to_le16(IEEE80211_FCTL_TODS):
547 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200548 iftype != NL80211_IFTYPE_AP_VLAN &&
549 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800550 return -1;
551 break;
552 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
553 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543ee2009-11-10 20:10:05 +0100554 iftype != NL80211_IFTYPE_MESH_POINT &&
555 iftype != NL80211_IFTYPE_AP_VLAN &&
556 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800557 return -1;
558 if (iftype == NL80211_IFTYPE_MESH_POINT) {
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700559 if (mesh_flags == MESH_FLAGS_AE_A4)
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800560 return -1;
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700561 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800562 skb_copy_bits(skb, hdrlen +
563 offsetof(struct ieee80211s_hdr, eaddr1),
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100564 tmp.h_dest, 2 * ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800565 }
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100566 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
Zhu Yie31a16d2009-05-21 21:47:03 +0800567 }
568 break;
569 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700570 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200571 iftype != NL80211_IFTYPE_P2P_CLIENT &&
572 iftype != NL80211_IFTYPE_MESH_POINT) ||
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100573 (is_multicast_ether_addr(tmp.h_dest) &&
574 ether_addr_equal(tmp.h_source, addr)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800575 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700576 if (iftype == NL80211_IFTYPE_MESH_POINT) {
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700577 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800578 return -1;
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700579 if (mesh_flags == MESH_FLAGS_AE_A4)
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800580 skb_copy_bits(skb, hdrlen +
581 offsetof(struct ieee80211s_hdr, eaddr1),
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100582 tmp.h_source, ETH_ALEN);
583 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700584 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800585 break;
586 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300587 if (iftype != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100588 iftype != NL80211_IFTYPE_STATION &&
589 iftype != NL80211_IFTYPE_OCB)
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300590 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800591 break;
592 }
593
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100594 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
595 tmp.h_proto = payload.proto;
Zhu Yie31a16d2009-05-21 21:47:03 +0800596
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100597 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
598 tmp.h_proto != htons(ETH_P_AARP) &&
599 tmp.h_proto != htons(ETH_P_IPX)) ||
600 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800601 /* remove RFC1042 or Bridge-Tunnel encapsulation and
602 * replace EtherType */
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100603 hdrlen += ETH_ALEN + 2;
604 else
Felix Fietkauc0417782016-06-29 10:36:39 +0200605 tmp.h_proto = htons(skb->len - hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800606
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100607 pskb_pull(skb, hdrlen);
608
609 if (!ehdr)
Johannes Bergd58ff352017-06-16 14:29:23 +0200610 ehdr = skb_push(skb, sizeof(struct ethhdr));
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100611 memcpy(ehdr, &tmp, sizeof(tmp));
612
Zhu Yie31a16d2009-05-21 21:47:03 +0800613 return 0;
614}
Johannes Berg7f6990c2016-10-05 15:29:49 +0200615EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800616
Felix Fietkau2b67f942016-02-08 14:34:42 +0100617static void
618__frame_add_frag(struct sk_buff *skb, struct page *page,
619 void *ptr, int len, int size)
620{
621 struct skb_shared_info *sh = skb_shinfo(skb);
622 int page_offset;
623
Felix Fietkau81c044f2020-01-13 19:21:07 +0100624 get_page(page);
Felix Fietkau2b67f942016-02-08 14:34:42 +0100625 page_offset = ptr - page_address(page);
626 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
627}
628
629static void
630__ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
631 int offset, int len)
632{
633 struct skb_shared_info *sh = skb_shinfo(skb);
Matthias Kaehlckeaa1702d2017-04-13 10:05:04 -0700634 const skb_frag_t *frag = &sh->frags[0];
Felix Fietkau2b67f942016-02-08 14:34:42 +0100635 struct page *frag_page;
636 void *frag_ptr;
637 int frag_len, frag_size;
638 int head_size = skb->len - skb->data_len;
639 int cur_len;
640
641 frag_page = virt_to_head_page(skb->head);
642 frag_ptr = skb->data;
643 frag_size = head_size;
644
645 while (offset >= frag_size) {
646 offset -= frag_size;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100647 frag_page = skb_frag_page(frag);
648 frag_ptr = skb_frag_address(frag);
649 frag_size = skb_frag_size(frag);
Matthias Kaehlckeaa1702d2017-04-13 10:05:04 -0700650 frag++;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100651 }
652
653 frag_ptr += offset;
654 frag_len = frag_size - offset;
655
656 cur_len = min(len, frag_len);
657
658 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
659 len -= cur_len;
660
661 while (len > 0) {
Felix Fietkau2b67f942016-02-08 14:34:42 +0100662 frag_len = skb_frag_size(frag);
663 cur_len = min(len, frag_len);
664 __frame_add_frag(frame, skb_frag_page(frag),
665 skb_frag_address(frag), cur_len, frag_len);
666 len -= cur_len;
Matthias Kaehlckeaa1702d2017-04-13 10:05:04 -0700667 frag++;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100668 }
669}
670
Felix Fietkau230fd282016-02-02 14:39:10 +0100671static struct sk_buff *
672__ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
Felix Fietkau2b67f942016-02-08 14:34:42 +0100673 int offset, int len, bool reuse_frag)
Felix Fietkau230fd282016-02-02 14:39:10 +0100674{
675 struct sk_buff *frame;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100676 int cur_len = len;
Felix Fietkau230fd282016-02-02 14:39:10 +0100677
678 if (skb->len - offset < len)
679 return NULL;
680
681 /*
Felix Fietkau2b67f942016-02-08 14:34:42 +0100682 * When reusing framents, copy some data to the head to simplify
683 * ethernet header handling and speed up protocol header processing
684 * in the stack later.
685 */
686 if (reuse_frag)
687 cur_len = min_t(int, len, 32);
688
689 /*
Felix Fietkau230fd282016-02-02 14:39:10 +0100690 * Allocate and reserve two bytes more for payload
691 * alignment since sizeof(struct ethhdr) is 14.
692 */
Felix Fietkau2b67f942016-02-08 14:34:42 +0100693 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
Gregory Greenman16a910a2016-07-05 15:23:10 +0300694 if (!frame)
695 return NULL;
Felix Fietkau230fd282016-02-02 14:39:10 +0100696
697 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
Felix Fietkau2b67f942016-02-08 14:34:42 +0100698 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
699
700 len -= cur_len;
701 if (!len)
702 return frame;
703
704 offset += cur_len;
705 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
Felix Fietkau230fd282016-02-02 14:39:10 +0100706
707 return frame;
708}
Zhu Yieaf85ca2009-12-01 10:18:37 +0800709
710void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
711 const u8 *addr, enum nl80211_iftype iftype,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700712 const unsigned int extra_headroom,
Johannes Berg8b935ee2016-10-05 16:17:01 +0200713 const u8 *check_da, const u8 *check_sa)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800714{
Felix Fietkau230fd282016-02-02 14:39:10 +0100715 unsigned int hlen = ALIGN(extra_headroom, 4);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800716 struct sk_buff *frame = NULL;
717 u16 ethertype;
718 u8 *payload;
Johannes Berg7f6990c2016-10-05 15:29:49 +0200719 int offset = 0, remaining;
Felix Fietkau230fd282016-02-02 14:39:10 +0100720 struct ethhdr eth;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100721 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
Felix Fietkau2bf0ccc2016-02-08 14:25:26 +0100722 bool reuse_skb = false;
Felix Fietkau230fd282016-02-02 14:39:10 +0100723 bool last = false;
Felix Fietkau88665f52016-02-02 14:39:08 +0100724
Felix Fietkau230fd282016-02-02 14:39:10 +0100725 while (!last) {
726 unsigned int subframe_len;
727 int len;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800728 u8 padding;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800729
Felix Fietkau230fd282016-02-02 14:39:10 +0100730 skb_copy_bits(skb, offset, &eth, sizeof(eth));
731 len = ntohs(eth.h_proto);
732 subframe_len = sizeof(struct ethhdr) + len;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800733 padding = (4 - subframe_len) & 0x3;
Felix Fietkau230fd282016-02-02 14:39:10 +0100734
Zhu Yieaf85ca2009-12-01 10:18:37 +0800735 /* the last MSDU has no padding */
Felix Fietkau230fd282016-02-02 14:39:10 +0100736 remaining = skb->len - offset;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800737 if (subframe_len > remaining)
738 goto purge;
739
Felix Fietkau230fd282016-02-02 14:39:10 +0100740 offset += sizeof(struct ethhdr);
Felix Fietkau230fd282016-02-02 14:39:10 +0100741 last = remaining <= subframe_len + padding;
Johannes Berg8b935ee2016-10-05 16:17:01 +0200742
743 /* FIXME: should we really accept multicast DA? */
744 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
745 !ether_addr_equal(check_da, eth.h_dest)) ||
746 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
747 offset += len + padding;
748 continue;
749 }
750
751 /* reuse skb for the last subframe */
Felix Fietkau2b67f942016-02-08 14:34:42 +0100752 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
Felix Fietkau230fd282016-02-02 14:39:10 +0100753 skb_pull(skb, offset);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800754 frame = skb;
Felix Fietkau230fd282016-02-02 14:39:10 +0100755 reuse_skb = true;
756 } else {
Felix Fietkau2b67f942016-02-08 14:34:42 +0100757 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
758 reuse_frag);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800759 if (!frame)
760 goto purge;
761
Felix Fietkau230fd282016-02-02 14:39:10 +0100762 offset += len + padding;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800763 }
764
765 skb_reset_network_header(frame);
766 frame->dev = skb->dev;
767 frame->priority = skb->priority;
768
769 payload = frame->data;
770 ethertype = (payload[6] << 8) | payload[7];
Joe Perchesac422d32012-05-08 18:56:55 +0000771 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yieaf85ca2009-12-01 10:18:37 +0800772 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perchesac422d32012-05-08 18:56:55 +0000773 ether_addr_equal(payload, bridge_tunnel_header))) {
Felix Fietkau230fd282016-02-02 14:39:10 +0100774 eth.h_proto = htons(ethertype);
775 skb_pull(frame, ETH_ALEN + 2);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800776 }
Felix Fietkau230fd282016-02-02 14:39:10 +0100777
778 memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
Zhu Yieaf85ca2009-12-01 10:18:37 +0800779 __skb_queue_tail(list, frame);
780 }
781
Felix Fietkau230fd282016-02-02 14:39:10 +0100782 if (!reuse_skb)
783 dev_kfree_skb(skb);
784
Zhu Yieaf85ca2009-12-01 10:18:37 +0800785 return;
786
787 purge:
788 __skb_queue_purge(list);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800789 dev_kfree_skb(skb);
790}
791EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
792
Zhu Yie31a16d2009-05-21 21:47:03 +0800793/* Given a data frame determine the 802.1p/1d tag to use. */
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800794unsigned int cfg80211_classify8021d(struct sk_buff *skb,
795 struct cfg80211_qos_map *qos_map)
Zhu Yie31a16d2009-05-21 21:47:03 +0800796{
797 unsigned int dscp;
cedric Vonckenc6ca5e22013-08-26 14:04:52 +0200798 unsigned char vlan_priority;
Johannes Berg1fc9b722019-02-06 13:17:14 +0200799 unsigned int ret;
Zhu Yie31a16d2009-05-21 21:47:03 +0800800
801 /* skb->priority values from 256->263 are magic values to
802 * directly indicate a specific 802.1d priority. This is used
803 * to allow 802.1d priority to be passed directly in from VLAN
804 * tags, etc.
805 */
Johannes Berg1fc9b722019-02-06 13:17:14 +0200806 if (skb->priority >= 256 && skb->priority <= 263) {
807 ret = skb->priority - 256;
808 goto out;
809 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800810
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100811 if (skb_vlan_tag_present(skb)) {
812 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
cedric Vonckenc6ca5e22013-08-26 14:04:52 +0200813 >> VLAN_PRIO_SHIFT;
Johannes Berg1fc9b722019-02-06 13:17:14 +0200814 if (vlan_priority > 0) {
815 ret = vlan_priority;
816 goto out;
817 }
cedric Vonckenc6ca5e22013-08-26 14:04:52 +0200818 }
819
Zhu Yie31a16d2009-05-21 21:47:03 +0800820 switch (skb->protocol) {
821 case htons(ETH_P_IP):
Dave Tähtb1565792011-12-22 12:55:08 -0800822 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
823 break;
824 case htons(ETH_P_IPV6):
825 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
Zhu Yie31a16d2009-05-21 21:47:03 +0800826 break;
Simon Wunderlich960d97f2014-03-03 17:23:12 +0100827 case htons(ETH_P_MPLS_UC):
828 case htons(ETH_P_MPLS_MC): {
829 struct mpls_label mpls_tmp, *mpls;
830
831 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
832 sizeof(*mpls), &mpls_tmp);
833 if (!mpls)
834 return 0;
835
Johannes Berg1fc9b722019-02-06 13:17:14 +0200836 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
Simon Wunderlich960d97f2014-03-03 17:23:12 +0100837 >> MPLS_LS_TC_SHIFT;
Johannes Berg1fc9b722019-02-06 13:17:14 +0200838 goto out;
Simon Wunderlich960d97f2014-03-03 17:23:12 +0100839 }
840 case htons(ETH_P_80221):
841 /* 802.21 is always network control traffic */
842 return 7;
Zhu Yie31a16d2009-05-21 21:47:03 +0800843 default:
844 return 0;
845 }
846
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800847 if (qos_map) {
848 unsigned int i, tmp_dscp = dscp >> 2;
849
850 for (i = 0; i < qos_map->num_des; i++) {
Johannes Berg1fc9b722019-02-06 13:17:14 +0200851 if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
852 ret = qos_map->dscp_exception[i].up;
853 goto out;
854 }
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800855 }
856
857 for (i = 0; i < 8; i++) {
858 if (tmp_dscp >= qos_map->up[i].low &&
Johannes Berg1fc9b722019-02-06 13:17:14 +0200859 tmp_dscp <= qos_map->up[i].high) {
860 ret = i;
861 goto out;
862 }
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800863 }
864 }
865
Johannes Berg1fc9b722019-02-06 13:17:14 +0200866 ret = dscp >> 5;
867out:
868 return array_index_nospec(ret, IEEE80211_NUM_TIDS);
Zhu Yie31a16d2009-05-21 21:47:03 +0800869}
870EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200871
Johannes Berg49a68e02019-02-07 23:26:38 +0100872const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
Johannes Berg517357c2009-07-02 17:18:40 +0200873{
Johannes Berg9caf0362012-11-29 01:25:20 +0100874 const struct cfg80211_bss_ies *ies;
875
876 ies = rcu_dereference(bss->ies);
877 if (!ies)
Johannes Berg517357c2009-07-02 17:18:40 +0200878 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +0100879
Johannes Berg49a68e02019-02-07 23:26:38 +0100880 return cfg80211_find_elem(id, ies->data, ies->len);
Johannes Berg517357c2009-07-02 17:18:40 +0200881}
Johannes Berg49a68e02019-02-07 23:26:38 +0100882EXPORT_SYMBOL(ieee80211_bss_get_elem);
Johannes Bergfffd0932009-07-08 14:22:54 +0200883
884void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
885{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800886 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200887 struct net_device *dev = wdev->netdev;
888 int i;
889
890 if (!wdev->connect_keys)
891 return;
892
David Spinadelb8676222016-09-22 23:16:50 +0300893 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200894 if (!wdev->connect_keys->params[i].cipher)
895 continue;
Hila Gonene35e4d22012-06-27 17:19:42 +0300896 if (rdev_add_key(rdev, dev, i, false, NULL,
897 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800898 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800899 continue;
900 }
Johannes Bergd4f29972017-02-13 20:53:38 +0100901 if (wdev->connect_keys->def == i &&
902 rdev_set_default_key(rdev, dev, i, true, true)) {
903 netdev_err(dev, "failed to set defkey %d\n", i);
904 continue;
905 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200906 }
907
Waiman Long453431a2020-08-06 23:18:13 -0700908 kfree_sensitive(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200909 wdev->connect_keys = NULL;
910}
Johannes Berg3d54d252009-08-21 14:51:05 +0200911
Daniel Drake1f6fc432012-08-02 18:41:48 +0100912void cfg80211_process_wdev_events(struct wireless_dev *wdev)
Johannes Berg3d54d252009-08-21 14:51:05 +0200913{
914 struct cfg80211_event *ev;
915 unsigned long flags;
Johannes Berg3d54d252009-08-21 14:51:05 +0200916
917 spin_lock_irqsave(&wdev->event_lock, flags);
918 while (!list_empty(&wdev->event_list)) {
919 ev = list_first_entry(&wdev->event_list,
920 struct cfg80211_event, list);
921 list_del(&ev->list);
922 spin_unlock_irqrestore(&wdev->event_lock, flags);
923
924 wdev_lock(wdev);
925 switch (ev->type) {
926 case EVENT_CONNECT_RESULT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200927 __cfg80211_connect_result(
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300928 wdev->netdev,
929 &ev->cr,
930 ev->cr.status == WLAN_STATUS_SUCCESS);
Johannes Berg3d54d252009-08-21 14:51:05 +0200931 break;
932 case EVENT_ROAMED:
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300933 __cfg80211_roamed(wdev, &ev->rm);
Johannes Berg3d54d252009-08-21 14:51:05 +0200934 break;
935 case EVENT_DISCONNECTED:
936 __cfg80211_disconnected(wdev->netdev,
937 ev->dc.ie, ev->dc.ie_len,
Johannes Berg80279fb2015-05-22 16:22:20 +0200938 ev->dc.reason,
939 !ev->dc.locally_generated);
Johannes Berg3d54d252009-08-21 14:51:05 +0200940 break;
941 case EVENT_IBSS_JOINED:
Antonio Quartullife94f3a2014-01-29 17:53:43 +0100942 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
943 ev->ij.channel);
Johannes Berg3d54d252009-08-21 14:51:05 +0200944 break;
Michal Kaziorf04c2202014-04-09 15:11:01 +0200945 case EVENT_STOPPED:
946 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
947 break;
Avraham Stern503c1fb2017-09-29 14:21:49 +0200948 case EVENT_PORT_AUTHORIZED:
949 __cfg80211_port_authorized(wdev, ev->pa.bssid);
950 break;
Johannes Berg3d54d252009-08-21 14:51:05 +0200951 }
952 wdev_unlock(wdev);
953
954 kfree(ev);
955
956 spin_lock_irqsave(&wdev->event_lock, flags);
957 }
958 spin_unlock_irqrestore(&wdev->event_lock, flags);
959}
960
961void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
962{
963 struct wireless_dev *wdev;
964
965 ASSERT_RTNL();
Johannes Berg3d54d252009-08-21 14:51:05 +0200966
Johannes Berg53873f12016-05-03 16:52:04 +0300967 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
Johannes Berg3d54d252009-08-21 14:51:05 +0200968 cfg80211_process_wdev_events(wdev);
Johannes Berg3d54d252009-08-21 14:51:05 +0200969}
970
971int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
972 struct net_device *dev, enum nl80211_iftype ntype,
Johannes Berg818a9862017-04-12 11:23:28 +0200973 struct vif_params *params)
Johannes Berg3d54d252009-08-21 14:51:05 +0200974{
975 int err;
976 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
977
Zhao, Gang73fb08e2014-03-19 17:04:36 +0800978 ASSERT_RTNL();
Johannes Berg3d54d252009-08-21 14:51:05 +0200979
980 /* don't support changing VLANs, you just re-create them */
981 if (otype == NL80211_IFTYPE_AP_VLAN)
982 return -EOPNOTSUPP;
983
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300984 /* cannot change into P2P device or NAN */
985 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
986 ntype == NL80211_IFTYPE_NAN)
Johannes Berg98104fde2012-06-16 00:19:54 +0200987 return -EOPNOTSUPP;
988
Johannes Berg3d54d252009-08-21 14:51:05 +0200989 if (!rdev->ops->change_virtual_intf ||
990 !(rdev->wiphy.interface_modes & (1 << ntype)))
991 return -EOPNOTSUPP;
992
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100993 /* if it's part of a bridge, reject changing type to station/ibss */
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +0100994 if (netif_is_bridge_port(dev) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200995 (ntype == NL80211_IFTYPE_ADHOC ||
996 ntype == NL80211_IFTYPE_STATION ||
997 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100998 return -EBUSY;
999
Michal Kazior6cbfb1b2015-05-22 10:57:22 +02001000 if (ntype != otype) {
Johannes Berg9bc383d2009-11-19 11:55:19 +01001001 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +01001002 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg194ff522013-12-30 23:12:37 +01001003 wdev_lock(dev->ieee80211_ptr);
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001004 rdev_set_qos_map(rdev, dev, NULL);
Johannes Berg194ff522013-12-30 23:12:37 +01001005 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001006
Johannes Berg3d54d252009-08-21 14:51:05 +02001007 switch (otype) {
Michal Kaziorac800142012-06-29 12:46:57 +02001008 case NL80211_IFTYPE_AP:
Ilan Peer7c8d5e02014-02-25 15:33:38 +02001009 cfg80211_stop_ap(rdev, dev, true);
Michal Kaziorac800142012-06-29 12:46:57 +02001010 break;
Johannes Berg3d54d252009-08-21 14:51:05 +02001011 case NL80211_IFTYPE_ADHOC:
1012 cfg80211_leave_ibss(rdev, dev, false);
1013 break;
1014 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +02001015 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg83739b02013-05-15 17:44:01 +02001016 wdev_lock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +02001017 cfg80211_disconnect(rdev, dev,
1018 WLAN_REASON_DEAUTH_LEAVING, true);
Johannes Berg83739b02013-05-15 17:44:01 +02001019 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +02001020 break;
1021 case NL80211_IFTYPE_MESH_POINT:
1022 /* mesh should be handled? */
1023 break;
1024 default:
1025 break;
1026 }
1027
1028 cfg80211_process_rdev_events(rdev);
Denis Kenziorc1d3ad82019-08-28 16:11:10 -05001029 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +02001030 }
1031
Johannes Berg818a9862017-04-12 11:23:28 +02001032 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
Johannes Berg3d54d252009-08-21 14:51:05 +02001033
1034 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1035
Johannes Berg9bc383d2009-11-19 11:55:19 +01001036 if (!err && params && params->use_4addr != -1)
1037 dev->ieee80211_ptr->use_4addr = params->use_4addr;
1038
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001039 if (!err) {
1040 dev->priv_flags &= ~IFF_DONT_BRIDGE;
1041 switch (ntype) {
1042 case NL80211_IFTYPE_STATION:
1043 if (dev->ieee80211_ptr->use_4addr)
1044 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001045 fallthrough;
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +01001046 case NL80211_IFTYPE_OCB:
Johannes Berg074ac8d2010-09-16 14:58:22 +02001047 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001048 case NL80211_IFTYPE_ADHOC:
1049 dev->priv_flags |= IFF_DONT_BRIDGE;
1050 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02001051 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001052 case NL80211_IFTYPE_AP:
1053 case NL80211_IFTYPE_AP_VLAN:
1054 case NL80211_IFTYPE_WDS:
1055 case NL80211_IFTYPE_MESH_POINT:
1056 /* bridging OK */
1057 break;
1058 case NL80211_IFTYPE_MONITOR:
1059 /* monitor can't bridge anyway */
1060 break;
1061 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +02001062 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001063 /* not happening */
1064 break;
Johannes Berg98104fde2012-06-16 00:19:54 +02001065 case NL80211_IFTYPE_P2P_DEVICE:
Ayala Bekercb3b7d82016-09-20 17:31:13 +03001066 case NL80211_IFTYPE_NAN:
Johannes Berg98104fde2012-06-16 00:19:54 +02001067 WARN_ON(1);
1068 break;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001069 }
1070 }
1071
Michal Kaziordbbae262012-06-29 12:47:01 +02001072 if (!err && ntype != otype && netif_running(dev)) {
1073 cfg80211_update_iface_num(rdev, ntype, 1);
1074 cfg80211_update_iface_num(rdev, otype, -1);
1075 }
1076
Johannes Berg3d54d252009-08-21 14:51:05 +02001077 return err;
1078}
John W. Linville254416a2009-12-09 16:43:52 -05001079
Johannes Berg0c1eca42017-02-15 15:02:08 +01001080static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1081{
1082 int modulation, streams, bitrate;
1083
1084 /* the formula below does only work for MCS values smaller than 32 */
1085 if (WARN_ON_ONCE(rate->mcs >= 32))
1086 return 0;
1087
1088 modulation = rate->mcs & 7;
1089 streams = (rate->mcs >> 3) + 1;
1090
1091 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1092
1093 if (modulation < 4)
1094 bitrate *= (modulation + 1);
1095 else if (modulation == 4)
1096 bitrate *= (modulation + 2);
1097 else
1098 bitrate *= (modulation + 3);
1099
1100 bitrate *= streams;
1101
1102 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1103 bitrate = (bitrate / 9) * 10;
1104
1105 /* do NOT round down here */
1106 return (bitrate + 50000) / 100000;
1107}
1108
Alexei Avshalom Lazar2a380752019-08-18 17:35:17 +03001109static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +03001110{
1111 static const u32 __mcs2bitrate[] = {
1112 /* control PHY */
1113 [0] = 275,
1114 /* SC PHY */
1115 [1] = 3850,
1116 [2] = 7700,
1117 [3] = 9625,
1118 [4] = 11550,
1119 [5] = 12512, /* 1251.25 mbps */
1120 [6] = 15400,
1121 [7] = 19250,
1122 [8] = 23100,
1123 [9] = 25025,
1124 [10] = 30800,
1125 [11] = 38500,
1126 [12] = 46200,
1127 /* OFDM PHY */
1128 [13] = 6930,
1129 [14] = 8662, /* 866.25 mbps */
1130 [15] = 13860,
1131 [16] = 17325,
1132 [17] = 20790,
1133 [18] = 27720,
1134 [19] = 34650,
1135 [20] = 41580,
1136 [21] = 45045,
1137 [22] = 51975,
1138 [23] = 62370,
1139 [24] = 67568, /* 6756.75 mbps */
1140 /* LP-SC PHY */
1141 [25] = 6260,
1142 [26] = 8340,
1143 [27] = 11120,
1144 [28] = 12510,
1145 [29] = 16680,
1146 [30] = 22240,
1147 [31] = 25030,
1148 };
1149
1150 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1151 return 0;
1152
1153 return __mcs2bitrate[rate->mcs];
1154}
1155
Alexei Avshalom Lazar2a380752019-08-18 17:35:17 +03001156static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
1157{
1158 static const u32 __mcs2bitrate[] = {
1159 /* control PHY */
1160 [0] = 275,
1161 /* SC PHY */
1162 [1] = 3850,
1163 [2] = 7700,
1164 [3] = 9625,
1165 [4] = 11550,
1166 [5] = 12512, /* 1251.25 mbps */
1167 [6] = 13475,
1168 [7] = 15400,
1169 [8] = 19250,
1170 [9] = 23100,
1171 [10] = 25025,
1172 [11] = 26950,
1173 [12] = 30800,
1174 [13] = 38500,
1175 [14] = 46200,
1176 [15] = 50050,
1177 [16] = 53900,
1178 [17] = 57750,
1179 [18] = 69300,
1180 [19] = 75075,
1181 [20] = 80850,
1182 };
1183
1184 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1185 return 0;
1186
1187 return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
1188}
1189
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001190static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1191{
1192 static const u32 base[4][10] = {
1193 { 6500000,
1194 13000000,
1195 19500000,
1196 26000000,
1197 39000000,
1198 52000000,
1199 58500000,
1200 65000000,
1201 78000000,
Pedersen, Thomas8fdd1362016-10-31 11:28:40 -07001202 /* not in the spec, but some devices use this: */
1203 86500000,
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001204 },
1205 { 13500000,
1206 27000000,
1207 40500000,
1208 54000000,
1209 81000000,
1210 108000000,
1211 121500000,
1212 135000000,
1213 162000000,
1214 180000000,
1215 },
1216 { 29300000,
1217 58500000,
1218 87800000,
1219 117000000,
1220 175500000,
1221 234000000,
1222 263300000,
1223 292500000,
1224 351000000,
1225 390000000,
1226 },
1227 { 58500000,
1228 117000000,
1229 175500000,
1230 234000000,
1231 351000000,
1232 468000000,
1233 526500000,
1234 585000000,
1235 702000000,
1236 780000000,
1237 },
1238 };
1239 u32 bitrate;
1240 int idx;
1241
Johannes Bergca8fe252017-05-04 07:52:10 +02001242 if (rate->mcs > 9)
1243 goto warn;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001244
Johannes Bergb51f3be2015-01-15 16:14:02 +01001245 switch (rate->bw) {
1246 case RATE_INFO_BW_160:
1247 idx = 3;
1248 break;
1249 case RATE_INFO_BW_80:
1250 idx = 2;
1251 break;
1252 case RATE_INFO_BW_40:
1253 idx = 1;
1254 break;
1255 case RATE_INFO_BW_5:
1256 case RATE_INFO_BW_10:
1257 default:
Johannes Bergca8fe252017-05-04 07:52:10 +02001258 goto warn;
Johannes Bergb51f3be2015-01-15 16:14:02 +01001259 case RATE_INFO_BW_20:
1260 idx = 0;
1261 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001262
1263 bitrate = base[idx][rate->mcs];
1264 bitrate *= rate->nss;
1265
1266 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1267 bitrate = (bitrate / 9) * 10;
1268
1269 /* do NOT round down here */
1270 return (bitrate + 50000) / 100000;
Johannes Bergca8fe252017-05-04 07:52:10 +02001271 warn:
1272 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1273 rate->bw, rate->mcs, rate->nss);
1274 return 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001275}
1276
Luca Coelhoc4cbaf72018-06-09 09:14:42 +03001277static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1278{
1279#define SCALE 2048
1280 u16 mcs_divisors[12] = {
1281 34133, /* 16.666666... */
1282 17067, /* 8.333333... */
1283 11378, /* 5.555555... */
1284 8533, /* 4.166666... */
1285 5689, /* 2.777777... */
1286 4267, /* 2.083333... */
1287 3923, /* 1.851851... */
1288 3413, /* 1.666666... */
1289 2844, /* 1.388888... */
1290 2560, /* 1.250000... */
1291 2276, /* 1.111111... */
1292 2048, /* 1.000000... */
1293 };
1294 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1295 u32 rates_969[3] = { 480388888, 453700000, 408333333 };
1296 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1297 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1298 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1299 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1300 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1301 u64 tmp;
1302 u32 result;
1303
1304 if (WARN_ON_ONCE(rate->mcs > 11))
1305 return 0;
1306
1307 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1308 return 0;
1309 if (WARN_ON_ONCE(rate->he_ru_alloc >
1310 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1311 return 0;
1312 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1313 return 0;
1314
1315 if (rate->bw == RATE_INFO_BW_160)
1316 result = rates_160M[rate->he_gi];
1317 else if (rate->bw == RATE_INFO_BW_80 ||
1318 (rate->bw == RATE_INFO_BW_HE_RU &&
1319 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1320 result = rates_969[rate->he_gi];
1321 else if (rate->bw == RATE_INFO_BW_40 ||
1322 (rate->bw == RATE_INFO_BW_HE_RU &&
1323 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1324 result = rates_484[rate->he_gi];
1325 else if (rate->bw == RATE_INFO_BW_20 ||
1326 (rate->bw == RATE_INFO_BW_HE_RU &&
1327 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1328 result = rates_242[rate->he_gi];
1329 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1330 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1331 result = rates_106[rate->he_gi];
1332 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1333 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1334 result = rates_52[rate->he_gi];
1335 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1336 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1337 result = rates_26[rate->he_gi];
Nathan Chancellor344c9712019-03-07 16:57:35 -07001338 else {
1339 WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1340 rate->bw, rate->he_ru_alloc);
Luca Coelhoc4cbaf72018-06-09 09:14:42 +03001341 return 0;
Nathan Chancellor344c9712019-03-07 16:57:35 -07001342 }
Luca Coelhoc4cbaf72018-06-09 09:14:42 +03001343
1344 /* now scale to the appropriate MCS */
1345 tmp = result;
1346 tmp *= SCALE;
1347 do_div(tmp, mcs_divisors[rate->mcs]);
1348 result = tmp;
1349
1350 /* and take NSS, DCM into account */
1351 result = (result * rate->nss) / 8;
1352 if (rate->he_dcm)
1353 result /= 2;
1354
John Crispin25d16d12019-05-23 10:27:24 +02001355 return result / 10000;
Luca Coelhoc4cbaf72018-06-09 09:14:42 +03001356}
1357
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03001358u32 cfg80211_calculate_bitrate(struct rate_info *rate)
John W. Linville254416a2009-12-09 16:43:52 -05001359{
Johannes Berg0c1eca42017-02-15 15:02:08 +01001360 if (rate->flags & RATE_INFO_FLAGS_MCS)
1361 return cfg80211_calculate_bitrate_ht(rate);
Alexei Avshalom Lazar2a380752019-08-18 17:35:17 +03001362 if (rate->flags & RATE_INFO_FLAGS_DMG)
1363 return cfg80211_calculate_bitrate_dmg(rate);
1364 if (rate->flags & RATE_INFO_FLAGS_EDMG)
1365 return cfg80211_calculate_bitrate_edmg(rate);
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001366 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1367 return cfg80211_calculate_bitrate_vht(rate);
Luca Coelhoc4cbaf72018-06-09 09:14:42 +03001368 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1369 return cfg80211_calculate_bitrate_he(rate);
John W. Linville254416a2009-12-09 16:43:52 -05001370
Johannes Berg0c1eca42017-02-15 15:02:08 +01001371 return rate->legacy;
John W. Linville254416a2009-12-09 16:43:52 -05001372}
Thomas Pedersen8097e142012-03-05 15:31:47 -08001373EXPORT_SYMBOL(cfg80211_calculate_bitrate);
Johannes Berg56d18932011-05-09 18:41:15 +02001374
Arend van Sprielc216e642012-11-25 19:13:28 +01001375int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1376 enum ieee80211_p2p_attr_id attr,
1377 u8 *buf, unsigned int bufsize)
Johannes Berg0ee45352012-10-29 19:48:40 +01001378{
1379 u8 *out = buf;
1380 u16 attr_remaining = 0;
1381 bool desired_attr = false;
1382 u16 desired_len = 0;
1383
1384 while (len > 0) {
1385 unsigned int iedatalen;
1386 unsigned int copy;
1387 const u8 *iedata;
1388
1389 if (len < 2)
1390 return -EILSEQ;
1391 iedatalen = ies[1];
1392 if (iedatalen + 2 > len)
1393 return -EILSEQ;
1394
1395 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1396 goto cont;
1397
1398 if (iedatalen < 4)
1399 goto cont;
1400
1401 iedata = ies + 2;
1402
1403 /* check WFA OUI, P2P subtype */
1404 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1405 iedata[2] != 0x9a || iedata[3] != 0x09)
1406 goto cont;
1407
1408 iedatalen -= 4;
1409 iedata += 4;
1410
1411 /* check attribute continuation into this IE */
1412 copy = min_t(unsigned int, attr_remaining, iedatalen);
1413 if (copy && desired_attr) {
1414 desired_len += copy;
1415 if (out) {
1416 memcpy(out, iedata, min(bufsize, copy));
1417 out += min(bufsize, copy);
1418 bufsize -= min(bufsize, copy);
1419 }
1420
1421
1422 if (copy == attr_remaining)
1423 return desired_len;
1424 }
1425
1426 attr_remaining -= copy;
1427 if (attr_remaining)
1428 goto cont;
1429
1430 iedatalen -= copy;
1431 iedata += copy;
1432
1433 while (iedatalen > 0) {
1434 u16 attr_len;
1435
1436 /* P2P attribute ID & size must fit */
1437 if (iedatalen < 3)
1438 return -EILSEQ;
1439 desired_attr = iedata[0] == attr;
1440 attr_len = get_unaligned_le16(iedata + 1);
1441 iedatalen -= 3;
1442 iedata += 3;
1443
1444 copy = min_t(unsigned int, attr_len, iedatalen);
1445
1446 if (desired_attr) {
1447 desired_len += copy;
1448 if (out) {
1449 memcpy(out, iedata, min(bufsize, copy));
1450 out += min(bufsize, copy);
1451 bufsize -= min(bufsize, copy);
1452 }
1453
1454 if (copy == attr_len)
1455 return desired_len;
1456 }
1457
1458 iedata += copy;
1459 iedatalen -= copy;
1460 attr_remaining = attr_len - copy;
1461 }
1462
1463 cont:
1464 len -= ies[1] + 2;
1465 ies += ies[1] + 2;
1466 }
1467
1468 if (attr_remaining && desired_attr)
1469 return -EILSEQ;
1470
1471 return -ENOENT;
1472}
1473EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1474
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001475static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
Johannes Berg29464cc2015-03-31 15:36:22 +02001476{
1477 int i;
1478
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001479 /* Make sure array values are legal */
1480 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1481 return false;
1482
1483 i = 0;
1484 while (i < n_ids) {
1485 if (ids[i] == WLAN_EID_EXTENSION) {
1486 if (id_ext && (ids[i + 1] == id))
1487 return true;
1488
1489 i += 2;
1490 continue;
1491 }
1492
1493 if (ids[i] == id && !id_ext)
Johannes Berg29464cc2015-03-31 15:36:22 +02001494 return true;
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001495
1496 i++;
1497 }
Johannes Berg29464cc2015-03-31 15:36:22 +02001498 return false;
1499}
1500
Johannes Berg8ac63442015-09-04 20:03:23 +02001501static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1502{
1503 /* we assume a validly formed IEs buffer */
1504 u8 len = ies[pos + 1];
1505
1506 pos += 2 + len;
1507
1508 /* the IE itself must have 255 bytes for fragments to follow */
1509 if (len < 255)
1510 return pos;
1511
1512 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1513 len = ies[pos + 1];
1514 pos += 2 + len;
1515 }
1516
1517 return pos;
1518}
1519
Johannes Berg29464cc2015-03-31 15:36:22 +02001520size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1521 const u8 *ids, int n_ids,
1522 const u8 *after_ric, int n_after_ric,
1523 size_t offset)
1524{
1525 size_t pos = offset;
1526
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001527 while (pos < ielen) {
1528 u8 ext = 0;
1529
1530 if (ies[pos] == WLAN_EID_EXTENSION)
1531 ext = 2;
1532 if ((pos + ext) >= ielen)
1533 break;
1534
1535 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1536 ies[pos] == WLAN_EID_EXTENSION))
1537 break;
1538
Johannes Berg29464cc2015-03-31 15:36:22 +02001539 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
Johannes Berg8ac63442015-09-04 20:03:23 +02001540 pos = skip_ie(ies, ielen, pos);
Johannes Berg29464cc2015-03-31 15:36:22 +02001541
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001542 while (pos < ielen) {
1543 if (ies[pos] == WLAN_EID_EXTENSION)
1544 ext = 2;
1545 else
1546 ext = 0;
1547
1548 if ((pos + ext) >= ielen)
1549 break;
1550
1551 if (!ieee80211_id_in_list(after_ric,
1552 n_after_ric,
1553 ies[pos + ext],
1554 ext == 2))
1555 pos = skip_ie(ies, ielen, pos);
Jouni Malinen312ca382018-12-05 12:55:54 +02001556 else
1557 break;
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001558 }
Johannes Berg29464cc2015-03-31 15:36:22 +02001559 } else {
Johannes Berg8ac63442015-09-04 20:03:23 +02001560 pos = skip_ie(ies, ielen, pos);
Johannes Berg29464cc2015-03-31 15:36:22 +02001561 }
1562 }
1563
1564 return pos;
1565}
1566EXPORT_SYMBOL(ieee80211_ie_split_ric);
1567
Johannes Berg1ce3e822012-08-01 17:00:55 +02001568bool ieee80211_operating_class_to_band(u8 operating_class,
Johannes Berg57fbcce2016-04-12 15:56:15 +02001569 enum nl80211_band *band)
Johannes Berg1ce3e822012-08-01 17:00:55 +02001570{
1571 switch (operating_class) {
1572 case 112:
1573 case 115 ... 127:
Eliad Peller954a86e2015-03-01 09:10:01 +02001574 case 128 ... 130:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001575 *band = NL80211_BAND_5GHZ;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001576 return true;
Arend van Spriel852f0462019-08-02 13:31:01 +02001577 case 131 ... 135:
1578 *band = NL80211_BAND_6GHZ;
1579 return true;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001580 case 81:
1581 case 82:
1582 case 83:
1583 case 84:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001584 *band = NL80211_BAND_2GHZ;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001585 return true;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001586 case 180:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001587 *band = NL80211_BAND_60GHZ;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001588 return true;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001589 }
1590
1591 return false;
1592}
1593EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1594
Arik Nemtsova38700d2015-03-18 08:46:08 +02001595bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1596 u8 *op_class)
1597{
1598 u8 vht_opclass;
Dan Carpenter84429382018-08-31 11:10:55 +03001599 u32 freq = chandef->center_freq1;
Arik Nemtsova38700d2015-03-18 08:46:08 +02001600
1601 if (freq >= 2412 && freq <= 2472) {
1602 if (chandef->width > NL80211_CHAN_WIDTH_40)
1603 return false;
1604
1605 /* 2.407 GHz, channels 1..13 */
1606 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1607 if (freq > chandef->chan->center_freq)
1608 *op_class = 83; /* HT40+ */
1609 else
1610 *op_class = 84; /* HT40- */
1611 } else {
1612 *op_class = 81;
1613 }
1614
1615 return true;
1616 }
1617
1618 if (freq == 2484) {
Masashi Honmaec649fe2019-10-21 16:50:45 +09001619 /* channel 14 is only for IEEE 802.11b */
1620 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
Arik Nemtsova38700d2015-03-18 08:46:08 +02001621 return false;
1622
1623 *op_class = 82; /* channel 14 */
1624 return true;
1625 }
1626
1627 switch (chandef->width) {
1628 case NL80211_CHAN_WIDTH_80:
1629 vht_opclass = 128;
1630 break;
1631 case NL80211_CHAN_WIDTH_160:
1632 vht_opclass = 129;
1633 break;
1634 case NL80211_CHAN_WIDTH_80P80:
1635 vht_opclass = 130;
1636 break;
1637 case NL80211_CHAN_WIDTH_10:
1638 case NL80211_CHAN_WIDTH_5:
1639 return false; /* unsupported for now */
1640 default:
1641 vht_opclass = 0;
1642 break;
1643 }
1644
1645 /* 5 GHz, channels 36..48 */
1646 if (freq >= 5180 && freq <= 5240) {
1647 if (vht_opclass) {
1648 *op_class = vht_opclass;
1649 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1650 if (freq > chandef->chan->center_freq)
1651 *op_class = 116;
1652 else
1653 *op_class = 117;
1654 } else {
1655 *op_class = 115;
1656 }
1657
1658 return true;
1659 }
1660
1661 /* 5 GHz, channels 52..64 */
1662 if (freq >= 5260 && freq <= 5320) {
1663 if (vht_opclass) {
1664 *op_class = vht_opclass;
1665 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1666 if (freq > chandef->chan->center_freq)
1667 *op_class = 119;
1668 else
1669 *op_class = 120;
1670 } else {
1671 *op_class = 118;
1672 }
1673
1674 return true;
1675 }
1676
1677 /* 5 GHz, channels 100..144 */
1678 if (freq >= 5500 && freq <= 5720) {
1679 if (vht_opclass) {
1680 *op_class = vht_opclass;
1681 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1682 if (freq > chandef->chan->center_freq)
1683 *op_class = 122;
1684 else
1685 *op_class = 123;
1686 } else {
1687 *op_class = 121;
1688 }
1689
1690 return true;
1691 }
1692
1693 /* 5 GHz, channels 149..169 */
1694 if (freq >= 5745 && freq <= 5845) {
1695 if (vht_opclass) {
1696 *op_class = vht_opclass;
1697 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1698 if (freq > chandef->chan->center_freq)
1699 *op_class = 126;
1700 else
1701 *op_class = 127;
1702 } else if (freq <= 5805) {
1703 *op_class = 124;
1704 } else {
1705 *op_class = 125;
1706 }
1707
1708 return true;
1709 }
1710
1711 /* 56.16 GHz, channel 1..4 */
Alexei Avshalom Lazar9cf0a0b2018-08-13 15:33:00 +03001712 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
Arik Nemtsova38700d2015-03-18 08:46:08 +02001713 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1714 return false;
1715
1716 *op_class = 180;
1717 return true;
1718 }
1719
1720 /* not supported yet */
1721 return false;
1722}
1723EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1724
Johannes Berg4c8dea62016-10-21 14:25:13 +02001725static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1726 u32 *beacon_int_gcd,
1727 bool *beacon_int_different)
1728{
1729 struct wireless_dev *wdev;
1730
1731 *beacon_int_gcd = 0;
1732 *beacon_int_different = false;
1733
1734 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1735 if (!wdev->beacon_interval)
1736 continue;
1737
1738 if (!*beacon_int_gcd) {
1739 *beacon_int_gcd = wdev->beacon_interval;
1740 continue;
1741 }
1742
1743 if (wdev->beacon_interval == *beacon_int_gcd)
1744 continue;
1745
1746 *beacon_int_different = true;
1747 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1748 }
1749
1750 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1751 if (*beacon_int_gcd)
1752 *beacon_int_different = true;
1753 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1754 }
1755}
1756
Johannes Berg56d18932011-05-09 18:41:15 +02001757int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05301758 enum nl80211_iftype iftype, u32 beacon_int)
Johannes Berg56d18932011-05-09 18:41:15 +02001759{
Johannes Berg4c8dea62016-10-21 14:25:13 +02001760 /*
1761 * This is just a basic pre-condition check; if interface combinations
1762 * are possible the driver must already be checking those with a call
1763 * to cfg80211_check_combinations(), in which case we'll validate more
1764 * through the cfg80211_calculate_bi_data() call and code in
1765 * cfg80211_iter_combinations().
1766 */
Johannes Berg56d18932011-05-09 18:41:15 +02001767
Purushottam Kushwaha12d20fc92016-08-11 15:14:02 +05301768 if (beacon_int < 10 || beacon_int > 10000)
Johannes Berg56d18932011-05-09 18:41:15 +02001769 return -EINVAL;
1770
Johannes Berg4c8dea62016-10-21 14:25:13 +02001771 return 0;
Johannes Berg56d18932011-05-09 18:41:15 +02001772}
Johannes Berg7527a782011-05-13 10:58:57 +02001773
Michal Kazior65a124d2014-04-09 15:29:22 +02001774int cfg80211_iter_combinations(struct wiphy *wiphy,
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301775 struct iface_combination_params *params,
Michal Kazior65a124d2014-04-09 15:29:22 +02001776 void (*iter)(const struct ieee80211_iface_combination *c,
1777 void *data),
1778 void *data)
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001779{
Felix Fietkau8c48b502014-05-05 11:48:40 +02001780 const struct ieee80211_regdomain *regdom;
1781 enum nl80211_dfs_regions region = 0;
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001782 int i, j, iftype;
1783 int num_interfaces = 0;
1784 u32 used_iftypes = 0;
Johannes Berg4c8dea62016-10-21 14:25:13 +02001785 u32 beacon_int_gcd;
1786 bool beacon_int_different;
1787
1788 /*
1789 * This is a bit strange, since the iteration used to rely only on
1790 * the data given by the driver, but here it now relies on context,
1791 * in form of the currently operating interfaces.
1792 * This is OK for all current users, and saves us from having to
1793 * push the GCD calculations into all the drivers.
1794 * In the future, this should probably rely more on data that's in
1795 * cfg80211 already - the only thing not would appear to be any new
1796 * interfaces (while being brought up) and channel/radar data.
1797 */
1798 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1799 &beacon_int_gcd, &beacon_int_different);
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001800
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301801 if (params->radar_detect) {
Felix Fietkau8c48b502014-05-05 11:48:40 +02001802 rcu_read_lock();
1803 regdom = rcu_dereference(cfg80211_regdomain);
1804 if (regdom)
1805 region = regdom->dfs_region;
1806 rcu_read_unlock();
1807 }
1808
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001809 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301810 num_interfaces += params->iftype_num[iftype];
1811 if (params->iftype_num[iftype] > 0 &&
Manikanta Pubbisettye6f40512019-07-22 12:44:50 +05301812 !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001813 used_iftypes |= BIT(iftype);
1814 }
1815
1816 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1817 const struct ieee80211_iface_combination *c;
1818 struct ieee80211_iface_limit *limits;
1819 u32 all_iftypes = 0;
1820
1821 c = &wiphy->iface_combinations[i];
1822
1823 if (num_interfaces > c->max_interfaces)
1824 continue;
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301825 if (params->num_different_channels > c->num_different_channels)
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001826 continue;
1827
1828 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1829 GFP_KERNEL);
1830 if (!limits)
1831 return -ENOMEM;
1832
1833 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
Manikanta Pubbisettye6f40512019-07-22 12:44:50 +05301834 if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001835 continue;
1836 for (j = 0; j < c->n_limits; j++) {
1837 all_iftypes |= limits[j].types;
1838 if (!(limits[j].types & BIT(iftype)))
1839 continue;
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301840 if (limits[j].max < params->iftype_num[iftype])
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001841 goto cont;
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301842 limits[j].max -= params->iftype_num[iftype];
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001843 }
1844 }
1845
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301846 if (params->radar_detect !=
1847 (c->radar_detect_widths & params->radar_detect))
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001848 goto cont;
1849
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301850 if (params->radar_detect && c->radar_detect_regions &&
Felix Fietkau8c48b502014-05-05 11:48:40 +02001851 !(c->radar_detect_regions & BIT(region)))
1852 goto cont;
1853
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001854 /* Finally check that all iftypes that we're currently
1855 * using are actually part of this combination. If they
1856 * aren't then we can't use this combination and have
1857 * to continue to the next.
1858 */
1859 if ((all_iftypes & used_iftypes) != used_iftypes)
1860 goto cont;
1861
Johannes Berg4c8dea62016-10-21 14:25:13 +02001862 if (beacon_int_gcd) {
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05301863 if (c->beacon_int_min_gcd &&
Johannes Berg4c8dea62016-10-21 14:25:13 +02001864 beacon_int_gcd < c->beacon_int_min_gcd)
Johannes Berg0507a3a2016-10-21 12:15:00 +02001865 goto cont;
Johannes Berg4c8dea62016-10-21 14:25:13 +02001866 if (!c->beacon_int_min_gcd && beacon_int_different)
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05301867 goto cont;
1868 }
1869
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001870 /* This combination covered all interface types and
1871 * supported the requested numbers, so we're good.
1872 */
Michal Kazior65a124d2014-04-09 15:29:22 +02001873
1874 (*iter)(c, data);
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001875 cont:
1876 kfree(limits);
1877 }
1878
Michal Kazior65a124d2014-04-09 15:29:22 +02001879 return 0;
1880}
1881EXPORT_SYMBOL(cfg80211_iter_combinations);
1882
1883static void
1884cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1885 void *data)
1886{
1887 int *num = data;
1888 (*num)++;
1889}
1890
1891int cfg80211_check_combinations(struct wiphy *wiphy,
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301892 struct iface_combination_params *params)
Michal Kazior65a124d2014-04-09 15:29:22 +02001893{
1894 int err, num = 0;
1895
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301896 err = cfg80211_iter_combinations(wiphy, params,
Michal Kazior65a124d2014-04-09 15:29:22 +02001897 cfg80211_iter_sum_ifcombs, &num);
1898 if (err)
1899 return err;
1900 if (num == 0)
1901 return -EBUSY;
1902
1903 return 0;
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001904}
1905EXPORT_SYMBOL(cfg80211_check_combinations);
1906
Johannes Berg34850ab2011-07-18 18:08:35 +02001907int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1908 const u8 *rates, unsigned int n_rates,
1909 u32 *mask)
1910{
1911 int i, j;
1912
Johannes Berga401d2b2011-07-20 00:52:16 +02001913 if (!sband)
1914 return -EINVAL;
1915
Johannes Berg34850ab2011-07-18 18:08:35 +02001916 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1917 return -EINVAL;
1918
1919 *mask = 0;
1920
1921 for (i = 0; i < n_rates; i++) {
1922 int rate = (rates[i] & 0x7f) * 5;
1923 bool found = false;
1924
1925 for (j = 0; j < sband->n_bitrates; j++) {
1926 if (sband->bitrates[j].bitrate == rate) {
1927 found = true;
1928 *mask |= BIT(j);
1929 break;
1930 }
1931 }
1932 if (!found)
1933 return -EINVAL;
1934 }
1935
1936 /*
1937 * mask must have at least one bit set here since we
1938 * didn't accept a 0-length rates array nor allowed
1939 * entries in the array that didn't exist
1940 */
1941
1942 return 0;
1943}
Johannes Berg11a2a352011-11-21 11:09:22 +01001944
Ilan Peerbdfbec22014-01-09 11:37:23 +02001945unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1946{
Johannes Berg57fbcce2016-04-12 15:56:15 +02001947 enum nl80211_band band;
Ilan Peerbdfbec22014-01-09 11:37:23 +02001948 unsigned int n_channels = 0;
1949
Johannes Berg57fbcce2016-04-12 15:56:15 +02001950 for (band = 0; band < NUM_NL80211_BANDS; band++)
Ilan Peerbdfbec22014-01-09 11:37:23 +02001951 if (wiphy->bands[band])
1952 n_channels += wiphy->bands[band]->n_channels;
1953
1954 return n_channels;
1955}
1956EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1957
Antonio Quartulli74063532014-05-19 21:53:21 +02001958int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1959 struct station_info *sinfo)
1960{
1961 struct cfg80211_registered_device *rdev;
1962 struct wireless_dev *wdev;
1963
1964 wdev = dev->ieee80211_ptr;
1965 if (!wdev)
1966 return -EOPNOTSUPP;
1967
1968 rdev = wiphy_to_rdev(wdev->wiphy);
1969 if (!rdev->ops->get_station)
1970 return -EOPNOTSUPP;
1971
Sven Eckelmann3c12d042018-06-06 10:53:55 +02001972 memset(sinfo, 0, sizeof(*sinfo));
1973
Antonio Quartulli74063532014-05-19 21:53:21 +02001974 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1975}
1976EXPORT_SYMBOL(cfg80211_get_station);
1977
Ayala Bekera442b762016-09-20 17:31:15 +03001978void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1979{
1980 int i;
1981
1982 if (!f)
1983 return;
1984
1985 kfree(f->serv_spec_info);
1986 kfree(f->srf_bf);
1987 kfree(f->srf_macs);
1988 for (i = 0; i < f->num_rx_filters; i++)
1989 kfree(f->rx_filters[i].filter);
1990
1991 for (i = 0; i < f->num_tx_filters; i++)
1992 kfree(f->tx_filters[i].filter);
1993
1994 kfree(f->rx_filters);
1995 kfree(f->tx_filters);
1996 kfree(f);
1997}
1998EXPORT_SYMBOL(cfg80211_free_nan_func);
1999
Rafał Miłecki4787cfa2017-01-04 18:58:30 +01002000bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
2001 u32 center_freq_khz, u32 bw_khz)
2002{
2003 u32 start_freq_khz, end_freq_khz;
2004
2005 start_freq_khz = center_freq_khz - (bw_khz / 2);
2006 end_freq_khz = center_freq_khz + (bw_khz / 2);
2007
2008 if (start_freq_khz >= freq_range->start_freq_khz &&
2009 end_freq_khz <= freq_range->end_freq_khz)
2010 return true;
2011
2012 return false;
2013}
2014
Arend van Spriel8689c052018-05-10 13:50:12 +02002015int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
2016{
Johannes Berg1d211d42018-05-23 14:53:41 +02002017 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
2018 sizeof(*(sinfo->pertid)),
2019 gfp);
Arend van Spriel8689c052018-05-10 13:50:12 +02002020 if (!sinfo->pertid)
2021 return -ENOMEM;
2022
2023 return 0;
2024}
2025EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
2026
Johannes Berg11a2a352011-11-21 11:09:22 +01002027/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
2028/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
2029const unsigned char rfc1042_header[] __aligned(2) =
2030 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2031EXPORT_SYMBOL(rfc1042_header);
2032
2033/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
2034const unsigned char bridge_tunnel_header[] __aligned(2) =
2035 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
2036EXPORT_SYMBOL(bridge_tunnel_header);
Dedy Lansky30ca1aa2018-07-29 14:59:16 +03002037
2038/* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
2039struct iapp_layer2_update {
2040 u8 da[ETH_ALEN]; /* broadcast */
2041 u8 sa[ETH_ALEN]; /* STA addr */
2042 __be16 len; /* 6 */
2043 u8 dsap; /* 0 */
2044 u8 ssap; /* 0 */
2045 u8 control;
2046 u8 xid_info[3];
2047} __packed;
2048
2049void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
2050{
2051 struct iapp_layer2_update *msg;
2052 struct sk_buff *skb;
2053
2054 /* Send Level 2 Update Frame to update forwarding tables in layer 2
2055 * bridge devices */
2056
2057 skb = dev_alloc_skb(sizeof(*msg));
2058 if (!skb)
2059 return;
2060 msg = skb_put(skb, sizeof(*msg));
2061
2062 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
2063 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
2064
2065 eth_broadcast_addr(msg->da);
2066 ether_addr_copy(msg->sa, addr);
2067 msg->len = htons(6);
2068 msg->dsap = 0;
2069 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
2070 msg->control = 0xaf; /* XID response lsb.1111F101.
2071 * F=0 (no poll command; unsolicited frame) */
2072 msg->xid_info[0] = 0x81; /* XID format identifier */
2073 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
2074 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
2075
2076 skb->dev = dev;
2077 skb->protocol = eth_type_trans(skb, dev);
2078 memset(skb->cb, 0, sizeof(skb->cb));
2079 netif_rx_ni(skb);
2080}
2081EXPORT_SYMBOL(cfg80211_send_layer2_update);
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002082
2083int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
2084 enum ieee80211_vht_chanwidth bw,
Johannes Berg9166cc42020-03-26 15:09:32 +02002085 int mcs, bool ext_nss_bw_capable,
2086 unsigned int max_vht_nss)
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002087{
2088 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002089 int ext_nss_bw;
2090 int supp_width;
2091 int i, mcs_encoding;
2092
2093 if (map == 0xffff)
2094 return 0;
2095
Johannes Berg9166cc42020-03-26 15:09:32 +02002096 if (WARN_ON(mcs > 9 || max_vht_nss > 8))
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002097 return 0;
2098 if (mcs <= 7)
2099 mcs_encoding = 0;
2100 else if (mcs == 8)
2101 mcs_encoding = 1;
2102 else
2103 mcs_encoding = 2;
2104
Johannes Berg9166cc42020-03-26 15:09:32 +02002105 if (!max_vht_nss) {
2106 /* find max_vht_nss for the given MCS */
2107 for (i = 7; i >= 0; i--) {
2108 int supp = (map >> (2 * i)) & 3;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002109
Johannes Berg9166cc42020-03-26 15:09:32 +02002110 if (supp == 3)
2111 continue;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002112
Johannes Berg9166cc42020-03-26 15:09:32 +02002113 if (supp >= mcs_encoding) {
2114 max_vht_nss = i + 1;
2115 break;
2116 }
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002117 }
2118 }
2119
2120 if (!(cap->supp_mcs.tx_mcs_map &
2121 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
2122 return max_vht_nss;
2123
2124 ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2125 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2126 supp_width = le32_get_bits(cap->vht_cap_info,
2127 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2128
2129 /* if not capable, treat ext_nss_bw as 0 */
2130 if (!ext_nss_bw_capable)
2131 ext_nss_bw = 0;
2132
2133 /* This is invalid */
2134 if (supp_width == 3)
2135 return 0;
2136
2137 /* This is an invalid combination so pretend nothing is supported */
2138 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2139 return 0;
2140
2141 /*
2142 * Cover all the special cases according to IEEE 802.11-2016
2143 * Table 9-250. All other cases are either factor of 1 or not
2144 * valid/supported.
2145 */
2146 switch (bw) {
2147 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2148 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2149 if ((supp_width == 1 || supp_width == 2) &&
2150 ext_nss_bw == 3)
2151 return 2 * max_vht_nss;
2152 break;
2153 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2154 if (supp_width == 0 &&
2155 (ext_nss_bw == 1 || ext_nss_bw == 2))
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002156 return max_vht_nss / 2;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002157 if (supp_width == 0 &&
2158 ext_nss_bw == 3)
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002159 return (3 * max_vht_nss) / 4;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002160 if (supp_width == 1 &&
2161 ext_nss_bw == 3)
2162 return 2 * max_vht_nss;
2163 break;
2164 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002165 if (supp_width == 0 && ext_nss_bw == 1)
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002166 return 0; /* not possible */
2167 if (supp_width == 0 &&
2168 ext_nss_bw == 2)
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002169 return max_vht_nss / 2;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002170 if (supp_width == 0 &&
2171 ext_nss_bw == 3)
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002172 return (3 * max_vht_nss) / 4;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002173 if (supp_width == 1 &&
2174 ext_nss_bw == 0)
2175 return 0; /* not possible */
2176 if (supp_width == 1 &&
2177 ext_nss_bw == 1)
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002178 return max_vht_nss / 2;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002179 if (supp_width == 1 &&
2180 ext_nss_bw == 2)
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002181 return (3 * max_vht_nss) / 4;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002182 break;
2183 }
2184
2185 /* not covered or invalid combination received */
2186 return max_vht_nss;
2187}
2188EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
Manikanta Pubbisettye6f40512019-07-22 12:44:50 +05302189
2190bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
2191 bool is_4addr, u8 check_swif)
2192
2193{
2194 bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
2195
2196 switch (check_swif) {
2197 case 0:
2198 if (is_vlan && is_4addr)
2199 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2200 return wiphy->interface_modes & BIT(iftype);
2201 case 1:
2202 if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
2203 return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
2204 return wiphy->software_iftypes & BIT(iftype);
2205 default:
2206 break;
2207 }
2208
2209 return false;
2210}
2211EXPORT_SYMBOL(cfg80211_iftype_allowed);