blob: c1238d582fd190c4ecb87b3b005d6527ca597dd9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Johannes Berg8318d782008-01-24 19:38:38 +01002/*
3 * Wireless utility functions
4 *
Johannes Bergd3236552009-04-20 14:31:42 +02005 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg2740f0c2014-09-03 15:24:58 +03006 * Copyright 2013-2014 Intel Mobile Communications GmbH
Johannes Berg8318d782008-01-24 19:38:38 +01007 */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -04008#include <linux/export.h>
Johannes Bergd3236552009-04-20 14:31:42 +02009#include <linux/bitops.h>
Zhu Yie31a16d2009-05-21 21:47:03 +080010#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Johannes Bergd3236552009-04-20 14:31:42 +020012#include <net/cfg80211.h>
Zhu Yie31a16d2009-05-21 21:47:03 +080013#include <net/ip.h>
Dave Tähtb1565792011-12-22 12:55:08 -080014#include <net/dsfield.h>
cedric Vonckenc6ca5e22013-08-26 14:04:52 +020015#include <linux/if_vlan.h>
Simon Wunderlich960d97f2014-03-03 17:23:12 +010016#include <linux/mpls.h>
Johannes Berg4c8dea62016-10-21 14:25:13 +020017#include <linux/gcd.h>
Johannes Berg8318d782008-01-24 19:38:38 +010018#include "core.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030019#include "rdev-ops.h"
20
Johannes Berg8318d782008-01-24 19:38:38 +010021
Johannes Bergbd815252008-10-29 20:00:45 +010022struct ieee80211_rate *
23ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
Johannes Berg881d9482009-01-21 15:13:48 +010024 u32 basic_rates, int bitrate)
Johannes Bergbd815252008-10-29 20:00:45 +010025{
26 struct ieee80211_rate *result = &sband->bitrates[0];
27 int i;
28
29 for (i = 0; i < sband->n_bitrates; i++) {
30 if (!(basic_rates & BIT(i)))
31 continue;
32 if (sband->bitrates[i].bitrate > bitrate)
33 continue;
34 result = &sband->bitrates[i];
35 }
36
37 return result;
38}
39EXPORT_SYMBOL(ieee80211_get_response_rate);
40
Simon Wunderlich74608ac2013-07-08 16:55:54 +020041u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
42 enum nl80211_bss_scan_width scan_width)
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070043{
44 struct ieee80211_rate *bitrates;
45 u32 mandatory_rates = 0;
46 enum ieee80211_rate_flags mandatory_flag;
47 int i;
48
49 if (WARN_ON(!sband))
50 return 1;
51
Johannes Berg57fbcce2016-04-12 15:56:15 +020052 if (sband->band == NL80211_BAND_2GHZ) {
Simon Wunderlich74608ac2013-07-08 16:55:54 +020053 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
54 scan_width == NL80211_BSS_CHAN_WIDTH_10)
55 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
56 else
57 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
58 } else {
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070059 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
Simon Wunderlich74608ac2013-07-08 16:55:54 +020060 }
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070061
62 bitrates = sband->bitrates;
63 for (i = 0; i < sband->n_bitrates; i++)
64 if (bitrates[i].flags & mandatory_flag)
65 mandatory_rates |= BIT(i);
66 return mandatory_rates;
67}
68EXPORT_SYMBOL(ieee80211_mandatory_rates);
69
Johannes Berg57fbcce2016-04-12 15:56:15 +020070int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +010071{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090072 /* see 802.11 17.3.8.3.2 and Annex J
73 * there are overlapping channel numbers in 5GHz and 2GHz bands */
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030074 if (chan <= 0)
75 return 0; /* not supported */
76 switch (band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +020077 case NL80211_BAND_2GHZ:
Bruno Randolf59eb21a2011-01-17 13:37:28 +090078 if (chan == 14)
79 return 2484;
80 else if (chan < 14)
81 return 2407 + chan * 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030082 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +020083 case NL80211_BAND_5GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030084 if (chan >= 182 && chan <= 196)
85 return 4000 + chan * 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090086 else
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030087 return 5000 + chan * 5;
88 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +020089 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030090 if (chan < 5)
91 return 56160 + chan * 2160;
92 break;
93 default:
94 ;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090095 }
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030096 return 0; /* not supported */
Johannes Berg8318d782008-01-24 19:38:38 +010097}
98EXPORT_SYMBOL(ieee80211_channel_to_frequency);
99
100int ieee80211_frequency_to_channel(int freq)
101{
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900102 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +0100103 if (freq == 2484)
104 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900105 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +0100106 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900107 else if (freq >= 4910 && freq <= 4980)
108 return (freq - 4000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300109 else if (freq <= 45000) /* DMG band lower limit */
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900110 return (freq - 5000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300111 else if (freq >= 58320 && freq <= 64800)
112 return (freq - 56160) / 2160;
113 else
114 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100115}
116EXPORT_SYMBOL(ieee80211_frequency_to_channel);
117
Arend Van Spriel543b9212016-11-17 12:48:53 +0000118struct ieee80211_channel *ieee80211_get_channel(struct wiphy *wiphy, int freq)
Johannes Berg906c7302008-03-16 18:34:33 +0100119{
Johannes Berg57fbcce2016-04-12 15:56:15 +0200120 enum nl80211_band band;
Johannes Berg906c7302008-03-16 18:34:33 +0100121 struct ieee80211_supported_band *sband;
122 int i;
123
Johannes Berg57fbcce2016-04-12 15:56:15 +0200124 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Berg906c7302008-03-16 18:34:33 +0100125 sband = wiphy->bands[band];
126
127 if (!sband)
128 continue;
129
130 for (i = 0; i < sband->n_channels; i++) {
131 if (sband->channels[i].center_freq == freq)
132 return &sband->channels[i];
133 }
134 }
135
136 return NULL;
137}
Arend Van Spriel543b9212016-11-17 12:48:53 +0000138EXPORT_SYMBOL(ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +0100139
Arend Van Spriel343884c2017-01-04 10:53:37 +0000140static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +0100141{
142 int i, want;
143
Arend Van Spriel343884c2017-01-04 10:53:37 +0000144 switch (sband->band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200145 case NL80211_BAND_5GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100146 want = 3;
147 for (i = 0; i < sband->n_bitrates; i++) {
148 if (sband->bitrates[i].bitrate == 60 ||
149 sband->bitrates[i].bitrate == 120 ||
150 sband->bitrates[i].bitrate == 240) {
151 sband->bitrates[i].flags |=
152 IEEE80211_RATE_MANDATORY_A;
153 want--;
154 }
155 }
156 WARN_ON(want);
157 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200158 case NL80211_BAND_2GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100159 want = 7;
160 for (i = 0; i < sband->n_bitrates; i++) {
161 if (sband->bitrates[i].bitrate == 10) {
162 sband->bitrates[i].flags |=
163 IEEE80211_RATE_MANDATORY_B |
164 IEEE80211_RATE_MANDATORY_G;
165 want--;
166 }
167
168 if (sband->bitrates[i].bitrate == 20 ||
169 sband->bitrates[i].bitrate == 55 ||
170 sband->bitrates[i].bitrate == 110 ||
171 sband->bitrates[i].bitrate == 60 ||
172 sband->bitrates[i].bitrate == 120 ||
173 sband->bitrates[i].bitrate == 240) {
174 sband->bitrates[i].flags |=
175 IEEE80211_RATE_MANDATORY_G;
176 want--;
177 }
178
Johannes Bergaac09fb2008-01-30 17:36:10 +0100179 if (sband->bitrates[i].bitrate != 10 &&
180 sband->bitrates[i].bitrate != 20 &&
181 sband->bitrates[i].bitrate != 55 &&
182 sband->bitrates[i].bitrate != 110)
Johannes Berg8318d782008-01-24 19:38:38 +0100183 sband->bitrates[i].flags |=
184 IEEE80211_RATE_ERP_G;
185 }
Ivo van Doorn406f2382008-02-02 23:53:10 +0100186 WARN_ON(want != 0 && want != 3 && want != 6);
Johannes Berg8318d782008-01-24 19:38:38 +0100187 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200188 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300189 /* check for mandatory HT MCS 1..4 */
190 WARN_ON(!sband->ht_cap.ht_supported);
191 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
192 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200193 case NUM_NL80211_BANDS:
Arend Van Spriel343884c2017-01-04 10:53:37 +0000194 default:
Johannes Berg8318d782008-01-24 19:38:38 +0100195 WARN_ON(1);
196 break;
197 }
198}
199
200void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
201{
Johannes Berg57fbcce2016-04-12 15:56:15 +0200202 enum nl80211_band band;
Johannes Berg8318d782008-01-24 19:38:38 +0100203
Johannes Berg57fbcce2016-04-12 15:56:15 +0200204 for (band = 0; band < NUM_NL80211_BANDS; band++)
Johannes Berg8318d782008-01-24 19:38:38 +0100205 if (wiphy->bands[band])
Arend Van Spriel343884c2017-01-04 10:53:37 +0000206 set_mandatory_flags_band(wiphy->bands[band]);
Johannes Berg8318d782008-01-24 19:38:38 +0100207}
Johannes Berg08645122009-05-11 13:54:58 +0200208
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300209bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
210{
211 int i;
212 for (i = 0; i < wiphy->n_cipher_suites; i++)
213 if (cipher == wiphy->cipher_suites[i])
214 return true;
215 return false;
216}
217
Johannes Bergfffd0932009-07-08 14:22:54 +0200218int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
219 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200220 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200221{
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200222 if (key_idx < 0 || key_idx > 5)
Johannes Berg08645122009-05-11 13:54:58 +0200223 return -EINVAL;
224
Johannes Berge31b8212010-10-05 19:39:30 +0200225 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
226 return -EINVAL;
227
228 if (pairwise && !mac_addr)
229 return -EINVAL;
230
Jouni Malinen37720562015-01-24 19:52:04 +0200231 switch (params->cipher) {
232 case WLAN_CIPHER_SUITE_TKIP:
233 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200234 case WLAN_CIPHER_SUITE_CCMP_256:
235 case WLAN_CIPHER_SUITE_GCMP:
236 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinen37720562015-01-24 19:52:04 +0200237 /* Disallow pairwise keys with non-zero index unless it's WEP
238 * or a vendor specific cipher (because current deployments use
239 * pairwise WEP keys with non-zero indices and for vendor
240 * specific ciphers this should be validated in the driver or
241 * hardware level - but 802.11i clearly specifies to use zero)
242 */
243 if (pairwise && key_idx)
244 return -EINVAL;
245 break;
246 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200247 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
248 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
249 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen37720562015-01-24 19:52:04 +0200250 /* Disallow BIP (group-only) cipher as pairwise cipher */
251 if (pairwise)
252 return -EINVAL;
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200253 if (key_idx < 4)
254 return -EINVAL;
Jouni Malinen37720562015-01-24 19:52:04 +0200255 break;
Johannes Berge9c8f8d2016-09-13 16:37:40 +0200256 case WLAN_CIPHER_SUITE_WEP40:
257 case WLAN_CIPHER_SUITE_WEP104:
258 if (key_idx > 3)
259 return -EINVAL;
Jouni Malinen37720562015-01-24 19:52:04 +0200260 default:
261 break;
262 }
Johannes Berg08645122009-05-11 13:54:58 +0200263
Johannes Berg08645122009-05-11 13:54:58 +0200264 switch (params->cipher) {
265 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200266 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200267 return -EINVAL;
268 break;
269 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200270 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200271 return -EINVAL;
272 break;
273 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200274 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200275 return -EINVAL;
276 break;
Jouni Malinencfcf1682015-01-24 19:52:05 +0200277 case WLAN_CIPHER_SUITE_CCMP_256:
278 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
279 return -EINVAL;
280 break;
281 case WLAN_CIPHER_SUITE_GCMP:
282 if (params->key_len != WLAN_KEY_LEN_GCMP)
283 return -EINVAL;
284 break;
285 case WLAN_CIPHER_SUITE_GCMP_256:
286 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
287 return -EINVAL;
288 break;
Johannes Berg08645122009-05-11 13:54:58 +0200289 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200290 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200291 return -EINVAL;
292 break;
293 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200294 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200295 return -EINVAL;
296 break;
Jouni Malinencfcf1682015-01-24 19:52:05 +0200297 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
298 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
299 return -EINVAL;
300 break;
301 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
302 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
303 return -EINVAL;
304 break;
305 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
306 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
307 return -EINVAL;
308 break;
Johannes Berg08645122009-05-11 13:54:58 +0200309 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300310 /*
311 * We don't know anything about this algorithm,
312 * allow using it -- but the driver must check
313 * all parameters! We still check below whether
314 * or not the driver supports this algorithm,
315 * of course.
316 */
317 break;
Johannes Berg08645122009-05-11 13:54:58 +0200318 }
319
Jouni Malinen9f26a952009-05-15 12:38:32 +0300320 if (params->seq) {
321 switch (params->cipher) {
322 case WLAN_CIPHER_SUITE_WEP40:
323 case WLAN_CIPHER_SUITE_WEP104:
324 /* These ciphers do not use key sequence */
325 return -EINVAL;
326 case WLAN_CIPHER_SUITE_TKIP:
327 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200328 case WLAN_CIPHER_SUITE_CCMP_256:
329 case WLAN_CIPHER_SUITE_GCMP:
330 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinen9f26a952009-05-15 12:38:32 +0300331 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinencfcf1682015-01-24 19:52:05 +0200332 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
333 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
334 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen9f26a952009-05-15 12:38:32 +0300335 if (params->seq_len != 6)
336 return -EINVAL;
337 break;
338 }
339 }
340
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300341 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200342 return -EINVAL;
343
Johannes Berg08645122009-05-11 13:54:58 +0200344 return 0;
345}
Zhu Yie31a16d2009-05-21 21:47:03 +0800346
Johannes Berg633adf12010-08-12 14:49:58 +0200347unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800348{
349 unsigned int hdrlen = 24;
350
351 if (ieee80211_is_data(fc)) {
352 if (ieee80211_has_a4(fc))
353 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200354 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800355 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200356 if (ieee80211_has_order(fc))
357 hdrlen += IEEE80211_HT_CTL_LEN;
358 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800359 goto out;
360 }
361
Fred Choufb142f42015-01-20 10:17:27 +0800362 if (ieee80211_is_mgmt(fc)) {
363 if (ieee80211_has_order(fc))
364 hdrlen += IEEE80211_HT_CTL_LEN;
365 goto out;
366 }
367
Zhu Yie31a16d2009-05-21 21:47:03 +0800368 if (ieee80211_is_ctl(fc)) {
369 /*
370 * ACK and CTS are 10 bytes, all others 16. To see how
371 * to get this condition consider
372 * subtype mask: 0b0000000011110000 (0x00F0)
373 * ACK subtype: 0b0000000011010000 (0x00D0)
374 * CTS subtype: 0b0000000011000000 (0x00C0)
375 * bits that matter: ^^^ (0x00E0)
376 * value of those: 0b0000000011000000 (0x00C0)
377 */
378 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
379 hdrlen = 10;
380 else
381 hdrlen = 16;
382 }
383out:
384 return hdrlen;
385}
386EXPORT_SYMBOL(ieee80211_hdrlen);
387
388unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
389{
390 const struct ieee80211_hdr *hdr =
391 (const struct ieee80211_hdr *)skb->data;
392 unsigned int hdrlen;
393
394 if (unlikely(skb->len < 10))
395 return 0;
396 hdrlen = ieee80211_hdrlen(hdr->frame_control);
397 if (unlikely(hdrlen > skb->len))
398 return 0;
399 return hdrlen;
400}
401EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
402
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100403static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
Zhu Yie31a16d2009-05-21 21:47:03 +0800404{
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100405 int ae = flags & MESH_FLAGS_AE;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200406 /* 802.11-2012, 8.2.4.7.3 */
Zhu Yie31a16d2009-05-21 21:47:03 +0800407 switch (ae) {
Johannes Berg7dd111e2012-10-25 21:51:59 +0200408 default:
Zhu Yie31a16d2009-05-21 21:47:03 +0800409 case 0:
410 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700411 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800412 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700413 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800414 return 18;
Zhu Yie31a16d2009-05-21 21:47:03 +0800415 }
416}
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100417
418unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
419{
420 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
421}
Johannes Berg9b395bc2012-10-26 00:36:40 +0200422EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800423
Johannes Berg7f6990c2016-10-05 15:29:49 +0200424int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
425 const u8 *addr, enum nl80211_iftype iftype)
Zhu Yie31a16d2009-05-21 21:47:03 +0800426{
427 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100428 struct {
429 u8 hdr[ETH_ALEN] __aligned(2);
430 __be16 proto;
431 } payload;
432 struct ethhdr tmp;
433 u16 hdrlen;
434 u8 mesh_flags = 0;
Zhu Yie31a16d2009-05-21 21:47:03 +0800435
436 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
437 return -1;
438
439 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100440 if (skb->len < hdrlen + 8)
441 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800442
443 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
444 * header
445 * IEEE 802.11 address fields:
446 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
447 * 0 0 DA SA BSSID n/a
448 * 0 1 DA BSSID SA n/a
449 * 1 0 BSSID SA DA n/a
450 * 1 1 RA TA DA SA
451 */
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100452 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
453 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
454
455 if (iftype == NL80211_IFTYPE_MESH_POINT)
456 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
Zhu Yie31a16d2009-05-21 21:47:03 +0800457
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700458 mesh_flags &= MESH_FLAGS_AE;
459
Zhu Yie31a16d2009-05-21 21:47:03 +0800460 switch (hdr->frame_control &
461 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
462 case cpu_to_le16(IEEE80211_FCTL_TODS):
463 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200464 iftype != NL80211_IFTYPE_AP_VLAN &&
465 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800466 return -1;
467 break;
468 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
469 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543ee2009-11-10 20:10:05 +0100470 iftype != NL80211_IFTYPE_MESH_POINT &&
471 iftype != NL80211_IFTYPE_AP_VLAN &&
472 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800473 return -1;
474 if (iftype == NL80211_IFTYPE_MESH_POINT) {
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700475 if (mesh_flags == MESH_FLAGS_AE_A4)
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800476 return -1;
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700477 if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800478 skb_copy_bits(skb, hdrlen +
479 offsetof(struct ieee80211s_hdr, eaddr1),
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100480 tmp.h_dest, 2 * ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800481 }
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100482 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
Zhu Yie31a16d2009-05-21 21:47:03 +0800483 }
484 break;
485 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700486 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200487 iftype != NL80211_IFTYPE_P2P_CLIENT &&
488 iftype != NL80211_IFTYPE_MESH_POINT) ||
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100489 (is_multicast_ether_addr(tmp.h_dest) &&
490 ether_addr_equal(tmp.h_source, addr)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800491 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700492 if (iftype == NL80211_IFTYPE_MESH_POINT) {
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700493 if (mesh_flags == MESH_FLAGS_AE_A5_A6)
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800494 return -1;
Rajkumar Manoharan5667c862017-05-14 21:41:55 -0700495 if (mesh_flags == MESH_FLAGS_AE_A4)
Zhu Yie3cf8b3f2010-03-29 17:35:07 +0800496 skb_copy_bits(skb, hdrlen +
497 offsetof(struct ieee80211s_hdr, eaddr1),
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100498 tmp.h_source, ETH_ALEN);
499 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700500 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800501 break;
502 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300503 if (iftype != NL80211_IFTYPE_ADHOC &&
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100504 iftype != NL80211_IFTYPE_STATION &&
505 iftype != NL80211_IFTYPE_OCB)
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300506 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800507 break;
508 }
509
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100510 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
511 tmp.h_proto = payload.proto;
Zhu Yie31a16d2009-05-21 21:47:03 +0800512
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100513 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
514 tmp.h_proto != htons(ETH_P_AARP) &&
515 tmp.h_proto != htons(ETH_P_IPX)) ||
516 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800517 /* remove RFC1042 or Bridge-Tunnel encapsulation and
518 * replace EtherType */
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100519 hdrlen += ETH_ALEN + 2;
520 else
Felix Fietkauc0417782016-06-29 10:36:39 +0200521 tmp.h_proto = htons(skb->len - hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800522
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100523 pskb_pull(skb, hdrlen);
524
525 if (!ehdr)
Johannes Bergd58ff352017-06-16 14:29:23 +0200526 ehdr = skb_push(skb, sizeof(struct ethhdr));
Felix Fietkau2d1c3042016-02-02 14:39:09 +0100527 memcpy(ehdr, &tmp, sizeof(tmp));
528
Zhu Yie31a16d2009-05-21 21:47:03 +0800529 return 0;
530}
Johannes Berg7f6990c2016-10-05 15:29:49 +0200531EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800532
Zhu Yieaf85ca2009-12-01 10:18:37 +0800533int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Johannes Bergc1e5f472014-05-19 17:53:16 +0200534 enum nl80211_iftype iftype,
535 const u8 *bssid, bool qos)
Zhu Yie31a16d2009-05-21 21:47:03 +0800536{
537 struct ieee80211_hdr hdr;
538 u16 hdrlen, ethertype;
539 __le16 fc;
540 const u8 *encaps_data;
541 int encaps_len, skip_header_bytes;
542 int nh_pos, h_pos;
543 int head_need;
544
545 if (unlikely(skb->len < ETH_HLEN))
546 return -EINVAL;
547
548 nh_pos = skb_network_header(skb) - skb->data;
549 h_pos = skb_transport_header(skb) - skb->data;
550
551 /* convert Ethernet header to proper 802.11 header (based on
552 * operation mode) */
553 ethertype = (skb->data[12] << 8) | skb->data[13];
554 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
555
556 switch (iftype) {
557 case NL80211_IFTYPE_AP:
558 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200559 case NL80211_IFTYPE_P2P_GO:
Zhu Yie31a16d2009-05-21 21:47:03 +0800560 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
561 /* DA BSSID SA */
562 memcpy(hdr.addr1, skb->data, ETH_ALEN);
563 memcpy(hdr.addr2, addr, ETH_ALEN);
564 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
565 hdrlen = 24;
566 break;
567 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200568 case NL80211_IFTYPE_P2P_CLIENT:
Zhu Yie31a16d2009-05-21 21:47:03 +0800569 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
570 /* BSSID SA DA */
571 memcpy(hdr.addr1, bssid, ETH_ALEN);
572 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
573 memcpy(hdr.addr3, skb->data, ETH_ALEN);
574 hdrlen = 24;
575 break;
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100576 case NL80211_IFTYPE_OCB:
Zhu Yie31a16d2009-05-21 21:47:03 +0800577 case NL80211_IFTYPE_ADHOC:
578 /* DA SA BSSID */
579 memcpy(hdr.addr1, skb->data, ETH_ALEN);
580 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
581 memcpy(hdr.addr3, bssid, ETH_ALEN);
582 hdrlen = 24;
583 break;
584 default:
585 return -EOPNOTSUPP;
586 }
587
588 if (qos) {
589 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
590 hdrlen += 2;
591 }
592
593 hdr.frame_control = fc;
594 hdr.duration_id = 0;
595 hdr.seq_ctrl = 0;
596
597 skip_header_bytes = ETH_HLEN;
598 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
599 encaps_data = bridge_tunnel_header;
600 encaps_len = sizeof(bridge_tunnel_header);
601 skip_header_bytes -= 2;
Simon Hormane5c5d222013-03-28 13:38:25 +0900602 } else if (ethertype >= ETH_P_802_3_MIN) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800603 encaps_data = rfc1042_header;
604 encaps_len = sizeof(rfc1042_header);
605 skip_header_bytes -= 2;
606 } else {
607 encaps_data = NULL;
608 encaps_len = 0;
609 }
610
611 skb_pull(skb, skip_header_bytes);
612 nh_pos -= skip_header_bytes;
613 h_pos -= skip_header_bytes;
614
615 head_need = hdrlen + encaps_len - skb_headroom(skb);
616
617 if (head_need > 0 || skb_cloned(skb)) {
618 head_need = max(head_need, 0);
619 if (head_need)
620 skb_orphan(skb);
621
Joe Perches24616152011-08-29 14:17:41 -0700622 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
Zhu Yie31a16d2009-05-21 21:47:03 +0800623 return -ENOMEM;
Zhu Yie31a16d2009-05-21 21:47:03 +0800624 }
625
626 if (encaps_data) {
627 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
628 nh_pos += encaps_len;
629 h_pos += encaps_len;
630 }
631
632 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
633
634 nh_pos += hdrlen;
635 h_pos += hdrlen;
636
637 /* Update skb pointers to various headers since this modified frame
638 * is going to go through Linux networking code that may potentially
639 * need things like pointer to IP header. */
Zhang Shengju3ef523a2016-03-03 01:16:57 +0000640 skb_reset_mac_header(skb);
Zhu Yie31a16d2009-05-21 21:47:03 +0800641 skb_set_network_header(skb, nh_pos);
642 skb_set_transport_header(skb, h_pos);
643
644 return 0;
645}
646EXPORT_SYMBOL(ieee80211_data_from_8023);
647
Felix Fietkau2b67f942016-02-08 14:34:42 +0100648static void
649__frame_add_frag(struct sk_buff *skb, struct page *page,
650 void *ptr, int len, int size)
651{
652 struct skb_shared_info *sh = skb_shinfo(skb);
653 int page_offset;
654
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700655 page_ref_inc(page);
Felix Fietkau2b67f942016-02-08 14:34:42 +0100656 page_offset = ptr - page_address(page);
657 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
658}
659
660static void
661__ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
662 int offset, int len)
663{
664 struct skb_shared_info *sh = skb_shinfo(skb);
Matthias Kaehlckeaa1702d2017-04-13 10:05:04 -0700665 const skb_frag_t *frag = &sh->frags[0];
Felix Fietkau2b67f942016-02-08 14:34:42 +0100666 struct page *frag_page;
667 void *frag_ptr;
668 int frag_len, frag_size;
669 int head_size = skb->len - skb->data_len;
670 int cur_len;
671
672 frag_page = virt_to_head_page(skb->head);
673 frag_ptr = skb->data;
674 frag_size = head_size;
675
676 while (offset >= frag_size) {
677 offset -= frag_size;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100678 frag_page = skb_frag_page(frag);
679 frag_ptr = skb_frag_address(frag);
680 frag_size = skb_frag_size(frag);
Matthias Kaehlckeaa1702d2017-04-13 10:05:04 -0700681 frag++;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100682 }
683
684 frag_ptr += offset;
685 frag_len = frag_size - offset;
686
687 cur_len = min(len, frag_len);
688
689 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
690 len -= cur_len;
691
692 while (len > 0) {
Felix Fietkau2b67f942016-02-08 14:34:42 +0100693 frag_len = skb_frag_size(frag);
694 cur_len = min(len, frag_len);
695 __frame_add_frag(frame, skb_frag_page(frag),
696 skb_frag_address(frag), cur_len, frag_len);
697 len -= cur_len;
Matthias Kaehlckeaa1702d2017-04-13 10:05:04 -0700698 frag++;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100699 }
700}
701
Felix Fietkau230fd282016-02-02 14:39:10 +0100702static struct sk_buff *
703__ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
Felix Fietkau2b67f942016-02-08 14:34:42 +0100704 int offset, int len, bool reuse_frag)
Felix Fietkau230fd282016-02-02 14:39:10 +0100705{
706 struct sk_buff *frame;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100707 int cur_len = len;
Felix Fietkau230fd282016-02-02 14:39:10 +0100708
709 if (skb->len - offset < len)
710 return NULL;
711
712 /*
Felix Fietkau2b67f942016-02-08 14:34:42 +0100713 * When reusing framents, copy some data to the head to simplify
714 * ethernet header handling and speed up protocol header processing
715 * in the stack later.
716 */
717 if (reuse_frag)
718 cur_len = min_t(int, len, 32);
719
720 /*
Felix Fietkau230fd282016-02-02 14:39:10 +0100721 * Allocate and reserve two bytes more for payload
722 * alignment since sizeof(struct ethhdr) is 14.
723 */
Felix Fietkau2b67f942016-02-08 14:34:42 +0100724 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
Gregory Greenman16a910a2016-07-05 15:23:10 +0300725 if (!frame)
726 return NULL;
Felix Fietkau230fd282016-02-02 14:39:10 +0100727
728 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
Felix Fietkau2b67f942016-02-08 14:34:42 +0100729 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
730
731 len -= cur_len;
732 if (!len)
733 return frame;
734
735 offset += cur_len;
736 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
Felix Fietkau230fd282016-02-02 14:39:10 +0100737
738 return frame;
739}
Zhu Yieaf85ca2009-12-01 10:18:37 +0800740
741void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
742 const u8 *addr, enum nl80211_iftype iftype,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700743 const unsigned int extra_headroom,
Johannes Berg8b935ee2016-10-05 16:17:01 +0200744 const u8 *check_da, const u8 *check_sa)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800745{
Felix Fietkau230fd282016-02-02 14:39:10 +0100746 unsigned int hlen = ALIGN(extra_headroom, 4);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800747 struct sk_buff *frame = NULL;
748 u16 ethertype;
749 u8 *payload;
Johannes Berg7f6990c2016-10-05 15:29:49 +0200750 int offset = 0, remaining;
Felix Fietkau230fd282016-02-02 14:39:10 +0100751 struct ethhdr eth;
Felix Fietkau2b67f942016-02-08 14:34:42 +0100752 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
Felix Fietkau2bf0ccc2016-02-08 14:25:26 +0100753 bool reuse_skb = false;
Felix Fietkau230fd282016-02-02 14:39:10 +0100754 bool last = false;
Felix Fietkau88665f52016-02-02 14:39:08 +0100755
Felix Fietkau230fd282016-02-02 14:39:10 +0100756 while (!last) {
757 unsigned int subframe_len;
758 int len;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800759 u8 padding;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800760
Felix Fietkau230fd282016-02-02 14:39:10 +0100761 skb_copy_bits(skb, offset, &eth, sizeof(eth));
762 len = ntohs(eth.h_proto);
763 subframe_len = sizeof(struct ethhdr) + len;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800764 padding = (4 - subframe_len) & 0x3;
Felix Fietkau230fd282016-02-02 14:39:10 +0100765
Zhu Yieaf85ca2009-12-01 10:18:37 +0800766 /* the last MSDU has no padding */
Felix Fietkau230fd282016-02-02 14:39:10 +0100767 remaining = skb->len - offset;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800768 if (subframe_len > remaining)
769 goto purge;
770
Felix Fietkau230fd282016-02-02 14:39:10 +0100771 offset += sizeof(struct ethhdr);
Felix Fietkau230fd282016-02-02 14:39:10 +0100772 last = remaining <= subframe_len + padding;
Johannes Berg8b935ee2016-10-05 16:17:01 +0200773
774 /* FIXME: should we really accept multicast DA? */
775 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
776 !ether_addr_equal(check_da, eth.h_dest)) ||
777 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
778 offset += len + padding;
779 continue;
780 }
781
782 /* reuse skb for the last subframe */
Felix Fietkau2b67f942016-02-08 14:34:42 +0100783 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
Felix Fietkau230fd282016-02-02 14:39:10 +0100784 skb_pull(skb, offset);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800785 frame = skb;
Felix Fietkau230fd282016-02-02 14:39:10 +0100786 reuse_skb = true;
787 } else {
Felix Fietkau2b67f942016-02-08 14:34:42 +0100788 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
789 reuse_frag);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800790 if (!frame)
791 goto purge;
792
Felix Fietkau230fd282016-02-02 14:39:10 +0100793 offset += len + padding;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800794 }
795
796 skb_reset_network_header(frame);
797 frame->dev = skb->dev;
798 frame->priority = skb->priority;
799
800 payload = frame->data;
801 ethertype = (payload[6] << 8) | payload[7];
Joe Perchesac422d32012-05-08 18:56:55 +0000802 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yieaf85ca2009-12-01 10:18:37 +0800803 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perchesac422d32012-05-08 18:56:55 +0000804 ether_addr_equal(payload, bridge_tunnel_header))) {
Felix Fietkau230fd282016-02-02 14:39:10 +0100805 eth.h_proto = htons(ethertype);
806 skb_pull(frame, ETH_ALEN + 2);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800807 }
Felix Fietkau230fd282016-02-02 14:39:10 +0100808
809 memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
Zhu Yieaf85ca2009-12-01 10:18:37 +0800810 __skb_queue_tail(list, frame);
811 }
812
Felix Fietkau230fd282016-02-02 14:39:10 +0100813 if (!reuse_skb)
814 dev_kfree_skb(skb);
815
Zhu Yieaf85ca2009-12-01 10:18:37 +0800816 return;
817
818 purge:
819 __skb_queue_purge(list);
Zhu Yieaf85ca2009-12-01 10:18:37 +0800820 dev_kfree_skb(skb);
821}
822EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
823
Zhu Yie31a16d2009-05-21 21:47:03 +0800824/* Given a data frame determine the 802.1p/1d tag to use. */
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800825unsigned int cfg80211_classify8021d(struct sk_buff *skb,
826 struct cfg80211_qos_map *qos_map)
Zhu Yie31a16d2009-05-21 21:47:03 +0800827{
828 unsigned int dscp;
cedric Vonckenc6ca5e22013-08-26 14:04:52 +0200829 unsigned char vlan_priority;
Zhu Yie31a16d2009-05-21 21:47:03 +0800830
831 /* skb->priority values from 256->263 are magic values to
832 * directly indicate a specific 802.1d priority. This is used
833 * to allow 802.1d priority to be passed directly in from VLAN
834 * tags, etc.
835 */
836 if (skb->priority >= 256 && skb->priority <= 263)
837 return skb->priority - 256;
838
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100839 if (skb_vlan_tag_present(skb)) {
840 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
cedric Vonckenc6ca5e22013-08-26 14:04:52 +0200841 >> VLAN_PRIO_SHIFT;
842 if (vlan_priority > 0)
843 return vlan_priority;
844 }
845
Zhu Yie31a16d2009-05-21 21:47:03 +0800846 switch (skb->protocol) {
847 case htons(ETH_P_IP):
Dave Tähtb1565792011-12-22 12:55:08 -0800848 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
849 break;
850 case htons(ETH_P_IPV6):
851 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
Zhu Yie31a16d2009-05-21 21:47:03 +0800852 break;
Simon Wunderlich960d97f2014-03-03 17:23:12 +0100853 case htons(ETH_P_MPLS_UC):
854 case htons(ETH_P_MPLS_MC): {
855 struct mpls_label mpls_tmp, *mpls;
856
857 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
858 sizeof(*mpls), &mpls_tmp);
859 if (!mpls)
860 return 0;
861
862 return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
863 >> MPLS_LS_TC_SHIFT;
864 }
865 case htons(ETH_P_80221):
866 /* 802.21 is always network control traffic */
867 return 7;
Zhu Yie31a16d2009-05-21 21:47:03 +0800868 default:
869 return 0;
870 }
871
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800872 if (qos_map) {
873 unsigned int i, tmp_dscp = dscp >> 2;
874
875 for (i = 0; i < qos_map->num_des; i++) {
876 if (tmp_dscp == qos_map->dscp_exception[i].dscp)
877 return qos_map->dscp_exception[i].up;
878 }
879
880 for (i = 0; i < 8; i++) {
881 if (tmp_dscp >= qos_map->up[i].low &&
882 tmp_dscp <= qos_map->up[i].high)
883 return i;
884 }
885 }
886
Zhu Yie31a16d2009-05-21 21:47:03 +0800887 return dscp >> 5;
888}
889EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200890
891const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
892{
Johannes Berg9caf0362012-11-29 01:25:20 +0100893 const struct cfg80211_bss_ies *ies;
894
895 ies = rcu_dereference(bss->ies);
896 if (!ies)
Johannes Berg517357c2009-07-02 17:18:40 +0200897 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +0100898
899 return cfg80211_find_ie(ie, ies->data, ies->len);
Johannes Berg517357c2009-07-02 17:18:40 +0200900}
901EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200902
903void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
904{
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800905 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200906 struct net_device *dev = wdev->netdev;
907 int i;
908
909 if (!wdev->connect_keys)
910 return;
911
David Spinadelb8676222016-09-22 23:16:50 +0300912 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200913 if (!wdev->connect_keys->params[i].cipher)
914 continue;
Hila Gonene35e4d22012-06-27 17:19:42 +0300915 if (rdev_add_key(rdev, dev, i, false, NULL,
916 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800917 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800918 continue;
919 }
Johannes Bergd4f29972017-02-13 20:53:38 +0100920 if (wdev->connect_keys->def == i &&
921 rdev_set_default_key(rdev, dev, i, true, true)) {
922 netdev_err(dev, "failed to set defkey %d\n", i);
923 continue;
924 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200925 }
926
Johannes Bergb47f6102014-09-10 13:39:54 +0300927 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200928 wdev->connect_keys = NULL;
929}
Johannes Berg3d54d252009-08-21 14:51:05 +0200930
Daniel Drake1f6fc432012-08-02 18:41:48 +0100931void cfg80211_process_wdev_events(struct wireless_dev *wdev)
Johannes Berg3d54d252009-08-21 14:51:05 +0200932{
933 struct cfg80211_event *ev;
934 unsigned long flags;
Johannes Berg3d54d252009-08-21 14:51:05 +0200935
936 spin_lock_irqsave(&wdev->event_lock, flags);
937 while (!list_empty(&wdev->event_list)) {
938 ev = list_first_entry(&wdev->event_list,
939 struct cfg80211_event, list);
940 list_del(&ev->list);
941 spin_unlock_irqrestore(&wdev->event_lock, flags);
942
943 wdev_lock(wdev);
944 switch (ev->type) {
945 case EVENT_CONNECT_RESULT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200946 __cfg80211_connect_result(
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +0300947 wdev->netdev,
948 &ev->cr,
949 ev->cr.status == WLAN_STATUS_SUCCESS);
Johannes Berg3d54d252009-08-21 14:51:05 +0200950 break;
951 case EVENT_ROAMED:
Avraham Stern29ce6ec2017-04-26 10:58:49 +0300952 __cfg80211_roamed(wdev, &ev->rm);
Johannes Berg3d54d252009-08-21 14:51:05 +0200953 break;
954 case EVENT_DISCONNECTED:
955 __cfg80211_disconnected(wdev->netdev,
956 ev->dc.ie, ev->dc.ie_len,
Johannes Berg80279fb2015-05-22 16:22:20 +0200957 ev->dc.reason,
958 !ev->dc.locally_generated);
Johannes Berg3d54d252009-08-21 14:51:05 +0200959 break;
960 case EVENT_IBSS_JOINED:
Antonio Quartullife94f3a2014-01-29 17:53:43 +0100961 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
962 ev->ij.channel);
Johannes Berg3d54d252009-08-21 14:51:05 +0200963 break;
Michal Kaziorf04c2202014-04-09 15:11:01 +0200964 case EVENT_STOPPED:
965 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
966 break;
Johannes Berg3d54d252009-08-21 14:51:05 +0200967 }
968 wdev_unlock(wdev);
969
970 kfree(ev);
971
972 spin_lock_irqsave(&wdev->event_lock, flags);
973 }
974 spin_unlock_irqrestore(&wdev->event_lock, flags);
975}
976
977void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
978{
979 struct wireless_dev *wdev;
980
981 ASSERT_RTNL();
Johannes Berg3d54d252009-08-21 14:51:05 +0200982
Johannes Berg53873f12016-05-03 16:52:04 +0300983 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
Johannes Berg3d54d252009-08-21 14:51:05 +0200984 cfg80211_process_wdev_events(wdev);
Johannes Berg3d54d252009-08-21 14:51:05 +0200985}
986
987int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
988 struct net_device *dev, enum nl80211_iftype ntype,
Johannes Berg818a9862017-04-12 11:23:28 +0200989 struct vif_params *params)
Johannes Berg3d54d252009-08-21 14:51:05 +0200990{
991 int err;
992 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
993
Zhao, Gang73fb08e2014-03-19 17:04:36 +0800994 ASSERT_RTNL();
Johannes Berg3d54d252009-08-21 14:51:05 +0200995
996 /* don't support changing VLANs, you just re-create them */
997 if (otype == NL80211_IFTYPE_AP_VLAN)
998 return -EOPNOTSUPP;
999
Ayala Bekercb3b7d82016-09-20 17:31:13 +03001000 /* cannot change into P2P device or NAN */
1001 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
1002 ntype == NL80211_IFTYPE_NAN)
Johannes Berg98104fde2012-06-16 00:19:54 +02001003 return -EOPNOTSUPP;
1004
Johannes Berg3d54d252009-08-21 14:51:05 +02001005 if (!rdev->ops->change_virtual_intf ||
1006 !(rdev->wiphy.interface_modes & (1 << ntype)))
1007 return -EOPNOTSUPP;
1008
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001009 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001010 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02001011 (ntype == NL80211_IFTYPE_ADHOC ||
1012 ntype == NL80211_IFTYPE_STATION ||
1013 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001014 return -EBUSY;
1015
Michal Kazior6cbfb1b2015-05-22 10:57:22 +02001016 if (ntype != otype) {
Johannes Berg9bc383d2009-11-19 11:55:19 +01001017 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +01001018 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg194ff522013-12-30 23:12:37 +01001019 wdev_lock(dev->ieee80211_ptr);
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001020 rdev_set_qos_map(rdev, dev, NULL);
Johannes Berg194ff522013-12-30 23:12:37 +01001021 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001022
Johannes Berg3d54d252009-08-21 14:51:05 +02001023 switch (otype) {
Michal Kaziorac800142012-06-29 12:46:57 +02001024 case NL80211_IFTYPE_AP:
Ilan Peer7c8d5e02014-02-25 15:33:38 +02001025 cfg80211_stop_ap(rdev, dev, true);
Michal Kaziorac800142012-06-29 12:46:57 +02001026 break;
Johannes Berg3d54d252009-08-21 14:51:05 +02001027 case NL80211_IFTYPE_ADHOC:
1028 cfg80211_leave_ibss(rdev, dev, false);
1029 break;
1030 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +02001031 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg83739b02013-05-15 17:44:01 +02001032 wdev_lock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +02001033 cfg80211_disconnect(rdev, dev,
1034 WLAN_REASON_DEAUTH_LEAVING, true);
Johannes Berg83739b02013-05-15 17:44:01 +02001035 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +02001036 break;
1037 case NL80211_IFTYPE_MESH_POINT:
1038 /* mesh should be handled? */
1039 break;
1040 default:
1041 break;
1042 }
1043
1044 cfg80211_process_rdev_events(rdev);
1045 }
1046
Johannes Berg818a9862017-04-12 11:23:28 +02001047 err = rdev_change_virtual_intf(rdev, dev, ntype, params);
Johannes Berg3d54d252009-08-21 14:51:05 +02001048
1049 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1050
Johannes Berg9bc383d2009-11-19 11:55:19 +01001051 if (!err && params && params->use_4addr != -1)
1052 dev->ieee80211_ptr->use_4addr = params->use_4addr;
1053
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001054 if (!err) {
1055 dev->priv_flags &= ~IFF_DONT_BRIDGE;
1056 switch (ntype) {
1057 case NL80211_IFTYPE_STATION:
1058 if (dev->ieee80211_ptr->use_4addr)
1059 break;
1060 /* fall through */
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +01001061 case NL80211_IFTYPE_OCB:
Johannes Berg074ac8d2010-09-16 14:58:22 +02001062 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001063 case NL80211_IFTYPE_ADHOC:
1064 dev->priv_flags |= IFF_DONT_BRIDGE;
1065 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02001066 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001067 case NL80211_IFTYPE_AP:
1068 case NL80211_IFTYPE_AP_VLAN:
1069 case NL80211_IFTYPE_WDS:
1070 case NL80211_IFTYPE_MESH_POINT:
1071 /* bridging OK */
1072 break;
1073 case NL80211_IFTYPE_MONITOR:
1074 /* monitor can't bridge anyway */
1075 break;
1076 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f782010-08-12 15:38:38 +02001077 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001078 /* not happening */
1079 break;
Johannes Berg98104fde2012-06-16 00:19:54 +02001080 case NL80211_IFTYPE_P2P_DEVICE:
Ayala Bekercb3b7d82016-09-20 17:31:13 +03001081 case NL80211_IFTYPE_NAN:
Johannes Berg98104fde2012-06-16 00:19:54 +02001082 WARN_ON(1);
1083 break;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001084 }
1085 }
1086
Michal Kaziordbbae262012-06-29 12:47:01 +02001087 if (!err && ntype != otype && netif_running(dev)) {
1088 cfg80211_update_iface_num(rdev, ntype, 1);
1089 cfg80211_update_iface_num(rdev, otype, -1);
1090 }
1091
Johannes Berg3d54d252009-08-21 14:51:05 +02001092 return err;
1093}
John W. Linville254416a2009-12-09 16:43:52 -05001094
Johannes Berg0c1eca42017-02-15 15:02:08 +01001095static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
1096{
1097 int modulation, streams, bitrate;
1098
1099 /* the formula below does only work for MCS values smaller than 32 */
1100 if (WARN_ON_ONCE(rate->mcs >= 32))
1101 return 0;
1102
1103 modulation = rate->mcs & 7;
1104 streams = (rate->mcs >> 3) + 1;
1105
1106 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1107
1108 if (modulation < 4)
1109 bitrate *= (modulation + 1);
1110 else if (modulation == 4)
1111 bitrate *= (modulation + 2);
1112 else
1113 bitrate *= (modulation + 3);
1114
1115 bitrate *= streams;
1116
1117 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1118 bitrate = (bitrate / 9) * 10;
1119
1120 /* do NOT round down here */
1121 return (bitrate + 50000) / 100000;
1122}
1123
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +03001124static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
1125{
1126 static const u32 __mcs2bitrate[] = {
1127 /* control PHY */
1128 [0] = 275,
1129 /* SC PHY */
1130 [1] = 3850,
1131 [2] = 7700,
1132 [3] = 9625,
1133 [4] = 11550,
1134 [5] = 12512, /* 1251.25 mbps */
1135 [6] = 15400,
1136 [7] = 19250,
1137 [8] = 23100,
1138 [9] = 25025,
1139 [10] = 30800,
1140 [11] = 38500,
1141 [12] = 46200,
1142 /* OFDM PHY */
1143 [13] = 6930,
1144 [14] = 8662, /* 866.25 mbps */
1145 [15] = 13860,
1146 [16] = 17325,
1147 [17] = 20790,
1148 [18] = 27720,
1149 [19] = 34650,
1150 [20] = 41580,
1151 [21] = 45045,
1152 [22] = 51975,
1153 [23] = 62370,
1154 [24] = 67568, /* 6756.75 mbps */
1155 /* LP-SC PHY */
1156 [25] = 6260,
1157 [26] = 8340,
1158 [27] = 11120,
1159 [28] = 12510,
1160 [29] = 16680,
1161 [30] = 22240,
1162 [31] = 25030,
1163 };
1164
1165 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1166 return 0;
1167
1168 return __mcs2bitrate[rate->mcs];
1169}
1170
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001171static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1172{
1173 static const u32 base[4][10] = {
1174 { 6500000,
1175 13000000,
1176 19500000,
1177 26000000,
1178 39000000,
1179 52000000,
1180 58500000,
1181 65000000,
1182 78000000,
Pedersen, Thomas8fdd1362016-10-31 11:28:40 -07001183 /* not in the spec, but some devices use this: */
1184 86500000,
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001185 },
1186 { 13500000,
1187 27000000,
1188 40500000,
1189 54000000,
1190 81000000,
1191 108000000,
1192 121500000,
1193 135000000,
1194 162000000,
1195 180000000,
1196 },
1197 { 29300000,
1198 58500000,
1199 87800000,
1200 117000000,
1201 175500000,
1202 234000000,
1203 263300000,
1204 292500000,
1205 351000000,
1206 390000000,
1207 },
1208 { 58500000,
1209 117000000,
1210 175500000,
1211 234000000,
1212 351000000,
1213 468000000,
1214 526500000,
1215 585000000,
1216 702000000,
1217 780000000,
1218 },
1219 };
1220 u32 bitrate;
1221 int idx;
1222
Johannes Bergca8fe252017-05-04 07:52:10 +02001223 if (rate->mcs > 9)
1224 goto warn;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001225
Johannes Bergb51f3be2015-01-15 16:14:02 +01001226 switch (rate->bw) {
1227 case RATE_INFO_BW_160:
1228 idx = 3;
1229 break;
1230 case RATE_INFO_BW_80:
1231 idx = 2;
1232 break;
1233 case RATE_INFO_BW_40:
1234 idx = 1;
1235 break;
1236 case RATE_INFO_BW_5:
1237 case RATE_INFO_BW_10:
1238 default:
Johannes Bergca8fe252017-05-04 07:52:10 +02001239 goto warn;
Johannes Bergb51f3be2015-01-15 16:14:02 +01001240 case RATE_INFO_BW_20:
1241 idx = 0;
1242 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001243
1244 bitrate = base[idx][rate->mcs];
1245 bitrate *= rate->nss;
1246
1247 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1248 bitrate = (bitrate / 9) * 10;
1249
1250 /* do NOT round down here */
1251 return (bitrate + 50000) / 100000;
Johannes Bergca8fe252017-05-04 07:52:10 +02001252 warn:
1253 WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
1254 rate->bw, rate->mcs, rate->nss);
1255 return 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001256}
1257
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03001258u32 cfg80211_calculate_bitrate(struct rate_info *rate)
John W. Linville254416a2009-12-09 16:43:52 -05001259{
Johannes Berg0c1eca42017-02-15 15:02:08 +01001260 if (rate->flags & RATE_INFO_FLAGS_MCS)
1261 return cfg80211_calculate_bitrate_ht(rate);
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +03001262 if (rate->flags & RATE_INFO_FLAGS_60G)
1263 return cfg80211_calculate_bitrate_60g(rate);
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001264 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1265 return cfg80211_calculate_bitrate_vht(rate);
John W. Linville254416a2009-12-09 16:43:52 -05001266
Johannes Berg0c1eca42017-02-15 15:02:08 +01001267 return rate->legacy;
John W. Linville254416a2009-12-09 16:43:52 -05001268}
Thomas Pedersen8097e142012-03-05 15:31:47 -08001269EXPORT_SYMBOL(cfg80211_calculate_bitrate);
Johannes Berg56d18932011-05-09 18:41:15 +02001270
Arend van Sprielc216e642012-11-25 19:13:28 +01001271int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1272 enum ieee80211_p2p_attr_id attr,
1273 u8 *buf, unsigned int bufsize)
Johannes Berg0ee45352012-10-29 19:48:40 +01001274{
1275 u8 *out = buf;
1276 u16 attr_remaining = 0;
1277 bool desired_attr = false;
1278 u16 desired_len = 0;
1279
1280 while (len > 0) {
1281 unsigned int iedatalen;
1282 unsigned int copy;
1283 const u8 *iedata;
1284
1285 if (len < 2)
1286 return -EILSEQ;
1287 iedatalen = ies[1];
1288 if (iedatalen + 2 > len)
1289 return -EILSEQ;
1290
1291 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1292 goto cont;
1293
1294 if (iedatalen < 4)
1295 goto cont;
1296
1297 iedata = ies + 2;
1298
1299 /* check WFA OUI, P2P subtype */
1300 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1301 iedata[2] != 0x9a || iedata[3] != 0x09)
1302 goto cont;
1303
1304 iedatalen -= 4;
1305 iedata += 4;
1306
1307 /* check attribute continuation into this IE */
1308 copy = min_t(unsigned int, attr_remaining, iedatalen);
1309 if (copy && desired_attr) {
1310 desired_len += copy;
1311 if (out) {
1312 memcpy(out, iedata, min(bufsize, copy));
1313 out += min(bufsize, copy);
1314 bufsize -= min(bufsize, copy);
1315 }
1316
1317
1318 if (copy == attr_remaining)
1319 return desired_len;
1320 }
1321
1322 attr_remaining -= copy;
1323 if (attr_remaining)
1324 goto cont;
1325
1326 iedatalen -= copy;
1327 iedata += copy;
1328
1329 while (iedatalen > 0) {
1330 u16 attr_len;
1331
1332 /* P2P attribute ID & size must fit */
1333 if (iedatalen < 3)
1334 return -EILSEQ;
1335 desired_attr = iedata[0] == attr;
1336 attr_len = get_unaligned_le16(iedata + 1);
1337 iedatalen -= 3;
1338 iedata += 3;
1339
1340 copy = min_t(unsigned int, attr_len, iedatalen);
1341
1342 if (desired_attr) {
1343 desired_len += copy;
1344 if (out) {
1345 memcpy(out, iedata, min(bufsize, copy));
1346 out += min(bufsize, copy);
1347 bufsize -= min(bufsize, copy);
1348 }
1349
1350 if (copy == attr_len)
1351 return desired_len;
1352 }
1353
1354 iedata += copy;
1355 iedatalen -= copy;
1356 attr_remaining = attr_len - copy;
1357 }
1358
1359 cont:
1360 len -= ies[1] + 2;
1361 ies += ies[1] + 2;
1362 }
1363
1364 if (attr_remaining && desired_attr)
1365 return -EILSEQ;
1366
1367 return -ENOENT;
1368}
1369EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1370
Johannes Berg29464cc2015-03-31 15:36:22 +02001371static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1372{
1373 int i;
1374
1375 for (i = 0; i < n_ids; i++)
1376 if (ids[i] == id)
1377 return true;
1378 return false;
1379}
1380
Johannes Berg8ac63442015-09-04 20:03:23 +02001381static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1382{
1383 /* we assume a validly formed IEs buffer */
1384 u8 len = ies[pos + 1];
1385
1386 pos += 2 + len;
1387
1388 /* the IE itself must have 255 bytes for fragments to follow */
1389 if (len < 255)
1390 return pos;
1391
1392 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1393 len = ies[pos + 1];
1394 pos += 2 + len;
1395 }
1396
1397 return pos;
1398}
1399
Johannes Berg29464cc2015-03-31 15:36:22 +02001400size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1401 const u8 *ids, int n_ids,
1402 const u8 *after_ric, int n_after_ric,
1403 size_t offset)
1404{
1405 size_t pos = offset;
1406
1407 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos])) {
1408 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
Johannes Berg8ac63442015-09-04 20:03:23 +02001409 pos = skip_ie(ies, ielen, pos);
Johannes Berg29464cc2015-03-31 15:36:22 +02001410
1411 while (pos < ielen &&
1412 !ieee80211_id_in_list(after_ric, n_after_ric,
1413 ies[pos]))
Johannes Berg8ac63442015-09-04 20:03:23 +02001414 pos = skip_ie(ies, ielen, pos);
Johannes Berg29464cc2015-03-31 15:36:22 +02001415 } else {
Johannes Berg8ac63442015-09-04 20:03:23 +02001416 pos = skip_ie(ies, ielen, pos);
Johannes Berg29464cc2015-03-31 15:36:22 +02001417 }
1418 }
1419
1420 return pos;
1421}
1422EXPORT_SYMBOL(ieee80211_ie_split_ric);
1423
Johannes Berg1ce3e822012-08-01 17:00:55 +02001424bool ieee80211_operating_class_to_band(u8 operating_class,
Johannes Berg57fbcce2016-04-12 15:56:15 +02001425 enum nl80211_band *band)
Johannes Berg1ce3e822012-08-01 17:00:55 +02001426{
1427 switch (operating_class) {
1428 case 112:
1429 case 115 ... 127:
Eliad Peller954a86e2015-03-01 09:10:01 +02001430 case 128 ... 130:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001431 *band = NL80211_BAND_5GHZ;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001432 return true;
1433 case 81:
1434 case 82:
1435 case 83:
1436 case 84:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001437 *band = NL80211_BAND_2GHZ;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001438 return true;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001439 case 180:
Johannes Berg57fbcce2016-04-12 15:56:15 +02001440 *band = NL80211_BAND_60GHZ;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001441 return true;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001442 }
1443
1444 return false;
1445}
1446EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1447
Arik Nemtsova38700d2015-03-18 08:46:08 +02001448bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1449 u8 *op_class)
1450{
1451 u8 vht_opclass;
1452 u16 freq = chandef->center_freq1;
1453
1454 if (freq >= 2412 && freq <= 2472) {
1455 if (chandef->width > NL80211_CHAN_WIDTH_40)
1456 return false;
1457
1458 /* 2.407 GHz, channels 1..13 */
1459 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1460 if (freq > chandef->chan->center_freq)
1461 *op_class = 83; /* HT40+ */
1462 else
1463 *op_class = 84; /* HT40- */
1464 } else {
1465 *op_class = 81;
1466 }
1467
1468 return true;
1469 }
1470
1471 if (freq == 2484) {
1472 if (chandef->width > NL80211_CHAN_WIDTH_40)
1473 return false;
1474
1475 *op_class = 82; /* channel 14 */
1476 return true;
1477 }
1478
1479 switch (chandef->width) {
1480 case NL80211_CHAN_WIDTH_80:
1481 vht_opclass = 128;
1482 break;
1483 case NL80211_CHAN_WIDTH_160:
1484 vht_opclass = 129;
1485 break;
1486 case NL80211_CHAN_WIDTH_80P80:
1487 vht_opclass = 130;
1488 break;
1489 case NL80211_CHAN_WIDTH_10:
1490 case NL80211_CHAN_WIDTH_5:
1491 return false; /* unsupported for now */
1492 default:
1493 vht_opclass = 0;
1494 break;
1495 }
1496
1497 /* 5 GHz, channels 36..48 */
1498 if (freq >= 5180 && freq <= 5240) {
1499 if (vht_opclass) {
1500 *op_class = vht_opclass;
1501 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1502 if (freq > chandef->chan->center_freq)
1503 *op_class = 116;
1504 else
1505 *op_class = 117;
1506 } else {
1507 *op_class = 115;
1508 }
1509
1510 return true;
1511 }
1512
1513 /* 5 GHz, channels 52..64 */
1514 if (freq >= 5260 && freq <= 5320) {
1515 if (vht_opclass) {
1516 *op_class = vht_opclass;
1517 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1518 if (freq > chandef->chan->center_freq)
1519 *op_class = 119;
1520 else
1521 *op_class = 120;
1522 } else {
1523 *op_class = 118;
1524 }
1525
1526 return true;
1527 }
1528
1529 /* 5 GHz, channels 100..144 */
1530 if (freq >= 5500 && freq <= 5720) {
1531 if (vht_opclass) {
1532 *op_class = vht_opclass;
1533 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1534 if (freq > chandef->chan->center_freq)
1535 *op_class = 122;
1536 else
1537 *op_class = 123;
1538 } else {
1539 *op_class = 121;
1540 }
1541
1542 return true;
1543 }
1544
1545 /* 5 GHz, channels 149..169 */
1546 if (freq >= 5745 && freq <= 5845) {
1547 if (vht_opclass) {
1548 *op_class = vht_opclass;
1549 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1550 if (freq > chandef->chan->center_freq)
1551 *op_class = 126;
1552 else
1553 *op_class = 127;
1554 } else if (freq <= 5805) {
1555 *op_class = 124;
1556 } else {
1557 *op_class = 125;
1558 }
1559
1560 return true;
1561 }
1562
1563 /* 56.16 GHz, channel 1..4 */
1564 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
1565 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1566 return false;
1567
1568 *op_class = 180;
1569 return true;
1570 }
1571
1572 /* not supported yet */
1573 return false;
1574}
1575EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1576
Johannes Berg4c8dea62016-10-21 14:25:13 +02001577static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1578 u32 *beacon_int_gcd,
1579 bool *beacon_int_different)
1580{
1581 struct wireless_dev *wdev;
1582
1583 *beacon_int_gcd = 0;
1584 *beacon_int_different = false;
1585
1586 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1587 if (!wdev->beacon_interval)
1588 continue;
1589
1590 if (!*beacon_int_gcd) {
1591 *beacon_int_gcd = wdev->beacon_interval;
1592 continue;
1593 }
1594
1595 if (wdev->beacon_interval == *beacon_int_gcd)
1596 continue;
1597
1598 *beacon_int_different = true;
1599 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1600 }
1601
1602 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1603 if (*beacon_int_gcd)
1604 *beacon_int_different = true;
1605 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1606 }
1607}
1608
Johannes Berg56d18932011-05-09 18:41:15 +02001609int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05301610 enum nl80211_iftype iftype, u32 beacon_int)
Johannes Berg56d18932011-05-09 18:41:15 +02001611{
Johannes Berg4c8dea62016-10-21 14:25:13 +02001612 /*
1613 * This is just a basic pre-condition check; if interface combinations
1614 * are possible the driver must already be checking those with a call
1615 * to cfg80211_check_combinations(), in which case we'll validate more
1616 * through the cfg80211_calculate_bi_data() call and code in
1617 * cfg80211_iter_combinations().
1618 */
Johannes Berg56d18932011-05-09 18:41:15 +02001619
Purushottam Kushwaha12d20fc92016-08-11 15:14:02 +05301620 if (beacon_int < 10 || beacon_int > 10000)
Johannes Berg56d18932011-05-09 18:41:15 +02001621 return -EINVAL;
1622
Johannes Berg4c8dea62016-10-21 14:25:13 +02001623 return 0;
Johannes Berg56d18932011-05-09 18:41:15 +02001624}
Johannes Berg7527a782011-05-13 10:58:57 +02001625
Michal Kazior65a124d2014-04-09 15:29:22 +02001626int cfg80211_iter_combinations(struct wiphy *wiphy,
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301627 struct iface_combination_params *params,
Michal Kazior65a124d2014-04-09 15:29:22 +02001628 void (*iter)(const struct ieee80211_iface_combination *c,
1629 void *data),
1630 void *data)
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001631{
Felix Fietkau8c48b502014-05-05 11:48:40 +02001632 const struct ieee80211_regdomain *regdom;
1633 enum nl80211_dfs_regions region = 0;
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001634 int i, j, iftype;
1635 int num_interfaces = 0;
1636 u32 used_iftypes = 0;
Johannes Berg4c8dea62016-10-21 14:25:13 +02001637 u32 beacon_int_gcd;
1638 bool beacon_int_different;
1639
1640 /*
1641 * This is a bit strange, since the iteration used to rely only on
1642 * the data given by the driver, but here it now relies on context,
1643 * in form of the currently operating interfaces.
1644 * This is OK for all current users, and saves us from having to
1645 * push the GCD calculations into all the drivers.
1646 * In the future, this should probably rely more on data that's in
1647 * cfg80211 already - the only thing not would appear to be any new
1648 * interfaces (while being brought up) and channel/radar data.
1649 */
1650 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1651 &beacon_int_gcd, &beacon_int_different);
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001652
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301653 if (params->radar_detect) {
Felix Fietkau8c48b502014-05-05 11:48:40 +02001654 rcu_read_lock();
1655 regdom = rcu_dereference(cfg80211_regdomain);
1656 if (regdom)
1657 region = regdom->dfs_region;
1658 rcu_read_unlock();
1659 }
1660
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001661 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301662 num_interfaces += params->iftype_num[iftype];
1663 if (params->iftype_num[iftype] > 0 &&
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001664 !(wiphy->software_iftypes & BIT(iftype)))
1665 used_iftypes |= BIT(iftype);
1666 }
1667
1668 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1669 const struct ieee80211_iface_combination *c;
1670 struct ieee80211_iface_limit *limits;
1671 u32 all_iftypes = 0;
1672
1673 c = &wiphy->iface_combinations[i];
1674
1675 if (num_interfaces > c->max_interfaces)
1676 continue;
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301677 if (params->num_different_channels > c->num_different_channels)
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001678 continue;
1679
1680 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1681 GFP_KERNEL);
1682 if (!limits)
1683 return -ENOMEM;
1684
1685 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1686 if (wiphy->software_iftypes & BIT(iftype))
1687 continue;
1688 for (j = 0; j < c->n_limits; j++) {
1689 all_iftypes |= limits[j].types;
1690 if (!(limits[j].types & BIT(iftype)))
1691 continue;
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301692 if (limits[j].max < params->iftype_num[iftype])
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001693 goto cont;
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301694 limits[j].max -= params->iftype_num[iftype];
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001695 }
1696 }
1697
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301698 if (params->radar_detect !=
1699 (c->radar_detect_widths & params->radar_detect))
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001700 goto cont;
1701
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301702 if (params->radar_detect && c->radar_detect_regions &&
Felix Fietkau8c48b502014-05-05 11:48:40 +02001703 !(c->radar_detect_regions & BIT(region)))
1704 goto cont;
1705
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001706 /* Finally check that all iftypes that we're currently
1707 * using are actually part of this combination. If they
1708 * aren't then we can't use this combination and have
1709 * to continue to the next.
1710 */
1711 if ((all_iftypes & used_iftypes) != used_iftypes)
1712 goto cont;
1713
Johannes Berg4c8dea62016-10-21 14:25:13 +02001714 if (beacon_int_gcd) {
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05301715 if (c->beacon_int_min_gcd &&
Johannes Berg4c8dea62016-10-21 14:25:13 +02001716 beacon_int_gcd < c->beacon_int_min_gcd)
Johannes Berg0507a3a2016-10-21 12:15:00 +02001717 goto cont;
Johannes Berg4c8dea62016-10-21 14:25:13 +02001718 if (!c->beacon_int_min_gcd && beacon_int_different)
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05301719 goto cont;
1720 }
1721
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001722 /* This combination covered all interface types and
1723 * supported the requested numbers, so we're good.
1724 */
Michal Kazior65a124d2014-04-09 15:29:22 +02001725
1726 (*iter)(c, data);
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001727 cont:
1728 kfree(limits);
1729 }
1730
Michal Kazior65a124d2014-04-09 15:29:22 +02001731 return 0;
1732}
1733EXPORT_SYMBOL(cfg80211_iter_combinations);
1734
1735static void
1736cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1737 void *data)
1738{
1739 int *num = data;
1740 (*num)++;
1741}
1742
1743int cfg80211_check_combinations(struct wiphy *wiphy,
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301744 struct iface_combination_params *params)
Michal Kazior65a124d2014-04-09 15:29:22 +02001745{
1746 int err, num = 0;
1747
Purushottam Kushwahae2273002016-10-12 18:25:35 +05301748 err = cfg80211_iter_combinations(wiphy, params,
Michal Kazior65a124d2014-04-09 15:29:22 +02001749 cfg80211_iter_sum_ifcombs, &num);
1750 if (err)
1751 return err;
1752 if (num == 0)
1753 return -EBUSY;
1754
1755 return 0;
Luciano Coelhocb2d9562014-02-17 16:52:35 +02001756}
1757EXPORT_SYMBOL(cfg80211_check_combinations);
1758
Johannes Berg34850ab2011-07-18 18:08:35 +02001759int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1760 const u8 *rates, unsigned int n_rates,
1761 u32 *mask)
1762{
1763 int i, j;
1764
Johannes Berga401d2b2011-07-20 00:52:16 +02001765 if (!sband)
1766 return -EINVAL;
1767
Johannes Berg34850ab2011-07-18 18:08:35 +02001768 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1769 return -EINVAL;
1770
1771 *mask = 0;
1772
1773 for (i = 0; i < n_rates; i++) {
1774 int rate = (rates[i] & 0x7f) * 5;
1775 bool found = false;
1776
1777 for (j = 0; j < sband->n_bitrates; j++) {
1778 if (sband->bitrates[j].bitrate == rate) {
1779 found = true;
1780 *mask |= BIT(j);
1781 break;
1782 }
1783 }
1784 if (!found)
1785 return -EINVAL;
1786 }
1787
1788 /*
1789 * mask must have at least one bit set here since we
1790 * didn't accept a 0-length rates array nor allowed
1791 * entries in the array that didn't exist
1792 */
1793
1794 return 0;
1795}
Johannes Berg11a2a352011-11-21 11:09:22 +01001796
Ilan Peerbdfbec22014-01-09 11:37:23 +02001797unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1798{
Johannes Berg57fbcce2016-04-12 15:56:15 +02001799 enum nl80211_band band;
Ilan Peerbdfbec22014-01-09 11:37:23 +02001800 unsigned int n_channels = 0;
1801
Johannes Berg57fbcce2016-04-12 15:56:15 +02001802 for (band = 0; band < NUM_NL80211_BANDS; band++)
Ilan Peerbdfbec22014-01-09 11:37:23 +02001803 if (wiphy->bands[band])
1804 n_channels += wiphy->bands[band]->n_channels;
1805
1806 return n_channels;
1807}
1808EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1809
Antonio Quartulli74063532014-05-19 21:53:21 +02001810int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1811 struct station_info *sinfo)
1812{
1813 struct cfg80211_registered_device *rdev;
1814 struct wireless_dev *wdev;
1815
1816 wdev = dev->ieee80211_ptr;
1817 if (!wdev)
1818 return -EOPNOTSUPP;
1819
1820 rdev = wiphy_to_rdev(wdev->wiphy);
1821 if (!rdev->ops->get_station)
1822 return -EOPNOTSUPP;
1823
1824 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1825}
1826EXPORT_SYMBOL(cfg80211_get_station);
1827
Ayala Bekera442b762016-09-20 17:31:15 +03001828void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1829{
1830 int i;
1831
1832 if (!f)
1833 return;
1834
1835 kfree(f->serv_spec_info);
1836 kfree(f->srf_bf);
1837 kfree(f->srf_macs);
1838 for (i = 0; i < f->num_rx_filters; i++)
1839 kfree(f->rx_filters[i].filter);
1840
1841 for (i = 0; i < f->num_tx_filters; i++)
1842 kfree(f->tx_filters[i].filter);
1843
1844 kfree(f->rx_filters);
1845 kfree(f->tx_filters);
1846 kfree(f);
1847}
1848EXPORT_SYMBOL(cfg80211_free_nan_func);
1849
Rafał Miłecki4787cfa2017-01-04 18:58:30 +01001850bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
1851 u32 center_freq_khz, u32 bw_khz)
1852{
1853 u32 start_freq_khz, end_freq_khz;
1854
1855 start_freq_khz = center_freq_khz - (bw_khz / 2);
1856 end_freq_khz = center_freq_khz + (bw_khz / 2);
1857
1858 if (start_freq_khz >= freq_range->start_freq_khz &&
1859 end_freq_khz <= freq_range->end_freq_khz)
1860 return true;
1861
1862 return false;
1863}
1864
Johannes Berg11a2a352011-11-21 11:09:22 +01001865/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1866/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1867const unsigned char rfc1042_header[] __aligned(2) =
1868 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1869EXPORT_SYMBOL(rfc1042_header);
1870
1871/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1872const unsigned char bridge_tunnel_header[] __aligned(2) =
1873 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1874EXPORT_SYMBOL(bridge_tunnel_header);