blob: e4b8db5e81ec710db0ee8e779046e04263441e08 [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 Berg1fc9b722019-02-06 13:17:14 +02008 * Copyright (C) 2018-2019 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
Johannes Berg57fbcce2016-04-12 15:56:15 +020075int ieee80211_channel_to_frequency(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)
84 return 2484;
85 else if (chan < 14)
86 return 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)
90 return 4000 + chan * 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090091 else
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030092 return 5000 + chan * 5;
93 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +020094 case NL80211_BAND_60GHZ:
Alexei Avshalom Lazar9cf0a0b2018-08-13 15:33:00 +030095 if (chan < 7)
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030096 return 56160 + chan * 2160;
97 break;
98 default:
99 ;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900100 }
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300101 return 0; /* not supported */
Johannes Berg8318d782008-01-24 19:38:38 +0100102}
103EXPORT_SYMBOL(ieee80211_channel_to_frequency);
104
105int ieee80211_frequency_to_channel(int freq)
106{
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900107 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +0100108 if (freq == 2484)
109 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900110 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +0100111 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900112 else if (freq >= 4910 && freq <= 4980)
113 return (freq - 4000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300114 else if (freq <= 45000) /* DMG band lower limit */
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900115 return (freq - 5000) / 5;
Alexei Avshalom Lazar9cf0a0b2018-08-13 15:33:00 +0300116 else if (freq >= 58320 && freq <= 70200)
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300117 return (freq - 56160) / 2160;
118 else
119 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100120}
121EXPORT_SYMBOL(ieee80211_frequency_to_channel);
122
Arend Van Spriel543b9212016-11-17 12:48:53 +0000123struct ieee80211_channel *ieee80211_get_channel(struct wiphy *wiphy, int freq)
Johannes Berg906c7302008-03-16 18:34:33 +0100124{
Johannes Berg57fbcce2016-04-12 15:56:15 +0200125 enum nl80211_band band;
Johannes Berg906c7302008-03-16 18:34:33 +0100126 struct ieee80211_supported_band *sband;
127 int i;
128
Johannes Berg57fbcce2016-04-12 15:56:15 +0200129 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Berg906c7302008-03-16 18:34:33 +0100130 sband = wiphy->bands[band];
131
132 if (!sband)
133 continue;
134
135 for (i = 0; i < sband->n_channels; i++) {
136 if (sband->channels[i].center_freq == freq)
137 return &sband->channels[i];
138 }
139 }
140
141 return NULL;
142}
Arend Van Spriel543b9212016-11-17 12:48:53 +0000143EXPORT_SYMBOL(ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +0100144
Arend Van Spriel343884c2017-01-04 10:53:37 +0000145static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +0100146{
147 int i, want;
148
Arend Van Spriel343884c2017-01-04 10:53:37 +0000149 switch (sband->band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200150 case NL80211_BAND_5GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100151 want = 3;
152 for (i = 0; i < sband->n_bitrates; i++) {
153 if (sband->bitrates[i].bitrate == 60 ||
154 sband->bitrates[i].bitrate == 120 ||
155 sband->bitrates[i].bitrate == 240) {
156 sband->bitrates[i].flags |=
157 IEEE80211_RATE_MANDATORY_A;
158 want--;
159 }
160 }
161 WARN_ON(want);
162 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200163 case NL80211_BAND_2GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100164 want = 7;
165 for (i = 0; i < sband->n_bitrates; i++) {
Richard Schütz1bd773c2017-09-07 17:47:43 +0200166 switch (sband->bitrates[i].bitrate) {
167 case 10:
168 case 20:
169 case 55:
170 case 110:
Johannes Berg8318d782008-01-24 19:38:38 +0100171 sband->bitrates[i].flags |=
172 IEEE80211_RATE_MANDATORY_B |
173 IEEE80211_RATE_MANDATORY_G;
174 want--;
Richard Schütz1bd773c2017-09-07 17:47:43 +0200175 break;
176 case 60:
177 case 120:
178 case 240:
Johannes Berg8318d782008-01-24 19:38:38 +0100179 sband->bitrates[i].flags |=
180 IEEE80211_RATE_MANDATORY_G;
181 want--;
Richard Schütz1bd773c2017-09-07 17:47:43 +0200182 /* fall through */
183 default:
Johannes Berg8318d782008-01-24 19:38:38 +0100184 sband->bitrates[i].flags |=
185 IEEE80211_RATE_ERP_G;
Richard Schütz1bd773c2017-09-07 17:47:43 +0200186 break;
187 }
Johannes Berg8318d782008-01-24 19:38:38 +0100188 }
Richard Schütz1bd773c2017-09-07 17:47:43 +0200189 WARN_ON(want != 0 && want != 3);
Johannes Berg8318d782008-01-24 19:38:38 +0100190 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200191 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300192 /* check for mandatory HT MCS 1..4 */
193 WARN_ON(!sband->ht_cap.ht_supported);
194 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
195 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200196 case NUM_NL80211_BANDS:
Arend Van Spriel343884c2017-01-04 10:53:37 +0000197 default:
Johannes Berg8318d782008-01-24 19:38:38 +0100198 WARN_ON(1);
199 break;
200 }
201}
202
203void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
204{
Johannes Berg57fbcce2016-04-12 15:56:15 +0200205 enum nl80211_band band;
Johannes Berg8318d782008-01-24 19:38:38 +0100206
Johannes Berg57fbcce2016-04-12 15:56:15 +0200207 for (band = 0; band < NUM_NL80211_BANDS; band++)
Johannes Berg8318d782008-01-24 19:38:38 +0100208 if (wiphy->bands[band])
Arend Van Spriel343884c2017-01-04 10:53:37 +0000209 set_mandatory_flags_band(wiphy->bands[band]);
Johannes Berg8318d782008-01-24 19:38:38 +0100210}
Johannes Berg08645122009-05-11 13:54:58 +0200211
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300212bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
213{
214 int i;
215 for (i = 0; i < wiphy->n_cipher_suites; i++)
216 if (cipher == wiphy->cipher_suites[i])
217 return true;
218 return false;
219}
220
Johannes Bergfffd0932009-07-08 14:22:54 +0200221int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
222 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200223 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200224{
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200225 if (key_idx < 0 || key_idx > 5)
Johannes Berg08645122009-05-11 13:54:58 +0200226 return -EINVAL;
227
Johannes Berge31b8212010-10-05 19:39:30 +0200228 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
229 return -EINVAL;
230
231 if (pairwise && !mac_addr)
232 return -EINVAL;
233
Jouni Malinen37720562015-01-24 19:52:04 +0200234 switch (params->cipher) {
235 case WLAN_CIPHER_SUITE_TKIP:
236 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200237 case WLAN_CIPHER_SUITE_CCMP_256:
238 case WLAN_CIPHER_SUITE_GCMP:
239 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinen37720562015-01-24 19:52:04 +0200240 /* Disallow pairwise keys with non-zero index unless it's WEP
241 * or a vendor specific cipher (because current deployments use
242 * pairwise WEP keys with non-zero indices and for vendor
243 * specific ciphers this should be validated in the driver or
244 * hardware level - but 802.11i clearly specifies to use zero)
245 */
246 if (pairwise && key_idx)
247 return -EINVAL;
248 break;
249 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200250 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
251 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
252 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen37720562015-01-24 19:52:04 +0200253 /* Disallow BIP (group-only) cipher as pairwise cipher */
254 if (pairwise)
255 return -EINVAL;
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200256 if (key_idx < 4)
257 return -EINVAL;
Jouni Malinen37720562015-01-24 19:52:04 +0200258 break;
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200259 case WLAN_CIPHER_SUITE_WEP40:
260 case WLAN_CIPHER_SUITE_WEP104:
261 if (key_idx > 3)
262 return -EINVAL;
Jouni Malinen37720562015-01-24 19:52:04 +0200263 default:
264 break;
265 }
Johannes Berg08645122009-05-11 13:54:58 +0200266
Johannes Berg08645122009-05-11 13:54:58 +0200267 switch (params->cipher) {
268 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200269 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200270 return -EINVAL;
271 break;
272 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200273 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200274 return -EINVAL;
275 break;
276 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200277 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200278 return -EINVAL;
279 break;
Jouni Malinencfcf1682015-01-24 19:52:05 +0200280 case WLAN_CIPHER_SUITE_CCMP_256:
281 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
282 return -EINVAL;
283 break;
284 case WLAN_CIPHER_SUITE_GCMP:
285 if (params->key_len != WLAN_KEY_LEN_GCMP)
286 return -EINVAL;
287 break;
288 case WLAN_CIPHER_SUITE_GCMP_256:
289 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
290 return -EINVAL;
291 break;
Johannes Berg08645122009-05-11 13:54:58 +0200292 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200293 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200294 return -EINVAL;
295 break;
296 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200297 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200298 return -EINVAL;
299 break;
Jouni Malinencfcf1682015-01-24 19:52:05 +0200300 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
301 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
302 return -EINVAL;
303 break;
304 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
305 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
306 return -EINVAL;
307 break;
308 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
309 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
310 return -EINVAL;
311 break;
Johannes Berg08645122009-05-11 13:54:58 +0200312 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300313 /*
314 * We don't know anything about this algorithm,
315 * allow using it -- but the driver must check
316 * all parameters! We still check below whether
317 * or not the driver supports this algorithm,
318 * of course.
319 */
320 break;
Johannes Berg08645122009-05-11 13:54:58 +0200321 }
322
Jouni Malinen9f26a952009-05-15 12:38:32 +0300323 if (params->seq) {
324 switch (params->cipher) {
325 case WLAN_CIPHER_SUITE_WEP40:
326 case WLAN_CIPHER_SUITE_WEP104:
327 /* These ciphers do not use key sequence */
328 return -EINVAL;
329 case WLAN_CIPHER_SUITE_TKIP:
330 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200331 case WLAN_CIPHER_SUITE_CCMP_256:
332 case WLAN_CIPHER_SUITE_GCMP:
333 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinen9f26a952009-05-15 12:38:32 +0300334 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200335 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
336 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
337 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen9f26a952009-05-15 12:38:32 +0300338 if (params->seq_len != 6)
339 return -EINVAL;
340 break;
341 }
342 }
343
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300344 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200345 return -EINVAL;
346
Johannes Berg08645122009-05-11 13:54:58 +0200347 return 0;
348}
Zhu Yie31a16d2009-05-21 21:47:03 +0800349
Johannes Berg633adf12010-08-12 14:49:58 +0200350unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800351{
352 unsigned int hdrlen = 24;
353
354 if (ieee80211_is_data(fc)) {
355 if (ieee80211_has_a4(fc))
356 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200357 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800358 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200359 if (ieee80211_has_order(fc))
360 hdrlen += IEEE80211_HT_CTL_LEN;
361 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800362 goto out;
363 }
364
Fred Choufb142f42015-01-20 10:17:27 +0800365 if (ieee80211_is_mgmt(fc)) {
366 if (ieee80211_has_order(fc))
367 hdrlen += IEEE80211_HT_CTL_LEN;
368 goto out;
369 }
370
Zhu Yie31a16d2009-05-21 21:47:03 +0800371 if (ieee80211_is_ctl(fc)) {
372 /*
373 * ACK and CTS are 10 bytes, all others 16. To see how
374 * to get this condition consider
375 * subtype mask: 0b0000000011110000 (0x00F0)
376 * ACK subtype: 0b0000000011010000 (0x00D0)
377 * CTS subtype: 0b0000000011000000 (0x00C0)
378 * bits that matter: ^^^ (0x00E0)
379 * value of those: 0b0000000011000000 (0x00C0)
380 */
381 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
382 hdrlen = 10;
383 else
384 hdrlen = 16;
385 }
386out:
387 return hdrlen;
388}
389EXPORT_SYMBOL(ieee80211_hdrlen);
390
391unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
392{
393 const struct ieee80211_hdr *hdr =
394 (const struct ieee80211_hdr *)skb->data;
395 unsigned int hdrlen;
396
397 if (unlikely(skb->len < 10))
398 return 0;
399 hdrlen = ieee80211_hdrlen(hdr->frame_control);
400 if (unlikely(hdrlen > skb->len))
401 return 0;
402 return hdrlen;
403}
404EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
405
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100406static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
Zhu Yie31a16d2009-05-21 21:47:03 +0800407{
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100408 int ae = flags & MESH_FLAGS_AE;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200409 /* 802.11-2012, 8.2.4.7.3 */
Zhu Yie31a16d2009-05-21 21:47:03 +0800410 switch (ae) {
Johannes Berg7dd111e2012-10-25 21:51:59 +0200411 default:
Zhu Yie31a16d2009-05-21 21:47:03 +0800412 case 0:
413 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700414 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800415 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700416 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800417 return 18;
Zhu Yie31a16d2009-05-21 21:47:03 +0800418 }
419}
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100420
421unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
422{
423 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
424}
Johannes Berg9b395bc2012-10-26 00:36:40 +0200425EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800426
Johannes Berg7f6990c2016-10-05 15:29:49 +0200427int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
Felix Fietkau24bba072018-02-27 13:03:07 +0100428 const u8 *addr, enum nl80211_iftype iftype,
429 u8 data_offset)
Zhu Yie31a16d2009-05-21 21:47:03 +0800430{
431 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100432 struct {
433 u8 hdr[ETH_ALEN] __aligned(2);
434 __be16 proto;
435 } payload;
436 struct ethhdr tmp;
437 u16 hdrlen;
438 u8 mesh_flags = 0;
Zhu Yie31a16d2009-05-21 21:47:03 +0800439
440 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
441 return -1;
442
Felix Fietkau24bba072018-02-27 13:03:07 +0100443 hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100444 if (skb->len < hdrlen + 8)
445 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800446
447 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
448 * header
449 * IEEE 802.11 address fields:
450 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
451 * 0 0 DA SA BSSID n/a
452 * 0 1 DA BSSID SA n/a
453 * 1 0 BSSID SA DA n/a
454 * 1 1 RA TA DA SA
455 */
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100456 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
457 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
458
459 if (iftype == NL80211_IFTYPE_MESH_POINT)
460 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
Zhu Yie31a16d2009-05-21 21:47:03 +0800461
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700462 mesh_flags &= MESH_FLAGS_AE;
463
Zhu Yie31a16d2009-05-21 21:47:03 +0800464 switch (hdr->frame_control &
465 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
466 case cpu_to_le16(IEEE80211_FCTL_TODS):
467 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200468 iftype != NL80211_IFTYPE_AP_VLAN &&
469 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800470 return -1;
471 break;
472 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
473 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543ee2009-11-10 20:10:05 +0100474 iftype != NL80211_IFTYPE_MESH_POINT &&
475 iftype != NL80211_IFTYPE_AP_VLAN &&
476 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800477 return -1;
478 if (iftype == NL80211_IFTYPE_MESH_POINT) {
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700479 if (mesh_flags == MESH_FLAGS_AE_A4)
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800480 return -1;
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700481 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800482 skb_copy_bits(skb, hdrlen +
483 offsetof(struct ieee80211s_hdr, eaddr1),
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100484 tmp.h_dest, 2 * ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800485 }
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100486 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
Zhu Yie31a16d2009-05-21 21:47:03 +0800487 }
488 break;
489 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700490 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200491 iftype != NL80211_IFTYPE_P2P_CLIENT &&
492 iftype != NL80211_IFTYPE_MESH_POINT) ||
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100493 (is_multicast_ether_addr(tmp.h_dest) &&
494 ether_addr_equal(tmp.h_source, addr)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800495 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700496 if (iftype == NL80211_IFTYPE_MESH_POINT) {
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700497 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800498 return -1;
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700499 if (mesh_flags == MESH_FLAGS_AE_A4)
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800500 skb_copy_bits(skb, hdrlen +
501 offsetof(struct ieee80211s_hdr, eaddr1),
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100502 tmp.h_source, ETH_ALEN);
503 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700504 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800505 break;
506 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300507 if (iftype != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100508 iftype != NL80211_IFTYPE_STATION &&
509 iftype != NL80211_IFTYPE_OCB)
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300510 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800511 break;
512 }
513
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100514 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
515 tmp.h_proto = payload.proto;
Zhu Yie31a16d2009-05-21 21:47:03 +0800516
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100517 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
518 tmp.h_proto != htons(ETH_P_AARP) &&
519 tmp.h_proto != htons(ETH_P_IPX)) ||
520 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800521 /* remove RFC1042 or Bridge-Tunnel encapsulation and
522 * replace EtherType */
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100523 hdrlen += ETH_ALEN + 2;
524 else
Felix Fietkauc0417782016-06-29 10:36:39 +0200525 tmp.h_proto = htons(skb->len - hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800526
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100527 pskb_pull(skb, hdrlen);
528
529 if (!ehdr)
Johannes Bergd58ff352017-06-16 14:29:23 +0200530 ehdr = skb_push(skb, sizeof(struct ethhdr));
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100531 memcpy(ehdr, &tmp, sizeof(tmp));
532
Zhu Yie31a16d2009-05-21 21:47:03 +0800533 return 0;
534}
Johannes Berg7f6990c2016-10-05 15:29:49 +0200535EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800536
Felix Fietkau2b67f942016-02-08 14:34:42 +0100537static void
538__frame_add_frag(struct sk_buff *skb, struct page *page,
539 void *ptr, int len, int size)
540{
541 struct skb_shared_info *sh = skb_shinfo(skb);
542 int page_offset;
543
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700544 page_ref_inc(page);
Felix Fietkau2b67f942016-02-08 14:34:42 +0100545 page_offset = ptr - page_address(page);
546 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
547}
548
549static void
550__ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
551 int offset, int len)
552{
553 struct skb_shared_info *sh = skb_shinfo(skb);
Matthias Kaehlckeaa1702d2017-04-13 10:05:04 -0700554 const skb_frag_t *frag = &sh->frags[0];
Felix Fietkau2b67f942016-02-08 14:34:42 +0100555 struct page *frag_page;
556 void *frag_ptr;
557 int frag_len, frag_size;
558 int head_size = skb->len - skb->data_len;
559 int cur_len;
560
561 frag_page = virt_to_head_page(skb->head);
562 frag_ptr = skb->data;
563 frag_size = head_size;
564
565 while (offset >= frag_size) {
566 offset -= frag_size;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100567 frag_page = skb_frag_page(frag);
568 frag_ptr = skb_frag_address(frag);
569 frag_size = skb_frag_size(frag);
Matthias Kaehlckeaa1702d2017-04-13 10:05:04 -0700570 frag++;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100571 }
572
573 frag_ptr += offset;
574 frag_len = frag_size - offset;
575
576 cur_len = min(len, frag_len);
577
578 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
579 len -= cur_len;
580
581 while (len > 0) {
Felix Fietkau2b67f942016-02-08 14:34:42 +0100582 frag_len = skb_frag_size(frag);
583 cur_len = min(len, frag_len);
584 __frame_add_frag(frame, skb_frag_page(frag),
585 skb_frag_address(frag), cur_len, frag_len);
586 len -= cur_len;
Matthias Kaehlckeaa1702d2017-04-13 10:05:04 -0700587 frag++;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100588 }
589}
590
Felix Fietkau230fd282016-02-02 14:39:10 +0100591static struct sk_buff *
592__ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
Felix Fietkau2b67f942016-02-08 14:34:42 +0100593 int offset, int len, bool reuse_frag)
Felix Fietkau230fd282016-02-02 14:39:10 +0100594{
595 struct sk_buff *frame;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100596 int cur_len = len;
Felix Fietkau230fd282016-02-02 14:39:10 +0100597
598 if (skb->len - offset < len)
599 return NULL;
600
601 /*
Felix Fietkau2b67f942016-02-08 14:34:42 +0100602 * When reusing framents, copy some data to the head to simplify
603 * ethernet header handling and speed up protocol header processing
604 * in the stack later.
605 */
606 if (reuse_frag)
607 cur_len = min_t(int, len, 32);
608
609 /*
Felix Fietkau230fd282016-02-02 14:39:10 +0100610 * Allocate and reserve two bytes more for payload
611 * alignment since sizeof(struct ethhdr) is 14.
612 */
Felix Fietkau2b67f942016-02-08 14:34:42 +0100613 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
Gregory Greenman16a910a2016-07-05 15:23:10 +0300614 if (!frame)
615 return NULL;
Felix Fietkau230fd282016-02-02 14:39:10 +0100616
617 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
Felix Fietkau2b67f942016-02-08 14:34:42 +0100618 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
619
620 len -= cur_len;
621 if (!len)
622 return frame;
623
624 offset += cur_len;
625 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
Felix Fietkau230fd282016-02-02 14:39:10 +0100626
627 return frame;
628}
Zhu Yieaf85ca2009-12-01 10:18:37 +0800629
630void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
631 const u8 *addr, enum nl80211_iftype iftype,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700632 const unsigned int extra_headroom,
Johannes Berg8b935ee2016-10-05 16:17:01 +0200633 const u8 *check_da, const u8 *check_sa)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800634{
Felix Fietkau230fd282016-02-02 14:39:10 +0100635 unsigned int hlen = ALIGN(extra_headroom, 4);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800636 struct sk_buff *frame = NULL;
637 u16 ethertype;
638 u8 *payload;
Johannes Berg7f6990c2016-10-05 15:29:49 +0200639 int offset = 0, remaining;
Felix Fietkau230fd282016-02-02 14:39:10 +0100640 struct ethhdr eth;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100641 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
Felix Fietkau2bf0ccc2016-02-08 14:25:26 +0100642 bool reuse_skb = false;
Felix Fietkau230fd282016-02-02 14:39:10 +0100643 bool last = false;
Felix Fietkau88665f52016-02-02 14:39:08 +0100644
Felix Fietkau230fd282016-02-02 14:39:10 +0100645 while (!last) {
646 unsigned int subframe_len;
647 int len;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800648 u8 padding;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800649
Felix Fietkau230fd282016-02-02 14:39:10 +0100650 skb_copy_bits(skb, offset, &eth, sizeof(eth));
651 len = ntohs(eth.h_proto);
652 subframe_len = sizeof(struct ethhdr) + len;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800653 padding = (4 - subframe_len) & 0x3;
Felix Fietkau230fd282016-02-02 14:39:10 +0100654
Zhu Yieaf85ca2009-12-01 10:18:37 +0800655 /* the last MSDU has no padding */
Felix Fietkau230fd282016-02-02 14:39:10 +0100656 remaining = skb->len - offset;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800657 if (subframe_len > remaining)
658 goto purge;
659
Felix Fietkau230fd282016-02-02 14:39:10 +0100660 offset += sizeof(struct ethhdr);
Felix Fietkau230fd282016-02-02 14:39:10 +0100661 last = remaining <= subframe_len + padding;
Johannes Berg8b935ee2016-10-05 16:17:01 +0200662
663 /* FIXME: should we really accept multicast DA? */
664 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
665 !ether_addr_equal(check_da, eth.h_dest)) ||
666 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
667 offset += len + padding;
668 continue;
669 }
670
671 /* reuse skb for the last subframe */
Felix Fietkau2b67f942016-02-08 14:34:42 +0100672 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
Felix Fietkau230fd282016-02-02 14:39:10 +0100673 skb_pull(skb, offset);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800674 frame = skb;
Felix Fietkau230fd282016-02-02 14:39:10 +0100675 reuse_skb = true;
676 } else {
Felix Fietkau2b67f942016-02-08 14:34:42 +0100677 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
678 reuse_frag);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800679 if (!frame)
680 goto purge;
681
Felix Fietkau230fd282016-02-02 14:39:10 +0100682 offset += len + padding;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800683 }
684
685 skb_reset_network_header(frame);
686 frame->dev = skb->dev;
687 frame->priority = skb->priority;
688
689 payload = frame->data;
690 ethertype = (payload[6] << 8) | payload[7];
Joe Perchesac422d32012-05-08 18:56:55 +0000691 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yieaf85ca2009-12-01 10:18:37 +0800692 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perchesac422d32012-05-08 18:56:55 +0000693 ether_addr_equal(payload, bridge_tunnel_header))) {
Felix Fietkau230fd282016-02-02 14:39:10 +0100694 eth.h_proto = htons(ethertype);
695 skb_pull(frame, ETH_ALEN + 2);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800696 }
Felix Fietkau230fd282016-02-02 14:39:10 +0100697
698 memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
Zhu Yieaf85ca2009-12-01 10:18:37 +0800699 __skb_queue_tail(list, frame);
700 }
701
Felix Fietkau230fd282016-02-02 14:39:10 +0100702 if (!reuse_skb)
703 dev_kfree_skb(skb);
704
Zhu Yieaf85ca2009-12-01 10:18:37 +0800705 return;
706
707 purge:
708 __skb_queue_purge(list);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800709 dev_kfree_skb(skb);
710}
711EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
712
Zhu Yie31a16d2009-05-21 21:47:03 +0800713/* Given a data frame determine the 802.1p/1d tag to use. */
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800714unsigned int cfg80211_classify8021d(struct sk_buff *skb,
715 struct cfg80211_qos_map *qos_map)
Zhu Yie31a16d2009-05-21 21:47:03 +0800716{
717 unsigned int dscp;
cedric Vonckenc6ca5e22013-08-26 14:04:52 +0200718 unsigned char vlan_priority;
Johannes Berg1fc9b722019-02-06 13:17:14 +0200719 unsigned int ret;
Zhu Yie31a16d2009-05-21 21:47:03 +0800720
721 /* skb->priority values from 256->263 are magic values to
722 * directly indicate a specific 802.1d priority. This is used
723 * to allow 802.1d priority to be passed directly in from VLAN
724 * tags, etc.
725 */
Johannes Berg1fc9b722019-02-06 13:17:14 +0200726 if (skb->priority >= 256 && skb->priority <= 263) {
727 ret = skb->priority - 256;
728 goto out;
729 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800730
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100731 if (skb_vlan_tag_present(skb)) {
732 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
cedric Vonckenc6ca5e22013-08-26 14:04:52 +0200733 >> VLAN_PRIO_SHIFT;
Johannes Berg1fc9b722019-02-06 13:17:14 +0200734 if (vlan_priority > 0) {
735 ret = vlan_priority;
736 goto out;
737 }
cedric Vonckenc6ca5e22013-08-26 14:04:52 +0200738 }
739
Zhu Yie31a16d2009-05-21 21:47:03 +0800740 switch (skb->protocol) {
741 case htons(ETH_P_IP):
Dave Tähtb1565792011-12-22 12:55:08 -0800742 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
743 break;
744 case htons(ETH_P_IPV6):
745 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
Zhu Yie31a16d2009-05-21 21:47:03 +0800746 break;
Simon Wunderlich960d97f2014-03-03 17:23:12 +0100747 case htons(ETH_P_MPLS_UC):
748 case htons(ETH_P_MPLS_MC): {
749 struct mpls_label mpls_tmp, *mpls;
750
751 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
752 sizeof(*mpls), &mpls_tmp);
753 if (!mpls)
754 return 0;
755
Johannes Berg1fc9b722019-02-06 13:17:14 +0200756 ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
Simon Wunderlich960d97f2014-03-03 17:23:12 +0100757 >> MPLS_LS_TC_SHIFT;
Johannes Berg1fc9b722019-02-06 13:17:14 +0200758 goto out;
Simon Wunderlich960d97f2014-03-03 17:23:12 +0100759 }
760 case htons(ETH_P_80221):
761 /* 802.21 is always network control traffic */
762 return 7;
Zhu Yie31a16d2009-05-21 21:47:03 +0800763 default:
764 return 0;
765 }
766
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800767 if (qos_map) {
768 unsigned int i, tmp_dscp = dscp >> 2;
769
770 for (i = 0; i < qos_map->num_des; i++) {
Johannes Berg1fc9b722019-02-06 13:17:14 +0200771 if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
772 ret = qos_map->dscp_exception[i].up;
773 goto out;
774 }
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800775 }
776
777 for (i = 0; i < 8; i++) {
778 if (tmp_dscp >= qos_map->up[i].low &&
Johannes Berg1fc9b722019-02-06 13:17:14 +0200779 tmp_dscp <= qos_map->up[i].high) {
780 ret = i;
781 goto out;
782 }
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800783 }
784 }
785
Johannes Berg1fc9b722019-02-06 13:17:14 +0200786 ret = dscp >> 5;
787out:
788 return array_index_nospec(ret, IEEE80211_NUM_TIDS);
Zhu Yie31a16d2009-05-21 21:47:03 +0800789}
790EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200791
Johannes Berg49a68e02019-02-07 23:26:38 +0100792const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
Johannes Berg517357c2009-07-02 17:18:40 +0200793{
Johannes Berg9caf0362012-11-29 01:25:20 +0100794 const struct cfg80211_bss_ies *ies;
795
796 ies = rcu_dereference(bss->ies);
797 if (!ies)
Johannes Berg517357c2009-07-02 17:18:40 +0200798 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +0100799
Johannes Berg49a68e02019-02-07 23:26:38 +0100800 return cfg80211_find_elem(id, ies->data, ies->len);
Johannes Berg517357c2009-07-02 17:18:40 +0200801}
Johannes Berg49a68e02019-02-07 23:26:38 +0100802EXPORT_SYMBOL(ieee80211_bss_get_elem);
Johannes Bergfffd0932009-07-08 14:22:54 +0200803
804void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
805{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800806 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200807 struct net_device *dev = wdev->netdev;
808 int i;
809
810 if (!wdev->connect_keys)
811 return;
812
David Spinadelb8676222016-09-22 23:16:50 +0300813 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200814 if (!wdev->connect_keys->params[i].cipher)
815 continue;
Hila Gonene35e4d22012-06-27 17:19:42 +0300816 if (rdev_add_key(rdev, dev, i, false, NULL,
817 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800818 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800819 continue;
820 }
Johannes Bergd4f29972017-02-13 20:53:38 +0100821 if (wdev->connect_keys->def == i &&
822 rdev_set_default_key(rdev, dev, i, true, true)) {
823 netdev_err(dev, "failed to set defkey %d\n", i);
824 continue;
825 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200826 }
827
Johannes Bergb47f6102014-09-10 13:39:54 +0300828 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200829 wdev->connect_keys = NULL;
830}
Johannes Berg3d54d252009-08-21 14:51:05 +0200831
Daniel Drake1f6fc432012-08-02 18:41:48 +0100832void cfg80211_process_wdev_events(struct wireless_dev *wdev)
Johannes Berg3d54d252009-08-21 14:51:05 +0200833{
834 struct cfg80211_event *ev;
835 unsigned long flags;
Johannes Berg3d54d252009-08-21 14:51:05 +0200836
837 spin_lock_irqsave(&wdev->event_lock, flags);
838 while (!list_empty(&wdev->event_list)) {
839 ev = list_first_entry(&wdev->event_list,
840 struct cfg80211_event, list);
841 list_del(&ev->list);
842 spin_unlock_irqrestore(&wdev->event_lock, flags);
843
844 wdev_lock(wdev);
845 switch (ev->type) {
846 case EVENT_CONNECT_RESULT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200847 __cfg80211_connect_result(
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300848 wdev->netdev,
849 &ev->cr,
850 ev->cr.status == WLAN_STATUS_SUCCESS);
Johannes Berg3d54d252009-08-21 14:51:05 +0200851 break;
852 case EVENT_ROAMED:
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300853 __cfg80211_roamed(wdev, &ev->rm);
Johannes Berg3d54d252009-08-21 14:51:05 +0200854 break;
855 case EVENT_DISCONNECTED:
856 __cfg80211_disconnected(wdev->netdev,
857 ev->dc.ie, ev->dc.ie_len,
Johannes Berg80279fb2015-05-22 16:22:20 +0200858 ev->dc.reason,
859 !ev->dc.locally_generated);
Johannes Berg3d54d252009-08-21 14:51:05 +0200860 break;
861 case EVENT_IBSS_JOINED:
Antonio Quartullife94f3a2014-01-29 17:53:43 +0100862 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
863 ev->ij.channel);
Johannes Berg3d54d252009-08-21 14:51:05 +0200864 break;
Michal Kaziorf04c2202014-04-09 15:11:01 +0200865 case EVENT_STOPPED:
866 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
867 break;
Avraham Stern503c1fb2017-09-29 14:21:49 +0200868 case EVENT_PORT_AUTHORIZED:
869 __cfg80211_port_authorized(wdev, ev->pa.bssid);
870 break;
Johannes Berg3d54d252009-08-21 14:51:05 +0200871 }
872 wdev_unlock(wdev);
873
874 kfree(ev);
875
876 spin_lock_irqsave(&wdev->event_lock, flags);
877 }
878 spin_unlock_irqrestore(&wdev->event_lock, flags);
879}
880
881void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
882{
883 struct wireless_dev *wdev;
884
885 ASSERT_RTNL();
Johannes Berg3d54d252009-08-21 14:51:05 +0200886
Johannes Berg53873f12016-05-03 16:52:04 +0300887 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
Johannes Berg3d54d252009-08-21 14:51:05 +0200888 cfg80211_process_wdev_events(wdev);
Johannes Berg3d54d252009-08-21 14:51:05 +0200889}
890
891int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
892 struct net_device *dev, enum nl80211_iftype ntype,
Johannes Berg818a9862017-04-12 11:23:28 +0200893 struct vif_params *params)
Johannes Berg3d54d252009-08-21 14:51:05 +0200894{
895 int err;
896 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
897
Zhao, Gang73fb08e2014-03-19 17:04:36 +0800898 ASSERT_RTNL();
Johannes Berg3d54d252009-08-21 14:51:05 +0200899
900 /* don't support changing VLANs, you just re-create them */
901 if (otype == NL80211_IFTYPE_AP_VLAN)
902 return -EOPNOTSUPP;
903
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300904 /* cannot change into P2P device or NAN */
905 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
906 ntype == NL80211_IFTYPE_NAN)
Johannes Berg98104fde2012-06-16 00:19:54 +0200907 return -EOPNOTSUPP;
908
Johannes Berg3d54d252009-08-21 14:51:05 +0200909 if (!rdev->ops->change_virtual_intf ||
910 !(rdev->wiphy.interface_modes & (1 << ntype)))
911 return -EOPNOTSUPP;
912
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100913 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000914 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200915 (ntype == NL80211_IFTYPE_ADHOC ||
916 ntype == NL80211_IFTYPE_STATION ||
917 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100918 return -EBUSY;
919
Michal Kazior6cbfb1b2015-05-22 10:57:22 +0200920 if (ntype != otype) {
Johannes Berg9bc383d2009-11-19 11:55:19 +0100921 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100922 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg194ff522013-12-30 23:12:37 +0100923 wdev_lock(dev->ieee80211_ptr);
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800924 rdev_set_qos_map(rdev, dev, NULL);
Johannes Berg194ff522013-12-30 23:12:37 +0100925 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg9bc383d2009-11-19 11:55:19 +0100926
Johannes Berg3d54d252009-08-21 14:51:05 +0200927 switch (otype) {
Michal Kaziorac800142012-06-29 12:46:57 +0200928 case NL80211_IFTYPE_AP:
Ilan Peer7c8d5e02014-02-25 15:33:38 +0200929 cfg80211_stop_ap(rdev, dev, true);
Michal Kaziorac800142012-06-29 12:46:57 +0200930 break;
Johannes Berg3d54d252009-08-21 14:51:05 +0200931 case NL80211_IFTYPE_ADHOC:
932 cfg80211_leave_ibss(rdev, dev, false);
933 break;
934 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200935 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg83739b02013-05-15 17:44:01 +0200936 wdev_lock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +0200937 cfg80211_disconnect(rdev, dev,
938 WLAN_REASON_DEAUTH_LEAVING, true);
Johannes Berg83739b02013-05-15 17:44:01 +0200939 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +0200940 break;
941 case NL80211_IFTYPE_MESH_POINT:
942 /* mesh should be handled? */
943 break;
944 default:
945 break;
946 }
947
948 cfg80211_process_rdev_events(rdev);
949 }
950
Johannes Berg818a9862017-04-12 11:23:28 +0200951 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
Johannes Berg3d54d252009-08-21 14:51:05 +0200952
953 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
954
Johannes Berg9bc383d2009-11-19 11:55:19 +0100955 if (!err && params && params->use_4addr != -1)
956 dev->ieee80211_ptr->use_4addr = params->use_4addr;
957
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100958 if (!err) {
959 dev->priv_flags &= ~IFF_DONT_BRIDGE;
960 switch (ntype) {
961 case NL80211_IFTYPE_STATION:
962 if (dev->ieee80211_ptr->use_4addr)
963 break;
964 /* fall through */
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100965 case NL80211_IFTYPE_OCB:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200966 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100967 case NL80211_IFTYPE_ADHOC:
968 dev->priv_flags |= IFF_DONT_BRIDGE;
969 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200970 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100971 case NL80211_IFTYPE_AP:
972 case NL80211_IFTYPE_AP_VLAN:
973 case NL80211_IFTYPE_WDS:
974 case NL80211_IFTYPE_MESH_POINT:
975 /* bridging OK */
976 break;
977 case NL80211_IFTYPE_MONITOR:
978 /* monitor can't bridge anyway */
979 break;
980 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f782010-08-12 15:38:38 +0200981 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100982 /* not happening */
983 break;
Johannes Berg98104fde2012-06-16 00:19:54 +0200984 case NL80211_IFTYPE_P2P_DEVICE:
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300985 case NL80211_IFTYPE_NAN:
Johannes Berg98104fde2012-06-16 00:19:54 +0200986 WARN_ON(1);
987 break;
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100988 }
989 }
990
Michal Kaziordbbae262012-06-29 12:47:01 +0200991 if (!err && ntype != otype && netif_running(dev)) {
992 cfg80211_update_iface_num(rdev, ntype, 1);
993 cfg80211_update_iface_num(rdev, otype, -1);
994 }
995
Johannes Berg3d54d252009-08-21 14:51:05 +0200996 return err;
997}
John W. Linville254416a2009-12-09 16:43:52 -0500998
Johannes Berg0c1eca42017-02-15 15:02:08 +0100999static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1000{
1001 int modulation, streams, bitrate;
1002
1003 /* the formula below does only work for MCS values smaller than 32 */
1004 if (WARN_ON_ONCE(rate->mcs >= 32))
1005 return 0;
1006
1007 modulation = rate->mcs & 7;
1008 streams = (rate->mcs >> 3) + 1;
1009
1010 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1011
1012 if (modulation < 4)
1013 bitrate *= (modulation + 1);
1014 else if (modulation == 4)
1015 bitrate *= (modulation + 2);
1016 else
1017 bitrate *= (modulation + 3);
1018
1019 bitrate *= streams;
1020
1021 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1022 bitrate = (bitrate / 9) * 10;
1023
1024 /* do NOT round down here */
1025 return (bitrate + 50000) / 100000;
1026}
1027
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +03001028static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
1029{
1030 static const u32 __mcs2bitrate[] = {
1031 /* control PHY */
1032 [0] = 275,
1033 /* SC PHY */
1034 [1] = 3850,
1035 [2] = 7700,
1036 [3] = 9625,
1037 [4] = 11550,
1038 [5] = 12512, /* 1251.25 mbps */
1039 [6] = 15400,
1040 [7] = 19250,
1041 [8] = 23100,
1042 [9] = 25025,
1043 [10] = 30800,
1044 [11] = 38500,
1045 [12] = 46200,
1046 /* OFDM PHY */
1047 [13] = 6930,
1048 [14] = 8662, /* 866.25 mbps */
1049 [15] = 13860,
1050 [16] = 17325,
1051 [17] = 20790,
1052 [18] = 27720,
1053 [19] = 34650,
1054 [20] = 41580,
1055 [21] = 45045,
1056 [22] = 51975,
1057 [23] = 62370,
1058 [24] = 67568, /* 6756.75 mbps */
1059 /* LP-SC PHY */
1060 [25] = 6260,
1061 [26] = 8340,
1062 [27] = 11120,
1063 [28] = 12510,
1064 [29] = 16680,
1065 [30] = 22240,
1066 [31] = 25030,
1067 };
1068
1069 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1070 return 0;
1071
1072 return __mcs2bitrate[rate->mcs];
1073}
1074
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001075static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1076{
1077 static const u32 base[4][10] = {
1078 { 6500000,
1079 13000000,
1080 19500000,
1081 26000000,
1082 39000000,
1083 52000000,
1084 58500000,
1085 65000000,
1086 78000000,
Pedersen, Thomas8fdd1362016-10-31 11:28:40 -07001087 /* not in the spec, but some devices use this: */
1088 86500000,
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001089 },
1090 { 13500000,
1091 27000000,
1092 40500000,
1093 54000000,
1094 81000000,
1095 108000000,
1096 121500000,
1097 135000000,
1098 162000000,
1099 180000000,
1100 },
1101 { 29300000,
1102 58500000,
1103 87800000,
1104 117000000,
1105 175500000,
1106 234000000,
1107 263300000,
1108 292500000,
1109 351000000,
1110 390000000,
1111 },
1112 { 58500000,
1113 117000000,
1114 175500000,
1115 234000000,
1116 351000000,
1117 468000000,
1118 526500000,
1119 585000000,
1120 702000000,
1121 780000000,
1122 },
1123 };
1124 u32 bitrate;
1125 int idx;
1126
Johannes Bergca8fe252017-05-04 07:52:10 +02001127 if (rate->mcs > 9)
1128 goto warn;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001129
Johannes Bergb51f3be2015-01-15 16:14:02 +01001130 switch (rate->bw) {
1131 case RATE_INFO_BW_160:
1132 idx = 3;
1133 break;
1134 case RATE_INFO_BW_80:
1135 idx = 2;
1136 break;
1137 case RATE_INFO_BW_40:
1138 idx = 1;
1139 break;
1140 case RATE_INFO_BW_5:
1141 case RATE_INFO_BW_10:
1142 default:
Johannes Bergca8fe252017-05-04 07:52:10 +02001143 goto warn;
Johannes Bergb51f3be2015-01-15 16:14:02 +01001144 case RATE_INFO_BW_20:
1145 idx = 0;
1146 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001147
1148 bitrate = base[idx][rate->mcs];
1149 bitrate *= rate->nss;
1150
1151 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1152 bitrate = (bitrate / 9) * 10;
1153
1154 /* do NOT round down here */
1155 return (bitrate + 50000) / 100000;
Johannes Bergca8fe252017-05-04 07:52:10 +02001156 warn:
1157 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1158 rate->bw, rate->mcs, rate->nss);
1159 return 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001160}
1161
Luca Coelhoc4cbaf72018-06-09 09:14:42 +03001162static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
1163{
1164#define SCALE 2048
1165 u16 mcs_divisors[12] = {
1166 34133, /* 16.666666... */
1167 17067, /* 8.333333... */
1168 11378, /* 5.555555... */
1169 8533, /* 4.166666... */
1170 5689, /* 2.777777... */
1171 4267, /* 2.083333... */
1172 3923, /* 1.851851... */
1173 3413, /* 1.666666... */
1174 2844, /* 1.388888... */
1175 2560, /* 1.250000... */
1176 2276, /* 1.111111... */
1177 2048, /* 1.000000... */
1178 };
1179 u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
1180 u32 rates_969[3] = { 480388888, 453700000, 408333333 };
1181 u32 rates_484[3] = { 229411111, 216666666, 195000000 };
1182 u32 rates_242[3] = { 114711111, 108333333, 97500000 };
1183 u32 rates_106[3] = { 40000000, 37777777, 34000000 };
1184 u32 rates_52[3] = { 18820000, 17777777, 16000000 };
1185 u32 rates_26[3] = { 9411111, 8888888, 8000000 };
1186 u64 tmp;
1187 u32 result;
1188
1189 if (WARN_ON_ONCE(rate->mcs > 11))
1190 return 0;
1191
1192 if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
1193 return 0;
1194 if (WARN_ON_ONCE(rate->he_ru_alloc >
1195 NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
1196 return 0;
1197 if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
1198 return 0;
1199
1200 if (rate->bw == RATE_INFO_BW_160)
1201 result = rates_160M[rate->he_gi];
1202 else if (rate->bw == RATE_INFO_BW_80 ||
1203 (rate->bw == RATE_INFO_BW_HE_RU &&
1204 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
1205 result = rates_969[rate->he_gi];
1206 else if (rate->bw == RATE_INFO_BW_40 ||
1207 (rate->bw == RATE_INFO_BW_HE_RU &&
1208 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
1209 result = rates_484[rate->he_gi];
1210 else if (rate->bw == RATE_INFO_BW_20 ||
1211 (rate->bw == RATE_INFO_BW_HE_RU &&
1212 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
1213 result = rates_242[rate->he_gi];
1214 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1215 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
1216 result = rates_106[rate->he_gi];
1217 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1218 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
1219 result = rates_52[rate->he_gi];
1220 else if (rate->bw == RATE_INFO_BW_HE_RU &&
1221 rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
1222 result = rates_26[rate->he_gi];
1223 else if (WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
1224 rate->bw, rate->he_ru_alloc))
1225 return 0;
1226
1227 /* now scale to the appropriate MCS */
1228 tmp = result;
1229 tmp *= SCALE;
1230 do_div(tmp, mcs_divisors[rate->mcs]);
1231 result = tmp;
1232
1233 /* and take NSS, DCM into account */
1234 result = (result * rate->nss) / 8;
1235 if (rate->he_dcm)
1236 result /= 2;
1237
1238 return result;
1239}
1240
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03001241u32 cfg80211_calculate_bitrate(struct rate_info *rate)
John W. Linville254416a2009-12-09 16:43:52 -05001242{
Johannes Berg0c1eca42017-02-15 15:02:08 +01001243 if (rate->flags & RATE_INFO_FLAGS_MCS)
1244 return cfg80211_calculate_bitrate_ht(rate);
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +03001245 if (rate->flags & RATE_INFO_FLAGS_60G)
1246 return cfg80211_calculate_bitrate_60g(rate);
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001247 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1248 return cfg80211_calculate_bitrate_vht(rate);
Luca Coelhoc4cbaf72018-06-09 09:14:42 +03001249 if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
1250 return cfg80211_calculate_bitrate_he(rate);
John W. Linville254416a2009-12-09 16:43:52 -05001251
Johannes Berg0c1eca42017-02-15 15:02:08 +01001252 return rate->legacy;
John W. Linville254416a2009-12-09 16:43:52 -05001253}
Thomas Pedersen8097e142012-03-05 15:31:47 -08001254EXPORT_SYMBOL(cfg80211_calculate_bitrate);
Johannes Berg56d18932011-05-09 18:41:15 +02001255
Arend van Sprielc216e642012-11-25 19:13:28 +01001256int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1257 enum ieee80211_p2p_attr_id attr,
1258 u8 *buf, unsigned int bufsize)
Johannes Berg0ee45352012-10-29 19:48:40 +01001259{
1260 u8 *out = buf;
1261 u16 attr_remaining = 0;
1262 bool desired_attr = false;
1263 u16 desired_len = 0;
1264
1265 while (len > 0) {
1266 unsigned int iedatalen;
1267 unsigned int copy;
1268 const u8 *iedata;
1269
1270 if (len < 2)
1271 return -EILSEQ;
1272 iedatalen = ies[1];
1273 if (iedatalen + 2 > len)
1274 return -EILSEQ;
1275
1276 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1277 goto cont;
1278
1279 if (iedatalen < 4)
1280 goto cont;
1281
1282 iedata = ies + 2;
1283
1284 /* check WFA OUI, P2P subtype */
1285 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1286 iedata[2] != 0x9a || iedata[3] != 0x09)
1287 goto cont;
1288
1289 iedatalen -= 4;
1290 iedata += 4;
1291
1292 /* check attribute continuation into this IE */
1293 copy = min_t(unsigned int, attr_remaining, iedatalen);
1294 if (copy && desired_attr) {
1295 desired_len += copy;
1296 if (out) {
1297 memcpy(out, iedata, min(bufsize, copy));
1298 out += min(bufsize, copy);
1299 bufsize -= min(bufsize, copy);
1300 }
1301
1302
1303 if (copy == attr_remaining)
1304 return desired_len;
1305 }
1306
1307 attr_remaining -= copy;
1308 if (attr_remaining)
1309 goto cont;
1310
1311 iedatalen -= copy;
1312 iedata += copy;
1313
1314 while (iedatalen > 0) {
1315 u16 attr_len;
1316
1317 /* P2P attribute ID & size must fit */
1318 if (iedatalen < 3)
1319 return -EILSEQ;
1320 desired_attr = iedata[0] == attr;
1321 attr_len = get_unaligned_le16(iedata + 1);
1322 iedatalen -= 3;
1323 iedata += 3;
1324
1325 copy = min_t(unsigned int, attr_len, iedatalen);
1326
1327 if (desired_attr) {
1328 desired_len += copy;
1329 if (out) {
1330 memcpy(out, iedata, min(bufsize, copy));
1331 out += min(bufsize, copy);
1332 bufsize -= min(bufsize, copy);
1333 }
1334
1335 if (copy == attr_len)
1336 return desired_len;
1337 }
1338
1339 iedata += copy;
1340 iedatalen -= copy;
1341 attr_remaining = attr_len - copy;
1342 }
1343
1344 cont:
1345 len -= ies[1] + 2;
1346 ies += ies[1] + 2;
1347 }
1348
1349 if (attr_remaining && desired_attr)
1350 return -EILSEQ;
1351
1352 return -ENOENT;
1353}
1354EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1355
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001356static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
Johannes Berg29464cc2015-03-31 15:36:22 +02001357{
1358 int i;
1359
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001360 /* Make sure array values are legal */
1361 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1362 return false;
1363
1364 i = 0;
1365 while (i < n_ids) {
1366 if (ids[i] == WLAN_EID_EXTENSION) {
1367 if (id_ext && (ids[i + 1] == id))
1368 return true;
1369
1370 i += 2;
1371 continue;
1372 }
1373
1374 if (ids[i] == id && !id_ext)
Johannes Berg29464cc2015-03-31 15:36:22 +02001375 return true;
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001376
1377 i++;
1378 }
Johannes Berg29464cc2015-03-31 15:36:22 +02001379 return false;
1380}
1381
Johannes Berg8ac63442015-09-04 20:03:23 +02001382static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1383{
1384 /* we assume a validly formed IEs buffer */
1385 u8 len = ies[pos + 1];
1386
1387 pos += 2 + len;
1388
1389 /* the IE itself must have 255 bytes for fragments to follow */
1390 if (len < 255)
1391 return pos;
1392
1393 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1394 len = ies[pos + 1];
1395 pos += 2 + len;
1396 }
1397
1398 return pos;
1399}
1400
Johannes Berg29464cc2015-03-31 15:36:22 +02001401size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1402 const u8 *ids, int n_ids,
1403 const u8 *after_ric, int n_after_ric,
1404 size_t offset)
1405{
1406 size_t pos = offset;
1407
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001408 while (pos < ielen) {
1409 u8 ext = 0;
1410
1411 if (ies[pos] == WLAN_EID_EXTENSION)
1412 ext = 2;
1413 if ((pos + ext) >= ielen)
1414 break;
1415
1416 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1417 ies[pos] == WLAN_EID_EXTENSION))
1418 break;
1419
Johannes Berg29464cc2015-03-31 15:36:22 +02001420 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
Johannes Berg8ac63442015-09-04 20:03:23 +02001421 pos = skip_ie(ies, ielen, pos);
Johannes Berg29464cc2015-03-31 15:36:22 +02001422
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001423 while (pos < ielen) {
1424 if (ies[pos] == WLAN_EID_EXTENSION)
1425 ext = 2;
1426 else
1427 ext = 0;
1428
1429 if ((pos + ext) >= ielen)
1430 break;
1431
1432 if (!ieee80211_id_in_list(after_ric,
1433 n_after_ric,
1434 ies[pos + ext],
1435 ext == 2))
1436 pos = skip_ie(ies, ielen, pos);
Jouni Malinen312ca382018-12-05 12:55:54 +02001437 else
1438 break;
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001439 }
Johannes Berg29464cc2015-03-31 15:36:22 +02001440 } else {
Johannes Berg8ac63442015-09-04 20:03:23 +02001441 pos = skip_ie(ies, ielen, pos);
Johannes Berg29464cc2015-03-31 15:36:22 +02001442 }
1443 }
1444
1445 return pos;
1446}
1447EXPORT_SYMBOL(ieee80211_ie_split_ric);
1448
Johannes Berg1ce3e822012-08-01 17:00:55 +02001449bool ieee80211_operating_class_to_band(u8 operating_class,
Johannes Berg57fbcce2016-04-12 15:56:15 +02001450 enum nl80211_band *band)
Johannes Berg1ce3e822012-08-01 17:00:55 +02001451{
1452 switch (operating_class) {
1453 case 112:
1454 case 115 ... 127:
Eliad Peller954a86e2015-03-01 09:10:01 +02001455 case 128 ... 130:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001456 *band = NL80211_BAND_5GHZ;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001457 return true;
1458 case 81:
1459 case 82:
1460 case 83:
1461 case 84:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001462 *band = NL80211_BAND_2GHZ;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001463 return true;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001464 case 180:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001465 *band = NL80211_BAND_60GHZ;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001466 return true;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001467 }
1468
1469 return false;
1470}
1471EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1472
Arik Nemtsova38700d2015-03-18 08:46:08 +02001473bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1474 u8 *op_class)
1475{
1476 u8 vht_opclass;
Dan Carpenter84429382018-08-31 11:10:55 +03001477 u32 freq = chandef->center_freq1;
Arik Nemtsova38700d2015-03-18 08:46:08 +02001478
1479 if (freq >= 2412 && freq <= 2472) {
1480 if (chandef->width > NL80211_CHAN_WIDTH_40)
1481 return false;
1482
1483 /* 2.407 GHz, channels 1..13 */
1484 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1485 if (freq > chandef->chan->center_freq)
1486 *op_class = 83; /* HT40+ */
1487 else
1488 *op_class = 84; /* HT40- */
1489 } else {
1490 *op_class = 81;
1491 }
1492
1493 return true;
1494 }
1495
1496 if (freq == 2484) {
1497 if (chandef->width > NL80211_CHAN_WIDTH_40)
1498 return false;
1499
1500 *op_class = 82; /* channel 14 */
1501 return true;
1502 }
1503
1504 switch (chandef->width) {
1505 case NL80211_CHAN_WIDTH_80:
1506 vht_opclass = 128;
1507 break;
1508 case NL80211_CHAN_WIDTH_160:
1509 vht_opclass = 129;
1510 break;
1511 case NL80211_CHAN_WIDTH_80P80:
1512 vht_opclass = 130;
1513 break;
1514 case NL80211_CHAN_WIDTH_10:
1515 case NL80211_CHAN_WIDTH_5:
1516 return false; /* unsupported for now */
1517 default:
1518 vht_opclass = 0;
1519 break;
1520 }
1521
1522 /* 5 GHz, channels 36..48 */
1523 if (freq >= 5180 && freq <= 5240) {
1524 if (vht_opclass) {
1525 *op_class = vht_opclass;
1526 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1527 if (freq > chandef->chan->center_freq)
1528 *op_class = 116;
1529 else
1530 *op_class = 117;
1531 } else {
1532 *op_class = 115;
1533 }
1534
1535 return true;
1536 }
1537
1538 /* 5 GHz, channels 52..64 */
1539 if (freq >= 5260 && freq <= 5320) {
1540 if (vht_opclass) {
1541 *op_class = vht_opclass;
1542 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1543 if (freq > chandef->chan->center_freq)
1544 *op_class = 119;
1545 else
1546 *op_class = 120;
1547 } else {
1548 *op_class = 118;
1549 }
1550
1551 return true;
1552 }
1553
1554 /* 5 GHz, channels 100..144 */
1555 if (freq >= 5500 && freq <= 5720) {
1556 if (vht_opclass) {
1557 *op_class = vht_opclass;
1558 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1559 if (freq > chandef->chan->center_freq)
1560 *op_class = 122;
1561 else
1562 *op_class = 123;
1563 } else {
1564 *op_class = 121;
1565 }
1566
1567 return true;
1568 }
1569
1570 /* 5 GHz, channels 149..169 */
1571 if (freq >= 5745 && freq <= 5845) {
1572 if (vht_opclass) {
1573 *op_class = vht_opclass;
1574 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1575 if (freq > chandef->chan->center_freq)
1576 *op_class = 126;
1577 else
1578 *op_class = 127;
1579 } else if (freq <= 5805) {
1580 *op_class = 124;
1581 } else {
1582 *op_class = 125;
1583 }
1584
1585 return true;
1586 }
1587
1588 /* 56.16 GHz, channel 1..4 */
Alexei Avshalom Lazar9cf0a0b2018-08-13 15:33:00 +03001589 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
Arik Nemtsova38700d2015-03-18 08:46:08 +02001590 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1591 return false;
1592
1593 *op_class = 180;
1594 return true;
1595 }
1596
1597 /* not supported yet */
1598 return false;
1599}
1600EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1601
Johannes Berg4c8dea62016-10-21 14:25:13 +02001602static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1603 u32 *beacon_int_gcd,
1604 bool *beacon_int_different)
1605{
1606 struct wireless_dev *wdev;
1607
1608 *beacon_int_gcd = 0;
1609 *beacon_int_different = false;
1610
1611 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1612 if (!wdev->beacon_interval)
1613 continue;
1614
1615 if (!*beacon_int_gcd) {
1616 *beacon_int_gcd = wdev->beacon_interval;
1617 continue;
1618 }
1619
1620 if (wdev->beacon_interval == *beacon_int_gcd)
1621 continue;
1622
1623 *beacon_int_different = true;
1624 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1625 }
1626
1627 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1628 if (*beacon_int_gcd)
1629 *beacon_int_different = true;
1630 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1631 }
1632}
1633
Johannes Berg56d18932011-05-09 18:41:15 +02001634int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05301635 enum nl80211_iftype iftype, u32 beacon_int)
Johannes Berg56d18932011-05-09 18:41:15 +02001636{
Johannes Berg4c8dea62016-10-21 14:25:13 +02001637 /*
1638 * This is just a basic pre-condition check; if interface combinations
1639 * are possible the driver must already be checking those with a call
1640 * to cfg80211_check_combinations(), in which case we'll validate more
1641 * through the cfg80211_calculate_bi_data() call and code in
1642 * cfg80211_iter_combinations().
1643 */
Johannes Berg56d18932011-05-09 18:41:15 +02001644
Purushottam Kushwaha12d20fc92016-08-11 15:14:02 +05301645 if (beacon_int < 10 || beacon_int > 10000)
Johannes Berg56d18932011-05-09 18:41:15 +02001646 return -EINVAL;
1647
Johannes Berg4c8dea62016-10-21 14:25:13 +02001648 return 0;
Johannes Berg56d18932011-05-09 18:41:15 +02001649}
Johannes Berg7527a782011-05-13 10:58:57 +02001650
Michal Kazior65a124d2014-04-09 15:29:22 +02001651int cfg80211_iter_combinations(struct wiphy *wiphy,
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301652 struct iface_combination_params *params,
Michal Kazior65a124d2014-04-09 15:29:22 +02001653 void (*iter)(const struct ieee80211_iface_combination *c,
1654 void *data),
1655 void *data)
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001656{
Felix Fietkau8c48b502014-05-05 11:48:40 +02001657 const struct ieee80211_regdomain *regdom;
1658 enum nl80211_dfs_regions region = 0;
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001659 int i, j, iftype;
1660 int num_interfaces = 0;
1661 u32 used_iftypes = 0;
Johannes Berg4c8dea62016-10-21 14:25:13 +02001662 u32 beacon_int_gcd;
1663 bool beacon_int_different;
1664
1665 /*
1666 * This is a bit strange, since the iteration used to rely only on
1667 * the data given by the driver, but here it now relies on context,
1668 * in form of the currently operating interfaces.
1669 * This is OK for all current users, and saves us from having to
1670 * push the GCD calculations into all the drivers.
1671 * In the future, this should probably rely more on data that's in
1672 * cfg80211 already - the only thing not would appear to be any new
1673 * interfaces (while being brought up) and channel/radar data.
1674 */
1675 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1676 &beacon_int_gcd, &beacon_int_different);
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001677
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301678 if (params->radar_detect) {
Felix Fietkau8c48b502014-05-05 11:48:40 +02001679 rcu_read_lock();
1680 regdom = rcu_dereference(cfg80211_regdomain);
1681 if (regdom)
1682 region = regdom->dfs_region;
1683 rcu_read_unlock();
1684 }
1685
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001686 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301687 num_interfaces += params->iftype_num[iftype];
1688 if (params->iftype_num[iftype] > 0 &&
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001689 !(wiphy->software_iftypes & BIT(iftype)))
1690 used_iftypes |= BIT(iftype);
1691 }
1692
1693 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1694 const struct ieee80211_iface_combination *c;
1695 struct ieee80211_iface_limit *limits;
1696 u32 all_iftypes = 0;
1697
1698 c = &wiphy->iface_combinations[i];
1699
1700 if (num_interfaces > c->max_interfaces)
1701 continue;
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301702 if (params->num_different_channels > c->num_different_channels)
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001703 continue;
1704
1705 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1706 GFP_KERNEL);
1707 if (!limits)
1708 return -ENOMEM;
1709
1710 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1711 if (wiphy->software_iftypes & BIT(iftype))
1712 continue;
1713 for (j = 0; j < c->n_limits; j++) {
1714 all_iftypes |= limits[j].types;
1715 if (!(limits[j].types & BIT(iftype)))
1716 continue;
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301717 if (limits[j].max < params->iftype_num[iftype])
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001718 goto cont;
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301719 limits[j].max -= params->iftype_num[iftype];
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001720 }
1721 }
1722
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301723 if (params->radar_detect !=
1724 (c->radar_detect_widths & params->radar_detect))
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001725 goto cont;
1726
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301727 if (params->radar_detect && c->radar_detect_regions &&
Felix Fietkau8c48b502014-05-05 11:48:40 +02001728 !(c->radar_detect_regions & BIT(region)))
1729 goto cont;
1730
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001731 /* Finally check that all iftypes that we're currently
1732 * using are actually part of this combination. If they
1733 * aren't then we can't use this combination and have
1734 * to continue to the next.
1735 */
1736 if ((all_iftypes & used_iftypes) != used_iftypes)
1737 goto cont;
1738
Johannes Berg4c8dea62016-10-21 14:25:13 +02001739 if (beacon_int_gcd) {
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05301740 if (c->beacon_int_min_gcd &&
Johannes Berg4c8dea62016-10-21 14:25:13 +02001741 beacon_int_gcd < c->beacon_int_min_gcd)
Johannes Berg0507a3a2016-10-21 12:15:00 +02001742 goto cont;
Johannes Berg4c8dea62016-10-21 14:25:13 +02001743 if (!c->beacon_int_min_gcd && beacon_int_different)
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05301744 goto cont;
1745 }
1746
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001747 /* This combination covered all interface types and
1748 * supported the requested numbers, so we're good.
1749 */
Michal Kazior65a124d2014-04-09 15:29:22 +02001750
1751 (*iter)(c, data);
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001752 cont:
1753 kfree(limits);
1754 }
1755
Michal Kazior65a124d2014-04-09 15:29:22 +02001756 return 0;
1757}
1758EXPORT_SYMBOL(cfg80211_iter_combinations);
1759
1760static void
1761cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1762 void *data)
1763{
1764 int *num = data;
1765 (*num)++;
1766}
1767
1768int cfg80211_check_combinations(struct wiphy *wiphy,
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301769 struct iface_combination_params *params)
Michal Kazior65a124d2014-04-09 15:29:22 +02001770{
1771 int err, num = 0;
1772
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301773 err = cfg80211_iter_combinations(wiphy, params,
Michal Kazior65a124d2014-04-09 15:29:22 +02001774 cfg80211_iter_sum_ifcombs, &num);
1775 if (err)
1776 return err;
1777 if (num == 0)
1778 return -EBUSY;
1779
1780 return 0;
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001781}
1782EXPORT_SYMBOL(cfg80211_check_combinations);
1783
Johannes Berg34850ab2011-07-18 18:08:35 +02001784int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1785 const u8 *rates, unsigned int n_rates,
1786 u32 *mask)
1787{
1788 int i, j;
1789
Johannes Berga401d2b2011-07-20 00:52:16 +02001790 if (!sband)
1791 return -EINVAL;
1792
Johannes Berg34850ab2011-07-18 18:08:35 +02001793 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1794 return -EINVAL;
1795
1796 *mask = 0;
1797
1798 for (i = 0; i < n_rates; i++) {
1799 int rate = (rates[i] & 0x7f) * 5;
1800 bool found = false;
1801
1802 for (j = 0; j < sband->n_bitrates; j++) {
1803 if (sband->bitrates[j].bitrate == rate) {
1804 found = true;
1805 *mask |= BIT(j);
1806 break;
1807 }
1808 }
1809 if (!found)
1810 return -EINVAL;
1811 }
1812
1813 /*
1814 * mask must have at least one bit set here since we
1815 * didn't accept a 0-length rates array nor allowed
1816 * entries in the array that didn't exist
1817 */
1818
1819 return 0;
1820}
Johannes Berg11a2a352011-11-21 11:09:22 +01001821
Ilan Peerbdfbec22014-01-09 11:37:23 +02001822unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1823{
Johannes Berg57fbcce2016-04-12 15:56:15 +02001824 enum nl80211_band band;
Ilan Peerbdfbec22014-01-09 11:37:23 +02001825 unsigned int n_channels = 0;
1826
Johannes Berg57fbcce2016-04-12 15:56:15 +02001827 for (band = 0; band < NUM_NL80211_BANDS; band++)
Ilan Peerbdfbec22014-01-09 11:37:23 +02001828 if (wiphy->bands[band])
1829 n_channels += wiphy->bands[band]->n_channels;
1830
1831 return n_channels;
1832}
1833EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1834
Antonio Quartulli74063532014-05-19 21:53:21 +02001835int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1836 struct station_info *sinfo)
1837{
1838 struct cfg80211_registered_device *rdev;
1839 struct wireless_dev *wdev;
1840
1841 wdev = dev->ieee80211_ptr;
1842 if (!wdev)
1843 return -EOPNOTSUPP;
1844
1845 rdev = wiphy_to_rdev(wdev->wiphy);
1846 if (!rdev->ops->get_station)
1847 return -EOPNOTSUPP;
1848
Sven Eckelmann3c12d042018-06-06 10:53:55 +02001849 memset(sinfo, 0, sizeof(*sinfo));
1850
Antonio Quartulli74063532014-05-19 21:53:21 +02001851 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1852}
1853EXPORT_SYMBOL(cfg80211_get_station);
1854
Ayala Bekera442b762016-09-20 17:31:15 +03001855void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1856{
1857 int i;
1858
1859 if (!f)
1860 return;
1861
1862 kfree(f->serv_spec_info);
1863 kfree(f->srf_bf);
1864 kfree(f->srf_macs);
1865 for (i = 0; i < f->num_rx_filters; i++)
1866 kfree(f->rx_filters[i].filter);
1867
1868 for (i = 0; i < f->num_tx_filters; i++)
1869 kfree(f->tx_filters[i].filter);
1870
1871 kfree(f->rx_filters);
1872 kfree(f->tx_filters);
1873 kfree(f);
1874}
1875EXPORT_SYMBOL(cfg80211_free_nan_func);
1876
Rafał Miłecki4787cfa2017-01-04 18:58:30 +01001877bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1878 u32 center_freq_khz, u32 bw_khz)
1879{
1880 u32 start_freq_khz, end_freq_khz;
1881
1882 start_freq_khz = center_freq_khz - (bw_khz / 2);
1883 end_freq_khz = center_freq_khz + (bw_khz / 2);
1884
1885 if (start_freq_khz >= freq_range->start_freq_khz &&
1886 end_freq_khz <= freq_range->end_freq_khz)
1887 return true;
1888
1889 return false;
1890}
1891
Arend van Spriel8689c052018-05-10 13:50:12 +02001892int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
1893{
Johannes Berg1d211d42018-05-23 14:53:41 +02001894 sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
1895 sizeof(*(sinfo->pertid)),
1896 gfp);
Arend van Spriel8689c052018-05-10 13:50:12 +02001897 if (!sinfo->pertid)
1898 return -ENOMEM;
1899
1900 return 0;
1901}
1902EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
1903
Johannes Berg11a2a352011-11-21 11:09:22 +01001904/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1905/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1906const unsigned char rfc1042_header[] __aligned(2) =
1907 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1908EXPORT_SYMBOL(rfc1042_header);
1909
1910/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1911const unsigned char bridge_tunnel_header[] __aligned(2) =
1912 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1913EXPORT_SYMBOL(bridge_tunnel_header);
Dedy Lansky30ca1aa2018-07-29 14:59:16 +03001914
1915/* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1916struct iapp_layer2_update {
1917 u8 da[ETH_ALEN]; /* broadcast */
1918 u8 sa[ETH_ALEN]; /* STA addr */
1919 __be16 len; /* 6 */
1920 u8 dsap; /* 0 */
1921 u8 ssap; /* 0 */
1922 u8 control;
1923 u8 xid_info[3];
1924} __packed;
1925
1926void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
1927{
1928 struct iapp_layer2_update *msg;
1929 struct sk_buff *skb;
1930
1931 /* Send Level 2 Update Frame to update forwarding tables in layer 2
1932 * bridge devices */
1933
1934 skb = dev_alloc_skb(sizeof(*msg));
1935 if (!skb)
1936 return;
1937 msg = skb_put(skb, sizeof(*msg));
1938
1939 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1940 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1941
1942 eth_broadcast_addr(msg->da);
1943 ether_addr_copy(msg->sa, addr);
1944 msg->len = htons(6);
1945 msg->dsap = 0;
1946 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
1947 msg->control = 0xaf; /* XID response lsb.1111F101.
1948 * F=0 (no poll command; unsolicited frame) */
1949 msg->xid_info[0] = 0x81; /* XID format identifier */
1950 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
1951 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
1952
1953 skb->dev = dev;
1954 skb->protocol = eth_type_trans(skb, dev);
1955 memset(skb->cb, 0, sizeof(skb->cb));
1956 netif_rx_ni(skb);
1957}
1958EXPORT_SYMBOL(cfg80211_send_layer2_update);
Johannes Bergb0aa75f2018-08-31 11:31:16 +03001959
1960int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
1961 enum ieee80211_vht_chanwidth bw,
1962 int mcs, bool ext_nss_bw_capable)
1963{
1964 u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
1965 int max_vht_nss = 0;
1966 int ext_nss_bw;
1967 int supp_width;
1968 int i, mcs_encoding;
1969
1970 if (map == 0xffff)
1971 return 0;
1972
1973 if (WARN_ON(mcs > 9))
1974 return 0;
1975 if (mcs <= 7)
1976 mcs_encoding = 0;
1977 else if (mcs == 8)
1978 mcs_encoding = 1;
1979 else
1980 mcs_encoding = 2;
1981
1982 /* find max_vht_nss for the given MCS */
1983 for (i = 7; i >= 0; i--) {
1984 int supp = (map >> (2 * i)) & 3;
1985
1986 if (supp == 3)
1987 continue;
1988
1989 if (supp >= mcs_encoding) {
1990 max_vht_nss = i;
1991 break;
1992 }
1993 }
1994
1995 if (!(cap->supp_mcs.tx_mcs_map &
1996 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
1997 return max_vht_nss;
1998
1999 ext_nss_bw = le32_get_bits(cap->vht_cap_info,
2000 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
2001 supp_width = le32_get_bits(cap->vht_cap_info,
2002 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
2003
2004 /* if not capable, treat ext_nss_bw as 0 */
2005 if (!ext_nss_bw_capable)
2006 ext_nss_bw = 0;
2007
2008 /* This is invalid */
2009 if (supp_width == 3)
2010 return 0;
2011
2012 /* This is an invalid combination so pretend nothing is supported */
2013 if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
2014 return 0;
2015
2016 /*
2017 * Cover all the special cases according to IEEE 802.11-2016
2018 * Table 9-250. All other cases are either factor of 1 or not
2019 * valid/supported.
2020 */
2021 switch (bw) {
2022 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2023 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2024 if ((supp_width == 1 || supp_width == 2) &&
2025 ext_nss_bw == 3)
2026 return 2 * max_vht_nss;
2027 break;
2028 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2029 if (supp_width == 0 &&
2030 (ext_nss_bw == 1 || ext_nss_bw == 2))
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002031 return max_vht_nss / 2;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002032 if (supp_width == 0 &&
2033 ext_nss_bw == 3)
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002034 return (3 * max_vht_nss) / 4;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002035 if (supp_width == 1 &&
2036 ext_nss_bw == 3)
2037 return 2 * max_vht_nss;
2038 break;
2039 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002040 if (supp_width == 0 && ext_nss_bw == 1)
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002041 return 0; /* not possible */
2042 if (supp_width == 0 &&
2043 ext_nss_bw == 2)
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002044 return max_vht_nss / 2;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002045 if (supp_width == 0 &&
2046 ext_nss_bw == 3)
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002047 return (3 * max_vht_nss) / 4;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002048 if (supp_width == 1 &&
2049 ext_nss_bw == 0)
2050 return 0; /* not possible */
2051 if (supp_width == 1 &&
2052 ext_nss_bw == 1)
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002053 return max_vht_nss / 2;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002054 if (supp_width == 1 &&
2055 ext_nss_bw == 2)
Johannes Berg93bc8ac2018-12-15 11:03:16 +02002056 return (3 * max_vht_nss) / 4;
Johannes Bergb0aa75f2018-08-31 11:31:16 +03002057 break;
2058 }
2059
2060 /* not covered or invalid combination received */
2061 return max_vht_nss;
2062}
2063EXPORT_SYMBOL(ieee80211_get_vht_max_nss);