blob: ce090c1c5e4fdb36459c4f6fde6b5af241a75f0e [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
Simon Wunderlich74608ac2013-07-08 16:55:54 +020036u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
37 enum nl80211_bss_scan_width scan_width)
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070038{
39 struct ieee80211_rate *bitrates;
40 u32 mandatory_rates = 0;
41 enum ieee80211_rate_flags mandatory_flag;
42 int i;
43
44 if (WARN_ON(!sband))
45 return 1;
46
Simon Wunderlich74608ac2013-07-08 16:55:54 +020047 if (sband->band == IEEE80211_BAND_2GHZ) {
48 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
49 scan_width == NL80211_BSS_CHAN_WIDTH_10)
50 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
51 else
52 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
53 } else {
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070054 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
Simon Wunderlich74608ac2013-07-08 16:55:54 +020055 }
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -070056
57 bitrates = sband->bitrates;
58 for (i = 0; i < sband->n_bitrates; i++)
59 if (bitrates[i].flags & mandatory_flag)
60 mandatory_rates |= BIT(i);
61 return mandatory_rates;
62}
63EXPORT_SYMBOL(ieee80211_mandatory_rates);
64
Bruno Randolf59eb21a2011-01-17 13:37:28 +090065int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +010066{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090067 /* see 802.11 17.3.8.3.2 and Annex J
68 * there are overlapping channel numbers in 5GHz and 2GHz bands */
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030069 if (chan <= 0)
70 return 0; /* not supported */
71 switch (band) {
72 case IEEE80211_BAND_2GHZ:
Bruno Randolf59eb21a2011-01-17 13:37:28 +090073 if (chan == 14)
74 return 2484;
75 else if (chan < 14)
76 return 2407 + chan * 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030077 break;
78 case IEEE80211_BAND_5GHZ:
79 if (chan >= 182 && chan <= 196)
80 return 4000 + chan * 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090081 else
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030082 return 5000 + chan * 5;
83 break;
84 case IEEE80211_BAND_60GHZ:
85 if (chan < 5)
86 return 56160 + chan * 2160;
87 break;
88 default:
89 ;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090090 }
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030091 return 0; /* not supported */
Johannes Berg8318d782008-01-24 19:38:38 +010092}
93EXPORT_SYMBOL(ieee80211_channel_to_frequency);
94
95int ieee80211_frequency_to_channel(int freq)
96{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090097 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +010098 if (freq == 2484)
99 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900100 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +0100101 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900102 else if (freq >= 4910 && freq <= 4980)
103 return (freq - 4000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300104 else if (freq <= 45000) /* DMG band lower limit */
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900105 return (freq - 5000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300106 else if (freq >= 58320 && freq <= 64800)
107 return (freq - 56160) / 2160;
108 else
109 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100110}
111EXPORT_SYMBOL(ieee80211_frequency_to_channel);
112
Johannes Berg6c507cd2008-03-26 14:14:55 +0100113struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
114 int freq)
Johannes Berg906c7302008-03-16 18:34:33 +0100115{
116 enum ieee80211_band band;
117 struct ieee80211_supported_band *sband;
118 int i;
119
120 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
121 sband = wiphy->bands[band];
122
123 if (!sband)
124 continue;
125
126 for (i = 0; i < sband->n_channels; i++) {
127 if (sband->channels[i].center_freq == freq)
128 return &sband->channels[i];
129 }
130 }
131
132 return NULL;
133}
Johannes Berg6c507cd2008-03-26 14:14:55 +0100134EXPORT_SYMBOL(__ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +0100135
Johannes Berg8318d782008-01-24 19:38:38 +0100136static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
137 enum ieee80211_band band)
138{
139 int i, want;
140
141 switch (band) {
142 case IEEE80211_BAND_5GHZ:
143 want = 3;
144 for (i = 0; i < sband->n_bitrates; i++) {
145 if (sband->bitrates[i].bitrate == 60 ||
146 sband->bitrates[i].bitrate == 120 ||
147 sband->bitrates[i].bitrate == 240) {
148 sband->bitrates[i].flags |=
149 IEEE80211_RATE_MANDATORY_A;
150 want--;
151 }
152 }
153 WARN_ON(want);
154 break;
155 case IEEE80211_BAND_2GHZ:
156 want = 7;
157 for (i = 0; i < sband->n_bitrates; i++) {
158 if (sband->bitrates[i].bitrate == 10) {
159 sband->bitrates[i].flags |=
160 IEEE80211_RATE_MANDATORY_B |
161 IEEE80211_RATE_MANDATORY_G;
162 want--;
163 }
164
165 if (sband->bitrates[i].bitrate == 20 ||
166 sband->bitrates[i].bitrate == 55 ||
167 sband->bitrates[i].bitrate == 110 ||
168 sband->bitrates[i].bitrate == 60 ||
169 sband->bitrates[i].bitrate == 120 ||
170 sband->bitrates[i].bitrate == 240) {
171 sband->bitrates[i].flags |=
172 IEEE80211_RATE_MANDATORY_G;
173 want--;
174 }
175
Johannes Bergaac09fb2008-01-30 17:36:10 +0100176 if (sband->bitrates[i].bitrate != 10 &&
177 sband->bitrates[i].bitrate != 20 &&
178 sband->bitrates[i].bitrate != 55 &&
179 sband->bitrates[i].bitrate != 110)
Johannes Berg8318d782008-01-24 19:38:38 +0100180 sband->bitrates[i].flags |=
181 IEEE80211_RATE_ERP_G;
182 }
Ivo van Doorn406f2382008-02-02 23:53:10 +0100183 WARN_ON(want != 0 && want != 3 && want != 6);
Johannes Berg8318d782008-01-24 19:38:38 +0100184 break;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300185 case IEEE80211_BAND_60GHZ:
186 /* check for mandatory HT MCS 1..4 */
187 WARN_ON(!sband->ht_cap.ht_supported);
188 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
189 break;
Johannes Berg8318d782008-01-24 19:38:38 +0100190 case IEEE80211_NUM_BANDS:
191 WARN_ON(1);
192 break;
193 }
194}
195
196void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
197{
198 enum ieee80211_band band;
199
200 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
201 if (wiphy->bands[band])
202 set_mandatory_flags_band(wiphy->bands[band], band);
203}
Johannes Berg08645122009-05-11 13:54:58 +0200204
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300205bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
206{
207 int i;
208 for (i = 0; i < wiphy->n_cipher_suites; i++)
209 if (cipher == wiphy->cipher_suites[i])
210 return true;
211 return false;
212}
213
Johannes Bergfffd0932009-07-08 14:22:54 +0200214int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
215 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200216 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200217{
218 if (key_idx > 5)
219 return -EINVAL;
220
Johannes Berge31b8212010-10-05 19:39:30 +0200221 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
222 return -EINVAL;
223
224 if (pairwise && !mac_addr)
225 return -EINVAL;
226
Johannes Berg08645122009-05-11 13:54:58 +0200227 /*
228 * Disallow pairwise keys with non-zero index unless it's WEP
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200229 * or a vendor specific cipher (because current deployments use
230 * pairwise WEP keys with non-zero indices and for vendor specific
231 * ciphers this should be validated in the driver or hardware level
232 * - but 802.11i clearly specifies to use zero)
Johannes Berg08645122009-05-11 13:54:58 +0200233 */
Johannes Berge31b8212010-10-05 19:39:30 +0200234 if (pairwise && key_idx &&
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200235 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
236 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
237 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
Johannes Berg08645122009-05-11 13:54:58 +0200238 return -EINVAL;
239
Johannes Berg08645122009-05-11 13:54:58 +0200240 switch (params->cipher) {
241 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200242 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200243 return -EINVAL;
244 break;
245 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200246 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200247 return -EINVAL;
248 break;
249 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200250 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200251 return -EINVAL;
252 break;
253 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200254 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200255 return -EINVAL;
256 break;
257 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200258 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200259 return -EINVAL;
260 break;
261 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300262 /*
263 * We don't know anything about this algorithm,
264 * allow using it -- but the driver must check
265 * all parameters! We still check below whether
266 * or not the driver supports this algorithm,
267 * of course.
268 */
269 break;
Johannes Berg08645122009-05-11 13:54:58 +0200270 }
271
Jouni Malinen9f26a952009-05-15 12:38:32 +0300272 if (params->seq) {
273 switch (params->cipher) {
274 case WLAN_CIPHER_SUITE_WEP40:
275 case WLAN_CIPHER_SUITE_WEP104:
276 /* These ciphers do not use key sequence */
277 return -EINVAL;
278 case WLAN_CIPHER_SUITE_TKIP:
279 case WLAN_CIPHER_SUITE_CCMP:
280 case WLAN_CIPHER_SUITE_AES_CMAC:
281 if (params->seq_len != 6)
282 return -EINVAL;
283 break;
284 }
285 }
286
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300287 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200288 return -EINVAL;
289
Johannes Berg08645122009-05-11 13:54:58 +0200290 return 0;
291}
Zhu Yie31a16d2009-05-21 21:47:03 +0800292
Johannes Berg633adf12010-08-12 14:49:58 +0200293unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800294{
295 unsigned int hdrlen = 24;
296
297 if (ieee80211_is_data(fc)) {
298 if (ieee80211_has_a4(fc))
299 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200300 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800301 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200302 if (ieee80211_has_order(fc))
303 hdrlen += IEEE80211_HT_CTL_LEN;
304 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800305 goto out;
306 }
307
308 if (ieee80211_is_ctl(fc)) {
309 /*
310 * ACK and CTS are 10 bytes, all others 16. To see how
311 * to get this condition consider
312 * subtype mask: 0b0000000011110000 (0x00F0)
313 * ACK subtype: 0b0000000011010000 (0x00D0)
314 * CTS subtype: 0b0000000011000000 (0x00C0)
315 * bits that matter: ^^^ (0x00E0)
316 * value of those: 0b0000000011000000 (0x00C0)
317 */
318 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
319 hdrlen = 10;
320 else
321 hdrlen = 16;
322 }
323out:
324 return hdrlen;
325}
326EXPORT_SYMBOL(ieee80211_hdrlen);
327
328unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
329{
330 const struct ieee80211_hdr *hdr =
331 (const struct ieee80211_hdr *)skb->data;
332 unsigned int hdrlen;
333
334 if (unlikely(skb->len < 10))
335 return 0;
336 hdrlen = ieee80211_hdrlen(hdr->frame_control);
337 if (unlikely(hdrlen > skb->len))
338 return 0;
339 return hdrlen;
340}
341EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
342
Johannes Berg9b395bc2012-10-26 00:36:40 +0200343unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800344{
345 int ae = meshhdr->flags & MESH_FLAGS_AE;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200346 /* 802.11-2012, 8.2.4.7.3 */
Zhu Yie31a16d2009-05-21 21:47:03 +0800347 switch (ae) {
Johannes Berg7dd111e2012-10-25 21:51:59 +0200348 default:
Zhu Yie31a16d2009-05-21 21:47:03 +0800349 case 0:
350 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700351 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800352 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700353 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800354 return 18;
Zhu Yie31a16d2009-05-21 21:47:03 +0800355 }
356}
Johannes Berg9b395bc2012-10-26 00:36:40 +0200357EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800358
Zhu Yieaf85ca2009-12-01 10:18:37 +0800359int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800360 enum nl80211_iftype iftype)
361{
362 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
363 u16 hdrlen, ethertype;
364 u8 *payload;
365 u8 dst[ETH_ALEN];
366 u8 src[ETH_ALEN] __aligned(2);
367
368 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
369 return -1;
370
371 hdrlen = ieee80211_hdrlen(hdr->frame_control);
372
373 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
374 * header
375 * IEEE 802.11 address fields:
376 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
377 * 0 0 DA SA BSSID n/a
378 * 0 1 DA BSSID SA n/a
379 * 1 0 BSSID SA DA n/a
380 * 1 1 RA TA DA SA
381 */
382 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
383 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
384
385 switch (hdr->frame_control &
386 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
387 case cpu_to_le16(IEEE80211_FCTL_TODS):
388 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200389 iftype != NL80211_IFTYPE_AP_VLAN &&
390 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800391 return -1;
392 break;
393 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
394 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543ee2009-11-10 20:10:05 +0100395 iftype != NL80211_IFTYPE_MESH_POINT &&
396 iftype != NL80211_IFTYPE_AP_VLAN &&
397 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800398 return -1;
399 if (iftype == NL80211_IFTYPE_MESH_POINT) {
400 struct ieee80211s_hdr *meshdr =
401 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800402 /* make sure meshdr->flags is on the linear part */
403 if (!pskb_may_pull(skb, hdrlen + 1))
404 return -1;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200405 if (meshdr->flags & MESH_FLAGS_AE_A4)
406 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800407 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800408 skb_copy_bits(skb, hdrlen +
409 offsetof(struct ieee80211s_hdr, eaddr1),
410 dst, ETH_ALEN);
411 skb_copy_bits(skb, hdrlen +
412 offsetof(struct ieee80211s_hdr, eaddr2),
413 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800414 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800415 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800416 }
417 break;
418 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700419 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200420 iftype != NL80211_IFTYPE_P2P_CLIENT &&
421 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800422 (is_multicast_ether_addr(dst) &&
Joe Perches4c764722012-05-08 18:56:56 +0000423 ether_addr_equal(src, addr)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800424 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700425 if (iftype == NL80211_IFTYPE_MESH_POINT) {
426 struct ieee80211s_hdr *meshdr =
427 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800428 /* make sure meshdr->flags is on the linear part */
429 if (!pskb_may_pull(skb, hdrlen + 1))
430 return -1;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200431 if (meshdr->flags & MESH_FLAGS_AE_A5_A6)
432 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700433 if (meshdr->flags & MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800434 skb_copy_bits(skb, hdrlen +
435 offsetof(struct ieee80211s_hdr, eaddr1),
436 src, ETH_ALEN);
437 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700438 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800439 break;
440 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300441 if (iftype != NL80211_IFTYPE_ADHOC &&
442 iftype != NL80211_IFTYPE_STATION)
443 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800444 break;
445 }
446
Zhu Yie3cf8b32010-03-29 17:35:07 +0800447 if (!pskb_may_pull(skb, hdrlen + 8))
Zhu Yie31a16d2009-05-21 21:47:03 +0800448 return -1;
449
450 payload = skb->data + hdrlen;
451 ethertype = (payload[6] << 8) | payload[7];
452
Joe Perches4c764722012-05-08 18:56:56 +0000453 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yie31a16d2009-05-21 21:47:03 +0800454 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perches4c764722012-05-08 18:56:56 +0000455 ether_addr_equal(payload, bridge_tunnel_header))) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800456 /* remove RFC1042 or Bridge-Tunnel encapsulation and
457 * replace EtherType */
458 skb_pull(skb, hdrlen + 6);
459 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
460 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
461 } else {
462 struct ethhdr *ehdr;
463 __be16 len;
464
465 skb_pull(skb, hdrlen);
466 len = htons(skb->len);
467 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
468 memcpy(ehdr->h_dest, dst, ETH_ALEN);
469 memcpy(ehdr->h_source, src, ETH_ALEN);
470 ehdr->h_proto = len;
471 }
472 return 0;
473}
474EXPORT_SYMBOL(ieee80211_data_to_8023);
475
Zhu Yieaf85ca2009-12-01 10:18:37 +0800476int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800477 enum nl80211_iftype iftype, u8 *bssid, bool qos)
478{
479 struct ieee80211_hdr hdr;
480 u16 hdrlen, ethertype;
481 __le16 fc;
482 const u8 *encaps_data;
483 int encaps_len, skip_header_bytes;
484 int nh_pos, h_pos;
485 int head_need;
486
487 if (unlikely(skb->len < ETH_HLEN))
488 return -EINVAL;
489
490 nh_pos = skb_network_header(skb) - skb->data;
491 h_pos = skb_transport_header(skb) - skb->data;
492
493 /* convert Ethernet header to proper 802.11 header (based on
494 * operation mode) */
495 ethertype = (skb->data[12] << 8) | skb->data[13];
496 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
497
498 switch (iftype) {
499 case NL80211_IFTYPE_AP:
500 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200501 case NL80211_IFTYPE_P2P_GO:
Zhu Yie31a16d2009-05-21 21:47:03 +0800502 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
503 /* DA BSSID SA */
504 memcpy(hdr.addr1, skb->data, ETH_ALEN);
505 memcpy(hdr.addr2, addr, ETH_ALEN);
506 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
507 hdrlen = 24;
508 break;
509 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200510 case NL80211_IFTYPE_P2P_CLIENT:
Zhu Yie31a16d2009-05-21 21:47:03 +0800511 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
512 /* BSSID SA DA */
513 memcpy(hdr.addr1, bssid, ETH_ALEN);
514 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
515 memcpy(hdr.addr3, skb->data, ETH_ALEN);
516 hdrlen = 24;
517 break;
518 case NL80211_IFTYPE_ADHOC:
519 /* DA SA BSSID */
520 memcpy(hdr.addr1, skb->data, ETH_ALEN);
521 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
522 memcpy(hdr.addr3, bssid, ETH_ALEN);
523 hdrlen = 24;
524 break;
525 default:
526 return -EOPNOTSUPP;
527 }
528
529 if (qos) {
530 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
531 hdrlen += 2;
532 }
533
534 hdr.frame_control = fc;
535 hdr.duration_id = 0;
536 hdr.seq_ctrl = 0;
537
538 skip_header_bytes = ETH_HLEN;
539 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
540 encaps_data = bridge_tunnel_header;
541 encaps_len = sizeof(bridge_tunnel_header);
542 skip_header_bytes -= 2;
Simon Hormane5c5d222013-03-28 13:38:25 +0900543 } else if (ethertype >= ETH_P_802_3_MIN) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800544 encaps_data = rfc1042_header;
545 encaps_len = sizeof(rfc1042_header);
546 skip_header_bytes -= 2;
547 } else {
548 encaps_data = NULL;
549 encaps_len = 0;
550 }
551
552 skb_pull(skb, skip_header_bytes);
553 nh_pos -= skip_header_bytes;
554 h_pos -= skip_header_bytes;
555
556 head_need = hdrlen + encaps_len - skb_headroom(skb);
557
558 if (head_need > 0 || skb_cloned(skb)) {
559 head_need = max(head_need, 0);
560 if (head_need)
561 skb_orphan(skb);
562
Joe Perches24616152011-08-29 14:17:41 -0700563 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
Zhu Yie31a16d2009-05-21 21:47:03 +0800564 return -ENOMEM;
Joe Perches24616152011-08-29 14:17:41 -0700565
Zhu Yie31a16d2009-05-21 21:47:03 +0800566 skb->truesize += head_need;
567 }
568
569 if (encaps_data) {
570 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
571 nh_pos += encaps_len;
572 h_pos += encaps_len;
573 }
574
575 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
576
577 nh_pos += hdrlen;
578 h_pos += hdrlen;
579
580 /* Update skb pointers to various headers since this modified frame
581 * is going to go through Linux networking code that may potentially
582 * need things like pointer to IP header. */
583 skb_set_mac_header(skb, 0);
584 skb_set_network_header(skb, nh_pos);
585 skb_set_transport_header(skb, h_pos);
586
587 return 0;
588}
589EXPORT_SYMBOL(ieee80211_data_from_8023);
590
Zhu Yieaf85ca2009-12-01 10:18:37 +0800591
592void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
593 const u8 *addr, enum nl80211_iftype iftype,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700594 const unsigned int extra_headroom,
595 bool has_80211_header)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800596{
597 struct sk_buff *frame = NULL;
598 u16 ethertype;
599 u8 *payload;
600 const struct ethhdr *eth;
601 int remaining, err;
602 u8 dst[ETH_ALEN], src[ETH_ALEN];
603
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700604 if (has_80211_header) {
605 err = ieee80211_data_to_8023(skb, addr, iftype);
606 if (err)
607 goto out;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800608
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700609 /* skip the wrapping header */
610 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
611 if (!eth)
612 goto out;
613 } else {
614 eth = (struct ethhdr *) skb->data;
615 }
Zhu Yieaf85ca2009-12-01 10:18:37 +0800616
617 while (skb != frame) {
618 u8 padding;
619 __be16 len = eth->h_proto;
620 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
621
622 remaining = skb->len;
623 memcpy(dst, eth->h_dest, ETH_ALEN);
624 memcpy(src, eth->h_source, ETH_ALEN);
625
626 padding = (4 - subframe_len) & 0x3;
627 /* the last MSDU has no padding */
628 if (subframe_len > remaining)
629 goto purge;
630
631 skb_pull(skb, sizeof(struct ethhdr));
632 /* reuse skb for the last subframe */
633 if (remaining <= subframe_len + padding)
634 frame = skb;
635 else {
636 unsigned int hlen = ALIGN(extra_headroom, 4);
637 /*
638 * Allocate and reserve two bytes more for payload
639 * alignment since sizeof(struct ethhdr) is 14.
640 */
641 frame = dev_alloc_skb(hlen + subframe_len + 2);
642 if (!frame)
643 goto purge;
644
645 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
646 memcpy(skb_put(frame, ntohs(len)), skb->data,
647 ntohs(len));
648
649 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
650 padding);
651 if (!eth) {
652 dev_kfree_skb(frame);
653 goto purge;
654 }
655 }
656
657 skb_reset_network_header(frame);
658 frame->dev = skb->dev;
659 frame->priority = skb->priority;
660
661 payload = frame->data;
662 ethertype = (payload[6] << 8) | payload[7];
663
Joe Perchesac422d32012-05-08 18:56:55 +0000664 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yieaf85ca2009-12-01 10:18:37 +0800665 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perchesac422d32012-05-08 18:56:55 +0000666 ether_addr_equal(payload, bridge_tunnel_header))) {
Zhu Yieaf85ca2009-12-01 10:18:37 +0800667 /* remove RFC1042 or Bridge-Tunnel
668 * encapsulation and replace EtherType */
669 skb_pull(frame, 6);
670 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
671 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
672 } else {
673 memcpy(skb_push(frame, sizeof(__be16)), &len,
674 sizeof(__be16));
675 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
676 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
677 }
678 __skb_queue_tail(list, frame);
679 }
680
681 return;
682
683 purge:
684 __skb_queue_purge(list);
685 out:
686 dev_kfree_skb(skb);
687}
688EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
689
Zhu Yie31a16d2009-05-21 21:47:03 +0800690/* Given a data frame determine the 802.1p/1d tag to use. */
691unsigned int cfg80211_classify8021d(struct sk_buff *skb)
692{
693 unsigned int dscp;
694
695 /* skb->priority values from 256->263 are magic values to
696 * directly indicate a specific 802.1d priority. This is used
697 * to allow 802.1d priority to be passed directly in from VLAN
698 * tags, etc.
699 */
700 if (skb->priority >= 256 && skb->priority <= 263)
701 return skb->priority - 256;
702
703 switch (skb->protocol) {
704 case htons(ETH_P_IP):
Dave Tähtb1565792011-12-22 12:55:08 -0800705 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
706 break;
707 case htons(ETH_P_IPV6):
708 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
Zhu Yie31a16d2009-05-21 21:47:03 +0800709 break;
710 default:
711 return 0;
712 }
713
714 return dscp >> 5;
715}
716EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200717
718const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
719{
Johannes Berg9caf0362012-11-29 01:25:20 +0100720 const struct cfg80211_bss_ies *ies;
721
722 ies = rcu_dereference(bss->ies);
723 if (!ies)
Johannes Berg517357c2009-07-02 17:18:40 +0200724 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +0100725
726 return cfg80211_find_ie(ie, ies->data, ies->len);
Johannes Berg517357c2009-07-02 17:18:40 +0200727}
728EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200729
730void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
731{
732 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
733 struct net_device *dev = wdev->netdev;
734 int i;
735
736 if (!wdev->connect_keys)
737 return;
738
739 for (i = 0; i < 6; i++) {
740 if (!wdev->connect_keys->params[i].cipher)
741 continue;
Hila Gonene35e4d22012-06-27 17:19:42 +0300742 if (rdev_add_key(rdev, dev, i, false, NULL,
743 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800744 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800745 continue;
746 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200747 if (wdev->connect_keys->def == i)
Hila Gonene35e4d22012-06-27 17:19:42 +0300748 if (rdev_set_default_key(rdev, dev, i, true, true)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800749 netdev_err(dev, "failed to set defkey %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800750 continue;
751 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200752 if (wdev->connect_keys->defmgmt == i)
Hila Gonene35e4d22012-06-27 17:19:42 +0300753 if (rdev_set_default_mgmt_key(rdev, dev, i))
Joe Perchese9c02682010-11-16 19:56:49 -0800754 netdev_err(dev, "failed to set mgtdef %d\n", i);
Johannes Bergfffd0932009-07-08 14:22:54 +0200755 }
756
757 kfree(wdev->connect_keys);
758 wdev->connect_keys = NULL;
759}
Johannes Berg3d54d252009-08-21 14:51:05 +0200760
Daniel Drake1f6fc432012-08-02 18:41:48 +0100761void cfg80211_process_wdev_events(struct wireless_dev *wdev)
Johannes Berg3d54d252009-08-21 14:51:05 +0200762{
763 struct cfg80211_event *ev;
764 unsigned long flags;
765 const u8 *bssid = NULL;
766
767 spin_lock_irqsave(&wdev->event_lock, flags);
768 while (!list_empty(&wdev->event_list)) {
769 ev = list_first_entry(&wdev->event_list,
770 struct cfg80211_event, list);
771 list_del(&ev->list);
772 spin_unlock_irqrestore(&wdev->event_lock, flags);
773
774 wdev_lock(wdev);
775 switch (ev->type) {
776 case EVENT_CONNECT_RESULT:
777 if (!is_zero_ether_addr(ev->cr.bssid))
778 bssid = ev->cr.bssid;
779 __cfg80211_connect_result(
780 wdev->netdev, bssid,
781 ev->cr.req_ie, ev->cr.req_ie_len,
782 ev->cr.resp_ie, ev->cr.resp_ie_len,
783 ev->cr.status,
784 ev->cr.status == WLAN_STATUS_SUCCESS,
785 NULL);
786 break;
787 case EVENT_ROAMED:
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530788 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
789 ev->rm.req_ie_len, ev->rm.resp_ie,
790 ev->rm.resp_ie_len);
Johannes Berg3d54d252009-08-21 14:51:05 +0200791 break;
792 case EVENT_DISCONNECTED:
793 __cfg80211_disconnected(wdev->netdev,
794 ev->dc.ie, ev->dc.ie_len,
795 ev->dc.reason, true);
796 break;
797 case EVENT_IBSS_JOINED:
798 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
799 break;
800 }
801 wdev_unlock(wdev);
802
803 kfree(ev);
804
805 spin_lock_irqsave(&wdev->event_lock, flags);
806 }
807 spin_unlock_irqrestore(&wdev->event_lock, flags);
808}
809
810void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
811{
812 struct wireless_dev *wdev;
813
814 ASSERT_RTNL();
815 ASSERT_RDEV_LOCK(rdev);
816
Johannes Berg89a54e42012-06-15 14:33:17 +0200817 list_for_each_entry(wdev, &rdev->wdev_list, list)
Johannes Berg3d54d252009-08-21 14:51:05 +0200818 cfg80211_process_wdev_events(wdev);
Johannes Berg3d54d252009-08-21 14:51:05 +0200819}
820
821int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
822 struct net_device *dev, enum nl80211_iftype ntype,
823 u32 *flags, struct vif_params *params)
824{
825 int err;
826 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
827
828 ASSERT_RDEV_LOCK(rdev);
829
830 /* don't support changing VLANs, you just re-create them */
831 if (otype == NL80211_IFTYPE_AP_VLAN)
832 return -EOPNOTSUPP;
833
Johannes Berg98104fde2012-06-16 00:19:54 +0200834 /* cannot change into P2P device type */
835 if (ntype == NL80211_IFTYPE_P2P_DEVICE)
836 return -EOPNOTSUPP;
837
Johannes Berg3d54d252009-08-21 14:51:05 +0200838 if (!rdev->ops->change_virtual_intf ||
839 !(rdev->wiphy.interface_modes & (1 << ntype)))
840 return -EOPNOTSUPP;
841
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100842 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000843 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200844 (ntype == NL80211_IFTYPE_ADHOC ||
845 ntype == NL80211_IFTYPE_STATION ||
846 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100847 return -EBUSY;
848
Michal Kaziorf8cdddb2012-06-08 10:55:44 +0200849 if (ntype != otype && netif_running(dev)) {
Johannes Berg7527a782011-05-13 10:58:57 +0200850 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
851 ntype);
852 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 Berg83739b02013-05-15 17:44:01 +0200867 wdev_lock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +0200868 cfg80211_disconnect(rdev, dev,
869 WLAN_REASON_DEAUTH_LEAVING, true);
Johannes Berg83739b02013-05-15 17:44:01 +0200870 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg3d54d252009-08-21 14:51:05 +0200871 break;
872 case NL80211_IFTYPE_MESH_POINT:
873 /* mesh should be handled? */
874 break;
875 default:
876 break;
877 }
878
879 cfg80211_process_rdev_events(rdev);
880 }
881
Hila Gonene35e4d22012-06-27 17:19:42 +0300882 err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
Johannes Berg3d54d252009-08-21 14:51:05 +0200883
884 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
885
Johannes Berg9bc383d2009-11-19 11:55:19 +0100886 if (!err && params && params->use_4addr != -1)
887 dev->ieee80211_ptr->use_4addr = params->use_4addr;
888
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100889 if (!err) {
890 dev->priv_flags &= ~IFF_DONT_BRIDGE;
891 switch (ntype) {
892 case NL80211_IFTYPE_STATION:
893 if (dev->ieee80211_ptr->use_4addr)
894 break;
895 /* fall through */
Johannes Berg074ac8d2010-09-16 14:58:22 +0200896 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100897 case NL80211_IFTYPE_ADHOC:
898 dev->priv_flags |= IFF_DONT_BRIDGE;
899 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200900 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100901 case NL80211_IFTYPE_AP:
902 case NL80211_IFTYPE_AP_VLAN:
903 case NL80211_IFTYPE_WDS:
904 case NL80211_IFTYPE_MESH_POINT:
905 /* bridging OK */
906 break;
907 case NL80211_IFTYPE_MONITOR:
908 /* monitor can't bridge anyway */
909 break;
910 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200911 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100912 /* not happening */
913 break;
Johannes Berg98104fde2012-06-16 00:19:54 +0200914 case NL80211_IFTYPE_P2P_DEVICE:
915 WARN_ON(1);
916 break;
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100917 }
918 }
919
Michal Kaziordbbae262012-06-29 12:47:01 +0200920 if (!err && ntype != otype && netif_running(dev)) {
921 cfg80211_update_iface_num(rdev, ntype, 1);
922 cfg80211_update_iface_num(rdev, otype, -1);
923 }
924
Johannes Berg3d54d252009-08-21 14:51:05 +0200925 return err;
926}
John W. Linville254416a2009-12-09 16:43:52 -0500927
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +0300928static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
929{
930 static const u32 __mcs2bitrate[] = {
931 /* control PHY */
932 [0] = 275,
933 /* SC PHY */
934 [1] = 3850,
935 [2] = 7700,
936 [3] = 9625,
937 [4] = 11550,
938 [5] = 12512, /* 1251.25 mbps */
939 [6] = 15400,
940 [7] = 19250,
941 [8] = 23100,
942 [9] = 25025,
943 [10] = 30800,
944 [11] = 38500,
945 [12] = 46200,
946 /* OFDM PHY */
947 [13] = 6930,
948 [14] = 8662, /* 866.25 mbps */
949 [15] = 13860,
950 [16] = 17325,
951 [17] = 20790,
952 [18] = 27720,
953 [19] = 34650,
954 [20] = 41580,
955 [21] = 45045,
956 [22] = 51975,
957 [23] = 62370,
958 [24] = 67568, /* 6756.75 mbps */
959 /* LP-SC PHY */
960 [25] = 6260,
961 [26] = 8340,
962 [27] = 11120,
963 [28] = 12510,
964 [29] = 16680,
965 [30] = 22240,
966 [31] = 25030,
967 };
968
969 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
970 return 0;
971
972 return __mcs2bitrate[rate->mcs];
973}
974
Johannes Bergdb9c64c2012-11-09 14:56:41 +0100975static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
976{
977 static const u32 base[4][10] = {
978 { 6500000,
979 13000000,
980 19500000,
981 26000000,
982 39000000,
983 52000000,
984 58500000,
985 65000000,
986 78000000,
987 0,
988 },
989 { 13500000,
990 27000000,
991 40500000,
992 54000000,
993 81000000,
994 108000000,
995 121500000,
996 135000000,
997 162000000,
998 180000000,
999 },
1000 { 29300000,
1001 58500000,
1002 87800000,
1003 117000000,
1004 175500000,
1005 234000000,
1006 263300000,
1007 292500000,
1008 351000000,
1009 390000000,
1010 },
1011 { 58500000,
1012 117000000,
1013 175500000,
1014 234000000,
1015 351000000,
1016 468000000,
1017 526500000,
1018 585000000,
1019 702000000,
1020 780000000,
1021 },
1022 };
1023 u32 bitrate;
1024 int idx;
1025
1026 if (WARN_ON_ONCE(rate->mcs > 9))
1027 return 0;
1028
1029 idx = rate->flags & (RATE_INFO_FLAGS_160_MHZ_WIDTH |
1030 RATE_INFO_FLAGS_80P80_MHZ_WIDTH) ? 3 :
1031 rate->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH ? 2 :
1032 rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH ? 1 : 0;
1033
1034 bitrate = base[idx][rate->mcs];
1035 bitrate *= rate->nss;
1036
1037 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1038 bitrate = (bitrate / 9) * 10;
1039
1040 /* do NOT round down here */
1041 return (bitrate + 50000) / 100000;
1042}
1043
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03001044u32 cfg80211_calculate_bitrate(struct rate_info *rate)
John W. Linville254416a2009-12-09 16:43:52 -05001045{
1046 int modulation, streams, bitrate;
1047
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001048 if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
1049 !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
John W. Linville254416a2009-12-09 16:43:52 -05001050 return rate->legacy;
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +03001051 if (rate->flags & RATE_INFO_FLAGS_60G)
1052 return cfg80211_calculate_bitrate_60g(rate);
Johannes Bergdb9c64c2012-11-09 14:56:41 +01001053 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1054 return cfg80211_calculate_bitrate_vht(rate);
John W. Linville254416a2009-12-09 16:43:52 -05001055
1056 /* the formula below does only work for MCS values smaller than 32 */
Johannes Berg2615f372012-05-08 21:36:20 +02001057 if (WARN_ON_ONCE(rate->mcs >= 32))
John W. Linville254416a2009-12-09 16:43:52 -05001058 return 0;
1059
1060 modulation = rate->mcs & 7;
1061 streams = (rate->mcs >> 3) + 1;
1062
1063 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1064 13500000 : 6500000;
1065
1066 if (modulation < 4)
1067 bitrate *= (modulation + 1);
1068 else if (modulation == 4)
1069 bitrate *= (modulation + 2);
1070 else
1071 bitrate *= (modulation + 3);
1072
1073 bitrate *= streams;
1074
1075 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1076 bitrate = (bitrate / 9) * 10;
1077
1078 /* do NOT round down here */
1079 return (bitrate + 50000) / 100000;
1080}
Thomas Pedersen8097e142012-03-05 15:31:47 -08001081EXPORT_SYMBOL(cfg80211_calculate_bitrate);
Johannes Berg56d18932011-05-09 18:41:15 +02001082
Arend van Sprielc216e642012-11-25 19:13:28 +01001083int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1084 enum ieee80211_p2p_attr_id attr,
1085 u8 *buf, unsigned int bufsize)
Johannes Berg0ee45352012-10-29 19:48:40 +01001086{
1087 u8 *out = buf;
1088 u16 attr_remaining = 0;
1089 bool desired_attr = false;
1090 u16 desired_len = 0;
1091
1092 while (len > 0) {
1093 unsigned int iedatalen;
1094 unsigned int copy;
1095 const u8 *iedata;
1096
1097 if (len < 2)
1098 return -EILSEQ;
1099 iedatalen = ies[1];
1100 if (iedatalen + 2 > len)
1101 return -EILSEQ;
1102
1103 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1104 goto cont;
1105
1106 if (iedatalen < 4)
1107 goto cont;
1108
1109 iedata = ies + 2;
1110
1111 /* check WFA OUI, P2P subtype */
1112 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1113 iedata[2] != 0x9a || iedata[3] != 0x09)
1114 goto cont;
1115
1116 iedatalen -= 4;
1117 iedata += 4;
1118
1119 /* check attribute continuation into this IE */
1120 copy = min_t(unsigned int, attr_remaining, iedatalen);
1121 if (copy && desired_attr) {
1122 desired_len += copy;
1123 if (out) {
1124 memcpy(out, iedata, min(bufsize, copy));
1125 out += min(bufsize, copy);
1126 bufsize -= min(bufsize, copy);
1127 }
1128
1129
1130 if (copy == attr_remaining)
1131 return desired_len;
1132 }
1133
1134 attr_remaining -= copy;
1135 if (attr_remaining)
1136 goto cont;
1137
1138 iedatalen -= copy;
1139 iedata += copy;
1140
1141 while (iedatalen > 0) {
1142 u16 attr_len;
1143
1144 /* P2P attribute ID & size must fit */
1145 if (iedatalen < 3)
1146 return -EILSEQ;
1147 desired_attr = iedata[0] == attr;
1148 attr_len = get_unaligned_le16(iedata + 1);
1149 iedatalen -= 3;
1150 iedata += 3;
1151
1152 copy = min_t(unsigned int, attr_len, iedatalen);
1153
1154 if (desired_attr) {
1155 desired_len += copy;
1156 if (out) {
1157 memcpy(out, iedata, min(bufsize, copy));
1158 out += min(bufsize, copy);
1159 bufsize -= min(bufsize, copy);
1160 }
1161
1162 if (copy == attr_len)
1163 return desired_len;
1164 }
1165
1166 iedata += copy;
1167 iedatalen -= copy;
1168 attr_remaining = attr_len - copy;
1169 }
1170
1171 cont:
1172 len -= ies[1] + 2;
1173 ies += ies[1] + 2;
1174 }
1175
1176 if (attr_remaining && desired_attr)
1177 return -EILSEQ;
1178
1179 return -ENOENT;
1180}
1181EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1182
Johannes Berg1ce3e822012-08-01 17:00:55 +02001183bool ieee80211_operating_class_to_band(u8 operating_class,
1184 enum ieee80211_band *band)
1185{
1186 switch (operating_class) {
1187 case 112:
1188 case 115 ... 127:
1189 *band = IEEE80211_BAND_5GHZ;
1190 return true;
1191 case 81:
1192 case 82:
1193 case 83:
1194 case 84:
1195 *band = IEEE80211_BAND_2GHZ;
1196 return true;
Vladimir Kondratiev55300a12013-04-23 09:54:21 +03001197 case 180:
1198 *band = IEEE80211_BAND_60GHZ;
1199 return true;
Johannes Berg1ce3e822012-08-01 17:00:55 +02001200 }
1201
1202 return false;
1203}
1204EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1205
Johannes Berg56d18932011-05-09 18:41:15 +02001206int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1207 u32 beacon_int)
1208{
1209 struct wireless_dev *wdev;
1210 int res = 0;
1211
1212 if (!beacon_int)
1213 return -EINVAL;
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
Johannes Berg56d18932011-05-09 18:41:15 +02001224 return res;
1225}
Johannes Berg7527a782011-05-13 10:58:57 +02001226
Michal Kaziord4e50c52012-06-29 12:47:07 +02001227int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
1228 struct wireless_dev *wdev,
1229 enum nl80211_iftype iftype,
1230 struct ieee80211_channel *chan,
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001231 enum cfg80211_chan_mode chanmode,
1232 u8 radar_detect)
Johannes Berg7527a782011-05-13 10:58:57 +02001233{
1234 struct wireless_dev *wdev_iter;
Johannes Berg463454b2012-06-05 12:16:50 +02001235 u32 used_iftypes = BIT(iftype);
Johannes Berg7527a782011-05-13 10:58:57 +02001236 int num[NUM_NL80211_IFTYPES];
Michal Kaziord4e50c52012-06-29 12:47:07 +02001237 struct ieee80211_channel
1238 *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
1239 struct ieee80211_channel *ch;
1240 enum cfg80211_chan_mode chmode;
1241 int num_different_channels = 0;
Johannes Berg7527a782011-05-13 10:58:57 +02001242 int total = 1;
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001243 bool radar_required;
Johannes Berg7527a782011-05-13 10:58:57 +02001244 int i, j;
1245
1246 ASSERT_RTNL();
1247
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001248 if (WARN_ON(hweight32(radar_detect) > 1))
1249 return -EINVAL;
1250
1251 switch (iftype) {
1252 case NL80211_IFTYPE_ADHOC:
1253 case NL80211_IFTYPE_AP:
1254 case NL80211_IFTYPE_AP_VLAN:
1255 case NL80211_IFTYPE_MESH_POINT:
1256 case NL80211_IFTYPE_P2P_GO:
1257 case NL80211_IFTYPE_WDS:
Simon Wunderlich683d41a2013-01-23 15:15:57 +01001258 radar_required = !!(chan &&
1259 (chan->flags & IEEE80211_CHAN_RADAR));
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001260 break;
1261 case NL80211_IFTYPE_P2P_CLIENT:
1262 case NL80211_IFTYPE_STATION:
Ilan Peerbba87ff2013-02-03 08:28:27 +02001263 case NL80211_IFTYPE_P2P_DEVICE:
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001264 case NL80211_IFTYPE_MONITOR:
1265 radar_required = false;
1266 break;
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001267 case NUM_NL80211_IFTYPES:
1268 case NL80211_IFTYPE_UNSPECIFIED:
1269 default:
1270 return -EINVAL;
1271 }
1272
1273 if (radar_required && !radar_detect)
1274 return -EINVAL;
1275
Johannes Berg7527a782011-05-13 10:58:57 +02001276 /* Always allow software iftypes */
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001277 if (rdev->wiphy.software_iftypes & BIT(iftype)) {
1278 if (radar_detect)
1279 return -EINVAL;
Johannes Berg7527a782011-05-13 10:58:57 +02001280 return 0;
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001281 }
Johannes Berg7527a782011-05-13 10:58:57 +02001282
Johannes Berg7527a782011-05-13 10:58:57 +02001283 memset(num, 0, sizeof(num));
Michal Kaziord4e50c52012-06-29 12:47:07 +02001284 memset(used_channels, 0, sizeof(used_channels));
Johannes Berg7527a782011-05-13 10:58:57 +02001285
1286 num[iftype] = 1;
1287
Michal Kaziord4e50c52012-06-29 12:47:07 +02001288 switch (chanmode) {
1289 case CHAN_MODE_UNDEFINED:
1290 break;
1291 case CHAN_MODE_SHARED:
1292 WARN_ON(!chan);
1293 used_channels[0] = chan;
1294 num_different_channels++;
1295 break;
1296 case CHAN_MODE_EXCLUSIVE:
1297 num_different_channels++;
1298 break;
1299 }
1300
Johannes Berg89a54e42012-06-15 14:33:17 +02001301 list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
Johannes Berg7527a782011-05-13 10:58:57 +02001302 if (wdev_iter == wdev)
1303 continue;
Johannes Berg6e3ab552013-04-19 12:19:39 +02001304 if (wdev_iter->iftype == NL80211_IFTYPE_P2P_DEVICE) {
Johannes Berg98104fde2012-06-16 00:19:54 +02001305 if (!wdev_iter->p2p_started)
1306 continue;
Johannes Berg6e3ab552013-04-19 12:19:39 +02001307 } else if (wdev_iter->netdev) {
1308 if (!netif_running(wdev_iter->netdev))
1309 continue;
Johannes Berg98104fde2012-06-16 00:19:54 +02001310 } else {
1311 WARN_ON(1);
1312 }
Johannes Berg7527a782011-05-13 10:58:57 +02001313
1314 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
1315 continue;
1316
Johannes Berg8e95ea42012-07-10 19:39:02 +02001317 /*
1318 * We may be holding the "wdev" mutex, but now need to lock
1319 * wdev_iter. This is OK because once we get here wdev_iter
1320 * is not wdev (tested above), but we need to use the nested
1321 * locking for lockdep.
1322 */
1323 mutex_lock_nested(&wdev_iter->mtx, 1);
1324 __acquire(wdev_iter->mtx);
1325 cfg80211_get_chan_state(wdev_iter, &ch, &chmode);
1326 wdev_unlock(wdev_iter);
Michal Kaziord4e50c52012-06-29 12:47:07 +02001327
1328 switch (chmode) {
1329 case CHAN_MODE_UNDEFINED:
1330 break;
1331 case CHAN_MODE_SHARED:
1332 for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
1333 if (!used_channels[i] || used_channels[i] == ch)
1334 break;
1335
Michal Kaziore4e32452012-06-29 12:47:08 +02001336 if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
Michal Kaziord4e50c52012-06-29 12:47:07 +02001337 return -EBUSY;
Michal Kaziord4e50c52012-06-29 12:47:07 +02001338
1339 if (used_channels[i] == NULL) {
1340 used_channels[i] = ch;
1341 num_different_channels++;
1342 }
1343 break;
1344 case CHAN_MODE_EXCLUSIVE:
1345 num_different_channels++;
1346 break;
1347 }
1348
Johannes Berg7527a782011-05-13 10:58:57 +02001349 num[wdev_iter->iftype]++;
1350 total++;
Johannes Berg463454b2012-06-05 12:16:50 +02001351 used_iftypes |= BIT(wdev_iter->iftype);
Johannes Berg7527a782011-05-13 10:58:57 +02001352 }
Johannes Berg7527a782011-05-13 10:58:57 +02001353
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001354 if (total == 1 && !radar_detect)
Johannes Berg8e8b41f2012-03-15 10:16:16 +01001355 return 0;
1356
Johannes Berg7527a782011-05-13 10:58:57 +02001357 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1358 const struct ieee80211_iface_combination *c;
1359 struct ieee80211_iface_limit *limits;
Johannes Berg463454b2012-06-05 12:16:50 +02001360 u32 all_iftypes = 0;
Johannes Berg7527a782011-05-13 10:58:57 +02001361
1362 c = &rdev->wiphy.iface_combinations[i];
1363
Michal Kaziord4e50c52012-06-29 12:47:07 +02001364 if (total > c->max_interfaces)
1365 continue;
1366 if (num_different_channels > c->num_different_channels)
1367 continue;
1368
Johannes Berg7527a782011-05-13 10:58:57 +02001369 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1370 GFP_KERNEL);
1371 if (!limits)
1372 return -ENOMEM;
Johannes Berg7527a782011-05-13 10:58:57 +02001373
1374 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1375 if (rdev->wiphy.software_iftypes & BIT(iftype))
1376 continue;
1377 for (j = 0; j < c->n_limits; j++) {
Johannes Berg463454b2012-06-05 12:16:50 +02001378 all_iftypes |= limits[j].types;
Lukasz Kucharczyke55a4042012-04-11 14:55:10 +02001379 if (!(limits[j].types & BIT(iftype)))
Johannes Berg7527a782011-05-13 10:58:57 +02001380 continue;
1381 if (limits[j].max < num[iftype])
1382 goto cont;
1383 limits[j].max -= num[iftype];
1384 }
1385 }
Johannes Berg463454b2012-06-05 12:16:50 +02001386
Simon Wunderlich11c4a072013-01-08 14:04:07 +01001387 if (radar_detect && !(c->radar_detect_widths & radar_detect))
1388 goto cont;
1389
Johannes Berg463454b2012-06-05 12:16:50 +02001390 /*
1391 * Finally check that all iftypes that we're currently
1392 * using are actually part of this combination. If they
1393 * aren't then we can't use this combination and have
1394 * to continue to the next.
1395 */
1396 if ((all_iftypes & used_iftypes) != used_iftypes)
1397 goto cont;
1398
1399 /*
1400 * This combination covered all interface types and
1401 * supported the requested numbers, so we're good.
1402 */
Johannes Berg7527a782011-05-13 10:58:57 +02001403 kfree(limits);
1404 return 0;
1405 cont:
1406 kfree(limits);
1407 }
1408
1409 return -EBUSY;
1410}
Johannes Berg34850ab2011-07-18 18:08:35 +02001411
1412int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1413 const u8 *rates, unsigned int n_rates,
1414 u32 *mask)
1415{
1416 int i, j;
1417
Johannes Berga401d2b2011-07-20 00:52:16 +02001418 if (!sband)
1419 return -EINVAL;
1420
Johannes Berg34850ab2011-07-18 18:08:35 +02001421 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1422 return -EINVAL;
1423
1424 *mask = 0;
1425
1426 for (i = 0; i < n_rates; i++) {
1427 int rate = (rates[i] & 0x7f) * 5;
1428 bool found = false;
1429
1430 for (j = 0; j < sband->n_bitrates; j++) {
1431 if (sband->bitrates[j].bitrate == rate) {
1432 found = true;
1433 *mask |= BIT(j);
1434 break;
1435 }
1436 }
1437 if (!found)
1438 return -EINVAL;
1439 }
1440
1441 /*
1442 * mask must have at least one bit set here since we
1443 * didn't accept a 0-length rates array nor allowed
1444 * entries in the array that didn't exist
1445 */
1446
1447 return 0;
1448}
Johannes Berg11a2a352011-11-21 11:09:22 +01001449
1450/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1451/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1452const unsigned char rfc1042_header[] __aligned(2) =
1453 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1454EXPORT_SYMBOL(rfc1042_header);
1455
1456/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1457const unsigned char bridge_tunnel_header[] __aligned(2) =
1458 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1459EXPORT_SYMBOL(bridge_tunnel_header);