blob: 7a1fcc6ee0606843063cb2c4952d68e091d9de09 [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 Berg4c8dea62016-10-21 14:25:13 +020016#include <linux/gcd.h>
Johannes Berg8318d782008-01-24 19:38:38 +010017#include "core.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030018#include "rdev-ops.h"
19
Johannes Berg8318d782008-01-24 19:38:38 +010020
Johannes Bergbd815252008-10-29 20:00:45 +010021struct ieee80211_rate *
22ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
Johannes Berg881d9482009-01-21 15:13:48 +010023 u32 basic_rates, int bitrate)
Johannes Bergbd815252008-10-29 20:00:45 +010024{
25 struct ieee80211_rate *result = &sband->bitrates[0];
26 int i;
27
28 for (i = 0; i < sband->n_bitrates; i++) {
29 if (!(basic_rates & BIT(i)))
30 continue;
31 if (sband->bitrates[i].bitrate > bitrate)
32 continue;
33 result = &sband->bitrates[i];
34 }
35
36 return result;
37}
38EXPORT_SYMBOL(ieee80211_get_response_rate);
39
Simon Wunderlich74608ac2013-07-08 16:55:54 +020040u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
41 enum nl80211_bss_scan_width scan_width)
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070042{
43 struct ieee80211_rate *bitrates;
44 u32 mandatory_rates = 0;
45 enum ieee80211_rate_flags mandatory_flag;
46 int i;
47
48 if (WARN_ON(!sband))
49 return 1;
50
Johannes Berg57fbcce2016-04-12 15:56:15 +020051 if (sband->band == NL80211_BAND_2GHZ) {
Simon Wunderlich74608ac2013-07-08 16:55:54 +020052 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
53 scan_width == NL80211_BSS_CHAN_WIDTH_10)
54 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
55 else
56 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
57 } else {
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070058 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
Simon Wunderlich74608ac2013-07-08 16:55:54 +020059 }
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070060
61 bitrates = sband->bitrates;
62 for (i = 0; i < sband->n_bitrates; i++)
63 if (bitrates[i].flags & mandatory_flag)
64 mandatory_rates |= BIT(i);
65 return mandatory_rates;
66}
67EXPORT_SYMBOL(ieee80211_mandatory_rates);
68
Johannes Berg57fbcce2016-04-12 15:56:15 +020069int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +010070{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090071 /* see 802.11 17.3.8.3.2 and Annex J
72 * there are overlapping channel numbers in 5GHz and 2GHz bands */
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030073 if (chan <= 0)
74 return 0; /* not supported */
75 switch (band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +020076 case NL80211_BAND_2GHZ:
Bruno Randolf59eb21a2011-01-17 13:37:28 +090077 if (chan == 14)
78 return 2484;
79 else if (chan < 14)
80 return 2407 + chan * 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030081 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +020082 case NL80211_BAND_5GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030083 if (chan >= 182 && chan <= 196)
84 return 4000 + chan * 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090085 else
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030086 return 5000 + chan * 5;
87 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +020088 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030089 if (chan < 5)
90 return 56160 + chan * 2160;
91 break;
92 default:
93 ;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090094 }
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030095 return 0; /* not supported */
Johannes Berg8318d782008-01-24 19:38:38 +010096}
97EXPORT_SYMBOL(ieee80211_channel_to_frequency);
98
99int ieee80211_frequency_to_channel(int freq)
100{
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900101 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +0100102 if (freq == 2484)
103 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900104 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +0100105 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900106 else if (freq >= 4910 && freq <= 4980)
107 return (freq - 4000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300108 else if (freq <= 45000) /* DMG band lower limit */
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900109 return (freq - 5000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300110 else if (freq >= 58320 && freq <= 64800)
111 return (freq - 56160) / 2160;
112 else
113 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100114}
115EXPORT_SYMBOL(ieee80211_frequency_to_channel);
116
Arend Van Spriel543b9212016-11-17 12:48:53 +0000117struct ieee80211_channel *ieee80211_get_channel(struct wiphy *wiphy, int freq)
Johannes Berg906c7302008-03-16 18:34:33 +0100118{
Johannes Berg57fbcce2016-04-12 15:56:15 +0200119 enum nl80211_band band;
Johannes Berg906c7302008-03-16 18:34:33 +0100120 struct ieee80211_supported_band *sband;
121 int i;
122
Johannes Berg57fbcce2016-04-12 15:56:15 +0200123 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Berg906c7302008-03-16 18:34:33 +0100124 sband = wiphy->bands[band];
125
126 if (!sband)
127 continue;
128
129 for (i = 0; i < sband->n_channels; i++) {
130 if (sband->channels[i].center_freq == freq)
131 return &sband->channels[i];
132 }
133 }
134
135 return NULL;
136}
Arend Van Spriel543b9212016-11-17 12:48:53 +0000137EXPORT_SYMBOL(ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +0100138
Arend Van Spriel343884c2017-01-04 10:53:37 +0000139static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +0100140{
141 int i, want;
142
Arend Van Spriel343884c2017-01-04 10:53:37 +0000143 switch (sband->band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200144 case NL80211_BAND_5GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100145 want = 3;
146 for (i = 0; i < sband->n_bitrates; i++) {
147 if (sband->bitrates[i].bitrate == 60 ||
148 sband->bitrates[i].bitrate == 120 ||
149 sband->bitrates[i].bitrate == 240) {
150 sband->bitrates[i].flags |=
151 IEEE80211_RATE_MANDATORY_A;
152 want--;
153 }
154 }
155 WARN_ON(want);
156 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200157 case NL80211_BAND_2GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100158 want = 7;
159 for (i = 0; i < sband->n_bitrates; i++) {
Richard Schütz1bd773c2017-09-07 17:47:43 +0200160 switch (sband->bitrates[i].bitrate) {
161 case 10:
162 case 20:
163 case 55:
164 case 110:
Johannes Berg8318d782008-01-24 19:38:38 +0100165 sband->bitrates[i].flags |=
166 IEEE80211_RATE_MANDATORY_B |
167 IEEE80211_RATE_MANDATORY_G;
168 want--;
Richard Schütz1bd773c2017-09-07 17:47:43 +0200169 break;
170 case 60:
171 case 120:
172 case 240:
Johannes Berg8318d782008-01-24 19:38:38 +0100173 sband->bitrates[i].flags |=
174 IEEE80211_RATE_MANDATORY_G;
175 want--;
Richard Schütz1bd773c2017-09-07 17:47:43 +0200176 /* fall through */
177 default:
Johannes Berg8318d782008-01-24 19:38:38 +0100178 sband->bitrates[i].flags |=
179 IEEE80211_RATE_ERP_G;
Richard Schütz1bd773c2017-09-07 17:47:43 +0200180 break;
181 }
Johannes Berg8318d782008-01-24 19:38:38 +0100182 }
Richard Schütz1bd773c2017-09-07 17:47:43 +0200183 WARN_ON(want != 0 && want != 3);
Johannes Berg8318d782008-01-24 19:38:38 +0100184 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200185 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300186 /* check for mandatory HT MCS 1..4 */
187 WARN_ON(!sband->ht_cap.ht_supported);
188 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
189 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200190 case NUM_NL80211_BANDS:
Arend Van Spriel343884c2017-01-04 10:53:37 +0000191 default:
Johannes Berg8318d782008-01-24 19:38:38 +0100192 WARN_ON(1);
193 break;
194 }
195}
196
197void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
198{
Johannes Berg57fbcce2016-04-12 15:56:15 +0200199 enum nl80211_band band;
Johannes Berg8318d782008-01-24 19:38:38 +0100200
Johannes Berg57fbcce2016-04-12 15:56:15 +0200201 for (band = 0; band < NUM_NL80211_BANDS; band++)
Johannes Berg8318d782008-01-24 19:38:38 +0100202 if (wiphy->bands[band])
Arend Van Spriel343884c2017-01-04 10:53:37 +0000203 set_mandatory_flags_band(wiphy->bands[band]);
Johannes Berg8318d782008-01-24 19:38:38 +0100204}
Johannes Berg08645122009-05-11 13:54:58 +0200205
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300206bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
207{
208 int i;
209 for (i = 0; i < wiphy->n_cipher_suites; i++)
210 if (cipher == wiphy->cipher_suites[i])
211 return true;
212 return false;
213}
214
Johannes Bergfffd0932009-07-08 14:22:54 +0200215int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
216 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200217 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200218{
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200219 if (key_idx < 0 || key_idx > 5)
Johannes Berg08645122009-05-11 13:54:58 +0200220 return -EINVAL;
221
Johannes Berge31b8212010-10-05 19:39:30 +0200222 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
223 return -EINVAL;
224
225 if (pairwise && !mac_addr)
226 return -EINVAL;
227
Jouni Malinen37720562015-01-24 19:52:04 +0200228 switch (params->cipher) {
229 case WLAN_CIPHER_SUITE_TKIP:
230 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200231 case WLAN_CIPHER_SUITE_CCMP_256:
232 case WLAN_CIPHER_SUITE_GCMP:
233 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinen37720562015-01-24 19:52:04 +0200234 /* Disallow pairwise keys with non-zero index unless it's WEP
235 * or a vendor specific cipher (because current deployments use
236 * pairwise WEP keys with non-zero indices and for vendor
237 * specific ciphers this should be validated in the driver or
238 * hardware level - but 802.11i clearly specifies to use zero)
239 */
240 if (pairwise && key_idx)
241 return -EINVAL;
242 break;
243 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200244 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
245 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
246 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen37720562015-01-24 19:52:04 +0200247 /* Disallow BIP (group-only) cipher as pairwise cipher */
248 if (pairwise)
249 return -EINVAL;
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200250 if (key_idx < 4)
251 return -EINVAL;
Jouni Malinen37720562015-01-24 19:52:04 +0200252 break;
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200253 case WLAN_CIPHER_SUITE_WEP40:
254 case WLAN_CIPHER_SUITE_WEP104:
255 if (key_idx > 3)
256 return -EINVAL;
Jouni Malinen37720562015-01-24 19:52:04 +0200257 default:
258 break;
259 }
Johannes Berg08645122009-05-11 13:54:58 +0200260
Johannes Berg08645122009-05-11 13:54:58 +0200261 switch (params->cipher) {
262 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200263 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200264 return -EINVAL;
265 break;
266 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200267 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200268 return -EINVAL;
269 break;
270 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200271 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200272 return -EINVAL;
273 break;
Jouni Malinencfcf1682015-01-24 19:52:05 +0200274 case WLAN_CIPHER_SUITE_CCMP_256:
275 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
276 return -EINVAL;
277 break;
278 case WLAN_CIPHER_SUITE_GCMP:
279 if (params->key_len != WLAN_KEY_LEN_GCMP)
280 return -EINVAL;
281 break;
282 case WLAN_CIPHER_SUITE_GCMP_256:
283 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
284 return -EINVAL;
285 break;
Johannes Berg08645122009-05-11 13:54:58 +0200286 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200287 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200288 return -EINVAL;
289 break;
290 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200291 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200292 return -EINVAL;
293 break;
Jouni Malinencfcf1682015-01-24 19:52:05 +0200294 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
295 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
296 return -EINVAL;
297 break;
298 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
299 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
300 return -EINVAL;
301 break;
302 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
303 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
304 return -EINVAL;
305 break;
Johannes Berg08645122009-05-11 13:54:58 +0200306 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300307 /*
308 * We don't know anything about this algorithm,
309 * allow using it -- but the driver must check
310 * all parameters! We still check below whether
311 * or not the driver supports this algorithm,
312 * of course.
313 */
314 break;
Johannes Berg08645122009-05-11 13:54:58 +0200315 }
316
Jouni Malinen9f26a952009-05-15 12:38:32 +0300317 if (params->seq) {
318 switch (params->cipher) {
319 case WLAN_CIPHER_SUITE_WEP40:
320 case WLAN_CIPHER_SUITE_WEP104:
321 /* These ciphers do not use key sequence */
322 return -EINVAL;
323 case WLAN_CIPHER_SUITE_TKIP:
324 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200325 case WLAN_CIPHER_SUITE_CCMP_256:
326 case WLAN_CIPHER_SUITE_GCMP:
327 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinen9f26a952009-05-15 12:38:32 +0300328 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200329 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
330 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
331 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen9f26a952009-05-15 12:38:32 +0300332 if (params->seq_len != 6)
333 return -EINVAL;
334 break;
335 }
336 }
337
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300338 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200339 return -EINVAL;
340
Johannes Berg08645122009-05-11 13:54:58 +0200341 return 0;
342}
Zhu Yie31a16d2009-05-21 21:47:03 +0800343
Johannes Berg633adf12010-08-12 14:49:58 +0200344unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800345{
346 unsigned int hdrlen = 24;
347
348 if (ieee80211_is_data(fc)) {
349 if (ieee80211_has_a4(fc))
350 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200351 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800352 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200353 if (ieee80211_has_order(fc))
354 hdrlen += IEEE80211_HT_CTL_LEN;
355 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800356 goto out;
357 }
358
Fred Choufb142f42015-01-20 10:17:27 +0800359 if (ieee80211_is_mgmt(fc)) {
360 if (ieee80211_has_order(fc))
361 hdrlen += IEEE80211_HT_CTL_LEN;
362 goto out;
363 }
364
Zhu Yie31a16d2009-05-21 21:47:03 +0800365 if (ieee80211_is_ctl(fc)) {
366 /*
367 * ACK and CTS are 10 bytes, all others 16. To see how
368 * to get this condition consider
369 * subtype mask: 0b0000000011110000 (0x00F0)
370 * ACK subtype: 0b0000000011010000 (0x00D0)
371 * CTS subtype: 0b0000000011000000 (0x00C0)
372 * bits that matter: ^^^ (0x00E0)
373 * value of those: 0b0000000011000000 (0x00C0)
374 */
375 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
376 hdrlen = 10;
377 else
378 hdrlen = 16;
379 }
380out:
381 return hdrlen;
382}
383EXPORT_SYMBOL(ieee80211_hdrlen);
384
385unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
386{
387 const struct ieee80211_hdr *hdr =
388 (const struct ieee80211_hdr *)skb->data;
389 unsigned int hdrlen;
390
391 if (unlikely(skb->len < 10))
392 return 0;
393 hdrlen = ieee80211_hdrlen(hdr->frame_control);
394 if (unlikely(hdrlen > skb->len))
395 return 0;
396 return hdrlen;
397}
398EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
399
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100400static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
Zhu Yie31a16d2009-05-21 21:47:03 +0800401{
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100402 int ae = flags & MESH_FLAGS_AE;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200403 /* 802.11-2012, 8.2.4.7.3 */
Zhu Yie31a16d2009-05-21 21:47:03 +0800404 switch (ae) {
Johannes Berg7dd111e2012-10-25 21:51:59 +0200405 default:
Zhu Yie31a16d2009-05-21 21:47:03 +0800406 case 0:
407 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700408 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800409 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700410 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800411 return 18;
Zhu Yie31a16d2009-05-21 21:47:03 +0800412 }
413}
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100414
415unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
416{
417 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
418}
Johannes Berg9b395bc2012-10-26 00:36:40 +0200419EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800420
Johannes Berg7f6990c2016-10-05 15:29:49 +0200421int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
422 const u8 *addr, enum nl80211_iftype iftype)
Zhu Yie31a16d2009-05-21 21:47:03 +0800423{
424 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100425 struct {
426 u8 hdr[ETH_ALEN] __aligned(2);
427 __be16 proto;
428 } payload;
429 struct ethhdr tmp;
430 u16 hdrlen;
431 u8 mesh_flags = 0;
Zhu Yie31a16d2009-05-21 21:47:03 +0800432
433 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
434 return -1;
435
436 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100437 if (skb->len < hdrlen + 8)
438 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800439
440 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
441 * header
442 * IEEE 802.11 address fields:
443 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
444 * 0 0 DA SA BSSID n/a
445 * 0 1 DA BSSID SA n/a
446 * 1 0 BSSID SA DA n/a
447 * 1 1 RA TA DA SA
448 */
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100449 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
450 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
451
452 if (iftype == NL80211_IFTYPE_MESH_POINT)
453 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
Zhu Yie31a16d2009-05-21 21:47:03 +0800454
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700455 mesh_flags &= MESH_FLAGS_AE;
456
Zhu Yie31a16d2009-05-21 21:47:03 +0800457 switch (hdr->frame_control &
458 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
459 case cpu_to_le16(IEEE80211_FCTL_TODS):
460 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200461 iftype != NL80211_IFTYPE_AP_VLAN &&
462 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800463 return -1;
464 break;
465 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
466 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543ee2009-11-10 20:10:05 +0100467 iftype != NL80211_IFTYPE_MESH_POINT &&
468 iftype != NL80211_IFTYPE_AP_VLAN &&
469 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800470 return -1;
471 if (iftype == NL80211_IFTYPE_MESH_POINT) {
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700472 if (mesh_flags == MESH_FLAGS_AE_A4)
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800473 return -1;
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700474 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800475 skb_copy_bits(skb, hdrlen +
476 offsetof(struct ieee80211s_hdr, eaddr1),
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100477 tmp.h_dest, 2 * ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800478 }
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100479 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
Zhu Yie31a16d2009-05-21 21:47:03 +0800480 }
481 break;
482 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700483 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200484 iftype != NL80211_IFTYPE_P2P_CLIENT &&
485 iftype != NL80211_IFTYPE_MESH_POINT) ||
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100486 (is_multicast_ether_addr(tmp.h_dest) &&
487 ether_addr_equal(tmp.h_source, addr)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800488 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700489 if (iftype == NL80211_IFTYPE_MESH_POINT) {
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700490 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800491 return -1;
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700492 if (mesh_flags == MESH_FLAGS_AE_A4)
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800493 skb_copy_bits(skb, hdrlen +
494 offsetof(struct ieee80211s_hdr, eaddr1),
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100495 tmp.h_source, ETH_ALEN);
496 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700497 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800498 break;
499 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300500 if (iftype != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100501 iftype != NL80211_IFTYPE_STATION &&
502 iftype != NL80211_IFTYPE_OCB)
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300503 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800504 break;
505 }
506
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100507 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
508 tmp.h_proto = payload.proto;
Zhu Yie31a16d2009-05-21 21:47:03 +0800509
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100510 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
511 tmp.h_proto != htons(ETH_P_AARP) &&
512 tmp.h_proto != htons(ETH_P_IPX)) ||
513 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800514 /* remove RFC1042 or Bridge-Tunnel encapsulation and
515 * replace EtherType */
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100516 hdrlen += ETH_ALEN + 2;
517 else
Felix Fietkauc0417782016-06-29 10:36:39 +0200518 tmp.h_proto = htons(skb->len - hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800519
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100520 pskb_pull(skb, hdrlen);
521
522 if (!ehdr)
Johannes Bergd58ff352017-06-16 14:29:23 +0200523 ehdr = skb_push(skb, sizeof(struct ethhdr));
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100524 memcpy(ehdr, &tmp, sizeof(tmp));
525
Zhu Yie31a16d2009-05-21 21:47:03 +0800526 return 0;
527}
Johannes Berg7f6990c2016-10-05 15:29:49 +0200528EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800529
Felix Fietkau2b67f942016-02-08 14:34:42 +0100530static void
531__frame_add_frag(struct sk_buff *skb, struct page *page,
532 void *ptr, int len, int size)
533{
534 struct skb_shared_info *sh = skb_shinfo(skb);
535 int page_offset;
536
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700537 page_ref_inc(page);
Felix Fietkau2b67f942016-02-08 14:34:42 +0100538 page_offset = ptr - page_address(page);
539 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
540}
541
542static void
543__ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
544 int offset, int len)
545{
546 struct skb_shared_info *sh = skb_shinfo(skb);
Matthias Kaehlckeaa1702d2017-04-13 10:05:04 -0700547 const skb_frag_t *frag = &sh->frags[0];
Felix Fietkau2b67f942016-02-08 14:34:42 +0100548 struct page *frag_page;
549 void *frag_ptr;
550 int frag_len, frag_size;
551 int head_size = skb->len - skb->data_len;
552 int cur_len;
553
554 frag_page = virt_to_head_page(skb->head);
555 frag_ptr = skb->data;
556 frag_size = head_size;
557
558 while (offset >= frag_size) {
559 offset -= frag_size;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100560 frag_page = skb_frag_page(frag);
561 frag_ptr = skb_frag_address(frag);
562 frag_size = skb_frag_size(frag);
Matthias Kaehlckeaa1702d2017-04-13 10:05:04 -0700563 frag++;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100564 }
565
566 frag_ptr += offset;
567 frag_len = frag_size - offset;
568
569 cur_len = min(len, frag_len);
570
571 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
572 len -= cur_len;
573
574 while (len > 0) {
Felix Fietkau2b67f942016-02-08 14:34:42 +0100575 frag_len = skb_frag_size(frag);
576 cur_len = min(len, frag_len);
577 __frame_add_frag(frame, skb_frag_page(frag),
578 skb_frag_address(frag), cur_len, frag_len);
579 len -= cur_len;
Matthias Kaehlckeaa1702d2017-04-13 10:05:04 -0700580 frag++;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100581 }
582}
583
Felix Fietkau230fd282016-02-02 14:39:10 +0100584static struct sk_buff *
585__ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
Felix Fietkau2b67f942016-02-08 14:34:42 +0100586 int offset, int len, bool reuse_frag)
Felix Fietkau230fd282016-02-02 14:39:10 +0100587{
588 struct sk_buff *frame;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100589 int cur_len = len;
Felix Fietkau230fd282016-02-02 14:39:10 +0100590
591 if (skb->len - offset < len)
592 return NULL;
593
594 /*
Felix Fietkau2b67f942016-02-08 14:34:42 +0100595 * When reusing framents, copy some data to the head to simplify
596 * ethernet header handling and speed up protocol header processing
597 * in the stack later.
598 */
599 if (reuse_frag)
600 cur_len = min_t(int, len, 32);
601
602 /*
Felix Fietkau230fd282016-02-02 14:39:10 +0100603 * Allocate and reserve two bytes more for payload
604 * alignment since sizeof(struct ethhdr) is 14.
605 */
Felix Fietkau2b67f942016-02-08 14:34:42 +0100606 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
Gregory Greenman16a910a2016-07-05 15:23:10 +0300607 if (!frame)
608 return NULL;
Felix Fietkau230fd282016-02-02 14:39:10 +0100609
610 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
Felix Fietkau2b67f942016-02-08 14:34:42 +0100611 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
612
613 len -= cur_len;
614 if (!len)
615 return frame;
616
617 offset += cur_len;
618 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
Felix Fietkau230fd282016-02-02 14:39:10 +0100619
620 return frame;
621}
Zhu Yieaf85ca2009-12-01 10:18:37 +0800622
623void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
624 const u8 *addr, enum nl80211_iftype iftype,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700625 const unsigned int extra_headroom,
Johannes Berg8b935ee2016-10-05 16:17:01 +0200626 const u8 *check_da, const u8 *check_sa)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800627{
Felix Fietkau230fd282016-02-02 14:39:10 +0100628 unsigned int hlen = ALIGN(extra_headroom, 4);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800629 struct sk_buff *frame = NULL;
630 u16 ethertype;
631 u8 *payload;
Johannes Berg7f6990c2016-10-05 15:29:49 +0200632 int offset = 0, remaining;
Felix Fietkau230fd282016-02-02 14:39:10 +0100633 struct ethhdr eth;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100634 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
Felix Fietkau2bf0ccc2016-02-08 14:25:26 +0100635 bool reuse_skb = false;
Felix Fietkau230fd282016-02-02 14:39:10 +0100636 bool last = false;
Felix Fietkau88665f52016-02-02 14:39:08 +0100637
Felix Fietkau230fd282016-02-02 14:39:10 +0100638 while (!last) {
639 unsigned int subframe_len;
640 int len;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800641 u8 padding;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800642
Felix Fietkau230fd282016-02-02 14:39:10 +0100643 skb_copy_bits(skb, offset, &eth, sizeof(eth));
644 len = ntohs(eth.h_proto);
645 subframe_len = sizeof(struct ethhdr) + len;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800646 padding = (4 - subframe_len) & 0x3;
Felix Fietkau230fd282016-02-02 14:39:10 +0100647
Zhu Yieaf85ca2009-12-01 10:18:37 +0800648 /* the last MSDU has no padding */
Felix Fietkau230fd282016-02-02 14:39:10 +0100649 remaining = skb->len - offset;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800650 if (subframe_len > remaining)
651 goto purge;
652
Felix Fietkau230fd282016-02-02 14:39:10 +0100653 offset += sizeof(struct ethhdr);
Felix Fietkau230fd282016-02-02 14:39:10 +0100654 last = remaining <= subframe_len + padding;
Johannes Berg8b935ee2016-10-05 16:17:01 +0200655
656 /* FIXME: should we really accept multicast DA? */
657 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
658 !ether_addr_equal(check_da, eth.h_dest)) ||
659 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
660 offset += len + padding;
661 continue;
662 }
663
664 /* reuse skb for the last subframe */
Felix Fietkau2b67f942016-02-08 14:34:42 +0100665 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
Felix Fietkau230fd282016-02-02 14:39:10 +0100666 skb_pull(skb, offset);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800667 frame = skb;
Felix Fietkau230fd282016-02-02 14:39:10 +0100668 reuse_skb = true;
669 } else {
Felix Fietkau2b67f942016-02-08 14:34:42 +0100670 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
671 reuse_frag);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800672 if (!frame)
673 goto purge;
674
Felix Fietkau230fd282016-02-02 14:39:10 +0100675 offset += len + padding;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800676 }
677
678 skb_reset_network_header(frame);
679 frame->dev = skb->dev;
680 frame->priority = skb->priority;
681
682 payload = frame->data;
683 ethertype = (payload[6] << 8) | payload[7];
Joe Perchesac422d32012-05-08 18:56:55 +0000684 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yieaf85ca2009-12-01 10:18:37 +0800685 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perchesac422d32012-05-08 18:56:55 +0000686 ether_addr_equal(payload, bridge_tunnel_header))) {
Felix Fietkau230fd282016-02-02 14:39:10 +0100687 eth.h_proto = htons(ethertype);
688 skb_pull(frame, ETH_ALEN + 2);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800689 }
Felix Fietkau230fd282016-02-02 14:39:10 +0100690
691 memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
Zhu Yieaf85ca2009-12-01 10:18:37 +0800692 __skb_queue_tail(list, frame);
693 }
694
Felix Fietkau230fd282016-02-02 14:39:10 +0100695 if (!reuse_skb)
696 dev_kfree_skb(skb);
697
Zhu Yieaf85ca2009-12-01 10:18:37 +0800698 return;
699
700 purge:
701 __skb_queue_purge(list);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800702 dev_kfree_skb(skb);
703}
704EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
705
Zhu Yie31a16d2009-05-21 21:47:03 +0800706/* Given a data frame determine the 802.1p/1d tag to use. */
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800707unsigned int cfg80211_classify8021d(struct sk_buff *skb,
708 struct cfg80211_qos_map *qos_map)
Zhu Yie31a16d2009-05-21 21:47:03 +0800709{
710 unsigned int dscp;
cedric Vonckenc6ca5e22013-08-26 14:04:52 +0200711 unsigned char vlan_priority;
Zhu Yie31a16d2009-05-21 21:47:03 +0800712
713 /* skb->priority values from 256->263 are magic values to
714 * directly indicate a specific 802.1d priority. This is used
715 * to allow 802.1d priority to be passed directly in from VLAN
716 * tags, etc.
717 */
718 if (skb->priority >= 256 && skb->priority <= 263)
719 return skb->priority - 256;
720
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100721 if (skb_vlan_tag_present(skb)) {
722 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
cedric Vonckenc6ca5e22013-08-26 14:04:52 +0200723 >> VLAN_PRIO_SHIFT;
724 if (vlan_priority > 0)
725 return vlan_priority;
726 }
727
Zhu Yie31a16d2009-05-21 21:47:03 +0800728 switch (skb->protocol) {
729 case htons(ETH_P_IP):
Dave Tähtb1565792011-12-22 12:55:08 -0800730 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
731 break;
732 case htons(ETH_P_IPV6):
733 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
Zhu Yie31a16d2009-05-21 21:47:03 +0800734 break;
Simon Wunderlich960d97f2014-03-03 17:23:12 +0100735 case htons(ETH_P_MPLS_UC):
736 case htons(ETH_P_MPLS_MC): {
737 struct mpls_label mpls_tmp, *mpls;
738
739 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
740 sizeof(*mpls), &mpls_tmp);
741 if (!mpls)
742 return 0;
743
744 return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
745 >> MPLS_LS_TC_SHIFT;
746 }
747 case htons(ETH_P_80221):
748 /* 802.21 is always network control traffic */
749 return 7;
Zhu Yie31a16d2009-05-21 21:47:03 +0800750 default:
751 return 0;
752 }
753
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800754 if (qos_map) {
755 unsigned int i, tmp_dscp = dscp >> 2;
756
757 for (i = 0; i < qos_map->num_des; i++) {
758 if (tmp_dscp == qos_map->dscp_exception[i].dscp)
759 return qos_map->dscp_exception[i].up;
760 }
761
762 for (i = 0; i < 8; i++) {
763 if (tmp_dscp >= qos_map->up[i].low &&
764 tmp_dscp <= qos_map->up[i].high)
765 return i;
766 }
767 }
768
Zhu Yie31a16d2009-05-21 21:47:03 +0800769 return dscp >> 5;
770}
771EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200772
773const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
774{
Johannes Berg9caf0362012-11-29 01:25:20 +0100775 const struct cfg80211_bss_ies *ies;
776
777 ies = rcu_dereference(bss->ies);
778 if (!ies)
Johannes Berg517357c2009-07-02 17:18:40 +0200779 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +0100780
781 return cfg80211_find_ie(ie, ies->data, ies->len);
Johannes Berg517357c2009-07-02 17:18:40 +0200782}
783EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200784
785void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
786{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800787 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200788 struct net_device *dev = wdev->netdev;
789 int i;
790
791 if (!wdev->connect_keys)
792 return;
793
David Spinadelb8676222016-09-22 23:16:50 +0300794 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200795 if (!wdev->connect_keys->params[i].cipher)
796 continue;
Hila Gonene35e4d22012-06-27 17:19:42 +0300797 if (rdev_add_key(rdev, dev, i, false, NULL,
798 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800799 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800800 continue;
801 }
Johannes Bergd4f29972017-02-13 20:53:38 +0100802 if (wdev->connect_keys->def == i &&
803 rdev_set_default_key(rdev, dev, i, true, true)) {
804 netdev_err(dev, "failed to set defkey %d\n", i);
805 continue;
806 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200807 }
808
Johannes Bergb47f6102014-09-10 13:39:54 +0300809 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200810 wdev->connect_keys = NULL;
811}
Johannes Berg3d54d252009-08-21 14:51:05 +0200812
Daniel Drake1f6fc432012-08-02 18:41:48 +0100813void cfg80211_process_wdev_events(struct wireless_dev *wdev)
Johannes Berg3d54d252009-08-21 14:51:05 +0200814{
815 struct cfg80211_event *ev;
816 unsigned long flags;
Johannes Berg3d54d252009-08-21 14:51:05 +0200817
818 spin_lock_irqsave(&wdev->event_lock, flags);
819 while (!list_empty(&wdev->event_list)) {
820 ev = list_first_entry(&wdev->event_list,
821 struct cfg80211_event, list);
822 list_del(&ev->list);
823 spin_unlock_irqrestore(&wdev->event_lock, flags);
824
825 wdev_lock(wdev);
826 switch (ev->type) {
827 case EVENT_CONNECT_RESULT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200828 __cfg80211_connect_result(
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300829 wdev->netdev,
830 &ev->cr,
831 ev->cr.status == WLAN_STATUS_SUCCESS);
Johannes Berg3d54d252009-08-21 14:51:05 +0200832 break;
833 case EVENT_ROAMED:
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300834 __cfg80211_roamed(wdev, &ev->rm);
Johannes Berg3d54d252009-08-21 14:51:05 +0200835 break;
836 case EVENT_DISCONNECTED:
837 __cfg80211_disconnected(wdev->netdev,
838 ev->dc.ie, ev->dc.ie_len,
Johannes Berg80279fb2015-05-22 16:22:20 +0200839 ev->dc.reason,
840 !ev->dc.locally_generated);
Johannes Berg3d54d252009-08-21 14:51:05 +0200841 break;
842 case EVENT_IBSS_JOINED:
Antonio Quartullife94f3a2014-01-29 17:53:43 +0100843 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
844 ev->ij.channel);
Johannes Berg3d54d252009-08-21 14:51:05 +0200845 break;
Michal Kaziorf04c2202014-04-09 15:11:01 +0200846 case EVENT_STOPPED:
847 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
848 break;
Johannes Berg3d54d252009-08-21 14:51:05 +0200849 }
850 wdev_unlock(wdev);
851
852 kfree(ev);
853
854 spin_lock_irqsave(&wdev->event_lock, flags);
855 }
856 spin_unlock_irqrestore(&wdev->event_lock, flags);
857}
858
859void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
860{
861 struct wireless_dev *wdev;
862
863 ASSERT_RTNL();
Johannes Berg3d54d252009-08-21 14:51:05 +0200864
Johannes Berg53873f12016-05-03 16:52:04 +0300865 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
Johannes Berg3d54d252009-08-21 14:51:05 +0200866 cfg80211_process_wdev_events(wdev);
Johannes Berg3d54d252009-08-21 14:51:05 +0200867}
868
869int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
870 struct net_device *dev, enum nl80211_iftype ntype,
Johannes Berg818a9862017-04-12 11:23:28 +0200871 struct vif_params *params)
Johannes Berg3d54d252009-08-21 14:51:05 +0200872{
873 int err;
874 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
875
Zhao, Gang73fb08e2014-03-19 17:04:36 +0800876 ASSERT_RTNL();
Johannes Berg3d54d252009-08-21 14:51:05 +0200877
878 /* don't support changing VLANs, you just re-create them */
879 if (otype == NL80211_IFTYPE_AP_VLAN)
880 return -EOPNOTSUPP;
881
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300882 /* cannot change into P2P device or NAN */
883 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
884 ntype == NL80211_IFTYPE_NAN)
Johannes Berg98104fde2012-06-16 00:19:54 +0200885 return -EOPNOTSUPP;
886
Johannes Berg3d54d252009-08-21 14:51:05 +0200887 if (!rdev->ops->change_virtual_intf ||
888 !(rdev->wiphy.interface_modes & (1 << ntype)))
889 return -EOPNOTSUPP;
890
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100891 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000892 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200893 (ntype == NL80211_IFTYPE_ADHOC ||
894 ntype == NL80211_IFTYPE_STATION ||
895 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100896 return -EBUSY;
897
Michal Kazior6cbfb1b2015-05-22 10:57:22 +0200898 if (ntype != otype) {
Johannes Berg9bc383d2009-11-19 11:55:19 +0100899 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100900 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg194ff522013-12-30 23:12:37 +0100901 wdev_lock(dev->ieee80211_ptr);
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800902 rdev_set_qos_map(rdev, dev, NULL);
Johannes Berg194ff522013-12-30 23:12:37 +0100903 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg9bc383d2009-11-19 11:55:19 +0100904
Johannes Berg3d54d252009-08-21 14:51:05 +0200905 switch (otype) {
Michal Kaziorac800142012-06-29 12:46:57 +0200906 case NL80211_IFTYPE_AP:
Ilan Peer7c8d5e02014-02-25 15:33:38 +0200907 cfg80211_stop_ap(rdev, dev, true);
Michal Kaziorac800142012-06-29 12:46:57 +0200908 break;
Johannes Berg3d54d252009-08-21 14:51:05 +0200909 case NL80211_IFTYPE_ADHOC:
910 cfg80211_leave_ibss(rdev, dev, false);
911 break;
912 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200913 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg83739b02013-05-15 17:44:01 +0200914 wdev_lock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +0200915 cfg80211_disconnect(rdev, dev,
916 WLAN_REASON_DEAUTH_LEAVING, true);
Johannes Berg83739b02013-05-15 17:44:01 +0200917 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +0200918 break;
919 case NL80211_IFTYPE_MESH_POINT:
920 /* mesh should be handled? */
921 break;
922 default:
923 break;
924 }
925
926 cfg80211_process_rdev_events(rdev);
927 }
928
Johannes Berg818a9862017-04-12 11:23:28 +0200929 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
Johannes Berg3d54d252009-08-21 14:51:05 +0200930
931 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
932
Johannes Berg9bc383d2009-11-19 11:55:19 +0100933 if (!err && params && params->use_4addr != -1)
934 dev->ieee80211_ptr->use_4addr = params->use_4addr;
935
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100936 if (!err) {
937 dev->priv_flags &= ~IFF_DONT_BRIDGE;
938 switch (ntype) {
939 case NL80211_IFTYPE_STATION:
940 if (dev->ieee80211_ptr->use_4addr)
941 break;
942 /* fall through */
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100943 case NL80211_IFTYPE_OCB:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200944 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100945 case NL80211_IFTYPE_ADHOC:
946 dev->priv_flags |= IFF_DONT_BRIDGE;
947 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200948 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100949 case NL80211_IFTYPE_AP:
950 case NL80211_IFTYPE_AP_VLAN:
951 case NL80211_IFTYPE_WDS:
952 case NL80211_IFTYPE_MESH_POINT:
953 /* bridging OK */
954 break;
955 case NL80211_IFTYPE_MONITOR:
956 /* monitor can't bridge anyway */
957 break;
958 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f782010-08-12 15:38:38 +0200959 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100960 /* not happening */
961 break;
Johannes Berg98104fde2012-06-16 00:19:54 +0200962 case NL80211_IFTYPE_P2P_DEVICE:
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300963 case NL80211_IFTYPE_NAN:
Johannes Berg98104fde2012-06-16 00:19:54 +0200964 WARN_ON(1);
965 break;
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100966 }
967 }
968
Michal Kaziordbbae262012-06-29 12:47:01 +0200969 if (!err && ntype != otype && netif_running(dev)) {
970 cfg80211_update_iface_num(rdev, ntype, 1);
971 cfg80211_update_iface_num(rdev, otype, -1);
972 }
973
Johannes Berg3d54d252009-08-21 14:51:05 +0200974 return err;
975}
John W. Linville254416a2009-12-09 16:43:52 -0500976
Johannes Berg0c1eca42017-02-15 15:02:08 +0100977static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
978{
979 int modulation, streams, bitrate;
980
981 /* the formula below does only work for MCS values smaller than 32 */
982 if (WARN_ON_ONCE(rate->mcs >= 32))
983 return 0;
984
985 modulation = rate->mcs & 7;
986 streams = (rate->mcs >> 3) + 1;
987
988 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
989
990 if (modulation < 4)
991 bitrate *= (modulation + 1);
992 else if (modulation == 4)
993 bitrate *= (modulation + 2);
994 else
995 bitrate *= (modulation + 3);
996
997 bitrate *= streams;
998
999 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1000 bitrate = (bitrate / 9) * 10;
1001
1002 /* do NOT round down here */
1003 return (bitrate + 50000) / 100000;
1004}
1005
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +03001006static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
1007{
1008 static const u32 __mcs2bitrate[] = {
1009 /* control PHY */
1010 [0] = 275,
1011 /* SC PHY */
1012 [1] = 3850,
1013 [2] = 7700,
1014 [3] = 9625,
1015 [4] = 11550,
1016 [5] = 12512, /* 1251.25 mbps */
1017 [6] = 15400,
1018 [7] = 19250,
1019 [8] = 23100,
1020 [9] = 25025,
1021 [10] = 30800,
1022 [11] = 38500,
1023 [12] = 46200,
1024 /* OFDM PHY */
1025 [13] = 6930,
1026 [14] = 8662, /* 866.25 mbps */
1027 [15] = 13860,
1028 [16] = 17325,
1029 [17] = 20790,
1030 [18] = 27720,
1031 [19] = 34650,
1032 [20] = 41580,
1033 [21] = 45045,
1034 [22] = 51975,
1035 [23] = 62370,
1036 [24] = 67568, /* 6756.75 mbps */
1037 /* LP-SC PHY */
1038 [25] = 6260,
1039 [26] = 8340,
1040 [27] = 11120,
1041 [28] = 12510,
1042 [29] = 16680,
1043 [30] = 22240,
1044 [31] = 25030,
1045 };
1046
1047 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1048 return 0;
1049
1050 return __mcs2bitrate[rate->mcs];
1051}
1052
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001053static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1054{
1055 static const u32 base[4][10] = {
1056 { 6500000,
1057 13000000,
1058 19500000,
1059 26000000,
1060 39000000,
1061 52000000,
1062 58500000,
1063 65000000,
1064 78000000,
Pedersen, Thomas8fdd1362016-10-31 11:28:40 -07001065 /* not in the spec, but some devices use this: */
1066 86500000,
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001067 },
1068 { 13500000,
1069 27000000,
1070 40500000,
1071 54000000,
1072 81000000,
1073 108000000,
1074 121500000,
1075 135000000,
1076 162000000,
1077 180000000,
1078 },
1079 { 29300000,
1080 58500000,
1081 87800000,
1082 117000000,
1083 175500000,
1084 234000000,
1085 263300000,
1086 292500000,
1087 351000000,
1088 390000000,
1089 },
1090 { 58500000,
1091 117000000,
1092 175500000,
1093 234000000,
1094 351000000,
1095 468000000,
1096 526500000,
1097 585000000,
1098 702000000,
1099 780000000,
1100 },
1101 };
1102 u32 bitrate;
1103 int idx;
1104
Johannes Bergca8fe252017-05-04 07:52:10 +02001105 if (rate->mcs > 9)
1106 goto warn;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001107
Johannes Bergb51f3be2015-01-15 16:14:02 +01001108 switch (rate->bw) {
1109 case RATE_INFO_BW_160:
1110 idx = 3;
1111 break;
1112 case RATE_INFO_BW_80:
1113 idx = 2;
1114 break;
1115 case RATE_INFO_BW_40:
1116 idx = 1;
1117 break;
1118 case RATE_INFO_BW_5:
1119 case RATE_INFO_BW_10:
1120 default:
Johannes Bergca8fe252017-05-04 07:52:10 +02001121 goto warn;
Johannes Bergb51f3be2015-01-15 16:14:02 +01001122 case RATE_INFO_BW_20:
1123 idx = 0;
1124 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001125
1126 bitrate = base[idx][rate->mcs];
1127 bitrate *= rate->nss;
1128
1129 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1130 bitrate = (bitrate / 9) * 10;
1131
1132 /* do NOT round down here */
1133 return (bitrate + 50000) / 100000;
Johannes Bergca8fe252017-05-04 07:52:10 +02001134 warn:
1135 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1136 rate->bw, rate->mcs, rate->nss);
1137 return 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001138}
1139
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03001140u32 cfg80211_calculate_bitrate(struct rate_info *rate)
John W. Linville254416a2009-12-09 16:43:52 -05001141{
Johannes Berg0c1eca42017-02-15 15:02:08 +01001142 if (rate->flags & RATE_INFO_FLAGS_MCS)
1143 return cfg80211_calculate_bitrate_ht(rate);
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +03001144 if (rate->flags & RATE_INFO_FLAGS_60G)
1145 return cfg80211_calculate_bitrate_60g(rate);
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001146 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1147 return cfg80211_calculate_bitrate_vht(rate);
John W. Linville254416a2009-12-09 16:43:52 -05001148
Johannes Berg0c1eca42017-02-15 15:02:08 +01001149 return rate->legacy;
John W. Linville254416a2009-12-09 16:43:52 -05001150}
Thomas Pedersen8097e142012-03-05 15:31:47 -08001151EXPORT_SYMBOL(cfg80211_calculate_bitrate);
Johannes Berg56d18932011-05-09 18:41:15 +02001152
Arend van Sprielc216e642012-11-25 19:13:28 +01001153int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1154 enum ieee80211_p2p_attr_id attr,
1155 u8 *buf, unsigned int bufsize)
Johannes Berg0ee45352012-10-29 19:48:40 +01001156{
1157 u8 *out = buf;
1158 u16 attr_remaining = 0;
1159 bool desired_attr = false;
1160 u16 desired_len = 0;
1161
1162 while (len > 0) {
1163 unsigned int iedatalen;
1164 unsigned int copy;
1165 const u8 *iedata;
1166
1167 if (len < 2)
1168 return -EILSEQ;
1169 iedatalen = ies[1];
1170 if (iedatalen + 2 > len)
1171 return -EILSEQ;
1172
1173 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1174 goto cont;
1175
1176 if (iedatalen < 4)
1177 goto cont;
1178
1179 iedata = ies + 2;
1180
1181 /* check WFA OUI, P2P subtype */
1182 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1183 iedata[2] != 0x9a || iedata[3] != 0x09)
1184 goto cont;
1185
1186 iedatalen -= 4;
1187 iedata += 4;
1188
1189 /* check attribute continuation into this IE */
1190 copy = min_t(unsigned int, attr_remaining, iedatalen);
1191 if (copy && desired_attr) {
1192 desired_len += copy;
1193 if (out) {
1194 memcpy(out, iedata, min(bufsize, copy));
1195 out += min(bufsize, copy);
1196 bufsize -= min(bufsize, copy);
1197 }
1198
1199
1200 if (copy == attr_remaining)
1201 return desired_len;
1202 }
1203
1204 attr_remaining -= copy;
1205 if (attr_remaining)
1206 goto cont;
1207
1208 iedatalen -= copy;
1209 iedata += copy;
1210
1211 while (iedatalen > 0) {
1212 u16 attr_len;
1213
1214 /* P2P attribute ID & size must fit */
1215 if (iedatalen < 3)
1216 return -EILSEQ;
1217 desired_attr = iedata[0] == attr;
1218 attr_len = get_unaligned_le16(iedata + 1);
1219 iedatalen -= 3;
1220 iedata += 3;
1221
1222 copy = min_t(unsigned int, attr_len, iedatalen);
1223
1224 if (desired_attr) {
1225 desired_len += copy;
1226 if (out) {
1227 memcpy(out, iedata, min(bufsize, copy));
1228 out += min(bufsize, copy);
1229 bufsize -= min(bufsize, copy);
1230 }
1231
1232 if (copy == attr_len)
1233 return desired_len;
1234 }
1235
1236 iedata += copy;
1237 iedatalen -= copy;
1238 attr_remaining = attr_len - copy;
1239 }
1240
1241 cont:
1242 len -= ies[1] + 2;
1243 ies += ies[1] + 2;
1244 }
1245
1246 if (attr_remaining && desired_attr)
1247 return -EILSEQ;
1248
1249 return -ENOENT;
1250}
1251EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1252
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001253static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
Johannes Berg29464cc2015-03-31 15:36:22 +02001254{
1255 int i;
1256
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001257 /* Make sure array values are legal */
1258 if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
1259 return false;
1260
1261 i = 0;
1262 while (i < n_ids) {
1263 if (ids[i] == WLAN_EID_EXTENSION) {
1264 if (id_ext && (ids[i + 1] == id))
1265 return true;
1266
1267 i += 2;
1268 continue;
1269 }
1270
1271 if (ids[i] == id && !id_ext)
Johannes Berg29464cc2015-03-31 15:36:22 +02001272 return true;
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001273
1274 i++;
1275 }
Johannes Berg29464cc2015-03-31 15:36:22 +02001276 return false;
1277}
1278
Johannes Berg8ac63442015-09-04 20:03:23 +02001279static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1280{
1281 /* we assume a validly formed IEs buffer */
1282 u8 len = ies[pos + 1];
1283
1284 pos += 2 + len;
1285
1286 /* the IE itself must have 255 bytes for fragments to follow */
1287 if (len < 255)
1288 return pos;
1289
1290 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1291 len = ies[pos + 1];
1292 pos += 2 + len;
1293 }
1294
1295 return pos;
1296}
1297
Johannes Berg29464cc2015-03-31 15:36:22 +02001298size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1299 const u8 *ids, int n_ids,
1300 const u8 *after_ric, int n_after_ric,
1301 size_t offset)
1302{
1303 size_t pos = offset;
1304
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001305 while (pos < ielen) {
1306 u8 ext = 0;
1307
1308 if (ies[pos] == WLAN_EID_EXTENSION)
1309 ext = 2;
1310 if ((pos + ext) >= ielen)
1311 break;
1312
1313 if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
1314 ies[pos] == WLAN_EID_EXTENSION))
1315 break;
1316
Johannes Berg29464cc2015-03-31 15:36:22 +02001317 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
Johannes Berg8ac63442015-09-04 20:03:23 +02001318 pos = skip_ie(ies, ielen, pos);
Johannes Berg29464cc2015-03-31 15:36:22 +02001319
Liad Kaufman2512b1b2017-08-05 11:44:31 +03001320 while (pos < ielen) {
1321 if (ies[pos] == WLAN_EID_EXTENSION)
1322 ext = 2;
1323 else
1324 ext = 0;
1325
1326 if ((pos + ext) >= ielen)
1327 break;
1328
1329 if (!ieee80211_id_in_list(after_ric,
1330 n_after_ric,
1331 ies[pos + ext],
1332 ext == 2))
1333 pos = skip_ie(ies, ielen, pos);
1334 }
Johannes Berg29464cc2015-03-31 15:36:22 +02001335 } else {
Johannes Berg8ac63442015-09-04 20:03:23 +02001336 pos = skip_ie(ies, ielen, pos);
Johannes Berg29464cc2015-03-31 15:36:22 +02001337 }
1338 }
1339
1340 return pos;
1341}
1342EXPORT_SYMBOL(ieee80211_ie_split_ric);
1343
Johannes Berg1ce3e822012-08-01 17:00:55 +02001344bool ieee80211_operating_class_to_band(u8 operating_class,
Johannes Berg57fbcce2016-04-12 15:56:15 +02001345 enum nl80211_band *band)
Johannes Berg1ce3e822012-08-01 17:00:55 +02001346{
1347 switch (operating_class) {
1348 case 112:
1349 case 115 ... 127:
Eliad Peller954a86e2015-03-01 09:10:01 +02001350 case 128 ... 130:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001351 *band = NL80211_BAND_5GHZ;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001352 return true;
1353 case 81:
1354 case 82:
1355 case 83:
1356 case 84:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001357 *band = NL80211_BAND_2GHZ;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001358 return true;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001359 case 180:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001360 *band = NL80211_BAND_60GHZ;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001361 return true;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001362 }
1363
1364 return false;
1365}
1366EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1367
Arik Nemtsova38700d2015-03-18 08:46:08 +02001368bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1369 u8 *op_class)
1370{
1371 u8 vht_opclass;
1372 u16 freq = chandef->center_freq1;
1373
1374 if (freq >= 2412 && freq <= 2472) {
1375 if (chandef->width > NL80211_CHAN_WIDTH_40)
1376 return false;
1377
1378 /* 2.407 GHz, channels 1..13 */
1379 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1380 if (freq > chandef->chan->center_freq)
1381 *op_class = 83; /* HT40+ */
1382 else
1383 *op_class = 84; /* HT40- */
1384 } else {
1385 *op_class = 81;
1386 }
1387
1388 return true;
1389 }
1390
1391 if (freq == 2484) {
1392 if (chandef->width > NL80211_CHAN_WIDTH_40)
1393 return false;
1394
1395 *op_class = 82; /* channel 14 */
1396 return true;
1397 }
1398
1399 switch (chandef->width) {
1400 case NL80211_CHAN_WIDTH_80:
1401 vht_opclass = 128;
1402 break;
1403 case NL80211_CHAN_WIDTH_160:
1404 vht_opclass = 129;
1405 break;
1406 case NL80211_CHAN_WIDTH_80P80:
1407 vht_opclass = 130;
1408 break;
1409 case NL80211_CHAN_WIDTH_10:
1410 case NL80211_CHAN_WIDTH_5:
1411 return false; /* unsupported for now */
1412 default:
1413 vht_opclass = 0;
1414 break;
1415 }
1416
1417 /* 5 GHz, channels 36..48 */
1418 if (freq >= 5180 && freq <= 5240) {
1419 if (vht_opclass) {
1420 *op_class = vht_opclass;
1421 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1422 if (freq > chandef->chan->center_freq)
1423 *op_class = 116;
1424 else
1425 *op_class = 117;
1426 } else {
1427 *op_class = 115;
1428 }
1429
1430 return true;
1431 }
1432
1433 /* 5 GHz, channels 52..64 */
1434 if (freq >= 5260 && freq <= 5320) {
1435 if (vht_opclass) {
1436 *op_class = vht_opclass;
1437 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1438 if (freq > chandef->chan->center_freq)
1439 *op_class = 119;
1440 else
1441 *op_class = 120;
1442 } else {
1443 *op_class = 118;
1444 }
1445
1446 return true;
1447 }
1448
1449 /* 5 GHz, channels 100..144 */
1450 if (freq >= 5500 && freq <= 5720) {
1451 if (vht_opclass) {
1452 *op_class = vht_opclass;
1453 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1454 if (freq > chandef->chan->center_freq)
1455 *op_class = 122;
1456 else
1457 *op_class = 123;
1458 } else {
1459 *op_class = 121;
1460 }
1461
1462 return true;
1463 }
1464
1465 /* 5 GHz, channels 149..169 */
1466 if (freq >= 5745 && freq <= 5845) {
1467 if (vht_opclass) {
1468 *op_class = vht_opclass;
1469 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1470 if (freq > chandef->chan->center_freq)
1471 *op_class = 126;
1472 else
1473 *op_class = 127;
1474 } else if (freq <= 5805) {
1475 *op_class = 124;
1476 } else {
1477 *op_class = 125;
1478 }
1479
1480 return true;
1481 }
1482
1483 /* 56.16 GHz, channel 1..4 */
1484 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
1485 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1486 return false;
1487
1488 *op_class = 180;
1489 return true;
1490 }
1491
1492 /* not supported yet */
1493 return false;
1494}
1495EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1496
Johannes Berg4c8dea62016-10-21 14:25:13 +02001497static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1498 u32 *beacon_int_gcd,
1499 bool *beacon_int_different)
1500{
1501 struct wireless_dev *wdev;
1502
1503 *beacon_int_gcd = 0;
1504 *beacon_int_different = false;
1505
1506 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1507 if (!wdev->beacon_interval)
1508 continue;
1509
1510 if (!*beacon_int_gcd) {
1511 *beacon_int_gcd = wdev->beacon_interval;
1512 continue;
1513 }
1514
1515 if (wdev->beacon_interval == *beacon_int_gcd)
1516 continue;
1517
1518 *beacon_int_different = true;
1519 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1520 }
1521
1522 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1523 if (*beacon_int_gcd)
1524 *beacon_int_different = true;
1525 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1526 }
1527}
1528
Johannes Berg56d18932011-05-09 18:41:15 +02001529int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05301530 enum nl80211_iftype iftype, u32 beacon_int)
Johannes Berg56d18932011-05-09 18:41:15 +02001531{
Johannes Berg4c8dea62016-10-21 14:25:13 +02001532 /*
1533 * This is just a basic pre-condition check; if interface combinations
1534 * are possible the driver must already be checking those with a call
1535 * to cfg80211_check_combinations(), in which case we'll validate more
1536 * through the cfg80211_calculate_bi_data() call and code in
1537 * cfg80211_iter_combinations().
1538 */
Johannes Berg56d18932011-05-09 18:41:15 +02001539
Purushottam Kushwaha12d20fc92016-08-11 15:14:02 +05301540 if (beacon_int < 10 || beacon_int > 10000)
Johannes Berg56d18932011-05-09 18:41:15 +02001541 return -EINVAL;
1542
Johannes Berg4c8dea62016-10-21 14:25:13 +02001543 return 0;
Johannes Berg56d18932011-05-09 18:41:15 +02001544}
Johannes Berg7527a782011-05-13 10:58:57 +02001545
Michal Kazior65a124d2014-04-09 15:29:22 +02001546int cfg80211_iter_combinations(struct wiphy *wiphy,
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301547 struct iface_combination_params *params,
Michal Kazior65a124d2014-04-09 15:29:22 +02001548 void (*iter)(const struct ieee80211_iface_combination *c,
1549 void *data),
1550 void *data)
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001551{
Felix Fietkau8c48b502014-05-05 11:48:40 +02001552 const struct ieee80211_regdomain *regdom;
1553 enum nl80211_dfs_regions region = 0;
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001554 int i, j, iftype;
1555 int num_interfaces = 0;
1556 u32 used_iftypes = 0;
Johannes Berg4c8dea62016-10-21 14:25:13 +02001557 u32 beacon_int_gcd;
1558 bool beacon_int_different;
1559
1560 /*
1561 * This is a bit strange, since the iteration used to rely only on
1562 * the data given by the driver, but here it now relies on context,
1563 * in form of the currently operating interfaces.
1564 * This is OK for all current users, and saves us from having to
1565 * push the GCD calculations into all the drivers.
1566 * In the future, this should probably rely more on data that's in
1567 * cfg80211 already - the only thing not would appear to be any new
1568 * interfaces (while being brought up) and channel/radar data.
1569 */
1570 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1571 &beacon_int_gcd, &beacon_int_different);
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001572
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301573 if (params->radar_detect) {
Felix Fietkau8c48b502014-05-05 11:48:40 +02001574 rcu_read_lock();
1575 regdom = rcu_dereference(cfg80211_regdomain);
1576 if (regdom)
1577 region = regdom->dfs_region;
1578 rcu_read_unlock();
1579 }
1580
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001581 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301582 num_interfaces += params->iftype_num[iftype];
1583 if (params->iftype_num[iftype] > 0 &&
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001584 !(wiphy->software_iftypes & BIT(iftype)))
1585 used_iftypes |= BIT(iftype);
1586 }
1587
1588 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1589 const struct ieee80211_iface_combination *c;
1590 struct ieee80211_iface_limit *limits;
1591 u32 all_iftypes = 0;
1592
1593 c = &wiphy->iface_combinations[i];
1594
1595 if (num_interfaces > c->max_interfaces)
1596 continue;
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301597 if (params->num_different_channels > c->num_different_channels)
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001598 continue;
1599
1600 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1601 GFP_KERNEL);
1602 if (!limits)
1603 return -ENOMEM;
1604
1605 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1606 if (wiphy->software_iftypes & BIT(iftype))
1607 continue;
1608 for (j = 0; j < c->n_limits; j++) {
1609 all_iftypes |= limits[j].types;
1610 if (!(limits[j].types & BIT(iftype)))
1611 continue;
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301612 if (limits[j].max < params->iftype_num[iftype])
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001613 goto cont;
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301614 limits[j].max -= params->iftype_num[iftype];
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001615 }
1616 }
1617
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301618 if (params->radar_detect !=
1619 (c->radar_detect_widths & params->radar_detect))
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001620 goto cont;
1621
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301622 if (params->radar_detect && c->radar_detect_regions &&
Felix Fietkau8c48b502014-05-05 11:48:40 +02001623 !(c->radar_detect_regions & BIT(region)))
1624 goto cont;
1625
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001626 /* Finally check that all iftypes that we're currently
1627 * using are actually part of this combination. If they
1628 * aren't then we can't use this combination and have
1629 * to continue to the next.
1630 */
1631 if ((all_iftypes & used_iftypes) != used_iftypes)
1632 goto cont;
1633
Johannes Berg4c8dea62016-10-21 14:25:13 +02001634 if (beacon_int_gcd) {
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05301635 if (c->beacon_int_min_gcd &&
Johannes Berg4c8dea62016-10-21 14:25:13 +02001636 beacon_int_gcd < c->beacon_int_min_gcd)
Johannes Berg0507a3a2016-10-21 12:15:00 +02001637 goto cont;
Johannes Berg4c8dea62016-10-21 14:25:13 +02001638 if (!c->beacon_int_min_gcd && beacon_int_different)
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05301639 goto cont;
1640 }
1641
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001642 /* This combination covered all interface types and
1643 * supported the requested numbers, so we're good.
1644 */
Michal Kazior65a124d2014-04-09 15:29:22 +02001645
1646 (*iter)(c, data);
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001647 cont:
1648 kfree(limits);
1649 }
1650
Michal Kazior65a124d2014-04-09 15:29:22 +02001651 return 0;
1652}
1653EXPORT_SYMBOL(cfg80211_iter_combinations);
1654
1655static void
1656cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1657 void *data)
1658{
1659 int *num = data;
1660 (*num)++;
1661}
1662
1663int cfg80211_check_combinations(struct wiphy *wiphy,
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301664 struct iface_combination_params *params)
Michal Kazior65a124d2014-04-09 15:29:22 +02001665{
1666 int err, num = 0;
1667
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301668 err = cfg80211_iter_combinations(wiphy, params,
Michal Kazior65a124d2014-04-09 15:29:22 +02001669 cfg80211_iter_sum_ifcombs, &num);
1670 if (err)
1671 return err;
1672 if (num == 0)
1673 return -EBUSY;
1674
1675 return 0;
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001676}
1677EXPORT_SYMBOL(cfg80211_check_combinations);
1678
Johannes Berg34850ab2011-07-18 18:08:35 +02001679int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1680 const u8 *rates, unsigned int n_rates,
1681 u32 *mask)
1682{
1683 int i, j;
1684
Johannes Berga401d2b2011-07-20 00:52:16 +02001685 if (!sband)
1686 return -EINVAL;
1687
Johannes Berg34850ab2011-07-18 18:08:35 +02001688 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1689 return -EINVAL;
1690
1691 *mask = 0;
1692
1693 for (i = 0; i < n_rates; i++) {
1694 int rate = (rates[i] & 0x7f) * 5;
1695 bool found = false;
1696
1697 for (j = 0; j < sband->n_bitrates; j++) {
1698 if (sband->bitrates[j].bitrate == rate) {
1699 found = true;
1700 *mask |= BIT(j);
1701 break;
1702 }
1703 }
1704 if (!found)
1705 return -EINVAL;
1706 }
1707
1708 /*
1709 * mask must have at least one bit set here since we
1710 * didn't accept a 0-length rates array nor allowed
1711 * entries in the array that didn't exist
1712 */
1713
1714 return 0;
1715}
Johannes Berg11a2a352011-11-21 11:09:22 +01001716
Ilan Peerbdfbec22014-01-09 11:37:23 +02001717unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1718{
Johannes Berg57fbcce2016-04-12 15:56:15 +02001719 enum nl80211_band band;
Ilan Peerbdfbec22014-01-09 11:37:23 +02001720 unsigned int n_channels = 0;
1721
Johannes Berg57fbcce2016-04-12 15:56:15 +02001722 for (band = 0; band < NUM_NL80211_BANDS; band++)
Ilan Peerbdfbec22014-01-09 11:37:23 +02001723 if (wiphy->bands[band])
1724 n_channels += wiphy->bands[band]->n_channels;
1725
1726 return n_channels;
1727}
1728EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1729
Antonio Quartulli74063532014-05-19 21:53:21 +02001730int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1731 struct station_info *sinfo)
1732{
1733 struct cfg80211_registered_device *rdev;
1734 struct wireless_dev *wdev;
1735
1736 wdev = dev->ieee80211_ptr;
1737 if (!wdev)
1738 return -EOPNOTSUPP;
1739
1740 rdev = wiphy_to_rdev(wdev->wiphy);
1741 if (!rdev->ops->get_station)
1742 return -EOPNOTSUPP;
1743
1744 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1745}
1746EXPORT_SYMBOL(cfg80211_get_station);
1747
Ayala Bekera442b762016-09-20 17:31:15 +03001748void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1749{
1750 int i;
1751
1752 if (!f)
1753 return;
1754
1755 kfree(f->serv_spec_info);
1756 kfree(f->srf_bf);
1757 kfree(f->srf_macs);
1758 for (i = 0; i < f->num_rx_filters; i++)
1759 kfree(f->rx_filters[i].filter);
1760
1761 for (i = 0; i < f->num_tx_filters; i++)
1762 kfree(f->tx_filters[i].filter);
1763
1764 kfree(f->rx_filters);
1765 kfree(f->tx_filters);
1766 kfree(f);
1767}
1768EXPORT_SYMBOL(cfg80211_free_nan_func);
1769
Rafał Miłecki4787cfa2017-01-04 18:58:30 +01001770bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1771 u32 center_freq_khz, u32 bw_khz)
1772{
1773 u32 start_freq_khz, end_freq_khz;
1774
1775 start_freq_khz = center_freq_khz - (bw_khz / 2);
1776 end_freq_khz = center_freq_khz + (bw_khz / 2);
1777
1778 if (start_freq_khz >= freq_range->start_freq_khz &&
1779 end_freq_khz <= freq_range->end_freq_khz)
1780 return true;
1781
1782 return false;
1783}
1784
Johannes Berg11a2a352011-11-21 11:09:22 +01001785/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1786/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1787const unsigned char rfc1042_header[] __aligned(2) =
1788 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1789EXPORT_SYMBOL(rfc1042_header);
1790
1791/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1792const unsigned char bridge_tunnel_header[] __aligned(2) =
1793 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1794EXPORT_SYMBOL(bridge_tunnel_header);