blob: 29c56615a87d4d61c7d05bd23d08e84bc1f43d6a [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{
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200224 if (key_idx < 0 || key_idx > 5)
Johannes Berg08645122009-05-11 13:54:58 +0200225 return -EINVAL;
226
Johannes Berge31b8212010-10-05 19:39:30 +0200227 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
228 return -EINVAL;
229
230 if (pairwise && !mac_addr)
231 return -EINVAL;
232
Jouni Malinen37720562015-01-24 19:52:04 +0200233 switch (params->cipher) {
234 case WLAN_CIPHER_SUITE_TKIP:
235 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200236 case WLAN_CIPHER_SUITE_CCMP_256:
237 case WLAN_CIPHER_SUITE_GCMP:
238 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinen37720562015-01-24 19:52:04 +0200239 /* Disallow pairwise keys with non-zero index unless it's WEP
240 * or a vendor specific cipher (because current deployments use
241 * pairwise WEP keys with non-zero indices and for vendor
242 * specific ciphers this should be validated in the driver or
243 * hardware level - but 802.11i clearly specifies to use zero)
244 */
245 if (pairwise && key_idx)
246 return -EINVAL;
247 break;
248 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200249 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
250 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
251 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen37720562015-01-24 19:52:04 +0200252 /* Disallow BIP (group-only) cipher as pairwise cipher */
253 if (pairwise)
254 return -EINVAL;
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200255 if (key_idx < 4)
256 return -EINVAL;
Jouni Malinen37720562015-01-24 19:52:04 +0200257 break;
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200258 case WLAN_CIPHER_SUITE_WEP40:
259 case WLAN_CIPHER_SUITE_WEP104:
260 if (key_idx > 3)
261 return -EINVAL;
Jouni Malinen37720562015-01-24 19:52:04 +0200262 default:
263 break;
264 }
Johannes Berg08645122009-05-11 13:54:58 +0200265
Johannes Berg08645122009-05-11 13:54:58 +0200266 switch (params->cipher) {
267 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200268 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200269 return -EINVAL;
270 break;
271 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200272 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200273 return -EINVAL;
274 break;
275 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200276 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200277 return -EINVAL;
278 break;
Jouni Malinencfcf1682015-01-24 19:52:05 +0200279 case WLAN_CIPHER_SUITE_CCMP_256:
280 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
281 return -EINVAL;
282 break;
283 case WLAN_CIPHER_SUITE_GCMP:
284 if (params->key_len != WLAN_KEY_LEN_GCMP)
285 return -EINVAL;
286 break;
287 case WLAN_CIPHER_SUITE_GCMP_256:
288 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
289 return -EINVAL;
290 break;
Johannes Berg08645122009-05-11 13:54:58 +0200291 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200292 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200293 return -EINVAL;
294 break;
295 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200296 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200297 return -EINVAL;
298 break;
Jouni Malinencfcf1682015-01-24 19:52:05 +0200299 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
300 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
301 return -EINVAL;
302 break;
303 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
304 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
305 return -EINVAL;
306 break;
307 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
308 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
309 return -EINVAL;
310 break;
Johannes Berg08645122009-05-11 13:54:58 +0200311 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300312 /*
313 * We don't know anything about this algorithm,
314 * allow using it -- but the driver must check
315 * all parameters! We still check below whether
316 * or not the driver supports this algorithm,
317 * of course.
318 */
319 break;
Johannes Berg08645122009-05-11 13:54:58 +0200320 }
321
Jouni Malinen9f26a952009-05-15 12:38:32 +0300322 if (params->seq) {
323 switch (params->cipher) {
324 case WLAN_CIPHER_SUITE_WEP40:
325 case WLAN_CIPHER_SUITE_WEP104:
326 /* These ciphers do not use key sequence */
327 return -EINVAL;
328 case WLAN_CIPHER_SUITE_TKIP:
329 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200330 case WLAN_CIPHER_SUITE_CCMP_256:
331 case WLAN_CIPHER_SUITE_GCMP:
332 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinen9f26a952009-05-15 12:38:32 +0300333 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200334 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
335 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
336 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen9f26a952009-05-15 12:38:32 +0300337 if (params->seq_len != 6)
338 return -EINVAL;
339 break;
340 }
341 }
342
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300343 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200344 return -EINVAL;
345
Johannes Berg08645122009-05-11 13:54:58 +0200346 return 0;
347}
Zhu Yie31a16d2009-05-21 21:47:03 +0800348
Johannes Berg633adf12010-08-12 14:49:58 +0200349unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800350{
351 unsigned int hdrlen = 24;
352
353 if (ieee80211_is_data(fc)) {
354 if (ieee80211_has_a4(fc))
355 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200356 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800357 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200358 if (ieee80211_has_order(fc))
359 hdrlen += IEEE80211_HT_CTL_LEN;
360 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800361 goto out;
362 }
363
Fred Choufb142f42015-01-20 10:17:27 +0800364 if (ieee80211_is_mgmt(fc)) {
365 if (ieee80211_has_order(fc))
366 hdrlen += IEEE80211_HT_CTL_LEN;
367 goto out;
368 }
369
Zhu Yie31a16d2009-05-21 21:47:03 +0800370 if (ieee80211_is_ctl(fc)) {
371 /*
372 * ACK and CTS are 10 bytes, all others 16. To see how
373 * to get this condition consider
374 * subtype mask: 0b0000000011110000 (0x00F0)
375 * ACK subtype: 0b0000000011010000 (0x00D0)
376 * CTS subtype: 0b0000000011000000 (0x00C0)
377 * bits that matter: ^^^ (0x00E0)
378 * value of those: 0b0000000011000000 (0x00C0)
379 */
380 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
381 hdrlen = 10;
382 else
383 hdrlen = 16;
384 }
385out:
386 return hdrlen;
387}
388EXPORT_SYMBOL(ieee80211_hdrlen);
389
390unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
391{
392 const struct ieee80211_hdr *hdr =
393 (const struct ieee80211_hdr *)skb->data;
394 unsigned int hdrlen;
395
396 if (unlikely(skb->len < 10))
397 return 0;
398 hdrlen = ieee80211_hdrlen(hdr->frame_control);
399 if (unlikely(hdrlen > skb->len))
400 return 0;
401 return hdrlen;
402}
403EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
404
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100405static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
Zhu Yie31a16d2009-05-21 21:47:03 +0800406{
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100407 int ae = flags & MESH_FLAGS_AE;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200408 /* 802.11-2012, 8.2.4.7.3 */
Zhu Yie31a16d2009-05-21 21:47:03 +0800409 switch (ae) {
Johannes Berg7dd111e2012-10-25 21:51:59 +0200410 default:
Zhu Yie31a16d2009-05-21 21:47:03 +0800411 case 0:
412 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700413 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800414 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700415 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800416 return 18;
Zhu Yie31a16d2009-05-21 21:47:03 +0800417 }
418}
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100419
420unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
421{
422 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
423}
Johannes Berg9b395bc2012-10-26 00:36:40 +0200424EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800425
Johannes Berg7f6990c2016-10-05 15:29:49 +0200426int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
427 const u8 *addr, enum nl80211_iftype iftype)
Zhu Yie31a16d2009-05-21 21:47:03 +0800428{
429 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100430 struct {
431 u8 hdr[ETH_ALEN] __aligned(2);
432 __be16 proto;
433 } payload;
434 struct ethhdr tmp;
435 u16 hdrlen;
436 u8 mesh_flags = 0;
Zhu Yie31a16d2009-05-21 21:47:03 +0800437
438 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
439 return -1;
440
441 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100442 if (skb->len < hdrlen + 8)
443 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800444
445 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
446 * header
447 * IEEE 802.11 address fields:
448 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
449 * 0 0 DA SA BSSID n/a
450 * 0 1 DA BSSID SA n/a
451 * 1 0 BSSID SA DA n/a
452 * 1 1 RA TA DA SA
453 */
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100454 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
455 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
456
457 if (iftype == NL80211_IFTYPE_MESH_POINT)
458 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
Zhu Yie31a16d2009-05-21 21:47:03 +0800459
Rajkumar Manoharan6568f8f2017-05-14 21:41:55 -0700460 mesh_flags &= MESH_FLAGS_AE;
461
Zhu Yie31a16d2009-05-21 21:47:03 +0800462 switch (hdr->frame_control &
463 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
464 case cpu_to_le16(IEEE80211_FCTL_TODS):
465 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200466 iftype != NL80211_IFTYPE_AP_VLAN &&
467 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800468 return -1;
469 break;
470 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
471 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543ee2009-11-10 20:10:05 +0100472 iftype != NL80211_IFTYPE_MESH_POINT &&
473 iftype != NL80211_IFTYPE_AP_VLAN &&
474 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800475 return -1;
476 if (iftype == NL80211_IFTYPE_MESH_POINT) {
Rajkumar Manoharan6568f8f2017-05-14 21:41:55 -0700477 if (mesh_flags == MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800478 return -1;
Rajkumar Manoharan6568f8f2017-05-14 21:41:55 -0700479 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800480 skb_copy_bits(skb, hdrlen +
481 offsetof(struct ieee80211s_hdr, eaddr1),
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100482 tmp.h_dest, 2 * ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800483 }
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100484 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
Zhu Yie31a16d2009-05-21 21:47:03 +0800485 }
486 break;
487 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700488 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200489 iftype != NL80211_IFTYPE_P2P_CLIENT &&
490 iftype != NL80211_IFTYPE_MESH_POINT) ||
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100491 (is_multicast_ether_addr(tmp.h_dest) &&
492 ether_addr_equal(tmp.h_source, addr)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800493 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700494 if (iftype == NL80211_IFTYPE_MESH_POINT) {
Rajkumar Manoharan6568f8f2017-05-14 21:41:55 -0700495 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800496 return -1;
Rajkumar Manoharan6568f8f2017-05-14 21:41:55 -0700497 if (mesh_flags == MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800498 skb_copy_bits(skb, hdrlen +
499 offsetof(struct ieee80211s_hdr, eaddr1),
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100500 tmp.h_source, ETH_ALEN);
501 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700502 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800503 break;
504 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300505 if (iftype != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100506 iftype != NL80211_IFTYPE_STATION &&
507 iftype != NL80211_IFTYPE_OCB)
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300508 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800509 break;
510 }
511
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100512 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
513 tmp.h_proto = payload.proto;
Zhu Yie31a16d2009-05-21 21:47:03 +0800514
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100515 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
516 tmp.h_proto != htons(ETH_P_AARP) &&
517 tmp.h_proto != htons(ETH_P_IPX)) ||
518 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800519 /* remove RFC1042 or Bridge-Tunnel encapsulation and
520 * replace EtherType */
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100521 hdrlen += ETH_ALEN + 2;
522 else
Felix Fietkauc0417782016-06-29 10:36:39 +0200523 tmp.h_proto = htons(skb->len - hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800524
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100525 pskb_pull(skb, hdrlen);
526
527 if (!ehdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800528 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100529 memcpy(ehdr, &tmp, sizeof(tmp));
530
Zhu Yie31a16d2009-05-21 21:47:03 +0800531 return 0;
532}
Johannes Berg7f6990c2016-10-05 15:29:49 +0200533EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800534
Felix Fietkau2b67f942016-02-08 14:34:42 +0100535static void
536__frame_add_frag(struct sk_buff *skb, struct page *page,
537 void *ptr, int len, int size)
538{
539 struct skb_shared_info *sh = skb_shinfo(skb);
540 int page_offset;
541
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700542 page_ref_inc(page);
Felix Fietkau2b67f942016-02-08 14:34:42 +0100543 page_offset = ptr - page_address(page);
544 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
545}
546
547static void
548__ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
549 int offset, int len)
550{
551 struct skb_shared_info *sh = skb_shinfo(skb);
552 const skb_frag_t *frag = &sh->frags[-1];
553 struct page *frag_page;
554 void *frag_ptr;
555 int frag_len, frag_size;
556 int head_size = skb->len - skb->data_len;
557 int cur_len;
558
559 frag_page = virt_to_head_page(skb->head);
560 frag_ptr = skb->data;
561 frag_size = head_size;
562
563 while (offset >= frag_size) {
564 offset -= frag_size;
565 frag++;
566 frag_page = skb_frag_page(frag);
567 frag_ptr = skb_frag_address(frag);
568 frag_size = skb_frag_size(frag);
569 }
570
571 frag_ptr += offset;
572 frag_len = frag_size - offset;
573
574 cur_len = min(len, frag_len);
575
576 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
577 len -= cur_len;
578
579 while (len > 0) {
580 frag++;
581 frag_len = skb_frag_size(frag);
582 cur_len = min(len, frag_len);
583 __frame_add_frag(frame, skb_frag_page(frag),
584 skb_frag_address(frag), cur_len, frag_len);
585 len -= cur_len;
586 }
587}
588
Felix Fietkau230fd282016-02-02 14:39:10 +0100589static struct sk_buff *
590__ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
Felix Fietkau2b67f942016-02-08 14:34:42 +0100591 int offset, int len, bool reuse_frag)
Felix Fietkau230fd282016-02-02 14:39:10 +0100592{
593 struct sk_buff *frame;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100594 int cur_len = len;
Felix Fietkau230fd282016-02-02 14:39:10 +0100595
596 if (skb->len - offset < len)
597 return NULL;
598
599 /*
Felix Fietkau2b67f942016-02-08 14:34:42 +0100600 * When reusing framents, copy some data to the head to simplify
601 * ethernet header handling and speed up protocol header processing
602 * in the stack later.
603 */
604 if (reuse_frag)
605 cur_len = min_t(int, len, 32);
606
607 /*
Felix Fietkau230fd282016-02-02 14:39:10 +0100608 * Allocate and reserve two bytes more for payload
609 * alignment since sizeof(struct ethhdr) is 14.
610 */
Felix Fietkau2b67f942016-02-08 14:34:42 +0100611 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
Gregory Greenman16a910a2016-07-05 15:23:10 +0300612 if (!frame)
613 return NULL;
Felix Fietkau230fd282016-02-02 14:39:10 +0100614
615 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
Felix Fietkau2b67f942016-02-08 14:34:42 +0100616 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
617
618 len -= cur_len;
619 if (!len)
620 return frame;
621
622 offset += cur_len;
623 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
Felix Fietkau230fd282016-02-02 14:39:10 +0100624
625 return frame;
626}
Zhu Yieaf85ca2009-12-01 10:18:37 +0800627
628void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
629 const u8 *addr, enum nl80211_iftype iftype,
Johannes Berg8b935ee2016-10-05 16:17:01 +0200630 const unsigned int extra_headroom,
631 const u8 *check_da, const u8 *check_sa)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800632{
Felix Fietkau230fd282016-02-02 14:39:10 +0100633 unsigned int hlen = ALIGN(extra_headroom, 4);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800634 struct sk_buff *frame = NULL;
635 u16 ethertype;
636 u8 *payload;
Johannes Berg7f6990c2016-10-05 15:29:49 +0200637 int offset = 0, remaining;
Felix Fietkau230fd282016-02-02 14:39:10 +0100638 struct ethhdr eth;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100639 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
Felix Fietkau2bf0ccc2016-02-08 14:25:26 +0100640 bool reuse_skb = false;
Felix Fietkau230fd282016-02-02 14:39:10 +0100641 bool last = false;
Felix Fietkau88665f52016-02-02 14:39:08 +0100642
Felix Fietkau230fd282016-02-02 14:39:10 +0100643 while (!last) {
644 unsigned int subframe_len;
645 int len;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800646 u8 padding;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800647
Felix Fietkau230fd282016-02-02 14:39:10 +0100648 skb_copy_bits(skb, offset, &eth, sizeof(eth));
649 len = ntohs(eth.h_proto);
650 subframe_len = sizeof(struct ethhdr) + len;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800651 padding = (4 - subframe_len) & 0x3;
Felix Fietkau230fd282016-02-02 14:39:10 +0100652
Zhu Yieaf85ca2009-12-01 10:18:37 +0800653 /* the last MSDU has no padding */
Felix Fietkau230fd282016-02-02 14:39:10 +0100654 remaining = skb->len - offset;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800655 if (subframe_len > remaining)
656 goto purge;
657
Felix Fietkau230fd282016-02-02 14:39:10 +0100658 offset += sizeof(struct ethhdr);
Felix Fietkau230fd282016-02-02 14:39:10 +0100659 last = remaining <= subframe_len + padding;
Johannes Berg8b935ee2016-10-05 16:17:01 +0200660
661 /* FIXME: should we really accept multicast DA? */
662 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
663 !ether_addr_equal(check_da, eth.h_dest)) ||
664 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
665 offset += len + padding;
666 continue;
667 }
668
669 /* reuse skb for the last subframe */
Felix Fietkau2b67f942016-02-08 14:34:42 +0100670 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
Felix Fietkau230fd282016-02-02 14:39:10 +0100671 skb_pull(skb, offset);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800672 frame = skb;
Felix Fietkau230fd282016-02-02 14:39:10 +0100673 reuse_skb = true;
674 } else {
Felix Fietkau2b67f942016-02-08 14:34:42 +0100675 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
676 reuse_frag);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800677 if (!frame)
678 goto purge;
679
Felix Fietkau230fd282016-02-02 14:39:10 +0100680 offset += len + padding;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800681 }
682
683 skb_reset_network_header(frame);
684 frame->dev = skb->dev;
685 frame->priority = skb->priority;
686
687 payload = frame->data;
688 ethertype = (payload[6] << 8) | payload[7];
Joe Perchesac422d32012-05-08 18:56:55 +0000689 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yieaf85ca2009-12-01 10:18:37 +0800690 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perchesac422d32012-05-08 18:56:55 +0000691 ether_addr_equal(payload, bridge_tunnel_header))) {
Felix Fietkau230fd282016-02-02 14:39:10 +0100692 eth.h_proto = htons(ethertype);
693 skb_pull(frame, ETH_ALEN + 2);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800694 }
Felix Fietkau230fd282016-02-02 14:39:10 +0100695
696 memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
Zhu Yieaf85ca2009-12-01 10:18:37 +0800697 __skb_queue_tail(list, frame);
698 }
699
Felix Fietkau230fd282016-02-02 14:39:10 +0100700 if (!reuse_skb)
701 dev_kfree_skb(skb);
702
Zhu Yieaf85ca2009-12-01 10:18:37 +0800703 return;
704
705 purge:
706 __skb_queue_purge(list);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800707 dev_kfree_skb(skb);
708}
709EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
710
Zhu Yie31a16d2009-05-21 21:47:03 +0800711/* Given a data frame determine the 802.1p/1d tag to use. */
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800712unsigned int cfg80211_classify8021d(struct sk_buff *skb,
713 struct cfg80211_qos_map *qos_map)
Zhu Yie31a16d2009-05-21 21:47:03 +0800714{
715 unsigned int dscp;
cedric Vonckenc6ca5e22013-08-26 14:04:52 +0200716 unsigned char vlan_priority;
Zhu Yie31a16d2009-05-21 21:47:03 +0800717
718 /* skb->priority values from 256->263 are magic values to
719 * directly indicate a specific 802.1d priority. This is used
720 * to allow 802.1d priority to be passed directly in from VLAN
721 * tags, etc.
722 */
723 if (skb->priority >= 256 && skb->priority <= 263)
724 return skb->priority - 256;
725
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100726 if (skb_vlan_tag_present(skb)) {
727 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
cedric Vonckenc6ca5e22013-08-26 14:04:52 +0200728 >> VLAN_PRIO_SHIFT;
729 if (vlan_priority > 0)
730 return vlan_priority;
731 }
732
Zhu Yie31a16d2009-05-21 21:47:03 +0800733 switch (skb->protocol) {
734 case htons(ETH_P_IP):
Dave Tähtb1565792011-12-22 12:55:08 -0800735 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
736 break;
737 case htons(ETH_P_IPV6):
738 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
Zhu Yie31a16d2009-05-21 21:47:03 +0800739 break;
Simon Wunderlich960d97f2014-03-03 17:23:12 +0100740 case htons(ETH_P_MPLS_UC):
741 case htons(ETH_P_MPLS_MC): {
742 struct mpls_label mpls_tmp, *mpls;
743
744 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
745 sizeof(*mpls), &mpls_tmp);
746 if (!mpls)
747 return 0;
748
749 return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
750 >> MPLS_LS_TC_SHIFT;
751 }
752 case htons(ETH_P_80221):
753 /* 802.21 is always network control traffic */
754 return 7;
Zhu Yie31a16d2009-05-21 21:47:03 +0800755 default:
756 return 0;
757 }
758
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800759 if (qos_map) {
760 unsigned int i, tmp_dscp = dscp >> 2;
761
762 for (i = 0; i < qos_map->num_des; i++) {
763 if (tmp_dscp == qos_map->dscp_exception[i].dscp)
764 return qos_map->dscp_exception[i].up;
765 }
766
767 for (i = 0; i < 8; i++) {
768 if (tmp_dscp >= qos_map->up[i].low &&
769 tmp_dscp <= qos_map->up[i].high)
770 return i;
771 }
772 }
773
Zhu Yie31a16d2009-05-21 21:47:03 +0800774 return dscp >> 5;
775}
776EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200777
778const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
779{
Johannes Berg9caf0362012-11-29 01:25:20 +0100780 const struct cfg80211_bss_ies *ies;
781
782 ies = rcu_dereference(bss->ies);
783 if (!ies)
Johannes Berg517357c2009-07-02 17:18:40 +0200784 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +0100785
786 return cfg80211_find_ie(ie, ies->data, ies->len);
Johannes Berg517357c2009-07-02 17:18:40 +0200787}
788EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200789
790void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
791{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800792 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200793 struct net_device *dev = wdev->netdev;
794 int i;
795
796 if (!wdev->connect_keys)
797 return;
798
David Spinadelb8676222016-09-22 23:16:50 +0300799 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200800 if (!wdev->connect_keys->params[i].cipher)
801 continue;
Hila Gonene35e4d22012-06-27 17:19:42 +0300802 if (rdev_add_key(rdev, dev, i, false, NULL,
803 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800804 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800805 continue;
806 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200807 if (wdev->connect_keys->def == i)
Hila Gonene35e4d22012-06-27 17:19:42 +0300808 if (rdev_set_default_key(rdev, dev, i, true, true)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800809 netdev_err(dev, "failed to set defkey %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800810 continue;
811 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200812 }
813
Johannes Bergb47f6102014-09-10 13:39:54 +0300814 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200815 wdev->connect_keys = NULL;
816}
Johannes Berg3d54d252009-08-21 14:51:05 +0200817
Daniel Drake1f6fc432012-08-02 18:41:48 +0100818void cfg80211_process_wdev_events(struct wireless_dev *wdev)
Johannes Berg3d54d252009-08-21 14:51:05 +0200819{
820 struct cfg80211_event *ev;
821 unsigned long flags;
Johannes Berg3d54d252009-08-21 14:51:05 +0200822
823 spin_lock_irqsave(&wdev->event_lock, flags);
824 while (!list_empty(&wdev->event_list)) {
825 ev = list_first_entry(&wdev->event_list,
826 struct cfg80211_event, list);
827 list_del(&ev->list);
828 spin_unlock_irqrestore(&wdev->event_lock, flags);
829
830 wdev_lock(wdev);
831 switch (ev->type) {
832 case EVENT_CONNECT_RESULT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200833 __cfg80211_connect_result(
Vidyullatha Kanchanapally3f1905b2017-03-31 00:22:33 +0300834 wdev->netdev,
835 &ev->cr,
836 ev->cr.status == WLAN_STATUS_SUCCESS);
Johannes Berg3d54d252009-08-21 14:51:05 +0200837 break;
838 case EVENT_ROAMED:
Avraham Stern9e841a62017-04-26 10:58:49 +0300839 __cfg80211_roamed(wdev, &ev->rm);
Johannes Berg3d54d252009-08-21 14:51:05 +0200840 break;
841 case EVENT_DISCONNECTED:
842 __cfg80211_disconnected(wdev->netdev,
843 ev->dc.ie, ev->dc.ie_len,
Johannes Berg80279fb2015-05-22 16:22:20 +0200844 ev->dc.reason,
845 !ev->dc.locally_generated);
Johannes Berg3d54d252009-08-21 14:51:05 +0200846 break;
847 case EVENT_IBSS_JOINED:
Antonio Quartullife94f3a2014-01-29 17:53:43 +0100848 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
849 ev->ij.channel);
Johannes Berg3d54d252009-08-21 14:51:05 +0200850 break;
Michal Kaziorf04c2202014-04-09 15:11:01 +0200851 case EVENT_STOPPED:
852 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
853 break;
Johannes Berg3d54d252009-08-21 14:51:05 +0200854 }
855 wdev_unlock(wdev);
856
857 kfree(ev);
858
859 spin_lock_irqsave(&wdev->event_lock, flags);
860 }
861 spin_unlock_irqrestore(&wdev->event_lock, flags);
862}
863
864void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
865{
866 struct wireless_dev *wdev;
867
868 ASSERT_RTNL();
Johannes Berg3d54d252009-08-21 14:51:05 +0200869
Johannes Berg53873f12016-05-03 16:52:04 +0300870 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
Johannes Berg3d54d252009-08-21 14:51:05 +0200871 cfg80211_process_wdev_events(wdev);
Johannes Berg3d54d252009-08-21 14:51:05 +0200872}
873
874int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
875 struct net_device *dev, enum nl80211_iftype ntype,
876 u32 *flags, struct vif_params *params)
877{
878 int err;
879 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
880
Zhao, Gang73fb08e2014-03-19 17:04:36 +0800881 ASSERT_RTNL();
Johannes Berg3d54d252009-08-21 14:51:05 +0200882
883 /* don't support changing VLANs, you just re-create them */
884 if (otype == NL80211_IFTYPE_AP_VLAN)
885 return -EOPNOTSUPP;
886
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300887 /* cannot change into P2P device or NAN */
888 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
889 ntype == NL80211_IFTYPE_NAN)
Johannes Berg98104fde2012-06-16 00:19:54 +0200890 return -EOPNOTSUPP;
891
Johannes Berg3d54d252009-08-21 14:51:05 +0200892 if (!rdev->ops->change_virtual_intf ||
893 !(rdev->wiphy.interface_modes & (1 << ntype)))
894 return -EOPNOTSUPP;
895
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100896 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000897 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200898 (ntype == NL80211_IFTYPE_ADHOC ||
899 ntype == NL80211_IFTYPE_STATION ||
900 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100901 return -EBUSY;
902
Michal Kazior6cbfb1b2015-05-22 10:57:22 +0200903 if (ntype != otype) {
Johannes Berg9bc383d2009-11-19 11:55:19 +0100904 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100905 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg194ff522013-12-30 23:12:37 +0100906 wdev_lock(dev->ieee80211_ptr);
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800907 rdev_set_qos_map(rdev, dev, NULL);
Johannes Berg194ff522013-12-30 23:12:37 +0100908 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg9bc383d2009-11-19 11:55:19 +0100909
Johannes Berg3d54d252009-08-21 14:51:05 +0200910 switch (otype) {
Michal Kaziorac800142012-06-29 12:46:57 +0200911 case NL80211_IFTYPE_AP:
Ilan Peer7c8d5e02014-02-25 15:33:38 +0200912 cfg80211_stop_ap(rdev, dev, true);
Michal Kaziorac800142012-06-29 12:46:57 +0200913 break;
Johannes Berg3d54d252009-08-21 14:51:05 +0200914 case NL80211_IFTYPE_ADHOC:
915 cfg80211_leave_ibss(rdev, dev, false);
916 break;
917 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200918 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg83739b02013-05-15 17:44:01 +0200919 wdev_lock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +0200920 cfg80211_disconnect(rdev, dev,
921 WLAN_REASON_DEAUTH_LEAVING, true);
Johannes Berg83739b02013-05-15 17:44:01 +0200922 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +0200923 break;
924 case NL80211_IFTYPE_MESH_POINT:
925 /* mesh should be handled? */
926 break;
927 default:
928 break;
929 }
930
931 cfg80211_process_rdev_events(rdev);
932 }
933
Hila Gonene35e4d22012-06-27 17:19:42 +0300934 err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
Johannes Berg3d54d252009-08-21 14:51:05 +0200935
936 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
937
Johannes Berg9bc383d2009-11-19 11:55:19 +0100938 if (!err && params && params->use_4addr != -1)
939 dev->ieee80211_ptr->use_4addr = params->use_4addr;
940
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100941 if (!err) {
942 dev->priv_flags &= ~IFF_DONT_BRIDGE;
943 switch (ntype) {
944 case NL80211_IFTYPE_STATION:
945 if (dev->ieee80211_ptr->use_4addr)
946 break;
947 /* fall through */
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100948 case NL80211_IFTYPE_OCB:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200949 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100950 case NL80211_IFTYPE_ADHOC:
951 dev->priv_flags |= IFF_DONT_BRIDGE;
952 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200953 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100954 case NL80211_IFTYPE_AP:
955 case NL80211_IFTYPE_AP_VLAN:
956 case NL80211_IFTYPE_WDS:
957 case NL80211_IFTYPE_MESH_POINT:
958 /* bridging OK */
959 break;
960 case NL80211_IFTYPE_MONITOR:
961 /* monitor can't bridge anyway */
962 break;
963 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200964 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100965 /* not happening */
966 break;
Johannes Berg98104fde2012-06-16 00:19:54 +0200967 case NL80211_IFTYPE_P2P_DEVICE:
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300968 case NL80211_IFTYPE_NAN:
Johannes Berg98104fde2012-06-16 00:19:54 +0200969 WARN_ON(1);
970 break;
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100971 }
972 }
973
Michal Kaziordbbae262012-06-29 12:47:01 +0200974 if (!err && ntype != otype && netif_running(dev)) {
975 cfg80211_update_iface_num(rdev, ntype, 1);
976 cfg80211_update_iface_num(rdev, otype, -1);
977 }
978
Johannes Berg3d54d252009-08-21 14:51:05 +0200979 return err;
980}
John W. Linville254416a2009-12-09 16:43:52 -0500981
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +0300982static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
983{
984 static const u32 __mcs2bitrate[] = {
985 /* control PHY */
986 [0] = 275,
987 /* SC PHY */
988 [1] = 3850,
989 [2] = 7700,
990 [3] = 9625,
991 [4] = 11550,
992 [5] = 12512, /* 1251.25 mbps */
993 [6] = 15400,
994 [7] = 19250,
995 [8] = 23100,
996 [9] = 25025,
997 [10] = 30800,
998 [11] = 38500,
999 [12] = 46200,
1000 /* OFDM PHY */
1001 [13] = 6930,
1002 [14] = 8662, /* 866.25 mbps */
1003 [15] = 13860,
1004 [16] = 17325,
1005 [17] = 20790,
1006 [18] = 27720,
1007 [19] = 34650,
1008 [20] = 41580,
1009 [21] = 45045,
1010 [22] = 51975,
1011 [23] = 62370,
1012 [24] = 67568, /* 6756.75 mbps */
1013 /* LP-SC PHY */
1014 [25] = 6260,
1015 [26] = 8340,
1016 [27] = 11120,
1017 [28] = 12510,
1018 [29] = 16680,
1019 [30] = 22240,
1020 [31] = 25030,
1021 };
1022
1023 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1024 return 0;
1025
1026 return __mcs2bitrate[rate->mcs];
1027}
1028
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001029static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1030{
1031 static const u32 base[4][10] = {
1032 { 6500000,
1033 13000000,
1034 19500000,
1035 26000000,
1036 39000000,
1037 52000000,
1038 58500000,
1039 65000000,
1040 78000000,
Pedersen, Thomas8fdd1362016-10-31 11:28:40 -07001041 /* not in the spec, but some devices use this: */
1042 86500000,
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001043 },
1044 { 13500000,
1045 27000000,
1046 40500000,
1047 54000000,
1048 81000000,
1049 108000000,
1050 121500000,
1051 135000000,
1052 162000000,
1053 180000000,
1054 },
1055 { 29300000,
1056 58500000,
1057 87800000,
1058 117000000,
1059 175500000,
1060 234000000,
1061 263300000,
1062 292500000,
1063 351000000,
1064 390000000,
1065 },
1066 { 58500000,
1067 117000000,
1068 175500000,
1069 234000000,
1070 351000000,
1071 468000000,
1072 526500000,
1073 585000000,
1074 702000000,
1075 780000000,
1076 },
1077 };
1078 u32 bitrate;
1079 int idx;
1080
1081 if (WARN_ON_ONCE(rate->mcs > 9))
1082 return 0;
1083
Johannes Bergb51f3be2015-01-15 16:14:02 +01001084 switch (rate->bw) {
1085 case RATE_INFO_BW_160:
1086 idx = 3;
1087 break;
1088 case RATE_INFO_BW_80:
1089 idx = 2;
1090 break;
1091 case RATE_INFO_BW_40:
1092 idx = 1;
1093 break;
1094 case RATE_INFO_BW_5:
1095 case RATE_INFO_BW_10:
1096 default:
1097 WARN_ON(1);
1098 /* fall through */
1099 case RATE_INFO_BW_20:
1100 idx = 0;
1101 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001102
1103 bitrate = base[idx][rate->mcs];
1104 bitrate *= rate->nss;
1105
1106 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1107 bitrate = (bitrate / 9) * 10;
1108
1109 /* do NOT round down here */
1110 return (bitrate + 50000) / 100000;
1111}
1112
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03001113u32 cfg80211_calculate_bitrate(struct rate_info *rate)
John W. Linville254416a2009-12-09 16:43:52 -05001114{
1115 int modulation, streams, bitrate;
1116
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001117 if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
1118 !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
John W. Linville254416a2009-12-09 16:43:52 -05001119 return rate->legacy;
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +03001120 if (rate->flags & RATE_INFO_FLAGS_60G)
1121 return cfg80211_calculate_bitrate_60g(rate);
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001122 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1123 return cfg80211_calculate_bitrate_vht(rate);
John W. Linville254416a2009-12-09 16:43:52 -05001124
1125 /* the formula below does only work for MCS values smaller than 32 */
Johannes Berg2615f372012-05-08 21:36:20 +02001126 if (WARN_ON_ONCE(rate->mcs >= 32))
John W. Linville254416a2009-12-09 16:43:52 -05001127 return 0;
1128
1129 modulation = rate->mcs & 7;
1130 streams = (rate->mcs >> 3) + 1;
1131
Johannes Bergb51f3be2015-01-15 16:14:02 +01001132 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
John W. Linville254416a2009-12-09 16:43:52 -05001133
1134 if (modulation < 4)
1135 bitrate *= (modulation + 1);
1136 else if (modulation == 4)
1137 bitrate *= (modulation + 2);
1138 else
1139 bitrate *= (modulation + 3);
1140
1141 bitrate *= streams;
1142
1143 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1144 bitrate = (bitrate / 9) * 10;
1145
1146 /* do NOT round down here */
1147 return (bitrate + 50000) / 100000;
1148}
Thomas Pedersen8097e142012-03-05 15:31:47 -08001149EXPORT_SYMBOL(cfg80211_calculate_bitrate);
Johannes Berg56d18932011-05-09 18:41:15 +02001150
Arend van Sprielc216e642012-11-25 19:13:28 +01001151int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1152 enum ieee80211_p2p_attr_id attr,
1153 u8 *buf, unsigned int bufsize)
Johannes Berg0ee45352012-10-29 19:48:40 +01001154{
1155 u8 *out = buf;
1156 u16 attr_remaining = 0;
1157 bool desired_attr = false;
1158 u16 desired_len = 0;
1159
1160 while (len > 0) {
1161 unsigned int iedatalen;
1162 unsigned int copy;
1163 const u8 *iedata;
1164
1165 if (len < 2)
1166 return -EILSEQ;
1167 iedatalen = ies[1];
1168 if (iedatalen + 2 > len)
1169 return -EILSEQ;
1170
1171 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1172 goto cont;
1173
1174 if (iedatalen < 4)
1175 goto cont;
1176
1177 iedata = ies + 2;
1178
1179 /* check WFA OUI, P2P subtype */
1180 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1181 iedata[2] != 0x9a || iedata[3] != 0x09)
1182 goto cont;
1183
1184 iedatalen -= 4;
1185 iedata += 4;
1186
1187 /* check attribute continuation into this IE */
1188 copy = min_t(unsigned int, attr_remaining, iedatalen);
1189 if (copy && desired_attr) {
1190 desired_len += copy;
1191 if (out) {
1192 memcpy(out, iedata, min(bufsize, copy));
1193 out += min(bufsize, copy);
1194 bufsize -= min(bufsize, copy);
1195 }
1196
1197
1198 if (copy == attr_remaining)
1199 return desired_len;
1200 }
1201
1202 attr_remaining -= copy;
1203 if (attr_remaining)
1204 goto cont;
1205
1206 iedatalen -= copy;
1207 iedata += copy;
1208
1209 while (iedatalen > 0) {
1210 u16 attr_len;
1211
1212 /* P2P attribute ID & size must fit */
1213 if (iedatalen < 3)
1214 return -EILSEQ;
1215 desired_attr = iedata[0] == attr;
1216 attr_len = get_unaligned_le16(iedata + 1);
1217 iedatalen -= 3;
1218 iedata += 3;
1219
1220 copy = min_t(unsigned int, attr_len, iedatalen);
1221
1222 if (desired_attr) {
1223 desired_len += copy;
1224 if (out) {
1225 memcpy(out, iedata, min(bufsize, copy));
1226 out += min(bufsize, copy);
1227 bufsize -= min(bufsize, copy);
1228 }
1229
1230 if (copy == attr_len)
1231 return desired_len;
1232 }
1233
1234 iedata += copy;
1235 iedatalen -= copy;
1236 attr_remaining = attr_len - copy;
1237 }
1238
1239 cont:
1240 len -= ies[1] + 2;
1241 ies += ies[1] + 2;
1242 }
1243
1244 if (attr_remaining && desired_attr)
1245 return -EILSEQ;
1246
1247 return -ENOENT;
1248}
1249EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1250
Johannes Berg29464cc2015-03-31 15:36:22 +02001251static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1252{
1253 int i;
1254
1255 for (i = 0; i < n_ids; i++)
1256 if (ids[i] == id)
1257 return true;
1258 return false;
1259}
1260
1261size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1262 const u8 *ids, int n_ids,
1263 const u8 *after_ric, int n_after_ric,
1264 size_t offset)
1265{
1266 size_t pos = offset;
1267
1268 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos])) {
1269 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1270 pos += 2 + ies[pos + 1];
1271
1272 while (pos < ielen &&
1273 !ieee80211_id_in_list(after_ric, n_after_ric,
1274 ies[pos]))
1275 pos += 2 + ies[pos + 1];
1276 } else {
1277 pos += 2 + ies[pos + 1];
1278 }
1279 }
1280
1281 return pos;
1282}
1283EXPORT_SYMBOL(ieee80211_ie_split_ric);
1284
Johannes Berg1ce3e822012-08-01 17:00:55 +02001285bool ieee80211_operating_class_to_band(u8 operating_class,
Johannes Berg57fbcce2016-04-12 15:56:15 +02001286 enum nl80211_band *band)
Johannes Berg1ce3e822012-08-01 17:00:55 +02001287{
1288 switch (operating_class) {
1289 case 112:
1290 case 115 ... 127:
Eliad Peller954a86e2015-03-01 09:10:01 +02001291 case 128 ... 130:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001292 *band = NL80211_BAND_5GHZ;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001293 return true;
1294 case 81:
1295 case 82:
1296 case 83:
1297 case 84:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001298 *band = NL80211_BAND_2GHZ;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001299 return true;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001300 case 180:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001301 *band = NL80211_BAND_60GHZ;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001302 return true;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001303 }
1304
1305 return false;
1306}
1307EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1308
Arik Nemtsova38700d2015-03-18 08:46:08 +02001309bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1310 u8 *op_class)
1311{
1312 u8 vht_opclass;
1313 u16 freq = chandef->center_freq1;
1314
1315 if (freq >= 2412 && freq <= 2472) {
1316 if (chandef->width > NL80211_CHAN_WIDTH_40)
1317 return false;
1318
1319 /* 2.407 GHz, channels 1..13 */
1320 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1321 if (freq > chandef->chan->center_freq)
1322 *op_class = 83; /* HT40+ */
1323 else
1324 *op_class = 84; /* HT40- */
1325 } else {
1326 *op_class = 81;
1327 }
1328
1329 return true;
1330 }
1331
1332 if (freq == 2484) {
1333 if (chandef->width > NL80211_CHAN_WIDTH_40)
1334 return false;
1335
1336 *op_class = 82; /* channel 14 */
1337 return true;
1338 }
1339
1340 switch (chandef->width) {
1341 case NL80211_CHAN_WIDTH_80:
1342 vht_opclass = 128;
1343 break;
1344 case NL80211_CHAN_WIDTH_160:
1345 vht_opclass = 129;
1346 break;
1347 case NL80211_CHAN_WIDTH_80P80:
1348 vht_opclass = 130;
1349 break;
1350 case NL80211_CHAN_WIDTH_10:
1351 case NL80211_CHAN_WIDTH_5:
1352 return false; /* unsupported for now */
1353 default:
1354 vht_opclass = 0;
1355 break;
1356 }
1357
1358 /* 5 GHz, channels 36..48 */
1359 if (freq >= 5180 && freq <= 5240) {
1360 if (vht_opclass) {
1361 *op_class = vht_opclass;
1362 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1363 if (freq > chandef->chan->center_freq)
1364 *op_class = 116;
1365 else
1366 *op_class = 117;
1367 } else {
1368 *op_class = 115;
1369 }
1370
1371 return true;
1372 }
1373
1374 /* 5 GHz, channels 52..64 */
1375 if (freq >= 5260 && freq <= 5320) {
1376 if (vht_opclass) {
1377 *op_class = vht_opclass;
1378 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1379 if (freq > chandef->chan->center_freq)
1380 *op_class = 119;
1381 else
1382 *op_class = 120;
1383 } else {
1384 *op_class = 118;
1385 }
1386
1387 return true;
1388 }
1389
1390 /* 5 GHz, channels 100..144 */
1391 if (freq >= 5500 && freq <= 5720) {
1392 if (vht_opclass) {
1393 *op_class = vht_opclass;
1394 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1395 if (freq > chandef->chan->center_freq)
1396 *op_class = 122;
1397 else
1398 *op_class = 123;
1399 } else {
1400 *op_class = 121;
1401 }
1402
1403 return true;
1404 }
1405
1406 /* 5 GHz, channels 149..169 */
1407 if (freq >= 5745 && freq <= 5845) {
1408 if (vht_opclass) {
1409 *op_class = vht_opclass;
1410 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1411 if (freq > chandef->chan->center_freq)
1412 *op_class = 126;
1413 else
1414 *op_class = 127;
1415 } else if (freq <= 5805) {
1416 *op_class = 124;
1417 } else {
1418 *op_class = 125;
1419 }
1420
1421 return true;
1422 }
1423
1424 /* 56.16 GHz, channel 1..4 */
1425 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
1426 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1427 return false;
1428
1429 *op_class = 180;
1430 return true;
1431 }
1432
1433 /* not supported yet */
1434 return false;
1435}
1436EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1437
Johannes Berg654c3b92016-10-21 14:25:13 +02001438static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1439 u32 *beacon_int_gcd,
1440 bool *beacon_int_different)
1441{
1442 struct wireless_dev *wdev;
1443
1444 *beacon_int_gcd = 0;
1445 *beacon_int_different = false;
1446
1447 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1448 if (!wdev->beacon_interval)
1449 continue;
1450
1451 if (!*beacon_int_gcd) {
1452 *beacon_int_gcd = wdev->beacon_interval;
1453 continue;
1454 }
1455
1456 if (wdev->beacon_interval == *beacon_int_gcd)
1457 continue;
1458
1459 *beacon_int_different = true;
1460 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1461 }
1462
1463 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1464 if (*beacon_int_gcd)
1465 *beacon_int_different = true;
1466 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1467 }
1468}
1469
Johannes Berg56d18932011-05-09 18:41:15 +02001470int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
Purushottam Kushwahac6800ff2016-10-12 18:26:51 +05301471 enum nl80211_iftype iftype, u32 beacon_int)
Johannes Berg56d18932011-05-09 18:41:15 +02001472{
Johannes Berg654c3b92016-10-21 14:25:13 +02001473 /*
1474 * This is just a basic pre-condition check; if interface combinations
1475 * are possible the driver must already be checking those with a call
1476 * to cfg80211_check_combinations(), in which case we'll validate more
1477 * through the cfg80211_calculate_bi_data() call and code in
1478 * cfg80211_iter_combinations().
1479 */
Johannes Berg56d18932011-05-09 18:41:15 +02001480
Purushottam Kushwaha12d20fc92016-08-11 15:14:02 +05301481 if (beacon_int < 10 || beacon_int > 10000)
Johannes Berg56d18932011-05-09 18:41:15 +02001482 return -EINVAL;
1483
Johannes Berg654c3b92016-10-21 14:25:13 +02001484 return 0;
Johannes Berg56d18932011-05-09 18:41:15 +02001485}
Johannes Berg7527a782011-05-13 10:58:57 +02001486
Michal Kazior65a124d2014-04-09 15:29:22 +02001487int cfg80211_iter_combinations(struct wiphy *wiphy,
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301488 struct iface_combination_params *params,
Michal Kazior65a124d2014-04-09 15:29:22 +02001489 void (*iter)(const struct ieee80211_iface_combination *c,
1490 void *data),
1491 void *data)
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001492{
Felix Fietkau8c48b502014-05-05 11:48:40 +02001493 const struct ieee80211_regdomain *regdom;
1494 enum nl80211_dfs_regions region = 0;
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001495 int i, j, iftype;
1496 int num_interfaces = 0;
1497 u32 used_iftypes = 0;
Johannes Berg654c3b92016-10-21 14:25:13 +02001498 u32 beacon_int_gcd;
1499 bool beacon_int_different;
1500
1501 /*
1502 * This is a bit strange, since the iteration used to rely only on
1503 * the data given by the driver, but here it now relies on context,
1504 * in form of the currently operating interfaces.
1505 * This is OK for all current users, and saves us from having to
1506 * push the GCD calculations into all the drivers.
1507 * In the future, this should probably rely more on data that's in
1508 * cfg80211 already - the only thing not would appear to be any new
1509 * interfaces (while being brought up) and channel/radar data.
1510 */
1511 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1512 &beacon_int_gcd, &beacon_int_different);
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001513
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301514 if (params->radar_detect) {
Felix Fietkau8c48b502014-05-05 11:48:40 +02001515 rcu_read_lock();
1516 regdom = rcu_dereference(cfg80211_regdomain);
1517 if (regdom)
1518 region = regdom->dfs_region;
1519 rcu_read_unlock();
1520 }
1521
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001522 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301523 num_interfaces += params->iftype_num[iftype];
1524 if (params->iftype_num[iftype] > 0 &&
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001525 !(wiphy->software_iftypes & BIT(iftype)))
1526 used_iftypes |= BIT(iftype);
1527 }
1528
1529 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1530 const struct ieee80211_iface_combination *c;
1531 struct ieee80211_iface_limit *limits;
1532 u32 all_iftypes = 0;
1533
1534 c = &wiphy->iface_combinations[i];
1535
1536 if (num_interfaces > c->max_interfaces)
1537 continue;
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301538 if (params->num_different_channels > c->num_different_channels)
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001539 continue;
1540
1541 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1542 GFP_KERNEL);
1543 if (!limits)
1544 return -ENOMEM;
1545
1546 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1547 if (wiphy->software_iftypes & BIT(iftype))
1548 continue;
1549 for (j = 0; j < c->n_limits; j++) {
1550 all_iftypes |= limits[j].types;
1551 if (!(limits[j].types & BIT(iftype)))
1552 continue;
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301553 if (limits[j].max < params->iftype_num[iftype])
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001554 goto cont;
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301555 limits[j].max -= params->iftype_num[iftype];
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001556 }
1557 }
1558
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301559 if (params->radar_detect !=
1560 (c->radar_detect_widths & params->radar_detect))
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001561 goto cont;
1562
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301563 if (params->radar_detect && c->radar_detect_regions &&
Felix Fietkau8c48b502014-05-05 11:48:40 +02001564 !(c->radar_detect_regions & BIT(region)))
1565 goto cont;
1566
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001567 /* Finally check that all iftypes that we're currently
1568 * using are actually part of this combination. If they
1569 * aren't then we can't use this combination and have
1570 * to continue to the next.
1571 */
1572 if ((all_iftypes & used_iftypes) != used_iftypes)
1573 goto cont;
1574
Johannes Berg654c3b92016-10-21 14:25:13 +02001575 if (beacon_int_gcd) {
Purushottam Kushwahac6800ff2016-10-12 18:26:51 +05301576 if (c->beacon_int_min_gcd &&
Johannes Berg654c3b92016-10-21 14:25:13 +02001577 beacon_int_gcd < c->beacon_int_min_gcd)
Johannes Berg95b55f92016-10-21 12:15:00 +02001578 goto cont;
Johannes Berg654c3b92016-10-21 14:25:13 +02001579 if (!c->beacon_int_min_gcd && beacon_int_different)
Purushottam Kushwahac6800ff2016-10-12 18:26:51 +05301580 goto cont;
1581 }
1582
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001583 /* This combination covered all interface types and
1584 * supported the requested numbers, so we're good.
1585 */
Michal Kazior65a124d2014-04-09 15:29:22 +02001586
1587 (*iter)(c, data);
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001588 cont:
1589 kfree(limits);
1590 }
1591
Michal Kazior65a124d2014-04-09 15:29:22 +02001592 return 0;
1593}
1594EXPORT_SYMBOL(cfg80211_iter_combinations);
1595
1596static void
1597cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1598 void *data)
1599{
1600 int *num = data;
1601 (*num)++;
1602}
1603
1604int cfg80211_check_combinations(struct wiphy *wiphy,
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301605 struct iface_combination_params *params)
Michal Kazior65a124d2014-04-09 15:29:22 +02001606{
1607 int err, num = 0;
1608
Purushottam Kushwaha11716392016-10-12 18:25:35 +05301609 err = cfg80211_iter_combinations(wiphy, params,
Michal Kazior65a124d2014-04-09 15:29:22 +02001610 cfg80211_iter_sum_ifcombs, &num);
1611 if (err)
1612 return err;
1613 if (num == 0)
1614 return -EBUSY;
1615
1616 return 0;
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001617}
1618EXPORT_SYMBOL(cfg80211_check_combinations);
1619
Johannes Berg34850ab2011-07-18 18:08:35 +02001620int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1621 const u8 *rates, unsigned int n_rates,
1622 u32 *mask)
1623{
1624 int i, j;
1625
Johannes Berga401d2b2011-07-20 00:52:16 +02001626 if (!sband)
1627 return -EINVAL;
1628
Johannes Berg34850ab2011-07-18 18:08:35 +02001629 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1630 return -EINVAL;
1631
1632 *mask = 0;
1633
1634 for (i = 0; i < n_rates; i++) {
1635 int rate = (rates[i] & 0x7f) * 5;
1636 bool found = false;
1637
1638 for (j = 0; j < sband->n_bitrates; j++) {
1639 if (sband->bitrates[j].bitrate == rate) {
1640 found = true;
1641 *mask |= BIT(j);
1642 break;
1643 }
1644 }
1645 if (!found)
1646 return -EINVAL;
1647 }
1648
1649 /*
1650 * mask must have at least one bit set here since we
1651 * didn't accept a 0-length rates array nor allowed
1652 * entries in the array that didn't exist
1653 */
1654
1655 return 0;
1656}
Johannes Berg11a2a352011-11-21 11:09:22 +01001657
Ilan Peerbdfbec22014-01-09 11:37:23 +02001658unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1659{
Johannes Berg57fbcce2016-04-12 15:56:15 +02001660 enum nl80211_band band;
Ilan Peerbdfbec22014-01-09 11:37:23 +02001661 unsigned int n_channels = 0;
1662
Johannes Berg57fbcce2016-04-12 15:56:15 +02001663 for (band = 0; band < NUM_NL80211_BANDS; band++)
Ilan Peerbdfbec22014-01-09 11:37:23 +02001664 if (wiphy->bands[band])
1665 n_channels += wiphy->bands[band]->n_channels;
1666
1667 return n_channels;
1668}
1669EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1670
Antonio Quartulli74063532014-05-19 21:53:21 +02001671int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1672 struct station_info *sinfo)
1673{
1674 struct cfg80211_registered_device *rdev;
1675 struct wireless_dev *wdev;
1676
1677 wdev = dev->ieee80211_ptr;
1678 if (!wdev)
1679 return -EOPNOTSUPP;
1680
1681 rdev = wiphy_to_rdev(wdev->wiphy);
1682 if (!rdev->ops->get_station)
1683 return -EOPNOTSUPP;
1684
1685 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1686}
1687EXPORT_SYMBOL(cfg80211_get_station);
1688
Ayala Bekera442b762016-09-20 17:31:15 +03001689void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1690{
1691 int i;
1692
1693 if (!f)
1694 return;
1695
1696 kfree(f->serv_spec_info);
1697 kfree(f->srf_bf);
1698 kfree(f->srf_macs);
1699 for (i = 0; i < f->num_rx_filters; i++)
1700 kfree(f->rx_filters[i].filter);
1701
1702 for (i = 0; i < f->num_tx_filters; i++)
1703 kfree(f->tx_filters[i].filter);
1704
1705 kfree(f->rx_filters);
1706 kfree(f->tx_filters);
1707 kfree(f);
1708}
1709EXPORT_SYMBOL(cfg80211_free_nan_func);
1710
Johannes Berg11a2a352011-11-21 11:09:22 +01001711/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1712/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1713const unsigned char rfc1042_header[] __aligned(2) =
1714 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1715EXPORT_SYMBOL(rfc1042_header);
1716
1717/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1718const unsigned char bridge_tunnel_header[] __aligned(2) =
1719 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1720EXPORT_SYMBOL(bridge_tunnel_header);
Johannes Berg7c62e272014-02-25 15:04:46 -08001721
1722bool cfg80211_is_gratuitous_arp_unsolicited_na(struct sk_buff *skb)
1723{
1724 const struct ethhdr *eth = (void *)skb->data;
1725 const struct {
1726 struct arphdr hdr;
1727 u8 ar_sha[ETH_ALEN];
1728 u8 ar_sip[4];
1729 u8 ar_tha[ETH_ALEN];
1730 u8 ar_tip[4];
1731 } __packed *arp;
1732 const struct ipv6hdr *ipv6;
1733 const struct icmp6hdr *icmpv6;
1734
1735 switch (eth->h_proto) {
1736 case cpu_to_be16(ETH_P_ARP):
1737 /* can't say - but will probably be dropped later anyway */
1738 if (!pskb_may_pull(skb, sizeof(*eth) + sizeof(*arp)))
1739 return false;
1740
1741 arp = (void *)(eth + 1);
1742
1743 if ((arp->hdr.ar_op == cpu_to_be16(ARPOP_REPLY) ||
1744 arp->hdr.ar_op == cpu_to_be16(ARPOP_REQUEST)) &&
1745 !memcmp(arp->ar_sip, arp->ar_tip, sizeof(arp->ar_sip)))
1746 return true;
1747 break;
1748 case cpu_to_be16(ETH_P_IPV6):
1749 /* can't say - but will probably be dropped later anyway */
1750 if (!pskb_may_pull(skb, sizeof(*eth) + sizeof(*ipv6) +
1751 sizeof(*icmpv6)))
1752 return false;
1753
1754 ipv6 = (void *)(eth + 1);
1755 icmpv6 = (void *)(ipv6 + 1);
1756
1757 if (icmpv6->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT &&
1758 !memcmp(&ipv6->saddr, &ipv6->daddr, sizeof(ipv6->saddr)))
1759 return true;
1760 break;
1761 default:
1762 /*
1763 * no need to support other protocols, proxy service isn't
1764 * specified for any others
1765 */
1766 break;
1767 }
1768
1769 return false;
1770}
1771EXPORT_SYMBOL(cfg80211_is_gratuitous_arp_unsolicited_na);