blob: e7ea1524ab0555cd9a401de49a677365ee266286 [file] [log] [blame]
Johannes Berg8318d782008-01-24 19:38:38 +01001/*
2 * Wireless utility functions
3 *
Johannes Bergd3236552009-04-20 14:31:42 +02004 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg2740f0c2014-09-03 15:24:58 +03005 * Copyright 2013-2014 Intel Mobile Communications GmbH
Johannes Berg8318d782008-01-24 19:38:38 +01006 */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -04007#include <linux/export.h>
Johannes Bergd3236552009-04-20 14:31:42 +02008#include <linux/bitops.h>
Zhu Yie31a16d2009-05-21 21:47:03 +08009#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Bergd3236552009-04-20 14:31:42 +020011#include <net/cfg80211.h>
Zhu Yie31a16d2009-05-21 21:47:03 +080012#include <net/ip.h>
Dave Tähtb1565792011-12-22 12:55:08 -080013#include <net/dsfield.h>
cedric Vonckenc6ca5e22013-08-26 14:04:52 +020014#include <linux/if_vlan.h>
Simon Wunderlich960d97f2014-03-03 17:23:12 +010015#include <linux/mpls.h>
Johannes Berg7c62e272014-02-25 15:04:46 -080016#include <net/ndisc.h>
17#include <linux/if_arp.h>
Johannes Berg654c3b92016-10-21 14:25:13 +020018#include <linux/gcd.h>
Johannes Berg8318d782008-01-24 19:38:38 +010019#include "core.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030020#include "rdev-ops.h"
21
Johannes Berg8318d782008-01-24 19:38:38 +010022
Johannes Bergbd815252008-10-29 20:00:45 +010023struct ieee80211_rate *
24ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
Johannes Berg881d9482009-01-21 15:13:48 +010025 u32 basic_rates, int bitrate)
Johannes Bergbd815252008-10-29 20:00:45 +010026{
27 struct ieee80211_rate *result = &sband->bitrates[0];
28 int i;
29
30 for (i = 0; i < sband->n_bitrates; i++) {
31 if (!(basic_rates & BIT(i)))
32 continue;
33 if (sband->bitrates[i].bitrate > bitrate)
34 continue;
35 result = &sband->bitrates[i];
36 }
37
38 return result;
39}
40EXPORT_SYMBOL(ieee80211_get_response_rate);
41
Simon Wunderlich74608ac2013-07-08 16:55:54 +020042u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
43 enum nl80211_bss_scan_width scan_width)
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070044{
45 struct ieee80211_rate *bitrates;
46 u32 mandatory_rates = 0;
47 enum ieee80211_rate_flags mandatory_flag;
48 int i;
49
50 if (WARN_ON(!sband))
51 return 1;
52
Johannes Berg57fbcce2016-04-12 15:56:15 +020053 if (sband->band == NL80211_BAND_2GHZ) {
Simon Wunderlich74608ac2013-07-08 16:55:54 +020054 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
55 scan_width == NL80211_BSS_CHAN_WIDTH_10)
56 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
57 else
58 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
59 } else {
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070060 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
Simon Wunderlich74608ac2013-07-08 16:55:54 +020061 }
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070062
63 bitrates = sband->bitrates;
64 for (i = 0; i < sband->n_bitrates; i++)
65 if (bitrates[i].flags & mandatory_flag)
66 mandatory_rates |= BIT(i);
67 return mandatory_rates;
68}
69EXPORT_SYMBOL(ieee80211_mandatory_rates);
70
Johannes Berg57fbcce2016-04-12 15:56:15 +020071int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +010072{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090073 /* see 802.11 17.3.8.3.2 and Annex J
74 * there are overlapping channel numbers in 5GHz and 2GHz bands */
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030075 if (chan <= 0)
76 return 0; /* not supported */
77 switch (band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +020078 case NL80211_BAND_2GHZ:
Bruno Randolf59eb21a2011-01-17 13:37:28 +090079 if (chan == 14)
80 return 2484;
81 else if (chan < 14)
82 return 2407 + chan * 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030083 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +020084 case NL80211_BAND_5GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030085 if (chan >= 182 && chan <= 196)
86 return 4000 + chan * 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090087 else
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030088 return 5000 + chan * 5;
89 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +020090 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030091 if (chan < 5)
92 return 56160 + chan * 2160;
93 break;
94 default:
95 ;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090096 }
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030097 return 0; /* not supported */
Johannes Berg8318d782008-01-24 19:38:38 +010098}
99EXPORT_SYMBOL(ieee80211_channel_to_frequency);
100
101int ieee80211_frequency_to_channel(int freq)
102{
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900103 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +0100104 if (freq == 2484)
105 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900106 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +0100107 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900108 else if (freq >= 4910 && freq <= 4980)
109 return (freq - 4000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300110 else if (freq <= 45000) /* DMG band lower limit */
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900111 return (freq - 5000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300112 else if (freq >= 58320 && freq <= 64800)
113 return (freq - 56160) / 2160;
114 else
115 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100116}
117EXPORT_SYMBOL(ieee80211_frequency_to_channel);
118
Johannes Berg6c507cd2008-03-26 14:14:55 +0100119struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
120 int freq)
Johannes Berg906c7302008-03-16 18:34:33 +0100121{
Johannes Berg57fbcce2016-04-12 15:56:15 +0200122 enum nl80211_band band;
Johannes Berg906c7302008-03-16 18:34:33 +0100123 struct ieee80211_supported_band *sband;
124 int i;
125
Johannes Berg57fbcce2016-04-12 15:56:15 +0200126 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Berg906c7302008-03-16 18:34:33 +0100127 sband = wiphy->bands[band];
128
129 if (!sband)
130 continue;
131
132 for (i = 0; i < sband->n_channels; i++) {
133 if (sband->channels[i].center_freq == freq)
134 return &sband->channels[i];
135 }
136 }
137
138 return NULL;
139}
Johannes Berg6c507cd2008-03-26 14:14:55 +0100140EXPORT_SYMBOL(__ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +0100141
Johannes Berg8318d782008-01-24 19:38:38 +0100142static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
Johannes Berg57fbcce2016-04-12 15:56:15 +0200143 enum nl80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +0100144{
145 int i, want;
146
147 switch (band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200148 case NL80211_BAND_5GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100149 want = 3;
150 for (i = 0; i < sband->n_bitrates; i++) {
151 if (sband->bitrates[i].bitrate == 60 ||
152 sband->bitrates[i].bitrate == 120 ||
153 sband->bitrates[i].bitrate == 240) {
154 sband->bitrates[i].flags |=
155 IEEE80211_RATE_MANDATORY_A;
156 want--;
157 }
158 }
159 WARN_ON(want);
160 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200161 case NL80211_BAND_2GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100162 want = 7;
163 for (i = 0; i < sband->n_bitrates; i++) {
164 if (sband->bitrates[i].bitrate == 10) {
165 sband->bitrates[i].flags |=
166 IEEE80211_RATE_MANDATORY_B |
167 IEEE80211_RATE_MANDATORY_G;
168 want--;
169 }
170
171 if (sband->bitrates[i].bitrate == 20 ||
172 sband->bitrates[i].bitrate == 55 ||
173 sband->bitrates[i].bitrate == 110 ||
174 sband->bitrates[i].bitrate == 60 ||
175 sband->bitrates[i].bitrate == 120 ||
176 sband->bitrates[i].bitrate == 240) {
177 sband->bitrates[i].flags |=
178 IEEE80211_RATE_MANDATORY_G;
179 want--;
180 }
181
Johannes Bergaac09fb2008-01-30 17:36:10 +0100182 if (sband->bitrates[i].bitrate != 10 &&
183 sband->bitrates[i].bitrate != 20 &&
184 sband->bitrates[i].bitrate != 55 &&
185 sband->bitrates[i].bitrate != 110)
Johannes Berg8318d782008-01-24 19:38:38 +0100186 sband->bitrates[i].flags |=
187 IEEE80211_RATE_ERP_G;
188 }
Ivo van Doorn406f2382008-02-02 23:53:10 +0100189 WARN_ON(want != 0 && want != 3 && want != 6);
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:
Johannes Berg8318d782008-01-24 19:38:38 +0100197 WARN_ON(1);
198 break;
199 }
200}
201
202void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
203{
Johannes Berg57fbcce2016-04-12 15:56:15 +0200204 enum nl80211_band band;
Johannes Berg8318d782008-01-24 19:38:38 +0100205
Johannes Berg57fbcce2016-04-12 15:56:15 +0200206 for (band = 0; band < NUM_NL80211_BANDS; band++)
Johannes Berg8318d782008-01-24 19:38:38 +0100207 if (wiphy->bands[band])
208 set_mandatory_flags_band(wiphy->bands[band], band);
209}
Johannes Berg08645122009-05-11 13:54:58 +0200210
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300211bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
212{
213 int i;
214 for (i = 0; i < wiphy->n_cipher_suites; i++)
215 if (cipher == wiphy->cipher_suites[i])
216 return true;
217 return false;
218}
219
Johannes Bergfffd0932009-07-08 14:22:54 +0200220int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
221 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200222 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200223{
Jouni Malinena2be0702020-03-27 18:20:31 +0530224 int max_key_idx = 5;
225
226 if (wiphy_ext_feature_isset(&rdev->wiphy,
227 NL80211_EXT_FEATURE_BEACON_PROTECTION))
228 max_key_idx = 7;
229 if (key_idx < 0 || key_idx > max_key_idx)
Johannes Berg08645122009-05-11 13:54:58 +0200230 return -EINVAL;
231
Johannes Berge31b8212010-10-05 19:39:30 +0200232 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
233 return -EINVAL;
234
235 if (pairwise && !mac_addr)
236 return -EINVAL;
237
Jouni Malinen37720562015-01-24 19:52:04 +0200238 switch (params->cipher) {
239 case WLAN_CIPHER_SUITE_TKIP:
240 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200241 case WLAN_CIPHER_SUITE_CCMP_256:
242 case WLAN_CIPHER_SUITE_GCMP:
243 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinen37720562015-01-24 19:52:04 +0200244 /* Disallow pairwise keys with non-zero index unless it's WEP
245 * or a vendor specific cipher (because current deployments use
246 * pairwise WEP keys with non-zero indices and for vendor
247 * specific ciphers this should be validated in the driver or
248 * hardware level - but 802.11i clearly specifies to use zero)
249 */
250 if (pairwise && key_idx)
251 return -EINVAL;
252 break;
253 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200254 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
255 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
256 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen37720562015-01-24 19:52:04 +0200257 /* Disallow BIP (group-only) cipher as pairwise cipher */
258 if (pairwise)
259 return -EINVAL;
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200260 if (key_idx < 4)
261 return -EINVAL;
Jouni Malinen37720562015-01-24 19:52:04 +0200262 break;
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200263 case WLAN_CIPHER_SUITE_WEP40:
264 case WLAN_CIPHER_SUITE_WEP104:
265 if (key_idx > 3)
266 return -EINVAL;
Jouni Malinen37720562015-01-24 19:52:04 +0200267 default:
268 break;
269 }
Johannes Berg08645122009-05-11 13:54:58 +0200270
Johannes Berg08645122009-05-11 13:54:58 +0200271 switch (params->cipher) {
272 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200273 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200274 return -EINVAL;
275 break;
276 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200277 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200278 return -EINVAL;
279 break;
280 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200281 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200282 return -EINVAL;
283 break;
Jouni Malinencfcf1682015-01-24 19:52:05 +0200284 case WLAN_CIPHER_SUITE_CCMP_256:
285 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
286 return -EINVAL;
287 break;
288 case WLAN_CIPHER_SUITE_GCMP:
289 if (params->key_len != WLAN_KEY_LEN_GCMP)
290 return -EINVAL;
291 break;
292 case WLAN_CIPHER_SUITE_GCMP_256:
293 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
294 return -EINVAL;
295 break;
Johannes Berg08645122009-05-11 13:54:58 +0200296 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200297 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200298 return -EINVAL;
299 break;
300 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200301 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200302 return -EINVAL;
303 break;
Jouni Malinencfcf1682015-01-24 19:52:05 +0200304 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
305 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
306 return -EINVAL;
307 break;
308 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
309 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
310 return -EINVAL;
311 break;
312 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
313 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
314 return -EINVAL;
315 break;
Johannes Berg08645122009-05-11 13:54:58 +0200316 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300317 /*
318 * We don't know anything about this algorithm,
319 * allow using it -- but the driver must check
320 * all parameters! We still check below whether
321 * or not the driver supports this algorithm,
322 * of course.
323 */
324 break;
Johannes Berg08645122009-05-11 13:54:58 +0200325 }
326
Jouni Malinen9f26a952009-05-15 12:38:32 +0300327 if (params->seq) {
328 switch (params->cipher) {
329 case WLAN_CIPHER_SUITE_WEP40:
330 case WLAN_CIPHER_SUITE_WEP104:
331 /* These ciphers do not use key sequence */
332 return -EINVAL;
333 case WLAN_CIPHER_SUITE_TKIP:
334 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200335 case WLAN_CIPHER_SUITE_CCMP_256:
336 case WLAN_CIPHER_SUITE_GCMP:
337 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinen9f26a952009-05-15 12:38:32 +0300338 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200339 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
340 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
341 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen9f26a952009-05-15 12:38:32 +0300342 if (params->seq_len != 6)
343 return -EINVAL;
344 break;
345 }
346 }
347
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300348 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200349 return -EINVAL;
350
Johannes Berg08645122009-05-11 13:54:58 +0200351 return 0;
352}
Zhu Yie31a16d2009-05-21 21:47:03 +0800353
Johannes Berg633adf12010-08-12 14:49:58 +0200354unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800355{
356 unsigned int hdrlen = 24;
357
358 if (ieee80211_is_data(fc)) {
359 if (ieee80211_has_a4(fc))
360 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200361 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800362 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200363 if (ieee80211_has_order(fc))
364 hdrlen += IEEE80211_HT_CTL_LEN;
365 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800366 goto out;
367 }
368
Fred Choufb142f42015-01-20 10:17:27 +0800369 if (ieee80211_is_mgmt(fc)) {
370 if (ieee80211_has_order(fc))
371 hdrlen += IEEE80211_HT_CTL_LEN;
372 goto out;
373 }
374
Zhu Yie31a16d2009-05-21 21:47:03 +0800375 if (ieee80211_is_ctl(fc)) {
376 /*
377 * ACK and CTS are 10 bytes, all others 16. To see how
378 * to get this condition consider
379 * subtype mask: 0b0000000011110000 (0x00F0)
380 * ACK subtype: 0b0000000011010000 (0x00D0)
381 * CTS subtype: 0b0000000011000000 (0x00C0)
382 * bits that matter: ^^^ (0x00E0)
383 * value of those: 0b0000000011000000 (0x00C0)
384 */
385 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
386 hdrlen = 10;
387 else
388 hdrlen = 16;
389 }
390out:
391 return hdrlen;
392}
393EXPORT_SYMBOL(ieee80211_hdrlen);
394
395unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
396{
397 const struct ieee80211_hdr *hdr =
398 (const struct ieee80211_hdr *)skb->data;
399 unsigned int hdrlen;
400
401 if (unlikely(skb->len < 10))
402 return 0;
403 hdrlen = ieee80211_hdrlen(hdr->frame_control);
404 if (unlikely(hdrlen > skb->len))
405 return 0;
406 return hdrlen;
407}
408EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
409
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100410static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
Zhu Yie31a16d2009-05-21 21:47:03 +0800411{
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100412 int ae = flags & MESH_FLAGS_AE;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200413 /* 802.11-2012, 8.2.4.7.3 */
Zhu Yie31a16d2009-05-21 21:47:03 +0800414 switch (ae) {
Johannes Berg7dd111e2012-10-25 21:51:59 +0200415 default:
Zhu Yie31a16d2009-05-21 21:47:03 +0800416 case 0:
417 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700418 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800419 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700420 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800421 return 18;
Zhu Yie31a16d2009-05-21 21:47:03 +0800422 }
423}
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100424
425unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
426{
427 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
428}
Johannes Berg9b395bc2012-10-26 00:36:40 +0200429EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800430
Johannes Berg7f6990c2016-10-05 15:29:49 +0200431int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
432 const u8 *addr, enum nl80211_iftype iftype)
Zhu Yie31a16d2009-05-21 21:47:03 +0800433{
434 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100435 struct {
436 u8 hdr[ETH_ALEN] __aligned(2);
437 __be16 proto;
438 } payload;
439 struct ethhdr tmp;
440 u16 hdrlen;
441 u8 mesh_flags = 0;
Zhu Yie31a16d2009-05-21 21:47:03 +0800442
443 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
444 return -1;
445
446 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100447 if (skb->len < hdrlen + 8)
448 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800449
450 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
451 * header
452 * IEEE 802.11 address fields:
453 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
454 * 0 0 DA SA BSSID n/a
455 * 0 1 DA BSSID SA n/a
456 * 1 0 BSSID SA DA n/a
457 * 1 1 RA TA DA SA
458 */
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100459 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
460 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
461
462 if (iftype == NL80211_IFTYPE_MESH_POINT)
463 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
Zhu Yie31a16d2009-05-21 21:47:03 +0800464
Rajkumar Manoharan6568f8f2017-05-14 21:41:55 -0700465 mesh_flags &= MESH_FLAGS_AE;
466
Zhu Yie31a16d2009-05-21 21:47:03 +0800467 switch (hdr->frame_control &
468 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
469 case cpu_to_le16(IEEE80211_FCTL_TODS):
470 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200471 iftype != NL80211_IFTYPE_AP_VLAN &&
472 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800473 return -1;
474 break;
475 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
476 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543ee2009-11-10 20:10:05 +0100477 iftype != NL80211_IFTYPE_MESH_POINT &&
478 iftype != NL80211_IFTYPE_AP_VLAN &&
479 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800480 return -1;
481 if (iftype == NL80211_IFTYPE_MESH_POINT) {
Rajkumar Manoharan6568f8f2017-05-14 21:41:55 -0700482 if (mesh_flags == MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800483 return -1;
Rajkumar Manoharan6568f8f2017-05-14 21:41:55 -0700484 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800485 skb_copy_bits(skb, hdrlen +
486 offsetof(struct ieee80211s_hdr, eaddr1),
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100487 tmp.h_dest, 2 * ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800488 }
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100489 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
Zhu Yie31a16d2009-05-21 21:47:03 +0800490 }
491 break;
492 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700493 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200494 iftype != NL80211_IFTYPE_P2P_CLIENT &&
495 iftype != NL80211_IFTYPE_MESH_POINT) ||
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100496 (is_multicast_ether_addr(tmp.h_dest) &&
497 ether_addr_equal(tmp.h_source, addr)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800498 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700499 if (iftype == NL80211_IFTYPE_MESH_POINT) {
Rajkumar Manoharan6568f8f2017-05-14 21:41:55 -0700500 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800501 return -1;
Rajkumar Manoharan6568f8f2017-05-14 21:41:55 -0700502 if (mesh_flags == MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800503 skb_copy_bits(skb, hdrlen +
504 offsetof(struct ieee80211s_hdr, eaddr1),
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100505 tmp.h_source, ETH_ALEN);
506 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700507 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800508 break;
509 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300510 if (iftype != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100511 iftype != NL80211_IFTYPE_STATION &&
512 iftype != NL80211_IFTYPE_OCB)
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300513 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800514 break;
515 }
516
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100517 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
518 tmp.h_proto = payload.proto;
Zhu Yie31a16d2009-05-21 21:47:03 +0800519
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100520 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
521 tmp.h_proto != htons(ETH_P_AARP) &&
522 tmp.h_proto != htons(ETH_P_IPX)) ||
523 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800524 /* remove RFC1042 or Bridge-Tunnel encapsulation and
525 * replace EtherType */
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100526 hdrlen += ETH_ALEN + 2;
527 else
Felix Fietkauc0417782016-06-29 10:36:39 +0200528 tmp.h_proto = htons(skb->len - hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800529
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100530 pskb_pull(skb, hdrlen);
531
532 if (!ehdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800533 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100534 memcpy(ehdr, &tmp, sizeof(tmp));
535
Zhu Yie31a16d2009-05-21 21:47:03 +0800536 return 0;
537}
Johannes Berg7f6990c2016-10-05 15:29:49 +0200538EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800539
Felix Fietkau2b67f942016-02-08 14:34:42 +0100540static void
541__frame_add_frag(struct sk_buff *skb, struct page *page,
542 void *ptr, int len, int size)
543{
544 struct skb_shared_info *sh = skb_shinfo(skb);
545 int page_offset;
546
Felix Fietkau4a0a3382020-01-13 19:21:07 +0100547 get_page(page);
Felix Fietkau2b67f942016-02-08 14:34:42 +0100548 page_offset = ptr - page_address(page);
549 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
550}
551
552static void
553__ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
554 int offset, int len)
555{
556 struct skb_shared_info *sh = skb_shinfo(skb);
Matthias Kaehlcke4bb797a2017-04-13 10:05:04 -0700557 const skb_frag_t *frag = &sh->frags[0];
Felix Fietkau2b67f942016-02-08 14:34:42 +0100558 struct page *frag_page;
559 void *frag_ptr;
560 int frag_len, frag_size;
561 int head_size = skb->len - skb->data_len;
562 int cur_len;
563
564 frag_page = virt_to_head_page(skb->head);
565 frag_ptr = skb->data;
566 frag_size = head_size;
567
568 while (offset >= frag_size) {
569 offset -= frag_size;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100570 frag_page = skb_frag_page(frag);
571 frag_ptr = skb_frag_address(frag);
572 frag_size = skb_frag_size(frag);
Matthias Kaehlcke4bb797a2017-04-13 10:05:04 -0700573 frag++;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100574 }
575
576 frag_ptr += offset;
577 frag_len = frag_size - offset;
578
579 cur_len = min(len, frag_len);
580
581 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
582 len -= cur_len;
583
584 while (len > 0) {
Felix Fietkau2b67f942016-02-08 14:34:42 +0100585 frag_len = skb_frag_size(frag);
586 cur_len = min(len, frag_len);
587 __frame_add_frag(frame, skb_frag_page(frag),
588 skb_frag_address(frag), cur_len, frag_len);
589 len -= cur_len;
Matthias Kaehlcke4bb797a2017-04-13 10:05:04 -0700590 frag++;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100591 }
592}
593
Felix Fietkau230fd282016-02-02 14:39:10 +0100594static struct sk_buff *
595__ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
Felix Fietkau2b67f942016-02-08 14:34:42 +0100596 int offset, int len, bool reuse_frag)
Felix Fietkau230fd282016-02-02 14:39:10 +0100597{
598 struct sk_buff *frame;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100599 int cur_len = len;
Felix Fietkau230fd282016-02-02 14:39:10 +0100600
601 if (skb->len - offset < len)
602 return NULL;
603
604 /*
Felix Fietkau2b67f942016-02-08 14:34:42 +0100605 * When reusing framents, copy some data to the head to simplify
606 * ethernet header handling and speed up protocol header processing
607 * in the stack later.
608 */
609 if (reuse_frag)
610 cur_len = min_t(int, len, 32);
611
612 /*
Felix Fietkau230fd282016-02-02 14:39:10 +0100613 * Allocate and reserve two bytes more for payload
614 * alignment since sizeof(struct ethhdr) is 14.
615 */
Felix Fietkau2b67f942016-02-08 14:34:42 +0100616 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
Gregory Greenman16a910a2016-07-05 15:23:10 +0300617 if (!frame)
618 return NULL;
Felix Fietkau230fd282016-02-02 14:39:10 +0100619
620 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
Felix Fietkau2b67f942016-02-08 14:34:42 +0100621 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
622
623 len -= cur_len;
624 if (!len)
625 return frame;
626
627 offset += cur_len;
628 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
Felix Fietkau230fd282016-02-02 14:39:10 +0100629
630 return frame;
631}
Zhu Yieaf85ca2009-12-01 10:18:37 +0800632
633void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
634 const u8 *addr, enum nl80211_iftype iftype,
Johannes Berg8b935ee2016-10-05 16:17:01 +0200635 const unsigned int extra_headroom,
636 const u8 *check_da, const u8 *check_sa)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800637{
Felix Fietkau230fd282016-02-02 14:39:10 +0100638 unsigned int hlen = ALIGN(extra_headroom, 4);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800639 struct sk_buff *frame = NULL;
640 u16 ethertype;
641 u8 *payload;
Johannes Berg7f6990c2016-10-05 15:29:49 +0200642 int offset = 0, remaining;
Felix Fietkau230fd282016-02-02 14:39:10 +0100643 struct ethhdr eth;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100644 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
Felix Fietkau2bf0ccc2016-02-08 14:25:26 +0100645 bool reuse_skb = false;
Felix Fietkau230fd282016-02-02 14:39:10 +0100646 bool last = false;
Felix Fietkau88665f52016-02-02 14:39:08 +0100647
Felix Fietkau230fd282016-02-02 14:39:10 +0100648 while (!last) {
649 unsigned int subframe_len;
650 int len;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800651 u8 padding;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800652
Felix Fietkau230fd282016-02-02 14:39:10 +0100653 skb_copy_bits(skb, offset, &eth, sizeof(eth));
654 len = ntohs(eth.h_proto);
655 subframe_len = sizeof(struct ethhdr) + len;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800656 padding = (4 - subframe_len) & 0x3;
Felix Fietkau230fd282016-02-02 14:39:10 +0100657
Zhu Yieaf85ca2009-12-01 10:18:37 +0800658 /* the last MSDU has no padding */
Felix Fietkau230fd282016-02-02 14:39:10 +0100659 remaining = skb->len - offset;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800660 if (subframe_len > remaining)
661 goto purge;
662
Felix Fietkau230fd282016-02-02 14:39:10 +0100663 offset += sizeof(struct ethhdr);
Felix Fietkau230fd282016-02-02 14:39:10 +0100664 last = remaining <= subframe_len + padding;
Johannes Berg8b935ee2016-10-05 16:17:01 +0200665
666 /* FIXME: should we really accept multicast DA? */
667 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
668 !ether_addr_equal(check_da, eth.h_dest)) ||
669 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
670 offset += len + padding;
671 continue;
672 }
673
674 /* reuse skb for the last subframe */
Felix Fietkau2b67f942016-02-08 14:34:42 +0100675 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
Felix Fietkau230fd282016-02-02 14:39:10 +0100676 skb_pull(skb, offset);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800677 frame = skb;
Felix Fietkau230fd282016-02-02 14:39:10 +0100678 reuse_skb = true;
679 } else {
Felix Fietkau2b67f942016-02-08 14:34:42 +0100680 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
681 reuse_frag);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800682 if (!frame)
683 goto purge;
684
Felix Fietkau230fd282016-02-02 14:39:10 +0100685 offset += len + padding;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800686 }
687
688 skb_reset_network_header(frame);
689 frame->dev = skb->dev;
690 frame->priority = skb->priority;
691
692 payload = frame->data;
693 ethertype = (payload[6] << 8) | payload[7];
Joe Perchesac422d32012-05-08 18:56:55 +0000694 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yieaf85ca2009-12-01 10:18:37 +0800695 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perchesac422d32012-05-08 18:56:55 +0000696 ether_addr_equal(payload, bridge_tunnel_header))) {
Felix Fietkau230fd282016-02-02 14:39:10 +0100697 eth.h_proto = htons(ethertype);
698 skb_pull(frame, ETH_ALEN + 2);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800699 }
Felix Fietkau230fd282016-02-02 14:39:10 +0100700
701 memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
Zhu Yieaf85ca2009-12-01 10:18:37 +0800702 __skb_queue_tail(list, frame);
703 }
704
Felix Fietkau230fd282016-02-02 14:39:10 +0100705 if (!reuse_skb)
706 dev_kfree_skb(skb);
707
Zhu Yieaf85ca2009-12-01 10:18:37 +0800708 return;
709
710 purge:
711 __skb_queue_purge(list);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800712 dev_kfree_skb(skb);
713}
714EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
715
Zhu Yie31a16d2009-05-21 21:47:03 +0800716/* Given a data frame determine the 802.1p/1d tag to use. */
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800717unsigned int cfg80211_classify8021d(struct sk_buff *skb,
718 struct cfg80211_qos_map *qos_map)
Zhu Yie31a16d2009-05-21 21:47:03 +0800719{
720 unsigned int dscp;
cedric Vonckenc6ca5e22013-08-26 14:04:52 +0200721 unsigned char vlan_priority;
Zhu Yie31a16d2009-05-21 21:47:03 +0800722
723 /* skb->priority values from 256->263 are magic values to
724 * directly indicate a specific 802.1d priority. This is used
725 * to allow 802.1d priority to be passed directly in from VLAN
726 * tags, etc.
727 */
728 if (skb->priority >= 256 && skb->priority <= 263)
729 return skb->priority - 256;
730
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;
734 if (vlan_priority > 0)
735 return vlan_priority;
736 }
737
Zhu Yie31a16d2009-05-21 21:47:03 +0800738 switch (skb->protocol) {
739 case htons(ETH_P_IP):
Dave Tähtb1565792011-12-22 12:55:08 -0800740 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
741 break;
742 case htons(ETH_P_IPV6):
743 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
Zhu Yie31a16d2009-05-21 21:47:03 +0800744 break;
Simon Wunderlich960d97f2014-03-03 17:23:12 +0100745 case htons(ETH_P_MPLS_UC):
746 case htons(ETH_P_MPLS_MC): {
747 struct mpls_label mpls_tmp, *mpls;
748
749 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
750 sizeof(*mpls), &mpls_tmp);
751 if (!mpls)
752 return 0;
753
754 return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
755 >> MPLS_LS_TC_SHIFT;
756 }
757 case htons(ETH_P_80221):
758 /* 802.21 is always network control traffic */
759 return 7;
Zhu Yie31a16d2009-05-21 21:47:03 +0800760 default:
761 return 0;
762 }
763
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800764 if (qos_map) {
765 unsigned int i, tmp_dscp = dscp >> 2;
766
767 for (i = 0; i < qos_map->num_des; i++) {
768 if (tmp_dscp == qos_map->dscp_exception[i].dscp)
769 return qos_map->dscp_exception[i].up;
770 }
771
772 for (i = 0; i < 8; i++) {
773 if (tmp_dscp >= qos_map->up[i].low &&
774 tmp_dscp <= qos_map->up[i].high)
775 return i;
776 }
777 }
778
Zhu Yie31a16d2009-05-21 21:47:03 +0800779 return dscp >> 5;
780}
781EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200782
783const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
784{
Johannes Berg9caf0362012-11-29 01:25:20 +0100785 const struct cfg80211_bss_ies *ies;
786
787 ies = rcu_dereference(bss->ies);
788 if (!ies)
Johannes Berg517357c2009-07-02 17:18:40 +0200789 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +0100790
791 return cfg80211_find_ie(ie, ies->data, ies->len);
Johannes Berg517357c2009-07-02 17:18:40 +0200792}
793EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200794
795void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
796{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800797 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200798 struct net_device *dev = wdev->netdev;
799 int i;
800
801 if (!wdev->connect_keys)
802 return;
803
David Spinadelb8676222016-09-22 23:16:50 +0300804 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200805 if (!wdev->connect_keys->params[i].cipher)
806 continue;
Hila Gonene35e4d22012-06-27 17:19:42 +0300807 if (rdev_add_key(rdev, dev, i, false, NULL,
808 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800809 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800810 continue;
811 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200812 if (wdev->connect_keys->def == i)
Hila Gonene35e4d22012-06-27 17:19:42 +0300813 if (rdev_set_default_key(rdev, dev, i, true, true)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800814 netdev_err(dev, "failed to set defkey %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800815 continue;
816 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200817 }
818
Johannes Bergb47f6102014-09-10 13:39:54 +0300819 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200820 wdev->connect_keys = NULL;
821}
Johannes Berg3d54d252009-08-21 14:51:05 +0200822
Daniel Drake1f6fc432012-08-02 18:41:48 +0100823void cfg80211_process_wdev_events(struct wireless_dev *wdev)
Johannes Berg3d54d252009-08-21 14:51:05 +0200824{
825 struct cfg80211_event *ev;
826 unsigned long flags;
Johannes Berg3d54d252009-08-21 14:51:05 +0200827
828 spin_lock_irqsave(&wdev->event_lock, flags);
829 while (!list_empty(&wdev->event_list)) {
830 ev = list_first_entry(&wdev->event_list,
831 struct cfg80211_event, list);
832 list_del(&ev->list);
833 spin_unlock_irqrestore(&wdev->event_lock, flags);
834
835 wdev_lock(wdev);
836 switch (ev->type) {
837 case EVENT_CONNECT_RESULT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200838 __cfg80211_connect_result(
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300839 wdev->netdev,
840 &ev->cr,
841 ev->cr.status == WLAN_STATUS_SUCCESS);
Johannes Berg3d54d252009-08-21 14:51:05 +0200842 break;
843 case EVENT_ROAMED:
Avraham Stern9e841a62017-04-26 10:58:49 +0300844 __cfg80211_roamed(wdev, &ev->rm);
Johannes Berg3d54d252009-08-21 14:51:05 +0200845 break;
846 case EVENT_DISCONNECTED:
847 __cfg80211_disconnected(wdev->netdev,
848 ev->dc.ie, ev->dc.ie_len,
Johannes Berg80279fb2015-05-22 16:22:20 +0200849 ev->dc.reason,
850 !ev->dc.locally_generated);
Johannes Berg3d54d252009-08-21 14:51:05 +0200851 break;
852 case EVENT_IBSS_JOINED:
Antonio Quartullife94f3a2014-01-29 17:53:43 +0100853 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
854 ev->ij.channel);
Johannes Berg3d54d252009-08-21 14:51:05 +0200855 break;
Michal Kaziorf04c2202014-04-09 15:11:01 +0200856 case EVENT_STOPPED:
857 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
858 break;
Johannes Berg3d54d252009-08-21 14:51:05 +0200859 }
860 wdev_unlock(wdev);
861
862 kfree(ev);
863
864 spin_lock_irqsave(&wdev->event_lock, flags);
865 }
866 spin_unlock_irqrestore(&wdev->event_lock, flags);
867}
868
869void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
870{
871 struct wireless_dev *wdev;
872
873 ASSERT_RTNL();
Johannes Berg3d54d252009-08-21 14:51:05 +0200874
Johannes Berg53873f12016-05-03 16:52:04 +0300875 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
Johannes Berg3d54d252009-08-21 14:51:05 +0200876 cfg80211_process_wdev_events(wdev);
Johannes Berg3d54d252009-08-21 14:51:05 +0200877}
878
879int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
880 struct net_device *dev, enum nl80211_iftype ntype,
881 u32 *flags, struct vif_params *params)
882{
883 int err;
884 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
885
Zhao, Gang73fb08e2014-03-19 17:04:36 +0800886 ASSERT_RTNL();
Johannes Berg3d54d252009-08-21 14:51:05 +0200887
888 /* don't support changing VLANs, you just re-create them */
889 if (otype == NL80211_IFTYPE_AP_VLAN)
890 return -EOPNOTSUPP;
891
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300892 /* cannot change into P2P device or NAN */
893 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
894 ntype == NL80211_IFTYPE_NAN)
Johannes Berg98104fde2012-06-16 00:19:54 +0200895 return -EOPNOTSUPP;
896
Johannes Berg3d54d252009-08-21 14:51:05 +0200897 if (!rdev->ops->change_virtual_intf ||
898 !(rdev->wiphy.interface_modes & (1 << ntype)))
899 return -EOPNOTSUPP;
900
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100901 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000902 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200903 (ntype == NL80211_IFTYPE_ADHOC ||
904 ntype == NL80211_IFTYPE_STATION ||
905 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100906 return -EBUSY;
907
Michal Kazior6cbfb1b2015-05-22 10:57:22 +0200908 if (ntype != otype) {
Johannes Berg9bc383d2009-11-19 11:55:19 +0100909 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100910 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg194ff522013-12-30 23:12:37 +0100911 wdev_lock(dev->ieee80211_ptr);
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800912 rdev_set_qos_map(rdev, dev, NULL);
Johannes Berg194ff522013-12-30 23:12:37 +0100913 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg9bc383d2009-11-19 11:55:19 +0100914
Johannes Berg3d54d252009-08-21 14:51:05 +0200915 switch (otype) {
Michal Kaziorac800142012-06-29 12:46:57 +0200916 case NL80211_IFTYPE_AP:
Ilan Peer7c8d5e02014-02-25 15:33:38 +0200917 cfg80211_stop_ap(rdev, dev, true);
Michal Kaziorac800142012-06-29 12:46:57 +0200918 break;
Johannes Berg3d54d252009-08-21 14:51:05 +0200919 case NL80211_IFTYPE_ADHOC:
920 cfg80211_leave_ibss(rdev, dev, false);
921 break;
922 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200923 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg83739b02013-05-15 17:44:01 +0200924 wdev_lock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +0200925 cfg80211_disconnect(rdev, dev,
926 WLAN_REASON_DEAUTH_LEAVING, true);
Johannes Berg83739b02013-05-15 17:44:01 +0200927 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +0200928 break;
929 case NL80211_IFTYPE_MESH_POINT:
930 /* mesh should be handled? */
931 break;
932 default:
933 break;
934 }
935
936 cfg80211_process_rdev_events(rdev);
Denis Kenziorc71213b2019-08-28 16:11:10 -0500937 cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +0200938 }
939
Hila Gonene35e4d22012-06-27 17:19:42 +0300940 err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
Johannes Berg3d54d252009-08-21 14:51:05 +0200941
942 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
943
Johannes Berg9bc383d2009-11-19 11:55:19 +0100944 if (!err && params && params->use_4addr != -1)
945 dev->ieee80211_ptr->use_4addr = params->use_4addr;
946
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100947 if (!err) {
948 dev->priv_flags &= ~IFF_DONT_BRIDGE;
949 switch (ntype) {
950 case NL80211_IFTYPE_STATION:
951 if (dev->ieee80211_ptr->use_4addr)
952 break;
953 /* fall through */
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100954 case NL80211_IFTYPE_OCB:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200955 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100956 case NL80211_IFTYPE_ADHOC:
957 dev->priv_flags |= IFF_DONT_BRIDGE;
958 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200959 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100960 case NL80211_IFTYPE_AP:
961 case NL80211_IFTYPE_AP_VLAN:
962 case NL80211_IFTYPE_WDS:
963 case NL80211_IFTYPE_MESH_POINT:
964 /* bridging OK */
965 break;
966 case NL80211_IFTYPE_MONITOR:
967 /* monitor can't bridge anyway */
968 break;
969 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200970 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100971 /* not happening */
972 break;
Johannes Berg98104fde2012-06-16 00:19:54 +0200973 case NL80211_IFTYPE_P2P_DEVICE:
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300974 case NL80211_IFTYPE_NAN:
Johannes Berg98104fde2012-06-16 00:19:54 +0200975 WARN_ON(1);
976 break;
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100977 }
978 }
979
Michal Kaziordbbae262012-06-29 12:47:01 +0200980 if (!err && ntype != otype && netif_running(dev)) {
981 cfg80211_update_iface_num(rdev, ntype, 1);
982 cfg80211_update_iface_num(rdev, otype, -1);
983 }
984
Johannes Berg3d54d252009-08-21 14:51:05 +0200985 return err;
986}
John W. Linville254416a2009-12-09 16:43:52 -0500987
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +0300988static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
989{
990 static const u32 __mcs2bitrate[] = {
991 /* control PHY */
992 [0] = 275,
993 /* SC PHY */
994 [1] = 3850,
995 [2] = 7700,
996 [3] = 9625,
997 [4] = 11550,
998 [5] = 12512, /* 1251.25 mbps */
999 [6] = 15400,
1000 [7] = 19250,
1001 [8] = 23100,
1002 [9] = 25025,
1003 [10] = 30800,
1004 [11] = 38500,
1005 [12] = 46200,
1006 /* OFDM PHY */
1007 [13] = 6930,
1008 [14] = 8662, /* 866.25 mbps */
1009 [15] = 13860,
1010 [16] = 17325,
1011 [17] = 20790,
1012 [18] = 27720,
1013 [19] = 34650,
1014 [20] = 41580,
1015 [21] = 45045,
1016 [22] = 51975,
1017 [23] = 62370,
1018 [24] = 67568, /* 6756.75 mbps */
1019 /* LP-SC PHY */
1020 [25] = 6260,
1021 [26] = 8340,
1022 [27] = 11120,
1023 [28] = 12510,
1024 [29] = 16680,
1025 [30] = 22240,
1026 [31] = 25030,
1027 };
1028
1029 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1030 return 0;
1031
1032 return __mcs2bitrate[rate->mcs];
1033}
1034
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001035static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1036{
1037 static const u32 base[4][10] = {
1038 { 6500000,
1039 13000000,
1040 19500000,
1041 26000000,
1042 39000000,
1043 52000000,
1044 58500000,
1045 65000000,
1046 78000000,
Pedersen, Thomas8fdd1362016-10-31 11:28:40 -07001047 /* not in the spec, but some devices use this: */
1048 86500000,
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001049 },
1050 { 13500000,
1051 27000000,
1052 40500000,
1053 54000000,
1054 81000000,
1055 108000000,
1056 121500000,
1057 135000000,
1058 162000000,
1059 180000000,
1060 },
1061 { 29300000,
1062 58500000,
1063 87800000,
1064 117000000,
1065 175500000,
1066 234000000,
1067 263300000,
1068 292500000,
1069 351000000,
1070 390000000,
1071 },
1072 { 58500000,
1073 117000000,
1074 175500000,
1075 234000000,
1076 351000000,
1077 468000000,
1078 526500000,
1079 585000000,
1080 702000000,
1081 780000000,
1082 },
1083 };
1084 u32 bitrate;
1085 int idx;
1086
1087 if (WARN_ON_ONCE(rate->mcs > 9))
1088 return 0;
1089
Johannes Bergb51f3be2015-01-15 16:14:02 +01001090 switch (rate->bw) {
1091 case RATE_INFO_BW_160:
1092 idx = 3;
1093 break;
1094 case RATE_INFO_BW_80:
1095 idx = 2;
1096 break;
1097 case RATE_INFO_BW_40:
1098 idx = 1;
1099 break;
1100 case RATE_INFO_BW_5:
1101 case RATE_INFO_BW_10:
1102 default:
1103 WARN_ON(1);
1104 /* fall through */
1105 case RATE_INFO_BW_20:
1106 idx = 0;
1107 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001108
1109 bitrate = base[idx][rate->mcs];
1110 bitrate *= rate->nss;
1111
1112 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1113 bitrate = (bitrate / 9) * 10;
1114
1115 /* do NOT round down here */
1116 return (bitrate + 50000) / 100000;
1117}
1118
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03001119u32 cfg80211_calculate_bitrate(struct rate_info *rate)
John W. Linville254416a2009-12-09 16:43:52 -05001120{
1121 int modulation, streams, bitrate;
1122
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001123 if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
1124 !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
John W. Linville254416a2009-12-09 16:43:52 -05001125 return rate->legacy;
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +03001126 if (rate->flags & RATE_INFO_FLAGS_60G)
1127 return cfg80211_calculate_bitrate_60g(rate);
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001128 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1129 return cfg80211_calculate_bitrate_vht(rate);
John W. Linville254416a2009-12-09 16:43:52 -05001130
1131 /* the formula below does only work for MCS values smaller than 32 */
Johannes Berg2615f372012-05-08 21:36:20 +02001132 if (WARN_ON_ONCE(rate->mcs >= 32))
John W. Linville254416a2009-12-09 16:43:52 -05001133 return 0;
1134
1135 modulation = rate->mcs & 7;
1136 streams = (rate->mcs >> 3) + 1;
1137
Johannes Bergb51f3be2015-01-15 16:14:02 +01001138 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
John W. Linville254416a2009-12-09 16:43:52 -05001139
1140 if (modulation < 4)
1141 bitrate *= (modulation + 1);
1142 else if (modulation == 4)
1143 bitrate *= (modulation + 2);
1144 else
1145 bitrate *= (modulation + 3);
1146
1147 bitrate *= streams;
1148
1149 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1150 bitrate = (bitrate / 9) * 10;
1151
1152 /* do NOT round down here */
1153 return (bitrate + 50000) / 100000;
1154}
Thomas Pedersen8097e142012-03-05 15:31:47 -08001155EXPORT_SYMBOL(cfg80211_calculate_bitrate);
Johannes Berg56d18932011-05-09 18:41:15 +02001156
Arend van Sprielc216e642012-11-25 19:13:28 +01001157int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1158 enum ieee80211_p2p_attr_id attr,
1159 u8 *buf, unsigned int bufsize)
Johannes Berg0ee45352012-10-29 19:48:40 +01001160{
1161 u8 *out = buf;
1162 u16 attr_remaining = 0;
1163 bool desired_attr = false;
1164 u16 desired_len = 0;
1165
1166 while (len > 0) {
1167 unsigned int iedatalen;
1168 unsigned int copy;
1169 const u8 *iedata;
1170
1171 if (len < 2)
1172 return -EILSEQ;
1173 iedatalen = ies[1];
1174 if (iedatalen + 2 > len)
1175 return -EILSEQ;
1176
1177 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1178 goto cont;
1179
1180 if (iedatalen < 4)
1181 goto cont;
1182
1183 iedata = ies + 2;
1184
1185 /* check WFA OUI, P2P subtype */
1186 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1187 iedata[2] != 0x9a || iedata[3] != 0x09)
1188 goto cont;
1189
1190 iedatalen -= 4;
1191 iedata += 4;
1192
1193 /* check attribute continuation into this IE */
1194 copy = min_t(unsigned int, attr_remaining, iedatalen);
1195 if (copy && desired_attr) {
1196 desired_len += copy;
1197 if (out) {
1198 memcpy(out, iedata, min(bufsize, copy));
1199 out += min(bufsize, copy);
1200 bufsize -= min(bufsize, copy);
1201 }
1202
1203
1204 if (copy == attr_remaining)
1205 return desired_len;
1206 }
1207
1208 attr_remaining -= copy;
1209 if (attr_remaining)
1210 goto cont;
1211
1212 iedatalen -= copy;
1213 iedata += copy;
1214
1215 while (iedatalen > 0) {
1216 u16 attr_len;
1217
1218 /* P2P attribute ID & size must fit */
1219 if (iedatalen < 3)
1220 return -EILSEQ;
1221 desired_attr = iedata[0] == attr;
1222 attr_len = get_unaligned_le16(iedata + 1);
1223 iedatalen -= 3;
1224 iedata += 3;
1225
1226 copy = min_t(unsigned int, attr_len, iedatalen);
1227
1228 if (desired_attr) {
1229 desired_len += copy;
1230 if (out) {
1231 memcpy(out, iedata, min(bufsize, copy));
1232 out += min(bufsize, copy);
1233 bufsize -= min(bufsize, copy);
1234 }
1235
1236 if (copy == attr_len)
1237 return desired_len;
1238 }
1239
1240 iedata += copy;
1241 iedatalen -= copy;
1242 attr_remaining = attr_len - copy;
1243 }
1244
1245 cont:
1246 len -= ies[1] + 2;
1247 ies += ies[1] + 2;
1248 }
1249
1250 if (attr_remaining && desired_attr)
1251 return -EILSEQ;
1252
1253 return -ENOENT;
1254}
1255EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1256
Johannes Berg29464cc2015-03-31 15:36:22 +02001257static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1258{
1259 int i;
1260
1261 for (i = 0; i < n_ids; i++)
1262 if (ids[i] == id)
1263 return true;
1264 return false;
1265}
1266
1267size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1268 const u8 *ids, int n_ids,
1269 const u8 *after_ric, int n_after_ric,
1270 size_t offset)
1271{
1272 size_t pos = offset;
1273
1274 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos])) {
1275 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1276 pos += 2 + ies[pos + 1];
1277
1278 while (pos < ielen &&
1279 !ieee80211_id_in_list(after_ric, n_after_ric,
1280 ies[pos]))
1281 pos += 2 + ies[pos + 1];
1282 } else {
1283 pos += 2 + ies[pos + 1];
1284 }
1285 }
1286
1287 return pos;
1288}
1289EXPORT_SYMBOL(ieee80211_ie_split_ric);
1290
Johannes Berg1ce3e822012-08-01 17:00:55 +02001291bool ieee80211_operating_class_to_band(u8 operating_class,
Johannes Berg57fbcce2016-04-12 15:56:15 +02001292 enum nl80211_band *band)
Johannes Berg1ce3e822012-08-01 17:00:55 +02001293{
1294 switch (operating_class) {
1295 case 112:
1296 case 115 ... 127:
Eliad Peller954a86e2015-03-01 09:10:01 +02001297 case 128 ... 130:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001298 *band = NL80211_BAND_5GHZ;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001299 return true;
1300 case 81:
1301 case 82:
1302 case 83:
1303 case 84:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001304 *band = NL80211_BAND_2GHZ;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001305 return true;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001306 case 180:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001307 *band = NL80211_BAND_60GHZ;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001308 return true;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001309 }
1310
1311 return false;
1312}
1313EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1314
Arik Nemtsova38700d2015-03-18 08:46:08 +02001315bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1316 u8 *op_class)
1317{
1318 u8 vht_opclass;
Dan Carpenterb699bcb2018-08-31 11:10:55 +03001319 u32 freq = chandef->center_freq1;
Arik Nemtsova38700d2015-03-18 08:46:08 +02001320
1321 if (freq >= 2412 && freq <= 2472) {
1322 if (chandef->width > NL80211_CHAN_WIDTH_40)
1323 return false;
1324
1325 /* 2.407 GHz, channels 1..13 */
1326 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1327 if (freq > chandef->chan->center_freq)
1328 *op_class = 83; /* HT40+ */
1329 else
1330 *op_class = 84; /* HT40- */
1331 } else {
1332 *op_class = 81;
1333 }
1334
1335 return true;
1336 }
1337
1338 if (freq == 2484) {
1339 if (chandef->width > NL80211_CHAN_WIDTH_40)
1340 return false;
1341
1342 *op_class = 82; /* channel 14 */
1343 return true;
1344 }
1345
1346 switch (chandef->width) {
1347 case NL80211_CHAN_WIDTH_80:
1348 vht_opclass = 128;
1349 break;
1350 case NL80211_CHAN_WIDTH_160:
1351 vht_opclass = 129;
1352 break;
1353 case NL80211_CHAN_WIDTH_80P80:
1354 vht_opclass = 130;
1355 break;
1356 case NL80211_CHAN_WIDTH_10:
1357 case NL80211_CHAN_WIDTH_5:
1358 return false; /* unsupported for now */
1359 default:
1360 vht_opclass = 0;
1361 break;
1362 }
1363
1364 /* 5 GHz, channels 36..48 */
1365 if (freq >= 5180 && freq <= 5240) {
1366 if (vht_opclass) {
1367 *op_class = vht_opclass;
1368 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1369 if (freq > chandef->chan->center_freq)
1370 *op_class = 116;
1371 else
1372 *op_class = 117;
1373 } else {
1374 *op_class = 115;
1375 }
1376
1377 return true;
1378 }
1379
1380 /* 5 GHz, channels 52..64 */
1381 if (freq >= 5260 && freq <= 5320) {
1382 if (vht_opclass) {
1383 *op_class = vht_opclass;
1384 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1385 if (freq > chandef->chan->center_freq)
1386 *op_class = 119;
1387 else
1388 *op_class = 120;
1389 } else {
1390 *op_class = 118;
1391 }
1392
1393 return true;
1394 }
1395
1396 /* 5 GHz, channels 100..144 */
1397 if (freq >= 5500 && freq <= 5720) {
1398 if (vht_opclass) {
1399 *op_class = vht_opclass;
1400 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1401 if (freq > chandef->chan->center_freq)
1402 *op_class = 122;
1403 else
1404 *op_class = 123;
1405 } else {
1406 *op_class = 121;
1407 }
1408
1409 return true;
1410 }
1411
1412 /* 5 GHz, channels 149..169 */
1413 if (freq >= 5745 && freq <= 5845) {
1414 if (vht_opclass) {
1415 *op_class = vht_opclass;
1416 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1417 if (freq > chandef->chan->center_freq)
1418 *op_class = 126;
1419 else
1420 *op_class = 127;
1421 } else if (freq <= 5805) {
1422 *op_class = 124;
1423 } else {
1424 *op_class = 125;
1425 }
1426
1427 return true;
1428 }
1429
1430 /* 56.16 GHz, channel 1..4 */
1431 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
1432 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1433 return false;
1434
1435 *op_class = 180;
1436 return true;
1437 }
1438
1439 /* not supported yet */
1440 return false;
1441}
1442EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1443
Johannes Berg654c3b92016-10-21 14:25:13 +02001444static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1445 u32 *beacon_int_gcd,
1446 bool *beacon_int_different)
1447{
1448 struct wireless_dev *wdev;
1449
1450 *beacon_int_gcd = 0;
1451 *beacon_int_different = false;
1452
1453 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1454 if (!wdev->beacon_interval)
1455 continue;
1456
1457 if (!*beacon_int_gcd) {
1458 *beacon_int_gcd = wdev->beacon_interval;
1459 continue;
1460 }
1461
1462 if (wdev->beacon_interval == *beacon_int_gcd)
1463 continue;
1464
1465 *beacon_int_different = true;
1466 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1467 }
1468
1469 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1470 if (*beacon_int_gcd)
1471 *beacon_int_different = true;
1472 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1473 }
1474}
1475
Johannes Berg56d18932011-05-09 18:41:15 +02001476int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
Purushottam Kushwahac6800ff2016-10-12 18:26:51 +05301477 enum nl80211_iftype iftype, u32 beacon_int)
Johannes Berg56d18932011-05-09 18:41:15 +02001478{
Johannes Berg654c3b92016-10-21 14:25:13 +02001479 /*
1480 * This is just a basic pre-condition check; if interface combinations
1481 * are possible the driver must already be checking those with a call
1482 * to cfg80211_check_combinations(), in which case we'll validate more
1483 * through the cfg80211_calculate_bi_data() call and code in
1484 * cfg80211_iter_combinations().
1485 */
Johannes Berg56d18932011-05-09 18:41:15 +02001486
Purushottam Kushwaha12d20fc92016-08-11 15:14:02 +05301487 if (beacon_int < 10 || beacon_int > 10000)
Johannes Berg56d18932011-05-09 18:41:15 +02001488 return -EINVAL;
1489
Johannes Berg654c3b92016-10-21 14:25:13 +02001490 return 0;
Johannes Berg56d18932011-05-09 18:41:15 +02001491}
Johannes Berg7527a782011-05-13 10:58:57 +02001492
Michal Kazior65a124d2014-04-09 15:29:22 +02001493int cfg80211_iter_combinations(struct wiphy *wiphy,
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301494 struct iface_combination_params *params,
Michal Kazior65a124d2014-04-09 15:29:22 +02001495 void (*iter)(const struct ieee80211_iface_combination *c,
1496 void *data),
1497 void *data)
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001498{
Felix Fietkau8c48b502014-05-05 11:48:40 +02001499 const struct ieee80211_regdomain *regdom;
1500 enum nl80211_dfs_regions region = 0;
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001501 int i, j, iftype;
1502 int num_interfaces = 0;
1503 u32 used_iftypes = 0;
Johannes Berg654c3b92016-10-21 14:25:13 +02001504 u32 beacon_int_gcd;
1505 bool beacon_int_different;
1506
1507 /*
1508 * This is a bit strange, since the iteration used to rely only on
1509 * the data given by the driver, but here it now relies on context,
1510 * in form of the currently operating interfaces.
1511 * This is OK for all current users, and saves us from having to
1512 * push the GCD calculations into all the drivers.
1513 * In the future, this should probably rely more on data that's in
1514 * cfg80211 already - the only thing not would appear to be any new
1515 * interfaces (while being brought up) and channel/radar data.
1516 */
1517 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1518 &beacon_int_gcd, &beacon_int_different);
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001519
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301520 if (params->radar_detect) {
Felix Fietkau8c48b502014-05-05 11:48:40 +02001521 rcu_read_lock();
1522 regdom = rcu_dereference(cfg80211_regdomain);
1523 if (regdom)
1524 region = regdom->dfs_region;
1525 rcu_read_unlock();
1526 }
1527
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001528 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301529 num_interfaces += params->iftype_num[iftype];
1530 if (params->iftype_num[iftype] > 0 &&
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001531 !(wiphy->software_iftypes & BIT(iftype)))
1532 used_iftypes |= BIT(iftype);
1533 }
1534
1535 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1536 const struct ieee80211_iface_combination *c;
1537 struct ieee80211_iface_limit *limits;
1538 u32 all_iftypes = 0;
1539
1540 c = &wiphy->iface_combinations[i];
1541
1542 if (num_interfaces > c->max_interfaces)
1543 continue;
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301544 if (params->num_different_channels > c->num_different_channels)
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001545 continue;
1546
1547 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1548 GFP_KERNEL);
1549 if (!limits)
1550 return -ENOMEM;
1551
1552 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1553 if (wiphy->software_iftypes & BIT(iftype))
1554 continue;
1555 for (j = 0; j < c->n_limits; j++) {
1556 all_iftypes |= limits[j].types;
1557 if (!(limits[j].types & BIT(iftype)))
1558 continue;
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301559 if (limits[j].max < params->iftype_num[iftype])
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001560 goto cont;
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301561 limits[j].max -= params->iftype_num[iftype];
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001562 }
1563 }
1564
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301565 if (params->radar_detect !=
1566 (c->radar_detect_widths & params->radar_detect))
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001567 goto cont;
1568
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301569 if (params->radar_detect && c->radar_detect_regions &&
Felix Fietkau8c48b502014-05-05 11:48:40 +02001570 !(c->radar_detect_regions & BIT(region)))
1571 goto cont;
1572
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001573 /* Finally check that all iftypes that we're currently
1574 * using are actually part of this combination. If they
1575 * aren't then we can't use this combination and have
1576 * to continue to the next.
1577 */
1578 if ((all_iftypes & used_iftypes) != used_iftypes)
1579 goto cont;
1580
Johannes Berg654c3b92016-10-21 14:25:13 +02001581 if (beacon_int_gcd) {
Purushottam Kushwahac6800ff2016-10-12 18:26:51 +05301582 if (c->beacon_int_min_gcd &&
Johannes Berg654c3b92016-10-21 14:25:13 +02001583 beacon_int_gcd < c->beacon_int_min_gcd)
Johannes Berg95b55f92016-10-21 12:15:00 +02001584 goto cont;
Johannes Berg654c3b92016-10-21 14:25:13 +02001585 if (!c->beacon_int_min_gcd && beacon_int_different)
Purushottam Kushwahac6800ff2016-10-12 18:26:51 +05301586 goto cont;
1587 }
1588
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001589 /* This combination covered all interface types and
1590 * supported the requested numbers, so we're good.
1591 */
Michal Kazior65a124d2014-04-09 15:29:22 +02001592
1593 (*iter)(c, data);
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001594 cont:
1595 kfree(limits);
1596 }
1597
Michal Kazior65a124d2014-04-09 15:29:22 +02001598 return 0;
1599}
1600EXPORT_SYMBOL(cfg80211_iter_combinations);
1601
1602static void
1603cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1604 void *data)
1605{
1606 int *num = data;
1607 (*num)++;
1608}
1609
1610int cfg80211_check_combinations(struct wiphy *wiphy,
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301611 struct iface_combination_params *params)
Michal Kazior65a124d2014-04-09 15:29:22 +02001612{
1613 int err, num = 0;
1614
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301615 err = cfg80211_iter_combinations(wiphy, params,
Michal Kazior65a124d2014-04-09 15:29:22 +02001616 cfg80211_iter_sum_ifcombs, &num);
1617 if (err)
1618 return err;
1619 if (num == 0)
1620 return -EBUSY;
1621
1622 return 0;
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001623}
1624EXPORT_SYMBOL(cfg80211_check_combinations);
1625
Johannes Berg34850ab2011-07-18 18:08:35 +02001626int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1627 const u8 *rates, unsigned int n_rates,
1628 u32 *mask)
1629{
1630 int i, j;
1631
Johannes Berga401d2b2011-07-20 00:52:16 +02001632 if (!sband)
1633 return -EINVAL;
1634
Johannes Berg34850ab2011-07-18 18:08:35 +02001635 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1636 return -EINVAL;
1637
1638 *mask = 0;
1639
1640 for (i = 0; i < n_rates; i++) {
1641 int rate = (rates[i] & 0x7f) * 5;
1642 bool found = false;
1643
1644 for (j = 0; j < sband->n_bitrates; j++) {
1645 if (sband->bitrates[j].bitrate == rate) {
1646 found = true;
1647 *mask |= BIT(j);
1648 break;
1649 }
1650 }
1651 if (!found)
1652 return -EINVAL;
1653 }
1654
1655 /*
1656 * mask must have at least one bit set here since we
1657 * didn't accept a 0-length rates array nor allowed
1658 * entries in the array that didn't exist
1659 */
1660
1661 return 0;
1662}
Johannes Berg11a2a352011-11-21 11:09:22 +01001663
Ilan Peerbdfbec22014-01-09 11:37:23 +02001664unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1665{
Johannes Berg57fbcce2016-04-12 15:56:15 +02001666 enum nl80211_band band;
Ilan Peerbdfbec22014-01-09 11:37:23 +02001667 unsigned int n_channels = 0;
1668
Johannes Berg57fbcce2016-04-12 15:56:15 +02001669 for (band = 0; band < NUM_NL80211_BANDS; band++)
Ilan Peerbdfbec22014-01-09 11:37:23 +02001670 if (wiphy->bands[band])
1671 n_channels += wiphy->bands[band]->n_channels;
1672
1673 return n_channels;
1674}
1675EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1676
Antonio Quartulli74063532014-05-19 21:53:21 +02001677int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1678 struct station_info *sinfo)
1679{
1680 struct cfg80211_registered_device *rdev;
1681 struct wireless_dev *wdev;
1682
1683 wdev = dev->ieee80211_ptr;
1684 if (!wdev)
1685 return -EOPNOTSUPP;
1686
1687 rdev = wiphy_to_rdev(wdev->wiphy);
1688 if (!rdev->ops->get_station)
1689 return -EOPNOTSUPP;
1690
1691 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1692}
1693EXPORT_SYMBOL(cfg80211_get_station);
1694
Ayala Bekera442b762016-09-20 17:31:15 +03001695void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1696{
1697 int i;
1698
1699 if (!f)
1700 return;
1701
1702 kfree(f->serv_spec_info);
1703 kfree(f->srf_bf);
1704 kfree(f->srf_macs);
1705 for (i = 0; i < f->num_rx_filters; i++)
1706 kfree(f->rx_filters[i].filter);
1707
1708 for (i = 0; i < f->num_tx_filters; i++)
1709 kfree(f->tx_filters[i].filter);
1710
1711 kfree(f->rx_filters);
1712 kfree(f->tx_filters);
1713 kfree(f);
1714}
1715EXPORT_SYMBOL(cfg80211_free_nan_func);
1716
Johannes Berg11a2a352011-11-21 11:09:22 +01001717/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1718/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1719const unsigned char rfc1042_header[] __aligned(2) =
1720 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1721EXPORT_SYMBOL(rfc1042_header);
1722
1723/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1724const unsigned char bridge_tunnel_header[] __aligned(2) =
1725 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1726EXPORT_SYMBOL(bridge_tunnel_header);
Johannes Berg7c62e272014-02-25 15:04:46 -08001727
1728bool cfg80211_is_gratuitous_arp_unsolicited_na(struct sk_buff *skb)
1729{
1730 const struct ethhdr *eth = (void *)skb->data;
1731 const struct {
1732 struct arphdr hdr;
1733 u8 ar_sha[ETH_ALEN];
1734 u8 ar_sip[4];
1735 u8 ar_tha[ETH_ALEN];
1736 u8 ar_tip[4];
1737 } __packed *arp;
1738 const struct ipv6hdr *ipv6;
1739 const struct icmp6hdr *icmpv6;
1740
1741 switch (eth->h_proto) {
1742 case cpu_to_be16(ETH_P_ARP):
1743 /* can't say - but will probably be dropped later anyway */
1744 if (!pskb_may_pull(skb, sizeof(*eth) + sizeof(*arp)))
1745 return false;
1746
1747 arp = (void *)(eth + 1);
1748
1749 if ((arp->hdr.ar_op == cpu_to_be16(ARPOP_REPLY) ||
1750 arp->hdr.ar_op == cpu_to_be16(ARPOP_REQUEST)) &&
1751 !memcmp(arp->ar_sip, arp->ar_tip, sizeof(arp->ar_sip)))
1752 return true;
1753 break;
1754 case cpu_to_be16(ETH_P_IPV6):
1755 /* can't say - but will probably be dropped later anyway */
1756 if (!pskb_may_pull(skb, sizeof(*eth) + sizeof(*ipv6) +
1757 sizeof(*icmpv6)))
1758 return false;
1759
1760 ipv6 = (void *)(eth + 1);
1761 icmpv6 = (void *)(ipv6 + 1);
1762
1763 if (icmpv6->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT &&
1764 !memcmp(&ipv6->saddr, &ipv6->daddr, sizeof(ipv6->saddr)))
1765 return true;
1766 break;
1767 default:
1768 /*
1769 * no need to support other protocols, proxy service isn't
1770 * specified for any others
1771 */
1772 break;
1773 }
1774
1775 return false;
1776}
1777EXPORT_SYMBOL(cfg80211_is_gratuitous_arp_unsolicited_na);
jianzhou76d32522020-03-13 15:56:44 +08001778
Dedy Lanskyab567892018-07-29 14:59:16 +03001779/* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
1780struct iapp_layer2_update {
1781 u8 da[ETH_ALEN]; /* broadcast */
1782 u8 sa[ETH_ALEN]; /* STA addr */
1783 __be16 len; /* 6 */
1784 u8 dsap; /* 0 */
1785 u8 ssap; /* 0 */
1786 u8 control;
1787 u8 xid_info[3];
1788} __packed;
1789
1790void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
1791{
1792 struct iapp_layer2_update *msg;
1793 struct sk_buff *skb;
1794
1795 /* Send Level 2 Update Frame to update forwarding tables in layer 2
1796 * bridge devices */
1797
1798 skb = dev_alloc_skb(sizeof(*msg));
1799 if (!skb)
1800 return;
1801 msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
1802
1803 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
1804 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
1805
1806 eth_broadcast_addr(msg->da);
1807 ether_addr_copy(msg->sa, addr);
1808 msg->len = htons(6);
1809 msg->dsap = 0;
1810 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
1811 msg->control = 0xaf; /* XID response lsb.1111F101.
1812 * F=0 (no poll command; unsolicited frame) */
1813 msg->xid_info[0] = 0x81; /* XID format identifier */
1814 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
1815 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
1816
1817 skb->dev = dev;
1818 skb->protocol = eth_type_trans(skb, dev);
1819 memset(skb->cb, 0, sizeof(skb->cb));
1820 netif_rx_ni(skb);
1821}
1822EXPORT_SYMBOL(cfg80211_send_layer2_update);