blob: 0962f107f57f831d625f59c3e505e70e30d72b96 [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 Berg8318d782008-01-24 19:38:38 +01005 */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -04006#include <linux/export.h>
Johannes Bergd3236552009-04-20 14:31:42 +02007#include <linux/bitops.h>
Zhu Yie31a16d2009-05-21 21:47:03 +08008#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Johannes Bergd3236552009-04-20 14:31:42 +020010#include <net/cfg80211.h>
Zhu Yie31a16d2009-05-21 21:47:03 +080011#include <net/ip.h>
Dave Tähtb1565792011-12-22 12:55:08 -080012#include <net/dsfield.h>
Johannes Berg8318d782008-01-24 19:38:38 +010013#include "core.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030014#include "rdev-ops.h"
15
Johannes Berg8318d782008-01-24 19:38:38 +010016
Johannes Bergbd815252008-10-29 20:00:45 +010017struct ieee80211_rate *
18ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
Johannes Berg881d9482009-01-21 15:13:48 +010019 u32 basic_rates, int bitrate)
Johannes Bergbd815252008-10-29 20:00:45 +010020{
21 struct ieee80211_rate *result = &sband->bitrates[0];
22 int i;
23
24 for (i = 0; i < sband->n_bitrates; i++) {
25 if (!(basic_rates & BIT(i)))
26 continue;
27 if (sband->bitrates[i].bitrate > bitrate)
28 continue;
29 result = &sband->bitrates[i];
30 }
31
32 return result;
33}
34EXPORT_SYMBOL(ieee80211_get_response_rate);
35
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070036u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband)
37{
38 struct ieee80211_rate *bitrates;
39 u32 mandatory_rates = 0;
40 enum ieee80211_rate_flags mandatory_flag;
41 int i;
42
43 if (WARN_ON(!sband))
44 return 1;
45
46 if (sband->band == IEEE80211_BAND_2GHZ)
47 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
48 else
49 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
50
51 bitrates = sband->bitrates;
52 for (i = 0; i < sband->n_bitrates; i++)
53 if (bitrates[i].flags & mandatory_flag)
54 mandatory_rates |= BIT(i);
55 return mandatory_rates;
56}
57EXPORT_SYMBOL(ieee80211_mandatory_rates);
58
Bruno Randolf59eb21a2011-01-17 13:37:28 +090059int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +010060{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090061 /* see 802.11 17.3.8.3.2 and Annex J
62 * there are overlapping channel numbers in 5GHz and 2GHz bands */
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030063 if (chan <= 0)
64 return 0; /* not supported */
65 switch (band) {
66 case IEEE80211_BAND_2GHZ:
Bruno Randolf59eb21a2011-01-17 13:37:28 +090067 if (chan == 14)
68 return 2484;
69 else if (chan < 14)
70 return 2407 + chan * 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030071 break;
72 case IEEE80211_BAND_5GHZ:
73 if (chan >= 182 && chan <= 196)
74 return 4000 + chan * 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090075 else
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030076 return 5000 + chan * 5;
77 break;
78 case IEEE80211_BAND_60GHZ:
79 if (chan < 5)
80 return 56160 + chan * 2160;
81 break;
82 default:
83 ;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090084 }
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030085 return 0; /* not supported */
Johannes Berg8318d782008-01-24 19:38:38 +010086}
87EXPORT_SYMBOL(ieee80211_channel_to_frequency);
88
89int ieee80211_frequency_to_channel(int freq)
90{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090091 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +010092 if (freq == 2484)
93 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090094 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +010095 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090096 else if (freq >= 4910 && freq <= 4980)
97 return (freq - 4000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030098 else if (freq <= 45000) /* DMG band lower limit */
Bruno Randolf59eb21a2011-01-17 13:37:28 +090099 return (freq - 5000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300100 else if (freq >= 58320 && freq <= 64800)
101 return (freq - 56160) / 2160;
102 else
103 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100104}
105EXPORT_SYMBOL(ieee80211_frequency_to_channel);
106
Johannes Berg6c507cd2008-03-26 14:14:55 +0100107struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
108 int freq)
Johannes Berg906c7302008-03-16 18:34:33 +0100109{
110 enum ieee80211_band band;
111 struct ieee80211_supported_band *sband;
112 int i;
113
114 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
115 sband = wiphy->bands[band];
116
117 if (!sband)
118 continue;
119
120 for (i = 0; i < sband->n_channels; i++) {
121 if (sband->channels[i].center_freq == freq)
122 return &sband->channels[i];
123 }
124 }
125
126 return NULL;
127}
Johannes Berg6c507cd2008-03-26 14:14:55 +0100128EXPORT_SYMBOL(__ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +0100129
Johannes Berg8318d782008-01-24 19:38:38 +0100130static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
131 enum ieee80211_band band)
132{
133 int i, want;
134
135 switch (band) {
136 case IEEE80211_BAND_5GHZ:
137 want = 3;
138 for (i = 0; i < sband->n_bitrates; i++) {
139 if (sband->bitrates[i].bitrate == 60 ||
140 sband->bitrates[i].bitrate == 120 ||
141 sband->bitrates[i].bitrate == 240) {
142 sband->bitrates[i].flags |=
143 IEEE80211_RATE_MANDATORY_A;
144 want--;
145 }
146 }
147 WARN_ON(want);
148 break;
149 case IEEE80211_BAND_2GHZ:
150 want = 7;
151 for (i = 0; i < sband->n_bitrates; i++) {
152 if (sband->bitrates[i].bitrate == 10) {
153 sband->bitrates[i].flags |=
154 IEEE80211_RATE_MANDATORY_B |
155 IEEE80211_RATE_MANDATORY_G;
156 want--;
157 }
158
159 if (sband->bitrates[i].bitrate == 20 ||
160 sband->bitrates[i].bitrate == 55 ||
161 sband->bitrates[i].bitrate == 110 ||
162 sband->bitrates[i].bitrate == 60 ||
163 sband->bitrates[i].bitrate == 120 ||
164 sband->bitrates[i].bitrate == 240) {
165 sband->bitrates[i].flags |=
166 IEEE80211_RATE_MANDATORY_G;
167 want--;
168 }
169
Johannes Bergaac09fb2008-01-30 17:36:10 +0100170 if (sband->bitrates[i].bitrate != 10 &&
171 sband->bitrates[i].bitrate != 20 &&
172 sband->bitrates[i].bitrate != 55 &&
173 sband->bitrates[i].bitrate != 110)
Johannes Berg8318d782008-01-24 19:38:38 +0100174 sband->bitrates[i].flags |=
175 IEEE80211_RATE_ERP_G;
176 }
Ivo van Doorn406f2382008-02-02 23:53:10 +0100177 WARN_ON(want != 0 && want != 3 && want != 6);
Johannes Berg8318d782008-01-24 19:38:38 +0100178 break;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300179 case IEEE80211_BAND_60GHZ:
180 /* check for mandatory HT MCS 1..4 */
181 WARN_ON(!sband->ht_cap.ht_supported);
182 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
183 break;
Johannes Berg8318d782008-01-24 19:38:38 +0100184 case IEEE80211_NUM_BANDS:
185 WARN_ON(1);
186 break;
187 }
188}
189
190void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
191{
192 enum ieee80211_band band;
193
194 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
195 if (wiphy->bands[band])
196 set_mandatory_flags_band(wiphy->bands[band], band);
197}
Johannes Berg08645122009-05-11 13:54:58 +0200198
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300199bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
200{
201 int i;
202 for (i = 0; i < wiphy->n_cipher_suites; i++)
203 if (cipher == wiphy->cipher_suites[i])
204 return true;
205 return false;
206}
207
Johannes Bergfffd0932009-07-08 14:22:54 +0200208int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
209 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200210 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200211{
212 if (key_idx > 5)
213 return -EINVAL;
214
Johannes Berge31b8212010-10-05 19:39:30 +0200215 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
216 return -EINVAL;
217
218 if (pairwise && !mac_addr)
219 return -EINVAL;
220
Johannes Berg08645122009-05-11 13:54:58 +0200221 /*
222 * Disallow pairwise keys with non-zero index unless it's WEP
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200223 * or a vendor specific cipher (because current deployments use
224 * pairwise WEP keys with non-zero indices and for vendor specific
225 * ciphers this should be validated in the driver or hardware level
226 * - but 802.11i clearly specifies to use zero)
Johannes Berg08645122009-05-11 13:54:58 +0200227 */
Johannes Berge31b8212010-10-05 19:39:30 +0200228 if (pairwise && key_idx &&
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200229 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
230 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
231 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
Johannes Berg08645122009-05-11 13:54:58 +0200232 return -EINVAL;
233
Johannes Berg08645122009-05-11 13:54:58 +0200234 switch (params->cipher) {
235 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200236 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200237 return -EINVAL;
238 break;
239 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200240 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200241 return -EINVAL;
242 break;
243 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200244 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200245 return -EINVAL;
246 break;
247 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200248 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200249 return -EINVAL;
250 break;
251 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200252 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200253 return -EINVAL;
254 break;
255 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300256 /*
257 * We don't know anything about this algorithm,
258 * allow using it -- but the driver must check
259 * all parameters! We still check below whether
260 * or not the driver supports this algorithm,
261 * of course.
262 */
263 break;
Johannes Berg08645122009-05-11 13:54:58 +0200264 }
265
Jouni Malinen9f26a952009-05-15 12:38:32 +0300266 if (params->seq) {
267 switch (params->cipher) {
268 case WLAN_CIPHER_SUITE_WEP40:
269 case WLAN_CIPHER_SUITE_WEP104:
270 /* These ciphers do not use key sequence */
271 return -EINVAL;
272 case WLAN_CIPHER_SUITE_TKIP:
273 case WLAN_CIPHER_SUITE_CCMP:
274 case WLAN_CIPHER_SUITE_AES_CMAC:
275 if (params->seq_len != 6)
276 return -EINVAL;
277 break;
278 }
279 }
280
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300281 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200282 return -EINVAL;
283
Johannes Berg08645122009-05-11 13:54:58 +0200284 return 0;
285}
Zhu Yie31a16d2009-05-21 21:47:03 +0800286
Johannes Berg633adf12010-08-12 14:49:58 +0200287unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800288{
289 unsigned int hdrlen = 24;
290
291 if (ieee80211_is_data(fc)) {
292 if (ieee80211_has_a4(fc))
293 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200294 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800295 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200296 if (ieee80211_has_order(fc))
297 hdrlen += IEEE80211_HT_CTL_LEN;
298 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800299 goto out;
300 }
301
302 if (ieee80211_is_ctl(fc)) {
303 /*
304 * ACK and CTS are 10 bytes, all others 16. To see how
305 * to get this condition consider
306 * subtype mask: 0b0000000011110000 (0x00F0)
307 * ACK subtype: 0b0000000011010000 (0x00D0)
308 * CTS subtype: 0b0000000011000000 (0x00C0)
309 * bits that matter: ^^^ (0x00E0)
310 * value of those: 0b0000000011000000 (0x00C0)
311 */
312 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
313 hdrlen = 10;
314 else
315 hdrlen = 16;
316 }
317out:
318 return hdrlen;
319}
320EXPORT_SYMBOL(ieee80211_hdrlen);
321
322unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
323{
324 const struct ieee80211_hdr *hdr =
325 (const struct ieee80211_hdr *)skb->data;
326 unsigned int hdrlen;
327
328 if (unlikely(skb->len < 10))
329 return 0;
330 hdrlen = ieee80211_hdrlen(hdr->frame_control);
331 if (unlikely(hdrlen > skb->len))
332 return 0;
333 return hdrlen;
334}
335EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
336
Johannes Berg9b395bc2012-10-26 00:36:40 +0200337unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800338{
339 int ae = meshhdr->flags & MESH_FLAGS_AE;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200340 /* 802.11-2012, 8.2.4.7.3 */
Zhu Yie31a16d2009-05-21 21:47:03 +0800341 switch (ae) {
Johannes Berg7dd111e2012-10-25 21:51:59 +0200342 default:
Zhu Yie31a16d2009-05-21 21:47:03 +0800343 case 0:
344 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700345 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800346 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700347 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800348 return 18;
Zhu Yie31a16d2009-05-21 21:47:03 +0800349 }
350}
Johannes Berg9b395bc2012-10-26 00:36:40 +0200351EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800352
Zhu Yieaf85ca2009-12-01 10:18:37 +0800353int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800354 enum nl80211_iftype iftype)
355{
356 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
357 u16 hdrlen, ethertype;
358 u8 *payload;
359 u8 dst[ETH_ALEN];
360 u8 src[ETH_ALEN] __aligned(2);
361
362 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
363 return -1;
364
365 hdrlen = ieee80211_hdrlen(hdr->frame_control);
366
367 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
368 * header
369 * IEEE 802.11 address fields:
370 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
371 * 0 0 DA SA BSSID n/a
372 * 0 1 DA BSSID SA n/a
373 * 1 0 BSSID SA DA n/a
374 * 1 1 RA TA DA SA
375 */
376 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
377 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
378
379 switch (hdr->frame_control &
380 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
381 case cpu_to_le16(IEEE80211_FCTL_TODS):
382 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200383 iftype != NL80211_IFTYPE_AP_VLAN &&
384 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800385 return -1;
386 break;
387 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
388 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543ee2009-11-10 20:10:05 +0100389 iftype != NL80211_IFTYPE_MESH_POINT &&
390 iftype != NL80211_IFTYPE_AP_VLAN &&
391 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800392 return -1;
393 if (iftype == NL80211_IFTYPE_MESH_POINT) {
394 struct ieee80211s_hdr *meshdr =
395 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800396 /* make sure meshdr->flags is on the linear part */
397 if (!pskb_may_pull(skb, hdrlen + 1))
398 return -1;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200399 if (meshdr->flags & MESH_FLAGS_AE_A4)
400 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800401 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800402 skb_copy_bits(skb, hdrlen +
403 offsetof(struct ieee80211s_hdr, eaddr1),
404 dst, ETH_ALEN);
405 skb_copy_bits(skb, hdrlen +
406 offsetof(struct ieee80211s_hdr, eaddr2),
407 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800408 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800409 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800410 }
411 break;
412 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700413 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200414 iftype != NL80211_IFTYPE_P2P_CLIENT &&
415 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800416 (is_multicast_ether_addr(dst) &&
Joe Perches4c764722012-05-08 18:56:56 +0000417 ether_addr_equal(src, addr)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800418 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700419 if (iftype == NL80211_IFTYPE_MESH_POINT) {
420 struct ieee80211s_hdr *meshdr =
421 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800422 /* make sure meshdr->flags is on the linear part */
423 if (!pskb_may_pull(skb, hdrlen + 1))
424 return -1;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200425 if (meshdr->flags & MESH_FLAGS_AE_A5_A6)
426 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700427 if (meshdr->flags & MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800428 skb_copy_bits(skb, hdrlen +
429 offsetof(struct ieee80211s_hdr, eaddr1),
430 src, ETH_ALEN);
431 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700432 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800433 break;
434 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300435 if (iftype != NL80211_IFTYPE_ADHOC &&
436 iftype != NL80211_IFTYPE_STATION)
437 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800438 break;
439 }
440
Zhu Yie3cf8b32010-03-29 17:35:07 +0800441 if (!pskb_may_pull(skb, hdrlen + 8))
Zhu Yie31a16d2009-05-21 21:47:03 +0800442 return -1;
443
444 payload = skb->data + hdrlen;
445 ethertype = (payload[6] << 8) | payload[7];
446
Joe Perches4c764722012-05-08 18:56:56 +0000447 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yie31a16d2009-05-21 21:47:03 +0800448 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perches4c764722012-05-08 18:56:56 +0000449 ether_addr_equal(payload, bridge_tunnel_header))) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800450 /* remove RFC1042 or Bridge-Tunnel encapsulation and
451 * replace EtherType */
452 skb_pull(skb, hdrlen + 6);
453 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
454 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
455 } else {
456 struct ethhdr *ehdr;
457 __be16 len;
458
459 skb_pull(skb, hdrlen);
460 len = htons(skb->len);
461 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
462 memcpy(ehdr->h_dest, dst, ETH_ALEN);
463 memcpy(ehdr->h_source, src, ETH_ALEN);
464 ehdr->h_proto = len;
465 }
466 return 0;
467}
468EXPORT_SYMBOL(ieee80211_data_to_8023);
469
Zhu Yieaf85ca2009-12-01 10:18:37 +0800470int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800471 enum nl80211_iftype iftype, u8 *bssid, bool qos)
472{
473 struct ieee80211_hdr hdr;
474 u16 hdrlen, ethertype;
475 __le16 fc;
476 const u8 *encaps_data;
477 int encaps_len, skip_header_bytes;
478 int nh_pos, h_pos;
479 int head_need;
480
481 if (unlikely(skb->len < ETH_HLEN))
482 return -EINVAL;
483
484 nh_pos = skb_network_header(skb) - skb->data;
485 h_pos = skb_transport_header(skb) - skb->data;
486
487 /* convert Ethernet header to proper 802.11 header (based on
488 * operation mode) */
489 ethertype = (skb->data[12] << 8) | skb->data[13];
490 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
491
492 switch (iftype) {
493 case NL80211_IFTYPE_AP:
494 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200495 case NL80211_IFTYPE_P2P_GO:
Zhu Yie31a16d2009-05-21 21:47:03 +0800496 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
497 /* DA BSSID SA */
498 memcpy(hdr.addr1, skb->data, ETH_ALEN);
499 memcpy(hdr.addr2, addr, ETH_ALEN);
500 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
501 hdrlen = 24;
502 break;
503 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200504 case NL80211_IFTYPE_P2P_CLIENT:
Zhu Yie31a16d2009-05-21 21:47:03 +0800505 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
506 /* BSSID SA DA */
507 memcpy(hdr.addr1, bssid, ETH_ALEN);
508 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
509 memcpy(hdr.addr3, skb->data, ETH_ALEN);
510 hdrlen = 24;
511 break;
512 case NL80211_IFTYPE_ADHOC:
513 /* DA SA BSSID */
514 memcpy(hdr.addr1, skb->data, ETH_ALEN);
515 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
516 memcpy(hdr.addr3, bssid, ETH_ALEN);
517 hdrlen = 24;
518 break;
519 default:
520 return -EOPNOTSUPP;
521 }
522
523 if (qos) {
524 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
525 hdrlen += 2;
526 }
527
528 hdr.frame_control = fc;
529 hdr.duration_id = 0;
530 hdr.seq_ctrl = 0;
531
532 skip_header_bytes = ETH_HLEN;
533 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
534 encaps_data = bridge_tunnel_header;
535 encaps_len = sizeof(bridge_tunnel_header);
536 skip_header_bytes -= 2;
Simon Hormane5c5d222013-03-28 13:38:25 +0900537 } else if (ethertype >= ETH_P_802_3_MIN) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800538 encaps_data = rfc1042_header;
539 encaps_len = sizeof(rfc1042_header);
540 skip_header_bytes -= 2;
541 } else {
542 encaps_data = NULL;
543 encaps_len = 0;
544 }
545
546 skb_pull(skb, skip_header_bytes);
547 nh_pos -= skip_header_bytes;
548 h_pos -= skip_header_bytes;
549
550 head_need = hdrlen + encaps_len - skb_headroom(skb);
551
552 if (head_need > 0 || skb_cloned(skb)) {
553 head_need = max(head_need, 0);
554 if (head_need)
555 skb_orphan(skb);
556
Joe Perches24616152011-08-29 14:17:41 -0700557 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
Zhu Yie31a16d2009-05-21 21:47:03 +0800558 return -ENOMEM;
Joe Perches24616152011-08-29 14:17:41 -0700559
Zhu Yie31a16d2009-05-21 21:47:03 +0800560 skb->truesize += head_need;
561 }
562
563 if (encaps_data) {
564 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
565 nh_pos += encaps_len;
566 h_pos += encaps_len;
567 }
568
569 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
570
571 nh_pos += hdrlen;
572 h_pos += hdrlen;
573
574 /* Update skb pointers to various headers since this modified frame
575 * is going to go through Linux networking code that may potentially
576 * need things like pointer to IP header. */
577 skb_set_mac_header(skb, 0);
578 skb_set_network_header(skb, nh_pos);
579 skb_set_transport_header(skb, h_pos);
580
581 return 0;
582}
583EXPORT_SYMBOL(ieee80211_data_from_8023);
584
Zhu Yieaf85ca2009-12-01 10:18:37 +0800585
586void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
587 const u8 *addr, enum nl80211_iftype iftype,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700588 const unsigned int extra_headroom,
589 bool has_80211_header)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800590{
591 struct sk_buff *frame = NULL;
592 u16 ethertype;
593 u8 *payload;
594 const struct ethhdr *eth;
595 int remaining, err;
596 u8 dst[ETH_ALEN], src[ETH_ALEN];
597
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700598 if (has_80211_header) {
599 err = ieee80211_data_to_8023(skb, addr, iftype);
600 if (err)
601 goto out;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800602
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700603 /* skip the wrapping header */
604 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
605 if (!eth)
606 goto out;
607 } else {
608 eth = (struct ethhdr *) skb->data;
609 }
Zhu Yieaf85ca2009-12-01 10:18:37 +0800610
611 while (skb != frame) {
612 u8 padding;
613 __be16 len = eth->h_proto;
614 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
615
616 remaining = skb->len;
617 memcpy(dst, eth->h_dest, ETH_ALEN);
618 memcpy(src, eth->h_source, ETH_ALEN);
619
620 padding = (4 - subframe_len) & 0x3;
621 /* the last MSDU has no padding */
622 if (subframe_len > remaining)
623 goto purge;
624
625 skb_pull(skb, sizeof(struct ethhdr));
626 /* reuse skb for the last subframe */
627 if (remaining <= subframe_len + padding)
628 frame = skb;
629 else {
630 unsigned int hlen = ALIGN(extra_headroom, 4);
631 /*
632 * Allocate and reserve two bytes more for payload
633 * alignment since sizeof(struct ethhdr) is 14.
634 */
635 frame = dev_alloc_skb(hlen + subframe_len + 2);
636 if (!frame)
637 goto purge;
638
639 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
640 memcpy(skb_put(frame, ntohs(len)), skb->data,
641 ntohs(len));
642
643 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
644 padding);
645 if (!eth) {
646 dev_kfree_skb(frame);
647 goto purge;
648 }
649 }
650
651 skb_reset_network_header(frame);
652 frame->dev = skb->dev;
653 frame->priority = skb->priority;
654
655 payload = frame->data;
656 ethertype = (payload[6] << 8) | payload[7];
657
Joe Perchesac422d32012-05-08 18:56:55 +0000658 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yieaf85ca2009-12-01 10:18:37 +0800659 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perchesac422d32012-05-08 18:56:55 +0000660 ether_addr_equal(payload, bridge_tunnel_header))) {
Zhu Yieaf85ca2009-12-01 10:18:37 +0800661 /* remove RFC1042 or Bridge-Tunnel
662 * encapsulation and replace EtherType */
663 skb_pull(frame, 6);
664 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
665 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
666 } else {
667 memcpy(skb_push(frame, sizeof(__be16)), &len,
668 sizeof(__be16));
669 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
670 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
671 }
672 __skb_queue_tail(list, frame);
673 }
674
675 return;
676
677 purge:
678 __skb_queue_purge(list);
679 out:
680 dev_kfree_skb(skb);
681}
682EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
683
Zhu Yie31a16d2009-05-21 21:47:03 +0800684/* Given a data frame determine the 802.1p/1d tag to use. */
685unsigned int cfg80211_classify8021d(struct sk_buff *skb)
686{
687 unsigned int dscp;
688
689 /* skb->priority values from 256->263 are magic values to
690 * directly indicate a specific 802.1d priority. This is used
691 * to allow 802.1d priority to be passed directly in from VLAN
692 * tags, etc.
693 */
694 if (skb->priority >= 256 && skb->priority <= 263)
695 return skb->priority - 256;
696
697 switch (skb->protocol) {
698 case htons(ETH_P_IP):
Dave Tähtb1565792011-12-22 12:55:08 -0800699 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
700 break;
701 case htons(ETH_P_IPV6):
702 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
Zhu Yie31a16d2009-05-21 21:47:03 +0800703 break;
704 default:
705 return 0;
706 }
707
708 return dscp >> 5;
709}
710EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200711
712const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
713{
Johannes Berg9caf0362012-11-29 01:25:20 +0100714 const struct cfg80211_bss_ies *ies;
715
716 ies = rcu_dereference(bss->ies);
717 if (!ies)
Johannes Berg517357c2009-07-02 17:18:40 +0200718 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +0100719
720 return cfg80211_find_ie(ie, ies->data, ies->len);
Johannes Berg517357c2009-07-02 17:18:40 +0200721}
722EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200723
724void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
725{
726 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
727 struct net_device *dev = wdev->netdev;
728 int i;
729
730 if (!wdev->connect_keys)
731 return;
732
733 for (i = 0; i < 6; i++) {
734 if (!wdev->connect_keys->params[i].cipher)
735 continue;
Hila Gonene35e4d22012-06-27 17:19:42 +0300736 if (rdev_add_key(rdev, dev, i, false, NULL,
737 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800738 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800739 continue;
740 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200741 if (wdev->connect_keys->def == i)
Hila Gonene35e4d22012-06-27 17:19:42 +0300742 if (rdev_set_default_key(rdev, dev, i, true, true)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800743 netdev_err(dev, "failed to set defkey %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800744 continue;
745 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200746 if (wdev->connect_keys->defmgmt == i)
Hila Gonene35e4d22012-06-27 17:19:42 +0300747 if (rdev_set_default_mgmt_key(rdev, dev, i))
Joe Perchese9c02682010-11-16 19:56:49 -0800748 netdev_err(dev, "failed to set mgtdef %d\n", i);
Johannes Bergfffd0932009-07-08 14:22:54 +0200749 }
750
751 kfree(wdev->connect_keys);
752 wdev->connect_keys = NULL;
753}
Johannes Berg3d54d252009-08-21 14:51:05 +0200754
Daniel Drake1f6fc432012-08-02 18:41:48 +0100755void cfg80211_process_wdev_events(struct wireless_dev *wdev)
Johannes Berg3d54d252009-08-21 14:51:05 +0200756{
757 struct cfg80211_event *ev;
758 unsigned long flags;
759 const u8 *bssid = NULL;
760
761 spin_lock_irqsave(&wdev->event_lock, flags);
762 while (!list_empty(&wdev->event_list)) {
763 ev = list_first_entry(&wdev->event_list,
764 struct cfg80211_event, list);
765 list_del(&ev->list);
766 spin_unlock_irqrestore(&wdev->event_lock, flags);
767
768 wdev_lock(wdev);
769 switch (ev->type) {
770 case EVENT_CONNECT_RESULT:
771 if (!is_zero_ether_addr(ev->cr.bssid))
772 bssid = ev->cr.bssid;
773 __cfg80211_connect_result(
774 wdev->netdev, bssid,
775 ev->cr.req_ie, ev->cr.req_ie_len,
776 ev->cr.resp_ie, ev->cr.resp_ie_len,
777 ev->cr.status,
778 ev->cr.status == WLAN_STATUS_SUCCESS,
779 NULL);
780 break;
781 case EVENT_ROAMED:
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530782 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
783 ev->rm.req_ie_len, ev->rm.resp_ie,
784 ev->rm.resp_ie_len);
Johannes Berg3d54d252009-08-21 14:51:05 +0200785 break;
786 case EVENT_DISCONNECTED:
787 __cfg80211_disconnected(wdev->netdev,
788 ev->dc.ie, ev->dc.ie_len,
789 ev->dc.reason, true);
790 break;
791 case EVENT_IBSS_JOINED:
792 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
793 break;
794 }
795 wdev_unlock(wdev);
796
797 kfree(ev);
798
799 spin_lock_irqsave(&wdev->event_lock, flags);
800 }
801 spin_unlock_irqrestore(&wdev->event_lock, flags);
802}
803
804void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
805{
806 struct wireless_dev *wdev;
807
808 ASSERT_RTNL();
809 ASSERT_RDEV_LOCK(rdev);
810
811 mutex_lock(&rdev->devlist_mtx);
812
Johannes Berg89a54e42012-06-15 14:33:17 +0200813 list_for_each_entry(wdev, &rdev->wdev_list, list)
Johannes Berg3d54d252009-08-21 14:51:05 +0200814 cfg80211_process_wdev_events(wdev);
815
816 mutex_unlock(&rdev->devlist_mtx);
817}
818
819int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
820 struct net_device *dev, enum nl80211_iftype ntype,
821 u32 *flags, struct vif_params *params)
822{
823 int err;
824 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
825
826 ASSERT_RDEV_LOCK(rdev);
827
828 /* don't support changing VLANs, you just re-create them */
829 if (otype == NL80211_IFTYPE_AP_VLAN)
830 return -EOPNOTSUPP;
831
Johannes Berg98104fde2012-06-16 00:19:54 +0200832 /* cannot change into P2P device type */
833 if (ntype == NL80211_IFTYPE_P2P_DEVICE)
834 return -EOPNOTSUPP;
835
Johannes Berg3d54d252009-08-21 14:51:05 +0200836 if (!rdev->ops->change_virtual_intf ||
837 !(rdev->wiphy.interface_modes & (1 << ntype)))
838 return -EOPNOTSUPP;
839
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100840 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000841 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200842 (ntype == NL80211_IFTYPE_ADHOC ||
843 ntype == NL80211_IFTYPE_STATION ||
844 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100845 return -EBUSY;
846
Michal Kaziorf8cdddb2012-06-08 10:55:44 +0200847 if (ntype != otype && netif_running(dev)) {
Michal Kaziore4e32452012-06-29 12:47:08 +0200848 mutex_lock(&rdev->devlist_mtx);
Johannes Berg7527a782011-05-13 10:58:57 +0200849 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
850 ntype);
Michal Kaziore4e32452012-06-29 12:47:08 +0200851 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg7527a782011-05-13 10:58:57 +0200852 if (err)
853 return err;
854
Johannes Berg9bc383d2009-11-19 11:55:19 +0100855 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100856 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg9bc383d2009-11-19 11:55:19 +0100857
Johannes Berg3d54d252009-08-21 14:51:05 +0200858 switch (otype) {
Michal Kaziorac800142012-06-29 12:46:57 +0200859 case NL80211_IFTYPE_AP:
860 cfg80211_stop_ap(rdev, dev);
861 break;
Johannes Berg3d54d252009-08-21 14:51:05 +0200862 case NL80211_IFTYPE_ADHOC:
863 cfg80211_leave_ibss(rdev, dev, false);
864 break;
865 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200866 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200867 cfg80211_disconnect(rdev, dev,
868 WLAN_REASON_DEAUTH_LEAVING, true);
869 break;
870 case NL80211_IFTYPE_MESH_POINT:
871 /* mesh should be handled? */
872 break;
873 default:
874 break;
875 }
876
877 cfg80211_process_rdev_events(rdev);
878 }
879
Hila Gonene35e4d22012-06-27 17:19:42 +0300880 err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
Johannes Berg3d54d252009-08-21 14:51:05 +0200881
882 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
883
Johannes Berg9bc383d2009-11-19 11:55:19 +0100884 if (!err && params && params->use_4addr != -1)
885 dev->ieee80211_ptr->use_4addr = params->use_4addr;
886
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100887 if (!err) {
888 dev->priv_flags &= ~IFF_DONT_BRIDGE;
889 switch (ntype) {
890 case NL80211_IFTYPE_STATION:
891 if (dev->ieee80211_ptr->use_4addr)
892 break;
893 /* fall through */
Johannes Berg074ac8d2010-09-16 14:58:22 +0200894 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100895 case NL80211_IFTYPE_ADHOC:
896 dev->priv_flags |= IFF_DONT_BRIDGE;
897 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200898 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100899 case NL80211_IFTYPE_AP:
900 case NL80211_IFTYPE_AP_VLAN:
901 case NL80211_IFTYPE_WDS:
902 case NL80211_IFTYPE_MESH_POINT:
903 /* bridging OK */
904 break;
905 case NL80211_IFTYPE_MONITOR:
906 /* monitor can't bridge anyway */
907 break;
908 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200909 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100910 /* not happening */
911 break;
Johannes Berg98104fde2012-06-16 00:19:54 +0200912 case NL80211_IFTYPE_P2P_DEVICE:
913 WARN_ON(1);
914 break;
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100915 }
916 }
917
Michal Kaziordbbae262012-06-29 12:47:01 +0200918 if (!err && ntype != otype && netif_running(dev)) {
919 cfg80211_update_iface_num(rdev, ntype, 1);
920 cfg80211_update_iface_num(rdev, otype, -1);
921 }
922
Johannes Berg3d54d252009-08-21 14:51:05 +0200923 return err;
924}
John W. Linville254416a2009-12-09 16:43:52 -0500925
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +0300926static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
927{
928 static const u32 __mcs2bitrate[] = {
929 /* control PHY */
930 [0] = 275,
931 /* SC PHY */
932 [1] = 3850,
933 [2] = 7700,
934 [3] = 9625,
935 [4] = 11550,
936 [5] = 12512, /* 1251.25 mbps */
937 [6] = 15400,
938 [7] = 19250,
939 [8] = 23100,
940 [9] = 25025,
941 [10] = 30800,
942 [11] = 38500,
943 [12] = 46200,
944 /* OFDM PHY */
945 [13] = 6930,
946 [14] = 8662, /* 866.25 mbps */
947 [15] = 13860,
948 [16] = 17325,
949 [17] = 20790,
950 [18] = 27720,
951 [19] = 34650,
952 [20] = 41580,
953 [21] = 45045,
954 [22] = 51975,
955 [23] = 62370,
956 [24] = 67568, /* 6756.75 mbps */
957 /* LP-SC PHY */
958 [25] = 6260,
959 [26] = 8340,
960 [27] = 11120,
961 [28] = 12510,
962 [29] = 16680,
963 [30] = 22240,
964 [31] = 25030,
965 };
966
967 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
968 return 0;
969
970 return __mcs2bitrate[rate->mcs];
971}
972
Johannes Bergdb9c64c2012-11-09 14:56:41 +0100973static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
974{
975 static const u32 base[4][10] = {
976 { 6500000,
977 13000000,
978 19500000,
979 26000000,
980 39000000,
981 52000000,
982 58500000,
983 65000000,
984 78000000,
985 0,
986 },
987 { 13500000,
988 27000000,
989 40500000,
990 54000000,
991 81000000,
992 108000000,
993 121500000,
994 135000000,
995 162000000,
996 180000000,
997 },
998 { 29300000,
999 58500000,
1000 87800000,
1001 117000000,
1002 175500000,
1003 234000000,
1004 263300000,
1005 292500000,
1006 351000000,
1007 390000000,
1008 },
1009 { 58500000,
1010 117000000,
1011 175500000,
1012 234000000,
1013 351000000,
1014 468000000,
1015 526500000,
1016 585000000,
1017 702000000,
1018 780000000,
1019 },
1020 };
1021 u32 bitrate;
1022 int idx;
1023
1024 if (WARN_ON_ONCE(rate->mcs > 9))
1025 return 0;
1026
1027 idx = rate->flags & (RATE_INFO_FLAGS_160_MHZ_WIDTH |
1028 RATE_INFO_FLAGS_80P80_MHZ_WIDTH) ? 3 :
1029 rate->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH ? 2 :
1030 rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH ? 1 : 0;
1031
1032 bitrate = base[idx][rate->mcs];
1033 bitrate *= rate->nss;
1034
1035 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1036 bitrate = (bitrate / 9) * 10;
1037
1038 /* do NOT round down here */
1039 return (bitrate + 50000) / 100000;
1040}
1041
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03001042u32 cfg80211_calculate_bitrate(struct rate_info *rate)
John W. Linville254416a2009-12-09 16:43:52 -05001043{
1044 int modulation, streams, bitrate;
1045
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001046 if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
1047 !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
John W. Linville254416a2009-12-09 16:43:52 -05001048 return rate->legacy;
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +03001049 if (rate->flags & RATE_INFO_FLAGS_60G)
1050 return cfg80211_calculate_bitrate_60g(rate);
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001051 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1052 return cfg80211_calculate_bitrate_vht(rate);
John W. Linville254416a2009-12-09 16:43:52 -05001053
1054 /* the formula below does only work for MCS values smaller than 32 */
Johannes Berg2615f372012-05-08 21:36:20 +02001055 if (WARN_ON_ONCE(rate->mcs >= 32))
John W. Linville254416a2009-12-09 16:43:52 -05001056 return 0;
1057
1058 modulation = rate->mcs & 7;
1059 streams = (rate->mcs >> 3) + 1;
1060
1061 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1062 13500000 : 6500000;
1063
1064 if (modulation < 4)
1065 bitrate *= (modulation + 1);
1066 else if (modulation == 4)
1067 bitrate *= (modulation + 2);
1068 else
1069 bitrate *= (modulation + 3);
1070
1071 bitrate *= streams;
1072
1073 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1074 bitrate = (bitrate / 9) * 10;
1075
1076 /* do NOT round down here */
1077 return (bitrate + 50000) / 100000;
1078}
Thomas Pedersen8097e142012-03-05 15:31:47 -08001079EXPORT_SYMBOL(cfg80211_calculate_bitrate);
Johannes Berg56d18932011-05-09 18:41:15 +02001080
Arend van Sprielc216e642012-11-25 19:13:28 +01001081int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1082 enum ieee80211_p2p_attr_id attr,
1083 u8 *buf, unsigned int bufsize)
Johannes Berg0ee45352012-10-29 19:48:40 +01001084{
1085 u8 *out = buf;
1086 u16 attr_remaining = 0;
1087 bool desired_attr = false;
1088 u16 desired_len = 0;
1089
1090 while (len > 0) {
1091 unsigned int iedatalen;
1092 unsigned int copy;
1093 const u8 *iedata;
1094
1095 if (len < 2)
1096 return -EILSEQ;
1097 iedatalen = ies[1];
1098 if (iedatalen + 2 > len)
1099 return -EILSEQ;
1100
1101 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1102 goto cont;
1103
1104 if (iedatalen < 4)
1105 goto cont;
1106
1107 iedata = ies + 2;
1108
1109 /* check WFA OUI, P2P subtype */
1110 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1111 iedata[2] != 0x9a || iedata[3] != 0x09)
1112 goto cont;
1113
1114 iedatalen -= 4;
1115 iedata += 4;
1116
1117 /* check attribute continuation into this IE */
1118 copy = min_t(unsigned int, attr_remaining, iedatalen);
1119 if (copy && desired_attr) {
1120 desired_len += copy;
1121 if (out) {
1122 memcpy(out, iedata, min(bufsize, copy));
1123 out += min(bufsize, copy);
1124 bufsize -= min(bufsize, copy);
1125 }
1126
1127
1128 if (copy == attr_remaining)
1129 return desired_len;
1130 }
1131
1132 attr_remaining -= copy;
1133 if (attr_remaining)
1134 goto cont;
1135
1136 iedatalen -= copy;
1137 iedata += copy;
1138
1139 while (iedatalen > 0) {
1140 u16 attr_len;
1141
1142 /* P2P attribute ID & size must fit */
1143 if (iedatalen < 3)
1144 return -EILSEQ;
1145 desired_attr = iedata[0] == attr;
1146 attr_len = get_unaligned_le16(iedata + 1);
1147 iedatalen -= 3;
1148 iedata += 3;
1149
1150 copy = min_t(unsigned int, attr_len, iedatalen);
1151
1152 if (desired_attr) {
1153 desired_len += copy;
1154 if (out) {
1155 memcpy(out, iedata, min(bufsize, copy));
1156 out += min(bufsize, copy);
1157 bufsize -= min(bufsize, copy);
1158 }
1159
1160 if (copy == attr_len)
1161 return desired_len;
1162 }
1163
1164 iedata += copy;
1165 iedatalen -= copy;
1166 attr_remaining = attr_len - copy;
1167 }
1168
1169 cont:
1170 len -= ies[1] + 2;
1171 ies += ies[1] + 2;
1172 }
1173
1174 if (attr_remaining && desired_attr)
1175 return -EILSEQ;
1176
1177 return -ENOENT;
1178}
1179EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1180
Johannes Berg1ce3e822012-08-01 17:00:55 +02001181bool ieee80211_operating_class_to_band(u8 operating_class,
1182 enum ieee80211_band *band)
1183{
1184 switch (operating_class) {
1185 case 112:
1186 case 115 ... 127:
1187 *band = IEEE80211_BAND_5GHZ;
1188 return true;
1189 case 81:
1190 case 82:
1191 case 83:
1192 case 84:
1193 *band = IEEE80211_BAND_2GHZ;
1194 return true;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001195 case 180:
1196 *band = IEEE80211_BAND_60GHZ;
1197 return true;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001198 }
1199
1200 return false;
1201}
1202EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1203
Johannes Berg56d18932011-05-09 18:41:15 +02001204int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1205 u32 beacon_int)
1206{
1207 struct wireless_dev *wdev;
1208 int res = 0;
1209
1210 if (!beacon_int)
1211 return -EINVAL;
1212
1213 mutex_lock(&rdev->devlist_mtx);
1214
Johannes Berg89a54e42012-06-15 14:33:17 +02001215 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Berg56d18932011-05-09 18:41:15 +02001216 if (!wdev->beacon_interval)
1217 continue;
1218 if (wdev->beacon_interval != beacon_int) {
1219 res = -EINVAL;
1220 break;
1221 }
1222 }
1223
1224 mutex_unlock(&rdev->devlist_mtx);
1225
1226 return res;
1227}
Johannes Berg7527a782011-05-13 10:58:57 +02001228
Michal Kaziord4e50c52012-06-29 12:47:07 +02001229int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
1230 struct wireless_dev *wdev,
1231 enum nl80211_iftype iftype,
1232 struct ieee80211_channel *chan,
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001233 enum cfg80211_chan_mode chanmode,
1234 u8 radar_detect)
Johannes Berg7527a782011-05-13 10:58:57 +02001235{
1236 struct wireless_dev *wdev_iter;
Johannes Berg463454b2012-06-05 12:16:50 +02001237 u32 used_iftypes = BIT(iftype);
Johannes Berg7527a782011-05-13 10:58:57 +02001238 int num[NUM_NL80211_IFTYPES];
Michal Kaziord4e50c52012-06-29 12:47:07 +02001239 struct ieee80211_channel
1240 *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
1241 struct ieee80211_channel *ch;
1242 enum cfg80211_chan_mode chmode;
1243 int num_different_channels = 0;
Johannes Berg7527a782011-05-13 10:58:57 +02001244 int total = 1;
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001245 bool radar_required;
Johannes Berg7527a782011-05-13 10:58:57 +02001246 int i, j;
1247
1248 ASSERT_RTNL();
Michal Kaziore4e32452012-06-29 12:47:08 +02001249 lockdep_assert_held(&rdev->devlist_mtx);
Johannes Berg7527a782011-05-13 10:58:57 +02001250
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001251 if (WARN_ON(hweight32(radar_detect) > 1))
1252 return -EINVAL;
1253
1254 switch (iftype) {
1255 case NL80211_IFTYPE_ADHOC:
1256 case NL80211_IFTYPE_AP:
1257 case NL80211_IFTYPE_AP_VLAN:
1258 case NL80211_IFTYPE_MESH_POINT:
1259 case NL80211_IFTYPE_P2P_GO:
1260 case NL80211_IFTYPE_WDS:
Simon Wunderlich683d41a2013-01-23 15:15:57 +01001261 radar_required = !!(chan &&
1262 (chan->flags & IEEE80211_CHAN_RADAR));
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001263 break;
1264 case NL80211_IFTYPE_P2P_CLIENT:
1265 case NL80211_IFTYPE_STATION:
Ilan Peerbba87ff2013-02-03 08:28:27 +02001266 case NL80211_IFTYPE_P2P_DEVICE:
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001267 case NL80211_IFTYPE_MONITOR:
1268 radar_required = false;
1269 break;
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001270 case NUM_NL80211_IFTYPES:
1271 case NL80211_IFTYPE_UNSPECIFIED:
1272 default:
1273 return -EINVAL;
1274 }
1275
1276 if (radar_required && !radar_detect)
1277 return -EINVAL;
1278
Johannes Berg7527a782011-05-13 10:58:57 +02001279 /* Always allow software iftypes */
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001280 if (rdev->wiphy.software_iftypes & BIT(iftype)) {
1281 if (radar_detect)
1282 return -EINVAL;
Johannes Berg7527a782011-05-13 10:58:57 +02001283 return 0;
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001284 }
Johannes Berg7527a782011-05-13 10:58:57 +02001285
Johannes Berg7527a782011-05-13 10:58:57 +02001286 memset(num, 0, sizeof(num));
Michal Kaziord4e50c52012-06-29 12:47:07 +02001287 memset(used_channels, 0, sizeof(used_channels));
Johannes Berg7527a782011-05-13 10:58:57 +02001288
1289 num[iftype] = 1;
1290
Michal Kaziord4e50c52012-06-29 12:47:07 +02001291 switch (chanmode) {
1292 case CHAN_MODE_UNDEFINED:
1293 break;
1294 case CHAN_MODE_SHARED:
1295 WARN_ON(!chan);
1296 used_channels[0] = chan;
1297 num_different_channels++;
1298 break;
1299 case CHAN_MODE_EXCLUSIVE:
1300 num_different_channels++;
1301 break;
1302 }
1303
Johannes Berg89a54e42012-06-15 14:33:17 +02001304 list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
Johannes Berg7527a782011-05-13 10:58:57 +02001305 if (wdev_iter == wdev)
1306 continue;
Johannes Berg6e3ab552013-04-19 12:19:39 +02001307 if (wdev_iter->iftype == NL80211_IFTYPE_P2P_DEVICE) {
Johannes Berg98104fde2012-06-16 00:19:54 +02001308 if (!wdev_iter->p2p_started)
1309 continue;
Johannes Berg6e3ab552013-04-19 12:19:39 +02001310 } else if (wdev_iter->netdev) {
1311 if (!netif_running(wdev_iter->netdev))
1312 continue;
Johannes Berg98104fde2012-06-16 00:19:54 +02001313 } else {
1314 WARN_ON(1);
1315 }
Johannes Berg7527a782011-05-13 10:58:57 +02001316
1317 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
1318 continue;
1319
Johannes Berg8e95ea42012-07-10 19:39:02 +02001320 /*
1321 * We may be holding the "wdev" mutex, but now need to lock
1322 * wdev_iter. This is OK because once we get here wdev_iter
1323 * is not wdev (tested above), but we need to use the nested
1324 * locking for lockdep.
1325 */
1326 mutex_lock_nested(&wdev_iter->mtx, 1);
1327 __acquire(wdev_iter->mtx);
1328 cfg80211_get_chan_state(wdev_iter, &ch, &chmode);
1329 wdev_unlock(wdev_iter);
Michal Kaziord4e50c52012-06-29 12:47:07 +02001330
1331 switch (chmode) {
1332 case CHAN_MODE_UNDEFINED:
1333 break;
1334 case CHAN_MODE_SHARED:
1335 for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
1336 if (!used_channels[i] || used_channels[i] == ch)
1337 break;
1338
Michal Kaziore4e32452012-06-29 12:47:08 +02001339 if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
Michal Kaziord4e50c52012-06-29 12:47:07 +02001340 return -EBUSY;
Michal Kaziord4e50c52012-06-29 12:47:07 +02001341
1342 if (used_channels[i] == NULL) {
1343 used_channels[i] = ch;
1344 num_different_channels++;
1345 }
1346 break;
1347 case CHAN_MODE_EXCLUSIVE:
1348 num_different_channels++;
1349 break;
1350 }
1351
Johannes Berg7527a782011-05-13 10:58:57 +02001352 num[wdev_iter->iftype]++;
1353 total++;
Johannes Berg463454b2012-06-05 12:16:50 +02001354 used_iftypes |= BIT(wdev_iter->iftype);
Johannes Berg7527a782011-05-13 10:58:57 +02001355 }
Johannes Berg7527a782011-05-13 10:58:57 +02001356
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001357 if (total == 1 && !radar_detect)
Johannes Berg8e8b41f2012-03-15 10:16:16 +01001358 return 0;
1359
Johannes Berg7527a782011-05-13 10:58:57 +02001360 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1361 const struct ieee80211_iface_combination *c;
1362 struct ieee80211_iface_limit *limits;
Johannes Berg463454b2012-06-05 12:16:50 +02001363 u32 all_iftypes = 0;
Johannes Berg7527a782011-05-13 10:58:57 +02001364
1365 c = &rdev->wiphy.iface_combinations[i];
1366
Michal Kaziord4e50c52012-06-29 12:47:07 +02001367 if (total > c->max_interfaces)
1368 continue;
1369 if (num_different_channels > c->num_different_channels)
1370 continue;
1371
Johannes Berg7527a782011-05-13 10:58:57 +02001372 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1373 GFP_KERNEL);
1374 if (!limits)
1375 return -ENOMEM;
Johannes Berg7527a782011-05-13 10:58:57 +02001376
1377 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1378 if (rdev->wiphy.software_iftypes & BIT(iftype))
1379 continue;
1380 for (j = 0; j < c->n_limits; j++) {
Johannes Berg463454b2012-06-05 12:16:50 +02001381 all_iftypes |= limits[j].types;
Lukasz Kucharczyke55a4042012-04-11 14:55:10 +02001382 if (!(limits[j].types & BIT(iftype)))
Johannes Berg7527a782011-05-13 10:58:57 +02001383 continue;
1384 if (limits[j].max < num[iftype])
1385 goto cont;
1386 limits[j].max -= num[iftype];
1387 }
1388 }
Johannes Berg463454b2012-06-05 12:16:50 +02001389
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001390 if (radar_detect && !(c->radar_detect_widths & radar_detect))
1391 goto cont;
1392
Johannes Berg463454b2012-06-05 12:16:50 +02001393 /*
1394 * Finally check that all iftypes that we're currently
1395 * using are actually part of this combination. If they
1396 * aren't then we can't use this combination and have
1397 * to continue to the next.
1398 */
1399 if ((all_iftypes & used_iftypes) != used_iftypes)
1400 goto cont;
1401
1402 /*
1403 * This combination covered all interface types and
1404 * supported the requested numbers, so we're good.
1405 */
Johannes Berg7527a782011-05-13 10:58:57 +02001406 kfree(limits);
1407 return 0;
1408 cont:
1409 kfree(limits);
1410 }
1411
1412 return -EBUSY;
1413}
Johannes Berg34850ab2011-07-18 18:08:35 +02001414
1415int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1416 const u8 *rates, unsigned int n_rates,
1417 u32 *mask)
1418{
1419 int i, j;
1420
Johannes Berga401d2b2011-07-20 00:52:16 +02001421 if (!sband)
1422 return -EINVAL;
1423
Johannes Berg34850ab2011-07-18 18:08:35 +02001424 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1425 return -EINVAL;
1426
1427 *mask = 0;
1428
1429 for (i = 0; i < n_rates; i++) {
1430 int rate = (rates[i] & 0x7f) * 5;
1431 bool found = false;
1432
1433 for (j = 0; j < sband->n_bitrates; j++) {
1434 if (sband->bitrates[j].bitrate == rate) {
1435 found = true;
1436 *mask |= BIT(j);
1437 break;
1438 }
1439 }
1440 if (!found)
1441 return -EINVAL;
1442 }
1443
1444 /*
1445 * mask must have at least one bit set here since we
1446 * didn't accept a 0-length rates array nor allowed
1447 * entries in the array that didn't exist
1448 */
1449
1450 return 0;
1451}
Johannes Berg11a2a352011-11-21 11:09:22 +01001452
1453/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1454/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1455const unsigned char rfc1042_header[] __aligned(2) =
1456 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1457EXPORT_SYMBOL(rfc1042_header);
1458
1459/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1460const unsigned char bridge_tunnel_header[] __aligned(2) =
1461 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1462EXPORT_SYMBOL(bridge_tunnel_header);